* src/vm/jit/jit.c: Moved instruction stuff to...
[cacao.git] / src / vm / jit / jit.h
1 /* src/vm/jit/jit.h - code generation header
2
3    Copyright (C) 1996-2005, 2006, 2007, 2008
4    CACAOVM - Verein zur Foerderung der freien virtuellen Maschine CACAO
5
6    This file is part of CACAO.
7
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.
12
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.
17
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
21    02110-1301, USA.
22
23 */
24
25
26 #ifndef _JIT_H
27 #define _JIT_H
28
29 /* forward typedefs ***********************************************************/
30
31 typedef struct jitdata jitdata;
32 typedef struct basicblock basicblock;
33 typedef struct exception_entry exception_entry;
34
35
36 #include "config.h"
37 #include "vm/types.h"
38
39 #include "toolbox/chain.h"
40
41 #include "vm/global.h"
42 #include "vm/method.h"
43 #include "vm/references.h"
44 #include "vm/resolve.h"
45
46 #if defined(ENABLE_STATISTICS)
47 # include "vm/statistics.h"
48 #endif
49
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"
55
56 #if defined(ENABLE_INLINING)
57 # include "vm/jit/inline/inline.h"
58 #endif
59
60 #include "vm/jit/ir/bytecode.h"
61 #include "vm/jit/ir/instruction.hpp"
62
63 #if defined(ENABLE_LOOP)
64 # include "vm/jit/loop/loop.h"
65 #endif
66 #if defined(ENABLE_SSA) 
67 # include "vm/jit/optimizing/lsra.h"
68 #endif
69 #if defined(ENABLE_LSRA)
70 # include "vm/jit/allocator/lsra.h"
71 #endif
72
73 #include "vm/jit/verify/typeinfo.h"
74
75
76 /* common jit/codegen macros **************************************************/
77
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) \
82         switch(var->type) { \
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; \
86         }
87
88 # define COUNT_WRITE_SPILLS(var) \
89         switch(var->type) { \
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; \
93         }
94
95 #else
96 # define COUNT(x)                /* nothing */
97 # define COUNT_SPILLS            /* nothing */
98 # define COUNT_READ_SPILLS(x)    /* nothing */
99 # define COUNT_WRITE_SPILLS(x)   /* nothing */
100 #endif
101
102 typedef struct interface_info interface_info;
103
104 struct interface_info {
105         s4 flags;
106         s4 regoff;
107 };
108
109
110 /* jitdata ********************************************************************/
111
112 struct jitdata {
113         methodinfo      *m;               /* methodinfo of the method compiled    */
114         codeinfo        *code;
115         codegendata     *cd;
116         registerdata    *rd;
117 #if defined(ENABLE_LOOP)
118         loopdata        *ld;
119 #endif
120 #if defined(ENABLE_SSA) || defined(ENABLE_LSRA)
121         lsradata        *ls;
122 #endif
123
124         u4               flags;           /* contains JIT compiler flags          */
125
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)         */
133
134         varinfo         *var;             /* array of variables                   */
135         s4               vartop;          /* next free index in var array         */
136
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  */
144
145         s4              *reverselocalmap; /* map from CACAO varindex to javaindex */
146                                           /* (varindex must be < localcount)      */
147
148         s4               maxlocals;       /* max. number of javalocals            */
149
150         interface_info  *interface_map;   /* interface variables (for simplereg)  */
151         s4               maxinterfaces;   /* max. number of interface variables   */
152
153         s4               exceptiontablelength; /* exceptiontable length           */
154         exception_entry *exceptiontable;       /* the exceptiontable              */
155
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   */
161 };
162
163 #define FOR_EACH_BASICBLOCK(jd, it) \
164         for ((it) = (jd)->basicblocks; (it) != NULL; (it) = (it)->next)
165
166 #define UNUSED                     -1
167
168 #define JITDATA_FLAG_PARSE               0x00000001
169 #define JITDATA_FLAG_VERIFY              0x00000002
170
171 #define JITDATA_FLAG_INSTRUMENT          0x00000004
172
173 #define JITDATA_FLAG_IFCONV              0x00000008
174 #define JITDATA_FLAG_REORDER             0x00000010
175 #define JITDATA_FLAG_INLINE              0x00000020
176
177 #define JITDATA_FLAG_COUNTDOWN           0x00000100
178
179 #define JITDATA_FLAG_SHOWINTERMEDIATE    0x20000000
180 #define JITDATA_FLAG_SHOWDISASSEMBLE     0x40000000
181 #define JITDATA_FLAG_VERBOSECALL         0x80000000
182
183
184 #define JITDATA_HAS_FLAG_PARSE(jd) \
185     ((jd)->flags & JITDATA_FLAG_PARSE)
186
187 #define JITDATA_HAS_FLAG_VERIFY(jd) \
188     ((jd)->flags & JITDATA_FLAG_VERIFY)
189
190 #define JITDATA_HAS_FLAG_INSTRUMENT(jd) \
191     ((jd)->flags & JITDATA_FLAG_INSTRUMENT)
192
193 #define JITDATA_HAS_FLAG_IFCONV(jd) \
194     ((jd)->flags & JITDATA_FLAG_IFCONV)
195
196 #define JITDATA_HAS_FLAG_REORDER(jd) \
197     ((jd)->flags & JITDATA_FLAG_REORDER)
198
199 #define JITDATA_HAS_FLAG_INLINE(jd) \
200     ((jd)->flags & JITDATA_FLAG_INLINE)
201
202 #define JITDATA_HAS_FLAG_COUNTDOWN(jd) \
203     ((jd)->flags & JITDATA_FLAG_COUNTDOWN)
204
205 #define JITDATA_HAS_FLAG_SHOWINTERMEDIATE(jd) \
206     ((jd)->flags & JITDATA_FLAG_SHOWINTERMEDIATE)
207
208 #define JITDATA_HAS_FLAG_SHOWDISASSEMBLE(jd) \
209     ((jd)->flags & JITDATA_FLAG_SHOWDISASSEMBLE)
210
211 #define JITDATA_HAS_FLAG_VERBOSECALL(jd) \
212     ((jd)->flags & JITDATA_FLAG_VERBOSECALL)
213
214
215 /* exception_entry ************************************************************/
216
217 struct exception_entry {
218         basicblock           *start;
219         basicblock           *end;
220         basicblock           *handler;
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                  */
225 };
226
227
228 /* macros for accessing variables *********************************************
229  
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])).
232
233 ******************************************************************************/
234
235 #define VAROP(v) (jd->var + (v).varindex)
236 #define VAR(i)   (jd->var + (i))
237
238 static inline bool var_is_local(const jitdata *jd, s4 i) {
239         return (i < jd->localcount);
240 }
241
242 static inline bool var_is_prealloc(const jitdata *jd, s4 i) {
243         return ((i >= jd->localcount) && (jd->var[i].flags & PREALLOC));
244 }
245
246 static inline bool var_is_inout(const jitdata *jd, s4 i) {
247         const varinfo *v = jd->var + i;
248         return (
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))
253                 )
254         );
255 }
256
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));
260 }
261
262 static inline bool var_is_saved(const jitdata *jd, s4 i) {
263         return (jd->var[i].flags & SAVEDVAR);
264 }
265
266
267 /* basicblock *****************************************************************/
268
269 /* flags */
270
271 #define BBDELETED            -2
272 #define BBUNDEF              -1
273 #define BBREACHED            0
274 #define BBFINISHED           1
275
276 #define BBTYPECHECK_UNDEF    2
277 #define BBTYPECHECK_REACHED  3
278
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                */
282
283 #define BBFLAG_REPLACEMENT   0x01  /* put a replacement point at the start    */
284
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.                                             */
289
290 /* XXX "flags" should probably be called "state", as it is an integer state   */
291
292 struct basicblock {
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      */
298
299         s4            icount;       /* number of intermediate code instructions   */
300         instruction  *iinstr;       /* pointer to intermediate code instructions  */
301
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        */
310
311         s4            predecessorcount;
312         s4            successorcount;
313         basicblock  **predecessors; /* array of predecessor basic blocks          */
314         basicblock  **successors;   /* array of successor basic blocks            */
315
316         branchref    *branchrefs;   /* list of branches to be patched             */
317
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     */
325
326         s4            mpc;          /* machine code pc at start of block          */
327
328         /* TODO: those fields are probably usefull for other passes as well. */
329
330 #if defined(ENABLE_SSA)         
331         basicblock   *idom;         /* Immediate dominator, parent in dominator tree */
332         basicblock  **domsuccessors;/* Children in dominator tree                 */
333         s4            domsuccessorcount;
334
335         basicblock  **domfrontier;  /* Dominance frontier                         */
336         s4            domfrontiercount;
337
338         basicblock  **exhandlers;   /* Exception handlers for this block */
339         s4            exhandlercount;
340         basicblock  **expredecessors; /* Blocks this block is exception handler for */
341         s4            expredecessorcount;
342         s4            exouts;       /* Number of exceptional exits */
343
344         instruction  *phis;         /* Phi functions */
345         s4            phicount;     /* Number of phi functions */
346
347         void         *vp;           /* Freely used by different passes            */
348 #endif
349 };
350
351 #define FOR_EACH_SUCCESSOR(bptr, it) \
352         for ((it) = (bptr)->successors; (it) != (bptr)->successors + (bptr)->successorcount; ++(it))
353
354 #define FOR_EACH_PREDECESSOR(bptr, it) \
355         for ( \
356                 (it) = (bptr)->predecessors; \
357                 (it) != (bptr)->predecessors + ((bptr)->predecessorcount < 0 ? 0 : (bptr)->predecessorcount); \
358                 ++(it) \
359         )
360
361 #define FOR_EACH_INSTRUCTION(bptr, it) \
362         for ((it) = (bptr)->iinstr; (it) != (bptr)->iinstr + (bptr)->icount; ++(it))
363
364 #define FOR_EACH_INSTRUCTION_REV(bptr, it) \
365         for ((it) = (bptr)->iinstr + (bptr)->icount - 1; (it) != (bptr)->iinstr - 1; --(it))
366
367 #if defined(ENABLE_SSA)
368
369 #define FOR_EACH_EXHANDLER(bptr, it) \
370         for ((it) = (bptr)->exhandlers; (it) != (bptr)->exhandlers + (bptr)->exhandlercount; ++(it))
371
372 #define FOR_EACH_EXPREDECESSOR(bptr, it) \
373         for ((it) = (bptr)->expredecessors; (it) != (bptr)->expredecessors + (bptr)->expredecessorcount; ++(it))
374
375 #endif
376
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    */
384 /*                      can be found.                                         */
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:           */
389 /*                                                                            */
390 /*                      javalocals[javaindex] == JAVALOCAL_FROM_RETADDR(nr)   */
391 /*                      RETADDR_FROM_JAVALOCAL(javalocals[javaindex]) == nr   */
392
393 #define JAVALOCAL_FROM_RETADDR(nr)  (UNUSED - (1 + (nr)))
394 #define RETADDR_FROM_JAVALOCAL(jl)  (UNUSED - (1 + (jl)))
395
396
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. */
399
400 #define BASICBLOCK_INIT(bptr,m)                        \
401         do {                                               \
402                 bptr->mpc    = -1;                             \
403                 bptr->flags  = -1;                             \
404                 bptr->type   = BBTYPE_STD;                     \
405                 bptr->method = (m);                            \
406         } while (0)
407
408 static inline bool basicblock_reached(const basicblock *bptr) {
409         return (bptr->flags >= BBREACHED);
410 }
411
412
413
414 /***************************** register types *********************************/
415
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)               */
422
423 #define REG_END   -1        /* last entry in tables                           */
424  
425 #define PARAMMODE_NUMBERED  0 
426 #define PARAMMODE_STUFFED   1
427
428
429 /* function prototypes ********************************************************/
430
431 /* compiler initialisation */
432 void jit_init(void);
433
434 /* compiler finalisation */
435 void jit_close(void);
436
437 /* create a new jitdata */
438 jitdata *jit_jitdata_new(methodinfo *m);
439
440 /* compile a method with jit compiler */
441 u1 *jit_compile(methodinfo *m);
442 u1 *jit_recompile(methodinfo *m);
443
444 void jit_invalidate_code(methodinfo *m);
445 codeinfo *jit_get_current_code(methodinfo *m);
446 void jit_request_optimization(methodinfo *m);
447
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);
451 #endif
452 void *jit_compile_handle(methodinfo *m, void *pv, void *ra, void *mptr);
453
454 s4 jit_complement_condition(s4 opcode);
455
456 void jit_renumber_basicblocks(jitdata *jd);
457 #if !defined(NDEBUG)
458 void jit_check_basicblock_numbers(jitdata *jd);
459 #endif
460
461
462 /* machine dependent functions ************************************************/
463
464 #if defined(ENABLE_JIT)
465 void md_init(void);
466 #endif
467
468 #if defined(ENABLE_INTRP)
469 void intrp_md_init(void);
470 #endif
471
472 void *md_jit_method_patch_address(void *pv, void *ra, void *mptr);
473
474 #endif /* _JIT_H */
475
476
477 /*
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  * ---------------------------------------------------------------------
482  * Local variables:
483  * mode: c
484  * indent-tabs-mode: t
485  * c-basic-offset: 4
486  * tab-width: 4
487  * End:
488  * vim:noexpandtab:sw=4:ts=4:
489  */