interplane.Get Omega
Read in your language

omega-bridge

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

Brought by drakestapleton

GitHub aien-dev/omega-bridge#24 (code only)

closed derived 6998fb6dc01349f0 #24

Imported from GitHub aien-dev/omega-bridge#24 (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 4f231fa972e772a930e954ac56ecc1d727929269 · Head 4a0a26ff96deb53d81a3e665d550b14a9cbc89d2

Fetch it

A git bundle of the pull request's head, 24987 bytes.

curl -fsSL https://interplane.io/drakestapleton/omega-bridge/patches/6998fb6dc01349f0.bundle -o 6998fb6dc01349f0.bundle
git fetch 6998fb6dc01349f0.bundle refs/pull/24/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
24
4a0a26ff96deb53d81a3e665d550b14a9cbc89d2
{"kind":"github-import","owner":"aien-dev","repo":"omega-bridge","number":24,"url":"https://github.com/aien-dev/omega-bridge/pull/24","export_sha256":"967c79e26a893657b5cdaa45efd442be5dbd2ecfe68e25f9a8c1b5da4d60d452","imported_utc":"2026-11-18T16:39:57Z","metadata":"unavailable","state_derived":true}

Changes

diff --git a/docs/NATIVE-ABI.md b/docs/NATIVE-ABI.md
index 8391609..5afad11 100644
--- a/docs/NATIVE-ABI.md
+++ b/docs/NATIVE-ABI.md
@@ -202,7 +202,15 @@ is refused (exit 110, no output). The default AArch64 C file:
 - gives the code a read-only 32-entry runtime table whose trap entry restores the caller's stack pointer from x19
   and returns the trap code from the exported function (no setjmp/longjmp, nothing crosses the caller's
   frame) and whose other entries refuse with status 34, since function-mode units of this ABI use none
-  of them. This relies on the seeded compiler never writing x19..x28 (OSC-1 uses x0..x16, x29, x30);
+  of them. The contract for a unit's code is what the trampoline guarantees: it may write x0..x17,
+  x20..x28, v0..v31 and the flags (x20..x28 and d8..d15 are restored on a return and on a trap); it must
+  not write x18 (the platform register, which the trampoline does not save), x19 or x29 and must keep sp
+  balanced. The hosted and the freestanding trampolines give the same contract. OSC-1 (OM-1) allocates x0..x6, x8..x15 and x17, with x7
+  the runtime table and x16 scratch; an OM-2 omc with loop-constant hoisting (omega-mv
+  perf/omc-loop-checks) also keeps loop constants in x20..x26, and its int/float conversions can write
+  d8..d15. tests/native/v2 decodes `hoist`'s code (x20..x26 and d8..d15 written, x18, x19 and x29 never
+  named in any unit) and calls both trampolines directly with a hand-assembled unit that writes x20..x29
+  and d8..d15, with one mutant per restore pair in each;
 - reaches each unit through a two-instruction stub (`bti c`, branch) so the code runs under branch
   target identification (BTI) without changing the compiler's bytes;
 - exports, per unit, `uint32_t omega_<name>(uint64_t *result, ...)`.
@@ -442,7 +450,8 @@ also checks that the binding refuses 2^64+3, -1 and 2.0 as scalars and `bytes` f
 that a call works after those refusals. The C caller also checks a null buffer, a wrong length,
 misalignment, result/buffer overlap, a view overlapping an edit array, a call after traps, reentry from a
 signal handler (status 33), a trap from a frame holding a 4,000,000-byte local array, that array on a
-256 KiB thread, and that x19..x28 and d8..d15 keep the caller's values across a normal return and a trap.
+256 KiB thread, and that x19..x28 and d8..d15 keep the caller's values across a normal return and a trap
+raised in a callee of the exported function, and runs the hoisting unit on two threads.
 The Rust caller runs the array on a default thread and is built twice, under edition 2021 and edition
 2024, with warnings as errors. The C caller runs a second time against a library built with BTI enforced.
 `tests/native/reference.c`, prototypes for the seven test units (with `hsum`, bound at its last function)
