* src/vm/jit/stack.c (stack_reanalyse_block): Removed unused
[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 5473 2006-09-11 23:32:45Z 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         exceptiontable *ex;
1186
1187 #if defined(STACK_VERBOSE)
1188         stack_verbose_block_enter(sd, true);
1189 #endif
1190
1191         b = sd->bptr;
1192
1193         if (!b->iinstr) {
1194                 orig = b->original;
1195                 assert(orig != NULL);
1196
1197                 /* clone the instruction list */
1198
1199                 cloneinstructions = true;
1200
1201                 assert(orig->iinstr);
1202                 len = orig->icount;
1203                 iptr = DMNEW(instruction, len + 1);
1204
1205                 MCOPY(iptr, orig->iinstr, instruction, len);
1206                 iptr[len].opc = ICMD_NOP;
1207                 b->iinstr = iptr;
1208                 b->icount = ++len;
1209
1210                 /* allocate space for the clone's block variables */
1211
1212                 stack_grow_variable_array(sd, orig->varcount);
1213
1214                 /* we already have the invars set */
1215
1216                 assert(b->indepth == orig->indepth);
1217
1218                 /* calculate relocation shifts for invars and block variables */
1219
1220                 if (orig->indepth) {
1221                         invarstart = orig->invars[0];
1222                         invarshift = b->invars[0] - invarstart;
1223                 }
1224                 else {
1225                         invarstart = INT_MAX;
1226                         invarshift = 0;
1227                 }
1228                 blockvarstart = orig->varstart;
1229                 blockvarshift = sd->vartop - blockvarstart;
1230
1231                 /* copy block variables */
1232
1233                 b->varstart = sd->vartop;
1234                 b->varcount = orig->varcount;
1235                 sd->vartop += b->varcount;
1236                 MCOPY(sd->var + b->varstart, sd->var + orig->varstart, varinfo, b->varcount);
1237
1238                 /* copy outvars */
1239
1240                 b->outdepth = orig->outdepth;
1241                 b->outvars = DMNEW(s4, orig->outdepth);
1242                 MCOPY(b->outvars, orig->outvars, s4, orig->outdepth);
1243
1244                 /* clone exception handlers */
1245
1246                 for (i=0; sd->handlers[i]; ++i) {
1247                         ex = DNEW(exceptiontable);
1248                         ex->handler = sd->handlers[i]->handler;
1249                         ex->start = b;
1250                         ex->end = b; /* XXX hack, see end of new_stack_analyse */
1251                         ex->catchtype = sd->handlers[i]->catchtype;
1252                         ex->down = NULL;
1253
1254                         assert(sd->extableend->down == NULL);
1255                         sd->extableend->down = ex;
1256                         sd->extableend = ex;
1257                         sd->jd->cd->exceptiontablelength++;
1258
1259                         sd->handlers[i] = ex;
1260                 }
1261         }
1262         else {
1263                 cloneinstructions = false;
1264                 invarshift = 0;
1265                 blockvarshift = 0;
1266                 invarstart = sd->vartop;
1267                 blockvarstart = sd->vartop;
1268                 iptr = b->iinstr;
1269         }
1270
1271         if (b->original) {
1272                 /* find exception handlers for the cloned block */
1273                 len = 0;
1274                 ex = sd->jd->cd->exceptiontable;
1275                 for (; ex != NULL; ex = ex->down) {
1276                         /* XXX the cloned exception handlers have identical */
1277                         /* start end end blocks.                            */
1278                         if ((ex->start == b) && (ex->end == b)) {
1279                                 sd->handlers[len++] = ex;
1280                         }
1281                 }
1282                 sd->handlers[len] = NULL;
1283         }
1284
1285 #if defined(STACK_VERBOSE)
1286         printf("invarstart = %d, blockvarstart = %d\n", invarstart, blockvarstart);
1287         printf("invarshift = %d, blockvarshift = %d\n", invarshift, blockvarshift);
1288 #endif
1289
1290         /* mark block as finished */
1291
1292         b->flags = BBFINISHED;
1293
1294         /* initialize locals at the start of this block */
1295
1296         if (b->inlocals)
1297                 MCOPY(sd->var, b->inlocals, varinfo, sd->localcount);
1298
1299         /* reach exception handlers for this block */
1300
1301         if (!stack_reach_handlers(sd))
1302                 return false;
1303
1304         superblockend = false;
1305
1306         for (len = b->icount; len--; iptr++) {
1307 #if defined(STACK_VERBOSE)
1308                 new_show_icmd(sd->jd, iptr, false, SHOW_STACK);
1309                 printf("\n");
1310 #endif
1311
1312                 maythrow = false;
1313
1314                 switch (iptr->opc) {
1315                         case ICMD_RET:
1316                                 j = iptr->s1.varindex;
1317
1318                                 if (sd->var[j].type != TYPE_RET) {
1319                                         exceptions_throw_verifyerror(sd->m, "RET with non-returnAddress value");
1320                                         return false;
1321                                 }
1322
1323                                 iptr->dst.block = stack_mark_reached_from_outvars(sd, sd->var[j].vv.retaddr);
1324                                 superblockend = true;
1325                                 break;
1326
1327                         case ICMD_JSR:
1328                                 iptr->sx.s23.s3.jsrtarget.block = stack_mark_reached_from_outvars(sd, iptr->sx.s23.s3.jsrtarget.block);
1329                                 superblockend = true;
1330                                 break;
1331
1332                         case ICMD_RETURN:
1333                                 superblockend = true;
1334                                 break;
1335
1336                         case ICMD_CHECKNULL:
1337                         case ICMD_PUTSTATICCONST:
1338                                 maythrow = true;
1339                                 break;
1340
1341                         case ICMD_NOP:
1342                         case ICMD_IINC:
1343                         case ICMD_INLINE_START:
1344                         case ICMD_INLINE_END:
1345                         case ICMD_INLINE_GOTO:
1346                                 break;
1347
1348                         case ICMD_GOTO:
1349                                 iptr->dst.block = stack_mark_reached_from_outvars(sd, iptr->dst.block);
1350                                 superblockend = true;
1351                                 break;
1352
1353                                 /* pop 0 push 1 const */
1354
1355                         case ICMD_ACONST:
1356                                 maythrow = true;
1357                         case ICMD_ICONST:
1358                         case ICMD_LCONST:
1359                         case ICMD_FCONST:
1360                         case ICMD_DCONST:
1361
1362                                 /* pop 0 push 1 load */
1363
1364                         case ICMD_ILOAD:
1365                         case ICMD_LLOAD:
1366                         case ICMD_FLOAD:
1367                         case ICMD_DLOAD:
1368                         case ICMD_ALOAD:
1369                                 RELOCATE(iptr->dst.varindex);
1370                                 break;
1371
1372                                 /* pop 2 push 1 */
1373
1374                         case ICMD_IALOAD:
1375                         case ICMD_LALOAD:
1376                         case ICMD_FALOAD:
1377                         case ICMD_DALOAD:
1378                         case ICMD_AALOAD:
1379                         case ICMD_BALOAD:
1380                         case ICMD_CALOAD:
1381                         case ICMD_SALOAD:
1382                                 RELOCATE(iptr->sx.s23.s2.varindex);
1383                                 RELOCATE(iptr->s1.varindex);
1384                                 RELOCATE(iptr->dst.varindex);
1385                                 maythrow = true;
1386                                 break;
1387
1388                                 /* pop 3 push 0 */
1389
1390                         case ICMD_IASTORE:
1391                         case ICMD_LASTORE:
1392                         case ICMD_FASTORE:
1393                         case ICMD_DASTORE:
1394                         case ICMD_AASTORE:
1395                         case ICMD_BASTORE:
1396                         case ICMD_CASTORE:
1397                         case ICMD_SASTORE:
1398                                 RELOCATE(iptr->sx.s23.s3.varindex);
1399                                 RELOCATE(iptr->sx.s23.s2.varindex);
1400                                 RELOCATE(iptr->s1.varindex);
1401                                 maythrow = true;
1402                                 break;
1403
1404                                 /* pop 1 push 0 store */
1405
1406                         case ICMD_ISTORE:
1407                         case ICMD_LSTORE:
1408                         case ICMD_FSTORE:
1409                         case ICMD_DSTORE:
1410                         case ICMD_ASTORE:
1411                                 RELOCATE(iptr->s1.varindex);
1412
1413                                 j = iptr->dst.varindex;
1414                                 COPY_VAL_AND_TYPE(*sd, iptr->s1.varindex, j);
1415                                 break;
1416
1417                                 /* pop 1 push 0 */
1418
1419                         case ICMD_ARETURN:
1420                         case ICMD_ATHROW:
1421                                 maythrow = true;
1422                         case ICMD_IRETURN:
1423                         case ICMD_LRETURN:
1424                         case ICMD_FRETURN:
1425                         case ICMD_DRETURN:
1426                                 RELOCATE(iptr->s1.varindex);
1427                                 superblockend = true;
1428                                 break;
1429
1430                         case ICMD_PUTSTATIC:
1431                         case ICMD_PUTFIELDCONST:
1432                                 maythrow = true;
1433                         case ICMD_POP:
1434                                 RELOCATE(iptr->s1.varindex);
1435                                 break;
1436
1437                                 /* pop 1 push 0 branch */
1438
1439                         case ICMD_IFNULL:
1440                         case ICMD_IFNONNULL:
1441
1442                         case ICMD_IFEQ:
1443                         case ICMD_IFNE:
1444                         case ICMD_IFLT:
1445                         case ICMD_IFGE:
1446                         case ICMD_IFGT:
1447                         case ICMD_IFLE:
1448
1449                         case ICMD_IF_LEQ:
1450                         case ICMD_IF_LNE:
1451                         case ICMD_IF_LLT:
1452                         case ICMD_IF_LGE:
1453                         case ICMD_IF_LGT:
1454                         case ICMD_IF_LLE:
1455                                 RELOCATE(iptr->s1.varindex);
1456                                 iptr->dst.block = stack_mark_reached_from_outvars(sd, iptr->dst.block);
1457                                 break;
1458
1459                                 /* pop 1 push 0 table branch */
1460
1461                         case ICMD_TABLESWITCH:
1462                                 i = iptr->sx.s23.s3.tablehigh - iptr->sx.s23.s2.tablelow + 1 + 1;
1463
1464                                 if (cloneinstructions) {
1465                                         table = DMNEW(branch_target_t, i);
1466                                         MCOPY(table, iptr->dst.table, branch_target_t, i);
1467                                         iptr->dst.table = table;
1468                                 }
1469                                 else {
1470                                         table = iptr->dst.table;
1471                                 }
1472
1473                                 RELOCATE(iptr->s1.varindex);
1474                                 while (i--) {
1475                                         table->block = stack_mark_reached_from_outvars(sd, table->block);
1476                                         table++;
1477                                 }
1478                                 superblockend = true;
1479                                 break;
1480
1481                         case ICMD_LOOKUPSWITCH:
1482                                 i = iptr->sx.s23.s2.lookupcount;
1483                                 if (cloneinstructions) {
1484                                         lookup = DMNEW(lookup_target_t, i);
1485                                         MCOPY(lookup, iptr->dst.lookup, lookup_target_t, i);
1486                                         iptr->dst.lookup = lookup;
1487                                 }
1488                                 else {
1489                                         lookup = iptr->dst.lookup;
1490                                 }
1491                                 RELOCATE(iptr->s1.varindex);
1492                                 while (i--) {
1493                                         lookup->target.block = stack_mark_reached_from_outvars(sd, lookup->target.block);
1494                                         lookup++;
1495                                 }
1496                                 iptr->sx.s23.s3.lookupdefault.block = stack_mark_reached_from_outvars(sd, iptr->sx.s23.s3.lookupdefault.block);
1497                                 superblockend = true;
1498                                 break;
1499
1500                         case ICMD_MONITORENTER:
1501                         case ICMD_MONITOREXIT:
1502                                 RELOCATE(iptr->s1.varindex);
1503                                 maythrow = true;
1504                                 break;
1505
1506                                 /* pop 2 push 0 branch */
1507
1508                         case ICMD_IF_ICMPEQ:
1509                         case ICMD_IF_ICMPNE:
1510                         case ICMD_IF_ICMPLT:
1511                         case ICMD_IF_ICMPGE:
1512                         case ICMD_IF_ICMPGT:
1513                         case ICMD_IF_ICMPLE:
1514
1515                         case ICMD_IF_LCMPEQ:
1516                         case ICMD_IF_LCMPNE:
1517                         case ICMD_IF_LCMPLT:
1518                         case ICMD_IF_LCMPGE:
1519                         case ICMD_IF_LCMPGT:
1520                         case ICMD_IF_LCMPLE:
1521
1522                         case ICMD_IF_FCMPEQ:
1523                         case ICMD_IF_FCMPNE:
1524
1525                         case ICMD_IF_FCMPL_LT:
1526                         case ICMD_IF_FCMPL_GE:
1527                         case ICMD_IF_FCMPL_GT:
1528                         case ICMD_IF_FCMPL_LE:
1529
1530                         case ICMD_IF_FCMPG_LT:
1531                         case ICMD_IF_FCMPG_GE:
1532                         case ICMD_IF_FCMPG_GT:
1533                         case ICMD_IF_FCMPG_LE:
1534
1535                         case ICMD_IF_DCMPEQ:
1536                         case ICMD_IF_DCMPNE:
1537
1538                         case ICMD_IF_DCMPL_LT:
1539                         case ICMD_IF_DCMPL_GE:
1540                         case ICMD_IF_DCMPL_GT:
1541                         case ICMD_IF_DCMPL_LE:
1542
1543                         case ICMD_IF_DCMPG_LT:
1544                         case ICMD_IF_DCMPG_GE:
1545                         case ICMD_IF_DCMPG_GT:
1546                         case ICMD_IF_DCMPG_LE:
1547
1548                         case ICMD_IF_ACMPEQ:
1549                         case ICMD_IF_ACMPNE:
1550                                 RELOCATE(iptr->sx.s23.s2.varindex);
1551                                 RELOCATE(iptr->s1.varindex);
1552                                 iptr->dst.block = stack_mark_reached_from_outvars(sd, iptr->dst.block);
1553                                 break;
1554
1555                                 /* pop 2 push 0 */
1556
1557                         case ICMD_PUTFIELD:
1558                         case ICMD_IASTORECONST:
1559                         case ICMD_LASTORECONST:
1560                         case ICMD_AASTORECONST:
1561                         case ICMD_BASTORECONST:
1562                         case ICMD_CASTORECONST:
1563                         case ICMD_SASTORECONST:
1564                                 maythrow = true;
1565                         case ICMD_POP2:
1566                                 RELOCATE(iptr->sx.s23.s2.varindex);
1567                                 RELOCATE(iptr->s1.varindex);
1568                                 break;
1569
1570                                 /* pop 0 push 1 copy */
1571
1572                         case ICMD_COPY:
1573                         case ICMD_MOVE:
1574                                 RELOCATE(iptr->dst.varindex);
1575                                 RELOCATE(iptr->s1.varindex);
1576                                 COPY_VAL_AND_TYPE(*sd, iptr->s1.varindex, iptr->dst.varindex);
1577                                 break;
1578
1579                                 /* pop 2 push 1 */
1580
1581                         case ICMD_IDIV:
1582                         case ICMD_IREM:
1583                         case ICMD_LDIV:
1584                         case ICMD_LREM:
1585                                 maythrow = true;
1586                         case ICMD_IADD:
1587                         case ICMD_ISUB:
1588                         case ICMD_IMUL:
1589                         case ICMD_ISHL:
1590                         case ICMD_ISHR:
1591                         case ICMD_IUSHR:
1592                         case ICMD_IAND:
1593                         case ICMD_IOR:
1594                         case ICMD_IXOR:
1595                         case ICMD_LADD:
1596                         case ICMD_LSUB:
1597                         case ICMD_LMUL:
1598                         case ICMD_LOR:
1599                         case ICMD_LAND:
1600                         case ICMD_LXOR:
1601                         case ICMD_LSHL:
1602                         case ICMD_LSHR:
1603                         case ICMD_LUSHR:
1604                         case ICMD_FADD:
1605                         case ICMD_FSUB:
1606                         case ICMD_FMUL:
1607                         case ICMD_FDIV:
1608                         case ICMD_FREM:
1609                         case ICMD_DADD:
1610                         case ICMD_DSUB:
1611                         case ICMD_DMUL:
1612                         case ICMD_DDIV:
1613                         case ICMD_DREM:
1614                         case ICMD_LCMP:
1615                         case ICMD_FCMPL:
1616                         case ICMD_FCMPG:
1617                         case ICMD_DCMPL:
1618                         case ICMD_DCMPG:
1619                                 RELOCATE(iptr->sx.s23.s2.varindex);
1620                                 RELOCATE(iptr->s1.varindex);
1621                                 RELOCATE(iptr->dst.varindex);
1622                                 break;
1623
1624                                 /* pop 1 push 1 */
1625
1626                         case ICMD_CHECKCAST:
1627                         case ICMD_ARRAYLENGTH:
1628                         case ICMD_INSTANCEOF:
1629                         case ICMD_NEWARRAY:
1630                         case ICMD_ANEWARRAY:
1631                                 maythrow = true;
1632                         case ICMD_GETFIELD:
1633                         case ICMD_IADDCONST:
1634                         case ICMD_ISUBCONST:
1635                         case ICMD_IMULCONST:
1636                         case ICMD_IMULPOW2:
1637                         case ICMD_IDIVPOW2:
1638                         case ICMD_IREMPOW2:
1639                         case ICMD_IANDCONST:
1640                         case ICMD_IORCONST:
1641                         case ICMD_IXORCONST:
1642                         case ICMD_ISHLCONST:
1643                         case ICMD_ISHRCONST:
1644                         case ICMD_IUSHRCONST:
1645                         case ICMD_LADDCONST:
1646                         case ICMD_LSUBCONST:
1647                         case ICMD_LMULCONST:
1648                         case ICMD_LMULPOW2:
1649                         case ICMD_LDIVPOW2:
1650                         case ICMD_LREMPOW2:
1651                         case ICMD_LANDCONST:
1652                         case ICMD_LORCONST:
1653                         case ICMD_LXORCONST:
1654                         case ICMD_LSHLCONST:
1655                         case ICMD_LSHRCONST:
1656                         case ICMD_LUSHRCONST:
1657                         case ICMD_INEG:
1658                         case ICMD_INT2BYTE:
1659                         case ICMD_INT2CHAR:
1660                         case ICMD_INT2SHORT:
1661                         case ICMD_LNEG:
1662                         case ICMD_FNEG:
1663                         case ICMD_DNEG:
1664                         case ICMD_I2L:
1665                         case ICMD_I2F:
1666                         case ICMD_I2D:
1667                         case ICMD_L2I:
1668                         case ICMD_L2F:
1669                         case ICMD_L2D:
1670                         case ICMD_F2I:
1671                         case ICMD_F2L:
1672                         case ICMD_F2D:
1673                         case ICMD_D2I:
1674                         case ICMD_D2L:
1675                         case ICMD_D2F:
1676                                 RELOCATE(iptr->s1.varindex);
1677                                 RELOCATE(iptr->dst.varindex);
1678                                 break;
1679
1680                                 /* pop 0 push 1 */
1681
1682                         case ICMD_GETSTATIC:
1683                         case ICMD_NEW:
1684                                 maythrow = true;
1685                                 RELOCATE(iptr->dst.varindex);
1686                                 break;
1687
1688                                 /* pop many push any */
1689
1690                         case ICMD_INVOKESTATIC:
1691                         case ICMD_INVOKESPECIAL:
1692                         case ICMD_INVOKEVIRTUAL:
1693                         case ICMD_INVOKEINTERFACE:
1694                         case ICMD_BUILTIN:
1695                         case ICMD_MULTIANEWARRAY:
1696                                 i = iptr->s1.argcount;
1697                                 if (cloneinstructions) {
1698                                         argp = DMNEW(s4, i);
1699                                         MCOPY(argp, iptr->sx.s23.s2.args, s4, i);
1700                                         iptr->sx.s23.s2.args = argp;
1701                                 }
1702                                 else {
1703                                         argp = iptr->sx.s23.s2.args;
1704                                 }
1705
1706                                 maythrow = true;
1707                                 while (--i >= 0) {
1708                                         RELOCATE(*argp);
1709                                         argp++;
1710                                 }
1711                                 RELOCATE(iptr->dst.varindex);
1712                                 break;
1713
1714                         default:
1715                                 *exceptionptr =
1716                                         new_internalerror("Unknown ICMD %d during stack re-analysis",
1717                                                         iptr->opc);
1718                                 return false;
1719                 } /* switch */
1720
1721 #if defined(STACK_VERBOSE)
1722                 new_show_icmd(sd->jd, iptr, false, SHOW_STACK);
1723                 printf("\n");
1724 #endif
1725         }
1726
1727         /* relocate outvars */
1728
1729         for (i=0; i<b->outdepth; ++i) {
1730                 RELOCATE(b->outvars[i]);
1731         }
1732
1733 #if defined(STACK_VERBOSE)
1734         stack_verbose_block_exit(sd, superblockend);
1735 #endif
1736
1737         /* propagate to the next block */
1738
1739         if (!superblockend)
1740                 if (!stack_reach_next_block(sd))
1741                         return false;
1742
1743         return true;
1744 }
1745
1746
1747 /* stack_analyse ***************************************************************
1748
1749    Analyse_stack uses the intermediate code created by parse.c to
1750    build a model of the JVM operand stack for the current method.
1751    
1752    The following checks are performed:
1753      - check for operand stack underflow (before each instruction)
1754      - check for operand stack overflow (after[1] each instruction)
1755      - check for matching stack depth at merging points
1756      - check for matching basic types[2] at merging points
1757      - check basic types for instruction input (except for BUILTIN*
1758            opcodes, INVOKE* opcodes and MULTIANEWARRAY)
1759    
1760    [1]) Checking this after the instruction should be ok. parse.c
1761    counts the number of required stack slots in such a way that it is
1762    only vital that we don't exceed `maxstack` at basic block
1763    boundaries.
1764    
1765    [2]) 'basic types' means the distinction between INT, LONG, FLOAT,
1766    DOUBLE and ADDRESS types. Subtypes of INT and different ADDRESS
1767    types are not discerned.
1768
1769 *******************************************************************************/
1770
1771 bool new_stack_analyse(jitdata *jd)
1772 {
1773         methodinfo   *m;              /* method being analyzed                    */
1774         codeinfo     *code;
1775         codegendata  *cd;
1776         registerdata *rd;
1777         stackdata_t   sd;
1778         int           b_index;        /* basic block index                        */
1779         int           stackdepth;
1780         stackptr      curstack;       /* current stack top                        */
1781         stackptr      copy;
1782         int           opcode;         /* opcode of current instruction            */
1783         int           i, j;
1784         int           javaindex;
1785         int           len;            /* # of instructions after the current one  */
1786         bool          superblockend;  /* if true, no fallthrough to next block    */
1787         bool          deadcode;       /* true if no live code has been reached    */
1788         instruction  *iptr;           /* the current instruction                  */
1789         basicblock   *tbptr;
1790         basicblock   *original;
1791         exceptiontable *ex;
1792
1793         stackptr     *last_store_boundary;
1794         stackptr      coalescing_boundary;
1795
1796         stackptr      src1, src2, src3, src4, dst1, dst2;
1797
1798         branch_target_t *table;
1799         lookup_target_t *lookup;
1800 #if defined(ENABLE_VERIFIER)
1801         int           expectedtype;   /* used by CHECK_BASIC_TYPE                 */
1802 #endif
1803         builtintable_entry *bte;
1804         methoddesc         *md;
1805         constant_FMIref    *fmiref;
1806 #if defined(ENABLE_STATISTICS)
1807         int           iteration_count;  /* number of iterations of analysis       */
1808 #endif
1809         int           new_index; /* used to get a new var index with GET_NEW_INDEX*/
1810
1811 #if defined(STACK_VERBOSE)
1812         new_show_method(jd, SHOW_PARSE);
1813 #endif
1814
1815         /* get required compiler data - initialization */
1816
1817         m    = jd->m;
1818         code = jd->code;
1819         cd   = jd->cd;
1820         rd   = jd->rd;
1821
1822         /* initialize the stackdata_t struct */
1823
1824         sd.m = m;
1825         sd.jd = jd;
1826         sd.varcount = jd->varcount;
1827         sd.vartop =  jd->vartop;
1828         sd.localcount = jd->localcount;
1829         sd.var = jd->var;
1830         sd.handlers = DMNEW(exceptiontable *, cd->exceptiontablelength + 1);
1831         sd.exstack.type = TYPE_ADR;
1832         sd.exstack.prev = NULL;
1833         sd.exstack.varnum = 0; /* XXX do we need a real variable index here? */
1834
1835 #if defined(ENABLE_LSRA)
1836         m->maxlifetimes = 0;
1837 #endif
1838
1839 #if defined(ENABLE_STATISTICS)
1840         iteration_count = 0;
1841 #endif
1842
1843         /* find the last real basic block */
1844         
1845         sd.last_real_block = NULL;
1846         tbptr = jd->new_basicblocks;
1847         while (tbptr->next) {
1848                 sd.last_real_block = tbptr;
1849                 tbptr = tbptr->next;
1850         }
1851         assert(sd.last_real_block);
1852
1853         /* find the last exception handler */
1854
1855         if (cd->exceptiontablelength)
1856                 sd.extableend = cd->exceptiontable + cd->exceptiontablelength - 1;
1857         else
1858                 sd.extableend = NULL;
1859
1860         /* init jd->interface_map */
1861
1862         jd->interface_map = DMNEW(interface_info, m->maxstack * 5);
1863         for (i = 0; i < m->maxstack * 5; i++)
1864                 jd->interface_map[i].flags = UNUSED;
1865
1866         last_store_boundary = DMNEW(stackptr, cd->maxlocals);
1867
1868         /* initialize flags and invars (none) of first block */
1869
1870         jd->new_basicblocks[0].flags = BBREACHED;
1871         jd->new_basicblocks[0].invars = NULL;
1872         jd->new_basicblocks[0].indepth = 0;
1873
1874         /* stack analysis loop (until fixpoint reached) **************************/
1875
1876         do {
1877 #if defined(ENABLE_STATISTICS)
1878                 iteration_count++;
1879 #endif
1880
1881                 /* initialize loop over basic blocks */
1882
1883                 sd.bptr = jd->new_basicblocks;
1884                 superblockend = true;
1885                 sd.repeat = false;
1886                 curstack = NULL; stackdepth = 0;
1887                 deadcode = true;
1888
1889                 /* iterate over basic blocks *****************************************/
1890
1891                 for (; sd.bptr; sd.bptr = sd.bptr->next) {
1892
1893                         if (sd.bptr->flags == BBDELETED) {
1894                                 /* This block has been deleted - do nothing. */
1895
1896                                 continue;
1897                         }
1898
1899                         if (superblockend && (sd.bptr->flags < BBREACHED)) {
1900                                 /* This block has not been reached so far, and we      */
1901                                 /* don't fall into it, so we'll have to iterate again. */
1902
1903                                 sd.repeat = true;
1904                                 continue;
1905                         }
1906
1907                         if (sd.bptr->flags > BBREACHED) {
1908                                 /* This block is already finished. */
1909
1910                                 superblockend = true;
1911                                 continue;
1912                         }
1913
1914                         if (sd.bptr->original && sd.bptr->original->flags < BBFINISHED) {
1915                                 /* This block is a clone and the original has not been */
1916                                 /* analysed, yet. Analyse it on the next iteration.    */
1917
1918                                 sd.repeat = true;
1919                                 continue;
1920                         }
1921
1922                         /* This block has to be analysed now. */
1923
1924                         /* XXX The rest of this block is still indented one level too */
1925                         /* much in order to avoid a giant diff by changing that.      */
1926
1927                                 /* We know that sd.bptr->flags == BBREACHED. */
1928                                 /* This block has been reached before.    */
1929
1930                                 assert(sd.bptr->flags == BBREACHED);
1931                                 stackdepth = sd.bptr->indepth;
1932
1933                                 /* find exception handlers for this block */
1934
1935                                 /* determine the active exception handlers for this block */
1936                                 /* XXX could use a faster algorithm with sorted lists or  */
1937                                 /* something?                                             */
1938
1939                                 original = (sd.bptr->original) ? sd.bptr->original : sd.bptr;
1940
1941                                 len = 0;
1942                                 ex = cd->exceptiontable;
1943                                 for (; ex != NULL; ex = ex->down) {
1944                                         if ((ex->start <= original) && (ex->end > original)) {
1945                                                 sd.handlers[len++] = ex;
1946                                         }
1947                                 }
1948                                 sd.handlers[len] = NULL;
1949
1950
1951                                 /* reanalyse cloned block */
1952
1953                                 if (sd.bptr->original) {
1954                                         if (!stack_reanalyse_block(&sd))
1955                                                 return false;
1956                                         continue;
1957                                 }
1958
1959                                 /* reset the new pointer for allocating stackslots */
1960
1961                                 sd.new = jd->new_stack;
1962
1963                                 /* create the instack of this block */
1964
1965                                 curstack = stack_create_instack(&sd);
1966
1967                                 /* initialize locals at the start of this block */
1968
1969                                 if (sd.bptr->inlocals)
1970                                         MCOPY(sd.var, sd.bptr->inlocals, varinfo, sd.localcount);
1971
1972                                 /* set up local variables for analyzing this block */
1973
1974                                 deadcode = false;
1975                                 superblockend = false;
1976                                 len = sd.bptr->icount;
1977                                 iptr = sd.bptr->iinstr;
1978                                 b_index = sd.bptr - jd->new_basicblocks;
1979
1980                                 /* mark the block as analysed */
1981
1982                                 sd.bptr->flags = BBFINISHED;
1983
1984                                 /* reset variables for dependency checking */
1985
1986                                 coalescing_boundary = sd.new;
1987                                 for( i = 0; i < cd->maxlocals; i++)
1988                                         last_store_boundary[i] = sd.new;
1989
1990                                 /* remember the start of this block's variables */
1991   
1992                                 sd.bptr->varstart = sd.vartop;
1993
1994 #if defined(STACK_VERBOSE)
1995                                 stack_verbose_block_enter(&sd, false);
1996 #endif
1997   
1998                                 /* reach exception handlers for this block */
1999
2000                                 if (!stack_reach_handlers(&sd))
2001                                         return false;
2002
2003                                 /* iterate over ICMDs ****************************************/
2004
2005                                 while (--len >= 0)  {
2006
2007 #if defined(STACK_VERBOSE)
2008                                         new_show_icmd(jd, iptr, false, SHOW_PARSE); printf("\n");
2009                                         for( copy = curstack; copy; copy = copy->prev ) {
2010                                                 printf("%2d(%d", copy->varnum, copy->type);
2011                                                 if (IS_OUTVAR(copy))
2012                                                         printf("S");
2013                                                 if (IS_PREALLOC(copy))
2014                                                         printf("A");
2015                                                 printf(") ");
2016                                         }
2017                                         printf("\n");
2018 #endif
2019
2020                                         /* fetch the current opcode */
2021
2022                                         opcode = iptr->opc;
2023
2024                                         /* automatically replace some ICMDs with builtins */
2025
2026 #if defined(USEBUILTINTABLE)
2027                                         IF_NO_INTRP(
2028                                                 bte = builtintable_get_automatic(opcode);
2029
2030                                                 if (bte && bte->opcode == opcode) {
2031                                                         iptr->opc           = ICMD_BUILTIN;
2032                                                         iptr->flags.bits    = 0;
2033                                                         iptr->sx.s23.s3.bte = bte;
2034                                                         /* iptr->line is already set */
2035                                                         jd->isleafmethod = false;
2036                                                         goto icmd_BUILTIN;
2037                                                 }
2038                                         );
2039 #endif /* defined(USEBUILTINTABLE) */
2040
2041                                         /* main opcode switch *************************************/
2042
2043                                         switch (opcode) {
2044
2045                                                 /* pop 0 push 0 */
2046
2047                                         case ICMD_NOP:
2048 icmd_NOP:
2049                                                 CLR_SX;
2050                                                 OP0_0;
2051                                                 break;
2052
2053                                         case ICMD_CHECKNULL:
2054                                                 coalescing_boundary = sd.new;
2055                                                 COUNT(count_check_null);
2056                                                 USE_S1(TYPE_ADR);
2057                                                 CLR_SX;
2058                                                 CLR_DST; /* XXX live through? */
2059                                                 break;
2060
2061                                         case ICMD_RET:
2062                                                 j = iptr->s1.varindex = 
2063                                                         jd->local_map[iptr->s1.varindex * 5 + TYPE_ADR];
2064
2065                                                 if (sd.var[j].type != TYPE_RET) {
2066                                                         exceptions_throw_verifyerror(m, "RET with non-returnAddress value");
2067                                                         return false;
2068                                                 }
2069                 
2070                                                 CLR_SX;
2071
2072                                                 iptr->dst.block = stack_mark_reached(&sd, sd.var[j].vv.retaddr, curstack, stackdepth);
2073 #if 0
2074                                                 IF_NO_INTRP( rd->locals[iptr->s1.localindex/*XXX invalid here*/][TYPE_ADR].type = TYPE_ADR; );
2075 #endif
2076                                                 superblockend = true;
2077                                                 break;
2078
2079                                         case ICMD_RETURN:
2080                                                 COUNT(count_pcmd_return);
2081                                                 CLR_SX;
2082                                                 OP0_0;
2083                                                 superblockend = true;
2084                                                 break;
2085
2086
2087                                                 /* pop 0 push 1 const */
2088
2089         /************************** ICONST OPTIMIZATIONS **************************/
2090
2091                                         case ICMD_ICONST:
2092                                                 COUNT(count_pcmd_load);
2093                                                 if (len == 0)
2094                                                         goto normal_ICONST;
2095
2096                                                 switch (iptr[1].opc) {
2097                                                         case ICMD_IADD:
2098                                                                 iptr->opc = ICMD_IADDCONST;
2099                                                                 /* FALLTHROUGH */
2100
2101                                                         icmd_iconst_tail:
2102                                                                 iptr[1].opc = ICMD_NOP;
2103                                                                 OP1_1(TYPE_INT, TYPE_INT);
2104                                                                 COUNT(count_pcmd_op);
2105                                                                 break;
2106
2107                                                         case ICMD_ISUB:
2108                                                                 iptr->opc = ICMD_ISUBCONST;
2109                                                                 goto icmd_iconst_tail;
2110 #if SUPPORT_CONST_MUL
2111                                                         case ICMD_IMUL:
2112                                                                 iptr->opc = ICMD_IMULCONST;
2113                                                                 goto icmd_iconst_tail;
2114 #else /* SUPPORT_CONST_MUL */
2115                                                         case ICMD_IMUL:
2116                                                                 if (iptr->sx.val.i == 0x00000002)
2117                                                                         iptr->sx.val.i = 1;
2118                                                                 else if (iptr->sx.val.i == 0x00000004)
2119                                                                         iptr->sx.val.i = 2;
2120                                                                 else if (iptr->sx.val.i == 0x00000008)
2121                                                                         iptr->sx.val.i = 3;
2122                                                                 else if (iptr->sx.val.i == 0x00000010)
2123                                                                         iptr->sx.val.i = 4;
2124                                                                 else if (iptr->sx.val.i == 0x00000020)
2125                                                                         iptr->sx.val.i = 5;
2126                                                                 else if (iptr->sx.val.i == 0x00000040)
2127                                                                         iptr->sx.val.i = 6;
2128                                                                 else if (iptr->sx.val.i == 0x00000080)
2129                                                                         iptr->sx.val.i = 7;
2130                                                                 else if (iptr->sx.val.i == 0x00000100)
2131                                                                         iptr->sx.val.i = 8;
2132                                                                 else if (iptr->sx.val.i == 0x00000200)
2133                                                                         iptr->sx.val.i = 9;
2134                                                                 else if (iptr->sx.val.i == 0x00000400)
2135                                                                         iptr->sx.val.i = 10;
2136                                                                 else if (iptr->sx.val.i == 0x00000800)
2137                                                                         iptr->sx.val.i = 11;
2138                                                                 else if (iptr->sx.val.i == 0x00001000)
2139                                                                         iptr->sx.val.i = 12;
2140                                                                 else if (iptr->sx.val.i == 0x00002000)
2141                                                                         iptr->sx.val.i = 13;
2142                                                                 else if (iptr->sx.val.i == 0x00004000)
2143                                                                         iptr->sx.val.i = 14;
2144                                                                 else if (iptr->sx.val.i == 0x00008000)
2145                                                                         iptr->sx.val.i = 15;
2146                                                                 else if (iptr->sx.val.i == 0x00010000)
2147                                                                         iptr->sx.val.i = 16;
2148                                                                 else if (iptr->sx.val.i == 0x00020000)
2149                                                                         iptr->sx.val.i = 17;
2150                                                                 else if (iptr->sx.val.i == 0x00040000)
2151                                                                         iptr->sx.val.i = 18;
2152                                                                 else if (iptr->sx.val.i == 0x00080000)
2153                                                                         iptr->sx.val.i = 19;
2154                                                                 else if (iptr->sx.val.i == 0x00100000)
2155                                                                         iptr->sx.val.i = 20;
2156                                                                 else if (iptr->sx.val.i == 0x00200000)
2157                                                                         iptr->sx.val.i = 21;
2158                                                                 else if (iptr->sx.val.i == 0x00400000)
2159                                                                         iptr->sx.val.i = 22;
2160                                                                 else if (iptr->sx.val.i == 0x00800000)
2161                                                                         iptr->sx.val.i = 23;
2162                                                                 else if (iptr->sx.val.i == 0x01000000)
2163                                                                         iptr->sx.val.i = 24;
2164                                                                 else if (iptr->sx.val.i == 0x02000000)
2165                                                                         iptr->sx.val.i = 25;
2166                                                                 else if (iptr->sx.val.i == 0x04000000)
2167                                                                         iptr->sx.val.i = 26;
2168                                                                 else if (iptr->sx.val.i == 0x08000000)
2169                                                                         iptr->sx.val.i = 27;
2170                                                                 else if (iptr->sx.val.i == 0x10000000)
2171                                                                         iptr->sx.val.i = 28;
2172                                                                 else if (iptr->sx.val.i == 0x20000000)
2173                                                                         iptr->sx.val.i = 29;
2174                                                                 else if (iptr->sx.val.i == 0x40000000)
2175                                                                         iptr->sx.val.i = 30;
2176                                                                 else if (iptr->sx.val.i == 0x80000000)
2177                                                                         iptr->sx.val.i = 31;
2178                                                                 else
2179                                                                         goto normal_ICONST;
2180
2181                                                                 iptr->opc = ICMD_IMULPOW2;
2182                                                                 goto icmd_iconst_tail;
2183 #endif /* SUPPORT_CONST_MUL */
2184                                                         case ICMD_IDIV:
2185                                                                 if (iptr->sx.val.i == 0x00000002)
2186                                                                         iptr->sx.val.i = 1;
2187                                                                 else if (iptr->sx.val.i == 0x00000004)
2188                                                                         iptr->sx.val.i = 2;
2189                                                                 else if (iptr->sx.val.i == 0x00000008)
2190                                                                         iptr->sx.val.i = 3;
2191                                                                 else if (iptr->sx.val.i == 0x00000010)
2192                                                                         iptr->sx.val.i = 4;
2193                                                                 else if (iptr->sx.val.i == 0x00000020)
2194                                                                         iptr->sx.val.i = 5;
2195                                                                 else if (iptr->sx.val.i == 0x00000040)
2196                                                                         iptr->sx.val.i = 6;
2197                                                                 else if (iptr->sx.val.i == 0x00000080)
2198                                                                         iptr->sx.val.i = 7;
2199                                                                 else if (iptr->sx.val.i == 0x00000100)
2200                                                                         iptr->sx.val.i = 8;
2201                                                                 else if (iptr->sx.val.i == 0x00000200)
2202                                                                         iptr->sx.val.i = 9;
2203                                                                 else if (iptr->sx.val.i == 0x00000400)
2204                                                                         iptr->sx.val.i = 10;
2205                                                                 else if (iptr->sx.val.i == 0x00000800)
2206                                                                         iptr->sx.val.i = 11;
2207                                                                 else if (iptr->sx.val.i == 0x00001000)
2208                                                                         iptr->sx.val.i = 12;
2209                                                                 else if (iptr->sx.val.i == 0x00002000)
2210                                                                         iptr->sx.val.i = 13;
2211                                                                 else if (iptr->sx.val.i == 0x00004000)
2212                                                                         iptr->sx.val.i = 14;
2213                                                                 else if (iptr->sx.val.i == 0x00008000)
2214                                                                         iptr->sx.val.i = 15;
2215                                                                 else if (iptr->sx.val.i == 0x00010000)
2216                                                                         iptr->sx.val.i = 16;
2217                                                                 else if (iptr->sx.val.i == 0x00020000)
2218                                                                         iptr->sx.val.i = 17;
2219                                                                 else if (iptr->sx.val.i == 0x00040000)
2220                                                                         iptr->sx.val.i = 18;
2221                                                                 else if (iptr->sx.val.i == 0x00080000)
2222                                                                         iptr->sx.val.i = 19;
2223                                                                 else if (iptr->sx.val.i == 0x00100000)
2224                                                                         iptr->sx.val.i = 20;
2225                                                                 else if (iptr->sx.val.i == 0x00200000)
2226                                                                         iptr->sx.val.i = 21;
2227                                                                 else if (iptr->sx.val.i == 0x00400000)
2228                                                                         iptr->sx.val.i = 22;
2229                                                                 else if (iptr->sx.val.i == 0x00800000)
2230                                                                         iptr->sx.val.i = 23;
2231                                                                 else if (iptr->sx.val.i == 0x01000000)
2232                                                                         iptr->sx.val.i = 24;
2233                                                                 else if (iptr->sx.val.i == 0x02000000)
2234                                                                         iptr->sx.val.i = 25;
2235                                                                 else if (iptr->sx.val.i == 0x04000000)
2236                                                                         iptr->sx.val.i = 26;
2237                                                                 else if (iptr->sx.val.i == 0x08000000)
2238                                                                         iptr->sx.val.i = 27;
2239                                                                 else if (iptr->sx.val.i == 0x10000000)
2240                                                                         iptr->sx.val.i = 28;
2241                                                                 else if (iptr->sx.val.i == 0x20000000)
2242                                                                         iptr->sx.val.i = 29;
2243                                                                 else if (iptr->sx.val.i == 0x40000000)
2244                                                                         iptr->sx.val.i = 30;
2245                                                                 else if (iptr->sx.val.i == 0x80000000)
2246                                                                         iptr->sx.val.i = 31;
2247                                                                 else
2248                                                                         goto normal_ICONST;
2249
2250                                                                 iptr->opc = ICMD_IDIVPOW2;
2251                                                                 goto icmd_iconst_tail;
2252
2253                                                         case ICMD_IREM:
2254                                                                 /*log_text("stack.c: ICMD_ICONST/ICMD_IREM");*/
2255                                                                 if ((iptr->sx.val.i == 0x00000002) ||
2256                                                                         (iptr->sx.val.i == 0x00000004) ||
2257                                                                         (iptr->sx.val.i == 0x00000008) ||
2258                                                                         (iptr->sx.val.i == 0x00000010) ||
2259                                                                         (iptr->sx.val.i == 0x00000020) ||
2260                                                                         (iptr->sx.val.i == 0x00000040) ||
2261                                                                         (iptr->sx.val.i == 0x00000080) ||
2262                                                                         (iptr->sx.val.i == 0x00000100) ||
2263                                                                         (iptr->sx.val.i == 0x00000200) ||
2264                                                                         (iptr->sx.val.i == 0x00000400) ||
2265                                                                         (iptr->sx.val.i == 0x00000800) ||
2266                                                                         (iptr->sx.val.i == 0x00001000) ||
2267                                                                         (iptr->sx.val.i == 0x00002000) ||
2268                                                                         (iptr->sx.val.i == 0x00004000) ||
2269                                                                         (iptr->sx.val.i == 0x00008000) ||
2270                                                                         (iptr->sx.val.i == 0x00010000) ||
2271                                                                         (iptr->sx.val.i == 0x00020000) ||
2272                                                                         (iptr->sx.val.i == 0x00040000) ||
2273                                                                         (iptr->sx.val.i == 0x00080000) ||
2274                                                                         (iptr->sx.val.i == 0x00100000) ||
2275                                                                         (iptr->sx.val.i == 0x00200000) ||
2276                                                                         (iptr->sx.val.i == 0x00400000) ||
2277                                                                         (iptr->sx.val.i == 0x00800000) ||
2278                                                                         (iptr->sx.val.i == 0x01000000) ||
2279                                                                         (iptr->sx.val.i == 0x02000000) ||
2280                                                                         (iptr->sx.val.i == 0x04000000) ||
2281                                                                         (iptr->sx.val.i == 0x08000000) ||
2282                                                                         (iptr->sx.val.i == 0x10000000) ||
2283                                                                         (iptr->sx.val.i == 0x20000000) ||
2284                                                                         (iptr->sx.val.i == 0x40000000) ||
2285                                                                         (iptr->sx.val.i == 0x80000000))
2286                                                                 {
2287                                                                         iptr->opc = ICMD_IREMPOW2;
2288                                                                         iptr->sx.val.i -= 1;
2289                                                                         goto icmd_iconst_tail;
2290                                                                 }
2291                                                                 goto normal_ICONST;
2292 #if SUPPORT_CONST_LOGICAL
2293                                                         case ICMD_IAND:
2294                                                                 iptr->opc = ICMD_IANDCONST;
2295                                                                 goto icmd_iconst_tail;
2296
2297                                                         case ICMD_IOR:
2298                                                                 iptr->opc = ICMD_IORCONST;
2299                                                                 goto icmd_iconst_tail;
2300
2301                                                         case ICMD_IXOR:
2302                                                                 iptr->opc = ICMD_IXORCONST;
2303                                                                 goto icmd_iconst_tail;
2304
2305 #endif /* SUPPORT_CONST_LOGICAL */
2306                                                         case ICMD_ISHL:
2307                                                                 iptr->opc = ICMD_ISHLCONST;
2308                                                                 goto icmd_iconst_tail;
2309
2310                                                         case ICMD_ISHR:
2311                                                                 iptr->opc = ICMD_ISHRCONST;
2312                                                                 goto icmd_iconst_tail;
2313
2314                                                         case ICMD_IUSHR:
2315                                                                 iptr->opc = ICMD_IUSHRCONST;
2316                                                                 goto icmd_iconst_tail;
2317 #if SUPPORT_LONG_SHIFT
2318                                                         case ICMD_LSHL:
2319                                                                 iptr->opc = ICMD_LSHLCONST;
2320                                                                 goto icmd_lconst_tail;
2321
2322                                                         case ICMD_LSHR:
2323                                                                 iptr->opc = ICMD_LSHRCONST;
2324                                                                 goto icmd_lconst_tail;
2325
2326                                                         case ICMD_LUSHR:
2327                                                                 iptr->opc = ICMD_LUSHRCONST;
2328                                                                 goto icmd_lconst_tail;
2329 #endif /* SUPPORT_LONG_SHIFT */
2330                                                         case ICMD_IF_ICMPEQ:
2331                                                                 iptr[1].opc = ICMD_IFEQ;
2332                                                                 /* FALLTHROUGH */
2333
2334                                                         icmd_if_icmp_tail:
2335                                                                 /* set the constant for the following icmd */
2336                                                                 iptr[1].sx.val.i = iptr->sx.val.i;
2337
2338                                                                 /* this instruction becomes a nop */
2339                                                                 iptr->opc = ICMD_NOP;
2340                                                                 goto icmd_NOP;
2341
2342                                                         case ICMD_IF_ICMPLT:
2343                                                                 iptr[1].opc = ICMD_IFLT;
2344                                                                 goto icmd_if_icmp_tail;
2345
2346                                                         case ICMD_IF_ICMPLE:
2347                                                                 iptr[1].opc = ICMD_IFLE;
2348                                                                 goto icmd_if_icmp_tail;
2349
2350                                                         case ICMD_IF_ICMPNE:
2351                                                                 iptr[1].opc = ICMD_IFNE;
2352                                                                 goto icmd_if_icmp_tail;
2353
2354                                                         case ICMD_IF_ICMPGT:
2355                                                                 iptr[1].opc = ICMD_IFGT;
2356                                                                 goto icmd_if_icmp_tail;
2357
2358                                                         case ICMD_IF_ICMPGE:
2359                                                                 iptr[1].opc = ICMD_IFGE;
2360                                                                 goto icmd_if_icmp_tail;
2361
2362 #if SUPPORT_CONST_STORE
2363                                                         case ICMD_IASTORE:
2364                                                         case ICMD_BASTORE:
2365                                                         case ICMD_CASTORE:
2366                                                         case ICMD_SASTORE:
2367                                                                 IF_INTRP( goto normal_ICONST; )
2368 # if SUPPORT_CONST_STORE_ZERO_ONLY
2369                                                                 if (iptr->sx.val.i != 0)
2370                                                                         goto normal_ICONST;
2371 # endif
2372                                                                 switch (iptr[1].opc) {
2373                                                                         case ICMD_IASTORE:
2374                                                                                 iptr->opc = ICMD_IASTORECONST;
2375                                                                                 iptr->flags.bits |= INS_FLAG_CHECK;
2376                                                                                 break;
2377                                                                         case ICMD_BASTORE:
2378                                                                                 iptr->opc = ICMD_BASTORECONST;
2379                                                                                 iptr->flags.bits |= INS_FLAG_CHECK;
2380                                                                                 break;
2381                                                                         case ICMD_CASTORE:
2382                                                                                 iptr->opc = ICMD_CASTORECONST;
2383                                                                                 iptr->flags.bits |= INS_FLAG_CHECK;
2384                                                                                 break;
2385                                                                         case ICMD_SASTORE:
2386                                                                                 iptr->opc = ICMD_SASTORECONST;
2387                                                                                 iptr->flags.bits |= INS_FLAG_CHECK;
2388                                                                                 break;
2389                                                                 }
2390
2391                                                                 iptr[1].opc = ICMD_NOP;
2392
2393                                                                 /* copy the constant to s3 */
2394                                                                 /* XXX constval -> astoreconstval? */
2395                                                                 iptr->sx.s23.s3.constval = iptr->sx.val.i;
2396                                                                 OP2_0(TYPE_ADR, TYPE_INT);
2397                                                                 COUNT(count_pcmd_op);
2398                                                                 break;
2399
2400                                                         case ICMD_PUTSTATIC:
2401                                                         case ICMD_PUTFIELD:
2402                                                                 IF_INTRP( goto normal_ICONST; )
2403 # if SUPPORT_CONST_STORE_ZERO_ONLY
2404                                                                 if (iptr->sx.val.i != 0)
2405                                                                         goto normal_ICONST;
2406 # endif
2407                                                                 /* XXX check field type? */
2408
2409                                                                 /* copy the constant to s2 */
2410                                                                 /* XXX constval -> fieldconstval? */
2411                                                                 iptr->sx.s23.s2.constval = iptr->sx.val.i;
2412
2413 putconst_tail:
2414                                                                 /* set the field reference (s3) */
2415                                                                 if (iptr[1].flags.bits & INS_FLAG_UNRESOLVED) {
2416                                                                         iptr->sx.s23.s3.uf = iptr[1].sx.s23.s3.uf;
2417                                                                         iptr->flags.bits |= INS_FLAG_UNRESOLVED;
2418                                                                 }
2419                                                                 else {
2420                                                                         iptr->sx.s23.s3.fmiref = iptr[1].sx.s23.s3.fmiref;
2421                                                                 }
2422                                                                 
2423                                                                 switch (iptr[1].opc) {
2424                                                                         case ICMD_PUTSTATIC:
2425                                                                                 iptr->opc = ICMD_PUTSTATICCONST;
2426                                                                                 OP0_0;
2427                                                                                 break;
2428                                                                         case ICMD_PUTFIELD:
2429                                                                                 iptr->opc = ICMD_PUTFIELDCONST;
2430                                                                                 OP1_0(TYPE_ADR);
2431                                                                                 break;
2432                                                                 }
2433
2434                                                                 iptr[1].opc = ICMD_NOP;
2435                                                                 COUNT(count_pcmd_op);
2436                                                                 break;
2437 #endif /* SUPPORT_CONST_STORE */
2438
2439                                                         default:
2440                                                                 goto normal_ICONST;
2441                                                 }
2442
2443                                                 /* if we get here, the ICONST has been optimized */
2444                                                 break;
2445
2446 normal_ICONST:
2447                                                 /* normal case of an unoptimized ICONST */
2448                                                 OP0_1(TYPE_INT);
2449                                                 break;
2450
2451         /************************** LCONST OPTIMIZATIONS **************************/
2452
2453                                         case ICMD_LCONST:
2454                                                 COUNT(count_pcmd_load);
2455                                                 if (len == 0)
2456                                                         goto normal_LCONST;
2457
2458                                                 /* switch depending on the following instruction */
2459
2460                                                 switch (iptr[1].opc) {
2461 #if SUPPORT_LONG_ADD
2462                                                         case ICMD_LADD:
2463                                                                 iptr->opc = ICMD_LADDCONST;
2464                                                                 /* FALLTHROUGH */
2465
2466                                                         icmd_lconst_tail:
2467                                                                 /* instruction of type LONG -> LONG */
2468                                                                 iptr[1].opc = ICMD_NOP;
2469                                                                 OP1_1(TYPE_LNG, TYPE_LNG);
2470                                                                 COUNT(count_pcmd_op);
2471                                                                 break;
2472
2473                                                         case ICMD_LSUB:
2474                                                                 iptr->opc = ICMD_LSUBCONST;
2475                                                                 goto icmd_lconst_tail;
2476
2477 #endif /* SUPPORT_LONG_ADD */
2478 #if SUPPORT_LONG_MUL && SUPPORT_CONST_MUL
2479                                                         case ICMD_LMUL:
2480                                                                 iptr->opc = ICMD_LMULCONST;
2481                                                                 goto icmd_lconst_tail;
2482 #else /* SUPPORT_LONG_MUL && SUPPORT_CONST_MUL */
2483 # if SUPPORT_LONG_SHIFT
2484                                                         case ICMD_LMUL:
2485                                                                 if (iptr->sx.val.l == 0x00000002)
2486                                                                         iptr->sx.val.i = 1;
2487                                                                 else if (iptr->sx.val.l == 0x00000004)
2488                                                                         iptr->sx.val.i = 2;
2489                                                                 else if (iptr->sx.val.l == 0x00000008)
2490                                                                         iptr->sx.val.i = 3;
2491                                                                 else if (iptr->sx.val.l == 0x00000010)
2492                                                                         iptr->sx.val.i = 4;
2493                                                                 else if (iptr->sx.val.l == 0x00000020)
2494                                                                         iptr->sx.val.i = 5;
2495                                                                 else if (iptr->sx.val.l == 0x00000040)
2496                                                                         iptr->sx.val.i = 6;
2497                                                                 else if (iptr->sx.val.l == 0x00000080)
2498                                                                         iptr->sx.val.i = 7;
2499                                                                 else if (iptr->sx.val.l == 0x00000100)
2500                                                                         iptr->sx.val.i = 8;
2501                                                                 else if (iptr->sx.val.l == 0x00000200)
2502                                                                         iptr->sx.val.i = 9;
2503                                                                 else if (iptr->sx.val.l == 0x00000400)
2504                                                                         iptr->sx.val.i = 10;
2505                                                                 else if (iptr->sx.val.l == 0x00000800)
2506                                                                         iptr->sx.val.i = 11;
2507                                                                 else if (iptr->sx.val.l == 0x00001000)
2508                                                                         iptr->sx.val.i = 12;
2509                                                                 else if (iptr->sx.val.l == 0x00002000)
2510                                                                         iptr->sx.val.i = 13;
2511                                                                 else if (iptr->sx.val.l == 0x00004000)
2512                                                                         iptr->sx.val.i = 14;
2513                                                                 else if (iptr->sx.val.l == 0x00008000)
2514                                                                         iptr->sx.val.i = 15;
2515                                                                 else if (iptr->sx.val.l == 0x00010000)
2516                                                                         iptr->sx.val.i = 16;
2517                                                                 else if (iptr->sx.val.l == 0x00020000)
2518                                                                         iptr->sx.val.i = 17;
2519                                                                 else if (iptr->sx.val.l == 0x00040000)
2520                                                                         iptr->sx.val.i = 18;
2521                                                                 else if (iptr->sx.val.l == 0x00080000)
2522                                                                         iptr->sx.val.i = 19;
2523                                                                 else if (iptr->sx.val.l == 0x00100000)
2524                                                                         iptr->sx.val.i = 20;
2525                                                                 else if (iptr->sx.val.l == 0x00200000)
2526                                                                         iptr->sx.val.i = 21;
2527                                                                 else if (iptr->sx.val.l == 0x00400000)
2528                                                                         iptr->sx.val.i = 22;
2529                                                                 else if (iptr->sx.val.l == 0x00800000)
2530                                                                         iptr->sx.val.i = 23;
2531                                                                 else if (iptr->sx.val.l == 0x01000000)
2532                                                                         iptr->sx.val.i = 24;
2533                                                                 else if (iptr->sx.val.l == 0x02000000)
2534                                                                         iptr->sx.val.i = 25;
2535                                                                 else if (iptr->sx.val.l == 0x04000000)
2536                                                                         iptr->sx.val.i = 26;
2537                                                                 else if (iptr->sx.val.l == 0x08000000)
2538                                                                         iptr->sx.val.i = 27;
2539                                                                 else if (iptr->sx.val.l == 0x10000000)
2540                                                                         iptr->sx.val.i = 28;
2541                                                                 else if (iptr->sx.val.l == 0x20000000)
2542                                                                         iptr->sx.val.i = 29;
2543                                                                 else if (iptr->sx.val.l == 0x40000000)
2544                                                                         iptr->sx.val.i = 30;
2545                                                                 else if (iptr->sx.val.l == 0x80000000)
2546                                                                         iptr->sx.val.i = 31;
2547                                                                 else {
2548                                                                         goto normal_LCONST;
2549                                                                 }
2550                                                                 iptr->opc = ICMD_LMULPOW2;
2551                                                                 goto icmd_lconst_tail;
2552 # endif /* SUPPORT_LONG_SHIFT */
2553 #endif /* SUPPORT_LONG_MUL && SUPPORT_CONST_MUL */
2554 #if SUPPORT_LONG_DIV_POW2
2555                                                         case ICMD_LDIV:
2556                                                                 if (iptr->sx.val.l == 0x00000002)
2557                                                                         iptr->sx.val.i = 1;
2558                                                                 else if (iptr->sx.val.l == 0x00000004)
2559                                                                         iptr->sx.val.i = 2;
2560                                                                 else if (iptr->sx.val.l == 0x00000008)
2561                                                                         iptr->sx.val.i = 3;
2562                                                                 else if (iptr->sx.val.l == 0x00000010)
2563                                                                         iptr->sx.val.i = 4;
2564                                                                 else if (iptr->sx.val.l == 0x00000020)
2565                                                                         iptr->sx.val.i = 5;
2566                                                                 else if (iptr->sx.val.l == 0x00000040)
2567                                                                         iptr->sx.val.i = 6;
2568                                                                 else if (iptr->sx.val.l == 0x00000080)
2569                                                                         iptr->sx.val.i = 7;
2570                                                                 else if (iptr->sx.val.l == 0x00000100)
2571                                                                         iptr->sx.val.i = 8;
2572                                                                 else if (iptr->sx.val.l == 0x00000200)
2573                                                                         iptr->sx.val.i = 9;
2574                                                                 else if (iptr->sx.val.l == 0x00000400)
2575                                                                         iptr->sx.val.i = 10;
2576                                                                 else if (iptr->sx.val.l == 0x00000800)
2577                                                                         iptr->sx.val.i = 11;
2578                                                                 else if (iptr->sx.val.l == 0x00001000)
2579                                                                         iptr->sx.val.i = 12;
2580                                                                 else if (iptr->sx.val.l == 0x00002000)
2581                                                                         iptr->sx.val.i = 13;
2582                                                                 else if (iptr->sx.val.l == 0x00004000)
2583                                                                         iptr->sx.val.i = 14;
2584                                                                 else if (iptr->sx.val.l == 0x00008000)
2585                                                                         iptr->sx.val.i = 15;
2586                                                                 else if (iptr->sx.val.l == 0x00010000)
2587                                                                         iptr->sx.val.i = 16;
2588                                                                 else if (iptr->sx.val.l == 0x00020000)
2589                                                                         iptr->sx.val.i = 17;
2590                                                                 else if (iptr->sx.val.l == 0x00040000)
2591                                                                         iptr->sx.val.i = 18;
2592                                                                 else if (iptr->sx.val.l == 0x00080000)
2593                                                                         iptr->sx.val.i = 19;
2594                                                                 else if (iptr->sx.val.l == 0x00100000)
2595                                                                         iptr->sx.val.i = 20;
2596                                                                 else if (iptr->sx.val.l == 0x00200000)
2597                                                                         iptr->sx.val.i = 21;
2598                                                                 else if (iptr->sx.val.l == 0x00400000)
2599                                                                         iptr->sx.val.i = 22;
2600                                                                 else if (iptr->sx.val.l == 0x00800000)
2601                                                                         iptr->sx.val.i = 23;
2602                                                                 else if (iptr->sx.val.l == 0x01000000)
2603                                                                         iptr->sx.val.i = 24;
2604                                                                 else if (iptr->sx.val.l == 0x02000000)
2605                                                                         iptr->sx.val.i = 25;
2606                                                                 else if (iptr->sx.val.l == 0x04000000)
2607                                                                         iptr->sx.val.i = 26;
2608                                                                 else if (iptr->sx.val.l == 0x08000000)
2609                                                                         iptr->sx.val.i = 27;
2610                                                                 else if (iptr->sx.val.l == 0x10000000)
2611                                                                         iptr->sx.val.i = 28;
2612                                                                 else if (iptr->sx.val.l == 0x20000000)
2613                                                                         iptr->sx.val.i = 29;
2614                                                                 else if (iptr->sx.val.l == 0x40000000)
2615                                                                         iptr->sx.val.i = 30;
2616                                                                 else if (iptr->sx.val.l == 0x80000000)
2617                                                                         iptr->sx.val.i = 31;
2618                                                                 else {
2619                                                                         goto normal_LCONST;
2620                                                                 }
2621                                                                 iptr->opc = ICMD_LDIVPOW2;
2622                                                                 goto icmd_lconst_tail;
2623 #endif /* SUPPORT_LONG_DIV_POW2 */
2624
2625 #if SUPPORT_LONG_REM_POW2
2626                                                         case ICMD_LREM:
2627                                                                 if ((iptr->sx.val.l == 0x00000002) ||
2628                                                                         (iptr->sx.val.l == 0x00000004) ||
2629                                                                         (iptr->sx.val.l == 0x00000008) ||
2630                                                                         (iptr->sx.val.l == 0x00000010) ||
2631                                                                         (iptr->sx.val.l == 0x00000020) ||
2632                                                                         (iptr->sx.val.l == 0x00000040) ||
2633                                                                         (iptr->sx.val.l == 0x00000080) ||
2634                                                                         (iptr->sx.val.l == 0x00000100) ||
2635                                                                         (iptr->sx.val.l == 0x00000200) ||
2636                                                                         (iptr->sx.val.l == 0x00000400) ||
2637                                                                         (iptr->sx.val.l == 0x00000800) ||
2638                                                                         (iptr->sx.val.l == 0x00001000) ||
2639                                                                         (iptr->sx.val.l == 0x00002000) ||
2640                                                                         (iptr->sx.val.l == 0x00004000) ||
2641                                                                         (iptr->sx.val.l == 0x00008000) ||
2642                                                                         (iptr->sx.val.l == 0x00010000) ||
2643                                                                         (iptr->sx.val.l == 0x00020000) ||
2644                                                                         (iptr->sx.val.l == 0x00040000) ||
2645                                                                         (iptr->sx.val.l == 0x00080000) ||
2646                                                                         (iptr->sx.val.l == 0x00100000) ||
2647                                                                         (iptr->sx.val.l == 0x00200000) ||
2648                                                                         (iptr->sx.val.l == 0x00400000) ||
2649                                                                         (iptr->sx.val.l == 0x00800000) ||
2650                                                                         (iptr->sx.val.l == 0x01000000) ||
2651                                                                         (iptr->sx.val.l == 0x02000000) ||
2652                                                                         (iptr->sx.val.l == 0x04000000) ||
2653                                                                         (iptr->sx.val.l == 0x08000000) ||
2654                                                                         (iptr->sx.val.l == 0x10000000) ||
2655                                                                         (iptr->sx.val.l == 0x20000000) ||
2656                                                                         (iptr->sx.val.l == 0x40000000) ||
2657                                                                         (iptr->sx.val.l == 0x80000000))
2658                                                                 {
2659                                                                         iptr->opc = ICMD_LREMPOW2;
2660                                                                         iptr->sx.val.l -= 1;
2661                                                                         goto icmd_lconst_tail;
2662                                                                 }
2663                                                                 goto normal_LCONST;
2664 #endif /* SUPPORT_LONG_REM_POW2 */
2665
2666 #if SUPPORT_LONG_LOGICAL && SUPPORT_CONST_LOGICAL
2667
2668                                                         case ICMD_LAND:
2669                                                                 iptr->opc = ICMD_LANDCONST;
2670                                                                 goto icmd_lconst_tail;
2671
2672                                                         case ICMD_LOR:
2673                                                                 iptr->opc = ICMD_LORCONST;
2674                                                                 goto icmd_lconst_tail;
2675
2676                                                         case ICMD_LXOR:
2677                                                                 iptr->opc = ICMD_LXORCONST;
2678                                                                 goto icmd_lconst_tail;
2679 #endif /* SUPPORT_LONG_LOGICAL && SUPPORT_CONST_LOGICAL */
2680
2681 #if SUPPORT_LONG_CMP_CONST
2682                                                         case ICMD_LCMP:
2683                                                                 if ((len <= 1) || (iptr[2].sx.val.i != 0))
2684                                                                         goto normal_LCONST;
2685
2686                                                                 /* switch on the instruction after LCONST - LCMP */
2687
2688                                                                 switch (iptr[2].opc) {
2689                                                                         case ICMD_IFEQ:
2690                                                                                 iptr->opc = ICMD_IF_LEQ;
2691                                                                                 /* FALLTHROUGH */
2692
2693                                                                         icmd_lconst_lcmp_tail:
2694                                                                                 /* convert LCONST, LCMP, IFXX to IF_LXX */
2695                                                                                 iptr->dst.insindex = iptr[2].dst.insindex;
2696                                                                                 iptr[1].opc = ICMD_NOP;
2697                                                                                 iptr[2].opc = ICMD_NOP;
2698
2699                                                                                 OP1_BRANCH(TYPE_LNG);
2700                                                                                 BRANCH(tbptr, copy);
2701                                                                                 COUNT(count_pcmd_bra);
2702                                                                                 COUNT(count_pcmd_op);
2703                                                                                 break;
2704
2705                                                                         case ICMD_IFNE:
2706                                                                                 iptr->opc = ICMD_IF_LNE;
2707                                                                                 goto icmd_lconst_lcmp_tail;
2708
2709                                                                         case ICMD_IFLT:
2710                                                                                 iptr->opc = ICMD_IF_LLT;
2711                                                                                 goto icmd_lconst_lcmp_tail;
2712
2713                                                                         case ICMD_IFGT:
2714                                                                                 iptr->opc = ICMD_IF_LGT;
2715                                                                                 goto icmd_lconst_lcmp_tail;
2716
2717                                                                         case ICMD_IFLE:
2718                                                                                 iptr->opc = ICMD_IF_LLE;
2719                                                                                 goto icmd_lconst_lcmp_tail;
2720
2721                                                                         case ICMD_IFGE:
2722                                                                                 iptr->opc = ICMD_IF_LGE;
2723                                                                                 goto icmd_lconst_lcmp_tail;
2724
2725                                                                         default:
2726                                                                                 goto normal_LCONST;
2727                                                                 } /* end switch on opcode after LCONST - LCMP */
2728                                                                 break;
2729 #endif /* SUPPORT_LONG_CMP_CONST */
2730
2731 #if SUPPORT_CONST_STORE
2732                                                         case ICMD_LASTORE:
2733                                                                 IF_INTRP( goto normal_LCONST; )
2734 # if SUPPORT_CONST_STORE_ZERO_ONLY
2735                                                                 if (iptr->sx.val.l != 0)
2736                                                                         goto normal_LCONST;
2737 # endif
2738 #if SIZEOF_VOID_P == 4
2739                                                                 /* the constant must fit into a ptrint */
2740                                                                 if (iptr->sx.val.l < -0x80000000L || iptr->sx.val.l >= 0x80000000L)
2741                                                                         goto normal_LCONST;
2742 #endif
2743                                                                 /* move the constant to s3 */
2744                                                                 iptr->sx.s23.s3.constval = iptr->sx.val.l;
2745
2746                                                                 iptr->opc = ICMD_LASTORECONST;
2747                                                                 iptr->flags.bits |= INS_FLAG_CHECK;
2748                                                                 OP2_0(TYPE_ADR, TYPE_INT);
2749
2750                                                                 iptr[1].opc = ICMD_NOP;
2751                                                                 COUNT(count_pcmd_op);
2752                                                                 break;
2753
2754                                                         case ICMD_PUTSTATIC:
2755                                                         case ICMD_PUTFIELD:
2756                                                                 IF_INTRP( goto normal_LCONST; )
2757 # if SUPPORT_CONST_STORE_ZERO_ONLY
2758                                                                 if (iptr->sx.val.l != 0)
2759                                                                         goto normal_LCONST;
2760 # endif
2761 #if SIZEOF_VOID_P == 4
2762                                                                 /* the constant must fit into a ptrint */
2763                                                                 if (iptr->sx.val.l < -0x80000000L || iptr->sx.val.l >= 0x80000000L)
2764                                                                         goto normal_LCONST;
2765 #endif
2766                                                                 /* XXX check field type? */
2767
2768                                                                 /* copy the constant to s2 */
2769                                                                 /* XXX constval -> fieldconstval? */
2770                                                                 iptr->sx.s23.s2.constval = iptr->sx.val.l;
2771
2772                                                                 goto putconst_tail;
2773
2774 #endif /* SUPPORT_CONST_STORE */
2775
2776                                                         default:
2777                                                                 goto normal_LCONST;
2778                                                 } /* end switch opcode after LCONST */
2779
2780                                                 /* if we get here, the LCONST has been optimized */
2781                                                 break;
2782
2783 normal_LCONST:
2784                                                 /* the normal case of an unoptimized LCONST */
2785                                                 OP0_1(TYPE_LNG);
2786                                                 break;
2787
2788         /************************ END OF LCONST OPTIMIZATIONS *********************/
2789
2790                                         case ICMD_FCONST:
2791                                                 COUNT(count_pcmd_load);
2792                                                 OP0_1(TYPE_FLT);
2793                                                 break;
2794
2795                                         case ICMD_DCONST:
2796                                                 COUNT(count_pcmd_load);
2797                                                 OP0_1(TYPE_DBL);
2798                                                 break;
2799
2800         /************************** ACONST OPTIMIZATIONS **************************/
2801
2802                                         case ICMD_ACONST:
2803                                                 coalescing_boundary = sd.new;
2804                                                 COUNT(count_pcmd_load);
2805 #if SUPPORT_CONST_STORE
2806                                                 IF_INTRP( goto normal_ACONST; )
2807
2808                                                 /* We can only optimize if the ACONST is resolved
2809                                                  * and there is an instruction after it. */
2810
2811                                                 if ((len == 0) || (iptr->flags.bits & INS_FLAG_UNRESOLVED))
2812                                                         goto normal_ACONST;
2813
2814                                                 switch (iptr[1].opc) {
2815                                                         case ICMD_AASTORE:
2816                                                                 /* We can only optimize for NULL values
2817                                                                  * here because otherwise a checkcast is
2818                                                                  * required. */
2819                                                                 if (iptr->sx.val.anyptr != NULL)
2820                                                                         goto normal_ACONST;
2821
2822                                                                 /* copy the constant (NULL) to s3 */
2823                                                                 iptr->sx.s23.s3.constval = 0;
2824                                                                 iptr->opc = ICMD_AASTORECONST;
2825                                                                 iptr->flags.bits |= INS_FLAG_CHECK;
2826                                                                 OP2_0(TYPE_ADR, TYPE_INT);
2827
2828                                                                 iptr[1].opc = ICMD_NOP;
2829                                                                 COUNT(count_pcmd_op);
2830                                                                 break;
2831
2832                                                         case ICMD_PUTSTATIC:
2833                                                         case ICMD_PUTFIELD:
2834 # if SUPPORT_CONST_STORE_ZERO_ONLY
2835                                                                 if (iptr->sx.val.anyptr != NULL)
2836                                                                         goto normal_ACONST;
2837 # endif
2838                                                                 /* XXX check field type? */
2839                                                                 /* copy the constant to s2 */
2840                                                                 /* XXX constval -> fieldconstval? */
2841                                                                 iptr->sx.s23.s2.constval = (ptrint) iptr->sx.val.anyptr;
2842
2843                                                                 goto putconst_tail;
2844
2845                                                         default:
2846                                                                 goto normal_ACONST;
2847                                                 }
2848
2849                                                 /* if we get here the ACONST has been optimized */
2850                                                 break;
2851
2852 normal_ACONST:
2853 #endif /* SUPPORT_CONST_STORE */
2854                                                 OP0_1(TYPE_ADR);
2855                                                 break;
2856
2857
2858                                                 /* pop 0 push 1 load */
2859
2860                                         case ICMD_ILOAD:
2861                                         case ICMD_LLOAD:
2862                                         case ICMD_FLOAD:
2863                                         case ICMD_DLOAD:
2864                                         case ICMD_ALOAD:
2865                                                 COUNT(count_load_instruction);
2866                                                 i = opcode - ICMD_ILOAD; /* type */
2867
2868                                                 j = iptr->s1.varindex = 
2869                                                         jd->local_map[iptr->s1.varindex * 5 + i];
2870
2871                                                 if (sd.var[j].type == TYPE_RET) {
2872                                                         exceptions_throw_verifyerror(m, "forbidden load of returnAddress");
2873                                                         return false;
2874                                                 }
2875                 
2876                                                 LOAD(i, j);
2877                                                 break;
2878
2879                                                 /* pop 2 push 1 */
2880
2881                                         case ICMD_LALOAD:
2882                                         case ICMD_FALOAD:
2883                                         case ICMD_DALOAD:
2884                                         case ICMD_AALOAD:
2885                                                 coalescing_boundary = sd.new;
2886                                                 iptr->flags.bits |= INS_FLAG_CHECK;
2887                                                 COUNT(count_check_null);
2888                                                 COUNT(count_check_bound);
2889                                                 COUNT(count_pcmd_mem);
2890                                                 OP2_1(TYPE_ADR, TYPE_INT, opcode - ICMD_IALOAD);
2891                                                 break;
2892
2893                                         case ICMD_IALOAD:
2894                                         case ICMD_BALOAD:
2895                                         case ICMD_CALOAD:
2896                                         case ICMD_SALOAD:
2897                                                 coalescing_boundary = sd.new;
2898                                                 iptr->flags.bits |= INS_FLAG_CHECK;
2899                                                 COUNT(count_check_null);
2900                                                 COUNT(count_check_bound);
2901                                                 COUNT(count_pcmd_mem);
2902                                                 OP2_1(TYPE_ADR, TYPE_INT, TYPE_INT);
2903                                                 break;
2904
2905                                                 /* pop 0 push 0 iinc */
2906
2907                                         case ICMD_IINC:
2908                                                 STATISTICS_STACKDEPTH_DISTRIBUTION(count_store_depth);
2909
2910                                                 last_store_boundary[iptr->s1.varindex] = sd.new;
2911
2912                                                 iptr->s1.varindex = 
2913                                                         jd->local_map[iptr->s1.varindex * 5 + TYPE_INT];
2914
2915                                                 copy = curstack;
2916                                                 i = stackdepth - 1;
2917                                                 while (copy) {
2918                                                         if ((copy->varkind == LOCALVAR) &&
2919                                                                 (copy->varnum == iptr->s1.varindex))
2920                                                         {
2921                                                                 assert(IS_LOCALVAR(copy));
2922                                                                 SET_TEMPVAR(copy);
2923                                                         }
2924                                                         i--;
2925                                                         copy = copy->prev;
2926                                                 }
2927
2928                                                 iptr->dst.varindex = iptr->s1.varindex;
2929                                                 break;
2930
2931                                                 /* pop 1 push 0 store */
2932
2933                                         case ICMD_ISTORE:
2934                                         case ICMD_LSTORE:
2935                                         case ICMD_FSTORE:
2936                                         case ICMD_DSTORE:
2937                                         case ICMD_ASTORE:
2938                                                 REQUIRE(1);
2939
2940                                                 i = opcode - ICMD_ISTORE; /* type */
2941                                                 javaindex = iptr->dst.varindex;
2942                                                 j = iptr->dst.varindex = 
2943                                                         jd->local_map[javaindex * 5 + i];
2944
2945                                                 COPY_VAL_AND_TYPE(sd, curstack->varnum, j);
2946
2947 #if defined(ENABLE_STATISTICS)
2948                                                 if (opt_stat) {
2949                                                         count_pcmd_store++;
2950                                                         i = sd.new - curstack;
2951                                                         if (i >= 20)
2952                                                                 count_store_length[20]++;
2953                                                         else
2954                                                                 count_store_length[i]++;
2955                                                         i = stackdepth - 1;
2956                                                         if (i >= 10)
2957                                                                 count_store_depth[10]++;
2958                                                         else
2959                                                                 count_store_depth[i]++;
2960                                                 }
2961 #endif
2962                                                 /* check for conflicts as described in Figure 5.2 */
2963
2964                                                 copy = curstack->prev;
2965                                                 i = stackdepth - 2;
2966                                                 while (copy) {
2967                                                         if ((copy->varkind == LOCALVAR) &&
2968                                                                 (copy->varnum == j))
2969                                                         {
2970                                                                 copy->varkind = TEMPVAR;
2971                                                                 assert(IS_LOCALVAR(copy));
2972                                                                 SET_TEMPVAR(copy);
2973                                                         }
2974                                                         i--;
2975                                                         copy = copy->prev;
2976                                                 }
2977
2978                                                 /* if the variable is already coalesced, don't bother */
2979
2980                                                 if (IS_OUTVAR(curstack)
2981                                                         || (curstack->varkind == LOCALVAR 
2982                                                                 && curstack->varnum != j))
2983                                                         goto store_tail;
2984
2985                                                 /* there is no STORE Lj while curstack is live */
2986
2987                                                 if (curstack < last_store_boundary[javaindex])
2988                                                         goto assume_conflict;
2989
2990                                                 /* curstack must be after the coalescing boundary */
2991
2992                                                 if (curstack < coalescing_boundary)
2993                                                         goto assume_conflict;
2994
2995                                                 /* there is no DEF LOCALVAR(j) while curstack is live */
2996
2997                                                 copy = sd.new; /* most recent stackslot created + 1 */
2998                                                 while (--copy > curstack) {
2999                                                         if (copy->varkind == LOCALVAR && copy->varnum == j)
3000                                                                 goto assume_conflict;
3001                                                 }
3002
3003                                                 /* coalesce the temporary variable with Lj */
3004                                                 assert((curstack->varkind == TEMPVAR)
3005                                                                         || (curstack->varkind == UNDEFVAR));
3006                                                 assert(!IS_LOCALVAR(curstack));
3007                                                 assert(!IS_OUTVAR(curstack));
3008                                                 assert(!IS_PREALLOC(curstack));
3009
3010                                                 assert(curstack->creator);
3011                                                 assert(curstack->creator->dst.varindex == curstack->varnum);
3012                                                 RELEASE_INDEX(sd, curstack);
3013                                                 curstack->varkind = LOCALVAR;
3014                                                 curstack->varnum = j;
3015                                                 curstack->creator->dst.varindex = j;
3016                                                 goto store_tail;
3017
3018                                                 /* revert the coalescing, if it has been done earlier */
3019 assume_conflict:
3020                                                 if ((curstack->varkind == LOCALVAR)
3021                                                         && (curstack->varnum == j))
3022                                                 {
3023                                                         assert(IS_LOCALVAR(curstack));
3024                                                         SET_TEMPVAR(curstack);
3025                                                 }
3026
3027                                                 /* remember the stack boundary at this store */
3028 store_tail:
3029                                                 last_store_boundary[javaindex] = sd.new;
3030
3031                                                 STORE(opcode - ICMD_ISTORE, j);
3032                                                 break;
3033
3034                                         /* pop 3 push 0 */
3035
3036                                         case ICMD_AASTORE:
3037                                                 coalescing_boundary = sd.new;
3038                                                 iptr->flags.bits |= INS_FLAG_CHECK;
3039                                                 COUNT(count_check_null);
3040                                                 COUNT(count_check_bound);
3041                                                 COUNT(count_pcmd_mem);
3042
3043                                                 bte = builtintable_get_internal(BUILTIN_canstore);
3044                                                 md = bte->md;
3045
3046                                                 if (md->memuse > rd->memuse)
3047                                                         rd->memuse = md->memuse;
3048                                                 if (md->argintreguse > rd->argintreguse)
3049                                                         rd->argintreguse = md->argintreguse;
3050                                                 /* XXX non-leaf method? */
3051
3052                                                 /* make all stack variables saved */
3053
3054                                                 copy = curstack;
3055                                                 while (copy) {
3056                                                         sd.var[copy->varnum].flags |= SAVEDVAR;
3057                                                         /* in case copy->varnum is/will be a LOCALVAR */
3058                                                         /* once and set back to a non LOCALVAR        */
3059                                                         /* the correct SAVEDVAR flag has to be        */
3060                                                         /* remembered in copy->flags, too             */
3061                                                         copy->flags |= SAVEDVAR;
3062                                                         copy = copy->prev;
3063                                                 }
3064
3065                                                 OP3_0(TYPE_ADR, TYPE_INT, TYPE_ADR);
3066                                                 break;
3067
3068
3069                                         case ICMD_LASTORE:
3070                                         case ICMD_FASTORE:
3071                                         case ICMD_DASTORE:
3072                                                 coalescing_boundary = sd.new;
3073                                                 iptr->flags.bits |= INS_FLAG_CHECK;
3074                                                 COUNT(count_check_null);
3075                                                 COUNT(count_check_bound);
3076                                                 COUNT(count_pcmd_mem);
3077                                                 OP3_0(TYPE_ADR, TYPE_INT, opcode - ICMD_IASTORE);
3078                                                 break;
3079
3080                                         case ICMD_IASTORE:
3081                                         case ICMD_BASTORE:
3082                                         case ICMD_CASTORE:
3083                                         case ICMD_SASTORE:
3084                                                 coalescing_boundary = sd.new;
3085                                                 iptr->flags.bits |= INS_FLAG_CHECK;
3086                                                 COUNT(count_check_null);
3087                                                 COUNT(count_check_bound);
3088                                                 COUNT(count_pcmd_mem);
3089                                                 OP3_0(TYPE_ADR, TYPE_INT, TYPE_INT);
3090                                                 break;
3091
3092                                                 /* pop 1 push 0 */
3093
3094                                         case ICMD_POP:
3095 #ifdef ENABLE_VERIFIER
3096                                                 if (opt_verify) {
3097                                                         REQUIRE(1);
3098                                                         if (IS_2_WORD_TYPE(curstack->type))
3099                                                                 goto throw_stack_category_error;
3100                                                 }
3101 #endif
3102                                                 OP1_0_ANY;
3103                                                 break;
3104
3105                                         case ICMD_IRETURN:
3106                                         case ICMD_LRETURN:
3107                                         case ICMD_FRETURN:
3108                                         case ICMD_DRETURN:
3109                                         case ICMD_ARETURN:
3110                                                 coalescing_boundary = sd.new;
3111                                                 IF_JIT( md_return_alloc(jd, curstack); )
3112                                                 COUNT(count_pcmd_return);
3113                                                 OP1_0(opcode - ICMD_IRETURN);
3114                                                 superblockend = true;
3115                                                 break;
3116
3117                                         case ICMD_ATHROW:
3118                                                 coalescing_boundary = sd.new;
3119                                                 COUNT(count_check_null);
3120                                                 OP1_0(TYPE_ADR);
3121                                                 curstack = NULL; stackdepth = 0;
3122                                                 superblockend = true;
3123                                                 break;
3124
3125                                         case ICMD_PUTSTATIC:
3126                                                 coalescing_boundary = sd.new;
3127                                                 COUNT(count_pcmd_mem);
3128                                                 INSTRUCTION_GET_FIELDREF(iptr, fmiref);
3129                                                 OP1_0(fmiref->parseddesc.fd->type);
3130                                                 break;
3131
3132                                                 /* pop 1 push 0 branch */
3133
3134                                         case ICMD_IFNULL:
3135                                         case ICMD_IFNONNULL:
3136                                                 COUNT(count_pcmd_bra);
3137                                                 OP1_BRANCH(TYPE_ADR);
3138                                                 BRANCH(tbptr, copy);
3139                                                 break;
3140
3141                                         case ICMD_IFEQ:
3142                                         case ICMD_IFNE:
3143                                         case ICMD_IFLT:
3144                                         case ICMD_IFGE:
3145                                         case ICMD_IFGT:
3146                                         case ICMD_IFLE:
3147                                                 COUNT(count_pcmd_bra);
3148                                                 /* iptr->sx.val.i is set implicitly in parse by
3149                                                    clearing the memory or from IF_ICMPxx
3150                                                    optimization. */
3151
3152                                                 OP1_BRANCH(TYPE_INT);
3153 /*                                              iptr->sx.val.i = 0; */
3154                                                 BRANCH(tbptr, copy);
3155                                                 break;
3156
3157                                                 /* pop 0 push 0 branch */
3158
3159                                         case ICMD_GOTO:
3160                                                 COUNT(count_pcmd_bra);
3161                                                 OP0_BRANCH;
3162                                                 BRANCH(tbptr, copy);
3163                                                 superblockend = true;
3164                                                 break;
3165
3166                                                 /* pop 1 push 0 table branch */
3167
3168                                         case ICMD_TABLESWITCH:
3169                                                 COUNT(count_pcmd_table);
3170                                                 OP1_BRANCH(TYPE_INT);
3171
3172                                                 table = iptr->dst.table;
3173                                                 BRANCH_TARGET(*table, tbptr, copy);
3174                                                 table++;
3175
3176                                                 i = iptr->sx.s23.s3.tablehigh
3177                                                   - iptr->sx.s23.s2.tablelow + 1;
3178
3179                                                 while (--i >= 0) {
3180                                                         BRANCH_TARGET(*table, tbptr, copy);
3181                                                         table++;
3182                                                 }
3183                                                 superblockend = true;
3184                                                 break;
3185
3186                                                 /* pop 1 push 0 table branch */
3187
3188                                         case ICMD_LOOKUPSWITCH:
3189                                                 COUNT(count_pcmd_table);
3190                                                 OP1_BRANCH(TYPE_INT);
3191
3192                                                 BRANCH_TARGET(iptr->sx.s23.s3.lookupdefault, tbptr, copy);
3193
3194                                                 lookup = iptr->dst.lookup;
3195
3196                                                 i = iptr->sx.s23.s2.lookupcount;
3197
3198                                                 while (--i >= 0) {
3199                                                         BRANCH_TARGET(lookup->target, tbptr, copy);
3200                                                         lookup++;
3201                                                 }
3202                                                 superblockend = true;
3203                                                 break;
3204
3205                                         case ICMD_MONITORENTER:
3206                                         case ICMD_MONITOREXIT:
3207                                                 coalescing_boundary = sd.new;
3208                                                 COUNT(count_check_null);
3209                                                 OP1_0(TYPE_ADR);
3210                                                 break;
3211
3212                                                 /* pop 2 push 0 branch */
3213
3214                                         case ICMD_IF_ICMPEQ:
3215                                         case ICMD_IF_ICMPNE:
3216                                         case ICMD_IF_ICMPLT:
3217                                         case ICMD_IF_ICMPGE:
3218                                         case ICMD_IF_ICMPGT:
3219                                         case ICMD_IF_ICMPLE:
3220                                                 COUNT(count_pcmd_bra);
3221                                                 OP2_BRANCH(TYPE_INT, TYPE_INT);
3222                                                 BRANCH(tbptr, copy);
3223                                                 break;
3224
3225                                         case ICMD_IF_ACMPEQ:
3226                                         case ICMD_IF_ACMPNE:
3227                                                 COUNT(count_pcmd_bra);
3228                                                 OP2_BRANCH(TYPE_ADR, TYPE_ADR);
3229                                                 BRANCH(tbptr, copy);
3230                                                 break;
3231
3232                                                 /* pop 2 push 0 */
3233
3234                                         case ICMD_PUTFIELD:
3235                                                 coalescing_boundary = sd.new;
3236                                                 COUNT(count_check_null);
3237                                                 COUNT(count_pcmd_mem);
3238                                                 INSTRUCTION_GET_FIELDREF(iptr, fmiref);
3239                                                 OP2_0(TYPE_ADR, fmiref->parseddesc.fd->type);
3240                                                 break;
3241
3242                                         case ICMD_POP2:
3243                                                 REQUIRE(1);
3244                                                 if (!IS_2_WORD_TYPE(curstack->type)) {
3245                                                         /* ..., cat1 */
3246 #ifdef ENABLE_VERIFIER
3247                                                         if (opt_verify) {
3248                                                                 REQUIRE(2);
3249                                                                 if (IS_2_WORD_TYPE(curstack->prev->type))
3250                                                                         goto throw_stack_category_error;
3251                                                         }
3252 #endif
3253                                                         OP2_0_ANY_ANY; /* pop two slots */
3254                                                 }
3255                                                 else {
3256                                                         iptr->opc = ICMD_POP;
3257                                                         OP1_0_ANY; /* pop one (two-word) slot */
3258                                                 }
3259                                                 break;
3260
3261                                                 /* pop 0 push 1 dup */
3262
3263                                         case ICMD_DUP:
3264 #ifdef ENABLE_VERIFIER
3265                                                 if (opt_verify) {
3266                                                         REQUIRE(1);
3267                                                         if (IS_2_WORD_TYPE(curstack->type))
3268                                                                 goto throw_stack_category_error;
3269                                                 }
3270 #endif
3271                                                 COUNT(count_dup_instruction);
3272
3273 icmd_DUP:
3274                                                 src1 = curstack;
3275
3276                                                 COPY_UP(src1);
3277                                                 coalescing_boundary = sd.new - 1;
3278                                                 break;
3279
3280                                         case ICMD_DUP2:
3281                                                 REQUIRE(1);
3282                                                 if (IS_2_WORD_TYPE(curstack->type)) {
3283                                                         /* ..., cat2 */
3284                                                         iptr->opc = ICMD_DUP;
3285                                                         goto icmd_DUP;
3286                                                 }
3287                                                 else {
3288                                                         REQUIRE(2);
3289                                                         /* ..., ????, cat1 */
3290 #ifdef ENABLE_VERIFIER
3291                                                         if (opt_verify) {
3292                                                                 if (IS_2_WORD_TYPE(curstack->prev->type))
3293                                                                         goto throw_stack_category_error;
3294                                                         }
3295 #endif
3296                                                         src1 = curstack->prev;
3297                                                         src2 = curstack;
3298
3299                                                         COPY_UP(src1); iptr++; len--;
3300                                                         COPY_UP(src2);
3301
3302                                                         coalescing_boundary = sd.new;
3303                                                 }
3304                                                 break;
3305
3306                                                 /* pop 2 push 3 dup */
3307
3308                                         case ICMD_DUP_X1:
3309 #ifdef ENABLE_VERIFIER
3310                                                 if (opt_verify) {
3311                                                         REQUIRE(2);
3312                                                         if (IS_2_WORD_TYPE(curstack->type) ||
3313                                                                 IS_2_WORD_TYPE(curstack->prev->type))
3314                                                                         goto throw_stack_category_error;
3315                                                 }
3316 #endif
3317
3318 icmd_DUP_X1:
3319                                                 src1 = curstack->prev;
3320                                                 src2 = curstack;
3321                                                 POPANY; POPANY;
3322                                                 stackdepth -= 2;
3323
3324                                                 DUP_SLOT(src2); dst1 = curstack; stackdepth++;
3325
3326                                                 MOVE_UP(src1); iptr++; len--;
3327                                                 MOVE_UP(src2); iptr++; len--;
3328
3329                                                 COPY_DOWN(curstack, dst1);
3330
3331                                                 coalescing_boundary = sd.new;
3332                                                 break;
3333
3334                                         case ICMD_DUP2_X1:
3335                                                 REQUIRE(2);
3336                                                 if (IS_2_WORD_TYPE(curstack->type)) {
3337                                                         /* ..., ????, cat2 */
3338 #ifdef ENABLE_VERIFIER
3339                                                         if (opt_verify) {
3340                                                                 if (IS_2_WORD_TYPE(curstack->prev->type))
3341                                                                         goto throw_stack_category_error;
3342                                                         }
3343 #endif
3344                                                         iptr->opc = ICMD_DUP_X1;
3345                                                         goto icmd_DUP_X1;
3346                                                 }
3347                                                 else {
3348                                                         /* ..., ????, cat1 */
3349 #ifdef ENABLE_VERIFIER
3350                                                         if (opt_verify) {
3351                                                                 REQUIRE(3);
3352                                                                 if (IS_2_WORD_TYPE(curstack->prev->type)
3353                                                                         || IS_2_WORD_TYPE(curstack->prev->prev->type))
3354                                                                                 goto throw_stack_category_error;
3355                                                         }
3356 #endif
3357
3358 icmd_DUP2_X1:
3359                                                         src1 = curstack->prev->prev;
3360                                                         src2 = curstack->prev;
3361                                                         src3 = curstack;
3362                                                         POPANY; POPANY; POPANY;
3363                                                         stackdepth -= 3;
3364
3365                                                         DUP_SLOT(src2); dst1 = curstack; stackdepth++;
3366                                                         DUP_SLOT(src3); dst2 = curstack; stackdepth++;
3367
3368                                                         MOVE_UP(src1); iptr++; len--;
3369                                                         MOVE_UP(src2); iptr++; len--;
3370                                                         MOVE_UP(src3); iptr++; len--;
3371
3372                                                         COPY_DOWN(curstack, dst2); iptr++; len--;
3373                                                         COPY_DOWN(curstack->prev, dst1);
3374
3375                                                         coalescing_boundary = sd.new;
3376                                                 }
3377                                                 break;
3378
3379                                                 /* pop 3 push 4 dup */
3380
3381                                         case ICMD_DUP_X2:
3382                                                 REQUIRE(2);
3383                                                 if (IS_2_WORD_TYPE(curstack->prev->type)) {
3384                                                         /* ..., cat2, ???? */
3385 #ifdef ENABLE_VERIFIER
3386                                                         if (opt_verify) {
3387                                                                 if (IS_2_WORD_TYPE(curstack->type))
3388                                                                         goto throw_stack_category_error;
3389                                                         }
3390 #endif
3391                                                         iptr->opc = ICMD_DUP_X1;
3392                                                         goto icmd_DUP_X1;
3393                                                 }
3394                                                 else {
3395                                                         /* ..., cat1, ???? */
3396 #ifdef ENABLE_VERIFIER
3397                                                         if (opt_verify) {
3398                                                                 REQUIRE(3);
3399                                                                 if (IS_2_WORD_TYPE(curstack->type)
3400                                                                         || IS_2_WORD_TYPE(curstack->prev->prev->type))
3401                                                                                         goto throw_stack_category_error;
3402                                                         }
3403 #endif
3404 icmd_DUP_X2:
3405                                                         src1 = curstack->prev->prev;
3406                                                         src2 = curstack->prev;
3407                                                         src3 = curstack;
3408                                                         POPANY; POPANY; POPANY;
3409                                                         stackdepth -= 3;
3410
3411                                                         DUP_SLOT(src3); dst1 = curstack; stackdepth++;
3412
3413                                                         MOVE_UP(src1); iptr++; len--;
3414                                                         MOVE_UP(src2); iptr++; len--;
3415                                                         MOVE_UP(src3); iptr++; len--;
3416
3417                                                         COPY_DOWN(curstack, dst1);
3418
3419                                                         coalescing_boundary = sd.new;
3420                                                 }
3421                                                 break;
3422
3423                                         case ICMD_DUP2_X2:
3424                                                 REQUIRE(2);
3425                                                 if (IS_2_WORD_TYPE(curstack->type)) {
3426                                                         /* ..., ????, cat2 */
3427                                                         if (IS_2_WORD_TYPE(curstack->prev->type)) {
3428                                                                 /* ..., cat2, cat2 */
3429                                                                 iptr->opc = ICMD_DUP_X1;
3430                                                                 goto icmd_DUP_X1;
3431                                                         }
3432                                                         else {
3433                                                                 /* ..., cat1, cat2 */
3434 #ifdef ENABLE_VERIFIER
3435                                                                 if (opt_verify) {
3436                                                                         REQUIRE(3);
3437                                                                         if (IS_2_WORD_TYPE(curstack->prev->prev->type))
3438                                                                                         goto throw_stack_category_error;
3439                                                                 }
3440 #endif
3441                                                                 iptr->opc = ICMD_DUP_X2;
3442                                                                 goto icmd_DUP_X2;
3443                                                         }
3444                                                 }
3445
3446                                                 REQUIRE(3);
3447                                                 /* ..., ????, ????, cat1 */
3448
3449                                                 if (IS_2_WORD_TYPE(curstack->prev->prev->type)) {
3450                                                         /* ..., cat2, ????, cat1 */
3451 #ifdef ENABLE_VERIFIER
3452                                                         if (opt_verify) {
3453                                                                 if (IS_2_WORD_TYPE(curstack->prev->type))
3454                                                                         goto throw_stack_category_error;
3455                                                         }
3456 #endif
3457                                                         iptr->opc = ICMD_DUP2_X1;
3458                                                         goto icmd_DUP2_X1;
3459                                                 }
3460                                                 else {
3461                                                         /* ..., cat1, ????, cat1 */
3462 #ifdef ENABLE_VERIFIER
3463                                                         if (opt_verify) {
3464                                                                 REQUIRE(4);
3465                                                                 if (IS_2_WORD_TYPE(curstack->prev->type)
3466                                                                         || IS_2_WORD_TYPE(curstack->prev->prev->prev->type))
3467                                                                         goto throw_stack_category_error;
3468                                                         }
3469 #endif
3470
3471                                                         src1 = curstack->prev->prev->prev;
3472                                                         src2 = curstack->prev->prev;
3473                                                         src3 = curstack->prev;
3474                                                         src4 = curstack;
3475                                                         POPANY; POPANY; POPANY; POPANY;
3476                                                         stackdepth -= 4;
3477
3478                                                         DUP_SLOT(src3); dst1 = curstack; stackdepth++;
3479                                                         DUP_SLOT(src4); dst2 = curstack; stackdepth++;
3480
3481                                                         MOVE_UP(src1); iptr++; len--;
3482                                                         MOVE_UP(src2); iptr++; len--;
3483                                                         MOVE_UP(src3); iptr++; len--;
3484                                                         MOVE_UP(src4); iptr++; len--;
3485
3486                                                         COPY_DOWN(curstack, dst2); iptr++; len--;
3487                                                         COPY_DOWN(curstack->prev, dst1);
3488
3489                                                         coalescing_boundary = sd.new;
3490                                                 }
3491                                                 break;
3492
3493                                                 /* pop 2 push 2 swap */
3494
3495                                         case ICMD_SWAP:
3496 #ifdef ENABLE_VERIFIER
3497                                                 if (opt_verify) {
3498                                                         REQUIRE(2);
3499                                                         if (IS_2_WORD_TYPE(curstack->type)
3500                                                                 || IS_2_WORD_TYPE(curstack->prev->type))
3501                                                                 goto throw_stack_category_error;
3502                                                 }
3503 #endif
3504
3505                                                 src1 = curstack->prev;
3506                                                 src2 = curstack;
3507                                                 POPANY; POPANY;
3508                                                 stackdepth -= 2;
3509
3510                                                 MOVE_UP(src2); iptr++; len--;
3511                                                 MOVE_UP(src1);
3512
3513                                                 coalescing_boundary = sd.new;
3514                                                 break;
3515
3516                                                 /* pop 2 push 1 */
3517
3518                                         case ICMD_IDIV:
3519                                         case ICMD_IREM:
3520                                                 coalescing_boundary = sd.new;
3521 #if !SUPPORT_DIVISION
3522                                                 bte = iptr->sx.s23.s3.bte;
3523                                                 md = bte->md;
3524
3525                                                 if (md->memuse > rd->memuse)
3526                                                         rd->memuse = md->memuse;
3527                                                 if (md->argintreguse > rd->argintreguse)
3528                                                         rd->argintreguse = md->argintreguse;
3529
3530                                                 /* make all stack variables saved */
3531
3532                                                 copy = curstack;
3533                                                 while (copy) {
3534                                                         sd.var[copy->varnum].flags |= SAVEDVAR;
3535                                                         copy->flags |= SAVEDVAR;
3536                                                         copy = copy->prev;
3537                                                 }
3538                                                 /* FALLTHROUGH */
3539
3540 #endif /* !SUPPORT_DIVISION */
3541
3542                                         case ICMD_ISHL:
3543                                         case ICMD_ISHR:
3544                                         case ICMD_IUSHR:
3545                                         case ICMD_IADD:
3546                                         case ICMD_ISUB:
3547                                         case ICMD_IMUL:
3548                                         case ICMD_IAND:
3549                                         case ICMD_IOR:
3550                                         case ICMD_IXOR:
3551                                                 COUNT(count_pcmd_op);
3552                                                 OP2_1(TYPE_INT, TYPE_INT, TYPE_INT);
3553                                                 break;
3554
3555                                         case ICMD_LDIV:
3556                                         case ICMD_LREM:
3557                                                 coalescing_boundary = sd.new;
3558 #if !(SUPPORT_DIVISION && SUPPORT_LONG && SUPPORT_LONG_DIV)
3559                                                 bte = iptr->sx.s23.s3.bte;
3560                                                 md = bte->md;
3561
3562                                                 if (md->memuse > rd->memuse)
3563                                                         rd->memuse = md->memuse;
3564                                                 if (md->argintreguse > rd->argintreguse)
3565                                                         rd->argintreguse = md->argintreguse;
3566                                                 /* XXX non-leaf method? */
3567
3568                                                 /* make all stack variables saved */
3569
3570                                                 copy = curstack;
3571                                                 while (copy) {
3572                                                         sd.var[copy->varnum].flags |= SAVEDVAR;
3573                                                         copy->flags |= SAVEDVAR;
3574                                                         copy = copy->prev;
3575                                                 }
3576                                                 /* FALLTHROUGH */
3577
3578 #endif /* !(SUPPORT_DIVISION && SUPPORT_LONG && SUPPORT_LONG_DIV) */
3579
3580                                         case ICMD_LMUL:
3581                                         case ICMD_LADD:
3582                                         case ICMD_LSUB:
3583 #if SUPPORT_LONG_LOGICAL
3584                                         case ICMD_LAND:
3585                                         case ICMD_LOR:
3586                                         case ICMD_LXOR:
3587 #endif /* SUPPORT_LONG_LOGICAL */
3588                                                 COUNT(count_pcmd_op);
3589                                                 OP2_1(TYPE_LNG, TYPE_LNG, TYPE_LNG);
3590                                                 break;
3591
3592                                         case ICMD_LSHL:
3593                                         case ICMD_LSHR:
3594                                         case ICMD_LUSHR:
3595                                                 COUNT(count_pcmd_op);
3596                                                 OP2_1(TYPE_LNG, TYPE_INT, TYPE_LNG);
3597                                                 break;
3598
3599                                         case ICMD_FADD:
3600                                         case ICMD_FSUB:
3601                                         case ICMD_FMUL:
3602                                         case ICMD_FDIV:
3603                                         case ICMD_FREM:
3604                                                 COUNT(count_pcmd_op);
3605                                                 OP2_1(TYPE_FLT, TYPE_FLT, TYPE_FLT);
3606                                                 break;
3607
3608                                         case ICMD_DADD:
3609                                         case ICMD_DSUB:
3610                                         case ICMD_DMUL:
3611                                         case ICMD_DDIV:
3612                                         case ICMD_DREM:
3613                                                 COUNT(count_pcmd_op);
3614                                                 OP2_1(TYPE_DBL, TYPE_DBL, TYPE_DBL);
3615                                                 break;
3616
3617                                         case ICMD_LCMP:
3618                                                 COUNT(count_pcmd_op);
3619 #if SUPPORT_LONG_CMP_CONST
3620                                                 if ((len == 0) || (iptr[1].sx.val.i != 0))
3621                                                         goto normal_LCMP;
3622
3623                                                 switch (iptr[1].opc) {
3624                                                 case ICMD_IFEQ:
3625                                                         iptr->opc = ICMD_IF_LCMPEQ;
3626                                                 icmd_lcmp_if_tail:
3627                                                         iptr->dst.insindex = iptr[1].dst.insindex;
3628                                                         iptr[1].opc = ICMD_NOP;
3629
3630                                                         OP2_BRANCH(TYPE_LNG, TYPE_LNG);
3631                                                         BRANCH(tbptr, copy);
3632
3633                                                         COUNT(count_pcmd_bra);
3634                                                         break;
3635                                                 case ICMD_IFNE:
3636                                                         iptr->opc = ICMD_IF_LCMPNE;
3637                                                         goto icmd_lcmp_if_tail;
3638                                                 case ICMD_IFLT:
3639                                                         iptr->opc = ICMD_IF_LCMPLT;
3640                                                         goto icmd_lcmp_if_tail;
3641                                                 case ICMD_IFGT:
3642                                                         iptr->opc = ICMD_IF_LCMPGT;
3643                                                         goto icmd_lcmp_if_tail;
3644                                                 case ICMD_IFLE:
3645                                                         iptr->opc = ICMD_IF_LCMPLE;
3646                                                         goto icmd_lcmp_if_tail;
3647                                                 case ICMD_IFGE:
3648                                                         iptr->opc = ICMD_IF_LCMPGE;
3649                                                         goto icmd_lcmp_if_tail;
3650                                                 default:
3651                                                         goto normal_LCMP;
3652                                                 }
3653                                                 break;
3654 normal_LCMP:
3655 #endif /* SUPPORT_LONG_CMP_CONST */
3656                                                         OP2_1(TYPE_LNG, TYPE_LNG, TYPE_INT);
3657                                                 break;
3658
3659                                                 /* XXX why is this deactivated? */
3660 #if 0
3661                                         case ICMD_FCMPL:
3662                                                 COUNT(count_pcmd_op);
3663                                                 if ((len == 0) || (iptr[1].sx.val.i != 0))
3664                                                         goto normal_FCMPL;
3665
3666                                                 switch (iptr[1].opc) {
3667                                                 case ICMD_IFEQ:
3668                                                         iptr->opc = ICMD_IF_FCMPEQ;
3669                                                 icmd_if_fcmpl_tail:
3670                                                         iptr->dst.insindex = iptr[1].dst.insindex;
3671                                                         iptr[1].opc = ICMD_NOP;
3672
3673                                                         OP2_BRANCH(TYPE_FLT, TYPE_FLT);
3674                                                         BRANCH(tbptr, copy);
3675
3676                                                         COUNT(count_pcmd_bra);
3677                                                         break;
3678                                                 case ICMD_IFNE:
3679                                                         iptr->opc = ICMD_IF_FCMPNE;
3680                                                         goto icmd_if_fcmpl_tail;
3681                                                 case ICMD_IFLT:
3682                                                         iptr->opc = ICMD_IF_FCMPL_LT;
3683                                                         goto icmd_if_fcmpl_tail;
3684                                                 case ICMD_IFGT:
3685                                                         iptr->opc = ICMD_IF_FCMPL_GT;
3686                                                         goto icmd_if_fcmpl_tail;
3687                                                 case ICMD_IFLE:
3688                                                         iptr->opc = ICMD_IF_FCMPL_LE;
3689                                                         goto icmd_if_fcmpl_tail;
3690                                                 case ICMD_IFGE:
3691                                                         iptr->opc = ICMD_IF_FCMPL_GE;
3692                                                         goto icmd_if_fcmpl_tail;
3693                                                 default:
3694                                                         goto normal_FCMPL;
3695                                                 }
3696                                                 break;
3697
3698 normal_FCMPL:
3699                                                 OPTT2_1(TYPE_FLT, TYPE_FLT, TYPE_INT);
3700                                                 break;
3701
3702                                         case ICMD_FCMPG:
3703                                                 COUNT(count_pcmd_op);
3704                                                 if ((len == 0) || (iptr[1].sx.val.i != 0))
3705                                                         goto normal_FCMPG;
3706
3707                                                 switch (iptr[1].opc) {
3708                                                 case ICMD_IFEQ:
3709                                                         iptr->opc = ICMD_IF_FCMPEQ;
3710                                                 icmd_if_fcmpg_tail:
3711                                                         iptr->dst.insindex = iptr[1].dst.insindex;
3712                                                         iptr[1].opc = ICMD_NOP;
3713
3714                                                         OP2_BRANCH(TYPE_FLT, TYPE_FLT);
3715                                                         BRANCH(tbptr, copy);
3716
3717                                                         COUNT(count_pcmd_bra);
3718                                                         break;
3719                                                 case ICMD_IFNE:
3720                                                         iptr->opc = ICMD_IF_FCMPNE;
3721                                                         goto icmd_if_fcmpg_tail;
3722                                                 case ICMD_IFLT:
3723                                                         iptr->opc = ICMD_IF_FCMPG_LT;
3724                                                         goto icmd_if_fcmpg_tail;
3725                                                 case ICMD_IFGT:
3726                                                         iptr->opc = ICMD_IF_FCMPG_GT;
3727                                                         goto icmd_if_fcmpg_tail;
3728                                                 case ICMD_IFLE:
3729                                                         iptr->opc = ICMD_IF_FCMPG_LE;
3730                                                         goto icmd_if_fcmpg_tail;
3731                                                 case ICMD_IFGE:
3732                                                         iptr->opc = ICMD_IF_FCMPG_GE;
3733                                                         goto icmd_if_fcmpg_tail;
3734                                                 default:
3735                                                         goto normal_FCMPG;
3736                                                 }
3737                                                 break;
3738
3739 normal_FCMPG:
3740                                                 OP2_1(TYPE_FLT, TYPE_FLT, TYPE_INT);
3741                                                 break;
3742
3743                                         case ICMD_DCMPL:
3744                                                 COUNT(count_pcmd_op);
3745                                                 if ((len == 0) || (iptr[1].sx.val.i != 0))
3746                                                         goto normal_DCMPL;
3747
3748                                                 switch (iptr[1].opc) {
3749                                                 case ICMD_IFEQ:
3750                                                         iptr->opc = ICMD_IF_DCMPEQ;
3751                                                 icmd_if_dcmpl_tail:
3752                                                         iptr->dst.insindex = iptr[1].dst.insindex;
3753                                                         iptr[1].opc = ICMD_NOP;
3754
3755                                                         OP2_BRANCH(TYPE_DBL, TYPE_DBL);
3756                                                         BRANCH(tbptr, copy);
3757
3758                                                         COUNT(count_pcmd_bra);
3759                                                         break;
3760                                                 case ICMD_IFNE:
3761                                                         iptr->opc = ICMD_IF_DCMPNE;
3762                                                         goto icmd_if_dcmpl_tail;
3763                                                 case ICMD_IFLT:
3764                                                         iptr->opc = ICMD_IF_DCMPL_LT;
3765                                                         goto icmd_if_dcmpl_tail;
3766                                                 case ICMD_IFGT:
3767                                                         iptr->opc = ICMD_IF_DCMPL_GT;
3768                                                         goto icmd_if_dcmpl_tail;
3769                                                 case ICMD_IFLE:
3770                                                         iptr->opc = ICMD_IF_DCMPL_LE;
3771                                                         goto icmd_if_dcmpl_tail;
3772                                                 case ICMD_IFGE:
3773                                                         iptr->opc = ICMD_IF_DCMPL_GE;
3774                                                         goto icmd_if_dcmpl_tail;
3775                                                 default:
3776                                                         goto normal_DCMPL;
3777                                                 }
3778                                                 break;
3779
3780 normal_DCMPL:
3781                                                 OPTT2_1(TYPE_DBL, TYPE_INT);
3782                                                 break;
3783
3784                                         case ICMD_DCMPG:
3785                                                 COUNT(count_pcmd_op);
3786                                                 if ((len == 0) || (iptr[1].sx.val.i != 0))
3787                                                         goto normal_DCMPG;
3788
3789                                                 switch (iptr[1].opc) {
3790                                                 case ICMD_IFEQ:
3791                                                         iptr->opc = ICMD_IF_DCMPEQ;
3792                                                 icmd_if_dcmpg_tail:
3793                                                         iptr->dst.insindex = iptr[1].dst.insindex;
3794                                                         iptr[1].opc = ICMD_NOP;
3795
3796                                                         OP2_BRANCH(TYPE_DBL, TYPE_DBL);
3797                                                         BRANCH(tbptr, copy);
3798
3799                                                         COUNT(count_pcmd_bra);
3800                                                         break;
3801                                                 case ICMD_IFNE:
3802                                                         iptr->opc = ICMD_IF_DCMPNE;
3803                                                         goto icmd_if_dcmpg_tail;
3804                                                 case ICMD_IFLT:
3805                                                         iptr->opc = ICMD_IF_DCMPG_LT;
3806                                                         goto icmd_if_dcmpg_tail;
3807                                                 case ICMD_IFGT:
3808                                                         iptr->opc = ICMD_IF_DCMPG_GT;
3809                                                         goto icmd_if_dcmpg_tail;
3810                                                 case ICMD_IFLE:
3811                                                         iptr->opc = ICMD_IF_DCMPG_LE;
3812                                                         goto icmd_if_dcmpg_tail;
3813                                                 case ICMD_IFGE:
3814                                                         iptr->opc = ICMD_IF_DCMPG_GE;
3815                                                         goto icmd_if_dcmpg_tail;
3816                                                 default:
3817                                                         goto normal_DCMPG;
3818                                                 }
3819                                                 break;
3820
3821 normal_DCMPG:
3822                                                 OP2_1(TYPE_DBL, TYPE_DBL, TYPE_INT);
3823                                                 break;
3824 #else
3825                                         case ICMD_FCMPL:
3826                                         case ICMD_FCMPG:
3827                                                 COUNT(count_pcmd_op);
3828                                                 OP2_1(TYPE_FLT, TYPE_FLT, TYPE_INT);
3829                                                 break;
3830
3831                                         case ICMD_DCMPL:
3832                                         case ICMD_DCMPG:
3833                                                 COUNT(count_pcmd_op);
3834                                                 OP2_1(TYPE_DBL, TYPE_DBL, TYPE_INT);
3835                                                 break;
3836 #endif
3837
3838                                                 /* pop 1 push 1 */
3839
3840                                         case ICMD_INEG:
3841                                         case ICMD_INT2BYTE:
3842                                         case ICMD_INT2CHAR:
3843                                         case ICMD_INT2SHORT:
3844                                                 COUNT(count_pcmd_op);
3845                                                 OP1_1(TYPE_INT, TYPE_INT);
3846                                                 break;
3847                                         case ICMD_LNEG:
3848                                                 COUNT(count_pcmd_op);
3849                                                 OP1_1(TYPE_LNG, TYPE_LNG);
3850                                                 break;
3851                                         case ICMD_FNEG:
3852                                                 COUNT(count_pcmd_op);
3853                                                 OP1_1(TYPE_FLT, TYPE_FLT);
3854                                                 break;
3855                                         case ICMD_DNEG:
3856                                                 COUNT(count_pcmd_op);
3857                                                 OP1_1(TYPE_DBL, TYPE_DBL);
3858                                                 break;
3859
3860                                         case ICMD_I2L:
3861                                                 COUNT(count_pcmd_op);
3862                                                 OP1_1(TYPE_INT, TYPE_LNG);
3863                                                 break;
3864                                         case ICMD_I2F:
3865                                                 COUNT(count_pcmd_op);
3866                                                 OP1_1(TYPE_INT, TYPE_FLT);
3867                                                 break;
3868                                         case ICMD_I2D:
3869                                                 COUNT(count_pcmd_op);
3870                                                 OP1_1(TYPE_INT, TYPE_DBL);
3871                                                 break;
3872                                         case ICMD_L2I:
3873                                                 COUNT(count_pcmd_op);
3874                                                 OP1_1(TYPE_LNG, TYPE_INT);
3875                                                 break;
3876                                         case ICMD_L2F:
3877                                                 COUNT(count_pcmd_op);
3878                                                 OP1_1(TYPE_LNG, TYPE_FLT);
3879                                                 break;
3880                                         case ICMD_L2D:
3881                                                 COUNT(count_pcmd_op);
3882                                                 OP1_1(TYPE_LNG, TYPE_DBL);
3883                                                 break;
3884                                         case ICMD_F2I:
3885                                                 COUNT(count_pcmd_op);
3886                                                 OP1_1(TYPE_FLT, TYPE_INT);
3887                                                 break;
3888                                         case ICMD_F2L:
3889                                                 COUNT(count_pcmd_op);
3890                                                 OP1_1(TYPE_FLT, TYPE_LNG);
3891                                                 break;
3892                                         case ICMD_F2D:
3893                                                 COUNT(count_pcmd_op);
3894                                                 OP1_1(TYPE_FLT, TYPE_DBL);
3895                                                 break;
3896                                         case ICMD_D2I:
3897                                                 COUNT(count_pcmd_op);
3898                                                 OP1_1(TYPE_DBL, TYPE_INT);
3899                                                 break;
3900                                         case ICMD_D2L:
3901                                                 COUNT(count_pcmd_op);
3902                                                 OP1_1(TYPE_DBL, TYPE_LNG);
3903                                                 break;
3904                                         case ICMD_D2F:
3905                                                 COUNT(count_pcmd_op);
3906                                                 OP1_1(TYPE_DBL, TYPE_FLT);
3907                                                 break;
3908
3909                                         case ICMD_CHECKCAST:
3910                                                 coalescing_boundary = sd.new;
3911                                                 if (iptr->flags.bits & INS_FLAG_ARRAY) {
3912                                                         /* array type cast-check */
3913
3914                                                         bte = builtintable_get_internal(BUILTIN_arraycheckcast);
3915                                                         md = bte->md;
3916
3917                                                         if (md->memuse > rd->memuse)
3918                                                                 rd->memuse = md->memuse;
3919                                                         if (md->argintreguse > rd->argintreguse)
3920                                                                 rd->argintreguse = md->argintreguse;
3921
3922                                                         /* make all stack variables saved */
3923
3924                                                         copy = curstack;
3925                                                         while (copy) {
3926                                                                 sd.var[copy->varnum].flags |= SAVEDVAR;
3927                                                                 copy->flags |= SAVEDVAR;
3928                                                                 copy = copy->prev;
3929                                                         }
3930                                                 }
3931                                                 OP1_1(TYPE_ADR, TYPE_ADR);
3932                                                 break;
3933
3934                                         case ICMD_INSTANCEOF:
3935                                         case ICMD_ARRAYLENGTH:
3936                                                 coalescing_boundary = sd.new;
3937                                                 OP1_1(TYPE_ADR, TYPE_INT);
3938                                                 break;
3939
3940                                         case ICMD_NEWARRAY:
3941                                         case ICMD_ANEWARRAY:
3942                                                 coalescing_boundary = sd.new;
3943                                                 OP1_1(TYPE_INT, TYPE_ADR);
3944                                                 break;
3945
3946                                         case ICMD_GETFIELD:
3947                                                 coalescing_boundary = sd.new;
3948                                                 COUNT(count_check_null);
3949                                                 COUNT(count_pcmd_mem);
3950                                                 INSTRUCTION_GET_FIELDREF(iptr, fmiref);
3951                                                 OP1_1(TYPE_ADR, fmiref->parseddesc.fd->type);
3952                                                 break;
3953
3954                                                 /* pop 0 push 1 */
3955
3956                                         case ICMD_GETSTATIC:
3957                                                 coalescing_boundary = sd.new;
3958                                                 COUNT(count_pcmd_mem);
3959                                                 INSTRUCTION_GET_FIELDREF(iptr, fmiref);
3960                                                 OP0_1(fmiref->parseddesc.fd->type);
3961                                                 break;
3962
3963                                         case ICMD_NEW:
3964                                                 coalescing_boundary = sd.new;
3965                                                 OP0_1(TYPE_ADR);
3966                                                 break;
3967
3968                                         case ICMD_JSR:
3969                                                 OP0_1(TYPE_RET);
3970
3971                                                 assert(sd.bptr->next);  /* XXX exception */
3972                                                 sd.var[curstack->varnum].vv.retaddr = sd.bptr->next;
3973
3974                                                 tbptr = BLOCK_OF(iptr->sx.s23.s3.jsrtarget.insindex);
3975                                                 tbptr->type = BBTYPE_SBR;
3976
3977                                                 tbptr = stack_mark_reached(&sd, tbptr, curstack, stackdepth);
3978                                                 if (!tbptr)
3979                                                         return false;
3980
3981                                                 iptr->sx.s23.s3.jsrtarget.block = tbptr;
3982
3983                                                 /* We need to check for overflow right here because
3984                                                  * the pushed value is poped afterwards */
3985                                                 CHECKOVERFLOW;
3986
3987                                                 superblockend = true;
3988                                                 /* XXX should not be marked as interface, as it does not need to be */
3989                                                 /* allocated. Same for the invar of the target. */
3990                                                 break;
3991
3992                                         /* pop many push any */
3993
3994                                         case ICMD_BUILTIN:
3995 icmd_BUILTIN:
3996                                                 bte = iptr->sx.s23.s3.bte;
3997                                                 md = bte->md;
3998                                                 goto _callhandling;
3999
4000                                         case ICMD_INVOKESTATIC:
4001                                         case ICMD_INVOKESPECIAL:
4002                                         case ICMD_INVOKEVIRTUAL:
4003                                         case ICMD_INVOKEINTERFACE:
4004                                                 COUNT(count_pcmd_met);
4005
4006                                                 /* Check for functions to replace with builtin
4007                                                  * functions. */
4008
4009                                                 if (builtintable_replace_function(iptr))
4010                                                         goto icmd_BUILTIN;
4011
4012                                                 INSTRUCTION_GET_METHODDESC(iptr, md);
4013                                                 /* XXX resurrect this COUNT? */
4014 /*                          if (lm->flags & ACC_STATIC) */
4015 /*                              {COUNT(count_check_null);} */
4016
4017                                         _callhandling:
4018
4019                                                 coalescing_boundary = sd.new;
4020
4021                                                 i = md->paramcount;
4022
4023                                                 if (md->memuse > rd->memuse)
4024                                                         rd->memuse = md->memuse;
4025                                                 if (md->argintreguse > rd->argintreguse)
4026                                                         rd->argintreguse = md->argintreguse;
4027                                                 if (md->argfltreguse > rd->argfltreguse)
4028                                                         rd->argfltreguse = md->argfltreguse;
4029
4030                                                 REQUIRE(i);
4031
4032                                                 /* XXX optimize for <= 2 args */
4033                                                 /* XXX not for ICMD_BUILTIN */
4034                                                 iptr->s1.argcount = stackdepth;
4035                                                 iptr->sx.s23.s2.args = DMNEW(s4, stackdepth);
4036
4037                                                 copy = curstack;
4038                                                 for (i-- ; i >= 0; i--) {
4039                                                         iptr->sx.s23.s2.args[i] = copy->varnum;
4040
4041                                                         /* do not change STACKVARs or LOCALVARS to ARGVAR*/
4042                                                         /* ->  won't help anyway */
4043                                                         if (!(IS_OUTVAR(copy) || IS_LOCALVAR(copy))) {
4044
4045 #if defined(SUPPORT_PASS_FLOATARGS_IN_INTREGS)
4046                         /* If we pass float arguments in integer argument registers, we
4047                          * are not allowed to precolor them here. Floats have to be moved
4048                          * to this regs explicitly in codegen().
4049                          * Only arguments that are passed by stack anyway can be precolored
4050                          * (michi 2005/07/24) */
4051                                                         if (!(sd.var[copy->varnum].flags & SAVEDVAR) &&
4052                                                            (!IS_FLT_DBL_TYPE(copy->type) 
4053                                                                 || md->params[i].inmemory)) {
4054 #else
4055                                                         if (!(sd.var[copy->varnum].flags & SAVEDVAR)) {
4056 #endif
4057
4058                                                                 SET_PREALLOC(copy);
4059
4060 #if defined(ENABLE_INTRP)
4061                                                                 if (!opt_intrp) {
4062 #endif
4063                                                                         if (md->params[i].inmemory) {
4064                                                                                 sd.var[copy->varnum].vv.regoff =
4065                                                                                         md->params[i].regoff;
4066                                                                                 sd.var[copy->varnum].flags |= 
4067                                                                                         INMEMORY;
4068                                                                         }
4069                                                                         else {
4070                                                                                 if (IS_FLT_DBL_TYPE(copy->type)) {
4071 #if defined(SUPPORT_PASS_FLOATARGS_IN_INTREGS)
4072                                                                                         assert(0); /* XXX is this assert ok? */
4073 #else
4074                                                                                         sd.var[copy->varnum].vv.regoff = 
4075                                                                                 rd->argfltregs[md->params[i].regoff];
4076 #endif /* SUPPORT_PASS_FLOATARGS_IN_INTREGS */
4077                                                                                 }
4078                                                                                 else {
4079 #if defined(SUPPORT_COMBINE_INTEGER_REGISTERS)
4080                                                                                         if (IS_2_WORD_TYPE(copy->type))
4081                                                                                                 sd.var[copy->varnum].vv.regoff = 
4082                                 PACK_REGS( rd->argintregs[GET_LOW_REG(md->params[i].regoff)],
4083                                                    rd->argintregs[GET_HIGH_REG(md->params[i].regoff)]);
4084
4085                                                                                         else
4086 #endif /* SUPPORT_COMBINE_INTEGER_REGISTERS */
4087                                                                                                 sd.var[copy->varnum].vv.regoff = 
4088                                                                                 rd->argintregs[md->params[i].regoff];
4089                                                                                 }
4090                                                                         }
4091 #if defined(ENABLE_INTRP)
4092                                                                 } /* end if (!opt_intrp) */
4093 #endif
4094                                                         }
4095                                                         }
4096                                                         copy = copy->prev;
4097                                                 }
4098
4099                                                 /* deal with live-through stack slots "under" the */
4100                                                 /* arguments */
4101                                                 /* XXX not for ICMD_BUILTIN */
4102
4103                                                 i = md->paramcount;
4104
4105                                                 while (copy) {
4106                                                         SET_TEMPVAR(copy);
4107                                                         iptr->sx.s23.s2.args[i++] = copy->varnum;
4108                                                         sd.var[copy->varnum].flags |= SAVEDVAR;
4109                                                         copy = copy->prev;
4110                                                 }
4111
4112                                                 /* pop the arguments */
4113
4114                                                 i = md->paramcount;
4115
4116                                                 stackdepth -= i;
4117                                                 while (--i >= 0) {
4118                                                         POPANY;
4119                                                 }
4120
4121                                                 /* push the return value */
4122
4123                                                 if (md->returntype.type != TYPE_VOID) {
4124                                                         GET_NEW_VAR(sd, new_index, md->returntype.type);
4125                                                         DST(md->returntype.type, new_index);
4126                                                         stackdepth++;
4127                                                 }
4128                                                 break;
4129
4130                                         case ICMD_INLINE_START:
4131                                         case ICMD_INLINE_END:
4132                                                 CLR_S1;
4133                                                 CLR_DST;
4134                                                 break;
4135
4136                                         case ICMD_MULTIANEWARRAY:
4137                                                 coalescing_boundary = sd.new;
4138                                                 if (rd->argintreguse < 3)
4139                                                         rd->argintreguse = 3;
4140
4141                                                 i = iptr->s1.argcount;
4142
4143                                                 REQUIRE(i);
4144
4145                                                 iptr->sx.s23.s2.args = DMNEW(s4, i);
4146
4147 #if defined(SPECIALMEMUSE)
4148 # if defined(__DARWIN__)
4149                                                 if (rd->memuse < (i + INT_ARG_CNT +LA_SIZE_IN_POINTERS))
4150                                                         rd->memuse = i + LA_SIZE_IN_POINTERS + INT_ARG_CNT;
4151 # else
4152                                                 if (rd->memuse < (i + LA_SIZE_IN_POINTERS + 3))
4153                                                         rd->memuse = i + LA_SIZE_IN_POINTERS + 3;
4154 # endif
4155 #else
4156 # if defined(__I386__)
4157                                                 if (rd->memuse < i + 3)
4158                                                         rd->memuse = i + 3; /* n integer args spilled on stack */
4159 # elif defined(__MIPS__) && SIZEOF_VOID_P == 4
4160                                                 if (rd->memuse < i + 2)
4161                                                         rd->memuse = i + 2; /* 4*4 bytes callee save space */
4162 # else
4163                                                 if (rd->memuse < i)
4164                                                         rd->memuse = i; /* n integer args spilled on stack */
4165 # endif /* defined(__I386__) */
4166 #endif
4167                                                 copy = curstack;
4168                                                 while (--i >= 0) {
4169                                         /* check INT type here? Currently typecheck does this. */
4170                                                         iptr->sx.s23.s2.args[i] = copy->varnum;
4171                                                         if (!(sd.var[copy->varnum].flags & SAVEDVAR)
4172                                                                 && (!IS_OUTVAR(copy))
4173                                                                 && (!IS_LOCALVAR(copy)) ) {
4174                                                                 copy->varkind = ARGVAR;
4175                                                                 sd.var[copy->varnum].flags |=
4176                                                                         INMEMORY & PREALLOC;
4177 #if defined(SPECIALMEMUSE)
4178 # if defined(__DARWIN__)
4179                                                                 sd.var[copy->varnum].vv.regoff = i + 
4180                                                                         LA_SIZE_IN_POINTERS + INT_ARG_CNT;
4181 # else
4182                                                                 sd.var[copy->varnum].vv.regoff = i + 
4183                                                                         LA_SIZE_IN_POINTERS + 3;
4184 # endif
4185 #else
4186 # if defined(__I386__)
4187                                                                 sd.var[copy->varnum].vv.regoff = i + 3;
4188 # elif defined(__MIPS__) && SIZEOF_VOID_P == 4
4189                                                                 sd.var[copy->varnum].vv.regoff = i + 2;
4190 # else
4191                                                                 sd.var[copy->varnum].vv.regoff = i;
4192 # endif /* defined(__I386__) */
4193 #endif /* defined(SPECIALMEMUSE) */
4194                                                         }
4195                                                         copy = copy->prev;
4196                                                 }
4197                                                 while (copy) {
4198                                                         sd.var[copy->varnum].flags |= SAVEDVAR;
4199                                                         copy = copy->prev;
4200                                                 }
4201
4202                                                 i = iptr->s1.argcount;
4203                                                 stackdepth -= i;
4204                                                 while (--i >= 0) {
4205                                                         POPANY;
4206                                                 }
4207                                                 GET_NEW_VAR(sd, new_index, TYPE_ADR);
4208                                                 DST(TYPE_ADR, new_index);
4209                                                 stackdepth++;
4210                                                 break;
4211
4212                                         default:
4213                                                 *exceptionptr =
4214                                                         new_internalerror("Unknown ICMD %d", opcode);
4215                                                 return false;
4216                                         } /* switch */
4217
4218                                         CHECKOVERFLOW;
4219                                         iptr++;
4220                                 } /* while instructions */
4221
4222                                 /* stack slots at basic block end become interfaces */
4223
4224                                 sd.bptr->outdepth = stackdepth;
4225                                 sd.bptr->outvars = DMNEW(s4, stackdepth);
4226
4227                                 i = stackdepth - 1;
4228                                 for (copy = curstack; copy; i--, copy = copy->prev) {
4229                                         varinfo *v;
4230                                         s4 t;
4231
4232                                         /* with the new vars rd->interfaces will be removed */
4233                                         /* and all in and outvars have to be STACKVARS!     */
4234                                         /* in the moment i.e. SWAP with in and out vars can */
4235                                         /* create an unresolvable conflict */
4236
4237                                         SET_TEMPVAR(copy);
4238                                         t = copy->type;
4239                                         if (t == TYPE_RET)
4240                                                 t = TYPE_ADR;
4241
4242                                         v = sd.var + copy->varnum;
4243                                         v->flags |= OUTVAR;
4244
4245                                         if (jd->interface_map[i*5 + t].flags == UNUSED) {
4246                                                 /* no interface var until now for this depth and */
4247                                                 /* type */
4248                                                 jd->interface_map[i*5 + t].flags = v->flags;
4249                                         }
4250                                         else {
4251                                                 jd->interface_map[i*5 + t].flags |= v->flags;
4252                                         }
4253
4254                                         sd.bptr->outvars[i] = copy->varnum;
4255                                 }
4256
4257                                 /* check if interface slots at basic block begin must be saved */
4258                                 IF_NO_INTRP(
4259                                         for (i=0; i<sd.bptr->indepth; ++i) {
4260                                                 varinfo *v = sd.var + sd.bptr->invars[i];
4261                                                 s4 t;
4262
4263                                                 t = v->type;
4264                                                 if (t == TYPE_RET)
4265                                                         t = TYPE_ADR;
4266
4267                                                 if (jd->interface_map[i*5 + t].flags == UNUSED) {
4268                                                         /* no interface var until now for this depth and */
4269                                                         /* type */
4270                                                         jd->interface_map[i*5 + t].flags = v->flags;
4271                                                 }
4272                                                 else {
4273                                                         jd->interface_map[i*5 + t].flags |= v->flags;
4274                                                 }
4275                                         }
4276                                 );
4277
4278                                 /* store the number of this block's variables */
4279
4280                                 sd.bptr->varcount = sd.vartop - sd.bptr->varstart;
4281
4282 #if defined(STACK_VERBOSE)
4283                                 stack_verbose_block_exit(&sd, superblockend);
4284 #endif
4285
4286                                 /* reach the following block, if any */
4287
4288                                 if (!superblockend)
4289                                         if (!stack_reach_next_block(&sd))
4290                                                 return false;
4291
4292                 } /* for blocks */
4293
4294         } while (sd.repeat && !deadcode);
4295
4296         /* XXX reset TYPE_RET to TYPE_ADR */
4297
4298         for (i=0; i<sd.vartop; ++i) {
4299                 if (sd.var[i].type == TYPE_RET)
4300                         sd.var[i].type = TYPE_ADR;
4301         }
4302
4303         /* XXX hack to fix up the ranges of the cloned single-block handlers */
4304
4305         ex = cd->exceptiontable;
4306         for (; ex != NULL; ex = ex->down) {
4307                 if (ex->start == ex->end) {
4308                         assert(ex->end->next);
4309                         ex->end = ex->end->next;
4310                 }
4311         }
4312
4313         /* gather statistics *****************************************************/
4314
4315 #if defined(ENABLE_STATISTICS)
4316         if (opt_stat) {
4317                 if (jd->new_basicblockcount > count_max_basic_blocks)
4318                         count_max_basic_blocks = jd->new_basicblockcount;
4319                 count_basic_blocks += jd->new_basicblockcount;
4320                 if (jd->new_instructioncount > count_max_javainstr)
4321                         count_max_javainstr = jd->new_instructioncount;
4322                 count_javainstr += jd->new_instructioncount;
4323                 if (jd->new_stackcount > count_upper_bound_new_stack)
4324                         count_upper_bound_new_stack = jd->new_stackcount;
4325                 if ((sd.new - jd->new_stack) > count_max_new_stack)
4326                         count_max_new_stack = (sd.new - jd->new_stack);
4327
4328                 sd.bptr = jd->new_basicblocks;
4329                 for (; sd.bptr; sd.bptr = sd.bptr->next) {
4330                         if (sd.bptr->flags > BBREACHED) {
4331                                 if (sd.bptr->indepth >= 10)
4332                                         count_block_stack[10]++;
4333                                 else
4334                                         count_block_stack[sd.bptr->indepth]++;
4335                                 len = sd.bptr->icount;
4336                                 if (len < 10)
4337                                         count_block_size_distribution[len]++;
4338                                 else if (len <= 12)
4339                                         count_block_size_distribution[10]++;
4340                                 else if (len <= 14)
4341                                         count_block_size_distribution[11]++;
4342                                 else if (len <= 16)
4343                                         count_block_size_distribution[12]++;
4344                                 else if (len <= 18)
4345                                         count_block_size_distribution[13]++;
4346                                 else if (len <= 20)
4347                                         count_block_size_distribution[14]++;
4348                                 else if (len <= 25)
4349                                         count_block_size_distribution[15]++;
4350                                 else if (len <= 30)
4351                                         count_block_size_distribution[16]++;
4352                                 else
4353                                         count_block_size_distribution[17]++;
4354                         }
4355                 }
4356
4357                 if (iteration_count == 1)
4358                         count_analyse_iterations[0]++;
4359                 else if (iteration_count == 2)
4360                         count_analyse_iterations[1]++;
4361                 else if (iteration_count == 3)
4362                         count_analyse_iterations[2]++;
4363                 else if (iteration_count == 4)
4364                         count_analyse_iterations[3]++;
4365                 else
4366                         count_analyse_iterations[4]++;
4367
4368                 if (jd->new_basicblockcount <= 5)
4369                         count_method_bb_distribution[0]++;
4370                 else if (jd->new_basicblockcount <= 10)
4371                         count_method_bb_distribution[1]++;
4372                 else if (jd->new_basicblockcount <= 15)
4373                         count_method_bb_distribution[2]++;
4374                 else if (jd->new_basicblockcount <= 20)
4375                         count_method_bb_distribution[3]++;
4376                 else if (jd->new_basicblockcount <= 30)
4377                         count_method_bb_distribution[4]++;
4378                 else if (jd->new_basicblockcount <= 40)
4379                         count_method_bb_distribution[5]++;
4380                 else if (jd->new_basicblockcount <= 50)
4381                         count_method_bb_distribution[6]++;
4382                 else if (jd->new_basicblockcount <= 75)
4383                         count_method_bb_distribution[7]++;
4384                 else
4385                         count_method_bb_distribution[8]++;
4386         }
4387 #endif /* defined(ENABLE_STATISTICS) */
4388
4389         /* everything's ok *******************************************************/
4390
4391         return true;
4392
4393         /* goto labels for throwing verifier exceptions **************************/
4394
4395 #if defined(ENABLE_VERIFIER)
4396
4397 throw_stack_underflow:
4398         exceptions_throw_verifyerror(m, "Unable to pop operand off an empty stack");
4399         return false;
4400
4401 throw_stack_overflow:
4402         exceptions_throw_verifyerror(m, "Stack size too large");
4403         return false;
4404
4405 throw_stack_depth_error:
4406         exceptions_throw_verifyerror(m,"Stack depth mismatch");
4407         return false;
4408
4409 throw_stack_type_error:
4410         exceptions_throw_verifyerror_for_stack(m, expectedtype);
4411         return false;
4412
4413 throw_stack_category_error:
4414         exceptions_throw_verifyerror(m, "Attempt to split long or double on the stack");
4415         return false;
4416
4417 #endif
4418 }
4419
4420
4421 /* functions for verbose stack analysis output ********************************/
4422
4423 #if defined(STACK_VERBOSE)
4424 static void stack_verbose_show_varinfo(stackdata_t *sd, varinfo *v)
4425 {
4426         printf("%c", show_jit_type_letters[v->type]);
4427         if (v->type == TYPE_RET)
4428                 printf("{L%03d}", v->vv.retaddr->nr);
4429 }
4430
4431
4432 static void stack_verbose_show_variable(stackdata_t *sd, s4 index)
4433 {
4434         assert(index >= 0 && index < sd->vartop);
4435         stack_verbose_show_varinfo(sd, sd->var + index);
4436 }
4437
4438
4439 static void stack_verbose_show_block(stackdata_t *sd, basicblock *bptr)
4440 {
4441         s4 i;
4442
4443         printf("L%03d type:%d in:%d [", bptr->nr, bptr->type, bptr->indepth);
4444         if (bptr->invars) {
4445                 for (i=0; i<bptr->indepth; ++i) {
4446                         if (i)
4447                                 putchar(' ');
4448                         stack_verbose_show_variable(sd, bptr->invars[i]);
4449                 }
4450         }
4451         else
4452                 putchar('-');
4453         printf("] inlocals [");
4454         if (bptr->inlocals) {
4455                 for (i=0; i<sd->localcount; ++i) {
4456                         if (i)
4457                                 putchar(' ');
4458                         stack_verbose_show_varinfo(sd, bptr->inlocals + i);
4459                 }
4460         }
4461         else
4462                 putchar('-');
4463         printf("] out:%d [", bptr->outdepth);
4464         if (bptr->outvars) {
4465                 for (i=0; i<bptr->outdepth; ++i) {
4466                         if (i)
4467                                 putchar(' ');
4468                         stack_verbose_show_variable(sd, bptr->outvars[i]);
4469                 }
4470         }
4471         else
4472                 putchar('-');
4473         printf("]");
4474
4475         if (bptr->original)
4476                 printf(" (clone of L%03d)", bptr->original->nr);
4477         else {
4478                 basicblock *b = bptr->copied_to;
4479                 if (b) {
4480                         printf(" (copied to ");
4481                         for (; b; b = b->copied_to)
4482                                 printf("L%03d ", b->nr);
4483                         printf(")");
4484                 }
4485         }
4486 }
4487
4488
4489 static void stack_verbose_block_enter(stackdata_t *sd, bool reanalyse)
4490 {
4491         int i;
4492
4493         printf("======================================== STACK %sANALYSE BLOCK ", 
4494                         (reanalyse) ? "RE-" : "");
4495         stack_verbose_show_block(sd, sd->bptr);
4496         printf("\n");
4497
4498         if (sd->handlers[0]) {
4499                 printf("HANDLERS: ");
4500                 for (i=0; sd->handlers[i]; ++i) {
4501                         printf("L%03d ", sd->handlers[i]->handler->nr);
4502                 }
4503                 printf("\n");
4504         }
4505         printf("\n");
4506 }
4507
4508
4509 static void stack_verbose_block_exit(stackdata_t *sd, bool superblockend)
4510 {
4511         printf("%s ", (superblockend) ? "SUPERBLOCKEND" : "END");
4512         stack_verbose_show_block(sd, sd->bptr);
4513         printf("\n");
4514 }
4515 #endif
4516
4517
4518 /*
4519  * These are local overrides for various environment variables in Emacs.
4520  * Please do not remove this and leave it at the end of the file, where
4521  * Emacs will automagically detect them.
4522  * ---------------------------------------------------------------------
4523  * Local variables:
4524  * mode: c
4525  * indent-tabs-mode: t
4526  * c-basic-offset: 4
4527  * tab-width: 4
4528  * End:
4529  * vim:noexpandtab:sw=4:ts=4:
4530  */