Remaining lazy loading stuff for alpha.
[cacao.git] / src / vm / jit / parse.c
1 /* src/vm/jit/parse.c - parser for JavaVM to intermediate code translation
2
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
7
8    This file is part of CACAO.
9
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.
14
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.
19
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
23    02111-1307, USA.
24
25    Contact: cacao@complang.tuwien.ac.at
26
27    Author: Andreas Krall
28
29    Changes: Carolyn Oates
30             Edwin Steiner
31             Joseph Wenninger
32             Christian Thalinger
33
34    $Id: parse.c 2394 2005-04-27 12:42:00Z twisti $
35
36 */
37
38
39 #include <string.h>
40
41 #include "config.h"
42 #include "types.h"
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/patcher.h"
60 #include "vm/jit/inline/parseRT.h"
61 #include "vm/jit/inline/parseXTA.h"
62 #include "vm/jit/inline/inline.h"
63 #include "vm/jit/loop/loop.h"
64 #include "vm/jit/inline/parseRTprint.h"
65
66 bool DEBUG = false;
67 bool DEBUG2 = false;
68 bool DEBUG3 = false;
69 bool DEBUG4 = false;  /*opcodes for parse.c*/
70
71
72 /*INLINING*/
73 #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);
74 #define debug_writebranch1
75
76 /*******************************************************************************
77
78         function 'parse' scans the JavaVM code and generates intermediate code
79
80         During parsing the block index table is used to store at bit pos 0
81         a flag which marks basic block starts and at position 1 to 31 the
82         intermediate instruction index. After parsing the block index table
83         is scanned, for marked positions a block is generated and the block
84         number is stored in the block index table.
85
86 *******************************************************************************/
87
88 static exceptiontable* fillextable(methodinfo *m, 
89                 exceptiontable* extable, exceptiontable *raw_extable, 
90                 int exceptiontablelength, 
91                 int *label_index, int *block_count, 
92                 t_inlining_globals *inline_env)
93 {
94         int b_count, p, src, insertBlock;
95         
96         if (exceptiontablelength == 0) 
97                 return extable;
98
99         /*if (m->exceptiontablelength > 0) {
100           METHINFOx(m);
101           printf("m->exceptiontablelength=%i\n",m->exceptiontablelength);
102           panic("exceptiontablelength > 0");
103           }*/
104
105         b_count = *block_count;
106
107         for (src = exceptiontablelength-1; src >=0; src--) {
108                 /* printf("Excepiont table index: %d\n",i); */
109                 p = raw_extable[src].startpc;
110                 if (label_index != NULL) p = label_index[p];
111                 extable->startpc = p;
112                 bound_check(p);
113                 block_insert(p);
114                 
115 /*** 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)]); 
116   fflush(stdout); } ***/   
117                 p = raw_extable[src].endpc; /* see JVM Spec 4.7.3 */
118                 if (p <= raw_extable[src].startpc)
119                         panic("Invalid exception handler range");
120
121                 if (p >inline_env->method->jcodelength) {
122                         panic("Invalid exception handler end is after code end");
123                 }
124                 if (p<inline_env->method->jcodelength) insertBlock=1; else insertBlock=0;
125                 /*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
126                         printf("%s:fillextab: endpc:%ld\n",m->name->text,p);*/
127                 if (label_index != NULL) p = label_index[p];
128                 extable->endpc = p;
129                 bound_check1(p);
130                 /*if (p < inline_env->method->jcodelength) {
131                         block_insert(p); }*/
132                 if (insertBlock) block_insert(p);
133
134                 p = raw_extable[src].handlerpc;
135                 if (label_index != NULL) p = label_index[p];
136                 extable->handlerpc = p;
137                 bound_check(p);
138                 block_insert(p);
139
140                 extable->catchtype  = raw_extable[src].catchtype;
141                 extable->next = NULL;
142                 extable->down = &extable[1];
143                 extable--;
144         }
145
146         *block_count = b_count;
147         return extable; /*&extable[i];*/  /* return the next free xtable* */
148 }
149
150
151
152 methodinfo *parse(methodinfo *m, codegendata *cd, t_inlining_globals *inline_env)
153 {
154         int  p;                     /* java instruction counter           */
155         int  nextp;                 /* start of next java instruction     */
156         int  opcode;                /* java opcode                        */
157         int  i;                     /* temporary for different uses (ctrs)*/
158         int  ipc = 0;               /* intermediate instruction counter   */
159         int  b_count = 0;           /* basic block counter                */
160         int  s_count = 0;           /* stack element counter              */
161         bool blockend = false;      /* true if basic block end has been reached   */
162         bool iswide = false;        /* true if last instruction was a wide*/
163         instruction *iptr;          /* current ptr into instruction array */
164         int gp;                     /* global java instruction counter    */
165                                     /* inlining info for current method   */
166
167         inlining_methodinfo *inlinfo = inline_env->inlining_rootinfo;
168         inlining_methodinfo *tmpinlinf;
169         int nextgp = -1;            /* start of next method to be inlined */
170         int *label_index = NULL;    /* label redirection table            */
171         int firstlocal = 0;         /* first local variable of method     */
172         exceptiontable* nextex;     /* points next free entry in extable  */
173         u1 *instructionstart;       /* 1 for pcs which are valid instr. starts    */
174
175         u2 lineindex = 0;
176         u2 currentline = 0;
177         u2 linepcchange = 0;
178
179         u2 skipBasicBlockChange;
180
181 METHINFOt(m,"\nPARSING: ",DEBUG4);
182 if ((opt_rt) || (opt_xta)) {
183   FILE *Missed;
184
185   if (opt_rt)  Missed =  rtMissed;  
186   if (opt_xta) Missed = xtaMissed;  
187
188   if (m->methodUsed != USED) {
189     if (opt_verbose) {
190       printf(" rta/xta missed: "); fflush(stdout);
191       METHINFO(m,opt_verbose);
192       }
193     if ( (Missed = fopen("Missed", "a")) == NULL) {
194       printf("CACAO - rt/xtaMissed file: cant open file to write append \n");
195       }
196     else {
197       utf_fprint(Missed,m->class->name); 
198        fprintf(Missed," "); fflush(Missed);
199       utf_fprint(Missed,m->name);
200        fprintf(Missed," "); fflush(Missed);
201       utf_fprint(Missed,m->descriptor); 
202        fprintf(Missed,"\n"); fflush(Missed);
203       fclose(Missed);
204       }
205    } 
206 }
207         /* INLINING */
208
209         if (useinlining) {
210                 label_index = inlinfo->label_index;
211                 m->maxstack = inline_env->cummaxstack;
212                 /*JOWENN m->exceptiontablelength = inline_env->cumextablelength;*/
213                 tmpinlinf = (inlining_methodinfo*) 
214                                 list_first(inlinfo->inlinedmethods);
215                 if (tmpinlinf != NULL) nextgp = tmpinlinf->startgp;
216         }
217
218 /**** static analysis has to be called before inlining
219         which has to be called before reg_set
220         which has to be called before parse (or ???)
221         will check if method being parsed was analysed here
222         if (opt_xta && opt_verbose) { 
223                 **RT_jit_parse(m);**
224                 printf("XTA requested, not available\n");
225                 }
226         if (opt_vta && opt_verbose)  
227                     printf("VTA requested, not yet implemented\n");
228         ****/ 
229
230         /* allocate instruction array and block index table */
231         
232         /* 1 additional for end ipc * # cum inline methods*/
233         
234         m->basicblockindex = DMNEW(s4, inline_env->cumjcodelength + inline_env->cummethods);
235         memset(m->basicblockindex, 0, sizeof(s4) * (inline_env->cumjcodelength + inline_env->cummethods));
236
237         instructionstart = DMNEW(u1, inline_env->cumjcodelength + inline_env->cummethods);
238         memset(instructionstart, 0, sizeof(u1) * (inline_env->cumjcodelength + inline_env->cummethods));
239
240         /* 1 additional for TRACEBUILTIN and 4 for MONITORENTER/EXIT */
241         /* additional MONITOREXITS are reached by branches which are 3 bytes */
242         
243         iptr = m->instructions = DMNEW(instruction, inline_env->cumjcodelength + 5);
244
245         /* Zero the intermediate instructions array so we don't have any
246          * invalid pointers in it if we cannot finish analyse_stack(). */
247
248         memset(iptr, 0, sizeof(instruction) * (inline_env->cumjcodelength + 5));
249         
250         /* compute branch targets of exception table */
251         /*
252 if (m->exceptiontable == NULL) {
253   printf("m->exceptiontable=NULL\n");fflush(stdout);
254   }
255 else {
256   printf("m->exceptiontable != NULL\n");fflush(stdout);
257   }
258 printf("m->exceptiontablelength=%i, inline_env->method->exceptiontablelength=%i,inline_env->cumextablelength=%i\n",
259 m->exceptiontablelength, inline_env->method->exceptiontablelength,inline_env->cumextablelength);
260         */
261         /*
262 if (m->exceptiontablelength > 0)
263         m->exceptiontable = DMNEW(exceptiontable, m->exceptiontablelength + 1); 
264         */
265
266         nextex = fillextable(m, 
267           &(cd->exceptiontable[cd->exceptiontablelength-1]), m->exceptiontable, m->exceptiontablelength, 
268           label_index, &b_count, inline_env);
269         s_count = 1 + m->exceptiontablelength; /* initialize stack element counter   */
270
271 #if defined(USE_THREADS)
272         if (checksync && (m->flags & ACC_SYNCHRONIZED)) {
273                 m->isleafmethod = false;
274                 inline_env->method->isleafmethod = false;
275         }                       
276 #endif
277
278         /* scan all java instructions */
279         currentline = 0;
280         linepcchange = 0;
281
282         if (m->linenumbercount == 0) {
283                 lineindex = 0;
284                 /*printf("linenumber count == 0\n");*/
285         } else {
286                 linepcchange = m->linenumbers[0].start_pc;
287         }
288
289         skipBasicBlockChange=0;
290         for (p = 0, gp = 0; p < inline_env->method->jcodelength; gp += (nextp - p), p = nextp) {
291           
292                 /* DEBUG */      if (DEBUG==true) printf("----- p:%d gp:%d\n",p,gp);
293
294                 /* mark this position as a valid instruction start */
295                 if (!iswide) {
296                         instructionstart[gp] = 1;
297                         /*log_text("new start of instruction");*/
298                         /*printf ("%s, linepcchange %d,p %d\n",inline_env->method->name->text,linepcchange,p);*/
299                         if (linepcchange==p) {
300                                 if (inline_env->method->linenumbercount > lineindex) {
301                                         currentline = inline_env->method->linenumbers[lineindex].line_number;
302                                         lineindex++;
303                                         if (lineindex < inline_env->method->linenumbercount)
304                                                 linepcchange = inline_env->method->linenumbers[lineindex].start_pc;
305                                         /*printf("Line number changed to: %ld\n",currentline);*/
306                                 }
307                         }
308                 }
309
310                 /*INLINING*/
311                 if ((useinlining) && (gp == nextgp)) {
312                         u1 *tptr;
313                         bool *readonly = NULL;
314                         int argBlockIdx=0;
315
316                         block_insert(gp);               /* JJJJJJJJJJ */
317                         blockend=false;
318                         instructionstart[gp] = 1;
319                         m->basicblockindex[gp] |= (ipc << 1);  /*FIXME: necessary ? */
320
321                         opcode = code_get_u1(p,inline_env->method);
322                         nextp = p += jcommandsize[opcode];
323                         if (nextp > inline_env->method->jcodelength)
324                                 panic("Unexpected end of bytecode");
325                         tmpinlinf = list_first(inlinfo->inlinedmethods);
326                         firstlocal = tmpinlinf->firstlocal;
327                         label_index = tmpinlinf->label_index;
328                         readonly = tmpinlinf->readonly;
329
330                         for (i=0,tptr=tmpinlinf->method->paramtypes;i<tmpinlinf->method->paramcount;i++,tptr++) {
331                                 if ( ((*tptr)==TYPE_LNG) ||
332                                   ((*tptr)==TYPE_DBL) )
333                                         argBlockIdx+=2;
334                                 else
335                                         argBlockIdx++;
336                         }
337
338                         for (i = 0, tptr = tmpinlinf->method->paramtypes + tmpinlinf->method->paramcount - 1; i < tmpinlinf->method->paramcount; i++, tptr--) {
339                                 int op;
340
341                                 if ((i == 0) && inlineparamopt) {
342                                         OP1(ICMD_CLEAR_ARGREN, firstlocal);
343                                 }
344
345                                 if (!inlineparamopt || !readonly[i]) {
346                                         op = ICMD_ISTORE;
347
348                                 } else {
349                                         op = ICMD_READONLY_ARG;
350                                 }
351
352                                 op += *tptr;
353                                 if ( ((*tptr)==TYPE_LNG) ||
354                                   ((*tptr)==TYPE_DBL) )
355                                         argBlockIdx-=2;
356                                 else
357                                         argBlockIdx--;
358
359                                 OP1(op, firstlocal + argBlockIdx);
360                                 /* OP1(op, firstlocal + tmpinlinf->method->paramcount - 1 - i); */
361                                 /* printf("inline argument load operation for local: %ld\n",firstlocal + tmpinlinf->method->paramcount - 1 - i); */
362                         }
363                         skipBasicBlockChange=1;
364 METHINFOt(inline_env->method,"BEFORE SAVE: ",DEBUG);
365                         inlining_save_compiler_variables();
366 METHINFOt(inline_env->method,"AFTER SAVE: ",DEBUG);
367                         inlining_set_compiler_variables(tmpinlinf);
368 METHINFOt(inline_env->method,"AFTER SET :: ",DEBUG);
369 METHINFOt(m,"\n.......Parsing (inlined): ",DEBUG);
370 METHINFO(inline_env->method,DEBUG);
371
372                         OP1(ICMD_INLINE_START,tmpinlinf->level);
373
374                         if (inlinfo->inlinedmethods == NULL) {
375                                 gp = -1;
376                         } else {
377                                 tmpinlinf = list_first(inlinfo->inlinedmethods);
378                                 nextgp = (tmpinlinf != NULL) ? tmpinlinf->startgp : -1;
379                         }
380                         if (inline_env->method->exceptiontablelength > 0) 
381                           nextex = fillextable(m, nextex, 
382                             inline_env->method->exceptiontable, inline_env->method->exceptiontablelength, 
383                             label_index, &b_count, inline_env);
384                         continue;
385                 }
386           
387                 opcode = code_get_u1(p,inline_env->method);            /* fetch op code  */
388          if (DEBUG==true) 
389                 {
390                         printf("Parse p=%i<%i<%i<   opcode=<%i> %s\n",
391                            p, gp, inline_env->jcodelength, opcode, opcode_names[opcode]);
392                         if (label_index)
393                                 printf("label_index[%d]=%d\n",p,label_index[p]);
394                 }
395          /*
396 printf("basicblockindex[gp=%i]=%i=%p ipc=%i=%p shifted ipc=%i=%p\n",
397 gp,m->basicblockindex[gp],m->basicblockindex[gp],ipc,ipc,(ipc<<1),(ipc<<1));
398 fflush(stdout);
399          */
400                 if (!skipBasicBlockChange) {
401                         m->basicblockindex[gp] |= (ipc << 1); /*store intermed cnt*/
402                 } else skipBasicBlockChange=0;
403                 /*
404 printf("basicblockindex[gp=%i]=%i=%p \n",
405 gp,m->basicblockindex[gp],m->basicblockindex[gp]);
406 fflush(stdout);
407                 */
408
409                 if (blockend) {
410                         block_insert(gp);               /* start new block                */
411                         blockend = false;
412                         /*printf("blockend was set: new blockcount: %ld at:%ld\n",b_count,gp);*/
413                 }
414
415                 nextp = p + jcommandsize[opcode];   /* compute next instruction start */
416                 if (nextp > inline_env->method->jcodelength)
417                         panic("Unexpected end of bytecode");
418                 s_count += stackreq[opcode];            /* compute stack element count    */
419 SHOWOPCODE(DEBUG4)
420                 switch (opcode) {
421                 case JAVA_NOP:
422                         break;
423
424                         /* pushing constants onto the stack p */
425
426                 case JAVA_BIPUSH:
427                         LOADCONST_I(code_get_s1(p+1,inline_env->method));
428                         break;
429
430                 case JAVA_SIPUSH:
431                         LOADCONST_I(code_get_s2(p+1,inline_env->method));
432                         break;
433
434                 case JAVA_LDC1:
435                         i = code_get_u1(p+1,inline_env->method);
436
437                         goto pushconstantitem;
438                 case JAVA_LDC2:
439                 case JAVA_LDC2W:
440                         i = code_get_u2(p + 1,inline_env->method);
441
442                 pushconstantitem:
443
444                         if (i >= inline_env->method->class->cpcount) 
445                                 error("Attempt to access constant outside range: %d >= %d", i, inline_env->method->class->cpcount);
446
447                         switch (inline_env->method->class->cptags[i]) {
448                         case CONSTANT_Integer:
449                                 LOADCONST_I(((constant_integer *) (inline_env->method->class->cpinfos[i]))->value);
450                                 break;
451                         case CONSTANT_Long:
452                                 LOADCONST_L(((constant_long *) (inline_env->method->class->cpinfos[i]))->value);
453                                 break;
454                         case CONSTANT_Float:
455                                 LOADCONST_F(((constant_float *) (inline_env->method->class->cpinfos[i]))->value);
456                                 break;
457                         case CONSTANT_Double:
458                                 LOADCONST_D(((constant_double *) (inline_env->method->class->cpinfos[i]))->value);
459                                 break;
460                         case CONSTANT_String:
461                                 LOADCONST_A(literalstring_new((utf *) (inline_env->method->class->cpinfos[i])));
462                                 break;
463                         default: panic("Invalid constant type to push");
464                         }
465                         break;
466
467                 case JAVA_ACONST_NULL:
468                         LOADCONST_A(NULL);
469                         break;
470
471                 case JAVA_ICONST_M1:
472                 case JAVA_ICONST_0:
473                 case JAVA_ICONST_1:
474                 case JAVA_ICONST_2:
475                 case JAVA_ICONST_3:
476                 case JAVA_ICONST_4:
477                 case JAVA_ICONST_5:
478                         LOADCONST_I(opcode - JAVA_ICONST_0);
479                         break;
480
481                 case JAVA_LCONST_0:
482                 case JAVA_LCONST_1:
483                         LOADCONST_L(opcode - JAVA_LCONST_0);
484                         break;
485
486                 case JAVA_FCONST_0:
487                 case JAVA_FCONST_1:
488                 case JAVA_FCONST_2:
489                         LOADCONST_F(opcode - JAVA_FCONST_0);
490                         break;
491
492                 case JAVA_DCONST_0:
493                 case JAVA_DCONST_1:
494                         LOADCONST_D(opcode - JAVA_DCONST_0);
495                         break;
496
497                         /* loading variables onto the stack */
498
499                 case JAVA_ILOAD:
500                 case JAVA_LLOAD:
501                 case JAVA_FLOAD:
502                 case JAVA_DLOAD:
503                 case JAVA_ALOAD:
504                         if (!iswide) {
505                                 i = code_get_u1(p + 1,inline_env->method);
506                         } else {
507                                 i = code_get_u2(p + 1,inline_env->method);
508                                 nextp = p + 3;
509                                 iswide = false;
510                         }
511                         OP1LOAD(opcode, i + firstlocal);
512                         break;
513
514                 case JAVA_ILOAD_0:
515                 case JAVA_ILOAD_1:
516                 case JAVA_ILOAD_2:
517                 case JAVA_ILOAD_3:
518                         OP1LOAD(ICMD_ILOAD, opcode - JAVA_ILOAD_0 + firstlocal);
519                         break;
520
521                 case JAVA_LLOAD_0:
522                 case JAVA_LLOAD_1:
523                 case JAVA_LLOAD_2:
524                 case JAVA_LLOAD_3:
525                         OP1LOAD(ICMD_LLOAD, opcode - JAVA_LLOAD_0 + firstlocal);
526                         break;
527
528                 case JAVA_FLOAD_0:
529                 case JAVA_FLOAD_1:
530                 case JAVA_FLOAD_2:
531                 case JAVA_FLOAD_3:
532                         OP1LOAD(ICMD_FLOAD, opcode - JAVA_FLOAD_0 + firstlocal);
533                         break;
534
535                 case JAVA_DLOAD_0:
536                 case JAVA_DLOAD_1:
537                 case JAVA_DLOAD_2:
538                 case JAVA_DLOAD_3:
539                         OP1LOAD(ICMD_DLOAD, opcode - JAVA_DLOAD_0 + firstlocal);
540                         break;
541
542                 case JAVA_ALOAD_0:
543                 case JAVA_ALOAD_1:
544                 case JAVA_ALOAD_2:
545                 case JAVA_ALOAD_3:
546                         OP1LOAD(ICMD_ALOAD, opcode - JAVA_ALOAD_0 + firstlocal);
547                         break;
548
549                         /* storing stack values into local variables */
550
551                 case JAVA_ISTORE:
552                 case JAVA_LSTORE:
553                 case JAVA_FSTORE:
554                 case JAVA_DSTORE:
555                 case JAVA_ASTORE:
556                         if (!iswide) {
557                                 i = code_get_u1(p + 1,inline_env->method);
558                         } else {
559                                 i = code_get_u2(p + 1,inline_env->method);
560                                 iswide = false;
561                                 nextp = p + 3;
562                         }
563                         OP1STORE(opcode, i + firstlocal);
564                         break;
565
566                 case JAVA_ISTORE_0:
567                 case JAVA_ISTORE_1:
568                 case JAVA_ISTORE_2:
569                 case JAVA_ISTORE_3:
570                         OP1STORE(ICMD_ISTORE, opcode - JAVA_ISTORE_0 + firstlocal);
571                         break;
572
573                 case JAVA_LSTORE_0:
574                 case JAVA_LSTORE_1:
575                 case JAVA_LSTORE_2:
576                 case JAVA_LSTORE_3:
577                         OP1STORE(ICMD_LSTORE, opcode - JAVA_LSTORE_0 + firstlocal);
578                         break;
579
580                 case JAVA_FSTORE_0:
581                 case JAVA_FSTORE_1:
582                 case JAVA_FSTORE_2:
583                 case JAVA_FSTORE_3:
584                         OP1STORE(ICMD_FSTORE, opcode - JAVA_FSTORE_0 + firstlocal);
585                         break;
586
587                 case JAVA_DSTORE_0:
588                 case JAVA_DSTORE_1:
589                 case JAVA_DSTORE_2:
590                 case JAVA_DSTORE_3:
591                         OP1STORE(ICMD_DSTORE, opcode - JAVA_DSTORE_0 + firstlocal);
592                         break;
593
594                 case JAVA_ASTORE_0:
595                 case JAVA_ASTORE_1:
596                 case JAVA_ASTORE_2:
597                 case JAVA_ASTORE_3:
598                         OP1STORE(ICMD_ASTORE, opcode - JAVA_ASTORE_0 + firstlocal);
599                         break;
600
601                 case JAVA_IINC:
602                         {
603                                 int v;
604                                 
605                                 if (!iswide) {
606                                         i = code_get_u1(p + 1,inline_env->method);
607                                         v = code_get_s1(p + 2,inline_env->method);
608
609                                 } else {
610                                         i = code_get_u2(p + 1,inline_env->method);
611                                         v = code_get_s2(p + 3,inline_env->method);
612                                         iswide = false;
613                                         nextp = p + 5;
614                                 }
615                                 INDEX_ONEWORD(i + firstlocal);
616                                 OP2I(opcode, i + firstlocal, v);
617                         }
618                         break;
619
620                         /* wider index for loading, storing and incrementing */
621
622                 case JAVA_WIDE:
623                         iswide = true;
624                         nextp = p + 1;
625                         break;
626
627                         /* managing arrays ************************************************/
628
629                 case JAVA_NEWARRAY:
630                         OP(ICMD_CHECKASIZE);
631                         switch (code_get_s1(p + 1, inline_env->method)) {
632                         case 4:
633                                 BUILTIN1(BUILTIN_newarray_boolean, TYPE_ADR, currentline);
634                                 break;
635                         case 5:
636                                 BUILTIN1(BUILTIN_newarray_char, TYPE_ADR, currentline);
637                                 break;
638                         case 6:
639                                 BUILTIN1(BUILTIN_newarray_float, TYPE_ADR, currentline);
640                                 break;
641                         case 7:
642                                 BUILTIN1(BUILTIN_newarray_double, TYPE_ADR, currentline);
643                                 break;
644                         case 8:
645                                 BUILTIN1(BUILTIN_newarray_byte, TYPE_ADR, currentline);
646                                 break;
647                         case 9:
648                                 BUILTIN1(BUILTIN_newarray_short, TYPE_ADR, currentline);
649                                 break;
650                         case 10:
651                                 BUILTIN1(BUILTIN_newarray_int, TYPE_ADR, currentline);
652                                 break;
653                         case 11:
654                                 BUILTIN1(BUILTIN_newarray_long, TYPE_ADR, currentline);
655                                 break;
656                         default: panic("Invalid array-type to create");
657                         }
658                         OP(ICMD_CHECKEXCEPTION);
659                         break;
660
661                 case JAVA_ANEWARRAY:
662                         OP(ICMD_CHECKASIZE);
663                         i = code_get_u2(p + 1, inline_env->method);
664                         {
665                                 classinfo *component;
666                                 constant_classref *compr;
667                                 constant_classref *cr;
668                                 classinfo         *c;
669
670 #if defined(__X86_64__) || defined(__I386__) || defined(__ALPHA__)
671                                 compr = (constant_classref *) class_getconstant(inline_env->method->class, i, CONSTANT_Class);
672
673                                 if (!(cr = class_get_classref_multiarray_of(1, compr)))
674                                         return NULL;
675
676                                 if (!resolve_classref(inline_env->method, cr, resolveLazy, true, &c))
677                                         return NULL;
678
679                                 if (c) {
680                                         LOADCONST_A_BUILTIN(c->vftbl);
681                                         BUILTIN2T(BUILTIN_newarray, TYPE_ADR, NULL, currentline);
682
683                                 } else {
684                                         LOADCONST_A_BUILTIN(cr);
685                                         BUILTIN2T(PATCHER_builtin_newarray, TYPE_ADR, cr, currentline);
686                                 }
687                                 s_count++;
688 #else
689                                 cr = (constant_classref *) class_getconstant(inline_env->method->class, i, CONSTANT_Class);
690
691                                 if (!resolve_classref(inline_env->method,
692                                                         cr,resolveEager,true,&component))
693                                         return NULL;
694
695                                 c = class_array_of(component,true);
696                                 if (!c)
697                                         return NULL;
698                                 LOADCONST_A_BUILTIN(c->vftbl);
699                                 s_count++;
700                                 BUILTIN2(BUILTIN_newarray, TYPE_ADR, currentline);
701 #endif
702                         }
703                         OP(ICMD_CHECKEXCEPTION);
704                         break;
705
706                 case JAVA_MULTIANEWARRAY:
707                         inline_env->method->isleafmethod = false;
708                         i = code_get_u2(p + 1, inline_env->method);
709                         {
710                                 classinfo *component;
711                                 classinfo *c;
712                             constant_classref *cr;
713                                 vftbl_t *arrayvftbl;
714                                 s4 v = code_get_u1(p + 3, inline_env->method);
715
716 #if defined(__X86_64__) || defined(__I386__) || defined(__ALPHA__)
717                                 cr = (constant_classref *) class_getconstant(inline_env->method->class, i, CONSTANT_Class);
718
719                                 if (!resolve_classref(inline_env->method, cr, resolveLazy, true, &c))
720                                         return NULL;
721
722                                 if (c) {
723                                         OP2AT(opcode, v, c->vftbl, NULL, currentline);
724
725                                 } else {
726                                         OP2AT(opcode, v, cr, PATCHER_builtin_multianewarray, currentline);
727                                 }
728 #else
729 /*                              vftbl *arrayvftbl = */
730 /*                                      ((classinfo *) class_getconstant(class, i, CONSTANT_Class))->vftbl; */
731 /*                              OP2A(opcode, v, arrayvftbl,currentline); */
732
733                                 
734                                 cr = (constant_classref *) class_getconstant(inline_env->method->class, i, CONSTANT_Class);
735
736                                 if (!resolve_classref_or_classinfo(inline_env->method,
737                                                         CLASSREF_OR_CLASSINFO(cr),resolveEager,true,&component))
738                                         return NULL;
739
740                                 arrayvftbl = component->vftbl;
741                                 OP2A(opcode, v, arrayvftbl, currentline);
742
743 /*                              classinfo *arrayclass = */
744 /*                                      (classinfo *) class_getconstant(class, i, CONSTANT_Class); */
745 /*                              OP2A(opcode, v, arrayclass, currentline); */
746 #endif
747                         }
748                         break;
749
750                 case JAVA_IFEQ:
751                 case JAVA_IFLT:
752                 case JAVA_IFLE:
753                 case JAVA_IFNE:
754                 case JAVA_IFGT:
755                 case JAVA_IFGE:
756                 case JAVA_IFNULL:
757                 case JAVA_IFNONNULL:
758                 case JAVA_IF_ICMPEQ:
759                 case JAVA_IF_ICMPNE:
760                 case JAVA_IF_ICMPLT:
761                 case JAVA_IF_ICMPGT:
762                 case JAVA_IF_ICMPLE:
763                 case JAVA_IF_ICMPGE:
764                 case JAVA_IF_ACMPEQ:
765                 case JAVA_IF_ACMPNE:
766                 case JAVA_GOTO:
767                 case JAVA_JSR:
768                         i = p + code_get_s2(p + 1,inline_env->method);
769                         if (useinlining) { 
770                                 debug_writebranch;
771                                 i = label_index[i];
772                         }
773                         bound_check(i);
774                         block_insert(i);
775                         blockend = true;
776                         OP1(opcode, i);
777                         break;
778                 case JAVA_GOTO_W:
779                 case JAVA_JSR_W:
780                         i = p + code_get_s4(p + 1,inline_env->method);
781                         if (useinlining) { 
782                                 debug_writebranch;
783                                 i = label_index[i];
784                         }
785                         bound_check(i);
786                         /*printf("B6 JSR_W\t"); fflush(stdout);*/
787                         block_insert(i);
788                         blockend = true;
789                         OP1(opcode, i);
790                         break;
791
792                 case JAVA_RET:
793                         if (!iswide) {
794                                 i = code_get_u1(p + 1,inline_env->method);
795                         } else {
796                                 i = code_get_u2(p + 1,inline_env->method);
797                                 nextp = p + 3;
798                                 iswide = false;
799                         }
800                         blockend = true;
801                                 
802                         /*
803                           if (inline_env->isinlinedmethod) {
804                           OP1(ICMD_GOTO, inlinfo->stopgp);
805                           break;
806                           }*/
807
808                         OP1LOAD(opcode, i + firstlocal);
809                         break;
810
811                 case JAVA_IRETURN:
812                 case JAVA_LRETURN:
813                 case JAVA_FRETURN:
814                 case JAVA_DRETURN:
815                 case JAVA_ARETURN:
816                 case JAVA_RETURN:
817                         if (inline_env->isinlinedmethod) {
818                                 /*                                      if (p==m->jcodelength-1) {*/ /* return is at end of inlined method */
819                                 /*                                              OP(ICMD_NOP); */
820                                 /*                                              break; */
821                                 /*                                      } */
822                                 if (nextp>inline_env->method->jcodelength-1) {
823                                         /* OP1(ICMD_GOTO, inlinfo->stopgp);
824                                            OP(ICMD_NOP);
825                                            OP(ICMD_NOP);
826                                         */
827                                         blockend=true;
828                                         break;
829                                 } /* JJJJJJJ */
830                                 blockend = true;
831                                 OP1(ICMD_GOTO, inlinfo->stopgp);
832                                 break;
833                         }
834
835                         blockend = true;
836                         OP(opcode);
837                         break;
838
839                 case JAVA_ATHROW:
840                         blockend = true;
841                         OP(opcode);
842                         break;
843                                 
844
845                         /* table jumps ********************************/
846
847                 case JAVA_LOOKUPSWITCH:
848                         {
849                                 s4 num, j;
850                                 s4 *tablep;
851                                 s4 prevvalue=0;
852
853                                 blockend = true;
854                                 nextp = ALIGN((p + 1), 4);
855                                 if (nextp + 8 > inline_env->method->jcodelength)
856                                         panic("Unexpected end of bytecode");
857                                 if (!useinlining) {
858                                         tablep = (s4 *) (inline_env->method->jcode + nextp);
859
860                                 } else {
861                                         num = code_get_u4(nextp + 4,inline_env->method);
862                                         tablep = DMNEW(s4, num * 2 + 2);
863                                 }
864
865                                 OP2A(opcode, 0, tablep,currentline);
866
867                                 /* default target */
868
869                                 j =  p + code_get_s4(nextp,inline_env->method);
870                                 if (useinlining) 
871                                         j = label_index[j];
872                                 *tablep = j;     /* restore for little endian */
873                                 tablep++;
874                                 nextp += 4;
875                                 bound_check(j);
876                                 block_insert(j);
877
878                                 /* number of pairs */
879
880                                 num = code_get_u4(nextp,inline_env->method);
881                                 *tablep = num;
882                                 tablep++;
883                                 nextp += 4;
884
885                                 if (nextp + 8*(num) > inline_env->method->jcodelength)
886                                         panic("Unexpected end of bytecode");
887
888                                 for (i = 0; i < num; i++) {
889                                         /* value */
890
891                                         j = code_get_s4(nextp,inline_env->method);
892                                         *tablep = j; /* restore for little endian */
893                                         tablep++;
894                                         nextp += 4;
895
896                                         /* check if the lookup table is sorted correctly */
897                                         
898                                         if (i && (j <= prevvalue)) {
899                                                 *exceptionptr = new_verifyerror(m, "Unsorted lookup switch");
900                                                 return NULL;
901                                         }
902                                         prevvalue = j;
903
904                                         /* target */
905
906                                         j = p + code_get_s4(nextp,inline_env->method);
907                                         if (useinlining)
908                                                 j = label_index[j];
909                                         *tablep = j; /* restore for little endian */
910                                         tablep++;
911                                         nextp += 4;
912                                         bound_check(j);
913                                         block_insert(j);
914                                 }
915
916                                 break;
917                         }
918
919
920                 case JAVA_TABLESWITCH:
921                         {
922                                 s4 num, j;
923                                 s4 *tablep;
924
925                                 blockend = true;
926                                 nextp = ALIGN((p + 1), 4);
927                                 if (nextp + 12 > inline_env->method->jcodelength)
928                                         panic("Unexpected end of bytecode");
929                                 if (!useinlining) {
930                                         tablep = (s4 *) (inline_env->method->jcode + nextp);
931
932                                 } else {
933                                         num = code_get_u4(nextp + 8,inline_env->method) - code_get_u4(nextp + 4,inline_env->method);
934                                         tablep = DMNEW(s4, num + 1 + 3);
935                                 }
936
937                                 OP2A(opcode, 0, tablep,currentline);
938
939                                 /* default target */
940
941                                 j = p + code_get_s4(nextp,inline_env->method);
942                                 if (useinlining)
943                                         j = label_index[j];
944                                 *tablep = j;     /* restore for little endian */
945                                 tablep++;
946                                 nextp += 4;
947                                 bound_check(j);
948                                 block_insert(j);
949
950                                 /* lower bound */
951
952                                 j = code_get_s4(nextp,inline_env->method);
953                                 *tablep = j;     /* restore for little endian */
954                                 tablep++;
955                                 nextp += 4;
956
957                                 /* upper bound */
958
959                                 num = code_get_s4(nextp,inline_env->method);
960                                 *tablep = num;   /* restore for little endian */
961                                 tablep++;
962                                 nextp += 4;
963
964                                 num -= j;  /* difference of upper - lower */
965                                 if (num < 0)
966                                         panic("invalid TABLESWITCH: upper bound < lower bound");
967
968                                 if (nextp + 4*(num+1) > inline_env->method->jcodelength)
969                                         panic("Unexpected end of bytecode");
970
971                                 for (i = 0; i <= num; i++) {
972                                         j = p + code_get_s4(nextp,inline_env->method);
973                                         if (useinlining) {
974                                                 /*printf("TABLESWITCH: j before mapping=%ld\n",j);*/
975                                                 j = label_index[j];
976                                         }
977                                         *tablep = j; /* restore for little endian */
978                                         tablep++;
979                                         nextp += 4;
980                                         bound_check(j);
981                                         block_insert(j);
982                                         /*printf("TABLESWITCH: block_insert(%ld)\n",j);*/
983                                 }
984
985                                 break;
986                         }
987
988
989                         /* load and store of object fields *******************/
990
991                 case JAVA_AASTORE:
992                         BUILTIN3(BUILTIN_aastore, TYPE_VOID, currentline);
993                         break;
994
995                 case JAVA_GETSTATIC:
996                 case JAVA_PUTSTATIC:
997 #if defined(__X86_64__) || defined(__I386__) || defined(__ALPHA__)
998                 case JAVA_GETFIELD:
999                 case JAVA_PUTFIELD:
1000 #endif
1001                         i = code_get_u2(p + 1, inline_env->method);
1002                         {
1003                                 constant_FMIref  *fr;
1004                                 unresolved_field *uf;
1005                                 fieldinfo        *fi;
1006                                 classinfo        *c;
1007
1008                                 fr = class_getconstant(inline_env->method->class, i, CONSTANT_Fieldref);
1009 #if defined(__X86_64__) || defined(__I386__) || defined(__ALPHA__)
1010                                 OP2A_NOINC(opcode, fr->parseddesc.fd->type, fr, currentline);
1011
1012                                 if (!(uf = create_unresolved_field(inline_env->method->class,
1013                                                                                                    inline_env->method,
1014                                                                                                    iptr,
1015                                                                                                    NULL)))
1016                                         return NULL;
1017
1018                                 /* store unresolved_field pointer */
1019
1020                                 iptr->target = uf;
1021
1022                                 /* only with -noverify, otherwise the typechecker does this */
1023
1024                                 if (!opt_verify) {
1025                                         if (!resolve_field(uf, resolveLazy, &fi))
1026                                                 return NULL;
1027
1028                                         iptr->val.a = fi;
1029                                 }
1030                                 PINC;
1031 #else
1032                                 {
1033                                 classinfo *frclass;
1034                                 if (!resolve_classref(inline_env->method,fr->classref,resolveEager,true,&frclass))
1035                                         return NULL;
1036
1037                                 fi = class_resolvefield(frclass,
1038                                                                                 fr->name,
1039                                                                                 fr->descriptor,
1040                                                                                 inline_env->method->class,
1041                                                                                 true);
1042
1043                                 if (!fi)
1044                                         return NULL;
1045
1046                                 OP2A(opcode, fi->type, fi, currentline);
1047                                 if (!fi->class->initialized) {
1048                                         inline_env->method->isleafmethod = false;
1049                                 }
1050                                 }
1051 #endif
1052                         }
1053                         break;
1054
1055 #if !defined(__X86_64__) && !defined(__I386__) && !defined(__ALPHA__)
1056                 case JAVA_PUTFIELD:
1057                 case JAVA_GETFIELD:
1058                         i = code_get_u2(p + 1,inline_env->method);
1059                         {
1060                                 constant_FMIref *fr;
1061                                 fieldinfo *fi;
1062                                 classinfo *frclass;
1063
1064                                 fr = class_getconstant(inline_env->method->class, i, CONSTANT_Fieldref);
1065                                 if (!resolve_classref(inline_env->method,fr->classref,resolveEager,true,&frclass))
1066                                         return NULL;
1067
1068                                 fi = class_resolvefield(frclass,
1069                                                                                 fr->name,
1070                                                                                 fr->descriptor,
1071                                                                                 inline_env->method->class,
1072                                                                                 true);
1073
1074                                 if (!fi)
1075                                         return NULL;
1076
1077                                 OP2A(opcode, fi->type, fi, currentline);
1078                         }
1079                         break;
1080 #endif
1081
1082
1083                         /* method invocation *****/
1084
1085                 case JAVA_INVOKESTATIC:
1086                         i = code_get_u2(p + 1, inline_env->method);
1087                         {
1088                                 constant_FMIref *mr;
1089                                 methodinfo *mi;
1090                                 unresolved_method *um;
1091                                 classinfo *mrclass;
1092
1093                                 inline_env->method->isleafmethod = false;
1094
1095                                 mr = class_getconstant(inline_env->method->class, i, CONSTANT_Methodref);
1096 #if defined(__X86_64__) || defined(__I386__) || defined(__ALPHA__)
1097                                 OP2A_NOINC(opcode, mr->parseddesc.md->paramcount, mr, currentline);
1098
1099                                 um = create_unresolved_method(inline_env->method->class,
1100                                                                                           inline_env->method,
1101                                                                                           iptr,
1102                                                                                           NULL);
1103
1104                                 if (!um)
1105                                         return NULL;
1106
1107                                 /* store the unresolved_method pointer */
1108
1109                                 iptr->target = um;
1110
1111                                 /* only with -noverify, otherwise the typechecker does this */
1112
1113                                 if (!opt_verify) {
1114                                         if (!resolve_method(um, resolveLazy, &mi))
1115                                                 return NULL;
1116
1117                                         iptr->val.a = mi;
1118                                 }
1119                                 PINC;
1120 #else
1121                                 if (!resolve_classref(inline_env->method,mr->classref,resolveEager,true,&mrclass))
1122                                         return NULL;
1123
1124                                 mi = class_resolveclassmethod(mrclass,
1125                                                                                           mr->name,
1126                                                                                           mr->descriptor,
1127                                                                                           inline_env->method->class,
1128                                                                                           true);
1129
1130                                 if (!mi)
1131                                         return NULL;
1132
1133 if (DEBUG4==true) { 
1134         method_display_w_class(mi); 
1135         printf("\tINVOKE STAT\n");
1136         fflush(stdout);}
1137
1138                                 if (!(mi->flags & ACC_STATIC)) {
1139                                         *exceptionptr =
1140                                                 new_exception(string_java_lang_IncompatibleClassChangeError);
1141                                         return NULL;
1142                                 }
1143
1144                                 method_descriptor2types(mi);
1145                                 OP2A(opcode, mi->paramcount, mi, currentline);
1146 #endif
1147                         }
1148                         break;
1149
1150                 case JAVA_INVOKESPECIAL:
1151                 case JAVA_INVOKEVIRTUAL:
1152                         i = code_get_u2(p + 1, inline_env->method);
1153                         {
1154                                 constant_FMIref *mr;
1155                                 methodinfo *mi;
1156                                 unresolved_method *um;
1157                                 classinfo *mrclass;
1158
1159                                 inline_env->method->isleafmethod = false;
1160
1161                                 mr = class_getconstant(inline_env->method->class, i, CONSTANT_Methodref);
1162 #if defined(__X86_64__) || defined(__I386__) || defined(__ALPHA__)
1163                                 OP2A_NOINC(opcode, mr->parseddesc.md->paramcount + 1, mr, currentline);
1164
1165                                 um = create_unresolved_method(inline_env->method->class,
1166                                                                                           inline_env->method,
1167                                                                                           iptr,
1168                                                                                           NULL);
1169
1170                                 if (!um)
1171                                         return NULL;
1172
1173                                 /* store the unresolved_method* */
1174
1175                                 iptr->target = um;
1176
1177                                 /* only with -noverify, otherwise the typechecker does this */
1178
1179                                 if (!opt_verify) {
1180                                         if (!resolve_method(um, resolveLazy, &mi))
1181                                                 return NULL;
1182
1183                                         iptr->val.a = mi;
1184                                 }
1185                                 PINC;
1186 #else
1187                                 if (!resolve_classref(inline_env->method,mr->classref,resolveEager,true,&mrclass))
1188                                         return NULL;
1189
1190                                 mi = class_resolveclassmethod(mrclass,
1191                                                                                           mr->name,
1192                                                                                           mr->descriptor,
1193                                                                                           inline_env->method->class,
1194                                                                                           true);
1195
1196                                 if (!mi)
1197                                         return NULL;
1198
1199 if (DEBUG4==true) { 
1200         method_display_w_class(mi); 
1201         printf("\tINVOKE SPEC/VIRT\n");
1202         fflush(stdout);}
1203
1204                                 if (mi->flags & ACC_STATIC) {
1205                                         *exceptionptr =
1206                                                 new_exception(string_java_lang_IncompatibleClassChangeError);
1207                                         return NULL;
1208                                 }
1209
1210                                 method_descriptor2types(mi);
1211                                 OP2A(opcode, mi->paramcount, mi, currentline);
1212 #endif
1213                         }
1214                         break;
1215
1216                 case JAVA_INVOKEINTERFACE:
1217                         i = code_get_u2(p + 1,inline_env->method);
1218                         {
1219                                 constant_FMIref *mr;
1220                                 methodinfo *mi;
1221                                 classinfo *mrclass;
1222                                 unresolved_method *um;
1223                                 
1224                                 inline_env->method->isleafmethod = false;
1225
1226                                 mr = class_getconstant(inline_env->method->class, i, CONSTANT_InterfaceMethodref);
1227 #if defined(__X86_64__) || defined(__I386__) || defined(__ALPHA__)
1228                                 OP2A_NOINC(opcode, mr->parseddesc.md->paramcount + 1, mr, currentline);
1229
1230                                 um = create_unresolved_method(inline_env->method->class,
1231                                                                                           inline_env->method,
1232                                                                                           iptr,
1233                                                                                           NULL);
1234
1235                                 if (!um)
1236                                         return NULL;
1237
1238                                 /* store the unresolved_method* */
1239
1240                                 iptr->target = um;
1241
1242                                 /* only with -noverify, otherwise the typechecker does this */
1243
1244                                 if (!opt_verify) {
1245                                         if (!resolve_method(um, resolveLazy, &mi))
1246                                                 return NULL;
1247
1248                                         iptr->val.a = mi;
1249                                 }
1250                                 PINC;
1251 #else
1252                                 if (!resolve_classref(inline_env->method,mr->classref,resolveEager,true,&mrclass))
1253                                         return NULL;
1254
1255                                 mi = class_resolveinterfacemethod(mrclass,
1256                                                                                                   mr->name,
1257                                                                                                   mr->descriptor,
1258                                                                                                   inline_env->method->class,
1259                                                                                                   true);
1260                                 if (!mi)
1261                                         return NULL;
1262
1263                                 if (mi->flags & ACC_STATIC) {
1264                                         *exceptionptr =
1265                                                 new_exception(string_java_lang_IncompatibleClassChangeError);
1266                                         return NULL;
1267                                 }
1268
1269 if (DEBUG4==true) { 
1270         method_display_w_class(mi); 
1271         printf("\tINVOKE INTERFACE\n");
1272         fflush(stdout);}
1273
1274                                 method_descriptor2types(mi);
1275                                 OP2A(opcode, mi->paramcount, mi, currentline);
1276 #endif
1277                         }
1278                         break;
1279
1280                         /* miscellaneous object operations *******/
1281
1282                 case JAVA_NEW:
1283                         {
1284                                 constant_classref *cr;
1285                                 classinfo         *cls;
1286                                 
1287 #if defined(__X86_64__) || defined(__I386__) || defined(__ALPHA__)
1288                                 i = code_get_u2(p + 1, inline_env->method);
1289                                 cr = (constant_classref *) class_getconstant(inline_env->method->class, i, CONSTANT_Class);
1290
1291                                 if (!resolve_classref(inline_env->method, cr, resolveLazy, true, &cls))
1292                                         return NULL;
1293
1294                                 /* <clinit> can throw an exception over native code */
1295
1296                                 if (cls && cls->initialized) {
1297                                         LOADCONST_A_BUILTIN(cls);
1298                                         BUILTIN1T(BUILTIN_new, TYPE_ADR, NULL, currentline);
1299
1300                                 } else {
1301                                         LOADCONST_A_BUILTIN(cr);
1302                                         BUILTIN1T(PATCHER_builtin_new, TYPE_ADR, cr, currentline);
1303                                 }
1304
1305                                 s_count++;
1306                                 OP(ICMD_CHECKEXCEPTION);
1307 #else
1308                                 i = code_get_u2(p + 1,inline_env->method);
1309                                 cr = (constant_classref *) class_getconstant(inline_env->method->class, i, CONSTANT_Class);
1310                                 if (!resolve_classref(inline_env->method,cr,resolveEager,true,&cls))
1311                                         return NULL;
1312                                 LOADCONST_A_BUILTIN(cls);
1313                                 s_count++;
1314                                 BUILTIN1(BUILTIN_new, TYPE_ADR, currentline);
1315                                 OP(ICMD_CHECKEXCEPTION);
1316 #endif
1317                         }
1318                         break;
1319
1320                 case JAVA_CHECKCAST:
1321                         i = code_get_u2(p + 1, inline_env->method);
1322                         {
1323                                 constant_classref *cr;
1324                                 classinfo *cls;
1325                                 
1326                                 cr = (constant_classref *) class_getconstant(inline_env->method->class, i, CONSTANT_Class);
1327
1328 #if defined(__X86_64__) || defined(__I386__) || defined(__ALPHA__)
1329                                 if (!resolve_classref(inline_env->method, cr, resolveLazy, true, &cls))
1330                                         return NULL;
1331
1332                                 if (cr->name->text[0] == '[') {
1333                                         /* array type cast-check */
1334                                         if (cls) {
1335                                                 LOADCONST_A_BUILTIN(cls->vftbl);
1336                                                 BUILTIN2T(BUILTIN_checkarraycast, TYPE_ADR, NULL, currentline);
1337
1338                                         } else {
1339                                                 LOADCONST_A_BUILTIN(cr);
1340                                                 BUILTIN2T(PATCHER_builtin_checkarraycast, TYPE_ADR, cr, currentline);
1341                                         }
1342                                         s_count++;
1343
1344                                 } else {
1345                                         /* object type cast-check */
1346                                         OP2AT(opcode, 1, cls, cr, currentline);
1347                                 }
1348 #else
1349                                 if (!resolve_classref(inline_env->method,
1350                                                         cr,resolveEager,true,&cls))
1351                                         return NULL;
1352
1353                                 if (cls->vftbl->arraydesc) {
1354                                         /* array type cast-check */
1355                                         LOADCONST_A_BUILTIN(cls->vftbl);
1356                                         s_count++;
1357                                         BUILTIN2(BUILTIN_checkarraycast, TYPE_ADR,currentline);
1358
1359                                 } else { /* object type cast-check */
1360                                         /*
1361                                           +                                               LOADCONST_A_BUILTIN(class_getconstant(class, i, CONSTANT_Class));
1362                                           +                                               s_count++;
1363                                           +                                               BUILTIN2(BUILTIN_checkcast, TYPE_ADR,currentline);
1364                                           +                                             */
1365                                         OP2A(opcode, 1, cls, currentline);
1366                                 }
1367 #endif
1368                         }
1369                         break;
1370
1371                 case JAVA_INSTANCEOF:
1372                         i = code_get_u2(p + 1,inline_env->method);
1373                         {
1374                                 constant_classref *cr;
1375                                 classinfo *cls;
1376                                 
1377                                 cr = (constant_classref *) class_getconstant(inline_env->method->class, i, CONSTANT_Class);
1378
1379 #if defined(__X86_64__) || defined(__I386__) || defined(__ALPHA__)
1380                                 if (!resolve_classref(inline_env->method, cr, resolveLazy, true, &cls))
1381                                         return NULL;
1382
1383                                 if (cr->name->text[0] == '[') {
1384                                         /* array type cast-check */
1385                                         if (cls) {
1386                                                 LOADCONST_A_BUILTIN(cls->vftbl);
1387                                                 BUILTIN2T(BUILTIN_arrayinstanceof, TYPE_INT, NULL, currentline);
1388
1389                                         } else {
1390                                                 LOADCONST_A_BUILTIN(cr);
1391                                                 BUILTIN2T(PATCHER_builtin_arrayinstanceof, TYPE_INT, cr, currentline);
1392                                         }
1393                                         s_count++;
1394
1395                                 } else {
1396                                         /* object type cast-check */
1397                                         OP2AT(opcode, 1, cls, cr, currentline);
1398                                 }
1399 #else
1400                                 if (!resolve_classref(inline_env->method,
1401                                                         cr,resolveEager,true,&cls))
1402                                         return NULL;
1403
1404                                 if (cls->vftbl->arraydesc) {
1405                                         /* array type cast-check */
1406                                         LOADCONST_A_BUILTIN(cls->vftbl);
1407                                         s_count++;
1408                                         BUILTIN2(BUILTIN_arrayinstanceof, TYPE_INT, currentline);
1409                                 }
1410                                 else { /* object type cast-check */
1411                                         /*
1412                                           LOADCONST_A_BUILTIN(class_getconstant(class, i, CONSTANT_Class));
1413                                           s_count++;
1414                                           BUILTIN2(BUILTIN_instanceof, TYPE_INT,currentline);
1415                                           +                                             */
1416                                         OP2A(opcode, 1, cls, currentline);
1417                                 }
1418 #endif
1419                         }
1420                         break;
1421
1422                 case JAVA_MONITORENTER:
1423 #if defined(USE_THREADS)
1424                         if (checksync) {
1425                                 OP(ICMD_CHECKNULL);
1426                                 BUILTIN1(BUILTIN_monitorenter, TYPE_VOID, currentline);
1427                         } else
1428 #endif
1429                                 {
1430                                         OP(ICMD_CHECKNULL);
1431                                         OP(ICMD_POP);
1432                                 }
1433                         break;
1434
1435                 case JAVA_MONITOREXIT:
1436 #if defined(USE_THREADS)
1437                         if (checksync) {
1438                                 BUILTIN1(BUILTIN_monitorexit, TYPE_VOID, currentline);
1439                         } else
1440 #endif
1441                                 {
1442                                         OP(ICMD_POP);
1443                                 }
1444                         break;
1445
1446                         /* any other basic operation **************************************/
1447
1448                 case JAVA_IDIV:
1449                         OP(opcode);
1450                         break;
1451
1452                 case JAVA_IREM:
1453                         OP(opcode);
1454                         break;
1455
1456                 case JAVA_LDIV:
1457                         OP(opcode);
1458                         break;
1459
1460                 case JAVA_LREM:
1461                         OP(opcode);
1462                         break;
1463
1464                 case JAVA_FREM:
1465 #if defined(__I386__)
1466                         OP(opcode);
1467 #else
1468                         BUILTIN2(BUILTIN_frem, TYPE_FLOAT,currentline);
1469 #endif
1470                         break;
1471
1472                 case JAVA_DREM:
1473 #if defined(__I386__)
1474                         OP(opcode);
1475 #else
1476                         BUILTIN2(BUILTIN_drem, TYPE_DOUBLE,currentline);
1477 #endif
1478                         break;
1479
1480                 case JAVA_F2I:
1481 #if defined(__ALPHA__)
1482                         if (!opt_noieee) {
1483                                 BUILTIN1(BUILTIN_f2i, TYPE_INT,currentline);
1484                         } else
1485 #endif
1486                                 {
1487                                         OP(opcode);
1488                                 }
1489                         break;
1490
1491                 case JAVA_F2L:
1492 #if defined(__ALPHA__)
1493                         if (!opt_noieee) {
1494                                 BUILTIN1(BUILTIN_f2l, TYPE_LONG,currentline);
1495                         } else 
1496 #endif
1497                                 {
1498                                         OP(opcode);
1499                                 }
1500                         break;
1501
1502                 case JAVA_D2I:
1503 #if defined(__ALPHA__)
1504                         if (!opt_noieee) {
1505                                 BUILTIN1(BUILTIN_d2i, TYPE_INT,currentline);
1506                         } else
1507 #endif
1508                                 {
1509                                         OP(opcode);
1510                                 }
1511                         break;
1512
1513                 case JAVA_D2L:
1514 #if defined(__ALPHA__)
1515                         if (!opt_noieee) {
1516                                 BUILTIN1(BUILTIN_d2l, TYPE_LONG,currentline);
1517                         } else
1518 #endif
1519                                 {
1520                                         OP(opcode);
1521                                 }
1522                         break;
1523
1524                 case JAVA_BREAKPOINT:
1525                         *exceptionptr =
1526                                 new_verifyerror(m, "Quick instructions shouldn't appear yet.");
1527                         return NULL;
1528
1529                 case 204: /* unused opcode */
1530                 case 205:
1531                 case 206:
1532                 case 207:
1533                 case 208:
1534                 case 209:
1535                 case 210:
1536                 case 211:
1537                 case 212:
1538                 case 213:
1539                 case 214:
1540                 case 215:
1541                 case 216:
1542                 case 217:
1543                 case 218:
1544                 case 219:
1545                 case 220:
1546                 case 221:
1547                 case 222:
1548                 case 223:
1549                 case 224:
1550                 case 225:
1551                 case 226:
1552                 case 227:
1553                 case 228:
1554                 case 229:
1555                 case 230:
1556                 case 231:
1557                 case 232:
1558                 case 233:
1559                 case 234:
1560                 case 235:
1561                 case 236:
1562                 case 237:
1563                 case 238:
1564                 case 239:
1565                 case 240:
1566                 case 241:
1567                 case 242:
1568                 case 243:
1569                 case 244:
1570                 case 245:
1571                 case 246:
1572                 case 247:
1573                 case 248:
1574                 case 249:
1575                 case 250:
1576                 case 251:
1577                 case 252:
1578                 case 253:
1579                 case 254:
1580                 case 255:
1581                         printf("Illegal opcode %d at instr %d\n", opcode, ipc);
1582                         panic("Illegal opcode encountered");
1583                         break;
1584
1585                 default:
1586                         OP(opcode);
1587                         break;
1588                                 
1589                 } /* end switch */
1590
1591                 /* If WIDE was used correctly, iswide should have been reset by now. */
1592                 if (iswide && opcode != JAVA_WIDE)
1593                         panic("Illegal instruction: WIDE before incompatible opcode");
1594                 
1595                 /* INLINING */
1596                   
1597                 /* if (inline_env->isinlinedmethod && p == inline_env->method->jcodelength - 1) { */ /* end of an inlined method */
1598                 if (inline_env->isinlinedmethod && (nextp >= inline_env->method->jcodelength) ) { /* end of an inlined method */
1599                         /*                printf("setting gp from %d to %d\n",gp, inlinfo->stopgp); */
1600                         gp = inlinfo->stopgp; 
1601                         inlining_restore_compiler_variables();
1602                         OP(ICMD_INLINE_END);
1603 /*label_index = inlinfo->label_index;*/
1604
1605 METHINFOt(inline_env->method,"AFTER RESTORE : ",DEBUG);
1606                         list_remove(inlinfo->inlinedmethods, list_first(inlinfo->inlinedmethods));
1607                         if (inlinfo->inlinedmethods == NULL) { /* JJJJ */
1608                                 nextgp = -1;
1609                         } else {
1610                                 tmpinlinf = list_first(inlinfo->inlinedmethods);
1611                                 nextgp = (tmpinlinf != NULL) ? tmpinlinf->startgp : -1;
1612                         }
1613                         /*                printf("nextpgp: %d\n", nextgp); */
1614                         label_index=inlinfo->label_index;
1615                         firstlocal = inlinfo->firstlocal;
1616                 }
1617
1618         } /* end for */
1619
1620
1621         if (p != m->jcodelength) {
1622                 printf("p (%d) != m->jcodelength (%d)\n",p,m->jcodelength);
1623                 panic("Command-sequence crosses code-boundary");
1624         }
1625         if (!blockend) {
1626                 *exceptionptr = new_verifyerror(m, "Falling off the end of the code");
1627                 return NULL;
1628         }
1629
1630         /* adjust block count if target 0 is not first intermediate instruction */
1631
1632         if (!m->basicblockindex[0] || (m->basicblockindex[0] > 1))
1633                 b_count++;
1634
1635         /* copy local to method variables */
1636
1637         m->instructioncount = ipc;
1638         m->basicblockcount = b_count;
1639         m->stackcount = s_count + m->basicblockcount * m->maxstack;
1640
1641         /* allocate stack table */
1642
1643         m->stack = DMNEW(stackelement, m->stackcount);
1644
1645         {
1646                 basicblock *bptr;
1647
1648                 bptr = m->basicblocks = DMNEW(basicblock, b_count + 1);    /* one more for end ipc */
1649
1650                 b_count = 0;
1651                 m->c_debug_nr = 0;
1652         
1653                 /* additional block if target 0 is not first intermediate instruction */
1654
1655                 if (!m->basicblockindex[0] || (m->basicblockindex[0] > 1)) {
1656                         bptr->iinstr = m->instructions;
1657                         bptr->mpc = -1;
1658                         bptr->flags = -1;
1659                         bptr->type = BBTYPE_STD;
1660                         bptr->branchrefs = NULL;
1661                         bptr->pre_count = 0;
1662                         bptr->debug_nr = m->c_debug_nr++;
1663                         bptr++;
1664                         b_count++;
1665                         (bptr - 1)->next = bptr;
1666                 }
1667
1668                 /* allocate blocks */
1669
1670                 for (p = 0; p < inline_env->cumjcodelength; p++) { 
1671                 /* for (p = 0; p < m->jcodelength; p++) { */
1672                         if (m->basicblockindex[p] & 1) {
1673                                 /* check if this block starts at the beginning of an instruction */
1674                                 if (!instructionstart[p]) {
1675                                         printf("Basic Block beginn: %d\n",p);
1676                                         panic("Branch into middle of instruction");
1677                                 }
1678                                 /* allocate the block */
1679                                 bptr->iinstr = m->instructions + (m->basicblockindex[p] >> 1);
1680                                 bptr->debug_nr = m->c_debug_nr++;
1681                                 if (b_count != 0)
1682                                         (bptr - 1)->icount = bptr->iinstr - (bptr - 1)->iinstr;
1683                                 bptr->mpc = -1;
1684                                 bptr->flags = -1;
1685                                 bptr->lflags = 0;
1686                                 bptr->type = BBTYPE_STD;
1687                                 bptr->branchrefs = NULL;
1688                                 m->basicblockindex[p] = b_count;
1689                                 bptr->pre_count = 0;
1690                                 bptr++;
1691                                 b_count++;
1692                                 (bptr - 1)->next = bptr;
1693                         }
1694                 }
1695
1696                 /* allocate additional block at end */
1697
1698                 bptr->instack = bptr->outstack = NULL;
1699                 bptr->indepth = bptr->outdepth = 0;
1700                 bptr->iinstr = NULL;
1701                 (bptr - 1)->icount = (m->instructions + m->instructioncount) - (bptr - 1)->iinstr;
1702                 bptr->icount = 0;
1703                 bptr->mpc = -1;
1704                 bptr->flags = -1;
1705                 bptr->lflags = 0;
1706                 bptr->type = BBTYPE_STD;
1707                 bptr->branchrefs = NULL;
1708                 bptr->pre_count = 0;
1709                 bptr->debug_nr = m->c_debug_nr++;
1710                 (bptr - 1)->next = bptr;
1711                 bptr->next = NULL;
1712
1713                 if (cd->exceptiontablelength > 0) {
1714                         cd->exceptiontable[cd->exceptiontablelength - 1].down = NULL;
1715                 }
1716                 
1717                 for (i = 0; i < cd->exceptiontablelength; ++i) {
1718                         p = cd->exceptiontable[i].startpc;
1719                         cd->exceptiontable[i].start = m->basicblocks + m->basicblockindex[p];
1720
1721                         p = cd->exceptiontable[i].endpc;
1722                         cd->exceptiontable[i].end = (p == inline_env->method->jcodelength) ? (m->basicblocks + m->basicblockcount /*+ 1*/) : (m->basicblocks + m->basicblockindex[p]);
1723
1724                         p = cd->exceptiontable[i].handlerpc;
1725                         cd->exceptiontable[i].handler = m->basicblocks + m->basicblockindex[p];
1726             }
1727         }
1728         
1729         if (useinlining) inlining_cleanup(inline_env);
1730
1731         /* just return methodinfo* to signal everything was ok */
1732
1733         return m;
1734 }
1735
1736
1737 /*
1738  * These are local overrides for various environment variables in Emacs.
1739  * Please do not remove this and leave it at the end of the file, where
1740  * Emacs will automagically detect them.
1741  * ---------------------------------------------------------------------
1742  * Local variables:
1743  * mode: c
1744  * indent-tabs-mode: t
1745  * c-basic-offset: 4
1746  * tab-width: 4
1747  * End:
1748  */
1749