Added support for modulo operator.
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"
30 #define PUSH_I32(top, value) \
32 top.sp->i32 = value; \
36 #define PUSH_F(top, value) \
45 int value = top.sp[-1].i32;
53 float value = top.sp[-1].f;
58 // Pawn's original semantics specify some kind of floor operation, so
59 // (8 / -3 == -3) and (-8 / 3 == -1). The SourcePawn 1 compiler (spcomp) uses
60 // the upstream algorithm when constant folding. However the SourcePawn 1 JIT
61 // does not. My guess is that Borja freaked out seeing the 13-instruction x86
62 // sequence necessary to get this functionality (I vaguely recall a thread
63 // where we discussed this with Mark Peter? But the ITB forums are gone...),
64 // and we just decided to break the damn thing. So SP1 was inconsistent, but
65 // we are not. And for completeness, we assert that the C compiler does the
66 // right thing, since C89 leaves it unspecified.
67 STATIC_ASSERT(8 / -3 == -2);
68 STATIC_ASSERT(-8 / 3 == -2);
71 PushNewRef(Zone *zone, Top &top, void *location, Heap::Tenure tenure)
73 Reference *ref = Reference::NewStack(zone, location, tenure);
81 // Generate an array with completely static sizes.
83 GenerateArray(Zone *zone, Handle<ArrayMap> map)
85 assert(map->type()->elements() != -1);
87 Local<Array> array(zone, Array::New(zone, map, map->type()->elements(), Heap::Tenure_Stack));
91 if (map->type()->levels() == 1)
94 Local<ArrayMap> contained(zone, map->contained());
95 Local<Array> sub(zone);
96 for (unsigned i = 0; i < unsigned(map->type()->elements()); i++) {
97 sub = GenerateArray(zone, contained);
100 array->writeObject(zone, i, sub);
107 CopyArray(Zone *zone, Array *source, Array *dest)
109 assert(source->type()->levels() == 1);
110 assert(dest->type()->levels() == 1);
111 assert(source->type()->contained()->primitive() ==
112 dest->type()->contained()->primitive());
113 assert(source->nelements() >= dest->nelements());
114 assert(dest->elementSize() == source->elementSize());
116 memcpy(source->vec(), dest->vec(), dest->nelements() * dest->elementSize());
121 InvokeNative(Zone *zone, Handle<Native> native, void *identity, LocalSlot *args, unsigned argc,
126 if (!zone->stack().pushNativeFrame(native, guard, argc + 1))
129 intptr_t *cells = guard.args();
131 // params[0] == argc, in ISPE
134 // Pass the formal arguments.
135 Local<Type> type(zone, native->type());
136 Local<Type> param(zone);
137 for (unsigned i = 0; i < type->parameters()->length(); i++) {
138 param = type->parameterAt(i);
140 // For floats, we rely on the aliasing of |i32| and |f|.
141 LocalSlot &arg = args[-int(i) - 1];
142 if (param->isInt32() || param->isFloat()) {
143 cells[i + 1] = arg.i32;
144 } else if (param->isReference()) {
145 cells[i + 1] = reinterpret_cast<intptr_t>(arg.ref->location());
147 assert(param->isArray());
148 cells[i + 1] = reinterpret_cast<intptr_t>(arg.array->vec());
152 // For variadic arguments, the compiler left either arrays or references.
153 for (unsigned i = type->parameters()->length(); i < argc; i++) {
154 LocalSlot &arg = args[-int(i) - 1];
156 if (arg.obj->is(MapKind_Reference)) {
157 cells[i + 1] = reinterpret_cast<intptr_t>(arg.ref->location());
159 assert(arg.obj->is(MapKind_Array));
160 cells[i + 1] = reinterpret_cast<intptr_t>(arg.array->vec());
164 typedef intptr_t (*INVOKE)(void *, intptr_t *);
165 INVOKE invoke = (INVOKE)native->function();
166 *result = invoke(identity, cells);
168 return !zone->hasPendingException();
172 ke::Interpret(Zone *zone, Frame *entryFrame, LocalSlot *result)
174 Top &top = *zone->stack().top();
178 LocalSlot *locals = entryFrame->locals();
181 Opcode op = Opcode(*top.pc);
188 PUSH_I32(top, READ_INT32(top.pc));
192 PUSH_F(top, READ_FLOAT(top.pc));
202 if (top.fp == entryFrame)
206 zone->stack().popInlineFrame();
207 locals = top.fp->locals();
212 top.pc += OpcodeInfo[*top.pc].length;
222 #define RELATIONAL_JUMP(op) \
224 int right = POP_I32(top); \
225 int left = POP_I32(top); \
226 if (left op right) { \
227 top.pc += READ_INT32(top.pc); \
256 #undef RELATIONAL_JUMP
260 int value = POP_I32(top);
262 top.pc += READ_INT32(top.pc);
270 int value = POP_I32(top);
272 top.pc += READ_INT32(top.pc);
278 #define RELATIONAL_JUMP_F(op) \
280 float right = POP_F(top); \
281 float left = POP_F(top); \
282 if (left op right) { \
283 top.pc += READ_INT32(top.pc); \
289 RELATIONAL_JUMP_F(<);
293 RELATIONAL_JUMP_F(<=);
297 RELATIONAL_JUMP_F(>);
301 RELATIONAL_JUMP_F(>=);
305 RELATIONAL_JUMP_F(==);
309 RELATIONAL_JUMP_F(!=);
314 float value = POP_F(top);
316 top.pc += READ_INT32(top.pc);
324 float value = POP_F(top);
326 top.pc += READ_INT32(top.pc);
333 top.pc += READ_INT32(top.pc);
338 int slot = READ_INT32(top.pc);
339 *top.sp++ = locals[slot];
345 int slot = READ_INT32(top.pc);
346 locals[slot] = top.sp[-1];
352 top.sp[-2].i32 = top.sp[-2].i32 + top.sp[-1].i32;
359 top.sp[-2].i32 = top.sp[-2].i32 - top.sp[-1].i32;
366 top.sp[-2].i32 = top.sp[-2].i32 * top.sp[-1].i32;
373 if (top.sp[-1].i32 == 0) {
374 zone->reportError(Message_IntegerDivideByZero);
377 top.sp[-2].i32 = top.sp[-2].i32 / top.sp[-1].i32;
384 if (top.sp[-1].i32 == 0) {
385 zone->reportError(Message_IntegerDivideByZero);
388 top.sp[-2].i32 = top.sp[-2].i32 % top.sp[-1].i32;
395 top.sp[-2].i32 = top.sp[-2].i32 < top.sp[-1].i32;
402 top.sp[-2].i32 = top.sp[-2].i32 <= top.sp[-1].i32;
409 top.sp[-2].i32 = top.sp[-2].i32 > top.sp[-1].i32;
416 top.sp[-2].i32 = top.sp[-2].i32 >= top.sp[-1].i32;
423 top.sp[-2].i32 = (top.sp[-2].i32 == top.sp[-1].i32);
430 top.sp[-2].i32 = (top.sp[-2].i32 != top.sp[-1].i32);
437 top.sp[-2].f = top.sp[-2].f + top.sp[-1].f;
444 top.sp[-2].f = top.sp[-2].f - top.sp[-1].f;
451 top.sp[-2].f = top.sp[-2].f * top.sp[-1].f;
458 top.sp[-2].f = top.sp[-2].f / top.sp[-1].f;
465 top.sp[-2].f = top.sp[-2].f < top.sp[-1].f;
472 top.sp[-2].f = top.sp[-2].f <= top.sp[-1].f;
479 top.sp[-2].f = top.sp[-2].f > top.sp[-1].f;
486 top.sp[-2].f = top.sp[-2].f >= top.sp[-1].f;
493 top.sp[-2].i32 = (top.sp[-2].f == top.sp[-1].f);
500 top.sp[-2].i32 = (top.sp[-2].f != top.sp[-1].f);
507 top.sp[-2].i32 = top.sp[-2].i32 << top.sp[-1].i32;
514 top.sp[-2].i32 = top.sp[-2].i32 >> top.sp[-1].i32;
521 top.sp[-2].i32 = unsigned(top.sp[-2].i32) << unsigned(top.sp[-1].i32);
528 top.sp[-2].i32 = top.sp[-2].i32 & top.sp[-1].i32;
535 top.sp[-2].i32 = top.sp[-2].i32 | top.sp[-1].i32;
542 top.sp[-2].i32 = top.sp[-2].i32 ^ top.sp[-1].i32;
549 top.sp[-1].i32 = ~top.sp[-1].i32;
555 top.sp[-1].i32 = top.sp[-1].i32 ? 0 : 1;
561 top.sp[-1].i32 = top.sp[-1].f ? 0 : 1;
567 top.sp[-1].i32 = -top.sp[-1].i32;
573 top.sp[-1].f = -top.sp[-1].f;
579 Local<Function> newFun(zone, top.sp[-1].fun);
582 if (!zone->stack().pushInlineFrame(newFun))
585 locals = top.fp->locals();
591 unsigned slot = READ_UINT32(top.pc);
592 *top.sp++ = top.fp->arg(slot);
598 unsigned slot = READ_UINT32(top.pc);
599 top.fp->arg(slot) = *--top.sp;
605 Array *array = top.sp[-2].array;
606 int index = top.sp[-1].i32;
607 if (index < 0 || unsigned(index) >= array->nelements()) {
608 zone->reportError(Message_ArrayIndexOutOfBounds, index, array->nelements());
616 Array *array = top.sp[-2].array;
617 int index = top.sp[-1].i32;
618 top.sp[-2].i32 = array->readInt32(index);
625 Array *array = top.sp[-2].array;
626 int index = top.sp[-1].i32;
627 top.sp[-2].f = array->readFloat(index);
634 Array *array = top.sp[-2].array;
635 int index = top.sp[-1].i32;
636 top.sp[-2].i32 = array->readChar(index);
643 Array *array = top.sp[-3].array;
644 int index = top.sp[-2].i32;
645 array->writeInt32(index, top.sp[-1].i32);
646 top.sp[-3] = top.sp[-1];
653 Array *array = top.sp[-3].array;
654 int index = top.sp[-2].i32;
655 array->writeFloat(index, top.sp[-1].f);
656 top.sp[-3] = top.sp[-1];
663 Array *array = top.sp[-3].array;
664 int index = top.sp[-2].i32;
665 array->writeChar(index, top.sp[-1].i32);
666 top.sp[-3] = top.sp[-1];
673 Array *array = top.sp[-2].array;
674 int index = top.sp[-1].i32;
675 top.sp[-2].array = Array::cast(array->readObject(index));
682 int ival = POP_I32(top);
683 float fval = (float)ival;
688 case OP_NEWARRAY_LIFO:
690 Local<ArrayMap> map(zone, ArrayMap::cast(top.fp->code()->map(READ_UINT32(top.pc))));
691 Array *array = GenerateArray(zone, map);
701 unsigned slot = READ_UINT32(top.pc);
702 locals[slot].address = zone->heap().lifoPosition();
708 unsigned slot = READ_UINT32(top.pc);
709 zone->heap().unwindLifo(locals[slot].address);
715 unsigned slot = READ_UINT32(top.pc);
716 if (!PushNewRef(zone, top, &locals[slot], Heap::Tenure_Stack))
723 unsigned slot = READ_UINT32(top.pc);
724 if (!PushNewRef(zone, top, &top.fp->arg(slot), Heap::Tenure_Stack))
729 case OP_ARRAYREF_LIFO:
731 Local<Array> array(zone, top.sp[-2].array);
732 Local<Reference> ref(zone, Reference::NewArray(zone, array, top.sp[-1].i32, Heap::Tenure_Stack));
735 top.sp[-2].ref = ref;
740 case OP_SLOTREF_LIFO:
742 Reference *ref = Reference::NewStack(zone, top.sp[-1], Heap::Tenure_Stack);
745 top.sp[-1].ref = ref;
751 top.sp[-1].i32 = top.sp[-1].ref->getInt();
757 top.sp[-2].ref->setInt(top.sp[-1].i32);
758 top.sp[-2].i32 = top.sp[-1].i32;
765 top.sp[-1].f = top.sp[-1].ref->getFloat();
771 top.sp[-2].ref->setFloat(top.sp[-1].f);
772 top.sp[-2].f = top.sp[-1].f;
778 top.sp->obj = top.fp->code()->object(READ_UINT32(top.pc));
785 // Note for this assignment opcode, we return the left-hand side, not the
786 // right, since in this case the result of the expression could be different.
787 if (!CopyArray(zone, top.sp[-2].array, top.sp[-1].array))
796 top.sp[-1] = top.sp[-2];
803 top.sp[-2] = top.sp[-4];
804 top.sp[-1] = top.sp[-3];
810 LocalSlot slot = top.sp[-2];
811 top.sp[-2] = top.sp[-1];
818 LocalSlot slot = top.sp[-3];
819 top.sp[-3] = top.sp[-2];
820 top.sp[-2] = top.sp[-1];
828 top.sp[-1] = top.sp[-3];
835 top.sp[-1] = top.sp[-4];
841 unsigned index = READ_UINT32(top.pc);
842 unsigned argc = READ_UINT32(top.pc + 4);
844 Local<Native> native(zone, Native::cast(top.fp->code()->module()->natives()->at(index)));
845 if (!native->function()) {
846 zone->reportError(Message_NativeNotBound, native->name()->chars());
851 void *nativeIdentity = top.fp->code()->module()->nativeIdentity();
852 if (!InvokeNative(zone, native, nativeIdentity, top.sp, argc, &result))
856 PUSH_I32(top, result);
863 top.sp[-1].module = top.fp->code()->module();
867 case OP_GETMODULE_FN:
869 top.sp[-1].fun = top.sp[-1].module->getFunction(READ_UINT32(top.pc));
879 top.pc += OpcodeInfo[op].length;
886 // Unwind frames until we can continue.
887 while (top.fp != entryFrame)
888 zone->stack().popInlineFrame();