interplane.Get Omega
Read in your language

omega-bridge

Omega-first language interoperability: checked conversion backends and native language bindings

Brought by drakestapleton

branch main Branches Tags History

omega-bridge / docs / WALKTHROUGH.md

198 lines · 11491 bytes Raw Blame History Rendered
1# How Omega Bridge works, and how it was built
2
3This page is for an engineer who has not seen the project before. It explains the problem, the
4mechanism and the design choices, and records one approach that failed. It ends with a recipe you can
5run offline from a fresh checkout. Every claim links to the file it comes from.
6
7Revision described: `main` at `7af4d11c3a9c853abc20a99f97d0a4f01524bb66`.
8
9## The problem
10
11Omega programs compile to AArch64 machine code with the seeded Omega compiler, which has no outside
12toolchain. That keeps Omega trustworthy, but an existing C or Python application cannot use an Omega
13function unless something connects the two. The goal recorded in [DESIGN.md](DESIGN.md#user-goal) is
14to let existing languages and tools adopt Omega with as little change as possible, and to say exactly
15what is supported instead of claiming that any language translates perfectly.
16
17## The mechanism
18
19There are two separate routes. Do not confuse them.
20
211. **Conversion** ([src/toc.om](../src/toc.om)). A bounded subset of Omega is turned into GNU C11 or
22 Python 3 source. The generated program prints the same output, exits with the same status and traps
23 with the same code as the natively compiled Omega program.
242. **Native call bindings** ([src/native.om](../src/native.om), [NATIVE-ABI.md](NATIVE-ABI.md)). These
25 leave the program as Omega machine code in a shared library and generate the header or module that
26 C, C++, Python, Rust, Zig or Go needs to call it. Nothing is translated.
27
28This walkthrough covers route 1. A conversion request goes through [tools/bridge](../tools/bridge) in
29this order:
30
311. Check `build/compiler.sha256`, so the converter binary matches its source. A mismatch exits 65.
322. Check that the seed compiler's SHA-256 is
33 `191b776ef98821597c3e30655da6d680a9b66edbc9ceaca3c39c61ea6df098e9`. A mismatch exits 65.
343. Copy the input once into a private temporary directory. Validation and generation both read that
35 copy, so a file changed during the request cannot pass one step and fail the other.
364. Compile the copy with the seed compiler as a whole-unit semantic check. If the seed rejects the
37 program, nothing is emitted.
385. Feed the copy to `build/bridge`, the converter compiled from `src/toc.om`. Its front end (lexer,
39 name tables, two-pass signature scan; `tok`, `namescan` and `walk_sig` in `src/toc.om`) builds
40 bounded typed expression nodes (`node`, `expr`, `callnode`). Separate writers then emit C or Python
41 from those nodes (`eexpr`, `eassign`, `efor`, `generate`). This is code generation from parsed nodes,
42 not text substitution ([DESIGN.md, "Implemented architecture"](DESIGN.md#implemented-architecture-and-study-reconciliation-l483)).
436. Write the output to stdout only after the whole conversion succeeds. A refused feature exits with the
44 compiler's error code and leaves stdout empty.
45
46The generated code keeps Omega's observable rules: fixed-width arithmetic, arrays that carry their
47length, checked bounds, and defined traps. Python is made to wrap at fixed widths instead of using its
48unbounded integers. The full list of supported features and their limits is in
49[CAPABILITIES.md](CAPABILITIES.md). The calling convention for host programs is in [ABI.md](ABI.md):
50every exported function returns a status code and writes its result through a pointer.
51
52## How it was built
53
54- **Recovered source, not a new compiler.** `src/toc.om` began as uncommitted Omega source,
55 `seed/toc/toc.om` on omega base `29b12760f4c962606411d6bf25032aedef063301`. Its original digest is
56 in [RECOVERY.json](RECOVERY.json), and its licence was kept ([PROVENANCE.md](PROVENANCE.md)). The
57 lexer, tables and signature scan were kept and extended. The typed nodes and target writers replaced
58 its unchecked emission (commits `614f4ba`, `e24141f` and `7714900`, 2026-10-22).
59- **The pinned seed is the checker.** The bridge does not have its own judgement of what is valid
60 Omega. Every input first passes the pinned seed compiler ([tools/bridge](../tools/bridge)). The
61 native, C and Python runs therefore share one front end. They are independent executions, not
62 independent front ends ([PROVENANCE.md](PROVENANCE.md)).
63- **Exact source dependency.** [tools/bootstrap.sh](../tools/bootstrap.sh) fetches omega at the pinned
64 commit, rebuilds the seed chain (seed0, hex1, asm2, om4, omc) from source and compares three omc
65 stages. No compiler binary is stored in this repository.
66- **Opt-in only, rollback by not importing.** Conversion writes to stdout and creates temporary files
67 under `build/`. It never installs or replaces anything ([DESIGN.md, "Adoption and efficiency"](DESIGN.md#adoption-and-efficiency)).
68 To roll back, stop linking or importing the generated file.
69- **Checks have no off switch.** Library wrappers return errors as values. There is no flag that turns
70 the bounds or alias checks off ([DESIGN.md](DESIGN.md), decisions 29 and 30).
71
72## A failed approach: the recovered emitter as found
73
74The first step was to run the recovered converter unchanged against the inherited test corpus. It
75failed 8 of 20 cases ([evidence/01-recovered-red.log](../evidence/01-recovered-red.log)):
76
77```text
78FAIL t_conv ('C mismatch', 105, 0, b'', b'', b'')
79FAIL t_exit ('conversion', 16, b'toc: line 2: error 16\n')
80FAIL t_ovf ('gcc', ... implicit declaration of function 'o_add_s' ...)
81FAIL t_stack ('C mismatch', 106, -11, b'', b'', b'')
82cases=20 failures=8 RC=1
83```
84
85The emitter copied source text through with too few checks. A conversion trap (exit 105) became a C
86program that exited 0, and a stack trap (106) became a segmentation fault (-11). That is why the
87emitter was replaced by typed nodes with separate writers instead of being patched case by case. A
88second red run, [evidence/22-inherited-text-red.log](../evidence/22-inherited-text-red.log), showed
89that the inherited `text.om` program was not covered. It is now refused explicitly (see the recipe
90below).
91
92## Limitations
93
94- The converters accept a bounded scalar subset plus read-only text constants. Floats, heap operations
95 and other target languages are refused ([CAPABILITIES.md](CAPABILITIES.md)). Converting Python or C *into*
96 Omega is not implemented.
97- `tools/bootstrap.sh` proves a fixpoint, not diverse double compilation. Both routes come from related
98 seed code ([DESIGN.md](DESIGN.md)).
99- The source bootstrap needs Linux AArch64. Generated C needs a GNU C11 compiler and POSIX. Generated
100 Python needs Python 3.
101- No speed-up is claimed. Timings are recorded in `evidence/measurements.json` without a speed claim.
102
103## Reproduce it
104
105### Offline part (no GPU, no model, no network after the fetch)
106
107The host used for the run below: Linux 7.0.0-1019-nvidia aarch64, gcc 13.3.0, Python 3. The bootstrap
108needs network access once, to fetch the pinned omega commit. To avoid that, point `OMEGA_SOURCE` at an
109existing checkout.
110
111```sh
112git clone https://github.com/aien-dev/omega-bridge && cd omega-bridge
113sh tools/bootstrap.sh # ends: bootstrap fixpoint RC=0, seed 191b776e...
114sh tools/build.sh # writes build/bridge and build/compiler.sha256
115```
116
117The fetch URL in `tools/bootstrap.sh` still uses the old name, `https://github.com/aien-spark/omega.git`.
118GitHub redirects it to [aien-dev/omega-mv](https://github.com/aien-dev/omega-mv) (checked
1192026-11-04 with `gh api repos/aien-spark/omega`). The pinned commit and its hash checks do not depend on
120the name. This page leaves the script unchanged, because AGENTS.md asks for review before any change to
121the bootstrap supply chain.
122
123**Positive case.** Convert `tests/fixtures/fib.om` to both targets and compare against the natively
124compiled program:
125
126```sh
127build/bootstrap/omc < tests/fixtures/fib.om > build/fib.native && chmod +x build/fib.native
128build/fib.native > build/fib.native.out
129tools/bridge c tests/fixtures/fib.om > build/fib.c
130gcc -std=gnu11 -O2 build/fib.c -o build/fib && build/fib > build/fib.c.out
131tools/bridge python tests/fixtures/fib.om > build/fib.py && python3 build/fib.py > build/fib.py.out
132sha256sum build/fib.*.out
133```
134
135Expected: all three exit 0, print the 26 Fibonacci numbers from 0 to 75025, and have the same hash,
136`327ac5f5a3f39c7248466ce0c5863fb988abbed4ab983eb2ac039ad86b06fddd`.
137
138**The same trap in all three.** `tests/fixtures/t_div0.om` divides by zero. Native, C and Python all
139exit 102, as `tests/fixtures/t_div0.want` records.
140
141**A host caller.** `python3 tests/attachments.py` converts `examples/attachment.om`, builds
142`examples/caller.c` with UBSan and runs it and `examples/caller.py`:
143
144```text
145C caller: values, mutation, traps, invalid lengths and recovery OK
146Python caller: values, mutation, traps, invalid lengths and recovery OK
147attachment cases=2 failures=0 RC=0
148```
149
150**Refusal case.** `tests/unsupported/five_params.om` is valid Omega; the native compiler accepts it. Its
151function takes five scalar parameters, above the converter's four-parameter limit:
152
153```sh
154tools/bridge c tests/unsupported/five_params.om > build/five.c; echo rc=$?; wc -c < build/five.c
155```
156
157Expected: `toc: line 1: error 24`, `unsupported bridge capability`, `rc=24`, and `0` bytes of output.
158
159**Text case.** `tests/fixtures/text_program.om` (the inherited seed text program) now converts: its
160text becomes `static const uint8_t g0[9]={97,9,98,92,99,34,100,10,101,};` in C and immutable `bytes`
161in Python, and `tests/differential.py` checks both against the native program. `python3 tests/text.py`
162runs the full text suite (exact bytes, refusals, bounds, library views, compiled mutants).
163
164**Negative control on the pin.** A seed with one extra byte is refused before conversion:
165
166```sh
167cp build/bootstrap/omc /tmp/omc-tampered && printf '\0' >> /tmp/omc-tampered
168OMEGA_SEED=/tmp/omc-tampered tools/bridge c tests/fixtures/fib.om; echo rc=$? # rc=65, no output
169```
170
171**Full suite.** `sh tools/test.sh` ends with `ALL SUITES RC=0`. The suite's lines include
172`cases=113 failures=0 RC=0` (differential), `refusal cases=50 failures=0 RC=0`,
173`control cases=7 failures=0 RC=0` and
174`robustness cases=225 target_accepts=6 target_refusals=444 failures=0 RC=0`. It took 19 s on the host
175above.
176
177### Optional parts (not needed for the recipe above)
178
179- Native call bindings need `OMEGA_NATIVE_OMC`, the seeded omega-mv compiler; see
180 [NATIVE-ABI.md](NATIVE-ABI.md).
181- The GPU adapter converts no host program. Device runs go through CUDA++ in an authorised hardware
182 window; see [GPU-ADAPTER.md](GPU-ADAPTER.md) and [GPU-SUPPORT-MATRIX.md](GPU-SUPPORT-MATRIX.md).
183
184## Source map
185
186| What | Where |
187|---|---|
188| Conversion CLI and pin checks | [tools/bridge](../tools/bridge) |
189| Converter (front end, typed nodes, C and Python writers) | [src/toc.om](../src/toc.om) |
190| Seed bootstrap from pinned source | [tools/bootstrap.sh](../tools/bootstrap.sh) |
191| Native, C and Python differential tests | [tests/differential.py](../tests/differential.py), text: [tests/text.py](../tests/text.py) |
192| Refusals | [tests/refusals.py](../tests/refusals.py), [tests/unsupported/](../tests/unsupported/) |
193| Host callers | [tests/attachments.py](../tests/attachments.py), [examples/caller.c](../examples/caller.c), [examples/caller.py](../examples/caller.py) |
194| Damaged-output controls | [tests/controls.py](../tests/controls.py) |
195| Design decisions | [DESIGN.md](DESIGN.md) |
196| Supported features and limits | [CAPABILITIES.md](CAPABILITIES.md) |
197| Caller ABI | [ABI.md](ABI.md) |
198| Provenance | [PROVENANCE.md](PROVENANCE.md), [RECOVERY.json](RECOVERY.json) |