Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
- Added support for simulation option fileNamePrefix (in the simulation runtime)
- Added support for quoted identifiers in generated code
  - See for example testsuite/mosfiles-dassl/JapaneseBouncingBall.mos


git-svn-id: https://openmodelica.org/svn/OpenModelica/trunk@7954 f25d12d1-65f4-0310-ae8a-bbce733d8d8e
  • Loading branch information
sjoelund committed Feb 17, 2011
1 parent 8740ef5 commit b6ef61d
Show file tree
Hide file tree
Showing 12 changed files with 81 additions and 19 deletions.
11 changes: 8 additions & 3 deletions Compiler/FrontEnd/Absyn.mo
Expand Up @@ -2845,9 +2845,7 @@ algorithm outPaths := match(path)
case (QUALIFIED(name = n,path = p))
equation
strings = pathToStringList(p);
strings = listAppend(strings,{n});
then
strings;
then n::strings;
end match;
end pathToStringList;

Expand Down Expand Up @@ -5287,4 +5285,11 @@ algorithm
CREF_IDENT(str,{}) := cr;
end crefIdent;

public function unqotePathIdents
input Path inPath;
output Path path;
algorithm
path := stringListPath(Util.listMap(pathToStringList(inPath), System.unquoteIdentifier));
end unqotePathIdents;

end Absyn;
3 changes: 2 additions & 1 deletion Compiler/FrontEnd/Static.mo
Expand Up @@ -6658,6 +6658,7 @@ algorithm
Absyn.ComponentRef cr;
DAE.ComponentRef cr_1;
list<Absyn.NamedArg> args;
list<String> pathIdents;
Boolean impl;
Interactive.InteractiveSymbolTable st;
Prefix.Prefix pre;
Expand All @@ -6684,7 +6685,7 @@ algorithm
equation
(cache,cr_1) = elabUntypedCref(cache,env,cr,impl,pre,info);
className = componentRefToPath(cr_1) "this extracts the fileNamePrefix which is used when generating code and init-file" ;
cname_str = Absyn.pathString(className);
cname_str = Absyn.pathString(Absyn.unqotePathIdents(className)) "easier than checking if the file system supports UTF-8...";

defaulSimOpt = CevalScript.buildSimulationOptionsFromModelExperimentAnnotation(st, className, cname_str);

Expand Down
4 changes: 2 additions & 2 deletions Compiler/Script/Interactive.mo
Expand Up @@ -569,7 +569,7 @@ algorithm
Absyn.CREF(Absyn.CREF_IDENT(name = ident,subscripts = {})),value = Absyn.CREF(cr))),
(st as SYMBOLTABLE(lstVarVal = vars)))
equation
value = getVariableValueLst(listReverse(Absyn.pathToStringList(Absyn.crefToPath(cr))), vars);
value = getVariableValueLst(Absyn.pathToStringList(Absyn.crefToPath(cr)), vars);
str = ValuesUtil.valString(value);
t = Types.typeOfValue(value);
newst = addVarToSymboltable(ident, value, t, st);
Expand Down Expand Up @@ -904,7 +904,7 @@ algorithm
* SimulationResult, etc are not in the environment, but it's nice to be able to script them anyway */
case (Absyn.CREF(cr),(st as SYMBOLTABLE(lstVarVal = vars)),info)
equation
value = getVariableValueLst(listReverse(Absyn.pathToStringList(Absyn.crefToPath(cr))), vars);
value = getVariableValueLst(Absyn.pathToStringList(Absyn.crefToPath(cr)), vars);
then (value,st);

case (exp,(st as SYMBOLTABLE(ast = p)),info)
Expand Down
7 changes: 7 additions & 0 deletions Compiler/Util/System.mo
Expand Up @@ -759,4 +759,11 @@ public function stringHashDjb2Mod
external "builtin";
end stringHashDjb2Mod;

public function unquoteIdentifier
"Quoted identifiers, for example 'xyz' need to be translated into canonical form; for example _omcQuot_0x78797A"
input String str;
output String outStr;
external "C" outStr=System_unquoteIdentifier(str);
end unquoteIdentifier;

end System;
7 changes: 7 additions & 0 deletions Compiler/runtime/System_omc.cpp
Expand Up @@ -516,4 +516,11 @@ extern char* System_unescapedString(char* str)
return res;
}

