Skip to content

Commit

Permalink
Changing the way functions are generated
Browse files Browse the repository at this point in the history
- No struct is returned. Outputs 2..n are passed by reference
  - We could implement isPresent in code generation if we wanted to now (but it was removed in Modelica 3.0)
- Many array functions now take a value as input instead of pointer
  - Since we no longer generate a temporary for all called functions, we cannot pass the pointer of a temporary to the function
  - Most of the simple array functions should be inlined or macro'd and common functions moved to base_array.h
- Fewer temporaries should be generated now. The compressed tarball is ~20% smaller
- NORETCALL statements in match-expressions now remove statements without effect (`_ := boolAnd()` and the like can be optimized away)
'''''Do make clean, especially if using 32-bit omc'''''


git-svn-id: https://openmodelica.org/svn/OpenModelica/trunk@19922 f25d12d1-65f4-0310-ae8a-bbce733d8d8e
  • Loading branch information
sjoelund committed Apr 2, 2014
1 parent 87d0fc3 commit f326687
Show file tree
Hide file tree
Showing 25 changed files with 999 additions and 974 deletions.
1 change: 1 addition & 0 deletions Compiler/BackEnd/SimCode.mo
Expand Up @@ -341,6 +341,7 @@ uniontype RecordDeclaration

record RECORD_DECL_FULL
String name "struct (record) name ? encoded";
Option<String> aliasName "alias of struct (record) name ? encoded. Code generators can generate an aliasing typedef using this, and avoid problems when casting a record from one type to another (*(othertype*)(&var)), which only works if you have a lhs value.";
Absyn.Path defPath "definition path";
list<Variable> variables "only name and type";
end RECORD_DECL_FULL;
Expand Down
78 changes: 69 additions & 9 deletions Compiler/BackEnd/SimCodeUtil.mo
Expand Up @@ -580,12 +580,15 @@ public function elaborateFunctions
protected
list<SimCode.Function> fns;
list<String> outRecordTypes;
HashTableStringToPath.HashTable ht;
algorithm
(extraRecordDecls, outRecordTypes) := elaborateRecordDeclarationsForMetarecords(literals, {}, {});
(functions, outRecordTypes, extraRecordDecls, outIncludes, includeDirs, libs) := elaborateFunctions2(program, daeElements, {}, outRecordTypes, extraRecordDecls, includes, {}, {});
extraRecordDecls := List.unique(extraRecordDecls);
(extraRecordDecls, _) := elaborateRecordDeclarationsFromTypes(metarecordTypes, extraRecordDecls, outRecordTypes);
extraRecordDecls := List.sort(extraRecordDecls, orderRecordDecls);
ht := HashTableStringToPath.emptyHashTableSized(BaseHashTable.lowBucketSize);
(extraRecordDecls,_) := List.mapFold(extraRecordDecls, aliasRecordDeclarations, ht);
end elaborateFunctions;

