raz / commit 6c50d7c8

commit
6c50d7c897e3457d08ddebe6b78f3fe8f12ed086
author
Luis <42945842+ldiaz94@users.noreply.github.com>
date
parent
d8137081
Checks out of the way

A check now jumps, when it fails, to code at the end of its procedure
(EndFails): where it stands it is a compare and a jump not taken. The
failing code puts the values where Rt.fail wants them and calls a stub
for its kind (failStubs, made by EntryPoint), which says the kind,
takes the place from its return address and aligns the stack; it was
lea, mov, and, call and ud2 inline at every check. Code is aligned to
32 bytes, the windows x86 fetches it in: at 16, rat ran 5% slower, by
where its loops fell, which is worth the 0.6% the padding costs.
rat's text 581,988 to 562,668 bytes (-3.3%), its speed within 1%;
the tools and examples 532,023 to 520,167. 8,373 lines of code.

patch · log

4 files changed, +82 -33

doc/compiler.md (history) modified +14 -8

@@ -1,6 +1,6 @@
11 # Reading the raz compiler
22
3-The compiler is eleven files of Free Pascal, under 8,300 lines of code.
3+The compiler is eleven files of Free Pascal, under 8,400 lines of code.
44 This guide walks through them in the order they are best read: from the
55 characters of a source file to the bytes of an executable. It assumes
66 you know the language from `doc/raz.md`, and enough x86-64 to read a
@@ -11,9 +11,9 @@
1111 | `src/diag.pas` | 310 | 229 | Messages: positions, source excerpts, colour, counts |
1212 | `src/scan.pas` | 886 | 747 | Symbols, literals, comments, positions |
1313 | `src/sym.pas` | 617 | 512 | Types, objects, scopes, layout |
14-| `src/chunk.pas` | 300 | 234 | The compiled program: chunks, fixups, line notes |
14+| `src/chunk.pas` | 302 | 234 | The compiled program: chunks, fixups, line notes |
1515 | `src/x64.pas` | 1,113 | 930 | The instruction encoder, jumps within a procedure, the assembler |
16-| `src/gen.pas` | 1,570 | 1,232 | The stack machine, for x86-64: values, operations, checks, calls, frames |
16+| `src/gen.pas` | 1,611 | 1,263 | The stack machine, for x86-64: values, operations, checks, calls, frames |
1717 | `src/item.pas` | 1,433 | 1,194 | Items: types, constants, variables, operators, conversions, calls |
1818 | `src/parse.pas` | 2,904 | 2,575 | Declarations, statements, expressions, built-ins, recovery, warnings |
1919 | `src/link.pas` | 247 | 190 | Tree shaking, layout, patching, the places |
@@ -320,11 +320,17 @@
320320 a shift's count is cut to their width first, and `DivTrap` makes the
321321 smallest value of one divided by -1 fault as an `I64`'s does.
322322
323-Checks: `Check` jumps over a call to `Rt.fail` when the check holds.
324-The call passes what failed, the values involved and its own address,
325-by which `Rt` finds the place; that code has the place of the
326-offending expression (`LineCol` in `scan.pas`), and the code after it
327-the statement's again.
323+Checks: `Check` jumps, when the check fails, to code at the end of the
324+procedure (`EndFails`), so that a check that holds is a jump not taken.
325+That code puts the values involved where `Rt.fail` wants them and
326+calls a stub for its kind of failure (`failStubs`, made by
327+`EntryPoint`), which says the kind and passes the address it was
328+called from, by which `Rt` finds the place: the failing code has the
329+place of the offending expression (`LineCol` in `scan.pas`). A check
330+costs a compare and a jump where it stands, and a dozen bytes out of
331+the way. Code is aligned to 32 bytes, the windows x86 fetches it in,
332+so that where a procedure's loops fall doesn't depend on what comes
333+before it.
328334
329335 What the machine catches, the code doesn't test for (report section
330336 10, appendix A): on x86-64 Linux a division by zero raises SIGFPE, a

src/chunk.pas modified +6 -4

