Merge from subtype.
[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-2005, 2006, 2007, 2008
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.h"
34
35 #include "native/native.hpp"
36
37 #include "threads/lock.hpp"
38
39 #include "toolbox/logging.h"
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                 /* We check here for the space of 1 instruction in the
538                    instruction array.  If an opcode is converted to more than
539                    1 instruction, this is checked in the corresponding
540                    case. */
541
542                 INSTRUCTIONS_CHECK(1);
543
544                 /* translate this bytecode instruction */
545                 switch (opcode) {
546
547                 case BC_nop:
548                         break;
549
550                 /* pushing constants onto the stack ***********************************/
551
552                 case BC_bipush:
553                         OP_LOADCONST_I(SUCK_BE_S1(m->jcode + bcindex + 1));
554                         break;
555
556                 case BC_sipush:
557                         OP_LOADCONST_I(SUCK_BE_S2(m->jcode + bcindex + 1));
558                         break;
559
560                 case BC_ldc1:
561                         i = SUCK_BE_U1(m->jcode + bcindex + 1);
562                         goto pushconstantitem;
563
564                 case BC_ldc2:
565                 case BC_ldc2w:
566                         i = SUCK_BE_U2(m->jcode + bcindex + 1);
567
568                 pushconstantitem:
569
570 #if defined(ENABLE_VERIFIER)
571                         if (i >= m->clazz->cpcount) {
572                                 exceptions_throw_verifyerror(m,
573                                         "Attempt to access constant outside range");
574                                 return false;
575                         }
576 #endif
577
578                         switch (m->clazz->cptags[i]) {
579                         case CONSTANT_Integer:
580                                 OP_LOADCONST_I(((constant_integer *) (m->clazz->cpinfos[i]))->value);
581                                 break;
582                         case CONSTANT_Long:
583                                 OP_LOADCONST_L(((constant_long *) (m->clazz->cpinfos[i]))->value);
584                                 break;
585                         case CONSTANT_Float:
586                                 OP_LOADCONST_F(((constant_float *) (m->clazz->cpinfos[i]))->value);
587                                 break;
588                         case CONSTANT_Double:
589                                 OP_LOADCONST_D(((constant_double *) (m->clazz->cpinfos[i]))->value);
590                                 break;
591                         case CONSTANT_String:
592                                 OP_LOADCONST_STRING(literalstring_new((utf *) (m->clazz->cpinfos[i])));
593                                 break;
594                         case CONSTANT_Class:
595                                 cr = (constant_classref *) (m->clazz->cpinfos[i]);
596
597                                 if (!resolve_classref(m, cr, resolveLazy, true, true, &c))
598                                         return false;
599
600                                 /* if not resolved, c == NULL */
601
602                                 OP_LOADCONST_CLASSINFO_OR_CLASSREF_CHECK(c, cr);
603
604                                 break;
605
606 #if defined(ENABLE_VERIFIER)
607                         default:
608                                 exceptions_throw_verifyerror(m,
609                                                 "Invalid constant type to push");
610                                 return false;
611 #endif
612                         }
613                         break;
614
615                 case BC_aconst_null:
616                         OP_LOADCONST_NULL();
617                         break;
618
619                 case BC_iconst_m1:
620                 case BC_iconst_0:
621                 case BC_iconst_1:
622                 case BC_iconst_2:
623                 case BC_iconst_3:
624                 case BC_iconst_4:
625                 case BC_iconst_5:
626                         OP_LOADCONST_I(opcode - BC_iconst_0);
627                         break;
628
629                 case BC_lconst_0:
630                 case BC_lconst_1:
631                         OP_LOADCONST_L(opcode - BC_lconst_0);
632                         break;
633
634                 case BC_fconst_0:
635                 case BC_fconst_1:
636                 case BC_fconst_2:
637                         OP_LOADCONST_F(opcode - BC_fconst_0);
638                         break;
639
640                 case BC_dconst_0:
641                 case BC_dconst_1:
642                         OP_LOADCONST_D(opcode - BC_dconst_0);
643                         break;
644
645                 /* stack operations ***************************************************/
646
647                 /* We need space for additional instruction so we can
648                    translate these instructions to sequences of ICMD_COPY and
649                    ICMD_MOVE instructions. */
650
651                 case BC_dup_x1:
652                         INSTRUCTIONS_CHECK(4);
653                         OP(opcode);
654                         OP(ICMD_NOP);
655                         OP(ICMD_NOP);
656                         OP(ICMD_NOP);
657                         break;
658
659                 case BC_dup_x2:
660                         INSTRUCTIONS_CHECK(6);
661                         OP(opcode);
662                         OP(ICMD_NOP);
663                         OP(ICMD_NOP);
664                         OP(ICMD_NOP);
665                         OP(ICMD_NOP);
666                         OP(ICMD_NOP);
667                         break;
668
669                 case BC_dup2:
670                         INSTRUCTIONS_CHECK(2);
671                         OP(opcode);
672                         OP(ICMD_NOP);
673                         break;
674
675                 case BC_dup2_x1:
676                         INSTRUCTIONS_CHECK(7);
677                         OP(opcode);
678                         OP(ICMD_NOP);
679                         OP(ICMD_NOP);
680                         OP(ICMD_NOP);
681                         OP(ICMD_NOP);
682                         OP(ICMD_NOP);
683                         OP(ICMD_NOP);
684                         break;
685
686                 case BC_dup2_x2:
687                         INSTRUCTIONS_CHECK(9);
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                         OP(ICMD_NOP);
696                         OP(ICMD_NOP);
697                         break;
698
699                 case BC_swap:
700                         INSTRUCTIONS_CHECK(3);
701                         OP(opcode);
702                         OP(ICMD_NOP);
703                         OP(ICMD_NOP);
704                         break;
705
706                 /* local variable access instructions *********************************/
707
708                 case BC_iload:
709                 case BC_fload:
710                 case BC_aload:
711                         if (iswide == false) {
712                                 i = SUCK_BE_U1(m->jcode + bcindex + 1);
713                         }
714                         else {
715                                 i = SUCK_BE_U2(m->jcode + bcindex + 1);
716                                 nextbc = bcindex + 3;
717                                 iswide = false;
718                         }
719                         OP_LOAD_ONEWORD(opcode, i, opcode - BC_iload);
720                         break;
721
722                 case BC_lload:
723                 case BC_dload:
724                         if (iswide == false) {
725                                 i = SUCK_BE_U1(m->jcode + bcindex + 1);
726                         }
727                         else {
728                                 i = SUCK_BE_U2(m->jcode + bcindex + 1);
729                                 nextbc = bcindex + 3;
730                                 iswide = false;
731                         }
732                         OP_LOAD_TWOWORD(opcode, i, opcode - BC_iload);
733                         break;
734
735                 case BC_iload_0:
736                 case BC_iload_1:
737                 case BC_iload_2:
738                 case BC_iload_3:
739                         OP_LOAD_ONEWORD(ICMD_ILOAD, opcode - BC_iload_0, TYPE_INT);
740                         break;
741
742                 case BC_lload_0:
743                 case BC_lload_1:
744                 case BC_lload_2:
745                 case BC_lload_3:
746                         OP_LOAD_TWOWORD(ICMD_LLOAD, opcode - BC_lload_0, TYPE_LNG);
747                         break;
748
749                 case BC_fload_0:
750                 case BC_fload_1:
751                 case BC_fload_2:
752                 case BC_fload_3:
753                         OP_LOAD_ONEWORD(ICMD_FLOAD, opcode - BC_fload_0, TYPE_FLT);
754                         break;
755
756                 case BC_dload_0:
757                 case BC_dload_1:
758                 case BC_dload_2:
759                 case BC_dload_3:
760                         OP_LOAD_TWOWORD(ICMD_DLOAD, opcode - BC_dload_0, TYPE_DBL);
761                         break;
762
763                 case BC_aload_0:
764                 case BC_aload_1:
765                 case BC_aload_2:
766                 case BC_aload_3:
767                         OP_LOAD_ONEWORD(ICMD_ALOAD, opcode - BC_aload_0, TYPE_ADR);
768                         break;
769
770                 case BC_istore:
771                 case BC_fstore:
772                 case BC_astore:
773                         if (iswide == false) {
774                                 i = SUCK_BE_U1(m->jcode + bcindex + 1);
775                         }
776                         else {
777                                 i = SUCK_BE_U2(m->jcode + bcindex + 1);
778                                 nextbc = bcindex + 3;
779                                 iswide = false;
780                         }
781                         OP_STORE_ONEWORD(opcode, i, opcode - BC_istore);
782                         break;
783
784                 case BC_lstore:
785                 case BC_dstore:
786                         if (iswide == false) {
787                                 i = SUCK_BE_U1(m->jcode + bcindex + 1);
788                         }
789                         else {
790                                 i = SUCK_BE_U2(m->jcode + bcindex + 1);
791                                 nextbc = bcindex + 3;
792                                 iswide = false;
793                         }
794                         OP_STORE_TWOWORD(opcode, i, opcode - BC_istore);
795                         break;
796
797                 case BC_istore_0:
798                 case BC_istore_1:
799                 case BC_istore_2:
800                 case BC_istore_3:
801                         OP_STORE_ONEWORD(ICMD_ISTORE, opcode - BC_istore_0, TYPE_INT);
802                         break;
803
804                 case BC_lstore_0:
805                 case BC_lstore_1:
806                 case BC_lstore_2:
807                 case BC_lstore_3:
808                         OP_STORE_TWOWORD(ICMD_LSTORE, opcode - BC_lstore_0, TYPE_LNG);
809                         break;
810
811                 case BC_fstore_0:
812                 case BC_fstore_1:
813                 case BC_fstore_2:
814                 case BC_fstore_3:
815                         OP_STORE_ONEWORD(ICMD_FSTORE, opcode - BC_fstore_0, TYPE_FLT);
816                         break;
817
818                 case BC_dstore_0:
819                 case BC_dstore_1:
820                 case BC_dstore_2:
821                 case BC_dstore_3:
822                         OP_STORE_TWOWORD(ICMD_DSTORE, opcode - BC_dstore_0, TYPE_DBL);
823                         break;
824
825                 case BC_astore_0:
826                 case BC_astore_1:
827                 case BC_astore_2:
828                 case BC_astore_3:
829                         OP_STORE_ONEWORD(ICMD_ASTORE, opcode - BC_astore_0, TYPE_ADR);
830                         break;
831
832                 case BC_iinc:
833                         {
834                                 int v;
835
836                                 if (iswide == false) {
837                                         i = SUCK_BE_U1(m->jcode + bcindex + 1);
838                                         v = SUCK_BE_S1(m->jcode + bcindex + 2);
839                                 }
840                                 else {
841                                         i = SUCK_BE_U2(m->jcode + bcindex + 1);
842                                         v = SUCK_BE_S2(m->jcode + bcindex + 3);
843                                         nextbc = bcindex + 5;
844                                         iswide = false;
845                                 }
846                                 INDEX_ONEWORD(i);
847                                 LOCALTYPE_USED(i, TYPE_INT);
848                                 OP_LOCALINDEX_I(opcode, i, v);
849                         }
850                         break;
851
852                 /* wider index for loading, storing and incrementing ******************/
853
854                 case BC_wide:
855                         bcindex++;
856                         iswide = true;
857                         goto fetch_opcode;
858
859                 /* managing arrays ****************************************************/
860
861                 case BC_newarray:
862                         switch (SUCK_BE_S1(m->jcode + bcindex + 1)) {
863                         case 4:
864                                 bte = builtintable_get_internal(BUILTIN_newarray_boolean);
865                                 break;
866                         case 5:
867                                 bte = builtintable_get_internal(BUILTIN_newarray_char);
868                                 break;
869                         case 6:
870                                 bte = builtintable_get_internal(BUILTIN_newarray_float);
871                                 break;
872                         case 7:
873                                 bte = builtintable_get_internal(BUILTIN_newarray_double);
874                                 break;
875                         case 8:
876                                 bte = builtintable_get_internal(BUILTIN_newarray_byte);
877                                 break;
878                         case 9:
879                                 bte = builtintable_get_internal(BUILTIN_newarray_short);
880                                 break;
881                         case 10:
882                                 bte = builtintable_get_internal(BUILTIN_newarray_int);
883                                 break;
884                         case 11:
885                                 bte = builtintable_get_internal(BUILTIN_newarray_long);
886                                 break;
887 #if defined(ENABLE_VERIFIER)
888                         default:
889                                 exceptions_throw_verifyerror(m, "Invalid array-type to create");
890                                 return false;
891 #endif
892                         }
893                         OP_BUILTIN_CHECK_EXCEPTION(bte);
894                         break;
895
896                 case BC_anewarray:
897                         i = SUCK_BE_U2(m->jcode + bcindex + 1);
898                         compr = (constant_classref *) class_getconstant(m->clazz, i, CONSTANT_Class);
899                         if (compr == NULL)
900                                 return false;
901
902                         if (!(cr = class_get_classref_multiarray_of(1, compr)))
903                                 return false;
904
905                         if (!resolve_classref(m, cr, resolveLazy, true, true, &c))
906                                 return false;
907
908                         INSTRUCTIONS_CHECK(2);
909                         OP_LOADCONST_CLASSINFO_OR_CLASSREF_NOCHECK(c, cr);
910                         bte = builtintable_get_internal(BUILTIN_newarray);
911                         OP_BUILTIN_CHECK_EXCEPTION(bte);
912                         s_count++;
913                         break;
914
915                 case BC_multianewarray:
916                         i = SUCK_BE_U2(m->jcode + bcindex + 1);
917                         j = SUCK_BE_U1(m->jcode + bcindex + 3);
918   
919                         cr = (constant_classref *) class_getconstant(m->clazz, i, CONSTANT_Class);
920                         if (cr == NULL)
921                                 return false;
922   
923                         if (!resolve_classref(m, cr, resolveLazy, true, true, &c))
924                                 return false;
925   
926                         /* if unresolved, c == NULL */
927   
928                         iptr->s1.argcount = j;
929                         OP_S3_CLASSINFO_OR_CLASSREF(opcode, c, cr, INS_FLAG_CHECK);
930                         code_unflag_leafmethod(code);
931                         break;
932
933                 /* control flow instructions ******************************************/
934
935                 case BC_ifeq:
936                 case BC_iflt:
937                 case BC_ifle:
938                 case BC_ifne:
939                 case BC_ifgt:
940                 case BC_ifge:
941                 case BC_ifnull:
942                 case BC_ifnonnull:
943                 case BC_if_icmpeq:
944                 case BC_if_icmpne:
945                 case BC_if_icmplt:
946                 case BC_if_icmpgt:
947                 case BC_if_icmple:
948                 case BC_if_icmpge:
949                 case BC_if_acmpeq:
950                 case BC_if_acmpne:
951                 case BC_goto:
952                         i = bcindex + SUCK_BE_S2(m->jcode + bcindex + 1);
953                         CHECK_BYTECODE_INDEX(i);
954                         MARK_BASICBLOCK(&pd, i);
955                         blockend = true;
956                         OP_INSINDEX(opcode, i);
957                         break;
958
959                 case BC_goto_w:
960                         i = bcindex + SUCK_BE_S4(m->jcode + bcindex + 1);
961                         CHECK_BYTECODE_INDEX(i);
962                         MARK_BASICBLOCK(&pd, i);
963                         blockend = true;
964                         OP_INSINDEX(ICMD_GOTO, i);
965                         break;
966
967                 case BC_jsr:
968                         i = bcindex + SUCK_BE_S2(m->jcode + bcindex + 1);
969 jsr_tail:
970                         CHECK_BYTECODE_INDEX(i);
971                         MARK_BASICBLOCK(&pd, i);
972                         blockend = true;
973                         OP_PREPARE_ZEROFLAGS(BC_jsr);
974                         iptr->sx.s23.s3.jsrtarget.insindex = i;
975                         PINC;
976                         break;
977
978                 case BC_jsr_w:
979                         i = bcindex + SUCK_BE_S4(m->jcode + bcindex + 1);
980                         goto jsr_tail;
981
982                 case BC_ret:
983                         if (iswide == false) {
984                                 i = SUCK_BE_U1(m->jcode + bcindex + 1);
985                         }
986                         else {
987                                 i = SUCK_BE_U2(m->jcode + bcindex + 1);
988                                 nextbc = bcindex + 3;
989                                 iswide = false;
990                         }
991                         blockend = true;
992
993                         OP_LOAD_ONEWORD(opcode, i, TYPE_ADR);
994                         break;
995
996                 case BC_ireturn:
997                 case BC_lreturn:
998                 case BC_freturn:
999                 case BC_dreturn:
1000                 case BC_areturn:
1001                 case BC_return:
1002                         blockend = true;
1003                         /* XXX ARETURN will need a flag in the typechecker */
1004                         OP(opcode);
1005                         break;
1006
1007                 case BC_athrow:
1008                         blockend = true;
1009                         /* XXX ATHROW will need a flag in the typechecker */
1010                         OP(opcode);
1011                         break;
1012
1013
1014                 /* table jumps ********************************************************/
1015
1016                 case BC_lookupswitch:
1017                         {
1018                                 s4 num, j;
1019                                 lookup_target_t *lookup;
1020 #if defined(ENABLE_VERIFIER)
1021                                 s4 prevvalue = 0;
1022 #endif
1023                                 blockend = true;
1024                                 nextbc = MEMORY_ALIGN((bcindex + 1), 4);
1025
1026                                 CHECK_END_OF_BYTECODE(nextbc + 8);
1027
1028                                 OP_PREPARE_ZEROFLAGS(opcode);
1029
1030                                 /* default target */
1031
1032                                 j = bcindex + SUCK_BE_S4(m->jcode + nextbc);
1033                                 iptr->sx.s23.s3.lookupdefault.insindex = j;
1034                                 nextbc += 4;
1035                                 CHECK_BYTECODE_INDEX(j);
1036                                 MARK_BASICBLOCK(&pd, j);
1037
1038                                 /* number of pairs */
1039
1040                                 num = SUCK_BE_U4(m->jcode + nextbc);
1041                                 iptr->sx.s23.s2.lookupcount = num;
1042                                 nextbc += 4;
1043
1044                                 /* allocate the intermediate code table */
1045
1046                                 lookup = (lookup_target_t*) DumpMemory::allocate(sizeof(lookup_target_t) * num);
1047                                 iptr->dst.lookup = lookup;
1048
1049                                 /* iterate over the lookup table */
1050
1051                                 CHECK_END_OF_BYTECODE(nextbc + 8 * num);
1052
1053                                 for (i = 0; i < num; i++) {
1054                                         /* value */
1055
1056                                         j = SUCK_BE_S4(m->jcode + nextbc);
1057                                         lookup->value = j;
1058
1059                                         nextbc += 4;
1060
1061 #if defined(ENABLE_VERIFIER)
1062                                         /* check if the lookup table is sorted correctly */
1063
1064                                         if (i && (j <= prevvalue)) {
1065                                                 exceptions_throw_verifyerror(m, "Unsorted lookup switch");
1066                                                 return false;
1067                                         }
1068                                         prevvalue = j;
1069 #endif
1070                                         /* target */
1071
1072                                         j = bcindex + SUCK_BE_S4(m->jcode + nextbc);
1073                                         lookup->target.insindex = j;
1074                                         lookup++;
1075                                         nextbc += 4;
1076                                         CHECK_BYTECODE_INDEX(j);
1077                                         MARK_BASICBLOCK(&pd, j);
1078                                 }
1079
1080                                 PINC;
1081                                 break;
1082                         }
1083
1084                 case BC_tableswitch:
1085                         {
1086                                 s4 num, j;
1087                                 s4 deftarget;
1088                                 branch_target_t *table;
1089
1090                                 blockend = true;
1091                                 nextbc = MEMORY_ALIGN((bcindex + 1), 4);
1092
1093                                 CHECK_END_OF_BYTECODE(nextbc + 12);
1094
1095                                 OP_PREPARE_ZEROFLAGS(opcode);
1096
1097                                 /* default target */
1098
1099                                 deftarget = bcindex + SUCK_BE_S4(m->jcode + nextbc);
1100                                 nextbc += 4;
1101                                 CHECK_BYTECODE_INDEX(deftarget);
1102                                 MARK_BASICBLOCK(&pd, deftarget);
1103
1104                                 /* lower bound */
1105
1106                                 j = SUCK_BE_S4(m->jcode + nextbc);
1107                                 iptr->sx.s23.s2.tablelow = j;
1108                                 nextbc += 4;
1109
1110                                 /* upper bound */
1111
1112                                 num = SUCK_BE_S4(m->jcode + nextbc);
1113                                 iptr->sx.s23.s3.tablehigh = num;
1114                                 nextbc += 4;
1115
1116                                 /* calculate the number of table entries */
1117
1118                                 num = num - j + 1;
1119
1120 #if defined(ENABLE_VERIFIER)
1121                                 if (num < 1) {
1122                                         exceptions_throw_verifyerror(m,
1123                                                         "invalid TABLESWITCH: upper bound < lower bound");
1124                                         return false;
1125                                 }
1126 #endif
1127                                 /* create the intermediate code table */
1128                                 /* the first entry is the default target */
1129
1130                                 table = (branch_target_t*) DumpMemory::allocate(sizeof(branch_target_t) * (1 + num));
1131                                 iptr->dst.table = table;
1132                                 (table++)->insindex = deftarget;
1133
1134                                 /* iterate over the target table */
1135
1136                                 CHECK_END_OF_BYTECODE(nextbc + 4 * num);
1137
1138                                 for (i = 0; i < num; i++) {
1139                                         j = bcindex + SUCK_BE_S4(m->jcode + nextbc);
1140                                         (table++)->insindex = j;
1141                                         nextbc += 4;
1142                                         CHECK_BYTECODE_INDEX(j);
1143                                         MARK_BASICBLOCK(&pd, j);
1144                                 }
1145
1146                                 PINC;
1147                                 break;
1148                         }
1149
1150
1151                 /* load and store of object fields ************************************/
1152
1153                 case BC_aastore:
1154                         OP(opcode);
1155                         code_unflag_leafmethod(code);
1156                         break;
1157
1158                 case BC_getstatic:
1159                 case BC_putstatic:
1160                 case BC_getfield:
1161                 case BC_putfield:
1162                         i = SUCK_BE_U2(m->jcode + bcindex + 1);
1163                         fmi = (constant_FMIref*) class_getconstant(m->clazz, i, CONSTANT_Fieldref);
1164
1165                         if (fmi == NULL)
1166                                 return false;
1167
1168                         OP_PREPARE_ZEROFLAGS(opcode);
1169                         iptr->sx.s23.s3.fmiref = fmi;
1170
1171                         /* only with -noverify, otherwise the typechecker does this */
1172
1173 #if defined(ENABLE_VERIFIER)
1174                         if (!JITDATA_HAS_FLAG_VERIFY(jd)) {
1175 #endif
1176                                 result = resolve_field_lazy(m, fmi);
1177
1178                                 if (result == resolveFailed)
1179                                         return false;
1180
1181                                 if (result != resolveSucceeded) {
1182                                         uf = resolve_create_unresolved_field(m->clazz, m, iptr);
1183
1184                                         if (uf == NULL)
1185                                                 return false;
1186
1187                                         /* store the unresolved_field pointer */
1188
1189                                         iptr->sx.s23.s3.uf = uf;
1190                                         iptr->flags.bits |= INS_FLAG_UNRESOLVED;
1191                                 }
1192 #if defined(ENABLE_VERIFIER)
1193                         }
1194 #endif
1195                         PINC;
1196                         break;
1197
1198
1199                 /* method invocation **************************************************/
1200
1201                 case BC_invokestatic:
1202                         OP_PREPARE_ZEROFLAGS(opcode);
1203
1204                         i = SUCK_BE_U2(m->jcode + bcindex + 1);
1205                         fmi = (constant_FMIref*) class_getconstant(m->clazz, i, CONSTANT_Methodref);
1206
1207                         if (fmi == NULL)
1208                                 return false;
1209
1210                         md = fmi->parseddesc.md;
1211
1212                         if (md->params == NULL)
1213                                 if (!descriptor_params_from_paramtypes(md, ACC_STATIC))
1214                                         return false;
1215
1216                         goto invoke_method;
1217
1218                 case BC_invokespecial:
1219                         OP_PREPARE_FLAGS(opcode, INS_FLAG_CHECK);
1220
1221                         i = SUCK_BE_U2(m->jcode + bcindex + 1);
1222                         fmi = (constant_FMIref*) class_getconstant(m->clazz, i, CONSTANT_Methodref);
1223
1224                         goto invoke_nonstatic_method;
1225
1226                 case BC_invokeinterface:
1227                         OP_PREPARE_ZEROFLAGS(opcode);
1228
1229                         i = SUCK_BE_U2(m->jcode + bcindex + 1);
1230                         fmi = (constant_FMIref*) class_getconstant(m->clazz, i, CONSTANT_InterfaceMethodref);
1231
1232                         goto invoke_nonstatic_method;
1233
1234                 case BC_invokevirtual:
1235                         OP_PREPARE_ZEROFLAGS(opcode);
1236
1237                         i = SUCK_BE_U2(m->jcode + bcindex + 1);
1238                         fmi = (constant_FMIref*) class_getconstant(m->clazz, i, CONSTANT_Methodref);
1239
1240 invoke_nonstatic_method:
1241                         if (fmi == NULL)
1242                                 return false;
1243
1244                         md = fmi->parseddesc.md;
1245
1246                         if (md->params == NULL)
1247                                 if (!descriptor_params_from_paramtypes(md, 0))
1248                                         return false;
1249
1250 invoke_method:
1251                         code_unflag_leafmethod(code);
1252
1253                         iptr->sx.s23.s3.fmiref = fmi;
1254
1255                         /* only with -noverify, otherwise the typechecker does this */
1256
1257 #if defined(ENABLE_VERIFIER)
1258                         if (!JITDATA_HAS_FLAG_VERIFY(jd)) {
1259 #endif
1260                                 result = resolve_method_lazy(m, fmi, 
1261                                                                                          (opcode == BC_invokespecial));
1262
1263                                 if (result == resolveFailed)
1264                                         return false;
1265
1266                                 if (result == resolveSucceeded) {
1267                                         methodinfo *mi = iptr->sx.s23.s3.fmiref->p.method;
1268
1269                                         /* if this call is monomorphic, turn it into an
1270                                            INVOKESPECIAL */
1271
1272                                         assert(IS_FMIREF_RESOLVED(iptr->sx.s23.s3.fmiref));
1273
1274                                         if ((iptr->opc == ICMD_INVOKEVIRTUAL)
1275                                                 && (mi->flags & (ACC_FINAL | ACC_PRIVATE)))
1276                                         {
1277                                                 iptr->opc         = ICMD_INVOKESPECIAL;
1278                                                 iptr->flags.bits |= INS_FLAG_CHECK;
1279                                         }
1280                                 }
1281                                 else {
1282                                         um = resolve_create_unresolved_method(m->clazz, m, fmi,
1283                                                         (opcode == BC_invokestatic),
1284                                                         (opcode == BC_invokespecial));
1285
1286                                         if (um == NULL)
1287                                                 return false;
1288
1289                                         /* store the unresolved_method pointer */
1290
1291                                         iptr->sx.s23.s3.um = um;
1292                                         iptr->flags.bits |= INS_FLAG_UNRESOLVED;
1293                                 }
1294 #if defined(ENABLE_VERIFIER)
1295                         }
1296 #endif
1297                         PINC;
1298                         break;
1299
1300                 /* instructions taking class arguments ********************************/
1301
1302                 case BC_new:
1303                         i = SUCK_BE_U2(m->jcode + bcindex + 1);
1304                         cr = (constant_classref*) class_getconstant(m->clazz, i, CONSTANT_Class);
1305
1306                         if (cr == NULL)
1307                                 return false;
1308
1309                         if (!resolve_classref(m, cr, resolveLazy, true, true, &c))
1310                                 return false;
1311
1312                         INSTRUCTIONS_CHECK(2);
1313                         OP_LOADCONST_CLASSINFO_OR_CLASSREF_NOCHECK(c, cr);
1314                         bte = builtintable_get_internal(BUILTIN_new);
1315                         OP_BUILTIN_CHECK_EXCEPTION(bte);
1316                         s_count++;
1317                         break;
1318
1319                 case BC_checkcast:
1320                         i = SUCK_BE_U2(m->jcode + bcindex + 1);
1321                         cr = (constant_classref*) class_getconstant(m->clazz, i, CONSTANT_Class);
1322
1323                         if (cr == NULL)
1324                                 return false;
1325
1326                         if (!resolve_classref(m, cr, resolveLazy, true, true, &c))
1327                                 return false;
1328
1329                         if (cr->name->text[0] == '[') {
1330                                 /* array type cast-check */
1331                                 flags = INS_FLAG_CHECK | INS_FLAG_ARRAY;
1332                                 code_unflag_leafmethod(code);
1333                         }
1334                         else {
1335                                 /* object type cast-check */
1336                                 flags = INS_FLAG_CHECK;
1337                         }
1338                         OP_S3_CLASSINFO_OR_CLASSREF(opcode, c, cr, flags);
1339                         break;
1340
1341                 case BC_instanceof:
1342                         i = SUCK_BE_U2(m->jcode + bcindex + 1);
1343                         cr = (constant_classref*) class_getconstant(m->clazz, i, CONSTANT_Class);
1344
1345                         if (cr == NULL)
1346                                 return false;
1347
1348                         if (!resolve_classref(m, cr, resolveLazy, true, true, &c))
1349                                 return false;
1350
1351                         if (cr->name->text[0] == '[') {
1352                                 /* array type cast-check */
1353                                 INSTRUCTIONS_CHECK(2);
1354                                 OP_LOADCONST_CLASSINFO_OR_CLASSREF_NOCHECK(c, cr);
1355                                 bte = builtintable_get_internal(BUILTIN_arrayinstanceof);
1356                                 OP_BUILTIN_NO_EXCEPTION(bte);
1357                                 s_count++;
1358                         }
1359                         else {
1360                                 /* object type cast-check */
1361                                 OP_S3_CLASSINFO_OR_CLASSREF(opcode, c, cr, 0 /* flags*/);
1362                         }
1363                         break;
1364
1365                 /* synchronization instructions ***************************************/
1366
1367                 case BC_monitorenter:
1368 #if defined(ENABLE_THREADS)
1369                         if (checksync) {
1370                                 bte = builtintable_get_internal(LOCK_monitor_enter);
1371                                 OP_BUILTIN_CHECK_EXCEPTION(bte);
1372                         }
1373                         else
1374 #endif
1375                         {
1376                                 OP_CHECK_EXCEPTION(ICMD_CHECKNULL);
1377                                 OP(ICMD_POP);
1378                         }
1379                         break;
1380
1381                 case BC_monitorexit:
1382 #if defined(ENABLE_THREADS)
1383                         if (checksync) {
1384                                 bte = builtintable_get_internal(LOCK_monitor_exit);
1385                                 OP_BUILTIN_CHECK_EXCEPTION(bte);
1386                         }
1387                         else
1388 #endif
1389                         {
1390                                 OP_CHECK_EXCEPTION(ICMD_CHECKNULL);
1391                                 OP(ICMD_POP);
1392                         }
1393                         break;
1394
1395                 /* arithmetic instructions that may become builtin functions **********/
1396
1397                 case BC_idiv:
1398 #if !SUPPORT_DIVISION
1399                         bte = builtintable_get_internal(BUILTIN_idiv);
1400                         OP_BUILTIN_ARITHMETIC(opcode, bte);
1401 #else
1402 # if SUPPORT_HARDWARE_DIVIDE_BY_ZERO
1403                         OP(opcode);
1404 # else
1405                         OP_CHECK_EXCEPTION(opcode);
1406 # endif
1407 #endif
1408                         break;
1409
1410                 case BC_irem:
1411 #if !SUPPORT_DIVISION
1412                         bte = builtintable_get_internal(BUILTIN_irem);
1413                         OP_BUILTIN_ARITHMETIC(opcode, bte);
1414 #else
1415 # if SUPPORT_HARDWARE_DIVIDE_BY_ZERO
1416                         OP(opcode);
1417 # else
1418                         OP_CHECK_EXCEPTION(opcode);
1419 # endif
1420 #endif
1421                         break;
1422
1423                 case BC_ldiv:
1424 #if !(SUPPORT_DIVISION && SUPPORT_LONG && SUPPORT_LONG_DIV)
1425                         bte = builtintable_get_internal(BUILTIN_ldiv);
1426                         OP_BUILTIN_ARITHMETIC(opcode, bte);
1427 #else
1428 # if SUPPORT_HARDWARE_DIVIDE_BY_ZERO
1429                         OP(opcode);
1430 # else
1431                         OP_CHECK_EXCEPTION(opcode);
1432 # endif
1433 #endif
1434                         break;
1435
1436                 case BC_lrem:
1437 #if !(SUPPORT_DIVISION && SUPPORT_LONG && SUPPORT_LONG_DIV)
1438                         bte = builtintable_get_internal(BUILTIN_lrem);
1439                         OP_BUILTIN_ARITHMETIC(opcode, bte);
1440 #else
1441 # if SUPPORT_HARDWARE_DIVIDE_BY_ZERO
1442                         OP(opcode);
1443 # else
1444                         OP_CHECK_EXCEPTION(opcode);
1445 # endif
1446 #endif
1447                         break;
1448
1449                 case BC_frem:
1450 #if defined(__I386__)
1451                         OP(opcode);
1452 #else
1453                         bte = builtintable_get_internal(BUILTIN_frem);
1454                         OP_BUILTIN_NO_EXCEPTION(bte);
1455 #endif
1456                         break;
1457
1458                 case BC_drem:
1459 #if defined(__I386__)
1460                         OP(opcode);
1461 #else
1462                         bte = builtintable_get_internal(BUILTIN_drem);
1463                         OP_BUILTIN_NO_EXCEPTION(bte);
1464 #endif
1465                         break;
1466
1467                 case BC_f2i:
1468 #if defined(__ALPHA__)
1469                         bte = builtintable_get_internal(BUILTIN_f2i);
1470                         OP_BUILTIN_NO_EXCEPTION(bte);
1471 #else
1472                         OP(opcode);
1473 #endif
1474                         break;
1475
1476                 case BC_f2l:
1477 #if defined(__ALPHA__)
1478                         bte = builtintable_get_internal(BUILTIN_f2l);
1479                         OP_BUILTIN_NO_EXCEPTION(bte);
1480 #else
1481                         OP(opcode);
1482 #endif
1483                         break;
1484
1485                 case BC_d2i:
1486 #if defined(__ALPHA__)
1487                         bte = builtintable_get_internal(BUILTIN_d2i);
1488                         OP_BUILTIN_NO_EXCEPTION(bte);
1489 #else
1490                         OP(opcode);
1491 #endif
1492                         break;
1493
1494                 case BC_d2l:
1495 #if defined(__ALPHA__)
1496                         bte = builtintable_get_internal(BUILTIN_d2l);
1497                         OP_BUILTIN_NO_EXCEPTION(bte);
1498 #else
1499                         OP(opcode);
1500 #endif
1501                         break;
1502
1503
1504                 /* invalid opcodes ****************************************************/
1505
1506                         /* check for invalid opcodes if the verifier is enabled */
1507 #if defined(ENABLE_VERIFIER)
1508                 case BC_breakpoint:
1509                         exceptions_throw_verifyerror(m, "Quick instructions shouldn't appear, yet.");
1510                         return false;
1511
1512
1513                 /* Unused opcodes ************************************************** */
1514
1515                 case 186:
1516                 case 203:
1517                 case 204:
1518                 case 205:
1519                 case 206:
1520                 case 207:
1521                 case 208:
1522                 case 209:
1523                 case 210:
1524                 case 211:
1525                 case 212:
1526                 case 213:
1527                 case 214:
1528                 case 215:
1529                 case 216:
1530                 case 217:
1531                 case 218:
1532                 case 219:
1533                 case 220:
1534                 case 221:
1535                 case 222:
1536                 case 223:
1537                 case 224:
1538                 case 225:
1539                 case 226:
1540                 case 227:
1541                 case 228:
1542                 case 229:
1543                 case 230:
1544                 case 231:
1545                 case 232:
1546                 case 233:
1547                 case 234:
1548                 case 235:
1549                 case 236:
1550                 case 237:
1551                 case 238:
1552                 case 239:
1553                 case 240:
1554                 case 241:
1555                 case 242:
1556                 case 243:
1557                 case 244:
1558                 case 245:
1559                 case 246:
1560                 case 247:
1561                 case 248:
1562                 case 249:
1563                 case 250:
1564                 case 251:
1565                 case 252:
1566                 case 253:
1567                 case 254:
1568                 case 255:
1569                         exceptions_throw_verifyerror(m, "Illegal opcode %d at instr %d\n",
1570                                                                                  opcode, ircount);
1571                         return false;
1572                         break;
1573 #endif /* defined(ENABLE_VERIFIER) */
1574
1575                 /* opcodes that don't require translation *****************************/
1576
1577                 default:
1578                         /* Straight-forward translation to HIR. */
1579                         OP(opcode);
1580                         break;
1581
1582                 } /* end switch */
1583
1584                 /* verifier checks ****************************************************/
1585
1586 #if defined(ENABLE_VERIFIER)
1587                 /* If WIDE was used correctly, iswide should have been reset by now. */
1588                 if (iswide) {
1589                         exceptions_throw_verifyerror(m,
1590                                         "Illegal instruction: WIDE before incompatible opcode");
1591                         return false;
1592                 }
1593 #endif /* defined(ENABLE_VERIFIER) */
1594
1595         } /* end for */
1596
1597         if (JITDATA_HAS_FLAG_REORDER(jd)) {
1598                 /* add a NOP to the last basic block */
1599
1600                 INSTRUCTIONS_CHECK(1);
1601                 OP(ICMD_NOP);
1602         }
1603
1604         /*** END OF LOOP **********************************************************/
1605
1606         /* assert that we did not write more ICMDs than allocated */
1607
1608         assert(ircount <= pd.instructionslength);
1609         assert(ircount == (iptr - pd.instructions));
1610
1611         /*** verifier checks ******************************************************/
1612
1613 #if defined(ENABLE_VERIFIER)
1614         if (bcindex != m->jcodelength) {
1615                 exceptions_throw_verifyerror(m,
1616                                 "Command-sequence crosses code-boundary");
1617                 return false;
1618         }
1619
1620         if (!blockend) {
1621                 exceptions_throw_verifyerror(m, "Falling off the end of the code");
1622                 return false;
1623         }
1624 #endif /* defined(ENABLE_VERIFIER) */
1625
1626         /*** setup the methodinfo, allocate stack and basic blocks ****************/
1627
1628         /* identify basic blocks */
1629
1630         /* check if first instruction is a branch target */
1631
1632         if (pd.basicblockstart[0] == 1) {
1633                 jd->branchtoentry = true;
1634         }
1635         else {
1636                 /* first instruction always starts a basic block */
1637
1638                 iptr = pd.instructions;
1639
1640                 iptr->flags.bits |= INS_FLAG_BASICBLOCK;
1641         }
1642
1643         /* Iterate over all bytecode instructions and set missing
1644            basic-block starts in IR instructions. */
1645
1646         for (bcindex = 0; bcindex < m->jcodelength; bcindex++) {
1647                 /* Does the current bytecode instruction start a basic
1648                    block? */
1649
1650                 if (pd.basicblockstart[bcindex] == 1) {
1651 #if defined(ENABLE_VERIFIER)
1652                         /* Check if this bytecode basic-block start at the
1653                            beginning of a bytecode instruction. */
1654
1655                         if (pd.bytecodestart[bcindex] == 0) {
1656                                 exceptions_throw_verifyerror(m,
1657                                                                                  "Branch into middle of instruction");
1658                                 return false;
1659                         }
1660 #endif
1661
1662                         /* Get the IR instruction mapped to the bytecode
1663                            instruction and set the basic block flag. */
1664
1665                         irindex = pd.bytecodemap[bcindex];
1666                         iptr    = pd.instructions + irindex;
1667
1668                         iptr->flags.bits |= INS_FLAG_BASICBLOCK;
1669                 }
1670         }
1671
1672         /* IR instruction index to basic-block index mapping */
1673
1674         pd.instructionmap = (s4*) DumpMemory::allocate(sizeof(s4) * ircount);
1675         MZERO(pd.instructionmap, s4, ircount);
1676
1677         /* Iterate over all IR instructions and count the basic blocks. */
1678
1679         iptr = pd.instructions;
1680
1681         bbcount = 0;
1682
1683         for (i = 0; i < ircount; i++, iptr++) {
1684                 if (INSTRUCTION_STARTS_BASICBLOCK(iptr)) {
1685                         /* store the basic-block number in the IR instruction
1686                            map */
1687
1688                         pd.instructionmap[i] = bbcount;
1689
1690                         /* post-increment the basic-block count */
1691
1692                         bbcount++;
1693                 }
1694         }
1695
1696         /* Allocate basic block array (one more for end ipc). */
1697
1698         jd->basicblocks = (basicblock*) DumpMemory::allocate(sizeof(basicblock) * (bbcount + 1));
1699         MZERO(jd->basicblocks, basicblock, bbcount + 1);
1700
1701         /* Now iterate again over all IR instructions and initialize the
1702            basic block structures and, in the same loop, resolve the
1703            branch-target instruction indices to basic blocks. */
1704
1705         iptr = pd.instructions;
1706         bptr = jd->basicblocks;
1707
1708         bbcount = 0;
1709
1710         for (i = 0; i < ircount; i++, iptr++) {
1711                 /* check for basic block */
1712
1713                 if (INSTRUCTION_STARTS_BASICBLOCK(iptr)) {
1714                         /* intialize the basic block */
1715
1716                         BASICBLOCK_INIT(bptr, m);
1717
1718                         bptr->iinstr = iptr;
1719
1720                         if (bbcount > 0) {
1721                                 bptr[-1].icount = bptr->iinstr - bptr[-1].iinstr;
1722                         }
1723
1724                         /* bptr->icount is set when the next block is allocated */
1725
1726                         bptr->nr = bbcount++;
1727                         bptr++;
1728                         bptr[-1].next = bptr;
1729                 }
1730
1731                 /* resolve instruction indices to basic blocks */
1732
1733                 switch (iptr->opc) {
1734                 case ICMD_IFEQ:
1735                 case ICMD_IFLT:
1736                 case ICMD_IFLE:
1737                 case ICMD_IFNE:
1738                 case ICMD_IFGT:
1739                 case ICMD_IFGE:
1740                 case ICMD_IFNULL:
1741                 case ICMD_IFNONNULL:
1742                 case ICMD_IF_ICMPEQ:
1743                 case ICMD_IF_ICMPNE:
1744                 case ICMD_IF_ICMPLT:
1745                 case ICMD_IF_ICMPGT:
1746                 case ICMD_IF_ICMPLE:
1747                 case ICMD_IF_ICMPGE:
1748                 case ICMD_IF_ACMPEQ:
1749                 case ICMD_IF_ACMPNE:
1750                 case ICMD_GOTO:
1751                         BYTECODEINDEX_TO_BASICBLOCK(iptr->dst);
1752                         break;
1753
1754                 case ICMD_JSR:
1755                         BYTECODEINDEX_TO_BASICBLOCK(iptr->sx.s23.s3.jsrtarget);
1756                         break;
1757
1758                 case ICMD_TABLESWITCH:
1759                         table = iptr->dst.table;
1760
1761                         BYTECODEINDEX_TO_BASICBLOCK(*table);
1762                         table++;
1763
1764                         j = iptr->sx.s23.s3.tablehigh - iptr->sx.s23.s2.tablelow + 1;
1765
1766                         while (--j >= 0) {
1767                                 BYTECODEINDEX_TO_BASICBLOCK(*table);
1768                                 table++;
1769                         }
1770                         break;
1771
1772                 case ICMD_LOOKUPSWITCH:
1773                         BYTECODEINDEX_TO_BASICBLOCK(iptr->sx.s23.s3.lookupdefault);
1774
1775                         lookup = iptr->dst.lookup;
1776
1777                         j = iptr->sx.s23.s2.lookupcount;
1778
1779                         while (--j >= 0) {
1780                                 BYTECODEINDEX_TO_BASICBLOCK(lookup->target);
1781                                 lookup++;
1782                         }
1783                         break;
1784                 }
1785         }
1786
1787         /* set instruction count of last real block */
1788
1789         if (bbcount > 0) {
1790                 bptr[-1].icount = (pd.instructions + ircount) - bptr[-1].iinstr;
1791         }
1792
1793         /* allocate additional block at end */
1794
1795         BASICBLOCK_INIT(bptr, m);
1796         bptr->nr = bbcount;
1797
1798         /* set basicblock pointers in exception table */
1799
1800         if (!parse_resolve_exception_table(jd, &pd))
1801                 return false;
1802
1803         /* store the local map */
1804
1805         jd->local_map = local_map;
1806
1807         /* calculate local variable renaming */
1808
1809         {
1810                 s4 nlocals = 0;
1811                 s4 i;
1812                 s4 t;
1813                 s4 varindex;
1814                 s4 *mapptr;
1815                 s4 *reversemap;
1816
1817                 mapptr = local_map;
1818
1819                 /* iterate over local_map[0..m->maxlocals*5-1] and allocate a unique */
1820                 /* variable index for each _used_ (javaindex,type) pair.             */
1821                 /* (local_map[javaindex*5+type] = cacaoindex)                        */
1822                 /* Unused (javaindex,type) pairs are marked with UNUSED.             */
1823
1824                 for (i = 0; i < (m->maxlocals * 5); i++, mapptr++) {
1825                         if (*mapptr)
1826                                 *mapptr = nlocals++;
1827                         else
1828                                 *mapptr = UNUSED;
1829                 }
1830
1831                 jd->localcount = nlocals;
1832
1833                 /* calculate the (maximum) number of variables needed */
1834
1835                 jd->varcount = 
1836                           nlocals                                      /* local variables */
1837                         + bbcount * m->maxstack                                 /* invars */
1838                         + s_count;         /* variables created within blocks (non-invar) */
1839
1840                 /* reserve the first indices for local variables */
1841
1842                 jd->vartop = nlocals;
1843
1844                 /* reserve extra variables needed by stack analyse */
1845
1846                 jd->varcount += STACK_EXTRA_VARS;
1847                 jd->vartop   += STACK_EXTRA_VARS;
1848
1849                 /* The verifier needs space for saving invars in some cases and */
1850                 /* extra variables.                                             */
1851
1852 #if defined(ENABLE_VERIFIER)
1853                 jd->varcount += VERIFIER_EXTRA_LOCALS + VERIFIER_EXTRA_VARS + m->maxstack;
1854                 jd->vartop   += VERIFIER_EXTRA_LOCALS + VERIFIER_EXTRA_VARS + m->maxstack;
1855 #endif
1856                 /* allocate and initialize the variable array */
1857
1858                 jd->var = (varinfo*) DumpMemory::allocate(sizeof(varinfo) * jd->varcount);
1859                 MZERO(jd->var, varinfo, jd->varcount);
1860
1861                 /* set types of all locals in jd->var */
1862                 /* and fill the reverselocalmap       */
1863
1864                 reversemap = (s4*) DumpMemory::allocate(sizeof(s4) * nlocals);
1865
1866                 for (i = 0; i < m->maxlocals; i++)
1867                         for (t=0; t<5; t++) {
1868                                 varindex = local_map[5*i + t];
1869                                 if (varindex != UNUSED) {
1870                                         VAR(varindex)->type = t;
1871                                         reversemap[varindex] = i;
1872                                 }
1873                         }
1874
1875                 jd->reverselocalmap = reversemap;
1876         }
1877
1878         /* assign local variables to method variables */
1879
1880         jd->instructions     = pd.instructions;
1881         jd->instructioncount = ircount;
1882         jd->basicblockcount  = bbcount;
1883         jd->stackcount       = s_count + bbcount * m->maxstack; /* in-stacks */
1884
1885         /* allocate stack table */
1886
1887         jd->stack = (stackelement_t*) DumpMemory::allocate(sizeof(stackelement_t) * jd->stackcount);
1888
1889         /* everything's ok */
1890
1891         return true;
1892
1893         /*** goto labels for throwing verifier exceptions *************************/
1894
1895 #if defined(ENABLE_VERIFIER)
1896
1897 throw_unexpected_end_of_bytecode:
1898         exceptions_throw_verifyerror(m, "Unexpected end of bytecode");
1899         return false;
1900
1901 throw_invalid_bytecode_index:
1902         exceptions_throw_verifyerror(m, "Illegal target of branch instruction");
1903         return false;
1904
1905 throw_illegal_local_variable_number:
1906         exceptions_throw_verifyerror(m, "Illegal local variable number");
1907         return false;
1908
1909 #endif /* ENABLE_VERIFIER */
1910 }
1911
1912 #if defined(__cplusplus)
1913 }
1914 #endif
1915
1916 /*
1917  * These are local overrides for various environment variables in Emacs.
1918  * Please do not remove this and leave it at the end of the file, where
1919  * Emacs will automagically detect them.
1920  * ---------------------------------------------------------------------
1921  * Local variables:
1922  * mode: c
1923  * indent-tabs-mode: t
1924  * c-basic-offset: 4
1925  * tab-width: 4
1926  * End:
1927  * vim:noexpandtab:sw=4:ts=4:
1928  */