Skip to content

Commit

Permalink
- Hanlde multi dimensional array exressions in array equations.
Browse files Browse the repository at this point in the history
- Fix cref expansions.



git-svn-id: https://openmodelica.org/svn/OpenModelica/trunk@24980 f25d12d1-65f4-0310-ae8a-bbce733d8d8e
  • Loading branch information
mahge committed Mar 7, 2015
1 parent 95053ca commit a4fa7a9
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 38 deletions.
10 changes: 1 addition & 9 deletions Compiler/FrontEnd/CheckModel.mo
Expand Up @@ -473,15 +473,7 @@ algorithm
// a := expr; // where a is array
case (DAE.STMT_ASSIGN_ARR(lhs=exp1), _, _)
equation
cr = Expression.expCref(exp1);
subs = ComponentReference.crefLastSubs(cr);
if not listEmpty(subs) // not an empty subs list
then
subs = List.fill(DAE.WHOLEDIM(), listLength(subs));
cr = ComponentReference.crefSetLastSubs(cr, subs);
end if;
crlst = ComponentReference.expandCref(cr, true);
ht = List.fold(crlst, BaseHashSet.add, iht);
(_, (_, ht)) = Expression.traverseExpTopDown(exp1, statementOutputsCrefFinder, (inCrefExpansion, iht));
then ht;

case(DAE.STMT_IF(statementLst = stmts, else_ = elsebranch), _, _)
Expand Down
50 changes: 32 additions & 18 deletions Compiler/FrontEnd/ComponentReference.mo
Expand Up @@ -2859,18 +2859,18 @@ public function expandCref_impl
input Boolean expandRecord;
output list<DAE.ComponentRef> outCref;
algorithm
outCref := match(inCref,expandRecord)
outCref := matchcontinue(inCref,expandRecord)
local
DAE.Ident id;
DAE.Type ty,correctTy;
DAE.Type ty, basety,correctTy;
DAE.TypeSource source;
list<DAE.Dimension> dims;
list<DAE.Subscript> subs;
DAE.ComponentRef cref;
list<DAE.ComponentRef> crefs, crefs2;
list<DAE.Var> varLst;

// A simple cref without subscripts but record type.
// A scalar record ident cref. Expand record true
case (DAE.CREF_IDENT(_, DAE.T_COMPLEX(varLst=varLst,complexClassType=ClassInf.RECORD(_)), {}),true)
equation
// Create a list of crefs from names
Expand All @@ -2879,49 +2879,64 @@ algorithm
then
List.map1Flat(crefs,expandCref_impl,true);

// A simple cref without subscripts but array type.
case (DAE.CREF_IDENT(id, correctTy as DAE.T_ARRAY(ty=DAE.T_COMPLEX(varLst=varLst,complexClassType=ClassInf.RECORD(_)), dims = dims), {}),true)
// A array record ident cref without subscripts. Expand record true
case (DAE.CREF_IDENT(id, ty as DAE.T_ARRAY(source=source), {}),true)
equation
// Flatten T_ARRAY(T_ARRAY(T_COMPLEX(), dim2,src), dim1,src) types to one level T_ARRAY(simpletype, alldims, src)
(basety as DAE.T_COMPLEX(varLst=varLst,complexClassType=ClassInf.RECORD(_)), dims) = Types.flattenArrayTypeOpt(ty);
correctTy = DAE.T_ARRAY(basety,dims,source);
// Create a list of : subscripts to generate all elements.
subs = List.fill(DAE.WHOLEDIM(), listLength(dims));
crefs = expandCref2(id, correctTy, subs, dims);
then
expandCrefLst(crefs,varLst,{});

// A simple cref without subscripts but array type.
case (DAE.CREF_IDENT(id, correctTy as DAE.T_ARRAY( dims = dims), {}),_)
// A array type cref (possibly record but no expansion of records) without subscripts.
case (DAE.CREF_IDENT(id, ty as DAE.T_ARRAY(source=source), {}),_)
equation
// Flatten T_ARRAY(T_ARRAY(T_..., dim2,src), dim1,src) types to one level T_ARRAY(simpletype, alldims, src)
(basety, dims) = Types.flattenArrayTypeOpt(ty);
correctTy = DAE.T_ARRAY(basety,dims,source);
// Create a list of : subscripts to generate all elements.
subs = List.fill(DAE.WHOLEDIM(), listLength(dims));
then
expandCref2(id, correctTy, subs, dims);

// A simple cref with subscripts and array type.
case (DAE.CREF_IDENT(id, correctTy as DAE.T_ARRAY(ty=DAE.T_COMPLEX(varLst=varLst,complexClassType=ClassInf.RECORD(_)), dims = dims), subs),true)
// A array complex cref with subscripts. Expand record true
case (DAE.CREF_IDENT(id, ty as DAE.T_ARRAY(source=source), subs),true)
equation
// Flatten T_ARRAY(T_ARRAY(T_COMPLEX(), dim2,src), dim1,src) types to one level T_ARRAY(simpletype, alldims, src)
(basety as DAE.T_COMPLEX(varLst=varLst,complexClassType=ClassInf.RECORD(_)), dims) = Types.flattenArrayTypeOpt(ty);
correctTy = DAE.T_ARRAY(basety,dims,source);
// Use the subscripts to generate only the wanted elements.
crefs = expandCref2(id, correctTy, subs, dims);
then
expandCrefLst(crefs,varLst,{});

