1 /******************************** comp/defines.c *******************************
3 Copyright (c) 1997 A. Krall, R. Grafl, M. Gschwind, M. Probst
5 See file COPYRIGHT for information on usage and disclaimer of warranties
7 defines all the constants and data structures of the compiler
9 Authors: Reinhard Grafl EMAIL: cacao@complang.tuwien.ac.at
10 Andreas Krall EMAIL: cacao@complang.tuwien.ac.at
11 Michael Gschwind EMAIL: cacao@complang.tuwien.ac.at
13 Last Change: 1997/09/22
15 *******************************************************************************/
19 /*********************** resolve typedef-cycles *******************************/
21 typedef struct stackinfo stackinfo;
22 typedef struct basicblock basicblock;
23 typedef struct varinfo varinfo;
24 typedef struct subroutineinfo subroutineinfo;
27 /***************************** basic block structure **************************/
29 #define BLOCKTYPE_JAVA 1
30 #define BLOCKTYPE_EXCREATOR 2
31 #define BLOCKTYPE_EXFORWARDER 3
33 /* there are three kinds of basic blocks:
34 JAVA ........ block which contains JavaVM code (normal case)
35 EXCREATOR ... block which creates an exception and calls the handler
36 EXFORWARDER .. block which does the dispatching to all possible handlers
40 u2 type; /* block type */
41 bool reached; /* true, when block has been reached; the field stack
42 contains the stack valid before entering block */
43 bool finished; /* true, if 'pcmdlist' is finished */
44 subroutineinfo *subroutine; /* info about blocks reachable by JSR */
45 listnode linkage; /* list chaining */
46 u4 jpc; /* JavaVM program counter at start of block */
47 stackinfo *stack; /* stack description */
48 list pcmdlist; /* list of pseudo commands */
49 u4 mpc; /* program counter within compiled machine code */
50 java_objectheader *exproto;
51 /* if (type==EXCREATOR) contains pointer to exception
53 varinfo *exvar; /* if (type==EXFORWARDER) contains exception variable */
54 u4 throwpos; /* if (type!=JAVA) contains position in program where
55 the exception was raised */
59 /***************************** subroutine structure ***************************/
61 struct subroutineinfo {
62 bool returnfinished; /* true if return allready has been processed */
64 stackinfo *returnstack; /* stack structure at position of return */
65 chain *callers; /* a list of all callers */
70 /************************** stack element structure ***************************/
73 stackinfo *prev; /* pointer to next element towards bootom */
74 varinfo *var; /* pointer to variable which contains this value */
78 /************************* pseudo variable structure **************************/
81 listnode linkage; /* list chaining */
83 u2 type; /* basic type of variable */
84 u2 number; /* sequential numbering (used for debugging) */
86 bool saved; /* true if variable sould survive subroutine calls */
88 struct reginfo *reg; /* registerused by variable */
90 /* temporary fields used during parsing */
92 list copies; /* list of all variables which are copies */
93 listnode copylink; /* chaining for copy list */
94 varinfo *original; /* pointer to original variable (self reference
97 bool globalscope; /* true if variable is activ in whole subroutine */
98 bool active; /* true if variable is currently active (parsing
99 a block where the variable is valid ) */
103 typedef varinfo *varid;
108 /************************** pseudo command structure **************************/
110 /* pseudo command tags */
112 #define TAG_LOADCONST_I 1 /* load integer constant */
113 #define TAG_LOADCONST_L 2 /* load long constant */
114 #define TAG_LOADCONST_F 3 /* load float constant */
115 #define TAG_LOADCONST_D 4 /* load double constant */
116 #define TAG_LOADCONST_A 5 /* load address constant */
121 #define TAG_TABLEJUMP 10
122 #define TAG_METHOD 11
124 #define TAG_ACTIVATE 13
127 typedef struct pcmd {
128 listnode linkage; /* list chaining */
129 int tag; /* kind of pseudo command */
130 int opcode; /* opcode and kind of pseudo command */
132 varinfo *dest; /* optional destination operand */
133 varinfo *source1; /* 3 optional source operands */
138 struct { /* load integer constant */
141 struct { /* load long constant */
144 struct { /* load float constant */
147 struct { /* load double constant */
150 struct { /* load address constant */
155 u2 type; /* type of value */
158 struct { /* memory access (load/store) */
159 int type; /* access type */
160 u4 offset; /* offset relative to base register */
163 struct { /* branch */
164 basicblock *target; /* branch target */
167 struct { /* branch using table */
168 u4 targetcount; /* number of entries */
169 basicblock **targets; /* branch target */
172 struct { /* method call */
173 methodinfo *method; /* pointer to 'methodinfo'-structure */
174 functionptr builtin; /* C function pointer or NULL */
175 u2 paramnum; /* number of parameters */
176 varid *params; /* table of parameter variables */
177 varid exceptionvar; /* exception variable */
189 /***************** forward references in branch instructions ******************/
191 typedef struct mcodereference {
192 listnode linkage; /* list chaining */
194 bool incode; /* true if code address, false if data address */
195 s4 msourcepos; /* patching position in code/data segment */
196 basicblock *target; /* target basic block */
202 /********** JavaVM operation codes (sortet) and instruction lengths ***********/
204 u1 jcommandsize[256] = {
208 #define CMD_ACONST_NULL 1
210 #define CMD_ICONST_M1 2
212 #define CMD_ICONST_0 3
214 #define CMD_ICONST_1 4
216 #define CMD_ICONST_2 5
218 #define CMD_ICONST_3 6
220 #define CMD_ICONST_4 7
222 #define CMD_ICONST_5 8
224 #define CMD_LCONST_0 9
226 #define CMD_LCONST_1 10
228 #define CMD_FCONST_0 11
230 #define CMD_FCONST_1 12
232 #define CMD_FCONST_2 13
234 #define CMD_DCONST_0 14
236 #define CMD_DCONST_1 15
238 #define CMD_BIPUSH 16
240 #define CMD_SIPUSH 17
258 #define CMD_ILOAD_0 26
260 #define CMD_ILOAD_1 27
262 #define CMD_ILOAD_2 28
264 #define CMD_ILOAD_3 29
266 #define CMD_LLOAD_0 30
268 #define CMD_LLOAD_1 31
270 #define CMD_LLOAD_2 32
272 #define CMD_LLOAD_3 33
274 #define CMD_FLOAD_0 34
276 #define CMD_FLOAD_1 35
278 #define CMD_FLOAD_2 36
280 #define CMD_FLOAD_3 37
282 #define CMD_DLOAD_0 38
284 #define CMD_DLOAD_1 39
286 #define CMD_DLOAD_2 40
288 #define CMD_DLOAD_3 41
290 #define CMD_ALOAD_0 42
292 #define CMD_ALOAD_1 43
294 #define CMD_ALOAD_2 44
296 #define CMD_ALOAD_3 45
298 #define CMD_IALOAD 46
300 #define CMD_LALOAD 47
302 #define CMD_FALOAD 48
304 #define CMD_DALOAD 49
306 #define CMD_AALOAD 50
308 #define CMD_BALOAD 51
310 #define CMD_CALOAD 52
312 #define CMD_SALOAD 53
314 #define CMD_ISTORE 54
316 #define CMD_LSTORE 55
318 #define CMD_FSTORE 56
320 #define CMD_DSTORE 57
322 #define CMD_ASTORE 58
324 #define CMD_ISTORE_0 59
326 #define CMD_ISTORE_1 60
328 #define CMD_ISTORE_2 61
330 #define CMD_ISTORE_3 62
332 #define CMD_LSTORE_0 63
334 #define CMD_LSTORE_1 64
336 #define CMD_LSTORE_2 65
338 #define CMD_LSTORE_3 66
340 #define CMD_FSTORE_0 67
342 #define CMD_FSTORE_1 68
344 #define CMD_FSTORE_2 69
346 #define CMD_FSTORE_3 70
348 #define CMD_DSTORE_0 71
350 #define CMD_DSTORE_1 72
352 #define CMD_DSTORE_2 73
354 #define CMD_DSTORE_3 74
356 #define CMD_ASTORE_0 75
358 #define CMD_ASTORE_1 76
360 #define CMD_ASTORE_2 77
362 #define CMD_ASTORE_3 78
364 #define CMD_IASTORE 79
366 #define CMD_LASTORE 80
368 #define CMD_FASTORE 81
370 #define CMD_DASTORE 82
372 #define CMD_AASTORE 83
374 #define CMD_BASTORE 84
376 #define CMD_CASTORE 85
378 #define CMD_SASTORE 86
386 #define CMD_DUP_X1 90
388 #define CMD_DUP_X2 91
392 #define CMD_DUP2_X1 93
394 #define CMD_DUP2_X2 94
454 #define CMD_IUSHR 124
456 #define CMD_LUSHR 125
496 #define CMD_INT2BYTE 145
498 #define CMD_INT2CHAR 146
500 #define CMD_INT2SHORT 147
504 #define CMD_FCMPL 149
506 #define CMD_FCMPG 150
508 #define CMD_DCMPL 151
510 #define CMD_DCMPG 152
524 #define CMD_IF_ICMPEQ 159
526 #define CMD_IF_ICMPNE 160
528 #define CMD_IF_ICMPLT 161
530 #define CMD_IF_ICMPGE 162
532 #define CMD_IF_ICMPGT 163
534 #define CMD_IF_ICMPLE 164
536 #define CMD_IF_ACMPEQ 165
538 #define CMD_IF_ACMPNE 166
546 #define CMD_TABLESWITCH 170
547 0, /* length must be computed */
548 #define CMD_LOOKUPSWITCH 171
549 0, /* length must be computed */
550 #define CMD_IRETURN 172
552 #define CMD_LRETURN 173
554 #define CMD_FRETURN 174
556 #define CMD_DRETURN 175
558 #define CMD_ARETURN 176
560 #define CMD_RETURN 177
562 #define CMD_GETSTATIC 178
564 #define CMD_PUTSTATIC 179
566 #define CMD_GETFIELD 180
568 #define CMD_PUTFIELD 181
570 #define CMD_INVOKEVIRTUAL 182
572 #define CMD_INVOKESPECIAL 183
574 #define CMD_INVOKESTATIC 184
576 #define CMD_INVOKEINTERFACE 185
581 #define CMD_NEWARRAY 188
583 #define CMD_ANEWARRAY 189
585 #define CMD_ARRAYLENGTH 190
587 #define CMD_ATHROW 191
589 #define CMD_CHECKCAST 192
591 #define CMD_INSTANCEOF 193
593 #define CMD_MONITORENTER 194
595 #define CMD_MONITOREXIT 195
598 0, /* length must be computed */
599 #define CMD_MULTIANEWARRAY 197
601 #define CMD_IFNULL 198
603 #define CMD_IFNONNULL 199
605 #define CMD_GOTO_W 200
607 #define CMD_JSR_W 201
609 #define CMD_BREAKPOINT 202
612 1,1,1,1,1,1,1,1,1,1, /* unused */
613 1,1,1,1,1,1,1,1,1,1,
614 1,1,1,1,1,1,1,1,1,1,
615 1,1,1,1,1,1,1,1,1,1,
616 1,1,1,1,1,1,1,1,1,1,
620 #define CMD_TRACEBUILT 253 /* internal opcode */
621 #define CMD_IFEQL 254 /* internal opcode */
622 #define CMD_IF_UCMPGE 255 /* internal opcode */
624 #define CMD_LOADCONST_I (CMD_IF_UCMPGE+TAG_LOADCONST_I) /* internal opcodes */
625 #define CMD_LOADCONST_L (CMD_IF_UCMPGE+TAG_LOADCONST_L)
626 #define CMD_LOADCONST_F (CMD_IF_UCMPGE+TAG_LOADCONST_F)
627 #define CMD_LOADCONST_D (CMD_IF_UCMPGE+TAG_LOADCONST_D)
628 #define CMD_LOADCONST_A (CMD_IF_UCMPGE+TAG_LOADCONST_A)
629 #define CMD_MOVE (CMD_IF_UCMPGE+TAG_MOVE)
631 #define CMD_TABLEJUMP (CMD_IF_UCMPGE+TAG_TABLEJUMP)
632 #define CMD_BUILTIN (CMD_IF_UCMPGE+TAG_METHOD)
633 #define CMD_DROP (CMD_IF_UCMPGE+TAG_DROP)
634 #define CMD_ACTIVATE (CMD_IF_UCMPGE+TAG_ACTIVATE)
637 /******************* description of JavaVM instructions ***********************/
649 stdopdescriptor *stdopdescriptors[256];
651 stdopdescriptor stdopdescriptortable[] = {
652 { CMD_IADD, TYPE_INT, TYPE_INT, TYPE_INT, NULL, true, false },
653 { CMD_ISUB, TYPE_INT, TYPE_INT, TYPE_INT, NULL, true, false },
654 { CMD_IMUL, TYPE_INT, TYPE_INT, TYPE_INT, NULL, true, false },
655 { CMD_ISHL, TYPE_INT, TYPE_INT, TYPE_INT, NULL, true, false },
656 { CMD_ISHR, TYPE_INT, TYPE_INT, TYPE_INT, NULL, true, false },
657 { CMD_IUSHR, TYPE_INT, TYPE_INT, TYPE_INT, NULL, true, false },
658 { CMD_IAND, TYPE_INT, TYPE_INT, TYPE_INT, NULL, true, false },
659 { CMD_IOR, TYPE_INT, TYPE_INT, TYPE_INT, NULL, true, false },
660 { CMD_IXOR, TYPE_INT, TYPE_INT, TYPE_INT, NULL, true, false },
661 { CMD_INEG, TYPE_INT, TYPE_VOID,TYPE_INT, NULL, true, false },
663 { CMD_LADD, TYPE_LONG, TYPE_LONG, TYPE_LONG,
664 (functionptr) builtin_ladd , SUPPORT_LONG && SUPPORT_LONG_ADD, false },
665 { CMD_LSUB, TYPE_LONG, TYPE_LONG, TYPE_LONG,
666 (functionptr) builtin_lsub , SUPPORT_LONG && SUPPORT_LONG_ADD, false },
667 { CMD_LMUL, TYPE_LONG, TYPE_LONG, TYPE_LONG,
668 (functionptr) builtin_lmul , SUPPORT_LONG && SUPPORT_LONG_MULDIV, false },
669 { CMD_LSHL, TYPE_LONG, TYPE_INT, TYPE_LONG,
670 (functionptr) builtin_lshl , SUPPORT_LONG && SUPPORT_LONG_SHIFT, false },
671 { CMD_LSHR, TYPE_LONG, TYPE_INT, TYPE_LONG,
672 (functionptr) builtin_lshr, SUPPORT_LONG && SUPPORT_LONG_SHIFT, false },
673 { CMD_LUSHR, TYPE_LONG, TYPE_INT, TYPE_LONG,
674 (functionptr) builtin_lushr, SUPPORT_LONG && SUPPORT_LONG_SHIFT, false },
675 { CMD_LAND, TYPE_LONG, TYPE_LONG, TYPE_LONG,
676 (functionptr) builtin_land, SUPPORT_LONG && SUPPORT_LONG_LOG, false },
677 { CMD_LOR, TYPE_LONG, TYPE_LONG, TYPE_LONG,
678 (functionptr) builtin_lor , SUPPORT_LONG && SUPPORT_LONG_LOG, false },
679 { CMD_LXOR, TYPE_LONG, TYPE_LONG, TYPE_LONG,
680 (functionptr) builtin_lxor, SUPPORT_LONG && SUPPORT_LONG_LOG, false },
681 { CMD_LNEG, TYPE_LONG, TYPE_VOID, TYPE_LONG,
682 (functionptr) builtin_lneg, SUPPORT_LONG && SUPPORT_LONG_ADD, false },
683 { CMD_LCMP, TYPE_LONG, TYPE_LONG, TYPE_INT,
684 (functionptr) builtin_lcmp, SUPPORT_LONG && SUPPORT_LONG_CMP, false },
686 { CMD_FADD, TYPE_FLOAT, TYPE_FLOAT, TYPE_FLOAT,
687 (functionptr) builtin_fadd, SUPPORT_FLOAT, true },
688 { CMD_FSUB, TYPE_FLOAT, TYPE_FLOAT, TYPE_FLOAT,
689 (functionptr) builtin_fsub, SUPPORT_FLOAT, true },
690 { CMD_FMUL, TYPE_FLOAT, TYPE_FLOAT, TYPE_FLOAT,
691 (functionptr) builtin_fmul, SUPPORT_FLOAT, true },
692 { CMD_FDIV, TYPE_FLOAT, TYPE_FLOAT, TYPE_FLOAT,
693 (functionptr) builtin_fdiv, SUPPORT_FLOAT, true },
694 { CMD_FREM, TYPE_FLOAT, TYPE_FLOAT, TYPE_FLOAT,
695 (functionptr) builtin_frem, SUPPORT_FLOAT, true },
696 { CMD_FNEG, TYPE_FLOAT, TYPE_VOID, TYPE_FLOAT,
697 (functionptr) builtin_fneg, SUPPORT_FLOAT, true },
698 { CMD_FCMPL, TYPE_FLOAT, TYPE_FLOAT, TYPE_INT,
699 (functionptr) builtin_fcmpl, SUPPORT_FLOAT, true },
700 { CMD_FCMPG, TYPE_FLOAT, TYPE_FLOAT, TYPE_INT,
701 (functionptr) builtin_fcmpg, SUPPORT_FLOAT, true },
703 { CMD_DADD, TYPE_DOUBLE, TYPE_DOUBLE, TYPE_DOUBLE,
704 (functionptr) builtin_dadd, SUPPORT_DOUBLE, true },
705 { CMD_DSUB, TYPE_DOUBLE, TYPE_DOUBLE, TYPE_DOUBLE,
706 (functionptr) builtin_dsub, SUPPORT_DOUBLE, true },
707 { CMD_DMUL, TYPE_DOUBLE, TYPE_DOUBLE, TYPE_DOUBLE,
708 (functionptr) builtin_dmul, SUPPORT_DOUBLE, true },
709 { CMD_DDIV, TYPE_DOUBLE, TYPE_DOUBLE, TYPE_DOUBLE,
710 (functionptr) builtin_ddiv, SUPPORT_DOUBLE, true },
711 { CMD_DREM, TYPE_DOUBLE, TYPE_DOUBLE, TYPE_DOUBLE,
712 (functionptr) builtin_drem, SUPPORT_DOUBLE, true },
713 { CMD_DNEG, TYPE_DOUBLE, TYPE_VOID, TYPE_DOUBLE,
714 (functionptr) builtin_dneg, SUPPORT_DOUBLE, true },
715 { CMD_DCMPL, TYPE_DOUBLE, TYPE_DOUBLE, TYPE_INT,
716 (functionptr) builtin_dcmpl, SUPPORT_DOUBLE, true },
717 { CMD_DCMPG, TYPE_DOUBLE, TYPE_DOUBLE, TYPE_INT,
718 (functionptr) builtin_dcmpg, SUPPORT_DOUBLE, true },
720 { CMD_INT2BYTE, TYPE_INT, TYPE_VOID, TYPE_INT, NULL, true,false },
721 { CMD_INT2CHAR, TYPE_INT, TYPE_VOID, TYPE_INT, NULL, true,false },
722 { CMD_INT2SHORT, TYPE_INT, TYPE_VOID, TYPE_INT, NULL, true,false },
723 { CMD_I2L, TYPE_INT, TYPE_VOID, TYPE_LONG,
724 (functionptr) builtin_i2l, SUPPORT_LONG && SUPPORT_LONG_ICVT, false },
725 { CMD_I2F, TYPE_INT, TYPE_VOID, TYPE_FLOAT,
726 (functionptr) builtin_i2f, SUPPORT_FLOAT, true },
727 { CMD_I2D, TYPE_INT, TYPE_VOID, TYPE_DOUBLE,
728 (functionptr) builtin_i2d, SUPPORT_DOUBLE, true },
729 { CMD_L2I, TYPE_LONG, TYPE_VOID, TYPE_INT,
730 (functionptr) builtin_l2i, SUPPORT_LONG && SUPPORT_LONG_ICVT, false },
731 { CMD_L2F, TYPE_LONG, TYPE_VOID, TYPE_FLOAT,
732 (functionptr) builtin_l2f, SUPPORT_LONG && SUPPORT_FLOAT && SUPPORT_LONG_FCVT, true },
733 { CMD_L2D, TYPE_LONG, TYPE_VOID, TYPE_DOUBLE,
734 (functionptr) builtin_l2d, SUPPORT_LONG && SUPPORT_DOUBLE && SUPPORT_LONG_FCVT, true },
735 { CMD_F2I, TYPE_FLOAT, TYPE_VOID, TYPE_INT,
736 (functionptr) builtin_f2i, SUPPORT_FLOAT, true },
737 { CMD_F2L, TYPE_FLOAT, TYPE_VOID, TYPE_LONG,
738 (functionptr) builtin_f2l, SUPPORT_FLOAT && SUPPORT_LONG && SUPPORT_LONG_FCVT, true },
739 { CMD_F2D, TYPE_FLOAT, TYPE_VOID, TYPE_DOUBLE,
740 (functionptr) builtin_f2d, SUPPORT_FLOAT && SUPPORT_DOUBLE, true },
741 { CMD_D2I, TYPE_DOUBLE, TYPE_VOID, TYPE_INT,
742 (functionptr) builtin_d2i, SUPPORT_DOUBLE, true },
743 { CMD_D2L, TYPE_DOUBLE, TYPE_VOID, TYPE_LONG,
744 (functionptr) builtin_d2l, SUPPORT_DOUBLE && SUPPORT_LONG && SUPPORT_LONG_FCVT, true },
745 { CMD_D2F, TYPE_DOUBLE, TYPE_VOID, TYPE_FLOAT,
746 (functionptr) builtin_d2f, SUPPORT_DOUBLE && SUPPORT_FLOAT, true },
752 /***************************** register types *********************************/
754 #define REG_RES 0 /* reserved register for OS or code generator */
755 #define REG_RET 1 /* return value register */
756 #define REG_EXC 2 /* exception value register */
757 #define REG_SAV 3 /* (callee) saved register */
758 #define REG_TMP 4 /* scratch temporary register (caller saved) */
759 #define REG_ARG 5 /* argument register (caller saved) */
761 #define REG_END -1 /* last entry in tables */
763 #define PARAMMODE_NUMBERED 0
764 #define PARAMMODE_STUFFED 1
766 /***************************** register info block ****************************/
768 extern int regdescint[]; /* description of integer registers */
769 extern int regdescfloat[]; /* description of floating point registers */
771 extern int reg_parammode;