From 6dff42cfa5cbcbe832107be1fb04d23f6993a2d1 Mon Sep 17 00:00:00 2001 From: janis Date: Sun, 2 Nov 2025 16:49:46 +0100 Subject: [PATCH] var decl/ varref codegen --- lang/src/codegen.asm | 77 +++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 72 insertions(+), 5 deletions(-) diff --git a/lang/src/codegen.asm b/lang/src/codegen.asm index 5cf1f8d..2bafc2f 100644 --- a/lang/src/codegen.asm +++ b/lang/src/codegen.asm @@ -8,6 +8,7 @@ extern vec_extend extern vec_get extern vec_push extern vec_insert_sorted +extern vec_binary_search_by extern vec_insert_many extern vec_init_with extern int_to_str2 @@ -967,8 +968,9 @@ codegen_expr: ; function_ctx: [8..16] ; ctx [0..8] sub rsp, 80 - mov [rsp], rdi ; ctx - mov [rsp + 8], rsi ; &function_ctx + mov [rsp], rdi ; ctx + mov [rsp + 8], rsi ; &function_ctx + mov [rsp + 16], rdx ; expr index mov rdi, [rdi] ; ast mov rsi, rdx ; statement index @@ -1241,12 +1243,13 @@ codegen_expr: lea rdx, [rsp + 64] ; scratch value call codegen_move_dst_src -.after_unspill_rdx: - ; free [scratch, rhs, lhs] + ; free scratch mov rdi, [rsp + 8] ; &function_ctx lea rsi, [rsp + 64] ; scratch value call codegen_free_operand +.after_unspill_rdx: + ; free [rhs, lhs] mov rdi, [rsp + 8] ; &function_ctx lea rsi, [rsp + 48] ; right operand call codegen_free_operand @@ -1286,9 +1289,70 @@ codegen_expr: jmp .done .var_decl: + ; allocate place for variable + mov rdi, [rsp + 8] ; &function_ctx + mov rsi, 8 ; size_of:: + call codegen_allocate_place + mov [rsp + 64], rax ; Operand + mov [rsp + 72], rdx ; Operand.value + neg rdx + mov [rsp + 24], rdx ; &(_, offset) + + mov rdi, [rsp + 8] ; &function_ctx + lea rdi, [rsp + 8] ; stack_vars + lea rsi, [rsp + 16] ; &(index, offset) + mov rdx, stackvar_cmp + mov rcx, 0 + call vec_insert_sorted + + mov rax, [rsp + 64] ; Operand + mov rdx, [rsp + 72] ; Operand.value + jmp .done + .var_ref: -.assignment: + mov rax, [rax + 8] ; AstNode.data + mov rax, [rax] ; variable index + + mov [rsp + 16], rax + mov qword [rsp + 24], 0 + ; lookup variable in stack_vars + + mov rdi, [rsp + 8] ; &function_ctx + lea rdi, [rdi + 8] ; stack_vars + lea rsi, [rsp + 16] ; &(index, offset) + mov rdx, stackvar_cmp + mov rcx, 0 + call vec_binary_search_by + cmp rdx, 1 + je .panic ; variable not found + + mov rdi, [rsp + 8] ; &function_ctx + lea rdi, [rdi + 8] ; stack_vars + mov rsi, rax ; index + call vec_get + mov rdx, [rax + 8] ; offset + neg rdx + mov rax, qword [rel OPERAND_RBP_OFFSET] + mov [rsp + 16], rax + mov [rsp + 24], rdx + + ; allocate value (?) + mov rdi, [rsp + 8] ; &function_ctx + mov rsi, 8 ; width = size_of::<* const()> + call codegen_allocate_value + mov [rsp + 32], rax ; dst + mov [rsp + 40], rdx + + ; lea dst, [rbp - offset] + mov rdi, [rsp] ; ctx + lea rsi, [rdi + 8] ; &ctx.text + mov rdi, [rsp + 8] ; &function_ctx + lea rdx, [rsp + 32] ; dst + lea rcx, [rsp + 16] ; src + mov r8, 'lea ' + call codegen_binary_op_rm64_rm64 .place_to_value: +.assignment: .value_to_place: .deref: .address_of: @@ -1875,4 +1939,7 @@ section .rdata ; Operand { kind: REGISTER, register: 0, width: 8, len: 0, padding: 0, value: 0 } align 8 OPERAND_RAX dq 0x0000_8001, 0 + align 8 OPERAND_RDX dq 0x0000_8301, 0 + align 8 + OPERAND_RBP_OFFSET dq 0x0000_8002, 0