Implement re-tag unary expression.
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"
32 #define PUSH_I32(top, value) \
34 top.sp->i32 = value; \
38 #define PUSH_F(top, value) \
47 int value = top.sp[-1].i32;
55 float value = top.sp[-1].f;
60 // Pawn's original semantics specify some kind of floor operation, so
61 // (8 / -3 == -3) and (-8 / 3 == -1). The SourcePawn 1 compiler (spcomp) uses
62 // the upstream algorithm when constant folding. However the SourcePawn 1 JIT
63 // does not. My guess is that Borja freaked out seeing the 13-instruction x86
64 // sequence necessary to get this functionality (I vaguely recall a thread
65 // where we discussed this with Mark Peter? But the ITB forums are gone...),
66 // and we just decided to break the damn thing. So SP1 was inconsistent, but
67 // we are not. And for completeness, we assert that the C compiler does the
68 // right thing, since C89 leaves it unspecified.
69 STATIC_ASSERT(8 / -3 == -2);
70 STATIC_ASSERT(-8 / 3 == -2);
73 PushNewRef(Zone *zone, Top &top, void *location, Heap::Tenure tenure)
75 Reference *ref = Reference::NewStack(zone, location, tenure);
83 // Generate an array with completely static sizes.
85 GenerateArray(Zone *zone, Handle<ArrayMap> map, Heap::Tenure tenure)
87 assert(map->type()->elements() != -1);
89 Local<Array> array(zone, Array::New(zone, map, map->type()->elements(), tenure));
93 if (map->type()->levels() == 1)
96 Local<ArrayMap> contained(zone, map->contained());
97 Local<Array> sub(zone);
98 for (unsigned i = 0; i < unsigned(map->type()->elements()); i++) {
99 sub = GenerateArray(zone, contained, tenure);
102 array->writeObject(zone, i, sub);
109 CopyArray(Zone *zone, Array *source, Array *dest)
111 assert(source->type()->levels() == 1);
112 assert(dest->type()->levels() == 1);
113 assert(source->type()->contained()->primitive() ==
114 dest->type()->contained()->primitive());
115 assert(source->nelements() >= dest->nelements());
116 assert(dest->elementSize() == source->elementSize());
118 memcpy(source->vec(), dest->vec(), dest->nelements() * dest->elementSize());
123 InvokeNative(Zone *zone, Handle<Native> native, void *identity, LocalSlot *args, unsigned argc,
128 if (!zone->stack().pushNativeFrame(native, guard, argc + 1))
131 intptr_t *cells = guard.args();
133 // params[0] == argc, in ISPE
136 // Pass the formal arguments.
137 Local<Type> type(zone, native->type());
138 Local<Type> param(zone);
139 for (unsigned i = 0; i < type->parameters()->length(); i++) {
140 param = type->parameterAt(i);
142 // For floats, we rely on the aliasing of |i32| and |f|.
143 LocalSlot &arg = args[-int(i) - 1];
144 if (param->isInt32() || param->isFloat()) {
145 cells[i + 1] = arg.i32;
146 } else if (param->isReference()) {
147 cells[i + 1] = reinterpret_cast<intptr_t>(arg.ref->location());
149 assert(param->isArray());
150 cells[i + 1] = reinterpret_cast<intptr_t>(arg.array->vec());
154 // For variadic arguments, the compiler left either arrays or references.
155 for (unsigned i = type->parameters()->length(); i < argc; i++) {
156 LocalSlot &arg = args[-int(i) - 1];
158 if (arg.obj->is(MapKind_Reference)) {
159 cells[i + 1] = reinterpret_cast<intptr_t>(arg.ref->location());
161 assert(arg.obj->is(MapKind_Array));
162 cells[i + 1] = reinterpret_cast<intptr_t>(arg.array->vec());
166 typedef intptr_t (*INVOKE)(void *, intptr_t *);
167 INVOKE invoke = (INVOKE)native->function();
168 *result = invoke(identity, cells);
170 return !zone->hasPendingException();
174 ke::Interpret(Zone *zone, Frame *entryFrame, LocalSlot *result)
176 Top &top = *zone->stack().top();
180 LocalSlot *locals = entryFrame->locals();
183 Opcode op = Opcode(*top.pc);
190 PUSH_I32(top, READ_INT32(top.pc));
194 PUSH_F(top, READ_FLOAT(top.pc));
204 if (top.fp == entryFrame)
208 zone->stack().popInlineFrame();
209 locals = top.fp->locals();
214 top.pc += OpcodeInfo[*top.pc].length;
224 #define RELATIONAL_JUMP(op) \
226 int right = POP_I32(top); \
227 int left = POP_I32(top); \
228 if (left op right) { \
229 top.pc += READ_INT32(top.pc); \
258 #undef RELATIONAL_JUMP
262 int value = POP_I32(top);
264 top.pc += READ_INT32(top.pc);
272 int value = POP_I32(top);
274 top.pc += READ_INT32(top.pc);
280 #define RELATIONAL_JUMP_F(op) \
282 float right = POP_F(top); \
283 float left = POP_F(top); \
284 if (left op right) { \
285 top.pc += READ_INT32(top.pc); \
291 RELATIONAL_JUMP_F(<);
295 RELATIONAL_JUMP_F(<=);
299 RELATIONAL_JUMP_F(>);
303 RELATIONAL_JUMP_F(>=);
307 RELATIONAL_JUMP_F(==);
311 RELATIONAL_JUMP_F(!=);
316 float value = POP_F(top);
318 top.pc += READ_INT32(top.pc);
326 float value = POP_F(top);
328 top.pc += READ_INT32(top.pc);
335 top.pc += READ_INT32(top.pc);
340 int slot = READ_INT32(top.pc);
341 *top.sp++ = locals[slot];
347 int slot = READ_INT32(top.pc);
348 locals[slot] = top.sp[-1];
354 top.sp[-2].i32 = top.sp[-2].i32 + top.sp[-1].i32;
361 top.sp[-2].i32 = top.sp[-2].i32 - top.sp[-1].i32;
368 top.sp[-2].i32 = top.sp[-2].i32 * top.sp[-1].i32;
375 if (top.sp[-1].i32 == 0) {
376 zone->reportError(Message_IntegerDivideByZero);
379 top.sp[-2].i32 = top.sp[-2].i32 / top.sp[-1].i32;
386 if (top.sp[-1].i32 == 0) {
387 zone->reportError(Message_IntegerDivideByZero);
390 top.sp[-2].i32 = top.sp[-2].i32 % top.sp[-1].i32;
397 top.sp[-2].i32 = top.sp[-2].i32 < top.sp[-1].i32;
404 top.sp[-2].i32 = top.sp[-2].i32 <= top.sp[-1].i32;
411 top.sp[-2].i32 = top.sp[-2].i32 > top.sp[-1].i32;
418 top.sp[-2].i32 = top.sp[-2].i32 >= top.sp[-1].i32;
425 top.sp[-2].i32 = (top.sp[-2].i32 == top.sp[-1].i32);
432 top.sp[-2].i32 = (top.sp[-2].i32 != top.sp[-1].i32);
439 top.sp[-2].f = top.sp[-2].f + top.sp[-1].f;
446 top.sp[-2].f = top.sp[-2].f - top.sp[-1].f;
453 top.sp[-2].f = top.sp[-2].f * top.sp[-1].f;
460 top.sp[-2].f = top.sp[-2].f / top.sp[-1].f;
467 top.sp[-2].f = top.sp[-2].f < top.sp[-1].f;
474 top.sp[-2].f = top.sp[-2].f <= top.sp[-1].f;
481 top.sp[-2].f = top.sp[-2].f > top.sp[-1].f;
488 top.sp[-2].f = top.sp[-2].f >= top.sp[-1].f;
495 top.sp[-2].i32 = (top.sp[-2].f == top.sp[-1].f);
502 top.sp[-2].i32 = (top.sp[-2].f != top.sp[-1].f);
509 top.sp[-2].i32 = top.sp[-2].i32 << top.sp[-1].i32;
516 top.sp[-2].i32 = top.sp[-2].i32 >> top.sp[-1].i32;
523 top.sp[-2].i32 = unsigned(top.sp[-2].i32) << unsigned(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[-1].i32 = ~top.sp[-1].i32;
557 top.sp[-1].i32 = top.sp[-1].i32 ? 0 : 1;
563 top.sp[-1].i32 = top.sp[-1].f ? 0 : 1;
569 top.sp[-1].i32 = -top.sp[-1].i32;
575 top.sp[-1].f = -top.sp[-1].f;
581 Local<Function> newFun(zone, top.sp[-1].fun);
584 if (!zone->stack().pushInlineFrame(newFun))
587 locals = top.fp->locals();
593 unsigned slot = READ_UINT32(top.pc);
594 *top.sp++ = top.fp->arg(slot);
600 unsigned slot = READ_UINT32(top.pc);
601 top.fp->arg(slot) = *--top.sp;
607 Array *array = top.sp[-2].array;
608 int index = top.sp[-1].i32;
609 if (index < 0 || unsigned(index) >= array->nelements()) {
610 zone->reportError(Message_ArrayIndexOutOfBounds, index, array->nelements());
618 Array *array = top.sp[-2].array;
619 int index = top.sp[-1].i32;
620 top.sp[-2].i32 = array->readInt32(index);
627 Array *array = top.sp[-2].array;
628 int index = top.sp[-1].i32;
629 top.sp[-2].f = array->readFloat(index);
636 Array *array = top.sp[-2].array;
637 int index = top.sp[-1].i32;
638 top.sp[-2].i32 = array->readChar(index);
645 Array *array = top.sp[-3].array;
646 int index = top.sp[-2].i32;
647 array->writeInt32(index, top.sp[-1].i32);
648 top.sp[-3] = top.sp[-1];
655 Array *array = top.sp[-3].array;
656 int index = top.sp[-2].i32;
657 array->writeFloat(index, top.sp[-1].f);
658 top.sp[-3] = top.sp[-1];
665 Array *array = top.sp[-3].array;
666 int index = top.sp[-2].i32;
667 array->writeChar(index, top.sp[-1].i32);
668 top.sp[-3] = top.sp[-1];
675 Array *array = top.sp[-2].array;
676 int index = top.sp[-1].i32;
677 top.sp[-2].array = Array::cast(array->readObject(index));
684 int ival = POP_I32(top);
685 float fval = (float)ival;
692 Heap::Tenure tenure = (Heap::Tenure)top.pc[1];
693 Local<ArrayMap> map(zone, ArrayMap::cast(top.fp->code()->map(READ_UINT32(top.pc + 1))));
694 Array *array = GenerateArray(zone, map, tenure);
704 unsigned slot = READ_UINT32(top.pc);
705 locals[slot].address = zone->heap().lifoPosition();
711 unsigned slot = READ_UINT32(top.pc);
712 zone->heap().unwindLifo(locals[slot].address);
718 unsigned slot = READ_UINT32(top.pc);
719 if (!PushNewRef(zone, top, &locals[slot], Heap::Tenure_Stack))
726 unsigned slot = READ_UINT32(top.pc);
727 if (!PushNewRef(zone, top, &top.fp->arg(slot), Heap::Tenure_Stack))
732 case OP_ARRAYREF_LIFO:
734 Local<Array> array(zone, top.sp[-2].array);
735 Local<Reference> ref(zone, Reference::NewArray(zone, array, top.sp[-1].i32, Heap::Tenure_Stack));
738 top.sp[-2].ref = ref;
743 case OP_SLOTREF_LIFO:
745 Reference *ref = Reference::NewStack(zone, top.sp[-1], Heap::Tenure_Stack);
748 top.sp[-1].ref = ref;
754 top.sp[-1].i32 = top.sp[-1].ref->getInt();
760 top.sp[-2].ref->setInt(top.sp[-1].i32);
761 top.sp[-2].i32 = top.sp[-1].i32;
768 top.sp[-1].f = top.sp[-1].ref->getFloat();
774 top.sp[-2].ref->setFloat(top.sp[-1].f);
775 top.sp[-2].f = top.sp[-1].f;
781 top.sp->obj = top.fp->code()->object(READ_UINT32(top.pc));
788 // Note for this assignment opcode, we return the left-hand side, not the
789 // right, since in this case the result of the expression could be different.
790 if (!CopyArray(zone, top.sp[-2].array, top.sp[-1].array))
799 top.sp[-1] = top.sp[-2];
806 top.sp[-2] = top.sp[-4];
807 top.sp[-1] = top.sp[-3];
813 LocalSlot slot = top.sp[-2];
814 top.sp[-2] = top.sp[-1];
821 LocalSlot slot = top.sp[-3];
822 top.sp[-3] = top.sp[-2];
823 top.sp[-2] = top.sp[-1];
831 top.sp[-1] = top.sp[-3];
838 top.sp[-1] = top.sp[-4];
844 unsigned index = READ_UINT32(top.pc);
845 unsigned argc = READ_UINT32(top.pc + 4);
847 Local<Native> native(zone, Native::cast(top.fp->code()->module()->natives()->at(index)));
848 if (!native->function()) {
849 zone->reportError(Message_NativeNotBound, native->name()->chars());
854 void *nativeIdentity = top.fp->code()->module()->nativeIdentity();
855 if (!InvokeNative(zone, native, nativeIdentity, top.sp, argc, &result))
859 PUSH_I32(top, result);
866 top.sp[-1].module = top.fp->code()->module();
870 case OP_GETMODULE_FN:
872 top.sp[-1].fun = top.sp[-1].module->getFunction(READ_UINT32(top.pc));
878 unsigned slot = READ_UINT32(top.pc);
880 top.sp[-1].i32 = top.fp->code()->module()->environment()->getInt32(slot);
886 unsigned slot = READ_UINT32(top.pc);
888 top.sp[-1].f = top.fp->code()->module()->environment()->getFloat(slot);
894 unsigned slot = READ_UINT32(top.pc);
896 top.sp[-1].obj = top.fp->code()->module()->environment()->getObject(slot);
902 unsigned slot = READ_UINT32(top.pc);
903 top.fp->code()->module()->environment()->setInt32(slot, top.sp[-1].i32);
909 unsigned slot = READ_UINT32(top.pc);
910 top.fp->code()->module()->environment()->setFloat(slot, top.sp[-1].f);
916 unsigned slot = READ_UINT32(top.pc);
917 top.fp->code()->module()->environment()->setObject(slot, top.sp[-1].obj);
924 // Because the stack is a union, these are actually no-ops in the
935 top.pc += OpcodeInfo[op].length;
942 // Unwind frames until we can continue.
943 while (top.fp != entryFrame)
944 zone->stack().popInlineFrame();