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.
| @@ -1,6 +1,6 @@ | ||
| 1 | 1 | # Reading the raz compiler |
| 2 | 2 | |
| 3 | -The compiler is eleven files of Free Pascal, under 8,400 lines of code. | |
| 3 | +The compiler is eleven files of Free Pascal, under 10,000 lines of code. | |
| 4 | 4 | This guide walks through them in the order they are best read: from the |
| 5 | 5 | characters of a source file to the bytes of an executable. It assumes |
| 6 | 6 | you know the language from `doc/raz.md`, and enough x86-64 to read a |
| @@ -13,9 +13,9 @@ | ||
| 13 | 13 | | `src/sym.pas` | 617 | 512 | Types, objects, scopes, layout | |
| 14 | 14 | | `src/chunk.pas` | 302 | 234 | The compiled program: chunks, fixups, line notes | |
| 15 | 15 | | `src/x64.pas` | 1,113 | 930 | The instruction encoder, jumps within a procedure, the assembler | |
| 16 | -| `src/gen.pas` | 1,616 | 1,270 | The stack machine, for x86-64: values, operations, checks, calls, frames | | |
| 16 | +| `src/gen.pas` | 1,633 | 1,280 | The stack machine, for x86-64: values, operations, checks, calls, frames | | |
| 17 | 17 | | `src/item.pas` | 1,444 | 1,204 | Items: types, constants, variables, operators, conversions, calls | |
| 18 | -| `src/parse.pas` | 2,904 | 2,575 | Declarations, statements, expressions, built-ins, recovery, warnings | | |
| 18 | +| `src/parse.pas` | 2,913 | 2,579 | Declarations, statements, expressions, built-ins, recovery, warnings | | |
| 19 | 19 | | `src/link.pas` | 247 | 190 | Tree shaking, layout, patching, the places | |
| 20 | 20 | | `src/elf.pas` | 435 | 352 | Static and dynamic ELF, symbol table, line numbers | |
| 21 | 21 | | `src/raz.pas` | 197 | 147 | The command line | |
| @@ -358,16 +358,19 @@ | ||
| 358 | 358 | results (`ResultTo`, `CopyResult`). |
| 359 | 359 | |
| 360 | 360 | Frames: `BeginProc` gives the parameters their slots; `EnterBody` |
| 361 | -writes the prologue, which zeroes the locals (with a few stores, or a | |
| 362 | -loop for more; there are none to zero in `scale`) but not the | |
| 363 | -temporaries, since each is written before it is read; `EndProc` writes | |
| 361 | +writes the prologue, which zeroes the locals in memory (with a few | |
| 362 | +stores, or a loop for more; there are none to zero in `scale`) but not | |
| 363 | +the temporaries, since each is written before it is read; `EndProc` writes | |
| 364 | 364 | the epilogue and patches the frame's size in. Within a statement, |
| 365 | 365 | scratch space comes from `AllocTemp` and is given back by |
| 366 | 366 | `ReleaseTemps`; `AllocPerm` reserves space for the whole procedure. |
| 367 | 367 | |
| 368 | -Register variables: the parser chooses them (`ChooseRegs`, below) and | |
| 369 | -tells gen (`UseRegs`): the registers, as bits of `VarRegs`, and the | |
| 370 | -slots they are saved in, a quadword each after the variables. The | |
| 368 | +Register variables: the parser chooses them (`ChooseRegs`, below), | |
| 369 | +then gives the locals left in memory their slots (`PlaceLocals`), and | |
| 370 | +tells gen (`UseRegs`) the registers, as bits of `VarRegs`. A variable | |
| 371 | +in a register has no slot of its own. Gen saves the registers in slots | |
| 372 | +a quadword each, after the locals and not zeroed, as each is written | |
| 373 | +first. The | |
| 371 | 374 | prologue saves them, zeroes them, and takes a parameter's value from |
| 372 | 375 | where it came; the epilogue restores them. A variable in one is an |
| 373 | 376 | item at `bReg`: `PushVar` notes it (`lHeld`) rather than copying it, |
| @@ -553,7 +556,8 @@ | ||
| 553 | 556 | address wanted after all, by a `var` parameter, say, comes to |
| 554 | 557 | `Address` (`item.pas`), which puts the variable back in memory and |
| 555 | 558 | sets `demoted`: `Body` then throws the procedure's code away |
| 556 | -(`MarkCode`, `Redo`) and compiles its statements again. Its notes for | |
| 559 | +(`MarkCode`, `Redo`), places the locals again, and compiles its | |
| 560 | +statements again. Its notes for | |
| 557 | 561 | the trap report are made after, by `VarKind`, which marks a variable |
| 558 | 562 | in a register with the register's number. |
| 559 | 563 |
| @@ -154,7 +154,8 @@ | ||
| 154 | 154 | procedure KeepTemps; |
| 155 | 155 | |
| 156 | 156 | procedure BeginProc(sig: PType; params: PObj); |
| 157 | -procedure UseRegs(mask: longint; at: int64); | |
| 157 | +procedure LocalsFromScratch; | |
| 158 | +procedure UseRegs(mask: longint); | |
| 158 | 159 | procedure MarkCode(var m: TCodeMark); |
| 159 | 160 | procedure Redo(const m: TCodeMark); |
| 160 | 161 | procedure EnterBody(c, line: longint); |
| @@ -203,6 +204,7 @@ | ||
| 203 | 204 | frameTop, frameMax, permTop: int64; |
| 204 | 205 | frameFix: longint; { where the frame size is patched in } |
| 205 | 206 | paramTop: int64; { the parameters' slots end here } |
| 207 | + zeroTop: int64; { the locals the prologue zeroes end here } | |
| 206 | 208 | retSlot: int64; { holds the hidden result pointer } |
| 207 | 209 | resType: PType; { the procedure's result, or nil } |
| 208 | 210 | exitL: longint; |
| @@ -1352,12 +1354,26 @@ | ||
| 1352 | 1354 | paramTop := frameTop; |
| 1353 | 1355 | end; |
| 1354 | 1356 | |
| 1355 | -{ The procedure keeps variables in the registers of mask, saved at at; | |
| 1356 | - the frame as it stands is where a second try at its body starts. } | |
| 1357 | -procedure UseRegs(mask: longint; at: int64); | |
| 1357 | +{ The frame as BeginProc left it, with the parameters' slots only: the | |
| 1358 | + locals are given theirs again once their registers are chosen. } | |
| 1359 | +procedure LocalsFromScratch; | |
| 1360 | +begin | |
| 1361 | + frameTop := paramTop; frameMax := paramTop; permTop := paramTop; | |
| 1362 | +end; | |
| 1363 | + | |
| 1364 | +{ The locals in memory have their slots: the prologue zeroes those and | |
| 1365 | + nothing below. The procedure keeps variables in the registers of | |
| 1366 | + mask, which are saved next, a quadword each, in slots that need no | |
| 1367 | + zeroing; the frame as it then stands is where a second try at the | |
| 1368 | + body starts. } | |
| 1369 | +procedure UseRegs(mask: longint); | |
| 1370 | +var i, k: longint; | |
| 1358 | 1371 | begin |
| 1372 | + zeroTop := permTop; | |
| 1359 | 1373 | regMask := mask; |
| 1360 | - saveAt := at; | |
| 1374 | + k := 0; | |
| 1375 | + for i := 0 to 4 do if mask and (1 shl i) <> 0 then inc(k); | |
| 1376 | + if k > 0 then saveAt := AllocPerm(8 * k, 8); | |
| 1361 | 1377 | frameAt[0] := frameTop; frameAt[1] := frameMax; frameAt[2] := permTop; |
| 1362 | 1378 | end; |
| 1363 | 1379 | |
| @@ -1377,9 +1393,10 @@ | ||
| 1377 | 1393 | end; |
| 1378 | 1394 | |
| 1379 | 1395 | { The prologue, which is for the procedure's first line. Only the |
| 1380 | - locals are zeroed: the parameters are stored over their slots next, | |
| 1381 | - and every temporary is written before it is read. The locals are all | |
| 1382 | - declared by now, below the parameters' slots. } | |
| 1396 | + locals in memory are zeroed, from the parameters' slots down to | |
| 1397 | + zeroTop: the parameters are stored over their slots next, the | |
| 1398 | + registers of variables are zeroed in place, their save slots are | |
| 1399 | + written before they are read, and so is every temporary. } | |
| 1383 | 1400 | procedure EnterBody(c, line: longint); |
| 1384 | 1401 | var i, top: longint; n: int64; p: PObj; |
| 1385 | 1402 | begin |
| @@ -1391,7 +1408,7 @@ | ||
| 1391 | 1408 | Say('sub rsp, frame'); |
| 1392 | 1409 | Put1($48); Put1($81); Put1($EC); { sub rsp, imm32 } |
| 1393 | 1410 | frameFix := pc; Put4(0); |
| 1394 | - n := (AlignUp(frameMax, 8) - paramTop) div 8; | |
| 1411 | + n := (AlignUp(zeroTop, 8) - paramTop) div 8; | |
| 1395 | 1412 | if n > 0 then MovRI(RAX, 0); |
| 1396 | 1413 | if n <= 4 then |
| 1397 | 1414 | for i := 1 to n do Store(Mem(RBP, -paramTop - 8 * i), RAX, 8) |
| @@ -1782,12 +1782,12 @@ | ||
| 1782 | 1782 | its "begin" to its end, a use counts 1, in a loop 4, in two 16, and |
| 1783 | 1783 | a variable ADR is taken of can't have one. A register costs a save |
| 1784 | 1784 | and a restore, so a variable used less than three times keeps its |
| 1785 | - place in memory. The registers are saved after the variables. } | |
| 1785 | + place in memory. } | |
| 1786 | 1786 | procedure ChooseRegs(params, before: PObj); |
| 1787 | 1787 | var |
| 1788 | 1788 | cand: array[0..63] of PObj; weight: array[0..63] of longint; |
| 1789 | 1789 | isLoop: array[0..255] of boolean; |
| 1790 | - n, i, k, d, loops, mask, best: longint; o: PObj; st: TScanState; afterDot, wasSkipping: boolean; | |
| 1790 | + n, i, k, d, loops, best: longint; o: PObj; st: TScanState; afterDot, wasSkipping: boolean; | |
| 1791 | 1791 | |
| 1792 | 1792 | function Find(name: longint): longint; |
| 1793 | 1793 | var j: longint; |
| @@ -1807,7 +1807,6 @@ | ||
| 1807 | 1807 | while o <> nil do begin Add(o); o := o^.next end; |
| 1808 | 1808 | if before = nil then o := curModule^.locals else o := before^.link; |
| 1809 | 1809 | while o <> nil do begin Add(o); o := o^.link end; |
| 1810 | - mask := 0; | |
| 1811 | 1810 | if n > 0 then begin |
| 1812 | 1811 | SaveScan(st); |
| 1813 | 1812 | wasSkipping := skipping; |
| @@ -1846,17 +1845,12 @@ | ||
| 1846 | 1845 | if (cand[i]^.reg = 0) and (weight[i] >= 3) and ((best < 0) or (weight[i] > weight[best])) then best := i; |
| 1847 | 1846 | if best < 0 then break; |
| 1848 | 1847 | cand[best]^.reg := VarRegs[k]; |
| 1849 | - mask := mask or (1 shl k); | |
| 1850 | 1848 | end; |
| 1851 | 1849 | end; |
| 1852 | - k := 0; | |
| 1853 | - for i := 0 to 4 do if mask and (1 shl i) <> 0 then inc(k); | |
| 1854 | - if k = 0 then UseRegs(0, 0) | |
| 1855 | - else UseRegs(mask, AllocPerm(8 * k, 8)); | |
| 1856 | 1850 | end; |
| 1857 | 1851 | |
| 1858 | -{ The registers the variables of the procedure are in, as ChooseRegs's | |
| 1859 | - mask } | |
| 1852 | +{ The registers the variables of the procedure are in, as bits of | |
| 1853 | + VarRegs } | |
| 1860 | 1854 | function RegsOf(params, before: PObj): longint; |
| 1861 | 1855 | var o: PObj; m, k: longint; |
| 1862 | 1856 | begin |
| @@ -1875,6 +1869,22 @@ | ||
| 1875 | 1869 | RegsOf := m; |
| 1876 | 1870 | end; |
| 1877 | 1871 | |
| 1872 | +{ Gives each local not in a register its slot in the frame, the slots | |
| 1873 | + the prologue zeroes, and then tells gen which registers the | |
| 1874 | + variables are in. A variable in a register has no slot: it starts at | |
| 1875 | + zero in its register, and the trap report finds it there. } | |
| 1876 | +procedure PlaceLocals(params, before: PObj); | |
| 1877 | +var o: PObj; | |
| 1878 | +begin | |
| 1879 | + LocalsFromScratch; | |
| 1880 | + if before = nil then o := curModule^.locals else o := before^.link; | |
| 1881 | + while o <> nil do begin | |
| 1882 | + if (o^.cls = oVar) and (o^.reg = 0) then o^.val := AllocPerm(o^.typ^.size, o^.typ^.align); | |
| 1883 | + o := o^.link; | |
| 1884 | + end; | |
| 1885 | + UseRegs(RegsOf(params, before)); | |
| 1886 | +end; | |
| 1887 | + | |
| 1878 | 1888 | procedure Body(p: PObj; sig: PType; line: longint); |
| 1879 | 1889 | var |
| 1880 | 1890 | q, params, last, o, before: PObj; old: PType; st: TScanState; |
| @@ -1905,6 +1915,7 @@ | ||
| 1905 | 1915 | before := curModule^.lastLocal; |
| 1906 | 1916 | Declarations(false); |
| 1907 | 1917 | ChooseRegs(params, before); |
| 1918 | + PlaceLocals(params, before); | |
| 1908 | 1919 | { the body, made again if a variable in a register must be in memory } |
| 1909 | 1920 | SaveScan(st); |
| 1910 | 1921 | MarkCode(mark); drops0 := numDrops; errs := errors; |
| @@ -1922,7 +1933,7 @@ | ||
| 1922 | 1933 | if not demoted or (errors <> errs) then break; |
| 1923 | 1934 | numDrops := drops0; |
| 1924 | 1935 | Redo(mark); |
| 1925 | - UseRegs(RegsOf(params, before), chunks^[p^.chunk].saveAt); | |
| 1936 | + PlaceLocals(params, before); | |
| 1926 | 1937 | RestoreScan(st); |
| 1927 | 1938 | until false; |
| 1928 | 1939 | EndProc(p^.chunk); |
| @@ -2227,10 +2238,8 @@ | ||
| 2227 | 2238 | o^.lev := 0; |
| 2228 | 2239 | o^.chunk := NewChunk(secBss, curModule^.sname, names[i]); |
| 2229 | 2240 | SetBss(o^.chunk, t^.size, t^.align); |
| 2230 | - end else begin | |
| 2231 | - o^.lev := 1; | |
| 2232 | - o^.val := AllocPerm(t^.size, t^.align); | |
| 2233 | - end; | |
| 2241 | + end else | |
| 2242 | + o^.lev := 1; { its slot comes from PlaceLocals, if it needs one } | |
| 2234 | 2243 | Declare(o); |
| 2235 | 2244 | end; |
| 2236 | 2245 | Expect(sSemicolon); |
| @@ -28,39 +28,36 @@ | ||
| 28 | 28 | 0 push rbp |
| 29 | 29 | 1 mov rbp, rsp |
| 30 | 30 | 4 sub rsp, 32 |
| 31 | - 11 xor eax, eax | |
| 32 | - 13 mov [rbp - 8], rax | |
| 33 | - 17 mov [rbp - 16], rax | |
| 34 | - 21 mov [rbp - 16], rbx | |
| 35 | - 25 xor ebx, ebx | |
| 31 | + 11 mov [rbp - 8], rbx | |
| 32 | + 15 xor ebx, ebx | |
| 36 | 33 | 20 for i := 1 to 3 do |
| 37 | - 27 mov rbx, 1 | |
| 38 | - 32 mov qword [rbp - 24], 3 | |
| 39 | - 40 mov r9, [rbp - 24] | |
| 40 | - 44 cmp rbx, r9 | |
| 41 | - 47 jg 139 | |
| 34 | + 17 mov rbx, 1 | |
| 35 | + 22 mov qword [rbp - 16], 3 | |
| 36 | + 30 mov r9, [rbp - 16] | |
| 37 | + 34 cmp rbx, r9 | |
| 38 | + 37 jg 129 | |
| 42 | 39 | 21 if i # 2 then total := add(total, twice(i)); end; |
| 43 | - 53 cmp rbx, 2 | |
| 44 | - 57 je 107 | |
| 45 | - 63 mov r8, [Listing.total] | |
| 46 | - 70 mov [rbp - 32], r8 | |
| 47 | - 74 mov rdi, rbx | |
| 48 | - 77 call Listing.twice | |
| 49 | - 82 mov r9, rax | |
| 50 | - 85 mov rdi, [rbp - 32] | |
| 51 | - 89 mov rsi, r9 | |
| 52 | - 92 call Listing.add | |
| 53 | - 97 mov r8, rax | |
| 54 | - 100 mov [Listing.total], r8 | |
| 55 | - 107 mov r8, [rbp - 24] | |
| 56 | - 111 sub r8, rbx | |
| 57 | - 114 cmp r8, 1 | |
| 58 | - 118 jb 139 | |
| 59 | - 124 mov r8, rbx | |
| 60 | - 127 add r8, 1 | |
| 61 | - 131 mov rbx, r8 | |
| 62 | - 134 jmp 40 | |
| 40 | + 43 cmp rbx, 2 | |
| 41 | + 47 je 97 | |
| 42 | + 53 mov r8, [Listing.total] | |
| 43 | + 60 mov [rbp - 24], r8 | |
| 44 | + 64 mov rdi, rbx | |
| 45 | + 67 call Listing.twice | |
| 46 | + 72 mov r9, rax | |
| 47 | + 75 mov rdi, [rbp - 24] | |
| 48 | + 79 mov rsi, r9 | |
| 49 | + 82 call Listing.add | |
| 50 | + 87 mov r8, rax | |
| 51 | + 90 mov [Listing.total], r8 | |
| 52 | + 97 mov r8, [rbp - 16] | |
| 53 | + 101 sub r8, rbx | |
| 54 | + 104 cmp r8, 1 | |
| 55 | + 108 jb 129 | |
| 56 | + 114 mov r8, rbx | |
| 57 | + 117 add r8, 1 | |
| 58 | + 121 mov rbx, r8 | |
| 59 | + 124 jmp 30 | |
| 63 | 60 | 23 end main; |
| 64 | - 139 mov rbx, [rbp - 16] | |
| 65 | - 143 leave | |
| 66 | - 144 ret | |
| 61 | + 129 mov rbx, [rbp - 8] | |
| 62 | + 133 leave | |
| 63 | + 134 ret |