From 2ca60ace5981fbdeace1d902c573025c8a46edc2 Mon Sep 17 00:00:00 2001 From: janis Date: Sat, 1 Nov 2025 03:01:48 +0100 Subject: [PATCH] codegen working? pogU? --- lang/src/codegen.asm | 54 +++++++++++++++++++++++++------------------- 1 file changed, 31 insertions(+), 23 deletions(-) diff --git a/lang/src/codegen.asm b/lang/src/codegen.asm index 349abbe..9e74725 100644 --- a/lang/src/codegen.asm +++ b/lang/src/codegen.asm @@ -21,11 +21,9 @@ section .rdata GLOBAL_LEN equ $ - GLOBAL_ COLON_NL db ":", 10 COLON_NL_LEN equ $ - COLON_NL - RET_NL db "ret", 10 - RET_NL_LEN equ $ - RET_NL PROLOGUE db "push rbp", 10, "mov rbp, rsp", 10 PROLOGUE_LEN equ $ - PROLOGUE - EPILOGUE db "mov rsp, rbp", 10, "pop rbp", 10, "ret", 10 + EPILOGUE db "pop rbp", 10, "ret", 10 EPILOGUE_LEN equ $ - EPILOGUE MOV_RAX db "mov rax, " MOV_RAX_LEN equ $ - MOV_RAX @@ -49,6 +47,8 @@ section .rdata SUB_RSP_LEN equ $ - SUB_RSP ADD_RSP db "add rsp, " ADD_RSP_LEN equ $ - ADD_RSP + BODY db ".body:", 10 + BODY_LEN equ $ - BODY REGISTER_NAMES db "abcdsidibpspr8r9r10r11r12r13r14r15" @@ -359,6 +359,7 @@ codegen_function: bts word [rsp + 72], 7 ; mark rsp as used bts word [rsp + 72], 6 ; mark rbp as used bts word [rsp + 72], 0 ; mark rax as used + mov word [rsp + 76], 0 ; dirtied_register_bitset = 0 ; push "section .text\n" mov rdi, [rsp] ; ctx @@ -442,9 +443,9 @@ codegen_function: mov [rsp + 104], rsi ; scratch mov rax, [rsp + 24] ; current_stack_size - mov [rsp + 112], rax add rax, 8 ; size_of:: mov [rsp + 24], rax ; current_stack_size += size_of:: + mov [rsp + 112], rax lea rdi, [rsp + 32] ; stack-vars lea rsi, [rsp + 104] ; &scratch: &(index, offset) mov rdx, stackvar_cmp @@ -472,6 +473,12 @@ codegen_function: ; "jmp .body\n" ; ".body:\n" + mov rdi, [rsp] ; ctx + lea rdi, [rdi + 8] ; &ctx.text + lea rsi, [rel BODY] + mov rdx, BODY_LEN + call vec_extend + mov rdi, [rsp] ; ctx lea rsi, [rsp + 24] ; &function_ctx @@ -491,7 +498,7 @@ codegen_function: mov rdi, [rsp] ; ctx lea rdi, [rdi + 8] ; &ctx.text lea rsi, [rsp + 24] ; &function_ctx - mov rdx, 0 ; push = false + mov rdx, 1 ; pop = true call codegen_push_pop_dirtied_registers ; "add rsp, {current_stack_size}\n" @@ -508,13 +515,6 @@ codegen_function: mov rcx, 10 ; radix call int_to_str2 - ; ; push "\n" - ; mov rdi, [rsp] ; ctx - ; lea rdi, [rdi + 8] ; &ctx.text - ; lea rsi, [rel COLON_NL] - ; inc rsi - ; call vec_push - mov rdi, [rsp] ; ctx lea rdi, [rdi + 8] ; &ctx.text lea rsi, [rax + rdx] @@ -523,11 +523,19 @@ codegen_function: mov rdx, rdx ; length from int_to_str2 call vec_extend + ; push "\n" + mov rdi, [rsp] ; ctx + lea rdi, [rdi + 8] ; &ctx.text + lea rsi, [rel COLON_NL] + inc rsi + call vec_push + + ; "pop rbp\n" ; "ret\n" mov rdi, [rsp] ; ctx lea rdi, [rdi + 8] ; &ctx.text - lea rsi, [rel RET_NL] - mov rdx, RET_NL_LEN + lea rsi, [rel EPILOGUE] + mov rdx, EPILOGUE_LEN call vec_extend ; ".prologue:\n" @@ -551,13 +559,6 @@ codegen_function: mov rcx, 10 ; radix call int_to_str2 - ; ; push "\n" - ; mov rdi, [rsp] ; ctx - ; lea rdi, [rdi + 8] ; &ctx.text - ; lea rsi, [rel COLON_NL] - ; inc rsi - ; call vec_push - mov rdi, [rsp] ; ctx lea rdi, [rdi + 8] ; &ctx.text lea rsi, [rax + rdx] @@ -566,11 +567,18 @@ codegen_function: mov rdx, rdx ; length from int_to_str2 call vec_extend + ; push "\n" + mov rdi, [rsp] ; ctx + lea rdi, [rdi + 8] ; &ctx.text + lea rsi, [rel COLON_NL] + inc rsi + call vec_push + ; "push{dirtied registers}\n" mov rdi, [rsp] ; ctx lea rdi, [rdi + 8] ; &ctx.text lea rsi, [rsp + 24] ; &function_ctx - mov rdx, 1 ; push = false + mov rdx, 0 ; pop = false call codegen_push_pop_dirtied_registers ; "jmp .args\n" @@ -594,7 +602,7 @@ codegen_function: ;; rdi: *text ;; rsi: &function_ctx -;; rdx: push: bool +;; rdx: pop: bool codegen_push_pop_dirtied_registers: push rbp mov rbp, rsp