448f0cc539d6206c4c5493caef05f0586a6e3f32
[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 };
387
388
389 #define INSTRUCTION_STARTS_BASICBLOCK(iptr) \
390         ((iptr)->flags.bits & INS_FLAG_BASICBLOCK)
391
392 #define INSTRUCTION_IS_RESOLVED(iptr) \
393         (!((iptr)->flags.bits & INS_FLAG_UNRESOLVED))
394
395 #define INSTRUCTION_IS_UNRESOLVED(iptr) \
396         ((iptr)->flags.bits & INS_FLAG_UNRESOLVED)
397
398 #define INSTRUCTION_MUST_CHECK(iptr) \
399         ((iptr)->flags.bits & INS_FLAG_CHECK)
400
401 #define INSTRUCTION_GET_FIELDREF(iptr,fref) \
402         do { \
403                 if (iptr->flags.bits & INS_FLAG_UNRESOLVED) \
404                         fref = iptr->sx.s23.s3.uf->fieldref; \
405                 else \
406                         fref = iptr->sx.s23.s3.fmiref; \
407         } while (0)
408
409 #define INSTRUCTION_GET_METHODREF(iptr,mref) \
410         do { \
411                 if (iptr->flags.bits & INS_FLAG_UNRESOLVED) \
412                         mref = iptr->sx.s23.s3.um->methodref; \
413                 else \
414                         mref = iptr->sx.s23.s3.fmiref; \
415         } while (0)
416
417 #define INSTRUCTION_GET_METHODDESC(iptr, md) \
418         do { \
419                 if (iptr->flags.bits & INS_FLAG_UNRESOLVED) \
420                         md = iptr->sx.s23.s3.um->methodref->parseddesc.md; \
421                 else \
422                         md = iptr->sx.s23.s3.fmiref->parseddesc.md; \
423         } while (0)
424
425 /* additional info structs for special instructions ***************************/
426
427 /* for ICMD_INLINE_START and ICMD_INLINE_END */
428
429 struct insinfo_inline {
430         /* fields copied from the inlining tree ----------------------------------*/
431         insinfo_inline *parent;     /* insinfo of the surrounding inlining, if any*/
432         methodinfo     *method;     /* the inlined method starting/ending here    */
433         methodinfo     *outer;      /* the outer method suspended/resumed here    */
434         s4              synclocal;      /* local index used for synchronization   */
435         bool            synchronize;    /* true if synchronization is needed      */
436         s4              throughcount;   /* total # of pass-through variables      */
437         s4              paramcount;     /* number of parameters of original call  */
438         s4              stackvarscount; /* source stackdepth at INLINE_START      */
439         s4             *stackvars;      /* stack vars at INLINE_START             */
440
441         /* fields set by inlining ------------------------------------------------*/
442         s4         *javalocals_start; /* javalocals at start of inlined body      */
443         s4         *javalocals_end;   /* javalocals after inlined body            */
444
445         /* fields set by replacement point creation ------------------------------*/
446 #if defined(ENABLE_REPLACEMENT)
447         rplpoint   *rp;             /* replacement point at INLINE_START          */
448 #endif
449
450         /* fields set by the codegen ---------------------------------------------*/
451         s4          startmpc;       /* machine code offset of start of inlining   */
452 };
453
454
455 /* basicblock *****************************************************************/
456
457 /* flags */
458
459 #define BBDELETED            -2
460 #define BBUNDEF              -1
461 #define BBREACHED            0
462 #define BBFINISHED           1
463
464 #define BBTYPECHECK_UNDEF    2
465 #define BBTYPECHECK_REACHED  3
466
467 #define BBTYPE_STD           0  /* standard basic block type                  */
468 #define BBTYPE_EXH           1  /* exception handler basic block type         */
469 #define BBTYPE_SBR           2  /* subroutine basic block type                */
470
471 #define BBFLAG_REPLACEMENT   0x01  /* put a replacement point at the start    */
472
473 /* XXX basicblock wastes quite a lot of memory by having four flag fields     */
474 /* (flags, bitflags, type and lflags). Probably the last three could be       */
475 /* combined without loss of efficiency. The first one could be combined with  */
476 /* the others by using bitfields.                                             */
477
478 /* XXX "flags" should probably be called "state", as it is an integer state   */
479
480 struct basicblock {
481         s4            nr;           /* basic block number                         */
482         s4            flags;        /* used during stack analysis, init with -1   */
483         s4            bitflags;     /* OR of BBFLAG_... constants, init with 0    */
484         s4            type;         /* basic block type (std, xhandler, subroutine*/
485         s4            lflags;       /* used during loop copying, init with 0      */
486
487         s4            icount;       /* number of intermediate code instructions   */
488         instruction  *iinstr;       /* pointer to intermediate code instructions  */
489
490         varinfo      *inlocals;     /* copy of locals on block entry              */
491         s4           *javalocals;   /* map from java locals to cacao variables[+] */
492         s4           *invars;       /* array of in-variables at begin of block    */
493         s4           *outvars;      /* array of out-variables at end of block     */
494         s4            indepth;      /* stack depth at begin of basic block        */
495         s4            outdepth;     /* stack depth end of basic block             */
496         s4            varstart;     /* index of first non-invar block variable    */
497         s4            varcount;     /* number of non-invar block variables        */
498
499         s4            predecessorcount;
500         s4            successorcount;
501         basicblock  **predecessors; /* array of predecessor basic blocks          */
502         basicblock  **successors;   /* array of successor basic blocks            */
503
504         branchref    *branchrefs;   /* list of branches to be patched             */
505
506         basicblock   *next;         /* used to build a BB list (instead of array) */
507         basicblock   *copied_to;    /* points to the copy of this basic block     */
508                                 /* when loop nodes are copied                 */
509         basicblock   *original;     /* block of which this block is a clone       */
510                                     /* NULL for the original block itself         */
511         methodinfo   *method;       /* method this block belongs to               */
512         insinfo_inline *inlineinfo; /* inlineinfo for the start of this block     */
513
514         s4            mpc;          /* machine code pc at start of block          */
515
516         /* TODO: those fields are probably usefull for other passes as well. */
517
518 #if defined(ENABLE_SSA)         
519         basicblock   *idom;         /* Immediate dominator, parent in dominator tree */
520         basicblock  **domsuccessors;/* Children in dominator tree                 */
521         s4            domsuccessorcount;
522
523         basicblock  **domfrontier;  /* Dominance frontier                         */
524         s4            domfrontiercount;
525
526         basicblock  **exhandlers;   /* Exception handlers for this block */
527         s4            exhandlercount;
528         basicblock  **expredecessors; /* Blocks this block is exception handler for */
529         s4            expredecessorcount;
530         s4            exouts;       /* Number of exceptional exits */
531
532         instruction  *phis;         /* Phi functions */
533         s4            phicount;     /* Number of phi functions */
534
535         void         *vp;           /* Freely used by different passes            */
536 #endif
537 };
538
539 #define FOR_EACH_SUCCESSOR(bptr, it) \
540         for ((it) = (bptr)->successors; (it) != (bptr)->successors + (bptr)->successorcount; ++(it))
541
542 #define FOR_EACH_PREDECESSOR(bptr, it) \
543         for ( \
544                 (it) = (bptr)->predecessors; \
545                 (it) != (bptr)->predecessors + ((bptr)->predecessorcount < 0 ? 0 : (bptr)->predecessorcount); \
546                 ++(it) \
547         )
548
549 #define FOR_EACH_INSTRUCTION(bptr, it) \
550         for ((it) = (bptr)->iinstr; (it) != (bptr)->iinstr + (bptr)->icount; ++(it))
551
552 #if defined(ENABLE_SSA)
553
554 #define FOR_EACH_EXHANDLER(bptr, it) \
555         for ((it) = (bptr)->exhandlers; (it) != (bptr)->exhandlers + (bptr)->exhandlercount; ++(it))
556
557 #define FOR_EACH_EXPREDECESSOR(bptr, it) \
558         for ((it) = (bptr)->expredecessors; (it) != (bptr)->expredecessors + (bptr)->expredecessorcount; ++(it))
559
560 #endif
561
562 /* [+]...the javalocals array: This array is indexed by the javaindex (the    */
563 /*       local variable index ocurring in the original bytecode). An element  */
564 /*       javalocals[javaindex] encodes where to find the contents of the      */
565 /*       original variable at this point in the program.                      */
566 /*       There are three cases for javalocals[javaindex]:                     */
567 /*           >= 0.......it's an index into the jd->var array, where the       */
568 /*                      CACAO variable corresponding to the original local    */
569 /*                      can be found.                                         */
570 /*           UNUSED.....the original variable is not live at this point       */
571 /*           < UNUSED...the original variable contains a returnAddress at     */
572 /*                      this point. The number of the block to return to can  */
573 /*                      be calculated using RETADDR_FROM_JAVALOCAL:           */
574 /*                                                                            */
575 /*                      javalocals[javaindex] == JAVALOCAL_FROM_RETADDR(nr)   */
576 /*                      RETADDR_FROM_JAVALOCAL(javalocals[javaindex]) == nr   */
577
578 #define JAVALOCAL_FROM_RETADDR(nr)  (UNUSED - (1 + (nr)))
579 #define RETADDR_FROM_JAVALOCAL(jl)  (UNUSED - (1 + (jl)))
580
581
582 /* Macro for initializing newly allocated basic block's. It does not
583    need to zero fields, as we zero out the whole basic block array. */
584
585 #define BASICBLOCK_INIT(bptr,m)                        \
586         do {                                               \
587                 bptr->mpc    = -1;                             \
588                 bptr->flags  = -1;                             \
589                 bptr->type   = BBTYPE_STD;                     \
590                 bptr->method = (m);                            \
591         } while (0)
592
593 static inline bool basicblock_reached(const basicblock *bptr) {
594         return (bptr->flags >= BBREACHED);
595 }
596
597 /* data-flow constants for the ICMD table ************************************/
598
599 #define DF_0_TO_0      0
600 #define DF_1_TO_0      1
601 #define DF_2_TO_0      2
602 #define DF_3_TO_0      3
603
604 #define DF_DST_BASE    4      /* from this value on, iptr->dst is a variable */
605
606 #define DF_0_TO_1      (DF_DST_BASE + 0)
607 #define DF_1_TO_1      (DF_DST_BASE + 1)
608 #define DF_2_TO_1      (DF_DST_BASE + 2)
609 #define DF_3_TO_1      (DF_DST_BASE + 3)
610 #define DF_N_TO_1      (DF_DST_BASE + 4)
611
612 #define DF_INVOKE      (DF_DST_BASE + 5)
613 #define DF_BUILTIN     (DF_DST_BASE + 6)
614
615 #define DF_COPY        (DF_DST_BASE + 7)
616 #define DF_MOVE        (DF_DST_BASE + 8)
617
618 #define DF_DUP         -1
619 #define DF_DUP_X1      -1
620 #define DF_DUP_X2      -1
621 #define DF_DUP2        -1
622 #define DF_DUP2_X1     -1
623 #define DF_DUP2_X2     -1
624 #define DF_SWAP        -1
625
626 /* special data-flow recognized by verify/generate.pl: */
627 #define DF_LOAD        DF_COPY
628 #define DF_STORE       DF_MOVE
629 #define DF_IINC        DF_1_TO_1
630 #define DF_POP         DF_1_TO_0
631 #define DF_POP2        DF_2_TO_0
632
633
634 /* control-flow constants for the ICMD table *********************************/
635
636 #define CF_NORMAL      0
637 #define CF_IF          1
638
639 #define CF_END_BASE    2  /* from here on, they mark the end of a superblock */
640
641 #define CF_END         (CF_END_BASE + 0)
642 #define CF_GOTO        (CF_END_BASE + 1)
643 #define CF_TABLE       (CF_END_BASE + 2)
644 #define CF_LOOKUP      (CF_END_BASE + 3)
645 #define CF_JSR         (CF_END_BASE + 4)
646 #define CF_RET         (CF_END_BASE + 5)
647
648
649 /* flag constants for the ICMD table *****************************************/
650
651 #define ICMDTABLE_PEI    0x0001               /* ICMD may throw an exception */
652 #define ICMDTABLE_CALLS  0x0002     /* needs registers to be saved, may call */
653
654
655 /* ICMD table entry **********************************************************/
656
657 typedef struct icmdtable_entry_t icmdtable_entry_t;
658
659 struct icmdtable_entry_t {
660 #if !defined(NDEBUG)
661         char *name;                                /* name, without ICMD_ prefix */
662 #endif
663         s4    dataflow;                             /* a DF_ constant, see above */
664         s4    controlflow;                          /* a CF_ constant, see above */
665         s4    flags;                        /* a combination of ICMDTABLE_ flags */
666 };
667
668
669 /* the ICMD table ************************************************************/
670
671 extern icmdtable_entry_t icmd_table[256];
672
673
674 /********** JavaVM operation codes (sorted) and instruction lengths ***********/
675
676 enum {
677         ICMD_NOP               = BC_nop,
678
679         ICMD_ACONST            = BC_aconst_null,
680
681         ICMD_CHECKNULL         = 2,
682
683         ICMD_ICONST            = BC_iconst_0,
684
685         /* 3 */
686         /* 4 */
687
688         ICMD_IDIVPOW2          = 5,
689         ICMD_LDIVPOW2          = 6,
690
691         /* 7 */
692         /* 8 */
693
694         ICMD_LCONST            = BC_lconst_0,
695
696         ICMD_LCMPCONST         = 10,
697
698         ICMD_FCONST            = BC_fconst_0,
699
700         /* 12 */
701         /* 13 */
702
703         ICMD_DCONST            = BC_dconst_0,
704
705         ICMD_COPY              = 15,
706         ICMD_MOVE              = 16,
707
708         /* 17 */
709         /* 18 */
710         /* 19 */
711         /* 20 */
712
713         /* Order of LOAD instructions must be equal to order of TYPE_*
714            defines. */
715
716         ICMD_ILOAD            = BC_iload,
717         ICMD_LLOAD            = BC_lload,
718         ICMD_FLOAD            = BC_fload,
719         ICMD_DLOAD            = BC_dload,
720         ICMD_ALOAD            = BC_aload,
721
722         ICMD_IADDCONST        = 26,
723         ICMD_ISUBCONST        = 27,
724         ICMD_IMULCONST        = 28,
725         ICMD_IANDCONST        = 29,
726         ICMD_IORCONST         = 30,
727         ICMD_IXORCONST        = 31,
728
729         ICMD_ISHLCONST        = 32,
730         ICMD_ISHRCONST        = 33,
731         ICMD_IUSHRCONST       = 34,
732
733         ICMD_IREMPOW2         = 35,
734
735         ICMD_LADDCONST        = 36,
736         ICMD_LSUBCONST        = 37,
737         ICMD_LMULCONST        = 38,
738         ICMD_LANDCONST        = 39,
739         ICMD_LORCONST         = 40,
740         ICMD_LXORCONST        = 41,
741
742         ICMD_LSHLCONST        = 42,
743         ICMD_LSHRCONST        = 43,
744         ICMD_LUSHRCONST       = 44,
745
746         ICMD_LREMPOW2         = 45,
747
748         ICMD_IALOAD           = BC_iaload,
749         ICMD_LALOAD           = BC_laload,
750         ICMD_FALOAD           = BC_faload,
751         ICMD_DALOAD           = BC_daload,
752         ICMD_AALOAD           = BC_aaload,
753         ICMD_BALOAD           = BC_baload,
754         ICMD_CALOAD           = BC_caload,
755         ICMD_SALOAD           = BC_saload,
756
757         /* Order of STORE instructions must be equal to order of TYPE_*
758            defines. */
759
760         ICMD_ISTORE           = BC_istore,
761         ICMD_LSTORE           = BC_lstore,
762         ICMD_FSTORE           = BC_fstore,
763         ICMD_DSTORE           = BC_dstore,
764         ICMD_ASTORE           = BC_astore,
765
766         ICMD_IF_LEQ           = 59,
767         ICMD_IF_LNE           = 60,
768         ICMD_IF_LLT           = 61,
769         ICMD_IF_LGE           = 62,
770         ICMD_IF_LGT           = 63,
771         ICMD_IF_LLE           = 64,
772
773         ICMD_IF_LCMPEQ        = 65,
774         ICMD_IF_LCMPNE        = 66,
775         ICMD_IF_LCMPLT        = 67,
776         ICMD_IF_LCMPGE        = 68,
777         ICMD_IF_LCMPGT        = 69,
778         ICMD_IF_LCMPLE        = 70,
779
780         /* 71 */
781         /* 72 */
782         /* 73 */
783         /* 74 */
784         /* 75 */
785         /* 76 */
786         /* 77 */
787         /* 78 */
788
789         ICMD_IASTORE          = BC_iastore,
790         ICMD_LASTORE          = BC_lastore,
791         ICMD_FASTORE          = BC_fastore,
792         ICMD_DASTORE          = BC_dastore,
793         ICMD_AASTORE          = BC_aastore,
794         ICMD_BASTORE          = BC_bastore,
795         ICMD_CASTORE          = BC_castore,
796         ICMD_SASTORE          = BC_sastore,
797
798         ICMD_POP              = BC_pop,
799         ICMD_POP2             = BC_pop2,
800         ICMD_DUP              = BC_dup,
801         ICMD_DUP_X1           = BC_dup_x1,
802         ICMD_DUP_X2           = BC_dup_x2,
803         ICMD_DUP2             = BC_dup2,
804         ICMD_DUP2_X1          = BC_dup2_x1,
805         ICMD_DUP2_X2          = BC_dup2_x2,
806         ICMD_SWAP             = BC_swap,
807
808         ICMD_IADD             = BC_iadd,
809         ICMD_LADD             = BC_ladd,
810         ICMD_FADD             = BC_fadd,
811         ICMD_DADD             = BC_dadd,
812
813         ICMD_ISUB             = BC_isub,
814         ICMD_LSUB             = BC_lsub,
815         ICMD_FSUB             = BC_fsub,
816         ICMD_DSUB             = BC_dsub,
817
818         ICMD_IMUL             = BC_imul,
819         ICMD_LMUL             = BC_lmul,
820         ICMD_FMUL             = BC_fmul,
821         ICMD_DMUL             = BC_dmul,
822
823         ICMD_IDIV             = BC_idiv,
824         ICMD_LDIV             = BC_ldiv,
825         ICMD_FDIV             = BC_fdiv,
826         ICMD_DDIV             = BC_ddiv,
827
828         ICMD_IREM             = BC_irem,
829         ICMD_LREM             = BC_lrem,
830         ICMD_FREM             = BC_frem,
831         ICMD_DREM             = BC_drem,
832
833         ICMD_INEG             = BC_ineg,
834         ICMD_LNEG             = BC_lneg,
835         ICMD_FNEG             = BC_fneg,
836         ICMD_DNEG             = BC_dneg,
837
838         ICMD_ISHL             = BC_ishl,
839         ICMD_LSHL             = BC_lshl,
840         ICMD_ISHR             = BC_ishr,
841         ICMD_LSHR             = BC_lshr,
842         ICMD_IUSHR            = BC_iushr,
843         ICMD_LUSHR            = BC_lushr,
844
845         ICMD_IAND             = BC_iand,
846         ICMD_LAND             = BC_land,
847         ICMD_IOR              = BC_ior,
848         ICMD_LOR              = BC_lor,
849         ICMD_IXOR             = BC_ixor,
850         ICMD_LXOR             = BC_lxor,
851
852         ICMD_IINC             = BC_iinc,
853
854         ICMD_I2L              = BC_i2l,
855         ICMD_I2F              = BC_i2f,
856         ICMD_I2D              = BC_i2d,
857         ICMD_L2I              = BC_l2i,
858         ICMD_L2F              = BC_l2f,
859         ICMD_L2D              = BC_l2d,
860         ICMD_F2I              = BC_f2i,
861         ICMD_F2L              = BC_f2l,
862         ICMD_F2D              = BC_f2d,
863         ICMD_D2I              = BC_d2i,
864         ICMD_D2L              = BC_d2l,
865         ICMD_D2F              = BC_d2f,
866
867         ICMD_INT2BYTE         = BC_int2byte,
868         ICMD_INT2CHAR         = BC_int2char,
869         ICMD_INT2SHORT        = BC_int2short,
870
871         ICMD_LCMP             = BC_lcmp,
872         ICMD_FCMPL            = BC_fcmpl,
873         ICMD_FCMPG            = BC_fcmpg,
874         ICMD_DCMPL            = BC_dcmpl,
875         ICMD_DCMPG            = BC_dcmpg,
876
877         ICMD_IFEQ             = BC_ifeq,
878         ICMD_IFNE             = BC_ifne,
879         ICMD_IFLT             = BC_iflt,
880         ICMD_IFGE             = BC_ifge,
881         ICMD_IFGT             = BC_ifgt,
882         ICMD_IFLE             = BC_ifle,
883
884         ICMD_IF_ICMPEQ        = BC_if_icmpeq,
885         ICMD_IF_ICMPNE        = BC_if_icmpne,
886         ICMD_IF_ICMPLT        = BC_if_icmplt,
887         ICMD_IF_ICMPGE        = BC_if_icmpge,
888         ICMD_IF_ICMPGT        = BC_if_icmpgt,
889         ICMD_IF_ICMPLE        = BC_if_icmple,
890         ICMD_IF_ACMPEQ        = BC_if_acmpeq,
891         ICMD_IF_ACMPNE        = BC_if_acmpne,
892
893         ICMD_GOTO             = BC_goto,
894         ICMD_JSR              = BC_jsr,
895         ICMD_RET              = BC_ret,
896
897         ICMD_TABLESWITCH      = BC_tableswitch,
898         ICMD_LOOKUPSWITCH     = BC_lookupswitch,
899
900         ICMD_IRETURN          = BC_ireturn,
901         ICMD_LRETURN          = BC_lreturn,
902         ICMD_FRETURN          = BC_freturn,
903         ICMD_DRETURN          = BC_dreturn,
904         ICMD_ARETURN          = BC_areturn,
905         ICMD_RETURN           = BC_return,
906
907         ICMD_GETSTATIC        = BC_getstatic,
908         ICMD_PUTSTATIC        = BC_putstatic,
909         ICMD_GETFIELD         = BC_getfield,
910         ICMD_PUTFIELD         = BC_putfield,
911
912         ICMD_INVOKEVIRTUAL    = BC_invokevirtual,
913         ICMD_INVOKESPECIAL    = BC_invokespecial,
914         ICMD_INVOKESTATIC     = BC_invokestatic,
915         ICMD_INVOKEINTERFACE  = BC_invokeinterface,
916
917         /* 186 */
918
919         ICMD_NEW              = BC_new,
920         ICMD_NEWARRAY         = BC_newarray,
921         ICMD_ANEWARRAY        = BC_anewarray,
922
923         ICMD_ARRAYLENGTH      = BC_arraylength,
924
925         ICMD_ATHROW           = BC_athrow,
926
927         ICMD_CHECKCAST        = BC_checkcast,
928         ICMD_INSTANCEOF       = BC_instanceof,
929
930         ICMD_MONITORENTER     = BC_monitorenter,
931         ICMD_MONITOREXIT      = BC_monitorexit,
932
933         /* 196 */
934
935         ICMD_MULTIANEWARRAY   = BC_multianewarray,
936
937         ICMD_IFNULL           = BC_ifnull,
938         ICMD_IFNONNULL        = BC_ifnonnull,
939
940         /* 200 */
941         /* 201 */
942         /* 202 */
943
944         ICMD_IASTORECONST     = 204,
945         ICMD_LASTORECONST     = 205,
946         ICMD_FASTORECONST     = 206,
947         ICMD_DASTORECONST     = 207,
948         ICMD_AASTORECONST     = 208,
949         ICMD_BASTORECONST     = 209,
950         ICMD_CASTORECONST     = 210,
951         ICMD_SASTORECONST     = 211,
952
953         ICMD_PUTSTATICCONST   = 212,
954         ICMD_PUTFIELDCONST    = 213,
955
956         ICMD_IMULPOW2         = 214,
957         ICMD_LMULPOW2         = 215,
958
959         ICMD_GETEXCEPTION     = 249,
960         ICMD_PHI              = 250,
961
962         ICMD_INLINE_START     = 251,        /* instruction before inlined method  */
963         ICMD_INLINE_END       = 252,        /* instruction after inlined method   */
964         ICMD_INLINE_BODY      = 253,        /* start of inlined body              */
965
966         ICMD_BUILTIN          = 255         /* internal opcode                    */
967 };
968
969 /* Additional instruction accessors */
970
971 methoddesc *instruction_call_site(const instruction *iptr);
972
973 static inline bool instruction_has_dst(const instruction *iptr) {
974         if (
975                 (icmd_table[iptr->opc].dataflow == DF_INVOKE) ||
976                 (icmd_table[iptr->opc].dataflow == DF_BUILTIN)
977                 ) {
978                 return instruction_call_site(iptr)->returntype.type != TYPE_VOID;
979         } else {
980                 return icmd_table[iptr->opc].dataflow >= DF_DST_BASE;
981         }
982 }
983
984 /***************************** register types *********************************/
985
986 #define REG_RES   0         /* reserved register for OS or code generator     */
987 #define REG_RET   1         /* return value register                          */
988 #define REG_EXC   2         /* exception value register                       */
989 #define REG_SAV   3         /* (callee) saved register                        */
990 #define REG_TMP   4         /* scratch temporary register (caller saved)      */
991 #define REG_ARG   5         /* argument register (caller saved)               */
992
993 #define REG_END   -1        /* last entry in tables                           */
994  
995 #define PARAMMODE_NUMBERED  0 
996 #define PARAMMODE_STUFFED   1
997
998
999 /* function prototypes ********************************************************/
1000
1001 /* compiler initialisation */
1002 void jit_init(void);
1003
1004 /* compiler finalisation */
1005 void jit_close(void);
1006
1007 /* create a new jitdata */
1008 jitdata *jit_jitdata_new(methodinfo *m);
1009
1010 /* compile a method with jit compiler */
1011 u1 *jit_compile(methodinfo *m);
1012 u1 *jit_recompile(methodinfo *m);
1013
1014 void jit_invalidate_code(methodinfo *m);
1015 codeinfo *jit_get_current_code(methodinfo *m);
1016 void jit_request_optimization(methodinfo *m);
1017
1018 /* patch the method entrypoint */
1019 #if !defined(JIT_COMPILER_VIA_SIGNAL)
1020 u1 *jit_asm_compile(methodinfo *m, u1 *mptr, u1 *sp, u1 *ra);
1021 #endif
1022 void *jit_compile_handle(methodinfo *m, void *pv, void *ra, void *mptr);
1023
1024 s4 jit_complement_condition(s4 opcode);
1025
1026 void jit_renumber_basicblocks(jitdata *jd);
1027 #if !defined(NDEBUG)
1028 void jit_check_basicblock_numbers(jitdata *jd);
1029 #endif
1030
1031
1032 /* machine dependent functions ************************************************/
1033
1034 #if defined(ENABLE_JIT)
1035 void md_init(void);
1036 #endif
1037
1038 #if defined(ENABLE_INTRP)
1039 void intrp_md_init(void);
1040 #endif
1041
1042 void *md_jit_method_patch_address(void *pv, void *ra, void *mptr);
1043
1044 #endif /* _JIT_H */
1045
1046
1047 /*
1048  * These are local overrides for various environment variables in Emacs.
1049  * Please do not remove this and leave it at the end of the file, where
1050  * Emacs will automagically detect them.
1051  * ---------------------------------------------------------------------
1052  * Local variables:
1053  * mode: c
1054  * indent-tabs-mode: t
1055  * c-basic-offset: 4
1056  * tab-width: 4
1057  * End:
1058  * vim:noexpandtab:sw=4:ts=4:
1059  */