Home /Modules/vm/SLVMHeap.sleela

SLVMHeap.sleela

lib/vm/SLVMHeap.sleela · 145 line(s) · sleela

<< lib/vm/SLVMHeap.sleela
   SLeeLa Standard Library Definition.
   Definition: Defines SLVMHeap — the object store of the secondary Sleela VM.
     It hands out stable integer handles and lays every object out over SLRAM as
     a small header (classId, fieldCount, mark) followed by its field slots. A
     value never embeds another object; it embeds a handle, so the GC can relocate
     or reclaim without the SLeeLa surface needing pointers.
   
   Allocation is a bump pointer over an SLRAM arena (unbounded tape => the VM can
   express arbitrarily large object graphs). mark()/sweepReset() give the native
   tri-colour collector (GC.md) a surface to drive; here they maintain the mark
   bit so a Sleela-sourced VM can reason about liveness without crossing to C.
   
   Family: vm. Composes SLRAM; composed by SLVM. >>
#sleela 2.5
material;
class SLVMHeap {
  < ---- 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 = 928;
  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; }
  < Per-object header layout (words), then fieldCount field slots follow. >
  < classId of this object >
  static final int H_CLASS  = 0;
  < number of field slots >
  static final int H_FIELDS = 1;
  < GC mark bit (0 = white, 1 = reachable) >
  static final int H_MARK   = 2;
  < header size in words >
  static final int HEADER   = 3;
  < Each field slot is a packed SLVMValue: 2 words (tag, payload). >
  static final int SLOT     = 2;

  SLRAM arena;
  int capacityWords;
  < bump pointer (word index of next free cell) >
  int next;
  int liveObjects;
  < total objects ever allocated >
  int allocated;

  void configure(int words) {
    capacityWords = words;
    arena = new SLRAM(); arena.configure(words, 32); arena.allocate();
    next = 0; liveObjects = 0; allocated = 0;
  }

  < Allocate an object of classId with fieldCount fields; returns its handle >
  < (the word index of its header). All fields start as NULL. >
  int allocate(int classId, int fieldCount) {
    int size = HEADER + (fieldCount * SLOT);
    < out of heap; GC must run >
    if (next + size > capacityWords) { return -1; }
    int handle = next;
    arena.writeWord(handle + H_CLASS, classId);
    arena.writeWord(handle + H_FIELDS, fieldCount);
    arena.writeWord(handle + H_MARK, 0);
    int i = 0;
    while (i < fieldCount) {
      int slot = handle + HEADER + (i * SLOT);
      < TAG_NULL >
      arena.writeWord(slot, 0);
      < payload 0 >
      arena.writeWord(slot + 1, 0);
      i = i + 1;
    }
    next = next + size;
    liveObjects = liveObjects + 1;
    allocated = allocated + 1;
    return handle;
  }

  int classOf(int handle)   { return arena.readWord(handle + H_CLASS); }
  int fieldCount(int handle){ return arena.readWord(handle + H_FIELDS); }

  < Read field i of an object into the provided SLVMValue carrier. >
  void getField(int handle, int index, SLVMValue out) {
    int slot = handle + HEADER + (index * SLOT);
    out.unpack(arena.readWord(slot), arena.readWord(slot + 1));
  }

  < Write a value into field i of an object. >
  void setField(int handle, int index, SLVMValue v) {
    int slot = handle + HEADER + (index * SLOT);
    arena.writeWord(slot, v.packedTag());
    arena.writeWord(slot + 1, v.packedPayload());
  }

  < --- GC surface (drives / mirrors the native collector) --- >
  void mark(int handle)    { if (handle >= 0) { arena.writeWord(handle + H_MARK, 1); } }
  boolean marked(int handle){ return arena.readWord(handle + H_MARK) == 1; }
  void clearMarks() {
    int i = 0;
    while (i < next) {
      int fc = arena.readWord(i + H_FIELDS);
      arena.writeWord(i + H_MARK, 0);
      i = i + HEADER + (fc * SLOT);
    }
  }

  int bytesUsed()   { return next * 4; }
  int wordsUsed()   { return next; }
  int capacity()    { return capacityWords; }
  int objectCount() { return liveObjects; }
  int totalAllocated() { return allocated; }
  boolean full(int fieldCount) { return next + HEADER + (fieldCount * SLOT) > capacityWords; }
}