#include "compiler.h" #include "interpreter.h" #include "types.h" #include "include/estr.h" #include char* compileGroundProgram(GroundProgram* program) { Estr start = CREATE_ESTR("global _start\nsection .text\n_start:\n"); Estr data = CREATE_ESTR("section .rodata\n"); // Create data section for (size_t i = 0; i < program->size; i++) { } // Generate assembly for (size_t i = 0; i < program->size; i++) { GroundInstruction gi = program->instructions[i]; switch (gi.type) { case END: { 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); APPEND_ESTR(start, " mov rax, 60\n"); APPEND_ESTR(start, " mov rdi, "); APPEND_ESTR(start, retstr); APPEND_ESTR(start, "\n"); 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; }