Skip to content

Commit

Permalink
- Annotated a few functions with __OpenModelica_EarlyInline
Browse files Browse the repository at this point in the history
  - A NEW (r204+) OMDev or MetaModelica/RML is needed for this (older RML version do not parse annotations in regular functions):
    OMDEV: http://www.ida.liu.se/~adrpo/omc/omdev/mingw/
    Debian/Ubuntu Linux: apt-get update && apt-get upgrade rml-mmc
    OSX: port update && port upgrade rml-mmc
- Also fixed a bug where inlined call expressions were not boxed
- TODO: Propagate the inline annotation of functions in crefs that are pointers to a specific function


git-svn-id: https://openmodelica.org/svn/OpenModelica/trunk@7735 f25d12d1-65f4-0310-ae8a-bbce733d8d8e
  • Loading branch information
sjoelund committed Jan 21, 2011
1 parent 92d5d03 commit 83125e9
Show file tree
Hide file tree
Showing 9 changed files with 91 additions and 35 deletions.
2 changes: 1 addition & 1 deletion Compiler/FrontEnd/ClassInf.mo
Expand Up @@ -443,7 +443,7 @@ algorithm
case (BLOCK(path = p),NEWDEF()) then BLOCK(p);
case (CONNECTOR(path = p,isExpandable=isExpandable),NEWDEF()) then CONNECTOR(p,isExpandable);
case (TYPE(path = p),NEWDEF()) then TYPE(p); // A type can be constructed with long definition
case (PACKAGE(path = p),NEWDEF()) then PACKAGE(p);
case (PACKAGE(path = p),NEWDEF()) then PACKAGE(p);
case (FUNCTION(path = p),NEWDEF()) then FUNCTION(p);
case (ENUMERATION(path = p),NEWDEF()) then ENUMERATION(p);
case (IS_NEW(path = p),NEWDEF()) then IS_NEW(p);
Expand Down
2 changes: 2 additions & 0 deletions Compiler/FrontEnd/ComponentReference.mo
Expand Up @@ -173,6 +173,7 @@ public function makeCrefIdent
input DAE.ExpType identType "type of the identifier, without considering the subscripts";
input list<DAE.Subscript> subscriptLst;
output DAE.ComponentRef outCrefIdent;
annotation(__OpenModelica_EarlyInline = true);
algorithm
outCrefIdent := DAE.CREF_IDENT(ident, identType, subscriptLst); // shareCref(DAE.CREF_IDENT(ident, identType, subscriptLst));
end makeCrefIdent;
Expand All @@ -185,6 +186,7 @@ public function makeCrefQual
input list<DAE.Subscript> subscriptLst;
input DAE.ComponentRef componentRef;
output DAE.ComponentRef outCrefQual;
annotation(__OpenModelica_EarlyInline = true);
protected
DAE.ComponentRef subCref;
algorithm
Expand Down
1 change: 1 addition & 0 deletions Compiler/FrontEnd/DAE.mo
Expand Up @@ -958,6 +958,7 @@ uniontype ExpType "- Basic types
end ET_FUNCTION_REFERENCE_VAR;
record ET_FUNCTION_REFERENCE_FUNC "MetaModelica Function Reference that is a direct reference to a function"
Boolean builtin;
ExpType resType "type of the non-boxptr function";
end ET_FUNCTION_REFERENCE_FUNC;

record ET_METATYPE "MetaModelica boxed types (any)" end ET_METATYPE;
Expand Down
62 changes: 41 additions & 21 deletions Compiler/FrontEnd/Expression.mo
Expand Up @@ -1188,7 +1188,7 @@ algorithm
outExp := match (e)
local
DAE.ExpType ty;
case (DAE.BOX(e)) then e;
case (DAE.BOX(e)) then unboxExp(e);
else e;
end match;
end unboxExp;
Expand Down Expand Up @@ -4135,15 +4135,27 @@ algorithm
((expl_1,ext_arg_1)) = traverseExpListTopDown(expl, rel, ext_arg);
then
((DAE.METARECORDCALL(fn,expl_1,fieldNames,i),ext_arg_1));

