Add support for SP1-style fixed array initializers.
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 memcpy(dest->vec(), source->vec(), source->length() * source->elementSize());
149 GetElement(Array *array, int index, LocalSlot *vp)
151 if (array->type()->contained()->isTraceable()) {
152 vp->obj = array->readObject(index);
156 switch (array->type()->contained()->pod()) {
157 case PrimitiveType_Float:
158 vp->f = array->readFloat(index);
161 case PrimitiveType_Char:
162 vp->i32 = array->readChar(index);
166 assert(array->type()->contained()->pod() == PrimitiveType_Int32);
167 vp->i32 = array->readInt32(index);
173 SetElement(Zone *zone, Array *array, int index, const LocalSlot &v)
175 if (array->type()->contained()->isTraceable()) {
176 array->writeObject(zone, index, v.obj);
180 switch (array->type()->contained()->pod()) {
181 case PrimitiveType_Float:
182 array->writeFloat(index, v.f);
185 case PrimitiveType_Char:
186 array->writeChar(index, v.i32);
190 assert(array->type()->contained()->pod() == PrimitiveType_Int32);
191 array->writeInt32(index, v.i32);
197 GetField(Struct *obj, Descriptor *desc, LocalSlot *vp)
199 // :TODO: assert no GC.
200 if (desc->type()->isTraceable()) {
201 vp->obj = obj->get<Object *>(desc);
205 switch (desc->type()->pod()) {
206 case PrimitiveType_Float:
207 vp->f = obj->get<float>(desc);
211 assert(desc->type()->pod() == PrimitiveType_Int32);
212 vp->i32 = obj->get<int>(desc);
218 GetField(Struct *obj, unsigned index, LocalSlot *vp)
220 Descriptor *desc = Descriptor::cast(obj->map()->type()->fields()->at(index));
221 GetField(obj, desc, vp);
225 SetField(Zone *zone, Struct *obj, Descriptor *desc, const LocalSlot &v)
227 // :TODO: assert no GC.
228 if (desc->type()->isTraceable()) {
229 obj->set(zone, desc, v.obj);
233 switch (desc->type()->pod()) {
234 case PrimitiveType_Float:
235 obj->set<float>(desc, v.f);
239 assert(desc->type()->pod() == PrimitiveType_Int32);
240 obj->set<int>(desc, v.i32);
246 SetField(Zone *zone, Struct *obj, unsigned index, const LocalSlot &v)
248 Descriptor *desc = Descriptor::cast(obj->map()->type()->fields()->at(index));
249 SetField(zone, obj, desc, v);
252 // Even though this function can fail, we immediately bail out, so it doesn't
253 // matter if we've leaving things unrooted.
255 CopyStruct(Zone *zone, Handle<Struct> left, Handle<Struct> right)
257 assert(left->map()->type() == right->map()->type());
259 Local<Type> type(zone, left->map()->type());
260 Local<Struct> leftStruct(zone);
261 Local<Struct> rightStruct(zone);
262 Local<Array> leftArray(zone);
263 Local<Array> rightArray(zone);
264 for (unsigned i = 0; i < type->fields()->length(); i++) {
265 Descriptor *desc = Descriptor::cast(type->fields()->at(i));
268 GetField(right, desc, &rval);
270 // If the field is a struct, we need to deep-copy.
271 if (desc->type()->isStruct()) {
274 GetField(left, desc, &lval);
275 leftStruct = Struct::cast(lval.obj);
276 rightStruct = Struct::cast(rval.obj);
277 if (!CopyStruct(zone, leftStruct, rightStruct))
279 } else if (desc->type()->isArray()) {
282 GetField(left, desc, &lval);
283 leftArray = Array::cast(lval.obj);
284 rightArray = Array::cast(rval.obj);
285 if (!CopyArray(zone, leftArray, rightArray))
288 SetField(zone, left, desc, rval);
296 InvokeNative(Zone *zone, Handle<Native> native, void *identity, LocalSlot *args, unsigned argc,
301 if (!zone->stack().pushNativeFrame(native, guard, argc + 1))
304 intptr_t *cells = guard.args();
306 // params[0] == argc, in ISPE
309 // Pass the formal arguments.
310 Local<Type> type(zone, native->type());
311 Local<Type> param(zone);
312 for (unsigned i = 0; i < type->parameters()->length(); i++) {
313 param = type->parameterAt(i);
315 // For floats, we rely on the aliasing of |i32| and |f|.
316 LocalSlot &arg = args[-int(i) - 1];
317 if (param->isInt32() || param->isFloat()) {
318 cells[i + 1] = arg.i32;
319 } else if (param->isReference()) {
320 cells[i + 1] = reinterpret_cast<intptr_t>(arg.ref->location());
322 assert(param->isArray());
323 cells[i + 1] = reinterpret_cast<intptr_t>(arg.array->vec());
327 // For variadic arguments, the compiler left either arrays or references.
328 for (unsigned i = type->parameters()->length(); i < argc; i++) {
329 LocalSlot &arg = args[-int(i) - 1];
331 if (arg.obj->is(MapKind_Reference)) {
332 cells[i + 1] = reinterpret_cast<intptr_t>(arg.ref->location());
334 assert(arg.obj->is(MapKind_Array));
335 cells[i + 1] = reinterpret_cast<intptr_t>(arg.array->vec());
339 typedef intptr_t (*INVOKE)(void *, intptr_t *);
340 INVOKE invoke = (INVOKE)native->function();
341 *result = invoke(identity, cells);
343 return !zone->hasPendingException();
347 ke::Interpret(Zone *zone, Frame *entryFrame, LocalSlot *result)
349 Top &top = *zone->stack().top();
353 LocalSlot *locals = entryFrame->locals();
356 Opcode op = Opcode(*top.pc);
358 if (OpcodeInfo[op].nuses > 0)
359 assert(top.sp - OpcodeInfo[op].nuses >= top.fp->stackBase());
367 PUSH_I32(top, READ_INT32(top.pc));
371 PUSH_F(top, READ_FLOAT(top.pc));
381 if (top.fp == entryFrame)
385 zone->stack().popInlineFrame();
386 locals = top.fp->locals();
391 top.pc += OpcodeInfo[*top.pc].length;
401 #define RELATIONAL_JUMP(op) \
403 int right = POP_I32(top); \
404 int left = POP_I32(top); \
405 if (left op right) { \
406 top.pc += READ_INT32(top.pc); \
435 #undef RELATIONAL_JUMP
439 int value = POP_I32(top);
441 top.pc += READ_INT32(top.pc);
449 int value = POP_I32(top);
451 top.pc += READ_INT32(top.pc);
457 #define RELATIONAL_JUMP_F(op) \
459 float right = POP_F(top); \
460 float left = POP_F(top); \
461 if (left op right) { \
462 top.pc += READ_INT32(top.pc); \
468 RELATIONAL_JUMP_F(<);
472 RELATIONAL_JUMP_F(<=);
476 RELATIONAL_JUMP_F(>);
480 RELATIONAL_JUMP_F(>=);
484 RELATIONAL_JUMP_F(==);
488 RELATIONAL_JUMP_F(!=);
493 float value = POP_F(top);
495 top.pc += READ_INT32(top.pc);
503 float value = POP_F(top);
505 top.pc += READ_INT32(top.pc);
512 top.pc += READ_INT32(top.pc);
517 int slot = READ_INT32(top.pc);
518 *top.sp++ = locals[slot];
524 int slot = READ_INT32(top.pc);
525 locals[slot] = top.sp[-1];
531 top.sp[-2].i32 = top.sp[-2].i32 + top.sp[-1].i32;
538 top.sp[-2].i32 = top.sp[-2].i32 - top.sp[-1].i32;
545 top.sp[-2].i32 = top.sp[-2].i32 * top.sp[-1].i32;
552 if (top.sp[-1].i32 == 0) {
553 zone->reportError(Message_IntegerDivideByZero);
556 top.sp[-2].i32 = top.sp[-2].i32 / top.sp[-1].i32;
563 if (top.sp[-1].i32 == 0) {
564 zone->reportError(Message_IntegerDivideByZero);
567 top.sp[-2].i32 = top.sp[-2].i32 % top.sp[-1].i32;
574 top.sp[-2].i32 = top.sp[-2].i32 < top.sp[-1].i32;
581 top.sp[-2].i32 = top.sp[-2].i32 <= top.sp[-1].i32;
588 top.sp[-2].i32 = top.sp[-2].i32 > top.sp[-1].i32;
595 top.sp[-2].i32 = top.sp[-2].i32 >= top.sp[-1].i32;
602 top.sp[-2].i32 = (top.sp[-2].i32 == top.sp[-1].i32);
609 top.sp[-2].i32 = (top.sp[-2].i32 != top.sp[-1].i32);
616 top.sp[-2].f = top.sp[-2].f + top.sp[-1].f;
623 top.sp[-2].f = top.sp[-2].f - top.sp[-1].f;
630 top.sp[-2].f = top.sp[-2].f * top.sp[-1].f;
637 top.sp[-2].f = top.sp[-2].f / top.sp[-1].f;
644 top.sp[-2].f = top.sp[-2].f < top.sp[-1].f;
651 top.sp[-2].f = top.sp[-2].f <= top.sp[-1].f;
658 top.sp[-2].f = top.sp[-2].f > top.sp[-1].f;
665 top.sp[-2].f = top.sp[-2].f >= top.sp[-1].f;
672 top.sp[-2].i32 = (top.sp[-2].f == top.sp[-1].f);
679 top.sp[-2].i32 = (top.sp[-2].f != top.sp[-1].f);
686 top.sp[-2].i32 = top.sp[-2].i32 << top.sp[-1].i32;
693 top.sp[-2].i32 = top.sp[-2].i32 >> top.sp[-1].i32;
700 top.sp[-2].i32 = unsigned(top.sp[-2].i32) << unsigned(top.sp[-1].i32);
707 top.sp[-2].i32 = top.sp[-2].i32 & top.sp[-1].i32;
714 top.sp[-2].i32 = top.sp[-2].i32 | top.sp[-1].i32;
721 top.sp[-2].i32 = top.sp[-2].i32 ^ top.sp[-1].i32;
728 top.sp[-1].i32 = ~top.sp[-1].i32;
734 top.sp[-1].i32 = top.sp[-1].i32 ? 0 : 1;
740 top.sp[-1].i32 = top.sp[-1].f ? 0 : 1;
746 top.sp[-1].i32 = -top.sp[-1].i32;
752 top.sp[-1].f = -top.sp[-1].f;
758 Local<Function> newFun(zone, top.sp[-1].fun);
761 if (!zone->stack().pushInlineFrame(newFun))
764 locals = top.fp->locals();
770 unsigned slot = READ_UINT32(top.pc);
771 *top.sp++ = top.fp->arg(slot);
777 unsigned slot = READ_UINT32(top.pc);
778 top.fp->arg(slot) = top.sp[-1];
784 Array *array = Array::cast(top.sp[-2].obj);
785 int index = top.sp[-1].i32;
787 if (index < 0 || unsigned(index) >= array->length()) {
788 zone->reportError(Message_ArrayIndexOutOfBounds, index, array->length());
792 GetElement(array, index, &top.sp[-2]);
799 Array *array = Array::cast(top.sp[-3].obj);
800 int index = top.sp[-2].i32;
802 if (index < 0 || unsigned(index) >= array->length()) {
803 zone->reportError(Message_ArrayIndexOutOfBounds, index, array->length());
807 SetElement(zone, array, index, top.sp[-1]);
808 top.sp[-3] = top.sp[-1];
815 int ival = POP_I32(top);
816 float fval = (float)ival;
823 Heap::Tenure tenure = (Heap::Tenure)top.pc[1];
824 Local<ArrayMap> map(zone, ArrayMap::cast(top.fp->code()->map(READ_UINT32(top.pc + 1))));
825 Array *array = Array::NewFixed(zone, map, tenure);
835 Heap::Tenure tenure = (Heap::Tenure)top.pc[1];
836 Local<ArrayMap> map(zone, ArrayMap::cast(top.fp->code()->map(READ_UINT32(top.pc + 1))));
837 Array *array = Array::NewEmpty(zone, map, tenure);
847 Heap::Tenure tenure = (Heap::Tenure)top.pc[1];
848 Local<ArrayMap> map(zone, ArrayMap::cast(top.fp->code()->map(READ_UINT32(top.pc + 1))));
849 assert(map->type()->levels() < MAX_ARRAY_DEPTH);
851 int dims[MAX_ARRAY_DEPTH];
852 for (unsigned i = map->type()->levels() - 1; i < map->type()->levels(); i--)
853 dims[i] = POP_I32(top);
855 Array *array = Array::NewSized(zone, map, dims, tenure);
865 Heap::Tenure tenure = (Heap::Tenure)top.pc[1];
866 Local<StructMap> map(zone, StructMap::cast(top.fp->code()->map(READ_UINT32(top.pc + 1))));
867 top.sp->obj = Struct::New(zone, map, tenure);
876 unsigned slot = READ_UINT32(top.pc);
877 locals[slot].address = zone->heap().lifoPosition();
883 unsigned slot = READ_UINT32(top.pc);
884 zone->heap().unwindLifo(locals[slot].address);
890 unsigned slot = READ_UINT32(top.pc);
891 if (!PushNewRef(zone, top, &locals[slot], Heap::Tenure_Stack))
898 unsigned slot = READ_UINT32(top.pc);
899 if (!PushNewRef(zone, top, &top.fp->arg(slot), Heap::Tenure_Stack))
904 case OP_ARRAYREF_LIFO:
906 Local<Array> array(zone, Array::cast(top.sp[-2].obj));
908 int index = top.sp[-1].i32;
909 if (index < 0 || unsigned(index) >= array->length()) {
910 zone->reportError(Message_ArrayIndexOutOfBounds, index, array->length());
914 Local<Reference> ref(zone, Reference::NewArray(zone, array, index, Heap::Tenure_Stack));
917 top.sp[-2].ref = ref;
922 case OP_SLOTREF_LIFO:
924 Reference *ref = Reference::NewStack(zone, top.sp[-1], Heap::Tenure_Stack);
927 top.sp[-1].ref = ref;
931 case OP_FIELDREF_LIFO:
933 Local<Struct> obj(zone, Struct::cast(top.sp[-1].obj));
934 top.sp[-1].obj = Reference::NewField(zone, obj, READ_UINT32(top.pc), Heap::Tenure_Stack);
942 top.sp[-1].i32 = top.sp[-1].ref->getInt();
948 top.sp[-2].ref->setInt(top.sp[-1].i32);
949 top.sp[-2].i32 = top.sp[-1].i32;
956 top.sp[-1].f = top.sp[-1].ref->getFloat();
962 top.sp[-2].ref->setFloat(top.sp[-1].f);
963 top.sp[-2].f = top.sp[-1].f;
969 top.sp->obj = top.fp->code()->object(READ_UINT32(top.pc));
976 // Note for this assignment opcode, we return the left-hand side, not the
977 // right, since in this case the result of the expression could be different.
978 Local<Array> left(zone, Array::cast(top.sp[-2].array));
979 Local<Array> right(zone, Array::cast(top.sp[-1].array));
980 if (!CopyArray(zone, left, right))
989 top.sp[-1] = top.sp[-2];
996 top.sp[-2] = top.sp[-4];
997 top.sp[-1] = top.sp[-3];
1003 LocalSlot slot = top.sp[-2];
1004 top.sp[-2] = top.sp[-1];
1011 LocalSlot slot = top.sp[-3];
1012 top.sp[-3] = top.sp[-2];
1013 top.sp[-2] = top.sp[-1];
1021 top.sp[-1] = top.sp[-3];
1028 top.sp[-1] = top.sp[-4];
1034 unsigned index = READ_UINT32(top.pc);
1035 unsigned argc = READ_UINT32(top.pc + 4);
1037 Local<Native> native(zone, Native::cast(top.fp->code()->module()->natives()->at(index)));
1038 if (!native->function()) {
1039 zone->reportError(Message_NativeNotBound, native->name()->chars());
1044 void *nativeIdentity = top.fp->code()->module()->nativeIdentity();
1045 if (!InvokeNative(zone, native, nativeIdentity, top.sp, argc, &result))
1049 PUSH_I32(top, result);
1056 top.sp[-1].module = top.fp->code()->module();
1060 case OP_GETMODULE_FN:
1062 top.sp[-1].fun = top.sp[-1].module->getFunction(READ_UINT32(top.pc));
1066 case OP_GETGLOBAL_I:
1068 unsigned slot = READ_UINT32(top.pc);
1070 top.sp[-1].i32 = top.fp->code()->module()->environment()->getInt32(slot);
1074 case OP_GETGLOBAL_F:
1076 unsigned slot = READ_UINT32(top.pc);
1078 top.sp[-1].f = top.fp->code()->module()->environment()->getFloat(slot);
1082 case OP_GETGLOBAL_O:
1084 unsigned slot = READ_UINT32(top.pc);
1086 top.sp[-1].obj = top.fp->code()->module()->environment()->getObject(slot);
1090 case OP_SETGLOBAL_I:
1092 unsigned slot = READ_UINT32(top.pc);
1093 top.fp->code()->module()->environment()->setInt32(slot, top.sp[-1].i32);
1097 case OP_SETGLOBAL_F:
1099 unsigned slot = READ_UINT32(top.pc);
1100 top.fp->code()->module()->environment()->setFloat(slot, top.sp[-1].f);
1104 case OP_SETGLOBAL_O:
1106 unsigned slot = READ_UINT32(top.pc);
1107 top.fp->code()->module()->environment()->setObject(slot, top.sp[-1].obj);
1114 // Because the stack is a union, these are actually no-ops in the
1135 Struct *obj = Struct::cast(top.sp[-1].obj);
1136 GetField(obj, READ_UINT32(top.pc), &top.sp[-1]);
1142 Struct *obj = Struct::cast(top.sp[-2].obj);
1143 SetField(zone, obj, READ_UINT32(top.pc), top.sp[-1]);
1144 top.sp[-2] = top.sp[-1];
1151 Local<Struct> left(zone, Struct::cast(top.sp[-2].obj));
1152 Local<Struct> right(zone, Struct::cast(top.sp[-1].obj));
1153 if (!CopyStruct(zone, left, right))
1155 top.sp[-2].obj = top.sp[-1].obj;
1160 case OP_TABLESWITCH:
1162 int value = top.sp[-1].i32;
1163 int low = READ_TABLESWITCH_LOW(top.pc);
1164 int high = READ_TABLESWITCH_HIGH(top.pc);
1168 unsigned index = unsigned(value - low);
1169 unsigned tablesize = TableSwitchEntries(low, high);
1170 if (index < tablesize) {
1171 top.pc += OP_TABLESWITCH_LENGTH + OP_JUMP_LENGTH;
1172 top.pc += index * OP_JUMP_LENGTH;
1184 top.pc += OpcodeInfo[op].length;
1185 assert(top.sp >= top.fp->stackBase());
1192 // Unwind frames until we can continue.
1193 while (top.fp != entryFrame)
1194 zone->stack().popInlineFrame();