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