raz / commit f3dc7707

commit
f3dc770785572f840e50aae266f5af39d54537e2
author
Luis <42945842+ldiaz94@users.noreply.github.com>
date
parent
69417b8a
compiler.md: what the compiler asks of Sys, Rt and Files, held by test/asks.sh

patch · log

3 files changed, +125 -0

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

@@ -2439,6 +2439,68 @@
24392439
24402440 The recipe for an interrupt's entry, and its test, follow.
24412441
2442+## What the compiler asks of Sys, Rt and Files
2443+
2444+What `src/` takes from `lib/`, for a host that has to supply it
2445+(`Host`, and a program that brings its own library): every qualified
2446+name, in code, of a library module. Nothing else of `lib/` is used by
2447+`src/` itself; the library modules listed import others in turn
2448+(`Heap`, `Mem`, `Out`, `Files` and the rest call `Sys` and `Rt`), and a
2449+replacement for one has to give what the others ask of it. `Rt` is
2450+also found by name for `fail`, `exit` and the places, as the previous
2451+section says, and `NEW(p, a)` needs `a` to be a `^Mem.Allocator` and
2452+calls its `alloc` field, the one library type the compiler knows by
2453+name. Besides the library, `src/` makes two system calls itself,
2454+`stat` in `Parse.identity` (a file's device and inode, for the
2455+one-module-of-each-name check) and `memfd_create` and `execve` in
2456+`Raz.raz` (`raz run`), and reads `SYSTEM.ARGV`.
2457+
2458+`test/asks.sh` keeps this list true: it fails when `src/` uses a
2459+library name that is not a row here, or a row names one that `src/`
2460+no longer uses. Each row is one name; a type is listed with its
2461+fields as the code reads them.
2462+
2463+`Sys`, the system:
2464+
2465+| Name | What for |
2466+|---|---|
2467+| `Sys.access` | `Raz.muslHere`: whether the dynamic loaders of glibc and musl are installed |
2468+| `Sys.chmod` | `Elf.writeElf`: makes the program's file executable (mode 755) |
2469+| `Sys.close` | `Scan` and `Parse`: closes a source or `RAZ-VERSION` file after reading |
2470+| `Sys.closeDir`, `Sys.openDir`, `Sys.nextEntry`, `Sys.Dir` | `Parse`: the files of a directory, to find a module's other files for the one-module-of-each-name check |
2471+| `Sys.env`, `Sys.envAt` | `Diag` (`NO_COLOR`) and `Raz.userLibraries` (`RAZ_PATH`, `HOME`; `envAt` tells unset from empty) |
2472+| `Sys.errDenied`, `Sys.errNoEntry` | `Scan`: "permission denied" and "there is no such file" in a message |
2473+| `Sys.info`, `Sys.Info` | `Scan` and `Parse`: whether a path is a file (`file`) and its length (`size`) |
2474+| `Sys.isTerminal` | `Diag`: colour only when standard error is a terminal |
2475+| `Sys.openRead`, `Sys.read` | `Scan`: reads a source file whole; `Parse`: the library's `RAZ-VERSION` |
2476+| `Sys.readLink` | `Raz`: `/proc/self/exe`, to find the library beside the compiler |
2477+| `Sys.stderr`, `Sys.write` | `Diag`: its messages |
2478+| `Sys.workDir` | `Diag`: shows a path relative to the working directory; `Elf`: the compilation directory of the line table |
2479+
2480+`Rt`, the run time, besides `fail`, `exit` and the places (above):
2481+
2482+| Name | What for |
2483+|---|---|
2484+| `Rt.exit` | `Diag`: ends the compiler, with status 1, after an error that stops it |
2485+
2486+`Files`, whole files and the command line:
2487+
2488+| Name | What for |
2489+|---|---|
2490+| `Files.arg` | `Raz`: the command line's words, and `ARGC()` for how many |
2491+| `Files.write` | `Elf.writeElf`: writes the executable |
2492+
2493+The rest of the library, which `Diag`, `Sym`, `X64` and `Parse` use for
2494+text and `Scan` for tables:
2495+
2496+| Name | What for |
2497+|---|---|
2498+| `Buf.Buf`, `Buf.init`, `Buf.add`, `Buf.str`, `Buf.bytes`, `Buf.reset`, `Buf.done` | A growing buffer: `Diag`'s message and working directory, a type's name in `Sym`, an instruction's text in `X64`, a name made in `Parse` |
2499+| `Fmt.write`, `Fmt.Arg`, `Fmt.str`, `Fmt.int`, `Fmt.char`, `Fmt.hex` | Formatting: `Diag.say`'s `{}` messages and a type's name |
2500+| `Out.str`, `Out.ln`, `Out.flush` | Standard output: the `-S` listing, `-h` and `--version` |
2501+| `Str.copy`, `Str.find`, `Str.hasPrefix`, `Str.hexVal` | A kept copy of a path (`Diag`), the one-character operators (`Scan`), a prefix of a path, a file name or a mnemonic (`Diag`, `Parse`, `X64`), and a hex digit (`Parse`) |
2502+| `Heap.heap`, `Mem.alloc`, `Mem.free` | The allocator that `NEW`, `Buf.init` and `Str.copy` are given, and the blocks of `Scan`'s tables and `Diag`'s lists |
2503+
24422504 ## This compiler's choices
24432505
24442506 What the report's appendix A leaves to an implementation, and what

