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