Skip to content

Commit

Permalink
- Fixed reductions so that they are constant evaluated in classes whe…
Browse files Browse the repository at this point in the history
…n possible.

- Added testcase to test constant evaluation of reductions
  (ConstantReductions.mo).
- Added missing reduction function 'product'.


git-svn-id: https://openmodelica.org/svn/OpenModelica/trunk@4841 f25d12d1-65f4-0310-ae8a-bbce733d8d8e
  • Loading branch information
perost committed Jan 21, 2010
1 parent ea7dce6 commit bd4af31
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 3 deletions.
17 changes: 17 additions & 0 deletions Compiler/Ceval.mo
Expand Up @@ -4987,6 +4987,22 @@ algorithm
end matchcontinue;
end valueAdd;

protected function valueMul
"Multiplies two Values. Used (indirectly) by cevalReduction."
input Values.Value v1;
input Values.Value v2;
output Values.Value res;
algorithm
res := matchcontinue(v1, v2)
case (Values.INTEGER(i1), Values.INTEGER(i2))
local Integer i1, i2, res;
equation res = i1 * i2; then Values.INTEGER(res);
case (Values.REAL(r1), Values.REAL(r2))
local Real r1, r2, res;
equation res = r1 *. r2; then Values.REAL(res);
end matchcontinue;
end valueMul;

protected function valueMax
"Returns the maximum of two Values. Used (indirectly) by cevalReduction."
input Values.Value v1;
Expand Down Expand Up @@ -5033,6 +5049,7 @@ algorithm
op := matchcontinue(reductionName)
case "max" then valueMax;
case "min" then valueMin;
case "product" then valueMul;
case "sum" then valueAdd;
end matchcontinue;
end lookupReductionOp;
Expand Down
15 changes: 12 additions & 3 deletions Compiler/Static.mo
Expand Up @@ -546,8 +546,10 @@ algorithm
Absyn.Exp exp;
equation
(cache,e,prop,st_1) = elabCallReduction(cache,env, fn, exp, iterators, impl, st,doVect);
c = Types.propAllConst(prop);
(cache, e_1, prop_1) = cevalIfConstant(cache, e, prop, c, impl, env);
then
(cache,e,prop,st_1);
(cache,e_1,prop,st_1);
case (cache,env,Absyn.RANGE(start = start,step = NONE,stop = stop),impl,st,doVect)
equation
(cache,start_1,DAE.PROP(start_t,c_start),st_1) = elabExp(cache,env, start, impl, st,doVect) "Range expressions without step value, e.g. 1:5" ;
Expand Down Expand Up @@ -1373,15 +1375,22 @@ algorithm
prop = DAE.PROP(ty, const);
then
(cache, exp_1, prop, st);
/* min, max, sum and product */
case (cache,env,fn,exp,{(iter,SOME(iterexp))},impl,st,doVect)
equation
(cache,iterexp_1,DAE.PROP((DAE.T_ARRAY((arraydim as DAE.DIM(_)),iterty),_),iterconst),_)
= elabExp(cache,env, iterexp, impl, st,doVect);
env_1 = Env.openScope(env, false, SOME(Env.forScopeName));
// Elaborate the expression with a variable iterator first, so that any
// subscripts using the iterator aren't substituted.
env_1 = Env.extendFrameForIterator(env_1, iter, iterty, DAE.UNBOUND(), SCode.VAR());
(cache,exp_1,DAE.PROP(expty,expconst),st) = elabExp(cache,env_1, exp, impl, st,doVect) "const so that expr is elaborated to const" ;
(cache,exp_1,_,st) = elabExp(cache,env_1, exp, impl, st,doVect);
// Then elaborate the expression with a constant iterator, to get the
// correct type of the expression.
env_1 = Env.extendFrameForIterator(env_1, iter, iterty, DAE.VALBOUND(Values.INTEGER(1)), SCode.CONST());
(cache,_,DAE.PROP(expty,expconst),st) = elabExp(cache,env_1, exp, impl, st,doVect) "const so that expr is elaborated to const" ;
const = Types.constAnd(expconst, iterconst);
prop = DAE.PROP((DAE.T_ARRAY(arraydim,expty),NONE),const);
prop = DAE.PROP(expty, expconst);
fn_1 = Absyn.crefToPath(fn);
then
(cache,DAE.REDUCTION(fn_1,exp_1,iter,iterexp_1),prop,st);
Expand Down

0 comments on commit bd4af31

Please sign in to comment.