raz / src / README.md

165 lines · 10.1 KiB · raw · history · last change 6d1c7219

src: the compiler in raz #

The port of the Pascal compiler, the seed, to raz. The seed built src/ from the port to v0.19.0, and went in v0.20.0; git has it, in seed/ at the tag v0.19.0. Since then the release ../BOOT names builds src/ (boot.sh fetches it), and a comment here that names the seed means the Pascal compiler.

One module to a seed unit, with the seed's name, written to the same rules: plain code, each module opening with what it is, what it depends on and what it leaves out.

make prev-bench  # the compiler's speed against BOOT's: a tenth slower fails
make fixpoint    # BOOT's builds src, that builds src, that builds it again

While it was ported, each unit was tested against the seed's: a pair of dumpers, one over each side's units, read the same files or ran the same pseudo-random script of calls, and had to print the same, with the same messages and status; the compiler built whole had to give every program the messages and status the seed gave. That test was dropped after v0.17.0: the two had grown apart, the compiler making better code since v0.10.1 and following newer reports since, and make check, conform/ and the fixpoint test the compiler as it is. test/Recover.raz, the pattern of going on after an error, is run by make check.

make fixpoint (fixpoint.sh) is the proof that the compiler is whole: BOOT's release builds Raz.raz (stage 1), stage 1 builds it (stage 2), and stage 2 builds it again (stage 3). Stages 2 and 3 must be the same file; they are (stage 1 is not, when BOOT's compiler makes other code than this one; until v0.19.0 the seed was stage 0). Stage 3 then stands in for ./raz, and make check and make bench run on it. Each stage is built by the same command in the same place, since a program holds the paths it was made from.

What the port found #

What the port found in the seed, which the seed kept; a record now.

