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