From d4b4428de619a71f6ce764a973fca91aa075478e Mon Sep 17 00:00:00 2001 From: Luis <42945842+ldiaz94@users.noreply.github.com> Date: Sun, 11 Oct 2026 00:49:58 +0100 Subject: [PATCH] Zero only a procedure's locals in its prologue Params are stored over their slots and every temp is written before it's read, so neither needs zeroing. Up to 4 quadwords are stored directly; more use the loop; none, nothing. --- diff --git a/doc/compiler.md b/doc/compiler.md index 47d5175..98e0226 100644 --- a/doc/compiler.md +++ b/doc/compiler.md @@ -12,7 +12,7 @@ | `src/scan.pas` | 584 | 495 | Symbols, literals, comments, positions | | `src/sym.pas` | 510 | 424 | Types, objects, scopes, layout | | `src/x64.pas` | 682 | 548 | Code buffer, chunks, fixups, line notes, instruction encoder | -| `src/gen.pas` | 1,460 | 1,276 | Items: loading, storing, copying, frames, calls | +| `src/gen.pas` | 1,468 | 1,281 | Items: loading, storing, copying, frames, calls | | `src/parse.pas` | 1,851 | 1,720 | Declarations, statements, expressions, built-ins | | `src/link.pas` | 131 | 95 | Tree shaking, layout, patching | | `src/elf.pas` | 425 | 351 | Static and dynamic ELF, symbol table, line numbers | @@ -85,11 +85,6 @@ push rbp ; EnterBody: the frame mov rbp,rsp sub rsp,0x10 ; its size, patched in by EndProc -mov r10,0xfffffffffffffffe ; zero the frame, a quadword at a -xor eax,eax ; time, r10 counting up to zero -mov QWORD PTR [rbp+r10*8+0x0],rax -add r10,0x1 -jne mov QWORD PTR [rbp-0x8],rdi ; the parameters, into their slots mov QWORD PTR [rbp-0x10],rsi mov rax,QWORD PTR [rbp-0x8] ; a @@ -273,7 +268,9 @@ in `rdi`, as C's structs do, and so do several results. Frames: `BeginProc` gives the parameters their slots; `EnterBody` -writes the prologue, including the loop that zeroes the frame; +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 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. diff --git a/src/gen.pas b/src/gen.pas index c1e0ec3..9c5b35d 100644 --- a/src/gen.pas +++ b/src/gen.pas @@ -132,7 +132,8 @@ var frameTop, frameMax, permTop: int64; - frameFix, zeroFix: longint; { where the frame size is patched in } + frameFix: longint; { where the frame size is patched in } + paramTop: int64; { the parameters' slots end here } retSlot: int64; { holds the hidden result pointer } pushEnd: longint; { the code just after the last push of rax } result: PType; { the procedure's result, or nil } @@ -1302,11 +1303,15 @@ end; p := p^.next; end; + paramTop := frameTop; 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. } procedure EnterBody(c, line: longint); -var i, top: longint; +var i, top: longint; n: int64; begin BeginChunk(c); NoteLine(line); @@ -1314,14 +1319,18 @@ MovRR(RBP, RSP); Put1($48); Put1($81); Put1($EC); { sub rsp, imm32 } frameFix := pc; Put4(0); - { zero the frame, a quadword at a time: r10 counts up to zero } - Put1($49); Put1($C7); Put1($C2); { mov r10, imm32 } - zeroFix := pc; Put4(0); - MovRI(RAX, 0); - top := pc; - Store(MemIdx(RBP, R10, 8, 0), RAX, 8); - AluI(aluAdd, R10, 1); - JccBack(ccNE, top); + n := (AlignUp(frameMax, 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) + else begin + { a quadword at a time: r10 counts up to zero } + MovRI(R10, -n); + top := pc; + Store(MemIdx(RBP, R10, 8, -paramTop), RAX, 8); + AluI(aluAdd, R10, 1); + JccBack(ccNE, top); + end; for i := 0 to numIn - 1 do if inReg[i] < 0 then MovsdMX(Mem(RBP, inSlot[i]), -1 - inReg[i]) else Store(Mem(RBP, inSlot[i]), inReg[i], inSize[i]); @@ -1453,7 +1462,6 @@ if n < 16 then n := 16; if n > high(longint) then Mark('frame too big'); Put4At(frameFix, n); - Put4At(zeroFix, -(n div 8)); EndChunk(c); end;