diff --git a/build.sh b/build.sh index 9662790..72cd5f6 100644 --- a/build.sh +++ b/build.sh @@ -24,6 +24,9 @@ # byte for byte, and both builds must write the same si.sh # 6. tools/ledger.om, built for both targets, must print tests/ledger/expected.tsv # 7. the Omega harness examples, on both targets (tests/examples.sh) +# 8. the examples of compiler words beyond the imported harness (tests/omc), on both targets +# 9. control: omc.om with count's slice check deleted must build a compiler that fails both trap-3 +# examples of count on every target that runs, while count_word still passes # Last line: RC=0 when every step passed. set -eu R=$(CDPATH= cd -- "$(dirname -- "$0")" && pwd) @@ -176,4 +179,28 @@ done sh tests/examples.sh "$O/omc" "$O/omc-x86" ${A64:+"$A64"} || fail "harness examples" pass "harness examples" +# 8. The examples of words this repository adds to the compiler (seed/PROVENANCE.md), on both targets. +EXAMPLES_ROOT=tests/omc sh tests/examples.sh "$O/omc" "$O/omc-x86" ${A64:+"$A64"} || fail "tests/omc examples" +pass "tests/omc examples" + +# 9. Control: count without its slice check must be caught by the trap-3 examples. +awk '/^fn / { f = ($2 == "cnt_word") } f && $0 == "vs_chk e ro rn" { f = 0; next } { print }' $S/omc/omc.om > $B/omc.nochk.om +[ "$(wc -l < $B/omc.nochk.om)" -eq "$(($(wc -l < $S/omc/omc.om) - 1))" ] || fail "control: cnt_word's vs_chk line was not deleted exactly once" +a64 $O/omc < $B/omc.nochk.om > $B/omc-nochk || fail "control: the compiler without count's check does not build" +mk $B/omc-nochk +{ printf 'target x86linux\n'; cat $B/omc.nochk.om; } | a64 $O/omc > $B/omc-x86-nochk || fail "control: omc-x86 without count's check does not build" +mk $B/omc-x86-nochk +[ "$(sha $B/omc-nochk)" != "$(sha $O/omc)" ] || fail "control: deleting count's check did not change omc" +if EXAMPLES_ROOT=tests/omc sh tests/examples.sh $B/omc-nochk $B/omc-x86-nochk ${A64:+"$A64"} > $B/nochk.log; then + fail "control: the compiler without count's slice check passed tests/omc" +fi +for t in a64 x86; do + if grep -q "^NOT_RUN $t " $B/nochk.log; then note "NOT_RUN control on $t"; continue; fi + for c in count_trap3 count_trap3_wrap; do + grep -q "^FAIL $t examples/$c: exit " $B/nochk.log || fail "control: $t examples/$c did not catch the missing check" + done + grep -qx "PASS $t examples/count_word" $B/nochk.log || fail "control: $t count_word failed for another reason" +done +pass "control: count without its slice check fails count_trap3 and count_trap3_wrap; count_word still passes" + printf 'RC=0\n' diff --git a/seed/PINS.tsv b/seed/PINS.tsv index b4a2b3f..8ed9981 100644 --- a/seed/PINS.tsv +++ b/seed/PINS.tsv @@ -3,5 +3,5 @@ seed0.bin 408 aca00cf3677236809f0f223437398c6bb429e4c65f13d58762bcf96d13799474 r hex1.bin 776 920e1756aec399c7c74f5cedd1676abb91b844ea75448e14aa50dcb0bbc7ba5d seed0 from rung1/hex1.hex; then hex1 from hex1.hex asm2.bin 2464 6988abe072fac3cdef2240d5ad13a3ecd8fb576fa09112d344b9e7c2315969c4 hex1 from rung2/asm2.hex1 om4.bin 14224 bfc1cfd197348c398be19425b21704ad5ea79d28e33c0e1eff676142f47812b7 asm2 from rung2/table.s2 + rung3/table3.s2 + rung4/table4.s2 + rung4/om4.s -omc 660940 cfd96b48b0460ab5557853d6856f9c741b96ac8d0d624e06198defb4cbfa0d8a om4 from omc/omc.om (stage1), then omc from omc.om twice (stage2 = stage3 = stage1) -omc-x86 5081428 5287a5aac3e2c5d372fadd6b04a038fbe1278d4c3673650796d178b1f90dba46 omc from "target x86linux" + omc/omc.om; then omc-x86 from the same input +omc 673336 6445b4719849e8e63332d053f3478667af24b1ed66ad4ff35b3bdb0722cac113 om4 from omc/omc.om (stage1), then omc from omc.om twice (stage2 = stage3 = stage1) +omc-x86 5175010 29e8e958c92d2d2bf13c013bd955a6f931f9f9a60a218e2195305252c043d79e omc from "target x86linux" + omc/omc.om; then omc-x86 from the same input diff --git a/seed/PROVENANCE.md b/seed/PROVENANCE.md index ace7806..f6be7bb 100644 --- a/seed/PROVENANCE.md +++ b/seed/PROVENANCE.md @@ -1,6 +1,6 @@ # seed/: provenance -Every file here except `PINS.tsv`, `SHA256SUMS`, `README.md` and this file is copied byte for byte from omega-mv, the Omega compiler project: +Every file here except `PINS.tsv`, `SHA256SUMS`, `README.md` and this file is copied byte for byte from omega-mv, the Omega compiler project, except `omc/omc.om`, which this repository has since changed ([below](#differences-from-omega-mv)): | | | |---|---| @@ -16,6 +16,33 @@ git -C omega-mv checkout 6fbc6cbe4bfa4092608e2b5f06d2e8f537893992 (cd omega-mv/seed && sha256sum -c ../../sovereign-intelligence/seed/SHA256SUMS) ``` -`NOTICE` is checked from omega-mv's root, the rest from its `seed/` directory. +`NOTICE` is checked from omega-mv's root, the rest from its `seed/` directory. `omc/omc.om` reports `FAILED` there: `SHA256SUMS` describes this tree, and omega-mv's copy has sha256 `062c9649af6a1d499b911e5b2f39b5419d7b75047616fe59fbcdc25f7b7b5b73`, the file of this repository's first commit `3e3116f`. -Only the sources needed to grow the compiler are copied. Binaries are not: `build.sh` makes every one of them and checks each against `PINS.tsv`. The pins equal the binaries omega-mv commits at that commit (`seed/rung0/seed0.bin`, `seed/rung1/hex1.bin`, `seed/rung2/asm2.bin`, `seed/rung4/om4.bin`, `seed/out/omc`). `omc-x86` is new: omega-mv has never committed or pinned a native x86-64 omc. +Only the sources needed to grow the compiler are copied. Binaries are not: `build.sh` makes every one of them and checks each against `PINS.tsv`. The pins of `seed0.bin`, `hex1.bin`, `asm2.bin` and `om4.bin` equal the binaries omega-mv commits at that commit (`seed/rung0/seed0.bin`, `seed/rung1/hex1.bin`, `seed/rung2/asm2.bin`, `seed/rung4/om4.bin`). The pins of `omc` and `omc-x86` are those of the changed `omc/omc.om`; omega-mv's `seed/out/omc` is the earlier pin, below. `omc-x86` is new: omega-mv has never committed or pinned a native x86-64 omc. + +## Differences from omega-mv + +`omc/omc.om` differs from omega-mv `6fbc6cbe` by one feature: the OM-2 value word `count A off n v`, the number of `uchar` elements of `A[off, off+n)` equal to `v`, checked as `read` and `write` check their slices (trap 3). It replaces the byte-at-a-time loop in which `tools/ledger.om` spent almost all its time: omc-x86 keeps every name in a stack slot, so that loop costs 37 x86-64 instructions per byte, and its loop variable makes two store-to-load round trips per pass. Its contract, refusals and examples are in [`../tests/omc/README.md`](../tests/omc/README.md). + +| | omega-mv `6fbc6cbe` | here | +|---|---|---| +| `omc/omc.om` sha256 | `062c9649af6a1d499b911e5b2f39b5419d7b75047616fe59fbcdc25f7b7b5b73` | `fd4a6c4c4453a1df72fa4d82ef2d7ba0fbf1cf60276bcb6bf030dc720ce6039d` | +| `omc` pin | 660940 bytes, `cfd96b48b0460ab5557853d6856f9c741b96ac8d0d624e06198defb4cbfa0d8a` | 673336 bytes, `6445b4719849e8e63332d053f3478667af24b1ed66ad4ff35b3bdb0722cac113` | +| `omc-x86` pin | 5081428 bytes, `5287a5aac3e2c5d372fadd6b04a038fbe1278d4c3673650796d178b1f90dba46` | 5175010 bytes, `29e8e958c92d2d2bf13c013bd955a6f931f9f9a60a218e2195305252c043d79e` | + +The diff, 212 lines added and 2 changed (`git diff 3e3116f -- seed/omc/omc.om` shows it whole), has five parts: + +1. `kw2`, the OM-2 keyword table: `count` (packed `500069396323`) is code 179, a code no other word uses. Like every `kw2` word it is reserved only in an OM-2 unit, so no OM-1 unit, `omc.om` among them, changes meaning. The two-line note above `kw2` names it. +2. `rdval`: code 179 calls `cnt_word` (4 lines, before `get`). +3. `cnt_word` (new): reads `A` (`arrname`; `fz_arg`, so a tracked `fresh` array is error 27; a struct or layout is error 7), `off` and `n` as `ulong`, `v` as `uchar`, then calls `vs_chk`, the slice check of `read` and `write`, unchanged, so a non-`uchar` array is error 7 and an out-of-bounds slice traps 3 before any element is read. Then it calls `cnt_a64` or `xcount` and moves the count to the destination once the temporaries are given back. +4. `cnt_a64` (new): the AArch64 loop, 16 bytes per pass in NEON (`LD1`, `CMEQ`, `SUB`), the lanes summed by `UADDLV` every 255 passes, the last 0..15 bytes with `LDRB` and `CINC`. Four new pool temporaries; no name's register is written. +5. `xcount` (new): the same loop for x86-64 in SSE2 (`MOVDQU`, `PCMPEQB`, `PSUBB`, `PSADBW`), 128 fixed bytes after four setup moves; `target x86efi` takes the same code. + +Every machine word and byte was checked against llvm-mc 18 and disassembled from a compiled program on both targets. What changes and what does not: + +- **The seed chain still closes.** om4 builds the changed `omc.om` (it stays in om4's subset), and stage1 = stage2 = stage3. omc-x86 rebuilds itself byte for byte, and omc-x86 compiling `omc.om` gives exactly the AArch64 `omc` (diverse double compilation). `build.sh` checks all of it against the pins above. +- **Every unit that does not use `count` compiles to the same bytes as before.** Checked on 2026-11-07 with the old and new pins, on both targets: the 110 harness examples, `compiler/si.om`, `tools/ledger.om` as it was before it used `count`, and `omc/omc.om` itself, 226 compiles, all byte-identical. +- **Trap semantics are exact.** `count` traps 3 exactly when `n > len A` or `off > len A - n`, never otherwise; `off + n` is never formed. `build.sh` step 9 deletes the one `vs_chk` call of `cnt_word` and requires the resulting compiler to fail `count_trap3` and `count_trap3_wrap` on every target that runs, while `count_word` still passes. +- **Not run:** a `target x86efi` program using `count` compiles to a PE image holding the same 128-byte loop, but no UEFI firmware was run here. + +Measured on 2026-11-07, on a 4-vCPU x86-64 host (Intel Xeon, 2.30 GHz), page cache warm, the median of 9 interleaved runs each: the stack ledger over the 44 checkouts of `stack/STACK.tsv` (cloned from `https://rosa.radicle.network/.git` at the listed heads; 68,575 files, 954,659,395 bytes, listed as `tools/ledger-refresh.sh` lists them) took 2.05 s built by the old omc-x86 and 0.34 s built by this one, 6.1 times faster. User time fell from 1.79 s to 0.12 s; the rest is the kernel opening and reading the files. A Python 3 oracle that prints the same table byte for byte (`bytes.count`) took 0.58 s. All three outputs were byte-identical to `stack/LEDGER.tsv`. An earlier measurement the same day on another 4-vCPU x86-64 host gave 3.33 s for the old ledger and 1.25 s for Python; this host is faster, so compare ratios, not seconds. diff --git a/seed/SHA256SUMS b/seed/SHA256SUMS index 7a10ecb..3a35410 100644 --- a/seed/SHA256SUMS +++ b/seed/SHA256SUMS @@ -1,5 +1,5 @@ 367c530877825b64a9933193f571a63e2b8e26e651e0cf5397d26d8fb3e904ea NOTICE -062c9649af6a1d499b911e5b2f39b5419d7b75047616fe59fbcdc25f7b7b5b73 omc/omc.om +fd4a6c4c4453a1df72fa4d82ef2d7ba0fbf1cf60276bcb6bf030dc720ce6039d omc/omc.om 5e515adc462f68eb204a2ddb8a144a44aeab79f6ddcfb637e709bfe3047dee52 rung0/load0.sh baaf081fe6055a768485c5d25d7435681ce00b04cfa80b26c48169f78944e14d rung0/seed0.hex 2d3fecbf3c2a727f8ae486ba07d528012b2dcd5f4135394f5a268869d3aad69c rung1/hex1.hex diff --git a/seed/omc/omc.om b/seed/omc/omc.om index a1f71a7..99c1365 100644 --- a/seed/omc/omc.om +++ b/seed/omc/omc.om @@ -1374,8 +1374,8 @@ ret kw2 w end note ======== OM-2 words (spec/omegese-om2.md): reserved only in an OM-2 unit. rotr 59, rotl 60 are -note binary operators; dit 225 and scrub 226 are program-mode value words (OMEGESE 9.13.1); the rest are -note statement and declaration words of later sections. ======== +note binary operators; dit 225 and scrub 226 are program-mode value words (OMEGESE 9.13.1); count 179 is a +note value word (cnt_word); the rest are statement and declaration words of later sections. ======== fn kw2 w ulong to ulong if eq w 1920233330 ret 59 @@ -1437,6 +1437,9 @@ end if eq w 422877225843 ret 226 end +if eq w 500069396323 +ret 179 +end ret 0 end @@ -5752,6 +5755,114 @@ r = restore dst sl n a 1 vdone 1 r 9 end +note cnt_word: OM-2 `count A off n v`, a ulong: how many of the n uchar elements of A from off equal v. +note A is an array name (a local, global, text, or a view or edit parameter, sized or unsized); off and n +note are ulong, v is uchar. The four values are read left to right; then the slice is checked exactly as +note read and write check theirs (vs_chk: a non-uchar A is error 7; n <= len A and off <= len A - n, else +note trap 3), and only then is any element read, so an out-of-bounds slice traps 3 whatever v is and an +note in-bounds one never traps. A tracked fresh array (error 27, fz_arg), a struct or layout (error 7) and +note a contract clause (error 30, cp_token) are refused; an IR unit is never OM-2, so count is no word there +note (error 3), and the irmode guard (error 9, as dit) only keeps it so. Code (AArch64): P = A + off +note (ADD, C6.2.6), M = n (MOV = ORR), V0 = v in all 16 byte lanes (DUP), S = 0 (MOVZ, C6.2.285); then +note while M >= 16 (CMP, C6.2.96; B.LO, C6.2.34): a block of K = min(M >> 4, 255) chunks (LSR = UBFM, +note C6.2.487; CMP; B.LS; MOVZ X16, #255; V1 = 0 by MOVI, C7.2.247), each chunk LD1 {V2.16B}, [P], #16 +note (post-index), CMEQ V2, V2, V0 (0xff where equal), SUB V1.16B, V1, V2 (+1 per equal lane; 255 chunks +note keep each lane <= 255), SUB M, M, #16, SUBS X16, X16, #1, B.NE; then UADDLV H2, V1.16B (the 16 lanes, +note at most 4080), UMOV W16, V2.H[0], ADD S, S, X16, B back (C6.2.35). The last M < 16 bytes one at a +note time: CBZ M (C6.2.78), UMOV WK, V0.B[0], LDRB W16, [P], #1 (post-index), CMP W16, WK, CINC S, S, EQ +note (CSINC), SUBS M, M, #1, B.NE. Every word is a fixed part from the instruction's Arm ARM encoding ORed +note with register fields; each fixed part was checked against llvm-mc 18 at two or more register +note choices. P, M, S and K are new temporaries, so no name's register changes; X16 is scratch; +note V0-V2 carry nothing between operations (see scrub). The result moves to the destination after the +note temporaries are given back. +fn cnt_word dst ulong to void +if ne get irmode 0 0 +fail 9 +end +x = mul get depth 0 32 +sl = get nslot 0 +put fr add x 8 sl +e = arrname +fz_arg e +if ge get nm_kind sub e 1 6 +fail 7 +end +rdval 0 5 255 0 +vs_off x +rdval 0 5 255 0 +if ne get rt 0 5 +fail 7 +end +put fr add x 12 matreg get rk 0 get rv 0 get rt 0 +rdval 0 2 255 0 +if ne get rt 0 2 +fail 7 +end +v = matreg get rk 0 get rv 0 get rt 0 +ro = get fr add x 11 +rn = get fr add x 12 +vs_chk e ro rn +if ne get xtarget 0 0 +xcount e ro rn v +put nslot 0 sl +d = destreg dst +xrr 137 0 2 +xstore 0 d +vdone 1 d 5 +ret +end +s = cnt_a64 e ro rn v +put nslot 0 sl +d = destreg dst +if ne d s +emit or or 2852127712 d shl s 16 +end +vdone 1 d 5 +end + +note cnt_a64: the AArch64 loop of cnt_word (see there), after vs_chk; gives S, the register of the count +fn cnt_a64 e ulong ro ulong rn ulong v ulong to ulong +i = sub e 1 +p = alloc +if ge get nm_kind i 4 +matconst p get nm_addr i +e3 2332033024 p p ro +else +e3 2332033024 p ulong get nm_reg i ro +end +m = alloc +emit or or 2852127712 m shl rn 16 +emit or 1308691456 shl v 5 +s = alloc +emit or 3531603968 s +k = alloc +emit or 4043325471 shl m 5 +emit 1409286659 +emit or 3544513552 shl m 5 +emit 4043570719 +emit 1409286217 +emit 3531612144 +emit 1862329345 +emit or 1289711618 shl p 5 +emit 1847626818 +emit 1847755809 +emit or or 3506454528 m shl m 5 +emit 4043310608 +emit 1426063201 +emit 1848653858 +emit 235027536 +emit or or 2333081600 s shl s 5 +emit 402653168 +emit or 3019899104 m +emit or 234961920 k +emit or 943723536 shl p 5 +emit or 1795162655 shl k 16 +emit or or or 2592084992 s shl s 5 shl s 16 +emit or or 4043310080 m shl m 5 +emit 1426063233 +ret s +end + note Native Linux ARM64 host operations. Encoding forms are those in seed/rung2/table.s2. note Syscall numbers and layouts: Linux asm-generic/unistd.h, fcntl.h and time.h. note No raw address or arbitrary syscall is exposed to an Omega program. @@ -8578,6 +8689,10 @@ ab_seal vdec ret end +if eq c 179 +cnt_word dst +ret +end if eq c 51 e = arrname if ge get nm_kind sub e 1 6 @@ -17012,6 +17127,101 @@ emitb 15 emitb 5 end +note xcount: the x86-64 loop of cnt_word, after vs_chk. RSI = A + off (LEA of a global or text, else the +note array's slot; ADD), RCX = n, AL = v (zero-extended uchar). XMM0 = v in all 16 byte lanes (MOVD, +note PUNPCKLBW, PSHUFLW 0, PSHUFD 0), XMM3 = 0, EDX = 0 (the count). While RCX >= 16 (CMP; JB): a block of +note R8 = min(RCX >> 4, 255) chunks (MOV, SHR, CMP, JBE, MOV R8D 255; PXOR XMM1), each chunk MOVDQU XMM2, +note [RSI]; PCMPEQB XMM2, XMM0 (0xff where equal); PSUBB XMM1, XMM2 (+1 per equal lane; 255 chunks keep +note each lane <= 255); ADD RSI, 16; SUB RCX, 16; DEC R8; JNE; then PSADBW XMM1, XMM3 (each 8-lane half +note summed into its quadword), MOVQ R9 / ADD RDX twice around PSRLDQ XMM1, 8, JMP back. The last RCX < 16 +note bytes one at a time: TEST; JE; CMP [RSI], AL; JNE; INC RDX; INC RSI; DEC RCX; JNE. SSE2 only (every +note x86-64 CPU), unaligned loads, never a byte outside the checked slice. The 128 bytes after the four +note setup moves are fixed (Intel SDM vol. 2 encodings, checked byte for byte against llvm-mc 18; the short +note jumps' distances are constant); RDX holds the result for cnt_word. RAX, RCX, RDX, RSI, R8, R9 and +note XMM0-XMM3 are emitter scratch here: language values live in RBP slots. +fn xcount e ulong ro ulong rn ulong v ulong to void +i = sub e 1 +if ge get nm_kind i 4 +xaddr 6 get nm_addr i +else +xload 6 ulong get nm_reg i +end +xload 0 ro +xrr 1 6 0 +xload 1 rn +xload 0 v +xb 102 15 110 +emitb 192 +xb 102 15 96 +emitb 192 +xb 242 15 112 +emitb 192 +emitb 0 +xb 102 15 112 +emitb 192 +emitb 0 +xb 102 15 239 +emitb 219 +emitb 49 +emitb 210 +xb 72 131 249 +emitb 16 +emitb 114 +emitb 78 +xb 73 137 200 +xb 73 193 232 +emitb 4 +xb 73 129 248 +xb 255 0 0 +emitb 0 +emitb 118 +emitb 6 +xb 65 184 255 +xb 0 0 0 +xb 102 15 239 +emitb 201 +xb 243 15 111 +emitb 22 +xb 102 15 116 +emitb 208 +xb 102 15 248 +emitb 202 +xb 72 131 198 +emitb 16 +xb 72 131 233 +emitb 16 +xb 73 255 200 +emitb 117 +emitb 231 +xb 102 15 246 +emitb 203 +xb 102 73 15 +emitb 126 +emitb 201 +xb 76 1 202 +xb 102 15 115 +emitb 217 +emitb 8 +xb 102 73 15 +emitb 126 +emitb 201 +xb 76 1 202 +emitb 235 +emitb 172 +xb 72 133 201 +emitb 116 +emitb 15 +emitb 56 +emitb 6 +emitb 117 +emitb 3 +xb 72 255 194 +xb 72 255 198 +xb 72 255 201 +emitb 117 +emitb 241 +end + fn xarray w ulong to void if ne get nb 0 0 fail 9 diff --git a/tests/examples.sh b/tests/examples.sh index 8ad724f..68cce4e 100644 --- a/tests/examples.sh +++ b/tests/examples.sh @@ -36,8 +36,10 @@ # examples/hello) for confirmed divergences. A listed case that fails prints KNOWN; one that # passes prints FIXED and fails the run, so the list is pruned; a line naming no case fails too. # -# EXAMPLES_ONLY=a64 or EXAMPLES_ONLY=x86 runs one target. Exit 0 when nothing FAILs, 1 on any -# FAIL or FIXED, 2 on a usage error. +# EXAMPLES_ONLY=a64 or EXAMPLES_ONLY=x86 runs one target. EXAMPLES_ROOT=DIR reads DIR/examples/*.om +# and DIR/skills/*/examples/*.om instead of the harness (default harness/; tests/omc holds the examples +# of compiler words that are not in the imported harness). Exit 0 when nothing FAILs, 1 on any FAIL or +# FIXED, 2 on a usage error. set -u usage() { echo "usage: sh $0 OMC_A64 OMC_X86 [QEMU_A64]" >&2; exit 2; } @@ -54,7 +56,8 @@ ONLY=${EXAMPLES_ONLY:-} case $ONLY in ''|a64|x86) ;; *) usage ;; esac here=$(cd "$(dirname "$0")" && pwd) -root=$(dirname "$here")/harness +root=${EXAMPLES_ROOT:-$(dirname "$here")/harness} +root=$(cd "$root" 2>/dev/null && pwd) || { echo "examples: no directory ${EXAMPLES_ROOT:-harness}" >&2; exit 2; } known=$here/examples.known [ -d "$root/examples" ] || { echo "examples: no $root/examples" >&2; exit 2; } diff --git a/tests/omc/README.md b/tests/omc/README.md new file mode 100644 index 0000000..2f01ce7 --- /dev/null +++ b/tests/omc/README.md @@ -0,0 +1,95 @@ +# tests/omc: compiler words beyond the imported harness + +`harness/` is the Omega harness imported verbatim (`harness/PROVENANCE.md`), so a word this repository +adds to `seed/omc/omc.om` cannot be documented or tested there. Its guide notes are here, written as the +rows the harness guide (`harness/skills/writing-omega/SKILL.md`) gains when it takes the word. Its examples +are in `examples/`, in the harness format (`NAME.om`, `NAME.expect`, optional `NAME.stdout`), and +`build.sh` runs them on both targets: + +```sh +EXAMPLES_ROOT=tests/omc sh tests/examples.sh out/omc out/omc-x86 [qemu-aarch64-static] +``` + +Every rule below was observed with the compilers `build.sh` grows from `seed/` (pins in +`seed/PINS.tsv`), on AArch64 and on x86-64 through `target x86linux`, with the same results on both. + +## count (OM-2) + +`count A off n v` is a `ulong`: how many of the `n` elements of `A` from `off` equal `v`. + +| | | +|---|---| +| 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 | +| A | the name of a `uchar` array: a local, a global, a text, or a `view` / `edit` parameter, sized or unsized (`*`) | +| off, n | `ulong`; a bare number takes `ulong` | +| v | `uchar`; a bare number takes `uchar` (0..255) | +| result | `ulong`, 0..n | +| traps | 3 when the slice is outside `A`: unless `n <= len A` and `off <= len A - n`. Nothing else | +| order | the four values are read left to right; then the slice is checked; then the elements are read | + +The slice check is the one `read` and `write` make (`vs_chk` in `seed/omc/omc.om`): `off + n` is never +formed, so it cannot wrap (`count_trap3_wrap.om`). An in-bounds slice never traps, including `n = 0` at +`off = len A` (`count_word.om`). The word replaces the loop + +``` +c = ulong 0 +for i ulong n bound N +if eq get A add off i v +c = add c 1 +end +end +``` + +with one machine loop, 16 bytes per pass (AArch64: `LD1`, `CMEQ`, `SUB` in NEON lanes; x86-64: `MOVDQU`, +`PCMPEQB`, `PSUBB` in SSE2, which every x86-64 CPU has), summed every 255 passes, and the last 0..15 bytes +one at a time. The loop above costs about 37 x86-64 instructions per byte under omc-x86, because every +name lives in a stack slot; `count` measured at over 20 GB/s on a cached 1 MiB buffer. + +### Rows for the harness guide + +Section 4, Words: + +| Kind | Words | +|---|---| +| arrays (OM-2) | `count A off n v` | + +Section 5, Traps: the row "array index or I/O slice out of bounds", trap 3, gains `count`'s slice +(`count_trap3.om`, `count_trap3_wrap.om`). + +Section 9, OM-2 units, gains: "`count A off n v`, the number of `uchar` elements of `A[off, off+n)` equal to +`v`, the slice checked as `read` checks it (trap 3)". + +Section 11, Compiler errors: `count` adds no error number. Each refusal is an existing code with its +existing meaning; code 26 is still never emitted. + +| Code | Refusal of count | Example | +|---|---|---| +| 3 | `count` in an OM-1 unit (an unknown name or function) | `count_err03_om1` | +| 4 | fewer than four values (reported on the next line, as every error 4) | `count_err04_short` | +| 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` | +| 7 | `off` or `n` not `ulong`, `v` not `uchar` | `count_err07_offset`, `count_err07_value` | +| 8 | a bare number `v` outside 0..255 | `count_err08_value` | +| 9 | `part` as `A` (`part` is only a call argument) | `count_err09_part` | +| 16 | `count` as a name in an OM-2 unit, or as a statement | `count_err16_name`, `count_err16_statement` | +| 27 | `A` a tracked `fresh` array (it leaves the analysis, as with `read` and `write`) | `count_err27_fresh` | +| 30 | `count` in a `requires`, `ensures` or `invariant` clause | `count_err30_contract` | + +Section 12, Common mistakes: + +| Mistake | Write instead | Refusal | Example | +|---|---|---|---| +| `count a 0 n 10` on its own line | `k = count a 0 n 10` | error 16 | `count_err16_statement` | +| `count a 0 n 10` with `a` a `uint` array | a `uchar` array, or a loop | error 7 | `count_err07_element` | +| `count a o n c` with `c` an `int` | `uchar c` | error 7 | `count_err07_value` | +| 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` | + +Section 14, Example index: OM-2 and contracts gain `count_word`; traps gain `count_trap3`, +`count_trap3_wrap`; refusals gain `count_err*`. + +### What build.sh checks + +- step 8: the 16 examples here pass on both targets; +- step 9 (negative control): `seed/omc/omc.om` with the one line `vs_chk e ro rn` of `fn cnt_word` + deleted builds a compiler whose `count` reads without checking. That compiler must fail + `count_trap3` and `count_trap3_wrap` (no exit 103) on every target that runs, while `count_word` still + passes, so the failure is the missing check and nothing else. diff --git a/tests/omc/examples/count_err03_om1.expect b/tests/omc/examples/count_err03_om1.expect new file mode 100644 index 0000000..b2e04dd --- /dev/null +++ b/tests/omc/examples/count_err03_om1.expect @@ -0,0 +1,2 @@ +compile 3 +stderr omc: line 4: error 3 diff --git a/tests/omc/examples/count_err03_om1.om b/tests/omc/examples/count_err03_om1.om new file mode 100644 index 0000000..de25ddf --- /dev/null +++ b/tests/omc/examples/count_err03_om1.om @@ -0,0 +1,6 @@ +note count_err03_om1: count is an OM-2 word; in an OM-1 unit it is an unknown name, error 3. Fix: an om2 first line +fn main to int +a = new uchar 4 0 +n = count a 0 4 10 +ret 0 +end diff --git a/tests/omc/examples/count_err04_short.expect b/tests/omc/examples/count_err04_short.expect new file mode 100644 index 0000000..5a93673 --- /dev/null +++ b/tests/omc/examples/count_err04_short.expect @@ -0,0 +1,2 @@ +compile 4 +stderr omc: line 6: error 4 diff --git a/tests/omc/examples/count_err04_short.om b/tests/omc/examples/count_err04_short.om new file mode 100644 index 0000000..ceee04c --- /dev/null +++ b/tests/omc/examples/count_err04_short.om @@ -0,0 +1,7 @@ +note count_err04_short: count takes four values, A off n v; with three the line ends first, error 4 +om2 +fn main to int +a = new uchar 4 0 +n = count a 0 4 +ret 0 +end diff --git a/tests/omc/examples/count_err07_element.expect b/tests/omc/examples/count_err07_element.expect new file mode 100644 index 0000000..4249627 --- /dev/null +++ b/tests/omc/examples/count_err07_element.expect @@ -0,0 +1,2 @@ +compile 7 +stderr omc: line 5: error 7 diff --git a/tests/omc/examples/count_err07_element.om b/tests/omc/examples/count_err07_element.om new file mode 100644 index 0000000..77344a8 --- /dev/null +++ b/tests/omc/examples/count_err07_element.om @@ -0,0 +1,7 @@ +note count_err07_element: count counts uchar elements only; a uint array is error 7, as read and write refuse it +om2 +fn main to int +a = new uint 4 0 +n = count a 0 4 10 +ret 0 +end diff --git a/tests/omc/examples/count_err07_offset.expect b/tests/omc/examples/count_err07_offset.expect new file mode 100644 index 0000000..4249627 --- /dev/null +++ b/tests/omc/examples/count_err07_offset.expect @@ -0,0 +1,2 @@ +compile 7 +stderr omc: line 5: error 7 diff --git a/tests/omc/examples/count_err07_offset.om b/tests/omc/examples/count_err07_offset.om new file mode 100644 index 0000000..93d21cd --- /dev/null +++ b/tests/omc/examples/count_err07_offset.om @@ -0,0 +1,7 @@ +note count_err07_offset: off and n are ulong; a uint offset is error 7. Fix: ulong o +om2 +fn main to int +a = new uchar 4 0 +n = count a uint 0 4 10 +ret 0 +end diff --git a/tests/omc/examples/count_err07_scalar.expect b/tests/omc/examples/count_err07_scalar.expect new file mode 100644 index 0000000..4249627 --- /dev/null +++ b/tests/omc/examples/count_err07_scalar.expect @@ -0,0 +1,2 @@ +compile 7 +stderr omc: line 5: error 7 diff --git a/tests/omc/examples/count_err07_scalar.om b/tests/omc/examples/count_err07_scalar.om new file mode 100644 index 0000000..66728ae --- /dev/null +++ b/tests/omc/examples/count_err07_scalar.om @@ -0,0 +1,7 @@ +note count_err07_scalar: A must name an array; a scalar name is error 7 +om2 +fn main to int +x = ulong 3 +n = count x 0 4 10 +ret 0 +end diff --git a/tests/omc/examples/count_err07_struct.expect b/tests/omc/examples/count_err07_struct.expect new file mode 100644 index 0000000..4249627 --- /dev/null +++ b/tests/omc/examples/count_err07_struct.expect @@ -0,0 +1,2 @@ +compile 7 +stderr omc: line 5: error 7 diff --git a/tests/omc/examples/count_err07_struct.om b/tests/omc/examples/count_err07_struct.om new file mode 100644 index 0000000..f3c0fe9 --- /dev/null +++ b/tests/omc/examples/count_err07_struct.om @@ -0,0 +1,11 @@ +note count_err07_struct: A must name an array; a struct (or a layout) is error 7 +om2 +struct P a uint b uint end +fn f p view P to ulong +ret count p 0 1 10 +end +fn main to int +p = new P 1 2 +r = f p +ret 0 +end diff --git a/tests/omc/examples/count_err07_value.expect b/tests/omc/examples/count_err07_value.expect new file mode 100644 index 0000000..4249627 --- /dev/null +++ b/tests/omc/examples/count_err07_value.expect @@ -0,0 +1,2 @@ +compile 7 +stderr omc: line 5: error 7 diff --git a/tests/omc/examples/count_err07_value.om b/tests/omc/examples/count_err07_value.om new file mode 100644 index 0000000..85ca4eb --- /dev/null +++ b/tests/omc/examples/count_err07_value.om @@ -0,0 +1,7 @@ +note count_err07_value: v is a uchar; an int value is error 7. Fix: uchar v, or a bare number +om2 +fn main to int +a = new uchar 4 0 +n = count a 0 4 int 10 +ret 0 +end diff --git a/tests/omc/examples/count_err08_value.expect b/tests/omc/examples/count_err08_value.expect new file mode 100644 index 0000000..0ae9548 --- /dev/null +++ b/tests/omc/examples/count_err08_value.expect @@ -0,0 +1,2 @@ +compile 8 +stderr omc: line 5: error 8 diff --git a/tests/omc/examples/count_err08_value.om b/tests/omc/examples/count_err08_value.om new file mode 100644 index 0000000..78643c5 --- /dev/null +++ b/tests/omc/examples/count_err08_value.om @@ -0,0 +1,7 @@ +note count_err08_value: a bare number v takes the type uchar; 256 does not fit, error 8 +om2 +fn main to int +a = new uchar 4 0 +n = count a 0 4 256 +ret 0 +end diff --git a/tests/omc/examples/count_err09_part.expect b/tests/omc/examples/count_err09_part.expect new file mode 100644 index 0000000..0659d98 --- /dev/null +++ b/tests/omc/examples/count_err09_part.expect @@ -0,0 +1,2 @@ +compile 9 +stderr omc: line 5: error 9 diff --git a/tests/omc/examples/count_err09_part.om b/tests/omc/examples/count_err09_part.om new file mode 100644 index 0000000..148ada1 --- /dev/null +++ b/tests/omc/examples/count_err09_part.om @@ -0,0 +1,7 @@ +note count_err09_part: part is only an argument of a call; as count's A it is error 9. Fix: count a o n v +om2 +fn main to int +a = new uchar 4 0 +n = count part a 0 2 0 2 0 +ret 0 +end diff --git a/tests/omc/examples/count_err16_name.expect b/tests/omc/examples/count_err16_name.expect new file mode 100644 index 0000000..b42a41a --- /dev/null +++ b/tests/omc/examples/count_err16_name.expect @@ -0,0 +1,2 @@ +compile 16 +stderr omc: line 4: error 16 diff --git a/tests/omc/examples/count_err16_name.om b/tests/omc/examples/count_err16_name.om new file mode 100644 index 0000000..d69ec62 --- /dev/null +++ b/tests/omc/examples/count_err16_name.om @@ -0,0 +1,6 @@ +note count_err16_name: count is reserved in an OM-2 unit; as a name it is error 16 +om2 +fn main to int +count = ulong 1 +ret 0 +end diff --git a/tests/omc/examples/count_err16_statement.expect b/tests/omc/examples/count_err16_statement.expect new file mode 100644 index 0000000..64ff08c --- /dev/null +++ b/tests/omc/examples/count_err16_statement.expect @@ -0,0 +1,2 @@ +compile 16 +stderr omc: line 5: error 16 diff --git a/tests/omc/examples/count_err16_statement.om b/tests/omc/examples/count_err16_statement.om new file mode 100644 index 0000000..9168031 --- /dev/null +++ b/tests/omc/examples/count_err16_statement.om @@ -0,0 +1,7 @@ +note count_err16_statement: count is a value, not a statement; a bare count line is error 16. Fix: n = count ... +om2 +fn main to int +a = new uchar 4 0 +count a 0 4 10 +ret 0 +end diff --git a/tests/omc/examples/count_err27_fresh.expect b/tests/omc/examples/count_err27_fresh.expect new file mode 100644 index 0000000..2a72e5e --- /dev/null +++ b/tests/omc/examples/count_err27_fresh.expect @@ -0,0 +1,2 @@ +compile 27 +stderr omc: line 4: error 27 diff --git a/tests/omc/examples/count_err27_fresh.om b/tests/omc/examples/count_err27_fresh.om new file mode 100644 index 0000000..2ce4574 --- /dev/null +++ b/tests/omc/examples/count_err27_fresh.om @@ -0,0 +1,10 @@ +note count_err27_fresh: count reads its slice, so a tracked fresh array leaves the analysis, error 27, as in read and write +om2 +fn f s edit uchar 8 fresh to ulong +ret count s 0 8 10 +end +fn main to int +b = new uchar 8 0 +r = f b +ret 0 +end diff --git a/tests/omc/examples/count_err30_contract.expect b/tests/omc/examples/count_err30_contract.expect new file mode 100644 index 0000000..ffd705f --- /dev/null +++ b/tests/omc/examples/count_err30_contract.expect @@ -0,0 +1,2 @@ +compile 30 +stderr omc: line 4: error 30 diff --git a/tests/omc/examples/count_err30_contract.om b/tests/omc/examples/count_err30_contract.om new file mode 100644 index 0000000..fb975fa --- /dev/null +++ b/tests/omc/examples/count_err30_contract.om @@ -0,0 +1,11 @@ +note count_err30_contract: a contract clause takes integer words on parameters only; count is error 30 +om2 +fn f a view uchar * n ulong to ulong +requires eq count a 0 n 10 0 +ret n +end +fn main to int +b = new uchar 4 0 +r = f b 2 +ret 0 +end diff --git a/tests/omc/examples/count_trap3.expect b/tests/omc/examples/count_trap3.expect new file mode 100644 index 0000000..63a88c0 --- /dev/null +++ b/tests/omc/examples/count_trap3.expect @@ -0,0 +1,2 @@ +compile 0 +exit 103 diff --git a/tests/omc/examples/count_trap3.om b/tests/omc/examples/count_trap3.om new file mode 100644 index 0000000..c6d75b2 --- /dev/null +++ b/tests/omc/examples/count_trap3.om @@ -0,0 +1,11 @@ +note count_trap3: count checks its slice as read and write do, n <= len A and off <= len A - n, before it +note reads a byte; here off 5 and n 4 end one past an 8-byte array, so it traps 3, exit 103, whatever v is +om2 +text before "before\n" +fn main to int +k = write 1 before 0 len before +a = new uchar 8 10 +o = ulong 5 +n = count a o 4 10 +ret int n +end diff --git a/tests/omc/examples/count_trap3.stdout b/tests/omc/examples/count_trap3.stdout new file mode 100644 index 0000000..90be1f3 --- /dev/null +++ b/tests/omc/examples/count_trap3.stdout @@ -0,0 +1 @@ +before diff --git a/tests/omc/examples/count_trap3_wrap.expect b/tests/omc/examples/count_trap3_wrap.expect new file mode 100644 index 0000000..63a88c0 --- /dev/null +++ b/tests/omc/examples/count_trap3_wrap.expect @@ -0,0 +1,2 @@ +compile 0 +exit 103 diff --git a/tests/omc/examples/count_trap3_wrap.om b/tests/omc/examples/count_trap3_wrap.om new file mode 100644 index 0000000..f3d1e6b --- /dev/null +++ b/tests/omc/examples/count_trap3_wrap.om @@ -0,0 +1,11 @@ +note count_trap3_wrap: off + n is never formed, so it cannot wrap: off 2^64-1 and n 2 would sum to 1, +note inside a 16-byte array, but off > len A - n, so count traps 3, exit 103 +om2 +text before "before\n" +fn main to int +k = write 1 before 0 len before +a = new uchar 16 10 +o = ulong 18446744073709551615 +n = count a o 2 10 +ret int n +end diff --git a/tests/omc/examples/count_trap3_wrap.stdout b/tests/omc/examples/count_trap3_wrap.stdout new file mode 100644 index 0000000..90be1f3 --- /dev/null +++ b/tests/omc/examples/count_trap3_wrap.stdout @@ -0,0 +1 @@ +before diff --git a/tests/omc/examples/count_word.expect b/tests/omc/examples/count_word.expect new file mode 100644 index 0000000..420e65e --- /dev/null +++ b/tests/omc/examples/count_word.expect @@ -0,0 +1,2 @@ +compile 0 +exit 0 diff --git a/tests/omc/examples/count_word.om b/tests/omc/examples/count_word.om new file mode 100644 index 0000000..4cce4f5 --- /dev/null +++ b/tests/omc/examples/count_word.om @@ -0,0 +1,201 @@ +note count_word: OM-2 `count A off n v`, a ulong, is how many of the n uchar elements of A from off +note equal v. Checked here against a byte-by-byte loop: every off 0..47 with lengths 0..40, the 16-byte +note chunk edges and the 4080-byte block edges, four values, over mixed bytes and over a run where every +note byte equals v; then in a local array, a text, a sized view, an unsized view, a part and an edit +note parameter, and with the destination also an input. Prints the number of cases and their counts. +om2 +global g uchar 70000 +global lens ulong 64 +global nlen ulong 1 +global cases ulong 1 +global total ulong 1 +global ob uchar 64 +text tok "count_word ok cases " +text tsum " sum " +text tbad "count_word MISMATCH\n" +text tnl "\n" +text tt "a\nbb\n\nccc\n" + +note naive: the same count over g, one byte at a time +fn naive o ulong n ulong v uchar to ulong +c = ulong 0 +for i ulong n bound 70000 +if eq get g add o i v +c = add c 1 +end +end +ret c +end + +note bad: report a mismatch and stop with exit 1 +fn bad to void +k = write 1 tbad 0 len tbad +exit 1 +end + +note one: compare count with naive for g[o, o + n) and value v +fn one o ulong n ulong v uchar to void +a = count g o n v +b = naive o n v +if ne a b +bad +end +put cases 0 add get cases 0 1 +put total 0 add get total 0 a +end + +note addlen: append n to the length list +fn addlen n ulong to void +k = get nlen 0 +put lens k n +put nlen 0 add k 1 +end + +note sweep: every off in [base, base + 48) with every listed length and four values +fn sweep base ulong to void +for j ulong 48 +o = add base j +for q ulong get nlen 0 bound 64 +n = get lens q +one o n 10 +one o n 0 +one o n 255 +one o n 37 +end +end +end + +note emu: print v in decimal +fn emu v ulong to void +d = new uchar 20 0 +x = v +k = ulong 0 +going = true +while going bound 21 +put d k uchar add rem x 10 48 +k = add k 1 +x = div x 10 +if eq x 0 +going = false +end +end +p = ulong 0 +while gt k 0 bound 21 +k = sub k 1 +put ob p get d k +p = add p 1 +end +w = write 1 ob 0 p +end + +note views: count through a sized view, an unsized view and an edit parameter +fn cs a view uchar 16 o ulong n ulong v uchar to ulong +ret count a o n v +end + +fn cv a view uchar * o ulong n ulong v uchar to ulong +ret count a o n v +end + +fn ce a edit uchar * o ulong n ulong v uchar to ulong +put a 0 v +ret count a o n v +end + +note forms: the other kinds of array, and a destination that is also an input +fn forms to void +loc = new uchar 16 7 +put loc 3 10 +put loc 9 10 +put loc 15 10 +if ne count loc 0 16 10 3 +bad +end +if ne count loc 4 11 10 1 +bad +end +if ne count loc 0 16 7 13 +bad +end +if ne count tt 0 len tt 10 4 +bad +end +if ne count tt 1 3 98 2 +bad +end +if ne cs loc 0 16 10 3 +bad +end +if ne cs loc 10 6 10 1 +bad +end +if ne cv loc 0 16 10 3 +bad +end +if ne cv part loc 3 7 0 7 10 2 +bad +end +if ne cv part loc 4 0 0 0 10 0 +bad +end +if ne ce loc 0 16 10 4 +bad +end +o = ulong 2 +o = count loc o 14 10 +if ne o 3 +bad +end +n = ulong 16 +n = count loc 0 n 10 +if ne n 4 +bad +end +end + +fn main to int +x = uint 12345 +for i ulong 30000 +x = add mul x 1103515245 12345 +b = uchar and shr x 16 255 +if lt b 64 +b = uchar 10 +end +put g i b +end +for i ulong 40000 +put g add 30000 i 10 +end +for i ulong 41 +addlen i +end +addlen 63 +addlen 64 +addlen 65 +addlen 255 +addlen 256 +addlen 257 +addlen 1000 +addlen 4079 +addlen 4080 +addlen 4081 +addlen 4095 +addlen 4096 +addlen 4097 +addlen 8160 +addlen 8161 +addlen 12000 +sweep 0 +sweep 30000 +one 0 70000 10 +one 0 70000 37 +one 69999 1 10 +one 70000 0 10 +forms +k = write 1 tok 0 len tok +emu get cases 0 +k = write 1 tsum 0 len tsum +emu get total 0 +k = write 1 tnl 0 len tnl +ret 0 +end diff --git a/tests/omc/examples/count_word.stdout b/tests/omc/examples/count_word.stdout new file mode 100644 index 0000000..6c7da8d --- /dev/null +++ b/tests/omc/examples/count_word.stdout @@ -0,0 +1 @@ +count_word ok cases 21892 sum 3374608 diff --git a/tools/ledger.om b/tools/ledger.om index fb8dabb..9ac7313 100644 --- a/tools/ledger.om +++ b/tools/ledger.om @@ -320,15 +320,9 @@ s = add d 1 ret catof add o s sub n s end -note nlines: the newline bytes among the first m bytes of fb +note nlines: the newline bytes among the first m bytes of fb (count checks m against fb, trap 3) fn nlines m ulong to ulong -c = ulong 0 -for i ulong m bound 1048576 -if eq get fb i 10 -c = add c 1 -end -end -ret c +ret count fb 0 m 10 end note countf: open the path at inp[o, o+n) and add its file, bytes and lines to cur