Make fetching args from stack (__INTERNAL_fmt) bit clearer

This commit is contained in:
2025-07-31 18:22:26 +02:00
parent be81866768
commit 68ee2828c1

View File

@@ -128,6 +128,9 @@ eprintf:
; To achieve that, pass ~RAX (thus ~pointer_to_array) and push length of array to the stack (from within function wrapper). See format() ; To achieve that, pass ~RAX (thus ~pointer_to_array) and push length of array to the stack (from within function wrapper). See format()
__INTERNAL_fmt: __INTERNAL_fmt:
%macro load_arg 1 %macro load_arg 1
%define OFFSET_SHIFT_NONE (RBP_OFFSET_CALLER) ; offset from rbp to get pushed args from original caller
%define OFFSET_SHIFT_FD (RBP_OFFSET_CALLER*2 + SIZE_QWORD) ; ^same, but account for function wrapper (which has func prologue and CALL to __INTERNAL_fmt)
%define OFFSET_SHIFT_MEM (RBP_OFFSET_CALLER*2 + SIZE_QWORD*4) ; ^same, but account for arg len (of array passed to rax) pushed to stack (+ stack alignment)
cmp r14, 4 cmp r14, 4
ja %%fromStack ja %%fromStack
mov %1, [__fmt_Args + SIZE_QWORD * r14] mov %1, [__fmt_Args + SIZE_QWORD * r14]
@@ -137,16 +140,13 @@ __INTERNAL_fmt:
jle %%stackNoShift jle %%stackNoShift
test rbx, rbx test rbx, rbx
jnz %%stackShift_format jnz %%stackShift_format
mov %1, [rbp + (RBP_OFFSET_CALLER*2 + SIZE_QWORD) + ((r14-5) * SIZE_QWORD)] ;Offset when called from wrapper like fwrite() (shifted args, output is to FD) mov %1, [rbp + OFFSET_SHIFT_FD + ((r14 - 5) * SIZE_QWORD)]
jmp %%continue jmp %%continue
%%stackShift_format: %%stackShift_format:
mov %1, [rbp + (RBP_OFFSET_CALLER*2 + SIZE_QWORD*4) + ((r14-5) * SIZE_QWORD)] ;Offset when called from wrapper like format() (shifted args, output is memory; extra push (+stack offset) for length of array) mov %1, [rbp + OFFSET_SHIFT_MEM + ((r14 -5) * SIZE_QWORD)]
;Not entirely sure why I need to do SIZE_QWORD*4 though, should be *3 (because of one push and one stack offset: 16 bytes)
;At %%fromStack I was also not sure why (according to comment at description: '+8 to account for possible R9 arg becoming a stack arg'); this does not apply
;It 1*8 is needed it checks out: 1 + 2 + that extra 1 = 4. Still not sure where the extra 8 is coming from.
jmp %%continue jmp %%continue
%%stackNoShift: %%stackNoShift:
mov %1, [rbp + RBP_OFFSET_CALLER + ((r14-5) * SIZE_QWORD)] ;Offset when called from wrapper like printf (no shifted args) mov %1, [rbp + OFFSET_SHIFT_NONE + ((r14 - 5) * SIZE_QWORD)]
%%continue: %%continue:
%endmacro %endmacro