Small refactoring to make callees pushed explicitly.
authorDavid Anderson <dvander@alliedmods.net>
Sun Jul 15 14:40:11 2012 -0700 (2012-07-15)
changeset 73ef5492aa8d8b
parent 72 40746ab0a30b
child 74 80e29e23eb4e
Small refactoring to make callees pushed explicitly.
jitcraft/BytecodeCompiler.cpp
jitcraft/BytecodeEmitter.h
jitcraft/Interpreter.cpp
jitcraft/Opcodes.cpp
jitcraft/Opcodes.tbl
jitcraft/Stack.h
jitcraft/jitcraft.vcxproj
     1.1 --- a/jitcraft/BytecodeCompiler.cpp	Sat Jul 14 21:01:52 2012 -0700
     1.2 +++ b/jitcraft/BytecodeCompiler.cpp	Sun Jul 15 14:40:11 2012 -0700
     1.3 @@ -149,6 +149,14 @@
     1.4          break;
     1.5        }
     1.6  
     1.7 +      case BoundName::FUNCTION:
     1.8 +      {
     1.9 +        NamedFunction *fun = name->binding()->toNamedFunction();
    1.10 +        __ module();
    1.11 +        __ getmodule_fn(fun->slot());
    1.12 +        break;
    1.13 +      }
    1.14 +
    1.15        default:
    1.16        {
    1.17          NamedConstant *constant = name->binding()->toNamedConstant();
    1.18 @@ -608,6 +616,13 @@
    1.19  {
    1.20      Local<Type> funType(zone_, node->callee()->type());
    1.21  
    1.22 +    // Sniff out natives, which have a specific opcode and are guaranteed to
    1.23 +    // be called via name proxy.
    1.24 +    NamedFunction *native = NULL;
    1.25 +    if (funType->isNative()) {
    1.26 +        native = node->callee()->toNameProxy()->binding()->toNamedFunction();
    1.27 +    }
    1.28 +
    1.29      for (size_t i = node->arguments()->length() - 1; i < node->arguments()->length(); i--) {
    1.30          Expression *arg = node->arguments()->at(i);
    1.31  
    1.32 @@ -657,14 +672,15 @@
    1.33          }
    1.34      }
    1.35  
    1.36 -    NamedFunction *fun = node->callee()->toNameProxy()->binding()->toNamedFunction();
    1.37 -
    1.38      __ note_position(node->pos());
    1.39 -    if (fun->isNative())
    1.40 -        __ callnative(fun->slot(), node->arguments()->length());
    1.41 -    else
    1.42 -        __ callglobal(funType, fun->slot());
    1.43 -    __ drop(node->arguments()->length());
    1.44 +    if (native) {
    1.45 +        __ callnative(native->slot(), node->arguments()->length());
    1.46 +        __ drop(node->arguments()->length());
    1.47 +    } else {
    1.48 +        node->callee()->accept(this);
    1.49 +        __ call(funType);
    1.50 +        __ drop(node->arguments()->length() + 1);
    1.51 +    }
    1.52  }
    1.53  
    1.54  void
     2.1 --- a/jitcraft/BytecodeEmitter.h	Sat Jul 14 21:01:52 2012 -0700
     2.2 +++ b/jitcraft/BytecodeEmitter.h	Sun Jul 15 14:40:11 2012 -0700
     2.3 @@ -347,11 +347,10 @@
     2.4      void stop() {
     2.5          singleByteOp(OP_STOP);
     2.6      }
     2.7 -    void callglobal(Handle<Type> fun, unsigned index) {
     2.8 +    void call(Handle<Type> fun) {
     2.9          recordOops();
    2.10 -        ensure(OP_CALLGLOBAL_LENGTH);
    2.11 -        writeOp(OP_CALLGLOBAL);
    2.12 -        writeUint32(index);
    2.13 +        ensure(OP_CALL_LENGTH);
    2.14 +        writeOp(OP_CALL);
    2.15  
    2.16          if (fun->returnType()->isVoid())
    2.17              drop(1);
    2.18 @@ -537,6 +536,16 @@
    2.19      void returnvoid() {
    2.20          singleByteOp(OP_RETURNVOID);
    2.21      }
    2.22 +    void module() {
    2.23 +        singleByteOp(OP_MODULE);
    2.24 +        markOop(1);
    2.25 +    }
    2.26 +    void getmodule_fn(unsigned index) {
    2.27 +        ensure(OP_GETMODULE_FN_LENGTH);
    2.28 +        writeOp(OP_GETMODULE_FN);
    2.29 +        writeUint32(index);
    2.30 +        markOop(1);
    2.31 +    }
    2.32  
    2.33      // Helpers.
    2.34      void get(Variable *var) {
     3.1 --- a/jitcraft/Interpreter.cpp	Sat Jul 14 21:01:52 2012 -0700
     3.2 +++ b/jitcraft/Interpreter.cpp	Sun Jul 15 14:40:11 2012 -0700
     3.3 @@ -407,11 +407,13 @@
     3.4      top.sp--;
     3.5      break;
     3.6  
     3.7 -case OP_CALLGLOBAL:
     3.8 +case OP_CALL:
     3.9  {
    3.10      zone->heap().reclaim();
    3.11 -    unsigned index = READ_UINT32(top.pc);
    3.12 -    Local<Function> newFun(zone, top.fp->code()->module()->getFunction(index));
    3.13 +
    3.14 +    Local<Function> newFun(zone, top.sp[-1].fun);
    3.15 +    top.sp--;
    3.16 +
    3.17      if (!zone->stack().pushInlineFrame(newFun))
    3.18          goto error;
    3.19  
    3.20 @@ -690,6 +692,19 @@
    3.21      break;
    3.22  }
    3.23  
    3.24 +case OP_MODULE:
    3.25 +{
    3.26 +    top.sp++;
    3.27 +    top.sp[-1].module = top.fp->code()->module();
    3.28 +    break;
    3.29 +}
    3.30 +
    3.31 +case OP_GETMODULE_FN:
    3.32 +{
    3.33 +    top.sp[-1].fun = top.sp[-1].module->getFunction(READ_UINT32(top.pc));
    3.34 +    break;
    3.35 +}
    3.36 +
    3.37  default:
    3.38      assert(false);
    3.39      return false;
     4.1 --- a/jitcraft/Opcodes.cpp	Sat Jul 14 21:01:52 2012 -0700
     4.2 +++ b/jitcraft/Opcodes.cpp	Sun Jul 15 14:40:11 2012 -0700
     4.3 @@ -76,7 +76,7 @@
     4.4      // any other position indicates a failure to mark that position as a
     4.5      // safepoint, which is a bug.
     4.6      Opcode op = Opcode(*(start() + pcOffset));
     4.7 -    assert(op == OP_CALLGLOBAL || op == OP_NEWARRAY_LIFO || op == OP_VARREF_LIFO ||
     4.8 +    assert(op == OP_CALL || op == OP_NEWARRAY_LIFO || op == OP_VARREF_LIFO ||
     4.9             op == OP_ARGREF_LIFO || op == OP_SLOTREF_LIFO || op == OP_ARRAYREF_LIFO ||
    4.10             op == OP_ARRAYCOPY);
    4.11  
    4.12 @@ -184,6 +184,8 @@
    4.13            case OP_ROLL3:
    4.14            case OP_PICK2:
    4.15            case OP_PICK3:
    4.16 +          case OP_CALL:
    4.17 +          case OP_MODULE:
    4.18              break;
    4.19  
    4.20            case OP_STOP:
    4.21 @@ -229,10 +231,10 @@
    4.22            case OP_SETARG:
    4.23            case OP_SAVELIFO:
    4.24            case OP_UNWINDLIFO:
    4.25 -          case OP_CALLGLOBAL:
    4.26            case OP_VARREF_LIFO:
    4.27            case OP_ARGREF_LIFO:
    4.28            case OP_OBJECT:
    4.29 +          case OP_GETMODULE_FN:
    4.30              fprintf(fp, "%u", READ_UINT32(pc));
    4.31              break;
    4.32  
     5.1 --- a/jitcraft/Opcodes.tbl	Sat Jul 14 21:01:52 2012 -0700
     5.2 +++ b/jitcraft/Opcodes.tbl	Sun Jul 15 14:40:11 2012 -0700
     5.3 @@ -75,11 +75,9 @@
     5.4  // void, this acts as a RETURNVOID. Otherwise, it must be unreachable.
     5.5  OPDEF(OP_STOP,          19,         "stop",         1,      0,  0)
     5.6  
     5.7 -// Call a function with N arguments.
     5.8 -//
     5.9 -// CALLGLOBAL - followed by an extra immediate, containing the function index
    5.10 -// in the code's global.
    5.11 -OPDEF(OP_CALLGLOBAL,    20,         "callglobal",   5,      -1, 1)
    5.12 +// Call a function with N arguments. The target function must be the last item
    5.13 +// on the stack.
    5.14 +OPDEF(OP_CALL,          20,         "call",         1,      -1, 1)
    5.15  
    5.16  // Similar to GET/LOCAL, except for arguments.
    5.17  OPDEF(OP_GETARG,        21,         "getarg",       5,      0,  1)
    5.18 @@ -199,3 +197,10 @@
    5.19  
    5.20  // Returns from a function that does return a value.
    5.21  OPDEF(OP_RETURNVOID,    63,         "returnvoid",   1,      0, 0)
    5.22 +
    5.23 +// Loads the module of the running function.
    5.24 +OPDEF(OP_MODULE,        64,         "module",       1,      0, 1)
    5.25 +
    5.26 +// Loads a function from a module object. The operand is the slot number
    5.27 +// in the module.
    5.28 +OPDEF(OP_GETMODULE_FN,  65,         "getmodule.fn", 5,      1, 1)
     6.1 --- a/jitcraft/Stack.h	Sat Jul 14 21:01:52 2012 -0700
     6.2 +++ b/jitcraft/Stack.h	Sun Jul 15 14:40:11 2012 -0700
     6.3 @@ -33,6 +33,7 @@
     6.4  class Frame;
     6.5  class Stack;
     6.6  class Zone;
     6.7 +class Module;
     6.8      
     6.9  union LocalSlot
    6.10  {
    6.11 @@ -42,6 +43,8 @@
    6.12      Object *obj;
    6.13      Array *array;
    6.14      Reference *ref;
    6.15 +    Function *fun;
    6.16 +    Module *module;
    6.17  };
    6.18  
    6.19  struct Top
     7.1 --- a/jitcraft/jitcraft.vcxproj	Sat Jul 14 21:01:52 2012 -0700
     7.2 +++ b/jitcraft/jitcraft.vcxproj	Sun Jul 15 14:40:11 2012 -0700
     7.3 @@ -166,6 +166,7 @@
     7.4      <ClInclude Include="Stack.h" />
     7.5      <ClInclude Include="Strings.h" />
     7.6      <ClInclude Include="StringPool.h" />
     7.7 +    <ClInclude Include="Structure.h" />
     7.8      <ClInclude Include="TypeManager.h" />
     7.9      <ClInclude Include="Types.h" />
    7.10      <ClInclude Include="Vector.h" />