#include #include #include #include #include #include "utils/Functions.h" #include "vm/Operation.h" #include "vm/Script.h" void sError(Script* sc, const char* format, ...) { va_list args; va_start(args, format); vsnprintf(sc->error, SCRIPT_ERROR_SIZE, format, args); va_end(args); } static void sCannotSetValueType(Script* sc) { sError(sc, "cannot set value type"); } static bool sRead(Script* sc, void* buffer, int length) { if(sc->readIndex + length > sc->code->length) { sError(sc, "cannot read expected %d bytes of data from bytecode", length); return true; } memcpy(buffer, sc->code->code + sc->readIndex, (size_t)length); sc->readIndex += length; return false; } static Operation sReadOperation(Script* sc) { unsigned char c; if(sRead(sc, &c, 1)) { return OP_NOTHING; } return (Operation)c; } static Value* sPeekStack(Script* sc, ValueType type) { if(sc->stackIndex <= 0) { sError(sc, "stack underflow"); return NULL; } ValueType stackType = vGetType(sc->stack[sc->stackIndex - 1]); if(stackType != type) { sError(sc, "expected %s on stack but got %s", vtGetName(type), vtGetName(stackType)); return NULL; } return sc->stack + (sc->stackIndex - 1); } bool sPopInt32(Script* sc, int32* i) { Value* v = sPeekStack(sc, VT_INT); if(v == NULL) { return true; } *i = v->data.intValue; sc->stackIndex--; return false; } bool sPopFloat(Script* sc, float* f) { Value* v = sPeekStack(sc, VT_FLOAT); if(v == NULL) { return true; } *f = v->data.floatValue; sc->stackIndex--; return false; } static bool sReadInt32(Script* sc, int32* i) { return sRead(sc, i, sizeof(int32)); } static bool sReadFloat(Script* sc, float* f) { return sRead(sc, f, sizeof(float)); } static Value* sPushStack(Script* sc, int values) { if(sc->stackIndex + values > SCRIPT_STACK_SIZE) { sError(sc, "stack overflow"); return NULL; } Value* v = sc->stack + sc->stackIndex; for(int i = 0; i < values; i++) { if(vSetType(v + i, VT_NOT_SET)) { sCannotSetValueType(sc); return NULL; } } sc->stackIndex += values; return v; } bool sPushInt32(Script* sc, int32 i) { Value* v = sPushStack(sc, 1); if(vSetType(v, VT_INT)) { sCannotSetValueType(sc); return true; } v->data.intValue = i; return false; } bool sPushFloat(Script* sc, float f) { Value* v = sPushStack(sc, 1); if(vSetType(v, VT_FLOAT)) { sCannotSetValueType(sc); return true; } v->data.floatValue = f; return false; } static void sPushInt32Value(Script* sc) { int32 value = 0; if(!sReadInt32(sc, &value)) { sPushInt32(sc, value); } } static void sPushFloatValue(Script* sc) { float value = 0; if(!sReadFloat(sc, &value)) { sPushFloat(sc, value); } } #define ZERO_CHECK(name) \ if(values[0] == 0) { \ sError(sc, name " by 0"); \ return; \ } #define OP_BASE(type, Type, RType, op, check) \ { \ type values[2]; \ if(!sPop##Type(sc, values) && !sPop##Type(sc, values + 1)) { \ check; \ sPush##RType(sc, values[1] op values[0]); \ } \ } #define CHECKED_NUMBER_OP(type, Type, op, check) \ OP_BASE(type, Type, Type, op, check) #define NUMBER_OP(type, Type, op) CHECKED_NUMBER_OP(type, Type, op, ) #define BOOL_OP(type, Type, op) OP_BASE(type, Type, Int32, op, ) #define DIVISION(type, Type) \ CHECKED_NUMBER_OP(type, Type, /, ZERO_CHECK("division")); #define MODULE(type, Type) \ CHECKED_NUMBER_OP(type, Type, %, ZERO_CHECK("module")); #define INVERT_SIGN(type, Type) \ { \ type value = 0; \ if(!sPop##Type(sc, &value)) { \ sPush##Type(sc, -value); \ } \ } static void sReserveBytes(Script* sc) { int32 values = 0; int32 offset = 0; if(sReadInt32(sc, &values) || sReadInt32(sc, &offset)) { return; } int32 oldIndex = sc->stackVarIndex; sc->stackVarIndex = sc->stackIndex - offset; sPushStack(sc, values - offset); sPushInt32(sc, oldIndex); } static void sGlobalReserveBytes(Script* sc) { int32 values = 0; if(sReadInt32(sc, &values)) { return; } sc->stackVarIndex = sc->stackIndex; if(values >= 0) { sPushStack(sc, values); } else { sc->stackIndex += values; if(sc->stackIndex < 0) { sError(sc, "invalid global free %d", values); } } } static void sNot(Script* sc) { int32 value = 0; if(!sPopInt32(sc, &value)) { sPushInt32(sc, !value); } } static void sBitNotInt32(Script* sc) { int32 value = 0; if(!sPopInt32(sc, &value)) { sPushInt32(sc, ~value); } } static void sLine(Script* sc) { sRead(sc, &sc->line, 2); } static void sGoTo(Script* sc) { int32 gotoIndex; if(!sReadInt32(sc, &gotoIndex)) { sc->readIndex = gotoIndex; } } static void sGoSub(Script* sc) { int32 gotoIndex; int32 offset; if(sReadInt32(sc, &gotoIndex) || sReadInt32(sc, &offset)) { return; } int index = sc->stackIndex - offset - 1; if(index < 0 || index >= SCRIPT_STACK_SIZE) { sError(sc, "invalid gosub offset"); return; } else if(vGetType(sc->stack[index]) != VT_INT) { sError(sc, "gosub expects an int got %s", vtGetName(vGetType(sc->stack[index]))); return; } sc->stack[index].data.intValue = sc->readIndex; sc->readIndex = gotoIndex; } static void sReturn(Script* sc) { int32 values = 0; int32 varIndex = 0; if(sReadInt32(sc, &values) || sPopInt32(sc, &varIndex)) { return; } sc->stackVarIndex = varIndex; if(sc->stackIndex < values) { sError(sc, "invalid return index"); return; } sc->stackIndex -= values; if(sPopInt32(sc, &sc->readIndex) || sc->readIndex < 0) { sError(sc, "read index is corrupt"); } } static void sReturnPointer(Script* sc) { Value* v = sPeekStack(sc, VT_ARRAY); if(v == NULL) { return; } sc->stackIndex--; sReturn(sc); Value* p = sPushStack(sc, 1); if(p != NULL) { *p = *v; } } #define RETURN(type, Type) \ { \ type value; \ if(!sPop##Type(sc, &value)) { \ sReturn(sc); \ sPush##Type(sc, value); \ } \ } static void sIfGoTo(Script* sc) { int32 gotoIndex = 0; int32 value = false; if(!sReadInt32(sc, &gotoIndex) && !sPopInt32(sc, &value) && !value) { sc->readIndex = gotoIndex; } } static void sPeekFalseGoTo(Script* sc) { int32 gotoIndex = 0; if(sReadInt32(sc, &gotoIndex)) { return; } Value* v = sPeekStack(sc, VT_INT); if(v != NULL && v->data.intValue == 0) { sc->readIndex = gotoIndex; } } static void sPeekTrueGoTo(Script* sc) { int32 gotoIndex = 0; if(sReadInt32(sc, &gotoIndex)) { return; } Value* v = sPeekStack(sc, VT_INT); if(v != NULL && v->data.intValue != 0) { sc->readIndex = gotoIndex; } } static void sNewArray(Script* sc) { int32 length = 0; int32 size = 0; if(sReadInt32(sc, &size) || sPopInt32(sc, &length)) { return; } Value* p = sPushStack(sc, 1); if(vSetType(p, VT_ARRAY) || vSetOffset(p, 0)) { sCannotSetValueType(sc); return; } p->data.intValue = asAllocate(&sc->arrays, size, length); if(p->data.intValue == -1) { sError(sc, "out of memory"); } else if(p->data.intValue == -2) { sError(sc, "bad allocation"); } } static void sLength(Script* sc) { Value* v = sPeekStack(sc, VT_ARRAY); if(v == NULL) { return; } sc->stackIndex--; SnuviArray* a = asGet(&sc->arrays, v->data.intValue); if(a == NULL) { sError(sc, "invalid heap pointer %d", v->data.intValue); return; } sPushInt32(sc, a->length); } static void sDereference(Script* sc) { int32 address = 0; if(sReadInt32(sc, &address)) { return; } Value* v = sPushStack(sc, 1); if(vSetType(v, VT_POINTER) || vSetOffset(v, address + sc->stackVarIndex)) { sCannotSetValueType(sc); return; } v->data.intValue = -1; } static void sGlobalDereference(Script* sc) { int32 address = 0; if(sReadInt32(sc, &address)) { return; } Value* v = sPushStack(sc, 1); if(vSetType(v, VT_POINTER) || vSetOffset(v, address)) { sCannotSetValueType(sc); return; } v->data.intValue = -1; } static void sDuplicateReference(Script* sc) { Value* v = sPeekStack(sc, VT_POINTER); if(v != NULL) { Value* copy = sPushStack(sc, 1); if(copy != NULL) { *copy = *v; } } } static void sAddReference(Script* sc) { int32 size = 0; int32 add = 0; if(sReadInt32(sc, &size) || sPopInt32(sc, &add)) { return; } Value* v = sPeekStack(sc, VT_POINTER); if(v != NULL) { if(vSetOffset(v, vGetOffset(*v) + add * size)) { sCannotSetValueType(sc); } } else { sc->error[0] = '\0'; v = sPeekStack(sc, VT_ARRAY); if(vSetType(v, VT_POINTER) || vSetOffset(v, vGetOffset(*v) + add * size)) { sCannotSetValueType(sc); } } } static void sPushStructReference(Script* sc) { int32 address = 0; if(sReadInt32(sc, &address)) { return; } Value* v = sPushStack(sc, 1); if(v != NULL) { int index = address + sc->stackVarIndex; if(index < 0 || index >= SCRIPT_STACK_SIZE) { sError(sc, "invalid struct reference address"); return; } *v = sc->stack[index]; } } static Value* sLoadFromPointer(Script* sc, Value* p, ValueType wantedType) { int32 offset = vGetOffset(*p); if(p->data.intValue < 0) { if(offset < 0 || offset >= SCRIPT_STACK_SIZE) { sError(sc, "invalid offset"); return NULL; } Value* v = sc->stack + offset; ValueType type = vGetType(*v); if(type != wantedType && type != VT_NOT_SET) { sError(sc, "pointer did not point to %s but %s", vtGetName(type), vtGetName(type)); return NULL; } return v; } SnuviArray* a = asGet(&sc->arrays, p->data.intValue); if(a == NULL) { sError(sc, "invalid heap pointer %d", p->data.intValue); return NULL; } if(offset >= a->realLength) { sError(sc, "invalid heap pointer offset %d %d", offset, a->realLength); return NULL; } return a->data + offset; } SnuviArray* sGetArray(Script* sc) { Value* v = sPeekStack(sc, VT_ARRAY); if(v == NULL) { return NULL; } sc->stackIndex--; return asGet(&sc->arrays, v->data.intValue); } Value* sPopStructPointer(Script* sc, ValueType type) { Value* v = sPeekStack(sc, VT_POINTER); if(v == NULL) { return NULL; } sc->stackIndex--; return sLoadFromPointer(sc, v, type); } static void sStoreInt32(Script* sc) { int32 i = 0; if(sPopInt32(sc, &i)) { return; } Value* v = sPeekStack(sc, VT_POINTER); if(v == NULL) { return; } sc->stackIndex--; v = sLoadFromPointer(sc, v, VT_INT); if(vSetType(v, VT_INT)) { sCannotSetValueType(sc); return; } v->data.intValue = i; } static void sStoreFloat(Script* sc) { float f = 0; if(sPopFloat(sc, &f)) { return; } Value* v = sPeekStack(sc, VT_POINTER); if(v == NULL) { return; } sc->stackIndex--; v = sLoadFromPointer(sc, v, VT_FLOAT); if(vSetType(v, VT_FLOAT)) { sCannotSetValueType(sc); return; } v->data.floatValue = f; } static void sStoreArray(Script* sc) { Value* array = sPeekStack(sc, VT_ARRAY); if(array == NULL) { return; } sc->stackIndex--; Value* v = sPeekStack(sc, VT_POINTER); if(v == NULL) { return; } sc->stackIndex--; v = sLoadFromPointer(sc, v, VT_ARRAY); if(v != NULL) { *v = *array; } } static void sLoadInt32(Script* sc) { Value* v = sPeekStack(sc, VT_POINTER); if(v == NULL) { return; } sc->stackIndex--; v = sLoadFromPointer(sc, v, VT_INT); if(v != NULL) { sPushInt32(sc, v->data.intValue); } } static void sLoadFloat(Script* sc) { Value* v = sPeekStack(sc, VT_POINTER); if(v == NULL) { return; } sc->stackIndex--; v = sLoadFromPointer(sc, v, VT_FLOAT); if(v != NULL) { sPushFloat(sc, v->data.floatValue); } } static void sLoadArray(Script* sc) { Value* v = sPeekStack(sc, VT_POINTER); if(v == NULL) { return; } sc->stackIndex--; v = sLoadFromPointer(sc, v, VT_ARRAY); if(v != NULL) { Value* array = sPushStack(sc, 1); if(array != NULL) { *array = *v; } } } static void sEqualArrays(Script* sc, bool wanted) { Value* a = sPeekStack(sc, VT_ARRAY); if(a == NULL) { return; } sc->stackIndex--; Value* b = sPeekStack(sc, VT_ARRAY); if(b == NULL) { return; } sc->stackIndex--; sPushInt32(sc, (vGetOffset(*a) == vGetOffset(*b) && a->data.intValue == b->data.intValue) == wanted); } static void sCall(Script* sc) { int32 function = 0; if(sReadInt32(sc, &function)) { return; } if(gfsCall(sc, function)) { sError(sc, "invalid function call"); } } static void sPushText(Script* sc) { int32 length; if(sReadInt32(sc, &length)) { return; } if(length < 0 || length > 65535) { sError(sc, "too large string"); return; } Value* p = sPushStack(sc, 1); if(vSetType(p, VT_ARRAY) || vSetOffset(p, 0)) { sCannotSetValueType(sc); return; } p->data.intValue = asAllocate(&sc->arrays, 1, length); if(p->data.intValue == -1) { sError(sc, "out of memory"); return; } else if(p->data.intValue == -2) { sError(sc, "bad allocation"); return; } SnuviArray* a = asGet(&sc->arrays, p->data.intValue); if(a == NULL) { sError(sc, "cannot find text array"); return; } for(int i = 0; i < length; i++) { sReadInt32(sc, &(a->data[i].data.intValue)); if(vSetType(a->data + i, VT_INT)) { sCannotSetValueType(sc); return; } } } static Value* sChangeBase(Script* sc, char* c) { if(sRead(sc, c, sizeof(char))) { return NULL; } Value* p = sPeekStack(sc, VT_POINTER); if(p == NULL) { return NULL; } sc->stackIndex--; return sLoadFromPointer(sc, p, VT_INT); } static void sChange(Script* sc) { char c; Value* v = sChangeBase(sc, &c); if(v != NULL) { v->data.intValue += c; } } static void sPushBeforeChange(Script* sc) { char c; Value* v = sChangeBase(sc, &c); if(v != NULL) { sPushInt32(sc, v->data.intValue); v->data.intValue += c; } } static void sPushAfterChange(Script* sc) { char c; Value* v = sChangeBase(sc, &c); if(v != NULL) { v->data.intValue += c; sPushInt32(sc, v->data.intValue); } } static void sFloatToInt32(Script* sc) { Value* v = sPeekStack(sc, VT_FLOAT); if(vSetType(v, VT_INT)) { sCannotSetValueType(sc); return; } v->data.intValue = (int)v->data.floatValue; } static void sInt32ToFloat(Script* sc) { Value* v = sPeekStack(sc, VT_INT); if(vSetType(v, VT_FLOAT)) { sCannotSetValueType(sc); return; } v->data.floatValue = (float)v->data.intValue; } #define CASE_NUMBER_OP(name, op) \ case OP_##name##_INT: NUMBER_OP(int32, Int32, op); return; \ case OP_##name##_FLOAT: \ NUMBER_OP(float, Float, op); \ return; #define CASE_BOOL_OP(name, op) \ case OP_##name##_INT: BOOL_OP(int32, Int32, op); return; \ case OP_##name##_FLOAT: \ BOOL_OP(float, Float, op); \ return; #define CASE_TYPE(TYPE, Type, type) \ case OP_RETURN_##TYPE: RETURN(type, Type); return; \ case OP_EQUAL_##TYPE: BOOL_OP(type, Type, ==); return; \ case OP_NOT_EQUAL_##TYPE: BOOL_OP(type, Type, !=); return; static void sConsumeInstruction(Script* sc) { Operation op = sReadOperation(sc); switch(op) { CASE_NUMBER_OP(ADD, +); CASE_NUMBER_OP(SUB, -); CASE_NUMBER_OP(MUL, *); CASE_BOOL_OP(LESS, <); CASE_BOOL_OP(LESS_EQUAL, <=); CASE_BOOL_OP(GREATER, >); CASE_BOOL_OP(GREATER_EQUAL, >=); CASE_TYPE(INT, Int32, int32); #pragma GCC diagnostic push #pragma GCC diagnostic ignored "-Wfloat-equal" CASE_TYPE(FLOAT, Float, float); case OP_DIV_FLOAT: DIVISION(float, Float); return; #pragma GCC diagnostic pop case OP_DIV_INT: DIVISION(int32, Int32); return; case OP_STORE_INT: sStoreInt32(sc); return; case OP_STORE_FLOAT: sStoreFloat(sc); return; case OP_LOAD_INT: sLoadInt32(sc); return; case OP_LOAD_FLOAT: sLoadFloat(sc); return; case OP_PUSH_PRE_CHANGE_INT: sPushAfterChange(sc); return; case OP_PUSH_POST_CHANGE_INT: sPushBeforeChange(sc); return; case OP_CHANGE_INT: sChange(sc); return; case OP_FLOAT_TO_INT: sFloatToInt32(sc); return; case OP_INT_TO_FLOAT: sInt32ToFloat(sc); return; case OP_BIT_AND_INT: NUMBER_OP(int32, Int32, &); return; case OP_BIT_OR_INT: NUMBER_OP(int32, Int32, |); return; case OP_BIT_XOR_INT: NUMBER_OP(int32, Int32, ^); return; case OP_LEFT_SHIFT_INT: NUMBER_OP(int32, Int32, <<); return; case OP_RIGHT_SHIFT_INT: NUMBER_OP(int32, Int32, >>); return; case OP_NOTHING: return; case OP_PUSH_INT: sPushInt32Value(sc); return; case OP_PUSH_FLOAT: sPushFloatValue(sc); return; case OP_PUSH_TEXT: sPushText(sc); return; case OP_MOD_INT: MODULE(int32, Int32); return; case OP_INVERT_SIGN_INT: INVERT_SIGN(int32, Int32); return; case OP_INVERT_SIGN_FLOAT: INVERT_SIGN(float, Float); return; case OP_NOT: sNot(sc); return; case OP_AND: BOOL_OP(int32, Int32, &&); return; case OP_OR: BOOL_OP(int32, Int32, ||); return; case OP_BIT_NOT_INT: sBitNotInt32(sc); return; case OP_LINE: sLine(sc); return; case OP_GOTO: sGoTo(sc); return; case OP_IF_GOTO: sIfGoTo(sc); return; case OP_PEEK_FALSE_GOTO: sPeekFalseGoTo(sc); return; case OP_PEEK_TRUE_GOTO: sPeekTrueGoTo(sc); return; case OP_GOSUB: sGoSub(sc); return; case OP_RETURN: sReturn(sc); return; case OP_RETURN_POINTER: sReturnPointer(sc); return; case OP_RESERVE: sReserveBytes(sc); return; case OP_GRESERVE: sGlobalReserveBytes(sc); return; case OP_DEREFERENCE_VAR: sDereference(sc); return; case OP_DEREFERENCE_GVAR: sGlobalDereference(sc); return; case OP_LOAD_ARRAY: sLoadArray(sc); return; case OP_DUPLICATE_REFERENCE: sDuplicateReference(sc); return; case OP_ADD_REFERENCE: sAddReference(sc); return; case OP_PUSH_STRUCT_REFERENCE: sPushStructReference(sc); return; case OP_NEW: sNewArray(sc); return; case OP_LENGTH: sLength(sc); return; case OP_STORE_ARRAY: sStoreArray(sc); return; case OP_EQUAL_POINTER: sEqualArrays(sc, true); return; case OP_NOT_EQUAL_POINTER: sEqualArrays(sc, false); return; case OP_CALL: sCall(sc); return; } sError(sc, "unknown operation %d", (int)op); } static bool sHasData(Script* sc) { return sc->readIndex < sc->code->length; } Script* sInit(ByteCode* code) { Script* sc = (Script*)malloc(sizeof(Script)); sc->error[0] = '\0'; sc->code = code; sc->readIndex = 0; sc->stackIndex = 0; sc->stackVarIndex = 0; sc->line = 0; asInit(&sc->arrays); return sc; } void sDelete(Script* sc) { bcDelete(sc->code); asDelete(&sc->arrays); free(sc); } void sRun(Script* sc) { while(sHasData(sc)) { sConsumeInstruction(sc); if(sc->error[0] != '\0') { return; } } }