Skip to content

Commit

Permalink
- Fixed bugs where constant evaluation/simplification resulted in unb…
Browse files Browse the repository at this point in the history
…oxed expressions

git-svn-id: https://openmodelica.org/svn/OpenModelica/trunk@7555 f25d12d1-65f4-0310-ae8a-bbce733d8d8e
  • Loading branch information
sjoelund committed Dec 23, 2010
1 parent bff745f commit 809f69e
Show file tree
Hide file tree
Showing 10 changed files with 94 additions and 53 deletions.
5 changes: 4 additions & 1 deletion Compiler/BackEnd/SimCode.mo
Expand Up @@ -1188,9 +1188,12 @@ protected function isTrivialLiteralExp
input DAE.Exp exp;
algorithm
_ := match exp
case DAE.BOX(DAE.SCONST(_)) then fail();
case DAE.BOX(DAE.RCONST(_)) then fail();
case DAE.BOX(_) then ();
case DAE.ICONST(_) then ();
case DAE.RCONST(_) then ();
case DAE.BCONST(_) then ();
case DAE.RCONST(_) then ();
case DAE.LIST(valList={}) then ();
case DAE.META_OPTION(NONE()) then ();
case DAE.SHARED_LITERAL(index=_) then ();
Expand Down
18 changes: 17 additions & 1 deletion Compiler/FrontEnd/Expression.mo
Expand Up @@ -1199,11 +1199,27 @@ public function unboxExp
output DAE.Exp outExp;
algorithm
outExp := match (e)
local
DAE.ExpType ty;
case(DAE.BOX(e)) then e;
else e;
else
equation
ty = typeof(e);
then DAE.UNBOX(e,ty);
end match;
end unboxExp;

public function boxExp
"takes an expression and boxes it"
input DAE.Exp e;
output DAE.Exp outExp;
algorithm
outExp := match (e)
case (DAE.BOX(_)) then e;
else DAE.BOX(e);
end match;
end boxExp;

public function subscriptExp
"function: subscriptExp
Returns the expression in a subscript index.
Expand Down
13 changes: 11 additions & 2 deletions Compiler/FrontEnd/ExpressionDump.mo
Expand Up @@ -128,7 +128,10 @@ algorithm
s = "DAE.ET_COMPLEX(" +& typeVarsStr(vars) +& "):" +& ClassInf.printStateStr(ci);
then s;

case(_) then "#Expression.typeString failed#";
case (DAE.ET_METATYPE()) then "METATYPE";
case (DAE.ET_BOXED(_)) then "BOXED";

case(_) then "#ExpressionDump.typeString failed#";

end matchcontinue;
end typeString;
Expand Down Expand Up @@ -563,6 +566,7 @@ algorithm
list<DAE.Exp> aexpl;
list<list<tuple<DAE.Exp, Boolean>>> lstes;
Absyn.MatchType matchTy;
DAE.ExpType et;

case (DAE.END(), _, _, _) then "end";

Expand Down Expand Up @@ -894,7 +898,12 @@ algorithm
s = stringAppendList({s1,s2,"\n","#cases#\n","end ",s1});
then s;

case (DAE.SHARED_LITERAL(index=_), _, _, _) then "#SHARED LITERAL#";
case (DAE.SHARED_LITERAL(index=i,ty=et), _, _, _)
equation
s1 = intString(i);
s2 = typeString(et);
s = stringAppendList({"#SHARED_LITERAL_",s1," (",s2,")#"});
then s;

case (e, _, _, _)
equation
Expand Down
19 changes: 11 additions & 8 deletions Compiler/FrontEnd/ExpressionSimplify.mo
Expand Up @@ -428,11 +428,10 @@ algorithm
then
outExp;

case (e as DAE.CALL(path = Absyn.IDENT("stringAppendList"), builtin = true, expLst = expl))
case (DAE.CALL(path = Absyn.IDENT("stringAppendList"), builtin = true, expLst = {e}))
equation
expl = Util.listMap(expl, simplify1);
{DAE.LIST(valList = expl)} = expl;
then simplifyStringAppendList(expl,{});
DAE.LIST(valList = expl) = simplify1(e);
then simplifyStringAppendList(expl,{});
end matchcontinue;
end simplifyBuiltinCalls;

