Skip to content

Commit 87488ed

Browse files
author
Rickard Lindberg
committed
Added more documentation to SimCode and did some refactoring.
git-svn-id: https://openmodelica.org/svn/OpenModelica/trunk@5270 f25d12d1-65f4-0310-ae8a-bbce733d8d8e
1 parent aa6e243 commit 87488ed

File tree

5 files changed

+249
-236
lines changed

5 files changed

+249
-236
lines changed

Compiler/SimCode.mo

Lines changed: 59 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,19 @@ package SimCode
3737

3838
RCS: $Id: SimCode.mo 3689 2009-02-26 07:38:30Z adrpo $
3939

40-
The entry point to this module is the translateModel function. (TODO: We need
41-
another entry point for translating only functions.)
40+
The entry points to this module are the translateModel function and the
41+
translateFunctions fuction.
4242

4343
Except for the entry points, the only other public functions are those that
4444
can be imported and called from templates.
45+
46+
The number of imported functions should be kept as low as possible. Today
47+
some of them are needed to generate target code from templates. More careful
48+
design of data structures passed to templates should reduce the number of
49+
imported functions needed.
50+
51+
Many of the functions in this module were originally copied from the Codegen
52+
and SimCodegen modules.
4553
"
4654

4755

@@ -59,7 +67,6 @@ public import Tpl;
5967
public import SCode;
6068
public import DAE;
6169

62-
// protected imports
6370
protected import DAEUtil;
6471
protected import SCodeUtil;
6572
protected import ClassInf;
@@ -70,7 +77,6 @@ protected import Debug;
7077
protected import Error;
7178
protected import Inst;
7279
protected import InnerOuter;
73-
// protected import Print;
7480
protected import Settings;
7581
protected import RTOpts;
7682
protected import System;
@@ -80,19 +86,14 @@ protected import ModUtil;
8086

8187

8288
public
83-
type Ident = String;
84-
type Type = Exp.Type;
85-
type Variables = list<Variable>;
86-
type Statements = list<Statement>;
87-
type Jacobian = list<tuple<Integer,Integer,Exp.Exp>>;
8889
type ExtConstructor = tuple<DAE.ComponentRef, String, list<DAE.Exp>>;
8990
type ExtDestructor = tuple<String, DAE.ComponentRef>;
9091
type ExtAlias = tuple<DAE.ComponentRef, DAE.ComponentRef>;
9192
type HelpVarInfo = tuple<Integer, Exp.Exp, Integer>; // helpvarindex, expression, whenclause index
9293

9394

9495
// Root data structure containing information required for templates to
95-
// generate simulation code.
96+
// generate simulation code for a Modelica model.
9697
uniontype SimCode
9798
record SIMCODE
9899
ModelInfo modelInfo;
@@ -118,6 +119,8 @@ uniontype SimCode
118119
end SIMCODE;
119120
end SimCode;
120121

122+
// Root data structure containing information required for templates to
123+
// generate C functions for Modelica/MetaModelica functions.
121124
uniontype FunctionCode
122125
record FUNCTIONCODE
123126
String name;
@@ -127,6 +130,7 @@ uniontype FunctionCode
127130
end FUNCTIONCODE;
128131
end FunctionCode;
129132

133+
// Container for metadata about a Modelica model.
130134
uniontype ModelInfo
131135
record MODELINFO
132136
String name;
@@ -136,6 +140,7 @@ uniontype ModelInfo
136140
end MODELINFO;
137141
end ModelInfo;
138142

143+
// Number of variables of various types in a Modelica model.
139144
uniontype VarInfo
140145
record VARINFO
141146
Integer numHelpVars;
@@ -152,6 +157,7 @@ uniontype VarInfo
152157
end VARINFO;
153158
end VarInfo;
154159

160+
// Container for metadata about variables in a Modelica model.
155161
uniontype SimVars
156162
record SIMVARS
157163
list<SimVar> stateVars;
@@ -166,6 +172,7 @@ uniontype SimVars
166172
end SIMVARS;
167173
end SimVars;
168174

175+
// Information about a variable in a Modelica model.
169176
uniontype SimVar
170177
record SIMVAR
171178
DAE.ComponentRef name;
@@ -175,23 +182,28 @@ uniontype SimVar
175182
Boolean isFixed;
176183
Exp.Type type_;
177184
Boolean isDiscrete;
178-
Option<DAE.ComponentRef> arrayCref; // if this var is the first in an array
185+
// arrayCref is the name of the array if this variable is the first in that
186+
// array
187+
Option<DAE.ComponentRef> arrayCref;
179188
end SIMVAR;
180189
end SimVar;
181190

191+
// Represents a Modelica or MetaModelica function.
192+
// TODO: I believe some of these fields can be removed. Check to see what is
193+
// used in templates.
182194
uniontype Function
183195
record FUNCTION
184196
Absyn.Path name;
185-
Variables inVars;
186-
Variables outVars;
197+
list<Variable> inVars;
198+
list<Variable> outVars;
187199
list<RecordDeclaration> recordDecls;
188200
list<Variable> functionArguments;
189201
list<Variable> variableDeclarations;
190202
list<Statement> body;
191203
end FUNCTION;
192204
record EXTERNAL_FUNCTION
193205
Absyn.Path name;
194-
Ident extName;
206+
String extName;
195207
list<Variable> funArgs;
196208
list<SimExtArg> extArgs;
197209
SimExtArg extReturn;
@@ -212,16 +224,17 @@ end Function;
212224

213225
uniontype RecordDeclaration
214226
record RECORD_DECL_FULL
215-
Ident name; // struct (record) name ? encoded
227+
String name; // struct (record) name ? encoded
216228
Absyn.Path defPath; //definition path
217-
Variables variables; //only name and type
229+
list<Variable> variables; //only name and type
218230
end RECORD_DECL_FULL;
219231
record RECORD_DECL_DEF
220232
Absyn.Path path; //definition path .. encoded ?
221-
list<Ident> fieldNames;
233+
list<String> fieldNames;
222234
end RECORD_DECL_DEF;
223235
end RecordDeclaration;
224236

237+
// Information about an argument to an external function.
225238
uniontype SimExtArg
226239
record SIMEXTARG
227240
DAE.ComponentRef cref;
@@ -250,20 +263,22 @@ uniontype Variable
250263
//the name should become a Path because templates will take responsibility
251264
//for ident encodings an alternative is to expect names with dot notation
252265
//inside the string; an encoding function sholud be then imported
253-
DAE.ComponentRef name "variable name";
254-
Type ty "variable type";
255-
Option<Exp.Exp> value "variable default value";
266+
DAE.ComponentRef name;
267+
Exp.Type ty;
268+
Option<Exp.Exp> value; // Default value
256269
list<DAE.Exp> instDims;
257270
end VARIABLE;
258271
end Variable;
259272

260-
// TODO: Replace this with just list<Algorithm.Statement>?
273+
// TODO: Replace Statement with just list<Algorithm.Statement>?
261274
uniontype Statement
262275
record ALGORITHM
263276
list<Algorithm.Statement> statementLst; // in functions
264277
end ALGORITHM;
265278
end Statement;
266279

280+
// Represents a single equation or a system of equations that must be solved
281+
// together.
267282
uniontype SimEqSystem
268283
record SES_RESIDUAL
269284
DAE.Exp exp;
@@ -321,6 +336,7 @@ uniontype ExtObjInfo
321336
end EXTOBJINFO;
322337
end ExtObjInfo;
323338

339+
// Platform specific parameters used when generating makefiles.
324340
uniontype MakefileParams
325341
record MAKEFILE_PARAMS
326342
String ccompiler;
@@ -335,8 +351,8 @@ uniontype MakefileParams
335351
end MAKEFILE_PARAMS;
336352
end MakefileParams;
337353

338-
/* Created ad used by templates to be able to generate different code depending
339-
on the context it is generated in. */
354+
// Constants of this type defined below are used by templates to be able to
355+
// generate different code depending on the context it is generated in.
340356
uniontype Context
341357
record SIMULATION
342358
Boolean genDiscrete;
@@ -352,11 +368,9 @@ public constant Context contextOther = OTHER();
352368

353369

354370
public function valueblockVars
355-
"Used by templates to get a list of variables from a valueblock.
356-
357-
It is a temporary fix and should be replaced."
371+
"Used by templates to get a list of variables from a valueblock."
358372
input DAE.Exp valueblock;
359-
output Variables vars;
373+
output list<Variable> vars;
360374
algorithm
361375
vars :=
362376
matchcontinue (valueblock)
@@ -371,6 +385,8 @@ algorithm
371385
end valueblockVars;
372386

373387
public function crefSubIsScalar
388+
"Used by templates to determine if a component reference's subscripts are
389+
scalar."
374390
input DAE.ComponentRef cref;
375391
output Boolean isScalar;
376392
list<DAE.Subscript> subs;
@@ -380,6 +396,7 @@ algorithm
380396
end crefSubIsScalar;
381397

382398
public function crefNoSub
399+
"Used by templates to determine if a component reference has no subscripts."
383400
input DAE.ComponentRef cref;
384401
output Boolean noSub;
385402
list<DAE.Subscript> subs;
@@ -391,6 +408,7 @@ algorithm
391408
end crefNoSub;
392409

393410
function crefSubs
411+
"Used by templates to get the subscript list from a component reference."
394412
input DAE.ComponentRef cref;
395413
output list<DAE.Subscript> subs;
396414
algorithm
@@ -411,6 +429,8 @@ algorithm
411429
end crefSubs;
412430

