* Removed all Id tags.
[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, 2007 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 */
26
27
28 #include "config.h"
29
30 #include <assert.h>
31 #include <string.h>
32
33 #include "vm/types.h"
34
35 #include "mm/memory.h"
36
37 #include "native/native.h"
38
39 #include "threads/lock-common.h"
40
41 #include "toolbox/logging.h"
42
43 #include "vm/builtin.h"
44 #include "vm/exceptions.h"
45 #include "vm/global.h"
46 #include "vm/stringlocal.h"
47
48 #include "vm/jit/asmpart.h"
49 #include "vm/jit/jit.h"
50 #include "vm/jit/parse.h"
51 #include "vm/jit/loop/loop.h"
52
53 #include "vmcore/linker.h"
54 #include "vmcore/loader.h"
55 #include "vmcore/options.h"
56 #include "vm/resolve.h"
57
58 #if defined(ENABLE_STATISTICS)
59 # include "vmcore/statistics.h"
60 #endif
61
62 #include "vmcore/suck.h"
63
64 #define INSTRUCTIONS_INCREMENT  5  /* number of additional instructions to    */
65                                    /* allocate if space runs out              */
66
67
68 /* local macros ***************************************************************/
69
70 #define BYTECODEINDEX_TO_BASICBLOCK(dst) \
71     do { \
72         (dst).block = \
73             parse_bytecodeindex_to_basicblock(jd, &pd, (dst).insindex); \
74     } while (0)
75
76
77 /* parserdata_t ***************************************************************/
78
79 typedef struct parsedata_t parsedata_t;
80
81 struct parsedata_t {
82         u1          *bytecodestart;         /* start of bytecode instructions     */
83         u1          *basicblockstart;       /* start of bytecode basic-blocks     */
84
85         s4          *bytecodemap;           /* bytecode to IR mapping             */
86         
87         instruction *instructions;          /* instruction array                  */
88         s4           instructionslength;    /* length of the instruction array    */
89
90         s4          *instructionmap;        /* IR to basic-block mapping          */
91 };
92
93
94 /* parse_setup *****************************************************************
95
96    Fills the passed parsedata_t structure.
97
98 *******************************************************************************/
99
100 static void parse_setup(jitdata *jd, parsedata_t *pd)
101 {
102         methodinfo *m;
103
104         /* get required compiler data */
105
106         m = jd->m;
107
108         /* bytecode start array */
109
110         pd->bytecodestart = DMNEW(u1, m->jcodelength + 1);
111         MZERO(pd->bytecodestart, u1, m->jcodelength + 1);
112
113         /* bytecode basic-block start array */
114
115         pd->basicblockstart = DMNEW(u1, m->jcodelength + 1);
116         MZERO(pd->basicblockstart, u1, m->jcodelength + 1);
117
118         /* bytecode instruction index to IR instruction mapping */
119
120         pd->bytecodemap = DMNEW(s4, m->jcodelength + 1);
121         MSET(pd->bytecodemap, -1, s4, m->jcodelength + 1);
122
123         /* allocate the instruction array */
124
125         pd->instructionslength = m->jcodelength + 1;
126         pd->instructions = DMNEW(instruction, pd->instructionslength);
127
128         /* Zero the intermediate instructions array so we don't have any
129            invalid pointers in it if we cannot finish stack_analyse(). */
130
131         MZERO(pd->instructions, instruction, pd->instructionslength);
132
133         /* The instructionmap is allocated later when we know the count of
134            instructions. */
135
136         pd->instructionmap = NULL;
137 }
138
139
140 /* parse_realloc_instructions **************************************************
141
142    Reallocate the instructions array so there is room for at least N 
143    additional instructions.
144
145    RETURN VALUE:
146        the new value for iptr
147
148 *******************************************************************************/
149
150 static instruction *parse_realloc_instructions(parsedata_t *pd, s4 icount, s4 n)
151 {
152         /* increase the size of the instruction array */
153
154         pd->instructionslength += (n + INSTRUCTIONS_INCREMENT);
155
156         /* reallocate the array */
157
158         pd->instructions = DMREALLOC(pd->instructions, instruction, icount,
159                                                                  pd->instructionslength);
160         MZERO(pd->instructions + icount, instruction,
161                   (pd->instructionslength - icount));
162
163         /* return the iptr */
164
165         return pd->instructions + icount;
166 }
167
168
169 /* parse_bytecodeindex_to_basicblock *******************************************
170
171    Resolves a bytecode index to the corresponding basic block.
172
173 *******************************************************************************/
174
175 static basicblock *parse_bytecodeindex_to_basicblock(jitdata *jd,
176                                                                                                          parsedata_t *pd,
177                                                                                                          s4 bcindex)
178 {
179         s4          irindex;
180         basicblock *bb;
181
182         irindex = pd->bytecodemap[bcindex];
183         bb      = jd->basicblocks + pd->instructionmap[irindex];
184
185         return bb;
186 }
187
188
189 /* parse_mark_exception_boundaries *********************************************
190
191    Mark exception handlers and the boundaries of the handled regions as
192    basic block boundaries.
193
194    IN:
195        jd...............current jitdata
196
197    RETURN VALUE:
198        true.............everything ok
199            false............an exception has been thrown
200
201 *******************************************************************************/
202
203 static bool parse_mark_exception_boundaries(jitdata *jd, parsedata_t *pd)
204 {
205         s4                   bcindex;
206         s4                   i;
207         s4                   len;
208         raw_exception_entry *rex;
209         methodinfo          *m;
210
211         m = jd->m;
212         
213         len = m->rawexceptiontablelength;
214
215         if (len == 0)
216                 return true;
217
218         rex = m->rawexceptiontable;
219
220         for (i = 0; i < len; ++i, ++rex) {
221
222                 /* the start of the handled region becomes a basic block start */
223
224                 bcindex = rex->startpc;
225                 CHECK_BYTECODE_INDEX(bcindex);
226                 MARK_BASICBLOCK(pd, bcindex);
227                 
228                 bcindex = rex->endpc; /* see JVM Spec 4.7.3 */
229                 CHECK_BYTECODE_INDEX_EXCLUSIVE(bcindex);
230
231                 /* check that the range is valid */
232
233 #if defined(ENABLE_VERIFIER)
234                 if (bcindex <= rex->startpc) {
235                         exceptions_throw_verifyerror(m, "Invalid exception handler range");
236                         return false;
237                 }
238 #endif
239                 
240                 /* End of handled region becomes a basic block boundary (if it
241                    is the bytecode end, we'll use the special end block that
242                    is created anyway). */
243
244                 if (bcindex < m->jcodelength)
245                         MARK_BASICBLOCK(pd, bcindex);
246                 else
247                         jd->branchtoend = true;
248
249                 /* the start of the handler becomes a basic block start  */
250
251                 bcindex = rex->handlerpc;
252                 CHECK_BYTECODE_INDEX(bcindex);
253                 MARK_BASICBLOCK(pd, bcindex);
254         }
255
256         /* everything ok */
257
258         return true;
259
260 #if defined(ENABLE_VERIFIER)
261 throw_invalid_bytecode_index:
262         exceptions_throw_verifyerror(m,
263                                                                  "Illegal bytecode index in exception table");
264         return false;
265 #endif
266 }
267
268
269 /* parse_resolve_exception_table ***********************************************
270
271    Enter the exception handlers and their ranges, resolved to basicblock *s,
272    in the jitdata.
273
274    IN:
275        jd...............current jitdata
276
277    RETURN VALUE:
278            true.............everything ok
279            false............an exception has been thrown
280
281 *******************************************************************************/
282
283 static bool parse_resolve_exception_table(jitdata *jd, parsedata_t *pd)
284 {
285         methodinfo          *m;
286         raw_exception_entry *rex;
287         exception_entry     *ex;
288         s4                   i;
289         s4                   len;
290         classinfo           *exclass;
291
292         m = jd->m;
293
294         len = m->rawexceptiontablelength;
295
296         /* common case: no handler entries */
297
298         if (len == 0)
299                 return true;
300
301         /* allocate the exception table */
302
303         jd->exceptiontablelength = len;
304         jd->exceptiontable = DMNEW(exception_entry, len + 1); /* XXX why +1? */
305
306         /* copy and resolve the entries */
307
308         ex = jd->exceptiontable;
309         rex = m->rawexceptiontable;
310
311         for (i = 0; i < len; ++i, ++rex, ++ex) {
312                 /* resolve instruction indices to basic blocks */
313
314                 ex->start   = parse_bytecodeindex_to_basicblock(jd, pd, rex->startpc);
315                 ex->end     = parse_bytecodeindex_to_basicblock(jd, pd, rex->endpc);
316                 ex->handler = parse_bytecodeindex_to_basicblock(jd, pd, rex->handlerpc);
317
318                 /* lazily resolve the catchtype */
319
320                 if (rex->catchtype.any != NULL) {
321                         if (!resolve_classref_or_classinfo(m,
322                                                                                            rex->catchtype,
323                                                                                            resolveLazy, true, false,
324                                                                                            &exclass))
325                                 return false;
326
327                         /* if resolved, enter the result of resolution in the table */
328
329                         if (exclass != NULL)
330                                 rex->catchtype.cls = exclass;
331                 }
332
333                 ex->catchtype = rex->catchtype;
334                 ex->next = NULL;   /* set by loop analysis */
335                 ex->down = ex + 1; /* link to next exception entry */
336         }
337
338         /* terminate the ->down linked list */
339
340         assert(ex != jd->exceptiontable);
341         ex[-1].down = NULL;
342
343         return true;
344 }
345
346
347 /*******************************************************************************
348
349         function 'parse' scans the JavaVM code and generates intermediate code
350
351         During parsing the block index table is used to store at bit pos 0
352         a flag which marks basic block starts and at position 1 to 31 the
353         intermediate instruction index. After parsing the block index table
354         is scanned, for marked positions a block is generated and the block
355         number is stored in the block index table.
356
357 *******************************************************************************/
358
359 /*** macro for checking the length of the bytecode ***/
360
361 #if defined(ENABLE_VERIFIER)
362 #define CHECK_END_OF_BYTECODE(neededlength) \
363         do { \
364                 if ((neededlength) > m->jcodelength) \
365                         goto throw_unexpected_end_of_bytecode; \
366         } while (0)
367 #else /* !ENABLE_VERIFIER */
368 #define CHECK_END_OF_BYTECODE(neededlength)
369 #endif /* ENABLE_VERIFIER */
370
371 bool parse(jitdata *jd)
372 {
373         methodinfo  *m;                     /* method being parsed                */
374         parsedata_t  pd;
375         instruction *iptr;                  /* current ptr into instruction array */
376
377         s4           bcindex;               /* bytecode instruction index         */
378         s4           nextbc;                /* start of next bytecode instruction */
379         s4           opcode;                /* bytecode instruction opcode        */
380
381         s4           irindex;               /* IR instruction index               */
382         s4           ircount;               /* IR instruction count               */
383
384         s4           bbcount;               /* basic block count                  */
385
386         int  s_count = 0;             /* stack element counter                    */
387         bool blockend;                /* true if basic block end has been reached */
388         bool iswide;                  /* true if last instruction was a wide      */
389
390         constant_classref  *cr;
391         constant_classref  *compr;
392         classinfo          *c;
393         builtintable_entry *bte;
394         constant_FMIref    *fmi;
395         methoddesc         *md;
396         unresolved_method  *um;
397         unresolved_field   *uf;
398
399         resolve_result_t    result;
400         u2                  lineindex = 0;
401         u2                  currentline = 0;
402         u2                  linepcchange = 0;
403         u4                  flags;
404         basicblock         *bptr;
405
406         int                *local_map; /* local pointer to renaming map           */
407                                        /* is assigned to rd->local_map at the end */
408         branch_target_t *table;
409         lookup_target_t *lookup;
410         s4               i;
411         s4               j;
412
413         /* get required compiler data */
414
415         m = jd->m;
416
417         /* allocate buffers for local variable renaming */
418
419         local_map = DMNEW(int, m->maxlocals * 5);
420
421         for (i = 0; i < m->maxlocals; i++) {
422                 local_map[i * 5 + 0] = 0;
423                 local_map[i * 5 + 1] = 0;
424                 local_map[i * 5 + 2] = 0;
425                 local_map[i * 5 + 3] = 0;
426                 local_map[i * 5 + 4] = 0;
427         }
428
429         /* initialize the parse data structures */
430   
431         parse_setup(jd, &pd);
432   
433         /* initialize local variables */
434   
435         iptr     = pd.instructions;
436         ircount  = 0;
437         bbcount  = 0;
438         blockend = false;
439         iswide   = false;
440
441         /* mark basic block boundaries for exception table */
442
443         if (!parse_mark_exception_boundaries(jd, &pd))
444                 return false;
445
446         /* initialize stack element counter */
447
448         s_count = 1 + m->rawexceptiontablelength;
449
450         /* setup line number info */
451
452         currentline = 0;
453         linepcchange = 0;
454
455         if (m->linenumbercount == 0) {
456                 lineindex = 0;
457         }
458         else {
459                 linepcchange = m->linenumbers[0].start_pc;
460         }
461
462         /*** LOOP OVER ALL BYTECODE INSTRUCTIONS **********************************/
463
464         for (bcindex = 0; bcindex < m->jcodelength; bcindex = nextbc) {
465
466                 /* mark this position as a valid bytecode instruction start */
467
468                 pd.bytecodestart[bcindex] = 1;
469
470                 /* change the current line number, if necessary */
471
472                 /* XXX rewrite this using pointer arithmetic */
473
474                 if (linepcchange == bcindex) {
475                         if (m->linenumbercount > lineindex) {
476 next_linenumber:
477                                 currentline = m->linenumbers[lineindex].line_number;
478                                 lineindex++;
479                                 if (lineindex < m->linenumbercount) {
480                                         linepcchange = m->linenumbers[lineindex].start_pc;
481                                         if (linepcchange == bcindex)
482                                                 goto next_linenumber;
483                                 }
484                         }
485                 }
486
487 fetch_opcode:
488                 /* fetch next opcode  */        
489
490                 opcode = SUCK_BE_U1(m->jcode + bcindex);
491
492                 /* If the previous instruction was a block-end instruction,
493                    mark the current bytecode instruction as basic-block
494                    starting instruction. */
495
496                 /* NOTE: Some compilers put a JAVA_NOP after a blockend
497                    instruction. */
498
499                 if (blockend && (opcode != JAVA_NOP)) {
500                         MARK_BASICBLOCK(&pd, bcindex);
501                         blockend = false;
502                 }
503
504                 /* If the current bytecode instruction was marked as
505                    basic-block starting instruction before (e.g. blockend,
506                    forward-branch target), mark the current IR instruction
507                    too. */
508
509                 if (pd.basicblockstart[bcindex] != 0) {
510                         /* We need a NOP as last instruction in each basic block
511                            for basic block reordering (may be replaced with a GOTO
512                            later). */
513
514                         INSTRUCTIONS_CHECK(1);
515                         OP(ICMD_NOP);
516                 }
517
518                 /* store intermediate instruction count (bit 0 mark block starts) */
519
520                 pd.bytecodemap[bcindex] = ircount;
521
522                 /* compute next instruction start */
523
524                 nextbc = bcindex + jcommandsize[opcode];
525
526                 CHECK_END_OF_BYTECODE(nextbc);
527
528                 /* add stack elements produced by this instruction */
529
530                 s_count += stackreq[opcode];
531
532                 /* We check here for the space of 1 instruction in the
533                    instruction array.  If an opcode is converted to more than
534                    1 instruction, this is checked in the corresponding
535                    case. */
536
537                 INSTRUCTIONS_CHECK(1);
538
539                 /* translate this bytecode instruction */
540                 switch (opcode) {
541
542                 case JAVA_NOP:
543                         break;
544
545                 /* pushing constants onto the stack ***********************************/
546
547                 case JAVA_BIPUSH:
548                         OP_LOADCONST_I(SUCK_BE_S1(m->jcode + bcindex + 1));
549                         break;
550
551                 case JAVA_SIPUSH:
552                         OP_LOADCONST_I(SUCK_BE_S2(m->jcode + bcindex + 1));
553                         break;
554
555                 case JAVA_LDC1:
556                         i = SUCK_BE_U1(m->jcode + bcindex + 1);
557                         goto pushconstantitem;
558
559                 case JAVA_LDC2:
560                 case JAVA_LDC2W:
561                         i = SUCK_BE_U2(m->jcode + bcindex + 1);
562
563                 pushconstantitem:
564
565 #if defined(ENABLE_VERIFIER)
566                         if (i >= m->class->cpcount) {
567                                 exceptions_throw_verifyerror(m,
568                                         "Attempt to access constant outside range");
569                                 return false;
570                         }
571 #endif
572
573                         switch (m->class->cptags[i]) {
574                         case CONSTANT_Integer:
575                                 OP_LOADCONST_I(((constant_integer *) (m->class->cpinfos[i]))->value);
576                                 break;
577                         case CONSTANT_Long:
578                                 OP_LOADCONST_L(((constant_long *) (m->class->cpinfos[i]))->value);
579                                 break;
580                         case CONSTANT_Float:
581                                 OP_LOADCONST_F(((constant_float *) (m->class->cpinfos[i]))->value);
582                                 break;
583                         case CONSTANT_Double:
584                                 OP_LOADCONST_D(((constant_double *) (m->class->cpinfos[i]))->value);
585                                 break;
586                         case CONSTANT_String:
587                                 OP_LOADCONST_STRING(literalstring_new((utf *) (m->class->cpinfos[i])));
588                                 break;
589                         case CONSTANT_Class:
590                                 cr = (constant_classref *) (m->class->cpinfos[i]);
591
592                                 if (!resolve_classref(m, cr, resolveLazy, true, true, &c))
593                                         return false;
594
595                                 /* if not resolved, c == NULL */
596
597                                 OP_LOADCONST_CLASSINFO_OR_CLASSREF_CHECK(c, cr);
598
599                                 break;
600
601 #if defined(ENABLE_VERIFIER)
602                         default:
603                                 exceptions_throw_verifyerror(m,
604                                                 "Invalid constant type to push");
605                                 return false;
606 #endif
607                         }
608                         break;
609
610                 case JAVA_ACONST_NULL:
611                         OP_LOADCONST_NULL();
612                         break;
613
614                 case JAVA_ICONST_M1:
615                 case JAVA_ICONST_0:
616                 case JAVA_ICONST_1:
617                 case JAVA_ICONST_2:
618                 case JAVA_ICONST_3:
619                 case JAVA_ICONST_4:
620                 case JAVA_ICONST_5:
621                         OP_LOADCONST_I(opcode - JAVA_ICONST_0);
622                         break;
623
624                 case JAVA_LCONST_0:
625                 case JAVA_LCONST_1:
626                         OP_LOADCONST_L(opcode - JAVA_LCONST_0);
627                         break;
628
629                 case JAVA_FCONST_0:
630                 case JAVA_FCONST_1:
631                 case JAVA_FCONST_2:
632                         OP_LOADCONST_F(opcode - JAVA_FCONST_0);
633                         break;
634
635                 case JAVA_DCONST_0:
636                 case JAVA_DCONST_1:
637                         OP_LOADCONST_D(opcode - JAVA_DCONST_0);
638                         break;
639
640                 /* stack operations ***************************************************/
641
642                 /* We need space for additional ICMDs so we can translate these       */
643                 /* instructions to sequences of ICMD_COPY and ICMD_MOVE instructions. */
644
645                 case JAVA_DUP_X1:
646                         INSTRUCTIONS_CHECK(4);
647                         OP(opcode);
648                         OP(ICMD_NOP);
649                         OP(ICMD_NOP);
650                         OP(ICMD_NOP);
651                         break;
652
653                 case JAVA_DUP_X2:
654                         INSTRUCTIONS_CHECK(6);
655                         OP(opcode);
656                         OP(ICMD_NOP);
657                         OP(ICMD_NOP);
658                         OP(ICMD_NOP);
659                         OP(ICMD_NOP);
660                         OP(ICMD_NOP);
661                         break;
662
663                 case JAVA_DUP2:
664                         INSTRUCTIONS_CHECK(2);
665                         OP(opcode);
666                         OP(ICMD_NOP);
667                         break;
668
669                 case JAVA_DUP2_X1:
670                         INSTRUCTIONS_CHECK(7);
671                         OP(opcode);
672                         OP(ICMD_NOP);
673                         OP(ICMD_NOP);
674                         OP(ICMD_NOP);
675                         OP(ICMD_NOP);
676                         OP(ICMD_NOP);
677                         OP(ICMD_NOP);
678                         break;
679
680                 case JAVA_DUP2_X2:
681                         INSTRUCTIONS_CHECK(9);
682                         OP(opcode);
683                         OP(ICMD_NOP);
684                         OP(ICMD_NOP);
685                         OP(ICMD_NOP);
686                         OP(ICMD_NOP);
687                         OP(ICMD_NOP);
688                         OP(ICMD_NOP);
689                         OP(ICMD_NOP);
690                         OP(ICMD_NOP);
691                         break;
692
693                 case JAVA_SWAP:
694                         INSTRUCTIONS_CHECK(3);
695                         OP(opcode);
696                         OP(ICMD_NOP);
697                         OP(ICMD_NOP);
698                         break;
699
700                 /* local variable access instructions *********************************/
701
702                 case JAVA_ILOAD:
703                 case JAVA_FLOAD:
704                 case JAVA_ALOAD:
705                         if (iswide == false) {
706                                 i = SUCK_BE_U1(m->jcode + bcindex + 1);
707                         }
708                         else {
709                                 i = SUCK_BE_U2(m->jcode + bcindex + 1);
710                                 nextbc = bcindex + 3;
711                                 iswide = false;
712                         }
713                         OP_LOAD_ONEWORD(opcode, i, opcode - JAVA_ILOAD);
714                         break;
715
716                 case JAVA_LLOAD:
717                 case JAVA_DLOAD:
718                         if (iswide == false) {
719                                 i = SUCK_BE_U1(m->jcode + bcindex + 1);
720                         }
721                         else {
722                                 i = SUCK_BE_U2(m->jcode + bcindex + 1);
723                                 nextbc = bcindex + 3;
724                                 iswide = false;
725                         }
726                         OP_LOAD_TWOWORD(opcode, i, opcode - JAVA_ILOAD);
727                         break;
728
729                 case JAVA_ILOAD_0:
730                 case JAVA_ILOAD_1:
731                 case JAVA_ILOAD_2:
732                 case JAVA_ILOAD_3:
733                         OP_LOAD_ONEWORD(ICMD_ILOAD, opcode - JAVA_ILOAD_0, TYPE_INT);
734                         break;
735
736                 case JAVA_LLOAD_0:
737                 case JAVA_LLOAD_1:
738                 case JAVA_LLOAD_2:
739                 case JAVA_LLOAD_3:
740                         OP_LOAD_TWOWORD(ICMD_LLOAD, opcode - JAVA_LLOAD_0, TYPE_LNG);
741                         break;
742
743                 case JAVA_FLOAD_0:
744                 case JAVA_FLOAD_1:
745                 case JAVA_FLOAD_2:
746                 case JAVA_FLOAD_3:
747                         OP_LOAD_ONEWORD(ICMD_FLOAD, opcode - JAVA_FLOAD_0, TYPE_FLT);
748                         break;
749
750                 case JAVA_DLOAD_0:
751                 case JAVA_DLOAD_1:
752                 case JAVA_DLOAD_2:
753                 case JAVA_DLOAD_3:
754                         OP_LOAD_TWOWORD(ICMD_DLOAD, opcode - JAVA_DLOAD_0, TYPE_DBL);
755                         break;
756
757                 case JAVA_ALOAD_0:
758                 case JAVA_ALOAD_1:
759                 case JAVA_ALOAD_2:
760                 case JAVA_ALOAD_3:
761                         OP_LOAD_ONEWORD(ICMD_ALOAD, opcode - JAVA_ALOAD_0, TYPE_ADR);
762                         break;
763
764                 case JAVA_ISTORE:
765                 case JAVA_FSTORE:
766                 case JAVA_ASTORE:
767                         if (iswide == false) {
768                                 i = SUCK_BE_U1(m->jcode + bcindex + 1);
769                         }
770                         else {
771                                 i = SUCK_BE_U2(m->jcode + bcindex + 1);
772                                 nextbc = bcindex + 3;
773                                 iswide = false;
774                         }
775                         OP_STORE_ONEWORD(opcode, i, opcode - JAVA_ISTORE);
776                         break;
777
778                 case JAVA_LSTORE:
779                 case JAVA_DSTORE:
780                         if (iswide == false) {
781                                 i = SUCK_BE_U1(m->jcode + bcindex + 1);
782                         }
783                         else {
784                                 i = SUCK_BE_U2(m->jcode + bcindex + 1);
785                                 nextbc = bcindex + 3;
786                                 iswide = false;
787                         }
788                         OP_STORE_TWOWORD(opcode, i, opcode - JAVA_ISTORE);
789                         break;
790
791                 case JAVA_ISTORE_0:
792                 case JAVA_ISTORE_1:
793                 case JAVA_ISTORE_2:
794                 case JAVA_ISTORE_3:
795                         OP_STORE_ONEWORD(ICMD_ISTORE, opcode - JAVA_ISTORE_0, TYPE_INT);
796                         break;
797
798                 case JAVA_LSTORE_0:
799                 case JAVA_LSTORE_1:
800                 case JAVA_LSTORE_2:
801                 case JAVA_LSTORE_3:
802                         OP_STORE_TWOWORD(ICMD_LSTORE, opcode - JAVA_LSTORE_0, TYPE_LNG);
803                         break;
804
805                 case JAVA_FSTORE_0:
806                 case JAVA_FSTORE_1:
807                 case JAVA_FSTORE_2:
808                 case JAVA_FSTORE_3:
809                         OP_STORE_ONEWORD(ICMD_FSTORE, opcode - JAVA_FSTORE_0, TYPE_FLT);
810                         break;
811
812                 case JAVA_DSTORE_0:
813                 case JAVA_DSTORE_1:
814                 case JAVA_DSTORE_2:
815                 case JAVA_DSTORE_3:
816                         OP_STORE_TWOWORD(ICMD_DSTORE, opcode - JAVA_DSTORE_0, TYPE_DBL);
817                         break;
818
819                 case JAVA_ASTORE_0:
820                 case JAVA_ASTORE_1:
821                 case JAVA_ASTORE_2:
822                 case JAVA_ASTORE_3:
823                         OP_STORE_ONEWORD(ICMD_ASTORE, opcode - JAVA_ASTORE_0, TYPE_ADR);
824                         break;
825
826                 case JAVA_IINC:
827                         {
828                                 int v;
829
830                                 if (iswide == false) {
831                                         i = SUCK_BE_U1(m->jcode + bcindex + 1);
832                                         v = SUCK_BE_S1(m->jcode + bcindex + 2);
833
834                                 }
835                                 else {
836                                         i = SUCK_BE_U2(m->jcode + bcindex + 1);
837                                         v = SUCK_BE_S2(m->jcode + bcindex + 3);
838                                         nextbc = bcindex + 5;
839                                         iswide = false;
840                                 }
841                                 INDEX_ONEWORD(i);
842                                 LOCALTYPE_USED(i, TYPE_INT);
843                                 OP_LOCALINDEX_I(opcode, i, v);
844                         }
845                         break;
846
847                 /* wider index for loading, storing and incrementing ******************/
848
849                 case JAVA_WIDE:
850                         bcindex++;
851                         iswide = true;
852                         goto fetch_opcode;
853
854                 /* managing arrays ****************************************************/
855
856                 case JAVA_NEWARRAY:
857                         switch (SUCK_BE_S1(m->jcode + bcindex + 1)) {
858                         case 4:
859                                 bte = builtintable_get_internal(BUILTIN_newarray_boolean);
860                                 break;
861                         case 5:
862                                 bte = builtintable_get_internal(BUILTIN_newarray_char);
863                                 break;
864                         case 6:
865                                 bte = builtintable_get_internal(BUILTIN_newarray_float);
866                                 break;
867                         case 7:
868                                 bte = builtintable_get_internal(BUILTIN_newarray_double);
869                                 break;
870                         case 8:
871                                 bte = builtintable_get_internal(BUILTIN_newarray_byte);
872                                 break;
873                         case 9:
874                                 bte = builtintable_get_internal(BUILTIN_newarray_short);
875                                 break;
876                         case 10:
877                                 bte = builtintable_get_internal(BUILTIN_newarray_int);
878                                 break;
879                         case 11:
880                                 bte = builtintable_get_internal(BUILTIN_newarray_long);
881                                 break;
882 #if defined(ENABLE_VERIFIER)
883                         default:
884                                 exceptions_throw_verifyerror(m, "Invalid array-type to create");
885                                 return false;
886 #endif
887                         }
888                         OP_BUILTIN_CHECK_EXCEPTION(bte);
889                         break;
890
891                 case JAVA_ANEWARRAY:
892                         i = SUCK_BE_U2(m->jcode + bcindex + 1);
893                         compr = (constant_classref *) class_getconstant(m->class, i, CONSTANT_Class);
894                         if (compr == NULL)
895                                 return false;
896
897                         if (!(cr = class_get_classref_multiarray_of(1, compr)))
898                                 return false;
899
900                         if (!resolve_classref(m, cr, resolveLazy, true, true, &c))
901                                 return false;
902
903                         INSTRUCTIONS_CHECK(2);
904                         OP_LOADCONST_CLASSINFO_OR_CLASSREF_NOCHECK(c, cr);
905                         bte = builtintable_get_internal(BUILTIN_newarray);
906                         OP_BUILTIN_CHECK_EXCEPTION(bte);
907                         s_count++;
908                         break;
909
910                 case JAVA_MULTIANEWARRAY:
911                         jd->isleafmethod = false;
912                         i = SUCK_BE_U2(m->jcode + bcindex + 1);
913                         j = SUCK_BE_U1(m->jcode + bcindex + 3);
914   
915                         cr = (constant_classref *) class_getconstant(m->class, i, CONSTANT_Class);
916                         if (cr == NULL)
917                                 return false;
918   
919                         if (!resolve_classref(m, cr, resolveLazy, true, true, &c))
920                                 return false;
921   
922                         /* if unresolved, c == NULL */
923   
924                         iptr->s1.argcount = j;
925                         OP_S3_CLASSINFO_OR_CLASSREF(opcode, c, cr, INS_FLAG_CHECK);
926                         break;
927
928                 /* control flow instructions ******************************************/
929
930                 case JAVA_IFEQ:
931                 case JAVA_IFLT:
932                 case JAVA_IFLE:
933                 case JAVA_IFNE:
934                 case JAVA_IFGT:
935                 case JAVA_IFGE:
936                 case JAVA_IFNULL:
937                 case JAVA_IFNONNULL:
938                 case JAVA_IF_ICMPEQ:
939                 case JAVA_IF_ICMPNE:
940                 case JAVA_IF_ICMPLT:
941                 case JAVA_IF_ICMPGT:
942                 case JAVA_IF_ICMPLE:
943                 case JAVA_IF_ICMPGE:
944                 case JAVA_IF_ACMPEQ:
945                 case JAVA_IF_ACMPNE:
946                 case JAVA_GOTO:
947                         i = bcindex + SUCK_BE_S2(m->jcode + bcindex + 1);
948                         CHECK_BYTECODE_INDEX(i);
949                         MARK_BASICBLOCK(&pd, i);
950                         blockend = true;
951                         OP_INSINDEX(opcode, i);
952                         break;
953
954                 case JAVA_GOTO_W:
955                         i = bcindex + SUCK_BE_S4(m->jcode + bcindex + 1);
956                         CHECK_BYTECODE_INDEX(i);
957                         MARK_BASICBLOCK(&pd, i);
958                         blockend = true;
959                         OP_INSINDEX(ICMD_GOTO, i);
960                         break;
961
962                 case JAVA_JSR:
963                         i = bcindex + SUCK_BE_S2(m->jcode + bcindex + 1);
964 jsr_tail:
965                         CHECK_BYTECODE_INDEX(i);
966                         MARK_BASICBLOCK(&pd, i);
967                         blockend = true;
968                         OP_PREPARE_ZEROFLAGS(JAVA_JSR);
969                         iptr->sx.s23.s3.jsrtarget.insindex = i;
970                         PINC;
971                         break;
972
973                 case JAVA_JSR_W:
974                         i = bcindex + SUCK_BE_S4(m->jcode + bcindex + 1);
975                         goto jsr_tail;
976
977                 case JAVA_RET:
978                         if (iswide == false) {
979                                 i = SUCK_BE_U1(m->jcode + bcindex + 1);
980                         }
981                         else {
982                                 i = SUCK_BE_U2(m->jcode + bcindex + 1);
983                                 nextbc = bcindex + 3;
984                                 iswide = false;
985                         }
986                         blockend = true;
987
988                         OP_LOAD_ONEWORD(opcode, i, TYPE_ADR);
989                         break;
990
991                 case JAVA_IRETURN:
992                 case JAVA_LRETURN:
993                 case JAVA_FRETURN:
994                 case JAVA_DRETURN:
995                 case JAVA_ARETURN:
996                 case JAVA_RETURN:
997                         blockend = true;
998                         /* XXX ARETURN will need a flag in the typechecker */
999                         OP(opcode);
1000                         break;
1001
1002                 case JAVA_ATHROW:
1003                         blockend = true;
1004                         /* XXX ATHROW will need a flag in the typechecker */
1005                         OP(opcode);
1006                         break;
1007
1008
1009                 /* table jumps ********************************************************/
1010
1011                 case JAVA_LOOKUPSWITCH:
1012                         {
1013                                 s4 num, j;
1014                                 lookup_target_t *lookup;
1015 #if defined(ENABLE_VERIFIER)
1016                                 s4 prevvalue = 0;
1017 #endif
1018                                 blockend = true;
1019                                 nextbc = MEMORY_ALIGN((bcindex + 1), 4);
1020
1021                                 CHECK_END_OF_BYTECODE(nextbc + 8);
1022
1023                                 OP_PREPARE_ZEROFLAGS(opcode);
1024
1025                                 /* default target */
1026
1027                                 j = bcindex + SUCK_BE_S4(m->jcode + nextbc);
1028                                 iptr->sx.s23.s3.lookupdefault.insindex = j;
1029                                 nextbc += 4;
1030                                 CHECK_BYTECODE_INDEX(j);
1031                                 MARK_BASICBLOCK(&pd, j);
1032
1033                                 /* number of pairs */
1034
1035                                 num = SUCK_BE_U4(m->jcode + nextbc);
1036                                 iptr->sx.s23.s2.lookupcount = num;
1037                                 nextbc += 4;
1038
1039                                 /* allocate the intermediate code table */
1040
1041                                 lookup = DMNEW(lookup_target_t, num);
1042                                 iptr->dst.lookup = lookup;
1043
1044                                 /* iterate over the lookup table */
1045
1046                                 CHECK_END_OF_BYTECODE(nextbc + 8 * num);
1047
1048                                 for (i = 0; i < num; i++) {
1049                                         /* value */
1050
1051                                         j = SUCK_BE_S4(m->jcode + nextbc);
1052                                         lookup->value = j;
1053
1054                                         nextbc += 4;
1055
1056 #if defined(ENABLE_VERIFIER)
1057                                         /* check if the lookup table is sorted correctly */
1058
1059                                         if (i && (j <= prevvalue)) {
1060                                                 exceptions_throw_verifyerror(m, "Unsorted lookup switch");
1061                                                 return false;
1062                                         }
1063                                         prevvalue = j;
1064 #endif
1065                                         /* target */
1066
1067                                         j = bcindex + SUCK_BE_S4(m->jcode + nextbc);
1068                                         lookup->target.insindex = j;
1069                                         lookup++;
1070                                         nextbc += 4;
1071                                         CHECK_BYTECODE_INDEX(j);
1072                                         MARK_BASICBLOCK(&pd, j);
1073                                 }
1074
1075                                 PINC;
1076                                 break;
1077                         }
1078
1079
1080                 case JAVA_TABLESWITCH:
1081                         {
1082                                 s4 num, j;
1083                                 s4 deftarget;
1084                                 branch_target_t *table;
1085
1086                                 blockend = true;
1087                                 nextbc = MEMORY_ALIGN((bcindex + 1), 4);
1088
1089                                 CHECK_END_OF_BYTECODE(nextbc + 12);
1090
1091                                 OP_PREPARE_ZEROFLAGS(opcode);
1092
1093                                 /* default target */
1094
1095                                 deftarget = bcindex + SUCK_BE_S4(m->jcode + nextbc);
1096                                 nextbc += 4;
1097                                 CHECK_BYTECODE_INDEX(deftarget);
1098                                 MARK_BASICBLOCK(&pd, deftarget);
1099
1100                                 /* lower bound */
1101
1102                                 j = SUCK_BE_S4(m->jcode + nextbc);
1103                                 iptr->sx.s23.s2.tablelow = j;
1104                                 nextbc += 4;
1105
1106                                 /* upper bound */
1107
1108                                 num = SUCK_BE_S4(m->jcode + nextbc);
1109                                 iptr->sx.s23.s3.tablehigh = num;
1110                                 nextbc += 4;
1111
1112                                 /* calculate the number of table entries */
1113
1114                                 num = num - j + 1;
1115
1116 #if defined(ENABLE_VERIFIER)
1117                                 if (num < 1) {
1118                                         exceptions_throw_verifyerror(m,
1119                                                         "invalid TABLESWITCH: upper bound < lower bound");
1120                                         return false;
1121                                 }
1122 #endif
1123                                 /* create the intermediate code table */
1124                                 /* the first entry is the default target */
1125
1126                                 table = DMNEW(branch_target_t, 1 + num);
1127                                 iptr->dst.table = table;
1128                                 (table++)->insindex = deftarget;
1129
1130                                 /* iterate over the target table */
1131
1132                                 CHECK_END_OF_BYTECODE(nextbc + 4 * num);
1133
1134                                 for (i = 0; i < num; i++) {
1135                                         j = bcindex + SUCK_BE_S4(m->jcode + nextbc);
1136                                         (table++)->insindex = j;
1137                                         nextbc += 4;
1138                                         CHECK_BYTECODE_INDEX(j);
1139                                         MARK_BASICBLOCK(&pd, j);
1140                                 }
1141
1142                                 PINC;
1143                                 break;
1144                         }
1145
1146
1147                 /* load and store of object fields ************************************/
1148
1149                 case JAVA_AASTORE:
1150                         OP(opcode);
1151                         jd->isleafmethod = false;
1152                         break;
1153
1154                 case JAVA_GETSTATIC:
1155                 case JAVA_PUTSTATIC:
1156                 case JAVA_GETFIELD:
1157                 case JAVA_PUTFIELD:
1158                         i = SUCK_BE_U2(m->jcode + bcindex + 1);
1159                         fmi = class_getconstant(m->class, i, CONSTANT_Fieldref);
1160
1161                         if (fmi == NULL)
1162                                 return false;
1163
1164                         OP_PREPARE_ZEROFLAGS(opcode);
1165                         iptr->sx.s23.s3.fmiref = fmi;
1166
1167                         /* only with -noverify, otherwise the typechecker does this */
1168
1169 #if defined(ENABLE_VERIFIER)
1170                         if (!JITDATA_HAS_FLAG_VERIFY(jd)) {
1171 #endif
1172                                 result = resolve_field_lazy(m, fmi);
1173
1174                                 if (result == resolveFailed)
1175                                         return false;
1176
1177                                 if (result != resolveSucceeded) {
1178                                         uf = resolve_create_unresolved_field(m->class, m, iptr);
1179
1180                                         if (uf == NULL)
1181                                                 return false;
1182
1183                                         /* store the unresolved_field pointer */
1184
1185                                         iptr->sx.s23.s3.uf = uf;
1186                                         iptr->flags.bits |= INS_FLAG_UNRESOLVED;
1187                                 }
1188 #if defined(ENABLE_VERIFIER)
1189                         }
1190 #endif
1191                         PINC;
1192                         break;
1193
1194
1195                 /* method invocation **************************************************/
1196
1197                 case JAVA_INVOKESTATIC:
1198                         OP_PREPARE_ZEROFLAGS(opcode);
1199
1200                         i = SUCK_BE_U2(m->jcode + bcindex + 1);
1201                         fmi = class_getconstant(m->class, i, CONSTANT_Methodref);
1202
1203                         if (fmi == NULL)
1204                                 return false;
1205
1206                         md = fmi->parseddesc.md;
1207
1208                         if (md->params == NULL)
1209                                 if (!descriptor_params_from_paramtypes(md, ACC_STATIC))
1210                                         return false;
1211
1212                         goto invoke_method;
1213
1214                 case JAVA_INVOKESPECIAL:
1215                         OP_PREPARE_FLAGS(opcode, INS_FLAG_CHECK);
1216
1217                         i = SUCK_BE_U2(m->jcode + bcindex + 1);
1218                         fmi = class_getconstant(m->class, i, CONSTANT_Methodref);
1219
1220                         goto invoke_nonstatic_method;
1221
1222                 case JAVA_INVOKEINTERFACE:
1223                         OP_PREPARE_ZEROFLAGS(opcode);
1224
1225                         i = SUCK_BE_U2(m->jcode + bcindex + 1);
1226                         fmi = class_getconstant(m->class, i, CONSTANT_InterfaceMethodref);
1227
1228                         goto invoke_nonstatic_method;
1229
1230                 case JAVA_INVOKEVIRTUAL:
1231                         OP_PREPARE_ZEROFLAGS(opcode);
1232
1233                         i = SUCK_BE_U2(m->jcode + bcindex + 1);
1234                         fmi = class_getconstant(m->class, i, CONSTANT_Methodref);
1235
1236 invoke_nonstatic_method:
1237                         if (fmi == NULL)
1238                                 return false;
1239
1240                         md = fmi->parseddesc.md;
1241
1242                         if (md->params == NULL)
1243                                 if (!descriptor_params_from_paramtypes(md, 0))
1244                                         return false;
1245
1246 invoke_method:
1247                         jd->isleafmethod = false;
1248
1249                         iptr->sx.s23.s3.fmiref = fmi;
1250
1251                         /* only with -noverify, otherwise the typechecker does this */
1252
1253 #if defined(ENABLE_VERIFIER)
1254                         if (!JITDATA_HAS_FLAG_VERIFY(jd)) {
1255 #endif
1256                                 result = resolve_method_lazy(m, fmi, 
1257                                                                                          (opcode == JAVA_INVOKESPECIAL));
1258
1259                                 if (result == resolveFailed)
1260                                         return false;
1261
1262                                 if (result == resolveSucceeded) {
1263                                         methodinfo *mi = iptr->sx.s23.s3.fmiref->p.method;
1264
1265                                         /* if this call is monomorphic, turn it into an
1266                                            INVOKESPECIAL */
1267
1268                                         assert(IS_FMIREF_RESOLVED(iptr->sx.s23.s3.fmiref));
1269
1270                                         if ((iptr->opc == ICMD_INVOKEVIRTUAL)
1271                                                 && (mi->flags & (ACC_FINAL | ACC_PRIVATE)))
1272                                         {
1273                                                 iptr->opc         = ICMD_INVOKESPECIAL;
1274                                                 iptr->flags.bits |= INS_FLAG_CHECK;
1275                                         }
1276                                 }
1277                                 else {
1278                                         um = resolve_create_unresolved_method(m->class, m, fmi,
1279                                                         (opcode == JAVA_INVOKESTATIC),
1280                                                         (opcode == JAVA_INVOKESPECIAL));
1281
1282                                         if (um == NULL)
1283                                                 return false;
1284
1285                                         /* store the unresolved_method pointer */
1286
1287                                         iptr->sx.s23.s3.um = um;
1288                                         iptr->flags.bits |= INS_FLAG_UNRESOLVED;
1289                                 }
1290 #if defined(ENABLE_VERIFIER)
1291                         }
1292 #endif
1293                         PINC;
1294                         break;
1295
1296                 /* instructions taking class arguments ********************************/
1297
1298                 case JAVA_NEW:
1299                         i = SUCK_BE_U2(m->jcode + bcindex + 1);
1300                         cr = class_getconstant(m->class, i, CONSTANT_Class);
1301
1302                         if (cr == NULL)
1303                                 return false;
1304
1305                         if (!resolve_classref(m, cr, resolveLazy, true, true, &c))
1306                                 return false;
1307
1308                         INSTRUCTIONS_CHECK(2);
1309                         OP_LOADCONST_CLASSINFO_OR_CLASSREF_NOCHECK(c, cr);
1310                         bte = builtintable_get_internal(BUILTIN_new);
1311                         OP_BUILTIN_CHECK_EXCEPTION(bte);
1312                         s_count++;
1313                         break;
1314
1315                 case JAVA_CHECKCAST:
1316                         i = SUCK_BE_U2(m->jcode + bcindex + 1);
1317                         cr = class_getconstant(m->class, i, CONSTANT_Class);
1318
1319                         if (cr == NULL)
1320                                 return false;
1321
1322                         if (!resolve_classref(m, cr, resolveLazy, true, true, &c))
1323                                 return false;
1324
1325                         if (cr->name->text[0] == '[') {
1326                                 /* array type cast-check */
1327                                 flags = INS_FLAG_CHECK | INS_FLAG_ARRAY;
1328                                 jd->isleafmethod = false;
1329                         }
1330                         else {
1331                                 /* object type cast-check */
1332                                 flags = INS_FLAG_CHECK;
1333                         }
1334                         OP_S3_CLASSINFO_OR_CLASSREF(opcode, c, cr, flags);
1335                         break;
1336
1337                 case JAVA_INSTANCEOF:
1338                         i = SUCK_BE_U2(m->jcode + bcindex + 1);
1339                         cr = class_getconstant(m->class, i, CONSTANT_Class);
1340
1341                         if (cr == NULL)
1342                                 return false;
1343
1344                         if (!resolve_classref(m, cr, resolveLazy, true, true, &c))
1345                                 return false;
1346
1347                         if (cr->name->text[0] == '[') {
1348                                 /* array type cast-check */
1349                                 INSTRUCTIONS_CHECK(2);
1350                                 OP_LOADCONST_CLASSINFO_OR_CLASSREF_NOCHECK(c, cr);
1351                                 bte = builtintable_get_internal(BUILTIN_arrayinstanceof);
1352                                 OP_BUILTIN_NO_EXCEPTION(bte);
1353                                 s_count++;
1354                         }
1355                         else {
1356                                 /* object type cast-check */
1357                                 OP_S3_CLASSINFO_OR_CLASSREF(opcode, c, cr, 0 /* flags*/);
1358                         }
1359                         break;
1360
1361                 /* synchronization instructions ***************************************/
1362
1363                 case JAVA_MONITORENTER:
1364 #if defined(ENABLE_THREADS)
1365                         if (checksync) {
1366                                 bte = builtintable_get_internal(LOCK_monitor_enter);
1367                                 OP_BUILTIN_CHECK_EXCEPTION(bte);
1368                         }
1369                         else
1370 #endif
1371                         {
1372                                 OP_CHECK_EXCEPTION(ICMD_CHECKNULL);
1373                                 OP(ICMD_POP);
1374                         }
1375                         break;
1376
1377                 case JAVA_MONITOREXIT:
1378 #if defined(ENABLE_THREADS)
1379                         if (checksync) {
1380                                 bte = builtintable_get_internal(LOCK_monitor_exit);
1381                                 OP_BUILTIN_CHECK_EXCEPTION(bte);
1382                         }
1383                         else
1384 #endif
1385                         {
1386                                 OP_CHECK_EXCEPTION(ICMD_CHECKNULL);
1387                                 OP(ICMD_POP);
1388                         }
1389                         break;
1390
1391                 /* arithmetic instructions that may become builtin functions **********/
1392
1393                 case JAVA_IDIV:
1394 #if !SUPPORT_DIVISION
1395                         bte = builtintable_get_internal(BUILTIN_idiv);
1396                         OP_BUILTIN_ARITHMETIC(opcode, bte);
1397 #else
1398 # if SUPPORT_HARDWARE_DIVIDE_BY_ZERO
1399                         OP(opcode);
1400 # else
1401                         OP_CHECK_EXCEPTION(opcode);
1402 # endif
1403 #endif
1404                         break;
1405
1406                 case JAVA_IREM:
1407 #if !SUPPORT_DIVISION
1408                         bte = builtintable_get_internal(BUILTIN_irem);
1409                         OP_BUILTIN_ARITHMETIC(opcode, bte);
1410 #else
1411 # if SUPPORT_HARDWARE_DIVIDE_BY_ZERO
1412                         OP(opcode);
1413 # else
1414                         OP_CHECK_EXCEPTION(opcode);
1415 # endif
1416 #endif
1417                         break;
1418
1419                 case JAVA_LDIV:
1420 #if !(SUPPORT_DIVISION && SUPPORT_LONG && SUPPORT_LONG_DIV)
1421                         bte = builtintable_get_internal(BUILTIN_ldiv);
1422                         OP_BUILTIN_ARITHMETIC(opcode, bte);
1423 #else
1424 # if SUPPORT_HARDWARE_DIVIDE_BY_ZERO
1425                         OP(opcode);
1426 # else
1427                         OP_CHECK_EXCEPTION(opcode);
1428 # endif
1429 #endif
1430                         break;
1431
1432                 case JAVA_LREM:
1433 #if !(SUPPORT_DIVISION && SUPPORT_LONG && SUPPORT_LONG_DIV)
1434                         bte = builtintable_get_internal(BUILTIN_lrem);
1435                         OP_BUILTIN_ARITHMETIC(opcode, bte);
1436 #else
1437 # if SUPPORT_HARDWARE_DIVIDE_BY_ZERO
1438                         OP(opcode);
1439 # else
1440                         OP_CHECK_EXCEPTION(opcode);
1441 # endif
1442 #endif
1443                         break;
1444
1445                 case JAVA_FREM:
1446 #if defined(__I386__)
1447                         OP(opcode);
1448 #else
1449                         bte = builtintable_get_internal(BUILTIN_frem);
1450                         OP_BUILTIN_NO_EXCEPTION(bte);
1451 #endif
1452                         break;
1453
1454                 case JAVA_DREM:
1455 #if defined(__I386__)
1456                         OP(opcode);
1457 #else
1458                         bte = builtintable_get_internal(BUILTIN_drem);
1459                         OP_BUILTIN_NO_EXCEPTION(bte);
1460 #endif
1461                         break;
1462
1463                 case JAVA_F2I:
1464 #if defined(__ALPHA__)
1465                         if (!opt_noieee) {
1466                                 bte = builtintable_get_internal(BUILTIN_f2i);
1467                                 OP_BUILTIN_NO_EXCEPTION(bte);
1468                         }
1469                         else
1470 #endif
1471                         {
1472                                 OP(opcode);
1473                         }
1474                         break;
1475
1476                 case JAVA_F2L:
1477 #if defined(__ALPHA__)
1478                         if (!opt_noieee) {
1479                                 bte = builtintable_get_internal(BUILTIN_f2l);
1480                                 OP_BUILTIN_NO_EXCEPTION(bte);
1481                         }
1482                         else
1483 #endif
1484                         {
1485                                 OP(opcode);
1486                         }
1487                         break;
1488
1489                 case JAVA_D2I:
1490 #if defined(__ALPHA__)
1491                         if (!opt_noieee) {
1492                                 bte = builtintable_get_internal(BUILTIN_d2i);
1493                                 OP_BUILTIN_NO_EXCEPTION(bte);
1494                         }
1495                         else
1496 #endif
1497                         {
1498                                 OP(opcode);
1499                         }
1500                         break;
1501
1502                 case JAVA_D2L:
1503 #if defined(__ALPHA__)
1504                         if (!opt_noieee) {
1505                                 bte = builtintable_get_internal(BUILTIN_d2l);
1506                                 OP_BUILTIN_NO_EXCEPTION(bte);
1507                         }
1508                         else
1509 #endif
1510                         {
1511                                 OP(opcode);
1512                         }
1513                         break;
1514
1515                 /* invalid opcodes ****************************************************/
1516
1517                         /* check for invalid opcodes if the verifier is enabled */
1518 #if defined(ENABLE_VERIFIER)
1519                 case JAVA_BREAKPOINT:
1520                         exceptions_throw_verifyerror(m, "Quick instructions shouldn't appear, yet.");
1521                         return false;
1522
1523                 case 186: /* unused opcode */
1524                 case 203:
1525                 case 204:
1526                 case 205:
1527                 case 206:
1528                 case 207:
1529                 case 208:
1530                 case 209:
1531                 case 210:
1532                 case 211:
1533                 case 212:
1534                 case 213:
1535                 case 214:
1536                 case 215:
1537                 case 216:
1538                 case 217:
1539                 case 218:
1540                 case 219:
1541                 case 220:
1542                 case 221:
1543                 case 222:
1544                 case 223:
1545                 case 224:
1546                 case 225:
1547                 case 226:
1548                 case 227:
1549                 case 228:
1550                 case 229:
1551                 case 230:
1552                 case 231:
1553                 case 232:
1554                 case 233:
1555                 case 234:
1556                 case 235:
1557                 case 236:
1558                 case 237:
1559                 case 238:
1560                 case 239:
1561                 case 240:
1562                 case 241:
1563                 case 242:
1564                 case 243:
1565                 case 244:
1566                 case 245:
1567                 case 246:
1568                 case 247:
1569                 case 248:
1570                 case 249:
1571                 case 250:
1572                 case 251:
1573                 case 252:
1574                 case 253:
1575                 case 254:
1576                 case 255:
1577                         exceptions_throw_verifyerror(m, "Illegal opcode %d at instr %d\n",
1578                                                                                  opcode, ircount);
1579                         return false;
1580                         break;
1581 #endif /* defined(ENABLE_VERIFIER) */
1582
1583                 /* opcodes that don't require translation *****************************/
1584
1585                 default:
1586                         /* straight-forward translation to ICMD */
1587                         OP(opcode);
1588                         break;
1589
1590                 } /* end switch */
1591
1592                 /* verifier checks ****************************************************/
1593
1594 #if defined(ENABLE_VERIFIER)
1595                 /* If WIDE was used correctly, iswide should have been reset by now. */
1596                 if (iswide) {
1597                         exceptions_throw_verifyerror(m,
1598                                         "Illegal instruction: WIDE before incompatible opcode");
1599                         return false;
1600                 }
1601 #endif /* defined(ENABLE_VERIFIER) */
1602
1603         } /* end for */
1604
1605         if (JITDATA_HAS_FLAG_REORDER(jd)) {
1606                 /* add a NOP to the last basic block */
1607
1608                 INSTRUCTIONS_CHECK(1);
1609                 OP(ICMD_NOP);
1610         }
1611
1612         /*** END OF LOOP **********************************************************/
1613
1614         /* assert that we did not write more ICMDs than allocated */
1615
1616         assert(ircount <= pd.instructionslength);
1617         assert(ircount == (iptr - pd.instructions));
1618
1619         /*** verifier checks ******************************************************/
1620
1621 #if defined(ENABLE_VERIFIER)
1622         if (bcindex != m->jcodelength) {
1623                 exceptions_throw_verifyerror(m,
1624                                 "Command-sequence crosses code-boundary");
1625                 return false;
1626         }
1627
1628         if (!blockend) {
1629                 exceptions_throw_verifyerror(m, "Falling off the end of the code");
1630                 return false;
1631         }
1632 #endif /* defined(ENABLE_VERIFIER) */
1633
1634         /*** setup the methodinfo, allocate stack and basic blocks ****************/
1635
1636         /* identify basic blocks */
1637
1638         /* check if first instruction is a branch target */
1639
1640         if (pd.basicblockstart[0] == 1) {
1641                 jd->branchtoentry = true;
1642         }
1643         else {
1644                 /* first instruction always starts a basic block */
1645
1646                 iptr = pd.instructions;
1647
1648                 iptr->flags.bits |= INS_FLAG_BASICBLOCK;
1649         }
1650
1651         /* Iterate over all bytecode instructions and set missing
1652            basic-block starts in IR instructions. */
1653
1654         for (bcindex = 0; bcindex < m->jcodelength; bcindex++) {
1655                 /* Does the current bytecode instruction start a basic
1656                    block? */
1657
1658                 if (pd.basicblockstart[bcindex] == 1) {
1659 #if defined(ENABLE_VERIFIER)
1660                         /* Check if this bytecode basic-block start at the
1661                            beginning of a bytecode instruction. */
1662
1663                         if (pd.bytecodestart[bcindex] == 0) {
1664                                 exceptions_throw_verifyerror(m,
1665                                                                                  "Branch into middle of instruction");
1666                                 return false;
1667                         }
1668 #endif
1669
1670                         /* Get the IR instruction mapped to the bytecode
1671                            instruction and set the basic block flag. */
1672
1673                         irindex = pd.bytecodemap[bcindex];
1674                         iptr    = pd.instructions + irindex;
1675
1676                         iptr->flags.bits |= INS_FLAG_BASICBLOCK;
1677                 }
1678         }
1679
1680         /* IR instruction index to basic-block index mapping */
1681
1682         pd.instructionmap = DMNEW(s4, ircount);
1683         MZERO(pd.instructionmap, s4, ircount);
1684
1685         /* Iterate over all IR instructions and count the basic blocks. */
1686
1687         iptr = pd.instructions;
1688
1689         bbcount = 0;
1690
1691         for (i = 0; i < ircount; i++, iptr++) {
1692                 if (INSTRUCTION_STARTS_BASICBLOCK(iptr)) {
1693                         /* store the basic-block number in the IR instruction
1694                            map */
1695
1696                         pd.instructionmap[i] = bbcount;
1697
1698                         /* post-increment the basic-block count */
1699
1700                         bbcount++;
1701                 }
1702         }
1703
1704         /* Allocate basic block array (one more for end ipc). */
1705
1706         jd->basicblocks = DMNEW(basicblock, bbcount + 1);
1707         MZERO(jd->basicblocks, basicblock, bbcount + 1);
1708
1709         /* Now iterate again over all IR instructions and initialize the
1710            basic block structures and, in the same loop, resolve the
1711            branch-target instruction indices to basic blocks. */
1712
1713         iptr = pd.instructions;
1714         bptr = jd->basicblocks;
1715
1716         bbcount = 0;
1717
1718         for (i = 0; i < ircount; i++, iptr++) {
1719                 /* check for basic block */
1720
1721                 if (INSTRUCTION_STARTS_BASICBLOCK(iptr)) {
1722                         /* intialize the basic block */
1723
1724                         BASICBLOCK_INIT(bptr, m);
1725
1726                         bptr->iinstr = iptr;
1727
1728                         if (bbcount > 0) {
1729                                 bptr[-1].icount = bptr->iinstr - bptr[-1].iinstr;
1730                         }
1731
1732                         /* bptr->icount is set when the next block is allocated */
1733
1734                         bptr->nr = bbcount++;
1735                         bptr++;
1736                         bptr[-1].next = bptr;
1737                 }
1738
1739                 /* resolve instruction indices to basic blocks */
1740
1741                 switch (iptr->opc) {
1742                 case JAVA_IFEQ:
1743                 case JAVA_IFLT:
1744                 case JAVA_IFLE:
1745                 case JAVA_IFNE:
1746                 case JAVA_IFGT:
1747                 case JAVA_IFGE:
1748                 case JAVA_IFNULL:
1749                 case JAVA_IFNONNULL:
1750                 case JAVA_IF_ICMPEQ:
1751                 case JAVA_IF_ICMPNE:
1752                 case JAVA_IF_ICMPLT:
1753                 case JAVA_IF_ICMPGT:
1754                 case JAVA_IF_ICMPLE:
1755                 case JAVA_IF_ICMPGE:
1756                 case JAVA_IF_ACMPEQ:
1757                 case JAVA_IF_ACMPNE:
1758                 case JAVA_GOTO:
1759                         BYTECODEINDEX_TO_BASICBLOCK(iptr->dst);
1760                         break;
1761
1762                 case ICMD_JSR:
1763                         BYTECODEINDEX_TO_BASICBLOCK(iptr->sx.s23.s3.jsrtarget);
1764                         break;
1765
1766                 case ICMD_TABLESWITCH:
1767                         table = iptr->dst.table;
1768
1769                         BYTECODEINDEX_TO_BASICBLOCK(*table);
1770                         table++;
1771
1772                         j = iptr->sx.s23.s3.tablehigh - iptr->sx.s23.s2.tablelow + 1;
1773
1774                         while (--j >= 0) {
1775                                 BYTECODEINDEX_TO_BASICBLOCK(*table);
1776                                 table++;
1777                         }
1778                         break;
1779
1780                 case ICMD_LOOKUPSWITCH:
1781                         BYTECODEINDEX_TO_BASICBLOCK(iptr->sx.s23.s3.lookupdefault);
1782
1783                         lookup = iptr->dst.lookup;
1784
1785                         j = iptr->sx.s23.s2.lookupcount;
1786
1787                         while (--j >= 0) {
1788                                 BYTECODEINDEX_TO_BASICBLOCK(lookup->target);
1789                                 lookup++;
1790                         }
1791                         break;
1792                 }
1793         }
1794
1795         /* set instruction count of last real block */
1796
1797         if (bbcount > 0) {
1798                 bptr[-1].icount = (pd.instructions + ircount) - bptr[-1].iinstr;
1799         }
1800
1801         /* allocate additional block at end */
1802
1803         BASICBLOCK_INIT(bptr, m);
1804         bptr->nr = bbcount;
1805
1806         /* set basicblock pointers in exception table */
1807
1808         if (!parse_resolve_exception_table(jd, &pd))
1809                 return false;
1810
1811         /* store the local map */
1812
1813         jd->local_map = local_map;
1814
1815         /* calculate local variable renaming */
1816
1817         {
1818                 s4 nlocals = 0;
1819                 s4 i;
1820                 s4 *mapptr;
1821
1822                 mapptr = local_map;
1823
1824                 /* iterate over local_map[0..m->maxlocals*5-1] and allocate a unique */
1825                 /* variable index for each _used_ (javaindex,type) pair.             */
1826                 /* (local_map[javaindex*5+type] = cacaoindex)                        */
1827                 /* Unused (javaindex,type) pairs are marked with UNUSED.             */
1828
1829                 for (i = 0; i < (m->maxlocals * 5); i++, mapptr++) {
1830                         if (*mapptr)
1831                                 *mapptr = nlocals++;
1832                         else
1833                                 *mapptr = UNUSED;
1834                 }
1835
1836                 jd->localcount = nlocals;
1837
1838                 /* calculate the (maximum) number of variables needed */
1839
1840                 jd->varcount = 
1841                           nlocals                                      /* local variables */
1842                         + bbcount * m->maxstack                                 /* invars */
1843                         + s_count;         /* variables created within blocks (non-invar) */
1844
1845                 /* reserve the first indices for local variables */
1846
1847                 jd->vartop = nlocals;
1848
1849                 /* reserve extra variables needed by stack analyse */
1850
1851                 jd->varcount += STACK_EXTRA_VARS;
1852                 jd->vartop   += STACK_EXTRA_VARS;
1853
1854                 /* The verifier needs space for saving invars in some cases and */
1855                 /* extra variables.                                             */
1856
1857 #if defined(ENABLE_VERIFIER)
1858                 jd->varcount += VERIFIER_EXTRA_LOCALS + VERIFIER_EXTRA_VARS + m->maxstack;
1859                 jd->vartop   += VERIFIER_EXTRA_LOCALS + VERIFIER_EXTRA_VARS + m->maxstack;
1860 #endif
1861                 /* allocate and initialize the variable array */
1862
1863                 jd->var = DMNEW(varinfo, jd->varcount);
1864                 MZERO(jd->var, varinfo, jd->varcount);
1865
1866                 /* set types of all locals in jd->var */
1867
1868                 for (mapptr = local_map, i = 0; i < (m->maxlocals * 5); i++, mapptr++)
1869                         if (*mapptr != UNUSED)
1870                                 VAR(*mapptr)->type = i%5;
1871         }
1872
1873         /* assign local variables to method variables */
1874
1875         jd->instructions     = pd.instructions;
1876         jd->instructioncount = ircount;
1877         jd->basicblockcount  = bbcount;
1878         jd->stackcount       = s_count + bbcount * m->maxstack; /* in-stacks */
1879
1880         /* allocate stack table */
1881
1882         jd->stack = DMNEW(stackelement, jd->stackcount);
1883
1884         /* everything's ok */
1885
1886         return true;
1887
1888         /*** goto labels for throwing verifier exceptions *************************/
1889
1890 #if defined(ENABLE_VERIFIER)
1891
1892 throw_unexpected_end_of_bytecode:
1893         exceptions_throw_verifyerror(m, "Unexpected end of bytecode");
1894         return false;
1895
1896 throw_invalid_bytecode_index:
1897         exceptions_throw_verifyerror(m, "Illegal target of branch instruction");
1898         return false;
1899
1900 throw_illegal_local_variable_number:
1901         exceptions_throw_verifyerror(m, "Illegal local variable number");
1902         return false;
1903
1904 #endif /* ENABLE_VERIFIER */
1905 }
1906
1907
1908 /*
1909  * These are local overrides for various environment variables in Emacs.
1910  * Please do not remove this and leave it at the end of the file, where
1911  * Emacs will automagically detect them.
1912  * ---------------------------------------------------------------------
1913  * Local variables:
1914  * mode: c
1915  * indent-tabs-mode: t
1916  * c-basic-offset: 4
1917  * tab-width: 4
1918  * End:
1919  * vim:noexpandtab:sw=4:ts=4:
1920  */