Skip to content

Commit

Permalink
Fix for bug #1526:
Browse files Browse the repository at this point in the history
- Implemented support for logical operations (and, or, not) on arrays.
- Added test case mofiles/ArrayLogic.


git-svn-id: https://openmodelica.org/svn/OpenModelica/trunk@9029 f25d12d1-65f4-0310-ae8a-bbce733d8d8e
  • Loading branch information
perost committed May 18, 2011
1 parent b2186fe commit 920892f
Show file tree
Hide file tree
Showing 15 changed files with 239 additions and 138 deletions.
2 changes: 1 addition & 1 deletion Compiler/BackEnd/BackendDAEOptimize.mo
Expand Up @@ -4486,7 +4486,7 @@ algorithm
case (currVar::restVar, currDerVar::restDerVar, inState, functionCall as DAE.CALL(path=fname, expLst=varExpListTotal, tuple_=tuple_, builtin=builtin, ty=et, inlineType=inlineType))
equation
e = partialNumericalDifferentiation(restVar, restDerVar, inState, functionCall);
absCurr = DAE.LBINARY(DAE.RELATION(currVar,DAE.GREATER(DAE.ET_REAL()),DAE.RCONST(1e-8),-1,NONE()),DAE.OR(),DAE.RELATION(currVar,DAE.LESS(DAE.ET_REAL()),DAE.RCONST(-1e-8),-1,NONE()));
absCurr = DAE.LBINARY(DAE.RELATION(currVar,DAE.GREATER(DAE.ET_REAL()),DAE.RCONST(1e-8),-1,NONE()),DAE.OR(DAE.ET_BOOL()),DAE.RELATION(currVar,DAE.LESS(DAE.ET_REAL()),DAE.RCONST(-1e-8),-1,NONE()));
delta = DAE.IFEXP( absCurr, DAE.BINARY(currVar,DAE.MUL(DAE.ET_REAL()),DAE.RCONST(1e-8)), DAE.RCONST(1e-8));
nArgs1 = listLength(varExpListTotal);
nArgs2 = listLength(restVar);
Expand Down
2 changes: 1 addition & 1 deletion Compiler/BackEnd/BackendDAETransform.mo
Expand Up @@ -1838,7 +1838,7 @@ algorithm
dae = BackendEquation.equationAddDAE(deq,dae);

wc = BackendDAE.WHEN_CLAUSE(dxdyecont,{BackendDAE.REINIT(dsxy,ds1,DAE.emptyElementSource)},NONE());
wc1 = BackendDAE.WHEN_CLAUSE(DAE.LUNARY(DAE.NOT(),dxdyecont),{BackendDAE.REINIT(dsxy,ds2,DAE.emptyElementSource)},NONE());
wc1 = BackendDAE.WHEN_CLAUSE(DAE.LUNARY(DAE.NOT(DAE.ET_BOOL()),dxdyecont),{BackendDAE.REINIT(dsxy,ds2,DAE.emptyElementSource)},NONE());
dae = BackendDAEUtil.whenClauseAddDAE({wc,wc1},dae);
eqcont = BackendDAE.EQUATION(dxdyecont,cont1,DAE.emptyElementSource);
dae = BackendEquation.equationAddDAE(eqcont,dae);
Expand Down
2 changes: 1 addition & 1 deletion Compiler/BackEnd/BackendVariable.mo
Expand Up @@ -1538,7 +1538,7 @@ algorithm
DAE.Exp min,max;
case (SOME(min)::(SOME(max)::{}),e,tp)
then DAE.LBINARY(DAE.RELATION(e,DAE.GREATEREQ(tp),min,-1,NONE()),
DAE.AND(),
DAE.AND(DAE.ET_BOOL()),
DAE.RELATION(e,DAE.LESSEQ(tp),max,-1,NONE()));
case (SOME(min)::(NONE()::{}),e,tp)
then DAE.RELATION(e,DAE.GREATEREQ(tp),min,-1,NONE());
Expand Down
6 changes: 3 additions & 3 deletions Compiler/BackEnd/XMLDump.mo
Expand Up @@ -3431,8 +3431,8 @@ algorithm
outString:=
match (inOperator)
local String error_msg;
case (DAE.AND()) then MathMLAnd;
case (DAE.OR()) then MathMLOr;
case (DAE.AND(ty = _)) then MathMLAnd;
case (DAE.OR(ty = _)) then MathMLOr;
else
equation
error_msg = "in XMLDump.lbinopSymbol - Unknown operator";
Expand All @@ -3454,7 +3454,7 @@ algorithm
outString:=
match (inOperator)
local String error_msg;
case (DAE.NOT()) then MathMLNot;
case (DAE.NOT(ty = _)) then MathMLNot;
else
equation
error_msg = "in XMLDump.lunaryopSymbol - Unknown operator";
Expand Down
12 changes: 6 additions & 6 deletions Compiler/FrontEnd/Ceval.mo
Expand Up @@ -616,14 +616,14 @@ algorithm
// special case when leftside is false...
// We allow errors on right hand side. and even if there is no errors, the performance
// will be better.
case (cache,env,DAE.LBINARY(exp1 = lh,operator = DAE.AND(),exp2 = rh),impl,stOpt,dimOpt,msg)
case (cache,env,DAE.LBINARY(exp1 = lh,operator = DAE.AND(_),exp2 = rh),impl,stOpt,dimOpt,msg)
equation
(cache,Values.BOOL(false),stOpt) = ceval(cache,env, lh, impl, stOpt, dimOpt, msg);
then
(cache,Values.BOOL(false),stOpt);

