Added support for declaring empty-length arrays.
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"
27 #include "Structures.h"
32 OpInfo ke::OpcodeInfo[] =
34 #define OPDEF(name, number, text, length, uses, defs) \
35 {text, length, uses, defs},
36 #include "Opcodes.tbl"
41 Code::New(Zone *zone, Handle<ByteArray> bytecode, Handle<FixedArray> locals,
42 Handle<FixedArray> objects, unsigned maxStackDepth, Handle<Module> module,
43 Handle<ByteArray> safepoints, Handle<ByteArray> sourcemap,
46 Local<Code> code(zone, Code::cast(zone->allocate(MapKind_Code, sizeof(Code), Heap::Tenure_Old)));
50 code->bytecode_ = bytecode;
51 code->locals_ = locals;
52 code->objects_ = objects;
53 code->maxStackDepth_ = maxStackDepth;
54 code->module_ = module;
55 code->safepoints_ = safepoints;
56 code->sourcemap_ = sourcemap;
57 code->lineNumber_ = lineNumber;
62 CompareSafepoints(const void *a, const void *b)
64 const unsigned *sa = reinterpret_cast<const unsigned *>(a);
65 const unsigned *sb = reinterpret_cast<const unsigned *>(b);
74 Code::safepointAt(unsigned pcOffset)
76 // Only specific opcodes may have safepoints. Asking for a safepoint at
77 // any other position indicates a failure to mark that position as a
78 // safepoint, which is a bug.
80 Opcode op = Opcode(*(start() + pcOffset));
81 assert(op == OP_CALL ||
85 op == OP_VARREF_LIFO ||
86 op == OP_ARGREF_LIFO ||
87 op == OP_SLOTREF_LIFO ||
88 op == OP_ARRAYREF_LIFO ||
95 size_t bytesPerSafepoint = BytesPerSafepoint(maxStackDepth());
96 assert(!(safepoints_->length() % bytesPerSafepoint));
98 size_t count = safepoints_->length() / bytesPerSafepoint;
100 void *result = bsearch(&pcOffset,
109 assert(*reinterpret_cast<unsigned *>(result) == pcOffset);
111 return Address(result) + sizeof(unsigned);
115 Code::pcToSource(unsigned pc)
117 assert(pc < bytecode_->length());
119 CompactBufferReader reader(sourcemap_->raw(), sourcemap_->raw() + sourcemap_->length());
124 while (!reader.done()) {
125 curPc += reader.readUint32();
129 line = reader.readUint32();
130 col = reader.readUint32();
133 return SourcePosition(lineNumber_ + line, col);
136 // All jumps must be the same length.
137 STATIC_ASSERT(OP_JL_LENGTH == OP_JUMP_LENGTH);
138 STATIC_ASSERT(OP_JLE_LENGTH == OP_JUMP_LENGTH);
139 STATIC_ASSERT(OP_JG_LENGTH == OP_JUMP_LENGTH);
140 STATIC_ASSERT(OP_JGE_LENGTH == OP_JUMP_LENGTH);
141 STATIC_ASSERT(OP_JEQ_LENGTH == OP_JUMP_LENGTH);
142 STATIC_ASSERT(OP_JNE_LENGTH == OP_JUMP_LENGTH);
143 STATIC_ASSERT(OP_JT_LENGTH == OP_JUMP_LENGTH);
144 STATIC_ASSERT(OP_JF_LENGTH == OP_JUMP_LENGTH);
147 ke::Disassemble(Code *code, FILE *fp)
149 bytecode *pc = code->start();
151 Opcode op = Opcode(*pc);
153 fprintf(fp, "%-4d %s ", int(pc - code->start()), OpcodeInfo[op].name);
181 case OP_SLOTREF_LIFO:
182 case OP_ARRAYREF_LIFO:
222 fprintf(fp, "%d", READ_INT32(pc));
226 fprintf(fp, "%f", READ_FLOAT(pc));
246 fprintf(fp, "%u (%+d)", unsigned((pc + READ_INT32(pc)) - code->start()), READ_INT32(pc));
253 Heap::Tenure tenure = (Heap::Tenure)pc[1];
254 //ArrayMap *map = ArrayMap::cast(code->map(READ_UINT32(pc + 1)));
255 if (tenure == Heap::Tenure_Stack)
256 fprintf(fp, " ; lifo");
257 else if (tenure == Heap::Tenure_Old)
258 fprintf(fp, " ; pretenured");
259 else if (tenure == Heap::Tenure_Default)
260 fprintf(fp, " ; newborn");
266 Heap::Tenure tenure = (Heap::Tenure)pc[1];
267 StructMap *map = StructMap::cast(code->map(READ_UINT32(pc + 1)));
268 fprintf(fp, "%s", map->type()->name()->chars());
269 if (tenure == Heap::Tenure_Stack)
270 fprintf(fp, " ; lifo");
271 else if (tenure == Heap::Tenure_Old)
272 fprintf(fp, " ; pretenured");
273 else if (tenure == Heap::Tenure_Default)
274 fprintf(fp, " ; newborn");
287 case OP_GETMODULE_FN:
296 case OP_FIELDREF_LIFO:
297 fprintf(fp, "%u", READ_UINT32(pc));
301 fprintf(fp, "%u %u", READ_UINT32(pc), READ_UINT32(pc + 4));
306 int low = READ_TABLESWITCH_LOW(pc);
307 int high = READ_TABLESWITCH_HIGH(pc);
308 fprintf(fp, "%d, %d\n", low, high);
322 pc += OpcodeInfo[op].length;
323 assert(pc < code->start() + code->length());
326 fprintf(fp, "\nSafepoints:\n");
327 ByteArray *safepoints = code->safepoints();
329 size_t nsafepoints = safepoints->length() / Code::BytesPerSafepoint(code->maxStackDepth());
330 for (size_t i = 0; i < nsafepoints; i++) {
331 Address address = safepoints->raw() + Code::BytesPerSafepoint(code->maxStackDepth()) * i;
332 fprintf(fp, "%4d: ", *reinterpret_cast<unsigned *>(address));
333 unsigned char *bits = (unsigned char *)(address + sizeof(unsigned));
334 for (unsigned j = 0; j < code->maxStackDepth(); j++) {
335 if (bits[j / 8] & (1 << (j % 8)))
336 fprintf(fp, "%d ", j);