Skip to content

Commit

Permalink
Make special case for BackendVariable.getVarSingle
Browse files Browse the repository at this point in the history
BackendVariable.getVar returns a list of often size 1, and is expected
to return a list of exactly size 1. This checks that the size is 1
directly in the call to getVarSingle. Also uses isPresent to avoid
constructing the indexes list if it is not desired.
  • Loading branch information
sjoelund authored and OpenModelica-Hudson committed Apr 20, 2016
1 parent a4a6b3a commit 25cc4ea
Show file tree
Hide file tree
Showing 13 changed files with 171 additions and 65 deletions.
6 changes: 3 additions & 3 deletions Compiler/BackEnd/BackendDAECreate.mo
Expand Up @@ -2673,17 +2673,17 @@ algorithm
Integer i;
case(_, _, _, _)
equation
(v::{}, i::{}) = BackendVariable.getVar(cr, iVars);
(v, i) = BackendVariable.getVarSingle(cr, iVars);
then
(v, i, 1);
case(_, _, _, _)
equation
(v::{}, i::{}) = BackendVariable.getVar(cr, iKnVars);
(v, i) = BackendVariable.getVarSingle(cr, iKnVars);
then
(v, i, 2);
case(_, _, _, _)
equation
(v::{}, i::{}) = BackendVariable.getVar(cr, iExtVars);
(v, i) = BackendVariable.getVarSingle(cr, iExtVars);
then
(v, i, 3);
end matchcontinue;
Expand Down
24 changes: 12 additions & 12 deletions Compiler/BackEnd/BackendDAEOptimize.mo
Expand Up @@ -172,17 +172,17 @@ algorithm
Boolean negate;

case (DAE.CALL(path=Absyn.IDENT(name="der"), expLst={DAE.CREF(componentRef=cr, ty=tp)}), (knvars, aliasvars, _)) equation
(var::{}, _) = BackendVariable.getVar(cr, knvars);
(var, _) = BackendVariable.getVarSingle(cr, knvars);
false = BackendVariable.isVarOnTopLevelAndInput(var);
(zero, _) = Expression.makeZeroExpression(Expression.arrayDimension(tp));
then (zero, (knvars, aliasvars, true));

case (DAE.CALL(path=Absyn.IDENT(name="pre"), expLst={e as DAE.CREF(componentRef=cr)}), (knvars, aliasvars, _)) equation
(_::{}, _) = BackendVariable.getVar(cr, knvars);
(_, _) = BackendVariable.getVarSingle(cr, knvars);
then(e, (knvars, aliasvars, true));

case (DAE.CALL(path=Absyn.IDENT(name="previous"), expLst={e as DAE.CREF(componentRef=cr)}), (knvars, aliasvars, _)) equation
(_::{}, _) = BackendVariable.getVar(cr, knvars);
(_, _) = BackendVariable.getVarSingle(cr, knvars);
then(e, (knvars, aliasvars, true));

case (DAE.CALL(path=Absyn.IDENT(name="pre"), expLst={e as DAE.CREF(componentRef=DAE.CREF_IDENT(ident="time"))}), (knvars, aliasvars, _))
Expand All @@ -198,7 +198,7 @@ algorithm
then (e, (knvars, aliasvars, true));

case (DAE.CALL(path=Absyn.IDENT(name="pre"), expLst={DAE.CREF(componentRef=cr, ty=tp)}, attr=attr), (knvars, aliasvars, _)) equation
(var::{}, _) = BackendVariable.getVar(cr, aliasvars);
(var, _) = BackendVariable.getVarSingle(cr, aliasvars);
(cr, negate) = BackendVariable.getAlias(var);
e = DAE.CREF(cr, tp);
e = if negate then Expression.negate(e) else e;
Expand All @@ -207,7 +207,7 @@ algorithm
then (e, (knvars, aliasvars, true));

case (DAE.CALL(path=Absyn.IDENT(name="previous"), expLst={DAE.CREF(componentRef=cr, ty=tp)}, attr=attr), (knvars, aliasvars, _)) equation
(var::{}, _) = BackendVariable.getVar(cr, aliasvars);
(var, _) = BackendVariable.getVarSingle(cr, aliasvars);
(cr, negate) = BackendVariable.getAlias(var);
e = DAE.CREF(cr, tp);
e = if negate then Expression.negate(e) else e;
Expand All @@ -229,7 +229,7 @@ algorithm
then (DAE.BCONST(false), (knvars, aliasvars, true));

case (DAE.CALL(path=Absyn.IDENT(name="change"), expLst={DAE.CREF(componentRef=cr, ty=tp)}, attr=attr), (knvars, aliasvars, _)) equation
(var::{}, _) = BackendVariable.getVar(cr, aliasvars);
(var, _) = BackendVariable.getVarSingle(cr, aliasvars);
(cr, negate) = BackendVariable.getAlias(var);
e = DAE.CREF(cr, tp);
e = if negate then Expression.negate(e) else e;
Expand All @@ -238,15 +238,15 @@ algorithm
then (e, (knvars, aliasvars, true));

