Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
- Handle type-casting from records with the same components but different names'
- Improve scalarization of record equations in the front-end. Made it worse in the back-end (back-end does not handle cast of records)


git-svn-id: https://openmodelica.org/svn/OpenModelica/trunk@13881 f25d12d1-65f4-0310-ae8a-bbce733d8d8e
  • Loading branch information
sjoelund committed Nov 13, 2012
1 parent eb15089 commit 55611ee
Show file tree
Hide file tree
Showing 10 changed files with 246 additions and 65 deletions.
20 changes: 17 additions & 3 deletions Compiler/BackEnd/BackendDAECreate.mo
Expand Up @@ -944,13 +944,17 @@ algorithm

case (DAE.EQUATION(exp = e1,scalar = e2,source = source),_,_)
equation
(e1,source) = ExpressionSimplify.simplifyAddSymbolicOperation(e1, source);
(e2,source) = ExpressionSimplify.simplifyAddSymbolicOperation(e2, source);
(e1,source,_) = Inline.inlineExp(e1, (SOME(functionTree),{DAE.NORM_INLINE()}), source);
(e2,source,_) = Inline.inlineExp(e2, (SOME(functionTree),{DAE.NORM_INLINE()}), source);
then
BackendDAE.EQUATION(e1,e2,source)::inEqns;

case (DAE.INITIALEQUATION(exp1 = e1,exp2 = e2,source = source),_,_)
equation
(e1,source) = ExpressionSimplify.simplifyAddSymbolicOperation(e1, source);
(e2,source) = ExpressionSimplify.simplifyAddSymbolicOperation(e2, source);
(e1,source,_) = Inline.inlineExp(e1, (SOME(functionTree),{DAE.NORM_INLINE()}), source);
(e2,source,_) = Inline.inlineExp(e2, (SOME(functionTree),{DAE.NORM_INLINE()}), source);
then
Expand All @@ -965,6 +969,7 @@ algorithm

case (DAE.DEFINE(componentRef = cr1, exp = e2, source = source),_,_)
equation
(e2,source) = ExpressionSimplify.simplifyAddSymbolicOperation(e2, source);
(e2,source,_) = Inline.inlineExp(e2, (SOME(functionTree),{DAE.NORM_INLINE()}), source);
e1 = Expression.crefExp(cr1);
then
Expand All @@ -973,21 +978,24 @@ algorithm
case (DAE.INITIALDEFINE(componentRef = cr1, exp = e2, source = source),_,_)
equation
e1 = Expression.crefExp(cr1);
(e2,source) = ExpressionSimplify.simplifyAddSymbolicOperation(e2, source);
(e2,source,_) = Inline.inlineExp(e2, (SOME(functionTree),{DAE.NORM_INLINE()}), source);
then
BackendDAE.EQUATION(e1,e2,source)::inEqns;

case (DAE.COMPLEX_EQUATION(lhs = e1,rhs = e2,source = source),_,_)
equation
//(e1,source,_) = Inline.inlineExp(e1, (SOME(functionTree),{DAE.NORM_INLINE()}), source);
//(e2,source,_) = Inline.inlineExp(e2, (SOME(functionTree),{DAE.NORM_INLINE()}), source);
(e1,source) = ExpressionSimplify.simplifyAddSymbolicOperation(e1, source);
(e2,source) = ExpressionSimplify.simplifyAddSymbolicOperation(e2, source);
(e1,source,_) = Inline.forceInlineExp(e1,(SOME(functionTree),{DAE.NORM_INLINE(),DAE.NO_INLINE()}),source);
(e2,source,_) = Inline.forceInlineExp(e2,(SOME(functionTree),{DAE.NORM_INLINE(),DAE.NO_INLINE()}),source);
then
lowerextendedRecordEqn(e1,e2,source,functionTree,inEqns);

case (DAE.INITIAL_COMPLEX_EQUATION(lhs = e1,rhs = e2,source = source),_,_)
equation
(e1,source) = ExpressionSimplify.simplifyAddSymbolicOperation(e1, source);
(e2,source) = ExpressionSimplify.simplifyAddSymbolicOperation(e2, source);
(e1,source,_) = Inline.inlineExp(e1, (SOME(functionTree),{DAE.NORM_INLINE()}), source);
(e2,source,_) = Inline.inlineExp(e2, (SOME(functionTree),{DAE.NORM_INLINE()}), source);
size = Expression.sizeOf(Expression.typeof(e1));
Expand All @@ -996,13 +1004,17 @@ algorithm

