Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/omega-bridge/blob/main/tests/seeded_views.py # omega-bridge Omega-first language interoperability: checked conversion backends and native language bindings Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `omega-bridge / tests / seeded_views.py` 334 lines ยท 13217 bytes Raw Blame History 1 #!/usr/bin/env python3 2 """Bit-exact seeded typed views/arrays, public float ABI and refusal/mutant controls. 3 4 Pinned source semantics: omega-mv `e5b32567` seed/omc/omc.om twid/tlog, 5 ldop/stop, fintr; OMEGESE.md 9.3/9.4 and Float values. 6 WolframLanguageEvaluator: {2^16,2^20,2^32-1} -> {65536,`1048576`,`4294967295`}. 7 Expected floating results come from executing the pinned compiler, never Python FP. 8 """ 9 import ctypes as C 10 import hashlib 11 import os 12 from pathlib import Path 13 import random 14 import re 15 import subprocess 16 import tempfile 17 18 ROOT = Path(__file__).resolve().parents[1] 19 PIN = "`2c82480596bbdc50cda9626866209ef662d5834f278f839ff4ce0250c495029f`" 20 OMC = Path(os.environ["OMEGA_SEEDED_OMC"]) 21 assert hashlib.sha256(OMC.read_bytes()).hexdigest() == PIN 22 23 24 def run(cmd, data=None, ok=0): 25 p = subprocess.run([str(x) for x in cmd], input=data, capture_output=True, timeout=300) 26 assert p.returncode == ok, (cmd, p.returncode, p.stderr.decode(errors="replace")) 27 return p.stdout 28 29 30 def convert(src, mode="c"): 31 with tempfile.TemporaryDirectory(prefix="omega-seeded-source-") as td: 32 path = Path(td) / "unit.om" 33 path.write_text(src) 34 return run([ROOT / "tools/bridge-seeded", mode, path]) 35 36 37 def cc(src, out, shared=False, compiler=None): 38 p = out.with_suffix(".c") 39 p.write_bytes(src) 40 flags = ["-shared", "-fPIC", "-DOMEGA_LIBRARY"] if shared else [] 41 run([compiler or os.environ.get("CC", "cc"), "-std=gnu11", "-O2", "-Wall", "-Wextra", "-Werror", *flags, p, "-o", out]) 42 43 44 def storage_codegen(generated, work): 45 """Use the emitted helpers, not hand-written stand-ins, for the GCC -O2 regression.""" 46 compiler = os.environ.get("CC", "cc") 47 if "aarch64" not in run([compiler, "-dumpmachine"]).decode(): 48 print("NOT_RUN fixed-width AArch64 codegen: CC is not an AArch64 compiler") 49 return 50 51 def violations(code, name): 52 lines = code.decode().splitlines() 53 helpers = [] 54 for prefix in ("static unsigned o_width(", "static __int128 o_value(", 55 "static uint64_t o_norm(", "static uint64_t o_get(", "static void o_put("): 56 selected = [line for line in lines if line.startswith(prefix)] 57 assert len(selected) == 1, prefix 58 helpers.extend(selected) 59 probes = [] 60 for typ in (3, 4, 5): 61 probes.append(f"uint64_t probe_load{typ}(const void*p,uint64_t n,uint64_t i)" 62 f"{{return o_get(p,n,i,{typ});}}") 63 probes.append(f"void probe_store{typ}(void*p,uint64_t n,uint64_t i,uint64_t v)" 64 f"{{o_put(p,n,i,v,{typ});}}") 65 source = work / (name + ".c") 66 assembly = source.with_suffix(".s") 67 source.write_text("#include \n" 68 "void o_trap(int) __attribute__((noreturn));\n" 69 + "\n".join(helpers + probes) + "\n") 70 run([compiler, "-O2", "-S", source, "-o", assembly]) 71 text = assembly.read_text() 72 bad = [] 73 for typ, bits in ((3, 16), (4, 32), (5, 64)): 74 for operation, instruction in (("load", "ldr"), ("store", "str")): 75 symbol = f"probe_{operation}{typ}" 76 match = re.search(rf"(?ms)^{symbol}:\n(.*?)^\s*\.size\s+{symbol},", text) 77 assert match, ("missing assembly function", symbol) 78 body = match.group(1) 79 opcode = instruction + ("h" if bits == 16 else "") 80 register = "x" if bits == 64 else "w" 81 if (len(re.findall(rf"(?m)^\s*{opcode}\s+{register}\d+,", body)) != 1 82 or re.search(r"\b(?:ldrb|strb)\b", body)): 83 bad.append(symbol) 84 return bad 85 86 assert not violations(generated, "storage-codegen"), "fixed-width accesses were not scalarized" 87 # Restore the reported byte loop: the assembly contract must reject it. 88 copies = (b"if(w==8)v=b[0];else if(w==16){uint16_t x;__builtin_memcpy(&x,b,2);v=x;}" 89 b"else if(w==32){uint32_t x;__builtin_memcpy(&x,b,4);v=x;}" 90 b"else if(w==64)__builtin_memcpy(&v,b,8);") 91 assert generated.count(copies) == 1 92 loop = b"for(unsigned j=0;j