Skip to content

Commit

Permalink
- implement support for functions return records down to code generat…
Browse files Browse the repository at this point in the history
…ion, code generation works for record = f(); f() = record

git-svn-id: https://openmodelica.org/svn/OpenModelica/trunk@11274 f25d12d1-65f4-0310-ae8a-bbce733d8d8e
  • Loading branch information
Jens Frenkel committed Mar 3, 2012
1 parent bd395b5 commit 9b8585e
Show file tree
Hide file tree
Showing 13 changed files with 935 additions and 419 deletions.
19 changes: 17 additions & 2 deletions Compiler/BackEnd/BackendDAE.mo
Expand Up @@ -147,8 +147,7 @@ uniontype Equation "- Equation"

record COMPLEX_EQUATION "complex equations: recordX = function call(x, y, ..);"
Integer index "Index in algorithm clauses";
.DAE.Exp lhs "left ; lhs";
.DAE.Exp rhs "right ; rhs";
list< .DAE.Exp> crefOrDerCref "crefOrDerCref ; CREF or der(CREF)" ;
.DAE.ElementSource source "origin of equation";
end COMPLEX_EQUATION;

Expand Down Expand Up @@ -251,6 +250,7 @@ uniontype Shared "Data shared for all equation-systems"
EquationArray removedEqs "these are equations that cannot solve for a variable. for example assertions, external function calls, algorithm sections without effect" ;
array<MultiDimEquation> arrayEqs "arrayEqs ; Array equations" ;
array< .DAE.Algorithm> algorithms "algorithms ; Algorithms" ;
array<ComplexEquation> complEqs "array for complex equations";
EventInfo eventInfo "eventInfo" ;
ExternalObjectClasses extObjClasses "classes of external objects, contains constructor & destructor";
BackendDAEType backendDAEType "indicate for what the BackendDAE is used";
Expand Down Expand Up @@ -330,6 +330,16 @@ uniontype MultiDimEquation "- Multi Dimensional Equation"
end MULTIDIM_EQUATION;
end MultiDimEquation;

public
uniontype ComplexEquation "- Complex Equation"
record COMPLEXEQUATION
Integer size "size of equation" ;
.DAE.Exp left "left ; lhs" ;
.DAE.Exp right "right ; rhs" ;
.DAE.ElementSource source "the element source";
end COMPLEXEQUATION;
end ComplexEquation;

public
uniontype CrefIndex "- Component Reference Index"
record CREFINDEX
Expand Down Expand Up @@ -515,6 +525,11 @@ uniontype StrongComponent
list<Value> vars "be carefule with states, this are solved for der(x)";
end SINGLEALGORITHM;

record SINGLECOMPLEXEQUATION
Value arrayIndx;
list<Value> eqns;
list<Value> vars "be carefule with states, this are solved for der(x)";
end SINGLECOMPLEXEQUATION;

end StrongComponent;

Expand Down
416 changes: 264 additions & 152 deletions Compiler/BackEnd/BackendDAECreate.mo

Large diffs are not rendered by default.

137 changes: 81 additions & 56 deletions Compiler/BackEnd/BackendDAEOptimize.mo

Large diffs are not rendered by default.

178 changes: 119 additions & 59 deletions Compiler/BackEnd/BackendDAETransform.mo

Large diffs are not rendered by default.

62 changes: 27 additions & 35 deletions Compiler/BackEnd/BackendDAEUtil.mo
Expand Up @@ -186,16 +186,6 @@ algorithm
tstr = stringAppendList({t1str," != ", t2str});
Error.addSourceMessage(Error.EQUATION_TYPE_MISMATCH_ERROR, {eqnstr,tstr}, DAEUtil.getElementSourceFileInfo(source));
then ();
case (eqn as BackendDAE.COMPLEX_EQUATION(lhs=e1,rhs=e2,source=source))
equation
eqnstr = BackendDump.equationStr(eqn);
t1 = Expression.typeof(e1);
t2 = Expression.typeof(e2);
t1str = ExpressionDump.typeString(t1);
t2str = ExpressionDump.typeString(t2);
tstr = stringAppendList({t1str," != ", t2str});
Error.addSourceMessage(Error.EQUATION_TYPE_MISMATCH_ERROR, {eqnstr,tstr}, DAEUtil.getElementSourceFileInfo(source));
then ();
//
case eqn then ();
end matchcontinue;
Expand Down Expand Up @@ -383,15 +373,7 @@ algorithm
b = Expression.equalTypes(t1,t2);
wrongEqns1 = List.consOnTrue(not b,e,wrongEqns);
then ((e,wrongEqns1));

case ((e as BackendDAE.COMPLEX_EQUATION(lhs=e1,rhs=e2),wrongEqns))
equation
t1 = Expression.typeof(e1);
t2 = Expression.typeof(e2);
b = Expression.equalTypes(t1,t2);
wrongEqns1 = List.consOnTrue(not b,e,wrongEqns);
then ((e,wrongEqns1));