source
1# src: the compiler in raz
2
3The port of the Pascal compiler, the **seed**, to raz. The seed built
4`src/` from the port to v0.19.0, and went in v0.20.0; git has it, in
5`seed/` at the tag v0.19.0. Since then the release `../BOOT` names
6builds `src/` (`boot.sh` fetches it), and a comment here that names
7the seed means the Pascal compiler.
8
9One module to a seed unit, with the seed's name, written to the same
10rules: plain code, each module opening with what it is, what it
11depends on and what it leaves out.
12
13 make prev-bench # the compiler's speed against BOOT's: a tenth slower fails
14 make fixpoint # BOOT's builds src, that builds src, that builds it again
15
16While it was ported, each unit was tested against the seed's: a pair
17of dumpers, one over each side's units, read the same files or ran the
18same pseudo-random script of calls, and had to print the same, with
19the same messages and status; the compiler built whole had to give
20every program the messages and status the seed gave. That test was
21dropped after v0.17.0: the two had grown apart, the compiler making
22better code since v0.10.1 and following newer reports since, and
23`make check`, `conform/` and the fixpoint test the compiler as it is.
24`test/Recover.raz`, the pattern of going on after an error, is run by
25`make check`.
26
27`make fixpoint` (`fixpoint.sh`) is the proof that the compiler is
28whole: BOOT's release builds `Raz.raz` (stage 1), stage 1 builds it
29(stage 2), and stage 2 builds it again (stage 3). Stages 2 and 3 must
30be the same file; they are (stage 1 is not, when BOOT's compiler makes
31other code than this one; until v0.19.0 the seed was stage 0). Stage 3 then stands in for `./raz`, and `make check` and
32`make bench` run on it. Each stage is built by the same command in the
33same place, since a program holds the paths it was made from.
34
35## What the port found
36
37What the port found in the seed, which the seed kept; a record now.
38
39- **Names.** An exported variable is read-only outside its module, so
40 what the parser sets in the seed's `diag` and `scan` (`quiet`,
41 `werror`, `recovery`, `skipping`) have setters. The `sbuf` array,
42 which can't be sliced outside, is `strBytes`.
43- **Going on after an error.** raz has no `setjmp`, and the seed's parser
44 ends its errors in a jump back to the statement or declaration being
45 read, which is how it reports up to 20 errors in a run. The language
46 gained `try` (report 2026-10.20) for this: `Scan.mark` and the others
47 show the error and return `Scan.Failed`, which the parser passes up
48 with `try` to where it reads on. `test/Recover.raz` is the pattern.
49- **`NO_COLOR` set to nothing.** `Sys.env` gives "" for a variable that
50 is unset and for one that is empty, so the port reads the second as
51 the first. no-color.org says a variable that is empty doesn't turn
52 colour off, so the port is the one that follows it and the seed
53 differs; no test depends on either.
54- **Strings are slices.** A message is a `[]U8` made by `Diag.say`,
55 which keeps what it makes: messages are made only to be shown. A name
56 from `nameOf` stays valid, since names are kept in chunks that are
57 never moved; the seed's was a copy.
58- **Reserved words as names.** The seed's `code` (the text buffer of
59 `Chunk`) is `text`; `module` (a record's, an import's) is `home`; an
60 object's `link` is `chain`. A seed type `TName` is `Name`, and the
61 procedures begin in lower case, as `Scan`'s do.
62- **Errors in `Sym`.** `insert` (a name declared twice) and `openScope`
63 (too many scopes) show the error and return `Scan.Failed`, as `mark`
64 does, for the parser to pass up with `try`. `saveScopes` returns what
65 the seed's `var d` gave.
66- **Setters.** What other units set in `Chunk` has one each (`setLive`,
67 `setAdr`, `setReals`, `setSaves`, `setCurFile`, and `rewindTo` for
68 gen's `Redo` and `Rewind`), and `Sym` has `setCurModule`. `Scan.grab`
69 is exported for the memory they all take.
70- **No `continue`.** The seed's loops that skip to the next round
71 (`MarkLive`, the line program, the assembler's statements) have the
72 rest of the body in an `else`, or in a procedure of its own.
73- **`X64`.** `code` of `Chunk` being reserved, its text is `Chunk.text`;
74 `Say` and the rest of the encoder keep their names, in lower case.
75 `ConstFn`, the parser's reader of a constant, returns an `ERROR` too,
76 and `assemble` returns what it passes up. `mem` marks an offset beyond
77 32 bits and goes on, since an operand has nowhere to return an error
78 to; the compile ends with the message shown. The listing's text is
79 made by `Diag.say`, which keeps it, and only when `-S` asks for it.
80- **`Link` and `Exe`.** What `raz.pas` set there has a setter each:
81 `Link.setPlaces`, `Exe.needLib`, `noLibs`, `setMainPath`,
82 `setStripped` and `X64.setListing`. The file is written by
83 `Files.write` and made executable by `Sys.chmod`, in place of one
84 `open` with mode 755 (which the umask would trim).
85- **`Gen`.** The seed's `Mark` in the middle of a push or a check
86 can't give an error back: `newEntry` (the stack's 256 places) and
87 `check` (4,096 checks a procedure) show it and go on without doing
88 what they couldn't, and the parser's `resetStack` makes the stack
89 right again; `endProc` returns its error ("frame too big") and
90 `codeProc` the assembler's. A `var` out of the seed became a result
91 (`topConst`, `operands`, `deferBegin`); the labels, which are chains
92 that a call extends, stay `var`. `inPlace` has a setter. The seed's
93 `Call` record is `CallInfo` (the procedure is `call`) and `TLink` is
94 `Link`. A `match` on an enumeration takes one name to an arm, and a
95 set holds at most 64 names, so `Scan.Sym` (over 64) has none: the
96 seed's sets of its names are chains of `=`.
97- **`Item`.** The seed's `Mark` ended in a jump, so here every
98 routine that can find an error returns an `ERROR` as its last result
99 and the ones that call it pass it up with `try`; `Item` shows the
100 message and returns what the seed jumped out of, so a script that
101 goes on after an error sees the same state. The seed's `Mismatch` of
102 two operands is `mismatchOps`, `demoted` and `checks` have setters,
103 and `matchBegin`, `toSlot` and `errorChunk` return their value and
104 the error. `op2` has a parameter it never read, now `_L`. A message
105 is made by `Diag.say` from strings, with no `shortstring` to add up.
106 A `Mark` with no place is at the scanner's, as in the seed.
107- **No `const` parameter.** The seed's `const x: TItem` is a value
108 parameter here, so an item of about 130 bytes is copied at each call
109 of `what`, `adrOf`, `notValue` and the others. The result is the
110 same; whether the compiler built from this is slower for it is for
111 the speed test of step 4 to say. A `const` parameter (a hint that
112 the callee won't change it, so that the caller may pass its address)
113 would be a language change. (It came in
114 report 2026-10.33; since v0.20.0, built by v0.19.0, which has it,
115 the routines that every program's compile goes through take their
116 `Item` as `const`, measured, as `doc/compiler.md` says.)
117- **`Parse` and the seed's jumps.** The seed's `Guard`, `setjmp` and
118 `longjmp` are `guard`, a record of what to put back, and the `ERROR`
119 that comes up through `try` to the statement or declaration that is
120 the recovery point (`statement`, `declaration`), which looks at it,
121 puts the state back (`restore`) and skips ahead, as the seed does.
122 `giveUp`, the seed's `GiveUp`, returns the error on to the next one,
123 or ends the compile if there is none (`guards` counts them). A routine
124 that can't fail has no `ERROR` (`declarations`, `declaration`). The
125 seed's nested procedures (`ChooseRegs`'s three) are procedures of the
126 module with its candidates in variables. `defer` and `module` being
127 words, the seed's `Defer` is `deferStatement`. A message is made
128 from strings with `Diag.say`, which has no shortstring to add up to.
129 `Scan.skipping` is exported to be read, and `Scan.strWithZero` gives
130 `Item.makeStr` a pointer to a string that may be empty: the seed's
131 `@sbuf` indexed nothing, and `ADR(Scan.strBytes()[0])`, which the
132 port of `Item` had, stopped on `""`.
133- **`Raz`.** The program is `proc main*: INT`. Where it is, for the
134 library beside it, it reads from `/proc/self/exe` as FPC's `ParamStr(0)`
135 does: `Files.arg(0)` is what the shell was typed, and `./raz` made the
136 library `./lib`, so the same program held other paths in its file.
137 `raz run` writes the memfd with `Exe.write` and executes it with
138 the system call, `execve`, which `Sys` has no procedure for. The
139 version is the module `Version`, which `check.sh` makes in
140 `build/src/` from `build/version.inc`, as the seed's `{$I}` is.
141 Two names of a file, for a module found in two places, are told by
142 the device and inode `SYSCALL` `stat` gives, which `Sys.info` lacks.
143- **Sets of `Scan.Sym`.** It has 76 names and a set holds 64, so the
144 operators a rule names are chains of `=` or the helpers `oneOf` and
145 `isRel`. (A `match` arm with several names, which the seed's `case`
146 has, was added to the report for this: 2026-10.21.)
147- **Strings made by the compiler.** A joined string (`"a" + "b"`) and
148 an error's text are built in memory from `Scan.grab`, which is never
149 freed, in place of `GetMem` and `FreeMem`; the compiler runs once.
150- **No bit cast.** A real's bits and back go through a pointer.
151- **Speed.** On the 348,025 lines of `test/genbench.sh`, 7.6 MB, the
152 seed's scanner takes 75 ms and this one 74. It took 121 at first.
153 What closed the gap, in the source: a byte loop in place of the slice
154 comparison `a[0..n] = b[0..n]` (97 ms), a 256-entry table for the
155 bytes of a name (84), and one for the symbols that are a byte each
156 (75). What is left for the code generator, which would help every
157 module: the slice comparison is slower than the loop it stands for.
158 On the whole compiler, the same 348,025 lines take 427 ms with the
159 seed's and 770 with this one (452,000 lines a second, over the 400,000
160 the budget wants): `Scan.tokPos`, a record of three words returned
161 through a pointer, is 9% of it, and the rest is spread thin; the
162 `const` parameter of `Item` isn't what stands out.
163 (The seed's `match` on an integer was a chain of compares, each
164 loading the selector from the frame and jumping over a `jmp`, where
165 FPC makes a jump table; now it makes one too.)