1 /* src/vm/jit/verify/typecheck-common.h - internal header for the type checker
3 Copyright (C) 1996-2005, 2006 R. Grafl, A. Krall, C. Kruegel,
4 C. Oates, R. Obermaisser, M. Platter, M. Probst, S. Ring,
5 E. Steiner, C. Thalinger, D. Thuernbeck, P. Tomsich, C. Ullrich,
6 J. Wenninger, Institut f. Computersprachen - TU Wien
8 This file is part of CACAO.
10 This program is free software; you can redistribute it and/or
11 modify it under the terms of the GNU General Public License as
12 published by the Free Software Foundation; either version 2, or (at
13 your option) any later version.
15 This program is distributed in the hope that it will be useful, but
16 WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
25 Contact: cacao@cacaojvm.org
27 Authors: Edwin Steiner
36 #ifndef _TYPECHECK_COMMON_H
37 #define _TYPECHECK_COMMON_H
41 #include "vm/global.h"
45 #include "vm/jit/jit.h"
48 /****************************************************************************/
50 /****************************************************************************/
52 #ifdef TYPECHECK_DEBUG
53 #define TYPECHECK_ASSERT(cond) assert(cond)
55 #define TYPECHECK_ASSERT(cond)
58 #ifdef TYPECHECK_VERBOSE_OPT
59 extern bool opt_typecheckverbose;
60 #define DOLOG(action) do { if (opt_typecheckverbose) {action;} } while(0)
65 #ifdef TYPECHECK_VERBOSE
66 #define TYPECHECK_VERBOSE_IMPORTANT
67 #define LOGNL DOLOG(puts(""))
68 #define LOG(str) DOLOG(puts(str);)
69 #define LOG1(str,a) DOLOG(printf(str,a); LOGNL)
70 #define LOG2(str,a,b) DOLOG(printf(str,a,b); LOGNL)
71 #define LOG3(str,a,b,c) DOLOG(printf(str,a,b,c); LOGNL)
72 #define LOGIF(cond,str) DOLOG(do {if (cond) { puts(str); }} while(0))
74 #define LOGINFO(info) DOLOG(do {typeinfo_print_short(stdout,(info)); LOGNL;} while(0))
77 #define typevector_print(x,y,z)
79 #define LOGFLUSH DOLOG(fflush(stdout))
80 #define LOGSTR(str) DOLOG(printf("%s", str))
81 #define LOGSTR1(str,a) DOLOG(printf(str,a))
82 #define LOGSTR2(str,a,b) DOLOG(printf(str,a,b))
83 #define LOGSTR3(str,a,b,c) DOLOG(printf(str,a,b,c))
84 #define LOGNAME(c) DOLOG(class_classref_or_classinfo_print(c))
85 #define LOGMETHOD(str,m) DOLOG(printf("%s", str); method_println(m);)
90 #define LOG3(str,a,b,c)
91 #define LOGIF(cond,str)
96 #define LOGSTR1(str,a)
97 #define LOGSTR2(str,a,b)
98 #define LOGSTR3(str,a,b,c)
100 #define LOGMETHOD(str,m)
103 #ifdef TYPECHECK_VERBOSE_IMPORTANT
104 #define LOGimp(str) DOLOG(puts(str);LOGNL)
105 #define LOGimpSTR(str) DOLOG(puts(str))
108 #define LOGimpSTR(str)
111 #if defined(TYPECHECK_VERBOSE) || defined(TYPECHECK_VERBOSE_IMPORTANT)
113 void typecheck_print_var(FILE *file, jitdata *jd, s4 index);
114 void typecheck_print_vararray(FILE *file, jitdata *jd, s4 *vars, int len);
118 /****************************************************************************/
120 /****************************************************************************/
122 #if defined(TYPECHECK_DEBUG) && !defined(TYPECHECK_NO_STATISTICS)
123 /*#define TYPECHECK_STATISTICS*/
126 #ifdef TYPECHECK_STATISTICS
127 #define STAT_ITERATIONS 10
128 #define STAT_BLOCKS 10
129 #define STAT_LOCALS 16
131 extern int stat_typechecked;
132 extern int stat_methods_with_handlers;
133 extern int stat_methods_maythrow;
134 extern int stat_iterations[STAT_ITERATIONS+1];
135 extern int stat_reached;
136 extern int stat_copied;
137 extern int stat_merged;
138 extern int stat_merging_changed;
139 extern int stat_blocks[STAT_BLOCKS+1];
140 extern int stat_locals[STAT_LOCALS+1];
142 extern int stat_ins_maythrow;
143 extern int stat_ins_stack;
144 extern int stat_ins_field;
145 extern int stat_ins_field_unresolved;
146 extern int stat_ins_field_uninitialized;
147 extern int stat_ins_invoke;
148 extern int stat_ins_invoke_unresolved;
149 extern int stat_ins_primload;
150 extern int stat_ins_aload;
151 extern int stat_ins_builtin;
152 extern int stat_ins_builtin_gen;
153 extern int stat_ins_branch;
154 extern int stat_ins_switch;
155 extern int stat_ins_primitive_return;
156 extern int stat_ins_areturn;
157 extern int stat_ins_areturn_unresolved;
158 extern int stat_ins_athrow;
159 extern int stat_ins_athrow_unresolved;
160 extern int stat_ins_unchecked;
161 extern int stat_handlers_reached;
162 extern int stat_savedstack;
164 #define TYPECHECK_MARK(var) ((var) = true)
165 #define TYPECHECK_COUNT(cnt) (cnt)++
166 #define TYPECHECK_COUNTIF(cond,cnt) do{if(cond) (cnt)++;} while(0)
167 #define TYPECHECK_COUNT_FREQ(array,val,limit) \
169 if ((val) < (limit)) (array)[val]++; \
170 else (array)[limit]++; \
173 void typecheck_print_statistics(FILE *file);
175 #else /* !defined(TYPECHECK_STATISTICS) */
177 #define TYPECHECK_COUNT(cnt)
178 #define TYPECHECK_MARK(var)
179 #define TYPECHECK_COUNTIF(cond,cnt)
180 #define TYPECHECK_COUNT_FREQ(array,val,limit)
182 #endif /* defined(TYPECHECK_STATISTICS) */
185 /****************************************************************************/
186 /* MACROS FOR THROWING EXCEPTIONS */
187 /****************************************************************************/
189 #define TYPECHECK_VERIFYERROR_ret(m,msg,retval) \
191 exceptions_throw_verifyerror((m), (msg)); \
195 #define TYPECHECK_VERIFYERROR_main(msg) TYPECHECK_VERIFYERROR_ret(state.m,(msg),NULL)
196 #define TYPECHECK_VERIFYERROR_bool(msg) TYPECHECK_VERIFYERROR_ret(state->m,(msg),false)
199 /****************************************************************************/
201 /****************************************************************************/
203 #define COPYTYPE(source,dest) \
204 {if (VAROP(source)->type == TYPE_ADR) \
205 TYPEINFO_COPY(VAROP(source)->typeinfo,VAROP(dest)->typeinfo);}
208 /****************************************************************************/
209 /* JSR VERIFICATION (stack-based verifier) */
210 /****************************************************************************/
212 typedef struct typecheck_jsr_t typecheck_jsr_t;
213 typedef struct typecheck_jsr_caller_t typecheck_jsr_caller_t;
215 struct typecheck_jsr_caller_t {
216 typecheck_jsr_caller_t *next; /* next in linked list */
217 basicblock *callblock; /* block containing the calling JSR */
220 struct typecheck_jsr_t {
221 typecheck_jsr_t *next; /* next (lower) in the call chain */
222 basicblock *start; /* for debugging */
223 typecheck_jsr_caller_t *callers; /* list of callers (blocks with JSR) */
224 basicblock *retblock; /* block with the RET for this sub */
225 bool active; /* true if this sub is currently active */
226 char *blockflags; /* saved block flags when JSR was traversed */
227 char *usedlocals; /* != 0 for each local used in this sub */
228 typedescriptor *retlocals; /* locals on the RET edge */
229 typedescriptor *retstack; /* stack on the RET edge */
230 s4 retdepth; /* stack depth on the RET edge */
233 /****************************************************************************/
234 /* VERIFIER STATE STRUCT */
235 /****************************************************************************/
237 /* verifier_state - This structure keeps the current state of the */
238 /* bytecode verifier for passing it between verifier functions. */
240 typedef struct verifier_state {
241 instruction *iptr; /* pointer to current instruction */
242 basicblock *bptr; /* pointer to current basic block */
244 methodinfo *m; /* the current method */
245 jitdata *jd; /* jitdata for current method */
246 codegendata *cd; /* codegendata for current method */
248 basicblock *basicblocks;
251 s4 numlocals; /* number of local variables */
252 s4 validlocals; /* number of Java-accessible locals */
255 typedescriptor returntype; /* return type of the current method */
258 s4 *savedinvars; /* saved invar pointer */
262 exception_entry **handlers; /* active exception handlers */
264 bool repeat; /* if true, blocks are iterated over again */
265 bool initmethod; /* true if this is an "<init>" method */
267 #ifdef TYPECHECK_STATISTICS
268 bool stat_maythrow; /* at least one instruction may throw */
271 /* the following fields are used by the stackbased verifier only: */
273 typedescriptor *locals; /* current local variables */
274 typedescriptor *startlocals; /* locals at the start of each block */
275 typedescriptor *startstack; /* stack at the start of each block */
276 s4 *indepth; /* stack depth at --''-- */
277 typedescriptor *stackceiling; /* upper edge of verifier stack */
279 typecheck_jsr_t *topjsr; /* most recently called subroutine */
280 typecheck_jsr_t **jsrinfos; /* subroutine info for each block */
283 void typecheck_init_flags(verifier_state *state, s4 minflags);
284 void typecheck_reset_flags(verifier_state *state);
286 bool typecheck_copy_types(verifier_state *state,
287 s4 *srcvars, s4 *dstvars, s4 n);
289 typecheck_result typecheck_merge_types(verifier_state *state,
294 typecheck_result typestate_merge(verifier_state *state,
295 s4 *srcvars, varinfo *srclocals,
296 s4 *dstvars, varinfo *dstlocals,
299 bool typestate_reach(verifier_state *state,
300 basicblock *destblock,
301 s4 *srcvars, varinfo *srclocals, s4 n);
303 bool typecheck_init_locals(verifier_state *state, bool newthis);
305 #endif /* _TYPECHECK_COMMON_H */
308 * These are local overrides for various environment variables in Emacs.
309 * Please do not remove this and leave it at the end of the file, where
310 * Emacs will automagically detect them.
311 * ---------------------------------------------------------------------
314 * indent-tabs-mode: t
318 * vim:noexpandtab:sw=4:ts=4: