Fix typo.
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, 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, Handle<FixedArray> strings, unsigned maxStackDepth,
43 unsigned maxLoopDepth, 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;
54 code->safepoints_ = safepoints;
55 code->sourcemap_ = sourcemap;
56 code->lineNumber_ = 0;
57 code->strings_ = strings;
58 code->maxLoopDepth_ = maxLoopDepth;
63 CompareSafepoints(const void *a, const void *b)
65 const unsigned *sa = reinterpret_cast<const unsigned *>(a);
66 const unsigned *sb = reinterpret_cast<const unsigned *>(b);
75 Code::safepointAt(unsigned pcOffset)
77 // Only specific opcodes may have safepoints. Asking for a safepoint at
78 // any other position indicates a failure to mark that position as a
79 // safepoint, which is a bug.
81 Opcode op = Opcode(*(start() + pcOffset));
82 assert(op == OP_CALL ||
86 op == OP_VARREF_LIFO ||
87 op == OP_ARGREF_LIFO ||
88 op == OP_SLOTREF_LIFO ||
90 op == OP_CALLNATIVE ||
97 size_t bytesPerSafepoint = BytesPerSafepoint(maxStackDepth());
98 assert(!(safepoints_->length() % bytesPerSafepoint));
100 size_t count = safepoints_->length() / bytesPerSafepoint;
102 void *result = bsearch(&pcOffset,
111 assert(*reinterpret_cast<unsigned *>(result) == pcOffset);
113 return Address(result) + sizeof(unsigned);
117 Code::pcToSource(unsigned pc)
119 assert(pc < bytecode_->length());
121 CompactBufferReader reader(sourcemap_->raw(), sourcemap_->raw() + sourcemap_->length());
126 while (!reader.done()) {
127 curPc += reader.readUint32();
131 line = reader.readUint32();
132 col = reader.readUint32();
135 return SourcePosition(lineNumber_ + line, col);
138 // All jumps must be the same length.
139 STATIC_ASSERT(OP_JT_LENGTH == OP_JUMP_LENGTH);
140 STATIC_ASSERT(OP_JF_LENGTH == OP_JUMP_LENGTH);
143 ke::Disassemble(Code *code, FILE *fp)
145 bytecode *pc = code->start();
147 Opcode op = Opcode(*pc);
149 fprintf(fp, "%-4d %s ", int(pc - code->start()), OpcodeInfo[op].name);
178 case OP_SLOTREF_LIFO:
218 fprintf(fp, "; lifo region");
226 fprintf(fp, "%d", READ_INT32(pc));
230 fprintf(fp, "%f", READ_FLOAT(pc));
238 fprintf(fp, "%u (%+d)", unsigned((pc + READ_INT32(pc)) - code->start()), READ_INT32(pc));
245 Heap::Tenure tenure = (Heap::Tenure)pc[1];
246 //ArrayMap *map = ArrayMap::cast(code->map(READ_UINT32(pc + 1)));
247 if (tenure == Heap::Tenure_Stack)
248 fprintf(fp, " ; lifo");
249 else if (tenure == Heap::Tenure_Old)
250 fprintf(fp, " ; pretenured");
251 else if (tenure == Heap::Tenure_Default)
252 fprintf(fp, " ; newborn");
258 Heap::Tenure tenure = (Heap::Tenure)pc[1];
259 StructMap *map = StructMap::cast(code->map(READ_UINT32(pc + 1)));
260 fprintf(fp, "%s", StructType::cast(map->type())->name()->chars());
261 if (tenure == Heap::Tenure_Stack)
262 fprintf(fp, " ; lifo");
263 else if (tenure == Heap::Tenure_Old)
264 fprintf(fp, " ; pretenured");
265 else if (tenure == Heap::Tenure_Default)
266 fprintf(fp, " ; newborn");
281 fprintf(fp, "%u", READ_UINT32(pc));
286 unsigned argc = READ_UINT32(pc);
287 fprintf(fp, " ; %u args", argc);
293 int low = READ_TABLESWITCH_LOW(pc);
294 int high = READ_TABLESWITCH_HIGH(pc);
295 fprintf(fp, "%d, %d\n", low, high);
302 unsigned index = READ_UINT32(pc);
303 fprintf(fp, "%s", String::cast(code->strings()->at(index))->chars());
317 pc += OpcodeInfo[op].length;
318 assert(pc < code->start() + code->length());
321 fprintf(fp, "\nSafepoints:\n");
322 ByteArray *safepoints = code->safepoints();
324 size_t nsafepoints = safepoints->length() / Code::BytesPerSafepoint(code->maxStackDepth());
325 for (size_t i = 0; i < nsafepoints; i++) {
326 Address address = safepoints->raw() + Code::BytesPerSafepoint(code->maxStackDepth()) * i;
327 fprintf(fp, "%4d: ", *reinterpret_cast<unsigned *>(address));
328 unsigned char *bits = (unsigned char *)(address + sizeof(unsigned));
329 for (unsigned j = 0; j < code->maxStackDepth(); j++) {
330 if (bits[j / 8] & (1 << (j % 8)))
331 fprintf(fp, "%d ", j);