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 115 1999-01-20 01:52:45Z phil $
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 for (i = 0; i < exceptiontablelength; i++) {
309 p = extable[i].startpc;
312 p = extable[i].endpc;
316 p = extable[i].handlerpc;
321 s_count = 1 + exceptiontablelength; /* initialize stack element counter */
324 if (checksync && (method->flags & ACC_SYNCHRONIZED)) {
329 /* scan all java instructions */
331 for (p = 0; p < jcodelength; p = nextp) {
333 opcode = code_get_u1 (p); /* fetch op code */
335 block_index[p] |= (ipc << 1); /* store intermediate count */
338 block_insert(p); /* start new block */
342 nextp = p + jcommandsize[opcode]; /* compute next instruction start */
343 s_count += stackreq[opcode]; /* compute stack element count */
350 /* pushing constants onto the stack p */
353 LOADCONST_I(code_get_s1(p+1));
357 LOADCONST_I(code_get_s2(p+1));
361 i = code_get_u1(p+1);
362 goto pushconstantitem;
365 i = code_get_u2(p + 1);
369 if (i >= class->cpcount)
370 panic ("Attempt to access constant outside range");
372 switch (class->cptags[i]) {
373 case CONSTANT_Integer:
374 LOADCONST_I(((constant_integer*)
375 (class->cpinfos[i]))->value);
378 LOADCONST_L(((constant_long*)
379 (class->cpinfos[i]))->value);
382 LOADCONST_F(((constant_float*)
383 (class->cpinfos[i]))->value);
385 case CONSTANT_Double:
386 LOADCONST_D(((constant_double*)
387 (class->cpinfos[i]))->value);
389 case CONSTANT_String:
390 LOADCONST_A(literalstring_new((unicode*)
391 (class->cpinfos[i])));
393 default: panic("Invalid constant type to push");
397 case JAVA_ACONST_NULL:
408 LOADCONST_I(opcode - JAVA_ICONST_0);
413 LOADCONST_L(opcode - JAVA_LCONST_0);
419 LOADCONST_F(opcode - JAVA_FCONST_0);
424 LOADCONST_D(opcode - JAVA_DCONST_0);
427 /* loading variables onto the stack */
435 i = code_get_u1(p+1);
437 i = code_get_u2(p+1);
448 OP1(ICMD_ILOAD, opcode - JAVA_ILOAD_0);
455 OP1(ICMD_LLOAD, opcode - JAVA_LLOAD_0);
462 OP1(ICMD_FLOAD, opcode - JAVA_FLOAD_0);
469 OP1(ICMD_DLOAD, opcode - JAVA_DLOAD_0);
476 OP1(ICMD_ALOAD, opcode - JAVA_ALOAD_0);
479 /* storing stack values into local variables */
487 i = code_get_u1(p+1);
489 i = code_get_u2(p+1);
500 OP1(ICMD_ISTORE, opcode - JAVA_ISTORE_0);
507 OP1(ICMD_LSTORE, opcode - JAVA_LSTORE_0);
514 OP1(ICMD_FSTORE, opcode - JAVA_FSTORE_0);
521 OP1(ICMD_DSTORE, opcode - JAVA_DSTORE_0);
528 OP1(ICMD_ASTORE, opcode - JAVA_ASTORE_0);
536 i = code_get_u1(p + 1);
537 v = code_get_s1(p + 2);
540 i = code_get_u2(p + 1);
541 v = code_get_s2(p + 3);
549 /* wider index for loading, storing and incrementing */
556 /* managing arrays ************************************************/
559 OP2I(ICMD_CHECKASIZE, 0, 0);
560 switch (code_get_s1(p+1)) {
562 BUILTIN1((functionptr)builtin_newarray_boolean, TYPE_ADR);
565 BUILTIN1((functionptr)builtin_newarray_char, TYPE_ADR);
568 BUILTIN1((functionptr)builtin_newarray_float, TYPE_ADR);
571 BUILTIN1((functionptr)builtin_newarray_double, TYPE_ADR);
574 BUILTIN1((functionptr)builtin_newarray_byte, TYPE_ADR);
577 BUILTIN1((functionptr)builtin_newarray_short, TYPE_ADR);
580 BUILTIN1((functionptr)builtin_newarray_int, TYPE_ADR);
583 BUILTIN1((functionptr)builtin_newarray_long, TYPE_ADR);
585 default: panic("Invalid array-type to create");
590 OP2I(ICMD_CHECKASIZE, 0, 0);
591 i = code_get_u2(p+1);
592 /* array or class type ? */
593 if (class_constanttype (class, i) == CONSTANT_Arraydescriptor) {
594 LOADCONST_A(class_getconstant(class, i,
595 CONSTANT_Arraydescriptor));
596 BUILTIN2((functionptr)builtin_newarray_array, TYPE_ADR);
599 LOADCONST_A(class_getconstant(class, i, CONSTANT_Class));
600 BUILTIN2((functionptr)builtin_anewarray, TYPE_ADR);
604 case JAVA_MULTIANEWARRAY:
606 i = code_get_u2(p+1);
608 int v = code_get_u1(p+3);
609 constant_arraydescriptor *desc =
610 class_getconstant (class, i, CONSTANT_Arraydescriptor);
611 OP2A(opcode, v, desc);
633 i = p + code_get_s2(p+1);
641 i = p + code_get_s4(p+1);
650 i = code_get_u1(p+1);
652 i = code_get_u2(p+1);
676 /* table jumps ********************************/
678 case JAVA_LOOKUPSWITCH:
683 nextp = ALIGN((p + 1), 4);
684 OP2A(opcode, 0, jcode + nextp);
688 j = p + code_get_s4(nextp);
689 *((s4*)(jcode + nextp)) = j; /* restore for little endian */
694 /* number of pairs */
696 num = code_get_u4(nextp);
697 *((s4*)(jcode + nextp)) = num;
700 for (i = 0; i < num; i++) {
704 j = code_get_s4(nextp);
705 *((s4*)(jcode + nextp)) = j; /* restore for little endian */
710 j = p + code_get_s4(nextp);
711 *((s4*)(jcode + nextp)) = j; /* restore for little endian */
721 case JAVA_TABLESWITCH:
726 nextp = ALIGN((p + 1), 4);
727 OP2A(opcode, 0, jcode + nextp);
731 j = p + code_get_s4(nextp);
732 *((s4*)(jcode + nextp)) = j; /* restore for little endian */
739 j = code_get_s4(nextp);
740 *((s4*)(jcode + nextp)) = j; /* restore for little endian */
745 num = code_get_s4(nextp);
746 *((s4*)(jcode + nextp)) = num; /* restore for little endian */
751 for (i = 0; i <= num; i++) {
752 j = p + code_get_s4(nextp);
753 *((s4*)(jcode + nextp)) = j; /* restore for little endian */
763 /* load and store of object fields *******************/
766 BUILTIN3((functionptr) asm_builtin_aastore, TYPE_VOID);
771 i = code_get_u2(p + 1);
775 fr = class_getconstant (class, i, CONSTANT_Fieldref);
776 fi = class_findfield (fr->class, fr->name, fr->descriptor);
777 compiler_addinitclass (fr->class);
778 OP2A(opcode, fi->type, fi);
783 i = code_get_u2(p + 1);
787 fr = class_getconstant (class, i, CONSTANT_Fieldref);
788 fi = class_findfield (fr->class, fr->name, fr->descriptor);
789 OP2A(opcode, fi->type, fi);
794 /* method invocation *****/
796 case JAVA_INVOKESTATIC:
797 i = code_get_u2(p + 1);
802 mr = class_getconstant (class, i, CONSTANT_Methodref);
803 mi = class_findmethod (mr->class, mr->name, mr->descriptor);
804 if (! (mi->flags & ACC_STATIC))
805 panic ("Static/Nonstatic mismatch calling static method");
806 descriptor2types(mi);
808 OP2A(opcode, mi->paramcount, mi);
811 case JAVA_INVOKESPECIAL:
812 case JAVA_INVOKEVIRTUAL:
813 i = code_get_u2(p + 1);
818 mr = class_getconstant (class, i, CONSTANT_Methodref);
819 mi = class_findmethod (mr->class, mr->name, mr->descriptor);
820 if (mi->flags & ACC_STATIC)
821 panic ("Static/Nonstatic mismatch calling static method");
822 descriptor2types(mi);
824 OP2A(opcode, mi->paramcount, mi);
827 case JAVA_INVOKEINTERFACE:
828 i = code_get_u2(p + 1);
833 mr = class_getconstant (class, i, CONSTANT_InterfaceMethodref);
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);
843 /* miscellaneous object operations *******/
846 i = code_get_u2 (p+1);
847 LOADCONST_A(class_getconstant(class, i, CONSTANT_Class));
848 BUILTIN1((functionptr) builtin_new, TYPE_ADR);
852 i = code_get_u2(p+1);
854 /* array type cast-check */
855 if (class_constanttype (class, i) == CONSTANT_Arraydescriptor) {
856 LOADCONST_A(class_getconstant(class, i, CONSTANT_Arraydescriptor));
857 BUILTIN2((functionptr) asm_builtin_checkarraycast, TYPE_ADR);
859 else { /* object type cast-check */
860 OP2A(opcode, 1, (class_getconstant(class, i, CONSTANT_Class)));
864 case JAVA_INSTANCEOF:
865 i = code_get_u2(p+1);
867 /* array type cast-check */
868 if (class_constanttype (class, i) == CONSTANT_Arraydescriptor) {
869 LOADCONST_A(class_getconstant(class, i, CONSTANT_Arraydescriptor));
870 BUILTIN2((functionptr) builtin_arrayinstanceof, TYPE_INT);
872 else { /* object type cast-check */
873 OP2A(opcode, 1, (class_getconstant(class, i, CONSTANT_Class)));
877 case JAVA_MONITORENTER:
880 #ifdef SOFTNULLPTRCHECK
882 BUILTIN1((functionptr) asm_builtin_monitorenter, TYPE_VOID);
885 /* BUILTIN1((functionptr) builtin_monitorenter, TYPE_VOID); */
886 BUILTIN1((functionptr) asm_builtin_monitorenter, TYPE_VOID);
889 BUILTIN1((functionptr) builtin_monitorenter, TYPE_VOID);
895 OP(ICMD_NULLCHECKPOP);
899 case JAVA_MONITOREXIT:
902 BUILTIN1((functionptr) builtin_monitorexit, TYPE_VOID);
911 /* any other basic operation **************************************/
930 BUILTIN2((functionptr) builtin_frem, TYPE_FLOAT);
934 BUILTIN2((functionptr) builtin_drem, TYPE_DOUBLE);
939 BUILTIN1((functionptr) builtin_f2i, TYPE_INT);
948 BUILTIN1((functionptr) builtin_f2l, TYPE_LONG);
957 BUILTIN1((functionptr) builtin_d2i, TYPE_INT);
966 BUILTIN1((functionptr) builtin_d2l, TYPE_LONG);
973 case JAVA_BREAKPOINT:
974 panic("Illegal opcode Breakpoint encountered");
1030 printf("Illegal opcode %d at instr %d", opcode, ipc);
1031 panic("encountered");
1042 if (p != jcodelength)
1043 panic("Command-sequence crosses code-boundary");
1046 panic("Code does not end with branch/return/athrow - stmt");
1048 /* adjust block count if target 0 is not first intermediate instruction */
1050 if (!block_index[0] || (block_index[0] > 1))
1053 /* copy local to global variables */
1056 block_count = b_count;
1057 stack_count = s_count + block_count * maxstack;
1059 /* allocate stack table */
1061 stack = DMNEW(stackelement, stack_count);
1066 bptr = block = DMNEW(basicblock, b_count + 1); /* one more for end ipc */
1070 /* additional block if target 0 is not first intermediate instruction */
1072 if (!block_index[0] || (block_index[0] > 1)) {
1073 bptr->iinstr = instr;
1076 bptr->type = BBTYPE_STD;
1077 bptr->branchrefs = NULL;
1078 bptr->pre_count = 0;
1083 /* allocate blocks */
1085 for (p = 0; p < jcodelength; p++)
1086 if (block_index[p] & 1) {
1087 bptr->iinstr = instr + (block_index[p] >> 1);
1089 (bptr - 1)->icount = bptr->iinstr - (bptr - 1)->iinstr;
1092 bptr->type = BBTYPE_STD;
1093 bptr->branchrefs = NULL;
1094 block_index[p] = b_count;
1095 bptr->pre_count = 0;
1100 /* allocate additional block at end */
1102 bptr->iinstr = NULL;
1103 (bptr - 1)->icount = (instr + instr_count) - (bptr - 1)->iinstr;
1107 bptr->type = BBTYPE_STD;
1108 bptr->branchrefs = NULL;
1109 bptr->pre_count = 0;
1115 * These are local overrides for various environment variables in Emacs.
1116 * Please do not remove this and leave it at the end of the file, where
1117 * Emacs will automagically detect them.
1118 * ---------------------------------------------------------------------
1121 * indent-tabs-mode: t