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