2026-01-21 11:17:19 +11:00
|
|
|
#include "compiler.h"
|
2026-01-21 11:38:37 +11:00
|
|
|
#include "interpreter.h"
|
2026-01-21 11:17:19 +11:00
|
|
|
#include "types.h"
|
|
|
|
|
#include "include/estr.h"
|
2026-01-21 11:38:37 +11:00
|
|
|
#include <stdint.h>
|
2026-01-21 11:17:19 +11:00
|
|
|
|
|
|
|
|
char* compileGroundProgram(GroundProgram* program) {
|
|
|
|
|
Estr start = CREATE_ESTR("global _start\nsection .text\n_start:\n");
|
|
|
|
|
Estr data = CREATE_ESTR("section .rodata\n");
|
|
|
|
|
|
2026-01-21 11:38:37 +11:00
|
|
|
// Create data section
|
2026-01-21 11:17:19 +11:00
|
|
|
for (size_t i = 0; i < program->size; i++) {
|
2026-01-21 11:38:37 +11:00
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Generate assembly
|
|
|
|
|
for (size_t i = 0; i < program->size; i++) {
|
|
|
|
|
GroundInstruction gi = program->instructions[i];
|
|
|
|
|
switch (gi.type) {
|
2026-01-21 11:17:19 +11:00
|
|
|
case END: {
|
2026-01-21 11:38:37 +11:00
|
|
|
if (gi.args.length < 1) {
|
|
|
|
|
runtimeError(TOO_FEW_ARGS, "Expecting 1 arg for end instruction", &gi, i);
|
|
|
|
|
}
|
|
|
|
|
if (gi.args.length > 1) {
|
|
|
|
|
runtimeError(TOO_MANY_ARGS, "Expecting 1 arg for end instruction", &gi, i);
|
|
|
|
|
}
|
|
|
|
|
if (gi.args.args[0].type != VALUE) {
|
|
|
|
|
runtimeError(ARG_TYPE_MISMATCH, "Expecting an Int for end instruction", &gi, i);
|
|
|
|
|
}
|
|
|
|
|
if (gi.args.args[0].value.value.type != INT) {
|
|
|
|
|
runtimeError(ARG_TYPE_MISMATCH, "Expecting an Int for end instruction", &gi, i);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int64_t bigretint = gi.args.args[0].value.value.data.intVal;
|
|
|
|
|
int retint;
|
|
|
|
|
if (bigretint > 255) {
|
|
|
|
|
retint = 255;
|
|
|
|
|
}
|
|
|
|
|
else if (bigretint < 0) {
|
|
|
|
|
retint = 0;
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
retint = bigretint;
|
|
|
|
|
}
|
|
|
|
|
char retstr[8];
|
|
|
|
|
snprintf(retstr, sizeof(retstr), "%d", retint);
|
2026-01-21 11:17:19 +11:00
|
|
|
APPEND_ESTR(start, " mov rax, 60\n");
|
2026-01-21 11:38:37 +11:00
|
|
|
APPEND_ESTR(start, " mov rdi, ");
|
|
|
|
|
APPEND_ESTR(start, retstr);
|
|
|
|
|
APPEND_ESTR(start, "\n");
|
2026-01-21 11:17:19 +11:00
|
|
|
APPEND_ESTR(start, " syscall\n");
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
default: {
|
|
|
|
|
printf("no\n");
|
|
|
|
|
exit(1);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Estr complete = CREATE_ESTR("");
|
|
|
|
|
APPEND_ESTR(complete, start.str);
|
|
|
|
|
APPEND_ESTR(complete, data.str)
|
|
|
|
|
|
|
|
|
|
return complete.str;
|
|
|
|
|
}
|