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 843 2004-01-05 00:50:24Z twisti $
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
337 #define OP(o) iptr->opc=(o);iptr->op1=0;iptr->val.l=0;PINC
338 #define OP1(o,o1) iptr->opc=(o);iptr->op1=(o1);iptr->val.l=(0);PINC
339 #define OP2I(o,o1,v) iptr->opc=(o);iptr->op1=(o1);iptr->val.i=(v);PINC
340 #define OP2A(o,o1,v) iptr->opc=(o);iptr->op1=(o1);iptr->val.a=(v);PINC
341 #define BUILTIN1(v,t) isleafmethod=false;iptr->opc=ICMD_BUILTIN1;iptr->op1=t;\
343 #define BUILTIN2(v,t) isleafmethod=false;iptr->opc=ICMD_BUILTIN2;iptr->op1=t;\
345 #define BUILTIN3(v,t) isleafmethod=false;iptr->opc=ICMD_BUILTIN3;iptr->op1=t;\
348 #define INDEX_ONEWORD(num) \
349 do { if((num)<0 || (num)>=maxlocals) \
350 panic("Invalid local variable index"); } while (0)
351 #define INDEX_TWOWORD(num) \
352 do { if((num)<0 || ((num)+1)>=maxlocals) \
353 panic("Invalid local variable index"); } while (0)
355 #define OP1LOAD(o,o1) \
356 do {if (o == ICMD_LLOAD || o == ICMD_DLOAD) \
362 #define OP1STORE(o,o1) \
363 do {if (o == ICMD_LSTORE || o == ICMD_DSTORE) \
369 /* block generating and checking macros */
371 #define block_insert(i) \
373 if (!(block_index[(i)] & 1)) { \
375 block_index[(i)] |= 1; \
380 /* FIXME really use cumjcodelength for the bound_checkers ? */
382 #define bound_check(i) \
384 if (i < 0 || i >= cumjcodelength) { \
385 panic("branch target out of code-boundary"); \
389 /* bound_check1 is used for the inclusive ends of exception handler ranges */
390 #define bound_check1(i) \
392 if (i < 0 || i > cumjcodelength) { \
393 panic("branch target out of code-boundary"); \
399 static xtable* fillextable(xtable* extable, exceptiontable *raw_extable, int exceptiontablelength, int *label_index, int *block_count)
403 if (exceptiontablelength == 0)
406 b_count = *block_count;
408 for (i = 0; i < exceptiontablelength; i++) {
409 p = raw_extable[i].startpc;
410 if (label_index != NULL) p = label_index[p];
411 extable[i].startpc = p;
415 p = raw_extable[i].endpc;
416 if (p <= raw_extable[i].startpc)
417 panic("Invalid exception handler range");
418 if (label_index != NULL) p = label_index[p];
419 extable[i].endpc = p;
421 if (p < cumjcodelength)
424 p = raw_extable[i].handlerpc;
425 if (label_index != NULL) p = label_index[p];
426 extable[i].handlerpc = p;
430 extable[i].catchtype = raw_extable[i].catchtype;
432 extable[i].next = NULL;
433 extable[i].down = &extable[i + 1];
436 *block_count = b_count;
437 return &extable[i]; /* return the next free xtable* */
444 int p; /* java instruction counter */
445 int nextp; /* start of next java instruction */
446 int opcode; /* java opcode */
447 int i; /* temporary for different uses (counters) */
448 int ipc = 0; /* intermediate instruction counter */
449 int b_count = 0; /* basic block counter */
450 int s_count = 0; /* stack element counter */
451 bool blockend = false; /* true if basic block end has been reached */
452 bool iswide = false; /* true if last instruction was a wide */
453 instruction *iptr; /* current pointer into instruction array */
454 int gp; /* global java instruction counter */
455 /* inlining info for current method */
456 inlining_methodinfo *inlinfo = inlining_rootinfo;
457 inlining_methodinfo *tmpinlinf;
458 int nextgp = -1; /* start of next method to be inlined */
459 int *label_index = NULL; /* label redirection table */
460 int firstlocal = 0; /* first local variable of method */
461 xtable* nextex; /* points next free entry in extable */
462 u1 *instructionstart; /* 1 for pcs which are valid instr. starts */
466 if (compileverbose) {
467 char logtext[MAXLOGTEXT];
468 sprintf(logtext, "Parsing: ");
469 utf_sprint(logtext+strlen(logtext), method->class->name);
470 strcpy(logtext+strlen(logtext), ".");
471 utf_sprint(logtext+strlen(logtext), method->name);
472 utf_sprint(logtext+strlen(logtext), method->descriptor);
478 label_index = inlinfo->label_index;
479 maxstack = cummaxstack;
480 exceptiontablelength = cumextablelength;
483 useinltmp = useinlining; /* FIXME remove this after debugging */
484 /*useinlining = false;*/ /* and merge the if-statements */
487 cumjcodelength = jcodelength;
490 tmpinlinf = (inlining_methodinfo*) list_first(inlinfo->inlinedmethods);
491 if (tmpinlinf != NULL) nextgp = tmpinlinf->startgp;
494 if ((opt_rt || opt_xta || opt_vta) && (pOpcodes == 2 || pOpcodes == 3)) {
495 printf("PARSE method name =");
496 utf_display(method->class->name);
498 method_display(method);
503 if (opt_rt || opt_xta) {
504 RT_jit_parse(method);
508 printf("VTA requested, but not yet implemented\n");
512 /* allocate instruction array and block index table */
514 /* 1 additional for end ipc and 3 for loop unrolling */
516 block_index = DMNEW(int, cumjcodelength + 4);
517 instructionstart = DMNEW(u1, cumjcodelength + 4);
518 memset(instructionstart,0,sizeof(u1) * (cumjcodelength + 4));
520 /* 1 additional for TRACEBUILTIN and 4 for MONITORENTER/EXIT */
521 /* additional MONITOREXITS are reached by branches which are 3 bytes */
523 iptr = instr = DMNEW(instruction, cumjcodelength + 5);
525 /* XXX zero fields in the instructions loop? */
526 memset(iptr,0,sizeof(instruction) * (cumjcodelength + 5));
528 /* initialize block_index table (unrolled four times) */
533 for (i = 0, ip = block_index; i <= cumjcodelength; i += 4, ip += 4) {
541 /* compute branch targets of exception table */
543 extable = DMNEW(xtable, exceptiontablelength + 1);
545 for (i = 0; i < method->exceptiontablelength; i++) {
547 p = extable[i].startpc = raw_extable[i].startpc;
548 if (useinlining) p = label_index[p];
552 p = extable[i].endpc = raw_extable[i].endpc;
553 if (useinlining) p = label_index[p];
555 if (p < cumjcodelength)
558 p = extable[i].handlerpc = raw_extable[i].handlerpc;
562 extable[i].catchtype = raw_extable[i].catchtype;
564 extable[i].next = NULL;
565 extable[i].down = &extable[i+1];
569 nextex = fillextable(extable, raw_extable, method->exceptiontablelength, label_index, &b_count);
571 s_count = 1 + exceptiontablelength; /* initialize stack element counter */
574 if (checksync && (method->flags & ACC_SYNCHRONIZED)) {
575 isleafmethod = false;
579 /* scan all java instructions */
581 for (p = 0, gp = 0; p < jcodelength; gp += (nextp - p), p = nextp) {
583 /* DEBUG XXX */ /*printf("p:%d gp:%d ",p,gp);*/
585 /* mark this position as a valid instruction start */
587 instructionstart[p] = 1;
590 if ((useinlining) && (gp == nextgp)) {
592 bool *readonly = NULL;
594 opcode = code_get_u1(p);
595 nextp = p += jcommandsize[opcode];
596 if (nextp > jcodelength)
597 panic("Unexpected end of bytecode");
598 tmpinlinf = list_first(inlinfo->inlinedmethods);
599 firstlocal = tmpinlinf->firstlocal;
600 label_index = tmpinlinf->label_index;
601 readonly = tmpinlinf->readonly;
603 for (i = 0, tptr = tmpinlinf->method->paramtypes + tmpinlinf->method->paramcount - 1; i < tmpinlinf->method->paramcount; i++, tptr--) {
606 if ((i == 0) && inlineparamopt) {
607 OP1(ICMD_CLEAR_ARGREN, firstlocal);
610 if (!inlineparamopt || !readonly[i]) {
614 op = ICMD_READONLY_ARG;
618 OP1(op, firstlocal + tmpinlinf->method->paramcount - 1 - i);
620 /* block_index[gp] |= (ipc << 1);*/ /*FIXME: necessary ? */
623 inlining_save_compiler_variables();
624 inlining_set_compiler_variables(tmpinlinf);
626 if (inlinfo->inlinedmethods == NULL) {
629 tmpinlinf = list_first(inlinfo->inlinedmethods);
630 nextgp = (tmpinlinf != NULL) ? tmpinlinf->startgp : -1;
632 if (method->exceptiontablelength > 0)
633 nextex = fillextable(nextex, method->exceptiontable, method->exceptiontablelength, label_index, &b_count);
637 opcode = code_get_u1(p); /* fetch op code */
640 if (opt_rt && (pOpcodes == 2 || pOpcodes == 3)) {
641 printf("Parse<%i> p=%i<%i< opcode=<%i> %s\n",
642 pOpcodes, p, rt_jcodelength, opcode, icmd_names[opcode]);
645 block_index[gp] |= (ipc << 1); /* store intermediate count */
648 block_insert(gp); /* start new block */
652 nextp = p + jcommandsize[opcode]; /* compute next instruction start */
653 if (nextp > jcodelength)
654 panic("Unexpected end of bytecode");
655 s_count += stackreq[opcode]; /* compute stack element count */
661 /* pushing constants onto the stack p */
664 LOADCONST_I(code_get_s1(p+1));
668 LOADCONST_I(code_get_s2(p+1));
672 i = code_get_u1(p+1);
673 goto pushconstantitem;
676 i = code_get_u2(p + 1);
680 if (i >= class->cpcount)
681 panic ("Attempt to access constant outside range");
683 switch (class->cptags[i]) {
684 case CONSTANT_Integer:
685 LOADCONST_I(((constant_integer*)
686 (class->cpinfos[i]))->value);
689 LOADCONST_L(((constant_long*)
690 (class->cpinfos[i]))->value);
693 LOADCONST_F(((constant_float*)
694 (class->cpinfos[i]))->value);
696 case CONSTANT_Double:
697 LOADCONST_D(((constant_double*)
698 (class->cpinfos[i]))->value);
700 case CONSTANT_String:
701 LOADCONST_A(literalstring_new((utf*)
702 (class->cpinfos[i])));
704 default: panic("Invalid constant type to push");
708 case JAVA_ACONST_NULL:
719 LOADCONST_I(opcode - JAVA_ICONST_0);
724 LOADCONST_L(opcode - JAVA_LCONST_0);
730 LOADCONST_F(opcode - JAVA_FCONST_0);
735 LOADCONST_D(opcode - JAVA_DCONST_0);
738 /* loading variables onto the stack */
746 i = code_get_u1(p + 1);
748 i = code_get_u2(p + 1);
752 OP1LOAD(opcode, i + firstlocal);
759 OP1LOAD(ICMD_ILOAD, opcode - JAVA_ILOAD_0 + firstlocal);
766 OP1LOAD(ICMD_LLOAD, opcode - JAVA_LLOAD_0 + firstlocal);
773 OP1LOAD(ICMD_FLOAD, opcode - JAVA_FLOAD_0 + firstlocal);
780 OP1LOAD(ICMD_DLOAD, opcode - JAVA_DLOAD_0 + firstlocal);
787 OP1LOAD(ICMD_ALOAD, opcode - JAVA_ALOAD_0 + firstlocal);
790 /* storing stack values into local variables */
798 i = code_get_u1(p + 1);
800 i = code_get_u2(p + 1);
804 OP1STORE(opcode, i + firstlocal);
811 OP1STORE(ICMD_ISTORE, opcode - JAVA_ISTORE_0 + firstlocal);
818 OP1STORE(ICMD_LSTORE, opcode - JAVA_LSTORE_0 + firstlocal);
825 OP1STORE(ICMD_FSTORE, opcode - JAVA_FSTORE_0 + firstlocal);
832 OP1STORE(ICMD_DSTORE, opcode - JAVA_DSTORE_0 + firstlocal);
839 OP1STORE(ICMD_ASTORE, opcode - JAVA_ASTORE_0 + firstlocal);
847 i = code_get_u1(p + 1);
848 v = code_get_s1(p + 2);
851 i = code_get_u2(p + 1);
852 v = code_get_s2(p + 3);
856 INDEX_ONEWORD(i + firstlocal);
857 OP2I(opcode, i + firstlocal, v);
861 /* wider index for loading, storing and incrementing */
868 /* managing arrays ************************************************/
871 OP2I(ICMD_CHECKASIZE, 0, 0);
872 switch (code_get_s1(p + 1)) {
874 BUILTIN1(BUILTIN_newarray_boolean, TYPE_ADR);
877 BUILTIN1(BUILTIN_newarray_char, TYPE_ADR);
880 BUILTIN1(BUILTIN_newarray_float, TYPE_ADR);
883 BUILTIN1(BUILTIN_newarray_double, TYPE_ADR);
886 BUILTIN1(BUILTIN_newarray_byte, TYPE_ADR);
889 BUILTIN1(BUILTIN_newarray_short, TYPE_ADR);
892 BUILTIN1(BUILTIN_newarray_int, TYPE_ADR);
895 BUILTIN1(BUILTIN_newarray_long, TYPE_ADR);
897 default: panic("Invalid array-type to create");
902 OP2I(ICMD_CHECKASIZE, 0, 0);
903 i = code_get_u2(p + 1);
905 classinfo *component = (classinfo*)class_getconstant(class, i, CONSTANT_Class);
906 LOADCONST_A(class_array_of(component)->vftbl);
910 BUILTIN2(BUILTIN_newarray, TYPE_ADR);
914 case JAVA_MULTIANEWARRAY:
916 i = code_get_u2(p + 1);
918 int v = code_get_u1(p + 3);
919 vftbl *arrayvftbl = ((classinfo*)class_getconstant (class, i, CONSTANT_Class))->vftbl;
920 OP2A(opcode, v, arrayvftbl);
942 i = p + code_get_s2(p + 1);
954 i = p + code_get_s4(p + 1);
967 i = code_get_u1(p + 1);
969 i = code_get_u2(p + 1);
976 if (isinlinedmethod) {
977 OP1(ICMD_GOTO, inlinfo->stopgp);
981 OP1LOAD(opcode, i + firstlocal);
990 if (isinlinedmethod) {
991 /* if (p==jcodelength-1) {*/ /* return is at end of inlined method */
996 OP1(ICMD_GOTO, inlinfo->stopgp);
1010 /* table jumps ********************************/
1012 case JAVA_LOOKUPSWITCH:
1019 nextp = ALIGN((p + 1), 4);
1020 if (nextp + 8 > jcodelength)
1021 panic("Unexpected end of bytecode");
1023 tablep = (s4*)(jcode + nextp);
1026 num = code_get_u4(nextp + 4);
1027 tablep = DMNEW(s4, num * 2 + 2);
1030 OP2A(opcode, 0, tablep);
1032 /* default target */
1034 j = p + code_get_s4(nextp);
1037 *tablep = j; /* restore for little endian */
1043 /* number of pairs */
1045 num = code_get_u4(nextp);
1050 if (nextp + 8*(num) > jcodelength)
1051 panic("Unexpected end of bytecode");
1053 for (i = 0; i < num; i++) {
1056 j = code_get_s4(nextp);
1057 *tablep = j; /* restore for little endian */
1061 /* check if the lookup table is sorted correctly */
1063 if (i && (j <= prevvalue))
1064 panic("invalid LOOKUPSWITCH: table not sorted");
1069 j = p + code_get_s4(nextp);
1072 *tablep = j; /* restore for little endian */
1083 case JAVA_TABLESWITCH:
1089 nextp = ALIGN((p + 1), 4);
1090 if (nextp + 12 > jcodelength)
1091 panic("Unexpected end of bytecode");
1093 tablep = (s4*)(jcode + nextp);
1096 num = code_get_u4(nextp + 8) - code_get_u4(nextp + 4);
1097 tablep = DMNEW(s4, num + 1 + 3);
1100 OP2A(opcode, 0, tablep);
1102 /* default target */
1104 j = p + code_get_s4(nextp);
1107 *tablep = j; /* restore for little endian */
1115 j = code_get_s4(nextp);
1116 *tablep = j; /* restore for little endian */
1122 num = code_get_s4(nextp);
1123 *tablep = num; /* restore for little endian */
1127 num -= j; /* difference of upper - lower */
1129 panic("invalid TABLESWITCH: upper bound < lower bound");
1131 if (nextp + 4*(num+1) > jcodelength)
1132 panic("Unexpected end of bytecode");
1134 for (i = 0; i <= num; i++) {
1135 j = p + code_get_s4(nextp);
1138 *tablep = j; /* restore for little endian */
1149 /* load and store of object fields *******************/
1152 BUILTIN3(BUILTIN_aastore, TYPE_VOID);
1155 case JAVA_PUTSTATIC:
1156 case JAVA_GETSTATIC:
1157 i = code_get_u2(p + 1);
1159 constant_FMIref *fr;
1161 fr = class_getconstant(class, i, CONSTANT_Fieldref);
1162 fi = class_findfield(fr->class, fr->name, fr->descriptor);
1163 OP2A(opcode, fi->type, fi);
1169 i = code_get_u2(p + 1);
1171 constant_FMIref *fr;
1173 fr = class_getconstant (class, i, CONSTANT_Fieldref);
1174 fi = class_findfield (fr->class, fr->name, fr->descriptor);
1175 OP2A(opcode, fi->type, fi);
1180 /* method invocation *****/
1182 case JAVA_INVOKESTATIC:
1183 i = code_get_u2(p + 1);
1185 constant_FMIref *mr;
1188 mr = class_getconstant (class, i, CONSTANT_Methodref);
1189 mi = class_fetchmethod (mr->class, mr->name, mr->descriptor);
1190 /*RTAprint*/ if (((pOpcodes == 2) || (pOpcodes == 3)) && opt_rt)
1191 /*RTAprint*/ {printf(" method name =");
1192 /*RTAprint*/ utf_display(mi->class->name); printf(".");
1193 /*RTAprint*/ utf_display(mi->name);printf("\tINVOKE STATIC\n");
1194 /*RTAprint*/ fflush(stdout);}
1195 if (!(mi->flags & ACC_STATIC))
1196 panic ("Static/Nonstatic mismatch calling static method");
1197 descriptor2types(mi);
1200 OP2A(opcode, mi->paramcount, mi);
1204 case JAVA_INVOKESPECIAL:
1205 case JAVA_INVOKEVIRTUAL:
1206 i = code_get_u2(p + 1);
1208 constant_FMIref *mr;
1211 mr = class_getconstant (class, i, CONSTANT_Methodref);
1212 mi = class_fetchmethod (mr->class, mr->name, mr->descriptor);
1213 /*RTAprint*/ if (((pOpcodes == 2) || (pOpcodes == 3)) && opt_rt)
1214 /*RTAprint*/ {printf(" method name =");
1216 /*RTAprint*/ utf_display(mi->class->name); printf(".");
1217 /*RTAprint*/ utf_display(mi->name);printf("\tINVOKE SPECIAL/VIRTUAL\n");
1218 /*RTAprint*/ fflush(stdout);}
1220 if (mi->flags & ACC_STATIC)
1221 panic ("Static/Nonstatic mismatch calling static method");
1222 descriptor2types(mi);
1224 OP2A(opcode, mi->paramcount, mi);
1228 case JAVA_INVOKEINTERFACE:
1229 i = code_get_u2(p + 1);
1231 constant_FMIref *mr;
1234 mr = class_getconstant (class, i, CONSTANT_InterfaceMethodref);
1235 mi = class_fetchmethod (mr->class, mr->name, mr->descriptor);
1236 if (mi->flags & ACC_STATIC)
1237 panic ("Static/Nonstatic mismatch calling static method");
1238 descriptor2types(mi);
1240 OP2A(opcode, mi->paramcount, mi);
1244 /* miscellaneous object operations *******/
1247 i = code_get_u2 (p+1);
1249 LOADCONST_A(class_getconstant(class, i, CONSTANT_Class));
1251 BUILTIN1(BUILTIN_new, TYPE_ADR);
1254 case JAVA_CHECKCAST:
1255 #if defined(USE_THREADS) && defined(NATIVE_THREADS)
1256 isleafmethod = false;
1258 i = code_get_u2(p+1);
1260 classinfo *cls = (classinfo*)class_getconstant(class, i, CONSTANT_Class);
1261 if (cls->vftbl->arraydesc) {
1262 /* array type cast-check */
1263 LOADCONST_A(cls->vftbl);
1265 BUILTIN2(BUILTIN_checkarraycast, TYPE_ADR);
1267 else { /* object type cast-check */
1269 + LOADCONST_A(class_getconstant(class, i, CONSTANT_Class));
1271 + BUILTIN2(BUILTIN_checkcast, TYPE_ADR);
1273 OP2A(opcode, 1, cls);
1279 case JAVA_INSTANCEOF:
1280 #if defined(USE_THREADS) && defined(NATIVE_THREADS)
1281 isleafmethod = false;
1283 i = code_get_u2(p+1);
1286 classinfo *cls = (classinfo*)class_getconstant(class, i, CONSTANT_Class);
1287 if (cls->vftbl->arraydesc) {
1288 /* array type cast-check */
1289 LOADCONST_A(cls->vftbl);
1291 BUILTIN2(BUILTIN_arrayinstanceof, TYPE_INT);
1293 else { /* object type cast-check */
1295 LOADCONST_A(class_getconstant(class, i, CONSTANT_Class));
1297 BUILTIN2(BUILTIN_instanceof, TYPE_INT);
1299 OP2A(opcode, 1, cls);
1304 case JAVA_MONITORENTER:
1307 BUILTIN1(BUILTIN_monitorenter, TYPE_VOID);
1311 OP(ICMD_NULLCHECKPOP);
1315 case JAVA_MONITOREXIT:
1318 BUILTIN1(BUILTIN_monitorexit, TYPE_VOID);
1327 /* any other basic operation **************************************/
1346 #if defined(__I386__)
1349 BUILTIN2(BUILTIN_frem, TYPE_FLOAT);
1354 #if defined(__I386__)
1357 BUILTIN2(BUILTIN_drem, TYPE_DOUBLE);
1362 #if defined(__ALPHA__)
1364 BUILTIN1(BUILTIN_f2i, TYPE_INT);
1373 #if defined(__ALPHA__)
1375 BUILTIN1(BUILTIN_f2l, TYPE_LONG);
1384 #if defined(__ALPHA__)
1386 BUILTIN1(BUILTIN_d2i, TYPE_INT);
1395 #if defined(__ALPHA__)
1397 BUILTIN1(BUILTIN_d2l, TYPE_LONG);
1405 case JAVA_BREAKPOINT:
1406 panic("Illegal opcode Breakpoint encountered");
1409 case 186: /* unused opcode */
1463 printf("Illegal opcode %d at instr %d\n", opcode, ipc);
1464 panic("Illegal opcode encountered");
1473 /* If WIDE was used correctly, iswide should have been reset by now. */
1474 if (iswide && opcode != JAVA_WIDE)
1475 panic("Illegal instruction: WIDE before incompatible opcode");
1479 if (isinlinedmethod && p == jcodelength - 1) { /* end of an inlined method */
1480 /* printf("setting gp from %d to %d\n",gp, inlinfo->stopgp); */
1481 gp = inlinfo->stopgp;
1482 inlining_restore_compiler_variables();
1483 list_remove(inlinfo->inlinedmethods, list_first(inlinfo->inlinedmethods));
1484 if (inlinfo->inlinedmethods == NULL) {
1487 tmpinlinf = list_first(inlinfo->inlinedmethods);
1488 nextgp = (tmpinlinf != NULL) ? tmpinlinf->startgp : -1;
1490 /* printf("nextpgp: %d\n", nextgp); */
1491 label_index=inlinfo->label_index;
1492 firstlocal = inlinfo->firstlocal;
1496 if (p != jcodelength)
1497 panic("Command-sequence crosses code-boundary"); /* XXX change message */
1500 panic("Code does not end with branch/return/athrow - stmt");
1502 /* adjust block count if target 0 is not first intermediate instruction */
1504 if (!block_index[0] || (block_index[0] > 1))
1507 /* copy local to global variables */
1510 block_count = b_count;
1511 stack_count = s_count + block_count * maxstack;
1513 /* allocate stack table */
1515 stack = DMNEW(stackelement, stack_count);
1520 bptr = block = DMNEW(basicblock, b_count + 1); /* one more for end ipc */
1525 /* additional block if target 0 is not first intermediate instruction */
1527 if (!block_index[0] || (block_index[0] > 1)) {
1528 bptr->iinstr = instr;
1531 bptr->type = BBTYPE_STD;
1532 bptr->branchrefs = NULL;
1533 bptr->pre_count = 0;
1534 bptr->debug_nr = c_debug_nr++;
1537 (bptr - 1)->next = bptr;
1540 /* allocate blocks */
1542 for (p = 0; p < cumjcodelength; p++) {
1543 if (block_index[p] & 1) {
1544 /* check if this block starts at the beginning of an instruction */
1545 if (!instructionstart[p])
1546 panic("Branch into middle of instruction");
1547 /* allocate the block */
1548 bptr->iinstr = instr + (block_index[p] >> 1);
1549 bptr->debug_nr = c_debug_nr++;
1551 (bptr - 1)->icount = bptr->iinstr - (bptr - 1)->iinstr;
1555 bptr->type = BBTYPE_STD;
1556 bptr->branchrefs = NULL;
1557 block_index[p] = b_count;
1558 bptr->pre_count = 0;
1561 (bptr - 1)->next = bptr;
1565 /* allocate additional block at end */
1567 bptr->instack = bptr->outstack = NULL;
1568 bptr->indepth = bptr->outdepth = 0;
1569 bptr->iinstr = NULL;
1570 (bptr - 1)->icount = (instr + instr_count) - (bptr - 1)->iinstr;
1575 bptr->type = BBTYPE_STD;
1576 bptr->branchrefs = NULL;
1577 bptr->pre_count = 0;
1578 bptr->debug_nr = c_debug_nr++;
1579 (bptr - 1)->next = bptr;
1584 if (exceptiontablelength > 0)
1585 extable[exceptiontablelength - 1].down = NULL;
1589 for (i = 0; i < exceptiontablelength; ++i) {
1590 p = extable[i].startpc;
1591 extable[i].start = block + block_index[p];
1593 p = extable[i].endpc;
1594 extable[i].end = block + block_index[p];
1596 p = extable[i].handlerpc;
1597 extable[i].handler = block + block_index[p];
1601 if (useinlining) inlining_cleanup();
1602 useinlining = useinltmp;
1607 * These are local overrides for various environment variables in Emacs.
1608 * Please do not remove this and leave it at the end of the file, where
1609 * Emacs will automagically detect them.
1610 * ---------------------------------------------------------------------
1613 * indent-tabs-mode: t