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