From 12ae1f331e79f769f01d2aa70608868f16667516 Mon Sep 17 00:00:00 2001 From: Bad Diode Date: Tue, 9 Nov 2021 17:27:28 +0100 Subject: Add initial procedure calls for lambdas --- src/compiler.h | 45 +++++++++++++++++++++++++++++++++++---------- src/x86_64/prelude.asm | 2 +- 2 files changed, 36 insertions(+), 11 deletions(-) diff --git a/src/compiler.h b/src/compiler.h index 64e6df9..897b7bb 100644 --- a/src/compiler.h +++ b/src/compiler.h @@ -40,11 +40,13 @@ do { \ #define FIXNUM_SHIFT 2LU // Heap allocated objects. -#define STRING_INV_MASK ~7LU -#define STRING_MASK 7LU -#define STRING_TAG 3LU -#define PAIR_MASK 7LU -#define PAIR_TAG 1LU +#define PTR_MASK ~7LU +#define STRING_MASK 7LU +#define STRING_TAG 3LU +#define PAIR_MASK 7LU +#define PAIR_TAG 1LU +#define LAMBDA_MASK 7LU +#define LAMBDA_TAG 6LU void compile_object(Object *obj); void compile_fixnum(Object *obj); @@ -384,8 +386,13 @@ compile_proc_call(Object *obj) { } else if (sv_equal(&obj->head->text, &STRING("cdr"))) { compile_cdr(obj->tail); } else { - fprintf(stderr, "error: not implemented\n"); - exit(EXIT_FAILURE); + compile_object(obj->head); + context_printf(" pop rax\n"); + 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. } } @@ -440,12 +447,15 @@ compile_string(Object *obj) { void compile_lambda(Object *obj) { + context_printf(" ;; --> compile_lambda\n"); + // Create a new compilation context. char *prev_context = current_context; current_context = NULL; array_init(current_context, 0); char *name = generate_label("BDLP"); + context_printf("alignb 8\n"); context_printf("%s:\n", name); for (size_t i = 0; i < array_size(obj->body); i++) { compile_object(obj->body[i]); @@ -457,8 +467,20 @@ compile_lambda(Object *obj) { array_push(procedures, current_context); current_context = prev_context; - // TODO: Create tagged pointer with this lambda procedure. - // TODO: Push compiled object to the stack. + // Create tagged pointer with this lambda procedure. + context_printf(" mov rax, %s\n", name); + context_printf(" mov [r15], rax\n"); + context_printf(" mov rax, r15\n"); + context_printf(" or rax, %zu\n", LAMBDA_TAG); + + // Push compiled object to the stack. + context_printf(" push rax\n"); + + // TODO: When we add closed variables they will be stored here, for now just + // incrementing by a 64 bit pointer size, as that is what we are storing. + context_printf(" add r15, 8\n"); + + context_printf(" ;; <-- compile_lambda\n"); } void @@ -533,9 +555,11 @@ compile(Root *roots) { printf("%%define FIXNUM_SHIFT %zu\n", FIXNUM_SHIFT); printf("%%define PAIR_MASK %zu\n", PAIR_MASK); printf("%%define PAIR_TAG %zu\n", PAIR_TAG); - printf("%%define STRING_INV_MASK %zu\n", STRING_INV_MASK); + printf("%%define PTR_MASK %zu\n", PTR_MASK); printf("%%define STRING_MASK %zu\n", STRING_MASK); printf("%%define STRING_TAG %zu\n", STRING_TAG); + printf("%%define LAMBDA_MASK %zu\n", LAMBDA_MASK); + printf("%%define LAMBDA_TAG %zu\n", LAMBDA_TAG); printf("%%define HEAP_SIZE %zu\n", HEAP_SIZE); printf("\n"); @@ -552,6 +576,7 @@ compile(Root *roots) { } // Main context. + printf("alignb 8\n"); printf("global _start\n"); printf("_start:\n"); printf(" mov r15, bdl_heap\n"); diff --git a/src/x86_64/prelude.asm b/src/x86_64/prelude.asm index 05762b9..abb069b 100644 --- a/src/x86_64/prelude.asm +++ b/src/x86_64/prelude.asm @@ -73,7 +73,7 @@ bool_write: printstring: mov rsi, rdi - mov rax, STRING_INV_MASK + mov rax, PTR_MASK and rsi, rax mov rdx, [rsi] add rsi, 8 -- cgit v1.2.1