omega-mv
Omega: a language for verified programs, semantic identity and machine realization. Seed-born work and existing implementations have distinct provenance.
Brought by drakestapleton
GitHub aien-dev/omega-mv#838 (code only)
Apply it
git checkout -b patch-3964ed3f8eb17d2a 0b77e6ea294aa3d69ccbdb86b9c14d133abea689 curl -fsSL https://interplane.io/drakestapleton/omega-mv/patches/3964ed3f8eb17d2a.patch | git am
Provenance
github-import 1
aien-dev/omega-mv
838
3d8619e58ecb4ef26b92fa9cf8fd12340617aae4
{"kind":"github-import","owner":"aien-dev","repo":"omega-mv","number":838,"url":"https://github.com/aien-dev/omega-mv/pull/838","export_sha256":"967c79e26a893657b5cdaa45efd442be5dbd2ecfe68e25f9a8c1b5da4d60d452","imported_utc":"2026-11-18T16:38:45Z","metadata":"unavailable","state_derived":true}
Changes
diff --git a/.crumb b/.crumb index 35aa2238f..b879edad8 100644 --- a/.crumb +++ b/.crumb @@ -42,68 +42,68 @@ "children": [ { "digest": "9f19f11674ca5dfc3c9c3ae35fe6106557aee9583210da13d257e1b9d88e052c", - "last_commit": "d863780e", + "last_commit": "d863780eb", "name": "calibration", "purpose": "Directory calibration. Purpose not yet described by a human." }, { "digest": "26c2fbf4515e981db72f56dad2fc3fe0667c469551cf6071e28ac50005a36e42", - "last_commit": "943f5139", + "last_commit": "943f5139f", "name": "docs", "purpose": "Directory docs. Purpose not yet described by a human." }, { "digest": "536f7b8db9a92a1f586bad4379d47544a30d1b8e83c2d799c18e66cf8660c9ae", - "last_commit": "30bb116c", + "last_commit": "30bb116cc", "name": "doctrine", "purpose": "Directory doctrine. Purpose not yet described by a human." }, { "digest": "4947c0eb38550826cdfaba5d08384f3ebd284b82ab456ecf276617ce2ef998fd", - "last_commit": "43f25831", + "last_commit": "43f25831b", "name": "mk", "purpose": "Directory mk. Purpose not yet described by a human." }, { "digest": "62b92244c52fa8af6f511012dee2967e98dff162cc1d8bf45d0b2f15cb0adc5f", - "last_commit": "450bbefd", + "last_commit": "450bbefd4", "name": "research", "purpose": "Directory research. Purpose not yet described by a human." }, { "digest": "8bbd5f74b01446007499a9a9be4e9f50f4364af0f578d9cdf26a2afd84e3bb63", - "last_commit": "faebae8c", + "last_commit": "faebae8c1", "name": "spec", "purpose": "Directory spec. Purpose not yet described by a human." }, { "digest": "f390041dbba29a5707b5652e20e6bffd69e0a1f1ebfc493736ff5266370fa4aa", - "last_commit": "82a399b9", + "last_commit": "82a399b9f", "name": "src", "purpose": "Directory src. Purpose not yet described by a human." }, { "digest": "d75dd95349e3cd5e180a91ec848d3e25ac9c3b693f96d4397c9646e37823cba9", - "last_commit": "45f7d769", + "last_commit": "45f7d7691", "name": "tests", "purpose": "Directory tests. Purpose not yet described by a human." }, { "digest": "4e83dea21da137c30076c2dc0ca4f3a488f576f30a94e334558ad5ea523f3a6b", - "last_commit": "a50c3bd4", + "last_commit": "a50c3bd41", "name": "tools", "purpose": "Directory tools. Purpose not yet described by a human." } ], "children_root": "sha256:86ef1d4da867572dbd97c75562b8066d916c4b712e883715e0c3bdd3a13b5f1f", - "compiled_at": "2026-10-26T19:28:07Z", + "compiled_at": "2026-10-26T22:28:07Z", "compiler": "crumb-compile/1", - "digest": "sha256:cbd1d776bb45a312fa9e1d63b688b2085834b584fb1bf95d56e96eefa4d90c28", + "digest": "sha256:fc3cd52f0bac29e0d46bd80a937e53bc24261f107540265e03e189afbcf2487b", "evidence_files": 12477, "evidence_root": "sha256:9d96c8c22e5c574c4768d1eadd333b9750f73cd27be276a0f190c876960008a1", - "files": 17297, - "generated_at_commit": "813d6c4385665b61485ee9d1fa91c0c213ed4ce3", - "source_tree": "sha256:028d088bf1a5bb9001b4082b459ef4efaeaa6e33e4840a5e73a9a5ca07ae4ffe" + "files": 17336, + "generated_at_commit": "5689e15976d1d2379e06faca689ec485a6560b5a", + "source_tree": "sha256:9646d4ad407b94a8da4f81452875af346094748946c3319835623f177f525442" }, "owner_lane": "", "provenance": { diff --git a/seed/query/.crumb b/seed/query/.crumb new file mode 100644 index 000000000..3a0d989b6 --- /dev/null +++ b/seed/query/.crumb @@ -0,0 +1,20 @@ +{ + "extensions": { + "generated": { + "children": [], + "children_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", + "compiled_at": "2026-10-26T22:28:07Z", + "compiler": "crumb-compile/1", + "digest": "sha256:393c9ab2f9d93a11de2c7747e9b9cf10e01b6c23789493e6efd719fd3a7b9662", + "evidence_files": 0, + "evidence_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", + "files": 39, + "generated_at_commit": "5689e15976d1d2379e06faca689ec485a6560b5a", + "source_tree": "sha256:7cd2ec4545641aa7845dd514590a65ad7c9bbe5f37ae70d557e3c6af4e3ae22f" + } + }, + "layer": "seed", + "name": "query", + "purpose": "omq: hole oracle for agents writing seeded Omega; answers scope, valid next forms, obligations and repaired diagnostics by running the pinned omc on witness programs, plus a prefix-viability server", + "schema_version": "1.0.0" +} diff --git a/seed/query/README.md b/seed/query/README.md new file mode 100644 index 000000000..35b855c4d --- /dev/null +++ b/seed/query/README.md @@ -0,0 +1,144 @@ +# seed/query: omq, the hole oracle for agents writing seeded Omega + +`omq` answers one question an agent writing `.om` keeps asking: what may I write here? It reads a +program with one hole `??` and prints, as one JSON line, the names in scope with their types, the +functions, every statement or value form that fits the hole, the open obligations (blocks to close, +names the register pool still allows, contracts, effects) and the diagnostic with a repair when +the program does not compile. + +`omq` is written in seeded Omega. It does not reimplement the compiler's rules. It runs the pinned +`omc` as a subprocess on witness programs (memfd input and stderr, `fork`, `execprog`, `waitpid`) +and reports only what `omc` accepted. A form appears in `next` only after a witness with that form +at the hole compiled. When a rule changes in `omc`, the answers change with it; nothing in `omq` +needs editing. + +Status: EXERCISED on the host (Linux AArch64, DGX Spark, 2026-10-26) against compiler +`2c82480596bbdc50cda9626866209ef662d5834f278f839ff4ce0250c495029f` (omega-mv `0b77e6ea`). +`check.sh` printed `SEED QUERY PASS 45 checks`. This is host evidence only. It is not a merge, and +no agent-loop result is claimed here. + +| File | Contents | +|---|---| +| `omq.om` | The tool (one OM-1 program unit) | +| `check.sh` | Gate: goldens, negative controls, recompiled next forms, fake compilers, a mutant | +| `cases/*.om`, `cases/*.json` | Inputs and reviewed expected answers | + +## Build and use + +```sh +seed/out/omc < seed/query/omq.om > omq && chmod +x omq +./omq seed/out/omc < program.om # one JSON line on stdout +./omq seed/out/omc prefix # prefix-viability server (below) +sh seed/query/check.sh # PASS / FAIL / NOT_RUN (wrong compiler) +``` + +The first argument is the compiler. `omq` refuses one that fails its selftest (a known-good +program must compile and a known-bad one must not), so `/bin/true` or a wrong path gives exit 2, +never an answer. + +Hole syntax: a line that is only `??` is a statement hole; `??` inside a line is a value hole. With +no hole, `omq` only checks the program. Exit codes: 0 answer printed, 1 more than one hole or a +hole outside a function (the answer says which), 2 usage or a compiler that fails the selftest. + +## Answer (abridged from `cases/stmt_loop.json`) + +```json +{"hole":{"line":5,"kind":"statement","fn_line":2,"depth":2}, + "check":{"code":0}, + "scope":[{"name":"cnt","type":"ulong[]","writable":true},{"name":"n","type":"ulong"}], + "fns":["fn sum a view uint 8 n ulong to ulong"], + "next":["ret <ulong>","n = <ulong>","NEW = <value>","if <bool>","break","put cnt <ulong> <ulong>"], + "obligations":{"function":"fn sum a view uint 8 n ulong to ulong","open":["for","fn"], + "regs_free":11,"contracts":["requires le n 8"],"effects":["write:io"]}, + "compiles":70} +``` + +- `hole`: 1-based line, `statement` or `value`, the enclosing function's line, block depth. +- `check`: `code` 0 when the text before the hole, closed off by a trap and the missing `end`s, + compiles. The text after the hole is not checked: it is the part still to be written. Otherwise + the `omc` error `code` and `line`, and for an OM-2 diagnostic `sev`, `col`, `expect`, `found`, + `obligation`, `span`. `why` is a sentence; `fix_rename`, `fix_move_up` or `fix_append_end` is a + repair that `omq` recompiled, with `rc_after_fix`. +- `check.status`, one of four values and never a plain VALID: + - `INVALID`: `omc` rejects the text, and with a hole the error lies before it, so nothing written + at the hole can repair it; + - `INCOMPLETE`: with a hole, the closed-off witness compiles, so a valid completion exists; + - `VALID-UNPROVEN`: no hole and `omc` accepts the program. Well formed only: contracts become + run-time traps and no behaviour is proven; + - `UNKNOWN`: with a hole, the error lies after it in the witness's added closing lines and may + depend on what the hole holds. +- `scope`: names visible at the hole, typed by probing `omc` (an array is `T[]`; `writable` when + `put` into it compiles; a fresh array, whose elements may not be read before a `put`, is typed + by writing element 0 first; `struct` when `get NAME F` compiles for the first field `F` of a + declared struct). Candidates are parameters, `=` bindings, `for` variables and bare `NAME VALUE` + bindings (`pu s`), which `omc` accepts at the pin. A variable named `export` is a name, not a + function line. Names from closed blocks are absent. +- `next`: statement forms (or, at a value hole, values and operators of the expected type in + `expect`) that compiled at the hole. `<T>` is a slot for a value of type T. A call to a user + function is shown with a concrete argument list: a typed literal per scalar parameter and a + visible array per array parameter (the first of up to four candidates `omc` accepts). +- `obligations`: open blocks innermost first, how many more `ulong` names fit before error 12 + (error 14, capacity, does not end the count; an `inline` function is probed as a plain one, so + the count is its body's, not a call site's expansion), + the function's `requires`/`ensures` and the invariants of loops still open, and the host-word + effects the function already uses (`read:fs`, `write:io`, `net`, ...; the table covers every + host keyword of `omc`, which `check.sh` checks). +- `compiles`: how many times `omc` ran. + +## Prefix mode + +`omq OMC prefix` serves requests on fd 0: a 4-byte little-endian length, then that many bytes of +program prefix. For each it writes one line `V CODE`: whether the prefix can still be completed to +a program `omc` accepts, and the code. This replaces `omviable.om` (OmegaMind, handoffs +`OmegaMind/omviable/`), which this unit absorbed; OmegaMind's own `selftest.py` passed 13/13 cases +against it. An open word at the prefix end that is longer than any word `omc` accepts (a name over +`NAMEMAX` 8 bytes, a number over `NUMMAX` 20 bytes) is answered `2`, since growing it never +helps; before this, `local arr[i324i324` was answered `1 13` and sampled models ran to max_tokens +(OmegaMind, `OmegaMind/omviable/wdead-check.txt`). Note lines, text literals and prefixes of +`invariant` are exempt; a first word that merely starts with `note` is wrongly exempt (fails open). +#837 raises `NAMEMAX` to 63. + +## Limits + +- One hole per request. +- A call with several array parameters gives every array parameter the same candidate rank, so a + call that needs two different arrays of one element type may be missing from `next` + (`compress s w kt m o` in `om/sha256/sha256-om2.om`). +- Each answer costs tens to a few hundred compiles; at 1000+ line programs one answer took up to + 0.7 s on one X925 core. +- Struct-typed parameters are not filled. +- An abstract value is in `scope` with an empty type: no probe names its type. +- Scope order is first mention, so a loop variable reused after a closed loop of the same name + can come before names declared between the two loops. +- A hole inside an `inline` function whose caller passes a `fresh` array gives `check` code 27: + the trap at the hole leaves the callee's writes out of the caller's expansion. + +## Evidence + +`check.sh` on 2026-10-26: `SEED QUERY PASS 45 checks`, `omq` sha256 +`62a21f4462e217f4efcc3eaf1495ca65a0b51a7d47cb1dac25e54a92e1877f9c`. The checks are byte-identical +goldens for every case; negative controls (closed-block name absent, no `break` outside a loop, a +wrong-type array excluded, no contract forms in OM-1, a length-mismatched array not offered and +rejected by `omc`); every `next` form at the loop hole substituted and recompiled; the omitted +`else` refused; the error-13 repair recompiled; three fake compilers refused; prefix verdicts; a +corrupted golden detected; a mutant `omq` that accepts every witness caught. + +Real programs (harness `trial.py`, handoffs `OmegaIntent/realprog/`, pinned to cpu 17, X925): a +hole placed at a seeded random statement line of standalone repo programs (BLAKE3, SHA-256, the +WASM and SPIR-V emitters, the OM-2 gate, host tests). With the `omq` above (sha `b7fcd8a4…`), on +a sample drawn after the last scope change (seed 4242, 112 holes in 28 programs), every base +witness compiled and the removed statement's form was in `next` for 112 of 112; the 95% +Clopper-Pearson lower bound is 0.968 +(`WolframLanguageEvaluator: InverseBetaRegularized[0.025, 112, 1] -> 0.9676`). Median wall time +per answer 0.031 s, 90th percentile 0.26 s, maximum 0.69 s, all on cpu 17 (X925) +(`WolframLanguageEvaluator: {Median[w], Quantile[w,0.9], Max[w]} -> {0.031195, 0.26379, 0.68583}`; +data `realprog/fresh4.jsonl`). + +White-box differential (omega-mv #843, `om2 query` in `omc`, binary sha256 `2c3124f6…`): 827 +statement holes, up to three per tracked `.om` file that compiles (seed 838, 393 files). Comparing +`hole`, `scope`, `fns` and `obligations`, 625 of 827 answers are equal (525 before this change; +`WolframLanguageEvaluator: N[{625/827, 525/827}] -> {0.7557, 0.6348}`). Of the rest, 169 differ only +in `effects` (`omq` lists every host word use in the function; `omc`'s list is duplicate-free and +a subset of it in all 825 answered holes), 2 have no white-box answer (`omc` error 16), and 31 +differ in `scope` (6 of them also in `effects`, 1 also in `regs_free`): the three limits above +(16 abstract, 14 order, 1 fresh inline). diff --git a/seed/query/cases/arr_call.json b/seed/query/cases/arr_call.json new file mode 100644 index 000000000..7f4f6361a --- /dev/null +++ b/seed/query/cases/arr_call.json @@ -0,0 +1 @@ +{"hole":{"line":9,"kind":"statement","fn_line":4,"depth":1},"check":{"code":0,"status":"INCOMPLETE"},"scope":[{"name":"b","type":"uchar[]","writable":true},{"name":"s","type":"uint[]","writable":true},{"name":"k","type":"uint[]","writable":true},{"name":"v","type":"ulong"}],"fns":["fn fill a edit uint 64 n ulong to void","fn main to int"],"next":["ret <int>","v = <ulong>","NEW = <value>","if <bool>","while <bool>","for NAME T COUNT","trap <1..255>","exit <int>","put b <ulong> <uchar>","put s <ulong> <uint>","put k <ulong> <uint>","fill k ulong 1"],"obligations":{"function":"fn main to int","open":["fn"],"regs_free":12,"contracts":[],"effects":[]},"compiles":57} diff --git a/seed/query/cases/arr_call.om b/seed/query/cases/arr_call.om new file mode 100644 index 000000000..77f60db1a --- /dev/null +++ b/seed/query/cases/arr_call.om @@ -0,0 +1,11 @@ +fn fill a edit uint 64 n ulong to void +put a 0 uint n +end +fn main to int +b = new uchar 64 0 +s = new uint 8 0 +k = new uint 64 0 +v ulong 5 +?? +ret int v +end diff --git a/seed/query/cases/else_ret.json b/seed/query/cases/else_ret.json new file mode 100644 index 000000000..09983df83 --- /dev/null +++ b/seed/query/cases/else_ret.json @@ -0,0 +1 @@ +{"hole":{"line":5,"kind":"statement","fn_line":1,"depth":2},"check":{"code":0,"status":"INCOMPLETE"},"scope":[{"name":"a","type":"int"}],"fns":["fn choose a int to int","fn main to int"],"next":["ret <int>","a = <int>","NEW = <value>","if <bool>","while <bool>","for NAME T COUNT","trap <1..255>","exit <int>","end"],"obligations":{"function":"fn choose a int to int","open":["else","fn"],"regs_free":15,"contracts":[],"effects":[]},"compiles":42} diff --git a/seed/query/cases/else_ret.om b/seed/query/cases/else_ret.om new file mode 100644 index 000000000..533a82ba5 --- /dev/null +++ b/seed/query/cases/else_ret.om @@ -0,0 +1,10 @@ +fn choose a int to int +if lt a 0 +ret neg a +else +?? +end +end +fn main to int +ret choose int 3 +end diff --git a/seed/query/cases/err13.json b/seed/query/cases/err13.json new file mode 100644 index 000000000..97b04c6df --- /dev/null +++ b/seed/query/cases/err13.json @@ -0,0 +1 @@ +{"check":{"code":13,"line":2,"sev":"unknown","col":1,"expect":"name","found":"abcdefghijk","obligation":"-","span":[15,26],"why":"name longer than 8 characters: shorten it","fix_rename":"abcdefg0","rc_after_fix":0,"status":"INVALID"},"compiles":5} diff --git a/seed/query/cases/err13.om b/seed/query/cases/err13.om new file mode 100644 index 000000000..fe9561ece --- /dev/null +++ b/seed/query/cases/err13.om @@ -0,0 +1,4 @@ +fn main to int +abcdefghijk = uint 1 +ret int abcdefghijk +end diff --git a/seed/query/cases/err15.json b/seed/query/cases/err15.json new file mode 100644 index 000000000..e40dea205 --- /dev/null +++ b/seed/query/cases/err15.json @@ -0,0 +1 @@ +{"check":{"code":15,"line":5,"sev":"incomplete","col":1,"expect":"end","found":"","obligation":"-","span":[47,47],"why":"missing end: close every fn/if/while/for with end","fix_append_end":2,"rc_after_fix":18,"status":"INVALID"},"compiles":6} diff --git a/seed/query/cases/err15.om b/seed/query/cases/err15.om new file mode 100644 index 000000000..f38e6b946 --- /dev/null +++ b/seed/query/cases/err15.om @@ -0,0 +1,4 @@ +fn main to int +x = uint 1 +if eq x uint 1 +ret 0 diff --git a/seed/query/cases/err3.json b/seed/query/cases/err3.json new file mode 100644 index 000000000..0cd51ca21 --- /dev/null +++ b/seed/query/cases/err3.json @@ -0,0 +1 @@ +{"check":{"code":3,"line":2,"sev":"error","col":9,"expect":"name","found":"g","obligation":"-","span":[23,24],"why":"unknown name: define it before this line (globals, consts and texts above their first use; locals in an enclosing block that is still open)","fix_move_up":"global g uint 1","rc_after_fix":0,"status":"INVALID"},"compiles":5} diff --git a/seed/query/cases/err3.om b/seed/query/cases/err3.om new file mode 100644 index 000000000..0ca4da28f --- /dev/null +++ b/seed/query/cases/err3.om @@ -0,0 +1,5 @@ +fn main to int +x = get g 0 +ret int x +end +global g uint 1 diff --git a/seed/query/cases/outside.json b/seed/query/cases/outside.json new file mode 100644 index 000000000..d4905df79 --- /dev/null +++ b/seed/query/cases/outside.json @@ -0,0 +1 @@ +{"error":"the hole is not inside a function","compiles":2} diff --git a/seed/query/cases/outside.om b/seed/query/cases/outside.om new file mode 100644 index 000000000..a87b32ed9 --- /dev/null +++ b/seed/query/cases/outside.om @@ -0,0 +1,2 @@ +global g uint 1 +?? diff --git a/seed/query/cases/regs_capacity.json b/seed/query/cases/regs_capacity.json new file mode 100644 index 000000000..4f3ab3a3e --- /dev/null +++ b/seed/query/cases/regs_capacity.json @@ -0,0 +1 @@ +{"hole":{"line":11,"kind":"statement","fn_line":4,"depth":3},"check":{"code":0,"status":"INCOMPLETE"},"scope":[{"name":"a","type":"ulong"},{"name":"b","type":"ulong"}],"fns":["fn nop to void inline","fn main to int"],"next":["ret <int>","NEW = <value>","trap <1..255>","exit <int>","end","nop"],"obligations":{"function":"fn main to int","open":["for","for","fn"],"regs_free":16,"contracts":[],"effects":[]},"compiles":48} diff --git a/seed/query/cases/regs_capacity.om b/seed/query/cases/regs_capacity.om new file mode 100644 index 000000000..32559497c --- /dev/null +++ b/seed/query/cases/regs_capacity.om @@ -0,0 +1,25 @@ +om2 +fn nop to void inline +end +fn main to int +for a ulong 64 unroll +for b ulong 64 unroll +nop +nop +nop +nop +?? +nop +nop +nop +nop +nop +nop +nop +nop +nop +nop +end +end +ret 0 +end diff --git a/seed/query/cases/regs_inline.json b/seed/query/cases/regs_inline.json new file mode 100644 index 000000000..1617fa500 --- /dev/null +++ b/seed/query/cases/regs_inline.json @@ -0,0 +1 @@ +{"hole":{"line":4,"kind":"statement","fn_line":2,"depth":1},"check":{"code":0,"status":"INCOMPLETE"},"scope":[{"name":"x","type":"int"},{"name":"y","type":"int"}],"fns":["fn inner x int to int inline","fn main to int"],"next":["ret <int>","x = <int>","y = <int>","NEW = <value>","if <bool>","while <bool>","for NAME T COUNT","trap <1..255>","exit <int>"],"obligations":{"function":"fn inner x int to int inline","open":["fn"],"regs_free":14,"contracts":[],"effects":[]},"compiles":54} diff --git a/seed/query/cases/regs_inline.om b/seed/query/cases/regs_inline.om new file mode 100644 index 000000000..2659a3cbf --- /dev/null +++ b/seed/query/cases/regs_inline.om @@ -0,0 +1,9 @@ +om2 +fn inner x int to int inline +y = add x 1 +?? +ret y +end +fn main to int +ret sub inner 3 4 +end diff --git a/seed/query/cases/ret_value.json b/seed/query/cases/ret_value.json new file mode 100644 index 000000000..255e68540 --- /dev/null +++ b/seed/query/cases/ret_value.json @@ -0,0 +1 @@ +{"hole":{"line":3,"kind":"value","fn_line":1,"depth":1},"check":{"code":0,"status":"INCOMPLETE"},"scope":[{"name":"n","type":"uint"}],"fns":["fn main to int"],"expect":["uint"],"next":["n","add <uint> <uint>","sub <uint> <uint>","mul <uint> <uint>","div <uint> <uint>","rem <uint> <uint>","and <uint> <uint>","or <uint> <uint>","xor <uint> <uint>","shl <uint> <uint>","shr <uint> <uint>","uint <any>"],"obligations":{"function":"fn main to int","open":["fn"],"regs_free":15,"contracts":[],"effects":[]},"compiles":37} diff --git a/seed/query/cases/ret_value.om b/seed/query/cases/ret_value.om new file mode 100644 index 000000000..8551647a4 --- /dev/null +++ b/seed/query/cases/ret_value.om @@ -0,0 +1,4 @@ +fn main to int +n = uint 3 +ret int add n ?? +end diff --git a/seed/query/cases/scope_bare.json b/seed/query/cases/scope_bare.json new file mode 100644 index 000000000..39560f610 --- /dev/null +++ b/seed/query/cases/scope_bare.json @@ -0,0 +1 @@ +{"hole":{"line":5,"kind":"statement","fn_line":2,"depth":1},"check":{"code":0,"status":"INCOMPLETE"},"scope":[{"name":"s","type":"ulong"},{"name":"pu","type":"ulong"}],"fns":["fn main to int"],"next":["ret <int>","s = <ulong>","pu = <ulong>","NEW = <value>","if <bool>","while <bool>","for NAME T COUNT","trap <1..255>","exit <int>"],"obligations":{"function":"fn main to int","open":["fn"],"regs_free":14,"contracts":[],"effects":[]},"compiles":46} diff --git a/seed/query/cases/scope_bare.om b/seed/query/cases/scope_bare.om new file mode 100644 index 000000000..c43df0188 --- /dev/null +++ b/seed/query/cases/scope_bare.om @@ -0,0 +1,7 @@ +om2 +fn main to int +s = ulong 3 +pu s +?? +ret 0 +end diff --git a/seed/query/cases/scope_export.json b/seed/query/cases/scope_export.json new file mode 100644 index 000000000..da9f7a4b9 --- /dev/null +++ b/seed/query/cases/scope_export.json @@ -0,0 +1 @@ +{"hole":{"line":4,"kind":"statement","fn_line":1,"depth":1},"check":{"code":0,"status":"INCOMPLETE"},"scope":[{"name":"export","type":"ulong"},{"name":"big","type":"ulong"}],"fns":["fn n2 to ulong","fn main to int"],"next":["ret <ulong>","export = <ulong>","big = <ulong>","NEW = <value>","if <bool>","while <bool>","for NAME T COUNT","trap <1..255>","exit <int>"],"obligations":{"function":"fn n2 to ulong","open":["fn"],"regs_free":14,"contracts":[],"effects":[]},"compiles":43} diff --git a/seed/query/cases/scope_export.om b/seed/query/cases/scope_export.om new file mode 100644 index 000000000..539985381 --- /dev/null +++ b/seed/query/cases/scope_export.om @@ -0,0 +1,9 @@ +fn n2 to ulong +export = ulong 11 +big = ulong 9 +?? +ret add export big +end +fn main to int +ret int n2 +end diff --git a/seed/query/cases/scope_fresh.json b/seed/query/cases/scope_fresh.json new file mode 100644 index 000000000..592d254f8 --- /dev/null +++ b/seed/query/cases/scope_fresh.json @@ -0,0 +1 @@ +{"hole":{"line":3,"kind":"statement","fn_line":2,"depth":1},"check":{"code":0,"status":"INCOMPLETE"},"scope":[{"name":"s","type":"uchar[]","writable":true}],"fns":["fn f s edit uchar 8 fresh to ulong","fn main to int"],"next":["ret <ulong>","NEW = <value>","if <bool>","while <bool>","for NAME T COUNT","trap <1..255>","exit <int>","requires <bool>","ensures <bool>","put s <ulong> <uchar>"],"obligations":{"function":"fn f s edit uchar 8 fresh to ulong","open":["fn"],"regs_free":15,"contracts":[],"effects":[]},"compiles":54} diff --git a/seed/query/cases/scope_fresh.om b/seed/query/cases/scope_fresh.om new file mode 100644 index 000000000..d2a24b318 --- /dev/null +++ b/seed/query/cases/scope_fresh.om @@ -0,0 +1,10 @@ +om2 +fn f s edit uchar 8 fresh to ulong +?? +put s 0 1 +ret ulong get s 0 +end +fn main to int +a = new uchar 8 0 +ret int f a +end diff --git a/seed/query/cases/scope_struct.json b/seed/query/cases/scope_struct.json new file mode 100644 index 000000000..355380a1d --- /dev/null +++ b/seed/query/cases/scope_struct.json @@ -0,0 +1 @@ +{"hole":{"line":7,"kind":"statement","fn_line":4,"depth":1},"check":{"code":0,"status":"INCOMPLETE"},"scope":[{"name":"h","type":"struct"},{"name":"p","type":"struct"}],"fns":["fn main to int"],"next":["ret <int>","NEW = <value>","if <bool>","while <bool>","for NAME T COUNT","trap <1..255>","exit <int>"],"obligations":{"function":"fn main to int","open":["fn"],"regs_free":14,"contracts":[],"effects":[]},"compiles":41} diff --git a/seed/query/cases/scope_struct.om b/seed/query/cases/scope_struct.om new file mode 100644 index 000000000..87eabd0ae --- /dev/null +++ b/seed/query/cases/scope_struct.om @@ -0,0 +1,9 @@ +om2 +struct H x uint y ulong end +struct G v uint 2 t uchar end +fn main to int +h = new H 7 9 +p = new G 1 1 +?? +ret int get h x +end diff --git a/seed/query/cases/stmt_effects.json b/seed/query/cases/stmt_effects.json new file mode 100644 index 000000000..3a28605ed --- /dev/null +++ b/seed/query/cases/stmt_effects.json @@ -0,0 +1 @@ +{"hole":{"line":8,"kind":"statement","fn_line":2,"depth":1},"check":{"code":0,"status":"INCOMPLETE"},"scope":[{"name":"tot","type":"ulong[]","writable":true},{"name":"b","type":"uchar[]","writable":true},{"name":"r","type":"long"}],"fns":["fn main to int"],"next":["ret <int>","r = <long>","NEW = <value>","if <bool>","while <bool>","for NAME T COUNT","trap <1..255>","exit <int>","put tot <ulong> <ulong>","put b <ulong> <uchar>"],"obligations":{"function":"fn main to int","open":["fn"],"regs_free":14,"contracts":[],"effects":["write:io"]},"compiles":55} diff --git a/seed/query/cases/stmt_effects.om b/seed/query/cases/stmt_effects.om new file mode 100644 index 000000000..52e3c784d --- /dev/null +++ b/seed/query/cases/stmt_effects.om @@ -0,0 +1,10 @@ +global tot ulong 1 +fn main to int +b = new uchar 4 65 +if true +z = ulong 5 +end +r = write 1 b 0 4 +?? +ret 0 +end diff --git a/seed/query/cases/stmt_loop.json b/seed/query/cases/stmt_loop.json new file mode 100644 index 000000000..0f65e0798 --- /dev/null +++ b/seed/query/cases/stmt_loop.json @@ -0,0 +1 @@ +{"hole":{"line":5,"kind":"statement","fn_line":2,"depth":2},"check":{"code":0,"status":"INCOMPLETE"},"scope":[{"name":"cnt","type":"ulong[]","writable":true},{"name":"a","type":"uint[]"},{"name":"n","type":"ulong"},{"name":"s","type":"ulong"},{"name":"i","type":"ulong"}],"fns":["fn sum a view uint 8 n ulong to ulong","fn main to int"],"next":["ret <ulong>","n = <ulong>","s = <ulong>","i = <ulong>","NEW = <value>","if <bool>","while <bool>","for NAME T COUNT","break","continue","trap <1..255>","exit <int>","end","put cnt <ulong> <ulong>"],"obligations":{"function":"fn sum a view uint 8 n ulong to ulong","open":["for","fn"],"regs_free":11,"contracts":[],"effects":[]},"compiles":71} diff --git a/seed/query/cases/stmt_loop.om b/seed/query/cases/stmt_loop.om new file mode 100644 index 000000000..0954362fa --- /dev/null +++ b/seed/query/cases/stmt_loop.om @@ -0,0 +1,13 @@ +global cnt ulong 4 +fn sum a view uint 8 n ulong to ulong +s = ulong 0 +for i ulong n +?? +end +ret s +end +fn main to int +b = new uint 8 1 +t = sum b 8 +ret int t +end diff --git a/seed/query/cases/stmt_om2.json b/seed/query/cases/stmt_om2.json new file mode 100644 index 000000000..db92e3b92 --- /dev/null +++ b/seed/query/cases/stmt_om2.json @@ -0,0 +1 @@ +{"hole":{"line":10,"kind":"statement","fn_line":5,"depth":2},"check":{"code":0,"status":"INCOMPLETE"},"scope":[{"name":"a","type":"uint[]"},{"name":"n","type":"ulong"},{"name":"s","type":"ulong"},{"name":"i","type":"ulong"}],"fns":["fn sq x uint to ulong","fn sum a view uint 8 n ulong to ulong","fn main to int"],"next":["ret <ulong>","n = <ulong>","s = <ulong>","i = <ulong>","NEW = <value>","if <bool>","while <bool>","for NAME T COUNT","break","continue","trap <1..255>","exit <int>","end","invariant <bool>"],"obligations":{"function":"fn sum a view uint 8 n ulong to ulong","open":["for","fn"],"regs_free":11,"contracts":["requires le n 8","invariant le i n"],"effects":[]},"compiles":66} diff --git a/seed/query/cases/stmt_om2.om b/seed/query/cases/stmt_om2.om new file mode 100644 index 000000000..9c0b8dbf4 --- /dev/null +++ b/seed/query/cases/stmt_om2.om @@ -0,0 +1,20 @@ +om2 +fn sq x uint to ulong +ret mul ulong x ulong x +end +fn sum a view uint 8 n ulong to ulong +requires le n 8 +s = ulong 0 +for i ulong n +invariant le i n +?? +end +ret s +end +fn main to int +b = new uint 8 1 +c = new uchar 2 65 +t = sum b 8 +r = write 1 c 0 2 +ret int t +end diff --git a/seed/query/cases/twoholes.json b/seed/query/cases/twoholes.json new file mode 100644 index 000000000..3aef4d5ea --- /dev/null +++ b/seed/query/cases/twoholes.json @@ -0,0 +1 @@ +{"error":"more than one ?? hole","compiles":2} diff --git a/seed/query/cases/twoholes.om b/seed/query/cases/twoholes.om new file mode 100644 index 000000000..d62bc91e6 --- /dev/null +++ b/seed/query/cases/twoholes.om @@ -0,0 +1,4 @@ +fn main to int +?? +?? +end diff --git a/seed/query/cases/value_om2.json b/seed/query/cases/value_om2.json new file mode 100644 index 000000000..7ebe5f495 --- /dev/null +++ b/seed/query/cases/value_om2.json @@ -0,0 +1 @@ +{"hole":{"line":10,"kind":"value","fn_line":5,"depth":2},"check":{"code":0,"status":"INCOMPLETE"},"scope":[{"name":"a","type":"uint[]"},{"name":"n","type":"ulong"},{"name":"s","type":"ulong"},{"name":"i","type":"ulong"}],"fns":["fn sq x uint to ulong","fn sum a view uint 8 n ulong to ulong","fn main to int"],"expect":["ulong"],"next":["n","s","i","add <ulong> <ulong>","sub <ulong> <ulong>","mul <ulong> <ulong>","div <ulong> <ulong>","rem <ulong> <ulong>","and <ulong> <ulong>","or <ulong> <ulong>","xor <ulong> <ulong>","shl <ulong> <ulong>","shr <ulong> <ulong>","ulong <any>","len a","sq uint 1","sum a ulong 1"],"obligations":{"function":"fn sum a view uint 8 n ulong to ulong","open":["for","fn"],"regs_free":11,"contracts":["requires le n 8","invariant le i n"],"effects":[]},"compiles":68} diff --git a/seed/query/cases/value_om2.om b/seed/query/cases/value_om2.om new file mode 100644 index 000000000..935a18bbf --- /dev/null +++ b/seed/query/cases/value_om2.om @@ -0,0 +1,20 @@ +om2 +fn sq x uint to ulong +ret mul ulong x ulong x +end +fn sum a view uint 8 n ulong to ulong +requires le n 8 +s = ulong 0 +for i ulong n +invariant le i n +s = add s ?? +end +ret s +end +fn main to int +b = new uint 8 1 +c = new uchar 2 65 +t = sum b 8 +r = write 1 c 0 2 +ret int t +end diff --git a/seed/query/check.sh b/seed/query/check.sh new file mode 100755 index 000000000..916e189b5 --- /dev/null +++ b/seed/query/check.sh @@ -0,0 +1,151 @@ +#!/bin/sh +# Qualification for seed/query (omq, the hole oracle over the pinned omc). +# The tool is seeded Omega; this coordinator only compiles, runs and compares. +# Exit 0 PASS, 1 FAIL, 3 NOT_RUN (wrong compiler). +set -u +cd "$(dirname "$0")/../.." +ROOT=$(pwd) +OMC=${OMC:-$ROOT/seed/out/omc} +PIN=$(sed -n 's/^OMC_PIN=//p' om/units.sh) +[ -x "$OMC" ] || { echo "NOT_RUN query: no omc at $OMC"; exit 3; } +[ "$(sha256sum < "$OMC" | cut -c1-64)" = "$PIN" ] || { echo "NOT_RUN query: omc is not the pinned compiler $PIN"; exit 3; } +B=$ROOT/build/seed/query +rm -rf "$B"; mkdir -p "$B" +C=seed/query/cases +Q=$B/omq +fails=0; checks=0 +ok() { checks=$((checks + 1)); printf 'ok %s\n' "$*"; } +bad() { checks=$((checks + 1)); fails=$((fails + 1)); printf 'FAIL %s\n' "$*"; } + +"$OMC" < seed/query/omq.om > "$Q" 2> "$Q.err" && chmod +x "$Q" || { cat "$Q.err"; echo "QUERY FAIL build"; exit 1; } +echo "omc $PIN" +echo "omq sha256 $(sha256sum < "$Q" | cut -c1-64)" + +# 1. Goldens: every case answer is byte-identical to the reviewed expected answer. +for f in $C/*.om; do + n=$(basename "$f" .om) + "$Q" "$OMC" < "$f" > "$B/$n.json" + rc=$? + want=0 + case $n in twoholes|outside) want=1 ;; esac + if [ $rc = $want ] && cmp -s "$B/$n.json" "$C/$n.json"; then ok "golden $n rc=$rc" + else bad "golden $n rc=$rc expected=$want"; diff "$C/$n.json" "$B/$n.json" | head -5; fi +done + +# 2. Negative controls: properties that a wrong oracle would break. +has() { grep -q -- "$2" "$B/$1.json"; } +if has stmt_effects '"name":"z"'; then bad "closed-block name z offered in scope"; else ok "closed-block name z absent from scope"; fi +if has stmt_effects '"break"'; then bad "break offered outside a loop"; else ok "break absent outside a loop"; fi +if has stmt_loop '"break"'; then ok "break offered inside a loop"; else bad "break missing inside a loop"; fi +if has value_om2 '"next":\[[^]]*"a"[,\]]'; then bad "uint[] name a offered for a ulong hole"; else ok "wrong-type array a excluded at a ulong hole"; fi +if has stmt_loop '"requires <bool>"'; then bad "contract word offered in an OM-1 unit"; else ok "no contract forms in an OM-1 unit"; fi +if has stmt_om2 '"invariant <bool>"'; then ok "invariant offered in an OM-2 loop"; else bad "invariant missing in an OM-2 loop"; fi +if has stmt_effects '"effects":\["write:io"\]'; then ok "effect write:io reported"; else bad "effect write:io missing"; fi +if has else_ret '"check":{"code":0,"status":"INCOMPLETE"}'; then ok "hole in an else after a returning then: base witness compiles"; else bad "else_ret base witness rejected"; fi +if has arr_call '"fill k ulong 1"' && ! has arr_call '"fill [sb] '; then ok "array argument: the length-matching array k, not s or b"; else bad "array argument choice"; fi +if has arr_call '"name":"v","type":"ulong"'; then ok "binding without = (v ulong 5) in scope"; else bad "binding without = missing from scope"; fi +sed 's/^??$/fill k ulong 1/' $C/arr_call.om | "$OMC" > /dev/null 2>&1 && ok "offered call fill k ulong 1 compiles" || bad "offered call fill k ulong 1 rejected" +sed 's/^??$/fill s ulong 1/' $C/arr_call.om | "$OMC" > /dev/null 2>&1 && bad "control: fill s (uint 8 for uint 64) accepted" || ok "control: fill s (uint 8 for uint 64) rejected by omc" +if has ret_value '"expect":\["uint"\]' && has ret_value '"next":\["n",'; then ok "value hole inside a ret line: uint expected, n offered"; else bad "value hole inside a ret line answered empty"; fi +sed 's/??/n/' $C/ret_value.om | "$OMC" > /dev/null 2>&1 && ok "offered ret int add n n compiles" || bad "offered ret value rejected" +# Outcome: three values, never a plain VALID. A hole with a compiling base is INCOMPLETE, an +# error before the hole is INVALID, a whole accepted program is VALID-UNPROVEN. +if has err3 '"status":"INVALID"' && has stmt_loop '"status":"INCOMPLETE"'; then ok "status INVALID / INCOMPLETE"; else bad "status INVALID / INCOMPLETE"; fi +printf 'fn main to int\nret int 0\nend\n' | "$Q" "$OMC" > "$B/whole.json" +grep -q '"status":"VALID-UNPROVEN"' "$B/whole.json" && ok "status VALID-UNPROVEN for an accepted whole program" || bad "status for an accepted whole program" +if grep -h -o '"status":"VALID"' "$B"/*.json | grep -q .; then bad "plain VALID emitted"; else ok "no answer claims plain VALID"; fi + +# 3. Every next form at the loop hole really compiles: substitute a concrete instance +# for each listed shape and recompile. This checks omq's claims with omc directly. +inst() { # inst FORM -> concrete statement + case "$1" in + 'ret <ulong>') echo 'ret ulong 1' ;; + 'n = <ulong>'|'s = <ulong>'|'i = <ulong>') echo "${1% = *} = ulong 1" ;; + 'NEW = <value>') echo '_n = ulong 1' ;; + 'if <bool>') printf 'if true\nend' ;; + 'while <bool>') printf 'while false\nend' ;; + 'for NAME T COUNT') printf 'for _k ulong 1\nend' ;; + 'trap <1..255>') echo 'trap 1' ;; + 'exit <int>') echo 'exit 0' ;; + 'end') printf 'end\nfor _k ulong 1' ;; + 'put cnt <ulong> <ulong>') echo 'put cnt 0 ulong 1' ;; + *) echo "$1" ;; + esac +} +forms=$(sed 's/.*"next":\[\([^]]*\)\].*/\1/' "$B/stmt_loop.json" | tr ',' '\n' | sed 's/^"//; s/"$//') +nf=0; nbad=0 +IFS=' +' +for form in $forms; do + nf=$((nf + 1)) + s=$(inst "$form") + awk -v s="$s" '$0 == "??" { print s; next } { print }' $C/stmt_loop.om > "$B/form.om" + "$OMC" < "$B/form.om" > /dev/null 2>&1 || { nbad=$((nbad + 1)); echo " form does not compile: $form"; } +done +unset IFS +if [ $nf -gt 0 ] && [ $nbad = 0 ]; then ok "all $nf next forms at the loop hole compile"; else bad "$nbad of $nf next forms do not compile"; fi +# and a form omq rejected really does not compile: else at a for-loop hole. +awk '$0 == "??" { print "else"; next } { print }' $C/stmt_loop.om > "$B/form.om" +if "$OMC" < "$B/form.om" > /dev/null 2>&1; then bad "else compiles but omq omitted it"; else ok "omitted form else is refused by omc"; fi + +# 4. Repairs: each fix, applied by hand, yields the reported rc. +sed 's/abcdefghijk/abcdefg0/g' $C/err13.om > "$B/fix.om"; "$OMC" < "$B/fix.om" > /dev/null 2>&1 +r=$?; if [ $r = 0 ] && has err13 '"rc_after_fix":0'; then ok "err13 rename repair compiles"; else bad "err13 repair rc=$r"; fi + +# 5. A compiler that is not omc is refused, never answered. +for fake in /bin/false /bin/cat /nonexistent; do + "$Q" "$fake" < $C/stmt_loop.om > "$B/fake.json" 2> /dev/null + r=$? + if [ $r = 2 ] && [ ! -s "$B/fake.json" ]; then ok "fake compiler $fake refused rc=2"; else bad "fake compiler $fake rc=$r"; fi +done + +# 6. Prefix mode (absorbed omviable): length-prefixed requests, one verdict line each. +req() { # req TEXT -> 4-byte little-endian length + bytes + n=$(printf '%s' "$1" | wc -c) + printf "$(printf '\\%03o\\%03o\\%03o\\%03o' $((n & 255)) $((n >> 8 & 255)) $((n >> 16 & 255)) $((n >> 24 & 255)))" + printf '%s' "$1" +} +H='fn main to int +' +{ + req "${H}x = add" + req "${H}x = add int 1 2 +ret x +end +" + req "${H}x = add int 1 2 +ret y" + req "${H}x = add int 1 2 +ret y " + req "${H}abcdefghij = 1" + req "${H}local arr[i324i324" + req "${H}invarian" + req "${H}note extraordinarily" + req "${H}x = 1234567890123456789012" +} | "$Q" "$OMC" prefix > "$B/prefix.txt" +# rows 6-9: an open word longer than any omc word is dead (2), except keyword prefixes and notes +printf '1 4\n0 0\n1 3\n2 3\n2 13\n2 13\n1 4\n1 15\n2 6\n' > "$B/prefix.want" +if cmp -s "$B/prefix.txt" "$B/prefix.want"; then ok "prefix verdicts 9/9 (dead open word)"; else bad "prefix verdicts"; diff "$B/prefix.want" "$B/prefix.txt"; fi + +# 7. The goldens can fail: a corrupted expected answer must not compare equal. +sed 's/"regs_free":11/"regs_free":12/' $C/stmt_loop.json > "$B/corrupt.json" +if cmp -s "$B/stmt_loop.json" "$B/corrupt.json"; then bad "corrupted golden still matches"; else ok "corrupted golden is detected"; fi + +# 8. Mutant: an omq whose statement witnesses accept any compiler verdict must be caught. +awk '/^fn tryc / { m = 1 } m && $0 == "ret run" { print "ret mul run 0"; m = 0; next } { print }' \ + seed/query/omq.om > "$B/mutant.om" +"$OMC" < "$B/mutant.om" > "$B/mutant" 2> /dev/null && chmod +x "$B/mutant" +"$B/mutant" "$OMC" < $C/stmt_effects.om > "$B/mutant.json" +if cmp -s "$B/mutant.json" $C/stmt_effects.json; then bad "mutant omq (accept every witness) not caught" +elif grep -q '"break"' "$B/mutant.json"; then ok "mutant omq caught: offers break outside a loop" +else ok "mutant omq caught: golden differs"; fi + +# 9. Host-word closure: the effect table covers every omc host keyword (code >= 100). +sed -n 16,18p seed/omc/genkw.awk | grep -oE '[a-z0-9]+:[0-9]+' \ + | awk -F: '$2 >= 100 && $1 != "const" { print $1 }' | sort > "$B/kw.txt" +grep '^text hostw' seed/query/omq.om | grep -oE '[a-z0-9]+:[a-z]+' | cut -d: -f1 \ + | grep -v -x -e read -e write -e exit | sort > "$B/hw.txt" +if cmp -s "$B/kw.txt" "$B/hw.txt"; then ok "effect table == $(wc -l < "$B/kw.txt") omc host keywords"; else bad "effect table differs from omc host keywords"; diff "$B/kw.txt" "$B/hw.txt"; fi + +if [ $fails = 0 ]; then echo "SEED QUERY PASS $checks checks"; exit 0; fi +echo "SEED QUERY FAIL $fails of $checks checks"; exit 1 diff --git a/seed/query/omq.om b/seed/query/omq.om new file mode 100644 index 000000000..a84b813d5 --- /dev/null +++ b/seed/query/omq.om @@ -0,0 +1,2794 @@ +om2 +note omq: exact hole oracle over the pinned omc. Usage: omq OMC < program > answer.json +note The program holds at most one hole, the word ??. A line that is only ?? is a statement hole; +note ?? inside a line is a value hole. Every scope entry, expected type and next form in the answer +note is a witness program the real compiler accepted: omq builds the witness, runs OMC on it and +note reports what passed. It does not reimplement any Omega rule. See seed/query/README.md. + +const GON ulong 0 +const GWN ulong 1 +const GSL ulong 2 +const GNL ulong 3 +const GHL ulong 4 +const GHA ulong 5 +const GHK ulong 6 +const GFL ulong 7 +const GD0 ulong 8 +note prefix mode word limits of omc 2c824805: a name over 8 bytes is error 13, a number over 20 +note bytes is error 6 (MEASURED by OmegaMind). longnames (#837) raises NAMEMAX to 63. +const NAMEMAX ulong 8 +const NUMMAX ulong 20 +const GD1 ulong 9 +const GEND ulong 10 +const GMI ulong 11 +const GME ulong 12 +const GDN ulong 13 +const GPN ulong 14 +const GRUN ulong 15 +const GEN ulong 16 +const GNC ulong 17 +const GLOOP ulong 18 +const GIF ulong 19 +const GOK ulong 20 +const GSEP ulong 21 +const GCB ulong 22 +const GNB ulong 23 +const GEXP ulong 24 +const GNH ulong 25 +const GOM2 ulong 26 +const GRC ulong 27 +const GDL ulong 28 +const GDC ulong 29 +const GDA ulong 30 +const GDB ulong 31 +const GSK ulong 32 +const GSA ulong 33 +const GNT ulong 34 +const GNOI ulong 35 + +global src uchar 1048576 +global lsa ulong 65536 +global lea ulong 65536 +global wit uchar 2097152 +global ob uchar 1048576 +global eb uchar 8192 +global pk uchar 4200 +global cb uchar 4096 +global g ulong 64 +global ca ulong 512 +global cl ulong 512 +global ct ulong 512 +global bk ulong 1024 +global bln ulong 1024 + +text tys "bool uchar ushort uint ulong byte short int long f32 f64 " +text bops "add sub mul div rem and or xor shl shr " +text cops "eq ne lt le gt ge " +text hostw "read:io write:io exit:io fopen:fs fclose:fs fsync:fs fdsync:fs memfd:fs fchmod:fs chmod:fs chown:fs faccess:fs fchdir:fs syncfs:fs flock:fs umask:fs fseek:fs ftrunc:fs truncate:fs unlink:fs makedir:fs rmdir:fs cwd:fs chdir:fs readlnk:fs getdnts:fs fstat:fs stat:fs lstat:fs renames:fs link:fs symlnk:fs pread:fs pwrite:fs socknew:net bind:net connect:net listen:net accept:net sockname:net sockopt:net recv:net send:net shutdown:net peercred:net sendmsg:net waitio:proc pollfds:proc fork:proc getpid:proc getppid:proc killpid:proc waitpid:proc pipefds:proc dupfd:proc execprog:proc setsid:proc getpgid:proc setpgid:proc getsid:proc getuid:proc geteuid:proc getgid:proc getegid:proc argcnt:env argread:env envcnt:env envread:env monons:clock realns:clock sleepns:clock random:random devctl:device mapnew:device mapdrop:device mapread:device mapwrite:device devcall:device uvmctl:device mapaddr:device mapget:device mapset:device maphold:device mapbind:device " +text tnl "\n" +text ttrap "trap 1\n" +text tend "end\n" +text tq "_q = " +text tdevnull "/dev/null" +text tmfd "omq" +text tusage "usage: omq OMC < program > answer.json\n" +text tbad "omq: cannot run the compiler\n" +text tfn "fn" +text texport "export" +text tif "if" +text twhile "while" +text tfor "for" +text telse "else" +text tkend "end" +text tglobal "global" +text tconst "const" +text ttext "text" +text tstruct "struct" +text teq "=" +text tto "to" +text tvoid "void" +text treq "requires" +text tens "ensures" +text tinv "invariant" +text tnote "note" +text tom2 "om2" +text tdiag "diag" +text tfmt "fmt" +text tview "view" +text tedit "edit" +text tout "out" +text tfresh "fresh" +text tinline "inline" +text ttrue "true" +text tlen "len " +text tget "get " +text tput "put " +text tdiagr "omc-diag 1 " +text r3 "unknown name: define it before this line (globals, consts and texts above their first use; locals in an enclosing block that is still open)" +text r4 "the line ends too early: an operation or call is missing an operand" +text r5 "extra words: an operation or call got more operands than it takes" +text r6 "malformed number" +text r7 "type mismatch: convert with TYPE value (checked), or use a name of the expected type; a name keeps its first type" +text r8 "number out of range for the type" +text r9 "word not allowed here (e.g. break outside a loop, more than six parameter slots, an inline cycle)" +text r10 "duplicate name: pick a new name" +text r11 "ambiguous bare number: write a typed number, e.g. uint 5" +text r12 "register pool exhausted: split the function into helpers, or end blocks so names die" +text r13 "name longer than 8 characters: shorten it" +text r14 "capacity exceeded" +text r15 "missing end: close every fn/if/while/for with end" +text r16 "syntax: unknown word or wrong form" +text r17 "line after ret/break/continue/trap/exit in the same block: delete it or move it above" +text r18 "missing ret: the function's end is reachable; add ret VALUE or trap N before end" +text r19 "a global passed as an argument, or one array twice in a call: name the global inside the callee" +text r20 "write to a view or text: make the parameter edit" +text r21 "constant index out of range" +text r22 "bad text literal" +text r27 "uninitialised fresh element read" +text r30 "contract syntax or a forbidden predicate operation" +text kwhy "why" +text kafter "rc_after_fix" +text kfixr "fix_rename" +text kfixm "fix_move_up" +text kfixe "fix_append_end" +text kcode "code" +text kstat "status" +text sinval "INVALID" +text sincom "INCOMPLETE" +text svalun "VALID-UNPROVEN" +text sunk "UNKNOWN" +text kline "line" +text kcol "col" +text kspan "span" +text kexpect "expect" +text kfound "found" +text kobl "obligation" +text ksev "sev" +text teqsp " = " +text tifb "if true" +text twhb "while false" +text tforb "for _q ulong 1" +text tbrk "break" +text tcont "continue" +text tifo "if <bool>" +text twho "while <bool>" +text tforo "for NAME T COUNT" +text ttrapo "trap <1..255>" +text texito "exit <int>" +text texit0 "exit 0" +text tretw "ret" +text texitw "exit" +text tputw "put" +text ttrapw "trap" +text treqt "requires true" +text tenst "ensures true" +text tinvt "invariant true" +text treqo "requires <bool>" +text tenso "ensures <bool>" +text tinvo "invariant <bool>" +text jhole "hole" +text jline "line" +text jkind "kind" +text jstmt "statement" +text jvalue "value" +text jfnl "fn_line" +text jdepth "depth" +text jscope "scope" +text jname "name" +text jtype "type" +text jwr "writable" +text jfns "fns" +text jexp "expect" +text jobl "obligations" +text jfunc "function" +text jopen "open" +text jregs "regs_free" +text jcon "contracts" +text jeff "effects" +text jcheck "check" +text jnext "next" +text jruns "compiles" +text jerr "error" +text jmany "more than one ?? hole" +text jout "the hole is not inside a function" +text jwhile "while" +text tgood "fn main to int\nret 0\nend\n" +text tbadp "fn main to int\nret 0\n" +text tomcl "omc: line " +text tprefix "prefix" +text thdr "om2 diag\n" +global lb uchar 4 + +note ---------------------------------------------------------------- output buffer (JSON) + +fn ob1 b uchar to void +n = get g GON +if lt n 1048000 +put ob n b +put g GON add n 1 +end +end + +fn obt t view uchar * to void +for i ulong len t +ob1 get t i +end +end + +fn obe b uchar to void +if or eq b 34 eq b 92 +ob1 92 +ob1 b +ret +end +if eq b 9 +ob1 92 +ob1 116 +ret +end +if lt b 32 +ob1 32 +ret +end +ob1 b +end + +fn obs a ulong n ulong to void +for i ulong n +obe get src add a i +end +end + +fn obcb to void +for i ulong get g GCB +obe get cb i +end +end + +fn obn v ulong to void +d = new uchar 24 0 +k = ulong 0 +x = v +while true +put d k uchar add ulong 48 rem x 10 +k = add k 1 +x = div x 10 +if eq x 0 +break +end +end +while gt k 0 +k = sub k 1 +ob1 get d k +end +end + +fn obsep to void +if ne get g GSEP 0 +ob1 44 +end +put g GSEP 1 +end + +fn obkey t view uchar * to void +obsep +ob1 34 +obt t +ob1 34 +ob1 58 +put g GSEP 0 +end + +fn obstr a ulong n ulong to void +ob1 34 +obs a n +ob1 34 +put g GSEP 1 +end + +fn obtstr t view uchar * to void +ob1 34 +obt t +ob1 34 +put g GSEP 1 +end + +fn obnum v ulong to void +obn v +put g GSEP 1 +end + +fn obopen b uchar to void +ob1 b +put g GSEP 0 +end + +fn obclose b uchar to void +ob1 b +put g GSEP 1 +end + +note ---------------------------------------------------------------- source words and lines + +fn isb c uchar to bool +ret or eq c 32 eq c 9 +end + +fn skb p ulong to ulong +q = p +while and lt q get g GSL isb get src q +q = add q 1 +end +ret q +end + +fn wde p ulong to ulong +q = p +n = get g GSL +while lt q n +c = get src q +if or isb c eq c 10 +ret q +end +q = add q 1 +end +ret q +end + +fn sweq a ulong n ulong t view uchar * to bool +if ne n len t +ret false +end +for i ulong n +if ne get src add a i get t i +ret false +end +end +ret true +end + +fn lnw i ulong k ulong to ulong +note start offset of word k (0-based) of line i, or the line end when it has fewer words +p = skb get lsa i +e = get lea i +for j ulong k +if ge p e +ret e +end +p = skb wde p +end +if gt p e +ret e +end +ret p +end + +fn lwis i ulong k ulong t view uchar * to bool +a = lnw i k +if ge a get lea i +ret false +end +ret sweq a sub wde a a t +end + +fn isfn i ulong to bool +note a function line: fn NAME ..., or export fn NAME ... (a variable named export is not one) +if lwis i 0 tfn +ret true +end +ret and lwis i 0 texport lwis i 1 tfn +end + +fn opener i ulong to bool +if or lwis i 0 tif or lwis i 0 twhile lwis i 0 tfor +ret true +end +ret isfn i +end + +note ---------------------------------------------------------------- witness buffer + +fn w1 b uchar to void +n = get g GWN +if lt n 2097000 +put wit n b +put g GWN add n 1 +end +end + +fn wt t view uchar * to void +for i ulong len t +w1 get t i +end +end + +fn wsr a ulong n ulong to void +for i ulong n +w1 get src add a i +end +end + +fn wln i ulong to void +a = get lsa i +e = get lea i +if and ne get g GNOI 0 eq i get g GFL +e = noinl a e +end +wsr a sub e a +w1 10 +end + +fn noinl a ulong e ulong to ulong +note the fn line without its last word when that word is inline +q = e +while gt q a +if not isb get src sub q 1 +break +end +q = sub q 1 +end +s = q +while gt s a +if isb get src sub s 1 +break +end +s = sub s 1 +end +if sweq s sub q s tinline +ret s +end +ret e +end + +fn wlns i ulong j ulong to void +k = i +while lt k j +wln k +k = add k 1 +end +end + +fn wcb to void +for i ulong get g GCB +w1 get cb i +end +end + +fn wcl d ulong ended bool to void +note close d open blocks, a trap before each end; with GNT set only the innermost gets one, for +note a hole whose outer block already terminates (an else after a then branch that returns) +for k ulong d +if not and eq k 0 ended +if or eq k 0 eq get g GNT 0 +wt ttrap +end +end +wt tend +end +end + +fn clmode to void +note choose the closers: when the base witness says line after ret (17), drop the outer traps +put g GNT 0 +put g GWN 0 +wbody +if eq run 17 +put g GNT 1 +end +end + +fn wrest to void +wlns add get g GEND 1 get g GNL +end + +fn wholev to void +note the hole line with ?? replaced by the scratch text +i = get g GHL +a = get lsa i +h = get g GHA +wsr a sub h a +wcb +e = add h 2 +wsr e sub get lea i e +w1 10 +end + +note ---------------------------------------------------------------- scratch text + +fn cbc to void +put g GCB 0 +end + +fn cb1 b uchar to void +n = get g GCB +if lt n 4000 +put cb n b +put g GCB add n 1 +end +end + +fn cbt t view uchar * to void +for i ulong len t +cb1 get t i +end +end + +fn cbs a ulong n ulong to void +for i ulong n +cb1 get src add a i +end +end + +fn cbn v ulong to void +if ge v 10 +cbn div v 10 +end +cb1 uchar add ulong 48 rem v 10 +end + +fn cbtw t view uchar * a ulong to ulong +note copy the word of t that starts at a; return the offset after its blank +p = a +while and lt p len t ne get t p 32 +cb1 get t p +p = add p 1 +end +ret add p 1 +end + +fn tyat k ulong to ulong +note start offset in tys of type k (1-based) +p = ulong 0 +j = ulong 1 +while lt j k +while ne get tys p 32 +p = add p 1 +end +p = add p 1 +j = add j 1 +end +ret p +end + +fn cbty k ulong to void +r = cbtw tys tyat k +end + +fn cbz k ulong to void +note a literal of type k: true for bool, else TYPE 1 (1.0 for floats) +if eq k 1 +cbt ttrue +ret +end +cbty k +cb1 32 +cb1 49 +if ge k 10 +cb1 46 +cb1 48 +end +end + +fn cbname c ulong to void +cbs get ca c get cl c +end + +fn obty k ulong to void +p = tyat k +while ne get tys p 32 +ob1 get tys p +p = add p 1 +end +end + +note ---------------------------------------------------------------- running the compiler + +fn wflush fd int to bool +p = ulong 0 +n = get g GWN +while lt p n +w = pwrite fd wit p sub n p long p +if le w 0 +ret false +end +p = add p ulong w +end +ret true +end + +fn run to ulong +note compile the witness buffer; return the exit code (256 + signal when killed) +fi = int get g GMI +fe = int get g GME +r = ftrunc fi 0 +if not wflush fi +ret 255 +end +r = fseek fi 0 0 +r = ftrunc fe 0 +r = fseek fe 0 0 +put g GRUN add get g GRUN 1 +pid = fork +if eq pid 0 +r = dupfd fi 0 0 +r = dupfd int get g GDN 1 0 +r = dupfd fe 2 0 +r = execprog pk 0 get g GPN +exit 127 +end +ret reap pid +end + +fn reap pid long to ulong +ws = new uchar 4 0 +if lt pid 0 +ret 255 +end +r = waitpid int pid ws 0 4 0 +k = pread int get g GME eb 0 8000 0 +if lt k 0 +k = 0 +end +put g GEN ulong k +if ne and get ws 0 127 0 +ret add ulong 256 ulong and get ws 0 127 +end +ret ulong get ws 1 +end + +note ---------------------------------------------------------------- witnesses at the hole + +fn trys ended bool to bool +ret eq tryc ended 0 +end + +fn tryc ended bool to ulong +note statement witness: prefix, scratch lines, closers, rest; the compiler's exit code +put g GWN 0 +wlns 0 get g GHL +wcb +w1 10 +wcl get g GD0 ended +wrest +ret run +end + +fn tryv to bool +note value witness: the hole line with ?? replaced by the scratch text; a hole line that is a ret or +note exit already ends its block, so the innermost closer gets no trap (else omc error 17) +put g GWN 0 +wlns 0 get g GHL +wholev +h = get g GHL +wcl get g GD1 or lwis h 0 tretw lwis h 0 texitw +wrest +ret eq run 0 +end + +fn tryblk ended bool to bool +note a block opener in scratch: the witness closes it with its own end +put g GWN 0 +wlns 0 get g GHL +wcb +w1 10 +if not ended +wt ttrap +end +wt tend +wcl get g GD0 false +wrest +ret eq run 0 +end + +fn tryend to bool +put g GWN 0 +wlns 0 get g GHL +wt tend +d = get g GD0 +if gt d 1 +wcl sub d 1 false +end +wrest +ret eq run 0 +end + +fn tryelse to bool +put g GWN 0 +wlns 0 get g GHL +cbc +cbt telse +wcb +w1 10 +wcl get g GD0 false +wrest +ret eq run 0 +end + +note ---------------------------------------------------------------- input and the hole + +fn rdin to void +n = ulong 0 +while lt n 1048000 +k = read 0 src n sub ulong 1048000 n +if le k 0 +put g GSL n +ret +end +n = add n ulong k +end +put g GSL n +end + +fn mklines to void +n = get g GSL +k = ulong 0 +s = ulong 0 +for p ulong n +if and eq get src p 10 lt k 65535 +put lsa k s +put lea k p +k = add k 1 +s = add p 1 +end +end +if and lt s n lt k 65535 +put lsa k s +put lea k n +k = add k 1 +end +put g GNL k +end + +fn isqq p ulong q ulong to bool +if ne sub q p 2 +ret false +end +ret and eq get src p 63 eq get src add p 1 63 +end + +fn fhole to ulong +note find ?? words; return their count; the first is the hole +c = ulong 0 +for i ulong get g GNL +p = skb get lsa i +e = get lea i +while lt p e +q = wde p +if isqq p q +if eq c 0 +put g GHL i +put g GHA p +put g GHK 2 +if eq skb q e +if eq p skb get lsa i +put g GHK 1 +end +end +end +c = add c 1 +end +p = skb q +end +end +ret c +end + +fn ctx to bool +note enclosing function, open blocks before the hole, the line that closes the function +h = get g GHL +f = h +while true +if isfn f +break +end +if eq f 0 +ret false +end +f = sub f 1 +end +put g GFL f +put bk 0 1 +put bln 0 f +d = ulong 1 +i = add f 1 +while lt i h +d = ctxl i d +i = add i 1 +end +put g GD0 d +put g GD1 d +if and eq get g GHK 2 opener h +put g GD1 add d 1 +end +ctxflag d +ret ctxend +end + +fn ctxl i ulong d ulong to ulong +note one line of the block scan; return the new depth +if lwis i 0 tkend +if gt d 0 +ret sub d 1 +end +ret 0 +end +if lwis i 0 telse +if gt d 0 +put bk sub d 1 3 +end +ret d +end +if and opener i lt d 1000 +k = ulong 4 +if lwis i 0 tif +k = 2 +end +if lwis i 0 twhile +k = 5 +end +put bk d k +put bln d i +ret add d 1 +end +ret d +end + +fn ctxflag d ulong to void +put g GLOOP 0 +put g GIF 0 +for k ulong d +if ge get bk k 4 +put g GLOOP 1 +end +end +if eq get bk sub d 1 2 +put g GIF 1 +end +end + +fn ctxend to bool +d = get g GD1 +i = add get g GHL 1 +n = get g GNL +while lt i n +if lwis i 0 tkend +d = sub d 1 +if eq d 0 +put g GEND i +ret true +end +else +if and opener i ne d 0 +d = add d 1 +end +end +i = add i 1 +end +put g GEND n +ret true +end + +note ---------------------------------------------------------------- candidate names + +fn addc a ulong n ulong to void +if or eq n 0 gt n 8 +ret +end +c = get src a +if not or eq c 95 or and ge c 97 le c 122 and ge c 65 le c 90 +ret +end +k = get g GNC +for j ulong k +if eq get cl j n +if seqs get ca j a n +ret +end +end +end +if lt k 500 +put ca k a +put cl k n +put ct k 0 +put g GNC add k 1 +end +end + +fn seqs a ulong b ulong n ulong to bool +for i ulong n +if ne get src add a i get src add b i +ret false +end +end +ret true +end + +fn gtop to void +note top-level global, const and text names +d = ulong 0 +for i ulong get g GNL +if eq d 0 +if or lwis i 0 tglobal or lwis i 0 tconst lwis i 0 ttext +a = lnw i 1 +addc a sub wde a a +end +end +d = dstep i d +end +end + +fn dstep i ulong d ulong to ulong +note block depth after line i, without touching the block stack +if lwis i 0 tkend +if gt d 0 +ret sub d 1 +end +ret 0 +end +if opener i +ret add d 1 +end +ret d +end + +fn gfn to void +note parameter names of the enclosing fn line (words before `to`) +f = get g GFL +k = ulong 2 +if lwis f 0 texport +k = 3 +end +while true +a = lnw f k +if ge a get lea f +ret +end +if lwis f k tto +ret +end +addc a sub wde a a +k = add k 1 +end +end + +fn gloc to void +note names bound before the hole in the enclosing function +i = add get g GFL 1 +while lt i get g GHL +if or or lwis i 1 teq nmtyped i barenv i +a = lnw i 0 +addc a sub wde a a +end +if lwis i 0 tfor +a = lnw i 1 +addc a sub wde a a +end +i = add i 1 +end +end + +fn nmtyped i ulong to bool +note NAME TYPE VALUE: the binding without = that omc accepts at the pin; the probes decide +if le get lea i lnw i 2 +ret false +end +t = tyof lnw i 1 +ret and gt t 0 lt t 12 +end + +fn barenv i ulong to bool +note NAME VALUE: a first binding without = or a type word, which omc accepts at the pin (pu s); the +note probes decide, so this only skips lines whose first word is a block, clause or statement word +if le get lea i lnw i 1 +ret false +end +if opener i +ret false +end +if or or lwis i 0 tnote lwis i 0 tkend lwis i 0 telse +ret false +end +if or or lwis i 0 tretw lwis i 0 texitw or lwis i 0 tputw lwis i 0 ttrapw +ret false +end +ret not or or lwis i 0 treq lwis i 0 tens lwis i 0 tinv +end + +note ---------------------------------------------------------------- probes + +fn pscal c ulong to void +note ct: bit 6 visible, low 4 bits the scalar type, bit 4 array, bit 5 writable array +cbc +cbt tq +cbname c +if trys false +for k ulong 11 +cbc +cbt tq +cb1 101 +cb1 113 +cb1 32 +cbname c +cb1 32 +cbz add k 1 +if trys false +put ct c or ulong 64 add k 1 +ret +end +end +put ct c 64 +ret +end +parr c +end + +fn parr c ulong to void +cbc +cbt tq +cbt tlen +cbname c +if not trys false +pstru c +ret +end +for k ulong 11 +cbc +cbt tq +cb1 101 +cb1 113 +cb1 32 +cbt tget +cbname c +cb1 32 +cb1 48 +cb1 32 +cbz add k 1 +if trys false +put ct c or ulong 80 add k 1 +pwrit c +ret +end +end +pfresh c +end + +fn pfresh c ulong to void +note an array whose elements are not written yet (fresh): get before a put is omc error 27, so write +note element 0 first and then read it back at each type +for k ulong 11 +cbc +cbt tput +cbname c +cb1 32 +cb1 48 +cb1 32 +cbz add k 1 +cb1 10 +cbt tq +cb1 101 +cb1 113 +cb1 32 +cbt tget +cbname c +cb1 32 +cb1 48 +cb1 32 +cbz add k 1 +if trys false +put ct c or ulong 112 add k 1 +ret +end +end +put ct c 80 +end + +fn pstru c ulong to void +note a struct value (ct bit 7): get c F, or get c F 0 for an array field, F the first field of a +note struct declared in the unit +for i ulong get g GNL +if lwis i 0 tstruct +a = lnw i 2 +if lt a get lea i +cbc +cbt tq +cbt tget +cbname c +cb1 32 +cbs a sub wde a a +if trys false +put ct c 192 +ret +end +cb1 32 +cb1 48 +if trys false +put ct c 192 +ret +end +end +end +end +end + +fn pwrit c ulong to void +cbc +cbt tput +cbname c +cb1 32 +cb1 48 +cb1 32 +cbt tget +cbname c +cb1 32 +cb1 48 +if trys false +put ct c or get ct c 32 +end +end + +note ---------------------------------------------------------------- answer sections + +fn ansctx to void +obkey jhole +obopen 123 +okv jline get g GHL +obkey jkind +if eq get g GHK 1 +obtstr jstmt +else +obtstr jvalue +end +okv jfnl get g GFL +obkey jdepth +obnum get g GD0 +obclose 125 +end + +fn okv t view uchar * v ulong to void +obkey t +obnum add v 1 +end + +fn ansscope to void +obkey jscope +obopen 91 +for c ulong get g GNC +v = get ct c +if ne and v 64 0 +obsep +obopen 123 +obkey jname +obstr get ca c get cl c +obkey jtype +ob1 34 +if ne and v 15 0 +obty and v 15 +end +if ne and v 16 0 +ob1 91 +ob1 93 +end +if ne and v 128 0 +obt tstruct +end +ob1 34 +put g GSEP 1 +if ne and v 32 0 +obkey jwr +obt ttrue +put g GSEP 1 +end +obclose 125 +end +end +obclose 93 +end + +fn ansfns to void +obkey jfns +obopen 91 +for i ulong get g GNL +if isfn i +a = lnw i 0 +obsep +obstr a sub get lea i a +end +end +obclose 93 +end + +fn emitf to void +note output the scratch text as one checked next form +obsep +obopen 34 +obcb +obclose 34 +end + +fn nstmt to void +note statement hole: every form below is listed only if its witness compiled +cbc +cb1 114 +cb1 101 +cb1 116 +if trys true +cbc +cbt tnote +cbc +cb1 114 +cb1 101 +cb1 116 +emitf +end +for k ulong 11 +cbc +cb1 114 +cb1 101 +cb1 116 +cb1 32 +cbz add k 1 +if trys true +cbc +cbt tglobal +cbc +cbt tnote +cbc +cb1 114 +cb1 101 +cb1 116 +cb1 32 +cb1 60 +cbty add k 1 +cb1 62 +emitf +end +end +nassign +nblocks +nputs +ncalls +end + +fn nassign to void +for c ulong get g GNC +v = get ct c +if and ne and v 64 0 eq and v 16 0 +if ne and v 15 0 +cbc +cbname c +cb1 32 +cb1 61 +cb1 32 +cbname c +if trys false +cbc +cbname c +cb1 32 +cb1 61 +cb1 32 +cb1 60 +cbty and v 15 +cb1 62 +emitf +end +end +end +end +cbc +cbt tq +cbt tq +cb1 48 +nnew +end + +fn nnew to void +cbc +cbt tq +cbz 5 +if trys false +cbc +cbt tnote +cbc +cbt tvoid +cbc +cb1 78 +cb1 69 +cb1 87 +cbt teqsp +cb1 60 +cb1 118 +cb1 97 +cb1 108 +cb1 117 +cb1 101 +cb1 62 +emitf +end +end + + +fn nblocks to void +cbc +cbt tifb +if tryblk false +cbc +cbt tifo +emitf +end +cbc +cbt twhb +if tryblk false +cbc +cbt twho +emitf +end +cbc +cbt tforb +if tryblk false +cbc +cbt tforo +emitf +end +nends +end + +fn nends to void +cbc +cbt tbrk +if trys true +emitf +end +cbc +cbt tcont +if trys true +emitf +end +cbc +cbt ttrap +put g GCB sub get g GCB 1 +if trys true +cbc +cbt ttrapo +emitf +end +cbc +cbt texit0 +if trys true +cbc +cbt texito +emitf +end +if tryend +cbc +cbt tkend +emitf +end +if eq get g GIF 1 +cbc +if tryelse +cbc +cbt telse +emitf +end +end +ncontr +end + +fn firstln to ulong +note first line that is not blank or a note +i = ulong 0 +while and lt i get g GNL or eq skb get lsa i get lea i lwis i 0 tnote +i = add i 1 +end +ret i +end + +fn ncontr to void +note contracts are OM-2 words; in an OM-1 unit `requires true` would bind a name instead +if eq get g GOM2 0 +ret +end +cbc +cbt treqt +if trys false +cbc +cbt treqo +emitf +end +cbc +cbt tenst +if trys false +cbc +cbt tenso +emitf +end +cbc +cbt tinvt +if trys false +cbc +cbt tinvo +emitf +end +end + +fn nputs to void +for c ulong get g GNC +v = get ct c +if eq and v 96 96 +cbc +cbt tput +cbname c +cb1 32 +cb1 60 +cb1 117 +cb1 108 +cb1 111 +cb1 110 +cb1 103 +cb1 62 +cb1 32 +cb1 60 +cbty and v 15 +cb1 62 +emitf +end +end +end + +note ---------------------------------------------------------------- calls + +fn fnret i ulong to ulong +note type index of the return of fn line i: 0 unknown or non-scalar, 12 void +k = ulong 1 +while true +a = lnw i k +if ge a get lea i +ret 0 +end +if lwis i k tto +ret tyof lnw i add k 1 +end +k = add k 1 +end +ret 0 +end + +fn tyof a ulong to ulong +e = wde a +if sweq a sub e a tvoid +ret 12 +end +for k ulong 11 +p = tyat add k 1 +q = p +while ne get tys q 32 +q = add q 1 +end +if tyeq a sub e a p sub q p +ret add k 1 +end +end +ret 0 +end + +fn tyeq a ulong n ulong p ulong m ulong to bool +if ne n m +ret false +end +for i ulong n +if ne get src add a i get tys add p i +ret false +end +end +ret true +end + +fn cbcall i ulong to bool +note scratch: the call of fn line i, a typed literal per scalar parameter and the first visible +note array of the element type per array parameter; false when no argument fits a parameter +k = ulong 2 +if lwis i 0 texport +k = 3 +end +a = lnw i sub k 1 +cbc +cbs a sub wde a a +while true +a = lnw i add k 1 +if ge a get lea i +ret false +end +if lwis i k tto +ret true +end +m = cbarg i k +if eq m 0 +ret false +end +k = add k m +end +ret false +end + +fn ismode i ulong k ulong to bool +ret or or lwis i k tview lwis i k tout or lwis i k tedit lwis i k tfresh +end + +fn cbarg i ulong k ulong to ulong +note append the argument for the parameter at word k of fn line i; return the words the +note parameter spans (name TYPE, name TYPE LEN, name MODE TYPE LEN), 0 when nothing fits +m = ulong 0 +if ismode i add k 1 +m = 1 +end +t = tyof lnw i add add k 1 m +if or eq t 0 eq t 12 +ret 0 +end +n = lnw i add add k 2 m +arr = ne m 0 +if and lt n get lea i not lwis i add add k 2 m tto +if isarrw n +arr = true +end +end +cb1 32 +if not arr +cbz t +ret 2 +end +if not cbarr t +ret 0 +end +ret add 3 m +end + +fn isarrw n ulong to bool +note the word after a parameter's type is a length (digits) or * +c = get src n +ret or eq c 42 and ge c 48 le c 57 +end + +fn cbarr t ulong to bool +note the GSK-th visible array of element type t (each array parameter takes the same rank) +put g GSA 1 +s = get g GSK +for c ulong get g GNC +v = get ct c +if and eq and v 80 80 eq and v 15 t +if eq s 0 +cbname c +ret true +end +s = sub s 1 +end +end +ret false +end + +fn callrk to bool +note step to the next array rank; false when the call has no array parameter or ranks are spent +if eq get g GSA 0 +ret false +end +put g GSA 0 +put g GSK add get g GSK 1 +ret lt get g GSK 4 +end + +fn ncalls to void +for i ulong get g GNL +if isfn i +if eq fnret i 12 +put g GSK 0 +put g GSA 0 +while true +if cbcall i +if trys false +emitf +break +end +end +if not callrk +break +end +end +end +end +end +end + +note ---------------------------------------------------------------- value holes + +fn vexpect to void +note expected types: the scalar literals and array names the hole position accepts +m = ulong 0 +obkey jexp +obopen 91 +for k ulong 11 +cbc +cbz add k 1 +if tryv +m = or m shl ulong 1 k +obsep +obopen 34 +obty add k 1 +obclose 34 +end +end +for c ulong get g GNC +if ne and get ct c 16 0 +cbc +cbname c +if tryv +obsep +obopen 34 +cbc +cbname c +obcb +obclose 34 +end +end +end +obclose 93 +put g GEXP m +end + +fn vnext to void +m = get g GEXP +for c ulong get g GNC +v = get ct c +if and ne and v 64 0 eq and v 16 0 +if ne and m shl ulong 1 sub and v 15 1 0 +cbc +cbname c +if tryv +emitf +end +end +end +end +for k ulong 11 +if ne and m shl ulong 1 k 0 +vops add k 1 +end +end +vcalls +end + +fn vops t ulong to void +note operations with result type t +if eq t 1 +vcmp +ret +end +p = ulong 0 +while lt p len bops +cbc +q = cbtw bops p +cb1 32 +cbz t +cb1 32 +cbz t +if tryv +cbc +r = cbtw bops p +cbtyh t +cbtyh t +emitf +end +p = q +end +vconv t +end + +fn cbtyh t ulong to void +cb1 32 +cb1 60 +cbty t +cb1 62 +end + +fn vcmp to void +p = ulong 0 +while lt p len cops +cbc +q = cbtw cops p +cb1 32 +cbz 4 +cb1 32 +cbz 4 +if tryv +cbc +r = cbtw cops p +cb1 32 +cb1 60 +cb1 84 +cb1 62 +cb1 32 +cb1 60 +cb1 84 +cb1 62 +emitf +end +p = q +end +end + +fn vconv t ulong to void +note a checked conversion from another type +cbc +cbty t +cb1 32 +cbz 5 +if eq t 5 +cbc +cbty t +cb1 32 +cbz 4 +end +if tryv +cbc +cbty t +cb1 32 +cb1 60 +cb1 97 +cb1 110 +cb1 121 +cb1 62 +emitf +end +vgets t +end + +fn vgets t ulong to void +for c ulong get g GNC +v = get ct c +if eq v or ulong 80 or and v 32 t +cbc +cbt tget +cbname c +cb1 32 +cb1 48 +if tryv +cbc +cbt tget +cbname c +cbtyh 5 +emitf +end +end +if and eq t 5 ne and v 16 0 +cbc +cbt tlen +cbname c +if tryv +emitf +end +end +end +end + +fn vcalls to void +for i ulong get g GNL +if isfn i +r = fnret i +if and gt r 0 lt r 12 +if ne and get g GEXP shl ulong 1 sub r 1 0 +put g GSK 0 +put g GSA 0 +while true +if cbcall i +if tryv +emitf +break +end +end +if not callrk +break +end +end +end +end +end +end +end + +note ---------------------------------------------------------------- obligations + +fn regs to ulong +note how many more ulong names fit at the hole before the register pool is exhausted +note an inline function is probed as a plain one: its body's registers, not a call site's expansion +put g GNOI 1 +lo = ulong 0 +hi = ulong 17 +while lt add lo 1 hi +mid = div add lo hi 2 +cbc +for j ulong mid +cb1 95 +cb1 113 +cbn j +cbt teqsp +cbz 5 +cb1 10 +end +put g GCB sub get g GCB 1 +note error 14 (capacity, e.g. unroll fuel) is not the register pool: only error 12 bounds the count +r = tryc false +if or eq r 0 eq r 14 +lo = mid +else +hi = mid +end +end +put g GNOI 0 +ret lo +end + +fn ansobl to void +obkey jobl +obopen 123 +obkey jfunc +f = get g GFL +a = lnw f 0 +obsep +obstr a sub get lea f a +obkey jopen +obopen 91 +d = get g GD0 +while gt d 0 +d = sub d 1 +obsep +obopen 34 +k = get bk d +if eq k 1 +obt tfn +end +if eq k 2 +obt tif +end +if eq k 3 +obt telse +end +if eq k 4 +obt tfor +end +if eq k 5 +obt twhile +end +obclose 34 +end +obclose 93 +obkey jregs +obnum regs +anscon +anseff +obclose 125 +end + +fn anscon to void +note contract clauses in force: the function prefix and the invariants of open loops +obkey jcon +obopen 91 +i = add get g GFL 1 +while lt i get g GHL +if or lwis i 0 treq lwis i 0 tens +obcl i +end +if lwis i 0 tinv +if invopen i +obcl i +end +end +i = add i 1 +end +obclose 93 +end + +fn obcl i ulong to void +a = lnw i 0 +obsep +obstr a sub get lea i a +end + +fn invopen i ulong to bool +note is the loop that owns invariant line i still open at the hole +o = i +while gt o 0 +o = sub o 1 +if not or lwis o 0 tinv lwis o 0 tnote +break +end +end +for k ulong get g GD0 +if eq get bln k o +ret true +end +end +ret false +end + +fn anseff to void +note host operations used by the enclosing function, with their effect class +obkey jeff +obopen 91 +i = get g GFL +while lt i get g GEND +p = skb get lsa i +while lt p get lea i +q = wde p +effw p sub q p +p = skb q +end +i = add i 1 +end +obclose 93 +end + +fn effw a ulong n ulong to void +p = ulong 0 +m = len hostw +while lt p m +q = p +while ne get hostw q 58 +q = add q 1 +end +if hweq a n p sub q p +e = q +while ne get hostw e 32 +e = add e 1 +end +obsep +ob1 34 +obs a n +ob1 58 +r = add q 1 +while lt r e +ob1 get hostw r +r = add r 1 +end +obclose 34 +ret +end +while ne get hostw q 32 +q = add q 1 +end +p = add q 1 +end +end + +fn hweq a ulong n ulong p ulong m ulong to bool +if ne n m +ret false +end +for i ulong n +if ne get src add a i get hostw add p i +ret false +end +end +ret true +end + +note ---------------------------------------------------------------- check and repair + +fn wdiag to ulong +note the program (hole removed) with an om2 diag line; return the byte shift it adds +put g GWN 0 +i = ulong 0 +while and lt i get g GNL or eq skb get lsa i get lea i lwis i 0 tnote +i = add i 1 +end +if lwis i 0 tom2 +if lwis i 1 tdiag +wbody +ret 0 +end +if lwis i 1 tfmt +wbody +ret 0 +end +wlns 0 i +cbc +cbt tom2 +cb1 32 +cbt tdiag +a = lnw i 1 +if lt a get lea i +cb1 32 +cbs a sub get lea i a +end +wcb +w1 10 +wbodyf add i 1 +ret 0 +end +cbc +cbt tom2 +cb1 32 +cbt tdiag +wcb +w1 10 +wbody +ret 9 +end + +fn wbody to void +wbodyf 0 +end + +fn wbodyf i ulong to void +note lines from i, with the hole closed off as in the base witness +if eq get g GHK 0 +wlns i get g GNL +ret +end +wlns i get g GHL +wcl get g GD0 false +wrest +end + +fn dfield k view uchar * to ulong +note offset in eb of the value of field k= on the omc-diag line, or 8192 when absent +n = get g GEN +p = ulong 0 +while lt add p len tdiagr n +if ebeq p tdiagr +break +end +p = add p 1 +end +while lt add p len k n +if ebeq p k +if eq get eb add p len k 61 +ret add add p len k 1 +end +end +p = add p 1 +end +ret 8192 +end + +fn ebeq p ulong t view uchar * to bool +for i ulong len t +if ne get eb add p i get t i +ret false +end +end +ret true +end + +fn ebnum p ulong to ulong +v = ulong 0 +q = p +while lt q get g GEN +c = get eb q +if or lt c 48 gt c 57 +ret v +end +v = add mul v 10 ulong sub c 48 +q = add q 1 +end +ret v +end + + +fn ebword k view uchar * to void +p = dfield k +if ge p 8192 +obtstr tvoid +ret +end +ob1 34 +while lt p get g GEN +c = get eb p +if or eq c 32 eq c 10 +break +end +ob1 c +p = add p 1 +end +ob1 34 +put g GSEP 1 +end + +fn hexv c uchar to ulong +if ge c 97 +ret ulong sub c 87 +end +ret ulong sub c 48 +end + +fn ebfound to void +note found= is hex; decode it to text, keeping the found bytes in scratch +cbc +p = dfield kfound +if lt p 8192 +while lt add p 1 get g GEN +c = get eb p +if or eq c 32 or eq c 10 eq c 45 +break +end +cb1 uchar or shl hexv c 4 hexv get eb add p 1 +p = add p 2 +end +end +ob1 34 +obcb +ob1 34 +put g GSEP 1 +end + +fn anscheck to void +obkey jcheck +obopen 123 +put g GWN 0 +wbody +rc = run +put g GRC rc +put g GOK 0 +if eq rc 0 +put g GOK 1 +end +obkey kcode +obnum rc +if ne rc 0 +put g GDL ebline +okn kline get g GDL +sh = wdiag +rd = run +if eq rd rc +diagrec sh +end +end +ostat rc +obclose 125 +end + +fn ostat rc ulong to void +note the outcome. No hole: VALID-UNPROVEN when omc accepts (nothing here proves behaviour), else +note INVALID. A hole: INCOMPLETE when the base witness compiles (a valid completion exists), +note INVALID when the error sits before the hole (no text at the hole can reach back), else +note UNKNOWN (the error may depend on what the hole holds). Never a plain VALID. +obkey kstat +if eq rc 0 +if eq get g GHK 0 +obtstr svalun +else +obtstr sincom +end +ret +end +if or eq get g GHK 0 le get g GDL get g GHL +obtstr sinval +ret +end +obtstr sunk +end + +fn okn t view uchar * v ulong to void +obkey t +obnum v +end + +fn ebline to ulong +note line N of the human record omc: line N: error E +p = ulong 0 +n = get g GEN +while lt p n +c = get eb p +if and ge c 48 le c 57 +ret ebnum p +end +p = add p 1 +end +ret 0 +end + +fn diagrec sh ulong to void +obkey ksev +ebword ksev +obkey kcol +obnum ebnum dfield kcol +obkey kexpect +ebword kexpect +obkey kfound +ebfound +obkey kobl +ebword kobl +p = dfield kspan +a = ebnum p +put g GDA sub a sh +obkey kspan +obopen 91 +obnum sub a sh +while and lt p get g GEN ne get eb p 45 +p = add p 1 +end +obsep +obnum sub ebnum add p 1 sh +obclose 93 +repair +end + +note ---------------------------------------------------------------- repairs + + +fn rtext e ulong to void +if eq e 3 +obtstr r3 +end +if eq e 4 +obtstr r4 +end +if eq e 5 +obtstr r5 +end +if eq e 6 +obtstr r6 +end +if eq e 7 +obtstr r7 +end +if eq e 8 +obtstr r8 +end +if eq e 9 +obtstr r9 +end +rtext2 e +end + +fn rtext2 e ulong to void +if eq e 10 +obtstr r10 +end +if eq e 11 +obtstr r11 +end +if eq e 12 +obtstr r12 +end +if eq e 13 +obtstr r13 +end +if eq e 14 +obtstr r14 +end +if eq e 15 +obtstr r15 +end +if eq e 16 +obtstr r16 +end +rtext3 e +end + +fn rtext3 e ulong to void +if eq e 17 +obtstr r17 +end +if eq e 18 +obtstr r18 +end +if eq e 19 +obtstr r19 +end +if eq e 20 +obtstr r20 +end +if eq e 21 +obtstr r21 +end +if eq e 22 +obtstr r22 +end +if eq e 27 +obtstr r27 +end +if eq e 30 +obtstr r30 +end +if or lt e 3 or eq e 23 or gt e 30 and gt e 23 lt e 27 +obtstr tvoid +end +end + +fn repair to void +e = get g GRC +obkey kwhy +rtext e +if eq e 13 +rlong +end +if eq e 3 +rmove +end +if eq e 15 +rend +end +end + +fn rlong to void +note rename the long name (in scratch) to an unused 8-character name, everywhere; recompile +n = get g GCB +if or le n 8 gt n 64 +ret +end +put g GDC ulong get cb 7 +for d ulong 10 +put cb 7 uchar add ulong 48 d +if not inuse +rdorename n +ret +end +end +end + +fn inuse to bool +note is the first 8 bytes of scratch already a word in the source +for i ulong get g GNL +p = skb get lsa i +while lt p get lea i +q = wde p +if eq sub q p 8 +if cbeqsrc p 8 +ret true +end +end +p = skb q +end +end +ret false +end + +fn cbeqsrc p ulong n ulong to bool +for i ulong n +if ne get cb i get src add p i +ret false +end +end +ret true +end + +fn rdorename n ulong to void +note n is the long name's length; cb holds the long name with byte 7 replaced +put g GWN 0 +p = ulong 0 +m = get g GSL +while lt p m +if wordat p n +for j ulong 8 +w1 get cb j +end +p = add p n +else +w1 get src p +p = add p 1 +end +end +rafter +end + +fn wordat p ulong n ulong to bool +note whole-word match of the original long name (scratch, byte 7 differs) at p +if gt add p n get g GSL +ret false +end +if gt p 0 +if not wsep get src sub p 1 +ret false +end +end +if lt add p n get g GSL +if not wsep get src add p n +ret false +end +end +for i ulong n +if ne i 7 +if ne get cb i get src add p i +ret false +end +end +end +ret eq ulong get src add p 7 get g GDC +end + +fn wsep c uchar to bool +ret or isb c or eq c 10 or eq c 40 eq c 41 +end + +fn rafter to void +obkey kfixr +ob1 34 +for j ulong 8 +obe get cb j +end +ob1 34 +put g GSEP 1 +r = run +obkey kafter +obnum r +end + +fn rmove to void +note an unknown global/const/text defined later at top level: move that line above the first fn +n = get g GCB +k = get g GNL +for i ulong k +if or lwis i 0 tglobal or lwis i 0 tconst lwis i 0 ttext +a = lnw i 1 +if eq sub wde a a n +if cbeqsrc a n +if gt i firstfn +rdomove i +ret +end +end +end +end +end +end + +fn firstfn to ulong +for i ulong get g GNL +if isfn i +ret i +end +end +ret get g GNL +end + +fn rdomove m ulong to void +f = firstfn +put g GWN 0 +wlns 0 f +wln m +wlns f m +wlns add m 1 get g GNL +obkey kfixm +obstr get lsa m sub get lea m get lsa m +r = run +obkey kafter +obnum r +end + +fn rend to void +note append the missing end lines and recompile +put g GWN 0 +wlns 0 get g GNL +for k ulong 8 +wt tend +r = run +if ne r 15 +obkey kfixe +obnum add k 1 +obkey kafter +obnum r +ret +end +end +end + +note ---------------------------------------------------------------- main + +fn setup to bool +if lt argcnt 2 +r = write 2 tusage 0 len tusage +ret false +end +n = argread 1 pk 4 4000 +if le n 0 +ret false +end +put pk 0 1 +put pk 1 0 +put pk 2 uchar and ulong n 255 +put pk 3 uchar shr ulong n 8 +put g GPN add ulong n 4 +fi = memfd tmfd 0 len tmfd 1 +fe = memfd tmfd 0 len tmfd 1 +dn = fopen tdevnull 0 len tdevnull 524289 0 +if or lt fi 0 or lt fe 0 lt dn 0 +ret false +end +put g GMI ulong fi +put g GME ulong fe +put g GDN ulong dn +ret true +end + +fn selftest to bool +note the given OMC must accept a minimal unit and reject a truncated one with error 15 +put g GWN 0 +wt tgood +if ne run 0 +ret false +end +put g GWN 0 +wt tbadp +if ne run 15 +ret false +end +ret and ge get g GEN len tomcl ebeq 0 tomcl +end + +note ---------------------------------------------------------------- prefix mode +note omq OMC prefix: the prefix-viability server first written by OmegaMind as omviable.om +note (handoffs/OmegaMind/omviable, 2026-10-26), absorbed here so one implementation exists. +note request on fd 0: 4-byte little-endian length L, then L bytes of program prefix. +note the prefix is compiled as "om2 diag" LF prefix. reply on fd 1: one line "V CODE" where +note V 0 = a whole unit omc accepts, 1 = a prefix omc can still complete (first diag +note sev=incomplete, or sev=unknown whose span ends at the prefix end on a word byte), +note 2 = dead (any other refusal), 3 = the compiler did not run; CODE = omc exit status. + +fn pmode to bool +if lt argcnt 3 +ret false +end +k = argread 2 cb 0 64 +if ne k long len tprefix +ret false +end +for i ulong len tprefix +if ne get cb i get tprefix i +ret false +end +end +ret true +end + +fn rdlen to long +got = ulong 0 +while lt got 4 +r = read 0 lb got sub 4 got +if le r 0 +ret neg long 1 +end +got = add got ulong r +end +v = ulong 0 +for i ulong 4 +v = or v shl ulong get lb i mul i 8 +end +ret long v +end + +fn rdn n ulong to bool +got = ulong 0 +while lt got n +r = read 0 src got sub n got +if le r 0 +ret false +end +got = add got ulong r +end +ret true +end + +fn wordch c uchar to bool +a = or and ge c 97 le c 122 and ge c 65 le c 90 +ret or a or and ge c 48 le c 57 eq c 95 +end + +fn pspan to ulong +p = dfield kspan +while and lt p get g GEN ne get eb p 45 +p = add p 1 +end +ret ebnum add p 1 +end + +note prefix mode: the open word at the prefix end is dead once it is longer than any word omc +note accepts, because growing it never helps (OmegaMind mask eval: runaway words such as +note "arr[i324i324..." reached max_tokens). Exempt: note lines, text literals (odd quote count on +note the line) and prefixes of the long keyword invariant. +fn spc c uchar to bool +ret or or eq c 32 eq c 10 or eq c 9 eq c 13 +end + +fn freetxt b ulong to bool +q = ulong 0 +i = add b 1 +note and evaluates both sides, so the scan tests i before reading src at i - 1 +while gt i 0 +if eq get src sub i 1 10 +break +end +i = sub i 1 +if eq get src i 34 +q = add q 1 +end +end +if eq and q 1 1 +ret true +end +if gt add i 4 b +ret false +end +for j ulong len tnote +if ne get src add i j get tnote j +ret false +end +end +ret true +end + +fn kwpre b ulong w ulong to bool +if gt w len tinv +ret false +end +for j ulong w +if ne get src add b j get tinv j +ret false +end +end +ret true +end + +fn wdead n ulong to bool +b = n +while gt b 0 +if spc get src sub b 1 +break +end +b = sub b 1 +end +w = sub n b +if or eq w 0 freetxt b +ret false +end +if gt w NUMMAX +ret true +end +f = get src b +if or and ge f 48 le f 57 eq f 45 +ret false +end +ret and gt w NAMEMAX not kwpre b w +end + +fn pverd rc ulong n ulong to ulong +if eq rc 0 +ret 0 +end +if ge rc 250 +ret 3 +end +if wdead n +ret 2 +end +s = dfield ksev +if ge s get g GEN +ret 2 +end +c = get eb s +if eq c 105 +ret 1 +end +if ne c 117 +ret 2 +end +if eq n 0 +ret 2 +end +if and eq pspan get g GWN wordch get src sub n 1 +ret 1 +end +ret 2 +end + +fn pserve to int +while true +m = rdlen +if lt m 0 +ret 0 +end +n = ulong m +if gt n 1048000 +ret 92 +end +if not rdn n +ret 93 +end +put g GWN 0 +wt thdr +wsr 0 n +rc = run +v = pverd rc n +cbc +cb1 uchar add v 48 +cb1 32 +cbn rc +cb1 10 +r = write 1 cb 0 get g GCB +end +ret 0 +end + +fn main to int +if not setup +r = write 2 tbad 0 len tbad +ret 2 +end +if not selftest +r = write 2 tbad 0 len tbad +ret 2 +end +if pmode +ret pserve +end +rdin +mklines +put g GOM2 0 +if lwis firstln 0 tom2 +put g GOM2 1 +end +nh = fhole +put g GNH nh +obopen 123 +if gt nh 1 +obkey jerr +obtstr jmany +ansdone +ret 1 +end +if eq nh 0 +anscheck +ansdone +ret 0 +end +if not ctx +obkey jerr +obtstr jout +ansdone +ret 1 +end +ansctx +clmode +anscheck +put g GNC 0 +gtop +gfn +gloc +for c ulong get g GNC +pscal c +end +ansscope +ansfns +ansnext +ansobl +ansdone +ret 0 +end + +fn ansnext to void +if eq get g GHK 2 +vexpect +end +obkey jnext +obopen 91 +if eq get g GHK 1 +nstmt +else +vnext +end +obclose 93 +end + +fn ansdone to void +obkey jruns +obnum get g GRUN +obclose 125 +ob1 10 +p = ulong 0 +n = get g GON +while lt p n +w = write 1 ob p sub n p +if le w 0 +ret +end +p = add p ulong w +end +end