Skip to content

Commit

Permalink
- Fix some bad enum to int conversions in the back end.
Browse files Browse the repository at this point in the history
- Some code clean up.


git-svn-id: https://openmodelica.org/svn/OpenModelica/trunk@25063 f25d12d1-65f4-0310-ae8a-bbce733d8d8e
  • Loading branch information
perost committed Mar 12, 2015
1 parent b640ae0 commit dce49f5
Show file tree
Hide file tree
Showing 7 changed files with 72 additions and 94 deletions.
49 changes: 20 additions & 29 deletions Compiler/BackEnd/BackendDAEUtil.mo
Expand Up @@ -1338,7 +1338,7 @@ algorithm

case (DAE.CREF(), _, _, _)
equation
indices = rangeIntExprs(rangeExpr);
indices = rangeExprs(rangeExpr);
clonedElements = List.fill(arrayVar, listLength(indices));
newElements = generateArrayElements(clonedElements, indices, iteratorExp);
then newElements;
Expand All @@ -1347,7 +1347,7 @@ algorithm
equation
// If the range is constant, then we can use it to generate only those
// array elements that are actually used.
indices = rangeIntExprs(rangeExpr);
indices = rangeExprs(rangeExpr);
clonedElements = List.fill(arrayVar, listLength(indices));
newElements = generateArrayElements(clonedElements, indices, iteratorExp);
then newElements;
Expand Down Expand Up @@ -1376,31 +1376,23 @@ algorithm
end matchcontinue;
end explodeArrayVars;

protected function rangeIntExprs
protected function rangeExprs
"Tries to convert a range to a list of integer expressions. Returns a list of
integer expressions if possible, or fails. Used by explodeArrayVars."
input DAE.Exp range;
output list<DAE.Exp> integers;
input DAE.Exp inRange;
output list<DAE.Exp> outValues;
algorithm
integers := match(range)
outValues := match inRange
local
list<DAE.Exp> arrayElements;
Integer start, stop;
list<Integer> vals;

case (DAE.ARRAY(array = arrayElements)) then arrayElements;

case (DAE.RANGE(start = DAE.ICONST(integer = start), stop = DAE.ICONST(integer = stop), step = NONE()))
equation
vals = ExpressionSimplify.simplifyRange(start, 1, stop);
arrayElements = List.map(vals, Expression.makeIntegerExp);
then
arrayElements;

case (_) then fail();
case DAE.ARRAY(array = arrayElements) then arrayElements;
case DAE.RANGE() then Expression.expandRange(inRange);

end match;
end rangeIntExprs;
end rangeExprs;

public function daeSize
"author: Frenkel TUD
Expand Down Expand Up @@ -2879,12 +2871,13 @@ algorithm
Boolean b;
list<DAE.Exp> explst;
Option<DAE.Exp> stepvalueopt;
Integer istart, istep, istop, i;
Integer i;
list<DAE.ComponentRef> crlst;
Option<DAE.FunctionTree> ofunctionTree;
DAE.FunctionTree functionTree;
tuple<BackendDAE.Variables, list<Integer>,Option<DAE.FunctionTree>> tpl;
Integer diffindx;
list<DAE.Subscript> subs;

case (DAE.LBINARY(), tpl)
then (inExp, false, tpl);
Expand All @@ -2900,17 +2893,15 @@ algorithm
case (DAE.RANGE(), tpl)
then (inExp, false, tpl);

case (DAE.ASUB(exp=DAE.CREF(componentRef=cr), sub=explst), (vars, pa, ofunctionTree)) equation
{DAE.RANGE(start=startvalue, step=stepvalueopt, stop=stopvalue)} = ExpressionSimplify.simplifyList(explst, {});
stepvalue = Util.getOptionOrDefault(stepvalueopt, DAE.ICONST(1));
istart = Expression.expInt(startvalue);
istep = Expression.expInt(stepvalue);
istop = Expression.expInt(stopvalue);
ilst = List.intRange3(istart, istep, istop);
crlst = List.map1r(ilst, ComponentReference.subscriptCrefWithInt, cr);
(varslst, p) = BackendVariable.getVarLst(crlst, vars,{},{});
pa = incidenceRowExp1(varslst, p, pa, 0);
then (inExp, false, (vars, pa, ofunctionTree));
case (DAE.ASUB(exp=DAE.CREF(componentRef=cr), sub=explst), (vars, pa, ofunctionTree))
algorithm
{e1 as DAE.RANGE()} := ExpressionSimplify.simplifyList(explst, {});
subs := list(DAE.INDEX(e) for e in extendRange(e1, vars));
crlst := list(ComponentReference.subscriptCref(cr, {s}) for s in subs);
(varslst, p) := BackendVariable.getVarLst(crlst, vars,{},{});
pa := incidenceRowExp1(varslst, p, pa, 0);
then
(inExp, false, (vars, pa, ofunctionTree));

