Home /Modules/vm/SLVMFrame.sleela

SLVMFrame.sleela

lib/vm/SLVMFrame.sleela · 131 line(s) · sleela

<< lib/vm/SLVMFrame.sleela
   SLeeLa Standard Library Definition.
   Definition: Defines SLVMFrame — one activation record on a thread's call
     stack in the secondary Sleela VM. A frame owns a local-variable array and an
     operand stack (both SLVMValue carriers laid out over SLRAM), remembers the
     method it is running and the return instruction index to resume the caller,
     and tracks the current instruction pointer within the method's code window.
   
   Frames are what make the secondary VM a real interpreter rather than a flat
   opcode tape: CALL pushes a frame, RET pops it and restores the caller's ip.
   
   Family: vm. Composes SLRAM, SLVMValue, SLVMMethod; composed by SLVMThread. >>
#sleela 2.5
material;
class SLVMFrame {
  < ---- 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 = 927;
  String metaAuthor = "Max Rupplin vs. Dr. Mrs. Elisabeth Harkins";
  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-09-28";
  < 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; }
  SLVMMethod method;
  < caller ip to resume on RET >
  int returnIp;
  < caller's "this" class, for RET context >
  int callerClassId;
  < current instruction index (within method window) >
  int ip;

  < local slots: 2 words each (tag,payload) >
  SLRAM locals;
  int localCount;

  < operand stack: 2 words each (tag,payload) >
  SLRAM operands;
  < operand count (not word index) >
  int sp;
  < max operands >
  int capacity;

  void configure(SLVMMethod m, int retIp, int callerClass, int operandCap) {
    method = m;
    returnIp = retIp;
    callerClassId = callerClass;
    ip = (m != null) ? m.entryPoint() : 0;
    localCount = (m != null) ? m.locals() : 0;
    locals = new SLRAM(); locals.configure(localCount * 2 + 2, 32); locals.allocate();
    capacity = operandCap;
    operands = new SLRAM(); operands.configure(operandCap * 2 + 2, 32); operands.allocate();
    sp = 0;
  }

  < --- locals --- >
  void setLocal(int index, SLVMValue v) {
    int slot = index * 2;
    locals.writeWord(slot, v.packedTag());
    locals.writeWord(slot + 1, v.packedPayload());
  }
  void getLocal(int index, SLVMValue out) {
    int slot = index * 2;
    out.unpack(locals.readWord(slot), locals.readWord(slot + 1));
  }

  < --- operand stack --- >
  void push(SLVMValue v) {
    if (sp >= capacity) { return; }
    int slot = sp * 2;
    operands.writeWord(slot, v.packedTag());
    operands.writeWord(slot + 1, v.packedPayload());
    sp = sp + 1;
  }
  void pop(SLVMValue out) {
    if (sp == 0) { out.setNull(); return; }
    sp = sp - 1;
    int slot = sp * 2;
    out.unpack(operands.readWord(slot), operands.readWord(slot + 1));
  }
  void peek(SLVMValue out) {
    if (sp == 0) { out.setNull(); return; }
    int slot = (sp - 1) * 2;
    out.unpack(operands.readWord(slot), operands.readWord(slot + 1));
  }

  < --- ip control --- >
  int instructionPointer() { return ip; }
  void setIp(int newIp)     { ip = newIp; }
  void advance()            { ip = ip + 1; }

  SLVMMethod runningMethod(){ return method; }
  int returnAddress()       { return returnIp; }
  int callerClass()         { return callerClassId; }
  int operandDepth()        { return sp; }
  int localSlots()          { return localCount; }
  boolean empty()           { return sp == 0; }
}