From a908454cbc8cb541e9afb43d7679ca218ffade91 Mon Sep 17 00:00:00 2001 From: Drake Stapleton Date: Thu, 1 Oct 2026 04:29:39 -0500 Subject: [PATCH 1/2] OSC-2 item 2: fixed-layout structs (Lane 27) Unit-level `struct Name { field: type, ... }` (<= 16 structs, 1..16 fields of integer, bool or [T; N], <= 64 cells, declared before use, no recursion). Layout: 8-byte cells in declaration order, prefix-sum offsets, no padding, bound into the IR digest (encoding version 2 only when a unit has structs; struct-free units encode exactly as OSC-1, so all existing golden IR and machine code stay byte-identical). New IR ops FLOAD/FSTORE in the interpreter and the AArch64 back end; array fields are bounds-checked (TRAP BOUNDS). Ownership as for arrays: struct literal in `let own` initialises every field exactly once, moves (let and own params), & / &mut with the lexical borrow rules, deterministic release at scope end with the same pool events. Struct return is UNSUPPORTED; ensures reads fields only through shared &. New diagnostics UNKNOWN_FIELD, DUPLICATE_FIELD, MISSING_FIELD, UNDEFINED_TYPE, RECURSIVE_STRUCT. Tests: 4 golden programs, 36 st_* negatives, layout and destruction-order checks, struct fuzz (plain + ASan, mismatches=0). Receipt: osc2_receipt.sh ITEM=structs. Docs: OSC-2-DESIGN section 2. OSC-2 slice; not a general Omega compiler; no self-hosting. Co-Authored-By: Drake Stapleton Co-Authored-By: Claude Fable 5.1 Co-Authored-By: Claude Opus 5.5 --- docs/osc/OSC-2-DESIGN.md | 150 ++++++++ mk/compiler.mk | 6 +- src/compiler/osc_cg.c | 21 ++ src/compiler/osc_check.c | 152 ++++++-- src/compiler/osc_diag.c | 3 +- src/compiler/osc_diag.h | 6 + src/compiler/osc_interp.c | 24 ++ src/compiler/osc_ir.c | 103 +++++- src/compiler/osc_ir.h | 34 +- src/compiler/osc_lex.c | 7 +- src/compiler/osc_lex.h | 2 + src/compiler/osc_lower.c | 55 ++- src/compiler/osc_parse.c | 327 +++++++++++++++++- src/compiler/osc_parse.h | 16 +- tests/compiler/neg/st_alloc.osc | 6 + tests/compiler/neg/st_array_field_len.osc | 6 + .../neg/st_array_field_scalar_init.osc | 6 + tests/compiler/neg/st_array_field_whole.osc | 7 + .../compiler/neg/st_borrow_outlives_owner.osc | 12 + tests/compiler/neg/st_by_value_param.osc | 4 + tests/compiler/neg/st_call_wrong_struct.osc | 9 + tests/compiler/neg/st_capacity_fields.osc | 4 + tests/compiler/neg/st_capacity_size.osc | 4 + tests/compiler/neg/st_capacity_structs.osc | 20 ++ .../compiler/neg/st_duplicate_field_init.osc | 7 + tests/compiler/neg/st_empty.osc | 4 + tests/compiler/neg/st_ensures_mut_field.osc | 7 + .../compiler/neg/st_field_declared_twice.osc | 4 + .../neg/st_field_init_type_mismatch.osc | 7 + tests/compiler/neg/st_field_of_array.osc | 6 + tests/compiler/neg/st_field_type_mismatch.osc | 7 + tests/compiler/neg/st_index_struct.osc | 7 + tests/compiler/neg/st_literal_other_type.osc | 7 + tests/compiler/neg/st_missing_field_init.osc | 7 + tests/compiler/neg/st_move_wrong_struct.osc | 9 + tests/compiler/neg/st_mutable_alias.osc | 8 + tests/compiler/neg/st_oob_field.osc | 7 + tests/compiler/neg/st_owner_field.osc | 5 + tests/compiler/neg/st_recursive.osc | 4 + tests/compiler/neg/st_redefined.osc | 5 + tests/compiler/neg/st_return_type.osc | 6 + .../compiler/neg/st_scalar_field_indexed.osc | 7 + tests/compiler/neg/st_store_struct.osc | 7 + tests/compiler/neg/st_struct_as_scalar.osc | 7 + tests/compiler/neg/st_struct_field.osc | 5 + tests/compiler/neg/st_undefined_type.osc | 3 + tests/compiler/neg/st_unknown_field.osc | 7 + tests/compiler/neg/st_unknown_field_init.osc | 7 + tests/compiler/neg/st_use_after_move.osc | 8 + .../compiler/neg/st_write_through_shared.osc | 6 + tests/compiler/osc2_receipt.sh | 33 +- tests/compiler/progs/structs_contracts.osc | 28 ++ tests/compiler/progs/structs_dtor.osc | 19 + tests/compiler/progs/structs_fields.osc | 24 ++ tests/compiler/progs/structs_owner.osc | 41 +++ tests/compiler/test_osc_backend.c | 6 +- tests/compiler/test_osc_compiler.c | 232 +++++++++++++ 57 files changed, 1468 insertions(+), 63 deletions(-) create mode 100644 tests/compiler/neg/st_alloc.osc create mode 100644 tests/compiler/neg/st_array_field_len.osc create mode 100644 tests/compiler/neg/st_array_field_scalar_init.osc create mode 100644 tests/compiler/neg/st_array_field_whole.osc create mode 100644 tests/compiler/neg/st_borrow_outlives_owner.osc create mode 100644 tests/compiler/neg/st_by_value_param.osc create mode 100644 tests/compiler/neg/st_call_wrong_struct.osc create mode 100644 tests/compiler/neg/st_capacity_fields.osc create mode 100644 tests/compiler/neg/st_capacity_size.osc create mode 100644 tests/compiler/neg/st_capacity_structs.osc create mode 100644 tests/compiler/neg/st_duplicate_field_init.osc create mode 100644 tests/compiler/neg/st_empty.osc create mode 100644 tests/compiler/neg/st_ensures_mut_field.osc create mode 100644 tests/compiler/neg/st_field_declared_twice.osc create mode 100644 tests/compiler/neg/st_field_init_type_mismatch.osc create mode 100644 tests/compiler/neg/st_field_of_array.osc create mode 100644 tests/compiler/neg/st_field_type_mismatch.osc create mode 100644 tests/compiler/neg/st_index_struct.osc create mode 100644 tests/compiler/neg/st_literal_other_type.osc create mode 100644 tests/compiler/neg/st_missing_field_init.osc create mode 100644 tests/compiler/neg/st_move_wrong_struct.osc create mode 100644 tests/compiler/neg/st_mutable_alias.osc create mode 100644 tests/compiler/neg/st_oob_field.osc create mode 100644 tests/compiler/neg/st_owner_field.osc create mode 100644 tests/compiler/neg/st_recursive.osc create mode 100644 tests/compiler/neg/st_redefined.osc create mode 100644 tests/compiler/neg/st_return_type.osc create mode 100644 tests/compiler/neg/st_scalar_field_indexed.osc create mode 100644 tests/compiler/neg/st_store_struct.osc create mode 100644 tests/compiler/neg/st_struct_as_scalar.osc create mode 100644 tests/compiler/neg/st_struct_field.osc create mode 100644 tests/compiler/neg/st_undefined_type.osc create mode 100644 tests/compiler/neg/st_unknown_field.osc create mode 100644 tests/compiler/neg/st_unknown_field_init.osc create mode 100644 tests/compiler/neg/st_use_after_move.osc create mode 100644 tests/compiler/neg/st_write_through_shared.osc create mode 100644 tests/compiler/progs/structs_contracts.osc create mode 100644 tests/compiler/progs/structs_dtor.osc create mode 100644 tests/compiler/progs/structs_fields.osc create mode 100644 tests/compiler/progs/structs_owner.osc diff --git a/docs/osc/OSC-2-DESIGN.md b/docs/osc/OSC-2-DESIGN.md index 35d9acce2..584dd1531 100644 --- a/docs/osc/OSC-2-DESIGN.md +++ b/docs/osc/OSC-2-DESIGN.md @@ -116,3 +116,153 @@ without contracts produce the same bytes as OSC-1. `requires` / `ensures`; each unit compiles or is refused with CONTRACT_VIOLATION only; compiled units run native vs interpreter with 0 mismatches and must hit both TRAP REQUIRES and TRAP ENSURES. + +## 2. Structs + +### 2.1 Subset statement + +OSC-2 item 2 adds fixed-layout structs to the OSC-1 subset. A struct is a named group of +fields that lives in one pool allocation and is handled exactly like an owned array: it is +created by its owner, moved, borrowed with `&` / `&mut`, and released at scope end. A struct +is never a value: there is no struct copy, compare, or return. OSC-2 slice; not a general +Omega compiler; no self-hosting. + +### 2.2 Grammar + +``` +unit := (struct | fn)* +struct := "struct" NAME "{" field ("," field)* "}" +field := NAME ":" ftype +ftype := scalar | "[" scalar ";" INT "]" scalar = integer type or bool +type := ... | "own" NAME | "&" NAME | "&mut" NAME (NAME = a declared struct) +let := ... | "let" NAME ":" "own" NAME "=" NAME "{" init ("," init)* "}" ";" +init := NAME ":" expr (scalar field) + | NAME ":" "[" expr ";" INT "]" (array field, INT = its length) +expr := ... | NAME "." NAME | NAME "." NAME "[" expr "]" +stmt := ... | NAME "." NAME "=" expr ";" | NAME "." NAME "[" expr "]" "=" expr ";" +``` + +- `struct` declarations sit at unit level, before any function that names them. + `struct` is now a keyword and `.` a token. +- A struct literal appears only as the initialiser of `let x: own S = S { ... }`. + Fields may be listed in any order. The values are evaluated in source order. +- A local, parameter, or binding of struct type is always `own S`, `&S`, or `&mut S`. + +### 2.3 Layout (fixed, part of the IR digest) + +- Every field takes whole 8-byte cells, in declaration order. A scalar takes 1 cell and + `[T; N]` takes N cells. Field offsets are prefix sums, with no padding and no reordering. + Example: `struct L { a: u8, b: [i16; 3], c: bool, d: [u64; 2], e: i64 }` has offsets + 0 1 4 5 7 and uses 8 cells. `test_osc_compiler` asserts this. +- A struct owner is one pool allocation of `ncells` cells. This uses the same `alloc` / + `release` events and the same 64-slot pool as arrays. +- IR: `OscUnit.structs[]` records each struct's name, fields (name, scalar type, array length, + offset) and cell count. `osc_ir_validate` re-checks the prefix sums. A struct reference + type is `OSC_T_REF` with `sid` = struct index + 1. Two new instructions are added, + `FLOAD dst, a, idx|-1, field` and `FSTORE a, idx|-1, value, field`. FSTORE needs an own or + `&mut` reference. +- Encoding: a unit with no structs encodes exactly as in OSC-1 (version byte 1), so every + existing program keeps byte-identical IR and machine code. A unit with structs uses version + byte 2 and adds the struct table (names, field types, lengths, offsets, cell counts), so + the digest binds the layout. Reordering two fields changes the digest. + +### 2.4 Semantics + +- **Creation.** `let s: own S = S { f: e, ... }` allocates one object and stores every + field. An array field `[e; N]` fills all N elements with `e`. +- **Field access.** `s.f` reads and `s.f = v` writes. For an array field, `s.f[i]` reads and + `s.f[i] = v` writes, with the same bounds checks as arrays: a constant index outside + 0..N-1 is STATIC_OUT_OF_BOUNDS, and a dynamic one traps with TRAP BOUNDS (3) in both the + interpreter and native code. A field is used at its declared type. Arithmetic follows the + OSC-1 rules (wrap for unsigned types, overflow trap for signed types). +- **Ownership (OSC-0B, as for arrays).** + - `let t: own S = s;` and passing `s` to an `own S` parameter are moves. Any later use of + `s` is USE_AFTER_MOVE. + - `&s` and `&mut s` follow the same lexical borrow rules as arrays: no `&mut` together with + any other live borrow (MUTABLE_ALIAS), no write through `&` (READ_ONLY_BORROW), and no + borrow outliving its owner (BORROW_OUTLIVES_OWNER). + - Struct borrows produce the same model trace events, which are replayed against the + OSC-0B model. +- **Destruction.** Owners are released at scope end in reverse declaration order, structs + and arrays alike. A moved-from owner is not released again. + `tests/compiler/progs/structs_dtor.osc` asserts the event sequence alloc 1 2 3, + release 3 2, alloc 4, release 4 1 in both engines. +- **Functions.** A struct passes as `own S`, `&S`, or `&mut S`. Returning a struct (any + `-> S`, `-> own S`, `-> &S`) is refused as UNSUPPORTED, transition `struct return type`. + Bare `S` as a parameter or local type is UNSUPPORTED (`struct by value`). +- **Contracts.** + - `requires` may read fields (and array-field elements) through any struct parameter. + - `ensures` may read them only through a shared `&S` parameter; otherwise the clause is + CONTRACT_INVALID (`struct field read in ensures`). This is the same rule as array + element reads (section 1.3): an `own` or `&mut` parameter may have changed by the time + of the return. + +### 2.5 Diagnostics + +New kinds (appended; existing kinds unchanged): + +| Kind | Transition | Cause | +|---|---|---| +| UNKNOWN_FIELD | `access unknown field` / `initialise unknown field` | `s.z` or `S { z: .. }` with no field `z` | +| DUPLICATE_FIELD | `field initialised twice` / `field declared twice` | a field given twice in a literal or declaration | +| MISSING_FIELD | `field not initialised` | a literal omits a field | +| UNDEFINED_TYPE | `undefined struct type` | a type name that is not a declared struct | +| RECURSIVE_STRUCT | `struct field of its own type` | a field whose type names the struct itself | + +Existing kinds used by structs: + +- **TYPE_MISMATCH:** + - a field given a value of the wrong type + - a literal of another struct + - an array field set without `[e; N]`, or with the wrong N + - indexing a struct, or `.f` on an array + - indexing a scalar field, or using an array field without an index + - a struct used as a scalar + - a struct passed or moved where another struct type is needed +- **UNSUPPORTED:** + - `empty struct` + - `struct-typed field` + - `owner or borrow field` + - `struct by value` + - `struct return type` + - `alloc of a struct` +- **CAPACITY:** `struct capacity`, `field capacity`, `struct size`. +- **REDEFINED_NAME:** `struct declared twice`. +- **STATIC_OUT_OF_BOUNDS** on `s.f[k]`. +- **CONTRACT_INVALID:** `struct field read in ensures`. + +### 2.6 Limits + +- At most 16 structs per unit. +- Each struct has 1 to 16 fields and at most 64 cells. +- An array field is `[T; N]` of any integer type or bool, N >= 1. +- Fields cannot be structs, owners, or borrows. +- Struct literals appear only in `let own` initialisers. + +### 2.7 Evidence + +- Golden programs: + - `structs_fields.osc`: layout use, field read/write, array fields, TRAP BOUNDS. + - `structs_owner.osc`: moves, own / `&` / `&mut` parameters, borrow bindings. + - `structs_contracts.osc`: TRAP REQUIRES and TRAP ENSURES on fields. + - `structs_dtor.osc`: destruction order. +- Negatives in `tests/compiler/neg/st_*.osc` (36), each asserting kind, object, line, and + transition. They include: + - unknown field + - duplicate field init + - missing field init + - field type mismatch + - write through shared borrow + - use after move + - mutable alias + - borrow outlives owner + - recursive struct + - undefined struct type +- `test_osc_compiler` adds: + - the layout assertion, with the digest binding the layout and struct-free units still + using version 1; + - the destruction-order event check; + - **struct fuzz:** generated units with random field types, literals, field writes, + moves, and `&` / `&mut` / own calls with `requires` on fields. Every unit compiles, + and native and interpreter outcomes are identical (`struct fuzz: ... mismatches=0`) in + the plain and ASan/UBSan runs. The fuzz must hit TRAP BOUNDS and TRAP REQUIRES. diff --git a/mk/compiler.mk b/mk/compiler.mk index b47faf451..71abe2b9d 100644 --- a/mk/compiler.mk +++ b/mk/compiler.mk @@ -8,7 +8,7 @@ # agreement + cross-process determinism, plain and ASan/UBSan # oscc build/compiler/oscc : prints IR and code digests # compiler-receipt digest-named receipt under evidence/OSC-1/receipts (clean tree only) -# osc2-receipt ITEM= (default contracts): receipt under evidence/OSC-2/receipts (clean tree only) +# osc2-receipt ITEM=contracts|structs (default contracts): receipt under evidence/OSC-2/receipts (clean tree only) ifndef COMPILER_MK COMPILER_MK := 1 .PHONY: test-compiler oscc compiler-receipt osc0b-model-receipt osc2-receipt @@ -70,7 +70,9 @@ test-compiler: $(OSC_DIR)/test_osc_model $(OSC_DIR)/test_osc_model_asan \ sh tests/compiler/determinism.sh $(abspath $(OSC_DIR)/oscc) > $(OSC_DIR)/determinism.out @tail -1 $(OSC_DIR)/determinism.out; grep -q '^OSC1_DETERMINISM_PASS$$' $(OSC_DIR)/determinism.out @grep "^contract fuzz:" $(OSC_DIR)/compiler.out - @echo "test-compiler: PASS (OSC-1 slice + OSC-2 contracts; OSC-2 slice; not a general Omega compiler; no self-hosting.)" + @grep "^struct fuzz: .* mismatches=0$$" $(OSC_DIR)/compiler.out + @grep "^struct fuzz: .* mismatches=0$$" $(OSC_DIR)/compiler_asan.out + @echo "test-compiler: PASS (OSC-1 slice + OSC-2 contracts + structs; OSC-2 slice; not a general Omega compiler; no self-hosting.)" osc0b-model-receipt: sh tests/compiler/osc0b_model_receipt.sh diff --git a/src/compiler/osc_cg.c b/src/compiler/osc_cg.c index 327d9de4e..0d894b538 100644 --- a/src/compiler/osc_cg.c +++ b/src/compiler/osc_cg.c @@ -380,6 +380,27 @@ static void gen_func(Cg *g, const OscUnit *u, int fi) { E(g, osc_a64_mem(OSC_A64_STR_UOFF, 11, 12, 0)); } break; + case OSC_I_FLOAD: + case OSC_I_FSTORE: { + /* OSC-2 structs: cell = field offset (+ checked element index) */ + const OscField *fd = &u->structs[T[in->a].sid - 1].fields[in->imm]; + int base = 9; + ld(g, 9, in->a); + if (in->op == OSC_I_FSTORE) ld(g, 11, in->c); + if (in->b >= 0) { + ld(g, 10, in->b); + bounds(g, 10, fd->alen); + E(g, osc_a64_r3s(OSC_A64_ADD_REG, 12, 9, 10, OSC_A64_LSL, 3)); + base = 12; + } + if (in->op == OSC_I_FLOAD) { + E(g, osc_a64_mem(OSC_A64_LDR_UOFF, 11, base, 8 * (int64_t)fd->off)); + st(g, 11, in->dst); + } else { + E(g, osc_a64_mem(OSC_A64_STR_UOFF, 11, base, 8 * (int64_t)fd->off)); + } + break; + } case OSC_I_CALL: /* Direct BL needs the callee already emitted. osc_ir_validate refuses * calls to later functions (and self), so this cannot trigger on a diff --git a/src/compiler/osc_check.c b/src/compiler/osc_check.c index 3e9994e75..34d68b712 100644 --- a/src/compiler/osc_check.c +++ b/src/compiler/osc_check.c @@ -473,7 +473,7 @@ static int ovf(C *c, const OscNode *n, const char *transition, const char *msg) static int fold(C *c, int i, OscScalar t, i128 *out) { OscNode *n = NODE(i); - char msg[160]; + char msg[320]; if (t == OSC_T_BOOL) return tmismatch(c, n, "bool", "integer literal where bool is required"); int sg = osc_scalar_signed(t); unsigned w = osc_scalar_width(t); @@ -729,7 +729,7 @@ static int chk_call(C *c, int i, OscScalar want, int as_stmt); static int want_ok(C *c, const OscNode *n, OscScalar got, OscScalar want) { if (want && got != want) { - char msg[160]; + char msg[320]; snprintf(msg, sizeof msg, "expression of type %s where %s is required", tname(got), tname(want)); return tmismatch(c, n, tname(got), msg); } @@ -747,8 +747,23 @@ static int resolve(C *c, const OscNode *n) return s; } +/* "[u8; 4]" for an array ref type, the struct name for a struct ref type */ +static const char *rty(C *c, const OscType *t, char *buf, size_t cap) +{ + if (t->sid) snprintf(buf, cap, "%s", c->ast->structs[t->sid - 1].name); + else snprintf(buf, cap, "[%s; %u]", tname(t->elem), t->len); + return buf; +} + +static int chk_index_len(C *c, const char *name, uint32_t dline, unsigned len, int ix, const OscNode *at); + /* index expression + static bounds; arr symbol s */ static int chk_index(C *c, int s, int ix, const OscNode *at) +{ + return chk_index_len(c, c->sym[s].name, c->sym[s].line, c->sym[s].ty.len, ix, at); +} + +static int chk_index_len(C *c, const char *name, uint32_t dline, unsigned len, int ix, const OscNode *at) { OscScalar it; if (ctxfree(c, ix)) it = (OscScalar)chk(c, ix, OSC_T_I64); @@ -758,25 +773,70 @@ static int chk_index(C *c, int s, int ix, const OscNode *at) const OscNode *x = NODE(ix); if (x->is_const) { int64_t v = osc_scalar_signed(it) ? (int64_t)x->cval : (int64_t)(x->cval > (uint64_t)INT64_MAX ? -1 : (int64_t)x->cval); - int oob = osc_scalar_signed(it) ? (v < 0 || v >= (int64_t)c->sym[s].ty.len) - : (x->cval >= (uint64_t)c->sym[s].ty.len); + int oob = osc_scalar_signed(it) ? (v < 0 || v >= (int64_t)len) + : (x->cval >= (uint64_t)len); if (oob) { char other[64]; if (osc_scalar_signed(it)) snprintf(other, sizeof other, "index %lld", (long long)v); else snprintf(other, sizeof other, "index %llu", (unsigned long long)x->cval); - osc_diag_set(c->d, OSC_DIAG_STATIC_OUT_OF_BOUNDS, at->line, at->col, c->sym[s].name, c->sym[s].line, other, + osc_diag_set(c->d, OSC_DIAG_STATIC_OUT_OF_BOUNDS, at->line, at->col, name, dline, other, "index outside 0..N-1", "constant %s outside 0..%u of '%s'", other, - c->sym[s].ty.len - 1u, c->sym[s].name); + len - 1u, name); return -1; } } return 0; } +/* OSC-2 structs: resolve the field of ON_FIELD / ON_FSTORE node n (base symbol + * s, already resolved), check its index, set n->hi. Returns the field or NULL. */ +static const OscField *chk_field(C *c, OscNode *n, int s) +{ + char msg[320], fname[64]; + Sym *y = &c->sym[s]; + osc_tok_text(c->ast->src, &c->ast->toks[n->lo], fname, sizeof fname); + if (y->kind == SK_SCALAR || !y->ty.sid) { + snprintf(msg, sizeof msg, "'%s' is %s, not a struct ('.%s')", y->name, + y->kind == SK_SCALAR ? "a scalar" : "an array", fname); + tmismatch(c, n, y->name, msg); + return NULL; + } + const OscStruct *st = &c->ast->structs[y->ty.sid - 1]; + int f = -1; + for (int k = 0; k < st->nfields; k++) + if (!strcmp(st->fields[k].name, fname)) f = k; + if (f < 0) { + const OscToken *ft = &c->ast->toks[n->lo]; + osc_diag_set(c->d, OSC_DIAG_UNKNOWN_FIELD, ft->line, ft->col, fname, c->ast->struct_line[y->ty.sid - 1], + st->name, "access unknown field", "struct '%s' (of '%s') has no field '%s'", st->name, y->name, + fname); + return NULL; + } + n->hi = f; + const OscField *fd = &st->fields[f]; + if (fd->alen && n->a < 0) { + snprintf(msg, sizeof msg, "array field '%s.%s' is used element by element ('%s.%s[i]')", y->name, fname, + y->name, fname); + tmismatch(c, n, y->name, msg); + return NULL; + } + if (!fd->alen && n->a >= 0) { + snprintf(msg, sizeof msg, "field '%s.%s' is a scalar, not an array", y->name, fname); + tmismatch(c, n, y->name, msg); + return NULL; + } + if (fd->alen) { + char qn[64]; + snprintf(qn, sizeof qn, "%.30s.%.30s", y->name, fname); + if (chk_index_len(c, qn, y->line, fd->alen, n->a, n)) return NULL; + } + return fd; +} + static int chk(C *c, int i, OscScalar want) { OscNode *n = NODE(i); - char msg[160]; + char msg[320]; if (ctxfree(c, i) && !want) { osc_diag_set(c->d, OSC_DIAG_AMBIGUOUS_WIDTH, n->line, n->col, c->sobj, 0, NULL, "literal without type context", "integer literal has no type from context (ambiguous width)"); @@ -799,8 +859,8 @@ static int chk(C *c, int i, OscScalar want) if (s < 0) return -1; n->sym = s; if (c->sym[s].kind != SK_SCALAR) { - snprintf(msg, sizeof msg, "'%s' is an array %s, not a scalar value", c->sym[s].name, - c->sym[s].kind == SK_OWNER ? "owner" : "borrow"); + snprintf(msg, sizeof msg, "'%s' is %s %s, not a scalar value", c->sym[s].name, + c->sym[s].ty.sid ? "a struct" : "an array", c->sym[s].kind == SK_OWNER ? "owner" : "borrow"); return tmismatch(c, n, c->sym[s].name, msg); } t = c->sym[s].ty.s; @@ -818,8 +878,9 @@ static int chk(C *c, int i, OscScalar want) c->sym[s].name); return -1; } - if (c->sym[s].kind == SK_SCALAR) { - snprintf(msg, sizeof msg, "'%s' is a scalar, not an array", c->sym[s].name); + if (c->sym[s].kind == SK_SCALAR || c->sym[s].ty.sid) { + snprintf(msg, sizeof msg, "'%s' is a %s, not an array", c->sym[s].name, + c->sym[s].ty.sid ? "struct" : "scalar"); return tmismatch(c, n, c->sym[s].name, msg); } if (chk_index(c, s, n->a, n)) return -1; @@ -827,6 +888,25 @@ static int chk(C *c, int i, OscScalar want) t = c->sym[s].ty.elem; break; } + case ON_FIELD: { + if (contract_name(c, n)) return -1; + int s = resolve(c, n); + if (s < 0) return -1; + n->sym = s; + if (c->contract == 2 && c->sym[s].kind != SK_SCALAR && + !(c->sym[s].kind == SK_BORROW && c->sym[s].ty.ref == OSC_REF_SHARED)) { + osc_diag_set(c->d, OSC_DIAG_CONTRACT_INVALID, n->line, n->col, c->sym[s].name, c->sym[s].line, NULL, + "struct field read in ensures", + "an ensures clause may read fields only through a shared '&' parameter ('%s' is not one)", + c->sym[s].name); + return -1; + } + const OscField *fd = chk_field(c, n, s); + if (!fd) return -1; + if (access(c, s, 0, n->line, n->col)) return -1; + t = fd->s; + break; + } case ON_CALL: { if (c->contract) { char cn[64]; @@ -974,7 +1054,10 @@ static int find_fn(C *c, const char *name, int *after) return -1; } -static int same_arr(const OscType *a, const OscType *b) { return a->elem == b->elem && a->len == b->len; } +static int same_arr(const OscType *a, const OscType *b) +{ + return a->elem == b->elem && a->len == b->len && a->sid == b->sid; +} /* OSC-2: check fn fi's requires / ensures clauses (after its parameters are * declared, before its body). See docs/osc/OSC-2-DESIGN.md section 1. */ @@ -1125,7 +1208,8 @@ static int chk_call(C *c, int i, OscScalar want, int as_stmt) if (s < 0) return -1; a->sym = s; if (c->sym[s].kind != SK_OWNER || !same_arr(&c->sym[s].ty, pt)) { - snprintf(msg, sizeof msg, "'%s' is not an owner of [%s; %u]", c->sym[s].name, tname(pt->elem), pt->len); + char tb[72]; + snprintf(msg, sizeof msg, "'%s' is not an owner of %s", c->sym[s].name, rty(c, pt, tb, sizeof tb)); return tmismatch(c, a, c->sym[s].name, msg); } if (do_move(c, s, -1, a->line, a->col)) return -1; @@ -1140,7 +1224,9 @@ static int chk_call(C *c, int i, OscScalar want, int as_stmt) if (s < 0) return -1; a->sym = s; if (c->sym[s].kind == SK_SCALAR || !same_arr(&c->sym[s].ty, pt)) { - snprintf(msg, sizeof msg, "'%s' is not an array [%s; %u]", c->sym[s].name, tname(pt->elem), pt->len); + char tb[72]; + snprintf(msg, sizeof msg, "'%s' is not %s %s", c->sym[s].name, pt->sid ? "a struct" : "an array", + rty(c, pt, tb, sizeof tb)); return tmismatch(c, a, c->sym[s].name, msg); } int b = take_borrow(c, s, a->mut, a->line, a->col); @@ -1167,7 +1253,7 @@ static void set_sobj_tok(C *c, const OscNode *n) { osc_node_name(c->ast, n, c->s /* borrow node bn into a borrow of type ty (SHARED/MUT). Returns borrow index. */ static int chk_borrow_src(C *c, OscNode *bn, const OscType *ty, int *src) { - char msg[160]; + char msg[320]; int s = resolve(c, bn); if (s < 0) return -1; bn->sym = s; @@ -1177,8 +1263,9 @@ static int chk_borrow_src(C *c, OscNode *bn, const OscType *ty, int *src) return tmismatch(c, bn, c->sym[s].name, msg); } if (!same_arr(&c->sym[s].ty, ty) || (bn->mut != 0) != (ty->ref == OSC_REF_MUT)) { - snprintf(msg, sizeof msg, "borrow does not match %s[%s; %u]", ty->ref == OSC_REF_MUT ? "&mut " : "&", - tname(ty->elem), ty->len); + char tb[72]; + snprintf(msg, sizeof msg, "borrow does not match %s%s", ty->ref == OSC_REF_MUT ? "&mut " : "&", + rty(c, ty, tb, sizeof tb)); return tmismatch(c, bn, c->sym[s].name, msg); } return 0; @@ -1191,7 +1278,7 @@ static int outlives(C *c, const OscNode *at, int s, int b, const char *who, cons if (y->kind == SK_OWNER) tr_ev(c, OSC_EV_RELEASE, y->obj, -1, -1, -1, at->line, 1); else tr_ev(c, OSC_EV_END_BORROW, -1, -1, y->bor, -1, at->line, 1); (void)b; - char msg[160]; + char msg[320]; snprintf(msg, sizeof msg, "borrow of '%s' would outlive it (%s)", y->name, transition); osc_diag_set(c->d, OSC_DIAG_BORROW_OUTLIVES_OWNER, at->line, at->col, y->name, y->line, who, transition, "%s", msg); return -1; @@ -1237,7 +1324,7 @@ static int chk_if(C *c, int i) if (m1 != m2) { int sym = c->obj[o].sym; uint32_t ml = m1 ? s1->obj[o].move_line : c->obj[o].move_line; - char msg[160]; + char msg[320]; snprintf(msg, sizeof msg, "'%s' is moved on only one path of the 'if' at line %u (no drop flags)", sym >= 0 ? c->sym[sym].name : "?", n->line); osc_diag_set(c->d, OSC_DIAG_CONDITIONAL_MOVE, ml, 0, sym >= 0 ? c->sym[sym].name : "?", @@ -1267,7 +1354,7 @@ static int chk_loop_body(C *c, OscNode *n, int is_for) c->loop_depth++; if (is_for) { if (push_scope(c, n->line)) goto out; - OscType ti = {OSC_T_I64, OSC_REF_NONE, OSC_T_VOID, 0}; + OscType ti = {OSC_T_I64, OSC_REF_NONE, OSC_T_VOID, 0, 0}; int s = declare(c, n, SK_SCALAR, &ti, 0); if (s < 0) goto out; n->sym = s; @@ -1320,7 +1407,7 @@ static int chk_return_ensures(C *c, OscNode *n) static int chk_stmt(C *c, int i) { OscNode *n = NODE(i); - char msg[160]; + char msg[320]; switch (n->kind) { case ON_LET: { set_sobj_tok(c, n); @@ -1339,7 +1426,11 @@ static int chk_stmt(C *c, int i) char name[64]; osc_node_name(c->ast, n, name, sizeof name); if (lookup(c, name) >= 0) return declare(c, n, SK_OWNER, &n->ty, 0) < 0 ? -1 : -1; - if (chk(c, n->a, n->ty.elem) < 0) return -1; + if (n->ty.sid) { /* struct literal: field values in source order */ + const OscStruct *st = &c->ast->structs[n->ty.sid - 1]; + for (int fi = n->a; fi >= 0; fi = NODE(fi)->next) + if (chk(c, NODE(fi)->a, st->fields[NODE(fi)->hi].s) < 0) return -1; + } else if (chk(c, n->a, n->ty.elem) < 0) return -1; int s = declare(c, n, SK_OWNER, &n->ty, 0); if (s < 0) return -1; int o = new_obj(c, s, n->line); @@ -1360,7 +1451,8 @@ static int chk_stmt(C *c, int i) sn->sym = src; n->sym2 = src; if (c->sym[src].kind != SK_OWNER || !same_arr(&c->sym[src].ty, &n->ty)) { - snprintf(msg, sizeof msg, "'%s' is not an owner of [%s; %u]", c->sym[src].name, tname(n->ty.elem), n->ty.len); + char tb[72]; + snprintf(msg, sizeof msg, "'%s' is not an owner of %s", c->sym[src].name, rty(c, &n->ty, tb, sizeof tb)); return tmismatch(c, sn, c->sym[src].name, msg); } int o = new_obj(c, -1, n->line); @@ -1444,14 +1536,24 @@ static int chk_stmt(C *c, int i) if (s < 0) return -1; n->sym = s; Sym *y = &c->sym[s]; - if (y->kind == SK_SCALAR) { - snprintf(msg, sizeof msg, "'%s' is a scalar, not an array", y->name); + if (y->kind == SK_SCALAR || y->ty.sid) { + snprintf(msg, sizeof msg, "'%s' is a %s, not an array", y->name, y->ty.sid ? "struct" : "scalar"); return tmismatch(c, n, y->name, msg); } if (chk_index(c, s, n->a, n)) return -1; if (chk(c, n->b, y->ty.elem) < 0) return -1; return access(c, s, 1, n->line, n->col); } + case ON_FSTORE: { + set_sobj_tok(c, n); + int s = resolve(c, n); + if (s < 0) return -1; + n->sym = s; + const OscField *fd = chk_field(c, n, s); + if (!fd) return -1; + if (chk(c, n->b, fd->s) < 0) return -1; + return access(c, s, 1, n->line, n->col); + } case ON_IF: return chk_if(c, i); case ON_WHILE: diff --git a/src/compiler/osc_diag.c b/src/compiler/osc_diag.c index 4df5ba1a0..0145fd737 100644 --- a/src/compiler/osc_diag.c +++ b/src/compiler/osc_diag.c @@ -12,7 +12,8 @@ static const char *const kind_names[OSC_DIAG__COUNT] = { "NONE", "SYNTAX", "UNSUPPORTED", "CAPACITY", "UNDEFINED_NAME", "REDEFINED_NAME", "TYPE_MISMATCH", "AMBIGUOUS_WIDTH", "OVERFLOW_UNSAFE", "UNBOUNDED_LOOP", "MISSING_RETURN", "USE_AFTER_MOVE", "CONDITIONAL_MOVE", "MUTABLE_ALIAS", "BORROW_OUTLIVES_OWNER", "STATIC_OUT_OF_BOUNDS", "IMMUTABLE_ASSIGN", - "READ_ONLY_BORROW", "CONTRACT_INVALID", "CONTRACT_VIOLATION"}; + "READ_ONLY_BORROW", "CONTRACT_INVALID", "CONTRACT_VIOLATION", "UNKNOWN_FIELD", "DUPLICATE_FIELD", + "MISSING_FIELD", "UNDEFINED_TYPE", "RECURSIVE_STRUCT"}; const char *osc_diag_kind_name(int kind) { diff --git a/src/compiler/osc_diag.h b/src/compiler/osc_diag.h index f74d917d4..8e74e6eb6 100644 --- a/src/compiler/osc_diag.h +++ b/src/compiler/osc_diag.h @@ -30,6 +30,12 @@ typedef enum { /* appended for OSC-2 contracts (docs/osc/OSC-2-DESIGN.md section 1) */ OSC_DIAG_CONTRACT_INVALID, /* ill-formed clause: result in requires / void fn, call, own read */ OSC_DIAG_CONTRACT_VIOLATION, /* clause decided false at compile time (constant folding) */ + /* appended for OSC-2 structs (docs/osc/OSC-2-DESIGN.md section 2) */ + OSC_DIAG_UNKNOWN_FIELD, /* s.f / literal names a field the struct does not declare */ + OSC_DIAG_DUPLICATE_FIELD, /* field declared twice, or initialised twice in a literal */ + OSC_DIAG_MISSING_FIELD, /* struct literal leaves a field uninitialised */ + OSC_DIAG_UNDEFINED_TYPE, /* struct type name not declared (before use) */ + OSC_DIAG_RECURSIVE_STRUCT, /* struct field names the struct itself */ OSC_DIAG__COUNT } OscDiagKind; diff --git a/src/compiler/osc_interp.c b/src/compiler/osc_interp.c index b3b3aac67..7529f941e 100644 --- a/src/compiler/osc_interp.c +++ b/src/compiler/osc_interp.c @@ -116,6 +116,22 @@ static uint64_t *elem(Ctx *c, const OscType *rt_, uint64_t base, OscScalar ti, u return (uint64_t *)(uintptr_t)base + idx; } +/* OSC-2 structs: pointer to cell (field off + element idx) of struct ref base; + * an array field index obeys the same bounds rule as elem() against alen */ +static uint64_t *fcell(Ctx *c, const OscType *rt_, uint64_t base, unsigned field, bool has_ix, OscScalar ti, + uint64_t idx) { + const OscField *fd = &c->u->structs[rt_->sid - 1].fields[field]; + uint64_t cell = fd->off; + if (has_ix) { + if (osc_scalar_signed(ti) && (int64_t)idx < 0) trap(c, OSC_TRAP_BOUNDS); + if (idx >= fd->alen) trap(c, OSC_TRAP_BOUNDS); + cell += idx; + } + uintptr_t lo = (uintptr_t)&c->rt->cells[0][0], hi = lo + sizeof c->rt->cells; + if ((uintptr_t)base < lo || (uintptr_t)base + 8 * (cell + 1) > hi) trap(c, OSC_TRAP_RUNTIME); + return (uint64_t *)(uintptr_t)base + cell; +} + static uint64_t run_func(Ctx *c, int fi, const uint64_t *args) { const OscFunc *f = &c->u->funcs[fi]; const OscType *T = f->vtype; @@ -140,6 +156,14 @@ static uint64_t run_func(Ctx *c, int fi, const uint64_t *args) { case OSC_I_RELEASE: osc_rt_release(c->rt, v[in->a], T[in->a].len); break; case OSC_I_LOAD: v[in->dst] = *elem(c, &T[in->a], v[in->a], T[in->b].s, v[in->b]); break; case OSC_I_STORE: *elem(c, &T[in->a], v[in->a], T[in->b].s, v[in->b]) = v[in->c]; break; + case OSC_I_FLOAD: + v[in->dst] = *fcell(c, &T[in->a], v[in->a], (unsigned)in->imm, in->b >= 0, + in->b >= 0 ? T[in->b].s : OSC_T_VOID, in->b >= 0 ? v[in->b] : 0); + break; + case OSC_I_FSTORE: + *fcell(c, &T[in->a], v[in->a], (unsigned)in->imm, in->b >= 0, in->b >= 0 ? T[in->b].s : OSC_T_VOID, + in->b >= 0 ? v[in->b] : 0) = v[in->c]; + break; case OSC_I_CALL: { uint64_t ca[OSC_MAX_PARAMS] = {0}; for (int k = 0; k < in->nargs; k++) ca[k] = v[in->args[k]]; diff --git a/src/compiler/osc_ir.c b/src/compiler/osc_ir.c index eb5248979..13a4d8980 100644 --- a/src/compiler/osc_ir.c +++ b/src/compiler/osc_ir.c @@ -43,19 +43,26 @@ static int vfail(char *err, size_t n, const char *fmt, ...) { static bool is_value_scalar(OscScalar s) { return s >= OSC_T_BOOL && s <= OSC_T_I64; } static bool type_is_scalar(const OscType *t) { - return is_value_scalar(t->s) && t->ref == OSC_REF_NONE && t->elem == OSC_T_VOID && t->len == 0; + return is_value_scalar(t->s) && t->ref == OSC_REF_NONE && t->elem == OSC_T_VOID && t->len == 0 && t->sid == 0; } +/* The unit whose struct table type_is_ref consults (set by osc_ir_validate; + * validation is single-threaded per call). */ +static const OscUnit *g_unit; static bool type_is_ref(const OscType *t) { - return t->s == OSC_T_REF && t->ref >= OSC_REF_OWN && t->ref <= OSC_REF_MUT && - is_value_scalar(t->elem) && t->len >= 1 && t->len <= OSC_MAX_ARRAY_LEN; + if (t->s != OSC_T_REF || t->ref < OSC_REF_OWN || t->ref > OSC_REF_MUT) return false; + if (t->sid == 0) return is_value_scalar(t->elem) && t->len >= 1 && t->len <= OSC_MAX_ARRAY_LEN; + return g_unit && t->sid <= g_unit->nstructs && t->elem == OSC_T_VOID && + t->len == g_unit->structs[t->sid - 1].ncells; } +static bool type_is_struct_ref(const OscType *t) { return type_is_ref(t) && t->sid != 0; } +static bool type_is_array_ref(const OscType *t) { return type_is_ref(t) && t->sid == 0; } static bool type_eq(const OscType *a, const OscType *b) { - return a->s == b->s && a->ref == b->ref && a->elem == b->elem && a->len == b->len; + return a->s == b->s && a->ref == b->ref && a->elem == b->elem && a->len == b->len && a->sid == b->sid; } /* may a value of REF type `src` be bound to a REF slot of type `dst`? */ static bool ref_bindable(const OscType *dst, const OscType *src) { if (!type_is_ref(dst) || !type_is_ref(src)) return false; - if (dst->elem != src->elem || dst->len != src->len) return false; + if (dst->elem != src->elem || dst->len != src->len || dst->sid != src->sid) return false; switch (dst->ref) { case OSC_REF_OWN: return src->ref == OSC_REF_OWN; /* move */ case OSC_REF_MUT: return src->ref == OSC_REF_OWN || src->ref == OSC_REF_MUT; @@ -84,6 +91,8 @@ static int insn_uses(const OscInsn *in, int16_t *u) { case OSC_I_CBR: u[k++] = in->a; break; case OSC_I_BIN: case OSC_I_CMP: case OSC_I_LOAD: u[k++] = in->a; u[k++] = in->b; break; case OSC_I_STORE: u[k++] = in->a; u[k++] = in->b; u[k++] = in->c; break; + case OSC_I_FLOAD: u[k++] = in->a; if (in->b >= 0) u[k++] = in->b; break; + case OSC_I_FSTORE: u[k++] = in->a; if (in->b >= 0) u[k++] = in->b; u[k++] = in->c; break; case OSC_I_CALL: for (int i = 0; i < in->nargs && i < OSC_MAX_PARAMS; i++) u[k++] = in->args[i]; break; case OSC_I_RET: if (in->a >= 0) u[k++] = in->a; break; default: break; @@ -93,7 +102,7 @@ static int insn_uses(const OscInsn *in, int16_t *u) { static int insn_def(const OscInsn *in) { switch (in->op) { case OSC_I_CONST: case OSC_I_MOV: case OSC_I_BIN: case OSC_I_UN: case OSC_I_CMP: - case OSC_I_CAST: case OSC_I_ALLOC: case OSC_I_LOAD: return in->dst; + case OSC_I_CAST: case OSC_I_ALLOC: case OSC_I_LOAD: case OSC_I_FLOAD: return in->dst; case OSC_I_CALL: return in->dst; /* may be -1 */ default: return -1; } @@ -158,7 +167,10 @@ static int validate_insn(const OscUnit *u, int fi, const OscFunc *f, uint32_t ii case OSC_I_ALLOC: NEED(VR(in->dst) && VR(in->a), "ALLOC bad vreg", fi, ii); NEED(type_is_ref(&T[in->dst]) && T[in->dst].ref == OSC_REF_OWN, "ALLOC dst not an owner", fi, ii); - NEED(type_is_scalar(&T[in->a]) && T[in->a].s == T[in->dst].elem, "ALLOC init type != elem type", fi, ii); + if (T[in->dst].sid) /* struct: every cell starts at 0 (u64 0), fields are stored next */ + NEED(type_is_scalar(&T[in->a]) && T[in->a].s == OSC_T_U64, "ALLOC struct init not u64", fi, ii); + else + NEED(type_is_scalar(&T[in->a]) && T[in->a].s == T[in->dst].elem, "ALLOC init type != elem type", fi, ii); break; case OSC_I_RELEASE: NEED(VR(in->a), "RELEASE bad vreg", fi, ii); @@ -166,17 +178,33 @@ static int validate_insn(const OscUnit *u, int fi, const OscFunc *f, uint32_t ii break; case OSC_I_LOAD: NEED(VR(in->dst) && VR(in->a) && VR(in->b), "LOAD bad vreg", fi, ii); - NEED(type_is_ref(&T[in->a]), "LOAD base not a ref", fi, ii); + NEED(type_is_array_ref(&T[in->a]), "LOAD base not an array ref", fi, ii); NEED(type_is_scalar(&T[in->b]) && osc_scalar_is_int(T[in->b].s), "LOAD index not integer", fi, ii); NEED(type_is_scalar(&T[in->dst]) && T[in->dst].s == T[in->a].elem, "LOAD dst type != elem", fi, ii); break; case OSC_I_STORE: NEED(VR(in->a) && VR(in->b) && VR(in->c), "STORE bad vreg", fi, ii); - NEED(type_is_ref(&T[in->a]) && (T[in->a].ref == OSC_REF_OWN || T[in->a].ref == OSC_REF_MUT), + NEED(type_is_array_ref(&T[in->a]) && (T[in->a].ref == OSC_REF_OWN || T[in->a].ref == OSC_REF_MUT), "STORE through a shared borrow or non-ref", fi, ii); NEED(type_is_scalar(&T[in->b]) && osc_scalar_is_int(T[in->b].s), "STORE index not integer", fi, ii); NEED(type_is_scalar(&T[in->c]) && T[in->c].s == T[in->a].elem, "STORE value type != elem", fi, ii); break; + case OSC_I_FLOAD: case OSC_I_FSTORE: { + bool ld = in->op == OSC_I_FLOAD; + NEED(VR(in->a) && type_is_struct_ref(&T[in->a]), "%s base not a struct ref", fi, ii, ld ? "FLOAD" : "FSTORE"); + const OscStruct *st = &u->structs[T[in->a].sid - 1]; + NEED(in->imm < st->nfields, "%s field %llu out of range", fi, ii, ld ? "FLOAD" : "FSTORE", + (unsigned long long)in->imm); + const OscField *fd = &st->fields[in->imm]; + if (fd->alen) NEED(VR(in->b) && type_is_scalar(&T[in->b]) && osc_scalar_is_int(T[in->b].s), + "field index not integer", fi, ii); + else NEED(in->b == -1, "scalar field with an index", fi, ii); + int v = ld ? in->dst : in->c; + NEED(VR(v) && type_is_scalar(&T[v]) && T[v].s == fd->s, "field value type != field type", fi, ii); + if (!ld) NEED(T[in->a].ref == OSC_REF_OWN || T[in->a].ref == OSC_REF_MUT, + "FSTORE through a shared borrow", fi, ii); + break; + } case OSC_I_CALL: { NEED(in->callee >= 0 && in->callee < fi, "CALL to function %d not defined earlier", fi, ii, in->callee); const OscFunc *g = &u->funcs[in->callee]; @@ -223,7 +251,7 @@ static int validate_func(const OscUnit *u, int fi, char *err, size_t n) { return vfail(err, n, "func %d: contract text not terminated", fi); if (f->nparams > OSC_MAX_PARAMS) return vfail(err, n, "func %d: %u params > %d", fi, f->nparams, OSC_MAX_PARAMS); if (f->nvregs > OSC_MAX_VREGS || f->nparams > f->nvregs) return vfail(err, n, "func %d: bad nvregs", fi); - if (!(f->ret.s == OSC_T_VOID && f->ret.ref == OSC_REF_NONE && f->ret.elem == OSC_T_VOID && f->ret.len == 0) && + if (!(f->ret.s == OSC_T_VOID && f->ret.ref == OSC_REF_NONE && f->ret.elem == OSC_T_VOID && f->ret.len == 0 && f->ret.sid == 0) && !type_is_scalar(&f->ret)) return vfail(err, n, "func %d: return type must be void or scalar", fi); for (int v = 0; v < f->nvregs; v++) @@ -301,9 +329,37 @@ static int validate_func(const OscUnit *u, int fi, char *err, size_t n) { return rc; } +/* struct table: names terminated and unique, fields well-typed, offsets are + * the prefix sums of the field cell counts (no padding), 1..64 cells. */ +static int validate_structs(const OscUnit *u, char *err, size_t n) { + if (u->nstructs > OSC_MAX_STRUCTS) return vfail(err, n, "too many structs"); + for (int k = 0; k < u->nstructs; k++) { + const OscStruct *s = &u->structs[k]; + if (!str_ok(s->name, sizeof s->name) || !s->name[0]) return vfail(err, n, "struct %d: bad name", k); + for (int j = 0; j < k; j++) + if (!strcmp(u->structs[j].name, s->name)) return vfail(err, n, "struct %d: duplicate name", k); + if (s->nfields < 1 || s->nfields > OSC_MAX_FIELDS) return vfail(err, n, "struct %d: bad field count", k); + unsigned off = 0; + for (int f = 0; f < s->nfields; f++) { + const OscField *fd = &s->fields[f]; + if (!str_ok(fd->name, sizeof fd->name) || !fd->name[0]) return vfail(err, n, "struct %d field %d: bad name", k, f); + for (int g = 0; g < f; g++) + if (!strcmp(s->fields[g].name, fd->name)) return vfail(err, n, "struct %d field %d: duplicate", k, f); + if (!is_value_scalar(fd->s) || fd->alen > OSC_MAX_ARRAY_LEN) + return vfail(err, n, "struct %d field %d: bad type", k, f); + if (fd->off != off) return vfail(err, n, "struct %d field %d: offset %u != %u", k, f, fd->off, off); + off += fd->alen ? fd->alen : 1; + } + if (off != s->ncells || off < 1 || off > OSC_MAX_ARRAY_LEN) return vfail(err, n, "struct %d: bad size", k); + } + return 0; +} + int osc_ir_validate(const OscUnit *u, char *err, size_t n) { if (!u) return vfail(err, n, "null unit"); if (u->nfuncs > OSC_MAX_FUNCS) return vfail(err, n, "too many functions"); + g_unit = u; + if (validate_structs(u, err, n)) return -1; for (int fi = 0; fi < u->nfuncs; fi++) if (validate_func(u, fi, err, n)) return -1; if (err && n) err[0] = 0; @@ -337,7 +393,10 @@ static void w64(W *w, uint64_t v) { static void wvr(W *w, int16_t v) { w16(w, (uint16_t)v); } /* two's complement LE; -1 = 0xFFFF */ static void wtype(W *w, const OscType *t) { w8(w, (uint8_t)t->s); - if (t->s == OSC_T_REF) { w8(w, (uint8_t)t->ref); w8(w, (uint8_t)t->elem); w16(w, t->len); } + if (t->s == OSC_T_REF) { + w8(w, (uint8_t)t->ref); w8(w, (uint8_t)t->elem); w16(w, t->len); + if (t->sid) w8(w, t->sid); /* struct ref (elem VOID); array refs encode as OSC-1 */ + } } static void wtext(W *w, const char *s, size_t cap) { size_t k = strnlen(s, cap); @@ -346,8 +405,26 @@ static void wtext(W *w, const char *s, size_t cap) { } static void encode_unit(const OscUnit *u, W *w) { - static const uint8_t magic[8] = {'O', 'S', 'C', '1', 'I', 'R', 0, 1}; /* format version 1 */ + /* format version 1 (OSC-1, no structs) / 2 (OSC-2 structs: struct table + * after the magic). A unit without structs encodes exactly as version 1. */ + uint8_t magic[8] = {'O', 'S', 'C', '1', 'I', 'R', 0, 1}; + if (u->nstructs) magic[7] = 2; wbytes(w, magic, sizeof magic); + if (u->nstructs) { + w8(w, u->nstructs); + for (int k = 0; k < u->nstructs; k++) { + const OscStruct *s = &u->structs[k]; + size_t sl = strnlen(s->name, sizeof s->name); + w8(w, sl); wbytes(w, s->name, sl); + w8(w, s->nfields); w16(w, s->ncells); + for (int f = 0; f < s->nfields; f++) { + const OscField *fd = &s->fields[f]; + size_t fl = strnlen(fd->name, sizeof fd->name); + w8(w, fl); wbytes(w, fd->name, fl); + w8(w, (uint8_t)fd->s); w16(w, fd->alen); w16(w, fd->off); + } + } + } w16(w, u->nfuncs); for (int fi = 0; fi < u->nfuncs; fi++) { const OscFunc *f = &u->funcs[fi]; @@ -372,6 +449,8 @@ static void encode_unit(const OscUnit *u, W *w) { case OSC_I_RELEASE: wvr(w, in->a); break; case OSC_I_LOAD: wvr(w, in->dst); wvr(w, in->a); wvr(w, in->b); break; case OSC_I_STORE: wvr(w, in->a); wvr(w, in->b); wvr(w, in->c); break; + case OSC_I_FLOAD: wvr(w, in->dst); wvr(w, in->a); wvr(w, in->b); w8(w, in->imm); break; + case OSC_I_FSTORE: wvr(w, in->a); wvr(w, in->b); wvr(w, in->c); w8(w, in->imm); break; case OSC_I_CALL: wvr(w, in->dst); w16(w, (uint16_t)in->callee); w8(w, in->nargs); for (int k = 0; k < in->nargs; k++) wvr(w, in->args[k]); diff --git a/src/compiler/osc_ir.h b/src/compiler/osc_ir.h index ce006ac78..4bb895286 100644 --- a/src/compiler/osc_ir.h +++ b/src/compiler/osc_ir.h @@ -36,6 +36,8 @@ #define OSC_MAX_ARRAY_LEN 64 /* elements per unique allocation */ #define OSC_NAME_MAX 64 #define OSC_CLAUSE_MAX 256 /* requires/ensures source text (recorded; enforced by the front end, OSC-2) */ +#define OSC_MAX_STRUCTS 16 /* OSC-2 structs (docs/osc/OSC-2-DESIGN.md section 2) */ +#define OSC_MAX_FIELDS 16 /* fields per struct */ /* ---- types ------------------------------------------------------------ */ typedef enum { @@ -57,7 +59,10 @@ typedef struct { OscScalar s; /* for OSC_T_REF: the elements are `elem` */ OscRefKind ref; /* OSC_REF_NONE unless s == OSC_T_REF */ OscScalar elem; /* element scalar type (integer or bool) */ - uint16_t len; /* static array length N, 1..OSC_MAX_ARRAY_LEN */ + uint16_t len; /* static array length N, 1..OSC_MAX_ARRAY_LEN; */ + /* for a struct ref: the struct's cell count */ + uint8_t sid; /* OSC-2 structs: 0 = array ref (or scalar); */ + /* k+1 = ref to OscUnit.structs[k] (elem VOID) */ } OscType; /* ---- instructions ----------------------------------------------------- */ @@ -82,7 +87,12 @@ typedef enum { /* terminators */ OSC_I_BR, /* goto blk_t */ OSC_I_CBR, /* if a(bool) goto blk_t else goto blk_f */ - OSC_I_RET /* return a (or nothing if a == -1 and function is void) */ + OSC_I_RET, /* return a (or nothing if a == -1 and function is void) */ + /* appended for OSC-2 structs (docs/osc/OSC-2-DESIGN.md section 2) */ + OSC_I_FLOAD, /* dst = field imm of struct ref a; for an array field, element */ + /* b (any integer type, else -1); b out of range: TRAP_BOUNDS */ + OSC_I_FSTORE /* field imm of struct ref a (element b, as FLOAD) = c ; */ + /* a is REF OWN or REF MUT */ } OscOp; typedef enum { @@ -159,9 +169,29 @@ typedef struct { char ensures_text[OSC_CLAUSE_MAX]; } OscFunc; +/* OSC-2 structs: fixed layout, no padding. Field k occupies cells + * [off, off + max(alen, 1)) of the struct's allocation; every cell is one + * 8-byte canonical value; offsets are the prefix sums of the field cell + * counts in declaration order; ncells = the total (1..OSC_MAX_ARRAY_LEN). */ +typedef struct { + char name[OSC_NAME_MAX]; + OscScalar s; /* integer scalar or bool */ + uint16_t alen; /* 0 = scalar field; N = inline array [s; N] */ + uint16_t off; /* first cell */ +} OscField; + +typedef struct { + char name[OSC_NAME_MAX]; + uint8_t nfields; /* 1..OSC_MAX_FIELDS */ + uint16_t ncells; /* size in bytes = 8 * ncells */ + OscField fields[OSC_MAX_FIELDS]; +} OscStruct; + typedef struct { OscFunc funcs[OSC_MAX_FUNCS]; uint16_t nfuncs; + OscStruct structs[OSC_MAX_STRUCTS]; /* OSC-2; 0 structs = OSC-1 encoding */ + uint8_t nstructs; } OscUnit; /* Structural validation (types agree, vregs in range, every block ends in one diff --git a/src/compiler/osc_lex.c b/src/compiler/osc_lex.c index 9d741ec03..e515e7c73 100644 --- a/src/compiler/osc_lex.c +++ b/src/compiler/osc_lex.c @@ -12,7 +12,7 @@ static const struct { const char *w; int k; } keywords[] = { {"in", OT_IN}, {"return", OT_RETURN}, {"requires", OT_REQUIRES}, {"ensures", OT_ENSURES}, {"as", OT_AS}, {"true", OT_TRUE}, {"false", OT_FALSE}, {"u8", OT_U8}, {"u16", OT_U16}, {"u32", OT_U32}, {"u64", OT_U64}, {"i8", OT_I8}, {"i16", OT_I16}, {"i32", OT_I32}, - {"i64", OT_I64}, {"bool", OT_BOOL}, + {"i64", OT_I64}, {"bool", OT_BOOL}, {"struct", OT_STRUCT}, }; /* Reserved: destruction words (destruction has no surface syntax, III.8) and @@ -20,7 +20,7 @@ static const struct { const char *w; int k; } keywords[] = { static const char *const reserved[] = { "free", "drop", "delete", "dealloc", "effect", "cap", "capability", "mint", "revoke", "grant", "publish", "promote", "authority", "admin", - "syscall", "io", "print", "import", "extern", "unsafe", "asm", "loop", "match", "struct", "enum", + "syscall", "io", "print", "import", "extern", "unsafe", "asm", "loop", "match", "enum", "var", "const", "self", "rec", "break", "continue", }; @@ -30,6 +30,7 @@ static const char *const kind_names[OT__COUNT] = { "ensures", "as", "true", "false", "u8", "u16", "u32", "u64", "i8", "i16", "i32", "i64", "bool", "(", ")", "{", "}", "[", "]", ",", ";", ":", "->", "..", "=", "+", "-", "*", "/", "%", "&", "|", "^", "~", "!", "&&", "||", "<<", ">>", "==", "!=", "<", "<=", ">", ">=", + "struct", ".", }; const char *osc_tok_kind_name(int kind) @@ -162,7 +163,7 @@ int osc_lex(const char *src, size_t len, OscToken *toks, uint32_t cap, uint32_t {"]", OT_RBRACK}, {",", OT_COMMA}, {";", OT_SEMI}, {":", OT_COLON}, {"=", OT_ASSIGN}, {"+", OT_PLUS}, {"-", OT_MINUS}, {"*", OT_STAR}, {"/", OT_SLASH}, {"%", OT_PERCENT}, {"&", OT_AMP}, {"|", OT_PIPE}, {"^", OT_CARET}, {"~", OT_TILDE}, {"!", OT_BANG}, - {"<", OT_LT}, {">", OT_GT}, + {"<", OT_LT}, {">", OT_GT}, {".", OT_DOT}, }; int found = 0; for (size_t k = 0; k < sizeof punct / sizeof punct[0]; k++) { diff --git a/src/compiler/osc_lex.h b/src/compiler/osc_lex.h index ba0bb4e1a..263458544 100644 --- a/src/compiler/osc_lex.h +++ b/src/compiler/osc_lex.h @@ -25,6 +25,8 @@ typedef enum { OT_ARROW, OT_DOTDOT, OT_ASSIGN, OT_PLUS, OT_MINUS, OT_STAR, OT_SLASH, OT_PERCENT, OT_AMP, OT_PIPE, OT_CARET, OT_TILDE, OT_BANG, OT_ANDAND, OT_OROR, OT_SHL, OT_SHR, OT_EQ, OT_NE, OT_LT, OT_LE, OT_GT, OT_GE, + /* OSC-2 item 2 (structs), appended */ + OT_STRUCT, OT_DOT, OT__COUNT } OscTokKind; diff --git a/src/compiler/osc_lower.c b/src/compiler/osc_lower.c index b6fc71ae4..1ae17e180 100644 --- a/src/compiler/osc_lower.c +++ b/src/compiler/osc_lower.c @@ -43,7 +43,7 @@ static int newv(L *l, const OscType *t, uint32_t line) } static int newvs(L *l, OscScalar s, uint32_t line) { - OscType t = {s, OSC_REF_NONE, OSC_T_VOID, 0}; + OscType t = {s, OSC_REF_NONE, OSC_T_VOID, 0, 0}; return newv(l, &t, line); } @@ -176,6 +176,16 @@ static int expr(L *l, int i) x->b = (int16_t)ix; return r; } + case ON_FIELD: { + int ix = n->a >= 0 ? expr(l, n->a) : -1; + int r = newvs(l, n->ty.s, n->line); + OscInsn *x = emit(l, OSC_I_FLOAD, n->line); + x->dst = (int16_t)r; + x->a = l->vreg[n->sym]; + x->b = (int16_t)ix; + x->imm = (uint64_t)n->hi; + return r; + } case ON_CALL: return call(l, i, 1); case ON_CAST: { int a = expr(l, n->a); @@ -270,6 +280,37 @@ static void stmt(L *l, int i) break; } case ON_LET_ALLOC: { + if (n->ty.sid) { + /* struct literal: field values in source order, then one + * allocation (cells zeroed), then one FSTORE per cell */ + int vals[OSC_MAX_FIELDS], nv = 0; + for (int fi = n->a; fi >= 0; fi = NODE(fi)->next) vals[nv++] = expr(l, NODE(fi)->a); + int zero = kconst(l, OSC_T_U64, 0, n->line); + int r = newv(l, &n->ty, n->line); + l->vreg[n->sym] = (int16_t)r; + OscInsn *x = emit(l, OSC_I_ALLOC, n->line); + x->dst = (int16_t)r; + x->a = (int16_t)zero; + int ix = -1, k = 0; + for (int fi = n->a; fi >= 0; fi = NODE(fi)->next, k++) { + const OscNode *fn = NODE(fi); + unsigned cnt = fn->flag ? (unsigned)fn->ival : 1; + for (unsigned e = 0; e < cnt; e++) { + if (fn->flag) { + if (ix < 0) ix = newvs(l, OSC_T_U64, n->line); + OscInsn *c = emit(l, OSC_I_CONST, fn->line); + c->dst = (int16_t)ix; + c->imm = e; + } + x = emit(l, OSC_I_FSTORE, fn->line); + x->a = (int16_t)r; + x->b = (int16_t)(fn->flag ? ix : -1); + x->c = (int16_t)vals[k]; + x->imm = (uint64_t)fn->hi; + } + } + break; + } int v = expr(l, n->a); int r = newv(l, &n->ty, n->line); l->vreg[n->sym] = (int16_t)r; @@ -304,6 +345,16 @@ static void stmt(L *l, int i) x->c = (int16_t)v; break; } + case ON_FSTORE: { + int ix = n->a >= 0 ? expr(l, n->a) : -1; + int v = expr(l, n->b); + OscInsn *x = emit(l, OSC_I_FSTORE, n->line); + x->a = l->vreg[n->sym]; + x->b = (int16_t)ix; + x->c = (int16_t)v; + x->imm = (uint64_t)n->hi; + break; + } case ON_IF: { int c = expr(l, n->a); int tb = newblk(l, n->line); @@ -491,6 +542,8 @@ int osc_lower(const OscAst *ast, OscUnit *out, OscDiag *d) rc = lower_fn(l, (int)fi); } out->nfuncs = (uint16_t)ast->nfns; + memcpy(out->structs, ast->structs, sizeof out->structs); + out->nstructs = ast->nstructs; free(l); return rc; } diff --git a/src/compiler/osc_parse.c b/src/compiler/osc_parse.c index b25465491..30080d075 100644 --- a/src/compiler/osc_parse.c +++ b/src/compiler/osc_parse.c @@ -130,18 +130,297 @@ static int parse_arr_body(P *p, OscType *ty, OscRefKind rk) return 0; } -/* ptype: scalar | own[..] | &[..] | &mut[..] */ +static int parse_expr(P *p); + +/* ---- OSC-2 structs (docs/osc/OSC-2-DESIGN.md section 2) ---------------- */ +static int tok_eq(P *p, const OscToken *t, const char *s) +{ + size_t n = strlen(s); + return t->kind == OT_NAME && t->len == n && memcmp(p->ast->src + t->off, s, n) == 0; +} + +/* index of the struct named by token t, -1 if none is declared (yet) */ +static int find_struct(P *p, const OscToken *t) +{ + for (int k = 0; k < p->ast->nstructs; k++) + if (tok_eq(p, t, p->ast->structs[k].name)) return k; + return -1; +} + +/* index of field `t` in struct k, -1 if none */ +static int find_field(P *p, int k, const OscToken *t) +{ + const OscStruct *s = &p->ast->structs[k]; + for (int f = 0; f < s->nfields; f++) + if (tok_eq(p, t, s->fields[f].name)) return f; + return -1; +} + +static int undefined_type(P *p, const OscToken *t) +{ + char nm[64]; + osc_tok_text(p->ast->src, t, nm, sizeof nm); + osc_diag_set(p->d, OSC_DIAG_UNDEFINED_TYPE, t->line, t->col, nm, 0, NULL, "undefined struct type", + "struct type '%s' is not declared (structs are declared before use)", nm); + return -1; +} + +/* a struct named where a value type is expected (let x: S, p: S, -> S) */ +static int struct_by_value(P *p, const OscToken *t, const char *transition) +{ + char nm[64]; + osc_tok_text(p->ast->src, t, nm, sizeof nm); + osc_diag_set(p->d, OSC_DIAG_UNSUPPORTED, t->line, t->col, nm, p->ast->struct_line[find_struct(p, t)], NULL, + transition, "struct '%s' is used through 'own', '&' or '&mut' only; it is never a value " + "or a return type", nm); + return -1; +} + +/* NAME with the ref kind already consumed: a reference to a declared struct */ +static int parse_struct_ref(P *p, OscType *ty, OscRefKind rk) +{ + const OscToken *t = cur(p); + int k = find_struct(p, t); + if (k < 0) return undefined_type(p, t); + adv(p); + memset(ty, 0, sizeof *ty); + ty->s = OSC_T_REF; + ty->ref = rk; + ty->elem = OSC_T_VOID; + ty->len = p->ast->structs[k].ncells; + ty->sid = (uint8_t)(k + 1); + return 0; +} + +/* "[" ... (array) or NAME (struct), ref kind already consumed */ +static int parse_ref_body(P *p, OscType *ty, OscRefKind rk) +{ + if (at(p, OT_NAME)) return parse_struct_ref(p, ty, rk); + return parse_arr_body(p, ty, rk); +} + +/* ptype: scalar | own[..] | &[..] | &mut[..] | own S | &S | &mut S */ static int parse_ptype(P *p, OscType *ty) { - if (at(p, OT_OWN)) { adv(p); return parse_arr_body(p, ty, OSC_REF_OWN); } + if (at(p, OT_OWN)) { adv(p); return parse_ref_body(p, ty, OSC_REF_OWN); } if (at(p, OT_AMP)) { adv(p); - if (at(p, OT_MUT)) { adv(p); return parse_arr_body(p, ty, OSC_REF_MUT); } - return parse_arr_body(p, ty, OSC_REF_SHARED); + if (at(p, OT_MUT)) { adv(p); return parse_ref_body(p, ty, OSC_REF_MUT); } + return parse_ref_body(p, ty, OSC_REF_SHARED); } + if (at(p, OT_NAME) && find_struct(p, cur(p)) >= 0) return struct_by_value(p, cur(p), "struct by value"); return parse_scalar(p, ty); } +/* struct_decl = "struct" NAME "{" field { "," field } [","] "}" ; + * field = NAME ":" ( scalar | "[" scalar ";" INT "]" ) */ +static int parse_struct(P *p) +{ + OscAst *a = p->ast; + const OscToken *st = cur(p); + adv(p); + if (!at(p, OT_NAME)) return syntax(p, "a struct name"); + const OscToken *nt = cur(p); + char sname[64]; + osc_tok_text(a->src, nt, sname, sizeof sname); + int prev = find_struct(p, nt); + if (prev >= 0) { + osc_diag_set(p->d, OSC_DIAG_REDEFINED_NAME, nt->line, nt->col, sname, a->struct_line[prev], NULL, + "struct declared twice", "struct '%s' is already declared", sname); + return -1; + } + if (a->nstructs >= OSC_MAX_STRUCTS) { + osc_diag_set(p->d, OSC_DIAG_CAPACITY, nt->line, nt->col, sname, 0, NULL, "struct capacity", + "more than %d structs", OSC_MAX_STRUCTS); + return -1; + } + adv(p); + OscStruct *s = &a->structs[a->nstructs]; + memset(s, 0, sizeof *s); + snprintf(s->name, sizeof s->name, "%s", sname); + if (expect(p, OT_LBRACE)) return -1; + if (at(p, OT_RBRACE)) { + osc_diag_set(p->d, OSC_DIAG_UNSUPPORTED, nt->line, nt->col, sname, 0, NULL, "empty struct", + "struct '%s' declares no fields (1..%d required)", sname, OSC_MAX_FIELDS); + return -1; + } + unsigned cells = 0; + while (!at(p, OT_RBRACE)) { + const OscToken *ft = cur(p); + if (!at(p, OT_NAME)) return syntax(p, "a field name"); + char fname[64]; + osc_tok_text(a->src, ft, fname, sizeof fname); + for (int g = 0; g < s->nfields; g++) + if (!strcmp(s->fields[g].name, fname)) { + osc_diag_set(p->d, OSC_DIAG_DUPLICATE_FIELD, ft->line, ft->col, fname, nt->line, sname, + "field declared twice", "struct '%s' declares field '%s' twice", sname, fname); + return -1; + } + if (s->nfields >= OSC_MAX_FIELDS) { + osc_diag_set(p->d, OSC_DIAG_CAPACITY, ft->line, ft->col, sname, nt->line, fname, "field capacity", + "struct '%s' has more than %d fields", sname, OSC_MAX_FIELDS); + return -1; + } + adv(p); + if (expect(p, OT_COLON)) return -1; + OscField *fd = &s->fields[s->nfields]; + snprintf(fd->name, sizeof fd->name, "%s", fname); + const OscToken *tt = cur(p); + if (at(p, OT_OWN) || at(p, OT_AMP) || at(p, OT_NAME)) { + uint32_t q = p->pos; + while (q < a->ntok && (a->toks[q].kind == OT_OWN || a->toks[q].kind == OT_AMP || a->toks[q].kind == OT_MUT)) q++; + const OscToken *rt = &a->toks[q]; + if (tok_eq(p, rt, sname)) { + osc_diag_set(p->d, OSC_DIAG_RECURSIVE_STRUCT, rt->line, rt->col, sname, nt->line, fname, + "struct field of its own type", "field '%s' of struct '%s' names '%s' itself " + "(no recursive or self-referential structs)", fname, sname, sname); + return -1; + } + if (rt->kind == OT_NAME && find_struct(p, rt) < 0 && tt->kind == OT_NAME) return undefined_type(p, rt); + osc_diag_set(p->d, OSC_DIAG_UNSUPPORTED, tt->line, tt->col, fname, nt->line, sname, + tt->kind == OT_NAME ? "struct-typed field" : "owner or borrow field", + "field '%s': a field is an integer, bool or fixed array [T; N]", fname); + return -1; + } + if (at(p, OT_LBRACK)) { + OscType at_; + if (parse_arr_body(p, &at_, OSC_REF_OWN)) return -1; + fd->s = at_.elem; + fd->alen = at_.len; + } else { + OscType sc; + if (parse_scalar(p, &sc)) return -1; + fd->s = sc.s; + } + fd->off = (uint16_t)cells; + cells += fd->alen ? fd->alen : 1; + if (cells > OSC_MAX_ARRAY_LEN) { + osc_diag_set(p->d, OSC_DIAG_CAPACITY, tt->line, tt->col, sname, nt->line, fname, "struct size", + "struct '%s' needs more than %d cells", sname, OSC_MAX_ARRAY_LEN); + return -1; + } + s->nfields++; + if (at(p, OT_COMMA)) { adv(p); continue; } + if (!at(p, OT_RBRACE)) return syntax(p, "',' or '}' after a field"); + } + adv(p); + s->ncells = (uint16_t)cells; + a->struct_line[a->nstructs++] = st->line; + return 0; +} + +/* struct literal S "{" NAME ":" ( expr | "[" expr ";" INT "]" ) { "," ... } [","] "}" + * as the initialiser of `let x: own S`; cur is S. Fills n->a with ON_FINIT + * nodes. Every field exactly once: UNKNOWN_FIELD / DUPLICATE_FIELD / MISSING_FIELD. */ +static int parse_struct_lit(P *p, int n) +{ + OscAst *a = p->ast; + const OscToken *lt = cur(p); + int k = find_struct(p, lt); + char bname[64]; + osc_node_name(a, N(n), bname, sizeof bname); + if (k < 0) return undefined_type(p, lt); + const OscStruct *s = &a->structs[k]; + if (k + 1 != N(n)->ty.sid) { + osc_diag_set(p->d, OSC_DIAG_TYPE_MISMATCH, lt->line, lt->col, bname, N(n)->line, + s->name, "struct literal of another type", "'%s' is declared own %s, initialised with a %s literal", + bname, a->structs[N(n)->ty.sid - 1].name, s->name); + return -1; + } + adv(p); + if (expect(p, OT_LBRACE)) return -1; + uint32_t seen = 0; + int last = -1; + while (!at(p, OT_RBRACE)) { + const OscToken *ft = cur(p); + if (!at(p, OT_NAME)) return syntax(p, "a field name"); + char fname[64]; + osc_tok_text(a->src, ft, fname, sizeof fname); + int f = find_field(p, k, ft); + if (f < 0) { + osc_diag_set(p->d, OSC_DIAG_UNKNOWN_FIELD, ft->line, ft->col, fname, a->struct_line[k], s->name, + "initialise unknown field", "struct '%s' has no field '%s'", s->name, fname); + return -1; + } + if (seen >> f & 1) { + osc_diag_set(p->d, OSC_DIAG_DUPLICATE_FIELD, ft->line, ft->col, fname, lt->line, s->name, + "field initialised twice", "field '%s' of '%s' is initialised twice", fname, s->name); + return -1; + } + seen |= 1u << f; + int fi = new_node(p, ON_FINIT, ft); + if (fi < 0) return -1; + N(fi)->hi = f; + adv(p); + if (expect(p, OT_COLON)) return -1; + const OscField *fd = &s->fields[f]; + if (fd->alen) { + if (!at(p, OT_LBRACK)) { + const OscToken *t = cur(p); + osc_diag_set(p->d, OSC_DIAG_TYPE_MISMATCH, t->line, t->col, fname, a->struct_line[k], s->name, + "array field without [e; N]", "array field '%s' is initialised with '[value; %u]'", + fname, fd->alen); + return -1; + } + adv(p); + int e = parse_expr(p); + if (e < 0) return -1; + N(fi)->a = e; + if (expect(p, OT_SEMI)) return -1; + if (!at(p, OT_INT)) return syntax(p, "array length INT"); + const OscToken *nt = cur(p); + if (nt->ival != fd->alen) { + osc_diag_set(p->d, OSC_DIAG_TYPE_MISMATCH, nt->line, nt->col, fname, a->struct_line[k], s->name, + "array field length mismatch", "array field '%s' has length %u, initialiser gives %llu", + fname, fd->alen, (unsigned long long)nt->ival); + return -1; + } + N(fi)->flag = 1; + N(fi)->ival = nt->ival; + adv(p); + if (expect(p, OT_RBRACK)) return -1; + } else { + int e = parse_expr(p); + if (e < 0) return -1; + N(fi)->a = e; + } + if (last < 0) N(n)->a = fi; else N(last)->next = fi; + last = fi; + if (at(p, OT_COMMA)) { adv(p); continue; } + if (!at(p, OT_RBRACE)) return syntax(p, "',' or '}' in a struct literal"); + } + for (int f = 0; f < s->nfields; f++) + if (!(seen >> f & 1)) { + const OscToken *t = cur(p); + osc_diag_set(p->d, OSC_DIAG_MISSING_FIELD, t->line, t->col, s->fields[f].name, lt->line, s->name, + "field not initialised", "struct literal of '%s' does not initialise field '%s'", + s->name, s->fields[f].name); + return -1; + } + adv(p); + return 0; +} + +/* NAME "." NAME [ "[" expr "]" ] ; cur is the first NAME. kind = ON_FIELD / ON_FSTORE */ +static int parse_field_ref(P *p, int kind) +{ + int n = new_node(p, kind, cur(p)); + if (n < 0) return -1; + adv(p); + adv(p); /* '.' */ + if (!at(p, OT_NAME)) return syntax(p, "a field name after '.'"); + N(n)->lo = p->pos; + adv(p); + if (at(p, OT_LBRACK)) { + adv(p); + int ix = parse_expr(p); + if (ix < 0) return -1; + N(n)->a = ix; + if (expect(p, OT_RBRACK)) return -1; + } + return n; +} + static int parse_expr(P *p); static int parse_block(P *p); @@ -202,6 +481,7 @@ static int parse_primary(P *p) return n; case OT_NAME: if (peek_kind(p, 1) == OT_LPAREN) return parse_call(p); + if (peek_kind(p, 1) == OT_DOT) return parse_field_ref(p, ON_FIELD); if (peek_kind(p, 1) == OT_LBRACK) { n = new_node(p, ON_INDEX, t); if (n < 0) return -1; @@ -365,9 +645,20 @@ static int parse_stmt(P *p) return -1; } adv(p); - if (parse_arr_body(p, &N(n)->ty, OSC_REF_OWN)) return -1; + if (parse_ref_body(p, &N(n)->ty, OSC_REF_OWN)) return -1; if (expect(p, OT_ASSIGN)) return -1; - if (at(p, OT_ALLOC)) { + if (N(n)->ty.sid && at(p, OT_NAME) && peek_kind(p, 1) == OT_LBRACE) { + N(n)->kind = ON_LET_ALLOC; + if (parse_struct_lit(p, n)) return -1; + } else if (N(n)->ty.sid && at(p, OT_ALLOC)) { + const OscToken *at_ = cur(p); + char nm[64]; + osc_node_name(p->ast, N(n), nm, sizeof nm); + osc_diag_set(p->d, OSC_DIAG_UNSUPPORTED, at_->line, at_->col, nm, 0, + p->ast->structs[N(n)->ty.sid - 1].name, "alloc of a struct", + "a struct owner is created by a struct literal, not alloc"); + return -1; + } else if (at(p, OT_ALLOC)) { adv(p); N(n)->kind = ON_LET_ALLOC; if (expect(p, OT_LPAREN)) return -1; @@ -390,7 +681,7 @@ static int parse_stmt(P *p) adv(p); OscRefKind rk = OSC_REF_SHARED; if (at(p, OT_MUT)) { rk = OSC_REF_MUT; adv(p); } - if (parse_arr_body(p, &N(n)->ty, rk)) return -1; + if (parse_ref_body(p, &N(n)->ty, rk)) return -1; if (expect(p, OT_ASSIGN)) return -1; if (!at(p, OT_AMP)) return syntax(p, "a borrow '&name' or '&mut name'"); int b = parse_borrow(p); @@ -398,6 +689,7 @@ static int parse_stmt(P *p) N(n)->a = b; } else { N(n)->mut = (uint8_t)mut; + if (at(p, OT_NAME) && find_struct(p, cur(p)) >= 0) return struct_by_value(p, cur(p), "struct by value"); if (parse_scalar(p, &N(n)->ty)) return -1; if (expect(p, OT_ASSIGN)) return -1; int e = parse_expr(p); @@ -418,6 +710,16 @@ static int parse_stmt(P *p) if (expect(p, OT_SEMI)) return -1; return n; } + if (k1 == OT_DOT) { + n = parse_field_ref(p, ON_FSTORE); + if (n < 0) return -1; + if (expect(p, OT_ASSIGN)) return -1; + int v = parse_expr(p); + if (v < 0) return -1; + N(n)->b = v; + if (expect(p, OT_SEMI)) return -1; + return n; + } if (k1 == OT_ASSIGN) { n = new_node(p, ON_ASSIGN, t); if (n < 0) return -1; @@ -644,6 +946,12 @@ static int parse_fn(P *p) N(f)->ty.s = OSC_T_VOID; if (at(p, OT_ARROW)) { adv(p); + { + uint32_t q = p->pos; + while (q < a->ntok && (a->toks[q].kind == OT_OWN || a->toks[q].kind == OT_AMP || a->toks[q].kind == OT_MUT)) q++; + if (a->toks[q].kind == OT_NAME && find_struct(p, &a->toks[q]) >= 0) + return struct_by_value(p, &a->toks[q], "struct return type"); + } if (at(p, OT_OWN) || at(p, OT_AMP) || at(p, OT_LBRACK)) { const OscToken *t = cur(p); osc_diag_set(p->d, OSC_DIAG_UNSUPPORTED, t->line, t->col, fname, N(f)->line, NULL, @@ -679,7 +987,10 @@ int osc_parse(OscAst *ast, OscDiag *d) ast->nnodes = 0; ast->nfns = 0; ast->nrel = 0; - while (!at(&p, OT_EOF)) + ast->nstructs = 0; + while (!at(&p, OT_EOF)) { + if (at(&p, OT_STRUCT)) { if (parse_struct(&p)) return -1; continue; } if (parse_fn(&p)) return -1; + } return 0; } diff --git a/src/compiler/osc_parse.h b/src/compiler/osc_parse.h index d97424fcc..27112e84d 100644 --- a/src/compiler/osc_parse.h +++ b/src/compiler/osc_parse.h @@ -46,7 +46,16 @@ typedef enum { ON_BORROW, /* tok = name; mut */ ON_BIN, /* op = OscTokKind of the operator (incl. && ||); a, b */ ON_UN, /* op = OT_MINUS / OT_TILDE / OT_BANG; a */ - ON_CAST /* a; ty = target scalar */ + ON_CAST, /* a; ty = target scalar */ + /* OSC-2 structs (docs/osc/OSC-2-DESIGN.md section 2). A struct literal is an + * ON_LET_ALLOC whose ty is a struct ref (ty.sid != 0) and whose a is the + * first ON_FINIT (linked by next, source order). */ + ON_FINIT, /* tok = field name; a = value expr; hi = field index; + * flag = 1 for an array field "[e; N]" (ival = N) */ + ON_FIELD, /* tok = struct binding; lo = field name token; a = index or -1; + * checker: sym = binding, hi = field index */ + ON_FSTORE /* tok = struct binding; lo = field name token; a = index or -1; + * b = value; checker: sym = binding, hi = field index */ } OscNodeKind; typedef struct { @@ -93,6 +102,11 @@ typedef struct { /* filled by the checker: symbol ids of owners to release, in emission order */ int16_t rel[OSC_AST_MAX_REL]; uint32_t nrel; + /* OSC-2 structs: declarations in source order (parser); copied into the + * OscUnit by the lowerer. struct_line = line of the declaration. */ + OscStruct structs[OSC_MAX_STRUCTS]; + uint32_t struct_line[OSC_MAX_STRUCTS]; + uint8_t nstructs; } OscAst; /* Parse the token stream into ast (ast->src/toks/ntok must be set). 0 ok, -1 diff --git a/tests/compiler/neg/st_alloc.osc b/tests/compiler/neg/st_alloc.osc new file mode 100644 index 000000000..6950e5fd6 --- /dev/null +++ b/tests/compiler/neg/st_alloc.osc @@ -0,0 +1,6 @@ +// expect: UNSUPPORTED object=a line=5 +// transition: alloc of a struct +struct A { v: u8 } +fn f() { + let a: own A = alloc(0); +} diff --git a/tests/compiler/neg/st_array_field_len.osc b/tests/compiler/neg/st_array_field_len.osc new file mode 100644 index 000000000..e9619657e --- /dev/null +++ b/tests/compiler/neg/st_array_field_len.osc @@ -0,0 +1,6 @@ +// expect: TYPE_MISMATCH object=v line=5 +// transition: array field length mismatch +struct A { v: [u8; 3] } +fn f() { + let a: own A = A { v: [1; 4] }; +} diff --git a/tests/compiler/neg/st_array_field_scalar_init.osc b/tests/compiler/neg/st_array_field_scalar_init.osc new file mode 100644 index 000000000..6a4619c38 --- /dev/null +++ b/tests/compiler/neg/st_array_field_scalar_init.osc @@ -0,0 +1,6 @@ +// expect: TYPE_MISMATCH object=v line=5 +// transition: array field without [e; N] +struct A { v: [u8; 3] } +fn f() { + let a: own A = A { v: 1 }; +} diff --git a/tests/compiler/neg/st_array_field_whole.osc b/tests/compiler/neg/st_array_field_whole.osc new file mode 100644 index 000000000..454273662 --- /dev/null +++ b/tests/compiler/neg/st_array_field_whole.osc @@ -0,0 +1,7 @@ +// expect: TYPE_MISMATCH object=return line=6 +// transition: type mismatch +struct A { v: [u8; 2] } +fn f() -> u8 { + let a: own A = A { v: [1; 2] }; + return a.v; +} diff --git a/tests/compiler/neg/st_borrow_outlives_owner.osc b/tests/compiler/neg/st_borrow_outlives_owner.osc new file mode 100644 index 000000000..a4cc735a2 --- /dev/null +++ b/tests/compiler/neg/st_borrow_outlives_owner.osc @@ -0,0 +1,12 @@ +// expect: BORROW_OUTLIVES_OWNER object=q line=9 +// transition: outer borrow assigned a borrow of an inner owner +struct P { x: i32, y: i32 } +fn f() -> i32 { + let p: own P = P { x: 1, y: 2 }; + let mut r: &P = &p; + { + let q: own P = P { x: 3, y: 4 }; + r = &q; + } + return r.x; +} diff --git a/tests/compiler/neg/st_by_value_param.osc b/tests/compiler/neg/st_by_value_param.osc new file mode 100644 index 000000000..4158d7ac8 --- /dev/null +++ b/tests/compiler/neg/st_by_value_param.osc @@ -0,0 +1,4 @@ +// expect: UNSUPPORTED object=A line=4 +// transition: struct by value +struct A { v: u8 } +fn f(a: A) { } diff --git a/tests/compiler/neg/st_call_wrong_struct.osc b/tests/compiler/neg/st_call_wrong_struct.osc new file mode 100644 index 000000000..fde1c2d61 --- /dev/null +++ b/tests/compiler/neg/st_call_wrong_struct.osc @@ -0,0 +1,9 @@ +// expect: TYPE_MISMATCH object=return line=8 +// transition: type mismatch +struct A { v: u8 } +struct B { v: u8 } +fn g(b: &B) -> u8 { return b.v; } +fn f() -> u8 { + let a: own A = A { v: 1 }; + return g(&a); +} diff --git a/tests/compiler/neg/st_capacity_fields.osc b/tests/compiler/neg/st_capacity_fields.osc new file mode 100644 index 000000000..d8228c493 --- /dev/null +++ b/tests/compiler/neg/st_capacity_fields.osc @@ -0,0 +1,4 @@ +// expect: CAPACITY object=P line=3 +// transition: field capacity +struct P { f0: u8, f1: u8, f2: u8, f3: u8, f4: u8, f5: u8, f6: u8, f7: u8, f8: u8, f9: u8, f10: u8, f11: u8, f12: u8, f13: u8, f14: u8, f15: u8, f16: u8 } +fn f() { } diff --git a/tests/compiler/neg/st_capacity_size.osc b/tests/compiler/neg/st_capacity_size.osc new file mode 100644 index 000000000..736e7d566 --- /dev/null +++ b/tests/compiler/neg/st_capacity_size.osc @@ -0,0 +1,4 @@ +// expect: CAPACITY object=P line=3 +// transition: struct size +struct P { a: [u64; 60], b: [u8; 5] } +fn f() { } diff --git a/tests/compiler/neg/st_capacity_structs.osc b/tests/compiler/neg/st_capacity_structs.osc new file mode 100644 index 000000000..262f6fb8f --- /dev/null +++ b/tests/compiler/neg/st_capacity_structs.osc @@ -0,0 +1,20 @@ +// expect: CAPACITY object=S16 line=19 +// transition: struct capacity +struct S0 { v: u8 } +struct S1 { v: u8 } +struct S2 { v: u8 } +struct S3 { v: u8 } +struct S4 { v: u8 } +struct S5 { v: u8 } +struct S6 { v: u8 } +struct S7 { v: u8 } +struct S8 { v: u8 } +struct S9 { v: u8 } +struct S10 { v: u8 } +struct S11 { v: u8 } +struct S12 { v: u8 } +struct S13 { v: u8 } +struct S14 { v: u8 } +struct S15 { v: u8 } +struct S16 { v: u8 } +fn f() { } diff --git a/tests/compiler/neg/st_duplicate_field_init.osc b/tests/compiler/neg/st_duplicate_field_init.osc new file mode 100644 index 000000000..33fb88e12 --- /dev/null +++ b/tests/compiler/neg/st_duplicate_field_init.osc @@ -0,0 +1,7 @@ +// expect: DUPLICATE_FIELD object=x line=5 +// transition: field initialised twice +struct P { x: i32, y: i32 } +fn f() -> i32 { + let p: own P = P { x: 1, y: 2, x: 3 }; + return p.x; +} diff --git a/tests/compiler/neg/st_empty.osc b/tests/compiler/neg/st_empty.osc new file mode 100644 index 000000000..4e207e83e --- /dev/null +++ b/tests/compiler/neg/st_empty.osc @@ -0,0 +1,4 @@ +// expect: UNSUPPORTED object=P line=3 +// transition: empty struct +struct P { } +fn f() { } diff --git a/tests/compiler/neg/st_ensures_mut_field.osc b/tests/compiler/neg/st_ensures_mut_field.osc new file mode 100644 index 000000000..d542e5637 --- /dev/null +++ b/tests/compiler/neg/st_ensures_mut_field.osc @@ -0,0 +1,7 @@ +// expect: CONTRACT_INVALID object=a line=4 +// transition: struct field read in ensures +struct A { v: u8 } +fn f(a: &mut A) -> u8 ensures a.v == result { + a.v = 3; + return 3; +} diff --git a/tests/compiler/neg/st_field_declared_twice.osc b/tests/compiler/neg/st_field_declared_twice.osc new file mode 100644 index 000000000..54172bc83 --- /dev/null +++ b/tests/compiler/neg/st_field_declared_twice.osc @@ -0,0 +1,4 @@ +// expect: DUPLICATE_FIELD object=x line=3 +// transition: field declared twice +struct P { x: i32, x: u8 } +fn f() { } diff --git a/tests/compiler/neg/st_field_init_type_mismatch.osc b/tests/compiler/neg/st_field_init_type_mismatch.osc new file mode 100644 index 000000000..8aada8b67 --- /dev/null +++ b/tests/compiler/neg/st_field_init_type_mismatch.osc @@ -0,0 +1,7 @@ +// expect: TYPE_MISMATCH object=p line=5 +// transition: type mismatch +struct P { x: i32, ok: bool } +fn f(v: i32) -> i32 { + let p: own P = P { x: v, ok: v }; + return p.x; +} diff --git a/tests/compiler/neg/st_field_of_array.osc b/tests/compiler/neg/st_field_of_array.osc new file mode 100644 index 000000000..26fb5852a --- /dev/null +++ b/tests/compiler/neg/st_field_of_array.osc @@ -0,0 +1,6 @@ +// expect: TYPE_MISMATCH object=return line=5 +// transition: type mismatch +fn f() -> u8 { + let a: own [u8; 2] = alloc(1); + return a.v; +} diff --git a/tests/compiler/neg/st_field_type_mismatch.osc b/tests/compiler/neg/st_field_type_mismatch.osc new file mode 100644 index 000000000..70ef67d0c --- /dev/null +++ b/tests/compiler/neg/st_field_type_mismatch.osc @@ -0,0 +1,7 @@ +// expect: TYPE_MISMATCH object=p line=6 +// transition: type mismatch +struct P { x: i32, ok: bool } +fn f(v: u32) { + let p: own P = P { x: 1, ok: true }; + p.x = v; +} diff --git a/tests/compiler/neg/st_index_struct.osc b/tests/compiler/neg/st_index_struct.osc new file mode 100644 index 000000000..02194da24 --- /dev/null +++ b/tests/compiler/neg/st_index_struct.osc @@ -0,0 +1,7 @@ +// expect: TYPE_MISMATCH object=return line=6 +// transition: type mismatch +struct A { v: u8 } +fn f() -> u8 { + let a: own A = A { v: 1 }; + return a[0]; +} diff --git a/tests/compiler/neg/st_literal_other_type.osc b/tests/compiler/neg/st_literal_other_type.osc new file mode 100644 index 000000000..752811def --- /dev/null +++ b/tests/compiler/neg/st_literal_other_type.osc @@ -0,0 +1,7 @@ +// expect: TYPE_MISMATCH object=a line=6 +// transition: struct literal of another type +struct A { v: u8 } +struct B { v: u8 } +fn f() { + let a: own A = B { v: 1 }; +} diff --git a/tests/compiler/neg/st_missing_field_init.osc b/tests/compiler/neg/st_missing_field_init.osc new file mode 100644 index 000000000..b8b893f11 --- /dev/null +++ b/tests/compiler/neg/st_missing_field_init.osc @@ -0,0 +1,7 @@ +// expect: MISSING_FIELD object=y line=5 +// transition: field not initialised +struct P { x: i32, y: i32 } +fn f() -> i32 { + let p: own P = P { x: 1 }; + return p.x; +} diff --git a/tests/compiler/neg/st_move_wrong_struct.osc b/tests/compiler/neg/st_move_wrong_struct.osc new file mode 100644 index 000000000..c4ae5157d --- /dev/null +++ b/tests/compiler/neg/st_move_wrong_struct.osc @@ -0,0 +1,9 @@ +// expect: TYPE_MISMATCH object=b line=7 +// transition: type mismatch +struct A { v: u8 } +struct B { v: u8 } +fn f() -> u8 { + let a: own A = A { v: 1 }; + let b: own B = a; + return b.v; +} diff --git a/tests/compiler/neg/st_mutable_alias.osc b/tests/compiler/neg/st_mutable_alias.osc new file mode 100644 index 000000000..ce292289c --- /dev/null +++ b/tests/compiler/neg/st_mutable_alias.osc @@ -0,0 +1,8 @@ +// expect: MUTABLE_ALIAS object=p line=7 +// transition: borrow_shared while borrow_mut live +struct P { x: i32, y: i32 } +fn g(a: &mut P, b: &P) { a.x = b.y; } +fn f() { + let p: own P = P { x: 1, y: 2 }; + g(&mut p, &p); +} diff --git a/tests/compiler/neg/st_oob_field.osc b/tests/compiler/neg/st_oob_field.osc new file mode 100644 index 000000000..2e38dba1a --- /dev/null +++ b/tests/compiler/neg/st_oob_field.osc @@ -0,0 +1,7 @@ +// expect: STATIC_OUT_OF_BOUNDS object=a.v line=6 +// transition: index outside 0..N-1 +struct A { v: [u8; 2] } +fn f() -> u8 { + let a: own A = A { v: [1; 2] }; + return a.v[2]; +} diff --git a/tests/compiler/neg/st_owner_field.osc b/tests/compiler/neg/st_owner_field.osc new file mode 100644 index 000000000..e91a8fb90 --- /dev/null +++ b/tests/compiler/neg/st_owner_field.osc @@ -0,0 +1,5 @@ +// expect: UNSUPPORTED object=a line=4 +// transition: owner or borrow field +struct A { v: u8 } +struct B { a: &A } +fn f() { } diff --git a/tests/compiler/neg/st_recursive.osc b/tests/compiler/neg/st_recursive.osc new file mode 100644 index 000000000..6645c15d4 --- /dev/null +++ b/tests/compiler/neg/st_recursive.osc @@ -0,0 +1,4 @@ +// expect: RECURSIVE_STRUCT object=N line=3 +// transition: struct field of its own type +struct N { v: i32, next: N } +fn f() { } diff --git a/tests/compiler/neg/st_redefined.osc b/tests/compiler/neg/st_redefined.osc new file mode 100644 index 000000000..fec0f7f4b --- /dev/null +++ b/tests/compiler/neg/st_redefined.osc @@ -0,0 +1,5 @@ +// expect: REDEFINED_NAME object=P line=4 +// transition: struct declared twice +struct P { x: i32 } +struct P { y: i32 } +fn f() { } diff --git a/tests/compiler/neg/st_return_type.osc b/tests/compiler/neg/st_return_type.osc new file mode 100644 index 000000000..d9780716e --- /dev/null +++ b/tests/compiler/neg/st_return_type.osc @@ -0,0 +1,6 @@ +// expect: UNSUPPORTED object=A line=4 +// transition: struct return type +struct A { v: u8 } +fn f(a: own A) -> own A { + return a; +} diff --git a/tests/compiler/neg/st_scalar_field_indexed.osc b/tests/compiler/neg/st_scalar_field_indexed.osc new file mode 100644 index 000000000..a5f315de4 --- /dev/null +++ b/tests/compiler/neg/st_scalar_field_indexed.osc @@ -0,0 +1,7 @@ +// expect: TYPE_MISMATCH object=return line=6 +// transition: type mismatch +struct A { v: u8 } +fn f() -> u8 { + let a: own A = A { v: 1 }; + return a.v[0]; +} diff --git a/tests/compiler/neg/st_store_struct.osc b/tests/compiler/neg/st_store_struct.osc new file mode 100644 index 000000000..65da553e3 --- /dev/null +++ b/tests/compiler/neg/st_store_struct.osc @@ -0,0 +1,7 @@ +// expect: TYPE_MISMATCH object=a line=6 +// transition: type mismatch +struct A { v: u8 } +fn f() { + let a: own A = A { v: 1 }; + a[0] = 2; +} diff --git a/tests/compiler/neg/st_struct_as_scalar.osc b/tests/compiler/neg/st_struct_as_scalar.osc new file mode 100644 index 000000000..e41ee005b --- /dev/null +++ b/tests/compiler/neg/st_struct_as_scalar.osc @@ -0,0 +1,7 @@ +// expect: TYPE_MISMATCH object=return line=6 +// transition: type mismatch +struct A { v: u8 } +fn f() -> u8 { + let a: own A = A { v: 1 }; + return a + 1; +} diff --git a/tests/compiler/neg/st_struct_field.osc b/tests/compiler/neg/st_struct_field.osc new file mode 100644 index 000000000..947da8be4 --- /dev/null +++ b/tests/compiler/neg/st_struct_field.osc @@ -0,0 +1,5 @@ +// expect: UNSUPPORTED object=a line=4 +// transition: struct-typed field +struct A { v: u8 } +struct B { a: A } +fn f() { } diff --git a/tests/compiler/neg/st_undefined_type.osc b/tests/compiler/neg/st_undefined_type.osc new file mode 100644 index 000000000..efecb4066 --- /dev/null +++ b/tests/compiler/neg/st_undefined_type.osc @@ -0,0 +1,3 @@ +// expect: UNDEFINED_TYPE object=Q line=3 +// transition: undefined struct type +fn f(p: &Q) { } diff --git a/tests/compiler/neg/st_unknown_field.osc b/tests/compiler/neg/st_unknown_field.osc new file mode 100644 index 000000000..85ae5f68e --- /dev/null +++ b/tests/compiler/neg/st_unknown_field.osc @@ -0,0 +1,7 @@ +// expect: UNKNOWN_FIELD object=z line=6 +// transition: access unknown field +struct P { x: i32, y: i32 } +fn f() -> i32 { + let p: own P = P { x: 1, y: 2 }; + return p.z; +} diff --git a/tests/compiler/neg/st_unknown_field_init.osc b/tests/compiler/neg/st_unknown_field_init.osc new file mode 100644 index 000000000..ad363db23 --- /dev/null +++ b/tests/compiler/neg/st_unknown_field_init.osc @@ -0,0 +1,7 @@ +// expect: UNKNOWN_FIELD object=z line=5 +// transition: initialise unknown field +struct P { x: i32, y: i32 } +fn f() -> i32 { + let p: own P = P { x: 1, y: 2, z: 3 }; + return p.x; +} diff --git a/tests/compiler/neg/st_use_after_move.osc b/tests/compiler/neg/st_use_after_move.osc new file mode 100644 index 000000000..c99b9408b --- /dev/null +++ b/tests/compiler/neg/st_use_after_move.osc @@ -0,0 +1,8 @@ +// expect: USE_AFTER_MOVE object=p line=7 +// transition: use after move +struct P { x: i32, y: i32 } +fn f() -> i32 { + let p: own P = P { x: 1, y: 2 }; + let q: own P = p; + return p.x + q.y; +} diff --git a/tests/compiler/neg/st_write_through_shared.osc b/tests/compiler/neg/st_write_through_shared.osc new file mode 100644 index 000000000..57ca70841 --- /dev/null +++ b/tests/compiler/neg/st_write_through_shared.osc @@ -0,0 +1,6 @@ +// expect: READ_ONLY_BORROW object=p line=5 +// transition: write through shared borrow +struct P { x: i32, y: i32 } +fn f(p: &P) { + p.x = 1; +} diff --git a/tests/compiler/osc2_receipt.sh b/tests/compiler/osc2_receipt.sh index 087ede889..e36018465 100755 --- a/tests/compiler/osc2_receipt.sh +++ b/tests/compiler/osc2_receipt.sh @@ -1,10 +1,10 @@ #!/bin/sh # OSC-2 compiler slice receipt, one per item (docs/osc/OSC-2-DESIGN.md). # OSC-2 slice; not a general Omega compiler; no self-hosting. -# Usage: tests/compiler/osc2_receipt.sh [ITEM] (ITEM default: contracts) +# Usage: tests/compiler/osc2_receipt.sh [ITEM] (ITEM: contracts (default) or structs) # 1. refuses a dirty tree (any tracked or untracked change): receipts record an exact commit # 2. runs `make test-compiler` into a temporary OUT_DIR (model sweep, back end, -# compiler golden/negative/model agreement, contract fuzz, determinism; plain + ASan/UBSan) +# compiler golden/negative/model agreement, contract + struct fuzz, determinism; plain + ASan/UBSan) # 3. compiles every golden program with oscc and records its IR and machine-code digests # 4. checks that the existing Omega core sources (src/omega_*, src/aarch64_*, # src/language/, tools/omegatool.c, Makefile) are unchanged against the merge @@ -14,7 +14,7 @@ # Never overwrites existing evidence (evidence/OSC-1 is never touched). Timing is not recorded. set -eu ITEM=${1:-${ITEM:-contracts}} -case "$ITEM" in contracts) ;; *) echo "osc2-receipt: REFUSED: unknown ITEM $ITEM (known: contracts)" >&2; exit 2 ;; esac +case "$ITEM" in contracts|structs) ;; *) echo "osc2-receipt: REFUSED: unknown ITEM $ITEM (known: contracts structs)" >&2; exit 2 ;; esac ROOT=$(cd "$(dirname "$0")/../.." && pwd -P) cd "$ROOT" EVD=evidence/OSC-2/receipts @@ -42,6 +42,15 @@ for v in "$MSUM" "$BCOUNTS" "$BCHECKS" "$CLINE" "$DLINE"; do [ -n "$v" ] || die FLINE=$(grep "^contract fuzz:" "$O/compiler.out" | tail -1) FLINE_ASAN=$(grep "^contract fuzz:" "$O/compiler_asan.out" | tail -1) [ -n "$FLINE" ] && [ -n "$FLINE_ASAN" ] || die "no contract fuzz line" +SLINE=$(grep "^struct fuzz:" "$O/compiler.out" | tail -1) +SLINE_ASAN=$(grep "^struct fuzz:" "$O/compiler_asan.out" | tail -1) +if [ "$ITEM" = structs ]; then + case "$SLINE" in *" mismatches=0") ;; *) die "no struct fuzz line with mismatches=0" ;; esac + case "$SLINE_ASAN" in *" mismatches=0") ;; *) die "no ASan struct fuzz line with mismatches=0" ;; esac + LAYOUT=$(grep "^struct layout:" "$O/compiler.out" | tail -1) + DTOR=$(grep "^struct destruction order:" "$O/compiler.out" | tail -1) + [ -n "$LAYOUT" ] && [ -n "$DTOR" ] || die "no struct layout / destruction order line" +fi for k in OVERFLOW DIV0 BOUNDS LOOP_BOUND CAST OOM SHIFT REQUIRES ENSURES; do n=$(tc $k); [ -n "$n" ] && [ "$n" -gt 0 ] || die "trap $k not observed in golden corpus" done @@ -75,6 +84,10 @@ if [ -n "${PHYSICS_DIR:-}" ] && [ -d "$PHYSICS_DIR" ]; then GATES="{${G%, }}" fi +SUBX= +if [ "$ITEM" = structs ]; then + SUBX="; OSC-2 item 2: unit-level struct declarations (<= 16 structs, 1..16 fields of integer, bool or [T; N], <= 64 cells, declared before use, no recursion) with a fixed layout of 8-byte cells in declaration order (no padding, bound into the IR digest), struct literals initialising every field exactly once in let own, field reads and writes (array fields bounds-checked, TRAP BOUNDS), moves, own / & / &mut parameters with the array borrow rules, deterministic destruction with the same alloc/release events; requires reads fields through any struct parameter, ensures only through a shared & parameter; returning a struct is refused (UNSUPPORTED)" +fi R="$T/receipt.json" { printf '{\n' @@ -83,7 +96,7 @@ R="$T/receipt.json" printf ' "item": "%s",\n' "$ITEM" printf '' printf ' "statement": "OSC-2 slice; not a general Omega compiler; no self-hosting.",\n' - printf ' "subset": "integer scalars u8 u16 u32 u64 (wrap) and i8 i16 i32 i64 (checked, trap on overflow), bool; let / let mut; arithmetic, bitwise, shift, compare, logical (short-circuit), checked as-casts; if/else; while with static bound N (trap past N) and for over literal ranges; calls to earlier functions in the unit (no recursion, <= 6 params); unique allocation own [T; N] (N <= 64), moves, shared and mutable borrows with lexical lifetimes, bounds-checked indexing, deterministic destruction at scope end; OSC-2 item 1: requires/ensures are pure bool expressions over parameters (and result in ensures; element reads through parameters, ensures only via shared borrows; no calls), refused statically when constant folding decides them false (CONTRACT_VIOLATION), otherwise checked at run time (TRAP REQUIRES = 9 at entry, TRAP ENSURES = 10 at each return before releases; statically true clauses elided)",\n' + printf ' "subset": "integer scalars u8 u16 u32 u64 (wrap) and i8 i16 i32 i64 (checked, trap on overflow), bool; let / let mut; arithmetic, bitwise, shift, compare, logical (short-circuit), checked as-casts; if/else; while with static bound N (trap past N) and for over literal ranges; calls to earlier functions in the unit (no recursion, <= 6 params); unique allocation own [T; N] (N <= 64), moves, shared and mutable borrows with lexical lifetimes, bounds-checked indexing, deterministic destruction at scope end; OSC-2 item 1: requires/ensures are pure bool expressions over parameters (and result in ensures; element reads through parameters, ensures only via shared borrows; no calls), refused statically when constant folding decides them false (CONTRACT_VIOLATION), otherwise checked at run time (TRAP REQUIRES = 9 at entry, TRAP ENSURES = 10 at each return before releases; statically true clauses elided)%s",\n' "$SUBX" printf ' "pipeline": "surface text -> lexer/parser -> typed AST -> name/type + ownership/borrow analysis -> typed IR (canonical encoding + sha256) -> AArch64 via src/compiler/osc_a64 (validating encoder, decoder mirror)",\n' printf ' "repo_commit": "%s",\n' "$COMMIT" printf ' "base_commit": "%s",\n' "$BASE" @@ -98,6 +111,12 @@ R="$T/receipt.json" "$(tc none)" "$(tc OVERFLOW)" "$(tc DIV0)" "$(tc BOUNDS)" "$(tc LOOP_BOUND)" "$(tc CAST)" "$(tc OOM)" "$(tc SHIFT)" "$(tc RUNTIME)" "$(tc REQUIRES)" "$(tc ENSURES)" printf ' "contract_fuzz": "%s; native == interpreter on every run",\n' "$FLINE" printf ' "contract_fuzz_asan": "%s",\n' "$FLINE_ASAN" + if [ "$ITEM" = structs ]; then + printf ' "struct_fuzz": "%s; native == interpreter on every run",\n' "$SLINE" + printf ' "struct_fuzz_asan": "%s",\n' "$SLINE_ASAN" + printf ' "struct_layout": "%s",\n' "$LAYOUT" + printf ' "struct_destruction_order": "%s",\n' "$DTOR" + fi printf ' "determinism": "%s (oscc in separate processes, byte-identical IR digest and machine code)",\n' "$DLINE" printf ' "sanitizers": "all three test binaries also pass under -fsanitize=address,undefined -fno-sanitize-recover=all (model sweep at 10^5)",\n' printf ' "golden_programs": %s,\n' "$NPROG" @@ -113,7 +132,11 @@ R="$T/receipt.json" printf ' "omega_core_sources_unchanged_vs_base": %s,\n' "$CORE_UNCHANGED" printf ' "m6_m9_m14_gates": %s,\n' "$GATES" printf ' "self_host": "no: the OSC-2 compiler slice cannot compile any part of itself (docs/osc/OSC-1-SELF-HOST-STATEMENT.md)",\n' - printf ' "not_covered": "contracts beyond constant folding (no symbolic proof), effects/capabilities, structs, strings, generations, arenas, unsafe physical, FFI, Flow IR optimisation, self-hosting (OSC-14); not a general Omega compiler",\n' + if [ "$ITEM" = structs ]; then + printf ' "not_covered": "struct return values, struct-typed and owner/borrow fields, recursive structs, struct literals outside let own initialisers, whole-struct copy or compare, contracts beyond constant folding (no symbolic proof), effects/capabilities, strings, generations, arenas, unsafe physical, FFI, Flow IR optimisation, self-hosting (OSC-14); not a general Omega compiler",\n' + else + printf ' "not_covered": "contracts beyond constant folding (no symbolic proof), effects/capabilities, structs, strings, generations, arenas, unsafe physical, FFI, Flow IR optimisation, self-hosting (OSC-14); not a general Omega compiler",\n' + fi printf ' "timing_measured": false\n' printf '}\n' } >"$R" diff --git a/tests/compiler/progs/structs_contracts.osc b/tests/compiler/progs/structs_contracts.osc new file mode 100644 index 000000000..99f84a886 --- /dev/null +++ b/tests/compiler/progs/structs_contracts.osc @@ -0,0 +1,28 @@ +// entry: run_req run_ens +// OSC-2 structs in contracts: requires reads fields through any struct +// parameter; ensures reads fields only through a shared & parameter. +struct Win { lo: u16, hi: u16 } + +fn width(w: &mut Win) -> u16 requires w.hi >= w.lo { + w.lo = w.lo + 0; + return w.hi - w.lo; +} + +fn clampv(w: &Win, v: u16) -> u16 ensures result >= w.lo { + if v < w.lo { return v; } + return v; +} + +fn run_req(a: u16, b: u16) -> u16 { + let w: own Win = Win { lo: a, hi: b }; + return width(&mut w); +} + +fn run_ens(a: u16, v: u16) -> u16 { + let w: own Win = Win { lo: a, hi: 100 }; + return clampv(&w, v); +} +// expect-run: run_req 3 9 -> 6 +// expect-run: run_req 9 3 -> trap=REQUIRES +// expect-run: run_ens 5 7 -> 7 +// expect-run: run_ens 5 2 -> trap=ENSURES diff --git a/tests/compiler/progs/structs_dtor.osc b/tests/compiler/progs/structs_dtor.osc new file mode 100644 index 000000000..7b97b4e61 --- /dev/null +++ b/tests/compiler/progs/structs_dtor.osc @@ -0,0 +1,19 @@ +// entry: dtor +// OSC-2 structs: deterministic destruction. Owners are released at scope +// end in reverse declaration order, structs and arrays alike, using the same +// alloc/release events as arrays. A moved-from struct is not released again. +// The release order is asserted by test_osc_compiler (serials 3 2 4 1). +struct Pair { a: u32, b: u32 } + +fn dtor(k: u32) -> u32 { + let p: own Pair = Pair { a: k, b: 1 }; + { + let arr: own [u32; 2] = alloc(2); + let q: own Pair = Pair { a: arr[0], b: arr[1] }; + p.b = q.a + q.b; + } + let r: own Pair = Pair { a: 9, b: 9 }; + let s: own Pair = r; + return p.a + p.b + s.a; +} +// expect-run: dtor 1 -> 14 diff --git a/tests/compiler/progs/structs_fields.osc b/tests/compiler/progs/structs_fields.osc new file mode 100644 index 000000000..fe975a4eb --- /dev/null +++ b/tests/compiler/progs/structs_fields.osc @@ -0,0 +1,24 @@ +// entry: fields_rw pick_bits +// OSC-2 structs: declaration, literal (each field once), field reads and +// writes, array fields with dynamic indexes and a BOUNDS trap. +struct Pt { x: i32, y: i32, ok: bool } +struct Rec { tag: u8, vals: [i16; 4], n: u64 } + +fn fields_rw(a: i32, b: i32) -> i32 { + let p: own Pt = Pt { y: b, x: a, ok: a > b }; + p.x = p.x - p.y; + if p.ok { p.y = 7; } + return p.x + p.y; +} + +fn pick_bits(i: u8, v: i16) -> i64 { + let r: own Rec = Rec { tag: i, vals: [v; 4], n: 3 }; + r.vals[1] = v - 1; + r.n = r.n + (r.tag as u64); + return (r.vals[i] as i64) + (r.n as i64); +} +// expect-run: fields_rw 5 2 -> 10 +// expect-run: fields_rw 1 4 -> 1 +// expect-run: pick_bits 1 10 -> 13 +// expect-run: pick_bits 3 -2 -> 4 +// expect-run: pick_bits 4 0 -> trap=BOUNDS diff --git a/tests/compiler/progs/structs_owner.osc b/tests/compiler/progs/structs_owner.osc new file mode 100644 index 000000000..e51c523bc --- /dev/null +++ b/tests/compiler/progs/structs_owner.osc @@ -0,0 +1,41 @@ +// entry: moves borrow_calls +// OSC-2 structs: moves (let and own parameters), shared and mutable borrows, +// passing structs to functions as own, & and &mut parameters. +struct Acc { sum: i64, hits: [u32; 3] } + +fn total(a: &Acc) -> i64 { + return a.sum + (a.hits[0] as i64) + (a.hits[1] as i64) + (a.hits[2] as i64); +} + +fn add(a: &mut Acc, k: i64, slot: u8) { + a.sum = a.sum + k; + a.hits[slot] = a.hits[slot] + 1; +} + +fn consume(a: own Acc) -> i64 { + return total(&a) * 2; +} + +fn moves(k: i32) -> i64 { + let a: own Acc = Acc { sum: k as i64, hits: [0; 3] }; + let b: own Acc = a; + b.hits[2] = 5; + let c: own Acc = b; + return consume(c) + 1; +} + +fn borrow_calls(k: i16, slot: u8) -> i64 { + let a: own Acc = Acc { hits: [1; 3], sum: 0 }; + add(&mut a, k as i64, slot); + { + let m: &mut Acc = &mut a; + add(&mut m, 1, 0); + m.sum = m.sum * 2; + } + let r: &Acc = &a; + let t: i64 = total(&r); + return t + (r.hits[0] as i64); +} +// expect-run: moves 10 -> 31 +// expect-run: borrow_calls 3 1 -> 15 +// expect-run: borrow_calls 3 3 -> trap=BOUNDS diff --git a/tests/compiler/test_osc_backend.c b/tests/compiler/test_osc_backend.c index d76c214c8..f13c6e289 100644 --- a/tests/compiler/test_osc_backend.c +++ b/tests/compiler/test_osc_backend.c @@ -56,9 +56,9 @@ static uint64_t sm(void) { } /* ---- IR builder --------------------------------------------------------- */ -static OscType TS(OscScalar s) { OscType t = {s, OSC_REF_NONE, OSC_T_VOID, 0}; return t; } -static OscType TR(OscRefKind k, OscScalar e, int len) { OscType t = {OSC_T_REF, k, e, (uint16_t)len}; return t; } -static const OscType TVOID = {OSC_T_VOID, OSC_REF_NONE, OSC_T_VOID, 0}; +static OscType TS(OscScalar s) { OscType t = {s, OSC_REF_NONE, OSC_T_VOID, 0, 0}; return t; } +static OscType TR(OscRefKind k, OscScalar e, int len) { OscType t = {OSC_T_REF, k, e, (uint16_t)len, 0}; return t; } +static const OscType TVOID = {OSC_T_VOID, OSC_REF_NONE, OSC_T_VOID, 0, 0}; typedef struct { OscUnit *u; OscFunc *f; int fi; int cur; } B; diff --git a/tests/compiler/test_osc_compiler.c b/tests/compiler/test_osc_compiler.c index 68ca502ef..48eaea074 100644 --- a/tests/compiler/test_osc_compiler.c +++ b/tests/compiler/test_osc_compiler.c @@ -643,6 +643,235 @@ static void contract_fuzz(unsigned n) CHECK(cf_compiled > n / 2, "contract fuzz compiled only %lu of %u units", cf_compiled, n); } + +/* ------------------------------------------------------------ OSC-2 structs */ +/* Layout: fixed 8-byte cells in declaration order, scalar = 1 cell, [T; N] = + * N cells, offsets are prefix sums (no padding). The layout is part of the IR + * digest: reordering two fields changes it; a unit without structs keeps the + * OSC-1 encoding (format version byte 1). */ +static void struct_layout(void) +{ + static const char *s1 = "struct L { a: u8, b: [i16; 3], c: bool, d: [u64; 2], e: i64 }\n" + "struct M { z: [u8; 64] }\nfn f() { }\n"; + static const char *s2 = "struct L { b: [i16; 3], a: u8, c: bool, d: [u64; 2], e: i64 }\n" + "struct M { z: [u8; 64] }\nfn f() { }\n"; + static const char *s3 = "fn f() { }\n"; + static const uint16_t off[5] = {0, 1, 4, 5, 7}, alen[5] = {0, 3, 0, 2, 0}; + static const OscScalar sc[5] = {OSC_T_U8, OSC_T_I16, OSC_T_BOOL, OSC_T_U64, OSC_T_I64}; + OscDiag d; + int rc = osc_compile(s1, strlen(s1), U1, &d, NULL); + CHECK(rc == 0, "struct layout: refused: %s %s", osc_diag_kind_name(d.kind), d.message); + if (rc) return; + CHECK(U1->nstructs == 2, "struct layout: nstructs %u", U1->nstructs); + const OscStruct *L = &U1->structs[0]; + CHECK(strcmp(L->name, "L") == 0 && L->nfields == 5 && L->ncells == 8, "struct layout: L has %u fields, %u cells", + L->nfields, L->ncells); + for (int k = 0; k < 5 && k < L->nfields; k++) + CHECK(L->fields[k].off == off[k] && L->fields[k].alen == alen[k] && L->fields[k].s == sc[k], + "struct layout: field %s off %u alen %u", L->fields[k].name, L->fields[k].off, L->fields[k].alen); + CHECK(U1->structs[1].ncells == 64 && U1->structs[1].fields[0].off == 0, "struct layout: M has %u cells", + U1->structs[1].ncells); + uint8_t buf[4096], g1[32], g2[32]; + size_t n = 0; + CHECK(osc_ir_encode(U1, buf, sizeof buf, &n) == 0 && n > 8 && buf[7] == 2, "struct layout: encoding version"); + CHECK(osc_ir_digest(U1, g1) == 0, "struct layout: digest"); + rc = osc_compile(s2, strlen(s2), U2, &d, NULL); + CHECK(rc == 0 && U2->structs[0].fields[0].off == 0 && U2->structs[0].fields[1].off == 3, + "struct layout: reordered L offsets"); + CHECK(rc == 0 && osc_ir_digest(U2, g2) == 0 && memcmp(g1, g2, 32) != 0, "struct layout: digest ignores layout"); + rc = osc_compile(s3, strlen(s3), U2, &d, NULL); + CHECK(rc == 0 && osc_ir_encode(U2, buf, sizeof buf, &n) == 0 && n > 8 && buf[7] == 1, + "struct layout: struct-free unit is not OSC-1 encoded"); + printf("struct layout: L offsets 0 1 4 5 7 cells 8; M cells 64; digest binds layout\n"); +} + +/* Destruction order of progs/structs_dtor.osc: owners released at scope end in + * reverse declaration order, the moved-from struct is not released, in the + * interpreter and natively. */ +static void struct_dtor_order(const char *dir) +{ + char path[1024]; + snprintf(path, sizeof path, "%s/structs_dtor.osc", dir); + size_t len; + char *src = read_file(path, &len); + CHECK(src != NULL, "structs_dtor.osc unreadable"); + if (!src) return; + OscDiag d; + OscCode c; + char err[160]; + memset(&c, 0, sizeof c); + int rc = osc_compile(src, len, U1, &d, NULL); + free(src); + CHECK(rc == 0 && osc_cg_compile(U1, &c, err, sizeof err) == 0, "structs_dtor: compile failed"); + if (rc) return; + OscNative nm; + if (osc_native_map(&nm, c.code, c.len) != 0) { CHECK(0, "structs_dtor: map failed"); osc_cg_free(&c); return; } + static const uint8_t want_kind[8] = {1, 1, 1, 2, 2, 1, 2, 2}; + static const uint32_t want_ser[8] = {1, 2, 3, 3, 2, 4, 4, 1}; + uint64_t args[1] = {1}, ri = 0, rn = 0; + osc_rt_reset(RI); + osc_rt_reset(RN); + int ti = osc_interp_run(U1, 0, args, 1, RI, &ri); + int tn = osc_rt_call_native(RN, osc_native_at(&nm, c.entry[0]), args, 1, &rn); + CHECK(ti == 0 && tn == 0 && ri == 14 && rn == 14, "structs_dtor: run %d/%d ret %llu/%llu", ti, tn, + (unsigned long long)ri, (unsigned long long)rn); + const OscRt *rts[2] = {RI, RN}; + for (int r = 0; r < 2; r++) { + CHECK(rts[r]->nev == 8, "structs_dtor: %u pool events, expected 8", rts[r]->nev); + for (unsigned k = 0; k < 8 && k < rts[r]->nev; k++) + CHECK(rts[r]->ev[k].kind == want_kind[k] && rts[r]->ev[k].serial == want_ser[k], + "structs_dtor: event %u is kind %u serial %u, expected kind %u serial %u", k, rts[r]->ev[k].kind, + rts[r]->ev[k].serial, want_kind[k], want_ser[k]); + } + osc_native_unmap(&nm); + osc_cg_free(&c); + printf("struct destruction order: alloc 1 2 3, release 3 2, alloc 4, release 4 1 (interp == native)\n"); +} + +/* Struct fuzz: generated units with 1..2 structs of random integer, bool and + * [T; N] fields, an entry that builds a struct literal, writes fields + * (dynamic array-field indexes reach BOUNDS), moves it, passes it as &mut, + * & (with a requires on a field) and own, and sums the fields. Every unit + * must compile; interpreter and native outcomes must be identical. */ +static unsigned long sf_units, sf_runs, sf_mismatch, sf_trap[OSC_TRAP_MAX + 1]; + +static void struct_fuzz(unsigned n) +{ + static const char *tys[] = {"u8", "u16", "u32", "u64", "i8", "i16", "i32", "i64", "bool"}; + static char src[8192]; + sm_state = 0x05C2517Cull; + for (unsigned u = 0; u < n; u++) { + unsigned ns = 1 + (unsigned)(sm() % 2), nf[2], ft[2][6], fl[2][6]; + src[0] = 0; + for (unsigned s = 0; s < ns; s++) { + nf[s] = 1 + (unsigned)(sm() % 6); + cf_cat(src, sizeof src, "struct S%u {", s); + for (unsigned f = 0; f < nf[s]; f++) { + ft[s][f] = (unsigned)(sm() % 9); + fl[s][f] = ft[s][f] != 8 && sm() % 3 == 0 ? 1 + (unsigned)(sm() % 4) : 0; + if (fl[s][f]) cf_cat(src, sizeof src, "%s f%u: [%s; %u]", f ? "," : "", f, tys[ft[s][f]], fl[s][f]); + else cf_cat(src, sizeof src, "%s f%u: %s", f ? "," : "", f, tys[ft[s][f]]); + } + cf_cat(src, sizeof src, " }\n"); + } + /* reader: requires on a field, sums every field through & */ + for (unsigned s = 0; s < ns; s++) { + cf_cat(src, sizeof src, "fn rd%u(p: &S%u, k: i64) -> i64", s, s); + unsigned rf = (unsigned)(sm() % nf[s]); + if (sm() % 3 == 0) { + if (ft[s][rf] == 8) cf_cat(src, sizeof src, " requires p.f%u%s || k != 3", rf, fl[s][rf] ? "[0]" : ""); + else cf_cat(src, sizeof src, " requires p.f%u%s != %u", rf, fl[s][rf] ? "[0]" : "", (unsigned)(sm() % 6)); + } + cf_cat(src, sizeof src, " {\n let mut acc: i64 = k;\n"); + for (unsigned f = 0; f < nf[s]; f++) { + char ix[8]; + snprintf(ix, sizeof ix, "%s", fl[s][f] ? "[0]" : ""); + if (ft[s][f] == 8) cf_cat(src, sizeof src, " if p.f%u%s { acc = acc + 1; }\n", f, ix); + else if (ft[s][f] == 3) cf_cat(src, sizeof src, " acc = acc + ((p.f%u%s & 255) as i64);\n", f, ix); + else cf_cat(src, sizeof src, " acc = acc + (p.f%u%s as i64);\n", f, ix); + } + cf_cat(src, sizeof src, " return acc;\n}\n"); + /* writer through &mut, dynamic index */ + cf_cat(src, sizeof src, "fn wr%u(p: &mut S%u, i: u8, b: i64) {\n", s, s); + for (unsigned f = 0; f < nf[s]; f++) { + if (sm() % 2) continue; + char ix[16]; + snprintf(ix, sizeof ix, "%s", fl[s][f] ? (sm() % 2 ? "[i & 3]" : "[i % 2]") : ""); + if (ft[s][f] == 8) cf_cat(src, sizeof src, " p.f%u%s = b > 0;\n", f, ix); + else cf_cat(src, sizeof src, " p.f%u%s = p.f%u%s %s ((b & 7) as %s);\n", f, ix, f, ix, + sm() % 2 ? "+" : "*", tys[ft[s][f]]); + } + cf_cat(src, sizeof src, "}\n"); + cf_cat(src, sizeof src, "fn own%u(p: own S%u, i: u8, b: i64) -> i64 {\n wr%u(&mut p, i, b);\n" + " return rd%u(&p, b);\n}\n", s, s, s, s); + } + cf_cat(src, sizeof src, "fn entry(a: i64, b: i64, i: u8) -> i64 {\n let mut acc: i64 = 0;\n"); + for (unsigned s = 0; s < ns; s++) { + cf_cat(src, sizeof src, " let v%u: own S%u = S%u {", s, s, s); + for (unsigned f = 0; f < nf[s]; f++) { + char val[64]; + if (ft[s][f] == 8) snprintf(val, sizeof val, "a > b"); + else if (sm() % 8 == 0) snprintf(val, sizeof val, "a as %s", tys[ft[s][f]]); + else snprintf(val, sizeof val, "(a & 15) as %s", tys[ft[s][f]]); + if (fl[s][f]) cf_cat(src, sizeof src, "%s f%u: [%s; %u]", f ? "," : "", f, val, fl[s][f]); + else cf_cat(src, sizeof src, "%s f%u: %s", f ? "," : "", f, val); + } + cf_cat(src, sizeof src, " };\n"); + const char *cur = "v"; + unsigned ops = 1 + (unsigned)(sm() % 4); + for (unsigned o = 0; o < ops; o++) { + unsigned f = (unsigned)(sm() % nf[s]); + switch (sm() % 4) { + case 0: + if (ft[s][f] == 8) break; + if (fl[s][f]) cf_cat(src, sizeof src, " %s%u.f%u[i] = %s%u.f%u[0] + ((b & 3) as %s);\n", cur, s, + f, cur, s, f, tys[ft[s][f]]); + else cf_cat(src, sizeof src, " %s%u.f%u = %s%u.f%u - ((b & 3) as %s);\n", cur, s, f, cur, s, + f, tys[ft[s][f]]); + break; + case 1: cf_cat(src, sizeof src, " wr%u(&mut %s%u, i, b);\n", s, cur, s); break; + case 2: cf_cat(src, sizeof src, " acc = acc + rd%u(&%s%u, a & 7);\n", s, cur, s); break; + default: + if (cur[0] == 'v') { + cf_cat(src, sizeof src, " let m%u: own S%u = v%u;\n", s, s, s); + cur = "m"; + } + } + } + if (sm() % 2) cf_cat(src, sizeof src, " acc = acc + own%u(%s%u, i, b);\n", s, cur, s); + else cf_cat(src, sizeof src, " acc = acc + rd%u(&%s%u, 0);\n", s, cur, s); + } + cf_cat(src, sizeof src, " return acc;\n}\n"); + sf_units++; + + OscDiag d; + int rc = osc_compile(src, strlen(src), U1, &d, NULL); + CHECK(rc == 0, "struct fuzz unit %u refused %s line %u object=%s: %s\n%s", u, osc_diag_kind_name(d.kind), + d.line, d.object, d.message, src); + if (rc) continue; + OscCode c; + char err[160]; + memset(&c, 0, sizeof c); + int cg = osc_cg_compile(U1, &c, err, sizeof err); + CHECK(cg == 0, "struct fuzz unit %u codegen refused: %s", u, err); + if (cg) continue; + OscNative nm; + int mr = osc_native_map(&nm, c.code, c.len); + CHECK(mr == 0, "struct fuzz unit %u native map failed (%d)", u, mr); + if (mr) { osc_cg_free(&c); continue; } + int fi = U1->nfuncs - 1; + void *entry = osc_native_at(&nm, c.entry[fi]); + for (unsigned it = 0; it < 48; it++) { + uint64_t args[OSC_MAX_PARAMS] = {0}, ri = 0, rn = 0; + args[0] = gen_arg(OSC_T_I64); + args[1] = gen_arg(OSC_T_I64); + args[2] = it & 1 ? (uint64_t)(it % 5) : gen_arg(OSC_T_U8); + osc_rt_reset(RI); + osc_rt_reset(RN); + int t1 = osc_interp_run_prevalidated(U1, fi, args, 3, RI, &ri); + int t2 = osc_rt_call_native(RN, entry, args, 3, &rn); + sf_runs++; + diff_runs++; + int same = t1 == t2 && t1 >= 0 && (t1 != 0 || ri == rn) && osc_rt_same_outcome(RI, RN); + CHECK(same, "struct fuzz unit %u args %llu %llu %llu: interp trap %d ret %llu vs native trap %d ret %llu\n%s", + u, (unsigned long long)args[0], (unsigned long long)args[1], (unsigned long long)args[2], t1, + (unsigned long long)ri, t2, (unsigned long long)rn, src); + if (!same) { sf_mismatch++; break; } + if (t1 >= 0 && t1 <= OSC_TRAP_MAX) sf_trap[t1]++; + CHECK(t1 != OSC_TRAP_RUNTIME, "struct fuzz unit %u RUNTIME trap", u); + if (t1 == 0) CHECK(RI->live_count == 0 && RN->live_count == 0, "struct fuzz unit %u leak", u); + } + osc_native_unmap(&nm); + osc_cg_free(&c); + } + printf("struct fuzz: units=%lu runs=%lu ok=%lu bounds=%lu requires=%lu overflow=%lu cast=%lu mismatches=%lu\n", + sf_units, sf_runs, sf_trap[0], sf_trap[OSC_TRAP_BOUNDS], sf_trap[OSC_TRAP_REQUIRES], + sf_trap[OSC_TRAP_OVERFLOW], sf_trap[OSC_TRAP_CAST], sf_mismatch); + CHECK(sf_trap[0] > 0, "struct fuzz never returned normally"); + CHECK(sf_trap[OSC_TRAP_BOUNDS] > 0, "struct fuzz never hit TRAP BOUNDS"); + CHECK(sf_trap[OSC_TRAP_REQUIRES] > 0, "struct fuzz never hit TRAP REQUIRES"); +} + int main(int argc, char **argv) { unsigned fuzz = argc > 1 ? (unsigned)strtoul(argv[1], NULL, 10) : 1000; @@ -672,6 +901,9 @@ int main(int argc, char **argv) for (int i = 0; i < np; i++) mutate_fuzz(pdir, pv[i], muts); printf("front-end mutants: %lu (accepted %lu, refused %lu)\n", mut_total, mut_accepted, mut_refused); contract_fuzz(fuzz / 5 ? fuzz / 5 : 20); + struct_layout(); + struct_dtor_order(pdir); + struct_fuzz(fuzz / 8 ? fuzz / 8 : 16); const char *const *tn = trap_names; printf("trap coverage (runs, interpreter == native):\n"); From 74ddca02f298c1c960c67ae44c1a0bd44ef8e756 Mon Sep 17 00:00:00 2001 From: Drake Stapleton Date: Thu, 1 Oct 2026 04:43:49 -0500 Subject: [PATCH 2/2] OSC-2 item 2: structs receipt osc2-structs-9ed6766ba798f007a5206ef597b7ac387c6083ccc3773ae3b91a48aaa443e66f.json Co-Authored-By: Drake Stapleton Co-Authored-By: Claude Fable 5.1 Co-Authored-By: Claude Opus 5.5 --- ...7ac387c6083ccc3773ae3b91a48aaa443e66f.json | 66 +++++++++++++++++++ 1 file changed, 66 insertions(+) create mode 100644 evidence/OSC-2/receipts/osc2-structs-9ed6766ba798f007a5206ef597b7ac387c6083ccc3773ae3b91a48aaa443e66f.json diff --git a/evidence/OSC-2/receipts/osc2-structs-9ed6766ba798f007a5206ef597b7ac387c6083ccc3773ae3b91a48aaa443e66f.json b/evidence/OSC-2/receipts/osc2-structs-9ed6766ba798f007a5206ef597b7ac387c6083ccc3773ae3b91a48aaa443e66f.json new file mode 100644 index 000000000..ccba73a86 --- /dev/null +++ b/evidence/OSC-2/receipts/osc2-structs-9ed6766ba798f007a5206ef597b7ac387c6083ccc3773ae3b91a48aaa443e66f.json @@ -0,0 +1,66 @@ +{ + "schema": "omega.osc2.compiler.receipt.v1", + "verdict": "PASS", + "item": "structs", + "statement": "OSC-2 slice; not a general Omega compiler; no self-hosting.", + "subset": "integer scalars u8 u16 u32 u64 (wrap) and i8 i16 i32 i64 (checked, trap on overflow), bool; let / let mut; arithmetic, bitwise, shift, compare, logical (short-circuit), checked as-casts; if/else; while with static bound N (trap past N) and for over literal ranges; calls to earlier functions in the unit (no recursion, <= 6 params); unique allocation own [T; N] (N <= 64), moves, shared and mutable borrows with lexical lifetimes, bounds-checked indexing, deterministic destruction at scope end; OSC-2 item 1: requires/ensures are pure bool expressions over parameters (and result in ensures; element reads through parameters, ensures only via shared borrows; no calls), refused statically when constant folding decides them false (CONTRACT_VIOLATION), otherwise checked at run time (TRAP REQUIRES = 9 at entry, TRAP ENSURES = 10 at each return before releases; statically true clauses elided); OSC-2 item 2: unit-level struct declarations (<= 16 structs, 1..16 fields of integer, bool or [T; N], <= 64 cells, declared before use, no recursion) with a fixed layout of 8-byte cells in declaration order (no padding, bound into the IR digest), struct literals initialising every field exactly once in let own, field reads and writes (array fields bounds-checked, TRAP BOUNDS), moves, own / & / &mut parameters with the array borrow rules, deterministic destruction with the same alloc/release events; requires reads fields through any struct parameter, ensures only through a shared & parameter; returning a struct is refused (UNSUPPORTED)", + "pipeline": "surface text -> lexer/parser -> typed AST -> name/type + ownership/borrow analysis -> typed IR (canonical encoding + sha256) -> AArch64 via src/compiler/osc_a64 (validating encoder, decoder mirror)", + "repo_commit": "a908454cbc8cb541e9afb43d7679ca218ffade91", + "base_commit": "0abdb08aec443cad3b66cf17afee6b713b7af271", + "tree_clean": true, + "host_arch": "aarch64", + "c_compiler": "cc (Ubuntu 13.3.0-6ubuntu2~24.04.1) 13.3.0", + "osc0b_model": {"seed": "0x05c0b5eed0010001", "sequences": 1000000, "steps": 21905422, "false_accepts": 0, "wrong_name": 0, "false_rejects": 0, "result": "OSC0B_MODEL_PASS"}, + "backend": "units=15 funcs_fuzzed=361 diff_runs=722000 words_roundtrip=13006 enc_random_ok=45779 enc_random_refused=126221 dec_random_words=76497 validator_neg=40; checks=798964 failed=0; native == interpreter on every run", + "compiler": "golden=31/31 funcs=117 entries=82 expect_runs=86 diff_runs=99136 trace_replays=77 rt_replays=62738 rt_events=209464 neg=82/82 neg_traced=16 checks=274660 failed=0", + "golden_trap_coverage": {"none": 62738, "overflow": 3195, "div0": 23, "bounds": 5347, "loop_bound": 2100, "cast": 1499, "oom": 365, "shift": 2805, "runtime": 0, "requires": 1793, "ensures": 2135}, + "contract_fuzz": "contract fuzz: units=200 compiled=174 static_refused=26 runs=11136 ok=882 requires=5435 ensures=4384 overflow=435 other_traps=0 mismatches=0; native == interpreter on every run", + "contract_fuzz_asan": "contract fuzz: units=200 compiled=174 static_refused=26 runs=11136 ok=882 requires=5435 ensures=4384 overflow=435 other_traps=0 mismatches=0", + "struct_fuzz": "struct fuzz: units=125 runs=6000 ok=3805 bounds=1087 requires=155 overflow=224 cast=729 mismatches=0; native == interpreter on every run", + "struct_fuzz_asan": "struct fuzz: units=125 runs=6000 ok=3805 bounds=1087 requires=155 overflow=224 cast=729 mismatches=0", + "struct_layout": "struct layout: L offsets 0 1 4 5 7 cells 8; M cells 64; digest binds layout", + "struct_destruction_order": "struct destruction order: alloc 1 2 3, release 3 2, alloc 4, release 4 1 (interp == native)", + "determinism": "programs=31 identical=31 (oscc in separate processes, byte-identical IR digest and machine code)", + "sanitizers": "all three test binaries also pass under -fsanitize=address,undefined -fno-sanitize-recover=all (model sweep at 10^5)", + "golden_programs": 31, + "negative_programs": 82, + "golden_corpus_sha256": "bc73ba1a6a71f8931282aaca95e3632081197b63accfca2417ce3ffe1eeae2aa", + "golden_corpus": [ + {"program": "tests/compiler/progs/arrays.osc", "source_sha256": "0d41e301111e2c132ceba7d7272eb42ae0d1b1cb0341c4f76d3fec2ce38a6ee8", "ir_sha256": "3c3b6e50085dd82cde7ec97d2feb580d38b714f8cb4f60db35b9a4f36530df0d", "code_sha256": "e6b45cce88509c39fa0503302691906d79e4189de57244a9bc1036ba15c91ac5"}, + {"program": "tests/compiler/progs/bitrev.osc", "source_sha256": "7a831a9f47434a76b65bdf837fecaf96f4a85d2c31c4682b280301b8aa588d1c", "ir_sha256": "e3aa5e5e5c58fbc686025370e8c87eceb5c4805b2a9d21d55bfbf7bfdba6cc36", "code_sha256": "94674978c5ca8c96ccc0fed847668ab4b73c0dd1f3f7527a613b26cd307c2362"}, + {"program": "tests/compiler/progs/bounds.osc", "source_sha256": "0519478f54c98ca06f3414ff9ac5dc4193ed5b2b43a7a89b2333ef3bb54b3094", "ir_sha256": "7e7bb041550f9a5a4445c0e028650f6e0cf9afcea61819a95e29b92eca13d966", "code_sha256": "07bb15370990980cd1b27be4ee87612133a58463e305e067870b6668ebc9a2d9"}, + {"program": "tests/compiler/progs/calls.osc", "source_sha256": "add5b82ec5ea73b322e9b1620220f9ff73ba9f76aeff031e0654980e2f36bcbc", "ir_sha256": "9d50bea67d3928a457201dfe35541b912b9acfbdaf97e901e7ea2e0f3185524d", "code_sha256": "a6434af2275da292a0a573b355cab439bfb26f85be6b0b0dcdc709edc66d7a94"}, + {"program": "tests/compiler/progs/casts.osc", "source_sha256": "a5093b32ee0d204d20920e5fd8f8c39c8e10b92c89aabb41fb703e7e28637fb3", "ir_sha256": "cc35e86f43ba0ebe29ea6906c0af5e01fa30964c75b0a69b05370c4100ae80b3", "code_sha256": "fdff78f367ea211da5e5058ffe715ea7fb14a1bee2727e69aefd9fda2b27bece"}, + {"program": "tests/compiler/progs/collatz.osc", "source_sha256": "912412fa1ffd990786a055d2b43a35f9fd629fa59299b1181463f3b314cd741f", "ir_sha256": "fdf94380724fa0994a074cbed2cf789c3ed4dfeb14450e648b9144ada4e57edd", "code_sha256": "9b06e959179f262f45ef72015ee1001a026262754804c107b640813131e014bf"}, + {"program": "tests/compiler/progs/contracts_basic.osc", "source_sha256": "8a1370cec47deb9671ef6e4a55a66a94b97d3f34754fa7c64ae741853134bb99", "ir_sha256": "94f2e0f6c9c8c15630725d5105007a7ecdb26212c898219b4d99be340d027ede", "code_sha256": "b7d9a360486ca56847ee83a0dc7731026305a20a9dfd250d2d6bc3197b74a0fc"}, + {"program": "tests/compiler/progs/contracts_ensures.osc", "source_sha256": "4bade10adc6078e9a8cb77b12fd529f0cb733bcf24629018a95402528a0bc3f0", "ir_sha256": "c2a7c5cca6ffd933c90d89d20e0b8dcb5c56fbf831fd9b882ad729de1efefa9f", "code_sha256": "de5746dd6d6c22049987e7bd5bdde6a238fe24785e4c654a852e383a0e657f6a"}, + {"program": "tests/compiler/progs/contracts_requires.osc", "source_sha256": "83f5842fba1f7f027cb655fcff243f1650761afedecb1cd168992a08b7924224", "ir_sha256": "16bc4b6267ee1d4a75cc60d35514baf10cc3f326b3a3e47222ca271587c9dac3", "code_sha256": "599922ff8eff90977309cf593da6cac4f951eb182bdf8a8b156c36ca6c7917ff"}, + {"program": "tests/compiler/progs/factorial.osc", "source_sha256": "3e410e9214a30628b199f7bfe8f99d7e9f439e73af2d279719fa7b523bc74e21", "ir_sha256": "cc36ae8edf9a203d4f82bd1b351b92f2df818427d26239ae8c6542e175674700", "code_sha256": "a3fc5f8c4a5ae8ce6381a0db02a03e391df791a0ce2d6f778face3a30532ecd3"}, + {"program": "tests/compiler/progs/fib.osc", "source_sha256": "9490114a0c756edab0ee7af37252b5883cc7c11b69e54ba5481530b2dcb8e03c", "ir_sha256": "bfe54f4d1502277153becb83faa0fd2975496a1875837fa8e8aa72431aa0ad00", "code_sha256": "d2e25ea89ebd59e49e29303696b7b2bff4ff39d45f62bd5566db609830d153a8"}, + {"program": "tests/compiler/progs/gcd.osc", "source_sha256": "0fad11202f1f6ce6e845b266f24df86cbc0e6160a4772c043a15b60fa54a720f", "ir_sha256": "5d649066a565d7e7ab680ea6b2827306701714da7b23755836814da7cccfa328", "code_sha256": "eb4cbe78da3176853323b2d11b970444c51fb61a33dbc770ec355cc10b94d45d"}, + {"program": "tests/compiler/progs/ifchain.osc", "source_sha256": "89fd46fc610516121dbd38cca232a98abccb55abf1593409ca575ab7e293699e", "ir_sha256": "2323f2753d8e2eb269d65db1b8e9a6006b0e09ed98e0f3f74268143cf870342d", "code_sha256": "f7c3aa9e0ae96c5d1a1c0fbcb44b7504843aac8ed5035b2400d50819cc999e03"}, + {"program": "tests/compiler/progs/isort.osc", "source_sha256": "7dceeaf139addf172152b8861602d02d654e5d2f9fab451d2c38d823ddd1b7db", "ir_sha256": "fe5d0090baeb54d9afd33a9745b7a60136e513aaca0c03f5275d5efab4de3091", "code_sha256": "b99188f630088cd1c48b4e57d48431982eb3671c50fbe5ea42e17db680baf100"}, + {"program": "tests/compiler/progs/loopbound.osc", "source_sha256": "71def740b4af102745e954e5d06582cf28b5e90e633a254a3f248bd1ed8c10c3", "ir_sha256": "d5994fccb2c93ba53d8e3a73222aa54f28f2159509f992167c8e77e779734db2", "code_sha256": "e8b5a7ca1240adbcfc66c6e1e117b01ad692c4faed32381742e0ba9a7313bd83"}, + {"program": "tests/compiler/progs/minmax.osc", "source_sha256": "95c17e1cb5ffc836ef2bcf16ab39246b5c0bbb8acb1c05a6e08b838a12b60b7e", "ir_sha256": "a5ee50c4f631cfda3282c6a6a42842f20986ae4129fa4e22656ea836c8a01543", "code_sha256": "387756318095cc219eec337923bf12440927b254cac189872b17ea617f17ae41"}, + {"program": "tests/compiler/progs/nested.osc", "source_sha256": "8e9aaf037c142ea43dcc4e9013013688eb19f03f24a9e8511058623ced32dc35", "ir_sha256": "94e13f928a06a8d2ddff59030bb412cfcb5e4888570f9b7fc44786c135415c04", "code_sha256": "0eb5b788611a907d360730c08b78c6b0cfbf3a454323b42808f77f8aed300e71"}, + {"program": "tests/compiler/progs/oom.osc", "source_sha256": "1d82ff5262c21e4760b2404dc8fc649863a35ac4a164e4ae6684adcd7cecfc7e", "ir_sha256": "6ce4fcbb5b507668183dd67678a93dad7fe11d7b3f02c2466815712fb300116c", "code_sha256": "5c788e2eef2be25b04013f7bd8378d715756293c36f9509c14e9d5d465e5552a"}, + {"program": "tests/compiler/progs/ops.osc", "source_sha256": "2f68f1cf7d7d4569c90d79e4afc2bbdb9cf1bc2fc39cc8f8e23555c90cdd0223", "ir_sha256": "9742eb592d1cfcd72c622d2ab28fe1dd1fa921f14cd3418662f303be851e1162", "code_sha256": "0c038096a7f33e702937c2979eba15d6fc5a25dd66ba12c5b746df8529e7a959"}, + {"program": "tests/compiler/progs/popcount.osc", "source_sha256": "eab7407d3cfc923d15f52e04e00d96a9e7331b0e2496552b8258e7f88bc65236", "ir_sha256": "15aa9536feb7cbbe9cd03efcf2517321964bb479c0331f45610225c64af186ac", "code_sha256": "04510e1bf63889d5dd0bb7b85eeffb7b016ad87a65a7e0e2209df082f1a9d1db"}, + {"program": "tests/compiler/progs/prefix.osc", "source_sha256": "a4852da0953c8e11e5a9e51d00be81a6cc9e1f9e938cbd9d186f34ccb53cc524", "ir_sha256": "6feaa9af48e493a02a3de461aeae3b88b1c813c9011a462537e88c661704ee63", "code_sha256": "f4c78d8644d86ce7f1edd36ebd6958d5250a34dedde3435587e34dfb2d474362"}, + {"program": "tests/compiler/progs/reborrow.osc", "source_sha256": "caf5e895fcd0660d6ca7c6360228901cb4eee5a7afe128b59334aec904e63feb", "ir_sha256": "34086f590aeaec09669b9656cfca7db57beffb67a59d7e1ba22548336de146b2", "code_sha256": "a6a0a251ed5ddcbc431e38ed73602ee9202fd2d601bd7085ae70ccb5974daf62"}, + {"program": "tests/compiler/progs/scopes.osc", "source_sha256": "9904f63cb89734f2411f04b098640cb0ebe8069a49ae7b1a40c728d434770070", "ir_sha256": "81e996a3c8c6515d57375609aba9ae7c6c5822626b1a8da9cd0943b70db4a1ed", "code_sha256": "e3faba53225b4f3a9398b5d39f6a224b24df08cf9b6a67ca5652a439f0717127"}, + {"program": "tests/compiler/progs/shifts.osc", "source_sha256": "dd33980fb0f3b8d61477b6c197508e3c46d894c0ebc0f99244c01b5e86ee8780", "ir_sha256": "7d2e542f2241ccfacbd6a812c1671733dda94410ede4e3847b43c0955a6f69b3", "code_sha256": "c07f4547cac6e250b1c76021f0955fd530844fcc593a33192592ee0f917ecf59"}, + {"program": "tests/compiler/progs/shortcircuit.osc", "source_sha256": "c3f272b3f6b6d27c9cde9887197e73dd622ea30ff802fb87c250a5e2f704e1d0", "ir_sha256": "863365c4653fb44aac42316b1cde491107ad739d58b7af5b4b55e3a2774c5d41", "code_sha256": "bbd44412068130fdf65d66f718f74c6e0e7e36ee19282f19e5c2f3bbcf66ba2e"}, + {"program": "tests/compiler/progs/sixparams.osc", "source_sha256": "535fead297b7c4b7946c25b0b1f716634b4342701c3ff486dbd8189ed8bc3770", "ir_sha256": "176dee2ab6b66ff28dc04f095964c51f59dcda5518a9696a95a9c588dd78083e", "code_sha256": "c1e8cf802ef21c2b08b08dd266303c715e64ab7323cb5267fe2492de1e54cee8"}, + {"program": "tests/compiler/progs/structs_contracts.osc", "source_sha256": "06573d57946360c9e65090b61696a005ffe3fd3f3c1ca117765d9a3028a6f43a", "ir_sha256": "06b233f9e16e229fc606c8bda44b24e43473a2cd376053341e6ee644e124fe99", "code_sha256": "d52177fe222b3e548cc1df3bb294d524b41bcb093b64e8fc41f153094eacebf0"}, + {"program": "tests/compiler/progs/structs_dtor.osc", "source_sha256": "71f951fae6f1471dc52c5edb43abfa7b7692835e0d9fe8c63ae18a2e44e97ad5", "ir_sha256": "c7b4d22c623f0b1c4d004474c532ca74cbf23911cdcd299ad81843f4ef9a27df", "code_sha256": "3d73cda95a76bd70f7a5e4aa7673a360b2c40423381db88cabadd3bbfe363b0d"}, + {"program": "tests/compiler/progs/structs_fields.osc", "source_sha256": "4da454a3b5633ddec4f39f35c67236b26b87dde922d829ff5eb39df2195b5f2e", "ir_sha256": "865a9a09510cf16363e38345c47544ac1c9b48f7eddec67729f7e1ca40d03461", "code_sha256": "69be8bd0763aec308f5a002820ae496bc86dd1ac93111e532790dab27dd10586"}, + {"program": "tests/compiler/progs/structs_owner.osc", "source_sha256": "babf04cbcfbece1e1c4c4d23c59b97fb7644f639f77d987d6732c613313170cc", "ir_sha256": "11dd47aca0387de3b7fc3f546272b165c9f2d2675626dda03a255a4dd4942807", "code_sha256": "3e1df27680d16d47f1c2568d9e7f3f066ef29eb13e0faa89a16e12c182ec3da7"}, + {"program": "tests/compiler/progs/types.osc", "source_sha256": "ca4d49a4aca05b04e9935dd7cc560a023c4e4cffa36ff84c7755883a90c2b0d5", "ir_sha256": "f623556285814fbd02241fe4f24025bf93811581655beab6073e8d867b42b3a5", "code_sha256": "45c042eeb15cebf30615b7ad341b58a41bd805d83a08c358304b4284785eee35"} + ], + "omega_core_sources_unchanged_vs_base": true, + "m6_m9_m14_gates": {"m6": {"total": 10, "passed": 10, "failed": 0}, "m9": {"total": 10, "passed": 10, "failed": 0}, "m14": {"total": 10, "passed": 10, "failed": 0}}, + "self_host": "no: the OSC-2 compiler slice cannot compile any part of itself (docs/osc/OSC-1-SELF-HOST-STATEMENT.md)", + "not_covered": "struct return values, struct-typed and owner/borrow fields, recursive structs, struct literals outside let own initialisers, whole-struct copy or compare, contracts beyond constant folding (no symbolic proof), effects/capabilities, strings, generations, arenas, unsafe physical, FFI, Flow IR optimisation, self-hosting (OSC-14); not a general Omega compiler", + "timing_measured": false +}