2026-06-21 14:35:48 +10:00
|
|
|
#include "../../include/ground.h"
|
|
|
|
|
|
|
|
|
|
static inline void doLabels(GroundProgram* program, GroundState* state) {
|
|
|
|
|
|
|
|
|
|
for (GroundSize i = 0; i < program->len; i++) {
|
|
|
|
|
GroundInstruction* instruction = &program->at[i];
|
|
|
|
|
if (instruction->type == GroundInstruction_CREATELABEL) {
|
|
|
|
|
if (instruction->args.len > 0) {
|
|
|
|
|
GroundArg* arg = &instruction->args.at[0];
|
|
|
|
|
GroundLabel* label = NULL;
|
|
|
|
|
HASH_FIND_STR(state->labels, arg->as.ref->string, label);
|
|
|
|
|
|
|
|
|
|
if (label == NULL) {
|
|
|
|
|
label = malloc(sizeof(GroundLabel));
|
|
|
|
|
if (label == NULL) {
|
2026-07-05 18:01:09 +10:00
|
|
|
Ground.Log.Error("malloc failed in Ground.New.Bytecode -> doLabels");
|
2026-06-21 14:35:48 +10:00
|
|
|
Ground.Flags.error = true;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
strncpy(label->name, arg->as.ref->string, 2047);
|
|
|
|
|
label->name[2047] = '\0';
|
|
|
|
|
label->lineNum = i;
|
|
|
|
|
HASH_ADD_STR(state->labels, name, label);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for (GroundSize i = 0; i < program->len; i++) {
|
|
|
|
|
GroundInstruction* instruction = &program->at[i];
|
|
|
|
|
if (instruction->type == GroundInstruction_JUMP) {
|
|
|
|
|
GroundArg* arg = &instruction->args.at[0];
|
|
|
|
|
GroundSize* line = Ground.State.findLabel(state, arg->as.ref->string);
|
|
|
|
|
if (line == NULL) {
|
|
|
|
|
char buf[2048];
|
2026-07-05 18:01:09 +10:00
|
|
|
snprintf(buf, 2047, "couldn't find label '%s' (instruction JUMP at %zu) in Ground.New.Bytecode -> doLabels", arg->as.ref->string, i);
|
2026-06-21 14:35:48 +10:00
|
|
|
Ground.Log.Error(buf);
|
|
|
|
|
Ground.Flags.error = true;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
arg->_offset = *line;
|
|
|
|
|
|
|
|
|
|
} else if (instruction->type == GroundInstruction_IF) {
|
|
|
|
|
GroundArg* arg = &instruction->args.at[1];
|
|
|
|
|
GroundSize* line = Ground.State.findLabel(state, arg->as.ref->string);
|
|
|
|
|
if (line == NULL) {
|
|
|
|
|
char buf[2048];
|
2026-07-05 18:01:09 +10:00
|
|
|
snprintf(buf, 2047, "couldn't find label '%s' (instruction IF at %zu) in Ground.New.Bytecode -> doLabels", arg->as.ref->string, i);
|
2026-06-21 14:35:48 +10:00
|
|
|
Ground.Log.Error(buf);
|
|
|
|
|
Ground.Flags.error = true;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
arg->_offset = *line;
|
|
|
|
|
|
|
|
|
|
} else if (instruction->type == GroundInstruction_CATCH) {
|
|
|
|
|
GroundArg* arg = &instruction->args.at[1];
|
|
|
|
|
GroundSize* line = Ground.State.findLabel(state, arg->as.ref->string);
|
|
|
|
|
if (line == NULL) {
|
|
|
|
|
char buf[2048];
|
2026-07-05 18:01:09 +10:00
|
|
|
snprintf(buf, 2047, "couldn't find label '%s' (instruction CATCH at %zu) in Ground.New.Bytecode -> doLabels", arg->as.ref->string, i);
|
2026-06-21 14:35:48 +10:00
|
|
|
Ground.Log.Error(buf);
|
|
|
|
|
Ground.Flags.error = true;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
arg->_offset = *line;
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Assigns an offset to each variable referenced.
|
|
|
|
|
*/
|
|
|
|
|
static inline GroundSize doOffsets(GroundProgram* gp, GroundState* state, GroundBytecodeProgram* gbp) {
|
|
|
|
|
GroundSize size = 0;
|
|
|
|
|
|
2026-07-04 17:53:14 +10:00
|
|
|
// Add size of state and assign offsets to pre-existing variables
|
2026-06-21 14:35:48 +10:00
|
|
|
GroundVariable *s, *tmp;
|
|
|
|
|
HASH_ITER(hh, state->variables, s, tmp) {
|
2026-07-04 17:53:14 +10:00
|
|
|
s->_offset = size++;
|
2026-06-21 14:35:48 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for (GroundSize i = 0; i < gp->len; i++) {
|
2026-07-05 18:01:09 +10:00
|
|
|
// Handle instructions which deal with struct fields
|
|
|
|
|
switch (gp->at[i].type) {
|
|
|
|
|
case GroundInstruction_STRUCT: {
|
|
|
|
|
GroundInstruction* inst = &gp->at[i];
|
|
|
|
|
GroundVariable* gsv = NULL;
|
|
|
|
|
HASH_FIND_STR(state->variables, inst->args.at[0].as.ref->string, gsv);
|
|
|
|
|
if (gsv == NULL) {
|
|
|
|
|
Ground.Log.Error("unexpected null variable in Ground.New.Bytecode -> doOffsets");
|
|
|
|
|
Ground.Flags.error = true;
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
if (gsv->value.type.type != GroundType_Struct) {
|
|
|
|
|
Ground.Log.Error("unexpected null variable in Ground.New.Bytecode -> doOffsets");
|
|
|
|
|
Ground.Flags.error = true;
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
GroundStruct* gs = &gsv->value.as.Struct;
|
|
|
|
|
|
|
|
|
|
// Set offset for each struct field
|
|
|
|
|
GroundObjectField *s, *tmp;
|
|
|
|
|
GroundSize i = 0;
|
|
|
|
|
HASH_ITER(hh, gs->fields, s, tmp) {
|
|
|
|
|
s->offset = i++;
|
|
|
|
|
}
|
|
|
|
|
|
2026-07-08 12:06:18 +10:00
|
|
|
// Now go through and add offsets to everything (skip struct name at index 0)
|
|
|
|
|
for (GroundSize j = 1; j < inst->args.len; j++) {
|
2026-07-05 18:01:09 +10:00
|
|
|
// struct field name
|
|
|
|
|
GroundArg* arg = &inst->args.at[j];
|
|
|
|
|
GroundObjectField* field = NULL;
|
2026-07-08 12:06:18 +10:00
|
|
|
HASH_FIND_STR(gs->fields, arg->as.ref->string, field);
|
2026-07-05 18:01:09 +10:00
|
|
|
if (field == NULL) {
|
|
|
|
|
Ground.Log.Error("unexpected null struct field in Ground.New.Bytecode -> doOffsets");
|
|
|
|
|
Ground.Flags.error = true;
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
arg->_offset = field->offset;
|
|
|
|
|
// 2nd arg offset is already set, but what we do next depends on it
|
|
|
|
|
j++;
|
|
|
|
|
// 0 -> none
|
|
|
|
|
// 1 -> init
|
|
|
|
|
// 2 -> set
|
|
|
|
|
// 3 -> attach closure to function
|
|
|
|
|
switch (inst->args.at[j]._offset) {
|
|
|
|
|
case 0: case 3: break; // these do not require extra values
|
|
|
|
|
case 1: // but these do, so we process them
|
|
|
|
|
case 2: { // it's safe to assume that literals will have
|
|
|
|
|
j++; // been dealt with accordingly
|
|
|
|
|
arg = &inst->args.at[j];
|
|
|
|
|
GroundVariable* item = NULL;
|
|
|
|
|
HASH_FIND_STR(state->variables, arg->as.ref->string, item);
|
|
|
|
|
|
|
|
|
|
if (item == NULL) {
|
|
|
|
|
item = malloc(sizeof(GroundVariable));
|
|
|
|
|
if (item == NULL) {
|
|
|
|
|
Ground.Log.Error("malloc failed in Ground.New.Bytecode -> doOffsets");
|
|
|
|
|
Ground.Flags.error = true;
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
item->value = Ground.New.Value.Int(0);
|
|
|
|
|
strncpy(item->name, arg->as.ref->string, 2047);
|
|
|
|
|
item->_offset = size++;
|
|
|
|
|
HASH_ADD_STR(state->variables, name, item);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
arg->_offset = item->_offset;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
case GroundInstruction_GETFIELD: {
|
|
|
|
|
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
case GroundInstruction_SETFIELD: {
|
|
|
|
|
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
case GroundInstruction_CALLMETHOD: {
|
|
|
|
|
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
default: break;
|
|
|
|
|
}
|
2026-06-21 14:35:48 +10:00
|
|
|
for (GroundSize j = 0; j < gp->at[i].args.len; j++) {
|
|
|
|
|
GroundArg* arg = &gp->at[i].args.at[j];
|
|
|
|
|
|
|
|
|
|
if (
|
|
|
|
|
arg->type == GroundArg_Label ||
|
|
|
|
|
arg->type == GroundArg_LineRef
|
|
|
|
|
) continue;
|
|
|
|
|
|
|
|
|
|
if (arg->type == GroundArg_Value) {
|
|
|
|
|
GroundVariable* item = malloc(sizeof(GroundVariable));
|
|
|
|
|
if (item == NULL) {
|
2026-07-05 18:01:09 +10:00
|
|
|
Ground.Log.Error("malloc failed in Ground.New.Bytecode -> doOffsets");
|
2026-06-21 14:35:48 +10:00
|
|
|
Ground.Flags.error = true;
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
item->value = Ground.Copy.Value(&arg->as.value);
|
|
|
|
|
if (Ground.Flags.error) return 0;
|
|
|
|
|
item->_offset = size++;
|
|
|
|
|
snprintf(item->name, sizeof(item->name) - 1, "_._.ground_internal_constant_%zu", item->_offset);
|
|
|
|
|
HASH_ADD_STR(state->variables, name, item);
|
2026-06-21 16:13:00 +10:00
|
|
|
arg->_offset = item->_offset;
|
2026-06-21 14:35:48 +10:00
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
GroundVariable* item = NULL;
|
|
|
|
|
HASH_FIND_STR(state->variables, arg->as.ref->string, item);
|
|
|
|
|
|
|
|
|
|
if (item == NULL) {
|
|
|
|
|
item = malloc(sizeof(GroundVariable));
|
|
|
|
|
if (item == NULL) {
|
2026-07-05 18:01:09 +10:00
|
|
|
Ground.Log.Error("malloc failed in Ground.New.Bytecode -> doOffsets");
|
2026-06-21 14:35:48 +10:00
|
|
|
Ground.Flags.error = true;
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
item->value = Ground.New.Value.Int(0);
|
|
|
|
|
strncpy(item->name, arg->as.ref->string, 2047);
|
|
|
|
|
item->_offset = size++;
|
|
|
|
|
HASH_ADD_STR(state->variables, name, item);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
arg->_offset = item->_offset;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Convert to GroundBytecodeInstruction
|
|
|
|
|
GroundBytecodeInstruction inst = {
|
|
|
|
|
.type = gp->at[i].type,
|
|
|
|
|
.args.at = malloc(sizeof(GroundSize) * gp->at[i].args.len),
|
|
|
|
|
.args.len = gp->at[i].args.len,
|
|
|
|
|
.args.capacity = gp->at[i].args.len
|
|
|
|
|
};
|
|
|
|
|
|
2026-06-21 16:13:00 +10:00
|
|
|
if (inst.args.at == NULL) {
|
2026-07-05 18:01:09 +10:00
|
|
|
Ground.Log.Error("malloc failed in Ground.New.Bytecode -> doOffsets");
|
2026-06-21 16:13:00 +10:00
|
|
|
Ground.Flags.error = true;
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
2026-06-21 14:35:48 +10:00
|
|
|
|
|
|
|
|
for (GroundSize j = 0; j < gp->at[i].args.len; j++) {
|
|
|
|
|
inst.args.at[j] = gp->at[i].args.at[j]._offset;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
gbp->at[i] = inst;
|
|
|
|
|
gbp->len++;
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
return size;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
GroundBytecode _GroundNewBytecode(GroundProgram* program, GroundState* state) {
|
|
|
|
|
|
|
|
|
|
GroundBytecode bytecode = {
|
|
|
|
|
.program = {
|
|
|
|
|
.at = malloc(sizeof(GroundBytecodeInstruction) * program->len),
|
|
|
|
|
.capacity = program->len,
|
|
|
|
|
.len = 0,
|
|
|
|
|
},
|
|
|
|
|
.heap = {
|
|
|
|
|
.heap = NULL,
|
|
|
|
|
.capacity = 0,
|
|
|
|
|
.len = 0
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
if (bytecode.program.at == NULL) {
|
|
|
|
|
Ground.Log.Error("malloc failed in Ground.New.Bytecode");
|
|
|
|
|
Ground.Flags.error = true;
|
|
|
|
|
return bytecode;
|
|
|
|
|
}
|
|
|
|
|
|
2026-07-04 17:53:14 +10:00
|
|
|
// Add core types to state
|
|
|
|
|
for (unsigned int i = 0; i < (unsigned int)GroundType_Object; i++) {
|
|
|
|
|
GroundVariable* var = malloc(sizeof(GroundVariable));
|
|
|
|
|
if (var == NULL) {
|
|
|
|
|
Ground.Log.Error("malloc failed in Ground.New.Bytecode");
|
|
|
|
|
Ground.Flags.error = true;
|
|
|
|
|
return bytecode;
|
|
|
|
|
}
|
|
|
|
|
var->value = (GroundValue) {
|
|
|
|
|
.type = {GroundType_CoreType},
|
|
|
|
|
.as.CoreType = (enum GroundTypeType) i
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
switch ((enum GroundTypeType) i) {
|
|
|
|
|
case GroundType_Int: {
|
|
|
|
|
sprintf(var->name, "int");
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
case GroundType_Double: {
|
|
|
|
|
sprintf(var->name, "double");
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
case GroundType_Char: {
|
|
|
|
|
sprintf(var->name, "char");
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
case GroundType_Bool: {
|
|
|
|
|
sprintf(var->name, "bool");
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
case GroundType_String: {
|
|
|
|
|
sprintf(var->name, "string");
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
case GroundType_List: {
|
|
|
|
|
sprintf(var->name, "list");
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
case GroundType_Function: {
|
|
|
|
|
sprintf(var->name, "function");
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
case GroundType_Struct: {
|
|
|
|
|
sprintf(var->name, "struct");
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
default: break; // unreachable (in theory)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
HASH_ADD_STR(state->variables, name, var);
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
state->_size += (unsigned int)GroundType_Object + 1;
|
|
|
|
|
|
2026-06-21 14:35:48 +10:00
|
|
|
// Precompute some really cool stuff
|
|
|
|
|
doLabels(program, state);
|
|
|
|
|
if (Ground.Flags.error) return bytecode;
|
|
|
|
|
|
|
|
|
|
GroundSize size = doOffsets(program, state, &bytecode.program);
|
|
|
|
|
if (Ground.Flags.error) return bytecode;
|
|
|
|
|
|
|
|
|
|
// Allocate heap
|
2026-07-04 19:59:00 +10:00
|
|
|
bytecode.heap.heap = malloc(sizeof(GroundBytecodeValue) * size);
|
2026-06-21 14:35:48 +10:00
|
|
|
if (bytecode.heap.heap == NULL) {
|
|
|
|
|
Ground.Log.Error("malloc failed in Ground.New.Bytecode");
|
|
|
|
|
Ground.Flags.error = true;
|
|
|
|
|
return bytecode;
|
|
|
|
|
}
|
|
|
|
|
bytecode.heap.capacity = size;
|
|
|
|
|
bytecode.heap.len = size;
|
|
|
|
|
|
|
|
|
|
GroundVariable *s, *tmp;
|
|
|
|
|
|
2026-06-21 16:13:00 +10:00
|
|
|
// Copy constants into heap at their assigned offsets
|
2026-06-21 14:35:48 +10:00
|
|
|
HASH_ITER(hh, state->variables, s, tmp) {
|
2026-07-02 11:35:56 +10:00
|
|
|
bytecode.heap.heap[s->_offset] = Ground.New.BytecodeValue(Ground.Copy.Value(&s->value));
|
2026-06-21 14:35:48 +10:00
|
|
|
if (Ground.Flags.error) return bytecode;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return bytecode;
|
|
|
|
|
|
|
|
|
|
}
|