protected function elaborateFunctions2
Expand Down Expand Up @@ -3124,7 +3127,7 @@ algorithm
// true = ComponentReference.crefEqualNoStringCompare(cr, cr2);
(tp as DAE.T_COMPLEX(varLst=varLst, complexClassType=ClassInf.RECORD(path))) = Expression.typeof(e1);
// tmp
ident = System.stringReplace(Absyn.pathString(path), ".", "_");
ident = Absyn.pathStringReplaceDot(path, "_");
crtmp = ComponentReference.makeCrefIdent("$TMP_" +& ident +& intString(iuniqueEqIndex), tp, {});
tempvars = createTempVars(varLst, crtmp, itempvars);
// 0 = a - tmp
Expand All @@ -3146,7 +3149,7 @@ algorithm
// ((e2_1, (_, _))) = BackendDAEUtil.extendArrExp((e2, (NONE(), false)));
(tp as DAE.T_COMPLEX(varLst=varLst, complexClassType=ClassInf.RECORD(path))) = Expression.typeof(e2);
// tmp
ident = System.stringReplace(Absyn.pathString(path), ".", "_");
ident = Absyn.pathStringReplaceDot(path, "_");
crtmp = ComponentReference.makeCrefIdent("$TMP_" +& ident +& intString(iuniqueEqIndex), tp, {});
tempvars = createTempVars(varLst, crtmp, itempvars);
// 0 = a - tmp
Expand All @@ -3167,7 +3170,7 @@ algorithm
((e2_1, (_, _))) = BackendDAEUtil.extendArrExp((e2, (NONE(), false)));
// true = ComponentReference.crefEqualNoStringCompare(cr, cr2);
// tmp = f()
ident = System.stringReplace(Absyn.pathString(path), ".", "_");
ident = Absyn.pathStringReplaceDot(path, "_");
cr = ComponentReference.makeCrefIdent("$TMP_" +& ident +& intString(iuniqueEqIndex), tp, {});
e1_1 = Expression.crefExp(cr);
stms = DAE.STMT_ASSIGN(tp, e1_1, e2_1, source);
Expand All @@ -3188,7 +3191,7 @@ algorithm
((e1_1, (_, _))) = BackendDAEUtil.extendArrExp((e2, (NONE(), false)));
// true = ComponentReference.crefEqualNoStringCompare(cr, cr2);
// tmp = f()
ident = System.stringReplace(Absyn.pathString(path), ".", "_");
ident = Absyn.pathStringReplaceDot(path, "_");
cr = ComponentReference.makeCrefIdent("$TMP_" +& ident +& intString(iuniqueEqIndex), tp, {});
e2_1 = Expression.crefExp(cr);
stms = DAE.STMT_ASSIGN(tp, e2_1, e1_1, source);
Expand All @@ -3208,7 +3211,7 @@ algorithm
// true = ComponentReference.crefEqualNoStringCompare(cr, cr2);
// tmp = f()
tp = Expression.typeof(e1);
ident = System.stringReplace(Absyn.pathString(path), ".", "_");
ident = Absyn.pathStringReplaceDot(path, "_");
cr = ComponentReference.makeCrefIdent("$TMP_" +& ident +& intString(iuniqueEqIndex), tp, {});
crexplst = List.map1(expl, Expression.generateCrefsExpFromExp, cr);
stms = DAE.STMT_TUPLE_ASSIGN(tp, crexplst, e2, source);
Expand Down Expand Up @@ -5181,7 +5184,7 @@ algorithm
vars = List.map(varlst, typesVarNoBinding);
rt_1 = sname :: rt;
(accRecDecls, rt_2) = elaborateNestedRecordDeclarations(varlst, accRecDecls, rt_1);
recDecl = SimCode.RECORD_DECL_FULL(sname, name, vars);
recDecl = SimCode.RECORD_DECL_FULL(sname, NONE(), name, vars);
accRecDecls = List.appendElt(recDecl, accRecDecls);
then (accRecDecls, rt_2);

Expand All @@ -5202,7 +5205,7 @@ algorithm
then (accRecDecls, rt);

case (_, accRecDecls, rt) then
(SimCode.RECORD_DECL_FULL("#an odd record#", Absyn.IDENT("?noname?"), {}) :: accRecDecls , rt);
(SimCode.RECORD_DECL_FULL("#an odd record#", NONE(), Absyn.IDENT("?noname?"), {}) :: accRecDecls , rt);
end matchcontinue;
end elaborateRecordDeclarationsForRecord;

