* src/vm/jit/stack.c: Added JSR elimination by block specialization.
[cacao.git] / src / vm / jit / stack.c
1 /* src/vm/jit/stack.c - stack analysis
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    Authors: Andreas Krall
28
29    Changes: Edwin Steiner
30             Christian Thalinger
31             Christian Ullrich
32
33    $Id: stack.c 5472 2006-09-11 23:24:46Z edwin $
34
35 */
36
37
38 #include "config.h"
39
40 #include <assert.h>
41 #include <stdio.h>
42 #include <string.h>
43 #include <limits.h>
44
45 #include "vm/types.h"
46
47 #include "arch.h"
48 #include "md-abi.h"
49
50 #include "mm/memory.h"
51 #include "native/native.h"
52 #include "toolbox/logging.h"
53 #include "vm/global.h"
54 #include "vm/builtin.h"
55 #include "vm/options.h"
56 #include "vm/resolve.h"
57 #include "vm/statistics.h"
58 #include "vm/stringlocal.h"
59 #include "vm/jit/cfg.h"
60 #include "vm/jit/codegen-common.h"
61 #include "vm/jit/abi.h"
62 #include "vm/jit/show.h"
63
64 #if defined(ENABLE_DISASSEMBLER)
65 # include "vm/jit/disass.h"
66 #endif
67
68 #include "vm/jit/jit.h"
69 #include "vm/jit/stack.h"
70
71 #if defined(ENABLE_LSRA)
72 # include "vm/jit/allocator/lsra.h"
73 #endif
74
75 /*#define STACK_VERBOSE*/
76
77
78 /* macro for saving #ifdefs ***************************************************/
79
80 #if defined(ENABLE_INTRP)
81 #define IF_INTRP(x) if (opt_intrp) { x }
82 #define IF_NO_INTRP(x) if (!opt_intrp) { x }
83 #else
84 #define IF_INTRP(x)
85 #define IF_NO_INTRP(x) { x }
86 #endif
87
88 #if defined(ENABLE_INTRP)
89 #if defined(ENABLE_JIT)
90 #define IF_JIT(x) if (!opt_intrp) { x }
91 #else
92 #define IF_JIT(x)
93 #endif
94 #else /* !defined(ENABLE_INTRP) */
95 #define IF_JIT(x) { x }
96 #endif /* defined(ENABLE_INTRP) */
97
98 #if defined(ENABLE_STATISTICS)
99 #define STATISTICS_STACKDEPTH_DISTRIBUTION(distr)                    \
100     do {                                                             \
101         if (opt_stat) {                                              \
102             if (stackdepth >= 10)                                    \
103                 count_store_depth[10]++;                             \
104             else                                                     \
105                 count_store_depth[stackdepth]++;                     \
106         }                                                            \
107     } while (0)
108 #else /* !defined(ENABLE_STATISTICS) */
109 #define STATISTICS_STACKDEPTH_DISTRIBUTION(distr)
110 #endif
111
112 /* stackdata_t *****************************************************************
113
114    This struct holds internal data during stack analysis.
115
116 *******************************************************************************/
117
118 typedef struct stackdata_t stackdata_t;
119
120 struct stackdata_t {
121     basicblock *bptr;             /* the current basic block being analysed   */
122     stackptr new;                 /* next free stackelement                   */
123     s4 vartop;                    /* next free variable index                 */
124     s4 localcount;                /* number of locals (at the start of var)   */
125     s4 varcount;                  /* total number of variables allocated      */
126     varinfo *var;                 /* variable array (same as jd->var)         */
127         methodinfo *m;                /* the method being analysed                */
128         jitdata *jd;                  /* current jitdata                          */
129         basicblock *last_real_block;  /* the last block before the empty one      */
130         bool repeat;                  /* if true, iterate the analysis again      */
131         exceptiontable **handlers;    /* exception handlers for the current block */
132         exceptiontable *extableend;   /* points to the last exception entry       */
133         stackelement exstack;         /* instack for exception handlers           */
134 };
135
136
137 /* macros for allocating/releasing variable indices *****************/
138
139 #define GET_NEW_INDEX(sd, new_varindex)                              \
140     do {                                                             \
141         assert((sd).vartop < (sd).varcount);                         \
142         (new_varindex) = ((sd).vartop)++;                            \
143     } while (0)
144
145 /* Not implemented now - could be used to reuse varindices.         */
146 /* Pay attention to not release a localvar once implementing it!    */
147 #define RELEASE_INDEX(sd, varindex)
148
149 #define GET_NEW_VAR(sd, new_varindex, newtype)                       \
150     do {                                                             \
151         GET_NEW_INDEX((sd), (new_varindex));                         \
152         (sd).var[new_index].type = (newtype);                        \
153     } while (0)
154
155
156 /* macros for querying variable properties **************************/
157
158 #define IS_OUTVAR(sp)                                                \
159     (sd.var[(sp)->varnum].flags & OUTVAR)
160
161 #define IS_PREALLOC(sp)                                              \
162     (sd.var[(sp)->varnum].flags & PREALLOC)
163
164 #define IS_TEMPVAR(sp)                                               \
165     ( ((sp)->varnum >= sd.localcount)                                \
166       && !(sd.var[(sp)->varnum].flags & (OUTVAR | PREALLOC)) )
167
168 #define IS_LOCALVAR_SD(sd, sp)                                       \
169          ((sp)->varnum < (sd).localcount)
170
171 #define IS_LOCALVAR(sp)                                              \
172     IS_LOCALVAR_SD(sd, (sp))
173
174
175 /* macros for setting variable properties ****************************/
176
177 #define SET_TEMPVAR(sp)                                              \
178     do {                                                             \
179         if (IS_LOCALVAR((sp))) {                                     \
180             GET_NEW_VAR(sd, new_index, (sp)->type);                  \
181             sd.var[new_index].flags = (sp)->flags;                   \
182             (sp)->varnum = new_index;                                \
183             (sp)->varkind = TEMPVAR;                                 \
184             if ((sp)->creator)                                       \
185                 (sp)->creator->dst.varindex = new_index;             \
186         }                                                            \
187         sd.var[(sp)->varnum].flags &= ~(OUTVAR | PREALLOC);          \
188     } while (0);
189
190 #define SET_PREALLOC(sp)                                             \
191     do {                                                             \
192         assert(!IS_LOCALVAR((sp)));                                  \
193         sd.var[(sp)->varnum].flags |= PREALLOC;                      \
194     } while (0);
195
196
197 /* macros for source operands ***************************************/
198
199 #define CLR_S1                                                       \
200     (iptr->s1.varindex = -1)
201
202 #define USE_S1(type1)                                                \
203     do {                                                             \
204         REQUIRE(1);                                                  \
205         CHECK_BASIC_TYPE(type1, curstack->type);                     \
206         iptr->s1.varindex = curstack->varnum;                        \
207     } while (0)
208
209 #define USE_S1_ANY                                                   \
210     do {                                                             \
211         REQUIRE(1);                                                  \
212         iptr->s1.varindex = curstack->varnum;                        \
213     } while (0)
214
215 #define USE_S1_S2(type1, type2)                                      \
216     do {                                                             \
217         REQUIRE(2);                                                  \
218         CHECK_BASIC_TYPE(type1, curstack->prev->type);               \
219         CHECK_BASIC_TYPE(type2, curstack->type);                     \
220         iptr->sx.s23.s2.varindex = curstack->varnum;                 \
221         iptr->s1.varindex = curstack->prev->varnum;                  \
222     } while (0)
223
224 #define USE_S1_S2_ANY_ANY                                            \
225     do {                                                             \
226         REQUIRE(2);                                                  \
227         iptr->sx.s23.s2.varindex = curstack->varnum;                 \
228         iptr->s1.varindex = curstack->prev->varnum;                  \
229     } while (0)
230
231 #define USE_S1_S2_S3(type1, type2, type3)                            \
232     do {                                                             \
233         REQUIRE(3);                                                  \
234         CHECK_BASIC_TYPE(type1, curstack->prev->prev->type);         \
235         CHECK_BASIC_TYPE(type2, curstack->prev->type);               \
236         CHECK_BASIC_TYPE(type3, curstack->type);                     \
237         iptr->sx.s23.s3.varindex = curstack->varnum;                 \
238         iptr->sx.s23.s2.varindex = curstack->prev->varnum;           \
239         iptr->s1.varindex = curstack->prev->prev->varnum;            \
240     } while (0)
241
242 /* The POPANY macro does NOT check stackdepth, or set stackdepth!   */
243 #define POPANY                                                       \
244     do {                                                             \
245         if (curstack->varkind == UNDEFVAR)                           \
246             curstack->varkind = TEMPVAR;                             \
247         curstack = curstack->prev;                                   \
248     } while (0)
249
250 #define POP_S1(type1)                                                \
251     do {                                                             \
252         USE_S1(type1);                                               \
253         if (curstack->varkind == UNDEFVAR)                           \
254             curstack->varkind = TEMPVAR;                             \
255         curstack = curstack->prev;                                   \
256     } while (0)
257
258 #define POP_S1_ANY                                                   \
259     do {                                                             \
260         USE_S1_ANY;                                                  \
261         if (curstack->varkind == UNDEFVAR)                           \
262             curstack->varkind = TEMPVAR;                             \
263         curstack = curstack->prev;                                   \
264     } while (0)
265
266 #define POP_S1_S2(type1, type2)                                      \
267     do {                                                             \
268         USE_S1_S2(type1, type2);                                     \
269         if (curstack->varkind == UNDEFVAR)                           \
270             curstack->varkind = TEMPVAR;                             \
271         if (curstack->prev->varkind == UNDEFVAR)                     \
272             curstack->prev->varkind = TEMPVAR;                       \
273         curstack = curstack->prev->prev;                             \
274     } while (0)
275
276 #define POP_S1_S2_ANY_ANY                                            \
277     do {                                                             \
278         USE_S1_S2_ANY_ANY;                                           \
279         if (curstack->varkind == UNDEFVAR)                           \
280             curstack->varkind = TEMPVAR;                             \
281         if (curstack->prev->varkind == UNDEFVAR)                     \
282             curstack->prev->varkind = TEMPVAR;                       \
283         curstack = curstack->prev->prev;                             \
284     } while (0)
285
286 #define POP_S1_S2_S3(type1, type2, type3)                            \
287     do {                                                             \
288         USE_S1_S2_S3(type1, type2, type3);                           \
289         if (curstack->varkind == UNDEFVAR)                           \
290             curstack->varkind = TEMPVAR;                             \
291         if (curstack->prev->varkind == UNDEFVAR)                     \
292             curstack->prev->varkind = TEMPVAR;                       \
293         if (curstack->prev->prev->varkind == UNDEFVAR)               \
294             curstack->prev->prev->varkind = TEMPVAR;                 \
295         curstack = curstack->prev->prev->prev;                       \
296     } while (0)
297
298 #define CLR_SX                                                       \
299     (iptr->sx.val.l = 0)
300
301
302 /* macros for setting the destination operand ***********************/
303
304 #define CLR_DST                                                      \
305     (iptr->dst.varindex = -1)
306
307 #define DST(typed, index)                                            \
308     do {                                                             \
309         NEWSTACKn((typed),(index));                                  \
310         curstack->creator = iptr;                                    \
311         iptr->dst.varindex = (index);                                \
312     } while (0)
313
314 #define DST_LOCALVAR(typed, index)                                   \
315     do {                                                             \
316         NEWSTACK((typed), LOCALVAR, (index));                        \
317         curstack->creator = iptr;                                    \
318         iptr->dst.varindex = (index);                                \
319     } while (0)
320
321
322 /* macro for propagating constant values ****************************/
323
324 #define COPY_VAL_AND_TYPE(sd, sindex, dindex)                        \
325     do {                                                             \
326         (sd).var[(dindex)].type = (sd).var[(sindex)].type;           \
327         (sd).var[(dindex)].vv  = (sd).var[(sindex)].vv;              \
328     } while (0)                                                      \
329
330
331 /* stack modelling macros *******************************************/
332
333 #define OP0_1(typed)                                                 \
334     do {                                                             \
335         CLR_S1;                                                      \
336         GET_NEW_VAR(sd, new_index, (typed));                         \
337         DST(typed, new_index);                                       \
338         stackdepth++;                                                \
339     } while (0)
340
341 #define OP1_0_ANY                                                    \
342     do {                                                             \
343         POP_S1_ANY;                                                  \
344         CLR_DST;                                                     \
345         stackdepth--;                                                \
346     } while (0)
347
348 #define OP1_BRANCH(type1)                                            \
349     do {                                                             \
350         POP_S1(type1);                                               \
351         stackdepth--;                                                \
352     } while (0)
353
354 #define OP1_1(type1, typed)                                          \
355     do {                                                             \
356         POP_S1(type1);                                               \
357         GET_NEW_VAR(sd, new_index, (typed));                         \
358         DST(typed, new_index);                                       \
359     } while (0)
360
361 #define OP2_1(type1, type2, typed)                                   \
362     do {                                                             \
363         POP_S1_S2(type1, type2);                                     \
364         GET_NEW_VAR(sd, new_index, (typed));                         \
365         DST(typed, new_index);                                       \
366         stackdepth--;                                                \
367     } while (0)
368
369 #define OP0_0                                                        \
370     do {                                                             \
371         CLR_S1;                                                      \
372         CLR_DST;                                                     \
373     } while (0)
374
375 #define OP0_BRANCH                                                   \
376     do {                                                             \
377         CLR_S1;                                                      \
378     } while (0)
379
380 #define OP1_0(type1)                                                 \
381     do {                                                             \
382         POP_S1(type1);                                               \
383         CLR_DST;                                                     \
384         stackdepth--;                                                \
385     } while (0)
386
387 #define OP2_0(type1, type2)                                          \
388     do {                                                             \
389         POP_S1_S2(type1, type2);                                     \
390         CLR_DST;                                                     \
391         stackdepth -= 2;                                             \
392     } while (0)
393
394 #define OP2_BRANCH(type1, type2)                                     \
395     do {                                                             \
396         POP_S1_S2(type1, type2);                                     \
397         stackdepth -= 2;                                             \
398     } while (0)
399
400 #define OP2_0_ANY_ANY                                                \
401     do {                                                             \
402         POP_S1_S2_ANY_ANY;                                           \
403         CLR_DST;                                                     \
404         stackdepth -= 2;                                             \
405     } while (0)
406
407 #define OP3_0(type1, type2, type3)                                   \
408     do {                                                             \
409         POP_S1_S2_S3(type1, type2, type3);                           \
410         CLR_DST;                                                     \
411         stackdepth -= 3;                                             \
412     } while (0)
413
414 #define LOAD(type1, index)                                           \
415     do {                                                             \
416         DST_LOCALVAR(type1, index);                                  \
417         stackdepth++;                                                \
418     } while (0)
419
420 #define STORE(type1, index)                                          \
421     do {                                                             \
422         POP_S1(type1);                                               \
423         stackdepth--;                                                \
424     } while (0)
425
426
427 /* macros for DUP elimination ***************************************/
428
429 /* XXX replace NEW_VAR with NEW_INDEX */
430 #define DUP_SLOT(sp)                                                 \
431     do {                                                             \
432         GET_NEW_VAR(sd, new_index, (sp)->type);                      \
433         COPY_VAL_AND_TYPE(sd, (sp)->varnum, new_index);              \
434         NEWSTACK((sp)->type, TEMPVAR, new_index);                    \
435     } while(0)
436
437 /* does not check input stackdepth */
438 #define MOVE_UP(sp)                                                  \
439     do {                                                             \
440         iptr->opc = ICMD_MOVE;                                       \
441         iptr->s1.varindex = (sp)->varnum;                            \
442         DUP_SLOT(sp);                                                \
443         curstack->creator = iptr;                                    \
444         iptr->dst.varindex = curstack->varnum;                       \
445         stackdepth++;                                                \
446     } while (0)
447
448 /* does not check input stackdepth */
449 #define COPY_UP(sp)                                                  \
450     do {                                                             \
451         SET_TEMPVAR((sp));                                           \
452         iptr->opc = ICMD_COPY;                                       \
453         iptr->s1.varindex = (sp)->varnum;                            \
454         DUP_SLOT(sp);                                                \
455         curstack->creator = iptr;                                    \
456         iptr->dst.varindex = curstack->varnum;                       \
457         stackdepth++;                                                \
458     } while (0)
459
460 #define COPY_DOWN(s, d)                                              \
461     do {                                                             \
462         SET_TEMPVAR((s));                                            \
463         iptr->opc = ICMD_COPY;                                       \
464         iptr->s1.varindex = (s)->varnum;                             \
465         iptr->dst.varindex = (d)->varnum;                            \
466         (d)->creator = iptr;                                         \
467     } while (0)
468
469
470 /* macros for branching / reaching basic blocks *********************/
471
472 #if defined(ENABLE_VERIFIER)
473 #define MARKREACHED(b, c)                                            \
474     do {                                                             \
475         if (!stack_mark_reached(&sd, (b), curstack, stackdepth))     \
476             return false;                                            \
477     } while (0)
478 #else
479 #define MARKREACHED(b, c)                                            \
480     do {                                                             \
481         (void) stack_mark_reached(&sd, (b), curstack, stackdepth);   \
482     } while (0)
483 #endif
484
485 #define BRANCH_TARGET(bt, tempbptr, tempsp)                          \
486     do {                                                             \
487         (bt).block = tempbptr = BLOCK_OF((bt).insindex);             \
488         MARKREACHED(tempbptr, tempsp);                               \
489     } while (0)
490
491 #define BRANCH(tempbptr, tempsp)                                     \
492     do {                                                             \
493         iptr->dst.block = tempbptr = BLOCK_OF(iptr->dst.insindex);   \
494         MARKREACHED(tempbptr, tempsp);                               \
495     } while (0)
496
497
498 /* forward declarations *******************************************************/
499
500 static void stack_create_invars(stackdata_t *sd, basicblock *b, 
501                                                                 stackptr curstack, int stackdepth);
502 static void stack_create_invars_from_outvars(stackdata_t *sd, basicblock *b);
503
504 #if defined(STACK_VERBOSE)
505 static void stack_verbose_show_varinfo(stackdata_t *sd, varinfo *v);
506 static void stack_verbose_show_variable(stackdata_t *sd, s4 index);
507 static void stack_verbose_show_block(stackdata_t *sd, basicblock *bptr);
508 static void stack_verbose_block_enter(stackdata_t *sd, bool reanalyse);
509 static void stack_verbose_block_exit(stackdata_t *sd, bool superblockend);
510 #endif
511
512
513 /* stack_init ******************************************************************
514
515    Initialized the stack analysis subsystem (called by jit_init).
516
517 *******************************************************************************/
518
519 bool stack_init(void)
520 {
521         return true;
522 }
523
524
525 /* stack_grow_variable_array ***************************************************
526
527    Grow the variable array so the given number of additional variables fits in.
528
529    IN:
530       sd...........stack analysis data
531           num..........number of additional variables
532
533 *******************************************************************************/
534
535 static void stack_grow_variable_array(stackdata_t *sd, s4 num)
536 {
537         s4 newcount;
538
539         assert(num >= 0);
540
541         if (num == 0)
542                 return;
543
544         /* XXX avoid too many reallocations */
545         newcount = sd->varcount + num;
546
547         sd->var = DMREALLOC(sd->var, varinfo, sd->varcount, newcount);
548         sd->varcount = newcount;
549         sd->jd->var = sd->var;
550         sd->jd->varcount = newcount;
551 }
552
553
554 /* stack_append_block **********************************************************
555
556    Append the given block after the last real block of the method (before
557    the pseudo-block at the end).
558
559    IN:
560       sd...........stack analysis data
561           b............the block to append
562
563 *******************************************************************************/
564
565 static void stack_append_block(stackdata_t *sd, basicblock *b)
566 {
567 #if defined(STACK_VERBOSE)
568         printf("APPENDING BLOCK L%0d\n", b->nr);
569 #endif
570
571         b->next = sd->last_real_block->next;
572         sd->last_real_block->next = b;
573         sd->last_real_block = b;
574         sd->jd->new_basicblockcount++;
575 }
576
577
578 /* stack_clone_block ***********************************************************
579
580    Create a copy of the given block and insert it at the end of the method.
581
582    CAUTION: This function does not copy the any variables or the instruction
583    list. It _does_, however, reserve space for the block's invars in the
584    variable array.
585
586    IN:
587       sd...........stack analysis data
588           b............the block to clone
589
590    RETURN VALUE:
591       a pointer to the copy
592
593 *******************************************************************************/
594
595 static basicblock * stack_clone_block(stackdata_t *sd, basicblock *b)
596 {
597         basicblock *clone;
598
599         clone = DNEW(basicblock);
600         *clone  = *b;
601
602         clone->iinstr = NULL;
603         clone->inlocals = NULL;
604         clone->invars = NULL;
605
606         clone->original = (b->original) ? b->original : b;
607         clone->copied_to = clone->original->copied_to;
608         clone->original->copied_to = clone;
609         clone->nr = sd->m->c_debug_nr++;
610         clone->next = NULL;
611         clone->flags = BBREACHED;
612
613         stack_append_block(sd, clone);
614
615         /* allocate space for the invars of the clone */
616
617         stack_grow_variable_array(sd, b->indepth);
618
619 #if defined(STACK_VERBOSE)
620         printf("cloning block L%03d ------> L%03d\n", b->nr, clone->nr);
621 #endif
622
623         return clone;
624 }
625
626
627 /* stack_create_invars *********************************************************
628
629    Create the invars for the given basic block. Also make a copy of the locals.
630
631    IN:
632       sd...........stack analysis data
633           b............block to create the invars for
634           curstack.....current stack top
635           stackdepth...current stack depth
636
637    This function creates STACKDEPTH invars and sets their types to the
638    types to the types of the corresponding slot in the current stack.
639
640 *******************************************************************************/
641
642 static void stack_create_invars(stackdata_t *sd, basicblock *b, 
643                                                                 stackptr curstack, int stackdepth)
644 {
645         stackptr sp;
646         int i;
647         int index;
648         varinfo *v;
649
650         assert(sd->vartop + stackdepth <= sd->varcount);
651
652         b->indepth = stackdepth;
653         b->invars = DMNEW(s4, stackdepth);
654
655         /* allocate the variable indices */
656         index = (sd->vartop += stackdepth);
657
658         i = stackdepth;
659         for (sp = curstack; i--; sp = sp->prev) {
660                 b->invars[i] = --index;
661                 v = sd->var + index;
662                 v->type = sp->type;
663                 v->flags = OUTVAR;
664                 v->vv = sd->var[sp->varnum].vv;
665 #if defined(STACK_VERBOSE) && 0
666                 printf("\tinvar[%d]: %d\n", i, sd->var[b->invars[i]]);
667 #endif
668         }
669
670         /* copy the current state of the local variables */
671
672         v = DMNEW(varinfo, sd->localcount);
673         b->inlocals = v;
674         for (i=0; i<sd->localcount; ++i)
675                 *v++ = sd->var[i];
676 }
677
678
679 /* stack_create_invars_from_outvars ********************************************
680
681    Create the invars for the given basic block. Also make a copy of the locals.
682    Types are propagated from the outvars of the current block.
683
684    IN:
685       sd...........stack analysis data
686           b............block to create the invars for
687
688 *******************************************************************************/
689
690 static void stack_create_invars_from_outvars(stackdata_t *sd, basicblock *b)
691 {
692         int i;
693         int n;
694         varinfo *sv, *dv;
695
696         n = sd->bptr->outdepth;
697         assert(sd->vartop + n <= sd->varcount);
698
699         b->indepth = n;
700         b->invars = DMNEW(s4, n);
701
702         if (n) {
703                 dv = sd->var + sd->vartop;
704
705                 /* allocate the invars */
706
707                 for (i=0; i<n; ++i, ++dv) {
708                         sv = sd->var + sd->bptr->outvars[i];
709                         b->invars[i] = sd->vartop++;
710                         dv->type = sv->type;
711                         dv->flags = OUTVAR;
712                         dv->vv = sv->vv;
713                 }
714         }
715
716         /* copy the current state of the local variables */
717
718         dv = DMNEW(varinfo, sd->localcount);
719         b->inlocals = dv;
720         for (i=0; i<sd->localcount; ++i)
721                 *dv++ = sd->var[i];
722 }
723
724
725 /* stack_check_invars **********************************************************
726
727    Check the current stack against the invars of the given basic block.
728    Depth and types must match.
729
730    IN:
731       sd...........stack analysis data
732           b............block which invars to check against
733           curstack.....current stack top
734           stackdepth...current stack depth
735
736    RETURN VALUE:
737       the destinaton block
738           NULL.........a VerifyError has been thrown
739
740 *******************************************************************************/
741
742 static basicblock * stack_check_invars(stackdata_t *sd, basicblock *b,
743                                                                            stackptr curstack, int stackdepth)
744 {
745         int i;
746         stackptr sp;
747         basicblock *orig;
748         bool separable;
749
750 #if defined(STACK_VERBOSE)
751         printf("stack_check_invars(L%03d)\n", b->nr);
752 #endif
753
754         /* find original of b */
755         if (b->original)
756                 b = b->original;
757         orig = b;
758
759 #if defined(STACK_VERBOSE)
760         printf("original is L%03d\n", orig->nr);
761 #endif
762
763         i = orig->indepth;
764
765         if (i != stackdepth) {
766                 exceptions_throw_verifyerror(sd->m, "Stack depth mismatch");
767                 return NULL;
768         }
769
770         do {
771                 separable = false;
772
773 #if defined(STACK_VERBOSE)
774                 printf("checking against ");
775                 stack_verbose_show_block(sd, b); printf("\n");
776 #endif
777
778                 sp = curstack;
779                 for (i = orig->indepth; i--; sp = sp->prev) {
780                         if (sd->var[b->invars[i]].type != sp->type) {
781                                 exceptions_throw_verifyerror_for_stack(sd->m, 
782                                                 sd->var[b->invars[i]].type);
783                                 return NULL;
784                         }
785
786                         if (sp->type == TYPE_RET) {
787                                 if (sd->var[b->invars[i]].vv.retaddr != sd->var[sp->varnum].vv.retaddr) {
788                                         separable = true;
789                                         break;
790                                 }
791                         }
792                 }
793
794                 if (b->inlocals) {
795                         for (i=0; i<sd->localcount; ++i) {
796                                 if (sd->var[i].type == TYPE_RET && b->inlocals[i].type == TYPE_RET) {
797                                         if (sd->var[i].vv.retaddr != b->inlocals[i].vv.retaddr) {
798                                                 separable = true;
799                                                 break;
800                                         }
801                                 }
802                         }
803                 }
804
805                 if (!separable) {
806                         /* XXX mark mixed type variables void */
807                         /* XXX cascading collapse? */
808 #if defined(STACK_VERBOSE)
809                         printf("------> using L%03d\n", b->nr);
810 #endif
811                         return b;
812                 }
813         } while ((b = b->copied_to) != NULL);
814
815         b = stack_clone_block(sd, orig);
816         if (!b)
817                 return NULL;
818
819         stack_create_invars(sd, b, curstack, stackdepth);
820         return b;
821 }
822
823
824 /* stack_check_invars_from_outvars *********************************************
825
826    Check the outvars of the current block against the invars of the given block.
827    Depth and types must match.
828
829    IN:
830       sd...........stack analysis data
831           b............block which invars to check against
832
833    RETURN VALUE:
834       the destinaton block
835           NULL.........a VerifyError has been thrown
836
837 *******************************************************************************/
838
839 static basicblock * stack_check_invars_from_outvars(stackdata_t *sd, basicblock *b)
840 {
841         int i;
842         int n;
843         varinfo *sv, *dv;
844         basicblock *orig;
845         bool separable;
846
847 #if defined(STACK_VERBOSE)
848         printf("stack_check_invars_from_outvars(L%03d)\n", b->nr);
849 #endif
850
851         /* find original of b */
852         if (b->original)
853                 b = b->original;
854         orig = b;
855
856 #if defined(STACK_VERBOSE)
857         printf("original is L%03d\n", orig->nr);
858 #endif
859
860         i = orig->indepth;
861         n = sd->bptr->outdepth;
862
863         if (i != n) {
864                 exceptions_throw_verifyerror(sd->m, "Stack depth mismatch");
865                 return NULL;
866         }
867
868         do {
869                 separable = false;
870
871 #if defined(STACK_VERBOSE)
872                 printf("checking against ");
873                 stack_verbose_show_block(sd, b); printf("\n");
874 #endif
875
876                 if (n) {
877                         dv = sd->var + b->invars[0];
878
879                         for (i=0; i<n; ++i, ++dv) {
880                                 sv = sd->var + sd->bptr->outvars[i];
881                                 if (sv->type != dv->type) {
882                                         exceptions_throw_verifyerror_for_stack(sd->m, dv->type);
883                                         return NULL;
884                                 }
885
886                                 if (dv->type == TYPE_RET) {
887                                         if (sv->vv.retaddr != dv->vv.retaddr) {
888                                                 separable = true;
889                                                 break;
890                                         }
891                                 }
892                         }
893                 }
894
895                 if (b->inlocals) {
896                         for (i=0; i<sd->localcount; ++i) {
897                                 if (sd->var[i].type == TYPE_RET && b->inlocals[i].type == TYPE_RET) {
898                                         if (sd->var[i].vv.retaddr != b->inlocals[i].vv.retaddr) {
899                                                 separable = true;
900                                                 break;
901                                         }
902                                 }
903                         }
904                 }
905
906                 if (!separable) {
907                         /* XXX mark mixed type variables void */
908                         /* XXX cascading collapse? */
909 #if defined(STACK_VERBOSE)
910                         printf("------> using L%03d\n", b->nr);
911 #endif
912                         return b;
913                 }
914         } while ((b = b->copied_to) != NULL);
915
916         b = stack_clone_block(sd, orig);
917         if (!b)
918                 return NULL;
919
920         stack_create_invars_from_outvars(sd, b);
921         return b;
922 }
923
924
925 /* stack_create_instack ********************************************************
926
927    Create the instack of the current basic block.
928
929    IN:
930       sd...........stack analysis data
931
932    RETURN VALUE:
933       the current stack top at the start of the basic block.
934
935 *******************************************************************************/
936
937 static stackptr stack_create_instack(stackdata_t *sd)
938 {
939     stackptr sp;
940         int depth;
941         int index;
942
943         if ((depth = sd->bptr->indepth) == 0)
944                 return NULL;
945
946     sp = (sd->new += depth);
947
948         while (depth--) {
949                 sp--;
950                 index = sd->bptr->invars[depth];
951                 sp->varnum = index;
952                 sp->type = sd->var[index].type;
953                 sp->prev = sp - 1;
954                 sp->creator = NULL;
955                 sp->flags = 0;
956                 sp->varkind = STACKVAR;
957         }
958         sp->prev = NULL;
959
960         /* return the top of the created stack */
961         return sd->new - 1;
962 }
963
964
965 /* stack_mark_reached **********************************************************
966
967    Mark the given block reached and propagate the current stack and locals to
968    it. This function specializes the target block, if necessary, and returns
969    a pointer to the specialized target.
970
971    IN:
972       sd...........stack analysis data
973           b............the block to reach
974           curstack.....the current stack top
975           stackdepth...the current stack depth
976
977    RETURN VALUE:
978       a pointer to (a specialized version of) the target
979           NULL.........a VerifyError has been thrown
980
981 *******************************************************************************/
982
983 static basicblock *stack_mark_reached(stackdata_t *sd, basicblock *b, stackptr curstack, int stackdepth) 
984 {
985 #if defined(STACK_VERBOSE)
986         printf("stack_mark_reached(L%03d from L%03d)\n", b->nr, sd->bptr->nr);
987 #endif
988         /* mark targets of backward branches */
989         if (b <= sd->bptr)
990                 b->bitflags |= BBFLAG_REPLACEMENT;
991
992         if (b->flags < BBREACHED) {
993                 /* b is reached for the first time. Create its invars. */
994
995 #if defined(STACK_VERBOSE)
996                 printf("reached L%03d for the first time\n", b->nr);
997 #endif
998
999                 stack_create_invars(sd, b, curstack, stackdepth);
1000
1001                 b->flags = BBREACHED;
1002
1003                 return b;
1004         } 
1005         else {
1006                 /* b has been reached before. Check that its invars match. */
1007
1008                 return stack_check_invars(sd, b, curstack, stackdepth);
1009         }
1010 }
1011
1012
1013 /* stack_mark_reached_from_outvars *********************************************
1014
1015    Mark the given block reached and propagate the outvars of the current block
1016    and the current locals to it. This function specializes the target block, 
1017    if necessary, and returns a pointer to the specialized target.
1018
1019    IN:
1020       sd...........stack analysis data
1021           b............the block to reach
1022
1023    RETURN VALUE:
1024       a pointer to (a specialized version of) the target
1025           NULL.........a VerifyError has been thrown
1026
1027 *******************************************************************************/
1028
1029 static basicblock *stack_mark_reached_from_outvars(stackdata_t *sd, basicblock *b)
1030 {
1031 #if defined(STACK_VERBOSE)
1032         printf("stack_mark_reached_from_outvars(L%03d from L%03d)\n", b->nr, sd->bptr->nr);
1033 #endif
1034         /* mark targets of backward branches */
1035         if (b <= sd->bptr)
1036                 b->bitflags |= BBFLAG_REPLACEMENT;
1037
1038         if (b->flags < BBREACHED) {
1039                 /* b is reached for the first time. Create its invars. */
1040
1041 #if defined(STACK_VERBOSE)
1042                 printf("reached L%03d for the first time\n", b->nr);
1043 #endif
1044
1045                 stack_create_invars_from_outvars(sd, b);
1046
1047                 b->flags = BBREACHED;
1048
1049                 return b;
1050         } 
1051         else {
1052                 /* b has been reached before. Check that its invars match. */
1053
1054                 return stack_check_invars_from_outvars(sd, b);
1055         }
1056 }
1057
1058
1059 /* stack_reach_next_block ******************************************************
1060
1061    Mark the following block reached and propagate the outvars of the current block
1062    and the current locals to it. This function specializes the target block, 
1063    if necessary, and returns a pointer to the specialized target.
1064
1065    IN:
1066       sd...........stack analysis data
1067
1068    RETURN VALUE:
1069       a pointer to (a specialized version of) the following block
1070           NULL.........a VerifyError has been thrown
1071
1072 *******************************************************************************/
1073
1074 static bool stack_reach_next_block(stackdata_t *sd)
1075 {
1076         basicblock *tbptr;
1077         instruction *iptr;
1078
1079         tbptr = (sd->bptr->original) ? sd->bptr->original : sd->bptr;
1080         tbptr = stack_mark_reached_from_outvars(sd, tbptr->next);
1081         if (!tbptr)
1082                 return false;
1083
1084         if (tbptr != sd->bptr->next) {
1085 #if defined(STACK_VERBOSE)
1086                 printf("NEXT IS NON-CONSEQUITIVE L%03d\n", tbptr->nr);
1087 #endif
1088                 iptr = sd->bptr->iinstr + sd->bptr->icount - 1;
1089                 assert(iptr->opc == ICMD_NOP);
1090                 iptr->opc = ICMD_GOTO;
1091                 iptr->dst.block = tbptr;
1092
1093                 if (tbptr->flags < BBFINISHED)
1094                         sd->repeat = true; /* XXX check if we really need to repeat */
1095         }
1096
1097         return true;
1098 }
1099
1100
1101 /* stack_reach_handlers ********************************************************
1102
1103    Reach the exception handlers for the current block.
1104
1105    IN:
1106       sd...........stack analysis data
1107
1108    RETURN VALUE:
1109      true.........everything ok
1110          false........a VerifyError has been thrown
1111
1112 *******************************************************************************/
1113
1114 static bool stack_reach_handlers(stackdata_t *sd)
1115 {
1116         s4 i;
1117         basicblock *tbptr;
1118
1119 #if defined(STACK_VERBOSE)
1120         printf("reaching exception handlers...\n");
1121 #endif
1122
1123         for (i=0; sd->handlers[i]; ++i) {
1124                 tbptr = sd->handlers[i]->handler;
1125
1126                 tbptr->type = BBTYPE_EXH;
1127                 tbptr->predecessorcount = CFG_UNKNOWN_PREDECESSORS;
1128
1129                 /* reach (and specialize) the handler block */
1130
1131                 tbptr = stack_mark_reached(sd, tbptr, &(sd->exstack), 1);
1132
1133                 if (tbptr == NULL)
1134                         return false;
1135
1136                 sd->handlers[i]->handler = tbptr;
1137         }
1138
1139         return true;
1140 }
1141
1142
1143 /* stack_reanalyse_block  ******************************************************
1144
1145    Re-analyse the current block. This is called if either the block itself
1146    has already been analysed before, or the current block is a clone of an
1147    already analysed block, and this clone is reached for the first time.
1148    In the latter case, this function does all that is necessary for fully
1149    cloning the block (cloning the instruction list and variables, etc.).
1150
1151    IN:
1152       sd...........stack analysis data
1153
1154    RETURN VALUE:
1155      true.........everything ok
1156          false........a VerifyError has been thrown
1157
1158 *******************************************************************************/
1159
1160 #define RELOCATE(index)                                              \
1161     do {                                                             \
1162         if ((index) >= blockvarstart)                                \
1163             (index) += blockvarshift;                                \
1164         else if ((index) >= invarstart)                              \
1165             (index) += invarshift;                                   \
1166     } while (0)
1167
1168 bool stack_reanalyse_block(stackdata_t *sd)
1169 {
1170         instruction *iptr;
1171         basicblock *b;
1172         basicblock *orig;
1173         s4 len;
1174         s4 invarstart;
1175         s4 blockvarstart;
1176         s4 invarshift;
1177         s4 blockvarshift;
1178         s4 i, j;
1179         s4 *argp;
1180         branch_target_t *table;
1181         lookup_target_t *lookup;
1182         bool superblockend;
1183         bool maythrow;
1184         bool cloneinstructions;
1185         basicblock *tbptr;
1186         exceptiontable *ex;
1187
1188 #if defined(STACK_VERBOSE)
1189         stack_verbose_block_enter(sd, true);
1190 #endif
1191
1192         b = sd->bptr;
1193
1194         if (!b->iinstr) {
1195                 orig = b->original;
1196                 assert(orig != NULL);
1197
1198                 /* clone the instruction list */
1199
1200                 cloneinstructions = true;
1201
1202                 assert(orig->iinstr);
1203                 len = orig->icount;
1204                 iptr = DMNEW(instruction, len + 1);
1205
1206                 MCOPY(iptr, orig->iinstr, instruction, len);
1207                 iptr[len].opc = ICMD_NOP;
1208                 b->iinstr = iptr;
1209                 b->icount = ++len;
1210
1211                 /* allocate space for the clone's block variables */
1212
1213                 stack_grow_variable_array(sd, orig->varcount);
1214
1215                 /* we already have the invars set */
1216
1217                 assert(b->indepth == orig->indepth);
1218
1219                 /* calculate relocation shifts for invars and block variables */
1220
1221                 if (orig->indepth) {
1222                         invarstart = orig->invars[0];
1223                         invarshift = b->invars[0] - invarstart;
1224                 }
1225                 else {
1226                         invarstart = INT_MAX;
1227                         invarshift = 0;
1228                 }
1229                 blockvarstart = orig->varstart;
1230                 blockvarshift = sd->vartop - blockvarstart;
1231
1232                 /* copy block variables */
1233
1234                 b->varstart = sd->vartop;
1235                 b->varcount = orig->varcount;
1236                 sd->vartop += b->varcount;
1237                 MCOPY(sd->var + b->varstart, sd->var + orig->varstart, varinfo, b->varcount);
1238
1239                 /* copy outvars */
1240
1241                 b->outdepth = orig->outdepth;
1242                 b->outvars = DMNEW(s4, orig->outdepth);
1243                 MCOPY(b->outvars, orig->outvars, s4, orig->outdepth);
1244
1245                 /* clone exception handlers */
1246
1247                 for (i=0; sd->handlers[i]; ++i) {
1248                         ex = DNEW(exceptiontable);
1249                         ex->handler = sd->handlers[i]->handler;
1250                         ex->start = b;
1251                         ex->end = b; /* XXX hack, see end of new_stack_analyse */
1252                         ex->catchtype = sd->handlers[i]->catchtype;
1253                         ex->down = NULL;
1254
1255                         assert(sd->extableend->down == NULL);
1256                         sd->extableend->down = ex;
1257                         sd->extableend = ex;
1258                         sd->jd->cd->exceptiontablelength++;
1259
1260                         sd->handlers[i] = ex;
1261                 }
1262         }
1263         else {
1264                 cloneinstructions = false;
1265                 invarshift = 0;
1266                 blockvarshift = 0;
1267                 invarstart = sd->vartop;
1268                 blockvarstart = sd->vartop;
1269                 iptr = b->iinstr;
1270         }
1271
1272         if (b->original) {
1273                 /* find exception handlers for the cloned block */
1274                 len = 0;
1275                 ex = sd->jd->cd->exceptiontable;
1276                 for (; ex != NULL; ex = ex->down) {
1277                         /* XXX the cloned exception handlers have identical */
1278                         /* start end end blocks.                            */
1279                         if ((ex->start == b) && (ex->end == b)) {
1280                                 sd->handlers[len++] = ex;
1281                         }
1282                 }
1283                 sd->handlers[len] = NULL;
1284         }
1285
1286 #if defined(STACK_VERBOSE)
1287         printf("invarstart = %d, blockvarstart = %d\n", invarstart, blockvarstart);
1288         printf("invarshift = %d, blockvarshift = %d\n", invarshift, blockvarshift);
1289 #endif
1290
1291         /* mark block as finished */
1292
1293         b->flags = BBFINISHED;
1294
1295         /* initialize locals at the start of this block */
1296
1297         if (b->inlocals)
1298                 MCOPY(sd->var, b->inlocals, varinfo, sd->localcount);
1299
1300         /* reach exception handlers for this block */
1301
1302         if (!stack_reach_handlers(sd))
1303                 return false;
1304
1305         superblockend = false;
1306
1307         for (len = b->icount; len--; iptr++) {
1308 #if defined(STACK_VERBOSE)
1309                 new_show_icmd(sd->jd, iptr, false, SHOW_STACK);
1310                 printf("\n");
1311 #endif
1312
1313                 maythrow = false;
1314
1315                 switch (iptr->opc) {
1316                         case ICMD_RET:
1317                                 j = iptr->s1.varindex;
1318
1319                                 if (sd->var[j].type != TYPE_RET) {
1320                                         exceptions_throw_verifyerror(sd->m, "RET with non-returnAddress value");
1321                                         return false;
1322                                 }
1323
1324                                 iptr->dst.block = stack_mark_reached_from_outvars(sd, sd->var[j].vv.retaddr);
1325                                 superblockend = true;
1326                                 break;
1327
1328                         case ICMD_JSR:
1329                                 iptr->sx.s23.s3.jsrtarget.block = stack_mark_reached_from_outvars(sd, iptr->sx.s23.s3.jsrtarget.block);
1330                                 superblockend = true;
1331                                 break;
1332
1333                         case ICMD_RETURN:
1334                                 superblockend = true;
1335                                 break;
1336
1337                         case ICMD_CHECKNULL:
1338                         case ICMD_PUTSTATICCONST:
1339                                 maythrow = true;
1340                                 break;
1341
1342                         case ICMD_NOP:
1343                         case ICMD_IINC:
1344                         case ICMD_INLINE_START:
1345                         case ICMD_INLINE_END:
1346                         case ICMD_INLINE_GOTO:
1347                                 break;
1348
1349                         case ICMD_GOTO:
1350                                 iptr->dst.block = stack_mark_reached_from_outvars(sd, iptr->dst.block);
1351                                 superblockend = true;
1352                                 break;
1353
1354                                 /* pop 0 push 1 const */
1355
1356                         case ICMD_ACONST:
1357                                 maythrow = true;
1358                         case ICMD_ICONST:
1359                         case ICMD_LCONST:
1360                         case ICMD_FCONST:
1361                         case ICMD_DCONST:
1362
1363                                 /* pop 0 push 1 load */
1364
1365                         case ICMD_ILOAD:
1366                         case ICMD_LLOAD:
1367                         case ICMD_FLOAD:
1368                         case ICMD_DLOAD:
1369                         case ICMD_ALOAD:
1370                                 RELOCATE(iptr->dst.varindex);
1371                                 break;
1372
1373                                 /* pop 2 push 1 */
1374
1375                         case ICMD_IALOAD:
1376                         case ICMD_LALOAD:
1377                         case ICMD_FALOAD:
1378                         case ICMD_DALOAD:
1379                         case ICMD_AALOAD:
1380                         case ICMD_BALOAD:
1381                         case ICMD_CALOAD:
1382                         case ICMD_SALOAD:
1383                                 RELOCATE(iptr->sx.s23.s2.varindex);
1384                                 RELOCATE(iptr->s1.varindex);
1385                                 RELOCATE(iptr->dst.varindex);
1386                                 maythrow = true;
1387                                 break;
1388
1389                                 /* pop 3 push 0 */
1390
1391                         case ICMD_IASTORE:
1392                         case ICMD_LASTORE:
1393                         case ICMD_FASTORE:
1394                         case ICMD_DASTORE:
1395                         case ICMD_AASTORE:
1396                         case ICMD_BASTORE:
1397                         case ICMD_CASTORE:
1398                         case ICMD_SASTORE:
1399                                 RELOCATE(iptr->sx.s23.s3.varindex);
1400                                 RELOCATE(iptr->sx.s23.s2.varindex);
1401                                 RELOCATE(iptr->s1.varindex);
1402                                 maythrow = true;
1403                                 break;
1404
1405                                 /* pop 1 push 0 store */
1406
1407                         case ICMD_ISTORE:
1408                         case ICMD_LSTORE:
1409                         case ICMD_FSTORE:
1410                         case ICMD_DSTORE:
1411                         case ICMD_ASTORE:
1412                                 RELOCATE(iptr->s1.varindex);
1413
1414                                 j = iptr->dst.varindex;
1415                                 COPY_VAL_AND_TYPE(*sd, iptr->s1.varindex, j);
1416                                 break;
1417
1418                                 /* pop 1 push 0 */
1419
1420                         case ICMD_ARETURN:
1421                         case ICMD_ATHROW:
1422                                 maythrow = true;
1423                         case ICMD_IRETURN:
1424                         case ICMD_LRETURN:
1425                         case ICMD_FRETURN:
1426                         case ICMD_DRETURN:
1427                                 RELOCATE(iptr->s1.varindex);
1428                                 superblockend = true;
1429                                 break;
1430
1431                         case ICMD_PUTSTATIC:
1432                         case ICMD_PUTFIELDCONST:
1433                                 maythrow = true;
1434                         case ICMD_POP:
1435                                 RELOCATE(iptr->s1.varindex);
1436                                 break;
1437
1438                                 /* pop 1 push 0 branch */
1439
1440                         case ICMD_IFNULL:
1441                         case ICMD_IFNONNULL:
1442
1443                         case ICMD_IFEQ:
1444                         case ICMD_IFNE:
1445                         case ICMD_IFLT:
1446                         case ICMD_IFGE:
1447                         case ICMD_IFGT:
1448                         case ICMD_IFLE:
1449
1450                         case ICMD_IF_LEQ:
1451                         case ICMD_IF_LNE:
1452                         case ICMD_IF_LLT:
1453                         case ICMD_IF_LGE:
1454                         case ICMD_IF_LGT:
1455                         case ICMD_IF_LLE:
1456                                 RELOCATE(iptr->s1.varindex);
1457                                 iptr->dst.block = stack_mark_reached_from_outvars(sd, iptr->dst.block);
1458                                 break;
1459
1460                                 /* pop 1 push 0 table branch */
1461
1462                         case ICMD_TABLESWITCH:
1463                                 i = iptr->sx.s23.s3.tablehigh - iptr->sx.s23.s2.tablelow + 1 + 1;
1464
1465                                 if (cloneinstructions) {
1466                                         table = DMNEW(branch_target_t, i);
1467                                         MCOPY(table, iptr->dst.table, branch_target_t, i);
1468                                         iptr->dst.table = table;
1469                                 }
1470                                 else {
1471                                         table = iptr->dst.table;
1472                                 }
1473
1474                                 RELOCATE(iptr->s1.varindex);
1475                                 while (i--) {
1476                                         table->block = stack_mark_reached_from_outvars(sd, table->block);
1477                                         table++;
1478                                 }
1479                                 superblockend = true;
1480                                 break;
1481
1482                         case ICMD_LOOKUPSWITCH:
1483                                 i = iptr->sx.s23.s2.lookupcount;
1484                                 if (cloneinstructions) {
1485                                         lookup = DMNEW(lookup_target_t, i);
1486                                         MCOPY(lookup, iptr->dst.lookup, lookup_target_t, i);
1487                                         iptr->dst.lookup = lookup;
1488                                 }
1489                                 else {
1490                                         lookup = iptr->dst.lookup;
1491                                 }
1492                                 RELOCATE(iptr->s1.varindex);
1493                                 while (i--) {
1494                                         lookup->target.block = stack_mark_reached_from_outvars(sd, lookup->target.block);
1495                                         lookup++;
1496                                 }
1497                                 iptr->sx.s23.s3.lookupdefault.block = stack_mark_reached_from_outvars(sd, iptr->sx.s23.s3.lookupdefault.block);
1498                                 superblockend = true;
1499                                 break;
1500
1501                         case ICMD_MONITORENTER:
1502                         case ICMD_MONITOREXIT:
1503                                 RELOCATE(iptr->s1.varindex);
1504                                 maythrow = true;
1505                                 break;
1506
1507                                 /* pop 2 push 0 branch */
1508
1509                         case ICMD_IF_ICMPEQ:
1510                         case ICMD_IF_ICMPNE:
1511                         case ICMD_IF_ICMPLT:
1512                         case ICMD_IF_ICMPGE:
1513                         case ICMD_IF_ICMPGT:
1514                         case ICMD_IF_ICMPLE:
1515
1516                         case ICMD_IF_LCMPEQ:
1517                         case ICMD_IF_LCMPNE:
1518                         case ICMD_IF_LCMPLT:
1519                         case ICMD_IF_LCMPGE:
1520                         case ICMD_IF_LCMPGT:
1521                         case ICMD_IF_LCMPLE:
1522
1523                         case ICMD_IF_FCMPEQ:
1524                         case ICMD_IF_FCMPNE:
1525
1526                         case ICMD_IF_FCMPL_LT:
1527                         case ICMD_IF_FCMPL_GE:
1528                         case ICMD_IF_FCMPL_GT:
1529                         case ICMD_IF_FCMPL_LE:
1530
1531                         case ICMD_IF_FCMPG_LT:
1532                         case ICMD_IF_FCMPG_GE:
1533                         case ICMD_IF_FCMPG_GT:
1534                         case ICMD_IF_FCMPG_LE:
1535
1536                         case ICMD_IF_DCMPEQ:
1537                         case ICMD_IF_DCMPNE:
1538
1539                         case ICMD_IF_DCMPL_LT:
1540                         case ICMD_IF_DCMPL_GE:
1541                         case ICMD_IF_DCMPL_GT:
1542                         case ICMD_IF_DCMPL_LE:
1543
1544                         case ICMD_IF_DCMPG_LT:
1545                         case ICMD_IF_DCMPG_GE:
1546                         case ICMD_IF_DCMPG_GT:
1547                         case ICMD_IF_DCMPG_LE:
1548
1549                         case ICMD_IF_ACMPEQ:
1550                         case ICMD_IF_ACMPNE:
1551                                 RELOCATE(iptr->sx.s23.s2.varindex);
1552                                 RELOCATE(iptr->s1.varindex);
1553                                 iptr->dst.block = stack_mark_reached_from_outvars(sd, iptr->dst.block);
1554                                 break;
1555
1556                                 /* pop 2 push 0 */
1557
1558                         case ICMD_PUTFIELD:
1559                         case ICMD_IASTORECONST:
1560                         case ICMD_LASTORECONST:
1561                         case ICMD_AASTORECONST:
1562                         case ICMD_BASTORECONST:
1563                         case ICMD_CASTORECONST:
1564                         case ICMD_SASTORECONST:
1565                                 maythrow = true;
1566                         case ICMD_POP2:
1567                                 RELOCATE(iptr->sx.s23.s2.varindex);
1568                                 RELOCATE(iptr->s1.varindex);
1569                                 break;
1570
1571                                 /* pop 0 push 1 copy */
1572
1573                         case ICMD_COPY:
1574                         case ICMD_MOVE:
1575                                 RELOCATE(iptr->dst.varindex);
1576                                 RELOCATE(iptr->s1.varindex);
1577                                 COPY_VAL_AND_TYPE(*sd, iptr->s1.varindex, iptr->dst.varindex);
1578                                 break;
1579
1580                                 /* pop 2 push 1 */
1581
1582                         case ICMD_IDIV:
1583                         case ICMD_IREM:
1584                         case ICMD_LDIV:
1585                         case ICMD_LREM:
1586                                 maythrow = true;
1587                         case ICMD_IADD:
1588                         case ICMD_ISUB:
1589                         case ICMD_IMUL:
1590                         case ICMD_ISHL:
1591                         case ICMD_ISHR:
1592                         case ICMD_IUSHR:
1593                         case ICMD_IAND:
1594                         case ICMD_IOR:
1595                         case ICMD_IXOR:
1596                         case ICMD_LADD:
1597                         case ICMD_LSUB:
1598                         case ICMD_LMUL:
1599                         case ICMD_LOR:
1600                         case ICMD_LAND:
1601                         case ICMD_LXOR:
1602                         case ICMD_LSHL:
1603                         case ICMD_LSHR:
1604                         case ICMD_LUSHR:
1605                         case ICMD_FADD:
1606                         case ICMD_FSUB:
1607                         case ICMD_FMUL:
1608                         case ICMD_FDIV:
1609                         case ICMD_FREM:
1610                         case ICMD_DADD:
1611                         case ICMD_DSUB:
1612                         case ICMD_DMUL:
1613                         case ICMD_DDIV:
1614                         case ICMD_DREM:
1615                         case ICMD_LCMP:
1616                         case ICMD_FCMPL:
1617                         case ICMD_FCMPG:
1618                         case ICMD_DCMPL:
1619                         case ICMD_DCMPG:
1620                                 RELOCATE(iptr->sx.s23.s2.varindex);
1621                                 RELOCATE(iptr->s1.varindex);
1622                                 RELOCATE(iptr->dst.varindex);
1623                                 break;
1624
1625                                 /* pop 1 push 1 */
1626
1627                         case ICMD_CHECKCAST:
1628                         case ICMD_ARRAYLENGTH:
1629                         case ICMD_INSTANCEOF:
1630                         case ICMD_NEWARRAY:
1631                         case ICMD_ANEWARRAY:
1632                                 maythrow = true;
1633                         case ICMD_GETFIELD:
1634                         case ICMD_IADDCONST:
1635                         case ICMD_ISUBCONST:
1636                         case ICMD_IMULCONST:
1637                         case ICMD_IMULPOW2:
1638                         case ICMD_IDIVPOW2:
1639                         case ICMD_IREMPOW2:
1640                         case ICMD_IANDCONST:
1641                         case ICMD_IORCONST:
1642                         case ICMD_IXORCONST:
1643                         case ICMD_ISHLCONST:
1644                         case ICMD_ISHRCONST:
1645                         case ICMD_IUSHRCONST:
1646                         case ICMD_LADDCONST:
1647                         case ICMD_LSUBCONST:
1648                         case ICMD_LMULCONST:
1649                         case ICMD_LMULPOW2:
1650                         case ICMD_LDIVPOW2:
1651                         case ICMD_LREMPOW2:
1652                         case ICMD_LANDCONST:
1653                         case ICMD_LORCONST:
1654                         case ICMD_LXORCONST:
1655                         case ICMD_LSHLCONST:
1656                         case ICMD_LSHRCONST:
1657                         case ICMD_LUSHRCONST:
1658                         case ICMD_INEG:
1659                         case ICMD_INT2BYTE:
1660                         case ICMD_INT2CHAR:
1661                         case ICMD_INT2SHORT:
1662                         case ICMD_LNEG:
1663                         case ICMD_FNEG:
1664                         case ICMD_DNEG:
1665                         case ICMD_I2L:
1666                         case ICMD_I2F:
1667                         case ICMD_I2D:
1668                         case ICMD_L2I:
1669                         case ICMD_L2F:
1670                         case ICMD_L2D:
1671                         case ICMD_F2I:
1672                         case ICMD_F2L:
1673                         case ICMD_F2D:
1674                         case ICMD_D2I:
1675                         case ICMD_D2L:
1676                         case ICMD_D2F:
1677                                 RELOCATE(iptr->s1.varindex);
1678                                 RELOCATE(iptr->dst.varindex);
1679                                 break;
1680
1681                                 /* pop 0 push 1 */
1682
1683                         case ICMD_GETSTATIC:
1684                         case ICMD_NEW:
1685                                 maythrow = true;
1686                                 RELOCATE(iptr->dst.varindex);
1687                                 break;
1688
1689                                 /* pop many push any */
1690
1691                         case ICMD_INVOKESTATIC:
1692                         case ICMD_INVOKESPECIAL:
1693                         case ICMD_INVOKEVIRTUAL:
1694                         case ICMD_INVOKEINTERFACE:
1695                         case ICMD_BUILTIN:
1696                         case ICMD_MULTIANEWARRAY:
1697                                 i = iptr->s1.argcount;
1698                                 if (cloneinstructions) {
1699                                         argp = DMNEW(s4, i);
1700                                         MCOPY(argp, iptr->sx.s23.s2.args, s4, i);
1701                                         iptr->sx.s23.s2.args = argp;
1702                                 }
1703                                 else {
1704                                         argp = iptr->sx.s23.s2.args;
1705                                 }
1706
1707                                 maythrow = true;
1708                                 while (--i >= 0) {
1709                                         RELOCATE(*argp);
1710                                         argp++;
1711                                 }
1712                                 RELOCATE(iptr->dst.varindex);
1713                                 break;
1714
1715                         default:
1716                                 *exceptionptr =
1717                                         new_internalerror("Unknown ICMD %d during stack re-analysis",
1718                                                         iptr->opc);
1719                                 return false;
1720                 } /* switch */
1721
1722 #if defined(STACK_VERBOSE)
1723                 new_show_icmd(sd->jd, iptr, false, SHOW_STACK);
1724                 printf("\n");
1725 #endif
1726         }
1727
1728         /* relocate outvars */
1729
1730         for (i=0; i<b->outdepth; ++i) {
1731                 RELOCATE(b->outvars[i]);
1732         }
1733
1734 #if defined(STACK_VERBOSE)
1735         stack_verbose_block_exit(sd, superblockend);
1736 #endif
1737
1738         /* propagate to the next block */
1739
1740         if (!superblockend)
1741                 if (!stack_reach_next_block(sd))
1742                         return false;
1743
1744         return true;
1745 }
1746
1747
1748 /* stack_analyse ***************************************************************
1749
1750    Analyse_stack uses the intermediate code created by parse.c to
1751    build a model of the JVM operand stack for the current method.
1752    
1753    The following checks are performed:
1754      - check for operand stack underflow (before each instruction)
1755      - check for operand stack overflow (after[1] each instruction)
1756      - check for matching stack depth at merging points
1757      - check for matching basic types[2] at merging points
1758      - check basic types for instruction input (except for BUILTIN*
1759            opcodes, INVOKE* opcodes and MULTIANEWARRAY)
1760    
1761    [1]) Checking this after the instruction should be ok. parse.c
1762    counts the number of required stack slots in such a way that it is
1763    only vital that we don't exceed `maxstack` at basic block
1764    boundaries.
1765    
1766    [2]) 'basic types' means the distinction between INT, LONG, FLOAT,
1767    DOUBLE and ADDRESS types. Subtypes of INT and different ADDRESS
1768    types are not discerned.
1769
1770 *******************************************************************************/
1771
1772 bool new_stack_analyse(jitdata *jd)
1773 {
1774         methodinfo   *m;              /* method being analyzed                    */
1775         codeinfo     *code;
1776         codegendata  *cd;
1777         registerdata *rd;
1778         stackdata_t   sd;
1779         int           b_index;        /* basic block index                        */
1780         int           stackdepth;
1781         stackptr      curstack;       /* current stack top                        */
1782         stackptr      copy;
1783         int           opcode;         /* opcode of current instruction            */
1784         int           i, j;
1785         int           javaindex;
1786         int           len;            /* # of instructions after the current one  */
1787         bool          superblockend;  /* if true, no fallthrough to next block    */
1788         bool          deadcode;       /* true if no live code has been reached    */
1789         instruction  *iptr;           /* the current instruction                  */
1790         basicblock   *tbptr;
1791         basicblock   *original;
1792         exceptiontable *ex;
1793
1794         stackptr     *last_store_boundary;
1795         stackptr      coalescing_boundary;
1796
1797         stackptr      src1, src2, src3, src4, dst1, dst2;
1798
1799         branch_target_t *table;
1800         lookup_target_t *lookup;
1801 #if defined(ENABLE_VERIFIER)
1802         int           expectedtype;   /* used by CHECK_BASIC_TYPE                 */
1803 #endif
1804         builtintable_entry *bte;
1805         methoddesc         *md;
1806         constant_FMIref    *fmiref;
1807 #if defined(ENABLE_STATISTICS)
1808         int           iteration_count;  /* number of iterations of analysis       */
1809 #endif
1810         int           new_index; /* used to get a new var index with GET_NEW_INDEX*/
1811
1812 #if defined(STACK_VERBOSE)
1813         new_show_method(jd, SHOW_PARSE);
1814 #endif
1815
1816         /* get required compiler data - initialization */
1817
1818         m    = jd->m;
1819         code = jd->code;
1820         cd   = jd->cd;
1821         rd   = jd->rd;
1822
1823         /* initialize the stackdata_t struct */
1824
1825         sd.m = m;
1826         sd.jd = jd;
1827         sd.varcount = jd->varcount;
1828         sd.vartop =  jd->vartop;
1829         sd.localcount = jd->localcount;
1830         sd.var = jd->var;
1831         sd.handlers = DMNEW(exceptiontable *, cd->exceptiontablelength + 1);
1832         sd.exstack.type = TYPE_ADR;
1833         sd.exstack.prev = NULL;
1834         sd.exstack.varnum = 0; /* XXX do we need a real variable index here? */
1835
1836 #if defined(ENABLE_LSRA)
1837         m->maxlifetimes = 0;
1838 #endif
1839
1840 #if defined(ENABLE_STATISTICS)
1841         iteration_count = 0;
1842 #endif
1843
1844         /* find the last real basic block */
1845         
1846         sd.last_real_block = NULL;
1847         tbptr = jd->new_basicblocks;
1848         while (tbptr->next) {
1849                 sd.last_real_block = tbptr;
1850                 tbptr = tbptr->next;
1851         }
1852         assert(sd.last_real_block);
1853
1854         /* find the last exception handler */
1855
1856         if (cd->exceptiontablelength)
1857                 sd.extableend = cd->exceptiontable + cd->exceptiontablelength - 1;
1858         else
1859                 sd.extableend = NULL;
1860
1861         /* init jd->interface_map */
1862
1863         jd->interface_map = DMNEW(interface_info, m->maxstack * 5);
1864         for (i = 0; i < m->maxstack * 5; i++)
1865                 jd->interface_map[i].flags = UNUSED;
1866
1867         last_store_boundary = DMNEW(stackptr, cd->maxlocals);
1868
1869         /* initialize flags and invars (none) of first block */
1870
1871         jd->new_basicblocks[0].flags = BBREACHED;
1872         jd->new_basicblocks[0].invars = NULL;
1873         jd->new_basicblocks[0].indepth = 0;
1874
1875         /* stack analysis loop (until fixpoint reached) **************************/
1876
1877         do {
1878 #if defined(ENABLE_STATISTICS)
1879                 iteration_count++;
1880 #endif
1881
1882                 /* initialize loop over basic blocks */
1883
1884                 sd.bptr = jd->new_basicblocks;
1885                 superblockend = true;
1886                 sd.repeat = false;
1887                 curstack = NULL; stackdepth = 0;
1888                 deadcode = true;
1889
1890                 /* iterate over basic blocks *****************************************/
1891
1892                 for (; sd.bptr; sd.bptr = sd.bptr->next) {
1893
1894                         if (sd.bptr->flags == BBDELETED) {
1895                                 /* This block has been deleted - do nothing. */
1896
1897                                 continue;
1898                         }
1899
1900                         if (superblockend && (sd.bptr->flags < BBREACHED)) {
1901                                 /* This block has not been reached so far, and we      */
1902                                 /* don't fall into it, so we'll have to iterate again. */
1903
1904                                 sd.repeat = true;
1905                                 continue;
1906                         }
1907
1908                         if (sd.bptr->flags > BBREACHED) {
1909                                 /* This block is already finished. */
1910
1911                                 superblockend = true;
1912                                 continue;
1913                         }
1914
1915                         if (sd.bptr->original && sd.bptr->original->flags < BBFINISHED) {
1916                                 /* This block is a clone and the original has not been */
1917                                 /* analysed, yet. Analyse it on the next iteration.    */
1918
1919                                 sd.repeat = true;
1920                                 continue;
1921                         }
1922
1923                         /* This block has to be analysed now. */
1924
1925                         /* XXX The rest of this block is still indented one level too */
1926                         /* much in order to avoid a giant diff by changing that.      */
1927
1928                                 /* We know that sd.bptr->flags == BBREACHED. */
1929                                 /* This block has been reached before.    */
1930
1931                                 assert(sd.bptr->flags == BBREACHED);
1932                                 stackdepth = sd.bptr->indepth;
1933
1934                                 /* find exception handlers for this block */
1935
1936                                 /* determine the active exception handlers for this block */
1937                                 /* XXX could use a faster algorithm with sorted lists or  */
1938                                 /* something?                                             */
1939
1940                                 original = (sd.bptr->original) ? sd.bptr->original : sd.bptr;
1941
1942                                 len = 0;
1943                                 ex = cd->exceptiontable;
1944                                 for (; ex != NULL; ex = ex->down) {
1945                                         if ((ex->start <= original) && (ex->end > original)) {
1946                                                 sd.handlers[len++] = ex;
1947                                         }
1948                                 }
1949                                 sd.handlers[len] = NULL;
1950
1951
1952                                 /* reanalyse cloned block */
1953
1954                                 if (sd.bptr->original) {
1955                                         if (!stack_reanalyse_block(&sd))
1956                                                 return false;
1957                                         continue;
1958                                 }
1959
1960                                 /* reset the new pointer for allocating stackslots */
1961
1962                                 sd.new = jd->new_stack;
1963
1964                                 /* create the instack of this block */
1965
1966                                 curstack = stack_create_instack(&sd);
1967
1968                                 /* initialize locals at the start of this block */
1969
1970                                 if (sd.bptr->inlocals)
1971                                         MCOPY(sd.var, sd.bptr->inlocals, varinfo, sd.localcount);
1972
1973                                 /* set up local variables for analyzing this block */
1974
1975                                 deadcode = false;
1976                                 superblockend = false;
1977                                 len = sd.bptr->icount;
1978                                 iptr = sd.bptr->iinstr;
1979                                 b_index = sd.bptr - jd->new_basicblocks;
1980
1981                                 /* mark the block as analysed */
1982
1983                                 sd.bptr->flags = BBFINISHED;
1984
1985                                 /* reset variables for dependency checking */
1986
1987                                 coalescing_boundary = sd.new;
1988                                 for( i = 0; i < cd->maxlocals; i++)
1989                                         last_store_boundary[i] = sd.new;
1990
1991                                 /* remember the start of this block's variables */
1992   
1993                                 sd.bptr->varstart = sd.vartop;
1994
1995 #if defined(STACK_VERBOSE)
1996                                 stack_verbose_block_enter(&sd, false);
1997 #endif
1998   
1999                                 /* reach exception handlers for this block */
2000
2001                                 if (!stack_reach_handlers(&sd))
2002                                         return false;
2003
2004                                 /* iterate over ICMDs ****************************************/
2005
2006                                 while (--len >= 0)  {
2007
2008 #if defined(STACK_VERBOSE)
2009                                         new_show_icmd(jd, iptr, false, SHOW_PARSE); printf("\n");
2010                                         for( copy = curstack; copy; copy = copy->prev ) {
2011                                                 printf("%2d(%d", copy->varnum, copy->type);
2012                                                 if (IS_OUTVAR(copy))
2013                                                         printf("S");
2014                                                 if (IS_PREALLOC(copy))
2015                                                         printf("A");
2016                                                 printf(") ");
2017                                         }
2018                                         printf("\n");
2019 #endif
2020
2021                                         /* fetch the current opcode */
2022
2023                                         opcode = iptr->opc;
2024
2025                                         /* automatically replace some ICMDs with builtins */
2026
2027 #if defined(USEBUILTINTABLE)
2028                                         IF_NO_INTRP(
2029                                                 bte = builtintable_get_automatic(opcode);
2030
2031                                                 if (bte && bte->opcode == opcode) {
2032                                                         iptr->opc           = ICMD_BUILTIN;
2033                                                         iptr->flags.bits    = 0;
2034                                                         iptr->sx.s23.s3.bte = bte;
2035                                                         /* iptr->line is already set */
2036                                                         jd->isleafmethod = false;
2037                                                         goto icmd_BUILTIN;
2038                                                 }
2039                                         );
2040 #endif /* defined(USEBUILTINTABLE) */
2041
2042                                         /* main opcode switch *************************************/
2043
2044                                         switch (opcode) {
2045
2046                                                 /* pop 0 push 0 */
2047
2048                                         case ICMD_NOP:
2049 icmd_NOP:
2050                                                 CLR_SX;
2051                                                 OP0_0;
2052                                                 break;
2053
2054                                         case ICMD_CHECKNULL:
2055                                                 coalescing_boundary = sd.new;
2056                                                 COUNT(count_check_null);
2057                                                 USE_S1(TYPE_ADR);
2058                                                 CLR_SX;
2059                                                 CLR_DST; /* XXX live through? */
2060                                                 break;
2061
2062                                         case ICMD_RET:
2063                                                 j = iptr->s1.varindex = 
2064                                                         jd->local_map[iptr->s1.varindex * 5 + TYPE_ADR];
2065
2066                                                 if (sd.var[j].type != TYPE_RET) {
2067                                                         exceptions_throw_verifyerror(m, "RET with non-returnAddress value");
2068                                                         return false;
2069                                                 }
2070                 
2071                                                 CLR_SX;
2072
2073                                                 iptr->dst.block = stack_mark_reached(&sd, sd.var[j].vv.retaddr, curstack, stackdepth);
2074 #if 0
2075                                                 IF_NO_INTRP( rd->locals[iptr->s1.localindex/*XXX invalid here*/][TYPE_ADR].type = TYPE_ADR; );
2076 #endif
2077                                                 superblockend = true;
2078                                                 break;
2079
2080                                         case ICMD_RETURN:
2081                                                 COUNT(count_pcmd_return);
2082                                                 CLR_SX;
2083                                                 OP0_0;
2084                                                 superblockend = true;
2085                                                 break;
2086
2087
2088                                                 /* pop 0 push 1 const */
2089
2090         /************************** ICONST OPTIMIZATIONS **************************/
2091
2092                                         case ICMD_ICONST:
2093                                                 COUNT(count_pcmd_load);
2094                                                 if (len == 0)
2095                                                         goto normal_ICONST;
2096
2097                                                 switch (iptr[1].opc) {
2098                                                         case ICMD_IADD:
2099                                                                 iptr->opc = ICMD_IADDCONST;
2100                                                                 /* FALLTHROUGH */
2101
2102                                                         icmd_iconst_tail:
2103                                                                 iptr[1].opc = ICMD_NOP;
2104                                                                 OP1_1(TYPE_INT, TYPE_INT);
2105                                                                 COUNT(count_pcmd_op);
2106                                                                 break;
2107
2108                                                         case ICMD_ISUB:
2109                                                                 iptr->opc = ICMD_ISUBCONST;
2110                                                                 goto icmd_iconst_tail;
2111 #if SUPPORT_CONST_MUL
2112                                                         case ICMD_IMUL:
2113                                                                 iptr->opc = ICMD_IMULCONST;
2114                                                                 goto icmd_iconst_tail;
2115 #else /* SUPPORT_CONST_MUL */
2116                                                         case ICMD_IMUL:
2117                                                                 if (iptr->sx.val.i == 0x00000002)
2118                                                                         iptr->sx.val.i = 1;
2119                                                                 else if (iptr->sx.val.i == 0x00000004)
2120                                                                         iptr->sx.val.i = 2;
2121                                                                 else if (iptr->sx.val.i == 0x00000008)
2122                                                                         iptr->sx.val.i = 3;
2123                                                                 else if (iptr->sx.val.i == 0x00000010)
2124                                                                         iptr->sx.val.i = 4;
2125                                                                 else if (iptr->sx.val.i == 0x00000020)
2126                                                                         iptr->sx.val.i = 5;
2127                                                                 else if (iptr->sx.val.i == 0x00000040)
2128                                                                         iptr->sx.val.i = 6;
2129                                                                 else if (iptr->sx.val.i == 0x00000080)
2130                                                                         iptr->sx.val.i = 7;
2131                                                                 else if (iptr->sx.val.i == 0x00000100)
2132                                                                         iptr->sx.val.i = 8;
2133                                                                 else if (iptr->sx.val.i == 0x00000200)
2134                                                                         iptr->sx.val.i = 9;
2135                                                                 else if (iptr->sx.val.i == 0x00000400)
2136                                                                         iptr->sx.val.i = 10;
2137                                                                 else if (iptr->sx.val.i == 0x00000800)
2138                                                                         iptr->sx.val.i = 11;
2139                                                                 else if (iptr->sx.val.i == 0x00001000)
2140                                                                         iptr->sx.val.i = 12;
2141                                                                 else if (iptr->sx.val.i == 0x00002000)
2142                                                                         iptr->sx.val.i = 13;
2143                                                                 else if (iptr->sx.val.i == 0x00004000)
2144                                                                         iptr->sx.val.i = 14;
2145                                                                 else if (iptr->sx.val.i == 0x00008000)
2146                                                                         iptr->sx.val.i = 15;
2147                                                                 else if (iptr->sx.val.i == 0x00010000)
2148                                                                         iptr->sx.val.i = 16;
2149                                                                 else if (iptr->sx.val.i == 0x00020000)
2150                                                                         iptr->sx.val.i = 17;
2151                                                                 else if (iptr->sx.val.i == 0x00040000)
2152                                                                         iptr->sx.val.i = 18;
2153                                                                 else if (iptr->sx.val.i == 0x00080000)
2154                                                                         iptr->sx.val.i = 19;
2155                                                                 else if (iptr->sx.val.i == 0x00100000)
2156                                                                         iptr->sx.val.i = 20;
2157                                                                 else if (iptr->sx.val.i == 0x00200000)
2158                                                                         iptr->sx.val.i = 21;
2159                                                                 else if (iptr->sx.val.i == 0x00400000)
2160                                                                         iptr->sx.val.i = 22;
2161                                                                 else if (iptr->sx.val.i == 0x00800000)
2162                                                                         iptr->sx.val.i = 23;
2163                                                                 else if (iptr->sx.val.i == 0x01000000)
2164                                                                         iptr->sx.val.i = 24;
2165                                                                 else if (iptr->sx.val.i == 0x02000000)
2166                                                                         iptr->sx.val.i = 25;
2167                                                                 else if (iptr->sx.val.i == 0x04000000)
2168                                                                         iptr->sx.val.i = 26;
2169                                                                 else if (iptr->sx.val.i == 0x08000000)
2170                                                                         iptr->sx.val.i = 27;
2171                                                                 else if (iptr->sx.val.i == 0x10000000)
2172                                                                         iptr->sx.val.i = 28;
2173                                                                 else if (iptr->sx.val.i == 0x20000000)
2174                                                                         iptr->sx.val.i = 29;
2175                                                                 else if (iptr->sx.val.i == 0x40000000)
2176                                                                         iptr->sx.val.i = 30;
2177                                                                 else if (iptr->sx.val.i == 0x80000000)
2178                                                                         iptr->sx.val.i = 31;
2179                                                                 else
2180                                                                         goto normal_ICONST;
2181
2182                                                                 iptr->opc = ICMD_IMULPOW2;
2183                                                                 goto icmd_iconst_tail;
2184 #endif /* SUPPORT_CONST_MUL */
2185                                                         case ICMD_IDIV:
2186                                                                 if (iptr->sx.val.i == 0x00000002)
2187                                                                         iptr->sx.val.i = 1;
2188                                                                 else if (iptr->sx.val.i == 0x00000004)
2189                                                                         iptr->sx.val.i = 2;
2190                                                                 else if (iptr->sx.val.i == 0x00000008)
2191                                                                         iptr->sx.val.i = 3;
2192                                                                 else if (iptr->sx.val.i == 0x00000010)
2193                                                                         iptr->sx.val.i = 4;
2194                                                                 else if (iptr->sx.val.i == 0x00000020)
2195                                                                         iptr->sx.val.i = 5;
2196                                                                 else if (iptr->sx.val.i == 0x00000040)
2197                                                                         iptr->sx.val.i = 6;
2198                                                                 else if (iptr->sx.val.i == 0x00000080)
2199                                                                         iptr->sx.val.i = 7;
2200                                                                 else if (iptr->sx.val.i == 0x00000100)
2201                                                                         iptr->sx.val.i = 8;
2202                                                                 else if (iptr->sx.val.i == 0x00000200)
2203                                                                         iptr->sx.val.i = 9;
2204                                                                 else if (iptr->sx.val.i == 0x00000400)
2205                                                                         iptr->sx.val.i = 10;
2206                                                                 else if (iptr->sx.val.i == 0x00000800)
2207                                                                         iptr->sx.val.i = 11;
2208                                                                 else if (iptr->sx.val.i == 0x00001000)
2209                                                                         iptr->sx.val.i = 12;
2210                                                                 else if (iptr->sx.val.i == 0x00002000)
2211                                                                         iptr->sx.val.i = 13;
2212                                                                 else if (iptr->sx.val.i == 0x00004000)
2213                                                                         iptr->sx.val.i = 14;
2214                                                                 else if (iptr->sx.val.i == 0x00008000)
2215                                                                         iptr->sx.val.i = 15;
2216                                                                 else if (iptr->sx.val.i == 0x00010000)
2217                                                                         iptr->sx.val.i = 16;
2218                                                                 else if (iptr->sx.val.i == 0x00020000)
2219                                                                         iptr->sx.val.i = 17;
2220                                                                 else if (iptr->sx.val.i == 0x00040000)
2221                                                                         iptr->sx.val.i = 18;
2222                                                                 else if (iptr->sx.val.i == 0x00080000)
2223                                                                         iptr->sx.val.i = 19;
2224                                                                 else if (iptr->sx.val.i == 0x00100000)
2225                                                                         iptr->sx.val.i = 20;
2226                                                                 else if (iptr->sx.val.i == 0x00200000)
2227                                                                         iptr->sx.val.i = 21;
2228                                                                 else if (iptr->sx.val.i == 0x00400000)
2229                                                                         iptr->sx.val.i = 22;
2230                                                                 else if (iptr->sx.val.i == 0x00800000)
2231                                                                         iptr->sx.val.i = 23;
2232                                                                 else if (iptr->sx.val.i == 0x01000000)
2233                                                                         iptr->sx.val.i = 24;
2234                                                                 else if (iptr->sx.val.i == 0x02000000)
2235                                                                         iptr->sx.val.i = 25;
2236                                                                 else if (iptr->sx.val.i == 0x04000000)
2237                                                                         iptr->sx.val.i = 26;
2238                                                                 else if (iptr->sx.val.i == 0x08000000)
2239                                                                         iptr->sx.val.i = 27;
2240                                                                 else if (iptr->sx.val.i == 0x10000000)
2241                                                                         iptr->sx.val.i = 28;
2242                                                                 else if (iptr->sx.val.i == 0x20000000)
2243                                                                         iptr->sx.val.i = 29;
2244                                                                 else if (iptr->sx.val.i == 0x40000000)
2245                                                                         iptr->sx.val.i = 30;
2246                                                                 else if (iptr->sx.val.i == 0x80000000)
2247                                                                         iptr->sx.val.i = 31;
2248                                                                 else
2249                                                                         goto normal_ICONST;
2250
2251                                                                 iptr->opc = ICMD_IDIVPOW2;
2252                                                                 goto icmd_iconst_tail;
2253
2254                                                         case ICMD_IREM:
2255                                                                 /*log_text("stack.c: ICMD_ICONST/ICMD_IREM");*/
2256                                                                 if ((iptr->sx.val.i == 0x00000002) ||
2257                                                                         (iptr->sx.val.i == 0x00000004) ||
2258                                                                         (iptr->sx.val.i == 0x00000008) ||
2259                                                                         (iptr->sx.val.i == 0x00000010) ||
2260                                                                         (iptr->sx.val.i == 0x00000020) ||
2261                                                                         (iptr->sx.val.i == 0x00000040) ||
2262                                                                         (iptr->sx.val.i == 0x00000080) ||
2263                                                                         (iptr->sx.val.i == 0x00000100) ||
2264                                                                         (iptr->sx.val.i == 0x00000200) ||
2265                                                                         (iptr->sx.val.i == 0x00000400) ||
2266                                                                         (iptr->sx.val.i == 0x00000800) ||
2267                                                                         (iptr->sx.val.i == 0x00001000) ||
2268                                                                         (iptr->sx.val.i == 0x00002000) ||
2269                                                                         (iptr->sx.val.i == 0x00004000) ||
2270                                                                         (iptr->sx.val.i == 0x00008000) ||
2271                                                                         (iptr->sx.val.i == 0x00010000) ||
2272                                                                         (iptr->sx.val.i == 0x00020000) ||
2273                                                                         (iptr->sx.val.i == 0x00040000) ||
2274                                                                         (iptr->sx.val.i == 0x00080000) ||
2275                                                                         (iptr->sx.val.i == 0x00100000) ||
2276                                                                         (iptr->sx.val.i == 0x00200000) ||
2277                                                                         (iptr->sx.val.i == 0x00400000) ||
2278                                                                         (iptr->sx.val.i == 0x00800000) ||
2279                                                                         (iptr->sx.val.i == 0x01000000) ||
2280                                                                         (iptr->sx.val.i == 0x02000000) ||
2281                                                                         (iptr->sx.val.i == 0x04000000) ||
2282                                                                         (iptr->sx.val.i == 0x08000000) ||
2283                                                                         (iptr->sx.val.i == 0x10000000) ||
2284                                                                         (iptr->sx.val.i == 0x20000000) ||
2285                                                                         (iptr->sx.val.i == 0x40000000) ||
2286                                                                         (iptr->sx.val.i == 0x80000000))
2287                                                                 {
2288                                                                         iptr->opc = ICMD_IREMPOW2;
2289                                                                         iptr->sx.val.i -= 1;
2290                                                                         goto icmd_iconst_tail;
2291                                                                 }
2292                                                                 goto normal_ICONST;
2293 #if SUPPORT_CONST_LOGICAL
2294                                                         case ICMD_IAND:
2295                                                                 iptr->opc = ICMD_IANDCONST;
2296                                                                 goto icmd_iconst_tail;
2297
2298                                                         case ICMD_IOR:
2299                                                                 iptr->opc = ICMD_IORCONST;
2300                                                                 goto icmd_iconst_tail;
2301
2302                                                         case ICMD_IXOR:
2303                                                                 iptr->opc = ICMD_IXORCONST;
2304                                                                 goto icmd_iconst_tail;
2305
2306 #endif /* SUPPORT_CONST_LOGICAL */
2307                                                         case ICMD_ISHL:
2308                                                                 iptr->opc = ICMD_ISHLCONST;
2309                                                                 goto icmd_iconst_tail;
2310
2311                                                         case ICMD_ISHR:
2312                                                                 iptr->opc = ICMD_ISHRCONST;
2313                                                                 goto icmd_iconst_tail;
2314
2315                                                         case ICMD_IUSHR:
2316                                                                 iptr->opc = ICMD_IUSHRCONST;
2317                                                                 goto icmd_iconst_tail;
2318 #if SUPPORT_LONG_SHIFT
2319                                                         case ICMD_LSHL:
2320                                                                 iptr->opc = ICMD_LSHLCONST;
2321                                                                 goto icmd_lconst_tail;
2322
2323                                                         case ICMD_LSHR:
2324                                                                 iptr->opc = ICMD_LSHRCONST;
2325                                                                 goto icmd_lconst_tail;
2326
2327                                                         case ICMD_LUSHR:
2328                                                                 iptr->opc = ICMD_LUSHRCONST;
2329                                                                 goto icmd_lconst_tail;
2330 #endif /* SUPPORT_LONG_SHIFT */
2331                                                         case ICMD_IF_ICMPEQ:
2332                                                                 iptr[1].opc = ICMD_IFEQ;
2333                                                                 /* FALLTHROUGH */
2334
2335                                                         icmd_if_icmp_tail:
2336                                                                 /* set the constant for the following icmd */
2337                                                                 iptr[1].sx.val.i = iptr->sx.val.i;
2338
2339                                                                 /* this instruction becomes a nop */
2340                                                                 iptr->opc = ICMD_NOP;
2341                                                                 goto icmd_NOP;
2342
2343                                                         case ICMD_IF_ICMPLT:
2344                                                                 iptr[1].opc = ICMD_IFLT;
2345                                                                 goto icmd_if_icmp_tail;
2346
2347                                                         case ICMD_IF_ICMPLE:
2348                                                                 iptr[1].opc = ICMD_IFLE;
2349                                                                 goto icmd_if_icmp_tail;
2350
2351                                                         case ICMD_IF_ICMPNE:
2352                                                                 iptr[1].opc = ICMD_IFNE;
2353                                                                 goto icmd_if_icmp_tail;
2354
2355                                                         case ICMD_IF_ICMPGT:
2356                                                                 iptr[1].opc = ICMD_IFGT;
2357                                                                 goto icmd_if_icmp_tail;
2358
2359                                                         case ICMD_IF_ICMPGE:
2360                                                                 iptr[1].opc = ICMD_IFGE;
2361                                                                 goto icmd_if_icmp_tail;
2362
2363 #if SUPPORT_CONST_STORE
2364                                                         case ICMD_IASTORE:
2365                                                         case ICMD_BASTORE:
2366                                                         case ICMD_CASTORE:
2367                                                         case ICMD_SASTORE:
2368                                                                 IF_INTRP( goto normal_ICONST; )
2369 # if SUPPORT_CONST_STORE_ZERO_ONLY
2370                                                                 if (iptr->sx.val.i != 0)
2371                                                                         goto normal_ICONST;
2372 # endif
2373                                                                 switch (iptr[1].opc) {
2374                                                                         case ICMD_IASTORE:
2375                                                                                 iptr->opc = ICMD_IASTORECONST;
2376                                                                                 iptr->flags.bits |= INS_FLAG_CHECK;
2377                                                                                 break;
2378                                                                         case ICMD_BASTORE:
2379                                                                                 iptr->opc = ICMD_BASTORECONST;
2380                                                                                 iptr->flags.bits |= INS_FLAG_CHECK;
2381                                                                                 break;
2382                                                                         case ICMD_CASTORE:
2383                                                                                 iptr->opc = ICMD_CASTORECONST;
2384                                                                                 iptr->flags.bits |= INS_FLAG_CHECK;
2385                                                                                 break;
2386                                                                         case ICMD_SASTORE:
2387                                                                                 iptr->opc = ICMD_SASTORECONST;
2388                                                                                 iptr->flags.bits |= INS_FLAG_CHECK;
2389                                                                                 break;
2390                                                                 }
2391
2392                                                                 iptr[1].opc = ICMD_NOP;
2393
2394                                                                 /* copy the constant to s3 */
2395                                                                 /* XXX constval -> astoreconstval? */
2396                                                                 iptr->sx.s23.s3.constval = iptr->sx.val.i;
2397                                                                 OP2_0(TYPE_ADR, TYPE_INT);
2398                                                                 COUNT(count_pcmd_op);
2399                                                                 break;
2400
2401                                                         case ICMD_PUTSTATIC:
2402                                                         case ICMD_PUTFIELD:
2403                                                                 IF_INTRP( goto normal_ICONST; )
2404 # if SUPPORT_CONST_STORE_ZERO_ONLY
2405                                                                 if (iptr->sx.val.i != 0)
2406                                                                         goto normal_ICONST;
2407 # endif
2408                                                                 /* XXX check field type? */
2409
2410                                                                 /* copy the constant to s2 */
2411                                                                 /* XXX constval -> fieldconstval? */
2412                                                                 iptr->sx.s23.s2.constval = iptr->sx.val.i;
2413
2414 putconst_tail:
2415                                                                 /* set the field reference (s3) */
2416                                                                 if (iptr[1].flags.bits & INS_FLAG_UNRESOLVED) {
2417                                                                         iptr->sx.s23.s3.uf = iptr[1].sx.s23.s3.uf;
2418                                                                         iptr->flags.bits |= INS_FLAG_UNRESOLVED;
2419                                                                 }
2420                                                                 else {
2421                                                                         iptr->sx.s23.s3.fmiref = iptr[1].sx.s23.s3.fmiref;
2422                                                                 }
2423                                                                 
2424                                                                 switch (iptr[1].opc) {
2425                                                                         case ICMD_PUTSTATIC:
2426                                                                                 iptr->opc = ICMD_PUTSTATICCONST;
2427                                                                                 OP0_0;
2428                                                                                 break;
2429                                                                         case ICMD_PUTFIELD:
2430                                                                                 iptr->opc = ICMD_PUTFIELDCONST;
2431                                                                                 OP1_0(TYPE_ADR);
2432                                                                                 break;
2433                                                                 }
2434
2435                                                                 iptr[1].opc = ICMD_NOP;
2436                                                                 COUNT(count_pcmd_op);
2437                                                                 break;
2438 #endif /* SUPPORT_CONST_STORE */
2439
2440                                                         default:
2441                                                                 goto normal_ICONST;
2442                                                 }
2443
2444                                                 /* if we get here, the ICONST has been optimized */
2445                                                 break;
2446
2447 normal_ICONST:
2448                                                 /* normal case of an unoptimized ICONST */
2449                                                 OP0_1(TYPE_INT);
2450                                                 break;
2451
2452         /************************** LCONST OPTIMIZATIONS **************************/
2453
2454                                         case ICMD_LCONST:
2455                                                 COUNT(count_pcmd_load);
2456                                                 if (len == 0)
2457                                                         goto normal_LCONST;
2458
2459                                                 /* switch depending on the following instruction */
2460
2461                                                 switch (iptr[1].opc) {
2462 #if SUPPORT_LONG_ADD
2463                                                         case ICMD_LADD:
2464                                                                 iptr->opc = ICMD_LADDCONST;
2465                                                                 /* FALLTHROUGH */
2466
2467                                                         icmd_lconst_tail:
2468                                                                 /* instruction of type LONG -> LONG */
2469                                                                 iptr[1].opc = ICMD_NOP;
2470                                                                 OP1_1(TYPE_LNG, TYPE_LNG);
2471                                                                 COUNT(count_pcmd_op);
2472                                                                 break;
2473
2474                                                         case ICMD_LSUB:
2475                                                                 iptr->opc = ICMD_LSUBCONST;
2476                                                                 goto icmd_lconst_tail;
2477
2478 #endif /* SUPPORT_LONG_ADD */
2479 #if SUPPORT_LONG_MUL && SUPPORT_CONST_MUL
2480                                                         case ICMD_LMUL:
2481                                                                 iptr->opc = ICMD_LMULCONST;
2482                                                                 goto icmd_lconst_tail;
2483 #else /* SUPPORT_LONG_MUL && SUPPORT_CONST_MUL */
2484 # if SUPPORT_LONG_SHIFT
2485                                                         case ICMD_LMUL:
2486                                                                 if (iptr->sx.val.l == 0x00000002)
2487                                                                         iptr->sx.val.i = 1;
2488                                                                 else if (iptr->sx.val.l == 0x00000004)
2489                                                                         iptr->sx.val.i = 2;
2490                                                                 else if (iptr->sx.val.l == 0x00000008)
2491                                                                         iptr->sx.val.i = 3;
2492                                                                 else if (iptr->sx.val.l == 0x00000010)
2493                                                                         iptr->sx.val.i = 4;
2494                                                                 else if (iptr->sx.val.l == 0x00000020)
2495                                                                         iptr->sx.val.i = 5;
2496                                                                 else if (iptr->sx.val.l == 0x00000040)
2497                                                                         iptr->sx.val.i = 6;
2498                                                                 else if (iptr->sx.val.l == 0x00000080)
2499                                                                         iptr->sx.val.i = 7;
2500                                                                 else if (iptr->sx.val.l == 0x00000100)
2501                                                                         iptr->sx.val.i = 8;
2502                                                                 else if (iptr->sx.val.l == 0x00000200)
2503                                                                         iptr->sx.val.i = 9;
2504                                                                 else if (iptr->sx.val.l == 0x00000400)
2505                                                                         iptr->sx.val.i = 10;
2506                                                                 else if (iptr->sx.val.l == 0x00000800)
2507                                                                         iptr->sx.val.i = 11;
2508                                                                 else if (iptr->sx.val.l == 0x00001000)
2509                                                                         iptr->sx.val.i = 12;
2510                                                                 else if (iptr->sx.val.l == 0x00002000)
2511                                                                         iptr->sx.val.i = 13;
2512                                                                 else if (iptr->sx.val.l == 0x00004000)
2513                                                                         iptr->sx.val.i = 14;
2514                                                                 else if (iptr->sx.val.l == 0x00008000)
2515                                                                         iptr->sx.val.i = 15;
2516                                                                 else if (iptr->sx.val.l == 0x00010000)
2517                                                                         iptr->sx.val.i = 16;
2518                                                                 else if (iptr->sx.val.l == 0x00020000)
2519                                                                         iptr->sx.val.i = 17;
2520                                                                 else if (iptr->sx.val.l == 0x00040000)
2521                                                                         iptr->sx.val.i = 18;
2522                                                                 else if (iptr->sx.val.l == 0x00080000)
2523                                                                         iptr->sx.val.i = 19;
2524                                                                 else if (iptr->sx.val.l == 0x00100000)
2525                                                                         iptr->sx.val.i = 20;
2526                                                                 else if (iptr->sx.val.l == 0x00200000)
2527                                                                         iptr->sx.val.i = 21;
2528                                                                 else if (iptr->sx.val.l == 0x00400000)
2529                                                                         iptr->sx.val.i = 22;
2530                                                                 else if (iptr->sx.val.l == 0x00800000)
2531                                                                         iptr->sx.val.i = 23;
2532                                                                 else if (iptr->sx.val.l == 0x01000000)
2533                                                                         iptr->sx.val.i = 24;
2534                                                                 else if (iptr->sx.val.l == 0x02000000)
2535                                                                         iptr->sx.val.i = 25;
2536                                                                 else if (iptr->sx.val.l == 0x04000000)
2537                                                                         iptr->sx.val.i = 26;
2538                                                                 else if (iptr->sx.val.l == 0x08000000)
2539                                                                         iptr->sx.val.i = 27;
2540                                                                 else if (iptr->sx.val.l == 0x10000000)
2541                                                                         iptr->sx.val.i = 28;
2542                                                                 else if (iptr->sx.val.l == 0x20000000)
2543                                                                         iptr->sx.val.i = 29;
2544                                                                 else if (iptr->sx.val.l == 0x40000000)
2545                                                                         iptr->sx.val.i = 30;
2546                                                                 else if (iptr->sx.val.l == 0x80000000)
2547                                                                         iptr->sx.val.i = 31;
2548                                                                 else {
2549                                                                         goto normal_LCONST;
2550                                                                 }
2551                                                                 iptr->opc = ICMD_LMULPOW2;
2552                                                                 goto icmd_lconst_tail;
2553 # endif /* SUPPORT_LONG_SHIFT */
2554 #endif /* SUPPORT_LONG_MUL && SUPPORT_CONST_MUL */
2555 #if SUPPORT_LONG_DIV_POW2
2556                                                         case ICMD_LDIV:
2557                                                                 if (iptr->sx.val.l == 0x00000002)
2558                                                                         iptr->sx.val.i = 1;
2559                                                                 else if (iptr->sx.val.l == 0x00000004)
2560                                                                         iptr->sx.val.i = 2;
2561                                                                 else if (iptr->sx.val.l == 0x00000008)
2562                                                                         iptr->sx.val.i = 3;
2563                                                                 else if (iptr->sx.val.l == 0x00000010)
2564                                                                         iptr->sx.val.i = 4;
2565                                                                 else if (iptr->sx.val.l == 0x00000020)
2566                                                                         iptr->sx.val.i = 5;
2567                                                                 else if (iptr->sx.val.l == 0x00000040)
2568                                                                         iptr->sx.val.i = 6;
2569                                                                 else if (iptr->sx.val.l == 0x00000080)
2570                                                                         iptr->sx.val.i = 7;
2571                                                                 else if (iptr->sx.val.l == 0x00000100)
2572                                                                         iptr->sx.val.i = 8;
2573                                                                 else if (iptr->sx.val.l == 0x00000200)
2574                                                                         iptr->sx.val.i = 9;
2575                                                                 else if (iptr->sx.val.l == 0x00000400)
2576                                                                         iptr->sx.val.i = 10;
2577                                                                 else if (iptr->sx.val.l == 0x00000800)
2578                                                                         iptr->sx.val.i = 11;
2579                                                                 else if (iptr->sx.val.l == 0x00001000)
2580                                                                         iptr->sx.val.i = 12;
2581                                                                 else if (iptr->sx.val.l == 0x00002000)
2582                                                                         iptr->sx.val.i = 13;
2583                                                                 else if (iptr->sx.val.l == 0x00004000)
2584                                                                         iptr->sx.val.i = 14;
2585                                                                 else if (iptr->sx.val.l == 0x00008000)
2586                                                                         iptr->sx.val.i = 15;
2587                                                                 else if (iptr->sx.val.l == 0x00010000)
2588                                                                         iptr->sx.val.i = 16;
2589                                                                 else if (iptr->sx.val.l == 0x00020000)
2590                                                                         iptr->sx.val.i = 17;
2591                                                                 else if (iptr->sx.val.l == 0x00040000)
2592                                                                         iptr->sx.val.i = 18;
2593                                                                 else if (iptr->sx.val.l == 0x00080000)
2594                                                                         iptr->sx.val.i = 19;
2595                                                                 else if (iptr->sx.val.l == 0x00100000)
2596                                                                         iptr->sx.val.i = 20;
2597                                                                 else if (iptr->sx.val.l == 0x00200000)
2598                                                                         iptr->sx.val.i = 21;
2599                                                                 else if (iptr->sx.val.l == 0x00400000)
2600                                                                         iptr->sx.val.i = 22;
2601                                                                 else if (iptr->sx.val.l == 0x00800000)
2602                                                                         iptr->sx.val.i = 23;
2603                                                                 else if (iptr->sx.val.l == 0x01000000)
2604                                                                         iptr->sx.val.i = 24;
2605                                                                 else if (iptr->sx.val.l == 0x02000000)
2606                                                                         iptr->sx.val.i = 25;
2607                                                                 else if (iptr->sx.val.l == 0x04000000)
2608                                                                         iptr->sx.val.i = 26;
2609                                                                 else if (iptr->sx.val.l == 0x08000000)
2610                                                                         iptr->sx.val.i = 27;
2611                                                                 else if (iptr->sx.val.l == 0x10000000)
2612                                                                         iptr->sx.val.i = 28;
2613                                                                 else if (iptr->sx.val.l == 0x20000000)
2614                                                                         iptr->sx.val.i = 29;
2615                                                                 else if (iptr->sx.val.l == 0x40000000)
2616                                                                         iptr->sx.val.i = 30;
2617                                                                 else if (iptr->sx.val.l == 0x80000000)
2618                                                                         iptr->sx.val.i = 31;
2619                                                                 else {
2620                                                                         goto normal_LCONST;
2621                                                                 }
2622                                                                 iptr->opc = ICMD_LDIVPOW2;
2623                                                                 goto icmd_lconst_tail;
2624 #endif /* SUPPORT_LONG_DIV_POW2 */
2625
2626 #if SUPPORT_LONG_REM_POW2
2627                                                         case ICMD_LREM:
2628                                                                 if ((iptr->sx.val.l == 0x00000002) ||
2629                                                                         (iptr->sx.val.l == 0x00000004) ||
2630                                                                         (iptr->sx.val.l == 0x00000008) ||
2631                                                                         (iptr->sx.val.l == 0x00000010) ||
2632                                                                         (iptr->sx.val.l == 0x00000020) ||
2633                                                                         (iptr->sx.val.l == 0x00000040) ||
2634                                                                         (iptr->sx.val.l == 0x00000080) ||
2635                                                                         (iptr->sx.val.l == 0x00000100) ||
2636                                                                         (iptr->sx.val.l == 0x00000200) ||
2637                                                                         (iptr->sx.val.l == 0x00000400) ||
2638                                                                         (iptr->sx.val.l == 0x00000800) ||
2639                                                                         (iptr->sx.val.l == 0x00001000) ||
2640                                                                         (iptr->sx.val.l == 0x00002000) ||
2641                                                                         (iptr->sx.val.l == 0x00004000) ||
2642                                                                         (iptr->sx.val.l == 0x00008000) ||
2643                                                                         (iptr->sx.val.l == 0x00010000) ||
2644                                                                         (iptr->sx.val.l == 0x00020000) ||
2645                                                                         (iptr->sx.val.l == 0x00040000) ||
2646                                                                         (iptr->sx.val.l == 0x00080000) ||
2647                                                                         (iptr->sx.val.l == 0x00100000) ||
2648                                                                         (iptr->sx.val.l == 0x00200000) ||
2649                                                                         (iptr->sx.val.l == 0x00400000) ||
2650                                                                         (iptr->sx.val.l == 0x00800000) ||
2651                                                                         (iptr->sx.val.l == 0x01000000) ||
2652                                                                         (iptr->sx.val.l == 0x02000000) ||
2653                                                                         (iptr->sx.val.l == 0x04000000) ||
2654                                                                         (iptr->sx.val.l == 0x08000000) ||
2655                                                                         (iptr->sx.val.l == 0x10000000) ||
2656                                                                         (iptr->sx.val.l == 0x20000000) ||
2657                                                                         (iptr->sx.val.l == 0x40000000) ||
2658                                                                         (iptr->sx.val.l == 0x80000000))
2659                                                                 {
2660                                                                         iptr->opc = ICMD_LREMPOW2;
2661                                                                         iptr->sx.val.l -= 1;
2662                                                                         goto icmd_lconst_tail;
2663                                                                 }
2664                                                                 goto normal_LCONST;
2665 #endif /* SUPPORT_LONG_REM_POW2 */
2666
2667 #if SUPPORT_LONG_LOGICAL && SUPPORT_CONST_LOGICAL
2668
2669                                                         case ICMD_LAND:
2670                                                                 iptr->opc = ICMD_LANDCONST;
2671                                                                 goto icmd_lconst_tail;
2672
2673                                                         case ICMD_LOR:
2674                                                                 iptr->opc = ICMD_LORCONST;
2675                                                                 goto icmd_lconst_tail;
2676
2677                                                         case ICMD_LXOR:
2678                                                                 iptr->opc = ICMD_LXORCONST;
2679                                                                 goto icmd_lconst_tail;
2680 #endif /* SUPPORT_LONG_LOGICAL && SUPPORT_CONST_LOGICAL */
2681
2682 #if SUPPORT_LONG_CMP_CONST
2683                                                         case ICMD_LCMP:
2684                                                                 if ((len <= 1) || (iptr[2].sx.val.i != 0))
2685                                                                         goto normal_LCONST;
2686
2687                                                                 /* switch on the instruction after LCONST - LCMP */
2688
2689                                                                 switch (iptr[2].opc) {
2690                                                                         case ICMD_IFEQ:
2691                                                                                 iptr->opc = ICMD_IF_LEQ;
2692                                                                                 /* FALLTHROUGH */
2693
2694                                                                         icmd_lconst_lcmp_tail:
2695                                                                                 /* convert LCONST, LCMP, IFXX to IF_LXX */
2696                                                                                 iptr->dst.insindex = iptr[2].dst.insindex;
2697                                                                                 iptr[1].opc = ICMD_NOP;
2698                                                                                 iptr[2].opc = ICMD_NOP;
2699
2700                                                                                 OP1_BRANCH(TYPE_LNG);
2701                                                                                 BRANCH(tbptr, copy);
2702                                                                                 COUNT(count_pcmd_bra);
2703                                                                                 COUNT(count_pcmd_op);
2704                                                                                 break;
2705
2706                                                                         case ICMD_IFNE:
2707                                                                                 iptr->opc = ICMD_IF_LNE;
2708                                                                                 goto icmd_lconst_lcmp_tail;
2709
2710                                                                         case ICMD_IFLT:
2711                                                                                 iptr->opc = ICMD_IF_LLT;
2712                                                                                 goto icmd_lconst_lcmp_tail;
2713
2714                                                                         case ICMD_IFGT:
2715                                                                                 iptr->opc = ICMD_IF_LGT;
2716                                                                                 goto icmd_lconst_lcmp_tail;
2717
2718                                                                         case ICMD_IFLE:
2719                                                                                 iptr->opc = ICMD_IF_LLE;
2720                                                                                 goto icmd_lconst_lcmp_tail;
2721
2722                                                                         case ICMD_IFGE:
2723                                                                                 iptr->opc = ICMD_IF_LGE;
2724                                                                                 goto icmd_lconst_lcmp_tail;
2725
2726                                                                         default:
2727                                                                                 goto normal_LCONST;
2728                                                                 } /* end switch on opcode after LCONST - LCMP */
2729                                                                 break;
2730 #endif /* SUPPORT_LONG_CMP_CONST */
2731
2732 #if SUPPORT_CONST_STORE
2733                                                         case ICMD_LASTORE:
2734                                                                 IF_INTRP( goto normal_LCONST; )
2735 # if SUPPORT_CONST_STORE_ZERO_ONLY
2736                                                                 if (iptr->sx.val.l != 0)
2737                                                                         goto normal_LCONST;
2738 # endif
2739 #if SIZEOF_VOID_P == 4
2740                                                                 /* the constant must fit into a ptrint */
2741                                                                 if (iptr->sx.val.l < -0x80000000L || iptr->sx.val.l >= 0x80000000L)
2742                                                                         goto normal_LCONST;
2743 #endif
2744                                                                 /* move the constant to s3 */
2745                                                                 iptr->sx.s23.s3.constval = iptr->sx.val.l;
2746
2747                                                                 iptr->opc = ICMD_LASTORECONST;
2748                                                                 iptr->flags.bits |= INS_FLAG_CHECK;
2749                                                                 OP2_0(TYPE_ADR, TYPE_INT);
2750
2751                                                                 iptr[1].opc = ICMD_NOP;
2752                                                                 COUNT(count_pcmd_op);
2753                                                                 break;
2754
2755                                                         case ICMD_PUTSTATIC:
2756                                                         case ICMD_PUTFIELD:
2757                                                                 IF_INTRP( goto normal_LCONST; )
2758 # if SUPPORT_CONST_STORE_ZERO_ONLY
2759                                                                 if (iptr->sx.val.l != 0)
2760                                                                         goto normal_LCONST;
2761 # endif
2762 #if SIZEOF_VOID_P == 4
2763                                                                 /* the constant must fit into a ptrint */
2764                                                                 if (iptr->sx.val.l < -0x80000000L || iptr->sx.val.l >= 0x80000000L)
2765                                                                         goto normal_LCONST;
2766 #endif
2767                                                                 /* XXX check field type? */
2768
2769                                                                 /* copy the constant to s2 */
2770                                                                 /* XXX constval -> fieldconstval? */
2771                                                                 iptr->sx.s23.s2.constval = iptr->sx.val.l;
2772
2773                                                                 goto putconst_tail;
2774
2775 #endif /* SUPPORT_CONST_STORE */
2776
2777                                                         default:
2778                                                                 goto normal_LCONST;
2779                                                 } /* end switch opcode after LCONST */
2780
2781                                                 /* if we get here, the LCONST has been optimized */
2782                                                 break;
2783
2784 normal_LCONST:
2785                                                 /* the normal case of an unoptimized LCONST */
2786                                                 OP0_1(TYPE_LNG);
2787                                                 break;
2788
2789         /************************ END OF LCONST OPTIMIZATIONS *********************/
2790
2791                                         case ICMD_FCONST:
2792                                                 COUNT(count_pcmd_load);
2793                                                 OP0_1(TYPE_FLT);
2794                                                 break;
2795
2796                                         case ICMD_DCONST:
2797                                                 COUNT(count_pcmd_load);
2798                                                 OP0_1(TYPE_DBL);
2799                                                 break;
2800
2801         /************************** ACONST OPTIMIZATIONS **************************/
2802
2803                                         case ICMD_ACONST:
2804                                                 coalescing_boundary = sd.new;
2805                                                 COUNT(count_pcmd_load);
2806 #if SUPPORT_CONST_STORE
2807                                                 IF_INTRP( goto normal_ACONST; )
2808
2809                                                 /* We can only optimize if the ACONST is resolved
2810                                                  * and there is an instruction after it. */
2811
2812                                                 if ((len == 0) || (iptr->flags.bits & INS_FLAG_UNRESOLVED))
2813                                                         goto normal_ACONST;
2814
2815                                                 switch (iptr[1].opc) {
2816                                                         case ICMD_AASTORE:
2817                                                                 /* We can only optimize for NULL values
2818                                                                  * here because otherwise a checkcast is
2819                                                                  * required. */
2820                                                                 if (iptr->sx.val.anyptr != NULL)
2821                                                                         goto normal_ACONST;
2822
2823                                                                 /* copy the constant (NULL) to s3 */
2824                                                                 iptr->sx.s23.s3.constval = 0;
2825                                                                 iptr->opc = ICMD_AASTORECONST;
2826                                                                 iptr->flags.bits |= INS_FLAG_CHECK;
2827                                                                 OP2_0(TYPE_ADR, TYPE_INT);
2828
2829                                                                 iptr[1].opc = ICMD_NOP;
2830                                                                 COUNT(count_pcmd_op);
2831                                                                 break;
2832
2833                                                         case ICMD_PUTSTATIC:
2834                                                         case ICMD_PUTFIELD:
2835 # if SUPPORT_CONST_STORE_ZERO_ONLY
2836                                                                 if (iptr->sx.val.anyptr != NULL)
2837                                                                         goto normal_ACONST;
2838 # endif
2839                                                                 /* XXX check field type? */
2840                                                                 /* copy the constant to s2 */
2841                                                                 /* XXX constval -> fieldconstval? */
2842                                                                 iptr->sx.s23.s2.constval = (ptrint) iptr->sx.val.anyptr;
2843
2844                                                                 goto putconst_tail;
2845
2846                                                         default:
2847                                                                 goto normal_ACONST;
2848                                                 }
2849
2850                                                 /* if we get here the ACONST has been optimized */
2851                                                 break;
2852
2853 normal_ACONST:
2854 #endif /* SUPPORT_CONST_STORE */
2855                                                 OP0_1(TYPE_ADR);
2856                                                 break;
2857
2858
2859                                                 /* pop 0 push 1 load */
2860
2861                                         case ICMD_ILOAD:
2862                                         case ICMD_LLOAD:
2863                                         case ICMD_FLOAD:
2864                                         case ICMD_DLOAD:
2865                                         case ICMD_ALOAD:
2866                                                 COUNT(count_load_instruction);
2867                                                 i = opcode - ICMD_ILOAD; /* type */
2868
2869                                                 j = iptr->s1.varindex = 
2870                                                         jd->local_map[iptr->s1.varindex * 5 + i];
2871
2872                                                 if (sd.var[j].type == TYPE_RET) {
2873                                                         exceptions_throw_verifyerror(m, "forbidden load of returnAddress");
2874                                                         return false;
2875                                                 }
2876                 
2877                                                 LOAD(i, j);
2878                                                 break;
2879
2880                                                 /* pop 2 push 1 */
2881
2882                                         case ICMD_LALOAD:
2883                                         case ICMD_FALOAD:
2884                                         case ICMD_DALOAD:
2885                                         case ICMD_AALOAD:
2886                                                 coalescing_boundary = sd.new;
2887                                                 iptr->flags.bits |= INS_FLAG_CHECK;
2888                                                 COUNT(count_check_null);
2889                                                 COUNT(count_check_bound);
2890                                                 COUNT(count_pcmd_mem);
2891                                                 OP2_1(TYPE_ADR, TYPE_INT, opcode - ICMD_IALOAD);
2892                                                 break;
2893
2894                                         case ICMD_IALOAD:
2895                                         case ICMD_BALOAD:
2896                                         case ICMD_CALOAD:
2897                                         case ICMD_SALOAD:
2898                                                 coalescing_boundary = sd.new;
2899                                                 iptr->flags.bits |= INS_FLAG_CHECK;
2900                                                 COUNT(count_check_null);
2901                                                 COUNT(count_check_bound);
2902                                                 COUNT(count_pcmd_mem);
2903                                                 OP2_1(TYPE_ADR, TYPE_INT, TYPE_INT);
2904                                                 break;
2905
2906                                                 /* pop 0 push 0 iinc */
2907
2908                                         case ICMD_IINC:
2909                                                 STATISTICS_STACKDEPTH_DISTRIBUTION(count_store_depth);
2910
2911                                                 last_store_boundary[iptr->s1.varindex] = sd.new;
2912
2913                                                 iptr->s1.varindex = 
2914                                                         jd->local_map[iptr->s1.varindex * 5 + TYPE_INT];
2915
2916                                                 copy = curstack;
2917                                                 i = stackdepth - 1;
2918                                                 while (copy) {
2919                                                         if ((copy->varkind == LOCALVAR) &&
2920                                                                 (copy->varnum == iptr->s1.varindex))
2921                                                         {
2922                                                                 assert(IS_LOCALVAR(copy));
2923                                                                 SET_TEMPVAR(copy);
2924                                                         }
2925                                                         i--;
2926                                                         copy = copy->prev;
2927                                                 }
2928
2929                                                 iptr->dst.varindex = iptr->s1.varindex;
2930                                                 break;
2931
2932                                                 /* pop 1 push 0 store */
2933
2934                                         case ICMD_ISTORE:
2935                                         case ICMD_LSTORE:
2936                                         case ICMD_FSTORE:
2937                                         case ICMD_DSTORE:
2938                                         case ICMD_ASTORE:
2939                                                 REQUIRE(1);
2940
2941                                                 i = opcode - ICMD_ISTORE; /* type */
2942                                                 javaindex = iptr->dst.varindex;
2943                                                 j = iptr->dst.varindex = 
2944                                                         jd->local_map[javaindex * 5 + i];
2945
2946                                                 COPY_VAL_AND_TYPE(sd, curstack->varnum, j);
2947
2948 #if defined(ENABLE_STATISTICS)
2949                                                 if (opt_stat) {
2950                                                         count_pcmd_store++;
2951                                                         i = sd.new - curstack;
2952                                                         if (i >= 20)
2953                                                                 count_store_length[20]++;
2954                                                         else
2955                                                                 count_store_length[i]++;
2956                                                         i = stackdepth - 1;
2957                                                         if (i >= 10)
2958                                                                 count_store_depth[10]++;
2959                                                         else
2960                                                                 count_store_depth[i]++;
2961                                                 }
2962 #endif
2963                                                 /* check for conflicts as described in Figure 5.2 */
2964
2965                                                 copy = curstack->prev;
2966                                                 i = stackdepth - 2;
2967                                                 while (copy) {
2968                                                         if ((copy->varkind == LOCALVAR) &&
2969                                                                 (copy->varnum == j))
2970                                                         {
2971                                                                 copy->varkind = TEMPVAR;
2972                                                                 assert(IS_LOCALVAR(copy));
2973                                                                 SET_TEMPVAR(copy);
2974                                                         }
2975                                                         i--;
2976                                                         copy = copy->prev;
2977                                                 }
2978
2979                                                 /* if the variable is already coalesced, don't bother */
2980
2981                                                 if (IS_OUTVAR(curstack)
2982                                                         || (curstack->varkind == LOCALVAR 
2983                                                                 && curstack->varnum != j))
2984                                                         goto store_tail;
2985
2986                                                 /* there is no STORE Lj while curstack is live */
2987
2988                                                 if (curstack < last_store_boundary[javaindex])
2989                                                         goto assume_conflict;
2990
2991                                                 /* curstack must be after the coalescing boundary */
2992
2993                                                 if (curstack < coalescing_boundary)
2994                                                         goto assume_conflict;
2995
2996                                                 /* there is no DEF LOCALVAR(j) while curstack is live */
2997
2998                                                 copy = sd.new; /* most recent stackslot created + 1 */
2999                                                 while (--copy > curstack) {
3000                                                         if (copy->varkind == LOCALVAR && copy->varnum == j)
3001                                                                 goto assume_conflict;
3002                                                 }
3003
3004                                                 /* coalesce the temporary variable with Lj */
3005                                                 assert((curstack->varkind == TEMPVAR)
3006                                                                         || (curstack->varkind == UNDEFVAR));
3007                                                 assert(!IS_LOCALVAR(curstack));
3008                                                 assert(!IS_OUTVAR(curstack));
3009                                                 assert(!IS_PREALLOC(curstack));
3010
3011                                                 assert(curstack->creator);
3012                                                 assert(curstack->creator->dst.varindex == curstack->varnum);
3013                                                 RELEASE_INDEX(sd, curstack);
3014                                                 curstack->varkind = LOCALVAR;
3015                                                 curstack->varnum = j;
3016                                                 curstack->creator->dst.varindex = j;
3017                                                 goto store_tail;
3018
3019                                                 /* revert the coalescing, if it has been done earlier */
3020 assume_conflict:
3021                                                 if ((curstack->varkind == LOCALVAR)
3022                                                         && (curstack->varnum == j))
3023                                                 {
3024                                                         assert(IS_LOCALVAR(curstack));
3025                                                         SET_TEMPVAR(curstack);
3026                                                 }
3027
3028                                                 /* remember the stack boundary at this store */
3029 store_tail:
3030                                                 last_store_boundary[javaindex] = sd.new;
3031
3032                                                 STORE(opcode - ICMD_ISTORE, j);
3033                                                 break;
3034
3035                                         /* pop 3 push 0 */
3036
3037                                         case ICMD_AASTORE:
3038                                                 coalescing_boundary = sd.new;
3039                                                 iptr->flags.bits |= INS_FLAG_CHECK;
3040                                                 COUNT(count_check_null);
3041                                                 COUNT(count_check_bound);
3042                                                 COUNT(count_pcmd_mem);
3043
3044                                                 bte = builtintable_get_internal(BUILTIN_canstore);
3045                                                 md = bte->md;
3046
3047                                                 if (md->memuse > rd->memuse)
3048                                                         rd->memuse = md->memuse;
3049                                                 if (md->argintreguse > rd->argintreguse)
3050                                                         rd->argintreguse = md->argintreguse;
3051                                                 /* XXX non-leaf method? */
3052
3053                                                 /* make all stack variables saved */
3054
3055                                                 copy = curstack;
3056                                                 while (copy) {
3057                                                         sd.var[copy->varnum].flags |= SAVEDVAR;
3058                                                         /* in case copy->varnum is/will be a LOCALVAR */
3059                                                         /* once and set back to a non LOCALVAR        */
3060                                                         /* the correct SAVEDVAR flag has to be        */
3061                                                         /* remembered in copy->flags, too             */
3062                                                         copy->flags |= SAVEDVAR;
3063                                                         copy = copy->prev;
3064                                                 }
3065
3066                                                 OP3_0(TYPE_ADR, TYPE_INT, TYPE_ADR);
3067                                                 break;
3068
3069
3070                                         case ICMD_LASTORE:
3071                                         case ICMD_FASTORE:
3072                                         case ICMD_DASTORE:
3073                                                 coalescing_boundary = sd.new;
3074                                                 iptr->flags.bits |= INS_FLAG_CHECK;
3075                                                 COUNT(count_check_null);
3076                                                 COUNT(count_check_bound);
3077                                                 COUNT(count_pcmd_mem);
3078                                                 OP3_0(TYPE_ADR, TYPE_INT, opcode - ICMD_IASTORE);
3079                                                 break;
3080
3081                                         case ICMD_IASTORE:
3082                                         case ICMD_BASTORE:
3083                                         case ICMD_CASTORE:
3084                                         case ICMD_SASTORE:
3085                                                 coalescing_boundary = sd.new;
3086                                                 iptr->flags.bits |= INS_FLAG_CHECK;
3087                                                 COUNT(count_check_null);
3088                                                 COUNT(count_check_bound);
3089                                                 COUNT(count_pcmd_mem);
3090                                                 OP3_0(TYPE_ADR, TYPE_INT, TYPE_INT);
3091                                                 break;
3092
3093                                                 /* pop 1 push 0 */
3094
3095                                         case ICMD_POP:
3096 #ifdef ENABLE_VERIFIER
3097                                                 if (opt_verify) {
3098                                                         REQUIRE(1);
3099                                                         if (IS_2_WORD_TYPE(curstack->type))
3100                                                                 goto throw_stack_category_error;
3101                                                 }
3102 #endif
3103                                                 OP1_0_ANY;
3104                                                 break;
3105
3106                                         case ICMD_IRETURN:
3107                                         case ICMD_LRETURN:
3108                                         case ICMD_FRETURN:
3109                                         case ICMD_DRETURN:
3110                                         case ICMD_ARETURN:
3111                                                 coalescing_boundary = sd.new;
3112                                                 IF_JIT( md_return_alloc(jd, curstack); )
3113                                                 COUNT(count_pcmd_return);
3114                                                 OP1_0(opcode - ICMD_IRETURN);
3115                                                 superblockend = true;
3116                                                 break;
3117
3118                                         case ICMD_ATHROW:
3119                                                 coalescing_boundary = sd.new;
3120                                                 COUNT(count_check_null);
3121                                                 OP1_0(TYPE_ADR);
3122                                                 curstack = NULL; stackdepth = 0;
3123                                                 superblockend = true;
3124                                                 break;
3125
3126                                         case ICMD_PUTSTATIC:
3127                                                 coalescing_boundary = sd.new;
3128                                                 COUNT(count_pcmd_mem);
3129                                                 INSTRUCTION_GET_FIELDREF(iptr, fmiref);
3130                                                 OP1_0(fmiref->parseddesc.fd->type);
3131                                                 break;
3132
3133                                                 /* pop 1 push 0 branch */
3134
3135                                         case ICMD_IFNULL:
3136                                         case ICMD_IFNONNULL:
3137                                                 COUNT(count_pcmd_bra);
3138                                                 OP1_BRANCH(TYPE_ADR);
3139                                                 BRANCH(tbptr, copy);
3140                                                 break;
3141
3142                                         case ICMD_IFEQ:
3143                                         case ICMD_IFNE:
3144                                         case ICMD_IFLT:
3145                                         case ICMD_IFGE:
3146                                         case ICMD_IFGT:
3147                                         case ICMD_IFLE:
3148                                                 COUNT(count_pcmd_bra);
3149                                                 /* iptr->sx.val.i is set implicitly in parse by
3150                                                    clearing the memory or from IF_ICMPxx
3151                                                    optimization. */
3152
3153                                                 OP1_BRANCH(TYPE_INT);
3154 /*                                              iptr->sx.val.i = 0; */
3155                                                 BRANCH(tbptr, copy);
3156                                                 break;
3157
3158                                                 /* pop 0 push 0 branch */
3159
3160                                         case ICMD_GOTO:
3161                                                 COUNT(count_pcmd_bra);
3162                                                 OP0_BRANCH;
3163                                                 BRANCH(tbptr, copy);
3164                                                 superblockend = true;
3165                                                 break;
3166
3167                                                 /* pop 1 push 0 table branch */
3168
3169                                         case ICMD_TABLESWITCH:
3170                                                 COUNT(count_pcmd_table);
3171                                                 OP1_BRANCH(TYPE_INT);
3172
3173                                                 table = iptr->dst.table;
3174                                                 BRANCH_TARGET(*table, tbptr, copy);
3175                                                 table++;
3176
3177                                                 i = iptr->sx.s23.s3.tablehigh
3178                                                   - iptr->sx.s23.s2.tablelow + 1;
3179
3180                                                 while (--i >= 0) {
3181                                                         BRANCH_TARGET(*table, tbptr, copy);
3182                                                         table++;
3183                                                 }
3184                                                 superblockend = true;
3185                                                 break;
3186
3187                                                 /* pop 1 push 0 table branch */
3188
3189                                         case ICMD_LOOKUPSWITCH:
3190                                                 COUNT(count_pcmd_table);
3191                                                 OP1_BRANCH(TYPE_INT);
3192
3193                                                 BRANCH_TARGET(iptr->sx.s23.s3.lookupdefault, tbptr, copy);
3194
3195                                                 lookup = iptr->dst.lookup;
3196
3197                                                 i = iptr->sx.s23.s2.lookupcount;
3198
3199                                                 while (--i >= 0) {
3200                                                         BRANCH_TARGET(lookup->target, tbptr, copy);
3201                                                         lookup++;
3202                                                 }
3203                                                 superblockend = true;
3204                                                 break;
3205
3206                                         case ICMD_MONITORENTER:
3207                                         case ICMD_MONITOREXIT:
3208                                                 coalescing_boundary = sd.new;
3209                                                 COUNT(count_check_null);
3210                                                 OP1_0(TYPE_ADR);
3211                                                 break;
3212
3213                                                 /* pop 2 push 0 branch */
3214
3215                                         case ICMD_IF_ICMPEQ:
3216                                         case ICMD_IF_ICMPNE:
3217                                         case ICMD_IF_ICMPLT:
3218                                         case ICMD_IF_ICMPGE:
3219                                         case ICMD_IF_ICMPGT:
3220                                         case ICMD_IF_ICMPLE:
3221                                                 COUNT(count_pcmd_bra);
3222                                                 OP2_BRANCH(TYPE_INT, TYPE_INT);
3223                                                 BRANCH(tbptr, copy);
3224                                                 break;
3225
3226                                         case ICMD_IF_ACMPEQ:
3227                                         case ICMD_IF_ACMPNE:
3228                                                 COUNT(count_pcmd_bra);
3229                                                 OP2_BRANCH(TYPE_ADR, TYPE_ADR);
3230                                                 BRANCH(tbptr, copy);
3231                                                 break;
3232
3233                                                 /* pop 2 push 0 */
3234
3235                                         case ICMD_PUTFIELD:
3236                                                 coalescing_boundary = sd.new;
3237                                                 COUNT(count_check_null);
3238                                                 COUNT(count_pcmd_mem);
3239                                                 INSTRUCTION_GET_FIELDREF(iptr, fmiref);
3240                                                 OP2_0(TYPE_ADR, fmiref->parseddesc.fd->type);
3241                                                 break;
3242
3243                                         case ICMD_POP2:
3244                                                 REQUIRE(1);
3245                                                 if (!IS_2_WORD_TYPE(curstack->type)) {
3246                                                         /* ..., cat1 */
3247 #ifdef ENABLE_VERIFIER
3248                                                         if (opt_verify) {
3249                                                                 REQUIRE(2);
3250                                                                 if (IS_2_WORD_TYPE(curstack->prev->type))
3251                                                                         goto throw_stack_category_error;
3252                                                         }
3253 #endif
3254                                                         OP2_0_ANY_ANY; /* pop two slots */
3255                                                 }
3256                                                 else {
3257                                                         iptr->opc = ICMD_POP;
3258                                                         OP1_0_ANY; /* pop one (two-word) slot */
3259                                                 }
3260                                                 break;
3261
3262                                                 /* pop 0 push 1 dup */
3263
3264                                         case ICMD_DUP:
3265 #ifdef ENABLE_VERIFIER
3266                                                 if (opt_verify) {
3267                                                         REQUIRE(1);
3268                                                         if (IS_2_WORD_TYPE(curstack->type))
3269                                                                 goto throw_stack_category_error;
3270                                                 }
3271 #endif
3272                                                 COUNT(count_dup_instruction);
3273
3274 icmd_DUP:
3275                                                 src1 = curstack;
3276
3277                                                 COPY_UP(src1);
3278                                                 coalescing_boundary = sd.new - 1;
3279                                                 break;
3280
3281                                         case ICMD_DUP2:
3282                                                 REQUIRE(1);
3283                                                 if (IS_2_WORD_TYPE(curstack->type)) {
3284                                                         /* ..., cat2 */
3285                                                         iptr->opc = ICMD_DUP;
3286                                                         goto icmd_DUP;
3287                                                 }
3288                                                 else {
3289                                                         REQUIRE(2);
3290                                                         /* ..., ????, cat1 */
3291 #ifdef ENABLE_VERIFIER
3292                                                         if (opt_verify) {
3293                                                                 if (IS_2_WORD_TYPE(curstack->prev->type))
3294                                                                         goto throw_stack_category_error;
3295                                                         }
3296 #endif
3297                                                         src1 = curstack->prev;
3298                                                         src2 = curstack;
3299
3300                                                         COPY_UP(src1); iptr++; len--;
3301                                                         COPY_UP(src2);
3302
3303                                                         coalescing_boundary = sd.new;
3304                                                 }
3305                                                 break;
3306
3307                                                 /* pop 2 push 3 dup */
3308
3309                                         case ICMD_DUP_X1:
3310 #ifdef ENABLE_VERIFIER
3311                                                 if (opt_verify) {
3312                                                         REQUIRE(2);
3313                                                         if (IS_2_WORD_TYPE(curstack->type) ||
3314                                                                 IS_2_WORD_TYPE(curstack->prev->type))
3315                                                                         goto throw_stack_category_error;
3316                                                 }
3317 #endif
3318
3319 icmd_DUP_X1:
3320                                                 src1 = curstack->prev;
3321                                                 src2 = curstack;
3322                                                 POPANY; POPANY;
3323                                                 stackdepth -= 2;
3324
3325                                                 DUP_SLOT(src2); dst1 = curstack; stackdepth++;
3326
3327                                                 MOVE_UP(src1); iptr++; len--;
3328                                                 MOVE_UP(src2); iptr++; len--;
3329
3330                                                 COPY_DOWN(curstack, dst1);
3331
3332                                                 coalescing_boundary = sd.new;
3333                                                 break;
3334
3335                                         case ICMD_DUP2_X1:
3336                                                 REQUIRE(2);
3337                                                 if (IS_2_WORD_TYPE(curstack->type)) {
3338                                                         /* ..., ????, cat2 */
3339 #ifdef ENABLE_VERIFIER
3340                                                         if (opt_verify) {
3341                                                                 if (IS_2_WORD_TYPE(curstack->prev->type))
3342                                                                         goto throw_stack_category_error;
3343                                                         }
3344 #endif
3345                                                         iptr->opc = ICMD_DUP_X1;
3346                                                         goto icmd_DUP_X1;
3347                                                 }
3348                                                 else {
3349                                                         /* ..., ????, cat1 */
3350 #ifdef ENABLE_VERIFIER
3351                                                         if (opt_verify) {
3352                                                                 REQUIRE(3);
3353                                                                 if (IS_2_WORD_TYPE(curstack->prev->type)
3354                                                                         || IS_2_WORD_TYPE(curstack->prev->prev->type))
3355                                                                                 goto throw_stack_category_error;
3356                                                         }
3357 #endif
3358
3359 icmd_DUP2_X1:
3360                                                         src1 = curstack->prev->prev;
3361                                                         src2 = curstack->prev;
3362                                                         src3 = curstack;
3363                                                         POPANY; POPANY; POPANY;
3364                                                         stackdepth -= 3;
3365
3366                                                         DUP_SLOT(src2); dst1 = curstack; stackdepth++;
3367                                                         DUP_SLOT(src3); dst2 = curstack; stackdepth++;
3368
3369                                                         MOVE_UP(src1); iptr++; len--;
3370                                                         MOVE_UP(src2); iptr++; len--;
3371                                                         MOVE_UP(src3); iptr++; len--;
3372
3373                                                         COPY_DOWN(curstack, dst2); iptr++; len--;
3374                                                         COPY_DOWN(curstack->prev, dst1);
3375
3376                                                         coalescing_boundary = sd.new;
3377                                                 }
3378                                                 break;
3379
3380                                                 /* pop 3 push 4 dup */
3381
3382                                         case ICMD_DUP_X2:
3383                                                 REQUIRE(2);
3384                                                 if (IS_2_WORD_TYPE(curstack->prev->type)) {
3385                                                         /* ..., cat2, ???? */
3386 #ifdef ENABLE_VERIFIER
3387                                                         if (opt_verify) {
3388                                                                 if (IS_2_WORD_TYPE(curstack->type))
3389                                                                         goto throw_stack_category_error;
3390                                                         }
3391 #endif
3392                                                         iptr->opc = ICMD_DUP_X1;
3393                                                         goto icmd_DUP_X1;
3394                                                 }
3395                                                 else {
3396                                                         /* ..., cat1, ???? */
3397 #ifdef ENABLE_VERIFIER
3398                                                         if (opt_verify) {
3399                                                                 REQUIRE(3);
3400                                                                 if (IS_2_WORD_TYPE(curstack->type)
3401                                                                         || IS_2_WORD_TYPE(curstack->prev->prev->type))
3402                                                                                         goto throw_stack_category_error;
3403                                                         }
3404 #endif
3405 icmd_DUP_X2:
3406                                                         src1 = curstack->prev->prev;
3407                                                         src2 = curstack->prev;
3408                                                         src3 = curstack;
3409                                                         POPANY; POPANY; POPANY;
3410                                                         stackdepth -= 3;
3411
3412                                                         DUP_SLOT(src3); dst1 = curstack; stackdepth++;
3413
3414                                                         MOVE_UP(src1); iptr++; len--;
3415                                                         MOVE_UP(src2); iptr++; len--;
3416                                                         MOVE_UP(src3); iptr++; len--;
3417
3418                                                         COPY_DOWN(curstack, dst1);
3419
3420                                                         coalescing_boundary = sd.new;
3421                                                 }
3422                                                 break;
3423
3424                                         case ICMD_DUP2_X2:
3425                                                 REQUIRE(2);
3426                                                 if (IS_2_WORD_TYPE(curstack->type)) {
3427                                                         /* ..., ????, cat2 */
3428                                                         if (IS_2_WORD_TYPE(curstack->prev->type)) {
3429                                                                 /* ..., cat2, cat2 */
3430                                                                 iptr->opc = ICMD_DUP_X1;
3431                                                                 goto icmd_DUP_X1;
3432                                                         }
3433                                                         else {
3434                                                                 /* ..., cat1, cat2 */
3435 #ifdef ENABLE_VERIFIER
3436                                                                 if (opt_verify) {
3437                                                                         REQUIRE(3);
3438                                                                         if (IS_2_WORD_TYPE(curstack->prev->prev->type))
3439                                                                                         goto throw_stack_category_error;
3440                                                                 }
3441 #endif
3442                                                                 iptr->opc = ICMD_DUP_X2;
3443                                                                 goto icmd_DUP_X2;
3444                                                         }
3445                                                 }
3446
3447                                                 REQUIRE(3);
3448                                                 /* ..., ????, ????, cat1 */
3449
3450                                                 if (IS_2_WORD_TYPE(curstack->prev->prev->type)) {
3451                                                         /* ..., cat2, ????, cat1 */
3452 #ifdef ENABLE_VERIFIER
3453                                                         if (opt_verify) {
3454                                                                 if (IS_2_WORD_TYPE(curstack->prev->type))
3455                                                                         goto throw_stack_category_error;
3456                                                         }
3457 #endif
3458                                                         iptr->opc = ICMD_DUP2_X1;
3459                                                         goto icmd_DUP2_X1;
3460                                                 }
3461                                                 else {
3462                                                         /* ..., cat1, ????, cat1 */
3463 #ifdef ENABLE_VERIFIER
3464                                                         if (opt_verify) {
3465                                                                 REQUIRE(4);
3466                                                                 if (IS_2_WORD_TYPE(curstack->prev->type)
3467                                                                         || IS_2_WORD_TYPE(curstack->prev->prev->prev->type))
3468                                                                         goto throw_stack_category_error;
3469                                                         }
3470 #endif
3471
3472                                                         src1 = curstack->prev->prev->prev;
3473                                                         src2 = curstack->prev->prev;
3474                                                         src3 = curstack->prev;
3475                                                         src4 = curstack;
3476                                                         POPANY; POPANY; POPANY; POPANY;
3477                                                         stackdepth -= 4;
3478
3479                                                         DUP_SLOT(src3); dst1 = curstack; stackdepth++;
3480                                                         DUP_SLOT(src4); dst2 = curstack; stackdepth++;
3481
3482                                                         MOVE_UP(src1); iptr++; len--;
3483                                                         MOVE_UP(src2); iptr++; len--;
3484                                                         MOVE_UP(src3); iptr++; len--;
3485                                                         MOVE_UP(src4); iptr++; len--;
3486
3487                                                         COPY_DOWN(curstack, dst2); iptr++; len--;
3488                                                         COPY_DOWN(curstack->prev, dst1);
3489
3490                                                         coalescing_boundary = sd.new;
3491                                                 }
3492                                                 break;
3493
3494                                                 /* pop 2 push 2 swap */
3495
3496                                         case ICMD_SWAP:
3497 #ifdef ENABLE_VERIFIER
3498                                                 if (opt_verify) {
3499                                                         REQUIRE(2);
3500                                                         if (IS_2_WORD_TYPE(curstack->type)
3501                                                                 || IS_2_WORD_TYPE(curstack->prev->type))
3502                                                                 goto throw_stack_category_error;
3503                                                 }
3504 #endif
3505
3506                                                 src1 = curstack->prev;
3507                                                 src2 = curstack;
3508                                                 POPANY; POPANY;
3509                                                 stackdepth -= 2;
3510
3511                                                 MOVE_UP(src2); iptr++; len--;
3512                                                 MOVE_UP(src1);
3513
3514                                                 coalescing_boundary = sd.new;
3515                                                 break;
3516
3517                                                 /* pop 2 push 1 */
3518
3519                                         case ICMD_IDIV:
3520                                         case ICMD_IREM:
3521                                                 coalescing_boundary = sd.new;
3522 #if !SUPPORT_DIVISION
3523                                                 bte = iptr->sx.s23.s3.bte;
3524                                                 md = bte->md;
3525
3526                                                 if (md->memuse > rd->memuse)
3527                                                         rd->memuse = md->memuse;
3528                                                 if (md->argintreguse > rd->argintreguse)
3529                                                         rd->argintreguse = md->argintreguse;
3530
3531                                                 /* make all stack variables saved */
3532
3533                                                 copy = curstack;
3534                                                 while (copy) {
3535                                                         sd.var[copy->varnum].flags |= SAVEDVAR;
3536                                                         copy->flags |= SAVEDVAR;
3537                                                         copy = copy->prev;
3538                                                 }
3539                                                 /* FALLTHROUGH */
3540
3541 #endif /* !SUPPORT_DIVISION */
3542
3543                                         case ICMD_ISHL:
3544                                         case ICMD_ISHR:
3545                                         case ICMD_IUSHR:
3546                                         case ICMD_IADD:
3547                                         case ICMD_ISUB:
3548                                         case ICMD_IMUL:
3549                                         case ICMD_IAND:
3550                                         case ICMD_IOR:
3551                                         case ICMD_IXOR:
3552                                                 COUNT(count_pcmd_op);
3553                                                 OP2_1(TYPE_INT, TYPE_INT, TYPE_INT);
3554                                                 break;
3555
3556                                         case ICMD_LDIV:
3557                                         case ICMD_LREM:
3558                                                 coalescing_boundary = sd.new;
3559 #if !(SUPPORT_DIVISION && SUPPORT_LONG && SUPPORT_LONG_DIV)
3560                                                 bte = iptr->sx.s23.s3.bte;
3561                                                 md = bte->md;
3562
3563                                                 if (md->memuse > rd->memuse)
3564                                                         rd->memuse = md->memuse;
3565                                                 if (md->argintreguse > rd->argintreguse)
3566                                                         rd->argintreguse = md->argintreguse;
3567                                                 /* XXX non-leaf method? */
3568
3569                                                 /* make all stack variables saved */
3570
3571                                                 copy = curstack;
3572                                                 while (copy) {
3573                                                         sd.var[copy->varnum].flags |= SAVEDVAR;
3574                                                         copy->flags |= SAVEDVAR;
3575                                                         copy = copy->prev;
3576                                                 }
3577                                                 /* FALLTHROUGH */
3578
3579 #endif /* !(SUPPORT_DIVISION && SUPPORT_LONG && SUPPORT_LONG_DIV) */
3580
3581                                         case ICMD_LMUL:
3582                                         case ICMD_LADD:
3583                                         case ICMD_LSUB:
3584 #if SUPPORT_LONG_LOGICAL
3585                                         case ICMD_LAND:
3586                                         case ICMD_LOR:
3587                                         case ICMD_LXOR:
3588 #endif /* SUPPORT_LONG_LOGICAL */
3589                                                 COUNT(count_pcmd_op);
3590                                                 OP2_1(TYPE_LNG, TYPE_LNG, TYPE_LNG);
3591                                                 break;
3592
3593                                         case ICMD_LSHL:
3594                                         case ICMD_LSHR:
3595                                         case ICMD_LUSHR:
3596                                                 COUNT(count_pcmd_op);
3597                                                 OP2_1(TYPE_LNG, TYPE_INT, TYPE_LNG);
3598                                                 break;
3599
3600                                         case ICMD_FADD:
3601                                         case ICMD_FSUB:
3602                                         case ICMD_FMUL:
3603                                         case ICMD_FDIV:
3604                                         case ICMD_FREM:
3605                                                 COUNT(count_pcmd_op);
3606                                                 OP2_1(TYPE_FLT, TYPE_FLT, TYPE_FLT);
3607                                                 break;
3608
3609                                         case ICMD_DADD:
3610                                         case ICMD_DSUB:
3611                                         case ICMD_DMUL:
3612                                         case ICMD_DDIV:
3613                                         case ICMD_DREM:
3614                                                 COUNT(count_pcmd_op);
3615                                                 OP2_1(TYPE_DBL, TYPE_DBL, TYPE_DBL);
3616                                                 break;
3617
3618                                         case ICMD_LCMP:
3619                                                 COUNT(count_pcmd_op);
3620 #if SUPPORT_LONG_CMP_CONST
3621                                                 if ((len == 0) || (iptr[1].sx.val.i != 0))
3622                                                         goto normal_LCMP;
3623
3624                                                 switch (iptr[1].opc) {
3625                                                 case ICMD_IFEQ:
3626                                                         iptr->opc = ICMD_IF_LCMPEQ;
3627                                                 icmd_lcmp_if_tail:
3628                                                         iptr->dst.insindex = iptr[1].dst.insindex;
3629                                                         iptr[1].opc = ICMD_NOP;
3630
3631                                                         OP2_BRANCH(TYPE_LNG, TYPE_LNG);
3632                                                         BRANCH(tbptr, copy);
3633
3634                                                         COUNT(count_pcmd_bra);
3635                                                         break;
3636                                                 case ICMD_IFNE:
3637                                                         iptr->opc = ICMD_IF_LCMPNE;
3638                                                         goto icmd_lcmp_if_tail;
3639                                                 case ICMD_IFLT:
3640                                                         iptr->opc = ICMD_IF_LCMPLT;
3641                                                         goto icmd_lcmp_if_tail;
3642                                                 case ICMD_IFGT:
3643                                                         iptr->opc = ICMD_IF_LCMPGT;
3644                                                         goto icmd_lcmp_if_tail;
3645                                                 case ICMD_IFLE:
3646                                                         iptr->opc = ICMD_IF_LCMPLE;
3647                                                         goto icmd_lcmp_if_tail;
3648                                                 case ICMD_IFGE:
3649                                                         iptr->opc = ICMD_IF_LCMPGE;
3650                                                         goto icmd_lcmp_if_tail;
3651                                                 default:
3652                                                         goto normal_LCMP;
3653                                                 }
3654                                                 break;
3655 normal_LCMP:
3656 #endif /* SUPPORT_LONG_CMP_CONST */
3657                                                         OP2_1(TYPE_LNG, TYPE_LNG, TYPE_INT);
3658                                                 break;
3659
3660                                                 /* XXX why is this deactivated? */
3661 #if 0
3662                                         case ICMD_FCMPL:
3663                                                 COUNT(count_pcmd_op);
3664                                                 if ((len == 0) || (iptr[1].sx.val.i != 0))
3665                                                         goto normal_FCMPL;
3666
3667                                                 switch (iptr[1].opc) {
3668                                                 case ICMD_IFEQ:
3669                                                         iptr->opc = ICMD_IF_FCMPEQ;
3670                                                 icmd_if_fcmpl_tail:
3671                                                         iptr->dst.insindex = iptr[1].dst.insindex;
3672                                                         iptr[1].opc = ICMD_NOP;
3673
3674                                                         OP2_BRANCH(TYPE_FLT, TYPE_FLT);
3675                                                         BRANCH(tbptr, copy);
3676
3677                                                         COUNT(count_pcmd_bra);
3678                                                         break;
3679                                                 case ICMD_IFNE:
3680                                                         iptr->opc = ICMD_IF_FCMPNE;
3681                                                         goto icmd_if_fcmpl_tail;
3682                                                 case ICMD_IFLT:
3683                                                         iptr->opc = ICMD_IF_FCMPL_LT;
3684                                                         goto icmd_if_fcmpl_tail;
3685                                                 case ICMD_IFGT:
3686                                                         iptr->opc = ICMD_IF_FCMPL_GT;
3687                                                         goto icmd_if_fcmpl_tail;
3688                                                 case ICMD_IFLE:
3689                                                         iptr->opc = ICMD_IF_FCMPL_LE;
3690                                                         goto icmd_if_fcmpl_tail;
3691                                                 case ICMD_IFGE:
3692                                                         iptr->opc = ICMD_IF_FCMPL_GE;
3693                                                         goto icmd_if_fcmpl_tail;
3694                                                 default:
3695                                                         goto normal_FCMPL;
3696                                                 }
3697                                                 break;
3698
3699 normal_FCMPL:
3700                                                 OPTT2_1(TYPE_FLT, TYPE_FLT, TYPE_INT);
3701                                                 break;
3702
3703                                         case ICMD_FCMPG:
3704                                                 COUNT(count_pcmd_op);
3705                                                 if ((len == 0) || (iptr[1].sx.val.i != 0))
3706                                                         goto normal_FCMPG;
3707
3708                                                 switch (iptr[1].opc) {
3709                                                 case ICMD_IFEQ:
3710                                                         iptr->opc = ICMD_IF_FCMPEQ;
3711                                                 icmd_if_fcmpg_tail:
3712                                                         iptr->dst.insindex = iptr[1].dst.insindex;
3713                                                         iptr[1].opc = ICMD_NOP;
3714
3715                                                         OP2_BRANCH(TYPE_FLT, TYPE_FLT);
3716                                                         BRANCH(tbptr, copy);
3717
3718                                                         COUNT(count_pcmd_bra);
3719                                                         break;
3720                                                 case ICMD_IFNE:
3721                                                         iptr->opc = ICMD_IF_FCMPNE;
3722                                                         goto icmd_if_fcmpg_tail;
3723                                                 case ICMD_IFLT:
3724                                                         iptr->opc = ICMD_IF_FCMPG_LT;
3725                                                         goto icmd_if_fcmpg_tail;
3726                                                 case ICMD_IFGT:
3727                                                         iptr->opc = ICMD_IF_FCMPG_GT;
3728                                                         goto icmd_if_fcmpg_tail;
3729                                                 case ICMD_IFLE:
3730                                                         iptr->opc = ICMD_IF_FCMPG_LE;
3731                                                         goto icmd_if_fcmpg_tail;
3732                                                 case ICMD_IFGE:
3733                                                         iptr->opc = ICMD_IF_FCMPG_GE;
3734                                                         goto icmd_if_fcmpg_tail;
3735                                                 default:
3736                                                         goto normal_FCMPG;
3737                                                 }
3738                                                 break;
3739
3740 normal_FCMPG:
3741                                                 OP2_1(TYPE_FLT, TYPE_FLT, TYPE_INT);
3742                                                 break;
3743
3744                                         case ICMD_DCMPL:
3745                                                 COUNT(count_pcmd_op);
3746                                                 if ((len == 0) || (iptr[1].sx.val.i != 0))
3747                                                         goto normal_DCMPL;
3748
3749                                                 switch (iptr[1].opc) {
3750                                                 case ICMD_IFEQ:
3751                                                         iptr->opc = ICMD_IF_DCMPEQ;
3752                                                 icmd_if_dcmpl_tail:
3753                                                         iptr->dst.insindex = iptr[1].dst.insindex;
3754                                                         iptr[1].opc = ICMD_NOP;
3755
3756                                                         OP2_BRANCH(TYPE_DBL, TYPE_DBL);
3757                                                         BRANCH(tbptr, copy);
3758
3759                                                         COUNT(count_pcmd_bra);
3760                                                         break;
3761                                                 case ICMD_IFNE:
3762                                                         iptr->opc = ICMD_IF_DCMPNE;
3763                                                         goto icmd_if_dcmpl_tail;
3764                                                 case ICMD_IFLT:
3765                                                         iptr->opc = ICMD_IF_DCMPL_LT;
3766                                                         goto icmd_if_dcmpl_tail;
3767                                                 case ICMD_IFGT:
3768                                                         iptr->opc = ICMD_IF_DCMPL_GT;
3769                                                         goto icmd_if_dcmpl_tail;
3770                                                 case ICMD_IFLE:
3771                                                         iptr->opc = ICMD_IF_DCMPL_LE;
3772                                                         goto icmd_if_dcmpl_tail;
3773                                                 case ICMD_IFGE:
3774                                                         iptr->opc = ICMD_IF_DCMPL_GE;
3775                                                         goto icmd_if_dcmpl_tail;
3776                                                 default:
3777                                                         goto normal_DCMPL;
3778                                                 }
3779                                                 break;
3780
3781 normal_DCMPL:
3782                                                 OPTT2_1(TYPE_DBL, TYPE_INT);
3783                                                 break;
3784
3785                                         case ICMD_DCMPG:
3786                                                 COUNT(count_pcmd_op);
3787                                                 if ((len == 0) || (iptr[1].sx.val.i != 0))
3788                                                         goto normal_DCMPG;
3789
3790                                                 switch (iptr[1].opc) {
3791                                                 case ICMD_IFEQ:
3792                                                         iptr->opc = ICMD_IF_DCMPEQ;
3793                                                 icmd_if_dcmpg_tail:
3794                                                         iptr->dst.insindex = iptr[1].dst.insindex;
3795                                                         iptr[1].opc = ICMD_NOP;
3796
3797                                                         OP2_BRANCH(TYPE_DBL, TYPE_DBL);
3798                                                         BRANCH(tbptr, copy);
3799
3800                                                         COUNT(count_pcmd_bra);
3801                                                         break;
3802                                                 case ICMD_IFNE:
3803                                                         iptr->opc = ICMD_IF_DCMPNE;
3804                                                         goto icmd_if_dcmpg_tail;
3805                                                 case ICMD_IFLT:
3806                                                         iptr->opc = ICMD_IF_DCMPG_LT;
3807                                                         goto icmd_if_dcmpg_tail;
3808                                                 case ICMD_IFGT:
3809                                                         iptr->opc = ICMD_IF_DCMPG_GT;
3810                                                         goto icmd_if_dcmpg_tail;
3811                                                 case ICMD_IFLE:
3812                                                         iptr->opc = ICMD_IF_DCMPG_LE;
3813                                                         goto icmd_if_dcmpg_tail;
3814                                                 case ICMD_IFGE:
3815                                                         iptr->opc = ICMD_IF_DCMPG_GE;
3816                                                         goto icmd_if_dcmpg_tail;
3817                                                 default:
3818                                                         goto normal_DCMPG;
3819                                                 }
3820                                                 break;
3821
3822 normal_DCMPG:
3823                                                 OP2_1(TYPE_DBL, TYPE_DBL, TYPE_INT);
3824                                                 break;
3825 #else
3826                                         case ICMD_FCMPL:
3827                                         case ICMD_FCMPG:
3828                                                 COUNT(count_pcmd_op);
3829                                                 OP2_1(TYPE_FLT, TYPE_FLT, TYPE_INT);
3830                                                 break;
3831
3832                                         case ICMD_DCMPL:
3833                                         case ICMD_DCMPG:
3834                                                 COUNT(count_pcmd_op);
3835                                                 OP2_1(TYPE_DBL, TYPE_DBL, TYPE_INT);
3836                                                 break;
3837 #endif
3838
3839                                                 /* pop 1 push 1 */
3840
3841                                         case ICMD_INEG:
3842                                         case ICMD_INT2BYTE:
3843                                         case ICMD_INT2CHAR:
3844                                         case ICMD_INT2SHORT:
3845                                                 COUNT(count_pcmd_op);
3846                                                 OP1_1(TYPE_INT, TYPE_INT);
3847                                                 break;
3848                                         case ICMD_LNEG:
3849                                                 COUNT(count_pcmd_op);
3850                                                 OP1_1(TYPE_LNG, TYPE_LNG);
3851                                                 break;
3852                                         case ICMD_FNEG:
3853                                                 COUNT(count_pcmd_op);
3854                                                 OP1_1(TYPE_FLT, TYPE_FLT);
3855                                                 break;
3856                                         case ICMD_DNEG:
3857                                                 COUNT(count_pcmd_op);
3858                                                 OP1_1(TYPE_DBL, TYPE_DBL);
3859                                                 break;
3860
3861                                         case ICMD_I2L:
3862                                                 COUNT(count_pcmd_op);
3863                                                 OP1_1(TYPE_INT, TYPE_LNG);
3864                                                 break;
3865                                         case ICMD_I2F:
3866                                                 COUNT(count_pcmd_op);
3867                                                 OP1_1(TYPE_INT, TYPE_FLT);
3868                                                 break;
3869                                         case ICMD_I2D:
3870                                                 COUNT(count_pcmd_op);
3871                                                 OP1_1(TYPE_INT, TYPE_DBL);
3872                                                 break;
3873                                         case ICMD_L2I:
3874                                                 COUNT(count_pcmd_op);
3875                                                 OP1_1(TYPE_LNG, TYPE_INT);
3876                                                 break;
3877                                         case ICMD_L2F:
3878                                                 COUNT(count_pcmd_op);
3879                                                 OP1_1(TYPE_LNG, TYPE_FLT);
3880                                                 break;
3881                                         case ICMD_L2D:
3882                                                 COUNT(count_pcmd_op);
3883                                                 OP1_1(TYPE_LNG, TYPE_DBL);
3884                                                 break;
3885                                         case ICMD_F2I:
3886                                                 COUNT(count_pcmd_op);
3887                                                 OP1_1(TYPE_FLT, TYPE_INT);
3888                                                 break;
3889                                         case ICMD_F2L:
3890                                                 COUNT(count_pcmd_op);
3891                                                 OP1_1(TYPE_FLT, TYPE_LNG);
3892                                                 break;
3893                                         case ICMD_F2D:
3894                                                 COUNT(count_pcmd_op);
3895                                                 OP1_1(TYPE_FLT, TYPE_DBL);
3896                                                 break;
3897                                         case ICMD_D2I:
3898                                                 COUNT(count_pcmd_op);
3899                                                 OP1_1(TYPE_DBL, TYPE_INT);
3900                                                 break;
3901                                         case ICMD_D2L:
3902                                                 COUNT(count_pcmd_op);
3903                                                 OP1_1(TYPE_DBL, TYPE_LNG);
3904                                                 break;
3905                                         case ICMD_D2F:
3906                                                 COUNT(count_pcmd_op);
3907                                                 OP1_1(TYPE_DBL, TYPE_FLT);
3908                                                 break;
3909
3910                                         case ICMD_CHECKCAST:
3911                                                 coalescing_boundary = sd.new;
3912                                                 if (iptr->flags.bits & INS_FLAG_ARRAY) {
3913                                                         /* array type cast-check */
3914
3915                                                         bte = builtintable_get_internal(BUILTIN_arraycheckcast);
3916                                                         md = bte->md;
3917
3918                                                         if (md->memuse > rd->memuse)
3919                                                                 rd->memuse = md->memuse;
3920                                                         if (md->argintreguse > rd->argintreguse)
3921                                                                 rd->argintreguse = md->argintreguse;
3922
3923                                                         /* make all stack variables saved */
3924
3925                                                         copy = curstack;
3926                                                         while (copy) {
3927                                                                 sd.var[copy->varnum].flags |= SAVEDVAR;
3928                                                                 copy->flags |= SAVEDVAR;
3929                                                                 copy = copy->prev;
3930                                                         }
3931                                                 }
3932                                                 OP1_1(TYPE_ADR, TYPE_ADR);
3933                                                 break;
3934
3935                                         case ICMD_INSTANCEOF:
3936                                         case ICMD_ARRAYLENGTH:
3937                                                 coalescing_boundary = sd.new;
3938                                                 OP1_1(TYPE_ADR, TYPE_INT);
3939                                                 break;
3940
3941                                         case ICMD_NEWARRAY:
3942                                         case ICMD_ANEWARRAY:
3943                                                 coalescing_boundary = sd.new;
3944                                                 OP1_1(TYPE_INT, TYPE_ADR);
3945                                                 break;
3946
3947                                         case ICMD_GETFIELD:
3948                                                 coalescing_boundary = sd.new;
3949                                                 COUNT(count_check_null);
3950                                                 COUNT(count_pcmd_mem);
3951                                                 INSTRUCTION_GET_FIELDREF(iptr, fmiref);
3952                                                 OP1_1(TYPE_ADR, fmiref->parseddesc.fd->type);
3953                                                 break;
3954
3955                                                 /* pop 0 push 1 */
3956
3957                                         case ICMD_GETSTATIC:
3958                                                 coalescing_boundary = sd.new;
3959                                                 COUNT(count_pcmd_mem);
3960                                                 INSTRUCTION_GET_FIELDREF(iptr, fmiref);
3961                                                 OP0_1(fmiref->parseddesc.fd->type);
3962                                                 break;
3963
3964                                         case ICMD_NEW:
3965                                                 coalescing_boundary = sd.new;
3966                                                 OP0_1(TYPE_ADR);
3967                                                 break;
3968
3969                                         case ICMD_JSR:
3970                                                 OP0_1(TYPE_RET);
3971
3972                                                 assert(sd.bptr->next);  /* XXX exception */
3973                                                 sd.var[curstack->varnum].vv.retaddr = sd.bptr->next;
3974
3975                                                 tbptr = BLOCK_OF(iptr->sx.s23.s3.jsrtarget.insindex);
3976                                                 tbptr->type = BBTYPE_SBR;
3977
3978                                                 tbptr = stack_mark_reached(&sd, tbptr, curstack, stackdepth);
3979                                                 if (!tbptr)
3980                                                         return false;
3981
3982                                                 iptr->sx.s23.s3.jsrtarget.block = tbptr;
3983
3984                                                 /* We need to check for overflow right here because
3985                                                  * the pushed value is poped afterwards */
3986                                                 CHECKOVERFLOW;
3987
3988                                                 superblockend = true;
3989                                                 /* XXX should not be marked as interface, as it does not need to be */
3990                                                 /* allocated. Same for the invar of the target. */
3991                                                 break;
3992
3993                                         /* pop many push any */
3994
3995                                         case ICMD_BUILTIN:
3996 icmd_BUILTIN:
3997                                                 bte = iptr->sx.s23.s3.bte;
3998                                                 md = bte->md;
3999                                                 goto _callhandling;
4000
4001                                         case ICMD_INVOKESTATIC:
4002                                         case ICMD_INVOKESPECIAL:
4003                                         case ICMD_INVOKEVIRTUAL:
4004                                         case ICMD_INVOKEINTERFACE:
4005                                                 COUNT(count_pcmd_met);
4006
4007                                                 /* Check for functions to replace with builtin
4008                                                  * functions. */
4009
4010                                                 if (builtintable_replace_function(iptr))
4011                                                         goto icmd_BUILTIN;
4012
4013                                                 INSTRUCTION_GET_METHODDESC(iptr, md);
4014                                                 /* XXX resurrect this COUNT? */
4015 /*                          if (lm->flags & ACC_STATIC) */
4016 /*                              {COUNT(count_check_null);} */
4017
4018                                         _callhandling:
4019
4020                                                 coalescing_boundary = sd.new;
4021
4022                                                 i = md->paramcount;
4023
4024                                                 if (md->memuse > rd->memuse)
4025                                                         rd->memuse = md->memuse;
4026                                                 if (md->argintreguse > rd->argintreguse)
4027                                                         rd->argintreguse = md->argintreguse;
4028                                                 if (md->argfltreguse > rd->argfltreguse)
4029                                                         rd->argfltreguse = md->argfltreguse;
4030
4031                                                 REQUIRE(i);
4032
4033                                                 /* XXX optimize for <= 2 args */
4034                                                 /* XXX not for ICMD_BUILTIN */
4035                                                 iptr->s1.argcount = stackdepth;
4036                                                 iptr->sx.s23.s2.args = DMNEW(s4, stackdepth);
4037
4038                                                 copy = curstack;
4039                                                 for (i-- ; i >= 0; i--) {
4040                                                         iptr->sx.s23.s2.args[i] = copy->varnum;
4041
4042                                                         /* do not change STACKVARs or LOCALVARS to ARGVAR*/
4043                                                         /* ->  won't help anyway */
4044                                                         if (!(IS_OUTVAR(copy) || IS_LOCALVAR(copy))) {
4045
4046 #if defined(SUPPORT_PASS_FLOATARGS_IN_INTREGS)
4047                         /* If we pass float arguments in integer argument registers, we
4048                          * are not allowed to precolor them here. Floats have to be moved
4049                          * to this regs explicitly in codegen().
4050                          * Only arguments that are passed by stack anyway can be precolored
4051                          * (michi 2005/07/24) */
4052                                                         if (!(sd.var[copy->varnum].flags & SAVEDVAR) &&
4053                                                            (!IS_FLT_DBL_TYPE(copy->type) 
4054                                                                 || md->params[i].inmemory)) {
4055 #else
4056                                                         if (!(sd.var[copy->varnum].flags & SAVEDVAR)) {
4057 #endif
4058
4059                                                                 SET_PREALLOC(copy);
4060
4061 #if defined(ENABLE_INTRP)
4062                                                                 if (!opt_intrp) {
4063 #endif
4064                                                                         if (md->params[i].inmemory) {
4065                                                                                 sd.var[copy->varnum].vv.regoff =
4066                                                                                         md->params[i].regoff;
4067                                                                                 sd.var[copy->varnum].flags |= 
4068                                                                                         INMEMORY;
4069                                                                         }
4070                                                                         else {
4071                                                                                 if (IS_FLT_DBL_TYPE(copy->type)) {
4072 #if defined(SUPPORT_PASS_FLOATARGS_IN_INTREGS)
4073                                                                                         assert(0); /* XXX is this assert ok? */
4074 #else
4075                                                                                         sd.var[copy->varnum].vv.regoff = 
4076                                                                                 rd->argfltregs[md->params[i].regoff];
4077 #endif /* SUPPORT_PASS_FLOATARGS_IN_INTREGS */
4078                                                                                 }
4079                                                                                 else {
4080 #if defined(SUPPORT_COMBINE_INTEGER_REGISTERS)
4081                                                                                         if (IS_2_WORD_TYPE(copy->type))
4082                                                                                                 sd.var[copy->varnum].vv.regoff = 
4083                                 PACK_REGS( rd->argintregs[GET_LOW_REG(md->params[i].regoff)],
4084                                                    rd->argintregs[GET_HIGH_REG(md->params[i].regoff)]);
4085
4086                                                                                         else
4087 #endif /* SUPPORT_COMBINE_INTEGER_REGISTERS */
4088                                                                                                 sd.var[copy->varnum].vv.regoff = 
4089                                                                                 rd->argintregs[md->params[i].regoff];
4090                                                                                 }
4091                                                                         }
4092 #if defined(ENABLE_INTRP)
4093                                                                 } /* end if (!opt_intrp) */
4094 #endif
4095                                                         }
4096                                                         }
4097                                                         copy = copy->prev;
4098                                                 }
4099
4100                                                 /* deal with live-through stack slots "under" the */
4101                                                 /* arguments */
4102                                                 /* XXX not for ICMD_BUILTIN */
4103
4104                                                 i = md->paramcount;
4105
4106                                                 while (copy) {
4107                                                         SET_TEMPVAR(copy);
4108                                                         iptr->sx.s23.s2.args[i++] = copy->varnum;
4109                                                         sd.var[copy->varnum].flags |= SAVEDVAR;
4110                                                         copy = copy->prev;
4111                                                 }
4112
4113                                                 /* pop the arguments */
4114
4115                                                 i = md->paramcount;
4116
4117                                                 stackdepth -= i;
4118                                                 while (--i >= 0) {
4119                                                         POPANY;
4120                                                 }
4121
4122                                                 /* push the return value */
4123
4124                                                 if (md->returntype.type != TYPE_VOID) {
4125                                                         GET_NEW_VAR(sd, new_index, md->returntype.type);
4126                                                         DST(md->returntype.type, new_index);
4127                                                         stackdepth++;
4128                                                 }
4129                                                 break;
4130
4131                                         case ICMD_INLINE_START:
4132                                         case ICMD_INLINE_END:
4133                                                 CLR_S1;
4134                                                 CLR_DST;
4135                                                 break;
4136
4137                                         case ICMD_MULTIANEWARRAY:
4138                                                 coalescing_boundary = sd.new;
4139                                                 if (rd->argintreguse < 3)
4140                                                         rd->argintreguse = 3;
4141
4142                                                 i = iptr->s1.argcount;
4143
4144                                                 REQUIRE(i);
4145
4146                                                 iptr->sx.s23.s2.args = DMNEW(s4, i);
4147
4148 #if defined(SPECIALMEMUSE)
4149 # if defined(__DARWIN__)
4150                                                 if (rd->memuse < (i + INT_ARG_CNT +LA_SIZE_IN_POINTERS))
4151                                                         rd->memuse = i + LA_SIZE_IN_POINTERS + INT_ARG_CNT;
4152 # else
4153                                                 if (rd->memuse < (i + LA_SIZE_IN_POINTERS + 3))
4154                                                         rd->memuse = i + LA_SIZE_IN_POINTERS + 3;
4155 # endif
4156 #else
4157 # if defined(__I386__)
4158                                                 if (rd->memuse < i + 3)
4159                                                         rd->memuse = i + 3; /* n integer args spilled on stack */
4160 # elif defined(__MIPS__) && SIZEOF_VOID_P == 4
4161                                                 if (rd->memuse < i + 2)
4162                                                         rd->memuse = i + 2; /* 4*4 bytes callee save space */
4163 # else
4164                                                 if (rd->memuse < i)
4165                                                         rd->memuse = i; /* n integer args spilled on stack */
4166 # endif /* defined(__I386__) */
4167 #endif
4168                                                 copy = curstack;
4169                                                 while (--i >= 0) {
4170                                         /* check INT type here? Currently typecheck does this. */
4171                                                         iptr->sx.s23.s2.args[i] = copy->varnum;
4172                                                         if (!(sd.var[copy->varnum].flags & SAVEDVAR)
4173                                                                 && (!IS_OUTVAR(copy))
4174                                                                 && (!IS_LOCALVAR(copy)) ) {
4175                                                                 copy->varkind = ARGVAR;
4176                                                                 sd.var[copy->varnum].flags |=
4177                                                                         INMEMORY & PREALLOC;
4178 #if defined(SPECIALMEMUSE)
4179 # if defined(__DARWIN__)
4180                                                                 sd.var[copy->varnum].vv.regoff = i + 
4181                                                                         LA_SIZE_IN_POINTERS + INT_ARG_CNT;
4182 # else
4183                                                                 sd.var[copy->varnum].vv.regoff = i + 
4184                                                                         LA_SIZE_IN_POINTERS + 3;
4185 # endif
4186 #else
4187 # if defined(__I386__)
4188                                                                 sd.var[copy->varnum].vv.regoff = i + 3;
4189 # elif defined(__MIPS__) && SIZEOF_VOID_P == 4
4190                                                                 sd.var[copy->varnum].vv.regoff = i + 2;
4191 # else
4192                                                                 sd.var[copy->varnum].vv.regoff = i;
4193 # endif /* defined(__I386__) */
4194 #endif /* defined(SPECIALMEMUSE) */
4195                                                         }
4196                                                         copy = copy->prev;
4197                                                 }
4198                                                 while (copy) {
4199                                                         sd.var[copy->varnum].flags |= SAVEDVAR;
4200                                                         copy = copy->prev;
4201                                                 }
4202
4203                                                 i = iptr->s1.argcount;
4204                                                 stackdepth -= i;
4205                                                 while (--i >= 0) {
4206                                                         POPANY;
4207                                                 }
4208                                                 GET_NEW_VAR(sd, new_index, TYPE_ADR);
4209                                                 DST(TYPE_ADR, new_index);
4210                                                 stackdepth++;
4211                                                 break;
4212
4213                                         default:
4214                                                 *exceptionptr =
4215                                                         new_internalerror("Unknown ICMD %d", opcode);
4216                                                 return false;
4217                                         } /* switch */
4218
4219                                         CHECKOVERFLOW;
4220                                         iptr++;
4221                                 } /* while instructions */
4222
4223                                 /* stack slots at basic block end become interfaces */
4224
4225                                 sd.bptr->outdepth = stackdepth;
4226                                 sd.bptr->outvars = DMNEW(s4, stackdepth);
4227
4228                                 i = stackdepth - 1;
4229                                 for (copy = curstack; copy; i--, copy = copy->prev) {
4230                                         varinfo *v;
4231                                         s4 t;
4232
4233                                         /* with the new vars rd->interfaces will be removed */
4234                                         /* and all in and outvars have to be STACKVARS!     */
4235                                         /* in the moment i.e. SWAP with in and out vars can */
4236                                         /* create an unresolvable conflict */
4237
4238                                         SET_TEMPVAR(copy);
4239                                         t = copy->type;
4240                                         if (t == TYPE_RET)
4241                                                 t = TYPE_ADR;
4242
4243                                         v = sd.var + copy->varnum;
4244                                         v->flags |= OUTVAR;
4245
4246                                         if (jd->interface_map[i*5 + t].flags == UNUSED) {
4247                                                 /* no interface var until now for this depth and */
4248                                                 /* type */
4249                                                 jd->interface_map[i*5 + t].flags = v->flags;
4250                                         }
4251                                         else {
4252                                                 jd->interface_map[i*5 + t].flags |= v->flags;
4253                                         }
4254
4255                                         sd.bptr->outvars[i] = copy->varnum;
4256                                 }
4257
4258                                 /* check if interface slots at basic block begin must be saved */
4259                                 IF_NO_INTRP(
4260                                         for (i=0; i<sd.bptr->indepth; ++i) {
4261                                                 varinfo *v = sd.var + sd.bptr->invars[i];
4262                                                 s4 t;
4263
4264                                                 t = v->type;
4265                                                 if (t == TYPE_RET)
4266                                                         t = TYPE_ADR;
4267
4268                                                 if (jd->interface_map[i*5 + t].flags == UNUSED) {
4269                                                         /* no interface var until now for this depth and */
4270                                                         /* type */
4271                                                         jd->interface_map[i*5 + t].flags = v->flags;
4272                                                 }
4273                                                 else {
4274                                                         jd->interface_map[i*5 + t].flags |= v->flags;
4275                                                 }
4276                                         }
4277                                 );
4278
4279                                 /* store the number of this block's variables */
4280
4281                                 sd.bptr->varcount = sd.vartop - sd.bptr->varstart;
4282
4283 #if defined(STACK_VERBOSE)
4284                                 stack_verbose_block_exit(&sd, superblockend);
4285 #endif
4286
4287                                 /* reach the following block, if any */
4288
4289                                 if (!superblockend)
4290                                         if (!stack_reach_next_block(&sd))
4291                                                 return false;
4292
4293                 } /* for blocks */
4294
4295         } while (sd.repeat && !deadcode);
4296
4297         /* XXX reset TYPE_RET to TYPE_ADR */
4298
4299         for (i=0; i<sd.vartop; ++i) {
4300                 if (sd.var[i].type == TYPE_RET)
4301                         sd.var[i].type = TYPE_ADR;
4302         }
4303
4304         /* XXX hack to fix up the ranges of the cloned single-block handlers */
4305
4306         ex = cd->exceptiontable;
4307         for (; ex != NULL; ex = ex->down) {
4308                 if (ex->start == ex->end) {
4309                         assert(ex->end->next);
4310                         ex->end = ex->end->next;
4311                 }
4312         }
4313
4314         /* gather statistics *****************************************************/
4315
4316 #if defined(ENABLE_STATISTICS)
4317         if (opt_stat) {
4318                 if (jd->new_basicblockcount > count_max_basic_blocks)
4319                         count_max_basic_blocks = jd->new_basicblockcount;
4320                 count_basic_blocks += jd->new_basicblockcount;
4321                 if (jd->new_instructioncount > count_max_javainstr)
4322                         count_max_javainstr = jd->new_instructioncount;
4323                 count_javainstr += jd->new_instructioncount;
4324                 if (jd->new_stackcount > count_upper_bound_new_stack)
4325                         count_upper_bound_new_stack = jd->new_stackcount;
4326                 if ((sd.new - jd->new_stack) > count_max_new_stack)
4327                         count_max_new_stack = (sd.new - jd->new_stack);
4328
4329                 sd.bptr = jd->new_basicblocks;
4330                 for (; sd.bptr; sd.bptr = sd.bptr->next) {
4331                         if (sd.bptr->flags > BBREACHED) {
4332                                 if (sd.bptr->indepth >= 10)
4333                                         count_block_stack[10]++;
4334                                 else
4335                                         count_block_stack[sd.bptr->indepth]++;
4336                                 len = sd.bptr->icount;
4337                                 if (len < 10)
4338                                         count_block_size_distribution[len]++;
4339                                 else if (len <= 12)
4340                                         count_block_size_distribution[10]++;
4341                                 else if (len <= 14)
4342                                         count_block_size_distribution[11]++;
4343                                 else if (len <= 16)
4344                                         count_block_size_distribution[12]++;
4345                                 else if (len <= 18)
4346                                         count_block_size_distribution[13]++;
4347                                 else if (len <= 20)
4348                                         count_block_size_distribution[14]++;
4349                                 else if (len <= 25)
4350                                         count_block_size_distribution[15]++;
4351                                 else if (len <= 30)
4352                                         count_block_size_distribution[16]++;
4353                                 else
4354                                         count_block_size_distribution[17]++;
4355                         }
4356                 }
4357
4358                 if (iteration_count == 1)
4359                         count_analyse_iterations[0]++;
4360                 else if (iteration_count == 2)
4361                         count_analyse_iterations[1]++;
4362                 else if (iteration_count == 3)
4363                         count_analyse_iterations[2]++;
4364                 else if (iteration_count == 4)
4365                         count_analyse_iterations[3]++;
4366                 else
4367                         count_analyse_iterations[4]++;
4368
4369                 if (jd->new_basicblockcount <= 5)
4370                         count_method_bb_distribution[0]++;
4371                 else if (jd->new_basicblockcount <= 10)
4372                         count_method_bb_distribution[1]++;
4373                 else if (jd->new_basicblockcount <= 15)
4374                         count_method_bb_distribution[2]++;
4375                 else if (jd->new_basicblockcount <= 20)
4376                         count_method_bb_distribution[3]++;
4377                 else if (jd->new_basicblockcount <= 30)
4378                         count_method_bb_distribution[4]++;
4379                 else if (jd->new_basicblockcount <= 40)
4380                         count_method_bb_distribution[5]++;
4381                 else if (jd->new_basicblockcount <= 50)
4382                         count_method_bb_distribution[6]++;
4383                 else if (jd->new_basicblockcount <= 75)
4384                         count_method_bb_distribution[7]++;
4385                 else
4386                         count_method_bb_distribution[8]++;
4387         }
4388 #endif /* defined(ENABLE_STATISTICS) */
4389
4390         /* everything's ok *******************************************************/
4391
4392         return true;
4393
4394         /* goto labels for throwing verifier exceptions **************************/
4395
4396 #if defined(ENABLE_VERIFIER)
4397
4398 throw_stack_underflow:
4399         exceptions_throw_verifyerror(m, "Unable to pop operand off an empty stack");
4400         return false;
4401
4402 throw_stack_overflow:
4403         exceptions_throw_verifyerror(m, "Stack size too large");
4404         return false;
4405
4406 throw_stack_depth_error:
4407         exceptions_throw_verifyerror(m,"Stack depth mismatch");
4408         return false;
4409
4410 throw_stack_type_error:
4411         exceptions_throw_verifyerror_for_stack(m, expectedtype);
4412         return false;
4413
4414 throw_stack_category_error:
4415         exceptions_throw_verifyerror(m, "Attempt to split long or double on the stack");
4416         return false;
4417
4418 #endif
4419 }
4420
4421
4422 /* functions for verbose stack analysis output ********************************/
4423
4424 #if defined(STACK_VERBOSE)
4425 static void stack_verbose_show_varinfo(stackdata_t *sd, varinfo *v)
4426 {
4427         printf("%c", show_jit_type_letters[v->type]);
4428         if (v->type == TYPE_RET)
4429                 printf("{L%03d}", v->vv.retaddr->nr);
4430 }
4431
4432
4433 static void stack_verbose_show_variable(stackdata_t *sd, s4 index)
4434 {
4435         assert(index >= 0 && index < sd->vartop);
4436         stack_verbose_show_varinfo(sd, sd->var + index);
4437 }
4438
4439
4440 static void stack_verbose_show_block(stackdata_t *sd, basicblock *bptr)
4441 {
4442         s4 i;
4443
4444         printf("L%03d type:%d in:%d [", bptr->nr, bptr->type, bptr->indepth);
4445         if (bptr->invars) {
4446                 for (i=0; i<bptr->indepth; ++i) {
4447                         if (i)
4448                                 putchar(' ');
4449                         stack_verbose_show_variable(sd, bptr->invars[i]);
4450                 }
4451         }
4452         else
4453                 putchar('-');
4454         printf("] inlocals [");
4455         if (bptr->inlocals) {
4456                 for (i=0; i<sd->localcount; ++i) {
4457                         if (i)
4458                                 putchar(' ');
4459                         stack_verbose_show_varinfo(sd, bptr->inlocals + i);
4460                 }
4461         }
4462         else
4463                 putchar('-');
4464         printf("] out:%d [", bptr->outdepth);
4465         if (bptr->outvars) {
4466                 for (i=0; i<bptr->outdepth; ++i) {
4467                         if (i)
4468                                 putchar(' ');
4469                         stack_verbose_show_variable(sd, bptr->outvars[i]);
4470                 }
4471         }
4472         else
4473                 putchar('-');
4474         printf("]");
4475
4476         if (bptr->original)
4477                 printf(" (clone of L%03d)", bptr->original->nr);
4478         else {
4479                 basicblock *b = bptr->copied_to;
4480                 if (b) {
4481                         printf(" (copied to ");
4482                         for (; b; b = b->copied_to)
4483                                 printf("L%03d ", b->nr);
4484                         printf(")");
4485                 }
4486         }
4487 }
4488
4489
4490 static void stack_verbose_block_enter(stackdata_t *sd, bool reanalyse)
4491 {
4492         int i;
4493
4494         printf("======================================== STACK %sANALYSE BLOCK ", 
4495                         (reanalyse) ? "RE-" : "");
4496         stack_verbose_show_block(sd, sd->bptr);
4497         printf("\n");
4498
4499         if (sd->handlers[0]) {
4500                 printf("HANDLERS: ");
4501                 for (i=0; sd->handlers[i]; ++i) {
4502                         printf("L%03d ", sd->handlers[i]->handler->nr);
4503                 }
4504                 printf("\n");
4505         }
4506         printf("\n");
4507 }
4508
4509
4510 static void stack_verbose_block_exit(stackdata_t *sd, bool superblockend)
4511 {
4512         printf("%s ", (superblockend) ? "SUPERBLOCKEND" : "END");
4513         stack_verbose_show_block(sd, sd->bptr);
4514         printf("\n");
4515 }
4516 #endif
4517
4518
4519 /*
4520  * These are local overrides for various environment variables in Emacs.
4521  * Please do not remove this and leave it at the end of the file, where
4522  * Emacs will automagically detect them.
4523  * ---------------------------------------------------------------------
4524  * Local variables:
4525  * mode: c
4526  * indent-tabs-mode: t
4527  * c-basic-offset: 4
4528  * tab-width: 4
4529  * End:
4530  * vim:noexpandtab:sw=4:ts=4:
4531  */