Skip to content

Commit

Permalink
Use JASS_MAX_ARRAY_SIZE for jass class allocation limit (#1088)
Browse files Browse the repository at this point in the history
  • Loading branch information
Frotty authored Jan 14, 2024
1 parent 6859ece commit a381685
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,21 @@
import de.peeeq.wurstscript.jassIm.*;
import de.peeeq.wurstscript.utils.Constants;

import javax.annotation.Nullable;
import java.util.Optional;
import java.util.function.Supplier;

/**
* Manages object ids in a queue. This way the time each object is
* inactive is maximized and thus errors should be easier to detect
*/
public class RecycleCodeGeneratorQueue implements RecycleCodeGenerator {

public static boolean setTestMode = false;


private @Nullable Supplier<ImExpr> maxSizeElementFn = null;

@Override
public void createAllocFunc(ImTranslator translator, ImProg prog, ImClass c) {
ImFunction f = translator.allocFunc.getFor(c);
Expand All @@ -19,12 +28,17 @@ public void createAllocFunc(ImTranslator translator, ImProg prog, ImClass c) {
ImStmts body = f.getBody();
Element tr = c.getTrace();

if (maxSizeElementFn == null) {
Optional<ImVar> maxSizeVar = prog.getGlobals().stream().filter(var -> !setTestMode && var.getName().equals("JASS_MAX_ARRAY_SIZE")).findFirst();
maxSizeVar.ifPresentOrElse(imVar -> this.maxSizeElementFn = (() -> JassIm.ImVarAccess(imVar)),
() -> this.maxSizeElementFn = () -> JassIm.ImIntVal(Constants.MAX_ARRAY_SIZE));
}

ImVar thisVar = JassIm.ImVar(tr, translator.selfType(c), "this", false); // TODO change type
locals.add(thisVar);

ClassManagementVars mVars = translator.getClassManagementVarsFor(c);

int maxSize = Constants.MAX_ARRAY_SIZE;
// if freeCount == 0 then
ImStmts elseBlock = JassIm.ImStmts();
ImStmts thenBlock = JassIm.ImStmts();
Expand All @@ -33,9 +47,9 @@ public void createAllocFunc(ImTranslator translator, ImProg prog, ImClass c) {
thenBlock, elseBlock));
ImStmts ifEnoughMemory = JassIm.ImStmts();
ImStmts ifNotEnoughMemory = JassIm.ImStmts();
// if maxIndex < 8191
// if maxIndex < JASS_MAX_ARRAY_SIZE
thenBlock.add(JassIm.ImIf(tr,
JassIm.ImOperatorCall(WurstOperator.LESS, JassIm.ImExprs(JassIm.ImVarAccess(mVars.maxIndex), JassIm.ImIntVal(maxSize))),
JassIm.ImOperatorCall(WurstOperator.LESS, JassIm.ImExprs(JassIm.ImVarAccess(mVars.maxIndex), maxSizeElementFn.get())),
ifEnoughMemory, ifNotEnoughMemory));
// maxIndex = maxIndex + 1
ifEnoughMemory.add(JassIm.ImSet(tr, JassIm.ImVarAccess(mVars.maxIndex), JassIm.ImOperatorCall(WurstOperator.PLUS, JassIm.ImExprs(JassIm.ImVarAccess(mVars.maxIndex), JassIm.ImIntVal(1)))));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
import de.peeeq.wurstscript.jassprinter.JassPrinter;
import de.peeeq.wurstscript.luaAst.LuaCompilationUnit;
import de.peeeq.wurstscript.translation.imtranslation.ImTranslator;
import de.peeeq.wurstscript.translation.imtranslation.RecycleCodeGenerator;
import de.peeeq.wurstscript.translation.imtranslation.RecycleCodeGeneratorQueue;
import de.peeeq.wurstscript.utils.Utils;
import org.testng.Assert;

Expand Down Expand Up @@ -155,6 +157,7 @@ CompilationResult run() {

private CompilationResult testScript() {
RunArgs runArgs = new RunArgs();
RecycleCodeGeneratorQueue.setTestMode = true;
if (withStdLib) {
runArgs = runArgs.with("-lib", StdLib.getLib());
}
Expand Down Expand Up @@ -194,8 +197,8 @@ private CompilationResult testScript() {
return new CompilationResult(model, gui);
}

// translate with different options:

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

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

RecycleCodeGeneratorQueue.setTestMode = false;

return new CompilationResult(model, gui);
}

Expand Down

0 comments on commit a381685

Please sign in to comment.