Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/omega-bridge/patches/eb0ce0945fadde4d # omega-bridge Omega-first language interoperability: checked conversion backends and native language bindings Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers ## GitHub aien-dev/omega-bridge#26 (code only) closed derived `eb0ce0945fadde4d` #26 Imported from GitHub aien-dev/omega-bridge#26 (code only) GitHub's metadata for this pull request is unavailable: its state is DERIVED from the commits (merged: the head is reachable from this project's main; open: GitHub kept a merge ref; else closed). Base `ed420fcbffa1ad83db78111deb836dd880ed891e` · Head `619a7fed1d66dba8dfff28204462d19f045ee2f2` ### Fetch it A git bundle of the pull request's head, 8090 bytes. ``` curl -fsSL https://interplane.io/drakestapleton/omega-bridge/patches/eb0ce0945fadde4d.bundle -o eb0ce0945fadde4d.bundle git fetch eb0ce0945fadde4d.bundle refs/pull/26/head ``` Raw bundle · Diff ### Provenance This proposal is imported from GitHub; it is not signed by its author. Its import statement and source record: ``` github-import 1 aien-dev/omega-bridge 26 619a7fed1d66dba8dfff28204462d19f045ee2f2 ``` ``` {"kind":"github-import","owner":"aien-dev","repo":"omega-bridge","number":26,"url":"https://github.com/aien-dev/omega-bridge/pull/26","export_sha256":"967c79e26a893657b5cdaa45efd442be5dbd2ecfe68e25f9a8c1b5da4d60d452","imported_utc":"2026-11-18T16:39:57Z","metadata":"unavailable","state_derived":true} ``` ### Changes ``` diff --git a/AGENTS.md b/AGENTS.md index 9a6abba..e11feb7 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -58,6 +58,9 @@ python3 tests/attachments.py # Native call ABI v1: one compiled Omega function called from C, C++, Python, Rust, Zig, Go OMEGA_NATIVE_OMC=/path/to/omega-mv/seed/out/omc sh tests/native/run.sh +# f32 and ushort array kinds (C oracle, refusals, add->sub mutant) +OMEGA_NATIVE_OMC=/path/to/omega-mv/seed/out/omc sh tests/native/kinds.sh + # Hosted x86 native output (cross-build only; this command does not execute callers) OMEGA_NATIVE_OMC=/path/to/baseline/omc OMEGA_NATIVE_X86_OMC=/path/to/x86/omc \ CC=x86_64-linux-gnu-gcc tools/native --target x86linux build/native-x86 tests/native/madd.om diff --git a/README.md b/README.md index 2bd66d1..7847eef 100644 --- a/README.md +++ b/README.md @@ -42,6 +42,8 @@ OMEGA_NATIVE_OMC= tools/native build/native tests/native/madd.om That writes `libomega_native.so` and, from the same Omega signatures, `omega_native.h` (C and C++), `omega_native.py`, `omega_native.rs`, `omega_native.zig` and `omega_native.go`; a program includes or imports its file and calls `omega_madd`, nothing is declared by hand. One generator, [src/native.om](src/native.om), writes all six. Contract, limits and the qualification run: [native call ABI](docs/NATIVE-ABI.md). +`f32` and `ushort` array kinds (4-byte and 2-byte elements) are qualified by `OMEGA_NATIVE_OMC= sh tests/native/kinds.sh`. The contract is in [native call ABI](docs/NATIVE-ABI.md). + The native target defaults to `aarch64linux`. Hosted x86-64 SysV Linux bindings are selected explicitly; there is no build-host architecture inference: diff --git a/docs/NATIVE-ABI.md b/docs/NATIVE-ABI.md index 9d71830..8c1648e 100644 --- a/docs/NATIVE-ABI.md +++ b/docs/NATIVE-ABI.md @@ -213,8 +213,16 @@ not compiler-custody or behavioral evidence for the deliberately mismatched code Names must be C identifiers, a parameter name may not end in `_len` (the binding names a length `o__len`), and each unit name appears once; anything else is refused before any C is written. -Supported signatures: up to 6 parameters, each `ulong`, `long`, or `edit`/`view` of `ulong` or `uchar` -with a fixed length; result `ulong`, `long` or `void`. A unit may hold several functions: the seeded +Supported signatures: up to 6 parameters, each `ulong`, `long`, or `edit`/`view` of `ulong`, `uchar`, +`f32` or `ushort` with a fixed length; result `ulong`, `long` or `void`. An `f32` array is packed +binary32 (4-byte elements, 4-byte alignment) and a `ushort` array is packed 16-bit words (2-byte +elements, 2-byte alignment), for example binary16 bits that the Omega code converts itself. C and C++ +declarations stay `void *` / `const void *`; the generated library checks the element size, alignment, +length and overlap. Python ctypes uses `c_float` and `c_uint16`. Rust and Zig declarations type these +arrays as byte pointers (`*mut u8` / `*const u8`, Zig `?[*]u8`); Go stays `unsafe.Pointer`. +`OMEGA_NATIVE_OMC= sh tests/native/kinds.sh` binds `fsum.om` and `halve.om`, compares every +lane to a C `_Float16` oracle, refuses misaligned, wrong-length, overlapping and null arrays (status +32), and requires a mutant that changes fsum's `add` to `sub` to fail. A unit may hold several functions: the seeded compiler enters function-mode code at the unit's last function (the leading entry branches to it), so the binding exports that one and the earlier functions stay private helpers, called directly inside the embedded code, with any Omega types. Only the last function's signature must be supported diff --git a/src/native.om b/src/native.om index eac6ae2..02f39bb 100644 --- a/src/native.om +++ b/src/native.om @@ -7,8 +7,10 @@ note zig and go are that library's declarations for those languages, written fro note signatures (tools/native runs all six). Output on stdout, only after every unit is accepted. note The bound function is the unit's last one, which is where the compiler enters function-mode note code (word 0 branches to it); earlier functions are private helpers with any types. The bound -note function's parameters are ulong or long scalars, or edit/view arrays of ulong or uchar; its -note result is ulong, long or void. Anything else, and a missing or unknown argument, is refused: +note function's parameters are ulong or long scalars, or edit/view arrays of ulong, uchar, f32 or +note ushort; its result is ulong, long or void. An f32 array is 4-byte aligned binary32 and a ushort +note array is 2-byte aligned. Rust and Zig declarations type those two as byte pointers; the C library +note enforces element size and alignment. Python ctypes uses c_float and c_uint16. note trap 10 (exit 110) and no output. global inb uchar 4194304 global outb uchar 16777216 @@ -30,13 +32,13 @@ text xjmp ":\\nendbr64\\njmp ombr_code_~:\\nendbr64\\njmp ombr2_code_" text xasm "/* SysV AMD64 adapter to the pinned private Omega frame ABI. R12 anchors the native\n * stack, R14 the runtime table, R15 the owned-stack floor. Advanced host helpers\n * that clobber those registers are forbidden in function mode; legacy read/write preserve them.\n * CET shadow-stack enforcement is not supported by the nonlocal trap return. */\n__asm__(\".text\\n.balign 16\\n.local ombr_enter\\n.hidden ombr_enter\\n.type ombr_enter,@function\\nombr_enter:\\n\"\n \"endbr64\\npush %rbx\\npush %rbp\\npush %r12\\npush %r13\\npush %r14\\npush %r15\\npush %rcx\\n\"\n \"mov %rsp,%r12\\nmov %rdx,%r14\\nmov %r9,%r15\\nmov %rdi,%r10\\n\"\n \"mov %r8,%rbp\\nmov %r8,%rsp\\nsub $256,%rsp\\n\"\n \"mov 0(%rsi),%rax\\nmov %rax,-8(%rbp)\\nmov 8(%rsi),%rax\\nmov %rax,-16(%rbp)\\n\"\n \"mov 16(%rsi),%rax\\nmov %rax,-24(%rbp)\\nmov 24(%rsi),%rax\\nmov %rax,-32(%rbp)\\n\"\n \"mov 32(%rsi),%rax\\nmov %rax,-40(%rbp)\\nmov 40(%rsi),%rax\\nmov %rax,-48(%rbp)\\n\"\n \"call *%r10\\nmov %r12,%rsp\\nmov (%rsp),%rcx\\nmov %rax,(%rcx)\\nxor %eax,%eax\\n\"\n \"ombr_leave:\\nadd $8,%rsp\\npop %r15\\npop %r14\\npop %r13\\npop %r12\\npop %rbp\\npop %rbx\\nret\\n\"\n \".size ombr_enter,.-ombr_enter\\n.local ombr_trap\\n.hidden ombr_trap\\n.type ombr_trap,@function\\nombr_trap:\\n\"\n \"endbr64\\nmov %r12,%rsp\\nlea -1(%rsi),%eax\\ncmp $13,%eax\\nmov $34,%eax\\ncmovbe %esi,%eax\\njmp ombr_leave\\n\"\n \".size ombr_trap,.-ombr_trap\\n.local ombr_no_entry\\n.hidden ombr_no_entry\\n.type ombr_no_entry,@function\\nombr_no_entry:\\n\"\n \"endbr64\\nmov %r12,%rsp\\nmov $34,%eax\\njmp ombr_leave\\n.size ombr_no_entry,.-ombr_no_entry\\n\");\nuint32_t ombr_xenter(const void *code, const uint64_t *args, const ombr_rt *rt, uint64_t *out, void *top, void *floor) __asm__(\"ombr_enter\") __attribute__((visibility(\"hidden\")));\n/* The true mapping floor must not move when ABI-v2 out buffers lower top. */\n#define ombr_enter(code,args,rt,out,top) ombr_xenter(code,args,rt,out,top,ombr_stk - OMBR_STACK)\n" text xs "/* ombr_enter" text xe "static void ombr_free" -text kw "toeditviewulonglongucharvoidmain" +text kw "toeditviewulonglongucharvoidmainf32ushort" text fr "__asm__(\".section .text.omega_bridge,\\\"ax\\\",@progbits\\n.balign 4\\n.local ombr_code_~\\n.hidden ombr_code_~\\nombr_code_~:\\n.byte ~\\n.previous\\n\");\nextern const unsigned char ombr_entry_~[] __attribute__((visibility(\"hidden\")));\n__attribute__((visibility(\"default\"))) uint32_t omega_~(uint64_t *result~, uint64_t o_~, void *o_~, const void *o_~_len~)\n{\n ombr_span sp[7];\n unsigned ns = 0;\n uint64_t a[6] = {0};\n if (ombr_span_add(sp, &ns, result, 1u, 1u, 8u, 1))\n return OMEGA_BAD_ARG;\n~ if (ombr_span_add(sp, &ns, o_~, o_~_len, ~u, ~))\n return OMEGA_BAD_ARG;\n a[~] = (uint64_t)(uintptr_t)o_~;\n~ a[~] = o_~ if (ombr_overlap(sp, ns))\n return OMEGA_BAD_ARG;\n return ombr_run(ombr_entry_~, a, result, ~);\n}\n~\\n.local ombr_entry_~\\n.hidden ombr_entry_~\\nombr_entry_~:\\nhint #34\\nb ombr_code_" text pre "/* Generated by omega-bridge src/native.om: native call ABI v1 (docs/NATIVE-ABI.md).\n * The Omega machine code below is embedded byte for byte as the seeded compiler wrote it.\n * This file only adapts the C calling convention to it. Regenerate it; do not edit it. */\n#include \n#include \n#include \n#include \n#define OMEGA_OK 0u\n#define OMEGA_BAD_ARG 32u\n#define OMEGA_REENTRY 33u\n#define OMEGA_BAD_RT 34u\n#define OMEGA_NO_STACK 35u\n#define OMBR_STACK (8u << 20)\n#define OMBR_GUARD (8u << 20)\n/* entry[2] is the trap entry (OSC-1 section 7). */\ntypedef struct ombr_rt { const void *entry[32]; } ombr_rt;\ntypedef struct ombr_span { uintptr_t lo, hi; int w; } ombr_span;\nstatic _Thread_local volatile sig_atomic_t ombr_active;\nstatic _Thread_local unsigned char *ombr_stk;\nstatic pthread_key_t ombr_key;\nstatic int ombr_keyok;\nstatic pthread_once_t ombr_once = PTHREAD_ONCE_INIT;\nuint32_t ombr_enter(const void *code, const uint64_t *args, const ombr_rt *rt, uint64_t *out, void *top)\n __attribute__((visibility(\"hidden\")));\nextern const unsigned char ombr_trap[] __attribute__((visibility(\"hidden\")));\nextern const unsigned char ombr_no_entry[] __attribute__((visibility(\"hidden\")));\n/* ombr_enter saves the callee-saved registers on the caller's stack, keeps that stack pointer in x19 and\n * runs the code on the binding's own stack. A normal return stores x0 through out and\n * returns 0. A runtime entry restores the stack pointer from x19 and returns a status; out is not\n * written. The seeded compiler never writes x19..x28. Runtime entries are reached by blr/br, so they\n * start with bti jc. */\n__asm__(\".text\\n.balign 4\\n.local ombr_enter\\n.hidden ombr_enter\\n.type ombr_enter,%function\\nombr_enter:\\n\"\n \"hint #34\\nstp x29,x30,[sp,#-176]!\\nmov x29,sp\\nstp x19,x20,[sp,#16]\\nstp x21,x22,[sp,#32]\\n\"\n \"stp x23,x24,[sp,#48]\\nstp x25,x26,[sp,#64]\\nstp x27,x28,[sp,#80]\\nstp d8,d9,[sp,#96]\\n\"\n \"stp d10,d11,[sp,#112]\\nstp d12,d13,[sp,#128]\\nstp d14,d15,[sp,#144]\\nstr x3,[sp,#160]\\n\"\n \"mov x19,sp\\nmov x16,x0\\nmov x17,x1\\nmov x7,x2\\nmov sp,x4\\n\"\n \"ldp x0,x1,[x17]\\nldp x2,x3,[x17,#16]\\nldp x4,x5,[x17,#32]\\nblr x16\\n\"\n \"mov sp,x19\\nldr x9,[sp,#160]\\nstr x0,[x9]\\nmov w0,#0\\n\"\n \"ombr_leave:\\nldp d14,d15,[sp,#144]\\nldp d12,d13,[sp,#128]\\nldp d10,d11,[sp,#112]\\n\"\n \"ldp d8,d9,[sp,#96]\\nldp x27,x28,[sp,#80]\\nldp x25,x26,[sp,#64]\\nldp x23,x24,[sp,#48]\\n\"\n \"ldp x21,x22,[sp,#32]\\nldp x19,x20,[sp,#16]\\nldp x29,x30,[sp],#176\\nret\\n.size ombr_enter,.-ombr_enter\\n\"\n \".local ombr_trap\\n.hidden ombr_trap\\n.type ombr_trap,%function\\nombr_trap:\\nhint #38\\n\"\n \"mov sp,x19\\nsub x2,x1,#1\\nmov w3,#34\\ncmp x2,#13\\ncsel w0,w1,w3,ls\\nb ombr_leave\\n\"\n \".size ombr_trap,.-ombr_trap\\n\"\n \".local ombr_no_entry\\n.hidden ombr_no_entry\\n.type ombr_no_entry,%function\\nombr_no_entry:\\nhint #38\\n\"\n \"mov sp,x19\\nmov w0,#34\\nb ombr_leave\\n.size ombr_no_entry,.-ombr_no_entry\\n\");\nstatic void ombr_free(void *p)\n{\n munmap(p, OMBR_STACK + OMBR_GUARD);\n}\nstatic void ombr_init(void)\n{\n ombr_keyok = pthread_key_create(&ombr_key, ombr_free) == 0;\n}\n/* The thread's Omega stack: OMBR_STACK usable bytes above an OMBR_GUARD no-access region. A function-mode\n * unit has no call cycles and at most 4 MiB of local arrays per function (OMEGESE, local arrays), so a frame that\n * would pass the bottom starts inside the guard and faults there; nothing below the guard is touched. */\nstatic unsigned char *ombr_stack(void)\n{\n if (ombr_stk)\n return ombr_stk;\n pthread_once(&ombr_once, ombr_init);\n if (!ombr_keyok)\n return 0;\n unsigned char *p = mmap(0, OMBR_STACK + OMBR_GUARD, PROT_READ | PROT_WRITE,\n MAP_PRIVATE | MAP_ANONYMOUS | MAP_NORESERVE, -1, 0);\n if (p == MAP_FAILED)\n return 0;\n if (mprotect(p, OMBR_GUARD, PROT_NONE) != 0 || pthread_setspecific(ombr_key, p) != 0) {\n munmap(p, OMBR_STACK + OMBR_GUARD);\n return 0;\n }\n ombr_stk = p + OMBR_GUARD + OMBR_STACK;\n return ombr_stk;\n}\nstatic int ombr_span_add(ombr_span *sp, unsigned *ns, const void *p, uint64_t n, uint64_t want, uint64_t es, int w)\n{\n uintptr_t lo = (uintptr_t)p;\n if (!p || n != want || lo % es != 0u || n > (UINTPTR_MAX - lo) / es)\n return 1;\n sp[*ns].lo = lo;\n sp[*ns].hi = lo + n * es;\n sp[*ns].w = w;\n (*ns)++;\n return 0;\n}\nstatic int ombr_overlap(const ombr_span *sp, unsigned ns)\n{\n for (unsigned i = 0; i < ns; i++)\n for (unsigned j = i + 1; j < ns; j++)\n if ((sp[i].w || sp[j].w) && sp[i].lo < sp[j].hi && sp[j].lo < sp[i].hi)\n return 1;\n return 0;\n}\n/* One read-only runtime table for every call and thread: the code only reads it (a write would fault). */\nstatic const ombr_rt ombr_rt_table = {{ ombr_no_entry, ombr_no_entry, ombr_trap, ombr_no_entry, ombr_no_entry, ombr_no_entry, ombr_no_entry, ombr_no_entry, ombr_no_entry, ombr_no_entry, ombr_no_entry, ombr_no_entry, ombr_no_entry, ombr_no_entry, ombr_no_entry, ombr_no_entry, ombr_no_entry, ombr_no_entry, ombr_no_entry, ombr_no_entry, ombr_no_entry, ombr_no_entry, ombr_no_entry, ombr_no_entry, ombr_no_entry, ombr_no_entry, ombr_no_entry, ombr_no_entry, ombr_no_entry, ombr_no_entry, ombr_no_entry, ombr_no_entry }};\nstatic uint32_t ombr_run(const void *code, const uint64_t *args, uint64_t *result, int has_value)\n{\n if (ombr_active)\n return OMEGA_REENTRY;\n ombr_active = 1;\n unsigned char *top = ombr_stack();\n if (!top) {\n ombr_active = 0;\n return OMEGA_NO_STACK;\n }\n uint64_t v = 0;\n uint32_t s = ombr_enter(code, args, &ombr_rt_table, &v, top);\n ombr_active = 0;\n if (s == OMEGA_OK)\n *result = has_value ? v : 0u;\n return s;\n}\n" note mo: the output names; tb: the binding texts, fragments separated by ~ (byte 126): 0-11 h, note 12-21 python, 22-34 rust, 35-46 zig, 47-64 go (its C preamble includes the generated header). text mo "chpythonrustziggoapihapicfreest" -text tb "/* Generated by omega-bridge src/native.om: native call ABI v1 (docs/NATIVE-ABI.md).\n * C and C++ declarations for libomega_native. Regenerate it; do not edit it.\n * Status: 0 done; 1..14 the Omega trap code; 32 argument refused (null, wrong length, misaligned,\n * overlap); 33 reentry; 34 bad runtime entry; 35 no stack. The result is written only on status 0.\n * A long is its two's-complement bits; an array is its address then its element count.\n * Parameter names carry o_ so no language's reserved words collide. */\n#ifndef OMEGA_NATIVE_H\n#define OMEGA_NATIVE_H\n#include \n#ifdef __cplusplus\nextern \"C\" {\n#endif\n#define OMEGA_OK 0u\n#define OMEGA_BAD_ARG 32u\n#define OMEGA_REENTRY 33u\n#define OMEGA_BAD_RT 34u\n#define OMEGA_NO_STACK 35u\n~/* ~ */\nuint32_t omega_~(uint64_t *result~, uint64_t o_~, void *o_~, const void *o_~, void *o_~, const void *o_~_len~);\n~#ifdef __cplusplus\n}\n#endif\n#endif\n~# Generated by omega-bridge src/native.om: native call ABI v1 (docs/NATIVE-ABI.md).\n# ctypes declarations for libomega_native.so beside this file. Regenerate it; do not edit it.\n# Status: 0 done; 1..14 the Omega trap code; 32 argument refused (None, wrong length, misaligned,\n# overlap); 33 reentry; 34 bad runtime entry; 35 no stack. The result (a ctypes.c_uint64 passed with\n# ctypes.byref) is written only on status 0. A scalar is an int in 0 .. 2**64-1 (a long is passed as its\n# two's-complement bits); anything else raises ctypes.ArgumentError, nothing is truncated. An array is a\n# ctypes array of its element type (bytes for a view uchar) then its element count; an edit array must\n# be writable, so bytes are refused there.\nimport ctypes, os\nOMEGA_OK, OMEGA_BAD_ARG, OMEGA_REENTRY, OMEGA_BAD_RT, OMEGA_NO_STACK = 0, 32, 33, 34, 35\nlib = ctypes.CDLL(os.path.join(os.path.dirname(os.path.abspath(__file__)), \"libomega_native.so\"))\nclass _u64:\n @classmethod\n def from_param(cls, v):\n if type(v) is not int or v < 0 or v > 18446744073709551615:\n raise ValueError(\"omega scalar: an int in 0 .. 2**64-1 expected, got %r\" % (v,))\n return ctypes.c_uint64(v)\n_res = ctypes.POINTER(ctypes.c_uint64)\n_a3 = ctypes.POINTER(ctypes.c_uint64) # edit ulong: a writable ctypes array of c_uint64\n_a4 = ctypes.POINTER(ctypes.c_uint64) # view ulong: a ctypes array of c_uint64\n_a5 = ctypes.POINTER(ctypes.c_ubyte) # edit uchar: a writable ctypes array of c_ubyte\n_a6 = ctypes.c_char_p # view uchar: bytes, or a ctypes array of c_char\n~# ~\nomega_~ = lib.omega_~\nomega_~.argtypes = [_res~, _u64~, _a~]\nomega_~.restype = ctypes.c_uint32\n~// Generated by omega-bridge src/native.om: native call ABI v1 (docs/NATIVE-ABI.md).\n// Declarations for libomega_native (build with -L ; Rust 1.82 or later). Regenerate it; do not edit it.\n// Status: 0 done; 1..14 the Omega trap code; 32 argument refused (null, wrong length, misaligned,\n// overlap); 33 reentry; 34 bad runtime entry; 35 no stack. The result is written only on status 0.\n// A long is its two's-complement bits (an i64 as u64); an array is its pointer then its element count.\n#[allow(dead_code)]\npub const OMEGA_OK: u32 = 0;\n#[allow(dead_code)]\npub const OMEGA_BAD_ARG: u32 = 32;\n#[allow(dead_code)]\npub const OMEGA_REENTRY: u32 = 33;\n#[allow(dead_code)]\npub const OMEGA_BAD_RT: u32 = 34;\n#[allow(dead_code)]\npub const OMEGA_NO_STACK: u32 = 35;\n#[allow(dead_code)]\n#[link(name = \"omega_native\")]\nunsafe extern \"C\" {\n~ // ~\n pub fn omega_~(result: *mut u64~, o_~: u64~: *mut u64~: *const u64~: *mut u8~: *const u8~_len: u64~) -> u32;\n~}\n~// Generated by omega-bridge src/native.om: native call ABI v1 (docs/NATIVE-ABI.md).\n// Declarations for libomega_native (build with -L -lomega_native -lc). Regenerate it; do not edit it.\n// Status: 0 done; 1..14 the Omega trap code; 32 argument refused (null, wrong length, misaligned,\n// overlap); 33 reentry; 34 bad runtime entry; 35 no stack. The result is written only on status 0.\n// A long is its two's-complement bits (@bitCast); an array is its pointer then its element count.\npub const OMEGA_OK: u32 = 0;\npub const OMEGA_BAD_ARG: u32 = 32;\npub const OMEGA_REENTRY: u32 = 33;\npub const OMEGA_BAD_RT: u32 = 34;\npub const OMEGA_NO_STACK: u32 = 35;\n~// ~\npub extern fn omega_~(result: ?*u64~, o_~: u64~: ?[*]u64~: ?[*]const u64~: ?[*]u8~: ?[*]const u8~_len: u64~) u32;\n~// Generated by omega-bridge src/native.om: native call ABI v1 (docs/NATIVE-ABI.md).\n// cgo package omeganative: this file, omega_native.h and libomega_native.so in one folder. Regenerate it;\n// do not edit it. No run-time path is baked in: the program's build names the folder (CGO_LDFLAGS set to\n// -Wl,-rpath,), or LD_LIBRARY_PATH does.\n// Status: 0 done; 1..14 the Omega trap code; 32 argument refused (nil, wrong length, misaligned,\n// overlap); 33 reentry; 34 bad runtime entry; 35 no stack. The result is written only on status 0.\n// A long is its two's-complement bits (an int64 as uint64); an array is an unsafe.Pointer to memory\n// holding no Go pointers, then its element count.\npackage omeganative\n\n/*\n#cgo CFLAGS: -I${SRCDIR}\n#cgo LDFLAGS: -L${SRCDIR} -lomega_native\n#include \"omega_native.h\"\n*/\nimport \"C\"\n\nimport \"unsafe\"\n\nconst (\n\tOmegaOK uint32 = 0\n\tOmegaBadArg uint32 = 32\n\tOmegaReentry uint32 = 33\n\tOmegaBadRt uint32 = 34\n\tOmegaNoStack uint32 = 35\n)\n~\n// ~\nfunc Omega_~(result *uint64~, o_~ uint64~ unsafe.Pointer~ unsafe.Pointer~ unsafe.Pointer~ unsafe.Pointer~_len uint64~) uint32 {\n\treturn uint32(C.omega_~((*C.uint64_t)(unsafe.Pointer(result))~, C.uint64_t(o_~)~, o_~_len)~))\n}\n" +text tb "/* Generated by omega-bridge src/native.om: native call ABI v1 (docs/NATIVE-ABI.md).\n * C and C++ declarations for libomega_native. Regenerate it; do not edit it.\n * Status: 0 done; 1..14 the Omega trap code; 32 argument refused (null, wrong length, misaligned,\n * overlap); 33 reentry; 34 bad runtime entry; 35 no stack. The result is written only on status 0.\n * A long is its two's-complement bits; an array is its address then its element count.\n * Parameter names carry o_ so no language's reserved words collide. */\n#ifndef OMEGA_NATIVE_H\n#define OMEGA_NATIVE_H\n#include \n#ifdef __cplusplus\nextern \"C\" {\n#endif\n#define OMEGA_OK 0u\n#define OMEGA_BAD_ARG 32u\n#define OMEGA_REENTRY 33u\n#define OMEGA_BAD_RT 34u\n#define OMEGA_NO_STACK 35u\n~/* ~ */\nuint32_t omega_~(uint64_t *result~, uint64_t o_~, void *o_~, const void *o_~, void *o_~, const void *o_~_len~);\n~#ifdef __cplusplus\n}\n#endif\n#endif\n~# Generated by omega-bridge src/native.om: native call ABI v1 (docs/NATIVE-ABI.md).\n# ctypes declarations for libomega_native.so beside this file. Regenerate it; do not edit it.\n# Status: 0 done; 1..14 the Omega trap code; 32 argument refused (None, wrong length, misaligned,\n# overlap); 33 reentry; 34 bad runtime entry; 35 no stack. The result (a ctypes.c_uint64 passed with\n# ctypes.byref) is written only on status 0. A scalar is an int in 0 .. 2**64-1 (a long is passed as its\n# two's-complement bits); anything else raises ctypes.ArgumentError, nothing is truncated. An array is a\n# ctypes array of its element type (bytes for a view uchar, c_float for f32, c_uint16 for ushort) then its element count; an edit array must\n# be writable, so bytes are refused there.\nimport ctypes, os\nOMEGA_OK, OMEGA_BAD_ARG, OMEGA_REENTRY, OMEGA_BAD_RT, OMEGA_NO_STACK = 0, 32, 33, 34, 35\nlib = ctypes.CDLL(os.path.join(os.path.dirname(os.path.abspath(__file__)), \"libomega_native.so\"))\nclass _u64:\n @classmethod\n def from_param(cls, v):\n if type(v) is not int or v < 0 or v > 18446744073709551615:\n raise ValueError(\"omega scalar: an int in 0 .. 2**64-1 expected, got %r\" % (v,))\n return ctypes.c_uint64(v)\n_res = ctypes.POINTER(ctypes.c_uint64)\n_a3 = ctypes.POINTER(ctypes.c_uint64) # edit ulong: a writable ctypes array of c_uint64\n_a4 = ctypes.POINTER(ctypes.c_uint64) # view ulong: a ctypes array of c_uint64\n_a5 = ctypes.POINTER(ctypes.c_ubyte) # edit uchar: a writable ctypes array of c_ubyte\n_a6 = ctypes.c_char_p # view uchar: bytes, or a ctypes array of c_char\n_a7 = ctypes.POINTER(ctypes.c_float) # edit f32: a writable ctypes array of c_float\n_a8 = ctypes.POINTER(ctypes.c_float) # view f32: a ctypes array of c_float\n_a9 = ctypes.POINTER(ctypes.c_uint16) # edit ushort: a writable ctypes array of c_uint16\n_a10 = ctypes.POINTER(ctypes.c_uint16) # view ushort: a ctypes array of c_uint16\n~# ~\nomega_~ = lib.omega_~\nomega_~.argtypes = [_res~, _u64~, _a~]\nomega_~.restype = ctypes.c_uint32\n~// Generated by omega-bridge src/native.om: native call ABI v1 (docs/NATIVE-ABI.md).\n// Declarations for libomega_native (build with -L ; Rust 1.82 or later). Regenerate it; do not edit it.\n// Status: 0 done; 1..14 the Omega trap code; 32 argument refused (null, wrong length, misaligned,\n// overlap); 33 reentry; 34 bad runtime entry; 35 no stack. The result is written only on status 0.\n// A long is its two's-complement bits (an i64 as u64); an array is its pointer then its element count.\n#[allow(dead_code)]\npub const OMEGA_OK: u32 = 0;\n#[allow(dead_code)]\npub const OMEGA_BAD_ARG: u32 = 32;\n#[allow(dead_code)]\npub const OMEGA_REENTRY: u32 = 33;\n#[allow(dead_code)]\npub const OMEGA_BAD_RT: u32 = 34;\n#[allow(dead_code)]\npub const OMEGA_NO_STACK: u32 = 35;\n#[allow(dead_code)]\n#[link(name = \"omega_native\")]\nunsafe extern \"C\" {\n~ // ~\n pub fn omega_~(result: *mut u64~, o_~: u64~: *mut u64~: *const u64~: *mut u8~: *const u8~_len: u64~) -> u32;\n~}\n~// Generated by omega-bridge src/native.om: native call ABI v1 (docs/NATIVE-ABI.md).\n// Declarations for libomega_native (build with -L -lomega_native -lc). Regenerate it; do not edit it.\n// Status: 0 done; 1..14 the Omega trap code; 32 argument refused (null, wrong length, misaligned,\n// overlap); 33 reentry; 34 bad runtime entry; 35 no stack. The result is written only on status 0.\n// A long is its two's-complement bits (@bitCast); an array is its pointer then its element count.\npub const OMEGA_OK: u32 = 0;\npub const OMEGA_BAD_ARG: u32 = 32;\npub const OMEGA_REENTRY: u32 = 33;\npub const OMEGA_BAD_RT: u32 = 34;\npub const OMEGA_NO_STACK: u32 = 35;\n~// ~\npub extern fn omega_~(result: ?*u64~, o_~: u64~: ?[*]u64~: ?[*]const u64~: ?[*]u8~: ?[*]const u8~_len: u64~) u32;\n~// Generated by omega-bridge src/native.om: native call ABI v1 (docs/NATIVE-ABI.md).\n// cgo package omeganative: this file, omega_native.h and libomega_native.so in one folder. Regenerate it;\n// do not edit it. No run-time path is baked in: the program's build names the folder (CGO_LDFLAGS set to\n// -Wl,-rpath,), or LD_LIBRARY_PATH does.\n// Status: 0 done; 1..14 the Omega trap code; 32 argument refused (nil, wrong length, misaligned,\n// overlap); 33 reentry; 34 bad runtime entry; 35 no stack. The result is written only on status 0.\n// A long is its two's-complement bits (an int64 as uint64); an array is an unsafe.Pointer to memory\n// holding no Go pointers, then its element count.\npackage omeganative\n\n/*\n#cgo CFLAGS: -I${SRCDIR}\n#cgo LDFLAGS: -L${SRCDIR} -lomega_native\n#include \"omega_native.h\"\n*/\nimport \"C\"\n\nimport \"unsafe\"\n\nconst (\n\tOmegaOK uint32 = 0\n\tOmegaBadArg uint32 = 32\n\tOmegaReentry uint32 = 33\n\tOmegaBadRt uint32 = 34\n\tOmegaNoStack uint32 = 35\n)\n~\n// ~\nfunc Omega_~(result *uint64~, o_~ uint64~ unsafe.Pointer~ unsafe.Pointer~ unsafe.Pointer~ unsafe.Pointer~_len uint64~) uint32 {\n\treturn uint32(C.omega_~((*C.uint64_t)(unsafe.Pointer(result))~, C.uint64_t(o_~)~, o_~_len)~))\n}\n" note Freestanding output accepts integer scalars only: no arrays, storage, host words or OM-2. global fns ulong 1024 global fne ulong 1024 @@ -525,7 +527,8 @@ end ret fl end note pkind: the kind of the parameter whose type starts at token ty: 1 ulong, 2 long, 3 edit ulong, -note 4 view ulong, 5 edit uchar, 6 view uchar; 0 when refused. +note 4 view ulong, 5 edit uchar, 6 view uchar, 7 edit f32, 8 view f32, 9 edit ushort, 10 view ushort; +note 0 when refused. Odd array kinds are writable. fn pkind ty ulong nt ulong to ulong if eq tkis ty 10 5 1 ret 1 @@ -551,6 +554,12 @@ end if eq tkis el 19 5 1 ret add 5 w end +if eq tkis el 32 3 1 +ret add 7 w +end +if eq tkis el 35 6 1 +ret add 9 w +end ret 0 end note cidr: 1 when bytes s .. e-1 form a C identifier ([A-Za-z_][A-Za-z0-9_]*), else 0. @@ -767,7 +776,7 @@ if le x 2 ofr 7 opn j else -if or eq x 3 eq x 5 +if eq rem x 2 1 ofr 8 else ofr 9 @@ -791,14 +800,18 @@ ofr 15 if le x 4 odec 8 else +if le x 6 odec 1 -end -ofr 15 -if or eq x 3 eq x 5 -odec 1 else -odec 0 +if le x 8 +odec 4 +else +odec 2 +end end +end +ofr 15 +odec rem x 2 ofr 16 odec j ofr 17 @@ -827,8 +840,9 @@ odec get st 3 ofr 23 end note edeclC: a declaration with the type before the name (h): the comment -note fragments at cb (open, close + head) and the type fragments at tb (result, scalar, the four -note array kinds, the length suffix, close). +note fragments at cb (open, close + head) and the type fragments at tb (result, scalar, writable +note void*, readonly const void*, two unused old kind slots, the length suffix, close). Writable +note arrays, including f32 and ushort, use the void* fragment; readonly arrays use const void*. fn edeclC cb ulong tp ulong to void ofb cb osig @@ -841,7 +855,11 @@ if le x 2 ofb add tp 1 opn j else -ofb add tp sub x 1 +if eq rem x 2 1 +ofb add tp 2 +else +ofb add tp 3 +end opn j ofb add tp 1 opn j @@ -852,6 +870,8 @@ ofb add tp 7 end note edeclR: a declaration with the name before the type (rust, zig, the go wrapper head): the note type fragments at tp are result, the name prefix, scalar, the four array kinds, length, close. +note Kinds above 6 (f32, ushort) reuse the uchar pointer fragment of the same writability, so Rust +note and Zig see a byte pointer; the C library still checks the element size. fn edeclR cb ulong tp ulong to void ofb cb osig @@ -865,7 +885,12 @@ opn j if le x 2 ofb add tp 2 else -ofb add tp x +ax = x +if gt x 6 +ax = rem x 2 +ax = sub 6 ax +end +ofb add tp ax ofb add tp 1 opn j ofb add tp 7 diff --git a/tests/native/fsum.om b/tests/native/fsum.om new file mode 100644 index 0000000..fa9f8ea --- /dev/null +++ b/tests/native/fsum.om @@ -0,0 +1,18 @@ +note fsum: d[i] = a[i] + f16 h[i] in f32 for 4 lanes (h binary16 bits, converted here; zero and normal +note inputs), and returns the f32 bit pattern of d[0] + d[3]: an f32 view, a ushort view and an f32 edit array. +fn h2b h ushort to uint +v = uint h +s = shl and shr v 15 1 31 +e = and shr v 10 31 +m = and v 1023 +if eq e 0 +ret s +end +ret or or s shl add e 112 23 shl m 13 +end +fn fsum a view f32 4 h view ushort 4 d edit f32 4 to ulong +for i ulong 4 +put d i add get a i bitcast f32 h2b get h i +end +ret ulong bitcast uint add get d 0 get d 3 +end diff --git a/tests/native/halve.om b/tests/native/halve.om new file mode 100644 index 0000000..be08ce2 --- /dev/null +++ b/tests/native/halve.om @@ -0,0 +1,7 @@ +note halve: h[i] = binary16 bits of half the f16 value h[i] (exponent minus 1; normal inputs above 2^-13), +note an edit ushort array written in place. +fn halve h edit ushort 3 to void +for i ulong 3 +put h i sub get h i 1024 +end +end diff --git a/tests/native/kinds.c b/tests/native/kinds.c new file mode 100644 index 0000000..4c85991 --- /dev/null +++ b/tests/native/kinds.c @@ -0,0 +1,37 @@ +// kinds.c: f32 and ushort array kinds of native ABI v1 (docs/NATIVE-ABI.md), checked against values computed +// by this C caller (AArch64 _Float16 conversion as the f16 oracle), plus the refusals for those kinds. +#include +#include +#include +uint32_t omega_fsum(uint64_t *r, const void *a, uint64_t an, const void *h, uint64_t hn, void *d, uint64_t dn); +uint32_t omega_halve(uint64_t *r, void *h, uint64_t hn); +static int fails; +static void check(const char *what, int ok) { printf("%s %s\n", ok ? "PASS" : "FAIL", what); if (!ok) fails++; } +int main(void) { + float a[4] = { 1.5f, -2.25f, 1e-3f, 65504.0f }, d[4]; + _Float16 hf[4] = { (_Float16) 0.5f, (_Float16) -3.0f, (_Float16) 0.0999755859375f, (_Float16) 2.0f }; + uint16_t h[4]; memcpy(h, hf, sizeof h); + uint64_t r = 0; + check("fsum status 0", omega_fsum(&r, a, 4, h, 4, d, 4) == 0); + int same = 1; + for (int i = 0; i < 4; i++) { float w = a[i] + (float) hf[i]; same &= memcmp(&w, &d[i], 4) == 0; } + check("fsum f32 lanes == C oracle", same); + float s = d[0] + d[3]; uint32_t sb; memcpy(&sb, &s, 4); + check("fsum result bits == C oracle", r == sb); + _Float16 g[3] = { (_Float16) 6.0f, (_Float16) -0.75f, (_Float16) 1024.0f }; uint16_t gb[3]; memcpy(gb, g, sizeof gb); + check("halve status 0", omega_halve(&r, gb, 3) == 0); + int halved = 1; + for (int i = 0; i < 3; i++) { _Float16 w = g[i] / 2; uint16_t wb; memcpy(&wb, &w, 2); halved &= wb == gb[i]; } + check("halve ushort edit == C oracle", halved); + char raw[64] __attribute__((aligned(8))); + memcpy(raw + 4, a, 16); + check("f32 view misaligned (2 bytes) refused", omega_fsum(&r, raw + 2, 4, h, 4, d, 4) == 32); + check("f32 view at 4-byte alignment accepted", omega_fsum(&r, raw + 4, 4, h, 4, d, 4) == 0); + check("ushort view misaligned refused", omega_fsum(&r, a, 4, (char *) h + 1, 4, d, 4) == 32); + check("f32 wrong length refused", omega_fsum(&r, a, 3, h, 4, d, 4) == 32); + check("f32 edit overlapping f32 view refused", omega_fsum(&r, a, 4, h, 4, a, 4) == 32); + check("ushort view overlapping f32 edit refused", omega_fsum(&r, a, 4, (uint16_t *) d, 4, d, 4) == 32); + check("null ushort edit refused", omega_halve(&r, NULL, 3) == 32); + printf("KINDS %s\n", fails ? "FAIL" : "PASS"); + return fails != 0; +} diff --git a/tests/native/kinds.sh b/tests/native/kinds.sh new file mode 100755 index 0000000..055e2e0 --- /dev/null +++ b/tests/native/kinds.sh @@ -0,0 +1,51 @@ +#!/bin/sh +# f32 and ushort array kinds of the native call ABI (docs/NATIVE-ABI.md): two Omega units with f32/ushort +# view and edit arrays, called from C and compared to C-computed values, the refusals for those kinds, +# and a negative control (fsum's add turned into sub) that must fail. Same pinned compiler as run.sh. +# The generator's one argument names the output (c, python, rust); a missing argument is a refusal. +set -eu +PIN=734f0a39319fbfa930bcc62cb30dbf06d47d78e126372b74f5baf478e6126b61 +ROOT=$(cd "$(dirname "$0")/../.." && pwd) +T=$ROOT/tests/native +O=$ROOT/build/native-kinds +OMC=${OMEGA_NATIVE_OMC:?set OMEGA_NATIVE_OMC to the seeded omc (omega seed/out/omc, sha256 $PIN)} +got=$(sha256sum < "$OMC" | cut -d' ' -f1) +[ "$got" = "$PIN" ] || { echo "FAIL omc sha256 $got, pinned $PIN"; exit 1; } +rm -rf "$O"; mkdir -p "$O/mut" +"$OMC" < "$ROOT/src/native.om" > "$O/native"; chmod +x "$O/native" +frame() { # frame + "$OMC" < "$2" > "$1/fsum.bin"; "$OMC" < "$T/halve.om" > "$1/halve.bin" + { cat "$2"; printf '\0'; printf '%08d' "$(wc -c < "$1/fsum.bin")"; cat "$1/fsum.bin" + cat "$T/halve.om"; printf '\0'; printf '%08d' "$(wc -c < "$1/halve.bin")"; cat "$1/halve.bin"; } +} +build() { # build + frame "$1" "$2" | "$O/native" c > "$1/omega_native.c" + cc -std=gnu11 -O2 -Wall -Wextra -Werror -o "$1/kinds" "$T/kinds.c" "$1/omega_native.c" +} +build "$O" "$T/fsum.om" +"$O/kinds" +# Declaration texts for the same frame. Python names the element types. Rust types f32/ushort as +# byte pointers (docs/NATIVE-ABI.md); a fragment-index overrun would not contain these markers. +frame "$O" "$T/fsum.om" | "$O/native" python > "$O/omega_native.py" +grep -q '_a7 = ctypes.POINTER(ctypes.c_float)' "$O/omega_native.py" +grep -q '_a10 = ctypes.POINTER(ctypes.c_uint16)' "$O/omega_native.py" +grep -q 'omega_fsum.argtypes = \[_res, _a8, _u64, _a10, _u64, _a7, _u64\]' "$O/omega_native.py" +frame "$O" "$T/fsum.om" | "$O/native" rust > "$O/omega_native.rs" +grep -q 'o_a: \*const u8, o_a_len: u64, o_h: \*const u8, o_h_len: u64, o_d: \*mut u8, o_d_len: u64' "$O/omega_native.rs" +# A uint array is still outside the subset: exit 110, and the previous C file is untouched. +cp "$O/omega_native.c" "$O/keep.c" +printf 'fn bad a view uint 1 to ulong\nret 0\nend\n' > "$O/bad.om" +"$OMC" < "$O/bad.om" > "$O/bad.bin" +rc=0 +{ cat "$O/bad.om"; printf '\0'; printf '%08d' "$(wc -c < "$O/bad.bin")"; cat "$O/bad.bin"; } | "$O/native" c > "$O/bad.out" 2>"$O/bad.err" || rc=$? +if [ "$rc" = 110 ] && [ ! -s "$O/bad.out" ] && cmp -s "$O/omega_native.c" "$O/keep.c"; then + echo "PASS view uint refused (exit 110), previous output untouched" +else + echo "FAIL view uint refusal (exit $rc)"; cat "$O/bad.err"; exit 1 +fi +sed 's/^put d i add get a i bitcast f32 h2b get h i$/put d i sub get a i bitcast f32 h2b get h i/' "$T/fsum.om" > "$O/mut/fsum.om" +grep -q '^put d i sub' "$O/mut/fsum.om" || { echo "FAIL mutant not applied"; exit 1; } +build "$O/mut" "$O/mut/fsum.om" +if "$O/mut/kinds" > "$O/mut/log" 2>&1; then echo "FAIL mutant fsum (add -> sub) not caught"; exit 1; fi +echo "PASS mutant fsum (add -> sub) caught ($(grep -c '^FAIL' "$O/mut/log") failing checks)" +echo "NATIVE-KINDS PASS" ```