Home /Modules/operating-system/SLCEmitter.sleela

SLCEmitter.sleela

lib/operating-system/SLCEmitter.sleela · 155 line(s) · sleela

<< lib/operating-system/SLCEmitter.sleela
   SLeeLa Standard Library Definition.
   Definition: Defines SLCEmitter — the C backend of the OS-construction family.
     Given a configured SLOSModel, it emits C source text that bootstraps the
     designed OS: a generated os_config.h header carrying the model's identity,
     shape, kernel command line and boot parameters as #defines, and an
     os_boot.c early-boot translation unit whose os_early_init() stages the
     bootloader, mounts the root filesystem, loads the built-in drivers, and
     starts PID 1 (the service manager the model names).
   
   This is the "compiling the Sleela produces C" half of the pipeline: the OS is
   described once in SLeeLa (the SL* model classes), and this emitter lowers that
   object model to portable C the native toolchain compiles toward the ISO. All
   output is ordinary String concatenation on OP_ADD; no new VM opcode is used.
   
   Family: operating-system. Consumes SLOSModel and its composed specs. >>
#sleela 2.5
material;
class SLCEmitter {
  < ---- 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 = 10871;
  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-10";
  < 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; }
  String intToStr(int v) { return "" + v; }

  < Emit the generated configuration header for the model. >
  String emitConfigHeader(SLOSModel m) {
    String h = "/* os_config.h - generated by SLeeLa lib/os from " + m.identity() + " */\n";
    h = h + "#ifndef OS_CONFIG_H\n#define OS_CONFIG_H\n\n";
    h = h + "#define OS_NAME        \"" + m.name() + "\"\n";
    h = h + "#define OS_VERSION     \"" + m.osVersion() + "\"\n";
    h = h + "#define OS_EDITION     \"" + m.osEdition() + "\"\n";
    h = h + "#define OS_SHAPE       \"" + m.osShape().name() + "\"\n";
    h = h + "#define OS_ARCH        \"" + m.architectures().primaryArch().arch() + "\"\n";
    h = h + "#define OS_TRIPLE      \"" + m.architectures().primaryArch().targetTriple() + "\"\n";
    h = h + "#define OS_WORD_BITS   " + intToStr(m.architectures().primaryArch().bits()) + "\n";
    h = h + "#define OS_FIRMWARE    \"" + m.bootloader().firmwareName() + "\"\n";
    h = h + "#define OS_LOADER      \"" + m.bootloader().loaderName() + "\"\n";
    h = h + "#define OS_KERNEL      \"" + m.kernelSpec().name() + "\"\n";
    h = h + "#define OS_KERNEL_VER  \"" + m.kernelSpec().kernelVersion() + "\"\n";
    h = h + "#define OS_CMDLINE     \"" + m.kernelSpec().commandLine() + "\"\n";
    h = h + "#define OS_ROOTFS      \"" + m.layout().fsFormat() + "\"\n";
    h = h + "#define OS_INIT        \"" + m.serviceSet().serviceManager() + "\"\n";
    if (m.desktopSpec().hasDesktop()) {
      h = h + "#define OS_DESKTOP     \"" + m.desktopSpec().shell() + "\"\n";
    } else {
      h = h + "#define OS_HEADLESS    1\n";
    }
    h = h + "\n#endif /* OS_CONFIG_H */\n";
    return h;
  }

  < Emit the early-boot C translation unit for the model. >
  String emitBootUnit(SLOSModel m) {
    String c = "/* os_boot.c - generated early-boot path for " + m.name() + " */\n";
    c = c + "#include \"os_config.h\"\n";
    c = c + "#include <stdint.h>\n\n";
    c = c + "/* Provided by the native layer; one per boot phase. */\n";
    c = c + "extern int firmware_handoff(const char *loader);\n";
    c = c + "extern int kernel_start(const char *cmdline);\n";
    c = c + "extern int mount_rootfs(const char *format);\n";
    c = c + "extern int load_driver(const char *name);\n";
    c = c + "extern int start_init(const char *manager);\n\n";
    c = c + "int os_early_init(void) {\n";
    c = c + "    if (firmware_handoff(OS_LOADER) != 0) return 1;\n";
    c = c + "    if (kernel_start(OS_CMDLINE) != 0)   return 2;\n";
    c = c + "    if (mount_rootfs(OS_ROOTFS) != 0)    return 3;\n";
    < One load_driver() call per built-in driver, emitted from the model. >
    c = c + emitBuiltinDriverCalls(m);
    c = c + "    if (start_init(OS_INIT) != 0)        return 5;\n";
    c = c + "    return 0;\n}\n";
    return c;
  }

  < Walk the driver manifest and emit a load_driver() call for each built-in. >
  String emitBuiltinDriverCalls(SLOSModel m) {
    String manifest = m.driverSet().driverManifest();
    String out = "";
    String line = "";
    int i = 0;
    int n = strLen(manifest);
    while (i <= n) {
      String ch = charAt(manifest, i);
      if (ch == "\n" || i == n) {
        if (containsBuiltin(line)) {
          out = out + "    if (load_driver(\"" + driverName(line) + "\") != 0) return 4;\n";
        }
        line = "";
      } else {
        line = line + ch;
      }
      i = i + 1;
    }
    return out;
  }

  < ---- Small text helpers (manifest lines are "name binding") ------------- >
  boolean containsBuiltin(String line) { return endsWith(line, " builtin"); }
  String driverName(String line) {
    < Return the token before the trailing " builtin". >
    < length of " builtin" >
    int cut = strLen(line) - 8;
    if (cut < 0) { return line; }
    return substr(line, 0, cut);
  }

  < String primitives wrap the shared native text intrinsics (same contract as >
  < lib/cpu/SLCompilerDriver): SLeeLa Strings are Java-style, length+substring. >
  int strLen(String s) { return native sleela_str_length(s); }
  String substr(String s, int a, int b) { return native sleela_str_substring(s, a, b); }
  String charAt(String s, int i) { return substr(s, i, i + 1); }
  boolean endsWith(String s, String suf) {
    int ls = strLen(s); int lf = strLen(suf);
    if (lf > ls) { return false; }
    return substr(s, ls - lf, ls) == suf;
  }
}