Skip to content

Commit

Permalink
- Removed DAE.ExpType from DAE.LIST/CONS
Browse files Browse the repository at this point in the history
- Added some expression simplifications for MetaModelica (cons, listReverse, listAppend)


git-svn-id: https://openmodelica.org/svn/OpenModelica/trunk@7698 f25d12d1-65f4-0310-ae8a-bbce733d8d8e
  • Loading branch information
sjoelund committed Jan 14, 2011
1 parent 3817916 commit 21a4faf
Show file tree
Hide file tree
Showing 14 changed files with 111 additions and 93 deletions.
13 changes: 6 additions & 7 deletions Compiler/BackEnd/SimCode.mo
Expand Up @@ -1166,24 +1166,23 @@ algorithm
local
list<DAE.Exp> es;
DAE.ExpType ty;
case DAE.LIST(ty,es as _::_) then listToCons2(ty,es);
case DAE.LIST(es as _::_) then listToCons2(es);
end match;
end listToCons;

protected function listToCons2
"Converts a DAE.LIST to a chain of DAE.CONS"
input DAE.ExpType ty;
input list<DAE.Exp> es;
output DAE.Exp o;
algorithm
o := match (ty,es)
o := match es
local
DAE.Exp car,cdr;
case (ty,{}) then DAE.LIST(ty,{});
case (ty,car::es)
case ({}) then DAE.LIST({});
case (car::es)
equation
cdr = listToCons2(ty,es);
then DAE.CONS(ty,car,cdr);
cdr = listToCons2(es);
then DAE.CONS(car,cdr);
end match;
end listToCons2;

Expand Down
4 changes: 2 additions & 2 deletions Compiler/BackEnd/XMLDump.mo
Expand Up @@ -1842,12 +1842,12 @@ algorithm
// NOT PART OF THE MODELICA LANGUAGE
then ();
// MetaModelica list
case (DAE.LIST(_,es))
case (DAE.LIST(valList=es))
equation
// NOT PART OF THE MODELICA LANGUAGE
then ();
// MetaModelica list cons
case (DAE.CONS(_,e1,e2))
case (DAE.CONS(car=e1,cdr=e2))
equation
// NOT PART OF THE MODELICA LANGUAGE
then ();
Expand Down
2 changes: 0 additions & 2 deletions Compiler/FrontEnd/DAE.mo
Expand Up @@ -1111,12 +1111,10 @@ uniontype Exp "Expressions

/* Part of MetaModelica extension. KS */
record LIST "MetaModelica list"
ExpType ty;
list<Exp> valList;
end LIST;

record CONS "MetaModelica list cons"
ExpType ty;
Exp car;
Exp cdr;
end CONS;
Expand Down
28 changes: 18 additions & 10 deletions Compiler/FrontEnd/Expression.mo
Expand Up @@ -1509,8 +1509,8 @@ algorithm
case (DAE.SIZE(_,SOME(_))) then DAE.ET_ARRAY(DAE.ET_INT(),{DAE.DIM_UNKNOWN()});

// MetaModelica extension
case (DAE.LIST(ty = tp)) then DAE.ET_METATYPE(); // was tp, but the type of a LIST is a LIST
case (DAE.CONS(ty = tp)) then DAE.ET_METATYPE(); // CONS creates lists
case (DAE.LIST(valList = _)) then DAE.ET_METATYPE();
case (DAE.CONS(car = _)) then DAE.ET_METATYPE();
case (DAE.META_TUPLE(_)) then DAE.ET_METATYPE();
case (DAE.TUPLE(_)) then DAE.ET_METATYPE();
case (DAE.META_OPTION(_))then DAE.ET_METATYPE();
Expand Down Expand Up @@ -3685,18 +3685,18 @@ algorithm
((e,ext_arg_3));

// MetaModelica list
case ((e as DAE.CONS(tp,e1,e2)),rel,ext_arg)
case ((e as DAE.CONS(e1,e2)),rel,ext_arg)
equation
((e1_1,ext_arg_1)) = traverseExp(e1, rel, ext_arg);
((e2_1,ext_arg_2)) = traverseExp(e2, rel, ext_arg_1);
((e,ext_arg_3)) = rel((DAE.CONS(tp,e1_1,e2_1),ext_arg_2));
((e,ext_arg_3)) = rel((DAE.CONS(e1_1,e2_1),ext_arg_2));
then
((e,ext_arg_3));

