raz / commit d4b4428d

commit
d4b4428de619a71f6ce764a973fca91aa075478e
author
Luis <42945842+ldiaz94@users.noreply.github.com>
date
parent
a312fba8
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.

patch · log

2 files changed, +23 -18

doc/compiler.md (history) modified +4 -7

@@ -12,7 +12,7 @@
1212 | `src/scan.pas` | 584 | 495 | Symbols, literals, comments, positions |
1313 | `src/sym.pas` | 510 | 424 | Types, objects, scopes, layout |
1414 | `src/x64.pas` | 682 | 548 | Code buffer, chunks, fixups, line notes, instruction encoder |
15-| `src/gen.pas` | 1,460 | 1,276 | Items: loading, storing, copying, frames, calls |
15+| `src/gen.pas` | 1,468 | 1,281 | Items: loading, storing, copying, frames, calls |
1616 | `src/parse.pas` | 1,851 | 1,720 | Declarations, statements, expressions, built-ins |
1717 | `src/link.pas` | 131 | 95 | Tree shaking, layout, patching |
1818 | `src/elf.pas` | 425 | 351 | Static and dynamic ELF, symbol table, line numbers |
@@ -85,11 +85,6 @@
8585 push rbp ; EnterBody: the frame
8686 mov rbp,rsp
8787 sub rsp,0x10 ; its size, patched in by EndProc
88-mov r10,0xfffffffffffffffe ; zero the frame, a quadword at a
89-xor eax,eax ; time, r10 counting up to zero
90-mov QWORD PTR [rbp+r10*8+0x0],rax
91-add r10,0x1
92-jne <back to the mov>
9388 mov QWORD PTR [rbp-0x8],rdi ; the parameters, into their slots
9489 mov QWORD PTR [rbp-0x10],rsi
9590 mov rax,QWORD PTR [rbp-0x8] ; a
@@ -273,7 +268,9 @@
273268 in `rdi`, as C's structs do, and so do several results.
274269
275270 Frames: `BeginProc` gives the parameters their slots; `EnterBody`
276-writes the prologue, including the loop that zeroes the frame;
271+writes the prologue, which zeroes the locals (with a few stores, or a
272+loop for more; there are none to zero in `scale`) but not the
273+temporaries, since each is written before it is read;
277274 `EndProc` writes the epilogue and patches the frame's size in. Within
278275 a statement, scratch space comes from `AllocTemp` and is given back by
279276 `ReleaseTemps`; `AllocPerm` reserves space for the whole procedure.

src/gen.pas modified +19 -11

@@ -132,7 +132,8 @@
132132
133133 var
134134 frameTop, frameMax, permTop: int64;
135- frameFix, zeroFix: longint; { where the frame size is patched in }
135+ frameFix: longint; { where the frame size is patched in }
136+ paramTop: int64; { the parameters' slots end here }
136137 retSlot: int64; { holds the hidden result pointer }
137138 pushEnd: longint; { the code just after the last push of rax }
138139 result: PType; { the procedure's result, or nil }
@@ -1302,11 +1303,15 @@
13021303 end;
13031304 p := p^.next;
13041305 end;
1306+ paramTop := frameTop;
13051307 end;
13061308
13071309 { The prologue, which is for the procedure's first line. }
1310+{ Only the locals are zeroed: the parameters are stored over their
1311+ slots next, and every temporary is written before it is read. The
1312+ locals are all declared by now, below the parameters' slots. }
13081313 procedure EnterBody(c, line: longint);
1309-var i, top: longint;
1314+var i, top: longint; n: int64;
13101315 begin
13111316 BeginChunk(c);
13121317 NoteLine(line);
@@ -1314,14 +1319,18 @@
13141319 MovRR(RBP, RSP);
13151320 Put1($48); Put1($81); Put1($EC); { sub rsp, imm32 }
13161321 frameFix := pc; Put4(0);
1317- { zero the frame, a quadword at a time: r10 counts up to zero }
1318- Put1($49); Put1($C7); Put1($C2); { mov r10, imm32 }
1319- zeroFix := pc; Put4(0);
1320- MovRI(RAX, 0);
1321- top := pc;
1322- Store(MemIdx(RBP, R10, 8, 0), RAX, 8);
1323- AluI(aluAdd, R10, 1);
1324- JccBack(ccNE, top);
1322+ n := (AlignUp(frameMax, 8) - paramTop) div 8;
1323+ if n > 0 then MovRI(RAX, 0);
1324+ if n <= 4 then
1325+ for i := 1 to n do Store(Mem(RBP, -paramTop - 8 * i), RAX, 8)
1326+ else begin
1327+ { a quadword at a time: r10 counts up to zero }
1328+ MovRI(R10, -n);
1329+ top := pc;
1330+ Store(MemIdx(RBP, R10, 8, -paramTop), RAX, 8);
1331+ AluI(aluAdd, R10, 1);
1332+ JccBack(ccNE, top);
1333+ end;
13251334 for i := 0 to numIn - 1 do
13261335 if inReg[i] < 0 then MovsdMX(Mem(RBP, inSlot[i]), -1 - inReg[i])
13271336 else Store(Mem(RBP, inSlot[i]), inReg[i], inSize[i]);
@@ -1453,7 +1462,6 @@
14531462 if n < 16 then n := 16;
14541463 if n > high(longint) then Mark('frame too big');
14551464 Put4At(frameFix, n);
1456- Put4At(zeroFix, -(n div 8));
14571465 EndChunk(c);
14581466 end;
14591467