Skip to content

Commit

Permalink
Change the Modelica string to be implemented as a MetaModelica string…
Browse files Browse the repository at this point in the history
… (boxed)

- Note: Some Modelica functions might have become slower if string literals were not put into the _literals file


git-svn-id: https://openmodelica.org/svn/OpenModelica/trunk@23321 f25d12d1-65f4-0310-ae8a-bbce733d8d8e
  • Loading branch information
sjoelund committed Nov 11, 2014
1 parent fae56d9 commit 29141f0
Show file tree
Hide file tree
Showing 34 changed files with 361 additions and 419 deletions.
3 changes: 1 addition & 2 deletions Compiler/FrontEnd/ClassLoader.mo
Expand Up @@ -396,8 +396,7 @@ algorithm

case (Absyn.CLASS(body=Absyn.PARTS(classParts=cp),info=info),_,_,_)
equation
if (System.regularFileExists(filename))
then
if (System.regularFileExists(filename)) then
contents = System.readFile(filename);
namesToFind = System.strtok(contents, "\n");
namesToFind = List.removeOnTrue("",stringEqual,List.map(namesToFind,System.trimWhitespace));
Expand Down
8 changes: 4 additions & 4 deletions Compiler/Script/CevalScript.mo
Expand Up @@ -2682,17 +2682,17 @@ algorithm
usercflags = Util.makeValueOrDefault(System.readEnv,"MODELICAUSERCFLAGS","");
workdir = System.pwd();
touch_res = 0 == System.systemCall("touch " + touch_file, "");
_ = 0 == System.systemCall("uname -a", touch_file);
System.systemCall("uname -a", touch_file);
uname = System.readFile(touch_file);
rm_res = 0 == System.systemCall("rm " + touch_file, "");
_ = System.platform();
// _ = System.platform();
senddata = System.getRTLibs();
gcc = System.getCCompiler();
have_corba = Corba.haveCorba();
_ = System.systemCall("rm -f " + touch_file, "");
System.systemCall("rm -f " + touch_file, "");
gcc_res = 0 == System.systemCall(gcc + " --version", touch_file);
gccVersion = System.readFile(touch_file);
_ = System.systemCall("rm -f " + touch_file, "");
System.systemCall("rm -f " + touch_file, "");
confcmd = System.configureCommandLine();
vals = {Values.STRING(omhome),
Values.STRING(omlib),
Expand Down
64 changes: 23 additions & 41 deletions Compiler/Template/CodegenC.tpl
Expand Up @@ -2218,7 +2218,7 @@ template functionWhenReinitStatementThen(Boolean initialCall, list<WhenOperator>
<<
<%preExp%>
FILE_INFO info = {<%infoArgs(getElementSourceFileInfo(source))%>};
omc_terminate(info, <%msgVar%>);
omc_terminate(info, MMC_STRINGDATA(<%msgVar%>));
>>
case ASSERT(source=SOURCE(info=info)) then
assertCommon(condition, List.fill(message,1), level, contextSimulationDiscrete, &varDecls, &auxFunction, info)
Expand Down Expand Up @@ -3077,6 +3077,7 @@ template functionDAE(list<SimEqSystem> allEquationsPlusWhen, list<SimWhenClause>
int <%symbolName(modelNamePrefix,"functionDAE")%>(DATA *data)
{
int equationIndexes[1] = {0};<%/*reinits may use equation indexes, even though it has no equation...*/%>
<%addRootsTempArray()%>
<%varDecls%>

TRACE_PUSH
Expand Down Expand Up @@ -4588,9 +4589,7 @@ end simulationInitFile;
template commonHeader(String filePrefix)
::=
<<
<% if acceptMetaModelicaGrammar() then "#define __OPENMODELICA__METAMODELICA"%>
<% if acceptMetaModelicaGrammar() then "#include \"meta/meta_modelica.h\"" %>

#include "meta/meta_modelica.h"
#include "util/modelica.h"
#include <stdio.h>
#include <stdlib.h>
Expand Down Expand Up @@ -5624,7 +5623,7 @@ template generateInFunc(Text fname, list<Variable> functionArguments, list<Varia
DLLExport
int in_<%fname%>(type_description * inArgs, type_description * outVar)
{
<% if acceptMetaModelicaGrammar() then "if (!mmc_GC_state) mmc_GC_init();" %>
if (!mmc_GC_state) mmc_GC_init();
<%functionArguments |> var => '<%funArgDefinition(var)%>;' ;separator="\n"%>
<%outVars |> var => '<%funArgDefinition(var)%>;' ;separator="\n"%>
<%functionArguments |> arg => readInVar(arg) ;separator="\n"%>
Expand Down Expand Up @@ -6812,16 +6811,16 @@ case SIMEXTARG(outputIndex=oi, isArray=true, cref=c, type_=ty) then
match expTypeShort(ty)
case "integer" then
'unpack_integer_array(&<%contextCref(c,contextFunction,&auxFunction)%>);'
case "string" then
'unpack_string_array(&<%contextCref(c,contextFunction,&auxFunction)%>);'
else ""
case SIMEXTARG(outputIndex=oi, isArray=false, type_=ty, cref=c) then
let cr = '<%extVarName(c)%>'
<<
<%contextCref(c,contextFunction,&auxFunction)%> = (<%expTypeModelica(ty)%>)<%
if acceptMetaModelicaGrammar() then
(match ty
match ty
case T_STRING(__) then 'mmc_mk_scon(<%cr%>)'
else cr)
else cr %>;
else cr%>;
>>
end extFunCallVarcopy;

Expand Down Expand Up @@ -6850,13 +6849,10 @@ template extArg(SimExtArg extArg, Text &preExp, Text &varDecls, Text &auxFunctio
case SIMEXTARG(cref=c, outputIndex=oi, isArray=true, type_=t) then
let name = contextCref(c,contextFunction,&auxFunction)
let shortTypeStr = expTypeShort(t)
'(<%extType(t,isInput,true)%>) data_of_<%shortTypeStr%>_array(&(<%name%>))'
'(<%extType(t,isInput,true)%>) data_of_<%shortTypeStr%>_c89_array(&(<%name%>))'
case SIMEXTARG(cref=c, isInput=ii, outputIndex=0, type_=t) then
let cr = match t case T_STRING(__) then contextCref(c,contextFunction,&auxFunction) else extVarName(c)
if acceptMetaModelicaGrammar() then
(match t case T_STRING(__) then 'MMC_STRINGDATA(<%cr%>)' else cr)
else
cr
(match t case T_STRING(__) then 'MMC_STRINGDATA(<%cr%>)' else cr)
case SIMEXTARG(cref=c, isInput=ii, outputIndex=oi, type_=t) then
'&<%extVarName(c)%>'
case SIMEXTARGEXP(__) then
Expand All @@ -6880,7 +6876,7 @@ template extArgF77(SimExtArg extArg, Text &preExp, Text &varDecls, Text &auxFunc
'(int*) &<%contextCref(c,contextFunction,&auxFunction)%><%suffix%>'
case SIMEXTARG(cref=c, outputIndex=oi, type_ = T_STRING(__)) then
// modelica_string SHOULD NOT BE PREFIXED by &!
'(char*)<%contextCref(c,contextFunction,&auxFunction)%>'
'(char*)MMC_STRINGDATA(<%contextCref(c,contextFunction,&auxFunction)%>)'
case SIMEXTARG(cref=c, outputIndex=oi, type_=t) then
// Always prefix fortran arguments with &.
let suffix = if oi then "_ext"
Expand All @@ -6890,7 +6886,7 @@ template extArgF77(SimExtArg extArg, Text &preExp, Text &varDecls, Text &auxFunc
let texp = daeExp(exp, contextFunction, &preExp, &varDecls, &auxFunction)
let tvar = tempDecl(expTypeFromExpFlag(exp,8),&varDecls)
let &preExp += '<%tvar%> = <%texp%>;<%\n%>'
'(char*)<%tvar%>'
'(char*)MMC_STRINGDATA(<%tvar%>)'
case SIMEXTARGEXP(__) then
daeExternalF77Exp(exp, contextFunction, &preExp, &varDecls, &auxFunction)
case SIMEXTARGSIZE(cref=c) then
Expand Down Expand Up @@ -7580,7 +7576,9 @@ template algStmtForGeneric(DAE.Statement stmt, Context context, Text &varDecls,
::=
match stmt
case STMT_FOR(__) then
let iterType = expType(type_, iterIsArray)
let iterType = match expType(type_, iterIsArray)
case "modelica_string" then "modelica_metatype"
case s then s
let arrayType = expTypeArray(type_)
let tvar = match iterType
case "modelica_metatype"
Expand Down Expand Up @@ -7678,7 +7676,7 @@ case STMT_TERMINATE(__) then
<<
<%preExp%>
FILE_INFO info = {<%infoArgs(getElementSourceFileInfo(source))%>};
omc_terminate(info, <%msgVar%>);
omc_terminate(info, MMC_STRINGDATA(<%msgVar%>));
>>
end algStmtTerminate;

Expand Down Expand Up @@ -7949,7 +7947,7 @@ end daeExternalF77Exp;
template daeExpSconst(String string)
"Generates code for a string constant."
::=
'"<%Util.escapeModelicaStringToCString(string)%>"'
'mmc_mk_scon("<%Util.escapeModelicaStringToCString(string)%>")'
end daeExpSconst;


Expand Down Expand Up @@ -8348,13 +8346,8 @@ case BINARY(__) then
let e2 = daeExp(exp2, context, &preExp, &varDecls, &auxFunction)
match operator
case ADD(ty = T_STRING(__)) then
let tmpStr = if acceptMetaModelicaGrammar()
then tempDecl("modelica_metatype", &varDecls)
else tempDecl("modelica_string", &varDecls)
let &preExp += if acceptMetaModelicaGrammar() then
'<%tmpStr%> = stringAppend(<%e1%>,<%e2%>);<%\n%>'
else
'<%tmpStr%> = cat_modelica_string(<%e1%>,<%e2%>);<%\n%>'
let tmpStr = tempDecl("modelica_metatype", &varDecls)
let &preExp += '<%tmpStr%> = stringAppend(<%e1%>,<%e2%>);<%\n%>'
tmpStr
case ADD(__) then '(<%e1%> + <%e2%>)'
case SUB(__) then '(<%e1%> - <%e2%>)'
Expand Down Expand Up @@ -8901,7 +8894,7 @@ template daeExpCall(Exp call, Context context, Text &preExp, Text &varDecls, Tex

case CALL(path=IDENT(name="print"), expLst={e1}) then
let var1 = daeExp(e1, context, &preExp, &varDecls, &auxFunction)
if acceptMetaModelicaGrammar() then 'print(<%var1%>)' else 'fputs(<%var1%>,stdout)'
'fputs(MMC_STRINGDATA(<%var1%>),stdout)'

case CALL(path=IDENT(name="max"), attr=CALL_ATTR(ty = T_REAL(__)), expLst={e1,e2}) then
let var1 = daeExp(e1, context, &preExp, &varDecls, &auxFunction)
Expand Down Expand Up @@ -10321,7 +10314,7 @@ template expTypeShort(DAE.Type type)
match type
case T_INTEGER(__) then "integer"
case T_REAL(__) then "real"
case T_STRING(__) then if acceptMetaModelicaGrammar() then "metatype" else "string"
case T_STRING(__) then "string"
case T_BOOL(__) then "boolean"
case T_ENUMERATION(__) then "integer"
case T_SUBTYPE_BASIC(__) then expTypeShort(complexType)
Expand Down Expand Up @@ -10475,10 +10468,7 @@ template expTypeFromExpFlag(Exp exp, Integer flag)
match exp
case ICONST(__) then match flag case 8 then "int" case 1 then "integer" else "modelica_integer"
case RCONST(__) then match flag case 1 then "real" else "modelica_real"
case SCONST(__) then if acceptMetaModelicaGrammar() then
(match flag case 1 then "metatype" else "modelica_metatype")
else
(match flag case 1 then "string" case 2 then "modelica_string_t" else "modelica_string")
case SCONST(__) then match flag case 1 then "string" else "modelica_string"
case BCONST(__) then match flag case 1 then "boolean" else "modelica_boolean"
case ENUM_LITERAL(__) then match flag case 8 then "int" case 1 then "integer" else "modelica_integer"
case e as BINARY(__)
Expand Down Expand Up @@ -10776,9 +10766,7 @@ end assertCommon;

template expToFormatString(Exp exp, Context context, Text &preExp, Text &varDecls, Text &auxFunction)
::=
let pre = (match typeof(exp) case T_STRING(__) then (if acceptMetaModelicaGrammar() then "MMC_STRINGDATA("))
let post = (if pre then ")")
pre + daeExp(exp, context, &preExp, &varDecls, &auxFunction) + post
'MMC_STRINGDATA(<%daeExp(exp, context, &preExp, &varDecls, &auxFunction)%>)'
end expToFormatString;

template assertCommonVar(Text condVar, Text msgVar, Context context, Text &preExpMsg, Text &varDecls, builtin.SourceInfo info)
Expand Down Expand Up @@ -10812,7 +10800,6 @@ template literalExpConst(Exp lit, Integer litindex, Text &preLit) "These should
match lit
case SCONST(__) then
let escstr = Util.escapeModelicaStringToCString(string)
if acceptMetaModelicaGrammar() then
/* TODO: Change this when OMC takes constant input arguments (so we cannot write to them)
The cost of not doing this properly is small (<257 bytes of constants)
match unescapedStringLength(escstr)
Expand All @@ -10824,11 +10811,6 @@ template literalExpConst(Exp lit, Integer litindex, Text &preLit) "These should
static const MMC_DEFSTRINGLIT(<%tmp%>,<%unescapedStringLength(escstr)%>,<%name%>_data);
#define <%name%> MMC_REFSTRINGLIT(<%tmp%>)
>>
else
<<
#define <%name%>_data "<%escstr%>"
static const char <%name%>[<%intAdd(1,unescapedStringLength(escstr))%>] = <%name%>_data;
>>
case lit as MATRIX(ty=ty as T_ARRAY(__))
case lit as ARRAY(ty=ty as T_ARRAY(__)) then
let ndim = listLength(getDimensionSizes(ty))
Expand Down
13 changes: 7 additions & 6 deletions Compiler/runtime/Dynload.cpp
Expand Up @@ -79,7 +79,7 @@ static int value_to_type_desc(void *value, type_description *desc)
void *data = MMC_STRUCTDATA(value)[UNBOX_OFFSET];
int len = MMC_HDRSTRLEN(MMC_GETHDR(data));
desc->type = TYPE_DESC_STRING;
desc->data.string = GC_strdup(MMC_STRINGDATA(data));
desc->data.string = data;
}; break;
case Values__ARRAY_3dBOX2: {
void *data = MMC_STRUCTDATA(value)[UNBOX_OFFSET];
Expand Down Expand Up @@ -272,7 +272,7 @@ static void *generate_array(enum type_desc_e type, int curdim, int ndims,
*data = ptr;
}; break;
case TYPE_DESC_STRING: {
modelica_string_const *ptr = *((modelica_string_const**)data);
modelica_string *ptr = *((modelica_string**)data);
for (i = 0; i < cur_dim_size; ++i, --ptr) {
tmp.data.string = *ptr;
lst = (void *) mmc_mk_cons(type_desc_to_value(&tmp), lst);
Expand Down Expand Up @@ -562,7 +562,8 @@ void *type_desc_to_value(type_description *desc)
return (void *) Values__BOOL(MMC_TRUE);
return (void *) Values__BOOL(MMC_FALSE);
case TYPE_DESC_STRING:
return (void *) Values__STRING(mmc_mk_scon(desc->data.string));
/* Duplicate since we unload the object */
return (void *) Values__STRING(mmc_mk_scon(MMC_STRINGDATA(desc->data.string)));
case TYPE_DESC_TUPLE: {
type_description *e = desc->data.tuple.element + desc->data.tuple.elements;
void *lst = (void *) mmc_mk_nil();
Expand Down Expand Up @@ -612,7 +613,7 @@ void *type_desc_to_value(type_description *desc)
desc->data.bool_array.dim_size, &ptr);
};
case TYPE_DESC_STRING_ARRAY: {
void *ptr = (modelica_string_t *) desc->data.string_array.data
void *ptr = (modelica_string *) desc->data.string_array.data
+ base_array_nr_of_elements(desc->data.string_array) - 1;
return generate_array(TYPE_DESC_STRING, 1, desc->data.string_array.ndims,
desc->data.string_array.dim_size, &ptr);
Expand Down Expand Up @@ -715,14 +716,14 @@ static int get_array_data(int curdim, int dims, const _index_t *dim_size,
*data = ++ptr;
}; break;
case TYPE_DESC_STRING: {
modelica_string_const *ptr = *((modelica_string_const**)data);
modelica_string *ptr = *((modelica_string**)data);
int len;
void *str;
if (MMC_HDRCTOR(MMC_GETHDR(item)) != Values__STRING_3dBOX1)
return -1;
str = MMC_STRUCTDATA(item)[UNBOX_OFFSET+0];
len = MMC_HDRSTRLEN(MMC_GETHDR(str));
*ptr = init_modelica_string(MMC_STRINGDATA(str));
*ptr = str;
*data = ++ptr;
}; break;
default:
Expand Down
9 changes: 4 additions & 5 deletions Compiler/runtime/IOStreamExt_omc.cpp
Expand Up @@ -128,8 +128,7 @@ extern void IOStreamExt_printBuffer(int id, int whereToPrint)
extern const char* IOStreamExt_appendReversedList(modelica_metatype lst)
{
int lstLen, i, acc, len;
modelica_string_const res_head;
modelica_string_t res, tmp;
char *res, *tmp, *res_head;
modelica_metatype car, lstHead;
lstLen = listLength(lst);
acc = 0;
Expand All @@ -140,7 +139,7 @@ extern const char* IOStreamExt_appendReversedList(modelica_metatype lst)
tmp = MMC_STRINGDATA(MMC_CAR(lst));
acc += strlen(tmp);
}
res = (char*) malloc(acc+1);
res = (char*) GC_malloc(acc+1);
res_head = res;
res += acc;
res[0] = '\0';
Expand All @@ -167,7 +166,7 @@ extern void IOStreamExt_printReversedList(modelica_metatype lst, int whereToPrin
case 2: f = stderr; break;
default: MMC_THROW();
}
strs = (const char**) malloc(sizeof(const char*)*lstLen);
strs = (const char**) GC_malloc(sizeof(const char*)*lstLen);

for (i=0; i<lstLen ; i++, lst = MMC_CDR(lst)) {
strs[i] = MMC_STRINGDATA(MMC_CAR(lst));
Expand All @@ -176,7 +175,7 @@ extern void IOStreamExt_printReversedList(modelica_metatype lst, int whereToPrin
fprintf(f, "%s", strs[lstLen-1-i]);
}
fflush(f);
free(strs);
GC_free(strs);
}

}
5 changes: 3 additions & 2 deletions Compiler/runtime/System_omc.c
Expand Up @@ -700,9 +700,10 @@ extern const char* System_snprintff(const char *fmt, int len, double d)
extern const char* System_realpath(const char *path)
{
char buf[PATH_MAX];
if (realpath(path, buf) == NULL)
if (realpath(path, buf) == NULL) {
MMC_THROW();
return init_modelica_string(buf);
}
return GC_strdup(buf);
}

extern int System_fileIsNewerThan(const char *file1, const char *file2)
Expand Down
20 changes: 19 additions & 1 deletion SimulationRuntime/c/meta/meta_modelica.h
Expand Up @@ -50,6 +50,7 @@ extern "C" {
#include "gc/mmc_gc.h"
#include "meta_modelica_string_lit.h"
#include "omc_inline.h"
#include "openmodelica.h"

/*
*
Expand Down Expand Up @@ -320,6 +321,23 @@ static inline void mmc_prim_set_real(struct mmc_real *p, double d)
*(data + 1) = u.data[1];
}

static inline void* mmc_alloc_scon(size_t nbytes)
{
unsigned int header = MMC_STRINGHDR(nbytes);
unsigned int nwords = MMC_HDRSLOTS(header) + 1;
struct mmc_string *p;
void *res;
if (nbytes == 0) return mmc_emptystring;
p = (struct mmc_string *) omc_alloc_interface.malloc_string(nwords*sizeof(void*));
p->header = header;
p->data[0] = 0;
res = MMC_TAGPTR(p);
#ifdef MMC_MK_DEBUG
fprintf(stderr, "STRING slots: %u size: %d str: %s\n", MMC_HDRSLOTS(header), nbytes, s); fflush(NULL);
#endif
return res;
}

static inline void* mmc_mk_scon(const char *s)
{
unsigned int nbytes = strlen(s);
Expand All @@ -338,7 +356,7 @@ static inline void* mmc_mk_scon(const char *s)
res = MMC_TAGPTR(p);
MMC_CHECK_STRING(res);
#ifdef MMC_MK_DEBUG
fprintf(stderr, "STRING slots: %u size: %d str: %s\n", MMC_HDRSLOTS(header), strlen(s), s); fflush(NULL);
fprintf(stderr, "STRING slots: %u size: %d str: %s\n", MMC_HDRSLOTS(header), nbytes, s); fflush(NULL);
#endif
return res;
}
Expand Down
4 changes: 1 addition & 3 deletions SimulationRuntime/c/meta/meta_modelica_builtin.c
Expand Up @@ -28,8 +28,6 @@
*
*/

#define __OPENMODELICA__METAMODELICA

#include "meta_modelica_builtin.h"
#include "meta_modelica.h"
#include <float.h>
Expand Down Expand Up @@ -649,7 +647,7 @@ modelica_integer tick(void)

void boxptr_print(threadData_t *threadData,modelica_metatype str)
{
fprintf(stdout, "%s", MMC_STRINGDATA(str));
fputs(MMC_STRINGDATA(str), stdout);
}

modelica_real mmc_clock(void)
Expand Down

0 comments on commit 29141f0

Please sign in to comment.