case ((e as DAE.LIST(tp,expl)),rel,ext_arg)
case ((e as DAE.LIST(expl)),rel,ext_arg)
equation
((expl_1,ext_arg_1)) = traverseExpList(expl, rel, ext_arg);
((e,ext_arg_2)) = rel((DAE.LIST(tp,expl_1),ext_arg_1));
((e,ext_arg_2)) = rel((DAE.LIST(expl_1),ext_arg_1));
then
((e,ext_arg_2));

Expand Down Expand Up @@ -4095,18 +4095,18 @@ algorithm
((DAE.REDUCTION(path,e1_1,id,e2_1),ext_arg_2));

// MetaModelica list
case ((e as DAE.CONS(tp,e1,e2)),rel,ext_arg)
case ((e as DAE.CONS(e1,e2)),rel,ext_arg)
equation
((e1_1,ext_arg_1)) = traverseExpTopDown(e1, rel, ext_arg);
((e2_1,ext_arg_2)) = traverseExpTopDown(e2, rel, ext_arg_1);
then
((DAE.CONS(tp,e1_1,e2_1),ext_arg_2));
((DAE.CONS(e1_1,e2_1),ext_arg_2));

case ((e as DAE.LIST(tp,expl)),rel,ext_arg)
case ((e as DAE.LIST(expl)),rel,ext_arg)
equation
((expl_1,ext_arg_1)) = traverseExpListTopDown(expl, rel, ext_arg);
then
((DAE.LIST(tp,expl_1),ext_arg_1));
((DAE.LIST(expl_1),ext_arg_1));

case ((e as DAE.META_TUPLE(expl)),rel,ext_arg)
equation
Expand Down Expand Up @@ -6209,5 +6209,13 @@ algorithm
end match;
end isBuiltinFunctionReference;

public function makeCons "DAE.CONS"
input DAE.Exp car;
input DAE.Exp cdr;
output DAE.Exp exp;
algorithm
exp := DAE.CONS(car,cdr);
end makeCons;

end Expression;

8 changes: 4 additions & 4 deletions Compiler/FrontEnd/ExpressionDump.mo
Expand Up @@ -842,15 +842,15 @@ algorithm
s;

// MetaModelica list
case (DAE.LIST(_,es), _, _, _)
case (DAE.LIST(valList=es), _, _, _)
equation
s = Util.stringDelimitList(Util.listMap3(es,printExp2Str, stringDelimiter, opcreffunc, opcallfunc),",");
s = stringAppendList({"List(", s, ")"});
then
s;

// MetaModelica list cons
case (DAE.CONS(_,e1,e2), _, _, _)
case (DAE.CONS(car=e1,cdr=e2), _, _, _)
equation
s1 = printExp2Str(e1, stringDelimiter, opcreffunc, opcallfunc);
s2 = printExp2Str(e2, stringDelimiter, opcreffunc, opcallfunc);
Expand Down Expand Up @@ -2046,7 +2046,7 @@ algorithm
();

// MetaModelica list
case (DAE.LIST(_,es),_)
case (DAE.LIST(valList=es),_)
equation
Print.printBuf("List(");
printList(es, printExp, ",");
Expand Down Expand Up @@ -2077,7 +2077,7 @@ algorithm
();

// MetaModelica list cons
case (DAE.CONS(_,e1,e2),_)
case (DAE.CONS(car=e1,cdr=e2),_)
equation
Print.printBuf("listCons(");
printExp(e1);
Expand Down
64 changes: 58 additions & 6 deletions Compiler/FrontEnd/ExpressionSimplify.mo
Expand Up @@ -173,6 +173,13 @@ algorithm
false,matrix);
then
e;

// MetaModelica builtin operators are calls, which means this has to be done
// before the generic CALL case
case e
equation
true = RTOpts.acceptMetaModelicaGrammar();
then simplifyMetaModelica(e);

// simplify argument expression of functions
case DAE.CALL( path, exps_1, b,b2, t,b3)
Expand Down Expand Up @@ -307,6 +314,24 @@ algorithm
then
exp1;

// anything else
case e
then
e;
end matchcontinue;
end simplify1;

