Added global variable support.
1 /* vim: set ts=4 sw=4 tw=99 et:
3 * Copyright (C) 2012 David Anderson
5 * This file is part of JITCraft.
7 * JITCraft is free software: you can redistribute it and/or modify it under
8 * the terms of the GNU General Public License as published by the Free
9 * Software Foundation, either version 3 of the License, or (at your option)
12 * Foobar is distributed in the hope that it will be useful, but WITHOUT ANY
13 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
14 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along with
17 * JITCraft. If not, see http://www.gnu.org/licenses/.
26 #include "CompactBuffer.h"
31 OpInfo ke::OpcodeInfo[] =
33 #define OPDEF(name, number, text, length, uses, defs) \
34 {text, length, uses, defs},
35 #include "Opcodes.tbl"
40 Code::New(Zone *zone, Handle<ByteArray> bytecode, Handle<FixedArray> locals,
41 Handle<FixedArray> objects, unsigned maxStackDepth, Handle<Module> module,
42 Handle<ByteArray> safepoints, Handle<ByteArray> sourcemap,
45 Local<Code> code(zone, Code::cast(zone->allocate(MapKind_Code, sizeof(Code), Heap::Tenure_Old)));
49 code->bytecode_ = bytecode;
50 code->locals_ = locals;
51 code->objects_ = objects;
52 code->maxStackDepth_ = maxStackDepth;
53 code->module_ = module;
54 code->safepoints_ = safepoints;
55 code->sourcemap_ = sourcemap;
56 code->lineNumber_ = lineNumber;
61 CompareSafepoints(const void *a, const void *b)
63 const unsigned *sa = reinterpret_cast<const unsigned *>(a);
64 const unsigned *sb = reinterpret_cast<const unsigned *>(b);
73 Code::safepointAt(unsigned pcOffset)
75 // Only specific opcodes may have safepoints. Asking for a safepoint at
76 // any other position indicates a failure to mark that position as a
77 // safepoint, which is a bug.
78 Opcode op = Opcode(*(start() + pcOffset));
79 assert(op == OP_CALL || op == OP_NEWARRAY || op == OP_VARREF_LIFO ||
80 op == OP_ARGREF_LIFO || op == OP_SLOTREF_LIFO || op == OP_ARRAYREF_LIFO ||
86 size_t bytesPerSafepoint = BytesPerSafepoint(maxStackDepth());
87 assert(!(safepoints_->length() % bytesPerSafepoint));
89 size_t count = safepoints_->length() / bytesPerSafepoint;
91 void *result = bsearch(&pcOffset,
100 assert(*reinterpret_cast<unsigned *>(result) == pcOffset);
102 return Address(result) + sizeof(unsigned);
106 Code::pcToSource(unsigned pc)
108 assert(pc < bytecode_->length());
110 CompactBufferReader reader(sourcemap_->raw(), sourcemap_->raw() + sourcemap_->length());
115 while (!reader.done()) {
116 curPc += reader.readUint32();
120 line = reader.readUint32();
121 col = reader.readUint32();
124 return SourcePosition(lineNumber_ + line, col);
127 // All jumps must be the same length.
128 STATIC_ASSERT(OP_JL_LENGTH == OP_JUMP_LENGTH);
129 STATIC_ASSERT(OP_JLE_LENGTH == OP_JUMP_LENGTH);
130 STATIC_ASSERT(OP_JG_LENGTH == OP_JUMP_LENGTH);
131 STATIC_ASSERT(OP_JGE_LENGTH == OP_JUMP_LENGTH);
132 STATIC_ASSERT(OP_JEQ_LENGTH == OP_JUMP_LENGTH);
133 STATIC_ASSERT(OP_JNE_LENGTH == OP_JUMP_LENGTH);
134 STATIC_ASSERT(OP_JT_LENGTH == OP_JUMP_LENGTH);
135 STATIC_ASSERT(OP_JF_LENGTH == OP_JUMP_LENGTH);
138 ke::Disassemble(Code *code, FILE *fp)
140 bytecode *pc = code->start();
142 Opcode op = Opcode(*pc);
144 fprintf(fp, "%-4d %s ", int(pc - code->start()), OpcodeInfo[op].name);
178 case OP_SLOTREF_LIFO:
179 case OP_ARRAYREF_LIFO:
214 fprintf(fp, "%d", READ_INT32(pc));
218 fprintf(fp, "%f", READ_FLOAT(pc));
238 fprintf(fp, "%u (%+d)", unsigned((pc + READ_INT32(pc)) - code->start()), READ_INT32(pc));
243 Heap::Tenure tenure = (Heap::Tenure)pc[1];
244 ArrayMap *map = ArrayMap::cast(code->map(READ_UINT32(pc + 1)));
245 fprintf(fp, "%s:", map->type()->innermost()->tag()
246 ? map->type()->innermost()->tag()->chars()
248 for (; map; map = map->contained()) {
249 Type *type = map->type();
250 fprintf(fp, "[%d]", type->elements());
252 if (tenure == Heap::Tenure_Stack)
253 fprintf(fp, " ; lifo");
254 else if (tenure == Heap::Tenure_Old)
255 fprintf(fp, " ; pretenured");
268 case OP_GETMODULE_FN:
275 fprintf(fp, "%u", READ_UINT32(pc));
279 fprintf(fp, "%u %u", READ_UINT32(pc), READ_UINT32(pc + 4));
292 pc += OpcodeInfo[op].length;
293 assert(pc < code->start() + code->length());
296 fprintf(fp, "\nSafepoints:\n");
297 ByteArray *safepoints = code->safepoints();
299 size_t nsafepoints = safepoints->length() / Code::BytesPerSafepoint(code->maxStackDepth());
300 for (size_t i = 0; i < nsafepoints; i++) {
301 Address address = safepoints->raw() + Code::BytesPerSafepoint(code->maxStackDepth()) * i;
302 fprintf(fp, "%4d: ", *reinterpret_cast<unsigned *>(address));
303 unsigned char *bits = (unsigned char *)(address + sizeof(unsigned));
304 for (unsigned j = 0; j < code->maxStackDepth(); j++) {
305 if (bits[j / 8] & (1 << (j % 8)))
306 fprintf(fp, "%d ", j);