Support FieldExpressions as references.
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 || op == OP_NEWARRAY || op == OP_VARREF_LIFO ||
82 op == OP_ARGREF_LIFO || op == OP_SLOTREF_LIFO || op == OP_ARRAYREF_LIFO ||
89 size_t bytesPerSafepoint = BytesPerSafepoint(maxStackDepth());
90 assert(!(safepoints_->length() % bytesPerSafepoint));
92 size_t count = safepoints_->length() / bytesPerSafepoint;
94 void *result = bsearch(&pcOffset,
103 assert(*reinterpret_cast<unsigned *>(result) == pcOffset);
105 return Address(result) + sizeof(unsigned);
109 Code::pcToSource(unsigned pc)
111 assert(pc < bytecode_->length());
113 CompactBufferReader reader(sourcemap_->raw(), sourcemap_->raw() + sourcemap_->length());
118 while (!reader.done()) {
119 curPc += reader.readUint32();
123 line = reader.readUint32();
124 col = reader.readUint32();
127 return SourcePosition(lineNumber_ + line, col);
130 // All jumps must be the same length.
131 STATIC_ASSERT(OP_JL_LENGTH == OP_JUMP_LENGTH);
132 STATIC_ASSERT(OP_JLE_LENGTH == OP_JUMP_LENGTH);
133 STATIC_ASSERT(OP_JG_LENGTH == OP_JUMP_LENGTH);
134 STATIC_ASSERT(OP_JGE_LENGTH == OP_JUMP_LENGTH);
135 STATIC_ASSERT(OP_JEQ_LENGTH == OP_JUMP_LENGTH);
136 STATIC_ASSERT(OP_JNE_LENGTH == OP_JUMP_LENGTH);
137 STATIC_ASSERT(OP_JT_LENGTH == OP_JUMP_LENGTH);
138 STATIC_ASSERT(OP_JF_LENGTH == OP_JUMP_LENGTH);
141 ke::Disassemble(Code *code, FILE *fp)
143 bytecode *pc = code->start();
145 Opcode op = Opcode(*pc);
147 fprintf(fp, "%-4d %s ", int(pc - code->start()), OpcodeInfo[op].name);
175 case OP_SLOTREF_LIFO:
176 case OP_ARRAYREF_LIFO:
216 fprintf(fp, "%d", READ_INT32(pc));
220 fprintf(fp, "%f", READ_FLOAT(pc));
240 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");
256 Heap::Tenure tenure = (Heap::Tenure)pc[1];
257 StructMap *map = StructMap::cast(code->map(READ_UINT32(pc + 1)));
258 fprintf(fp, "%s", map->type()->name()->chars());
259 if (tenure == Heap::Tenure_Stack)
260 fprintf(fp, " ; lifo");
261 else if (tenure == Heap::Tenure_Old)
262 fprintf(fp, " ; pretenured");
275 case OP_GETMODULE_FN:
284 case OP_FIELDREF_LIFO:
285 fprintf(fp, "%u", READ_UINT32(pc));
289 fprintf(fp, "%u %u", READ_UINT32(pc), READ_UINT32(pc + 4));
302 pc += OpcodeInfo[op].length;
303 assert(pc < code->start() + code->length());
306 fprintf(fp, "\nSafepoints:\n");
307 ByteArray *safepoints = code->safepoints();
309 size_t nsafepoints = safepoints->length() / Code::BytesPerSafepoint(code->maxStackDepth());
310 for (size_t i = 0; i < nsafepoints; i++) {
311 Address address = safepoints->raw() + Code::BytesPerSafepoint(code->maxStackDepth()) * i;
312 fprintf(fp, "%4d: ", *reinterpret_cast<unsigned *>(address));
313 unsigned char *bits = (unsigned char *)(address + sizeof(unsigned));
314 for (unsigned j = 0; j < code->maxStackDepth(); j++) {
315 if (bits[j / 8] & (1 << (j % 8)))
316 fprintf(fp, "%d ", j);