Get preinc working again.
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"
29 #include "NativeTable.h"
34 #define PUSH_I32(top, value) \
36 top.sp->i32 = value; \
40 #define PUSH_F(top, value) \
49 int value = top.sp[-1].i32;
57 float value = top.sp[-1].f;
62 // Pawn's original semantics specify some kind of floor operation, so
63 // (8 / -3 == -3) and (-8 / 3 == -1). The SourcePawn 1 compiler (spcomp) uses
64 // the upstream algorithm when constant folding. However the SourcePawn 1 JIT
65 // does not. My guess is that Borja freaked out seeing the 13-instruction x86
66 // sequence necessary to get this functionality (I vaguely recall a thread
67 // where we discussed this with Mark Peter? But the ITB forums are gone...),
68 // and we just decided to break the damn thing. So SP1 was inconsistent, but
69 // we are not. And for completeness, we assert that the C compiler does the
70 // right thing, since C89 leaves it unspecified.
71 STATIC_ASSERT(8 / -3 == -2);
72 STATIC_ASSERT(-8 / 3 == -2);
75 PushNewRef(Zone *zone, Top &top, void *location, Heap::Tenure tenure)
77 Reference *ref = Reference::NewStack(zone, location, tenure);
85 static bool CopyStruct(Zone *zone, Handle<Struct> left, Handle<Struct> right);
88 ChangeArrayLength(Zone *zone, Handle<Array> source, Handle<Array> dest)
93 // Even though this function can fail, we immediately bail out, so it doesn't
94 // matter if we've leaving things unrooted.
96 CopyArray(Zone *zone, Handle<Array> dest, Handle<Array> source)
98 if (source->length() != dest->length()) {
99 if (!ChangeArrayLength(zone, source, dest))
103 Local<Type> contained(zone, source->type()->contained());
104 if (contained->isArray() && ArrayType::cast(contained)->isFixedLength()) {
105 assert(dest->type()->contained()->isArray());
106 assert(source->length() == dest->length());
108 Local<Array> left(zone);
109 Local<Array> right(zone);
110 for (unsigned i = 0; i < source->length(); i++) {
111 left = Array::cast(dest->readObject(i));
112 right = Array::cast(source->readObject(i));
113 if (!CopyArray(zone, left, right))
119 if (contained->isStruct()) {
120 assert(dest->type()->contained() == contained);
121 assert(source->length() == dest->length());
123 Local<Struct> left(zone);
124 Local<Struct> right(zone);
125 for (unsigned i = 0; i < source->length(); i++) {
126 left = Struct::cast(dest->readObject(i));
127 right = Struct::cast(source->readObject(i));
128 if (!CopyStruct(zone, left, right))
134 if (contained->isTraceable()) {
135 for (unsigned i = 0; i < source->length(); i++)
136 dest->writeObject(zone, i, source->readObject(i));
140 assert(contained->pod() == dest->type()->contained()->pod());
141 assert(contained->pod() == PrimitiveType_Char
142 ? source->length() <= dest->length()
143 : source->length() == dest->length());
145 assert(dest->length());
146 memcpy(dest->bytes(), source->bytes(), source->length() * Array::SizeForElement(dest->type()));
151 GetElement(Array *array, int index, LocalSlot *vp)
153 if (array->type()->contained()->isTraceable()) {
154 vp->obj = array->readObject(index);
158 switch (array->type()->contained()->pod()) {
159 case PrimitiveType_Float:
160 vp->f = array->readFloat(index);
163 case PrimitiveType_Char:
164 vp->i32 = array->readChar(index);
168 assert(array->type()->contained()->pod() == PrimitiveType_Int32);
169 vp->i32 = array->readInt32(index);
175 SetElement(Zone *zone, Array *array, int index, const LocalSlot &v)
177 if (array->type()->contained()->isTraceable()) {
178 array->writeObject(zone, index, v.obj);
182 switch (array->type()->contained()->pod()) {
183 case PrimitiveType_Float:
184 array->writeFloat(index, v.f);
187 case PrimitiveType_Char:
188 array->writeChar(index, v.i32);
192 assert(array->type()->contained()->pod() == PrimitiveType_Int32);
193 array->writeInt32(index, v.i32);
199 FetchField(Struct *obj, Descriptor *desc, LocalSlot *vp)
201 // :TODO: assert no GC.
202 if (desc->type()->isTraceable()) {
203 vp->obj = obj->get<Object *>(desc);
207 switch (desc->type()->pod()) {
208 case PrimitiveType_Float:
209 vp->f = obj->get<float>(desc);
213 assert(desc->type()->pod() == PrimitiveType_Int32);
214 vp->i32 = obj->get<int>(desc);
220 GetField(Struct *obj, unsigned index, LocalSlot *vp)
222 StructType *type = StructType::cast(obj->map()->type());
223 Descriptor *desc = Descriptor::cast(type->fields()->at(index));
224 FetchField(obj, desc, vp);
228 GetGlobal(Zone *zone, Handle<Struct> obj, Handle<String> name, LocalSlot *vp)
230 Local<StructType> type(zone, StructType::cast(obj->map()->type()));
231 Local<Descriptor> desc(zone, type->lookupField(name, NULL));
232 FetchField(obj, desc, vp);
236 StoreField(Zone *zone, Struct *obj, Descriptor *desc, const LocalSlot &v)
238 // :TODO: assert no GC.
239 if (desc->type()->isTraceable()) {
240 obj->set(zone, desc, v.obj);
244 switch (desc->type()->pod()) {
245 case PrimitiveType_Float:
246 obj->set<float>(desc, v.f);
250 assert(desc->type()->pod() == PrimitiveType_Int32);
251 obj->set<int>(desc, v.i32);
257 SetField(Zone *zone, Struct *obj, unsigned index, const LocalSlot &v)
259 StructType *type = StructType::cast(obj->map()->type());
260 Descriptor *desc = Descriptor::cast(type->fields()->at(index));
261 StoreField(zone, obj, desc, v);
265 SetGlobal(Zone *zone, Handle<Struct> obj, Handle<String> name, LocalSlot &v)
267 Local<StructType> type(zone, StructType::cast(obj->map()->type()));
268 Local<Descriptor> desc(zone, type->lookupField(name, NULL));
269 StoreField(zone, obj, desc, v);
272 // Even though this function can fail, we immediately bail out, so it doesn't
273 // matter if we've leaving things unrooted.
275 CopyStruct(Zone *zone, Handle<Struct> left, Handle<Struct> right)
277 assert(left->map()->type() == right->map()->type());
279 Local<StructType> type(zone, StructType::cast(left->map()->type()));
280 Local<Struct> leftStruct(zone);
281 Local<Struct> rightStruct(zone);
282 Local<Array> leftArray(zone);
283 Local<Array> rightArray(zone);
284 for (unsigned i = 0; i < type->fields()->length(); i++) {
285 Descriptor *desc = Descriptor::cast(type->fields()->at(i));
288 FetchField(right, desc, &rval);
290 // If the field is a struct, we need to deep-copy.
291 if (desc->type()->isStruct()) {
294 FetchField(left, desc, &lval);
295 leftStruct = Struct::cast(lval.obj);
296 rightStruct = Struct::cast(rval.obj);
297 if (!CopyStruct(zone, leftStruct, rightStruct))
299 } else if (desc->type()->isArray()) {
302 FetchField(left, desc, &lval);
303 leftArray = Array::cast(lval.obj);
304 rightArray = Array::cast(rval.obj);
305 if (!CopyArray(zone, leftArray, rightArray))
308 StoreField(zone, left, desc, rval);
316 InvokeNative(Zone *zone, Handle<Native> native, void *identity, LocalSlot *args, unsigned argc,
321 void *address = zone->nativeTable()->address(native->index());
323 Local<String> name(zone, zone->nativeTable()->name(native->index()));
324 zone->reportError(Message_NativeNotBound, name->chars());
328 if (!zone->stack().pushNativeFrame(native, guard, argc + 1))
331 intptr_t *cells = guard.args();
333 // params[0] == argc, in ISPE
336 // Pass the formal arguments.
337 Local<FunctionType> type(zone, native->type());
338 Local<Type> param(zone);
339 for (unsigned i = 0; i < type->parameters()->length(); i++) {
340 param = type->parameterAt(i);
342 // For floats, we rely on the aliasing of |i32| and |f|.
343 LocalSlot &arg = args[-int(i) - 1];
344 if (param->isInt32() || param->isFloat()) {
345 cells[i + 1] = arg.i32;
346 } else if (param->isReference()) {
347 cells[i + 1] = reinterpret_cast<intptr_t>(arg.ref->location());
349 assert(param->isArray());
350 cells[i + 1] = reinterpret_cast<intptr_t>(arg.array->bytes());
354 // For variadic arguments, the compiler left either arrays or references.
355 for (unsigned i = type->parameters()->length(); i < argc; i++) {
356 LocalSlot &arg = args[-int(i) - 1];
358 if (arg.obj->is(MapKind_Reference)) {
359 cells[i + 1] = reinterpret_cast<intptr_t>(arg.ref->location());
361 assert(arg.obj->is(MapKind_Array));
362 cells[i + 1] = reinterpret_cast<intptr_t>(arg.array->bytes());
366 typedef intptr_t (*INVOKE)(void *, intptr_t *);
367 INVOKE invoke = (INVOKE)address;
368 *result = invoke(identity, cells);
370 return !zone->hasPendingException();
374 ke::Interpret(Zone *zone, Frame *entryFrame, LocalSlot *result)
376 Top &top = *zone->stack().top();
380 LocalSlot *locals = entryFrame->locals();
383 Opcode op = Opcode(*top.pc);
385 if (OpcodeInfo[op].nuses > 0)
386 assert(top.sp - OpcodeInfo[op].nuses >= top.fp->stackBase());
394 PUSH_I32(top, READ_INT32(top.pc));
398 PUSH_F(top, READ_FLOAT(top.pc));
408 if (top.fp == entryFrame)
412 zone->stack().popInlineFrame();
413 locals = top.fp->locals();
418 top.pc += OpcodeInfo[*top.pc].length;
430 int value = POP_I32(top);
432 top.pc += READ_INT32(top.pc);
440 int value = POP_I32(top);
442 top.pc += READ_INT32(top.pc);
449 top.pc += READ_INT32(top.pc);
454 unsigned slot = READ_UINT32(top.pc);
455 assert(slot < top.fp->code()->nlocals());
456 *top.sp++ = locals[slot];
462 unsigned slot = READ_UINT32(top.pc);
463 assert(slot < top.fp->code()->nlocals());
464 locals[slot] = top.sp[-1];
470 top.sp[-2].i32 = top.sp[-2].i32 + top.sp[-1].i32;
477 top.sp[-2].i32 = top.sp[-2].i32 - top.sp[-1].i32;
484 top.sp[-2].i32 = top.sp[-2].i32 * top.sp[-1].i32;
491 if (top.sp[-1].i32 == 0) {
492 zone->reportError(Message_IntegerDivideByZero);
495 top.sp[-2].i32 = top.sp[-2].i32 / top.sp[-1].i32;
502 if (top.sp[-1].i32 == 0) {
503 zone->reportError(Message_IntegerDivideByZero);
506 top.sp[-2].i32 = top.sp[-2].i32 % top.sp[-1].i32;
513 top.sp[-2].i32 = top.sp[-2].i32 < top.sp[-1].i32;
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 top.sp[-2].i32 = (top.sp[-2].i32 == top.sp[-1].i32);
548 top.sp[-2].i32 = (top.sp[-2].i32 != top.sp[-1].i32);
555 top.sp[-2].f = top.sp[-2].f + top.sp[-1].f;
562 top.sp[-2].f = top.sp[-2].f - top.sp[-1].f;
569 top.sp[-2].f = top.sp[-2].f * top.sp[-1].f;
576 top.sp[-2].f = top.sp[-2].f / top.sp[-1].f;
583 top.sp[-2].f = top.sp[-2].f < top.sp[-1].f;
590 top.sp[-2].f = top.sp[-2].f <= top.sp[-1].f;
597 top.sp[-2].f = top.sp[-2].f > top.sp[-1].f;
604 top.sp[-2].f = top.sp[-2].f >= top.sp[-1].f;
611 top.sp[-2].i32 = (top.sp[-2].f == top.sp[-1].f);
618 top.sp[-2].i32 = (top.sp[-2].f != top.sp[-1].f);
625 top.sp[-2].i32 = top.sp[-2].i32 << top.sp[-1].i32;
632 top.sp[-2].i32 = top.sp[-2].i32 >> top.sp[-1].i32;
639 top.sp[-2].i32 = unsigned(top.sp[-2].i32) >> unsigned(top.sp[-1].i32);
646 top.sp[-2].i32 = top.sp[-2].i32 & top.sp[-1].i32;
653 top.sp[-2].i32 = top.sp[-2].i32 | top.sp[-1].i32;
660 top.sp[-2].i32 = top.sp[-2].i32 ^ top.sp[-1].i32;
667 top.sp[-1].i32 = ~top.sp[-1].i32;
673 top.sp[-1].i32 = top.sp[-1].i32 ? 0 : 1;
679 top.sp[-1].i32 = top.sp[-1].f ? 0 : 1;
685 top.sp[-1].i32 = -top.sp[-1].i32;
691 top.sp[-1].f = -top.sp[-1].f;
697 Local<Function> newFun(zone, Function::cast(top.sp[-1].obj));
700 if (!zone->stack().pushInlineFrame(newFun))
703 locals = top.fp->locals();
709 unsigned slot = READ_UINT32(top.pc);
710 *top.sp++ = top.fp->arg(slot);
716 unsigned slot = READ_UINT32(top.pc);
717 top.fp->arg(slot) = top.sp[-1];
723 Array *array = Array::cast(top.sp[-2].obj);
724 int index = top.sp[-1].i32;
726 if (index < 0 || unsigned(index) >= array->length()) {
727 zone->reportError(Message_ArrayIndexOutOfBounds, index, array->length());
731 GetElement(array, index, &top.sp[-2]);
738 Array *array = Array::cast(top.sp[-3].obj);
739 int index = top.sp[-2].i32;
741 if (index < 0 || unsigned(index) >= array->length()) {
742 zone->reportError(Message_ArrayIndexOutOfBounds, index, array->length());
746 SetElement(zone, array, index, top.sp[-1]);
747 top.sp[-3] = top.sp[-1];
754 int ival = POP_I32(top);
755 float fval = (float)ival;
762 top.sp[-1].i32 = !!top.sp[-1].i32;
768 Heap::Tenure tenure = (Heap::Tenure)top.pc[1];
769 Local<ArrayMap> map(zone, ArrayMap::cast(top.fp->code()->map(READ_UINT32(top.pc + 1))));
770 Array *array = Array::NewFixed(zone, map, tenure);
780 Heap::Tenure tenure = (Heap::Tenure)top.pc[1];
781 Local<ArrayMap> map(zone, ArrayMap::cast(top.fp->code()->map(READ_UINT32(top.pc + 1))));
782 Array *array = Array::NewEmpty(zone, map, tenure);
792 Heap::Tenure tenure = (Heap::Tenure)top.pc[1];
793 Local<ArrayMap> map(zone, ArrayMap::cast(top.fp->code()->map(READ_UINT32(top.pc + 1))));
794 assert(map->type()->levels() < MAX_ARRAY_DEPTH);
796 int dims[MAX_ARRAY_DEPTH];
797 for (unsigned i = map->type()->levels() - 1; i < map->type()->levels(); i--)
798 dims[i] = POP_I32(top);
800 Array *array = Array::NewSized(zone, map, dims, tenure);
810 Heap::Tenure tenure = (Heap::Tenure)top.pc[1];
811 Local<StructMap> map(zone, StructMap::cast(top.fp->code()->map(READ_UINT32(top.pc + 1))));
812 top.sp->obj = Struct::New(zone, map, tenure);
821 unsigned slot = READ_UINT32(top.pc);
822 locals[slot].address = zone->heap().lifoPosition();
828 unsigned slot = READ_UINT32(top.pc);
829 zone->heap().unwindLifo(locals[slot].address);
835 unsigned slot = READ_UINT32(top.pc);
836 if (!PushNewRef(zone, top, &locals[slot], Heap::Tenure_Stack))
843 unsigned slot = READ_UINT32(top.pc);
844 if (!PushNewRef(zone, top, &top.fp->arg(slot), Heap::Tenure_Stack))
849 case OP_SLOTREF_LIFO:
851 Reference *ref = Reference::NewStack(zone, top.sp[-1], Heap::Tenure_Stack);
854 top.sp[-1].ref = ref;
860 top.sp[-1].i32 = top.sp[-1].ref->getInt();
866 top.sp[-2].ref->setInt(top.sp[-1].i32);
867 top.sp[-2].i32 = top.sp[-1].i32;
874 top.sp[-1].f = top.sp[-1].ref->getFloat();
880 top.sp[-2].ref->setFloat(top.sp[-1].f);
881 top.sp[-2].f = top.sp[-1].f;
887 top.sp->obj = top.fp->code()->object(READ_UINT32(top.pc));
894 // Note for this assignment opcode, we return the left-hand side, not the
895 // right, since in this case the result of the expression could be different.
896 Local<Array> left(zone, Array::cast(top.sp[-2].array));
897 Local<Array> right(zone, Array::cast(top.sp[-1].array));
898 if (!CopyArray(zone, left, right))
907 top.sp[-1] = top.sp[-2];
914 top.sp[-2] = top.sp[-4];
915 top.sp[-1] = top.sp[-3];
921 LocalSlot slot = top.sp[-2];
922 top.sp[-2] = top.sp[-1];
929 LocalSlot slot = top.sp[-3];
930 top.sp[-3] = top.sp[-2];
931 top.sp[-2] = top.sp[-1];
939 top.sp[-1] = top.sp[-3];
946 top.sp[-1] = top.sp[-4];
952 unsigned argc = READ_UINT32(top.pc);
954 Local<Native> native(zone, Native::cast(top.sp[-1].obj));
958 void *nativeIdentity = top.fp->code()->module()->nativeIdentity();
959 if (!InvokeNative(zone, native, nativeIdentity, top.sp, argc, &result))
963 PUSH_I32(top, result);
970 // Because the stack is a union, these are actually no-ops in the
991 Struct *obj = Struct::cast(top.sp[-1].obj);
992 GetField(obj, READ_UINT32(top.pc), &top.sp[-1]);
998 Struct *obj = Struct::cast(top.sp[-2].obj);
999 SetField(zone, obj, READ_UINT32(top.pc), top.sp[-1]);
1000 top.sp[-2] = top.sp[-1];
1007 Local<Struct> left(zone, Struct::cast(top.sp[-2].obj));
1008 Local<Struct> right(zone, Struct::cast(top.sp[-1].obj));
1009 if (!CopyStruct(zone, left, right))
1011 top.sp[-2].obj = top.sp[-1].obj;
1016 case OP_TABLESWITCH:
1018 int value = top.sp[-1].i32;
1019 int low = READ_TABLESWITCH_LOW(top.pc);
1020 int high = READ_TABLESWITCH_HIGH(top.pc);
1024 unsigned index = unsigned(value - low);
1025 unsigned tablesize = TableSwitchEntries(low, high);
1026 if (index < tablesize) {
1027 top.pc += OP_TABLESWITCH_LENGTH + OP_JUMP_LENGTH;
1028 top.pc += index * OP_JUMP_LENGTH;
1036 Array *array = Array::cast(top.sp[-1].obj);
1037 top.sp[-1].i32 = array->length();
1043 Local<Function> fun(zone, Function::cast(top.sp[-1].obj));
1044 zone->registerPublic(fun);
1051 Local<Code> code(zone, top.fp->code());
1052 Local<Struct> obj(zone, code->module()->globals());
1053 Local<String> name(zone, String::cast(code->strings()->at(READ_UINT32(top.pc))));
1054 SetGlobal(zone, obj, name, top.sp[-1]);
1060 Local<Code> code(zone, top.fp->code());
1061 Local<Struct> obj(zone, code->module()->globals());
1062 Local<String> name(zone, String::cast(code->strings()->at(READ_UINT32(top.pc))));
1063 GetGlobal(zone, obj, name, top.sp);
1074 top.pc += OpcodeInfo[op].length;
1075 assert(top.sp >= top.fp->stackBase());
1082 // Unwind frames until we can continue.
1083 while (top.fp != entryFrame)
1084 zone->stack().popInlineFrame();