case (DAE.UNBOX(e1,tp),rel,ext_arg)
equation
((e1_1,ext_arg_1)) = traverseExpTopDown(e1, rel, ext_arg);
then
((DAE.UNBOX(e1_1,tp),ext_arg_1));

case (DAE.BOX(e1),rel,ext_arg)
equation
((e1_1,ext_arg_1)) = traverseExpTopDown(e1, rel, ext_arg);
then
((DAE.BOX(e1_1),ext_arg_1));

// ---------------------

case (e,rel,ext_arg)
equation
str = ExpressionDump.printExpStr(e);
str = "Expression.traverseExpTopDown1 not implemented correctly: " +& str;
Error.addMessage(Error.INTERNAL_ERROR, {str});
then
fail();
then fail();
end matchcontinue;
end traverseExpTopDown1;

Expand Down Expand Up @@ -5311,7 +5323,9 @@ public function isCrefScalar
output Boolean isScalar;
algorithm
isScalar := matchcontinue(inExp)
local ComponentRef cr; Boolean b;
local
ComponentRef cr;
Boolean b;

case DAE.CREF(ty = DAE.ET_ARRAY(ty = _))
equation
Expand All @@ -5322,7 +5336,7 @@ algorithm

case DAE.CREF(ty = _) then true;

case _ then false;
else false;
end matchcontinue;
end isCrefScalar;

Expand All @@ -5345,12 +5359,13 @@ algorithm
then false;

/* For several terms, all must be positive or zero and at least one must be > 0 */
case(e) equation
(terms_ as _::_) = terms(e);
true = Util.listMapAllValue(terms_,expIsPositiveOrZero,true);
_::_ = Util.listSelect(terms_,expIsPositive);
then
false;
case(e)
equation
(terms_ as _::_) = terms(e);
true = Util.listMapAllValue(terms_,expIsPositiveOrZero,true);
_::_ = Util.listSelect(terms_,expIsPositive);
then
false;

case(e) then true;
end matchcontinue;
Expand All @@ -5366,15 +5381,16 @@ See also expIsPositiveOrZero.
output Boolean res;
algorithm
res := matchcontinue(e)
local DAE.Exp e1,e2;
local
DAE.Exp e1,e2;

/* constant >= 0 */
case(e) equation
true = isConst(e);
false = expReal(e) <. intReal(0);
then true;

case(_) then false;
else false;
end matchcontinue;
end expIsPositive;

Expand Down Expand Up @@ -5405,7 +5421,7 @@ algorithm
case(DAE.BINARY(e1,DAE.MUL(_),e2)) equation
true = expEqual(e1,e2);
then true;
case(_) then false;
else false;
end matchcontinue;
end expIsPositiveOrZero;

Expand All @@ -5414,11 +5430,13 @@ public function isEven "returns true if expression is even"
output Boolean even;
algorithm
even := matchcontinue(e)
local Integer i;
case(DAE.ICONST(i)) equation
0 = intMod(i,2);
then true;
case(_) then false;
local
Integer i;
case(DAE.ICONST(i))
equation
0 = intMod(i,2);
then true;
else false;
end matchcontinue;
end isEven;

Expand All @@ -5429,7 +5447,7 @@ algorithm
res := matchcontinue(tp)
case(DAE.ET_REAL()) then true;
case(DAE.ET_INT()) then true;
case(_) then false;
else false;
end matchcontinue;
end isIntegerOrReal;

