Skip to content

Commit

Permalink
- Handle other types than integer ranges in BackendDAEUtil.extendRange.
Browse files Browse the repository at this point in the history
git-svn-id: https://openmodelica.org/svn/OpenModelica/trunk@25037 f25d12d1-65f4-0310-ae8a-bbce733d8d8e
  • Loading branch information
perost committed Mar 11, 2015
1 parent 361b4a6 commit 3464a66
Show file tree
Hide file tree
Showing 2 changed files with 104 additions and 11 deletions.
59 changes: 48 additions & 11 deletions Compiler/BackEnd/BackendDAEUtil.mo
Expand Up @@ -8053,26 +8053,63 @@ public function extendRange
input BackendDAE.Variables inKnVariables;
output list<DAE.Exp> outExpLst = {};
protected
DAE.Exp start, stepvalue, stop;
Option<DAE.Exp> step;
Integer istart, istop, istep;
list<Integer> ilst;
DAE.Exp start, step, stop;
Option<DAE.Exp> ostep;
DAE.Type ty;
algorithm
try
DAE.RANGE(start=start, step=step, stop=stop) := inRangeExp;
stepvalue := Util.getOptionOrDefault(step, DAE.ICONST(1));
istart := expInt(start, inKnVariables);
istep := expInt(stepvalue, inKnVariables);
istop := expInt(stop, inKnVariables);
ilst := List.intRange3(istart, istep, istop);
outExpLst := List.map(ilst, Expression.makeIntegerExp);
DAE.RANGE(ty = ty, start = start, step = ostep, stop = stop) := inRangeExp;

start := evalExp(start, inKnVariables);
stop := evalExp(stop, inKnVariables);

if isSome(ostep) then
SOME(step) := ostep;
ostep := SOME(evalExp(step, inKnVariables));
end if;

outExpLst := Expression.expandRange(DAE.RANGE(ty, start, ostep, stop));
else
if Flags.isSet(Flags.FAILTRACE) then
Debug.trace("BackendDAECreate.extendRange failed. Maybe some ZeroCrossing are not supported\n");
end if;
end try;
end extendRange;

public function evalExp
input DAE.Exp inExp;
input BackendDAE.Variables inKnVariables;
output DAE.Exp outExp;
algorithm
outExp := match inExp
local
DAE.Exp e, e1, e2;

case DAE.CREF()
algorithm
((BackendDAE.VAR(bindExp = SOME(e)) :: _), _) :=
BackendVariable.getVar(inExp.componentRef, inKnVariables);
then
e;

case DAE.BINARY(operator = DAE.ADD(DAE.T_INTEGER()))
algorithm
e1 := evalExp(inExp.exp1, inKnVariables);
e2 := evalExp(inExp.exp2, inKnVariables);
then
DAE.ICONST(Expression.expInt(e1) + Expression.expInt(e2));

case DAE.BINARY(operator = DAE.SUB(DAE.T_INTEGER()))
algorithm
e1 := evalExp(inExp.exp1, inKnVariables);
e2 := evalExp(inExp.exp2, inKnVariables);
then
DAE.ICONST(Expression.expInt(e1) - Expression.expInt(e2));

else inExp;
end match;
end evalExp;

public function expInt "returns the int value of an expression"
input DAE.Exp inExp;
input BackendDAE.Variables inKnVariables;
Expand Down
56 changes: 56 additions & 0 deletions Compiler/FrontEnd/Expression.mo
Expand Up @@ -12108,5 +12108,61 @@ algorithm
outExp := typeCast(inExp, ty);
end typeCastElements;

public function expandRange
input DAE.Exp inRange;
output list<DAE.Exp> outValues;
protected
DAE.Exp start_exp, stop_exp;
Option<DAE.Exp> ostep_exp;
Integer istep;
Real rstep;
list<DAE.Exp> vals;
list<String> enum_names;
Absyn.Path enum_type;
algorithm
DAE.RANGE(start = start_exp, step = ostep_exp, stop = stop_exp) := inRange;

outValues := match (start_exp, stop_exp)
case (DAE.ICONST(), DAE.ICONST())
algorithm
DAE.ICONST(istep) := Util.getOptionOrDefault(ostep_exp, DAE.ICONST(1));
then
list(DAE.ICONST(i) for i in
List.intRange3(start_exp.integer, istep, stop_exp.integer));

case (DAE.RCONST(), DAE.RCONST())
algorithm
DAE.RCONST(rstep) := Util.getOptionOrDefault(ostep_exp, DAE.RCONST(1.0));
then
list(DAE.RCONST(r) for r in
ExpressionSimplify.simplifyRangeReal(start_exp.real, rstep, stop_exp.real));

case (DAE.BCONST(false), DAE.BCONST(true))
then {start_exp, stop_exp};

case (DAE.BCONST(true), DAE.BCONST(false))
then {};

case (DAE.BCONST(), DAE.BCONST())
then {start_exp};

case (DAE.ENUM_LITERAL(), DAE.ENUM_LITERAL())
algorithm
if start_exp.index > stop_exp.index then
vals := {};
elseif start_exp.index == stop_exp.index then
vals := {start_exp};
else
DAE.RANGE(ty = DAE.T_ENUMERATION(path = enum_type, names = enum_names)) := inRange;
enum_names := List.sublist(enum_names, start_exp.index,
(stop_exp.index - start_exp.index) + 1);
vals := makeEnumLiterals(enum_type, enum_names);
end if;
then
vals;

end match;
end expandRange;

annotation(__OpenModelica_Interface="frontend");
end Expression;

0 comments on commit 3464a66

Please sign in to comment.