Expand All @@ -452,17 +451,21 @@ algorithm
DAE.Exp exp,exp1,exp2;
list<DAE.Exp> rest;
case ({},{}) then DAE.SCONST("");
case ({},{exp}) then exp;
case ({},{exp1,exp2}) then DAE.BINARY(exp2,DAE.ADD(DAE.ET_STRING()),exp1);
case ({},{exp}) then Expression.unboxExp(exp);
case ({},{exp1,exp2})
equation
exp1 = Expression.unboxExp(exp1);
exp2 = Expression.unboxExp(exp2);
then DAE.BINARY(exp2,DAE.ADD(DAE.ET_STRING()),exp1);
case ({},acc)
equation
acc = listReverse(acc);
exp = DAE.LIST(DAE.ET_STRING(),acc);
then Static.makeBuiltinCall("stringAppendList",{exp},DAE.ET_STRING());
case (DAE.SCONST(s1)::rest,DAE.SCONST(s2)::acc)
case (DAE.BOX(DAE.SCONST(s1))::rest,DAE.BOX(DAE.SCONST(s2))::acc)
equation
s = s2 +& s1;
then simplifyStringAppendList(rest,DAE.SCONST(s)::acc);
then simplifyStringAppendList(rest,DAE.BOX(DAE.SCONST(s))::acc);
case (exp::rest,acc) then simplifyStringAppendList(rest,exp::acc);
end match;
end simplifyStringAppendList;
Expand Down
18 changes: 0 additions & 18 deletions Compiler/FrontEnd/MetaUtil.mo
Expand Up @@ -773,24 +773,6 @@ end fixAstForUniontype;

/* ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^END^OF^HELPERFUNCTIONS^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ */

public function fixMetaTuple "If a Values.Value is a META_TUPLE, and the property is PROP_TUPLE,
convert the type to the correct format. Else return the type of a PROP"
input DAE.Properties prop;
output DAE.Type outType;
algorithm
outType := matchcontinue (prop)
local
Option<Absyn.Path> path;
list<DAE.Type> tys;
DAE.Type ty;
case DAE.PROP_TUPLE((DAE.T_TUPLE(tys),path),_)
equation
ty = (DAE.T_METATUPLE(tys),path);
then ty;
case DAE.PROP(ty,_) then ty;
end matchcontinue;
end fixMetaTuple;

public function constructorCallTypeToNamesAndTypes "Fetches the field names
and types from a record call or metarecord call"
input DAE.Type inType;
Expand Down
4 changes: 2 additions & 2 deletions Compiler/FrontEnd/Static.mo
Expand Up @@ -591,8 +591,8 @@ algorithm
typeList = Util.listMap(propList, Types.getPropType);
constList = Types.getConstList(propList);
c = Util.listFold(constList, Types.constAnd, DAE.C_CONST());
(es_1, t) = Types.listMatchSuperType(es_1, typeList, true);
t = Types.boxIfUnboxedType(t);
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);
Expand Down
46 changes: 34 additions & 12 deletions Compiler/FrontEnd/Types.mo
Expand Up @@ -1286,6 +1286,7 @@ algorithm
explist = Util.listMap(vl, ValuesUtil.valueExp);
ts = Util.listMap(vl, typeOfValue);
(_,tp) = listMatchSuperType(explist, ts, true);
tp = boxIfUnboxedType(tp);
then
((DAE.T_LIST(tp),NONE()));

Expand All @@ -1295,21 +1296,19 @@ algorithm
then tp;
case Values.OPTION(SOME(v))
equation
tp = typeOfValue(v);
tp = boxIfUnboxedType(typeOfValue(v));
tp = (DAE.T_METAOPTION(tp),NONE());
then tp;
case Values.META_TUPLE(valueLst = vs)
equation
ts = Util.listMap(vs, typeOfValue);
ts = Util.listMap(ts, boxIfUnboxedType);
then
((DAE.T_METATUPLE(ts),NONE()));

case (v)
equation
true = RTOpts.debugFlag("failtrace");
Debug.fprint("failtrace", "- Types.typeOfValue failed: ");
str = ValuesUtil.valString(v);
Debug.fprintln("failtrace", str);
str = "- Types.typeOfValue failed: " +& ValuesUtil.valString(v);
Error.addMessage(Error.INTERNAL_ERROR, {str});
then
fail();
end matchcontinue;
Expand Down Expand Up @@ -3846,7 +3845,7 @@ algorithm
end matchcontinue;
end matchProp;