case (DAE.CALL(path=Absyn.IDENT(name="edge"), expLst={DAE.CREF(componentRef=cr, ty=tp)}), (knvars, aliasvars, _)) equation
(_::{}, _) = BackendVariable.getVar(cr, knvars);
(_, _) = BackendVariable.getVarSingle(cr, knvars);
zero = Expression.arrayFill(Expression.arrayDimension(tp), DAE.BCONST(false));
then (zero, (knvars, aliasvars, true));

case (DAE.CALL(path=Absyn.IDENT(name="edge"), expLst={DAE.CREF(componentRef=DAE.CREF_IDENT(ident="time"))}), (knvars, aliasvars, _))
then (DAE.BCONST(false), (knvars, aliasvars, true));

case (DAE.CALL(path=Absyn.IDENT(name="edge"), expLst={DAE.CREF(componentRef=cr, ty=tp)}, attr=attr), (knvars, aliasvars, _)) equation
(var::{}, _) = BackendVariable.getVar(cr, aliasvars);
(var, _) = BackendVariable.getVarSingle(cr, aliasvars);
(cr, negate) = BackendVariable.getAlias(var);
e = DAE.CREF(cr, tp);
e = if negate then Expression.negate(e) else e;
Expand Down Expand Up @@ -2323,7 +2323,7 @@ algorithm
DAE.Exp e;
case (DAE.CREF(componentRef = cr),(knvars,_))
equation
(v::{},_::{}) = BackendVariable.getVar(cr,knvars);
(v,_) = BackendVariable.getVarSingle(cr,knvars);
true = BackendVariable.isFinalVar(v);
e = BackendVariable.varBindExpStartValue(v);
then (e,(knvars,true));
Expand Down Expand Up @@ -3810,7 +3810,7 @@ algorithm
then (exp,vars);
case (e1 as DAE.CALL(path=Absyn.IDENT(name = "der"), expLst={DAE.CREF(componentRef=cr)}))
equation
({v}, _) = BackendVariable.getVar(cr, vars);
(v, _) = BackendVariable.getVarSingle(cr, vars);
(vars, e1) = updateStatesVar(vars, v, e1);
then (e1, vars);
case (e1 as DAE.CALL(path=Absyn.IDENT(name = "der"), expLst={DAE.CREF(componentRef=cr)}))
Expand Down Expand Up @@ -3844,7 +3844,7 @@ algorithm
DAE.Exp e;
case (e as DAE.CALL(path = Absyn.IDENT(name = "der"), expLst = {DAE.CREF(componentRef = cr)}), vars)
equation
({v}, _) = BackendVariable.getVar(cr, vars);
(v, _) = BackendVariable.getVarSingle(cr, vars);
(vars, e) = updateStatesVar(vars, v, e);
then (e, vars);
case (e as DAE.CALL(path = Absyn.IDENT(name = "der"), expLst = {DAE.CREF(componentRef = cr)}), vars)
Expand Down Expand Up @@ -5268,7 +5268,7 @@ algorithm
String str;

case (DAE.CALL(path=Absyn.IDENT(name="der"), expLst={DAE.CREF(componentRef=cr, ty=ty)}), (vars, eqnLst, shared, addVar, _)) equation
({v}, _) = BackendVariable.getVar(cr, vars);
(v, _) = BackendVariable.getVarSingle(cr, vars);
cref = BackendVariable.varCref(v);
v1 = BackendVariable.createAliasDerVar(cref);
v1 = BackendVariable.mergeNominalAttribute(v, v1, false);
Expand Down
4 changes: 2 additions & 2 deletions Compiler/BackEnd/BackendEquation.mo
Expand Up @@ -2375,9 +2375,9 @@ algorithm

case(_,e1 as DAE.CREF(componentRef = cr)) algorithm
try
({v}, _) := BackendVariable.getVar(cr, inVars);
(v, _) := BackendVariable.getVarSingle(cr, inVars);
else
({v}, _) := BackendVariable.getVar(cr, knvars);
(v, _) := BackendVariable.getVarSingle(cr, knvars);
end try;

lhs := ComponentReference.makeCrefIdent(conCrefName, DAE.T_REAL_DEFAULT, {});
Expand Down
109 changes: 95 additions & 14 deletions Compiler/BackEnd/BackendVariable.mo
Expand Up @@ -773,7 +773,7 @@ public function isDiscrete
protected
BackendDAE.Var v;
algorithm
({v},_) := getVar(cr,vars);
(v,_) := getVarSingle(cr,vars);
outBoolean := isVarDiscrete(v);
end isDiscrete;

