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 274 2003-05-09 13:39:39Z carolyn $
12 include Rapid Type Analysis parse - 5/2003 - carolyn
15 *******************************************************************************/
18 /* data about the currently parsed method */
20 static classinfo *rt_class; /* class the compiled method belongs to */
21 static methodinfo *rt_method; /* pointer to method info of compiled method */
22 static utf *rt_descriptor; /* type descriptor of compiled method */
23 static int rt_jcodelength; /*length of JavaVM-codes */
24 static u1 *rt_jcode; /* pointer to start of JavaVM-code */
27 /* macros for byte code fetching ***********************************************
29 fetch a byte code of given size from position p in code array jcode
31 *******************************************************************************/
33 #define code_get_u1(p) jcode[p]
34 #define code_get_s1(p) ((s1)jcode[p])
35 #define code_get_u2(p) ((((u2)jcode[p])<<8)+jcode[p+1])
36 #define code_get_s2(p) ((s2)((((u2)jcode[p])<<8)+jcode[p+1]))
37 #define code_get_u4(p) ((((u4)jcode[p])<<24)+(((u4)jcode[p+1])<<16)\
38 +(((u4)jcode[p+2])<<8)+jcode[p+3])
39 #define code_get_s4(p) ((s4)((((u4)jcode[p])<<24)+(((u4)jcode[p+1])<<16)\
40 +(((u4)jcode[p+2])<<8)+jcode[p+3]))
43 /* functionc compiler_addinitclass *********************************************
45 add class into the list of classes to initialize
47 *******************************************************************************/
49 static void compiler_addinitclass (classinfo *c)
53 if (c->initialized) return;
55 cl = chain_first(uninitializedclasses);
60 cl = chain_next(uninitializedclasses);
66 sprintf(logtext, "compiler_addinitclass: ");
67 utf_sprint(logtext+strlen(logtext), c->name);
70 chain_addlast(uninitializedclasses, c);
75 sprintf(logtext, "compiler_addinitclass: ");
76 utf_sprint(logtext+strlen(logtext), c->name);
79 chain_addbefore(uninitializedclasses, c);
82 cl = chain_next(uninitializedclasses);
87 /* function descriptor2types ***************************************************
89 decodes a already checked method descriptor. The parameter count, the
90 return type and the argument types are stored in the passed methodinfo.
92 *******************************************************************************/
94 static void descriptor2types (methodinfo *m)
100 types = DMNEW (u1, m->descriptor->blength);
103 if (!(m->flags & ACC_STATIC)) {
108 utf_ptr = m->descriptor->text + 1;
110 while ((c = *utf_ptr++) != ')') {
117 case 'Z': *tptr++ = TYPE_INT;
119 case 'J': *tptr++ = TYPE_LNG;
121 case 'F': *tptr++ = TYPE_FLT;
123 case 'D': *tptr++ = TYPE_DBL;
125 case 'L': *tptr++ = TYPE_ADR;
126 while (*utf_ptr++ != ';');
128 case '[': *tptr++ = TYPE_ADR;
132 while (*utf_ptr++ != ';') /* skip */;
134 default: panic ("Ill formed methodtype-descriptor");
138 /* compute return type */
140 switch (*utf_ptr++) {
145 case 'Z': m->returntype = TYPE_INT;
147 case 'J': m->returntype = TYPE_LNG;
149 case 'F': m->returntype = TYPE_FLT;
151 case 'D': m->returntype = TYPE_DBL;
154 case 'L': m->returntype = TYPE_ADR;
156 case 'V': m->returntype = TYPE_VOID;
159 default: panic ("Ill formed methodtype-descriptor");
162 m->paramcount = pcount;
163 m->paramtypes = types;
169 /* function allocate_literals **************************************************
171 Scans the JavaVM code of a method and allocates string literals (in the
172 same order as the old JIT). Needed to generate the same addresses as the
175 *******************************************************************************/
177 static void allocate_literals()
184 for (p = 0; p < jcodelength; p = nextp) {
187 nextp = p + jcommandsize[opcode];
191 if (code_get_u1(p + 1) == JAVA_IINC)
197 case JAVA_LOOKUPSWITCH:
198 nextp = ALIGN((p + 1), 4);
199 num = code_get_u4(nextp + 4);
200 nextp = nextp + 8 + 8 * num;
203 case JAVA_TABLESWITCH:
204 nextp = ALIGN ((p + 1),4);
205 num = code_get_s4(nextp + 4);
206 num = code_get_s4(nextp + 8) - num;
207 nextp = nextp + 16 + 4 * num;
211 i = code_get_u1(p+1);
212 goto pushconstantitem;
215 i = code_get_u2(p + 1);
217 if (class_constanttype(class, i) == CONSTANT_String) {
218 s = class_getconstant(class, i, CONSTANT_String);
219 (void) literalstring_new(s);
228 /*******************************************************************************
230 function 'parse' scans the JavaVM code and generates intermediate code
232 During parsing the block index table is used to store at bit pos 0
233 a flag which marks basic block starts and at position 1 to 31 the
234 intermediate instruction index. After parsing the block index table
235 is scanned, for marked positions a block is generated and the block
236 number is stored in the block index table.
238 *******************************************************************************/
240 /* intermediate code generating macros */
242 #define PINC iptr++;ipc++
243 #define LOADCONST_I(v) iptr->opc=ICMD_ICONST;iptr->op1=0;iptr->val.i=(v);PINC
244 #define LOADCONST_L(v) iptr->opc=ICMD_LCONST;iptr->op1=0;iptr->val.l=(v);PINC
245 #define LOADCONST_F(v) iptr->opc=ICMD_FCONST;iptr->op1=0;iptr->val.f=(v);PINC
246 #define LOADCONST_D(v) iptr->opc=ICMD_DCONST;iptr->op1=0;iptr->val.d=(v);PINC
247 #define LOADCONST_A(v) iptr->opc=ICMD_ACONST;iptr->op1=0;iptr->val.a=(v);PINC
248 #define OP(o) iptr->opc=(o);iptr->op1=0;iptr->val.l=0;PINC
249 #define OP1(o,o1) iptr->opc=(o);iptr->op1=(o1);iptr->val.l=(0);PINC
250 #define OP2I(o,o1,v) iptr->opc=(o);iptr->op1=(o1);iptr->val.i=(v);PINC
251 #define OP2A(o,o1,v) iptr->opc=(o);iptr->op1=(o1);iptr->val.a=(v);PINC
252 #define BUILTIN1(v,t) isleafmethod=false;iptr->opc=ICMD_BUILTIN1;iptr->op1=t;\
254 #define BUILTIN2(v,t) isleafmethod=false;iptr->opc=ICMD_BUILTIN2;iptr->op1=t;\
256 #define BUILTIN3(v,t) isleafmethod=false;iptr->opc=ICMD_BUILTIN3;iptr->op1=t;\
260 /* block generating and checking macros */
262 #define block_insert(i) {if(!(block_index[i]&1))\
263 {b_count++;block_index[i] |= 1;}}
264 #define bound_check(i) {if((i< 0) || (i>=jcodelength)) \
265 panic("branch target out of code-boundary");}
266 #define bound_check1(i) {if((i< 0) || (i>jcodelength)) \
267 panic("branch target out of code-boundary");}
272 int p; /* java instruction counter */
273 int nextp; /* start of next java instruction */
274 int opcode; /* java opcode */
275 int i; /* temporary for different uses (counters) */
276 int ipc = 0; /* intermediate instruction counter */
277 int b_count = 0; /* basic block counter */
278 int s_count = 0; /* stack element counter */
279 bool blockend = false; /* true if basic block end has been reached */
280 bool iswide = false; /* true if last instruction was a wide */
281 instruction *iptr; /* current pointer into instruction array */
283 RT_jit_parse(method);
284 /*RTAprint*/ if ((pOpcodes == 2) || (pOpcodes == 3))
285 /*RTAprint*/ {printf("PARSE method name =");
286 /*RTAprint*/ utf_display(rt_method->class->name);printf(".");
287 /*RTAprint*/ utf_display(rt_method->name);printf("\n\n");
288 /*RTAprint*/ method_display(rt_method); printf(">\n\n");fflush(stdout);}
291 /* generate the same addresses as the old JIT compiler */
297 /* allocate instruction array and block index table */
299 /* 1 additional for end ipc and 3 for loop unrolling */
301 block_index = DMNEW(int, jcodelength + 3);
303 /* 1 additional for TRACEBUILTIN and 4 for MONITORENTER/EXIT */
304 /* additional MONITOREXITS are reached by branches which are 3 bytes */
306 iptr = instr = DMNEW(instruction, jcodelength + 5);
308 /* initialize block_index table (unrolled four times) */
313 for (i = 0, ip = block_index; i <= jcodelength; i += 4, ip += 4) {
321 /* compute branch targets of exception table */
323 extable = DMNEW(xtable, exceptiontablelength + 1);
325 for (i = 0; i < exceptiontablelength; i++) {
327 p = extable[i].startpc = raw_extable[i].startpc;
331 p = extable[i].endpc = raw_extable[i].endpc;
336 p = extable[i].handlerpc = raw_extable[i].handlerpc;
340 extable[i].catchtype = raw_extable[i].catchtype;
342 extable[i].next = NULL;
343 extable[i].down = &extable[i+1];
346 if (exceptiontablelength > 0)
347 extable[exceptiontablelength-1].down = NULL;
351 s_count = 1 + exceptiontablelength; /* initialize stack element counter */
354 if (checksync && (method->flags & ACC_SYNCHRONIZED)) {
359 /* scan all java instructions */
361 for (p = 0; p < jcodelength; p = nextp) {
363 opcode = code_get_u1 (p); /* fetch op code */
365 /*RTAprint*/ if ((pOpcodes == 1) || (pOpcodes == 3))
366 /*RTAprint*/ {printf("Parse RT p=%i<%i< opcode=<%i> %s\n",
367 /*RTAprint*/ p,rt_jcodelength,opcode,icmd_names[opcode]);}
369 block_index[p] |= (ipc << 1); /* store intermediate count */
372 block_insert(p); /* start new block */
376 nextp = p + jcommandsize[opcode]; /* compute next instruction start */
377 s_count += stackreq[opcode]; /* compute stack element count */
384 /* pushing constants onto the stack p */
387 LOADCONST_I(code_get_s1(p+1));
391 LOADCONST_I(code_get_s2(p+1));
395 i = code_get_u1(p+1);
396 goto pushconstantitem;
399 i = code_get_u2(p + 1);
403 if (i >= class->cpcount)
404 panic ("Attempt to access constant outside range");
406 switch (class->cptags[i]) {
407 case CONSTANT_Integer:
408 LOADCONST_I(((constant_integer*)
409 (class->cpinfos[i]))->value);
412 LOADCONST_L(((constant_long*)
413 (class->cpinfos[i]))->value);
416 LOADCONST_F(((constant_float*)
417 (class->cpinfos[i]))->value);
419 case CONSTANT_Double:
420 LOADCONST_D(((constant_double*)
421 (class->cpinfos[i]))->value);
423 case CONSTANT_String:
424 LOADCONST_A(literalstring_new((utf*)
425 (class->cpinfos[i])));
427 default: panic("Invalid constant type to push");
431 case JAVA_ACONST_NULL:
442 LOADCONST_I(opcode - JAVA_ICONST_0);
447 LOADCONST_L(opcode - JAVA_LCONST_0);
453 LOADCONST_F(opcode - JAVA_FCONST_0);
458 LOADCONST_D(opcode - JAVA_DCONST_0);
461 /* loading variables onto the stack */
469 i = code_get_u1(p+1);
471 i = code_get_u2(p+1);
482 OP1(ICMD_ILOAD, opcode - JAVA_ILOAD_0);
489 OP1(ICMD_LLOAD, opcode - JAVA_LLOAD_0);
496 OP1(ICMD_FLOAD, opcode - JAVA_FLOAD_0);
503 OP1(ICMD_DLOAD, opcode - JAVA_DLOAD_0);
510 OP1(ICMD_ALOAD, opcode - JAVA_ALOAD_0);
513 /* storing stack values into local variables */
521 i = code_get_u1(p+1);
523 i = code_get_u2(p+1);
534 OP1(ICMD_ISTORE, opcode - JAVA_ISTORE_0);
541 OP1(ICMD_LSTORE, opcode - JAVA_LSTORE_0);
548 OP1(ICMD_FSTORE, opcode - JAVA_FSTORE_0);
555 OP1(ICMD_DSTORE, opcode - JAVA_DSTORE_0);
562 OP1(ICMD_ASTORE, opcode - JAVA_ASTORE_0);
570 i = code_get_u1(p + 1);
571 v = code_get_s1(p + 2);
574 i = code_get_u2(p + 1);
575 v = code_get_s2(p + 3);
583 /* wider index for loading, storing and incrementing */
590 /* managing arrays ************************************************/
593 OP2I(ICMD_CHECKASIZE, 0, 0);
594 switch (code_get_s1(p+1)) {
596 BUILTIN1((functionptr)builtin_newarray_boolean, TYPE_ADR);
599 BUILTIN1((functionptr)builtin_newarray_char, TYPE_ADR);
602 BUILTIN1((functionptr)builtin_newarray_float, TYPE_ADR);
605 BUILTIN1((functionptr)builtin_newarray_double, TYPE_ADR);
608 BUILTIN1((functionptr)builtin_newarray_byte, TYPE_ADR);
611 BUILTIN1((functionptr)builtin_newarray_short, TYPE_ADR);
614 BUILTIN1((functionptr)builtin_newarray_int, TYPE_ADR);
617 BUILTIN1((functionptr)builtin_newarray_long, TYPE_ADR);
619 default: panic("Invalid array-type to create");
624 OP2I(ICMD_CHECKASIZE, 0, 0);
625 i = code_get_u2(p+1);
626 /* array or class type ? */
627 if (class_constanttype (class, i) == CONSTANT_Arraydescriptor) {
629 LOADCONST_A(class_getconstant(class, i,
630 CONSTANT_Arraydescriptor));
631 BUILTIN2((functionptr)builtin_newarray_array, TYPE_ADR);
634 LOADCONST_A(class_getconstant(class, i, CONSTANT_Class));
637 BUILTIN2((functionptr) asm_builtin_anewarray, TYPE_ADR);
639 BUILTIN2((functionptr)builtin_anewarray, TYPE_ADR);
644 case JAVA_MULTIANEWARRAY:
646 i = code_get_u2(p+1);
648 int v = code_get_u1(p+3);
649 constant_arraydescriptor *desc =
650 class_getconstant (class, i, CONSTANT_Arraydescriptor);
651 OP2A(opcode, v, desc);
673 i = p + code_get_s2(p+1);
681 i = p + code_get_s4(p+1);
690 i = code_get_u1(p+1);
692 i = code_get_u2(p+1);
716 /* table jumps ********************************/
718 case JAVA_LOOKUPSWITCH:
723 nextp = ALIGN((p + 1), 4);
724 OP2A(opcode, 0, jcode + nextp);
728 j = p + code_get_s4(nextp);
729 *((s4*)(jcode + nextp)) = j; /* restore for little endian */
734 /* number of pairs */
736 num = code_get_u4(nextp);
737 *((s4*)(jcode + nextp)) = num;
740 for (i = 0; i < num; i++) {
744 j = code_get_s4(nextp);
745 *((s4*)(jcode + nextp)) = j; /* restore for little endian */
750 j = p + code_get_s4(nextp);
751 *((s4*)(jcode + nextp)) = j; /* restore for little endian */
761 case JAVA_TABLESWITCH:
766 nextp = ALIGN((p + 1), 4);
767 OP2A(opcode, 0, jcode + nextp);
771 j = p + code_get_s4(nextp);
772 *((s4*)(jcode + nextp)) = j; /* restore for little endian */
779 j = code_get_s4(nextp);
780 *((s4*)(jcode + nextp)) = j; /* restore for little endian */
785 num = code_get_s4(nextp);
786 *((s4*)(jcode + nextp)) = num; /* restore for little endian */
791 for (i = 0; i <= num; i++) {
792 j = p + code_get_s4(nextp);
793 *((s4*)(jcode + nextp)) = j; /* restore for little endian */
803 /* load and store of object fields *******************/
806 BUILTIN3((functionptr) asm_builtin_aastore, TYPE_VOID);
811 i = code_get_u2(p + 1);
815 fr = class_getconstant (class, i, CONSTANT_Fieldref);
816 fi = class_findfield (fr->class, fr->name, fr->descriptor);
817 compiler_addinitclass (fr->class);
818 OP2A(opcode, fi->type, fi);
823 i = code_get_u2(p + 1);
827 fr = class_getconstant (class, i, CONSTANT_Fieldref);
828 fi = class_findfield (fr->class, fr->name, fr->descriptor);
829 OP2A(opcode, fi->type, fi);
834 /* method invocation *****/
836 case JAVA_INVOKESTATIC:
837 i = code_get_u2(p + 1);
842 mr = class_getconstant (class, i, CONSTANT_Methodref);
843 mi = class_findmethod (mr->class, mr->name, mr->descriptor);
844 if (! (mi->flags & ACC_STATIC))
845 panic ("Static/Nonstatic mismatch calling static method");
846 descriptor2types(mi);
848 OP2A(opcode, mi->paramcount, mi);
851 case JAVA_INVOKESPECIAL:
852 case JAVA_INVOKEVIRTUAL:
853 i = code_get_u2(p + 1);
858 mr = class_getconstant (class, i, CONSTANT_Methodref);
859 mi = class_findmethod (mr->class, mr->name, mr->descriptor);
860 if (mi->flags & ACC_STATIC)
861 panic ("Static/Nonstatic mismatch calling static method");
862 descriptor2types(mi);
864 OP2A(opcode, mi->paramcount, mi);
867 case JAVA_INVOKEINTERFACE:
868 i = code_get_u2(p + 1);
873 mr = class_getconstant (class, i, CONSTANT_InterfaceMethodref);
874 mi = class_findmethod (mr->class, mr->name, mr->descriptor);
875 if (mi->flags & ACC_STATIC)
876 panic ("Static/Nonstatic mismatch calling static method");
877 descriptor2types(mi);
879 OP2A(opcode, mi->paramcount, mi);
883 /* miscellaneous object operations *******/
886 i = code_get_u2 (p+1);
887 LOADCONST_A(class_getconstant(class, i, CONSTANT_Class));
889 BUILTIN1((functionptr) builtin_new, TYPE_ADR);
893 i = code_get_u2(p+1);
895 /* array type cast-check */
896 if (class_constanttype (class, i) == CONSTANT_Arraydescriptor) {
897 LOADCONST_A(class_getconstant(class, i, CONSTANT_Arraydescriptor));
899 BUILTIN2((functionptr) asm_builtin_checkarraycast, TYPE_ADR);
901 else { /* object type cast-check */
903 LOADCONST_A(class_getconstant(class, i, CONSTANT_Class));
905 BUILTIN2((functionptr) asm_builtin_checkcast, TYPE_ADR);
907 OP2A(opcode, 1, (class_getconstant(class, i, CONSTANT_Class)));
911 case JAVA_INSTANCEOF:
912 i = code_get_u2(p+1);
914 /* array type cast-check */
915 if (class_constanttype (class, i) == CONSTANT_Arraydescriptor) {
916 LOADCONST_A(class_getconstant(class, i, CONSTANT_Arraydescriptor));
918 BUILTIN2((functionptr) builtin_arrayinstanceof, TYPE_INT);
920 else { /* object type cast-check */
922 LOADCONST_A(class_getconstant(class, i, CONSTANT_Class));
924 BUILTIN2((functionptr) builtin_instanceof, TYPE_INT);
926 OP2A(opcode, 1, (class_getconstant(class, i, CONSTANT_Class)));
930 case JAVA_MONITORENTER:
933 #ifdef SOFTNULLPTRCHECK
935 BUILTIN1((functionptr) asm_builtin_monitorenter, TYPE_VOID);
938 /* BUILTIN1((functionptr) builtin_monitorenter, TYPE_VOID); */
939 BUILTIN1((functionptr) asm_builtin_monitorenter, TYPE_VOID);
942 BUILTIN1((functionptr) builtin_monitorenter, TYPE_VOID);
948 OP(ICMD_NULLCHECKPOP);
952 case JAVA_MONITOREXIT:
955 BUILTIN1((functionptr) builtin_monitorexit, TYPE_VOID);
964 /* any other basic operation **************************************/
983 BUILTIN2((functionptr) builtin_frem, TYPE_FLOAT);
987 BUILTIN2((functionptr) builtin_drem, TYPE_DOUBLE);
992 BUILTIN1((functionptr) builtin_f2i, TYPE_INT);
1001 BUILTIN1((functionptr) builtin_f2l, TYPE_LONG);
1010 BUILTIN1((functionptr) builtin_d2i, TYPE_INT);
1019 BUILTIN1((functionptr) builtin_d2l, TYPE_LONG);
1026 case JAVA_BREAKPOINT:
1027 panic("Illegal opcode Breakpoint encountered");
1083 printf("Illegal opcode %d at instr %d", opcode, ipc);
1084 panic("encountered");
1095 if (p != jcodelength)
1096 panic("Command-sequence crosses code-boundary");
1099 panic("Code does not end with branch/return/athrow - stmt");
1101 /* adjust block count if target 0 is not first intermediate instruction */
1103 if (!block_index[0] || (block_index[0] > 1))
1106 /* copy local to global variables */
1109 block_count = b_count;
1110 stack_count = s_count + block_count * maxstack;
1112 /* allocate stack table */
1114 stack = DMNEW(stackelement, stack_count);
1119 bptr = block = DMNEW(basicblock, b_count + 1); /* one more for end ipc */
1124 /* additional block if target 0 is not first intermediate instruction */
1126 if (!block_index[0] || (block_index[0] > 1)) {
1127 bptr->iinstr = instr;
1130 bptr->type = BBTYPE_STD;
1131 bptr->branchrefs = NULL;
1132 bptr->pre_count = 0;
1133 bptr->debug_nr = c_debug_nr++;
1136 (bptr - 1)->next = bptr;
1140 /* allocate blocks */
1143 for (p = 0; p < jcodelength; p++)
1145 if (block_index[p] & 1) {
1146 bptr->iinstr = instr + (block_index[p] >> 1);
1147 bptr->debug_nr = c_debug_nr++;
1149 (bptr - 1)->icount = bptr->iinstr - (bptr - 1)->iinstr;
1153 bptr->type = BBTYPE_STD;
1154 bptr->branchrefs = NULL;
1155 block_index[p] = b_count;
1156 bptr->pre_count = 0;
1160 (bptr - 1)->next = bptr;
1163 /* allocate additional block at end */
1166 bptr->instack = bptr->outstack = NULL;
1167 bptr->indepth = bptr->outdepth = 0;
1168 bptr->iinstr = NULL;
1169 (bptr - 1)->icount = (instr + instr_count) - (bptr - 1)->iinstr;
1174 bptr->type = BBTYPE_STD;
1175 bptr->branchrefs = NULL;
1176 bptr->pre_count = 0;
1177 bptr->debug_nr = c_debug_nr++;
1179 (bptr - 1)->next = bptr;
1186 for (i = 0; i < exceptiontablelength; ++i) {
1187 p = extable[i].startpc;
1188 extable[i].start = block + block_index[p];
1190 p = extable[i].endpc;
1191 extable[i].end = block + block_index[p];
1193 p = extable[i].handlerpc;
1194 extable[i].handler = block + block_index[p];
1199 #include "parseRT.h"
1202 * These are local overrides for various environment variables in Emacs.
1203 * Please do not remove this and leave it at the end of the file, where
1204 * Emacs will automagically detect them.
1205 * ---------------------------------------------------------------------
1208 * indent-tabs-mode: t