diff --git a/README.md b/README.md index 9aad1e8..9240c35 100644 --- a/README.md +++ b/README.md @@ -117,4 +117,6 @@ Supported instructions so far: * jump * if * @ (label creation) +* print +* println * end diff --git a/src/compiler.c b/src/compiler.c index 8e31730..716aed9 100644 --- a/src/compiler.c +++ b/src/compiler.c @@ -50,7 +50,7 @@ char* processValueString(GroundArg arg) { } if (arg.type == VALUE) { if (arg.value.value.type != INT) { - printf("Only int is supported right now"); + printf("Only int is supported right now\n"); exit(1); } char* buf = malloc(sizeof(char) * 64); @@ -63,6 +63,59 @@ char* processValueString(GroundArg arg) { char* compileGroundProgram(GroundProgram* program) { Estr start = CREATE_ESTR("global _start\nsection .text\n_start:\n"); Estr data = CREATE_ESTR("section .bss\n"); + Estr helpers = CREATE_ESTR(""); + + APPEND_ESTR(helpers, "\n; Helper: Print integer in rax\n"); + APPEND_ESTR(helpers, "print_int:\n"); + APPEND_ESTR(helpers, " push rbp\n"); + APPEND_ESTR(helpers, " mov rbp, rsp\n"); + APPEND_ESTR(helpers, " sub rsp, 32 ; Allocate buffer on stack\n"); + APPEND_ESTR(helpers, " mov rdi, rsp ; RDI = buffer pointer\n"); + APPEND_ESTR(helpers, " add rdi, 31 ; Point to end of buffer\n"); + APPEND_ESTR(helpers, " mov byte [rdi], 0 ; Null terminator\n"); + APPEND_ESTR(helpers, " dec rdi\n"); + APPEND_ESTR(helpers, " mov rbx, 10 ; Divisor\n"); + APPEND_ESTR(helpers, " test rax, rax\n"); + APPEND_ESTR(helpers, " jns .positive\n"); + APPEND_ESTR(helpers, " neg rax ; Make positive\n"); + APPEND_ESTR(helpers, " push rax\n"); + APPEND_ESTR(helpers, " mov byte [rdi], '-'\n"); + APPEND_ESTR(helpers, " dec rdi\n"); + APPEND_ESTR(helpers, " pop rax\n"); + APPEND_ESTR(helpers, ".positive:\n"); + APPEND_ESTR(helpers, " xor rcx, rcx ; Digit counter\n"); + APPEND_ESTR(helpers, ".convert_loop:\n"); + APPEND_ESTR(helpers, " xor rdx, rdx\n"); + APPEND_ESTR(helpers, " div rbx ; rax = rax/10, rdx = remainder\n"); + APPEND_ESTR(helpers, " add dl, '0' ; Convert to ASCII\n"); + APPEND_ESTR(helpers, " mov [rdi], dl\n"); + APPEND_ESTR(helpers, " dec rdi\n"); + APPEND_ESTR(helpers, " inc rcx\n"); + APPEND_ESTR(helpers, " test rax, rax\n"); + APPEND_ESTR(helpers, " jnz .convert_loop\n"); + APPEND_ESTR(helpers, " inc rdi ; Point back to first digit\n"); + APPEND_ESTR(helpers, " ; Now print the string\n"); + APPEND_ESTR(helpers, " mov rax, 1 ; sys_write\n"); + APPEND_ESTR(helpers, " mov rsi, rdi ; Buffer\n"); + APPEND_ESTR(helpers, " mov rdx, rcx ; Length\n"); + APPEND_ESTR(helpers, " mov rdi, 1 ; stdout\n"); + APPEND_ESTR(helpers, " syscall\n"); + APPEND_ESTR(helpers, " mov rsp, rbp\n"); + APPEND_ESTR(helpers, " pop rbp\n"); + APPEND_ESTR(helpers, " ret\n"); + + APPEND_ESTR(helpers, "\n; Helper: Print string at address in rax, length in rbx\n"); + APPEND_ESTR(helpers, "print_string:\n"); + APPEND_ESTR(helpers, " push rax\n"); + APPEND_ESTR(helpers, " push rbx\n"); + APPEND_ESTR(helpers, " mov rdi, 1 ; stdout\n"); + APPEND_ESTR(helpers, " mov rsi, rax ; String address\n"); + APPEND_ESTR(helpers, " mov rdx, rbx ; Length\n"); + APPEND_ESTR(helpers, " mov rax, 1 ; sys_write\n"); + APPEND_ESTR(helpers, " syscall\n"); + APPEND_ESTR(helpers, " pop rbx\n"); + APPEND_ESTR(helpers, " pop rax\n"); + APPEND_ESTR(helpers, " ret\n"); VariableTable varTable = createVariableTable(); @@ -77,7 +130,7 @@ char* compileGroundProgram(GroundProgram* program) { } // Create data section - for (size_t i = 0; i < program->size; i++) { + for (size_t i = 0; i < varTable.count; i++) { if (strcmp(varTable.vars[i].name, "") == 0) { continue; } @@ -386,6 +439,42 @@ char* compileGroundProgram(GroundProgram* program) { APPEND_ESTR(start, "], rax\n"); break; } + case PRINT: + case PRINTLN: { + if (gi.args.length < 1) { + runtimeError(TOO_FEW_ARGS, "Expecting 1 or more args", &gi, i); + } + + for (size_t j = 0; j < gi.args.length; j++) { + if (gi.args.args[j].type != VALUE && gi.args.args[j].type != VALREF) { + runtimeError(ARG_TYPE_MISMATCH, "Expecting a Value", &gi, i); + } + if (j > 0) { + // Print space between arguments + APPEND_ESTR(start, " ; print space\n"); + APPEND_ESTR(start, " mov rax, 1\n"); + APPEND_ESTR(start, " mov rdi, 1\n"); + APPEND_ESTR(start, " lea rsi, [rel space_char]\n"); + APPEND_ESTR(start, " mov rdx, 1\n"); + APPEND_ESTR(start, " syscall\n"); + } + APPEND_ESTR(start, " ; print int\n"); + APPEND_ESTR(start, " mov rax, "); + APPEND_ESTR(start, processValueString(gi.args.args[j])); + APPEND_ESTR(start, "\n"); + APPEND_ESTR(start, " call print_int\n"); + } + + if (gi.type == PRINTLN) { + APPEND_ESTR(start, " ; print newline\n"); + APPEND_ESTR(start, " mov rax, 1\n"); + APPEND_ESTR(start, " mov rdi, 1\n"); + APPEND_ESTR(start, " lea rsi, [rel newline_char]\n"); + APPEND_ESTR(start, " mov rdx, 1\n"); + APPEND_ESTR(start, " syscall\n"); + } + break; + } case END: { if (gi.args.length < 1) { runtimeError(TOO_FEW_ARGS, "Expecting 1 arg for end instruction", &gi, i); @@ -401,6 +490,10 @@ char* compileGroundProgram(GroundProgram* program) { APPEND_ESTR(start, " syscall\n"); break; } + case DROP: { + // Drop does nothing in ground->asm as we use the stack + break; + } default: { printf("no\n"); exit(1); @@ -411,6 +504,10 @@ char* compileGroundProgram(GroundProgram* program) { Estr complete = CREATE_ESTR(""); APPEND_ESTR(complete, start.str); APPEND_ESTR(complete, " ; End of program\n mov rax, 60\n mov rdi, 0\n syscall\n"); + APPEND_ESTR(complete, helpers.str); + APPEND_ESTR(complete, "\nsection .rodata\n"); + APPEND_ESTR(complete, "newline_char: db 10\n"); + APPEND_ESTR(complete, "space_char: db 32\n"); APPEND_ESTR(complete, data.str) return complete.str;