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: 1998/05/07
13 *******************************************************************************/
18 /* macros for byte code fetching ***********************************************
20 fetch a byte code of given size from position p in code array jcode
22 *******************************************************************************/
24 #define code_get_u1(p) jcode[p]
25 #define code_get_s1(p) ((s1)jcode[p])
26 #define code_get_u2(p) ((((u2)jcode[p])<<8)+jcode[p+1])
27 #define code_get_s2(p) ((s2)((((u2)jcode[p])<<8)+jcode[p+1]))
28 #define code_get_u4(p) ((((u4)jcode[p])<<24)+(((u4)jcode[p+1])<<16)\
29 +(((u4)jcode[p+2])<<8)+jcode[p+3])
30 #define code_get_s4(p) ((s4)((((u4)jcode[p])<<24)+(((u4)jcode[p+1])<<16)\
31 +(((u4)jcode[p+2])<<8)+jcode[p+3]))
34 /* functionc compiler_addinitclass *********************************************
36 add class into the list of classes to initialize
38 *******************************************************************************/
40 static void compiler_addinitclass (classinfo *c)
44 if (c->initialized) return;
46 cl = chain_first(uninitializedclasses);
51 cl = chain_next(uninitializedclasses);
57 sprintf(logtext, "compiler_addinitclass: ");
58 unicode_sprint(logtext+strlen(logtext), c->name);
61 chain_addlast(uninitializedclasses, c);
66 sprintf(logtext, "compiler_addinitclass: ");
67 unicode_sprint(logtext+strlen(logtext), c->name);
70 chain_addbefore(uninitializedclasses, c);
73 cl = chain_next(uninitializedclasses);
78 /* function descriptor2types ***************************************************
80 decodes a already checked method descriptor. The parameter count, the
81 return type and the argument types are stored in the passed methodinfo.
83 *******************************************************************************/
85 static void descriptor2types (methodinfo *m)
92 types = DMNEW (u1, m->descriptor->length);
95 if (!(m->flags & ACC_STATIC)) {
100 cptr = m->descriptor->text;
102 while ((c = *cptr++) != ')') {
109 case 'Z': *tptr++ = TYPE_INT;
111 case 'J': *tptr++ = TYPE_LNG;
113 case 'F': *tptr++ = TYPE_FLT;
115 case 'D': *tptr++ = TYPE_DBL;
117 case 'L': *tptr++ = TYPE_ADR;
118 while (*cptr++ != ';');
120 case '[': *tptr++ = TYPE_ADR;
124 while (*cptr++ != ';') /* skip */;
126 default: panic ("Ill formed methodtype-descriptor");
130 /* compute return type */
137 case 'Z': m->returntype = TYPE_INT;
139 case 'J': m->returntype = TYPE_LNG;
141 case 'F': m->returntype = TYPE_FLT;
143 case 'D': m->returntype = TYPE_DBL;
146 case 'L': m->returntype = TYPE_ADR;
148 case 'V': m->returntype = TYPE_VOID;
151 default: panic ("Ill formed methodtype-descriptor");
154 m->paramcount = pcount;
155 m->paramtypes = types;
161 /* function allocate_literals **************************************************
163 Scans the JavaVM code of a method and allocates string literals (in the
164 same order as the old JIT). Needed to generate the same addresses as the
167 *******************************************************************************/
169 static void allocate_literals()
176 for (p = 0; p < jcodelength; p = nextp) {
179 nextp = p + jcommandsize[opcode];
183 if (code_get_u1(p + 1) == JAVA_IINC)
189 case JAVA_LOOKUPSWITCH:
190 nextp = ALIGN((p + 1), 4);
191 num = code_get_u4(nextp + 4);
192 nextp = nextp + 8 + 8 * num;
195 case JAVA_TABLESWITCH:
196 nextp = ALIGN ((p + 1),4);
197 num = code_get_s4(nextp + 4);
198 num = code_get_s4(nextp + 8) - num;
199 nextp = nextp + 16 + 4 * num;
203 i = code_get_u1(p+1);
204 goto pushconstantitem;
207 i = code_get_u2(p + 1);
209 if (class_constanttype(class, i) == CONSTANT_String) {
210 s = class_getconstant(class, i, CONSTANT_String);
211 (void) literalstring_new(s);
220 /*******************************************************************************
222 function 'parse' scans the JavaVM code and generates intermediate code
224 During parsing the block index table is used to store at bit pos 0
225 a flag which marks basic block starts and at position 1 to 31 the
226 intermediate instruction index. After parsing the block index table
227 is scanned, for marked positions a block is generated and the block
228 number is stored in the block index table.
230 *******************************************************************************/
232 /* intermediate code generating macros */
234 #define PINC iptr++;ipc++
235 #define LOADCONST_I(v) iptr->opc=ICMD_ICONST;iptr->op1=0;iptr->val.i=(v);PINC
236 #define LOADCONST_L(v) iptr->opc=ICMD_LCONST;iptr->op1=0;iptr->val.l=(v);PINC
237 #define LOADCONST_F(v) iptr->opc=ICMD_FCONST;iptr->op1=0;iptr->val.f=(v);PINC
238 #define LOADCONST_D(v) iptr->opc=ICMD_DCONST;iptr->op1=0;iptr->val.d=(v);PINC
239 #define LOADCONST_A(v) iptr->opc=ICMD_ACONST;iptr->op1=0;iptr->val.a=(v);PINC
240 #define OP(o) iptr->opc=(o);iptr->op1=0;iptr->val.l=0;PINC
241 #define OP1(o,o1) iptr->opc=(o);iptr->op1=(o1);iptr->val.l=(0);PINC
242 #define OP2I(o,o1,v) iptr->opc=(o);iptr->op1=(o1);iptr->val.i=(v);PINC
243 #define OP2A(o,o1,v) iptr->opc=(o);iptr->op1=(o1);iptr->val.a=(v);PINC
244 #define BUILTIN1(v,t) isleafmethod=false;iptr->opc=ICMD_BUILTIN1;iptr->op1=t;\
246 #define BUILTIN2(v,t) isleafmethod=false;iptr->opc=ICMD_BUILTIN2;iptr->op1=t;\
248 #define BUILTIN3(v,t) isleafmethod=false;iptr->opc=ICMD_BUILTIN3;iptr->op1=t;\
252 /* block generating and checking macros */
254 #define block_insert(i) {if(!(block_index[i]&1))\
255 {b_count++;block_index[i] |= 1;}}
256 #define bound_check(i) {if((i< 0) || (i>=jcodelength)) \
257 panic("branch target out of code-boundary");}
258 #define bound_check1(i) {if((i< 0) || (i>jcodelength)) \
259 panic("branch target out of code-boundary");}
264 int p; /* java instruction counter */
265 int nextp; /* start of next java instruction */
266 int opcode; /* java opcode */
267 int i; /* temporary for different uses (counters) */
268 int ipc = 0; /* intermediate instruction counter */
269 int b_count = 0; /* basic block counter */
270 int s_count = 0; /* stack element counter */
271 bool blockend = false; /* true if basic block end has been reached */
272 bool iswide = false; /* true if last instruction was a wide */
273 instruction *iptr; /* current pointer into instruction array */
277 /* generate the same addresses as the old JIT compiler */
283 /* allocate instruction array and block index table */
285 /* 1 additional for end ipc and 3 for loop unrolling */
287 block_index = DMNEW(int, jcodelength + 3);
289 /* 1 additional for TRACEBUILTIN and 4 for MONITORENTER/EXIT */
290 /* additional MONITOREXITS are reached by branches which are 3 bytes */
292 iptr = instr = DMNEW(instruction, jcodelength + 5);
294 /* initialize block_index table (unrolled four times) */
299 for (i = 0, ip = block_index; i <= jcodelength; i += 4, ip += 4) {
307 /* compute branch targets of exception table */
309 for (i = 0; i < exceptiontablelength; i++) {
310 p = extable[i].startpc;
313 p = extable[i].endpc;
317 p = extable[i].handlerpc;
322 s_count = 1 + exceptiontablelength; /* initialize stack element counter */
325 if (checksync && (method->flags & ACC_SYNCHRONIZED)) {
330 /* scan all java instructions */
332 for (p = 0; p < jcodelength; p = nextp) {
334 opcode = code_get_u1 (p); /* fetch op code */
336 block_index[p] |= (ipc << 1); /* store intermediate count */
339 block_insert(p); /* start new block */
343 nextp = p + jcommandsize[opcode]; /* compute next instruction start */
344 s_count += stackreq[opcode]; /* compute stack element count */
351 /* pushing constants onto the stack p */
354 LOADCONST_I(code_get_s1(p+1));
358 LOADCONST_I(code_get_s2(p+1));
362 i = code_get_u1(p+1);
363 goto pushconstantitem;
366 i = code_get_u2(p + 1);
370 if (i >= class->cpcount)
371 panic ("Attempt to access constant outside range");
373 switch (class->cptags[i]) {
374 case CONSTANT_Integer:
375 LOADCONST_I(((constant_integer*)
376 (class->cpinfos[i]))->value);
379 LOADCONST_L(((constant_long*)
380 (class->cpinfos[i]))->value);
383 LOADCONST_F(((constant_float*)
384 (class->cpinfos[i]))->value);
386 case CONSTANT_Double:
387 LOADCONST_D(((constant_double*)
388 (class->cpinfos[i]))->value);
390 case CONSTANT_String:
391 LOADCONST_A(literalstring_new((unicode*)
392 (class->cpinfos[i])));
394 default: panic("Invalid constant type to push");
398 case JAVA_ACONST_NULL:
409 LOADCONST_I(opcode - JAVA_ICONST_0);
414 LOADCONST_L(opcode - JAVA_LCONST_0);
420 LOADCONST_F(opcode - JAVA_FCONST_0);
425 LOADCONST_D(opcode - JAVA_DCONST_0);
428 /* loading variables onto the stack */
436 i = code_get_u1(p+1);
438 i = code_get_u2(p+1);
449 OP1(ICMD_ILOAD, opcode - JAVA_ILOAD_0);
456 OP1(ICMD_LLOAD, opcode - JAVA_LLOAD_0);
463 OP1(ICMD_FLOAD, opcode - JAVA_FLOAD_0);
470 OP1(ICMD_DLOAD, opcode - JAVA_DLOAD_0);
477 OP1(ICMD_ALOAD, opcode - JAVA_ALOAD_0);
480 /* storing stack values into local variables */
488 i = code_get_u1(p+1);
490 i = code_get_u2(p+1);
501 OP1(ICMD_ISTORE, opcode - JAVA_ISTORE_0);
508 OP1(ICMD_LSTORE, opcode - JAVA_LSTORE_0);
515 OP1(ICMD_FSTORE, opcode - JAVA_FSTORE_0);
522 OP1(ICMD_DSTORE, opcode - JAVA_DSTORE_0);
529 OP1(ICMD_ASTORE, opcode - JAVA_ASTORE_0);
537 i = code_get_u1(p + 1);
538 v = code_get_s1(p + 2);
541 i = code_get_u2(p + 1);
542 v = code_get_s2(p + 3);
550 /* wider index for loading, storing and incrementing */
557 /* managing arrays ************************************************/
560 OP2I(ICMD_CHECKASIZE, 0, 0);
561 switch (code_get_s1(p+1)) {
563 BUILTIN1((functionptr)builtin_newarray_boolean, TYPE_ADR);
566 BUILTIN1((functionptr)builtin_newarray_char, TYPE_ADR);
569 BUILTIN1((functionptr)builtin_newarray_float, TYPE_ADR);
572 BUILTIN1((functionptr)builtin_newarray_double, TYPE_ADR);
575 BUILTIN1((functionptr)builtin_newarray_byte, TYPE_ADR);
578 BUILTIN1((functionptr)builtin_newarray_short, TYPE_ADR);
581 BUILTIN1((functionptr)builtin_newarray_int, TYPE_ADR);
584 BUILTIN1((functionptr)builtin_newarray_long, TYPE_ADR);
586 default: panic("Invalid array-type to create");
591 OP2I(ICMD_CHECKASIZE, 0, 0);
592 i = code_get_u2(p+1);
593 /* array or class type ? */
594 if (class_constanttype (class, i) == CONSTANT_Arraydescriptor) {
595 LOADCONST_A(class_getconstant(class, i,
596 CONSTANT_Arraydescriptor));
597 BUILTIN2((functionptr)builtin_newarray_array, TYPE_ADR);
600 LOADCONST_A(class_getconstant(class, i, CONSTANT_Class));
601 BUILTIN2((functionptr)builtin_anewarray, TYPE_ADR);
605 case JAVA_MULTIANEWARRAY:
607 i = code_get_u2(p+1);
609 int v = code_get_u1(p+3);
610 constant_arraydescriptor *desc =
611 class_getconstant (class, i, CONSTANT_Arraydescriptor);
612 OP2A(opcode, v, desc);
634 i = p + code_get_s2(p+1);
642 i = p + code_get_s4(p+1);
651 i = code_get_u1(p+1);
653 i = code_get_u2(p+1);
677 /* table jumps ********************************/
679 case JAVA_LOOKUPSWITCH:
684 nextp = ALIGN((p + 1), 4);
685 OP2A(opcode, 0, jcode + nextp);
689 j = p + code_get_s4(nextp);
690 *((s4*)(jcode + nextp)) = j; /* restore for little endian */
695 /* number of pairs */
697 num = code_get_u4(nextp);
698 *((s4*)(jcode + nextp)) = num;
701 for (i = 0; i < num; i++) {
705 j = code_get_s4(nextp);
706 *((s4*)(jcode + nextp)) = j; /* restore for little endian */
711 j = p + code_get_s4(nextp);
712 *((s4*)(jcode + nextp)) = j; /* restore for little endian */
722 case JAVA_TABLESWITCH:
727 nextp = ALIGN((p + 1), 4);
728 OP2A(opcode, 0, jcode + nextp);
732 j = p + code_get_s4(nextp);
733 *((s4*)(jcode + nextp)) = j; /* restore for little endian */
740 j = code_get_s4(nextp);
741 *((s4*)(jcode + nextp)) = j; /* restore for little endian */
746 num = code_get_s4(nextp);
747 *((s4*)(jcode + nextp)) = num; /* restore for little endian */
752 for (i = 0; i <= num; i++) {
753 j = p + code_get_s4(nextp);
754 *((s4*)(jcode + nextp)) = j; /* restore for little endian */
764 /* load and store of object fields *******************/
767 BUILTIN3((functionptr) asm_builtin_aastore, TYPE_VOID);
772 i = code_get_u2(p + 1);
776 fr = class_getconstant (class, i, CONSTANT_Fieldref);
777 fi = class_findfield (fr->class, fr->name, fr->descriptor);
778 compiler_addinitclass (fr->class);
779 OP2A(opcode, fi->type, fi);
784 i = code_get_u2(p + 1);
788 fr = class_getconstant (class, i, CONSTANT_Fieldref);
789 fi = class_findfield (fr->class, fr->name, fr->descriptor);
790 OP2A(opcode, fi->type, fi);
795 /* method invocation *****/
797 case JAVA_INVOKESTATIC:
798 i = code_get_u2(p + 1);
803 mr = class_getconstant (class, i, CONSTANT_Methodref);
804 mi = class_findmethod (mr->class, mr->name, mr->descriptor);
805 if (! (mi->flags & ACC_STATIC))
806 panic ("Static/Nonstatic mismatch calling static method");
807 descriptor2types(mi);
809 OP2A(opcode, mi->paramcount, mi);
812 case JAVA_INVOKESPECIAL:
813 case JAVA_INVOKEVIRTUAL:
814 i = code_get_u2(p + 1);
819 mr = class_getconstant (class, i, CONSTANT_Methodref);
820 mi = class_findmethod (mr->class, mr->name, mr->descriptor);
821 if (mi->flags & ACC_STATIC)
822 panic ("Static/Nonstatic mismatch calling static method");
823 descriptor2types(mi);
825 OP2A(opcode, mi->paramcount, mi);
828 case JAVA_INVOKEINTERFACE:
829 i = code_get_u2(p + 1);
834 mr = class_getconstant (class, i, CONSTANT_InterfaceMethodref);
835 mi = class_findmethod (mr->class, mr->name, mr->descriptor);
836 if (mi->flags & ACC_STATIC)
837 panic ("Static/Nonstatic mismatch calling static method");
838 descriptor2types(mi);
840 OP2A(opcode, mi->paramcount, mi);
844 /* miscellaneous object operations *******/
847 i = code_get_u2 (p+1);
848 LOADCONST_A(class_getconstant(class, i, CONSTANT_Class));
849 BUILTIN1((functionptr) builtin_new, TYPE_ADR);
853 i = code_get_u2(p+1);
855 /* array type cast-check */
856 if (class_constanttype (class, i) == CONSTANT_Arraydescriptor) {
857 LOADCONST_A(class_getconstant(class, i, CONSTANT_Arraydescriptor));
858 BUILTIN2((functionptr) asm_builtin_checkarraycast, TYPE_ADR);
860 else { /* object type cast-check */
861 OP2A(opcode, 1, (class_getconstant(class, i, CONSTANT_Class)));
865 case JAVA_INSTANCEOF:
866 i = code_get_u2(p+1);
868 /* array type cast-check */
869 if (class_constanttype (class, i) == CONSTANT_Arraydescriptor) {
870 LOADCONST_A(class_getconstant(class, i, CONSTANT_Arraydescriptor));
871 BUILTIN2((functionptr) builtin_arrayinstanceof, TYPE_INT);
873 else { /* object type cast-check */
874 OP2A(opcode, 1, (class_getconstant(class, i, CONSTANT_Class)));
878 case JAVA_MONITORENTER:
881 #ifdef SOFTNULLPTRCHECK
883 BUILTIN1((functionptr) asm_builtin_monitorenter, TYPE_VOID);
886 /* BUILTIN1((functionptr) builtin_monitorenter, TYPE_VOID); */
887 BUILTIN1((functionptr) asm_builtin_monitorenter, TYPE_VOID);
890 BUILTIN1((functionptr) builtin_monitorenter, TYPE_VOID);
896 OP(ICMD_NULLCHECKPOP);
900 case JAVA_MONITOREXIT:
903 BUILTIN1((functionptr) builtin_monitorexit, TYPE_VOID);
912 /* any other basic operation **************************************/
931 BUILTIN2((functionptr) builtin_frem, TYPE_FLOAT);
935 BUILTIN2((functionptr) builtin_drem, TYPE_DOUBLE);
940 BUILTIN1((functionptr) builtin_f2i, TYPE_INT);
949 BUILTIN1((functionptr) builtin_f2l, TYPE_LONG);
958 BUILTIN1((functionptr) builtin_d2i, TYPE_INT);
967 BUILTIN1((functionptr) builtin_d2l, TYPE_LONG);
974 case JAVA_BREAKPOINT:
975 panic("Illegal opcode Breakpoint encountered");
1031 printf("Illegal opcode %d at instr %d", opcode, ipc);
1032 panic("encountered");
1043 if (p != jcodelength)
1044 panic("Command-sequence crosses code-boundary");
1047 panic("Code does not end with branch/return/athrow - stmt");
1049 /* adjust block count if target 0 is not first intermediate instruction */
1051 if (!block_index[0] || (block_index[0] > 1))
1054 /* copy local to global variables */
1057 block_count = b_count;
1058 stack_count = s_count + block_count * maxstack;
1060 /* allocate stack table */
1062 stack = DMNEW(stackelement, stack_count);
1067 bptr = block = DMNEW(basicblock, b_count + 1); /* one more for end ipc */
1071 /* additional block if target 0 is not first intermediate instruction */
1073 if (!block_index[0] || (block_index[0] > 1)) {
1074 bptr->iinstr = instr;
1077 bptr->type = BBTYPE_STD;
1078 bptr->branchrefs = NULL;
1079 bptr->pre_count = 0;
1084 /* allocate blocks */
1086 for (p = 0; p < jcodelength; p++)
1087 if (block_index[p] & 1) {
1088 bptr->iinstr = instr + (block_index[p] >> 1);
1090 (bptr - 1)->icount = bptr->iinstr - (bptr - 1)->iinstr;
1093 bptr->type = BBTYPE_STD;
1094 bptr->branchrefs = NULL;
1095 block_index[p] = b_count;
1096 bptr->pre_count = 0;
1101 /* allocate additional block at end */
1103 bptr->iinstr = NULL;
1104 (bptr - 1)->icount = (instr + instr_count) - (bptr - 1)->iinstr;
1108 bptr->type = BBTYPE_STD;
1109 bptr->branchrefs = NULL;
1110 bptr->pre_count = 0;
1116 * These are local overrides for various environment variables in Emacs.
1117 * Please do not remove this and leave it at the end of the file, where
1118 * Emacs will automagically detect them.
1119 * ---------------------------------------------------------------------
1122 * indent-tabs-mode: t