case (DAE.ARRAY_EQUATION(dimension=dims,exp = e1,array = e2,source = source),_,_)
equation
(e1,source) = ExpressionSimplify.simplifyAddSymbolicOperation(e1, source);
(e2,source) = ExpressionSimplify.simplifyAddSymbolicOperation(e2, source);
(e1,source,_) = Inline.inlineExp(e1, (SOME(functionTree),{DAE.NORM_INLINE()}), source);
(e2,source,_) = Inline.inlineExp(e2, (SOME(functionTree),{DAE.NORM_INLINE()}), source);
then
lowerArrayEqn(dims,e1,e2,source,inEqns);

case (DAE.INITIAL_ARRAY_EQUATION(dimension=dims,exp = e1,array = e2,source = source),_,_)
equation
(e1,source) = ExpressionSimplify.simplifyAddSymbolicOperation(e1, source);
(e2,source) = ExpressionSimplify.simplifyAddSymbolicOperation(e2, source);
(e1,source,_) = Inline.inlineExp(e1, (SOME(functionTree),{DAE.NORM_INLINE()}), source);
(e2,source,_) = Inline.inlineExp(e2, (SOME(functionTree),{DAE.NORM_INLINE()}), source);
then
Expand Down Expand Up @@ -2026,7 +2038,9 @@ algorithm
// case for complex equations, array equations and equations
case (target :: rest_targets, source :: rest_sources, _, _, _)
equation
(target,eq_source,_) = Inline.inlineExp(target, (SOME(funcs),{DAE.NORM_INLINE()}), inEq_source);
(target,eq_source) = ExpressionSimplify.simplifyAddSymbolicOperation(target, inEq_source);
(source,eq_source) = ExpressionSimplify.simplifyAddSymbolicOperation(source, eq_source);
(target,eq_source,_) = Inline.inlineExp(target, (SOME(funcs),{DAE.NORM_INLINE()}), eq_source);
(source,eq_source,_) = Inline.inlineExp(source, (SOME(funcs),{DAE.NORM_INLINE()}), eq_source);
eqns = lowerextendedRecordEqn(target,source,eq_source,funcs,iEqns);
then
Expand Down
2 changes: 1 addition & 1 deletion Compiler/FrontEnd/DAEUtil.mo
Expand Up @@ -3833,7 +3833,7 @@ algorithm
end match;
end traverseDAE;

protected function traverseDAEFuncLst "help function to traverseDae. Traverses the functions "
public function traverseDAEFuncLst "help function to traverseDae. Traverses the functions "
input list<tuple<DAE.AvlKey,DAE.AvlValue>> ifuncLst;
input FuncExpType func;
input Type_a iextraArg;
Expand Down
37 changes: 37 additions & 0 deletions Compiler/FrontEnd/Expression.mo
Expand Up @@ -8622,5 +8622,42 @@ algorithm
end match;
end dimensionsToExps;

public function splitRecord
"Splits a record into its elements. Works for crefs, records constructor calls, and casts of the same"
input DAE.Exp inExp;
input DAE.Type ty;
output list<DAE.Exp> outExps;
algorithm
outExps := match (inExp,ty)
local
DAE.Exp exp;
DAE.Type tt,ty2;
list<DAE.Var> vs;
DAE.ComponentRef cr;
Absyn.Path p1,p2;
list<DAE.Exp> exps;
case (DAE.CAST(exp=exp),_) then splitRecord(exp,ty);
case (DAE.CREF(componentRef=cr),DAE.T_COMPLEX(varLst = vs))
then List.map1(vs,splitRecord2,cr);
case (DAE.CALL(path=p1,expLst=exps,attr=DAE.CALL_ATTR(ty=DAE.T_COMPLEX(complexClassType=ClassInf.RECORD(p2)))),_)
equation
true = Absyn.pathEqual(p1,p2) "is record constructor";
then exps;
end match;
end splitRecord;

protected function splitRecord2
input DAE.Var var;
input DAE.ComponentRef cr;
output DAE.Exp exp;
protected
String n;
DAE.Type tt,ty;
algorithm
DAE.TYPES_VAR(name = n,ty = tt) := var;
ty := Types.simplifyType(tt);
exp := makeCrefExp(ComponentReference.crefPrependIdent(cr, n, {}, ty), ty);
end splitRecord2;

end Expression;

20 changes: 20 additions & 0 deletions Compiler/FrontEnd/ExpressionSimplify.mo
Expand Up @@ -41,6 +41,7 @@ encapsulated package ExpressionSimplify

// public imports
public import Absyn;
public import ClassInf;
public import DAE;
public import Error;
public import SCode;
Expand Down Expand Up @@ -672,6 +673,7 @@ algorithm
list<list<DAE.Exp>> mexps,mexps_1;
Option<DAE.Exp> eo;
DAE.Dimensions dims;
Absyn.Path p1,p2,p3;

