Home /Modules/cpu/SLFrontendC.sleela

SLFrontendC.sleela

lib/cpu/SLFrontendC.sleela · 92 line(s) · sleela

<< lib/cpu/SLFrontendC.sleela
   SLeeLa Standard Library Definition.
   Definition: Defines SLFrontendC โ€” the C-language frontend. It lowers C
     translation units to the common SLIR: declarations become memory/stack
     slots, expressions become IR temporaries and arithmetic ops, function calls
     become IR_PARAM/IR_CALL, and `return` becomes IR_RET. The shared backend
     then compiles the IR to machine code for the SLInstructionSet.
   
   SCOPE NOTE: a full C compiler (preprocessor, the entire type system, the
   standard library) is far beyond one class; this frontend models the real
   C-to-IR lowering contract and demonstrates a complete lowering for a small C
   function (int add3(void){ return 2+3; } then write+exit). Extending it to
   full C means growing parse() into a complete C parser โ€” the IR and backend
   below it do not change. This mirrors how clang targets LLVM IR.
   
   Family: cpu. Extends SLFrontend. >>
#sleela 2.5
material;
class SLFrontendC extends SLFrontend {
  < ---- 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 = 10219;
  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-06";
  < 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; }

  void init(String path) { configure("c", LANG_C, path); }

  < Model lowering of the CANONICAL equivalence program: >
  < int main(void){ int x = 2 + 3; write(1, &x, 1); return 0; } >
  < Every frontend in the family lowers this same computation (x = 2 + 3, >
  < write x, exit) so that reducing all four to the Sleela opcode substrate >
  < yields the identical observable result โ€” the operational statement of 1:1 >
  < Turing effect (see SLTuringBridge). The language-specific lowering shapes >
  < (C automatic storage, C++ objects, JVM bytecode) differ; the computed >
  < value does not. >
  SLIR lower() {
    SLIR m = intermediate();
    SLIRInstruction k = new SLIRInstruction();

    int c2 = m.allocTemp();
    int c3 = m.allocTemp();
    int x  = m.allocTemp();

    m.emit(k.IR_CONST, c2, -1, -1, 2);
    m.emit(k.IR_CONST, c3, -1, -1, 3);
    < x = 2 + 3 >
    m.emit(k.IR_ADD,   x,  c2, c3, 0);
    < write(x) -> SYS_WRITE >
    m.emit(k.IR_SYSCALL, -1, x, -1, 0);
    < return 0 -> SYS_EXIT >
    m.emit(k.IR_SYSCALL, -1, -1, -1, 4);
    m.emit(k.IR_HALT, -1, -1, -1, 0);
    return m;
  }
}