spaces to tabs for asm files

This commit is contained in:
nub31
2025-02-02 22:43:40 +01:00
parent d318c99792
commit c0798eff3d
6 changed files with 178 additions and 173 deletions

View File

@@ -0,0 +1,5 @@
<component name="ProjectCodeStyleConfiguration">
<state>
<option name="PREFERRED_PROJECT_CODE_STYLE" value="Default" />
</state>
</component>

View File

@@ -1,167 +1,167 @@
global gc_init, gc_alloc global gc_init, gc_alloc
section .bss section .bss
alloc_list: resq 1 alloc_list: resq 1
stack_start: resq 1 stack_start: resq 1
section .data section .data
gc_threshold_b: dq 4096 ; default of 4096 bytes, this will scale when gc_collect is ran gc_threshold_b: dq 4096 ; default of 4096 bytes, this will scale when gc_collect is ran
gc_threshold_c: dq 1024 ; default of 1024 allocations gc_threshold_c: dq 1024 ; default of 1024 allocations
total_alloc_b: dq 0 ; counts the allocated bytes total_alloc_b: dq 0 ; counts the allocated bytes
total_alloc_c: dq 0 ; count the amount of allocations total_alloc_c: dq 0 ; count the amount of allocations
section .text section .text
gc_init: gc_init:
mov [stack_start], rsp mov [stack_start], rsp
ret ret
gc_alloc: gc_alloc:
add rdi, 17 ; add space for metadata add rdi, 17 ; add space for metadata
mov rdx, [total_alloc_b] ; load total allocations in bytes mov rdx, [total_alloc_b] ; load total allocations in bytes
cmp rdx, [gc_threshold_b] ; has total exceeded threshold? cmp rdx, [gc_threshold_b] ; has total exceeded threshold?
jae .collect ; yes? run gc jae .collect ; yes? run gc
mov rdx, [total_alloc_c] ; load total allocation count mov rdx, [total_alloc_c] ; load total allocation count
cmp rdx, [gc_threshold_c] ; has count exceeded threshold? cmp rdx, [gc_threshold_c] ; has count exceeded threshold?
jae .collect ; yes? run gc jae .collect ; yes? run gc
jmp .collect_end jmp .collect_end
.collect: .collect:
push rdi push rdi
call gc_collect call gc_collect
pop rdi pop rdi
.collect_end: .collect_end:
add [total_alloc_b], rdi ; update total allocated bytes add [total_alloc_b], rdi ; update total allocated bytes
inc qword [total_alloc_c] ; update total allocation count inc qword [total_alloc_c] ; update total allocation count
push rdi push rdi
call sys_mmap ; allocate size + metadata call sys_mmap ; allocate size + metadata
pop rdi pop rdi
mov byte [rax], 0 ; set mark to 0 mov byte [rax], 0 ; set mark to 0
mov qword [rax + 1], rdi ; set total size of object (including metadata) mov qword [rax + 1], rdi ; set total size of object (including metadata)
mov rsi, [alloc_list] ; load first item in allocation list mov rsi, [alloc_list] ; load first item in allocation list
mov qword [rax + 9], rsi ; make current head of allocation list the next item in this object mov qword [rax + 9], rsi ; make current head of allocation list the next item in this object
mov [alloc_list], rax ; update head of allocation list so it points to this object mov [alloc_list], rax ; update head of allocation list so it points to this object
add rax, 17 ; skip metadata for return value add rax, 17 ; skip metadata for return value
ret ret
gc_collect: gc_collect:
call gc_mark_stack call gc_mark_stack
call gc_sweep call gc_sweep
mov qword [total_alloc_c], 0 ; reset allocation count mov qword [total_alloc_c], 0 ; reset allocation count
mov rdi, [total_alloc_b] ; since we just swept, all the memory is in use mov rdi, [total_alloc_b] ; since we just swept, all the memory is in use
shl rdi, 1 ; double the currently used memory shl rdi, 1 ; double the currently used memory
mov rsi, 4096 mov rsi, 4096
call max ; get the largest of total_alloc_b * 2 and 4096 call max ; get the largest of total_alloc_b * 2 and 4096
mov qword [gc_threshold_b], rax ; update threshold to new value mov qword [gc_threshold_b], rax ; update threshold to new value
ret ret
gc_mark_stack: gc_mark_stack:
mov r8, rsp ; load current stack pointer mov r8, rsp ; load current stack pointer
mov r9, [stack_start] ; load start of stack mov r9, [stack_start] ; load start of stack
.loop: .loop:
cmp r8, r9 ; have we reached end of stack? cmp r8, r9 ; have we reached end of stack?
ja .done ; yes? return ja .done ; yes? return
mov rdi, [r8] ; no? load the value mov rdi, [r8] ; no? load the value
call gc_mark ; this might be an allocation, check call gc_mark ; this might be an allocation, check
add r8, 8 ; next item in stack add r8, 8 ; next item in stack
jmp .loop jmp .loop
.done: .done:
ret ret
gc_mark: gc_mark:
test rdi, rdi ; is input null? test rdi, rdi ; is input null?
jz .done ; yes? return jz .done ; yes? return
mov rsi, [alloc_list] ; load start of allocation list mov rsi, [alloc_list] ; load start of allocation list
.loop: .loop:
test rsi, rsi ; reached end of list? test rsi, rsi ; reached end of list?
jz .done ; yes? return jz .done ; yes? return
lea rdx, [rsi + 17] lea rdx, [rsi + 17]
cmp rdx, rdi ; no? is this the input object? cmp rdx, rdi ; no? is this the input object?
je .mark_object ; yes? mark it je .mark_object ; yes? mark it
mov rsi, [rsi + 9] ; no? next item mov rsi, [rsi + 9] ; no? next item
jmp .loop jmp .loop
.mark_object: .mark_object:
mov al, [rdi] ; load mark mov al, [rdi] ; load mark
test al, al ; already marked? test al, al ; already marked?
jnz .done ; yes? return jnz .done ; yes? return
mov byte [rdi - 17], 1 ; mark object mov byte [rdi - 17], 1 ; mark object
mov rcx, [rdi + 1] ; load object size mov rcx, [rdi + 1] ; load object size
mov rdx, rdi ; start of data mov rdx, rdi ; start of data
add rcx, rdx ; end of data add rcx, rdx ; end of data
.scan_object: .scan_object:
cmp rdx, rcx ; done scanning? cmp rdx, rcx ; done scanning?
jae .done ; yes? return jae .done ; yes? return
mov rdi, [rdx] ; load value mov rdi, [rdx] ; load value
call gc_mark call gc_mark
add rdx, 8 ; next object add rdx, 8 ; next object
jmp .scan_object jmp .scan_object
.done: .done:
ret ret
gc_sweep: gc_sweep:
mov rdi, [alloc_list] mov rdi, [alloc_list]
xor rsi, rsi xor rsi, rsi
.loop: .loop:
test rdi, rdi ; reached end of list? test rdi, rdi ; reached end of list?
jz .done ; yes? return jz .done ; yes? return
mov al, [rdi] mov al, [rdi]
test al, al ; is object marked? test al, al ; is object marked?
jz .free ; no? free it jz .free ; no? free it
mov byte [rdi], 0 ; yes? clear mark for next marking mov byte [rdi], 0 ; yes? clear mark for next marking
mov rsi, rdi mov rsi, rdi
mov rdi, [rdi + 9] ; load the next object in the list mov rdi, [rdi + 9] ; load the next object in the list
jmp .loop ; repeat jmp .loop ; repeat
.free: .free:
mov rdx, [rdi + 9] ; save address of next object in list mov rdx, [rdi + 9] ; save address of next object in list
test rsi, rsi test rsi, rsi
jz .remove_head jz .remove_head
mov [rsi + 9], rdx ; unlink the current node by setting the previous node's next to the next node's address mov [rsi + 9], rdx ; unlink the current node by setting the previous node's next to the next node's address
jmp .free_memory jmp .free_memory
.remove_head: .remove_head:
mov [alloc_list], rdx ; update head node to be the next node mov [alloc_list], rdx ; update head node to be the next node
.free_memory: .free_memory:
push rsi ; save previous node since it will also be the previous node for the next item push rsi ; save previous node since it will also be the previous node for the next item
push rdx ; save next node push rdx ; save next node
mov rsi, [rdi + 1] ; get length of the object mov rsi, [rdi + 1] ; get length of the object
sub [total_alloc_b], rsi ; remove this allocation from total allocations sub [total_alloc_b], rsi ; remove this allocation from total allocations
call sys_munmap ; free the memory call sys_munmap ; free the memory
pop rdi ; input for next iteration pop rdi ; input for next iteration
pop rsi ; prev node for next iteration pop rsi ; prev node for next iteration
jmp .loop jmp .loop
.done: .done:
ret ret
sys_mmap: sys_mmap:
mov rax, 9 mov rax, 9
mov rsi, rdi mov rsi, rdi
mov rdi, 0 mov rdi, 0
mov rdx, 3 mov rdx, 3
mov r10, 34 mov r10, 34
mov r8, -1 mov r8, -1
mov r9, 0 mov r9, 0
syscall syscall
cmp rax, -1 cmp rax, -1
je .error je .error
ret ret
.error: .error:
mov rax, 60 mov rax, 60
mov rdi, 1 mov rdi, 1
syscall syscall
sys_munmap: sys_munmap:
mov rax, 11 mov rax, 11
syscall syscall
cmp rax, -1 cmp rax, -1
je .error je .error
ret ret
.error: .error:
mov rax, 60 mov rax, 60
mov rdi, 1 mov rdi, 1
syscall syscall
max: max:
cmp rdi, rsi cmp rdi, rsi
jae .left jae .left
mov rax, rsi mov rax, rsi
ret ret
.left: .left:
mov rax, rdi mov rax, rdi
ret ret

View File

@@ -2,19 +2,19 @@ global str_cmp
section .text section .text
str_cmp: str_cmp:
xor rdx, rdx xor rdx, rdx
.loop: .loop:
mov al, [rsi + rdx] mov al, [rsi + rdx]
mov bl, [rdi + rdx] mov bl, [rdi + rdx]
inc rdx inc rdx
cmp al, bl cmp al, bl
jne .not_equal jne .not_equal
cmp al, 0 cmp al, 0
je .equal je .equal
jmp .loop jmp .loop
.not_equal: .not_equal:
mov rax, 0 mov rax, 0
ret ret
.equal: .equal:
mov rax, 1 mov rax, 1
ret ret

View File

@@ -2,5 +2,5 @@ global arr_size
section .text section .text
arr_size: arr_size:
mov rax, [rdi] mov rax, [rdi]
ret ret

View File

@@ -1,23 +1,23 @@
section .bss section .bss
buffer resb 20 buffer resb 20
section .text section .text
global itoa global itoa
itoa: itoa:
mov rax, rdi mov rax, rdi
mov rsi, buffer + 19 mov rsi, buffer + 19
mov byte [rsi], 0 mov byte [rsi], 0
dec rsi dec rsi
.loop: .loop:
xor rdx, rdx xor rdx, rdx
mov rcx, 10 mov rcx, 10
div rcx div rcx
add dl, '0' add dl, '0'
mov [rsi], dl mov [rsi], dl
dec rsi dec rsi
test rax, rax test rax, rax
jnz .loop jnz .loop
inc rsi inc rsi
mov rax, rsi mov rax, rsi
ret ret

View File

@@ -2,12 +2,12 @@ global str_len
section .text section .text
str_len: str_len:
xor rax, rax xor rax, rax
.loop: .loop:
cmp byte [rdi], 0 cmp byte [rdi], 0
jz .done jz .done
inc rax inc rax
inc rdi inc rdi
jmp .loop jmp .loop
.done: .done:
ret ret