protected function matchTypeList
public function matchTypeList
input list<DAE.Exp> exps;
input Type expType;
input Type expectedType;
Expand Down Expand Up @@ -4217,6 +4216,7 @@ algorithm
case (DAE.TUPLE(elist),(DAE.T_TUPLE(tupleType = tys1),_),(DAE.T_METATUPLE(tys2),p2),printFailtrace)
equation
true = RTOpts.acceptMetaModelicaGrammar();
tys2 = Util.listMap(tys2, boxIfUnboxedType);
(elist_1,tys_1) = matchTypeTuple(elist, tys1, tys2, printFailtrace);
then
(DAE.META_TUPLE(elist_1),(DAE.T_METATUPLE(tys_1),p2));
Expand All @@ -4231,6 +4231,7 @@ algorithm
(DAE.MATCHEXPRESSION(matchTy,inputs,localDecls,cases,et),expected);
case (DAE.META_TUPLE(elist),(DAE.T_METATUPLE(tys1),_),(DAE.T_METATUPLE(tys2),p2),printFailtrace)
equation
tys2 = Util.listMap(tys2, boxIfUnboxedType);
(elist_1,tys_1) = matchTypeTuple(elist, tys1, tys2, printFailtrace);
then
(DAE.META_TUPLE(elist_1),(DAE.T_METATUPLE(tys_1),p2));
Expand Down Expand Up @@ -4297,28 +4298,32 @@ algorithm
(e,t1) = matchType(e,t1,unboxedType(t2),printFailtrace);
t2 = (DAE.T_BOXED(t1),NONE());
t = elabType(t2);
then (DAE.BOX(e),t2);
e = Expression.boxExp(e);
then (e,t2);

case (e, t1 as (DAE.T_BOOL(_),_), (DAE.T_BOXED(t2),_),printFailtrace)
equation
(e,t1) = matchType(e,t1,unboxedType(t2),printFailtrace);
t2 = (DAE.T_BOXED(t1),NONE());
t = elabType(t2);
then (DAE.BOX(e),t2);
e = Expression.boxExp(e);
then (e,t2);

case (e, t1 as (DAE.T_REAL(_),_), (DAE.T_BOXED(t2),_),printFailtrace)
equation
(e,t1) = matchType(e,t1,unboxedType(t2),printFailtrace);
t2 = (DAE.T_BOXED(t1),NONE());
t = elabType(t2);
then (DAE.BOX(e),t2);
e = Expression.boxExp(e);
then (e,t2);

case (e, t1 as (DAE.T_STRING(_),_), (DAE.T_BOXED(t2),_),printFailtrace)
equation
(e,t1) = matchType(e,t1,unboxedType(t2),printFailtrace);
t2 = (DAE.T_BOXED(t1),NONE());
t = elabType(t2);
then (DAE.BOX(e),t2);
e = Expression.boxExp(e);
then (e,t2);

case (e as DAE.CALL(path = path1, expLst = elist), t1 as (DAE.T_COMPLEX(complexClassType = ClassInf.RECORD(_), complexVarLst = v),SOME(path2)), (DAE.T_BOXED(t2),_),printFailtrace)
equation
Expand Down Expand Up @@ -5191,7 +5196,10 @@ algorithm
outType := matchcontinue ty
local
list<Type> tys;
case ((DAE.T_TUPLE(tys),_)) then ((DAE.T_METATUPLE(tys),NONE()));
case ((DAE.T_TUPLE(tys),_))
equation
tys = Util.listMap(tys, boxIfUnboxedType);
then ((DAE.T_METATUPLE(tys),NONE()));
case ty then Util.if_(isBoxedType(ty), ty, (DAE.T_BOXED(ty),NONE()));
end matchcontinue;
end boxIfUnboxedType;
Expand Down Expand Up @@ -5311,28 +5319,38 @@ algorithm
case ((DAE.T_TUPLE(type_list1),_),(DAE.T_METATUPLE(type_list2),_))
equation
type_list1 = Util.listMap(type_list1, boxIfUnboxedType);
type_list2 = Util.listMap(type_list2, boxIfUnboxedType);
type_list1 = Util.listThreadMap(type_list1,type_list2,superType);
then ((DAE.T_METATUPLE(type_list1),NONE()));
case ((DAE.T_METATUPLE(type_list1),_),(DAE.T_TUPLE(type_list2),_))
equation
type_list1 = Util.listMap(type_list1, boxIfUnboxedType);
type_list2 = Util.listMap(type_list2, boxIfUnboxedType);
type_list1 = Util.listThreadMap(type_list1,type_list2,superType);
then ((DAE.T_METATUPLE(type_list1),NONE()));
case ((DAE.T_METATUPLE(type_list1),_),(DAE.T_METATUPLE(type_list2),_))
equation
type_list1 = Util.listMap(type_list1, boxIfUnboxedType);
type_list2 = Util.listMap(type_list2, boxIfUnboxedType);
type_list1 = Util.listThreadMap(type_list1,type_list2,superType);
then ((DAE.T_METATUPLE(type_list1),NONE()));

