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 / tools / native

237 lines · 13780 bytes Raw Blame History
1#!/bin/sh
2# tools/native: one Omega library and its declarations for six languages, from Omega function units.
3# Usage: OMEGA_NATIVE_OMC=<seeded omc> tools/native [--target aarch64linux|x86linux] [--freestanding] [--host] [--float|--float-next] OUTDIR unit.om [unit.om...]
4# Each unit is one .om file in the native call subset (docs/NATIVE-ABI.md). An OM-1 unit (ABI v1): its
5# last function is bound as omega_<name>, earlier ones are private helpers. An OM-2 unit (its first
6# line that is not a note is exactly om2; ABI v2): each `export fn` (or, with none, its last function)
7# is bound as omega2_<name>, and the unit carries its identity, read through omega2_ident_<unit>(),
8# <unit> being the file's basename with every byte outside [A-Za-z0-9_] turned into _. OM-1 units are
9# compiled by OMEGA_NATIVE_OMC (pinned below), OM-2 units by OMEGA_NATIVE_OMC2, whose sha256 is the omc
10# digest embedded in their identity. For --target x86linux, OMEGA_NATIVE_X86_OMC (also pinned below)
11# compiles both profiles; OMEGA_NATIVE_OMC still builds the generator. OUTDIR receives
12# omega_native.c the library source: every unit's compiled code, embedded unchanged, behind
13# uint32_t omega_<name>(uint64_t *result, ...) or omega2_<name>(...)
14# omega_native.h its C and C++ declarations
15# omega_native.py ctypes bindings (import it from beside libomega_native.so)
16# omega_native.rs a Rust module (mod omega_native;), Rust 1.82 or later
17# omega_native.zig a Zig 0.13 file (@import("omega_native.zig"))
18# omega_native.go a cgo package omeganative: a folder of that name holding it, omega_native.h
19# and the library
20# --float selects OMEGA_NATIVE_FLOAT_OMC (e5b32567) for hosted AArch64 OM-1 units,
21# exposing f32/f64 scalar parameters/results in all six generated C/FFI bindings.
22# --float-next selects the separately pinned draft-1049 compiler through
23# OMEGA_NATIVE_FLOAT_NEXT_OMC; no existing compiler pin is replaced.
24# omega_api.h only with an OM-2 unit: its layouts as C structs and its exports as static
25# inline C functions that abort() on a nonzero status (includes omega_native.h)
26# omega_api_decl.h only with an OM-2 unit: the same layouts and the exports' prototypes only, so
27# omega_api_defs.c an object that includes it references no C library function; their
28# out-of-line definitions (the abort() calls) are in omega_api_defs.c
29# libomega_native.so the library, built with omega_native.h included, so a declaration that
30# disagrees with its definition fails this build
31# work/ the generator (native-gen), each unit's code (<unit>.bin), the framed
32# generator input (units.in) and every unit's identity (ident.txt, OM-1 units
33# included); tests/native/run.sh reads them. Not an interface.
34# Everything is produced in a scratch folder and installed together after the library compiles. A
35# refusal (a unit outside the subset, or a name the binding cannot carry) names the unit, exits 110
36# and installs nothing; a previous run's outputs are replaced only on success. Extra C flags:
37# OMEGA_NATIVE_CFLAGS.
38# --freestanding accepts pure integer-scalar OM-1 units and OM-2 units without host effects, and
39# installs omega_native.{c,h,o} plus work/; with an OM-2 unit also omega_api_decl.h and
40# omega_api_defs.{c,o}, whose wrappers call __builtin_trap() on a nonzero status (output fapic)
41# instead of abort(). No omega_api.h (its inline wrappers call abort()). The objects have no
42# undefined symbols except the GPU_FS_LINK libc-string allowlist. Calls use the caller's stack, an
43# OM-2 out parameter is a zero-filled buffer in the entry's frame copied back only on status 0, and
44# traps return status without TLS, heap or a hosted runtime.
45# Freestanding x86 output uses the same contract with a SysV adapter (owned-stack floor 0, as on AArch64).
46# --host admits the bounded OM-2 file/identity, clock and device words documented in NATIVE-ABI.md.
47# Mapping units require the exact context-format compiler fingerprint and emit explicit-context C
48# exports; other host words and OM-1 host units remain refused. Identities end with host=<words>.
49# --host supports aarch64linux and x86linux, but not freestanding.
50set -eu
51PIN=734f0a39319fbfa930bcc62cb30dbf06d47d78e126372b74f5baf478e6126b61
52X86_PIN=be8c51301307555254fc59e351d281aae1aa096d91521777e33a41c8b446448a
53FLOAT_PIN=2c82480596bbdc50cda9626866209ef662d5834f278f839ff4ce0250c495029f
54X86_HOST_PIN=c5ac9979232afd0d750e1d5088af69692032997136f761e585a25aa9a1cfc87e
55ROOT=$(cd "$(dirname "$0")/.." && pwd)
56OMC=${OMEGA_NATIVE_OMC:?set OMEGA_NATIVE_OMC to the seeded omc (omega seed/out/omc, sha256 $PIN)}
57got=$(sha256sum < "$OMC" | cut -d' ' -f1)
58[ "$got" = "$PIN" ] || { echo "tools/native: omc sha256 $got, pinned $PIN" >&2; exit 1; }
59CMODE=c
60TARGET=aarch64linux
61HOST=0
62FLOAT=0
63while [ $# -gt 0 ]; do
64 case "$1" in
65 --freestanding) CMODE=freest; shift ;;
66 --float) FLOAT=1; shift ;;
67 --float-next) FLOAT=2; shift ;;
68 --host) HOST=1; shift ;;
69 --target)
70 [ $# -ge 2 ] || { echo "tools/native: --target requires aarch64linux or x86linux" >&2; exit 2; }
71 TARGET=$2; shift 2 ;;
72 --) shift; break ;;
73 --*) echo "tools/native: unknown option $1" >&2; exit 2 ;;
74 *) break ;;
75 esac
76done
77[ "$HOST" = 0 ] || [ "$CMODE" != freest ] || { echo "tools/native: --host is not freestanding; nothing installed" >&2; exit 110; }
78if [ "$FLOAT" != 0 ]; then
79 [ "$CMODE" = c ] && [ "$TARGET" = aarch64linux ] || { echo 'tools/native: float profiles require hosted aarch64linux output' >&2; exit 110; }
80 if [ "$FLOAT" = 1 ]; then
81 FLOAT_OMC=${OMEGA_NATIVE_FLOAT_OMC:?tools/native: --float requires OMEGA_NATIVE_FLOAT_OMC (omega-mv e5b32567)}
82 else
83 FLOAT_PIN=d89bdc08ca21ea9726f1117adc791f683287d3698ea112a9db5519df2e79aeec
84 FLOAT_OMC=${OMEGA_NATIVE_FLOAT_NEXT_OMC:?tools/native: --float-next requires OMEGA_NATIVE_FLOAT_NEXT_OMC (omega-mv draft 1049)}
85 fi
86 got=$(sha256sum < "$FLOAT_OMC" | cut -d' ' -f1)
87 [ "$got" = "$FLOAT_PIN" ] || { echo "tools/native: float omc sha256 $got, pinned $FLOAT_PIN" >&2; exit 1; }
88fi
89case "$TARGET" in
90 aarch64linux) ;;
91 x86linux)
92 [ "$HOST" = 0 ] || X86_PIN=$X86_HOST_PIN
93 X86_OMC=${OMEGA_NATIVE_X86_OMC:?set OMEGA_NATIVE_X86_OMC to the pinned x86 Linux compiler (sha256 $X86_PIN)}
94 got=$(sha256sum < "$X86_OMC" | cut -d' ' -f1)
95 [ "$got" = "$X86_PIN" ] || { echo "tools/native: x86 omc sha256 $got, pinned $X86_PIN" >&2; exit 1; } ;;
96 *) echo "tools/native: unknown target $TARGET (expected aarch64linux or x86linux)" >&2; exit 2 ;;
97esac
98[ $# -ge 2 ] || { echo "usage: OMEGA_NATIVE_OMC=<omc> tools/native [--target aarch64linux|x86linux] [--freestanding] [--host] [--float|--float-next] OUTDIR unit.om [unit.om...]" >&2; exit 2; }
99OUT=$1; shift
100mkdir -p "$OUT"
101OUT=$(cd "$OUT" && pwd)
102W=$(mktemp -d "$OUT/.native-work.XXXXXX")
103mkdir "$W/work"
104trap 'rm -rf "$W"' EXIT
105"$OMC" < "$ROOT/src/native.om" > "$W/work/native-gen"; chmod +x "$W/work/native-gen"
106generate() {
107 if [ "$FLOAT" != 0 ]; then
108 if [ "$HOST" = 1 ]; then "$W/work/native-gen" "$1" fhost
109 else "$W/work/native-gen" "$1" float; fi
110 return
111 fi
112 if [ "$TARGET" = x86linux ]; then
113 if [ "$HOST" = 1 ]; then
114 "$W/work/native-gen" "$1" x86host
115 else
116 "$W/work/native-gen" "$1" x86linux
117 fi
118 elif [ "$HOST" = 1 ]; then
119 "$W/work/native-gen" "$1" host
120 else
121 "$W/work/native-gen" "$1"
122 fi
123}
124# The generator's input: each unit's source, a 0 byte, the code length in 8 decimal digits, the code;
125# for an OM-2 unit then the line `omega-unit <unit> <sha256 of its selected compiler>`.
126# Each unit is tried alone first, so a refusal names the unit.
127: > "$W/work/units.in"; : > "$W/work/ident.txt"
128v2=0
129for u in "$@"; do
130 b=$(basename "$u" .om)
131 # One snapshot supplies profile, compilation, framing and identity even if the
132 # caller replaces the original file while its compiler is running.
133 cp -- "$u" "$W/work/$b.om"
134 src=$W/work/$b.om
135 # The profile: the first line that is not a note (the generator applies the same rule).
136 if [ "$(sed -n '/^ *note\( \|$\)/d; p; q' "$src")" = om2 ]; then
137 abi=2; prof=OM-2; v2=1
138 else
139 abi=1; prof=OM-1
140 fi
141 if [ "$TARGET" = x86linux ]; then
142 pin=$X86_PIN
143 # Only compiler input gets the directive. Framing and identity retain the
144 # original snapshot, including its OM-2 profile marker and exact source hash.
145 { printf 'target x86linux\n'; cat "$src"; } > "$W/work/$b.compile.om"
146 "$X86_OMC" < "$W/work/$b.compile.om" > "$W/work/$b.bin"
147 elif [ $abi = 2 ]; then
148 OMC2=${OMEGA_NATIVE_OMC2:?tools/native: $u is an OM-2 unit; set OMEGA_NATIVE_OMC2 to the OM-2 compiler}
149 pin=$(sha256sum < "$OMC2" | cut -d' ' -f1)
150 "$OMC2" < "$src" > "$W/work/$b.bin"
151 elif [ "$FLOAT" != 0 ]; then
152 pin=$FLOAT_PIN
153 "$FLOAT_OMC" < "$src" > "$W/work/$b.bin"
154 else
155 pin=$PIN
156 "$OMC" < "$src" > "$W/work/$b.bin"
157 fi
158 name=$(printf '%s' "$b" | tr -c 'A-Za-z0-9_' '_')
159 { cat "$src"; printf '\0'; printf '%08d' "$(wc -c < "$W/work/$b.bin")"; cat "$W/work/$b.bin"
160 [ $abi = 1 ] || printf 'omega-unit %s %s\n' "$name" "$pin"; } > "$W/one.in"
161 if ! generate "$CMODE" < "$W/one.in" > /dev/null 2>&1; then
162 echo "tools/native: refused $u: outside the native call subset, or a name the binding cannot carry (docs/NATIVE-ABI.md); nothing installed" >&2
163 exit 110
164 fi
165 cat "$W/one.in" >> "$W/work/units.in"
166 if [ $abi = 2 ]; then
167 # The generator states an OM-2 unit's identity (with host= when it uses host words).
168 id=$(generate ident < "$W/one.in")
169 printf '%s path=%s\n' "$id" "$u" >> "$W/work/ident.txt"
170 else
171 echo "omega-ident 1 unit=$name abi=$abi profile=$prof src=$(sha256sum < "$src" | cut -d' ' -f1) omc=$pin path=$u" >> "$W/work/ident.txt"
172 fi
173done
174words="c h python rust zig go"; [ $v2 = 0 ] || words="$words api apih apic"
175[ "$CMODE" != freest ] || { words="freest h"; [ $v2 = 0 ] || words="$words apih fapic"; }
176for w in $words; do
177 if ! generate "$w" < "$W/work/units.in" > "$W/$w.out" 2> /dev/null; then
178 echo "tools/native: refused the set of units (a bound or unit name repeats); nothing installed" >&2
179 exit 110
180 fi
181done
182mv "$W/$CMODE.out" "$W/omega_native.c"; mv "$W/h.out" "$W/omega_native.h"
183if [ "$CMODE" = c ]; then
184 mv "$W/python.out" "$W/omega_native.py"; mv "$W/rust.out" "$W/omega_native.rs"
185 mv "$W/zig.out" "$W/omega_native.zig"; mv "$W/go.out" "$W/omega_native.go"
186fi
187if [ "$CMODE" = freest ]; then
188 [ $v2 = 0 ] || { mv "$W/apih.out" "$W/omega_api_decl.h"; mv "$W/fapic.out" "$W/omega_api_defs.c"; }
189elif [ $v2 = 1 ]; then
190 mv "$W/api.out" "$W/omega_api.h"; mv "$W/apih.out" "$W/omega_api_decl.h"; mv "$W/apic.out" "$W/omega_api_defs.c"
191fi
192# The library names itself (soname), so every caller records libomega_native.so and finds it via rpath.
193if [ "$CMODE" = freest ]; then
194 objs=omega_native.o
195 ${CC:-cc} -std=gnu11 -O2 -Wall -Wextra -Werror -Wmissing-prototypes -ffreestanding -fno-builtin -fno-stack-protector \
196 -include "$W/omega_native.h" ${OMEGA_NATIVE_CFLAGS:-} -c -o "$W/omega_native.o" "$W/omega_native.c"
197 if [ $v2 = 1 ]; then
198 objs="$objs omega_api_defs.o"
199 ${CC:-cc} -std=gnu11 -O2 -Wall -Wextra -Werror -Wmissing-prototypes -ffreestanding -fno-builtin -fno-stack-protector \
200 -I "$W" ${OMEGA_NATIVE_CFLAGS:-} -c -o "$W/omega_api_defs.o" "$W/omega_api_defs.c"
201 fi
202 # The objects resolve each other's omega2_* definitions. The GOT marker is linker metadata;
203 # no unresolved runtime function, including a libc byte helper, is permitted.
204 : > "$W/undefined.txt"; : > "$W/defined.txt"
205 for o in $objs; do
206 if ! nm -u "$W/$o" >> "$W/undefined.txt" || ! nm --defined-only -g "$W/$o" >> "$W/defined.txt"; then
207 echo "tools/native: freestanding symbol inspection failed; nothing installed" >&2
208 exit 1
209 fi
210 done
211 if ! awk -v defs="$W/defined.txt" 'BEGIN {while ((getline l < defs) > 0) {n = split(l, f); if (n) def[f[n]] = 1}}
212 NF && !($NF in def) && $NF != "_GLOBAL_OFFSET_TABLE_" {print $NF; bad=1} END {exit bad}' \
213 "$W/undefined.txt" > "$W/refused-imports.txt"; then
214 echo "tools/native: freestanding imports refused by inspection; nothing installed" >&2
215 cat "$W/refused-imports.txt" >&2
216 exit 1
217 fi
218else
219${CC:-cc} -std=gnu11 -O2 -Wall -Wextra -Werror -fPIC -shared -Wl,-soname,libomega_native.so \
220 -include "$W/omega_native.h" ${OMEGA_NATIVE_CFLAGS:-} -o "$W/libomega_native.so" "$W/omega_native.c"
221fi
222# The API headers must compile on their own: their layout size and offset checks are C static
223# assertions. The definitions are compiled against their prototypes (a disagreement fails here).
224if [ $v2 = 1 ]; then
225 [ "$CMODE" = freest ] || ${CC:-cc} -std=gnu11 -fsyntax-only -Wall -Wextra -Werror -I "$W" -x c "$W/omega_api.h"
226 ${CC:-cc} -std=gnu11 -fsyntax-only -Wall -Wextra -Werror -I "$W" -x c "$W/omega_api_decl.h"
227 ${CC:-cc} -std=gnu11 -fsyntax-only -Wall -Wextra -Werror -Wmissing-prototypes -I "$W" "$W/omega_api_defs.c"
228fi
229# Install together: the previous run's outputs go, the new set comes in.
230for f in omega_native.c omega_native.h omega_native.o omega_native.py omega_native.rs omega_native.zig omega_native.go omega_api.h omega_api_decl.h omega_api_defs.c omega_api_defs.o libomega_native.so work; do
231 rm -rf "$OUT/$f"; [ ! -e "$W/$f" ] || mv "$W/$f" "$OUT/$f"
232done
233if [ "$CMODE" = freest ]; then
234 echo "tools/native: $# unit(s) -> $OUT/omega_native.{c,h,o}$([ $v2 = 0 ] || echo ' + omega_api_decl.h, omega_api_defs.{c,o}') (+ work/)"
235else
236 echo "tools/native: $# unit(s) -> $OUT/libomega_native.so + omega_native.{c,h,py,rs,zig,go}$([ $v2 = 0 ] || echo ' + omega_api.h, omega_api_decl.h, omega_api_defs.c') (+ work/)"
237fi