Skip to content

Commit

Permalink
- Inst&Type if-statements
Browse files Browse the repository at this point in the history
git-svn-id: https://openmodelica.org/svn/OpenModelica/trunk@12044 f25d12d1-65f4-0310-ae8a-bbce733d8d8e
  • Loading branch information
sjoelund committed Jun 13, 2012
1 parent 6cead72 commit c04aee2
Show file tree
Hide file tree
Showing 4 changed files with 121 additions and 7 deletions.
7 changes: 7 additions & 0 deletions Compiler/FrontEnd/SCodeDump.mo
Expand Up @@ -55,6 +55,13 @@ algorithm
outString := Tpl.tplString(SCodeDumpTpl.dumpProgram, inProgram);
end programStr;

public function statementStr
input SCode.Statement stmt;
output String outString;
algorithm
outString := Tpl.tplString(SCodeDumpTpl.dumpStatement, stmt);
end statementStr;

public function equationStr
input SCode.EEquation inEEquation;
output String outString;
Expand Down
63 changes: 61 additions & 2 deletions Compiler/FrontEnd/SCodeExpand.mo
Expand Up @@ -779,10 +779,11 @@ algorithm
DAE.Exp rhs, lhs, exp;
DAE.Statement eq;
DAE.ComponentRef cref1, cref2;
DAE.Type ty1, ty2;
DAE.Type ty1, ty2, ty;
list<list<DAE.Subscript>> subs;
list<DAE.Statement> accum_el;
list<DAE.Dimension> dims;
list<tuple<DAE.Exp,list<Statement>>> branches;

case (InstTypes.ASSIGN_STMT(lhs = lhs, rhs = rhs), _, _)
equation
Expand All @@ -794,7 +795,16 @@ algorithm
accum_el;

case (InstTypes.NORETCALL_STMT(exp = exp), _, _)
then DAE.STMT_NORETCALL(exp, DAE.emptyElementSource)::inAccumEl;
equation
ty = Expression.typeof(exp);
dims = Types.getDimensions(ty);
accum_el = expandArray(exp, dims, {} :: inSubscripts, inAccumEl, expandNoretcall);
then accum_el;

case (InstTypes.IF_STMT(branches = branches), _, _)
equation
accum_el = expandArray(branches, {}, {} :: inSubscripts, inAccumEl, expandIfStmt);
then accum_el;

else
equation
Expand Down Expand Up @@ -834,4 +844,53 @@ algorithm
end match;
end expandAssignment;

protected function expandNoretcall
input DAE.Exp inExp;
input list<list<DAE.Subscript>> inSubscripts;
input list<DAE.Statement> inAccumEl;
output list<DAE.Statement> outAccumEl;
algorithm
outAccumEl := match(inExp, inSubscripts, inAccumEl)
local
list<list<DAE.Subscript>> subs;
list<DAE.Subscript> comp_subs;
DAE.Statement eq;
list<DAE.Exp> sub_expl;
DAE.Exp exp;

case (exp, subs as comp_subs :: _, _)
equation
subs = listReverse(subs);
sub_expl = List.map(comp_subs, Expression.subscriptExp);
exp = subscriptExp(exp, sub_expl, subs);
(exp, _) = ExpressionSimplify.simplify(exp);
eq = DAE.STMT_NORETCALL(exp, DAE.emptyElementSource);
then
eq :: inAccumEl;

end match;
end expandNoretcall;

protected function expandIfStmt
input list<tuple<DAE.Exp,list<Statement>>> branches;
input list<list<DAE.Subscript>> inSubscripts;
input list<DAE.Statement> inAccumEl;
output list<DAE.Statement> outAccumEl;
algorithm
outAccumEl := match(branches, inSubscripts, inAccumEl)
local
list<list<DAE.Subscript>> subs;
list<DAE.Subscript> comp_subs;
DAE.Statement eq;
list<DAE.Exp> sub_expl;

case (branches, subs as comp_subs :: _, _)
equation
print("TODO: Expand if-stmt\n");
then
inAccumEl;

end match;
end expandIfStmt;

