From 3e451d8e02ebc39e4b1e5aced5f4c0885ce3138e Mon Sep 17 00:00:00 2001 From: Luis <42945842+ldiaz94@users.noreply.github.com> Date: Mon, 12 Oct 2026 14:26:52 +0100 Subject: [PATCH] Slices in two registers, arguments on the stack A slice goes as its two words, in two registers or both on the stack, as System V passes a struct of two integers, not as the address of a copy: the procedure keeps them in its frame, and s[i] is one load the fewer. Arguments no longer wait in frame slots: they stay on the value stack where computing them left them, constants, addresses and register variables only noted, and Call moves each into its register (ArgTo), r8 and r9 last, through rax, as they may hold others. A call in a later argument spills the earlier ones, as it spills any value under it, which keeps the order of evaluation. PopArg, LoadArg and the argument slots go. rat on 26 MB: 668 ms to 634, its text -0.8%. 8,390 lines of code. --- diff --git a/doc/compiler.md b/doc/compiler.md index 2c97534..079e832 100644 --- a/doc/compiler.md +++ b/doc/compiler.md @@ -13,8 +13,8 @@ | `src/sym.pas` | 617 | 512 | Types, objects, scopes, layout | | `src/chunk.pas` | 302 | 234 | The compiled program: chunks, fixups, line notes | | `src/x64.pas` | 1,113 | 930 | The instruction encoder, jumps within a procedure, the assembler | -| `src/gen.pas` | 1,611 | 1,263 | The stack machine, for x86-64: values, operations, checks, calls, frames | -| `src/item.pas` | 1,433 | 1,194 | Items: types, constants, variables, operators, conversions, calls | +| `src/gen.pas` | 1,616 | 1,270 | The stack machine, for x86-64: values, operations, checks, calls, frames | +| `src/item.pas` | 1,444 | 1,204 | Items: types, constants, variables, operators, conversions, calls | | `src/parse.pas` | 2,904 | 2,575 | Declarations, statements, expressions, built-ins, recovery, warnings | | `src/link.pas` | 247 | 190 | Tree shaking, layout, patching, the places | | `src/elf.pas` | 435 | 352 | Static and dynamic ELF, symbol table, line numbers | @@ -344,11 +344,15 @@ own `gen.pas`: `Rt.fail` stops for a nil pointer, a division by zero and a stack overflow as the signals would. -Calls: the arguments wait for `Call`, computed left to right: a -constant or a noted address as itself, anything else in a frame slot -(`PopArg`), so that computing a later one can't disturb it; only then -are they loaded into the registers System V wants (`LoadArg`), and the -stack aligned for the call. A record +Calls: the arguments are computed left to right onto the stack, where +they wait for `Call`, as any values do: a constant, an address or a +variable in a register only noted, the rest in registers, or spilled +by a call in a later argument, so that it can't disturb them. `Call` +spills what is under them, moves each into the register System V +wants (`ArgTo`), `r8` and `r9` last since they may hold others, and +aligns the stack. A slice goes as its two words, in two registers or +both on the stack, as System V passes a struct of two integers, and +the procedure keeps them in its frame. A record passed by value goes as the address of a copy; one returned comes back through a hidden pointer in `rdi`, as C's structs do, and so do several results (`ResultTo`, `CopyResult`). diff --git a/src/gen.pas b/src/gen.pas index fd2910c..0bb2347 100644 --- a/src/gen.pas +++ b/src/gen.pas @@ -16,12 +16,11 @@ needs it in a register, so that it can be an instruction's operand. Calls use the System V convention. Arguments are computed left to - right into frame slots, then loaded into registers just before the - call, so that computing one argument can't disturb another; a - constant or an address, which nothing can disturb, goes straight to - its register. A record, - array, slice or variant passed by value goes as the address of a copy - the caller makes; one returned goes through a hidden pointer, as C's + right onto the stack, and moved into their registers just before the + call: a call in a later argument spills the earlier ones, as any call + spills the values under it, so that it can't disturb them. A slice goes as its two words, as System V passes a + 16-byte struct of two integers; a record, array or variant passed by + value goes as the address of a copy the caller makes; one returned goes through a hidden pointer, as C's structs do, and so do several results. A procedure's frame holds its parameters, its variables, and the @@ -62,10 +61,6 @@ chunk: longint; { bGlobal: the chunk; bSlot: the frame slot; bReg: the register } off: int64; { added to the address } end; - - { How an argument waits for its call: in a frame slot, or noted, as a - constant or as the address of the frame or a chunk plus an offset } - TArgHow = (argSlot, argConst, argAdr, argHeld); { The code made so far, to go back to for a second try at a body } TCodeMark = record pc, fixups, lines, said: longint; end; @@ -75,10 +70,8 @@ param: PObj; { the parameter the next argument is for } obj: PObj; { the procedure called, if it has a name } n: longint; { the arguments } - slots: array[0..63] of int64; { each one's frame slot, constant or offset } - how: array[0..63] of TArgHow; - adrChunk: array[0..63] of longint; { argAdr: the chunk, or -1 for the frame } isF: array[0..63] of boolean; { ... goes in an xmm register } + pair: array[0..63] of boolean; { ... is a slice's first word, the next its second } target: int64; { the frame slot of a procedure value, if chunk < 0 } chunk: longint; { else the procedure's chunk } code: boolean; { a code procedure: two results come in rax and rdx } @@ -152,7 +145,6 @@ procedure SlicePtr(size, tmp: int64; checked: boolean; const p: TPos); procedure CopySlice(dOff, sOff, size: int64; checked: boolean; const p: TPos); -procedure PopArg(var c: TCall); procedure Call(var c: TCall); function AllocTemp(size, align: int64): int64; @@ -1172,46 +1164,27 @@ { The call c, its arguments already in their frame slots: they go to registers or the stack, the procedure is called, and its result, if any, is pushed on the value stack. } -{ The value on top is argument c.n of c, which waits for the call: a - constant or an address is noted, a real or anything else goes to a - frame slot. } -procedure PopArg(var c: TCall); -var i: longint; +{ Argument value i, as it is on the stack, into register r } +procedure ArgTo(i, r: longint); begin - i := depth - 1; - c.how[c.n] := argSlot; - if not c.isF[c.n] and (stack[i].loc = lConst) then c.how[c.n] := argConst - else if stack[i].loc = lAdr then c.how[c.n] := argAdr - else if stack[i].loc = lHeld then c.how[c.n] := argHeld; - if c.how[c.n] = argSlot then begin - c.slots[c.n] := AllocTemp(8, 8); - Store(Mem(RBP, c.slots[c.n]), Top, 8); - end else begin - c.slots[c.n] := stack[i].val; - c.adrChunk[c.n] := stack[i].chunk; + case stack[i].loc of + lConst: MovRI(r, stack[i].val); + lHeld: MovRR(r, stack[i].val); + lAdr: Lea(r, AdrMem(i, 0)); + lReg: if RegOf(i) <> r then MovRR(r, RegOf(i)); + else Load(r, Mem(RBP, stack[i].val), 8, false); end; - Drop; end; -{ Argument i of c into register r } -procedure LoadArg(const c: TCall; i, r: longint); -begin - case c.how[i] of - argConst: MovRI(r, c.slots[i]); - argHeld: MovRR(r, c.slots[i]); - argAdr: - if c.adrChunk[i] < 0 then Lea(r, Mem(RBP, c.slots[i])) - else Lea(r, MemRip(c.adrChunk[i], c.slots[i])); - else Load(r, Mem(RBP, c.slots[i]), 8, false); - end; -end; - procedure Call(var c: TCall); var - res: PType; i, ni, nf, k, pad, r: longint; - onStack: array[0..63] of boolean; + res: PType; i, ni, nf, k, pad, r, first: longint; + onStack: array[0..63] of boolean; late: array[4..5] of longint; begin - SpillBelow(depth); { the registers in use, saved around the call } + { the arguments are the c.n values on top, where computing them left + them; the registers in use under them are saved around the call } + first := depth - c.n; + SpillBelow(first); res := c.sig^.base; c.ret := 0; ni := 0; nf := 0; @@ -1224,7 +1197,13 @@ k := 0; for i := 0 to c.n - 1 do begin if c.isF[i] then begin onStack[i] := nf >= 8; if nf < 8 then inc(nf) end - else begin onStack[i] := ni >= 6; if ni < 6 then inc(ni) end; + else begin + { a slice's words both go in registers, or both on the stack } + if (i > 0) and c.pair[i - 1] then onStack[i] := onStack[i - 1] + else if c.pair[i] then onStack[i] := ni > 4 + else onStack[i] := ni >= 6; + if not onStack[i] then inc(ni); + end; if onStack[i] then inc(k); end; { System V wants rsp a multiple of 16 at the call: a quadword of @@ -1233,15 +1212,28 @@ if pad <> 0 then AluI(aluSub, RSP, 8); for i := c.n - 1 downto 0 do if onStack[i] then begin - LoadArg(c, i, RAX); + ArgTo(first + i, RAX); Push(RAX); end; ni := 0; nf := 0; + late[4] := -1; late[5] := -1; if (c.ret <> 0) and not c.code then begin Lea(RDI, Mem(RBP, c.ret)); ni := 1 end; + { rdi, rsi, rdx, rcx and the xmm registers first; r8 and r9 last, as + they may hold arguments still } for i := 0 to c.n - 1 do if not onStack[i] then - if c.isF[i] then begin MovsdXM(nf, Mem(RBP, c.slots[i])); inc(nf) end - else begin LoadArg(c, i, IntRegs[ni]); inc(ni) end; + if c.isF[i] then begin + if stack[first + i].loc = lFrame then MovsdXM(nf, Mem(RBP, stack[first + i].val)) + else begin ArgTo(first + i, RAX); MovqXR(nf, RAX) end; + inc(nf); + end else begin + if ni < 4 then ArgTo(first + i, IntRegs[ni]) else late[ni] := first + i; + inc(ni); + end; + if late[4] >= 0 then ArgTo(late[4], RAX); + if late[5] >= 0 then ArgTo(late[5], R9); + if late[4] >= 0 then MovRR(R8, RAX); + for i := 1 to c.n do Drop; { a procedure taking ... is told in al how many xmm registers it got } if c.sig^.variadic then MovRI(RAX, nf); if c.ext then CallM(MemRip(c.chunk, 0)) @@ -1309,7 +1301,7 @@ pushed it. A var parameter, and a record or array passed by value, holds an address (ind). } procedure BeginProc(sig: PType; params: PObj); -var p: PObj; ni, nf: longint; stackOff: int64; f: boolean; +var p: PObj; ni, nf, k: longint; stackOff: int64; f: boolean; begin frameTop := 0; frameMax := 0; permTop := 0; ResetStack(0, 0); @@ -1326,9 +1318,22 @@ end; p := params; while p <> nil do begin - p^.ind := p^.isVar or IsAggregate(p^.typ); + p^.ind := p^.isVar or IsAggregate(p^.typ) and (p^.typ^.form <> fSlice); f := (p^.typ^.form = fReal) and not p^.isVar; - if (f and (nf < 8)) or (not f and (ni < 6)) then begin + if (p^.typ^.form = fSlice) and not p^.isVar then begin + { its two words, in two registers or both on the stack } + if ni <= 4 then begin + p^.val := AllocPerm(16, 8); + for k := 0 to 1 do begin + inReg[numIn] := IntRegs[ni]; inSlot[numIn] := p^.val + 8 * k; + inSize[numIn] := 8; inObj[numIn] := nil; + inc(numIn); inc(ni); + end; + end else begin + p^.val := stackOff; + inc(stackOff, 16); + end; + end else if (f and (nf < 8)) or (not f and (ni < 6)) then begin p^.val := AllocPerm(8, 8); if f then begin inReg[numIn] := -1 - nf; inc(nf) end else begin inReg[numIn] := IntRegs[ni]; inc(ni) end; diff --git a/src/item.pas b/src/item.pas index 03691dc..999949b 100644 --- a/src/item.pas +++ b/src/item.pas @@ -1140,10 +1140,10 @@ end; procedure PassArg(var c: TCall; var a: TItem); -var p: PObj; s: int64; +var p: PObj; adr: TAdr; s: int64; begin p := c.param; - if c.n > High(c.slots) then MarkAt(a.pos, 'too many arguments'); + if c.n >= High(c.isF) then MarkAt(a.pos, 'too many arguments'); if p = nil then begin { past the parameters of a C procedure that ends in ...: a string goes as a C string, an integer constant as an INT, a real @@ -1155,8 +1155,8 @@ else if IsF32(a.typ) then MarkAt(a.pos, 'an F32 goes to ... as F64(x)') else if not IsScalar(a.typ) then MarkAt(a.pos, 'only a single value goes to ...'); c.isF[c.n] := a.typ^.form = fReal; + c.pair[c.n] := false; Value(a); - PopArg(c); inc(c.n); exit; end; @@ -1170,6 +1170,17 @@ if not Same(a.typ, p^.typ) then Mismatch(a, 'a var parameter needs exactly ' + TypeName(p^.typ) + ', found ' + What(a)); if a.vol then MarkAt(a.pos, 'can''t pass through a volatile pointer to a var parameter'); Address(a); + end else if p^.typ^.form = fSlice then begin + { its two words, read now, so that a later argument can't change them } + Coerce(a, p^.typ); + if (a.mode <> mVar) or a.vol or not (a.base in [bFrame, bGlobal, bSlot]) then CopyToTemp(a); + adr := AdrOf(a); + PushVar(adr, 8, false, kWord); + c.pair[c.n] := true; + inc(c.n); + c.isF[c.n] := false; + inc(adr.off, 8); + PushVar(adr, 8, false, kWord); end else if IsAggregate(p^.typ) then begin Coerce(a, p^.typ); if a.isVar or a.vol or (a.base <> bFrame) then CopyToTemp(a); @@ -1178,7 +1189,7 @@ Coerce(a, p^.typ); Value(a); end; - PopArg(c); + c.pair[c.n] := false; inc(c.n); c.param := p^.next; end; diff --git a/test/list/Listing.expect b/test/list/Listing.expect index 8b68ed6..c5e2676 100644 --- a/test/list/Listing.expect +++ b/test/list/Listing.expect @@ -27,7 +27,7 @@ 17 proc main*; 0 push rbp 1 mov rbp, rsp - 4 sub rsp, 48 + 4 sub rsp, 32 11 xor eax, eax 13 mov [rbp - 8], rax 17 mov [rbp - 16], rax @@ -38,30 +38,29 @@ 32 mov qword [rbp - 24], 3 40 mov r9, [rbp - 24] 44 cmp rbx, r9 - 47 jg 144 + 47 jg 139 21 if i # 2 then total := add(total, twice(i)); end; 53 cmp rbx, 2 - 57 je 112 + 57 je 107 63 mov r8, [Listing.total] 70 mov [rbp - 32], r8 74 mov rdi, rbx 77 call Listing.twice - 82 mov r8, rax - 85 mov [rbp - 40], r8 - 89 mov rdi, [rbp - 32] - 93 mov rsi, [rbp - 40] - 97 call Listing.add - 102 mov r8, rax - 105 mov [Listing.total], r8 - 112 mov r8, [rbp - 24] - 116 sub r8, rbx - 119 cmp r8, 1 - 123 jb 144 - 129 mov r8, rbx - 132 add r8, 1 - 136 mov rbx, r8 - 139 jmp 40 + 82 mov r9, rax + 85 mov rdi, [rbp - 32] + 89 mov rsi, r9 + 92 call Listing.add + 97 mov r8, rax + 100 mov [Listing.total], r8 + 107 mov r8, [rbp - 24] + 111 sub r8, rbx + 114 cmp r8, 1 + 118 jb 139 + 124 mov r8, rbx + 127 add r8, 1 + 131 mov rbx, r8 + 134 jmp 40 23 end main; - 144 mov rbx, [rbp - 16] - 148 leave - 149 ret + 139 mov rbx, [rbp - 16] + 143 leave + 144 ret diff --git a/test/run/ThreadOverflow.stderr b/test/run/ThreadOverflow.stderr index 016b284..c3884f5 100644 --- a/test/run/ThreadOverflow.stderr +++ b/test/run/ThreadOverflow.stderr @@ -1,6 +1,6 @@ test/run/ThreadOverflow.raz:6:1: stopped by a stack overflow in ThreadOverflow.down at test/run/ThreadOverflow.raz:6:1 - in ThreadOverflow.down at test/run/ThreadOverflow.raz:10:5: n = 1871 - and 1871 more the same + in ThreadOverflow.down at test/run/ThreadOverflow.raz:10:5: n = 1926 + and 1926 more the same in ThreadOverflow.work at test/run/ThreadOverflow.raz:15:5: _arg = nil in Thread.run at lib/Thread.linux-x86_64.raz:80:5: arg = 0x?, t = 0x?