Home /Modules/cpu/SLAssembler.sleela

SLAssembler.sleela

lib/cpu/SLAssembler.sleela · 85 line(s) · sleela

<< lib/cpu/SLAssembler.sleela
   SLeeLa Standard Library Definition.
   Definition: Defines SLAssembler — a tiny assembler that turns instruction
     mnemonics (from SLInstructionSet) and operands into encoded words appended
     to an SLProgram. It is the convenience layer programs are written through:
     emit(opcode, rd, rs, imm) encodes one instruction via SLInstruction.
   
   This lets a Sleela program for the Sleela CPU be authored in Sleela source,
   assembled into an SLProgram image, and then loaded by the OS.
   
   Family: cpu. Composes SLInstructionSet, SLInstruction, SLProgram. >>
#sleela 2.5
material;
class SLAssembler {
  < ---- 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 = 10201;
  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; }
  SLInstructionSet isa;
  SLProgram program;

  void configure(SLInstructionSet instructionSet, SLProgram target) {
    isa = instructionSet; program = target;
  }

  < Encode and append one instruction; returns the address (index) it landed at. >
  int emit(int opcode, int rd, int rs, int imm) {
    SLInstruction instr = new SLInstruction();
    instr.configure(opcode, rd, rs, imm);
    return program.append(instr.encode());
  }

  < Convenience emitters for the common instructions. >
  int mov(int rd, int imm) { return emit(isa.MOV, rd, 0, imm); }
  int add(int rd, int rs) { return emit(isa.ADD, rd, rs, 0); }
  int sub(int rd, int rs) { return emit(isa.SUB, rd, rs, 0); }
  int cmp(int rd, int rs) { return emit(isa.CMP, rd, rs, 0); }
  int load(int rd, int addr) { return emit(isa.LOAD, rd, 0, addr); }
  int store(int rs, int addr) { return emit(isa.STORE, 0, rs, addr); }
  int jmp(int addr) { return emit(isa.JMP, 0, 0, addr); }
  int jz(int addr) { return emit(isa.JZ, 0, 0, addr); }
  int jnz(int addr) { return emit(isa.JNZ, 0, 0, addr); }
  int syscall(int number) { return emit(isa.SYSCALL, 0, 0, number); }
  int halt() { return emit(isa.HALT, 0, 0, 0); }

  SLProgram assembled() { return program; }
}