Home /Modules/operating-system/SLCompatLoaderEmitter.sleela

SLCompatLoaderEmitter.sleela

lib/operating-system/os-creator/SLCompatLoaderEmitter.sleela · 207 line(s) · sleela

<< lib/operating-system/os-creator/SLCompatLoaderEmitter.sleela
   SLeeLa Standard Library Definition.
   Definition: Defines SLCompatLoaderEmitter — the OS Creator(TM) emitter for the
     foreign-executable COMPATIBILITY piece. Given an SLOSModel whose SLCompatSet
     declares guest families to support, it produces the C and C++ source for a
     "compat loader" userspace component that loads and unloads the OS components
     needed to run each guest's executables (the kernel binfmt_misc interpreter
     registration, the translation runtime — Wine/QEMU/WSL/Rosetta/Darling — and
     the guest shared libraries), and dispatches a foreign executable (or a
     library + executable) to the right runtime. It emits a compat.conf listing
     each supported "host runs guest" route and its components.
   
   This is the OS Creator realisation of lib/operating-system/SLCompatLayer + SLCompatSet: the
   design model names the components; this emitter writes the actual loader that
   registers/handles them in the generated OS. It mirrors SLOSLoaderEmitter.
   
   Family: operating-system. OS Creator(TM) piece emitter; consumes SLOSModel's SLCompatSet,
     fills SLOSPiece. >>
#sleela 2.5
material;
class SLCompatLoaderEmitter {
  < ---- Descriptive metadata (version 1.11 nested-friendly accessors) ---- >
  << Eleven catalogued attributes with getter/setter pairs, so a GUI, loader,
     or catalog can read and (where appropriate) override this record.
     Values are faithful to the subject's real origin where publicly known. >>
  String metaVersion = "1.0.0";
  int metaId = 11003;
  String metaAuthor = "Kiro Agent";
  String metaDate = "2026-10-16";
  String metaCopyright = "SLeeLa project";
  String metaTrademark = "none";
  String metaCorporation = "none";
  < metaOrigin: National | <nation> | International | Galactic >
  String metaOrigin = "International";
  String metaBirthDate = "2026-10-11";
  < metaScienceRating: maturity/robustness on a 1..10 scale >
  int metaScienceRating = 9;
  < metaNationalSchedule: a real, citable instrument or "none" >
  String metaNationalSchedule = "none";

  String getMetaVersion() { return metaVersion; }
  void setMetaVersion(String v) { metaVersion = v; }
  int getMetaId() { return metaId; }
  void setMetaId(int v) { metaId = v; }
  String getAuthor() { return metaAuthor; }
  void setAuthor(String v) { metaAuthor = v; }
  String getDate() { return metaDate; }
  void setDate(String v) { metaDate = v; }
  String getCopyright() { return metaCopyright; }
  void setCopyright(String v) { metaCopyright = v; }
  String getTrademark() { return metaTrademark; }
  void setTrademark(String v) { metaTrademark = v; }
  String getCorporation() { return metaCorporation; }
  void setCorporation(String v) { metaCorporation = v; }
  String getOrigin() { return metaOrigin; }
  void setOrigin(String v) { metaOrigin = v; }
  String getBirthDate() { return metaBirthDate; }
  void setBirthDate(String v) { metaBirthDate = v; }
  int getScienceRating() { return metaScienceRating; }
  void setScienceRating(int v) { metaScienceRating = v; }
  String getNationalSchedule() { return metaNationalSchedule; }
  void setNationalSchedule(String v) { metaNationalSchedule = v; }
  SLOSPiece emit(SLOSModel m) {
    SLOSPiece p = new SLOSPiece();
    p.configure("Compat Loader", "userspace/compat-loader", "compat_loader");

    SLCompatSet cs = m.compatSet();
    String host = cs.hostName();

    < ---- C surface: load/unload/handle the compat components ---- >
    String c = "/* compat_loader.c - foreign-executable compatibility loader, OS Creator(TM) for " + m.name() + " */\n";
    c = c + "/* Host OS: " + host + ". Loads binfmt_misc interpreters + translation runtimes\n";
    c = c + "   + guest libraries so this OS can run other families' executables. */\n";
    c = c + "#include <stdint.h>\n\n";
    c = c + "extern int binfmt_register(const char *rule);     /* FS-level interpreter */\n";
    c = c + "extern int binfmt_unregister(const char *name);\n";
    c = c + "extern int runtime_load(const char *runtime);     /* Wine/QEMU/WSL/Rosetta */\n";
    c = c + "extern int runtime_unload(const char *runtime);\n";
    c = c + "extern int libs_provide(const char *libset);      /* guest shared libraries */\n";
    c = c + "extern int run_foreign(const char *runtime, const char *path);\n\n";
    c = c + emitLoadFn(cs);
    c = c + emitUnloadFn(cs);
    c = c + emitHandleFn();
    p.setC(c);

    < ---- C++ surface: a CompatLoader class modeling the routes ---- >
    String cpp = "// compat_loader.cpp - compatibility loader (C++ surface) for " + m.name() + "\n";
    cpp = cpp + "#include <string>\n#include <vector>\nnamespace oscreator {\n";
    cpp = cpp + "struct Route { std::string guest, runtime, libs; bool binfmt; };\n";
    cpp = cpp + "class CompatLoader {\npublic:\n";
    cpp = cpp + "  std::string host = \"" + host + "\";\n";
    cpp = cpp + "  std::vector<Route> routes = {" + routeInits(cs) + "};\n";
    cpp = cpp + "  int loadAll() {\n";
    cpp = cpp + "    for (auto &r : routes) {\n";
    cpp = cpp + "      if (r.binfmt) registerInterpreter(r);\n";
    cpp = cpp + "      loadRuntime(r.runtime); provideLibs(r.libs);\n";
    cpp = cpp + "    }\n    return 0;\n  }\n";
    cpp = cpp + "  int unloadAll() { for (auto &r : routes) { unloadRuntime(r.runtime); if (r.binfmt) unregister(r.guest); } return 0; }\n";
    cpp = cpp + "  // run a guest executable (optionally with a guest library dir on the loader path)\n";
    cpp = cpp + "  int run(const Route &r, const std::string &exe, const std::string &libDir);\n";
    cpp = cpp + "private:\n";
    cpp = cpp + "  void registerInterpreter(const Route&); void unregister(const std::string&);\n";
    cpp = cpp + "  void loadRuntime(const std::string&); void unloadRuntime(const std::string&);\n";
    cpp = cpp + "  void provideLibs(const std::string&);\n";
    cpp = cpp + "};\n} // namespace oscreator\n";
    p.setCpp(cpp);

    < ---- config: the supported routes + their components ---- >
    String conf = "# compat.conf - generated by OS Creator(TM)\n";
    conf = conf + "host = " + host + "\n";
    conf = conf + "# format: route = <host> runs <guest> via <runtime> [+ <interpreter>] + <libs>\n";
    if (!cs.hasAny()) {
      conf = conf + "# (no foreign-exec support configured for this OS)\n";
    } else {
      conf = conf + routeConf(cs, "Windows");
      conf = conf + routeConf(cs, "Linux");
      conf = conf + routeConf(cs, "macOS");
    }
    p.setConfig("compat.conf", conf);

    p.setReadme("# Compat Loader piece\n\nLoads/unloads the OS components (`binfmt_misc` interpreters, "
      + "translation runtimes such as Wine/QEMU/WSL/Rosetta/Darling, and guest shared libraries) that let "
      + "this " + host + " OS run other families' executables. Routes are listed in `compat.conf`. "
      + "`compat_loader.c`/`.cpp` register each route at boot and dispatch a foreign executable "
      + "(or a library + executable) to its runtime.\n");
    return p;
  }