case (DAE.ASUB(exp=e1, sub={DAE.ICONST(i)}), tpl) equation
e1 = Expression.nthArrayExp(e1, i);
Expand Down
4 changes: 2 additions & 2 deletions Compiler/BackEnd/BackendVariable.mo
Expand Up @@ -4126,8 +4126,8 @@ algorithm

case (SOME(min),SOME(max))
equation
rmin = Expression.expReal(min);
rmax = Expression.expReal(max);
rmin = Expression.toReal(min);
rmax = Expression.toReal(max);
true = realGt(rmin,rmax);
s1 = ComponentReference.printComponentRefStr(cr1);
s2 = if negate then " = -" else " = ";
Expand Down
8 changes: 4 additions & 4 deletions Compiler/BackEnd/EvaluateFunctions.mo
Expand Up @@ -1722,7 +1722,7 @@ algorithm
(exp1,_) = BackendVarTransform.replaceExp(exp1,replIn,NONE());
(exp1,_) = ExpressionSimplify.simplify(exp1);
isCon = Expression.isConst(exp1);
isIf = if isCon then Expression.getBoolConst(exp1) else false;
isIf = if isCon then Expression.toBool(exp1) else false;

// check if its the IF case, if true then evaluate:
if Flags.isSet(Flags.EVAL_FUNC_DUMP) then
Expand Down Expand Up @@ -1791,7 +1791,7 @@ algorithm
(exp1,_) = BackendVarTransform.replaceExp(exp1,replIn,NONE());
(exp1,_) = ExpressionSimplify.simplify(exp1);
isCon = Expression.isConst(exp1);
isElseIf = if isCon then Expression.getBoolConst(exp1) else false;
isElseIf = if isCon then Expression.toBool(exp1) else false;
if isCon and not isElseIf then
(stmts,isElseIf) = evaluateElse(else_,info);
end if;
Expand Down Expand Up @@ -2159,7 +2159,7 @@ algorithm
//ExpressionDump.dumpExp(exp);
// check if this could be evaluated
const = Expression.isConst(exp);
isElseIf = if const then Expression.getBoolConst(exp) else false;
isElseIf = if const then Expression.toBool(exp) else false;
//print("do we have to use the elseif: "+boolString(isElseIf)+"\n");
if const and isElseIf then
(stmts,(funcs,repl,idx)) = evaluateFunctions_updateStatement(stmts,(funcs,replIn,idx),{}); // is this elseif case
Expand Down Expand Up @@ -2486,7 +2486,7 @@ algorithm
end if;
(constOutExps,_,varOutExps) = List.intersection1OnTrue(outExps,allLHS,Expression.expEqual);

_ = (not listEmpty(constOutExps)) and listEmpty(varOutExps);
//_ = (not listEmpty(constOutExps)) and listEmpty(varOutExps);
//repl = bcallret3(not predicted, BackendVarTransform.removeReplacements,replIn,varCrefs,NONE(),replIn);
//bcall(not predicted,print,"remove the replacement for: "+stringDelimitList(List.map(varCrefs,ComponentReference.crefStr),"\n")+"\n");
repl = replIn;
Expand Down
4 changes: 2 additions & 2 deletions Compiler/BackEnd/RemoveSimpleEquations.mo
Expand Up @@ -2967,8 +2967,8 @@ algorithm
Real rmin, rmax;
case ((SOME(min), SOME(max)), _, _)
equation
rmin = Expression.expReal(min);
rmax = Expression.expReal(max);
rmin = Expression.toReal(min);
rmax = Expression.toReal(max);
true = realGt(rmin, rmax);
s4 = ExpressionDump.printExpStr(min);
s5 = ExpressionDump.printExpStr(max);
Expand Down
49 changes: 18 additions & 31 deletions Compiler/FrontEnd/Expression.mo
Expand Up @@ -1495,17 +1495,26 @@ end arrayDimensionSetFirst;
/* Getter */
/***************************************************/