// Logical lhs AND rhs
case (cache,env,DAE.LBINARY(exp1 = lh,operator = DAE.AND(),exp2 = rh),impl,stOpt,dimOpt,msg)
case (cache,env,DAE.LBINARY(exp1 = lh,operator = DAE.AND(_),exp2 = rh),impl,stOpt,dimOpt,msg)
equation
(cache,Values.BOOL(lhvBool),stOpt) = ceval(cache,env, lh, impl, stOpt, dimOpt, msg);
(cache,Values.BOOL(rhvBool),stOpt) = ceval(cache,env, rh, impl, stOpt, dimOpt, msg);
Expand All @@ -632,14 +632,14 @@ algorithm
(cache,Values.BOOL(resBool),stOpt);

// true OR rhs
case (cache,env,DAE.LBINARY(exp1 = lh,operator = DAE.OR(),exp2 = rh),impl,stOpt,dimOpt,msg)
case (cache,env,DAE.LBINARY(exp1 = lh,operator = DAE.OR(_),exp2 = rh),impl,stOpt,dimOpt,msg)
equation
(cache,Values.BOOL(true),stOpt) = ceval(cache,env, lh, impl, stOpt, dimOpt, msg);
then
(cache,Values.BOOL(true),stOpt);

// lhs OR rhs
case (cache,env,DAE.LBINARY(exp1 = lh,operator = DAE.OR(),exp2 = rh),impl,stOpt,dimOpt,msg)
case (cache,env,DAE.LBINARY(exp1 = lh,operator = DAE.OR(_),exp2 = rh),impl,stOpt,dimOpt,msg)
equation
(cache,Values.BOOL(lhvBool),stOpt) = ceval(cache,env, lh, impl, stOpt, dimOpt, msg);
(cache,Values.BOOL(rhvBool),stOpt) = ceval(cache,env, rh, impl, stOpt, dimOpt, msg);
Expand All @@ -650,15 +650,15 @@ algorithm
// Special case for a boolean expression like if( expression or ARRAY_IDEX_OUT_OF_BOUNDS_ERROR)
// "expression" in this case we return the lh expression to be equall to
// the previous c-code generation.
case (cache,env,DAE.LBINARY(exp1 = lh,operator = DAE.OR(),exp2 = rh),impl,stOpt,dimOpt,msg)
case (cache,env,DAE.LBINARY(exp1 = lh,operator = DAE.OR(_),exp2 = rh),impl,stOpt,dimOpt,msg)
equation
(cache,v as Values.BOOL(rhvBool),stOpt) = ceval(cache,env, lh, impl, stOpt, dimOpt, msg);
failure((_,_,_) = ceval(cache,env, rh, impl, stOpt, dimOpt, msg));
then
(cache,v,stOpt);

