optimise
This commit is contained in:
@@ -1,6 +1,33 @@
|
||||
#include "../../../include/ground.h"
|
||||
#include <stdint.h>
|
||||
#include <inttypes.h>
|
||||
#include <string.h>
|
||||
|
||||
#define HEAP_GET(heap, idx) (&(heap)->heap[(idx)])
|
||||
#define HEAP_SET(heap, idx, val) ((heap)->heap[(idx)] = (val))
|
||||
|
||||
static void printValue(GroundValue* val) {
|
||||
switch (val->type.type) {
|
||||
case GroundType_Int:
|
||||
printf("%" PRId64, val->as.Int);
|
||||
break;
|
||||
case GroundType_Double:
|
||||
printf("%f", val->as.Double);
|
||||
break;
|
||||
case GroundType_Bool:
|
||||
printf(val->as.Bool ? "true" : "false");
|
||||
break;
|
||||
case GroundType_Char:
|
||||
printf("%c", val->as.Char);
|
||||
break;
|
||||
case GroundType_String:
|
||||
printf("%s", val->as.String.cstr);
|
||||
break;
|
||||
default:
|
||||
printf("<fixme>");
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
int64_t _GroundBytecodeInstructionExecute(GroundBytecodeInstruction* instruction, GroundBytecodeHeap* heap) {
|
||||
|
||||
@@ -20,11 +47,10 @@ int64_t _GroundBytecodeInstructionExecute(GroundBytecodeInstruction* instruction
|
||||
&&PAUSE, &&DROP, &&LICENSE, &&ERRORCMD, &&THROW, &&CATCH
|
||||
};
|
||||
|
||||
// Jump to the spot
|
||||
goto *jumpTable[instruction->type];
|
||||
|
||||
IF: {
|
||||
GroundValue* cond = Ground.Bytecode.Heap.get(heap, instruction->args.at[0]);
|
||||
GroundValue* cond = HEAP_GET(heap, instruction->args.at[0]);
|
||||
if (cond->as.Bool) {
|
||||
return instruction->args.at[1];
|
||||
}
|
||||
@@ -41,69 +67,19 @@ int64_t _GroundBytecodeInstructionExecute(GroundBytecodeInstruction* instruction
|
||||
}
|
||||
PRINT: {
|
||||
for (GroundSize i = 0; i < instruction->args.len; i++) {
|
||||
GroundValue* val = Ground.Bytecode.Heap.get(heap, instruction->args.at[i]);
|
||||
switch (val->type.type) {
|
||||
case GroundType_Int:
|
||||
printf("%" PRId64, val->as.Int);
|
||||
break;
|
||||
|
||||
case GroundType_Double:
|
||||
printf("%f", val->as.Double);
|
||||
break;
|
||||
|
||||
case GroundType_Bool:
|
||||
printf(val->as.Bool ? "true" : "false");
|
||||
break;
|
||||
|
||||
case GroundType_Char:
|
||||
printf("%c", val->as.Char);
|
||||
break;
|
||||
|
||||
case GroundType_String:
|
||||
printf("%s", val->as.String.cstr);
|
||||
break;
|
||||
|
||||
default:
|
||||
printf("<fixme>");
|
||||
break;
|
||||
}
|
||||
printValue(HEAP_GET(heap, instruction->args.at[i]));
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
PRINTLN: {
|
||||
for (GroundSize i = 0; i < instruction->args.len; i++) {
|
||||
GroundValue* val = Ground.Bytecode.Heap.get(heap, instruction->args.at[i]);
|
||||
switch (val->type.type) {
|
||||
case GroundType_Int:
|
||||
printf("%" PRId64, val->as.Int);
|
||||
break;
|
||||
|
||||
case GroundType_Double:
|
||||
printf("%f", val->as.Double);
|
||||
break;
|
||||
|
||||
case GroundType_Bool:
|
||||
printf(val->as.Bool ? "true" : "false");
|
||||
break;
|
||||
|
||||
case GroundType_Char:
|
||||
printf("%c", val->as.Char);
|
||||
break;
|
||||
|
||||
case GroundType_String:
|
||||
printf("%s", val->as.String.cstr);
|
||||
break;
|
||||
|
||||
default:
|
||||
printf("<fixme>");
|
||||
break;
|
||||
}
|
||||
printValue(HEAP_GET(heap, instruction->args.at[i]));
|
||||
}
|
||||
printf("\n");
|
||||
return -1;
|
||||
}
|
||||
SET: {
|
||||
Ground.Bytecode.Heap.set(heap, instruction->args.at[0], *Ground.Bytecode.Heap.get(heap, instruction->args.at[1]));
|
||||
HEAP_SET(heap, instruction->args.at[0], *HEAP_GET(heap, instruction->args.at[1]));
|
||||
return -1;
|
||||
}
|
||||
GETTYPE: {
|
||||
@@ -136,9 +112,9 @@ int64_t _GroundBytecodeInstructionExecute(GroundBytecodeInstruction* instruction
|
||||
return -1;
|
||||
}
|
||||
ADD: {
|
||||
GroundValue* final = Ground.Bytecode.Heap.get(heap, instruction->args.at[2]);
|
||||
GroundValue* left = Ground.Bytecode.Heap.get(heap, instruction->args.at[0]);
|
||||
GroundValue* right = Ground.Bytecode.Heap.get(heap, instruction->args.at[1]);
|
||||
GroundValue* final = HEAP_GET(heap, instruction->args.at[2]);
|
||||
GroundValue* left = HEAP_GET(heap, instruction->args.at[0]);
|
||||
GroundValue* right = HEAP_GET(heap, instruction->args.at[1]);
|
||||
|
||||
switch (left->type.type) {
|
||||
case GroundType_Int: {
|
||||
@@ -180,7 +156,8 @@ int64_t _GroundBytecodeInstructionExecute(GroundBytecodeInstruction* instruction
|
||||
case GroundType_String: {
|
||||
switch (right->type.type) {
|
||||
case GroundType_String: {
|
||||
char* buf = malloc(sizeof(char) * (left->as.String.len + right->as.String.len + 1));
|
||||
GroundSize total = left->as.String.len + right->as.String.len;
|
||||
char* buf = malloc(total + 1);
|
||||
if (buf == NULL) {
|
||||
Ground.Log.Error("malloc failed (instruction ADD{string, string, dirref}) in Ground.Instruction.execute()");
|
||||
Ground.Flags.error = true;
|
||||
@@ -206,9 +183,9 @@ int64_t _GroundBytecodeInstructionExecute(GroundBytecodeInstruction* instruction
|
||||
return -1;
|
||||
}
|
||||
SUBTRACT: {
|
||||
GroundValue* final = Ground.Bytecode.Heap.get(heap, instruction->args.at[2]);
|
||||
GroundValue* left = Ground.Bytecode.Heap.get(heap, instruction->args.at[0]);
|
||||
GroundValue* right = Ground.Bytecode.Heap.get(heap, instruction->args.at[1]);
|
||||
GroundValue* final = HEAP_GET(heap, instruction->args.at[2]);
|
||||
GroundValue* left = HEAP_GET(heap, instruction->args.at[0]);
|
||||
GroundValue* right = HEAP_GET(heap, instruction->args.at[1]);
|
||||
|
||||
switch (left->type.type) {
|
||||
case GroundType_Int: {
|
||||
@@ -256,9 +233,9 @@ int64_t _GroundBytecodeInstructionExecute(GroundBytecodeInstruction* instruction
|
||||
return -1;
|
||||
}
|
||||
MULTIPLY: {
|
||||
GroundValue* final = Ground.Bytecode.Heap.get(heap, instruction->args.at[2]);
|
||||
GroundValue* left = Ground.Bytecode.Heap.get(heap, instruction->args.at[0]);
|
||||
GroundValue* right = Ground.Bytecode.Heap.get(heap, instruction->args.at[1]);
|
||||
GroundValue* final = HEAP_GET(heap, instruction->args.at[2]);
|
||||
GroundValue* left = HEAP_GET(heap, instruction->args.at[0]);
|
||||
GroundValue* right = HEAP_GET(heap, instruction->args.at[1]);
|
||||
|
||||
switch (left->type.type) {
|
||||
case GroundType_Int: {
|
||||
@@ -306,9 +283,9 @@ int64_t _GroundBytecodeInstructionExecute(GroundBytecodeInstruction* instruction
|
||||
return -1;
|
||||
}
|
||||
DIVIDE: {
|
||||
GroundValue* final = Ground.Bytecode.Heap.get(heap, instruction->args.at[2]);
|
||||
GroundValue* left = Ground.Bytecode.Heap.get(heap, instruction->args.at[0]);
|
||||
GroundValue* right = Ground.Bytecode.Heap.get(heap, instruction->args.at[1]);
|
||||
GroundValue* final = HEAP_GET(heap, instruction->args.at[2]);
|
||||
GroundValue* left = HEAP_GET(heap, instruction->args.at[0]);
|
||||
GroundValue* right = HEAP_GET(heap, instruction->args.at[1]);
|
||||
|
||||
switch (left->type.type) {
|
||||
case GroundType_Int: {
|
||||
@@ -356,9 +333,9 @@ int64_t _GroundBytecodeInstructionExecute(GroundBytecodeInstruction* instruction
|
||||
return -1;
|
||||
}
|
||||
EQUAL: {
|
||||
GroundValue* final = Ground.Bytecode.Heap.get(heap, instruction->args.at[2]);
|
||||
GroundValue* left = Ground.Bytecode.Heap.get(heap, instruction->args.at[0]);
|
||||
GroundValue* right = Ground.Bytecode.Heap.get(heap, instruction->args.at[1]);
|
||||
GroundValue* final = HEAP_GET(heap, instruction->args.at[2]);
|
||||
GroundValue* left = HEAP_GET(heap, instruction->args.at[0]);
|
||||
GroundValue* right = HEAP_GET(heap, instruction->args.at[1]);
|
||||
|
||||
switch (left->type.type) {
|
||||
case GroundType_Int: {
|
||||
@@ -413,14 +390,13 @@ int64_t _GroundBytecodeInstructionExecute(GroundBytecodeInstruction* instruction
|
||||
}
|
||||
case GroundType_String: {
|
||||
if (right->type.type == GroundType_String) {
|
||||
*final = Ground.New.Value.Bool(strcmp(left->as.String.cstr, right->as.String.cstr));
|
||||
*final = Ground.New.Value.Bool(strcmp(left->as.String.cstr, right->as.String.cstr) == 0);
|
||||
} else {
|
||||
*final = Ground.New.Value.Bool(false);
|
||||
}
|
||||
break;
|
||||
}
|
||||
default: {
|
||||
// FIXME implement for complex types
|
||||
*final = Ground.New.Value.Bool(false);
|
||||
break;
|
||||
}
|
||||
@@ -428,9 +404,9 @@ int64_t _GroundBytecodeInstructionExecute(GroundBytecodeInstruction* instruction
|
||||
return -1;
|
||||
}
|
||||
INEQUAL: {
|
||||
GroundValue* final = Ground.Bytecode.Heap.get(heap, instruction->args.at[2]);
|
||||
GroundValue* left = Ground.Bytecode.Heap.get(heap, instruction->args.at[0]);
|
||||
GroundValue* right = Ground.Bytecode.Heap.get(heap, instruction->args.at[1]);
|
||||
GroundValue* final = HEAP_GET(heap, instruction->args.at[2]);
|
||||
GroundValue* left = HEAP_GET(heap, instruction->args.at[0]);
|
||||
GroundValue* right = HEAP_GET(heap, instruction->args.at[1]);
|
||||
|
||||
switch (left->type.type) {
|
||||
case GroundType_Int: {
|
||||
@@ -485,14 +461,13 @@ int64_t _GroundBytecodeInstructionExecute(GroundBytecodeInstruction* instruction
|
||||
}
|
||||
case GroundType_String: {
|
||||
if (right->type.type == GroundType_String) {
|
||||
*final = Ground.New.Value.Bool(!strcmp(left->as.String.cstr, right->as.String.cstr));
|
||||
*final = Ground.New.Value.Bool(strcmp(left->as.String.cstr, right->as.String.cstr) != 0);
|
||||
} else {
|
||||
*final = Ground.New.Value.Bool(true);
|
||||
}
|
||||
break;
|
||||
}
|
||||
default: {
|
||||
// FIXME implement for complex types
|
||||
*final = Ground.New.Value.Bool(false);
|
||||
break;
|
||||
}
|
||||
@@ -500,7 +475,7 @@ int64_t _GroundBytecodeInstructionExecute(GroundBytecodeInstruction* instruction
|
||||
return -1;
|
||||
}
|
||||
NOT: {
|
||||
Ground.Bytecode.Heap.set(heap, instruction->args.at[1], Ground.New.Value.Bool(!Ground.Bytecode.Heap.get(heap, instruction->args.at[0])->as.Bool));
|
||||
HEAP_SET(heap, instruction->args.at[1], Ground.New.Value.Bool(!HEAP_GET(heap, instruction->args.at[0])->as.Bool));
|
||||
return -1;
|
||||
}
|
||||
GREATER: {
|
||||
|
||||
Reference in New Issue
Block a user