Skip to content

Commit

Permalink
A function returning a record does not always assign the inputs direc… (
Browse files Browse the repository at this point in the history
#625)

* A function returning a record does not always assign the inputs directly to the record elements.

  - We can not assume that every function that returns a record just assigns the
    arguments it is given directly to the member of the record. That is only partially
    true for record constructor functions.
  • Loading branch information
mahge committed Dec 11, 2019
1 parent c2927a9 commit 4582b4c
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 12 deletions.
3 changes: 2 additions & 1 deletion OMCompiler/Compiler/FrontEnd/Expression.mo
Expand Up @@ -12141,6 +12141,7 @@ end makeVectorCall;

public function expandCrefs
input DAE.Exp inExp;
input Boolean expandRecord;
output DAE.Exp outExp;
input output Integer dummy=0 "For traversal";
algorithm
Expand All @@ -12150,7 +12151,7 @@ algorithm
list<DAE.Exp> exp_lst;
DAE.Exp exp;
case DAE.CREF(ty=arr_ty as DAE.T_ARRAY()) algorithm
exp_lst := list(makeCrefExp(cr, arr_ty.ty) for cr in ComponentReference.expandCref(inExp.componentRef, true));
exp_lst := list(makeCrefExp(cr, arr_ty.ty) for cr in ComponentReference.expandCref(inExp.componentRef, expandRecord));
exp := listToArray(exp_lst, arr_ty.dims);
then exp;
else inExp;
Expand Down
14 changes: 7 additions & 7 deletions OMCompiler/Compiler/FrontEnd/Inline.mo
Expand Up @@ -838,8 +838,7 @@ algorithm
true = checkInlineType(inlineType,fns);
(fn,comment) = getFunctionBody(p,fns);
(checkcr,repl) = getInlineHashTableVarTransform();
if (Config.acceptMetaModelicaGrammar())
then // MetaModelica
if (Config.acceptMetaModelicaGrammar()) then // MetaModelica
crefs = List.map(fn,getInputCrefs);
crefs = List.select(crefs,removeWilds);
argmap = List.threadTuple(crefs,args);
Expand All @@ -860,12 +859,10 @@ algorithm
// merge statements to one line
(repl,assrtStmts) = mergeFunctionBody(stmts,repl,{});
// depend on detection of assert or not
if (listEmpty(assrtStmts))
then // no assert detected
if (listEmpty(assrtStmts)) then // no assert detected

// output
newExp = Expression.makeTuple(list( getReplacementCheckComplex(repl,cr,ty) for cr in lst_cr));

// compare types
true = checkExpsTypeEquiv(e1, newExp);
// input map cref again function args
Expand Down Expand Up @@ -907,7 +904,6 @@ algorithm
end matchcontinue;
end inlineCall;


protected function inlineAssert "inlines an assert.
author:Waurich TUD 2013-10"
input DAE.Statement assrtIn;
Expand Down Expand Up @@ -1259,6 +1255,7 @@ algorithm
end matchcontinue;
end checkInlineType;

// TODO: mahge: This needs to be rewritten completely.
public function extendCrefRecords
"extends crefs from records"
input list<tuple<DAE.ComponentRef, DAE.Exp>> inArgmap;
Expand All @@ -1276,6 +1273,7 @@ algorithm
list<DAE.Exp> expl;
list<DAE.ComponentRef> crlst;
list<tuple<DAE.ComponentRef,DAE.ComponentRef>> creftpllst;
Absyn.Path rpath;
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())))::res,ht)
Expand All @@ -1299,7 +1297,9 @@ algorithm
(new1,ht2) = extendCrefRecords(new,ht1);
res2 = listAppend(new1,res1);
then ((c,e)::res2,ht2);
case((c,e as (DAE.CALL(expLst = expl,attr=DAE.CALL_ATTR(ty=DAE.T_COMPLEX(varLst=varLst)))))::res,ht)
// If the call is to a record constructor then "extend the cref and inline" them
case((c,e as (DAE.CALL(expLst = expl,attr=DAE.CALL_ATTR(ty=DAE.T_COMPLEX(complexClassType=ClassInf.RECORD(rpath), varLst=varLst)))))::res,ht)
guard AbsynUtil.pathEqual(e.path,rpath)
equation
(res1,ht1) = extendCrefRecords(res,ht);
crlst = List.map1(varLst,extendCrefRecords2,c);
Expand Down
11 changes: 7 additions & 4 deletions OMCompiler/Compiler/SimCode/SimCodeUtil.mo
Expand Up @@ -6099,6 +6099,7 @@ algorithm
end createSingleComplexEqnCode;

// TODO: are the cases really correct?
// mahge: These should be revised or removed!!
protected function createSingleComplexEqnCode2
input list<DAE.ComponentRef> crefs;
input DAE.Exp inExp3;
Expand Down Expand Up @@ -6180,7 +6181,7 @@ algorithm
// check all crefs are on the lhs
ht = HashSet.emptyHashSet();
ht = List.fold(crefs, BaseHashSet.add, ht);
expLst = Expression.traverseExpList(expLst, Expression.expandCrefs, 0) "The routines generate bad code for arrays inside the record unless we expand them";
expLst = Expression.traverseExpList(expLst, function Expression.expandCrefs(expandRecord=true), 0) "The routines generate bad code for arrays inside the record unless we expand them";
List.foldAllValue(expLst, createSingleComplexEqnCode3, true, ht);
(e2_1, _) = Expression.extendArrExp(e2, false);

Expand Down Expand Up @@ -6211,7 +6212,7 @@ algorithm
// check all crefs are on the rhs => turn
ht = HashSet.emptyHashSet();
ht = List.fold(crefs, BaseHashSet.add, ht);
expLst = Expression.traverseExpList(expLst, Expression.expandCrefs, 0) "The routines generate bad code for arrays inside the record unless we expand them";
expLst = Expression.traverseExpList(expLst, function Expression.expandCrefs(expandRecord=true), 0) "The routines generate bad code for arrays inside the record unless we expand them";
List.foldAllValue(expLst, createSingleComplexEqnCode3, true, ht);
(e1_1, _) = Expression.extendArrExp(e1, false);
// true = ComponentReference.crefEqualNoStringCompare(cr, cr2);
Expand Down Expand Up @@ -14009,6 +14010,7 @@ protected
list<SimCodeVar.SimVar> vars;
SimCode.SimCode simCode;
Integer index;
list<DAE.ComponentRef> crf_lst;
algorithm
if SimCodeFunctionUtil.inFunctionContext(context) then
return;
Expand All @@ -14018,13 +14020,14 @@ algorithm
case DAE.CREF(ty=DAE.T_ARRAY())
algorithm
simCode := getSimCode();
vars := list(cref2simvar(cr, simCode) for cr in ComponentReference.expandCref(e.componentRef, true));
crf_lst := ComponentReference.expandCref(e.componentRef, true);
vars := list(cref2simvar(cr, simCode) for cr in crf_lst);
if not listEmpty(vars) then
SimCodeVar.SIMVAR(index=index)::vars := vars;
for v in vars loop
// The array needs to be expanded because it's not stored in contiguous memory
if v.index <> index+1 then
e := Expression.expandCrefs(e);
e := Expression.expandCrefs(e, false /*do not expand records*/);
break;
end if;
index := v.index;
Expand Down

0 comments on commit 4582b4c

Please sign in to comment.