Skip to content

Commit

Permalink
- Added new API: compareFilesAndMove (compares two files and replaces…
Browse files Browse the repository at this point in the history
… the destination if they differ)

- BuildOMC.mos uses fewer system() calls now, instead using built-in API functions
- Fixed code generation of MMC_TRY for matchcontinue to use the faster MMC_TRY_INTERNAL instead (~2% performance gain for the bootstrapped compiler)


git-svn-id: https://openmodelica.org/svn/OpenModelica/trunk@18000 f25d12d1-65f4-0310-ae8a-bbce733d8d8e
  • Loading branch information
sjoelund committed Nov 4, 2013
1 parent d89d4f9 commit 75f0bfb
Show file tree
Hide file tree
Showing 6 changed files with 92 additions and 4 deletions.
10 changes: 10 additions & 0 deletions Compiler/FrontEnd/ModelicaBuiltin.mo
Expand Up @@ -1371,6 +1371,16 @@ external "builtin" annotation(__OpenModelica_Impure=true);
annotation(preferredView="text");
end writeFile;

function compareFilesAndMove
input String newFile;
input String oldFile;
output Boolean success;
external "builtin" annotation(__OpenModelica_Impure=true,Documentation(info="<html>
<p>Compares <i>newFile</i> and <i>oldFile</i>. If they differ, overwrite <i>oldFile</i> with <i>newFile</i></p>
<p>Basically: test -f ../oldFile && cmp newFile oldFile || mv newFile oldFile</p>
</html>"));
end compareFilesAndMove;

function regex "Sets the error buffer and returns -1 if the regex does not compile.
The returned result is the same as POSIX regex():
Expand Down
11 changes: 11 additions & 0 deletions Compiler/Script/CevalScript.mo
Expand Up @@ -1824,6 +1824,17 @@ algorithm
then
(cache,Values.BOOL(false),st);

case (cache,env,"compareFilesAndMove",{Values.STRING(str1),Values.STRING(str2)},st,_)
equation
true = System.regularFileExists(str1);
b = System.regularFileExists(str2) and System.fileContentsEqual(str1,str2);
b = Debug.bcallret2(not b,System.rename,str1,str2,b);
then
(cache,Values.BOOL(b),st);

case (cache,env,"compareFilesAndMove",_,st,_)
then (cache,Values.BOOL(false),st);

case (cache,env,"readFileNoNumeric",{Values.STRING(str)},st,_)
equation
str_1 = System.readFileNoNumeric(str);
Expand Down
8 changes: 4 additions & 4 deletions Compiler/Template/CodegenC.tpl
Expand Up @@ -6916,10 +6916,10 @@ case STMT_FAILURE(__) then
;separator="\n")
<<
<%tmp%> = 0; /* begin failure */
MMC_TRY()
MMC_TRY_INTERNAL(mmc_jumper)
<%stmtBody%>
<%tmp%> = 1;
MMC_CATCH()
MMC_CATCH_INTERNAL(mmc_jumper)
if (<%tmp%>) MMC_THROW_INTERNAL(); /* end failure */
>>
end algStmtFailure;
Expand Down Expand Up @@ -8846,13 +8846,13 @@ case exp as MATCHEXPRESSION(__) then
case MATCH(switch=SOME(_)) then '<%done%> = 0;<%\n%>{'
else 'for (<%ix%> = 0, <%done%> = 0; <%ix%> < <%listLength(exp.cases)%> && !<%done%>; <%ix%>++) {'
%>
<% match exp.matchType case MATCHCONTINUE(__) then "MMC_TRY()" %>
<% match exp.matchType case MATCHCONTINUE(__) then "MMC_TRY_INTERNAL(mmc_jumper)" %>
switch (MMC_SWITCH_CAST(<%ix%>)) {
<%daeExpMatchCases(exp.cases,tupleAssignExps,exp.matchType,ix,res,startIndexOutputs,prefix,startIndexInputs,exp.inputs,onPatternFail,done,context,&varDecls)%>
}

<% match exp.matchType case MATCHCONTINUE(__) then "MMC_CATCH()" %>
<% match exp.matchType case MATCHCONTINUE(__) then "MMC_CATCH_INTERNAL(mmc_jumper)" %>
}

if (!<%done%>) MMC_THROW_INTERNAL();
Expand Down
14 changes: 14 additions & 0 deletions Compiler/Util/System.mo
Expand Up @@ -987,6 +987,20 @@ public function fileIsNewerThan
external "C" result = System_fileIsNewerThan(file1,file2) annotation(Library = {"omcruntime"});
end fileIsNewerThan;

public function fileContentsEqual
input String file1;
input String file2;
output Boolean result;
external "C" result = SystemImpl__fileContentsEqual(file1,file2) annotation(Library = {"omcruntime"});
end fileContentsEqual;

public function rename
input String source;
input String dest;
output Boolean result;
external "C" result = SystemImpl__rename(source,dest) annotation(Library = {"omcruntime"});
end rename;

public function numProcessors
output Integer result;
external "C" result = System_numProcessors() annotation(Library = {"omcruntime"});
Expand Down
14 changes: 14 additions & 0 deletions Compiler/runtime/System_rml.c
Expand Up @@ -1842,3 +1842,17 @@ RML_BEGIN_LABEL(System__initGarbageCollector)
RML_TAILCALLK(rmlSC);
}
RML_END_LABEL

RML_BEGIN_LABEL(System__fileContentsEqual)
{
rmlA0 = mk_icon(SystemImpl__fileContentsEqual(RML_STRINGDATA(rmlA0), RML_STRINGDATA(rmlA1)));
RML_TAILCALLK(rmlSC);
}
RML_END_LABEL

RML_BEGIN_LABEL(System__rename)
{
rmlA0 = mk_icon(SystemImpl__rename(RML_STRINGDATA(rmlA0), RML_STRINGDATA(rmlA1)));
RML_TAILCALLK(rmlSC);
}
RML_END_LABEL
39 changes: 39 additions & 0 deletions Compiler/runtime/systemimpl.c
Expand Up @@ -2398,6 +2398,45 @@ void SystemImpl__initGarbageCollector(void)
GC_init();
}

int SystemImpl__fileContentsEqual(const char *file1, const char *file2)
{
char buf1[8192],buf2[8192];
FILE *f1,*f2;
int i1,i2,totalread=0,error=0;
#if !defined(_MSC_VER)
struct stat stbuf1;
struct stat stbuf2;
if (stat(file1, &stbuf1)) return 0;
if (stat(file2, &stbuf2)) return 0;
if (stbuf1.st_size != stbuf2.st_size) return 0;
#endif
f1 = fopen(file1,"rb");
if (f1 == NULL) {
return 0;
}
f2 = fopen(file2,"rb");
if (f2 == NULL) {
fclose(f1);
return 0;
}
do {
i1 = fread(buf1,1,8192,f1);
i2 = fread(buf2,1,8192,f2);
if (i1 != i2 || strncmp(buf1,buf2,i1)) {
error = 1;
}
totalread += i1;
} while(i1 != 0 && error == 0);
fclose(f1);
fclose(f2);
return !error;
}

int SystemImpl__rename(const char *source, const char *dest)
{
return 0==rename(source,dest);
}

#ifdef __cplusplus
}
#endif

0 comments on commit 75f0bfb

Please sign in to comment.