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 730 2003-12-11 21:23:31Z edwin $
43 #include "loop/loop.h"
49 #include "toolbox/memory.h"
50 #include "toolbox/loging.h"
53 /* data about the currently parsed method */
55 classinfo *rt_class; /* class the compiled method belongs to */
56 methodinfo *rt_method; /* pointer to method info of compiled method */
57 utf *rt_descriptor; /* type descriptor of compiled method */
58 int rt_jcodelength; /* length of JavaVM-codes */
59 u1 *rt_jcode; /* pointer to start of JavaVM-code */
65 /*#define debug_writebranch printf("op: %s i: %d label_index[i]: %d\n",icmd_names[opcode], i, label_index[i]);*/
66 #define debug_writebranch
70 /* functionc compiler_addinitclass *********************************************
72 add class into the list of classes to initialize
74 *******************************************************************************/
76 void compiler_addinitclass(classinfo *c)
80 if (c->initialized) return;
82 cl = chain_first(uninitializedclasses);
87 cl = chain_next(uninitializedclasses);
93 char logtext[MAXLOGTEXT];
94 sprintf(logtext, "compiler_addinitclass: ");
95 utf_sprint(logtext+strlen(logtext), c->name);
98 chain_addlast(uninitializedclasses, c);
103 char logtext[MAXLOGTEXT];
104 sprintf(logtext, "compiler_addinitclass: ");
105 utf_sprint(logtext+strlen(logtext), c->name);
108 chain_addbefore(uninitializedclasses, c);
111 cl = chain_next(uninitializedclasses);
117 /* function descriptor2typesL ***************************************************
119 decodes a already checked method descriptor. The parameter count, the
120 return type and the argument types are stored in the passed methodinfo.
121 gets and saves classptr for object ref.s
123 *******************************************************************************/
125 classSetNode *descriptor2typesL(methodinfo *m)
132 classinfo** classtypes;
135 classSetNode *p=NULL;
136 if (debugInfo >= 1) {
137 printf("In descriptor2typesL >>>\t"); fflush(stdout);
138 utf_display(m->class->name); printf(".");
139 method_display(m);fflush(stdout);
143 desc = MNEW (char, 256);
144 types = DMNEW (u1, m->descriptor->blength);
145 classtypes = MNEW (classinfo*, m->descriptor->blength+1);
146 m->returnclass = NULL;
148 if (!(m->flags & ACC_STATIC)) {
150 if (debugInfo >= 1) {
151 printf("param #0 (this?) method class =");utf_display(m->class->name);printf("\n");
153 classtypes[pcount] = m->class;
154 p = addClassCone(p, m->class);
158 utf_ptr = m->descriptor->text + 1;
159 strcpy (desc,utf_ptr);
161 while ((c = *desc++) != ')') {
168 case 'Z': *tptr++ = TYPE_INT;
170 case 'J': *tptr++ = TYPE_LNG;
172 case 'F': *tptr++ = TYPE_FLT;
174 case 'D': *tptr++ = TYPE_DBL;
176 case 'L': *tptr++ = TYPE_ADR;
177 /* get class string */
178 class = strtok(desc,";");
179 desc = strtok(NULL,"\0");
180 /* get/save classinfo ptr */
181 classtypes[pcount-1] = class_get(utf_new_char(class));
182 p = addClassCone(p, class_get(utf_new_char(class)));
183 if (debugInfo >= 1) {
184 printf("LParam#%i 's class type is: %s\n",pcount-1,class);fflush(stdout);
185 printf("Lclasstypes[%i]=",pcount-1);fflush(stdout);
186 utf_display(classtypes[pcount-1]->name);
189 case '[': *tptr++ = TYPE_ADR;
192 /* get class string */
194 class = strtok(desc,";");
195 desc = strtok(NULL,"\0");
196 /* get/save classinfo ptr */
197 classtypes[pcount-1] = class_get(utf_new_char(class));
198 p= addClassCone(p, class_get(utf_new_char(class)));
199 if (debugInfo >= 1) {
200 printf("[Param#%i 's class type is: %s\n",pcount-1,class);
201 printf("[classtypes[%i]=",pcount-1);fflush(stdout);
202 utf_display(classtypes[pcount-1]->name);
207 classtypes[pcount-1] = NULL;
210 panic("Ill formed methodtype-descriptor");
214 /* compute return type */
220 case 'Z': m->returntype = TYPE_INT;
222 case 'J': m->returntype = TYPE_LNG;
224 case 'F': m->returntype = TYPE_FLT;
226 case 'D': m->returntype = TYPE_DBL;
229 m->returntype = TYPE_ADR;
237 m->returntype = TYPE_ADR;
239 /* get class string */
240 class = strtok(desc,";");
241 m->returnclass = class_get(utf_new_char(class));
242 if (m->returnclass == NULL) {
243 printf("class=%s :\t",class);
244 panic ("return class not found");
247 case 'V': m->returntype = TYPE_VOID;
250 default: panic("Ill formed methodtype-descriptor-ReturnType");
253 m->paramcount = pcount;
254 m->paramtypes = types;
255 m->paramclass = classtypes;
259 for (i=0; i< m->paramcount; i++) {
260 if ((m->paramtypes[i] == TYPE_ADR) && (m->paramclass[i] != NULL)) {
261 printf("Param #%i is:\t",i);
262 utf_display(m->paramclass[i]->name);
267 if ((m->returntype == TYPE_ADR) && (m->returnclass != NULL)) {
268 printf("\tReturn Type is:\t"); fflush(stdout);
269 utf_display(m->returnclass->name);
273 printf("params2types: START results in a set \n");
274 printf("param2types: A Set size=%i=\n",sizeOfSet(p));
283 /* function descriptor2types ***************************************************
285 decodes a already checked method descriptor. The parameter count, the
286 return type and the argument types are stored in the passed methodinfo.
288 *******************************************************************************/
290 void descriptor2types(methodinfo *m)
296 types = DMNEW(u1, m->descriptor->blength);
299 if (!(m->flags & ACC_STATIC)) {
304 utf_ptr = m->descriptor->text + 1;
306 while ((c = *utf_ptr++) != ')') {
313 case 'Z': *tptr++ = TYPE_INT;
315 case 'J': *tptr++ = TYPE_LNG;
317 case 'F': *tptr++ = TYPE_FLT;
319 case 'D': *tptr++ = TYPE_DBL;
321 case 'L': *tptr++ = TYPE_ADR;
322 while (*utf_ptr++ != ';');
324 case '[': *tptr++ = TYPE_ADR;
328 while (*utf_ptr++ != ';') /* skip */;
330 default: panic ("Ill formed methodtype-descriptor");
334 /* compute return type */
336 switch (*utf_ptr++) {
341 case 'Z': m->returntype = TYPE_INT;
343 case 'J': m->returntype = TYPE_LNG;
345 case 'F': m->returntype = TYPE_FLT;
347 case 'D': m->returntype = TYPE_DBL;
350 case 'L': m->returntype = TYPE_ADR;
352 case 'V': m->returntype = TYPE_VOID;
355 default: panic ("Ill formed methodtype-descriptor");
358 m->paramcount = pcount;
359 m->paramtypes = types;
364 /*******************************************************************************
366 function 'parse' scans the JavaVM code and generates intermediate code
368 During parsing the block index table is used to store at bit pos 0
369 a flag which marks basic block starts and at position 1 to 31 the
370 intermediate instruction index. After parsing the block index table
371 is scanned, for marked positions a block is generated and the block
372 number is stored in the block index table.
374 *******************************************************************************/
376 /* intermediate code generating macros */
378 #define PINC iptr++;ipc++
379 #define LOADCONST_I(v) iptr->opc=ICMD_ICONST;iptr->op1=0;iptr->val.i=(v);PINC
380 #define LOADCONST_L(v) iptr->opc=ICMD_LCONST;iptr->op1=0;iptr->val.l=(v);PINC
381 #define LOADCONST_F(v) iptr->opc=ICMD_FCONST;iptr->op1=0;iptr->val.f=(v);PINC
382 #define LOADCONST_D(v) iptr->opc=ICMD_DCONST;iptr->op1=0;iptr->val.d=(v);PINC
383 #define LOADCONST_A(v) iptr->opc=ICMD_ACONST;iptr->op1=0;iptr->val.a=(v);PINC
384 #define OP(o) iptr->opc=(o);iptr->op1=0;iptr->val.l=0;PINC
385 #define OP1(o,o1) iptr->opc=(o);iptr->op1=(o1);iptr->val.l=(0);PINC
386 #define OP2I(o,o1,v) iptr->opc=(o);iptr->op1=(o1);iptr->val.i=(v);PINC
387 #define OP2A(o,o1,v) iptr->opc=(o);iptr->op1=(o1);iptr->val.a=(v);PINC
388 #define BUILTIN1(v,t) isleafmethod=false;iptr->opc=ICMD_BUILTIN1;iptr->op1=t;\
390 #define BUILTIN2(v,t) isleafmethod=false;iptr->opc=ICMD_BUILTIN2;iptr->op1=t;\
392 #define BUILTIN3(v,t) isleafmethod=false;iptr->opc=ICMD_BUILTIN3;iptr->op1=t;\
395 #define INDEX_ONEWORD(num) \
396 do { if((num)<0 || (num)>=maxlocals) \
397 panic("Invalid local variable index"); } while (0)
398 #define INDEX_TWOWORD(num) \
399 do { if((num)<0 || ((num)+1)>=maxlocals) \
400 panic("Invalid local variable index"); } while (0)
402 #define OP1LOAD(o,o1) \
403 do {if (o == ICMD_LLOAD || o == ICMD_DLOAD) \
409 #define OP1STORE(o,o1) \
410 do {if (o == ICMD_LSTORE || o == ICMD_DSTORE) \
416 /* block generating and checking macros */
418 #define block_insert(i) \
420 if (!(block_index[(i)] & 1)) { \
422 block_index[(i)] |= 1; \
427 /* FIXME really use cumjcodelength for the bound_checkers ? */
429 #define bound_check(i) \
431 if (i < 0 || i >= cumjcodelength) { \
432 panic("branch target out of code-boundary"); \
436 /* bound_check1 is used for the inclusive ends of exception handler ranges */
437 #define bound_check1(i) \
439 if (i < 0 || i > cumjcodelength) { \
440 panic("branch target out of code-boundary"); \
446 static xtable* fillextable(xtable* extable, exceptiontable *raw_extable, int exceptiontablelength, int *label_index, int *block_count)
450 if (exceptiontablelength == 0)
453 b_count = *block_count;
454 for (i = 0; i < exceptiontablelength; i++) {
456 p = raw_extable[i].startpc;
457 if (label_index != NULL) p = label_index[p];
458 extable[i].startpc = p;
462 p = raw_extable[i].endpc;
463 if (label_index != NULL) p = label_index[p];
464 extable[i].endpc = p;
466 if (p < cumjcodelength)
469 p = raw_extable[i].handlerpc;
470 if (label_index != NULL) p = label_index[p];
471 extable[i].handlerpc = p;
475 extable[i].catchtype = raw_extable[i].catchtype;
477 extable[i].next = NULL;
478 extable[i].down = &extable[i+1];
480 *block_count = b_count;
481 return &extable[i]; /* return the next free xtable* */
488 int p; /* java instruction counter */
489 int nextp; /* start of next java instruction */
490 int opcode; /* java opcode */
491 int i; /* temporary for different uses (counters) */
492 int ipc = 0; /* intermediate instruction counter */
493 int b_count = 0; /* basic block counter */
494 int s_count = 0; /* stack element counter */
495 bool blockend = false; /* true if basic block end has been reached */
496 bool iswide = false; /* true if last instruction was a wide */
497 instruction *iptr; /* current pointer into instruction array */
498 int gp; /* global java instruction counter */
499 /* inlining info for current method */
500 inlining_methodinfo *inlinfo = inlining_rootinfo;
501 inlining_methodinfo *tmpinlinf;
502 int nextgp = -1; /* start of next method to be inlined */
503 int *label_index = NULL; /* label redirection table */
504 int firstlocal = 0; /* first local variable of method */
505 xtable* nextex; /* points next free entry in extable */
506 u1 *instructionstart; /* 1 for pcs which are valid instr. starts */
510 if (compileverbose) {
511 char logtext[MAXLOGTEXT];
512 sprintf(logtext, "Parsing: ");
513 utf_sprint(logtext+strlen(logtext), method->class->name);
514 strcpy(logtext+strlen(logtext), ".");
515 utf_sprint(logtext+strlen(logtext), method->name);
516 utf_sprint(logtext+strlen(logtext), method->descriptor);
522 label_index = inlinfo->label_index;
523 maxstack = cummaxstack;
524 exceptiontablelength = cumextablelength;
527 useinltmp = useinlining; /* FIXME remove this after debugging */
528 /*useinlining = false;*/ /* and merge the if-statements */
531 cumjcodelength = jcodelength;
534 tmpinlinf = (inlining_methodinfo*) list_first(inlinfo->inlinedmethods);
535 if (tmpinlinf != NULL) nextgp = tmpinlinf->startgp;
538 if ((opt_rt || opt_xta || opt_vta) && (pOpcodes == 2 || pOpcodes == 3)) {
539 printf("PARSE method name =");
540 utf_display(method->class->name);
542 method_display(method);
547 if (opt_rt || opt_xta) {
548 RT_jit_parse(method);
552 printf("VTA requested, but not yet implemented\n");
556 /* allocate instruction array and block index table */
558 /* 1 additional for end ipc and 3 for loop unrolling */
560 block_index = DMNEW(int, cumjcodelength + 4);
561 instructionstart = DMNEW(u1, cumjcodelength + 4);
562 memset(instructionstart,0,sizeof(u1) * (cumjcodelength + 4));
564 /* 1 additional for TRACEBUILTIN and 4 for MONITORENTER/EXIT */
565 /* additional MONITOREXITS are reached by branches which are 3 bytes */
567 iptr = instr = DMNEW(instruction, cumjcodelength + 5);
569 /* XXX zero fields in the instructions loop? */
570 memset(iptr,0,sizeof(instruction) * (cumjcodelength + 5));
572 /* initialize block_index table (unrolled four times) */
577 for (i = 0, ip = block_index; i <= cumjcodelength; i += 4, ip += 4) {
585 /* compute branch targets of exception table */
587 extable = DMNEW(xtable, exceptiontablelength + 1);
589 for (i = 0; i < method->exceptiontablelength; i++) {
591 p = extable[i].startpc = raw_extable[i].startpc;
592 if (useinlining) p = label_index[p];
596 p = extable[i].endpc = raw_extable[i].endpc;
597 if (useinlining) p = label_index[p];
599 if (p < cumjcodelength)
602 p = extable[i].handlerpc = raw_extable[i].handlerpc;
606 extable[i].catchtype = raw_extable[i].catchtype;
608 extable[i].next = NULL;
609 extable[i].down = &extable[i+1];
613 nextex = fillextable(extable, raw_extable, method->exceptiontablelength, label_index, &b_count);
615 s_count = 1 + exceptiontablelength; /* initialize stack element counter */
618 if (checksync && (method->flags & ACC_SYNCHRONIZED)) {
619 isleafmethod = false;
623 /* scan all java instructions */
625 for (p = 0, gp = 0; p < jcodelength; gp += (nextp - p), p = nextp) {
627 /* DEBUG XXX */ /*printf("p:%d gp:%d ",p,gp);*/
629 /* mark this position as a valid instruction start */
631 instructionstart[p] = 1;
634 if ((useinlining) && (gp == nextgp)) {
636 bool *readonly = NULL;
638 opcode = code_get_u1(p);
639 nextp = p += jcommandsize[opcode];
640 if (nextp > jcodelength)
641 panic("Unexpected end of bytecode");
642 tmpinlinf = list_first(inlinfo->inlinedmethods);
643 firstlocal = tmpinlinf->firstlocal;
644 label_index = tmpinlinf->label_index;
645 readonly = tmpinlinf->readonly;
647 for (i = 0, tptr = tmpinlinf->method->paramtypes + tmpinlinf->method->paramcount - 1; i < tmpinlinf->method->paramcount; i++, tptr--) {
650 if ((i == 0) && inlineparamopt) {
651 OP1(ICMD_CLEAR_ARGREN, firstlocal);
654 if (!inlineparamopt || !readonly[i]) {
658 op = ICMD_READONLY_ARG;
662 OP1(op, firstlocal + tmpinlinf->method->paramcount - 1 - i);
664 /* block_index[gp] |= (ipc << 1);*/ /*FIXME: necessary ? */
667 inlining_save_compiler_variables();
668 inlining_set_compiler_variables(tmpinlinf);
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);
921 BUILTIN1(BUILTIN_newarray_char, TYPE_ADR);
924 BUILTIN1(BUILTIN_newarray_float, TYPE_ADR);
927 BUILTIN1(BUILTIN_newarray_double, TYPE_ADR);
930 BUILTIN1(BUILTIN_newarray_byte, TYPE_ADR);
933 BUILTIN1(BUILTIN_newarray_short, TYPE_ADR);
936 BUILTIN1(BUILTIN_newarray_int, TYPE_ADR);
939 BUILTIN1(BUILTIN_newarray_long, TYPE_ADR);
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(class_array_of(component)->vftbl);
954 BUILTIN2(BUILTIN_newarray, TYPE_ADR);
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);
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:
1062 nextp = ALIGN((p + 1), 4);
1063 if (nextp + 8 > jcodelength)
1064 panic("Unexpected end of bytecode");
1066 tablep = (s4*)(jcode + nextp);
1069 num = code_get_u4(nextp + 4);
1070 tablep = DMNEW(s4, num * 2 + 2);
1073 OP2A(opcode, 0, tablep);
1075 /* default target */
1077 j = p + code_get_s4(nextp);
1080 *tablep = j; /* restore for little endian */
1086 /* number of pairs */
1088 num = code_get_u4(nextp);
1093 if (nextp + 8*(num) > jcodelength)
1094 panic("Unexpected end of bytecode");
1096 for (i = 0; i < num; i++) {
1099 j = code_get_s4(nextp);
1100 *tablep = j; /* restore for little endian */
1106 j = p + code_get_s4(nextp);
1109 *tablep = j; /* restore for little endian */
1120 case JAVA_TABLESWITCH:
1126 nextp = ALIGN((p + 1), 4);
1127 if (nextp + 12 > jcodelength)
1128 panic("Unexpected end of bytecode");
1130 tablep = (s4*)(jcode + nextp);
1133 num = code_get_u4(nextp + 8) - code_get_u4(nextp + 4);
1134 tablep = DMNEW(s4, num + 1 + 3);
1137 OP2A(opcode, 0, tablep);
1139 /* default target */
1141 j = p + code_get_s4(nextp);
1144 *tablep = j; /* restore for little endian */
1152 j = code_get_s4(nextp);
1153 *tablep = j; /* restore for little endian */
1159 num = code_get_s4(nextp);
1160 *tablep = num; /* restore for little endian */
1166 if (nextp + 4*(num+1) > jcodelength)
1167 panic("Unexpected end of bytecode");
1169 for (i = 0; i <= num; i++) {
1170 j = p + code_get_s4(nextp);
1173 *tablep = j; /* restore for little endian */
1184 /* load and store of object fields *******************/
1187 BUILTIN3(BUILTIN_aastore, TYPE_VOID);
1190 case JAVA_PUTSTATIC:
1191 case JAVA_GETSTATIC:
1192 i = code_get_u2(p + 1);
1194 constant_FMIref *fr;
1196 fr = class_getconstant (class, i, CONSTANT_Fieldref);
1197 fi = class_findfield (fr->class, fr->name, fr->descriptor);
1198 compiler_addinitclass (fr->class);
1199 OP2A(opcode, fi->type, fi);
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);
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);
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);
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);
1280 /* miscellaneous object operations *******/
1283 i = code_get_u2 (p+1);
1285 LOADCONST_A(class_getconstant(class, i, CONSTANT_Class));
1287 BUILTIN1(BUILTIN_new, TYPE_ADR);
1290 case JAVA_CHECKCAST:
1291 i = code_get_u2(p+1);
1293 classinfo *cls = (classinfo*)class_getconstant(class, i, CONSTANT_Class);
1294 if (cls->vftbl->arraydesc) {
1295 /* array type cast-check */
1296 LOADCONST_A(cls->vftbl);
1298 BUILTIN2(BUILTIN_checkarraycast, TYPE_ADR);
1300 else { /* object type cast-check */
1302 + LOADCONST_A(class_getconstant(class, i, CONSTANT_Class));
1304 + BUILTIN2(BUILTIN_checkcast, TYPE_ADR);
1306 OP2A(opcode, 1, cls);
1312 case JAVA_INSTANCEOF:
1313 i = code_get_u2(p+1);
1316 classinfo *cls = (classinfo*)class_getconstant(class, i, CONSTANT_Class);
1317 if (cls->vftbl->arraydesc) {
1318 /* array type cast-check */
1319 LOADCONST_A(cls->vftbl);
1321 BUILTIN2(BUILTIN_arrayinstanceof, TYPE_INT);
1323 else { /* object type cast-check */
1325 LOADCONST_A(class_getconstant(class, i, CONSTANT_Class));
1327 BUILTIN2(BUILTIN_instanceof, TYPE_INT);
1329 OP2A(opcode, 1, cls);
1334 case JAVA_MONITORENTER:
1337 BUILTIN1(BUILTIN_monitorenter, TYPE_VOID);
1341 OP(ICMD_NULLCHECKPOP);
1345 case JAVA_MONITOREXIT:
1348 BUILTIN1(BUILTIN_monitorexit, TYPE_VOID);
1357 /* any other basic operation **************************************/
1376 #if defined(__I386__)
1379 BUILTIN2(BUILTIN_frem, TYPE_FLOAT);
1384 #if defined(__I386__)
1387 BUILTIN2(BUILTIN_drem, TYPE_DOUBLE);
1392 #if defined(__ALPHA__)
1394 BUILTIN1(BUILTIN_f2i, TYPE_INT);
1403 #if defined(__ALPHA__)
1405 BUILTIN1(BUILTIN_f2l, TYPE_LONG);
1414 #if defined(__ALPHA__)
1416 BUILTIN1(BUILTIN_d2i, TYPE_INT);
1425 #if defined(__ALPHA__)
1427 BUILTIN1(BUILTIN_d2l, TYPE_LONG);
1435 case JAVA_BREAKPOINT:
1436 panic("Illegal opcode Breakpoint encountered");
1492 printf("Illegal opcode %d at instr %d", opcode, ipc);
1493 panic("Illegal opcode encountered");
1502 /* If WIDE was used correctly, iswide should have been reset by now. */
1503 if (iswide && opcode != JAVA_WIDE)
1504 panic("Illegal instruction: WIDE before incompatible opcode");
1508 if (isinlinedmethod && p == jcodelength - 1) { /* end of an inlined method */
1509 /* printf("setting gp from %d to %d\n",gp, inlinfo->stopgp); */
1510 gp = inlinfo->stopgp;
1511 inlining_restore_compiler_variables();
1512 list_remove(inlinfo->inlinedmethods, list_first(inlinfo->inlinedmethods));
1513 if (inlinfo->inlinedmethods == NULL) {
1516 tmpinlinf = list_first(inlinfo->inlinedmethods);
1517 nextgp = (tmpinlinf != NULL) ? tmpinlinf->startgp : -1;
1519 /* printf("nextpgp: %d\n", nextgp); */
1520 label_index=inlinfo->label_index;
1521 firstlocal = inlinfo->firstlocal;
1525 if (p != jcodelength)
1526 panic("Command-sequence crosses code-boundary"); /* XXX change message */
1529 panic("Code does not end with branch/return/athrow - stmt");
1531 /* adjust block count if target 0 is not first intermediate instruction */
1533 if (!block_index[0] || (block_index[0] > 1))
1536 /* copy local to global variables */
1539 block_count = b_count;
1540 stack_count = s_count + block_count * maxstack;
1542 /* allocate stack table */
1544 stack = DMNEW(stackelement, stack_count);
1549 bptr = block = DMNEW(basicblock, b_count + 1); /* one more for end ipc */
1554 /* additional block if target 0 is not first intermediate instruction */
1556 if (!block_index[0] || (block_index[0] > 1)) {
1557 bptr->iinstr = instr;
1560 bptr->type = BBTYPE_STD;
1561 bptr->branchrefs = NULL;
1562 bptr->pre_count = 0;
1563 bptr->debug_nr = c_debug_nr++;
1566 (bptr - 1)->next = bptr;
1569 /* allocate blocks */
1571 for (p = 0; p < cumjcodelength; p++) {
1572 if (block_index[p] & 1) {
1573 /* check if this block starts at the beginning of an instruction */
1574 if (!instructionstart[p])
1575 panic("Branch into middle of instruction");
1576 /* allocate the block */
1577 bptr->iinstr = instr + (block_index[p] >> 1);
1578 bptr->debug_nr = c_debug_nr++;
1580 (bptr - 1)->icount = bptr->iinstr - (bptr - 1)->iinstr;
1584 bptr->type = BBTYPE_STD;
1585 bptr->branchrefs = NULL;
1586 block_index[p] = b_count;
1587 bptr->pre_count = 0;
1590 (bptr - 1)->next = bptr;
1594 /* allocate additional block at end */
1596 bptr->instack = bptr->outstack = NULL;
1597 bptr->indepth = bptr->outdepth = 0;
1598 bptr->iinstr = NULL;
1599 (bptr - 1)->icount = (instr + instr_count) - (bptr - 1)->iinstr;
1604 bptr->type = BBTYPE_STD;
1605 bptr->branchrefs = NULL;
1606 bptr->pre_count = 0;
1607 bptr->debug_nr = c_debug_nr++;
1608 (bptr - 1)->next = bptr;
1613 if (exceptiontablelength > 0)
1614 extable[exceptiontablelength - 1].down = NULL;
1618 for (i = 0; i < exceptiontablelength; ++i) {
1619 p = extable[i].startpc;
1620 extable[i].start = block + block_index[p];
1622 p = extable[i].endpc;
1623 extable[i].end = block + block_index[p];
1625 p = extable[i].handlerpc;
1626 extable[i].handler = block + block_index[p];
1630 if (useinlining) inlining_cleanup();
1631 useinlining = useinltmp;
1636 * These are local overrides for various environment variables in Emacs.
1637 * Please do not remove this and leave it at the end of the file, where
1638 * Emacs will automagically detect them.
1639 * ---------------------------------------------------------------------
1642 * indent-tabs-mode: t