From 6c50d7c897e3457d08ddebe6b78f3fe8f12ed086 Mon Sep 17 00:00:00 2001 From: Luis <42945842+ldiaz94@users.noreply.github.com> Date: Mon, 12 Oct 2026 14:20:29 +0100 Subject: [PATCH] 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. --- diff --git a/doc/compiler.md b/doc/compiler.md index 1e9dcae..2c97534 100644 --- a/doc/compiler.md +++ b/doc/compiler.md @@ -1,6 +1,6 @@ # Reading the raz compiler -The compiler is eleven files of Free Pascal, under 8,300 lines of code. +The compiler is eleven files of Free Pascal, under 8,400 lines of code. This guide walks through them in the order they are best read: from the characters of a source file to the bytes of an executable. It assumes you know the language from `doc/raz.md`, and enough x86-64 to read a @@ -11,9 +11,9 @@ | `src/diag.pas` | 310 | 229 | Messages: positions, source excerpts, colour, counts | | `src/scan.pas` | 886 | 747 | Symbols, literals, comments, positions | | `src/sym.pas` | 617 | 512 | Types, objects, scopes, layout | -| `src/chunk.pas` | 300 | 234 | The compiled program: chunks, fixups, line notes | +| `src/chunk.pas` | 302 | 234 | The compiled program: chunks, fixups, line notes | | `src/x64.pas` | 1,113 | 930 | The instruction encoder, jumps within a procedure, the assembler | -| `src/gen.pas` | 1,570 | 1,232 | The stack machine, for x86-64: values, operations, checks, calls, frames | +| `src/gen.pas` | 1,611 | 1,263 | The stack machine, for x86-64: values, operations, checks, calls, frames | | `src/item.pas` | 1,433 | 1,194 | Items: types, constants, variables, operators, conversions, calls | | `src/parse.pas` | 2,904 | 2,575 | Declarations, statements, expressions, built-ins, recovery, warnings | | `src/link.pas` | 247 | 190 | Tree shaking, layout, patching, the places | @@ -320,11 +320,17 @@ a shift's count is cut to their width first, and `DivTrap` makes the smallest value of one divided by -1 fault as an `I64`'s does. -Checks: `Check` jumps over a call to `Rt.fail` when the check holds. -The call passes what failed, the values involved and its own address, -by which `Rt` finds the place; that code has the place of the -offending expression (`LineCol` in `scan.pas`), and the code after it -the statement's again. +Checks: `Check` jumps, when the check fails, to code at the end of the +procedure (`EndFails`), so that a check that holds is a jump not taken. +That code puts the values involved where `Rt.fail` wants them and +calls a stub for its kind of failure (`failStubs`, made by +`EntryPoint`), which says the kind and passes the address it was +called from, by which `Rt` finds the place: the failing code has the +place of the offending expression (`LineCol` in `scan.pas`). A check +costs a compare and a jump where it stands, and a dozen bytes out of +the way. Code is aligned to 32 bytes, the windows x86 fetches it in, +so that where a procedure's loops fall doesn't depend on what comes +before it. What the machine catches, the code doesn't test for (report section 10, appendix A): on x86-64 Linux a division by zero raises SIGFPE, a diff --git a/src/chunk.pas b/src/chunk.pas index 993889f..71054df 100644 --- a/src/chunk.pas +++ b/src/chunk.pas @@ -168,12 +168,14 @@ procedure BeginChunk(c: longint); begin - { code is aligned to 16 bytes, padded with CC, x86's int3: a trap if - it were ever run } - while pc and 15 <> 0 do Put1($CC); + { code is aligned to 32 bytes, the windows in which x86 fetches and + caches it, so that a procedure's loops sit in them the same way + whatever comes before: rat ran 5% slower at 16, by where its loops + fell. Padded with CC, x86's int3: a trap if it were ever run } + while pc and 31 <> 0 do Put1($CC); curChunk := c; chunks^[c].start := pc; - chunks^[c].align := 16; + chunks^[c].align := 32; chunks^[c].firstFix := numFixups; end; diff --git a/src/gen.pas b/src/gen.pas index b2866bd..fd2910c 100644 --- a/src/gen.pas +++ b/src/gen.pas @@ -94,6 +94,9 @@ var rtFail, argcChunk, argvChunk: longint; + { the stubs that Check's code calls, one for each kind of failure, + made by EntryPoint for those used } + failStubs: array[0..5] of longint = (-1, -1, -1, -1, -1, -1); depth: longint; { the values on the stack } pushed: longint; { quadwords pushed on the machine's stack } codeChunk: longint = -1; { the procedure or body being compiled, or -1 } @@ -194,6 +197,7 @@ end; const + StubNames: array[0..5] of string[6] = ('check0', 'check1', 'check2', 'check3', 'check4', 'check5'); { the registers of the stack: the value at depth d is in Regs[d mod NumRegs]. rax, rdx and rcx, which division and shifts need, and rsi and rdi, which copying needs, aren't among them. } @@ -220,6 +224,13 @@ regMask: longint; { the registers of variables, as bits of VarRegs } saveAt: int64; { where they are saved, a quadword each } frameAt: array[0..2] of int64; { frameTop, frameMax, permTop after the declarations } + { the checks of the procedure, whose failures EndFails makes } + fails: array[0..4095] of record + L, what, ra, name, at: longint; + m: TMem; + n: int64; + end; + numFails: longint; function KindOf(t: PType): TKind; begin @@ -980,27 +991,42 @@ a..b (1), each for a length len, a position a of the enumeration called name (2), or a slice of a pointer a..b that runs backwards (3). a is in register ra, b in RDX; len is in memory at m, or n if - m.base is NoBase. The failing code has p for its place, and the code - after it the place before it again; it never returns, and may use - any register once it has read what it needs. } + m.base is NoBase. The failing code is out of the way, at the + procedure's end (EndFails), so that a check that holds costs a jump + not taken; it has p for its place, never returns, and may use any + register once it has read what it needs. } procedure Check(cc, what: longint; const p: TPos; ra: longint; const m: TMem; n: int64; name: longint); -var L, line, col: longint; before: TLine; +begin + if numFails > High(fails) then Mark('more than ' + IntStr(High(fails) + 1) + ' checks in one procedure'); + fails[numFails].L := NoLabel; + Jcc(cc xor 1, fails[numFails].L); { x86's conditions come in pairs, the other one its last bit flipped } + fails[numFails].what := what; + fails[numFails].ra := ra; + fails[numFails].m := m; + fails[numFails].n := n; + fails[numFails].name := name; + fails[numFails].at := p.a; + inc(numFails); +end; + +{ The failures of the procedure's checks: each gives Rt.fail its values + and the place of the offending expression, through a stub for its + kind, which takes the place from where it was called } +procedure EndFails; +var i, line, col: longint; begin - L := NoLabel; - Jcc(cc, L); - before := lines^[numLines - 1]; - LineCol(p.a, line, col); - NoteLine(line, col); - MovRR(RSI, ra); - if m.base = NoBase then MovRI(RCX, n) else Load(RCX, m, 8, false); - Lea(R8, Here); - MovRI(RDI, what); - if name >= 0 then Lea(R9, MemRip(name, 0)); - AluI(aluAnd, RSP, -16); - CallChunk(rtFail); - Ud2; - FixLink(L); - NoteLine(before.line, before.col); + for i := 0 to numFails - 1 do + with fails[i] do begin + FixLink(L); + LineCol(at, line, col); + NoteLine(line, col); + if ra <> RSI then MovRR(RSI, ra); + if m.base = NoBase then MovRI(RCX, n) else Load(RCX, m, 8, false); + if name >= 0 then Lea(R9, MemRip(name, 0)); + if failStubs[what] < 0 then failStubs[what] := NewChunk(secText, Intern('Rt', 2), Intern(@StubNames[what][1], 6)); + CallChunk(failStubs[what]); + end; + numFails := 0; end; { For Check: a length known here, not in memory } @@ -1340,6 +1366,7 @@ procedure Redo(const m: TCodeMark); begin pc := m.pc; numFixups := m.fixups; numLines := m.lines; Unsay(m.said); + numFails := 0; frameTop := frameAt[0]; frameMax := frameAt[1]; permTop := frameAt[2]; exitL := NoLabel; end; @@ -1461,6 +1488,7 @@ end; Leave; Ret; + EndFails; n := AlignUp(frameMax, 16); if n < 16 then n := 16; if n > high(longint) then Mark('frame too big'); @@ -1503,7 +1531,7 @@ nil, nil, rtld_fini, stack_end), with rsp aligned; its main first gives Rt C's exit, in cExit. } function EntryPoint(dynamic: boolean; first: PModule; main: PObj; cExit, places, placesVar, exitProc: longint): longint; -var entry, start: longint; m: PModule; +var entry, start, i: longint; m: PModule; begin entry := NewChunk(secText, -1, Intern('_start', 6)); if dynamic then begin @@ -1548,6 +1576,19 @@ CallChunk(exitProc); Ud2; EndChunk(start); + { Rt.fail, for the failures of each kind: the place is where the + check's call is, a byte before where it would return to } + for i := 0 to High(failStubs) do + if failStubs[i] >= 0 then begin + BeginChunk(failStubs[i]); + MovRI(RDI, i); + Load(R8, Mem(RSP, 0), 8, false); + AluI(aluSub, R8, 1); + AluI(aluAnd, RSP, -16); + CallChunk(rtFail); + Ud2; + EndChunk(failStubs[i]); + end; EntryPoint := entry; end; diff --git a/src/link.pas b/src/link.pas index a5bef05..dabec8e 100644 --- a/src/link.pas +++ b/src/link.pas @@ -202,7 +202,7 @@ procedure LinkAll(headerSize, roExtra, dynSize: int64); var c, f, at: longint; e, p, v: int64; begin - textOff := (headerSize + 15) div 16 * 16; { as each code chunk is } + textOff := (headerSize + 31) div 32 * 32; { as each code chunk is } e := Place(secText, Base + textOff); textSize := e - (Base + textOff); if placesChunk >= 0 then MakePlaces(Base + textOff, e);