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