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