1 /* src/vm/jit/parse.c - parser for JavaVM to intermediate code translation
3 Copyright (C) 1996-2005 R. Grafl, A. Krall, C. Kruegel, C. Oates,
4 R. Obermaisser, M. Platter, M. Probst, S. Ring, E. Steiner,
5 C. Thalinger, D. Thuernbeck, P. Tomsich, C. Ullrich, J. Wenninger,
6 Institut f. Computersprachen - TU Wien
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
34 $Id: parse.c 2358 2005-04-22 22:01:51Z jowenn $
43 #include "mm/memory.h"
44 #include "native/native.h"
45 #include "toolbox/logging.h"
46 #include "vm/builtin.h"
47 #include "vm/exceptions.h"
48 #include "vm/global.h"
49 #include "vm/linker.h"
50 #include "vm/loader.h"
51 #include "vm/resolve.h"
52 #include "vm/options.h"
53 #include "vm/statistics.h"
54 #include "vm/stringlocal.h"
55 #include "vm/tables.h"
56 #include "vm/jit/asmpart.h"
57 #include "vm/jit/jit.h"
58 #include "vm/jit/parse.h"
59 #include "vm/jit/inline/parseRT.h"
60 #include "vm/jit/inline/parseXTA.h"
61 #include "vm/jit/inline/inline.h"
62 #include "vm/jit/loop/loop.h"
63 #include "vm/jit/inline/parseRTprint.h"
68 bool DEBUG4 = false; /*opcodes for parse.c*/
72 #define debug_writebranch if (DEBUG2==true) printf("op:: %s i: %d label_index[i]: %d label_index=0x%p\n",opcode_names[opcode], i, label_index[i], (void *)label_index);
73 #define debug_writebranch1
75 /*******************************************************************************
77 function 'parse' scans the JavaVM code and generates intermediate code
79 During parsing the block index table is used to store at bit pos 0
80 a flag which marks basic block starts and at position 1 to 31 the
81 intermediate instruction index. After parsing the block index table
82 is scanned, for marked positions a block is generated and the block
83 number is stored in the block index table.
85 *******************************************************************************/
87 static exceptiontable* fillextable(methodinfo *m,
88 exceptiontable* extable, exceptiontable *raw_extable,
89 int exceptiontablelength,
90 int *label_index, int *block_count,
91 t_inlining_globals *inline_env)
93 int b_count, p, src, insertBlock;
95 if (exceptiontablelength == 0)
98 /*if (m->exceptiontablelength > 0) {
100 printf("m->exceptiontablelength=%i\n",m->exceptiontablelength);
101 panic("exceptiontablelength > 0");
104 b_count = *block_count;
106 for (src = exceptiontablelength-1; src >=0; src--) {
107 /* printf("Excepiont table index: %d\n",i); */
108 p = raw_extable[src].startpc;
109 if (label_index != NULL) p = label_index[p];
110 extable->startpc = p;
114 /*** if (DEBUG==true){printf("---------------------block_inserted:b_count=%i m->basicblockindex[(p=%i)]=%i=%p\n",b_count,p,m->basicblockindex[(p)],m->basicblockindex[(p)]);
115 fflush(stdout); } ***/
116 p = raw_extable[src].endpc; /* see JVM Spec 4.7.3 */
117 if (p <= raw_extable[src].startpc)
118 panic("Invalid exception handler range");
120 if (p >inline_env->method->jcodelength) {
121 panic("Invalid exception handler end is after code end");
123 if (p<inline_env->method->jcodelength) insertBlock=1; else insertBlock=0;
124 /*if (label_index !=NULL) printf("%s:translating endpc:%ld to %ld, label_index:%p\n",m->name->text,p,label_index[p],label_index); else
125 printf("%s:fillextab: endpc:%ld\n",m->name->text,p);*/
126 if (label_index != NULL) p = label_index[p];
129 /*if (p < inline_env->method->jcodelength) {
131 if (insertBlock) block_insert(p);
133 p = raw_extable[src].handlerpc;
134 if (label_index != NULL) p = label_index[p];
135 extable->handlerpc = p;
139 extable->catchtype = raw_extable[src].catchtype;
140 extable->next = NULL;
141 extable->down = &extable[1];
145 *block_count = b_count;
146 return extable; /*&extable[i];*/ /* return the next free xtable* */
151 methodinfo *parse(methodinfo *m, codegendata *cd, t_inlining_globals *inline_env)
153 int p; /* java instruction counter */
154 int nextp; /* start of next java instruction */
155 int opcode; /* java opcode */
156 int i; /* temporary for different uses (ctrs)*/
157 int ipc = 0; /* intermediate instruction counter */
158 int b_count = 0; /* basic block counter */
159 int s_count = 0; /* stack element counter */
160 bool blockend = false; /* true if basic block end has been reached */
161 bool iswide = false; /* true if last instruction was a wide*/
162 instruction *iptr; /* current ptr into instruction array */
163 int gp; /* global java instruction counter */
164 /* inlining info for current method */
166 inlining_methodinfo *inlinfo = inline_env->inlining_rootinfo;
167 inlining_methodinfo *tmpinlinf;
168 int nextgp = -1; /* start of next method to be inlined */
169 int *label_index = NULL; /* label redirection table */
170 int firstlocal = 0; /* first local variable of method */
171 exceptiontable* nextex; /* points next free entry in extable */
172 u1 *instructionstart; /* 1 for pcs which are valid instr. starts */
178 u2 skipBasicBlockChange;
180 METHINFOt(m,"\nPARSING: ",DEBUG4);
181 if ((opt_rt) || (opt_xta)) {
184 if (opt_rt) Missed = rtMissed;
185 if (opt_xta) Missed = xtaMissed;
187 if (m->methodUsed != USED) {
189 printf(" rta/xta missed: "); fflush(stdout);
190 METHINFO(m,opt_verbose);
192 if ( (Missed = fopen("Missed", "a")) == NULL) {
193 printf("CACAO - rt/xtaMissed file: cant open file to write append \n");
196 utf_fprint(Missed,m->class->name);
197 fprintf(Missed," "); fflush(Missed);
198 utf_fprint(Missed,m->name);
199 fprintf(Missed," "); fflush(Missed);
200 utf_fprint(Missed,m->descriptor);
201 fprintf(Missed,"\n"); fflush(Missed);
209 label_index = inlinfo->label_index;
210 m->maxstack = inline_env->cummaxstack;
211 /*JOWENN m->exceptiontablelength = inline_env->cumextablelength;*/
212 tmpinlinf = (inlining_methodinfo*)
213 list_first(inlinfo->inlinedmethods);
214 if (tmpinlinf != NULL) nextgp = tmpinlinf->startgp;
217 /**** static analysis has to be called before inlining
218 which has to be called before reg_set
219 which has to be called before parse (or ???)
220 will check if method being parsed was analysed here
221 if (opt_xta && opt_verbose) {
223 printf("XTA requested, not available\n");
225 if (opt_vta && opt_verbose)
226 printf("VTA requested, not yet implemented\n");
229 /* allocate instruction array and block index table */
231 /* 1 additional for end ipc * # cum inline methods*/
233 m->basicblockindex = DMNEW(s4, inline_env->cumjcodelength + inline_env->cummethods);
234 memset(m->basicblockindex, 0, sizeof(s4) * (inline_env->cumjcodelength + inline_env->cummethods));
236 instructionstart = DMNEW(u1, inline_env->cumjcodelength + inline_env->cummethods);
237 memset(instructionstart, 0, sizeof(u1) * (inline_env->cumjcodelength + inline_env->cummethods));
239 /* 1 additional for TRACEBUILTIN and 4 for MONITORENTER/EXIT */
240 /* additional MONITOREXITS are reached by branches which are 3 bytes */
242 iptr = m->instructions = DMNEW(instruction, inline_env->cumjcodelength + 5);
244 /* Zero the intermediate instructions array so we don't have any
245 * invalid pointers in it if we cannot finish analyse_stack(). */
247 memset(iptr, 0, sizeof(instruction) * (inline_env->cumjcodelength + 5));
249 /* compute branch targets of exception table */
251 if (m->exceptiontable == NULL) {
252 printf("m->exceptiontable=NULL\n");fflush(stdout);
255 printf("m->exceptiontable != NULL\n");fflush(stdout);
257 printf("m->exceptiontablelength=%i, inline_env->method->exceptiontablelength=%i,inline_env->cumextablelength=%i\n",
258 m->exceptiontablelength, inline_env->method->exceptiontablelength,inline_env->cumextablelength);
261 if (m->exceptiontablelength > 0)
262 m->exceptiontable = DMNEW(exceptiontable, m->exceptiontablelength + 1);
265 nextex = fillextable(m,
266 &(cd->exceptiontable[cd->exceptiontablelength-1]), m->exceptiontable, m->exceptiontablelength,
267 label_index, &b_count, inline_env);
268 s_count = 1 + m->exceptiontablelength; /* initialize stack element counter */
270 #if defined(USE_THREADS)
271 if (checksync && (m->flags & ACC_SYNCHRONIZED)) {
272 m->isleafmethod = false;
273 inline_env->method->isleafmethod = false;
277 /* scan all java instructions */
281 if (m->linenumbercount == 0) {
283 /*printf("linenumber count == 0\n");*/
285 linepcchange = m->linenumbers[0].start_pc;
288 skipBasicBlockChange=0;
289 for (p = 0, gp = 0; p < inline_env->method->jcodelength; gp += (nextp - p), p = nextp) {
291 /* DEBUG */ if (DEBUG==true) printf("----- p:%d gp:%d\n",p,gp);
293 /* mark this position as a valid instruction start */
295 instructionstart[gp] = 1;
296 /*log_text("new start of instruction");*/
297 /*printf ("%s, linepcchange %d,p %d\n",inline_env->method->name->text,linepcchange,p);*/
298 if (linepcchange==p) {
299 if (inline_env->method->linenumbercount > lineindex) {
300 currentline = inline_env->method->linenumbers[lineindex].line_number;
302 if (lineindex < inline_env->method->linenumbercount)
303 linepcchange = inline_env->method->linenumbers[lineindex].start_pc;
304 /*printf("Line number changed to: %ld\n",currentline);*/
310 if ((useinlining) && (gp == nextgp)) {
312 bool *readonly = NULL;
315 block_insert(gp); /* JJJJJJJJJJ */
317 instructionstart[gp] = 1;
318 m->basicblockindex[gp] |= (ipc << 1); /*FIXME: necessary ? */
320 opcode = code_get_u1(p,inline_env->method);
321 nextp = p += jcommandsize[opcode];
322 if (nextp > inline_env->method->jcodelength)
323 panic("Unexpected end of bytecode");
324 tmpinlinf = list_first(inlinfo->inlinedmethods);
325 firstlocal = tmpinlinf->firstlocal;
326 label_index = tmpinlinf->label_index;
327 readonly = tmpinlinf->readonly;
329 for (i=0,tptr=tmpinlinf->method->paramtypes;i<tmpinlinf->method->paramcount;i++,tptr++) {
330 if ( ((*tptr)==TYPE_LNG) ||
331 ((*tptr)==TYPE_DBL) )
337 for (i = 0, tptr = tmpinlinf->method->paramtypes + tmpinlinf->method->paramcount - 1; i < tmpinlinf->method->paramcount; i++, tptr--) {
340 if ((i == 0) && inlineparamopt) {
341 OP1(ICMD_CLEAR_ARGREN, firstlocal);
344 if (!inlineparamopt || !readonly[i]) {
348 op = ICMD_READONLY_ARG;
352 if ( ((*tptr)==TYPE_LNG) ||
353 ((*tptr)==TYPE_DBL) )
358 OP1(op, firstlocal + argBlockIdx);
359 /* OP1(op, firstlocal + tmpinlinf->method->paramcount - 1 - i); */
360 /* printf("inline argument load operation for local: %ld\n",firstlocal + tmpinlinf->method->paramcount - 1 - i); */
362 skipBasicBlockChange=1;
363 METHINFOt(inline_env->method,"BEFORE SAVE: ",DEBUG);
364 inlining_save_compiler_variables();
365 METHINFOt(inline_env->method,"AFTER SAVE: ",DEBUG);
366 inlining_set_compiler_variables(tmpinlinf);
367 METHINFOt(inline_env->method,"AFTER SET :: ",DEBUG);
368 METHINFOt(m,"\n.......Parsing (inlined): ",DEBUG);
369 METHINFO(inline_env->method,DEBUG);
371 OP1(ICMD_INLINE_START,tmpinlinf->level);
373 if (inlinfo->inlinedmethods == NULL) {
376 tmpinlinf = list_first(inlinfo->inlinedmethods);
377 nextgp = (tmpinlinf != NULL) ? tmpinlinf->startgp : -1;
379 if (inline_env->method->exceptiontablelength > 0)
380 nextex = fillextable(m, nextex,
381 inline_env->method->exceptiontable, inline_env->method->exceptiontablelength,
382 label_index, &b_count, inline_env);
386 opcode = code_get_u1(p,inline_env->method); /* fetch op code */
389 printf("Parse p=%i<%i<%i< opcode=<%i> %s\n",
390 p, gp, inline_env->jcodelength, opcode, opcode_names[opcode]);
392 printf("label_index[%d]=%d\n",p,label_index[p]);
395 printf("basicblockindex[gp=%i]=%i=%p ipc=%i=%p shifted ipc=%i=%p\n",
396 gp,m->basicblockindex[gp],m->basicblockindex[gp],ipc,ipc,(ipc<<1),(ipc<<1));
399 if (!skipBasicBlockChange) {
400 m->basicblockindex[gp] |= (ipc << 1); /*store intermed cnt*/
401 } else skipBasicBlockChange=0;
403 printf("basicblockindex[gp=%i]=%i=%p \n",
404 gp,m->basicblockindex[gp],m->basicblockindex[gp]);
409 block_insert(gp); /* start new block */
411 /*printf("blockend was set: new blockcount: %ld at:%ld\n",b_count,gp);*/
414 nextp = p + jcommandsize[opcode]; /* compute next instruction start */
415 if (nextp > inline_env->method->jcodelength)
416 panic("Unexpected end of bytecode");
417 s_count += stackreq[opcode]; /* compute stack element count */
423 /* pushing constants onto the stack p */
426 LOADCONST_I(code_get_s1(p+1,inline_env->method));
430 LOADCONST_I(code_get_s2(p+1,inline_env->method));
434 i = code_get_u1(p+1,inline_env->method);
436 goto pushconstantitem;
439 i = code_get_u2(p + 1,inline_env->method);
443 if (i >= inline_env->method->class->cpcount)
444 error("Attempt to access constant outside range: %d >= %d", i, inline_env->method->class->cpcount);
446 switch (inline_env->method->class->cptags[i]) {
447 case CONSTANT_Integer:
448 LOADCONST_I(((constant_integer *) (inline_env->method->class->cpinfos[i]))->value);
451 LOADCONST_L(((constant_long *) (inline_env->method->class->cpinfos[i]))->value);
454 LOADCONST_F(((constant_float *) (inline_env->method->class->cpinfos[i]))->value);
456 case CONSTANT_Double:
457 LOADCONST_D(((constant_double *) (inline_env->method->class->cpinfos[i]))->value);
459 case CONSTANT_String:
460 LOADCONST_A(literalstring_new((utf *) (inline_env->method->class->cpinfos[i])));
462 default: panic("Invalid constant type to push");
466 case JAVA_ACONST_NULL:
477 LOADCONST_I(opcode - JAVA_ICONST_0);
482 LOADCONST_L(opcode - JAVA_LCONST_0);
488 LOADCONST_F(opcode - JAVA_FCONST_0);
493 LOADCONST_D(opcode - JAVA_DCONST_0);
496 /* loading variables onto the stack */
504 i = code_get_u1(p + 1,inline_env->method);
506 i = code_get_u2(p + 1,inline_env->method);
510 OP1LOAD(opcode, i + firstlocal);
517 OP1LOAD(ICMD_ILOAD, opcode - JAVA_ILOAD_0 + firstlocal);
524 OP1LOAD(ICMD_LLOAD, opcode - JAVA_LLOAD_0 + firstlocal);
531 OP1LOAD(ICMD_FLOAD, opcode - JAVA_FLOAD_0 + firstlocal);
538 OP1LOAD(ICMD_DLOAD, opcode - JAVA_DLOAD_0 + firstlocal);
545 OP1LOAD(ICMD_ALOAD, opcode - JAVA_ALOAD_0 + firstlocal);
548 /* storing stack values into local variables */
556 i = code_get_u1(p + 1,inline_env->method);
558 i = code_get_u2(p + 1,inline_env->method);
562 OP1STORE(opcode, i + firstlocal);
569 OP1STORE(ICMD_ISTORE, opcode - JAVA_ISTORE_0 + firstlocal);
576 OP1STORE(ICMD_LSTORE, opcode - JAVA_LSTORE_0 + firstlocal);
583 OP1STORE(ICMD_FSTORE, opcode - JAVA_FSTORE_0 + firstlocal);
590 OP1STORE(ICMD_DSTORE, opcode - JAVA_DSTORE_0 + firstlocal);
597 OP1STORE(ICMD_ASTORE, opcode - JAVA_ASTORE_0 + firstlocal);
605 i = code_get_u1(p + 1,inline_env->method);
606 v = code_get_s1(p + 2,inline_env->method);
609 i = code_get_u2(p + 1,inline_env->method);
610 v = code_get_s2(p + 3,inline_env->method);
614 INDEX_ONEWORD(i + firstlocal);
615 OP2I(opcode, i + firstlocal, v);
619 /* wider index for loading, storing and incrementing */
626 /* managing arrays ************************************************/
630 switch (code_get_s1(p + 1,inline_env->method)) {
632 BUILTIN1(BUILTIN_newarray_boolean, TYPE_ADR,currentline);
635 BUILTIN1(BUILTIN_newarray_char, TYPE_ADR,currentline);
638 BUILTIN1(BUILTIN_newarray_float, TYPE_ADR,currentline);
641 BUILTIN1(BUILTIN_newarray_double, TYPE_ADR,currentline);
644 BUILTIN1(BUILTIN_newarray_byte, TYPE_ADR,currentline);
647 BUILTIN1(BUILTIN_newarray_short, TYPE_ADR,currentline);
650 BUILTIN1(BUILTIN_newarray_int, TYPE_ADR,currentline);
653 BUILTIN1(BUILTIN_newarray_long, TYPE_ADR,currentline);
655 default: panic("Invalid array-type to create");
657 OP(ICMD_CHECKEXCEPTION);
662 i = code_get_u2(p + 1,inline_env->method);
664 classinfo *component;
665 constant_classref *compr;
666 constant_classref *cr;
669 #if defined(__X86_64__) || defined(__I386__)
670 compr = (constant_classref *) class_getconstant(inline_env->method->class, i, CONSTANT_Class);
672 if (!(cr = class_get_classref_multiarray_of(1, compr)))
675 if (!resolve_classref(inline_env->method, cr, resolveLazy, true, &c))
679 LOADCONST_A_BUILTIN(c->vftbl);
680 BUILTIN2(BUILTIN_newarray, TYPE_ADR, currentline);
683 LOADCONST_A_BUILTIN(cr);
684 BUILTIN2(asm_wrapper_patcher_BUILTIN_newarray, TYPE_ADR, currentline);
688 cr = (constant_classref *) class_getconstant(inline_env->method->class, i, CONSTANT_Class);
690 if (!resolve_classref(inline_env->method,
691 cr,resolveEager,true,&component))
694 c = class_array_of(component,true);
697 LOADCONST_A_BUILTIN(c->vftbl);
699 BUILTIN2(BUILTIN_anewarray, TYPE_ADR, currentline);
702 OP(ICMD_CHECKEXCEPTION);
705 case JAVA_MULTIANEWARRAY:
706 inline_env->method->isleafmethod = false;
707 i = code_get_u2(p + 1, inline_env->method);
709 classinfo *component;
711 constant_classref *cr;
713 s4 v = code_get_u1(p + 3, inline_env->method);
715 #if defined(__X86_64__) || defined(__I386__)
716 cr = (constant_classref *) class_getconstant(inline_env->method->class, i, CONSTANT_Class);
718 if (!resolve_classref(inline_env->method, cr, resolveLazy, true, &c))
722 OP2AT(opcode, v, c->vftbl, BUILTIN_multianewarray, currentline);
725 OP2AT(opcode, v, cr, asm_wrapper_patcher_BUILTIN_multianewarray, currentline);
728 /* vftbl *arrayvftbl = */
729 /* ((classinfo *) class_getconstant(class, i, CONSTANT_Class))->vftbl; */
730 /* OP2A(opcode, v, arrayvftbl,currentline); */
733 cr = (constant_classref *) class_getconstant(inline_env->method->class, i, CONSTANT_Class);
735 if (!resolve_classref_or_classinfo(inline_env->method,
736 CLASSREF_OR_CLASSINFO(cr),resolveEager,true,&component))
739 arrayvftbl = component->vftbl;
740 OP2A(opcode, v, arrayvftbl, currentline);
742 /* classinfo *arrayclass = */
743 /* (classinfo *) class_getconstant(class, i, CONSTANT_Class); */
744 /* OP2A(opcode, v, arrayclass, currentline); */
767 i = p + code_get_s2(p + 1,inline_env->method);
779 i = p + code_get_s4(p + 1,inline_env->method);
785 /*printf("B6 JSR_W\t"); fflush(stdout);*/
793 i = code_get_u1(p + 1,inline_env->method);
795 i = code_get_u2(p + 1,inline_env->method);
802 if (inline_env->isinlinedmethod) {
803 OP1(ICMD_GOTO, inlinfo->stopgp);
807 OP1LOAD(opcode, i + firstlocal);
816 if (inline_env->isinlinedmethod) {
817 /* if (p==m->jcodelength-1) {*/ /* return is at end of inlined method */
821 if (nextp>inline_env->method->jcodelength-1) {
822 /* OP1(ICMD_GOTO, inlinfo->stopgp);
830 OP1(ICMD_GOTO, inlinfo->stopgp);
844 /* table jumps ********************************/
846 case JAVA_LOOKUPSWITCH:
853 nextp = ALIGN((p + 1), 4);
854 if (nextp + 8 > inline_env->method->jcodelength)
855 panic("Unexpected end of bytecode");
857 tablep = (s4 *) (inline_env->method->jcode + nextp);
860 num = code_get_u4(nextp + 4,inline_env->method);
861 tablep = DMNEW(s4, num * 2 + 2);
864 OP2A(opcode, 0, tablep,currentline);
868 j = p + code_get_s4(nextp,inline_env->method);
871 *tablep = j; /* restore for little endian */
877 /* number of pairs */
879 num = code_get_u4(nextp,inline_env->method);
884 if (nextp + 8*(num) > inline_env->method->jcodelength)
885 panic("Unexpected end of bytecode");
887 for (i = 0; i < num; i++) {
890 j = code_get_s4(nextp,inline_env->method);
891 *tablep = j; /* restore for little endian */
895 /* check if the lookup table is sorted correctly */
897 if (i && (j <= prevvalue)) {
898 *exceptionptr = new_verifyerror(m, "Unsorted lookup switch");
905 j = p + code_get_s4(nextp,inline_env->method);
908 *tablep = j; /* restore for little endian */
919 case JAVA_TABLESWITCH:
925 nextp = ALIGN((p + 1), 4);
926 if (nextp + 12 > inline_env->method->jcodelength)
927 panic("Unexpected end of bytecode");
929 tablep = (s4 *) (inline_env->method->jcode + nextp);
932 num = code_get_u4(nextp + 8,inline_env->method) - code_get_u4(nextp + 4,inline_env->method);
933 tablep = DMNEW(s4, num + 1 + 3);
936 OP2A(opcode, 0, tablep,currentline);
940 j = p + code_get_s4(nextp,inline_env->method);
943 *tablep = j; /* restore for little endian */
951 j = code_get_s4(nextp,inline_env->method);
952 *tablep = j; /* restore for little endian */
958 num = code_get_s4(nextp,inline_env->method);
959 *tablep = num; /* restore for little endian */
963 num -= j; /* difference of upper - lower */
965 panic("invalid TABLESWITCH: upper bound < lower bound");
967 if (nextp + 4*(num+1) > inline_env->method->jcodelength)
968 panic("Unexpected end of bytecode");
970 for (i = 0; i <= num; i++) {
971 j = p + code_get_s4(nextp,inline_env->method);
973 /*printf("TABLESWITCH: j before mapping=%ld\n",j);*/
976 *tablep = j; /* restore for little endian */
981 /*printf("TABLESWITCH: block_insert(%ld)\n",j);*/
988 /* load and store of object fields *******************/
991 BUILTIN3(BUILTIN_aastore, TYPE_VOID, currentline);
996 #if defined(__X86_64__) || defined(__I386__)
1000 i = code_get_u2(p + 1, inline_env->method);
1002 constant_FMIref *fr;
1003 unresolved_field *uf;
1007 fr = class_getconstant(inline_env->method->class, i, CONSTANT_Fieldref);
1008 #if defined(__X86_64__) || defined(__I386__)
1009 OP2A_NOINC(opcode, fr->parseddesc.fd->type, fr, currentline);
1011 if (!(uf = create_unresolved_field(inline_env->method->class,
1017 /* store unresolved_field pointer */
1021 /* only with -noverify, otherwise the typechecker does this */
1024 if (!resolve_field(uf, resolveLazy, &fi))
1033 if (!resolve_classref(inline_env->method,fr->classref,resolveEager,true,&frclass))
1036 fi = class_resolvefield(frclass,
1039 inline_env->method->class,
1045 OP2A(opcode, fi->type, fi, currentline);
1046 if (!fi->class->initialized) {
1047 inline_env->method->isleafmethod = false;
1054 #if !defined(__X86_64__) && !defined(__I386__)
1057 i = code_get_u2(p + 1,inline_env->method);
1059 constant_FMIref *fr;
1063 fr = class_getconstant(inline_env->method->class, i, CONSTANT_Fieldref);
1064 if (!resolve_classref(inline_env->method,fr->classref,resolveEager,true,&frclass))
1067 fi = class_resolvefield(frclass,
1070 inline_env->method->class,
1076 OP2A(opcode, fi->type, fi, currentline);
1082 /* method invocation *****/
1084 case JAVA_INVOKESTATIC:
1085 i = code_get_u2(p + 1, inline_env->method);
1087 constant_FMIref *mr;
1089 unresolved_method *um;
1092 inline_env->method->isleafmethod = false;
1094 mr = class_getconstant(inline_env->method->class, i, CONSTANT_Methodref);
1095 #if defined(__X86_64__) || defined(__I386__)
1096 OP2A_NOINC(opcode, mr->parseddesc.md->paramcount, mr, currentline);
1098 um = create_unresolved_method(inline_env->method->class,
1106 /* store the unresolved_method pointer */
1110 /* only with -noverify, otherwise the typechecker does this */
1113 if (!resolve_method(um, resolveLazy, &mi))
1120 if (!resolve_classref(inline_env->method,mr->classref,resolveEager,true,&mrclass))
1123 mi = class_resolveclassmethod(mrclass,
1126 inline_env->method->class,
1133 method_display_w_class(mi);
1134 printf("\tINVOKE STAT\n");
1137 if (!(mi->flags & ACC_STATIC)) {
1139 new_exception(string_java_lang_IncompatibleClassChangeError);
1143 method_descriptor2types(mi);
1144 OP2A(opcode, mi->paramcount, mi, currentline);
1149 case JAVA_INVOKESPECIAL:
1150 case JAVA_INVOKEVIRTUAL:
1151 i = code_get_u2(p + 1, inline_env->method);
1153 constant_FMIref *mr;
1155 unresolved_method *um;
1158 inline_env->method->isleafmethod = false;
1160 mr = class_getconstant(inline_env->method->class, i, CONSTANT_Methodref);
1161 #if defined(__X86_64__) || defined(__I386__)
1162 OP2A_NOINC(opcode, mr->parseddesc.md->paramcount + 1, mr, currentline);
1164 um = create_unresolved_method(inline_env->method->class,
1172 /* store the unresolved_method* */
1176 /* only with -noverify, otherwise the typechecker does this */
1179 if (!resolve_method(um, resolveLazy, &mi))
1186 if (!resolve_classref(inline_env->method,mr->classref,resolveEager,true,&mrclass))
1189 mi = class_resolveclassmethod(mrclass,
1192 inline_env->method->class,
1199 method_display_w_class(mi);
1200 printf("\tINVOKE SPEC/VIRT\n");
1203 if (mi->flags & ACC_STATIC) {
1205 new_exception(string_java_lang_IncompatibleClassChangeError);
1209 method_descriptor2types(mi);
1210 OP2A(opcode, mi->paramcount, mi, currentline);
1215 case JAVA_INVOKEINTERFACE:
1216 i = code_get_u2(p + 1,inline_env->method);
1218 constant_FMIref *mr;
1221 unresolved_method *um;
1223 inline_env->method->isleafmethod = false;
1225 mr = class_getconstant(inline_env->method->class, i, CONSTANT_InterfaceMethodref);
1226 #if defined(__X86_64__) || defined(__I386__)
1227 OP2A_NOINC(opcode, mr->parseddesc.md->paramcount + 1, mr, currentline);
1229 um = create_unresolved_method(inline_env->method->class,
1237 /* store the unresolved_method* */
1241 /* only with -noverify, otherwise the typechecker does this */
1244 if (!resolve_method(um, resolveLazy, &mi))
1251 if (!resolve_classref(inline_env->method,mr->classref,resolveEager,true,&mrclass))
1254 mi = class_resolveinterfacemethod(mrclass,
1257 inline_env->method->class,
1262 if (mi->flags & ACC_STATIC) {
1264 new_exception(string_java_lang_IncompatibleClassChangeError);
1269 method_display_w_class(mi);
1270 printf("\tINVOKE INTERFACE\n");
1273 method_descriptor2types(mi);
1274 OP2A(opcode, mi->paramcount, mi, currentline);
1279 /* miscellaneous object operations *******/
1283 constant_classref *cr;
1286 #if defined(__X86_64__) || defined(__I386__)
1287 i = code_get_u2(p + 1, inline_env->method);
1288 cr = (constant_classref *) class_getconstant(inline_env->method->class, i, CONSTANT_Class);
1290 if (!resolve_classref(inline_env->method, cr, resolveLazy, true, &cls))
1293 /* <clinit> can throw an exception over native code */
1295 if (cls && cls->initialized) {
1296 LOADCONST_A_BUILTIN(cls);
1297 BUILTIN1(BUILTIN_new, TYPE_ADR, currentline);
1300 LOADCONST_A_BUILTIN(cr);
1301 BUILTIN1(asm_wrapper_patcher_BUILTIN_new, TYPE_ADR, currentline);
1305 OP(ICMD_CHECKEXCEPTION);
1307 i = code_get_u2(p + 1,inline_env->method);
1308 cr = (constant_classref *) class_getconstant(inline_env->method->class, i, CONSTANT_Class);
1309 if (!resolve_classref(inline_env->method,cr,resolveEager,true,&cls))
1311 LOADCONST_A_BUILTIN(cls);
1313 BUILTIN1(BUILTIN_new, TYPE_ADR, currentline);
1314 OP(ICMD_CHECKEXCEPTION);
1319 case JAVA_CHECKCAST:
1320 i = code_get_u2(p + 1, inline_env->method);
1322 constant_classref *cr;
1325 cr = (constant_classref *) class_getconstant(inline_env->method->class, i, CONSTANT_Class);
1327 #if defined(__X86_64__) || defined(__I386__)
1328 if (!resolve_classref(inline_env->method, cr, resolveLazy, true, &cls))
1331 if (cr->name->text[0] == '[') {
1332 /* array type cast-check */
1334 LOADCONST_A_BUILTIN(cls->vftbl);
1335 BUILTIN2(BUILTIN_checkarraycast, TYPE_ADR, currentline);
1338 LOADCONST_A_BUILTIN(cr);
1339 BUILTIN2(asm_wrapper_patcher_BUILTIN_checkarraycast, TYPE_ADR, currentline);
1344 /* object type cast-check */
1345 OP2AT(opcode, 1, cls, cr, currentline);
1348 if (!resolve_classref(inline_env->method,
1349 cr,resolveEager,true,&cls))
1352 if (cls->vftbl->arraydesc) {
1353 /* array type cast-check */
1354 LOADCONST_A_BUILTIN(cls->vftbl);
1356 BUILTIN2(BUILTIN_checkarraycast, TYPE_ADR,currentline);
1358 } else { /* object type cast-check */
1360 + LOADCONST_A_BUILTIN(class_getconstant(class, i, CONSTANT_Class));
1362 + BUILTIN2(BUILTIN_checkcast, TYPE_ADR,currentline);
1364 OP2A(opcode, 1, cls, currentline);
1370 case JAVA_INSTANCEOF:
1371 i = code_get_u2(p + 1,inline_env->method);
1373 constant_classref *cr;
1376 cr = (constant_classref *) class_getconstant(inline_env->method->class, i, CONSTANT_Class);
1378 #if defined(__X86_64__) || defined(__I386__)
1379 if (!resolve_classref(inline_env->method, cr, resolveLazy, true, &cls))
1382 if (cr->name->text[0] == '[') {
1383 /* array type cast-check */
1385 LOADCONST_A_BUILTIN(cls->vftbl);
1386 BUILTIN2(BUILTIN_arrayinstanceof, TYPE_INT, currentline);
1389 LOADCONST_A_BUILTIN(cr);
1390 BUILTIN2(asm_wrapper_patcher_BUILTIN_arrayinstanceof, TYPE_INT, currentline);
1395 /* object type cast-check */
1396 OP2AT(opcode, 1, cls, cr, currentline);
1399 if (!resolve_classref(inline_env->method,
1400 cr,resolveEager,true,&cls))
1403 if (cls->vftbl->arraydesc) {
1404 /* array type cast-check */
1405 LOADCONST_A_BUILTIN(cls->vftbl);
1407 BUILTIN2(BUILTIN_arrayinstanceof, TYPE_INT, currentline);
1409 else { /* object type cast-check */
1411 LOADCONST_A_BUILTIN(class_getconstant(class, i, CONSTANT_Class));
1413 BUILTIN2(BUILTIN_instanceof, TYPE_INT,currentline);
1415 OP2A(opcode, 1, cls, currentline);
1421 case JAVA_MONITORENTER:
1422 #if defined(USE_THREADS)
1424 BUILTIN1(BUILTIN_monitorenter, TYPE_VOID,currentline);
1428 OP(ICMD_NULLCHECKPOP);
1432 case JAVA_MONITOREXIT:
1433 #if defined(USE_THREADS)
1435 BUILTIN1(BUILTIN_monitorexit, TYPE_VOID,currentline);
1436 OP(ICMD_CHECKEXCEPTION);
1444 /* any other basic operation **************************************/
1463 #if defined(__I386__)
1466 BUILTIN2(BUILTIN_frem, TYPE_FLOAT,currentline);
1471 #if defined(__I386__)
1474 BUILTIN2(BUILTIN_drem, TYPE_DOUBLE,currentline);
1479 #if defined(__ALPHA__)
1481 BUILTIN1(BUILTIN_f2i, TYPE_INT,currentline);
1490 #if defined(__ALPHA__)
1492 BUILTIN1(BUILTIN_f2l, TYPE_LONG,currentline);
1501 #if defined(__ALPHA__)
1503 BUILTIN1(BUILTIN_d2i, TYPE_INT,currentline);
1512 #if defined(__ALPHA__)
1514 BUILTIN1(BUILTIN_d2l, TYPE_LONG,currentline);
1522 case JAVA_BREAKPOINT:
1524 new_verifyerror(m, "Quick instructions shouldn't appear yet.");
1527 case 204: /* unused opcode */
1579 printf("Illegal opcode %d at instr %d\n", opcode, ipc);
1580 panic("Illegal opcode encountered");
1589 /* If WIDE was used correctly, iswide should have been reset by now. */
1590 if (iswide && opcode != JAVA_WIDE)
1591 panic("Illegal instruction: WIDE before incompatible opcode");
1595 /* if (inline_env->isinlinedmethod && p == inline_env->method->jcodelength - 1) { */ /* end of an inlined method */
1596 if (inline_env->isinlinedmethod && (nextp >= inline_env->method->jcodelength) ) { /* end of an inlined method */
1597 /* printf("setting gp from %d to %d\n",gp, inlinfo->stopgp); */
1598 gp = inlinfo->stopgp;
1599 inlining_restore_compiler_variables();
1600 OP(ICMD_INLINE_END);
1601 /*label_index = inlinfo->label_index;*/
1603 METHINFOt(inline_env->method,"AFTER RESTORE : ",DEBUG);
1604 list_remove(inlinfo->inlinedmethods, list_first(inlinfo->inlinedmethods));
1605 if (inlinfo->inlinedmethods == NULL) { /* JJJJ */
1608 tmpinlinf = list_first(inlinfo->inlinedmethods);
1609 nextgp = (tmpinlinf != NULL) ? tmpinlinf->startgp : -1;
1611 /* printf("nextpgp: %d\n", nextgp); */
1612 label_index=inlinfo->label_index;
1613 firstlocal = inlinfo->firstlocal;
1619 if (p != m->jcodelength) {
1620 printf("p (%d) != m->jcodelength (%d)\n",p,m->jcodelength);
1621 panic("Command-sequence crosses code-boundary");
1624 *exceptionptr = new_verifyerror(m, "Falling off the end of the code");
1628 /* adjust block count if target 0 is not first intermediate instruction */
1630 if (!m->basicblockindex[0] || (m->basicblockindex[0] > 1))
1633 /* copy local to method variables */
1635 m->instructioncount = ipc;
1636 m->basicblockcount = b_count;
1637 m->stackcount = s_count + m->basicblockcount * m->maxstack;
1639 /* allocate stack table */
1641 m->stack = DMNEW(stackelement, m->stackcount);
1646 bptr = m->basicblocks = DMNEW(basicblock, b_count + 1); /* one more for end ipc */
1651 /* additional block if target 0 is not first intermediate instruction */
1653 if (!m->basicblockindex[0] || (m->basicblockindex[0] > 1)) {
1654 bptr->iinstr = m->instructions;
1657 bptr->type = BBTYPE_STD;
1658 bptr->branchrefs = NULL;
1659 bptr->pre_count = 0;
1660 bptr->debug_nr = m->c_debug_nr++;
1663 (bptr - 1)->next = bptr;
1666 /* allocate blocks */
1668 for (p = 0; p < inline_env->cumjcodelength; p++) {
1669 /* for (p = 0; p < m->jcodelength; p++) { */
1670 if (m->basicblockindex[p] & 1) {
1671 /* check if this block starts at the beginning of an instruction */
1672 if (!instructionstart[p]) {
1673 printf("Basic Block beginn: %d\n",p);
1674 panic("Branch into middle of instruction");
1676 /* allocate the block */
1677 bptr->iinstr = m->instructions + (m->basicblockindex[p] >> 1);
1678 bptr->debug_nr = m->c_debug_nr++;
1680 (bptr - 1)->icount = bptr->iinstr - (bptr - 1)->iinstr;
1684 bptr->type = BBTYPE_STD;
1685 bptr->branchrefs = NULL;
1686 m->basicblockindex[p] = b_count;
1687 bptr->pre_count = 0;
1690 (bptr - 1)->next = bptr;
1694 /* allocate additional block at end */
1696 bptr->instack = bptr->outstack = NULL;
1697 bptr->indepth = bptr->outdepth = 0;
1698 bptr->iinstr = NULL;
1699 (bptr - 1)->icount = (m->instructions + m->instructioncount) - (bptr - 1)->iinstr;
1704 bptr->type = BBTYPE_STD;
1705 bptr->branchrefs = NULL;
1706 bptr->pre_count = 0;
1707 bptr->debug_nr = m->c_debug_nr++;
1708 (bptr - 1)->next = bptr;
1711 if (cd->exceptiontablelength > 0) {
1712 cd->exceptiontable[cd->exceptiontablelength - 1].down = NULL;
1715 for (i = 0; i < cd->exceptiontablelength; ++i) {
1716 p = cd->exceptiontable[i].startpc;
1717 cd->exceptiontable[i].start = m->basicblocks + m->basicblockindex[p];
1719 p = cd->exceptiontable[i].endpc;
1720 cd->exceptiontable[i].end = (p == inline_env->method->jcodelength) ? (m->basicblocks + m->basicblockcount /*+ 1*/) : (m->basicblocks + m->basicblockindex[p]);
1722 p = cd->exceptiontable[i].handlerpc;
1723 cd->exceptiontable[i].handler = m->basicblocks + m->basicblockindex[p];
1727 if (useinlining) inlining_cleanup(inline_env);
1729 /* just return methodinfo* to signal everything was ok */
1736 * These are local overrides for various environment variables in Emacs.
1737 * Please do not remove this and leave it at the end of the file, where
1738 * Emacs will automagically detect them.
1739 * ---------------------------------------------------------------------
1742 * indent-tabs-mode: t