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