Skip to content

Commit

Permalink
- Added support for boolean min/max operators
Browse files Browse the repository at this point in the history
git-svn-id: https://openmodelica.org/svn/OpenModelica/trunk@7952 f25d12d1-65f4-0310-ae8a-bbce733d8d8e
  • Loading branch information
sjoelund committed Feb 17, 2011
1 parent 805990a commit 8740ef5
Show file tree
Hide file tree
Showing 5 changed files with 146 additions and 58 deletions.
118 changes: 78 additions & 40 deletions Compiler/FrontEnd/Ceval.mo
Expand Up @@ -3702,7 +3702,7 @@ algorithm
(outCache,outValue,outInteractiveInteractiveSymbolTableOption):=
matchcontinue (inCache,inEnv,inExpExpLst,inBoolean,inInteractiveInteractiveSymbolTableOption,inMsg)
local
Values.Value v,v_1;
Values.Value v,v1,v2,v_1;
list<Env.Frame> env;
DAE.Exp arr,s1,s2;
Boolean impl;
Expand All @@ -3714,27 +3714,46 @@ algorithm
case (cache,env,{arr},impl,st,msg)
equation
(cache,v,_) = ceval(cache,env, arr, impl, st,NONE(), msg);
(v_1) = cevalBuiltinMax2(v);
(v_1) = cevalBuiltinMaxArr(v);
then
(cache,v_1,st);
case (cache,env,{s1,s2},impl,st,msg)
equation
(cache,Values.INTEGER(i1),_) = ceval(cache,env, s1, impl, st,NONE(), msg);
(cache,Values.INTEGER(i2),_) = ceval(cache,env, s2, impl, st,NONE(), msg);
i = intMax(i1, i2);
then
(cache,Values.INTEGER(i),st);
case (cache,env,{s1,s2},impl,st,msg)
equation
(cache,Values.REAL(r1),_) = ceval(cache,env, s1, impl, st,NONE(), msg);
(cache,Values.REAL(r2),_) = ceval(cache,env, s2, impl, st,NONE(), msg);
r = realMax(r1, r2);
(cache,v1,_) = ceval(cache,env, s1, impl, st,NONE(), msg);
(cache,v2,_) = ceval(cache,env, s2, impl, st,NONE(), msg);
v = cevalBuiltinMax2(v1,v2);
then
(cache,Values.REAL(r),st);
(cache,v,st);
end matchcontinue;
end cevalBuiltinMax;

protected function cevalBuiltinMax2 "function: cevalBuiltinMax2
protected function cevalBuiltinMax2
input Values.Value v1;
input Values.Value v2;
output Values.Value outValue;
algorithm
outValue := match (v1,v2)
local
Integer i1,i2,i;
Real r1,r2,r;
Boolean b1,b2,b;
String s1,s2,s;
case (Values.INTEGER(i1),Values.INTEGER(i2))
equation
i = intMax(i1, i2);
then Values.INTEGER(i);
case (Values.REAL(r1),Values.REAL(r2))
equation
r = realMax(r1, r2);
then Values.REAL(r);
case (Values.BOOL(b1),Values.BOOL(b2))
equation
b = boolOr(b1, b2);
then Values.BOOL(b);
end match;
end cevalBuiltinMax2;

protected function cevalBuiltinMaxArr "function: cevalBuiltinMax2
Helper function to cevalBuiltinMax."
input Values.Value inValue;
output Values.Value outValue;
Expand All @@ -3752,23 +3771,23 @@ algorithm

case (Values.ARRAY(valueLst = (v1 :: (vls as (_ :: _)))))
equation
(Values.INTEGER(i1)) = cevalBuiltinMax2(v1);
(Values.INTEGER(i2)) = cevalBuiltinMax2(ValuesUtil.makeArray(vls));
(Values.INTEGER(i1)) = cevalBuiltinMaxArr(v1);
(Values.INTEGER(i2)) = cevalBuiltinMaxArr(ValuesUtil.makeArray(vls));
resI = intMax(i1, i2);
then
Values.INTEGER(resI);

case (Values.ARRAY(valueLst = (v1 :: (vls as (_ :: _)))))
equation
(Values.REAL(r1)) = cevalBuiltinMax2(v1);
(Values.REAL(r2)) = cevalBuiltinMax2(ValuesUtil.makeArray(vls));
(Values.REAL(r1)) = cevalBuiltinMaxArr(v1);
(Values.REAL(r2)) = cevalBuiltinMaxArr(ValuesUtil.makeArray(vls));
resR = realMax(r1, r2);
then
Values.REAL(resR);

case (Values.ARRAY(valueLst = {vl}))
equation
(v) = cevalBuiltinMax2(vl);
(v) = cevalBuiltinMaxArr(vl);
then
v;

Expand All @@ -3778,7 +3797,7 @@ algorithm
then
fail();
end matchcontinue;
end cevalBuiltinMax2;
end cevalBuiltinMaxArr;

