Collect error messages per-translation unit.
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/.
19 #ifndef _include_bytecode_emitter_h_
20 #define _include_bytecode_emitter_h_
24 #include "../Opcodes.h"
25 #include "../Handles.h"
26 #include "../CompactBuffer.h"
28 #include "../HashTable.h"
29 #include "../VirtualObjectVisitor.h"
33 // An overarching note on overflows: Though we should use CheckedInt, we rely
34 // on a source-length cap covering all possible overflow checks. As long as
35 // the source length is capped to INT_MAX, the number of AST nodes will be
36 // strictly beneath that cap, and so to will most things generated by the
37 // AST. For example, local and argument counts, number of functions, fields
38 // in a struct, et cetera. Not everything can be covered by this cap: the
39 // stack depth, number of bytecodes for example. In these cases we explicitly
40 // test for overflow of INT_MAX and in other cases we assert.
42 // In the future, we should of course use a checked int wrapper.
56 OopRecord(unsigned length, unsigned offset)
76 Entry(String *string, unsigned index)
85 typedef Entry Payload;
87 static uint32 hash(String *string) {
88 return HashPointer(string);
91 static bool matches(String *key, Entry &e) {
92 return key == e.string;
96 typedef HashTable<Policy, ZoneAllocatorPolicy> Set;
101 void accept(VirtualObjectVisitor *visitor);
103 bool add(Handle<String> name, unsigned *indexp);
104 size_t stringCount() const {
105 return strings_.length();
107 String *stringAt(size_t i) const {
108 return strings_[i].string;
113 Vector<Entry> strings_;
117 // The bytecode emitter assists in building Code objects, by emitting opcodes
118 // and tracking stack information. Since the SemA creates temporary pool/root
119 // scopes, we cannot use ScopedRoot or PoolObjects. Instead, BCE gets a
120 // special marking function.
121 class BytecodeEmitter : public AutoRooted
124 PoolAllocator &pool_;
127 bytecode *buffer_pos_;
128 bytecode *buffer_end_;
129 bytecode inline_buffer_[512];
131 unsigned maxStackDepth_;
132 Vector<bool> oopMap_;
133 Vector<OopRecord *> oopRecords_;
134 CompactBufferWriter sourcemap_;
135 unsigned lastCodePosition_;
137 StringTable strings_;
138 Vector<Symbol *> locals_;
139 Vector<Object *> objects_;
141 unsigned maxLoopDepth_;
144 // The maximum size of a code object must be such that the distance between
145 // two instructions fits into a signed, 31-bit integer. This ensures that
146 // all code-bounded buffers (like the number of object literals or
147 // safepoints) does not need to be bounds checked. It also means we can add
148 // any two offsets and be guaranteed that they don't overflow.
149 static const size_t MAX_LENGTH = INT_MAX / 2;
152 bool growBuffer(size_t bytes);
154 // Checking the return value is not necessary, since the inline buffer is
155 // used as a ballast.
156 bool ensure(size_t bytes) {
157 if (buffer_pos_ + bytes > buffer_end_)
158 return growBuffer(bytes);
162 void writeUint8(unsigned value) {
163 assert(value <= UCHAR_MAX);
164 *buffer_pos_++ = value;
166 void writeFloat(float value) {
167 *(float *)buffer_pos_ = value;
168 buffer_pos_ += sizeof(float);
170 void writeInt32(int value) {
171 *(int *)buffer_pos_ = value;
172 buffer_pos_ += sizeof(int);
174 void writeUint32(unsigned value) {
175 *(unsigned *)buffer_pos_ = value;
176 buffer_pos_ += sizeof(unsigned);
178 void writeOp(Opcode op) {
183 for (int i = 0; i < OpcodeInfo[op].nuses; i++)
185 for (int i = 0; i < OpcodeInfo[op].ndefs; i++)
186 oopMap_.append(false);
187 if (oopMap_.length() > maxStackDepth_)
188 maxStackDepth_ = oopMap_.length();
190 void writeObject(Object *obj) {
193 writeUint32(objects_.length());
194 if (!objects_.append(obj))
198 int position() const {
199 assert(buffer_pos_ - buffer_ < INT_MAX / 2);
200 return int(buffer_pos_ - buffer_);
204 BytecodeEmitter(PoolAllocator &pool);
207 void accept(VirtualObjectVisitor *visitor);
209 bytecode *buffer() const {
212 unsigned length() const {
213 return unsigned(buffer_pos_ - buffer_);
216 return oom_ || sourcemap_.oom();
218 unsigned stackDepth() const {
219 return oopMap_.length();
221 unsigned maxStackDepth() const {
222 return maxStackDepth_;
225 void drop(unsigned amt) {
226 assert(amt <= stackDepth());
227 for (unsigned i = 0; i < amt; i++)
231 void note_position(const SourcePosition &pos);
232 void setFirstLine(unsigned line) {
233 assert(!sourcemap_.length());
238 // A label represents a local control-flow target in the bytecode. A label
239 // may be in three states:
241 // (1) Empty - the label is not used yet.
242 // (2) Pending - the label has incoming jumps, but the actual location of
243 // is not yet known. The list of jumps is threaded through
244 // the four bytes reserved for each emitted jump target. In
245 // this case, the offset field is the location of the most
246 // recently emitted incoming jump.
247 // (3) Bound - the label's position has been pinned.
249 // A jump can be emitted to a label in any state. Binding a label
250 // immediately backpatches all pending incoming jumps. A label must be
251 // bound if it is used.
257 void setOffset(int offset) {
259 assert(offset_ == offset);
263 static const int INVALID_OFFSET = -1;
267 : offset_(INVALID_OFFSET),
273 assert(bound_ || offset_ == INVALID_OFFSET);
282 void bind(int offset) {
287 int link(int offset) {
296 int patchJumpAt(int at, int to) {
297 assert(at >= 0 && at < position());
298 int old = *(int *)(buffer_ + at + 1);
299 *(int *)(buffer_ + at + 1) = (to - at);
303 void singleByteOp(Opcode op) {
304 assert(OpcodeInfo[op].length == 1);
305 ensure(OpcodeInfo[op].length);
309 // Mark a stack slot as having an object pointer.
310 void markOop(unsigned depth, bool oop = true) {
313 oopMap_[oopMap_.length() - depth] = oop;
315 bool getOop(unsigned depth) {
318 return oopMap_[oopMap_.length() - depth];
321 // Mark the current bytecode position as either re-entrant (meaning, it
322 // can leave the interpreter or call to another function), or causing GC.
323 // This will record which stack slots have objects and associate that
324 // information with the current pc.
328 void int_(int value) {
337 void getlocal(VariableSymbol *var);
338 void setlocal(VariableSymbol *var);
339 void getarg(VariableSymbol *var);
340 void setarg(VariableSymbol *var);
342 void j(Opcode op, Label *label) {
343 // Since jumps can skip around the stream, do not try to do anything
344 // in a potentially truncated state.
348 assert(op == OP_JT || op == OP_JF || op == OP_JUMP ||
349 op == OP_CONTINUE || op == OP_BREAK);
350 ensure(OP_JUMP_LENGTH);
352 if (label->bound()) {
353 // We can immediately emit this jump.
354 int distance = label->offset() - position();
356 writeInt32(distance);
358 // We can't emit this jump yet. Add a placeholder.
359 int old = label->link(position());
364 void bind(Label *label) {
368 assert(!label->bound());
370 int offset = label->offset();
371 while (offset != Label::INVALID_OFFSET)
372 offset = patchJumpAt(offset, position());
374 label->bind(position());
376 void jump(Label *label) {
380 void binary(Opcode op) {
383 void unary(Opcode op) {
388 singleByteOp(OP_POP);
391 singleByteOp(OP_STOP);
393 void call(Handle<FunctionType> fun, unsigned argc) {
396 if (fun->isNative()) {
397 ensure(OP_CALLNATIVE_LENGTH);
398 writeOp(OP_CALLNATIVE);
402 ensure(OP_CALL_LENGTH);
406 if (fun->returnType()->isVoid())
408 else if (fun->returnType()->isTraceable())
413 void getelem(Type *type) {
414 singleByteOp(OP_GETELEM);
416 if (type->isTraceable())
419 void setelem(Type *type) {
420 singleByteOp(OP_SETELEM);
423 void getfield(unsigned index, Type *type) {
424 ensure(OP_GETFIELD_LENGTH);
425 writeOp(OP_GETFIELD);
428 if (type->isTraceable())
431 void setfield(unsigned index, Type *type) {
432 ensure(OP_SETFIELD_LENGTH);
433 writeOp(OP_SETFIELD);
436 if (type->isTraceable())
441 int offset = position();
443 ensure(OP_ENTERLOOP);
444 writeOp(OP_ENTERLOOP);
448 if (loopDepth_ > maxLoopDepth_)
449 maxLoopDepth_ = loopDepth_;
454 singleByteOp(OP_LEAVELOOP);
459 void setLoopUsesLifoStack(int pos) {
460 assert(pos >= 0 && pos < position());
461 assert(*(buffer() + pos) == OP_ENTERLOOP);
462 *(buffer() + pos + 1) = 1;
465 void float_(float value) {
466 ensure(OP_FLOAT_LENGTH);
471 singleByteOp(OP_CVT_I2F);
473 void newarray(Opcode op, Handle<ArrayMap> map, Heap::Tenure tenure) {
474 assert(op == OP_NEWFIXED ||
479 ensure(OpcodeInfo[op].length);
484 if (op == OP_NEWSIZED)
485 drop(map->type()->levels());
489 void newstruct(Handle<Map> map, Heap::Tenure tenure) {
491 ensure(OP_NEWSTRUCT_LENGTH);
492 writeOp(OP_NEWSTRUCT);
497 void deparray(ArrayMap *map) {
499 ensure(OP_DEPARRAY_LENGTH);
500 writeOp(OP_DEPARRAY);
504 void savelifo(unsigned slot) {
505 ensure(OP_SAVELIFO_LENGTH);
506 writeOp(OP_SAVELIFO);
509 void unwindlifo(unsigned slot) {
510 ensure(OP_UNWINDLIFO_LENGTH);
511 writeOp(OP_UNWINDLIFO);
515 void newref(VariableSymbol *var);
517 void slotref_lifo() {
519 singleByteOp(OP_SLOTREF_LIFO);
522 void getref(Type *type) {
523 if (type->pod() == PrimitiveType_Int32) {
524 singleByteOp(OP_GETREF_I);
526 assert(type->pod() == PrimitiveType_Float);
527 singleByteOp(OP_GETREF_F);
530 void setref(Type *type) {
531 if (type->pod() == PrimitiveType_Int32) {
532 singleByteOp(OP_SETREF_I);
534 assert(type->pod() == PrimitiveType_Float);
535 singleByteOp(OP_SETREF_F);
538 void object(Handle<Object> obj) {
541 ensure(OP_OBJECT_LENGTH);
547 singleByteOp(OP_DUPARRAY);
551 singleByteOp(OP_COPYARRAY);
555 singleByteOp(OP_COPYSTRUCT);
559 singleByteOp(OP_SIZEOF);
563 bool oop = getOop(1);
564 singleByteOp(OP_DUP);
568 bool oop2 = getOop(2);
569 bool oop1 = getOop(1);
571 singleByteOp(OP_DUP2);
577 bool oop2 = getOop(2);
578 bool oop1 = getOop(1);
580 singleByteOp(OP_SWAP);
586 bool oop3 = getOop(3);
587 bool oop2 = getOop(2);
588 bool oop1 = getOop(1);
590 singleByteOp(OP_ROLL3);
597 bool oop2 = getOop(2);
598 bool oop1 = getOop(1);
600 singleByteOp(OP_PICK2);
607 bool oop3 = getOop(3);
608 bool oop2 = getOop(2);
609 bool oop1 = getOop(1);
611 singleByteOp(OP_PICK3);
620 singleByteOp(OP_RETURNVOID);
623 singleByteOp(OP_BITNOT);
626 singleByteOp(OP_NOT);
629 singleByteOp(OP_NOT_F);
632 singleByteOp(OP_NEG);
635 singleByteOp(OP_NEG_F);
638 singleByteOp(OP_TRUE);
641 singleByteOp(OP_FALSE);
644 unsigned addString(Handle<String> name) {
646 if (!strings_.add(name, &index)) {
653 void getglobal(Symbol *sym);
654 void setglobal(Symbol *sym);
658 singleByteOp(OP_PUBLIC);
661 void bitcast(Type *type) {
662 ensure(OP_BITCAST_LENGTH);
667 void tableswitch(int low, int high, Label *def, Label **labels) {
668 unsigned tablesize = TableSwitchEntries(low, high);
669 assert(tablesize <= MAX_TABLESWITCH_ENTRIES);
671 ensure(OP_TABLESWITCH_LENGTH);
672 writeOp(OP_TABLESWITCH);
677 // When labels are patched, we assume each position is a jump. Rather
678 // than hack position offsets to make labels work without actual
679 // incoming jumps, we emit a jump inline.
680 for (unsigned i = 0; i < tablesize; i++)
684 bool allocate(VariableSymbol *sym);
690 void get(Variable *var) {
691 if (var->location() == Variable::STACK) {
694 assert(var->location() == Variable::PARAMETER);
698 void set(Variable *var) {
699 assert(!var->type()->isReference());
700 if (var->location() == Variable::STACK) {
703 assert(var->location() == Variable::PARAMETER);
712 #endif // _include_bytecode_emitter_h_