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