Skip to content

Commit

Permalink
- Added simplify rules for the cat() operator
Browse files Browse the repository at this point in the history
git-svn-id: https://openmodelica.org/svn/OpenModelica/trunk@9570 f25d12d1-65f4-0310-ae8a-bbce733d8d8e
  • Loading branch information
sjoelund committed Aug 1, 2011
1 parent 15ec366 commit 0988891
Show file tree
Hide file tree
Showing 7 changed files with 136 additions and 34 deletions.
28 changes: 28 additions & 0 deletions Compiler/FrontEnd/Expression.mo
Expand Up @@ -1541,6 +1541,34 @@ algorithm
end match;
end dimensionSize;

public function addDimensions
input DAE.Dimension dim1;
input DAE.Dimension dim2;
output DAE.Dimension dim;
algorithm
dim := match(dim1,dim2)
local
Integer i1,i2,i;
case (DAE.DIM_INTEGER(integer = i1),DAE.DIM_INTEGER(integer = i2))
equation
i = i1+i2;
then DAE.DIM_INTEGER(i);
case (DAE.DIM_ENUM(size = i1),DAE.DIM_INTEGER(integer = i2))
equation
i = i1+i2;
then DAE.DIM_INTEGER(i);
case (DAE.DIM_INTEGER(integer = i1),DAE.DIM_ENUM(size = i2))
equation
i = i1+i2;
then DAE.DIM_INTEGER(i);
case (DAE.DIM_ENUM(size = i1),DAE.DIM_ENUM(size = i2))
equation
i = i1+i2;
then DAE.DIM_INTEGER(i);
else DAE.DIM_UNKNOWN();
end match;
end addDimensions;

public function dimensionSizeAll
"Extracts an integer from an array dimension. Also handles DIM_EXP and
DIM_UNKNOWN if checkModel is used."
Expand Down
55 changes: 54 additions & 1 deletion Compiler/FrontEnd/ExpressionSimplify.mo
Expand Up @@ -605,7 +605,7 @@ protected function simplifyBuiltinCalls "simplifies some builtin calls (with no
algorithm
outExp := matchcontinue (exp)
local
list<DAE.Exp> expl;
list<DAE.Exp> es,expl;
DAE.Exp e,len_exp,just_exp,e1,e2,e3,e4;
DAE.ExpType tp;
DAE.Operator op;
Expand Down Expand Up @@ -699,9 +699,62 @@ algorithm
e = Expression.makeBuiltinCall("delay",{e,e3,e4},tp);
then DAE.UNARY(op,e);

case DAE.CALL(path=Absyn.IDENT("cat"),expLst=_::{e1}) then e1;

case DAE.CALL(path=Absyn.IDENT("cat"),expLst=DAE.ICONST(i)::es,attr=DAE.CALL_ATTR(ty=tp))
equation
es = simplifyCat(i,es,{},false);
i = listLength(es);
e = Expression.makeBuiltinCall("cat",DAE.ICONST(i)::es,tp);
then e;

end matchcontinue;
end simplifyBuiltinCalls;

