Enable dynamic array declarations.
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, Handle<Struct> left, Handle<Struct> right);
87 ChangeArrayLength(Zone *zone, Handle<Array> source, Handle<Array> dest)
92 // Even though this function can fail, we immediately bail out, so it doesn't
93 // matter if we've leaving things unrooted.
95 CopyArray(Zone *zone, Handle<Array> dest, Handle<Array> source)
97 if (source->length() != dest->length()) {
98 if (!ChangeArrayLength(zone, source, dest))
102 Local<Type> contained(zone, source->type()->contained());
103 if (contained->isArray() && contained->isFixedLength()) {
104 assert(dest->type()->contained()->isArray());
105 assert(source->length() == dest->length());
107 Local<Array> left(zone);
108 Local<Array> right(zone);
109 for (unsigned i = 0; i < source->length(); i++) {
110 left = Array::cast(dest->readObject(i));
111 right = Array::cast(source->readObject(i));
112 if (!CopyArray(zone, left, right))
118 if (contained->isStruct()) {
119 assert(dest->type()->contained() == contained);
120 assert(source->length() == dest->length());
122 Local<Struct> left(zone);
123 Local<Struct> right(zone);
124 for (unsigned i = 0; i < source->length(); i++) {
125 left = Struct::cast(dest->readObject(i));
126 right = Struct::cast(source->readObject(i));
127 if (!CopyStruct(zone, left, right))
133 if (contained->isTraceable()) {
134 for (unsigned i = 0; i < source->length(); i++)
135 dest->writeObject(zone, i, source->readObject(i));
139 assert(contained->pod() == dest->type()->contained()->pod());
140 assert(contained->pod() == PrimitiveType_Char
141 ? source->length() <= dest->length()
142 : source->length() == dest->length());
144 assert(dest->length());
145 memcpy(dest->bytes(), source->bytes(), source->length() * Array::SizeForElement(dest->type()));
150 GetElement(Array *array, int index, LocalSlot *vp)
152 if (array->type()->contained()->isTraceable()) {
153 vp->obj = array->readObject(index);
157 switch (array->type()->contained()->pod()) {
158 case PrimitiveType_Float:
159 vp->f = array->readFloat(index);
162 case PrimitiveType_Char:
163 vp->i32 = array->readChar(index);
167 assert(array->type()->contained()->pod() == PrimitiveType_Int32);
168 vp->i32 = array->readInt32(index);
174 SetElement(Zone *zone, Array *array, int index, const LocalSlot &v)
176 if (array->type()->contained()->isTraceable()) {
177 array->writeObject(zone, index, v.obj);
181 switch (array->type()->contained()->pod()) {
182 case PrimitiveType_Float:
183 array->writeFloat(index, v.f);
186 case PrimitiveType_Char:
187 array->writeChar(index, v.i32);
191 assert(array->type()->contained()->pod() == PrimitiveType_Int32);
192 array->writeInt32(index, v.i32);
198 GetField(Struct *obj, Descriptor *desc, LocalSlot *vp)
200 // :TODO: assert no GC.
201 if (desc->type()->isTraceable()) {
202 vp->obj = obj->get<Object *>(desc);
206 switch (desc->type()->pod()) {
207 case PrimitiveType_Float:
208 vp->f = obj->get<float>(desc);
212 assert(desc->type()->pod() == PrimitiveType_Int32);
213 vp->i32 = obj->get<int>(desc);
219 GetField(Struct *obj, unsigned index, LocalSlot *vp)
221 Descriptor *desc = Descriptor::cast(obj->map()->type()->fields()->at(index));
222 GetField(obj, desc, vp);
226 SetField(Zone *zone, Struct *obj, Descriptor *desc, const LocalSlot &v)
228 // :TODO: assert no GC.
229 if (desc->type()->isTraceable()) {
230 obj->set(zone, desc, v.obj);
234 switch (desc->type()->pod()) {
235 case PrimitiveType_Float:
236 obj->set<float>(desc, v.f);
240 assert(desc->type()->pod() == PrimitiveType_Int32);
241 obj->set<int>(desc, v.i32);
247 SetField(Zone *zone, Struct *obj, unsigned index, const LocalSlot &v)
249 Descriptor *desc = Descriptor::cast(obj->map()->type()->fields()->at(index));
250 SetField(zone, obj, desc, v);
253 // Even though this function can fail, we immediately bail out, so it doesn't
254 // matter if we've leaving things unrooted.
256 CopyStruct(Zone *zone, Handle<Struct> left, Handle<Struct> right)
258 assert(left->map()->type() == right->map()->type());
260 Local<Type> type(zone, left->map()->type());
261 Local<Struct> leftStruct(zone);
262 Local<Struct> rightStruct(zone);
263 Local<Array> leftArray(zone);
264 Local<Array> rightArray(zone);
265 for (unsigned i = 0; i < type->fields()->length(); i++) {
266 Descriptor *desc = Descriptor::cast(type->fields()->at(i));
269 GetField(right, desc, &rval);
271 // If the field is a struct, we need to deep-copy.
272 if (desc->type()->isStruct()) {
275 GetField(left, desc, &lval);
276 leftStruct = Struct::cast(lval.obj);
277 rightStruct = Struct::cast(rval.obj);
278 if (!CopyStruct(zone, leftStruct, rightStruct))
280 } else if (desc->type()->isArray()) {
283 GetField(left, desc, &lval);
284 leftArray = Array::cast(lval.obj);
285 rightArray = Array::cast(rval.obj);
286 if (!CopyArray(zone, leftArray, rightArray))
289 SetField(zone, left, desc, rval);
297 InvokeNative(Zone *zone, Handle<Native> native, void *identity, LocalSlot *args, unsigned argc,
302 if (!zone->stack().pushNativeFrame(native, guard, argc + 1))
305 intptr_t *cells = guard.args();
307 // params[0] == argc, in ISPE
310 // Pass the formal arguments.
311 Local<Type> type(zone, native->type());
312 Local<Type> param(zone);
313 for (unsigned i = 0; i < type->parameters()->length(); i++) {
314 param = type->parameterAt(i);
316 // For floats, we rely on the aliasing of |i32| and |f|.
317 LocalSlot &arg = args[-int(i) - 1];
318 if (param->isInt32() || param->isFloat()) {
319 cells[i + 1] = arg.i32;
320 } else if (param->isReference()) {
321 cells[i + 1] = reinterpret_cast<intptr_t>(arg.ref->location());
323 assert(param->isArray());
324 cells[i + 1] = reinterpret_cast<intptr_t>(arg.array->bytes());
328 // For variadic arguments, the compiler left either arrays or references.
329 for (unsigned i = type->parameters()->length(); i < argc; i++) {
330 LocalSlot &arg = args[-int(i) - 1];
332 if (arg.obj->is(MapKind_Reference)) {
333 cells[i + 1] = reinterpret_cast<intptr_t>(arg.ref->location());
335 assert(arg.obj->is(MapKind_Array));
336 cells[i + 1] = reinterpret_cast<intptr_t>(arg.array->bytes());
340 typedef intptr_t (*INVOKE)(void *, intptr_t *);
341 INVOKE invoke = (INVOKE)native->function();
342 *result = invoke(identity, cells);
344 return !zone->hasPendingException();
348 ke::Interpret(Zone *zone, Frame *entryFrame, LocalSlot *result)
350 Top &top = *zone->stack().top();
354 LocalSlot *locals = entryFrame->locals();
357 Opcode op = Opcode(*top.pc);
359 if (OpcodeInfo[op].nuses > 0)
360 assert(top.sp - OpcodeInfo[op].nuses >= top.fp->stackBase());
368 PUSH_I32(top, READ_INT32(top.pc));
372 PUSH_F(top, READ_FLOAT(top.pc));
382 if (top.fp == entryFrame)
386 zone->stack().popInlineFrame();
387 locals = top.fp->locals();
392 top.pc += OpcodeInfo[*top.pc].length;
402 #define RELATIONAL_JUMP(op) \
404 int right = POP_I32(top); \
405 int left = POP_I32(top); \
406 if (left op right) { \
407 top.pc += READ_INT32(top.pc); \
436 #undef RELATIONAL_JUMP
440 int value = POP_I32(top);
442 top.pc += READ_INT32(top.pc);
450 int value = POP_I32(top);
452 top.pc += READ_INT32(top.pc);
458 #define RELATIONAL_JUMP_F(op) \
460 float right = POP_F(top); \
461 float left = POP_F(top); \
462 if (left op right) { \
463 top.pc += READ_INT32(top.pc); \
469 RELATIONAL_JUMP_F(<);
473 RELATIONAL_JUMP_F(<=);
477 RELATIONAL_JUMP_F(>);
481 RELATIONAL_JUMP_F(>=);
485 RELATIONAL_JUMP_F(==);
489 RELATIONAL_JUMP_F(!=);
494 float value = POP_F(top);
496 top.pc += READ_INT32(top.pc);
504 float value = POP_F(top);
506 top.pc += READ_INT32(top.pc);
513 top.pc += READ_INT32(top.pc);
518 int slot = READ_INT32(top.pc);
519 *top.sp++ = locals[slot];
525 int slot = READ_INT32(top.pc);
526 locals[slot] = top.sp[-1];
532 top.sp[-2].i32 = top.sp[-2].i32 + top.sp[-1].i32;
539 top.sp[-2].i32 = top.sp[-2].i32 - top.sp[-1].i32;
546 top.sp[-2].i32 = top.sp[-2].i32 * top.sp[-1].i32;
553 if (top.sp[-1].i32 == 0) {
554 zone->reportError(Message_IntegerDivideByZero);
557 top.sp[-2].i32 = top.sp[-2].i32 / top.sp[-1].i32;
564 if (top.sp[-1].i32 == 0) {
565 zone->reportError(Message_IntegerDivideByZero);
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].i32 = top.sp[-2].i32 <= top.sp[-1].i32;
589 top.sp[-2].i32 = top.sp[-2].i32 > top.sp[-1].i32;
596 top.sp[-2].i32 = top.sp[-2].i32 >= top.sp[-1].i32;
603 top.sp[-2].i32 = (top.sp[-2].i32 == top.sp[-1].i32);
610 top.sp[-2].i32 = (top.sp[-2].i32 != top.sp[-1].i32);
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].f = top.sp[-2].f / top.sp[-1].f;
645 top.sp[-2].f = top.sp[-2].f < top.sp[-1].f;
652 top.sp[-2].f = top.sp[-2].f <= top.sp[-1].f;
659 top.sp[-2].f = top.sp[-2].f > top.sp[-1].f;
666 top.sp[-2].f = top.sp[-2].f >= top.sp[-1].f;
673 top.sp[-2].i32 = (top.sp[-2].f == top.sp[-1].f);
680 top.sp[-2].i32 = (top.sp[-2].f != top.sp[-1].f);
687 top.sp[-2].i32 = top.sp[-2].i32 << top.sp[-1].i32;
694 top.sp[-2].i32 = top.sp[-2].i32 >> top.sp[-1].i32;
701 top.sp[-2].i32 = unsigned(top.sp[-2].i32) << unsigned(top.sp[-1].i32);
708 top.sp[-2].i32 = top.sp[-2].i32 & top.sp[-1].i32;
715 top.sp[-2].i32 = top.sp[-2].i32 | top.sp[-1].i32;
722 top.sp[-2].i32 = top.sp[-2].i32 ^ top.sp[-1].i32;
729 top.sp[-1].i32 = ~top.sp[-1].i32;
735 top.sp[-1].i32 = top.sp[-1].i32 ? 0 : 1;
741 top.sp[-1].i32 = top.sp[-1].f ? 0 : 1;
747 top.sp[-1].i32 = -top.sp[-1].i32;
753 top.sp[-1].f = -top.sp[-1].f;
759 Local<Function> newFun(zone, top.sp[-1].fun);
762 if (!zone->stack().pushInlineFrame(newFun))
765 locals = top.fp->locals();
771 unsigned slot = READ_UINT32(top.pc);
772 *top.sp++ = top.fp->arg(slot);
778 unsigned slot = READ_UINT32(top.pc);
779 top.fp->arg(slot) = top.sp[-1];
785 Array *array = Array::cast(top.sp[-2].obj);
786 int index = top.sp[-1].i32;
788 if (index < 0 || unsigned(index) >= array->length()) {
789 zone->reportError(Message_ArrayIndexOutOfBounds, index, array->length());
793 GetElement(array, index, &top.sp[-2]);
800 Array *array = Array::cast(top.sp[-3].obj);
801 int index = top.sp[-2].i32;
803 if (index < 0 || unsigned(index) >= array->length()) {
804 zone->reportError(Message_ArrayIndexOutOfBounds, index, array->length());
808 SetElement(zone, array, index, top.sp[-1]);
809 top.sp[-3] = top.sp[-1];
816 int ival = POP_I32(top);
817 float fval = (float)ival;
824 Heap::Tenure tenure = (Heap::Tenure)top.pc[1];
825 Local<ArrayMap> map(zone, ArrayMap::cast(top.fp->code()->map(READ_UINT32(top.pc + 1))));
826 Array *array = Array::NewFixed(zone, map, tenure);
836 Heap::Tenure tenure = (Heap::Tenure)top.pc[1];
837 Local<ArrayMap> map(zone, ArrayMap::cast(top.fp->code()->map(READ_UINT32(top.pc + 1))));
838 Array *array = Array::NewEmpty(zone, map, tenure);
848 Heap::Tenure tenure = (Heap::Tenure)top.pc[1];
849 Local<ArrayMap> map(zone, ArrayMap::cast(top.fp->code()->map(READ_UINT32(top.pc + 1))));
850 assert(map->type()->levels() < MAX_ARRAY_DEPTH);
852 int dims[MAX_ARRAY_DEPTH];
853 for (unsigned i = map->type()->levels() - 1; i < map->type()->levels(); i--)
854 dims[i] = POP_I32(top);
856 Array *array = Array::NewSized(zone, map, dims, tenure);
866 Heap::Tenure tenure = (Heap::Tenure)top.pc[1];
867 Local<StructMap> map(zone, StructMap::cast(top.fp->code()->map(READ_UINT32(top.pc + 1))));
868 top.sp->obj = Struct::New(zone, map, tenure);
877 unsigned slot = READ_UINT32(top.pc);
878 locals[slot].address = zone->heap().lifoPosition();
884 unsigned slot = READ_UINT32(top.pc);
885 zone->heap().unwindLifo(locals[slot].address);
891 unsigned slot = READ_UINT32(top.pc);
892 if (!PushNewRef(zone, top, &locals[slot], Heap::Tenure_Stack))
899 unsigned slot = READ_UINT32(top.pc);
900 if (!PushNewRef(zone, top, &top.fp->arg(slot), Heap::Tenure_Stack))
905 case OP_SLOTREF_LIFO:
907 Reference *ref = Reference::NewStack(zone, top.sp[-1], Heap::Tenure_Stack);
910 top.sp[-1].ref = ref;
914 case OP_FIELDREF_LIFO:
916 Local<Struct> obj(zone, Struct::cast(top.sp[-1].obj));
917 top.sp[-1].obj = Reference::NewField(zone, obj, READ_UINT32(top.pc), Heap::Tenure_Stack);
925 top.sp[-1].i32 = top.sp[-1].ref->getInt();
931 top.sp[-2].ref->setInt(top.sp[-1].i32);
932 top.sp[-2].i32 = top.sp[-1].i32;
939 top.sp[-1].f = top.sp[-1].ref->getFloat();
945 top.sp[-2].ref->setFloat(top.sp[-1].f);
946 top.sp[-2].f = top.sp[-1].f;
952 top.sp->obj = top.fp->code()->object(READ_UINT32(top.pc));
959 // Note for this assignment opcode, we return the left-hand side, not the
960 // right, since in this case the result of the expression could be different.
961 Local<Array> left(zone, Array::cast(top.sp[-2].array));
962 Local<Array> right(zone, Array::cast(top.sp[-1].array));
963 if (!CopyArray(zone, left, right))
972 top.sp[-1] = top.sp[-2];
979 top.sp[-2] = top.sp[-4];
980 top.sp[-1] = top.sp[-3];
986 LocalSlot slot = top.sp[-2];
987 top.sp[-2] = top.sp[-1];
994 LocalSlot slot = top.sp[-3];
995 top.sp[-3] = top.sp[-2];
996 top.sp[-2] = top.sp[-1];
1004 top.sp[-1] = top.sp[-3];
1011 top.sp[-1] = top.sp[-4];
1017 unsigned index = READ_UINT32(top.pc);
1018 unsigned argc = READ_UINT32(top.pc + 4);
1020 Local<Native> native(zone, Native::cast(top.fp->code()->module()->natives()->at(index)));
1021 if (!native->function()) {
1022 zone->reportError(Message_NativeNotBound, native->name()->chars());
1027 void *nativeIdentity = top.fp->code()->module()->nativeIdentity();
1028 if (!InvokeNative(zone, native, nativeIdentity, top.sp, argc, &result))
1032 PUSH_I32(top, result);
1039 top.sp[-1].module = top.fp->code()->module();
1043 case OP_GETMODULE_FN:
1045 top.sp[-1].fun = top.sp[-1].module->getFunction(READ_UINT32(top.pc));
1049 case OP_GETGLOBAL_I:
1051 unsigned slot = READ_UINT32(top.pc);
1053 top.sp[-1].i32 = top.fp->code()->module()->environment()->getInt32(slot);
1057 case OP_GETGLOBAL_F:
1059 unsigned slot = READ_UINT32(top.pc);
1061 top.sp[-1].f = top.fp->code()->module()->environment()->getFloat(slot);
1065 case OP_GETGLOBAL_O:
1067 unsigned slot = READ_UINT32(top.pc);
1069 top.sp[-1].obj = top.fp->code()->module()->environment()->getObject(slot);
1073 case OP_SETGLOBAL_I:
1075 unsigned slot = READ_UINT32(top.pc);
1076 top.fp->code()->module()->environment()->setInt32(slot, top.sp[-1].i32);
1080 case OP_SETGLOBAL_F:
1082 unsigned slot = READ_UINT32(top.pc);
1083 top.fp->code()->module()->environment()->setFloat(slot, top.sp[-1].f);
1087 case OP_SETGLOBAL_O:
1089 unsigned slot = READ_UINT32(top.pc);
1090 top.fp->code()->module()->environment()->setObject(slot, top.sp[-1].obj);
1097 // Because the stack is a union, these are actually no-ops in the
1118 Struct *obj = Struct::cast(top.sp[-1].obj);
1119 GetField(obj, READ_UINT32(top.pc), &top.sp[-1]);
1125 Struct *obj = Struct::cast(top.sp[-2].obj);
1126 SetField(zone, obj, READ_UINT32(top.pc), top.sp[-1]);
1127 top.sp[-2] = top.sp[-1];
1134 Local<Struct> left(zone, Struct::cast(top.sp[-2].obj));
1135 Local<Struct> right(zone, Struct::cast(top.sp[-1].obj));
1136 if (!CopyStruct(zone, left, right))
1138 top.sp[-2].obj = top.sp[-1].obj;
1143 case OP_TABLESWITCH:
1145 int value = top.sp[-1].i32;
1146 int low = READ_TABLESWITCH_LOW(top.pc);
1147 int high = READ_TABLESWITCH_HIGH(top.pc);
1151 unsigned index = unsigned(value - low);
1152 unsigned tablesize = TableSwitchEntries(low, high);
1153 if (index < tablesize) {
1154 top.pc += OP_TABLESWITCH_LENGTH + OP_JUMP_LENGTH;
1155 top.pc += index * OP_JUMP_LENGTH;
1167 top.pc += OpcodeInfo[op].length;
1168 assert(top.sp >= top.fp->stackBase());
1175 // Unwind frames until we can continue.
1176 while (top.fp != entryFrame)
1177 zone->stack().popInlineFrame();