Expand Down Expand Up @@ -6213,7 +6231,7 @@ public function isBuiltinFunctionReference
output Boolean b;
algorithm
b := match exp
case DAE.CREF(ty=DAE.ET_FUNCTION_REFERENCE_FUNC(true)) then true;
case DAE.CREF(ty=DAE.ET_FUNCTION_REFERENCE_FUNC(builtin=true)) then true;
else false;
end match;
end isBuiltinFunctionReference;
Expand All @@ -6222,6 +6240,7 @@ public function makeCons "DAE.CONS"
input DAE.Exp car;
input DAE.Exp cdr;
output DAE.Exp exp;
annotation(__OpenModelica_EarlyInline = true);
algorithm
exp := DAE.CONS(car,cdr);
end makeCons;
Expand All @@ -6232,6 +6251,7 @@ public function makeBuiltinCall
input list<DAE.Exp> args;
input DAE.ExpType result_type;
output DAE.Exp call;
annotation(__OpenModelica_EarlyInline = true);
algorithm
call := DAE.CALL(Absyn.IDENT(name),args,false,true,result_type,DAE.NO_INLINE());
end makeBuiltinCall;
Expand Down
5 changes: 5 additions & 0 deletions Compiler/FrontEnd/ExpressionSimplify.mo
Expand Up @@ -423,6 +423,11 @@ algorithm
equation
DAE.BOX(e1_1) = simplify(e1);
then e1_1;

case DAE.IFEXP(e,DAE.BOX(e1),DAE.BOX(e2))
equation
e = simplify(DAE.IFEXP(e,e1,e2));
then DAE.BOX(e);
end matchcontinue;
end simplifyMetaModelica;

Expand Down
39 changes: 33 additions & 6 deletions Compiler/FrontEnd/Inline.mo
Expand Up @@ -58,6 +58,7 @@ protected import ComponentReference;
protected import Debug;
protected import DAEUtil;
protected import Expression;
protected import ExpressionDump;
protected import ExpressionSimplify;

public function inlineCalls
Expand Down Expand Up @@ -1149,8 +1150,8 @@ end getRhsExp;
protected function replaceArgs
"function: replaceArgs
finds DAE.CREF and replaces them with new exps if the cref is in the argmap"
input tuple<DAE.Exp, list<tuple<DAE.ComponentRef, DAE.Exp>>> inTuple;
output tuple<DAE.Exp, list<tuple<DAE.ComponentRef, DAE.Exp>>> outTuple;
input tuple<DAE.Exp, list<tuple<DAE.ComponentRef,DAE.Exp>>> inTuple;
output tuple<DAE.Exp, list<tuple<DAE.ComponentRef,DAE.Exp>>> outTuple;
algorithm
outTuple := matchcontinue(inTuple)
local
Expand Down Expand Up @@ -1179,21 +1180,47 @@ algorithm
e = ExpressionSimplify.simplify(e);
then
((e,argmap));
/* TODO: Use the inlineType of the function reference! */
case((DAE.CALL(path,expLst,tuple_,false,DAE.ET_METATYPE(),inlineType),argmap))
equation
cref = ComponentReference.pathToCref(path);
(e as DAE.CREF(componentRef=cref)) = getExpFromArgMap(argmap,cref);
(e as DAE.CREF(componentRef=cref,ty=ty)) = getExpFromArgMap(argmap,cref);
path = ComponentReference.crefToPath(cref);
expLst = Util.listMap(expLst,Expression.unboxExp);
b = Expression.isBuiltinFunctionReference(e);
e = DAE.CALL(path,expLst,tuple_,b,DAE.ET_METATYPE(),inlineType);
ty2 = functionReferenceType(ty);
e = DAE.CALL(path,expLst,tuple_,b,ty2,inlineType);
e = boxIfUnboxedFunRef(e,ty);
e = ExpressionSimplify.simplify(e);
then
((e,argmap));
then ((e,argmap));
case((e,argmap)) then ((e,argmap));
end matchcontinue;
end replaceArgs;

protected function boxIfUnboxedFunRef
input DAE.Exp exp;
input DAE.ExpType ty;
output DAE.Exp outExp;
algorithm
outExp := match (exp,ty)
case (exp,DAE.ET_FUNCTION_REFERENCE_FUNC(resType=DAE.ET_METATYPE())) then exp;
case (exp,DAE.ET_FUNCTION_REFERENCE_FUNC(resType=DAE.ET_STRING())) then exp;
case (exp,DAE.ET_FUNCTION_REFERENCE_FUNC(resType=_))
then DAE.BOX(exp);
else exp;
end match;
end boxIfUnboxedFunRef;