extern char* System_unquoteIdentifier(char *str)
{
char *res = SystemImpl__unquoteIdentifier(str);
if (res == NULL) return str;
return res;
}

}
13 changes: 13 additions & 0 deletions Compiler/runtime/System_rml.c
Expand Up @@ -1983,3 +1983,16 @@ RML_BEGIN_LABEL(System__escapedString)
}
}
RML_END_LABEL

RML_BEGIN_LABEL(System__unquoteIdentifier)
{
char *str = SystemImpl__unquoteIdentifier(RML_STRINGDATA(rmlA0));
if (str == NULL) {
RML_TAILCALLK(rmlSC);
} else {
rmlA0 = mk_scon(str);
free(str);
RML_TAILCALLK(rmlSC);
}
}
RML_END_LABEL
21 changes: 21 additions & 0 deletions Compiler/runtime/systemimpl.c
Expand Up @@ -1102,6 +1102,27 @@ extern void* SystemImpl__regex(const char* str, const char* re, int maxn, int ex
return lst;
}

char* SystemImpl__unquoteIdentifier(const char* str)
{
const char lookupTbl[] = {'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
char *res,*cur;
int len,i;
const int offset = 10;
const char _omcQuot[]="_omcQuot_";
if (*str != '\'') return NULL;
len = strlen(str)-2;
res = (char*) malloc(2*len+offset+64);
cur = res;
cur += sprintf(cur,"%s",_omcQuot);
for (i=0; i<len; i++) {
unsigned char c = str[i+1];
cur += sprintf(cur,"%c",lookupTbl[c/16]);
cur += sprintf(cur,"%c",lookupTbl[c%16]);
}
*cur = '\0';
return res;
}

#ifdef __cplusplus
}
#endif
Expand Down
22 changes: 12 additions & 10 deletions Compiler/susan_codegen/SimCode/SimCodeC.tpl
Expand Up @@ -91,7 +91,7 @@ case SIMCODE(__) then
<<
<%simulationFileHeader(simCode)%>

<%globalData(modelInfo)%>
<%globalData(modelInfo,fileNamePrefix)%>

<%equationInfo(appendLists(appendAllequation(JacobianMatrixes),allEquationsPlusWhen))%>

Expand Down Expand Up @@ -188,7 +188,7 @@ case SIMCODE(modelInfo=MODELINFO(__), extObjInfo=EXTOBJINFO(__)) then
end simulationFileHeader;


template globalData(ModelInfo modelInfo)
template globalData(ModelInfo modelInfo, String fileNamePrefix)
"Generates global data in simulation file."
::=
match modelInfo
Expand Down Expand Up @@ -221,6 +221,7 @@ case MODELINFO(varInfo=VARINFO(__), vars=SIMVARS(__)) then

extern "C" { // adrpo: this is needed for Visual C++ compilation to work!
const char *model_name="<%dotPath(name)%>";
const char *model_fileprefix="<%fileNamePrefix%>";
const char *model_dir="<%directory%>";
}

Expand Down Expand Up @@ -717,6 +718,7 @@ template functionInitializeDataStruc()

returnData->initFixed = init_fixed;
returnData->modelName = model_name;
returnData->modelFilePrefix = model_fileprefix;
returnData->statesNames = state_names;
returnData->stateDerivativesNames = derivative_names;
returnData->algebraicsNames = algvars_names;
Expand Down Expand Up @@ -2710,8 +2712,8 @@ template crefToCStr(ComponentRef cr)
"Helper function to cref."
::=
match cr
case CREF_IDENT(__) then '<%ident%><%subscriptsToCStr(subscriptLst)%>'
case CREF_QUAL(__) then '<%ident%><%subscriptsToCStr(subscriptLst)%>$P<%crefToCStr(componentRef)%>'
case CREF_IDENT(__) then '<%unquoteIdentifier(ident)%><%subscriptsToCStr(subscriptLst)%>'
case CREF_QUAL(__) then '<%unquoteIdentifier(ident)%><%subscriptsToCStr(subscriptLst)%>$P<%crefToCStr(componentRef)%>'
case WILD(__) then ''
else "CREF_NOT_IDENT_OR_QUAL"
end crefToCStr;
Expand Down Expand Up @@ -2755,8 +2757,8 @@ template crefToMStr(ComponentRef cr)
"Helper function to crefM."
::=
match cr
case CREF_IDENT(__) then '<%ident%><%subscriptsToMStr(subscriptLst)%>'
case CREF_QUAL(__) then '<%ident%><%subscriptsToMStr(subscriptLst)%>P<%crefToMStr(componentRef)%>'
case CREF_IDENT(__) then '<%unquoteIdentifier(ident)%><%subscriptsToMStr(subscriptLst)%>'
case CREF_QUAL(__) then '<%unquoteIdentifier(ident)%><%subscriptsToMStr(subscriptLst)%>P<%crefToMStr(componentRef)%>'
else "CREF_NOT_IDENT_OR_QUAL"
end crefToMStr;

