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