Fix bug in bytecode resizing.
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/.
20 #include "Interpreter.h"
25 #include "Reference.h"
27 #include "Environments.h"
28 #include "Structures.h"
33 #define PUSH_I32(top, value) \
35 top.sp->i32 = value; \
39 #define PUSH_F(top, value) \
48 int value = top.sp[-1].i32;
56 float value = top.sp[-1].f;
61 // Pawn's original semantics specify some kind of floor operation, so
62 // (8 / -3 == -3) and (-8 / 3 == -1). The SourcePawn 1 compiler (spcomp) uses
63 // the upstream algorithm when constant folding. However the SourcePawn 1 JIT
64 // does not. My guess is that Borja freaked out seeing the 13-instruction x86
65 // sequence necessary to get this functionality (I vaguely recall a thread
66 // where we discussed this with Mark Peter? But the ITB forums are gone...),
67 // and we just decided to break the damn thing. So SP1 was inconsistent, but
68 // we are not. And for completeness, we assert that the C compiler does the
69 // right thing, since C89 leaves it unspecified.
70 STATIC_ASSERT(8 / -3 == -2);
71 STATIC_ASSERT(-8 / 3 == -2);
74 PushNewRef(Zone *zone, Top &top, void *location, Heap::Tenure tenure)
76 Reference *ref = Reference::NewStack(zone, location, tenure);
84 static bool CopyStruct(Zone *zone, Struct *left, Struct *right);
86 // Even though this function can fail, we immediately bail out, so it doesn't
87 // matter if we've leaving things unrooted.
89 CopyArray(Zone *zone, Array *source, Array *dest)
91 if (source->type()->contained()->isArray()) {
92 assert(dest->type()->contained()->isArray());
93 assert(source->length() == dest->length());
95 for (unsigned i = 0; i < source->length(); i++) {
96 Array *left = Array::cast(source->readObject(i));
97 Array *right = Array::cast(dest->readObject(i));
98 if (!CopyArray(zone, left, right))
101 } else if (source->type()->contained()->isStruct()) {
102 assert(dest->type()->contained() == source->type()->contained());
103 assert(source->length() == dest->length());
105 for (unsigned i = 0; i < source->length(); i++) {
106 Struct *left = Struct::cast(source->readObject(i));
107 Struct *right = Struct::cast(dest->readObject(i));
108 if (!CopyStruct(zone, left, right))
112 assert(source->type()->contained()->pod() == dest->type()->contained()->pod());
113 assert(source->type()->contained()->pod() == PrimitiveType_Char
114 ? source->length() >= dest->length()
115 : source->length() == dest->length());
117 memcpy(source->vec(), dest->vec(), dest->length() * dest->elementSize());
123 GetElement(Array *array, int index, LocalSlot *vp)
125 if (array->type()->contained()->isTraceable()) {
126 vp->obj = array->readObject(index);
130 switch (array->type()->contained()->pod()) {
131 case PrimitiveType_Float:
132 vp->f = array->readFloat(index);
135 case PrimitiveType_Char:
136 vp->i32 = array->readChar(index);
140 assert(array->type()->contained()->pod() == PrimitiveType_Int32);
141 vp->i32 = array->readInt32(index);
147 SetElement(Zone *zone, Array *array, int index, const LocalSlot &v)
149 if (array->type()->contained()->isTraceable()) {
150 array->writeObject(zone, index, v.obj);
154 switch (array->type()->contained()->pod()) {
155 case PrimitiveType_Float:
156 array->writeFloat(index, v.f);
159 case PrimitiveType_Char:
160 array->writeChar(index, v.i32);
164 assert(array->type()->contained()->pod() == PrimitiveType_Int32);
165 array->writeInt32(index, v.i32);
171 GetField(Struct *obj, Descriptor *desc, LocalSlot *vp)
173 // :TODO: assert no GC.
174 if (desc->type()->isTraceable()) {
175 vp->obj = obj->get<Object *>(desc);
179 switch (desc->type()->pod()) {
180 case PrimitiveType_Float:
181 vp->f = obj->get<float>(desc);
185 assert(desc->type()->pod() == PrimitiveType_Int32);
186 vp->i32 = obj->get<int>(desc);
192 GetField(Struct *obj, unsigned index, LocalSlot *vp)
194 Descriptor *desc = Descriptor::cast(obj->map()->type()->fields()->at(index));
195 GetField(obj, desc, vp);
199 SetField(Zone *zone, Struct *obj, Descriptor *desc, const LocalSlot &v)
201 // :TODO: assert no GC.
202 if (desc->type()->isTraceable()) {
203 obj->set(zone, desc, v.obj);
207 switch (desc->type()->pod()) {
208 case PrimitiveType_Float:
209 obj->set<float>(desc, v.f);
213 assert(desc->type()->pod() == PrimitiveType_Int32);
214 obj->set<int>(desc, v.i32);
220 SetField(Zone *zone, Struct *obj, unsigned index, const LocalSlot &v)
222 Descriptor *desc = Descriptor::cast(obj->map()->type()->fields()->at(index));
223 SetField(zone, obj, desc, v);
226 // Even though this function can fail, we immediately bail out, so it doesn't
227 // matter if we've leaving things unrooted.
229 CopyStruct(Zone *zone, Struct *left, Struct *right)
231 assert(left->map()->type() == right->map()->type());
233 Type *type = left->map()->type();
234 for (unsigned i = 0; i < type->fields()->length(); i++) {
235 Descriptor *desc = Descriptor::cast(type->fields()->at(i));
238 GetField(right, desc, &rval);
240 // If the field is a struct, we need to deep-copy.
241 if (desc->type()->isStruct()) {
244 GetField(left, desc, &lval);
245 if (!CopyStruct(zone, Struct::cast(lval.obj), Struct::cast(rval.obj)))
247 } else if (desc->type()->isArray()) {
250 GetField(left, desc, &lval);
251 if (!CopyArray(zone, Array::cast(lval.obj), Array::cast(rval.obj)))
254 SetField(zone, left, desc, rval);
262 InvokeNative(Zone *zone, Handle<Native> native, void *identity, LocalSlot *args, unsigned argc,
267 if (!zone->stack().pushNativeFrame(native, guard, argc + 1))
270 intptr_t *cells = guard.args();
272 // params[0] == argc, in ISPE
275 // Pass the formal arguments.
276 Local<Type> type(zone, native->type());
277 Local<Type> param(zone);
278 for (unsigned i = 0; i < type->parameters()->length(); i++) {
279 param = type->parameterAt(i);
281 // For floats, we rely on the aliasing of |i32| and |f|.
282 LocalSlot &arg = args[-int(i) - 1];
283 if (param->isInt32() || param->isFloat()) {
284 cells[i + 1] = arg.i32;
285 } else if (param->isReference()) {
286 cells[i + 1] = reinterpret_cast<intptr_t>(arg.ref->location());
288 assert(param->isArray());
289 cells[i + 1] = reinterpret_cast<intptr_t>(arg.array->vec());
293 // For variadic arguments, the compiler left either arrays or references.
294 for (unsigned i = type->parameters()->length(); i < argc; i++) {
295 LocalSlot &arg = args[-int(i) - 1];
297 if (arg.obj->is(MapKind_Reference)) {
298 cells[i + 1] = reinterpret_cast<intptr_t>(arg.ref->location());
300 assert(arg.obj->is(MapKind_Array));
301 cells[i + 1] = reinterpret_cast<intptr_t>(arg.array->vec());
305 typedef intptr_t (*INVOKE)(void *, intptr_t *);
306 INVOKE invoke = (INVOKE)native->function();
307 *result = invoke(identity, cells);
309 return !zone->hasPendingException();
313 ke::Interpret(Zone *zone, Frame *entryFrame, LocalSlot *result)
315 Top &top = *zone->stack().top();
319 LocalSlot *locals = entryFrame->locals();
322 Opcode op = Opcode(*top.pc);
324 if (OpcodeInfo[op].nuses > 0)
325 assert(top.sp - OpcodeInfo[op].nuses >= top.fp->stackBase());
333 PUSH_I32(top, READ_INT32(top.pc));
337 PUSH_F(top, READ_FLOAT(top.pc));
347 if (top.fp == entryFrame)
351 zone->stack().popInlineFrame();
352 locals = top.fp->locals();
357 top.pc += OpcodeInfo[*top.pc].length;
367 #define RELATIONAL_JUMP(op) \
369 int right = POP_I32(top); \
370 int left = POP_I32(top); \
371 if (left op right) { \
372 top.pc += READ_INT32(top.pc); \
401 #undef RELATIONAL_JUMP
405 int value = POP_I32(top);
407 top.pc += READ_INT32(top.pc);
415 int value = POP_I32(top);
417 top.pc += READ_INT32(top.pc);
423 #define RELATIONAL_JUMP_F(op) \
425 float right = POP_F(top); \
426 float left = POP_F(top); \
427 if (left op right) { \
428 top.pc += READ_INT32(top.pc); \
434 RELATIONAL_JUMP_F(<);
438 RELATIONAL_JUMP_F(<=);
442 RELATIONAL_JUMP_F(>);
446 RELATIONAL_JUMP_F(>=);
450 RELATIONAL_JUMP_F(==);
454 RELATIONAL_JUMP_F(!=);
459 float value = POP_F(top);
461 top.pc += READ_INT32(top.pc);
469 float value = POP_F(top);
471 top.pc += READ_INT32(top.pc);
478 top.pc += READ_INT32(top.pc);
483 int slot = READ_INT32(top.pc);
484 *top.sp++ = locals[slot];
490 int slot = READ_INT32(top.pc);
491 locals[slot] = top.sp[-1];
497 top.sp[-2].i32 = top.sp[-2].i32 + top.sp[-1].i32;
504 top.sp[-2].i32 = top.sp[-2].i32 - top.sp[-1].i32;
511 top.sp[-2].i32 = top.sp[-2].i32 * top.sp[-1].i32;
518 if (top.sp[-1].i32 == 0) {
519 zone->reportError(Message_IntegerDivideByZero);
522 top.sp[-2].i32 = top.sp[-2].i32 / top.sp[-1].i32;
529 if (top.sp[-1].i32 == 0) {
530 zone->reportError(Message_IntegerDivideByZero);
533 top.sp[-2].i32 = top.sp[-2].i32 % top.sp[-1].i32;
540 top.sp[-2].i32 = top.sp[-2].i32 < top.sp[-1].i32;
547 top.sp[-2].i32 = top.sp[-2].i32 <= top.sp[-1].i32;
554 top.sp[-2].i32 = top.sp[-2].i32 > top.sp[-1].i32;
561 top.sp[-2].i32 = top.sp[-2].i32 >= top.sp[-1].i32;
568 top.sp[-2].i32 = (top.sp[-2].i32 == top.sp[-1].i32);
575 top.sp[-2].i32 = (top.sp[-2].i32 != top.sp[-1].i32);
582 top.sp[-2].f = top.sp[-2].f + top.sp[-1].f;
589 top.sp[-2].f = top.sp[-2].f - top.sp[-1].f;
596 top.sp[-2].f = top.sp[-2].f * top.sp[-1].f;
603 top.sp[-2].f = top.sp[-2].f / top.sp[-1].f;
610 top.sp[-2].f = top.sp[-2].f < top.sp[-1].f;
617 top.sp[-2].f = top.sp[-2].f <= top.sp[-1].f;
624 top.sp[-2].f = top.sp[-2].f > top.sp[-1].f;
631 top.sp[-2].f = top.sp[-2].f >= top.sp[-1].f;
638 top.sp[-2].i32 = (top.sp[-2].f == top.sp[-1].f);
645 top.sp[-2].i32 = (top.sp[-2].f != top.sp[-1].f);
652 top.sp[-2].i32 = top.sp[-2].i32 << top.sp[-1].i32;
659 top.sp[-2].i32 = top.sp[-2].i32 >> top.sp[-1].i32;
666 top.sp[-2].i32 = unsigned(top.sp[-2].i32) << unsigned(top.sp[-1].i32);
673 top.sp[-2].i32 = top.sp[-2].i32 & top.sp[-1].i32;
680 top.sp[-2].i32 = top.sp[-2].i32 | top.sp[-1].i32;
687 top.sp[-2].i32 = top.sp[-2].i32 ^ top.sp[-1].i32;
694 top.sp[-1].i32 = ~top.sp[-1].i32;
700 top.sp[-1].i32 = top.sp[-1].i32 ? 0 : 1;
706 top.sp[-1].i32 = top.sp[-1].f ? 0 : 1;
712 top.sp[-1].i32 = -top.sp[-1].i32;
718 top.sp[-1].f = -top.sp[-1].f;
724 Local<Function> newFun(zone, top.sp[-1].fun);
727 if (!zone->stack().pushInlineFrame(newFun))
730 locals = top.fp->locals();
736 unsigned slot = READ_UINT32(top.pc);
737 *top.sp++ = top.fp->arg(slot);
743 unsigned slot = READ_UINT32(top.pc);
744 top.fp->arg(slot) = top.sp[-1];
750 Array *array = Array::cast(top.sp[-2].obj);
751 int index = top.sp[-1].i32;
753 if (index < 0 || unsigned(index) >= array->length()) {
754 zone->reportError(Message_ArrayIndexOutOfBounds, index, array->length());
758 GetElement(array, index, &top.sp[-2]);
765 Array *array = Array::cast(top.sp[-3].obj);
766 int index = top.sp[-2].i32;
768 if (index < 0 || unsigned(index) >= array->length()) {
769 zone->reportError(Message_ArrayIndexOutOfBounds, index, array->length());
773 SetElement(zone, array, index, top.sp[-1]);
774 top.sp[-3] = top.sp[-1];
781 int ival = POP_I32(top);
782 float fval = (float)ival;
789 Heap::Tenure tenure = (Heap::Tenure)top.pc[1];
790 Local<ArrayMap> map(zone, ArrayMap::cast(top.fp->code()->map(READ_UINT32(top.pc + 1))));
791 Array *array = Array::New(zone, map, map->type()->elements(), tenure);
801 Heap::Tenure tenure = (Heap::Tenure)top.pc[1];
802 Local<StructMap> map(zone, StructMap::cast(top.fp->code()->map(READ_UINT32(top.pc + 1))));
803 top.sp->obj = Struct::New(zone, map, tenure);
812 unsigned slot = READ_UINT32(top.pc);
813 locals[slot].address = zone->heap().lifoPosition();
819 unsigned slot = READ_UINT32(top.pc);
820 zone->heap().unwindLifo(locals[slot].address);
826 unsigned slot = READ_UINT32(top.pc);
827 if (!PushNewRef(zone, top, &locals[slot], Heap::Tenure_Stack))
834 unsigned slot = READ_UINT32(top.pc);
835 if (!PushNewRef(zone, top, &top.fp->arg(slot), Heap::Tenure_Stack))
840 case OP_ARRAYREF_LIFO:
842 Local<Array> array(zone, Array::cast(top.sp[-2].obj));
843 Local<Reference> ref(zone, Reference::NewArray(zone, array, top.sp[-1].i32, Heap::Tenure_Stack));
846 top.sp[-2].ref = ref;
851 case OP_SLOTREF_LIFO:
853 Reference *ref = Reference::NewStack(zone, top.sp[-1], Heap::Tenure_Stack);
856 top.sp[-1].ref = ref;
860 case OP_FIELDREF_LIFO:
862 Local<Struct> obj(zone, Struct::cast(top.sp[-1].obj));
863 top.sp[-1].obj = Reference::NewField(zone, obj, READ_UINT32(top.pc), Heap::Tenure_Stack);
871 top.sp[-1].i32 = top.sp[-1].ref->getInt();
877 top.sp[-2].ref->setInt(top.sp[-1].i32);
878 top.sp[-2].i32 = top.sp[-1].i32;
885 top.sp[-1].f = top.sp[-1].ref->getFloat();
891 top.sp[-2].ref->setFloat(top.sp[-1].f);
892 top.sp[-2].f = top.sp[-1].f;
898 top.sp->obj = top.fp->code()->object(READ_UINT32(top.pc));
905 // Note for this assignment opcode, we return the left-hand side, not the
906 // right, since in this case the result of the expression could be different.
907 if (!CopyArray(zone, top.sp[-2].array, top.sp[-1].array))
916 top.sp[-1] = top.sp[-2];
923 top.sp[-2] = top.sp[-4];
924 top.sp[-1] = top.sp[-3];
930 LocalSlot slot = top.sp[-2];
931 top.sp[-2] = top.sp[-1];
938 LocalSlot slot = top.sp[-3];
939 top.sp[-3] = top.sp[-2];
940 top.sp[-2] = top.sp[-1];
948 top.sp[-1] = top.sp[-3];
955 top.sp[-1] = top.sp[-4];
961 unsigned index = READ_UINT32(top.pc);
962 unsigned argc = READ_UINT32(top.pc + 4);
964 Local<Native> native(zone, Native::cast(top.fp->code()->module()->natives()->at(index)));
965 if (!native->function()) {
966 zone->reportError(Message_NativeNotBound, native->name()->chars());
971 void *nativeIdentity = top.fp->code()->module()->nativeIdentity();
972 if (!InvokeNative(zone, native, nativeIdentity, top.sp, argc, &result))
976 PUSH_I32(top, result);
983 top.sp[-1].module = top.fp->code()->module();
987 case OP_GETMODULE_FN:
989 top.sp[-1].fun = top.sp[-1].module->getFunction(READ_UINT32(top.pc));
995 unsigned slot = READ_UINT32(top.pc);
997 top.sp[-1].i32 = top.fp->code()->module()->environment()->getInt32(slot);
1001 case OP_GETGLOBAL_F:
1003 unsigned slot = READ_UINT32(top.pc);
1005 top.sp[-1].f = top.fp->code()->module()->environment()->getFloat(slot);
1009 case OP_GETGLOBAL_O:
1011 unsigned slot = READ_UINT32(top.pc);
1013 top.sp[-1].obj = top.fp->code()->module()->environment()->getObject(slot);
1017 case OP_SETGLOBAL_I:
1019 unsigned slot = READ_UINT32(top.pc);
1020 top.fp->code()->module()->environment()->setInt32(slot, top.sp[-1].i32);
1024 case OP_SETGLOBAL_F:
1026 unsigned slot = READ_UINT32(top.pc);
1027 top.fp->code()->module()->environment()->setFloat(slot, top.sp[-1].f);
1031 case OP_SETGLOBAL_O:
1033 unsigned slot = READ_UINT32(top.pc);
1034 top.fp->code()->module()->environment()->setObject(slot, top.sp[-1].obj);
1041 // Because the stack is a union, these are actually no-ops in the
1062 Struct *obj = Struct::cast(top.sp[-1].obj);
1063 GetField(obj, READ_UINT32(top.pc), &top.sp[-1]);
1069 Struct *obj = Struct::cast(top.sp[-2].obj);
1070 SetField(zone, obj, READ_UINT32(top.pc), top.sp[-1]);
1071 top.sp[-2] = top.sp[-1];
1078 if (!CopyStruct(zone, Struct::cast(top.sp[-2].obj), Struct::cast(top.sp[-1].obj)))
1080 top.sp[-2].obj = top.sp[-1].obj;
1085 case OP_TABLESWITCH:
1087 int value = top.sp[-1].i32;
1088 int low = READ_TABLESWITCH_LOW(top.pc);
1089 int high = READ_TABLESWITCH_HIGH(top.pc);
1093 unsigned index = unsigned(value - low);
1094 unsigned tablesize = TableSwitchEntries(low, high);
1095 if (index < tablesize) {
1096 top.pc += OP_TABLESWITCH_LENGTH + OP_JUMP_LENGTH;
1097 top.pc += index * OP_JUMP_LENGTH;
1109 top.pc += OpcodeInfo[op].length;
1110 assert(top.sp >= top.fp->stackBase());
1117 // Unwind frames until we can continue.
1118 while (top.fp != entryFrame)
1119 zone->stack().popInlineFrame();