jitcraft/Stack.h
changeset 65 7c942b5adebc
parent 62 e04c5be84792
child 68 b40b0e18a918
     1.1 --- a/jitcraft/Stack.h	Sun Jul 08 17:57:44 2012 -0700
     1.2 +++ b/jitcraft/Stack.h	Mon Jul 09 01:22:03 2012 -0700
     1.3 @@ -31,6 +31,7 @@
     1.4  class Array;
     1.5  class Reference;
     1.6  class Frame;
     1.7 +class Stack;
     1.8      
     1.9  union LocalSlot
    1.10  {
    1.11 @@ -47,16 +48,6 @@
    1.12      LocalSlot *sp;
    1.13      bytecode *pc;
    1.14      Frame *fp;
    1.15 -
    1.16 -    Top(LocalSlot *sp, bytecode *pc, Frame *fp)
    1.17 -      : sp(sp), pc(pc), fp(NULL)
    1.18 -    {
    1.19 -    }
    1.20 -
    1.21 -    Top(LocalSlot *sp)
    1.22 -      : sp(sp), pc(NULL), fp(NULL)
    1.23 -    {
    1.24 -    }
    1.25  };
    1.26  
    1.27  class Frame
    1.28 @@ -111,11 +102,34 @@
    1.29      void accept(VirtualObjectVisitor *visitor);
    1.30  };
    1.31  
    1.32 +// RAII object for calling a function from C++.
    1.33 +class InvokeArgs
    1.34 +{
    1.35 +    friend class Stack;
    1.36 +
    1.37 +    Stack *stack_;
    1.38 +    LocalSlot *args_;
    1.39 +    unsigned nargs_;
    1.40 +
    1.41 +  public:
    1.42 +    InvokeArgs()
    1.43 +      : stack_(NULL),
    1.44 +        args_(NULL),
    1.45 +        nargs_(0)
    1.46 +    {
    1.47 +    }
    1.48 +
    1.49 +    ~InvokeArgs();
    1.50 +
    1.51 +    LocalSlot &arg(unsigned index) {
    1.52 +        assert(index < nargs_);
    1.53 +        return args_[nargs_ - index - 1];
    1.54 +    }
    1.55 +};
    1.56 +
    1.57  class Stack
    1.58  {
    1.59 -    char *buffer_;
    1.60 -    char *end_;
    1.61 -    Top *top_;
    1.62 +    friend class InvokeArgs;
    1.63  
    1.64    private:
    1.65      static const size_t STACK_SIZE = 1024 * 1024;
    1.66 @@ -127,20 +141,24 @@
    1.67      bool initialize();
    1.68  
    1.69    public:
    1.70 -    Frame *pushFrame(Function *fun);
    1.71 -    Frame *popFrame();
    1.72 +    Frame *pushInlineFrame(Handle<Function> fun);
    1.73 +    Frame *popInlineFrame();
    1.74 +
    1.75 +    bool pushInvokeFrame(Handle<Function> fun, InvokeArgs &args);
    1.76  
    1.77      Top *top() {
    1.78 -        return top_;
    1.79 -    }
    1.80 -    void setTop(Top *top) {
    1.81 -        top_ = top;
    1.82 -    }
    1.83 -    Top begin() {
    1.84 -        return Top((LocalSlot *)buffer_);
    1.85 +        return &top_;
    1.86      }
    1.87      bool isInCallStack(Handle<Module> module);
    1.88      void accept(VirtualObjectVisitor *visitor);
    1.89 +
    1.90 +  private:
    1.91 +    void popInvokeFrame(InvokeArgs &args);
    1.92 +
    1.93 +  private:
    1.94 +    char *buffer_;
    1.95 +    char *end_;
    1.96 +    Top top_;
    1.97  };
    1.98  
    1.99  }