Skip to content

Commit c065581

Browse files
author
Jens Frenkel
committed
- first attempt to inline function with multiple statements
git-svn-id: https://openmodelica.org/svn/OpenModelica/trunk@14226 f25d12d1-65f4-0310-ae8a-bbce733d8d8e
1 parent 2e1aa11 commit c065581

File tree

1 file changed

+115
-0
lines changed

1 file changed

+115
-0
lines changed

Compiler/FrontEnd/Inline.mo

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ protected import ExpressionSimplify;
6868
protected import Flags;
6969
protected import List;
7070
protected import Types;
71+
protected import VarTransform;
7172

7273

7374
public function inlineCalls
@@ -1462,11 +1463,14 @@ algorithm
14621463
list<DAE.Element> fn;
14631464
Absyn.Path p;
14641465
list<DAE.Exp> args;
1466+
DAE.ComponentRef cr;
14651467
list<DAE.ComponentRef> crefs;
14661468
list<tuple<DAE.ComponentRef, DAE.Exp>> argmap;
14671469
DAE.Exp newExp,newExp1, e1;
14681470
DAE.InlineType inlineType;
14691471
HashTableCG.HashTable checkcr;
1472+
list<DAE.Statement> stmts;
1473+
VarTransform.VariableReplacements repl;
14701474
case ((e1 as DAE.CALL(p,args,DAE.CALL_ATTR(inlineType=inlineType)),(fns,_)))
14711475
equation
14721476
false = Config.acceptMetaModelicaGrammar();
@@ -1484,10 +1488,121 @@ algorithm
14841488
((newExp1,(fns1,_))) = Expression.traverseExp(newExp,forceInlineCall,(fns,true));
14851489
then
14861490
((newExp1,(fns,true)));
1491+
case ((e1 as DAE.CALL(p,args,DAE.CALL_ATTR(inlineType=inlineType)),(fns,_)))
1492+
equation
1493+
false = Config.acceptMetaModelicaGrammar();
1494+
true = checkInlineType(inlineType,fns);
1495+
fn = getFunctionBody(p,fns);
1496+
// get inputs, body and outpout
1497+
(crefs,{cr},stmts) = getFunctionInputsOutputBody(fn,{},{},{});
1498+
// merge statements to one line
1499+
repl = mergeFunctionBody(stmts,VarTransform.emptyReplacements());
1500+
newExp = VarTransform.getReplacement(repl,cr);
1501+
argmap = List.threadTuple(crefs,args);
1502+
(argmap,checkcr) = extendCrefRecords(argmap,HashTableCG.emptyHashTable());
1503+
((newExp,(_,_,true))) = Expression.traverseExp(newExp,replaceArgs,(argmap,checkcr,true));
1504+
// compare types
1505+
true = checkExpsTypeEquiv(e1, newExp);
1506+
// for inlinecalls in functions
1507+
((newExp1,(fns1,_))) = Expression.traverseExp(newExp,forceInlineCall,(fns,true));
1508+
then
1509+
((newExp1,(fns,true)));
14871510
else inTuple;
14881511
end matchcontinue;
14891512
end forceInlineCall;
14901513

