can actually call functions!!!!!!!!!!

This commit is contained in:
janis 2025-11-04 02:12:33 +01:00
parent 22d85c1473
commit fd8a1f74bf
Signed by: janis
SSH key fingerprint: SHA256:bB1qbbqmDXZNT0KKD5c2Dfjg53JGhj7B3CFcLIzSqq8

View file

@ -376,7 +376,7 @@ codegen_allocate_stack_value:
mov rsi, 0xF
not rsi
and rax, rsi ; clear kind to make it a value
and rax, OPERAND_RBP_VALUE ; Operand.kind = OPERAND_RBP_VALUE
or rax, OPERAND_RBP_VALUE ; Operand.kind = OPERAND_RBP_VALUE
pop rbp
ret
@ -995,11 +995,10 @@ codegen_call_expr:
mov rbx, rdx ; copy of *AstCallExpr
; initialize param_operands
mov rcx, [rbx + 16] ; AstCallExpr.args_len
lea rdi, [rsp + 24] ; param_operands
mov rsi, 16 ; size_of::<Operand>
mov rdx, 0 ; drop = None
mov rcx, rbx ; initial capacity
mov rcx, 16 ; initial capacity
call vec_init_with
; codegen callee
@ -1011,16 +1010,16 @@ codegen_call_expr:
mov [rsp + 72], rdx
; If callee is in a param-register, move to stack
cmp byte [rax], OPERAND_REGISTER
cmp al, OPERAND_REGISTER
jne .arg_loop_init
mov rdx, 0b00000011_00111100
movzx rcx, byte [rax + 1] ; callee register
movzx rcx, byte [rsp + 64 + 1] ; callee register
bt rdx, rcx
jnc .arg_loop_init
; allocate stack slot
mov rdi, [rsp + 8] ; &function_ctx
movzx rsi, word [rax + 2] ; Operand.width
movzx rsi, word [rsp + 64 + 2] ; Operand.width
call codegen_allocate_stack_value
mov [rsp + 80], rax ; new stack operand
mov [rsp + 88], rdx
@ -1060,11 +1059,11 @@ codegen_call_expr:
; if the operand is in a register, move it to the stack
; we know already that these are values
cmp byte [rax], OPERAND_REGISTER
cmp byte [rsp + 80], OPERAND_REGISTER
jne .store_param
; allocate stack slot
mov rdi, [rsp + 8] ; &function_ctx
movzx rsi, word [rax + 2] ; Operand.width
movzx rsi, word [rsp + 80 + 2] ; Operand.width
call codegen_allocate_stack_value
mov [rsp + 96], rax ; new stack operand
mov [rsp + 104], rdx
@ -1861,9 +1860,29 @@ codegen_expr:
jmp .done
.var_ref:
mov rax, [rax + 8] ; AstNode.data
mov rax, [rax] ; variable index
mov rbx, [rax + 8] ; AstNode.data
mov rax, [rbx] ; variable index
; check if we're trying to get a reference to a function
mov rdi, [rsp] ; ctx
mov rdi, [rdi] ; ast
mov rsi, rax ; variable index
call vec_get
cmp byte [rax], AST_FUNCTION
jne .var_ref_var
mov rax, [rax + 8] ; *AstFunction
; create label operand for function
mov rcx, [rel OPERAND_LABEL0]
movzx rdx, word [rax + 8] ; AstFunction.name_len
shl rdx, 32
or rcx, rdx
mov rdx, [rax + 0] ; AstFunction.name
mov rax, rcx
jmp .done
.var_ref_var:
mov rax, [rbx]
mov [rsp + 16], rax
mov qword [rsp + 24], 0
; lookup variable in stack_vars
@ -1903,6 +1922,9 @@ codegen_expr:
je .ptv_register_place
cmp al, OPERAND_RBP_PLACE
je .ptv_rbp_place
cmp al, OPERAND_LABEL
je .done
jmp .panic
.ptv_rbp_offset:
mov byte [rsp + 16], OPERAND_RBP_PVALUE
@ -2134,6 +2156,8 @@ section .rdata
OPERAND_RBP_P dq 0x0008_0009, 0
align 8
OPERAND_ZERO dq 0x0001_000b, 0
align 8
OPERAND_LABEL0 dq 0x0008_000d, 0
section .text
@ -2278,8 +2302,7 @@ codegen_write_operand:
mov rax, [rsp + 8] ; op
mov rdi, [rsp] ; *text
mov rsi, [rax + 8] ; Operand.value
mov rdx, [rax + 4] ; Operand.len
mov dx, dx ; low 16 bits
movzx rdx, word [rax + 4] ; Operand.len
call vec_extend
mov byte [rsp + 16], ']'
@ -2307,13 +2330,18 @@ codegen_write_operand:
mov rax, [rsp + 8] ; op
mov rdi, [rsp] ; *text
mov rsi, [rax + 8] ; op.value
mov rdx, [rax + 2] ; op.len
mov dx, dx ; low 16 bits
movzx rdx, word [rax + 4] ; Operand.len
call vec_extend
jmp .epilogue
.label:
jmp .panic
; write address name
mov rax, [rsp + 8] ; op
mov rdi, [rsp] ; *text
mov rsi, [rax + 8] ; Operand.value
movzx rdx, word [rax + 4] ; Operand.len
call vec_extend
jmp .epilogue
.epilogue:
add rsp, 40
@ -2683,7 +2711,10 @@ codegen_move_dst_src:
mov cx, word [rsi + 2] ; dst.width
mov bx, word [rdx + 2] ; src.width
cmp cx, bx
jne .panic ; mismatched widths
jb .panic ; dst.width < src.width
je .width_ok
mov qword [rsp + 24], 'movzx '
.width_ok:
; ; if dst.width == 8 && src.kind == OPERAND_ADDRESS
; xor rbx, rbx
@ -2799,5 +2830,7 @@ section .rdata
CALL_ dq "call "
CALL_LEN equ $ - CALL_
NEWLINE db 10
align 8
MOVZX_ dq "movzx "