44 : _language_especification(
"C"), _flags(), _compiled_functions(), _file_name(), _ids(0), _package_functions(), _package_prefix()
46 _annotations.insert(pair<string, int>(
"StartTime", 0));
47 _annotations.insert(pair<string, int>(
"StopTime", 1));
48 _annotations.insert(pair<string, int>(
"Tolerance", 2));
49 _annotations.insert(pair<string, int>(
"AbsTolerance", 3));
50 _annotations.insert(pair<string, int>(
"StepSize", 4));
69 addCompiledFunctions(BuiltInFunction::instance().functions());
74 string Utils::trimString(
string str)
78 size_t endpos = ret.find_last_not_of(
" \t");
79 if (string::npos != endpos) {
80 ret = ret.substr(0, endpos + 1);
84 size_t startpos = ret.find_first_not_of(
" \t");
85 if (string::npos != startpos) {
86 ret = ret.substr(startpos);
91 string Utils::languageEspecification() {
return _language_especification; }
93 void Utils::setCompileFlags(
CompileFlags flags) { _flags = flags; }
95 bool Utils::checkTypeString(
string t)
97 if (t !=
"Integer" && t !=
"Real" && t !=
"Boolean") {
103 bool Utils::checkExperimentAnnotations(
string *annotation)
105 map<string, int>::iterator it = _annotations.find(*annotation);
106 return it != _annotations.end();
109 string Utils::opString(
BinOpType bot) {
return _binop[bot]; }
111 string Utils::getVarName(
string name)
114 int ub = name.length() - 1;
115 for (
int i = 0; i < ub; i++) {
121 string Utils::iteratorVar(
int dim)
124 buffer <<
"_d" << dim + 1;
132 while (getline(*package, line)) {
133 if (!line.compare(token)) {
145 string compiled_package_name = file_name;
148 assert(!compiled_package_name.empty());
149 compiled_package_name = package_name;
151 string pname = packageName(file_name);
152 name = packagePath(pname +
".c");
153 name.append(pname).append(
".moo");
155 package.open(name.c_str());
156 if (package.good()) {
162 string prefix = cp.prefix();
167 while (getline(package, line)) {
168 if (!line.compare(
"DEPENDENCES")) {
169 objects = getValue(&package,
"ENDDEPENDENCES");
170 }
else if (!line.compare(
"FUNCTION")) {
171 fname = getValue(&package,
"ENDFUNCTION").first();
172 }
else if (!line.compare(
"DERIVATIVE")) {
173 derivative = getValue(&package,
"ENDDERIVATIVE").first();
174 }
else if (!line.compare(
"INCLUDEDIRECTORY")) {
175 includeDir = getValue(&package,
"ENDINCLUDEDIRECTORY").first();
176 }
else if (!line.compare(
"LIBRARYDIRECTORY")) {
177 libraryDir = getValue(&package,
"ENDLIBRARYDIRECTORY").first();
178 }
else if (!line.compare(
"LIBRARIES")) {
179 libraries = getValue(&package,
"ENDLIBRARIES");
180 }
else if (!line.compare(
"ENDDEFINITION")) {
181 CompiledFunction fi(fname, includeDir, libraryDir, libraries, prefix);
186 cp.setObjects(objects);
187 cp.setDefinitions(cft);
199 pt.insert(cp.get().name(), cp.get());
203 bool Utils::searchCompiledPackage(
string pname, CompileFlags flags) {
return !packagePath(packageName(pname), flags,
".moo").empty(); }
205 bool Utils::checkCodeFiles(
string name,
string ext)
207 if (!ext.compare(
".mo")) {
210 ifstream h((name +
".h").c_str());
211 ifstream c((name +
".c").c_str());
212 if (h.good() && c.good()) {
220 string Utils::packagePath(
string name)
222 list<string> objs = _flags.objects();
223 for (list<string>::iterator it = objs.begin(); it != objs.end(); it++) {
226 size_t found = i.rfind(
"/");
227 if (found != std::string::npos) {
228 p.erase(p.begin() + found + 1, p.end());
229 i.erase(i.begin(), i.begin() + found + 1);
231 if (!i.compare(name)) {
238 string Utils::generatePath(std::string path,
string file_name)
240 if (path.back() ==
'/') {
241 return path + file_name;
243 return path +
SLASH + file_name;
246 string Utils::packagePath(
string pname, CompileFlags flags,
string ext)
248 string fname = flags.path();
249 fname.append(pname + ext);
250 ifstream f(fname.c_str());
251 if (f.good() && checkCodeFiles(flags.path() + pname, ext)) {
256 if (flags.hasOutputFile()) {
257 fname = flags.outputFilePath();
258 fname.append(pname + ext);
259 f.open(fname.c_str());
260 if (f.good() && checkCodeFiles(flags.outputFilePath() +
SLASH + pname, ext)) {
262 return flags.outputFilePath();
266 list<string> paths = flags.libraryPaths();
267 for (list<string>::iterator it = paths.begin(); it != paths.end(); it++) {
269 fname.append(
SLASH + pname + ext);
270 f.open(fname.c_str());
271 if (f.good() && checkCodeFiles(*it +
SLASH + pname, ext)) {
281 string Utils::packageName(
string name)
283 size_t f = name.find(
"pkg_");
284 if (f == string::npos) {
285 return "pkg_" + name;
290 string Utils::environmentVariable(
string ev)
292 char *li = getenv(ev.c_str());
299 string Utils::getFilePath(
string file)
303 size_t found = path.rfind(
"/");
304 if (found != std::string::npos) {
305 path.erase(path.begin() + found + 1, path.end());
310 string Utils::getFileName(
string file)
313 size_t f = fn.rfind(
"/");
314 if (f == std::string::npos) {
317 fn.erase(fn.begin(), fn.begin() + f + 1);
321 string Utils::baseFileName(
string file)
323 string base_file = getFileName(file);
324 size_t f = base_file.rfind(
".");
325 if (f == std::string::npos) {
328 base_file.erase(base_file.begin() + f, base_file.end());
334 void Utils::addCompiledFunction(CompiledFunction f) { _compiled_functions.
insert(f.name(), f); }
338 bool Utils::checkCompiledFunctions(
string name)
341 return cf.is_initialized();
344 bool Utils::checkBuiltInFunctions(
string name) {
return BuiltInFunction::instance().lookup(name); }
346 Variable Utils::variable(AST_Expression exp)
351 AST_Expression_ComponentReference var_exp = exp->getAsComponentReference();
354 AST_Expression_Derivative der = exp->getAsDerivative();
357 AST_Expression_ComponentReference var_exp = args->getAsComponentReference();
374 for (
auto r : ranges) {
375 string idx_var = range->iterator(dim++);
381 return Expression(var);
384 string Utils::tabs(
int t)
387 for (
int i = 0; i < t; i++) {
397 void Utils::setPackageFunctions(
IR::FunctionTable package_functions) { _package_functions = package_functions; }
399 string Utils::packagePrefix() {
return _package_prefix; }
401 void Utils::setPackagePrefix(
string package_prefix) { _package_prefix = package_prefix; }