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
omega-mv / docs / osc / OSC-1-DESIGN.md
| 1 | # OSC-1: the first compiler slice (design, normative) |
| 2 | |
| 3 | **OSC-1 slice; not a general Omega compiler; no self-hosting.** |
| 4 | |
| 5 | Status: implemented as a host-tested slice (Lane 22, 2026-10-01). Governing record: |
| 6 | [OMEGA-SYSTEMS-CORE-0000](../adr/OMEGA-SYSTEMS-CORE-0000.md) and the audit |
| 7 | sections it cites ([OMEGA_SYSTEMS_CORE_CODE_AUDIT.md](../../OMEGA_SYSTEMS_CORE_CODE_AUDIT.md) |
| 8 | III.6 pipeline, III.7 OSC-1 definition, III.8 acceptance tests, II.9 determinism, |
| 9 | II.11 executable model). |
| 10 | |
| 11 | ## 1. What OSC-1 is |
| 12 | |
| 13 | OSC-0 III.7 defines OSC-1 as "the smallest vertical slice: source -> parser -> |
| 14 | typed value -> unique allocation -> borrow -> bounds check -> deterministic |
| 15 | destruction -> native execution". This slice implements exactly that path, for |
| 16 | a small language that extends V0 (spec/omega-language-v0.md), plus the integer |
| 17 | and control-flow core needed to write programs that exercise it: |
| 18 | |
| 19 | - integer scalars u8 u16 u32 u64 (wrap, V0 compatible) and i8 i16 i32 i64 |
| 20 | (checked: overflow is a deterministic trap, OSC-0 III.3), and bool; |
| 21 | - `let` bindings (immutable by default, `let mut` for reassignment); |
| 22 | - arithmetic, bitwise, shift, comparison, logical operators; |
| 23 | - `if` / `else`, `while ... bound N` and `for i in A .. B` loops whose trip count |
| 24 | has a static bound; |
| 25 | - calls to functions defined earlier in the same unit (no recursion); |
| 26 | - unique allocation `own [T; N]`, moves, shared and mutable borrows `&[T; N]` / |
| 27 | `&mut [T; N]`, indexing with a bounds check, deterministic destruction at |
| 28 | scope end; |
| 29 | - `requires` / `ensures` clauses parsed and recorded as text, **not checked** (superseded by OSC-2 item 1: `docs/osc/OSC-2-DESIGN.md` section 1) |
| 30 | (OSC-0 decision 3, III.6). Enforcement is OSC-2 and mandatory before any |
| 31 | production C migrates. |
| 32 | |
| 33 | Pipeline (III.6 steps realised here): lexer/parser -> typed AST (disposable) -> |
| 34 | name/type resolution -> ownership + borrow analysis (lexical lifetimes) -> typed |
| 35 | IR (`src/compiler/osc_ir.h`, canonical encoding + SHA-256 digest) -> realization |
| 36 | to AArch64 through an in-repo encoder whose every emitted word is mirrored in a |
| 37 | decoder. Not in OSC-1: effects/capabilities (III.6 step 6), structs, strings, |
| 38 | generics, generations, arenas, unsafe physical, FFI, Flow IR optimisation. |
| 39 | |
| 40 | ## 2. Surface grammar (EBNF) |
| 41 | |
| 42 | ``` |
| 43 | unit = { fn_def } EOF ; |
| 44 | fn_def = "fn" NAME "(" [ param { "," param } ] ")" [ "->" type ] |
| 45 | [ "requires" clause ] [ "ensures" clause ] block ; |
| 46 | param = NAME ":" ptype ; (* at most 6 params *) |
| 47 | ptype = type | arr_type ; |
| 48 | arr_type = "own" "[" type ";" INT "]" (* owner, moved into the callee *) |
| 49 | | "&" "[" type ";" INT "]" (* shared borrow *) |
| 50 | | "&" "mut" "[" type ";" INT "]" ; (* mutable borrow *) |
| 51 | type = "u8" | "u16" | "u32" | "u64" | "i8" | "i16" | "i32" | "i64" | "bool" ; |
| 52 | clause = { token } ; (* up to "ensures" / "{" at paren depth 0; recorded text *) |
| 53 | block = "{" { stmt } "}" ; |
| 54 | stmt = "let" [ "mut" ] NAME ":" type "=" expr ";" |
| 55 | | "let" NAME ":" "own" "[" type ";" INT "]" "=" "alloc" "(" expr ")" ";" |
| 56 | | "let" NAME ":" "own" "[" type ";" INT "]" "=" NAME ";" (* move *) |
| 57 | | "let" [ "mut" ] NAME ":" ( "&" | "&" "mut" ) "[" type ";" INT "]" "=" borrow ";" |
| 58 | | NAME "=" ( expr | borrow ) ";" (* mut scalar, or mut borrow binding *) |
| 59 | | NAME "[" expr "]" "=" expr ";" |
| 60 | | "if" expr block [ "else" ( block | if_stmt ) ] |
| 61 | | "while" expr "bound" INT block |
| 62 | | "for" NAME "in" INT ".." INT block (* i: i64, half-open, literal ends *) |
| 63 | | "return" [ expr ] ";" |
| 64 | | call ";" |
| 65 | | block ; |
| 66 | borrow = "&" NAME | "&" "mut" NAME ; |
| 67 | expr = lor ; |
| 68 | lor = land { "||" land } ; (* short-circuit *) |
| 69 | land = cmp { "&&" cmp } ; (* short-circuit *) |
| 70 | cmp = bor [ cmp_op bor ] ; (* non-associative *) |
| 71 | bor = bxor { "|" bxor } ; |
| 72 | bxor = band { "^" band } ; |
| 73 | band = shift { "&" shift } ; |
| 74 | shift = add { ( "<<" | ">>" ) add } ; |
| 75 | add = mul { ( "+" | "-" ) mul } ; |
| 76 | mul = unary { ( "*" | "/" | "%" ) unary } ; |
| 77 | unary = ( "-" | "~" | "!" ) unary | postfix ; |
| 78 | postfix = primary [ "as" type ] ; |
| 79 | primary = INT | "true" | "false" | NAME | NAME "[" expr "]" | call | "(" expr ")" ; |
| 80 | call = NAME "(" [ arg { "," arg } ] ")" ; |
| 81 | arg = expr | borrow ; (* an owner NAME as an `own` arg is a move *) |
| 82 | cmp_op = "==" | "!=" | "<" | "<=" | ">" | ">=" ; |
| 83 | ``` |
| 84 | |
| 85 | Literals, comments and identifiers are spelled as in V0 (decimal, `0x`, `0b`; |
| 86 | `//` and `/* */`). Precedence differs from V0 only by the added levels; every V0 |
| 87 | operator keeps its V0 meaning. `free`, `drop`, `delete`, `dealloc` are reserved |
| 88 | and are a syntax error: destruction has no surface syntax, so a forgotten free |
| 89 | is inexpressible (III.8). |
| 90 | |
| 91 | ## 3. Static rules (all refusals are typed diagnostics, section 6) |
| 92 | |
| 93 | Types. No implicit conversion: both operands of a binary operator have the |
| 94 | identical type (shift: the amount may be any integer type), comparison operands |
| 95 | have the identical type, `if`/`while` conditions and `&& || !` operands are |
| 96 | `bool`. `as` converts between integer types only (checked at run time). The |
| 97 | array element type is an integer type or bool; N is 1..64. |
| 98 | |
| 99 | Literals. A literal gets its type from the context (let annotation, the other |
| 100 | operand, the parameter / return / element type it is passed to, the `as` |
| 101 | source is refused for literals). A literal with no context is refused |
| 102 | (ambiguous width, as in V0); a literal that does not fit its type is refused |
| 103 | (`overflow-unsafe`). |
| 104 | |
| 105 | Overflow-unsafe, refused at compile time: a constant expression of a signed |
| 106 | type whose value overflows; a literal shift amount >= width; a literal zero |
| 107 | divisor; a negative literal for an unsigned type. |
| 108 | |
| 109 | Names. Every name is defined before use, in an enclosing scope. Shadowing in |
| 110 | an inner scope is refused (keeps diagnostics unambiguous). Functions are called |
| 111 | only after their definition (no recursion, no forward reference). |
| 112 | |
| 113 | Loops. `while` without `bound N` is refused (`unbounded loop`). `for` ends must |
| 114 | be integer literals with A <= B. N and B - A are at most 1_000_000. A `while` |
| 115 | that would start iteration N+1 traps with OSC_TRAP_LOOP_BOUND. |
| 116 | |
| 117 | Return. Every path of a non-void function ends in `return expr;`. A function |
| 118 | returns a scalar only (no `->` arr_type in OSC-1). |
| 119 | |
| 120 | ### 3.1 Ownership and borrows (lexical lifetimes) |
| 121 | |
| 122 | Objects: each `alloc(...)` creates one unique allocation owned by the binding. |
| 123 | An owner binding is in one of: `owned`, `moved`. A borrow binding (or a borrow |
| 124 | argument for the duration of a call) holds a borrow of one or more owners. |
| 125 | |
| 126 | - Move: `let b: own[..] = a;` or passing owner `a` to an `own` parameter. After |
| 127 | a move, any use of `a` (read, index, borrow, move) is refused: **use after |
| 128 | move**. A move inside a loop body of an owner declared outside that loop is |
| 129 | refused (use after move on the next iteration). A move on only some paths of |
| 130 | an `if` is refused (`conditional move`): OSC-1 has no drop flags (superseded by OSC-3 item 3, drop flags). |
| 131 | - Shared borrow `&a`: allowed while `a` is owned and not mutably borrowed. |
| 132 | - Mutable borrow `&mut a`: `a` must be owned and not borrowed at all; through a |
| 133 | borrow binding `r: &mut`, `&mut r` is a reborrow of the same target. |
| 134 | - While a borrow of `a` is live (borrow binding in scope, or a call argument |
| 135 | during the call): moving `a`, storing into `a[i]` directly, or taking |
| 136 | `&mut a` with any other live borrow is refused: **mutable alias**. A call |
| 137 | `f(&mut a, &a)` or `f(&mut a, &mut a)` is a mutable alias. While a `&mut` |
| 138 | borrow of `a` is live, any direct use of `a` is refused (mutable alias). |
| 139 | - **Borrow outlives owner**: a borrow binding declared in an outer scope is |
| 140 | assigned a borrow of an owner declared in an inner scope (the owner is |
| 141 | destroyed while the borrow is still in scope); also refused: returning or |
| 142 | storing a borrow in any other place (OSC-1 has nowhere else to put one). |
| 143 | - Indexing: `a[i]` reads through an owner or any borrow; `a[i] = v` writes |
| 144 | through an owner (not borrowed) or a `&mut` borrow binding. A shared borrow is |
| 145 | read only. **Statically known out-of-bounds**: an index that is an integer |
| 146 | literal (or a constant expression) outside 0..N-1 is refused at compile time. |
| 147 | Any other index is checked at run time: out of range is a deterministic trap |
| 148 | OSC_TRAP_BOUNDS. |
| 149 | - Destruction: at the end of each scope, every owner declared in it that was |
| 150 | not moved is destroyed (OSC_I_RELEASE) in reverse declaration order, |
| 151 | including on early `return`. An `own` parameter is an owner of the function |
| 152 | scope. There is no other way to free. |
| 153 | |
| 154 | The ownership analysis records, per function, the event sequence it checked |
| 155 | (alloc, borrow_shared, borrow_mut, end_borrow, move, use, release, scope end). |
| 156 | The tests replay these sequences through the II.11 executable model |
| 157 | (`src/compiler/model/`) and require the model's verdict to agree with the |
| 158 | compiler's (accept/accept, or the same named invalid transition). |
| 159 | |
| 160 | ## 4. IR |
| 161 | |
| 162 | `src/compiler/osc_ir.h` is the data definition. Notes: |
| 163 | |
| 164 | - vregs 0..nparams-1 are the parameters. Each named local gets one vreg; |
| 165 | temporaries get fresh vregs. Every vreg has one type. |
| 166 | - The front end lowers `&&`/`||` to branches, loops to blocks with an explicit |
| 167 | counter vreg, CMP and CBR on the bound, and `OSC_I_TRAP 4` when exceeded. |
| 168 | - Release instructions are emitted by the front end at every scope exit. |
| 169 | - The IR has a canonical little-endian encoding with no padding |
| 170 | (`osc_ir_encode`); the SHA-256 of that encoding is the unit's identity. |
| 171 | Source spelling, comments and whitespace never reach it. |
| 172 | |
| 173 | ## 5. Semantics shared by interpreter and native code |
| 174 | |
| 175 | Values: 64-bit canonical (unsigned zero-extended, signed sign-extended, bool |
| 176 | 0/1). Unsigned results are reduced modulo 2^w. Signed results must be |
| 177 | representable or the run traps with OSC_TRAP_OVERFLOW. Division truncates |
| 178 | toward zero; remainder has the sign of the dividend; divisor 0 traps DIV0. |
| 179 | Shift amount must be in 0..w-1 or the run traps SHIFT. Signed `<<` traps |
| 180 | OVERFLOW if the result is not representable. `as` traps CAST if the value is |
| 181 | not representable in the target type. Array elements are 8-byte canonical |
| 182 | cells; allocation initialises every element to the given value; release zeroes |
| 183 | the cells. Allocation takes the lowest free pool slot; pool exhaustion traps |
| 184 | OOM. A trap ends the whole run: no destructors run (no unwinding, OSC-0 |
| 185 | decision 6); the harness resets the pool. |
| 186 | |
| 187 | Observable outcome of a run = (trap code, return value if no trap, the pool |
| 188 | event log). The interpreter and native code must agree on all three. |
| 189 | |
| 190 | ## 6. Diagnostics |
| 191 | |
| 192 | Every refusal is an `OscDiag` with: `kind` (enum, below), `line`, `col`, |
| 193 | `object` (the name involved), `origin_line` (where that object / borrow was |
| 194 | created), `other` (the conflicting borrow or name, if any), `transition` (the |
| 195 | attempted transition as text, e.g. "use after move", "borrow_mut while |
| 196 | borrow_shared live"). Kinds: SYNTAX, UNSUPPORTED, CAPACITY, UNDEFINED_NAME, |
| 197 | REDEFINED_NAME, TYPE_MISMATCH, AMBIGUOUS_WIDTH, OVERFLOW_UNSAFE, |
| 198 | UNBOUNDED_LOOP, MISSING_RETURN, USE_AFTER_MOVE, CONDITIONAL_MOVE, |
| 199 | MUTABLE_ALIAS, BORROW_OUTLIVES_OWNER, STATIC_OUT_OF_BOUNDS, IMMUTABLE_ASSIGN, |
| 200 | READ_ONLY_BORROW. Negative tests assert the kind and the fields. |
| 201 | |
| 202 | ## 7. Native ABI (AArch64, Linux host) |
| 203 | |
| 204 | - Each function: params in x0..x5, return in x0 (bool 0/1, canonical |
| 205 | extension), hidden runtime pointer `OscRt *` in x7. |
| 206 | - Frame: `stp x29, x30, [sp, #-16]!; mov x29, sp; sub sp, sp, #F` with F = |
| 207 | 8 * nvregs + 8 rounded up to 16. Slot 0 holds x7; vreg v lives at |
| 208 | `[sp, #8 * (v + 1)]`. Every instruction loads its operands from slots into |
| 209 | x9..x15 and stores its result to the destination slot (no register |
| 210 | allocation in OSC-1; deterministic and simple). |
| 211 | - Calls between compiled functions use BL; runtime entry points are reached by |
| 212 | `ldr x16, [x7_slot]`, `ldr x16, [x16, #off]`, `blr x16`. |
| 213 | - Encoder: `src/compiler/osc_a64.c`. Every emitter refuses an out-of-range |
| 214 | field (no silent masking), SP and XZR are distinct operand kinds, and every |
| 215 | emitted word decodes through `osc_a64_decode` back to the same fields. |
| 216 | The existing `src/aarch64_encoder.c` is not changed. |
| 217 | |
| 218 | ## 8. Determinism (II.9) |
| 219 | |
| 220 | Compiler output is a pure function of the source bytes: no timestamps, paths, |
| 221 | environment, pointer values or hash-order dependence. Gate: compile every |
| 222 | corpus program twice in separate processes and compare IR digests and machine |
| 223 | code bytes. |
| 224 | |
| 225 | ## 9. Self-hosting |
| 226 | |
| 227 | See [OSC-1-SELF-HOST-STATEMENT.md](OSC-1-SELF-HOST-STATEMENT.md). Short form: |
| 228 | the OSC-1 compiler cannot compile any part of itself, and this slice makes no |
| 229 | self-hosting claim. |
| 230 | |
| 231 | ## 10. Implementation notes and evidence |
| 232 | |
| 233 | - Files: `src/compiler/` (front end `osc_lex/parse/check/lower/front`, IR |
| 234 | `osc_ir`, reference interpreter `osc_interp`, encoder/decoder `osc_a64`, |
| 235 | codegen `osc_cg`, loader `osc_native`, bootstrap runtime `osc_rt`, CLI |
| 236 | `oscc_main.c`), the II.11 model in `src/compiler/model/`, tests in |
| 237 | `tests/compiler/` (OSC-0 III.8 names `tests/osc/`; this slice keeps its tests |
| 238 | next to its sources under `tests/compiler/`). |
| 239 | - Encoder. `src/aarch64_encoder.c` (used by `omega_program_realize`, M5/M6/M9/M14) |
| 240 | masks register fields silently (`rm & 0x1f`), which is exactly what OSC-0 III.6 |
| 241 | says must be hardened. Changing it would change the inputs of the existing |
| 242 | gates, so it is left untouched and OSC-1 uses its own validating encoder |
| 243 | `osc_a64.c` with a decoder mirror. The existing M6/M9/M14 gates were run |
| 244 | before and after this work and are unchanged. |
| 245 | - `make test-compiler` (host, single core, about 70 s): OSC-0B model sweep |
| 246 | (10^6 sequences), back end differential test, compiler golden / negative / |
| 247 | model-agreement test, cross-process determinism; the three test binaries also |
| 248 | run under ASan/UBSan. `make compiler-receipt` writes the content-addressed |
| 249 | receipt `evidence/OSC-1/receipts/osc1-compiler-<sha256>.json`; |
| 250 | `make osc0b-model-receipt` writes the OSC-0B exit receipt |
| 251 | `evidence/OSC-1/receipts/osc0b-model-<sha256>.json`. |
| 252 | - The reference interpreter is a second implementation of section 5 (signed |
| 253 | arithmetic computed exactly in 128 bits and range-checked), not a copy of the |
| 254 | codegen's flag logic. It is a differential oracle; agreement between two |
| 255 | implementations is evidence, not proof. |
| 256 | |
| 257 | ## 11. Explicit allocation |
| 258 | |
| 259 | Every heap byte the compiler takes comes from one `OscAlloc` (`src/compiler/osc_alloc.h`) the |
| 260 | caller passes in: `alloc(ctx, n)`, `realloc(ctx, p, old_n, new_n)`, `free(ctx, p, n)`. Sizes travel |
| 261 | with every call (Zig style), so an allocator needs no per-block header and a test allocator can |
| 262 | check them. Memory is never assumed zeroed; the checker and lowerer clear what they need. |
| 263 | |
| 264 | - Entry points: `osc_compile_a`, `osc_compile_imports_a`, `osc_cg_compile_a`, `osc_ir_validate_a` |
| 265 | take the allocator first (NULL means libc). `osc_compile`, `osc_compile_imports`, |
| 266 | `osc_cg_compile`, `osc_ir_validate` are the same over `osc_alloc_libc()`, so no caller changes. |
| 267 | `OscCode.a` records the allocator that owns `.code`; `osc_cg_free` frees with it (NULL = libc). |
| 268 | - A refusal is an ordinary failure: `OSC_DIAG_CAPACITY` from the front end; `-1` with "out of memory" |
| 269 | in `err` from the back end; `osc_ir_validate_a` returns `-2` with the same text. All scratch is |
| 270 | freed on every failure path. |
| 271 | - Output is a pure function of the source, whatever the allocator. |
| 272 | - `OscAllocMeter` wraps any allocator: it counts calls, tracks live and peak bytes, refuses the k-th |
| 273 | call (`fail_at`) or any call past a byte `budget`, and flags a free or realloc with a wrong size. |
| 274 | - `tests/compiler/test_osc_alloc.c` refuses every allocation call of every golden program in turn |
| 275 | (plus a generated unit that grows the back end buffers), requires a clean failure and zero live |
| 276 | bytes each time, and checks that a budget of the measured peak P succeeds and P-1 fails. |
| 277 | - Not covered: `osc_ir_encode`, `osc_ir_digest` and the interpreter entry validate over libc; the |
| 278 | `oscc` driver keeps its own file buffers on libc. |