Merged with tip.
[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 instruction instruction;
34 typedef struct insinfo_inline insinfo_inline;
35 typedef struct exception_entry exception_entry;
36
37
38 #include "config.h"
39 #include "vm/types.h"
40
41 #include "toolbox/chain.h"
42
43 #include "vm/global.h"
44 #include "vm/resolve.h"
45
46 #include "vm/jit/codegen-common.h"
47 #include "vm/jit/reg.h"
48 #include "vm/jit/replace.h"
49 #include "vm/jit/stack.h"
50 #include "vm/jit/stacktrace.h"
51
52 #if defined(ENABLE_INLINING)
53 # include "vm/jit/inline/inline.h"
54 #endif
55
56 #include "vm/jit/ir/bytecode.h"
57
58 #if defined(ENABLE_LOOP)
59 # include "vm/jit/loop/loop.h"
60 #endif
61 #if defined(ENABLE_SSA) 
62 # include "vm/jit/optimizing/lsra.h"
63 #endif
64 #if defined(ENABLE_LSRA)
65 # include "vm/jit/allocator/lsra.h"
66 #endif
67
68 #include "vm/jit/verify/typeinfo.h"
69
70 #include "vmcore/method.h"
71 #include "vmcore/references.h"
72
73 #if defined(ENABLE_STATISTICS)
74 # include "vmcore/statistics.h"
75 #endif
76
77
78 /* common jit/codegen macros **************************************************/
79
80 #if defined(ENABLE_STATISTICS)
81 # define COUNT(x)        (x)++
82 # define COUNT_SPILLS             /* use COUNT_(READ|WRITE)_SPILLS instead */
83 # define COUNT_READ_SPILLS(var) \
84         switch(var->type) { \
85         case TYPE_FLT: count_spills_read_flt++; break; \
86         case TYPE_DBL: count_spills_read_dbl++; break; \
87         default: count_spills_read_ila++; break; \
88         }
89
90 # define COUNT_WRITE_SPILLS(var) \
91         switch(var->type) { \
92         case TYPE_FLT: count_spills_write_flt++; break; \
93         case TYPE_DBL: count_spills_write_dbl++; break; \
94         default: count_spills_write_ila++; break; \
95         }
96
97 #else
98 # define COUNT(x)                /* nothing */
99 # define COUNT_SPILLS            /* nothing */
100 # define COUNT_READ_SPILLS(x)    /* nothing */
101 # define COUNT_WRITE_SPILLS(x)   /* nothing */
102 #endif
103
104 typedef struct interface_info interface_info;
105
106 struct interface_info {
107         s4 flags;
108         s4 regoff;
109 };
110
111
112 /* jitdata ********************************************************************/
113
114 struct jitdata {
115         methodinfo      *m;               /* methodinfo of the method compiled    */
116         codeinfo        *code;
117         codegendata     *cd;
118         registerdata    *rd;
119 #if defined(ENABLE_LOOP)
120         loopdata        *ld;
121 #endif
122 #if defined(ENABLE_SSA) || defined(ENABLE_LSRA)
123         lsradata        *ls;
124 #endif
125
126         u4               flags;           /* contains JIT compiler flags          */
127
128         instruction     *instructions;    /* ICMDs, valid between parse and stack */
129         basicblock      *basicblocks;     /* start of basic block list            */
130         stackelement_t  *stack;           /* XXX should become stack.c internal   */
131         s4               instructioncount;/* XXX remove this?                     */
132         s4               basicblockcount; /* number of basic blocks               */
133         s4               stackcount;      /* number of stackelements to allocate  */
134                                       /* (passed from parse to stack)         */
135
136         varinfo         *var;             /* array of variables                   */
137         s4               vartop;          /* next free index in var array         */
138
139         s4               varcount;        /* number of variables in var array     */
140         s4               localcount;      /* number of locals at start of var ar. */
141     s4              *local_map;       /* map for renaming (de-coallescing)    */
142                                          /* locals and keeping the coalescing info for simplereg. */
143                          /* local_map[javaindex * 5 + type] =                     */
144                          /*     >= 0......index into jd->var, or                  */
145                                          /*     UNUSED....this (javaindex,type) pair is not used  */
146
147         s4              *reverselocalmap; /* map from CACAO varindex to javaindex */
148                                           /* (varindex must be < localcount)      */
149
150         s4               maxlocals;       /* max. number of javalocals            */
151
152         interface_info  *interface_map;   /* interface variables (for simplereg)  */
153         s4               maxinterfaces;   /* max. number of interface variables   */
154
155         s4               exceptiontablelength; /* exceptiontable length           */
156         exception_entry *exceptiontable;       /* the exceptiontable              */
157
158         basicblock      *returnblock;          /* block containing the *RETURN    */
159                                                /* (only use if returncount==1)    */
160         s4               returncount;          /* number of return instructions   */
161         bool             branchtoentry;        /* true if first block is a target */
162         bool             branchtoend;          /* true if end dummy is a target   */
163 };
164
165 #define FOR_EACH_BASICBLOCK(jd, it) \
166         for ((it) = (jd)->basicblocks; (it) != NULL; (it) = (it)->next)
167
168 #define UNUSED                     -1
169
170 #define JITDATA_FLAG_PARSE               0x00000001
171 #define JITDATA_FLAG_VERIFY              0x00000002
172
173 #define JITDATA_FLAG_INSTRUMENT          0x00000004
174
175 #define JITDATA_FLAG_IFCONV              0x00000008
176 #define JITDATA_FLAG_REORDER             0x00000010
177 #define JITDATA_FLAG_INLINE              0x00000020
178
179 #define JITDATA_FLAG_COUNTDOWN           0x00000100
180
181 #define JITDATA_FLAG_SHOWINTERMEDIATE    0x20000000
182 #define JITDATA_FLAG_SHOWDISASSEMBLE     0x40000000
183 #define JITDATA_FLAG_VERBOSECALL         0x80000000
184
185
186 #define JITDATA_HAS_FLAG_PARSE(jd) \
187     ((jd)->flags & JITDATA_FLAG_PARSE)
188
189 #define JITDATA_HAS_FLAG_VERIFY(jd) \
190     ((jd)->flags & JITDATA_FLAG_VERIFY)
191
192 #define JITDATA_HAS_FLAG_INSTRUMENT(jd) \
193     ((jd)->flags & JITDATA_FLAG_INSTRUMENT)
194
195 #define JITDATA_HAS_FLAG_IFCONV(jd) \
196     ((jd)->flags & JITDATA_FLAG_IFCONV)
197
198 #define JITDATA_HAS_FLAG_REORDER(jd) \
199     ((jd)->flags & JITDATA_FLAG_REORDER)
200
201 #define JITDATA_HAS_FLAG_INLINE(jd) \
202     ((jd)->flags & JITDATA_FLAG_INLINE)
203
204 #define JITDATA_HAS_FLAG_COUNTDOWN(jd) \
205     ((jd)->flags & JITDATA_FLAG_COUNTDOWN)
206
207 #define JITDATA_HAS_FLAG_SHOWINTERMEDIATE(jd) \
208     ((jd)->flags & JITDATA_FLAG_SHOWINTERMEDIATE)
209
210 #define JITDATA_HAS_FLAG_SHOWDISASSEMBLE(jd) \
211     ((jd)->flags & JITDATA_FLAG_SHOWDISASSEMBLE)
212
213 #define JITDATA_HAS_FLAG_VERBOSECALL(jd) \
214     ((jd)->flags & JITDATA_FLAG_VERBOSECALL)
215
216
217 /* exception_entry ************************************************************/
218
219 struct exception_entry {
220         basicblock           *start;
221         basicblock           *end;
222         basicblock           *handler;
223         classref_or_classinfo catchtype; /* catchtype of exc. (NULL == catchall)  */
224         exception_entry      *next;      /* next in list of exceptions when       */
225                                                                          /* loops are copied                      */
226         exception_entry      *down;      /* next exception_entry                  */
227 };
228
229
230 /* macros for accessing variables *********************************************
231  
232    Use VAROP for s1, s2, s3 and dst operands (eg. VAROP(iptr->s1)),
233    use VAR if you have the variable index (eg. VAR(iptr->sx.s23.s2.args[0])).
234
235 ******************************************************************************/
236
237 #define VAROP(v) (jd->var + (v).varindex)
238 #define VAR(i)   (jd->var + (i))
239
240 static inline bool var_is_local(const jitdata *jd, s4 i) {
241         return (i < jd->localcount);
242 }
243
244 static inline bool var_is_prealloc(const jitdata *jd, s4 i) {
245         return ((i >= jd->localcount) && (jd->var[i].flags & PREALLOC));
246 }
247
248 static inline bool var_is_inout(const jitdata *jd, s4 i) {
249         const varinfo *v = jd->var + i;
250         return (
251                 (i >= jd->localcount) && (
252                         (!(v->flags & PREALLOC) && (v->flags & INOUT)) ||
253                         /* special case of TYPE_RET, used with JSR */
254                         ((v->flags & PREALLOC) && (v->flags & INOUT) && (v->type == TYPE_RET))
255                 )
256         );
257 }
258
259 static inline bool var_is_temp(const jitdata *jd, s4 i) {
260         const varinfo *v = jd->var + i;
261         return ((i >= jd->localcount) && !(v->flags & PREALLOC) && !(v->flags & INOUT));
262 }
263
264 static inline bool var_is_saved(const jitdata *jd, s4 i) {
265         return (jd->var[i].flags & SAVEDVAR);
266 }
267
268
269 /**************************** instruction structure ***************************/
270
271 /* branch_target_t: used in TABLESWITCH tables */
272
273 typedef union {
274     s4                         insindex; /* used in parse                     */
275     basicblock                *block;    /* valid after parse                 */
276 } branch_target_t;
277
278 /* lookup_target_t: used in LOOKUPSWITCH tables */
279
280 typedef struct {
281     s4                         value;    /* case value                        */
282     branch_target_t            target;   /* branch target, see above          */
283 } lookup_target_t;
284
285 /*** s1 operand ***/
286
287 typedef union {
288         s4                         varindex;
289     s4                         argcount;
290 } s1_operand_t;
291
292 /*** s2 operand ***/
293
294 typedef union {
295         s4                         varindex;
296         s4                        *args;
297     classref_or_classinfo      c;
298     unresolved_class          *uc;
299     ptrint                     constval;         /* for PUT*CONST             */
300     s4                         tablelow;         /* for TABLESWITCH           */
301     u4                         lookupcount;      /* for LOOKUPSWITCH          */
302         s4                         retaddrnr;        /* for ASTORE                */
303         instruction              **iargs;            /* for PHI                   */
304 } s2_operand_t;
305
306 /*** s3 operand ***/
307
308 typedef union {
309         s4                         varindex;
310     ptrint                     constval;
311     classref_or_classinfo      c;
312     constant_FMIref           *fmiref;
313     unresolved_method         *um;
314     unresolved_field          *uf;
315     insinfo_inline            *inlineinfo;       /* for INLINE_START/END      */
316     s4                         tablehigh;        /* for TABLESWITCH           */
317     branch_target_t            lookupdefault;    /* for LOOKUPSWITCH          */
318     branch_target_t            jsrtarget;        /* for JSR                   */
319         s4                         javaindex;        /* for *STORE                */
320     struct builtintable_entry *bte;
321 } s3_operand_t;
322
323 /*** val operand ***/
324
325 typedef union {
326     s4                        i;
327     s8                        l;
328     float                     f;
329     double                    d;
330     void                     *anyptr;
331     java_handle_t            *stringconst;       /* for ACONST with string    */
332     classref_or_classinfo     c;                 /* for ACONST with class     */
333 } val_operand_t;
334
335 /*** dst operand ***/
336
337 typedef union {
338         s4                         varindex;
339     basicblock                *block;       /* valid after parse              */
340     branch_target_t           *table;       /* for TABLESWITCH                */
341     lookup_target_t           *lookup;      /* for LOOKUPSWITCH               */
342     s4                         insindex;    /* used in parse                  */
343 } dst_operand_t;
344
345 /*** flags (32 bits) ***/
346
347 #define INS_FLAG_BASICBLOCK    0x01    /* marks a basic block start           */
348 #define INS_FLAG_UNRESOLVED    0x02    /* contains unresolved field/meth/class*/
349 #define INS_FLAG_CLASS         0x04    /* for ACONST, PUT*CONST with class    */
350 #define INS_FLAG_ARRAY         0x08    /* for CHECKCAST/INSTANCEOF with array */
351 #define INS_FLAG_CHECK         0x10    /* for *ALOAD|*ASTORE: check index     */
352                                        /* for BUILTIN: check exception        */
353 #define INS_FLAG_KILL_PREV     0x04    /* for *STORE, invalidate prev local   */
354 #define INS_FLAG_KILL_NEXT     0x08    /* for *STORE, invalidate next local   */
355 #define INS_FLAG_RETADDR       0x10    /* for ASTORE: op is a returnAddress   */
356
357 #define INS_FLAG_ID_SHIFT      5
358 #define INS_FLAG_ID_MASK       (~0 << INS_FLAG_ID_SHIFT)
359
360 typedef union {
361     u4                  bits;
362 } flags_operand_t;
363
364 /*** instruction ***/
365
366 /* The instruction format for the intermediate representation: */
367
368 struct instruction {
369     u2                      opc;    /* opcode       */
370     u2                      line;   /* line number  */
371 #if SIZEOF_VOID_P == 8
372     flags_operand_t         flags;  /* 4 bytes      */
373 #endif
374     s1_operand_t            s1;     /* pointer-size */
375     union {
376         struct {
377             s2_operand_t    s2;     /* pointer-size */
378             s3_operand_t    s3;     /* pointer-size */
379         } s23;                      /*     XOR      */
380         val_operand_t       val;    /*  long-size   */
381     } sx;
382     dst_operand_t           dst;    /* pointer-size */
383 #if SIZEOF_VOID_P == 4
384     flags_operand_t         flags;  /* 4 bytes      */
385 #endif
386 #if defined(ENABLE_ESCAPE_REASON)
387         void *escape_reasons;
388 #endif
389 };
390
391
392 #define INSTRUCTION_STARTS_BASICBLOCK(iptr) \
393         ((iptr)->flags.bits & INS_FLAG_BASICBLOCK)
394
395 #define INSTRUCTION_IS_RESOLVED(iptr) \
396         (!((iptr)->flags.bits & INS_FLAG_UNRESOLVED))
397
398 #define INSTRUCTION_IS_UNRESOLVED(iptr) \
399         ((iptr)->flags.bits & INS_FLAG_UNRESOLVED)
400
401 #define INSTRUCTION_MUST_CHECK(iptr) \
402         ((iptr)->flags.bits & INS_FLAG_CHECK)
403
404 #define INSTRUCTION_GET_FIELDREF(iptr,fref) \
405         do { \
406                 if (iptr->flags.bits & INS_FLAG_UNRESOLVED) \
407                         fref = iptr->sx.s23.s3.uf->fieldref; \
408                 else \
409                         fref = iptr->sx.s23.s3.fmiref; \
410         } while (0)
411
412 #define INSTRUCTION_GET_METHODREF(iptr,mref) \
413         do { \
414                 if (iptr->flags.bits & INS_FLAG_UNRESOLVED) \
415                         mref = iptr->sx.s23.s3.um->methodref; \
416                 else \
417                         mref = iptr->sx.s23.s3.fmiref; \
418         } while (0)
419
420 #define INSTRUCTION_GET_METHODDESC(iptr, md) \
421         do { \
422                 if (iptr->flags.bits & INS_FLAG_UNRESOLVED) \
423                         md = iptr->sx.s23.s3.um->methodref->parseddesc.md; \
424                 else \
425                         md = iptr->sx.s23.s3.fmiref->parseddesc.md; \
426         } while (0)
427
428 /* additional info structs for special instructions ***************************/
429
430 /* for ICMD_INLINE_START and ICMD_INLINE_END */
431
432 struct insinfo_inline {
433         /* fields copied from the inlining tree ----------------------------------*/
434         insinfo_inline *parent;     /* insinfo of the surrounding inlining, if any*/
435         methodinfo     *method;     /* the inlined method starting/ending here    */
436         methodinfo     *outer;      /* the outer method suspended/resumed here    */
437         s4              synclocal;      /* local index used for synchronization   */
438         bool            synchronize;    /* true if synchronization is needed      */
439         s4              throughcount;   /* total # of pass-through variables      */
440         s4              paramcount;     /* number of parameters of original call  */
441         s4              stackvarscount; /* source stackdepth at INLINE_START      */
442         s4             *stackvars;      /* stack vars at INLINE_START             */
443
444         /* fields set by inlining ------------------------------------------------*/
445         s4         *javalocals_start; /* javalocals at start of inlined body      */
446         s4         *javalocals_end;   /* javalocals after inlined body            */
447
448         /* fields set by replacement point creation ------------------------------*/
449 #if defined(ENABLE_REPLACEMENT)
450         rplpoint   *rp;             /* replacement point at INLINE_START          */
451 #endif
452
453         /* fields set by the codegen ---------------------------------------------*/
454         s4          startmpc;       /* machine code offset of start of inlining   */
455 };
456
457
458 /* basicblock *****************************************************************/
459
460 /* flags */
461
462 #define BBDELETED            -2
463 #define BBUNDEF              -1
464 #define BBREACHED            0
465 #define BBFINISHED           1
466
467 #define BBTYPECHECK_UNDEF    2
468 #define BBTYPECHECK_REACHED  3
469
470 #define BBTYPE_STD           0  /* standard basic block type                  */
471 #define BBTYPE_EXH           1  /* exception handler basic block type         */
472 #define BBTYPE_SBR           2  /* subroutine basic block type                */
473
474 #define BBFLAG_REPLACEMENT   0x01  /* put a replacement point at the start    */
475
476 /* XXX basicblock wastes quite a lot of memory by having four flag fields     */
477 /* (flags, bitflags, type and lflags). Probably the last three could be       */
478 /* combined without loss of efficiency. The first one could be combined with  */
479 /* the others by using bitfields.                                             */
480
481 /* XXX "flags" should probably be called "state", as it is an integer state   */
482
483 struct basicblock {
484         s4            nr;           /* basic block number                         */
485         s4            flags;        /* used during stack analysis, init with -1   */
486         s4            bitflags;     /* OR of BBFLAG_... constants, init with 0    */
487         s4            type;         /* basic block type (std, xhandler, subroutine*/
488         s4            lflags;       /* used during loop copying, init with 0      */
489
490         s4            icount;       /* number of intermediate code instructions   */
491         instruction  *iinstr;       /* pointer to intermediate code instructions  */
492
493         varinfo      *inlocals;     /* copy of locals on block entry              */
494         s4           *javalocals;   /* map from java locals to cacao variables[+] */
495         s4           *invars;       /* array of in-variables at begin of block    */
496         s4           *outvars;      /* array of out-variables at end of block     */
497         s4            indepth;      /* stack depth at begin of basic block        */
498         s4            outdepth;     /* stack depth end of basic block             */
499         s4            varstart;     /* index of first non-invar block variable    */
500         s4            varcount;     /* number of non-invar block variables        */
501
502         s4            predecessorcount;
503         s4            successorcount;
504         basicblock  **predecessors; /* array of predecessor basic blocks          */
505         basicblock  **successors;   /* array of successor basic blocks            */
506
507         branchref    *branchrefs;   /* list of branches to be patched             */
508
509         basicblock   *next;         /* used to build a BB list (instead of array) */
510         basicblock   *copied_to;    /* points to the copy of this basic block     */
511                                 /* when loop nodes are copied                 */
512         basicblock   *original;     /* block of which this block is a clone       */
513                                     /* NULL for the original block itself         */
514         methodinfo   *method;       /* method this block belongs to               */
515         insinfo_inline *inlineinfo; /* inlineinfo for the start of this block     */
516
517         s4            mpc;          /* machine code pc at start of block          */
518
519         /* TODO: those fields are probably usefull for other passes as well. */
520
521 #if defined(ENABLE_SSA)         
522         basicblock   *idom;         /* Immediate dominator, parent in dominator tree */
523         basicblock  **domsuccessors;/* Children in dominator tree                 */
524         s4            domsuccessorcount;
525
526         basicblock  **domfrontier;  /* Dominance frontier                         */
527         s4            domfrontiercount;
528
529         basicblock  **exhandlers;   /* Exception handlers for this block */
530         s4            exhandlercount;
531         basicblock  **expredecessors; /* Blocks this block is exception handler for */
532         s4            expredecessorcount;
533         s4            exouts;       /* Number of exceptional exits */
534
535         instruction  *phis;         /* Phi functions */
536         s4            phicount;     /* Number of phi functions */
537
538         void         *vp;           /* Freely used by different passes            */
539 #endif
540 };
541
542 #define FOR_EACH_SUCCESSOR(bptr, it) \
543         for ((it) = (bptr)->successors; (it) != (bptr)->successors + (bptr)->successorcount; ++(it))
544
545 #define FOR_EACH_PREDECESSOR(bptr, it) \
546         for ( \
547                 (it) = (bptr)->predecessors; \
548                 (it) != (bptr)->predecessors + ((bptr)->predecessorcount < 0 ? 0 : (bptr)->predecessorcount); \
549                 ++(it) \
550         )
551
552 #define FOR_EACH_INSTRUCTION(bptr, it) \
553         for ((it) = (bptr)->iinstr; (it) != (bptr)->iinstr + (bptr)->icount; ++(it))
554
555 #define FOR_EACH_INSTRUCTION_REV(bptr, it) \
556         for ((it) = (bptr)->iinstr + (bptr)->icount - 1; (it) != (bptr)->iinstr - 1; --(it))
557
558 #if defined(ENABLE_SSA)
559
560 #define FOR_EACH_EXHANDLER(bptr, it) \
561         for ((it) = (bptr)->exhandlers; (it) != (bptr)->exhandlers + (bptr)->exhandlercount; ++(it))
562
563 #define FOR_EACH_EXPREDECESSOR(bptr, it) \
564         for ((it) = (bptr)->expredecessors; (it) != (bptr)->expredecessors + (bptr)->expredecessorcount; ++(it))
565
566 #endif
567
568 /* [+]...the javalocals array: This array is indexed by the javaindex (the    */
569 /*       local variable index ocurring in the original bytecode). An element  */
570 /*       javalocals[javaindex] encodes where to find the contents of the      */
571 /*       original variable at this point in the program.                      */
572 /*       There are three cases for javalocals[javaindex]:                     */
573 /*           >= 0.......it's an index into the jd->var array, where the       */
574 /*                      CACAO variable corresponding to the original local    */
575 /*                      can be found.                                         */
576 /*           UNUSED.....the original variable is not live at this point       */
577 /*           < UNUSED...the original variable contains a returnAddress at     */
578 /*                      this point. The number of the block to return to can  */
579 /*                      be calculated using RETADDR_FROM_JAVALOCAL:           */
580 /*                                                                            */
581 /*                      javalocals[javaindex] == JAVALOCAL_FROM_RETADDR(nr)   */
582 /*                      RETADDR_FROM_JAVALOCAL(javalocals[javaindex]) == nr   */
583
584 #define JAVALOCAL_FROM_RETADDR(nr)  (UNUSED - (1 + (nr)))
585 #define RETADDR_FROM_JAVALOCAL(jl)  (UNUSED - (1 + (jl)))
586
587
588 /* Macro for initializing newly allocated basic block's. It does not
589    need to zero fields, as we zero out the whole basic block array. */
590
591 #define BASICBLOCK_INIT(bptr,m)                        \
592         do {                                               \
593                 bptr->mpc    = -1;                             \
594                 bptr->flags  = -1;                             \
595                 bptr->type   = BBTYPE_STD;                     \
596                 bptr->method = (m);                            \
597         } while (0)
598
599 static inline bool basicblock_reached(const basicblock *bptr) {
600         return (bptr->flags >= BBREACHED);
601 }
602
603 /* data-flow constants for the ICMD table ************************************/
604
605 #define DF_0_TO_0      0
606 #define DF_1_TO_0      1
607 #define DF_2_TO_0      2
608 #define DF_3_TO_0      3
609
610 #define DF_DST_BASE    4      /* from this value on, iptr->dst is a variable */
611
612 #define DF_0_TO_1      (DF_DST_BASE + 0)
613 #define DF_1_TO_1      (DF_DST_BASE + 1)
614 #define DF_2_TO_1      (DF_DST_BASE + 2)
615 #define DF_3_TO_1      (DF_DST_BASE + 3)
616 #define DF_N_TO_1      (DF_DST_BASE + 4)
617
618 #define DF_INVOKE      (DF_DST_BASE + 5)
619 #define DF_BUILTIN     (DF_DST_BASE + 6)
620
621 #define DF_COPY        (DF_DST_BASE + 7)
622 #define DF_MOVE        (DF_DST_BASE + 8)
623
624 #define DF_DUP         -1
625 #define DF_DUP_X1      -1
626 #define DF_DUP_X2      -1
627 #define DF_DUP2        -1
628 #define DF_DUP2_X1     -1
629 #define DF_DUP2_X2     -1
630 #define DF_SWAP        -1
631
632 /* special data-flow recognized by verify/generate.pl: */
633 #define DF_LOAD        DF_COPY
634 #define DF_STORE       DF_MOVE
635 #define DF_IINC        DF_1_TO_1
636 #define DF_POP         DF_1_TO_0
637 #define DF_POP2        DF_2_TO_0
638
639
640 /* control-flow constants for the ICMD table *********************************/
641
642 #define CF_NORMAL      0
643 #define CF_IF          1
644
645 #define CF_END_BASE    2  /* from here on, they mark the end of a superblock */
646
647 #define CF_END         (CF_END_BASE + 0)
648 #define CF_GOTO        (CF_END_BASE + 1)
649 #define CF_TABLE       (CF_END_BASE + 2)
650 #define CF_LOOKUP      (CF_END_BASE + 3)
651 #define CF_JSR         (CF_END_BASE + 4)
652 #define CF_RET         (CF_END_BASE + 5)
653
654
655 /* flag constants for the ICMD table *****************************************/
656
657 #define ICMDTABLE_PEI    0x0001               /* ICMD may throw an exception */
658 #define ICMDTABLE_CALLS  0x0002     /* needs registers to be saved, may call */
659
660
661 /* ICMD table entry **********************************************************/
662
663 typedef struct icmdtable_entry_t icmdtable_entry_t;
664
665 struct icmdtable_entry_t {
666 #if !defined(NDEBUG)
667         char *name;                                /* name, without ICMD_ prefix */
668 #endif
669         s4    dataflow;                             /* a DF_ constant, see above */
670         s4    controlflow;                          /* a CF_ constant, see above */
671         s4    flags;                        /* a combination of ICMDTABLE_ flags */
672 };
673
674
675 /* the ICMD table ************************************************************/
676
677 extern icmdtable_entry_t icmd_table[256];
678
679
680 /********** JavaVM operation codes (sorted) and instruction lengths ***********/
681
682 enum {
683         ICMD_NOP               = BC_nop,
684
685         ICMD_ACONST            = BC_aconst_null,
686
687         ICMD_CHECKNULL         = 2,
688
689         ICMD_ICONST            = BC_iconst_0,
690
691         /* 3 */
692         /* 4 */
693
694         ICMD_IDIVPOW2          = 5,
695         ICMD_LDIVPOW2          = 6,
696
697         /* 7 */
698         /* 8 */
699
700         ICMD_LCONST            = BC_lconst_0,
701
702         ICMD_LCMPCONST         = 10,
703
704         ICMD_FCONST            = BC_fconst_0,
705
706         /* 12 */
707         /* 13 */
708
709         ICMD_DCONST            = BC_dconst_0,
710
711         ICMD_COPY              = 15,
712         ICMD_MOVE              = 16,
713
714         /* 17 */
715         /* 18 */
716         /* 19 */
717         /* 20 */
718
719         /* Order of LOAD instructions must be equal to order of TYPE_*
720            defines. */
721
722         ICMD_ILOAD            = BC_iload,
723         ICMD_LLOAD            = BC_lload,
724         ICMD_FLOAD            = BC_fload,
725         ICMD_DLOAD            = BC_dload,
726         ICMD_ALOAD            = BC_aload,
727
728         ICMD_IADDCONST        = 26,
729         ICMD_ISUBCONST        = 27,
730         ICMD_IMULCONST        = 28,
731         ICMD_IANDCONST        = 29,
732         ICMD_IORCONST         = 30,
733         ICMD_IXORCONST        = 31,
734
735         ICMD_ISHLCONST        = 32,
736         ICMD_ISHRCONST        = 33,
737         ICMD_IUSHRCONST       = 34,
738
739         ICMD_IREMPOW2         = 35,
740
741         ICMD_LADDCONST        = 36,
742         ICMD_LSUBCONST        = 37,
743         ICMD_LMULCONST        = 38,
744         ICMD_LANDCONST        = 39,
745         ICMD_LORCONST         = 40,
746         ICMD_LXORCONST        = 41,
747
748         ICMD_LSHLCONST        = 42,
749         ICMD_LSHRCONST        = 43,
750         ICMD_LUSHRCONST       = 44,
751
752         ICMD_LREMPOW2         = 45,
753
754         ICMD_IALOAD           = BC_iaload,
755         ICMD_LALOAD           = BC_laload,
756         ICMD_FALOAD           = BC_faload,
757         ICMD_DALOAD           = BC_daload,
758         ICMD_AALOAD           = BC_aaload,
759         ICMD_BALOAD           = BC_baload,
760         ICMD_CALOAD           = BC_caload,
761         ICMD_SALOAD           = BC_saload,
762
763         /* Order of STORE instructions must be equal to order of TYPE_*
764            defines. */
765
766         ICMD_ISTORE           = BC_istore,
767         ICMD_LSTORE           = BC_lstore,
768         ICMD_FSTORE           = BC_fstore,
769         ICMD_DSTORE           = BC_dstore,
770         ICMD_ASTORE           = BC_astore,
771
772         ICMD_IF_LEQ           = 59,
773         ICMD_IF_LNE           = 60,
774         ICMD_IF_LLT           = 61,
775         ICMD_IF_LGE           = 62,
776         ICMD_IF_LGT           = 63,
777         ICMD_IF_LLE           = 64,
778
779         ICMD_IF_LCMPEQ        = 65,
780         ICMD_IF_LCMPNE        = 66,
781         ICMD_IF_LCMPLT        = 67,
782         ICMD_IF_LCMPGE        = 68,
783         ICMD_IF_LCMPGT        = 69,
784         ICMD_IF_LCMPLE        = 70,
785
786         /* 71 */
787         /* 72 */
788         /* 73 */
789         /* 74 */
790         /* 75 */
791         /* 76 */
792         /* 77 */
793         /* 78 */
794
795         ICMD_IASTORE          = BC_iastore,
796         ICMD_LASTORE          = BC_lastore,
797         ICMD_FASTORE          = BC_fastore,
798         ICMD_DASTORE          = BC_dastore,
799         ICMD_AASTORE          = BC_aastore,
800         ICMD_BASTORE          = BC_bastore,
801         ICMD_CASTORE          = BC_castore,
802         ICMD_SASTORE          = BC_sastore,
803
804         ICMD_POP              = BC_pop,
805         ICMD_POP2             = BC_pop2,
806         ICMD_DUP              = BC_dup,
807         ICMD_DUP_X1           = BC_dup_x1,
808         ICMD_DUP_X2           = BC_dup_x2,
809         ICMD_DUP2             = BC_dup2,
810         ICMD_DUP2_X1          = BC_dup2_x1,
811         ICMD_DUP2_X2          = BC_dup2_x2,
812         ICMD_SWAP             = BC_swap,
813
814         ICMD_IADD             = BC_iadd,
815         ICMD_LADD             = BC_ladd,
816         ICMD_FADD             = BC_fadd,
817         ICMD_DADD             = BC_dadd,
818
819         ICMD_ISUB             = BC_isub,
820         ICMD_LSUB             = BC_lsub,
821         ICMD_FSUB             = BC_fsub,
822         ICMD_DSUB             = BC_dsub,
823
824         ICMD_IMUL             = BC_imul,
825         ICMD_LMUL             = BC_lmul,
826         ICMD_FMUL             = BC_fmul,
827         ICMD_DMUL             = BC_dmul,
828
829         ICMD_IDIV             = BC_idiv,
830         ICMD_LDIV             = BC_ldiv,
831         ICMD_FDIV             = BC_fdiv,
832         ICMD_DDIV             = BC_ddiv,
833
834         ICMD_IREM             = BC_irem,
835         ICMD_LREM             = BC_lrem,
836         ICMD_FREM             = BC_frem,
837         ICMD_DREM             = BC_drem,
838
839         ICMD_INEG             = BC_ineg,
840         ICMD_LNEG             = BC_lneg,
841         ICMD_FNEG             = BC_fneg,
842         ICMD_DNEG             = BC_dneg,
843
844         ICMD_ISHL             = BC_ishl,
845         ICMD_LSHL             = BC_lshl,
846         ICMD_ISHR             = BC_ishr,
847         ICMD_LSHR             = BC_lshr,
848         ICMD_IUSHR            = BC_iushr,
849         ICMD_LUSHR            = BC_lushr,
850
851         ICMD_IAND             = BC_iand,
852         ICMD_LAND             = BC_land,
853         ICMD_IOR              = BC_ior,
854         ICMD_LOR              = BC_lor,
855         ICMD_IXOR             = BC_ixor,
856         ICMD_LXOR             = BC_lxor,
857
858         ICMD_IINC             = BC_iinc,
859
860         ICMD_I2L              = BC_i2l,
861         ICMD_I2F              = BC_i2f,
862         ICMD_I2D              = BC_i2d,
863         ICMD_L2I              = BC_l2i,
864         ICMD_L2F              = BC_l2f,
865         ICMD_L2D              = BC_l2d,
866         ICMD_F2I              = BC_f2i,
867         ICMD_F2L              = BC_f2l,
868         ICMD_F2D              = BC_f2d,
869         ICMD_D2I              = BC_d2i,
870         ICMD_D2L              = BC_d2l,
871         ICMD_D2F              = BC_d2f,
872
873         ICMD_INT2BYTE         = BC_int2byte,
874         ICMD_INT2CHAR         = BC_int2char,
875         ICMD_INT2SHORT        = BC_int2short,
876
877         ICMD_LCMP             = BC_lcmp,
878         ICMD_FCMPL            = BC_fcmpl,
879         ICMD_FCMPG            = BC_fcmpg,
880         ICMD_DCMPL            = BC_dcmpl,
881         ICMD_DCMPG            = BC_dcmpg,
882
883         ICMD_IFEQ             = BC_ifeq,
884         ICMD_IFNE             = BC_ifne,
885         ICMD_IFLT             = BC_iflt,
886         ICMD_IFGE             = BC_ifge,
887         ICMD_IFGT             = BC_ifgt,
888         ICMD_IFLE             = BC_ifle,
889
890         ICMD_IF_ICMPEQ        = BC_if_icmpeq,
891         ICMD_IF_ICMPNE        = BC_if_icmpne,
892         ICMD_IF_ICMPLT        = BC_if_icmplt,
893         ICMD_IF_ICMPGE        = BC_if_icmpge,
894         ICMD_IF_ICMPGT        = BC_if_icmpgt,
895         ICMD_IF_ICMPLE        = BC_if_icmple,
896         ICMD_IF_ACMPEQ        = BC_if_acmpeq,
897         ICMD_IF_ACMPNE        = BC_if_acmpne,
898
899         ICMD_GOTO             = BC_goto,
900         ICMD_JSR              = BC_jsr,
901         ICMD_RET              = BC_ret,
902
903         ICMD_TABLESWITCH      = BC_tableswitch,
904         ICMD_LOOKUPSWITCH     = BC_lookupswitch,
905
906         ICMD_IRETURN          = BC_ireturn,
907         ICMD_LRETURN          = BC_lreturn,
908         ICMD_FRETURN          = BC_freturn,
909         ICMD_DRETURN          = BC_dreturn,
910         ICMD_ARETURN          = BC_areturn,
911         ICMD_RETURN           = BC_return,
912
913         ICMD_GETSTATIC        = BC_getstatic,
914         ICMD_PUTSTATIC        = BC_putstatic,
915         ICMD_GETFIELD         = BC_getfield,
916         ICMD_PUTFIELD         = BC_putfield,
917
918         ICMD_INVOKEVIRTUAL    = BC_invokevirtual,
919         ICMD_INVOKESPECIAL    = BC_invokespecial,
920         ICMD_INVOKESTATIC     = BC_invokestatic,
921         ICMD_INVOKEINTERFACE  = BC_invokeinterface,
922
923         /* 186 */
924
925         ICMD_NEW              = BC_new,
926         ICMD_NEWARRAY         = BC_newarray,
927         ICMD_ANEWARRAY        = BC_anewarray,
928
929         ICMD_ARRAYLENGTH      = BC_arraylength,
930
931         ICMD_ATHROW           = BC_athrow,
932
933         ICMD_CHECKCAST        = BC_checkcast,
934         ICMD_INSTANCEOF       = BC_instanceof,
935
936         ICMD_MONITORENTER     = BC_monitorenter,
937         ICMD_MONITOREXIT      = BC_monitorexit,
938
939         /* 196 */
940
941         ICMD_MULTIANEWARRAY   = BC_multianewarray,
942
943         ICMD_IFNULL           = BC_ifnull,
944         ICMD_IFNONNULL        = BC_ifnonnull,
945
946         /* 200 */
947         /* 201 */
948         /* 202 */
949
950         ICMD_IASTORECONST     = 204,
951         ICMD_LASTORECONST     = 205,
952         ICMD_FASTORECONST     = 206,
953         ICMD_DASTORECONST     = 207,
954         ICMD_AASTORECONST     = 208,
955         ICMD_BASTORECONST     = 209,
956         ICMD_CASTORECONST     = 210,
957         ICMD_SASTORECONST     = 211,
958
959         ICMD_PUTSTATICCONST   = 212,
960         ICMD_PUTFIELDCONST    = 213,
961
962         ICMD_IMULPOW2         = 214,
963         ICMD_LMULPOW2         = 215,
964
965         ICMD_GETEXCEPTION     = 249,
966         ICMD_PHI              = 250,
967
968         ICMD_INLINE_START     = 251,        /* instruction before inlined method  */
969         ICMD_INLINE_END       = 252,        /* instruction after inlined method   */
970         ICMD_INLINE_BODY      = 253,        /* start of inlined body              */
971
972         ICMD_BUILTIN          = 255         /* internal opcode                    */
973 };
974
975 /* Additional instruction accessors */
976
977 methoddesc *instruction_call_site(const instruction *iptr);
978
979 static inline bool instruction_has_dst(const instruction *iptr) {
980         if (
981                 (icmd_table[iptr->opc].dataflow == DF_INVOKE) ||
982                 (icmd_table[iptr->opc].dataflow == DF_BUILTIN)
983                 ) {
984                 return instruction_call_site(iptr)->returntype.type != TYPE_VOID;
985         } else {
986                 return icmd_table[iptr->opc].dataflow >= DF_DST_BASE;
987         }
988 }
989
990 /***************************** register types *********************************/
991
992 #define REG_RES   0         /* reserved register for OS or code generator     */
993 #define REG_RET   1         /* return value register                          */
994 #define REG_EXC   2         /* exception value register                       */
995 #define REG_SAV   3         /* (callee) saved register                        */
996 #define REG_TMP   4         /* scratch temporary register (caller saved)      */
997 #define REG_ARG   5         /* argument register (caller saved)               */
998
999 #define REG_END   -1        /* last entry in tables                           */
1000  
1001 #define PARAMMODE_NUMBERED  0 
1002 #define PARAMMODE_STUFFED   1
1003
1004
1005 /* function prototypes ********************************************************/
1006
1007 /* compiler initialisation */
1008 void jit_init(void);
1009
1010 /* compiler finalisation */
1011 void jit_close(void);
1012
1013 /* create a new jitdata */
1014 jitdata *jit_jitdata_new(methodinfo *m);
1015
1016 /* compile a method with jit compiler */
1017 u1 *jit_compile(methodinfo *m);
1018 u1 *jit_recompile(methodinfo *m);
1019
1020 void jit_invalidate_code(methodinfo *m);
1021 codeinfo *jit_get_current_code(methodinfo *m);
1022 void jit_request_optimization(methodinfo *m);
1023
1024 /* patch the method entrypoint */
1025 #if !defined(JIT_COMPILER_VIA_SIGNAL)
1026 u1 *jit_asm_compile(methodinfo *m, u1 *mptr, u1 *sp, u1 *ra);
1027 #endif
1028 void *jit_compile_handle(methodinfo *m, void *pv, void *ra, void *mptr);
1029
1030 s4 jit_complement_condition(s4 opcode);
1031
1032 void jit_renumber_basicblocks(jitdata *jd);
1033 #if !defined(NDEBUG)
1034 void jit_check_basicblock_numbers(jitdata *jd);
1035 #endif
1036
1037
1038 /* machine dependent functions ************************************************/
1039
1040 #if defined(ENABLE_JIT)
1041 void md_init(void);
1042 #endif
1043
1044 #if defined(ENABLE_INTRP)
1045 void intrp_md_init(void);
1046 #endif
1047
1048 void *md_jit_method_patch_address(void *pv, void *ra, void *mptr);
1049
1050 #endif /* _JIT_H */
1051
1052
1053 /*
1054  * These are local overrides for various environment variables in Emacs.
1055  * Please do not remove this and leave it at the end of the file, where
1056  * Emacs will automagically detect them.
1057  * ---------------------------------------------------------------------
1058  * Local variables:
1059  * mode: c
1060  * indent-tabs-mode: t
1061  * c-basic-offset: 4
1062  * tab-width: 4
1063  * End:
1064  * vim:noexpandtab:sw=4:ts=4:
1065  */