// Real -> Real
case(DAE.RCONST(r),DAE.T_REAL(varLst = _)) then DAE.RCONST(r);
Expand Down Expand Up @@ -728,6 +730,12 @@ algorithm
mexps_1 = List.map1List(mexps, addCast, tp2);
then DAE.MATRIX(tp,n,mexps_1);

// simplify record constructor from one to another
case (DAE.CALL(p1,exps,attr=DAE.CALL_ATTR(ty=DAE.T_COMPLEX(complexClassType=ClassInf.RECORD(p2)))),DAE.T_COMPLEX(complexClassType=ClassInf.RECORD(p3)))
equation
true = Absyn.pathEqual(p1,p2) "It is a record constructor since it has the same path called as its output type";
then DAE.CALL(p3,exps,DAE.CALL_ATTR(tp,false,false,DAE.NO_INLINE(),DAE.NO_TAIL()));

// fill(e, ...) can be simplified
case(DAE.CALL(path=Absyn.IDENT("fill"),expLst=e::exps),_)
equation
Expand Down Expand Up @@ -4543,5 +4551,17 @@ algorithm
end match;
end checkZeroLengthArrayOp;

public function simplifyAddSymbolicOperation
input DAE.Exp exp;
input DAE.ElementSource source;
output DAE.Exp outExp;
output DAE.ElementSource outSource;
protected
Boolean changed;
algorithm
(outExp,changed) := simplify(exp);
outSource := DAEUtil.condAddSymbolicTransformation(changed,source,DAE.SIMPLIFY(exp,outExp));
end simplifyAddSymbolicOperation;

end ExpressionSimplify;

8 changes: 6 additions & 2 deletions Compiler/FrontEnd/Inline.mo
Expand Up @@ -1294,8 +1294,7 @@ algorithm
equation
((e_1,(fns,true))) = Expression.traverseExp(e,inlineCall,(fns,false));
source = DAEUtil.addSymbolicTransformation(source,DAE.OP_INLINE(e,e_1));
(e_2,b) = ExpressionSimplify.simplify(e_1);
source = DAEUtil.addSymbolicTransformationSimplify(b,source,e_1,e_2);
(e_2,source) = ExpressionSimplify.simplifyAddSymbolicOperation(e_1, source);
then
(e_2,source,true);
else (inExp,inSource,false);
Expand Down Expand Up @@ -1527,6 +1526,11 @@ algorithm
list<DAE.ComponentRef> crlst;
list<tuple<DAE.ComponentRef,DAE.ComponentRef>> creftpllst;
case ({},ht) then ({},ht);
/* All elements of the record have correct type already. No cast needed. */
case((c,(DAE.CAST(exp=e,ty=DAE.T_COMPLEX(varLst=_))))::res,ht)
equation
(new1,ht1) = extendCrefRecords((c,e)::res,ht);
then (new1,ht1);
case((c,e as (DAE.CREF(componentRef = cref,ty=DAE.T_COMPLEX(varLst=varLst))))::res,ht)
equation
(res1,ht1) = extendCrefRecords(res,ht);
Expand Down
52 changes: 43 additions & 9 deletions Compiler/FrontEnd/Inst.mo
Expand Up @@ -8120,6 +8120,7 @@ algorithm
pf, dims, idxs, inst_dims, impl, comment, info, graph, csets);
source = DAEUtil.createElementSource(info, Env.getEnvPath(env), PrefixUtil.prefixToCrefOpt(pre), NONE(), NONE());
(cache,dae) = addArrayVarEquation(cache,env,ih,ci_state, dae, ty, mod, InstUtil.toConst(vt), pre, n, source);
cache = addRecordConstructorFunction(cache,Types.arrayElementType(ty));
Error.updateCurrentComponent("",Absyn.dummyInfo);
then
(cache,compenv,ih,store,dae,csets,ty,graph);
Expand All @@ -8139,6 +8140,7 @@ algorithm
pf, dims, idxs, inst_dims, impl, comment, info, graph, csets);
source = DAEUtil.createElementSource(info, Env.getEnvPath(env), PrefixUtil.prefixToCrefOpt(pre), NONE(), NONE());
(cache,dae) = addArrayVarEquation(cache,compenv,ih,ci_state, dae, ty, mod, InstUtil.toConst(vt), pre, n, source);
cache = addRecordConstructorFunction(cache,Types.arrayElementType(ty));
Error.updateCurrentComponent("",Absyn.dummyInfo);
then
(cache,compenv,ih,store,dae,csets,ty,graph);
Expand Down Expand Up @@ -8604,7 +8606,7 @@ algorithm
else
equation
true = Flags.isSet(Flags.FAILTRACE);
Debug.fprintln(Flags.FAILTRACE, "- Inst.instScalar failed on " +& inName +& "\n");
Debug.fprintln(Flags.FAILTRACE, "- Inst.instScalar failed on " +& inName +& " in scope " +& PrefixUtil.printPrefixStr(inPrefix) +& " env: " +& Env.printEnvPathStr(inEnv) +& "\n");
then
fail();
end matchcontinue;
Expand Down Expand Up @@ -8657,6 +8659,15 @@ algorithm
then
dae;

