raz / sbcl / README.md

141 lines · 7.0 KiB · raw · history · last change 498caf72

raz in SBCL #

An example, kept as a reference: what an implementation of the report looks like when it is written from the report alone. The top-level build doesn't make or check it; src/ is raz's compiler.

A second compiler for raz, written in Common Lisp for SBCL from the report alone: doc/raz.md, the language, and doc/raz-lib.md, the library. It shares no code with the compiler in src/, and its library, lib/ here, is written afresh from volume 2. It follows report 2026-10.23 and passes the conformance suite, conform/: 301 of 301.

make            # ./raz, from src/*.lisp (needs sbcl)
make check      # conform/, run from the directory above
./raz [-I dir]... [-o exe] [-S] file.raz

-S also writes the Lisp it made to exe.lisp. With RAZ_DEBUG set in the environment, a mistake of the compiler's own comes with a backtrace.

How it works #

The compiler reads each module once, front to back, checking it and turning it into Lisp as it goes. Each procedure becomes a DEFUN. At the end the compiler compiles them with SBCL into its own image and saves the image, program and all, as the executable, which starts at START in src/rt.lisp.

FileWhat
src/package.lispthe packages: RAZ, and RAZP for the program's procedures
src/scan.lispthe scanner: UTF-8 checked, then tokens
src/types.lisptypes, their layout, and sameness (5.10)
src/rt.lispthe run time the compiled code calls
src/comp.lispwhat the rest share: modules, names, items, frames, the data block
src/expr.lispexpressions, conversions, built-ins and calls
src/stmt.lispstatements
src/decl.lispdeclarations, procedures, modules and generic instances
src/driver.lispthe command line, and the executable

Memory. Every raz variable lives at an address, as ADR needs. The module variables, string literals, constant arrays and records, and the errors' messages lie in one block, the data, mapped at 200000000000H, so that the compiler knows each of their addresses. A procedure's parameters, variables and temporaries lie in its frame, on a raz stack of 256 MB that each thread maps for itself; a frame is zeroed as the procedure starts.

Values. In Lisp an integer, a pointer, an ERROR or a set is an integer, kept within its type's range; a BOOL is T or NIL; a real a DOUBLE-FLOAT or SINGLE-FLOAT. An array, a record, a variant or a slice is handled by its address. An ERROR is the address of a slice of its message, so MSG reads it there. A procedure value is an index into a table of the program's procedures; one given to C becomes a C function made by SBCL, so C can call it back, from its own threads too.

Results. A procedure returns its results as Lisp's multiple values; for each record, array, variant or slice among them the caller passes the address of a place to put it.

Deferred statements are copied, by the compiler, to each place their statement list can be left: its end, and each return, try and break that leaves it. Nothing about them is kept as the program runs, and a stop, Rt.exit or Sys.exit runs none of them, as 7.7 wants.

This implementation's choices (appendix A) #

The library #

lib/ has every module of volume 2: the core (Rt, Out, Mem, Arena, Lock, Str, Buf, and Sys) and the rest (Heap, Fmt, List, Map, Sort, Math, Files, Opt, Thread, C, Bytes), in raz, over SYSCALL and a few code procedures.

Beyond conform/ #

It compiles the raz compiler, src/ (with -I build/src for its Version), in about 4 seconds. That compiler, run on SBCL, passes conform/, and makes executables byte for byte the same as the native one's, its own included.

Of the reference compiler's own tests, test/run and examples/, all pass whose expected output this target can give: the rest are x86 code procedures (Asm, Atomic, Edge, Results, Simd, Single, Cpu) and a module only for linux-x86_64 (Target). Of test/err, it rejects every program the report makes an error; the others are that compiler's own limits, its warnings under -Werror and its assembler.

