Renamed jitcraft/ to src/, fixed some /Wp64 warnings.
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/.
19 #include "Interpreter.h"
24 #include "Reference.h"
26 #include "Environments.h"
31 #define PUSH_I32(top, value) \
33 top.sp->i32 = value; \
37 #define PUSH_F(top, value) \
46 int value = top.sp[-1].i32;
54 float value = top.sp[-1].f;
59 // Pawn's original semantics specify some kind of floor operation, so
60 // (8 / -3 == -3) and (-8 / 3 == -1). The SourcePawn 1 compiler (spcomp) uses
61 // the upstream algorithm when constant folding. However the SourcePawn 1 JIT
62 // does not. My guess is that Borja freaked out seeing the 13-instruction x86
63 // sequence necessary to get this functionality (I vaguely recall a thread
64 // where we discussed this with Mark Peter? But the ITB forums are gone...),
65 // and we just decided to break the damn thing. So SP1 was inconsistent, but
66 // we are not. And for completeness, we assert that the C compiler does the
67 // right thing, since C89 leaves it unspecified.
68 STATIC_ASSERT(8 / -3 == -2);
69 STATIC_ASSERT(-8 / 3 == -2);
72 PushNewRef(Zone *zone, Top &top, void *location, Heap::Tenure tenure)
74 Reference *ref = Reference::NewStack(zone, location, tenure);
82 // Generate an array with completely static sizes.
84 GenerateArray(Zone *zone, Handle<ArrayMap> map, Heap::Tenure tenure)
86 assert(map->type()->elements() != -1);
88 Local<Array> array(zone, Array::New(zone, map, map->type()->elements(), tenure));
92 if (map->type()->levels() == 1)
95 Local<ArrayMap> contained(zone, map->contained());
96 Local<Array> sub(zone);
97 for (unsigned i = 0; i < unsigned(map->type()->elements()); i++) {
98 sub = GenerateArray(zone, contained, tenure);
101 array->writeObject(zone, i, sub);
108 CopyArray(Zone *zone, Array *source, Array *dest)
110 assert(source->type()->levels() == 1);
111 assert(dest->type()->levels() == 1);
112 assert(source->type()->contained()->primitive() ==
113 dest->type()->contained()->primitive());
114 assert(source->nelements() >= dest->nelements());
115 assert(dest->elementSize() == source->elementSize());
117 memcpy(source->vec(), dest->vec(), dest->nelements() * dest->elementSize());
122 InvokeNative(Zone *zone, Handle<Native> native, void *identity, LocalSlot *args, unsigned argc,
127 if (!zone->stack().pushNativeFrame(native, guard, argc + 1))
130 intptr_t *cells = guard.args();
132 // params[0] == argc, in ISPE
135 // Pass the formal arguments.
136 Local<Type> type(zone, native->type());
137 Local<Type> param(zone);
138 for (unsigned i = 0; i < type->parameters()->length(); i++) {
139 param = type->parameterAt(i);
141 // For floats, we rely on the aliasing of |i32| and |f|.
142 LocalSlot &arg = args[-int(i) - 1];
143 if (param->isInt32() || param->isFloat()) {
144 cells[i + 1] = arg.i32;
145 } else if (param->isReference()) {
146 cells[i + 1] = reinterpret_cast<intptr_t>(arg.ref->location());
148 assert(param->isArray());
149 cells[i + 1] = reinterpret_cast<intptr_t>(arg.array->vec());
153 // For variadic arguments, the compiler left either arrays or references.
154 for (unsigned i = type->parameters()->length(); i < argc; i++) {
155 LocalSlot &arg = args[-int(i) - 1];
157 if (arg.obj->is(MapKind_Reference)) {
158 cells[i + 1] = reinterpret_cast<intptr_t>(arg.ref->location());
160 assert(arg.obj->is(MapKind_Array));
161 cells[i + 1] = reinterpret_cast<intptr_t>(arg.array->vec());
165 typedef intptr_t (*INVOKE)(void *, intptr_t *);
166 INVOKE invoke = (INVOKE)native->function();
167 *result = invoke(identity, cells);
169 return !zone->hasPendingException();
173 ke::Interpret(Zone *zone, Frame *entryFrame, LocalSlot *result)
175 Top &top = *zone->stack().top();
179 LocalSlot *locals = entryFrame->locals();
182 Opcode op = Opcode(*top.pc);
189 PUSH_I32(top, READ_INT32(top.pc));
193 PUSH_F(top, READ_FLOAT(top.pc));
203 if (top.fp == entryFrame)
207 zone->stack().popInlineFrame();
208 locals = top.fp->locals();
213 top.pc += OpcodeInfo[*top.pc].length;
223 #define RELATIONAL_JUMP(op) \
225 int right = POP_I32(top); \
226 int left = POP_I32(top); \
227 if (left op right) { \
228 top.pc += READ_INT32(top.pc); \
257 #undef RELATIONAL_JUMP
261 int value = POP_I32(top);
263 top.pc += READ_INT32(top.pc);
271 int value = POP_I32(top);
273 top.pc += READ_INT32(top.pc);
279 #define RELATIONAL_JUMP_F(op) \
281 float right = POP_F(top); \
282 float left = POP_F(top); \
283 if (left op right) { \
284 top.pc += READ_INT32(top.pc); \
290 RELATIONAL_JUMP_F(<);
294 RELATIONAL_JUMP_F(<=);
298 RELATIONAL_JUMP_F(>);
302 RELATIONAL_JUMP_F(>=);
306 RELATIONAL_JUMP_F(==);
310 RELATIONAL_JUMP_F(!=);
315 float value = POP_F(top);
317 top.pc += READ_INT32(top.pc);
325 float value = POP_F(top);
327 top.pc += READ_INT32(top.pc);
334 top.pc += READ_INT32(top.pc);
339 int slot = READ_INT32(top.pc);
340 *top.sp++ = locals[slot];
346 int slot = READ_INT32(top.pc);
347 locals[slot] = top.sp[-1];
353 top.sp[-2].i32 = top.sp[-2].i32 + top.sp[-1].i32;
360 top.sp[-2].i32 = top.sp[-2].i32 - top.sp[-1].i32;
367 top.sp[-2].i32 = top.sp[-2].i32 * top.sp[-1].i32;
374 if (top.sp[-1].i32 == 0) {
375 zone->reportError(Message_IntegerDivideByZero);
378 top.sp[-2].i32 = top.sp[-2].i32 / top.sp[-1].i32;
385 if (top.sp[-1].i32 == 0) {
386 zone->reportError(Message_IntegerDivideByZero);
389 top.sp[-2].i32 = top.sp[-2].i32 % top.sp[-1].i32;
396 top.sp[-2].i32 = top.sp[-2].i32 < top.sp[-1].i32;
403 top.sp[-2].i32 = top.sp[-2].i32 <= top.sp[-1].i32;
410 top.sp[-2].i32 = top.sp[-2].i32 > top.sp[-1].i32;
417 top.sp[-2].i32 = top.sp[-2].i32 >= top.sp[-1].i32;
424 top.sp[-2].i32 = (top.sp[-2].i32 == top.sp[-1].i32);
431 top.sp[-2].i32 = (top.sp[-2].i32 != top.sp[-1].i32);
438 top.sp[-2].f = top.sp[-2].f + top.sp[-1].f;
445 top.sp[-2].f = top.sp[-2].f - top.sp[-1].f;
452 top.sp[-2].f = top.sp[-2].f * top.sp[-1].f;
459 top.sp[-2].f = top.sp[-2].f / top.sp[-1].f;
466 top.sp[-2].f = top.sp[-2].f < top.sp[-1].f;
473 top.sp[-2].f = top.sp[-2].f <= top.sp[-1].f;
480 top.sp[-2].f = top.sp[-2].f > top.sp[-1].f;
487 top.sp[-2].f = top.sp[-2].f >= top.sp[-1].f;
494 top.sp[-2].i32 = (top.sp[-2].f == top.sp[-1].f);
501 top.sp[-2].i32 = (top.sp[-2].f != top.sp[-1].f);
508 top.sp[-2].i32 = top.sp[-2].i32 << top.sp[-1].i32;
515 top.sp[-2].i32 = top.sp[-2].i32 >> top.sp[-1].i32;
522 top.sp[-2].i32 = unsigned(top.sp[-2].i32) << unsigned(top.sp[-1].i32);
529 top.sp[-2].i32 = top.sp[-2].i32 & top.sp[-1].i32;
536 top.sp[-2].i32 = top.sp[-2].i32 | top.sp[-1].i32;
543 top.sp[-2].i32 = top.sp[-2].i32 ^ top.sp[-1].i32;
550 top.sp[-1].i32 = ~top.sp[-1].i32;
556 top.sp[-1].i32 = top.sp[-1].i32 ? 0 : 1;
562 top.sp[-1].i32 = top.sp[-1].f ? 0 : 1;
568 top.sp[-1].i32 = -top.sp[-1].i32;
574 top.sp[-1].f = -top.sp[-1].f;
580 Local<Function> newFun(zone, top.sp[-1].fun);
583 if (!zone->stack().pushInlineFrame(newFun))
586 locals = top.fp->locals();
592 unsigned slot = READ_UINT32(top.pc);
593 *top.sp++ = top.fp->arg(slot);
599 unsigned slot = READ_UINT32(top.pc);
600 top.fp->arg(slot) = *--top.sp;
606 Array *array = top.sp[-2].array;
607 int index = top.sp[-1].i32;
608 if (index < 0 || unsigned(index) >= array->nelements()) {
609 zone->reportError(Message_ArrayIndexOutOfBounds, index, array->nelements());
617 Array *array = top.sp[-2].array;
618 int index = top.sp[-1].i32;
619 top.sp[-2].i32 = array->readInt32(index);
626 Array *array = top.sp[-2].array;
627 int index = top.sp[-1].i32;
628 top.sp[-2].f = array->readFloat(index);
635 Array *array = top.sp[-2].array;
636 int index = top.sp[-1].i32;
637 top.sp[-2].i32 = array->readChar(index);
644 Array *array = top.sp[-3].array;
645 int index = top.sp[-2].i32;
646 array->writeInt32(index, top.sp[-1].i32);
647 top.sp[-3] = top.sp[-1];
654 Array *array = top.sp[-3].array;
655 int index = top.sp[-2].i32;
656 array->writeFloat(index, top.sp[-1].f);
657 top.sp[-3] = top.sp[-1];
664 Array *array = top.sp[-3].array;
665 int index = top.sp[-2].i32;
666 array->writeChar(index, top.sp[-1].i32);
667 top.sp[-3] = top.sp[-1];
674 Array *array = top.sp[-2].array;
675 int index = top.sp[-1].i32;
676 top.sp[-2].array = Array::cast(array->readObject(index));
683 int ival = POP_I32(top);
684 float fval = (float)ival;
691 Heap::Tenure tenure = (Heap::Tenure)top.pc[1];
692 Local<ArrayMap> map(zone, ArrayMap::cast(top.fp->code()->map(READ_UINT32(top.pc + 1))));
693 Array *array = GenerateArray(zone, map, tenure);
703 unsigned slot = READ_UINT32(top.pc);
704 locals[slot].address = zone->heap().lifoPosition();
710 unsigned slot = READ_UINT32(top.pc);
711 zone->heap().unwindLifo(locals[slot].address);
717 unsigned slot = READ_UINT32(top.pc);
718 if (!PushNewRef(zone, top, &locals[slot], Heap::Tenure_Stack))
725 unsigned slot = READ_UINT32(top.pc);
726 if (!PushNewRef(zone, top, &top.fp->arg(slot), Heap::Tenure_Stack))
731 case OP_ARRAYREF_LIFO:
733 Local<Array> array(zone, top.sp[-2].array);
734 Local<Reference> ref(zone, Reference::NewArray(zone, array, top.sp[-1].i32, Heap::Tenure_Stack));
737 top.sp[-2].ref = ref;
742 case OP_SLOTREF_LIFO:
744 Reference *ref = Reference::NewStack(zone, top.sp[-1], Heap::Tenure_Stack);
747 top.sp[-1].ref = ref;
753 top.sp[-1].i32 = top.sp[-1].ref->getInt();
759 top.sp[-2].ref->setInt(top.sp[-1].i32);
760 top.sp[-2].i32 = top.sp[-1].i32;
767 top.sp[-1].f = top.sp[-1].ref->getFloat();
773 top.sp[-2].ref->setFloat(top.sp[-1].f);
774 top.sp[-2].f = top.sp[-1].f;
780 top.sp->obj = top.fp->code()->object(READ_UINT32(top.pc));
787 // Note for this assignment opcode, we return the left-hand side, not the
788 // right, since in this case the result of the expression could be different.
789 if (!CopyArray(zone, top.sp[-2].array, top.sp[-1].array))
798 top.sp[-1] = top.sp[-2];
805 top.sp[-2] = top.sp[-4];
806 top.sp[-1] = top.sp[-3];
812 LocalSlot slot = top.sp[-2];
813 top.sp[-2] = top.sp[-1];
820 LocalSlot slot = top.sp[-3];
821 top.sp[-3] = top.sp[-2];
822 top.sp[-2] = top.sp[-1];
830 top.sp[-1] = top.sp[-3];
837 top.sp[-1] = top.sp[-4];
843 unsigned index = READ_UINT32(top.pc);
844 unsigned argc = READ_UINT32(top.pc + 4);
846 Local<Native> native(zone, Native::cast(top.fp->code()->module()->natives()->at(index)));
847 if (!native->function()) {
848 zone->reportError(Message_NativeNotBound, native->name()->chars());
853 void *nativeIdentity = top.fp->code()->module()->nativeIdentity();
854 if (!InvokeNative(zone, native, nativeIdentity, top.sp, argc, &result))
858 PUSH_I32(top, result);
865 top.sp[-1].module = top.fp->code()->module();
869 case OP_GETMODULE_FN:
871 top.sp[-1].fun = top.sp[-1].module->getFunction(READ_UINT32(top.pc));
877 unsigned slot = READ_UINT32(top.pc);
879 top.sp[-1].i32 = top.fp->code()->module()->environment()->getInt32(slot);
885 unsigned slot = READ_UINT32(top.pc);
887 top.sp[-1].f = top.fp->code()->module()->environment()->getFloat(slot);
893 unsigned slot = READ_UINT32(top.pc);
895 top.sp[-1].obj = top.fp->code()->module()->environment()->getObject(slot);
901 unsigned slot = READ_UINT32(top.pc);
902 top.fp->code()->module()->environment()->setInt32(slot, top.sp[-1].i32);
908 unsigned slot = READ_UINT32(top.pc);
909 top.fp->code()->module()->environment()->setFloat(slot, top.sp[-1].f);
915 unsigned slot = READ_UINT32(top.pc);
916 top.fp->code()->module()->environment()->setObject(slot, top.sp[-1].obj);
926 top.pc += OpcodeInfo[op].length;
933 // Unwind frames until we can continue.
934 while (top.fp != entryFrame)
935 zone->stack().popInlineFrame();