Merge from subtype.
[cacao.git] / src / vm / jit / verify / typecheck-common.hpp
1 /* src/vm/jit/verify/typecheck-common.h - internal header for the type checker
2
3    Copyright (C) 1996-2005, 2006, 2008
4    CACAOVM - Verein zur Foerderung der freien virtuellen Maschine CACAO
5
6    This file is part of CACAO.
7
8    This program is free software; you can redistribute it and/or
9    modify it under the terms of the GNU General Public License as
10    published by the Free Software Foundation; either version 2, or (at
11    your option) any later version.
12
13    This program is distributed in the hope that it will be useful, but
14    WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16    General Public License for more details.
17
18    You should have received a copy of the GNU General Public License
19    along with this program; if not, write to the Free Software
20    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21    02110-1301, USA.
22
23 */
24
25
26 #ifndef _TYPECHECK_COMMON_H
27 #define _TYPECHECK_COMMON_H
28
29 #include "config.h"
30 #include "vm/types.h"
31 #include "vm/global.h"
32
33 #include <assert.h>
34
35 #include "vm/jit/jit.hpp"
36
37 #if defined(__cplusplus)
38 extern "C" {
39 #endif
40
41 /****************************************************************************/
42 /* DEBUG HELPERS                                                            */
43 /****************************************************************************/
44
45 #ifdef TYPECHECK_DEBUG
46 #define TYPECHECK_ASSERT(cond)  assert(cond)
47 #else
48 #define TYPECHECK_ASSERT(cond)
49 #endif
50
51 #ifdef TYPECHECK_VERBOSE_OPT
52 extern bool opt_typecheckverbose;
53 #define DOLOG(action)  do { if (opt_typecheckverbose) {action;} } while(0)
54 #else
55 #define DOLOG(action)
56 #endif
57
58 #ifdef TYPECHECK_VERBOSE
59 #define TYPECHECK_VERBOSE_IMPORTANT
60 #define LOGNL              DOLOG(puts(""))
61 #define LOG(str)           DOLOG(puts(str);)
62 #define LOG1(str,a)        DOLOG(printf(str,a); LOGNL)
63 #define LOG2(str,a,b)      DOLOG(printf(str,a,b); LOGNL)
64 #define LOG3(str,a,b,c)    DOLOG(printf(str,a,b,c); LOGNL)
65 #define LOGIF(cond,str)    DOLOG(do {if (cond) { puts(str); }} while(0))
66 #ifdef  TYPEINFO_DEBUG
67 #define LOGINFO(info)      DOLOG(do {typeinfo_print_short(stdout,(info)); LOGNL;} while(0))
68 #else
69 #define LOGINFO(info)
70 #define typevector_print(x,y,z)
71 #endif
72 #define LOGFLUSH           DOLOG(fflush(stdout))
73 #define LOGSTR(str)        DOLOG(printf("%s", str))
74 #define LOGSTR1(str,a)     DOLOG(printf(str,a))
75 #define LOGSTR2(str,a,b)   DOLOG(printf(str,a,b))
76 #define LOGSTR3(str,a,b,c) DOLOG(printf(str,a,b,c))
77 #define LOGNAME(c)         DOLOG(class_classref_or_classinfo_print(c))
78 #define LOGMETHOD(str,m)   DOLOG(printf("%s", str); method_println(m);)
79 #else
80 #define LOG(str)
81 #define LOG1(str,a)
82 #define LOG2(str,a,b)
83 #define LOG3(str,a,b,c)
84 #define LOGIF(cond,str)
85 #define LOGINFO(info)
86 #define LOGFLUSH
87 #define LOGNL
88 #define LOGSTR(str)
89 #define LOGSTR1(str,a)
90 #define LOGSTR2(str,a,b)
91 #define LOGSTR3(str,a,b,c)
92 #define LOGNAME(c)
93 #define LOGMETHOD(str,m)
94 #endif
95
96 #ifdef TYPECHECK_VERBOSE_IMPORTANT
97 #define LOGimp(str)     DOLOG(puts(str);LOGNL)
98 #define LOGimpSTR(str)  DOLOG(puts(str))
99 #else
100 #define LOGimp(str)
101 #define LOGimpSTR(str)
102 #endif
103
104 #if defined(TYPECHECK_VERBOSE) || defined(TYPECHECK_VERBOSE_IMPORTANT)
105 #include <stdio.h>
106 void typecheck_print_var(FILE *file, jitdata *jd, s4 index);
107 void typecheck_print_vararray(FILE *file, jitdata *jd, s4 *vars, int len);
108 #endif
109
110
111 /****************************************************************************/
112 /* STATISTICS                                                               */
113 /****************************************************************************/
114
115 #if defined(TYPECHECK_DEBUG) && !defined(TYPECHECK_NO_STATISTICS)
116 /*#define TYPECHECK_STATISTICS*/
117 #endif
118
119 #ifdef TYPECHECK_STATISTICS
120 #define STAT_ITERATIONS  10
121 #define STAT_BLOCKS      10
122 #define STAT_LOCALS      16
123
124 extern int stat_typechecked;
125 extern int stat_methods_with_handlers;
126 extern int stat_methods_maythrow;
127 extern int stat_iterations[STAT_ITERATIONS+1];
128 extern int stat_reached;
129 extern int stat_copied;
130 extern int stat_merged;
131 extern int stat_merging_changed;
132 extern int stat_blocks[STAT_BLOCKS+1];
133 extern int stat_locals[STAT_LOCALS+1];
134 extern int stat_ins;
135 extern int stat_ins_maythrow;
136 extern int stat_ins_stack;
137 extern int stat_ins_field;
138 extern int stat_ins_field_unresolved;
139 extern int stat_ins_field_uninitialized;
140 extern int stat_ins_invoke;
141 extern int stat_ins_invoke_unresolved;
142 extern int stat_ins_primload;
143 extern int stat_ins_aload;
144 extern int stat_ins_builtin;
145 extern int stat_ins_builtin_gen;
146 extern int stat_ins_branch;
147 extern int stat_ins_switch;
148 extern int stat_ins_primitive_return;
149 extern int stat_ins_areturn;
150 extern int stat_ins_areturn_unresolved;
151 extern int stat_ins_athrow;
152 extern int stat_ins_athrow_unresolved;
153 extern int stat_ins_unchecked;
154 extern int stat_handlers_reached;
155 extern int stat_savedstack;
156
157 #define TYPECHECK_MARK(var)   ((var) = true)
158 #define TYPECHECK_COUNT(cnt)  (cnt)++
159 #define TYPECHECK_COUNTIF(cond,cnt)  do{if(cond) (cnt)++;} while(0)
160 #define TYPECHECK_COUNT_FREQ(array,val,limit) \
161         do {                                                                      \
162                 if ((val) < (limit)) (array)[val]++;  \
163                 else (array)[limit]++;                            \
164         } while (0)
165
166 void typecheck_print_statistics(FILE *file);
167
168 #else /* !defined(TYPECHECK_STATISTICS) */
169                                                    
170 #define TYPECHECK_COUNT(cnt)
171 #define TYPECHECK_MARK(var)
172 #define TYPECHECK_COUNTIF(cond,cnt)
173 #define TYPECHECK_COUNT_FREQ(array,val,limit)
174
175 #endif /* defined(TYPECHECK_STATISTICS) */
176
177
178 /****************************************************************************/
179 /* MACROS FOR THROWING EXCEPTIONS                                           */
180 /****************************************************************************/
181
182 #define TYPECHECK_VERIFYERROR_ret(m,msg,retval)                      \
183     do {                                                             \
184         exceptions_throw_verifyerror((m), (msg));                    \
185         return (retval);                                             \
186     } while (0)
187
188 #define TYPECHECK_VERIFYERROR_main(msg)  TYPECHECK_VERIFYERROR_ret(state.m,(msg),NULL)
189 #define TYPECHECK_VERIFYERROR_bool(msg)  TYPECHECK_VERIFYERROR_ret(state->m,(msg),false)
190
191
192 /****************************************************************************/
193 /* MISC MACROS                                                              */
194 /****************************************************************************/
195
196 #define COPYTYPE(source,dest)                                        \
197     {if (VAROP(source)->type == TYPE_ADR)                            \
198             TYPEINFO_COPY(VAROP(source)->typeinfo,VAROP(dest)->typeinfo);}
199
200
201 /****************************************************************************/
202 /* JSR VERIFICATION (stack-based verifier)                                  */
203 /****************************************************************************/
204
205 typedef struct typecheck_jsr_t typecheck_jsr_t;
206 typedef struct typecheck_jsr_caller_t typecheck_jsr_caller_t;
207
208 struct typecheck_jsr_caller_t {
209         typecheck_jsr_caller_t *next;                   /* next in linked list */
210         basicblock *callblock;             /* block containing the calling JSR */
211 };
212
213 struct typecheck_jsr_t {
214         typecheck_jsr_t *next;               /* next (lower) in the call chain */
215         basicblock  *start;                                   /* for debugging */
216         typecheck_jsr_caller_t *callers;  /* list of callers (blocks with JSR) */
217         basicblock  *retblock;              /* block with the RET for this sub */
218         bool         active;           /* true if this sub is currently active */
219         char        *blockflags;   /* saved block flags when JSR was traversed */
220         char        *usedlocals;       /* != 0 for each local used in this sub */
221         typedescriptor_t *retlocals;                 /* locals on the RET edge */
222         typedescriptor_t *retstack;                   /* stack on the RET edge */
223         s4              retdepth;               /* stack depth on the RET edge */
224 };
225
226 /****************************************************************************/
227 /* VERIFIER STATE STRUCT                                                    */
228 /****************************************************************************/
229
230 /* verifier_state - This structure keeps the current state of the      */
231 /* bytecode verifier for passing it between verifier functions.        */
232
233 typedef struct verifier_state {
234     instruction *iptr;               /* pointer to current instruction */
235     basicblock *bptr;                /* pointer to current basic block */
236
237         methodinfo *m;                               /* the current method */
238         jitdata *jd;                         /* jitdata for current method */
239         codegendata *cd;                 /* codegendata for current method */
240
241         basicblock *basicblocks;
242         s4 basicblockcount;
243         
244         s4 numlocals;                         /* number of local variables */
245         s4 validlocals;                /* number of Java-accessible locals */
246         
247         typedescriptor_t returntype;  /* return type of the current method */
248
249         s4 *savedindices;
250         s4 *savedinvars;                            /* saved invar pointer */
251
252         s4 exinvars;
253         
254     exception_entry **handlers;           /* active exception handlers */
255         
256     bool repeat;            /* if true, blocks are iterated over again */
257     bool initmethod;             /* true if this is an "<init>" method */
258
259 #ifdef TYPECHECK_STATISTICS
260         bool stat_maythrow;          /* at least one instruction may throw */
261 #endif
262
263         /* the following fields are used by the stackbased verifier only:  */
264
265         typedescriptor_t *locals;               /* current local variables */
266         typedescriptor_t *startlocals;/* locals at the start of each block */
267         typedescriptor_t *startstack;  /* stack at the start of each block */
268         s4               *indepth;                /* stack depth at --''-- */
269         typedescriptor_t *stackceiling;    /* upper edge of verifier stack */
270
271         typecheck_jsr_t *topjsr;        /* most recently called subroutine */
272         typecheck_jsr_t **jsrinfos;      /* subroutine info for each block */
273 } verifier_state;
274
275 void typecheck_init_flags(verifier_state *state, s4 minflags);
276 void typecheck_reset_flags(verifier_state *state);
277
278 bool typecheck_copy_types(verifier_state *state,
279                                                   s4 *srcvars, s4 *dstvars, s4 n);
280
281 typecheck_result typecheck_merge_types(verifier_state *state,
282                                                                            s4 *srcvars,
283                                                                            s4 *dstvars,
284                                                                            s4 n);
285
286 typecheck_result typestate_merge(verifier_state *state,
287                                                  s4 *srcvars, varinfo *srclocals,
288                                                  s4 *dstvars, varinfo *dstlocals,
289                                                  s4 n);
290
291 bool typestate_reach(verifier_state *state,
292                                          basicblock *destblock,
293                                          s4 *srcvars, varinfo *srclocals, s4 n);
294
295 bool typecheck_init_locals(verifier_state *state, bool newthis);
296
297 #if defined(__cplusplus)
298 }
299 #endif
300
301 #endif /* _TYPECHECK_COMMON_H */
302
303 /*
304  * These are local overrides for various environment variables in Emacs.
305  * Please do not remove this and leave it at the end of the file, where
306  * Emacs will automagically detect them.
307  * ---------------------------------------------------------------------
308  * Local variables:
309  * mode: c++
310  * indent-tabs-mode: t
311  * c-basic-offset: 4
312  * tab-width: 4
313  * End:
314  * vim:noexpandtab:sw=4:ts=4:
315  */