Skip to content

Commit

Permalink
- Added preprocessor macros MMC_TRY, MMC_CATCH, MMC_THROW to easier c…
Browse files Browse the repository at this point in the history
…hange exception implementation in the bootstrapped compiler

git-svn-id: https://openmodelica.org/svn/OpenModelica/trunk@7189 f25d12d1-65f4-0310-ae8a-bbce733d8d8e
  • Loading branch information
sjoelund committed Nov 25, 2010
1 parent 8c76e29 commit 9f04776
Show file tree
Hide file tree
Showing 16 changed files with 139 additions and 103 deletions.
58 changes: 38 additions & 20 deletions Compiler/SimCodeC.mo
Expand Up @@ -14340,7 +14340,8 @@ algorithm
txt = Tpl.writeText(txt, i_fname);
txt = Tpl.writeTok(txt, Tpl.ST_STRING_LIST({
"(type_description * inArgs, type_description * outVar)\n",
"{\n"
"{\n",
" modelica_boolean __tmpFailure;\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 @@ -14353,14 +14354,23 @@ 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 = fun_350(txt, i_outVars);
txt = Tpl.writeTok(txt, Tpl.ST_STRING("_"));
txt = Tpl.writeText(txt, i_fname);
txt = Tpl.writeTok(txt, Tpl.ST_STRING("("));
txt = Tpl.pushIter(txt, Tpl.ITER_OPTIONS(0, NONE(), SOME(Tpl.ST_STRING(", ")), 0, 0, Tpl.ST_NEW_LINE(), 0, Tpl.ST_NEW_LINE()));
txt = lm_351(txt, i_functionArguments);
txt = Tpl.popIter(txt);
txt = Tpl.writeTok(txt, Tpl.ST_LINE(");\n"));
txt = Tpl.writeTok(txt, Tpl.ST_STRING_LIST({
");\n",
"__tmpFailure = 0;\n",
"MMC_CATCH();\n",
"if (__tmpFailure) return 1;\n"
}, true));
txt = fun_353(txt, i_outVars);
txt = Tpl.softNewLine(txt);
txt = Tpl.writeTok(txt, Tpl.ST_LINE("return 0;\n"));
Expand Down Expand Up @@ -20381,7 +20391,7 @@ algorithm
local
DAE.Statement i_s;
equation
txt = Tpl.writeTok(txt, Tpl.ST_STRING("throw 1;"));
txt = Tpl.writeTok(txt, Tpl.ST_STRING("MMC_THROW();"));
txt = Tpl.writeTok(txt, Tpl.ST_NEW_LINE());
then (txt, i_varDecls);

Expand Down Expand Up @@ -22400,7 +22410,7 @@ algorithm
txt = Tpl.writeText(txt, i_tmp);
txt = Tpl.writeTok(txt, Tpl.ST_STRING_LIST({
" = 0; /* begin failure */\n",
"try {\n"
"MMC_TRY()\n"
}, true));
txt = Tpl.pushBlock(txt, Tpl.BT_INDENT(2));
txt = Tpl.writeText(txt, i_stmtBody);
Expand All @@ -22409,11 +22419,11 @@ algorithm
txt = Tpl.writeTok(txt, Tpl.ST_LINE(" = 1;\n"));
txt = Tpl.popBlock(txt);
txt = Tpl.writeTok(txt, Tpl.ST_STRING_LIST({
"} catch (int ex) {}\n",
"MMC_CATCH()\n",
"if ("
}, false));
txt = Tpl.writeText(txt, i_tmp);
txt = Tpl.writeTok(txt, Tpl.ST_STRING(") throw 1; /* end failure */"));
txt = Tpl.writeTok(txt, Tpl.ST_STRING(") MMC_THROW(); /* end failure */"));
then (txt, i_varDecls);

case ( txt,
Expand Down Expand Up @@ -22498,7 +22508,10 @@ algorithm
i_body = Tpl.pushIter(Tpl.emptyTxt, Tpl.ITER_OPTIONS(0, NONE(), SOME(Tpl.ST_NEW_LINE()), 0, 0, Tpl.ST_NEW_LINE(), 0, Tpl.ST_NEW_LINE()));
(i_body, i_varDecls) = lm_523(i_body, i_tryBody, i_varDecls, i_context);
i_body = Tpl.popIter(i_body);
txt = Tpl.writeTok(txt, Tpl.ST_LINE("try {\n"));
txt = Tpl.writeTok(txt, Tpl.ST_STRING_LIST({
"#error \"Using STMT_TRY: This is deprecated, and should be matched with catch anyway.\"\n",
"try {\n"
}, true));
txt = Tpl.pushBlock(txt, Tpl.BT_INDENT(2));
txt = Tpl.writeText(txt, i_body);
txt = Tpl.softNewLine(txt);
Expand Down Expand Up @@ -22588,7 +22601,10 @@ algorithm
i_body = Tpl.pushIter(Tpl.emptyTxt, Tpl.ITER_OPTIONS(0, NONE(), SOME(Tpl.ST_NEW_LINE()), 0, 0, Tpl.ST_NEW_LINE(), 0, Tpl.ST_NEW_LINE()));
(i_body, i_varDecls) = lm_525(i_body, i_catchBody, i_varDecls, i_context);
i_body = Tpl.popIter(i_body);
txt = Tpl.writeTok(txt, Tpl.ST_LINE("catch (int i) {\n"));
txt = Tpl.writeTok(txt, Tpl.ST_STRING_LIST({
"#error \"Using STMT_CATCH: This is deprecated, and should be matched with catch anyway.\"\n",
"catch (int i) {\n"
}, true));
txt = Tpl.pushBlock(txt, Tpl.BT_INDENT(2));
txt = Tpl.writeText(txt, i_body);
txt = Tpl.softNewLine(txt);
Expand Down Expand Up @@ -29201,7 +29217,7 @@ algorithm
case ( txt,
Absyn.MATCHCONTINUE() )
equation
txt = Tpl.writeTok(txt, Tpl.ST_STRING("throw 1"));
txt = Tpl.writeTok(txt, Tpl.ST_STRING("MMC_THROW()"));
then txt;

case ( txt,
Expand Down Expand Up @@ -29259,7 +29275,7 @@ algorithm
case ( txt,
Absyn.MATCHCONTINUE() )
equation
txt = Tpl.writeTok(txt, Tpl.ST_STRING("try "));
txt = Tpl.writeTok(txt, Tpl.ST_STRING("MMC_TRY()"));
then txt;

case ( txt,
Expand Down Expand Up @@ -29489,7 +29505,7 @@ algorithm
_,
_ )
equation
txt = Tpl.writeTok(txt, Tpl.ST_STRING("throw 1;"));
txt = Tpl.writeTok(txt, Tpl.ST_STRING("MMC_THROW();"));
txt = Tpl.writeTok(txt, Tpl.ST_NEW_LINE());
then txt;

Expand Down Expand Up @@ -29637,7 +29653,7 @@ algorithm
case ( txt,
Absyn.MATCHCONTINUE() )
equation
txt = Tpl.writeTok(txt, Tpl.ST_STRING(" catch (int ex) {}"));
txt = Tpl.writeTok(txt, Tpl.ST_STRING("MMC_CATCH()"));
then txt;

case ( txt,
Expand Down Expand Up @@ -29743,10 +29759,10 @@ algorithm
i_preExp = Tpl.writeText(i_preExp, i_done);
i_preExp = Tpl.writeTok(i_preExp, Tpl.ST_STRING("; "));
i_preExp = Tpl.writeText(i_preExp, i_ix);
i_preExp = Tpl.writeTok(i_preExp, Tpl.ST_STRING("++) "));
i_preExp = fun_634(i_preExp, i_exp_matchType);
i_preExp = Tpl.writeTok(i_preExp, Tpl.ST_LINE("{\n"));
i_preExp = Tpl.writeTok(i_preExp, Tpl.ST_LINE("++) {\n"));
i_preExp = Tpl.pushBlock(i_preExp, Tpl.BT_INDENT(2));
i_preExp = fun_634(i_preExp, i_exp_matchType);
i_preExp = Tpl.softNewLine(i_preExp);
i_preExp = Tpl.writeTok(i_preExp, Tpl.ST_STRING("switch ("));
i_preExp = Tpl.writeText(i_preExp, i_ix);
i_preExp = Tpl.writeTok(i_preExp, Tpl.ST_LINE(") {\n"));
Expand All @@ -29755,13 +29771,15 @@ algorithm
i_preExp = Tpl.popIter(i_preExp);
i_preExp = Tpl.softNewLine(i_preExp);
i_preExp = Tpl.writeTok(i_preExp, Tpl.ST_LINE("}\n"));
i_preExp = Tpl.popBlock(i_preExp);
i_preExp = Tpl.writeTok(i_preExp, Tpl.ST_STRING("}"));
i_preExp = fun_641(i_preExp, i_exp_matchType);
i_preExp = Tpl.softNewLine(i_preExp);
i_preExp = Tpl.writeTok(i_preExp, Tpl.ST_STRING("if (!"));
i_preExp = Tpl.popBlock(i_preExp);
i_preExp = Tpl.writeTok(i_preExp, Tpl.ST_STRING_LIST({
"}\n",
"if (!"
}, false));
i_preExp = Tpl.writeText(i_preExp, i_done);
i_preExp = Tpl.writeTok(i_preExp, Tpl.ST_LINE(") throw 1;\n"));
i_preExp = Tpl.writeTok(i_preExp, Tpl.ST_LINE(") MMC_THROW();\n"));
i_preExp = Tpl.popBlock(i_preExp);
i_preExp = Tpl.writeTok(i_preExp, Tpl.ST_LINE("}\n"));
i_preExp = Tpl.popBlock(i_preExp);
Expand Down Expand Up @@ -32397,7 +32415,7 @@ algorithm
(i_expPart, i_preExp, i_varDecls) = daeExp(Tpl.emptyTxt, i_rhs, i_context, i_preExp, i_varDecls);
txt = Tpl.writeText(txt, i_preExp);
txt = Tpl.softNewLine(txt);
(txt, i_expPart, _, i_varDecls, i_assignments) = patternMatch(txt, i_lhs, i_expPart, Tpl.strTokText(Tpl.ST_STRING("throw 1")), i_varDecls, i_assignments);
(txt, i_expPart, _, i_varDecls, i_assignments) = patternMatch(txt, i_lhs, i_expPart, Tpl.strTokText(Tpl.ST_STRING("MMC_THROW()")), i_varDecls, i_assignments);
txt = Tpl.writeText(txt, i_assignments);
then (txt, i_varDecls);

Expand Down
5 changes: 2 additions & 3 deletions Compiler/runtime/Dynload.cpp
Expand Up @@ -93,9 +93,8 @@ static int execute_function(void *in_arg, void **out_arg,
if (debugFlag) { fprintf(stderr, "calling the function\n"); fflush(stderr); }

/* call our function pointer! */
try {
func(arglst, &retarg);
retval = 0;
try { /* Don't let external C functions throwing C++ exceptions kill OMC! */
retval = func(arglst, &retarg);
} catch (...) {
retval = 1;
}
Expand Down
28 changes: 14 additions & 14 deletions Compiler/runtime/IOStreamExt_omc.cpp
Expand Up @@ -49,79 +49,79 @@ extern "C" {
extern int IOStreamExt_createFile(const char* filename)
{
fprintf(stderr, "NYI: %s:%d\n", __FILE__, __LINE__);
throw 1;
MMC_THROW();
}

extern void IOStreamExt_closeFile(int id)
{
fprintf(stderr, "NYI: %s:%d\n", __FILE__, __LINE__);
throw 1;
MMC_THROW();
}

extern void IOStreamExt_deleteFile(int id)
{
fprintf(stderr, "NYI: %s:%d\n", __FILE__, __LINE__);
throw 1;
MMC_THROW();
}

extern void IOStreamExt_clearFile(int id)
{
fprintf(stderr, "NYI: %s:%d\n", __FILE__, __LINE__);
throw 1;
MMC_THROW();
}

extern void IOStreamExt_printFile(int id, int whereToPrint)
{
fprintf(stderr, "NYI: %s:%d\n", __FILE__, __LINE__);
throw 1;
MMC_THROW();
}

extern const char* IOStreamExt_readFile(int id)
{
fprintf(stderr, "NYI: %s:%d\n", __FILE__, __LINE__);
throw 1;
MMC_THROW();
}

extern void IOStreamExt_appendFile(int id, const char* str)
{
fprintf(stderr, "NYI: %s:%d\n", __FILE__, __LINE__);
throw 1;
MMC_THROW();
}

extern int IOStreamExt_createBuffer()
{
fprintf(stderr, "NYI: %s:%d\n", __FILE__, __LINE__);
throw 1;
MMC_THROW();
}

extern void IOStreamExt_deleteBuffer(int id)
{
fprintf(stderr, "NYI: %s:%d\n", __FILE__, __LINE__);
throw 1;
MMC_THROW();
}

extern void IOStreamExt_clearBuffer(int id)
{
fprintf(stderr, "NYI: %s:%d\n", __FILE__, __LINE__);
throw 1;
MMC_THROW();
}

extern const char* IOStreamExt_readBuffer(int id)
{
fprintf(stderr, "NYI: %s:%d\n", __FILE__, __LINE__);
throw 1;
MMC_THROW();
}

extern void IOStreamExt_appendBuffer(int id, const char* str)
{
fprintf(stderr, "NYI: %s:%d\n", __FILE__, __LINE__);
throw 1;
MMC_THROW();
}

extern void IOStreamExt_printBuffer(int id, int whereToPrint)
{
fprintf(stderr, "NYI: %s:%d\n", __FILE__, __LINE__);
throw 1;
MMC_THROW();
}

extern const char* IOStreamExt_appendReversedList(modelica_metatype lst)
Expand Down Expand Up @@ -164,7 +164,7 @@ extern void IOStreamExt_printReversedList(modelica_metatype lst, int whereToPrin
switch (whereToPrint) {
case 1: f = stdout; break;
case 2: f = stderr; break;
default: throw 1;
default: MMC_THROW();
}
strs = (const char**) malloc(sizeof(const char*)*lstLen);

Expand Down
5 changes: 3 additions & 2 deletions Compiler/runtime/OptManager_omc.cpp
Expand Up @@ -29,20 +29,21 @@
*/

#include "optmanager.cpp"
#include "meta_modelica.h"

extern "C"
{

extern void OptManager_setOption(const char *strEntry, int strValue)
{
if (OptManagerImpl__setOption(strEntry,strValue)) throw 1;
if (OptManagerImpl__setOption(strEntry,strValue)) MMC_THROW();
}

extern int OptManager_getOption(const char *strEntry)
{
int res = OptManagerImpl__getOption(strEntry);
if (res == -1)
throw 1;
MMC_THROW();
return res;
}

Expand Down
15 changes: 8 additions & 7 deletions Compiler/runtime/Print_omc.cpp
Expand Up @@ -30,21 +30,22 @@

#include <stdio.h>
#include <stdlib.h>
#include "meta_modelica.h"
extern "C" {

#include "printimpl.c"

extern void Print_printErrorBuf(const char* str)
{
if (PrintImpl__printErrorBuf(str))
throw 1;
MMC_THROW();
}

extern void Print_printBuf(const char* str)
{
//fprintf(stderr, "Print_writeBuf %s\n", str);
if (PrintImpl__printBuf(str))
throw 1;
MMC_THROW();
}

extern int Print_hasBufNewLineAtEnd(void)
Expand All @@ -61,15 +62,15 @@ extern const char* Print_getString(void)
{
const char* res = PrintImpl__getString();
if (res == NULL)
throw 1;
MMC_THROW();
return strdup(res);
}

extern const char* Print_getErrorString(void)
{
const char* res = PrintImpl__getErrorString();
if (res == NULL)
throw 1;
MMC_THROW();
return strdup(res);
}

Expand All @@ -86,20 +87,20 @@ extern void Print_clearBuf(void)
extern void Print_printBufSpace(int numSpace)
{
if (PrintImpl__printBufSpace(numSpace))
throw 1;
MMC_THROW();
}

extern void Print_printBufNewLine(void)
{
if (PrintImpl__printBufNewLine())
throw 1;
MMC_THROW();
}

extern void Print_writeBuf(const char* filename)
{
//fprintf(stderr, "Print_writeBuf %s: %s\n", filename, buf);
if (PrintImpl__writeBuf(filename))
throw 1;
MMC_THROW();
}

}
2 changes: 1 addition & 1 deletion Compiler/runtime/RTOpts_omc.cpp
Expand Up @@ -50,7 +50,7 @@ extern modelica_metatype RTOpts_args(modelica_metatype args) {
const char *arg = MMC_STRINGDATA(head);
switch (RTOptsImpl__arg(arg)) {
case ARG_FAILURE:
throw 1;
MMC_THROW();
break;
case ARG_CONSUME:
break;
Expand Down

0 comments on commit 9f04776

Please sign in to comment.