raz / commit d9e9f115

commit
d9e9f115dd8493b2c41bd9321abc5f4f54b32e4c
author
Luis <42945842+ldiaz94@users.noreply.github.com>
date
parent
7a137131
compiler.md: A program with its own Rt; test/own builds a -bare program against an Rt of exit and fail alone

patch · log

4 files changed, +143 -0

doc/compiler.md (history) modified +96 -0

@@ -2294,6 +2294,102 @@
22942294 puts them between those and the standard library, for both finding
22952295 modules and checking duplicate names. No subdirectories are searched.
22962296
2297+## A program with its own Rt
2298+
2299+A hosted program uses `lib/Rt.raz`. A freestanding one, a kernel's
2300+(`raz -bare`), has to bring its own, and this says what the compiler
2301+asks of it and what a program's start may count on. Where this and the
2302+code disagree, the code (`Gen.entryPoint`, `Raz.raz`) is right.
2303+
2304+**Giving a program its own `Rt`.** A program has one module of each
2305+name, and the library is the `lib/` beside the compiler (`Raz.raz` finds
2306+it through `/proc/self/exe`), so an `Rt.raz` beside the main file meets
2307+`lib/Rt.raz` and is refused. What works today is staging: copy `raz`
2308+beside a `lib/` holding the program's own modules, and build with that
2309+copy. The library is then all of what is in that `lib/`, and nothing of
2310+raz's unless the program copies or links it there (a symbolic link
2311+chooses a module). `test/own` does it: `check.sh` copies `raz` to
2312+`build/check/own/`, with `test/own/lib/Rt.raz` as its whole library,
2313+and builds `test/own/Own.raz` and `OwnFail.raz` with it, under `-bare
2314+-s`.
2315+
2316+**What `Rt` must declare.** `Raz.raz` finds each name in `Rt`'s scope
2317+(`objIn`) and stops with "Rt has no X" where one is missing:
2318+
2319+| Name | What | Needed |
2320+|---|---|---|
2321+| `proc exit*(status: INT)` | Ends the program; never returns, so the compiler puts `ud2` after its call. | always |
2322+| `proc fail*(what, a, b, len: INT; at: UINT; name: ^U8; rdi, rsi: UINT)` | A failed check; never returns. | always |
2323+| variable `places` | The compiler stores the address of the places table in it, for `Rt` to say where a check failed. | unless `-s` |
2324+| `proc calls` and variable `teller` | `teller` is given the address of `calls`, which says the calls under way. | unless `-s` |
2325+| `proc place*` and variable `placer` | `placer` is given the address of `place`, which says where an address in the code comes from. | unless `-s` |
2326+| `proc startProfile*` and variable `profiler` | `profiler` is given `startProfile`, which `Rt`'s body calls when `RAZ_PROFILE` is set. | unless `-s`, `-noprofile` or `-bare` |
2327+| variable `cExit: proc(status: I32)` | Given C's `exit`. | only in a program that uses C, which `-bare` refuses |
2328+
2329+Each variable is 8 bytes, for an address stored with `lea` and a store
2330+at the entry. `-s` leaves out the places and what needs them, so a
2331+failure is told by its address, and a program built with it needs
2332+`Rt` to declare only `exit` and `fail` (this is what `test/own` does).
2333+Without `-s` the compiler links all of the `unless -s` rows, even
2334+under `-bare`, and a missing name stops it ("Rt has no places"): a
2335+kernel that wants its symbols for a debugger and no places has no way
2336+yet.
2337+
2338+**`fail`'s kinds.** The compiler's code calls `Rt.fail` through a stub,
2339+one for each kind it uses, that makes the call from where the check
2340+was (the parser gives a `fail` declared in a module called `Rt` the
2341+chunk `Gen.rtFail`; the stubs are `Rt.check0` to `Rt.check7`). `what`
2342+says why. These are the numbers `lib/Rt.raz` gives; an `Rt` that wants
2343+names for them writes constants:
2344+
2345+ const
2346+ Index = 0; (* index a, of something of len elements *)
2347+ Slice = 1; (* slice a..b, of something of len elements *)
2348+ Enum = 2; (* a is no position of the enumeration called name *)
2349+ BackwardSlice = 3; (* slice a..b of a pointer, which runs backwards *)
2350+ NilPointer = 4; (* a nil pointer, reached more than NilPage bytes in *)
2351+ DivisionFault = 5; (* not raised by the code: Rt's signal handler tells it *)
2352+ StackOverflow = 6; (* nor this *)
2353+ View = 7; (* a view at index a runs past len *)
2354+
2355+x86-64 code raises 0 to 4 and 7. Kinds 5 and 6 are what the machine
2356+catches itself, which `lib/Rt.raz`'s signal handlers tell with the same
2357+procedure; a kernel's fault handlers may too. The arguments `what, a,
2358+b, len, at, name` come in `rdi, rsi, rdx, rcx, r8, r9`, as System V
2359+gives the first six integers; `rdi` and `rsi` as the failing code had
2360+them (a parameter may live there; `Rt` shows them in the trap report)
2361+go on the stack, the seventh and eighth. `at` is an address inside the
2362+call, one byte before the return address, so that `Rt` finds the
2363+failing line. `name` is a zero-ended string, the enumeration's, for
2364+`Enum`, else unset. `rsp` is a multiple of 16 at the call. The stub puts
2365+`ud2` after it, so `fail` must end the program or halt the machine.
2366+That is the whole panic hook: a kernel's `fail` prints and halts.
2367+
2368+**What a program's start may count on.** The entry point, `_start`,
2369+does this and no more:
2370+
2371+- Hosted (not `-bare`, no C): reads `argc` from `[rsp]` and `argv` from
2372+ `rsp + 8` into the program's `argc` and `argv`, which `ARGC()` and
2373+ `SYSTEM.ARGV()` read. It aligns nothing: Linux enters a program
2374+ with `rsp` a multiple of 16, the compiler's calls count on it, and
2375+ `rbp` is left as it is (Linux gives 0).
2376+- Under `-bare`: reads nothing from the stack and aligns nothing, so
2377+ `ARGC()` is 0. The loader or boot stub enters it with `rsp` a
2378+ multiple of 16, the bss zeroed (the compiler's variables count on
2379+ it), the direction flag clear, and `rbp` 0, where a trace of frames
2380+ ends.
2381+- Both then store into `Rt`'s variables the addresses they are to hold
2382+ (the table above); call the body of every used module in the order
2383+ the modules were finished (a module after those it imports, `Rt`
2384+ early), except `Rt`'s own under `-bare`, whose body sets the signal
2385+ handlers by system calls the bare machine hasn't; then `main`, if the
2386+ program has one.
2387+- `main` may return. `Rt.exit` is then called with its result, or 0 for
2388+ `proc main*` or when there is no `main`.
2389+
2390+Under `-bare`, what `Rt`'s body does (handlers, tables, a first stack)
2391+a kernel does in a module body of its own, which runs before `main`.
2392+
22972393 ## This compiler's choices
22982394
22992395 What the report's appendix A leaves to an implementation, and what

test/check.sh (history) modified +15 -0

@@ -153,6 +153,21 @@
153153 && cmp -s $out/Index.out $out/IndexBare.out; then pass=$((pass + 1))
154154 else echo "FAIL raz -bare: status $status"; cat $out/Bare.out $out/Bare.err $out/IndexBare.out; fail=$((fail + 1)); fi
155155
156+# a program with its own Rt (doc/compiler.md): the compiler copied
157+# beside a lib/ of nothing but test/own/lib/Rt.raz, whose exit and
158+# fail make their system calls themselves; Own ends with main's 3 after
159+# saying hello, OwnFail with 2 after Rt.fail's kind 0, an index
160+rm -rf $out/own; mkdir -p $out/own
161+cp $raz $out/own/raz
162+cp -r test/own/lib $out/own/lib
163+$out/own/raz -Werror -bare -s -o $out/own/Own test/own/Own.raz
164+status=$( { ( exec $out/own/Own ) > $out/own/Own.out 2>&1; echo $?; } 2>/dev/null )
165+$out/own/raz -Werror -bare -s -o $out/own/OwnFail test/own/OwnFail.raz
166+statusFail=$( { ( exec $out/own/OwnFail ) > $out/own/OwnFail.out 2>&1; echo $?; } 2>/dev/null )
167+if [ $status = 3 ] && [ $statusFail = 2 ] && [ "$(cat $out/own/Own.out)" = hello ] \
168+ && [ "$(cat $out/own/OwnFail.out)" = 'check failed: 0' ]; then pass=$((pass + 1))
169+else echo "FAIL own Rt: status $status, $statusFail"; cat $out/own/Own.out $out/own/OwnFail.out; fail=$((fail + 1)); fi
170+
156171 # raz -S: test/list/X.raz lists as X.expect
157172 for f in test/list/*.raz; do
158173 want=test/list/$(basename $f .raz).expect

test/own/Own.raz (history) added +15 -0

@@ -0,0 +1,15 @@
1+module Own;
2+
3+(* A program for the bare machine that brings its own Rt, test/own/lib's,
4+ of exit and fail alone: it says hello by that Rt's system call, then
5+ ends with main's result, 3. *)
6+
7+import Rt;
8+
9+proc main*: INT;
10+begin
11+ Rt.say("hello\n");
12+ return 3;
13+end;
14+
15+end Own.

test/own/OwnFail.raz (history) added +17 -0

@@ -0,0 +1,17 @@
1+module OwnFail;
2+
3+(* As Own, but a check fails, an index out of range, which reaches the
4+ program's own Rt.fail with kind 0: it says so and ends with status 2. *)
5+import Rt;
6+
7+var a: [4]INT;
8+
9+proc main*;
10+var i: INT;
11+begin
12+ i := 4;
13+ a[i] := 1;
14+ Rt.say("not reached\n");
15+end;
16+
17+end OwnFail.