Skip to content

Commit

Permalink
Implement new API function saveTotalModelDebug (#8126)
Browse files Browse the repository at this point in the history
- Implement new API function saveTotalModelDebug that instead of
  instantiation uses a simple heuristic based on which identifiers are
  used in a class. It typically produces larger files than the normal
  saveTotalModel, but can be used in cases where saveTotalModel fails.
  • Loading branch information
perost committed Nov 12, 2021
1 parent 1cbaefa commit effe7db
Show file tree
Hide file tree
Showing 7 changed files with 824 additions and 0 deletions.
1 change: 1 addition & 0 deletions OMCompiler/Compiler/.cmake/meta_modelica_source_list.cmake
Expand Up @@ -381,6 +381,7 @@ set(OMC_MM_BACKEND_SOURCES
${CMAKE_CURRENT_SOURCE_DIR}/Script/NFApi.mo
${CMAKE_CURRENT_SOURCE_DIR}/Script/Conversion.mo
${CMAKE_CURRENT_SOURCE_DIR}/Script/Obfuscate.mo
${CMAKE_CURRENT_SOURCE_DIR}/Script/TotalModelDebug.mo

${CMAKE_CURRENT_SOURCE_DIR}/SimCode/HpcOmSimCodeMain.mo
${CMAKE_CURRENT_SOURCE_DIR}/SimCode/SerializeInitXML.mo
Expand Down
13 changes: 13 additions & 0 deletions OMCompiler/Compiler/FrontEnd/AbsynUtil.mo
Expand Up @@ -5542,5 +5542,18 @@ algorithm
end match;
end pathReplaceFirst;

function pathContains
input Absyn.Path path;
input Absyn.Ident name;
output Boolean res;
algorithm
res := match path
case Absyn.Path.IDENT() then path.name == name;
case Absyn.Path.QUALIFIED()
then path.name == name or pathContains(path.path, name);
case Absyn.Path.FULLYQUALIFIED() then pathContains(path.path, name);
end match;
end pathContains;

annotation(__OpenModelica_Interface="frontend");
end AbsynUtil;
12 changes: 12 additions & 0 deletions OMCompiler/Compiler/FrontEnd/ModelicaBuiltin.mo
Expand Up @@ -2173,6 +2173,18 @@ external "builtin";
annotation(preferredView="text");
end saveTotalModel;

function saveTotalModelDebug
"Saves the className model in a single file, together with all other classes
that it depends on. This function uses a naive heuristic based on which
identifiers are used and might save things which are not actually used,
and is meant to be used in cases where the normal saveTotalModel fails."
input String filename;
input TypeName className;
output Boolean success;
external "builtin";
annotation(preferredView="text");
end saveTotalModelDebug;

function save
input TypeName className;
output Boolean success;
Expand Down
12 changes: 12 additions & 0 deletions OMCompiler/Compiler/NFFrontEnd/NFModelicaBuiltin.mo
Expand Up @@ -2397,6 +2397,18 @@ external "builtin";
annotation(preferredView="text");
end saveTotalModel;

function saveTotalModelDebug
"Saves the className model in a single file, together with all other classes
that it depends on. This function uses a naive heuristic based on which
identifiers are used and might save things which are not actually used,
and is meant to be used in cases where the normal saveTotalModel fails."
input String filename;
input TypeName className;
output Boolean success;
external "builtin";
annotation(preferredView="text");
end saveTotalModelDebug;

function save
input TypeName className;
output Boolean success;
Expand Down
36 changes: 36 additions & 0 deletions OMCompiler/Compiler/Script/CevalScriptBackend.mo
Expand Up @@ -129,6 +129,7 @@ import SymbolicJacobian;
import SymbolTable;
import System;
import TaskGraphResults;
import TotalModelDebug;
import Tpl;
import Types;
import Uncertainties;
Expand Down Expand Up @@ -2036,6 +2037,22 @@ algorithm
Values.BOOL(_), Values.BOOL(_), Values.BOOL(_)},_)
then (cache, Values.BOOL(false));

case (cache,_,"saveTotalModelDebug",{Values.STRING(filename),Values.CODE(Absyn.C_TYPENAME(classpath))},_)
equation
Values.ENUM_LITERAL(index=access) = Interactive.checkAccessAnnotationAndEncryption(classpath, SymbolTable.getAbsyn());
if (access >= 9) then // i.e., Access.documentation
saveTotalModelDebug(filename, classpath);
b = true;
else
Error.addMessage(Error.SAVE_ENCRYPTED_CLASS_ERROR, {});
b = false;
end if;
then
(cache, Values.BOOL(b));

case (cache,_,"saveTotalModelDebug",{Values.STRING(_),Values.CODE(Absyn.C_TYPENAME(_))},_)
then (cache, Values.BOOL(false));

case (cache,_,"getDocumentationAnnotation",{Values.CODE(Absyn.C_TYPENAME(classpath))},_)
equation
Values.ENUM_LITERAL(index=access) = Interactive.checkAccessAnnotationAndEncryption(classpath, SymbolTable.getAbsyn());
Expand Down Expand Up @@ -7403,6 +7420,25 @@ algorithm
System.writeFile(filename, str + str1);
end saveTotalModel;

protected function saveTotalModelDebug
input String filename;
input Absyn.Path classPath;
protected
SCode.Program prog;
String str, name_str, cls_str;
algorithm
runFrontEndLoadProgram(classPath);
prog := SymbolTable.getSCode();
prog := TotalModelDebug.getTotalModel(prog, classPath);
prog := SCodeUtil.removeBuiltinsFromTopScope(prog);
prog := SCodeUtil.stripCommentsFromProgram(prog, stripAnnotations = false, stripComments = true);

str := SCodeDump.programStr(prog, SCodeDump.defaultOptions);
name_str := AbsynUtil.pathLastIdent(classPath) + "_total";
cls_str := "\nmodel " + name_str + "\n extends " + AbsynUtil.pathString(classPath) + ";\nend " + name_str + ";\n";
System.writeFile(filename, str + cls_str);
end saveTotalModelDebug;

protected function getDymolaStateAnnotation
"Returns the __Dymola_state annotation of a class.
This is annotated with the annotation:
Expand Down

0 comments on commit effe7db

Please sign in to comment.