Allow more empty array creation.
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 if (source->type()->contained()->isArray()) {
103 assert(dest->type()->contained()->isArray());
104 assert(source->length() == dest->length());
106 Local<Array> left(zone);
107 Local<Array> right(zone);
108 for (unsigned i = 0; i < source->length(); i++) {
109 left = Array::cast(dest->readObject(i));
110 right = Array::cast(source->readObject(i));
111 if (!CopyArray(zone, left, right))
114 } else if (source->type()->contained()->isStruct()) {
115 assert(dest->type()->contained() == source->type()->contained());
116 assert(source->length() == dest->length());
118 Local<Struct> left(zone);
119 Local<Struct> right(zone);
120 for (unsigned i = 0; i < source->length(); i++) {
121 left = Struct::cast(dest->readObject(i));
122 right = Struct::cast(source->readObject(i));
123 if (!CopyStruct(zone, left, right))
127 assert(source->type()->contained()->pod() == dest->type()->contained()->pod());
128 assert(source->type()->contained()->pod() == PrimitiveType_Char
129 ? source->length() <= dest->length()
130 : source->length() == dest->length());
132 memcpy(dest->vec(), source->vec(), source->length() * source->elementSize());
138 GetElement(Array *array, int index, LocalSlot *vp)
140 if (array->type()->contained()->isTraceable()) {
141 vp->obj = array->readObject(index);
145 switch (array->type()->contained()->pod()) {
146 case PrimitiveType_Float:
147 vp->f = array->readFloat(index);
150 case PrimitiveType_Char:
151 vp->i32 = array->readChar(index);
155 assert(array->type()->contained()->pod() == PrimitiveType_Int32);
156 vp->i32 = array->readInt32(index);
162 SetElement(Zone *zone, Array *array, int index, const LocalSlot &v)
164 if (array->type()->contained()->isTraceable()) {
165 array->writeObject(zone, index, v.obj);
169 switch (array->type()->contained()->pod()) {
170 case PrimitiveType_Float:
171 array->writeFloat(index, v.f);
174 case PrimitiveType_Char:
175 array->writeChar(index, v.i32);
179 assert(array->type()->contained()->pod() == PrimitiveType_Int32);
180 array->writeInt32(index, v.i32);
186 GetField(Struct *obj, Descriptor *desc, LocalSlot *vp)
188 // :TODO: assert no GC.
189 if (desc->type()->isTraceable()) {
190 vp->obj = obj->get<Object *>(desc);
194 switch (desc->type()->pod()) {
195 case PrimitiveType_Float:
196 vp->f = obj->get<float>(desc);
200 assert(desc->type()->pod() == PrimitiveType_Int32);
201 vp->i32 = obj->get<int>(desc);
207 GetField(Struct *obj, unsigned index, LocalSlot *vp)
209 Descriptor *desc = Descriptor::cast(obj->map()->type()->fields()->at(index));
210 GetField(obj, desc, vp);
214 SetField(Zone *zone, Struct *obj, Descriptor *desc, const LocalSlot &v)
216 // :TODO: assert no GC.
217 if (desc->type()->isTraceable()) {
218 obj->set(zone, desc, v.obj);
222 switch (desc->type()->pod()) {
223 case PrimitiveType_Float:
224 obj->set<float>(desc, v.f);
228 assert(desc->type()->pod() == PrimitiveType_Int32);
229 obj->set<int>(desc, v.i32);
235 SetField(Zone *zone, Struct *obj, unsigned index, const LocalSlot &v)
237 Descriptor *desc = Descriptor::cast(obj->map()->type()->fields()->at(index));
238 SetField(zone, obj, desc, v);
241 // Even though this function can fail, we immediately bail out, so it doesn't
242 // matter if we've leaving things unrooted.
244 CopyStruct(Zone *zone, Handle<Struct> left, Handle<Struct> right)
246 assert(left->map()->type() == right->map()->type());
248 Local<Type> type(zone, left->map()->type());
249 Local<Struct> leftStruct(zone);
250 Local<Struct> rightStruct(zone);
251 Local<Array> leftArray(zone);
252 Local<Array> rightArray(zone);
253 for (unsigned i = 0; i < type->fields()->length(); i++) {
254 Descriptor *desc = Descriptor::cast(type->fields()->at(i));
257 GetField(right, desc, &rval);
259 // If the field is a struct, we need to deep-copy.
260 if (desc->type()->isStruct()) {
263 GetField(left, desc, &lval);
264 leftStruct = Struct::cast(lval.obj);
265 rightStruct = Struct::cast(rval.obj);
266 if (!CopyStruct(zone, leftStruct, rightStruct))
268 } else if (desc->type()->isArray()) {
271 GetField(left, desc, &lval);
272 leftArray = Array::cast(lval.obj);
273 rightArray = Array::cast(rval.obj);
274 if (!CopyArray(zone, leftArray, rightArray))
277 SetField(zone, left, desc, rval);
285 InvokeNative(Zone *zone, Handle<Native> native, void *identity, LocalSlot *args, unsigned argc,
290 if (!zone->stack().pushNativeFrame(native, guard, argc + 1))
293 intptr_t *cells = guard.args();
295 // params[0] == argc, in ISPE
298 // Pass the formal arguments.
299 Local<Type> type(zone, native->type());
300 Local<Type> param(zone);
301 for (unsigned i = 0; i < type->parameters()->length(); i++) {
302 param = type->parameterAt(i);
304 // For floats, we rely on the aliasing of |i32| and |f|.
305 LocalSlot &arg = args[-int(i) - 1];
306 if (param->isInt32() || param->isFloat()) {
307 cells[i + 1] = arg.i32;
308 } else if (param->isReference()) {
309 cells[i + 1] = reinterpret_cast<intptr_t>(arg.ref->location());
311 assert(param->isArray());
312 cells[i + 1] = reinterpret_cast<intptr_t>(arg.array->vec());
316 // For variadic arguments, the compiler left either arrays or references.
317 for (unsigned i = type->parameters()->length(); i < argc; i++) {
318 LocalSlot &arg = args[-int(i) - 1];
320 if (arg.obj->is(MapKind_Reference)) {
321 cells[i + 1] = reinterpret_cast<intptr_t>(arg.ref->location());
323 assert(arg.obj->is(MapKind_Array));
324 cells[i + 1] = reinterpret_cast<intptr_t>(arg.array->vec());
328 typedef intptr_t (*INVOKE)(void *, intptr_t *);
329 INVOKE invoke = (INVOKE)native->function();
330 *result = invoke(identity, cells);
332 return !zone->hasPendingException();
336 ke::Interpret(Zone *zone, Frame *entryFrame, LocalSlot *result)
338 Top &top = *zone->stack().top();
342 LocalSlot *locals = entryFrame->locals();
345 Opcode op = Opcode(*top.pc);
347 if (OpcodeInfo[op].nuses > 0)
348 assert(top.sp - OpcodeInfo[op].nuses >= top.fp->stackBase());
356 PUSH_I32(top, READ_INT32(top.pc));
360 PUSH_F(top, READ_FLOAT(top.pc));
370 if (top.fp == entryFrame)
374 zone->stack().popInlineFrame();
375 locals = top.fp->locals();
380 top.pc += OpcodeInfo[*top.pc].length;
390 #define RELATIONAL_JUMP(op) \
392 int right = POP_I32(top); \
393 int left = POP_I32(top); \
394 if (left op right) { \
395 top.pc += READ_INT32(top.pc); \
424 #undef RELATIONAL_JUMP
428 int value = POP_I32(top);
430 top.pc += READ_INT32(top.pc);
438 int value = POP_I32(top);
440 top.pc += READ_INT32(top.pc);
446 #define RELATIONAL_JUMP_F(op) \
448 float right = POP_F(top); \
449 float left = POP_F(top); \
450 if (left op right) { \
451 top.pc += READ_INT32(top.pc); \
457 RELATIONAL_JUMP_F(<);
461 RELATIONAL_JUMP_F(<=);
465 RELATIONAL_JUMP_F(>);
469 RELATIONAL_JUMP_F(>=);
473 RELATIONAL_JUMP_F(==);
477 RELATIONAL_JUMP_F(!=);
482 float value = POP_F(top);
484 top.pc += READ_INT32(top.pc);
492 float value = POP_F(top);
494 top.pc += READ_INT32(top.pc);
501 top.pc += READ_INT32(top.pc);
506 int slot = READ_INT32(top.pc);
507 *top.sp++ = locals[slot];
513 int slot = READ_INT32(top.pc);
514 locals[slot] = top.sp[-1];
520 top.sp[-2].i32 = top.sp[-2].i32 + top.sp[-1].i32;
527 top.sp[-2].i32 = top.sp[-2].i32 - top.sp[-1].i32;
534 top.sp[-2].i32 = top.sp[-2].i32 * top.sp[-1].i32;
541 if (top.sp[-1].i32 == 0) {
542 zone->reportError(Message_IntegerDivideByZero);
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 top.sp[-2].i32 = top.sp[-2].i32 < top.sp[-1].i32;
570 top.sp[-2].i32 = top.sp[-2].i32 <= top.sp[-1].i32;
577 top.sp[-2].i32 = top.sp[-2].i32 > top.sp[-1].i32;
584 top.sp[-2].i32 = top.sp[-2].i32 >= top.sp[-1].i32;
591 top.sp[-2].i32 = (top.sp[-2].i32 == top.sp[-1].i32);
598 top.sp[-2].i32 = (top.sp[-2].i32 != top.sp[-1].i32);
605 top.sp[-2].f = top.sp[-2].f + top.sp[-1].f;
612 top.sp[-2].f = top.sp[-2].f - top.sp[-1].f;
619 top.sp[-2].f = top.sp[-2].f * top.sp[-1].f;
626 top.sp[-2].f = top.sp[-2].f / top.sp[-1].f;
633 top.sp[-2].f = top.sp[-2].f < top.sp[-1].f;
640 top.sp[-2].f = top.sp[-2].f <= top.sp[-1].f;
647 top.sp[-2].f = top.sp[-2].f > top.sp[-1].f;
654 top.sp[-2].f = top.sp[-2].f >= top.sp[-1].f;
661 top.sp[-2].i32 = (top.sp[-2].f == top.sp[-1].f);
668 top.sp[-2].i32 = (top.sp[-2].f != top.sp[-1].f);
675 top.sp[-2].i32 = top.sp[-2].i32 << top.sp[-1].i32;
682 top.sp[-2].i32 = top.sp[-2].i32 >> top.sp[-1].i32;
689 top.sp[-2].i32 = unsigned(top.sp[-2].i32) << unsigned(top.sp[-1].i32);
696 top.sp[-2].i32 = top.sp[-2].i32 & top.sp[-1].i32;
703 top.sp[-2].i32 = top.sp[-2].i32 | top.sp[-1].i32;
710 top.sp[-2].i32 = top.sp[-2].i32 ^ top.sp[-1].i32;
717 top.sp[-1].i32 = ~top.sp[-1].i32;
723 top.sp[-1].i32 = top.sp[-1].i32 ? 0 : 1;
729 top.sp[-1].i32 = top.sp[-1].f ? 0 : 1;
735 top.sp[-1].i32 = -top.sp[-1].i32;
741 top.sp[-1].f = -top.sp[-1].f;
747 Local<Function> newFun(zone, top.sp[-1].fun);
750 if (!zone->stack().pushInlineFrame(newFun))
753 locals = top.fp->locals();
759 unsigned slot = READ_UINT32(top.pc);
760 *top.sp++ = top.fp->arg(slot);
766 unsigned slot = READ_UINT32(top.pc);
767 top.fp->arg(slot) = top.sp[-1];
773 Array *array = Array::cast(top.sp[-2].obj);
774 int index = top.sp[-1].i32;
776 if (index < 0 || unsigned(index) >= array->length()) {
777 zone->reportError(Message_ArrayIndexOutOfBounds, index, array->length());
781 GetElement(array, index, &top.sp[-2]);
788 Array *array = Array::cast(top.sp[-3].obj);
789 int index = top.sp[-2].i32;
791 if (index < 0 || unsigned(index) >= array->length()) {
792 zone->reportError(Message_ArrayIndexOutOfBounds, index, array->length());
796 SetElement(zone, array, index, top.sp[-1]);
797 top.sp[-3] = top.sp[-1];
804 int ival = POP_I32(top);
805 float fval = (float)ival;
812 Heap::Tenure tenure = (Heap::Tenure)top.pc[1];
813 Local<ArrayMap> map(zone, ArrayMap::cast(top.fp->code()->map(READ_UINT32(top.pc + 1))));
814 Array *array = Array::New(zone, map, tenure);
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::New(zone, map, 0, tenure);
836 Heap::Tenure tenure = (Heap::Tenure)top.pc[1];
837 Local<StructMap> map(zone, StructMap::cast(top.fp->code()->map(READ_UINT32(top.pc + 1))));
838 top.sp->obj = Struct::New(zone, map, tenure);
847 unsigned slot = READ_UINT32(top.pc);
848 locals[slot].address = zone->heap().lifoPosition();
854 unsigned slot = READ_UINT32(top.pc);
855 zone->heap().unwindLifo(locals[slot].address);
861 unsigned slot = READ_UINT32(top.pc);
862 if (!PushNewRef(zone, top, &locals[slot], Heap::Tenure_Stack))
869 unsigned slot = READ_UINT32(top.pc);
870 if (!PushNewRef(zone, top, &top.fp->arg(slot), Heap::Tenure_Stack))
875 case OP_ARRAYREF_LIFO:
877 Local<Array> array(zone, Array::cast(top.sp[-2].obj));
879 int index = top.sp[-1].i32;
880 if (index < 0 || unsigned(index) >= array->length()) {
881 zone->reportError(Message_ArrayIndexOutOfBounds, index, array->length());
885 Local<Reference> ref(zone, Reference::NewArray(zone, array, index, Heap::Tenure_Stack));
888 top.sp[-2].ref = ref;
893 case OP_SLOTREF_LIFO:
895 Reference *ref = Reference::NewStack(zone, top.sp[-1], Heap::Tenure_Stack);
898 top.sp[-1].ref = ref;
902 case OP_FIELDREF_LIFO:
904 Local<Struct> obj(zone, Struct::cast(top.sp[-1].obj));
905 top.sp[-1].obj = Reference::NewField(zone, obj, READ_UINT32(top.pc), Heap::Tenure_Stack);
913 top.sp[-1].i32 = top.sp[-1].ref->getInt();
919 top.sp[-2].ref->setInt(top.sp[-1].i32);
920 top.sp[-2].i32 = top.sp[-1].i32;
927 top.sp[-1].f = top.sp[-1].ref->getFloat();
933 top.sp[-2].ref->setFloat(top.sp[-1].f);
934 top.sp[-2].f = top.sp[-1].f;
940 top.sp->obj = top.fp->code()->object(READ_UINT32(top.pc));
947 // Note for this assignment opcode, we return the left-hand side, not the
948 // right, since in this case the result of the expression could be different.
949 Local<Array> left(zone, Array::cast(top.sp[-2].array));
950 Local<Array> right(zone, Array::cast(top.sp[-1].array));
951 if (!CopyArray(zone, left, right))
960 top.sp[-1] = top.sp[-2];
967 top.sp[-2] = top.sp[-4];
968 top.sp[-1] = top.sp[-3];
974 LocalSlot slot = top.sp[-2];
975 top.sp[-2] = top.sp[-1];
982 LocalSlot slot = top.sp[-3];
983 top.sp[-3] = top.sp[-2];
984 top.sp[-2] = top.sp[-1];
992 top.sp[-1] = top.sp[-3];
999 top.sp[-1] = top.sp[-4];
1005 unsigned index = READ_UINT32(top.pc);
1006 unsigned argc = READ_UINT32(top.pc + 4);
1008 Local<Native> native(zone, Native::cast(top.fp->code()->module()->natives()->at(index)));
1009 if (!native->function()) {
1010 zone->reportError(Message_NativeNotBound, native->name()->chars());
1015 void *nativeIdentity = top.fp->code()->module()->nativeIdentity();
1016 if (!InvokeNative(zone, native, nativeIdentity, top.sp, argc, &result))
1020 PUSH_I32(top, result);
1027 top.sp[-1].module = top.fp->code()->module();
1031 case OP_GETMODULE_FN:
1033 top.sp[-1].fun = top.sp[-1].module->getFunction(READ_UINT32(top.pc));
1037 case OP_GETGLOBAL_I:
1039 unsigned slot = READ_UINT32(top.pc);
1041 top.sp[-1].i32 = top.fp->code()->module()->environment()->getInt32(slot);
1045 case OP_GETGLOBAL_F:
1047 unsigned slot = READ_UINT32(top.pc);
1049 top.sp[-1].f = top.fp->code()->module()->environment()->getFloat(slot);
1053 case OP_GETGLOBAL_O:
1055 unsigned slot = READ_UINT32(top.pc);
1057 top.sp[-1].obj = top.fp->code()->module()->environment()->getObject(slot);
1061 case OP_SETGLOBAL_I:
1063 unsigned slot = READ_UINT32(top.pc);
1064 top.fp->code()->module()->environment()->setInt32(slot, top.sp[-1].i32);
1068 case OP_SETGLOBAL_F:
1070 unsigned slot = READ_UINT32(top.pc);
1071 top.fp->code()->module()->environment()->setFloat(slot, top.sp[-1].f);
1075 case OP_SETGLOBAL_O:
1077 unsigned slot = READ_UINT32(top.pc);
1078 top.fp->code()->module()->environment()->setObject(slot, top.sp[-1].obj);
1085 // Because the stack is a union, these are actually no-ops in the
1106 Struct *obj = Struct::cast(top.sp[-1].obj);
1107 GetField(obj, READ_UINT32(top.pc), &top.sp[-1]);
1113 Struct *obj = Struct::cast(top.sp[-2].obj);
1114 SetField(zone, obj, READ_UINT32(top.pc), top.sp[-1]);
1115 top.sp[-2] = top.sp[-1];
1122 Local<Struct> left(zone, Struct::cast(top.sp[-2].obj));
1123 Local<Struct> right(zone, Struct::cast(top.sp[-1].obj));
1124 if (!CopyStruct(zone, left, right))
1126 top.sp[-2].obj = top.sp[-1].obj;
1131 case OP_TABLESWITCH:
1133 int value = top.sp[-1].i32;
1134 int low = READ_TABLESWITCH_LOW(top.pc);
1135 int high = READ_TABLESWITCH_HIGH(top.pc);
1139 unsigned index = unsigned(value - low);
1140 unsigned tablesize = TableSwitchEntries(low, high);
1141 if (index < tablesize) {
1142 top.pc += OP_TABLESWITCH_LENGTH + OP_JUMP_LENGTH;
1143 top.pc += index * OP_JUMP_LENGTH;
1155 top.pc += OpcodeInfo[op].length;
1156 assert(top.sp >= top.fp->stackBase());
1163 // Unwind frames until we can continue.
1164 while (top.fp != entryFrame)
1165 zone->stack().popInlineFrame();