Skip to content

Commit

Permalink
attempt to fix #10284
Browse files Browse the repository at this point in the history
- use proper size types where needed in external C functions
- use "modelica_integer" instead of "int" when generating MetaModelica code
- update bootstrapping sources (new C declarations needed)
  • Loading branch information
adrpo committed Mar 2, 2023
1 parent ef60d40 commit cd8d4fa
Show file tree
Hide file tree
Showing 19 changed files with 315 additions and 303 deletions.
2 changes: 1 addition & 1 deletion OMCompiler/Compiler/Template/CodegenCFunctions.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -1300,7 +1300,7 @@ template extType(Type type, Boolean isInput, Boolean isArray, Boolean returnType
"Generates type for external function argument or return value."
::=
let s = match type
case T_INTEGER(__) then "int"
case T_INTEGER(__) then if not acceptMetaModelicaGrammar() then 'int' else 'modelica_integer'
case T_REAL(__) then "double"
case T_STRING(__) then "const char*"
case T_BOOL(__) then "int"
Expand Down
4 changes: 2 additions & 2 deletions OMCompiler/Compiler/Util/System.mo
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,7 @@ public function appendFile
end appendFile;

public function readFile
"Does not fail. Returns strings describing the error instead."
"Fails if the file is longer than the max supported MM string"
input String inString;
output String outString;
external "C" outString = System_readFile(inString) annotation(Library = "omcruntime");
Expand Down Expand Up @@ -413,7 +413,7 @@ public function setEnv ""
input String value;
input Boolean overwrite "is always true on Windows, so recommended to always call it using true";
output Integer outInteger;
external "C" outInteger=setenv(varName,value,overwrite) annotation(Library = "omcruntime");
external "C" outInteger=SystemImpl__setenv(varName,value,overwrite) annotation(Library = "omcruntime");
end setEnv;

public function subDirectories
Expand Down
2 changes: 1 addition & 1 deletion OMCompiler/Compiler/boot/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ if(EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/bomc/sources.tar.gz")
else()
#download and unpack the sources
file(MAKE_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/bomc/)
file(DOWNLOAD https://build.openmodelica.org/omc/bootstrap/sources.tar.gz
file(DOWNLOAD https://build.openmodelica.org/omc/bootstrap/sources20230302.tar.gz
${CMAKE_CURRENT_SOURCE_DIR}/bomc/sources.tar.gz SHOW_PROGRESS)
execute_process(COMMAND tar xzf sources.tar.gz
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/bomc/)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94372,7 +94372,10 @@ switch (MMC_SWITCH_CAST(tmp3)) {
case 0: {
if (mmc__uniontype__metarecord__typedef__equal(tmp3_2,0,1) == 0) goto tmp2_end;
_txt = tmp3_1;
tmpMeta[0] = omc_Tpl_writeTok(threadData, _txt, _OMC_LIT1573);
tmpMeta[0] = omc_Tpl_writeTok(threadData, _txt,
omc_Config_acceptMetaModelicaGrammar(threadData) ?
_OMC_LIT1365 :
_OMC_LIT1573);
goto tmp2_done;
}
case 1: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ DLLExport
void boxptr_StackOverflow_setStacktraceMessages(threadData_t *threadData, modelica_metatype _numSkip, modelica_metatype _numFrames);
static const MMC_DEFSTRUCTLIT(boxvar_lit_StackOverflow_setStacktraceMessages,2,0) {(void*) boxptr_StackOverflow_setStacktraceMessages,0}};
#define boxvar_StackOverflow_setStacktraceMessages MMC_REFSTRUCTLIT(boxvar_lit_StackOverflow_setStacktraceMessages)
extern void mmc_setStacktraceMessages_threadData(OpenModelica_threadData_ThreadData*, int /*_numSkip*/, int /*_numFrames*/);
extern void mmc_setStacktraceMessages_threadData(OpenModelica_threadData_ThreadData*, modelica_integer /*_numSkip*/, modelica_integer /*_numFrames*/);
DLLExport
modelica_metatype omc_StackOverflow_getStacktraceMessages(threadData_t *threadData);
#define boxptr_StackOverflow_getStacktraceMessages omc_StackOverflow_getStacktraceMessages
Expand Down
4 changes: 2 additions & 2 deletions OMCompiler/Compiler/runtime/Print_omc.c
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ extern int Print_hasBufNewLineAtEnd(threadData_t *threadData)
return PrintImpl__hasBufNewLineAtEnd(threadData);
}

extern int Print_getBufLength(threadData_t *threadData)
extern modelica_integer Print_getBufLength(threadData_t *threadData)
{
return PrintImpl__getBufLength(threadData);
}
Expand Down Expand Up @@ -105,7 +105,7 @@ extern void Print_clearBuf(threadData_t *threadData)
PrintImpl__clearBuf(threadData);
}

extern void Print_printBufSpace(threadData_t *threadData,int numSpace)
extern void Print_printBufSpace(threadData_t *threadData, modelica_integer numSpace)
{
if (PrintImpl__printBufSpace(threadData,numSpace))
MMC_THROW();
Expand Down
94 changes: 46 additions & 48 deletions OMCompiler/Compiler/runtime/System_omc.c
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ extern const char* System_stringReplace(const char* str, const char* source, con

extern const char* System_makeC89Identifier(const char* str)
{
int i=0, len=strlen(str);
modelica_integer i=0, len=strlen(str);
char *res = omc_alloc_interface.malloc_strdup(str);
if (!((res[0]>='a' && res[0]<='z') || (res[0]>='A' && res[0]<='Z'))) {
res[0] = '_';
Expand All @@ -99,7 +99,7 @@ extern const char* System_makeC89Identifier(const char* str)
return res;
}

extern int System_stringFind(const char* str, const char* searchStr)
extern modelica_integer System_stringFind(const char* str, const char* searchStr)
{
const char *found = strstr(str, searchStr);
if (found == NULL)
Expand All @@ -116,25 +116,25 @@ extern const char* System_stringFindString(const char* str, const char* searchSt
return strcpy(ModelicaAllocateString(strlen(found)), found);
}

extern void System_realtimeTick(int ix)
extern void System_realtimeTick(modelica_integer ix)
{
if (ix < 0 || ix >= NUM_USER_RT_CLOCKS) MMC_THROW();
rt_tick(ix);
}

extern double System_realtimeTock(int ix)
extern double System_realtimeTock(modelica_integer ix)
{
if (ix < 0 || ix >= NUM_USER_RT_CLOCKS) MMC_THROW();
return rt_tock(ix);
}

extern void System_realtimeClear(int ix)
extern void System_realtimeClear(modelica_integer ix)
{
if (ix < 0 || ix >= NUM_USER_RT_CLOCKS) MMC_THROW();
rt_clear(ix);
}

extern int System_realtimeNtick(int ix)
extern modelica_integer System_realtimeNtick(modelica_integer ix)
{
if (ix < 0 || ix >= NUM_USER_RT_CLOCKS) MMC_THROW();
return rt_ncall(ix);
Expand Down Expand Up @@ -208,28 +208,28 @@ extern const char* System_dirname(const char* str)
return res;
}

extern int System_strncmp(const char *str1, const char *str2, int len)
extern modelica_integer System_strncmp(const char *str1, const char *str2, modelica_integer len)
{
int res= strncmp(str1,str2,len);
modelica_integer res= strncmp(str1,str2,len);
/* adrpo: 2010-10-07, return -1, 0, +1 so we can pattern match on it directly! */
if (res>0) res = 1;
else if (res<0) res = -1;
return res;
}

extern int System_strcmp(const char *str1, const char *str2)
extern modelica_integer System_strcmp(const char *str1, const char *str2)
{
int res = strcmp(str1,str2);
modelica_integer res = strcmp(str1,str2);
/* adrpo: 2010-10-07, return -1, 0, +1 so we can pattern match on it directly! */
if (res>0) res = 1;
else if (res<0) res = -1;
return res;
}

extern int System_strcmp_offset(const char *str1, int offset1, int length1, const char *str2, int offset2, int length2)
extern modelica_integer System_strcmp_offset(const char *str1, modelica_integer offset1, modelica_integer length1, const char *str2, modelica_integer offset2, modelica_integer length2)
{
int n = length1 > length2 ? length1 : length2;
int res = strncmp(str1+offset1-1, str2+offset2-1, n);
modelica_integer n = length1 > length2 ? length1 : length2;
modelica_integer res = strncmp(str1+offset1-1, str2+offset2-1, n);
if (res>0) res = 1;
else if (res<0) res = -1;
return res;
Expand Down Expand Up @@ -295,7 +295,7 @@ extern void System_setUsesCardinality(int b)
usesCardinality = b;
}

extern void* System_strtok(const char *str0, const char *delimit)
extern modelica_metatype System_strtok(const char *str0, const char *delimit)
{
char *s;
void *res = mmc_mk_nil();
Expand All @@ -312,13 +312,13 @@ extern void* System_strtok(const char *str0, const char *delimit)
return listReverse(res);
}

extern char* System_substring(const char *str, int start, int stop)
extern char* System_substring(const char *str, modelica_integer start, modelica_integer stop)
{
char* substring = NULL;
int startIndex = start;
int stopIndex = stop;
int len1 = strlen(str);
int len2 = 0;
modelica_integer startIndex = start;
modelica_integer stopIndex = stop;
modelica_integer len1 = strlen(str);
modelica_integer len2 = 0;
void *res = NULL;

/* Check arguments */
Expand Down Expand Up @@ -346,7 +346,7 @@ extern char* System_substring(const char *str, int start, int stop)

extern char* System_toupper(const char *str)
{
int i;
modelica_integer i;
char* strToUpper = strcpy(ModelicaAllocateString(strlen(str)),str);
for (i = 0; i < strlen(strToUpper); i++)
{
Expand All @@ -357,7 +357,7 @@ extern char* System_toupper(const char *str)

extern char* System_tolower(const char *str)
{
int i;
modelica_integer i;
char* strToLower = strcpy(ModelicaAllocateString(strlen(str)),str);
for (i = 0; i < strlen(strToLower); i++)
{
Expand All @@ -379,19 +379,17 @@ void System_setClassnamesForSimulation(const char *class_names)
class_names_for_simulation = omc_alloc_interface.malloc_strdup(class_names);
}

extern double System_getVariableValue(double _timeStamp, void* _timeValues, void* _varValues)
extern double System_getVariableValue(double _timeStamp, modelica_metatype _timeValues, modelica_metatype _varValues)
{
double res = 0;
if (SystemImpl__getVariableValue(_timeStamp,_timeValues,_varValues,&res))
MMC_THROW();
return res;
}

extern void* System_getFileModificationTime(const char *fileName)
extern modelica_metatype System_getFileModificationTime(const char *fileName)
{
omc_stat_t attrib;
double elapsedTime; // the time elapsed as double
int result; // the result of the function call

if (omc_stat( fileName, &attrib ) != 0) {
return mmc_mk_none();
Expand All @@ -404,14 +402,14 @@ extern void* System_getFileModificationTime(const char *fileName)
/**
* @brief Scan directory for package files with given pattern except for packageName.
*
* @param directory Directory to search in
* @param pattern Pattern to search for, e.g. "*.mo" or "*.moc".
* @param packageName Name of packages, e.g. "package.mo" or "package.moc"
* @return void* List of file names matching pattern.
* @param directory Directory to search in
* @param pattern Pattern to search for, e.g. "*.mo" or "*.moc".
* @param packageName Name of packages, e.g. "package.mo" or "package.moc"
* @return modelica_metatype List of file names matching pattern.
*/
void* omc_scanDirForPackagePattern(const char* directory, const char* pattern, const wchar_t* packageName)
modelica_metatype omc_scanDirForPackagePattern(const char* directory, const char* pattern, const wchar_t* packageName)
{
void *res;
modelica_metatype res;
WIN32_FIND_DATAW FileData;
BOOL more = TRUE;
HANDLE sh;
Expand Down Expand Up @@ -447,10 +445,10 @@ void* omc_scanDirForPackagePattern(const char* directory, const char* pattern, c
/**
* @brief Scan directory for .mo files excluding package.mo.
*
* @param directory Directory to search in.
* @return void* List of file names.
* @param directory Directory to search in.
* @return modelica_metatype List of file names.
*/
void* System_moFiles(const char *directory)
modelica_metatype System_moFiles(const char *directory)
#if defined(__MINGW32__) || defined(_MSC_VER)
{
return omc_scanDirForPackagePattern(directory, "*.mo", L"package.mo");
Expand All @@ -476,18 +474,18 @@ void* System_moFiles(const char *directory)
/**
* @brief Scan directory for .moc files excluding package.moc.
*
* @param directory Directory to search in.
* @return void* List of file names.
* @param directory Directory to search in.
* @return modelica_metatype List of file names.
*/
void* System_mocFiles(const char *directory)
modelica_metatype System_mocFiles(const char *directory)
#if defined(__MINGW32__) || defined(_MSC_VER)
{
return omc_scanDirForPackagePattern(directory, "*.moc", L"package.moc");
}
#else
{
int i,count;
void *res;
modelica_metatype res;
struct dirent **files = NULL;
select_from_dir = directory;
count = scandir(directory, &files, file_select_moc, NULL);
Expand All @@ -502,19 +500,19 @@ void* System_mocFiles(const char *directory)
}
#endif

extern int System_lookupFunction(int _inLibHandle, const char* _inFunc)
extern modelica_integer System_lookupFunction(modelica_integer _inLibHandle, const char* _inFunc)
{
int res = SystemImpl__lookupFunction(_inLibHandle, _inFunc);
modelica_integer res = SystemImpl__lookupFunction(_inLibHandle, _inFunc);
if (res == -1) MMC_THROW();
return res;
}

extern void System_freeFunction(int _inFuncHandle, int printDebug)
extern void System_freeFunction(modelica_integer _inFuncHandle, int printDebug)
{
if (SystemImpl__freeFunction(_inFuncHandle, printDebug)) MMC_THROW();
}

extern void System_freeLibrary(int _inLibHandle, int printDebug)
extern void System_freeLibrary(modelica_integer _inLibHandle, int printDebug)
{
if (SystemImpl__freeLibrary(_inLibHandle, printDebug)) MMC_THROW();
}
Expand All @@ -524,7 +522,7 @@ extern int System_userIsRoot()
return CONFIG_USER_IS_ROOT;
}

extern int System_getuid()
extern modelica_integer System_getuid()
{
#if defined(__MINGW32__) || defined(_MSC_VER)
return 0;
Expand All @@ -540,7 +538,7 @@ extern const char* System_readEnv(const char *envname)
return strcpy(ModelicaAllocateString(strlen(envvalue)),envvalue);
}

extern void System_getCurrentDateTime(int* sec, int* min, int* hour, int* mday, int* mon, int* year)
extern void System_getCurrentDateTime(modelica_integer* sec, modelica_integer* min, modelica_integer* hour, modelica_integer* mday, modelica_integer* mon, modelica_integer* year)
{
time_t t;
struct tm* localTime;
Expand All @@ -560,9 +558,9 @@ extern const char* System_getUUIDStr()
return strcpy(ModelicaAllocateString(strlen(res)),res);
}

extern int System_loadLibrary(const char *name, int relativePath, int printDebug)
extern modelica_integer System_loadLibrary(const char *name, int relativePath, int printDebug)
{
int res = SystemImpl__loadLibrary(name, relativePath, printDebug);
modelica_integer res = SystemImpl__loadLibrary(name, relativePath, printDebug);
if (res == -1) MMC_THROW();
return res;
}
Expand Down Expand Up @@ -619,10 +617,10 @@ void* System_subDirectories(const char *directory)
}
#endif

extern void* System_regex(const char* str, const char* re, int maxn, int extended, int sensitive, int *nmatch)
extern void* System_regex(const char* str, const char* re, modelica_integer maxn, int extended, int sensitive, modelica_integer *nmatch)
{
void *res;
int i = 0;
modelica_integer i = 0;
void **matches = omc_alloc_interface.malloc(sizeof(void*)*maxn);
*nmatch = OpenModelica_regexImpl(str,re,maxn,extended,sensitive,mmc_mk_scon,(void**)matches);
res = mmc_mk_nil();
Expand Down

0 comments on commit cd8d4fa

Please sign in to comment.