// A simple cref with subscripts and array type.
case (DAE.CREF_IDENT(id, correctTy as DAE.T_ARRAY( dims = dims), subs),_)
// A array type cref (possibly record but no expansion of records) with subscripts.
case (DAE.CREF_IDENT(id, ty as DAE.T_ARRAY(source=source), subs),_)
equation
// Flatten T_ARRAY(T_ARRAY(T_..., dim2,src), dim1,src) types to one level T_ARRAY(simpletype, alldims, src)
(basety, dims) = Types.flattenArrayTypeOpt(ty);
correctTy = DAE.T_ARRAY(basety,dims,source);
// Use the subscripts to generate only the wanted elements.
then
expandCref2(id, correctTy, subs, dims);


// A qualified cref with array type.
case (DAE.CREF_QUAL(id, ty as DAE.T_ARRAY(dims=dims, source=source), subs, cref),_)
case (DAE.CREF_QUAL(id, ty as DAE.T_ARRAY(source=source), subs, cref),_)
equation
// Expand the rest of the cref.
crefs = expandCref_impl(cref,expandRecord);
// Flatten T_ARRAY(T_ARRAY(T_..., dim2,src), dim1,src) types to one level T_ARRAY(simpletype, alldims, src)
(basety, dims) = Types.flattenArrayTypeOpt(ty);
correctTy = DAE.T_ARRAY(basety,dims,source);
// Create a simple identifier for the head of the cref and expand it.
ty = DAE.T_ARRAY(DAE.T_NONE_DEFAULT,dims,source);
cref = DAE.CREF_IDENT(id, ty, subs);
crefs2 = expandCref_impl(cref,expandRecord);
cref = DAE.CREF_IDENT(id, correctTy, subs);
crefs2 = expandCref_impl(cref,false);
crefs2 = listReverse(crefs2);
crefs2 = List.map1(crefs2,crefSetType,ty);
// crefs2 = List.map1(crefs2,crefSetType,correctTy);
// Create all combinations of the two lists.
crefs = expandCrefQual(crefs2, crefs, {});
then
Expand All @@ -2940,7 +2955,7 @@ algorithm
// All other cases, no expansion.
else {inCref};

end match;
end matchcontinue;
end expandCref_impl;

protected function expandCrefLst
Expand All @@ -2959,7 +2974,6 @@ algorithm
// Create a list of crefs from names
crefs = List.map(varLst,creffromVar);
crefs = List.map1r(crefs,joinCrefs,cr);
crefs = List.map1Flat(crefs,expandCref_impl,true);
then
expandCrefLst(rest,varLst,crefs::inCrefsAcc);
end match;
Expand Down
20 changes: 9 additions & 11 deletions Compiler/SimCode/SimCodeUtil.mo
Expand Up @@ -6265,26 +6265,24 @@ algorithm
// => tmp = rhsexp;
// z1 = tmp[1]; z2 = tmp[2] ....
case (_, (BackendDAE.ARRAY_EQUATION(dimSize=ds, left=(e1 as DAE.ARRAY()), right=e2, source=source))::{}, _, _, _, _) equation
(DAE.ARRAY(array=expLst)) = Expression.replaceDerOpInExp(e1);
// Flattne multi-dimensional ARRAY{ARRAY} expressions
expLst = Expression.flattenArrayExpToList(e1);
// Replace the der() operators
expLst = List.map(expLst, Expression.replaceDerOpInExp);
e2_1 = Expression.replaceDerOpInExp(e2);
//check that {z1,z2,...} are in e1
crefs = List.map(inVars, BackendVariable.varCref);
ht = HashSet.emptyHashSet();
ht = List.fold(crefs, BaseHashSet.add, ht);
List.foldAllValue(expLst, createSingleComplexEqnCode3, true, ht);

// create tmp vars and exps
// create the lhs tmp var
ty = Expression.typeof(e1);
left = ComponentReference.makeCrefIdent("$TMP_" + intString(iuniqueEqIndex), ty, {});
lhse = DAE.CREF(left,ty);

// Expand the tmp cref and create the list of rhs vars
// to update the original lhs vars
crefstmp = ComponentReference.expandCref(left, false);
expLstTmp = List.map(crefstmp, Expression.crefExp);
tempvars = createArrayTempVar(left, ds, expLstTmp, itempvars);

// Create the simple assignments for the lhs vars from the tmp rhs's
exptl = List.threadTuple(expLst, expLstTmp);
(eqSystlst, uniqueEqIndex) = List.map1Fold(exptl, makeSES_SIMPLE_ASSIGN, source, iuniqueEqIndex);

// Create the array equation with the tmp var as lhs
eqSystlst = SimCode.SES_ARRAY_CALL_ASSIGN(uniqueEqIndex, lhse, e2_1, source)::eqSystlst;
then (eqSystlst, eqSystlst, uniqueEqIndex+1, tempvars);

Expand Down

0 comments on commit a4fa7a9

Please sign in to comment.