aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorBad Diode <bd@badd10de.dev>2021-11-09 17:27:28 +0100
committerBad Diode <bd@badd10de.dev>2021-11-09 17:27:28 +0100
commit12ae1f331e79f769f01d2aa70608868f16667516 (patch)
tree8088f71512c79842455311b973a8c86733424b78
parent795707881285fd171c552516b11e5f7aca65f064 (diff)
downloadbdl-12ae1f331e79f769f01d2aa70608868f16667516.tar.gz
bdl-12ae1f331e79f769f01d2aa70608868f16667516.zip
Add initial procedure calls for lambdas
-rw-r--r--src/compiler.h45
-rw-r--r--src/x86_64/prelude.asm2
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 { \
40#define FIXNUM_SHIFT 2LU 40#define FIXNUM_SHIFT 2LU
41 41
42// Heap allocated objects. 42// Heap allocated objects.
43#define STRING_INV_MASK ~7LU 43#define PTR_MASK ~7LU
44#define STRING_MASK 7LU 44#define STRING_MASK 7LU
45#define STRING_TAG 3LU 45#define STRING_TAG 3LU
46#define PAIR_MASK 7LU 46#define PAIR_MASK 7LU
47#define PAIR_TAG 1LU 47#define PAIR_TAG 1LU
48#define LAMBDA_MASK 7LU
49#define LAMBDA_TAG 6LU
48 50
49void compile_object(Object *obj); 51void compile_object(Object *obj);
50void compile_fixnum(Object *obj); 52void compile_fixnum(Object *obj);
@@ -384,8 +386,13 @@ compile_proc_call(Object *obj) {
384 } else if (sv_equal(&obj->head->text, &STRING("cdr"))) { 386 } else if (sv_equal(&obj->head->text, &STRING("cdr"))) {
385 compile_cdr(obj->tail); 387 compile_cdr(obj->tail);
386 } else { 388 } else {
387 fprintf(stderr, "error: not implemented\n"); 389 compile_object(obj->head);
388 exit(EXIT_FAILURE); 390 context_printf(" pop rax\n");
391 context_printf(" mov rcx, PTR_MASK\n");
392 context_printf(" and rcx, rax\n");
393 context_printf(" mov rax, [rcx]\n");
394 context_printf(" call rax\n");
395 // TODO: prepare call-stack with arguments etc.
389 } 396 }
390} 397}
391 398
@@ -440,12 +447,15 @@ compile_string(Object *obj) {
440 447
441void 448void
442compile_lambda(Object *obj) { 449compile_lambda(Object *obj) {
450 context_printf(" ;; --> compile_lambda\n");
451
443 // Create a new compilation context. 452 // Create a new compilation context.
444 char *prev_context = current_context; 453 char *prev_context = current_context;
445 current_context = NULL; 454 current_context = NULL;
446 array_init(current_context, 0); 455 array_init(current_context, 0);
447 456
448 char *name = generate_label("BDLP"); 457 char *name = generate_label("BDLP");
458 context_printf("alignb 8\n");
449 context_printf("%s:\n", name); 459 context_printf("%s:\n", name);
450 for (size_t i = 0; i < array_size(obj->body); i++) { 460 for (size_t i = 0; i < array_size(obj->body); i++) {
451 compile_object(obj->body[i]); 461 compile_object(obj->body[i]);
@@ -457,8 +467,20 @@ compile_lambda(Object *obj) {
457 array_push(procedures, current_context); 467 array_push(procedures, current_context);
458 current_context = prev_context; 468 current_context = prev_context;
459 469
460 // TODO: Create tagged pointer with this lambda procedure. 470 // Create tagged pointer with this lambda procedure.
461 // TODO: Push compiled object to the stack. 471 context_printf(" mov rax, %s\n", name);
472 context_printf(" mov [r15], rax\n");
473 context_printf(" mov rax, r15\n");
474 context_printf(" or rax, %zu\n", LAMBDA_TAG);
475
476 // Push compiled object to the stack.
477 context_printf(" push rax\n");
478
479 // TODO: When we add closed variables they will be stored here, for now just
480 // incrementing by a 64 bit pointer size, as that is what we are storing.
481 context_printf(" add r15, 8\n");
482
483 context_printf(" ;; <-- compile_lambda\n");
462} 484}
463 485
464void 486void
@@ -533,9 +555,11 @@ compile(Root *roots) {
533 printf("%%define FIXNUM_SHIFT %zu\n", FIXNUM_SHIFT); 555 printf("%%define FIXNUM_SHIFT %zu\n", FIXNUM_SHIFT);
534 printf("%%define PAIR_MASK %zu\n", PAIR_MASK); 556 printf("%%define PAIR_MASK %zu\n", PAIR_MASK);
535 printf("%%define PAIR_TAG %zu\n", PAIR_TAG); 557 printf("%%define PAIR_TAG %zu\n", PAIR_TAG);
536 printf("%%define STRING_INV_MASK %zu\n", STRING_INV_MASK); 558 printf("%%define PTR_MASK %zu\n", PTR_MASK);
537 printf("%%define STRING_MASK %zu\n", STRING_MASK); 559 printf("%%define STRING_MASK %zu\n", STRING_MASK);
538 printf("%%define STRING_TAG %zu\n", STRING_TAG); 560 printf("%%define STRING_TAG %zu\n", STRING_TAG);
561 printf("%%define LAMBDA_MASK %zu\n", LAMBDA_MASK);
562 printf("%%define LAMBDA_TAG %zu\n", LAMBDA_TAG);
539 printf("%%define HEAP_SIZE %zu\n", HEAP_SIZE); 563 printf("%%define HEAP_SIZE %zu\n", HEAP_SIZE);
540 printf("\n"); 564 printf("\n");
541 565
@@ -552,6 +576,7 @@ compile(Root *roots) {
552 } 576 }
553 577
554 // Main context. 578 // Main context.
579 printf("alignb 8\n");
555 printf("global _start\n"); 580 printf("global _start\n");
556 printf("_start:\n"); 581 printf("_start:\n");
557 printf(" mov r15, bdl_heap\n"); 582 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:
73 73
74printstring: 74printstring:
75 mov rsi, rdi 75 mov rsi, rdi
76 mov rax, STRING_INV_MASK 76 mov rax, PTR_MASK
77 and rsi, rax 77 and rsi, rax
78 mov rdx, [rsi] 78 mov rdx, [rsi]
79 add rsi, 8 79 add rsi, 8