protected function cevalBuiltinMin "function: cevalBuiltinMin
author: PA
Expand All @@ -3796,7 +3815,7 @@ algorithm
(outCache,outValue,outInteractiveInteractiveSymbolTableOption):=
matchcontinue (inCache,inEnv,inExpExpLst,inBoolean,inInteractiveInteractiveSymbolTableOption,inMsg)
local
Values.Value v,v_1;
Values.Value v,v1,v2,v_1;
list<Env.Frame> env;
DAE.Exp arr,s1,s2;
Boolean impl;
Expand All @@ -3808,27 +3827,46 @@ algorithm
case (cache,env,{arr},impl,st,msg)
equation
(cache,v,_) = ceval(cache,env, arr, impl, st,NONE(), msg);
(v_1) = cevalBuiltinMin2(v);
(v_1) = cevalBuiltinMinArr(v);
then
(cache,v_1,st);
case (cache,env,{s1,s2},impl,st,msg)
equation
(cache,Values.INTEGER(i1),_) = ceval(cache,env, s1, impl, st,NONE(), msg);
(cache,Values.INTEGER(i2),_) = ceval(cache,env, s2, impl, st,NONE(), msg);
i = intMin(i1, i2);
then
(cache,Values.INTEGER(i),st);
case (cache,env,{s1,s2},impl,st,msg)
equation
(cache,Values.REAL(r1),_) = ceval(cache,env, s1, impl, st,NONE(), msg);
(cache,Values.REAL(r2),_) = ceval(cache,env, s2, impl, st,NONE(), msg);
r = realMin(r1, r2);
(cache,v1,_) = ceval(cache,env, s1, impl, st,NONE(), msg);
(cache,v2,_) = ceval(cache,env, s2, impl, st,NONE(), msg);
v = cevalBuiltinMin2(v1,v2);
then
(cache,Values.REAL(r),st);
(cache,v,st);
end matchcontinue;
end cevalBuiltinMin;

protected function cevalBuiltinMin2 "function: cevalBuiltinMin2
protected function cevalBuiltinMin2
input Values.Value v1;
input Values.Value v2;
output Values.Value outValue;
algorithm
outValue := match (v1,v2)
local
Integer i1,i2,i;
Real r1,r2,r;
Boolean b1,b2,b;
String s1,s2,s;
case (Values.INTEGER(i1),Values.INTEGER(i2))
equation
i = intMin(i1, i2);
then Values.INTEGER(i);
case (Values.REAL(r1),Values.REAL(r2))
equation
r = realMin(r1, r2);
then Values.REAL(r);
case (Values.BOOL(b1),Values.BOOL(b2))
equation
b = boolAnd(b1, b2);
then Values.BOOL(b);
end match;
end cevalBuiltinMin2;

protected function cevalBuiltinMinArr "function: cevalBuiltinMinArr
Helper function to cevalBuiltinMin."
input Values.Value inValue;
output Values.Value outValue;
Expand All @@ -3845,28 +3883,28 @@ algorithm

case (Values.ARRAY(valueLst = (v1 :: (vls as (_ :: _)))))
equation
(Values.INTEGER(i1)) = cevalBuiltinMin2(v1);
(Values.INTEGER(i2)) = cevalBuiltinMin2(ValuesUtil.makeArray(vls));
(Values.INTEGER(i1)) = cevalBuiltinMinArr(v1);
(Values.INTEGER(i2)) = cevalBuiltinMinArr(ValuesUtil.makeArray(vls));
resI = intMin(i1, i2);
then
Values.INTEGER(resI);

case (Values.ARRAY(valueLst = (v1 :: (vls as (_ :: _)))))
equation
(Values.REAL(r1)) = cevalBuiltinMin2(v1);
(Values.REAL(r2)) = cevalBuiltinMin2(ValuesUtil.makeArray(vls));
(Values.REAL(r1)) = cevalBuiltinMinArr(v1);
(Values.REAL(r2)) = cevalBuiltinMinArr(ValuesUtil.makeArray(vls));
resR = realMin(r1, r2);
then
Values.REAL(resR);

case (Values.ARRAY(valueLst = {vl}))
equation
(v) = cevalBuiltinMin2(vl);
(v) = cevalBuiltinMinArr(vl);
then
v;

end matchcontinue;
end cevalBuiltinMin2;
end cevalBuiltinMinArr;

protected function cevalBuiltinDifferentiate "function cevalBuiltinDifferentiate
author: LP
Expand Down
29 changes: 29 additions & 0 deletions Compiler/FrontEnd/Expression.mo
Expand Up @@ -2535,6 +2535,35 @@ algorithm
end matchcontinue;
end makeDiff;

public function makeLBinary
"Makes a binary logical expression of all elements in the list."
input list<DAE.Exp> inExpLst;
input DAE.Operator op;
output DAE.Exp outExp;
algorithm
outExp := match (inExpLst,op)
local
DAE.Exp e1,e2,res;
list<DAE.Exp> rest,lst;
Operator op;
String str;
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)
equation
res = makeLBinary(rest,op);
res = DAE.LBINARY(e1,op,res);
then res;
else
equation
str = "Expression.makeLBinary failed for operator " +& ExpressionDump.lbinopSymbol(op);
Error.addMessage(Error.INTERNAL_ERROR, {str});
then fail();
end match;
end makeLBinary;

