Skip to content

Commit

Permalink
add names to compiled loads
Browse files Browse the repository at this point in the history
  • Loading branch information
Jakobeha committed Jun 28, 2024
1 parent c39e5c7 commit f223126
Showing 1 changed file with 22 additions and 11 deletions.
33 changes: 22 additions & 11 deletions src/main/java/org/prlprg/bc2ir/CFGCompiler.java
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,7 @@
import org.prlprg.ir.cfg.StmtData.GnuRIs;
import org.prlprg.ir.cfg.StmtData.LdFun;
import org.prlprg.ir.cfg.StmtData.NamelessCall;
import org.prlprg.ir.cfg.StmtData.RValue_;
import org.prlprg.ir.cfg.builder.CFGCursor;
import org.prlprg.ir.closure.Closure;
import org.prlprg.ir.closure.Promise;
Expand Down Expand Up @@ -338,7 +339,7 @@ private CFGCompiler(Bc bc, CFG cfg, boolean isPromise, Module module) {
cursor = new CFGCursor(cfg);

// Insert `LdFunctionEnv`, which goes before all bytecode instructions.
env = cursor.insert(new StmtData.LdFunctionEnv());
env = cursor.insert("enclos", new StmtData.LdFunctionEnv());
var entry_ = cfg.addBB("entry_");
cursor.insert(new JumpData.Goto(entry_));
cursor.moveToStart(entry_);
Expand Down Expand Up @@ -705,18 +706,18 @@ case SetLoopVal() -> {
case LdTrue() -> push(new Constant(SEXPs.TRUE));
case LdFalse() -> push(new Constant(SEXPs.FALSE));
case GetVar(var name) -> {
pushInsert(new StmtData.LdVar(get(name), false, env));
pushInsert(name, new StmtData.LdVar(get(name), false, env));
pushInsert(new StmtData.Force(pop(RValue.class), compileFrameState(), env));
}
case DdVal(var name) -> {
var ddIndex = get(name).ddNum();
pushInsert(new StmtData.LdDdVal(ddIndex, false, env));
pushInsert(name, new StmtData.LdDdVal(ddIndex, false, env));
pushInsert(new StmtData.Force(pop(RValue.class), compileFrameState(), env));
}
case SetVar(var name) -> insert(new StmtData.StVar(get(name), top(RValue.class), env));
case GetFun(var name) -> pushCallFunInsert(new StmtData.LdFun(get(name), env));
case GetFun(var name) -> pushCallFunInsert(name, new StmtData.LdFun(get(name), env));
case GetGlobFun(var name) ->
pushCallFunInsert(new StmtData.LdFun(get(name), StaticEnv.GLOBAL));
pushCallFunInsert(name, new StmtData.LdFun(get(name), StaticEnv.GLOBAL));
// ???: GNU-R calls `SYMVALUE` and `INTERNAL` to implement these, but we don't store that in
// our `RegSymSxp` data-structure. So the next three implementations may be incorrect.
case GetSymFun(var name) -> pushCall(BuiltinId.referencedBy(get(name)));
Expand Down Expand Up @@ -810,7 +811,7 @@ case MakeClosure(var arg) -> {
case Not(var ast) -> pushInsert(mkUnop(ast, StmtData.Not::new));
case DotsErr() -> insert(new StmtData.Error("'...' used in an incorrect context", env));
case StartAssign(var name) -> {
var lhs = cursor.insert(new StmtData.LdVar(get(name), false, env));
var lhs = cursor.insert(get(name).toString(), new StmtData.LdVar(get(name), false, env));
var rhs = top(RValue.class);
pushComplexAssign(false, get(name), lhs, rhs);
}
Expand Down Expand Up @@ -1037,12 +1038,12 @@ case Or2nd(var ast) -> {
cursor.insert(new JumpData.Goto(bbAfterCurrent()));
}
case GetVarMissOk(var name) -> {
pushInsert(new StmtData.LdVar(get(name), true, env));
pushInsert(name, new StmtData.LdVar(get(name), true, env));
pushInsert(new StmtData.Force(pop(RValue.class), compileFrameState(), env));
}
case DdValMissOk(var name) -> {
var ddIndex = get(name).ddNum();
pushInsert(new StmtData.LdDdVal(ddIndex, true, env));
pushInsert(name, new StmtData.LdDdVal(ddIndex, true, env));
pushInsert(new StmtData.Force(pop(RValue.class), compileFrameState(), env));
}
case Visible() -> insert(new StmtData.Visible());
Expand All @@ -1051,7 +1052,7 @@ case StartAssign2(var name) -> {
// GNU-R has "cells" and stores the assign on the main stack.
// But we don't have cells, and since we're compiling, we can store the assignment on its
// own stack.
var lhs = cursor.insert(new StmtData.LdVarSuper(get(name), env));
var lhs = cursor.insert(get(name).toString(), new StmtData.LdVarSuper(get(name), env));
var rhs = top(RValue.class);
pushComplexAssign(true, get(name), lhs, rhs);
}
Expand Down Expand Up @@ -1279,8 +1280,8 @@ case BaseGuard(var exprIdx, var after) -> {
// PIR apparently just ignores the guards (`rir2pir.cpp:341`), but we can handle here.
var expr = get(exprIdx);
var fun = (RegSymSXP) expr.fun();
var sym = cursor.insert(new StmtData.LdFun(fun, env));
var base = cursor.insert(new StmtData.LdFun(fun, StaticEnv.BASE));
var sym = cursor.insert(fun.toString(), new StmtData.LdFun(fun, env));
var base = cursor.insert("base." + fun, new StmtData.LdFun(fun, StaticEnv.BASE));
var guard = cursor.insert(new StmtData.Eq(Optional.of(expr), sym, base, env));

var safeBb = cfg.addBB("baseGuardSafe");
Expand Down Expand Up @@ -1695,6 +1696,11 @@ private void pushInsert(StmtData.RValue_ data) {
push(cursor.insert(data));
}

/** {@link #pushInsert(RValue_)} but gives the instruction the name of the symbol. */
private void pushInsert(ConstPool.Idx<RegSymSXP> name, StmtData.RValue_ data) {
push(cursor.insert(get(name).toString(), data));
}

/**
* Insert a statement that produces a value, and begin a call with it as the function via {@link
* #pushCall(RValue)}.
Expand All @@ -1703,6 +1709,11 @@ private void pushCallFunInsert(StmtData.RValue_ data) {
pushCall(cursor.insert(data));
}

/** {@link #pushCallFunInsert(RValue_)} but gives the instruction the name of the symbol. */
private void pushCallFunInsert(ConstPool.Idx<RegSymSXP> name, StmtData.RValue_ data) {
pushCall(cursor.insert(get(name).toString(), data));
}

/**
* Insert a statement that produces a value, and add it to the current call arguments via {@link
* #pushCallArg(RValue)}.
Expand Down

0 comments on commit f223126

Please sign in to comment.