| | @@ -2294,6 +2294,102 @@ |
| 2294 | 2294 | puts them between those and the standard library, for both finding |
| 2295 | 2295 | modules and checking duplicate names. No subdirectories are searched. |
| 2296 | 2296 | |
| 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 | + |
| 2297 | 2393 | ## This compiler's choices |
| 2298 | 2394 | |
| 2299 | 2395 | What the report's appendix A leaves to an implementation, and what |