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 942 2004-03-06 17:32:16Z jowenn $
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,l) iptr->opc=(o);iptr->op1=(o1);iptr->val.a=(v);iptr->line=l;PINC
348 #define BUILTIN1(v,t,l) isleafmethod=false;iptr->opc=ICMD_BUILTIN1;iptr->op1=t;\
349 iptr->val.a=(v);iptr->line=l;PINC
350 #define BUILTIN2(v,t,l) isleafmethod=false;iptr->opc=ICMD_BUILTIN2;iptr->op1=t;\
351 iptr->val.a=(v);iptr->line=l;PINC
352 #define BUILTIN3(v,t,l) isleafmethod=false;iptr->opc=ICMD_BUILTIN3;iptr->op1=t;\
353 iptr->val.a=(v);iptr->line=l;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[p] = 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;
627 opcode = code_get_u1(p);
628 nextp = p += jcommandsize[opcode];
629 if (nextp > jcodelength)
630 panic("Unexpected end of bytecode");
631 tmpinlinf = list_first(inlinfo->inlinedmethods);
632 firstlocal = tmpinlinf->firstlocal;
633 label_index = tmpinlinf->label_index;
634 readonly = tmpinlinf->readonly;
636 for (i = 0, tptr = tmpinlinf->method->paramtypes + tmpinlinf->method->paramcount - 1; i < tmpinlinf->method->paramcount; i++, tptr--) {
639 if ((i == 0) && inlineparamopt) {
640 OP1(ICMD_CLEAR_ARGREN, firstlocal);
643 if (!inlineparamopt || !readonly[i]) {
647 op = ICMD_READONLY_ARG;
651 OP1(op, firstlocal + tmpinlinf->method->paramcount - 1 - i);
653 /* block_index[gp] |= (ipc << 1);*/ /*FIXME: necessary ? */
656 inlining_save_compiler_variables();
657 inlining_set_compiler_variables(tmpinlinf);
659 if (inlinfo->inlinedmethods == NULL) {
662 tmpinlinf = list_first(inlinfo->inlinedmethods);
663 nextgp = (tmpinlinf != NULL) ? tmpinlinf->startgp : -1;
665 if (method->exceptiontablelength > 0)
666 nextex = fillextable(nextex, method->exceptiontable, method->exceptiontablelength, label_index, &b_count);
670 opcode = code_get_u1(p); /* fetch op code */
673 if (opt_rt && (pOpcodes == 2 || pOpcodes == 3)) {
674 printf("Parse<%i> p=%i<%i< opcode=<%i> %s\n",
675 pOpcodes, p, rt_jcodelength, opcode, icmd_names[opcode]);
678 block_index[gp] |= (ipc << 1); /* store intermediate count */
681 block_insert(gp); /* start new block */
685 nextp = p + jcommandsize[opcode]; /* compute next instruction start */
686 if (nextp > jcodelength)
687 panic("Unexpected end of bytecode");
688 s_count += stackreq[opcode]; /* compute stack element count */
694 /* pushing constants onto the stack p */
697 LOADCONST_I(code_get_s1(p+1));
701 LOADCONST_I(code_get_s2(p+1));
705 i = code_get_u1(p+1);
706 goto pushconstantitem;
709 i = code_get_u2(p + 1);
713 if (i >= class->cpcount)
714 panic ("Attempt to access constant outside range");
716 switch (class->cptags[i]) {
717 case CONSTANT_Integer:
718 LOADCONST_I(((constant_integer*)
719 (class->cpinfos[i]))->value);
722 LOADCONST_L(((constant_long*)
723 (class->cpinfos[i]))->value);
726 LOADCONST_F(((constant_float*)
727 (class->cpinfos[i]))->value);
729 case CONSTANT_Double:
730 LOADCONST_D(((constant_double*)
731 (class->cpinfos[i]))->value);
733 case CONSTANT_String:
734 LOADCONST_A(literalstring_new((utf*)
735 (class->cpinfos[i])));
737 default: panic("Invalid constant type to push");
741 case JAVA_ACONST_NULL:
752 LOADCONST_I(opcode - JAVA_ICONST_0);
757 LOADCONST_L(opcode - JAVA_LCONST_0);
763 LOADCONST_F(opcode - JAVA_FCONST_0);
768 LOADCONST_D(opcode - JAVA_DCONST_0);
771 /* loading variables onto the stack */
779 i = code_get_u1(p + 1);
781 i = code_get_u2(p + 1);
785 OP1LOAD(opcode, i + firstlocal);
792 OP1LOAD(ICMD_ILOAD, opcode - JAVA_ILOAD_0 + firstlocal);
799 OP1LOAD(ICMD_LLOAD, opcode - JAVA_LLOAD_0 + firstlocal);
806 OP1LOAD(ICMD_FLOAD, opcode - JAVA_FLOAD_0 + firstlocal);
813 OP1LOAD(ICMD_DLOAD, opcode - JAVA_DLOAD_0 + firstlocal);
820 OP1LOAD(ICMD_ALOAD, opcode - JAVA_ALOAD_0 + firstlocal);
823 /* storing stack values into local variables */
831 i = code_get_u1(p + 1);
833 i = code_get_u2(p + 1);
837 OP1STORE(opcode, i + firstlocal);
844 OP1STORE(ICMD_ISTORE, opcode - JAVA_ISTORE_0 + firstlocal);
851 OP1STORE(ICMD_LSTORE, opcode - JAVA_LSTORE_0 + firstlocal);
858 OP1STORE(ICMD_FSTORE, opcode - JAVA_FSTORE_0 + firstlocal);
865 OP1STORE(ICMD_DSTORE, opcode - JAVA_DSTORE_0 + firstlocal);
872 OP1STORE(ICMD_ASTORE, opcode - JAVA_ASTORE_0 + firstlocal);
880 i = code_get_u1(p + 1);
881 v = code_get_s1(p + 2);
884 i = code_get_u2(p + 1);
885 v = code_get_s2(p + 3);
889 INDEX_ONEWORD(i + firstlocal);
890 OP2I(opcode, i + firstlocal, v);
894 /* wider index for loading, storing and incrementing */
901 /* managing arrays ************************************************/
904 OP2I(ICMD_CHECKASIZE, 0, 0);
905 switch (code_get_s1(p + 1)) {
907 BUILTIN1(BUILTIN_newarray_boolean, TYPE_ADR,currentline);
910 BUILTIN1(BUILTIN_newarray_char, TYPE_ADR,currentline);
913 BUILTIN1(BUILTIN_newarray_float, TYPE_ADR,currentline);
916 BUILTIN1(BUILTIN_newarray_double, TYPE_ADR,currentline);
919 BUILTIN1(BUILTIN_newarray_byte, TYPE_ADR,currentline);
922 BUILTIN1(BUILTIN_newarray_short, TYPE_ADR,currentline);
925 BUILTIN1(BUILTIN_newarray_int, TYPE_ADR,currentline);
928 BUILTIN1(BUILTIN_newarray_long, TYPE_ADR,currentline);
930 default: panic("Invalid array-type to create");
935 OP2I(ICMD_CHECKASIZE, 0, 0);
936 i = code_get_u2(p + 1);
938 classinfo *component = (classinfo*)class_getconstant(class, i, CONSTANT_Class);
939 LOADCONST_A_BUILTIN(class_array_of(component)->vftbl);
943 BUILTIN2(BUILTIN_newarray, TYPE_ADR,currentline);
947 case JAVA_MULTIANEWARRAY:
949 i = code_get_u2(p + 1);
951 int v = code_get_u1(p + 3);
952 vftbl *arrayvftbl = ((classinfo*)class_getconstant (class, i, CONSTANT_Class))->vftbl;
953 OP2A(opcode, v, arrayvftbl,currentline);
975 i = p + code_get_s2(p + 1);
987 i = p + code_get_s4(p + 1);
1000 i = code_get_u1(p + 1);
1002 i = code_get_u2(p + 1);
1009 if (isinlinedmethod) {
1010 OP1(ICMD_GOTO, inlinfo->stopgp);
1014 OP1LOAD(opcode, i + firstlocal);
1023 if (isinlinedmethod) {
1024 /* if (p==jcodelength-1) {*/ /* return is at end of inlined method */
1029 OP1(ICMD_GOTO, inlinfo->stopgp);
1043 /* table jumps ********************************/
1045 case JAVA_LOOKUPSWITCH:
1052 nextp = ALIGN((p + 1), 4);
1053 if (nextp + 8 > jcodelength)
1054 panic("Unexpected end of bytecode");
1056 tablep = (s4*)(jcode + nextp);
1059 num = code_get_u4(nextp + 4);
1060 tablep = DMNEW(s4, num * 2 + 2);
1063 OP2A(opcode, 0, tablep,currentline);
1065 /* default target */
1067 j = p + code_get_s4(nextp);
1070 *tablep = j; /* restore for little endian */
1076 /* number of pairs */
1078 num = code_get_u4(nextp);
1083 if (nextp + 8*(num) > jcodelength)
1084 panic("Unexpected end of bytecode");
1086 for (i = 0; i < num; i++) {
1089 j = code_get_s4(nextp);
1090 *tablep = j; /* restore for little endian */
1094 /* check if the lookup table is sorted correctly */
1096 if (i && (j <= prevvalue))
1097 panic("invalid LOOKUPSWITCH: table not sorted");
1102 j = p + code_get_s4(nextp);
1105 *tablep = j; /* restore for little endian */
1116 case JAVA_TABLESWITCH:
1122 nextp = ALIGN((p + 1), 4);
1123 if (nextp + 12 > jcodelength)
1124 panic("Unexpected end of bytecode");
1126 tablep = (s4*)(jcode + nextp);
1129 num = code_get_u4(nextp + 8) - code_get_u4(nextp + 4);
1130 tablep = DMNEW(s4, num + 1 + 3);
1133 OP2A(opcode, 0, tablep,currentline);
1135 /* default target */
1137 j = p + code_get_s4(nextp);
1140 *tablep = j; /* restore for little endian */
1148 j = code_get_s4(nextp);
1149 *tablep = j; /* restore for little endian */
1155 num = code_get_s4(nextp);
1156 *tablep = num; /* restore for little endian */
1160 num -= j; /* difference of upper - lower */
1162 panic("invalid TABLESWITCH: upper bound < lower bound");
1164 if (nextp + 4*(num+1) > jcodelength)
1165 panic("Unexpected end of bytecode");
1167 for (i = 0; i <= num; i++) {
1168 j = p + code_get_s4(nextp);
1171 *tablep = j; /* restore for little endian */
1182 /* load and store of object fields *******************/
1185 BUILTIN3(BUILTIN_aastore, TYPE_VOID,currentline);
1188 case JAVA_PUTSTATIC:
1189 case JAVA_GETSTATIC:
1190 i = code_get_u2(p + 1);
1192 constant_FMIref *fr;
1194 fr = class_getconstant(class, i, CONSTANT_Fieldref);
1195 fi = class_findfield(fr->class, fr->name, fr->descriptor);
1196 OP2A(opcode, fi->type, fi,currentline);
1197 if (!fi->class->initialized) {
1198 isleafmethod = false;
1205 i = code_get_u2(p + 1);
1207 constant_FMIref *fr;
1209 fr = class_getconstant (class, i, CONSTANT_Fieldref);
1210 fi = class_findfield (fr->class, fr->name, fr->descriptor);
1211 OP2A(opcode, fi->type, fi,currentline);
1216 /* method invocation *****/
1218 case JAVA_INVOKESTATIC:
1219 i = code_get_u2(p + 1);
1221 constant_FMIref *mr;
1224 mr = class_getconstant (class, i, CONSTANT_Methodref);
1225 mi = class_fetchmethod (mr->class, mr->name, mr->descriptor);
1226 /*RTAprint*/ if (((pOpcodes == 2) || (pOpcodes == 3)) && opt_rt)
1227 /*RTAprint*/ {printf(" method name =");
1228 /*RTAprint*/ utf_display(mi->class->name); printf(".");
1229 /*RTAprint*/ utf_display(mi->name);printf("\tINVOKE STATIC\n");
1230 /*RTAprint*/ fflush(stdout);}
1231 if (!(mi->flags & ACC_STATIC))
1232 panic ("Static/Nonstatic mismatch calling static method");
1233 descriptor2types(mi);
1236 OP2A(opcode, mi->paramcount, mi,currentline);
1240 case JAVA_INVOKESPECIAL:
1241 case JAVA_INVOKEVIRTUAL:
1242 i = code_get_u2(p + 1);
1244 constant_FMIref *mr;
1247 mr = class_getconstant (class, i, CONSTANT_Methodref);
1248 mi = class_fetchmethod (mr->class, mr->name, mr->descriptor);
1249 /*RTAprint*/ if (((pOpcodes == 2) || (pOpcodes == 3)) && opt_rt)
1250 /*RTAprint*/ {printf(" method name =");
1252 /*RTAprint*/ utf_display(mi->class->name); printf(".");
1253 /*RTAprint*/ utf_display(mi->name);printf("\tINVOKE SPECIAL/VIRTUAL\n");
1254 /*RTAprint*/ fflush(stdout);}
1256 if (mi->flags & ACC_STATIC)
1257 panic ("Static/Nonstatic mismatch calling static method");
1258 descriptor2types(mi);
1260 OP2A(opcode, mi->paramcount, mi,currentline);
1264 case JAVA_INVOKEINTERFACE:
1265 i = code_get_u2(p + 1);
1267 constant_FMIref *mr;
1270 mr = class_getconstant (class, i, CONSTANT_InterfaceMethodref);
1271 mi = class_fetchmethod (mr->class, mr->name, mr->descriptor);
1272 if (mi->flags & ACC_STATIC)
1273 panic ("Static/Nonstatic mismatch calling static method");
1274 descriptor2types(mi);
1276 OP2A(opcode, mi->paramcount, mi,currentline);
1280 /* miscellaneous object operations *******/
1283 i = code_get_u2 (p+1);
1285 LOADCONST_A_BUILTIN(class_getconstant(class, i, CONSTANT_Class));
1287 BUILTIN1(BUILTIN_new, TYPE_ADR,currentline);
1290 case JAVA_CHECKCAST:
1291 #if defined(USE_THREADS) && defined(NATIVE_THREADS)
1292 isleafmethod = false;
1294 i = code_get_u2(p+1);
1296 classinfo *cls = (classinfo*)class_getconstant(class, i, CONSTANT_Class);
1297 if (cls->vftbl->arraydesc) {
1298 /* array type cast-check */
1299 LOADCONST_A_BUILTIN(cls->vftbl);
1301 BUILTIN2(BUILTIN_checkarraycast, TYPE_ADR,currentline);
1303 else { /* object type cast-check */
1305 + LOADCONST_A_BUILTIN(class_getconstant(class, i, CONSTANT_Class));
1307 + BUILTIN2(BUILTIN_checkcast, TYPE_ADR,currentline);
1309 OP2A(opcode, 1, cls,currentline);
1315 case JAVA_INSTANCEOF:
1316 #if defined(USE_THREADS) && defined(NATIVE_THREADS)
1317 isleafmethod = false;
1319 i = code_get_u2(p+1);
1322 classinfo *cls = (classinfo*)class_getconstant(class, i, CONSTANT_Class);
1323 if (cls->vftbl->arraydesc) {
1324 /* array type cast-check */
1325 LOADCONST_A_BUILTIN(cls->vftbl);
1327 BUILTIN2(BUILTIN_arrayinstanceof, TYPE_INT,currentline);
1329 else { /* object type cast-check */
1331 LOADCONST_A_BUILTIN(class_getconstant(class, i, CONSTANT_Class));
1333 BUILTIN2(BUILTIN_instanceof, TYPE_INT,currentline);
1335 OP2A(opcode, 1, cls,currentline);
1340 case JAVA_MONITORENTER:
1343 BUILTIN1(BUILTIN_monitorenter, TYPE_VOID,currentline);
1347 OP(ICMD_NULLCHECKPOP);
1351 case JAVA_MONITOREXIT:
1354 BUILTIN1(BUILTIN_monitorexit, TYPE_VOID,currentline);
1363 /* any other basic operation **************************************/
1382 #if defined(__I386__)
1385 BUILTIN2(BUILTIN_frem, TYPE_FLOAT,currentline);
1390 #if defined(__I386__)
1393 BUILTIN2(BUILTIN_drem, TYPE_DOUBLE,currentline);
1398 #if defined(__ALPHA__)
1400 BUILTIN1(BUILTIN_f2i, TYPE_INT,currentline);
1409 #if defined(__ALPHA__)
1411 BUILTIN1(BUILTIN_f2l, TYPE_LONG,currentline);
1420 #if defined(__ALPHA__)
1422 BUILTIN1(BUILTIN_d2i, TYPE_INT,currentline);
1431 #if defined(__ALPHA__)
1433 BUILTIN1(BUILTIN_d2l, TYPE_LONG,currentline);
1441 case JAVA_BREAKPOINT:
1442 panic("Illegal opcode Breakpoint encountered");
1445 case 186: /* unused opcode */
1499 printf("Illegal opcode %d at instr %d\n", opcode, ipc);
1500 panic("Illegal opcode encountered");
1509 /* If WIDE was used correctly, iswide should have been reset by now. */
1510 if (iswide && opcode != JAVA_WIDE)
1511 panic("Illegal instruction: WIDE before incompatible opcode");
1515 if (isinlinedmethod && p == jcodelength - 1) { /* end of an inlined method */
1516 /* printf("setting gp from %d to %d\n",gp, inlinfo->stopgp); */
1517 gp = inlinfo->stopgp;
1518 inlining_restore_compiler_variables();
1519 list_remove(inlinfo->inlinedmethods, list_first(inlinfo->inlinedmethods));
1520 if (inlinfo->inlinedmethods == NULL) {
1523 tmpinlinf = list_first(inlinfo->inlinedmethods);
1524 nextgp = (tmpinlinf != NULL) ? tmpinlinf->startgp : -1;
1526 /* printf("nextpgp: %d\n", nextgp); */
1527 label_index=inlinfo->label_index;
1528 firstlocal = inlinfo->firstlocal;
1532 if (p != jcodelength)
1533 panic("Command-sequence crosses code-boundary");
1536 panic("Code does not end with branch/return/athrow - stmt");
1538 /* adjust block count if target 0 is not first intermediate instruction */
1540 if (!block_index[0] || (block_index[0] > 1))
1543 /* copy local to global variables */
1546 block_count = b_count;
1547 stack_count = s_count + block_count * maxstack;
1549 /* allocate stack table */
1551 stack = DMNEW(stackelement, stack_count);
1556 bptr = block = DMNEW(basicblock, b_count + 1); /* one more for end ipc */
1561 /* additional block if target 0 is not first intermediate instruction */
1563 if (!block_index[0] || (block_index[0] > 1)) {
1564 bptr->iinstr = instr;
1567 bptr->type = BBTYPE_STD;
1568 bptr->branchrefs = NULL;
1569 bptr->pre_count = 0;
1570 bptr->debug_nr = c_debug_nr++;
1573 (bptr - 1)->next = bptr;
1576 /* allocate blocks */
1578 for (p = 0; p < cumjcodelength; p++) {
1579 if (block_index[p] & 1) {
1580 /* check if this block starts at the beginning of an instruction */
1581 if (!instructionstart[p])
1582 panic("Branch into middle of instruction");
1583 /* allocate the block */
1584 bptr->iinstr = instr + (block_index[p] >> 1);
1585 bptr->debug_nr = c_debug_nr++;
1587 (bptr - 1)->icount = bptr->iinstr - (bptr - 1)->iinstr;
1591 bptr->type = BBTYPE_STD;
1592 bptr->branchrefs = NULL;
1593 block_index[p] = b_count;
1594 bptr->pre_count = 0;
1597 (bptr - 1)->next = bptr;
1601 /* allocate additional block at end */
1603 bptr->instack = bptr->outstack = NULL;
1604 bptr->indepth = bptr->outdepth = 0;
1605 bptr->iinstr = NULL;
1606 (bptr - 1)->icount = (instr + instr_count) - (bptr - 1)->iinstr;
1611 bptr->type = BBTYPE_STD;
1612 bptr->branchrefs = NULL;
1613 bptr->pre_count = 0;
1614 bptr->debug_nr = c_debug_nr++;
1615 (bptr - 1)->next = bptr;
1620 if (exceptiontablelength > 0)
1621 extable[exceptiontablelength - 1].down = NULL;
1625 for (i = 0; i < exceptiontablelength; ++i) {
1626 p = extable[i].startpc;
1627 extable[i].start = block + block_index[p];
1629 p = extable[i].endpc;
1630 extable[i].end = (p == cumjcodelength) ? last_block : (block + block_index[p]);
1632 p = extable[i].handlerpc;
1633 extable[i].handler = block + block_index[p];
1637 if (useinlining) inlining_cleanup();
1638 useinlining = useinltmp;
1643 * These are local overrides for various environment variables in Emacs.
1644 * Please do not remove this and leave it at the end of the file, where
1645 * Emacs will automagically detect them.
1646 * ---------------------------------------------------------------------
1649 * indent-tabs-mode: t