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