public function expReal "returns the real value if expression is constant Real"
input DAE.Exp exp;
output Real r;
public function toReal
"Returns the value of a constant Real expression."
input DAE.Exp inExp;
output Real outReal;
algorithm
r := match(exp) local Integer i;
case(DAE.RCONST(r)) then r;
case(DAE.ICONST(i)) then intReal(i);
case(DAE.CAST(_,DAE.ICONST(i))) then intReal(i);
case (DAE.ENUM_LITERAL(index = i)) then intReal(i);
outReal := match inExp
case DAE.RCONST() then inExp.real;
case DAE.ICONST() then intReal(inExp.integer);
case DAE.CAST() then toReal(inExp.exp);
case DAE.ENUM_LITERAL() then intReal(inExp.index);
end match;
end expReal;
end toReal;

public function toBool
"Returns the value of a constant Boolean expression."
input DAE.Exp inExp;
output Boolean outBool;
algorithm
DAE.BCONST(outBool) := inExp;
end toBool;

public function realExpIntLit "returns the int value if expression is constant Real that can be represented by an Integer"
input DAE.Exp exp;
Expand Down Expand Up @@ -1757,28 +1766,6 @@ algorithm
end match;
end get2dArrayOrMatrixContent;

public function getBoolConst "returns the expression as a Boolean value.
"
input DAE.Exp e;
output Boolean b;
algorithm
DAE.BCONST(b) := e;
end getBoolConst;

public function getRealConst "returns the expression as a Real value.
Integer constants are cast to Real"
input DAE.Exp ie;
output Real v;
algorithm
v := match (ie)
local Integer i; DAE.Exp e;
case(DAE.RCONST(v)) then v;
case(DAE.CAST(_,e)) then getRealConst(e);
case(DAE.ICONST(i)) then intReal(i);
case DAE.ENUM_LITERAL(index = i) then intReal(i);
end match;
end getRealConst;

// stefan
public function unboxExpType
"takes a type, and if it is boxed, unbox it
Expand Down
48 changes: 24 additions & 24 deletions Compiler/FrontEnd/ExpressionSimplify.mo
Expand Up @@ -1527,7 +1527,7 @@ algorithm
// sqrt function
case("sqrt",DAE.CALL(expLst={e}))
equation
r = realSqrt(Expression.getRealConst(e));
r = realSqrt(Expression.toReal(e));
then
DAE.RCONST(r);

Expand All @@ -1548,49 +1548,49 @@ algorithm
// sin function
case("sin",DAE.CALL(expLst={e}))
equation
r = realSin(Expression.getRealConst(e));
r = realSin(Expression.toReal(e));
then DAE.RCONST(r);

// cos function
case("cos",DAE.CALL(expLst={e}))
equation
r = realCos(Expression.getRealConst(e));
r = realCos(Expression.toReal(e));
then DAE.RCONST(r);

// sin function
case("asin",DAE.CALL(expLst={e}))
equation
r = Expression.getRealConst(e);
r = Expression.toReal(e);
true = r >= -1.0 and r <= 1.0;
r = realAsin(r);
then DAE.RCONST(r);

// cos function
case("acos",DAE.CALL(expLst={e}))
equation
r = Expression.getRealConst(e);
r = Expression.toReal(e);
true = r >= -1.0 and r <= 1.0;
r = realAcos(Expression.getRealConst(e));
r = realAcos(Expression.toReal(e));
then DAE.RCONST(r);

// tangent function
case("tan",DAE.CALL(expLst={e}))
equation
v1 = realSin(Expression.getRealConst(e));
v2 = realCos(Expression.getRealConst(e));
v1 = realSin(Expression.toReal(e));
v2 = realCos(Expression.toReal(e));
r = v1 / v2;
then DAE.RCONST(r);

// DAE.Exp function
case("exp",DAE.CALL(expLst={e}))
equation
r = realExp(Expression.getRealConst(e));
r = realExp(Expression.toReal(e));
then DAE.RCONST(r);

