Skip to content

Commit

Permalink
- Implemented simplification of size calls.
Browse files Browse the repository at this point in the history
- Added test case mofiles/ConstantSize and updated mofiles/DiagonalBlock.


git-svn-id: https://openmodelica.org/svn/OpenModelica/trunk@6025 f25d12d1-65f4-0310-ae8a-bbce733d8d8e
  • Loading branch information
perost committed Sep 6, 2010
1 parent bfe7352 commit 4c47984
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 8 deletions.
13 changes: 13 additions & 0 deletions Compiler/Exp.mo
Expand Up @@ -13251,6 +13251,19 @@ algorithm
end matchcontinue;
end dimensionSize;

public function dimensionSizeExp
"Converts a dimension to an integer expression."
input DAE.Dimension dim;
output DAE.Exp exp;
algorithm
exp := matchcontinue(dim)
local
Integer i;
case DAE.DIM_INTEGER(integer = i) then DAE.ICONST(i);
case DAE.DIM_ENUM(size = i) then DAE.ICONST(i);
end matchcontinue;
end dimensionSizeExp;

public function intDimension
"Converts an integer to an array dimension."
input Integer value;
Expand Down
54 changes: 46 additions & 8 deletions Compiler/Static.mo
Expand Up @@ -3221,7 +3221,8 @@ algorithm
local
DAE.Exp dimp,arraycrefe,exp;
DAE.Const c1,c2_1,c,c_1;
tuple<DAE.TType, Option<Absyn.Path>> arrtp;
DAE.Type arrtp;
DAE.Properties prop;
Boolean c2,impl;
list<Env.Frame> env;
Absyn.Exp arraycr,dim;
Expand All @@ -3230,30 +3231,67 @@ algorithm
DAE.DAElist dae,dae1,dae2;
Prefix pre;
String sp;

// size(A,x) that returns size of x:th dimension
Integer dim_int;
DAE.ExpType ety;
list<DAE.Dimension> dims;
DAE.Dimension d;

// size(A,x) for an array A with known dimensions and constant x.
// Returns the size of the x:th dimension.
case (cache, env, {arraycr, dim}, _, impl, pre)
equation
(cache,dimp,_,_,dae1) = elabExp(cache, env, dim, impl, NONE, true, pre);
dim_int = Exp.expInt(dimp);
(cache, arraycrefe, _, _, dae2) = elabExp(cache, env, arraycr, impl, NONE, false, pre);
ety = Exp.typeof(arraycrefe);
dims = Exp.arrayTypeDimensions(ety);
d = listNth(dims, dim_int - 1);
exp = Exp.dimensionSizeExp(d);
prop = DAE.PROP(DAE.T_INTEGER_DEFAULT, DAE.C_CONST);
dae = DAEUtil.joinDaes(dae1, dae2);
then
(cache, exp, prop, dae);

// The previous case failed, return a call to the size function instead.
case (cache,env,{arraycr,dim},_,impl,pre)
equation
(cache,dimp,DAE.PROP(_,c1),_,dae1) = elabExp(cache, env, dim, impl, NONE, true,pre) ;
(cache,dimp,DAE.PROP(_,c1),_,dae1) = elabExp(cache, env, dim, impl, NONE, true,pre);
(cache,arraycrefe,DAE.PROP(arrtp,_),_,dae2) = elabExp(cache, env, arraycr, impl, NONE, true,pre);
c2 = Types.dimensionsKnown(arrtp);
c2_1 = Types.boolConstSize(c2);
c = Types.constAnd(c1, c2_1);
exp = DAE.SIZE(arraycrefe,SOME(dimp));
prop = DAE.PROP(DAE.T_INTEGER_DEFAULT, c);
dae = DAEUtil.joinDaes(dae1,dae2);
then
(cache,exp,DAE.PROP(DAE.T_INTEGER_DEFAULT,c),dae);
(cache,exp,prop,dae);

// size(A)
// size(A) for an array A with known dimensions. Returns an array of all dimensions of A.
case (cache,env,{arraycr},_,impl,pre)
local list<Exp.Exp> dim_expl;
equation
(cache, arraycrefe, _, _, dae1) = elabExp(cache, env, arraycr, impl, NONE, false, pre);
ety = Exp.typeof(arraycrefe);
dims = Exp.arrayTypeDimensions(ety);
dim_expl = Util.listMap(dims, Exp.dimensionSizeExp);
dim_int = listLength(dim_expl);
exp = DAE.ARRAY(DAE.ET_ARRAY(DAE.ET_INT, {DAE.DIM_INTEGER(dim_int)}), true, dim_expl);
prop = DAE.PROP((DAE.T_ARRAY(DAE.DIM_INTEGER(dim_int), DAE.T_INTEGER_DEFAULT), NONE), DAE.C_CONST);
then
(cache, exp, prop, dae1);

// The previous case failed, return a call to the size function instead.
case (cache,env,{arraycr},_,impl,pre)
local Boolean c;
equation
(cache,arraycrefe,DAE.PROP(arrtp,_),_,dae1) = elabExp(cache,env, arraycr, impl, NONE,true,pre) ;
(cache,arraycrefe,DAE.PROP(arrtp,_),_,dae1) = elabExp(cache,env, arraycr, impl, NONE,true,pre);
c = Types.dimensionsKnown(arrtp);
c_1 = Types.boolConstSize(c);
exp = DAE.SIZE(arraycrefe,NONE);
prop = DAE.PROP((DAE.T_ARRAY(DAE.DIM_NONE, DAE.T_INTEGER_DEFAULT), NONE), c_1);
then
(cache,exp,DAE.PROP((DAE.T_ARRAY(DAE.DIM_INTEGER(1),DAE.T_INTEGER_DEFAULT),NONE),c_1),dae1);
(cache,exp,prop,dae1);

// failure!
case (cache,env,expl,_,impl,pre)
equation
Expand Down

0 comments on commit 4c47984

Please sign in to comment.