//
case inTpl then inTpl;
end matchcontinue;
Expand Down Expand Up @@ -461,15 +443,16 @@ algorithm
BackendDAE.EquationArray eqns,remeqns,inieqns;
array<BackendDAE.MultiDimEquation> arreqns;
array<DAE.Algorithm> algorithms;
array<BackendDAE.ComplexEquation> complEqs;
BackendDAE.EventInfo einfo;
BackendDAE.ExternalObjectClasses eoc;
BackendDAE.EqSystem eqs;
BackendDAE.BackendDAEType btp;
case(eqs as BackendDAE.EQSYSTEM(orderedVars=ordvars,orderedEqs=ordeqns),
BackendDAE.SHARED(knvars,exobj,aliasVars,inieqns,remeqns,arreqns,algorithms,einfo,eoc,btp))
BackendDAE.SHARED(knvars,exobj,aliasVars,inieqns,remeqns,arreqns,algorithms,complEqs,einfo,eoc,btp))
equation
((algs,_,_)) = BackendEquation.traverseBackendDAEEqns(ordeqns,expandAlgorithmsbyInitStmtsHelper,(algorithms,ordvars,{}));
then(eqs,BackendDAE.SHARED(knvars,exobj,aliasVars,inieqns,remeqns,arreqns,algs,einfo,eoc,btp));
then(eqs,BackendDAE.SHARED(knvars,exobj,aliasVars,inieqns,remeqns,arreqns,algs,complEqs,einfo,eoc,btp));
end match;
end expandAlgorithmsbyInitStmts1;

Expand Down Expand Up @@ -589,6 +572,7 @@ algorithm
list<BackendDAE.Var> knvarlst,extvarlst;
array<BackendDAE.MultiDimEquation> ae;
array<DAE.Algorithm> al;
array<BackendDAE.ComplexEquation> complEqs;
list<BackendDAE.WhenClause> wc;
list<BackendDAE.ZeroCrossing> zc;
BackendDAE.Variables vars, knvars, extVars;
Expand All @@ -600,7 +584,7 @@ algorithm
BackendDAE.BackendDAEType btp;
BackendDAE.Matching matching;
BackendDAE.EqSystems systs;
case (BackendDAE.DAE(systs,BackendDAE.SHARED(knvars,extVars,av,ieqns,seqns,ae,al,BackendDAE.EVENT_INFO(whenClauseLst = wc,zeroCrossingLst = zc),extObjCls,btp)),_)
case (BackendDAE.DAE(systs,BackendDAE.SHARED(knvars,extVars,av,ieqns,seqns,ae,al,complEqs,BackendDAE.EVENT_INFO(whenClauseLst = wc,zeroCrossingLst = zc),extObjCls,btp)),_)
equation
varlst = List.mapMap(systs,BackendVariable.daeVars,varList);
knvarlst = varList(knvars);
Expand All @@ -613,7 +597,7 @@ algorithm
knvars = BackendVariable.addVars(knvarlst, knvars);
extVars = BackendVariable.addVars(extvarlst, extVars);
then
BackendDAE.DAE(systs,BackendDAE.SHARED(knvars,extVars,av,ieqns,seqns,ae,al,BackendDAE.EVENT_INFO(wc,zc),extObjCls,btp));
BackendDAE.DAE(systs,BackendDAE.SHARED(knvars,extVars,av,ieqns,seqns,ae,al,complEqs,BackendDAE.EVENT_INFO(wc,zc),extObjCls,btp));
end match;
end translateDae;

Expand Down Expand Up @@ -879,20 +863,21 @@ algorithm
BackendDAE.EquationArray eqns,seqns,ie;
array<BackendDAE.MultiDimEquation> ae;
array<DAE.Algorithm> al;
array<BackendDAE.ComplexEquation> complEqs;
BackendDAE.EventInfo wc;
BackendDAE.ExternalObjectClasses extObjCls;
BackendDAE.EqSystems eqs;
BackendDAE.BackendDAEType btp;
case (cache,env,BackendDAE.DAE(eqs,BackendDAE.SHARED(knownVars = knvars,externalObjects=extVars,aliasVars = av,
initialEqs = ie,removedEqs = seqns,arrayEqs = ae,algorithms = al,eventInfo = wc,extObjClasses=extObjCls, backendDAEType=btp)))
initialEqs = ie,removedEqs = seqns,arrayEqs = ae,algorithms = al,complEqs=complEqs,eventInfo = wc,extObjClasses=extObjCls, backendDAEType=btp)))
equation
knvarlst = varList(knvars);
(varlst1,varlst2) = List.splitOnTrue(knvarlst,BackendVariable.isParam);
paramvars = listVar(varlst1);
knvarlst = List.map3(varlst1, calculateValue, cache, env, paramvars);
knvars = listVar(listAppend(knvarlst,varlst2));
then
BackendDAE.DAE(eqs,BackendDAE.SHARED(knvars,extVars,av,ie,seqns,ae,al,wc,extObjCls,btp));
BackendDAE.DAE(eqs,BackendDAE.SHARED(knvars,extVars,av,ie,seqns,ae,al,complEqs,wc,extObjCls,btp));
end match;
end calculateValues;