public function makeSum
"function: makeSum
Takes a list of expressions an makes a sum
Expand Down
16 changes: 16 additions & 0 deletions Compiler/FrontEnd/ExpressionSimplify.mo
Expand Up @@ -554,6 +554,22 @@ algorithm
equation
e = Expression.makeBuiltinCall("max",{e1,e2},tp);
then simplify1(e);
case (DAE.CALL(path=Absyn.IDENT("min"),ty=DAE.ET_BOOL(),expLst={e1,e2}))
equation
e = DAE.LBINARY(e1,DAE.AND(),e2);
then simplify1(e);
case (DAE.CALL(path=Absyn.IDENT("max"),ty=DAE.ET_BOOL(),expLst={e1,e2}))
equation
e = DAE.LBINARY(e1,DAE.OR(),e2);
then simplify1(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());
then simplify1(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());
then simplify1(e);

// cross
case (e as DAE.CALL(path = Absyn.IDENT("cross"), builtin = true, expLst = expl))
Expand Down
22 changes: 4 additions & 18 deletions Compiler/FrontEnd/Static.mo
Expand Up @@ -4443,37 +4443,23 @@ algorithm
then
(cache, call, DAE.PROP(elt_ty,c));

// min|max(x,y) where x & y are Real scalars.
// min|max(x,y) where x & y are scalars.
case (cache, env, _, {s1, s2}, impl, pre, info)
equation
(cache, s1_1, DAE.PROP(ty1, c1), _) =
elabExp(cache, env, s1, impl,NONE(), true, pre, info);
(cache, s2_1, DAE.PROP(ty2, c2), _) =
elabExp(cache, env, s2, impl,NONE(), true, pre, info);

true = Types.isRealOrSubTypeReal(ty1) or Types.isRealOrSubTypeReal(ty2);
(s1_1,ty) = Types.matchType(s1_1, ty1, DAE.T_REAL_DEFAULT, true);
(s2_1,ty) = Types.matchType(s2_1, ty2, DAE.T_REAL_DEFAULT, true);
ty = Types.scalarSuperType(ty1,ty2);
(s1_1,_) = Types.matchType(s1_1, ty1, ty, true);
(s2_1,_) = Types.matchType(s2_1, ty2, ty, true);
c = Types.constAnd(c1, c2);
tp = Types.elabType(ty);
call = Expression.makeBuiltinCall(inFnName, {s1_1, s2_1}, tp);
then
(cache, call, DAE.PROP(ty,c));

// min|max(x,y) where x & y are Integer scalars.
case (cache, env, _, {s1, s2}, impl, pre, info)
equation
(cache, s1_1, DAE.PROP(ty1, c1), _) =
elabExp(cache,env, s1, impl,NONE(),true,pre,info);
(cache, s2_1, DAE.PROP(ty2, c2), _) =
elabExp(cache,env, s2, impl,NONE(),true,pre,info);

true = Types.isInteger(ty1) and Types.isInteger(ty2);
c = Types.constAnd(c1, c2);
tp = Types.elabType(ty1);
call = Expression.makeBuiltinCall(inFnName, {s1_1, s2_1}, tp);
then
(cache, call, DAE.PROP(ty1,c));
end matchcontinue;
end elabBuiltinMinMaxCommon;

Expand Down
19 changes: 19 additions & 0 deletions Compiler/FrontEnd/Types.mo
Expand Up @@ -6606,4 +6606,23 @@ algorithm
end match;
end varHasMetaRecordType;

public function scalarSuperType
"Checks that the givens types are scalar and that one is subtype of the other (in the case of integers)."
input Type ty1;
input Type ty2;
output Type ty;
algorithm
ty := matchcontinue (ty1,ty2)
case ((DAE.T_INTEGER(_),_),(DAE.T_INTEGER(_),_)) then DAE.T_INTEGER_DEFAULT;
case ((DAE.T_REAL(_),_),(DAE.T_REAL(_),_)) then DAE.T_REAL_DEFAULT;
case ((DAE.T_INTEGER(_),_),(DAE.T_REAL(_),_)) then DAE.T_REAL_DEFAULT;
case ((DAE.T_REAL(_),_),(DAE.T_INTEGER(_),_)) then DAE.T_REAL_DEFAULT;
case ((DAE.T_COMPLEX(complexTypeOption = SOME(ty1)),_),ty2) then scalarSuperType(ty1,ty2);
case (ty1,(DAE.T_COMPLEX(complexTypeOption = SOME(ty2)),_)) then scalarSuperType(ty1,ty2);

case ((DAE.T_BOOL(_),_),(DAE.T_BOOL(_),_)) then DAE.T_BOOL_DEFAULT;
// case ((DAE.T_STRING(_),_),(DAE.T_STRING(_),_)) then DAE.T_STRING_DEFAULT;
end matchcontinue;
end scalarSuperType;

end Types;

0 comments on commit 8740ef5

Please sign in to comment.