Skip to content

Refactoring tuple and array translation #741

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 19 commits into from
Oct 20, 2018
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
96 changes: 39 additions & 57 deletions de.peeeq.wurstscript/parserspec/jass_im.parseq
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,9 @@ ImVar(@ignoreForEquality de.peeeq.wurstscript.ast.Element trace, ref ImType type

ImType =
ImSimpleType(String typename)
| ImArrayType(String typename)
| ImArrayTypeMulti(String typename, java.util.List<Integer> arraySize)
| ImArrayType(ref ImType entryType)
| ImArrayTypeMulti(ref ImType entryType, java.util.List<Integer> arraySize)
| ImTupleType(java.util.List<ImType> types, java.util.List<String> names)
| ImTupleArrayType(java.util.List<ImType> types, java.util.List<String> names)
| ImVoid()

ImFunction(@ignoreForEquality de.peeeq.wurstscript.ast.Element trace,
Expand Down Expand Up @@ -52,11 +51,7 @@ ImStmt =
| ImLoop(@ignoreForEquality de.peeeq.wurstscript.ast.Element trace, ImStmts body)
| ImExitwhen(@ignoreForEquality de.peeeq.wurstscript.ast.Element trace, ImExpr condition)
| ImReturn(@ignoreForEquality de.peeeq.wurstscript.ast.Element trace, ImExprOpt returnValue)
| ImSet(@ignoreForEquality de.peeeq.wurstscript.ast.Element trace, ref ImVar left, ImExpr right)
| ImSetTuple(@ignoreForEquality de.peeeq.wurstscript.ast.Element trace, ref ImVar left, int tupleIndex, ImExpr right)
| ImSetArray(@ignoreForEquality de.peeeq.wurstscript.ast.Element trace, ref ImVar left, ImExpr index, ImExpr right)
| ImSetArrayMulti(@ignoreForEquality de.peeeq.wurstscript.ast.Element trace, ref ImVar left, ImExprs indices, ImExpr right)
| ImSetArrayTuple(@ignoreForEquality de.peeeq.wurstscript.ast.Element trace, ref ImVar left, ImExpr index, int tupleIndex, ImExpr right)
| ImSet(@ignoreForEquality de.peeeq.wurstscript.ast.Element trace, ImLExpr left, ImExpr right)
| ImExpr
| ImVarargLoop(@ignoreForEquality de.peeeq.wurstscript.ast.Element trace, ImStmts body, ref ImVar loopVar)

Expand All @@ -72,26 +67,29 @@ ImFlatExprOpt =
ImExprs * ImExpr

ImExpr =
ImStatementExpr(ImStmts statements, ImExpr expr)
| ImFlatExpr

ImFlatExpr =
ImCall
| ImVarAccess(ref ImVar var)
| ImVarArrayAccess(ref ImVar var, ImExpr index)
| ImVarArrayMultiAccess(ref ImVar var, ImExpr index1, ImExpr index2)
| ImTupleExpr(ImExprs exprs)
| ImTupleSelection(ImExpr tupleExpr, int tupleIndex)
| ImClassRelatedExpr
| ImConst
| ImGetStackTrace()
| ImCompiletimeExpr(@ignoreForEquality de.peeeq.wurstscript.ast.Element trace, ImExpr expr, int executionOrderIndex)

| ImLExpr

// an expression which can be used on the left hand side of an assignment
ImLExpr =
ImVarAccess(ref ImVar var)
| ImVarArrayAccess(@ignoreForEquality de.peeeq.wurstscript.ast.Element trace, ref ImVar var, ImExprs indexes)
| ImTupleSelection(ImExpr tupleExpr, int tupleIndex) // can only be used as L-value if tupleExpr is l-exprs
| ImMemberAccess
| ImTupleExpr(ImExprs exprs) // can only be used as L-value if exprs only contains l-exprs
| ImStatementExpr(ImStmts statements, ImExpr expr) // can only be used as L-value if expr only contains l-exprs



ImClassRelatedExpr =
ImMethodCall(@ignoreForEquality de.peeeq.wurstscript.ast.Element trace, ref ImMethod method, ImExpr receiver, ImExprs arguments, boolean tuplesEliminated)
| ImAlloc(ref ImClass clazz)
| ImDealloc(ref ImClass clazz, ImExpr obj)
| ImMemberAccess(ImExpr receiver, ref ImVar var)
| ImMemberAccess(@ignoreForEquality de.peeeq.wurstscript.ast.Element trace, ImExpr receiver, ref ImVar var)
| ImInstanceof(ImExpr obj, ref ImClass clazz)
| ImTypeIdOfObj(ImExpr obj, ref ImClass clazz)
| ImTypeIdOfClass(ref ImClass clazz)
Expand All @@ -117,12 +115,10 @@ JassImElementWithName = ImVar | ImFunction | ImClass | ImMethod

ElementWithTrace = ImVar | ImFunction | ImClass | ImMethod | ImIf | ImLoop | ImExitwhen | ImReturn
| ImSet | ImSetTuple | ImSetArray | ImSetArrayMulti | ImSetArrayTuple
| ImMethodCall | ImFunctionCall | ImCompiletimeExpr
| ImMethodCall | ImFunctionCall | ImCompiletimeExpr | ImVarArrayAccess | ImMemberAccess

ElementWithTypes = ImTupleType | ImTupleArrayType

ElementWithLeft = ImSet| ImSetTuple| ImSetArray| ImSetArrayMulti| ImSetArrayTuple

ElementWithVar = ImVarAccess | ImVarArrayAccess | ImVarArrayMultiAccess | ImMemberAccess


Expand Down Expand Up @@ -160,41 +156,18 @@ ImStmt.flatten(
)
returns de.peeeq.wurstscript.translation.imtranslation.Flatten.Result
implemented by de.peeeq.wurstscript.translation.imtranslation.Flatten.flatten

ImStmt.attrPurity

ImLExpr.flattenL(
de.peeeq.wurstscript.translation.imtranslation.ImTranslator translator,
de.peeeq.wurstscript.jassIm.ImFunction f
)
returns de.peeeq.wurstscript.translation.imtranslation.Flatten.ResultL
implemented by de.peeeq.wurstscript.translation.imtranslation.Flatten.flattenL

ImStmt.attrPurity()
returns de.peeeq.wurstscript.translation.imtranslation.purity.PurityLevel
implemented by de.peeeq.wurstscript.translation.imtranslation.purity.PurityLevels.calculate

ImProg.eliminateTuples(de.peeeq.wurstscript.translation.imtranslation.ImTranslator translator)
returns void
implemented by de.peeeq.wurstscript.translation.imtranslation.EliminateTuples.eliminateTuplesProg

ImFunction.eliminateTuples(de.peeeq.wurstscript.translation.imtranslation.ImTranslator translator)
returns void
implemented by de.peeeq.wurstscript.translation.imtranslation.EliminateTuples.eliminateTuplesFunc

ImStmt.eliminateTuples(
de.peeeq.wurstscript.translation.imtranslation.ImTranslator translator,
de.peeeq.wurstscript.jassIm.ImFunction f
)
returns ImStmt
implemented by de.peeeq.wurstscript.translation.imtranslation.EliminateTuples.eliminateTuples

ImExpr.eliminateTuplesExpr(
de.peeeq.wurstscript.translation.imtranslation.ImTranslator translator,
de.peeeq.wurstscript.jassIm.ImFunction f
)
returns ImExpr
implemented by de.peeeq.wurstscript.translation.imtranslation.EliminateTuples.eliminateTuplesExpr

ImExprOpt.eliminateTuplesExprOpt(
de.peeeq.wurstscript.translation.imtranslation.ImTranslator translator,
de.peeeq.wurstscript.jassIm.ImFunction f
)
returns ImExprOpt
implemented by de.peeeq.wurstscript.translation.imtranslation.EliminateTuples.eliminateTuplesExpr


ImStmts.translate(
java.util.List<de.peeeq.wurstscript.jassAst.JassStatement> stmts,
de.peeeq.wurstscript.jassAst.JassFunction f,
Expand All @@ -221,7 +194,7 @@ ImConst.equalValue(ImConst other)
returns boolean
implemented by de.peeeq.wurstscript.translation.imtojass.Equality.equalValue

ImExpr.attrTyp
ImExpr.attrTyp()
returns ImType
implemented by de.peeeq.wurstscript.translation.imtojass.ImAttrType.getType

Expand All @@ -237,7 +210,7 @@ ImType.translateType()
returns String
implemented by de.peeeq.wurstscript.translation.imtojass.ImAttributes.translateType

ImVar.isGlobal
ImVar.isGlobal()
returns boolean
implemented by de.peeeq.wurstscript.translation.imtojass.ImAttributes.isGlobal

Expand Down Expand Up @@ -267,6 +240,13 @@ ImExpr.evaluate(de.peeeq.wurstscript.intermediatelang.interpreter.ProgramState g
returns de.peeeq.wurstscript.intermediatelang.ILconst
implemented by de.peeeq.wurstscript.intermediatelang.interpreter.EvaluateExpr.eval

ImLExpr.evaluateLvalue(de.peeeq.wurstscript.intermediatelang.interpreter.ProgramState globalState
, de.peeeq.wurstscript.intermediatelang.interpreter.LocalState localState)
returns de.peeeq.wurstscript.intermediatelang.ILaddress
implemented by de.peeeq.wurstscript.intermediatelang.interpreter.EvaluateExpr.evaluateLvalue



ImCompiletimeExpr.evaluationResult
returns java.util.concurrent.atomic.AtomicReference<de.peeeq.wurstscript.intermediatelang.ILconst>
implemented by de.peeeq.wurstscript.intermediatelang.interpreter.EvaluateExpr.compiletimeEvaluationResult
Expand Down Expand Up @@ -352,5 +332,7 @@ ImProg.attrSubclasses
returns com.google.common.collect.Multimap<ImClass, ImClass>
implemented by de.peeeq.wurstscript.translation.imtranslation.Subclasses.calculate


ImLExpr.isUsedAsLValue()
returns boolean
implemented by de.peeeq.wurstscript.translation.imtranslation.LValues.isUsedAsLValue

22 changes: 16 additions & 6 deletions de.peeeq.wurstscript/parserspec/wurstscript.parseq
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ WStatement = ActionStatement | ControlflowStatement | EndFunctionStatement(de.pe
ActionStatement =
StmtSkip(de.peeeq.wurstscript.parser.WPos source)
| LocalVarDef
| StmtSet(de.peeeq.wurstscript.parser.WPos source, NameRef updatedExpr, Expr right)
| StmtSet(de.peeeq.wurstscript.parser.WPos source, LExpr updatedExpr, Expr right)
| StmtCall
| StmtErr(de.peeeq.wurstscript.parser.WPos source)
| ExprMemberVar
Expand Down Expand Up @@ -166,7 +166,7 @@ StmtForRange =
| StmtForRangeDown(de.peeeq.wurstscript.parser.WPos source, LocalVarDef loopVar, Expr to, Expr step, WStatements body)

Expr =
NameRef
LExpr
| FunctionCall
| ExprBinary(de.peeeq.wurstscript.parser.WPos source, Expr left, de.peeeq.wurstscript.WurstOperator op, Expr right)
| ExprUnary(de.peeeq.wurstscript.parser.WPos source, de.peeeq.wurstscript.WurstOperator opU, Expr right)
Expand Down Expand Up @@ -232,6 +232,9 @@ IdentifierWithTypeParamDefs(de.peeeq.wurstscript.parser.WPos source, Identifier
// a reference to a variable, needs a varName:String attribute
NameRef = ExprVarArrayAccess | ExprVarAccess | ExprMemberVar | ExprMemberArrayVar

// an expression that can be used on the left-hand side of an assignment
LExpr = NameRef

VarDef =
GlobalOrLocalVarDef
| WParameter
Expand Down Expand Up @@ -433,7 +436,7 @@ FunctionCall.attrImplicitParameter
returns OptExpr
implemented by de.peeeq.wurstscript.attributes.AttrImplicitParameter.getImplicitParameter

NameRef.attrImplicitParameter
LExpr.attrImplicitParameter
returns OptExpr
implemented by de.peeeq.wurstscript.attributes.AttrImplicitParameter.getImplicitParameter

Expand Down Expand Up @@ -712,6 +715,13 @@ StmtForEach.attrCloseFunc
returns void
implemented by de.peeeq.wurstscript.translation.imtranslation.TLDTranslation.translate

LExpr.imTranslateExprLvalue(
de.peeeq.wurstscript.translation.imtranslation.ImTranslator translator,
de.peeeq.wurstscript.jassIm.ImFunction f
)
returns de.peeeq.wurstscript.jassIm.ImLExpr
implemented by de.peeeq.wurstscript.translation.imtranslation.ExprTranslation.translateLvalue

Expr.imTranslateExpr(
de.peeeq.wurstscript.translation.imtranslation.ImTranslator translator,
de.peeeq.wurstscript.jassIm.ImFunction f
Expand Down Expand Up @@ -763,11 +773,11 @@ FunctionLike.attrHasEmptyBody()

// Naming:

NameRef.attrNameLink
LExpr.attrNameLink
returns de.peeeq.wurstscript.attributes.names.NameLink
implemented by de.peeeq.wurstscript.attributes.AttrNameDef.calculate

NameRef.attrNameDef
LExpr.attrNameDef
returns NameDef
implemented by de.peeeq.wurstscript.attributes.AttrNameDef.calculateDef

Expand Down Expand Up @@ -1017,7 +1027,7 @@ ExprNewObject.getTypeName()
returns String
implemented by de.peeeq.wurstscript.attributes.SmallHelpers.getTypeName

NameRef.getVarName()
LExpr.getVarName()
returns String
implemented by de.peeeq.wurstscript.attributes.SmallHelpers.getVarName

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ private ImExpr constantToExprHashtable(ImCompiletimeExpr cte, Element trace, Arr
WPos errorPos = trace.attrErrorPos();
ImFunction initHashtable = findNative("InitHashtable", errorPos);
ImStmts stmts = JassIm.ImStmts(
JassIm.ImSet(trace, htVar, JassIm.ImFunctionCall(trace, initHashtable, JassIm.ImExprs(), false, CallType.NORMAL))
JassIm.ImSet(trace, JassIm.ImVarAccess(htVar), JassIm.ImFunctionCall(trace, initHashtable, JassIm.ImExprs(), false, CallType.NORMAL))
);

// we have to collect all values after all compiletime functions have run, so use delayedActions
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -394,8 +394,6 @@ public void checkProg(WurstModel model, List<CompilationUnit> toCheck) {
new EliminateClasses(imTranslator2, imProg2, !runArgs.isUncheckedDispatch()).eliminateClasses();
imTranslator2.assertProperties();
printDebugImProg("./test-output/im " + stage++ + "_classesEliminated.im");
new MultiArrayEliminator(imProg2, imTranslator2).run();
imTranslator2.assertProperties();

new VarargEliminator(imProg2).run();
printDebugImProg("./test-output/im " + stage++ + "_varargEliminated.im");
Expand Down Expand Up @@ -426,11 +424,15 @@ public void checkProg(WurstModel model, List<CompilationUnit> toCheck) {
// eliminate tuples
beginPhase(6, "eliminate tuples");
getImProg().flatten(imTranslator2);
getImProg().eliminateTuples(imTranslator2);
EliminateTuples.eliminateTuplesProg(getImProg(), imTranslator2);
getImTranslator().assertProperties(AssertProperty.NOTUPLES);

printDebugImProg("./test-output/im " + stage++ + "_withouttuples.im");

new MultiArrayEliminator(imProg2, imTranslator2).run();
printDebugImProg("./test-output/im " + stage++ + "_withoutmultiarrays.im");
imTranslator2.assertProperties();

beginPhase(7, "remove func refs");
new FuncRefRemover(imProg2, imTranslator2).run();

Expand Down Expand Up @@ -469,10 +471,12 @@ public void checkProg(WurstModel model, List<CompilationUnit> toCheck) {
beginPhase(12, "froptimize");
optimizer.optimize();

optimizer.removeGarbage();
imProg.flatten(imTranslator);
printDebugImProg("./test-output/im " + stage++ + "_afteroptimize.im");
}

optimizer.removeGarbage();


// translate flattened intermediate lang to jass:

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package de.peeeq.wurstscript.intermediatelang;

/**
*
*/
public interface ILaddress {
void set(ILconst value);
ILconst get();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package de.peeeq.wurstscript.intermediatelang;

import de.peeeq.wurstscript.types.WurstType;
import de.peeeq.wurstscript.types.WurstTypeString;

import java.util.Map;
import java.util.TreeMap;
import java.util.function.Supplier;

public class ILconstArray extends ILconstAbstract {

private final Map<Integer, ILconst> values = new TreeMap<>(); // including the quotes
private final Supplier<ILconst> defaultValue;

public ILconstArray(Supplier<ILconst> defaultValue) {
this.defaultValue = defaultValue;
}

@Override
public String print() {
StringBuilder s = new StringBuilder();
s.append("[");
for (Map.Entry<Integer, ILconst> e : values.entrySet()) {
if (s.length() > 1) {
s.append(", ");
}
s.append(e.getKey());
s.append(": ");
s.append(e.getValue());
}
s.append("]");
return s.toString();
}


@Override
public boolean isEqualTo(ILconst other) {
return other == this;
}

public void set(int index, ILconst value) {
values.put(index, value);
}

public ILconst get(int index) {
return values.computeIfAbsent(index, i -> defaultValue.get());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,7 @@ public class ILconstTuple extends ILconstAbstract {
private final ILconst[] values;

public ILconstTuple(ILconst... values) {
this.values = Arrays.stream(values)
.flatMap(c -> {
if (c instanceof ILconstTuple) {
return Arrays.stream(((ILconstTuple) c).values);
} else {
return Stream.of(c);
}
}).toArray(ILconst[]::new);
this.values = values;
}


Expand Down
Loading