raz / commit 088a65e9

commit
088a65e9b41cb6a5c8ccd7bb0d6e02f54d4946b9
author
Luis <42945842+ldiaz94@users.noreply.github.com>
date
parent
28428d5b
Plan: no IR or object files; the parser reaches the machine through gen

Separate compilation is the implementation's (report, appendix A), and a
whole program compiles in well under a second, so the IR isn't written
down: doc/ir.md goes, with steps 9 (object files) and 9f (tools/Obj.raz).
Step 7 makes gen the machine boundary a port rewrites; step 8 gives the
register stack its rules (saves around calls, spills, empty at defers).
Appendix A says this compiler compiles every module together.

patch · log

3 files changed, +29 -418

PLAN.md (history) modified +27 -19

@@ -66,7 +66,8 @@
6666
6767 ## Proposed, v0.6.0: raz to be rebuilt by anyone
6868
69-Under way: steps 1 to 3 are agreed; step 4 is for review. The freeze
69+Under way: steps 1 to 4 are done, step 3 set aside since; the change
70+to steps 7 and 8, and dropping 9 and 9f, is for review. The freeze
7071 doesn't hold for this work, by the owner's word.
7172
7273 **For:** a language a person alone can implement again, from the
@@ -77,12 +78,15 @@
7778 **Done when:** the report alone is enough to rebuild raz, with a
7879 conformance suite to say when a rebuild is done; the report has a
7980 system integer and optional reals, and leaves separate compilation to
80-the implementation; the compiler goes through an IR, which is what its
81-object files hold, and keeps its values in registers as Wirth's
82-compilers do; `make bench` still passes; a new code budget is written down; `v0.6.0` is tagged.
81+the implementation; the parser reaches the machine only through
82+`gen`, the boundary a port writes again, and the back end keeps its
83+values in registers as Wirth's compilers do; `make bench` still passes; a new code budget is written down; `v0.6.0` is tagged.
8384
8485 **Kept out:** a second back end (aarch64 is the milestone after, to
85-measure a port); an optimiser; the wishes below.
86+measure a port); an optimiser; separate compilation: object files, an
87+IR written down, and a tool to show them (the report leaves it to the
88+implementation, and a whole program compiles in well under a second);
89+the wishes below.
8690
8791 - [x] 1. The report as it stands, made complete: the whole grammar;
8892 the readings in CLAUDE.md moved into the report; what every
@@ -107,22 +111,31 @@
107111 - [x] 3. The IR, in its own document, `doc/ir.md`, for review: proposed, a stack
108112 code close to what the parser asks of `gen` today, with types
109113 and places, so that a back end turns its stack into registers and
110- an interpreter of it stays small.
114+ an interpreter of it stays small. Set aside by the owner: the
115+ compiler compiles the whole program at once, so nothing needs
116+ the IR written down; `doc/ir.md` is removed, and git keeps it.
111117 - [x] 4. The tests split: a conformance suite any implementation
112118 passes, apart from this compiler's own messages.
113119 - [ ] 5. The system integer.
114120 - [ ] 6. Optional reals: a target says whether it has them.
115-- [ ] 7. The parser emits the IR and the x86-64 back end reads it;
116- every test gives the same output as before.
117-- [ ] 8. Wirth's register stack in the back end: a value is held in
118- one of a stack of registers, not pushed.
119-- [ ] 9. Object files: written for each module, read in place of its
120- source when they are current; instances kept once each by the
121- linker.
121+- [ ] 7. The parser reaches the machine only through `gen`: calls of
122+ a stack machine, with the kind of each value (a word, a 64-bit
123+ integer, an `F32`, an `F64`) and the place in the source, and no
124+ x86-64 in the parser; a port is a new `gen`. Every test gives
125+ the same output as before.
126+- [ ] 8. Wirth's register stack in the x86-64 `gen`: the value at
127+ depth `d` is held in register `d`, not pushed. The registers in
128+ use are saved around a call, and spilled to the frame when an
129+ expression is deeper than the registers; the stack is empty when
130+ deferred statements run, a `return`'s results kept in the frame
131+ meanwhile; `rax`, `rdx` and `rcx`, which division and shifts
132+ need, aren't in the stack. `make bench` still passes.
122133 - [ ] 9a. Failures caught by the code where the machine can't: a
123134 `nil` test before a dereference or a call through a procedure
124135 value, a stack limit at each procedure's start, a zero test
125- before a division; chosen per target.
136+ before a division; chosen per target. A `nil` test is left out
137+ only where the offset falls in the unmapped page at address 0.
138+ On x86-64 that means a field past 4,095 bytes still gets one.
126139 - [ ] 9b. Assembly procedures in place of byte procedures: the
127140 target's own syntax (appendix A), for the instructions the
128141 encoder knows and a few of the system's, bytes for the rest.
@@ -135,11 +148,6 @@
135148 - [ ] 9e. A profiler in `Rt`: a program run with `RAZ_PROFILE` set
136149 samples where it is, and at its end says which procedures and
137150 lines took the time.
138-- [ ] 9f. `tools/Obj.raz`: an object file shown for a person to read,
139- its interface as raz, its code in the text form under each line
140- of source, its data as bytes and characters; written from
141- `doc/ir.md` alone, apart from the compiler, as the proof that the
142- form can be read by another program.
143151 - [ ] 10. `doc/compiler.md`, README, the budget, the report's version
144152 and the compiler's; tag `v0.6.0`.
145153

