summaryrefslogtreecommitdiff
path: root/src/runtime/c/teyjus/system/error.h
blob: 2db993270ff201d0fb80e91824d8bb4543329d5e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
//////////////////////////////////////////////////////////////////////////////
//Copyright 2008
//  Andrew Gacek, Steven Holte, Gopalan Nadathur, Xiaochu Qi, Zach Snow
//////////////////////////////////////////////////////////////////////////////
// This file is part of Teyjus.                                             //
//                                                                          //
// Teyjus is free software: you can redistribute it and/or modify           //
// it under the terms of the GNU General Public License as published by     //
// the Free Software Foundation, either version 3 of the License, or        //
// (at your option) any later version.                                      //
//                                                                          //
// Teyjus is distributed in the hope that it will be useful,                //
// but WITHOUT ANY WARRANTY; without even the implied warranty of           //
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the            //
// GNU General Public License for more details.                             //
//                                                                          //
// You should have received a copy of the GNU General Public License        //
// along with Teyjus.  If not, see <http://www.gnu.org/licenses/>.          //
//////////////////////////////////////////////////////////////////////////////
/****************************************************************************
 *                                                                          *
 *   File error.h -- error-handling functions                               *
 *                                                                          *
 ****************************************************************************/

#ifndef ERROR_H
#define ERROR_H

#include <stdlib.h>
#include <stdarg.h>
#include <setjmp.h>
#include "tjsignal.h"
#include "../simulator/mctypes.h" //to be modified

/****************************************************************************
 * Exception stack declarations.                                            *
 ****************************************************************************/

typedef enum EM_ExnType{
    EM_NO_ERR = 0,     // no errors
    EM_NO_EXN,         // used for warnings ?? 
    EM_ABORT,          // exit the executable immediately
    EM_EXIT,           // traverse the exception stack and exit
    EM_TOP_LEVEL,      // return to the toplevel 
    EM_QUERY,          // abort solving the query
    EM_QUERY_RESULT,   // query is solved; print answer
    EM_FAIL,           // fail to simulator level
} EM_ExnType;

//function call environment stack
extern SIGNAL_jmp_buf *EM_ExnHandlerStack;
extern int             EM_ExnHandlerStackTop;
extern int             EM_ExnHandlerStackSize;

//exception type
extern EM_ExnType      EM_CurrentExnType;

/****************************************************************************
 * Exception-handling macros                                                *
 ****************************************************************************/

//try 
#define EM_TRY \
if (EM_ExnHandlerStackTop >= EM_ExnHandlerStackSize) \
{ \
   EM_ExnHandlerStackSize = \
     (EM_ExnHandlerStackSize + 1) * 2; \
   EM_ExnHandlerStack = \
     (SIGNAL_jmp_buf *)EM_realloc((void *)EM_ExnHandlerStack, \
	   EM_ExnHandlerStackSize * sizeof(SIGNAL_jmp_buf)); \
} \
if (SIGNAL_setjmp(EM_ExnHandlerStack[EM_ExnHandlerStackTop++]) == 0) \
{

//catch
#define EM_CATCH \
   EM_ExnHandlerStackTop--; \
} \
else

//throw
/* Jump to the nearest (in a dynamic sense) EM_Try block, setting
   EM_CurrentExnType to TYPE. Given a constant, the conditional in
   this macro will be optimized away. 
   
   TODO: added cast to EM_CurrentExnType. */
#define EM_THROW(type) EM_THROWVAL((type), 1)

#define EM_THROWVAL(type, val) \
do { \
   if ((type) == EM_ABORT) \
      exit(1); \
   else \
   { \
      EM_CurrentExnType = (EM_ExnType)(type); \
      SIGNAL_longjmp(EM_ExnHandlerStack[--EM_ExnHandlerStackTop], val); \
   } \
} while(0)

//rethrow
/* pass the current exception to the next handler.  Use only within an
   EM_Catch block. */
#define EM_RETHROW() \
   SIGNAL_longjmp(EM_ExnHandlerStack[--EM_ExnHandlerStackTop], 1)

/* Here's an example use of the above macros:

...
EM_TRY
{
   foo();
   if (foobar)
      EM_THROW(EM_FOOBAR);
}
EM_CATCH
{
   un_foo();			// clean up
   if (EM_CurrentExnType == EM_FOOBAR)
      printf("foobar!");        // stop the error here
   else
      EM_RETHROW();             // let a later handler handle it
}
*/

/****************************************************************************
 * Routines which will generate errors automatically.                       *
 ****************************************************************************/
void *EM_malloc(unsigned int);
void *EM_realloc(void *, unsigned int);
char *EM_strdup(char *);

/****************************************************************************
 * Beginning error indices for different modules (by module abbreviation)   *
 ****************************************************************************/
/* general errors */
#define EM_NO_ERROR              0
#define EM_FIRST_ERR_INDEX       1
#define LINKER_FIRST_ERR_INDEX   50
#define LOADER_FIRST_ERR_INDEX   100
#define STREAM_FIRST_ERR_INDEX   150
#define SIM_FIRST_ERR_INDEX      200
#define BI_FIRST_ERR_INDEX       300
#define RT_FIRST_ERR_INDEX       400
#define FRONT_FIRST_ERR_INDEX    500

/****************************************************************************
 * General-use error messages                                               *
 ****************************************************************************/
enum
{
   EM_OUT_OF_MEMORY = EM_FIRST_ERR_INDEX,
   EM_OUT_OF_HEAP,
   EM_NEWLINE,
   EM_ERROR_COLON,
   EM_WARNING_COLON
};

/****************************************************************************
 * The routine that gets called in the event of an error                    *
 ****************************************************************************/
void EM_error(int inIndex, ...);


/****************************************************************************
 * Have there been any errors since last EM_Reset()?                        *
 ****************************************************************************/
extern Boolean EM_anyErrors;
void EM_reset();

#endif  //ERROR_H