sovereign-intelligence
Front door to Drake Stapleton's sovereign stack.
Brought by drakestapleton
sovereign-intelligence / tests / omc / README.md
| 1 | # tests/omc: compiler words beyond the imported harness |
| 2 | |
| 3 | `harness/` is the Omega harness imported verbatim (`harness/PROVENANCE.md`), so a word this repository |
| 4 | adds to `seed/omc/omc.om` cannot be documented or tested there. Its guide notes are here, written as the |
| 5 | rows the harness guide (`harness/skills/writing-omega/SKILL.md`) gains when it takes the word. Its examples |
| 6 | are in `examples/`, in the harness format (`NAME.om`, `NAME.expect`, optional `NAME.stdout`), and |
| 7 | `build.sh` runs them on both targets: |
| 8 | |
| 9 | ```sh |
| 10 | EXAMPLES_ROOT=tests/omc sh tests/examples.sh out/omc out/omc-x86 [qemu-aarch64-static] |
| 11 | ``` |
| 12 | |
| 13 | Every rule below was observed with the compilers `build.sh` grows from `seed/` (pins in |
| 14 | `seed/PINS.tsv`), on AArch64 and on x86-64 through `target x86linux`, with the same results on both. |
| 15 | |
| 16 | ## count (OM-2) |
| 17 | |
| 18 | `count A off n v` is a `ulong`: how many of the `n` elements of `A` from `off` equal `v`. |
| 19 | |
| 20 | | | | |
| 21 | |---|---| |
| 22 | | unit | OM-2 only (the first line that is not blank or `note` is `om2`); in an OM-1 unit `count` is an ordinary name | |
| 23 | | A | the name of a `uchar` array: a local, a global, a text, or a `view` / `edit` parameter, sized or unsized (`*`) | |
| 24 | | off, n | `ulong`; a bare number takes `ulong` | |
| 25 | | v | `uchar`; a bare number takes `uchar` (0..255) | |
| 26 | | result | `ulong`, 0..n | |
| 27 | | traps | 3 when the slice is outside `A`: unless `n <= len A` and `off <= len A - n`. Nothing else | |
| 28 | | order | the four values are read left to right; then the slice is checked; then the elements are read | |
| 29 | |
| 30 | The slice check is the one `read` and `write` make (`vs_chk` in `seed/omc/omc.om`): `off + n` is never |
| 31 | formed, so it cannot wrap (`count_trap3_wrap.om`). An in-bounds slice never traps, including `n = 0` at |
| 32 | `off = len A` (`count_word.om`). The word replaces the loop |
| 33 | |
| 34 | ``` |
| 35 | c = ulong 0 |
| 36 | for i ulong n bound N |
| 37 | if eq get A add off i v |
| 38 | c = add c 1 |
| 39 | end |
| 40 | end |
| 41 | ``` |
| 42 | |
| 43 | with one machine loop, 16 bytes per pass (AArch64: `LD1`, `CMEQ`, `SUB` in NEON lanes; x86-64: `MOVDQU`, |
| 44 | `PCMPEQB`, `PSUBB` in SSE2, which every x86-64 CPU has), summed every 255 passes, and the last 0..15 bytes |
| 45 | one at a time. The loop above costs about 37 x86-64 instructions per byte under omc-x86, because every |
| 46 | name lives in a stack slot; `count` measured at over 20 GB/s on a cached 1 MiB buffer. |
| 47 | |
| 48 | ### Rows for the harness guide |
| 49 | |
| 50 | Section 4, Words: |
| 51 | |
| 52 | | Kind | Words | |
| 53 | |---|---| |
| 54 | | arrays (OM-2) | `count A off n v` | |
| 55 | |
| 56 | Section 5, Traps: the row "array index or I/O slice out of bounds", trap 3, gains `count`'s slice |
| 57 | (`count_trap3.om`, `count_trap3_wrap.om`). |
| 58 | |
| 59 | Section 9, OM-2 units, gains: "`count A off n v`, the number of `uchar` elements of `A[off, off+n)` equal to |
| 60 | `v`, the slice checked as `read` checks it (trap 3)". |
| 61 | |
| 62 | Section 11, Compiler errors: `count` adds no error number. Each refusal is an existing code with its |
| 63 | existing meaning; code 26 is still never emitted. |
| 64 | |
| 65 | | Code | Refusal of count | Example | |
| 66 | |---|---|---| |
| 67 | | 3 | `count` in an OM-1 unit (an unknown name or function) | `count_err03_om1` | |
| 68 | | 4 | fewer than four values (reported on the next line, as every error 4) | `count_err04_short` | |
| 69 | | 7 | `A` not a `uchar` array: another element type, a scalar, a struct or layout, a `const` | `count_err07_element`, `count_err07_scalar`, `count_err07_struct` | |
| 70 | | 7 | `off` or `n` not `ulong`, `v` not `uchar` | `count_err07_offset`, `count_err07_value` | |
| 71 | | 8 | a bare number `v` outside 0..255 | `count_err08_value` | |
| 72 | | 9 | `part` as `A` (`part` is only a call argument) | `count_err09_part` | |
| 73 | | 16 | `count` as a name in an OM-2 unit, or as a statement | `count_err16_name`, `count_err16_statement` | |
| 74 | | 27 | `A` a tracked `fresh` array (it leaves the analysis, as with `read` and `write`) | `count_err27_fresh` | |
| 75 | | 30 | `count` in a `requires`, `ensures` or `invariant` clause | `count_err30_contract` | |
| 76 | |
| 77 | Section 12, Common mistakes: |
| 78 | |
| 79 | | Mistake | Write instead | Refusal | Example | |
| 80 | |---|---|---|---| |
| 81 | | `count a 0 n 10` on its own line | `k = count a 0 n 10` | error 16 | `count_err16_statement` | |
| 82 | | `count a 0 n 10` with `a` a `uint` array | a `uchar` array, or a loop | error 7 | `count_err07_element` | |
| 83 | | `count a o n c` with `c` an `int` | `uchar c` | error 7 | `count_err07_value` | |
| 84 | | a loop that checks `add off n` against `len a` itself | `count`, which checks `off <= len a - n` | wraps for huge `off` | `count_trap3_wrap` | |
| 85 | |
| 86 | Section 14, Example index: OM-2 and contracts gain `count_word`; traps gain `count_trap3`, |
| 87 | `count_trap3_wrap`; refusals gain `count_err*`. |
| 88 | |
| 89 | ### What build.sh checks |
| 90 | |
| 91 | - step 10: the 16 examples here pass on both targets; |
| 92 | - step 11 (negative control): `seed/omc/omc.om` with the one line `vs_chk e ro rn` of `fn cnt_word` |
| 93 | deleted builds a compiler whose `count` reads without checking. That compiler must fail |
| 94 | `count_trap3` and `count_trap3_wrap` (no exit 103) on every target that runs, while `count_word` still |
| 95 | passes, so the failure is the missing check and nothing else. |