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