jitcraft/Stack.h
changeset 68 b40b0e18a918
parent 65 7c942b5adebc
child 69 dfce0539a77d
     1.1 --- a/jitcraft/Stack.h	Mon Jul 09 22:25:52 2012 -0700
     1.2 +++ b/jitcraft/Stack.h	Tue Jul 10 02:11:13 2012 -0700
     1.3 @@ -56,7 +56,11 @@
     1.4  
     1.5      Frame *prev_;
     1.6      bytecode *prevpc_;
     1.7 -    Function *fun_;
     1.8 +    union {
     1.9 +        Function *fun_;
    1.10 +        Native *native_;
    1.11 +        Object *callee_;
    1.12 +    };
    1.13      Code *code_;
    1.14      Address lifoPosition_;
    1.15  
    1.16 @@ -70,6 +74,15 @@
    1.17      {
    1.18      }
    1.19  
    1.20 +    Frame(Frame *prev, bytecode *prevpc, Native *native, Address lifoPosition)
    1.21 +      : prev_(prev),
    1.22 +        prevpc_(prevpc),
    1.23 +        native_(native),
    1.24 +        code_(NULL),
    1.25 +        lifoPosition_(lifoPosition)
    1.26 +    {
    1.27 +    }
    1.28 +
    1.29      LocalSlot *locals() {
    1.30          return reinterpret_cast<LocalSlot *>(this + 1);
    1.31      }
    1.32 @@ -77,13 +90,17 @@
    1.33          return locals() + code_->nlocals();
    1.34      }
    1.35      Function *fun() const {
    1.36 -        assert(fun_);
    1.37 +        assert(fun_ && callee_->is(MapKind_Function));
    1.38          return fun_;
    1.39      }
    1.40      Function *maybeFun() const {
    1.41 -        return fun_;
    1.42 +        return (fun_ && callee_->is(MapKind_Function)) ? fun_ : NULL;
    1.43 +    }
    1.44 +    bool isNative() const {
    1.45 +        return callee_ && callee_->is(MapKind_Native);
    1.46      }
    1.47      Code *code() const {
    1.48 +        assert(code_);
    1.49          return code_;
    1.50      }
    1.51      Frame *prev() const {
    1.52 @@ -127,9 +144,32 @@
    1.53      }
    1.54  };
    1.55  
    1.56 +// RAII object for calling a native from C++.
    1.57 +class NativeArgs
    1.58 +{
    1.59 +    friend class Stack;
    1.60 +
    1.61 +    Stack *stack_;
    1.62 +    LocalSlot *argsBase_;
    1.63 +
    1.64 +  public:
    1.65 +    NativeArgs()
    1.66 +      : stack_(NULL),
    1.67 +        argsBase_(NULL)
    1.68 +    {
    1.69 +    }
    1.70 +
    1.71 +    ~NativeArgs();
    1.72 +
    1.73 +    intptr_t *args() {
    1.74 +        return reinterpret_cast<int *>(argsBase_);
    1.75 +    }
    1.76 +};
    1.77 +
    1.78  class Stack
    1.79  {
    1.80      friend class InvokeArgs;
    1.81 +    friend class NativeArgs;
    1.82  
    1.83    private:
    1.84      static const size_t STACK_SIZE = 1024 * 1024;
    1.85 @@ -145,6 +185,7 @@
    1.86      Frame *popInlineFrame();
    1.87  
    1.88      bool pushInvokeFrame(Handle<Function> fun, InvokeArgs &args);
    1.89 +    bool pushNativeFrame(Handle<Native> native, NativeArgs &args, unsigned argc);
    1.90  
    1.91      Top *top() {
    1.92          return &top_;
    1.93 @@ -154,6 +195,7 @@
    1.94  
    1.95    private:
    1.96      void popInvokeFrame(InvokeArgs &args);
    1.97 +    void popNativeFrame(NativeArgs &args);
    1.98  
    1.99    private:
   1.100      char *buffer_;