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