case (_, DAE.T_COMPLEX(complexClassType = ClassInf.RECORD(_)), _, DAE.MOD(eqModOption = SOME(DAE.TYPED(modifierAsExp = DAE.CAST(exp=DAE.CREF(_, _))))),
_, _, _, _)
equation
dae = instModEquation(inCref, inType, inMod, inSource, inImpl);
//move bindings from dae to inClassDae and use the resulting dae
dae = moveBindings(dae,inClassDae);
dae = DAEUtil.joinDaes(dae, inDae);
then dae;

// Parameter with binding.
case (_, _, SCode.PARAM(), DAE.MOD(eqModOption = SOME(DAE.TYPED(modifierAsExp = _))),
_, _, _, _)
Expand Down Expand Up @@ -8711,21 +8722,18 @@ algorithm
list<DAE.Element> restDae2;
DAE.Exp newBindExp;

case (DAE.DAE(DAE.EQUATION(scalar = newBindExp)::{}),
DAE.DAE(DAE.VAR(cref, kind, dir, prl, vis, ty, bind, dims, ct, src, varAttOpt, commOpt, inOut)::{}))
case (DAE.DAE(DAE.EQUATION(scalar = newBindExp)::{}),DAE.DAE(DAE.VAR(cref, kind, dir, prl, vis, ty, bind, dims, ct, src, varAttOpt, commOpt, inOut)::{}))
then (DAE.DAE({DAE.VAR(cref, kind, dir, prl, vis, ty, SOME(newBindExp), dims, ct, src, varAttOpt, commOpt, inOut)}));

case (DAE.DAE(DAE.EQUATION(scalar = newBindExp)::restDae1),
DAE.DAE(DAE.VAR(cref, kind, dir, prl, vis, ty, bind, dims, ct, src, varAttOpt, commOpt, inOut)::restDae2))
case (DAE.DAE(DAE.EQUATION(scalar = newBindExp)::restDae1),DAE.DAE(DAE.VAR(cref, kind, dir, prl, vis, ty, bind, dims, ct, src, varAttOpt, commOpt, inOut)::restDae2))
equation
DAE.DAE(restDae2) = moveBindings(DAE.DAE(restDae1),DAE.DAE(restDae2));
then (DAE.DAE(DAE.VAR(cref, kind, dir, prl, vis, ty, SOME(newBindExp), dims, ct, src, varAttOpt, commOpt, inOut)::restDae2));

case (_,_)
case (DAE.DAE(restDae1),DAE.DAE(restDae2))
equation
Debug.fprintln(Flags.FAILTRACE, "- Inst.moveBindings failed");
then
fail();
Debug.fprintln(Flags.FAILTRACE, "- Inst.moveBindings failed:" +& DAEDump.dumpElementsStr(restDae1) +& " ### " +& DAEDump.dumpElementsStr(restDae2));
then fail();
end match;
end moveBindings;

Expand Down Expand Up @@ -18077,4 +18085,30 @@ algorithm
end match;
end findUnboundVariableUseInCase;

protected function addRecordConstructorFunction "Add record constructor whenever we instantiate a variable. Needed so we can cast to this constructor freely."
input Env.Cache inCache;
input DAE.Type inType;
output Env.Cache outCache;
algorithm
outCache := matchcontinue (inCache,inType)
local
Absyn.Path p1;
DAE.Function fun;
list<DAE.Var> vars;
list<DAE.FuncArg> fargs;
DAE.Type ty;
case (_,ty as DAE.T_COMPLEX(varLst=vars,complexClassType=ClassInf.RECORD(p1)))
equation
p1 = Absyn.makeFullyQualified(p1);
failure(_ = DAEUtil.avlTreeGet(Env.getFunctionTree(inCache),p1));
fargs = Types.makeFargsList(vars);
ty = DAE.T_FUNCTION(fargs, ty, DAE.FUNCTION_ATTRIBUTES_DEFAULT, DAE.emptyTypeSource);
fun = DAE.RECORD_CONSTRUCTOR(p1,ty,DAE.emptyElementSource);
then addFunctionsToDAE(inCache, {fun}, SCode.NOT_PARTIAL());

else inCache;

end matchcontinue;
end addRecordConstructorFunction;

end Inst;

0 comments on commit 55611ee

Please sign in to comment.