protected function simplifyCat
input Integer dim;
input list<DAE.Exp> es;
input list<DAE.Exp> acc;
input Boolean changed;
output list<DAE.Exp> oes;
algorithm
oes := matchcontinue (dim,es,acc,changed)
local
list<DAE.Exp> es1,es2,esn;
DAE.Exp e;
Boolean sc;
DAE.Dimension ndim,dim1,dim2,dim11,dim21;
list<DAE.Dimension> dims;
DAE.ExpType etp;
Integer i1,i2,i;
list<list<tuple<DAE.Exp,Boolean>>> ms1,ms2,mss;
case (_,{},acc,true) then listReverse(acc);
case (1,DAE.ARRAY(array=es1,scalar=sc,ty=DAE.ET_ARRAY(arrayDimensions=dim1::dims,ty=etp))::DAE.ARRAY(array=es2,ty=DAE.ET_ARRAY(arrayDimensions=dim2::_))::es,acc,_)
equation
esn = listAppend(es1,es2);
ndim = Expression.addDimensions(dim1,dim2);
etp = DAE.ET_ARRAY(etp,ndim::dims);
e = DAE.ARRAY(etp,sc,esn);
then simplifyCat(dim,e::es,acc,true);
case (1,DAE.MATRIX(scalar=ms1,integer=i1,ty=DAE.ET_ARRAY(arrayDimensions=dim1::dims,ty=etp))::DAE.MATRIX(scalar=ms2,integer=i2,ty=DAE.ET_ARRAY(arrayDimensions=dim2::_))::es,acc,_)
equation
i = i1+i2;
mss = listAppend(ms1,ms2);
ndim = Expression.addDimensions(dim1,dim2);
etp = DAE.ET_ARRAY(etp,ndim::dims);
e = DAE.MATRIX(etp,i,mss);
then simplifyCat(dim,e::es,acc,true);
case (2,DAE.MATRIX(scalar=ms1,integer=i,ty=DAE.ET_ARRAY(arrayDimensions=dim11::dim1::dims,ty=etp))::DAE.MATRIX(scalar=ms2,ty=DAE.ET_ARRAY(arrayDimensions=_::dim2::_))::es,acc,_)
equation
mss = Util.listThreadMap(ms1,ms2,listAppend);
ndim = Expression.addDimensions(dim1,dim2);
etp = DAE.ET_ARRAY(etp,dim11::ndim::dims);
e = DAE.MATRIX(etp,i,mss);
then simplifyCat(dim,e::es,acc,true);
case (dim,e::es,acc,changed) then simplifyCat(dim,es,e::acc,changed);
end matchcontinue;
end simplifyCat;

protected function simplifyBuiltinStringFormat
input DAE.Exp exp;
input DAE.Exp len_exp;
Expand Down
2 changes: 1 addition & 1 deletion Compiler/FrontEnd/Patternm.mo
Expand Up @@ -1665,7 +1665,7 @@ algorithm
ty = Types.superType(ty, ty);
ty = Types.unboxedType(ty);
ty = Types.makeRegularTupleFromMetaTupleOnTrue(Types.allTuple(tys),ty);
exps = Types.matchTypes(exps, tys, ty, true);
(exps,_) = Types.matchTypes(exps, tys, ty, true);
cases = fixCaseReturnTypes2(cases,exps,info);
then (cases,ty);
else
Expand Down
4 changes: 2 additions & 2 deletions Compiler/FrontEnd/Static.mo
Expand Up @@ -617,7 +617,7 @@ algorithm
constList = Types.getConstList(propList);
c = Util.listFold(constList, Types.constAnd, DAE.C_CONST());
t = Types.boxIfUnboxedType(Util.listReduce(typeList, Types.superType));
es_1 = Types.matchTypes(es_1, typeList, t, true);
(es_1,_) = Types.matchTypes(es_1, typeList, t, true);
prop = DAE.PROP((DAE.T_LIST(t),NONE()),c);
then (cache,DAE.LIST(es_1),prop,st_2);
// ----------------------------------
Expand Down Expand Up @@ -5393,7 +5393,7 @@ algorithm
tys = Util.listMap(props,Types.getPropType);
ty::tys2 = Util.listMap1(tys,Types.makeNthDimUnknown,dim);
result_type = Util.listFold1(tys2,Types.arraySuperType,info,ty);
matrices_1 = Types.matchTypes(matrices_1,tys,result_type,false);
(matrices_1,tys) = Types.matchTypes(matrices_1,tys,result_type,false);
// true = sameDimensionsExceptionDimX(props,dim);
const2 = elabArrayConst(props);
const = Types.constAnd(const1, const2);
Expand Down
12 changes: 7 additions & 5 deletions Compiler/FrontEnd/Types.mo
Expand Up @@ -5571,18 +5571,20 @@ public function matchTypes
input Type expected;
input Boolean printFailtrace;
output list<DAE.Exp> outExps;
output list<Type> outTys;
algorithm
outExps := matchcontinue (exps,tys,expected,printFailtrace)
(outExps,outTys) := matchcontinue (exps,tys,expected,printFailtrace)
local
DAE.Type ty;
list<DAE.Type> otys;
DAE.Exp e;
case ({},{},_,_) then {};
case ({},{},_,_) then ({},{});
case (e::exps,ty::tys,expected,printFailtrace)
equation
(e,_) = matchType(e,ty,expected,printFailtrace);
exps = matchTypes(exps,tys,expected,printFailtrace);
(e,ty) = matchType(e,ty,expected,printFailtrace);
(exps,otys) = matchTypes(exps,tys,expected,printFailtrace);
then
e::exps;
(e::exps,ty::otys);
case (e::_,ty::_,expected,true)
equation
print("- Types.matchTypes failed for " +& ExpressionDump.printExpStr(e) +& " from " +& unparseType(ty) +& " to " +& unparseType(expected) +& "\n");
Expand Down
2 changes: 1 addition & 1 deletion Compiler/FrontEnd/ValuesUtil.mo
Expand Up @@ -1002,7 +1002,7 @@ algorithm
explist = Util.listMap(vallist, valueExp);
typelist = Util.listMap(vallist, Types.typeOfValue);
vt = Types.boxIfUnboxedType(Util.listReduce(typelist,Types.superType));
explist = Types.matchTypes(explist, typelist, vt, true);
(explist,_) = Types.matchTypes(explist, typelist, vt, true);
then DAE.LIST(explist);