413431
public function buildCrefExpFromAsub
432+
"Used by templates to convert an ASUB expression to a component reference
433+
with subscripts."
414434
input DAE.Exp cref;
415435
input list<DAE.Exp> subs;
416436
output DAE.Exp cRefOut;
@@ -433,6 +453,7 @@ algorithm
433453
end buildCrefExpFromAsub;
434454

435455
public function incrementInt
456+
"Used by templates to create new integers that are increments of another."
436457
input Integer inInt;
437458
input Integer increment;
438459
output Integer outInt;
@@ -441,7 +462,9 @@ algorithm
441462
end incrementInt;
442463

443464
public function translateModel
444-
"One of the entry points."
465+
"Entry point to translate a Modelica model for simulation.
466+
467+
Called from other places in the compiler."
445468
input Env.Cache inCache;
446469
input Env.Env inEnv;
447470
input Absyn.Path className "path for the model";
@@ -528,7 +551,9 @@ algorithm
528551
end translateModel;
529552

530553
public function translateFunctions
531-
"One of the entry points."
554+
"Entry point to translate Modelica/MetaModelica functions to C functions.
555+
556+
Called from other places in the compiler."
532557
input String name;
533558
input list<DAE.Element> daeElements;
534559
input list<DAE.Type> metarecordTypes;
@@ -555,6 +580,7 @@ algorithm
555580
end matchcontinue;
556581
end translateFunctions;
557582

583+
558584
/* Finds the called functions in DAELow and transforms them to a list of
559585
libraries and a list of Function uniontypes. */
560586
protected function createFunctions
@@ -737,7 +763,7 @@ algorithm
737763
DAE.Element comp;
738764
list<String> rt, rt_1, struct_funrefs, struct_funrefs_int;
739765
list<Absyn.Path> funrefPaths;
740-
Variables outVars, inVars, biVars, funArgs, varDecls;
766+
list<Variable> outVars, inVars, biVars, funArgs, varDecls;
741767
list<RecordDeclaration> recordDecls;
742768
list<Statement> body;
743769
DAE.InlineType inl;
@@ -833,7 +859,7 @@ algorithm
833859
outVar := matchcontinue(inElement)
834860
local
835861
String name;
836-
Type expType;
862+
Exp.Type expType;
837863
DAE.Type daeType;
838864
Exp.ComponentRef id;
839865
list<Exp.Subscript> inst_dims;
@@ -1245,7 +1271,6 @@ algorithm
12451271
end matchcontinue;
12461272
end helpVarInfoFromWhenConditionChecks;
12471273

1248-
12491274
protected function buildDiscreteVarChanges "
12501275
For all discrete variables in the model, generate code that checks if they have changed and if so generate code
12511276
that add events to the event queue: if (change(<discretevar>)) { AddEvent(c1);...;AddEvent(cn)}
@@ -1464,7 +1489,7 @@ algorithm
14641489
String first_str, last_str, path_str;
14651490
list<String> res,strs,rest_strs,decl_strs,rt,rt_1,rt_2,record_definition,fieldNames;
14661491
list<RecordDeclaration> accRecDecls;
1467-
Variables vars;
1492+
list<Variable> vars;
14681493

14691494
RecordDeclaration recDecl;
14701495
case ((DAE.T_COMPLEX(complexClassType = ClassInf.RECORD(name), complexVarLst = varlst),SOME(path)), accRecDecls, rt)
@@ -1548,7 +1573,6 @@ algorithm
15481573
end matchcontinue;
15491574
end elaborateNestedRecordDeclarations;
15501575

1551-
15521576
protected function elaborateRecordDeclarationsForMetarecords
15531577
input list<Exp.Exp> inExpl;
15541578
input list<RecordDeclaration> inAccRecordDecls;
@@ -4239,8 +4263,6 @@ algorithm
42394263
end matchcontinue;
42404264
end addMissingEquations;
42414265

4242-
4243-
42444266
protected function buildDiscreteVarChangesVar2
42454267
"Help relation to buildDiscreteVarChangesVar
42464268
For an equation e (not a when equation) containing a discrete variable v, if e contains a
@@ -4533,7 +4555,6 @@ algorithm
45334555
end matchcontinue;
45344556
end findDiscreteEquation;
45354557

4536-
45374558
protected function isMixedSystem "function: isMixedSystem
45384559
author: PA
45394560

@@ -5572,7 +5593,7 @@ algorithm
55725593
local
55735594
String name;
55745595
Types.Type typesType;
5575-
Type expType;
5596+
Exp.Type expType;
55765597
case (DAE.TYPES_VAR(name=name, type_=typesType))
55775598
equation
55785599
expType = Types.elabType(typesType);

0 commit comments

Comments
 (0)