Clean up array ops to match the new GET/SETFIELD style.
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_
25 #include "NameBinding.h"
27 #include "CompactBuffer.h"
34 typedef PoolList<ScopedRoot<Object> > ObjectList;
36 struct OopRecord : public PoolObject
42 OopRecord(unsigned length, unsigned offset)
56 bytecode *buffer_pos_;
57 bytecode *buffer_end_;
58 bytecode inline_buffer_[512];
60 unsigned maxStackDepth_;
62 PoolList<bool> oopMap_;
63 PoolList<OopRecord *> oopRecords_;
64 CompactBufferWriter sourcemap_;
65 unsigned lastCodePosition_;
69 // The maximum size of a code object must be such that the distance between
70 // two instructions fits into a signed, 31-bit integer. This ensures that
71 // all code-bounded buffers (like the number of object literals or
72 // safepoints) does not need to be bounds checked. It also means we can add
73 // any two offsets and be guaranteed that they don't overflow.
74 static const size_t MAX_LENGTH = INT_MAX / 2;
77 bool growBuffer(size_t bytes);
79 // Checking the return value is not necessary, since the inline buffer is
81 bool ensure(size_t bytes) {
82 if (buffer_pos_ + bytes > buffer_end_)
83 return growBuffer(bytes);
87 void writeUint8(unsigned value) {
88 assert(value <= UCHAR_MAX);
89 *buffer_pos_++ = value;
91 void writeFloat(float value) {
92 *(float *)buffer_pos_ = value;
93 buffer_pos_ += sizeof(float);
95 void writeInt32(int value) {
96 *(int *)buffer_pos_ = value;
97 buffer_pos_ += sizeof(int);
99 void writeUint32(unsigned value) {
100 *(unsigned *)buffer_pos_ = value;
101 buffer_pos_ += sizeof(unsigned);
103 void writeOp(Opcode op) {
108 for (int i = 0; i < OpcodeInfo[op].nuses; i++)
110 for (int i = 0; i < OpcodeInfo[op].ndefs; i++)
111 oopMap_.append(false);
112 if (oopMap_.length() > maxStackDepth_)
113 maxStackDepth_ = oopMap_.length();
115 void writeObject(Object *obj) {
116 writeUint32(objects_.length());
117 ScopedRoot<Object> root(obj);
118 if (!objects_.append(root))
122 int position() const {
123 assert(buffer_pos_ - buffer_ < INT_MAX / 2);
124 return int(buffer_pos_ - buffer_);
128 BytecodeEmitter(PoolAllocator &pool);
130 bytecode *buffer() const {
133 unsigned length() const {
134 return unsigned(buffer_pos_ - buffer_);
137 return oom_ || sourcemap_.oom();
139 unsigned stackDepth() const {
140 return oopMap_.length();
142 unsigned maxStackDepth() const {
143 return maxStackDepth_;
145 const ObjectList *objects() const {
148 PoolList<OopRecord *> *oopRecords() {
152 void drop(unsigned amt) {
153 assert(amt <= stackDepth());
154 for (unsigned i = 0; i < amt; i++)
158 void note_position(const SourcePosition &pos);
159 uint8 *sourcemap() const {
160 return sourcemap_.buffer();
162 size_t sourcemapLength() const {
163 return sourcemap_.length();
165 void setFirstLine(unsigned line) {
166 assert(!sourcemap_.length());
171 // A label represents a local control-flow target in the bytecode. A label
172 // may be in three states:
174 // (1) Empty - the label is not used yet.
175 // (2) Pending - the label has incoming jumps, but the actual location of
176 // is not yet known. The list of jumps is threaded through
177 // the four bytes reserved for each emitted jump target. In
178 // this case, the offset field is the location of the most
179 // recently emitted incoming jump.
180 // (3) Bound - the label's position has been pinned.
182 // A jump can be emitted to a label in any state. Binding a label
183 // immediately backpatches all pending incoming jumps. A label must be
184 // bound if it is used.
190 void setOffset(int offset) {
192 assert(offset_ == offset);
196 static const int INVALID_OFFSET = -1;
200 : offset_(INVALID_OFFSET),
206 assert(bound_ || offset_ == INVALID_OFFSET);
215 void bind(int offset) {
220 int link(int offset) {
229 int patchJumpAt(int at, int to) {
230 assert(at >= 0 && at < position());
231 int old = *(int *)(buffer_ + at + 1);
232 *(int *)(buffer_ + at + 1) = (to - at);
236 void singleByteOp(Opcode op) {
237 assert(OpcodeInfo[op].length == 1);
238 ensure(OpcodeInfo[op].length);
242 // Mark a stack slot as having an object pointer.
243 void markOop(unsigned depth, bool oop = true) {
246 oopMap_[oopMap_.length() - depth] = oop;
248 bool getOop(unsigned depth) {
251 return oopMap_[oopMap_.length() - depth];
254 // Mark the current bytecode position as either re-entrant (meaning, it
255 // can leave the interpreter or call to another function), or causing GC.
256 // This will record which stack slots have objects and associate that
257 // information with the current pc.
261 void int_(int value) {
270 void getlocal(Variable *var) {
271 ensure(OP_GETLOCAL_LENGTH);
272 writeOp(OP_GETLOCAL);
273 writeUint32(var->slot());
275 if (var->type()->isTraceable())
278 void setlocal(Variable *var) {
279 ensure(OP_SETLOCAL_LENGTH);
280 writeOp(OP_SETLOCAL);
281 writeUint32(var->slot());
283 if (var->type()->isTraceable())
286 void getarg(Variable *var) {
287 ensure(OP_GETARG_LENGTH);
289 writeUint32(var->slot());
291 if (var->type()->isTraceable())
294 void setarg(Variable *var) {
295 ensure(OP_SETARG_LENGTH);
297 writeUint32(var->slot());
299 if (var->type()->isTraceable())
303 void j(Opcode op, Label *label) {
304 // Since jumps can skip around the stream, do not try to do anything
305 // in a potentially truncated state.
309 assert(op >= OP_JL && op <= OP_JUMP);
310 ensure(OP_JUMP_LENGTH);
312 if (label->bound()) {
313 // We can immediately emit this jump.
314 int distance = label->offset() - position();
316 writeInt32(distance);
318 // We can't emit this jump yet. Add a placeholder.
319 int old = label->link(position());
324 void bind(Label *label) {
328 assert(!label->bound());
330 int offset = label->offset();
331 while (offset != Label::INVALID_OFFSET)
332 offset = patchJumpAt(offset, position());
334 label->bind(position());
336 void jump(Label *label) {
340 void binary(Opcode op) {
345 singleByteOp(OP_POP);
348 singleByteOp(OP_STOP);
350 void call(Handle<Type> fun) {
352 ensure(OP_CALL_LENGTH);
355 if (fun->returnType()->isVoid())
357 else if (fun->returnType()->isTraceable())
360 void callnative(unsigned index, unsigned argc) {
362 ensure(OP_CALLNATIVE_LENGTH);
363 writeOp(OP_CALLNATIVE);
368 void getelem(Type *type) {
369 singleByteOp(OP_GETELEM);
371 if (type->isTraceable())
374 void setelem(Type *type) {
375 singleByteOp(OP_SETELEM);
377 assert(!type->isTraceable());
380 void getfield(unsigned index, Type *type) {
381 ensure(OP_GETFIELD_LENGTH);
382 writeOp(OP_GETFIELD);
385 if (type->isTraceable())
388 void setfield(unsigned index, Type *type) {
389 ensure(OP_SETFIELD_LENGTH);
390 writeOp(OP_SETFIELD);
393 if (type->isTraceable())
397 void getglobal(Variable *var) {
398 ensure(OP_GETGLOBAL_F_LENGTH);
399 if (var->type()->isPrimitive()) {
400 switch (var->type()->primitive()) {
401 case PrimitiveType_Float:
402 writeOp(OP_GETGLOBAL_F);
405 assert(var->type()->primitive() == PrimitiveType_Int32);
406 writeOp(OP_GETGLOBAL_I);
410 writeOp(OP_GETGLOBAL_O);
413 writeUint32(var->slot());
415 void setglobal(Variable *var) {
416 ensure(OP_SETGLOBAL_F_LENGTH);
417 if (var->type()->isPrimitive()) {
418 switch (var->type()->primitive()) {
419 case PrimitiveType_Float:
420 writeOp(OP_SETGLOBAL_F);
423 assert(var->type()->primitive() == PrimitiveType_Int32);
424 writeOp(OP_SETGLOBAL_I);
428 writeOp(OP_SETGLOBAL_O);
430 writeUint32(var->slot());
434 singleByteOp(OP_BOUNDS);
437 void float_(float value) {
438 ensure(OP_FLOAT_LENGTH);
443 singleByteOp(OP_CVT_I2F);
445 void newarray(Handle<Map> map, Heap::Tenure tenure) {
447 ensure(OP_NEWARRAY_LENGTH);
448 writeOp(OP_NEWARRAY);
453 void newstruct(Handle<Map> map, Heap::Tenure tenure) {
455 ensure(OP_NEWSTRUCT_LENGTH);
456 writeOp(OP_NEWSTRUCT);
461 void savelifo(unsigned slot) {
462 ensure(OP_SAVELIFO_LENGTH);
463 writeOp(OP_SAVELIFO);
466 void unwindlifo(unsigned slot) {
467 ensure(OP_UNWINDLIFO_LENGTH);
468 writeOp(OP_UNWINDLIFO);
471 void newref(Variable *var) {
473 if (var->location() == Variable::STACK) {
474 ensure(OP_VARREF_LIFO_LENGTH);
475 writeOp(OP_VARREF_LIFO);
477 assert(var->location() == Variable::PARAMETER);
478 ensure(OP_ARGREF_LIFO_LENGTH);
479 writeOp(OP_ARGREF_LIFO);
481 writeUint32(var->slot());
485 void slotref_lifo() {
487 singleByteOp(OP_SLOTREF_LIFO);
490 void arrayref_lifo() {
492 singleByteOp(OP_ARRAYREF_LIFO);
495 void getref(Type *type) {
496 if (type->pod() == PrimitiveType_Int32) {
497 singleByteOp(OP_GETREF_I);
499 assert(type->pod() == PrimitiveType_Float);
500 singleByteOp(OP_GETREF_F);
503 void setref(Type *type) {
504 if (type->pod() == PrimitiveType_Int32) {
505 singleByteOp(OP_SETREF_I);
507 assert(type->pod() == PrimitiveType_Float);
508 singleByteOp(OP_SETREF_F);
511 void object(Handle<Object> obj) {
512 ensure(OP_OBJECT_LENGTH);
518 singleByteOp(OP_COPYARRAY);
522 singleByteOp(OP_COPYSTRUCT);
527 bool oop = getOop(1);
528 singleByteOp(OP_DUP);
532 bool oop2 = getOop(2);
533 bool oop1 = getOop(1);
535 singleByteOp(OP_DUP2);
541 bool oop2 = getOop(2);
542 bool oop1 = getOop(1);
544 singleByteOp(OP_SWAP);
550 bool oop3 = getOop(3);
551 bool oop2 = getOop(2);
552 bool oop1 = getOop(1);
554 singleByteOp(OP_ROLL3);
561 bool oop2 = getOop(2);
562 bool oop1 = getOop(1);
564 singleByteOp(OP_PICK2);
571 bool oop3 = getOop(3);
572 bool oop2 = getOop(2);
573 bool oop1 = getOop(1);
575 singleByteOp(OP_PICK3);
584 singleByteOp(OP_RETURNVOID);
587 singleByteOp(OP_MODULE);
590 void getmodule_fn(unsigned index) {
591 ensure(OP_GETMODULE_FN_LENGTH);
592 writeOp(OP_GETMODULE_FN);
597 singleByteOp(OP_BITNOT);
600 singleByteOp(OP_NOT);
603 singleByteOp(OP_NOT_F);
606 singleByteOp(OP_NEG);
609 singleByteOp(OP_NEG_F);
612 singleByteOp(OP_BITS_I2F);
615 singleByteOp(OP_BITS_F2I);
618 singleByteOp(OP_TRUE);
621 singleByteOp(OP_FALSE);
625 void get(Variable *var) {
626 if (var->location() == Variable::STACK) {
629 assert(var->location() == Variable::PARAMETER);
633 void set(Variable *var) {
634 assert(!var->type()->isReference());
635 if (var->location() == Variable::STACK) {
638 assert(var->location() == Variable::PARAMETER);
646 #endif // _include_bytecode_emitter_h_