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 / src / native.om

3695 lines · 112518 bytes Raw Blame History
1note native.om: omega-bridge native call ABI v1 binding generator (docs/NATIVE-ABI.md).
2note Input on stdin, one or more units: the unit's .om source, a 0 byte, its code length as 8 decimal
3note digits, then the function-mode machine code the seeded compiler wrote for that source.
4note One argument names the output: c is the library source, one C file that embeds each code
5note unchanged and exports uint32_t omega_<name>(uint64_t *result, ...) per unit; h, python, rust,
6note zig and go are that library's declarations for those languages, written from the same
7note signatures (tools/native runs all six). Output on stdout, only after every unit is accepted.
8note The bound function is the unit's last one, which is where the compiler enters function-mode
9note code (word 0 branches to it); earlier functions are private helpers with any types. The bound
10note function's default parameters are ulong or long scalars, or edit/view arrays of ulong, uchar, f32 or
11note ushort; its result is ulong, long or void. An f32 array is 4-byte aligned binary32 and a ushort
12note array is 2-byte aligned. Rust and Zig declarations type those two as byte pointers; the C library
13note enforces element size and alignment. Python ctypes uses c_float and c_uint16. An array length is
14note a decimal number or the name of an earlier "const NAME ulong DIGITS" line of the unit.
15note The explicit float/fhost hosted profile also admits f32/f64 scalar parameters and results by raw bit-copy.
16note trap 10 (exit 110) and no output.
17global inb uchar 16777216
18global outb uchar 16777216
19global outc uchar 16777216
20global st ulong 16
21global md uchar 8
22global tks ulong 40
23global tke ulong 40
24global pk ulong 6
25global pl ulong 6
26global ps ulong 6
27global pe ulong 6
28global dg uchar 24
29global us ulong 1024
30global ue ulong 1024
31text hx "0123456789abcdef"
32global xt ulong 1
33text xtname "x86linux"
34note Host words (docs/NATIVE-ABI.md, host words): hok is 1 when the generator's second argument is host,
35note hm[k] marks a host word used by the current unit. hw follows the omc keyword table; the nine
36note file/identity words, three clocks and ten device words pass only for OM-2 with host.
37global hok ulong 1
38global fok ulong 1
39global hm uchar 84
40global mdev ulong 1
41global munit ulong 1
42text mappin "6d1b1cf5a44803389a831b67f750ea4cd1c9f53edfde9d6d9241d7352887f1f8"
43text xmappin "c5ac9979232afd0d750e1d5088af69692032997136f761e585a25aa9a1cfc87e"
44text ctxparts "ctx_~(omega_native_context *context, uint64_t *result~ if (ombr2_overlap(sp, ns))\n return OMEGA_BAD_ARG;\n return ombr2_run(context, ombr2_entry_~#ifndef OMEGA_NATIVE_CONTEXT_DECL\n#define OMEGA_NATIVE_CONTEXT_DECL\n#define OMEGA_CONTEXT_BUSY 36u\ntypedef struct omega_native_context omega_native_context;\nuint32_t omega_native_context_create(uint64_t capacity, omega_native_context **out);\nuint32_t omega_native_context_destroy(omega_native_context *context);\n#endif\n"
45text hname "host"
46text xhname "x86host"
47text hpre " host="
48text hw "|fclose|fopen|fstat|stat|getuid|geteuid|getgid|getegid|lstat|fsync|fchmod|fseek|socknew|listen|accept|fork|dupfd|monons|unlink|makedir|getdnts|connect|bind|waitpid|recv|send|sleepns|argcnt|argread|waitio|execprog|renames|rmdir|killpid|pipefds|getpid|realns|sockname|cwd|readlnk|sockopt|random|envcnt|envread|chdir|ftrunc|link|symlnk|chmod|faccess|truncate|chown|fchdir|getppid|umask|fdsync|memfd|setsid|getpgid|setpgid|pread|pwrite|getsid|syncfs|shutdown|devctl|mapnew|mapdrop|mapread|mapwrite|devcall|uvmctl|mapaddr|mapget|mapset|maphold|mapbind|flock|execpure|peercred|pollfds|sendmsg|peername|"
49text xjmp ") \":\\nendbr64\\njmp \" OMBR_SYM(ombr_code_~) \":\\nendbr64\\njmp \" OMBR_SYM(ombr2_code_"
50text 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. Hosted device helpers and legacy read/write preserve these reserved registers;\n * the selected hosted compiler also restores its R13 argument-stack scratch.\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"
51text xs "/* ombr_enter"
52text xe "static void ombr_free"
53text fxs "/* ombr_enter saves"
54text fxe "static const ombr_rt ombr_rt_table"
55text xqd "\\n.quad "
56text xby "\\n.byte "
57text 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"
58text kw "toeditviewulonglongucharvoidmainf32ushortconstf64"
59text fr "__asm__(OMBR_CODE_SECTION \".balign 4\\n\" OMBR_LOCAL(ombr_code_~) OMBR_HIDDEN(ombr_code_~) OMBR_SYM(ombr_code_~) \":\\n.byte ~) \"\\n\" OMBR_PREVIOUS);\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] = {{0}};\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\" OMBR_LOCAL(ombr_entry_~) OMBR_HIDDEN(ombr_entry_~) OMBR_SYM(ombr_entry_~) \":\\nhint #34\\nb \" OMBR_SYM(ombr_code_~#if defined(__APPLE__)\n#error \"omega-bridge native binding: unit ~ makes Linux system calls (Omega host words), which macOS does not run\"\n#endif\n"
60text 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/* The object format's spelling of the embedded code and runtime symbols. ELF keeps them local and hidden\n * in .text.omega_bridge. Mach-O (macOS) prefixes C symbol names with an underscore and keeps them local\n * by not declaring them .globl; .alt_entry keeps the runtime entries in ombr_enter's atom. */\n#if defined(__APPLE__) && defined(__MACH__)\n#define OMBR_SYM(n) \"_\" #n\n#define OMBR_LOCAL(n) \"\"\n#define OMBR_HIDDEN(n) \"\"\n#define OMBR_FUNC(n) \"\"\n#define OMBR_ALT(n) \".alt_entry _\" #n \"\\n\"\n#define OMBR_SIZE(n) \"\"\n#define OMBR_LEAVE \"Lombr_leave\"\n#define OMBR_CODE_SECTION \".section __TEXT,__text,regular,pure_instructions\\n\"\n#define OMBR_PREVIOUS \"\"\n#elif defined(__ELF__)\n#define OMBR_SYM(n) #n\n#define OMBR_LOCAL(n) \".local \" #n \"\\n\"\n#define OMBR_HIDDEN(n) \".hidden \" #n \"\\n\"\n#define OMBR_FUNC(n) \".type \" #n \",%function\\n\"\n#define OMBR_ALT(n) OMBR_FUNC(n)\n#define OMBR_SIZE(n) \".size \" #n \",.-\" #n \"\\n\"\n#define OMBR_LEAVE \"ombr_leave\"\n#define OMBR_CODE_SECTION \".section .text.omega_bridge,\\\"ax\\\",@progbits\\n\"\n#define OMBR_PREVIOUS \".previous\\n\"\n#else\n#error \"omega-bridge native binding: ELF or Mach-O objects only\"\n#endif\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#if !defined(__aarch64__)\n#error \"omega-bridge native binding: the embedded code is AArch64\"\n#endif\n__asm__(\".text\\n.balign 4\\n\" OMBR_LOCAL(ombr_enter) OMBR_HIDDEN(ombr_enter) OMBR_FUNC(ombr_enter)\n OMBR_SYM(ombr_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\" OMBR_SIZE(ombr_enter)\n OMBR_LOCAL(ombr_trap) OMBR_HIDDEN(ombr_trap) OMBR_ALT(ombr_trap) OMBR_SYM(ombr_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 OMBR_SIZE(ombr_trap)\n OMBR_LOCAL(ombr_no_entry) OMBR_HIDDEN(ombr_no_entry) OMBR_ALT(ombr_no_entry) OMBR_SYM(ombr_no_entry) \":\\nhint #38\\n\"\n \"mov sp,x19\\nmov w0,#34\\nb \" OMBR_LEAVE \"\\n\" OMBR_SIZE(ombr_no_entry));\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"
61text predev "#define OMBR_USES_MAPS 1\n/* 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 <stdlib.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;\n#define OMEGA_CONTEXT_BUSY 36u\ntypedef struct omega_native_context {\n pthread_mutex_t lock;\n uint64_t *maps;\n const ombr_rt *rt;\n} omega_native_context;\nstatic _Alignas(16) uint64_t ombr_maps[516];\nstatic omega_native_context ombr_default = { .lock = PTHREAD_MUTEX_INITIALIZER, .maps = ombr_maps };\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/* The object format's spelling of the embedded code and runtime symbols. ELF keeps them local and hidden\n * in .text.omega_bridge. Mach-O (macOS) prefixes C symbol names with an underscore and keeps them local\n * by not declaring them .globl; .alt_entry keeps the runtime entries in ombr_enter's atom. */\n#if defined(__APPLE__) && defined(__MACH__)\n#define OMBR_SYM(n) \"_\" #n\n#define OMBR_LOCAL(n) \"\"\n#define OMBR_HIDDEN(n) \"\"\n#define OMBR_FUNC(n) \"\"\n#define OMBR_ALT(n) \".alt_entry _\" #n \"\\n\"\n#define OMBR_SIZE(n) \"\"\n#define OMBR_LEAVE \"Lombr_leave\"\n#define OMBR_CODE_SECTION \".section __TEXT,__text,regular,pure_instructions\\n\"\n#define OMBR_PREVIOUS \"\"\n#elif defined(__ELF__)\n#define OMBR_SYM(n) #n\n#define OMBR_LOCAL(n) \".local \" #n \"\\n\"\n#define OMBR_HIDDEN(n) \".hidden \" #n \"\\n\"\n#define OMBR_FUNC(n) \".type \" #n \",%function\\n\"\n#define OMBR_ALT(n) OMBR_FUNC(n)\n#define OMBR_SIZE(n) \".size \" #n \",.-\" #n \"\\n\"\n#define OMBR_LEAVE \"ombr_leave\"\n#define OMBR_CODE_SECTION \".section .text.omega_bridge,\\\"ax\\\",@progbits\\n\"\n#define OMBR_PREVIOUS \".previous\\n\"\n#else\n#error \"omega-bridge native binding: ELF or Mach-O objects only\"\n#endif\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#if !defined(__aarch64__)\n#error \"omega-bridge native binding: the embedded code is AArch64\"\n#endif\n__asm__(\".text\\n.balign 4\\n\" OMBR_LOCAL( ombr_enter ) OMBR_HIDDEN(ombr_enter) OMBR_FUNC(ombr_enter)\n OMBR_SYM(ombr_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\" OMBR_SIZE(ombr_enter)\n OMBR_LOCAL(ombr_trap) OMBR_HIDDEN(ombr_trap) OMBR_ALT(ombr_trap) OMBR_SYM(ombr_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 OMBR_SIZE(ombr_trap)\n OMBR_LOCAL(ombr_no_entry) OMBR_HIDDEN(ombr_no_entry) OMBR_ALT(ombr_no_entry) OMBR_SYM(ombr_no_entry) \":\\nhint #38\\n\"\n \"mov sp,x19\\nmov w0,#34\\nb \" OMBR_LEAVE \"\\n\" OMBR_SIZE(ombr_no_entry));\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_maps }};\n/* Explicit C ABI contexts: compatibility-runtime ownership, not driver policy. */\nstatic pthread_once_t ombr_map_once = PTHREAD_ONCE_INIT;\nstatic pthread_mutex_t ombr_id_lock = PTHREAD_MUTEX_INITIALIZER;\nstatic uint64_t ombr_next_context = 2;\nstatic void ombr_map_init(void)\n{\n ombr_maps[0] = 128;\n ombr_maps[1] = 67108864;\n ombr_maps[2] = 134217727;\n for (unsigned i = 0; i < 128; i++)\n ombr_maps[4 + i * 4 + 2] = ombr_maps[1];\n ombr_default.rt = &ombr_rt_table;\n}\n__attribute__((visibility(\"default\"))) uint32_t omega_native_context_create(uint64_t capacity,\n omega_native_context **out)\n{\n if (!out || capacity == 0 || capacity > 4098)\n return OMEGA_BAD_ARG;\n if (ombr_active)\n return OMEGA_REENTRY;\n if (pthread_mutex_lock(&ombr_id_lock) != 0)\n return OMEGA_BAD_RT;\n uint64_t id = ombr_next_context;\n if (id <= 16777215)\n ombr_next_context++;\n int unlock_error = pthread_mutex_unlock(&ombr_id_lock);\n if (unlock_error || id > 16777215)\n return OMEGA_BAD_RT;\n omega_native_context *c = calloc(1, sizeof *c);\n if (!c)\n return OMEGA_BAD_RT;\n c->maps = calloc((size_t)capacity + 1u, 32u);\n if (!c->maps) {\n free(c);\n return OMEGA_BAD_RT;\n }\n if (pthread_mutex_init(&c->lock, 0) != 0) {\n free(c->maps);\n free(c);\n return OMEGA_BAD_RT;\n }\n ombr_rt *rt = mmap(0, sizeof *rt, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);\n if (rt == MAP_FAILED) {\n pthread_mutex_destroy(&c->lock);\n free(c->maps);\n free(c);\n return OMEGA_BAD_RT;\n }\n *rt = ombr_rt_table;\n rt->entry[31] = c->maps;\n if (mprotect(rt, sizeof *rt, PROT_READ) != 0) {\n munmap(rt, sizeof *rt);\n pthread_mutex_destroy(&c->lock);\n free(c->maps);\n free(c);\n return OMEGA_BAD_RT;\n }\n c->rt = rt;\n c->maps[0] = capacity;\n c->maps[1] = id << 26;\n c->maps[2] = ((id + 1u) << 26) - 1u;\n for (uint64_t i = 0; i < capacity; i++)\n c->maps[4 + i * 4 + 2] = c->maps[1];\n *out = c;\n return OMEGA_OK;\n}\n__attribute__((visibility(\"default\"))) uint32_t omega_native_context_destroy(omega_native_context *c)\n{\n if (!c || c == &ombr_default)\n return OMEGA_BAD_ARG;\n if (ombr_active)\n return OMEGA_REENTRY;\n /* Caller has joined/drained users and waiters before entering destruction. */\n if (pthread_mutex_lock(&c->lock) != 0)\n return OMEGA_BAD_RT;\n for (uint64_t i = 0; i < c->maps[0]; i++) {\n if (c->maps[4 + i * 4] != 0) {\n if (pthread_mutex_unlock(&c->lock) != 0)\n return OMEGA_BAD_RT;\n return OMEGA_CONTEXT_BUSY;\n }\n }\n if (pthread_mutex_unlock(&c->lock) != 0 || pthread_mutex_destroy(&c->lock) != 0)\n return OMEGA_BAD_RT;\n int failed = munmap((void *)c->rt, sizeof *c->rt) != 0;\n free(c->maps);\n free(c);\n return failed ? OMEGA_BAD_RT : OMEGA_OK;\n}\nstatic uint32_t ombr_context_lock(omega_native_context *c)\n{\n if (!c)\n return OMEGA_BAD_ARG;\n if (ombr_active)\n return OMEGA_REENTRY;\n ombr_active = 1;\n if (c == &ombr_default && pthread_once(&ombr_map_once, ombr_map_init) != 0) {\n ombr_active = 0;\n return OMEGA_BAD_RT;\n }\n if (pthread_mutex_lock(&c->lock) != 0) {\n ombr_active = 0;\n return OMEGA_BAD_RT;\n }\n return OMEGA_OK;\n}\nstatic uint32_t ombr_context_unlock(omega_native_context *c, uint32_t status)\n{\n int failed = pthread_mutex_unlock(&c->lock);\n ombr_active = 0;\n return failed ? OMEGA_BAD_RT : status;\n}\nstatic uint32_t ombr_run(const void *code, const uint64_t *args, uint64_t *result, int has_value)\n{\n omega_native_context *context = &ombr_default;\n uint32_t s = ombr_context_lock(context);\n if (s != OMEGA_OK)\n return s;\n unsigned char *top = ombr_stack();\n if (!top)\n return ombr_context_unlock(context, OMEGA_NO_STACK);\n uint64_t v = 0;\n s = ombr_enter(code, args, context->rt, &v, top);\n if (s == OMEGA_OK)\n *result = has_value ? v : 0u;\n return ombr_context_unlock(context, s);\n}\n"
62note mo: the output names; tb: the binding texts, fragments separated by ~ (byte 126): 0-11 h,
63note 12-21 python, 22-34 rust, 35-46 zig, 47-64 go (its C preamble includes the generated header).
64text mo "chpythonrustziggoapihapicfreestidentfapic"
65text 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 <stdint.h>\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, sys\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__)),\n \"libomega_native.dylib\" if sys.platform == \"darwin\" else \"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 <its directory>; 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<its directory> -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,<folder>), 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"
66note Freestanding output accepts pure units only: no storage (global, text), host words or input.
67note An OM-1 unit has integer scalars only (no arrays or floats). Words after # in fw are OM-2 only:
68note views, layouts, out parameters, local arrays, part, unroll, inline, rotations and private f32/f64.
69note Public parameters and results stay integer-only; private floats require enabled hardware FP and inherit its control state.
70global fns ulong 1024
71global fne ulong 1024
72global fnc ulong 1
73global fo2 ulong 1
74text 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|*|f64|f32|"
75text fp "/* Generated pure integer-scalar freestanding native ABI v1 binding. */\n#include <stdint.h>\n#if !defined(__ELF__)\n#error \"omega-bridge freestanding binding: ELF objects only\"\n#endif\n#define OMBR_SYM(n) #n\n#define OMBR_LOCAL(n) \".local \" #n \"\\n\"\n#define OMBR_HIDDEN(n) \".hidden \" #n \"\\n\"\n#define OMBR_CODE_SECTION \".section .text.omega_bridge,\\\"ax\\\",@progbits\\n\"\n#define OMBR_PREVIOUS \".previous\\n\"\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"
76text 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_"
77fn fword k ulong to ulong
78i = ulong 0
79while lt add i 1 len fw
80if eq get fw i 124
81if and eq get fw add i 1 35 eq get fo2 0 0
82ret 0
83end
84j = add i 1
85while and lt j len fw ne get fw j 124
86j = add j 1
87end
88if eq fis k add i 1 sub j add i 1 1
89ret 1
90end
91i = j
92else
93i = add i 1
94end
95end
96ret 0
97end
98fn fmatch k ulong s ulong e ulong to ulong
99ret samenm get tks k get tke k s e
100end
101fn fadd k ulong to void
102n = get fnc 0
103for j ulong n
104if eq fmatch k get fns j get fne j 1
105ret
106end
107end
108if ge n len fns
109trap 10
110end
111put fns n get tks k
112put fne n get tke k
113put fnc 0 add n 1
114end
115fn fis k ulong off ulong n ulong to ulong
116s = get tks k
117e = get tke k
118if ne sub e s n
119ret 0
120end
121for j ulong n
122if ne get inb add s j get fw add off j
123ret 0
124end
125end
126ret 1
127end
128fn fnum k ulong to ulong
129s = get tks k
130e = get tke k
131if ge s e
132ret 0
133end
134if eq get inb s 45
135s = add s 1
136else
137if and eq get fo2 0 1 gt sub e s 2
138if and eq get inb s 48 eq get inb add s 1 120
139for j ulong sub e add s 2
140b = get inb add add s 2 j
141if and or lt b 48 gt b 57 or lt b 97 gt b 102
142ret 0
143end
144end
145ret 1
146end
147end
148end
149a = s
150while lt s e
151b = get inb s
152if or lt b 48 gt b 57
153break
154end
155s = add s 1
156end
157if eq s a
158ret 0
159end
160if eq get fo2 0 0
161if eq s e
162ret 1
163end
164ret 0
165end
166if lt s e
167if eq get inb s 46
168s = add s 1
169a = s
170while lt s e
171b = get inb s
172if or lt b 48 gt b 57
173break
174end
175s = add s 1
176end
177if eq s a
178ret 0
179end
180end
181end
182if lt s e
183if or eq get inb s 101 eq get inb s 69
184s = add s 1
185if lt s e
186if or eq get inb s 43 eq get inb s 45
187s = add s 1
188end
189end
190a = s
191while lt s e
192b = get inb s
193if or lt b 48 gt b 57
194break
195end
196s = add s 1
197end
198if eq s a
199ret 0
200end
201end
202end
203if lt s e
204if ne sub e s 3
205ret 0
206end
207if ne get inb s 102
208ret 0
209end
210b = get inb add s 1
211if eq b 54
212if ne get inb add s 2 52
213ret 0
214end
215else
216if ne b 51
217ret 0
218end
219if ne get inb add s 2 50
220ret 0
221end
222end
223s = add s 3
224end
225if eq s e
226ret 1
227end
228ret 0
229end
230fn fknown k ulong to ulong
231if eq fword k 1
232ret 1
233end
234if eq fnum k 1
235ret 1
236end
237for j ulong get fnc 0
238if eq fmatch k get fns j get fne j 1
239ret 1
240end
241end
242ret 0
243end
244note fextent recognizes a literal, *, or an earlier positive ulong constant without registering
245note an arbitrary identifier as a parameter name. cline is shared with public extent resolution.
246fn fextent k ulong p ulong q ulong to ulong
247if or eq fnum k 1 eq tvis k 83 1 1
248ret 1
249end
250i = p
251while lt i q
252if gt cline i q get tks k get tke k 0
253ret 1
254end
255while and lt i q ne get inb i 10
256i = add i 1
257end
258i = add i 1
259end
260ret 0
261end
262note fdecl2 registers only function/parameter names, not signature types. Treating every unknown
263note signature token as a name would admit an unsupported private type such as f128.
264note A layout/field line declares its second token. fw offsets: export 254, layout 261, field 268.
265fn fdecl2 nt ulong p ulong q ulong to void
266b = ulong 0
267if eq fis 0 254 6 1
268b = 1
269end
270if and gt nt b eq fis b 6 2 1
271if ge add b 1 nt
272trap 10
273end
274fadd add b 1
275k = add b 2
276while lt k nt
277if eq fis k 9 2 1
278break
279end
280if ge add k 1 nt
281trap 10
282end
283fadd k
284ty = add k 1
285if or or eq tkis ty 2 4 1 eq tkis ty 6 4 1 eq tvis ty 8 3 1
286if ge add k 2 nt
287trap 10
288end
289k = add k 3
290if lt k nt
291if eq fextent k p q 1
292k = add k 1
293end
294end
295if lt k nt
296if eq fis k 302 5 1
297if or or ne tkis ty 2 4 1 eq fextent sub k 1 p q 0 eq tvis sub k 1 83 1 1
298trap 10
299end
300k = add k 1
301end
302end
303else
304k = add k 2
305end
306end
307end
308if and ge nt 2 or eq fis 0 261 6 1 eq fis 0 268 5 1
309fadd 1
310end
311end
312fn fcheck p ulong q ulong to void
313put fnc 0 0
314for pass ulong 2
315i = p
316while lt i q
317nt = tokln i q
318next = add get st 7 1
319if gt nt 0
320if eq fis 0 1 4 0
321if eq pass 0
322if eq get fo2 0 1
323fdecl2 nt p i
324else
325if eq fis 0 6 2 1
326if lt nt 4
327trap 10
328end
329fadd 1
330k = ulong 2
331while lt add k 1 nt
332if eq fis k 9 2 1
333break
334end
335fadd k
336k = add k 2
337end
338end
339end
340if ge nt 2
341if or eq fis 0 28 3 1 eq fis 0 98 5 1
342fadd 1
343end
344if eq fis 1 241 1 1
345fadd 0
346end
347end
348else
349for k ulong nt
350if eq fknown k 0
351trap 10
352end
353end
354end
355end
356end
357i = next
358end
359end
360end
361note fpre: the fixed freestanding runtime; for x86linux only its assembly block becomes xfasm.
362fn fpre to void
363i = ulong 0
364while lt i len fp
365if and eq get xt 0 1 fxbegin i
366for j ulong len xfasm
367ob get xfasm j
368end
369while not fxend i
370i = add i 1
371if ge i len fp
372trap 10
373end
374end
375end
376ob get fp i
377i = add i 1
378end
379end
380fn fbody to void
381for i ulong len fb
382if eq get fb i 126
383break
384end
385ob get fb i
386end
387for j ulong get st 2
388if gt get pk j 2
389trap 10
390end
391ofr 19
392odec j
393ofr 20
394opn j
395ofr 18
396end
397skip = ulong 1
398for i ulong len fb
399if eq skip 0
400ob get fb i
401end
402if eq get fb i 126
403skip = 0
404end
405end
406oname 1
407ofr 22
408odec get st 3
409ofr 23
410end
411note ob: append one byte to the output pools, outb then outc (trap 6 when both are full).
412fn ob b uchar to void
413n = get st 1
414if lt n len outb
415put outb n b
416else
417k = sub n len outb
418if ge k len outc
419trap 6
420end
421put outc k b
422end
423put st 1 add n 1
424end
425note orng: copy input bytes s .. e-1 to the output.
426fn orng s ulong e ulong to void
427i = s
428while lt i e
429ob get inb i
430i = add i 1
431end
432end
433note odec: v in decimal.
434fn odec v ulong to void
435x = v
436n = ulong 0
437put dg n uchar add rem x 10 48
438n = add n 1
439x = div x 10
440while ne x 0
441put dg n uchar add rem x 10 48
442n = add n 1
443x = div x 10
444end
445while gt n 0
446n = sub n 1
447ob get dg n
448end
449end
450note ohex: one byte as 0xNN.
451fn ohex b uchar to void
452ob uchar 48
453ob uchar 120
454ob get hx shr b 4
455ob get hx and b 15
456end
457fn plen to ulong
458if eq get mdev 0 1
459ret len predev
460end
461ret len pre
462end
463fn pbyte i ulong to uchar
464if eq get mdev 0 1
465ret get predev i
466end
467ret get pre i
468end
469note Match only the assembly boundary markers, leaving the shared C runtime byte-identical.
470fn xbegin i ulong to bool
471if gt add i len xs plen
472ret false
473end
474for j ulong len xs
475if ne pbyte add i j get xs j
476ret false
477end
478end
479ret true
480end
481fn xend i ulong to bool
482if gt add i len xe plen
483ret false
484end
485for j ulong len xe
486if ne pbyte add i j get xe j
487ret false
488end
489end
490ret true
491end
492note The same marker match for the freestanding runtime fp.
493fn fxbegin i ulong to bool
494if gt add i len fxs len fp
495ret false
496end
497for j ulong len fxs
498if ne get fp add i j get fxs j
499ret false
500end
501end
502ret true
503end
504fn fxend i ulong to bool
505if gt add i len fxe len fp
506ret false
507end
508for j ulong len fxe
509if ne get fp add i j get fxe j
510ret false
511end
512end
513ret true
514end
515note opre: the fixed runtime part of the C file.
516fn opre to void
517i = ulong 0
518while lt i plen
519if and eq get xt 0 1 xbegin i
520for j ulong len xasm
521ob get xasm j
522end
523while not xend i
524i = add i 1
525if ge i plen
526trap 10
527end
528end
529end
530ob pbyte i
531i = add i 1
532end
533end
534note xbranch: architecture-specific private entry veneer; code bytes stay unchanged.
535fn xbranch k ulong to void
536c = ulong 0
537for i ulong len xjmp
538b = get xjmp i
539if eq b 126
540c = add c 1
541else
542if eq c k
543ob b
544end
545end
546end
547end
548note ofr: fragment k of fr (fragments are separated by ~, byte 126).
549fn ofr k ulong to void
550if and eq get xt 0 1 eq k 27
551xbranch 0
552ret
553end
554c = ulong 0
555for i ulong len fr
556b = get fr i
557if eq b 126
558c = add c 1
559else
560if eq c k
561ob b
562end
563end
564end
565end
566note ofb: fragment k of tb.
567fn ofb k ulong to void
568c = ulong 0
569for i ulong len tb
570b = get tb i
571if eq b 126
572c = add c 1
573else
574if eq c k
575ob b
576end
577end
578end
579end
580note mdis: 1 when the argument in md (its length in st 8) is mo[o .. o+n-1], else 0.
581fn mdis o ulong n ulong to ulong
582if ne get st 8 n
583ret 0
584end
585for j ulong n
586if ne get md j get mo add o j
587ret 0
588end
589end
590ret 1
591end
592note hostarg: 1 when the second argument (z bytes, read into md) is host.
593fn hostarg z long to ulong
594if ne z 4
595ret 0
596end
597for j ulong 4
598if ne get md j get hname j
599ret 0
600end
601end
602ret 1
603end
604note rmode: the first argument as an output number: 1 c, 2 h, 3 python, 4 rust, 5 zig, 6 go, 7 api,
605note 8 apih, 9 apic, 10 freest, 11 ident (each OM-2 unit's identity, one line each). An optional second
606note argument is x86linux, host, x86host, float or fhost (explicit hosted float profile, with host words).
607fn rmode to ulong
608if or lt argcnt 2 gt argcnt 3
609trap 10
610end
611if eq argcnt 3
612z = argread 2 md 0 8
613flag = fltarg z
614if ne flag 0
615put fok 0 1
616put hok 0 sub flag 1
617else
618if eq hostarg z 1
619put hok 0 1
620else
621if eq z 7
622for j ulong 7
623if ne get md j get xhname j
624trap 10
625end
626end
627put xt 0 1
628put hok 0 1
629else
630if ne z 8
631trap 10
632end
633for j ulong 8
634if ne get md j get xtname j
635trap 10
636end
637end
638put xt 0 1
639end
640end
641end
642end
643k = argread 1 md 0 8
644if lt k 0
645trap 10
646end
647put st 8 ulong k
648m = ulong 0
649if eq mdis 0 1 1
650m = 1
651end
652if eq mdis 1 1 1
653m = 2
654end
655if eq mdis 2 6 1
656m = 3
657end
658if eq mdis 8 4 1
659m = 4
660end
661if eq mdis 12 3 1
662m = 5
663end
664if eq mdis 15 2 1
665m = 6
666end
667if eq mdis 17 3 1
668m = 7
669end
670if eq mdis 17 4 1
671m = 8
672end
673if eq mdis 21 4 1
674m = 9
675end
676if eq mdis 25 6 1
677m = 10
678end
679if eq mdis 31 5 1
680m = 11
681end
682if eq mdis 36 5 1
683m = 12
684end
685if and eq get hok 0 1 or eq m 10 eq m 12
686trap 10
687end
688if eq m 0
689trap 10
690end
691ret m
692end
693note tkis: 1 when token k is the keyword kw[o .. o+n-1], else 0.
694fn tkis k ulong o ulong n ulong to ulong
695s = get tks k
696e = get tke k
697if ne sub e s n
698ret 0
699end
700for j ulong n
701if ne get inb add s j get kw add o j
702ret 0
703end
704end
705ret 1
706end
707note tknum: token k as a decimal number of 1 .. 8 digits.
708fn tknum k ulong to ulong
709s = get tks k
710e = get tke k
711if or eq s e gt sub e s 8
712trap 10
713end
714v = ulong 0
715for j ulong sub e s
716c = get inb add s j
717if or lt c 48 gt c 57
718trap 10
719end
720v = add mul v 10 ulong sub c 48
721end
722ret v
723end
724note tokln: split the line that starts at p (ending at a newline or at q) into tokens at spaces.
725fn tokln p ulong q ulong to ulong
726e = p
727while and lt e q ne get inb e 10
728e = add e 1
729end
730put st 7 e
731n = ulong 0
732s = p
733i = p
734while lt i e
735if eq get inb i 32
736if gt i s
737put tks n s
738put tke n i
739n = add n 1
740end
741s = add i 1
742end
743i = add i 1
744end
745if gt e s
746put tks n s
747put tke n e
748n = add n 1
749end
750ret n
751end
752note oname: the name of token k.
753fn oname k ulong to void
754orng get tks k get tke k
755end
756note opn: the name of parameter j.
757fn opn j ulong to void
758orng get ps j get pe j
759end
760note osig: the unit's signature line (st 6 .. st 7), the comment each binding carries.
761fn osig to void
762orng get st 6 get st 7
763end
764note sepat: where the 0 byte after the source starting at p is.
765fn sepat p ulong to ulong
766t = get st 0
767q = p
768while and lt q t ne get inb q 0
769q = add q 1
770end
771if ge q t
772trap 10
773end
774ret q
775end
776note isfn: 1 when the bytes at i are "fn ".
777fn isfn i ulong to ulong
778if ne get inb i 102
779ret 0
780end
781if ne get inb add i 1 110
782ret 0
783end
784if ne get inb add i 2 32
785ret 0
786end
787ret 1
788end
789note sigln: the start of the last line in p .. q-1 that begins with "fn " (the compiler's entry).
790fn sigln p ulong q ulong to ulong
791fl = ulong 0
792nf = ulong 0
793ls = p
794i = p
795while lt i q
796if eq i ls
797if eq isfn i 1
798fl = i
799nf = add nf 1
800end
801end
802if eq get inb i 10
803ls = add i 1
804end
805i = add i 1
806end
807if eq nf 0
808trap 10
809end
810ret fl
811end
812note pkind: the kind of the parameter whose type starts at token ty: 1 ulong, 2 long, 3 edit ulong,
813note 4 view ulong, 5 edit uchar, 6 view uchar, 7 edit f32, 8 view f32, 9 edit ushort, 10 view ushort;
814note 0 when refused. Odd array kinds are writable.
815fn pkind ty ulong nt ulong to ulong
816if eq tkis ty 32 3 1
817ret 11
818end
819if eq tkis ty 46 3 1
820ret 12
821end
822if eq tkis ty 10 5 1
823ret 1
824end
825if eq tkis ty 15 4 1
826ret 2
827end
828w = ulong 0
829if eq tkis ty 6 4 1
830w = 1
831else
832if ne tkis ty 2 4 1
833ret 0
834end
835end
836if ge add ty 2 nt
837ret 0
838end
839el = add ty 1
840if eq tkis el 10 5 1
841ret add 3 w
842end
843if eq tkis el 19 5 1
844ret add 5 w
845end
846if eq tkis el 32 3 1
847ret add 7 w
848end
849if eq tkis el 35 6 1
850ret add 9 w
851end
852ret 0
853end
854note Typed float scalars marshal by bit-copy in each ABI-v1 language binding.
855global fret ulong 1
856text flopt "floatfhost"
857fn fltarg z long to ulong
858if ne z 5
859ret 0
860end
861for i ulong 2
862same = true
863for j ulong 5
864if ne get md j get flopt add mul i 5 j
865same = false
866end
867end
868if same
869ret add i 1
870end
871end
872ret 0
873end
874text nflo "float~double~uint64_t~uint32_t omega_~ *result~, ~ *~);\n~)\n{\n ombr_span sp[7] = {{0}}; unsigned ns=0; uint64_t a[6]={0}, r=0;\n if(ombr_span_add(sp,&ns,result,1,1,~u,1))return OMEGA_BAD_ARG;\n~ memcpy(&a[~], &o_~, ~);\n~ if(ombr_overlap(sp,ns))return OMEGA_BAD_ARG;\n uint32_t status=ombr_run(ombr_entry_~,a,&r,~);\n if(!status)memcpy(result,&r,~);\n return status;\n}\n~#include <string.h>\n~ o_~const void *o_~void *o_~[] __attribute__((visibility(\"hidden\")));\n~/* Omega: ~ */\n"
875fn oflo k ulong to void
876c = ulong 0
877for i ulong len nflo
878b = get nflo i
879if eq b 126
880c = add c 1
881else
882if eq c k
883ob b
884end
885end
886end
887end
888fn fscalar k ulong to bool
889ret or le k 2 and ge k 11 le k 12
890end
891fn hasfloat to bool
892if ne get fret 0 0
893ret true
894end
895for j ulong get st 2
896if and ge get pk j 11 le get pk j 12
897ret true
898end
899end
900ret false
901end
902fn nftype k ulong to void
903if eq k 11
904oflo 0
905else
906if eq k 12
907oflo 1
908else
909oflo 2
910end
911end
912end
913fn nfsize k ulong to ulong
914if eq k 11
915ret 4
916end
917ret 8
918end
919fn nfsig to void
920oflo 3
921oname 1
922ob uchar 40
923nftype get fret 0
924oflo 4
925for j ulong get st 2
926oflo 5
927x = get pk j
928if fscalar x
929nftype x
930oflo 19
931opn j
932else
933if eq rem x 2 0
934oflo 20
935else
936oflo 21
937end
938opn j
939ofr 7
940opn j
941ofr 10
942end
943end
944end
945fn nfloat to void
946m = get st 5
947if eq m 3
948nfpy
949ret
950end
951if or eq m 4 eq m 5
952nfdecl m
953ret
954end
955if eq m 6
956nfgo
957ret
958end
959if and ne m 1 ne m 2
960trap 10
961end
962if eq m 1
963oflo 18
964end
965oflo 23
966osig
967oflo 24
968nfsig
969if eq m 2
970oflo 7
971ret
972end
973oflo 8
974odec nfsize get fret 0
975oflo 9
976for j ulong get st 2
977x = get pk j
978if fscalar x
979if le x 2
980ofr 19
981odec j
982ofr 20
983opn j
984ofr 18
985else
986oflo 10
987odec j
988oflo 11
989opn j
990oflo 12
991odec nfsize x
992oflo 13
993end
994else
995earr j x
996end
997end
998oflo 14
999oname 1
1000oflo 15
1001odec get st 3
1002oflo 16
1003odec nfsize get fret 0
1004oflo 17
1005end
1006text nbind ".argtypes = [ctypes.POINTER(~)~, ~_u64~_a~(result: *mut ~(result: ?*~, o_~: ~: *mut ~: *const ~: ?[*]~: ?[*]const ~_len: u64~(result *~ ~ unsafe.Pointer~_len uint64~) uint32 {\n\treturn uint32(C.omega_~((*C.~)(unsafe.Pointer(result))~, C.~(o_~)~, o_~_len)~))\n}\n"
1007text nftypes "float~double~uint64_t~ctypes.c_float~ctypes.c_double~ctypes.c_uint64~f32~f64~u64~f32~f64~u64~float32~float64~uint64~float~double~uint64_t"
1008fn onbind k ulong to void
1009c = ulong 0
1010for i ulong len nbind
1011b = get nbind i
1012if eq b 126
1013c = add c 1
1014else
1015if eq c k
1016ob b
1017end
1018end
1019end
1020end
1021fn ntype k ulong base ulong to void
1022n = add base 2
1023if eq k 11
1024n = base
1025end
1026if eq k 12
1027n = add base 1
1028end
1029c = ulong 0
1030for i ulong len nftypes
1031b = get nftypes i
1032if eq b 126
1033c = add c 1
1034else
1035if eq c n
1036ob b
1037end
1038end
1039end
1040end
1041fn nfpy to void
1042ofb 13
1043osig
1044ofb 14
1045oname 1
1046ofb 15
1047oname 1
1048ofb 16
1049oname 1
1050onbind 0
1051ntype get fret 0 3
1052onbind 1
1053for j ulong get st 2
1054onbind 2
1055x = get pk j
1056if fscalar x
1057if le x 2
1058onbind 3
1059else
1060ntype x 3
1061end
1062else
1063onbind 4
1064odec x
1065onbind 2
1066onbind 3
1067end
1068end
1069ofb 20
1070oname 1
1071ofb 21
1072end
1073fn nfdecl m ulong to void
1074if eq m 4
1075ofb 23
1076osig
1077ofb 24
1078else
1079ofb 36
1080osig
1081ofb 37
1082end
1083oname 1
1084if eq m 4
1085onbind 5
1086else
1087onbind 6
1088end
1089ntype get fret 0 6
1090for j ulong get st 2
1091onbind 7
1092opn j
1093x = get pk j
1094if fscalar x
1095onbind 8
1096ntype x 6
1097else
1098if eq m 4
1099if eq rem x 2 1
1100onbind 9
1101else
1102onbind 10
1103end
1104else
1105if eq rem x 2 1
1106onbind 11
1107else
1108onbind 12
1109end
1110end
1111if le x 4
1112ntype 1 6
1113else
1114ob uchar 117
1115ob uchar 56
1116end
1117onbind 7
1118opn j
1119onbind 13
1120end
1121end
1122if eq m 4
1123ofb 33
1124else
1125ofb 46
1126end
1127end
1128fn nfgo to void
1129ofb 48
1130osig
1131ofb 49
1132oname 1
1133onbind 14
1134ntype get fret 0 12
1135for j ulong get st 2
1136onbind 7
1137opn j
1138x = get pk j
1139if fscalar x
1140onbind 15
1141ntype x 12
1142else
1143onbind 16
1144onbind 7
1145opn j
1146onbind 17
1147end
1148end
1149onbind 18
1150oname 1
1151onbind 19
1152ntype get fret 0 15
1153onbind 20
1154for j ulong get st 2
1155x = get pk j
1156if fscalar x
1157onbind 21
1158ntype x 15
1159onbind 22
1160opn j
1161onbind 23
1162else
1163onbind 24
1164opn j
1165onbind 21
1166ntype 1 15
1167onbind 22
1168opn j
1169onbind 25
1170end
1171end
1172onbind 26
1173end
1174
1175
1176note cidr: 1 when bytes s .. e-1 form a C identifier ([A-Za-z_][A-Za-z0-9_]*), else 0.
1177fn cidr s ulong e ulong to ulong
1178if ge s e
1179ret 0
1180end
1181for j ulong sub e s
1182c = get inb add s j
1183ok = ulong 0
1184if eq c 95
1185ok = 1
1186end
1187if and ge c 97 le c 122
1188ok = 1
1189end
1190if and ge c 65 le c 90
1191ok = 1
1192end
1193if and gt j 0 and ge c 48 le c 57
1194ok = 1
1195end
1196if eq ok 0
1197ret 0
1198end
1199end
1200ret 1
1201end
1202note lensuf: 1 when bytes s .. e-1 end in "_len" (the binding names an array length o_<name>_len).
1203fn lensuf s ulong e ulong to ulong
1204if lt sub e s 4
1205ret 0
1206end
1207b = sub e 4
1208if ne get inb b 95
1209ret 0
1210end
1211if ne get inb add b 1 108
1212ret 0
1213end
1214if ne get inb add b 2 101
1215ret 0
1216end
1217if ne get inb add b 3 110
1218ret 0
1219end
1220ret 1
1221end
1222note pname: refuse a parameter name that is not a C identifier or ends in "_len".
1223fn pname s ulong e ulong to void
1224if eq cidr s e 0
1225trap 10
1226end
1227if eq lensuf s e 1
1228trap 10
1229end
1230end
1231note samenm: 1 when bytes a .. b-1 equal bytes c .. d-1.
1232fn samenm a ulong b ulong c ulong d ulong to ulong
1233if ne sub b a sub d c
1234ret 0
1235end
1236for j ulong sub b a
1237if ne get inb add a j get inb add c j
1238ret 0
1239end
1240end
1241ret 1
1242end
1243note isat: 1 when input bytes i .. i+n-1 lie before q and equal kw bytes o .. o+n-1, else 0.
1244fn isat i ulong o ulong n ulong q ulong to ulong
1245if gt add i n q
1246ret 0
1247end
1248for j ulong n
1249if ne get inb add i j get kw add o j
1250ret 0
1251end
1252end
1253ret 1
1254end
1255note spat: 1 when byte a lies before q and is a space, else 0.
1256fn spat a ulong q ulong to ulong
1257if ge a q
1258ret 0
1259end
1260if ne get inb a 32
1261ret 0
1262end
1263ret 1
1264end
1265note cline: the value on the line at i when it is exactly "const NAME ulong DIGITS" and NAME is the
1266note bytes s .. e-1; 0 when it is another line. A matching line whose value is not 1 .. 8 digits, or
1267note is 0, is refused.
1268fn cline i ulong q ulong s ulong e ulong to ulong
1269if eq isat i 41 5 q 0
1270ret 0
1271end
1272a = add i 5
1273if eq spat a q 0
1274ret 0
1275end
1276a = add a 1
1277n = sub e s
1278if gt add a n q
1279ret 0
1280end
1281if eq samenm a add a n s e 0
1282ret 0
1283end
1284a = add a n
1285if eq spat a q 0
1286ret 0
1287end
1288a = add a 1
1289if eq isat a 10 5 q 0
1290ret 0
1291end
1292a = add a 5
1293if eq spat a q 0
1294ret 0
1295end
1296a = add a 1
1297v = ulong 0
1298d = ulong 0
1299while and lt a q ne get inb a 10
1300c = get inb a
1301if or lt c 48 gt c 57
1302trap 10
1303end
1304if ge d 8
1305trap 10
1306end
1307v = add mul v 10 ulong sub c 48
1308d = add d 1
1309a = add a 1
1310end
1311if or eq d 0 eq v 0
1312trap 10
1313end
1314ret v
1315end
1316note cval: the value of the integer constant named by bytes s .. e-1, defined on a line of the unit
1317note before its signature line (st 9 is the unit start, st 6 the signature line); refused when absent.
1318fn cval s ulong e ulong to ulong
1319p = get st 9
1320q = get st 6
1321ls = p
1322i = p
1323while lt i q
1324if eq i ls
1325v = cline i q s e
1326if gt v 0
1327ret v
1328end
1329end
1330if eq get inb i 10
1331ls = add i 1
1332end
1333i = add i 1
1334end
1335trap 10
1336end
1337note tklen: an array length token: decimal digits, or the name of an integer constant (cval).
1338fn tklen k ulong to ulong
1339s = get tks k
1340e = get tke k
1341if eq s e
1342trap 10
1343end
1344c = get inb s
1345if and ge c 48 le c 57
1346ret tknum k
1347end
1348ret cval s e
1349end
1350note uname: refuse a unit name (token 1) that is not a C identifier or repeats an earlier unit name.
1351fn uname to void
1352s = get tks 1
1353e = get tke 1
1354if eq cidr s e 0
1355trap 10
1356end
1357n = get st 4
1358if ge n len us
1359trap 10
1360end
1361for j ulong n
1362if eq samenm s e get us j get ue j 1
1363trap 10
1364end
1365end
1366put us n s
1367put ue n e
1368put st 4 add n 1
1369end
1370note pparam: record the parameter at token k in slot st 2; return the token after it.
1371fn pparam k ulong nt ulong to ulong
1372np = get st 2
1373if ge np 6
1374trap 10
1375end
1376put ps np get tks k
1377put pe np get tke k
1378ki = pkind add k 1 nt
1379if eq ki 0
1380trap 10
1381end
1382put pk np ki
1383pname get tks k get tke k
1384put st 2 add np 1
1385if fscalar ki
1386ret add k 2
1387end
1388ln = tklen add k 3
1389if or eq ln 0 gt ln 16777216
1390trap 10
1391end
1392put pl np ln
1393ret add k 4
1394end
1395note parse: read the signature line at fl; parameters into ps pe pk pl, their count into st 2,
1396note 1 or 0 into st 3 for a function with or without a value.
1397fn parse fl ulong q ulong to void
1398put st 6 fl
1399nt = tokln fl q
1400if lt nt 4
1401trap 10
1402end
1403if eq tkis 1 28 4 1
1404trap 10
1405end
1406uname
1407put fret 0 0
1408put st 2 0
1409k = ulong 2
1410while eq tkis k 0 2 0
1411if ge add k 1 nt
1412trap 10
1413end
1414k = pparam k nt
1415if ge k nt
1416trap 10
1417end
1418end
1419if ne add k 2 nt
1420trap 10
1421end
1422r = add k 1
1423hv = ulong 2
1424if or eq tkis r 10 5 1 eq tkis r 15 4 1
1425hv = 1
1426end
1427if eq tkis r 32 3 1
1428hv = 1
1429put fret 0 11
1430end
1431if eq tkis r 46 3 1
1432hv = 1
1433put fret 0 12
1434end
1435if eq tkis r 24 4 1
1436hv = 0
1437end
1438if eq hv 2
1439trap 10
1440end
1441put st 3 hv
1442if and hasfloat eq get fok 0 0
1443trap 10
1444end
1445end
1446note codelen: the 8-digit code length after the 0 byte at q, checked against the input.
1447fn codelen q ulong to ulong
1448t = get st 0
1449c0 = add q 9
1450if gt c0 t
1451trap 10
1452end
1453cl = ulong 0
1454for j ulong 8
1455d = get inb add add q 1 j
1456if or lt d 48 gt d 57
1457trap 10
1458end
1459cl = add mul cl 10 ulong sub d 48
1460end
1461if eq cl 0
1462trap 10
1463end
1464if and eq get xt 0 0 ne rem cl 4 0
1465trap 10
1466end
1467if gt cl sub t c0
1468trap 10
1469end
1470ret cl
1471end
1472note xcode: x86linux code bytes as ") ":" (closing the OMBR_SYM label), then .quad words (byte 8q+7 is the high
1473note digit pair, so the little-endian assembler lays down the compiler's bytes in order), then any tail as .byte.
1474fn xcode c0 ulong cl ulong to void
1475ob uchar 41
1476ob uchar 32
1477ob uchar 34
1478ob uchar 58
1479nq = div cl 8
1480if gt nq 0
1481for j ulong len xqd
1482ob get xqd j
1483end
1484end
1485for q ulong nq
1486if gt q 0
1487ob uchar 44
1488end
1489ob uchar 48
1490ob uchar 120
1491for k ulong 8
1492b = get inb add add c0 mul q 8 sub 7 k
1493ob get hx shr b 4
1494ob get hx and b 15
1495end
1496end
1497r = mul nq 8
1498if lt r cl
1499for j ulong len xby
1500ob get xby j
1501end
1502end
1503for j ulong sub cl r
1504if gt j 0
1505ob uchar 44
1506end
1507ohex get inb add c0 add r j
1508end
1509end
1510note hassvc: 1 when a code word is an svc (bytes x1 xx x0 d4 under the mask 1f 00 e0 ff): a host word's
1511note Linux system call, which macOS numbers differently; the C file then refuses to build on macOS.
1512fn hassvc c0 ulong cl ulong to ulong
1513j = ulong 0
1514while lt j cl
1515p = add c0 j
1516if eq get inb add p 3 212
1517if eq and get inb add p 2 224 0
1518if eq and get inb p 31 1
1519ret 1
1520end
1521end
1522end
1523j = add j 4
1524end
1525ret 0
1526end
1527note ecode: the embedded machine code, unchanged (x86linux packs it as little-endian .quad words).
1528fn ecode c0 ulong cl ulong to void
1529if ne get xt 0 1
1530if eq hassvc c0 cl 1
1531ofr 28
1532oname 1
1533ofr 29
1534end
1535end
1536ofr 0
1537oname 1
1538ofr 1
1539oname 1
1540ofr 2
1541oname 1
1542if eq get xt 0 1
1543xcode c0 cl
1544else
1545ofr 3
1546for j ulong cl
1547if gt j 0
1548ob uchar 44
1549end
1550ohex get inb add c0 j
1551end
1552end
1553ofr 24
1554oname 1
1555ofr 25
1556oname 1
1557ofr 26
1558oname 1
1559ofr 27
1560oname 1
1561ofr 4
1562oname 1
1563if hasfloat
1564oflo 22
1565else
1566ofr 5
1567oname 1
1568end
1569end
1570note esig: the exported C parameter list.
1571fn esig to void
1572ofr 6
1573for j ulong get st 2
1574x = get pk j
1575if le x 2
1576ofr 7
1577opn j
1578else
1579if eq rem x 2 1
1580ofr 8
1581else
1582ofr 9
1583end
1584opn j
1585ofr 7
1586opn j
1587ofr 10
1588end
1589end
1590end
1591note earr: the checks and argument word for array parameter j of kind x.
1592fn earr j ulong x ulong to void
1593ofr 12
1594opn j
1595ofr 13
1596opn j
1597ofr 14
1598odec get pl j
1599ofr 15
1600if le x 4
1601odec 8
1602else
1603if le x 6
1604odec 1
1605else
1606if le x 8
1607odec 4
1608else
1609odec 2
1610end
1611end
1612end
1613ofr 15
1614odec rem x 2
1615ofr 16
1616odec j
1617ofr 17
1618opn j
1619ofr 18
1620end
1621note ebody: argument words, overlap check and the call.
1622fn ebody to void
1623ofr 11
1624for j ulong get st 2
1625x = get pk j
1626if le x 2
1627ofr 19
1628odec j
1629ofr 20
1630opn j
1631ofr 18
1632else
1633earr j x
1634end
1635end
1636ofr 21
1637oname 1
1638ofr 22
1639odec get st 3
1640ofr 23
1641end
1642note edeclC: a declaration with the type before the name (h): the comment
1643note fragments at cb (open, close + head) and the type fragments at tb (result, scalar, writable
1644note void*, readonly const void*, two unused old kind slots, the length suffix, close). Writable
1645note arrays, including f32 and ushort, use the void* fragment; readonly arrays use const void*.
1646fn edeclC cb ulong tp ulong to void
1647ofb cb
1648osig
1649ofb add cb 1
1650oname 1
1651ofb tp
1652for j ulong get st 2
1653x = get pk j
1654if le x 2
1655ofb add tp 1
1656opn j
1657else
1658if eq rem x 2 1
1659ofb add tp 2
1660else
1661ofb add tp 3
1662end
1663opn j
1664ofb add tp 1
1665opn j
1666ofb add tp 6
1667end
1668end
1669ofb add tp 7
1670end
1671note edeclR: a declaration with the name before the type (rust, zig, the go wrapper head): the
1672note type fragments at tp are result, the name prefix, scalar, the four array kinds, length, close.
1673note Kinds above 6 (f32, ushort) reuse the uchar pointer fragment of the same writability, so Rust
1674note and Zig see a byte pointer; the C library still checks the element size.
1675fn edeclR cb ulong tp ulong to void
1676ofb cb
1677osig
1678ofb add cb 1
1679oname 1
1680ofb tp
1681for j ulong get st 2
1682x = get pk j
1683ofb add tp 1
1684opn j
1685if le x 2
1686ofb add tp 2
1687else
1688ax = x
1689if gt x 6
1690ax = rem x 2
1691ax = sub 6 ax
1692end
1693ofb add tp ax
1694ofb add tp 1
1695opn j
1696ofb add tp 7
1697end
1698end
1699ofb add tp 8
1700end
1701note epy: the python declaration: the function, its argument types, its result type.
1702fn epy to void
1703ofb 13
1704osig
1705ofb 14
1706oname 1
1707ofb 15
1708oname 1
1709ofb 16
1710oname 1
1711ofb 17
1712for j ulong get st 2
1713if le get pk j 2
1714ofb 18
1715else
1716ofb 19
1717odec get pk j
1718ofb 18
1719end
1720end
1721ofb 20
1722oname 1
1723ofb 21
1724end
1725note ecall: the go wrapper's call of the C function and its end.
1726fn ecall to void
1727oname 1
1728ofb 59
1729for j ulong get st 2
1730if le get pk j 2
1731ofb 60
1732opn j
1733ofb 61
1734else
1735ofb 62
1736opn j
1737ofb 60
1738opn j
1739ofb 63
1740end
1741end
1742ofb 64
1743end
1744note emit: the parsed unit in the output named by st 5.
1745fn emit c0 ulong cl ulong to void
1746m = get st 5
1747if hasfloat
1748if eq m 1
1749ecode c0 cl
1750end
1751nfloat
1752ret
1753end
1754if eq m 10
1755ecode c0 cl
1756esig
1757fbody
1758end
1759if eq m 1
1760ecode c0 cl
1761esig
1762ebody
1763end
1764if eq m 2
1765edeclC 1 3
1766end
1767if eq m 3
1768epy
1769end
1770if eq m 4
1771edeclR 23 25
1772end
1773if eq m 5
1774edeclR 36 38
1775end
1776if eq m 6
1777edeclR 48 50
1778ecall
1779end
1780end
1781note ======== ABI v2: OM-2 units (docs/NATIVE-ABI.md, ABI v2) ========
1782note An OM-2 unit (its first line that is not a note is exactly om2) is bound as omega2_<name>, one symbol
1783note per `export fn` (in source order; without any export, the last fn as in v1), and carries its
1784note identity. Its frame is the v1 frame followed by the line `omega-unit NAME OMC`, NAME the unit's C
1785note name and OMC the sha256 of the compiler that wrote the code (64 hex digits), both supplied by
1786note tools/native. The source sha256 is computed here from the unit's exact bytes. A parameter is a
1787note ulong or long scalar (one slot), a sized array `view|edit T N` (one slot), an unsized view
1788note `view|edit T *` (two slots: pointer, element count), a layout `view|edit L` (one slot) or
1789note `out T N` (one slot), T any of uchar ushort uint ulong byte short int long; at most 6 slots; the
1790note result is ulong, long or void. s2: 0 the C prelude written, 1 2 the unit name, 3 the omc digest,
1791note 4 the export count, 5 slots used, 6 7 the source, 8 has a value, 9 out bytes, 10 layouts,
1792note 11 unit names, 12 the entry index, 13 14 the code, 16 17 the bound name, 18 the result type.
1793note q*: per parameter class (1 scalar, 2 sized array, 3 unsized view, 4 layout, 5 out), mode (0 view,
1794note 1 edit), element type, count (or layout size), first slot, layout index.
1795global s2 ulong 32
1796global qk ulong 6
1797global qm ulong 6
1798global qt ulong 6
1799global qn ulong 6
1800global qs ulong 6
1801global ql ulong 6
1802global lys ulong 32
1803global lye ulong 32
1804global lyz ulong 32
1805global lyl ulong 32
1806global lcs ulong 32
1807global lce ulong 32
1808global uns ulong 1024
1809global une ulong 1024
1810global exl ulong 32
1811global fds ulong 256
1812global fde ulong 256
1813global fdt ulong 256
1814global fdc ulong 256
1815global fdo ulong 256
1816global fdb ulong 256
1817global fdu ulong 256
1818global shk ulong 72
1819global shw ulong 64
1820global shh ulong 8
1821global shv ulong 8
1822global shb uchar 64
1823note kv: the OM-2 words: export 0, fn 6, out 8, om2 11, ushort 14, uint 20, byte 24, short 28, int 33,
1824note layout 36, field 42, end 47, note 50, bridge 54, c-type 60, c-define 66, little 74, big 80, * 83,
1825note at 84, omega-unit 86.
1826text kv "exportfnoutom2ushortuintbyteshortintlayoutfieldendnotebridgec-typec-definelittlebig*atomega-unit"
1827note fv: the v2 output fragments, separated by ~ (byte 126): 0 the C prelude, 1-10 the code and the
1828note identity, 11-13 the identity text, 14-20 an entry stub, 21-24 the C function head, 25-30 a C
1829note parameter, 31-50 the C body, 51-56 h, 57-75 and 155 python, 76-88 rust, 89-98 zig, 99-117 go,
1830note 118-154 the api header. tn: the element type names, 8 per language: C, ctypes, rust and zig, cgo.
1831text fv "/* ABI v2 (docs/NATIVE-ABI.md, ABI v2): OM-2 units, bound as omega2_<export>. An unsized view is its\n * pointer and its element count; a layout is a pointer to its fixed bytes; an out parameter is written\n * to a buffer the binding owns and copied to the caller only after status 0. */\n#include <string.h>\ntypedef struct ombr2_out { unsigned slot; uint64_t n; void *dst; } ombr2_out;\n/* A library of OM-2 units alone calls neither of the ABI v1 helpers. */\n__attribute__((unused)) static void ombr2_keep(void)\n{\n (void)ombr_overlap;\n (void)ombr_run;\n}\n/* An unsized view of n elements of es bytes. n == 0 is the empty view whatever the pointer (it adds no\n * range, so it overlaps nothing). Otherwise refused: a null pointer, a pointer not aligned to es,\n * n * es over 64 bits, or an end past the top of the address space. */\n__attribute__((unused)) static int ombr2_view(ombr_span *sp, unsigned *ns, const void *p, uint64_t n, uint64_t es, int w)\n{\n uintptr_t lo = (uintptr_t)p;\n if (n == 0u)\n return 0;\n if (!p || lo % es != 0u || n > UINT64_MAX / es || n * es > UINTPTR_MAX - lo)\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}\n/* A fixed range of nb > 0 bytes (a layout or an out parameter) aligned to es. */\n__attribute__((unused)) static int ombr2_fixed(ombr_span *sp, unsigned *ns, const void *p, uint64_t nb, uint64_t es,\n int w)\n{\n uintptr_t lo = (uintptr_t)p;\n if (!p || lo % es != 0u || nb > UINTPTR_MAX - lo)\n return 1;\n sp[*ns].lo = lo;\n sp[*ns].hi = lo + nb;\n sp[*ns].w = w;\n (*ns)++;\n return 0;\n}\n/* w: 0 view, 1 edit or the result, 2 out. Refused: an edit or the result overlapping any other range,\n * or two out ranges overlapping. An out may overlap a view: it is written only after the code returns. */\nstatic int ombr2_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 == 1 || sp[j].w == 1 || (sp[i].w == 2 && sp[j].w == 2))\n && sp[i].lo < sp[j].hi && sp[j].lo < sp[i].hi)\n return 1;\n return 0;\n}\n/* As ombr_run, and each out buffer is carved from the top of the thread's Omega stack (16-byte aligned,\n * zero-filled), passed in its slot and copied to the caller with memmove only after status 0. */\nstatic uint32_t ombr2_run(const void *code, uint64_t *args, uint64_t *result, int has_value, const ombr2_out *o,\n unsigned no)\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 unsigned char *buf[6] = {0};\n for (unsigned i = 0; i < no; i++) {\n top -= (o[i].n + 15u) / 16u * 16u;\n memset(top, 0, o[i].n);\n buf[i] = top;\n args[o[i].slot] = (uint64_t)(uintptr_t)top;\n }\n uint64_t v = 0;\n uint32_t s = ombr_enter(code, args, &ombr_rt_table, &v, top);\n if (s == OMEGA_OK) {\n for (unsigned i = 0; i < no; i++)\n memmove(o[i].dst, buf[i], o[i].n);\n *result = has_value ? v : 0u;\n }\n ombr_active = 0;\n return s;\n}\n~__asm__(OMBR_CODE_SECTION \".balign 4\\n\" OMBR_LOCAL(ombr2_code_~) OMBR_HIDDEN(ombr2_code_~) OMBR_SYM(ombr2_code_~) \":\\n.byte ~\\n\" OMBR_PREVIOUS);\n~static const char ombr2_ident_~[] = \"~\";\n__attribute__((visibility(\"default\"))) const char *omega2_ident_~(void)\n{\n return ombr2_ident_~;\n}\n~omega-ident 1 unit=~ abi=2 profile=OM-2 src=~ omc=~__asm__(OMBR_CODE_SECTION \".balign 4\\n\" OMBR_LOCAL(ombr2_entry_~) OMBR_HIDDEN(ombr2_entry_~) OMBR_SYM(ombr2_entry_~) \":\\nhint #34\\nb \" OMBR_SYM(ombr2_code_~) \"+~\\n\" OMBR_PREVIOUS);\nextern const unsigned char ombr2_entry_~[] __attribute__((visibility(\"hidden\")));\n~/* ~ */\n__attribute__((visibility(\"default\"))) uint32_t omega2_~(uint64_t *result~)\n{\n ombr_span sp[7] = {{0}};\n unsigned ns = 0;\n uint64_t a[6] = {0};\n ombr2_out ou[6] = {{0, 0, 0}};\n unsigned no = 0;\n if (ombr_span_add(sp, &ns, result, 1u, 1u, 8u, 1))\n return OMEGA_BAD_ARG;\n~, uint64_t o_~, ~const ~ *o_~, uint64_t o_~_len~ if (ombr_span_add(sp, &ns, o_~, o_~_len, ~u, ~))\n return OMEGA_BAD_ARG;\n~ a[~] = (uint64_t)(uintptr_t)o_~;\n~] = o_~ if (ombr2_view(sp, &ns, o_~ if (ombr2_fixed(sp, &ns, o_~_len;\n~2))\n return OMEGA_BAD_ARG;\n ou[no].slot = ~u;\n ou[no].n = ~u;\n ou[no].dst = o_~;\n no++;\n~1u, ~ if (ombr2_overlap(sp, ns))\n return OMEGA_BAD_ARG;\n return ombr2_run(ombr2_entry_~, a, result, ~, ou, no);\n}\n~#define OMEGA2_IDENT_~ \"~\"\nconst char *omega2_ident_~(void);\n~ */\nuint32_t omega2_~);\n~# ~\nomega2_~ = lib.omega2_~.argtypes = [_res~]\nomega2_~.restype = ctypes.c_uint32\n~, _u64~, ctypes.c_char_p~, ctypes.POINTER(ctypes.~)~omega2_ident_~.argtypes = []\nomega2_ident_~.restype = ctypes.c_char_p\nOMEGA2_IDENT_~ = b\"~\"\nif omega2_ident_~() != OMEGA2_IDENT_~:\n raise ImportError(\"libomega_native.so is not the library this binding was generated with: the identity of unit ~ differs\")\n~\n~ // ~\n pub fn omega2_~(result: *mut u64~) -> u32;\n~, o_~: u64~: *const ~: *mut ~_len: u64~ pub fn omega2_ident_~() -> *const u8;\n}\n#[allow(dead_code, non_upper_case_globals)]\npub const OMEGA2_IDENT_~: &str = \"~\";\n#[allow(dead_code)]\n#[link(name = \"omega_native\")]\nunsafe extern \"C\" {\n~// ~\npub extern fn omega2_~(result: ?*u64~) u32;\n~: ?[*]const ~: ?[*]~pub const OMEGA2_IDENT_~ = \"~\";\npub extern fn omega2_ident_~() [*:0]const u8;\n~\n// ~\nfunc Omega2_~(result *uint64~) uint32 {\n\treturn uint32(C.omega2_~((*C.uint64_t)(unsafe.Pointer(result))~))\n}\n~ uint64~ unsafe.Pointer~_len uint64~, C.uint64_t(o_~)~, (*C.~)(o_~_len)~\nconst Omega2Ident_~ = \"~\"\n\nfunc Omega2_ident_~() string {\n\treturn C.GoString(C.omega2_ident_~())\n}\n~/* Generated by omega-bridge src/native.om: the C API of the ABI v2 units in libomega_native\n * (docs/NATIVE-ABI.md, ABI v2). Each function calls omega2_<name> and calls abort() on a nonzero status.\n * Regenerate it; do not edit it. */\n#ifndef OMEGA_API_H\n#define OMEGA_API_H\n#include <stddef.h>\n#include <stdint.h>\n#include <stdlib.h>\n#include \"omega_native.h\"\n#ifdef __cplusplus\n#define OMBR_ASSERT static_assert\nextern \"C\" {\n#else\n#define OMBR_ASSERT _Static_assert\n#endif\n~#ifdef __cplusplus\n}\n#endif\n#endif\n~#define ~ ~typedef struct {\n~} ~;\nOMBR_ASSERT(sizeof(~) == ~, \"layout ~ size\");\n~OMBR_ASSERT(offsetof(~, ~ field ~\");\n~ ~[~];\n~ uint8_t ombr_pad~ */\nstatic inline ~void~uint64_t~int64_t~(~)\n{\n uint64_t r = 0;\n if (omega2_~(&r~) != OMEGA_OK)\n abort();\n~ (void)r;\n}\n~ return r;\n}\n~ return (int64_t)r;\n}\n~, ~ o_~ *o_~, size_t o_~, (uint8_t *)o_~, (const uint8_t *)o_~, (uint64_t)o_~]~ = lib."
1832text fvdev "/* ABI v2 (docs/NATIVE-ABI.md, ABI v2): OM-2 units, bound as omega2_<export>. An unsized view is its\n * pointer and its element count; a layout is a pointer to its fixed bytes; an out parameter is written\n * to a buffer the binding owns and copied to the caller only after status 0. */\n#include <string.h>\ntypedef struct ombr2_out { unsigned slot; uint64_t n; void *dst; } ombr2_out;\n/* A library of OM-2 units alone calls neither of the ABI v1 helpers. */\n__attribute__((unused)) static void ombr2_keep(void)\n{\n (void)ombr_overlap;\n (void)ombr_run;\n}\n/* An unsized view of n elements of es bytes. n == 0 is the empty view whatever the pointer (it adds no\n * range, so it overlaps nothing). Otherwise refused: a null pointer, a pointer not aligned to es,\n * n * es over 64 bits, or an end past the top of the address space. */\n__attribute__((unused)) static int ombr2_view(ombr_span *sp, unsigned *ns, const void *p, uint64_t n, uint64_t es, int w)\n{\n uintptr_t lo = (uintptr_t)p;\n if (n == 0u)\n return 0;\n if (!p || lo % es != 0u || n > UINT64_MAX / es || n * es > UINTPTR_MAX - lo)\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}\n/* A fixed range of nb > 0 bytes (a layout or an out parameter) aligned to es. */\n__attribute__((unused)) static int ombr2_fixed(ombr_span *sp, unsigned *ns, const void *p, uint64_t nb, uint64_t es,\n int w)\n{\n uintptr_t lo = (uintptr_t)p;\n if (!p || lo % es != 0u || nb > UINTPTR_MAX - lo)\n return 1;\n sp[*ns].lo = lo;\n sp[*ns].hi = lo + nb;\n sp[*ns].w = w;\n (*ns)++;\n return 0;\n}\n/* w: 0 view, 1 edit or the result, 2 out. Refused: an edit or the result overlapping any other range,\n * or two out ranges overlapping. An out may overlap a view: it is written only after the code returns. */\nstatic int ombr2_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 == 1 || sp[j].w == 1 || (sp[i].w == 2 && sp[j].w == 2))\n && sp[i].lo < sp[j].hi && sp[j].lo < sp[i].hi)\n return 1;\n return 0;\n}\n/* As ombr_run, and each out buffer is carved from the top of the thread's Omega stack (16-byte aligned,\n * zero-filled), passed in its slot and copied to the caller with memmove only after status 0. */\nstatic uint32_t ombr2_run(omega_native_context *context, const void *code, uint64_t *args, uint64_t *result, int has_value, const ombr2_out *o,\n unsigned no)\n{\n uint32_t s = ombr_context_lock(context);\n if (s != OMEGA_OK)\n return s;\n unsigned char *top = ombr_stack();\n if (!top)\n return ombr_context_unlock(context, OMEGA_NO_STACK);\n unsigned char *buf[6] = {0};\n for (unsigned i = 0; i < no; i++) {\n top -= (o[i].n + 15u) / 16u * 16u;\n memset(top, 0, o[i].n);\n buf[i] = top;\n args[o[i].slot] = (uint64_t)(uintptr_t)top;\n }\n uint64_t v = 0;\n s = ombr_enter(code, args, context->rt, &v, top);\n if (s == OMEGA_OK) {\n for (unsigned i = 0; i < no; i++)\n memmove(o[i].dst, buf[i], o[i].n);\n *result = has_value ? v : 0u;\n }\n return ombr_context_unlock(context, s);\n}\n~__asm__(OMBR_CODE_SECTION \".balign 4\\n\" OMBR_LOCAL(ombr2_code_~) OMBR_HIDDEN(ombr2_code_~) OMBR_SYM(ombr2_code_~) \":\\n.byte ~\\n\" OMBR_PREVIOUS);\n~static const char ombr2_ident_~[] = \"~\";\n__attribute__((visibility(\"default\"))) const char *omega2_ident_~(void)\n{\n return ombr2_ident_~;\n}\n~omega-ident 1 unit=~ abi=2 profile=OM-2 src=~ omc=~__asm__(OMBR_CODE_SECTION \".balign 4\\n\" OMBR_LOCAL(ombr2_entry_~) OMBR_HIDDEN(ombr2_entry_~) OMBR_SYM(ombr2_entry_~) \":\\nhint #34\\nb \" OMBR_SYM(ombr2_code_~) \"+~\\n\" OMBR_PREVIOUS);\nextern const unsigned char ombr2_entry_~[] __attribute__((visibility(\"hidden\")));\n~/* ~ */\n__attribute__((visibility(\"default\"))) uint32_t omega2_~(uint64_t *result~)\n{\n ombr_span sp[7] = {{0}};\n unsigned ns = 0;\n uint64_t a[6] = {0};\n ombr2_out ou[6] = {{0, 0, 0}};\n unsigned no = 0;\n if (ombr_span_add(sp, &ns, result, 1u, 1u, 8u, 1))\n return OMEGA_BAD_ARG;\n~, uint64_t o_~, ~const ~ *o_~, uint64_t o_~_len~ if (ombr_span_add(sp, &ns, o_~, o_~_len, ~u, ~))\n return OMEGA_BAD_ARG;\n~ a[~] = (uint64_t)(uintptr_t)o_~;\n~] = o_~ if (ombr2_view(sp, &ns, o_~ if (ombr2_fixed(sp, &ns, o_~_len;\n~2))\n return OMEGA_BAD_ARG;\n ou[no].slot = ~u;\n ou[no].n = ~u;\n ou[no].dst = o_~;\n no++;\n~1u, ~ if (ombr2_overlap(sp, ns))\n return OMEGA_BAD_ARG;\n return ombr2_run(&ombr_default, ombr2_entry_~, a, result, ~, ou, no);\n}\n~#define OMEGA2_IDENT_~ \"~\"\nconst char *omega2_ident_~(void);\n~ */\nuint32_t omega2_~);\n~# ~\nomega2_~ = lib.omega2_~.argtypes = [_res~]\nomega2_~.restype = ctypes.c_uint32\n~, _u64~, ctypes.c_char_p~, ctypes.POINTER(ctypes.~)~omega2_ident_~.argtypes = []\nomega2_ident_~.restype = ctypes.c_char_p\nOMEGA2_IDENT_~ = b\"~\"\nif omega2_ident_~() != OMEGA2_IDENT_~:\n raise ImportError(\"libomega_native.so is not the library this binding was generated with: the identity of unit ~ differs\")\n~\n~ // ~\n pub fn omega2_~(result: *mut u64~) -> u32;\n~, o_~: u64~: *const ~: *mut ~_len: u64~ pub fn omega2_ident_~() -> *const u8;\n}\n#[allow(dead_code, non_upper_case_globals)]\npub const OMEGA2_IDENT_~: &str = \"~\";\n#[allow(dead_code)]\n#[link(name = \"omega_native\")]\nunsafe extern \"C\" {\n~// ~\npub extern fn omega2_~(result: ?*u64~) u32;\n~: ?[*]const ~: ?[*]~pub const OMEGA2_IDENT_~ = \"~\";\npub extern fn omega2_ident_~() [*:0]const u8;\n~\n// ~\nfunc Omega2_~(result *uint64~) uint32 {\n\treturn uint32(C.omega2_~((*C.uint64_t)(unsafe.Pointer(result))~))\n}\n~ uint64~ unsafe.Pointer~_len uint64~, C.uint64_t(o_~)~, (*C.~)(o_~_len)~\nconst Omega2Ident_~ = \"~\"\n\nfunc Omega2_ident_~() string {\n\treturn C.GoString(C.omega2_ident_~())\n}\n~/* Generated by omega-bridge src/native.om: the C API of the ABI v2 units in libomega_native\n * (docs/NATIVE-ABI.md, ABI v2). Each function calls omega2_<name> and calls abort() on a nonzero status.\n * Regenerate it; do not edit it. */\n#ifndef OMEGA_API_H\n#define OMEGA_API_H\n#include <stddef.h>\n#include <stdint.h>\n#include <stdlib.h>\n#include \"omega_native.h\"\n#ifdef __cplusplus\n#define OMBR_ASSERT static_assert\nextern \"C\" {\n#else\n#define OMBR_ASSERT _Static_assert\n#endif\n~#ifdef __cplusplus\n}\n#endif\n#endif\n~#define ~ ~typedef struct {\n~} ~;\nOMBR_ASSERT(sizeof(~) == ~, \"layout ~ size\");\n~OMBR_ASSERT(offsetof(~, ~ field ~\");\n~ ~[~];\n~ uint8_t ombr_pad~ */\nstatic inline ~void~uint64_t~int64_t~(~)\n{\n uint64_t r = 0;\n if (omega2_~(&r~) != OMEGA_OK)\n abort();\n~ (void)r;\n}\n~ return r;\n}\n~ return (int64_t)r;\n}\n~, ~ o_~ *o_~, size_t o_~, (uint8_t *)o_~, (const uint8_t *)o_~, (uint64_t)o_~]~ = lib."
1833text tn "uint8_t~uint16_t~uint32_t~uint64_t~int8_t~int16_t~int32_t~int64_t~c_uint8~c_uint16~c_uint32~c_uint64~c_int8~c_int16~c_int32~c_int64~u8~u16~u32~u64~i8~i16~i32~i64~uint8_t~uint16_t~uint32_t~uint64_t~int8_t~int16_t~int32_t~int64_t"
1834note shx: SHA-256 (FIPS 180-4) K[0..63] then H0[0..7], 8 hex digits each.
1835text shx "428a2f9871374491b5c0fbcfe9b5dba53956c25b59f111f1923f82a4ab1c5ed5d807aa9812835b01243185be550c7dc372be5d7480deb1fe9bdc06a7c19bf174e49b69c1efbe47860fc19dc6240ca1cc2de92c6f4a7484aa5cb0a9dc76f988da983e5152a831c66db00327c8bf597fc7c6e00bf3d5a7914706ca63511429296727b70a852e1b21384d2c6dfc53380d13650a7354766a0abb81c2c92e92722c85a2bfe8a1a81a664bc24b8b70c76c51a3d192e819d6990624f40e3585106aa07019a4c1161e376c082748774c34b0bcb5391c0cb34ed8aa4a5b9cca4f682e6ff3748f82ee78a5636f84c878148cc7020890befffaa4506cebbef9a3f7c67178f26a09e667bb67ae853c6ef372a54ff53a510e527f9b05688c1f83d9ab5be0cd19"
1836note fa: the out-of-line API fragments (outputs apih and apic), separated by ~: 0 the close of a
1837note signature comment, 1 the end of a prototype, 2 the apih header's start, 3 the apic file's start.
1838text fa " */\n~);\n~/* Generated by omega-bridge src/native.om: the C API of the ABI v2 units in libomega_native\n * (docs/NATIVE-ABI.md, ABI v2), declarations only (output apih). The definitions are the generated C\n * file of output apic: each calls omega2_<name> and calls abort() on a nonzero status. Including this\n * header references no C library function. Regenerate it; do not edit it. */\n#ifndef OMEGA_API_DECL_H\n#define OMEGA_API_DECL_H\n#include <stddef.h>\n#include <stdint.h>\n#include \"omega_native.h\"\n#ifdef __cplusplus\n#define OMBR_ASSERT static_assert\nextern \"C\" {\n#else\n#define OMBR_ASSERT _Static_assert\n#endif\n~/* Generated by omega-bridge src/native.om: the definitions of the C API declared in\n * omega_api_decl.h (output apic). Each calls omega2_<name> and calls abort() on a nonzero status.\n * Regenerate it; do not edit it. */\n#include <stdlib.h>\n#include \"omega_api_decl.h\"\n"
1839fn ofa k ulong to void
1840c = ulong 0
1841for i ulong len fa
1842b = get fa i
1843if eq b 126
1844c = add c 1
1845else
1846if eq c k
1847ob b
1848end
1849end
1850end
1851end
1852note fq: the freestanding v2 fragments (outputs freest and fapic), separated by ~: 0 the runtime, 1-2 the
1853note out buffer in an entry's frame, 3-4 the end of an entry, 5 the trap of a refused call, 6 the fapic
1854note file's start.
1855text fq "/* ABI v2, freestanding (docs/NATIVE-ABI.md, ABI v2, output freest): OM-2 units on the caller's stack. The\n * argument checks are those of output c. Each out parameter is written to a buffer in the calling\n * function's frame (16-byte aligned, zero-filled) and copied to the caller only after status 0. No\n * thread-local state, heap or stack mapping; no C library calls are required. */\ntypedef struct ombr_span { uintptr_t lo, hi; int w; } ombr_span;\ntypedef struct ombr2_out { unsigned slot; uint64_t n; void *dst; } ombr2_out;\n/* A binding of OM-2 units alone does not call the ABI v1 helper. */\n__attribute__((unused)) static void ombr2_keep(void)\n{\n (void)ombr_run;\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}\n/* An unsized view of n elements of es bytes, as in output c. */\n__attribute__((unused)) static int ombr2_view(ombr_span *sp, unsigned *ns, const void *p, uint64_t n, uint64_t es, int w)\n{\n uintptr_t lo = (uintptr_t)p;\n if (n == 0u)\n return 0;\n if (!p || lo % es != 0u || n > UINT64_MAX / es || n * es > UINTPTR_MAX - lo)\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}\n/* A fixed range of nb > 0 bytes (a layout or an out parameter) aligned to es. */\n__attribute__((unused)) static int ombr2_fixed(ombr_span *sp, unsigned *ns, const void *p, uint64_t nb, uint64_t es,\n int w)\n{\n uintptr_t lo = (uintptr_t)p;\n if (!p || lo % es != 0u || nb > UINTPTR_MAX - lo)\n return 1;\n sp[*ns].lo = lo;\n sp[*ns].hi = lo + nb;\n sp[*ns].w = w;\n (*ns)++;\n return 0;\n}\n/* w: 0 view, 1 edit or the result, 2 out; refused overlaps as in output c. */\nstatic int ombr2_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 == 1 || sp[j].w == 1 || (sp[i].w == 2 && sp[j].w == 2))\n && sp[i].lo < sp[j].hi && sp[j].lo < sp[i].hi)\n return 1;\n return 0;\n}\n/* Byte transport only: volatile stores prevent libc idiom substitution. The copy source is\n * this entry's private out frame, disjoint from caller buffers checked by the wrapper. */\nstatic void ombr2_zero_bytes(unsigned char *p, uint64_t n) {\n volatile unsigned char *d = p;\n for (uint64_t i = 0; i < n; i++) d[i] = 0;\n}\nstatic void ombr2_copy_bytes(void *p, const unsigned char *s, uint64_t n) {\n volatile unsigned char *d = p;\n for (uint64_t i = 0; i < n; i++) d[i] = s[i];\n}\n/* As ombr_run; each out buffer is carved from ob (the entry's frame, 16-byte aligned, each buffer rounded\n * up to 16 bytes), zero-filled, passed in its slot and copied to the caller only after status 0. */\nstatic uint32_t ombr2_run(const void *code, uint64_t *args, uint64_t *result, int has_value, const ombr2_out *o,\n unsigned no, unsigned char *ob)\n{\n unsigned char *buf[6] = {0};\n for (unsigned i = 0; i < no; i++) {\n ombr2_zero_bytes(ob, o[i].n);\n buf[i] = ob;\n args[o[i].slot] = (uint64_t)(uintptr_t)ob;\n ob += (o[i].n + 15u) / 16u * 16u;\n }\n uint64_t v = 0;\n uint32_t s = ombr_enter(code, args, &ombr_rt_table, &v, 0);\n if (s == OMEGA_OK) {\n for (unsigned i = 0; i < no; i++)\n ombr2_copy_bytes(o[i].dst, buf[i], o[i].n);\n *result = has_value ? v : 0u;\n }\n return s;\n}\n~ _Alignas(16) unsigned char ob[~];\n~, ou, no, ob);\n}\n~, ou, no, 0);\n}\n~) != OMEGA_OK)\n __builtin_trap();\n~/* Generated by omega-bridge src/native.om: the freestanding definitions of the C API declared in\n * omega_api_decl.h (output fapic), for the freestanding binding (output freest). Each calls\n * omega2_<name> and executes a trap instruction (__builtin_trap) on a nonzero status; it calls no C\n * library function. Regenerate it; do not edit it. */\n#include \"omega_api_decl.h\"\n"
1856fn ofq k ulong to void
1857c = ulong 0
1858for i ulong len fq
1859b = get fq i
1860if eq b 126
1861c = add c 1
1862else
1863if eq c k
1864ob b
1865end
1866end
1867end
1868end
1869note ofv / otn: fragment k of fv / tn.
1870fn ofvd k ulong to void
1871c = ulong 0
1872for i ulong len fvdev
1873b = get fvdev i
1874if eq b 126
1875c = add c 1
1876else
1877if eq c k
1878ob b
1879end
1880end
1881end
1882end
1883fn ofv k ulong to void
1884if and eq get xt 0 1 eq k 17
1885xbranch 1
1886ret
1887end
1888if eq get mdev 0 1
1889ofvd k
1890ret
1891end
1892c = ulong 0
1893for i ulong len fv
1894b = get fv i
1895if eq b 126
1896c = add c 1
1897else
1898if eq c k
1899ob b
1900end
1901end
1902end
1903end
1904fn otn k ulong to void
1905c = ulong 0
1906for i ulong len tn
1907b = get tn i
1908if eq b 126
1909c = add c 1
1910else
1911if eq c k
1912ob b
1913end
1914end
1915end
1916end
1917note ---- SHA-256 of the unit's source bytes (the identity's src) ----
1918note rr: a 32-bit value rotated right by n (1 .. 31).
1919fn rr x ulong n ulong to ulong
1920ret and or shr x n shl x sub 32 n 4294967295
1921end
1922fn sg0 a ulong to ulong
1923ret xor xor rr a 2 rr a 13 rr a 22
1924end
1925fn sg1 e ulong to ulong
1926ret xor xor rr e 6 rr e 11 rr e 25
1927end
1928fn chf e ulong f ulong g ulong to ulong
1929ret xor and e f and xor e 4294967295 g
1930end
1931fn mjf a ulong b ulong c ulong to ulong
1932ret xor xor and a b and a c and b c
1933end
1934note shkin: K and the initial hash value from shx.
1935fn shkin to void
1936for i ulong 72
1937v = ulong 0
1938for j ulong 8
1939c = ulong get shx add mul i 8 j
1940if lt c 58
1941v = or shl v 4 sub c 48
1942else
1943v = or shl v 4 sub c 87
1944end
1945end
1946put shk i v
1947end
1948for i ulong 8
1949put shh i get shk add 64 i
1950end
1951end
1952note shsch: the message schedule shw of the block in shb.
1953fn shsch to void
1954for i ulong 16
1955j = mul i 4
1956v = shl ulong get shb j 24
1957v = or v shl ulong get shb add j 1 16
1958v = or v shl ulong get shb add j 2 8
1959v = or v ulong get shb add j 3
1960put shw i v
1961end
1962for i ulong 48
1963t = add i 16
1964x = get shw sub t 15
1965y = get shw sub t 2
1966s0 = xor xor rr x 7 rr x 18 shr x 3
1967s1 = xor xor rr y 17 rr y 19 shr y 10
1968x = and add get shw sub t 16 s0 4294967295
1969put shw t and add add x get shw sub t 7 s1 4294967295
1970end
1971end
1972note shrnd: round i on the working words shv (a .. h): the words move down one place, e and a get
1973note the new values.
1974fn shrnd i ulong to void
1975e = get shv 4
1976t1 = add add get shv 7 sg1 e chf e get shv 5 get shv 6
1977t1 = and add add t1 get shk i get shw i 4294967295
1978a = get shv 0
1979t2 = and add sg0 a mjf a get shv 1 get shv 2 4294967295
1980for j ulong 7
1981k = sub 7 j
1982put shv k get shv sub k 1
1983end
1984put shv 4 and add get shv 4 t1 4294967295
1985put shv 0 and add t1 t2 4294967295
1986end
1987note shblk: one 64-byte block of shb into the hash value shh.
1988fn shblk to void
1989shsch
1990for i ulong 8
1991put shv i get shh i
1992end
1993for i ulong 64
1994shrnd i
1995end
1996for i ulong 8
1997put shh i and add get shh i get shv i 4294967295
1998end
1999end
2000note shpad: byte k of the padded message inb[p .. p+n): the bytes, 0x80, zeros, then from lb the bit
2001note length, big endian.
2002fn shpad p ulong n ulong k ulong lb ulong to ulong
2003if lt k n
2004ret ulong get inb add p k
2005end
2006if eq k n
2007ret 128
2008end
2009if ge k lb
2010ret and shr mul n 8 mul sub 7 sub k lb 8 255
2011end
2012ret 0
2013end
2014note shsrc: the SHA-256 of inb[p .. q) into shh.
2015fn shsrc p ulong q ulong to void
2016shkin
2017n = sub q p
2018nb = div add n 72 64
2019lb = sub mul nb 64 8
2020for bk ulong nb
2021for i ulong 64
2022put shb i uchar shpad p n add mul bk 64 i lb
2023end
2024shblk
2025end
2026end
2027note ohx8: shh as 64 lowercase hex digits.
2028fn ohx8 to void
2029for i ulong 8
2030w = get shh i
2031for j ulong 8
2032ob get hx and shr w sub 28 mul j 4 15
2033end
2034end
2035end
2036note ---- reading OM-2 units ----
2037note tvis: 1 when token k is the OM-2 word kv[o .. o+n-1], else 0.
2038fn tvis k ulong o ulong n ulong to ulong
2039s = get tks k
2040e = get tke k
2041if ne sub e s n
2042ret 0
2043end
2044for j ulong n
2045if ne get inb add s j get kv add o j
2046ret 0
2047end
2048end
2049ret 1
2050end
2051note atkv: 1 when the bytes at i (below q) are kv[o .. o+n-1], else 0.
2052fn atkv i ulong q ulong o ulong n ulong to ulong
2053if gt add i n q
2054ret 0
2055end
2056for j ulong n
2057if ne get inb add i j get kv add o j
2058ret 0
2059end
2060end
2061ret 1
2062end
2063note atsp: 1 when the byte at i (below q) is a space, else 0.
2064fn atsp i ulong q ulong to ulong
2065if ge i q
2066ret 0
2067end
2068if ne get inb i 32
2069ret 0
2070end
2071ret 1
2072end
2073note lend: where the line starting at i ends (its newline, or q).
2074fn lend i ulong q ulong to ulong
2075e = i
2076while and lt e q ne get inb e 10
2077e = add e 1
2078end
2079ret e
2080end
2081note isexp / islay / isbrn: the line at i starts with "export fn " / "layout " / "note bridge ".
2082fn isexp i ulong q ulong to ulong
2083if and eq atkv i q 0 6 1 eq atsp add i 6 q 1
2084if and eq atkv add i 7 q 6 2 1 eq atsp add i 9 q 1
2085ret 1
2086end
2087end
2088ret 0
2089end
2090fn islay i ulong q ulong to ulong
2091if and eq atkv i q 36 6 1 eq atsp add i 6 q 1
2092ret 1
2093end
2094ret 0
2095end
2096fn isbrn i ulong q ulong to ulong
2097if and eq atkv i q 50 4 1 eq atsp add i 4 q 1
2098if and eq atkv add i 5 q 54 6 1 eq atsp add i 11 q 1
2099ret 1
2100end
2101end
2102ret 0
2103end
2104note prof: 1 when the unit p .. q-1 is OM-2: its first line that is not a note (a line whose first word
2105note is note) is exactly om2; else 0. A first such line that starts with the word om2 but is not exactly
2106note om2 (om2 diag, om2 fmt) is refused: that unit is not compiled to code.
2107fn prof p ulong q ulong to ulong
2108i = p
2109while lt i q
2110e = lend i q
2111s = i
2112while and lt s e eq get inb s 32
2113s = add s 1
2114end
2115nt = ulong 0
2116if eq atkv s e 50 4 1
2117if or eq add s 4 e eq atsp add s 4 e 1
2118nt = 1
2119end
2120end
2121if eq nt 0
2122if and eq s i eq sub e i 3
2123if eq atkv i e 11 3 1
2124ret 1
2125end
2126end
2127if eq atkv s e 11 3 1
2128if or eq add s 3 e eq atsp add s 3 e 1
2129trap 10
2130end
2131end
2132ret 0
2133end
2134i = add e 1
2135end
2136ret 0
2137end
2138note ismain: 1 when bytes s .. e-1 are main.
2139fn ismain s ulong e ulong to ulong
2140if ne sub e s 4
2141ret 0
2142end
2143for j ulong 4
2144if ne get inb add s j get kw add 28 j
2145ret 0
2146end
2147end
2148ret 1
2149end
2150note bname: refuse a bound name that is not a C identifier, is main, or repeats an earlier bound name
2151note (v1 unit names and v2 exports share one table: their symbols and wrappers must not collide).
2152fn bname s ulong e ulong to void
2153if eq cidr s e 0
2154trap 10
2155end
2156if eq ismain s e 1
2157trap 10
2158end
2159n = get st 4
2160if ge n len us
2161trap 10
2162end
2163for j ulong n
2164if eq samenm s e get us j get ue j 1
2165trap 10
2166end
2167end
2168put us n s
2169put ue n e
2170put st 4 add n 1
2171end
2172note tyix: token k as an element type: 0 uchar, 1 ushort, 2 uint, 3 ulong, 4 byte, 5 short, 6 int, 7 long;
2173note 8 when it is none of them. Its size is 1 << (type and 3).
2174fn tyix k ulong to ulong
2175if eq tkis k 19 5 1
2176ret 0
2177end
2178if eq tvis k 14 6 1
2179ret 1
2180end
2181if eq tvis k 20 4 1
2182ret 2
2183end
2184if eq tkis k 10 5 1
2185ret 3
2186end
2187if eq tvis k 24 4 1
2188ret 4
2189end
2190if eq tvis k 28 5 1
2191ret 5
2192end
2193if eq tvis k 33 3 1
2194ret 6
2195end
2196if eq tkis k 15 4 1
2197ret 7
2198end
2199ret 8
2200end
2201fn tsize t ulong to ulong
2202ret shl 1 and t 3
2203end
2204note lyfind: the index of the unit's layout named by token k (s2 10 when there is none).
2205fn lyfind k ulong to ulong
2206n = get s2 10
2207for j ulong n
2208if eq samenm get tks k get tke k get lys j get lye j 1
2209ret j
2210end
2211end
2212ret n
2213end
2214note lays: the unit's layouts (`layout NAME SIZE` lines) and their C names (`note bridge c-type L CNAME`,
2215note else L); a `note bridge` line that is neither c-type nor c-define is refused.
2216fn lays p ulong q ulong to void
2217put s2 10 0
2218i = p
2219while lt i q
2220e = lend i q
2221if eq islay i q 1
2222if ne tokln i q 3
2223trap 10
2224end
2225n = get s2 10
2226if ge n 32
2227trap 10
2228end
2229if eq cidr get tks 1 get tke 1 0
2230trap 10
2231end
2232if lt lyfind 1 n
2233trap 10
2234end
2235z = tknum 2
2236if gt z 4096
2237trap 10
2238end
2239put lys n get tks 1
2240put lye n get tke 1
2241put lcs n get tks 1
2242put lce n get tke 1
2243put lyz n z
2244put lyl n i
2245put s2 10 add n 1
2246end
2247i = add e 1
2248end
2249i = p
2250while lt i q
2251e = lend i q
2252if eq isbrn i q 1
2253if ne tokln i q 5
2254trap 10
2255end
2256if eq cidr get tks 3 get tke 3 0
2257trap 10
2258end
2259if eq tvis 2 60 6 1
2260li = lyfind 3
2261if ge li get s2 10
2262trap 10
2263end
2264if eq cidr get tks 4 get tke 4 0
2265trap 10
2266end
2267put lcs li get tks 4
2268put lce li get tke 4
2269else
2270if eq tvis 2 66 8 0
2271trap 10
2272end
2273x = tknum 4
2274end
2275end
2276i = add e 1
2277end
2278end
2279note exps: the starts of the unit's `export fn` lines, in source order, into exl; their count into s2 4.
2280fn exps p ulong q ulong to ulong
2281n = ulong 0
2282ls = p
2283i = p
2284while lt i q
2285if eq i ls
2286if eq isexp i q 1
2287if ge n 32
2288trap 10
2289end
2290put exl n i
2291n = add n 1
2292end
2293end
2294if eq get inb i 10
2295ls = add i 1
2296end
2297i = add i 1
2298end
2299put s2 4 n
2300ret n
2301end
2302note trail: the line `omega-unit NAME OMC` at i; the unit name must be new. Returns where the next unit
2303note starts.
2304fn trail i ulong to ulong
2305t = get st 0
2306if ge i t
2307trap 10
2308end
2309if ne tokln i t 3
2310trap 10
2311end
2312e = get st 7
2313if ge e t
2314trap 10
2315end
2316if eq tvis 0 86 10 0
2317trap 10
2318end
2319s = get tks 1
2320x = get tke 1
2321if eq cidr s x 0
2322trap 10
2323end
2324n = get s2 11
2325if ge n len uns
2326trap 10
2327end
2328for j ulong n
2329if eq samenm s x get uns j get une j 1
2330trap 10
2331end
2332end
2333put uns n s
2334put une n x
2335put s2 11 add n 1
2336put s2 1 s
2337put s2 2 x
2338h = get tks 2
2339if ne sub get tke 2 h 64
2340trap 10
2341end
2342for j ulong 64
2343c = get inb add h j
2344if and or lt c 48 gt c 57 or lt c 97 gt c 102
2345trap 10
2346end
2347end
2348put s2 3 h
2349ret add e 1
2350end
2351note pparam2: record the OM-2 parameter at token k as parameter st 2; return the token after it.
2352fn pparam2 k ulong nt ulong to ulong
2353np = get st 2
2354if ge np 6
2355trap 10
2356end
2357if ge add k 1 nt
2358trap 10
2359end
2360put ps np get tks k
2361put pe np get tke k
2362pname get tks k get tke k
2363put st 2 add np 1
2364sl = get s2 5
2365put qs np sl
2366put qm np 0
2367ty = add k 1
2368x = tyix ty
2369if or eq x 3 eq x 7
2370put qk np 1
2371put qt np x
2372put s2 5 add sl 1
2373ret add k 2
2374end
2375if ge add k 3 nt
2376trap 10
2377end
2378if eq tvis ty 8 3 1
2379t = tyix add k 2
2380if eq t 8
2381trap 10
2382end
2383n = tknum add k 3
2384if or eq n 0 gt n 65536
2385trap 10
2386end
2387ob2 = add get s2 9 mul n tsize t
2388if gt ob2 65536
2389trap 10
2390end
2391put s2 9 ob2
2392put qk np 5
2393put qm np 1
2394put qt np t
2395put qn np n
2396put s2 5 add sl 1
2397ret add k 4
2398end
2399m = ulong 2
2400if eq tkis ty 2 4 1
2401m = 1
2402end
2403if eq tkis ty 6 4 1
2404m = 0
2405end
2406if eq m 2
2407trap 10
2408end
2409put qm np m
2410t = tyix add k 2
2411if eq t 8
2412li = lyfind add k 2
2413if ge li get s2 10
2414trap 10
2415end
2416put qk np 4
2417put ql np li
2418put qn np get lyz li
2419put s2 5 add sl 1
2420ret add k 3
2421end
2422if ge add k 3 nt
2423trap 10
2424end
2425put qt np t
2426if eq tvis add k 3 83 1 1
2427put qk np 3
2428put s2 5 add sl 2
2429ret add k 4
2430end
2431n = tknum add k 3
2432if or eq n 0 gt n 16777216
2433trap 10
2434end
2435put qk np 2
2436put qn np n
2437put s2 5 add sl 1
2438ret add k 4
2439end
2440note parse2: the OM-2 signature line at fl (`export fn NAME ...` or `fn NAME ...`): its parameters into
2441note ps pe q*, their count into st 2, slots s2 5, has a value s2 8, result type s2 18, name s2 16 17.
2442fn parse2 fl ulong q ulong to void
2443put st 6 fl
2444nt = tokln fl q
2445ni = ulong 1
2446if eq tvis 0 0 6 1
2447if eq tvis 1 6 2 0
2448trap 10
2449end
2450ni = 2
2451end
2452if lt nt add ni 3
2453trap 10
2454end
2455bname get tks ni get tke ni
2456put s2 16 get tks ni
2457put s2 17 get tke ni
2458put st 2 0
2459put s2 5 0
2460put s2 9 0
2461k = add ni 1
2462while eq tkis k 0 2 0
2463k = pparam2 k nt
2464if ge k nt
2465trap 10
2466end
2467end
2468if ne add k 2 nt
2469trap 10
2470end
2471if gt get s2 5 6
2472trap 10
2473end
2474r = add k 1
2475put s2 8 1
2476put s2 18 3
2477if eq tkis r 15 4 1
2478put s2 18 7
2479else
2480if eq tkis r 24 4 1
2481put s2 8 0
2482put s2 18 9
2483else
2484if eq tkis r 10 5 0
2485trap 10
2486end
2487end
2488end
2489end
2490note ---- writing OM-2 units ----
2491fn oun to void
2492orng get s2 1 get s2 2
2493end
2494fn oen to void
2495orng get s2 16 get s2 17
2496end
2497note oid: the identity: omega-ident 1 unit=NAME abi=2 profile=OM-2 src=SHA256 omc=SHA256.
2498fn oid to void
2499ofv 11
2500oun
2501ofv 12
2502ohx8
2503ofv 13
2504h = get s2 3
2505orng h add h 64
2506ohost
2507end
2508note ohost: " host=W,W" naming the host words the unit uses (hm) in hw order; nothing when it uses none.
2509fn ohost to void
2510c = ulong 0
2511for k ulong len hm
2512c = add c ulong get hm k
2513end
2514if eq c 0
2515ret
2516end
2517for j ulong len hpre
2518ob get hpre j
2519end
2520c = ulong 0
2521k = ulong 0
2522i = ulong 1
2523while lt k 83
2524j = i
2525while ne get hw j 124
2526j = add j 1
2527end
2528if ne get hm k 0
2529if gt c 0
2530ob uchar 44
2531end
2532for x ulong sub j i
2533ob get hw add i x
2534end
2535c = add c 1
2536end
2537k = add k 1
2538i = add j 1
2539end
2540end
2541fn esz j ulong to ulong
2542ret tsize get qt j
2543end
2544note hasln: 1 when parameter j carries a count (a sized or unsized array).
2545fn hasln j ulong to ulong
2546if or eq get qk j 2 eq get qk j 3
2547ret 1
2548end
2549ret 0
2550end
2551note cpar: the C parameter list (the library and the header).
2552fn cpar to void
2553for j ulong get st 2
2554x = get qk j
2555if eq x 1
2556ofv 25
2557opn j
2558else
2559ofv 26
2560if eq get qm j 0
2561ofv 27
2562end
2563if eq x 4
2564otn 0
2565else
2566otn get qt j
2567end
2568ofv 28
2569opn j
2570if eq hasln j 1
2571ofv 29
2572opn j
2573ofv 30
2574end
2575end
2576end
2577end
2578note cslot: " a[S] = (uint64_t)(uintptr_t)o_N;"
2579fn cslot j ulong to void
2580ofv 36
2581odec get qs j
2582ofv 37
2583opn j
2584ofv 38
2585end
2586note cbody: the checks of each parameter, in order, and its slot words.
2587fn cbody to void
2588for j ulong get st 2
2589x = get qk j
2590if eq x 1
2591ofv 36
2592odec get qs j
2593ofv 39
2594opn j
2595ofv 38
2596end
2597if eq x 2
2598ofv 31
2599opn j
2600ofv 32
2601opn j
2602ofv 33
2603odec get qn j
2604ofv 34
2605odec esz j
2606ofv 34
2607odec get qm j
2608ofv 35
2609cslot j
2610end
2611if eq x 3
2612ofv 40
2613opn j
2614ofv 32
2615opn j
2616ofv 33
2617odec esz j
2618ofv 34
2619odec get qm j
2620ofv 35
2621cslot j
2622ofv 36
2623odec add get qs j 1
2624ofv 39
2625opn j
2626ofv 42
2627end
2628if eq x 4
2629ofv 41
2630opn j
2631ofv 26
2632odec get qn j
2633ofv 34
2634ofv 47
2635odec get qm j
2636ofv 35
2637cslot j
2638end
2639if eq x 5
2640ofv 41
2641opn j
2642ofv 26
2643odec mul get qn j esz j
2644ofv 34
2645odec esz j
2646ofv 34
2647ofv 43
2648odec get qs j
2649ofv 44
2650odec mul get qn j esz j
2651ofv 45
2652opn j
2653ofv 46
2654end
2655end
2656end
2657note ecode2: the unit's code, embedded unchanged, at ombr2_code_<unit>.
2658fn ecode2 to void
2659if ne get xt 0 1
2660if eq hassvc get s2 13 get s2 14 1
2661ofr 28
2662oun
2663ofr 29
2664end
2665end
2666ofv 1
2667oun
2668ofv 2
2669oun
2670ofv 3
2671oun
2672c0 = get s2 13
2673if eq get xt 0 1
2674xcode c0 get s2 14
2675else
2676ofv 4
2677for j ulong get s2 14
2678if gt j 0
2679ob uchar 44
2680end
2681ohex get inb add c0 j
2682end
2683end
2684ofv 5
2685ofv 6
2686oun
2687ofv 7
2688oid
2689ofv 8
2690oun
2691ofv 9
2692oun
2693ofv 10
2694end
2695note cfsend: the end of an entry in output freest: its out buffers in the entry's own frame (each rounded up
2696note to 16 bytes, as ombr2_run carves them), then the call of ombr2_run with that buffer.
2697fn cfsend to void
2698nb = ulong 0
2699for j ulong get st 2
2700if eq get qk j 5
2701nb = add nb mul div add mul get qn j tsize get qt j 15 16 16
2702end
2703end
2704if gt nb 0
2705ofq 1
2706odec nb
2707ofq 2
2708end
2709ofv 48
2710oen
2711ofv 49
2712odec get s2 8
2713if gt nb 0
2714ofq 3
2715else
2716ofq 4
2717end
2718end
2719fn oc k ulong to void
2720c = ulong 0
2721for i ulong len ctxparts
2722b = get ctxparts i
2723if eq b 126
2724c = add c 1
2725else
2726if eq c k
2727ob b
2728end
2729end
2730end
2731end
2732fn ccore ctx ulong to void
2733ofv 21
2734osig
2735ofv 22
2736if eq ctx 1
2737oc 0
2738end
2739oen
2740if eq ctx 1
2741oc 1
2742else
2743ofv 23
2744end
2745cpar
2746ofv 24
2747cbody
2748if eq get st 5 10
2749cfsend
2750else
2751if eq ctx 1
2752oc 2
2753else
2754ofv 48
2755end
2756oen
2757ofv 49
2758odec get s2 8
2759ofv 50
2760end
2761end
2762note cone: one entry in C: its stub (bti c, a branch to word k of the code) and its function.
2763fn cone to void
2764ofv 14
2765oen
2766ofv 15
2767oen
2768ofv 16
2769oen
2770ofv 17
2771oun
2772ofv 18
2773stride = ulong 4
2774if eq get xt 0 1
2775stride = 6
2776end
2777odec mul get s2 12 stride
2778ofv 19
2779oen
2780ofv 20
2781ccore 0
2782if eq get mdev 0 1
2783ccore 1
2784end
2785end
2786fn hone to void
2787ofv 21
2788osig
2789ofv 55
2790oen
2791ofv 23
2792cpar
2793ofv 56
2794if eq get mdev 0 1
2795ofv 21
2796osig
2797ofv 55
2798oc 0
2799oen
2800oc 1
2801cpar
2802ofv 56
2803end
2804end
2805fn hid to void
2806ofv 51
2807oun
2808ofv 52
2809oid
2810ofv 53
2811oun
2812ofv 54
2813end
2814note pyty: the ctypes type of the pointer of parameter j (a view of uchar takes bytes, as in v1).
2815fn pyty j ulong to void
2816x = get qk j
2817t = get qt j
2818if eq x 4
2819t = 0
2820end
2821if and eq get qm j 0 eq t 0
2822ofv 64
2823else
2824ofv 65
2825otn add 8 t
2826ofv 66
2827end
2828end
2829fn pyone to void
2830ofv 57
2831osig
2832ofv 58
2833oen
2834ofv 59
2835oen
2836ofv 58
2837oen
2838ofv 60
2839for j ulong get st 2
2840if eq get qk j 1
2841ofv 63
2842else
2843pyty j
2844if eq hasln j 1
2845ofv 63
2846end
2847end
2848end
2849ofv 61
2850oen
2851ofv 62
2852end
2853fn pyid to void
2854ofv 67
2855oun
2856ofv 155
2857ofv 67
2858oun
2859ofv 75
2860ofv 67
2861oun
2862ofv 68
2863oun
2864ofv 69
2865oun
2866ofv 70
2867oid
2868ofv 71
2869oun
2870ofv 72
2871oun
2872ofv 73
2873oun
2874ofv 74
2875end
2876note rzpar: the rust (z 0) or zig (z 1) parameter list.
2877fn rzpar z ulong to void
2878for j ulong get st 2
2879x = get qk j
2880ofv 80
2881opn j
2882if eq x 1
2883ofv 81
2884else
2885if eq get qm j 0
2886if eq z 0
2887ofv 82
2888else
2889ofv 93
2890end
2891else
2892if eq z 0
2893ofv 83
2894else
2895ofv 94
2896end
2897end
2898t = get qt j
2899if eq x 4
2900t = 0
2901end
2902otn add 16 t
2903if eq hasln j 1
2904ofv 80
2905opn j
2906ofv 84
2907end
2908end
2909end
2910end
2911fn rsone to void
2912ofv 76
2913osig
2914ofv 77
2915oen
2916ofv 78
2917rzpar 0
2918ofv 79
2919end
2920fn rsid to void
2921ofv 85
2922oun
2923ofv 86
2924oun
2925ofv 87
2926oid
2927ofv 88
2928end
2929fn zgone to void
2930ofv 89
2931osig
2932ofv 90
2933oen
2934ofv 91
2935rzpar 1
2936ofv 92
2937end
2938fn zgid to void
2939ofv 95
2940oun
2941ofv 96
2942oid
2943ofv 97
2944oun
2945ofv 98
2946end
2947fn goone to void
2948ofv 99
2949osig
2950ofv 100
2951oen
2952ofv 101
2953for j ulong get st 2
2954ofv 80
2955opn j
2956if eq get qk j 1
2957ofv 105
2958else
2959ofv 106
2960if eq hasln j 1
2961ofv 80
2962opn j
2963ofv 107
2964end
2965end
2966end
2967ofv 102
2968oen
2969ofv 103
2970for j ulong get st 2
2971if eq get qk j 1
2972ofv 108
2973opn j
2974ofv 109
2975else
2976ofv 110
2977t = get qt j
2978if eq get qk j 4
2979t = 0
2980end
2981otn add 24 t
2982ofv 111
2983opn j
2984ofv 109
2985if eq hasln j 1
2986ofv 108
2987opn j
2988ofv 112
2989end
2990end
2991end
2992ofv 104
2993end
2994fn goid to void
2995ofv 113
2996oun
2997ofv 114
2998oid
2999ofv 115
3000oun
3001ofv 116
3002oun
3003ofv 117
3004end
3005note ---- the api header (output api): layouts as C structs, exports as C functions ----
3006note adefs: `note bridge c-define NAME VALUE` lines as #define NAME VALUE.
3007fn adefs p ulong q ulong to void
3008i = p
3009while lt i q
3010e = lend i q
3011if eq isbrn i q 1
3012x = tokln i q
3013if eq tvis 2 66 8 1
3014ofv 120
3015oname 3
3016ofv 121
3017oname 4
3018ofv 75
3019end
3020end
3021i = add e 1
3022end
3023end
3024note afield: the field line just tokenized (nt tokens) into field slot n of a layout of z bytes:
3025note field F T [COUNT] at OFF little|big; COUNT omitted is a scalar (fdc 0), given is an array.
3026fn afield n ulong z ulong nt ulong to void
3027o = ulong 3
3028put fdc n 0
3029if eq nt 7
3030c = tknum 3
3031if or eq c 0 gt c 4096
3032trap 10
3033end
3034put fdc n c
3035o = 4
3036else
3037if ne nt 6
3038trap 10
3039end
3040end
3041if eq cidr get tks 1 get tke 1 0
3042trap 10
3043end
3044t = tyix 2
3045if eq t 8
3046trap 10
3047end
3048if eq tvis o 84 2 0
3049trap 10
3050end
3051off = tknum add o 1
3052put fdb n 2
3053if eq tvis add o 2 74 6 1
3054put fdb n 0
3055end
3056if eq tvis add o 2 80 3 1
3057put fdb n 1
3058end
3059if eq get fdb n 2
3060trap 10
3061end
3062c = get fdc n
3063if eq c 0
3064c = 1
3065end
3066if gt add off mul c tsize t z
3067trap 10
3068end
3069put fds n get tks 1
3070put fde n get tke 1
3071put fdt n t
3072put fdo n off
3073put fdu n 0
3074end
3075note afpad: an explicit padding member for bytes a .. b-1.
3076fn afpad a ulong b ulong to void
3077if lt a b
3078ofv 135
3079odec a
3080ofv 133
3081odec sub b a
3082ofv 134
3083end
3084end
3085note afmem: the member of field n: its type and count, or bytes when big-endian or misaligned.
3086fn afmem n ulong to void
3087t = get fdt n
3088c = get fdc n
3089z = tsize t
3090ofv 132
3091if or eq get fdb n 1 ne rem get fdo n z 0
3092otn 0
3093ofv 121
3094orng get fds n get fde n
3095ofv 133
3096if eq c 0
3097odec z
3098else
3099odec mul c z
3100end
3101ofv 134
3102else
3103otn t
3104ofv 121
3105orng get fds n get fde n
3106if eq c 0
3107ofv 38
3108else
3109ofv 133
3110odec c
3111ofv 134
3112end
3113end
3114end
3115note alrd: the field lines of layout li (up to its `end` line) into the field slots; returns their count.
3116fn alrd li ulong to ulong
3117z = get lyz li
3118q = get s2 7
3119i = add lend get lyl li q 1
3120n = ulong 0
3121go = ulong 1
3122while eq go 1
3123if ge i q
3124trap 10
3125end
3126e = lend i q
3127x = tokln i q
3128if eq x 0
3129trap 10
3130end
3131if eq tvis 0 47 3 1
3132if ne x 1
3133trap 10
3134end
3135go = 0
3136else
3137if eq tvis 0 42 5 0
3138trap 10
3139end
3140if ge n 256
3141trap 10
3142end
3143afield n z x
3144n = add n 1
3145end
3146i = add e 1
3147end
3148ret n
3149end
3150note fdmin: the unused field (of n) with the lowest offset.
3151fn fdmin n ulong to ulong
3152b = ulong 256
3153for j ulong n
3154if eq get fdu j 0
3155if eq b 256
3156b = j
3157else
3158if lt get fdo j get fdo b
3159b = j
3160end
3161end
3162end
3163end
3164put fdu b 1
3165ret b
3166end
3167note alay: layout li as a C struct with its members at their offsets (fields may be listed in any order;
3168note an overlap is refused), then its size and every field offset checked by the C compiler.
3169fn alay li ulong to void
3170n = alrd li
3171ofv 122
3172cur = ulong 0
3173for r ulong n
3174b = fdmin n
3175if lt get fdo b cur
3176trap 10
3177end
3178afpad cur get fdo b
3179afmem b
3180c = get fdc b
3181if eq c 0
3182c = 1
3183end
3184cur = add get fdo b mul c tsize get fdt b
3185end
3186afpad cur get lyz li
3187ofv 123
3188orng get lcs li get lce li
3189ofv 124
3190orng get lcs li get lce li
3191ofv 125
3192odec get lyz li
3193ofv 126
3194orng get lys li get lye li
3195ofv 127
3196for j ulong n
3197ofv 128
3198orng get lcs li get lce li
3199ofv 129
3200orng get fds j get fde j
3201ofv 125
3202odec get fdo j
3203ofv 126
3204orng get lys li get lye li
3205ofv 130
3206orng get fds j get fde j
3207ofv 131
3208end
3209end
3210note apone: one export as a C function that aborts on a nonzero status: static inline in output api;
3211note in apih its prototype only; in apic its out-of-line definition.
3212fn apone to void
3213ofv 21
3214osig
3215if eq get st 5 7
3216ofv 136
3217else
3218ofa 0
3219end
3220if eq get s2 8 0
3221ofv 137
3222else
3223if eq get s2 18 7
3224ofv 139
3225else
3226ofv 138
3227end
3228end
3229ofv 121
3230oen
3231ofv 140
3232if eq get st 2 0
3233ofv 137
3234end
3235for j ulong get st 2
3236if gt j 0
3237ofv 147
3238end
3239x = get qk j
3240if eq x 1
3241otn get qt j
3242ofv 148
3243opn j
3244else
3245if and eq get qm j 0 ne x 5
3246ofv 27
3247end
3248if eq x 4
3249orng get lcs get ql j get lce get ql j
3250else
3251otn get qt j
3252end
3253if or eq x 3 eq x 4
3254ofv 149
3255opn j
3256if eq x 3
3257ofv 150
3258opn j
3259ofv 30
3260end
3261else
3262ofv 148
3263opn j
3264ofv 133
3265odec get qn j
3266ofv 154
3267end
3268end
3269end
3270if eq get st 5 8
3271ofa 1
3272ret
3273end
3274ofv 141
3275oen
3276ofv 142
3277for j ulong get st 2
3278x = get qk j
3279if eq x 1
3280ofv 153
3281opn j
3282end
3283if or eq x 2 eq x 3
3284ofv 32
3285opn j
3286if eq x 2
3287ofv 147
3288odec get qn j
3289else
3290ofv 32
3291opn j
3292ofv 30
3293end
3294end
3295if eq x 4
3296if eq get qm j 0
3297ofv 152
3298else
3299ofv 151
3300end
3301opn j
3302end
3303if eq x 5
3304ofv 32
3305opn j
3306end
3307end
3308if eq get st 5 12
3309ofq 5
3310else
3311ofv 143
3312end
3313if eq get s2 8 0
3314ofv 144
3315else
3316if eq get s2 18 7
3317ofv 146
3318else
3319ofv 145
3320end
3321end
3322end
3323note eone2: the bound entry just parsed, in the output st 5.
3324fn eone2 to void
3325if eq get mdev 0 1
3326s = get s2 16
3327if ge sub get s2 17 s 4
3328if eq get inb s 99
3329if eq get inb add s 1 116
3330if eq get inb add s 2 120
3331if eq get inb add s 3 95
3332trap 10
3333end
3334end
3335end
3336end
3337end
3338end
3339m = get st 5
3340if or eq m 1 eq m 10
3341cone
3342end
3343if eq m 2
3344hone
3345end
3346if eq m 3
3347pyone
3348end
3349if eq m 4
3350rsone
3351end
3352if eq m 5
3353zgone
3354end
3355if eq m 6
3356goone
3357end
3358if or and ge m 7 le m 9 eq m 12
3359apone
3360end
3361end
3362note unit2: accept the OM-2 unit p .. q-1 (q its 0 byte), emit it, return where the next unit starts.
3363fn unit2 p ulong q ulong to ulong
3364put s2 6 p
3365put s2 7 q
3366lays p q
3367cl = codelen q
3368c0 = add q 9
3369put s2 13 c0
3370put s2 14 cl
3371nx = trail add c0 cl
3372if eq get munit 0 1
3373h = get s2 3
3374for j ulong 64
3375b = get mappin j
3376if eq get xt 0 1
3377b = get xmappin j
3378end
3379if ne get inb add h j b
3380trap 10
3381end
3382end
3383end
3384shsrc p q
3385nex = exps p q
3386stride = ulong 4
3387if eq get xt 0 1
3388stride = 6
3389end
3390if gt mul nex stride cl
3391trap 10
3392end
3393m = get st 5
3394if or eq m 1 eq m 10
3395if eq get s2 0 0
3396if eq m 10
3397ofq 0
3398else
3399ofv 0
3400end
3401put s2 0 1
3402end
3403ecode2
3404end
3405if eq m 2
3406hid
3407end
3408if eq m 3
3409pyid
3410end
3411if eq m 5
3412zgid
3413end
3414if eq m 6
3415goid
3416end
3417if or eq m 7 eq m 8
3418adefs p q
3419for li ulong get s2 10
3420alay li
3421end
3422end
3423if eq nex 0
3424put s2 12 0
3425parse2 sigln p q q
3426eone2
3427else
3428for k ulong nex
3429put s2 12 k
3430parse2 get exl k q
3431eone2
3432end
3433end
3434if eq m 4
3435rsid
3436end
3437if eq m 11
3438oid
3439ob uchar 10
3440end
3441ret nx
3442end
3443note hsep: 1 when byte b separates words for omc (space, tab, carriage return, newline, ( or )).
3444fn hsep b uchar to ulong
3445if eq hblank b 1
3446ret 1
3447end
3448if or or eq b 10 eq b 40 eq b 41
3449ret 1
3450end
3451ret 0
3452end
3453note hblank: 1 when byte b is a blank for omc (space, tab, carriage return).
3454fn hblank b uchar to ulong
3455if or or eq b 32 eq b 9 eq b 13
3456ret 1
3457end
3458ret 0
3459end
3460note hwfind: k when bytes s .. x-1 are word k (from 1) of hw, else 0.
3461fn hwfind s ulong x ulong to ulong
3462n = sub x s
3463k = ulong 0
3464i = ulong 1
3465while lt i len hw
3466j = i
3467while ne get hw j 124
3468j = add j 1
3469end
3470k = add k 1
3471if eq sub j i n
3472if eq hweq s i n 1
3473ret k
3474end
3475end
3476i = add j 1
3477end
3478ret 0
3479end
3480fn hweq s ulong o ulong n ulong to ulong
3481for j ulong n
3482if ne get inb add s j get hw add o j
3483ret 0
3484end
3485end
3486ret 1
3487end
3488note hword: admit the nine file/identity words, clocks and device words only for hosted OM-2.
3489fn hword k ulong v2 ulong to void
3490if eq get hok 0 0
3491trap 10
3492end
3493ok = ulong 0
3494if or or le k 9 eq k 18 eq k 27
3495ok = 1
3496end
3497if or eq k 37 and ge k 66 le k 77
3498ok = 1
3499end
3500if or eq ok 0 eq v2 0
3501trap 10
3502end
3503put hm sub k 1 1
3504if or and ge k 67 le k 70 and ge k 73 le k 77
3505put mdev 0 1
3506put munit 0 1
3507end
3508end
3509note hline: every word of bytes s .. e-1 through hwfind and hword.
3510fn hline s ulong e ulong v2 ulong to void
3511i = s
3512while lt i e
3513if eq hsep get inb i 1
3514i = add i 1
3515else
3516x = i
3517while and lt x e eq hsep get inb x 0
3518x = add x 1
3519end
3520k = hwfind i x
3521if gt k 0
3522hword k v2
3523end
3524i = x
3525end
3526end
3527end
3528note hscan: the host words of the unit at p .. q-1, split as omc splits words. A line whose first
3529note blank-separated word is exactly note is skipped; a host word inside a text literal refuses the unit
3530note too (fail closed).
3531fn hscan p ulong q ulong v2 ulong to void
3532put munit 0 0
3533for k ulong len hm
3534put hm k 0
3535end
3536i = p
3537while lt i q
3538e = lend i q
3539s = i
3540while and lt s e eq hblank get inb s 1
3541s = add s 1
3542end
3543x = s
3544while and lt x e eq hblank get inb x 0
3545x = add x 1
3546end
3547nt = ulong 0
3548if eq sub x s 4
3549nt = atkv s e 50 4
3550end
3551if eq nt 0
3552hline s e v2
3553end
3554i = add e 1
3555end
3556end
3557note unit: accept the unit at p, emit it, return where the next unit starts.
3558fn unit p ulong to ulong
3559q = sepat p
3560if eq get st 5 10
3561put fo2 0 prof p q
3562fcheck p q
3563end
3564v = prof p q
3565hscan p q v
3566if eq v 1
3567ret unit2 p q
3568end
3569fl = sigln p q
3570put st 9 p
3571parse fl q
3572cl = codelen q
3573c0 = add q 9
3574emit c0 cl
3575ret add c0 cl
3576end
3577note slurp: all of stdin into inb; refuse input that fills the pool.
3578fn slurp to ulong
3579t = ulong 0
3580k = read 0 inb 0 len inb
3581while gt k 0
3582t = add t ulong k
3583k = long 0
3584if lt t len inb
3585k = read 0 inb t sub len inb t
3586end
3587end
3588if lt k 0
3589trap 11
3590end
3591if ge t len inb
3592trap 6
3593end
3594ret t
3595end
3596fn main to int
3597m = rmode
3598t = slurp
3599put st 0 t
3600put st 1 0
3601put st 4 0
3602put st 5 m
3603if or eq m 1 eq m 2
3604p = ulong 0
3605while lt p t
3606q = sepat p
3607v = prof p q
3608if eq get hok 0 1
3609hscan p q prof p q
3610end
3611p = add add q 9 codelen q
3612if eq v 1
3613p = add lend p t 1
3614end
3615end
3616end
3617if eq m 10
3618fpre
3619end
3620if eq m 1
3621opre
3622else
3623if eq m 2
3624ofb 0
3625if eq get mdev 0 1
3626oc 3
3627end
3628end
3629if eq m 3
3630ofb 12
3631end
3632if eq m 4
3633ofb 22
3634end
3635if eq m 5
3636ofb 35
3637end
3638if eq m 6
3639ofb 47
3640end
3641if eq m 7
3642ofv 118
3643end
3644if eq m 8
3645ofa 2
3646end
3647if eq m 9
3648ofa 3
3649end
3650if eq m 12
3651ofq 6
3652end
3653end
3654p = ulong 0
3655nu = ulong 0
3656while lt p t
3657p = unit p
3658nu = add nu 1
3659end
3660if eq nu 0
3661trap 10
3662end
3663if eq m 2
3664ofb 11
3665end
3666if eq m 4
3667ofb 34
3668end
3669if or eq m 7 eq m 8
3670ofv 119
3671end
3672o = ulong 0
3673m = get st 1
3674e = m
3675if gt e len outb
3676e = len outb
3677end
3678while lt o e
3679w = write 1 outb o sub e o
3680if le w 0
3681ret 1
3682end
3683o = add o ulong w
3684end
3685o = ulong 0
3686e = sub m e
3687while lt o e
3688w = write 1 outc o sub e o
3689if le w 0
3690ret 1
3691end
3692o = add o ulong w
3693end
3694ret 0
3695end