Skip to content

Commit a381685

Browse files
authored
Use JASS_MAX_ARRAY_SIZE for jass class allocation limit (#1088)
1 parent 6859ece commit a381685

File tree

2 files changed

+23
-4
lines changed

2 files changed

+23
-4
lines changed

de.peeeq.wurstscript/src/main/java/de/peeeq/wurstscript/translation/imtranslation/RecycleCodeGeneratorQueue.java

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,21 @@
55
import de.peeeq.wurstscript.jassIm.*;
66
import de.peeeq.wurstscript.utils.Constants;
77

8+
import javax.annotation.Nullable;
9+
import java.util.Optional;
10+
import java.util.function.Supplier;
11+
812
/**
913
* Manages object ids in a queue. This way the time each object is
1014
* inactive is maximized and thus errors should be easier to detect
1115
*/
1216
public class RecycleCodeGeneratorQueue implements RecycleCodeGenerator {
1317

18+
public static boolean setTestMode = false;
19+
20+
21+
private @Nullable Supplier<ImExpr> maxSizeElementFn = null;
22+
1423
@Override
1524
public void createAllocFunc(ImTranslator translator, ImProg prog, ImClass c) {
1625
ImFunction f = translator.allocFunc.getFor(c);
@@ -19,12 +28,17 @@ public void createAllocFunc(ImTranslator translator, ImProg prog, ImClass c) {
1928
ImStmts body = f.getBody();
2029
Element tr = c.getTrace();
2130

31+
if (maxSizeElementFn == null) {
32+
Optional<ImVar> maxSizeVar = prog.getGlobals().stream().filter(var -> !setTestMode && var.getName().equals("JASS_MAX_ARRAY_SIZE")).findFirst();
33+
maxSizeVar.ifPresentOrElse(imVar -> this.maxSizeElementFn = (() -> JassIm.ImVarAccess(imVar)),
34+
() -> this.maxSizeElementFn = () -> JassIm.ImIntVal(Constants.MAX_ARRAY_SIZE));
35+
}
36+
2237
ImVar thisVar = JassIm.ImVar(tr, translator.selfType(c), "this", false); // TODO change type
2338
locals.add(thisVar);
2439

2540
ClassManagementVars mVars = translator.getClassManagementVarsFor(c);
2641

27-
int maxSize = Constants.MAX_ARRAY_SIZE;
2842
// if freeCount == 0 then
2943
ImStmts elseBlock = JassIm.ImStmts();
3044
ImStmts thenBlock = JassIm.ImStmts();
@@ -33,9 +47,9 @@ public void createAllocFunc(ImTranslator translator, ImProg prog, ImClass c) {
3347
thenBlock, elseBlock));
3448
ImStmts ifEnoughMemory = JassIm.ImStmts();
3549
ImStmts ifNotEnoughMemory = JassIm.ImStmts();
36-
// if maxIndex < 8191
50+
// if maxIndex < JASS_MAX_ARRAY_SIZE
3751
thenBlock.add(JassIm.ImIf(tr,
38-
JassIm.ImOperatorCall(WurstOperator.LESS, JassIm.ImExprs(JassIm.ImVarAccess(mVars.maxIndex), JassIm.ImIntVal(maxSize))),
52+
JassIm.ImOperatorCall(WurstOperator.LESS, JassIm.ImExprs(JassIm.ImVarAccess(mVars.maxIndex), maxSizeElementFn.get())),
3953
ifEnoughMemory, ifNotEnoughMemory));
4054
// maxIndex = maxIndex + 1
4155
ifEnoughMemory.add(JassIm.ImSet(tr, JassIm.ImVarAccess(mVars.maxIndex), JassIm.ImOperatorCall(WurstOperator.PLUS, JassIm.ImExprs(JassIm.ImVarAccess(mVars.maxIndex), JassIm.ImIntVal(1)))));

de.peeeq.wurstscript/src/test/java/tests/wurstscript/tests/WurstScriptTest.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525
import de.peeeq.wurstscript.jassprinter.JassPrinter;
2626
import de.peeeq.wurstscript.luaAst.LuaCompilationUnit;
2727
import de.peeeq.wurstscript.translation.imtranslation.ImTranslator;
28+
import de.peeeq.wurstscript.translation.imtranslation.RecycleCodeGenerator;
29+
import de.peeeq.wurstscript.translation.imtranslation.RecycleCodeGeneratorQueue;
2830
import de.peeeq.wurstscript.utils.Utils;
2931
import org.testng.Assert;
3032

@@ -155,6 +157,7 @@ CompilationResult run() {
155157

156158
private CompilationResult testScript() {
157159
RunArgs runArgs = new RunArgs();
160+
RecycleCodeGeneratorQueue.setTestMode = true;
158161
if (withStdLib) {
159162
runArgs = runArgs.with("-lib", StdLib.getLib());
160163
}
@@ -194,8 +197,8 @@ private CompilationResult testScript() {
194197
return new CompilationResult(model, gui);
195198
}
196199

197-
// translate with different options:
198200

201+
// translate with different options:
199202
testWithoutInliningAndOptimization(name, executeProg, executeTests, gui, compiler, model, executeProgOnlyAfterTransforms, runArgs);
200203

201204
testWithLocalOptimizations(name, executeProg, executeTests, gui, compiler, model, executeProgOnlyAfterTransforms, runArgs);
@@ -213,6 +216,8 @@ private CompilationResult testScript() {
213216
translateAndTestLua(name, executeProg, gui, model, compiler);
214217
}
215218

219+
RecycleCodeGeneratorQueue.setTestMode = false;
220+
216221
return new CompilationResult(model, gui);
217222
}
218223

0 commit comments

Comments
 (0)