// log function
case("log",DAE.CALL(expLst={e}))
equation
r = Expression.getRealConst(e);
r = Expression.toReal(e);
true = r > 0;
r = realLn(r);
then
Expand All @@ -1599,7 +1599,7 @@ algorithm
// log10 function
case("log10",DAE.CALL(expLst={e}))
equation
r = Expression.getRealConst(e);
r = Expression.toReal(e);
true = r > 0;
r = realLog10(r);
then
Expand All @@ -1614,8 +1614,8 @@ algorithm
// min function on reals
case("min",DAE.CALL(expLst={e, e1},attr=DAE.CALL_ATTR(ty=DAE.T_REAL())))
equation
v1 = Expression.getRealConst(e);
v2 = Expression.getRealConst(e1);
v1 = Expression.toReal(e);
v2 = Expression.toReal(e1);
r = realMin(v1, v2);
then DAE.RCONST(r);

Expand All @@ -1634,8 +1634,8 @@ algorithm
// max function on reals
case("max",DAE.CALL(expLst={e, e1},attr=DAE.CALL_ATTR(ty=DAE.T_REAL())))
equation
v1 = Expression.getRealConst(e);
v2 = Expression.getRealConst(e1);
v1 = Expression.toReal(e);
v2 = Expression.toReal(e1);
r = realMax(v1, v2);
then DAE.RCONST(r);

Expand Down Expand Up @@ -3446,8 +3446,8 @@ algorithm

case(DAE.LESS(),_,_)
equation
v1 = Expression.getRealConst(e1);
v2 = Expression.getRealConst(e2);
v1 = Expression.toReal(e1);
v2 = Expression.toReal(e2);
b = v1 < v2;
then b;

Expand All @@ -3459,8 +3459,8 @@ algorithm

case(DAE.LESSEQ(),_,_)
equation
v1 = Expression.getRealConst(e1);
v2 = Expression.getRealConst(e2);
v1 = Expression.toReal(e1);
v2 = Expression.toReal(e2);
b = v1 <= v2;
then b;

Expand All @@ -3472,8 +3472,8 @@ algorithm

case(DAE.EQUAL(),_,_)
equation
v1 = Expression.getRealConst(e1);
v2 = Expression.getRealConst(e2);
v1 = Expression.toReal(e1);
v2 = Expression.toReal(e2);
then realEq(v1,v2);

// Express GT, GE, NE using LE, LT, EQ
Expand Down Expand Up @@ -4618,8 +4618,8 @@ algorithm
// constants
case (_,oper,e1,e2)
equation
b1 = Expression.getBoolConst(e1);
b2 = Expression.getBoolConst(e2);
b1 = Expression.toBool(e1);
b2 = Expression.toBool(e2);
e3 = simplifyLBinaryConst(oper, b1, b2);
then e3;

Expand Down Expand Up @@ -4820,7 +4820,7 @@ algorithm
// not true => false, not false => true
case (_,DAE.NOT(DAE.T_BOOL()),e1)
equation
b1 = Expression.getBoolConst(e1);
b1 = Expression.toBool(e1);
b1 = not b1;
then DAE.BCONST(b1);

Expand Down
4 changes: 2 additions & 2 deletions Compiler/FrontEnd/Static.mo
Expand Up @@ -4696,7 +4696,7 @@ algorithm
variability = Types.getPropConst(prop1);
false = valueEq(variability,DAE.C_VAR());
// check if argument is non-negativ
rInterval = Expression.expReal(interval);
rInterval = Expression.toReal(interval);
true = rInterval >= 0.0;

ty = DAE.T_FUNCTION(
Expand Down Expand Up @@ -4737,7 +4737,7 @@ algorithm
ty2 = Types.arrayElementType(Types.getPropType(prop2));
(condition,_) = Types.matchType(condition,ty1,DAE.T_BOOL_DEFAULT,true);
(startInterval,_) = Types.matchType(startInterval,ty2,DAE.T_REAL_DEFAULT,true);
rStartInterval = Expression.expReal(startInterval);
rStartInterval = Expression.toReal(startInterval);
true = rStartInterval >= 0.0;

ty = DAE.T_FUNCTION(
Expand Down

0 comments on commit dce49f5

Please sign in to comment.