diff options
author | Bad Diode <bd@badd10de.dev> | 2021-11-15 22:33:53 +0100 |
---|---|---|
committer | Bad Diode <bd@badd10de.dev> | 2021-11-15 22:33:53 +0100 |
commit | 62984fb580b9e355cf9eacf9de53bff8c895c0b5 (patch) | |
tree | 913df113b13d66328262dc5075f2b9925db46c4a /src/compiler.h | |
parent | 26f1b9c35d337c0814158077fdc8f56b817e0b14 (diff) | |
download | bdl-62984fb580b9e355cf9eacf9de53bff8c895c0b5.tar.gz bdl-62984fb580b9e355cf9eacf9de53bff8c895c0b5.zip |
Add initial boilerplate for closure capture
Diffstat (limited to 'src/compiler.h')
-rw-r--r-- | src/compiler.h | 20 |
1 files changed, 16 insertions, 4 deletions
diff --git a/src/compiler.h b/src/compiler.h index 1312d99..e91798a 100644 --- a/src/compiler.h +++ b/src/compiler.h | |||
@@ -508,18 +508,30 @@ compile_lambda(Object *obj) { | |||
508 | current_context = prev_context; | 508 | current_context = prev_context; |
509 | current_env = prev_env; | 509 | current_env = prev_env; |
510 | 510 | ||
511 | // Create tagged pointer with this lambda procedure. | 511 | // Add function address. |
512 | context_printf(" mov rax, %s\n", name); | 512 | context_printf(" mov rax, %s\n", name); |
513 | context_printf(" mov [r15], rax\n"); | 513 | context_printf(" mov [r15], rax\n"); |
514 | context_printf(" mov rax, %ld\n", array_size(obj->env->captured)); | ||
515 | context_printf(" mov [r15 + 8], rax\n"); | ||
516 | |||
517 | // Add captured variables to the heap. | ||
518 | for (size_t i = 0; i < array_size(obj->env->captured); i++) { | ||
519 | ssize_t idx = find_var_index(current_env->locals, obj->env->captured[i]); | ||
520 | context_printf(" mov rax, [rbp + %ld]\n", 8 * idx); | ||
521 | context_printf(" mov [r15 + %ld], rax\n", 8 * (i + 2)); | ||
522 | // TODO: What about capturing captured variables or parameters? | ||
523 | assert(idx != -1 && "unexpected index"); | ||
524 | } | ||
525 | |||
526 | // Create tagged pointer with this lambda procedure. | ||
514 | context_printf(" mov rax, r15\n"); | 527 | context_printf(" mov rax, r15\n"); |
515 | context_printf(" or rax, %zu\n", LAMBDA_TAG); | 528 | context_printf(" or rax, %zu\n", LAMBDA_TAG); |
516 | 529 | ||
517 | // Push compiled object to the stack. | 530 | // Push compiled object to the stack. |
518 | context_printf(" push rax\n"); | 531 | context_printf(" push rax\n"); |
519 | 532 | ||
520 | // TODO: When we add closed variables they will be stored here, for now just | 533 | // Adjust the heap pointer depending on the number of variables captured. |
521 | // incrementing by a 64 bit pointer size, as that is what we are storing. | 534 | context_printf(" add r15, %ld\n", 8 * (array_size(obj->env->captured) + 2)); |
522 | context_printf(" add r15, 8\n"); | ||
523 | 535 | ||
524 | context_printf(" ;; <-- compile_lambda\n"); | 536 | context_printf(" ;; <-- compile_lambda\n"); |
525 | } | 537 | } |