Skip to content

Commit

Permalink
Update Java code-generator to support iota #645
Browse files Browse the repository at this point in the history
  • Loading branch information
peterwvj committed Sep 12, 2017
1 parent 79c94bf commit be393e5
Show file tree
Hide file tree
Showing 10 changed files with 257 additions and 147 deletions.
2 changes: 2 additions & 0 deletions core/codegen/ir/src/main/resources/ir.ast
Original file line number Diff line number Diff line change
Expand Up @@ -361,6 +361,7 @@ IR {-> package='org.overture.codegen.ir'
| {ternaryIf} [condition]:IR.#exp [trueValue]:IR.#exp [falseValue]:IR.#exp
| {maplet} [left]:IR.#exp [right]:IR.#exp
| {letBeSt} [header]:IR.#letBeSt.header [value]:IR.#exp
| {iota} [bindList]:IR.#multipleBind* [predicate]:IR.#exp
| #Seq
| #Set
| #Map
Expand Down Expand Up @@ -417,6 +418,7 @@ IR {-> package='org.overture.codegen.ir'

#RuntimeError {-> package='org.overture.codegen.ir.expressions'}
= {LetBeStNoBinding}
| {iota} [message]:java_String
| {patternMatch} [message]:java_String
| {missingMember} [message]:java_String
//Raised at runtime when an attemp is made
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
import org.overture.codegen.trans.letexps.IfExpTrans;
import org.overture.codegen.trans.patterns.PatternTrans;
import org.overture.codegen.trans.patterns.PatternVarPrefixes;
import org.overture.codegen.trans.quantifier.Exists1CounterData;
import org.overture.codegen.trans.quantifier.CounterData;
import org.overture.codegen.trans.uniontypes.NonDetStmTrans;
import org.overture.codegen.trans.uniontypes.UnionTypeTrans;
import org.overture.codegen.trans.uniontypes.UnionTypeVarPrefixes;
Expand Down Expand Up @@ -100,7 +100,7 @@ private List<DepthFirstAnalysisAdaptor> setupAnalysis()
ILanguageIterator langIte = new JavaLanguageIterator(transAssist, iteVarPrefixes);
LetBeStTrans letBeStTr = new LetBeStTrans(transAssist, langIte, iteVarPrefixes);
WhileStmTrans whileTr = new WhileStmTrans(transAssist, varMan.whileCond());
Exp2StmTrans exp2stmTr = new Exp2StmTrans(iteVarPrefixes, transAssist, consExists1CounterData(), langIte, exp2stmPrefixes);
Exp2StmTrans exp2stmTr = new Exp2StmTrans(iteVarPrefixes, transAssist, consCounterData(), langIte, exp2stmPrefixes);
PatternTrans patternTr = new PatternTrans(iteVarPrefixes, transAssist, patternPrefixes, varMan.casesExp());
PreCheckTrans preCheckTr = new PreCheckTrans(transAssist, new JavaValueSemanticsTag(false));
PostCheckTrans postCheckTr = new PostCheckTrans(postCheckCreator, transAssist, varMan.funcRes(), new JavaValueSemanticsTag(false));
Expand Down Expand Up @@ -154,15 +154,15 @@ private List<DepthFirstAnalysisAdaptor> setupAnalysis()
return series;
}

private Exists1CounterData consExists1CounterData()
private CounterData consCounterData()
{
AExternalTypeIR type = new AExternalTypeIR();
type.setName("Long");

IRInfo irInfo = codeGen.getIRGenerator().getIRInfo();
AIntLiteralExpIR initExp = irInfo.getExpAssistant().consIntLiteral(0);

return new Exists1CounterData(type, initExp);
return new CounterData(type, initExp);
}

public void clear()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
new RuntimeException("$node.getMessage()")
Original file line number Diff line number Diff line change
Expand Up @@ -24,61 +24,28 @@
import java.util.LinkedList;
import java.util.List;

import org.overture.codegen.ir.INode;
import org.overture.codegen.ir.IRInfo;
import org.overture.codegen.ir.ITempVarGen;
import org.overture.codegen.ir.SExpIR;
import org.overture.codegen.ir.SMultipleBindIR;
import org.overture.codegen.ir.SPatternIR;
import org.overture.codegen.ir.SStmIR;
import org.overture.codegen.ir.STypeIR;
import org.overture.codegen.ir.*;
import org.overture.codegen.ir.analysis.AnalysisException;
import org.overture.codegen.ir.analysis.DepthFirstAnalysisAdaptor;
import org.overture.codegen.ir.declarations.AVarDeclIR;
import org.overture.codegen.ir.expressions.AAndBoolBinaryExpIR;
import org.overture.codegen.ir.expressions.ABoolLiteralExpIR;
import org.overture.codegen.ir.expressions.ACaseAltExpExpIR;
import org.overture.codegen.ir.expressions.ACasesExpIR;
import org.overture.codegen.ir.expressions.ACompMapExpIR;
import org.overture.codegen.ir.expressions.ACompSeqExpIR;
import org.overture.codegen.ir.expressions.ACompSetExpIR;
import org.overture.codegen.ir.expressions.AEnumMapExpIR;
import org.overture.codegen.ir.expressions.AEnumSeqExpIR;
import org.overture.codegen.ir.expressions.AEnumSetExpIR;
import org.overture.codegen.ir.expressions.AEqualsBinaryExpIR;
import org.overture.codegen.ir.expressions.AExists1QuantifierExpIR;
import org.overture.codegen.ir.expressions.AExistsQuantifierExpIR;
import org.overture.codegen.ir.expressions.AFieldExpIR;
import org.overture.codegen.ir.expressions.AForAllQuantifierExpIR;
import org.overture.codegen.ir.expressions.AIdentifierVarExpIR;
import org.overture.codegen.ir.expressions.ALetBeStExpIR;
import org.overture.codegen.ir.expressions.ALetDefExpIR;
import org.overture.codegen.ir.expressions.AMapletExpIR;
import org.overture.codegen.ir.expressions.AOrBoolBinaryExpIR;
import org.overture.codegen.ir.expressions.ARecordModExpIR;
import org.overture.codegen.ir.expressions.ARecordModifierIR;
import org.overture.codegen.ir.expressions.ATernaryIfExpIR;
import org.overture.codegen.ir.expressions.SBoolBinaryExpIR;
import org.overture.codegen.ir.patterns.AIdentifierPatternIR;
import org.overture.codegen.ir.patterns.ASetMultipleBindIR;
import org.overture.codegen.ir.patterns.ATypeMultipleBindIR;
import org.overture.codegen.ir.expressions.*;
import org.overture.codegen.ir.patterns.*;
import org.overture.codegen.ir.statements.AAssignToExpStmIR;
import org.overture.codegen.ir.statements.ABlockStmIR;
import org.overture.codegen.ir.statements.ACaseAltStmStmIR;
import org.overture.codegen.ir.statements.ACasesStmIR;
import org.overture.codegen.ir.statements.AIfStmIR;
import org.overture.codegen.ir.types.ABoolBasicTypeIR;
import org.overture.codegen.ir.types.AIntNumericBasicTypeIR;
import org.overture.codegen.ir.types.SSetTypeIR;
import org.overture.codegen.ir.types.*;
import org.overture.codegen.ir.utils.AHeaderLetBeStIR;
import org.overture.codegen.trans.assistants.TransAssistantIR;
import org.overture.codegen.trans.comp.ComplexCompStrategy;
import org.overture.codegen.trans.comp.MapCompStrategy;
import org.overture.codegen.trans.comp.SeqCompStrategy;
import org.overture.codegen.trans.comp.SetCompStrategy;
import org.overture.codegen.trans.iota.IotaStrategy;
import org.overture.codegen.trans.iterator.ILanguageIterator;
import org.overture.codegen.trans.let.LetBeStStrategy;
import org.overture.codegen.trans.quantifier.Exists1CounterData;
import org.overture.codegen.trans.quantifier.CounterData;
import org.overture.codegen.trans.quantifier.Exists1QuantifierStrategy;
import org.overture.codegen.trans.quantifier.OrdinaryQuantifier;
import org.overture.codegen.trans.quantifier.OrdinaryQuantifierStrategy;
Expand All @@ -88,12 +55,12 @@ public class Exp2StmTrans extends DepthFirstAnalysisAdaptor
protected TransAssistantIR transAssistant;
protected ILanguageIterator langIterator;

protected Exists1CounterData counterData;
protected CounterData counterData;
protected Exp2StmVarPrefixes prefixes;
protected IterationVarPrefixes iteVarPrefixes;

public Exp2StmTrans(IterationVarPrefixes iteVarPrefixes,
TransAssistantIR transAssistant, Exists1CounterData counterData,
TransAssistantIR transAssistant, CounterData counterData,
ILanguageIterator langIterator, Exp2StmVarPrefixes prefixes)
{
this.transAssistant = transAssistant;
Expand Down Expand Up @@ -514,6 +481,42 @@ public void caseAExistsQuantifierExpIR(AExistsQuantifierExpIR node)
}
}

