jitcraft/Opcodes.cpp
changeset 12 75e385f68b7e
parent 8 069d207a8f2a
child 13 627b6b94e774
     1.1 --- a/jitcraft/Opcodes.cpp	Fri Jun 29 19:35:17 2012 -0700
     1.2 +++ b/jitcraft/Opcodes.cpp	Sat Jun 30 19:38:53 2012 -0700
     1.3 @@ -18,6 +18,9 @@
     1.4  #include "Opcodes.h"
     1.5  #include "Utility.h"
     1.6  #include "Code.h"
     1.7 +#include "Zone.h"
     1.8 +#include "Array.h"
     1.9 +#include "Heap-inl.h"
    1.10  
    1.11  using namespace jc;
    1.12  
    1.13 @@ -29,6 +32,31 @@
    1.14  #undef OPDEF
    1.15  };
    1.16  
    1.17 +void
    1.18 +Code::init(ByteArray *bytecode, FixedArray *locals, FixedArray *args,
    1.19 +           FixedArray *maps, unsigned maxStackDepth, Module *module)
    1.20 +{
    1.21 +    WRITE_FIELD_OBJ_NO_BARRIER(bytecode, bytecode);
    1.22 +    WRITE_FIELD_OBJ_NO_BARRIER(locals, locals);
    1.23 +    WRITE_FIELD_OBJ_NO_BARRIER(args, args);
    1.24 +    WRITE_FIELD_OBJ_NO_BARRIER(maps, maps);
    1.25 +    WRITE_FIELD(maxStackDepth, maxStackDepth);
    1.26 +    WRITE_FIELD(module, VoidPtr(module));
    1.27 +}
    1.28 +
    1.29 +Code *
    1.30 +Code::New(Zone *zone, ByteArray *bytecode, FixedArray *locals, FixedArray *args,
    1.31 +          FixedArray *maps, unsigned maxStackDepth, Module *module)
    1.32 +{
    1.33 +    Object *obj = zone->allocate(MapKind_Code, sizeof(Code), Heap::Tenure_Old);
    1.34 +    if (!obj)
    1.35 +        return NULL;
    1.36 +
    1.37 +    Code *code = Code::cast(obj);
    1.38 +    code->init(bytecode, locals, args, maps, maxStackDepth, module);
    1.39 +    return code;
    1.40 +}
    1.41 +
    1.42  // All jumps must be the same length.
    1.43  STATIC_ASSERT(OP_JL_LENGTH == OP_JUMP_LENGTH);
    1.44  STATIC_ASSERT(OP_JLE_LENGTH == OP_JUMP_LENGTH);
    1.45 @@ -99,26 +127,26 @@
    1.46              fprintf(fp, "%u (%+d)", unsigned((pc + READ_INT32(pc)) - code->start()), READ_INT32(pc));
    1.47              break;
    1.48  
    1.49 -          case OP_STACKARRAY:
    1.50 +          case OP_LIFOARRAY:
    1.51            {
    1.52 -            unsigned width = pc[1];
    1.53 -            unsigned dims = pc[2];
    1.54 -
    1.55 -            for (unsigned i = 0; i < dims; i++)
    1.56 -                fprintf(fp, "[%u]", READ_UINT32(pc + 2 + (i * sizeof(unsigned))));
    1.57 -            fprintf(fp, " ; (width = %s)\n", (width == 1 ? "char" : "cell"));
    1.58 -            pc += 3 + dims * sizeof(unsigned);
    1.59 -            continue;
    1.60 +            // :TODO: use Local
    1.61 +            ArrayMap *map = ArrayMap::cast(code->map(READ_UINT32(pc)));
    1.62 +            fprintf(fp, "%s:", map->type()->innermost()->tag()
    1.63 +                               ? map->type()->innermost()->tag()
    1.64 +                               : "_");
    1.65 +            for (; map; map = map->contained()) {
    1.66 +                Type *type = map->type();
    1.67 +                fprintf(fp, "[%d]", type->elements());
    1.68 +            }
    1.69 +            break;
    1.70            }
    1.71  
    1.72            case OP_GETLOCAL:
    1.73            case OP_SETLOCAL:
    1.74            case OP_GETARG:
    1.75            case OP_SETARG:
    1.76 -          case OP_UNWIND:
    1.77 -            fprintf(fp, "%u", READ_UINT32(pc));
    1.78 -            break;
    1.79 -
    1.80 +          case OP_SAVELIFO:
    1.81 +          case OP_UNWINDLIFO:
    1.82            case OP_CALLGLOBAL:
    1.83              fprintf(fp, "%u", READ_UINT32(pc));
    1.84              break;
    1.85 @@ -131,5 +159,6 @@
    1.86  
    1.87          fprintf(fp, "\n");
    1.88          pc += OpcodeInfo[op].length;
    1.89 +        assert(pc < code->start() + code->length());
    1.90      }
    1.91  }