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