Parser support for public variables.
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"
33 #define PUSH_I32(top, value) \
35 top.sp->i32 = value; \
39 #define PUSH_F(top, value) \
48 int value = top.sp[-1].i32;
56 float value = top.sp[-1].f;
61 // Pawn's original semantics specify some kind of floor operation, so
62 // (8 / -3 == -3) and (-8 / 3 == -1). The SourcePawn 1 compiler (spcomp) uses
63 // the upstream algorithm when constant folding. However the SourcePawn 1 JIT
64 // does not. My guess is that Borja freaked out seeing the 13-instruction x86
65 // sequence necessary to get this functionality (I vaguely recall a thread
66 // where we discussed this with Mark Peter? But the ITB forums are gone...),
67 // and we just decided to break the damn thing. So SP1 was inconsistent, but
68 // we are not. And for completeness, we assert that the C compiler does the
69 // right thing, since C89 leaves it unspecified.
70 STATIC_ASSERT(8 / -3 == -2);
71 STATIC_ASSERT(-8 / 3 == -2);
74 PushNewRef(Zone *zone, Top &top, void *location, Heap::Tenure tenure)
76 Reference *ref = Reference::NewStack(zone, location, tenure);
84 static bool CopyStruct(Zone *zone, Struct *left, Struct *right);
86 // Even though this function can fail, we immediately bail out, so it doesn't
87 // matter if we've leaving things unrooted.
89 CopyArray(Zone *zone, Array *source, Array *dest)
91 if (source->type()->contained()->isArray()) {
92 assert(dest->type()->contained()->isArray());
93 assert(source->length() == dest->length());
95 for (unsigned i = 0; i < source->length(); i++) {
96 Array *left = Array::cast(source->readObject(i));
97 Array *right = Array::cast(dest->readObject(i));
98 if (!CopyArray(zone, left, right))
101 } else if (source->type()->contained()->isStruct()) {
102 assert(dest->type()->contained() == source->type()->contained());
103 assert(source->length() == dest->length());
105 for (unsigned i = 0; i < source->length(); i++) {
106 Struct *left = Struct::cast(source->readObject(i));
107 Struct *right = Struct::cast(dest->readObject(i));
108 if (!CopyStruct(zone, left, right))
112 assert(source->type()->contained()->pod() == dest->type()->contained()->pod());
113 assert(source->type()->contained()->pod() == PrimitiveType_Char
114 ? source->length() >= dest->length()
115 : source->length() == dest->length());
117 memcpy(source->vec(), dest->vec(), dest->length() * dest->elementSize());
123 GetElement(Array *array, int index, LocalSlot *vp)
125 if (array->type()->contained()->isTraceable()) {
126 vp->obj = array->readObject(index);
130 switch (array->type()->contained()->pod()) {
131 case PrimitiveType_Float:
132 vp->f = array->readFloat(index);
135 case PrimitiveType_Char:
136 vp->i32 = array->readChar(index);
140 assert(array->type()->contained()->pod() == PrimitiveType_Int32);
141 vp->i32 = array->readInt32(index);
147 SetElement(Zone *zone, Array *array, int index, const LocalSlot &v)
149 if (array->type()->contained()->isTraceable()) {
150 array->writeObject(zone, index, v.obj);
154 switch (array->type()->contained()->pod()) {
155 case PrimitiveType_Float:
156 array->writeFloat(index, v.f);
159 case PrimitiveType_Char:
160 array->writeChar(index, v.i32);
164 assert(array->type()->contained()->pod() == PrimitiveType_Int32);
165 array->writeInt32(index, v.i32);
171 GetField(Struct *obj, Descriptor *desc, LocalSlot *vp)
173 // :TODO: assert no GC.
174 if (desc->type()->isTraceable()) {
175 vp->obj = obj->get<Object *>(desc);
179 switch (desc->type()->pod()) {
180 case PrimitiveType_Float:
181 vp->f = obj->get<float>(desc);
185 assert(desc->type()->pod() == PrimitiveType_Int32);
186 vp->i32 = obj->get<int>(desc);
192 GetField(Struct *obj, unsigned index, LocalSlot *vp)
194 Descriptor *desc = Descriptor::cast(obj->map()->type()->fields()->at(index));
195 GetField(obj, desc, vp);
199 SetField(Zone *zone, Struct *obj, Descriptor *desc, const LocalSlot &v)
201 // :TODO: assert no GC.
202 if (desc->type()->isTraceable()) {
203 obj->set(zone, desc, v.obj);
207 switch (desc->type()->pod()) {
208 case PrimitiveType_Float:
209 obj->set<float>(desc, v.f);
213 assert(desc->type()->pod() == PrimitiveType_Int32);
214 obj->set<int>(desc, v.i32);
220 SetField(Zone *zone, Struct *obj, unsigned index, const LocalSlot &v)
222 Descriptor *desc = Descriptor::cast(obj->map()->type()->fields()->at(index));
223 SetField(zone, obj, desc, v);
226 // Even though this function can fail, we immediately bail out, so it doesn't
227 // matter if we've leaving things unrooted.
229 CopyStruct(Zone *zone, Struct *left, Struct *right)
231 assert(left->map()->type() == right->map()->type());
233 Type *type = left->map()->type();
234 for (unsigned i = 0; i < type->fields()->length(); i++) {
235 Descriptor *desc = Descriptor::cast(type->fields()->at(i));
238 GetField(right, desc, &rval);
240 // If the field is a struct, we need to deep-copy.
241 if (desc->type()->isStruct()) {
244 GetField(left, desc, &lval);
245 if (!CopyStruct(zone, Struct::cast(lval.obj), Struct::cast(rval.obj)))
247 } else if (desc->type()->isArray()) {
250 GetField(left, desc, &lval);
251 if (!CopyArray(zone, Array::cast(lval.obj), Array::cast(rval.obj)))
254 SetField(zone, left, desc, rval);
262 InvokeNative(Zone *zone, Handle<Native> native, void *identity, LocalSlot *args, unsigned argc,
267 if (!zone->stack().pushNativeFrame(native, guard, argc + 1))
270 intptr_t *cells = guard.args();
272 // params[0] == argc, in ISPE
275 // Pass the formal arguments.
276 Local<Type> type(zone, native->type());
277 Local<Type> param(zone);
278 for (unsigned i = 0; i < type->parameters()->length(); i++) {
279 param = type->parameterAt(i);
281 // For floats, we rely on the aliasing of |i32| and |f|.
282 LocalSlot &arg = args[-int(i) - 1];
283 if (param->isInt32() || param->isFloat()) {
284 cells[i + 1] = arg.i32;
285 } else if (param->isReference()) {
286 cells[i + 1] = reinterpret_cast<intptr_t>(arg.ref->location());
288 assert(param->isArray());
289 cells[i + 1] = reinterpret_cast<intptr_t>(arg.array->vec());
293 // For variadic arguments, the compiler left either arrays or references.
294 for (unsigned i = type->parameters()->length(); i < argc; i++) {
295 LocalSlot &arg = args[-int(i) - 1];
297 if (arg.obj->is(MapKind_Reference)) {
298 cells[i + 1] = reinterpret_cast<intptr_t>(arg.ref->location());
300 assert(arg.obj->is(MapKind_Array));
301 cells[i + 1] = reinterpret_cast<intptr_t>(arg.array->vec());
305 typedef intptr_t (*INVOKE)(void *, intptr_t *);
306 INVOKE invoke = (INVOKE)native->function();
307 *result = invoke(identity, cells);
309 return !zone->hasPendingException();
313 ke::Interpret(Zone *zone, Frame *entryFrame, LocalSlot *result)
315 Top &top = *zone->stack().top();
319 LocalSlot *locals = entryFrame->locals();
322 Opcode op = Opcode(*top.pc);
329 PUSH_I32(top, READ_INT32(top.pc));
333 PUSH_F(top, READ_FLOAT(top.pc));
343 if (top.fp == entryFrame)
347 zone->stack().popInlineFrame();
348 locals = top.fp->locals();
353 top.pc += OpcodeInfo[*top.pc].length;
363 #define RELATIONAL_JUMP(op) \
365 int right = POP_I32(top); \
366 int left = POP_I32(top); \
367 if (left op right) { \
368 top.pc += READ_INT32(top.pc); \
397 #undef RELATIONAL_JUMP
401 int value = POP_I32(top);
403 top.pc += READ_INT32(top.pc);
411 int value = POP_I32(top);
413 top.pc += READ_INT32(top.pc);
419 #define RELATIONAL_JUMP_F(op) \
421 float right = POP_F(top); \
422 float left = POP_F(top); \
423 if (left op right) { \
424 top.pc += READ_INT32(top.pc); \
430 RELATIONAL_JUMP_F(<);
434 RELATIONAL_JUMP_F(<=);
438 RELATIONAL_JUMP_F(>);
442 RELATIONAL_JUMP_F(>=);
446 RELATIONAL_JUMP_F(==);
450 RELATIONAL_JUMP_F(!=);
455 float value = POP_F(top);
457 top.pc += READ_INT32(top.pc);
465 float value = POP_F(top);
467 top.pc += READ_INT32(top.pc);
474 top.pc += READ_INT32(top.pc);
479 int slot = READ_INT32(top.pc);
480 *top.sp++ = locals[slot];
486 int slot = READ_INT32(top.pc);
487 locals[slot] = top.sp[-1];
493 top.sp[-2].i32 = top.sp[-2].i32 + top.sp[-1].i32;
500 top.sp[-2].i32 = top.sp[-2].i32 - top.sp[-1].i32;
507 top.sp[-2].i32 = top.sp[-2].i32 * top.sp[-1].i32;
514 if (top.sp[-1].i32 == 0) {
515 zone->reportError(Message_IntegerDivideByZero);
518 top.sp[-2].i32 = top.sp[-2].i32 / top.sp[-1].i32;
525 if (top.sp[-1].i32 == 0) {
526 zone->reportError(Message_IntegerDivideByZero);
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[-2].i32 = top.sp[-2].i32 > top.sp[-1].i32;
557 top.sp[-2].i32 = top.sp[-2].i32 >= top.sp[-1].i32;
564 top.sp[-2].i32 = (top.sp[-2].i32 == top.sp[-1].i32);
571 top.sp[-2].i32 = (top.sp[-2].i32 != top.sp[-1].i32);
578 top.sp[-2].f = top.sp[-2].f + top.sp[-1].f;
585 top.sp[-2].f = top.sp[-2].f - top.sp[-1].f;
592 top.sp[-2].f = top.sp[-2].f * top.sp[-1].f;
599 top.sp[-2].f = top.sp[-2].f / top.sp[-1].f;
606 top.sp[-2].f = top.sp[-2].f < top.sp[-1].f;
613 top.sp[-2].f = top.sp[-2].f <= top.sp[-1].f;
620 top.sp[-2].f = top.sp[-2].f > top.sp[-1].f;
627 top.sp[-2].f = top.sp[-2].f >= top.sp[-1].f;
634 top.sp[-2].i32 = (top.sp[-2].f == top.sp[-1].f);
641 top.sp[-2].i32 = (top.sp[-2].f != top.sp[-1].f);
648 top.sp[-2].i32 = top.sp[-2].i32 << top.sp[-1].i32;
655 top.sp[-2].i32 = top.sp[-2].i32 >> top.sp[-1].i32;
662 top.sp[-2].i32 = unsigned(top.sp[-2].i32) << unsigned(top.sp[-1].i32);
669 top.sp[-2].i32 = top.sp[-2].i32 & top.sp[-1].i32;
676 top.sp[-2].i32 = top.sp[-2].i32 | top.sp[-1].i32;
683 top.sp[-2].i32 = top.sp[-2].i32 ^ top.sp[-1].i32;
690 top.sp[-1].i32 = ~top.sp[-1].i32;
696 top.sp[-1].i32 = top.sp[-1].i32 ? 0 : 1;
702 top.sp[-1].i32 = top.sp[-1].f ? 0 : 1;
708 top.sp[-1].i32 = -top.sp[-1].i32;
714 top.sp[-1].f = -top.sp[-1].f;
720 Local<Function> newFun(zone, top.sp[-1].fun);
723 if (!zone->stack().pushInlineFrame(newFun))
726 locals = top.fp->locals();
732 unsigned slot = READ_UINT32(top.pc);
733 *top.sp++ = top.fp->arg(slot);
739 unsigned slot = READ_UINT32(top.pc);
740 top.fp->arg(slot) = *--top.sp;
746 Array *array = Array::cast(top.sp[-2].obj);
747 int index = top.sp[-1].i32;
749 if (index < 0 || unsigned(index) >= array->length()) {
750 zone->reportError(Message_ArrayIndexOutOfBounds, index, array->length());
754 GetElement(array, index, &top.sp[-2]);
761 Array *array = Array::cast(top.sp[-3].obj);
762 int index = top.sp[-2].i32;
764 if (index < 0 || unsigned(index) >= array->length()) {
765 zone->reportError(Message_ArrayIndexOutOfBounds, index, array->length());
769 SetElement(zone, array, index, top.sp[-1]);
770 top.sp[-3] = top.sp[-1];
777 int ival = POP_I32(top);
778 float fval = (float)ival;
785 Heap::Tenure tenure = (Heap::Tenure)top.pc[1];
786 Local<ArrayMap> map(zone, ArrayMap::cast(top.fp->code()->map(READ_UINT32(top.pc + 1))));
787 Array *array = Array::New(zone, map, map->type()->elements(), tenure);
797 Heap::Tenure tenure = (Heap::Tenure)top.pc[1];
798 Local<StructMap> map(zone, StructMap::cast(top.fp->code()->map(READ_UINT32(top.pc + 1))));
799 top.sp->obj = Struct::New(zone, map, tenure);
808 unsigned slot = READ_UINT32(top.pc);
809 locals[slot].address = zone->heap().lifoPosition();
815 unsigned slot = READ_UINT32(top.pc);
816 zone->heap().unwindLifo(locals[slot].address);
822 unsigned slot = READ_UINT32(top.pc);
823 if (!PushNewRef(zone, top, &locals[slot], Heap::Tenure_Stack))
830 unsigned slot = READ_UINT32(top.pc);
831 if (!PushNewRef(zone, top, &top.fp->arg(slot), Heap::Tenure_Stack))
836 case OP_ARRAYREF_LIFO:
838 Local<Array> array(zone, Array::cast(top.sp[-2].obj));
839 Local<Reference> ref(zone, Reference::NewArray(zone, array, top.sp[-1].i32, Heap::Tenure_Stack));
842 top.sp[-2].ref = ref;
847 case OP_SLOTREF_LIFO:
849 Reference *ref = Reference::NewStack(zone, top.sp[-1], Heap::Tenure_Stack);
852 top.sp[-1].ref = ref;
856 case OP_FIELDREF_LIFO:
858 Local<Struct> obj(zone, Struct::cast(top.sp[-1].obj));
859 top.sp[-1].obj = Reference::NewField(zone, obj, READ_UINT32(top.pc), Heap::Tenure_Stack);
867 top.sp[-1].i32 = top.sp[-1].ref->getInt();
873 top.sp[-2].ref->setInt(top.sp[-1].i32);
874 top.sp[-2].i32 = top.sp[-1].i32;
881 top.sp[-1].f = top.sp[-1].ref->getFloat();
887 top.sp[-2].ref->setFloat(top.sp[-1].f);
888 top.sp[-2].f = top.sp[-1].f;
894 top.sp->obj = top.fp->code()->object(READ_UINT32(top.pc));
901 // Note for this assignment opcode, we return the left-hand side, not the
902 // right, since in this case the result of the expression could be different.
903 if (!CopyArray(zone, top.sp[-2].array, top.sp[-1].array))
912 top.sp[-1] = top.sp[-2];
919 top.sp[-2] = top.sp[-4];
920 top.sp[-1] = top.sp[-3];
926 LocalSlot slot = top.sp[-2];
927 top.sp[-2] = top.sp[-1];
934 LocalSlot slot = top.sp[-3];
935 top.sp[-3] = top.sp[-2];
936 top.sp[-2] = top.sp[-1];
944 top.sp[-1] = top.sp[-3];
951 top.sp[-1] = top.sp[-4];
957 unsigned index = READ_UINT32(top.pc);
958 unsigned argc = READ_UINT32(top.pc + 4);
960 Local<Native> native(zone, Native::cast(top.fp->code()->module()->natives()->at(index)));
961 if (!native->function()) {
962 zone->reportError(Message_NativeNotBound, native->name()->chars());
967 void *nativeIdentity = top.fp->code()->module()->nativeIdentity();
968 if (!InvokeNative(zone, native, nativeIdentity, top.sp, argc, &result))
972 PUSH_I32(top, result);
979 top.sp[-1].module = top.fp->code()->module();
983 case OP_GETMODULE_FN:
985 top.sp[-1].fun = top.sp[-1].module->getFunction(READ_UINT32(top.pc));
991 unsigned slot = READ_UINT32(top.pc);
993 top.sp[-1].i32 = top.fp->code()->module()->environment()->getInt32(slot);
999 unsigned slot = READ_UINT32(top.pc);
1001 top.sp[-1].f = top.fp->code()->module()->environment()->getFloat(slot);
1005 case OP_GETGLOBAL_O:
1007 unsigned slot = READ_UINT32(top.pc);
1009 top.sp[-1].obj = top.fp->code()->module()->environment()->getObject(slot);
1013 case OP_SETGLOBAL_I:
1015 unsigned slot = READ_UINT32(top.pc);
1016 top.fp->code()->module()->environment()->setInt32(slot, top.sp[-1].i32);
1020 case OP_SETGLOBAL_F:
1022 unsigned slot = READ_UINT32(top.pc);
1023 top.fp->code()->module()->environment()->setFloat(slot, top.sp[-1].f);
1027 case OP_SETGLOBAL_O:
1029 unsigned slot = READ_UINT32(top.pc);
1030 top.fp->code()->module()->environment()->setObject(slot, top.sp[-1].obj);
1037 // Because the stack is a union, these are actually no-ops in the
1058 Struct *obj = Struct::cast(top.sp[-1].obj);
1059 GetField(obj, READ_UINT32(top.pc), &top.sp[-1]);
1065 Struct *obj = Struct::cast(top.sp[-2].obj);
1066 SetField(zone, obj, READ_UINT32(top.pc), top.sp[-1]);
1067 top.sp[-2] = top.sp[-1];
1074 if (!CopyStruct(zone, Struct::cast(top.sp[-2].obj), Struct::cast(top.sp[-1].obj)))
1076 top.sp[-2].obj = top.sp[-1].obj;
1087 top.pc += OpcodeInfo[op].length;
1094 // Unwind frames until we can continue.
1095 while (top.fp != entryFrame)
1096 zone->stack().popInlineFrame();