protected function simplifyMetaModelica "simplifies MetaModelica expressions"
input DAE.Exp exp;
output DAE.Exp outExp;
algorithm
outExp := matchcontinue exp
local
DAE.Exp e,e1,e2,e1_1,e2_1;
Boolean b1,b2;
DAE.ExpType tp;
Absyn.Path path;
list<DAE.Exp> el;
case DAE.MATCHEXPRESSION(inputs={e}, localDecls={}, cases={
DAE.CASE(patterns={DAE.PAT_CONSTANT(exp=DAE.BCONST(b1))},localDecls={},body={},result=SOME(e1)),
DAE.CASE(patterns={DAE.PAT_CONSTANT(exp=DAE.BCONST(b2))},localDecls={},body={},result=SOME(e2))
Expand All @@ -328,12 +353,39 @@ algorithm
e = DAE.IFEXP(e, e1_1, e2_1);
then simplify(e);

// anything else
case e
then
e;
case DAE.CALL(path=Absyn.IDENT("listAppend"),expLst={e1,e2})
equation
DAE.LIST(el) = simplify(e1);
el = listReverse(el);
e2_1 = simplify(e2);
e = Util.listFold(el, Expression.makeCons, e2_1);
then simplify(e);

case DAE.CALL(path=Absyn.IDENT("listAppend"),expLst={e1,e2},ty=tp)
equation
DAE.LIST(valList={}) = simplify(e2);
then simplify(e1);

case DAE.CALL(path=path as Absyn.IDENT("listReverse"),expLst={e1},ty=tp)
equation
DAE.LIST(el) = simplify(e1);
el = Util.listMap(el,simplify);
el = listReverse(el);
e1_1 = DAE.LIST(el);
then e1_1;

case DAE.LIST(el)
equation
el = Util.listMap(el,simplify);
then DAE.LIST(el);

case DAE.CONS(e1,e2)
equation
DAE.LIST(el) = simplify(e2);
e1_1 = simplify(e1);
then DAE.LIST(e1_1::el);
end matchcontinue;
end simplify1;
end simplifyMetaModelica;

protected function simplifyCast "help function to simplify1"
input DAE.Exp exp;
Expand Down Expand Up @@ -473,7 +525,7 @@ algorithm
case ({},acc)
equation
acc = listReverse(acc);
exp = DAE.LIST(DAE.ET_STRING(),acc);
exp = DAE.LIST(acc);
then Static.makeBuiltinCall("stringAppendList",{exp},DAE.ET_STRING());
case (DAE.SCONST(s1)::rest,DAE.SCONST(s2)::acc)
equation
Expand Down
31 changes: 4 additions & 27 deletions Compiler/FrontEnd/MetaUtil.mo
Expand Up @@ -64,29 +64,6 @@ algorithm
end matchcontinue;
end isList;

public function simplifyListExp "function: simplifyListExp
Author: KS
Used by Static.elabExp to simplify some cons/list expressions.
"
input DAE.ExpType t;
input DAE.Exp e1;
input DAE.Exp e2;
output DAE.Exp expOut;
algorithm
expOut :=
matchcontinue (t,e1,e2)
local
DAE.Exp localE1,localE2;
DAE.ExpType tLocal;
list<DAE.Exp> expList,expList2;
case (tLocal,localE1,DAE.LIST(_,expList))
equation
expList2 = localE1::expList;
then DAE.LIST(tLocal,expList2);
case (tLocal,localE1,localE2) then DAE.CONS(tLocal,localE1,localE2);
end matchcontinue;
end simplifyListExp;

public function transformArrayNodesToListNodes "function: transformArrayNodesToListNodes"
input list<Absyn.Exp> inList;
input list<Absyn.Exp> accList;
Expand All @@ -97,21 +74,21 @@ algorithm
local
list<Absyn.Exp> localAccList,es,restList;
Absyn.Exp firstExp;
case ({},localAccList) then localAccList;
case ({},localAccList) then listReverse(localAccList);
case (Absyn.ARRAY({}) :: restList,localAccList)
equation
localAccList = listAppend(localAccList,{Absyn.LIST({})});
localAccList = Absyn.LIST({})::localAccList;
localAccList = transformArrayNodesToListNodes(restList,localAccList);
then localAccList;
case (Absyn.ARRAY(es) :: restList,localAccList)
equation
es = transformArrayNodesToListNodes(es,{});
localAccList = listAppend(localAccList,{Absyn.LIST(es)});
localAccList = Absyn.LIST(es)::localAccList;
localAccList = transformArrayNodesToListNodes(restList,localAccList);
then localAccList;
case (firstExp :: restList,localAccList)
equation
localAccList = listAppend(localAccList,{firstExp});
localAccList = firstExp::localAccList;
localAccList = transformArrayNodesToListNodes(restList,localAccList);
then localAccList;
end matchcontinue;
Expand Down
2 changes: 1 addition & 1 deletion Compiler/FrontEnd/Patternm.mo
Expand Up @@ -222,7 +222,7 @@ algorithm
case (cache,env,Absyn.ARRAY({}),ty,info)
equation
et = validPatternType(DAE.T_LIST_DEFAULT,ty,lhs,info);
then (cache,DAE.PAT_CONSTANT(et,DAE.LIST(DAE.ET_OTHER(),{})));
then (cache,DAE.PAT_CONSTANT(et,DAE.LIST({})));

case (cache,env,Absyn.ARRAY(exps),ty,info)
equation
Expand Down
8 changes: 4 additions & 4 deletions Compiler/FrontEnd/PrefixUtil.mo
Expand Up @@ -816,16 +816,16 @@ algorithm
(cache,DAE.REDUCTION(fcn,exp_1,id,iterexp_1));

// MetaModelica extension. KS
case (cache,env,ih,DAE.LIST(t,es),p)
case (cache,env,ih,DAE.LIST(es),p)
equation
(cache,es_1) = prefixExpList(cache, env, ih, es, p);
then (cache,DAE.LIST(t,es_1));
then (cache,DAE.LIST(es_1));

case (cache,env,ih,DAE.CONS(t,e1,e2),p)
case (cache,env,ih,DAE.CONS(e1,e2),p)
equation
(cache,e1) = prefixExp(cache, env, ih, e1, p);
(cache,e2) = prefixExp(cache, env, ih, e2, p);
then (cache,DAE.CONS(t,e1,e2));
then (cache,DAE.CONS(e1,e2));

case (cache,env,ih,DAE.META_TUPLE(es),p)
equation
Expand Down
17 changes: 5 additions & 12 deletions Compiler/FrontEnd/Static.mo
Expand Up @@ -517,11 +517,7 @@ algorithm
(e1_1,_) = Types.matchType(e1_1, t1, t, true);
(e2_1,_) = Types.matchType(e2_1, (DAE.T_LIST(t2),NONE()), (DAE.T_LIST(t),NONE()), true);

// If the second expression is a DAE.LIST, then we can create a DAE.LIST
// instead of DAE.CONS
tp_1 = Types.elabType(t);
exp = MetaUtil.simplifyListExp(tp_1,e1_1,e2_1);

exp = DAE.CONS(e1_1,e2_1);
c = Types.constAnd(c1,c2);
prop = DAE.PROP((DAE.T_LIST(t),NONE()),c);
then (cache,exp,prop,st);
Expand All @@ -543,7 +539,7 @@ algorithm
equation
t = DAE.T_LIST_DEFAULT;
prop = DAE.PROP(t,DAE.C_CONST());
then (cache,DAE.LIST(DAE.ET_METATYPE(),{}),prop,st);
then (cache,DAE.LIST({}),prop,st);

case (cache,env,Absyn.LIST(es),impl,st,doVect,pre,info,_)
equation
Expand All @@ -554,8 +550,7 @@ algorithm
t = Types.boxIfUnboxedType(Util.listReduce(typeList, Types.superType));
es_1 = Types.matchTypes(es_1, typeList, t, true);
prop = DAE.PROP((DAE.T_LIST(t),NONE()),c);
tp_1 = Types.elabType(t);
then (cache,DAE.LIST(tp_1,es_1),prop,st_2);
then (cache,DAE.LIST(es_1),prop,st_2);
// ----------------------------------

// Pattern matching has its own module that handles match expressions
Expand Down Expand Up @@ -674,16 +669,14 @@ algorithm
list<DAE.Properties> propList;
list<DAE.Type> typeList;
DAE.ExpType t2;
case (cache,env,{},prop,_,st,_,_,info)
then (cache,DAE.LIST(DAE.ET_OTHER(),{}),prop,st);
case (cache,env,{},prop,_,st,_,_,info) then (cache,DAE.LIST({}),prop,st);
case (cache,env,expList,prop as DAE.PROP((DAE.T_LIST(t),_),c),impl,st,doVect,pre,info)
equation
(cache,expExpList,propList,st) = elabExpList(cache,env,expList,impl,st,doVect,pre,info);
typeList = Util.listMap(propList, Types.getPropType);
(expExpList, t) = Types.listMatchSuperType(expExpList, typeList, true);
t2 = Types.elabType(t);
then
(cache,DAE.LIST(t2,expExpList),DAE.PROP((DAE.T_LIST(t),NONE()),c),st);
(cache,DAE.LIST(expExpList),DAE.PROP((DAE.T_LIST(t),NONE()),c),st);
case (_,_,_,_,_,_,_,_,_)
equation
Debug.fprintln("failtrace", "- elabListExp failed, non-matching args in list constructor?");
Expand Down

0 comments on commit 21a4faf

Please sign in to comment.