Skip to content

Commit

Permalink
- Change MetaModelica try/throw/catch to setjmp/longjmp implementatio…
Browse files Browse the repository at this point in the history
…n as it gives a 3-10x speedup

git-svn-id: https://openmodelica.org/svn/OpenModelica/trunk@7190 f25d12d1-65f4-0310-ae8a-bbce733d8d8e
  • Loading branch information
sjoelund committed Nov 25, 2010
1 parent 9f04776 commit b33ff76
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 15 deletions.
12 changes: 3 additions & 9 deletions Compiler/SimCodeC.mo
Expand Up @@ -14340,8 +14340,7 @@ algorithm
txt = Tpl.writeText(txt, i_fname);
txt = Tpl.writeTok(txt, Tpl.ST_STRING_LIST({
"(type_description * inArgs, type_description * outVar)\n",
"{\n",
" modelica_boolean __tmpFailure;\n"
"{\n"
}, true));
txt = Tpl.pushBlock(txt, Tpl.BT_INDENT(2));
txt = Tpl.pushIter(txt, Tpl.ITER_OPTIONS(0, NONE(), SOME(Tpl.ST_NEW_LINE()), 0, 0, Tpl.ST_NEW_LINE(), 0, Tpl.ST_NEW_LINE()));
Expand All @@ -14354,10 +14353,7 @@ algorithm
txt = lm_349(txt, i_functionArguments);
txt = Tpl.popIter(txt);
txt = Tpl.softNewLine(txt);
txt = Tpl.writeTok(txt, Tpl.ST_STRING_LIST({
"__tmpFailure = 1; /* check for success */\n",
"MMC_TRY();\n"
}, true));
txt = Tpl.writeTok(txt, Tpl.ST_LINE("MMC_TRY_TOP()\n"));
txt = fun_350(txt, i_outVars);
txt = Tpl.writeTok(txt, Tpl.ST_STRING("_"));
txt = Tpl.writeText(txt, i_fname);
Expand All @@ -14367,9 +14363,7 @@ algorithm
txt = Tpl.popIter(txt);
txt = Tpl.writeTok(txt, Tpl.ST_STRING_LIST({
");\n",
"__tmpFailure = 0;\n",
"MMC_CATCH();\n",
"if (__tmpFailure) return 1;\n"
"MMC_CATCH_TOP(return 1)\n"
}, true));
txt = fun_353(txt, i_outVars);
txt = Tpl.softNewLine(txt);
Expand Down
8 changes: 2 additions & 6 deletions Compiler/susan_codegen/SimCode/SimCodeC.tpl
Expand Up @@ -3129,16 +3129,12 @@ case FUNCTION(__) then
<<
int in_<%fname%>(type_description * inArgs, type_description * outVar)
{
modelica_boolean __tmpFailure;
<%functionArguments |> var => '<%funArgDefinition(var)%>;' ;separator="\n"%>
<%if outVars then '<%retType%> out;'%>
<%functionArguments |> arg => readInVar(arg) ;separator="\n"%>
__tmpFailure = 1; /* check for success */
MMC_TRY();
MMC_TRY_TOP()
<%if outVars then "out = "%>_<%fname%>(<%functionArguments |> var => funArgName(var) ;separator=", "%>);
__tmpFailure = 0;
MMC_CATCH();
if (__tmpFailure) return 1;
MMC_CATCH_TOP(return 1)
<%if outVars then (outVars |> var => writeOutVar(var, i1) ;separator="\n") else "write_noretcall(outVar);"%>
return 0;
}
Expand Down
2 changes: 2 additions & 0 deletions c_runtime/meta_modelica.c
Expand Up @@ -32,6 +32,8 @@

#include "modelica.h"

jmp_buf mmc_jumper;

const struct mmc_header mmc_prim_nil = { MMC_NILHDR };

union mmc_double_as_words {
Expand Down
13 changes: 13 additions & 0 deletions c_runtime/meta_modelica.h
Expand Up @@ -187,9 +187,22 @@ modelica_real mmc_prim_get_real(void *p);
#define mmc__unbox__string(X) MMC_STRINGDATA(X)
#define mmc__unbox__array(X) (*((base_array_t*)X))

#if 1
#include <setjmp.h>
extern jmp_buf mmc_jumper;
#define MMC_TRY() {jmp_buf old_mmc_jumper; *old_mmc_jumper = *mmc_jumper; if (setjmp(mmc_jumper) == 0) {
#define MMC_CATCH() *mmc_jumper = *old_mmc_jumper;} else {*mmc_jumper = *old_mmc_jumper;}}
#define MMC_THROW() longjmp(mmc_jumper,1)

#define MMC_TRY_TOP() if (setjmp(mmc_jumper) == 0) {
#define MMC_CATCH_TOP(X) } else {X;}

#else
/* Old C++ try/catch/throw implementation */
#define MMC_TRY() try {
#define MMC_CATCH() } catch (...) {}
#define MMC_THROW() throw 1
#endif

#if defined(__cplusplus)
}
Expand Down

0 comments on commit b33ff76

Please sign in to comment.