1 /* src/vm/jit/jit.h - code generation header
3 Copyright (C) 1996-2005, 2006, 2007, 2008
4 CACAOVM - Verein zur Foerderung der freien virtuellen Maschine CACAO
6 This file is part of CACAO.
8 This program is free software; you can redistribute it and/or
9 modify it under the terms of the GNU General Public License as
10 published by the Free Software Foundation; either version 2, or (at
11 your option) any later version.
13 This program is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
29 /* forward typedefs ***********************************************************/
31 typedef struct jitdata jitdata;
32 typedef struct basicblock basicblock;
33 typedef struct exception_entry exception_entry;
39 #include "toolbox/chain.h"
41 #include "vm/global.h"
42 #include "vm/method.h"
43 #include "vm/references.h"
44 #include "vm/resolve.h"
46 #if defined(ENABLE_STATISTICS)
47 # include "vm/statistics.h"
50 #include "vm/jit/codegen-common.h"
51 #include "vm/jit/reg.h"
52 #include "vm/jit/replace.h"
53 #include "vm/jit/stack.h"
54 #include "vm/jit/stacktrace.hpp"
56 #if defined(ENABLE_INLINING)
57 # include "vm/jit/inline/inline.h"
60 #include "vm/jit/ir/bytecode.h"
61 #include "vm/jit/ir/instruction.hpp"
63 #if defined(ENABLE_LOOP)
64 # include "vm/jit/loop/loop.h"
66 #if defined(ENABLE_SSA)
67 # include "vm/jit/optimizing/lsra.h"
69 #if defined(ENABLE_LSRA)
70 # include "vm/jit/allocator/lsra.h"
73 #include "vm/jit/verify/typeinfo.h"
76 /* common jit/codegen macros **************************************************/
78 #if defined(ENABLE_STATISTICS)
79 # define COUNT(x) (x)++
80 # define COUNT_SPILLS /* use COUNT_(READ|WRITE)_SPILLS instead */
81 # define COUNT_READ_SPILLS(var) \
83 case TYPE_FLT: count_spills_read_flt++; break; \
84 case TYPE_DBL: count_spills_read_dbl++; break; \
85 default: count_spills_read_ila++; break; \
88 # define COUNT_WRITE_SPILLS(var) \
90 case TYPE_FLT: count_spills_write_flt++; break; \
91 case TYPE_DBL: count_spills_write_dbl++; break; \
92 default: count_spills_write_ila++; break; \
96 # define COUNT(x) /* nothing */
97 # define COUNT_SPILLS /* nothing */
98 # define COUNT_READ_SPILLS(x) /* nothing */
99 # define COUNT_WRITE_SPILLS(x) /* nothing */
102 typedef struct interface_info interface_info;
104 struct interface_info {
110 /* jitdata ********************************************************************/
113 methodinfo *m; /* methodinfo of the method compiled */
117 #if defined(ENABLE_LOOP)
120 #if defined(ENABLE_SSA) || defined(ENABLE_LSRA)
124 u4 flags; /* contains JIT compiler flags */
126 instruction *instructions; /* ICMDs, valid between parse and stack */
127 basicblock *basicblocks; /* start of basic block list */
128 stackelement_t *stack; /* XXX should become stack.c internal */
129 s4 instructioncount;/* XXX remove this? */
130 s4 basicblockcount; /* number of basic blocks */
131 s4 stackcount; /* number of stackelements to allocate */
132 /* (passed from parse to stack) */
134 varinfo *var; /* array of variables */
135 s4 vartop; /* next free index in var array */
137 s4 varcount; /* number of variables in var array */
138 s4 localcount; /* number of locals at start of var ar. */
139 s4 *local_map; /* map for renaming (de-coallescing) */
140 /* locals and keeping the coalescing info for simplereg. */
141 /* local_map[javaindex * 5 + type] = */
142 /* >= 0......index into jd->var, or */
143 /* UNUSED....this (javaindex,type) pair is not used */
145 s4 *reverselocalmap; /* map from CACAO varindex to javaindex */
146 /* (varindex must be < localcount) */
148 s4 maxlocals; /* max. number of javalocals */
150 interface_info *interface_map; /* interface variables (for simplereg) */
151 s4 maxinterfaces; /* max. number of interface variables */
153 s4 exceptiontablelength; /* exceptiontable length */
154 exception_entry *exceptiontable; /* the exceptiontable */
156 basicblock *returnblock; /* block containing the *RETURN */
157 /* (only use if returncount==1) */
158 s4 returncount; /* number of return instructions */
159 bool branchtoentry; /* true if first block is a target */
160 bool branchtoend; /* true if end dummy is a target */
163 #define FOR_EACH_BASICBLOCK(jd, it) \
164 for ((it) = (jd)->basicblocks; (it) != NULL; (it) = (it)->next)
168 #define JITDATA_FLAG_PARSE 0x00000001
169 #define JITDATA_FLAG_VERIFY 0x00000002
171 #define JITDATA_FLAG_INSTRUMENT 0x00000004
173 #define JITDATA_FLAG_IFCONV 0x00000008
174 #define JITDATA_FLAG_REORDER 0x00000010
175 #define JITDATA_FLAG_INLINE 0x00000020
177 #define JITDATA_FLAG_COUNTDOWN 0x00000100
179 #define JITDATA_FLAG_SHOWINTERMEDIATE 0x20000000
180 #define JITDATA_FLAG_SHOWDISASSEMBLE 0x40000000
181 #define JITDATA_FLAG_VERBOSECALL 0x80000000
184 #define JITDATA_HAS_FLAG_PARSE(jd) \
185 ((jd)->flags & JITDATA_FLAG_PARSE)
187 #define JITDATA_HAS_FLAG_VERIFY(jd) \
188 ((jd)->flags & JITDATA_FLAG_VERIFY)
190 #define JITDATA_HAS_FLAG_INSTRUMENT(jd) \
191 ((jd)->flags & JITDATA_FLAG_INSTRUMENT)
193 #define JITDATA_HAS_FLAG_IFCONV(jd) \
194 ((jd)->flags & JITDATA_FLAG_IFCONV)
196 #define JITDATA_HAS_FLAG_REORDER(jd) \
197 ((jd)->flags & JITDATA_FLAG_REORDER)
199 #define JITDATA_HAS_FLAG_INLINE(jd) \
200 ((jd)->flags & JITDATA_FLAG_INLINE)
202 #define JITDATA_HAS_FLAG_COUNTDOWN(jd) \
203 ((jd)->flags & JITDATA_FLAG_COUNTDOWN)
205 #define JITDATA_HAS_FLAG_SHOWINTERMEDIATE(jd) \
206 ((jd)->flags & JITDATA_FLAG_SHOWINTERMEDIATE)
208 #define JITDATA_HAS_FLAG_SHOWDISASSEMBLE(jd) \
209 ((jd)->flags & JITDATA_FLAG_SHOWDISASSEMBLE)
211 #define JITDATA_HAS_FLAG_VERBOSECALL(jd) \
212 ((jd)->flags & JITDATA_FLAG_VERBOSECALL)
215 /* exception_entry ************************************************************/
217 struct exception_entry {
221 classref_or_classinfo catchtype; /* catchtype of exc. (NULL == catchall) */
222 exception_entry *next; /* next in list of exceptions when */
223 /* loops are copied */
224 exception_entry *down; /* next exception_entry */
228 /* macros for accessing variables *********************************************
230 Use VAROP for s1, s2, s3 and dst operands (eg. VAROP(iptr->s1)),
231 use VAR if you have the variable index (eg. VAR(iptr->sx.s23.s2.args[0])).
233 ******************************************************************************/
235 #define VAROP(v) (jd->var + (v).varindex)
236 #define VAR(i) (jd->var + (i))
238 static inline bool var_is_local(const jitdata *jd, s4 i) {
239 return (i < jd->localcount);
242 static inline bool var_is_prealloc(const jitdata *jd, s4 i) {
243 return ((i >= jd->localcount) && (jd->var[i].flags & PREALLOC));
246 static inline bool var_is_inout(const jitdata *jd, s4 i) {
247 const varinfo *v = jd->var + i;
249 (i >= jd->localcount) && (
250 (!(v->flags & PREALLOC) && (v->flags & INOUT)) ||
251 /* special case of TYPE_RET, used with JSR */
252 ((v->flags & PREALLOC) && (v->flags & INOUT) && (v->type == TYPE_RET))
257 static inline bool var_is_temp(const jitdata *jd, s4 i) {
258 const varinfo *v = jd->var + i;
259 return ((i >= jd->localcount) && !(v->flags & PREALLOC) && !(v->flags & INOUT));
262 static inline bool var_is_saved(const jitdata *jd, s4 i) {
263 return (jd->var[i].flags & SAVEDVAR);
267 /* basicblock *****************************************************************/
276 #define BBTYPECHECK_UNDEF 2
277 #define BBTYPECHECK_REACHED 3
279 #define BBTYPE_STD 0 /* standard basic block type */
280 #define BBTYPE_EXH 1 /* exception handler basic block type */
281 #define BBTYPE_SBR 2 /* subroutine basic block type */
283 #define BBFLAG_REPLACEMENT 0x01 /* put a replacement point at the start */
285 /* XXX basicblock wastes quite a lot of memory by having four flag fields */
286 /* (flags, bitflags, type and lflags). Probably the last three could be */
287 /* combined without loss of efficiency. The first one could be combined with */
288 /* the others by using bitfields. */
290 /* XXX "flags" should probably be called "state", as it is an integer state */
293 s4 nr; /* basic block number */
294 s4 flags; /* used during stack analysis, init with -1 */
295 s4 bitflags; /* OR of BBFLAG_... constants, init with 0 */
296 s4 type; /* basic block type (std, xhandler, subroutine*/
297 s4 lflags; /* used during loop copying, init with 0 */
299 s4 icount; /* number of intermediate code instructions */
300 instruction *iinstr; /* pointer to intermediate code instructions */
302 varinfo *inlocals; /* copy of locals on block entry */
303 s4 *javalocals; /* map from java locals to cacao variables[+] */
304 s4 *invars; /* array of in-variables at begin of block */
305 s4 *outvars; /* array of out-variables at end of block */
306 s4 indepth; /* stack depth at begin of basic block */
307 s4 outdepth; /* stack depth end of basic block */
308 s4 varstart; /* index of first non-invar block variable */
309 s4 varcount; /* number of non-invar block variables */
313 basicblock **predecessors; /* array of predecessor basic blocks */
314 basicblock **successors; /* array of successor basic blocks */
316 branchref *branchrefs; /* list of branches to be patched */
318 basicblock *next; /* used to build a BB list (instead of array) */
319 basicblock *copied_to; /* points to the copy of this basic block */
320 /* when loop nodes are copied */
321 basicblock *original; /* block of which this block is a clone */
322 /* NULL for the original block itself */
323 methodinfo *method; /* method this block belongs to */
324 insinfo_inline *inlineinfo; /* inlineinfo for the start of this block */
326 s4 mpc; /* machine code pc at start of block */
328 /* TODO: those fields are probably usefull for other passes as well. */
330 #if defined(ENABLE_SSA)
331 basicblock *idom; /* Immediate dominator, parent in dominator tree */
332 basicblock **domsuccessors;/* Children in dominator tree */
333 s4 domsuccessorcount;
335 basicblock **domfrontier; /* Dominance frontier */
338 basicblock **exhandlers; /* Exception handlers for this block */
340 basicblock **expredecessors; /* Blocks this block is exception handler for */
341 s4 expredecessorcount;
342 s4 exouts; /* Number of exceptional exits */
344 instruction *phis; /* Phi functions */
345 s4 phicount; /* Number of phi functions */
347 void *vp; /* Freely used by different passes */
351 #define FOR_EACH_SUCCESSOR(bptr, it) \
352 for ((it) = (bptr)->successors; (it) != (bptr)->successors + (bptr)->successorcount; ++(it))
354 #define FOR_EACH_PREDECESSOR(bptr, it) \
356 (it) = (bptr)->predecessors; \
357 (it) != (bptr)->predecessors + ((bptr)->predecessorcount < 0 ? 0 : (bptr)->predecessorcount); \
361 #define FOR_EACH_INSTRUCTION(bptr, it) \
362 for ((it) = (bptr)->iinstr; (it) != (bptr)->iinstr + (bptr)->icount; ++(it))
364 #define FOR_EACH_INSTRUCTION_REV(bptr, it) \
365 for ((it) = (bptr)->iinstr + (bptr)->icount - 1; (it) != (bptr)->iinstr - 1; --(it))
367 #if defined(ENABLE_SSA)
369 #define FOR_EACH_EXHANDLER(bptr, it) \
370 for ((it) = (bptr)->exhandlers; (it) != (bptr)->exhandlers + (bptr)->exhandlercount; ++(it))
372 #define FOR_EACH_EXPREDECESSOR(bptr, it) \
373 for ((it) = (bptr)->expredecessors; (it) != (bptr)->expredecessors + (bptr)->expredecessorcount; ++(it))
377 /* [+]...the javalocals array: This array is indexed by the javaindex (the */
378 /* local variable index ocurring in the original bytecode). An element */
379 /* javalocals[javaindex] encodes where to find the contents of the */
380 /* original variable at this point in the program. */
381 /* There are three cases for javalocals[javaindex]: */
382 /* >= 0.......it's an index into the jd->var array, where the */
383 /* CACAO variable corresponding to the original local */
385 /* UNUSED.....the original variable is not live at this point */
386 /* < UNUSED...the original variable contains a returnAddress at */
387 /* this point. The number of the block to return to can */
388 /* be calculated using RETADDR_FROM_JAVALOCAL: */
390 /* javalocals[javaindex] == JAVALOCAL_FROM_RETADDR(nr) */
391 /* RETADDR_FROM_JAVALOCAL(javalocals[javaindex]) == nr */
393 #define JAVALOCAL_FROM_RETADDR(nr) (UNUSED - (1 + (nr)))
394 #define RETADDR_FROM_JAVALOCAL(jl) (UNUSED - (1 + (jl)))
397 /* Macro for initializing newly allocated basic block's. It does not
398 need to zero fields, as we zero out the whole basic block array. */
400 #define BASICBLOCK_INIT(bptr,m) \
404 bptr->type = BBTYPE_STD; \
405 bptr->method = (m); \
408 static inline bool basicblock_reached(const basicblock *bptr) {
409 return (bptr->flags >= BBREACHED);
414 /***************************** register types *********************************/
416 #define REG_RES 0 /* reserved register for OS or code generator */
417 #define REG_RET 1 /* return value register */
418 #define REG_EXC 2 /* exception value register */
419 #define REG_SAV 3 /* (callee) saved register */
420 #define REG_TMP 4 /* scratch temporary register (caller saved) */
421 #define REG_ARG 5 /* argument register (caller saved) */
423 #define REG_END -1 /* last entry in tables */
425 #define PARAMMODE_NUMBERED 0
426 #define PARAMMODE_STUFFED 1
429 /* function prototypes ********************************************************/
431 /* compiler initialisation */
434 /* compiler finalisation */
435 void jit_close(void);
437 /* create a new jitdata */
438 jitdata *jit_jitdata_new(methodinfo *m);
440 /* compile a method with jit compiler */
441 u1 *jit_compile(methodinfo *m);
442 u1 *jit_recompile(methodinfo *m);
444 void jit_invalidate_code(methodinfo *m);
445 codeinfo *jit_get_current_code(methodinfo *m);
446 void jit_request_optimization(methodinfo *m);
448 /* patch the method entrypoint */
449 #if !defined(JIT_COMPILER_VIA_SIGNAL)
450 u1 *jit_asm_compile(methodinfo *m, u1 *mptr, u1 *sp, u1 *ra);
452 void *jit_compile_handle(methodinfo *m, void *pv, void *ra, void *mptr);
454 s4 jit_complement_condition(s4 opcode);
456 void jit_renumber_basicblocks(jitdata *jd);
458 void jit_check_basicblock_numbers(jitdata *jd);
462 /* machine dependent functions ************************************************/
464 #if defined(ENABLE_JIT)
468 #if defined(ENABLE_INTRP)
469 void intrp_md_init(void);
472 void *md_jit_method_patch_address(void *pv, void *ra, void *mptr);
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: