Structure declaration 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/.
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 // Generate an array with completely static sizes.
86 GenerateArray(Zone *zone, Handle<ArrayMap> map, Heap::Tenure tenure)
88 assert(map->type()->elements() != -1);
90 Local<Array> array(zone, Array::New(zone, map, map->type()->elements(), tenure));
94 if (map->type()->levels() == 1)
97 Local<ArrayMap> contained(zone, map->contained());
98 Local<Array> sub(zone);
99 for (unsigned i = 0; i < unsigned(map->type()->elements()); i++) {
100 sub = GenerateArray(zone, contained, tenure);
103 array->writeObject(zone, i, sub);
110 CopyArray(Zone *zone, Array *source, Array *dest)
112 assert(source->type()->levels() == 1);
113 assert(dest->type()->levels() == 1);
114 assert(source->type()->contained()->primitive() ==
115 dest->type()->contained()->primitive());
116 assert(source->nelements() >= dest->nelements());
117 assert(dest->elementSize() == source->elementSize());
119 memcpy(source->vec(), dest->vec(), dest->nelements() * dest->elementSize());
124 InvokeNative(Zone *zone, Handle<Native> native, void *identity, LocalSlot *args, unsigned argc,
129 if (!zone->stack().pushNativeFrame(native, guard, argc + 1))
132 intptr_t *cells = guard.args();
134 // params[0] == argc, in ISPE
137 // Pass the formal arguments.
138 Local<Type> type(zone, native->type());
139 Local<Type> param(zone);
140 for (unsigned i = 0; i < type->parameters()->length(); i++) {
141 param = type->parameterAt(i);
143 // For floats, we rely on the aliasing of |i32| and |f|.
144 LocalSlot &arg = args[-int(i) - 1];
145 if (param->isInt32() || param->isFloat()) {
146 cells[i + 1] = arg.i32;
147 } else if (param->isReference()) {
148 cells[i + 1] = reinterpret_cast<intptr_t>(arg.ref->location());
150 assert(param->isArray());
151 cells[i + 1] = reinterpret_cast<intptr_t>(arg.array->vec());
155 // For variadic arguments, the compiler left either arrays or references.
156 for (unsigned i = type->parameters()->length(); i < argc; i++) {
157 LocalSlot &arg = args[-int(i) - 1];
159 if (arg.obj->is(MapKind_Reference)) {
160 cells[i + 1] = reinterpret_cast<intptr_t>(arg.ref->location());
162 assert(arg.obj->is(MapKind_Array));
163 cells[i + 1] = reinterpret_cast<intptr_t>(arg.array->vec());
167 typedef intptr_t (*INVOKE)(void *, intptr_t *);
168 INVOKE invoke = (INVOKE)native->function();
169 *result = invoke(identity, cells);
171 return !zone->hasPendingException();
175 ke::Interpret(Zone *zone, Frame *entryFrame, LocalSlot *result)
177 Top &top = *zone->stack().top();
181 LocalSlot *locals = entryFrame->locals();
184 Opcode op = Opcode(*top.pc);
191 PUSH_I32(top, READ_INT32(top.pc));
195 PUSH_F(top, READ_FLOAT(top.pc));
205 if (top.fp == entryFrame)
209 zone->stack().popInlineFrame();
210 locals = top.fp->locals();
215 top.pc += OpcodeInfo[*top.pc].length;
225 #define RELATIONAL_JUMP(op) \
227 int right = POP_I32(top); \
228 int left = POP_I32(top); \
229 if (left op right) { \
230 top.pc += READ_INT32(top.pc); \
259 #undef RELATIONAL_JUMP
263 int value = POP_I32(top);
265 top.pc += READ_INT32(top.pc);
273 int value = POP_I32(top);
275 top.pc += READ_INT32(top.pc);
281 #define RELATIONAL_JUMP_F(op) \
283 float right = POP_F(top); \
284 float left = POP_F(top); \
285 if (left op right) { \
286 top.pc += READ_INT32(top.pc); \
292 RELATIONAL_JUMP_F(<);
296 RELATIONAL_JUMP_F(<=);
300 RELATIONAL_JUMP_F(>);
304 RELATIONAL_JUMP_F(>=);
308 RELATIONAL_JUMP_F(==);
312 RELATIONAL_JUMP_F(!=);
317 float value = POP_F(top);
319 top.pc += READ_INT32(top.pc);
327 float value = POP_F(top);
329 top.pc += READ_INT32(top.pc);
336 top.pc += READ_INT32(top.pc);
341 int slot = READ_INT32(top.pc);
342 *top.sp++ = locals[slot];
348 int slot = READ_INT32(top.pc);
349 locals[slot] = top.sp[-1];
355 top.sp[-2].i32 = top.sp[-2].i32 + top.sp[-1].i32;
362 top.sp[-2].i32 = top.sp[-2].i32 - top.sp[-1].i32;
369 top.sp[-2].i32 = top.sp[-2].i32 * top.sp[-1].i32;
376 if (top.sp[-1].i32 == 0) {
377 zone->reportError(Message_IntegerDivideByZero);
380 top.sp[-2].i32 = top.sp[-2].i32 / top.sp[-1].i32;
387 if (top.sp[-1].i32 == 0) {
388 zone->reportError(Message_IntegerDivideByZero);
391 top.sp[-2].i32 = top.sp[-2].i32 % top.sp[-1].i32;
398 top.sp[-2].i32 = top.sp[-2].i32 < top.sp[-1].i32;
405 top.sp[-2].i32 = top.sp[-2].i32 <= top.sp[-1].i32;
412 top.sp[-2].i32 = top.sp[-2].i32 > top.sp[-1].i32;
419 top.sp[-2].i32 = top.sp[-2].i32 >= top.sp[-1].i32;
426 top.sp[-2].i32 = (top.sp[-2].i32 == top.sp[-1].i32);
433 top.sp[-2].i32 = (top.sp[-2].i32 != top.sp[-1].i32);
440 top.sp[-2].f = top.sp[-2].f + top.sp[-1].f;
447 top.sp[-2].f = top.sp[-2].f - top.sp[-1].f;
454 top.sp[-2].f = top.sp[-2].f * top.sp[-1].f;
461 top.sp[-2].f = top.sp[-2].f / top.sp[-1].f;
468 top.sp[-2].f = top.sp[-2].f < top.sp[-1].f;
475 top.sp[-2].f = top.sp[-2].f <= top.sp[-1].f;
482 top.sp[-2].f = top.sp[-2].f > top.sp[-1].f;
489 top.sp[-2].f = top.sp[-2].f >= top.sp[-1].f;
496 top.sp[-2].i32 = (top.sp[-2].f == top.sp[-1].f);
503 top.sp[-2].i32 = (top.sp[-2].f != top.sp[-1].f);
510 top.sp[-2].i32 = top.sp[-2].i32 << top.sp[-1].i32;
517 top.sp[-2].i32 = top.sp[-2].i32 >> top.sp[-1].i32;
524 top.sp[-2].i32 = unsigned(top.sp[-2].i32) << unsigned(top.sp[-1].i32);
531 top.sp[-2].i32 = top.sp[-2].i32 & top.sp[-1].i32;
538 top.sp[-2].i32 = top.sp[-2].i32 | top.sp[-1].i32;
545 top.sp[-2].i32 = top.sp[-2].i32 ^ top.sp[-1].i32;
552 top.sp[-1].i32 = ~top.sp[-1].i32;
558 top.sp[-1].i32 = top.sp[-1].i32 ? 0 : 1;
564 top.sp[-1].i32 = top.sp[-1].f ? 0 : 1;
570 top.sp[-1].i32 = -top.sp[-1].i32;
576 top.sp[-1].f = -top.sp[-1].f;
582 Local<Function> newFun(zone, top.sp[-1].fun);
585 if (!zone->stack().pushInlineFrame(newFun))
588 locals = top.fp->locals();
594 unsigned slot = READ_UINT32(top.pc);
595 *top.sp++ = top.fp->arg(slot);
601 unsigned slot = READ_UINT32(top.pc);
602 top.fp->arg(slot) = *--top.sp;
608 Array *array = top.sp[-2].array;
609 int index = top.sp[-1].i32;
610 if (index < 0 || unsigned(index) >= array->nelements()) {
611 zone->reportError(Message_ArrayIndexOutOfBounds, index, array->nelements());
619 Array *array = top.sp[-2].array;
620 int index = top.sp[-1].i32;
621 top.sp[-2].i32 = array->readInt32(index);
628 Array *array = top.sp[-2].array;
629 int index = top.sp[-1].i32;
630 top.sp[-2].f = array->readFloat(index);
637 Array *array = top.sp[-2].array;
638 int index = top.sp[-1].i32;
639 top.sp[-2].i32 = array->readChar(index);
646 Array *array = top.sp[-3].array;
647 int index = top.sp[-2].i32;
648 array->writeInt32(index, top.sp[-1].i32);
649 top.sp[-3] = top.sp[-1];
656 Array *array = top.sp[-3].array;
657 int index = top.sp[-2].i32;
658 array->writeFloat(index, top.sp[-1].f);
659 top.sp[-3] = top.sp[-1];
666 Array *array = top.sp[-3].array;
667 int index = top.sp[-2].i32;
668 array->writeChar(index, top.sp[-1].i32);
669 top.sp[-3] = top.sp[-1];
676 Array *array = top.sp[-2].array;
677 int index = top.sp[-1].i32;
678 top.sp[-2].array = Array::cast(array->readObject(index));
685 int ival = POP_I32(top);
686 float fval = (float)ival;
693 Heap::Tenure tenure = (Heap::Tenure)top.pc[1];
694 Local<ArrayMap> map(zone, ArrayMap::cast(top.fp->code()->map(READ_UINT32(top.pc + 1))));
695 Array *array = GenerateArray(zone, map, tenure);
705 Heap::Tenure tenure = (Heap::Tenure)top.pc[1];
706 Local<StructMap> map(zone, StructMap::cast(top.fp->code()->map(READ_UINT32(top.pc + 1))));
707 top.sp->obj = Struct::New(zone, map, tenure);
716 unsigned slot = READ_UINT32(top.pc);
717 locals[slot].address = zone->heap().lifoPosition();
723 unsigned slot = READ_UINT32(top.pc);
724 zone->heap().unwindLifo(locals[slot].address);
730 unsigned slot = READ_UINT32(top.pc);
731 if (!PushNewRef(zone, top, &locals[slot], Heap::Tenure_Stack))
738 unsigned slot = READ_UINT32(top.pc);
739 if (!PushNewRef(zone, top, &top.fp->arg(slot), Heap::Tenure_Stack))
744 case OP_ARRAYREF_LIFO:
746 Local<Array> array(zone, top.sp[-2].array);
747 Local<Reference> ref(zone, Reference::NewArray(zone, array, top.sp[-1].i32, Heap::Tenure_Stack));
750 top.sp[-2].ref = ref;
755 case OP_SLOTREF_LIFO:
757 Reference *ref = Reference::NewStack(zone, top.sp[-1], Heap::Tenure_Stack);
760 top.sp[-1].ref = ref;
766 top.sp[-1].i32 = top.sp[-1].ref->getInt();
772 top.sp[-2].ref->setInt(top.sp[-1].i32);
773 top.sp[-2].i32 = top.sp[-1].i32;
780 top.sp[-1].f = top.sp[-1].ref->getFloat();
786 top.sp[-2].ref->setFloat(top.sp[-1].f);
787 top.sp[-2].f = top.sp[-1].f;
793 top.sp->obj = top.fp->code()->object(READ_UINT32(top.pc));
800 // Note for this assignment opcode, we return the left-hand side, not the
801 // right, since in this case the result of the expression could be different.
802 if (!CopyArray(zone, top.sp[-2].array, top.sp[-1].array))
811 top.sp[-1] = top.sp[-2];
818 top.sp[-2] = top.sp[-4];
819 top.sp[-1] = top.sp[-3];
825 LocalSlot slot = top.sp[-2];
826 top.sp[-2] = top.sp[-1];
833 LocalSlot slot = top.sp[-3];
834 top.sp[-3] = top.sp[-2];
835 top.sp[-2] = top.sp[-1];
843 top.sp[-1] = top.sp[-3];
850 top.sp[-1] = top.sp[-4];
856 unsigned index = READ_UINT32(top.pc);
857 unsigned argc = READ_UINT32(top.pc + 4);
859 Local<Native> native(zone, Native::cast(top.fp->code()->module()->natives()->at(index)));
860 if (!native->function()) {
861 zone->reportError(Message_NativeNotBound, native->name()->chars());
866 void *nativeIdentity = top.fp->code()->module()->nativeIdentity();
867 if (!InvokeNative(zone, native, nativeIdentity, top.sp, argc, &result))
871 PUSH_I32(top, result);
878 top.sp[-1].module = top.fp->code()->module();
882 case OP_GETMODULE_FN:
884 top.sp[-1].fun = top.sp[-1].module->getFunction(READ_UINT32(top.pc));
890 unsigned slot = READ_UINT32(top.pc);
892 top.sp[-1].i32 = top.fp->code()->module()->environment()->getInt32(slot);
898 unsigned slot = READ_UINT32(top.pc);
900 top.sp[-1].f = top.fp->code()->module()->environment()->getFloat(slot);
906 unsigned slot = READ_UINT32(top.pc);
908 top.sp[-1].obj = top.fp->code()->module()->environment()->getObject(slot);
914 unsigned slot = READ_UINT32(top.pc);
915 top.fp->code()->module()->environment()->setInt32(slot, top.sp[-1].i32);
921 unsigned slot = READ_UINT32(top.pc);
922 top.fp->code()->module()->environment()->setFloat(slot, top.sp[-1].f);
928 unsigned slot = READ_UINT32(top.pc);
929 top.fp->code()->module()->environment()->setObject(slot, top.sp[-1].obj);
936 // Because the stack is a union, these are actually no-ops in the
961 top.pc += OpcodeInfo[op].length;
968 // Unwind frames until we can continue.
969 while (top.fp != entryFrame)
970 zone->stack().popInlineFrame();