From e32231ffa4dfc4b5c3c65437f03190e57ccc9678 Mon Sep 17 00:00:00 2001 From: Bad Diode Date: Wed, 10 Nov 2021 10:27:45 +0100 Subject: Add support for args in function calls --- src/compiler.h | 24 ++++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/src/compiler.h b/src/compiler.h index 0223269..d4b9e72 100644 --- a/src/compiler.h +++ b/src/compiler.h @@ -363,6 +363,7 @@ compile_proc_call(Object *obj) { compile_object(obj->tail->head); context_printf(" pop rdi\n"); context_printf(" call display\n"); + compile_nil(); } else if (sv_equal(&obj->head->text, &STRING("not"))) { compile_not(obj->tail); } else if (sv_equal(&obj->head->text, &STRING("and"))) { @@ -387,12 +388,18 @@ compile_proc_call(Object *obj) { compile_cdr(obj->tail); } else { compile_object(obj->head); - context_printf(" pop rax\n"); + size_t n_args = 0; + while (obj->tail != NULL) { + obj = obj->tail; + compile_object(obj->head); + n_args++; + } + context_printf(" mov rax, [rsp + %zu]\n", 8 * n_args); context_printf(" mov rcx, PTR_MASK\n"); context_printf(" and rcx, rax\n"); context_printf(" mov rax, [rcx]\n"); context_printf(" call rax\n"); - // TODO: prepare call-stack with arguments etc. + context_printf(" push rax\n"); } } @@ -457,9 +464,22 @@ compile_lambda(Object *obj) { char *name = generate_label("BDLP"); context_printf("alignb 8\n"); context_printf("%s:\n", name); + + // Initialize function call frame. + context_printf(" push rbp\n"); + context_printf(" mov rbp, rsp\n"); + + // Procedure body. for (size_t i = 0; i < array_size(obj->body); i++) { compile_object(obj->body[i]); } + + // Return is stored in the `rax`. + context_printf(" pop rax\n"); + + // Restore the previous call frame. + context_printf(" mov rsp, rbp\n"); + context_printf(" pop rbp\n"); context_printf(" ret\n"); context_printf("\n"); -- cgit v1.2.1