Removed return value from descriptor_params_from_paramtypes.
[cacao.git] / src / vm / jit / jit.hpp
1 /* src/vm/jit/jit.hpp - Just-In-Time compiler
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_HPP
27 #define _JIT_HPP
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 "vm/global.h"
40 #include "vm/method.hpp"
41 #include "vm/references.h"
42 #include "vm/resolve.hpp"
43
44 #if defined(ENABLE_STATISTICS)
45 # include "vm/statistics.h"
46 #endif
47
48 #include "vm/jit/codegen-common.hpp"
49 #include "vm/jit/reg.h"
50 #include "vm/jit/replace.hpp"
51 #include "vm/jit/stack.h"
52 #include "vm/jit/stacktrace.hpp"
53
54 #if defined(ENABLE_INLINING)
55 # include "vm/jit/inline/inline.hpp"
56 #endif
57
58 #include "vm/jit/ir/bytecode.h"
59 #include "vm/jit/ir/instruction.hpp"
60
61 #if defined(ENABLE_LOOP)
62 # include "vm/jit/loop/loop.h"
63 #endif
64 #if defined(ENABLE_SSA) 
65 # include "vm/jit/optimizing/lsra.h"
66 #endif
67 #if defined(ENABLE_LSRA)
68 # include "vm/jit/allocator/lsra.h"
69 #endif
70
71 #include "vm/jit/verify/typeinfo.hpp"
72
73
74 /* common jit/codegen macros **************************************************/
75
76 #if defined(ENABLE_STATISTICS)
77 # define COUNT(x)        (x)++
78 # define COUNT_SPILLS             /* use COUNT_(READ|WRITE)_SPILLS instead */
79 # define COUNT_READ_SPILLS(var) \
80         switch(var->type) { \
81         case TYPE_FLT: count_spills_read_flt++; break; \
82         case TYPE_DBL: count_spills_read_dbl++; break; \
83         default: count_spills_read_ila++; break; \
84         }
85
86 # define COUNT_WRITE_SPILLS(var) \
87         switch(var->type) { \
88         case TYPE_FLT: count_spills_write_flt++; break; \
89         case TYPE_DBL: count_spills_write_dbl++; break; \
90         default: count_spills_write_ila++; break; \
91         }
92
93 #else
94 # define COUNT(x)                /* nothing */
95 # define COUNT_SPILLS            /* nothing */
96 # define COUNT_READ_SPILLS(x)    /* nothing */
97 # define COUNT_WRITE_SPILLS(x)   /* nothing */
98 #endif
99
100 typedef struct interface_info interface_info;
101
102 struct interface_info {
103         s4 flags;
104         s4 regoff;
105 };
106
107
108 /* jitdata ********************************************************************/
109
110 struct jitdata {
111         methodinfo      *m;               /* methodinfo of the method compiled    */
112         codeinfo        *code;
113         codegendata     *cd;
114         registerdata    *rd;
115 #if defined(ENABLE_LOOP)
116         loopdata        *ld;
117 #endif
118 #if defined(ENABLE_SSA) || defined(ENABLE_LSRA)
119         lsradata        *ls;
120 #endif
121
122         u4               flags;           /* contains JIT compiler flags          */
123
124         instruction     *instructions;    /* ICMDs, valid between parse and stack */
125         basicblock      *basicblocks;     /* start of basic block list            */
126         stackelement_t  *stack;           /* XXX should become stack.c internal   */
127         s4               instructioncount;/* XXX remove this?                     */
128         s4               basicblockcount; /* number of basic blocks               */
129         s4               stackcount;      /* number of stackelements to allocate  */
130                                       /* (passed from parse to stack)         */
131
132         varinfo         *var;             /* array of variables                   */
133         s4               vartop;          /* next free index in var array         */
134
135         s4               varcount;        /* number of variables in var array     */
136         s4               localcount;      /* number of locals at start of var ar. */
137     s4              *local_map;       /* map for renaming (de-coallescing)    */
138                                          /* locals and keeping the coalescing info for simplereg. */
139                          /* local_map[javaindex * 5 + type] =                     */
140                          /*     >= 0......index into jd->var, or                  */
141                                          /*     UNUSED....this (javaindex,type) pair is not used  */
142
143         s4              *reverselocalmap; /* map from CACAO varindex to javaindex */
144                                           /* (varindex must be < localcount)      */
145
146         s4               maxlocals;       /* max. number of javalocals            */
147
148         interface_info  *interface_map;   /* interface variables (for simplereg)  */
149         s4               maxinterfaces;   /* max. number of interface variables   */
150
151         s4               exceptiontablelength; /* exceptiontable length           */
152         exception_entry *exceptiontable;       /* the exceptiontable              */
153
154         basicblock      *returnblock;          /* block containing the *RETURN    */
155                                                /* (only use if returncount==1)    */
156         s4               returncount;          /* number of return instructions   */
157         bool             branchtoentry;        /* true if first block is a target */
158         bool             branchtoend;          /* true if end dummy is a target   */
159 };
160
161 #define FOR_EACH_BASICBLOCK(jd, it) \
162         for ((it) = (jd)->basicblocks; (it) != NULL; (it) = (it)->next)
163
164 #define UNUSED                     -1
165
166 #define JITDATA_FLAG_PARSE               0x00000001
167 #define JITDATA_FLAG_VERIFY              0x00000002
168
169 #define JITDATA_FLAG_INSTRUMENT          0x00000004
170
171 #define JITDATA_FLAG_IFCONV              0x00000008
172 #define JITDATA_FLAG_REORDER             0x00000010
173 #define JITDATA_FLAG_INLINE              0x00000020
174
175 #define JITDATA_FLAG_COUNTDOWN           0x00000100
176
177 #define JITDATA_FLAG_SHOWINTERMEDIATE    0x20000000
178 #define JITDATA_FLAG_SHOWDISASSEMBLE     0x40000000
179 #define JITDATA_FLAG_VERBOSECALL         0x80000000
180
181
182 #define JITDATA_HAS_FLAG_PARSE(jd) \
183     ((jd)->flags & JITDATA_FLAG_PARSE)
184
185 #define JITDATA_HAS_FLAG_VERIFY(jd) \
186     ((jd)->flags & JITDATA_FLAG_VERIFY)
187
188 #define JITDATA_HAS_FLAG_INSTRUMENT(jd) \
189     ((jd)->flags & JITDATA_FLAG_INSTRUMENT)
190
191 #define JITDATA_HAS_FLAG_IFCONV(jd) \
192     ((jd)->flags & JITDATA_FLAG_IFCONV)
193
194 #define JITDATA_HAS_FLAG_REORDER(jd) \
195     ((jd)->flags & JITDATA_FLAG_REORDER)
196
197 #define JITDATA_HAS_FLAG_INLINE(jd) \
198     ((jd)->flags & JITDATA_FLAG_INLINE)
199
200 #define JITDATA_HAS_FLAG_COUNTDOWN(jd) \
201     ((jd)->flags & JITDATA_FLAG_COUNTDOWN)
202
203 #define JITDATA_HAS_FLAG_SHOWINTERMEDIATE(jd) \
204     ((jd)->flags & JITDATA_FLAG_SHOWINTERMEDIATE)
205
206 #define JITDATA_HAS_FLAG_SHOWDISASSEMBLE(jd) \
207     ((jd)->flags & JITDATA_FLAG_SHOWDISASSEMBLE)
208
209 #define JITDATA_HAS_FLAG_VERBOSECALL(jd) \
210     ((jd)->flags & JITDATA_FLAG_VERBOSECALL)
211
212
213 /* exception_entry ************************************************************/
214
215 struct exception_entry {
216         basicblock           *start;
217         basicblock           *end;
218         basicblock           *handler;
219         classref_or_classinfo catchtype; /* catchtype of exc. (NULL == catchall)  */
220         exception_entry      *next;      /* next in list of exceptions when       */
221                                                                          /* loops are copied                      */
222         exception_entry      *down;      /* next exception_entry                  */
223 };
224
225
226 /* macros for accessing variables *********************************************
227  
228    Use VAROP for s1, s2, s3 and dst operands (eg. VAROP(iptr->s1)),
229    use VAR if you have the variable index (eg. VAR(iptr->sx.s23.s2.args[0])).
230
231 ******************************************************************************/
232
233 #define VAROP(v) (jd->var + (v).varindex)
234 #define VAR(i)   (jd->var + (i))
235
236 static inline bool var_is_local(const jitdata *jd, s4 i) {
237         return (i < jd->localcount);
238 }
239
240 static inline bool var_is_prealloc(const jitdata *jd, s4 i) {
241         return ((i >= jd->localcount) && (jd->var[i].flags & PREALLOC));
242 }
243
244 static inline bool var_is_inout(const jitdata *jd, s4 i) {
245         const varinfo *v = jd->var + i;
246         return (
247                 (i >= jd->localcount) && (
248                         (!(v->flags & PREALLOC) && (v->flags & INOUT)) ||
249                         /* special case of TYPE_RET, used with JSR */
250                         ((v->flags & PREALLOC) && (v->flags & INOUT) && (v->type == TYPE_RET))
251                 )
252         );
253 }
254
255 static inline bool var_is_temp(const jitdata *jd, s4 i) {
256         const varinfo *v = jd->var + i;
257         return ((i >= jd->localcount) && !(v->flags & PREALLOC) && !(v->flags & INOUT));
258 }
259
260 static inline bool var_is_saved(const jitdata *jd, s4 i) {
261         return (jd->var[i].flags & SAVEDVAR);
262 }
263
264
265 /* basicblock *****************************************************************/
266
267 /* flags */
268
269 #define BBDELETED            -2
270 #define BBUNDEF              -1
271 #define BBREACHED            0
272 #define BBFINISHED           1
273
274 #define BBTYPECHECK_UNDEF    2
275 #define BBTYPECHECK_REACHED  3
276
277 #define BBTYPE_STD           0  /* standard basic block type                  */
278 #define BBTYPE_EXH           1  /* exception handler basic block type         */
279 #define BBTYPE_SBR           2  /* subroutine basic block type                */
280
281 #define BBFLAG_REPLACEMENT   0x01  /* put a replacement point at the start    */
282
283 /* XXX basicblock wastes quite a lot of memory by having four flag fields     */
284 /* (flags, bitflags, type and lflags). Probably the last three could be       */
285 /* combined without loss of efficiency. The first one could be combined with  */
286 /* the others by using bitfields.                                             */
287
288 /* XXX "flags" should probably be called "state", as it is an integer state   */
289
290 struct basicblock {
291         s4            nr;           /* basic block number                         */
292         s4            flags;        /* used during stack analysis, init with -1   */
293         s4            bitflags;     /* OR of BBFLAG_... constants, init with 0    */
294         s4            type;         /* basic block type (std, xhandler, subroutine*/
295         s4            lflags;       /* used during loop copying, init with 0      */
296
297         s4            icount;       /* number of intermediate code instructions   */
298         instruction  *iinstr;       /* pointer to intermediate code instructions  */
299
300         varinfo      *inlocals;     /* copy of locals on block entry              */
301         s4           *javalocals;   /* map from java locals to cacao variables[+] */
302         s4           *invars;       /* array of in-variables at begin of block    */
303         s4           *outvars;      /* array of out-variables at end of block     */
304         s4            indepth;      /* stack depth at begin of basic block        */
305         s4            outdepth;     /* stack depth end of basic block             */
306         s4            varstart;     /* index of first non-invar block variable    */
307         s4            varcount;     /* number of non-invar block variables        */
308
309         s4            predecessorcount;
310         s4            successorcount;
311         basicblock  **predecessors; /* array of predecessor basic blocks          */
312         basicblock  **successors;   /* array of successor basic blocks            */
313
314         branchref    *branchrefs;   /* list of branches to be patched             */
315
316         basicblock   *next;         /* used to build a BB list (instead of array) */
317         basicblock   *copied_to;    /* points to the copy of this basic block     */
318                                 /* when loop nodes are copied                 */
319         basicblock   *original;     /* block of which this block is a clone       */
320                                     /* NULL for the original block itself         */
321         methodinfo   *method;       /* method this block belongs to               */
322         insinfo_inline *inlineinfo; /* inlineinfo for the start of this block     */
323
324         s4            mpc;          /* machine code pc at start of block          */
325
326         /* TODO: those fields are probably usefull for other passes as well. */
327
328 #if defined(ENABLE_SSA)         
329         basicblock   *idom;         /* Immediate dominator, parent in dominator tree */
330         basicblock  **domsuccessors;/* Children in dominator tree                 */
331         s4            domsuccessorcount;
332
333         basicblock  **domfrontier;  /* Dominance frontier                         */
334         s4            domfrontiercount;
335
336         basicblock  **exhandlers;   /* Exception handlers for this block */
337         s4            exhandlercount;
338         basicblock  **expredecessors; /* Blocks this block is exception handler for */
339         s4            expredecessorcount;
340         s4            exouts;       /* Number of exceptional exits */
341
342         instruction  *phis;         /* Phi functions */
343         s4            phicount;     /* Number of phi functions */
344
345         void         *vp;           /* Freely used by different passes            */
346 #endif
347 };
348
349 #define FOR_EACH_SUCCESSOR(bptr, it) \
350         for ((it) = (bptr)->successors; (it) != (bptr)->successors + (bptr)->successorcount; ++(it))
351
352 #define FOR_EACH_PREDECESSOR(bptr, it) \
353         for ( \
354                 (it) = (bptr)->predecessors; \
355                 (it) != (bptr)->predecessors + ((bptr)->predecessorcount < 0 ? 0 : (bptr)->predecessorcount); \
356                 ++(it) \
357         )
358
359 #define FOR_EACH_INSTRUCTION(bptr, it) \
360         for ((it) = (bptr)->iinstr; (it) != (bptr)->iinstr + (bptr)->icount; ++(it))
361
362 #define FOR_EACH_INSTRUCTION_REV(bptr, it) \
363         for ((it) = (bptr)->iinstr + (bptr)->icount - 1; (it) != (bptr)->iinstr - 1; --(it))
364
365 #if defined(ENABLE_SSA)
366
367 #define FOR_EACH_EXHANDLER(bptr, it) \
368         for ((it) = (bptr)->exhandlers; (it) != (bptr)->exhandlers + (bptr)->exhandlercount; ++(it))
369
370 #define FOR_EACH_EXPREDECESSOR(bptr, it) \
371         for ((it) = (bptr)->expredecessors; (it) != (bptr)->expredecessors + (bptr)->expredecessorcount; ++(it))
372
373 #endif
374
375 /* [+]...the javalocals array: This array is indexed by the javaindex (the    */
376 /*       local variable index ocurring in the original bytecode). An element  */
377 /*       javalocals[javaindex] encodes where to find the contents of the      */
378 /*       original variable at this point in the program.                      */
379 /*       There are three cases for javalocals[javaindex]:                     */
380 /*           >= 0.......it's an index into the jd->var array, where the       */
381 /*                      CACAO variable corresponding to the original local    */
382 /*                      can be found.                                         */
383 /*           UNUSED.....the original variable is not live at this point       */
384 /*           < UNUSED...the original variable contains a returnAddress at     */
385 /*                      this point. The number of the block to return to can  */
386 /*                      be calculated using RETADDR_FROM_JAVALOCAL:           */
387 /*                                                                            */
388 /*                      javalocals[javaindex] == JAVALOCAL_FROM_RETADDR(nr)   */
389 /*                      RETADDR_FROM_JAVALOCAL(javalocals[javaindex]) == nr   */
390
391 #define JAVALOCAL_FROM_RETADDR(nr)  (UNUSED - (1 + (nr)))
392 #define RETADDR_FROM_JAVALOCAL(jl)  (UNUSED - (1 + (jl)))
393
394
395 /* Macro for initializing newly allocated basic block's. It does not
396    need to zero fields, as we zero out the whole basic block array. */
397
398 #define BASICBLOCK_INIT(bptr,m)                        \
399         do {                                               \
400                 bptr->mpc    = -1;                             \
401                 bptr->flags  = -1;                             \
402                 bptr->type   = BBTYPE_STD;                     \
403                 bptr->method = (m);                            \
404         } while (0)
405
406 static inline bool basicblock_reached(const basicblock *bptr) {
407         return (bptr->flags >= BBREACHED);
408 }
409
410
411
412 /***************************** register types *********************************/
413
414 #define REG_RES   0         /* reserved register for OS or code generator     */
415 #define REG_RET   1         /* return value register                          */
416 #define REG_EXC   2         /* exception value register                       */
417 #define REG_SAV   3         /* (callee) saved register                        */
418 #define REG_TMP   4         /* scratch temporary register (caller saved)      */
419 #define REG_ARG   5         /* argument register (caller saved)               */
420
421 #define REG_END   -1        /* last entry in tables                           */
422  
423 #define PARAMMODE_NUMBERED  0 
424 #define PARAMMODE_STUFFED   1
425
426
427 /* function prototypes ********************************************************/
428
429 #ifdef __cplusplus
430 extern "C" {
431 #endif
432
433 /* compiler initialisation */
434 void jit_init(void);
435
436 /* compiler finalisation */
437 void jit_close(void);
438
439 /* create a new jitdata */
440 jitdata *jit_jitdata_new(methodinfo *m);
441
442 /* compile a method with jit compiler */
443 u1 *jit_compile(methodinfo *m);
444 u1 *jit_recompile(methodinfo *m);
445
446 void jit_invalidate_code(methodinfo *m);
447 codeinfo *jit_get_current_code(methodinfo *m);
448 void jit_request_optimization(methodinfo *m);
449
450 /* patch the method entrypoint */
451 #if !defined(JIT_COMPILER_VIA_SIGNAL)
452 void *jit_asm_compile(methodinfo *m, void* mptr, void* sp, void* ra);
453 #endif
454 void *jit_compile_handle(methodinfo *m, void *pv, void *ra, void *mptr);
455
456 s4 jit_complement_condition(s4 opcode);
457
458 void jit_renumber_basicblocks(jitdata *jd);
459 #if !defined(NDEBUG)
460 void jit_check_basicblock_numbers(jitdata *jd);
461 #endif
462
463
464 /* machine dependent functions ************************************************/
465
466 #if defined(ENABLE_JIT)
467 void md_init(void);
468 #endif
469
470 #if defined(ENABLE_INTRP)
471 void intrp_md_init(void);
472 #endif
473
474 void *md_jit_method_patch_address(void *pv, void *ra, void *mptr);
475
476 #ifdef __cplusplus
477 }
478 #endif
479
480 #endif // _JIT_HPP
481
482
483 /*
484  * These are local overrides for various environment variables in Emacs.
485  * Please do not remove this and leave it at the end of the file, where
486  * Emacs will automagically detect them.
487  * ---------------------------------------------------------------------
488  * Local variables:
489  * mode: c++
490  * indent-tabs-mode: t
491  * c-basic-offset: 4
492  * tab-width: 4
493  * End:
494  * vim:noexpandtab:sw=4:ts=4:
495  */