doc/ir.md deleted +0 -397

@@ -1,397 +0,0 @@
1-# The intermediate form of raz
2-
3-A draft, for review before any code (PLAN.md, v0.6.0, step 3).
4-
5-This is the code an object file holds (report, section 10): what the
6-parser makes of a module's procedures, before any machine is chosen.
7-A back end turns it into one target's machine code; an interpreter
8-could run it as it is. It is this compiler's choice, not the
9-language's (report, appendix A): another implementation may keep
10-object files of its own.
11-
12-It is a stack code, as Wirth's P-code was. The parser already
13-compiles to a stack machine through `rax` (`doc/compiler.md`); the
14-intermediate form is that stack machine written down, with the
15-machine taken out. Each call the parser makes into `gen.pas` today
16-becomes one or a few instructions here.
17-
18-## What it is for
19-
20-- **One parser, many machines.** The parser emits this, and knows
21- nothing of x86-64. A port writes a back end: instruction selection
22- and encoding, a few thousand lines at most, and leaves the rest of
23- the compiler alone.
24-- **Object files.** A module is compiled once into its interface and
25- this code, and linked into any program later (report, section 10).
26-- **Registers without an allocator.** A back end keeps the stack in a
27- stack of registers, as Wirth's compilers do: the value at depth `d`
28- lives in register `R[d]`. Nothing has to be allocated, and no
29- instruction moves a value the program didn't ask to move.
30-- **An interpreter as a fallback.** Every instruction is simple
31- enough that an interpreter of the whole form is a few hundred lines,
32- in raz or in anything else: a way to run raz on a machine before it
33- has a back end.
34-
35-## What it leaves out
36-
37-No SSA, no basic blocks, no optimisation; no types beyond what a
38-machine needs (the parser has checked everything else); no syntax
39-tree. Sizes and layouts are already worked out: an object file is
40-made for one target, its `INT` and its C layout. The same form serves
41-every target, but one object file serves one.
42-
43-## Values
44-
45-The stack holds values of four kinds:
46-
47-| Kind | What |
48-|---|---|
49-| `w` | a word: an integer as big as an address, `INT`, `UINT`, a pointer, a procedure, an `ERROR`, a `BOOL` or an enumeration |
50-| `l` | a 64-bit integer, `I64` and `U64`; the same as `w` where an address is 8 bytes |
51-| `f` | an `F32` |
52-| `d` | an `F64` |
53-
54-A narrower integer (`I8` to `I32`, `U8` to `U32`) is a `w` on the
55-stack, extended by its sign or by zeros as its type says, and cut back
56-when it is stored. Records, arrays, slices and variants are never on
57-the stack: their addresses are.
58-
59-An instruction names the kinds it works on after a dot: `add.w`,
60-`add.l`, `add.d`. Signed and unsigned are told apart only where they
61-differ: `div.s` and `div.u`, `lt.s` and `lt.u`, `shr.s` and `shr.u`.
62-
63-## A procedure
64-
65-```
66-proc Name.f params: w w results: w frame: 32 align 16
67- ...
68-end
69-```
70-
71-`params` and `results` are the kinds of the procedure's scalar
72-parameters and results, in order; a record, array, slice or variant
73-parameter is a `w`, its address. `frame` is the size of the frame,
74-whose slots the parser has laid out: its parameters, its variables,
75-and the temporaries of each statement. `frame k` pushes the address
76-of the slot at offset `k`.
77-
78-On entry the parameters are already in their slots, put there by the
79-back end's prologue; the frame is zero everywhere else (report,
80-section 4). A procedure returns with `ret`, taking its results from
81-the stack, as many as `results` says. A procedure with several
82-results, or a result that isn't a scalar, has a hidden first
83-parameter, the address to put them at, and the parser writes them
84-there itself; such a procedure's `results` is empty. This is the rule
85-of today's compiler (CLAUDE.md, "several results come back through a
86-hidden pointer"), and C's rule for a struct.
87-
88-## The instructions
89-
90-Each one takes its operands off the stack and pushes its result, the
91-operand pushed last being the right one: `const.w 7; const.w 2; sub.w`
92-leaves 5. `pos` is a place in the source, as a line and a column.
93-
94-**Constants and addresses**
95-
96-| Instruction | Does |
97-|---|---|
98-| `const.w n`, `const.l n`, `const.f x`, `const.d x` | pushes the constant |
99-| `frame k` | the address of the frame slot at offset `k` |
100-| `addr sym+k` | the address of a chunk of the program (a module variable, a string, a procedure, an `ERROR`), plus `k` |
101-| `extern name` | the address of a C symbol (report, section 9) |
102-| `drop` | throws the top value away |
103-| `dup` | pushes the top value again |
104-
105-**Memory**
106-
107-| Instruction | Does |
108-|---|---|
109-| `load.t` | takes an address, pushes the value there, of type `t`: `i8`, `u8`, `i16`, `u16`, `i32`, `u32`, `i64`, `w`, `f`, `d` |
110-| `store.t` | takes an address and a value, stores the value there |
111-| `vload.t`, `vstore.t` | the same, volatile: exactly one access of exactly that size, in this order among the volatile ones (report, section 7.6) |
112-| `uload.t`, `ustore.t` | the same, at an address that may be misaligned: a field of a packed record (report, section 5.5) |
113-| `copy n a` | takes a destination and a source, copies `n` bytes, both aligned to `a` |
114-| `zero n a` | takes an address, zeroes `n` bytes aligned to `a` |
115-
116-**Arithmetic.** On `w` and `l`: `add`, `sub`, `mul`, `div.s`, `div.u`,
117-`mod.s`, `mod.u`, `and`, `or`, `xor`, `shl`, `shr.s`, `shr.u`, `neg`,
118-`not`. On `f` and `d`: `add`, `sub`, `mul`, `div`, `neg`. All of them
119-wrap, and a shift takes its count modulo the width (report, section
120-6.1). `div` and `mod` on integers carry a `pos`: dividing by zero, or
121-the smallest integer by -1, stops the program there (report, section
122-10), by the machine's trap or by a test the back end puts in on a
123-machine that doesn't trap, as RISC-V doesn't.
124-
125-**Comparisons** push a `w`, 0 or 1: `eq`, `ne`, and `lt`, `le`, `gt`,
126-`ge` with `.s` or `.u` on integers, plain on reals, where they are
127-IEEE's (report, section 6.3).
128-
129-**Conversions**
130-
131-| Instruction | Does |
132-|---|---|
133-| `cut.t` | a `w` or `l` cut to the integer type `t` and extended back by its sign or zeros: what `I8(x)` does, and what keeps `U8` arithmetic wrapping at 8 bits |
134-| `wide.s`, `wide.u` | a `w` to an `l`, by sign or by zeros; nothing where they are the same |
135-| `narrow` | an `l` to a `w`, cut |
136-| `itof.s.k`, `itof.u.k` | an integer of kind `w` or `l` to the real kind `k` |
137-| `ftoi.k` | a real to an `l`, toward zero; a NaN or a value out of range gives the smallest `I64` (report, section 6.7) |
138-| `ftod`, `dtof` | `F32` to `F64` and back |
139-
140-**Checks** carry a `pos`; a failed one calls `Rt.fail` with what
141-failed (report, section 6.5).
142-
143-| Instruction | Does |
144-|---|---|
145-| `check.index pos` | takes an index and a length; stops the program unless `0 <= index < length` by value; leaves the index, as a `w` |
146-| `check.slice pos` | takes `lo`, `hi` and a length; stops the program unless `0 <= lo <= hi <= length`; leaves `lo` and `hi` |
147-| `check.order pos` | takes `lo` and `hi`; stops the program unless `lo <= hi` (a slice of a pointer) |
148-| `check.enum n name pos` | takes a `w`; stops the program unless it is below `n`, the number of names of the enumeration `name` |
149-| `check.nil pos` | looks at the top value, a pointer or a procedure, without taking it; stops the program if it is `nil` |
150-
151-A module that says `check off;` has no `check.index`, `check.slice`
152-or `check.order` in its code. `check.nil` comes before every
153-dereference and every call through a procedure value; a back end for
154-a machine that traps on a low address may make it nothing.
155-
156-**Control.** Labels are numbers, one set to each procedure.
157-
158-| Instruction | Does |
159-|---|---|
160-| `label n` | marks a place |
161-| `jump n` | goes there |
162-| `jz n`, `jnz n` | takes a `w`, and goes there if it is zero, or not |
163-| `line pos` | the statement that starts here begins at `pos`; it goes into the table of lines, for the debugger and the trap report |
164-| `gosub n` | runs the code at label `n`, in this same frame, until its `back`, then goes on after the `gosub` |
165-| `back` | returns from a `gosub` |
166-
167-At a `label`, every way in brings the same values on the stack, of the
168-same kinds. Most labels are reached with the stack empty; the one at
169-the end of `a and b` used as a value is reached with one `w`. A back
170-end keeping the stack in registers therefore finds each value in the
171-same register whichever way it came.
172-
173-**Calls**
174-
175-| Instruction | Does |
176-|---|---|
177-| `call sym` | calls a procedure of the program; its arguments are on the stack, the first pushed first, and its results replace them |
178-| `calli sig` | the same, through a procedure value pushed after the arguments; `sig` gives the kinds of its parameters and results |
179-| `callc name sig` | calls a C procedure (report, section 9), by C's convention, `...` and all |
180-| `ret` | returns, with the procedure's results on the stack |
181-
182-A record, array, slice or variant passed by value goes as the address
183-of a copy the caller makes (`copy`, then the temporary's address), as
184-today. Arguments are worked out left to right, onto the stack, so
185-working out one can't disturb another: the back end, not the parser,
186-moves them into the target's registers.
187-
188-**The rest**
189-
190-| Instruction | Does |
191-|---|---|
192-| `cas.t`, `xchg.t`, `xadd.t` | the atomic built-ins (report, section 6.8), on an address and one or two values |
193-| `fence` | a full barrier |
194-| `syscall n` | a Linux system call with `n` arguments; only for Linux targets |
195-| `argc`, `argv` | the command line (report, section 6.8) |
196-
197-## Code procedures and assembly
198-
199-A procedure written in machine code, or in assembly (PLAN.md, step
200-9b), isn't in this form: its bytes are the target's. The object file
201-keeps them as they are, with the name of the target they are for, and
202-a back end for another target rejects them. This is why a module that
203-holds machine code comes in one file per target (report, section 3).
204-
205-## Statements, as they come out
206-
207-What the parser emits for a few statements, the frame offsets made
208-up:
209-
210-```
211-x := a[i] + 1; (* a: [10]I64, i: I64, x: I64; all locals *)
212-
213- line 12:5
214- frame 0 (* x's slot: the destination first, section 7.5 *)
215- frame 16 (* a *)
216- frame 8 (* i *)
217- load.i64
218- const.w 10
219- check.index 12:14
220- const.w 8
221- mul.w
222- add.w (* the element's address *)
223- load.i64
224- const.l 1
225- add.l
226- store.i64
227-```
228-
229-```
230-if p # nil then p^.n := 0; end; (* p: ^Node, n at offset 8 *)
231-
232- line 13:5
233- frame 24
234- load.w
235- const.w 0
236- ne.w
237- jz 1
238- line 13:21
239- frame 24
240- load.w
241- check.nil 13:21
242- const.w 8
243- add.w
244- const.l 0
245- store.i64
246- label 1
247-```
248-
249-A back end keeping the stack in four registers turns the second into,
250-on x86-64:
251-
252-```
253-mov rax, [rbp-24]
254-test rax, rax
255-je L1
256-mov rax, [rbp-24] ; check.nil: nothing, the machine traps on nil
257-mov qword [rax+8], 0
258-L1:
259-```
260-
261-where today's compiler pushes and pops. A back end may match small
262-patterns such as `frame k; load.w` (one instruction on most machines)
263-or a comparison followed by `jz` (a compare and a branch), and that is
264-all the optimising it does.
265-
266-## Object files
267-
268-An object file is a module's, for one target. It holds, in this order:
269-
270-1. **A header**: the module's name, the target, the version of this
271- form, and the fingerprint of the module's interface.
272-2. **Its imports**: each module it read the interface of, with that
273- interface's fingerprint when it was compiled, and each generic
274- module it made an instance of, with the fingerprint of its whole
275- source. The object file is current while each of those is still the
276- one there, and its own source hasn't changed; when it isn't, the
277- compiler compiles the module again from its source.
278-3. **Its interface**: what it exports, as an importer needs it: its
279- constants with their values, its types with their layouts, its
280- variables, and its procedures' signatures. An importer reads this,
281- never the source.
282-4. **Its chunks**: each procedure in this form; each module variable
283- as a size and an alignment; each string and `ERROR` as its bytes.
284- A chunk is named, so that the linker finds it from other object
285- files.
286-5. **Its instances**: each instance of a generic module it made, from
287- the generic module's source, its chunks named as below. The linker
288- keeps one of each name.
289-6. **Its places**: the lines of its statements and the places of its
290- checks, for messages, the trap report and the debugger.
291-
292-Numbers are written as LEB128, the variable-length encoding of
293-WebAssembly and DWARF; names as a length and their bytes. A program
294-is linked from object files alone: the linker reads them, marks what
295-the entry point reaches (tree shaking, as today), hands each live
296-procedure to the back end, and writes the executable.
297-
298-## Naming an instance
299-
300-The report makes two instances with the same arguments one instance
301-(section 3), and leaves to the compiler how it finds them so. An
302-instance is named by the generic module, the fingerprint of its whole
303-source, and its arguments, so that two arguments that are the same
304-type (report, section 5.10) give one name:
305-
306-- A type is named as it is built, down to its records, variants and
307- enumerations. An alias, or a named pointer, array, slice, set or
308- procedure type, is named as what it stands for; a procedure type
309- without its parameters' names.
310-- A record, variant or enumeration is named by where it is declared,
311- with the fingerprint of its declaration: its module, its procedure if
312- it is local to one, or the instance's name if an instance declares
313- it, and its name; an anonymous one by its line and column in place
314- of a name.
315-- A procedure is named by its module and its name.
316-
317-Two object files that hold an instance of the same name hold the same
318-code.
319-
320-## The interpreter
321-
322-An interpreter keeps a stack of values, a frame per call, and memory
323-laid out as the object file's target says. Each instruction is a few
324-lines of it; `callc` and `syscall` are the only ones that need the
325-machine under it. It is not part of v0.6.0. It is here so that the
326-form stays small enough to have one: every instruction added must be
327-easy to interpret, or it doesn't belong.
328-
329-## Reading an object file
330-
331-`tools/Obj.raz` shows what an object file holds, for a person to
332-read: `Obj List.o` prints it, part by part, as the file has them. It
333-reads the binary form by itself, from this document, not through the
334-compiler; so it is also the proof that the form is described well
335-enough to be read by a program written apart.
336-
337-It prints the declarations in raz, the code in the text form above,
338-each statement under its line of source when the source is there,
339-and data as bytes beside their characters. For example:
340-
341-```
342-List.o linux-x86_64, form 1
343-interface 4F1A 09C2 77E0 3B15
344-
345-imports
346- Mem interface 1D02 88A1 C3F0 0E47 current
347- Rt interface 6B3E 0412 9D5C A781 changed: compile List again
348-
349-exports
350- type Node* = record next*: ^Node; val*: I64; end; size 16, align 8
351- type List* = record head*: ^Node; len*: INT; ... end; size 24, align 8
352- proc push*(l: ^List; v: I64): BOOL;
353-
354-proc List.push params: w l results: w frame 32 align 16
355- no address taken: l, v, n
356-
357- 12 | NEW(n, l^.a);
358- frame 16
359- ...
360- 13 | if n = nil then return false; end;
361- frame 16
362- load.w
363- const.w 0
364- eq.w
365- jz 1
366- const.w 0
367- ret
368- label 1
369- ...
370-
371-data List.Full rodata, 16 bytes, align 1
372- 0000 6E 6F 20 72 6F 6F 6D 20 66 6F 72 20 69 74 00 00 no room for it..
373-
374-instances none
375-
376-places 14 lines, 3 checks
377-```
378-
379-Each part can be asked for alone (`Obj -code List.o`, `Obj
380--interface List.o`), and `-S` gives the code alone, as `raz -S` does
381-for a whole program. The text form it prints is the one the back
382-end's tests compare, so that it is kept exact.
383-
384-## Settled
385-
386-These were open questions; the owner chose as proposed.
387-
388-1. **Locals in registers** are left out of v0.6.0. A procedure's
389- header has room for the variables whose address is never taken,
390- which the parser knows at the procedure's end, so that a later back
391- end can keep them in registers without the form changing; the dump
392- above shows it.
393-2. **Binary and text.** The object file is binary. The text form
394- is for people and for tests: `raz -S`, and `tools/Obj.raz`, which
395- prints a whole object file.
396-3. **Deferred statements** are compiled once, end in `back`, and are
397- run with `gosub` at each way out of their list.

doc/raz.md (history) modified +2 -2

@@ -1701,8 +1701,8 @@
17011701 - **Separate compilation.** Whether modules are compiled on their own,
17021702 and if so into what: the object file, its format, the form of the
17031703 code in it, when one is out of date, and how an instance is named so
1704- that the program keeps one (section 10). This compiler's are in
1705- `doc/ir.md`.
1704+ that the program keeps one (section 10). This compiler compiles
1705+ every module of a program together, and writes no object files.
17061706 - **Atomics.** How `CAS`, `XCHG` and `XADD` are made sequentially
17071707 consistent on a machine without atomic instructions (section 6.8):
17081708 on one with a single processor, by holding off interrupts while