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