Expand Down Expand Up @@ -2797,8 +2799,8 @@ end arrayCrefCStr;
template arrayCrefCStr2(ComponentRef cr)
::=
match cr
case CREF_IDENT(__) then '<%ident%>'
case CREF_QUAL(__) then '<%ident%>$P<%arrayCrefCStr2(componentRef)%>'
case CREF_IDENT(__) then '<%unquoteIdentifier(ident)%>'
case CREF_QUAL(__) then '<%unquoteIdentifier(ident)%>$P<%arrayCrefCStr2(componentRef)%>'
else "CREF_NOT_IDENT_OR_QUAL"
end arrayCrefCStr2;

Expand Down Expand Up @@ -2842,9 +2844,9 @@ template crefFunctionName(ComponentRef cr)
::=
match cr
case CREF_IDENT(__) then
System.stringReplace(ident, "_", "__")
System.stringReplace(unquoteIdentifier(ident), "_", "__")
case CREF_QUAL(__) then
'<%System.stringReplace(ident, "_", "__")%>_<%crefFunctionName(componentRef)%>'
'<%System.stringReplace(unquoteIdentifier(ident), "_", "__")%>_<%crefFunctionName(componentRef)%>'
end crefFunctionName;

template dotPath(Path path)
Expand Down
5 changes: 5 additions & 0 deletions Compiler/susan_codegen/SimCode/SimCodeTV.mo
Expand Up @@ -562,6 +562,11 @@ package System
input String unescapedString;
output String escapedString;
end escapedString;

function unquoteIdentifier
input String str;
output String outStr;
end unquoteIdentifier;
end System;


Expand Down
2 changes: 1 addition & 1 deletion c_runtime/simulation_input.cpp
Expand Up @@ -61,7 +61,7 @@ void read_commented_value(ifstream &f, signed char *str);

string *filename=(string*)getFlagValue("f",argc,argv);
if (filename == NULL) {
filename = new string(string(simData->modelName)+"_init.txt"); // model_name defined in generated code for model.
filename = new string(string(simData->modelFilePrefix)+"_init.txt"); // model_name defined in generated code for model.
}

ifstream file(filename->c_str());
Expand Down
2 changes: 1 addition & 1 deletion c_runtime/simulation_runtime.cpp
Expand Up @@ -388,7 +388,7 @@ callSolver(int argc, char**argv, string method, string outputFormat,
string *result_file = (string*) getFlagValue("r", argc, argv);
string result_file_cstr;
if (!result_file) {
result_file_cstr = string(globalData->modelName) + string("_res.") + outputFormat; /* TODO: Fix result file name based on mode */
result_file_cstr = string(globalData->modelFilePrefix) + string("_res.") + outputFormat; /* TODO: Fix result file name based on mode */
} else {
result_file_cstr = *result_file;
}
Expand Down
3 changes: 2 additions & 1 deletion c_runtime/simulation_runtime.h
Expand Up @@ -187,7 +187,8 @@ typedef struct sim_DATA {
DATA_INT intVariables;
DATA_BOOL boolVariables;

const char* modelName;
const char* modelName; // For error messages
const char* modelFilePrefix; // For filenames, input/output
const struct omc_varInfo* statesNames;
const struct omc_varInfo* stateDerivativesNames;
const struct omc_varInfo* algebraicsNames;
Expand Down

0 comments on commit b6ef61d

Please sign in to comment.