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 893 2004-01-19 12:53:24Z edwin $
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);PINC
333 #define LOADCONST_L(v) iptr->opc=ICMD_LCONST;/*iptr->op1=0*/;iptr->val.l=(v);PINC
334 #define LOADCONST_F(v) iptr->opc=ICMD_FCONST;/*iptr->op1=0*/;iptr->val.f=(v);PINC
335 #define LOADCONST_D(v) iptr->opc=ICMD_DCONST;/*iptr->op1=0*/;iptr->val.d=(v);PINC
336 #define LOADCONST_A(v) iptr->opc=ICMD_ACONST;/*iptr->op1=0*/;iptr->val.a=(v);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);PINC
344 #define OP(o) iptr->opc=(o);/*iptr->op1=0*/;/*iptr->val.l=0*/;PINC
345 #define OP1(o,o1) iptr->opc=(o);iptr->op1=(o1);/*iptr->val.l=(0)*/;PINC
346 #define OP2I(o,o1,v) iptr->opc=(o);iptr->op1=(o1);iptr->val.i=(v);PINC
347 #define OP2A(o,o1,v) iptr->opc=(o);iptr->op1=(o1);iptr->val.a=(v);PINC
348 #define BUILTIN1(v,t) isleafmethod=false;iptr->opc=ICMD_BUILTIN1;iptr->op1=t;\
350 #define BUILTIN2(v,t) isleafmethod=false;iptr->opc=ICMD_BUILTIN2;iptr->op1=t;\
352 #define BUILTIN3(v,t) isleafmethod=false;iptr->opc=ICMD_BUILTIN3;iptr->op1=t;\
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 */
476 if (compileverbose) {
477 char logtext[MAXLOGTEXT];
478 sprintf(logtext, "Parsing: ");
479 utf_sprint(logtext+strlen(logtext), method->class->name);
480 strcpy(logtext+strlen(logtext), ".");
481 utf_sprint(logtext+strlen(logtext), method->name);
482 utf_sprint(logtext+strlen(logtext), method->descriptor);
488 label_index = inlinfo->label_index;
489 maxstack = cummaxstack;
490 exceptiontablelength = cumextablelength;
493 useinltmp = useinlining; /* FIXME remove this after debugging */
494 /*useinlining = false;*/ /* and merge the if-statements */
497 cumjcodelength = jcodelength;
500 tmpinlinf = (inlining_methodinfo*) list_first(inlinfo->inlinedmethods);
501 if (tmpinlinf != NULL) nextgp = tmpinlinf->startgp;
504 if ((opt_rt || opt_xta || opt_vta) && (pOpcodes == 2 || pOpcodes == 3)) {
505 printf("PARSE method name =");
506 utf_display(method->class->name);
508 method_display(method);
513 if (opt_rt || opt_xta) {
514 RT_jit_parse(method);
518 printf("VTA requested, but not yet implemented\n");
522 /* allocate instruction array and block index table */
524 /* 1 additional for end ipc and 3 for loop unrolling */
526 block_index = DMNEW(int, cumjcodelength + 4);
527 instructionstart = DMNEW(u1, cumjcodelength + 4);
528 memset(instructionstart,0,sizeof(u1) * (cumjcodelength + 4));
530 /* 1 additional for TRACEBUILTIN and 4 for MONITORENTER/EXIT */
531 /* additional MONITOREXITS are reached by branches which are 3 bytes */
533 iptr = instr = DMNEW(instruction, cumjcodelength + 5);
535 /* Zero the intermediate instructions array so we don't have any
536 * invalid pointers in it if we cannot finish analyse_stack(). */
537 memset(iptr,0,sizeof(instruction) * (cumjcodelength + 5));
539 /* initialize block_index table (unrolled four times) */
544 for (i = 0, ip = block_index; i <= cumjcodelength; i += 4, ip += 4) {
552 /* compute branch targets of exception table */
554 extable = DMNEW(xtable, exceptiontablelength + 1);
556 for (i = 0; i < method->exceptiontablelength; i++) {
558 p = extable[i].startpc = raw_extable[i].startpc;
559 if (useinlining) p = label_index[p];
563 p = extable[i].endpc = raw_extable[i].endpc;
564 if (useinlining) p = label_index[p];
566 if (p < cumjcodelength)
569 p = extable[i].handlerpc = raw_extable[i].handlerpc;
573 extable[i].catchtype = raw_extable[i].catchtype;
575 extable[i].next = NULL;
576 extable[i].down = &extable[i+1];
580 nextex = fillextable(extable, raw_extable, method->exceptiontablelength, label_index, &b_count);
582 s_count = 1 + exceptiontablelength; /* initialize stack element counter */
585 if (checksync && (method->flags & ACC_SYNCHRONIZED)) {
586 isleafmethod = false;
590 /* scan all java instructions */
592 for (p = 0, gp = 0; p < jcodelength; gp += (nextp - p), p = nextp) {
594 /* DEBUG */ /*printf("p:%d gp:%d ",p,gp);*/
596 /* mark this position as a valid instruction start */
598 instructionstart[p] = 1;
601 if ((useinlining) && (gp == nextgp)) {
603 bool *readonly = NULL;
605 opcode = code_get_u1(p);
606 nextp = p += jcommandsize[opcode];
607 if (nextp > jcodelength)
608 panic("Unexpected end of bytecode");
609 tmpinlinf = list_first(inlinfo->inlinedmethods);
610 firstlocal = tmpinlinf->firstlocal;
611 label_index = tmpinlinf->label_index;
612 readonly = tmpinlinf->readonly;
614 for (i = 0, tptr = tmpinlinf->method->paramtypes + tmpinlinf->method->paramcount - 1; i < tmpinlinf->method->paramcount; i++, tptr--) {
617 if ((i == 0) && inlineparamopt) {
618 OP1(ICMD_CLEAR_ARGREN, firstlocal);
621 if (!inlineparamopt || !readonly[i]) {
625 op = ICMD_READONLY_ARG;
629 OP1(op, firstlocal + tmpinlinf->method->paramcount - 1 - i);
631 /* block_index[gp] |= (ipc << 1);*/ /*FIXME: necessary ? */
634 inlining_save_compiler_variables();
635 inlining_set_compiler_variables(tmpinlinf);
637 if (inlinfo->inlinedmethods == NULL) {
640 tmpinlinf = list_first(inlinfo->inlinedmethods);
641 nextgp = (tmpinlinf != NULL) ? tmpinlinf->startgp : -1;
643 if (method->exceptiontablelength > 0)
644 nextex = fillextable(nextex, method->exceptiontable, method->exceptiontablelength, label_index, &b_count);
648 opcode = code_get_u1(p); /* fetch op code */
651 if (opt_rt && (pOpcodes == 2 || pOpcodes == 3)) {
652 printf("Parse<%i> p=%i<%i< opcode=<%i> %s\n",
653 pOpcodes, p, rt_jcodelength, opcode, icmd_names[opcode]);
656 block_index[gp] |= (ipc << 1); /* store intermediate count */
659 block_insert(gp); /* start new block */
663 nextp = p + jcommandsize[opcode]; /* compute next instruction start */
664 if (nextp > jcodelength)
665 panic("Unexpected end of bytecode");
666 s_count += stackreq[opcode]; /* compute stack element count */
672 /* pushing constants onto the stack p */
675 LOADCONST_I(code_get_s1(p+1));
679 LOADCONST_I(code_get_s2(p+1));
683 i = code_get_u1(p+1);
684 goto pushconstantitem;
687 i = code_get_u2(p + 1);
691 if (i >= class->cpcount)
692 panic ("Attempt to access constant outside range");
694 switch (class->cptags[i]) {
695 case CONSTANT_Integer:
696 LOADCONST_I(((constant_integer*)
697 (class->cpinfos[i]))->value);
700 LOADCONST_L(((constant_long*)
701 (class->cpinfos[i]))->value);
704 LOADCONST_F(((constant_float*)
705 (class->cpinfos[i]))->value);
707 case CONSTANT_Double:
708 LOADCONST_D(((constant_double*)
709 (class->cpinfos[i]))->value);
711 case CONSTANT_String:
712 LOADCONST_A(literalstring_new((utf*)
713 (class->cpinfos[i])));
715 default: panic("Invalid constant type to push");
719 case JAVA_ACONST_NULL:
730 LOADCONST_I(opcode - JAVA_ICONST_0);
735 LOADCONST_L(opcode - JAVA_LCONST_0);
741 LOADCONST_F(opcode - JAVA_FCONST_0);
746 LOADCONST_D(opcode - JAVA_DCONST_0);
749 /* loading variables onto the stack */
757 i = code_get_u1(p + 1);
759 i = code_get_u2(p + 1);
763 OP1LOAD(opcode, i + firstlocal);
770 OP1LOAD(ICMD_ILOAD, opcode - JAVA_ILOAD_0 + firstlocal);
777 OP1LOAD(ICMD_LLOAD, opcode - JAVA_LLOAD_0 + firstlocal);
784 OP1LOAD(ICMD_FLOAD, opcode - JAVA_FLOAD_0 + firstlocal);
791 OP1LOAD(ICMD_DLOAD, opcode - JAVA_DLOAD_0 + firstlocal);
798 OP1LOAD(ICMD_ALOAD, opcode - JAVA_ALOAD_0 + firstlocal);
801 /* storing stack values into local variables */
809 i = code_get_u1(p + 1);
811 i = code_get_u2(p + 1);
815 OP1STORE(opcode, i + firstlocal);
822 OP1STORE(ICMD_ISTORE, opcode - JAVA_ISTORE_0 + firstlocal);
829 OP1STORE(ICMD_LSTORE, opcode - JAVA_LSTORE_0 + firstlocal);
836 OP1STORE(ICMD_FSTORE, opcode - JAVA_FSTORE_0 + firstlocal);
843 OP1STORE(ICMD_DSTORE, opcode - JAVA_DSTORE_0 + firstlocal);
850 OP1STORE(ICMD_ASTORE, opcode - JAVA_ASTORE_0 + firstlocal);
858 i = code_get_u1(p + 1);
859 v = code_get_s1(p + 2);
862 i = code_get_u2(p + 1);
863 v = code_get_s2(p + 3);
867 INDEX_ONEWORD(i + firstlocal);
868 OP2I(opcode, i + firstlocal, v);
872 /* wider index for loading, storing and incrementing */
879 /* managing arrays ************************************************/
882 OP2I(ICMD_CHECKASIZE, 0, 0);
883 switch (code_get_s1(p + 1)) {
885 BUILTIN1(BUILTIN_newarray_boolean, TYPE_ADR);
888 BUILTIN1(BUILTIN_newarray_char, TYPE_ADR);
891 BUILTIN1(BUILTIN_newarray_float, TYPE_ADR);
894 BUILTIN1(BUILTIN_newarray_double, TYPE_ADR);
897 BUILTIN1(BUILTIN_newarray_byte, TYPE_ADR);
900 BUILTIN1(BUILTIN_newarray_short, TYPE_ADR);
903 BUILTIN1(BUILTIN_newarray_int, TYPE_ADR);
906 BUILTIN1(BUILTIN_newarray_long, TYPE_ADR);
908 default: panic("Invalid array-type to create");
913 OP2I(ICMD_CHECKASIZE, 0, 0);
914 i = code_get_u2(p + 1);
916 classinfo *component = (classinfo*)class_getconstant(class, i, CONSTANT_Class);
917 LOADCONST_A_BUILTIN(class_array_of(component)->vftbl);
921 BUILTIN2(BUILTIN_newarray, TYPE_ADR);
925 case JAVA_MULTIANEWARRAY:
927 i = code_get_u2(p + 1);
929 int v = code_get_u1(p + 3);
930 vftbl *arrayvftbl = ((classinfo*)class_getconstant (class, i, CONSTANT_Class))->vftbl;
931 OP2A(opcode, v, arrayvftbl);
953 i = p + code_get_s2(p + 1);
965 i = p + code_get_s4(p + 1);
978 i = code_get_u1(p + 1);
980 i = code_get_u2(p + 1);
987 if (isinlinedmethod) {
988 OP1(ICMD_GOTO, inlinfo->stopgp);
992 OP1LOAD(opcode, i + firstlocal);
1001 if (isinlinedmethod) {
1002 /* if (p==jcodelength-1) {*/ /* return is at end of inlined method */
1007 OP1(ICMD_GOTO, inlinfo->stopgp);
1021 /* table jumps ********************************/
1023 case JAVA_LOOKUPSWITCH:
1030 nextp = ALIGN((p + 1), 4);
1031 if (nextp + 8 > jcodelength)
1032 panic("Unexpected end of bytecode");
1034 tablep = (s4*)(jcode + nextp);
1037 num = code_get_u4(nextp + 4);
1038 tablep = DMNEW(s4, num * 2 + 2);
1041 OP2A(opcode, 0, tablep);
1043 /* default target */
1045 j = p + code_get_s4(nextp);
1048 *tablep = j; /* restore for little endian */
1054 /* number of pairs */
1056 num = code_get_u4(nextp);
1061 if (nextp + 8*(num) > jcodelength)
1062 panic("Unexpected end of bytecode");
1064 for (i = 0; i < num; i++) {
1067 j = code_get_s4(nextp);
1068 *tablep = j; /* restore for little endian */
1072 /* check if the lookup table is sorted correctly */
1074 if (i && (j <= prevvalue))
1075 panic("invalid LOOKUPSWITCH: table not sorted");
1080 j = p + code_get_s4(nextp);
1083 *tablep = j; /* restore for little endian */
1094 case JAVA_TABLESWITCH:
1100 nextp = ALIGN((p + 1), 4);
1101 if (nextp + 12 > jcodelength)
1102 panic("Unexpected end of bytecode");
1104 tablep = (s4*)(jcode + nextp);
1107 num = code_get_u4(nextp + 8) - code_get_u4(nextp + 4);
1108 tablep = DMNEW(s4, num + 1 + 3);
1111 OP2A(opcode, 0, tablep);
1113 /* default target */
1115 j = p + code_get_s4(nextp);
1118 *tablep = j; /* restore for little endian */
1126 j = code_get_s4(nextp);
1127 *tablep = j; /* restore for little endian */
1133 num = code_get_s4(nextp);
1134 *tablep = num; /* restore for little endian */
1138 num -= j; /* difference of upper - lower */
1140 panic("invalid TABLESWITCH: upper bound < lower bound");
1142 if (nextp + 4*(num+1) > jcodelength)
1143 panic("Unexpected end of bytecode");
1145 for (i = 0; i <= num; i++) {
1146 j = p + code_get_s4(nextp);
1149 *tablep = j; /* restore for little endian */
1160 /* load and store of object fields *******************/
1163 BUILTIN3(BUILTIN_aastore, TYPE_VOID);
1166 case JAVA_PUTSTATIC:
1167 case JAVA_GETSTATIC:
1168 i = code_get_u2(p + 1);
1170 constant_FMIref *fr;
1172 fr = class_getconstant(class, i, CONSTANT_Fieldref);
1173 fi = class_findfield(fr->class, fr->name, fr->descriptor);
1174 OP2A(opcode, fi->type, fi);
1175 if (!fi->class->initialized) {
1176 isleafmethod = false;
1183 i = code_get_u2(p + 1);
1185 constant_FMIref *fr;
1187 fr = class_getconstant (class, i, CONSTANT_Fieldref);
1188 fi = class_findfield (fr->class, fr->name, fr->descriptor);
1189 OP2A(opcode, fi->type, fi);
1194 /* method invocation *****/
1196 case JAVA_INVOKESTATIC:
1197 i = code_get_u2(p + 1);
1199 constant_FMIref *mr;
1202 mr = class_getconstant (class, i, CONSTANT_Methodref);
1203 mi = class_fetchmethod (mr->class, mr->name, mr->descriptor);
1204 /*RTAprint*/ if (((pOpcodes == 2) || (pOpcodes == 3)) && opt_rt)
1205 /*RTAprint*/ {printf(" method name =");
1206 /*RTAprint*/ utf_display(mi->class->name); printf(".");
1207 /*RTAprint*/ utf_display(mi->name);printf("\tINVOKE STATIC\n");
1208 /*RTAprint*/ fflush(stdout);}
1209 if (!(mi->flags & ACC_STATIC))
1210 panic ("Static/Nonstatic mismatch calling static method");
1211 descriptor2types(mi);
1214 OP2A(opcode, mi->paramcount, mi);
1218 case JAVA_INVOKESPECIAL:
1219 case JAVA_INVOKEVIRTUAL:
1220 i = code_get_u2(p + 1);
1222 constant_FMIref *mr;
1225 mr = class_getconstant (class, i, CONSTANT_Methodref);
1226 mi = class_fetchmethod (mr->class, mr->name, mr->descriptor);
1227 /*RTAprint*/ if (((pOpcodes == 2) || (pOpcodes == 3)) && opt_rt)
1228 /*RTAprint*/ {printf(" method name =");
1230 /*RTAprint*/ utf_display(mi->class->name); printf(".");
1231 /*RTAprint*/ utf_display(mi->name);printf("\tINVOKE SPECIAL/VIRTUAL\n");
1232 /*RTAprint*/ fflush(stdout);}
1234 if (mi->flags & ACC_STATIC)
1235 panic ("Static/Nonstatic mismatch calling static method");
1236 descriptor2types(mi);
1238 OP2A(opcode, mi->paramcount, mi);
1242 case JAVA_INVOKEINTERFACE:
1243 i = code_get_u2(p + 1);
1245 constant_FMIref *mr;
1248 mr = class_getconstant (class, i, CONSTANT_InterfaceMethodref);
1249 mi = class_fetchmethod (mr->class, mr->name, mr->descriptor);
1250 if (mi->flags & ACC_STATIC)
1251 panic ("Static/Nonstatic mismatch calling static method");
1252 descriptor2types(mi);
1254 OP2A(opcode, mi->paramcount, mi);
1258 /* miscellaneous object operations *******/
1261 i = code_get_u2 (p+1);
1263 LOADCONST_A_BUILTIN(class_getconstant(class, i, CONSTANT_Class));
1265 BUILTIN1(BUILTIN_new, TYPE_ADR);
1268 case JAVA_CHECKCAST:
1269 #if defined(USE_THREADS) && defined(NATIVE_THREADS)
1270 isleafmethod = false;
1272 i = code_get_u2(p+1);
1274 classinfo *cls = (classinfo*)class_getconstant(class, i, CONSTANT_Class);
1275 if (cls->vftbl->arraydesc) {
1276 /* array type cast-check */
1277 LOADCONST_A_BUILTIN(cls->vftbl);
1279 BUILTIN2(BUILTIN_checkarraycast, TYPE_ADR);
1281 else { /* object type cast-check */
1283 + LOADCONST_A_BUILTIN(class_getconstant(class, i, CONSTANT_Class));
1285 + BUILTIN2(BUILTIN_checkcast, TYPE_ADR);
1287 OP2A(opcode, 1, cls);
1293 case JAVA_INSTANCEOF:
1294 #if defined(USE_THREADS) && defined(NATIVE_THREADS)
1295 isleafmethod = false;
1297 i = code_get_u2(p+1);
1300 classinfo *cls = (classinfo*)class_getconstant(class, i, CONSTANT_Class);
1301 if (cls->vftbl->arraydesc) {
1302 /* array type cast-check */
1303 LOADCONST_A_BUILTIN(cls->vftbl);
1305 BUILTIN2(BUILTIN_arrayinstanceof, TYPE_INT);
1307 else { /* object type cast-check */
1309 LOADCONST_A_BUILTIN(class_getconstant(class, i, CONSTANT_Class));
1311 BUILTIN2(BUILTIN_instanceof, TYPE_INT);
1313 OP2A(opcode, 1, cls);
1318 case JAVA_MONITORENTER:
1321 BUILTIN1(BUILTIN_monitorenter, TYPE_VOID);
1325 OP(ICMD_NULLCHECKPOP);
1329 case JAVA_MONITOREXIT:
1332 BUILTIN1(BUILTIN_monitorexit, TYPE_VOID);
1341 /* any other basic operation **************************************/
1360 #if defined(__I386__)
1363 BUILTIN2(BUILTIN_frem, TYPE_FLOAT);
1368 #if defined(__I386__)
1371 BUILTIN2(BUILTIN_drem, TYPE_DOUBLE);
1376 #if defined(__ALPHA__)
1378 BUILTIN1(BUILTIN_f2i, TYPE_INT);
1387 #if defined(__ALPHA__)
1389 BUILTIN1(BUILTIN_f2l, TYPE_LONG);
1398 #if defined(__ALPHA__)
1400 BUILTIN1(BUILTIN_d2i, TYPE_INT);
1409 #if defined(__ALPHA__)
1411 BUILTIN1(BUILTIN_d2l, TYPE_LONG);
1419 case JAVA_BREAKPOINT:
1420 panic("Illegal opcode Breakpoint encountered");
1423 case 186: /* unused opcode */
1477 printf("Illegal opcode %d at instr %d\n", opcode, ipc);
1478 panic("Illegal opcode encountered");
1487 /* If WIDE was used correctly, iswide should have been reset by now. */
1488 if (iswide && opcode != JAVA_WIDE)
1489 panic("Illegal instruction: WIDE before incompatible opcode");
1493 if (isinlinedmethod && p == jcodelength - 1) { /* end of an inlined method */
1494 /* printf("setting gp from %d to %d\n",gp, inlinfo->stopgp); */
1495 gp = inlinfo->stopgp;
1496 inlining_restore_compiler_variables();
1497 list_remove(inlinfo->inlinedmethods, list_first(inlinfo->inlinedmethods));
1498 if (inlinfo->inlinedmethods == NULL) {
1501 tmpinlinf = list_first(inlinfo->inlinedmethods);
1502 nextgp = (tmpinlinf != NULL) ? tmpinlinf->startgp : -1;
1504 /* printf("nextpgp: %d\n", nextgp); */
1505 label_index=inlinfo->label_index;
1506 firstlocal = inlinfo->firstlocal;
1510 if (p != jcodelength)
1511 panic("Command-sequence crosses code-boundary");
1514 panic("Code does not end with branch/return/athrow - stmt");
1516 /* adjust block count if target 0 is not first intermediate instruction */
1518 if (!block_index[0] || (block_index[0] > 1))
1521 /* copy local to global variables */
1524 block_count = b_count;
1525 stack_count = s_count + block_count * maxstack;
1527 /* allocate stack table */
1529 stack = DMNEW(stackelement, stack_count);
1534 bptr = block = DMNEW(basicblock, b_count + 1); /* one more for end ipc */
1539 /* additional block if target 0 is not first intermediate instruction */
1541 if (!block_index[0] || (block_index[0] > 1)) {
1542 bptr->iinstr = instr;
1545 bptr->type = BBTYPE_STD;
1546 bptr->branchrefs = NULL;
1547 bptr->pre_count = 0;
1548 bptr->debug_nr = c_debug_nr++;
1551 (bptr - 1)->next = bptr;
1554 /* allocate blocks */
1556 for (p = 0; p < cumjcodelength; p++) {
1557 if (block_index[p] & 1) {
1558 /* check if this block starts at the beginning of an instruction */
1559 if (!instructionstart[p])
1560 panic("Branch into middle of instruction");
1561 /* allocate the block */
1562 bptr->iinstr = instr + (block_index[p] >> 1);
1563 bptr->debug_nr = c_debug_nr++;
1565 (bptr - 1)->icount = bptr->iinstr - (bptr - 1)->iinstr;
1569 bptr->type = BBTYPE_STD;
1570 bptr->branchrefs = NULL;
1571 block_index[p] = b_count;
1572 bptr->pre_count = 0;
1575 (bptr - 1)->next = bptr;
1579 /* allocate additional block at end */
1581 bptr->instack = bptr->outstack = NULL;
1582 bptr->indepth = bptr->outdepth = 0;
1583 bptr->iinstr = NULL;
1584 (bptr - 1)->icount = (instr + instr_count) - (bptr - 1)->iinstr;
1589 bptr->type = BBTYPE_STD;
1590 bptr->branchrefs = NULL;
1591 bptr->pre_count = 0;
1592 bptr->debug_nr = c_debug_nr++;
1593 (bptr - 1)->next = bptr;
1598 if (exceptiontablelength > 0)
1599 extable[exceptiontablelength - 1].down = NULL;
1603 for (i = 0; i < exceptiontablelength; ++i) {
1604 p = extable[i].startpc;
1605 extable[i].start = block + block_index[p];
1607 p = extable[i].endpc;
1608 extable[i].end = (p == cumjcodelength) ? last_block : (block + block_index[p]);
1610 p = extable[i].handlerpc;
1611 extable[i].handler = block + block_index[p];
1615 if (useinlining) inlining_cleanup();
1616 useinlining = useinltmp;
1621 * These are local overrides for various environment variables in Emacs.
1622 * Please do not remove this and leave it at the end of the file, where
1623 * Emacs will automagically detect them.
1624 * ---------------------------------------------------------------------
1627 * indent-tabs-mode: t