1 /* src/vm/jit/verify/typecheck-typeinferer.c - type inference pass
3 Copyright (C) 1996-2005, 2006, 2007 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
29 #include "vm/global.h"
34 #include "mm/memory.h"
36 #include "native/native.h"
38 #include "toolbox/logging.h"
41 #include "vm/access.h"
42 #include "vm/builtin.h"
43 #include "vm/exceptions.h"
44 #include "vm/primitive.h"
45 #include "vm/resolve.h"
48 #include "vm/jit/jit.h"
49 #include "vm/jit/show.h"
50 #include "vm/jit/parse.h"
52 #include "vm/jit/verify/typecheck-typeinferer.h"
54 #include "vmcore/loader.h"
55 #include "vmcore/options.h"
57 #define TYPECHECK_NO_STATISTICS
58 #include <typecheck-common.h>
61 /* macros used by the generated code ******************************************/
63 #define EXCEPTION do { return false; } while (0)
64 #define VERIFY_ERROR(msg) assert(false)
66 #define CHECK_LOCAL_TYPE(index, t) \
67 assert(jd->var[(index)].type == (t));
69 #define STORE_LOCAL(t, index) \
71 typevector_store(jd->var, (index), (t), NULL); \
74 #define STORE_LOCAL_2_WORD(t, index) \
76 typevector_store(jd->var, (index), (t), NULL); \
79 #define REACH_BLOCK(target) \
81 if (!typestate_reach(state, (target), \
82 state->bptr->outvars, jd->var, \
83 state->bptr->outdepth)) \
87 #define REACH(target) REACH_BLOCK((target).block)
89 #define TYPECHECK_INT(v) assert(jd->var[(v)].type == TYPE_INT)
90 #define TYPECHECK_ADR(v) assert(jd->var[(v)].type == TYPE_ADR)
93 /* handle_fieldaccess **********************************************************
95 Verify an ICMD_{GET,PUT}{STATIC,FIELD}(CONST)?
98 state............the current state of the verifier
101 true.............successful verification,
102 false............an exception has been thrown.
104 *******************************************************************************/
107 handle_fieldaccess(verifier_state *state,
115 #define TYPECHECK_TYPEINFERER
116 #include <typecheck-fields.inc>
117 #undef TYPECHECK_TYPEINFERER
123 /* handle_invocation ***********************************************************
125 Verify an ICMD_INVOKE* instruction.
128 state............the current state of the verifier
131 true.............successful verification,
132 false............an exception has been thrown.
134 *******************************************************************************/
137 handle_invocation(verifier_state *state)
140 varinfo *dv; /* output variable of current instruction */
143 dv = VAROP(state->iptr->dst);
145 #define TYPECHECK_TYPEINFERER
146 #define OP1 VAR(state->iptr->sx.s23.s2.args[0])
147 #include <typecheck-invoke.inc>
149 #undef TYPECHECK_TYPEINFERER
155 /* handle_builtin **************************************************************
157 Verify the call of a builtin method.
160 state............the current state of the verifier
163 true.............successful verification,
164 false............an exception has been thrown.
166 *******************************************************************************/
169 handle_builtin(verifier_state *state)
172 varinfo *dv; /* output variable of current instruction */
175 dv = VAROP(state->iptr->dst);
177 #define TYPECHECK_TYPEINFERER
178 #define OP1 state->iptr->sx.s23.s2.args[0]
179 #include <typecheck-builtins.inc>
181 #undef TYPECHECK_TYPEINFERER
186 /* handle_multianewarray *******************************************************
188 Verify a MULTIANEWARRAY instruction.
191 state............the current state of the verifier
194 true.............successful verification,
195 false............an exception has been thrown.
197 *******************************************************************************/
200 handle_multianewarray(verifier_state *state)
203 varinfo *dv; /* output variable of current instruction */
206 dv = VAROP(state->iptr->dst);
208 #define TYPECHECK_TYPEINFERER
209 #include <typecheck-multianewarray.inc>
210 #undef TYPECHECK_TYPEINFERER
216 /* handle_basic_block **********************************************************
218 Perform bytecode verification of a basic block.
221 state............the current state of the verifier
224 true.............successful verification,
225 false............an exception has been thrown.
227 *******************************************************************************/
230 handle_basic_block(verifier_state *state)
232 int opcode; /* current opcode */
233 int len; /* for counting instructions, etc. */
234 bool superblockend; /* true if no fallthrough to next block */
235 instruction *iptr; /* the current instruction */
236 basicblock *tbptr; /* temporary for target block */
237 bool maythrow; /* true if this instruction may throw */
239 branch_target_t *table;
240 lookup_target_t *lookup;
241 jitdata *jd = state->jd;
244 LOGSTR1("\n---- BLOCK %04d ------------------------------------------------\n",state->bptr->nr);
246 DOLOG(show_basicblock(jd, state->bptr, SHOW_STACK));
248 superblockend = false;
249 state->bptr->flags = BBFINISHED;
251 /* prevent compiler warnings */
254 /* determine the active exception handlers for this block */
255 /* XXX could use a faster algorithm with sorted lists or */
258 for (ex = state->jd->exceptiontable; ex ; ex = ex->down) {
259 if ((ex->start->nr <= state->bptr->nr) && (ex->end->nr > state->bptr->nr)) {
260 LOG1("active handler L%03d", ex->handler->nr);
261 state->handlers[len++] = ex;
264 state->handlers[len] = NULL;
266 /* init variable types at the start of this block */
267 typevector_copy_inplace(state->bptr->inlocals, jd->var, state->numlocals);
269 DOLOG(typecheck_print_vararray(stdout, jd, state->bptr->invars,
270 state->bptr->indepth));
271 DOLOG(typevector_print(stdout, jd->var, state->numlocals));
274 /* loop over the instructions */
275 len = state->bptr->icount;
276 state->iptr = state->bptr->iinstr;
278 TYPECHECK_COUNT(stat_ins);
282 DOLOG(typevector_print(stdout, jd->var, state->numlocals));
284 DOLOG(show_icmd(jd, state->iptr, false, SHOW_STACK)); LOGNL; LOGFLUSH;
291 /* include generated code for ICMDs verification */
293 #define TYPECHECK_TYPEINFERER
295 #define METHOD (state->m)
297 #define BPTR (state->bptr)
298 #include <typecheck-typeinferer-gen.inc>
303 #undef TYPECHECK_TYPEINFERER
306 vm_abort("missing ICMD in type inferer: %d\n", opcode);
309 /* reach exception handlers for this instruction */
312 TYPECHECK_COUNT(stat_ins_maythrow);
313 TYPECHECK_MARK(state->stat_maythrow);
314 LOG("reaching exception handlers");
316 while (state->handlers[i]) {
317 TYPECHECK_COUNT(stat_handlers_reached);
318 if (state->handlers[i]->catchtype.any)
319 VAR(state->exinvars)->typeinfo.typeclass = state->handlers[i]->catchtype;
321 VAR(state->exinvars)->typeinfo.typeclass.cls = class_java_lang_Throwable;
322 if (!typestate_reach(state,
323 state->handlers[i]->handler,
324 &(state->exinvars), jd->var, 1))
330 LOG("\t\tnext instruction");
332 } /* while instructions */
334 LOG("instructions done");
336 DOLOG(typecheck_print_vararray(stdout, jd, state->bptr->outvars,
337 state->bptr->outdepth));
338 DOLOG(typevector_print(stdout, jd->var, state->numlocals));
341 /* propagate stack and variables to the following block */
342 if (!superblockend) {
343 LOG("reaching following block");
344 tbptr = state->bptr->next;
345 while (tbptr->flags == BBDELETED) {
348 if (!typestate_reach(state,tbptr,state->bptr->outvars, jd->var,
349 state->bptr->outdepth))
357 bool typecheck_infer_types(jitdata *jd)
361 varinfo *savedlocals;
362 verifier_state state; /* current state of the verifier */
364 /* get required compiler data */
369 /* some logging on entry */
372 LOGSTR("\n==============================================================================\n");
373 DOLOG( show_method(jd, SHOW_STACK) );
374 LOGSTR("\n==============================================================================\n");
375 LOGMETHOD("Entering type inference: ",cd->method);
377 /* initialize the verifier state */
382 state.basicblockcount = jd->basicblockcount;
383 state.basicblocks = jd->basicblocks;
384 state.savedindices = NULL;
385 state.savedinvars = NULL;
387 /* check that the basicblock numbers are valid */
390 jit_check_basicblock_numbers(jd);
393 /* check if this method is an instance initializer method */
395 state.initmethod = (state.m->name == utf_init);
397 /* initialize the basic block flags for the following CFG traversal */
399 typecheck_init_flags(&state, BBFINISHED);
401 /* number of local variables */
403 /* In <init> methods we use an extra local variable to indicate whether */
404 /* the 'this' reference has been initialized. */
405 /* TYPE_VOID...means 'this' has not been initialized, */
406 /* TYPE_INT....means 'this' has been initialized. */
408 state.numlocals = state.jd->localcount;
409 state.validlocals = state.numlocals;
410 if (state.initmethod)
411 state.numlocals++; /* VERIFIER_EXTRA_LOCALS */
413 /* allocate the buffer of active exception handlers */
415 state.handlers = DMNEW(exception_entry*, state.jd->exceptiontablelength + 1);
417 /* save local variables */
419 savedlocals = DMNEW(varinfo, state.numlocals);
420 MCOPY(savedlocals, jd->var, varinfo, state.numlocals);
422 /* initialized local variables of first block */
424 if (!typecheck_init_locals(&state, false))
427 /* initialize invars of exception handlers */
429 state.exinvars = state.numlocals;
430 VAR(state.exinvars)->type = TYPE_ADR;
431 typeinfo_init_classinfo(&(VAR(state.exinvars)->typeinfo),
432 class_java_lang_Throwable); /* changed later */
434 LOG("Exception handler stacks set.\n");
436 /* loop while there are still blocks to be checked */
438 TYPECHECK_COUNT(count_iterations);
440 state.repeat = false;
442 state.bptr = state.basicblocks;
444 for (; state.bptr; state.bptr = state.bptr->next) {
445 LOGSTR1("---- BLOCK %04d, ",state.bptr->nr);
446 LOGSTR1("blockflags: %d\n",state.bptr->flags);
449 /* verify reached block */
450 if (state.bptr->flags == BBTYPECHECK_REACHED) {
451 if (!handle_basic_block(&state))
456 LOGIF(state.repeat,"state.repeat == true");
457 } while (state.repeat);
461 /* reset the flags of blocks we haven't reached */
463 typecheck_reset_flags(&state);
467 MCOPY(jd->var, savedlocals, varinfo, state.numlocals);
469 /* everything's ok */
471 LOGimp("exiting type inference");
476 * These are local overrides for various environment variables in Emacs.
477 * Please do not remove this and leave it at the end of the file, where
478 * Emacs will automagically detect them.
479 * ---------------------------------------------------------------------
482 * indent-tabs-mode: t
486 * vim:noexpandtab:sw=4:ts=4: