1 /* jit/parse.c - parser for JavaVM to intermediate code translation
3 Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003
4 R. Grafl, A. Krall, C. Kruegel, C. Oates, R. Obermaisser,
5 M. Probst, S. Ring, E. Steiner, C. Thalinger, D. Thuernbeck,
6 P. Tomsich, J. Wenninger
8 This file is part of CACAO.
10 This program is free software; you can redistribute it and/or
11 modify it under the terms of the GNU General Public License as
12 published by the Free Software Foundation; either version 2, or (at
13 your option) any later version.
15 This program is distributed in the hope that it will be useful, but
16 WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
25 Contact: cacao@complang.tuwien.ac.at
29 Changes: Carolyn Oates
32 $Id: parse.c 1419 2004-10-21 09:59:33Z carolyn $
43 #include "loop/loop.h"
46 #include "exceptions.h"
51 #include "statistics.h"
52 #include "toolbox/memory.h"
53 #include "toolbox/logging.h"
55 #define METHINFO(mm) \
57 printf("PARSE method name ="); \
58 utf_display(mm->class->name); \
63 #define DEBUGMETH(mm) \
66 printf("PARSE method name ="); \
67 utf_display(mm->class->name); \
74 if (DEBUG4 == true) {printf("Parse p=%i<%i< opcode=<%i> %s\n", \
75 p, m->jcodelength,opcode,opcode_names[opcode]);}
79 bool DEBUG4 = false; /*opcodes*/
82 #define debug_writebranch if (DEBUG2==true) printf("op:: %s i: %d label_index[i]: %d label_index=%p\n",opcode_names[opcode], i, label_index[i],label_index);
83 #define debug_writebranch1
86 /* function descriptor2typesL ***************************************************
88 decodes a already checked method descriptor. The parameter count, the
89 return type and the argument types are stored in the passed methodinfo.
90 gets and saves classptr for object ref.s
92 *******************************************************************************/
94 classSetNode *descriptor2typesL(methodinfo *m)
101 classinfo** classtypes;
104 classSetNode *p=NULL;
105 if (debugInfo >= 1) {
106 printf("In descriptor2typesL >>>\t"); fflush(stdout);
107 utf_display(m->class->name); printf(".");
108 method_display(m);fflush(stdout);
112 desc = MNEW (char, 256);
113 types = DMNEW (u1, m->descriptor->blength);
114 classtypes = MNEW (classinfo*, m->descriptor->blength+1);
115 m->returnclass = NULL;
117 if (!(m->flags & ACC_STATIC)) {
119 if (debugInfo >= 1) {
120 printf("param #0 (this?) method class =");utf_display(m->class->name);printf("\n");
122 classtypes[pcount] = m->class;
123 p = addClassCone(p, m->class);
127 utf_ptr = m->descriptor->text + 1;
128 strcpy (desc,utf_ptr);
130 while ((c = *desc++) != ')') {
137 case 'Z': *tptr++ = TYPE_INT;
139 case 'J': *tptr++ = TYPE_LNG;
141 case 'F': *tptr++ = TYPE_FLT;
143 case 'D': *tptr++ = TYPE_DBL;
145 case 'L': *tptr++ = TYPE_ADR;
146 /* get class string */
147 class = strtok(desc,";");
148 desc = strtok(NULL,"\0");
149 /* get/save classinfo ptr */
150 classtypes[pcount-1] = class_get(utf_new_char(class));
151 p = addClassCone(p, class_get(utf_new_char(class)));
152 if (debugInfo >= 1) {
153 printf("LParam#%i 's class type is: %s\n",pcount-1,class);fflush(stdout);
154 printf("Lclasstypes[%i]=",pcount-1);fflush(stdout);
155 utf_display(classtypes[pcount-1]->name);
158 case '[': *tptr++ = TYPE_ADR;
161 /* get class string */
163 class = strtok(desc,";");
164 desc = strtok(NULL,"\0");
165 /* get/save classinfo ptr */
166 classtypes[pcount-1] = class_get(utf_new_char(class));
167 p= addClassCone(p, class_get(utf_new_char(class)));
168 if (debugInfo >= 1) {
169 printf("[Param#%i 's class type is: %s\n",pcount-1,class);
170 printf("[classtypes[%i]=",pcount-1);fflush(stdout);
171 utf_display(classtypes[pcount-1]->name);
176 classtypes[pcount-1] = NULL;
179 panic("Ill formed methodtype-descriptor");
183 /* compute return type */
189 case 'Z': m->returntype = TYPE_INT;
191 case 'J': m->returntype = TYPE_LNG;
193 case 'F': m->returntype = TYPE_FLT;
195 case 'D': m->returntype = TYPE_DBL;
198 m->returntype = TYPE_ADR;
206 m->returntype = TYPE_ADR;
208 /* get class string */
209 class = strtok(desc,";");
210 m->returnclass = class_get(utf_new_char(class));
211 if (m->returnclass == NULL) {
212 printf("class=%s :\t",class);
213 panic ("return class not found");
216 case 'V': m->returntype = TYPE_VOID;
219 default: panic("Ill formed methodtype-descriptor-ReturnType");
222 m->paramcount = pcount;
223 m->paramtypes = types;
224 m->paramclass = classtypes;
228 for (i=0; i< m->paramcount; i++) {
229 if ((m->paramtypes[i] == TYPE_ADR) && (m->paramclass[i] != NULL)) {
230 printf("Param #%i is:\t",i);
231 utf_display(m->paramclass[i]->name);
236 if ((m->returntype == TYPE_ADR) && (m->returnclass != NULL)) {
237 printf("\tReturn Type is:\t"); fflush(stdout);
238 utf_display(m->returnclass->name);
242 printf("params2types: START results in a set \n");
243 printf("param2types: A Set size=%i=\n",sizeOfSet(p));
252 /* function descriptor2types ***************************************************
254 decodes a already checked method descriptor. The parameter count, the
255 return type and the argument types are stored in the passed methodinfo.
257 *******************************************************************************/
259 void descriptor2types(methodinfo *m)
265 types = DMNEW(u1, m->descriptor->blength);
268 if (!(m->flags & ACC_STATIC)) {
273 utf_ptr = m->descriptor->text + 1;
275 while ((c = *utf_ptr++) != ')') {
296 while (*utf_ptr++ != ';');
303 while (*utf_ptr++ != ';') /* skip */;
306 panic("Ill formed methodtype-descriptor");
310 /* compute return type */
312 switch (*utf_ptr++) {
318 m->returntype = TYPE_INT;
321 m->returntype = TYPE_LNG;
324 m->returntype = TYPE_FLT;
327 m->returntype = TYPE_DBL;
331 m->returntype = TYPE_ADR;
334 m->returntype = TYPE_VOID;
337 panic("Ill formed methodtype-descriptor");
340 m->paramcount = pcount;
341 m->paramtypes = types;
346 /*******************************************************************************
348 function 'parse' scans the JavaVM code and generates intermediate code
350 During parsing the block index table is used to store at bit pos 0
351 a flag which marks basic block starts and at position 1 to 31 the
352 intermediate instruction index. After parsing the block index table
353 is scanned, for marked positions a block is generated and the block
354 number is stored in the block index table.
356 *******************************************************************************/
358 static exceptiontable* fillextable(methodinfo *m,
359 exceptiontable* extable, exceptiontable *raw_extable,
360 int exceptiontablelength,
361 int *label_index, int *block_count,
362 t_inlining_globals *inline_env)
366 if (exceptiontablelength == 0)
369 //if (m->exceptiontablelength > 0) {
371 // printf("m->exceptiontablelength=%i\n",m->exceptiontablelength);
372 // panic("exceptiontablelength > 0");
375 b_count = *block_count;
377 for (i = 0; i < exceptiontablelength; i++) {
378 /* printf("Excepiont table index: %d\n",i); */
379 p = raw_extable[i].startpc;
380 if (label_index != NULL) p = label_index[p];
381 extable[i].startpc = p;
385 /*** if (DEBUG==true){printf("---------------------block_inserted:b_count=%i m->basicblockindex[(p=%i)]=%i=%p\n",b_count,p,m->basicblockindex[(p)],m->basicblockindex[(p)]);
386 fflush(stdout); } ***/
387 p = raw_extable[i].endpc;
388 if (p <= raw_extable[i].startpc)
389 panic("Invalid exception handler range");
390 if (p >=m->jcodelength) {
391 panic("Invalid exception handler end is after code end");
393 if (label_index != NULL) p = label_index[p];
394 extable[i].endpc = p;
396 if (p < m->jcodelength) {
399 p = raw_extable[i].handlerpc;
400 if (label_index != NULL) p = label_index[p];
401 extable[i].handlerpc = p;
405 extable[i].catchtype = raw_extable[i].catchtype;
406 extable[i].next = NULL;
407 extable[i].down = &extable[i + 1];
410 *block_count = b_count;
411 return &extable[i]; /* return the next free xtable* */
416 methodinfo *parse(methodinfo *m, t_inlining_globals *inline_env)
418 int p; /* java instruction counter */
419 int nextp; /* start of next java instruction */
420 int opcode; /* java opcode */
421 int i; /* temporary for different uses (ctrs)*/
422 int ipc = 0; /* intermediate instruction counter */
423 int b_count = 0; /* basic block counter */
424 int s_count = 0; /* stack element counter */
425 bool blockend = false; /* true if basic block end has been reached */
426 bool iswide = false; /* true if last instruction was a wide*/
427 instruction *iptr; /* current ptr into instruction array */
428 int gp; /* global java instruction counter */
429 /* inlining info for current method */
431 inlining_methodinfo *inlinfo = inline_env->inlining_rootinfo;
432 inlining_methodinfo *tmpinlinf;
433 int nextgp = -1; /* start of next method to be inlined */
434 int *label_index = NULL; /* label redirection table */
435 int firstlocal = 0; /* first local variable of method */
436 exceptiontable* nextex; /* points next free entry in extable */
437 u1 *instructionstart; /* 1 for pcs which are valid instr. starts */
442 codegendata *cd=m->codegendata;
444 if (DEBUG==true) {printf("PARSING: "); fflush(stdout);
448 if (m->methodUsed != USED) {
450 printf(" rta missed: "); fflush(stdout);
453 if ( (rtMissed = fopen("rtMissed", "a")) == NULL) {
454 printf("CACAO - rtMissed file: cant open file to write append \n");
457 utf_fprint(rtMissed,m->class->name);
458 fprintf(rtMissed," "); fflush(rtMissed);
459 utf_fprint(rtMissed,m->name);
460 fprintf(rtMissed," "); fflush(rtMissed);
461 utf_fprint(rtMissed,m->descriptor);
462 fprintf(rtMissed,"\n"); fflush(rtMissed);
470 label_index = inlinfo->label_index;
471 m->maxstack = inline_env->cummaxstack;
472 /*JOWENN m->exceptiontablelength = inline_env->cumextablelength;*/
473 tmpinlinf = (inlining_methodinfo*)
474 list_first(inlinfo->inlinedmethods);
475 if (tmpinlinf != NULL) nextgp = tmpinlinf->startgp;
478 /**** static analysis has to be called before inlining
479 which has to be called before reg_set
480 which has to be called before parse (or ???)
481 will check if method being parsed was analysed here
483 if ((opt_xta) && (verbose)) {
484 /**RT_jit_parse(m);**/
485 printf("XTA requested, not available\n");
487 if ((opt_vta) && (verbose))
488 printf("VTA requested, not yet implemented\n");
490 /* allocate instruction array and block index table */
492 /* 1 additional for end ipc * # cum inline methods*/
494 m->basicblockindex = DMNEW(s4, inline_env->cumjcodelength + inline_env->cummethods);
495 memset(m->basicblockindex, 0, sizeof(s4) * (inline_env->cumjcodelength + inline_env->cummethods));
497 instructionstart = DMNEW(u1, inline_env->cumjcodelength + inline_env->cummethods);
498 memset(instructionstart, 0, sizeof(u1) * (inline_env->cumjcodelength + inline_env->cummethods));
500 /* 1 additional for TRACEBUILTIN and 4 for MONITORENTER/EXIT */
501 /* additional MONITOREXITS are reached by branches which are 3 bytes */
503 iptr = m->instructions = DMNEW(instruction, inline_env->cumjcodelength + 5);
505 /* Zero the intermediate instructions array so we don't have any
506 * invalid pointers in it if we cannot finish analyse_stack(). */
508 memset(iptr, 0, sizeof(instruction) * (inline_env->cumjcodelength + 5));
510 /* compute branch targets of exception table */
511 //if (m->exceptiontable == NULL) {
512 // printf("m->exceptiontable=NULL\n");fflush(stdout);
515 // printf("m->exceptiontable != NULL\n");fflush(stdout);
517 //printf("m->exceptiontablelength=%i, inline_env->method->exceptiontablelength=%i,inline_env->cumextablelength=%i\n",
518 //m->exceptiontablelength, inline_env->method->exceptiontablelength,inline_env->cumextablelength);
520 //if (m->exceptiontablelength > 0)
521 // m->exceptiontable = DMNEW(exceptiontable, m->exceptiontablelength + 1);
523 nextex = fillextable(m,
524 cd->exceptiontable, m->exceptiontable, m->exceptiontablelength,
525 label_index, &b_count, inline_env);
526 s_count = 1 + m->exceptiontablelength; /* initialize stack element counter */
529 if (checksync && (m->flags & ACC_SYNCHRONIZED)) {
530 m->isleafmethod = false;
531 inline_env->isleafmethod = false;
535 /* scan all java instructions */
539 if (m->linenumbercount == 0) {
543 linepcchange = m->linenumbers[0].start_pc;
546 for (p = 0, gp = 0; p < inline_env->method->jcodelength; gp += (nextp - p), p = nextp) {
548 /* DEBUG */ if (DEBUG==true) printf("----- p:%d gp:%d\n",p,gp);
550 /* mark this position as a valid instruction start */
552 instructionstart[gp] = 1;
553 /*log_text("new start of instruction");*/
554 if (linepcchange==p) {
555 if (inline_env->method->linenumbercount > lineindex) {
556 currentline = inline_env->method->linenumbers[lineindex].line_number;
558 if (lineindex < inline_env->method->linenumbercount)
559 linepcchange = inline_env->method->linenumbers[lineindex].start_pc;
560 /*printf("Line number changed to: %ld\n",currentline);*/
566 if ((useinlining) && (gp == nextgp)) {
568 bool *readonly = NULL;
571 opcode = code_get_u1(p,inline_env->method);
572 nextp = p += jcommandsize[opcode];
573 if (nextp > inline_env->method->jcodelength)
574 panic("Unexpected end of bytecode");
575 tmpinlinf = list_first(inlinfo->inlinedmethods);
576 firstlocal = tmpinlinf->firstlocal;
577 label_index = tmpinlinf->label_index;
578 readonly = tmpinlinf->readonly;
580 for (i = 0, tptr = tmpinlinf->method->paramtypes + tmpinlinf->method->paramcount - 1; i < tmpinlinf->method->paramcount; i++, tptr--) {
583 if ((i == 0) && inlineparamopt) {
584 OP1(ICMD_CLEAR_ARGREN, firstlocal);
587 if (!inlineparamopt || !readonly[i]) {
591 op = ICMD_READONLY_ARG;
595 OP1(op, firstlocal + tmpinlinf->method->paramcount - 1 - i);
597 /*m->basicblockindex[gp] |= (ipc << 1);*/ /*FIXME: necessary ? */
600 printf("BEFORE SAVE: "); fflush(stdout);
601 DEBUGMETH(inline_env->method);
603 inlining_save_compiler_variables();
605 printf("AFTER SAVE: "); fflush(stdout);
606 DEBUGMETH(inline_env->method);
608 inlining_set_compiler_variables(tmpinlinf);
610 printf("AFTER SET :: "); fflush(stdout);
611 DEBUGMETH(inline_env->method);
614 printf("\n.......Parsing (inlined): ");
616 DEBUGMETH(inline_env->method);
620 if (inlinfo->inlinedmethods == NULL) {
623 tmpinlinf = list_first(inlinfo->inlinedmethods);
624 nextgp = (tmpinlinf != NULL) ? tmpinlinf->startgp : -1;
626 if (inline_env->method->exceptiontablelength > 0)
627 nextex = fillextable(m, nextex,
628 inline_env->method->exceptiontable, inline_env->method->exceptiontablelength,
629 label_index, &b_count, inline_env);
633 opcode = code_get_u1(p,inline_env->method); /* fetch op code */
636 printf("Parse p=%i<%i< opcode=<%i> %s\n",
637 p, inline_env->jcodelength, opcode, opcode_names[opcode]);
640 //printf("basicblockindex[gp=%i]=%i=%p ipc=%i=%p shifted ipc=%i=%p\n",
641 //gp,m->basicblockindex[gp],m->basicblockindex[gp],ipc,ipc,(ipc<<1),(ipc<<1));
643 m->basicblockindex[gp] |= (ipc << 1); /*store intermed cnt*/
644 //printf("basicblockindex[gp=%i]=%i=%p \n",
645 //gp,m->basicblockindex[gp],m->basicblockindex[gp]);
649 //printf("B4 BEND\t"); fflush(stdout);
650 block_insert(gp); /* start new block */
654 nextp = p + jcommandsize[opcode]; /* compute next instruction start */
655 if (nextp > inline_env->method->jcodelength)
656 panic("Unexpected end of bytecode");
657 s_count += stackreq[opcode]; /* compute stack element count */
663 /* pushing constants onto the stack p */
666 LOADCONST_I(code_get_s1(p+1,inline_env->method));
670 LOADCONST_I(code_get_s2(p+1,inline_env->method));
674 i = code_get_u1(p+1,inline_env->method);
676 goto pushconstantitem;
679 i = code_get_u2(p + 1,inline_env->method);
683 if (i >= inline_env->method->class->cpcount)
684 panic ("Attempt to access constant outside range");
686 switch (inline_env->method->class->cptags[i]) {
687 case CONSTANT_Integer:
688 LOADCONST_I(((constant_integer *) (inline_env->method->class->cpinfos[i]))->value);
691 LOADCONST_L(((constant_long *) (inline_env->method->class->cpinfos[i]))->value);
694 LOADCONST_F(((constant_float *) (inline_env->method->class->cpinfos[i]))->value);
696 case CONSTANT_Double:
697 LOADCONST_D(((constant_double *) (inline_env->method->class->cpinfos[i]))->value);
699 case CONSTANT_String:
700 LOADCONST_A(literalstring_new((utf *) (inline_env->method->class->cpinfos[i])));
702 default: panic("Invalid constant type to push");
706 case JAVA_ACONST_NULL:
717 LOADCONST_I(opcode - JAVA_ICONST_0);
722 LOADCONST_L(opcode - JAVA_LCONST_0);
728 LOADCONST_F(opcode - JAVA_FCONST_0);
733 LOADCONST_D(opcode - JAVA_DCONST_0);
736 /* loading variables onto the stack */
744 i = code_get_u1(p + 1,inline_env->method);
746 i = code_get_u2(p + 1,inline_env->method);
750 OP1LOAD(opcode, i + firstlocal);
757 OP1LOAD(ICMD_ILOAD, opcode - JAVA_ILOAD_0 + firstlocal);
764 OP1LOAD(ICMD_LLOAD, opcode - JAVA_LLOAD_0 + firstlocal);
771 OP1LOAD(ICMD_FLOAD, opcode - JAVA_FLOAD_0 + firstlocal);
778 OP1LOAD(ICMD_DLOAD, opcode - JAVA_DLOAD_0 + firstlocal);
785 OP1LOAD(ICMD_ALOAD, opcode - JAVA_ALOAD_0 + firstlocal);
788 /* storing stack values into local variables */
796 i = code_get_u1(p + 1,inline_env->method);
798 i = code_get_u2(p + 1,inline_env->method);
802 OP1STORE(opcode, i + firstlocal);
809 OP1STORE(ICMD_ISTORE, opcode - JAVA_ISTORE_0 + firstlocal);
816 OP1STORE(ICMD_LSTORE, opcode - JAVA_LSTORE_0 + firstlocal);
823 OP1STORE(ICMD_FSTORE, opcode - JAVA_FSTORE_0 + firstlocal);
830 OP1STORE(ICMD_DSTORE, opcode - JAVA_DSTORE_0 + firstlocal);
837 OP1STORE(ICMD_ASTORE, opcode - JAVA_ASTORE_0 + firstlocal);
845 i = code_get_u1(p + 1,inline_env->method);
846 v = code_get_s1(p + 2,inline_env->method);
849 i = code_get_u2(p + 1,inline_env->method);
850 v = code_get_s2(p + 3,inline_env->method);
854 INDEX_ONEWORD(i + firstlocal);
855 OP2I(opcode, i + firstlocal, v);
859 /* wider index for loading, storing and incrementing */
866 /* managing arrays ************************************************/
870 switch (code_get_s1(p + 1,inline_env->method)) {
872 BUILTIN1(BUILTIN_newarray_boolean, TYPE_ADR,currentline);
875 BUILTIN1(BUILTIN_newarray_char, TYPE_ADR,currentline);
878 BUILTIN1(BUILTIN_newarray_float, TYPE_ADR,currentline);
881 BUILTIN1(BUILTIN_newarray_double, TYPE_ADR,currentline);
884 BUILTIN1(BUILTIN_newarray_byte, TYPE_ADR,currentline);
887 BUILTIN1(BUILTIN_newarray_short, TYPE_ADR,currentline);
890 BUILTIN1(BUILTIN_newarray_int, TYPE_ADR,currentline);
893 BUILTIN1(BUILTIN_newarray_long, TYPE_ADR,currentline);
895 default: panic("Invalid array-type to create");
897 OP(ICMD_CHECKEXCEPTION);
902 i = code_get_u2(p + 1,inline_env->method);
904 classinfo *component =
905 (classinfo *) class_getconstant(inline_env->method->class, i, CONSTANT_Class);
907 if (!class_load(component))
910 if (!class_link(component))
913 LOADCONST_A_BUILTIN(class_array_of(component)->vftbl);
914 /* LOADCONST_A_BUILTIN(component); */
916 BUILTIN2(BUILTIN_newarray, TYPE_ADR, currentline);
918 OP(ICMD_CHECKEXCEPTION);
921 case JAVA_MULTIANEWARRAY:
922 inline_env->method->isleafmethod = false;
923 i = code_get_u2(p + 1,inline_env->method);
926 s4 v = code_get_u1(p + 3,inline_env->method);
929 /* vftbl *arrayvftbl = */
930 /* ((classinfo *) class_getconstant(class, i, CONSTANT_Class))->vftbl; */
931 /* OP2A(opcode, v, arrayvftbl,currentline); */
934 classinfo *component =
935 (classinfo *) class_getconstant(inline_env->method->class, i, CONSTANT_Class);
937 if (!class_load(component))
940 if (!class_link(component))
943 arrayvftbl = component->vftbl;
944 OP2A(opcode, v, arrayvftbl, currentline);
946 /* classinfo *arrayclass = */
947 /* (classinfo *) class_getconstant(class, i, CONSTANT_Class); */
948 /* OP2A(opcode, v, arrayclass, currentline); */
970 i = p + code_get_s2(p + 1,inline_env->method);
982 i = p + code_get_s4(p + 1,inline_env->method);
988 //printf("B6 JSR_W\t"); fflush(stdout);
996 i = code_get_u1(p + 1,inline_env->method);
998 i = code_get_u2(p + 1,inline_env->method);
1005 if (inline_env->isinlinedmethod) {
1006 OP1(ICMD_GOTO, inlinfo->stopgp);
1010 OP1LOAD(opcode, i + firstlocal);
1019 if (inline_env->isinlinedmethod) {
1020 /* if (p==m->jcodelength-1) {*/ /* return is at end of inlined method */
1025 OP1(ICMD_GOTO, inlinfo->stopgp);
1039 /* table jumps ********************************/
1041 case JAVA_LOOKUPSWITCH:
1048 nextp = ALIGN((p + 1), 4);
1049 if (nextp + 8 > inline_env->method->jcodelength)
1050 panic("Unexpected end of bytecode");
1052 tablep = (s4 *) (inline_env->method->jcode + nextp);
1055 num = code_get_u4(nextp + 4,inline_env->method);
1056 tablep = DMNEW(s4, num * 2 + 2);
1059 OP2A(opcode, 0, tablep,currentline);
1061 /* default target */
1063 j = p + code_get_s4(nextp,inline_env->method);
1066 *tablep = j; /* restore for little endian */
1070 //printf("B7 LOOKUP1\t"); fflush(stdout);
1073 /* number of pairs */
1075 num = code_get_u4(nextp,inline_env->method);
1080 if (nextp + 8*(num) > inline_env->method->jcodelength)
1081 panic("Unexpected end of bytecode");
1083 for (i = 0; i < num; i++) {
1086 j = code_get_s4(nextp,inline_env->method);
1087 *tablep = j; /* restore for little endian */
1091 /* check if the lookup table is sorted correctly */
1093 if (i && (j <= prevvalue))
1094 panic("invalid LOOKUPSWITCH: table not sorted");
1099 j = p + code_get_s4(nextp,inline_env->method);
1102 *tablep = j; /* restore for little endian */
1106 //printf("B8 LOOKUP2\t"); fflush(stdout);
1114 case JAVA_TABLESWITCH:
1120 nextp = ALIGN((p + 1), 4);
1121 if (nextp + 12 > inline_env->method->jcodelength)
1122 panic("Unexpected end of bytecode");
1124 tablep = (s4 *) (inline_env->method->jcode + nextp);
1127 num = code_get_u4(nextp + 8,inline_env->method) - code_get_u4(nextp + 4,inline_env->method);
1128 tablep = DMNEW(s4, num + 1 + 3);
1131 OP2A(opcode, 0, tablep,currentline);
1133 /* default target */
1135 j = p + code_get_s4(nextp,inline_env->method);
1138 *tablep = j; /* restore for little endian */
1142 //printf("B9 TABLESWITCH1\t"); fflush(stdout);
1147 j = code_get_s4(nextp,inline_env->method);
1148 *tablep = j; /* restore for little endian */
1154 num = code_get_s4(nextp,inline_env->method);
1155 *tablep = num; /* restore for little endian */
1159 num -= j; /* difference of upper - lower */
1161 panic("invalid TABLESWITCH: upper bound < lower bound");
1163 if (nextp + 4*(num+1) > inline_env->method->jcodelength)
1164 panic("Unexpected end of bytecode");
1166 for (i = 0; i <= num; i++) {
1167 j = p + code_get_s4(nextp,inline_env->method);
1170 *tablep = j; /* restore for little endian */
1174 //printf("B10 TABLESWITCH2\t"); fflush(stdout);
1182 /* load and store of object fields *******************/
1185 BUILTIN3(BUILTIN_aastore, TYPE_VOID, currentline);
1188 case JAVA_PUTSTATIC:
1189 case JAVA_GETSTATIC:
1190 i = code_get_u2(p + 1,inline_env->method);
1192 constant_FMIref *fr;
1195 fr = class_getconstant(inline_env->method->class, i, CONSTANT_Fieldref);
1197 if (!class_load(fr->class))
1200 if (!class_link(fr->class))
1203 fi = class_resolvefield(fr->class,
1206 inline_env->method->class,
1212 OP2A(opcode, fi->type, fi, currentline);
1213 if (!fi->class->initialized) {
1214 inline_env->method->isleafmethod = false;
1221 i = code_get_u2(p + 1,inline_env->method);
1223 constant_FMIref *fr;
1226 fr = class_getconstant(inline_env->method->class, i, CONSTANT_Fieldref);
1228 if (!class_load(fr->class))
1231 if (!class_link(fr->class))
1234 fi = class_resolvefield(fr->class,
1237 inline_env->method->class,
1243 OP2A(opcode, fi->type, fi, currentline);
1248 /* method invocation *****/
1250 case JAVA_INVOKESTATIC:
1251 i = code_get_u2(p + 1,inline_env->method);
1253 constant_FMIref *mr;
1256 inline_env->method->isleafmethod = false;
1258 mr = class_getconstant(inline_env->method->class, i, CONSTANT_Methodref);
1260 if (!class_load(mr->class))
1263 if (!class_link(mr->class))
1266 mi = class_resolveclassmethod(mr->class,
1269 inline_env->method->class,
1275 /*RTAprint*/// if (((pOpcodes == 2) || (pOpcodes == 3)) && opt_rt)
1277 /*RTAprint*/ {printf(" method name =");
1278 /*RTAprint*/ utf_display(mi->class->name); printf(".");
1279 /*RTAprint*/ utf_display(mi->name);printf("\tINVOKE STATIC\n");
1280 /*RTAprint*/ fflush(stdout);}
1282 if (!(mi->flags & ACC_STATIC)) {
1284 new_exception(string_java_lang_IncompatibleClassChangeError);
1288 descriptor2types(mi);
1289 OP2A(opcode, mi->paramcount, mi, currentline);
1293 case JAVA_INVOKESPECIAL:
1294 case JAVA_INVOKEVIRTUAL:
1295 i = code_get_u2(p + 1,inline_env->method);
1297 constant_FMIref *mr;
1300 inline_env->method->isleafmethod = false;
1302 mr = class_getconstant(inline_env->method->class, i, CONSTANT_Methodref);
1304 if (!class_load(mr->class))
1307 if (!class_link(mr->class))
1310 mi = class_resolveclassmethod(mr->class,
1313 inline_env->method->class,
1319 /*RTAprint*/ // if (((pOpcodes == 2) || (pOpcodes == 3)) && opt_rt)
1321 /*RTAprint*/ {printf(" method name =");
1323 /*RTAprint*/ utf_display(mi->class->name); printf(".");
1324 /*RTAprint*/ utf_display(mi->name);printf("\tINVOKE SPECIAL/VIRTUAL\n");
1325 /*RTAprint*/ fflush(stdout);}
1327 if (mi->flags & ACC_STATIC) {
1329 new_exception(string_java_lang_IncompatibleClassChangeError);
1333 descriptor2types(mi);
1334 OP2A(opcode, mi->paramcount, mi, currentline);
1338 case JAVA_INVOKEINTERFACE:
1339 i = code_get_u2(p + 1,inline_env->method);
1341 constant_FMIref *mr;
1344 inline_env->method->isleafmethod = false;
1346 mr = class_getconstant(inline_env->method->class, i, CONSTANT_InterfaceMethodref);
1348 if (!class_load(mr->class))
1351 if (!class_link(mr->class))
1354 mi = class_resolveinterfacemethod(mr->class,
1357 inline_env->method->class,
1362 if (mi->flags & ACC_STATIC) {
1364 new_exception(string_java_lang_IncompatibleClassChangeError);
1368 descriptor2types(mi);
1369 OP2A(opcode, mi->paramcount, mi, currentline);
1373 /* miscellaneous object operations *******/
1376 i = code_get_u2(p + 1,inline_env->method);
1377 LOADCONST_A_BUILTIN(class_getconstant(inline_env->method->class, i, CONSTANT_Class));
1379 BUILTIN1(BUILTIN_new, TYPE_ADR, currentline);
1380 OP(ICMD_CHECKEXCEPTION);
1383 case JAVA_CHECKCAST:
1384 i = code_get_u2(p + 1,inline_env->method);
1387 (classinfo *) class_getconstant(inline_env->method->class, i, CONSTANT_Class);
1390 if (!class_load(cls))
1394 if (!class_link(cls))
1397 if (cls->vftbl->arraydesc) {
1398 /* array type cast-check */
1399 LOADCONST_A_BUILTIN(cls->vftbl);
1401 BUILTIN2(BUILTIN_checkarraycast, TYPE_ADR,currentline);
1403 } else { /* object type cast-check */
1405 + LOADCONST_A_BUILTIN(class_getconstant(class, i, CONSTANT_Class));
1407 + BUILTIN2(BUILTIN_checkcast, TYPE_ADR,currentline);
1409 OP2A(opcode, 1, cls, currentline);
1414 case JAVA_INSTANCEOF:
1415 i = code_get_u2(p + 1,inline_env->method);
1418 (classinfo *) class_getconstant(inline_env->method->class, i, CONSTANT_Class);
1421 if (!class_load(cls))
1425 if (!class_link(cls))
1428 if (cls->vftbl->arraydesc) {
1429 /* array type cast-check */
1430 LOADCONST_A_BUILTIN(cls->vftbl);
1432 BUILTIN2(BUILTIN_arrayinstanceof, TYPE_INT, currentline);
1434 else { /* object type cast-check */
1436 LOADCONST_A_BUILTIN(class_getconstant(class, i, CONSTANT_Class));
1438 BUILTIN2(BUILTIN_instanceof, TYPE_INT,currentline);
1440 OP2A(opcode, 1, cls, currentline);
1445 case JAVA_MONITORENTER:
1448 BUILTIN1(BUILTIN_monitorenter, TYPE_VOID,currentline);
1452 OP(ICMD_NULLCHECKPOP);
1456 case JAVA_MONITOREXIT:
1459 BUILTIN1(BUILTIN_monitorexit, TYPE_VOID,currentline);
1460 OP(ICMD_CHECKEXCEPTION);
1468 /* any other basic operation **************************************/
1487 #if defined(__I386__)
1490 BUILTIN2(BUILTIN_frem, TYPE_FLOAT,currentline);
1495 #if defined(__I386__)
1498 BUILTIN2(BUILTIN_drem, TYPE_DOUBLE,currentline);
1503 #if defined(__ALPHA__)
1505 BUILTIN1(BUILTIN_f2i, TYPE_INT,currentline);
1514 #if defined(__ALPHA__)
1516 BUILTIN1(BUILTIN_f2l, TYPE_LONG,currentline);
1525 #if defined(__ALPHA__)
1527 BUILTIN1(BUILTIN_d2i, TYPE_INT,currentline);
1536 #if defined(__ALPHA__)
1538 BUILTIN1(BUILTIN_d2l, TYPE_LONG,currentline);
1546 case JAVA_BREAKPOINT:
1548 new_verifyerror(m, "Quick instructions shouldn't appear yet.");
1551 case 204: /* unused opcode */
1603 printf("Illegal opcode %d at instr %d\n", opcode, ipc);
1604 panic("Illegal opcode encountered");
1613 /* If WIDE was used correctly, iswide should have been reset by now. */
1614 if (iswide && opcode != JAVA_WIDE)
1615 panic("Illegal instruction: WIDE before incompatible opcode");
1619 if (inline_env->isinlinedmethod && p == inline_env->method->jcodelength - 1) { /* end of an inlined method */
1620 /* printf("setting gp from %d to %d\n",gp, inlinfo->stopgp); */
1621 gp = inlinfo->stopgp;
1622 inlining_restore_compiler_variables();
1623 //label_index = inlinfo->label_index;
1625 printf("AFTER RESTORE : "); fflush(stdout);
1626 DEBUGMETH(inline_env->method);
1628 list_remove(inlinfo->inlinedmethods, list_first(inlinfo->inlinedmethods));
1629 if (inlinfo->inlinedmethods == NULL) {
1632 tmpinlinf = list_first(inlinfo->inlinedmethods);
1633 nextgp = (tmpinlinf != NULL) ? tmpinlinf->startgp : -1;
1635 /* printf("nextpgp: %d\n", nextgp); */
1636 label_index=inlinfo->label_index;
1637 firstlocal = inlinfo->firstlocal;
1642 if (p != m->jcodelength)
1643 panic("Command-sequence crosses code-boundary");
1646 *exceptionptr = new_verifyerror(m, "Falling off the end of the code");
1650 /* adjust block count if target 0 is not first intermediate instruction */
1652 if (!m->basicblockindex[0] || (m->basicblockindex[0] > 1))
1655 /* copy local to method variables */
1657 m->instructioncount = ipc;
1658 m->basicblockcount = b_count;
1659 m->stackcount = s_count + m->basicblockcount * m->maxstack;
1661 /* allocate stack table */
1663 m->stack = DMNEW(stackelement, m->stackcount);
1668 bptr = m->basicblocks = DMNEW(basicblock, b_count + 1); /* one more for end ipc */
1673 /* additional block if target 0 is not first intermediate instruction */
1675 if (!m->basicblockindex[0] || (m->basicblockindex[0] > 1)) {
1676 bptr->iinstr = m->instructions;
1679 bptr->type = BBTYPE_STD;
1680 bptr->branchrefs = NULL;
1681 bptr->pre_count = 0;
1682 bptr->debug_nr = c_debug_nr++;
1685 (bptr - 1)->next = bptr;
1688 /* allocate blocks */
1690 for (p = 0; p < inline_env->cumjcodelength; p++) {
1691 // for (p = 0; p < m->jcodelength; p++) {
1692 if (m->basicblockindex[p] & 1) {
1693 /* check if this block starts at the beginning of an instruction */
1694 if (!instructionstart[p])
1695 panic("Branch into middle of instruction");
1696 /* allocate the block */
1697 bptr->iinstr = m->instructions + (m->basicblockindex[p] >> 1);
1698 bptr->debug_nr = c_debug_nr++;
1700 (bptr - 1)->icount = bptr->iinstr - (bptr - 1)->iinstr;
1704 bptr->type = BBTYPE_STD;
1705 bptr->branchrefs = NULL;
1706 m->basicblockindex[p] = b_count;
1707 bptr->pre_count = 0;
1710 (bptr - 1)->next = bptr;
1714 /* allocate additional block at end */
1716 bptr->instack = bptr->outstack = NULL;
1717 bptr->indepth = bptr->outdepth = 0;
1718 bptr->iinstr = NULL;
1719 (bptr - 1)->icount = (m->instructions + m->instructioncount) - (bptr - 1)->iinstr;
1724 bptr->type = BBTYPE_STD;
1725 bptr->branchrefs = NULL;
1726 bptr->pre_count = 0;
1727 bptr->debug_nr = c_debug_nr++;
1728 (bptr - 1)->next = bptr;
1731 if (cd->exceptiontablelength > 0) {
1732 cd->exceptiontable[cd->exceptiontablelength - 1].down = NULL;
1735 for (i = 0; i < cd->exceptiontablelength; ++i) {
1736 p = cd->exceptiontable[i].startpc;
1737 cd->exceptiontable[i].start = m->basicblocks + m->basicblockindex[p];
1739 p = cd->exceptiontable[i].endpc;
1740 cd->exceptiontable[i].end = (p == m->jcodelength) ? (m->basicblocks + m->basicblockcount + 1) : (m->basicblocks + m->basicblockindex[p]);
1742 p = cd->exceptiontable[i].handlerpc;
1743 cd->exceptiontable[i].handler = m->basicblocks + m->basicblockindex[p];
1747 if (useinlining) inlining_cleanup(inline_env);
1749 /* just return methodinfo* to signal everything was ok */
1756 * These are local overrides for various environment variables in Emacs.
1757 * Please do not remove this and leave it at the end of the file, where
1758 * Emacs will automagically detect them.
1759 * ---------------------------------------------------------------------
1762 * indent-tabs-mode: t