@Override
public void caseAIotaExpIR(AIotaExpIR node) throws AnalysisException {

SStmIR enclosingStm = transAssistant.getEnclosingStm(node, "iota expression");

SExpIR predicate = node.getPredicate();
ITempVarGen tempVarNameGen = transAssistant.getInfo().getTempVarNameGen();
String resVarName = tempVarNameGen.nextVarName(prefixes.iota());
String counterName = tempVarNameGen.nextVarName(prefixes.iotaCounter());

IotaStrategy strategy = consIotaStrategy(predicate, tempVarNameGen, resVarName, counterName, node.getPredicate());

List<SMultipleBindIR> multipleSetBinds = filterBindList(node, node.getBindList());

ABlockStmIR block = transAssistant.consComplexCompIterationBlock(multipleSetBinds, tempVarNameGen, strategy, iteVarPrefixes);

if (multipleSetBinds.isEmpty())
{
transAssistant.replaceNodeWith(node, transAssistant.getInfo().getExpAssistant().consUndefinedExp());
} else
{
AIdentifierVarExpIR iotaResult = new AIdentifierVarExpIR();
iotaResult.setIsLocal(true);
iotaResult.setType(node.getType().clone());
iotaResult.setName(resVarName);

transform(enclosingStm, block, iotaResult, node);
block.apply(this);
}
}

