From f223126eada34197ab8d14e50c0bc6e29d27668c Mon Sep 17 00:00:00 2001 From: jakobeha Date: Fri, 28 Jun 2024 11:14:24 -0400 Subject: [PATCH] add names to compiled loads --- .../java/org/prlprg/bc2ir/CFGCompiler.java | 33 ++++++++++++------- 1 file changed, 22 insertions(+), 11 deletions(-) diff --git a/src/main/java/org/prlprg/bc2ir/CFGCompiler.java b/src/main/java/org/prlprg/bc2ir/CFGCompiler.java index cac5c1fc5..83334bdf7 100644 --- a/src/main/java/org/prlprg/bc2ir/CFGCompiler.java +++ b/src/main/java/org/prlprg/bc2ir/CFGCompiler.java @@ -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; @@ -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_); @@ -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))); @@ -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); } @@ -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()); @@ -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); } @@ -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"); @@ -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 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)}. @@ -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 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)}.