Expand Down Expand Up @@ -5886,7 +5889,7 @@ algorithm
((e2_1, (_, _))) = BackendDAEUtil.extendArrExp((e2, (NONE(), false)));
// true = ComponentReference.crefEqualNoStringCompare(cr, cr2);
// tmp = f()
ident = System.stringReplace(Absyn.pathString(path), ".", "_");
ident = Absyn.pathStringReplaceDot(path, "_");
cr1 = ComponentReference.makeCrefIdent("$TMP_" +& ident +& intString(iuniqueEqIndex), tp, {});
e1_1 = Expression.crefExp(cr1);
stms = DAE.STMT_ASSIGN(tp, e1_1, e2_1, source);
Expand All @@ -5911,7 +5914,7 @@ algorithm
((e1_1, (_, _))) = BackendDAEUtil.extendArrExp((e1, (NONE(), false)));
// true = ComponentReference.crefEqualNoStringCompare(cr, cr2);
// tmp = f()
ident = System.stringReplace(Absyn.pathString(path), ".", "_");
ident = Absyn.pathStringReplaceDot(path, "_");
cr1 = ComponentReference.makeCrefIdent("$TMP_" +& ident +& intString(iuniqueEqIndex), tp, {});
e2_1 = Expression.crefExp(cr1);
stms = DAE.STMT_ASSIGN(tp, e2_1, e1_1, source);
Expand Down Expand Up @@ -12057,4 +12060,61 @@ algorithm
end match;
end getProtected;

protected function aliasRecordDeclarations
input SimCode.RecordDeclaration inDecl;
input HashTableStringToPath.HashTable inHt;
output SimCode.RecordDeclaration decl;
output HashTableStringToPath.HashTable ht;
algorithm
(decl,ht) := match (inDecl,inHt)
local
list<SimCode.Variable> vars;
Absyn.Path name;
String str,sname;
Option<String> alias;
case (SimCode.RECORD_DECL_FULL(sname, _, name, vars),_)
equation
str = stringDelimitList(List.map(vars, variableString), "\n");
(alias,ht) = aliasRecordDeclarations2(str, name, inHt);
then (SimCode.RECORD_DECL_FULL(sname, alias, name, vars),ht);
else (inDecl,inHt);
end match;
end aliasRecordDeclarations;

protected function aliasRecordDeclarations2
input String str;
input Absyn.Path path;
input HashTableStringToPath.HashTable inHt;
output Option<String> alias;
output HashTableStringToPath.HashTable ht;
algorithm
(alias,ht) := matchcontinue (str,path,inHt)
local
String aliasStr;
case (_,_,_)
equation
aliasStr = Absyn.pathStringReplaceDot(BaseHashTable.get(str, inHt),"_");
then (SOME(aliasStr),inHt);
else
equation
ht = BaseHashTable.add((str,path),inHt);
then (NONE(),ht);
end matchcontinue;
end aliasRecordDeclarations2;

protected function variableString
input SimCode.Variable var;
output String str;
algorithm
str := match var
local
DAE.ComponentRef name;
DAE.Type ty;
case SimCode.VARIABLE(name=name, ty=ty)
then Types.unparseType(ty) +& " " +& ComponentReference.printComponentRefStr(name);
case SimCode.FUNCTION_PTR(name=str)
then "modelica_fnptr " +& str;
end match;
end variableString;

end SimCodeUtil;
15 changes: 15 additions & 0 deletions Compiler/FrontEnd/Algorithm.mo
Expand Up @@ -863,4 +863,19 @@ algorithm
DAE.STMT_ASSERT(cond=cond) := stmt;
end getAssertCond;

public function isNotDummyStatement
input DAE.Statement stmt;
output Boolean b;
algorithm
b := match stmt
local
DAE.Exp exp;
case DAE.STMT_NORETCALL(exp=exp)
equation
((_,b)) = Expression.traverseExp(exp,Expression.hasNoSideEffects,true);
then not b; // has side effects => this is an expression that could do something
else true;
end match;
end isNotDummyStatement;

end Algorithm;
10 changes: 6 additions & 4 deletions Compiler/FrontEnd/ExpressionSimplify.mo
Expand Up @@ -712,7 +712,7 @@ protected function simplifyMatch "simplifies MetaModelica match expressions"
input DAE.Exp exp;
output DAE.Exp outExp;
algorithm
outExp := match exp
outExp := matchcontinue exp
local
DAE.Exp e,e1,e2,e1_1,e2_1;
Boolean b,b1,b2;
Expand All @@ -725,29 +725,31 @@ algorithm
Option<Values.Value> v;
DAE.Type ty;
DAE.ReductionIterators riters;
case DAE.MATCHEXPRESSION(inputs={e}, localDecls={}, cases={
case DAE.MATCHEXPRESSION(inputs={e}, et=ty, 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))
})
equation
false = boolEq(b1,b2);
false = Types.isTuple(ty);
e1_1 = Util.if_(b1,e1,e2);
e2_1 = Util.if_(b1,e2,e1);
e = DAE.IFEXP(e, e1_1, e2_1);
then e;

