1 /* jit/parse.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 Parser for JavaVM to intermediate code translation
9 Author: Andreas Krall EMAIL: cacao@complang.tuwien.ac.at
11 Last Change: $Id: parse.c 132 1999-09-27 15:54:42Z chris $
13 *******************************************************************************/
17 /* macros for byte code fetching ***********************************************
19 fetch a byte code of given size from position p in code array jcode
21 *******************************************************************************/
23 #define code_get_u1(p) jcode[p]
24 #define code_get_s1(p) ((s1)jcode[p])
25 #define code_get_u2(p) ((((u2)jcode[p])<<8)+jcode[p+1])
26 #define code_get_s2(p) ((s2)((((u2)jcode[p])<<8)+jcode[p+1]))
27 #define code_get_u4(p) ((((u4)jcode[p])<<24)+(((u4)jcode[p+1])<<16)\
28 +(((u4)jcode[p+2])<<8)+jcode[p+3])
29 #define code_get_s4(p) ((s4)((((u4)jcode[p])<<24)+(((u4)jcode[p+1])<<16)\
30 +(((u4)jcode[p+2])<<8)+jcode[p+3]))
33 /* functionc compiler_addinitclass *********************************************
35 add class into the list of classes to initialize
37 *******************************************************************************/
39 static void compiler_addinitclass (classinfo *c)
43 if (c->initialized) return;
45 cl = chain_first(uninitializedclasses);
50 cl = chain_next(uninitializedclasses);
56 sprintf(logtext, "compiler_addinitclass: ");
57 unicode_sprint(logtext+strlen(logtext), c->name);
60 chain_addlast(uninitializedclasses, c);
65 sprintf(logtext, "compiler_addinitclass: ");
66 unicode_sprint(logtext+strlen(logtext), c->name);
69 chain_addbefore(uninitializedclasses, c);
72 cl = chain_next(uninitializedclasses);
77 /* function descriptor2types ***************************************************
79 decodes a already checked method descriptor. The parameter count, the
80 return type and the argument types are stored in the passed methodinfo.
82 *******************************************************************************/
84 static void descriptor2types (methodinfo *m)
91 types = DMNEW (u1, m->descriptor->length);
94 if (!(m->flags & ACC_STATIC)) {
99 cptr = m->descriptor->text;
101 while ((c = *cptr++) != ')') {
108 case 'Z': *tptr++ = TYPE_INT;
110 case 'J': *tptr++ = TYPE_LNG;
112 case 'F': *tptr++ = TYPE_FLT;
114 case 'D': *tptr++ = TYPE_DBL;
116 case 'L': *tptr++ = TYPE_ADR;
117 while (*cptr++ != ';');
119 case '[': *tptr++ = TYPE_ADR;
123 while (*cptr++ != ';') /* skip */;
125 default: panic ("Ill formed methodtype-descriptor");
129 /* compute return type */
136 case 'Z': m->returntype = TYPE_INT;
138 case 'J': m->returntype = TYPE_LNG;
140 case 'F': m->returntype = TYPE_FLT;
142 case 'D': m->returntype = TYPE_DBL;
145 case 'L': m->returntype = TYPE_ADR;
147 case 'V': m->returntype = TYPE_VOID;
150 default: panic ("Ill formed methodtype-descriptor");
153 m->paramcount = pcount;
154 m->paramtypes = types;
160 /* function allocate_literals **************************************************
162 Scans the JavaVM code of a method and allocates string literals (in the
163 same order as the old JIT). Needed to generate the same addresses as the
166 *******************************************************************************/
168 static void allocate_literals()
175 for (p = 0; p < jcodelength; p = nextp) {
178 nextp = p + jcommandsize[opcode];
182 if (code_get_u1(p + 1) == JAVA_IINC)
188 case JAVA_LOOKUPSWITCH:
189 nextp = ALIGN((p + 1), 4);
190 num = code_get_u4(nextp + 4);
191 nextp = nextp + 8 + 8 * num;
194 case JAVA_TABLESWITCH:
195 nextp = ALIGN ((p + 1),4);
196 num = code_get_s4(nextp + 4);
197 num = code_get_s4(nextp + 8) - num;
198 nextp = nextp + 16 + 4 * num;
202 i = code_get_u1(p+1);
203 goto pushconstantitem;
206 i = code_get_u2(p + 1);
208 if (class_constanttype(class, i) == CONSTANT_String) {
209 s = class_getconstant(class, i, CONSTANT_String);
210 (void) literalstring_new(s);
219 /*******************************************************************************
221 function 'parse' scans the JavaVM code and generates intermediate code
223 During parsing the block index table is used to store at bit pos 0
224 a flag which marks basic block starts and at position 1 to 31 the
225 intermediate instruction index. After parsing the block index table
226 is scanned, for marked positions a block is generated and the block
227 number is stored in the block index table.
229 *******************************************************************************/
231 /* intermediate code generating macros */
233 #define PINC iptr++;ipc++
234 #define LOADCONST_I(v) iptr->opc=ICMD_ICONST;iptr->op1=0;iptr->val.i=(v);PINC
235 #define LOADCONST_L(v) iptr->opc=ICMD_LCONST;iptr->op1=0;iptr->val.l=(v);PINC
236 #define LOADCONST_F(v) iptr->opc=ICMD_FCONST;iptr->op1=0;iptr->val.f=(v);PINC
237 #define LOADCONST_D(v) iptr->opc=ICMD_DCONST;iptr->op1=0;iptr->val.d=(v);PINC
238 #define LOADCONST_A(v) iptr->opc=ICMD_ACONST;iptr->op1=0;iptr->val.a=(v);PINC
239 #define OP(o) iptr->opc=(o);iptr->op1=0;iptr->val.l=0;PINC
240 #define OP1(o,o1) iptr->opc=(o);iptr->op1=(o1);iptr->val.l=(0);PINC
241 #define OP2I(o,o1,v) iptr->opc=(o);iptr->op1=(o1);iptr->val.i=(v);PINC
242 #define OP2A(o,o1,v) iptr->opc=(o);iptr->op1=(o1);iptr->val.a=(v);PINC
243 #define BUILTIN1(v,t) isleafmethod=false;iptr->opc=ICMD_BUILTIN1;iptr->op1=t;\
245 #define BUILTIN2(v,t) isleafmethod=false;iptr->opc=ICMD_BUILTIN2;iptr->op1=t;\
247 #define BUILTIN3(v,t) isleafmethod=false;iptr->opc=ICMD_BUILTIN3;iptr->op1=t;\
251 /* block generating and checking macros */
253 #define block_insert(i) {if(!(block_index[i]&1))\
254 {b_count++;block_index[i] |= 1;}}
255 #define bound_check(i) {if((i< 0) || (i>=jcodelength)) \
256 panic("branch target out of code-boundary");}
257 #define bound_check1(i) {if((i< 0) || (i>jcodelength)) \
258 panic("branch target out of code-boundary");}
263 int p; /* java instruction counter */
264 int nextp; /* start of next java instruction */
265 int opcode; /* java opcode */
266 int i; /* temporary for different uses (counters) */
267 int ipc = 0; /* intermediate instruction counter */
268 int b_count = 0; /* basic block counter */
269 int s_count = 0; /* stack element counter */
270 bool blockend = false; /* true if basic block end has been reached */
271 bool iswide = false; /* true if last instruction was a wide */
272 instruction *iptr; /* current pointer into instruction array */
276 /* generate the same addresses as the old JIT compiler */
282 /* allocate instruction array and block index table */
284 /* 1 additional for end ipc and 3 for loop unrolling */
286 block_index = DMNEW(int, jcodelength + 3);
288 /* 1 additional for TRACEBUILTIN and 4 for MONITORENTER/EXIT */
289 /* additional MONITOREXITS are reached by branches which are 3 bytes */
291 iptr = instr = DMNEW(instruction, jcodelength + 5);
293 /* initialize block_index table (unrolled four times) */
298 for (i = 0, ip = block_index; i <= jcodelength; i += 4, ip += 4) {
306 /* compute branch targets of exception table */
308 extable = DMNEW(xtable, exceptiontablelength + 1);
310 for (i = 0; i < exceptiontablelength; i++) {
312 p = extable[i].startpc = raw_extable[i].startpc;
316 p = extable[i].endpc = raw_extable[i].endpc;
321 p = extable[i].handlerpc = raw_extable[i].handlerpc;
325 extable[i].catchtype = raw_extable[i].catchtype;
327 extable[i].next = NULL;
328 extable[i].down = &extable[i+1];
331 if (exceptiontablelength > 0)
332 extable[exceptiontablelength-1].down = NULL;
336 s_count = 1 + exceptiontablelength; /* initialize stack element counter */
339 if (checksync && (method->flags & ACC_SYNCHRONIZED)) {
344 /* scan all java instructions */
346 for (p = 0; p < jcodelength; p = nextp) {
348 opcode = code_get_u1 (p); /* fetch op code */
350 block_index[p] |= (ipc << 1); /* store intermediate count */
353 block_insert(p); /* start new block */
357 nextp = p + jcommandsize[opcode]; /* compute next instruction start */
358 s_count += stackreq[opcode]; /* compute stack element count */
365 /* pushing constants onto the stack p */
368 LOADCONST_I(code_get_s1(p+1));
372 LOADCONST_I(code_get_s2(p+1));
376 i = code_get_u1(p+1);
377 goto pushconstantitem;
380 i = code_get_u2(p + 1);
384 if (i >= class->cpcount)
385 panic ("Attempt to access constant outside range");
387 switch (class->cptags[i]) {
388 case CONSTANT_Integer:
389 LOADCONST_I(((constant_integer*)
390 (class->cpinfos[i]))->value);
393 LOADCONST_L(((constant_long*)
394 (class->cpinfos[i]))->value);
397 LOADCONST_F(((constant_float*)
398 (class->cpinfos[i]))->value);
400 case CONSTANT_Double:
401 LOADCONST_D(((constant_double*)
402 (class->cpinfos[i]))->value);
404 case CONSTANT_String:
405 LOADCONST_A(literalstring_new((unicode*)
406 (class->cpinfos[i])));
408 default: panic("Invalid constant type to push");
412 case JAVA_ACONST_NULL:
423 LOADCONST_I(opcode - JAVA_ICONST_0);
428 LOADCONST_L(opcode - JAVA_LCONST_0);
434 LOADCONST_F(opcode - JAVA_FCONST_0);
439 LOADCONST_D(opcode - JAVA_DCONST_0);
442 /* loading variables onto the stack */
450 i = code_get_u1(p+1);
452 i = code_get_u2(p+1);
463 OP1(ICMD_ILOAD, opcode - JAVA_ILOAD_0);
470 OP1(ICMD_LLOAD, opcode - JAVA_LLOAD_0);
477 OP1(ICMD_FLOAD, opcode - JAVA_FLOAD_0);
484 OP1(ICMD_DLOAD, opcode - JAVA_DLOAD_0);
491 OP1(ICMD_ALOAD, opcode - JAVA_ALOAD_0);
494 /* storing stack values into local variables */
502 i = code_get_u1(p+1);
504 i = code_get_u2(p+1);
515 OP1(ICMD_ISTORE, opcode - JAVA_ISTORE_0);
522 OP1(ICMD_LSTORE, opcode - JAVA_LSTORE_0);
529 OP1(ICMD_FSTORE, opcode - JAVA_FSTORE_0);
536 OP1(ICMD_DSTORE, opcode - JAVA_DSTORE_0);
543 OP1(ICMD_ASTORE, opcode - JAVA_ASTORE_0);
551 i = code_get_u1(p + 1);
552 v = code_get_s1(p + 2);
555 i = code_get_u2(p + 1);
556 v = code_get_s2(p + 3);
564 /* wider index for loading, storing and incrementing */
571 /* managing arrays ************************************************/
574 OP2I(ICMD_CHECKASIZE, 0, 0);
575 switch (code_get_s1(p+1)) {
577 BUILTIN1((functionptr)builtin_newarray_boolean, TYPE_ADR);
580 BUILTIN1((functionptr)builtin_newarray_char, TYPE_ADR);
583 BUILTIN1((functionptr)builtin_newarray_float, TYPE_ADR);
586 BUILTIN1((functionptr)builtin_newarray_double, TYPE_ADR);
589 BUILTIN1((functionptr)builtin_newarray_byte, TYPE_ADR);
592 BUILTIN1((functionptr)builtin_newarray_short, TYPE_ADR);
595 BUILTIN1((functionptr)builtin_newarray_int, TYPE_ADR);
598 BUILTIN1((functionptr)builtin_newarray_long, TYPE_ADR);
600 default: panic("Invalid array-type to create");
605 OP2I(ICMD_CHECKASIZE, 0, 0);
606 i = code_get_u2(p+1);
607 /* array or class type ? */
608 if (class_constanttype (class, i) == CONSTANT_Arraydescriptor) {
609 LOADCONST_A(class_getconstant(class, i,
610 CONSTANT_Arraydescriptor));
611 BUILTIN2((functionptr)builtin_newarray_array, TYPE_ADR);
614 LOADCONST_A(class_getconstant(class, i, CONSTANT_Class));
615 BUILTIN2((functionptr)builtin_anewarray, TYPE_ADR);
619 case JAVA_MULTIANEWARRAY:
621 i = code_get_u2(p+1);
623 int v = code_get_u1(p+3);
624 constant_arraydescriptor *desc =
625 class_getconstant (class, i, CONSTANT_Arraydescriptor);
626 OP2A(opcode, v, desc);
648 i = p + code_get_s2(p+1);
656 i = p + code_get_s4(p+1);
665 i = code_get_u1(p+1);
667 i = code_get_u2(p+1);
691 /* table jumps ********************************/
693 case JAVA_LOOKUPSWITCH:
698 nextp = ALIGN((p + 1), 4);
699 OP2A(opcode, 0, jcode + nextp);
703 j = p + code_get_s4(nextp);
704 *((s4*)(jcode + nextp)) = j; /* restore for little endian */
709 /* number of pairs */
711 num = code_get_u4(nextp);
712 *((s4*)(jcode + nextp)) = num;
715 for (i = 0; i < num; i++) {
719 j = code_get_s4(nextp);
720 *((s4*)(jcode + nextp)) = j; /* restore for little endian */
725 j = p + code_get_s4(nextp);
726 *((s4*)(jcode + nextp)) = j; /* restore for little endian */
736 case JAVA_TABLESWITCH:
741 nextp = ALIGN((p + 1), 4);
742 OP2A(opcode, 0, jcode + nextp);
746 j = p + code_get_s4(nextp);
747 *((s4*)(jcode + nextp)) = j; /* restore for little endian */
754 j = code_get_s4(nextp);
755 *((s4*)(jcode + nextp)) = j; /* restore for little endian */
760 num = code_get_s4(nextp);
761 *((s4*)(jcode + nextp)) = num; /* restore for little endian */
766 for (i = 0; i <= num; i++) {
767 j = p + code_get_s4(nextp);
768 *((s4*)(jcode + nextp)) = j; /* restore for little endian */
778 /* load and store of object fields *******************/
781 BUILTIN3((functionptr) asm_builtin_aastore, TYPE_VOID);
786 i = code_get_u2(p + 1);
790 fr = class_getconstant (class, i, CONSTANT_Fieldref);
791 fi = class_findfield (fr->class, fr->name, fr->descriptor);
792 compiler_addinitclass (fr->class);
793 OP2A(opcode, fi->type, fi);
798 i = code_get_u2(p + 1);
802 fr = class_getconstant (class, i, CONSTANT_Fieldref);
803 fi = class_findfield (fr->class, fr->name, fr->descriptor);
804 OP2A(opcode, fi->type, fi);
809 /* method invocation *****/
811 case JAVA_INVOKESTATIC:
812 i = code_get_u2(p + 1);
817 mr = class_getconstant (class, i, CONSTANT_Methodref);
818 mi = class_findmethod (mr->class, mr->name, mr->descriptor);
819 if (! (mi->flags & ACC_STATIC))
820 panic ("Static/Nonstatic mismatch calling static method");
821 descriptor2types(mi);
823 OP2A(opcode, mi->paramcount, mi);
826 case JAVA_INVOKESPECIAL:
827 case JAVA_INVOKEVIRTUAL:
828 i = code_get_u2(p + 1);
833 mr = class_getconstant (class, i, CONSTANT_Methodref);
834 mi = class_findmethod (mr->class, mr->name, mr->descriptor);
835 if (mi->flags & ACC_STATIC)
836 panic ("Static/Nonstatic mismatch calling static method");
837 descriptor2types(mi);
839 OP2A(opcode, mi->paramcount, mi);
842 case JAVA_INVOKEINTERFACE:
843 i = code_get_u2(p + 1);
848 mr = class_getconstant (class, i, CONSTANT_InterfaceMethodref);
849 mi = class_findmethod (mr->class, mr->name, mr->descriptor);
850 if (mi->flags & ACC_STATIC)
851 panic ("Static/Nonstatic mismatch calling static method");
852 descriptor2types(mi);
854 OP2A(opcode, mi->paramcount, mi);
858 /* miscellaneous object operations *******/
861 i = code_get_u2 (p+1);
862 LOADCONST_A(class_getconstant(class, i, CONSTANT_Class));
863 BUILTIN1((functionptr) builtin_new, TYPE_ADR);
867 i = code_get_u2(p+1);
869 /* array type cast-check */
870 if (class_constanttype (class, i) == CONSTANT_Arraydescriptor) {
871 LOADCONST_A(class_getconstant(class, i, CONSTANT_Arraydescriptor));
872 BUILTIN2((functionptr) asm_builtin_checkarraycast, TYPE_ADR);
874 else { /* object type cast-check */
875 OP2A(opcode, 1, (class_getconstant(class, i, CONSTANT_Class)));
879 case JAVA_INSTANCEOF:
880 i = code_get_u2(p+1);
882 /* array type cast-check */
883 if (class_constanttype (class, i) == CONSTANT_Arraydescriptor) {
884 LOADCONST_A(class_getconstant(class, i, CONSTANT_Arraydescriptor));
885 BUILTIN2((functionptr) builtin_arrayinstanceof, TYPE_INT);
887 else { /* object type cast-check */
888 OP2A(opcode, 1, (class_getconstant(class, i, CONSTANT_Class)));
892 case JAVA_MONITORENTER:
895 #ifdef SOFTNULLPTRCHECK
897 BUILTIN1((functionptr) asm_builtin_monitorenter, TYPE_VOID);
900 /* BUILTIN1((functionptr) builtin_monitorenter, TYPE_VOID); */
901 BUILTIN1((functionptr) asm_builtin_monitorenter, TYPE_VOID);
904 BUILTIN1((functionptr) builtin_monitorenter, TYPE_VOID);
910 OP(ICMD_NULLCHECKPOP);
914 case JAVA_MONITOREXIT:
917 BUILTIN1((functionptr) builtin_monitorexit, TYPE_VOID);
926 /* any other basic operation **************************************/
945 BUILTIN2((functionptr) builtin_frem, TYPE_FLOAT);
949 BUILTIN2((functionptr) builtin_drem, TYPE_DOUBLE);
954 BUILTIN1((functionptr) builtin_f2i, TYPE_INT);
963 BUILTIN1((functionptr) builtin_f2l, TYPE_LONG);
972 BUILTIN1((functionptr) builtin_d2i, TYPE_INT);
981 BUILTIN1((functionptr) builtin_d2l, TYPE_LONG);
988 case JAVA_BREAKPOINT:
989 panic("Illegal opcode Breakpoint encountered");
1045 printf("Illegal opcode %d at instr %d", opcode, ipc);
1046 panic("encountered");
1057 if (p != jcodelength)
1058 panic("Command-sequence crosses code-boundary");
1061 panic("Code does not end with branch/return/athrow - stmt");
1063 /* adjust block count if target 0 is not first intermediate instruction */
1065 if (!block_index[0] || (block_index[0] > 1))
1068 /* copy local to global variables */
1071 block_count = b_count;
1072 stack_count = s_count + block_count * maxstack;
1074 /* allocate stack table */
1076 stack = DMNEW(stackelement, stack_count);
1081 bptr = block = DMNEW(basicblock, b_count + 1); /* one more for end ipc */
1086 /* additional block if target 0 is not first intermediate instruction */
1088 if (!block_index[0] || (block_index[0] > 1)) {
1089 bptr->iinstr = instr;
1092 bptr->type = BBTYPE_STD;
1093 bptr->branchrefs = NULL;
1094 bptr->pre_count = 0;
1095 bptr->debug_nr = c_debug_nr++;
1098 (bptr - 1)->next = bptr;
1102 /* allocate blocks */
1105 for (p = 0; p < jcodelength; p++)
1107 if (block_index[p] & 1) {
1108 bptr->iinstr = instr + (block_index[p] >> 1);
1109 bptr->debug_nr = c_debug_nr++;
1111 (bptr - 1)->icount = bptr->iinstr - (bptr - 1)->iinstr;
1115 bptr->type = BBTYPE_STD;
1116 bptr->branchrefs = NULL;
1117 block_index[p] = b_count;
1118 bptr->pre_count = 0;
1122 (bptr - 1)->next = bptr;
1125 /* allocate additional block at end */
1128 bptr->instack = bptr->outstack = NULL;
1129 bptr->indepth = bptr->outdepth = 0;
1130 bptr->iinstr = NULL;
1131 (bptr - 1)->icount = (instr + instr_count) - (bptr - 1)->iinstr;
1136 bptr->type = BBTYPE_STD;
1137 bptr->branchrefs = NULL;
1138 bptr->pre_count = 0;
1139 bptr->debug_nr = c_debug_nr++;
1141 (bptr - 1)->next = bptr;
1148 for (i = 0; i < exceptiontablelength; ++i) {
1149 p = extable[i].startpc;
1150 extable[i].start = block + block_index[p];
1152 p = extable[i].endpc;
1153 extable[i].end = block + block_index[p];
1155 p = extable[i].handlerpc;
1156 extable[i].handler = block + block_index[p];
1163 * These are local overrides for various environment variables in Emacs.
1164 * Please do not remove this and leave it at the end of the file, where
1165 * Emacs will automagically detect them.
1166 * ---------------------------------------------------------------------
1169 * indent-tabs-mode: t