* src/vm/jit/stack.c (IF_INTRP): New macro.
[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, 2006 R. Grafl, A. Krall, C. Kruegel,
4    C. Oates, R. Obermaisser, M. Platter, M. Probst, S. Ring,
5    E. Steiner, C. Thalinger, D. Thuernbeck, P. Tomsich, C. Ullrich,
6    J. Wenninger, 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., 51 Franklin Street, Fifth Floor, Boston, MA
23    02110-1301, USA.
24
25    Contact: cacao@cacaojvm.org
26
27    Author: Andreas Krall
28
29    Changes: Carolyn Oates
30             Edwin Steiner
31             Joseph Wenninger
32             Christian Thalinger
33
34    $Id: parse.c 5024 2006-06-10 14:53:54Z edwin $
35
36 */
37
38
39 #include "config.h"
40
41 #include <assert.h>
42 #include <string.h>
43
44 #include "vm/types.h"
45
46 #include "mm/memory.h"
47 #include "native/native.h"
48 #include "toolbox/logging.h"
49 #include "vm/builtin.h"
50 #include "vm/exceptions.h"
51 #include "vm/global.h"
52 #include "vm/linker.h"
53 #include "vm/loader.h"
54 #include "vm/resolve.h"
55 #include "vm/options.h"
56 #include "vm/statistics.h"
57 #include "vm/stringlocal.h"
58 #include "vm/jit/asmpart.h"
59 #include "vm/jit/jit.h"
60 #include "vm/jit/parse.h"
61 #include "vm/jit/patcher.h"
62 #include "vm/jit/loop/loop.h"
63
64 /*******************************************************************************
65
66         function 'parse' scans the JavaVM code and generates intermediate code
67
68         During parsing the block index table is used to store at bit pos 0
69         a flag which marks basic block starts and at position 1 to 31 the
70         intermediate instruction index. After parsing the block index table
71         is scanned, for marked positions a block is generated and the block
72         number is stored in the block index table.
73
74 *******************************************************************************/
75
76 static exceptiontable * new_fillextable(
77                                                                         jitdata *jd,
78                                                                         methodinfo *m, 
79                                                                         exceptiontable *extable, 
80                                                                         exceptiontable *raw_extable, 
81                                                                 int exceptiontablelength, 
82                                                                         int *block_count)
83 {
84         int b_count, p, src;
85         
86         if (exceptiontablelength == 0) 
87                 return extable;
88
89         b_count = *block_count;
90
91         for (src = exceptiontablelength-1; src >=0; src--) {
92                 /* the start of the handled region becomes a basic block start */
93                 p = raw_extable[src].startpc;
94                 CHECK_BYTECODE_INDEX(p);
95                 extable->startpc = p;
96                 new_block_insert(p);
97                 
98                 p = raw_extable[src].endpc; /* see JVM Spec 4.7.3 */
99                 CHECK_BYTECODE_INDEX_EXCLUSIVE(p);
100
101 #if defined(ENABLE_VERIFIER)
102                 if (p <= raw_extable[src].startpc) {
103                         *exceptionptr = new_verifyerror(m,
104                                 "Invalid exception handler range");
105                         return NULL;
106                 }
107 #endif
108                 extable->endpc = p;
109                 
110                 /* end of handled region becomes a basic block boundary  */
111                 /* (If it is the bytecode end, we'll use the special     */
112                 /* end block that is created anyway.)                    */
113                 if (p < m->jcodelength) 
114                         new_block_insert(p);
115
116                 /* the start of the handler becomes a basic block start  */
117                 p = raw_extable[src].handlerpc;
118                 CHECK_BYTECODE_INDEX(p);
119                 extable->handlerpc = p;
120                 new_block_insert(p);
121
122                 extable->catchtype = raw_extable[src].catchtype;
123                 extable->next = NULL;
124                 extable->down = &extable[1];
125                 extable--;
126         }
127
128         *block_count = b_count;
129         
130         /* everything ok */
131         return extable;
132
133 #if defined(ENABLE_VERIFIER)
134 throw_invalid_bytecode_index:
135         *exceptionptr =
136                 new_verifyerror(m, "Illegal bytecode index in exception table");
137         return NULL;
138 #endif
139 }
140
141 static exceptiontable * fillextable(methodinfo *m, 
142                                                                         exceptiontable *extable, 
143                                                                         exceptiontable *raw_extable, 
144                                                                 int exceptiontablelength, 
145                                                                         int *block_count)
146 {
147         int b_count, p, src;
148         
149         if (exceptiontablelength == 0) 
150                 return extable;
151
152         b_count = *block_count;
153
154         for (src = exceptiontablelength-1; src >=0; src--) {
155                 /* the start of the handled region becomes a basic block start */
156                 p = raw_extable[src].startpc;
157                 CHECK_BYTECODE_INDEX(p);
158                 extable->startpc = p;
159                 block_insert(p);
160                 
161                 p = raw_extable[src].endpc; /* see JVM Spec 4.7.3 */
162                 CHECK_BYTECODE_INDEX_EXCLUSIVE(p);
163
164 #if defined(ENABLE_VERIFIER)
165                 if (p <= raw_extable[src].startpc) {
166                         *exceptionptr = new_verifyerror(m,
167                                 "Invalid exception handler range");
168                         return NULL;
169                 }
170 #endif
171                 extable->endpc = p;
172                 
173                 /* end of handled region becomes a basic block boundary  */
174                 /* (If it is the bytecode end, we'll use the special     */
175                 /* end block that is created anyway.)                    */
176                 if (p < m->jcodelength) 
177                         block_insert(p);
178
179                 /* the start of the handler becomes a basic block start  */
180                 p = raw_extable[src].handlerpc;
181                 CHECK_BYTECODE_INDEX(p);
182                 extable->handlerpc = p;
183                 block_insert(p);
184
185                 extable->catchtype = raw_extable[src].catchtype;
186                 extable->next = NULL;
187                 extable->down = &extable[1];
188                 extable--;
189         }
190
191         *block_count = b_count;
192         
193         /* everything ok */
194         return extable;
195
196 #if defined(ENABLE_VERIFIER)
197 throw_invalid_bytecode_index:
198         *exceptionptr =
199                 new_verifyerror(m, "Illegal bytecode index in exception table");
200         return NULL;
201 #endif
202 }
203
204 /*** macro for checking the length of the bytecode ***/
205
206 #if defined(ENABLE_VERIFIER)
207 #define CHECK_END_OF_BYTECODE(neededlength) \
208         do { \
209                 if ((neededlength) > m->jcodelength) \
210                         goto throw_unexpected_end_of_bytecode; \
211         } while (0)
212 #else /* !ENABLE_VERIFIER */
213 #define CHECK_END_OF_BYTECODE(neededlength)
214 #endif /* ENABLE_VERIFIER */
215
216 bool new_parse(jitdata *jd)
217 {
218         methodinfo  *m;             /* method being parsed                      */
219         codegendata *cd;
220         int  p;                     /* java instruction counter                 */
221         int  nextp;                 /* start of next java instruction           */
222         int  opcode;                /* java opcode                              */
223         int  i;                     /* temporary for different uses (ctrs)      */
224         int  ipc = 0;               /* intermediate instruction counter         */
225         int  b_count = 0;           /* basic block counter                      */
226         int  s_count = 0;           /* stack element counter                    */
227         bool blockend = false;      /* true if basic block end has been reached */
228         bool iswide = false;        /* true if last instruction was a wide      */
229         new_instruction *iptr;      /* current ptr into instruction array       */
230         u1 *instructionstart;       /* 1 for pcs which are valid instr. starts  */
231         constant_classref  *cr;
232         constant_classref  *compr;
233         classinfo          *c;
234         builtintable_entry *bte;
235         constant_FMIref    *mr;
236         methoddesc         *md;
237         unresolved_method  *um;
238         resolve_result_t    result;
239         u2                  lineindex = 0;
240         u2                  currentline = 0;
241         u2                  linepcchange = 0;
242         u4                  flags;
243         basicblock         *bptr;
244
245         /* get required compiler data */
246
247         m  = jd->m;
248         cd = jd->cd;
249
250         /* allocate instruction array and block index table */
251
252         /* 1 additional for end ipc  */
253         jd->new_basicblockindex = DMNEW(s4, m->jcodelength + 1);
254         memset(jd->new_basicblockindex, 0, sizeof(s4) * (m->jcodelength + 1));
255
256         instructionstart = DMNEW(u1, m->jcodelength + 1);
257         memset(instructionstart, 0, sizeof(u1) * (m->jcodelength + 1));
258
259         /* IMPORTANT: We assume that parsing creates at most one instruction per */
260         /*            byte of original bytecode!                                 */
261
262         iptr = jd->new_instructions = DMNEW(new_instruction, m->jcodelength);
263
264         /* compute branch targets of exception table */
265
266         if (!new_fillextable(jd, m,
267                         &(cd->exceptiontable[cd->exceptiontablelength-1]),
268                         m->exceptiontable,
269                         m->exceptiontablelength,
270                         &b_count))
271         {
272                 return false;
273         }
274
275         s_count = 1 + m->exceptiontablelength; /* initialize stack element counter   */
276
277 #if defined(ENABLE_THREADS)
278         if (checksync && (m->flags & ACC_SYNCHRONIZED)) {
279                 m->isleafmethod = false;
280         }
281 #endif
282
283         /* setup line number info */
284
285         currentline = 0;
286         linepcchange = 0;
287
288         if (m->linenumbercount == 0) {
289                 lineindex = 0;
290         }
291         else {
292                 linepcchange = m->linenumbers[0].start_pc;
293         }
294
295         /*** LOOP OVER ALL BYTECODE INSTRUCTIONS **********************************/
296
297         for (p = 0; p < m->jcodelength; p = nextp) {
298
299                 /* mark this position as a valid instruction start */
300
301                 instructionstart[p] = 1;
302
303                 /* change the current line number, if necessary */
304
305                 /* XXX rewrite this using pointer arithmetic */
306
307                 if (linepcchange == p) {
308                         if (m->linenumbercount > lineindex) {
309 next_linenumber:
310                                 currentline = m->linenumbers[lineindex].line_number;
311                                 lineindex++;
312                                 if (lineindex < m->linenumbercount) {
313                                         linepcchange = m->linenumbers[lineindex].start_pc;
314                                         if (linepcchange == p)
315                                                 goto next_linenumber;
316                                 }
317                         }
318                 }
319
320                 /* fetch next opcode  */
321 fetch_opcode:
322                 opcode = code_get_u1(p, m);
323
324                 /* store intermediate instruction count (bit 0 mark block starts) */
325
326                 jd->new_basicblockindex[p] |= (ipc << 1);
327
328                 /* some compilers put a JAVA_NOP after a blockend instruction */
329
330                 if (blockend && (opcode != JAVA_NOP)) {
331                         /* start new block */
332
333                         new_block_insert(p);
334                         blockend = false;
335                 }
336
337                 /* compute next instruction start */
338
339                 nextp = p + jcommandsize[opcode];
340
341                 CHECK_END_OF_BYTECODE(nextp);
342
343                 /* add stack elements produced by this instruction */
344
345                 s_count += stackreq[opcode];
346
347                 /* translate this bytecode instruction */
348
349                 switch (opcode) {
350
351                 case JAVA_NOP:
352                         break;
353
354                 /* pushing constants onto the stack ***********************************/
355
356                 case JAVA_BIPUSH:
357                         NEW_OP_LOADCONST_I(code_get_s1(p+1,m));
358                         break;
359
360                 case JAVA_SIPUSH:
361                         NEW_OP_LOADCONST_I(code_get_s2(p+1,m));
362                         break;
363
364                 case JAVA_LDC1:
365                         i = code_get_u1(p + 1, m);
366                         goto pushconstantitem;
367
368                 case JAVA_LDC2:
369                 case JAVA_LDC2W:
370                         i = code_get_u2(p + 1, m);
371
372                 pushconstantitem:
373
374 #if defined(ENABLE_VERIFIER)
375                         if (i >= m->class->cpcount) {
376                                 *exceptionptr = new_verifyerror(m,
377                                         "Attempt to access constant outside range");
378                                 return false;
379                         }
380 #endif
381
382                         switch (m->class->cptags[i]) {
383                         case CONSTANT_Integer:
384                                 NEW_OP_LOADCONST_I(((constant_integer *) (m->class->cpinfos[i]))->value);
385                                 break;
386                         case CONSTANT_Long:
387                                 NEW_OP_LOADCONST_L(((constant_long *) (m->class->cpinfos[i]))->value);
388                                 break;
389                         case CONSTANT_Float:
390                                 NEW_OP_LOADCONST_F(((constant_float *) (m->class->cpinfos[i]))->value);
391                                 break;
392                         case CONSTANT_Double:
393                                 NEW_OP_LOADCONST_D(((constant_double *) (m->class->cpinfos[i]))->value);
394                                 break;
395                         case CONSTANT_String:
396                                 NEW_OP_LOADCONST_STRING(literalstring_new((utf *) (m->class->cpinfos[i])));
397                                 break;
398                         case CONSTANT_Class:
399                                 cr = (constant_classref *) (m->class->cpinfos[i]);
400
401                                 if (!resolve_classref(m, cr, resolveLazy, true,
402                                                                           true, &c))
403                                         return false;
404
405                                 /* if not resolved, c == NULL */
406
407                                 NEW_OP_LOADCONST_CLASSINFO_OR_CLASSREF(c, cr, 0 /* no extra flags */);
408
409                                 break;
410
411 #if defined(ENABLE_VERIFIER)
412                         default:
413                                 *exceptionptr = new_verifyerror(m,
414                                                 "Invalid constant type to push");
415                                 return false;
416 #endif
417                         }
418                         break;
419
420                 case JAVA_ACONST_NULL:
421                         NEW_OP_LOADCONST_NULL();
422                         break;
423
424                 case JAVA_ICONST_M1:
425                 case JAVA_ICONST_0:
426                 case JAVA_ICONST_1:
427                 case JAVA_ICONST_2:
428                 case JAVA_ICONST_3:
429                 case JAVA_ICONST_4:
430                 case JAVA_ICONST_5:
431                         NEW_OP_LOADCONST_I(opcode - JAVA_ICONST_0);
432                         break;
433
434                 case JAVA_LCONST_0:
435                 case JAVA_LCONST_1:
436                         NEW_OP_LOADCONST_L(opcode - JAVA_LCONST_0);
437                         break;
438
439                 case JAVA_FCONST_0:
440                 case JAVA_FCONST_1:
441                 case JAVA_FCONST_2:
442                         NEW_OP_LOADCONST_F(opcode - JAVA_FCONST_0);
443                         break;
444
445                 case JAVA_DCONST_0:
446                 case JAVA_DCONST_1:
447                         NEW_OP_LOADCONST_D(opcode - JAVA_DCONST_0);
448                         break;
449
450                 /* local variable access instructions *********************************/
451
452                 case JAVA_ILOAD:
453                 case JAVA_FLOAD:
454                 case JAVA_ALOAD:
455                         if (!iswide) {
456                                 i = code_get_u1(p + 1,m);
457                         }
458                         else {
459                                 i = code_get_u2(p + 1,m);
460                                 nextp = p + 3;
461                                 iswide = false;
462                         }
463                         NEW_OP_LOAD_ONEWORD(opcode, i);
464                         break;
465
466                 case JAVA_LLOAD:
467                 case JAVA_DLOAD:
468                         if (!iswide) {
469                                 i = code_get_u1(p + 1,m);
470                         }
471                         else {
472                                 i = code_get_u2(p + 1,m);
473                                 nextp = p + 3;
474                                 iswide = false;
475                         }
476                         NEW_OP_LOAD_TWOWORD(opcode, i);
477                         break;
478
479                 case JAVA_ILOAD_0:
480                 case JAVA_ILOAD_1:
481                 case JAVA_ILOAD_2:
482                 case JAVA_ILOAD_3:
483                         NEW_OP_LOAD_ONEWORD(ICMD_ILOAD, opcode - JAVA_ILOAD_0);
484                         break;
485
486                 case JAVA_LLOAD_0:
487                 case JAVA_LLOAD_1:
488                 case JAVA_LLOAD_2:
489                 case JAVA_LLOAD_3:
490                         NEW_OP_LOAD_TWOWORD(ICMD_LLOAD, opcode - JAVA_LLOAD_0);
491                         break;
492
493                 case JAVA_FLOAD_0:
494                 case JAVA_FLOAD_1:
495                 case JAVA_FLOAD_2:
496                 case JAVA_FLOAD_3:
497                         NEW_OP_LOAD_ONEWORD(ICMD_FLOAD, opcode - JAVA_FLOAD_0);
498                         break;
499
500                 case JAVA_DLOAD_0:
501                 case JAVA_DLOAD_1:
502                 case JAVA_DLOAD_2:
503                 case JAVA_DLOAD_3:
504                         NEW_OP_LOAD_TWOWORD(ICMD_DLOAD, opcode - JAVA_DLOAD_0);
505                         break;
506
507                 case JAVA_ALOAD_0:
508                 case JAVA_ALOAD_1:
509                 case JAVA_ALOAD_2:
510                 case JAVA_ALOAD_3:
511                         NEW_OP_LOAD_ONEWORD(ICMD_ALOAD, opcode - JAVA_ALOAD_0);
512                         break;
513
514                 case JAVA_ISTORE:
515                 case JAVA_FSTORE:
516                 case JAVA_ASTORE:
517                         if (!iswide) {
518                                 i = code_get_u1(p + 1,m);
519                         }
520                         else {
521                                 i = code_get_u2(p + 1,m);
522                                 iswide = false;
523                                 nextp = p + 3;
524                         }
525                         NEW_OP_STORE_ONEWORD(opcode, i);
526                         break;
527
528                 case JAVA_LSTORE:
529                 case JAVA_DSTORE:
530                         if (!iswide) {
531                                 i = code_get_u1(p + 1,m);
532                         }
533                         else {
534                                 i = code_get_u2(p + 1,m);
535                                 iswide = false;
536                                 nextp = p + 3;
537                         }
538                         NEW_OP_STORE_TWOWORD(opcode, i);
539                         break;
540
541                 case JAVA_ISTORE_0:
542                 case JAVA_ISTORE_1:
543                 case JAVA_ISTORE_2:
544                 case JAVA_ISTORE_3:
545                         NEW_OP_STORE_ONEWORD(ICMD_ISTORE, opcode - JAVA_ISTORE_0);
546                         break;
547
548                 case JAVA_LSTORE_0:
549                 case JAVA_LSTORE_1:
550                 case JAVA_LSTORE_2:
551                 case JAVA_LSTORE_3:
552                         NEW_OP_STORE_TWOWORD(ICMD_LSTORE, opcode - JAVA_LSTORE_0);
553                         break;
554
555                 case JAVA_FSTORE_0:
556                 case JAVA_FSTORE_1:
557                 case JAVA_FSTORE_2:
558                 case JAVA_FSTORE_3:
559                         NEW_OP_STORE_ONEWORD(ICMD_FSTORE, opcode - JAVA_FSTORE_0);
560                         break;
561
562                 case JAVA_DSTORE_0:
563                 case JAVA_DSTORE_1:
564                 case JAVA_DSTORE_2:
565                 case JAVA_DSTORE_3:
566                         NEW_OP_STORE_TWOWORD(ICMD_DSTORE, opcode - JAVA_DSTORE_0);
567                         break;
568
569                 case JAVA_ASTORE_0:
570                 case JAVA_ASTORE_1:
571                 case JAVA_ASTORE_2:
572                 case JAVA_ASTORE_3:
573                         NEW_OP_STORE_ONEWORD(ICMD_ASTORE, opcode - JAVA_ASTORE_0);
574                         break;
575
576                 case JAVA_IINC:
577                         {
578                                 int v;
579
580                                 if (!iswide) {
581                                         i = code_get_u1(p + 1,m);
582                                         v = code_get_s1(p + 2,m);
583
584                                 }
585                                 else {
586                                         i = code_get_u2(p + 1,m);
587                                         v = code_get_s2(p + 3,m);
588                                         iswide = false;
589                                         nextp = p + 5;
590                                 }
591                                 INDEX_ONEWORD(i);
592                                 NEW_OP_LOCALINDEX_I(opcode, i, v);
593                         }
594                         break;
595
596                 /* wider index for loading, storing and incrementing ******************/
597
598                 case JAVA_WIDE:
599                         iswide = true;
600                         p++;
601                         goto fetch_opcode;
602
603                 /* managing arrays ****************************************************/
604
605                 case JAVA_NEWARRAY:
606                         switch (code_get_s1(p + 1, m)) {
607                         case 4:
608                                 bte = builtintable_get_internal(BUILTIN_newarray_boolean);
609                                 break;
610                         case 5:
611                                 bte = builtintable_get_internal(BUILTIN_newarray_char);
612                                 break;
613                         case 6:
614                                 bte = builtintable_get_internal(BUILTIN_newarray_float);
615                                 break;
616                         case 7:
617                                 bte = builtintable_get_internal(BUILTIN_newarray_double);
618                                 break;
619                         case 8:
620                                 bte = builtintable_get_internal(BUILTIN_newarray_byte);
621                                 break;
622                         case 9:
623                                 bte = builtintable_get_internal(BUILTIN_newarray_short);
624                                 break;
625                         case 10:
626                                 bte = builtintable_get_internal(BUILTIN_newarray_int);
627                                 break;
628                         case 11:
629                                 bte = builtintable_get_internal(BUILTIN_newarray_long);
630                                 break;
631 #if defined(ENABLE_VERIFIER)
632                         default:
633                                 *exceptionptr = new_verifyerror(m,
634                                                 "Invalid array-type to create");
635                                 return false;
636 #endif
637                         }
638                         NEW_OP_BUILTIN_CHECK_EXCEPTION(bte);
639                         break;
640
641                 case JAVA_ANEWARRAY:
642                         i = code_get_u2(p + 1, m);
643                         compr = (constant_classref *) class_getconstant(m->class, i, CONSTANT_Class);
644                         if (!compr)
645                                 return false;
646
647                         if (!(cr = class_get_classref_multiarray_of(1, compr)))
648                                 return false;
649
650                         if (!resolve_classref(m, cr, resolveLazy, true, true, &c))
651                                 return false;
652
653                         NEW_OP_LOADCONST_CLASSINFO_OR_CLASSREF(c, cr, INS_FLAG_NOCHECK);
654                         bte = builtintable_get_internal(BUILTIN_newarray);
655                         NEW_OP_BUILTIN_CHECK_EXCEPTION(bte);
656                         s_count++;
657                         break;
658
659                 case JAVA_MULTIANEWARRAY:
660                         m->isleafmethod = false;
661                         i = code_get_u2(p + 1, m);
662                         {
663                                 s4 v = code_get_u1(p + 3, m);
664
665                                 cr = (constant_classref *) class_getconstant(m->class, i, CONSTANT_Class);
666                                 if (!cr)
667                                         return false;
668
669                                 if (!resolve_classref(m, cr, resolveLazy, true, true, &c))
670                                         return false;
671
672                                 /* if unresolved, c == NULL */
673
674                                 iptr->s1.argcount = v;
675                                 NEW_OP_S3_CLASSINFO_OR_CLASSREF(opcode, c, cr, 0 /* flags */);
676                         }
677                         break;
678
679                 /* control flow instructions ******************************************/
680
681                 case JAVA_IFEQ:
682                 case JAVA_IFLT:
683                 case JAVA_IFLE:
684                 case JAVA_IFNE:
685                 case JAVA_IFGT:
686                 case JAVA_IFGE:
687                 case JAVA_IFNULL:
688                 case JAVA_IFNONNULL:
689                 case JAVA_IF_ICMPEQ:
690                 case JAVA_IF_ICMPNE:
691                 case JAVA_IF_ICMPLT:
692                 case JAVA_IF_ICMPGT:
693                 case JAVA_IF_ICMPLE:
694                 case JAVA_IF_ICMPGE:
695                 case JAVA_IF_ACMPEQ:
696                 case JAVA_IF_ACMPNE:
697                 case JAVA_GOTO:
698                         i = p + code_get_s2(p + 1,m);
699                         CHECK_BYTECODE_INDEX(i);
700                         new_block_insert(i);
701                         blockend = true;
702                         NEW_OP_INSINDEX(opcode, i);
703                         break;
704
705                 case JAVA_GOTO_W:
706                         i = p + code_get_s4(p + 1,m);
707                         CHECK_BYTECODE_INDEX(i);
708                         new_block_insert(i);
709                         blockend = true;
710                         NEW_OP_INSINDEX(opcode, i);
711                         break;
712
713                 case JAVA_JSR:
714                         i = p + code_get_s2(p + 1,m);
715 jsr_tail:
716                         CHECK_BYTECODE_INDEX(i);
717                         new_block_insert(i);
718                         blockend = true;
719                         NEW_OP_PREPARE_ZEROFLAGS(JAVA_JSR);
720                         iptr->sx.s23.s3.jsrtarget.insindex = i;
721                         PINC;
722                         break;
723
724                 case JAVA_JSR_W:
725                         i = p + code_get_s4(p + 1,m);
726                         goto jsr_tail;
727
728                 case JAVA_RET:
729                         if (!iswide) {
730                                 i = code_get_u1(p + 1,m);
731                         }
732                         else {
733                                 i = code_get_u2(p + 1,m);
734                                 nextp = p + 3;
735                                 iswide = false;
736                         }
737                         blockend = true;
738
739                         NEW_OP_LOAD_ONEWORD(opcode, i);
740                         break;
741
742                 case JAVA_IRETURN:
743                 case JAVA_LRETURN:
744                 case JAVA_FRETURN:
745                 case JAVA_DRETURN:
746                 case JAVA_ARETURN:
747                 case JAVA_RETURN:
748                         blockend = true;
749                         /* XXX ARETURN will need a flag in the typechecker */
750                         NEW_OP(opcode);
751                         break;
752
753                 case JAVA_ATHROW:
754                         blockend = true;
755                         /* XXX ATHROW will need a flag in the typechecker */
756                         NEW_OP(opcode);
757                         break;
758
759
760                 /* table jumps ********************************************************/
761
762                 case JAVA_LOOKUPSWITCH:
763                         {
764                                 s4 num, j;
765                                 lookup_target_t *lookup;
766 #if defined(ENABLE_VERIFIER)
767                                 s4 prevvalue = 0;
768 #endif
769                                 blockend = true;
770                                 nextp = ALIGN((p + 1), 4);
771
772                                 CHECK_END_OF_BYTECODE(nextp + 8);
773
774                                 NEW_OP_PREPARE_ZEROFLAGS(opcode);
775
776                                 /* default target */
777
778                                 j =  p + code_get_s4(nextp, m);
779                                 iptr->sx.s23.s3.lookupdefault.insindex = j;
780                                 nextp += 4;
781                                 CHECK_BYTECODE_INDEX(j);
782                                 new_block_insert(j);
783
784                                 /* number of pairs */
785
786                                 num = code_get_u4(nextp, m);
787                                 iptr->sx.s23.s2.lookupcount = num;
788                                 nextp += 4;
789
790                                 /* allocate the intermediate code table */
791
792                                 lookup = DMNEW(lookup_target_t, num);
793                                 iptr->dst.lookup = lookup;
794
795                                 /* iterate over the lookup table */
796
797                                 CHECK_END_OF_BYTECODE(nextp + 8 * num);
798
799                                 for (i = 0; i < num; i++) {
800                                         /* value */
801
802                                         j = code_get_s4(nextp, m);
803                                         lookup->value = j;
804
805                                         nextp += 4;
806
807 #if defined(ENABLE_VERIFIER)
808                                         /* check if the lookup table is sorted correctly */
809
810                                         if (i && (j <= prevvalue)) {
811                                                 *exceptionptr = new_verifyerror(m, "Unsorted lookup switch");
812                                                 return false;
813                                         }
814                                         prevvalue = j;
815 #endif
816                                         /* target */
817
818                                         j = p + code_get_s4(nextp,m);
819                                         lookup->target.insindex = j;
820                                         lookup++;
821                                         nextp += 4;
822                                         CHECK_BYTECODE_INDEX(j);
823                                         new_block_insert(j);
824                                 }
825
826                                 PINC;
827                                 break;
828                         }
829
830
831                 case JAVA_TABLESWITCH:
832                         {
833                                 s4 num, j;
834                                 s4 deftarget;
835                                 branch_target_t *table;
836
837                                 blockend = true;
838                                 nextp = ALIGN((p + 1), 4);
839
840                                 CHECK_END_OF_BYTECODE(nextp + 12);
841
842                                 NEW_OP_PREPARE_ZEROFLAGS(opcode);
843
844                                 /* default target */
845
846                                 deftarget = p + code_get_s4(nextp, m);
847                                 nextp += 4;
848                                 CHECK_BYTECODE_INDEX(deftarget);
849                                 new_block_insert(deftarget);
850
851                                 /* lower bound */
852
853                                 j = code_get_s4(nextp, m);
854                                 iptr->sx.s23.s2.tablelow = j;
855                                 nextp += 4;
856
857                                 /* upper bound */
858
859                                 num = code_get_s4(nextp, m);
860                                 iptr->sx.s23.s3.tablehigh = num;
861                                 nextp += 4;
862
863                                 /* calculate the number of table entries */
864
865                                 num = num - j + 1;
866
867 #if defined(ENABLE_VERIFIER)
868                                 if (num < 1) {
869                                         *exceptionptr = new_verifyerror(m,
870                                                         "invalid TABLESWITCH: upper bound < lower bound");
871                                         return false;
872                                 }
873 #endif
874                                 /* create the intermediate code table */
875                                 /* the first entry is the default target */
876
877                                 table = MNEW(branch_target_t, 1 + num);
878                                 iptr->dst.table = table;
879                                 (table++)->insindex = deftarget;
880
881                                 /* iterate over the target table */
882
883                                 CHECK_END_OF_BYTECODE(nextp + 4 * num);
884
885                                 for (i = 0; i < num; i++) {
886                                         j = p + code_get_s4(nextp,m);
887                                         (table++)->insindex = j;
888                                         nextp += 4;
889                                         CHECK_BYTECODE_INDEX(j);
890                                         new_block_insert(j);
891                                 }
892
893                                 PINC;
894                                 break;
895                         }
896
897
898                 /* load and store of object fields ************************************/
899
900                 case JAVA_AASTORE:
901                         NEW_OP(opcode);
902                         m->isleafmethod = false;
903                         break;
904
905                 case JAVA_GETSTATIC:
906                 case JAVA_PUTSTATIC:
907                 case JAVA_GETFIELD:
908                 case JAVA_PUTFIELD:
909                         {
910                                 constant_FMIref  *fr;
911                                 unresolved_field *uf;
912
913                                 i = code_get_u2(p + 1, m);
914                                 fr = class_getconstant(m->class, i, CONSTANT_Fieldref);
915                                 if (!fr)
916                                         return false;
917
918                                 NEW_OP_PREPARE_ZEROFLAGS(opcode);
919                                 iptr->sx.s23.s3.fmiref = fr;
920
921                                 /* only with -noverify, otherwise the typechecker does this */
922
923 #if defined(ENABLE_VERIFIER)
924                                 if (!opt_verify) {
925 #endif
926                                         result = new_resolve_field_lazy(iptr, NULL, m);
927                                         if (result == resolveFailed)
928                                                 return false;
929
930                                         if (result != resolveSucceeded) {
931                                                 uf = new_create_unresolved_field(m->class, m, iptr);
932
933                                                 if (!uf)
934                                                         return false;
935
936                                                 /* store the unresolved_field pointer */
937
938                                                 iptr->sx.s23.s3.uf = uf;
939                                                 iptr->flags.bits = INS_FLAG_UNRESOLVED;
940                                         }
941 #if defined(ENABLE_VERIFIER)
942                                 }
943 #endif
944                                 PINC;
945                         }
946                         break;
947
948
949                 /* method invocation **************************************************/
950
951                 case JAVA_INVOKESTATIC:
952                         i = code_get_u2(p + 1, m);
953                         mr = class_getconstant(m->class, i, CONSTANT_Methodref);
954                         if (!mr)
955                                 return false;
956
957                         md = mr->parseddesc.md;
958
959                         if (!md->params)
960                                 if (!descriptor_params_from_paramtypes(md, ACC_STATIC))
961                                         return false;
962
963                         goto invoke_method;
964
965                 case JAVA_INVOKEINTERFACE:
966                         i = code_get_u2(p + 1, m);
967
968                         mr = class_getconstant(m->class, i,
969                                         CONSTANT_InterfaceMethodref);
970
971                         goto invoke_nonstatic_method;
972
973                 case JAVA_INVOKESPECIAL:
974                 case JAVA_INVOKEVIRTUAL:
975                         i = code_get_u2(p + 1, m);
976                         mr = class_getconstant(m->class, i, CONSTANT_Methodref);
977
978 invoke_nonstatic_method:
979                         if (!mr)
980                                 return false;
981
982                         md = mr->parseddesc.md;
983
984                         if (!md->params)
985                                 if (!descriptor_params_from_paramtypes(md, 0))
986                                         return false;
987
988 invoke_method:
989                         m->isleafmethod = false;
990
991                         NEW_OP_PREPARE_ZEROFLAGS(opcode);
992                         iptr->sx.s23.s3.fmiref = mr;
993
994                         /* only with -noverify, otherwise the typechecker does this */
995
996 #if defined(ENABLE_VERIFIER)
997                         if (!opt_verify) {
998 #endif
999                                 result = new_resolve_method_lazy(iptr, NULL, m);
1000                                 if (result == resolveFailed)
1001                                         return false;
1002
1003                                 if (result != resolveSucceeded) {
1004                                         um = new_create_unresolved_method(m->class, m, iptr);
1005
1006                                         if (!um)
1007                                                 return false;
1008
1009                                         /* store the unresolved_method pointer */
1010
1011                                         iptr->sx.s23.s3.um = um;
1012                                         iptr->flags.bits = INS_FLAG_UNRESOLVED;
1013                                 }
1014 #if defined(ENABLE_VERIFIER)
1015                         }
1016 #endif
1017                         PINC;
1018                         break;
1019
1020                 /* instructions taking class arguments ********************************/
1021
1022                 case JAVA_NEW:
1023                         i = code_get_u2(p + 1, m);
1024                         cr = (constant_classref *) class_getconstant(m->class, i, CONSTANT_Class);
1025                         if (!cr)
1026                                 return false;
1027
1028                         if (!resolve_classref(m, cr, resolveLazy, true, true, &c))
1029                                 return false;
1030
1031                         NEW_OP_LOADCONST_CLASSINFO_OR_CLASSREF(c, cr, INS_FLAG_NOCHECK);
1032                         bte = builtintable_get_internal(BUILTIN_new);
1033                         NEW_OP_BUILTIN_CHECK_EXCEPTION(bte);
1034                         s_count++;
1035                         break;
1036
1037                 case JAVA_CHECKCAST:
1038                         i = code_get_u2(p + 1, m);
1039                         cr = (constant_classref *) class_getconstant(m->class, i, CONSTANT_Class);
1040                         if (!cr)
1041                                 return false;
1042
1043                         if (!resolve_classref(m, cr, resolveLazy, true, true, &c))
1044                                 return false;
1045
1046                         if (cr->name->text[0] == '[') {
1047                                 /* array type cast-check */
1048                                 flags = INS_FLAG_ARRAY;
1049                                 m->isleafmethod = false;
1050                         }
1051                         else {
1052                                 /* object type cast-check */
1053                                 flags = 0;
1054                         }
1055                         NEW_OP_S3_CLASSINFO_OR_CLASSREF(opcode, c, cr, flags);
1056                         break;
1057
1058                 case JAVA_INSTANCEOF:
1059                         i = code_get_u2(p + 1,m);
1060                         cr = (constant_classref *) class_getconstant(m->class, i, CONSTANT_Class);
1061                         if (!cr)
1062                                 return false;
1063
1064                         if (!resolve_classref(m, cr, resolveLazy, true, true, &c))
1065                                 return false;
1066
1067                         if (cr->name->text[0] == '[') {
1068                                 /* array type cast-check */
1069                                 NEW_OP_LOADCONST_CLASSINFO_OR_CLASSREF(c, cr, INS_FLAG_NOCHECK);
1070                                 bte = builtintable_get_internal(BUILTIN_arrayinstanceof);
1071                                 NEW_OP_BUILTIN_NO_EXCEPTION(bte);
1072                                 s_count++;
1073                         }
1074                         else {
1075                                 /* object type cast-check */
1076                                 NEW_OP_S3_CLASSINFO_OR_CLASSREF(opcode, c, cr, 0 /* flags*/);
1077                         }
1078                         break;
1079
1080                 /* synchronization instructions ***************************************/
1081
1082                 case JAVA_MONITORENTER:
1083 #if defined(ENABLE_THREADS)
1084                         if (checksync) {
1085                                 /* XXX null check */
1086                                 bte = builtintable_get_internal(BUILTIN_monitorenter);
1087                                 NEW_OP_BUILTIN_CHECK_EXCEPTION(bte);
1088                         }
1089                         else
1090 #endif
1091                         {
1092                                 NEW_OP(ICMD_CHECKNULL_POP);
1093                         }
1094                         break;
1095
1096                 case JAVA_MONITOREXIT:
1097 #if defined(ENABLE_THREADS)
1098                         if (checksync) {
1099                                 /* XXX null check */
1100                                 bte = builtintable_get_internal(BUILTIN_monitorexit);
1101                                 NEW_OP_BUILTIN_CHECK_EXCEPTION(bte);
1102                         }
1103                         else
1104 #endif
1105                         {
1106                                 NEW_OP(ICMD_CHECKNULL_POP);
1107                         }
1108                         break;
1109
1110                 /* arithmetic instructions that may become builtin functions **********/
1111
1112                 case JAVA_IDIV:
1113 #if !SUPPORT_DIVISION
1114                         bte = builtintable_get_internal(BUILTIN_idiv);
1115                         NEW_OP_BUILTIN_ARITHMETIC(opcode, bte);
1116 #else
1117                         OP(opcode);
1118 #endif
1119                         break;
1120
1121                 case JAVA_IREM:
1122 #if !SUPPORT_DIVISION
1123                         bte = builtintable_get_internal(BUILTIN_irem);
1124                         NEW_OP_BUILTIN_ARITHMETIC(opcode, bte);
1125 #else
1126                         OP(opcode);
1127 #endif
1128                         break;
1129
1130                 case JAVA_LDIV:
1131 #if !(SUPPORT_DIVISION && SUPPORT_LONG && SUPPORT_LONG_DIV)
1132                         bte = builtintable_get_internal(BUILTIN_ldiv);
1133                         NEW_OP_BUILTIN_ARITHMETIC(opcode, bte);
1134 #else
1135                         OP(opcode);
1136 #endif
1137                         break;
1138
1139                 case JAVA_LREM:
1140 #if !(SUPPORT_DIVISION && SUPPORT_LONG && SUPPORT_LONG_DIV)
1141                         bte = builtintable_get_internal(BUILTIN_lrem);
1142                         NEW_OP_BUILTIN_ARITHMETIC(opcode, bte);
1143 #else
1144                         OP(opcode);
1145 #endif
1146                         break;
1147
1148                 case JAVA_FREM:
1149 #if defined(__I386__)
1150                         NEW_OP(opcode);
1151 #else
1152                         bte = builtintable_get_internal(BUILTIN_frem);
1153                         NEW_OP_BUILTIN_NO_EXCEPTION(bte);
1154 #endif
1155                         break;
1156
1157                 case JAVA_DREM:
1158 #if defined(__I386__)
1159                         NEW_OP(opcode);
1160 #else
1161                         bte = builtintable_get_internal(BUILTIN_drem);
1162                         NEW_OP_BUILTIN_NO_EXCEPTION(bte);
1163 #endif
1164                         break;
1165
1166                 case JAVA_F2I:
1167 #if defined(__ALPHA__)
1168                         if (!opt_noieee) {
1169                                 bte = builtintable_get_internal(BUILTIN_f2i);
1170                                 NEW_OP_BUILTIN_NO_EXCEPTION(bte);
1171                         }
1172                         else
1173 #endif
1174                         {
1175                                 NEW_OP(opcode);
1176                         }
1177                         break;
1178
1179                 case JAVA_F2L:
1180 #if defined(__ALPHA__)
1181                         if (!opt_noieee) {
1182                                 bte = builtintable_get_internal(BUILTIN_f2l);
1183                                 NEW_OP_BUILTIN_NO_EXCEPTION(bte);
1184                         }
1185                         else
1186 #endif
1187                         {
1188                                 NEW_OP(opcode);
1189                         }
1190                         break;
1191
1192                 case JAVA_D2I:
1193 #if defined(__ALPHA__)
1194                         if (!opt_noieee) {
1195                                 bte = builtintable_get_internal(BUILTIN_d2i);
1196                                 NEW_OP_BUILTIN_NO_EXCEPTION(bte);
1197                         }
1198                         else
1199 #endif
1200                         {
1201                                 NEW_OP(opcode);
1202                         }
1203                         break;
1204
1205                 case JAVA_D2L:
1206 #if defined(__ALPHA__)
1207                         if (!opt_noieee) {
1208                                 bte = builtintable_get_internal(BUILTIN_d2l);
1209                                 NEW_OP_BUILTIN_NO_EXCEPTION(bte);
1210                         }
1211                         else
1212 #endif
1213                         {
1214                                 NEW_OP(opcode);
1215                         }
1216                         break;
1217
1218                 /* invalid opcodes ****************************************************/
1219
1220                         /* check for invalid opcodes if the verifier is enabled */
1221 #if defined(ENABLE_VERIFIER)
1222                 case JAVA_BREAKPOINT:
1223                         *exceptionptr =
1224                                 new_verifyerror(m, "Quick instructions shouldn't appear, yet.");
1225                         return false;
1226
1227                 case 186: /* unused opcode */
1228                 case 203:
1229                 case 204:
1230                 case 205:
1231                 case 206:
1232                 case 207:
1233                 case 208:
1234                 case 209:
1235                 case 210:
1236                 case 211:
1237                 case 212:
1238                 case 213:
1239                 case 214:
1240                 case 215:
1241                 case 216:
1242                 case 217:
1243                 case 218:
1244                 case 219:
1245                 case 220:
1246                 case 221:
1247                 case 222:
1248                 case 223:
1249                 case 224:
1250                 case 225:
1251                 case 226:
1252                 case 227:
1253                 case 228:
1254                 case 229:
1255                 case 230:
1256                 case 231:
1257                 case 232:
1258                 case 233:
1259                 case 234:
1260                 case 235:
1261                 case 236:
1262                 case 237:
1263                 case 238:
1264                 case 239:
1265                 case 240:
1266                 case 241:
1267                 case 242:
1268                 case 243:
1269                 case 244:
1270                 case 245:
1271                 case 246:
1272                 case 247:
1273                 case 248:
1274                 case 249:
1275                 case 250:
1276                 case 251:
1277                 case 252:
1278                 case 253:
1279                 case 254:
1280                 case 255:
1281                         *exceptionptr =
1282                                 new_verifyerror(m,"Illegal opcode %d at instr %d\n",
1283                                                                   opcode, ipc);
1284                         return false;
1285                         break;
1286 #endif /* defined(ENABLE_VERIFIER) */
1287
1288                 /* opcodes that don't require translation *****************************/
1289
1290                 default:
1291                         /* straight-forward translation to ICMD */
1292                         NEW_OP(opcode);
1293                         break;
1294
1295                 } /* end switch */
1296
1297                 /* verifier checks ****************************************************/
1298
1299 #if defined(ENABLE_VERIFIER)
1300                 /* If WIDE was used correctly, iswide should have been reset by now. */
1301                 if (iswide) {
1302                         *exceptionptr = new_verifyerror(m,
1303                                         "Illegal instruction: WIDE before incompatible opcode");
1304                         return false;
1305                 }
1306 #endif /* defined(ENABLE_VERIFIER) */
1307
1308         } /* end for */
1309
1310         /*** END OF LOOP **********************************************************/
1311
1312         /* assert that we did not write more ICMDs than allocated */
1313
1314         assert(ipc == (iptr - jd->new_instructions));
1315         assert(ipc <= m->jcodelength);
1316
1317         /*** verifier checks ******************************************************/
1318
1319 #if defined(ENABLE_VERIFIER)
1320         if (p != m->jcodelength) {
1321                 *exceptionptr = new_verifyerror(m,
1322                                 "Command-sequence crosses code-boundary");
1323                 return false;
1324         }
1325
1326         if (!blockend) {
1327                 *exceptionptr = new_verifyerror(m, "Falling off the end of the code");
1328                 return false;
1329         }
1330 #endif /* defined(ENABLE_VERIFIER) */
1331
1332         /*** setup the methodinfo, allocate stack and basic blocks ****************/
1333
1334         /* adjust block count if target 0 is not first intermediate instruction */
1335
1336         if (!jd->new_basicblockindex[0] || (jd->new_basicblockindex[0] > 1))
1337                 b_count++;
1338
1339         /* copy local to method variables */
1340
1341         jd->new_instructioncount = ipc;
1342         jd->new_basicblockcount = b_count;
1343         jd->new_stackcount = s_count + jd->new_basicblockcount * m->maxstack; /* in-stacks */
1344
1345         /* allocate stack table */
1346
1347         jd->new_stack = DMNEW(stackelement, jd->new_stackcount);
1348
1349         /* build basic block list */
1350
1351         bptr = jd->new_basicblocks = DMNEW(basicblock, b_count + 1);    /* one more for end ipc */
1352
1353         b_count = 0;
1354         jd->new_c_debug_nr = 0;
1355
1356         /* additional block if target 0 is not first intermediate instruction */
1357
1358         if (!jd->new_basicblockindex[0] || (jd->new_basicblockindex[0] > 1)) {
1359                 BASICBLOCK_INIT(bptr, m);
1360
1361                 bptr->iinstr = /* XXX */ (instruction *) jd->new_instructions;
1362                 /* bptr->icount is set when the next block is allocated */
1363
1364                 bptr++;
1365                 b_count++;
1366                 bptr[-1].next = bptr;
1367         }
1368
1369         /* allocate blocks */
1370
1371         for (p = 0; p < m->jcodelength; p++) {
1372                 if (jd->new_basicblockindex[p] & 1) {
1373                         /* Check if this block starts at the beginning of an          */
1374                         /* instruction.                                               */
1375 #if defined(ENABLE_VERIFIER)
1376                         if (!instructionstart[p]) {
1377                                 *exceptionptr = new_verifyerror(m,
1378                                                 "Branch into middle of instruction");
1379                                 return false;
1380                         }
1381 #endif
1382
1383                         /* allocate the block */
1384
1385                         BASICBLOCK_INIT(bptr, m);
1386
1387                         bptr->iinstr = /* XXX */ (instruction *) (jd->new_instructions + (jd->new_basicblockindex[p] >> 1));
1388                         if (b_count) {
1389                                 bptr[-1].icount = /*XXX*/ (new_instruction *)bptr->iinstr - (new_instruction*) bptr[-1].iinstr;
1390                         }
1391                         /* bptr->icount is set when the next block is allocated */
1392
1393                         jd->new_basicblockindex[p] = b_count;
1394
1395                         bptr++;
1396                         b_count++;
1397                         bptr[-1].next = bptr;
1398                 }
1399         }
1400
1401         /* set instruction count of last real block */
1402
1403         if (b_count) {
1404                 bptr[-1].icount = (jd->new_instructions + jd->new_instructioncount) - /* XXX */ (new_instruction *) bptr[-1].iinstr;
1405         }
1406
1407         /* allocate additional block at end */
1408
1409         BASICBLOCK_INIT(bptr,m);
1410
1411         bptr->instack = bptr->outstack = NULL;
1412         bptr->indepth = bptr->outdepth = 0;
1413         bptr->iinstr = NULL;
1414         bptr->icount = 0;
1415         bptr->next = NULL;
1416
1417         /* set basicblock pointers in exception table */
1418
1419         if (cd->exceptiontablelength > 0) {
1420                 cd->exceptiontable[cd->exceptiontablelength - 1].down = NULL;
1421         }
1422
1423         for (i = 0; i < cd->exceptiontablelength; ++i) {
1424                 p = cd->exceptiontable[i].startpc;
1425                 cd->exceptiontable[i].start = jd->new_basicblocks + jd->new_basicblockindex[p];
1426
1427                 p = cd->exceptiontable[i].endpc;
1428                 cd->exceptiontable[i].end = (p == m->jcodelength) ? (jd->new_basicblocks + jd->new_basicblockcount /*+ 1*/) : (jd->new_basicblocks + jd->new_basicblockindex[p]);
1429
1430                 p = cd->exceptiontable[i].handlerpc;
1431                 cd->exceptiontable[i].handler = jd->new_basicblocks + jd->new_basicblockindex[p];
1432         }
1433
1434         /* XXX activate this if you want to try inlining */
1435 #if 0
1436         for (i = 0; i < m->exceptiontablelength; ++i) {
1437                 p = m->exceptiontable[i].startpc;
1438                 m->exceptiontable[i].start = jd->new_basicblocks + jd->new_basicblockindex[p];
1439
1440                 p = m->exceptiontable[i].endpc;
1441                 m->exceptiontable[i].end = (p == m->jcodelength) ? (jd->new_basicblocks + jd->new_basicblockcount /*+ 1*/) : (jd->new_basicblocks + jd->new_basicblockindex[p]);
1442
1443                 p = m->exceptiontable[i].handlerpc;
1444                 m->exceptiontable[i].handler = jd->new_basicblocks + jd->new_basicblockindex[p];
1445         }
1446 #endif
1447
1448         /* everything's ok */
1449
1450         return true;
1451
1452         /*** goto labels for throwing verifier exceptions *************************/
1453
1454 #if defined(ENABLE_VERIFIER)
1455
1456 throw_unexpected_end_of_bytecode:
1457         *exceptionptr = new_verifyerror(m, "Unexpected end of bytecode");
1458         return false;
1459
1460 throw_invalid_bytecode_index:
1461         *exceptionptr =
1462                 new_verifyerror(m, "Illegal target of branch instruction");
1463         return false;
1464
1465 throw_illegal_local_variable_number:
1466         *exceptionptr =
1467                 new_verifyerror(m, "Illegal local variable number");
1468         return false;
1469
1470 #endif /* ENABLE_VERIFIER */
1471 }
1472
1473
1474 bool parse(jitdata *jd)
1475 {
1476         methodinfo  *m;
1477         codegendata *cd;
1478         int  p;                     /* java instruction counter           */
1479         int  nextp;                 /* start of next java instruction     */
1480         int  opcode;                /* java opcode                        */
1481         int  i;                     /* temporary for different uses (ctrs)*/
1482         int  ipc = 0;               /* intermediate instruction counter   */
1483         int  b_count = 0;           /* basic block counter                */
1484         int  s_count = 0;           /* stack element counter              */
1485         bool blockend = false;      /* true if basic block end has been reached   */
1486         bool iswide = false;        /* true if last instruction was a wide*/
1487         instruction *iptr;          /* current ptr into instruction array */
1488
1489         u1 *instructionstart;       /* 1 for pcs which are valid instr. starts    */
1490
1491         constant_classref  *cr;
1492         constant_classref  *compr;
1493         classinfo          *c;
1494         builtintable_entry *bte;
1495
1496         constant_FMIref   *mr;
1497         methoddesc        *md;
1498         unresolved_method *um;
1499         resolve_result_t   result;
1500
1501         u2 lineindex = 0;
1502         u2 currentline = 0;
1503         u2 linepcchange = 0;
1504
1505         /* get required compiler data */
1506
1507         m  = jd->m;
1508         cd = jd->cd;
1509
1510         /* allocate instruction array and block index table */
1511         
1512         /* 1 additional for end ipc  */
1513         m->basicblockindex = DMNEW(s4, m->jcodelength + 1);
1514         memset(m->basicblockindex, 0, sizeof(s4) * (m->jcodelength + 1));
1515
1516         instructionstart = DMNEW(u1, m->jcodelength + 1);
1517         memset(instructionstart, 0, sizeof(u1) * (m->jcodelength + 1));
1518
1519         /* 1 additional for TRACEBUILTIN and 4 for MONITORENTER/EXIT */
1520         /* additional MONITOREXITS are reached by branches which are 3 bytes */
1521         
1522         iptr = m->instructions = DMNEW(instruction, m->jcodelength + 5);
1523
1524         /* Zero the intermediate instructions array so we don't have any
1525          * invalid pointers in it if we cannot finish analyse_stack(). */
1526
1527         memset(iptr, 0, sizeof(instruction) * (m->jcodelength + 5));
1528         
1529         /* compute branch targets of exception table */
1530
1531         if (!fillextable(m, 
1532                         &(cd->exceptiontable[cd->exceptiontablelength-1]), 
1533                         m->exceptiontable, 
1534                         m->exceptiontablelength, 
1535                         &b_count))
1536         {
1537                 return false;
1538         }
1539
1540         s_count = 1 + m->exceptiontablelength; /* initialize stack element counter   */
1541
1542 #if defined(ENABLE_THREADS)
1543         if (checksync && (m->flags & ACC_SYNCHRONIZED)) {
1544                 m->isleafmethod = false;
1545         }                       
1546 #endif
1547
1548         /* scan all java instructions */
1549         currentline = 0;
1550         linepcchange = 0;
1551
1552         if (m->linenumbercount == 0) {
1553                 lineindex = 0;
1554         } 
1555         else {
1556                 linepcchange = m->linenumbers[0].start_pc;
1557         }
1558
1559         for (p = 0; p < m->jcodelength; p = nextp) {
1560           
1561                 /* mark this position as a valid instruction start */
1562                 instructionstart[p] = 1;
1563                 if (linepcchange == p) {
1564                         if (m->linenumbercount > lineindex) {
1565 next_linenumber:
1566                                 currentline = m->linenumbers[lineindex].line_number;
1567                                 lineindex++;
1568                                 if (lineindex < m->linenumbercount) {
1569                                         linepcchange = m->linenumbers[lineindex].start_pc;
1570                                         if (linepcchange == p)
1571                                                 goto next_linenumber;
1572                                 }
1573                         }
1574                 }
1575
1576                 /* fetch next opcode  */
1577 fetch_opcode:
1578                 opcode = code_get_u1(p, m);
1579
1580                 m->basicblockindex[p] |= (ipc << 1); /*store intermed cnt*/
1581
1582                 /* some compilers put a JAVA_NOP after a blockend instruction */
1583
1584                 if (blockend && (opcode != JAVA_NOP)) {
1585                         /* start new block */
1586
1587                         block_insert(p);
1588                         blockend = false;
1589                 }
1590
1591                 nextp = p + jcommandsize[opcode];   /* compute next instruction start */
1592
1593                 CHECK_END_OF_BYTECODE(nextp);
1594
1595                 s_count += stackreq[opcode];            /* compute stack element count    */
1596                 switch (opcode) {
1597                 case JAVA_NOP:
1598                         break;
1599
1600                         /* pushing constants onto the stack p */
1601
1602                 case JAVA_BIPUSH:
1603                         LOADCONST_I(code_get_s1(p+1,m));
1604                         break;
1605
1606                 case JAVA_SIPUSH:
1607                         LOADCONST_I(code_get_s2(p+1,m));
1608                         break;
1609
1610                 case JAVA_LDC1:
1611                         i = code_get_u1(p + 1, m);
1612                         goto pushconstantitem;
1613
1614                 case JAVA_LDC2:
1615                 case JAVA_LDC2W:
1616                         i = code_get_u2(p + 1, m);
1617
1618                 pushconstantitem:
1619
1620 #if defined(ENABLE_VERIFIER)
1621                         if (i >= m->class->cpcount) {
1622                                 *exceptionptr = new_verifyerror(m,
1623                                         "Attempt to access constant outside range");
1624                                 return false;
1625                         }
1626 #endif
1627
1628                         switch (m->class->cptags[i]) {
1629                         case CONSTANT_Integer:
1630                                 LOADCONST_I(((constant_integer *) (m->class->cpinfos[i]))->value);
1631                                 break;
1632                         case CONSTANT_Long:
1633                                 LOADCONST_L(((constant_long *) (m->class->cpinfos[i]))->value);
1634                                 break;
1635                         case CONSTANT_Float:
1636                                 LOADCONST_F(((constant_float *) (m->class->cpinfos[i]))->value);
1637                                 break;
1638                         case CONSTANT_Double:
1639                                 LOADCONST_D(((constant_double *) (m->class->cpinfos[i]))->value);
1640                                 break;
1641                         case CONSTANT_String:
1642                                 LOADCONST_A(literalstring_new((utf *) (m->class->cpinfos[i])));
1643                                 break;
1644                         case CONSTANT_Class:
1645                                 cr = (constant_classref *) (m->class->cpinfos[i]);
1646
1647                                 if (!resolve_classref(m, cr, resolveLazy, true,
1648                                                                           true, &c))
1649                                         return false;
1650
1651                                 /* if not resolved, c == NULL */
1652
1653                                 if (c) {
1654                                         iptr->target = (void*) 0x02; /* XXX target used temporarily as flag */
1655                                         LOADCONST_A(c);
1656                                 }
1657                                 else {
1658                                         iptr->target = (void*) 0x03; /* XXX target used temporarily as flag */
1659                                         LOADCONST_A(cr);
1660                                 }
1661                                 break;
1662
1663 #if defined(ENABLE_VERIFIER)
1664                         default:
1665                                 *exceptionptr = new_verifyerror(m,
1666                                                 "Invalid constant type to push");
1667                                 return false;
1668 #endif
1669                         }
1670                         break;
1671
1672                 case JAVA_ACONST_NULL:
1673                         LOADCONST_A(NULL);
1674                         break;
1675
1676                 case JAVA_ICONST_M1:
1677                 case JAVA_ICONST_0:
1678                 case JAVA_ICONST_1:
1679                 case JAVA_ICONST_2:
1680                 case JAVA_ICONST_3:
1681                 case JAVA_ICONST_4:
1682                 case JAVA_ICONST_5:
1683                         LOADCONST_I(opcode - JAVA_ICONST_0);
1684                         break;
1685
1686                 case JAVA_LCONST_0:
1687                 case JAVA_LCONST_1:
1688                         LOADCONST_L(opcode - JAVA_LCONST_0);
1689                         break;
1690
1691                 case JAVA_FCONST_0:
1692                 case JAVA_FCONST_1:
1693                 case JAVA_FCONST_2:
1694                         LOADCONST_F(opcode - JAVA_FCONST_0);
1695                         break;
1696
1697                 case JAVA_DCONST_0:
1698                 case JAVA_DCONST_1:
1699                         LOADCONST_D(opcode - JAVA_DCONST_0);
1700                         break;
1701
1702                         /* loading variables onto the stack */
1703
1704                 case JAVA_ILOAD:
1705                 case JAVA_FLOAD:
1706                 case JAVA_ALOAD:
1707                         if (!iswide) {
1708                                 i = code_get_u1(p + 1,m);
1709                         } 
1710                         else {
1711                                 i = code_get_u2(p + 1,m);
1712                                 nextp = p + 3;
1713                                 iswide = false;
1714                         }
1715                         OP1LOAD_ONEWORD(opcode, i);
1716                         break;
1717
1718                 case JAVA_LLOAD:
1719                 case JAVA_DLOAD:
1720                         if (!iswide) {
1721                                 i = code_get_u1(p + 1,m);
1722                         } 
1723                         else {
1724                                 i = code_get_u2(p + 1,m);
1725                                 nextp = p + 3;
1726                                 iswide = false;
1727                         }
1728                         OP1LOAD_TWOWORD(opcode, i);
1729                         break;
1730
1731                 case JAVA_ILOAD_0:
1732                 case JAVA_ILOAD_1:
1733                 case JAVA_ILOAD_2:
1734                 case JAVA_ILOAD_3:
1735                         OP1LOAD_ONEWORD(ICMD_ILOAD, opcode - JAVA_ILOAD_0);
1736                         break;
1737
1738                 case JAVA_LLOAD_0:
1739                 case JAVA_LLOAD_1:
1740                 case JAVA_LLOAD_2:
1741                 case JAVA_LLOAD_3:
1742                         OP1LOAD_TWOWORD(ICMD_LLOAD, opcode - JAVA_LLOAD_0);
1743                         break;
1744
1745                 case JAVA_FLOAD_0:
1746                 case JAVA_FLOAD_1:
1747                 case JAVA_FLOAD_2:
1748                 case JAVA_FLOAD_3:
1749                         OP1LOAD_ONEWORD(ICMD_FLOAD, opcode - JAVA_FLOAD_0);
1750                         break;
1751
1752                 case JAVA_DLOAD_0:
1753                 case JAVA_DLOAD_1:
1754                 case JAVA_DLOAD_2:
1755                 case JAVA_DLOAD_3:
1756                         OP1LOAD_TWOWORD(ICMD_DLOAD, opcode - JAVA_DLOAD_0);
1757                         break;
1758
1759                 case JAVA_ALOAD_0:
1760                 case JAVA_ALOAD_1:
1761                 case JAVA_ALOAD_2:
1762                 case JAVA_ALOAD_3:
1763                         OP1LOAD_ONEWORD(ICMD_ALOAD, opcode - JAVA_ALOAD_0);
1764                         break;
1765
1766                         /* storing stack values into local variables */
1767
1768                 case JAVA_ISTORE:
1769                 case JAVA_FSTORE:
1770                 case JAVA_ASTORE:
1771                         if (!iswide) {
1772                                 i = code_get_u1(p + 1,m);
1773                         } 
1774                         else {
1775                                 i = code_get_u2(p + 1,m);
1776                                 iswide = false;
1777                                 nextp = p + 3;
1778                         }
1779                         OP1STORE_ONEWORD(opcode, i);
1780                         break;
1781
1782                 case JAVA_LSTORE:
1783                 case JAVA_DSTORE:
1784                         if (!iswide) {
1785                                 i = code_get_u1(p + 1,m);
1786                         } 
1787                         else {
1788                                 i = code_get_u2(p + 1,m);
1789                                 iswide = false;
1790                                 nextp = p + 3;
1791                         }
1792                         OP1STORE_TWOWORD(opcode, i);
1793                         break;
1794
1795                 case JAVA_ISTORE_0:
1796                 case JAVA_ISTORE_1:
1797                 case JAVA_ISTORE_2:
1798                 case JAVA_ISTORE_3:
1799                         OP1STORE_ONEWORD(ICMD_ISTORE, opcode - JAVA_ISTORE_0);
1800                         break;
1801
1802                 case JAVA_LSTORE_0:
1803                 case JAVA_LSTORE_1:
1804                 case JAVA_LSTORE_2:
1805                 case JAVA_LSTORE_3:
1806                         OP1STORE_TWOWORD(ICMD_LSTORE, opcode - JAVA_LSTORE_0);
1807                         break;
1808
1809                 case JAVA_FSTORE_0:
1810                 case JAVA_FSTORE_1:
1811                 case JAVA_FSTORE_2:
1812                 case JAVA_FSTORE_3:
1813                         OP1STORE_ONEWORD(ICMD_FSTORE, opcode - JAVA_FSTORE_0);
1814                         break;
1815
1816                 case JAVA_DSTORE_0:
1817                 case JAVA_DSTORE_1:
1818                 case JAVA_DSTORE_2:
1819                 case JAVA_DSTORE_3:
1820                         OP1STORE_TWOWORD(ICMD_DSTORE, opcode - JAVA_DSTORE_0);
1821                         break;
1822
1823                 case JAVA_ASTORE_0:
1824                 case JAVA_ASTORE_1:
1825                 case JAVA_ASTORE_2:
1826                 case JAVA_ASTORE_3:
1827                         OP1STORE_ONEWORD(ICMD_ASTORE, opcode - JAVA_ASTORE_0);
1828                         break;
1829
1830                 case JAVA_IINC:
1831                         {
1832                                 int v;
1833                                 
1834                                 if (!iswide) {
1835                                         i = code_get_u1(p + 1,m);
1836                                         v = code_get_s1(p + 2,m);
1837
1838                                 } 
1839                                 else {
1840                                         i = code_get_u2(p + 1,m);
1841                                         v = code_get_s2(p + 3,m);
1842                                         iswide = false;
1843                                         nextp = p + 5;
1844                                 }
1845                                 INDEX_ONEWORD(i);
1846                                 OP2I(opcode, i, v);
1847                         }
1848                         break;
1849
1850                         /* wider index for loading, storing and incrementing */
1851
1852                 case JAVA_WIDE:
1853                         iswide = true;
1854                         p++;
1855                         goto fetch_opcode;
1856
1857                 /* managing arrays ****************************************************/
1858
1859                 case JAVA_NEWARRAY:
1860                         switch (code_get_s1(p + 1, m)) {
1861                         case 4:
1862                                 bte = builtintable_get_internal(BUILTIN_newarray_boolean);
1863                                 break;
1864                         case 5:
1865                                 bte = builtintable_get_internal(BUILTIN_newarray_char);
1866                                 break;
1867                         case 6:
1868                                 bte = builtintable_get_internal(BUILTIN_newarray_float);
1869                                 break;
1870                         case 7:
1871                                 bte = builtintable_get_internal(BUILTIN_newarray_double);
1872                                 break;
1873                         case 8:
1874                                 bte = builtintable_get_internal(BUILTIN_newarray_byte);
1875                                 break;
1876                         case 9:
1877                                 bte = builtintable_get_internal(BUILTIN_newarray_short);
1878                                 break;
1879                         case 10:
1880                                 bte = builtintable_get_internal(BUILTIN_newarray_int);
1881                                 break;
1882                         case 11:
1883                                 bte = builtintable_get_internal(BUILTIN_newarray_long);
1884                                 break;
1885 #if defined(ENABLE_VERIFIER)
1886                         default:
1887                                 *exceptionptr = new_verifyerror(m,
1888                                                 "Invalid array-type to create");
1889                                 return false;
1890 #endif
1891                         }
1892                         BUILTIN(bte, true, NULL, currentline);
1893                         break;
1894
1895                 case JAVA_ANEWARRAY:
1896                         i = code_get_u2(p + 1, m);
1897                         compr = (constant_classref *) class_getconstant(m->class, i, CONSTANT_Class);
1898                         if (!compr)
1899                                 return false;
1900
1901                         if (!(cr = class_get_classref_multiarray_of(1, compr)))
1902                                 return false;
1903
1904                         if (!resolve_classref(m, cr, resolveLazy, true, true, &c))
1905                                 return false;
1906
1907                         LOADCONST_A_BUILTIN(c, cr);
1908                         bte = builtintable_get_internal(BUILTIN_newarray);
1909                         BUILTIN(bte, true, NULL, currentline);
1910                         s_count++;
1911                         break;
1912
1913                 case JAVA_MULTIANEWARRAY:
1914                         m->isleafmethod = false;
1915                         i = code_get_u2(p + 1, m);
1916                         {
1917                                 s4 v = code_get_u1(p + 3, m);
1918
1919                                 cr = (constant_classref *) class_getconstant(m->class, i, CONSTANT_Class);
1920                                 if (!cr)
1921                                         return false;
1922
1923                                 if (!resolve_classref(m, cr, resolveLazy, true, true, &c))
1924                                         return false;
1925
1926                                 /* if unresolved, c == NULL */
1927                                 OP2AT(opcode, v, c, cr, currentline);
1928                         }
1929                         break;
1930
1931                 case JAVA_IFEQ:
1932                 case JAVA_IFLT:
1933                 case JAVA_IFLE:
1934                 case JAVA_IFNE:
1935                 case JAVA_IFGT:
1936                 case JAVA_IFGE:
1937                 case JAVA_IFNULL:
1938                 case JAVA_IFNONNULL:
1939                 case JAVA_IF_ICMPEQ:
1940                 case JAVA_IF_ICMPNE:
1941                 case JAVA_IF_ICMPLT:
1942                 case JAVA_IF_ICMPGT:
1943                 case JAVA_IF_ICMPLE:
1944                 case JAVA_IF_ICMPGE:
1945                 case JAVA_IF_ACMPEQ:
1946                 case JAVA_IF_ACMPNE:
1947                 case JAVA_GOTO:
1948                 case JAVA_JSR:
1949                         i = p + code_get_s2(p + 1,m);
1950                         CHECK_BYTECODE_INDEX(i);
1951                         block_insert(i);
1952                         blockend = true;
1953                         OP1(opcode, i);
1954                         break;
1955
1956                 case JAVA_GOTO_W:
1957                 case JAVA_JSR_W:
1958                         i = p + code_get_s4(p + 1,m);
1959                         CHECK_BYTECODE_INDEX(i);
1960                         block_insert(i);
1961                         blockend = true;
1962                         OP1(opcode, i);
1963                         break;
1964
1965                 case JAVA_RET:
1966                         if (!iswide) {
1967                                 i = code_get_u1(p + 1,m);
1968                         } 
1969                         else {
1970                                 i = code_get_u2(p + 1,m);
1971                                 nextp = p + 3;
1972                                 iswide = false;
1973                         }
1974                         blockend = true;
1975                                 
1976                         OP1LOAD_ONEWORD(opcode, i);
1977                         break;
1978
1979                 case JAVA_IRETURN:
1980                 case JAVA_LRETURN:
1981                 case JAVA_FRETURN:
1982                 case JAVA_DRETURN:
1983                 case JAVA_ARETURN:
1984                 case JAVA_RETURN:
1985                         blockend = true;
1986                         /* zero val.a so no patcher is inserted */
1987                         /* the type checker may set this later  */
1988                         iptr->val.a = NULL;
1989                         OP(opcode);
1990                         break;
1991
1992                 case JAVA_ATHROW:
1993                         blockend = true;
1994                         /* zero val.a so no patcher is inserted */
1995                         /* the type checker may set this later  */
1996                         iptr->val.a = NULL;
1997                         OP(opcode);
1998                         break;
1999                                 
2000
2001                 /* table jumps ********************************************************/
2002
2003                 case JAVA_LOOKUPSWITCH:
2004                         {
2005                                 s4 num, j;
2006                                 s4 *tablep;
2007 #if defined(ENABLE_VERIFIER)
2008                                 s4 prevvalue = 0;
2009 #endif
2010
2011                                 blockend = true;
2012                                 nextp = ALIGN((p + 1), 4);
2013
2014                                 CHECK_END_OF_BYTECODE(nextp + 8);
2015
2016                                 tablep = (s4 *) (m->jcode + nextp);
2017
2018                                 OP2A(opcode, 0, tablep, currentline);
2019
2020                                 /* default target */
2021
2022                                 j =  p + code_get_s4(nextp, m);
2023                                 *tablep = j;     /* restore for little endian */
2024                                 tablep++;
2025                                 nextp += 4;
2026                                 CHECK_BYTECODE_INDEX(j);
2027                                 block_insert(j);
2028
2029                                 /* number of pairs */
2030
2031                                 num = code_get_u4(nextp, m);
2032                                 *tablep = num;
2033                                 tablep++;
2034                                 nextp += 4;
2035
2036                                 CHECK_END_OF_BYTECODE(nextp + 8 * num);
2037
2038                                 for (i = 0; i < num; i++) {
2039                                         /* value */
2040
2041                                         j = code_get_s4(nextp, m);
2042                                         *tablep = j; /* restore for little endian */
2043                                         tablep++;
2044                                         nextp += 4;
2045
2046 #if defined(ENABLE_VERIFIER)
2047                                         /* check if the lookup table is sorted correctly */
2048                                         
2049                                         if (i && (j <= prevvalue)) {
2050                                                 *exceptionptr = new_verifyerror(m, "Unsorted lookup switch");
2051                                                 return false;
2052                                         }
2053                                         prevvalue = j;
2054 #endif
2055
2056                                         /* target */
2057
2058                                         j = p + code_get_s4(nextp,m);
2059                                         *tablep = j; /* restore for little endian */
2060                                         tablep++;
2061                                         nextp += 4;
2062                                         CHECK_BYTECODE_INDEX(j);
2063                                         block_insert(j);
2064                                 }
2065
2066                                 break;
2067                         }
2068
2069
2070                 case JAVA_TABLESWITCH:
2071                         {
2072                                 s4 num, j;
2073                                 s4 *tablep;
2074
2075                                 blockend = true;
2076                                 nextp = ALIGN((p + 1), 4);
2077
2078                                 CHECK_END_OF_BYTECODE(nextp + 12);
2079
2080                                 tablep = (s4 *) (m->jcode + nextp);
2081
2082                                 OP2A(opcode, 0, tablep, currentline);
2083
2084                                 /* default target */
2085
2086                                 j = p + code_get_s4(nextp, m);
2087                                 *tablep = j;     /* restore for little endian */
2088                                 tablep++;
2089                                 nextp += 4;
2090                                 CHECK_BYTECODE_INDEX(j);
2091                                 block_insert(j);
2092
2093                                 /* lower bound */
2094
2095                                 j = code_get_s4(nextp, m);
2096                                 *tablep = j;     /* restore for little endian */
2097                                 tablep++;
2098                                 nextp += 4;
2099
2100                                 /* upper bound */
2101
2102                                 num = code_get_s4(nextp, m);
2103                                 *tablep = num;   /* restore for little endian */
2104                                 tablep++;
2105                                 nextp += 4;
2106
2107                                 num -= j;  /* difference of upper - lower */
2108
2109 #if defined(ENABLE_VERIFIER)
2110                                 if (num < 0) {
2111                                         *exceptionptr = new_verifyerror(m,
2112                                                         "invalid TABLESWITCH: upper bound < lower bound");
2113                                         return false;
2114                                 }
2115 #endif
2116
2117                                 CHECK_END_OF_BYTECODE(nextp + 4 * (num + 1));
2118
2119                                 for (i = 0; i <= num; i++) {
2120                                         j = p + code_get_s4(nextp,m);
2121                                         *tablep = j; /* restore for little endian */
2122                                         tablep++;
2123                                         nextp += 4;
2124                                         CHECK_BYTECODE_INDEX(j);
2125                                         block_insert(j);
2126                                 }
2127
2128                                 break;
2129                         }
2130
2131
2132                 /* load and store of object fields ************************************/
2133
2134                 case JAVA_AASTORE:
2135                         OP(opcode);
2136                         m->isleafmethod = false;
2137                         break;
2138
2139                 case JAVA_GETSTATIC:
2140                 case JAVA_PUTSTATIC:
2141                 case JAVA_GETFIELD:
2142                 case JAVA_PUTFIELD:
2143                         {
2144                                 constant_FMIref  *fr;
2145                                 unresolved_field *uf;
2146
2147                                 i = code_get_u2(p + 1, m);
2148                                 fr = class_getconstant(m->class, i,
2149                                                                            CONSTANT_Fieldref);
2150                                 if (!fr)
2151                                         return false;
2152
2153                                 OP2A_NOINC(opcode, fr->parseddesc.fd->type, fr, currentline);
2154
2155                                 /* only with -noverify, otherwise the typechecker does this */
2156
2157 #if defined(ENABLE_VERIFIER)
2158                                 if (!opt_verify) {
2159 #endif
2160                                         result = resolve_field_lazy(iptr,NULL,m);
2161                                         if (result == resolveFailed)
2162                                                 return false;
2163
2164                                         if (result != resolveSucceeded) {
2165                                                 uf = create_unresolved_field(m->class,
2166                                                                                                          m, iptr);
2167
2168                                                 if (!uf)
2169                                                         return false;
2170
2171                                                 /* store the unresolved_field pointer */
2172
2173                                                 /* XXX this will be changed */
2174                                                 iptr->val.a = uf;
2175                                                 iptr->target = (void*) 0x01; /* XXX target temporarily used as flag */
2176                                         }
2177                                         else {
2178                                                 iptr->target = NULL;
2179                                         }
2180 #if defined(ENABLE_VERIFIER)
2181                                 }
2182                                 else {
2183                                         iptr->target = NULL;
2184                                 }
2185 #endif
2186                                 PINC;
2187                         }
2188                         break;
2189                                 
2190
2191                 /* method invocation **************************************************/
2192
2193                 case JAVA_INVOKESTATIC:
2194                         i = code_get_u2(p + 1, m);
2195                         mr = class_getconstant(m->class, i,
2196                                         CONSTANT_Methodref);
2197                         if (!mr)
2198                                 return false;
2199
2200                         md = mr->parseddesc.md;
2201
2202                         if (!md->params)
2203                                 if (!descriptor_params_from_paramtypes(md, ACC_STATIC))
2204                                         return false;
2205
2206                         goto invoke_method;
2207
2208                 case JAVA_INVOKEINTERFACE:
2209                         i = code_get_u2(p + 1, m);
2210                                 
2211                         mr = class_getconstant(m->class, i,
2212                                         CONSTANT_InterfaceMethodref);
2213
2214                         goto invoke_nonstatic_method;
2215
2216                 case JAVA_INVOKESPECIAL:
2217                 case JAVA_INVOKEVIRTUAL:
2218                         i = code_get_u2(p + 1, m);
2219                         mr = class_getconstant(m->class, i,
2220                                         CONSTANT_Methodref);
2221
2222 invoke_nonstatic_method:
2223                         if (!mr)
2224                                 return false;
2225
2226                         md = mr->parseddesc.md;
2227
2228                         if (!md->params)
2229                                 if (!descriptor_params_from_paramtypes(md, 0))
2230                                         return false;
2231
2232 invoke_method:
2233                         m->isleafmethod = false;
2234
2235                         OP2A_NOINC(opcode, 0, mr, currentline);
2236
2237                         /* only with -noverify, otherwise the typechecker does this */
2238
2239 #if defined(ENABLE_VERIFIER)
2240                         if (!opt_verify) {
2241 #endif
2242                                 result = resolve_method_lazy(iptr,NULL,m);
2243                                 if (result == resolveFailed)
2244                                         return false;
2245
2246                                 if (result != resolveSucceeded) {
2247                                         um = create_unresolved_method(m->class,
2248                                                         m, iptr);
2249
2250                                         if (!um)
2251                                                 return false;
2252
2253                                         /* store the unresolved_method pointer */
2254
2255                                         /* XXX this will be changed */
2256                                         iptr->val.a = um;
2257                                         iptr->target = (void*) 0x01; /* XXX target temporarily used as flag */
2258                                 }
2259                                 else {
2260                                         /* the method could be resolved */
2261                                         iptr->target = NULL;
2262                                 }
2263 #if defined(ENABLE_VERIFIER)
2264                         }
2265                         else {
2266                                 iptr->target = NULL;
2267                         }
2268 #endif
2269                         PINC;
2270                         break;
2271
2272                 /* miscellaneous object operations ************************************/
2273
2274                 case JAVA_NEW:
2275                         i = code_get_u2(p + 1, m);
2276                         cr = (constant_classref *) class_getconstant(m->class, i, CONSTANT_Class);
2277                         if (!cr)
2278                                 return false;
2279
2280                         if (!resolve_classref(m, cr, resolveLazy, true, true,
2281                                                                   &c))
2282                                 return false;
2283
2284                         LOADCONST_A_BUILTIN(c, cr);
2285                         bte = builtintable_get_internal(BUILTIN_new);
2286                         BUILTIN(bte, true, NULL, currentline);
2287                         s_count++;
2288                         break;
2289
2290                 case JAVA_CHECKCAST:
2291                         i = code_get_u2(p + 1, m);
2292                         cr = (constant_classref *) class_getconstant(m->class, i, CONSTANT_Class);
2293                         if (!cr)
2294                                 return false;
2295
2296                         if (!resolve_classref(m, cr, resolveLazy, true,
2297                                                                   true, &c))
2298                                 return false;
2299
2300                         if (cr->name->text[0] == '[') {
2301                                 /* array type cast-check */
2302                                 OP2AT(opcode, 0, c, cr, currentline);
2303                                 m->isleafmethod = false;
2304
2305                         } 
2306                         else {
2307                                 /* object type cast-check */
2308                                 OP2AT(opcode, 1, c, cr, currentline);
2309                         }
2310                         break;
2311
2312                 case JAVA_INSTANCEOF:
2313                         i = code_get_u2(p + 1,m);
2314                         cr = (constant_classref *) class_getconstant(m->class, i, CONSTANT_Class);
2315                         if (!cr)
2316                                 return false;
2317
2318                         if (!resolve_classref(m, cr, resolveLazy, true, true, &c))
2319                                 return false;
2320
2321                         if (cr->name->text[0] == '[') {
2322                                 /* array type cast-check */
2323                                 LOADCONST_A_BUILTIN(c, cr);
2324                                 bte = builtintable_get_internal(BUILTIN_arrayinstanceof);
2325                                 BUILTIN(bte, false, NULL, currentline);
2326                                 s_count++;
2327
2328                         } 
2329                         else {
2330                                 /* object type cast-check */
2331                                 OP2AT(opcode, 1, c, cr, currentline);
2332                         }
2333                         break;
2334
2335                 case JAVA_MONITORENTER:
2336 #if defined(ENABLE_THREADS)
2337                         if (checksync) {
2338                                 OP(ICMD_CHECKNULL);
2339                                 bte = builtintable_get_internal(BUILTIN_monitorenter);
2340                                 BUILTIN(bte, false, NULL, currentline);
2341                         } 
2342                         else
2343 #endif
2344                                 {
2345                                         OP(ICMD_CHECKNULL);
2346                                         OP(ICMD_POP);
2347                                 }
2348                         break;
2349
2350                 case JAVA_MONITOREXIT:
2351 #if defined(ENABLE_THREADS)
2352                         if (checksync) {
2353                                 bte = builtintable_get_internal(BUILTIN_monitorexit);
2354                                 BUILTIN(bte, false, NULL, currentline);
2355                         } 
2356                         else
2357 #endif
2358                                 {
2359                                         OP(ICMD_POP);
2360                                 }
2361                         break;
2362
2363                 /* any other basic operation ******************************************/
2364
2365                 case JAVA_IDIV:
2366 #if !SUPPORT_DIVISION
2367                         bte = builtintable_get_internal(BUILTIN_idiv);
2368                         OP2A(opcode, bte->md->paramcount, bte, currentline);
2369                         m->isleafmethod = false;
2370 #else
2371                         OP(opcode);
2372 #endif
2373                         break;
2374
2375                 case JAVA_IREM:
2376 #if !SUPPORT_DIVISION
2377                         bte = builtintable_get_internal(BUILTIN_irem);
2378                         OP2A(opcode, bte->md->paramcount, bte, currentline);
2379                         m->isleafmethod = false;
2380 #else
2381                         OP(opcode);
2382 #endif
2383                         break;
2384
2385                 case JAVA_LDIV:
2386 #if !(SUPPORT_DIVISION && SUPPORT_LONG && SUPPORT_LONG_DIV)
2387                         bte = builtintable_get_internal(BUILTIN_ldiv);
2388                         OP2A(opcode, bte->md->paramcount, bte, currentline);
2389                         m->isleafmethod = false;
2390 #else
2391                         OP(opcode);
2392 #endif
2393                         break;
2394
2395                 case JAVA_LREM:
2396 #if !(SUPPORT_DIVISION && SUPPORT_LONG && SUPPORT_LONG_DIV)
2397                         bte = builtintable_get_internal(BUILTIN_lrem);
2398                         OP2A(opcode, bte->md->paramcount, bte, currentline);
2399                         m->isleafmethod = false;
2400 #else
2401                         OP(opcode);
2402 #endif
2403                         break;
2404
2405                 case JAVA_FREM:
2406 #if defined(__I386__)
2407                         OP(opcode);
2408 #else
2409                         bte = builtintable_get_internal(BUILTIN_frem);
2410                         BUILTIN(bte, false, NULL, currentline);
2411 #endif
2412                         break;
2413
2414                 case JAVA_DREM:
2415 #if defined(__I386__)
2416                         OP(opcode);
2417 #else
2418                         bte = builtintable_get_internal(BUILTIN_drem);
2419                         BUILTIN(bte, false, NULL, currentline);
2420 #endif
2421                         break;
2422
2423                 case JAVA_F2I:
2424 #if defined(__ALPHA__)
2425                         if (!opt_noieee) {
2426                                 bte = builtintable_get_internal(BUILTIN_f2i);
2427                                 BUILTIN(bte, false, NULL, currentline);
2428                         } 
2429                         else
2430 #endif
2431                                 {
2432                                         OP(opcode);
2433                                 }
2434                         break;
2435
2436                 case JAVA_F2L:
2437 #if defined(__ALPHA__)
2438                         if (!opt_noieee) {
2439                                 bte = builtintable_get_internal(BUILTIN_f2l);
2440                                 BUILTIN(bte, false, NULL, currentline);
2441                         } 
2442                         else 
2443 #endif
2444                                 {
2445                                         OP(opcode);
2446                                 }
2447                         break;
2448
2449                 case JAVA_D2I:
2450 #if defined(__ALPHA__)
2451                         if (!opt_noieee) {
2452                                 bte = builtintable_get_internal(BUILTIN_d2i);
2453                                 BUILTIN(bte, false, NULL, currentline);
2454                         } 
2455                         else
2456 #endif
2457                                 {
2458                                         OP(opcode);
2459                                 }
2460                         break;
2461
2462                 case JAVA_D2L:
2463 #if defined(__ALPHA__)
2464                         if (!opt_noieee) {
2465                                 bte = builtintable_get_internal(BUILTIN_d2l);
2466                                 BUILTIN(bte, false, NULL, currentline);
2467                         } 
2468                         else
2469 #endif
2470                                 {
2471                                         OP(opcode);
2472                                 }
2473                         break;
2474
2475                         /* check for invalid opcodes if the verifier is enabled */
2476 #if defined(ENABLE_VERIFIER)
2477                 case JAVA_BREAKPOINT:
2478                         *exceptionptr =
2479                                 new_verifyerror(m, "Quick instructions shouldn't appear yet.");
2480                         return false;
2481
2482                 case 186: /* unused opcode */
2483                 case 203:
2484                 case 204:
2485                 case 205:
2486                 case 206:
2487                 case 207:
2488                 case 208:
2489                 case 209:
2490                 case 210:
2491                 case 211:
2492                 case 212:
2493                 case 213:
2494                 case 214:
2495                 case 215:
2496                 case 216:
2497                 case 217:
2498                 case 218:
2499                 case 219:
2500                 case 220:
2501                 case 221:
2502                 case 222:
2503                 case 223:
2504                 case 224:
2505                 case 225:
2506                 case 226:
2507                 case 227:
2508                 case 228:
2509                 case 229:
2510                 case 230:
2511                 case 231:
2512                 case 232:
2513                 case 233:
2514                 case 234:
2515                 case 235:
2516                 case 236:
2517                 case 237:
2518                 case 238:
2519                 case 239:
2520                 case 240:
2521                 case 241:
2522                 case 242:
2523                 case 243:
2524                 case 244:
2525                 case 245:
2526                 case 246:
2527                 case 247:
2528                 case 248:
2529                 case 249:
2530                 case 250:
2531                 case 251:
2532                 case 252:
2533                 case 253:
2534                 case 254:
2535                 case 255:
2536                         *exceptionptr =
2537                                 new_verifyerror(m,"Illegal opcode %d at instr %d\n",
2538                                                                   opcode, ipc);
2539                         return false;
2540                         break;
2541 #endif /* defined(ENABLE_VERIFIER) */
2542
2543                 default:
2544                         /* straight-forward translation to ICMD */
2545                         OP(opcode);
2546                         break;
2547                                 
2548                 } /* end switch */
2549
2550 #if defined(ENABLE_VERIFIER)
2551                 /* If WIDE was used correctly, iswide should have been reset by now. */
2552                 if (iswide) {
2553                         *exceptionptr = new_verifyerror(m,
2554                                         "Illegal instruction: WIDE before incompatible opcode");
2555                         return false;
2556                 }
2557 #endif /* defined(ENABLE_VERIFIER) */
2558
2559         } /* end for */
2560
2561 #if defined(ENABLE_VERIFIER)
2562         if (p != m->jcodelength) {
2563                 *exceptionptr = new_verifyerror(m,
2564                                 "Command-sequence crosses code-boundary");
2565                 return false;
2566         }
2567
2568         if (!blockend) {
2569                 *exceptionptr = new_verifyerror(m, "Falling off the end of the code");
2570                 return false;
2571         }
2572 #endif /* defined(ENABLE_VERIFIER) */
2573
2574         /* adjust block count if target 0 is not first intermediate instruction */
2575
2576         if (!m->basicblockindex[0] || (m->basicblockindex[0] > 1))
2577                 b_count++;
2578
2579         /* copy local to method variables */
2580
2581         m->instructioncount = ipc;
2582         m->basicblockcount = b_count;
2583         m->stackcount = s_count + m->basicblockcount * m->maxstack;
2584
2585         /* allocate stack table */
2586
2587         m->stack = DMNEW(stackelement, m->stackcount);
2588
2589         {
2590                 basicblock *bptr;
2591
2592                 bptr = m->basicblocks = DMNEW(basicblock, b_count + 1);    /* one more for end ipc */
2593
2594                 b_count = 0;
2595                 m->c_debug_nr = 0;
2596         
2597                 /* additional block if target 0 is not first intermediate instruction */
2598
2599                 if (!m->basicblockindex[0] || (m->basicblockindex[0] > 1)) {
2600                         BASICBLOCK_INIT(bptr,m);
2601
2602                         bptr->iinstr = m->instructions;
2603                         /* bptr->icount is set when the next block is allocated */
2604
2605                         bptr++;
2606                         b_count++;
2607                         bptr[-1].next = bptr;
2608                 }
2609
2610                 /* allocate blocks */
2611
2612                 for (p = 0; p < m->jcodelength; p++) { 
2613                         if (m->basicblockindex[p] & 1) {
2614                                 /* Check if this block starts at the beginning of an          */
2615                                 /* instruction.                                               */
2616 #if defined(ENABLE_VERIFIER)
2617                                 if (!instructionstart[p]) {
2618                                         *exceptionptr = new_verifyerror(m,
2619                                                 "Branch into middle of instruction");
2620                                         return false;
2621                                 }
2622 #endif
2623
2624                                 /* allocate the block */
2625
2626                                 BASICBLOCK_INIT(bptr,m);
2627
2628                                 bptr->iinstr = m->instructions + (m->basicblockindex[p] >> 1);
2629                                 if (b_count) {
2630                                         bptr[-1].icount = bptr->iinstr - bptr[-1].iinstr;
2631                                 }
2632                                 /* bptr->icount is set when the next block is allocated */
2633
2634                                 m->basicblockindex[p] = b_count;
2635
2636                                 bptr++;
2637                                 b_count++;
2638                                 bptr[-1].next = bptr;
2639                         }
2640                 }
2641
2642                 /* set instruction count of last real block */
2643
2644                 if (b_count) {
2645                         bptr[-1].icount = (m->instructions + m->instructioncount) - bptr[-1].iinstr;
2646                 }
2647
2648                 /* allocate additional block at end */
2649
2650                 BASICBLOCK_INIT(bptr,m);
2651                 
2652                 bptr->instack = bptr->outstack = NULL;
2653                 bptr->indepth = bptr->outdepth = 0;
2654                 bptr->iinstr = NULL;
2655                 bptr->icount = 0;
2656                 bptr->next = NULL;
2657
2658                 /* set basicblock pointers in exception table */
2659
2660                 if (cd->exceptiontablelength > 0) {
2661                         cd->exceptiontable[cd->exceptiontablelength - 1].down = NULL;
2662                 }
2663                 
2664                 for (i = 0; i < cd->exceptiontablelength; ++i) {
2665                         p = cd->exceptiontable[i].startpc;
2666                         cd->exceptiontable[i].start = m->basicblocks + m->basicblockindex[p];
2667
2668                         p = cd->exceptiontable[i].endpc;
2669                         cd->exceptiontable[i].end = (p == m->jcodelength) ? (m->basicblocks + m->basicblockcount /*+ 1*/) : (m->basicblocks + m->basicblockindex[p]);
2670
2671                         p = cd->exceptiontable[i].handlerpc;
2672                         cd->exceptiontable[i].handler = m->basicblocks + m->basicblockindex[p];
2673             }
2674
2675                 /* XXX activate this if you want to try inlining */
2676 #if 0
2677                 for (i = 0; i < m->exceptiontablelength; ++i) {
2678                         p = m->exceptiontable[i].startpc;
2679                         m->exceptiontable[i].start = m->basicblocks + m->basicblockindex[p];
2680
2681                         p = m->exceptiontable[i].endpc;
2682                         m->exceptiontable[i].end = (p == m->jcodelength) ? (m->basicblocks + m->basicblockcount /*+ 1*/) : (m->basicblocks + m->basicblockindex[p]);
2683
2684                         p = m->exceptiontable[i].handlerpc;
2685                         m->exceptiontable[i].handler = m->basicblocks + m->basicblockindex[p];
2686             }
2687 #endif
2688
2689         }
2690
2691         /* everything's ok */
2692
2693         return true;
2694
2695 #if defined(ENABLE_VERIFIER)
2696
2697 throw_unexpected_end_of_bytecode:
2698         *exceptionptr = new_verifyerror(m, "Unexpected end of bytecode");
2699         return false;
2700
2701 throw_invalid_bytecode_index:
2702         *exceptionptr =
2703                 new_verifyerror(m, "Illegal target of branch instruction");
2704         return false;
2705
2706 throw_illegal_local_variable_number:
2707         *exceptionptr =
2708                 new_verifyerror(m, "Illegal local variable number");
2709         return false;
2710                 
2711 #endif /* ENABLE_VERIFIER */
2712 }
2713
2714
2715 /*
2716  * These are local overrides for various environment variables in Emacs.
2717  * Please do not remove this and leave it at the end of the file, where
2718  * Emacs will automagically detect them.
2719  * ---------------------------------------------------------------------
2720  * Local variables:
2721  * mode: c
2722  * indent-tabs-mode: t
2723  * c-basic-offset: 4
2724  * tab-width: 4
2725  * End:
2726  * vim:noexpandtab:sw=4:ts=4:
2727  */