MicroModelicaCCompiler  4.5.3
main.cpp
Go to the documentation of this file.
1 /*****************************************************************************
2 
3  This file is part of QSS Solver.
4 
5  QSS Solver is free software: you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation, either version 3 of the License, or
8  (at your option) any later version.
9 
10  QSS Solver is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU General Public License for more details.
14 
15  You should have received a copy of the GNU General Public License
16  along with QSS Solver. If not, see <http://www.gnu.org/licenses/>.
17 
18  ******************************************************************************/
19 
20 #include <getopt.h>
21 #include <cstdio>
22 #include <cstdlib>
23 #include <iostream>
24 #include <list>
25 #include <string>
26 
27 #include <ast/ast_types.hpp>
28 #include <ast/parser/parse.hpp>
30 #include <generator/files.hpp>
31 #include <generator/generator.hpp>
32 #include <ir/class.hpp>
33 #include <ir/mmo_ir.hpp>
34 #include <ir/mmo_model_checker.hpp>
35 #include <ir/mmo_settings.hpp>
36 #include <util/compile_flags.hpp>
37 #include <util/error.hpp>
38 #include <util/logger.hpp>
39 #include <util/symbol_table.hpp>
40 #include <util/util.hpp>
41 #include <util/util_types.hpp>
42 
43 using namespace std;
44 using namespace MicroModelica::Generator;
45 using namespace MicroModelica::IR;
46 using namespace MicroModelica::Util;
47 
48 void usage()
49 {
50  cout << "Usage mmoc [options] file" << endl;
51  cout << "Compile MicroModelica files and generate a C file that implements a model suitable for the Stand--Alone QSS solver." << endl;
52  cout << endl;
53  cout << "-d <flag>, --debug <flag>" << endl;
54  cout << " Sets the simulation debug <flag>, the debug information is written in the log file generated " << endl;
55  cout << " by the Stand--Alone QSS solver and is an OR combination of the following possibilities: " << endl;
56  cout << " SD_DBG_All: Enable all the debug flags" << endl;
57  cout << " SD_DBG_Dt: Log dt parameter changes in parallel simulations" << endl;
58  cout << " SD_DBG_ExternalEvent: Output step information for each external event in parallel simulations" << endl;
59  cout
60  << " SD_DBG_InitValues: Output the simulation initialization, including initial state and state derivatives values"
61  << endl;
62  cout << " SD_DBG_Memory: Output memory footprint" << endl;
63  cout << " SD_DBG_Synchronize: Output synchronization information in parallel simulations" << endl;
64  cout << " SD_DBG_StepInfo: Output for each simulation step:" << endl;
65  cout << " ** The step time" << endl;
66  cout << " ** The type and index of the variable that changes" << endl;
67  cout << " SD_DBG_VarChanges: Output the model's state variable changes and the number of handler executions" << endl;
68  cout << " for each event defined in the model" << endl;
69  cout << " SD_DBG_WaitFor: Output synchronization information in parallel simulations" << endl;
70  cout << " SD_DBG_Weights: Save the output values generated by the SD_DBG_VarChanges into a binary file" << endl;
71  cout << "-e, --external-structure-file <file>" << endl;
72  cout << " Read model incidence matrices from <file>." << endl;
73  cout << "-f, --force " << endl;
74  cout << " Force external package compilation." << endl;
75  cout << "-h, --help Display this information and exit" << endl;
76  cout << "-i <path>, --include <path>" << endl;
77  cout << " Include <path> in the library path search. " << endl;
78  cout << "-o <file>, --output <file>" << endl;
79  cout << " Sets the output to <file>" << endl;
80  cout << "-s, --settings-only" << endl;
81  cout << " Generate only the settings (.ini) file." << endl;
82  cout << "-t, --test" << endl;
83  cout << " Don't generate GiNaC expressions for testing code structure." << endl;
84  cout << "-v, --version Display version information and exit" << endl;
85  cout << endl;
86  cout << "Report bugs to: fernandez@cifasis-conicet.gov.ar" << endl;
87  cout << "MMOC Compiler home page: https://github.com/CIFASIS/qss-solver/tree/qss-solver-dev/src/mmoc " << endl;
88 }
89 
90 void version()
91 {
92  cout << "MicroModelica C Compiler 4.5.3" << endl;
93  cout << "License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>" << endl;
94  cout << "This is free software: you are free to change and redistribute it." << endl;
95  cout << "There is NO WARRANTY, to the extent permitted by law." << endl;
96 }
97 
98 int parsePackages(AST_StringList imports, CompileFlags& flags, bool recompile)
99 {
100  int r = 0;
101  CompileFlags flg;
102  AST_StringListIterator it;
103  Utils& utils = Utils::instance();
104  foreach (it, imports) {
105  string i = *current_element(it);
106  string p = utils.packagePath(i, flags);
107  flags.addObject(utils.generatePath(p, utils.packageName(i) + ".c"));
108  if (!utils.searchCompiledPackage(i, flags) || recompile) {
109  string file_name = utils.generatePath(p, i + ".mo");
110  AST_StoredDefinition sd = nullptr;
111  sd = parseFile(file_name, &r);
112  Error::instance().setFile(file_name);
113  if (r == 0) {
114  ModelChecker mmo(file_name);
115  r = mmo.apply(sd);
116  if (r == 0) {
117  flg.setOutputFile(utils.generatePath(p, i));
118  flg.setPath(p);
119  parsePackages(sd->imports(), flg, recompile);
120  list<string> objects = flg.objects();
121  for (list<string>::iterator it = objects.begin(); it != objects.end(); it++) {
122  flags.addObject(*it);
123  }
124  Error::instance().setFile(file_name);
125  utils.setCompileFlags(flg);
126  MicroModelicaIR ir(file_name);
127  r = ir.apply(sd);
128  if (r == 0) {
129  Package package = ir.definition().package();
130  utils.setPackageFunctions(package.definitions());
131  utils.setPackagePrefix(package.prefix());
132  Generator gen(ir.definition(), flg);
133  r = gen.generate();
135  utils.setPackagePrefix("");
136  if (r == 0) {
137  string compiled_file_name = utils.generatePath(p, "pkg_" + i + ".moo");
138  const bool FULL_PATH = true;
139  Option<CompiledPackage> cp = utils.readPackage(compiled_file_name, FULL_PATH, i);
140  assert(cp);
141  utils.addCompiledFunctions(cp->definitions());
142  }
143  }
144  }
145  }
146  delete sd;
147  } else {
148  utils.setCompileFlags(flags);
149  Option<CompiledPackage> pd = utils.readPackage(i);
150  if (!pd) {
151  Error::instance().add(0, EM_CANT_OPEN_FILE, ER_Error, "%s.moo", i.c_str());
152  return -1;
153  }
154  ImportTable objects = pd->objects();
155 
157  for (string import = objects.begin(it); !objects.end(it); import = objects.next(it)) {
158  flags.addObject(import);
159  }
160  }
161  }
162  return r;
163 }
164 
165 int main(int argc, char** argv)
166 {
167  int r = 0;
168  int opt;
169  extern char* optarg;
170  char str_arg[128];
171  bool recompile = false;
172  bool settings = false;
173  CompileFlags flags;
174  while (true) {
175  static struct option long_options[] = {{"version", no_argument, 0, 'v'}, {"help", no_argument, 0, 'h'},
176  {"include", required_argument, 0, 'i'}, {"external-structure-file", required_argument, 0, 'e'},
177  {"force", no_argument, 0, 'f'}, {"settings-only", no_argument, 0, 's'},
178  {"test", no_argument, 0, 't'}, {"debug", required_argument, 0, 'd'},
179  {"output", required_argument, 0, 'o'}, {0, 0, 0, 0}};
180  int option_index = 0;
181  opt = getopt_long(argc, argv, "vhmfsti:e:d:o:", long_options, &option_index);
182  if (opt == EOF) break;
183  switch (opt) {
184  case 'v':
185  version();
186  exit(0);
187  case 'h':
188  usage();
189  exit(0);
190  case 'm':
191  flags.setIncidenceMatrices(true);
192  break;
193  case 'd':
194  sscanf(optarg, "%s", str_arg);
195  flags.setDebug(str_arg);
196  break;
197  case 'o':
198  flags.setOutputFile(optarg);
199  break;
200  case 'i':
201  flags.addLibraryPath(optarg);
202  break;
203  case 'e':
204  flags.setExternalStructureFile(true);
205  break;
206  case 'f':
207  recompile = true;
208  break;
209  case 's':
210  settings = true;
211  break;
212  case 't':
213  flags.setTesting(true);
214  break;
215  case '?':
216  usage();
217  exit(-1);
218  break;
219  default:
220  abort();
221  }
222  }
223  string pkg = Utils::instance().environmentVariable("MMOC_PACKAGES");
224  if (!pkg.empty() && !flags.testing()) {
225  flags.addLibraryPath(pkg);
226  }
227  AST_StoredDefinition sd;
228  string file_name;
229  if (argv[optind] != nullptr) {
230  file_name = argv[optind];
231  string path = Utils::instance().getFilePath(file_name);
232  flags.setPath(path);
233  sd = parseFile(file_name, &r);
234  Error::instance().setFile(file_name);
235  if (r == 0) {
236  ModelChecker mmo(file_name);
237  r = mmo.apply(sd);
238  if (r == 0) {
239  if (settings) {
240  Settings set(file_name);
241  set.apply(sd);
242  Files sc(file_name, flags);
243  sc.settings(set.annotations());
244  return 0;
245  }
246  Logger::instance().setFile(file_name);
247  int res = parsePackages(sd->imports(), flags, recompile);
248  if (res != 0) {
249  Error::instance().show();
250  cout << "Exit code: " << res << endl;
251  return res;
252  }
253  Utils::instance().setCompileFlags(flags);
254  Error::instance().setFile(file_name);
255  MicroModelicaIR ir(file_name);
256 
257  r = ir.apply(sd);
258  if (r == 0) {
259  Generator gen(ir.definition(), flags);
260  r = gen.generate();
261  }
262  }
263  }
264  }
265  Error::instance().show();
266  cout << "Exit code: " << r << endl;
267  return r;
268 }
MicroModelica::Util::Utils::setPackagePrefix
void setPackagePrefix(std::string package_prefix)
Definition: util.cpp:418
ModelTable< std::string, std::string >
MicroModelica::Util
Definition: equation.hpp:44
MicroModelica::IR::ModelChecker
Definition: mmo_model_checker.hpp:65
MicroModelica::IR::MicroModelicaIR
Definition: mmo_ir.hpp:66
MicroModelica::Util::CompileFlags::setIncidenceMatrices
void setIncidenceMatrices(bool im)
Definition: compile_flags.cpp:92
ModelTable::begin
iterator begin()
Definition: table.hpp:68
mmo_model_checker.hpp
usage
void usage()
Definition: main.cpp:48
MicroModelica::IR::Settings::annotations
ModelAnnotation annotations()
Definition: mmo_settings.cpp:148
MicroModelica::Util::Utils::packageName
std::string packageName(std::string name)
Definition: util.cpp:298
MicroModelica::Generator::Files::settings
void settings(IR::ModelAnnotation annotation)
Definition: files.cpp:264
MicroModelica::Util::Utils::setPackageFunctions
void setPackageFunctions(IR::FunctionTable package_functions)
Definition: util.cpp:414
MicroModelica::Util::Utils::searchCompiledPackage
bool searchCompiledPackage(std::string pname, CompileFlags flags)
Definition: util.cpp:220
MicroModelica::Generator::Generator
Definition: generator.hpp:72
MicroModelica::Util::CompileFlags
Definition: compile_flags.hpp:65
symbol_table.hpp
MicroModelica::Util::Utils::setCompileFlags
void setCompileFlags(CompileFlags flags)
Definition: util.cpp:110
main
int main(int argc, char **argv)
Definition: main.cpp:165
generator.hpp
MicroModelica::Util::CompileFlags::setTesting
void setTesting(bool testing)
Definition: compile_flags.cpp:153
Option
Definition: util_types.hpp:32
MicroModelica::Util::CompileFlags::setExternalStructureFile
void setExternalStructureFile(bool s)
Definition: compile_flags.cpp:141
MicroModelica::IR::MicroModelicaIR::definition
StoredDefinition definition()
Definition: mmo_ir.hpp:113
MicroModelica::Generator::Generator::generate
int generate()
Definition: generator.cpp:66
MicroModelica::Generator::Package
Definition: package.hpp:64
MicroModelica::Util::Utils
Definition: util.hpp:81
MicroModelica::Util::ER_Error
@ ER_Error
ER_Error.
Definition: error.hpp:122
ModelTable::end
iterator end()
Definition: table.hpp:70
MicroModelica::Util::CompileFlags::setDebug
void setDebug(int s)
Definition: compile_flags.cpp:106
files.hpp
MicroModelica::IR::MicroModelicaIR::apply
int apply(AST_Node x)
Definition: mmo_ir.cpp:388
MicroModelica::IR::Settings
Definition: mmo_settings.hpp:69
MicroModelica::IR::Settings::apply
int apply(AST_Node x)
Definition: mmo_settings.cpp:142
util_types.hpp
MicroModelica::Generator
Definition: files.cpp:48
MicroModelica::Util::Utils::generatePath
std::string generatePath(std::string path, std::string file_name)
Definition: util.cpp:255
parsePackages
int parsePackages(AST_StringList imports, CompileFlags &flags, bool recompile)
Definition: main.cpp:98
stored_definition.hpp
MicroModelica::Util::CompileFlags::addObject
void addObject(string o)
Definition: compile_flags.cpp:126
MicroModelica::Util::Utils::readPackage
bool readPackage(std::string fileName, IR::CompiledPackageTable &pt)
MicroModelica::Util::CompileFlags::testing
bool testing()
Definition: compile_flags.cpp:155
ModelTable< std::string, std::string >::iterator
std::map< std::string, std::string >::iterator iterator
Definition: table.hpp:61
EM_CANT_OPEN_FILE
#define EM_CANT_OPEN_FILE
Definition: error.hpp:72
mmo_settings.hpp
compile_flags.hpp
MicroModelica::Util::Utils::addCompiledFunctions
void addCompiledFunctions(IR::CompiledFunctionTable fs)
Definition: util.cpp:353
MicroModelica::IR::ModelChecker::apply
int apply(AST_Node x)
Definition: mmo_model_checker.cpp:492
logger.hpp
ast_types.hpp
MicroModelica::Generator::Files
Definition: files.hpp:68
MicroModelica::Util::CompileFlags::setPath
void setPath(string p)
Definition: compile_flags.cpp:118
MicroModelica::Util::CompileFlags::objects
list< string > objects()
Definition: compile_flags.cpp:128
MicroModelica::IR::FunctionTable
ModelTable< std::string, Function > FunctionTable
Definition: class.hpp:130
MicroModelica::Util::Utils::packagePath
std::string packagePath(std::string pname, CompileFlags flags, std::string ext=".mo")
MicroModelica::IR
Definition: alg_usage.cpp:47
mmo_ir.hpp
MicroModelica::Util::CompileFlags::addLibraryPath
void addLibraryPath(string l)
Definition: compile_flags.cpp:124
class.hpp
current_element
#define current_element(it)
Definition: ast_types.hpp:34
util.hpp
version
void version()
Definition: main.cpp:90
MicroModelica::Util::CompileFlags::setOutputFile
void setOutputFile(string outputFile)
Definition: compile_flags.cpp:108
error.hpp
ModelTable::next
Value next(iterator &it)
Definition: table.hpp:78