diff options
author | Bad Diode <bd@badd10de.dev> | 2021-11-10 17:30:35 +0100 |
---|---|---|
committer | Bad Diode <bd@badd10de.dev> | 2021-11-10 17:30:35 +0100 |
commit | 45920c8340d2aa5a3c40a3c8aad519fa998bb50b (patch) | |
tree | 9e1534aa4d655b00fb2200cdbd20392948463c68 | |
parent | 09406d74711fd241a16f4e0d4cc76bfa11e42a60 (diff) | |
download | bdl-45920c8340d2aa5a3c40a3c8aad519fa998bb50b.tar.gz bdl-45920c8340d2aa5a3c40a3c8aad519fa998bb50b.zip |
Add locals for lambda procedures
-rw-r--r-- | src/compiler.h | 12 |
1 files changed, 9 insertions, 3 deletions
diff --git a/src/compiler.h b/src/compiler.h index 023a039..14c6954 100644 --- a/src/compiler.h +++ b/src/compiler.h | |||
@@ -461,6 +461,8 @@ compile_lambda(Object *obj) { | |||
461 | 461 | ||
462 | // Create a new compilation context. | 462 | // Create a new compilation context. |
463 | char *prev_context = current_context; | 463 | char *prev_context = current_context; |
464 | Environment *prev_env = current_env; | ||
465 | current_env = obj->env; | ||
464 | current_context = NULL; | 466 | current_context = NULL; |
465 | array_init(current_context, 0); | 467 | array_init(current_context, 0); |
466 | 468 | ||
@@ -470,6 +472,7 @@ compile_lambda(Object *obj) { | |||
470 | 472 | ||
471 | // Initialize function call frame. | 473 | // Initialize function call frame. |
472 | context_printf(" push rbp\n"); | 474 | context_printf(" push rbp\n"); |
475 | context_printf(" sub rsp, %zu\n", 8 * array_size(current_env->locals)); | ||
473 | context_printf(" mov rbp, rsp\n"); | 476 | context_printf(" mov rbp, rsp\n"); |
474 | 477 | ||
475 | // Procedure body. | 478 | // Procedure body. |
@@ -482,6 +485,7 @@ compile_lambda(Object *obj) { | |||
482 | 485 | ||
483 | // Restore the previous call frame. | 486 | // Restore the previous call frame. |
484 | context_printf(" mov rsp, rbp\n"); | 487 | context_printf(" mov rsp, rbp\n"); |
488 | context_printf(" add rsp, %zu\n", 8 * array_size(current_env->locals)); | ||
485 | context_printf(" pop rbp\n"); | 489 | context_printf(" pop rbp\n"); |
486 | context_printf(" ret\n"); | 490 | context_printf(" ret\n"); |
487 | context_printf("\n"); | 491 | context_printf("\n"); |
@@ -489,6 +493,7 @@ compile_lambda(Object *obj) { | |||
489 | // Restore previous compilation context. | 493 | // Restore previous compilation context. |
490 | array_push(procedures, current_context); | 494 | array_push(procedures, current_context); |
491 | current_context = prev_context; | 495 | current_context = prev_context; |
496 | current_env = prev_env; | ||
492 | 497 | ||
493 | // Create tagged pointer with this lambda procedure. | 498 | // Create tagged pointer with this lambda procedure. |
494 | context_printf(" mov rax, %s\n", name); | 499 | context_printf(" mov rax, %s\n", name); |
@@ -512,7 +517,8 @@ compile_def(Object *obj) { | |||
512 | compile_object(obj->var_expr); | 517 | compile_object(obj->var_expr); |
513 | ssize_t idx = find_local_index(current_env->locals, obj->var_name); | 518 | ssize_t idx = find_local_index(current_env->locals, obj->var_name); |
514 | context_printf(" pop rax\n"); | 519 | context_printf(" pop rax\n"); |
515 | context_printf(" mov [rbp + %ld], rax\n", 8 * (idx + 1)); | 520 | context_printf(" mov [rbp + %ld], rax\n", 8 * idx); |
521 | compile_nil(); | ||
516 | context_printf(" ;; <-- compile_def\n"); | 522 | context_printf(" ;; <-- compile_def\n"); |
517 | } | 523 | } |
518 | 524 | ||
@@ -520,7 +526,7 @@ void | |||
520 | compile_symbol(Object *obj) { | 526 | compile_symbol(Object *obj) { |
521 | context_printf(" ;; --> compile_symbol\n"); | 527 | context_printf(" ;; --> compile_symbol\n"); |
522 | ssize_t idx = find_local_index(current_env->locals, obj); | 528 | ssize_t idx = find_local_index(current_env->locals, obj); |
523 | context_printf(" mov rax, [rbp + %ld]\n", 8 * (idx + 1)); | 529 | context_printf(" mov rax, [rbp + %ld]\n", 8 * idx); |
524 | context_printf(" push rax\n"); | 530 | context_printf(" push rax\n"); |
525 | context_printf(" ;; <-- compile_symbol\n"); | 531 | context_printf(" ;; <-- compile_symbol\n"); |
526 | } | 532 | } |
@@ -632,7 +638,7 @@ compile(Program program) { | |||
632 | printf(" mov r15, bdl_heap\n"); | 638 | printf(" mov r15, bdl_heap\n"); |
633 | 639 | ||
634 | // Initialize main locals. | 640 | // Initialize main locals. |
635 | printf(" sub rsp, %zu\n", 8 * array_size(program.env->locals)); | 641 | printf(" sub rsp, %zu\n", 8 * array_size(current_env->locals)); |
636 | printf(" mov rbp, rsp\n"); | 642 | printf(" mov rbp, rsp\n"); |
637 | 643 | ||
638 | // Keep the bottom stack value set as NIL_VAL for a default return value. | 644 | // Keep the bottom stack value set as NIL_VAL for a default return value. |