From 52cfc7dc5480eb39c111df692c9a7585acdf9bea Mon Sep 17 00:00:00 2001 From: janis Date: Mon, 3 Nov 2025 15:16:18 +0100 Subject: [PATCH] yippie? --- lang/src/codegen.asm | 196 +++++++++++++++++++++++++++++++++++++----- lang/tests/codegen.rs | 2 +- 2 files changed, 176 insertions(+), 22 deletions(-) diff --git a/lang/src/codegen.asm b/lang/src/codegen.asm index 1146f5d..142f2b7 100644 --- a/lang/src/codegen.asm +++ b/lang/src/codegen.asm @@ -254,8 +254,8 @@ stackvar_cmp: push rbp mov rbp, rsp - mov rax, [rdi + 0] ; a.index - mov rcx, [rsi + 0] ; b.index + mov rax, [rsi + 0] ; a.index + mov rcx, [rdx + 0] ; b.index cmp rax, 0 jl .less jg .greater @@ -624,8 +624,8 @@ codegen_function: mov rdi, [rsp] ; ctx lea rdi, [rdi + 8] ; &ctx.text - lea rsi, [rsp + 104] ; src - lea rdx, [rsp + 80] ; dst + lea rsi, [rsp + 80] ; dst + lea rdx, [rsp + 104] ; src call codegen_move_dst_src inc r14 @@ -1368,11 +1368,178 @@ codegen_expr: jmp .done .place_to_value: -.assignment: + ; codegen inner expr + mov rdi, [rsp] ; ctx + mov rsi, [rsp + 8] ; &function_ctx + mov rdx, [rax + 8] ; AstNode.data + call codegen_expr + mov [rsp + 16], rax + mov [rsp + 24], rdx + + cmp al, OPERAND_RBP_OFFSET + je .ptv_rbp_offset + cmp al, OPERAND_ADDRESS_PLACE + je .ptv_address_place + cmp al, OPERAND_REGISTER_PLACE + je .ptv_register_place + cmp al, OPERAND_RBP_PLACE + je .ptv_rbp_place + +.ptv_rbp_offset: + mov byte [rsp + 16], OPERAND_RBP_PVALUE + jmp .ptv_done + +.ptv_address_place: + mov byte [rsp + 16], OPERAND_ADDRESS_VALUE + jmp .ptv_done + +.ptv_register_place: + mov rax, [rsp + 16] + mov rdx, [rsp + 24] + mov [rsp + 32], rax + mov [rsp + 40], rdx + + ; mov rdx, [rdx] + mov byte [rsp + 16], OPERAND_REGISTER + mov rdi, [rsp] ; ctx + lea rdi, [rdi + 8] ; &ctx.text + lea rsi, [rsp + 16] ; dst + lea rdx, [rsp + 32] ; src + mov rcx, 'mov ' + call codegen_binary_op_unchecked + + + jmp .ptv_done + +.ptv_rbp_place: + ; mov rax, [src] + ; mov rax, [rax] + ; mov [src], rax + + mov rdi, [rsp] + lea rdi, [rdi + 8] ; &ctx.text + lea rsi, [rel OPERAND_RAX] ; dst + lea rdx, [rsp + 16] ; src + call codegen_move_dst_src + + mov rdi, [rsp] ; ctx + lea rdi, [rdi + 8] ; &ctx.text + lea rsi, [rel OPERAND_RAX] ; rax + lea rdx, [rel OPERAND_RAX_P] ; [rax] + + mov byte [rsp + 16], OPERAND_RBP_VALUE + + mov rdi, [rsp] ; ctx + lea rdi, [rdi + 8] ; &ctx.text + lea rsi, [rsp + 16] ; dst + lea rdx, [rel OPERAND_RAX_P] ; rax + jmp .ptv_done + +.ptv_done: + mov rax, [rsp + 16] + mov rdx, [rsp + 24] + jmp .done + .value_to_place: + ; codegen inner expr + mov rdi, [rsp] ; ctx + mov rsi, [rsp + 8] ; &function_ctx + mov rdx, [rax + 8] ; AstNode.data + call codegen_expr + mov [rsp + 16], rax + mov [rsp + 24], rdx + + cmp al, OPERAND_RBP_PVALUE + je .vtp_rbp_pvalue + cmp al, OPERAND_ADDRESS_VALUE + je .vtp_address_value + cmp al, OPERAND_LAST_VALUE + jg .panic + + ; dst = allocate_place + mov rdi, [rsp + 8] ; &function_ctx + movzx rsi, word [rsp + 16 + 2] ; width + call codegen_allocate_place + mov [rsp + 32], rax + mov [rsp + 40], rdx + + ; mov dst, src + mov rdi, [rsp] ; ctx + lea rdi, [rdi + 8] ; &ctx.text + lea rsi, [rsp + 32] ; dst + lea rdx, [rsp + 16] ; src + call codegen_move_dst_src + + ; free src + mov rdi, [rsp + 8] ; &function_ctx + lea rsi, [rsp + 16] ; src + call codegen_free_operand + + ; return dst + mov rax, [rsp + 32] + mov rdx, [rsp + 40] + jmp .done + +.vtp_rbp_pvalue: + mov byte [rsp + 16], OPERAND_RBP_OFFSET + jmp .vtp_done +.vtp_address_value: + mov byte [rsp + 16], OPERAND_ADDRESS_PLACE + jmp .vtp_done + +.vtp_done: + mov rax, [rsp + 16] + mov rdx, [rsp + 24] + jmp .done + +.assignment: + mov rcx, [rax + 8] ; AstNode.data + mov rdx, [rax + 16] ; AstNode.extra + mov [rsp + 16], rcx ; dst + mov [rsp + 24], rdx ; src + + ; codegen src + mov rdi, [rsp] ; ctx + mov rsi, [rsp + 8] ; &function_ctx + call codegen_expr + mov [rsp + 32], rax ; src + mov [rsp + 40], rdx + + mov rdi, [rsp] ; ctx + mov rsi, [rsp + 8] ; &function_ctx + mov rdx, [rsp + 16] ; dst + call codegen_expr + mov [rsp + 48], rax ; dst + mov [rsp + 56], rdx + + ; mov dst, src + mov rdi, [rsp] ; ctx + lea rdi, [rdi + 8] ; &ctx.text + lea rsi, [rsp + 48] ; dst + lea rdx, [rsp + 32] ; src + call codegen_move_dst_src + + ; free [dst, src] + mov rdi, [rsp + 8] ; &function_ctx + lea rsi, [rsp + 48] ; dst + call codegen_free_operand + + mov rdi, [rsp + 8] ; &function_ctx + lea rsi, [rsp + 48] ; src + call codegen_free_operand + + xor rax, rax + xor rdx, rdx + jmp .done + .deref: .address_of: - ; TODO + ; codegen inner expr + mov rdi, [rsp] ; ctx + mov rsi, [rsp + 8] ; &function_ctx + mov rdx, [rax + 8] ; AstNode.data + call codegen_expr + jmp .done .done: add rsp, 80 @@ -1410,9 +1577,9 @@ section .rdata OPERAND_RBP_OFFSET equ 6 ; e.g. mov [rbp - 8], rax or lea rax, [rbp - 8] ; Address of a static-slot containing a Value OPERAND_ADDRESS_PLACE equ 7 ; e.g. lea [rel OPERAND_ADDRESS] - ; Register containing an address (Place) + ; Register containing an address pointing at a Value (Place) OPERAND_REGISTER_PLACE equ 8 ; e.g. [rax] - ; RBP-relative stack-slot containing an address (Place) + ; RBP-relative stack-slot containing an address pointing at a Value (Place) OPERAND_RBP_PLACE equ 9 ; e.g. mov rax, [rbp - 8]; mov [rax], src OPERAND_LAST_PLACE equ 10 ; operand kinds > this are not memory operands ; Immediate constant Value @@ -1537,25 +1704,12 @@ codegen_write_operand: call vec_push ; if op.kind == OPERAND_RBP_OFFSET - mov rax, [rsp + 8] ; op - mov al, byte [rax + 0] ; Operand.kind - cmp al, OPERAND_RBP_OFFSET - jne .rsp_offset_write mov qword [rsp + 16], 'rbp ' mov rdi, [rsp] ; *text lea rsi, [rsp + 16] mov rdx, 4 call vec_extend - jmp .check_sign -.rsp_offset_write: - mov qword [rsp + 16], 'rsp ' - mov rdi, [rsp] ; *text - lea rsi, [rsp + 16] - mov rdx, 4 - call vec_extend - -.check_sign: ; if value >= 0 mov rax, [rsp + 8] ; op mov rdi, [rax + 8] ; op.value diff --git a/lang/tests/codegen.rs b/lang/tests/codegen.rs index ff9c270..0188e24 100644 --- a/lang/tests/codegen.rs +++ b/lang/tests/codegen.rs @@ -93,7 +93,7 @@ fn main() { print_ast( b"fn main(a: u32) -> void { - return 2 * 3 + 4 * 5; + return 2 * 3 + 4 * 5 + a; }", |ast| unsafe { parse_func(ast) }, );