"""Write suyu's runtime pieces next to the lifted code, adapted to a64-kit's guest model (rt/a64.h). a64_suyu.h suyu's runtime header (FP scan/fixed-point helpers, FPX1 fast paths, prototypes) without its GuestContext, host memory bridge and layout pins: rt/a64.h defines the context and the memory accessors before including this, so the accessor prototypes here inherit their static inline. a64_suyu.c the out-of-line pieces the translated code calls: FPX1 exact bodies (recomp_fpe_*), the FP reciprocal estimates, SMULH/UMULH and the AES S-box. Both are GPL-2.0-or-later (from suyu/arm64_to_c.h). They are regenerated on every lift, so they always match the a64emit build that produced the code. """ import os import re import a64emit def _cut(text, start_marker, end_marker): a = text.find(start_marker) b = text.find(end_marker, a) if a < 0 or b < 0: raise SystemExit("a64_rtgen: suyu runtime layout changed (%r .. %r not found): update a64_rtgen.py" % (start_marker, end_marker)) return text[:a] + text[b:] def runtime_h(): h = a64emit.runtime_text(0) h = _cut(h, "typedef struct RecompHostMem {", "/* Where this module is actually loaded") h = h.replace("extern uint64_t g_module_base;\n", "") for proto in ("int recomp_save_init", "int recomp_save_write", "int recomp_save_read", "int recomp_save_delete", "int recomp_save_exists", "int recomp_load_segments"): h = re.sub(r"^%s\(.*\n" % re.escape(proto), "", h, flags=re.M) # (every context parameter A64_NOESC, rt/a64.h: these functions keep no pointer to it) h = re.sub(r"([(,]\s*)GuestContext\s*\*\s*(?=(?:\w+\s*)?[,)])", r"\1GuestContext *A64_NOESC ", h) return "/* Generated by a64-kit/lift/a64_rtgen.py from suyu/arm64_to_c.h (GPL-2.0-or-later). Do not edit. */\n" + h def _function(src, signature): """The text of one top-level C function, from its signature to its closing brace at column 0.""" a = src.find(signature) if a < 0: raise SystemExit("a64_rtgen: %r not found in suyu's runtime C" % signature) b = src.find("\n}\n", a) return src[a:b + 3] def runtime_c(): rc = a64emit.runtime_text(1) aes = rc[rc.find("static uint8_t recomp_gmul"):] aes = aes[:aes.find("\n}\n", aes.find("const uint8_t* recomp_aes_sbox")) + 3] parts = [ "/* Generated by a64-kit/lift/a64_rtgen.py from suyu/arm64_to_c.h (GPL-2.0-or-later). Do not edit. */", '#define A64_RT_IMPL /* (the out-of-line definitions: rt/a64.h inlines them elsewhere) */', '#include "a64.h"', aes, _function(rc, "uint64_t recomp_umulh(uint64_t a,uint64_t b){"), _function(rc, "uint64_t recomp_smulh(uint64_t a,uint64_t b){"), a64emit.runtime_text(2), a64emit.runtime_text(3), ] return "\n".join(parts) def write(out_dir): os.makedirs(out_dir, exist_ok=True) for name, text in (("a64_suyu.h", runtime_h()), ("a64_suyu.c", runtime_c())): path = os.path.join(out_dir, name) if not os.path.exists(path) or open(path, encoding="utf-8").read() != text: open(path, "w", encoding="utf-8", newline="\n").write(text) if __name__ == "__main__": import sys write(sys.argv[1])