Skip to content

Commit

Permalink
- try to improve readability of optimization source code (using space…
Browse files Browse the repository at this point in the history
…s, indention, …)

git-svn-id: https://openmodelica.org/svn/OpenModelica/trunk@17124 f25d12d1-65f4-0310-ae8a-bbce733d8d8e
  • Loading branch information
lochel committed Sep 7, 2013
1 parent e0cc6ae commit 001e6e6
Show file tree
Hide file tree
Showing 12 changed files with 491 additions and 455 deletions.
147 changes: 72 additions & 75 deletions Compiler/BackEnd/SimCodeMain.mo
Original file line number Diff line number Diff line change
Expand Up @@ -345,34 +345,34 @@ public function generateModelCode "
output Real timeSimCode;
output Real timeTemplates;
protected
list<String> includes,includeDirs;
list<String> includes, includeDirs;
list<SimCode.Function> functions;
String filename, funcfilename;
SimCode.SimCode simCode;
list<SimCode.RecordDeclaration> recordDecls;
BackendDAE.BackendDAE indexed_dlow,indexed_dlow_1;
BackendDAE.BackendDAE indexed_dlow, indexed_dlow_1;
Absyn.ComponentRef a_cref;
BackendQSS.QSSinfo qssInfo;
tuple<Integer,HashTableExpToIndex.HashTable,list<DAE.Exp>> literals;
tuple<Integer, HashTableExpToIndex.HashTable, list<DAE.Exp>> literals;
list<SimCode.JacobianMatrix> LinearMatrices;
algorithm
System.realtimeTick(GlobalScript.RT_CLOCK_SIMCODE);
a_cref := Absyn.pathToCref(className);
fileDir := CevalScript.getFileDir(a_cref, p);
(libs, includes, includeDirs, recordDecls, functions, outIndexedBackendDAE, _, literals) :=
SimCodeUtil.createFunctions(p, dae, inBackendDAE, className);
simCode := createSimCode(outIndexedBackendDAE,
simCode := createSimCode(outIndexedBackendDAE,
className, filenamePrefix, fileDir, functions, includes, includeDirs, libs, simSettingsOpt, recordDecls, literals, args);
timeSimCode := System.realtimeTock(GlobalScript.RT_CLOCK_SIMCODE);
Debug.execStat("SimCode",GlobalScript.RT_CLOCK_SIMCODE);
Debug.execStat("SimCode", GlobalScript.RT_CLOCK_SIMCODE);

System.realtimeTick(GlobalScript.RT_CLOCK_TEMPLATES);
callTargetTemplates(simCode,inBackendDAE,Config.simCodeTarget());
callTargetTemplates(simCode, inBackendDAE, Config.simCodeTarget());
timeTemplates := System.realtimeTock(GlobalScript.RT_CLOCK_TEMPLATES);
end generateModelCode;

protected function createSimCode
"SimCode generator switch - if the NUMPROC-Flag is set, the simcode will be extended with parallel informations."
protected function createSimCode "
SimCode generator switch - if the NUMPROC-Flag is set, the simcode will be extended with parallel informations."
input BackendDAE.BackendDAE inBackendDAE;
input Absyn.Path inClassName;
input String filenamePrefix;
Expand All @@ -386,87 +386,84 @@ protected function createSimCode
input tuple<Integer, HashTableExpToIndex.HashTable, list<DAE.Exp>> literals;
input Absyn.FunctionArgs args;
output SimCode.SimCode simCode;

algorithm
simCode := matchcontinue (inBackendDAE, inClassName, filenamePrefix, inString11, functions, externalFunctionIncludes, includeDirs, libs, simSettingsOpt, recordDecls, literals, args)
simCode := matchcontinue(inBackendDAE, inClassName, filenamePrefix, inString11, functions, externalFunctionIncludes, includeDirs, libs, simSettingsOpt, recordDecls, literals, args)
local
Integer numProc;
SimCode.SimCode tmpSimCode;
case(_,_,_,_,_,_,_,_,_,_,_,_)
equation
true = Flags.isSet(Flags.HPCOM);
numProc = Flags.getConfigInt(Flags.NUM_PROC);
true = (numProc > 0);
then
HpcOmSimCode.createSimCode(inBackendDAE, inClassName, filenamePrefix, inString11, functions, externalFunctionIncludes, includeDirs, libs, simSettingsOpt, recordDecls, literals, args);
else
equation
(tmpSimCode,_) = SimCodeUtil.createSimCode(inBackendDAE, inClassName, filenamePrefix, inString11, functions, externalFunctionIncludes, includeDirs, libs, simSettingsOpt, recordDecls, literals, args);
then tmpSimCode;
case(_, _, _, _, _, _, _, _, _, _, _, _) equation
true = Flags.isSet(Flags.HPCOM);
numProc = Flags.getConfigInt(Flags.NUM_PROC);
true = (numProc > 0);
then HpcOmSimCode.createSimCode(inBackendDAE, inClassName, filenamePrefix, inString11, functions, externalFunctionIncludes, includeDirs, libs, simSettingsOpt, recordDecls, literals, args);

else equation
(tmpSimCode, _) = SimCodeUtil.createSimCode(inBackendDAE, inClassName, filenamePrefix, inString11, functions, externalFunctionIncludes, includeDirs, libs, simSettingsOpt, recordDecls, literals, args);
then tmpSimCode;
end matchcontinue;
end createSimCode;

// TODO: use another switch ... later make it first class option like -target or so
// Update: inQSSrequiredData passed in order to call BackendQSS and generate the extra structures needed for QSS simulation.
protected function callTargetTemplates
"Generate target code by passing the SimCode data structure to templates."
protected function callTargetTemplates "
Generate target code by passing the SimCode data structure to templates."
input SimCode.SimCode simCode;
input BackendDAE.BackendDAE inQSSrequiredData;
input String target;
algorithm
_ := match (simCode,inQSSrequiredData,target)
_ := match(simCode, inQSSrequiredData, target)
local
BackendDAE.BackendDAE outIndexedBackendDAE;
BackendQSS.QSSinfo qssInfo;
String str;
SimCode.SimCode sc;

case (_,_,"CSharp")
equation
Tpl.tplNoret(CodegenCSharp.translateModel, simCode);
then ();
case (_,_,"Cpp")
equation
Tpl.tplNoret(CodegenCpp.translateModel, simCode);
then ();
case (_,_,"Adevs")
equation
Tpl.tplNoret(CodegenAdevs.translateModel, simCode);
then ();
case (_,outIndexedBackendDAE,"QSS")
/* as BackendDAE.DAE(eqs={ BackendDAE.EQSYSTEM( m=SOME(incidenceMatrix) ,mT=SOME(incidenceMatrixT), matching=BackendDAE.MATCHING(equationIndices, variableIndices,strongComponents)*/
equation
Debug.trace("Generating code for QSS solver\n");
(qssInfo,sc) = BackendQSS.generateStructureCodeQSS(outIndexedBackendDAE,simCode); //, equationIndices, variableIndices, incidenceMatrix, incidenceMatrixT, strongComponents,simCode);
Tpl.tplNoret2(CodegenQSS.translateModel, sc,qssInfo);
then ();
case (_,_,"C")
equation
Tpl.tplNoret2(SimCodeDump.dumpSimCode, simCode, false);
Tpl.tplNoret(CodegenC.translateModel, simCode);
then ();
case (_,_,"Dump")
equation
// Yes, do this better later on...
str = Tpl.tplString2(SimCodeDump.dumpSimCode, simCode, true);
0 = System.systemCall("saxonb-xslt -o '" +& str +& ".html' '" +& str +& ".xml' '" +& Settings.getInstallationDirectoryPath() +& "/share/omc/scripts/simcodedump.xsl'");
print("User-friendly html output to " +& str +& ".html - please enable javascript\n");
then ();
case (_,_,"XML")
equation
Tpl.tplNoret(CodegenXML.translateModel, simCode);
then ();
case (_,_,"Java")
equation
Tpl.tplNoret(CodegenJava.translateModel, simCode);
then ();
case (_,_,"None")
then ();
case (_,_,_)
equation
str = "Unknown template target: " +& target;
Error.addMessage(Error.INTERNAL_ERROR, {str});
then fail();
BackendDAE.BackendDAE outIndexedBackendDAE;
BackendQSS.QSSinfo qssInfo;
String str;
SimCode.SimCode sc;

case (_, _, "CSharp") equation
Tpl.tplNoret(CodegenCSharp.translateModel, simCode);
then ();

case (_, _, "Cpp") equation
Tpl.tplNoret(CodegenCpp.translateModel, simCode);
then ();

case (_, _, "Adevs") equation
Tpl.tplNoret(CodegenAdevs.translateModel, simCode);
then ();

case (_, outIndexedBackendDAE, "QSS") equation
/* as BackendDAE.DAE(eqs={ BackendDAE.EQSYSTEM( m=SOME(incidenceMatrix) , mT=SOME(incidenceMatrixT), matching=BackendDAE.MATCHING(equationIndices, variableIndices, strongComponents)*/
Debug.trace("Generating code for QSS solver\n");
(qssInfo, sc) = BackendQSS.generateStructureCodeQSS(outIndexedBackendDAE, simCode); //, equationIndices, variableIndices, incidenceMatrix, incidenceMatrixT, strongComponents, simCode);
Tpl.tplNoret2(CodegenQSS.translateModel, sc, qssInfo);
then ();

case (_, _, "C") equation
Tpl.tplNoret2(SimCodeDump.dumpSimCode, simCode, false);
Tpl.tplNoret(CodegenC.translateModel, simCode);
then ();

case (_, _, "Dump") equation
// Yes, do this better later on...
str = Tpl.tplString2(SimCodeDump.dumpSimCode, simCode, true);
0 = System.systemCall("saxonb-xslt -o '" +& str +& ".html' '" +& str +& ".xml' '" +& Settings.getInstallationDirectoryPath() +& "/share/omc/scripts/simcodedump.xsl'");
print("User-friendly html output to " +& str +& ".html - please enable javascript\n");
then ();

case (_, _, "XML") equation
Tpl.tplNoret(CodegenXML.translateModel, simCode);
then ();

case (_, _, "Java") equation
Tpl.tplNoret(CodegenJava.translateModel, simCode);
then ();

case (_, _, "None")
then ();

case (_, _, _) equation
str = "Unknown template target: " +& target;
Error.addMessage(Error.INTERNAL_ERROR, {str});
then fail();
end match;
end callTargetTemplates;

Expand Down
108 changes: 55 additions & 53 deletions Compiler/FrontEnd/DAEUtil.mo
Original file line number Diff line number Diff line change
Expand Up @@ -2608,7 +2608,8 @@ algorithm
end matchcontinue;
end evaluateAnnotation;

protected function evaluateAnnotationVisitor "author: Frenkel TUD, 2010-12"
protected function evaluateAnnotationVisitor "author: Frenkel TUD, 2010-12
helper of evaluateAnnotation"
input tuple<DAE.Exp,tuple<HashTable2.HashTable,Integer,Integer>> itpl;
output tuple<DAE.Exp,tuple<HashTable2.HashTable,Integer,Integer>> otpl;
algorithm
Expand Down Expand Up @@ -2663,7 +2664,8 @@ algorithm
end matchcontinue;
end evaluateAnnotationTraverse;

protected function replaceCrefInAnnotation
protected function replaceCrefInAnnotation "
helper of evaluateAnnotationTraverse"
input DAE.Exp inExp;
input HashTable2.HashTable inTable;
output DAE.Exp outExp;
Expand Down Expand Up @@ -5460,24 +5462,25 @@ end getDAEDeclsFromValueblocks;
public function transformationsBeforeBackend
input Env.Cache cache;
input list<Env.Frame> env;
input DAE.DAElist dae;
output DAE.DAElist d;
input DAE.DAElist inDAElist;
output DAE.DAElist outDAElist;
protected
list<DAE.Element> elts;
algorithm
DAE.DAE(elts) := dae;
elts := List.map1(elts,makeEvaluatedParamFinal,Env.getEvaluatedParams(cache));
d := DAE.DAE(elts);
DAE.DAE(elts) := inDAElist;
elts := List.map1(elts, makeEvaluatedParamFinal, Env.getEvaluatedParams(cache));
outDAElist := DAE.DAE(elts);
// Don't even run the function to try and do this; it doesn't work very well
// d := transformDerInline(d);
// outDAElist := transformDerInline(outDAElist);
end transformationsBeforeBackend;

protected function makeEvaluatedParamFinal
input DAE.Element ielt;
input HashTable.HashTable ht;
output DAE.Element oelt;
protected function makeEvaluatedParamFinal "
This function makes all evaluated parameters final."
input DAE.Element inElement;
input HashTable.HashTable ht "evaluated parameters";
output DAE.Element outElement;
algorithm
oelt := matchcontinue (ielt,ht)
outElement := matchcontinue(inElement, ht)
local
DAE.ComponentRef cr;
Option<DAE.VariableAttributes> varOpt;
Expand All @@ -5486,26 +5489,26 @@ algorithm
DAE.ElementSource source;
Option<SCode.Comment> cmt;
DAE.Element elt;
case (DAE.VAR(componentRef=cr,kind=DAE.PARAM(),variableAttributesOption=varOpt),_)
equation
_ = BaseHashTable.get(cr,ht);
// print("Make cr final " +& ComponentReference.printComponentRefStr(cr) +& "\n");
elt = setVariableAttributes(ielt,setFinalAttr(varOpt,true));
then elt;
case (DAE.COMP(id,elts,source,cmt),_)
equation
elts = List.map1(elts,makeEvaluatedParamFinal,ht);
then DAE.COMP(id,elts,source,cmt);
else ielt;

case (DAE.VAR(componentRef=cr, kind=DAE.PARAM(), variableAttributesOption=varOpt), _) equation
_ = BaseHashTable.get(cr, ht);
// print("Make cr final " +& ComponentReference.printComponentRefStr(cr) +& "\n");
elt = setVariableAttributes(inElement, setFinalAttr(varOpt, true));
then elt;

case (DAE.COMP(id, elts, source, cmt), _) equation
elts = List.map1(elts, makeEvaluatedParamFinal, ht);
then DAE.COMP(id, elts, source, cmt);

else inElement;
end matchcontinue;
end makeEvaluatedParamFinal;

public function setBindingSource
"@author: adrpo
public function setBindingSource "author: adrpo
This function will set the source of the binding"
input DAE.Binding inBinding;
input DAE.BindingSource bindingSource;
output DAE.Binding outBinding;
input DAE.Binding inBinding;
input DAE.BindingSource bindingSource;
output DAE.Binding outBinding;
algorithm
outBinding := match(inBinding, bindingSource)
local
Expand All @@ -5515,9 +5518,9 @@ algorithm
Values.Value valBound;

case (DAE.UNBOUND(), _) then inBinding;
case (DAE.EQBOUND(exp, evaluatedExp, cnst, _),_) then DAE.EQBOUND(exp, evaluatedExp, cnst, bindingSource);
case (DAE.VALBOUND(valBound, _),_) then DAE.VALBOUND(valBound, bindingSource);
end match;
case (DAE.EQBOUND(exp, evaluatedExp, cnst, _), _) then DAE.EQBOUND(exp, evaluatedExp, cnst, bindingSource);
case (DAE.VALBOUND(valBound, _), _) then DAE.VALBOUND(valBound, bindingSource);
end match;
end setBindingSource;

public function printBindingExpStr "prints a binding"
Expand Down Expand Up @@ -5606,9 +5609,10 @@ algorithm
end match;
end addDaeFunction;

public function addDaeExtFunction "add extermal functions present in the element list to the function tree
Note: normal functions are skipped.
See also addDaeFunction"
public function addDaeExtFunction "
add extermal functions present in the element list to the function tree
Note: normal functions are skipped.
See also addDaeFunction"
input list<DAE.Function> ifuncs;
input DAE.FunctionTree itree;
output DAE.FunctionTree outTree;
Expand All @@ -5632,8 +5636,8 @@ algorithm
end matchcontinue;
end addDaeExtFunction;

public function setAttrVariability
"Sets the variability attribute in an Attributes record."
public function setAttrVariability "
Sets the variability attribute in an Attributes record."
input DAE.Attributes inAttr;
input SCode.Variability inVar;
output DAE.Attributes outAttr;
Expand All @@ -5648,8 +5652,8 @@ algorithm
outAttr := DAE.ATTR(ct, prl, inVar, dir, io, vis);
end setAttrVariability;

public function getAttrVariability
"Get the variability attribute in an Attributes record."
public function getAttrVariability "
Get the variability attribute in an Attributes record."
input DAE.Attributes inAttr;
output SCode.Variability outVar;
algorithm
Expand Down Expand Up @@ -5848,8 +5852,8 @@ algorithm
DAE.VAR(componentRef=DAE.CREF_IDENT(ident=name)) := var;
end varName;

public function bindingExp
"help function to instBinding, returns the expression of a binding"
public function bindingExp "
help function to instBinding, returns the expression of a binding"
input DAE.Binding bind;
output Option<DAE.Exp> exp;
algorithm
Expand Down Expand Up @@ -5880,13 +5884,12 @@ algorithm
end match;
end isBound;

public function isCompleteFunction
"@author: adrpo
this function returns true if the given function is complete:
- has inputs
- has outputs
- has an algorithm section
note that record constructors are always considered complete"
public function isCompleteFunction "author: adrpo
this function returns true if the given function is complete:
- has inputs
- has outputs
- has an algorithm section
note that record constructors are always considered complete"
input DAE.Function f;
output Boolean isComplete;
algorithm
Expand All @@ -5909,11 +5912,10 @@ algorithm
end matchcontinue;
end isCompleteFunction;

public function isCompleteFunctionBody
"@author: adrpo
this function returns true if the given function body is complete"
input list<DAE.FunctionDefinition> functions;
output Boolean isComplete;
public function isCompleteFunctionBody "author: adrpo
this function returns true if the given function body is complete"
input list<DAE.FunctionDefinition> functions;
output Boolean isComplete;
algorithm
isComplete := matchcontinue(functions)
local
Expand Down

0 comments on commit 001e6e6

Please sign in to comment.