From b14e81459b1c0e5e708fafea8db0036942033e1a Mon Sep 17 00:00:00 2001 From: Luis <42945842+ldiaz94@users.noreply.github.com> Date: Mon, 12 Oct 2026 14:47:08 +0100 Subject: [PATCH] A prologue that zeroes only the locals in memory v0.7.0 step 1. A local gets its frame slot only once the registers are chosen (PlaceLocals), and only if it isn't in one; the save slots come after the locals and aren't zeroed, as each is written first. So Str.decode, all of whose variables are in registers, no longer zeroes seven quadwords in a loop on every call. rat: 701 -> 620 ms (-12%), text 558,060 -> 553,484. --- diff --git a/doc/compiler.md b/doc/compiler.md index 079e832..c538d16 100644 --- a/doc/compiler.md +++ b/doc/compiler.md @@ -1,6 +1,6 @@ # Reading the raz compiler -The compiler is eleven files of Free Pascal, under 8,400 lines of code. +The compiler is eleven files of Free Pascal, under 10,000 lines of code. This guide walks through them in the order they are best read: from the characters of a source file to the bytes of an executable. It assumes you know the language from `doc/raz.md`, and enough x86-64 to read a @@ -13,9 +13,9 @@ | `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,616 | 1,270 | The stack machine, for x86-64: values, operations, checks, calls, frames | +| `src/gen.pas` | 1,633 | 1,280 | 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/parse.pas` | 2,913 | 2,579 | 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 | | `src/raz.pas` | 197 | 147 | The command line | @@ -358,16 +358,19 @@ results (`ResultTo`, `CopyResult`). Frames: `BeginProc` gives the parameters their slots; `EnterBody` -writes the prologue, which zeroes the locals (with a few stores, or a -loop for more; there are none to zero in `scale`) but not the -temporaries, since each is written before it is read; `EndProc` writes +writes the prologue, which zeroes the locals in memory (with a few +stores, or a loop for more; there are none to zero in `scale`) but not +the temporaries, since each is written before it is read; `EndProc` writes the epilogue and patches the frame's size in. Within a statement, scratch space comes from `AllocTemp` and is given back by `ReleaseTemps`; `AllocPerm` reserves space for the whole procedure. -Register variables: the parser chooses them (`ChooseRegs`, below) and -tells gen (`UseRegs`): the registers, as bits of `VarRegs`, and the -slots they are saved in, a quadword each after the variables. The +Register variables: the parser chooses them (`ChooseRegs`, below), +then gives the locals left in memory their slots (`PlaceLocals`), and +tells gen (`UseRegs`) the registers, as bits of `VarRegs`. A variable +in a register has no slot of its own. Gen saves the registers in slots +a quadword each, after the locals and not zeroed, as each is written +first. The prologue saves them, zeroes them, and takes a parameter's value from where it came; the epilogue restores them. A variable in one is an item at `bReg`: `PushVar` notes it (`lHeld`) rather than copying it, @@ -553,7 +556,8 @@ address wanted after all, by a `var` parameter, say, comes to `Address` (`item.pas`), which puts the variable back in memory and sets `demoted`: `Body` then throws the procedure's code away -(`MarkCode`, `Redo`) and compiles its statements again. Its notes for +(`MarkCode`, `Redo`), places the locals again, and compiles its +statements again. Its notes for the trap report are made after, by `VarKind`, which marks a variable in a register with the register's number. diff --git a/src/gen.pas b/src/gen.pas index 0bb2347..89398db 100644 --- a/src/gen.pas +++ b/src/gen.pas @@ -154,7 +154,8 @@ procedure KeepTemps; procedure BeginProc(sig: PType; params: PObj); -procedure UseRegs(mask: longint; at: int64); +procedure LocalsFromScratch; +procedure UseRegs(mask: longint); procedure MarkCode(var m: TCodeMark); procedure Redo(const m: TCodeMark); procedure EnterBody(c, line: longint); @@ -203,6 +204,7 @@ frameTop, frameMax, permTop: int64; frameFix: longint; { where the frame size is patched in } paramTop: int64; { the parameters' slots end here } + zeroTop: int64; { the locals the prologue zeroes end here } retSlot: int64; { holds the hidden result pointer } resType: PType; { the procedure's result, or nil } exitL: longint; @@ -1352,12 +1354,26 @@ paramTop := frameTop; end; -{ The procedure keeps variables in the registers of mask, saved at at; - the frame as it stands is where a second try at its body starts. } -procedure UseRegs(mask: longint; at: int64); +{ The frame as BeginProc left it, with the parameters' slots only: the + locals are given theirs again once their registers are chosen. } +procedure LocalsFromScratch; +begin + frameTop := paramTop; frameMax := paramTop; permTop := paramTop; +end; + +{ The locals in memory have their slots: the prologue zeroes those and + nothing below. The procedure keeps variables in the registers of + mask, which are saved next, a quadword each, in slots that need no + zeroing; the frame as it then stands is where a second try at the + body starts. } +procedure UseRegs(mask: longint); +var i, k: longint; begin + zeroTop := permTop; regMask := mask; - saveAt := at; + k := 0; + for i := 0 to 4 do if mask and (1 shl i) <> 0 then inc(k); + if k > 0 then saveAt := AllocPerm(8 * k, 8); frameAt[0] := frameTop; frameAt[1] := frameMax; frameAt[2] := permTop; end; @@ -1377,9 +1393,10 @@ end; { The prologue, which is for the procedure's first line. Only the - locals are zeroed: the parameters are stored over their slots next, - and every temporary is written before it is read. The locals are all - declared by now, below the parameters' slots. } + locals in memory are zeroed, from the parameters' slots down to + zeroTop: the parameters are stored over their slots next, the + registers of variables are zeroed in place, their save slots are + written before they are read, and so is every temporary. } procedure EnterBody(c, line: longint); var i, top: longint; n: int64; p: PObj; begin @@ -1391,7 +1408,7 @@ Say('sub rsp, frame'); Put1($48); Put1($81); Put1($EC); { sub rsp, imm32 } frameFix := pc; Put4(0); - n := (AlignUp(frameMax, 8) - paramTop) div 8; + n := (AlignUp(zeroTop, 8) - paramTop) div 8; if n > 0 then MovRI(RAX, 0); if n <= 4 then for i := 1 to n do Store(Mem(RBP, -paramTop - 8 * i), RAX, 8) diff --git a/src/parse.pas b/src/parse.pas index 571a7fe..d11474c 100644 --- a/src/parse.pas +++ b/src/parse.pas @@ -1782,12 +1782,12 @@ its "begin" to its end, a use counts 1, in a loop 4, in two 16, and a variable ADR is taken of can't have one. A register costs a save and a restore, so a variable used less than three times keeps its - place in memory. The registers are saved after the variables. } + place in memory. } procedure ChooseRegs(params, before: PObj); var cand: array[0..63] of PObj; weight: array[0..63] of longint; isLoop: array[0..255] of boolean; - n, i, k, d, loops, mask, best: longint; o: PObj; st: TScanState; afterDot, wasSkipping: boolean; + n, i, k, d, loops, best: longint; o: PObj; st: TScanState; afterDot, wasSkipping: boolean; function Find(name: longint): longint; var j: longint; @@ -1807,7 +1807,6 @@ while o <> nil do begin Add(o); o := o^.next end; if before = nil then o := curModule^.locals else o := before^.link; while o <> nil do begin Add(o); o := o^.link end; - mask := 0; if n > 0 then begin SaveScan(st); wasSkipping := skipping; @@ -1846,17 +1845,12 @@ if (cand[i]^.reg = 0) and (weight[i] >= 3) and ((best < 0) or (weight[i] > weight[best])) then best := i; if best < 0 then break; cand[best]^.reg := VarRegs[k]; - mask := mask or (1 shl k); end; end; - k := 0; - for i := 0 to 4 do if mask and (1 shl i) <> 0 then inc(k); - if k = 0 then UseRegs(0, 0) - else UseRegs(mask, AllocPerm(8 * k, 8)); end; -{ The registers the variables of the procedure are in, as ChooseRegs's - mask } +{ The registers the variables of the procedure are in, as bits of + VarRegs } function RegsOf(params, before: PObj): longint; var o: PObj; m, k: longint; begin @@ -1875,6 +1869,22 @@ RegsOf := m; end; +{ Gives each local not in a register its slot in the frame, the slots + the prologue zeroes, and then tells gen which registers the + variables are in. A variable in a register has no slot: it starts at + zero in its register, and the trap report finds it there. } +procedure PlaceLocals(params, before: PObj); +var o: PObj; +begin + LocalsFromScratch; + if before = nil then o := curModule^.locals else o := before^.link; + while o <> nil do begin + if (o^.cls = oVar) and (o^.reg = 0) then o^.val := AllocPerm(o^.typ^.size, o^.typ^.align); + o := o^.link; + end; + UseRegs(RegsOf(params, before)); +end; + procedure Body(p: PObj; sig: PType; line: longint); var q, params, last, o, before: PObj; old: PType; st: TScanState; @@ -1905,6 +1915,7 @@ before := curModule^.lastLocal; Declarations(false); ChooseRegs(params, before); + PlaceLocals(params, before); { the body, made again if a variable in a register must be in memory } SaveScan(st); MarkCode(mark); drops0 := numDrops; errs := errors; @@ -1922,7 +1933,7 @@ if not demoted or (errors <> errs) then break; numDrops := drops0; Redo(mark); - UseRegs(RegsOf(params, before), chunks^[p^.chunk].saveAt); + PlaceLocals(params, before); RestoreScan(st); until false; EndProc(p^.chunk); @@ -2227,10 +2238,8 @@ o^.lev := 0; o^.chunk := NewChunk(secBss, curModule^.sname, names[i]); SetBss(o^.chunk, t^.size, t^.align); - end else begin - o^.lev := 1; - o^.val := AllocPerm(t^.size, t^.align); - end; + end else + o^.lev := 1; { its slot comes from PlaceLocals, if it needs one } Declare(o); end; Expect(sSemicolon); diff --git a/test/list/Listing.expect b/test/list/Listing.expect index c5e2676..283b535 100644 --- a/test/list/Listing.expect +++ b/test/list/Listing.expect @@ -28,39 +28,36 @@ 0 push rbp 1 mov rbp, rsp 4 sub rsp, 32 - 11 xor eax, eax - 13 mov [rbp - 8], rax - 17 mov [rbp - 16], rax - 21 mov [rbp - 16], rbx - 25 xor ebx, ebx + 11 mov [rbp - 8], rbx + 15 xor ebx, ebx 20 for i := 1 to 3 do - 27 mov rbx, 1 - 32 mov qword [rbp - 24], 3 - 40 mov r9, [rbp - 24] - 44 cmp rbx, r9 - 47 jg 139 + 17 mov rbx, 1 + 22 mov qword [rbp - 16], 3 + 30 mov r9, [rbp - 16] + 34 cmp rbx, r9 + 37 jg 129 21 if i # 2 then total := add(total, twice(i)); end; - 53 cmp rbx, 2 - 57 je 107 - 63 mov r8, [Listing.total] - 70 mov [rbp - 32], r8 - 74 mov rdi, rbx - 77 call Listing.twice - 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 + 43 cmp rbx, 2 + 47 je 97 + 53 mov r8, [Listing.total] + 60 mov [rbp - 24], r8 + 64 mov rdi, rbx + 67 call Listing.twice + 72 mov r9, rax + 75 mov rdi, [rbp - 24] + 79 mov rsi, r9 + 82 call Listing.add + 87 mov r8, rax + 90 mov [Listing.total], r8 + 97 mov r8, [rbp - 16] + 101 sub r8, rbx + 104 cmp r8, 1 + 108 jb 129 + 114 mov r8, rbx + 117 add r8, 1 + 121 mov rbx, r8 + 124 jmp 30 23 end main; - 139 mov rbx, [rbp - 16] - 143 leave - 144 ret + 129 mov rbx, [rbp - 8] + 133 leave + 134 ret