Skip to content
Closed
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 @@ -76,6 +76,8 @@ public void localOptimizations() {
totalCount.forEach((k, v) -> {
WLogger.info("== " + k + ": " + v);
});

InitFunctionCleaner.clean(trans.getImProg());
}

public void doNullsetting() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package de.peeeq.wurstscript.translation.imoptimizer;

import de.peeeq.wurstscript.jassIm.*;

import java.util.Collection;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

public class InitFunctionCleaner {

public static void clean(ImProg prog) {
List<ImFunction> initFuncs = prog.getFunctions().stream().filter(func -> func.getName().startsWith
("init_") && func.getBody().size() == 1).collect(Collectors.toList());

initFuncs.forEach(func -> {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please use normal for-loops -- better performance, better stack traces, better debugging, easier to read

ImVar globalBridge = null;
for (ImVar global : prog.getGlobals()) {
if (global.getName().equals("ref_function_" + func.getName())) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't like using the name here. Names should not matter in the intermediate language.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What to use then?

globalBridge = global;
break;
}
}
if (globalBridge != null) {
Collection<ImVarRead> imVarReads = globalBridge.attrReads();
if (imVarReads.size() == 1) {
ImVarRead initRead = imVarReads.iterator().next();
ImVar var = initRead.getVar();

Optional<ImFunction> mainFunc = prog.getFunctions().stream().filter(f -> f.getName().equals("main")).findFirst();
if (mainFunc.isPresent()) {
ImStmts body = mainFunc.get().getBody();
for (int i = 0; i < body.size(); i++) {
ImStmt imStmt = body.get(i);
if (imStmt instanceof ImFunctionCall) {
ImFunction calledFunc = ((ImFunctionCall) imStmt).getFunc();
if (calledFunc.getName().equals("TriggerAddCondition")) {
int finalI = i;
((ImFunctionCall) imStmt).getArguments().forEach(arg -> {
if ((arg instanceof ImFunctionCall) && ((ImFunctionCall) arg).getFunc().getName().equals("Condition")) {
ImExpr next = ((ImFunctionCall) arg).getArguments().iterator().next();
if ((next instanceof ImVarAccess) && ((ImVarAccess) next).getVar().structuralEquals(var)) {
imStmt.replaceBy(JassIm.ImNull());
if (body.get(finalI - 1) instanceof ImFunctionCall) {
body.get(finalI - 1).replaceBy(JassIm.ImNull());
}
body.get(finalI + 1).replaceBy(JassIm.ImNull());
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is the -1 and +1 doing here? Looks like you are assuming that there are always certain other statements around a call to TriggerAddCondition but that does not seem very reliable to me.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The init calls are always generated the same way. Unless someone names a function "ref_function_" or so (which is the name issue above) then it seems reliable to me.

}
}
});
}
}
}
}
}
}
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ private static void initFunctionsWithoutSideEffects() {
"R2S", "R2SW", "Rad2Deg", "S2I", "S2R", "Sin", "SquareRoot", "StringCase", "StringHash",
"StringLength", "SubString", "Tan", "TimerGetElapsed", "TimerGetRemaining", "TimerGetTimeout",
"VersionGet", "WaygateGetDestinationX", "WaygateGetDestinationY", "WaygateIsActive",
"WaygateSetDestination");
"WaygateSetDestination", "Player");
// just to be sure, sort it again
Collections.sort(functionsWithoutSideEffects);
}
Expand Down