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.
| @@ -12,7 +12,7 @@ | ||
| 12 | 12 | | `src/scan.pas` | 584 | 495 | Symbols, literals, comments, positions | |
| 13 | 13 | | `src/sym.pas` | 510 | 424 | Types, objects, scopes, layout | |
| 14 | 14 | | `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 | | |
| 16 | 16 | | `src/parse.pas` | 1,851 | 1,720 | Declarations, statements, expressions, built-ins | |
| 17 | 17 | | `src/link.pas` | 131 | 95 | Tree shaking, layout, patching | |
| 18 | 18 | | `src/elf.pas` | 425 | 351 | Static and dynamic ELF, symbol table, line numbers | |
| @@ -85,11 +85,6 @@ | ||
| 85 | 85 | push rbp ; EnterBody: the frame |
| 86 | 86 | mov rbp,rsp |
| 87 | 87 | 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> | |
| 93 | 88 | mov QWORD PTR [rbp-0x8],rdi ; the parameters, into their slots |
| 94 | 89 | mov QWORD PTR [rbp-0x10],rsi |
| 95 | 90 | mov rax,QWORD PTR [rbp-0x8] ; a |
| @@ -273,7 +268,9 @@ | ||
| 273 | 268 | in `rdi`, as C's structs do, and so do several results. |
| 274 | 269 | |
| 275 | 270 | 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; | |
| 277 | 274 | `EndProc` writes the epilogue and patches the frame's size in. Within |
| 278 | 275 | a statement, scratch space comes from `AllocTemp` and is given back by |
| 279 | 276 | `ReleaseTemps`; `AllocPerm` reserves space for the whole procedure. |
| @@ -132,7 +132,8 @@ | ||
| 132 | 132 | |
| 133 | 133 | var |
| 134 | 134 | 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 } | |
| 136 | 137 | retSlot: int64; { holds the hidden result pointer } |
| 137 | 138 | pushEnd: longint; { the code just after the last push of rax } |
| 138 | 139 | result: PType; { the procedure's result, or nil } |
| @@ -1302,11 +1303,15 @@ | ||
| 1302 | 1303 | end; |
| 1303 | 1304 | p := p^.next; |
| 1304 | 1305 | end; |
| 1306 | + paramTop := frameTop; | |
| 1305 | 1307 | end; |
| 1306 | 1308 | |
| 1307 | 1309 | { 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. } | |
| 1308 | 1313 | procedure EnterBody(c, line: longint); |
| 1309 | -var i, top: longint; | |
| 1314 | +var i, top: longint; n: int64; | |
| 1310 | 1315 | begin |
| 1311 | 1316 | BeginChunk(c); |
| 1312 | 1317 | NoteLine(line); |
| @@ -1314,14 +1319,18 @@ | ||
| 1314 | 1319 | MovRR(RBP, RSP); |
| 1315 | 1320 | Put1($48); Put1($81); Put1($EC); { sub rsp, imm32 } |
| 1316 | 1321 | 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; | |
| 1325 | 1334 | for i := 0 to numIn - 1 do |
| 1326 | 1335 | if inReg[i] < 0 then MovsdMX(Mem(RBP, inSlot[i]), -1 - inReg[i]) |
| 1327 | 1336 | else Store(Mem(RBP, inSlot[i]), inReg[i], inSize[i]); |
| @@ -1453,7 +1462,6 @@ | ||
| 1453 | 1462 | if n < 16 then n := 16; |
| 1454 | 1463 | if n > high(longint) then Mark('frame too big'); |
| 1455 | 1464 | Put4At(frameFix, n); |
| 1456 | - Put4At(zeroFix, -(n div 8)); | |
| 1457 | 1465 | EndChunk(c); |
| 1458 | 1466 | end; |
| 1459 | 1467 |