case ((DAE.T_LIST(t1),_),(DAE.T_LIST(t2),_))
equation
t1 = boxIfUnboxedType(t1);
t2 = boxIfUnboxedType(t2);
tp = superType(t1,t2);
then ((DAE.T_LIST(tp),NONE()));
case ((DAE.T_METAOPTION(t1),_),(DAE.T_METAOPTION(t2),_))
equation
t1 = boxIfUnboxedType(t1);
t2 = boxIfUnboxedType(t2);
tp = superType(t1,t2);
then ((DAE.T_METAOPTION(tp),NONE()));
case ((DAE.T_META_ARRAY(t1),_),(DAE.T_META_ARRAY(t2),_))
equation
t1 = boxIfUnboxedType(t1);
t2 = boxIfUnboxedType(t2);
tp = superType(t1,t2);
then ((DAE.T_META_ARRAY(tp),NONE()));

Expand Down Expand Up @@ -5550,18 +5568,22 @@ algorithm
case ((DAE.T_LIST(t1),_),prefix,bindings,info)
equation
t2 = fixPolymorphicRestype2(t1, prefix,bindings, info);
t2 = boxIfUnboxedType(t2);
then ((DAE.T_LIST(t2),NONE()));
case ((DAE.T_META_ARRAY(t1),_),prefix,bindings,info)
equation
t2 = fixPolymorphicRestype2(t1,prefix,bindings, info);
t2 = boxIfUnboxedType(t2);
then ((DAE.T_META_ARRAY(t2),NONE()));
case ((DAE.T_METAOPTION(t1),_),prefix,bindings,info)
equation
t2 = fixPolymorphicRestype2(t1, prefix,bindings, info);
t2 = boxIfUnboxedType(t2);
then ((DAE.T_METAOPTION(t2),NONE()));
case ((DAE.T_METATUPLE(tys),_),prefix,bindings,info)
equation
tys = Util.listMap3(tys, fixPolymorphicRestype2, prefix, bindings, info);
tys = Util.listMap(tys, boxIfUnboxedType);
then ((DAE.T_METATUPLE(tys),NONE()));
case ((DAE.T_TUPLE(tys),_),prefix,bindings,info)
equation
Expand Down
2 changes: 1 addition & 1 deletion Compiler/FrontEnd/Values.mo
Expand Up @@ -113,7 +113,7 @@ uniontype Value
we need to propagate this value in order to avoid running the code over and over again.
This is mostly an optimization."
end META_FAIL;

end Value;

public uniontype IntRealOp
Expand Down
17 changes: 11 additions & 6 deletions Compiler/FrontEnd/ValuesUtil.mo
Expand Up @@ -985,20 +985,26 @@ algorithm
case (Values.OPTION(SOME(v)))
equation
e = valueExp(v);
(e,_) = Types.matchType(e, Types.typeOfValue(v), DAE.T_BOXED_DEFAULT, true);
then DAE.META_OPTION(SOME(e));

case (Values.OPTION(NONE())) then DAE.META_OPTION(NONE());

case (Values.META_TUPLE(vallist))
equation
explist = Util.listMap(vallist, valueExp);
typelist = Util.listMap(vallist, Types.typeOfValue);
(explist,_) = Types.matchTypeTuple(explist, typelist, Util.listMap(typelist, Types.boxIfUnboxedType), true);
then DAE.META_TUPLE(explist);

case (Values.LIST({})) then DAE.LIST(DAE.ET_OTHER(),{});

case (Values.LIST(vallist))
equation
explist = Util.listMap(vallist, valueExp);
typelist = Util.listMap(vallist, Types.typeOfValue);
(explist,vt) = Types.listMatchSuperType(explist, typelist, true);
vt = Types.boxIfUnboxedType(Util.listReduce(typelist,Types.superType));
explist = Types.matchTypes(explist, typelist, vt, true);
t = Types.elabType(vt);
then DAE.LIST(t, explist);

Expand All @@ -1014,9 +1020,8 @@ algorithm

case (v)
equation
Debug.fprintln("failtrace", "ValuesUtil.valueExp failed for "+&valString(v)+&"\n");

Error.addMessage(Error.INTERNAL_ERROR, {"ValuesUtil.valueExp failed"});
s = "ValuesUtil.valueExp failed for " +& valString(v);
Error.addMessage(Error.INTERNAL_ERROR, {s});
then
fail();
end matchcontinue;
Expand Down Expand Up @@ -2034,9 +2039,9 @@ algorithm
equation
Print.printBuf("fail()");
then ();
case _
else
equation
Debug.fprintln("failtrace", "- ValuesUtil.valString2 failed");
Error.addMessage(Error.INTERNAL_ERROR, {"ValuesUtil.valString2 failed"});
then
fail();
end matchcontinue;
Expand Down

0 comments on commit 809f69e

Please sign in to comment.