Skip to content
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,7 @@ private static List<ImVar> definedLocals(ImStmt stmt) {
* that in their place.
*
* @return whether the statements which replaced an assignment assign a local which the flatten of an effect made
* (the assignment of a division which may stop the thread)
* (the assignment of an allocation or a division which may stop the thread)
*/
private boolean eliminateDeadCode(Map<ImStmt, Set<ImVar>> livenessInfo, ImFunction func) {
int locals = func.getLocals().size();
Expand All @@ -338,8 +338,8 @@ private boolean eliminateDeadCode(Map<ImStmt, Set<ImVar>> livenessInfo, ImFuncti

if (v == null || v.isGlobal()) continue;

if (!livenessInfo.get(s).contains(v) && !Flatten.mayStopTheThread(set.getRight())) {
// (an assignment of a division which may stop the thread is the statement which evaluates it)
if (!livenessInfo.get(s).contains(v) && !Flatten.requiresDiscardAssignment(set.getRight())) {
// These effects need their assignment even when the assigned value is dead.
final List<ImExpr> raw = new ArrayList<>();
collectLhsSideEffects(lhs, raw);
if (hasSideEffects(set.getRight())) raw.add(set.getRight());
Expand Down Expand Up @@ -389,10 +389,10 @@ private void collectLhsSideEffects(ImLExpr lhs, List<ImExpr> out) {


private boolean hasSideEffects(Element e) {
if (e instanceof ImMethodCall) return true;
if (e instanceof ImMethodCall || e instanceof ImDealloc) return true;
if (e instanceof ImFunctionCall call
&& (translator == null || !translator.isTrapFreeLuaIntrinsicCall(call))) return true;
if (e instanceof ImExpr expr && Flatten.mayStopTheThread(expr)) return true;
if (e instanceof ImExpr expr && Flatten.requiresDiscardAssignment(expr)) return true;
for (int i = 0; i < e.size(); i++) if (hasSideEffects(e.get(i))) return true;
return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,7 @@ public Set<ImVar> usedVariables(Element e) {
*/
public Set<ImFunction> directlyCalledFunctions(Element e) {
Set<ImFunction> calledFunctions = new LinkedHashSet<>();
Set<ImMethod> visitedMethods = new LinkedHashSet<>();
e.accept(new ImFunction.DefaultVisitor() {

@Override
Expand All @@ -273,13 +274,35 @@ public void visit(ImFunctionCall c) {
@Override
public void visit(ImMethodCall c) {
super.visit(c);
calledFunctions.add(c.getMethod().getImplementation());
collectImplementations(c.getMethod(), visitedMethods, calledFunctions);
}
});
return calledFunctions;

}

private void collectImplementations(ImMethod method, Set<ImMethod> visited, Set<ImFunction> functions) {
if (!visited.add(method)) {
return;
}
functions.add(method.getImplementation());
for (ImMethod subMethod : method.getSubMethods()) {
collectImplementations(subMethod, visited, functions);
}
}

private static boolean hasObjectLifetimeEffects(Element elem) {
if (elem instanceof ImAlloc || elem instanceof ImDealloc) {
return true;
}
for (int i = 0; i < elem.size(); i++) {
if (hasObjectLifetimeEffects(elem.get(i))) {
return true;
}
}
return false;
}

/**
* Variables directly used in e
*/
Expand Down Expand Up @@ -456,7 +479,7 @@ public boolean hasSideEffects(Element elem) {
Set<ImFunction> natives = calledNatives(elem);
Set<ImFunction> directFuncs = calledFunctions(elem);
Set<ImVar> imVars = directlySetVariables(elem);
return natives.size() + directFuncs.size() + imVars.size() > 0;
return natives.size() + directFuncs.size() + imVars.size() > 0 || hasObjectLifetimeEffects(elem);
}

/**
Expand All @@ -477,7 +500,7 @@ private ObservableSideEffectChecker(Predicate<ImFunction> isNativeWithoutSideEff
}

private boolean hasSideEffects(Element elem) {
if (!directlySetVariables(elem).isEmpty()) {
if (!directlySetVariables(elem).isEmpty() || hasObjectLifetimeEffects(elem)) {
return true;
}
for (ImFunction nativeFunc : calledNatives(elem)) {
Expand Down Expand Up @@ -518,7 +541,7 @@ private boolean functionHasSideEffects(ImFunction func) {
}

private boolean hasGlobalSideEffects(Element elem) {
if (hasMemberWrites(elem)) {
if (hasMemberWrites(elem) || hasObjectLifetimeEffects(elem)) {
return true;
}
for (ImVar var : directlySetVariables(elem)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,7 @@ private boolean garbageRound(boolean incremental, @Nullable List<ImVar> newlyUnr
}
// remove set statements to unread variables
final List<Pair<ImStmt, List<ImExpr>>> replacements = Lists.newArrayList();
// the unread locals of the assignments which stay because they evaluate a division which may stop the thread
// Unread locals which still evaluate an allocation or a potentially trapping division.
final Set<ImVar> keptLocals = new LinkedHashSet<>();
if (countThings) {
things += trans.setStatementsOf(f).size() + f.getLocals().size();
Expand All @@ -335,9 +335,8 @@ private boolean garbageRound(boolean incremental, @Nullable List<ImVar> newlyUnr
if (e.getLeft() instanceof ImVarAccess) {
ImVarAccess va = (ImVarAccess) e.getLeft();
if (!readVars.contains(va.getVar()) && !NamePreservation.isPreserved(va.getVar())) {
if (va.getVar().getParent() == f.getLocals() && Flatten.mayStopTheThread(e.getRight())) {
// What it does besides assigning is the division, and the statement which evaluates
// that is an assignment to a local (a flatten makes one): this one.
if (va.getVar().getParent() == f.getLocals() && Flatten.requiresDiscardAssignment(e.getRight())) {
// Flatten already gave this effect its necessary assignment; keep it stable across rounds.
keptLocals.add(va.getVar());
} else {
List<ImExpr> sideEffects = collectSideEffects(e.getRight(), sideEffectAnalyzer);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -252,9 +252,14 @@ public static boolean mayStopTheThread(ImExpr e) {
return !(divisor instanceof ImIntVal value) || value.getValI() == 0;
}

/** These effects have no statement form on both backends, so retain an assignment even if unread. */
public static boolean requiresDiscardAssignment(ImExpr expr) {
return expr instanceof ImAlloc || mayStopTheThread(expr);
}

/**
* Puts the effects of evaluating {@code e} into {@code result} as statements: the calls, deallocations and method
* calls, an and/or whose right side has statements as an if, and an operator which may stop the thread as an
* calls, an and/or whose right side has statements as an if, and an allocation or trapping operator as an
* assignment. The rest of an expression only produces a value, and it goes.
*/
private static void exprToStatements(List<ImStmt> result, Element e, ImTranslator t, ImFunction f) {
Expand All @@ -273,11 +278,11 @@ private static void exprToStatements(List<ImStmt> result, Element e, ImTranslato
} else if (e instanceof ImStatementExpr e2) {
flattenStatementsInto(result, e2.getStatements(), t, f);
exprToStatements(result, e2, t, f);
} else if (e instanceof ImOperatorCall oc && mayStopTheThread(oc)) {
} else if (e instanceof ImExpr expr && requiresDiscardAssignment(expr)) {
// Jass and Lua have no expression statement, so the value goes in a local which nothing reads.
ImType type = oc.attrTyp();
de.peeeq.wurstscript.ast.Element trace = oc.attrTrace();
Result res = flatten(oc, t, f);
ImType type = expr.attrTyp();
de.peeeq.wurstscript.ast.Element trace = expr.attrTrace();
Result res = expr.flatten(t, f);
result.addAll(res.stmts);
ImVar tempVar = JassIm.ImVar(trace, type, getTempVarName(), false);
f.getLocals().add(tempVar);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,14 @@ public static PurityLevel calculate(ImCall s) {
return ChangesTheWorld.instance;
}

public static PurityLevel calculate(ImAlloc s) {
return ChangesTheWorld.instance;
}

public static PurityLevel calculate(ImDealloc s) {
return ChangesTheWorld.instance;
}

public static PurityLevel calculate(ImSet s) {
return mergeWithChildren(s, WritesGlobals.instance);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import java.util.regex.Pattern;

import static org.testng.Assert.expectThrows;
import static org.testng.AssertJUnit.assertEquals;
import static org.testng.AssertJUnit.assertFalse;
import static org.testng.AssertJUnit.assertTrue;
import static org.testng.AssertJUnit.fail;
Expand Down Expand Up @@ -511,9 +512,9 @@ public void handleBoundGenericValuesKeepTheirNativeLuaRepresentation() throws IO
assertFalse("generic values and handle keys need no index conversions: " + init,
init.contains("GetHandleId") || init.contains("__wurst_objectToIndex")
|| init.contains("__wurst_classToIndex") || init.contains("__wurst_classFromIndex"));
assertTrue("each generic put is one direct table store: " + init,
init.contains("KeyedMap_map_storage[this10][key1] = value4")
&& init.contains("KeyedMap_map_storage[this11][key2] = value5"));
assertEquals("each generic put is one direct table store: " + init, 2,
Pattern.compile("KeyedMap_map_storage\\[this[0-9]*\\]\\[key[0-9]*\\] = value[0-9]*\\b")
.matcher(init).results().count());
assertFalse("generic get and put are table accesses, with no stub to define",
lua.contains("function __wurst_keyedMapGet(") || lua.contains("function __wurst_keyedMapPut("));
}
Expand Down Expand Up @@ -1348,7 +1349,8 @@ public void intKeyedClassMapLowersToSingleLuaIndexes() throws IOException {
String compiled = compiled("intKeyedClassMapLowersToSingleLuaIndexes");
String init = getFunctionBody(compiled, "init_Test");
assertTrue("writes under an int key lower to unguarded table stores and reads to the table index: " + init,
init.contains("[1093677104] = this6") && init.contains("m[1093677104] = nil")
Pattern.compile("\\[1093677104\\] = this[0-9]*\\b").matcher(init).find()
&& init.contains("m[1093677104] = nil")
&& init.contains("] ~= nil)") && !init.contains("__wurst_keyedMapPut")
&& !init.contains("__wurst_keyedMapRemove") && !init.contains("__wurst_keyedMapGet"));
assertFalse("an int key is never nil and needs no test: " + init, init.contains("(1093677104 ~= nil)"));
Expand Down
Loading
Loading