1514+
protected function mergeFunctionBody
1515+
input list<DAE.Statement> iStmts;
1516+
input VarTransform.VariableReplacements iRepl;
1517+
output VarTransform.VariableReplacements oRepl;
1518+
algorithm
1519+
oRepl := match(iStmts,iRepl)
1520+
local
1521+
list<DAE.Statement> stmts;
1522+
VarTransform.VariableReplacements repl;
1523+
DAE.ComponentRef cr;
1524+
DAE.Exp exp;
1525+
list<DAE.Exp> explst;
1526+
case ({},_) then iRepl;
1527+
case (DAE.STMT_ASSIGN(exp1 = DAE.CREF(componentRef = cr), exp = exp)::stmts,_)
1528+
equation
1529+
(exp,_) = VarTransform.replaceExp(exp,iRepl,NONE());
1530+
repl = VarTransform.addReplacementNoTransitive(iRepl,cr,exp);
1531+
then
1532+
mergeFunctionBody(stmts,repl);
1533+
case (DAE.STMT_ASSIGN_ARR(componentRef = cr, exp = exp)::stmts,_)
1534+
equation
1535+
(exp,_) = VarTransform.replaceExp(exp,iRepl,NONE());
1536+
repl = VarTransform.addReplacementNoTransitive(iRepl,cr,exp);
1537+
then
1538+
mergeFunctionBody(stmts,repl);
1539+
case (DAE.STMT_TUPLE_ASSIGN(expExpLst = explst, exp = exp)::stmts,_)
1540+
equation
1541+
(exp,_) = VarTransform.replaceExp(exp,iRepl,NONE());
1542+
repl = addTplAssignToRepl(explst,1,exp,iRepl);
1543+
then
1544+
mergeFunctionBody(stmts,repl);
1545+
end match;
1546+
end mergeFunctionBody;
1547+
1548+
protected function addTplAssignToRepl
1549+
input list<DAE.Exp> explst;
1550+
input Integer indx;
1551+
input DAE.Exp iExp;
1552+
input VarTransform.VariableReplacements iRepl;
1553+
output VarTransform.VariableReplacements oRepl;
1554+
algorithm
1555+
oRepl := match(explst,indx,iExp,iRepl)
1556+
local
1557+
VarTransform.VariableReplacements repl;
1558+
DAE.ComponentRef cr;
1559+
DAE.Exp exp;
1560+
list<DAE.Exp> rest;
1561+
DAE.Type tp;
1562+
case ({},_,_,_) then iRepl;
1563+
case (DAE.CREF(componentRef = cr,ty=tp)::rest,_,_,_)
1564+
equation
1565+
exp = DAE.TSUB(iExp,indx,tp);
1566+
repl = VarTransform.addReplacementNoTransitive(iRepl,cr,exp);
1567+
then
1568+
addTplAssignToRepl(rest,indx+1,iExp,repl);
1569+
end match;
1570+
end addTplAssignToRepl;
1571+
1572+
protected function getFunctionInputsOutputBody
1573+
input list<DAE.Element> fn;
1574+
input list<DAE.ComponentRef> iInputs;
1575+
input list<DAE.ComponentRef> iOutput;
1576+
input list<DAE.Statement> iBody;
1577+
output list<DAE.ComponentRef> oInputs;
1578+
output list<DAE.ComponentRef> oOutput;
1579+
output list<DAE.Statement> oBody;
1580+
algorithm
1581+
(oInputs,oOutput,oBody) := match(fn,iInputs,iOutput,iBody)
1582+
local
1583+
DAE.ComponentRef cr;
1584+
list<DAE.Statement> st;
1585+
list<DAE.Element> rest;
1586+
case ({},_,_,_) then (listReverse(iInputs),listReverse(iOutput),iBody);
1587+
case (DAE.VAR(componentRef=cr,direction=DAE.INPUT())::rest,_,_,_)
1588+
equation
1589+
(oInputs,oOutput,oBody) = getFunctionInputsOutputBody(rest,cr::iInputs,iOutput,iBody);
1590+
then
1591+
(oInputs,oOutput,oBody);
1592+
case (DAE.VAR(componentRef=cr,direction=DAE.OUTPUT())::rest,_,_,_)
1593+
equation
1594+
(oInputs,oOutput,oBody) = getFunctionInputsOutputBody(rest,iInputs,cr::iOutput,iBody);
1595+
then
1596+
(oInputs,oOutput,oBody);
1597+
case (DAE.ALGORITHM(algorithm_ = DAE.ALGORITHM_STMTS(st))::rest,_,_,_)
1598+
equation
1599+
st = listAppend(iBody,st);
1600+
(oInputs,oOutput,oBody) = getFunctionInputsOutputBody(rest,iInputs,iOutput,st);
1601+
then
1602+
(oInputs,oOutput,oBody);
1603+
end match;
1604+
end getFunctionInputsOutputBody;
1605+
14911606
protected function checkInlineType "
14921607
Author: Frenkel TUD, 2010-05"
14931608
input DAE.InlineType inIT;

0 commit comments

Comments
 (0)