/* MetaRecord */
Expand Down
67 changes: 43 additions & 24 deletions Compiler/Util/Debug.mo
Expand Up @@ -72,45 +72,64 @@ public function fprint
"function: fprint
author: LS
Flag controlled debugging"
input String inString1;
input String inString2;
input String flag;
input String str;
// annotation(__OpenModelica_EarlyInline=true);
algorithm
bprint(RTOpts.debugFlag(flag),str);
end fprint;

public function bprint
"function: bprint
author: LS
Boolean controlled debugging"
input Boolean cond;
input String str;
// annotation(__OpenModelica_EarlyInline=true);
algorithm
_ := matchcontinue (inString1,inString2)
local String flag,str;
case (flag,str)
_ := match (cond,str)
case (true,str)
equation
true = RTOpts.debugFlag(flag);
Print.printErrorBuf(str);
then
();
case (_,_) then ();
end matchcontinue;
end fprint;
then ();
else ();
end match;
end bprint;

public function fprintln
"function: fprintln
Flag controlled debugging, printing with newline."
input String inString1;
input String inString2;
public function bprintln
"function: bprintln
author: LS
Boolean controlled debugging"
input Boolean cond;
input String str;
// annotation(__OpenModelica_EarlyInline=true);
algorithm
_ := matchcontinue (inString1,inString2)
local String flag,str;
case (flag,str)
_ := match (cond,str)
case (true,str)
equation
true = RTOpts.debugFlag(flag);
Print.printErrorBuf(str);
Print.printErrorBuf("\n");
then
();
case (_,_) then ();
end matchcontinue;
then ();
else ();
end match;
end bprintln;

public function fprintln
"function: fprintln
Flag controlled debugging, printing with newline."
input String flag;
input String str;
// annotation(__OpenModelica_EarlyInline=true);
algorithm
bprintln(RTOpts.debugFlag(flag),str);
end fprintln;

public function fprintl
"function: fprintl
flag controlled debugging, printing of string list."
input String inString;
input list<String> inStringLst;
// annotation(__OpenModelica_EarlyInline=true);
algorithm
_ := matchcontinue (inString,inStringLst)
local
Expand Down

0 comments on commit 0988891

Please sign in to comment.