// NOT
case (cache,env,DAE.LUNARY(operator = DAE.NOT(),exp = e),impl,stOpt,dimOpt,msg)
case (cache,env,DAE.LUNARY(operator = DAE.NOT(_),exp = e),impl,stOpt,dimOpt,msg)
equation
(cache,Values.BOOL(b),stOpt) = ceval(cache,env, e, impl, stOpt, dimOpt, msg);
b_1 = boolNot(b);
Expand Down
2 changes: 1 addition & 1 deletion Compiler/FrontEnd/ConnectionGraph.mo
Expand Up @@ -1176,7 +1176,7 @@ algorithm
ExpressionDump.printExpStr(inExp) +& " = " +& Util.if_(result, "true", "false"));
then ((DAE.BCONST(result), roots));
// deal with NOT Connections.isRoot
case ((inExp as DAE.LUNARY(DAE.NOT(), DAE.CALL(path=Absyn.QUALIFIED("Connections", Absyn.IDENT("isRoot")),
case ((inExp as DAE.LUNARY(DAE.NOT(_), DAE.CALL(path=Absyn.QUALIFIED("Connections", Absyn.IDENT("isRoot")),
expLst={DAE.CREF(componentRef = cref)})), roots))
equation
result = Util.listContainsWithCompareFunc(cref, roots, ComponentReference.crefEqual);
Expand Down
12 changes: 9 additions & 3 deletions Compiler/FrontEnd/DAE.mo
Expand Up @@ -1425,11 +1425,17 @@ uniontype Operator "Operators which are overloaded in the abstract syntax are he
ExpType ty "type of the array";
end POW_ARR2;

record AND end AND;
record AND
ExpType ty;
end AND;

record OR end OR;
record OR
ExpType ty;
end OR;

record NOT end NOT;
record NOT
ExpType ty;
end NOT;

record LESS
ExpType ty;
Expand Down
6 changes: 3 additions & 3 deletions Compiler/FrontEnd/DAEDump.mo
Expand Up @@ -191,9 +191,9 @@ algorithm
case(DAE.POW_SCALAR_ARRAY(ty=ty)) then " POW_SCALAR_ARRAY ";
case(DAE.POW_ARR(ty=ty)) then " POW_ARR ";
case(DAE.POW_ARR2(ty=ty)) then " POW_ARR2 ";
case(DAE.OR()) then " OR ";
case(DAE.AND()) then " AND ";
case(DAE.NOT()) then " NOT ";
case(DAE.OR(_)) then " OR ";
case(DAE.AND(_)) then " AND ";
case(DAE.NOT(_)) then " NOT ";
case(DAE.LESSEQ(ty=ty)) then " LESSEQ ";
case(DAE.GREATER(ty=ty)) then " GREATER ";
case(DAE.GREATEREQ(ty=ty)) then " GREATEREQ ";
Expand Down
44 changes: 30 additions & 14 deletions Compiler/FrontEnd/Expression.mo
Expand Up @@ -338,9 +338,9 @@ algorithm
case(DAE.POW_ARRAY_SCALAR(_)) then Absyn.POW();
case(DAE.POW_ARR(_)) then Absyn.POW();
case(DAE.POW_ARR2(_)) then Absyn.POW();
case(DAE.AND()) then Absyn.AND();
case(DAE.OR()) then Absyn.OR();
case(DAE.NOT()) then Absyn.NOT();
case(DAE.AND(_)) then Absyn.AND();
case(DAE.OR(_)) then Absyn.OR();
case(DAE.NOT(_)) then Absyn.NOT();
case(DAE.LESS(_)) then Absyn.LESS();
case(DAE.LESSEQ(_)) then Absyn.LESSEQ();
case(DAE.GREATER(_)) then Absyn.GREATER();
Expand Down Expand Up @@ -955,9 +955,9 @@ algorithm
case (DAE.POW_SCALAR_ARRAY(ty = _), _) then DAE.POW_SCALAR_ARRAY(inType);
case (DAE.POW_ARR(ty = _), _) then DAE.POW_ARR(inType);
case (DAE.POW_ARR2(ty = _), _) then DAE.POW_ARR2(inType);
case (DAE.AND(), _) then DAE.AND();
case (DAE.OR(), _) then DAE.OR();
case (DAE.NOT(),_ ) then DAE.NOT();
case (DAE.AND(ty = _), _) then DAE.AND(inType);
case (DAE.OR(ty = _), _) then DAE.OR(inType);
case (DAE.NOT(ty = _),_ ) then DAE.NOT(inType);
case (DAE.LESS(ty = _), _) then inOp;
case (DAE.LESSEQ(ty = _), _) then inOp;
case (DAE.GREATER(ty = _), _) then inOp;
Expand Down Expand Up @@ -1345,6 +1345,22 @@ algorithm
end match;
end expLastSubs;

public function expDimensions
"Tries to return the dimensions from an expression, typically an array."
input Exp inExp;
output list<DAE.Dimension> outDims;
algorithm
outDims := match(inExp)
local
DAE.ExpType tp;
Exp e;

case DAE.ARRAY(ty = tp) then arrayDimension(tp);
case DAE.LUNARY(exp = e) then expDimensions(e);
case DAE.LBINARY(exp1 = e) then expDimensions(e);
end match;
end expDimensions;

public function arrayDimension "
Author BZ
Get dimension of array.
Expand Down Expand Up @@ -1647,9 +1663,9 @@ algorithm
case (DAE.POW_SCALAR_ARRAY(ty = t)) then t;
case (DAE.POW_ARR(ty = t)) then t;
case (DAE.POW_ARR2(ty = t)) then t;
case (DAE.AND()) then DAE.ET_BOOL();
case (DAE.OR()) then DAE.ET_BOOL();
case (DAE.NOT()) then DAE.ET_BOOL();
case (DAE.AND(ty = t)) then t;
case (DAE.OR(ty = t)) then t;
case (DAE.NOT(ty = t)) then t;
case (DAE.LESS(ty = t)) then t;
case (DAE.LESSEQ(ty = t)) then t;
case (DAE.GREATER(ty = t)) then t;
Expand Down Expand Up @@ -2627,8 +2643,8 @@ algorithm
list<DAE.Exp> rest,lst;
Operator op;
String str;
case ({},DAE.AND()) then DAE.BCONST(true);
case ({},DAE.OR()) then DAE.BCONST(false);
case ({},DAE.AND(_)) then DAE.BCONST(true);
case ({},DAE.OR(_)) then DAE.BCONST(false);
case ({e1},_) then e1;
case ({e1, e2},op) then DAE.LBINARY(e1,op,e2);
case ((e1 :: rest),op)
Expand Down Expand Up @@ -6517,9 +6533,9 @@ algorithm
case (DAE.POW_ARRAY_SCALAR(ty = _),DAE.POW_ARRAY_SCALAR(ty = _)) then true;
case (DAE.POW_ARR(ty = _),DAE.POW_ARR(ty = _)) then true;
case (DAE.POW_ARR2(ty = _),DAE.POW_ARR2(ty = _)) then true;
case (DAE.AND(),DAE.AND()) then true;
case (DAE.OR(),DAE.OR()) then true;
case (DAE.NOT(),DAE.NOT()) then true;
case (DAE.AND(ty = _),DAE.AND(ty = _)) then true;
case (DAE.OR(ty = _),DAE.OR(ty = _)) then true;
case (DAE.NOT(ty = _),DAE.NOT(ty = _)) then true;
case (DAE.LESS(ty = _),DAE.LESS(ty = _)) then true;
case (DAE.LESSEQ(ty = _),DAE.LESSEQ(ty = _)) then true;
case (DAE.GREATER(ty = _),DAE.GREATER(ty = _)) then true;
Expand Down
18 changes: 9 additions & 9 deletions Compiler/FrontEnd/ExpressionDump.mo
Expand Up @@ -320,8 +320,8 @@ public function lbinopSymbol
algorithm
outString:=
match (inOperator)
case (DAE.AND()) then " and ";
case (DAE.OR()) then " or ";
case (DAE.AND(_)) then " and ";
case (DAE.OR(_)) then " or ";
end match;
end lbinopSymbol;

Expand All @@ -332,7 +332,7 @@ public function lunaryopSymbol
output String outString;
algorithm
outString := match (inOperator)
case (DAE.NOT()) then " not ";
case (DAE.NOT(_)) then "not ";
end match;
end lunaryopSymbol;

Expand Down Expand Up @@ -1040,9 +1040,9 @@ algorithm
case (DAE.RELATION(operator = DAE.GREATEREQ(_))) then 6;
case (DAE.RELATION(operator = DAE.EQUAL(_))) then 6;
case (DAE.RELATION(operator = DAE.NEQUAL(_))) then 6;
case (DAE.LUNARY(operator = DAE.NOT())) then 7;
case (DAE.LBINARY(operator = DAE.AND())) then 8;
case (DAE.LBINARY(operator = DAE.OR())) then 9;
case (DAE.LUNARY(operator = DAE.NOT(_))) then 7;
case (DAE.LBINARY(operator = DAE.AND(_))) then 8;
case (DAE.LBINARY(operator = DAE.OR(_))) then 9;
case (DAE.RANGE(ty = _)) then 10;
case (DAE.IFEXP(expCond = _)) then 11;
case (DAE.TUPLE(_)) then 12; /* Not valid in inner expressions, only included here for completeness */
Expand Down Expand Up @@ -2276,8 +2276,8 @@ public function lbinopPriority
output Integer outInteger;
algorithm
outInteger := match (inOperator)
case (DAE.AND()) then 10;
case (DAE.OR()) then 10;
case (DAE.AND(_)) then 10;
case (DAE.OR(_)) then 10;
end match;
end lbinopPriority;

Expand All @@ -2289,7 +2289,7 @@ public function lunaryopPriority
output Integer outInteger;
algorithm
outInteger := match (inOperator)
case (DAE.NOT()) then 11;
case (DAE.NOT(_)) then 11;
end match;
end lunaryopPriority;

Expand Down
42 changes: 30 additions & 12 deletions Compiler/FrontEnd/ExpressionSimplify.mo
Expand Up @@ -350,7 +350,7 @@ algorithm
case (exp,DAE.BCONST(true),DAE.BCONST(false)) then exp;
case (exp,DAE.BCONST(false),DAE.BCONST(true))
equation
exp = DAE.LUNARY(DAE.NOT(), exp);
exp = DAE.LUNARY(DAE.NOT(DAE.ET_BOOL()), exp);
then exp;
// Are the branches equal?
case (cond,tb,fb)
Expand Down Expand Up @@ -553,19 +553,19 @@ algorithm
then e;
case (DAE.CALL(path=Absyn.IDENT("min"),ty=DAE.ET_BOOL(),expLst={e1,e2}))
equation
e = DAE.LBINARY(e1,DAE.AND(),e2);
e = DAE.LBINARY(e1,DAE.AND(DAE.ET_BOOL()),e2);
then e;
case (DAE.CALL(path=Absyn.IDENT("max"),ty=DAE.ET_BOOL(),expLst={e1,e2}))
equation
e = DAE.LBINARY(e1,DAE.OR(),e2);
e = DAE.LBINARY(e1,DAE.OR(DAE.ET_BOOL()),e2);
then e;
case (DAE.CALL(path=Absyn.IDENT("min"),ty=DAE.ET_ARRAY(DAE.ET_BOOL(),_),expLst={DAE.ARRAY(array=expl)}))
equation
e = Expression.makeLBinary(expl,DAE.AND());
e = Expression.makeLBinary(expl,DAE.AND(DAE.ET_BOOL()));
then e;
case (DAE.CALL(path=Absyn.IDENT("max"),ty=DAE.ET_ARRAY(DAE.ET_BOOL(),_),expLst={DAE.ARRAY(array=expl)}))
equation
e = Expression.makeLBinary(expl,DAE.OR());
e = Expression.makeLBinary(expl,DAE.OR(DAE.ET_BOOL()));
then e;

// cross
Expand Down Expand Up @@ -2551,6 +2551,14 @@ algorithm
then
exp;

case (DAE.LUNARY(operator = DAE.NOT(ty = t), exp = e), sub)
equation
e_1 = simplifyAsub(e, sub);
t2 = Expression.typeof(e_1);
exp = DAE.LUNARY(DAE.NOT(t2), e_1);
then
exp;

case (DAE.BINARY(exp1 = e1,operator = DAE.SUB_ARR(ty = t),exp2 = e2),sub)
equation
e1_1 = simplifyAsub(e1, sub);
Expand Down Expand Up @@ -2722,6 +2730,16 @@ algorithm
then
exp;

case (DAE.LBINARY(exp1 = e1, operator = op, exp2 = e2), sub)
equation
e1_1 = simplifyAsub(e1, sub);
e2_1 = simplifyAsub(e2, sub);
t2 = Expression.typeof(e1_1);
op = Expression.setOpType(op, t2);
exp = DAE.LBINARY(e1_1, op, e2_1);
then
exp;

case (DAE.ARRAY(ty = t,scalar = b,array = exps),sub)
equation
indx = Expression.expInt(sub);
Expand Down Expand Up @@ -2956,15 +2974,15 @@ algorithm
then
DAE.BCONST(b);

case(DAE.AND(),exp1,exp2)
case(DAE.AND(DAE.ET_BOOL()),exp1,exp2)
equation
b1 = Expression.getBoolConst(exp1);
b2 = Expression.getBoolConst(exp2);
b = b1 and b2;
then
DAE.BCONST(b);

case(DAE.OR(),exp1,exp2)
case(DAE.OR(DAE.ET_BOOL()),exp1,exp2)
equation
b1 = Expression.getBoolConst(exp1);
b2 = Expression.getBoolConst(exp2);
Expand Down Expand Up @@ -3428,11 +3446,11 @@ algorithm
DAE.BCONST(false);

// true AND e => e
case (DAE.AND(),e1 as DAE.BCONST(b),e2) then Util.if_(b,e2,e1);
case (DAE.AND(),e1,e2 as DAE.BCONST(b)) then Util.if_(b,e1,e2);
case (DAE.AND(_),e1 as DAE.BCONST(b),e2) then Util.if_(b,e2,e1);
case (DAE.AND(_),e1,e2 as DAE.BCONST(b)) then Util.if_(b,e1,e2);
// false OR e => e
case (DAE.OR(),e1 as DAE.BCONST(b),e2) then Util.if_(b,e1,e2);
case (DAE.OR(),e1,e2 as DAE.BCONST(b)) then Util.if_(b,e2,e1);
case (DAE.OR(_),e1 as DAE.BCONST(b),e2) then Util.if_(b,e1,e2);
case (DAE.OR(_),e1,e2 as DAE.BCONST(b)) then Util.if_(b,e2,e1);
end matchcontinue;
end simplifyBinary;

Expand Down Expand Up @@ -3487,7 +3505,7 @@ algorithm
Boolean b1;

// not true => false, not false => true
case(DAE.NOT(),e1)
case(DAE.NOT(DAE.ET_BOOL()),e1)
equation
b1 = Expression.getBoolConst(e1);
b1 = not b1;
Expand Down

0 comments on commit 920892f

Please sign in to comment.