aboutsummaryrefslogtreecommitdiff
path: root/src/generator.c
diff options
context:
space:
mode:
authorMustafa Quraish <[email protected]>2022-02-03 04:38:35 -0500
committerMustafa Quraish <[email protected]>2022-02-03 04:44:46 -0500
commit1a165b3fcdabf7462a8f86eab5a6274f92ce9653 (patch)
treeaaaebcbd82411f0e7379c6a719e035adcd29919d /src/generator.c
parentCheck for integer-like types in type-checking instead of exact ones (diff)
downloadcup-1a165b3fcdabf7462a8f86eab5a6274f92ce9653.tar.xz
cup-1a165b3fcdabf7462a8f86eab5a6274f92ce9653.zip
Add helper to create builtins for syscalls + implement `read()`
This was possible, but very tedious to do by hand before. Now we automate it based on the number of arguments. Note that currently we can't just add `syscall3()` etc as builtins because the actual numbers for the system calls vary from one system to another, and we want to maintain support for macOS and Linux (at least for now).
Diffstat (limited to 'src/generator.c')
-rw-r--r--src/generator.c58
1 files changed, 2 insertions, 56 deletions
diff --git a/src/generator.c b/src/generator.c
index aace3bc..829bf73 100644
--- a/src/generator.c
+++ b/src/generator.c
@@ -16,7 +16,7 @@ static Node *current_function = NULL;
static Node *defer_stack[DEFER_STACK_SIZE];
static i64 defer_stack_count = 0;
-void make_syscall(i64 syscall_no, FILE *out) {
+void generate_syscall(i64 syscall_no, FILE *out) {
#if __APPLE__
syscall_no += 0x2000000;
#endif
@@ -457,7 +457,7 @@ void generate_asm(Node *root, FILE *out)
fprintf(out, " call func_main\n");
fprintf(out, " mov rdi, rax\n");
- make_syscall(SYS_exit, out);
+ generate_syscall(SYS_exit, out);
// TODO: Don't generate code for functions that cannot get called.
// TODO: Add implementations of some primitives?
@@ -471,58 +471,4 @@ void generate_asm(Node *root, FILE *out)
for (i64 i = 0; i < num_string_literals; i++) {
fprintf(out, " global_string_%lld: db `%s`, 0\n", i, all_string_literals[i]);
}
-}
-
-void generate_builtins(FILE *out)
-{
- // Stolen shamelessly from tsoding/porth:
- // https://gitlab.com/tsoding/porth
- fprintf(out,
- "func_print:\n"
- " mov rdi, [rsp+8]\n"
- " mov r9, -3689348814741910323\n"
- " sub rsp, 40\n"
- " mov BYTE [rsp+31], 10\n"
- " lea rcx, [rsp+30]\n"
- " mov qword rbx, 0\n"
- ".L2:\n"
- " mov rax, rdi\n"
- " lea r8, [rsp+32]\n"
- " mul r9\n"
- " mov rax, rdi\n"
- " sub r8, rcx\n"
- " shr rdx, 3\n"
- " lea rsi, [rdx+rdx*4]\n"
- " add rsi, rsi\n"
- " sub rax, rsi\n"
- " add eax, 48\n"
- " mov BYTE [rcx], al\n"
- " mov rax, rdi\n"
- " mov rdi, rdx\n"
- " mov rdx, rcx\n"
- " sub rcx, 1\n"
- " cmp rax, 9\n"
- " ja .L2\n"
- " lea rax, [rsp+32]\n"
- " mov edi, 1\n"
- " sub rdx, rax\n"
- " xor eax, eax\n"
- " lea rsi, [rsp+32+rdx]\n"
- " mov rdx, r8\n"
- );
- make_syscall(SYS_write, out);
- fprintf(out, " add rsp, 40\n");
- fprintf(out, " ret\n");
-
- /////////////////////////////////////////////////////////////////
-
- // Write syscall
- fprintf(out,
- "func_write:\n"
- " mov rdi, [rsp+8]\n" // stdout
- " mov rsi, [rsp+16]\n"
- " mov rdx, [rsp+24]\n" // 1 byte
- );
- make_syscall(SYS_write, out);
- fprintf(out, " ret\n");
} \ No newline at end of file