Added complement operator support.
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;
216 #define RELATIONAL_JUMP(op) \
218 int right = POP_I32(top); \
219 int left = POP_I32(top); \
220 if (left op right) { \
221 top.pc += READ_INT32(top.pc); \
250 #undef RELATIONAL_JUMP
254 int value = POP_I32(top);
256 top.pc += READ_INT32(top.pc);
264 int value = POP_I32(top);
266 top.pc += READ_INT32(top.pc);
272 #define RELATIONAL_JUMP_F(op) \
274 float right = POP_F(top); \
275 float left = POP_F(top); \
276 if (left op right) { \
277 top.pc += READ_INT32(top.pc); \
283 RELATIONAL_JUMP_F(<);
287 RELATIONAL_JUMP_F(<=);
291 RELATIONAL_JUMP_F(>);
295 RELATIONAL_JUMP_F(>=);
299 RELATIONAL_JUMP_F(==);
303 RELATIONAL_JUMP_F(!=);
308 float value = POP_F(top);
310 top.pc += READ_INT32(top.pc);
318 float value = POP_F(top);
320 top.pc += READ_INT32(top.pc);
327 top.pc += READ_INT32(top.pc);
332 int slot = READ_INT32(top.pc);
333 *top.sp++ = locals[slot];
339 int slot = READ_INT32(top.pc);
340 locals[slot] = top.sp[-1];
346 top.sp[-2].i32 = top.sp[-2].i32 + top.sp[-1].i32;
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 if (top.sp[-1].i32 == 0) {
368 zone->reportError(Message_IntegerDivideByZero);
371 top.sp[-2].i32 = top.sp[-2].i32 / top.sp[-1].i32;
378 top.sp[-2].i32 = top.sp[-2].i32 < top.sp[-1].i32;
385 top.sp[-2].i32 = top.sp[-2].i32 <= top.sp[-1].i32;
392 top.sp[-2].i32 = top.sp[-2].i32 > top.sp[-1].i32;
399 top.sp[-2].i32 = top.sp[-2].i32 >= top.sp[-1].i32;
406 top.sp[-2].i32 = (top.sp[-2].i32 == top.sp[-1].i32);
413 top.sp[-2].i32 = (top.sp[-2].i32 != top.sp[-1].i32);
420 top.sp[-2].f = top.sp[-2].f + top.sp[-1].f;
427 top.sp[-2].f = top.sp[-2].f - top.sp[-1].f;
434 top.sp[-2].f = top.sp[-2].f * top.sp[-1].f;
441 top.sp[-2].f = top.sp[-2].f / top.sp[-1].f;
448 top.sp[-2].f = top.sp[-2].f < top.sp[-1].f;
455 top.sp[-2].f = top.sp[-2].f <= top.sp[-1].f;
462 top.sp[-2].f = top.sp[-2].f > top.sp[-1].f;
469 top.sp[-2].f = top.sp[-2].f >= top.sp[-1].f;
476 top.sp[-2].i32 = (top.sp[-2].f == top.sp[-1].f);
483 top.sp[-2].i32 = (top.sp[-2].f != top.sp[-1].f);
490 top.sp[-2].i32 = top.sp[-2].i32 << top.sp[-1].i32;
497 top.sp[-2].i32 = top.sp[-2].i32 >> top.sp[-1].i32;
504 top.sp[-2].i32 = unsigned(top.sp[-2].i32) << unsigned(top.sp[-1].i32);
511 top.sp[-2].i32 = top.sp[-2].i32 & top.sp[-1].i32;
518 top.sp[-2].i32 = top.sp[-2].i32 | top.sp[-1].i32;
525 top.sp[-2].i32 = top.sp[-2].i32 ^ top.sp[-1].i32;
532 top.sp[-1].i32 = ~top.sp[-1].i32;
542 zone->heap().reclaim();
544 Local<Function> newFun(zone, top.sp[-1].fun);
547 if (!zone->stack().pushInlineFrame(newFun))
550 locals = top.fp->locals();
556 unsigned slot = READ_UINT32(top.pc);
557 *top.sp++ = top.fp->arg(slot);
563 unsigned slot = READ_UINT32(top.pc);
564 top.fp->arg(slot) = *--top.sp;
570 Array *array = top.sp[-2].array;
571 int index = top.sp[-1].i32;
572 if (index < 0 || unsigned(index) >= array->nelements()) {
573 zone->reportError(Message_ArrayIndexOutOfBounds, index, array->nelements());
581 Array *array = top.sp[-2].array;
582 int index = top.sp[-1].i32;
583 top.sp[-2].i32 = array->readInt32(index);
590 Array *array = top.sp[-2].array;
591 int index = top.sp[-1].i32;
592 top.sp[-2].f = array->readFloat(index);
599 Array *array = top.sp[-2].array;
600 int index = top.sp[-1].i32;
601 top.sp[-2].i32 = array->readChar(index);
608 Array *array = top.sp[-3].array;
609 int index = top.sp[-2].i32;
610 array->writeInt32(index, top.sp[-1].i32);
611 top.sp[-3] = top.sp[-1];
618 Array *array = top.sp[-3].array;
619 int index = top.sp[-2].i32;
620 array->writeFloat(index, top.sp[-1].f);
621 top.sp[-3] = top.sp[-1];
628 Array *array = top.sp[-3].array;
629 int index = top.sp[-2].i32;
630 array->writeChar(index, top.sp[-1].i32);
631 top.sp[-3] = top.sp[-1];
638 Array *array = top.sp[-2].array;
639 int index = top.sp[-1].i32;
640 top.sp[-2].array = Array::cast(array->readObject(index));
647 int ival = POP_I32(top);
648 float fval = (float)ival;
653 case OP_NEWARRAY_LIFO:
655 Local<ArrayMap> map(zone, ArrayMap::cast(top.fp->code()->map(READ_UINT32(top.pc))));
656 Array *array = GenerateArray(zone, map);
666 unsigned slot = READ_UINT32(top.pc);
667 locals[slot].address = zone->heap().lifoPosition();
673 unsigned slot = READ_UINT32(top.pc);
674 zone->heap().unwindLifo(locals[slot].address);
680 unsigned slot = READ_UINT32(top.pc);
681 if (!PushNewRef(zone, top, &locals[slot], Heap::Tenure_Stack))
688 unsigned slot = READ_UINT32(top.pc);
689 if (!PushNewRef(zone, top, &top.fp->arg(slot), Heap::Tenure_Stack))
694 case OP_ARRAYREF_LIFO:
696 Local<Array> array(zone, top.sp[-2].array);
697 Local<Reference> ref(zone, Reference::NewArray(zone, array, top.sp[-1].i32, Heap::Tenure_Stack));
700 top.sp[-2].ref = ref;
705 case OP_SLOTREF_LIFO:
707 Reference *ref = Reference::NewStack(zone, top.sp[-1], Heap::Tenure_Stack);
710 top.sp[-1].ref = ref;
716 top.sp[-1].i32 = top.sp[-1].ref->getInt();
722 top.sp[-2].ref->setInt(top.sp[-1].i32);
723 top.sp[-2].i32 = top.sp[-1].i32;
730 top.sp[-1].f = top.sp[-1].ref->getFloat();
736 top.sp[-2].ref->setFloat(top.sp[-1].f);
737 top.sp[-2].f = top.sp[-1].f;
743 top.sp->obj = top.fp->code()->object(READ_UINT32(top.pc));
750 // Note for this assignment opcode, we return the left-hand side, not the
751 // right, since in this case the result of the expression could be different.
752 if (!CopyArray(zone, top.sp[-2].array, top.sp[-1].array))
761 top.sp[-1] = top.sp[-2];
768 top.sp[-2] = top.sp[-4];
769 top.sp[-1] = top.sp[-3];
775 LocalSlot slot = top.sp[-2];
776 top.sp[-2] = top.sp[-1];
783 LocalSlot slot = top.sp[-3];
784 top.sp[-3] = top.sp[-2];
785 top.sp[-2] = top.sp[-1];
793 top.sp[-1] = top.sp[-3];
800 top.sp[-1] = top.sp[-4];
806 unsigned index = READ_UINT32(top.pc);
807 unsigned argc = READ_UINT32(top.pc + 4);
809 Local<Native> native(zone, Native::cast(top.fp->code()->module()->natives()->at(index)));
810 if (!native->function()) {
811 zone->reportError(Message_NativeNotBound, native->name()->chars());
816 void *nativeIdentity = top.fp->code()->module()->nativeIdentity();
817 if (!InvokeNative(zone, native, nativeIdentity, top.sp, argc, &result))
821 PUSH_I32(top, result);
828 top.sp[-1].module = top.fp->code()->module();
832 case OP_GETMODULE_FN:
834 top.sp[-1].fun = top.sp[-1].module->getFunction(READ_UINT32(top.pc));
844 top.pc += OpcodeInfo[op].length;
851 // Unwind frames until we can continue.
852 while (top.fp != entryFrame)
853 zone->stack().popInlineFrame();