Expand Down Expand Up @@ -2722,11 +2722,16 @@ algorithm
equation
(v,indx) = getVar2(cr, inVariables) "if scalar found, return it";
then
({v},{indx});
({v},if isPresent(outIntegerLst) then {indx} else {});
case (_,_) /* check if array or record */
equation
crlst = ComponentReference.expandCref(cr,true);
(vLst as _::_,indxs) = getVarLst(crlst,inVariables);
if isPresent(outIntegerLst) then
(vLst as _::_,indxs) = getVarLst(crlst,inVariables);
else
(vLst as _::_,_) = getVarLst(crlst,inVariables);
indxs = {};
end if;
then
(vLst,indxs);
// try again check if variable indexes used
Expand All @@ -2735,7 +2740,12 @@ algorithm
// replace variables with WHOLEDIM()
(cr1,true) = replaceVarWithWholeDim(cr, false);
crlst = ComponentReference.expandCref(cr1,true);
(vLst as _::_,indxs) = getVarLst(crlst,inVariables);
if isPresent(outIntegerLst) then
(vLst as _::_,indxs) = getVarLst(crlst,inVariables);
else
(vLst as _::_,_) = getVarLst(crlst,inVariables);
indxs = {};
end if;
then
(vLst,indxs);
/* failure
Expand All @@ -2748,6 +2758,66 @@ algorithm
end matchcontinue;
end getVar;

public function getVarSingle
" Return a variable and its index in the vector.
The indexes is enumerated from 1..n
Normally a variable has only one index, but in case of an array variable
it may have several indexes and several scalar variables,
therefore a list of variables and a list of indexes is returned.

This function fails if there are more than a single returned value"
input DAE.ComponentRef cr;
input BackendDAE.Variables inVariables;
output BackendDAE.Var outVar;
output Integer outInteger;
algorithm
(outVar,outInteger) := matchcontinue (cr,inVariables)
local
BackendDAE.Var v;
Integer indx;
list<Integer> indxs;
list<BackendDAE.Var> vLst;
list<DAE.ComponentRef> crlst;
DAE.ComponentRef cr1;
case (_,_)
equation
(v,indx) = getVar2(cr, inVariables) "if scalar found, return it";
then (v,indx);
case (_,_) /* check if array or record */
equation
// TODO: Don't expand if > length 1
crlst = ComponentReference.expandCref(cr,true);
if isPresent(outInteger) then
({v},{indx}) = getVarLst(crlst,inVariables);
else
({v},_) = getVarLst(crlst,inVariables);
indx = 0;
end if;
then (v,indx);
// try again check if variable indexes used
case (_,_)
equation
// TODO: Don't expand if > length 1
// replace variables with WHOLEDIM()
(cr1,true) = replaceVarWithWholeDim(cr, false);
crlst = ComponentReference.expandCref(cr1,true);
if isPresent(outInteger) then
({v},{indx}) = getVarLst(crlst,inVariables);
else
({v},_) = getVarLst(crlst,inVariables);
indx = 0;
end if;
then (v,indx);
/* failure
case (_,_)
equation
fprintln(Flags.DAE_LOW, "- getVar failed on component reference: " + ComponentReference.printComponentRefStr(cr));
then
fail();
*/
end matchcontinue;
end getVarSingle;