case DAE.MATCHEXPRESSION(matchType=DAE.MATCH(switch=_), inputs={e}, localDecls={}, cases={
case DAE.MATCHEXPRESSION(matchType=DAE.MATCH(switch=_), et=ty, inputs={e}, localDecls={}, cases={
DAE.CASE(patterns={DAE.PAT_CONSTANT(exp=DAE.BCONST(b1))},localDecls={},body={},result=SOME(e1)),
DAE.CASE(patterns={DAE.PAT_WILD()},localDecls={},body={},result=SOME(e2))
})
equation
false = Types.isTuple(ty);
e1_1 = Util.if_(b1,e1,e2);
e2_1 = Util.if_(b1,e2,e1);
e = DAE.IFEXP(e, e1_1, e2_1);
then e;

else exp;
end match;
end matchcontinue;
end simplifyMatch;

protected function simplifyCast "help function to simplify1"
Expand Down
41 changes: 26 additions & 15 deletions Compiler/FrontEnd/Patternm.mo
Expand Up @@ -1908,11 +1908,16 @@ algorithm
(cache,body,elabResult,resultInfo,resType,st) = elabResultExp(cache,env,body,result,impl,st,performVectorization,pre,resultInfo);
(cache,dPatternGuard,st) = elabPatternGuard(cache,env,patternGuard,impl,st,performVectorization,pre,patternInfo);
localsTree = AvlTreeString.joinAvlTrees(matchExpLocalTree, caseLocalTree);
useTree = AvlTreeString.avlTreeNew();
// Start building the def-use chain bottom-up
useTree = AvlTreeString.avlTreeNew();
((_,useTree)) = Expression.traverseExp(DAE.META_OPTION(elabResult), useLocalCref, useTree);
(body,useTree) = statementListFindDeadStoreRemoveEmptyStatements(body,localsTree,useTree);
((_,useTree)) = Expression.traverseExp(DAE.META_OPTION(dPatternGuard), useLocalCref, useTree);
(elabPatterns,_) = traversePatternList(elabPatterns, checkDefUsePattern, (localsTree,useTree,patternInfo));
// Do the same thing again, for fun and glory
useTree = AvlTreeString.avlTreeNew();
((_,useTree)) = Expression.traverseExp(DAE.META_OPTION(elabResult), useLocalCref, useTree);
(body,useTree) = List.map1Fold(listReverse(body),statementFindDeadStore,localsTree,useTree);
body = listReverse(body);
(body,useTree) = statementListFindDeadStoreRemoveEmptyStatements(body,localsTree,useTree);
((_,useTree)) = Expression.traverseExp(DAE.META_OPTION(dPatternGuard), useLocalCref, useTree);
(elabPatterns,_) = traversePatternList(elabPatterns, checkDefUsePattern, (localsTree,useTree,patternInfo));
elabCase = DAE.CASE(elabPatterns, dPatternGuard, caseDecls, body, elabResult, resultInfo, 0, info);
Expand Down Expand Up @@ -2577,6 +2582,18 @@ algorithm
end match;
end addAliasesToEnv;

protected function statementListFindDeadStoreRemoveEmptyStatements
input list<DAE.Statement> inBody;
input AvlTreeString.AvlTree localsTree;
input AvlTreeString.AvlTree inUseTree;
output list<DAE.Statement> body;
output AvlTreeString.AvlTree useTree;
algorithm
(body,useTree) := List.map1Fold(listReverse(inBody),statementFindDeadStore,localsTree,inUseTree);
body := List.select(body,Algorithm.isNotDummyStatement);
body := listReverse(body);
end statementListFindDeadStoreRemoveEmptyStatements;

protected function statementFindDeadStore
input DAE.Statement inStatement;
input AvlTreeString.AvlTree localsTree;
Expand Down Expand Up @@ -2622,31 +2639,28 @@ algorithm
case (DAE.STMT_IF(exp=exp,statementLst=body,else_=else_,source=source),_,_)
equation
(else_,elseTree) = elseFindDeadStore(else_, localsTree, inUseTree);
(body,useTree) = List.map1Fold(listReverse(body),statementFindDeadStore,localsTree,inUseTree);
(body,useTree) = statementListFindDeadStoreRemoveEmptyStatements(body,localsTree,inUseTree);
((_,useTree)) = Expression.traverseExp(exp, useLocalCref, useTree);
useTree = AvlTreeString.joinAvlTrees(useTree,elseTree);
body = listReverse(body);
then (DAE.STMT_IF(exp,body,else_,source),useTree);

case (DAE.STMT_FOR(ty,b,id,index,exp,body,source),_,_)
equation
(body,useTree) = List.map1Fold(listReverse(body),statementFindDeadStore,localsTree,inUseTree);
(body,useTree) = statementListFindDeadStoreRemoveEmptyStatements(body,localsTree,inUseTree);
((_,useTree)) = Expression.traverseExp(exp, useLocalCref, useTree);
// TODO: We should remove ident from the use-tree in case of shadowing... But our avlTree cannot delete
useTree = AvlTreeString.joinAvlTrees(useTree,inUseTree);
body = listReverse(body);
then (DAE.STMT_FOR(ty,b,id,index,exp,body,source),useTree);

case (DAE.STMT_WHILE(exp=exp,statementLst=body,source=source),_,_)
equation
(body,useTree) = List.map1Fold(listReverse(body),statementFindDeadStore,localsTree,inUseTree);
(body,useTree) = statementListFindDeadStoreRemoveEmptyStatements(body,localsTree,inUseTree);
((_,useTree)) = Expression.traverseExp(exp, useLocalCref, useTree);
// The loop might not be entered just like if. The following should not remove all previous uses:
// while false loop
// return;
// end while;
useTree = AvlTreeString.joinAvlTrees(useTree,inUseTree);
body = listReverse(body);
then (DAE.STMT_WHILE(exp,body,source),useTree);

// No PARFOR in MetaModelica
Expand Down Expand Up @@ -2682,8 +2696,7 @@ algorithm
case (DAE.STMT_ARRAY_INIT(source=_),_,_) then (inStatement,inUseTree);
case (DAE.STMT_FAILURE(body=body,source=source),_,_)
equation
(body,useTree) = List.map1Fold(listReverse(body),statementFindDeadStore,localsTree,inUseTree);
body = listReverse(body);
(body,useTree) = statementListFindDeadStoreRemoveEmptyStatements(body,localsTree,inUseTree);
then (DAE.STMT_FAILURE(body,source),useTree);
end match;
end statementFindDeadStore;
Expand All @@ -2704,17 +2717,15 @@ algorithm
case (DAE.NOELSE(),_,_) then (inElse,inUseTree);
case (DAE.ELSEIF(exp,body,else_),_,_)
equation
(body,useTree) = List.map1Fold(listReverse(body),statementFindDeadStore,localsTree,inUseTree);
(body,useTree) = statementListFindDeadStoreRemoveEmptyStatements(body,localsTree,inUseTree);
((_,useTree)) = Expression.traverseExp(exp, useLocalCref, useTree);
(else_,elseTree) = elseFindDeadStore(else_, localsTree, inUseTree);
useTree = AvlTreeString.joinAvlTrees(useTree,elseTree);
body = listReverse(body);
else_ = DAE.ELSEIF(exp,body,else_);
then (else_,useTree);
case (DAE.ELSE(body),_,_)
equation
(body,useTree) = List.map1Fold(listReverse(body),statementFindDeadStore,localsTree,inUseTree);
body = listReverse(body);
(body,useTree) = statementListFindDeadStoreRemoveEmptyStatements(body,localsTree,inUseTree);
else_ = DAE.ELSE(body);
then (else_,useTree);
end match;
Expand Down

0 comments on commit f326687

Please sign in to comment.