e6a02dec479d75a5b09969e3a0f9e2e992fe10ef
[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    $Id$
32
33 */
34
35
36 #ifndef _TYPECHECK_COMMON_H
37 #define _TYPECHECK_COMMON_H
38
39 #include "config.h"
40 #include "vm/types.h"
41 #include "vm/global.h"
42
43 #include <assert.h>
44
45 #include "vm/jit/jit.h"
46
47
48 /****************************************************************************/
49 /* DEBUG HELPERS                                                            */
50 /****************************************************************************/
51
52 #ifdef TYPECHECK_DEBUG
53 #define TYPECHECK_ASSERT(cond)  assert(cond)
54 #else
55 #define TYPECHECK_ASSERT(cond)
56 #endif
57
58 #ifdef TYPECHECK_VERBOSE_OPT
59 extern bool opt_typecheckverbose;
60 #define DOLOG(action)  do { if (opt_typecheckverbose) {action;} } while(0)
61 #else
62 #define DOLOG(action)
63 #endif
64
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))
73 #ifdef  TYPEINFO_DEBUG
74 #define LOGINFO(info)      DOLOG(do {typeinfo_print_short(stdout,(info)); LOGNL;} while(0))
75 #else
76 #define LOGINFO(info)
77 #define typevector_print(x,y,z)
78 #endif
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);)
86 #else
87 #define LOG(str)
88 #define LOG1(str,a)
89 #define LOG2(str,a,b)
90 #define LOG3(str,a,b,c)
91 #define LOGIF(cond,str)
92 #define LOGINFO(info)
93 #define LOGFLUSH
94 #define LOGNL
95 #define LOGSTR(str)
96 #define LOGSTR1(str,a)
97 #define LOGSTR2(str,a,b)
98 #define LOGSTR3(str,a,b,c)
99 #define LOGNAME(c)
100 #define LOGMETHOD(str,m)
101 #endif
102
103 #ifdef TYPECHECK_VERBOSE_IMPORTANT
104 #define LOGimp(str)     DOLOG(puts(str);LOGNL)
105 #define LOGimpSTR(str)  DOLOG(puts(str))
106 #else
107 #define LOGimp(str)
108 #define LOGimpSTR(str)
109 #endif
110
111 #if defined(TYPECHECK_VERBOSE) || defined(TYPECHECK_VERBOSE_IMPORTANT)
112 #include <stdio.h>
113 void typecheck_print_var(FILE *file, jitdata *jd, s4 index);
114 void typecheck_print_vararray(FILE *file, jitdata *jd, s4 *vars, int len);
115 #endif
116
117
118 /****************************************************************************/
119 /* STATISTICS                                                               */
120 /****************************************************************************/
121
122 #if defined(TYPECHECK_DEBUG) && !defined(TYPECHECK_NO_STATISTICS)
123 /*#define TYPECHECK_STATISTICS*/
124 #endif
125
126 #ifdef TYPECHECK_STATISTICS
127 #define STAT_ITERATIONS  10
128 #define STAT_BLOCKS      10
129 #define STAT_LOCALS      16
130
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];
141 extern int stat_ins;
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;
163
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) \
168         do {                                                                      \
169                 if ((val) < (limit)) (array)[val]++;  \
170                 else (array)[limit]++;                            \
171         } while (0)
172
173 void typecheck_print_statistics(FILE *file);
174
175 #else /* !defined(TYPECHECK_STATISTICS) */
176                                                    
177 #define TYPECHECK_COUNT(cnt)
178 #define TYPECHECK_MARK(var)
179 #define TYPECHECK_COUNTIF(cond,cnt)
180 #define TYPECHECK_COUNT_FREQ(array,val,limit)
181
182 #endif /* defined(TYPECHECK_STATISTICS) */
183
184
185 /****************************************************************************/
186 /* MACROS FOR THROWING EXCEPTIONS                                           */
187 /****************************************************************************/
188
189 #define TYPECHECK_VERIFYERROR_ret(m,msg,retval)                      \
190     do {                                                             \
191         exceptions_throw_verifyerror((m), (msg));                    \
192         return (retval);                                             \
193     } while (0)
194
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)
197
198
199 /****************************************************************************/
200 /* MISC MACROS                                                              */
201 /****************************************************************************/
202
203 #define COPYTYPE(source,dest)                                        \
204     {if (VAROP(source)->type == TYPE_ADR)                            \
205             TYPEINFO_COPY(VAROP(source)->typeinfo,VAROP(dest)->typeinfo);}
206
207
208 /****************************************************************************/
209 /* JSR VERIFICATION (stack-based verifier)                                  */
210 /****************************************************************************/
211
212 typedef struct typecheck_jsr_t typecheck_jsr_t;
213 typedef struct typecheck_jsr_caller_t typecheck_jsr_caller_t;
214
215 struct typecheck_jsr_caller_t {
216         typecheck_jsr_caller_t *next;                   /* next in linked list */
217         basicblock *callblock;             /* block containing the calling JSR */
218 };
219
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 */
231 };
232
233 /****************************************************************************/
234 /* VERIFIER STATE STRUCT                                                    */
235 /****************************************************************************/
236
237 /* verifier_state - This structure keeps the current state of the      */
238 /* bytecode verifier for passing it between verifier functions.        */
239
240 typedef struct verifier_state {
241     instruction *iptr;               /* pointer to current instruction */
242     basicblock *bptr;                /* pointer to current basic block */
243
244         methodinfo *m;                               /* the current method */
245         jitdata *jd;                         /* jitdata for current method */
246         codegendata *cd;                 /* codegendata for current method */
247
248         basicblock *basicblocks;
249         s4 basicblockcount;
250         
251         s4 numlocals;                         /* number of local variables */
252         s4 validlocals;                /* number of Java-accessible locals */
253         s4 *reverselocalmap;
254         
255         typedescriptor returntype;    /* return type of the current method */
256
257         s4 *savedindices;
258         s4 *savedinvars;                            /* saved invar pointer */
259
260         s4 exinvars;
261         
262     exception_entry **handlers;           /* active exception handlers */
263         
264     bool repeat;            /* if true, blocks are iterated over again */
265     bool initmethod;             /* true if this is an "<init>" method */
266
267 #ifdef TYPECHECK_STATISTICS
268         bool stat_maythrow;          /* at least one instruction may throw */
269 #endif
270
271         /* the following fields are used by the stackbased verifier only:  */
272
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 */
278
279         typecheck_jsr_t *topjsr;        /* most recently called subroutine */
280         typecheck_jsr_t **jsrinfos;      /* subroutine info for each block */
281 } verifier_state;
282
283 void typecheck_init_flags(verifier_state *state, s4 minflags);
284 void typecheck_reset_flags(verifier_state *state);
285
286 bool typecheck_copy_types(verifier_state *state,
287                                                   s4 *srcvars, s4 *dstvars, s4 n);
288
289 typecheck_result typecheck_merge_types(verifier_state *state,
290                                                                            s4 *srcvars,
291                                                                            s4 *dstvars,
292                                                                            s4 n);
293
294 typecheck_result typestate_merge(verifier_state *state,
295                                                  s4 *srcvars, varinfo *srclocals,
296                                                  s4 *dstvars, varinfo *dstlocals,
297                                                  s4 n);
298
299 bool typestate_reach(verifier_state *state,
300                                          basicblock *destblock,
301                                          s4 *srcvars, varinfo *srclocals, s4 n);
302
303 bool typecheck_init_locals(verifier_state *state, bool newthis);
304
305 #endif /* _TYPECHECK_COMMON_H */
306
307 /*
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  * ---------------------------------------------------------------------
312  * Local variables:
313  * mode: c
314  * indent-tabs-mode: t
315  * c-basic-offset: 4
316  * tab-width: 4
317  * End:
318  * vim:noexpandtab:sw=4:ts=4:
319  */