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 991 2004-03-29 11:22:34Z stefan $
44 #include "loop/loop.h"
50 #include "toolbox/memory.h"
51 #include "toolbox/loging.h"
54 /* data about the currently parsed method */
56 classinfo *rt_class; /* class the compiled method belongs to */
57 methodinfo *rt_method; /* pointer to method info of compiled method */
58 utf *rt_descriptor; /* type descriptor of compiled method */
59 int rt_jcodelength; /* length of JavaVM-codes */
60 u1 *rt_jcode; /* pointer to start of JavaVM-code */
66 /*#define debug_writebranch printf("op: %s i: %d label_index[i]: %d\n",icmd_names[opcode], i, label_index[i]);*/
67 #define debug_writebranch
70 /* function descriptor2typesL ***************************************************
72 decodes a already checked method descriptor. The parameter count, the
73 return type and the argument types are stored in the passed methodinfo.
74 gets and saves classptr for object ref.s
76 *******************************************************************************/
78 classSetNode *descriptor2typesL(methodinfo *m)
85 classinfo** classtypes;
90 printf("In descriptor2typesL >>>\t"); fflush(stdout);
91 utf_display(m->class->name); printf(".");
92 method_display(m);fflush(stdout);
96 desc = MNEW (char, 256);
97 types = DMNEW (u1, m->descriptor->blength);
98 classtypes = MNEW (classinfo*, m->descriptor->blength+1);
99 m->returnclass = NULL;
101 if (!(m->flags & ACC_STATIC)) {
103 if (debugInfo >= 1) {
104 printf("param #0 (this?) method class =");utf_display(m->class->name);printf("\n");
106 classtypes[pcount] = m->class;
107 p = addClassCone(p, m->class);
111 utf_ptr = m->descriptor->text + 1;
112 strcpy (desc,utf_ptr);
114 while ((c = *desc++) != ')') {
121 case 'Z': *tptr++ = TYPE_INT;
123 case 'J': *tptr++ = TYPE_LNG;
125 case 'F': *tptr++ = TYPE_FLT;
127 case 'D': *tptr++ = TYPE_DBL;
129 case 'L': *tptr++ = TYPE_ADR;
130 /* get class string */
131 class = strtok(desc,";");
132 desc = strtok(NULL,"\0");
133 /* get/save classinfo ptr */
134 classtypes[pcount-1] = class_get(utf_new_char(class));
135 p = addClassCone(p, class_get(utf_new_char(class)));
136 if (debugInfo >= 1) {
137 printf("LParam#%i 's class type is: %s\n",pcount-1,class);fflush(stdout);
138 printf("Lclasstypes[%i]=",pcount-1);fflush(stdout);
139 utf_display(classtypes[pcount-1]->name);
142 case '[': *tptr++ = TYPE_ADR;
145 /* 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("[Param#%i 's class type is: %s\n",pcount-1,class);
154 printf("[classtypes[%i]=",pcount-1);fflush(stdout);
155 utf_display(classtypes[pcount-1]->name);
160 classtypes[pcount-1] = NULL;
163 panic("Ill formed methodtype-descriptor");
167 /* compute return type */
173 case 'Z': m->returntype = TYPE_INT;
175 case 'J': m->returntype = TYPE_LNG;
177 case 'F': m->returntype = TYPE_FLT;
179 case 'D': m->returntype = TYPE_DBL;
182 m->returntype = TYPE_ADR;
190 m->returntype = TYPE_ADR;
192 /* get class string */
193 class = strtok(desc,";");
194 m->returnclass = class_get(utf_new_char(class));
195 if (m->returnclass == NULL) {
196 printf("class=%s :\t",class);
197 panic ("return class not found");
200 case 'V': m->returntype = TYPE_VOID;
203 default: panic("Ill formed methodtype-descriptor-ReturnType");
206 m->paramcount = pcount;
207 m->paramtypes = types;
208 m->paramclass = classtypes;
212 for (i=0; i< m->paramcount; i++) {
213 if ((m->paramtypes[i] == TYPE_ADR) && (m->paramclass[i] != NULL)) {
214 printf("Param #%i is:\t",i);
215 utf_display(m->paramclass[i]->name);
220 if ((m->returntype == TYPE_ADR) && (m->returnclass != NULL)) {
221 printf("\tReturn Type is:\t"); fflush(stdout);
222 utf_display(m->returnclass->name);
226 printf("params2types: START results in a set \n");
227 printf("param2types: A Set size=%i=\n",sizeOfSet(p));
236 /* function descriptor2types ***************************************************
238 decodes a already checked method descriptor. The parameter count, the
239 return type and the argument types are stored in the passed methodinfo.
241 *******************************************************************************/
243 void descriptor2types(methodinfo *m)
249 types = DMNEW(u1, m->descriptor->blength);
252 if (!(m->flags & ACC_STATIC)) {
257 utf_ptr = m->descriptor->text + 1;
259 while ((c = *utf_ptr++) != ')') {
266 case 'Z': *tptr++ = TYPE_INT;
268 case 'J': *tptr++ = TYPE_LNG;
270 case 'F': *tptr++ = TYPE_FLT;
272 case 'D': *tptr++ = TYPE_DBL;
274 case 'L': *tptr++ = TYPE_ADR;
275 while (*utf_ptr++ != ';');
277 case '[': *tptr++ = TYPE_ADR;
281 while (*utf_ptr++ != ';') /* skip */;
283 default: panic ("Ill formed methodtype-descriptor");
287 /* compute return type */
289 switch (*utf_ptr++) {
294 case 'Z': m->returntype = TYPE_INT;
296 case 'J': m->returntype = TYPE_LNG;
298 case 'F': m->returntype = TYPE_FLT;
300 case 'D': m->returntype = TYPE_DBL;
303 case 'L': m->returntype = TYPE_ADR;
305 case 'V': m->returntype = TYPE_VOID;
308 default: panic ("Ill formed methodtype-descriptor");
311 m->paramcount = pcount;
312 m->paramtypes = types;
317 /*******************************************************************************
319 function 'parse' scans the JavaVM code and generates intermediate code
321 During parsing the block index table is used to store at bit pos 0
322 a flag which marks basic block starts and at position 1 to 31 the
323 intermediate instruction index. After parsing the block index table
324 is scanned, for marked positions a block is generated and the block
325 number is stored in the block index table.
327 *******************************************************************************/
329 /* intermediate code generating macros */
331 #define PINC iptr++;ipc++
332 #define LOADCONST_I(v) iptr->opc=ICMD_ICONST;/*iptr->op1=0*/;iptr->val.i=(v);iptr->line=currentline;iptr->clazz=class;PINC
333 #define LOADCONST_L(v) iptr->opc=ICMD_LCONST;/*iptr->op1=0*/;iptr->val.l=(v);iptr->line=currentline;iptr->clazz=class;PINC
334 #define LOADCONST_F(v) iptr->opc=ICMD_FCONST;/*iptr->op1=0*/;iptr->val.f=(v);iptr->line=currentline;iptr->clazz=class;PINC
335 #define LOADCONST_D(v) iptr->opc=ICMD_DCONST;/*iptr->op1=0*/;iptr->val.d=(v);iptr->line=currentline;iptr->clazz=class;PINC
336 #define LOADCONST_A(v) iptr->opc=ICMD_ACONST;/*iptr->op1=0*/;iptr->val.a=(v);iptr->line=currentline;iptr->clazz=class;PINC
338 /* ACONST instructions generated as arguments for builtin functions
339 * have op1 set to non-zero. This is used for stack overflow checking
341 #define LOADCONST_A_BUILTIN(v) \
342 iptr->opc=ICMD_ACONST;iptr->op1=1;iptr->val.a=(v);iptr->line=currentline;iptr->clazz=class;PINC
344 #define OP(o) iptr->opc=(o);/*iptr->op1=0*/;/*iptr->val.l=0*/;iptr->line=currentline;iptr->clazz=class;PINC
345 #define OP1(o,o1) iptr->opc=(o);iptr->op1=(o1);/*iptr->val.l=(0)*/;iptr->line=currentline;iptr->clazz=class;PINC
346 #define OP2I(o,o1,v) iptr->opc=(o);iptr->op1=(o1);iptr->val.i=(v);iptr->line=currentline;iptr->clazz=class;PINC
347 #define OP2A(o,o1,v,l) iptr->opc=(o);iptr->op1=(o1);iptr->val.a=(v);iptr->line=l;iptr->clazz=class;PINC
348 #define BUILTIN1(v,t,l) isleafmethod=false;iptr->opc=ICMD_BUILTIN1;iptr->op1=t;\
349 iptr->val.a=(v);iptr->line=l;iptr->clazz=class;PINC
350 #define BUILTIN2(v,t,l) isleafmethod=false;iptr->opc=ICMD_BUILTIN2;iptr->op1=t;\
351 iptr->val.a=(v);iptr->line=l;iptr->clazz=class;PINC
352 #define BUILTIN3(v,t,l) isleafmethod=false;iptr->opc=ICMD_BUILTIN3;iptr->op1=t;\
353 iptr->val.a=(v);iptr->line=l;iptr->clazz=class;PINC
355 /* We have to check local variables indices here because they are
356 * used in stack.c to index the locals array. */
358 #define INDEX_ONEWORD(num) \
359 do { if((num)<0 || (num)>=maxlocals) \
360 panic("Invalid local variable index"); } while (0)
361 #define INDEX_TWOWORD(num) \
362 do { if((num)<0 || ((num)+1)>=maxlocals) \
363 panic("Invalid local variable index"); } while (0)
365 #define OP1LOAD(o,o1) \
366 do {if (o == ICMD_LLOAD || o == ICMD_DLOAD) \
372 #define OP1STORE(o,o1) \
373 do {if (o == ICMD_LSTORE || o == ICMD_DSTORE) \
379 /* block generating and checking macros */
381 #define block_insert(i) \
383 if (!(block_index[(i)] & 1)) { \
385 block_index[(i)] |= 1; \
390 /* FIXME really use cumjcodelength for the bound_checkers ? */
392 #define bound_check(i) \
394 if (i < 0 || i >= cumjcodelength) { \
395 panic("branch target out of code-boundary"); \
399 /* bound_check1 is used for the inclusive ends of exception handler ranges */
400 #define bound_check1(i) \
402 if (i < 0 || i > cumjcodelength) { \
403 panic("branch target out of code-boundary"); \
409 static xtable* fillextable(xtable* extable, exceptiontable *raw_extable, int exceptiontablelength, int *label_index, int *block_count)
413 if (exceptiontablelength == 0)
416 b_count = *block_count;
418 for (i = 0; i < exceptiontablelength; i++) {
419 p = raw_extable[i].startpc;
420 if (label_index != NULL) p = label_index[p];
421 extable[i].startpc = p;
425 p = raw_extable[i].endpc;
426 if (p <= raw_extable[i].startpc)
427 panic("Invalid exception handler range");
428 if (label_index != NULL) p = label_index[p];
429 extable[i].endpc = p;
431 if (p < cumjcodelength)
434 p = raw_extable[i].handlerpc;
435 if (label_index != NULL) p = label_index[p];
436 extable[i].handlerpc = p;
440 extable[i].catchtype = raw_extable[i].catchtype;
442 extable[i].next = NULL;
443 extable[i].down = &extable[i + 1];
446 *block_count = b_count;
447 return &extable[i]; /* return the next free xtable* */
454 int p; /* java instruction counter */
455 int nextp; /* start of next java instruction */
456 int opcode; /* java opcode */
457 int i; /* temporary for different uses (counters) */
458 int ipc = 0; /* intermediate instruction counter */
459 int b_count = 0; /* basic block counter */
460 int s_count = 0; /* stack element counter */
461 bool blockend = false; /* true if basic block end has been reached */
462 bool iswide = false; /* true if last instruction was a wide */
463 instruction *iptr; /* current pointer into instruction array */
464 int gp; /* global java instruction counter */
465 /* inlining info for current method */
466 inlining_methodinfo *inlinfo = inlining_rootinfo;
467 inlining_methodinfo *tmpinlinf;
468 int nextgp = -1; /* start of next method to be inlined */
469 int *label_index = NULL; /* label redirection table */
470 int firstlocal = 0; /* first local variable of method */
471 xtable* nextex; /* points next free entry in extable */
472 u1 *instructionstart; /* 1 for pcs which are valid instr. starts */
480 if (compileverbose) {
481 char logtext[MAXLOGTEXT];
482 sprintf(logtext, "Parsing: ");
483 utf_sprint(logtext+strlen(logtext), method->class->name);
484 strcpy(logtext+strlen(logtext), ".");
485 utf_sprint(logtext+strlen(logtext), method->name);
486 utf_sprint(logtext+strlen(logtext), method->descriptor);
492 label_index = inlinfo->label_index;
493 maxstack = cummaxstack;
494 exceptiontablelength = cumextablelength;
497 useinltmp = useinlining; /* FIXME remove this after debugging */
498 /*useinlining = false;*/ /* and merge the if-statements */
501 cumjcodelength = jcodelength;
504 tmpinlinf = (inlining_methodinfo*) list_first(inlinfo->inlinedmethods);
505 if (tmpinlinf != NULL) nextgp = tmpinlinf->startgp;
508 if ((opt_rt || opt_xta || opt_vta) && (pOpcodes == 2 || pOpcodes == 3)) {
509 printf("PARSE method name =");
510 utf_display(method->class->name);
512 method_display(method);
517 if (opt_rt || opt_xta) {
518 RT_jit_parse(method);
522 printf("VTA requested, but not yet implemented\n");
526 /* allocate instruction array and block index table */
528 /* 1 additional for end ipc and 3 for loop unrolling */
530 block_index = DMNEW(int, cumjcodelength + 4);
531 instructionstart = DMNEW(u1, cumjcodelength + 4);
532 memset(instructionstart,0,sizeof(u1) * (cumjcodelength + 4));
534 /* 1 additional for TRACEBUILTIN and 4 for MONITORENTER/EXIT */
535 /* additional MONITOREXITS are reached by branches which are 3 bytes */
537 iptr = instr = DMNEW(instruction, cumjcodelength + 5);
539 /* Zero the intermediate instructions array so we don't have any
540 * invalid pointers in it if we cannot finish analyse_stack(). */
541 memset(iptr,0,sizeof(instruction) * (cumjcodelength + 5));
543 /* initialize block_index table (unrolled four times) */
548 for (i = 0, ip = block_index; i <= cumjcodelength; i += 4, ip += 4) {
556 /* compute branch targets of exception table */
558 extable = DMNEW(xtable, exceptiontablelength + 1);
560 for (i = 0; i < method->exceptiontablelength; i++) {
562 p = extable[i].startpc = raw_extable[i].startpc;
563 if (useinlining) p = label_index[p];
567 p = extable[i].endpc = raw_extable[i].endpc;
568 if (useinlining) p = label_index[p];
570 if (p < cumjcodelength)
573 p = extable[i].handlerpc = raw_extable[i].handlerpc;
577 extable[i].catchtype = raw_extable[i].catchtype;
579 extable[i].next = NULL;
580 extable[i].down = &extable[i+1];
584 nextex = fillextable(extable, raw_extable, method->exceptiontablelength, label_index, &b_count);
586 s_count = 1 + exceptiontablelength; /* initialize stack element counter */
589 if (checksync && (method->flags & ACC_SYNCHRONIZED)) {
590 isleafmethod = false;
594 /* scan all java instructions */
597 if (jlinenumbercount==0) {
600 linepcchange=jlinenumbers[0].start_pc;
603 for (p = 0, gp = 0; p < jcodelength; gp += (nextp - p), p = nextp) {
605 /* DEBUG */ /*printf("p:%d gp:%d ",p,gp);*/
607 /* mark this position as a valid instruction start */
609 instructionstart[gp] = 1;
610 /*log_text("new start of instruction");*/
611 if (linepcchange==p) {
612 if (jlinenumbercount>lineindex) {
613 currentline=jlinenumbers[lineindex].line_number;
615 if (lineindex<jlinenumbercount)
616 linepcchange=jlinenumbers[lineindex].start_pc;
617 /*printf("Line number changed to: %ld\n",currentline);*/
623 if ((useinlining) && (gp == nextgp)) {
625 bool *readonly = NULL;
628 opcode = code_get_u1(p);
629 nextp = p += jcommandsize[opcode];
630 if (nextp > jcodelength)
631 panic("Unexpected end of bytecode");
632 tmpinlinf = list_first(inlinfo->inlinedmethods);
633 firstlocal = tmpinlinf->firstlocal;
634 label_index = tmpinlinf->label_index;
635 readonly = tmpinlinf->readonly;
637 for (i = 0, tptr = tmpinlinf->method->paramtypes + tmpinlinf->method->paramcount - 1; i < tmpinlinf->method->paramcount; i++, tptr--) {
640 if ((i == 0) && inlineparamopt) {
641 OP1(ICMD_CLEAR_ARGREN, firstlocal);
644 if (!inlineparamopt || !readonly[i]) {
648 op = ICMD_READONLY_ARG;
652 OP1(op, firstlocal + tmpinlinf->method->paramcount - 1 - i);
654 /* block_index[gp] |= (ipc << 1);*/ /*FIXME: necessary ? */
657 inlining_save_compiler_variables();
658 inlining_set_compiler_variables(tmpinlinf);
659 if (compileverbose) {
660 char logtext[MAXLOGTEXT];
661 sprintf(logtext, "Parsing (inlined): ");
662 utf_sprint(logtext+strlen(logtext), method->class->name);
663 strcpy(logtext+strlen(logtext), ".");
664 utf_sprint(logtext+strlen(logtext), method->name);
665 utf_sprint(logtext+strlen(logtext), method->descriptor);
670 if (inlinfo->inlinedmethods == NULL) {
673 tmpinlinf = list_first(inlinfo->inlinedmethods);
674 nextgp = (tmpinlinf != NULL) ? tmpinlinf->startgp : -1;
676 if (method->exceptiontablelength > 0)
677 nextex = fillextable(nextex, method->exceptiontable, method->exceptiontablelength, label_index, &b_count);
681 opcode = code_get_u1(p); /* fetch op code */
684 if (opt_rt && (pOpcodes == 2 || pOpcodes == 3)) {
685 printf("Parse<%i> p=%i<%i< opcode=<%i> %s\n",
686 pOpcodes, p, rt_jcodelength, opcode, icmd_names[opcode]);
689 block_index[gp] |= (ipc << 1); /* store intermediate count */
692 block_insert(gp); /* start new block */
696 nextp = p + jcommandsize[opcode]; /* compute next instruction start */
697 if (nextp > jcodelength)
698 panic("Unexpected end of bytecode");
699 s_count += stackreq[opcode]; /* compute stack element count */
705 /* pushing constants onto the stack p */
708 LOADCONST_I(code_get_s1(p+1));
712 LOADCONST_I(code_get_s2(p+1));
716 i = code_get_u1(p+1);
717 goto pushconstantitem;
720 i = code_get_u2(p + 1);
724 if (i >= class->cpcount)
725 panic ("Attempt to access constant outside range");
727 switch (class->cptags[i]) {
728 case CONSTANT_Integer:
729 LOADCONST_I(((constant_integer*)
730 (class->cpinfos[i]))->value);
733 LOADCONST_L(((constant_long*)
734 (class->cpinfos[i]))->value);
737 LOADCONST_F(((constant_float*)
738 (class->cpinfos[i]))->value);
740 case CONSTANT_Double:
741 LOADCONST_D(((constant_double*)
742 (class->cpinfos[i]))->value);
744 case CONSTANT_String:
745 LOADCONST_A(literalstring_new((utf*)
746 (class->cpinfos[i])));
748 default: panic("Invalid constant type to push");
752 case JAVA_ACONST_NULL:
763 LOADCONST_I(opcode - JAVA_ICONST_0);
768 LOADCONST_L(opcode - JAVA_LCONST_0);
774 LOADCONST_F(opcode - JAVA_FCONST_0);
779 LOADCONST_D(opcode - JAVA_DCONST_0);
782 /* loading variables onto the stack */
790 i = code_get_u1(p + 1);
792 i = code_get_u2(p + 1);
796 OP1LOAD(opcode, i + firstlocal);
803 OP1LOAD(ICMD_ILOAD, opcode - JAVA_ILOAD_0 + firstlocal);
810 OP1LOAD(ICMD_LLOAD, opcode - JAVA_LLOAD_0 + firstlocal);
817 OP1LOAD(ICMD_FLOAD, opcode - JAVA_FLOAD_0 + firstlocal);
824 OP1LOAD(ICMD_DLOAD, opcode - JAVA_DLOAD_0 + firstlocal);
831 OP1LOAD(ICMD_ALOAD, opcode - JAVA_ALOAD_0 + firstlocal);
834 /* storing stack values into local variables */
842 i = code_get_u1(p + 1);
844 i = code_get_u2(p + 1);
848 OP1STORE(opcode, i + firstlocal);
855 OP1STORE(ICMD_ISTORE, opcode - JAVA_ISTORE_0 + firstlocal);
862 OP1STORE(ICMD_LSTORE, opcode - JAVA_LSTORE_0 + firstlocal);
869 OP1STORE(ICMD_FSTORE, opcode - JAVA_FSTORE_0 + firstlocal);
876 OP1STORE(ICMD_DSTORE, opcode - JAVA_DSTORE_0 + firstlocal);
883 OP1STORE(ICMD_ASTORE, opcode - JAVA_ASTORE_0 + firstlocal);
891 i = code_get_u1(p + 1);
892 v = code_get_s1(p + 2);
895 i = code_get_u2(p + 1);
896 v = code_get_s2(p + 3);
900 INDEX_ONEWORD(i + firstlocal);
901 OP2I(opcode, i + firstlocal, v);
905 /* wider index for loading, storing and incrementing */
912 /* managing arrays ************************************************/
915 OP2I(ICMD_CHECKASIZE, 0, 0);
916 switch (code_get_s1(p + 1)) {
918 BUILTIN1(BUILTIN_newarray_boolean, TYPE_ADR,currentline);
921 BUILTIN1(BUILTIN_newarray_char, TYPE_ADR,currentline);
924 BUILTIN1(BUILTIN_newarray_float, TYPE_ADR,currentline);
927 BUILTIN1(BUILTIN_newarray_double, TYPE_ADR,currentline);
930 BUILTIN1(BUILTIN_newarray_byte, TYPE_ADR,currentline);
933 BUILTIN1(BUILTIN_newarray_short, TYPE_ADR,currentline);
936 BUILTIN1(BUILTIN_newarray_int, TYPE_ADR,currentline);
939 BUILTIN1(BUILTIN_newarray_long, TYPE_ADR,currentline);
941 default: panic("Invalid array-type to create");
946 OP2I(ICMD_CHECKASIZE, 0, 0);
947 i = code_get_u2(p + 1);
949 classinfo *component = (classinfo*)class_getconstant(class, i, CONSTANT_Class);
950 LOADCONST_A_BUILTIN(class_array_of(component)->vftbl);
954 BUILTIN2(BUILTIN_newarray, TYPE_ADR,currentline);
958 case JAVA_MULTIANEWARRAY:
960 i = code_get_u2(p + 1);
962 int v = code_get_u1(p + 3);
963 vftbl *arrayvftbl = ((classinfo*)class_getconstant (class, i, CONSTANT_Class))->vftbl;
964 OP2A(opcode, v, arrayvftbl,currentline);
986 i = p + code_get_s2(p + 1);
998 i = p + code_get_s4(p + 1);
1011 i = code_get_u1(p + 1);
1013 i = code_get_u2(p + 1);
1020 if (isinlinedmethod) {
1021 OP1(ICMD_GOTO, inlinfo->stopgp);
1025 OP1LOAD(opcode, i + firstlocal);
1034 if (isinlinedmethod) {
1035 /* if (p==jcodelength-1) {*/ /* return is at end of inlined method */
1040 OP1(ICMD_GOTO, inlinfo->stopgp);
1054 /* table jumps ********************************/
1056 case JAVA_LOOKUPSWITCH:
1063 nextp = ALIGN((p + 1), 4);
1064 if (nextp + 8 > jcodelength)
1065 panic("Unexpected end of bytecode");
1067 tablep = (s4*)(jcode + nextp);
1070 num = code_get_u4(nextp + 4);
1071 tablep = DMNEW(s4, num * 2 + 2);
1074 OP2A(opcode, 0, tablep,currentline);
1076 /* default target */
1078 j = p + code_get_s4(nextp);
1081 *tablep = j; /* restore for little endian */
1087 /* number of pairs */
1089 num = code_get_u4(nextp);
1094 if (nextp + 8*(num) > jcodelength)
1095 panic("Unexpected end of bytecode");
1097 for (i = 0; i < num; i++) {
1100 j = code_get_s4(nextp);
1101 *tablep = j; /* restore for little endian */
1105 /* check if the lookup table is sorted correctly */
1107 if (i && (j <= prevvalue))
1108 panic("invalid LOOKUPSWITCH: table not sorted");
1113 j = p + code_get_s4(nextp);
1116 *tablep = j; /* restore for little endian */
1127 case JAVA_TABLESWITCH:
1133 nextp = ALIGN((p + 1), 4);
1134 if (nextp + 12 > jcodelength)
1135 panic("Unexpected end of bytecode");
1137 tablep = (s4*)(jcode + nextp);
1140 num = code_get_u4(nextp + 8) - code_get_u4(nextp + 4);
1141 tablep = DMNEW(s4, num + 1 + 3);
1144 OP2A(opcode, 0, tablep,currentline);
1146 /* default target */
1148 j = p + code_get_s4(nextp);
1151 *tablep = j; /* restore for little endian */
1159 j = code_get_s4(nextp);
1160 *tablep = j; /* restore for little endian */
1166 num = code_get_s4(nextp);
1167 *tablep = num; /* restore for little endian */
1171 num -= j; /* difference of upper - lower */
1173 panic("invalid TABLESWITCH: upper bound < lower bound");
1175 if (nextp + 4*(num+1) > jcodelength)
1176 panic("Unexpected end of bytecode");
1178 for (i = 0; i <= num; i++) {
1179 j = p + code_get_s4(nextp);
1182 *tablep = j; /* restore for little endian */
1193 /* load and store of object fields *******************/
1196 BUILTIN3(BUILTIN_aastore, TYPE_VOID,currentline);
1199 case JAVA_PUTSTATIC:
1200 case JAVA_GETSTATIC:
1201 i = code_get_u2(p + 1);
1203 constant_FMIref *fr;
1205 fr = class_getconstant(class, i, CONSTANT_Fieldref);
1206 fi = class_findfield(fr->class, fr->name, fr->descriptor);
1207 OP2A(opcode, fi->type, fi,currentline);
1208 if (!fi->class->initialized) {
1209 isleafmethod = false;
1216 i = code_get_u2(p + 1);
1218 constant_FMIref *fr;
1220 fr = class_getconstant (class, i, CONSTANT_Fieldref);
1221 fi = class_findfield (fr->class, fr->name, fr->descriptor);
1222 OP2A(opcode, fi->type, fi,currentline);
1227 /* method invocation *****/
1229 case JAVA_INVOKESTATIC:
1230 i = code_get_u2(p + 1);
1232 constant_FMIref *mr;
1235 mr = class_getconstant (class, i, CONSTANT_Methodref);
1236 mi = class_fetchmethod (mr->class, mr->name, mr->descriptor);
1237 /*RTAprint*/ if (((pOpcodes == 2) || (pOpcodes == 3)) && opt_rt)
1238 /*RTAprint*/ {printf(" method name =");
1239 /*RTAprint*/ utf_display(mi->class->name); printf(".");
1240 /*RTAprint*/ utf_display(mi->name);printf("\tINVOKE STATIC\n");
1241 /*RTAprint*/ fflush(stdout);}
1242 if (!(mi->flags & ACC_STATIC))
1243 panic ("Static/Nonstatic mismatch calling static method");
1244 descriptor2types(mi);
1247 OP2A(opcode, mi->paramcount, mi,currentline);
1251 case JAVA_INVOKESPECIAL:
1252 case JAVA_INVOKEVIRTUAL:
1253 i = code_get_u2(p + 1);
1255 constant_FMIref *mr;
1258 mr = class_getconstant (class, i, CONSTANT_Methodref);
1259 mi = class_fetchmethod (mr->class, mr->name, mr->descriptor);
1260 /*RTAprint*/ if (((pOpcodes == 2) || (pOpcodes == 3)) && opt_rt)
1261 /*RTAprint*/ {printf(" method name =");
1263 /*RTAprint*/ utf_display(mi->class->name); printf(".");
1264 /*RTAprint*/ utf_display(mi->name);printf("\tINVOKE SPECIAL/VIRTUAL\n");
1265 /*RTAprint*/ fflush(stdout);}
1267 if (mi->flags & ACC_STATIC)
1268 panic ("Static/Nonstatic mismatch calling static method");
1269 descriptor2types(mi);
1271 OP2A(opcode, mi->paramcount, mi,currentline);
1275 case JAVA_INVOKEINTERFACE:
1276 i = code_get_u2(p + 1);
1278 constant_FMIref *mr;
1281 mr = class_getconstant (class, i, CONSTANT_InterfaceMethodref);
1282 mi = class_fetchmethod (mr->class, mr->name, mr->descriptor);
1283 if (mi->flags & ACC_STATIC)
1284 panic ("Static/Nonstatic mismatch calling static method");
1285 descriptor2types(mi);
1287 OP2A(opcode, mi->paramcount, mi,currentline);
1291 /* miscellaneous object operations *******/
1294 i = code_get_u2 (p+1);
1296 LOADCONST_A_BUILTIN(class_getconstant(class, i, CONSTANT_Class));
1298 BUILTIN1(BUILTIN_new, TYPE_ADR,currentline);
1301 case JAVA_CHECKCAST:
1302 i = code_get_u2(p+1);
1304 classinfo *cls = (classinfo*)class_getconstant(class, i, CONSTANT_Class);
1305 if (cls->vftbl->arraydesc) {
1306 /* array type cast-check */
1307 LOADCONST_A_BUILTIN(cls->vftbl);
1309 BUILTIN2(BUILTIN_checkarraycast, TYPE_ADR,currentline);
1311 else { /* object type cast-check */
1313 + LOADCONST_A_BUILTIN(class_getconstant(class, i, CONSTANT_Class));
1315 + BUILTIN2(BUILTIN_checkcast, TYPE_ADR,currentline);
1317 OP2A(opcode, 1, cls,currentline);
1323 case JAVA_INSTANCEOF:
1324 i = code_get_u2(p+1);
1327 classinfo *cls = (classinfo*)class_getconstant(class, i, CONSTANT_Class);
1328 if (cls->vftbl->arraydesc) {
1329 /* array type cast-check */
1330 LOADCONST_A_BUILTIN(cls->vftbl);
1332 BUILTIN2(BUILTIN_arrayinstanceof, TYPE_INT,currentline);
1334 else { /* object type cast-check */
1336 LOADCONST_A_BUILTIN(class_getconstant(class, i, CONSTANT_Class));
1338 BUILTIN2(BUILTIN_instanceof, TYPE_INT,currentline);
1340 OP2A(opcode, 1, cls,currentline);
1345 case JAVA_MONITORENTER:
1348 BUILTIN1(BUILTIN_monitorenter, TYPE_VOID,currentline);
1352 OP(ICMD_NULLCHECKPOP);
1356 case JAVA_MONITOREXIT:
1359 BUILTIN1(BUILTIN_monitorexit, TYPE_VOID,currentline);
1368 /* any other basic operation **************************************/
1387 #if defined(__I386__)
1390 BUILTIN2(BUILTIN_frem, TYPE_FLOAT,currentline);
1395 #if defined(__I386__)
1398 BUILTIN2(BUILTIN_drem, TYPE_DOUBLE,currentline);
1403 #if defined(__ALPHA__)
1405 BUILTIN1(BUILTIN_f2i, TYPE_INT,currentline);
1414 #if defined(__ALPHA__)
1416 BUILTIN1(BUILTIN_f2l, TYPE_LONG,currentline);
1425 #if defined(__ALPHA__)
1427 BUILTIN1(BUILTIN_d2i, TYPE_INT,currentline);
1436 #if defined(__ALPHA__)
1438 BUILTIN1(BUILTIN_d2l, TYPE_LONG,currentline);
1446 case JAVA_BREAKPOINT:
1447 panic("Illegal opcode Breakpoint encountered");
1450 case 186: /* unused opcode */
1504 printf("Illegal opcode %d at instr %d\n", opcode, ipc);
1505 panic("Illegal opcode encountered");
1514 /* If WIDE was used correctly, iswide should have been reset by now. */
1515 if (iswide && opcode != JAVA_WIDE)
1516 panic("Illegal instruction: WIDE before incompatible opcode");
1520 if (isinlinedmethod && p == jcodelength - 1) { /* end of an inlined method */
1521 /* printf("setting gp from %d to %d\n",gp, inlinfo->stopgp); */
1522 gp = inlinfo->stopgp;
1523 inlining_restore_compiler_variables();
1524 list_remove(inlinfo->inlinedmethods, list_first(inlinfo->inlinedmethods));
1525 if (inlinfo->inlinedmethods == NULL) {
1528 tmpinlinf = list_first(inlinfo->inlinedmethods);
1529 nextgp = (tmpinlinf != NULL) ? tmpinlinf->startgp : -1;
1531 /* printf("nextpgp: %d\n", nextgp); */
1532 label_index=inlinfo->label_index;
1533 firstlocal = inlinfo->firstlocal;
1537 if (p != jcodelength)
1538 panic("Command-sequence crosses code-boundary");
1541 panic("Code does not end with branch/return/athrow - stmt");
1543 /* adjust block count if target 0 is not first intermediate instruction */
1545 if (!block_index[0] || (block_index[0] > 1))
1548 /* copy local to global variables */
1551 block_count = b_count;
1552 stack_count = s_count + block_count * maxstack;
1554 /* allocate stack table */
1556 stack = DMNEW(stackelement, stack_count);
1561 bptr = block = DMNEW(basicblock, b_count + 1); /* one more for end ipc */
1566 /* additional block if target 0 is not first intermediate instruction */
1568 if (!block_index[0] || (block_index[0] > 1)) {
1569 bptr->iinstr = instr;
1572 bptr->type = BBTYPE_STD;
1573 bptr->branchrefs = NULL;
1574 bptr->pre_count = 0;
1575 bptr->debug_nr = c_debug_nr++;
1578 (bptr - 1)->next = bptr;
1581 /* allocate blocks */
1583 for (p = 0; p < cumjcodelength; p++) {
1584 if (block_index[p] & 1) {
1585 /* check if this block starts at the beginning of an instruction */
1586 if (!instructionstart[p])
1587 panic("Branch into middle of instruction");
1588 /* allocate the block */
1589 bptr->iinstr = instr + (block_index[p] >> 1);
1590 bptr->debug_nr = c_debug_nr++;
1592 (bptr - 1)->icount = bptr->iinstr - (bptr - 1)->iinstr;
1596 bptr->type = BBTYPE_STD;
1597 bptr->branchrefs = NULL;
1598 block_index[p] = b_count;
1599 bptr->pre_count = 0;
1602 (bptr - 1)->next = bptr;
1606 /* allocate additional block at end */
1608 bptr->instack = bptr->outstack = NULL;
1609 bptr->indepth = bptr->outdepth = 0;
1610 bptr->iinstr = NULL;
1611 (bptr - 1)->icount = (instr + instr_count) - (bptr - 1)->iinstr;
1616 bptr->type = BBTYPE_STD;
1617 bptr->branchrefs = NULL;
1618 bptr->pre_count = 0;
1619 bptr->debug_nr = c_debug_nr++;
1620 (bptr - 1)->next = bptr;
1625 if (exceptiontablelength > 0)
1626 extable[exceptiontablelength - 1].down = NULL;
1630 for (i = 0; i < exceptiontablelength; ++i) {
1631 p = extable[i].startpc;
1632 extable[i].start = block + block_index[p];
1634 p = extable[i].endpc;
1635 extable[i].end = (p == cumjcodelength) ? last_block : (block + block_index[p]);
1637 p = extable[i].handlerpc;
1638 extable[i].handler = block + block_index[p];
1642 if (useinlining) inlining_cleanup();
1643 useinlining = useinltmp;
1648 * These are local overrides for various environment variables in Emacs.
1649 * Please do not remove this and leave it at the end of the file, where
1650 * Emacs will automagically detect them.
1651 * ---------------------------------------------------------------------
1654 * indent-tabs-mode: t