source
1# raz in SBCL
2
3An example, kept as a reference: what an implementation of the report
4looks like when it is written from the report alone. The top-level
5build doesn't make or check it; `src/` is raz's compiler.
6
7A second compiler for raz, written in Common Lisp for SBCL from the
8report alone: `doc/raz.md`, the language, and `doc/raz-lib.md`, the
9library. It shares no code with the compiler in `src/`, and its library,
10`lib/` here, is written afresh from volume 2. It follows report
112026-10.23 and passes the conformance suite, `conform/`: 301 of 301.
12
13 make # ./raz, from src/*.lisp (needs sbcl)
14 make check # conform/, run from the directory above
15 ./raz [-I dir]... [-o exe] [-S] file.raz
16
17`-S` also writes the Lisp it made to `exe.lisp`. With `RAZ_DEBUG` set
18in the environment, a mistake of the compiler's own comes with a
19backtrace.
20
21## How it works
22
23The compiler reads each module once, front to back, checking it and
24turning it into Lisp as it goes. Each procedure becomes a `DEFUN`. At
25the end the compiler compiles them with SBCL into its own image and
26saves the image, program and all, as the executable, which starts at
27`START` in `src/rt.lisp`.
28
29| File | What |
30|---|---|
31| `src/package.lisp` | the packages: `RAZ`, and `RAZP` for the program's procedures |
32| `src/scan.lisp` | the scanner: UTF-8 checked, then tokens |
33| `src/types.lisp` | types, their layout, and sameness (5.10) |
34| `src/rt.lisp` | the run time the compiled code calls |
35| `src/comp.lisp` | what the rest share: modules, names, items, frames, the data block |
36| `src/expr.lisp` | expressions, conversions, built-ins and calls |
37| `src/stmt.lisp` | statements |
38| `src/decl.lisp` | declarations, procedures, modules and generic instances |
39| `src/driver.lisp` | the command line, and the executable |
40
41**Memory.** Every raz variable lives at an address, as `ADR` needs.
42The module variables, string literals, constant arrays and records,
43and the errors' messages lie in one block, the data, mapped at
44`200000000000H`, so that the compiler knows each of their addresses.
45A procedure's parameters, variables and temporaries lie in its frame,
46on a raz stack of 256 MB that each thread maps for itself; a frame is
47zeroed as the procedure starts.
48
49**Values.** In Lisp an integer, a pointer, an `ERROR` or a set is an
50integer, kept within its type's range; a `BOOL` is `T` or `NIL`; a
51real a `DOUBLE-FLOAT` or `SINGLE-FLOAT`. An array, a record, a variant
52or a slice is handled by its address. An `ERROR` is the address of a
53slice of its message, so `MSG` reads it there. A procedure value is an
54index into a table of the program's procedures; one given to C becomes
55a C function made by SBCL, so C can call it back, from its own threads
56too.
57
58**Results.** A procedure returns its results as Lisp's multiple values;
59for each record, array, variant or slice among them the caller passes
60the address of a place to put it.
61
62**Deferred statements** are copied, by the compiler, to each place
63their statement list can be left: its end, and each `return`, `try`
64and `break` that leaves it. Nothing about them is kept as the program
65runs, and a stop, `Rt.exit` or `Sys.exit` runs none of them, as 7.7
66wants.
67
68## This implementation's choices (appendix A)
69
70- **The target** is `linux-sbcl`: Linux, on the machine SBCL runs on.
71 A module's files are looked for as `Name.linux-sbcl.raz`,
72 `Name.linux.raz`, `Name.sbcl.raz` and `Name.raz`. An address, and
73 so `INT`, is 8 bytes; reals are there; bytes are little-endian.
74- **Separate compilation**: none. Every module of a program is
75 compiled together, and no object file is written.
76- **Atomics**: `CAS` is the machine's, through SBCL; `XCHG` and `XADD`
77 loop on it.
78- **The calling convention** is SBCL's for raz procedures, and C's
79 (System V) where raz meets C, for `extern` procedures and callbacks.
80- **Code procedures.** The target's machine is SBCL, so its assembly
81 language is Lisp: each instruction, up to its `;`, is a Lisp form,
82 read in the package `RAZ`, which sees the parameters by their names
83 in capitals, and the value of the last is the result. A code
84 procedure returns no record, array or slice. The library uses them
85 for the few things only the run time can do (`Rt`'s end, `Math`,
86 threads, signals). x86 code procedures aren't this target's.
87- **`SYSCALL`** goes through the C library's `syscall()`, with
88 x86-64's numbers. A program that makes the system calls SBCL itself
89 relies on (signals' handlers, `clone`) is on its own.
90- **`Out`'s buffer**, `Sys.outSize`, is 4 KB.
91- **The executable** is an SBCL image, about 36 MB, which starts in a
92 few milliseconds. It carries the names of the procedures, which a
93 stop and `Rt.trace` print, but no line numbers for a debugger.
94- **Failures.** A failed check, a division by zero and a `nil`
95 procedure are checked by the compiled code; a `nil` pointer or a bad
96 address is caught by SBCL; a stack overflow by either: past the raz
97 stack, or past SBCL's own (256 MB). Each prints where it was, as far
98 as it can tell, and the calls under way, without their variables,
99 then runs `Rt.atExit`'s procedures and ends with status 2. SBCL may
100 also say on standard error that it used a stack's guard page.
101- **Messages.** One error is reported, as `file:line:col: what`, and
102 the compiler stops with status 1. It gives no warnings, and takes
103 `-Werror` and does nothing with it.
104- **Every program uses C**, in that SBCL's run time is a C program. A
105 module that says `link static` still must not be in a program with
106 an `extern` or a `link` of a library, which the compiler reports.
107
108## The library
109
110`lib/` has every module of volume 2: the core (`Rt`, `Out`, `Mem`,
111`Arena`, `Lock`, `Str`, `Buf`, and `Sys`) and the rest (`Heap`, `Fmt`,
112`List`, `Map`, `Sort`, `Math`, `Files`, `Opt`, `Thread`, `C`,
113`Bytes`), in raz, over `SYSCALL` and a few code procedures.
114
115- `Sys` makes Linux's system calls; `spawn` forks and runs the
116 program; `catch` has SBCL's run time handle the signals and write
117 each to a pipe, the fd it gives.
118- `Thread` runs each thread as one of SBCL's, on a raz stack of its
119 own; `Mutex` is a futex.
120- `Heap` keeps eight free lists, of blocks of 16 to 2048 bytes, and
121 gives a bigger block, or one aligned to more than 16 bytes, pages of
122 its own. It refuses a negative size, and an alignment that isn't a
123 power of 2 up to a page.
124- `Bytes`: volume 2 names its procedures but not their parameters.
125 Here they are `index(s, b)`, `last(s, b)` and `count(s, b)` for a
126 byte `b: U8`, and `find(s, sub)`, each an `INT`, -1 for none.
127
128## Beyond conform/
129
130It compiles the raz compiler, `src/` (with `-I build/src` for its
131`Version`), in about 4 seconds. That compiler, run on SBCL, passes
132`conform/`, and makes executables byte for byte the same as the native
133one's, its own included.
134
135Of the reference compiler's own tests, `test/run` and `examples/`, all
136pass whose expected output this target can give: the rest are x86
137code procedures (`Asm`, `Atomic`, `Edge`, `Results`, `Simd`, `Single`,
138`Cpu`) and a module only for `linux-x86_64` (`Target`). Of
139`test/err`, it rejects every program the report makes an error; the
140others are that compiler's own limits, its warnings under `-Werror`
141and its assembler.