protected function functionReferenceType
input DAE.ExpType ty1;
output DAE.ExpType ty2;
algorithm
ty2 := match ty1
case DAE.ET_FUNCTION_REFERENCE_FUNC(resType=ty2) then ty2;
else ty1;
end match;
end functionReferenceType;

protected function getExpFromArgMap
"function: getExpFromArgMap
returns the exp from the given argmap with the given key"
Expand Down
7 changes: 4 additions & 3 deletions Compiler/FrontEnd/Static.mo
Expand Up @@ -10138,7 +10138,7 @@ algorithm
SCode.Accessibility acc,acc_1;
SCode.Variability variability;
Option<Absyn.Path> optPath;
DAE.Type t;
DAE.Type t,resTy;
DAE.TType tt;
DAE.Binding binding;
DAE.Exp exp,exp1,exp2,crefExp,expASUB;
Expand Down Expand Up @@ -10217,11 +10217,12 @@ algorithm
isBuiltinFn = not valueEq(DAE.FUNCTION_NOT_BUILTIN(),isBuiltin);
(tt,optPath) = t;
t = (tt, Util.if_(isBuiltinFn, SOME(path), optPath)) "some builtin functions store NONE() there";
(_,SOME(fpath)) = t;
(DAE.T_FUNCTION(funcResultType=resTy),SOME(fpath)) = t;
et = Types.elabType(resTy);
t = Types.makeFunctionPolymorphicReference(t);
c = Absyn.pathToCref(fpath);
expCref = ComponentReference.toExpCref(c);
exp = Expression.makeCrefExp(expCref,DAE.ET_FUNCTION_REFERENCE_FUNC(isBuiltinFn));
exp = Expression.makeCrefExp(expCref,DAE.ET_FUNCTION_REFERENCE_FUNC(isBuiltinFn,et));
// This is not done by lookup - only elabCall. So we should do it here.
(cache,Util.SUCCESS()) = instantiateDaeFunction(cache,env,path,isBuiltinFn,NONE(),true);
then
Expand Down
2 changes: 1 addition & 1 deletion Compiler/Script/CevalScript.mo
Expand Up @@ -4676,7 +4676,7 @@ algorithm
acc = Util.listConsOnTrue(not listMember(name,acc),name,acc);
then ((e,acc));
*/
case ((e as DAE.CREF(componentRef=cr,ty=DAE.ET_FUNCTION_REFERENCE_FUNC(false)),acc))
case ((e as DAE.CREF(componentRef=cr,ty=DAE.ET_FUNCTION_REFERENCE_FUNC(builtin=false)),acc))
equation
Absyn.QUALIFIED(name,Absyn.IDENT(_)) = ComponentReference.crefToPath(cr);
acc = Util.listConsOnTrue(not listMember(name,acc),name,acc);
Expand Down
6 changes: 3 additions & 3 deletions Compiler/Util/Debug.mo
Expand Up @@ -322,7 +322,7 @@ public function bcallret1
end FuncTypeType_aToType_b;
replaceable type Type_a subtypeof Any;
replaceable type Type_b subtypeof Any;
//annotation(__OpenModelica_EarlyInline = true);
annotation(__OpenModelica_EarlyInline = true);
algorithm
res := match (flag,func,arg,default)
case (true,_,_,_)
Expand Down Expand Up @@ -351,7 +351,7 @@ public function bcallret2
replaceable type Type_a subtypeof Any;
replaceable type Type_b subtypeof Any;
replaceable type Type_c subtypeof Any;
//annotation(__OpenModelica_EarlyInline = true);
annotation(__OpenModelica_EarlyInline = true);
algorithm
res := match (flag,func,arg1,arg2,default)
case (true,_,_,_,_)
Expand Down Expand Up @@ -383,7 +383,7 @@ public function bcallret3
replaceable type Type_b subtypeof Any;
replaceable type Type_c subtypeof Any;
replaceable type Type_d subtypeof Any;
//annotation(__OpenModelica_EarlyInline = true);
annotation(__OpenModelica_EarlyInline = true);
algorithm
res := match (flag,func,arg1,arg2,arg3,default)
case (true,_,_,_,_,_)
Expand Down

0 comments on commit 83125e9

Please sign in to comment.