end SCodeExpand;
19 changes: 15 additions & 4 deletions Compiler/FrontEnd/SCodeInst.mo
Expand Up @@ -59,6 +59,7 @@ protected import InstSymbolTable;
protected import InstUtil;
protected import List;
protected import SCodeCheck;
protected import SCodeDump;
protected import SCodeExpand;
protected import SCodeFlattenRedeclare;
protected import SCodeLookup;
Expand Down Expand Up @@ -2340,14 +2341,14 @@ protected function instEEquation
input Prefix inPrefix;
output Equation outEquation;
algorithm
outEquation := match(inEquation, inEnv, inPrefix)
outEquation := matchcontinue (inEquation, inEnv, inPrefix)
local
Absyn.Exp exp1, exp2;
DAE.Exp dexp1, dexp2;
Absyn.ComponentRef cref1, cref2;
DAE.ComponentRef dcref1, dcref2;
Absyn.Info info;
String for_index;
String for_index,str;
list<SCode.EEquation> eql;
list<Equation> ieql;
list<Absyn.Exp> if_condition, expl, args;
Expand Down Expand Up @@ -2447,11 +2448,12 @@ algorithm

else
equation
print("Unknown equation in SCodeInst.instEEquation.\n");
str = SCodeDump.equationStr(inEquation);
print("Unknown or failed equation in SCodeInst.instEEquation: " +& str +& "\n");
then
fail();

end match;
end matchcontinue;
end instEEquation;

protected function instAlgorithmSections
Expand Down Expand Up @@ -2547,13 +2549,22 @@ algorithm
then
InstTypes.WHEN_STMT(inst_branches, info);

case (SCode.ALG_NORETCALL(exp = exp1, info = info), _, _)
equation
dexp1 = instExp(exp1, inEnv, inPrefix);
then InstTypes.NORETCALL_STMT(dexp1, info);

/*
case (SCode.ALG_ASSIGN(exp1, exp2, _, info), _, _)
equation
dexp1 = instExp(exp1, inEnv, inPrefix);
dexp2 = instExp(exp2, inEnv, inPrefix);
then InstTypes.ASSIGN_STMT(dexp1, dexp2, info);
*/
else
equation
print("SCodeInst.instStatement failed: " +& SCodeDump.statementStr(statement) +& "\n");
then fail();

end match;
end instStatement;
Expand Down
39 changes: 38 additions & 1 deletion Compiler/FrontEnd/Typing.mo
Expand Up @@ -1436,16 +1436,32 @@ protected function typeStatement
algorithm
outAcc := match (inStmt,inSymbolTable,inAcc)
local
DAE.Exp lhs,rhs;
DAE.Exp lhs,rhs,exp;
Absyn.Info info;
DAE.Type lty,rty;
SymbolTable st;
list<tuple<DAE.Exp,list<Statement>>> branches;
case (InstTypes.ASSIGN_STMT(lhs=lhs,rhs=rhs,info=info),st,_)
equation
(lhs,lty,_) = typeExp(lhs, NO_EVAL(), st);
(rhs,rty,_) = typeExp(rhs, EVAL_CONST(), st);
// rhs = typeCheck(rhs,lty,rty)
then typeAssignment(lhs,rhs,info,inAcc);
case (InstTypes.NORETCALL_STMT(exp=exp, info=info),st,_)
equation
// Let's try skipping evaluation. Maybe helps some external functions
(exp,_,_) = typeExp(exp, NO_EVAL(), st);
// TODO: Check variability/etc to potentially reduce the statement?
then InstTypes.NORETCALL_STMT(exp,info)::inAcc;
case (InstTypes.IF_STMT(branches=branches, info=info),st,_)
equation
branches = List.map1(branches, typeBranchStatement, st);
then
InstTypes.IF_STMT(branches, info) :: inAcc;
else
equation
print("Unknown statement in Typing.typeStatement\n");
then fail();
end match;
end typeStatement;

Expand All @@ -1472,4 +1488,25 @@ algorithm
end matchcontinue;
end typeAssignment;

protected function typeBranchStatement
input tuple<DAE.Exp, list<Statement>> inBranch;
input SymbolTable inSymbolTable;
output tuple<DAE.Exp, list<Statement>> outBranch;
algorithm
outBranch := match(inBranch, inSymbolTable)
local
DAE.Exp cond_exp;
list<Statement> branch_body;

case ((cond_exp, branch_body), _)
equation
(cond_exp, _, _) = typeExp(cond_exp, EVAL_CONST(), inSymbolTable);
/* TODO: Type-check the condition */
branch_body = typeStatements(branch_body, inSymbolTable);
then
((cond_exp, branch_body));

end match;
end typeBranchStatement;

end Typing;

0 comments on commit c04aee2

Please sign in to comment.