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