* src/vm/jit/verify/typecheck.c: Moved <assert.h> include to the right
[cacao.git] / src / vm / jit / verify / typecheck-common.h
1 #ifndef _TYPECHECK_COMMON_H
2 #define _TYPECHECK_COMMON_H
3
4 #include "config.h"
5 #include "vm/types.h"
6 #include "vm/global.h"
7
8 #include <assert.h>
9
10 #include "vm/jit/jit.h"
11
12 /****************************************************************************/
13 /* DEBUG HELPERS                                                            */
14 /****************************************************************************/
15
16 #ifdef TYPECHECK_DEBUG
17 #define TYPECHECK_ASSERT(cond)  assert(cond)
18 #else
19 #define TYPECHECK_ASSERT(cond)
20 #endif
21
22 #ifdef TYPECHECK_VERBOSE_OPT
23 extern bool opt_typecheckverbose;
24 #define DOLOG(action)  do { if (opt_typecheckverbose) {action;} } while(0)
25 #else
26 #define DOLOG(action)
27 #endif
28
29 #ifdef TYPECHECK_VERBOSE
30 #define TYPECHECK_VERBOSE_IMPORTANT
31 #define LOGNL              DOLOG(puts(""))
32 #define LOG(str)           DOLOG(puts(str);)
33 #define LOG1(str,a)        DOLOG(printf(str,a); LOGNL)
34 #define LOG2(str,a,b)      DOLOG(printf(str,a,b); LOGNL)
35 #define LOG3(str,a,b,c)    DOLOG(printf(str,a,b,c); LOGNL)
36 #define LOGIF(cond,str)    DOLOG(do {if (cond) { puts(str); }} while(0))
37 #ifdef  TYPEINFO_DEBUG
38 #define LOGINFO(info)      DOLOG(do {typeinfo_print_short(stdout,(info)); LOGNL;} while(0))
39 #else
40 #define LOGINFO(info)
41 #define typevector_print(x,y,z)
42 #endif
43 #define LOGFLUSH           DOLOG(fflush(stdout))
44 #define LOGSTR(str)        DOLOG(printf("%s", str))
45 #define LOGSTR1(str,a)     DOLOG(printf(str,a))
46 #define LOGSTR2(str,a,b)   DOLOG(printf(str,a,b))
47 #define LOGSTR3(str,a,b,c) DOLOG(printf(str,a,b,c))
48 #define LOGNAME(c)         DOLOG(class_classref_or_classinfo_print(c))
49 #define LOGMETHOD(str,m)   DOLOG(printf("%s", str); method_println(m);)
50 #else
51 #define LOG(str)
52 #define LOG1(str,a)
53 #define LOG2(str,a,b)
54 #define LOG3(str,a,b,c)
55 #define LOGIF(cond,str)
56 #define LOGINFO(info)
57 #define LOGFLUSH
58 #define LOGNL
59 #define LOGSTR(str)
60 #define LOGSTR1(str,a)
61 #define LOGSTR2(str,a,b)
62 #define LOGSTR3(str,a,b,c)
63 #define LOGNAME(c)
64 #define LOGMETHOD(str,m)
65 #endif
66
67 #ifdef TYPECHECK_VERBOSE_IMPORTANT
68 #define LOGimp(str)     DOLOG(puts(str);LOGNL)
69 #define LOGimpSTR(str)  DOLOG(puts(str))
70 #else
71 #define LOGimp(str)
72 #define LOGimpSTR(str)
73 #endif
74
75 #if defined(TYPECHECK_VERBOSE) || defined(TYPECHECK_VERBOSE_IMPORTANT)
76 #include <stdio.h>
77 static void typecheck_print_var(FILE *file, jitdata *jd, s4 index);
78 static void typecheck_print_vararray(FILE *file, jitdata *jd, s4 *vars, int len);
79 #endif
80
81
82 /****************************************************************************/
83 /* STATISTICS                                                               */
84 /****************************************************************************/
85
86 #ifdef TYPECHECK_DEBUG
87 /*#define TYPECHECK_STATISTICS*/
88 #endif
89
90 #ifdef TYPECHECK_STATISTICS
91 #define STAT_ITERATIONS  10
92 #define STAT_BLOCKS      10
93 #define STAT_LOCALS      16
94
95 static int stat_typechecked;
96 static int stat_methods_with_handlers;
97 static int stat_methods_maythrow;
98 static int stat_iterations[STAT_ITERATIONS+1];
99 static int stat_reached;
100 static int stat_copied;
101 static int stat_merged;
102 static int stat_merging_changed;
103 static int stat_blocks[STAT_BLOCKS+1];
104 static int stat_locals[STAT_LOCALS+1];
105 static int stat_ins;
106 static int stat_ins_maythrow;
107 static int stat_ins_stack;
108 static int stat_ins_field;
109 static int stat_ins_field_unresolved;
110 static int stat_ins_field_uninitialized;
111 static int stat_ins_invoke;
112 static int stat_ins_invoke_unresolved;
113 static int stat_ins_primload;
114 static int stat_ins_aload;
115 static int stat_ins_builtin;
116 static int stat_ins_builtin_gen;
117 static int stat_ins_branch;
118 static int stat_ins_switch;
119 static int stat_ins_primitive_return;
120 static int stat_ins_areturn;
121 static int stat_ins_areturn_unresolved;
122 static int stat_ins_athrow;
123 static int stat_ins_athrow_unresolved;
124 static int stat_ins_unchecked;
125 static int stat_handlers_reached;
126 static int stat_savedstack;
127
128 #define TYPECHECK_MARK(var)   ((var) = true)
129 #define TYPECHECK_COUNT(cnt)  (cnt)++
130 #define TYPECHECK_COUNTIF(cond,cnt)  do{if(cond) (cnt)++;} while(0)
131 #define TYPECHECK_COUNT_FREQ(array,val,limit) \
132         do {                                                                      \
133                 if ((val) < (limit)) (array)[val]++;  \
134                 else (array)[limit]++;                            \
135         } while (0)
136
137 #else
138                                                    
139 #define TYPECHECK_COUNT(cnt)
140 #define TYPECHECK_MARK(var)
141 #define TYPECHECK_COUNTIF(cond,cnt)
142 #define TYPECHECK_COUNT_FREQ(array,val,limit)
143 #endif
144
145
146 /****************************************************************************/
147 /* MACROS FOR THROWING EXCEPTIONS                                           */
148 /****************************************************************************/
149
150 #define TYPECHECK_VERIFYERROR_ret(m,msg,retval)                      \
151     do {                                                             \
152         exceptions_throw_verifyerror((m), (msg));                    \
153         return (retval);                                             \
154     } while (0)
155
156 #define TYPECHECK_VERIFYERROR_main(msg)  TYPECHECK_VERIFYERROR_ret(state.m,(msg),NULL)
157 #define TYPECHECK_VERIFYERROR_bool(msg)  TYPECHECK_VERIFYERROR_ret(state->m,(msg),false)
158
159
160 /****************************************************************************/
161 /* VERIFIER STATE STRUCT                                                    */
162 /****************************************************************************/
163
164 /* verifier_state - This structure keeps the current state of the      */
165 /* bytecode verifier for passing it between verifier functions.        */
166
167 typedef struct verifier_state {
168     instruction *iptr;               /* pointer to current instruction */
169     basicblock *bptr;                /* pointer to current basic block */
170
171         methodinfo *m;                               /* the current method */
172         jitdata *jd;                         /* jitdata for current method */
173         codegendata *cd;                 /* codegendata for current method */
174
175         basicblock *basicblocks;
176         s4 basicblockcount;
177         
178         s4 numlocals;                         /* number of local variables */
179         s4 validlocals;                /* number of Java-accessible locals */
180         s4 *reverselocalmap;
181         
182         typedescriptor returntype;    /* return type of the current method */
183
184         s4 *savedindices;
185         s4 *savedinvars;                            /* saved invar pointer */
186
187         s4 exinvars;
188         
189     exceptiontable **handlers;            /* active exception handlers */
190         
191     bool repeat;            /* if true, blocks are iterated over again */
192     bool initmethod;             /* true if this is an "<init>" method */
193
194 #ifdef TYPECHECK_STATISTICS
195         bool stat_maythrow;          /* at least one instruction may throw */
196 #endif
197 } verifier_state;
198
199 void typecheck_init_flags(verifier_state *state);
200 void typecheck_reset_flags(verifier_state *state);
201
202 #endif /* _TYPECHECK_COMMON_H */
203
204 /*
205  * These are local overrides for various environment variables in Emacs.
206  * Please do not remove this and leave it at the end of the file, where
207  * Emacs will automagically detect them.
208  * ---------------------------------------------------------------------
209  * Local variables:
210  * mode: c
211  * indent-tabs-mode: t
212  * c-basic-offset: 4
213  * tab-width: 4
214  * End:
215  * vim:noexpandtab:sw=4:ts=4:
216  */