Re-add LIFO stack optimizations.
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);
450 top.pc += READ_INT32(top.pc);
457 top.fp->leaveLoop(zone);
458 top.pc += READ_INT32(top.pc);
464 unsigned slot = READ_UINT32(top.pc);
465 assert(slot < top.fp->code()->nlocals());
466 *top.sp++ = locals[slot];
472 unsigned slot = READ_UINT32(top.pc);
473 assert(slot < top.fp->code()->nlocals());
474 locals[slot] = top.sp[-1];
480 top.sp[-2].i32 = top.sp[-2].i32 + top.sp[-1].i32;
487 top.sp[-2].i32 = top.sp[-2].i32 - top.sp[-1].i32;
494 top.sp[-2].i32 = top.sp[-2].i32 * top.sp[-1].i32;
501 if (top.sp[-1].i32 == 0) {
502 zone->reportError(Message_IntegerDivideByZero);
505 top.sp[-2].i32 = top.sp[-2].i32 / top.sp[-1].i32;
512 if (top.sp[-1].i32 == 0) {
513 zone->reportError(Message_IntegerDivideByZero);
516 top.sp[-2].i32 = top.sp[-2].i32 % top.sp[-1].i32;
523 top.sp[-2].i32 = top.sp[-2].i32 < top.sp[-1].i32;
530 top.sp[-2].i32 = top.sp[-2].i32 <= top.sp[-1].i32;
537 top.sp[-2].i32 = top.sp[-2].i32 > top.sp[-1].i32;
544 top.sp[-2].i32 = top.sp[-2].i32 >= top.sp[-1].i32;
551 top.sp[-2].i32 = (top.sp[-2].i32 == top.sp[-1].i32);
558 top.sp[-2].i32 = (top.sp[-2].i32 != top.sp[-1].i32);
565 top.sp[-2].f = top.sp[-2].f + top.sp[-1].f;
572 top.sp[-2].f = top.sp[-2].f - top.sp[-1].f;
579 top.sp[-2].f = top.sp[-2].f * top.sp[-1].f;
586 top.sp[-2].f = top.sp[-2].f / top.sp[-1].f;
593 top.sp[-2].f = top.sp[-2].f < top.sp[-1].f;
600 top.sp[-2].f = top.sp[-2].f <= top.sp[-1].f;
607 top.sp[-2].f = top.sp[-2].f > top.sp[-1].f;
614 top.sp[-2].f = top.sp[-2].f >= top.sp[-1].f;
621 top.sp[-2].i32 = (top.sp[-2].f == top.sp[-1].f);
628 top.sp[-2].i32 = (top.sp[-2].f != top.sp[-1].f);
635 top.sp[-2].i32 = top.sp[-2].i32 << top.sp[-1].i32;
642 top.sp[-2].i32 = top.sp[-2].i32 >> top.sp[-1].i32;
649 top.sp[-2].i32 = unsigned(top.sp[-2].i32) >> unsigned(top.sp[-1].i32);
656 top.sp[-2].i32 = top.sp[-2].i32 & top.sp[-1].i32;
663 top.sp[-2].i32 = top.sp[-2].i32 | top.sp[-1].i32;
670 top.sp[-2].i32 = top.sp[-2].i32 ^ top.sp[-1].i32;
677 top.sp[-1].i32 = ~top.sp[-1].i32;
683 top.sp[-1].i32 = top.sp[-1].i32 ? 0 : 1;
689 top.sp[-1].i32 = top.sp[-1].f ? 0 : 1;
695 top.sp[-1].i32 = -top.sp[-1].i32;
701 top.sp[-1].f = -top.sp[-1].f;
707 Local<Function> newFun(zone, Function::cast(top.sp[-1].obj));
710 if (!zone->stack().pushInlineFrame(newFun))
713 locals = top.fp->locals();
719 unsigned slot = READ_UINT32(top.pc);
720 *top.sp++ = top.fp->arg(slot);
726 unsigned slot = READ_UINT32(top.pc);
727 top.fp->arg(slot) = top.sp[-1];
733 Array *array = Array::cast(top.sp[-2].obj);
734 int index = top.sp[-1].i32;
736 if (index < 0 || unsigned(index) >= array->length()) {
737 zone->reportError(Message_ArrayIndexOutOfBounds, index, array->length());
741 GetElement(array, index, &top.sp[-2]);
748 Array *array = Array::cast(top.sp[-3].obj);
749 int index = top.sp[-2].i32;
751 if (index < 0 || unsigned(index) >= array->length()) {
752 zone->reportError(Message_ArrayIndexOutOfBounds, index, array->length());
756 SetElement(zone, array, index, top.sp[-1]);
757 top.sp[-3] = top.sp[-1];
764 int ival = POP_I32(top);
765 float fval = (float)ival;
772 top.sp[-1].i32 = !!top.sp[-1].i32;
778 Heap::Tenure tenure = (Heap::Tenure)top.pc[1];
779 Local<ArrayMap> map(zone, ArrayMap::cast(top.fp->code()->map(READ_UINT32(top.pc + 1))));
780 Array *array = Array::NewFixed(zone, map, tenure);
790 Heap::Tenure tenure = (Heap::Tenure)top.pc[1];
791 Local<ArrayMap> map(zone, ArrayMap::cast(top.fp->code()->map(READ_UINT32(top.pc + 1))));
792 Array *array = Array::NewEmpty(zone, map, tenure);
802 Heap::Tenure tenure = (Heap::Tenure)top.pc[1];
803 Local<ArrayMap> map(zone, ArrayMap::cast(top.fp->code()->map(READ_UINT32(top.pc + 1))));
804 assert(map->type()->levels() < MAX_ARRAY_DEPTH);
806 int dims[MAX_ARRAY_DEPTH];
807 for (unsigned i = map->type()->levels() - 1; i < map->type()->levels(); i--)
808 dims[i] = POP_I32(top);
810 Array *array = Array::NewSized(zone, map, dims, tenure);
820 Heap::Tenure tenure = (Heap::Tenure)top.pc[1];
821 Local<StructMap> map(zone, StructMap::cast(top.fp->code()->map(READ_UINT32(top.pc + 1))));
822 top.sp->obj = Struct::New(zone, map, tenure);
831 unsigned slot = READ_UINT32(top.pc);
832 locals[slot].address = zone->heap().lifoPosition();
838 unsigned slot = READ_UINT32(top.pc);
839 zone->heap().unwindLifo(locals[slot].address);
845 unsigned slot = READ_UINT32(top.pc);
846 if (!PushNewRef(zone, top, &locals[slot], Heap::Tenure_Stack))
853 unsigned slot = READ_UINT32(top.pc);
854 if (!PushNewRef(zone, top, &top.fp->arg(slot), Heap::Tenure_Stack))
859 case OP_SLOTREF_LIFO:
861 Reference *ref = Reference::NewStack(zone, top.sp[-1], Heap::Tenure_Stack);
864 top.sp[-1].ref = ref;
870 top.sp[-1].i32 = top.sp[-1].ref->getInt();
876 top.sp[-2].ref->setInt(top.sp[-1].i32);
877 top.sp[-2].i32 = top.sp[-1].i32;
884 top.sp[-1].f = top.sp[-1].ref->getFloat();
890 top.sp[-2].ref->setFloat(top.sp[-1].f);
891 top.sp[-2].f = top.sp[-1].f;
897 top.sp->obj = top.fp->code()->object(READ_UINT32(top.pc));
904 // Note for this assignment opcode, we return the left-hand side, not the
905 // right, since in this case the result of the expression could be different.
906 Local<Array> left(zone, Array::cast(top.sp[-2].array));
907 Local<Array> right(zone, Array::cast(top.sp[-1].array));
908 if (!CopyArray(zone, left, right))
917 top.sp[-1] = top.sp[-2];
924 top.sp[-2] = top.sp[-4];
925 top.sp[-1] = top.sp[-3];
931 LocalSlot slot = top.sp[-2];
932 top.sp[-2] = top.sp[-1];
939 LocalSlot slot = top.sp[-3];
940 top.sp[-3] = top.sp[-2];
941 top.sp[-2] = top.sp[-1];
949 top.sp[-1] = top.sp[-3];
956 top.sp[-1] = top.sp[-4];
962 unsigned argc = READ_UINT32(top.pc);
964 Local<Native> native(zone, Native::cast(top.sp[-1].obj));
968 void *nativeIdentity = top.fp->code()->module()->nativeIdentity();
969 if (!InvokeNative(zone, native, nativeIdentity, top.sp, argc, &result))
973 PUSH_I32(top, result);
980 // Because the stack is a union, these are actually no-ops in the
1001 Struct *obj = Struct::cast(top.sp[-1].obj);
1002 GetField(obj, READ_UINT32(top.pc), &top.sp[-1]);
1008 Struct *obj = Struct::cast(top.sp[-2].obj);
1009 SetField(zone, obj, READ_UINT32(top.pc), top.sp[-1]);
1010 top.sp[-2] = top.sp[-1];
1017 Local<Struct> left(zone, Struct::cast(top.sp[-2].obj));
1018 Local<Struct> right(zone, Struct::cast(top.sp[-1].obj));
1019 if (!CopyStruct(zone, left, right))
1021 top.sp[-2].obj = top.sp[-1].obj;
1026 case OP_TABLESWITCH:
1028 int value = top.sp[-1].i32;
1029 int low = READ_TABLESWITCH_LOW(top.pc);
1030 int high = READ_TABLESWITCH_HIGH(top.pc);
1034 unsigned index = unsigned(value - low);
1035 unsigned tablesize = TableSwitchEntries(low, high);
1036 if (index < tablesize) {
1037 top.pc += OP_TABLESWITCH_LENGTH + OP_JUMP_LENGTH;
1038 top.pc += index * OP_JUMP_LENGTH;
1046 Array *array = Array::cast(top.sp[-1].obj);
1047 top.sp[-1].i32 = array->length();
1053 Local<Function> fun(zone, Function::cast(top.sp[-1].obj));
1054 zone->registerPublic(fun);
1061 Local<Code> code(zone, top.fp->code());
1062 Local<Struct> obj(zone, code->module()->globals());
1063 Local<String> name(zone, String::cast(code->strings()->at(READ_UINT32(top.pc))));
1064 SetGlobal(zone, obj, name, top.sp[-1]);
1070 Local<Code> code(zone, top.fp->code());
1071 Local<Struct> obj(zone, code->module()->globals());
1072 Local<String> name(zone, String::cast(code->strings()->at(READ_UINT32(top.pc))));
1073 GetGlobal(zone, obj, name, top.sp);
1080 top.fp->enterLoop(zone);
1086 top.fp->leaveLoop(zone);
1096 top.pc += OpcodeInfo[op].length;
1097 assert(top.sp >= top.fp->stackBase());
1104 // Unwind frames until we can continue.
1105 while (top.fp != entryFrame)
1106 zone->stack().popInlineFrame();