public IotaStrategy consIotaStrategy(SExpIR pred, ITempVarGen tempVarNameGen, String resName, String counterName, SExpIR predicate) {

return new IotaStrategy(transAssistant, predicate, resName, counterName, langIterator, tempVarNameGen, iteVarPrefixes, counterData);
}

public OrdinaryQuantifierStrategy consOrdinaryQuantifierStrategy(SExpIR predicate, ITempVarGen tempVarNameGen, String var, TransAssistantIR transAssistant, OrdinaryQuantifier exists, ILanguageIterator langIterator, IterationVarPrefixes iteVarPrefixes) {
return new OrdinaryQuantifierStrategy(transAssistant, predicate, var, exists, langIterator, tempVarNameGen, iteVarPrefixes);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,8 @@ public String exists1()
{
return "exists1Counter_";
}

public String iota() { return "iotaExp_"; }

public String iotaCounter() { return "iotaCounter_"; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -382,7 +382,7 @@ public ACastUnaryExpIR consNextElementCall(STypeIR iteratorType,
return cast;
}

public SStmIR consConditionalIncrement(String counterName, SExpIR predicate)
public AIfStmIR consConditionalIncrement(String counterName, SExpIR predicate)
{
AIdentifierVarExpIR col = new AIdentifierVarExpIR();
col.setType(new AIntNumericBasicTypeIR());
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,174 @@
package org.overture.codegen.trans.iota;

import org.apache.log4j.Logger;
import org.overture.ast.expressions.AIotaExp;
import org.overture.ast.patterns.AIdentifierPattern;
import org.overture.codegen.ir.*;
import org.overture.codegen.ir.analysis.AnalysisException;
import org.overture.codegen.ir.declarations.AVarDeclIR;
import org.overture.codegen.ir.expressions.*;
import org.overture.codegen.ir.patterns.AIdentifierPatternIR;
import org.overture.codegen.ir.statements.*;
import org.overture.codegen.ir.types.ABoolBasicTypeIR;
import org.overture.codegen.ir.types.AErrorTypeIR;
import org.overture.codegen.ir.types.AIntNumericBasicTypeIR;
import org.overture.codegen.ir.types.ANatNumericBasicTypeIR;
import org.overture.codegen.trans.AbstractIterationStrategy;
import org.overture.codegen.trans.IterationVarPrefixes;
import org.overture.codegen.trans.assistants.TransAssistantIR;
import org.overture.codegen.trans.iterator.ILanguageIterator;
import org.overture.codegen.trans.quantifier.CounterData;

import java.util.LinkedList;
import java.util.List;

public class IotaStrategy extends AbstractIterationStrategy {

protected Logger log = Logger.getLogger(this.getClass().getName());

protected String resultVarName;
protected String counterName;
protected SExpIR predicate;

protected static final String MULTIPLE_RESULTS_ERROR_MSG = "Iota selects more than one result";
protected static final String NO_RESULT_ERROR_MSG = "Iota does not select a result";

protected CounterData counterData;

public IotaStrategy(TransAssistantIR transformationAssistant, SExpIR predicate, String resultVarName, String counterName, ILanguageIterator langIterator, ITempVarGen tempGen, IterationVarPrefixes iteVarPrefixes, CounterData counterData) {
super(transformationAssistant, langIterator, tempGen, iteVarPrefixes);
this.resultVarName = resultVarName;
this.counterName = counterName;
this.predicate = predicate;
this.counterData = counterData;
}

@Override
public List<AVarDeclIR> getOuterBlockDecls(AIdentifierVarExpIR setVar,
List<SPatternIR> patterns) throws AnalysisException
{
if (firstBind)
{
IRInfo info = transAssist.getInfo();

AIdentifierPatternIR resId = new AIdentifierPatternIR();
resId.setName(resultVarName);

List<AVarDeclIR> decls = new LinkedList<>();

STypeIR elemType = transAssist.getElementType(setVar.getType());

AUndefinedExpIR initExp = info.getExpAssistant().consUndefinedExp();
decls.add(info.getDeclAssistant().consLocalVarDecl(elemType, resId, initExp));

AIdentifierPatternIR countId = new AIdentifierPatternIR();
countId.setName(counterName);

decls.add(consCounterDecl(info, countId));

return decls;
} else
{
return null;
}
}

public AVarDeclIR consCounterDecl(IRInfo info, AIdentifierPatternIR countId) {

return transAssist.getInfo().getDeclAssistant().consLocalVarDecl(counterData.getType().clone(), transAssist.getInfo().getPatternAssistant().consIdPattern(counterName), counterData.getExp().clone());
}

@Override
public List<SStmIR> getForLoopStms(AIdentifierVarExpIR setVar,
List<SPatternIR> patterns, SPatternIR pattern)
{
if(lastBind)
{
AIdentifierVarExpIR col = new AIdentifierVarExpIR();
col.setType(new AIntNumericBasicTypeIR());
col.setIsLambda(false);
col.setIsLocal(true);
col.setName(counterName);

AIncrementStmIR inc = new AIncrementStmIR();
inc.setVar(col);

AGreaterNumericBinaryExpIR tooManyMatches = new AGreaterNumericBinaryExpIR();
tooManyMatches.setType(new ABoolBasicTypeIR());
tooManyMatches.setLeft(transAssist.getInfo().getExpAssistant().consIdVar(counterName, new ANatNumericBasicTypeIR()));
tooManyMatches.setRight(transAssist.getInfo().getExpAssistant().consIntLiteral(1));

String name = null;
if(pattern instanceof AIdentifierPatternIR)
{
name = ((AIdentifierPatternIR) pattern).getName();
}
else
{
log.error("Expected pattern to be an identifier at this point. Got: " + pattern);
}

AAssignToExpStmIR resultAssign = new AAssignToExpStmIR();
STypeIR elementType = transAssist.getElementType(setVar.getType());
resultAssign.setTarget(transAssist.getInfo().getExpAssistant().consIdVar(resultVarName, elementType));
resultAssign.setExp(transAssist.getInfo().getExpAssistant().consIdVar(name, elementType.clone()));

AIfStmIR matchesCheck = new AIfStmIR();
matchesCheck.setIfExp(tooManyMatches);
matchesCheck.setThenStm(consIotaMultipleResultsError());
matchesCheck.setElseStm(resultAssign);

ABlockStmIR outerThen = new ABlockStmIR();
outerThen.getStatements().add(inc);
outerThen.getStatements().add(matchesCheck);

AIfStmIR outerIf = new AIfStmIR();
outerIf.setIfExp(predicate);
outerIf.setThenStm(outerThen);

return packStm(outerIf);
}
else
{
return null;
}
}

@Override
public List<SStmIR> getPostOuterBlockStms(AIdentifierVarExpIR setVar,
List<SPatternIR> patterns)
{
AEqualsBinaryExpIR noMatches = new AEqualsBinaryExpIR();
noMatches.setType(new ABoolBasicTypeIR());
noMatches.setLeft(transAssist.getInfo().getExpAssistant().consIdVar(counterName, new AIntNumericBasicTypeIR()));
noMatches.setRight(transAssist.getInfo().getExpAssistant().consIntLiteral(0));

AIfStmIR ifStm = new AIfStmIR();
ifStm.setIfExp(noMatches);
ifStm.setThenStm(consIotaMultipleResultsError());

return packStm(ifStm);
}

public SStmIR consIotaNoMatchError()
{
return consIotaError(NO_RESULT_ERROR_MSG);
}

public SStmIR consIotaMultipleResultsError()
{
return consIotaError(MULTIPLE_RESULTS_ERROR_MSG);
}

public SStmIR consIotaError(String msg)
{
AIotaRuntimeErrorExpIR error = new AIotaRuntimeErrorExpIR();
error.setType(new AErrorTypeIR());
error.setMessage(msg);

ARaiseErrorStmIR raise = new ARaiseErrorStmIR();
raise.setError(error);

return raise;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
import org.overture.codegen.ir.SExpIR;
import org.overture.codegen.ir.STypeIR;

public class Exists1CounterData
public class CounterData
{
private STypeIR type;
private SExpIR exp;

public Exists1CounterData(STypeIR type, SExpIR exp)
public CounterData(STypeIR type, SExpIR exp)
{
super();
this.type = type;
Expand Down
Loading

0 comments on commit be393e5

Please sign in to comment.