From 3481ac42896ae7c5a7b40bf456cd203edeeefe4a Mon Sep 17 00:00:00 2001 From: Bad Diode Date: Tue, 16 Nov 2021 20:32:43 +0100 Subject: Replace `call` with `jmp` in preparation for TCO --- src/compiler.h | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/src/compiler.h b/src/compiler.h index 3d61833..d1952ed 100644 --- a/src/compiler.h +++ b/src/compiler.h @@ -379,11 +379,15 @@ compile_call(Object *obj) { compile_object(obj->head); } + char *lab_end = generate_label("BDLL"); context_printf(" mov rax, [rsp + %zu]\n", 8 * offset); - context_printf(" call rax\n"); + context_printf(" lea rcx, [%s]\n", lab_end); + context_printf(" push rcx\n"); + context_printf(" jmp rax\n"); + context_printf("%s:\n", lab_end); // Restore stack to previous location and store the result on top. - context_printf(" add rsp, %zu\n", 8 * (offset + 1)); + context_printf(" add rsp, %zu\n", 8 * (offset + 2)); context_printf(" push rax\n"); context_printf(" ;; <-- compile_call\n"); } @@ -531,10 +535,11 @@ compile_lambda(Object *obj) { context_printf(" pop rax\n"); // Restore the previous call frame. + context_printf(" mov rdi, [rbp + 8]\n"); context_printf(" mov rsp, rbp\n"); context_printf(" add rsp, %zu\n", 8 * array_size(current_env->locals)); context_printf(" pop rbp\n"); - context_printf(" ret\n"); + context_printf(" jmp rdi\n"); context_printf("\n"); // Restore previous compilation context. -- cgit v1.2.1