protected function replaceVarWithWholeDim
"Helper function to traverseExp. Traverses any expressions in a
component reference (i.e. in it's subscripts)."
Expand Down Expand Up @@ -2862,15 +2932,26 @@ protected
BackendDAE.Var v;
Integer indx;
algorithm
for cr in inComponentRefLst loop
try
(v,indx) := getVar2(cr, inVariables);
outVarLst := v::outVarLst;
outIntegerLst := indx::outIntegerLst;
else
// skip this element
end try;
end for;
if isPresent(outIntegerLst) then
for cr in inComponentRefLst loop
try
(v,indx) := getVar2(cr, inVariables);
outVarLst := v::outVarLst;
outIntegerLst := indx::outIntegerLst;
else
// skip this element
end try;
end for;
else
for cr in inComponentRefLst loop
try
(v,indx) := getVar2(cr, inVariables);
outVarLst := v::outVarLst;
else
// skip this element
end try;
end for;
end if;
end getVarLst;

public function getVar2
Expand Down Expand Up @@ -3550,7 +3631,7 @@ algorithm
equation
// check for cyclic bindings in start value
false = BaseHashSet.has(cr, hs);
({BackendDAE.VAR(bindExp = SOME(e))}, _) = getVar(cr, vars);
(BackendDAE.VAR(bindExp = SOME(e)), _) = getVarSingle(cr, vars);
hs = BaseHashSet.add(cr,hs);
(e, (_,_,hs)) = Expression.traverseExpBottomUp(e, replaceCrefWithBindExp, (vars,false,hs));
then (e, (vars,true,hs));
Expand Down
8 changes: 4 additions & 4 deletions Compiler/BackEnd/Differentiate.mo
Expand Up @@ -1110,7 +1110,7 @@ algorithm
case ((DAE.CREF(componentRef = cr, ty = tp)), _, BackendDAE.DIFFINPUTDATA(knownVars=SOME(knvars)), _, _)
equation
//print("\nExp-Cref\n known vars: " + ExpressionDump.printExpStr(e));
(var::{},_) = BackendVariable.getVar(cr, knvars);
(var,_) = BackendVariable.getVarSingle(cr, knvars);
false = BackendVariable.isVarOnTopLevelAndInput(var);
(zero,_) = Expression.makeZeroExpression(Expression.arrayDimension(tp));

Expand All @@ -1121,7 +1121,7 @@ algorithm
// d(discrete)/d(x) = 0
case ((DAE.CREF(componentRef = cr,ty = tp)), _, BackendDAE.DIFFINPUTDATA(allVars=SOME(timevars)), _, _)
equation
({BackendDAE.VAR(varKind = kind)},_) = BackendVariable.getVar(cr, timevars);
(BackendDAE.VAR(varKind = kind),_) = BackendVariable.getVarSingle(cr, timevars);
//print("\nExp-Cref\n known vars: " + ComponentReference.printComponentRefStr(cr));
true = listMember(kind,{BackendDAE.DISCRETE()}) or not Types.isReal(tp);
(zero,_) = Expression.makeZeroExpression(Expression.arrayDimension(tp));
Expand All @@ -1139,7 +1139,7 @@ algorithm
//se1 = ExpressionDump.printExpStr(e);
//print("\nExp-Cref\nDUMMY_STATE: " + se1);

({var},_) = BackendVariable.getVar(cr, timevars);
(var,_) = BackendVariable.getVarSingle(cr, timevars);
true = BackendVariable.isDummyStateVar(var);
cr = ComponentReference.crefPrefixDer(cr);
res = Expression.makeCrefExp(cr, tp);
Expand All @@ -1156,7 +1156,7 @@ algorithm
//print("\nExp-Cref\n all other vars: " + se1);

//({BackendDAE.VAR(varKind = BackendDAE.STATE(index=_))},_) = BackendVariable.getVar(cr, timevars);
({_},_) = BackendVariable.getVar(cr, timevars);
(_,_) = BackendVariable.getVarSingle(cr, timevars);
res = DAE.CALL(Absyn.IDENT("der"),{e},DAE.CALL_ATTR(tp,false,true,false,false,DAE.NO_INLINE(),DAE.NO_TAIL()));

//se1 = ExpressionDump.printExpStr(res);
Expand Down
6 changes: 3 additions & 3 deletions Compiler/BackEnd/DynamicOptimization.mo
Expand Up @@ -473,7 +473,7 @@ algorithm

case (DAE.CALL(path=Absyn.IDENT(name = "der"),expLst={DAE.CREF(componentRef=cr)}),(vars,lst,lst1,varLst))
equation
(var::{},_) = BackendVariable.getVar(cr, vars);
(var,_) = BackendVariable.getVarSingle(cr, vars);
true = BackendVariable.isVarOnTopLevelAndInput(var);
cr1 = ComponentReference.prependStringCref("$TMP$DER$P", cr);
//cr1 = ComponentReference.crefPrefixDer(cr);
Expand Down Expand Up @@ -834,11 +834,11 @@ algorithm
vars := BackendVariable.setVarAt(vars, vindx, var_con);

try
(_,{vindx}) := BackendVariable.getVar(cr, vars);
(_,vindx) := BackendVariable.getVarSingle(cr, vars);
vars := BackendVariable.setVarAt(vars, vindx, var_);
//print("var" + BackendDump.varString(var_));
else
(_,{vindx}) := BackendVariable.getVar(cr, knownVars);
(_,vindx) := BackendVariable.getVarSingle(cr, knownVars);
knownVars := BackendVariable.setVarAt(knownVars, vindx, var_);
//print("var" + BackendDump.varString(var_));
end try;
Expand Down
2 changes: 1 addition & 1 deletion Compiler/BackEnd/EvaluateParameter.mo
Expand Up @@ -752,7 +752,7 @@ algorithm
equation
// check for cyclic bindings in start value
false = BaseHashSet.has(cr, hs);
({v}, _) = BackendVariable.getVar(cr, vars);
(v, _) = BackendVariable.getVarSingle(cr, vars);
e = BackendVariable.varStartValueType(v);
hs = BaseHashSet.add(cr,hs);
(e, (_,b,hs)) = Expression.traverseExpBottomUp(e, replaceCrefWithBindStartExp, (vars,b,hs));
Expand Down

0 comments on commit 25cc4ea

Please sign in to comment.