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 485 2003-10-20 17:16:25Z twisti $
12 include Rapid Type Analysis parse - 5/2003 - carolyn
15 *******************************************************************************/
19 /* data about the currently parsed method */
21 static classinfo *rt_class; /* class the compiled method belongs to */
22 static methodinfo *rt_method; /* pointer to method info of compiled method */
23 static utf *rt_descriptor; /* type descriptor of compiled method */
24 static int rt_jcodelength; /*length of JavaVM-codes */
25 static u1 *rt_jcode; /* pointer to start of JavaVM-code */
28 /* macros for byte code fetching ***********************************************
30 fetch a byte code of given size from position p in code array jcode
32 *******************************************************************************/
34 #define code_get_u1(p) jcode[p]
35 #define code_get_s1(p) ((s1)jcode[p])
36 #define code_get_u2(p) ((((u2)jcode[p])<<8)+jcode[p+1])
37 #define code_get_s2(p) ((s2)((((u2)jcode[p])<<8)+jcode[p+1]))
38 #define code_get_u4(p) ((((u4)jcode[p])<<24)+(((u4)jcode[p+1])<<16)\
39 +(((u4)jcode[p+2])<<8)+jcode[p+3])
40 #define code_get_s4(p) ((s4)((((u4)jcode[p])<<24)+(((u4)jcode[p+1])<<16)\
41 +(((u4)jcode[p+2])<<8)+jcode[p+3]))
47 /*#define debug_writebranch printf("op: %s i: %d label_index[i]: %d\n",icmd_names[opcode], i, label_index[i]);*/
48 #define debug_writebranch
50 /* functionc compiler_addinitclass *********************************************
52 add class into the list of classes to initialize
54 *******************************************************************************/
56 static void compiler_addinitclass (classinfo *c)
60 if (c->initialized) return;
62 cl = chain_first(uninitializedclasses);
67 cl = chain_next(uninitializedclasses);
73 sprintf(logtext, "compiler_addinitclass: ");
74 utf_sprint(logtext+strlen(logtext), c->name);
77 chain_addlast(uninitializedclasses, c);
82 sprintf(logtext, "compiler_addinitclass: ");
83 utf_sprint(logtext+strlen(logtext), c->name);
86 chain_addbefore(uninitializedclasses, c);
89 cl = chain_next(uninitializedclasses);
94 /* function descriptor2typesL ***************************************************
96 decodes a already checked method descriptor. The parameter count, the
97 return type and the argument types are stored in the passed methodinfo.
98 gets and saves classptr for object ref.s
100 *******************************************************************************/
102 classSetNode * descriptor2typesL (methodinfo *m)
109 classinfo** classtypes;
112 classSetNode *p=NULL;
113 if (debugInfo >= 1) {
114 printf("In descriptor2typesL >>>\t"); fflush(stdout);
115 utf_display(m->class->name); printf(".");
116 method_display(m);fflush(stdout);
120 desc = MNEW (char, 256);
121 types = DMNEW (u1, m->descriptor->blength);
122 classtypes = MNEW (classinfo*, m->descriptor->blength+1);
123 m->returnclass = NULL;
125 if (!(m->flags & ACC_STATIC)) {
127 if (debugInfo >= 1) {
128 printf("param #0 (this?) method class =");utf_display(m->class->name);printf("\n");
130 classtypes[pcount] = m->class;
131 p = addClassCone(p, m->class);
135 utf_ptr = m->descriptor->text + 1;
136 strcpy (desc,utf_ptr);
138 while ((c = *desc++) != ')') {
145 case 'Z': *tptr++ = TYPE_INT;
147 case 'J': *tptr++ = TYPE_LNG;
149 case 'F': *tptr++ = TYPE_FLT;
151 case 'D': *tptr++ = TYPE_DBL;
153 case 'L': *tptr++ = TYPE_ADR;
154 /* get class string */
155 class = strtok(desc,";");
156 desc = strtok(NULL,"\0");
157 /* get/save classinfo ptr */
158 classtypes[pcount-1] = class_get(utf_new_char(class));
159 p = addClassCone(p, class_get(utf_new_char(class)));
160 if (debugInfo >= 1) {
161 printf("LParam#%i 's class type is: %s\n",pcount-1,class);fflush(stdout);
162 printf("Lclasstypes[%i]=",pcount-1);fflush(stdout);
163 utf_display(classtypes[pcount-1]->name);
166 case '[': *tptr++ = TYPE_ADR;
169 /* get class string */
171 class = strtok(desc,";");
172 desc = strtok(NULL,"\0");
173 /* get/save classinfo ptr */
174 classtypes[pcount-1] = class_get(utf_new_char(class));
175 p= addClassCone(p, class_get(utf_new_char(class)));
176 if (debugInfo >= 1) {
177 printf("[Param#%i 's class type is: %s\n",pcount-1,class);
178 printf("[classtypes[%i]=",pcount-1);fflush(stdout);
179 utf_display(classtypes[pcount-1]->name);
184 classtypes[pcount-1] = NULL;
187 panic("Ill formed methodtype-descriptor");
191 /* compute return type */
197 case 'Z': m->returntype = TYPE_INT;
199 case 'J': m->returntype = TYPE_LNG;
201 case 'F': m->returntype = TYPE_FLT;
203 case 'D': m->returntype = TYPE_DBL;
206 m->returntype = TYPE_ADR;
214 m->returntype = TYPE_ADR;
216 /* get class string */
217 class = strtok(desc,";");
218 m->returnclass = class_get(utf_new_char(class));
219 if (m->returnclass == NULL) {
220 printf("class=%s :\t",class);
221 panic ("return class not found");
224 case 'V': m->returntype = TYPE_VOID;
227 default: panic("Ill formed methodtype-descriptor-ReturnType");
230 m->paramcount = pcount;
231 m->paramtypes = types;
232 m->paramclass = classtypes;
236 for (i=0; i< m->paramcount; i++) {
237 if ((m->paramtypes[i] == TYPE_ADR) && (m->paramclass[i] != NULL)) {
238 printf("Param #%i is:\t",i);
239 utf_display(m->paramclass[i]->name);
244 if ((m->returntype == TYPE_ADR) && (m->returnclass != NULL)) {
245 printf("\tReturn Type is:\t"); fflush(stdout);
246 utf_display(m->returnclass->name);
250 printf("params2types: START results in a set \n");
251 printf("param2types: A Set size=%i=\n",sizeOfSet(p));
258 /* function descriptor2types ***************************************************
260 decodes a already checked method descriptor. The parameter count, the
261 return type and the argument types are stored in the passed methodinfo.
263 *******************************************************************************/
265 static void descriptor2types (methodinfo *m)
271 types = DMNEW (u1, m->descriptor->blength);
274 if (!(m->flags & ACC_STATIC)) {
279 utf_ptr = m->descriptor->text + 1;
281 while ((c = *utf_ptr++) != ')') {
288 case 'Z': *tptr++ = TYPE_INT;
290 case 'J': *tptr++ = TYPE_LNG;
292 case 'F': *tptr++ = TYPE_FLT;
294 case 'D': *tptr++ = TYPE_DBL;
296 case 'L': *tptr++ = TYPE_ADR;
297 while (*utf_ptr++ != ';');
299 case '[': *tptr++ = TYPE_ADR;
303 while (*utf_ptr++ != ';') /* skip */;
305 default: panic ("Ill formed methodtype-descriptor");
309 /* compute return type */
311 switch (*utf_ptr++) {
316 case 'Z': m->returntype = TYPE_INT;
318 case 'J': m->returntype = TYPE_LNG;
320 case 'F': m->returntype = TYPE_FLT;
322 case 'D': m->returntype = TYPE_DBL;
325 case 'L': m->returntype = TYPE_ADR;
327 case 'V': m->returntype = TYPE_VOID;
330 default: panic ("Ill formed methodtype-descriptor");
333 m->paramcount = pcount;
334 m->paramtypes = types;
340 /* function allocate_literals **************************************************
342 Scans the JavaVM code of a method and allocates string literals (in the
343 same order as the old JIT). Needed to generate the same addresses as the
346 *******************************************************************************/
348 static void allocate_literals()
355 for (p = 0; p < jcodelength; p = nextp) {
358 nextp = p + jcommandsize[opcode];
362 if (code_get_u1(p + 1) == JAVA_IINC)
368 case JAVA_LOOKUPSWITCH:
369 nextp = ALIGN((p + 1), 4);
370 num = code_get_u4(nextp + 4);
371 nextp = nextp + 8 + 8 * num;
374 case JAVA_TABLESWITCH:
375 nextp = ALIGN ((p + 1),4);
376 num = code_get_s4(nextp + 4);
377 num = code_get_s4(nextp + 8) - num;
378 nextp = nextp + 16 + 4 * num;
382 i = code_get_u1(p+1);
383 goto pushconstantitem;
386 i = code_get_u2(p + 1);
388 if (class_constanttype(class, i) == CONSTANT_String) {
389 s = class_getconstant(class, i, CONSTANT_String);
390 (void) literalstring_new(s);
399 /*******************************************************************************
401 function 'parse' scans the JavaVM code and generates intermediate code
403 During parsing the block index table is used to store at bit pos 0
404 a flag which marks basic block starts and at position 1 to 31 the
405 intermediate instruction index. After parsing the block index table
406 is scanned, for marked positions a block is generated and the block
407 number is stored in the block index table.
409 *******************************************************************************/
411 /* intermediate code generating macros */
413 #define PINC iptr++;ipc++
414 #define LOADCONST_I(v) iptr->opc=ICMD_ICONST;iptr->op1=0;iptr->val.i=(v);PINC
415 #define LOADCONST_L(v) iptr->opc=ICMD_LCONST;iptr->op1=0;iptr->val.l=(v);PINC
416 #define LOADCONST_F(v) iptr->opc=ICMD_FCONST;iptr->op1=0;iptr->val.f=(v);PINC
417 #define LOADCONST_D(v) iptr->opc=ICMD_DCONST;iptr->op1=0;iptr->val.d=(v);PINC
418 #define LOADCONST_A(v) iptr->opc=ICMD_ACONST;iptr->op1=0;iptr->val.a=(v);PINC
419 #define OP(o) iptr->opc=(o);iptr->op1=0;iptr->val.l=0;PINC
420 #define OP1(o,o1) iptr->opc=(o);iptr->op1=(o1);iptr->val.l=(0);PINC
421 #define OP2I(o,o1,v) iptr->opc=(o);iptr->op1=(o1);iptr->val.i=(v);PINC
422 #define OP2A(o,o1,v) iptr->opc=(o);iptr->op1=(o1);iptr->val.a=(v);PINC
423 #define BUILTIN1(v,t) isleafmethod=false;iptr->opc=ICMD_BUILTIN1;iptr->op1=t;\
425 #define BUILTIN2(v,t) isleafmethod=false;iptr->opc=ICMD_BUILTIN2;iptr->op1=t;\
427 #define BUILTIN3(v,t) isleafmethod=false;iptr->opc=ICMD_BUILTIN3;iptr->op1=t;\
431 /* block generating and checking macros */
433 #define block_insert(i) {if(!(block_index[i]&1))\
434 {b_count++;block_index[i] |= 1;}}
435 #define bound_check(i) {if((i< 0) || (i>=cumjcodelength)) \
436 panic("branch target out of code-boundary");}
437 #define bound_check1(i) {if((i< 0) || (i>cumjcodelength)) \
438 panic("branch target out of code-boundary");}
439 /* FIXME really use cumjcodelength for the bound_checkers ? */
441 static xtable* fillextable (xtable* extable, exceptiontable *raw_extable, int exceptiontablelength, int *label_index, int *block_count)
445 if (exceptiontablelength == 0)
448 b_count = *block_count;
449 for (i = 0; i < exceptiontablelength; i++) {
451 p = raw_extable[i].startpc;
452 if (label_index != NULL) p = label_index[p];
453 extable[i].startpc = p;
457 p = raw_extable[i].endpc;
458 if (label_index != NULL) p = label_index[p];
459 extable[i].endpc = p;
461 if (p < cumjcodelength)
464 p = raw_extable[i].handlerpc;
465 if (label_index != NULL) p = label_index[p];
466 extable[i].handlerpc = p;
470 extable[i].catchtype = raw_extable[i].catchtype;
472 extable[i].next = NULL;
473 extable[i].down = &extable[i+1];
475 *block_count = b_count;
476 return &extable[i]; /* return the next free xtable* */
481 int p; /* java instruction counter */
482 int nextp; /* start of next java instruction */
483 int opcode; /* java opcode */
484 int i; /* temporary for different uses (counters) */
485 int ipc = 0; /* intermediate instruction counter */
486 int b_count = 0; /* basic block counter */
487 int s_count = 0; /* stack element counter */
488 bool blockend = false; /* true if basic block end has been reached */
489 bool iswide = false; /* true if last instruction was a wide */
490 instruction *iptr; /* current pointer into instruction array */
491 int gp; /* global java instruction counter */
492 /* inlining info for current method */
493 inlining_methodinfo *inlinfo = inlining_rootinfo, *tmpinlinf;
494 int nextgp = -1; /* start of next method to be inlined */
495 int *label_index = NULL; /* label redirection table */
496 int firstlocal = 0; /* first local variable of method */
497 xtable* nextex; /* points next free entry in extable */
504 label_index = inlinfo->label_index;
505 maxstack = cummaxstack;
506 exceptiontablelength=cumextablelength;
509 useinltmp = useinlining; /*FIXME remove this after debugging */
510 /*useinlining = false;*/ /* and merge the if-statements */
513 cumjcodelength = jcodelength;
515 tmpinlinf = (inlining_methodinfo*) list_first(inlinfo->inlinedmethods);
516 if (tmpinlinf != NULL) nextgp = tmpinlinf->startgp;
519 /*RTAprint*/ if ( ((opt_rt) ||(opt_xta) || (opt_vta)) && ((pOpcodes == 2) || (pOpcodes == 3)) )
520 /*RTAprint*/ {printf("PARSE method name =");
521 /*RTAprint*/ utf_display(method->class->name);printf(".");
522 /*RTAprint*/ method_display(method); printf(">\n\n");fflush(stdout);}
523 if ((opt_rt) || (opt_xta)) {
524 RT_jit_parse(method);
528 printf("VTA requested, but not yet implemented\n");
533 /* generate the same addresses as the old JIT compiler */
539 /* allocate instruction array and block index table */
541 /* 1 additional for end ipc and 3 for loop unrolling */
543 block_index = DMNEW(int, cumjcodelength + 4);
545 /* 1 additional for TRACEBUILTIN and 4 for MONITORENTER/EXIT */
546 /* additional MONITOREXITS are reached by branches which are 3 bytes */
548 iptr = instr = DMNEW(instruction, cumjcodelength + 5);
550 /* initialize block_index table (unrolled four times) */
555 for (i = 0, ip = block_index; i <= cumjcodelength; i += 4, ip += 4) {
563 /* compute branch targets of exception table */
565 extable = DMNEW(xtable, exceptiontablelength + 1);
567 for (i = 0; i < method->exceptiontablelength; i++) {
569 p = extable[i].startpc = raw_extable[i].startpc;
570 if (useinlining) p = label_index[p];
574 p = extable[i].endpc = raw_extable[i].endpc;
575 if (useinlining) p = label_index[p];
577 if (p < cumjcodelength)
580 p = extable[i].handlerpc = raw_extable[i].handlerpc;
584 extable[i].catchtype = raw_extable[i].catchtype;
586 extable[i].next = NULL;
587 extable[i].down = &extable[i+1];
591 nextex = fillextable(extable, raw_extable, method->exceptiontablelength, label_index, &b_count);
593 s_count = 1 + exceptiontablelength; /* initialize stack element counter */
596 if (checksync && (method->flags & ACC_SYNCHRONIZED)) {
601 /* scan all java instructions */
603 for (p = 0, gp = 0; p < jcodelength; gp += (nextp - p), p = nextp) {
605 /* DEBUG printf("p:%d gp:%d ",p,gp); */
608 if ((useinlining) && (gp == nextgp)) {
610 bool *readonly = NULL;
612 opcode = code_get_u1 (p);
613 nextp = p += jcommandsize[opcode];
614 tmpinlinf = list_first(inlinfo->inlinedmethods);
615 firstlocal = tmpinlinf->firstlocal;
616 label_index = tmpinlinf->label_index;
617 readonly = tmpinlinf->readonly;
618 for (i=0, tptr=tmpinlinf->method->paramtypes + tmpinlinf->method->paramcount - 1 ; i<tmpinlinf->method->paramcount; i++, tptr--)
622 if ( (i==0) && inlineparamopt) {
623 OP1(ICMD_CLEAR_ARGREN, firstlocal);
626 if ( !inlineparamopt || !readonly[i] )
628 else op = ICMD_READONLY_ARG;
631 OP1(op, firstlocal + tmpinlinf->method->paramcount - 1 - i);
633 /* block_index[gp] |= (ipc << 1);*/ /*FIXME: necessary ? */
635 inlining_save_compiler_variables();
636 inlining_set_compiler_variables(tmpinlinf);
637 if (inlinfo->inlinedmethods == NULL) gp = -1;
639 tmpinlinf = list_first(inlinfo->inlinedmethods);
640 nextgp = (tmpinlinf != NULL) ? tmpinlinf->startgp : -1;
642 if (method->exceptiontablelength > 0)
643 nextex = fillextable(nextex, method->exceptiontable, method->exceptiontablelength, label_index, &b_count);
647 opcode = code_get_u1 (p); /* fetch op code */
650 /*RTAprint*/ if ((opt_rt) && ((pOpcodes == 2) || (pOpcodes == 3)) )
651 /*RTAprint*/ {printf("Parse<%i> p=%i<%i< opcode=<%i> %s\n",
652 /*RTAprint*/ pOpcodes, p,rt_jcodelength,opcode,icmd_names[opcode]);}
654 block_index[gp] |= (ipc << 1); /* store intermediate count */
657 block_insert(gp); /* start new block */
661 nextp = p + jcommandsize[opcode]; /* compute next instruction start */
662 s_count += stackreq[opcode]; /* compute stack element count */
669 /* pushing constants onto the stack p */
672 LOADCONST_I(code_get_s1(p+1));
676 LOADCONST_I(code_get_s2(p+1));
680 i = code_get_u1(p+1);
681 goto pushconstantitem;
684 i = code_get_u2(p + 1);
688 if (i >= class->cpcount)
689 panic ("Attempt to access constant outside range");
691 switch (class->cptags[i]) {
692 case CONSTANT_Integer:
693 LOADCONST_I(((constant_integer*)
694 (class->cpinfos[i]))->value);
697 LOADCONST_L(((constant_long*)
698 (class->cpinfos[i]))->value);
701 LOADCONST_F(((constant_float*)
702 (class->cpinfos[i]))->value);
704 case CONSTANT_Double:
705 LOADCONST_D(((constant_double*)
706 (class->cpinfos[i]))->value);
708 case CONSTANT_String:
709 LOADCONST_A(literalstring_new((utf*)
710 (class->cpinfos[i])));
712 default: panic("Invalid constant type to push");
716 case JAVA_ACONST_NULL:
727 LOADCONST_I(opcode - JAVA_ICONST_0);
732 LOADCONST_L(opcode - JAVA_LCONST_0);
738 LOADCONST_F(opcode - JAVA_FCONST_0);
743 LOADCONST_D(opcode - JAVA_DCONST_0);
746 /* loading variables onto the stack */
754 i = code_get_u1(p+1);
756 i = code_get_u2(p+1);
760 OP1(opcode, i + firstlocal);
767 OP1(ICMD_ILOAD, opcode - JAVA_ILOAD_0 + firstlocal);
774 OP1(ICMD_LLOAD, opcode - JAVA_LLOAD_0 + firstlocal);
781 OP1(ICMD_FLOAD, opcode - JAVA_FLOAD_0 + firstlocal);
788 OP1(ICMD_DLOAD, opcode - JAVA_DLOAD_0 + firstlocal);
795 OP1(ICMD_ALOAD, opcode - JAVA_ALOAD_0 + firstlocal);
798 /* storing stack values into local variables */
806 i = code_get_u1(p+1);
808 i = code_get_u2(p+1);
812 OP1(opcode, i + firstlocal);
819 OP1(ICMD_ISTORE, opcode - JAVA_ISTORE_0 + firstlocal);
826 OP1(ICMD_LSTORE, opcode - JAVA_LSTORE_0 + firstlocal);
833 OP1(ICMD_FSTORE, opcode - JAVA_FSTORE_0 + firstlocal);
840 OP1(ICMD_DSTORE, opcode - JAVA_DSTORE_0 + firstlocal);
847 OP1(ICMD_ASTORE, opcode - JAVA_ASTORE_0 + firstlocal);
855 i = code_get_u1(p + 1);
856 v = code_get_s1(p + 2);
859 i = code_get_u2(p + 1);
860 v = code_get_s2(p + 3);
864 OP2I(opcode, i + firstlocal, v);
868 /* wider index for loading, storing and incrementing */
875 /* managing arrays ************************************************/
878 OP2I(ICMD_CHECKASIZE, 0, 0);
879 switch (code_get_s1(p+1)) {
881 BUILTIN1((functionptr)builtin_newarray_boolean, TYPE_ADR);
884 BUILTIN1((functionptr)builtin_newarray_char, TYPE_ADR);
887 BUILTIN1((functionptr)builtin_newarray_float, TYPE_ADR);
890 BUILTIN1((functionptr)builtin_newarray_double, TYPE_ADR);
893 BUILTIN1((functionptr)builtin_newarray_byte, TYPE_ADR);
896 BUILTIN1((functionptr)builtin_newarray_short, TYPE_ADR);
899 BUILTIN1((functionptr)builtin_newarray_int, TYPE_ADR);
902 BUILTIN1((functionptr)builtin_newarray_long, TYPE_ADR);
904 default: panic("Invalid array-type to create");
909 OP2I(ICMD_CHECKASIZE, 0, 0);
910 i = code_get_u2(p+1);
911 /* array or class type ? */
912 if (class_constanttype (class, i) == CONSTANT_Arraydescriptor) {
914 LOADCONST_A(class_getconstant(class, i,
915 CONSTANT_Arraydescriptor));
916 #if defined(__I386__)
917 BUILTIN2((functionptr) asm_builtin_newarray_array, TYPE_ADR);
919 BUILTIN2((functionptr)builtin_newarray_array, TYPE_ADR);
923 LOADCONST_A(class_getconstant(class, i, CONSTANT_Class));
925 #if defined(__I386__)
926 BUILTIN2((functionptr) asm_builtin_anewarray, TYPE_ADR);
928 BUILTIN2((functionptr)builtin_anewarray, TYPE_ADR);
933 case JAVA_MULTIANEWARRAY:
935 i = code_get_u2(p+1);
937 int v = code_get_u1(p+3);
938 constant_arraydescriptor *desc =
939 class_getconstant (class, i, CONSTANT_Arraydescriptor);
940 OP2A(opcode, v, desc);
962 i = p + code_get_s2(p+1);
974 i = p + code_get_s4(p+1);
987 i = code_get_u1(p+1);
989 i = code_get_u2(p+1);
996 if (isinlinedmethod) {
997 OP1(ICMD_GOTO, inlinfo->stopgp);
1001 OP1(opcode, i + firstlocal);
1012 if (isinlinedmethod) {
1013 /* if (p==jcodelength-1) {*/ /* return is at end of inlined method */
1018 OP1(ICMD_GOTO, inlinfo->stopgp);
1032 /* table jumps ********************************/
1034 case JAVA_LOOKUPSWITCH:
1040 nextp = ALIGN((p + 1), 4);
1042 tablep = (s4*)(jcode + nextp);
1045 num = code_get_u4(nextp + 4);
1046 tablep = DMNEW(s4, num * 2 + 2);
1049 OP2A(opcode, 0, tablep);
1051 /* default target */
1053 j = p + code_get_s4(nextp);
1054 if (useinlining) j = label_index[j];
1055 *tablep = j; /* restore for little endian */
1061 /* number of pairs */
1063 num = code_get_u4(nextp);
1068 for (i = 0; i < num; i++) {
1072 j = code_get_s4(nextp);
1073 *tablep = j; /* restore for little endian */
1079 j = p + code_get_s4(nextp);
1080 if (useinlining) j = label_index[j];
1081 *tablep = j; /* restore for little endian */
1092 case JAVA_TABLESWITCH:
1098 nextp = ALIGN((p + 1), 4);
1100 tablep = (s4*)(jcode + nextp);
1103 num = code_get_u4(nextp + 8) - code_get_u4(nextp + 4);
1104 tablep = DMNEW(s4, num + 1 + 3);
1107 OP2A(opcode, 0, tablep);
1109 /* default target */
1111 j = p + code_get_s4(nextp);
1112 if (useinlining) j = label_index[j];
1113 *tablep = j; /* restore for little endian */
1121 j = code_get_s4(nextp);
1122 *tablep = j; /* restore for little endian */
1128 num = code_get_s4(nextp);
1129 *tablep = num; /* restore for little endian */
1135 for (i = 0; i <= num; i++) {
1136 j = p + code_get_s4(nextp);
1137 if (useinlining) j = label_index[j];
1138 *tablep = j; /* restore for little endian */
1149 /* load and store of object fields *******************/
1152 BUILTIN3((functionptr) asm_builtin_aastore, TYPE_VOID);
1155 case JAVA_PUTSTATIC:
1156 case JAVA_GETSTATIC:
1157 i = code_get_u2(p + 1);
1159 constant_FMIref *fr;
1161 fr = class_getconstant (class, i, CONSTANT_Fieldref);
1162 fi = class_findfield (fr->class, fr->name, fr->descriptor);
1163 compiler_addinitclass (fr->class);
1164 OP2A(opcode, fi->type, fi);
1169 i = code_get_u2(p + 1);
1171 constant_FMIref *fr;
1173 fr = class_getconstant (class, i, CONSTANT_Fieldref);
1174 fi = class_findfield (fr->class, fr->name, fr->descriptor);
1175 OP2A(opcode, fi->type, fi);
1180 /* method invocation *****/
1182 case JAVA_INVOKESTATIC:
1183 i = code_get_u2(p + 1);
1185 constant_FMIref *mr;
1188 mr = class_getconstant (class, i, CONSTANT_Methodref);
1189 mi = class_findmethod (mr->class, mr->name, mr->descriptor);
1190 /*RTAprint*/ if (((pOpcodes == 2) || (pOpcodes == 3)) && opt_rt)
1191 /*RTAprint*/ {printf(" method name =");
1192 /*RTAprint*/ utf_display(mi->class->name); printf(".");
1193 /*RTAprint*/ utf_display(mi->name);printf("\tINVOKE STATIC\n");
1194 /*RTAprint*/ fflush(stdout);}
1195 if (! (mi->flags & ACC_STATIC))
1196 panic ("Static/Nonstatic mismatch calling static method");
1197 descriptor2types(mi);
1200 OP2A(opcode, mi->paramcount, mi);
1203 case JAVA_INVOKESPECIAL:
1204 case JAVA_INVOKEVIRTUAL:
1205 i = code_get_u2(p + 1);
1207 constant_FMIref *mr;
1210 mr = class_getconstant (class, i, CONSTANT_Methodref);
1211 mi = class_findmethod (mr->class, mr->name, mr->descriptor);
1212 /*RTAprint*/ if (((pOpcodes == 2) || (pOpcodes == 3)) && opt_rt)
1213 /*RTAprint*/ {printf(" method name =");
1215 /*RTAprint*/ utf_display(mi->class->name); printf(".");
1216 /*RTAprint*/ utf_display(mi->name);printf("\tINVOKE SPECIAL/VIRTUAL\n");
1217 /*RTAprint*/ fflush(stdout);}
1219 if (mi->flags & ACC_STATIC)
1220 panic ("Static/Nonstatic mismatch calling static method");
1221 descriptor2types(mi);
1223 OP2A(opcode, mi->paramcount, mi);
1226 case JAVA_INVOKEINTERFACE:
1227 i = code_get_u2(p + 1);
1229 constant_FMIref *mr;
1232 mr = class_getconstant (class, i, CONSTANT_InterfaceMethodref);
1233 mi = class_findmethod (mr->class, mr->name, mr->descriptor);
1234 if (mi->flags & ACC_STATIC)
1235 panic ("Static/Nonstatic mismatch calling static method");
1236 descriptor2types(mi);
1238 OP2A(opcode, mi->paramcount, mi);
1242 /* miscellaneous object operations *******/
1245 i = code_get_u2 (p+1);
1247 LOADCONST_A(class_getconstant(class, i, CONSTANT_Class));
1249 BUILTIN1((functionptr) builtin_new, TYPE_ADR);
1252 case JAVA_CHECKCAST:
1253 i = code_get_u2(p+1);
1255 /* array type cast-check */
1256 if (class_constanttype (class, i) == CONSTANT_Arraydescriptor) {
1257 LOADCONST_A(class_getconstant(class, i, CONSTANT_Arraydescriptor));
1259 BUILTIN2((functionptr) asm_builtin_checkarraycast, TYPE_ADR);
1261 else { /* object type cast-check */
1263 LOADCONST_A(class_getconstant(class, i, CONSTANT_Class));
1265 BUILTIN2((functionptr) asm_builtin_checkcast, TYPE_ADR);
1267 OP2A(opcode, 1, (class_getconstant(class, i, CONSTANT_Class)));
1271 case JAVA_INSTANCEOF:
1272 i = code_get_u2(p+1);
1274 /* array type cast-check */
1275 if (class_constanttype (class, i) == CONSTANT_Arraydescriptor) {
1276 LOADCONST_A(class_getconstant(class, i, CONSTANT_Arraydescriptor));
1278 #if defined(__I386__)
1279 BUILTIN2((functionptr) asm_builtin_arrayinstanceof, TYPE_INT);
1281 BUILTIN2((functionptr) builtin_arrayinstanceof, TYPE_INT);
1284 else { /* object type cast-check */
1286 LOADCONST_A(class_getconstant(class, i, CONSTANT_Class));
1288 BUILTIN2((functionptr) builtin_instanceof, TYPE_INT);
1290 OP2A(opcode, 1, (class_getconstant(class, i, CONSTANT_Class)));
1294 case JAVA_MONITORENTER:
1297 BUILTIN1((functionptr) asm_builtin_monitorenter, TYPE_VOID);
1301 OP(ICMD_NULLCHECKPOP);
1305 case JAVA_MONITOREXIT:
1308 BUILTIN1((functionptr) asm_builtin_monitorexit, TYPE_VOID);
1317 /* any other basic operation **************************************/
1336 #if defined(__I386__)
1339 BUILTIN2((functionptr) builtin_frem, TYPE_FLOAT);
1344 #if defined(__I386__)
1347 BUILTIN2((functionptr) builtin_drem, TYPE_DOUBLE);
1353 BUILTIN1((functionptr) builtin_f2i, TYPE_INT);
1362 BUILTIN1((functionptr) builtin_f2l, TYPE_LONG);
1371 BUILTIN1((functionptr) builtin_d2i, TYPE_INT);
1380 BUILTIN1((functionptr) builtin_d2l, TYPE_LONG);
1387 case JAVA_BREAKPOINT:
1388 panic("Illegal opcode Breakpoint encountered");
1444 printf("Illegal opcode %d at instr %d", opcode, ipc);
1445 panic("encountered");
1456 if ((isinlinedmethod) && (p==jcodelength-1)) { /*end of an inlined method */
1457 /* printf("setting gp from %d to %d\n",gp, inlinfo->stopgp); */
1458 gp = inlinfo->stopgp;
1459 inlining_restore_compiler_variables();
1460 list_remove(inlinfo->inlinedmethods, list_first(inlinfo->inlinedmethods));
1461 if (inlinfo->inlinedmethods == NULL) nextgp = -1;
1463 tmpinlinf = list_first(inlinfo->inlinedmethods);
1464 nextgp = (tmpinlinf != NULL) ? tmpinlinf->startgp : -1;
1466 /* printf("nextpgp: %d\n", nextgp); */
1467 label_index=inlinfo->label_index;
1468 firstlocal = inlinfo->firstlocal;
1472 if (p != jcodelength)
1473 panic("Command-sequence crosses code-boundary");
1476 panic("Code does not end with branch/return/athrow - stmt");
1478 /* adjust block count if target 0 is not first intermediate instruction */
1480 if (!block_index[0] || (block_index[0] > 1))
1483 /* copy local to global variables */
1486 block_count = b_count;
1487 stack_count = s_count + block_count * maxstack;
1489 /* allocate stack table */
1491 stack = DMNEW(stackelement, stack_count);
1496 bptr = block = DMNEW(basicblock, b_count + 1); /* one more for end ipc */
1501 /* additional block if target 0 is not first intermediate instruction */
1503 if (!block_index[0] || (block_index[0] > 1)) {
1504 bptr->iinstr = instr;
1507 bptr->type = BBTYPE_STD;
1508 bptr->branchrefs = NULL;
1509 bptr->pre_count = 0;
1510 bptr->debug_nr = c_debug_nr++;
1513 (bptr - 1)->next = bptr;
1517 /* allocate blocks */
1520 for (p = 0; p < cumjcodelength; p++)
1522 if (block_index[p] & 1) {
1523 bptr->iinstr = instr + (block_index[p] >> 1);
1524 bptr->debug_nr = c_debug_nr++;
1526 (bptr - 1)->icount = bptr->iinstr - (bptr - 1)->iinstr;
1530 bptr->type = BBTYPE_STD;
1531 bptr->branchrefs = NULL;
1532 block_index[p] = b_count;
1533 bptr->pre_count = 0;
1537 (bptr - 1)->next = bptr;
1540 /* allocate additional block at end */
1543 bptr->instack = bptr->outstack = NULL;
1544 bptr->indepth = bptr->outdepth = 0;
1545 bptr->iinstr = NULL;
1546 (bptr - 1)->icount = (instr + instr_count) - (bptr - 1)->iinstr;
1551 bptr->type = BBTYPE_STD;
1552 bptr->branchrefs = NULL;
1553 bptr->pre_count = 0;
1554 bptr->debug_nr = c_debug_nr++;
1556 (bptr - 1)->next = bptr;
1561 if (exceptiontablelength > 0)
1562 extable[exceptiontablelength-1].down = NULL;
1566 for (i = 0; i < exceptiontablelength; ++i) {
1567 p = extable[i].startpc;
1568 extable[i].start = block + block_index[p];
1570 p = extable[i].endpc;
1571 extable[i].end = block + block_index[p];
1573 p = extable[i].handlerpc;
1574 extable[i].handler = block + block_index[p];
1578 if (useinlining) inlining_cleanup();
1579 useinlining = useinltmp;
1582 #include "parseRT.h"
1585 * These are local overrides for various environment variables in Emacs.
1586 * Please do not remove this and leave it at the end of the file, where
1587 * Emacs will automagically detect them.
1588 * ---------------------------------------------------------------------
1591 * indent-tabs-mode: t