  < One load block per supported guest route. >
  String emitLoadFn(SLCompatSet cs) {
    String s = "/* Load every configured compat route. Called once at userspace bring-up. */\n";
    s = s + "int compat_load_all(void) {\n";
    s = s + loadLine(cs, "Windows");
    s = s + loadLine(cs, "Linux");
    s = s + loadLine(cs, "macOS");
    s = s + "    return 0;\n}\n\n";
    return s;
  }
  String loadLine(SLCompatSet cs, String guest) {
    if (!cs.supports(guest)) { return ""; }
    < native >
    if (cs.hostName() == guest) { return ""; }
    SLCompatLayer L = cs.layerFor(guest);
    String out = "    /* " + L.summary() + " */\n";
    if (L.fsInterpreter() != "") {
      out = out + "    binfmt_register(\"" + L.guestExt() + " -> " + L.runtime() + "\");\n";
    }
    out = out + "    runtime_load(\"" + L.runtime() + "\");\n";
    out = out + "    libs_provide(\"" + L.guestLibToken() + "\");\n";
    return out;
  }

  String emitUnloadFn(SLCompatSet cs) {
    String s = "/* Unload every configured compat route. */\n";
    s = s + "int compat_unload_all(void) {\n";
    s = s + unloadLine(cs, "Windows");
    s = s + unloadLine(cs, "Linux");
    s = s + unloadLine(cs, "macOS");
    s = s + "    return 0;\n}\n\n";
    return s;
  }
  String unloadLine(SLCompatSet cs, String guest) {
    if (!cs.supports(guest)) { return ""; }
    if (cs.hostName() == guest) { return ""; }
    SLCompatLayer L = cs.layerFor(guest);
    String out = "    runtime_unload(\"" + L.runtime() + "\");\n";
    if (L.fsInterpreter() != "") { out = out + "    binfmt_unregister(\"" + L.binfmtName() + "\");\n"; }
    return out;
  }

  String emitHandleFn() {
    String s = "/* Dispatch a foreign executable to its runtime (empty runtime => native/binfmt). */\n";
    s = s + "int compat_run(const char *runtime, const char *path) {\n";
    s = s + "    if (runtime == 0 || runtime[0] == 0) return run_foreign(\"\", path); /* binfmt routes it */\n";
    s = s + "    return run_foreign(runtime, path);\n";
    s = s + "}\n";
    return s;
  }

  < C++ route initialisers, one brace-init per supported guest. >
  String routeInits(SLCompatSet cs) {
    String out = "";
    out = addRoute(out, cs, "Windows");
    out = addRoute(out, cs, "Linux");
    out = addRoute(out, cs, "macOS");
    return out;
  }
  String addRoute(String acc, SLCompatSet cs, String guest) {
    if (!cs.supports(guest) || cs.hostName() == guest) { return acc; }
    SLCompatLayer L = cs.layerFor(guest);
    String bf = "false";
    if (L.fsInterpreter() != "") { bf = "true"; }
    String r = "{\"" + guest + "\", \"" + L.runtime() + "\", \"" + L.guestLibToken() + "\", " + bf + "}";
    if (acc == "") { return r; }
    return acc + ", " + r;
  }

  < One "route = ..." conf line per supported guest, built directly from the >
  < compat set (no string char-walking needed — the set is the source of truth). >
  String routeConf(SLCompatSet cs, String guest) {
    if (!cs.supports(guest) || cs.hostName() == guest) { return ""; }
    SLCompatLayer L = cs.layerFor(guest);
    String line = "route = " + L.summary() + "\n";
    return line;
  }
}