1 /* vm/jit/parse.c - parser for JavaVM to intermediate code translation
3 Copyright (C) 1996-2005 R. Grafl, A. Krall, C. Kruegel, C. Oates,
4 R. Obermaisser, M. Platter, M. Probst, S. Ring, E. Steiner,
5 C. Thalinger, D. Thuernbeck, P. Tomsich, C. Ullrich, J. Wenninger,
6 Institut f. Computersprachen - TU Wien
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
33 $Id: parse.c 1735 2004-12-07 14:33:27Z twisti $
42 #include "mm/memory.h"
43 #include "native/native.h"
44 #include "toolbox/logging.h"
45 #include "vm/builtin.h"
46 #include "vm/exceptions.h"
47 #include "vm/global.h"
48 #include "vm/loader.h"
49 #include "vm/options.h"
50 #include "vm/statistics.h"
51 #include "vm/tables.h"
52 #include "vm/jit/jit.h"
53 #include "vm/jit/parse.h"
54 #include "vm/jit/inline/parseRT.h"
55 #include "vm/jit/inline/inline.h"
56 #include "vm/jit/loop/loop.h"
59 #define METHINFO(mm) \
61 printf("PARSE method name ="); \
62 utf_display(mm->class->name); \
67 #define DEBUGMETH(mm) \
70 printf("PARSE method name ="); \
71 utf_display(mm->class->name); \
78 if (DEBUG4 == true) {printf("Parse p=%i<%i< opcode=<%i> %s\n", \
79 p, m->jcodelength,opcode,opcode_names[opcode]);}
83 bool DEBUG4 = false; /*opcodes*/
86 #define debug_writebranch if (DEBUG2==true) printf("op:: %s i: %d label_index[i]: %d label_index=0x%x\n",opcode_names[opcode], i, label_index[i], label_index);
87 #define debug_writebranch1
90 /* function descriptor2typesL ***************************************************
92 decodes a already checked method descriptor. The parameter count, the
93 return type and the argument types are stored in the passed methodinfo.
94 gets and saves classptr for object ref.s
96 *******************************************************************************/
98 classSetNode *descriptor2typesL(methodinfo *m)
105 classinfo** classtypes;
108 classSetNode *p=NULL;
109 if (debugInfo >= 1) {
110 printf("In descriptor2typesL >>>\t"); fflush(stdout);
111 utf_display(m->class->name); printf(".");
112 method_display(m);fflush(stdout);
116 desc = MNEW (char, 256);
117 types = DMNEW (u1, m->descriptor->blength);
118 classtypes = MNEW (classinfo*, m->descriptor->blength+1);
119 m->returnclass = NULL;
121 if (!(m->flags & ACC_STATIC)) {
123 if (debugInfo >= 1) {
124 printf("param #0 (this?) method class =");utf_display(m->class->name);printf("\n");
126 classtypes[pcount] = m->class;
127 p = addClassCone(p, m->class);
131 utf_ptr = m->descriptor->text + 1;
132 strcpy (desc,utf_ptr);
134 while ((c = *desc++) != ')') {
141 case 'Z': *tptr++ = TYPE_INT;
143 case 'J': *tptr++ = TYPE_LNG;
145 case 'F': *tptr++ = TYPE_FLT;
147 case 'D': *tptr++ = TYPE_DBL;
149 case 'L': *tptr++ = TYPE_ADR;
150 /* get class string */
151 class = strtok(desc,";");
152 desc = strtok(NULL,"\0");
153 /* get/save classinfo ptr */
154 classtypes[pcount-1] = class_get(utf_new_char(class));
155 p = addClassCone(p, class_get(utf_new_char(class)));
156 if (debugInfo >= 1) {
157 printf("LParam#%i 's class type is: %s\n",pcount-1,class);fflush(stdout);
158 printf("Lclasstypes[%i]=",pcount-1);fflush(stdout);
159 utf_display(classtypes[pcount-1]->name);
162 case '[': *tptr++ = TYPE_ADR;
165 /* get class string */
167 class = strtok(desc,";");
168 desc = strtok(NULL,"\0");
169 /* get/save classinfo ptr */
170 classtypes[pcount-1] = class_get(utf_new_char(class));
171 p= addClassCone(p, class_get(utf_new_char(class)));
172 if (debugInfo >= 1) {
173 printf("[Param#%i 's class type is: %s\n",pcount-1,class);
174 printf("[classtypes[%i]=",pcount-1);fflush(stdout);
175 utf_display(classtypes[pcount-1]->name);
180 classtypes[pcount-1] = NULL;
183 panic("Ill formed methodtype-descriptor");
187 /* compute return type */
193 case 'Z': m->returntype = TYPE_INT;
195 case 'J': m->returntype = TYPE_LNG;
197 case 'F': m->returntype = TYPE_FLT;
199 case 'D': m->returntype = TYPE_DBL;
202 m->returntype = TYPE_ADR;
210 m->returntype = TYPE_ADR;
212 /* get class string */
213 class = strtok(desc,";");
214 m->returnclass = class_get(utf_new_char(class));
215 if (m->returnclass == NULL) {
216 printf("class=%s :\t",class);
217 panic ("return class not found");
220 case 'V': m->returntype = TYPE_VOID;
223 default: panic("Ill formed methodtype-descriptor-ReturnType");
226 m->paramcount = pcount;
227 m->paramtypes = types;
228 m->paramclass = classtypes;
232 for (i=0; i< m->paramcount; i++) {
233 if ((m->paramtypes[i] == TYPE_ADR) && (m->paramclass[i] != NULL)) {
234 printf("Param #%i is:\t",i);
235 utf_display(m->paramclass[i]->name);
240 if ((m->returntype == TYPE_ADR) && (m->returnclass != NULL)) {
241 printf("\tReturn Type is:\t"); fflush(stdout);
242 utf_display(m->returnclass->name);
246 printf("params2types: START results in a set \n");
247 printf("param2types: A Set size=%i=\n",sizeOfSet(p));
256 /* function descriptor2types ***************************************************
258 decodes a already checked method descriptor. The parameter count, the
259 return type and the argument types are stored in the passed methodinfo.
261 *******************************************************************************/
263 void descriptor2types(methodinfo *m)
269 types = DMNEW(u1, m->descriptor->blength);
272 if (!(m->flags & ACC_STATIC)) {
277 utf_ptr = m->descriptor->text + 1;
279 while ((c = *utf_ptr++) != ')') {
300 while (*utf_ptr++ != ';');
307 while (*utf_ptr++ != ';') /* skip */;
310 panic("Ill formed methodtype-descriptor");
314 /* compute return type */
316 switch (*utf_ptr++) {
322 m->returntype = TYPE_INT;
325 m->returntype = TYPE_LNG;
328 m->returntype = TYPE_FLT;
331 m->returntype = TYPE_DBL;
335 m->returntype = TYPE_ADR;
338 m->returntype = TYPE_VOID;
341 panic("Ill formed methodtype-descriptor");
344 m->paramcount = pcount;
345 m->paramtypes = types;
350 /*******************************************************************************
352 function 'parse' scans the JavaVM code and generates intermediate code
354 During parsing the block index table is used to store at bit pos 0
355 a flag which marks basic block starts and at position 1 to 31 the
356 intermediate instruction index. After parsing the block index table
357 is scanned, for marked positions a block is generated and the block
358 number is stored in the block index table.
360 *******************************************************************************/
362 static exceptiontable* fillextable(methodinfo *m,
363 exceptiontable* extable, exceptiontable *raw_extable,
364 int exceptiontablelength,
365 int *label_index, int *block_count,
366 t_inlining_globals *inline_env)
368 int b_count, i, p, src, insertBlock;
370 if (exceptiontablelength == 0)
374 /*if (m->exceptiontablelength > 0) {
376 printf("m->exceptiontablelength=%i\n",m->exceptiontablelength);
377 panic("exceptiontablelength > 0");
380 b_count = *block_count;
382 for (src = exceptiontablelength-1; src >=0; src--) {
383 /* printf("Excepiont table index: %d\n",i); */
384 p = raw_extable[src].startpc;
385 if (label_index != NULL) p = label_index[p];
386 extable->startpc = p;
390 /*** 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)]);
391 fflush(stdout); } ***/
392 p = raw_extable[src].endpc; /* see JVM Spec 4.7.3 */
393 if (p <= raw_extable[src].startpc)
394 panic("Invalid exception handler range");
396 if (p >inline_env->method->jcodelength) {
397 panic("Invalid exception handler end is after code end");
399 if (p<inline_env->method->jcodelength) insertBlock=1; else insertBlock=0;
400 /*if (label_index !=NULL) printf("%s:translating endpc:%ld to %ld, label_index:%p\n",m->name->text,p,label_index[p],label_index); else
401 printf("%s:fillextab: endpc:%ld\n",m->name->text,p);*/
402 if (label_index != NULL) p = label_index[p];
405 /*if (p < inline_env->method->jcodelength) {
407 if (insertBlock) block_insert(p);
409 p = raw_extable[src].handlerpc;
410 if (label_index != NULL) p = label_index[p];
411 extable->handlerpc = p;
415 extable->catchtype = raw_extable[src].catchtype;
416 extable->next = NULL;
417 extable->down = &extable[1];
421 *block_count = b_count;
422 return extable; /*&extable[i];*/ /* return the next free xtable* */
427 methodinfo *parse(methodinfo *m, codegendata *cd, t_inlining_globals *inline_env)
429 int p; /* java instruction counter */
430 int nextp; /* start of next java instruction */
431 int opcode; /* java opcode */
432 int i; /* temporary for different uses (ctrs)*/
433 int ipc = 0; /* intermediate instruction counter */
434 int b_count = 0; /* basic block counter */
435 int s_count = 0; /* stack element counter */
436 bool blockend = false; /* true if basic block end has been reached */
437 bool iswide = false; /* true if last instruction was a wide*/
438 instruction *iptr; /* current ptr into instruction array */
439 int gp; /* global java instruction counter */
440 /* inlining info for current method */
442 inlining_methodinfo *inlinfo = inline_env->inlining_rootinfo;
443 inlining_methodinfo *tmpinlinf;
444 int nextgp = -1; /* start of next method to be inlined */
445 int *label_index = NULL; /* label redirection table */
446 int firstlocal = 0; /* first local variable of method */
447 exceptiontable* nextex; /* points next free entry in extable */
448 u1 *instructionstart; /* 1 for pcs which are valid instr. starts */
454 u2 skipBasicBlockChange;
456 if (DEBUG4==true) {printf("\nPARSING: "); fflush(stdout);
460 if (m->methodUsed != USED) {
462 printf(" rta missed: "); fflush(stdout);
465 if ( (rtMissed = fopen("rtMissed", "a")) == NULL) {
466 printf("CACAO - rtMissed file: cant open file to write append \n");
469 utf_fprint(rtMissed,m->class->name);
470 fprintf(rtMissed," "); fflush(rtMissed);
471 utf_fprint(rtMissed,m->name);
472 fprintf(rtMissed," "); fflush(rtMissed);
473 utf_fprint(rtMissed,m->descriptor);
474 fprintf(rtMissed,"\n"); fflush(rtMissed);
482 label_index = inlinfo->label_index;
483 m->maxstack = inline_env->cummaxstack;
484 /*JOWENN m->exceptiontablelength = inline_env->cumextablelength;*/
485 tmpinlinf = (inlining_methodinfo*)
486 list_first(inlinfo->inlinedmethods);
487 if (tmpinlinf != NULL) nextgp = tmpinlinf->startgp;
490 /**** static analysis has to be called before inlining
491 which has to be called before reg_set
492 which has to be called before parse (or ???)
493 will check if method being parsed was analysed here
495 if (opt_xta && opt_verbose) {
496 /**RT_jit_parse(m);**/
497 printf("XTA requested, not available\n");
499 if (opt_vta && opt_verbose)
500 printf("VTA requested, not yet implemented\n");
502 /* allocate instruction array and block index table */
504 /* 1 additional for end ipc * # cum inline methods*/
506 m->basicblockindex = DMNEW(s4, inline_env->cumjcodelength + inline_env->cummethods);
507 memset(m->basicblockindex, 0, sizeof(s4) * (inline_env->cumjcodelength + inline_env->cummethods));
509 instructionstart = DMNEW(u1, inline_env->cumjcodelength + inline_env->cummethods);
510 memset(instructionstart, 0, sizeof(u1) * (inline_env->cumjcodelength + inline_env->cummethods));
512 /* 1 additional for TRACEBUILTIN and 4 for MONITORENTER/EXIT */
513 /* additional MONITOREXITS are reached by branches which are 3 bytes */
515 iptr = m->instructions = DMNEW(instruction, inline_env->cumjcodelength + 5);
517 /* Zero the intermediate instructions array so we don't have any
518 * invalid pointers in it if we cannot finish analyse_stack(). */
520 memset(iptr, 0, sizeof(instruction) * (inline_env->cumjcodelength + 5));
522 /* compute branch targets of exception table */
524 if (m->exceptiontable == NULL) {
525 printf("m->exceptiontable=NULL\n");fflush(stdout);
528 printf("m->exceptiontable != NULL\n");fflush(stdout);
530 printf("m->exceptiontablelength=%i, inline_env->method->exceptiontablelength=%i,inline_env->cumextablelength=%i\n",
531 m->exceptiontablelength, inline_env->method->exceptiontablelength,inline_env->cumextablelength);
534 if (m->exceptiontablelength > 0)
535 m->exceptiontable = DMNEW(exceptiontable, m->exceptiontablelength + 1);
538 nextex = fillextable(m,
539 &(cd->exceptiontable[cd->exceptiontablelength-1]), m->exceptiontable, m->exceptiontablelength,
540 label_index, &b_count, inline_env);
541 s_count = 1 + m->exceptiontablelength; /* initialize stack element counter */
543 #if defined(USE_THREADS)
544 if (checksync && (m->flags & ACC_SYNCHRONIZED)) {
545 m->isleafmethod = false;
546 inline_env->method->isleafmethod = false;
550 /* scan all java instructions */
554 if (m->linenumbercount == 0) {
558 linepcchange = m->linenumbers[0].start_pc;
561 skipBasicBlockChange=0;
562 for (p = 0, gp = 0; p < inline_env->method->jcodelength; gp += (nextp - p), p = nextp) {
564 /* DEBUG */ if (DEBUG==true) printf("----- p:%d gp:%d\n",p,gp);
566 /* mark this position as a valid instruction start */
568 instructionstart[gp] = 1;
569 /*log_text("new start of instruction");*/
570 if (linepcchange==p) {
571 if (inline_env->method->linenumbercount > lineindex) {
572 currentline = inline_env->method->linenumbers[lineindex].line_number;
574 if (lineindex < inline_env->method->linenumbercount)
575 linepcchange = inline_env->method->linenumbers[lineindex].start_pc;
576 /*printf("Line number changed to: %ld\n",currentline);*/
582 if ((useinlining) && (gp == nextgp)) {
584 bool *readonly = NULL;
587 block_insert(gp); /* JJJJJJJJJJ */
589 instructionstart[gp] = 1;
590 m->basicblockindex[gp] |= (ipc << 1); /*FIXME: necessary ? */
592 opcode = code_get_u1(p,inline_env->method);
593 nextp = p += jcommandsize[opcode];
594 if (nextp > inline_env->method->jcodelength)
595 panic("Unexpected end of bytecode");
596 tmpinlinf = list_first(inlinfo->inlinedmethods);
597 firstlocal = tmpinlinf->firstlocal;
598 label_index = tmpinlinf->label_index;
599 readonly = tmpinlinf->readonly;
601 for (i=0,tptr=tmpinlinf->method->paramtypes;i<tmpinlinf->method->paramcount;i++,tptr++) {
602 if ( ((*tptr)==TYPE_LNG) ||
603 ((*tptr)==TYPE_DBL) )
609 for (i = 0, tptr = tmpinlinf->method->paramtypes + tmpinlinf->method->paramcount - 1; i < tmpinlinf->method->paramcount; i++, tptr--) {
612 if ((i == 0) && inlineparamopt) {
613 OP1(ICMD_CLEAR_ARGREN, firstlocal);
616 if (!inlineparamopt || !readonly[i]) {
620 op = ICMD_READONLY_ARG;
624 if ( ((*tptr)==TYPE_LNG) ||
625 ((*tptr)==TYPE_DBL) )
630 OP1(op, firstlocal + argBlockIdx);
631 /* OP1(op, firstlocal + tmpinlinf->method->paramcount - 1 - i); */
632 /* printf("inline argument load operation for local: %ld\n",firstlocal + tmpinlinf->method->paramcount - 1 - i); */
634 skipBasicBlockChange=1;
636 printf("BEFORE SAVE: "); fflush(stdout);
637 DEBUGMETH(inline_env->method);
639 inlining_save_compiler_variables();
641 printf("AFTER SAVE: "); fflush(stdout);
642 DEBUGMETH(inline_env->method);
644 inlining_set_compiler_variables(tmpinlinf);
646 printf("AFTER SET :: "); fflush(stdout);
647 DEBUGMETH(inline_env->method);
650 printf("\n.......Parsing (inlined): ");
652 DEBUGMETH(inline_env->method);
656 OP1(ICMD_INLINE_START,tmpinlinf->level);
658 if (inlinfo->inlinedmethods == NULL) {
661 tmpinlinf = list_first(inlinfo->inlinedmethods);
662 nextgp = (tmpinlinf != NULL) ? tmpinlinf->startgp : -1;
664 if (inline_env->method->exceptiontablelength > 0)
665 nextex = fillextable(m, nextex,
666 inline_env->method->exceptiontable, inline_env->method->exceptiontablelength,
667 label_index, &b_count, inline_env);
671 opcode = code_get_u1(p,inline_env->method); /* fetch op code */
674 printf("Parse p=%i<%i<%i< opcode=<%i> %s\n",
675 p, gp, inline_env->jcodelength, opcode, opcode_names[opcode]);
677 printf("label_index[%d]=%d\n",p,label_index[p]);
680 printf("basicblockindex[gp=%i]=%i=%p ipc=%i=%p shifted ipc=%i=%p\n",
681 gp,m->basicblockindex[gp],m->basicblockindex[gp],ipc,ipc,(ipc<<1),(ipc<<1));
684 if (!skipBasicBlockChange) {
685 m->basicblockindex[gp] |= (ipc << 1); /*store intermed cnt*/
686 } else skipBasicBlockChange=0;
688 printf("basicblockindex[gp=%i]=%i=%p \n",
689 gp,m->basicblockindex[gp],m->basicblockindex[gp]);
694 block_insert(gp); /* start new block */
696 /*printf("blockend was set: new blockcount: %ld at:%ld\n",b_count,gp);*/
699 nextp = p + jcommandsize[opcode]; /* compute next instruction start */
700 if (nextp > inline_env->method->jcodelength)
701 panic("Unexpected end of bytecode");
702 s_count += stackreq[opcode]; /* compute stack element count */
708 /* pushing constants onto the stack p */
711 LOADCONST_I(code_get_s1(p+1,inline_env->method));
715 LOADCONST_I(code_get_s2(p+1,inline_env->method));
719 i = code_get_u1(p+1,inline_env->method);
721 goto pushconstantitem;
724 i = code_get_u2(p + 1,inline_env->method);
728 if (i >= inline_env->method->class->cpcount)
729 panic ("Attempt to access constant outside range");
731 switch (inline_env->method->class->cptags[i]) {
732 case CONSTANT_Integer:
733 LOADCONST_I(((constant_integer *) (inline_env->method->class->cpinfos[i]))->value);
736 LOADCONST_L(((constant_long *) (inline_env->method->class->cpinfos[i]))->value);
739 LOADCONST_F(((constant_float *) (inline_env->method->class->cpinfos[i]))->value);
741 case CONSTANT_Double:
742 LOADCONST_D(((constant_double *) (inline_env->method->class->cpinfos[i]))->value);
744 case CONSTANT_String:
745 LOADCONST_A(literalstring_new((utf *) (inline_env->method->class->cpinfos[i])));
747 default: panic("Invalid constant type to push");
751 case JAVA_ACONST_NULL:
762 LOADCONST_I(opcode - JAVA_ICONST_0);
767 LOADCONST_L(opcode - JAVA_LCONST_0);
773 LOADCONST_F(opcode - JAVA_FCONST_0);
778 LOADCONST_D(opcode - JAVA_DCONST_0);
781 /* loading variables onto the stack */
789 i = code_get_u1(p + 1,inline_env->method);
791 i = code_get_u2(p + 1,inline_env->method);
795 OP1LOAD(opcode, i + firstlocal);
802 OP1LOAD(ICMD_ILOAD, opcode - JAVA_ILOAD_0 + firstlocal);
809 OP1LOAD(ICMD_LLOAD, opcode - JAVA_LLOAD_0 + firstlocal);
816 OP1LOAD(ICMD_FLOAD, opcode - JAVA_FLOAD_0 + firstlocal);
823 OP1LOAD(ICMD_DLOAD, opcode - JAVA_DLOAD_0 + firstlocal);
830 OP1LOAD(ICMD_ALOAD, opcode - JAVA_ALOAD_0 + firstlocal);
833 /* storing stack values into local variables */
841 i = code_get_u1(p + 1,inline_env->method);
843 i = code_get_u2(p + 1,inline_env->method);
847 OP1STORE(opcode, i + firstlocal);
854 OP1STORE(ICMD_ISTORE, opcode - JAVA_ISTORE_0 + firstlocal);
861 OP1STORE(ICMD_LSTORE, opcode - JAVA_LSTORE_0 + firstlocal);
868 OP1STORE(ICMD_FSTORE, opcode - JAVA_FSTORE_0 + firstlocal);
875 OP1STORE(ICMD_DSTORE, opcode - JAVA_DSTORE_0 + firstlocal);
882 OP1STORE(ICMD_ASTORE, opcode - JAVA_ASTORE_0 + firstlocal);
890 i = code_get_u1(p + 1,inline_env->method);
891 v = code_get_s1(p + 2,inline_env->method);
894 i = code_get_u2(p + 1,inline_env->method);
895 v = code_get_s2(p + 3,inline_env->method);
899 INDEX_ONEWORD(i + firstlocal);
900 OP2I(opcode, i + firstlocal, v);
904 /* wider index for loading, storing and incrementing */
911 /* managing arrays ************************************************/
915 switch (code_get_s1(p + 1,inline_env->method)) {
917 BUILTIN1(BUILTIN_newarray_boolean, TYPE_ADR,currentline);
920 BUILTIN1(BUILTIN_newarray_char, TYPE_ADR,currentline);
923 BUILTIN1(BUILTIN_newarray_float, TYPE_ADR,currentline);
926 BUILTIN1(BUILTIN_newarray_double, TYPE_ADR,currentline);
929 BUILTIN1(BUILTIN_newarray_byte, TYPE_ADR,currentline);
932 BUILTIN1(BUILTIN_newarray_short, TYPE_ADR,currentline);
935 BUILTIN1(BUILTIN_newarray_int, TYPE_ADR,currentline);
938 BUILTIN1(BUILTIN_newarray_long, TYPE_ADR,currentline);
940 default: panic("Invalid array-type to create");
942 OP(ICMD_CHECKEXCEPTION);
947 i = code_get_u2(p + 1,inline_env->method);
949 classinfo *component =
950 (classinfo *) class_getconstant(inline_env->method->class, i, CONSTANT_Class);
952 if (!class_load(component))
955 if (!class_link(component))
958 LOADCONST_A_BUILTIN(class_array_of(component)->vftbl);
959 /* LOADCONST_A_BUILTIN(component); */
961 BUILTIN2(BUILTIN_newarray, TYPE_ADR, currentline);
963 OP(ICMD_CHECKEXCEPTION);
966 case JAVA_MULTIANEWARRAY:
967 inline_env->method->isleafmethod = false;
968 i = code_get_u2(p + 1,inline_env->method);
971 s4 v = code_get_u1(p + 3,inline_env->method);
974 /* vftbl *arrayvftbl = */
975 /* ((classinfo *) class_getconstant(class, i, CONSTANT_Class))->vftbl; */
976 /* OP2A(opcode, v, arrayvftbl,currentline); */
979 classinfo *component =
980 (classinfo *) class_getconstant(inline_env->method->class, i, CONSTANT_Class);
982 if (!class_load(component))
985 if (!class_link(component))
988 arrayvftbl = component->vftbl;
989 OP2A(opcode, v, arrayvftbl, currentline);
991 /* classinfo *arrayclass = */
992 /* (classinfo *) class_getconstant(class, i, CONSTANT_Class); */
993 /* OP2A(opcode, v, arrayclass, currentline); */
1004 case JAVA_IFNONNULL:
1005 case JAVA_IF_ICMPEQ:
1006 case JAVA_IF_ICMPNE:
1007 case JAVA_IF_ICMPLT:
1008 case JAVA_IF_ICMPGT:
1009 case JAVA_IF_ICMPLE:
1010 case JAVA_IF_ICMPGE:
1011 case JAVA_IF_ACMPEQ:
1012 case JAVA_IF_ACMPNE:
1015 i = p + code_get_s2(p + 1,inline_env->method);
1027 i = p + code_get_s4(p + 1,inline_env->method);
1033 /*printf("B6 JSR_W\t"); fflush(stdout);*/
1041 i = code_get_u1(p + 1,inline_env->method);
1043 i = code_get_u2(p + 1,inline_env->method);
1050 if (inline_env->isinlinedmethod) {
1051 OP1(ICMD_GOTO, inlinfo->stopgp);
1055 OP1LOAD(opcode, i + firstlocal);
1064 if (inline_env->isinlinedmethod) {
1065 /* if (p==m->jcodelength-1) {*/ /* return is at end of inlined method */
1069 if (nextp>inline_env->method->jcodelength-1) {
1070 /* OP1(ICMD_GOTO, inlinfo->stopgp);
1078 OP1(ICMD_GOTO, inlinfo->stopgp);
1092 /* table jumps ********************************/
1094 case JAVA_LOOKUPSWITCH:
1101 nextp = ALIGN((p + 1), 4);
1102 if (nextp + 8 > inline_env->method->jcodelength)
1103 panic("Unexpected end of bytecode");
1105 tablep = (s4 *) (inline_env->method->jcode + nextp);
1108 num = code_get_u4(nextp + 4,inline_env->method);
1109 tablep = DMNEW(s4, num * 2 + 2);
1112 OP2A(opcode, 0, tablep,currentline);
1114 /* default target */
1116 j = p + code_get_s4(nextp,inline_env->method);
1119 *tablep = j; /* restore for little endian */
1125 /* number of pairs */
1127 num = code_get_u4(nextp,inline_env->method);
1132 if (nextp + 8*(num) > inline_env->method->jcodelength)
1133 panic("Unexpected end of bytecode");
1135 for (i = 0; i < num; i++) {
1138 j = code_get_s4(nextp,inline_env->method);
1139 *tablep = j; /* restore for little endian */
1143 /* check if the lookup table is sorted correctly */
1145 if (i && (j <= prevvalue))
1146 panic("invalid LOOKUPSWITCH: table not sorted");
1151 j = p + code_get_s4(nextp,inline_env->method);
1154 *tablep = j; /* restore for little endian */
1165 case JAVA_TABLESWITCH:
1171 nextp = ALIGN((p + 1), 4);
1172 if (nextp + 12 > inline_env->method->jcodelength)
1173 panic("Unexpected end of bytecode");
1175 tablep = (s4 *) (inline_env->method->jcode + nextp);
1178 num = code_get_u4(nextp + 8,inline_env->method) - code_get_u4(nextp + 4,inline_env->method);
1179 tablep = DMNEW(s4, num + 1 + 3);
1182 OP2A(opcode, 0, tablep,currentline);
1184 /* default target */
1186 j = p + code_get_s4(nextp,inline_env->method);
1189 *tablep = j; /* restore for little endian */
1197 j = code_get_s4(nextp,inline_env->method);
1198 *tablep = j; /* restore for little endian */
1204 num = code_get_s4(nextp,inline_env->method);
1205 *tablep = num; /* restore for little endian */
1209 num -= j; /* difference of upper - lower */
1211 panic("invalid TABLESWITCH: upper bound < lower bound");
1213 if (nextp + 4*(num+1) > inline_env->method->jcodelength)
1214 panic("Unexpected end of bytecode");
1216 for (i = 0; i <= num; i++) {
1217 j = p + code_get_s4(nextp,inline_env->method);
1219 /*printf("TABLESWITCH: j before mapping=%ld\n",j);*/
1222 *tablep = j; /* restore for little endian */
1227 /*printf("TABLESWITCH: block_insert(%ld)\n",j);*/
1234 /* load and store of object fields *******************/
1237 BUILTIN3(BUILTIN_aastore, TYPE_VOID, currentline);
1240 case JAVA_PUTSTATIC:
1241 case JAVA_GETSTATIC:
1242 i = code_get_u2(p + 1,inline_env->method);
1244 constant_FMIref *fr;
1247 fr = class_getconstant(inline_env->method->class, i, CONSTANT_Fieldref);
1249 if (!class_load(fr->class))
1252 if (!class_link(fr->class))
1255 fi = class_resolvefield(fr->class,
1258 inline_env->method->class,
1264 OP2A(opcode, fi->type, fi, currentline);
1265 if (!fi->class->initialized) {
1266 inline_env->method->isleafmethod = false;
1273 i = code_get_u2(p + 1,inline_env->method);
1275 constant_FMIref *fr;
1278 fr = class_getconstant(inline_env->method->class, i, CONSTANT_Fieldref);
1280 if (!class_load(fr->class))
1283 if (!class_link(fr->class))
1286 fi = class_resolvefield(fr->class,
1289 inline_env->method->class,
1295 OP2A(opcode, fi->type, fi, currentline);
1300 /* method invocation *****/
1302 case JAVA_INVOKESTATIC:
1303 i = code_get_u2(p + 1,inline_env->method);
1305 constant_FMIref *mr;
1308 inline_env->method->isleafmethod = false;
1310 mr = class_getconstant(inline_env->method->class, i, CONSTANT_Methodref);
1312 if (!class_load(mr->class))
1315 if (!class_link(mr->class))
1318 mi = class_resolveclassmethod(mr->class,
1321 inline_env->method->class,
1328 method_display_w_class(mi);
1329 printf("\tINVOKE STATIC\n");
1332 if (!(mi->flags & ACC_STATIC)) {
1334 new_exception(string_java_lang_IncompatibleClassChangeError);
1338 descriptor2types(mi);
1339 OP2A(opcode, mi->paramcount, mi, currentline);
1343 case JAVA_INVOKESPECIAL:
1344 case JAVA_INVOKEVIRTUAL:
1345 i = code_get_u2(p + 1,inline_env->method);
1347 constant_FMIref *mr;
1350 inline_env->method->isleafmethod = false;
1352 mr = class_getconstant(inline_env->method->class, i, CONSTANT_Methodref);
1354 if (!class_load(mr->class))
1357 if (!class_link(mr->class))
1360 mi = class_resolveclassmethod(mr->class,
1363 inline_env->method->class,
1370 method_display_w_class(mi);
1371 printf("\tINVOKE SPEC/VIRT\n");
1374 if (mi->flags & ACC_STATIC) {
1376 new_exception(string_java_lang_IncompatibleClassChangeError);
1380 descriptor2types(mi);
1381 OP2A(opcode, mi->paramcount, mi, currentline);
1385 case JAVA_INVOKEINTERFACE:
1386 i = code_get_u2(p + 1,inline_env->method);
1388 constant_FMIref *mr;
1391 inline_env->method->isleafmethod = false;
1393 mr = class_getconstant(inline_env->method->class, i, CONSTANT_InterfaceMethodref);
1395 if (!class_load(mr->class))
1398 if (!class_link(mr->class))
1401 mi = class_resolveinterfacemethod(mr->class,
1404 inline_env->method->class,
1409 if (mi->flags & ACC_STATIC) {
1411 new_exception(string_java_lang_IncompatibleClassChangeError);
1416 method_display_w_class(mi);
1417 printf("\tINVOKE INTERFACE\n");
1419 descriptor2types(mi);
1420 OP2A(opcode, mi->paramcount, mi, currentline);
1424 /* miscellaneous object operations *******/
1427 i = code_get_u2(p + 1,inline_env->method);
1428 LOADCONST_A_BUILTIN(class_getconstant(inline_env->method->class, i, CONSTANT_Class));
1430 BUILTIN1(BUILTIN_new, TYPE_ADR, currentline);
1431 OP(ICMD_CHECKEXCEPTION);
1434 case JAVA_CHECKCAST:
1435 i = code_get_u2(p + 1,inline_env->method);
1438 (classinfo *) class_getconstant(inline_env->method->class, i, CONSTANT_Class);
1441 if (!class_load(cls))
1445 if (!class_link(cls))
1448 if (cls->vftbl->arraydesc) {
1449 /* array type cast-check */
1450 LOADCONST_A_BUILTIN(cls->vftbl);
1452 BUILTIN2(BUILTIN_checkarraycast, TYPE_ADR,currentline);
1454 } else { /* object type cast-check */
1456 + LOADCONST_A_BUILTIN(class_getconstant(class, i, CONSTANT_Class));
1458 + BUILTIN2(BUILTIN_checkcast, TYPE_ADR,currentline);
1460 OP2A(opcode, 1, cls, currentline);
1465 case JAVA_INSTANCEOF:
1466 i = code_get_u2(p + 1,inline_env->method);
1469 (classinfo *) class_getconstant(inline_env->method->class, i, CONSTANT_Class);
1472 if (!class_load(cls))
1476 if (!class_link(cls))
1479 if (cls->vftbl->arraydesc) {
1480 /* array type cast-check */
1481 LOADCONST_A_BUILTIN(cls->vftbl);
1483 BUILTIN2(BUILTIN_arrayinstanceof, TYPE_INT, currentline);
1485 else { /* object type cast-check */
1487 LOADCONST_A_BUILTIN(class_getconstant(class, i, CONSTANT_Class));
1489 BUILTIN2(BUILTIN_instanceof, TYPE_INT,currentline);
1491 OP2A(opcode, 1, cls, currentline);
1496 case JAVA_MONITORENTER:
1497 #if defined(USE_THREADS)
1499 BUILTIN1(BUILTIN_monitorenter, TYPE_VOID,currentline);
1503 OP(ICMD_NULLCHECKPOP);
1507 case JAVA_MONITOREXIT:
1508 #if defined(USE_THREADS)
1510 BUILTIN1(BUILTIN_monitorexit, TYPE_VOID,currentline);
1511 OP(ICMD_CHECKEXCEPTION);
1519 /* any other basic operation **************************************/
1538 #if defined(__I386__)
1541 BUILTIN2(BUILTIN_frem, TYPE_FLOAT,currentline);
1546 #if defined(__I386__)
1549 BUILTIN2(BUILTIN_drem, TYPE_DOUBLE,currentline);
1554 #if defined(__ALPHA__)
1556 BUILTIN1(BUILTIN_f2i, TYPE_INT,currentline);
1565 #if defined(__ALPHA__)
1567 BUILTIN1(BUILTIN_f2l, TYPE_LONG,currentline);
1576 #if defined(__ALPHA__)
1578 BUILTIN1(BUILTIN_d2i, TYPE_INT,currentline);
1587 #if defined(__ALPHA__)
1589 BUILTIN1(BUILTIN_d2l, TYPE_LONG,currentline);
1597 case JAVA_BREAKPOINT:
1599 new_verifyerror(m, "Quick instructions shouldn't appear yet.");
1602 case 204: /* unused opcode */
1654 printf("Illegal opcode %d at instr %d\n", opcode, ipc);
1655 panic("Illegal opcode encountered");
1664 /* If WIDE was used correctly, iswide should have been reset by now. */
1665 if (iswide && opcode != JAVA_WIDE)
1666 panic("Illegal instruction: WIDE before incompatible opcode");
1670 /* if (inline_env->isinlinedmethod && p == inline_env->method->jcodelength - 1) { */ /* end of an inlined method */
1671 if (inline_env->isinlinedmethod && (nextp >= inline_env->method->jcodelength) ) { /* end of an inlined method */
1672 /* printf("setting gp from %d to %d\n",gp, inlinfo->stopgp); */
1673 gp = inlinfo->stopgp;
1674 inlining_restore_compiler_variables();
1675 OP(ICMD_INLINE_END);
1676 /*label_index = inlinfo->label_index;*/
1679 printf("AFTER RESTORE : "); fflush(stdout);
1680 DEBUGMETH(inline_env->method);
1682 list_remove(inlinfo->inlinedmethods, list_first(inlinfo->inlinedmethods));
1683 if (inlinfo->inlinedmethods == NULL) { /* JJJJ */
1686 tmpinlinf = list_first(inlinfo->inlinedmethods);
1687 nextgp = (tmpinlinf != NULL) ? tmpinlinf->startgp : -1;
1689 /* printf("nextpgp: %d\n", nextgp); */
1690 label_index=inlinfo->label_index;
1691 firstlocal = inlinfo->firstlocal;
1697 if (p != m->jcodelength) {
1698 printf("p (%d) != m->jcodelength (%d)\n",p,m->jcodelength);
1699 panic("Command-sequence crosses code-boundary");
1702 *exceptionptr = new_verifyerror(m, "Falling off the end of the code");
1706 /* adjust block count if target 0 is not first intermediate instruction */
1708 if (!m->basicblockindex[0] || (m->basicblockindex[0] > 1))
1711 /* copy local to method variables */
1713 m->instructioncount = ipc;
1714 m->basicblockcount = b_count;
1715 m->stackcount = s_count + m->basicblockcount * m->maxstack;
1717 /* allocate stack table */
1719 m->stack = DMNEW(stackelement, m->stackcount);
1724 bptr = m->basicblocks = DMNEW(basicblock, b_count + 1); /* one more for end ipc */
1729 /* additional block if target 0 is not first intermediate instruction */
1731 if (!m->basicblockindex[0] || (m->basicblockindex[0] > 1)) {
1732 bptr->iinstr = m->instructions;
1735 bptr->type = BBTYPE_STD;
1736 bptr->branchrefs = NULL;
1737 bptr->pre_count = 0;
1738 bptr->debug_nr = m->c_debug_nr++;
1741 (bptr - 1)->next = bptr;
1744 /* allocate blocks */
1746 for (p = 0; p < inline_env->cumjcodelength; p++) {
1747 /* for (p = 0; p < m->jcodelength; p++) { */
1748 if (m->basicblockindex[p] & 1) {
1749 /* check if this block starts at the beginning of an instruction */
1750 if (!instructionstart[p]) {
1751 printf("Basic Block beginn: %d\n",p);
1752 panic("Branch into middle of instruction");
1754 /* allocate the block */
1755 bptr->iinstr = m->instructions + (m->basicblockindex[p] >> 1);
1756 bptr->debug_nr = m->c_debug_nr++;
1758 (bptr - 1)->icount = bptr->iinstr - (bptr - 1)->iinstr;
1762 bptr->type = BBTYPE_STD;
1763 bptr->branchrefs = NULL;
1764 m->basicblockindex[p] = b_count;
1765 bptr->pre_count = 0;
1768 (bptr - 1)->next = bptr;
1772 /* allocate additional block at end */
1774 bptr->instack = bptr->outstack = NULL;
1775 bptr->indepth = bptr->outdepth = 0;
1776 bptr->iinstr = NULL;
1777 (bptr - 1)->icount = (m->instructions + m->instructioncount) - (bptr - 1)->iinstr;
1782 bptr->type = BBTYPE_STD;
1783 bptr->branchrefs = NULL;
1784 bptr->pre_count = 0;
1785 bptr->debug_nr = m->c_debug_nr++;
1786 (bptr - 1)->next = bptr;
1789 if (cd->exceptiontablelength > 0) {
1790 cd->exceptiontable[cd->exceptiontablelength - 1].down = NULL;
1793 for (i = 0; i < cd->exceptiontablelength; ++i) {
1794 p = cd->exceptiontable[i].startpc;
1795 cd->exceptiontable[i].start = m->basicblocks + m->basicblockindex[p];
1797 p = cd->exceptiontable[i].endpc;
1798 cd->exceptiontable[i].end = (p == inline_env->method->jcodelength) ? (m->basicblocks + m->basicblockcount /*+ 1*/) : (m->basicblocks + m->basicblockindex[p]);
1800 p = cd->exceptiontable[i].handlerpc;
1801 cd->exceptiontable[i].handler = m->basicblocks + m->basicblockindex[p];
1805 if (useinlining) inlining_cleanup(inline_env);
1807 /* just return methodinfo* to signal everything was ok */
1814 * These are local overrides for various environment variables in Emacs.
1815 * Please do not remove this and leave it at the end of the file, where
1816 * Emacs will automagically detect them.
1817 * ---------------------------------------------------------------------
1820 * indent-tabs-mode: t