Expand Down Expand Up @@ -1088,14 +1073,15 @@ algorithm
BackendDAE.EquationArray eqns,remeqns,inieqns;
array<BackendDAE.MultiDimEquation> arreqns;
array<DAE.Algorithm> algorithms;
array<BackendDAE.ComplexEquation> complEqs;
BackendDAE.EventInfo einfo;
BackendDAE.ExternalObjectClasses eoc;
BackendDAE.EqSystems eqs;
BackendDAE.BackendDAEType btp;
case (inCref,inExp,inVar,BackendDAE.SHARED(knvars,exobj,aliasVars,inieqns,remeqns,arreqns,algorithms,einfo,eoc,btp))
case (inCref,inExp,inVar,BackendDAE.SHARED(knvars,exobj,aliasVars,inieqns,remeqns,arreqns,algorithms,complEqs,einfo,eoc,btp))
equation
aliasVars1 = updateAliasVariables(aliasVars,inCref,inExp,inVar);
then BackendDAE.SHARED(knvars,exobj,aliasVars1,inieqns,remeqns,arreqns,algorithms,einfo,eoc,btp);
then BackendDAE.SHARED(knvars,exobj,aliasVars1,inieqns,remeqns,arreqns,algorithms,complEqs,einfo,eoc,btp);
end match;
end updateAliasVariablesDAE;

Expand Down Expand Up @@ -1471,9 +1457,10 @@ algorithm
b = boolAnd(isDiscreteExp(e1,vars,knvars), isDiscreteExp(e2,vars,knvars));
then b;

case(BackendDAE.COMPLEX_EQUATION(lhs = e1,rhs = e2),vars,knvars) equation
b = boolAnd(isDiscreteExp(e1,vars,knvars), isDiscreteExp(e2,vars,knvars));
then b;
case(BackendDAE.COMPLEX_EQUATION(crefOrDerCref = expl),vars,knvars) equation
// fails if all mapped function calls doesn't return true
List.map2AllValue(expl,isDiscreteExp,true,vars,knvars);
then true;

case(BackendDAE.ARRAY_EQUATION(crefOrDerCref = expl),vars,knvars) equation
// fails if all mapped function calls doesn't return true
Expand Down Expand Up @@ -2706,15 +2693,16 @@ algorithm
BackendDAE.EquationArray eqns,remeqns,inieqns;
array<BackendDAE.MultiDimEquation> arreqns;
array<DAE.Algorithm> algorithms;
array<BackendDAE.ComplexEquation> complEqs;
list<BackendDAE.WhenClause> wclst,wclst1;
list<BackendDAE.ZeroCrossing> zc;
BackendDAE.ExternalObjectClasses eoc;
BackendDAE.EqSystems eqs;
BackendDAE.BackendDAEType btp;
case (inWcLst,BackendDAE.SHARED(knvars,exobj,aliasVars,inieqns,remeqns,arreqns,algorithms,BackendDAE.EVENT_INFO(wclst,zc),eoc,btp))
case (inWcLst,BackendDAE.SHARED(knvars,exobj,aliasVars,inieqns,remeqns,arreqns,algorithms,complEqs,BackendDAE.EVENT_INFO(wclst,zc),eoc,btp))
equation
wclst1 = listAppend(wclst,inWcLst);
then BackendDAE.SHARED(knvars,exobj,aliasVars,inieqns,remeqns,arreqns,algorithms,BackendDAE.EVENT_INFO(wclst1,zc),eoc,btp);
then BackendDAE.SHARED(knvars,exobj,aliasVars,inieqns,remeqns,arreqns,algorithms,complEqs,BackendDAE.EVENT_INFO(wclst1,zc),eoc,btp);
end match;
end whenClauseAddDAE;

Expand Down Expand Up @@ -3781,10 +3769,9 @@ algorithm
res;

// COMPLEX_EQUATION
case (vars,BackendDAE.COMPLEX_EQUATION(lhs = e1,rhs = e2),_,inIndexType)
case (vars,BackendDAE.COMPLEX_EQUATION(crefOrDerCref = expl),_,inIndexType)
equation
lst1 = incidenceRowExp(e1, vars, {},inIndexType);
res = incidenceRowExp(e2, vars, lst1,inIndexType);
res = incidenceRow1(expl, incidenceRowExp, vars, {},inIndexType);
then
res;

Expand Down Expand Up @@ -6356,6 +6343,11 @@ algorithm
i = listLength(ilst);
outInt = countComponents(comps,inInt+i);
then outInt;
case (BackendDAE.SINGLECOMPLEXEQUATION(vars=ilst)::comps,inInt)
equation
i = listLength(ilst);
outInt = countComponents(comps,inInt+i);
then outInt;
end match;
end countComponents;

Expand Down

0 comments on commit 9b8585e

Please sign in to comment.