1 /* src/vm/jit/verify/typecheck-common.h - internal header for the type checker
3 Copyright (C) 1996-2005, 2006, 2008
4 CACAOVM - Verein zur Foerderung der freien virtuellen Maschine CACAO
6 This file is part of CACAO.
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.
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.
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
26 #ifndef _TYPECHECK_COMMON_H
27 #define _TYPECHECK_COMMON_H
31 #include "vm/global.h"
35 #include "vm/jit/jit.hpp"
37 #if defined(__cplusplus)
41 /****************************************************************************/
43 /****************************************************************************/
45 #ifdef TYPECHECK_DEBUG
46 #define TYPECHECK_ASSERT(cond) assert(cond)
48 #define TYPECHECK_ASSERT(cond)
51 #ifdef TYPECHECK_VERBOSE_OPT
52 extern bool opt_typecheckverbose;
53 #define DOLOG(action) do { if (opt_typecheckverbose) {action;} } while(0)
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))
67 #define LOGINFO(info) DOLOG(do {typeinfo_print_short(stdout,(info)); LOGNL;} while(0))
70 #define typevector_print(x,y,z)
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);)
83 #define LOG3(str,a,b,c)
84 #define LOGIF(cond,str)
89 #define LOGSTR1(str,a)
90 #define LOGSTR2(str,a,b)
91 #define LOGSTR3(str,a,b,c)
93 #define LOGMETHOD(str,m)
96 #ifdef TYPECHECK_VERBOSE_IMPORTANT
97 #define LOGimp(str) DOLOG(puts(str);LOGNL)
98 #define LOGimpSTR(str) DOLOG(puts(str))
101 #define LOGimpSTR(str)
104 #if defined(TYPECHECK_VERBOSE) || defined(TYPECHECK_VERBOSE_IMPORTANT)
106 void typecheck_print_var(FILE *file, jitdata *jd, s4 index);
107 void typecheck_print_vararray(FILE *file, jitdata *jd, s4 *vars, int len);
111 /****************************************************************************/
113 /****************************************************************************/
115 #if defined(TYPECHECK_DEBUG) && !defined(TYPECHECK_NO_STATISTICS)
116 /*#define TYPECHECK_STATISTICS*/
119 #ifdef TYPECHECK_STATISTICS
120 #define STAT_ITERATIONS 10
121 #define STAT_BLOCKS 10
122 #define STAT_LOCALS 16
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];
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;
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) \
162 if ((val) < (limit)) (array)[val]++; \
163 else (array)[limit]++; \
166 void typecheck_print_statistics(FILE *file);
168 #else /* !defined(TYPECHECK_STATISTICS) */
170 #define TYPECHECK_COUNT(cnt)
171 #define TYPECHECK_MARK(var)
172 #define TYPECHECK_COUNTIF(cond,cnt)
173 #define TYPECHECK_COUNT_FREQ(array,val,limit)
175 #endif /* defined(TYPECHECK_STATISTICS) */
178 /****************************************************************************/
179 /* MACROS FOR THROWING EXCEPTIONS */
180 /****************************************************************************/
182 #define TYPECHECK_VERIFYERROR_ret(m,msg,retval) \
184 exceptions_throw_verifyerror((m), (msg)); \
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)
192 /****************************************************************************/
194 /****************************************************************************/
196 #define COPYTYPE(source,dest) \
197 {if (VAROP(source)->type == TYPE_ADR) \
198 TYPEINFO_COPY(VAROP(source)->typeinfo,VAROP(dest)->typeinfo);}
201 /****************************************************************************/
202 /* JSR VERIFICATION (stack-based verifier) */
203 /****************************************************************************/
205 typedef struct typecheck_jsr_t typecheck_jsr_t;
206 typedef struct typecheck_jsr_caller_t typecheck_jsr_caller_t;
208 struct typecheck_jsr_caller_t {
209 typecheck_jsr_caller_t *next; /* next in linked list */
210 basicblock *callblock; /* block containing the calling JSR */
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 */
226 /****************************************************************************/
227 /* VERIFIER STATE STRUCT */
228 /****************************************************************************/
230 /* verifier_state - This structure keeps the current state of the */
231 /* bytecode verifier for passing it between verifier functions. */
233 typedef struct verifier_state {
234 instruction *iptr; /* pointer to current instruction */
235 basicblock *bptr; /* pointer to current basic block */
237 methodinfo *m; /* the current method */
238 jitdata *jd; /* jitdata for current method */
239 codegendata *cd; /* codegendata for current method */
241 basicblock *basicblocks;
244 s4 numlocals; /* number of local variables */
245 s4 validlocals; /* number of Java-accessible locals */
247 typedescriptor_t returntype; /* return type of the current method */
250 s4 *savedinvars; /* saved invar pointer */
254 exception_entry **handlers; /* active exception handlers */
256 bool repeat; /* if true, blocks are iterated over again */
257 bool initmethod; /* true if this is an "<init>" method */
259 #ifdef TYPECHECK_STATISTICS
260 bool stat_maythrow; /* at least one instruction may throw */
263 /* the following fields are used by the stackbased verifier only: */
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 */
271 typecheck_jsr_t *topjsr; /* most recently called subroutine */
272 typecheck_jsr_t **jsrinfos; /* subroutine info for each block */
275 void typecheck_init_flags(verifier_state *state, s4 minflags);
276 void typecheck_reset_flags(verifier_state *state);
278 bool typecheck_copy_types(verifier_state *state,
279 s4 *srcvars, s4 *dstvars, s4 n);
281 typecheck_result typecheck_merge_types(verifier_state *state,
286 typecheck_result typestate_merge(verifier_state *state,
287 s4 *srcvars, varinfo *srclocals,
288 s4 *dstvars, varinfo *dstlocals,
291 bool typestate_reach(verifier_state *state,
292 basicblock *destblock,
293 s4 *srcvars, varinfo *srclocals, s4 n);
295 bool typecheck_init_locals(verifier_state *state, bool newthis);
297 #if defined(__cplusplus)
301 #endif /* _TYPECHECK_COMMON_H */
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 * ---------------------------------------------------------------------
310 * indent-tabs-mode: t
314 * vim:noexpandtab:sw=4:ts=4: