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
31 #include "vm/global.h"
36 #include "mm/memory.h"
38 #include "native/native.h"
40 #include "toolbox/logging.h"
43 #include "vm/access.h"
44 #include "vm/builtin.h"
45 #include "vm/exceptions.h"
46 #include "vm/primitive.h"
47 #include "vm/resolve.h"
50 #include "vm/jit/jit.h"
51 #include "vm/jit/show.h"
52 #include "vm/jit/parse.h"
54 #include "vm/jit/verify/typecheck-typeinferer.h"
56 #include "vmcore/loader.h"
57 #include "vmcore/options.h"
59 #define TYPECHECK_NO_STATISTICS
60 #include <typecheck-common.h>
63 /* macros used by the generated code ******************************************/
65 #define EXCEPTION do { return false; } while (0)
66 #define VERIFY_ERROR(msg) assert(false)
68 #define CHECK_LOCAL_TYPE(index, t) \
69 assert(jd->var[(index)].type == (t));
71 #define STORE_LOCAL(t, index) \
73 typevector_store(jd->var, (index), (t), NULL); \
76 #define STORE_LOCAL_2_WORD(t, index) \
78 typevector_store(jd->var, (index), (t), NULL); \
81 #define REACH_BLOCK(target) \
83 if (!typestate_reach(state, (target), \
84 state->bptr->outvars, jd->var, \
85 state->bptr->outdepth)) \
89 #define REACH(target) REACH_BLOCK((target).block)
91 #define TYPECHECK_INT(v) assert(jd->var[(v)].type == TYPE_INT)
92 #define TYPECHECK_ADR(v) assert(jd->var[(v)].type == TYPE_ADR)
95 /* handle_fieldaccess **********************************************************
97 Verify an ICMD_{GET,PUT}{STATIC,FIELD}(CONST)?
100 state............the current state of the verifier
103 true.............successful verification,
104 false............an exception has been thrown.
106 *******************************************************************************/
109 handle_fieldaccess(verifier_state *state,
117 #define TYPECHECK_TYPEINFERER
118 #include <typecheck-fields.inc>
119 #undef TYPECHECK_TYPEINFERER
125 /* handle_invocation ***********************************************************
127 Verify an ICMD_INVOKE* instruction.
130 state............the current state of the verifier
133 true.............successful verification,
134 false............an exception has been thrown.
136 *******************************************************************************/
139 handle_invocation(verifier_state *state)
142 varinfo *dv; /* output variable of current instruction */
145 dv = VAROP(state->iptr->dst);
147 #define TYPECHECK_TYPEINFERER
148 #define OP1 VAR(state->iptr->sx.s23.s2.args[0])
149 #include <typecheck-invoke.inc>
151 #undef TYPECHECK_TYPEINFERER
157 /* handle_builtin **************************************************************
159 Verify the call of a builtin method.
162 state............the current state of the verifier
165 true.............successful verification,
166 false............an exception has been thrown.
168 *******************************************************************************/
171 handle_builtin(verifier_state *state)
174 varinfo *dv; /* output variable of current instruction */
177 dv = VAROP(state->iptr->dst);
179 #define TYPECHECK_TYPEINFERER
180 #define OP1 state->iptr->sx.s23.s2.args[0]
181 #include <typecheck-builtins.inc>
183 #undef TYPECHECK_TYPEINFERER
188 /* handle_multianewarray *******************************************************
190 Verify a MULTIANEWARRAY instruction.
193 state............the current state of the verifier
196 true.............successful verification,
197 false............an exception has been thrown.
199 *******************************************************************************/
202 handle_multianewarray(verifier_state *state)
205 varinfo *dv; /* output variable of current instruction */
208 dv = VAROP(state->iptr->dst);
210 #define TYPECHECK_TYPEINFERER
211 #include <typecheck-multianewarray.inc>
212 #undef TYPECHECK_TYPEINFERER
218 /* handle_basic_block **********************************************************
220 Perform bytecode verification of a basic block.
223 state............the current state of the verifier
226 true.............successful verification,
227 false............an exception has been thrown.
229 *******************************************************************************/
232 handle_basic_block(verifier_state *state)
234 int opcode; /* current opcode */
235 int len; /* for counting instructions, etc. */
236 bool superblockend; /* true if no fallthrough to next block */
237 instruction *iptr; /* the current instruction */
238 basicblock *tbptr; /* temporary for target block */
239 bool maythrow; /* true if this instruction may throw */
241 branch_target_t *table;
242 lookup_target_t *lookup;
243 jitdata *jd = state->jd;
246 LOGSTR1("\n---- BLOCK %04d ------------------------------------------------\n",state->bptr->nr);
248 DOLOG(show_basicblock(jd, state->bptr, SHOW_STACK));
250 superblockend = false;
251 state->bptr->flags = BBFINISHED;
253 /* prevent compiler warnings */
256 /* determine the active exception handlers for this block */
257 /* XXX could use a faster algorithm with sorted lists or */
260 for (ex = state->jd->exceptiontable; ex ; ex = ex->down) {
261 if ((ex->start->nr <= state->bptr->nr) && (ex->end->nr > state->bptr->nr)) {
262 LOG1("active handler L%03d", ex->handler->nr);
263 state->handlers[len++] = ex;
266 state->handlers[len] = NULL;
268 /* init variable types at the start of this block */
269 typevector_copy_inplace(state->bptr->inlocals, jd->var, state->numlocals);
271 DOLOG(typecheck_print_vararray(stdout, jd, state->bptr->invars,
272 state->bptr->indepth));
273 DOLOG(typevector_print(stdout, jd->var, state->numlocals));
276 /* loop over the instructions */
277 len = state->bptr->icount;
278 state->iptr = state->bptr->iinstr;
280 TYPECHECK_COUNT(stat_ins);
284 DOLOG(typevector_print(stdout, jd->var, state->numlocals));
286 DOLOG(show_icmd(jd, state->iptr, false, SHOW_STACK)); LOGNL; LOGFLUSH;
293 /* include generated code for ICMDs verification */
295 #define TYPECHECK_TYPEINFERER
297 #define METHOD (state->m)
299 #define BPTR (state->bptr)
300 #include <typecheck-typeinferer-gen.inc>
305 #undef TYPECHECK_TYPEINFERER
308 vm_abort("missing ICMD in type inferer: %d\n", opcode);
311 /* reach exception handlers for this instruction */
314 TYPECHECK_COUNT(stat_ins_maythrow);
315 TYPECHECK_MARK(state->stat_maythrow);
316 LOG("reaching exception handlers");
318 while (state->handlers[i]) {
319 TYPECHECK_COUNT(stat_handlers_reached);
320 if (state->handlers[i]->catchtype.any)
321 VAR(state->exinvars)->typeinfo.typeclass = state->handlers[i]->catchtype;
323 VAR(state->exinvars)->typeinfo.typeclass.cls = class_java_lang_Throwable;
324 if (!typestate_reach(state,
325 state->handlers[i]->handler,
326 &(state->exinvars), jd->var, 1))
332 LOG("\t\tnext instruction");
334 } /* while instructions */
336 LOG("instructions done");
338 DOLOG(typecheck_print_vararray(stdout, jd, state->bptr->outvars,
339 state->bptr->outdepth));
340 DOLOG(typevector_print(stdout, jd->var, state->numlocals));
343 /* propagate stack and variables to the following block */
344 if (!superblockend) {
345 LOG("reaching following block");
346 tbptr = state->bptr->next;
347 while (tbptr->flags == BBDELETED) {
350 if (!typestate_reach(state,tbptr,state->bptr->outvars, jd->var,
351 state->bptr->outdepth))
359 bool typecheck_infer_types(jitdata *jd)
363 varinfo *savedlocals;
364 verifier_state state; /* current state of the verifier */
366 /* get required compiler data */
371 /* some logging on entry */
374 LOGSTR("\n==============================================================================\n");
375 DOLOG( show_method(jd, SHOW_STACK) );
376 LOGSTR("\n==============================================================================\n");
377 LOGMETHOD("Entering type inference: ",cd->method);
379 /* initialize the verifier state */
384 state.basicblockcount = jd->basicblockcount;
385 state.basicblocks = jd->basicblocks;
386 state.savedindices = NULL;
387 state.savedinvars = NULL;
389 /* check that the basicblock numbers are valid */
392 jit_check_basicblock_numbers(jd);
395 /* check if this method is an instance initializer method */
397 state.initmethod = (state.m->name == utf_init);
399 /* initialize the basic block flags for the following CFG traversal */
401 typecheck_init_flags(&state, BBFINISHED);
403 /* number of local variables */
405 /* In <init> methods we use an extra local variable to indicate whether */
406 /* the 'this' reference has been initialized. */
407 /* TYPE_VOID...means 'this' has not been initialized, */
408 /* TYPE_INT....means 'this' has been initialized. */
410 state.numlocals = state.jd->localcount;
411 state.validlocals = state.numlocals;
412 if (state.initmethod)
413 state.numlocals++; /* VERIFIER_EXTRA_LOCALS */
415 /* allocate the buffer of active exception handlers */
417 state.handlers = DMNEW(exception_entry*, state.jd->exceptiontablelength + 1);
419 /* save local variables */
421 savedlocals = DMNEW(varinfo, state.numlocals);
422 MCOPY(savedlocals, jd->var, varinfo, state.numlocals);
424 /* initialized local variables of first block */
426 if (!typecheck_init_locals(&state, false))
429 /* initialize invars of exception handlers */
431 state.exinvars = state.numlocals;
432 VAR(state.exinvars)->type = TYPE_ADR;
433 typeinfo_init_classinfo(&(VAR(state.exinvars)->typeinfo),
434 class_java_lang_Throwable); /* changed later */
436 LOG("Exception handler stacks set.\n");
438 /* loop while there are still blocks to be checked */
440 TYPECHECK_COUNT(count_iterations);
442 state.repeat = false;
444 state.bptr = state.basicblocks;
446 for (; state.bptr; state.bptr = state.bptr->next) {
447 LOGSTR1("---- BLOCK %04d, ",state.bptr->nr);
448 LOGSTR1("blockflags: %d\n",state.bptr->flags);
451 /* verify reached block */
452 if (state.bptr->flags == BBTYPECHECK_REACHED) {
453 if (!handle_basic_block(&state))
458 LOGIF(state.repeat,"state.repeat == true");
459 } while (state.repeat);
463 /* reset the flags of blocks we haven't reached */
465 typecheck_reset_flags(&state);
469 MCOPY(jd->var, savedlocals, varinfo, state.numlocals);
471 /* everything's ok */
473 LOGimp("exiting type inference");
478 * These are local overrides for various environment variables in Emacs.
479 * Please do not remove this and leave it at the end of the file, where
480 * Emacs will automagically detect them.
481 * ---------------------------------------------------------------------
484 * indent-tabs-mode: t
488 * vim:noexpandtab:sw=4:ts=4: