libk  Diff

Differences From Artifact [2b600a2718]:

To Artifact [f2e5b4d4ec]:


     1      1   ;; abi definition file for x86 linux 64-bit
     2      2   ; vim: ft=nasm
     3      3   
     4      4   ; syscall64 numbers - syscall table must be created first!
     5      5   %include "calls.x86.lin.64.s"
     6      6   
     7         -; syscall ops
     8         -%define sys.call syscall
            7  +; linux uses the common x86-64 ABI
            8  +%include "x86.syscall.64.s"
     9      9   
    10         -; register order for syscall convention
    11         -%define sys.reg.n 7
    12         -%define sys.reg.ret rax
    13         -%define sys.reg.0 rax
    14         -%define sys.reg.1 rdi
    15         -%define sys.reg.2 rsi
    16         -%define sys.reg.3 rdx
    17         -%define sys.reg.4 r10
    18         -%define sys.reg.5 r8
    19         -%define sys.reg.6 r9
    20         -
    21         -; register order for ccall convention
    22         -%define ccall.reg.ct 6
    23         -%define ccall.reg.ret rdi
    24         -%define ccall.reg.0 rdi
    25         -%define ccall.reg.1 rsi
    26         -%define ccall.reg.2 rdx
    27         -%define ccall.reg.3 rcx
    28         -%define ccall.reg.4 r8
    29         -%define ccall.reg.5 r9
    30         -
    31         -%macro sys 1-8
    32         -; syscall64 wrapper, ex. `sys sys.write, 1, msg, msg.len`
    33         -	%assign i 0
    34         -	%rep %0
    35         -		mov sys.reg. %+ i, %1 ; i'm actually shocked this worked
    36         -		%rotate 1
    37         -		%assign i i+1
    38         -	%endrep 
    39         -	syscall
    40         -%endmacro
    41         -
    42         -%macro sys.prep 1-8
    43         -	; for when we need to modify parameters before we
    44         -	; make the actual call.
    45         -	%assign i 0
    46         -	%rep %0
    47         -		mov sys.reg. %+ i, %1
    48         -		%rotate 1
    49         -		%assign i i+1
    50         -	%endrep
    51         -%endmacro
    52         -
    53         -%macro ccall 1-*
    54         -	%if %0 > ccall.reg.ct
    55         -		%assign ct ccall.reg.ct
    56         -	%else
    57         -		%assign ct %0-1
    58         -	%endif
    59         -	%assign i 0
    60         -	%rotate 1
    61         -	%rep ct
    62         -		; if the function is well-behaved, all its arguments fit
    63         -		; in registers. if not, things get ugly. see below.
    64         -		mov ccall.reg. %+ i, %1
    65         -		%assign i i+1
    66         -		%rotate 1
    67         -	%endrep
    68         -	%if %0 > ccall.reg.ct
    69         -		; if there are more parameters to a C function than the
    70         -		; number of permitted registers, they must be pushed in
    71         -		; reverse order to the stack.
    72         -		; keep your function signatures under control, people.
    73         -		%assign ct (%0-ct)-1
    74         -		%rotate ct
    75         -		%rep ct
    76         -			%rotate -1
    77         -			push %1
    78         -		%endrep
    79         -		%rotate ct
    80         -		push rsp ; it's our responsibility to preserve the stack
    81         -	%endif
    82         -	call %1
    83         -	%if %0 > ccall.reg.ct
    84         -		; the extra arguments are still on the stack; time to
    85         -		; dump them back into the Garbage Zone
    86         -		pop rsp
    87         -	%endif
    88         -%endmacro