Skip to content
This repository has been archived by the owner on May 18, 2019. It is now read-only.

Commit

Permalink
Handle inline where inputs are record fun-calls
Browse files Browse the repository at this point in the history
Use DAE.RSUB() to handle general expressions. When doing inline of a
function call, we can use DAE.RSUB instead of failing; previously we
only handled component references passed to the call. Also added code
generation and simplifications for DAE.RSUB since it was only used
for MetaModelica previously.

This fixes some of the issues raised in ticket:4423.
  • Loading branch information
sjoelund committed Dec 15, 2017
1 parent beb2a6b commit 72c299e
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 1 deletion.
14 changes: 14 additions & 0 deletions Compiler/FrontEnd/ExpressionSimplify.mo
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,8 @@ algorithm

case (DAE.CALL(),_) then (simplifyCall(inExp),options);

case (DAE.RSUB(),_) then (simplifyRSub(inExp),options);

case (DAE.MATCHEXPRESSION(),_) then (simplifyMatch(inExp),options);
case (DAE.UNBOX(),_) then (simplifyUnbox(inExp),options);
case (DAE.BOX(),_) then (simplifyUnbox(inExp),options);
Expand All @@ -302,6 +304,18 @@ algorithm
end match;
end simplifyWork;

protected function simplifyRSub
input output DAE.Exp e;
algorithm
e := match e
local
DAE.ComponentRef cr;
case (DAE.RSUB(exp=DAE.CREF(componentRef=cr), ix=-1))
then DAE.CREF(ComponentReference.joinCrefs(cr, ComponentReference.makeCrefIdent(e.fieldName, e.ty, {})), e.ty);
else e;
end match;
end simplifyRSub;

protected function simplifyAsubExp
input DAE.Exp origExp;
input DAE.Exp inExp;
Expand Down
14 changes: 13 additions & 1 deletion Compiler/FrontEnd/Inline.mo
Original file line number Diff line number Diff line change
Expand Up @@ -1424,7 +1424,7 @@ public function replaceArgs
algorithm
(outExp,outTuple) := matchcontinue (inExp,inTuple)
local
DAE.ComponentRef cref;
DAE.ComponentRef cref, firstCref;
list<tuple<DAE.ComponentRef, DAE.Exp>> argmap;
DAE.Exp e;
Absyn.Path path;
Expand All @@ -1447,6 +1447,18 @@ algorithm
BaseHashTable.hasKey(ComponentReference.crefFirstCref(cref),checkcr)
then (inExp,(argmap,checkcr,false));

case (DAE.CREF(componentRef = cref),(argmap,checkcr,true))
algorithm
firstCref := ComponentReference.crefFirstCref(cref);
{} := ComponentReference.crefSubs(firstCref);
e := getExpFromArgMap(argmap,firstCref);
while not ComponentReference.crefIsIdent(cref) loop
cref := ComponentReference.crefRest(cref);
{} := ComponentReference.crefSubs(cref);
e := DAE.RSUB(e, -1, ComponentReference.crefFirstIdent(cref), ComponentReference.crefType(cref));
end while;
then (e,inTuple);

case (DAE.CREF(componentRef = cref),(argmap,checkcr,true))
equation
getExpFromArgMap(argmap,ComponentReference.crefStripSubs(ComponentReference.crefFirstCref(cref)));
Expand Down
4 changes: 4 additions & 0 deletions Compiler/Template/CodegenCFunctions.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -3699,6 +3699,7 @@ template expTypeFromExpFlag(Exp exp, Integer flag)
case e as CONS(__)
case e as LIST(__)
case e as SIZE(__) then expTypeFlag(typeof(e), flag)
case c as RSUB(ix=-1) then expTypeFlag(c.ty, flag)

case META_TUPLE(__)
case META_OPTION(__)
Expand Down Expand Up @@ -6246,6 +6247,9 @@ template daeExpRsub(Exp inExp, Context context, Text &preExp,
"Generates code for an tsub expression."
::=
match inExp
case RSUB(ix=-1) then
let res = daeExp(exp, context, &preExp, &varDecls, &auxFunction)
'<%res%>._<%fieldName%>'
case RSUB(__) then
let res = daeExp(exp, context, &preExp, &varDecls, &auxFunction)
let offset = intAdd(ix,1) // 1-based
Expand Down
12 changes: 12 additions & 0 deletions Compiler/Template/CodegenCppCommon.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -973,10 +973,22 @@ template daeExp(Exp exp, Context context, Text &preExp /*BUFP*/, Text &varDecls
case e as PARTEVALFUNCTION(__)then daeExpPartEvalFunction(e, context, &preExp, &varDecls, simCode , &extraFuncs, &extraFuncsDecl, extraFuncsNamespace, stateDerVectorName, useFlatArrayNotation)
case e as BOX(__) then daeExpBox(e, context, &preExp, &varDecls, simCode, &extraFuncs, &extraFuncsDecl, extraFuncsNamespace, stateDerVectorName, useFlatArrayNotation)
case e as UNBOX(__) then daeExpUnbox(e, context, &preExp, &varDecls, simCode, &extraFuncs, &extraFuncsDecl, extraFuncsNamespace, stateDerVectorName, useFlatArrayNotation)
case e as RSUB(__) then daeExpRSub(e, context, &preExp, &varDecls, simCode, &extraFuncs, &extraFuncsDecl, extraFuncsNamespace, stateDerVectorName, useFlatArrayNotation)

else error(sourceInfo(), 'Unknown exp:<%ExpressionDumpTpl.dumpExp(exp,"\"")%>')
end daeExp;

template daeExpRSub(Exp exp, Context context, Text &preExp, Text &varDecls, SimCode simCode, Text& extraFuncs, Text& extraFuncsDecl,
Text extraFuncsNamespace, Text stateDerVectorName /*=__zDot*/, Boolean useFlatArrayNotation)
"Generates code for an tsub expression."
::=
match exp
case RSUB(ix=-1) then
let res = daeExp(exp, context, &preExp, &varDecls, simCode, &extraFuncs, &extraFuncsDecl, extraFuncsNamespace, stateDerVectorName, useFlatArrayNotation)
'<%res%>.<%fieldName%>_'
case RSUB(__) then
error(sourceInfo(), '<%ExpressionDumpTpl.dumpExp(exp,"\"")%>: failed')
end daeExpRSub;

template daeExpRange(Exp exp, Context context, Text &preExp, Text &varDecls, SimCode simCode, Text& extraFuncs, Text& extraFuncsDecl,
Text extraFuncsNamespace, Text stateDerVectorName /*=__zDot*/, Boolean useFlatArrayNotation)
Expand Down

0 comments on commit 72c299e

Please sign in to comment.