SLFrontendCpp.sleela
lib/cpu/SLFrontendCpp.sleela · 132 line(s) · sleela
<< lib/cpu/SLFrontendCpp.sleela
SLeeLa Standard Library Definition.
Definition: Defines SLFrontendCpp โ the C++ frontend. It extends the C
frontend's lowering with the C++-specific constructs that still reduce to
the same common SLIR: an object's method call lowers to a function call
whose first IR_PARAM is the `this` pointer; a constructor lowers to an
allocation plus an initializer call; virtual dispatch lowers to a vtable
load (IR_LOAD) followed by an indirect IR_CALL.
SCOPE NOTE: templates, overload resolution, exceptions, and the STL are a
full C++ front-end's job and are out of scope for one class; this models the
canonical C++-to-IR lowerings (this-pointer, construction, virtual dispatch)
and demonstrates one complete lowering. The IR and backend are unchanged โ
exactly how clang++ shares LLVM with clang.
Family: cpu. Extends SLFrontendC (C++ is a superset lowering). >>
#sleela 2.5
material;
class SLFrontendCpp extends SLFrontendC {
< ---- 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 = 10220;
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 initCpp(String path) { configure("cpp", LANG_CPP, path); }
< Model lowering of the CANONICAL equivalence program expressed with a C++ >
< object (so the C++-specific lowering is exercised), computing the same >
< value as every other frontend: >
< struct Box { int a, b; int sum(){ return a + b; } }; >
< int main(){ Box box{2, 3}; write(1, box.sum()); return 0; } >
< Construction stores the fields; box.sum() is a call with `this` as the >
< first param; the returned value is x = 2 + 3 = 5 โ identical to the C, >
< Java, and Sleela frontends, which is what makes the four 1:1 in Turing >
< effect once reduced to the Sleela opcode substrate (SLTuringBridge). >
SLIR lower() {
SLIR m = intermediate();
SLIRInstruction k = new SLIRInstruction();
int thisPtr = m.allocTemp();
int two = m.allocTemp();
int three = m.allocTemp();
int one = m.allocTemp();
int ret = m.allocTemp();
int sumLbl = m.allocLabel();
< Box box{2, 3}: fields a and b at this+0 and this+1. >
< &box >
m.emit(k.IR_CONST, thisPtr, -1, -1, 2048);
m.emit(k.IR_CONST, two, -1, -1, 2);
< box.a = 2 >
m.emit(k.IR_STORE, -1, thisPtr, two, 0);
m.emit(k.IR_CONST, one, -1, -1, 1);
< &box.b = this + 1 >
m.emit(k.IR_ADD, one, thisPtr, one, 0);
m.emit(k.IR_CONST, three, -1, -1, 3);
< box.b = 3 >
m.emit(k.IR_STORE, -1, one, three, 0);
< box.sum(): pass `this`, call the method, receive a + b. >
< push this >
m.emit(k.IR_PARAM, -1, thisPtr, -1, 0);
< ret = Box::sum(this) = 2 + 3 >
m.emit(k.IR_CALL, ret, -1, -1, sumLbl);
< write(ret) >
m.emit(k.IR_SYSCALL, -1, ret, -1, 0);
< return 0 >
m.emit(k.IR_SYSCALL, -1, -1, -1, 4);
m.emit(k.IR_HALT, -1, -1, -1, 0);
< Box::sum(this){ return this->a + this->b; } >
m.emit(k.IR_LABEL, -1, -1, -1, sumLbl);
int self = m.allocTemp();
int fa = m.allocTemp();
int fb = m.allocTemp();
int selfb = m.allocTemp();
int s = m.allocTemp();
int one2 = m.allocTemp();
< this >
m.emit(k.IR_CONST, self, -1, -1, 2048);
< a = this->a >
m.emit(k.IR_LOAD, fa, self, -1, 0);
m.emit(k.IR_CONST, one2, -1, -1, 1);
< &this->b >
m.emit(k.IR_ADD, selfb, self, one2, 0);
< b = this->b >
m.emit(k.IR_LOAD, fb, selfb, -1, 0);
< a + b >
m.emit(k.IR_ADD, s, fa, fb, 0);
m.emit(k.IR_RET, -1, s, -1, 0);
return m;
}
}