Handle-ize Code.
2 * Copyright (C) 2012 David Anderson
4 * This file is part of JITCraft.
6 * JITCraft is free software: you can redistribute it and/or modify it under
7 * the terms of the GNU General Public License as published by the Free
8 * Software Foundation, either version 3 of the License, or (at your option)
11 * Foobar is distributed in the hope that it will be useful, but WITHOUT ANY
12 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along with
16 * JITCraft. If not, see http://www.gnu.org/licenses/.
27 OpInfo jc::OpcodeInfo[] =
29 #define OPDEF(name, number, text, length, uses, defs) \
30 {text, length, uses, defs},
31 #include "Opcodes.tbl"
36 Code::New(Zone *zone, Handle<ByteArray> bytecode, Handle<FixedArray> locals,
37 Handle<FixedArray> objects, unsigned maxStackDepth, Handle<Module> module)
39 Local<Code> code(zone, Code::cast(zone->allocate(MapKind_Code, sizeof(Code), Heap::Tenure_Old)));
43 code->bytecode_ = bytecode;
44 code->locals_ = locals;
45 code->objects_ = objects;
46 code->maxStackDepth_ = maxStackDepth;
47 code->module_ = module;
51 // All jumps must be the same length.
52 STATIC_ASSERT(OP_JL_LENGTH == OP_JUMP_LENGTH);
53 STATIC_ASSERT(OP_JLE_LENGTH == OP_JUMP_LENGTH);
54 STATIC_ASSERT(OP_JG_LENGTH == OP_JUMP_LENGTH);
55 STATIC_ASSERT(OP_JGE_LENGTH == OP_JUMP_LENGTH);
56 STATIC_ASSERT(OP_JEQ_LENGTH == OP_JUMP_LENGTH);
57 STATIC_ASSERT(OP_JNE_LENGTH == OP_JUMP_LENGTH);
58 STATIC_ASSERT(OP_JT_LENGTH == OP_JUMP_LENGTH);
59 STATIC_ASSERT(OP_JF_LENGTH == OP_JUMP_LENGTH);
62 jc::Disassemble(Code *code, FILE *fp)
64 bytecode *pc = code->start();
66 Opcode op = Opcode(*pc);
68 fprintf(fp, "%-4d %s ", int(pc - code->start()), OpcodeInfo[op].name);
101 case OP_SLOTREF_LIFO:
102 case OP_ARRAYREF_LIFO:
118 fprintf(fp, "%d", READ_INT32(pc));
122 fprintf(fp, "%f", READ_FLOAT(pc));
134 fprintf(fp, "%u (%+d)", unsigned((pc + READ_INT32(pc)) - code->start()), READ_INT32(pc));
137 case OP_NEWARRAY_LIFO:
140 ArrayMap *map = ArrayMap::cast(code->map(READ_UINT32(pc)));
141 fprintf(fp, "%s:", map->type()->innermost()->tag()
142 ? map->type()->innermost()->tag()
144 for (; map; map = map->contained()) {
145 Type *type = map->type();
146 fprintf(fp, "[%d]", type->elements());
160 fprintf(fp, "%u", READ_UINT32(pc));
170 pc += OpcodeInfo[op].length;
171 assert(pc < code->start() + code->length());