@@ -168,12 +168,14 @@
168168
169169 procedure BeginChunk(c: longint);
170170 begin
171- { code is aligned to 16 bytes, padded with CC, x86's int3: a trap if
172- it were ever run }
173- while pc and 15 <> 0 do Put1($CC);
171+ { code is aligned to 32 bytes, the windows in which x86 fetches and
172+ caches it, so that a procedure's loops sit in them the same way
173+ whatever comes before: rat ran 5% slower at 16, by where its loops
174+ fell. Padded with CC, x86's int3: a trap if it were ever run }
175+ while pc and 31 <> 0 do Put1($CC);
174176 curChunk := c;
175177 chunks^[c].start := pc;
176- chunks^[c].align := 16;
178+ chunks^[c].align := 32;
177179 chunks^[c].firstFix := numFixups;
178180 end;
179181

src/gen.pas modified +61 -20

@@ -94,6 +94,9 @@
9494
9595 var
9696 rtFail, argcChunk, argvChunk: longint;
97+ { the stubs that Check's code calls, one for each kind of failure,
98+ made by EntryPoint for those used }
99+ failStubs: array[0..5] of longint = (-1, -1, -1, -1, -1, -1);
97100 depth: longint; { the values on the stack }
98101 pushed: longint; { quadwords pushed on the machine's stack }
99102 codeChunk: longint = -1; { the procedure or body being compiled, or -1 }
@@ -194,6 +197,7 @@
194197 end;
195198
196199 const
200+ StubNames: array[0..5] of string[6] = ('check0', 'check1', 'check2', 'check3', 'check4', 'check5');
197201 { the registers of the stack: the value at depth d is in Regs[d mod
198202 NumRegs]. rax, rdx and rcx, which division and shifts need, and rsi
199203 and rdi, which copying needs, aren't among them. }
@@ -220,6 +224,13 @@
220224 regMask: longint; { the registers of variables, as bits of VarRegs }
221225 saveAt: int64; { where they are saved, a quadword each }
222226 frameAt: array[0..2] of int64; { frameTop, frameMax, permTop after the declarations }
227+ { the checks of the procedure, whose failures EndFails makes }
228+ fails: array[0..4095] of record
229+ L, what, ra, name, at: longint;
230+ m: TMem;
231+ n: int64;
232+ end;
233+ numFails: longint;
223234
224235 function KindOf(t: PType): TKind;
225236 begin
@@ -980,27 +991,42 @@
980991 a..b (1), each for a length len, a position a of the enumeration
981992 called name (2), or a slice of a pointer a..b that runs backwards
982993 (3). a is in register ra, b in RDX; len is in memory at m, or n if
983- m.base is NoBase. The failing code has p for its place, and the code
984- after it the place before it again; it never returns, and may use
985- any register once it has read what it needs. }
994+ m.base is NoBase. The failing code is out of the way, at the
995+ procedure's end (EndFails), so that a check that holds costs a jump
996+ not taken; it has p for its place, never returns, and may use any
997+ register once it has read what it needs. }
986998 procedure Check(cc, what: longint; const p: TPos; ra: longint; const m: TMem; n: int64; name: longint);
987-var L, line, col: longint; before: TLine;
999+begin
1000+ if numFails > High(fails) then Mark('more than ' + IntStr(High(fails) + 1) + ' checks in one procedure');
1001+ fails[numFails].L := NoLabel;
1002+ Jcc(cc xor 1, fails[numFails].L); { x86's conditions come in pairs, the other one its last bit flipped }
1003+ fails[numFails].what := what;
1004+ fails[numFails].ra := ra;
1005+ fails[numFails].m := m;
1006+ fails[numFails].n := n;
1007+ fails[numFails].name := name;
1008+ fails[numFails].at := p.a;
1009+ inc(numFails);
1010+end;
1011+
1012+{ The failures of the procedure's checks: each gives Rt.fail its values
1013+ and the place of the offending expression, through a stub for its
1014+ kind, which takes the place from where it was called }
1015+procedure EndFails;
1016+var i, line, col: longint;
9881017 begin
989- L := NoLabel;
990- Jcc(cc, L);
991- before := lines^[numLines - 1];
992- LineCol(p.a, line, col);
993- NoteLine(line, col);
994- MovRR(RSI, ra);
995- if m.base = NoBase then MovRI(RCX, n) else Load(RCX, m, 8, false);
996- Lea(R8, Here);
997- MovRI(RDI, what);
998- if name >= 0 then Lea(R9, MemRip(name, 0));
999- AluI(aluAnd, RSP, -16);
1000- CallChunk(rtFail);
1001- Ud2;
1002- FixLink(L);
1003- NoteLine(before.line, before.col);
1018+ for i := 0 to numFails - 1 do
1019+ with fails[i] do begin
1020+ FixLink(L);
1021+ LineCol(at, line, col);
1022+ NoteLine(line, col);
1023+ if ra <> RSI then MovRR(RSI, ra);
1024+ if m.base = NoBase then MovRI(RCX, n) else Load(RCX, m, 8, false);
1025+ if name >= 0 then Lea(R9, MemRip(name, 0));
1026+ if failStubs[what] < 0 then failStubs[what] := NewChunk(secText, Intern('Rt', 2), Intern(@StubNames[what][1], 6));
1027+ CallChunk(failStubs[what]);
1028+ end;
1029+ numFails := 0;
10041030 end;
10051031
10061032 { For Check: a length known here, not in memory }
@@ -1340,6 +1366,7 @@
13401366 procedure Redo(const m: TCodeMark);
13411367 begin
13421368 pc := m.pc; numFixups := m.fixups; numLines := m.lines; Unsay(m.said);
1369+ numFails := 0;
13431370 frameTop := frameAt[0]; frameMax := frameAt[1]; permTop := frameAt[2];
13441371 exitL := NoLabel;
13451372 end;
@@ -1461,6 +1488,7 @@
14611488 end;
14621489 Leave;
14631490 Ret;
1491+ EndFails;
14641492 n := AlignUp(frameMax, 16);
14651493 if n < 16 then n := 16;
14661494 if n > high(longint) then Mark('frame too big');
@@ -1503,7 +1531,7 @@
15031531 nil, nil, rtld_fini, stack_end), with rsp aligned; its main first
15041532 gives Rt C's exit, in cExit. }
15051533 function EntryPoint(dynamic: boolean; first: PModule; main: PObj; cExit, places, placesVar, exitProc: longint): longint;
1506-var entry, start: longint; m: PModule;
1534+var entry, start, i: longint; m: PModule;
15071535 begin
15081536 entry := NewChunk(secText, -1, Intern('_start', 6));
15091537 if dynamic then begin
@@ -1548,6 +1576,19 @@
15481576 CallChunk(exitProc);
15491577 Ud2;
15501578 EndChunk(start);
1579+ { Rt.fail, for the failures of each kind: the place is where the
1580+ check's call is, a byte before where it would return to }
1581+ for i := 0 to High(failStubs) do
1582+ if failStubs[i] >= 0 then begin
1583+ BeginChunk(failStubs[i]);
1584+ MovRI(RDI, i);
1585+ Load(R8, Mem(RSP, 0), 8, false);
1586+ AluI(aluSub, R8, 1);
1587+ AluI(aluAnd, RSP, -16);
1588+ CallChunk(rtFail);
1589+ Ud2;
1590+ EndChunk(failStubs[i]);
1591+ end;
15511592 EntryPoint := entry;
15521593 end;
15531594

src/link.pas modified +1 -1

@@ -202,7 +202,7 @@
202202 procedure LinkAll(headerSize, roExtra, dynSize: int64);
203203 var c, f, at: longint; e, p, v: int64;
204204 begin
205- textOff := (headerSize + 15) div 16 * 16; { as each code chunk is }
205+ textOff := (headerSize + 31) div 32 * 32; { as each code chunk is }
206206 e := Place(secText, Base + textOff);
207207 textSize := e - (Base + textOff);
208208 if placesChunk >= 0 then MakePlaces(Base + textOff, e);