@@ -647,6 +656,12 @@ and element counts, the out pointers, and the traps the callers expect (the `par
 contract's order, element bounds, trap 9 after an out write). Before S4 existed the bridge was developed
 against hand-written OM-1 stand-ins of these units (commit 1392549); they are removed.
 
+The `hoist` unit needs an OM-2 compiler that hoists loop constants: recorded with omega-mv
+`perf/omc-loop-checks` @ c4953df7b (draft PR #865), sha256
+`13ce76c274d6bdce50613a242faa7cb07aadf65441ceecddf2d06bcfa4e48b69`, which keeps at most seven constants
+(x20..x26). Its decoded `hoist` code writes x20..x26 and d13, d14 and s15; that of 0d006207 (no hoisting)
+writes none of x20..x26, and the hoist check fails with it.
+
 The run checks: v1 byte identity (above); every identity string against `sha256sum` (oracle); the
 embedded code against the compiler's bytes; the exact exported symbol set (15 `omega2_` symbols, 12
 exports and 3 identities, and `omega_madd`; no private helper, no v1 name for a v2 unit); no caller
@@ -655,9 +670,14 @@ Python, Rust (2021 and 2024), Zig and Go callers (16 to 18 checks each); the lin
 languages (v1 bindings of an OM-1 unit named `bsum` against the library: `undefined reference to
 omega_bsum`, Python `undefined symbol: omega_bsum`); the stale identity (a header from `vec.om` with one
 note byte changed is detected; its Python binding refuses to import; the matching header passes); 15
-generator refusals of OM-2 input with a control; and three negative controls (the wrap check, the
+generator refusals of OM-2 input with a control; three negative controls (the wrap check, the
 edit-overlap rule and the out copy each removed from a copy of the generator must make the C caller
-fail, on the checks named).
+fail, on the checks named); `hoist`'s decoded destinations (x20..x26 and some of d8..d15 written) and no
+unit's code naming x19 or x29; and the trampoline called directly (`tests/native/v2/tramp.c`, which
+includes the generated C, so no C wrapper can keep a register for it) with a hand-assembled unit that
+writes x20..x29 and d8..d15, on a return and on a trap, plus ten mutants, each removing one restore from
+`ombr_leave` (x19/x20, x21/x22, x23/x24, x25/x26, x27/x28, the four d pairs, x29) in a copy of the
+generator, every one of which must make it fail.
 
 For the out-of-line API the run also checks: every existing output (`c h python rust zig go api`)
 byte-identical to 3bb8b8a's generator on the v2 set; `tests/native/v2/fsuse.c`, built with
diff --git a/src/native.om b/src/native.om
index 461077a..ab1e5b4 100644
--- a/src/native.om
+++ b/src/native.om
@@ -41,7 +41,7 @@ text xby "\\n.byte "
 text xfasm "/* SysV AMD64 adapter to the pinned private Omega frame ABI on the caller's stack. ombr_enter saves\n * the callee-saved registers there, keeps that stack pointer in R12, points R14 at the runtime table\n * and sets the owned-stack floor R15 to 0, so, as in the AArch64 binding, the pure scalar code runs on\n * the caller's stack with no bound check. A normal return stores RAX through out and returns 0. A\n * runtime entry restores the stack pointer from R12 and returns a status; out is not written.\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\\nxor %r15d,%r15d\\nmov %rdi,%r10\\n\"\n        \"mov %rsp,%rbp\\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\");\n"
 text kw "toeditviewulonglongucharvoidmainf32ushortconst"
 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 <pthread.h>\n#include <signal.h>\n#include <stdint.h>\n#include <sys/mman.h>\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"
+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 <pthread.h>\n#include <signal.h>\n#include <stdint.h>\n#include <sys/mman.h>\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. A unit's code must not write x18, x19 or x29 and must keep sp balanced; it may write x0..x17,\n * x20..x28 and v0..v31 (x20..x28 and d8..d15 are restored here). Runtime entries are reached by blr/br, so they 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 "chpythonrustziggoapihapicfreestfapic"
@@ -55,7 +55,7 @@ global fne ulong 1024
 global fnc ulong 1
 global fo2 ulong 1
 text fw "|note|fn|to|ret|if|else|end|for|while|bound|break|continue|trap|requires|ensures|invariant|result|const|bool|uchar|ushort|uint|ulong|byte|short|int|long|true|false|add|sub|mul|div|rem|and|or|xor|shl|shr|neg|not|eq|ne|lt|le|gt|ge|bitcast|(|)|=|void|#|om2|export|layout|field|out|at|little|big|edit|view|fresh|get|put|len|new|part|unroll|inline|rotr|rotl|*|"
-text fp "/* Generated pure integer-scalar freestanding native ABI v1 binding. */\n#include <stdint.h>\n#define OMEGA_OK 0u\n#define OMEGA_BAD_ARG 32u\n#define OMEGA_BAD_RT 34u\ntypedef struct ombr_rt { const void *entry[32]; } ombr_rt;\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 pure scalar code on the caller's 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\\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 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    uint64_t v = 0;\n    uint32_t status = ombr_enter(code, args, &ombr_rt_table, &v, 0);\n    if (status == OMEGA_OK)\n        *result = has_value ? v : 0u;\n    return status;\n}\n"
+text fp "/* Generated pure integer-scalar freestanding native ABI v1 binding. */\n#include <stdint.h>\n#define OMEGA_OK 0u\n#define OMEGA_BAD_ARG 32u\n#define OMEGA_BAD_RT 34u\ntypedef struct ombr_rt { const void *entry[32]; } ombr_rt;\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 pure scalar code on the caller's 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. A unit's code must not write x18, x19 or x29 and must keep sp balanced; it may write\n * x0..x17, x20..x28 and v0..v31 (x20..x28 and d8..d15 are restored here). 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\\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 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    uint64_t v = 0;\n    uint32_t status = ombr_enter(code, args, &ombr_rt_table, &v, 0);\n    if (status == OMEGA_OK)\n        *result = has_value ? v : 0u;\n    return status;\n}\n"
 text fb ")\n{\n    uint64_t a[6] = {0};\n    if (!result || (uintptr_t)result % 8u != 0u || (uintptr_t)result > UINTPTR_MAX - 8u)\n        return OMEGA_BAD_ARG;\n~    return ombr_run(ombr_entry_"
 fn fword k ulong to ulong
 i = ulong 0
diff --git a/tests/native/v2/caller.c b/tests/native/v2/caller.c
index 7408d34..655bd91 100644
--- a/tests/native/v2/caller.c
+++ b/tests/native/v2/caller.c
@@ -1,4 +1,4 @@
-/* Native ABI v2 oracle (tests/native/v2/run.sh): calls the OM-2 test units vec, lay and one, and the
+/* Native ABI v2 oracle (tests/native/v2/run.sh): calls the OM-2 test units vec, lay, one and hoist, and the
  * OM-1 unit madd, through the generated declarations only (omega_native.h, omega_api.h). Expected
  * values are computed here independently of Omega. Prints one PASS or FAIL line per check; exit 0
  * only when all pass. */
@@ -66,6 +66,42 @@ static void *swap_thread(void *arg)
     return 0;
 }
 
+/* hoist_thread: 5000 calls of the hoisting unit on this thread's own Omega stack; every fifth call traps
+ * in inner, after the loop wrote this thread's array. */
+static void *hoist_thread(void *arg)
+{
+    uint8_t *ok = arg;
+    uint64_t d[64] = {0};
+    *ok = 1;
+    for (int k = 0; k < 5000; k++) {
+        uint64_t r = R0, t = (uint64_t)(k % 5 == 4);
+        uint32_t s = omega2_hadd(&r, d, 64, t);
+        if (t ? s != 9 || r != R0 : s != OMEGA_OK || r != 1920569)
+            *ok = 0;
+        for (int i = 0; i < 64; i++)
+            if (d[i] != 70000ull * (uint64_t)(k + 1))
+                *ok = 0;
+    }
+    return 0;
+}
+
+/* ombr_probe(fn, result, a, b, c): set x19..x28 and d8..d15 to known values, call fn(result, a, b, c),
+ * then return the status in the low 32 bits and the number of changed registers in the high 32 bits
+ * (as tests/native/caller.c). */
+uint64_t ombr_probe(void *fn, uint64_t *result, uint64_t a, uint64_t b, uint64_t c);
+__asm__(".text\n.balign 4\n.globl ombr_probe\n.type ombr_probe,%function\nombr_probe:\nhint #34\n"
+        "stp x29,x30,[sp,#-160]!\nmov x29,sp\nstp x19,x20,[sp,#16]\nstp x21,x22,[sp,#32]\nstp x23,x24,[sp,#48]\n"
+        "stp x25,x26,[sp,#64]\nstp x27,x28,[sp,#80]\nstp d8,d9,[sp,#96]\nstp d10,d11,[sp,#112]\n"
+        "stp d12,d13,[sp,#128]\nstp d14,d15,[sp,#144]\nmov x16,x0\nmov x0,x1\nmov x1,x2\nmov x2,x3\nmov x3,x4\n"
+        "mov x19,#119\nmov x20,#120\nmov x21,#121\nmov x22,#122\nmov x23,#123\nmov x24,#124\nmov x25,#125\nmov x26,#126\nmov x27,#127\nmov x28,#128\n"
+        "mov x9,#108\nfmov d8,x9\nmov x9,#109\nfmov d9,x9\nmov x9,#110\nfmov d10,x9\nmov x9,#111\nfmov d11,x9\nmov x9,#112\nfmov d12,x9\nmov x9,#113\nfmov d13,x9\nmov x9,#114\nfmov d14,x9\nmov x9,#115\nfmov d15,x9\n"
+        "blr x16\nmov w0,w0\nmov x9,#0\n"
+        "cmp x19,#119\ncinc x9,x9,ne\ncmp x20,#120\ncinc x9,x9,ne\ncmp x21,#121\ncinc x9,x9,ne\ncmp x22,#122\ncinc x9,x9,ne\ncmp x23,#123\ncinc x9,x9,ne\ncmp x24,#124\ncinc x9,x9,ne\ncmp x25,#125\ncinc x9,x9,ne\ncmp x26,#126\ncinc x9,x9,ne\ncmp x27,#127\ncinc x9,x9,ne\ncmp x28,#128\ncinc x9,x9,ne\n"
+        "fmov x10,d8\ncmp x10,#108\ncinc x9,x9,ne\nfmov x10,d9\ncmp x10,#109\ncinc x9,x9,ne\nfmov x10,d10\ncmp x10,#110\ncinc x9,x9,ne\nfmov x10,d11\ncmp x10,#111\ncinc x9,x9,ne\nfmov x10,d12\ncmp x10,#112\ncinc x9,x9,ne\nfmov x10,d13\ncmp x10,#113\ncinc x9,x9,ne\nfmov x10,d14\ncmp x10,#114\ncinc x9,x9,ne\nfmov x10,d15\ncmp x10,#115\ncinc x9,x9,ne\n"
+        "orr x0,x0,x9,lsl #32\nldp d14,d15,[sp,#144]\nldp d12,d13,[sp,#128]\nldp d10,d11,[sp,#112]\nldp d8,d9,[sp,#96]\n"
+        "ldp x27,x28,[sp,#80]\nldp x25,x26,[sp,#64]\nldp x23,x24,[sp,#48]\nldp x21,x22,[sp,#32]\nldp x19,x20,[sp,#16]\n"
+        "ldp x29,x30,[sp],#160\nret\n.size ombr_probe,.-ombr_probe\n");
+
 int main(void)
 {
     uint64_t r;
@@ -348,6 +384,29 @@ int main(void)
     pthread_join(t2, 0);
     check("two threads x 20000 swap8 calls", ok1 && ok2);
 
+    /* Callee-saved registers: hoist's code keeps hadd's seven loop constants in x20..x26 and inner's in x20
+     * and x21, and inner's conversions write d8..d15 (with a hoisting OM-2 omc; run.sh fails unless the code
+     * writes them). The caller's x19..x28 and d8..d15 survive a normal return and a trap 9 raised in inner,
+     * the exported function's callee. 1920569 = 64 * 29997 + 761 (inner's result for k = 0). */
+    uint64_t hd[64], pv;
+    for (int i = 0; i < 64; i++)
+        hd[i] = (uint64_t)i;
+    r = R0;
+    pv = ombr_probe((void *)omega2_hadd, &r, (uint64_t)(uintptr_t)hd, 64, 0);
+    int hok = 1;
+    for (int i = 0; i < 64; i++)
+        hok &= hd[i] == (uint64_t)i + 70000;
+    check("hoist: x19..x28 and d8..d15 kept across a normal return", (uint32_t)pv == OMEGA_OK && (pv >> 32) == 0 && r == 1920569 && hok);
+    r = R0;
+    pv = ombr_probe((void *)omega2_hadd, &r, (uint64_t)(uintptr_t)hd, 64, 1);
+    check("hoist: x19..x28 and d8..d15 kept across a trap 9 in a callee", (uint32_t)pv == 9 && (pv >> 32) == 0 && r == R0);
+    uint8_t hk1 = 0, hk2 = 0;
+    pthread_create(&t1, 0, hoist_thread, &hk1);
+    pthread_create(&t2, 0, hoist_thread, &hk2);
+    pthread_join(t1, 0);
+    pthread_join(t2, 0);
+    check("two threads x 5000 hadd calls (every fifth a trap)", hk1 && hk2);
+
     free(lb);
     free(lw);
     printf("%s %d\n", fails ? "C-V2 FAIL" : "C-V2 PASS", fails);
diff --git a/tests/native/v2/hoist.om b/tests/native/v2/hoist.om
new file mode 100644
index 0000000..5876d6b
--- /dev/null
+++ b/tests/native/v2/hoist.om
@@ -0,0 +1,47 @@
+note hoist.om: an OM-2 unit for the native ABI v2 tests whose code writes callee-saved registers. An OM-2 omc
+note with loop-constant hoisting (omega-mv perf/omc-loop-checks, omc 13ce76c2) keeps the seven literals of
+note hadd's constant-count loop in x20..x26, the most it hoists, and inner's two in x20 and x21; inner's
+note conversions with eleven values live are made in v8 and up, so they write d8..d15. caller.c checks that
+note x19..x28 and d8..d15 keep the caller's values across a normal return and a trap 9 raised in inner, a
+note callee of the exported function, after both functions wrote those registers. run.sh decodes the code
+note and fails unless x20..x26 and some of d8..d15 are written and x19 and x29 are not.
+om2
+fn inner k ulong to ulong
+s = ulong 0
+for i ulong 64
+s = add s 50001
+s = sub s 50000
+end
+b = add s 1
+c = add s 2
+d = add s 3
+e = add s 4
+f = add s 5
+g = add s 6
+h = add s 7
+j = add s 8
+x = ulong f64 add s 10
+y = ulong f32 f64 add s 11
+if ne k 0
+trap 9
+end
+s = add s add b c
+s = add s add d e
+s = add s add f g
+s = add s add h j
+s = add s x
+ret add s y
+end
+export fn hadd d edit ulong 64 k ulong to ulong
+s = ulong 0
+for i ulong 64
+put d i add get d i 70000
+s = add s 90001
+s = sub s 80002
+s = add s 60003
+s = sub s 50004
+s = add s 40005
+s = sub s 30006
+end
+ret add s inner k
+end
diff --git a/tests/native/v2/run.sh b/tests/native/v2/run.sh
index 644d201..b96a8dd 100755
--- a/tests/native/v2/run.sh
+++ b/tests/native/v2/run.sh
@@ -1,9 +1,9 @@
 #!/bin/sh
 # Native call ABI v2 qualification (docs/NATIVE-ABI.md, ABI v2). OMEGA_NATIVE_OMC names the seeded omc
 # (pinned by tools/native); OMEGA_NATIVE_OMC2 names the OM-2 compiler that compiles this folder's OM-2
-# units (omega-mv om2/compiler-s4 @ f6a6492fd, sha256 49923639...803ec, when this run was recorded; its
-# digest is what the identities carry). A missing language toolchain is reported
-# NOT_RUN, never PASS. Exit 0 only when every run check passed. `run.sh measure` also runs the cost
+# units; it must hoist loop constants (omega-mv hoist/865-main, the #865 port onto main 9920aab1, sha256
+# 07820046...def7, when this run was recorded; its digest is what the identities carry), or the hoist
+# check fails. A missing language toolchain is reported NOT_RUN, never PASS. Exit 0 only when every run check passed. `run.sh measure` also runs the cost
 # measurement (tests/native/v2/measure.c).
 set -eu
 ROOT=$(cd "$(dirname "$0")/../../.." && pwd)
@@ -20,9 +20,9 @@ fails=0; notrun=""
 pass() { echo "PASS $1"; }
 fail() { echo "FAIL $1"; fails=$((fails + 1)); }
 echo "INFO omc $got; OM-2 compiler $OMC2 sha256 $(sha256sum < "$OMC2" | cut -d' ' -f1)"
-V2="vec lay one"
+V2="vec lay one hoist"
 units="$ROOT/tests/native/madd.om"; for u in $V2; do units="$units $T/$u.om"; done
-if "$ROOT/tools/native" "$O" $units > "$O/tools-native.log" 2>&1; then pass "tools/native: madd (OM-1) + vec, lay, one (OM-2): library, six bindings, omega_api.h"
+if "$ROOT/tools/native" "$O" $units > "$O/tools-native.log" 2>&1; then pass "tools/native: madd (OM-1) + vec, lay, one, hoist (OM-2): library, six bindings, omega_api.h"
 else fail "tools/native"; cat "$O/tools-native.log"; exit 1; fi
 W=$O/work; GEN=$W/native-gen
 
@@ -87,13 +87,28 @@ for u in $V2; do
   cmp -s "$W/$u.bin" "$O/$u.emb" || ok=0
 done
 if [ $ok = 1 ]; then pass "embedded code == compiler output, per OM-2 unit"; else fail "embedded code differs"; fi
+# Registers the code writes, decoded: the first operand of every instruction that has a destination (not
+# a store, compare, test or branch), and the second one too of a load pair. hoist must write x20..x26
+# (hadd's seven hoisted constants) and some of d8..d15 (inner's conversions; s8..s15 count); no unit's code
+# may name x18 (the platform register, which ombr_enter does not save), x19 or x29 at all (NATIVE-ABI.md:
+# the trampoline's trap path rests on x19). A non-hoisting OMEGA_NATIVE_OMC2 fails here.
+dst() { objdump -D -b binary -m aarch64 "$1" | awk -F'\t' 'NF >= 4 && $3 !~ /^(st|cmp|cmn|tst|cb|tb|b$|b\.|bl|br|ret|ccmp|fcmp|prfm|hint|nop|dmb)/ {split($4, a, ","); gsub(/ /, "", a[1]); print a[1]; if ($3 ~ /^ld(n|a?x)?p/) {gsub(/ /, "", a[2]); print a[2]}}' | sort -u; }
+dst "$W/hoist.bin" > "$O/hoist.dst"
+hx=$(grep -c -x -E 'x2[0-6]' "$O/hoist.dst" || true); hd=$(grep -c -x -E '[ds]([89]|1[0-5])' "$O/hoist.dst" || true)
+if [ "$hx" = 7 ] && [ "$hd" -ge 1 ]; then pass "hoist writes x20..x26 and $hd of d8..d15 (decoded destinations)"
+else fail "hoist writes $hx of x20..x26 and $hd of d8..d15: OMEGA_NATIVE_OMC2 does not hoist (omega-mv perf/omc-loop-checks)"; fi
+ok=1; for u in $V2; do
+  if objdump -D -b binary -m aarch64 "$W/$u.bin" | awk -F'\t' 'NF >= 4' | cut -f4 | grep -q -w -E '[xw](18|19|29)'; then ok=0; echo "INFO $u names x18, x19 or x29"; fi
+done
+if [ $ok = 1 ]; then pass "no OM-2 unit's code names x18, x19 or x29"; else fail "a unit's code names x18, x19 or x29"; fi
 # The exported symbols: one omega2_ per export, one ident per unit, the OM-1 unit's omega_, nothing else.
 nm -D --defined-only "$O/libomega_native.so" | awk '{print $3}' | grep -E '^omega' | sort > "$O/syms.txt"
-printf '%s\n' omega2_absorb omega2_bsum omega2_digest omega2_fillv omega2_halt omega2_ident_lay omega2_ident_one \
-  omega2_ident_vec omega2_lsum omega2_swap8 omega2_two omega2_vcopy omega2_vlen omega2_vpart omega2_wsum omega_madd | sort > "$O/syms.want"
-if cmp -s "$O/syms.txt" "$O/syms.want"; then pass "exported symbols: 15 omega2_ (12 exports, 3 idents) + omega_madd; no psum, no v1 name for a v2 unit"
+printf '%s\n' omega2_absorb omega2_bsum omega2_digest omega2_fillv omega2_hadd omega2_halt omega2_ident_hoist omega2_ident_lay \
+  omega2_ident_one omega2_ident_vec omega2_lsum omega2_swap8 omega2_two omega2_vcopy omega2_vlen omega2_vpart omega2_wsum omega_madd \
+  | sort > "$O/syms.want"
+if cmp -s "$O/syms.txt" "$O/syms.want"; then pass "exported symbols: 17 omega2_ (13 exports, 4 idents) + omega_madd; no psum, no v1 name for a v2 unit"
 else fail "exported symbols"; diff "$O/syms.want" "$O/syms.txt" || true; fi
-if nm --defined-only "$O/libomega_native.so" | awk '$3 ~ /^ombr2_code_/ {n++; if ($2 != "t") bad=1} END {exit bad || n != 3}'; then
+if nm --defined-only "$O/libomega_native.so" | awk '$3 ~ /^ombr2_code_/ {n++; if ($2 != "t") bad=1} END {exit bad || n != 4}'; then
   pass "ABI v2 embedded code labels are private local text symbols"
 else fail "ABI v2 private code linkage"; fi
 if nm --defined-only "$O/libomega_native.so" | awk '$3 ~ /^ombr/ && $2 !~ /^[a-z]$/ {bad=1} END {exit bad}'; then
@@ -269,6 +284,48 @@ mut wrap '/^text fv /s/ || n \* es > UINTPTR_MAX - lo)/)/' '#748 golden: vpart l
 mut overlap '/^text fv /s/if ((sp\[i\].w == 1 || sp\[j\].w == 1 ||/if ((/' 'vcopy edit == view'
 mut outcopy '/^text fv /s/memmove(o\[i\].dst, buf\[i\], o\[i\].n);/;/' 'digest: tag'
 
+# 9b. The trampoline alone: tramp.c includes the generated omega_native.c and calls ombr_enter directly
+# (no C wrapper that could keep a register for itself) with a hand-assembled unit that writes x20..x29 and
+# d8..d15, returning and trapping. Twice: the hosted trampoline (text pre, the library's) and the
+# freestanding one (text fp, tools/native --freestanding on the OM-1 unit smadd). Then, in each, one
+# mutant per restore pair in ombr_leave, each removed in a copy of the generator (x29: restored no more,
+# x30 still): every one must make tramp fail.
+tb() { cc -std=gnu11 -O2 -Wall -Wextra -Werror -include "$1/omega_native.h" -I "$1" "$T/tramp.c" -o "$1/tramp" > "$1/tramp-build.log" 2>&1; }
+FPO=$O/fp
+if "$ROOT/tools/native" --freestanding "$FPO" "$ROOT/tests/native/smadd.om" > "$O/fp-native.log" 2>&1; then :
+else fail "tools/native --freestanding smadd"; cat "$O/fp-native.log"; fi
+for k in pre fp; do
+  D=$O; [ $k = pre ] || D=$FPO
+  if tb "$D" && "$D/tramp" > "$D/tramp.log" 2>&1; then pass "trampoline (text $k) called directly: $(grep -c '^PASS' "$D/tramp.log") checks (return, trap)"
+  else fail "trampoline (text $k) called directly"; cat "$D/tramp-build.log" "$D/tramp.log"; fi
+done
+# tmut K NAME SED: the mutant of text K (pre: hosted library, units.in; fp: freestanding, smadd).
+tmut() {
+  k=$1; name=$2; M=$O/tmut-$k-$name; mkdir -p "$M"
+  if [ $k = pre ]; then D=$O; U=$W/units.in; c=c; else D=$FPO; U=$FPO/work/units.in; c=freest; fi
+  sed "/^text $k /$3" "$ROOT/src/native.om" > "$M/native.om"
+  n=$(diff "$ROOT/src/native.om" "$M/native.om" | grep -c '^<' || true)
+  [ "$n" = 1 ] || { fail "trampoline mutant $k $name changed $n lines"; return 0; }
+  "$OMC" < "$M/native.om" > "$M/gen"; chmod +x "$M/gen"
+  "$M/gen" $c < "$U" > "$M/omega_native.c"; "$M/gen" h < "$U" > "$M/omega_native.h"
+  cmp -s "$M/omega_native.c" "$D/omega_native.c" && { fail "trampoline mutant $k $name: output unchanged"; return 0; }
+  tb "$M" || { fail "trampoline mutant $k $name: build"; cat "$M/tramp-build.log"; return 0; }
+  if timeout 10 "$M/tramp" > "$M/tramp.log" 2>&1; then fail "trampoline mutant $k $name not caught"
+  else pass "trampoline mutant $k $name caught ($(grep -c '^FAIL' "$M/tramp.log") failing checks$(grep -q 'TRAMP FAIL' "$M/tramp.log" || echo ', crashed'))"; fi
+}
+for k in pre fp; do
+  tmut $k x19x20 's/ldp x19,x20,\[sp,#16\]\\\\n//'
+  tmut $k x21x22 's/ldp x21,x22,\[sp,#32\]\\\\n//'
+  tmut $k x23x24 's/ldp x23,x24,\[sp,#48\]\\\\n//'
+  tmut $k x25x26 's/ldp x25,x26,\[sp,#64\]\\\\n//'
+  tmut $k x27x28 's/ldp x27,x28,\[sp,#80\]\\\\n//'
+  tmut $k d8d9 's/ldp d8,d9,\[sp,#96\]\\\\n//'
+  tmut $k d10d11 's/ldp d10,d11,\[sp,#112\]\\\\n//'
+  tmut $k d12d13 's/ldp d12,d13,\[sp,#128\]\\\\n//'
+  tmut $k d14d15 's/ldp d14,d15,\[sp,#144\]\\\\n//'
+  tmut $k x29 's/ldp x29,x30,\[sp\],#176\\\\n/ldr x30,[sp,#8]\\\\nadd sp,sp,#176\\\\n/'
+done
+
 # 10a. The C API out of line (outputs apih, apic) for freestanding consumers. The inline header
 # (output api) and the out-of-line pair stay byte for byte what f637021's generator (with the current
 # prelude) wrote, as does every other output.
diff --git a/tests/native/v2/tramp.c b/tests/native/v2/tramp.c
new file mode 100644
index 0000000..ca5cbf3
--- /dev/null
+++ b/tests/native/v2/tramp.c
@@ -0,0 +1,64 @@
+/* tramp.c: the trampoline itself, with no C wrapper between the probe and ombr_enter (a wrapper that saves
+ * a callee-saved register for its own use would hide a missing restore). Built with the generated
+ * omega_native.c included, so the hidden ombr_enter and ombr_rt_table are in this file. The unit is
+ * hand-assembled: it writes every register a unit may write under docs/NATIVE-ABI.md that the caller
+ * expects kept (x20..x28, x29, d8..d15), then returns 42 or, for a nonzero first argument, takes the
+ * trap entry with code 9 after clobbering x30 as well. The probe sets x20..x28 and d8..d15 to known
+ * values and counts, after ombr_enter returns, every one that differs, plus x19 and x29 (its frame). */
+#include <stdio.h>
+#include <stdint.h>
+#include <stdlib.h>
+#include "omega_native.c"
+
+extern const unsigned char tr_clob[];
+__asm__(".text\n.balign 4\n.globl tr_clob\n.type tr_clob,%function\ntr_clob:\nhint #34\n"
+        "mov x20,#901\nmov x21,#902\nmov x22,#903\nmov x23,#904\nmov x24,#905\nmov x25,#906\nmov x26,#907\n"
+        "mov x27,#908\nmov x28,#909\nmov x29,#910\n"
+        "mov x9,#911\nfmov d8,x9\nmov x9,#912\nfmov d9,x9\nmov x9,#913\nfmov d10,x9\nmov x9,#914\nfmov d11,x9\n"
+        "mov x9,#915\nfmov d12,x9\nmov x9,#916\nfmov d13,x9\nmov x9,#917\nfmov d14,x9\nmov x9,#918\nfmov d15,x9\n"
+        "cbnz x0,1f\nmov x0,#42\nret\n"
+        "1:\nldr x16,[x7,#16]\nmov x1,#9\nmov x30,#0\nbr x16\n.size tr_clob,.-tr_clob\n");
+
+/* tr_probe(code, args, rt, out, top): ombr_enter's own arguments; returns its status in the low 32 bits
+ * and the number of changed registers in the high 32 bits. */
+uint64_t tr_probe(const void *code, const uint64_t *args, const ombr_rt *rt, uint64_t *out, void *top);
+__asm__(".text\n.balign 4\n.globl tr_probe\n.type tr_probe,%function\ntr_probe:\nhint #34\n"
+        "stp x29,x30,[sp,#-160]!\nmov x29,sp\nstp x19,x20,[sp,#16]\nstp x21,x22,[sp,#32]\nstp x23,x24,[sp,#48]\n"
+        "stp x25,x26,[sp,#64]\nstp x27,x28,[sp,#80]\nstp d8,d9,[sp,#96]\nstp d10,d11,[sp,#112]\n"
+        "stp d12,d13,[sp,#128]\nstp d14,d15,[sp,#144]\n"
+        "mov x19,#119\nmov x20,#120\nmov x21,#121\nmov x22,#122\nmov x23,#123\nmov x24,#124\nmov x25,#125\nmov x26,#126\nmov x27,#127\nmov x28,#128\n"
+        "mov x9,#108\nfmov d8,x9\nmov x9,#109\nfmov d9,x9\nmov x9,#110\nfmov d10,x9\nmov x9,#111\nfmov d11,x9\nmov x9,#112\nfmov d12,x9\nmov x9,#113\nfmov d13,x9\nmov x9,#114\nfmov d14,x9\nmov x9,#115\nfmov d15,x9\n"
+        "bl ombr_enter\nmov w0,w0\nmov x9,#0\n"
+        "mov x10,sp\ncmp x29,x10\ncinc x9,x9,ne\n"
+        "cmp x19,#119\ncinc x9,x9,ne\ncmp x20,#120\ncinc x9,x9,ne\ncmp x21,#121\ncinc x9,x9,ne\ncmp x22,#122\ncinc x9,x9,ne\ncmp x23,#123\ncinc x9,x9,ne\ncmp x24,#124\ncinc x9,x9,ne\ncmp x25,#125\ncinc x9,x9,ne\ncmp x26,#126\ncinc x9,x9,ne\ncmp x27,#127\ncinc x9,x9,ne\ncmp x28,#128\ncinc x9,x9,ne\n"
+        "fmov x10,d8\ncmp x10,#108\ncinc x9,x9,ne\nfmov x10,d9\ncmp x10,#109\ncinc x9,x9,ne\nfmov x10,d10\ncmp x10,#110\ncinc x9,x9,ne\nfmov x10,d11\ncmp x10,#111\ncinc x9,x9,ne\nfmov x10,d12\ncmp x10,#112\ncinc x9,x9,ne\nfmov x10,d13\ncmp x10,#113\ncinc x9,x9,ne\nfmov x10,d14\ncmp x10,#114\ncinc x9,x9,ne\nfmov x10,d15\ncmp x10,#115\ncinc x9,x9,ne\n"
+        "orr x0,x0,x9,lsl #32\nldp d14,d15,[sp,#144]\nldp d12,d13,[sp,#128]\nldp d10,d11,[sp,#112]\nldp d8,d9,[sp,#96]\n"
+        "ldp x27,x28,[sp,#80]\nldp x25,x26,[sp,#64]\nldp x23,x24,[sp,#48]\nldp x21,x22,[sp,#32]\nldp x19,x20,[sp,#16]\n"
+        "ldp x29,x30,[sp],#160\nret\n.size tr_probe,.-tr_probe\n");
+
+static int fails;
+static void check(const char *name, int ok)
+{
+    printf("%s %s\n", ok ? "PASS" : "FAIL", name);
+    if (!ok)
+        fails++;
+}
+
+int main(void)
+{
+    setvbuf(stdout, 0, _IOLBF, 0);
+    unsigned char *stk = aligned_alloc(16, 1u << 16);
+    if (!stk)
+        return 2;
+    void *top = stk + (1u << 16);
+    uint64_t a[6] = {0}, v = 7;
+    uint64_t pv = tr_probe(tr_clob, a, &ombr_rt_table, &v, top);
+    check("tramp: return, x19..x29 and d8..d15 kept", (uint32_t)pv == OMEGA_OK && (pv >> 32) == 0 && v == 42);
+    a[0] = 1;
+    v = 7;
+    pv = tr_probe(tr_clob, a, &ombr_rt_table, &v, top);
+    check("tramp: trap 9, x19..x29 and d8..d15 kept", (uint32_t)pv == 9 && (pv >> 32) == 0 && v == 7);
+    free(stk);
+    printf("%s %d\n", fails ? "TRAMP FAIL" : "TRAMP PASS", fails);
+    return fails != 0;
+}