Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions de.peeeq.wurstscript/parserspec/lua.parseq
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,13 @@ LuaStatement =
LuaDefinition
| LuaChunkLocal(LuaDefinition definition) // a definition printed as a local of the main chunk
| LuaAssignment(LuaExpr left, LuaExpr right)
| LuaMultipleAssignment(LuaExprlist targets, LuaExpr right) // the results of a call, one per target
| LuaWhile(LuaExpr cond, LuaStatements body)
| LuaFor(ref LuaVariable loopVar, LuaExpr from, LuaExpr to, LuaExprOpt step, LuaStatements body)
| LuaIf(LuaExpr cond, LuaStatements thenStmts, LuaStatements elseStmts)
| LuaBreak()
| LuaReturn(LuaExprOpt retVal)
| LuaReturnValues(LuaExprlist values) // several results
| LuaExpr

LuaExprOpt = LuaExpr | LuaNoExpr()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1027,8 +1027,8 @@ public LuaCompilationUnit transformProgToLua() {

timeTaker.beginPhase("eliminate tuples");
getImProg().flatten(imTranslator2);
// The tuples left are the multiple results of calls (LuaMultipleResults), whose shape the elimination checks.
EliminateTuples.eliminateTuplesProg(getImProg(), imTranslator2);
imTranslator2.assertProperties(AssertProperty.NOTUPLES);
timeTaker.endPhase();

optimizer.removeGarbage();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import de.peeeq.wurstscript.jassIm.*;
import de.peeeq.wurstscript.translation.imtranslation.ImHelper;
import de.peeeq.wurstscript.translation.imtranslation.ImTranslator;
import de.peeeq.wurstscript.translation.imtranslation.LuaMultipleResults;
import de.peeeq.wurstscript.types.TypesHelper;
import io.vavr.Tuple2;
import io.vavr.collection.HashMap;
Expand Down Expand Up @@ -37,26 +38,36 @@ public String getName() {

static class Value {
final @Nullable ImVar copyVar;
/** With a copyVar which holds a call's results (LuaMultipleResults): the component copied, otherwise -1. */
final int component;
final @Nullable ImConst constantValue;
final @Nullable ImTupleExpr constantTuple;

public Value(ImVar copyVar) {
this.copyVar = copyVar;
this(copyVar, -1);
}

/** A copy of component {@code component} of the results local {@code results}. */
public Value(ImVar results, int component) {
this.copyVar = results;
this.component = component;
this.constantValue = null;
this.constantTuple = null;
if(copyVar.isGlobal()) {
if(results.isGlobal()) {
throw new IllegalArgumentException("copyVar must not be a Global.");
}
}

public Value(ImConst constantValue) {
this.copyVar = null;
this.component = -1;
this.constantValue = constantValue;
this.constantTuple = null;
}

public Value(ImTupleExpr tupleExpr) {
this.copyVar = null;
this.component = -1;
this.constantValue = null;
this.constantTuple = tupleExpr;

Expand Down Expand Up @@ -93,7 +104,7 @@ public boolean equals(@Nullable Object obj) {

public boolean equalValue(Value other) {
if (copyVar != null && other.copyVar != null) {
return copyVar == other.copyVar;
return copyVar == other.copyVar && component == other.component;
} else if (constantValue != null && other.constantValue != null) {
return constantValue.equalValue(other.constantValue);
} else if (constantTuple != null && other.constantTuple != null) {
Expand All @@ -117,7 +128,7 @@ public boolean equalValue(Value other) {
@Override
public String toString() {
if (copyVar != null) {
return "copy of " + copyVar;
return "copy of " + copyVar + (component < 0 ? "" : "." + component);
} else if (constantValue != null) {
return "constant " + constantValue;
} else {
Expand Down Expand Up @@ -180,6 +191,9 @@ public void visit(ImVarAccess va) {
if (val.constantValue != null) {
va.replaceBy(val.constantValue.copy());
totalPropagated++;
} else if (val.component >= 0) {
va.replaceBy(JassIm.ImTupleSelection(JassIm.ImVarAccess(val.copyVar), val.component));
totalPropagated++;
} else if (val.copyVar != null) {
ImVar old = va.getVar();
ImVar target = val.copyVar;
Expand Down Expand Up @@ -328,6 +342,11 @@ private void analyzeComponent(List<Node> scc, Map<Node, Knowledge> knowledge) {
}
} else if(right instanceof ImTupleExpr) {
newValue = Value.tryValue(right);
} else if (right instanceof ImTupleSelection selection
&& selection.getTupleExpr() instanceof ImVarAccess results
&& LuaMultipleResults.isResultsLocal(results.getVar())) {
// a component of a call's results, which only another call writes
newValue = new Value(results.getVar(), selection.getTupleIndex());
}

if (newValue == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -435,7 +435,7 @@ private boolean isEffectFree(Element e) {
ImFunction target = call.getFunc();
if (!target.isNative() || localPlayerAnalysis().isLocalPlayerSource(target)
|| !(SideEffectFreeNatives.isFunctionWithoutSideEffect(target.getName())
|| translator.isLuaKeyedMapRead(target))) {
|| translator.isLuaTableRead(target))) {
return false;
}
} else if (e instanceof ImExpr) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,11 @@ public void doNullsetting() {
public boolean removeGarbage() {
boolean wasFlat = trans.isUnitTestMode() && trans.isFlat();
boolean changed = removeGarbageWithFacts();
// A Lua result which no call reads is not returned, and what computed it is then garbage of its own.
while (trans.luaMultipleResultsLowered() && LuaUnreadResults.drop(this, trans)) {
changed = true;
removeGarbageWithFacts();
}
if (wasFlat && !trans.isFlat()) {
throw new AssertionError("The garbage removal made a flat program not flat");
}
Expand Down Expand Up @@ -438,7 +443,7 @@ private void collectTargetEffects(ImExpr target, SideEffectAnalyzer analyzer, Li
}
}

private List<ImExpr> collectSideEffects(ImExpr expr, SideEffectAnalyzer analyzer) {
List<ImExpr> collectSideEffects(ImExpr expr, SideEffectAnalyzer analyzer) {
if (expr == null) {
return Collections.emptyList();
}
Expand All @@ -455,7 +460,7 @@ private List<ImExpr> collectSideEffects(ImExpr expr, SideEffectAnalyzer analyzer
}
if (analyzer.hasObservableSideEffects(expr, func -> func.isNative()
&& (SideEffectFreeNatives.isFunctionWithoutSideEffect(func.getName())
|| trans.isLuaKeyedMapRead(func)))) {
|| trans.isLuaTableRead(func)))) {
return Collections.singletonList(expr);
}
return Collections.emptyList();
Expand Down
Loading
Loading