test/asks.sh (history) added (executable) +59 -0

@@ -0,0 +1,59 @@
1+#!/bin/sh
2+# What the compiler asks of the library: every qualified name of lib/,
3+# Sys.write, Rt.exit and so on, that src/ uses in code (not in a comment,
4+# a string or a character) must be a row of the tables under "What the
5+# compiler asks of Sys, Rt and Files" in doc/compiler.md, and every row
6+# must be used. Prints the differences and exits 1 if there are any.
7+# Run from the repository's root.
8+
9+doc=doc/compiler.md
10+tmp=${TMPDIR:-/tmp}/asks.$$
11+trap 'rm -f $tmp.*' EXIT
12+
13+# src/*.raz with comments, strings and characters cut out, so that what
14+# is left is code. Comments (* ... *) don't nest and may span lines, and
15+# so may a `...` string; "..." and '...' end at the line's end at most.
16+code() {
17+ awk '
18+ BEGIN { state = "code" }
19+ {
20+ n = length($0); out = ""; i = 1
21+ while (i <= n) {
22+ c = substr($0, i, 1); d = substr($0, i, 2)
23+ if (state == "comment") {
24+ if (d == "*)") { state = "code"; i += 2 } else i++
25+ } else if (state == "raw") {
26+ if (c == "`") state = "code"
27+ i++
28+ } else if (state == "dq" || state == "sq") {
29+ if (c == "\\") i += 2
30+ else {
31+ if ((state == "dq" && c == "\"") || (state == "sq" && c == "\047")) state = "code"
32+ i++
33+ }
34+ } else if (d == "(*") { state = "comment"; i += 2 }
35+ else if (c == "`") { state = "raw"; i++ }
36+ else if (c == "\"") { state = "dq"; i++ }
37+ else if (c == "\047") { state = "sq"; i++ }
38+ else { out = out c; i++ }
39+ }
40+ if (state == "dq" || state == "sq") state = "code"
41+ print out
42+ }' src/*.raz
43+}
44+
45+# the modules of lib/, as the names a program imports them by
46+libs=$(ls lib | sed 's/\..*//' | sort -u | tr '\n' '|' | sed 's/|$//')
47+
48+code | grep -oE "\b($libs)\.[A-Za-z_][A-Za-z0-9_]*" | sort -u > $tmp.used
49+# the names in the first cell of a row of a table in the section:
50+# "| `Sys.close`, `Sys.Dir` | what for |"
51+sed -n '/^## What the compiler asks of Sys, Rt and Files/,/^## [^W]/p' $doc |
52+ sed -n 's/^| `\([^|]*\)|.*/`\1/p' |
53+ grep -oE '`[A-Za-z]+\.[A-Za-z_][A-Za-z0-9_]*`' | tr -d '`' | sort -u > $tmp.listed
54+
55+status=0
56+if [ ! -s $tmp.listed ]; then echo "asks: $doc has no rows for the library names"; status=1; fi
57+if comm -23 $tmp.used $tmp.listed | sed "s|^|src/ uses it, $doc doesn't say: |" | grep .; then status=1; fi
58+if comm -13 $tmp.used $tmp.listed | sed "s|^|$doc lists it, src/ never uses it: |" | grep .; then status=1; fi
59+exit $status

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

@@ -168,6 +168,10 @@
168168 && [ "$(cat $out/own/OwnFail.out)" = 'check failed: 0' ]; then pass=$((pass + 1))
169169 else echo "FAIL own Rt: status $status, $statusFail"; cat $out/own/Own.out $out/own/OwnFail.out; fail=$((fail + 1)); fi
170170
171+# what src/ asks of lib/ is what doc/compiler.md lists (test/asks.sh)
172+if sh test/asks.sh > $out/asks.out 2>&1; then pass=$((pass + 1))
173+else echo "FAIL test/asks.sh:"; cat $out/asks.out; fail=$((fail + 1)); fi
174+
171175 # raz -S: test/list/X.raz lists as X.expect
172176 for f in test/list/*.raz; do
173177 want=test/list/$(basename $f .raz).expect