Home /Modules/cpu/SLStack.sleela

SLStack.sleela

lib/cpu/SLStack.sleela · 103 line(s) · sleela

<< lib/cpu/SLStack.sleela
   SLeeLa Standard Library Definition.
   Definition: Defines SLStack — the hardware call/data stack a core uses for
     PUSH/POP and for CALL/RET return addresses. It is a region of main memory
     addressed through a stack pointer (SP) that grows downward from a base, the
     classic convention on x86/ARM. The control unit drives it; the OS gives
     each process its own stack region.
   
   Grows downward: push decrements SP then writes; pop reads then increments.
   Overflow (past the limit) and underflow (past the base) are reported, not
   silently wrapped.
   
   Family: cpu. Composes SLRAM. >>
#sleela 2.5
material;
class SLStack {
  < ---- 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 = 10284;
  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; }
  SLRAM memory;
  < highest address of the stack region (SP starts here) >
  int base;
  < lowest address the stack may reach >
  int limit;
  < stack pointer >
  int sp;
  int pushes;
  int pops;
  < set on overflow/underflow >
  int faulted;

  void configure(SLRAM ram, int stackBase, int stackLimit) {
    memory = ram; base = stackBase; limit = stackLimit; sp = stackBase;
    pushes = 0; pops = 0; faulted = 0;
  }

  boolean empty() { return sp >= base; }
  boolean full() { return sp <= limit; }

  < Push a word: pre-decrement SP, then store. Returns new SP, or -1 on overflow. >
  int push(int value) {
    if (sp <= limit) { faulted = 1; return -1; }
    sp = sp - 1;
    memory.writeWord(sp, value);
    pushes = pushes + 1;
    return sp;
  }

  < Pop a word: read at SP, then post-increment. Returns the value, or -1 on underflow. >
  int pop() {
    if (sp >= base) { faulted = 1; return -1; }
    int value = memory.readWord(sp);
    sp = sp + 1;
    pops = pops + 1;
    return value;
  }

  int peek() { if (sp >= base) { return -1; } return memory.readWord(sp); }
  int pointer() { return sp; }
  void setPointer(int newSp) { sp = newSp; }
  int depth() { return base - sp; }
  boolean faulting() { return faulted == 1; }
  void clearFault() { faulted = 0; }
}