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 725 2003-12-10 00:24:36Z 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 #define bound_check1(i) \
438 if (i < 0 || i > cumjcodelength) { \
439 panic("branch target out of code-boundary"); \
445 static xtable* fillextable(xtable* extable, exceptiontable *raw_extable, int exceptiontablelength, int *label_index, int *block_count)
449 if (exceptiontablelength == 0)
452 b_count = *block_count;
453 for (i = 0; i < exceptiontablelength; i++) {
455 p = raw_extable[i].startpc;
456 if (label_index != NULL) p = label_index[p];
457 extable[i].startpc = p;
461 p = raw_extable[i].endpc;
462 if (label_index != NULL) p = label_index[p];
463 extable[i].endpc = p;
465 if (p < cumjcodelength)
468 p = raw_extable[i].handlerpc;
469 if (label_index != NULL) p = label_index[p];
470 extable[i].handlerpc = p;
474 extable[i].catchtype = raw_extable[i].catchtype;
476 extable[i].next = NULL;
477 extable[i].down = &extable[i+1];
479 *block_count = b_count;
480 return &extable[i]; /* return the next free xtable* */
487 int p; /* java instruction counter */
488 int nextp; /* start of next java instruction */
489 int opcode; /* java opcode */
490 int i; /* temporary for different uses (counters) */
491 int ipc = 0; /* intermediate instruction counter */
492 int b_count = 0; /* basic block counter */
493 int s_count = 0; /* stack element counter */
494 bool blockend = false; /* true if basic block end has been reached */
495 bool iswide = false; /* true if last instruction was a wide */
496 instruction *iptr; /* current pointer into instruction array */
497 int gp; /* global java instruction counter */
498 /* inlining info for current method */
499 inlining_methodinfo *inlinfo = inlining_rootinfo;
500 inlining_methodinfo *tmpinlinf;
501 int nextgp = -1; /* start of next method to be inlined */
502 int *label_index = NULL; /* label redirection table */
503 int firstlocal = 0; /* first local variable of method */
504 xtable* nextex; /* points next free entry in extable */
508 if (compileverbose) {
509 char logtext[MAXLOGTEXT];
510 sprintf(logtext, "Parsing: ");
511 utf_sprint(logtext+strlen(logtext), method->class->name);
512 strcpy(logtext+strlen(logtext), ".");
513 utf_sprint(logtext+strlen(logtext), method->name);
514 utf_sprint(logtext+strlen(logtext), method->descriptor);
520 label_index = inlinfo->label_index;
521 maxstack = cummaxstack;
522 exceptiontablelength = cumextablelength;
525 useinltmp = useinlining; /* FIXME remove this after debugging */
526 /*useinlining = false;*/ /* and merge the if-statements */
529 cumjcodelength = jcodelength;
532 tmpinlinf = (inlining_methodinfo*) list_first(inlinfo->inlinedmethods);
533 if (tmpinlinf != NULL) nextgp = tmpinlinf->startgp;
536 if ((opt_rt || opt_xta || opt_vta) && (pOpcodes == 2 || pOpcodes == 3)) {
537 printf("PARSE method name =");
538 utf_display(method->class->name);
540 method_display(method);
545 if (opt_rt || opt_xta) {
546 RT_jit_parse(method);
550 printf("VTA requested, but not yet implemented\n");
554 /* allocate instruction array and block index table */
556 /* 1 additional for end ipc and 3 for loop unrolling */
558 block_index = DMNEW(int, cumjcodelength + 4);
560 /* 1 additional for TRACEBUILTIN and 4 for MONITORENTER/EXIT */
561 /* additional MONITOREXITS are reached by branches which are 3 bytes */
563 iptr = instr = DMNEW(instruction, cumjcodelength + 5);
565 /* XXX zero fields in the instructions loop? */
566 memset(iptr,0,sizeof(instruction) * (cumjcodelength + 5));
568 /* initialize block_index table (unrolled four times) */
573 for (i = 0, ip = block_index; i <= cumjcodelength; i += 4, ip += 4) {
581 /* compute branch targets of exception table */
583 extable = DMNEW(xtable, exceptiontablelength + 1);
585 for (i = 0; i < method->exceptiontablelength; i++) {
587 p = extable[i].startpc = raw_extable[i].startpc;
588 if (useinlining) p = label_index[p];
592 p = extable[i].endpc = raw_extable[i].endpc;
593 if (useinlining) p = label_index[p];
595 if (p < cumjcodelength)
598 p = extable[i].handlerpc = raw_extable[i].handlerpc;
602 extable[i].catchtype = raw_extable[i].catchtype;
604 extable[i].next = NULL;
605 extable[i].down = &extable[i+1];
609 nextex = fillextable(extable, raw_extable, method->exceptiontablelength, label_index, &b_count);
611 s_count = 1 + exceptiontablelength; /* initialize stack element counter */
614 if (checksync && (method->flags & ACC_SYNCHRONIZED)) {
615 isleafmethod = false;
619 /* scan all java instructions */
621 for (p = 0, gp = 0; p < jcodelength; gp += (nextp - p), p = nextp) {
623 /* DEBUG XXX */ /*printf("p:%d gp:%d ",p,gp);*/
626 if ((useinlining) && (gp == nextgp)) {
628 bool *readonly = NULL;
630 opcode = code_get_u1(p);
631 nextp = p += jcommandsize[opcode];
632 if (nextp > jcodelength)
633 panic("Unexpected end of bytecode");
634 tmpinlinf = list_first(inlinfo->inlinedmethods);
635 firstlocal = tmpinlinf->firstlocal;
636 label_index = tmpinlinf->label_index;
637 readonly = tmpinlinf->readonly;
639 for (i = 0, tptr = tmpinlinf->method->paramtypes + tmpinlinf->method->paramcount - 1; i < tmpinlinf->method->paramcount; i++, tptr--) {
642 if ((i == 0) && inlineparamopt) {
643 OP1(ICMD_CLEAR_ARGREN, firstlocal);
646 if (!inlineparamopt || !readonly[i]) {
650 op = ICMD_READONLY_ARG;
654 OP1(op, firstlocal + tmpinlinf->method->paramcount - 1 - i);
656 /* block_index[gp] |= (ipc << 1);*/ /*FIXME: necessary ? */
659 inlining_save_compiler_variables();
660 inlining_set_compiler_variables(tmpinlinf);
662 if (inlinfo->inlinedmethods == NULL) {
665 tmpinlinf = list_first(inlinfo->inlinedmethods);
666 nextgp = (tmpinlinf != NULL) ? tmpinlinf->startgp : -1;
668 if (method->exceptiontablelength > 0)
669 nextex = fillextable(nextex, method->exceptiontable, method->exceptiontablelength, label_index, &b_count);
673 opcode = code_get_u1(p); /* fetch op code */
676 if (opt_rt && (pOpcodes == 2 || pOpcodes == 3)) {
677 printf("Parse<%i> p=%i<%i< opcode=<%i> %s\n",
678 pOpcodes, p, rt_jcodelength, opcode, icmd_names[opcode]);
681 block_index[gp] |= (ipc << 1); /* store intermediate count */
684 block_insert(gp); /* start new block */
688 nextp = p + jcommandsize[opcode]; /* compute next instruction start */
689 s_count += stackreq[opcode]; /* compute stack element count */
695 /* pushing constants onto the stack p */
698 LOADCONST_I(code_get_s1(p+1));
702 LOADCONST_I(code_get_s2(p+1));
706 i = code_get_u1(p+1);
707 goto pushconstantitem;
710 i = code_get_u2(p + 1);
714 if (i >= class->cpcount)
715 panic ("Attempt to access constant outside range");
717 switch (class->cptags[i]) {
718 case CONSTANT_Integer:
719 LOADCONST_I(((constant_integer*)
720 (class->cpinfos[i]))->value);
723 LOADCONST_L(((constant_long*)
724 (class->cpinfos[i]))->value);
727 LOADCONST_F(((constant_float*)
728 (class->cpinfos[i]))->value);
730 case CONSTANT_Double:
731 LOADCONST_D(((constant_double*)
732 (class->cpinfos[i]))->value);
734 case CONSTANT_String:
735 LOADCONST_A(literalstring_new((utf*)
736 (class->cpinfos[i])));
738 default: panic("Invalid constant type to push");
742 case JAVA_ACONST_NULL:
753 LOADCONST_I(opcode - JAVA_ICONST_0);
758 LOADCONST_L(opcode - JAVA_LCONST_0);
764 LOADCONST_F(opcode - JAVA_FCONST_0);
769 LOADCONST_D(opcode - JAVA_DCONST_0);
772 /* loading variables onto the stack */
780 i = code_get_u1(p + 1);
782 i = code_get_u2(p + 1);
786 OP1LOAD(opcode, i + firstlocal);
793 OP1LOAD(ICMD_ILOAD, opcode - JAVA_ILOAD_0 + firstlocal);
800 OP1LOAD(ICMD_LLOAD, opcode - JAVA_LLOAD_0 + firstlocal);
807 OP1LOAD(ICMD_FLOAD, opcode - JAVA_FLOAD_0 + firstlocal);
814 OP1LOAD(ICMD_DLOAD, opcode - JAVA_DLOAD_0 + firstlocal);
821 OP1LOAD(ICMD_ALOAD, opcode - JAVA_ALOAD_0 + firstlocal);
824 /* storing stack values into local variables */
832 i = code_get_u1(p + 1);
834 i = code_get_u2(p + 1);
838 OP1STORE(opcode, i + firstlocal);
845 OP1STORE(ICMD_ISTORE, opcode - JAVA_ISTORE_0 + firstlocal);
852 OP1STORE(ICMD_LSTORE, opcode - JAVA_LSTORE_0 + firstlocal);
859 OP1STORE(ICMD_FSTORE, opcode - JAVA_FSTORE_0 + firstlocal);
866 OP1STORE(ICMD_DSTORE, opcode - JAVA_DSTORE_0 + firstlocal);
873 OP1STORE(ICMD_ASTORE, opcode - JAVA_ASTORE_0 + firstlocal);
881 i = code_get_u1(p + 1);
882 v = code_get_s1(p + 2);
885 i = code_get_u2(p + 1);
886 v = code_get_s2(p + 3);
890 INDEX_ONEWORD(i + firstlocal);
891 OP2I(opcode, i + firstlocal, v);
895 /* wider index for loading, storing and incrementing */
902 /* managing arrays ************************************************/
905 OP2I(ICMD_CHECKASIZE, 0, 0);
906 switch (code_get_s1(p + 1)) {
908 BUILTIN1((functionptr) builtin_newarray_boolean, TYPE_ADR);
911 BUILTIN1((functionptr) builtin_newarray_char, TYPE_ADR);
914 BUILTIN1((functionptr) builtin_newarray_float, TYPE_ADR);
917 BUILTIN1((functionptr) builtin_newarray_double, TYPE_ADR);
920 BUILTIN1((functionptr) builtin_newarray_byte, TYPE_ADR);
923 BUILTIN1((functionptr) builtin_newarray_short, TYPE_ADR);
926 BUILTIN1((functionptr) builtin_newarray_int, TYPE_ADR);
929 BUILTIN1((functionptr) builtin_newarray_long, TYPE_ADR);
931 default: panic("Invalid array-type to create");
936 OP2I(ICMD_CHECKASIZE, 0, 0);
937 i = code_get_u2(p + 1);
939 classinfo *component = (classinfo*)class_getconstant(class, i, CONSTANT_Class);
940 LOADCONST_A(class_array_of(component)->vftbl);
944 #if defined(__I386__)
945 BUILTIN2((functionptr) asm_builtin_newarray, TYPE_ADR);
947 BUILTIN2((functionptr)builtin_newarray, TYPE_ADR);
952 case JAVA_MULTIANEWARRAY:
954 i = code_get_u2(p + 1);
956 int v = code_get_u1(p + 3);
957 vftbl *arrayvftbl = ((classinfo*)class_getconstant (class, i, CONSTANT_Class))->vftbl;
958 OP2A(opcode, v, arrayvftbl);
980 i = p + code_get_s2(p + 1);
992 i = p + code_get_s4(p + 1);
1005 i = code_get_u1(p + 1);
1007 i = code_get_u2(p + 1);
1014 if (isinlinedmethod) {
1015 OP1(ICMD_GOTO, inlinfo->stopgp);
1019 OP1LOAD(opcode, i + firstlocal);
1028 if (isinlinedmethod) {
1029 /* if (p==jcodelength-1) {*/ /* return is at end of inlined method */
1034 OP1(ICMD_GOTO, inlinfo->stopgp);
1048 /* table jumps ********************************/
1050 case JAVA_LOOKUPSWITCH:
1056 nextp = ALIGN((p + 1), 4);
1057 if (nextp + 8 > jcodelength)
1058 panic("Unexpected end of bytecode");
1060 tablep = (s4*)(jcode + nextp);
1063 num = code_get_u4(nextp + 4);
1064 tablep = DMNEW(s4, num * 2 + 2);
1067 OP2A(opcode, 0, tablep);
1069 /* default target */
1071 j = p + code_get_s4(nextp);
1074 *tablep = j; /* restore for little endian */
1080 /* number of pairs */
1082 num = code_get_u4(nextp);
1087 if (nextp + 8*(num) > jcodelength)
1088 panic("Unexpected end of bytecode");
1090 for (i = 0; i < num; i++) {
1093 j = code_get_s4(nextp);
1094 *tablep = j; /* restore for little endian */
1100 j = p + code_get_s4(nextp);
1103 *tablep = j; /* restore for little endian */
1114 case JAVA_TABLESWITCH:
1120 nextp = ALIGN((p + 1), 4);
1121 if (nextp + 12 > jcodelength)
1122 panic("Unexpected end of bytecode");
1124 tablep = (s4*)(jcode + nextp);
1127 num = code_get_u4(nextp + 8) - code_get_u4(nextp + 4);
1128 tablep = DMNEW(s4, num + 1 + 3);
1131 OP2A(opcode, 0, tablep);
1133 /* default target */
1135 j = p + code_get_s4(nextp);
1138 *tablep = j; /* restore for little endian */
1146 j = code_get_s4(nextp);
1147 *tablep = j; /* restore for little endian */
1153 num = code_get_s4(nextp);
1154 *tablep = num; /* restore for little endian */
1160 if (nextp + 4*(num+1) > jcodelength)
1161 panic("Unexpected end of bytecode");
1163 for (i = 0; i <= num; i++) {
1164 j = p + code_get_s4(nextp);
1167 *tablep = j; /* restore for little endian */
1178 /* load and store of object fields *******************/
1181 BUILTIN3((functionptr) asm_builtin_aastore, TYPE_VOID);
1184 case JAVA_PUTSTATIC:
1185 case JAVA_GETSTATIC:
1186 i = code_get_u2(p + 1);
1188 constant_FMIref *fr;
1190 fr = class_getconstant (class, i, CONSTANT_Fieldref);
1191 fi = class_findfield (fr->class, fr->name, fr->descriptor);
1192 compiler_addinitclass (fr->class);
1193 OP2A(opcode, fi->type, fi);
1199 i = code_get_u2(p + 1);
1201 constant_FMIref *fr;
1203 fr = class_getconstant (class, i, CONSTANT_Fieldref);
1204 fi = class_findfield (fr->class, fr->name, fr->descriptor);
1205 OP2A(opcode, fi->type, fi);
1210 /* method invocation *****/
1212 case JAVA_INVOKESTATIC:
1213 i = code_get_u2(p + 1);
1215 constant_FMIref *mr;
1218 mr = class_getconstant (class, i, CONSTANT_Methodref);
1219 mi = class_fetchmethod (mr->class, mr->name, mr->descriptor);
1220 /*RTAprint*/ if (((pOpcodes == 2) || (pOpcodes == 3)) && opt_rt)
1221 /*RTAprint*/ {printf(" method name =");
1222 /*RTAprint*/ utf_display(mi->class->name); printf(".");
1223 /*RTAprint*/ utf_display(mi->name);printf("\tINVOKE STATIC\n");
1224 /*RTAprint*/ fflush(stdout);}
1225 if (!(mi->flags & ACC_STATIC))
1226 panic ("Static/Nonstatic mismatch calling static method");
1227 descriptor2types(mi);
1230 OP2A(opcode, mi->paramcount, mi);
1234 case JAVA_INVOKESPECIAL:
1235 case JAVA_INVOKEVIRTUAL:
1236 i = code_get_u2(p + 1);
1238 constant_FMIref *mr;
1241 mr = class_getconstant (class, i, CONSTANT_Methodref);
1242 mi = class_fetchmethod (mr->class, mr->name, mr->descriptor);
1243 /*RTAprint*/ if (((pOpcodes == 2) || (pOpcodes == 3)) && opt_rt)
1244 /*RTAprint*/ {printf(" method name =");
1246 /*RTAprint*/ utf_display(mi->class->name); printf(".");
1247 /*RTAprint*/ utf_display(mi->name);printf("\tINVOKE SPECIAL/VIRTUAL\n");
1248 /*RTAprint*/ fflush(stdout);}
1250 if (mi->flags & ACC_STATIC)
1251 panic ("Static/Nonstatic mismatch calling static method");
1252 descriptor2types(mi);
1254 OP2A(opcode, mi->paramcount, mi);
1258 case JAVA_INVOKEINTERFACE:
1259 i = code_get_u2(p + 1);
1261 constant_FMIref *mr;
1264 mr = class_getconstant (class, i, CONSTANT_InterfaceMethodref);
1265 mi = class_fetchmethod (mr->class, mr->name, mr->descriptor);
1266 if (mi->flags & ACC_STATIC)
1267 panic ("Static/Nonstatic mismatch calling static method");
1268 descriptor2types(mi);
1270 OP2A(opcode, mi->paramcount, mi);
1274 /* miscellaneous object operations *******/
1277 i = code_get_u2 (p+1);
1279 LOADCONST_A(class_getconstant(class, i, CONSTANT_Class));
1281 BUILTIN1((functionptr) builtin_new, TYPE_ADR);
1284 case JAVA_CHECKCAST:
1285 i = code_get_u2(p+1);
1287 classinfo *cls = (classinfo*)class_getconstant(class, i, CONSTANT_Class);
1288 if (cls->vftbl->arraydesc) {
1289 /* array type cast-check */
1290 LOADCONST_A(cls->vftbl);
1292 BUILTIN2((functionptr) asm_builtin_checkarraycast, TYPE_ADR);
1294 else { /* object type cast-check */
1296 + LOADCONST_A(class_getconstant(class, i, CONSTANT_Class));
1298 + BUILTIN2((functionptr) asm_builtin_checkcast, TYPE_ADR);
1300 OP2A(opcode, 1, cls);
1306 case JAVA_INSTANCEOF:
1307 i = code_get_u2(p+1);
1310 classinfo *cls = (classinfo*)class_getconstant(class, i, CONSTANT_Class);
1311 if (cls->vftbl->arraydesc) {
1312 /* array type cast-check */
1313 LOADCONST_A(cls->vftbl);
1315 #if defined(__I386__)
1316 BUILTIN2((functionptr) asm_builtin_arrayinstanceof, TYPE_INT);
1318 BUILTIN2((functionptr) builtin_arrayinstanceof, TYPE_INT);
1321 else { /* object type cast-check */
1323 LOADCONST_A(class_getconstant(class, i, CONSTANT_Class));
1325 BUILTIN2((functionptr) builtin_instanceof, TYPE_INT);
1327 OP2A(opcode, 1, cls);
1332 case JAVA_MONITORENTER:
1335 BUILTIN1((functionptr) asm_builtin_monitorenter, TYPE_VOID);
1339 OP(ICMD_NULLCHECKPOP);
1343 case JAVA_MONITOREXIT:
1346 BUILTIN1((functionptr) asm_builtin_monitorexit, TYPE_VOID);
1355 /* any other basic operation **************************************/
1374 #if defined(__I386__)
1377 BUILTIN2((functionptr) builtin_frem, TYPE_FLOAT);
1382 #if defined(__I386__)
1385 BUILTIN2((functionptr) builtin_drem, TYPE_DOUBLE);
1390 #if defined(__ALPHA__)
1392 BUILTIN1((functionptr) builtin_f2i, TYPE_INT);
1401 #if defined(__ALPHA__)
1403 BUILTIN1((functionptr) builtin_f2l, TYPE_LONG);
1412 #if defined(__ALPHA__)
1414 BUILTIN1((functionptr) builtin_d2i, TYPE_INT);
1423 #if defined(__ALPHA__)
1425 BUILTIN1((functionptr) builtin_d2l, TYPE_LONG);
1433 case JAVA_BREAKPOINT:
1434 panic("Illegal opcode Breakpoint encountered");
1490 printf("Illegal opcode %d at instr %d", opcode, ipc);
1491 panic("Illegal opcode encountered");
1502 if (isinlinedmethod && p == jcodelength - 1) { /* end of an inlined method */
1503 /* printf("setting gp from %d to %d\n",gp, inlinfo->stopgp); */
1504 gp = inlinfo->stopgp;
1505 inlining_restore_compiler_variables();
1506 list_remove(inlinfo->inlinedmethods, list_first(inlinfo->inlinedmethods));
1507 if (inlinfo->inlinedmethods == NULL) {
1510 tmpinlinf = list_first(inlinfo->inlinedmethods);
1511 nextgp = (tmpinlinf != NULL) ? tmpinlinf->startgp : -1;
1513 /* printf("nextpgp: %d\n", nextgp); */
1514 label_index=inlinfo->label_index;
1515 firstlocal = inlinfo->firstlocal;
1519 if (p != jcodelength)
1520 panic("Command-sequence crosses code-boundary"); /* XXX change message */
1523 panic("Code does not end with branch/return/athrow - stmt");
1525 /* adjust block count if target 0 is not first intermediate instruction */
1527 if (!block_index[0] || (block_index[0] > 1))
1530 /* copy local to global variables */
1533 block_count = b_count;
1534 stack_count = s_count + block_count * maxstack;
1536 /* allocate stack table */
1538 stack = DMNEW(stackelement, stack_count);
1543 bptr = block = DMNEW(basicblock, b_count + 1); /* one more for end ipc */
1548 /* additional block if target 0 is not first intermediate instruction */
1550 if (!block_index[0] || (block_index[0] > 1)) {
1551 bptr->iinstr = instr;
1554 bptr->type = BBTYPE_STD;
1555 bptr->branchrefs = NULL;
1556 bptr->pre_count = 0;
1557 bptr->debug_nr = c_debug_nr++;
1560 (bptr - 1)->next = bptr;
1563 /* allocate blocks */
1565 for (p = 0; p < cumjcodelength; p++) {
1566 if (block_index[p] & 1) {
1567 bptr->iinstr = instr + (block_index[p] >> 1);
1568 bptr->debug_nr = c_debug_nr++;
1570 (bptr - 1)->icount = bptr->iinstr - (bptr - 1)->iinstr;
1574 bptr->type = BBTYPE_STD;
1575 bptr->branchrefs = NULL;
1576 block_index[p] = b_count;
1577 bptr->pre_count = 0;
1580 (bptr - 1)->next = bptr;
1584 /* allocate additional block at end */
1586 bptr->instack = bptr->outstack = NULL;
1587 bptr->indepth = bptr->outdepth = 0;
1588 bptr->iinstr = NULL;
1589 (bptr - 1)->icount = (instr + instr_count) - (bptr - 1)->iinstr;
1594 bptr->type = BBTYPE_STD;
1595 bptr->branchrefs = NULL;
1596 bptr->pre_count = 0;
1597 bptr->debug_nr = c_debug_nr++;
1598 (bptr - 1)->next = bptr;
1603 if (exceptiontablelength > 0)
1604 extable[exceptiontablelength - 1].down = NULL;
1608 for (i = 0; i < exceptiontablelength; ++i) {
1609 p = extable[i].startpc;
1610 extable[i].start = block + block_index[p];
1612 p = extable[i].endpc;
1613 extable[i].end = block + block_index[p];
1615 p = extable[i].handlerpc;
1616 extable[i].handler = block + block_index[p];
1620 if (useinlining) inlining_cleanup();
1621 useinlining = useinltmp;
1626 * These are local overrides for various environment variables in Emacs.
1627 * Please do not remove this and leave it at the end of the file, where
1628 * Emacs will automagically detect them.
1629 * ---------------------------------------------------------------------
1632 * indent-tabs-mode: t