factor out num construction

This commit is contained in:
janis 2026-06-13 16:36:17 +02:00
parent f9767b9f72
commit 6eeeca1266
Signed by: janis
SSH key fingerprint: SHA256:bB1qbbqmDXZNT0KKD5c2Dfjg53JGhj7B3CFcLIzSqq8

View file

@ -388,20 +388,27 @@ p_eq:
cmovz rax, [rsi + ATOM_TRUE * 8] ; if no pair is unequal, return "t", else return nil cmovz rax, [rsi + ATOM_TRUE * 8] ; if no pair is unequal, return "t", else return nil
ret ret
make_num:
push rdi
mov rdi, 16
mov rsi, 8
call alloc
mov dword [rax], 1 ; refcount = 1
pop rdi
mov qword [rax + 8], rdi ; value = number
mov rdi, rax
mov rsi, OBJ_NUM
call obj_set_tag
ret
;; sum list $rdi in env $rsi ;; sum list $rdi in env $rsi
p_add: p_add:
sub rsp, 24 sub rsp, 24
mov qword [rsp + 8], rsi ; env mov qword [rsp + 8], rsi ; env
call eval_list call eval_list
mov qword [rsp], rax ; evaled list mov qword [rsp], rax ; evaled list
mov rdi, 16 mov rdi, 0
mov rsi, 8 call make_num
call alloc
mov rdi, rax
mov dword [rdi], 1 ; refcount = 1
mov qword [rdi + 8], 0 ; value = 0
mov rsi, OBJ_NUM
call obj_set_tag
mov rcx, rax ; acc mov rcx, rax ; acc
mov rdi, qword [rsp] ; $rdi = evaled list mov rdi, qword [rsp] ; $rdi = evaled list
mov rsi, qword [rsp + 8] ; $rsi = env mov rsi, qword [rsp + 8] ; $rsi = env
@ -415,14 +422,8 @@ p_sub:
mov qword [rsp + 8], rsi ; env mov qword [rsp + 8], rsi ; env
call eval_list call eval_list
mov qword [rsp], rax ; evaled list mov qword [rsp], rax ; evaled list
mov rdi, 16 mov rdi, 0
mov rsi, 8 call make_num
call alloc
mov rdi, rax
mov dword [rdi], 1 ; refcount = 1
mov qword [rdi + 8], 0 ; value = 0
mov rsi, OBJ_NUM
call obj_set_tag
mov rcx, rax ; acc mov rcx, rax ; acc
mov rdi, qword [rsp] ; $rdi = evaled list mov rdi, qword [rsp] ; $rdi = evaled list
mov rsi, qword [rsp + 8] ; $rsi = env mov rsi, qword [rsp + 8] ; $rsi = env
@ -436,14 +437,8 @@ p_mul:
mov qword [rsp + 8], rsi ; env mov qword [rsp + 8], rsi ; env
call eval_list call eval_list
mov qword [rsp], rax ; evaled list mov qword [rsp], rax ; evaled list
mov rdi, 16 mov rdi, 1
mov rsi, 8 call make_num
call alloc
mov rdi, rax
mov dword [rdi], 1 ; refcount = 1
mov qword [rdi + 8], 1 ; value = 1
mov rsi, OBJ_NUM
call obj_set_tag
mov rcx, rax ; acc mov rcx, rax ; acc
mov rdi, qword [rsp] ; $rdi = evaled list mov rdi, qword [rsp] ; $rdi = evaled list
mov rsi, qword [rsp + 8] ; $rsi = env mov rsi, qword [rsp + 8] ; $rsi = env
@ -479,17 +474,13 @@ p_rem:
p_car: p_car:
call eval_list call eval_list
mov rdi, rax mov rdi, rax
call get_car call get_caar
mov rdi, rax
call get_car
ret ret
p_cdr: p_cdr:
call eval_list call eval_list
mov rdi, rax mov rdi, rax
call get_car call get_cadr
mov rdi, rax
call get_cdr
ret ret
p_not: p_not:
@ -608,9 +599,7 @@ p_let_star_inner:
mov qword [rsp], rdi ; acc-env mov qword [rsp], rdi ; acc-env
mov qword [rsp + 8], rsi ; (var val) mov qword [rsp + 8], rsi ; (var val)
mov rdi, rsi mov rdi, rsi
call get_cdr call get_cdar
mov rdi, rax
call get_car
mov rdi, rax mov rdi, rax
mov rsi, qword [rsp] ; acc-env mov rsi, qword [rsp] ; acc-env
call eval call eval
@ -630,9 +619,7 @@ p_let:
sub rsp, 24 sub rsp, 24
call get_car call get_car
mov qword [rsp], rax ; bindings mov qword [rsp], rax ; bindings
call get_cdr call get_cdar
mov rdi, rax
call get_car
mov qword [rsp + 8], rax ; body mov qword [rsp + 8], rax ; body
mov qword [rsp + 16], rsi ; in_env mov qword [rsp + 16], rsi ; in_env
mov rdi, qword [rsp] ; $rdi = bindings mov rdi, qword [rsp] ; $rdi = bindings
@ -650,9 +637,7 @@ p_let_star:
sub rsp, 24 sub rsp, 24
call get_car call get_car
mov qword [rsp], rax ; bindings mov qword [rsp], rax ; bindings
call get_cdr call get_cdar
mov rdi, rax
call get_car
mov qword [rsp + 8], rax ; body mov qword [rsp + 8], rax ; body
mov qword [rsp + 16], rsi ; in_env mov qword [rsp + 16], rsi ; in_env
mov rdi, qword [rsp] ; $rdi = bindings mov rdi, qword [rsp] ; $rdi = bindings
@ -700,6 +685,8 @@ p_str_concat:
p_str_append: p_str_append:
;; return the nth element of a string ;; return the nth element of a string
p_str_nth: p_str_nth:
;; sets the nth element of a string to a char
p_set_nth:
;; returns a new string that is a substring from a a+b of the input string ;; returns a new string that is a substring from a a+b of the input string
;; (substr "hello world" 0 5) -> "hello" ;; (substr "hello world" 0 5) -> "hello"
p_str_substr: p_str_substr: