* src/vm/jit/stack.c (stack_grow_variable_array): Zero the additional
[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 5860 2006-10-29 23:37:20Z 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 maythrow;
1273         bool cloneinstructions;
1274         exception_entry *ex;
1275
1276 #if defined(STACK_VERBOSE)
1277         stack_verbose_block_enter(sd, true);
1278 #endif
1279
1280         b = sd->bptr;
1281
1282         if (!b->iinstr) {
1283                 orig = b->original;
1284                 assert(orig != NULL);
1285
1286                 /* clone the instruction list */
1287
1288                 cloneinstructions = true;
1289
1290                 assert(orig->iinstr);
1291                 len = orig->icount;
1292                 iptr = DMNEW(instruction, len + 1);
1293
1294                 MCOPY(iptr, orig->iinstr, instruction, len);
1295                 iptr[len].opc = ICMD_NOP;
1296                 b->iinstr = iptr;
1297                 b->icount = ++len;
1298
1299                 /* reserve space for the clone's block variables */
1300
1301                 stack_grow_variable_array(sd, orig->varcount);
1302
1303                 /* we already have the invars set */
1304
1305                 assert(b->indepth == orig->indepth);
1306
1307                 /* calculate relocation shifts for invars and block variables */
1308
1309                 if (orig->indepth) {
1310                         invarstart = orig->invars[0];
1311                         invarshift = b->invars[0] - invarstart;
1312                 }
1313                 else {
1314                         invarstart = INT_MAX;
1315                         invarshift = 0;
1316                 }
1317                 blockvarstart = orig->varstart;
1318                 blockvarshift = sd->vartop - blockvarstart;
1319
1320                 /* copy block variables */
1321
1322                 b->varstart = sd->vartop;
1323                 b->varcount = orig->varcount;
1324                 sd->vartop += b->varcount;
1325                 MCOPY(sd->var + b->varstart, sd->var + orig->varstart, varinfo, b->varcount);
1326
1327                 /* copy outvars */
1328
1329                 b->outdepth = orig->outdepth;
1330                 b->outvars = DMNEW(s4, orig->outdepth);
1331                 MCOPY(b->outvars, orig->outvars, s4, orig->outdepth);
1332
1333                 /* clone exception handlers */
1334
1335                 for (i=0; sd->handlers[i]; ++i) {
1336                         ex = DNEW(exception_entry);
1337                         ex->handler = sd->handlers[i]->handler;
1338                         ex->start = b;
1339                         ex->end = b; /* XXX hack, see end of stack_analyse */
1340                         ex->catchtype = sd->handlers[i]->catchtype;
1341                         ex->down = NULL;
1342
1343                         assert(sd->extableend->down == NULL);
1344                         sd->extableend->down = ex;
1345                         sd->extableend = ex;
1346                         sd->jd->exceptiontablelength++;
1347
1348                         sd->handlers[i] = ex;
1349                 }
1350         }
1351         else {
1352                 cloneinstructions = false;
1353                 invarshift = 0;
1354                 blockvarshift = 0;
1355                 invarstart = sd->vartop;
1356                 blockvarstart = sd->vartop;
1357                 iptr = b->iinstr;
1358         }
1359
1360         if (b->original) {
1361                 /* find exception handlers for the cloned block */
1362                 len = 0;
1363                 ex = sd->jd->exceptiontable;
1364                 for (; ex != NULL; ex = ex->down) {
1365                         /* XXX the cloned exception handlers have identical */
1366                         /* start end end blocks.                            */
1367                         if ((ex->start == b) && (ex->end == b)) {
1368                                 sd->handlers[len++] = ex;
1369                         }
1370                 }
1371                 sd->handlers[len] = NULL;
1372         }
1373
1374 #if defined(STACK_VERBOSE)
1375         printf("invarstart = %d, blockvarstart = %d\n", invarstart, blockvarstart);
1376         printf("invarshift = %d, blockvarshift = %d\n", invarshift, blockvarshift);
1377 #endif
1378
1379         /* mark block as finished */
1380
1381         b->flags = BBFINISHED;
1382
1383         /* initialize locals at the start of this block */
1384
1385         if (b->inlocals)
1386                 MCOPY(sd->var, b->inlocals, varinfo, sd->localcount);
1387
1388         /* reach exception handlers for this block */
1389
1390         if (!stack_reach_handlers(sd))
1391                 return false;
1392
1393         superblockend = false;
1394
1395         for (len = b->icount; len--; iptr++) {
1396 #if defined(STACK_VERBOSE)
1397                 show_icmd(sd->jd, iptr, false, SHOW_STACK);
1398                 printf("\n");
1399 #endif
1400
1401                 maythrow = false;
1402
1403                 switch (iptr->opc) {
1404                         case ICMD_RET:
1405                                 j = iptr->s1.varindex;
1406
1407 #if defined(ENABLE_VERIFIER)
1408                                 if (sd->var[j].type != TYPE_RET) {
1409                                         exceptions_throw_verifyerror(sd->m, "RET with non-returnAddress value");
1410                                         return false;
1411                                 }
1412 #endif
1413
1414                                 iptr->dst.block = stack_mark_reached_from_outvars(sd, sd->var[j].vv.retaddr);
1415                                 superblockend = true;
1416                                 break;
1417
1418                         case ICMD_JSR:
1419                                 iptr->sx.s23.s3.jsrtarget.block = stack_mark_reached_from_outvars(sd, iptr->sx.s23.s3.jsrtarget.block);
1420                                 superblockend = true;
1421                                 break;
1422
1423                         case ICMD_RETURN:
1424                                 superblockend = true;
1425                                 break;
1426
1427                         case ICMD_CHECKNULL:
1428                         case ICMD_PUTSTATICCONST:
1429                                 maythrow = true;
1430                                 break;
1431
1432                         case ICMD_NOP:
1433                         case ICMD_IINC:
1434                         case ICMD_INLINE_START:
1435                         case ICMD_INLINE_END:
1436                         case ICMD_INLINE_GOTO:
1437                                 break;
1438
1439                         case ICMD_GOTO:
1440                                 iptr->dst.block = stack_mark_reached_from_outvars(sd, iptr->dst.block);
1441                                 superblockend = true;
1442                                 break;
1443
1444                                 /* pop 0 push 1 const */
1445
1446                         case ICMD_ACONST:
1447                                 maythrow = true;
1448                         case ICMD_ICONST:
1449                         case ICMD_LCONST:
1450                         case ICMD_FCONST:
1451                         case ICMD_DCONST:
1452
1453                                 /* pop 0 push 1 load */
1454
1455                         case ICMD_ILOAD:
1456                         case ICMD_LLOAD:
1457                         case ICMD_FLOAD:
1458                         case ICMD_DLOAD:
1459                         case ICMD_ALOAD:
1460                                 RELOCATE(iptr->dst.varindex);
1461                                 break;
1462
1463                                 /* pop 2 push 1 */
1464
1465                         case ICMD_IALOAD:
1466                         case ICMD_LALOAD:
1467                         case ICMD_FALOAD:
1468                         case ICMD_DALOAD:
1469                         case ICMD_AALOAD:
1470                         case ICMD_BALOAD:
1471                         case ICMD_CALOAD:
1472                         case ICMD_SALOAD:
1473                                 RELOCATE(iptr->sx.s23.s2.varindex);
1474                                 RELOCATE(iptr->s1.varindex);
1475                                 RELOCATE(iptr->dst.varindex);
1476                                 maythrow = true;
1477                                 break;
1478
1479                                 /* pop 3 push 0 */
1480
1481                         case ICMD_IASTORE:
1482                         case ICMD_LASTORE:
1483                         case ICMD_FASTORE:
1484                         case ICMD_DASTORE:
1485                         case ICMD_AASTORE:
1486                         case ICMD_BASTORE:
1487                         case ICMD_CASTORE:
1488                         case ICMD_SASTORE:
1489                                 RELOCATE(iptr->sx.s23.s3.varindex);
1490                                 RELOCATE(iptr->sx.s23.s2.varindex);
1491                                 RELOCATE(iptr->s1.varindex);
1492                                 maythrow = true;
1493                                 break;
1494
1495                                 /* pop 1 push 0 store */
1496
1497                         case ICMD_ISTORE:
1498                         case ICMD_LSTORE:
1499                         case ICMD_FSTORE:
1500                         case ICMD_DSTORE:
1501                         case ICMD_ASTORE:
1502                                 RELOCATE(iptr->s1.varindex);
1503
1504                                 j = iptr->dst.varindex;
1505                                 COPY_VAL_AND_TYPE(*sd, iptr->s1.varindex, j);
1506                                 break;
1507
1508                                 /* pop 1 push 0 */
1509
1510                         case ICMD_ARETURN:
1511                         case ICMD_ATHROW:
1512                                 maythrow = true;
1513                         case ICMD_IRETURN:
1514                         case ICMD_LRETURN:
1515                         case ICMD_FRETURN:
1516                         case ICMD_DRETURN:
1517                                 RELOCATE(iptr->s1.varindex);
1518                                 superblockend = true;
1519                                 break;
1520
1521                         case ICMD_PUTSTATIC:
1522                         case ICMD_PUTFIELDCONST:
1523                                 maythrow = true;
1524                         case ICMD_POP:
1525                                 RELOCATE(iptr->s1.varindex);
1526                                 break;
1527
1528                                 /* pop 1 push 0 branch */
1529
1530                         case ICMD_IFNULL:
1531                         case ICMD_IFNONNULL:
1532
1533                         case ICMD_IFEQ:
1534                         case ICMD_IFNE:
1535                         case ICMD_IFLT:
1536                         case ICMD_IFGE:
1537                         case ICMD_IFGT:
1538                         case ICMD_IFLE:
1539
1540                         case ICMD_IF_LEQ:
1541                         case ICMD_IF_LNE:
1542                         case ICMD_IF_LLT:
1543                         case ICMD_IF_LGE:
1544                         case ICMD_IF_LGT:
1545                         case ICMD_IF_LLE:
1546                                 RELOCATE(iptr->s1.varindex);
1547                                 iptr->dst.block = stack_mark_reached_from_outvars(sd, iptr->dst.block);
1548                                 break;
1549
1550                                 /* pop 1 push 0 table branch */
1551
1552                         case ICMD_TABLESWITCH:
1553                                 i = iptr->sx.s23.s3.tablehigh - iptr->sx.s23.s2.tablelow + 1 + 1;
1554
1555                                 if (cloneinstructions) {
1556                                         table = DMNEW(branch_target_t, i);
1557                                         MCOPY(table, iptr->dst.table, branch_target_t, i);
1558                                         iptr->dst.table = table;
1559                                 }
1560                                 else {
1561                                         table = iptr->dst.table;
1562                                 }
1563
1564                                 RELOCATE(iptr->s1.varindex);
1565                                 while (i--) {
1566                                         table->block = stack_mark_reached_from_outvars(sd, table->block);
1567                                         table++;
1568                                 }
1569                                 superblockend = true;
1570                                 break;
1571
1572                         case ICMD_LOOKUPSWITCH:
1573                                 i = iptr->sx.s23.s2.lookupcount;
1574                                 if (cloneinstructions) {
1575                                         lookup = DMNEW(lookup_target_t, i);
1576                                         MCOPY(lookup, iptr->dst.lookup, lookup_target_t, i);
1577                                         iptr->dst.lookup = lookup;
1578                                 }
1579                                 else {
1580                                         lookup = iptr->dst.lookup;
1581                                 }
1582                                 RELOCATE(iptr->s1.varindex);
1583                                 while (i--) {
1584                                         lookup->target.block = stack_mark_reached_from_outvars(sd, lookup->target.block);
1585                                         lookup++;
1586                                 }
1587                                 iptr->sx.s23.s3.lookupdefault.block = stack_mark_reached_from_outvars(sd, iptr->sx.s23.s3.lookupdefault.block);
1588                                 superblockend = true;
1589                                 break;
1590
1591                         case ICMD_MONITORENTER:
1592                         case ICMD_MONITOREXIT:
1593                                 RELOCATE(iptr->s1.varindex);
1594                                 maythrow = true;
1595                                 break;
1596
1597                                 /* pop 2 push 0 branch */
1598
1599                         case ICMD_IF_ICMPEQ:
1600                         case ICMD_IF_ICMPNE:
1601                         case ICMD_IF_ICMPLT:
1602                         case ICMD_IF_ICMPGE:
1603                         case ICMD_IF_ICMPGT:
1604                         case ICMD_IF_ICMPLE:
1605
1606                         case ICMD_IF_LCMPEQ:
1607                         case ICMD_IF_LCMPNE:
1608                         case ICMD_IF_LCMPLT:
1609                         case ICMD_IF_LCMPGE:
1610                         case ICMD_IF_LCMPGT:
1611                         case ICMD_IF_LCMPLE:
1612
1613                         case ICMD_IF_FCMPEQ:
1614                         case ICMD_IF_FCMPNE:
1615
1616                         case ICMD_IF_FCMPL_LT:
1617                         case ICMD_IF_FCMPL_GE:
1618                         case ICMD_IF_FCMPL_GT:
1619                         case ICMD_IF_FCMPL_LE:
1620
1621                         case ICMD_IF_FCMPG_LT:
1622                         case ICMD_IF_FCMPG_GE:
1623                         case ICMD_IF_FCMPG_GT:
1624                         case ICMD_IF_FCMPG_LE:
1625
1626                         case ICMD_IF_DCMPEQ:
1627                         case ICMD_IF_DCMPNE:
1628
1629                         case ICMD_IF_DCMPL_LT:
1630                         case ICMD_IF_DCMPL_GE:
1631                         case ICMD_IF_DCMPL_GT:
1632                         case ICMD_IF_DCMPL_LE:
1633
1634                         case ICMD_IF_DCMPG_LT:
1635                         case ICMD_IF_DCMPG_GE:
1636                         case ICMD_IF_DCMPG_GT:
1637                         case ICMD_IF_DCMPG_LE:
1638
1639                         case ICMD_IF_ACMPEQ:
1640                         case ICMD_IF_ACMPNE:
1641                                 RELOCATE(iptr->sx.s23.s2.varindex);
1642                                 RELOCATE(iptr->s1.varindex);
1643                                 iptr->dst.block = stack_mark_reached_from_outvars(sd, iptr->dst.block);
1644                                 break;
1645
1646                                 /* pop 2 push 0 */
1647
1648                         case ICMD_PUTFIELD:
1649                         case ICMD_IASTORECONST:
1650                         case ICMD_LASTORECONST:
1651                         case ICMD_AASTORECONST:
1652                         case ICMD_BASTORECONST:
1653                         case ICMD_CASTORECONST:
1654                         case ICMD_SASTORECONST:
1655                                 maythrow = true;
1656                         case ICMD_POP2:
1657                                 RELOCATE(iptr->sx.s23.s2.varindex);
1658                                 RELOCATE(iptr->s1.varindex);
1659                                 break;
1660
1661                                 /* pop 0 push 1 copy */
1662
1663                         case ICMD_COPY:
1664                         case ICMD_MOVE:
1665                                 RELOCATE(iptr->dst.varindex);
1666                                 RELOCATE(iptr->s1.varindex);
1667                                 COPY_VAL_AND_TYPE(*sd, iptr->s1.varindex, iptr->dst.varindex);
1668                                 break;
1669
1670                                 /* pop 2 push 1 */
1671
1672                         case ICMD_IDIV:
1673                         case ICMD_IREM:
1674                         case ICMD_LDIV:
1675                         case ICMD_LREM:
1676                                 maythrow = true;
1677                         case ICMD_IADD:
1678                         case ICMD_ISUB:
1679                         case ICMD_IMUL:
1680                         case ICMD_ISHL:
1681                         case ICMD_ISHR:
1682                         case ICMD_IUSHR:
1683                         case ICMD_IAND:
1684                         case ICMD_IOR:
1685                         case ICMD_IXOR:
1686                         case ICMD_LADD:
1687                         case ICMD_LSUB:
1688                         case ICMD_LMUL:
1689                         case ICMD_LOR:
1690                         case ICMD_LAND:
1691                         case ICMD_LXOR:
1692                         case ICMD_LSHL:
1693                         case ICMD_LSHR:
1694                         case ICMD_LUSHR:
1695                         case ICMD_FADD:
1696                         case ICMD_FSUB:
1697                         case ICMD_FMUL:
1698                         case ICMD_FDIV:
1699                         case ICMD_FREM:
1700                         case ICMD_DADD:
1701                         case ICMD_DSUB:
1702                         case ICMD_DMUL:
1703                         case ICMD_DDIV:
1704                         case ICMD_DREM:
1705                         case ICMD_LCMP:
1706                         case ICMD_FCMPL:
1707                         case ICMD_FCMPG:
1708                         case ICMD_DCMPL:
1709                         case ICMD_DCMPG:
1710                                 RELOCATE(iptr->sx.s23.s2.varindex);
1711                                 RELOCATE(iptr->s1.varindex);
1712                                 RELOCATE(iptr->dst.varindex);
1713                                 break;
1714
1715                                 /* pop 1 push 1 */
1716
1717                         case ICMD_CHECKCAST:
1718                         case ICMD_ARRAYLENGTH:
1719                         case ICMD_INSTANCEOF:
1720                         case ICMD_NEWARRAY:
1721                         case ICMD_ANEWARRAY:
1722                                 maythrow = true;
1723                         case ICMD_GETFIELD:
1724                         case ICMD_IADDCONST:
1725                         case ICMD_ISUBCONST:
1726                         case ICMD_IMULCONST:
1727                         case ICMD_IMULPOW2:
1728                         case ICMD_IDIVPOW2:
1729                         case ICMD_IREMPOW2:
1730                         case ICMD_IANDCONST:
1731                         case ICMD_IORCONST:
1732                         case ICMD_IXORCONST:
1733                         case ICMD_ISHLCONST:
1734                         case ICMD_ISHRCONST:
1735                         case ICMD_IUSHRCONST:
1736                         case ICMD_LADDCONST:
1737                         case ICMD_LSUBCONST:
1738                         case ICMD_LMULCONST:
1739                         case ICMD_LMULPOW2:
1740                         case ICMD_LDIVPOW2:
1741                         case ICMD_LREMPOW2:
1742                         case ICMD_LANDCONST:
1743                         case ICMD_LORCONST:
1744                         case ICMD_LXORCONST:
1745                         case ICMD_LSHLCONST:
1746                         case ICMD_LSHRCONST:
1747                         case ICMD_LUSHRCONST:
1748                         case ICMD_INEG:
1749                         case ICMD_INT2BYTE:
1750                         case ICMD_INT2CHAR:
1751                         case ICMD_INT2SHORT:
1752                         case ICMD_LNEG:
1753                         case ICMD_FNEG:
1754                         case ICMD_DNEG:
1755                         case ICMD_I2L:
1756                         case ICMD_I2F:
1757                         case ICMD_I2D:
1758                         case ICMD_L2I:
1759                         case ICMD_L2F:
1760                         case ICMD_L2D:
1761                         case ICMD_F2I:
1762                         case ICMD_F2L:
1763                         case ICMD_F2D:
1764                         case ICMD_D2I:
1765                         case ICMD_D2L:
1766                         case ICMD_D2F:
1767                                 RELOCATE(iptr->s1.varindex);
1768                                 RELOCATE(iptr->dst.varindex);
1769                                 break;
1770
1771                                 /* pop 0 push 1 */
1772
1773                         case ICMD_GETSTATIC:
1774                         case ICMD_NEW:
1775                                 maythrow = true;
1776                                 RELOCATE(iptr->dst.varindex);
1777                                 break;
1778
1779                                 /* pop many push any */
1780
1781                         case ICMD_INVOKESTATIC:
1782                         case ICMD_INVOKESPECIAL:
1783                         case ICMD_INVOKEVIRTUAL:
1784                         case ICMD_INVOKEINTERFACE:
1785                         case ICMD_BUILTIN:
1786                         case ICMD_MULTIANEWARRAY:
1787                                 i = iptr->s1.argcount;
1788                                 if (cloneinstructions) {
1789                                         argp = DMNEW(s4, i);
1790                                         MCOPY(argp, iptr->sx.s23.s2.args, s4, i);
1791                                         iptr->sx.s23.s2.args = argp;
1792                                 }
1793                                 else {
1794                                         argp = iptr->sx.s23.s2.args;
1795                                 }
1796
1797                                 maythrow = true;
1798                                 while (--i >= 0) {
1799                                         RELOCATE(*argp);
1800                                         argp++;
1801                                 }
1802                                 RELOCATE(iptr->dst.varindex);
1803                                 break;
1804
1805                         default:
1806                                 *exceptionptr =
1807                                         new_internalerror("Unknown ICMD %d during stack re-analysis",
1808                                                         iptr->opc);
1809                                 return false;
1810                 } /* switch */
1811
1812 #if defined(STACK_VERBOSE)
1813                 show_icmd(sd->jd, iptr, false, SHOW_STACK);
1814                 printf("\n");
1815 #endif
1816         }
1817
1818         /* relocate outvars */
1819
1820         for (i=0; i<b->outdepth; ++i) {
1821                 RELOCATE(b->outvars[i]);
1822         }
1823
1824 #if defined(STACK_VERBOSE)
1825         stack_verbose_block_exit(sd, superblockend);
1826 #endif
1827
1828         /* propagate to the next block */
1829
1830         if (!superblockend)
1831                 if (!stack_reach_next_block(sd))
1832                         return false;
1833
1834         return true;
1835 }
1836
1837
1838 /* stack_change_to_tempvar *****************************************************
1839
1840    Change the given stackslot to a TEMPVAR. This includes creating a new
1841    temporary variable and changing the dst.varindex of the creator of the
1842    stacklot to the new variable index. If this stackslot has been passed
1843    through ICMDs between the point of its creation and the current point,
1844    then the variable index is also changed in these ICMDs.
1845
1846    IN:
1847       sd...........stack analysis data
1848           sp...........stackslot to change
1849           ilimit.......instruction up to which to look for ICMDs passing-through
1850                        the stackslot (exclusive). This may point exactly after the 
1851                                    last instruction, in which case the search is done to the
1852                                    basic block end.
1853
1854 *******************************************************************************/
1855
1856 static void stack_change_to_tempvar(stackdata_t *sd, stackptr sp, 
1857                                                                         instruction *ilimit)
1858 {
1859         s4 newindex;
1860         s4 oldindex;
1861         instruction *iptr;
1862         int i;
1863
1864         oldindex = sp->varnum;
1865
1866         /* create a new temporary variable */
1867
1868         GET_NEW_VAR(*sd, newindex, sp->type);
1869
1870         sd->var[newindex].flags = sp->flags;
1871
1872         /* change the stackslot */
1873
1874         sp->varnum = newindex;
1875         sp->varkind = TEMPVAR;
1876
1877         /* change the dst.varindex of the stackslot's creator */
1878
1879         if (sp->creator)
1880                 sp->creator->dst.varindex = newindex;
1881
1882         /* handle ICMDs this stackslot passed through, if any */
1883
1884         if (sp->flags & PASSTHROUGH) {
1885                 iptr = (sp->creator) ? (sp->creator + 1) : sd->bptr->iinstr;
1886
1887                 /* asser that the limit point to an ICMD, or after the last one */
1888                 assert(ilimit >= sd->bptr->iinstr);
1889                 assert(ilimit <= sd->bptr->iinstr + sd->bptr->icount);
1890
1891                 for (; iptr < ilimit; ++iptr) {
1892                         switch (iptr->opc) {
1893                                 case ICMD_INVOKESTATIC:
1894                                 case ICMD_INVOKESPECIAL:
1895                                 case ICMD_INVOKEVIRTUAL:
1896                                 case ICMD_INVOKEINTERFACE:
1897                                 case ICMD_BUILTIN:
1898
1899                                         for (i=0; i<iptr->s1.argcount; ++i)
1900                                                 if (iptr->sx.s23.s2.args[i] == oldindex) {
1901                                                         iptr->sx.s23.s2.args[i] = newindex;
1902                                                 }
1903                                         break;
1904                                 /* IMPORTANT: If any ICMD sets the PASSTHROUGH flag of a */
1905                                 /* stackslot, it must be added in this switch!           */
1906                         }
1907                 }
1908         }
1909 }
1910
1911
1912 /* stack_analyse ***************************************************************
1913
1914    Analyse_stack uses the intermediate code created by parse.c to
1915    build a model of the JVM operand stack for the current method.
1916    
1917    The following checks are performed:
1918      - check for operand stack underflow (before each instruction)
1919      - check for operand stack overflow (after[1] each instruction)
1920      - check for matching stack depth at merging points
1921      - check for matching basic types[2] at merging points
1922      - check basic types for instruction input (except for BUILTIN*
1923            opcodes, INVOKE* opcodes and MULTIANEWARRAY)
1924    
1925    [1]) Checking this after the instruction should be ok. parse.c
1926    counts the number of required stack slots in such a way that it is
1927    only vital that we don't exceed `maxstack` at basic block
1928    boundaries.
1929    
1930    [2]) 'basic types' means the distinction between INT, LONG, FLOAT,
1931    DOUBLE and ADDRESS types. Subtypes of INT and different ADDRESS
1932    types are not discerned.
1933
1934 *******************************************************************************/
1935
1936 bool stack_analyse(jitdata *jd)
1937 {
1938         methodinfo   *m;              /* method being analyzed                    */
1939         codeinfo     *code;
1940         registerdata *rd;
1941         stackdata_t   sd;
1942 #if defined(ENABLE_SSA)
1943         lsradata     *ls;
1944 #endif
1945         int           b_index;        /* basic block index                        */
1946         int           stackdepth;
1947         stackptr      curstack;       /* current stack top                        */
1948         stackptr      copy;
1949         int           opcode;         /* opcode of current instruction            */
1950         int           i, j;
1951         int           javaindex;
1952         int           len;            /* # of instructions after the current one  */
1953         bool          superblockend;  /* if true, no fallthrough to next block    */
1954         bool          deadcode;       /* true if no live code has been reached    */
1955         instruction  *iptr;           /* the current instruction                  */
1956         basicblock   *tbptr;
1957         basicblock   *original;
1958         exception_entry *ex;
1959
1960         stackptr     *last_store_boundary;
1961         stackptr      coalescing_boundary;
1962
1963         stackptr      src1, src2, src3, src4, dst1, dst2;
1964
1965         branch_target_t *table;
1966         lookup_target_t *lookup;
1967 #if defined(ENABLE_VERIFIER)
1968         int           expectedtype;   /* used by CHECK_BASIC_TYPE                 */
1969 #endif
1970         builtintable_entry *bte;
1971         methoddesc         *md;
1972         constant_FMIref    *fmiref;
1973 #if defined(ENABLE_STATISTICS)
1974         int           iteration_count;  /* number of iterations of analysis       */
1975 #endif
1976         int           new_index; /* used to get a new var index with GET_NEW_INDEX*/
1977
1978 #if defined(STACK_VERBOSE)
1979         show_method(jd, SHOW_PARSE);
1980 #endif
1981
1982         /* get required compiler data - initialization */
1983
1984         m    = jd->m;
1985         code = jd->code;
1986         rd   = jd->rd;
1987 #if defined(ENABLE_SSA)
1988         ls   = jd->ls;
1989 #endif
1990
1991         /* initialize the stackdata_t struct */
1992
1993         sd.m = m;
1994         sd.jd = jd;
1995         sd.varcount = jd->varcount;
1996         sd.vartop =  jd->vartop;
1997         sd.localcount = jd->localcount;
1998         sd.var = jd->var;
1999         sd.varsallocated = sd.varcount;
2000         sd.handlers = DMNEW(exception_entry *, jd->exceptiontablelength + 1);
2001
2002         /* prepare the variable for exception handler stacks               */
2003         /* (has been reserved by STACK_EXTRA_VARS, or VERIFIER_EXTRA_VARS) */
2004
2005         sd.exstack.type = TYPE_ADR;
2006         sd.exstack.prev = NULL;
2007         sd.exstack.varnum = sd.localcount;
2008         sd.var[sd.exstack.varnum].type = TYPE_ADR;
2009
2010 #if defined(ENABLE_LSRA)
2011         m->maxlifetimes = 0;
2012 #endif
2013
2014 #if defined(ENABLE_STATISTICS)
2015         iteration_count = 0;
2016 #endif
2017
2018         /* find the last real basic block */
2019         
2020         sd.last_real_block = NULL;
2021         tbptr = jd->basicblocks;
2022         while (tbptr->next) {
2023                 sd.last_real_block = tbptr;
2024                 tbptr = tbptr->next;
2025         }
2026         assert(sd.last_real_block);
2027
2028         /* find the last exception handler */
2029
2030         if (jd->exceptiontablelength)
2031                 sd.extableend = jd->exceptiontable + jd->exceptiontablelength - 1;
2032         else
2033                 sd.extableend = NULL;
2034
2035         /* init jd->interface_map */
2036
2037         jd->interface_map = DMNEW(interface_info, m->maxstack * 5);
2038         for (i = 0; i < m->maxstack * 5; i++)
2039                 jd->interface_map[i].flags = UNUSED;
2040
2041         last_store_boundary = DMNEW(stackptr, m->maxlocals);
2042
2043         /* initialize flags and invars (none) of first block */
2044
2045         jd->basicblocks[0].flags = BBREACHED;
2046         jd->basicblocks[0].invars = NULL;
2047         jd->basicblocks[0].indepth = 0;
2048         jd->basicblocks[0].inlocals = 
2049                 DMNEW(varinfo, jd->localcount + VERIFIER_EXTRA_LOCALS);
2050         MCOPY(jd->basicblocks[0].inlocals, jd->var, varinfo, 
2051                         jd->localcount + VERIFIER_EXTRA_LOCALS);
2052
2053         /* stack analysis loop (until fixpoint reached) **************************/
2054
2055         do {
2056 #if defined(ENABLE_STATISTICS)
2057                 iteration_count++;
2058 #endif
2059
2060                 /* initialize loop over basic blocks */
2061
2062                 sd.bptr = jd->basicblocks;
2063                 superblockend = true;
2064                 sd.repeat = false;
2065                 curstack = NULL; stackdepth = 0;
2066                 deadcode = true;
2067
2068                 /* iterate over basic blocks *****************************************/
2069
2070                 for (; sd.bptr; sd.bptr = sd.bptr->next) {
2071
2072                         if (sd.bptr->flags == BBDELETED) {
2073                                 /* This block has been deleted - do nothing. */
2074
2075                                 continue;
2076                         }
2077
2078                         if (sd.bptr->flags == BBTYPECHECK_REACHED) {
2079                                 /* re-analyse a block because its input changed */
2080                                 if (!stack_reanalyse_block(&sd))
2081                                         return false;
2082                                 superblockend = true; /* XXX */
2083                                 continue;
2084                         }
2085
2086                         if (superblockend && (sd.bptr->flags < BBREACHED)) {
2087                                 /* This block has not been reached so far, and we      */
2088                                 /* don't fall into it, so we'll have to iterate again. */
2089
2090                                 sd.repeat = true;
2091                                 continue;
2092                         }
2093
2094                         if (sd.bptr->flags > BBREACHED) {
2095                                 /* This block is already finished. */
2096
2097                                 superblockend = true;
2098                                 continue;
2099                         }
2100
2101                         if (sd.bptr->original && sd.bptr->original->flags < BBFINISHED) {
2102                                 /* This block is a clone and the original has not been */
2103                                 /* analysed, yet. Analyse it on the next iteration.    */
2104
2105                                 sd.repeat = true;
2106                                 /* XXX superblockend? */
2107                                 continue;
2108                         }
2109
2110                         /* This block has to be analysed now. */
2111
2112                         /* XXX The rest of this block is still indented one level too */
2113                         /* much in order to avoid a giant diff by changing that.      */
2114
2115                                 /* We know that sd.bptr->flags == BBREACHED. */
2116                                 /* This block has been reached before.    */
2117
2118                                 assert(sd.bptr->flags == BBREACHED);
2119                                 stackdepth = sd.bptr->indepth;
2120
2121                                 /* find exception handlers for this block */
2122
2123                                 /* determine the active exception handlers for this block */
2124                                 /* XXX could use a faster algorithm with sorted lists or  */
2125                                 /* something?                                             */
2126
2127                                 original = (sd.bptr->original) ? sd.bptr->original : sd.bptr;
2128
2129                                 len = 0;
2130                                 ex = jd->exceptiontable;
2131                                 for (; ex != NULL; ex = ex->down) {
2132                                         if ((ex->start <= original) && (ex->end > original)) {
2133                                                 sd.handlers[len++] = ex;
2134                                         }
2135                                 }
2136                                 sd.handlers[len] = NULL;
2137
2138
2139                                 /* reanalyse cloned block */
2140
2141                                 if (sd.bptr->original) {
2142                                         if (!stack_reanalyse_block(&sd))
2143                                                 return false;
2144                                         continue;
2145                                 }
2146
2147                                 /* reset the new pointer for allocating stackslots */
2148
2149                                 sd.new = jd->stack;
2150
2151                                 /* create the instack of this block */
2152
2153                                 curstack = stack_create_instack(&sd);
2154
2155                                 /* initialize locals at the start of this block */
2156
2157                                 if (sd.bptr->inlocals)
2158                                         MCOPY(sd.var, sd.bptr->inlocals, varinfo, sd.localcount);
2159
2160                                 /* set up local variables for analyzing this block */
2161
2162                                 deadcode = false;
2163                                 superblockend = false;
2164                                 len = sd.bptr->icount;
2165                                 iptr = sd.bptr->iinstr;
2166                                 b_index = sd.bptr - jd->basicblocks;
2167
2168                                 /* mark the block as analysed */
2169
2170                                 sd.bptr->flags = BBFINISHED;
2171
2172                                 /* reset variables for dependency checking */
2173
2174                                 coalescing_boundary = sd.new;
2175                                 for( i = 0; i < m->maxlocals; i++)
2176                                         last_store_boundary[i] = sd.new;
2177
2178                                 /* remember the start of this block's variables */
2179   
2180                                 sd.bptr->varstart = sd.vartop;
2181
2182 #if defined(STACK_VERBOSE)
2183                                 stack_verbose_block_enter(&sd, false);
2184 #endif
2185   
2186                                 /* reach exception handlers for this block */
2187
2188                                 if (!stack_reach_handlers(&sd))
2189                                         return false;
2190
2191                                 /* iterate over ICMDs ****************************************/
2192
2193                                 while (--len >= 0)  {
2194
2195 #if defined(STACK_VERBOSE)
2196                                         show_icmd(jd, iptr, false, SHOW_PARSE); printf("\n");
2197                                         for( copy = curstack; copy; copy = copy->prev ) {
2198                                                 printf("%2d(%d", copy->varnum, copy->type);
2199                                                 if (IS_INOUT(copy))
2200                                                         printf("S");
2201                                                 if (IS_PREALLOC(copy))
2202                                                         printf("A");
2203                                                 printf(") ");
2204                                         }
2205                                         printf("\n");
2206 #endif
2207
2208                                         /* fetch the current opcode */
2209
2210                                         opcode = iptr->opc;
2211
2212                                         /* automatically replace some ICMDs with builtins */
2213
2214 #if defined(USEBUILTINTABLE)
2215                                         bte = builtintable_get_automatic(opcode);
2216
2217                                         if (bte && bte->opcode == opcode) {
2218                                                 iptr->opc           = ICMD_BUILTIN;
2219                                                 iptr->flags.bits    = 0;
2220                                                 iptr->sx.s23.s3.bte = bte;
2221                                                 /* iptr->line is already set */
2222                                                 jd->isleafmethod = false;
2223                                                 goto icmd_BUILTIN;
2224                                         }
2225 #endif /* defined(USEBUILTINTABLE) */
2226
2227                                         /* main opcode switch *************************************/
2228
2229                                         switch (opcode) {
2230
2231                                                 /* pop 0 push 0 */
2232
2233                                         case ICMD_NOP:
2234 icmd_NOP:
2235                                                 CLR_SX;
2236                                                 OP0_0;
2237                                                 break;
2238
2239                                         case ICMD_CHECKNULL:
2240                                                 coalescing_boundary = sd.new;
2241                                                 COUNT(count_check_null);
2242                                                 USE_S1(TYPE_ADR);
2243                                                 CLR_SX;
2244                                                 iptr->dst.varindex = iptr->s1.varindex;
2245                                                 break;
2246
2247                                         case ICMD_RET:
2248                                                 j = iptr->s1.varindex = 
2249                                                         jd->local_map[iptr->s1.varindex * 5 + TYPE_ADR];
2250
2251 #if defined(ENABLE_VERIFIER)
2252                                                 if (sd.var[j].type != TYPE_RET) {
2253                                                         exceptions_throw_verifyerror(m, "RET with non-returnAddress value");
2254                                                         return false;
2255                                                 }
2256 #endif
2257                 
2258                                                 CLR_SX;
2259
2260                                                 iptr->dst.block = stack_mark_reached(&sd, sd.var[j].vv.retaddr, curstack, stackdepth);
2261                                                 superblockend = true;
2262                                                 break;
2263
2264                                         case ICMD_RETURN:
2265                                                 COUNT(count_pcmd_return);
2266                                                 CLR_SX;
2267                                                 OP0_0;
2268                                                 superblockend = true;
2269                                                 break;
2270
2271
2272                                                 /* pop 0 push 1 const */
2273
2274         /************************** ICONST OPTIMIZATIONS **************************/
2275
2276                                         case ICMD_ICONST:
2277                                                 COUNT(count_pcmd_load);
2278                                                 if (len == 0)
2279                                                         goto normal_ICONST;
2280
2281                                                 switch (iptr[1].opc) {
2282                                                         case ICMD_IADD:
2283                                                                 iptr->opc = ICMD_IADDCONST;
2284                                                                 /* FALLTHROUGH */
2285
2286                                                         icmd_iconst_tail:
2287                                                                 iptr[1].opc = ICMD_NOP;
2288                                                                 OP1_1(TYPE_INT, TYPE_INT);
2289                                                                 COUNT(count_pcmd_op);
2290                                                                 break;
2291
2292                                                         case ICMD_ISUB:
2293                                                                 iptr->opc = ICMD_ISUBCONST;
2294                                                                 goto icmd_iconst_tail;
2295 #if SUPPORT_CONST_MUL
2296                                                         case ICMD_IMUL:
2297                                                                 iptr->opc = ICMD_IMULCONST;
2298                                                                 goto icmd_iconst_tail;
2299 #else /* SUPPORT_CONST_MUL */
2300                                                         case ICMD_IMUL:
2301                                                                 if (iptr->sx.val.i == 0x00000002)
2302                                                                         iptr->sx.val.i = 1;
2303                                                                 else if (iptr->sx.val.i == 0x00000004)
2304                                                                         iptr->sx.val.i = 2;
2305                                                                 else if (iptr->sx.val.i == 0x00000008)
2306                                                                         iptr->sx.val.i = 3;
2307                                                                 else if (iptr->sx.val.i == 0x00000010)
2308                                                                         iptr->sx.val.i = 4;
2309                                                                 else if (iptr->sx.val.i == 0x00000020)
2310                                                                         iptr->sx.val.i = 5;
2311                                                                 else if (iptr->sx.val.i == 0x00000040)
2312                                                                         iptr->sx.val.i = 6;
2313                                                                 else if (iptr->sx.val.i == 0x00000080)
2314                                                                         iptr->sx.val.i = 7;
2315                                                                 else if (iptr->sx.val.i == 0x00000100)
2316                                                                         iptr->sx.val.i = 8;
2317                                                                 else if (iptr->sx.val.i == 0x00000200)
2318                                                                         iptr->sx.val.i = 9;
2319                                                                 else if (iptr->sx.val.i == 0x00000400)
2320                                                                         iptr->sx.val.i = 10;
2321                                                                 else if (iptr->sx.val.i == 0x00000800)
2322                                                                         iptr->sx.val.i = 11;
2323                                                                 else if (iptr->sx.val.i == 0x00001000)
2324                                                                         iptr->sx.val.i = 12;
2325                                                                 else if (iptr->sx.val.i == 0x00002000)
2326                                                                         iptr->sx.val.i = 13;
2327                                                                 else if (iptr->sx.val.i == 0x00004000)
2328                                                                         iptr->sx.val.i = 14;
2329                                                                 else if (iptr->sx.val.i == 0x00008000)
2330                                                                         iptr->sx.val.i = 15;
2331                                                                 else if (iptr->sx.val.i == 0x00010000)
2332                                                                         iptr->sx.val.i = 16;
2333                                                                 else if (iptr->sx.val.i == 0x00020000)
2334                                                                         iptr->sx.val.i = 17;
2335                                                                 else if (iptr->sx.val.i == 0x00040000)
2336                                                                         iptr->sx.val.i = 18;
2337                                                                 else if (iptr->sx.val.i == 0x00080000)
2338                                                                         iptr->sx.val.i = 19;
2339                                                                 else if (iptr->sx.val.i == 0x00100000)
2340                                                                         iptr->sx.val.i = 20;
2341                                                                 else if (iptr->sx.val.i == 0x00200000)
2342                                                                         iptr->sx.val.i = 21;
2343                                                                 else if (iptr->sx.val.i == 0x00400000)
2344                                                                         iptr->sx.val.i = 22;
2345                                                                 else if (iptr->sx.val.i == 0x00800000)
2346                                                                         iptr->sx.val.i = 23;
2347                                                                 else if (iptr->sx.val.i == 0x01000000)
2348                                                                         iptr->sx.val.i = 24;
2349                                                                 else if (iptr->sx.val.i == 0x02000000)
2350                                                                         iptr->sx.val.i = 25;
2351                                                                 else if (iptr->sx.val.i == 0x04000000)
2352                                                                         iptr->sx.val.i = 26;
2353                                                                 else if (iptr->sx.val.i == 0x08000000)
2354                                                                         iptr->sx.val.i = 27;
2355                                                                 else if (iptr->sx.val.i == 0x10000000)
2356                                                                         iptr->sx.val.i = 28;
2357                                                                 else if (iptr->sx.val.i == 0x20000000)
2358                                                                         iptr->sx.val.i = 29;
2359                                                                 else if (iptr->sx.val.i == 0x40000000)
2360                                                                         iptr->sx.val.i = 30;
2361                                                                 else if (iptr->sx.val.i == 0x80000000)
2362                                                                         iptr->sx.val.i = 31;
2363                                                                 else
2364                                                                         goto normal_ICONST;
2365
2366                                                                 iptr->opc = ICMD_IMULPOW2;
2367                                                                 goto icmd_iconst_tail;
2368 #endif /* SUPPORT_CONST_MUL */
2369                                                         case ICMD_IDIV:
2370                                                                 if (iptr->sx.val.i == 0x00000002)
2371                                                                         iptr->sx.val.i = 1;
2372                                                                 else if (iptr->sx.val.i == 0x00000004)
2373                                                                         iptr->sx.val.i = 2;
2374                                                                 else if (iptr->sx.val.i == 0x00000008)
2375                                                                         iptr->sx.val.i = 3;
2376                                                                 else if (iptr->sx.val.i == 0x00000010)
2377                                                                         iptr->sx.val.i = 4;
2378                                                                 else if (iptr->sx.val.i == 0x00000020)
2379                                                                         iptr->sx.val.i = 5;
2380                                                                 else if (iptr->sx.val.i == 0x00000040)
2381                                                                         iptr->sx.val.i = 6;
2382                                                                 else if (iptr->sx.val.i == 0x00000080)
2383                                                                         iptr->sx.val.i = 7;
2384                                                                 else if (iptr->sx.val.i == 0x00000100)
2385                                                                         iptr->sx.val.i = 8;
2386                                                                 else if (iptr->sx.val.i == 0x00000200)
2387                                                                         iptr->sx.val.i = 9;
2388                                                                 else if (iptr->sx.val.i == 0x00000400)
2389                                                                         iptr->sx.val.i = 10;
2390                                                                 else if (iptr->sx.val.i == 0x00000800)
2391                                                                         iptr->sx.val.i = 11;
2392                                                                 else if (iptr->sx.val.i == 0x00001000)
2393                                                                         iptr->sx.val.i = 12;
2394                                                                 else if (iptr->sx.val.i == 0x00002000)
2395                                                                         iptr->sx.val.i = 13;
2396                                                                 else if (iptr->sx.val.i == 0x00004000)
2397                                                                         iptr->sx.val.i = 14;
2398                                                                 else if (iptr->sx.val.i == 0x00008000)
2399                                                                         iptr->sx.val.i = 15;
2400                                                                 else if (iptr->sx.val.i == 0x00010000)
2401                                                                         iptr->sx.val.i = 16;
2402                                                                 else if (iptr->sx.val.i == 0x00020000)
2403                                                                         iptr->sx.val.i = 17;
2404                                                                 else if (iptr->sx.val.i == 0x00040000)
2405                                                                         iptr->sx.val.i = 18;
2406                                                                 else if (iptr->sx.val.i == 0x00080000)
2407                                                                         iptr->sx.val.i = 19;
2408                                                                 else if (iptr->sx.val.i == 0x00100000)
2409                                                                         iptr->sx.val.i = 20;
2410                                                                 else if (iptr->sx.val.i == 0x00200000)
2411                                                                         iptr->sx.val.i = 21;
2412                                                                 else if (iptr->sx.val.i == 0x00400000)
2413                                                                         iptr->sx.val.i = 22;
2414                                                                 else if (iptr->sx.val.i == 0x00800000)
2415                                                                         iptr->sx.val.i = 23;
2416                                                                 else if (iptr->sx.val.i == 0x01000000)
2417                                                                         iptr->sx.val.i = 24;
2418                                                                 else if (iptr->sx.val.i == 0x02000000)
2419                                                                         iptr->sx.val.i = 25;
2420                                                                 else if (iptr->sx.val.i == 0x04000000)
2421                                                                         iptr->sx.val.i = 26;
2422                                                                 else if (iptr->sx.val.i == 0x08000000)
2423                                                                         iptr->sx.val.i = 27;
2424                                                                 else if (iptr->sx.val.i == 0x10000000)
2425                                                                         iptr->sx.val.i = 28;
2426                                                                 else if (iptr->sx.val.i == 0x20000000)
2427                                                                         iptr->sx.val.i = 29;
2428                                                                 else if (iptr->sx.val.i == 0x40000000)
2429                                                                         iptr->sx.val.i = 30;
2430                                                                 else if (iptr->sx.val.i == 0x80000000)
2431                                                                         iptr->sx.val.i = 31;
2432                                                                 else
2433                                                                         goto normal_ICONST;
2434
2435                                                                 iptr->opc = ICMD_IDIVPOW2;
2436                                                                 goto icmd_iconst_tail;
2437
2438                                                         case ICMD_IREM:
2439                                                                 /*log_text("stack.c: ICMD_ICONST/ICMD_IREM");*/
2440                                                                 if ((iptr->sx.val.i == 0x00000002) ||
2441                                                                         (iptr->sx.val.i == 0x00000004) ||
2442                                                                         (iptr->sx.val.i == 0x00000008) ||
2443                                                                         (iptr->sx.val.i == 0x00000010) ||
2444                                                                         (iptr->sx.val.i == 0x00000020) ||
2445                                                                         (iptr->sx.val.i == 0x00000040) ||
2446                                                                         (iptr->sx.val.i == 0x00000080) ||
2447                                                                         (iptr->sx.val.i == 0x00000100) ||
2448                                                                         (iptr->sx.val.i == 0x00000200) ||
2449                                                                         (iptr->sx.val.i == 0x00000400) ||
2450                                                                         (iptr->sx.val.i == 0x00000800) ||
2451                                                                         (iptr->sx.val.i == 0x00001000) ||
2452                                                                         (iptr->sx.val.i == 0x00002000) ||
2453                                                                         (iptr->sx.val.i == 0x00004000) ||
2454                                                                         (iptr->sx.val.i == 0x00008000) ||
2455                                                                         (iptr->sx.val.i == 0x00010000) ||
2456                                                                         (iptr->sx.val.i == 0x00020000) ||
2457                                                                         (iptr->sx.val.i == 0x00040000) ||
2458                                                                         (iptr->sx.val.i == 0x00080000) ||
2459                                                                         (iptr->sx.val.i == 0x00100000) ||
2460                                                                         (iptr->sx.val.i == 0x00200000) ||
2461                                                                         (iptr->sx.val.i == 0x00400000) ||
2462                                                                         (iptr->sx.val.i == 0x00800000) ||
2463                                                                         (iptr->sx.val.i == 0x01000000) ||
2464                                                                         (iptr->sx.val.i == 0x02000000) ||
2465                                                                         (iptr->sx.val.i == 0x04000000) ||
2466                                                                         (iptr->sx.val.i == 0x08000000) ||
2467                                                                         (iptr->sx.val.i == 0x10000000) ||
2468                                                                         (iptr->sx.val.i == 0x20000000) ||
2469                                                                         (iptr->sx.val.i == 0x40000000) ||
2470                                                                         (iptr->sx.val.i == 0x80000000))
2471                                                                 {
2472                                                                         iptr->opc = ICMD_IREMPOW2;
2473                                                                         iptr->sx.val.i -= 1;
2474                                                                         goto icmd_iconst_tail;
2475                                                                 }
2476                                                                 goto normal_ICONST;
2477 #if SUPPORT_CONST_LOGICAL
2478                                                         case ICMD_IAND:
2479                                                                 iptr->opc = ICMD_IANDCONST;
2480                                                                 goto icmd_iconst_tail;
2481
2482                                                         case ICMD_IOR:
2483                                                                 iptr->opc = ICMD_IORCONST;
2484                                                                 goto icmd_iconst_tail;
2485
2486                                                         case ICMD_IXOR:
2487                                                                 iptr->opc = ICMD_IXORCONST;
2488                                                                 goto icmd_iconst_tail;
2489
2490 #endif /* SUPPORT_CONST_LOGICAL */
2491                                                         case ICMD_ISHL:
2492                                                                 iptr->opc = ICMD_ISHLCONST;
2493                                                                 goto icmd_iconst_tail;
2494
2495                                                         case ICMD_ISHR:
2496                                                                 iptr->opc = ICMD_ISHRCONST;
2497                                                                 goto icmd_iconst_tail;
2498
2499                                                         case ICMD_IUSHR:
2500                                                                 iptr->opc = ICMD_IUSHRCONST;
2501                                                                 goto icmd_iconst_tail;
2502 #if SUPPORT_LONG_SHIFT
2503                                                         case ICMD_LSHL:
2504                                                                 iptr->opc = ICMD_LSHLCONST;
2505                                                                 goto icmd_lconst_tail;
2506
2507                                                         case ICMD_LSHR:
2508                                                                 iptr->opc = ICMD_LSHRCONST;
2509                                                                 goto icmd_lconst_tail;
2510
2511                                                         case ICMD_LUSHR:
2512                                                                 iptr->opc = ICMD_LUSHRCONST;
2513                                                                 goto icmd_lconst_tail;
2514 #endif /* SUPPORT_LONG_SHIFT */
2515                                                         case ICMD_IF_ICMPEQ:
2516                                                                 iptr[1].opc = ICMD_IFEQ;
2517                                                                 /* FALLTHROUGH */
2518
2519                                                         icmd_if_icmp_tail:
2520                                                                 /* set the constant for the following icmd */
2521                                                                 iptr[1].sx.val.i = iptr->sx.val.i;
2522
2523                                                                 /* this instruction becomes a nop */
2524                                                                 iptr->opc = ICMD_NOP;
2525                                                                 goto icmd_NOP;
2526
2527                                                         case ICMD_IF_ICMPLT:
2528                                                                 iptr[1].opc = ICMD_IFLT;
2529                                                                 goto icmd_if_icmp_tail;
2530
2531                                                         case ICMD_IF_ICMPLE:
2532                                                                 iptr[1].opc = ICMD_IFLE;
2533                                                                 goto icmd_if_icmp_tail;
2534
2535                                                         case ICMD_IF_ICMPNE:
2536                                                                 iptr[1].opc = ICMD_IFNE;
2537                                                                 goto icmd_if_icmp_tail;
2538
2539                                                         case ICMD_IF_ICMPGT:
2540                                                                 iptr[1].opc = ICMD_IFGT;
2541                                                                 goto icmd_if_icmp_tail;
2542
2543                                                         case ICMD_IF_ICMPGE:
2544                                                                 iptr[1].opc = ICMD_IFGE;
2545                                                                 goto icmd_if_icmp_tail;
2546
2547 #if SUPPORT_CONST_STORE
2548                                                         case ICMD_IASTORE:
2549                                                         case ICMD_BASTORE:
2550                                                         case ICMD_CASTORE:
2551                                                         case ICMD_SASTORE:
2552 # if SUPPORT_CONST_STORE_ZERO_ONLY
2553                                                                 if (iptr->sx.val.i != 0)
2554                                                                         goto normal_ICONST;
2555 # endif
2556                                                                 switch (iptr[1].opc) {
2557                                                                         case ICMD_IASTORE:
2558                                                                                 iptr->opc = ICMD_IASTORECONST;
2559                                                                                 iptr->flags.bits |= INS_FLAG_CHECK;
2560                                                                                 break;
2561                                                                         case ICMD_BASTORE:
2562                                                                                 iptr->opc = ICMD_BASTORECONST;
2563                                                                                 iptr->flags.bits |= INS_FLAG_CHECK;
2564                                                                                 break;
2565                                                                         case ICMD_CASTORE:
2566                                                                                 iptr->opc = ICMD_CASTORECONST;
2567                                                                                 iptr->flags.bits |= INS_FLAG_CHECK;
2568                                                                                 break;
2569                                                                         case ICMD_SASTORE:
2570                                                                                 iptr->opc = ICMD_SASTORECONST;
2571                                                                                 iptr->flags.bits |= INS_FLAG_CHECK;
2572                                                                                 break;
2573                                                                 }
2574
2575                                                                 iptr[1].opc = ICMD_NOP;
2576
2577                                                                 /* copy the constant to s3 */
2578                                                                 /* XXX constval -> astoreconstval? */
2579                                                                 iptr->sx.s23.s3.constval = iptr->sx.val.i;
2580                                                                 OP2_0(TYPE_ADR, TYPE_INT);
2581                                                                 COUNT(count_pcmd_op);
2582                                                                 break;
2583
2584                                                         case ICMD_PUTSTATIC:
2585                                                         case ICMD_PUTFIELD:
2586 # if SUPPORT_CONST_STORE_ZERO_ONLY
2587                                                                 if (iptr->sx.val.i != 0)
2588                                                                         goto normal_ICONST;
2589 # endif
2590                                                                 /* XXX check field type? */
2591
2592                                                                 /* copy the constant to s2 */
2593                                                                 /* XXX constval -> fieldconstval? */
2594                                                                 iptr->sx.s23.s2.constval = iptr->sx.val.i;
2595
2596 putconst_tail:
2597                                                                 /* set the field reference (s3) */
2598                                                                 if (iptr[1].flags.bits & INS_FLAG_UNRESOLVED) {
2599                                                                         iptr->sx.s23.s3.uf = iptr[1].sx.s23.s3.uf;
2600                                                                         iptr->flags.bits |= INS_FLAG_UNRESOLVED;
2601                                                                         fmiref = iptr->sx.s23.s3.uf->fieldref;
2602                                                                 }
2603                                                                 else {
2604                                                                         fmiref = iptr[1].sx.s23.s3.fmiref;
2605                                                                         iptr->sx.s23.s3.fmiref = fmiref;
2606                                                                 }
2607
2608 #if defined(ENABLE_VERIFIER)
2609                                                                 expectedtype = fmiref->parseddesc.fd->type;
2610                                                                 switch (iptr[0].opc) {
2611                                                                         case ICMD_ICONST:
2612                                                                                 if (expectedtype != TYPE_INT)
2613                                                                                         goto throw_stack_type_error;
2614                                                                                 break;
2615                                                                         case ICMD_LCONST:
2616                                                                                 if (expectedtype != TYPE_LNG)
2617                                                                                         goto throw_stack_type_error;
2618                                                                                 break;
2619                                                                         case ICMD_ACONST:
2620                                                                                 if (expectedtype != TYPE_ADR)
2621                                                                                         goto throw_stack_type_error;
2622                                                                                 break;
2623                                                                         default:
2624                                                                                 assert(0);
2625                                                                 }
2626 #endif /* defined(ENABLE_VERIFIER) */
2627                                                                 
2628                                                                 switch (iptr[1].opc) {
2629                                                                         case ICMD_PUTSTATIC:
2630                                                                                 iptr->opc = ICMD_PUTSTATICCONST;
2631                                                                                 OP0_0;
2632                                                                                 break;
2633                                                                         case ICMD_PUTFIELD:
2634                                                                                 iptr->opc = ICMD_PUTFIELDCONST;
2635                                                                                 OP1_0(TYPE_ADR);
2636                                                                                 break;
2637                                                                 }
2638
2639                                                                 iptr[1].opc = ICMD_NOP;
2640                                                                 COUNT(count_pcmd_op);
2641                                                                 break;
2642 #endif /* SUPPORT_CONST_STORE */
2643
2644                                                         default:
2645                                                                 goto normal_ICONST;
2646                                                 }
2647
2648                                                 /* if we get here, the ICONST has been optimized */
2649                                                 break;
2650
2651 normal_ICONST:
2652                                                 /* normal case of an unoptimized ICONST */
2653                                                 OP0_1(TYPE_INT);
2654                                                 break;
2655
2656         /************************** LCONST OPTIMIZATIONS **************************/
2657
2658                                         case ICMD_LCONST:
2659                                                 COUNT(count_pcmd_load);
2660                                                 if (len == 0)
2661                                                         goto normal_LCONST;
2662
2663                                                 /* switch depending on the following instruction */
2664
2665                                                 switch (iptr[1].opc) {
2666 #if SUPPORT_LONG_ADD
2667                                                         case ICMD_LADD:
2668                                                                 iptr->opc = ICMD_LADDCONST;
2669                                                                 /* FALLTHROUGH */
2670
2671                                                         icmd_lconst_tail:
2672                                                                 /* instruction of type LONG -> LONG */
2673                                                                 iptr[1].opc = ICMD_NOP;
2674                                                                 OP1_1(TYPE_LNG, TYPE_LNG);
2675                                                                 COUNT(count_pcmd_op);
2676                                                                 break;
2677
2678                                                         case ICMD_LSUB:
2679                                                                 iptr->opc = ICMD_LSUBCONST;
2680                                                                 goto icmd_lconst_tail;
2681
2682 #endif /* SUPPORT_LONG_ADD */
2683 #if SUPPORT_LONG_MUL && SUPPORT_CONST_MUL
2684                                                         case ICMD_LMUL:
2685                                                                 iptr->opc = ICMD_LMULCONST;
2686                                                                 goto icmd_lconst_tail;
2687 #else /* SUPPORT_LONG_MUL && SUPPORT_CONST_MUL */
2688 # if SUPPORT_LONG_SHIFT
2689                                                         case ICMD_LMUL:
2690                                                                 if (iptr->sx.val.l == 0x00000002)
2691                                                                         iptr->sx.val.i = 1;
2692                                                                 else if (iptr->sx.val.l == 0x00000004)
2693                                                                         iptr->sx.val.i = 2;
2694                                                                 else if (iptr->sx.val.l == 0x00000008)
2695                                                                         iptr->sx.val.i = 3;
2696                                                                 else if (iptr->sx.val.l == 0x00000010)
2697                                                                         iptr->sx.val.i = 4;
2698                                                                 else if (iptr->sx.val.l == 0x00000020)
2699                                                                         iptr->sx.val.i = 5;
2700                                                                 else if (iptr->sx.val.l == 0x00000040)
2701                                                                         iptr->sx.val.i = 6;
2702                                                                 else if (iptr->sx.val.l == 0x00000080)
2703                                                                         iptr->sx.val.i = 7;
2704                                                                 else if (iptr->sx.val.l == 0x00000100)
2705                                                                         iptr->sx.val.i = 8;
2706                                                                 else if (iptr->sx.val.l == 0x00000200)
2707                                                                         iptr->sx.val.i = 9;
2708                                                                 else if (iptr->sx.val.l == 0x00000400)
2709                                                                         iptr->sx.val.i = 10;
2710                                                                 else if (iptr->sx.val.l == 0x00000800)
2711                                                                         iptr->sx.val.i = 11;
2712                                                                 else if (iptr->sx.val.l == 0x00001000)
2713                                                                         iptr->sx.val.i = 12;
2714                                                                 else if (iptr->sx.val.l == 0x00002000)
2715                                                                         iptr->sx.val.i = 13;
2716                                                                 else if (iptr->sx.val.l == 0x00004000)
2717                                                                         iptr->sx.val.i = 14;
2718                                                                 else if (iptr->sx.val.l == 0x00008000)
2719                                                                         iptr->sx.val.i = 15;
2720                                                                 else if (iptr->sx.val.l == 0x00010000)
2721                                                                         iptr->sx.val.i = 16;
2722                                                                 else if (iptr->sx.val.l == 0x00020000)
2723                                                                         iptr->sx.val.i = 17;
2724                                                                 else if (iptr->sx.val.l == 0x00040000)
2725                                                                         iptr->sx.val.i = 18;
2726                                                                 else if (iptr->sx.val.l == 0x00080000)
2727                                                                         iptr->sx.val.i = 19;
2728                                                                 else if (iptr->sx.val.l == 0x00100000)
2729                                                                         iptr->sx.val.i = 20;
2730                                                                 else if (iptr->sx.val.l == 0x00200000)
2731                                                                         iptr->sx.val.i = 21;
2732                                                                 else if (iptr->sx.val.l == 0x00400000)
2733                                                                         iptr->sx.val.i = 22;
2734                                                                 else if (iptr->sx.val.l == 0x00800000)
2735                                                                         iptr->sx.val.i = 23;
2736                                                                 else if (iptr->sx.val.l == 0x01000000)
2737                                                                         iptr->sx.val.i = 24;
2738                                                                 else if (iptr->sx.val.l == 0x02000000)
2739                                                                         iptr->sx.val.i = 25;
2740                                                                 else if (iptr->sx.val.l == 0x04000000)
2741                                                                         iptr->sx.val.i = 26;
2742                                                                 else if (iptr->sx.val.l == 0x08000000)
2743                                                                         iptr->sx.val.i = 27;
2744                                                                 else if (iptr->sx.val.l == 0x10000000)
2745                                                                         iptr->sx.val.i = 28;
2746                                                                 else if (iptr->sx.val.l == 0x20000000)
2747                                                                         iptr->sx.val.i = 29;
2748                                                                 else if (iptr->sx.val.l == 0x40000000)
2749                                                                         iptr->sx.val.i = 30;
2750                                                                 else if (iptr->sx.val.l == 0x80000000)
2751                                                                         iptr->sx.val.i = 31;
2752                                                                 else {
2753                                                                         goto normal_LCONST;
2754                                                                 }
2755                                                                 iptr->opc = ICMD_LMULPOW2;
2756                                                                 goto icmd_lconst_tail;
2757 # endif /* SUPPORT_LONG_SHIFT */
2758 #endif /* SUPPORT_LONG_MUL && SUPPORT_CONST_MUL */
2759 #if SUPPORT_LONG_DIV_POW2
2760                                                         case ICMD_LDIV:
2761                                                                 if (iptr->sx.val.l == 0x00000002)
2762                                                                         iptr->sx.val.i = 1;
2763                                                                 else if (iptr->sx.val.l == 0x00000004)
2764                                                                         iptr->sx.val.i = 2;
2765                                                                 else if (iptr->sx.val.l == 0x00000008)
2766                                                                         iptr->sx.val.i = 3;
2767                                                                 else if (iptr->sx.val.l == 0x00000010)
2768                                                                         iptr->sx.val.i = 4;
2769                                                                 else if (iptr->sx.val.l == 0x00000020)
2770                                                                         iptr->sx.val.i = 5;
2771                                                                 else if (iptr->sx.val.l == 0x00000040)
2772                                                                         iptr->sx.val.i = 6;
2773                                                                 else if (iptr->sx.val.l == 0x00000080)
2774                                                                         iptr->sx.val.i = 7;
2775                                                                 else if (iptr->sx.val.l == 0x00000100)
2776                                                                         iptr->sx.val.i = 8;
2777                                                                 else if (iptr->sx.val.l == 0x00000200)
2778                                                                         iptr->sx.val.i = 9;
2779                                                                 else if (iptr->sx.val.l == 0x00000400)
2780                                                                         iptr->sx.val.i = 10;
2781                                                                 else if (iptr->sx.val.l == 0x00000800)
2782                                                                         iptr->sx.val.i = 11;
2783                                                                 else if (iptr->sx.val.l == 0x00001000)
2784                                                                         iptr->sx.val.i = 12;
2785                                                                 else if (iptr->sx.val.l == 0x00002000)
2786                                                                         iptr->sx.val.i = 13;
2787                                                                 else if (iptr->sx.val.l == 0x00004000)
2788                                                                         iptr->sx.val.i = 14;
2789                                                                 else if (iptr->sx.val.l == 0x00008000)
2790                                                                         iptr->sx.val.i = 15;
2791                                                                 else if (iptr->sx.val.l == 0x00010000)
2792                                                                         iptr->sx.val.i = 16;
2793                                                                 else if (iptr->sx.val.l == 0x00020000)
2794                                                                         iptr->sx.val.i = 17;
2795                                                                 else if (iptr->sx.val.l == 0x00040000)
2796                                                                         iptr->sx.val.i = 18;
2797                                                                 else if (iptr->sx.val.l == 0x00080000)
2798                                                                         iptr->sx.val.i = 19;
2799                                                                 else if (iptr->sx.val.l == 0x00100000)
2800                                                                         iptr->sx.val.i = 20;
2801                                                                 else if (iptr->sx.val.l == 0x00200000)
2802                                                                         iptr->sx.val.i = 21;
2803                                                                 else if (iptr->sx.val.l == 0x00400000)
2804                                                                         iptr->sx.val.i = 22;
2805                                                                 else if (iptr->sx.val.l == 0x00800000)
2806                                                                         iptr->sx.val.i = 23;
2807                                                                 else if (iptr->sx.val.l == 0x01000000)
2808                                                                         iptr->sx.val.i = 24;
2809                                                                 else if (iptr->sx.val.l == 0x02000000)
2810                                                                         iptr->sx.val.i = 25;
2811                                                                 else if (iptr->sx.val.l == 0x04000000)
2812                                                                         iptr->sx.val.i = 26;
2813                                                                 else if (iptr->sx.val.l == 0x08000000)
2814                                                                         iptr->sx.val.i = 27;
2815                                                                 else if (iptr->sx.val.l == 0x10000000)
2816                                                                         iptr->sx.val.i = 28;
2817                                                                 else if (iptr->sx.val.l == 0x20000000)
2818                                                                         iptr->sx.val.i = 29;
2819                                                                 else if (iptr->sx.val.l == 0x40000000)
2820                                                                         iptr->sx.val.i = 30;
2821                                                                 else if (iptr->sx.val.l == 0x80000000)
2822                                                                         iptr->sx.val.i = 31;
2823                                                                 else {
2824                                                                         goto normal_LCONST;
2825                                                                 }
2826                                                                 iptr->opc = ICMD_LDIVPOW2;
2827                                                                 goto icmd_lconst_tail;
2828 #endif /* SUPPORT_LONG_DIV_POW2 */
2829
2830 #if SUPPORT_LONG_REM_POW2
2831                                                         case ICMD_LREM:
2832                                                                 if ((iptr->sx.val.l == 0x00000002) ||
2833                                                                         (iptr->sx.val.l == 0x00000004) ||
2834                                                                         (iptr->sx.val.l == 0x00000008) ||
2835                                                                         (iptr->sx.val.l == 0x00000010) ||
2836                                                                         (iptr->sx.val.l == 0x00000020) ||
2837                                                                         (iptr->sx.val.l == 0x00000040) ||
2838                                                                         (iptr->sx.val.l == 0x00000080) ||
2839                                                                         (iptr->sx.val.l == 0x00000100) ||
2840                                                                         (iptr->sx.val.l == 0x00000200) ||
2841                                                                         (iptr->sx.val.l == 0x00000400) ||
2842                                                                         (iptr->sx.val.l == 0x00000800) ||
2843                                                                         (iptr->sx.val.l == 0x00001000) ||
2844                                                                         (iptr->sx.val.l == 0x00002000) ||
2845                                                                         (iptr->sx.val.l == 0x00004000) ||
2846                                                                         (iptr->sx.val.l == 0x00008000) ||
2847                                                                         (iptr->sx.val.l == 0x00010000) ||
2848                                                                         (iptr->sx.val.l == 0x00020000) ||
2849                                                                         (iptr->sx.val.l == 0x00040000) ||
2850                                                                         (iptr->sx.val.l == 0x00080000) ||
2851                                                                         (iptr->sx.val.l == 0x00100000) ||
2852                                                                         (iptr->sx.val.l == 0x00200000) ||
2853                                                                         (iptr->sx.val.l == 0x00400000) ||
2854                                                                         (iptr->sx.val.l == 0x00800000) ||
2855                                                                         (iptr->sx.val.l == 0x01000000) ||
2856                                                                         (iptr->sx.val.l == 0x02000000) ||
2857                                                                         (iptr->sx.val.l == 0x04000000) ||
2858                                                                         (iptr->sx.val.l == 0x08000000) ||
2859                                                                         (iptr->sx.val.l == 0x10000000) ||
2860                                                                         (iptr->sx.val.l == 0x20000000) ||
2861                                                                         (iptr->sx.val.l == 0x40000000) ||
2862                                                                         (iptr->sx.val.l == 0x80000000))
2863                                                                 {
2864                                                                         iptr->opc = ICMD_LREMPOW2;
2865                                                                         iptr->sx.val.l -= 1;
2866                                                                         goto icmd_lconst_tail;
2867                                                                 }
2868                                                                 goto normal_LCONST;
2869 #endif /* SUPPORT_LONG_REM_POW2 */
2870
2871 #if SUPPORT_LONG_LOGICAL && SUPPORT_CONST_LOGICAL
2872
2873                                                         case ICMD_LAND:
2874                                                                 iptr->opc = ICMD_LANDCONST;
2875                                                                 goto icmd_lconst_tail;
2876
2877                                                         case ICMD_LOR:
2878                                                                 iptr->opc = ICMD_LORCONST;
2879                                                                 goto icmd_lconst_tail;
2880
2881                                                         case ICMD_LXOR:
2882                                                                 iptr->opc = ICMD_LXORCONST;
2883                                                                 goto icmd_lconst_tail;
2884 #endif /* SUPPORT_LONG_LOGICAL && SUPPORT_CONST_LOGICAL */
2885
2886 #if SUPPORT_LONG_CMP_CONST
2887                                                         case ICMD_LCMP:
2888                                                                 if ((len <= 1) || (iptr[2].sx.val.i != 0))
2889                                                                         goto normal_LCONST;
2890
2891                                                                 /* switch on the instruction after LCONST - LCMP */
2892
2893                                                                 switch (iptr[2].opc) {
2894                                                                         case ICMD_IFEQ:
2895                                                                                 iptr->opc = ICMD_IF_LEQ;
2896                                                                                 /* FALLTHROUGH */
2897
2898                                                                         icmd_lconst_lcmp_tail:
2899                                                                                 /* convert LCONST, LCMP, IFXX to IF_LXX */
2900                                                                                 iptr->dst.insindex = iptr[2].dst.insindex;
2901                                                                                 iptr[1].opc = ICMD_NOP;
2902                                                                                 iptr[2].opc = ICMD_NOP;
2903
2904                                                                                 OP1_BRANCH(TYPE_LNG);
2905                                                                                 BRANCH(tbptr);
2906                                                                                 COUNT(count_pcmd_bra);
2907                                                                                 COUNT(count_pcmd_op);
2908                                                                                 break;
2909
2910                                                                         case ICMD_IFNE:
2911                                                                                 iptr->opc = ICMD_IF_LNE;
2912                                                                                 goto icmd_lconst_lcmp_tail;
2913
2914                                                                         case ICMD_IFLT:
2915                                                                                 iptr->opc = ICMD_IF_LLT;
2916                                                                                 goto icmd_lconst_lcmp_tail;
2917
2918                                                                         case ICMD_IFGT:
2919                                                                                 iptr->opc = ICMD_IF_LGT;
2920                                                                                 goto icmd_lconst_lcmp_tail;
2921
2922                                                                         case ICMD_IFLE:
2923                                                                                 iptr->opc = ICMD_IF_LLE;
2924                                                                                 goto icmd_lconst_lcmp_tail;
2925
2926                                                                         case ICMD_IFGE:
2927                                                                                 iptr->opc = ICMD_IF_LGE;
2928                                                                                 goto icmd_lconst_lcmp_tail;
2929
2930                                                                         default:
2931                                                                                 goto normal_LCONST;
2932                                                                 } /* end switch on opcode after LCONST - LCMP */
2933                                                                 break;
2934 #endif /* SUPPORT_LONG_CMP_CONST */
2935
2936 #if SUPPORT_CONST_STORE
2937                                                         case ICMD_LASTORE:
2938 # if SUPPORT_CONST_STORE_ZERO_ONLY
2939                                                                 if (iptr->sx.val.l != 0)
2940                                                                         goto normal_LCONST;
2941 # endif
2942 #if SIZEOF_VOID_P == 4
2943                                                                 /* the constant must fit into a ptrint */
2944                                                                 if (iptr->sx.val.l < -0x80000000L || iptr->sx.val.l >= 0x80000000L)
2945                                                                         goto normal_LCONST;
2946 #endif
2947                                                                 /* move the constant to s3 */
2948                                                                 iptr->sx.s23.s3.constval = iptr->sx.val.l;
2949
2950                                                                 iptr->opc = ICMD_LASTORECONST;
2951                                                                 iptr->flags.bits |= INS_FLAG_CHECK;
2952                                                                 OP2_0(TYPE_ADR, TYPE_INT);
2953
2954                                                                 iptr[1].opc = ICMD_NOP;
2955                                                                 COUNT(count_pcmd_op);
2956                                                                 break;
2957
2958                                                         case ICMD_PUTSTATIC:
2959                                                         case ICMD_PUTFIELD:
2960 # if SUPPORT_CONST_STORE_ZERO_ONLY
2961                                                                 if (iptr->sx.val.l != 0)
2962                                                                         goto normal_LCONST;
2963 # endif
2964 #if SIZEOF_VOID_P == 4
2965                                                                 /* the constant must fit into a ptrint */
2966                                                                 if (iptr->sx.val.l < -0x80000000L || iptr->sx.val.l >= 0x80000000L)
2967                                                                         goto normal_LCONST;
2968 #endif
2969                                                                 /* XXX check field type? */
2970
2971                                                                 /* copy the constant to s2 */
2972                                                                 /* XXX constval -> fieldconstval? */
2973                                                                 iptr->sx.s23.s2.constval = iptr->sx.val.l;
2974
2975                                                                 goto putconst_tail;
2976
2977 #endif /* SUPPORT_CONST_STORE */
2978
2979                                                         default:
2980                                                                 goto normal_LCONST;
2981                                                 } /* end switch opcode after LCONST */
2982
2983                                                 /* if we get here, the LCONST has been optimized */
2984                                                 break;
2985
2986 normal_LCONST:
2987                                                 /* the normal case of an unoptimized LCONST */
2988                                                 OP0_1(TYPE_LNG);
2989                                                 break;
2990
2991         /************************ END OF LCONST OPTIMIZATIONS *********************/
2992
2993                                         case ICMD_FCONST:
2994                                                 COUNT(count_pcmd_load);
2995                                                 OP0_1(TYPE_FLT);
2996                                                 break;
2997
2998                                         case ICMD_DCONST:
2999                                                 COUNT(count_pcmd_load);
3000                                                 OP0_1(TYPE_DBL);
3001                                                 break;
3002
3003         /************************** ACONST OPTIMIZATIONS **************************/
3004
3005                                         case ICMD_ACONST:
3006                                                 coalescing_boundary = sd.new;
3007                                                 COUNT(count_pcmd_load);
3008 #if SUPPORT_CONST_STORE
3009                                                 /* We can only optimize if the ACONST is resolved
3010                                                  * and there is an instruction after it. */
3011
3012                                                 if ((len == 0) || (iptr->flags.bits & INS_FLAG_UNRESOLVED))
3013                                                         goto normal_ACONST;
3014
3015                                                 switch (iptr[1].opc) {
3016                                                         case ICMD_AASTORE:
3017                                                                 /* We can only optimize for NULL values
3018                                                                  * here because otherwise a checkcast is
3019                                                                  * required. */
3020                                                                 if (iptr->sx.val.anyptr != NULL)
3021                                                                         goto normal_ACONST;
3022
3023                                                                 /* copy the constant (NULL) to s3 */
3024                                                                 iptr->sx.s23.s3.constval = 0;
3025                                                                 iptr->opc = ICMD_AASTORECONST;
3026                                                                 iptr->flags.bits |= INS_FLAG_CHECK;
3027                                                                 OP2_0(TYPE_ADR, TYPE_INT);
3028
3029                                                                 iptr[1].opc = ICMD_NOP;
3030                                                                 COUNT(count_pcmd_op);
3031                                                                 break;
3032
3033                                                         case ICMD_PUTSTATIC:
3034                                                         case ICMD_PUTFIELD:
3035 # if SUPPORT_CONST_STORE_ZERO_ONLY
3036                                                                 if (iptr->sx.val.anyptr != NULL)
3037                                                                         goto normal_ACONST;
3038 # endif
3039                                                                 /* XXX check field type? */
3040                                                                 /* copy the constant to s2 */
3041                                                                 /* XXX constval -> fieldconstval? */
3042                                                                 iptr->sx.s23.s2.constval = (ptrint) iptr->sx.val.anyptr;
3043
3044                                                                 goto putconst_tail;
3045
3046                                                         default:
3047                                                                 goto normal_ACONST;
3048                                                 }
3049
3050                                                 /* if we get here the ACONST has been optimized */
3051                                                 break;
3052
3053 normal_ACONST:
3054 #endif /* SUPPORT_CONST_STORE */
3055                                                 OP0_1(TYPE_ADR);
3056                                                 break;
3057
3058
3059                                                 /* pop 0 push 1 load */
3060
3061                                         case ICMD_ILOAD:
3062                                         case ICMD_LLOAD:
3063                                         case ICMD_FLOAD:
3064                                         case ICMD_DLOAD:
3065                                         case ICMD_ALOAD:
3066                                                 COUNT(count_load_instruction);
3067                                                 i = opcode - ICMD_ILOAD; /* type */
3068
3069                                                 j = iptr->s1.varindex = 
3070                                                         jd->local_map[iptr->s1.varindex * 5 + i];
3071
3072 #if defined(ENABLE_VERIFIER)
3073                                                 if (sd.var[j].type == TYPE_RET) {
3074                                                         exceptions_throw_verifyerror(m, "forbidden load of returnAddress");
3075                                                         return false;
3076                                                 }
3077 #endif
3078                 
3079 #if defined(ENABLE_SSA)
3080                                                 if (ls != NULL) {
3081                                                         GET_NEW_VAR(sd, new_index, i);
3082                                                         DST(i, new_index);
3083                                                         stackdepth++;
3084                                                 }
3085                                                 else
3086
3087 #else
3088                                                 LOAD(i, j);
3089 #endif
3090                                                 break;
3091
3092                                                 /* pop 2 push 1 */
3093
3094                                         case ICMD_LALOAD:
3095                                         case ICMD_FALOAD:
3096                                         case ICMD_DALOAD:
3097                                         case ICMD_AALOAD:
3098                                                 coalescing_boundary = sd.new;
3099                                                 iptr->flags.bits |= INS_FLAG_CHECK;
3100                                                 COUNT(count_check_null);
3101                                                 COUNT(count_check_bound);
3102                                                 COUNT(count_pcmd_mem);
3103                                                 OP2_1(TYPE_ADR, TYPE_INT, opcode - ICMD_IALOAD);
3104                                                 break;
3105
3106                                         case ICMD_IALOAD:
3107                                         case ICMD_BALOAD:
3108                                         case ICMD_CALOAD:
3109                                         case ICMD_SALOAD:
3110                                                 coalescing_boundary = sd.new;
3111                                                 iptr->flags.bits |= INS_FLAG_CHECK;
3112                                                 COUNT(count_check_null);
3113                                                 COUNT(count_check_bound);
3114                                                 COUNT(count_pcmd_mem);
3115                                                 OP2_1(TYPE_ADR, TYPE_INT, TYPE_INT);
3116                                                 break;
3117
3118                                                 /* pop 0 push 0 iinc */
3119
3120                                         case ICMD_IINC:
3121                                                 STATISTICS_STACKDEPTH_DISTRIBUTION(count_store_depth);
3122 #if defined(ENABLE_SSA)
3123                                                 if (ls != NULL) {
3124                                                         iptr->s1.varindex = 
3125                                                                 jd->local_map[iptr->s1.varindex * 5 +TYPE_INT];
3126                                                 }
3127                                                 else {
3128 #endif
3129                                                 last_store_boundary[iptr->s1.varindex] = sd.new;
3130
3131                                                 iptr->s1.varindex = 
3132                                                         jd->local_map[iptr->s1.varindex * 5 + TYPE_INT];
3133
3134                                                 copy = curstack;
3135                                                 i = stackdepth - 1;
3136                                                 while (copy) {
3137                                                         if ((copy->varkind == LOCALVAR) &&
3138                                                                 (copy->varnum == iptr->s1.varindex))
3139                                                         {
3140                                                                 assert(IS_LOCALVAR(copy));
3141                                                                 SET_TEMPVAR(copy);
3142                                                         }
3143                                                         i--;
3144                                                         copy = copy->prev;
3145                                                 }
3146 #if defined(ENABLE_SSA)
3147                                                 }
3148 #endif
3149
3150                                                 iptr->dst.varindex = iptr->s1.varindex;
3151                                                 break;
3152
3153                                                 /* pop 1 push 0 store */
3154
3155                                         case ICMD_ISTORE:
3156                                         case ICMD_LSTORE:
3157                                         case ICMD_FSTORE:
3158                                         case ICMD_DSTORE:
3159                                         case ICMD_ASTORE:
3160                                                 REQUIRE(1);
3161
3162                                                 i = opcode - ICMD_ISTORE; /* type */
3163                                                 javaindex = iptr->dst.varindex;
3164                                                 j = iptr->dst.varindex = 
3165                                                         jd->local_map[javaindex * 5 + i];
3166
3167                                                 COPY_VAL_AND_TYPE(sd, curstack->varnum, j);
3168
3169 #if defined(ENABLE_STATISTICS)
3170                                                 if (opt_stat) {
3171                                                         count_pcmd_store++;
3172                                                         i = sd.new - curstack;
3173                                                         if (i >= 20)
3174                                                                 count_store_length[20]++;
3175                                                         else
3176                                                                 count_store_length[i]++;
3177                                                         i = stackdepth - 1;
3178                                                         if (i >= 10)
3179                                                                 count_store_depth[10]++;
3180                                                         else
3181                                                                 count_store_depth[i]++;
3182                                                 }
3183 #endif
3184
3185 #if defined(ENABLE_SSA)
3186                                                 if (ls != NULL) {
3187 #endif
3188                                                 /* check for conflicts as described in Figure 5.2 */
3189
3190                                                 copy = curstack->prev;
3191                                                 i = stackdepth - 2;
3192                                                 while (copy) {
3193                                                         if ((copy->varkind == LOCALVAR) &&
3194                                                                 (copy->varnum == j))
3195                                                         {
3196                                                                 copy->varkind = TEMPVAR;
3197                                                                 assert(IS_LOCALVAR(copy));
3198                                                                 SET_TEMPVAR(copy);
3199                                                         }
3200                                                         i--;
3201                                                         copy = copy->prev;
3202                                                 }
3203
3204                                                 /* if the variable is already coalesced, don't bother */
3205
3206                                                 /* We do not need to check against INOUT, as invars */
3207                                                 /* are always before the coalescing boundary.        */
3208
3209                                                 if (curstack->varkind == LOCALVAR)
3210                                                         goto store_tail;
3211
3212                                                 /* there is no STORE Lj while curstack is live */
3213
3214                                                 if (curstack < last_store_boundary[javaindex])
3215                                                         goto assume_conflict;
3216
3217                                                 /* curstack must be after the coalescing boundary */
3218
3219                                                 if (curstack < coalescing_boundary)
3220                                                         goto assume_conflict;
3221
3222                                                 /* there is no DEF LOCALVAR(j) while curstack is live */
3223
3224                                                 copy = sd.new; /* most recent stackslot created + 1 */
3225                                                 while (--copy > curstack) {
3226                                                         if (copy->varkind == LOCALVAR && copy->varnum == j)
3227                                                                 goto assume_conflict;
3228                                                 }
3229
3230                                                 /* coalesce the temporary variable with Lj */
3231                                                 assert((curstack->varkind == TEMPVAR)
3232                                                                         || (curstack->varkind == UNDEFVAR));
3233                                                 assert(!IS_LOCALVAR(curstack)); /* XXX correct? */
3234                                                 assert(!IS_INOUT(curstack));
3235                                                 assert(!IS_PREALLOC(curstack));
3236
3237                                                 assert(curstack->creator);
3238                                                 assert(curstack->creator->dst.varindex == curstack->varnum);
3239                                                 assert(!(curstack->flags & PASSTHROUGH));
3240                                                 RELEASE_INDEX(sd, curstack);
3241                                                 curstack->varkind = LOCALVAR;
3242                                                 curstack->varnum = j;
3243                                                 curstack->creator->dst.varindex = j;
3244                                                 goto store_tail;
3245
3246                                                 /* revert the coalescing, if it has been done earlier */
3247 assume_conflict:
3248                                                 if ((curstack->varkind == LOCALVAR)
3249                                                         && (curstack->varnum == j))
3250                                                 {
3251                                                         assert(IS_LOCALVAR(curstack));
3252                                                         SET_TEMPVAR(curstack);
3253                                                 }
3254
3255                                                 /* remember the stack boundary at this store */
3256 store_tail:
3257                                                 last_store_boundary[javaindex] = sd.new;
3258 #if defined(ENABLE_SSA)
3259                                                 } /* if (ls != NULL) */
3260 #endif
3261
3262                                                 if (opcode == ICMD_ASTORE && curstack->type == TYPE_RET)
3263                                                         STORE(TYPE_RET, j);
3264                                                 else
3265                                                         STORE(opcode - ICMD_ISTORE, j);
3266                                                 break;
3267
3268                                         /* pop 3 push 0 */
3269
3270                                         case ICMD_AASTORE:
3271                                                 coalescing_boundary = sd.new;
3272                                                 iptr->flags.bits |= INS_FLAG_CHECK;
3273                                                 COUNT(count_check_null);
3274                                                 COUNT(count_check_bound);
3275                                                 COUNT(count_pcmd_mem);
3276
3277                                                 bte = builtintable_get_internal(BUILTIN_canstore);
3278                                                 md = bte->md;
3279
3280                                                 if (md->memuse > rd->memuse)
3281                                                         rd->memuse = md->memuse;
3282                                                 if (md->argintreguse > rd->argintreguse)
3283                                                         rd->argintreguse = md->argintreguse;
3284                                                 /* XXX non-leaf method? */
3285
3286                                                 /* make all stack variables saved */
3287
3288                                                 copy = curstack;
3289                                                 while (copy) {
3290                                                         sd.var[copy->varnum].flags |= SAVEDVAR;
3291                                                         /* in case copy->varnum is/will be a LOCALVAR */
3292                                                         /* once and set back to a non LOCALVAR        */
3293                                                         /* the correct SAVEDVAR flag has to be        */
3294                                                         /* remembered in copy->flags, too             */
3295                                                         copy->flags |= SAVEDVAR;
3296                                                         copy = copy->prev;
3297                                                 }
3298
3299                                                 OP3_0(TYPE_ADR, TYPE_INT, TYPE_ADR);
3300                                                 break;
3301
3302
3303                                         case ICMD_LASTORE:
3304                                         case ICMD_FASTORE:
3305                                         case ICMD_DASTORE:
3306                                                 coalescing_boundary = sd.new;
3307                                                 iptr->flags.bits |= INS_FLAG_CHECK;
3308                                                 COUNT(count_check_null);
3309                                                 COUNT(count_check_bound);
3310                                                 COUNT(count_pcmd_mem);
3311                                                 OP3_0(TYPE_ADR, TYPE_INT, opcode - ICMD_IASTORE);
3312                                                 break;
3313
3314                                         case ICMD_IASTORE:
3315                                         case ICMD_BASTORE:
3316                                         case ICMD_CASTORE:
3317                                         case ICMD_SASTORE:
3318                                                 coalescing_boundary = sd.new;
3319                                                 iptr->flags.bits |= INS_FLAG_CHECK;
3320                                                 COUNT(count_check_null);
3321                                                 COUNT(count_check_bound);
3322                                                 COUNT(count_pcmd_mem);
3323                                                 OP3_0(TYPE_ADR, TYPE_INT, TYPE_INT);
3324                                                 break;
3325
3326                                                 /* pop 1 push 0 */
3327
3328                                         case ICMD_POP:
3329 #ifdef ENABLE_VERIFIER
3330                                                 if (opt_verify) {
3331                                                         REQUIRE(1);
3332                                                         if (IS_2_WORD_TYPE(curstack->type))
3333                                                                 goto throw_stack_category_error;
3334                                                 }
3335 #endif
3336                                                 OP1_0_ANY;
3337                                                 break;
3338
3339                                         case ICMD_IRETURN:
3340                                         case ICMD_LRETURN:
3341                                         case ICMD_FRETURN:
3342                                         case ICMD_DRETURN:
3343                                         case ICMD_ARETURN:
3344                                                 coalescing_boundary = sd.new;
3345                                                 /* Assert here that no LOCAL or INOUTS get */
3346                                                 /* preallocated, since tha macros are not   */
3347                                                 /* available in md-abi.c! */
3348                                                 if (IS_TEMPVAR(curstack))
3349                                                         md_return_alloc(jd, curstack);
3350                                                 COUNT(count_pcmd_return);
3351                                                 OP1_0(opcode - ICMD_IRETURN);
3352                                                 superblockend = true;
3353                                                 break;
3354
3355                                         case ICMD_ATHROW:
3356                                                 coalescing_boundary = sd.new;
3357                                                 COUNT(count_check_null);
3358                                                 OP1_0(TYPE_ADR);
3359                                                 curstack = NULL; stackdepth = 0;
3360                                                 superblockend = true;
3361                                                 break;
3362
3363                                         case ICMD_PUTSTATIC:
3364                                                 coalescing_boundary = sd.new;
3365                                                 COUNT(count_pcmd_mem);
3366                                                 INSTRUCTION_GET_FIELDREF(iptr, fmiref);
3367                                                 OP1_0(fmiref->parseddesc.fd->type);
3368                                                 break;
3369
3370                                                 /* pop 1 push 0 branch */
3371
3372                                         case ICMD_IFNULL:
3373                                         case ICMD_IFNONNULL:
3374                                                 COUNT(count_pcmd_bra);
3375                                                 OP1_BRANCH(TYPE_ADR);
3376                                                 BRANCH(tbptr);
3377                                                 break;
3378
3379                                         case ICMD_IFEQ:
3380                                         case ICMD_IFNE:
3381                                         case ICMD_IFLT:
3382                                         case ICMD_IFGE:
3383                                         case ICMD_IFGT:
3384                                         case ICMD_IFLE:
3385                                                 COUNT(count_pcmd_bra);
3386                                                 /* iptr->sx.val.i is set implicitly in parse by
3387                                                    clearing the memory or from IF_ICMPxx
3388                                                    optimization. */
3389
3390                                                 OP1_BRANCH(TYPE_INT);
3391 /*                                              iptr->sx.val.i = 0; */
3392                                                 BRANCH(tbptr);
3393                                                 break;
3394
3395                                                 /* pop 0 push 0 branch */
3396
3397                                         case ICMD_GOTO:
3398                                                 COUNT(count_pcmd_bra);
3399                                                 OP0_BRANCH;
3400                                                 BRANCH(tbptr);
3401                                                 superblockend = true;
3402                                                 break;
3403
3404                                                 /* pop 1 push 0 table branch */
3405
3406                                         case ICMD_TABLESWITCH:
3407                                                 COUNT(count_pcmd_table);
3408                                                 OP1_BRANCH(TYPE_INT);
3409
3410                                                 table = iptr->dst.table;
3411                                                 BRANCH_TARGET(*table, tbptr);
3412                                                 table++;
3413
3414                                                 i = iptr->sx.s23.s3.tablehigh
3415                                                   - iptr->sx.s23.s2.tablelow + 1;
3416
3417                                                 while (--i >= 0) {
3418                                                         BRANCH_TARGET(*table, tbptr);
3419                                                         table++;
3420                                                 }
3421                                                 superblockend = true;
3422                                                 break;
3423
3424                                                 /* pop 1 push 0 table branch */
3425
3426                                         case ICMD_LOOKUPSWITCH:
3427                                                 COUNT(count_pcmd_table);
3428                                                 OP1_BRANCH(TYPE_INT);
3429
3430                                                 BRANCH_TARGET(iptr->sx.s23.s3.lookupdefault, tbptr);
3431
3432                                                 lookup = iptr->dst.lookup;
3433
3434                                                 i = iptr->sx.s23.s2.lookupcount;
3435
3436                                                 while (--i >= 0) {
3437                                                         BRANCH_TARGET(lookup->target, tbptr);
3438                                                         lookup++;
3439                                                 }
3440                                                 superblockend = true;
3441                                                 break;
3442
3443                                         case ICMD_MONITORENTER:
3444                                         case ICMD_MONITOREXIT:
3445                                                 coalescing_boundary = sd.new;
3446                                                 COUNT(count_check_null);
3447                                                 OP1_0(TYPE_ADR);
3448                                                 break;
3449
3450                                                 /* pop 2 push 0 branch */
3451
3452                                         case ICMD_IF_ICMPEQ:
3453                                         case ICMD_IF_ICMPNE:
3454                                         case ICMD_IF_ICMPLT:
3455                                         case ICMD_IF_ICMPGE:
3456                                         case ICMD_IF_ICMPGT:
3457                                         case ICMD_IF_ICMPLE:
3458                                                 COUNT(count_pcmd_bra);
3459                                                 OP2_BRANCH(TYPE_INT, TYPE_INT);
3460                                                 BRANCH(tbptr);
3461                                                 break;
3462
3463                                         case ICMD_IF_ACMPEQ:
3464                                         case ICMD_IF_ACMPNE:
3465                                                 COUNT(count_pcmd_bra);
3466                                                 OP2_BRANCH(TYPE_ADR, TYPE_ADR);
3467                                                 BRANCH(tbptr);
3468                                                 break;
3469
3470                                                 /* pop 2 push 0 */
3471
3472                                         case ICMD_PUTFIELD:
3473                                                 coalescing_boundary = sd.new;
3474                                                 COUNT(count_check_null);
3475                                                 COUNT(count_pcmd_mem);
3476                                                 INSTRUCTION_GET_FIELDREF(iptr, fmiref);
3477                                                 OP2_0(TYPE_ADR, fmiref->parseddesc.fd->type);
3478                                                 break;
3479
3480                                         case ICMD_POP2:
3481                                                 REQUIRE(1);
3482                                                 if (!IS_2_WORD_TYPE(curstack->type)) {
3483                                                         /* ..., cat1 */
3484 #ifdef ENABLE_VERIFIER
3485                                                         if (opt_verify) {
3486                                                                 REQUIRE(2);
3487                                                                 if (IS_2_WORD_TYPE(curstack->prev->type))
3488                                                                         goto throw_stack_category_error;
3489                                                         }
3490 #endif
3491                                                         OP2_0_ANY_ANY; /* pop two slots */
3492                                                 }
3493                                                 else {
3494                                                         iptr->opc = ICMD_POP;
3495                                                         OP1_0_ANY; /* pop one (two-word) slot */
3496                                                 }
3497                                                 break;
3498
3499                                                 /* pop 0 push 1 dup */
3500
3501                                         case ICMD_DUP:
3502 #ifdef ENABLE_VERIFIER
3503                                                 if (opt_verify) {
3504                                                         REQUIRE(1);
3505                                                         if (IS_2_WORD_TYPE(curstack->type))
3506                                                                 goto throw_stack_category_error;
3507                                                 }
3508 #endif
3509                                                 COUNT(count_dup_instruction);
3510
3511 icmd_DUP:
3512                                                 src1 = curstack;
3513
3514                                                 COPY_UP(src1);
3515                                                 coalescing_boundary = sd.new - 1;
3516                                                 break;
3517
3518                                         case ICMD_DUP2:
3519                                                 REQUIRE(1);
3520                                                 if (IS_2_WORD_TYPE(curstack->type)) {
3521                                                         /* ..., cat2 */
3522                                                         iptr->opc = ICMD_DUP;
3523                                                         goto icmd_DUP;
3524                                                 }
3525                                                 else {
3526                                                         REQUIRE(2);
3527                                                         /* ..., ????, cat1 */
3528 #ifdef ENABLE_VERIFIER
3529                                                         if (opt_verify) {
3530                                                                 if (IS_2_WORD_TYPE(curstack->prev->type))
3531                                                                         goto throw_stack_category_error;
3532                                                         }
3533 #endif
3534                                                         src1 = curstack->prev;
3535                                                         src2 = curstack;
3536
3537                                                         COPY_UP(src1); iptr++; len--;
3538                                                         COPY_UP(src2);
3539
3540                                                         coalescing_boundary = sd.new;
3541                                                 }
3542                                                 break;
3543
3544                                                 /* pop 2 push 3 dup */
3545
3546                                         case ICMD_DUP_X1:
3547 #ifdef ENABLE_VERIFIER
3548                                                 if (opt_verify) {
3549                                                         REQUIRE(2);
3550                                                         if (IS_2_WORD_TYPE(curstack->type) ||
3551                                                                 IS_2_WORD_TYPE(curstack->prev->type))
3552                                                                         goto throw_stack_category_error;
3553                                                 }
3554 #endif
3555
3556 icmd_DUP_X1:
3557                                                 src1 = curstack->prev;
3558                                                 src2 = curstack;
3559                                                 POPANY; POPANY;
3560                                                 stackdepth -= 2;
3561
3562                                                 /* move non-temporary sources out of the way */
3563                                                 if (!IS_TEMPVAR(src2)) {
3564                                                         MOVE_TO_TEMP(src2); iptr++; len--;
3565                                                 }
3566
3567                                                 DUP_SLOT(src2); dst1 = curstack; stackdepth++;
3568
3569                                                 MOVE_UP(src1); iptr++; len--;
3570                                                 MOVE_UP(src2); iptr++; len--;
3571
3572                                                 COPY_DOWN(curstack, dst1);
3573
3574                                                 coalescing_boundary = sd.new;
3575                                                 break;
3576
3577                                         case ICMD_DUP2_X1:
3578                                                 REQUIRE(2);
3579                                                 if (IS_2_WORD_TYPE(curstack->type)) {
3580                                                         /* ..., ????, cat2 */
3581 #ifdef ENABLE_VERIFIER
3582                                                         if (opt_verify) {
3583                                                                 if (IS_2_WORD_TYPE(curstack->prev->type))
3584                                                                         goto throw_stack_category_error;
3585                                                         }
3586 #endif
3587                                                         iptr->opc = ICMD_DUP_X1;
3588                                                         goto icmd_DUP_X1;
3589                                                 }
3590                                                 else {
3591                                                         /* ..., ????, cat1 */
3592 #ifdef ENABLE_VERIFIER
3593                                                         if (opt_verify) {
3594                                                                 REQUIRE(3);
3595                                                                 if (IS_2_WORD_TYPE(curstack->prev->type)
3596                                                                         || IS_2_WORD_TYPE(curstack->prev->prev->type))
3597                                                                                 goto throw_stack_category_error;
3598                                                         }
3599 #endif
3600
3601 icmd_DUP2_X1:
3602                                                         src1 = curstack->prev->prev;
3603                                                         src2 = curstack->prev;
3604                                                         src3 = curstack;
3605                                                         POPANY; POPANY; POPANY;
3606                                                         stackdepth -= 3;
3607
3608                                                         /* move non-temporary sources out of the way */
3609                                                         if (!IS_TEMPVAR(src2)) {
3610                                                                 MOVE_TO_TEMP(src2); iptr++; len--;
3611                                                         }
3612                                                         if (!IS_TEMPVAR(src3)) {
3613                                                                 MOVE_TO_TEMP(src3); iptr++; len--;
3614                                                         }
3615
3616                                                         DUP_SLOT(src2); dst1 = curstack; stackdepth++;
3617                                                         DUP_SLOT(src3); dst2 = curstack; stackdepth++;
3618
3619                                                         MOVE_UP(src1); iptr++; len--;
3620                                                         MOVE_UP(src2); iptr++; len--;
3621                                                         MOVE_UP(src3); iptr++; len--;
3622
3623                                                         COPY_DOWN(curstack, dst2); iptr++; len--;
3624                                                         COPY_DOWN(curstack->prev, dst1);
3625
3626                                                         coalescing_boundary = sd.new;
3627                                                 }
3628                                                 break;
3629
3630                                                 /* pop 3 push 4 dup */
3631
3632                                         case ICMD_DUP_X2:
3633                                                 REQUIRE(2);
3634                                                 if (IS_2_WORD_TYPE(curstack->prev->type)) {
3635                                                         /* ..., cat2, ???? */
3636 #ifdef ENABLE_VERIFIER
3637                                                         if (opt_verify) {
3638                                                                 if (IS_2_WORD_TYPE(curstack->type))
3639                                                                         goto throw_stack_category_error;
3640                                                         }
3641 #endif
3642                                                         iptr->opc = ICMD_DUP_X1;
3643                                                         goto icmd_DUP_X1;
3644                                                 }
3645                                                 else {
3646                                                         /* ..., cat1, ???? */
3647 #ifdef ENABLE_VERIFIER
3648                                                         if (opt_verify) {
3649                                                                 REQUIRE(3);
3650                                                                 if (IS_2_WORD_TYPE(curstack->type)
3651                                                                         || IS_2_WORD_TYPE(curstack->prev->prev->type))
3652                                                                                         goto throw_stack_category_error;
3653                                                         }
3654 #endif
3655 icmd_DUP_X2:
3656                                                         src1 = curstack->prev->prev;
3657                                                         src2 = curstack->prev;
3658                                                         src3 = curstack;
3659                                                         POPANY; POPANY; POPANY;
3660                                                         stackdepth -= 3;
3661
3662                                                         /* move non-temporary sources out of the way */
3663                                                         if (!IS_TEMPVAR(src2)) {
3664                                                                 MOVE_TO_TEMP(src2); iptr++; len--;
3665                                                         }
3666                                                         if (!IS_TEMPVAR(src3)) {
3667                                                                 MOVE_TO_TEMP(src3); iptr++; len--;
3668                                                         }
3669
3670                                                         DUP_SLOT(src3); dst1 = curstack; stackdepth++;
3671
3672                                                         MOVE_UP(src1); iptr++; len--;
3673                                                         MOVE_UP(src2); iptr++; len--;
3674                                                         MOVE_UP(src3); iptr++; len--;
3675
3676                                                         COPY_DOWN(curstack, dst1);
3677
3678                                                         coalescing_boundary = sd.new;
3679                                                 }
3680                                                 break;
3681
3682                                         case ICMD_DUP2_X2:
3683                                                 REQUIRE(2);
3684                                                 if (IS_2_WORD_TYPE(curstack->type)) {
3685                                                         /* ..., ????, cat2 */
3686                                                         if (IS_2_WORD_TYPE(curstack->prev->type)) {
3687                                                                 /* ..., cat2, cat2 */
3688                                                                 iptr->opc = ICMD_DUP_X1;
3689                                                                 goto icmd_DUP_X1;
3690                                                         }
3691                                                         else {
3692                                                                 /* ..., cat1, cat2 */
3693 #ifdef ENABLE_VERIFIER
3694                                                                 if (opt_verify) {
3695                                                                         REQUIRE(3);
3696                                                                         if (IS_2_WORD_TYPE(curstack->prev->prev->type))
3697                                                                                         goto throw_stack_category_error;
3698                                                                 }
3699 #endif
3700                                                                 iptr->opc = ICMD_DUP_X2;
3701                                                                 goto icmd_DUP_X2;
3702                                                         }
3703                                                 }
3704
3705                                                 REQUIRE(3);
3706                                                 /* ..., ????, ????, cat1 */
3707
3708                                                 if (IS_2_WORD_TYPE(curstack->prev->prev->type)) {
3709                                                         /* ..., cat2, ????, cat1 */
3710 #ifdef ENABLE_VERIFIER
3711                                                         if (opt_verify) {
3712                                                                 if (IS_2_WORD_TYPE(curstack->prev->type))
3713                                                                         goto throw_stack_category_error;
3714                                                         }
3715 #endif
3716                                                         iptr->opc = ICMD_DUP2_X1;
3717                                                         goto icmd_DUP2_X1;
3718                                                 }
3719                                                 else {
3720                                                         /* ..., cat1, ????, cat1 */
3721 #ifdef ENABLE_VERIFIER
3722                                                         if (opt_verify) {
3723                                                                 REQUIRE(4);
3724                                                                 if (IS_2_WORD_TYPE(curstack->prev->type)
3725                                                                         || IS_2_WORD_TYPE(curstack->prev->prev->prev->type))
3726                                                                         goto throw_stack_category_error;
3727                                                         }
3728 #endif
3729
3730                                                         src1 = curstack->prev->prev->prev;
3731                                                         src2 = curstack->prev->prev;
3732                                                         src3 = curstack->prev;
3733                                                         src4 = curstack;
3734                                                         POPANY; POPANY; POPANY; POPANY;
3735                                                         stackdepth -= 4;
3736
3737                                                         /* move non-temporary sources out of the way */
3738                                                         if (!IS_TEMPVAR(src2)) {
3739                                                                 MOVE_TO_TEMP(src2); iptr++; len--;
3740                                                         }
3741                                                         if (!IS_TEMPVAR(src3)) {
3742                                                                 MOVE_TO_TEMP(src3); iptr++; len--;
3743                                                         }
3744                                                         if (!IS_TEMPVAR(src4)) {
3745                                                                 MOVE_TO_TEMP(src4); iptr++; len--;
3746                                                         }
3747
3748                                                         DUP_SLOT(src3); dst1 = curstack; stackdepth++;
3749                                                         DUP_SLOT(src4); dst2 = curstack; stackdepth++;
3750
3751                                                         MOVE_UP(src1); iptr++; len--;
3752                                                         MOVE_UP(src2); iptr++; len--;
3753                                                         MOVE_UP(src3); iptr++; len--;
3754                                                         MOVE_UP(src4); iptr++; len--;
3755
3756                                                         COPY_DOWN(curstack, dst2); iptr++; len--;
3757                                                         COPY_DOWN(curstack->prev, dst1);
3758
3759                                                         coalescing_boundary = sd.new;
3760                                                 }
3761                                                 break;
3762
3763                                                 /* pop 2 push 2 swap */
3764
3765                                         case ICMD_SWAP:
3766 #ifdef ENABLE_VERIFIER
3767                                                 if (opt_verify) {
3768                                                         REQUIRE(2);
3769                                                         if (IS_2_WORD_TYPE(curstack->type)
3770                                                                 || IS_2_WORD_TYPE(curstack->prev->type))
3771                                                                 goto throw_stack_category_error;
3772                                                 }
3773 #endif
3774
3775                                                 src1 = curstack->prev;
3776                                                 src2 = curstack;
3777                                                 POPANY; POPANY;
3778                                                 stackdepth -= 2;
3779
3780                                                 /* move non-temporary sources out of the way */
3781                                                 if (!IS_TEMPVAR(src1)) {
3782                                                         MOVE_TO_TEMP(src1); iptr++; len--;
3783                                                 }
3784
3785                                                 MOVE_UP(src2); iptr++; len--;
3786                                                 MOVE_UP(src1);
3787
3788                                                 coalescing_boundary = sd.new;
3789                                                 break;
3790
3791                                                 /* pop 2 push 1 */
3792
3793                                         case ICMD_IDIV:
3794                                         case ICMD_IREM:
3795                                                 coalescing_boundary = sd.new;
3796 #if !SUPPORT_DIVISION
3797                                                 bte = iptr->sx.s23.s3.bte;
3798                                                 md = bte->md;
3799
3800                                                 if (md->memuse > rd->memuse)
3801                                                         rd->memuse = md->memuse;
3802                                                 if (md->argintreguse > rd->argintreguse)
3803                                                         rd->argintreguse = md->argintreguse;
3804
3805                                                 /* make all stack variables saved */
3806
3807                                                 copy = curstack;
3808                                                 while (copy) {
3809                                                         sd.var[copy->varnum].flags |= SAVEDVAR;
3810                                                         copy->flags |= SAVEDVAR;
3811                                                         copy = copy->prev;
3812                                                 }
3813                                                 /* FALLTHROUGH */
3814
3815 #endif /* !SUPPORT_DIVISION */
3816
3817                                         case ICMD_ISHL:
3818                                         case ICMD_ISHR:
3819                                         case ICMD_IUSHR:
3820                                         case ICMD_IADD:
3821                                         case ICMD_ISUB:
3822                                         case ICMD_IMUL:
3823                                         case ICMD_IAND:
3824                                         case ICMD_IOR:
3825                                         case ICMD_IXOR:
3826                                                 COUNT(count_pcmd_op);
3827                                                 OP2_1(TYPE_INT, TYPE_INT, TYPE_INT);
3828                                                 break;
3829
3830                                         case ICMD_LDIV:
3831                                         case ICMD_LREM:
3832                                                 coalescing_boundary = sd.new;
3833 #if !(SUPPORT_DIVISION && SUPPORT_LONG && SUPPORT_LONG_DIV)
3834                                                 bte = iptr->sx.s23.s3.bte;
3835                                                 md = bte->md;
3836
3837                                                 if (md->memuse > rd->memuse)
3838                                                         rd->memuse = md->memuse;
3839                                                 if (md->argintreguse > rd->argintreguse)
3840                                                         rd->argintreguse = md->argintreguse;
3841                                                 /* XXX non-leaf method? */
3842
3843                                                 /* make all stack variables saved */
3844
3845                                                 copy = curstack;
3846                                                 while (copy) {
3847                                                         sd.var[copy->varnum].flags |= SAVEDVAR;
3848                                                         copy->flags |= SAVEDVAR;
3849                                                         copy = copy->prev;
3850                                                 }
3851                                                 /* FALLTHROUGH */
3852
3853 #endif /* !(SUPPORT_DIVISION && SUPPORT_LONG && SUPPORT_LONG_DIV) */
3854
3855                                         case ICMD_LMUL:
3856                                         case ICMD_LADD:
3857                                         case ICMD_LSUB:
3858 #if SUPPORT_LONG_LOGICAL
3859                                         case ICMD_LAND:
3860                                         case ICMD_LOR:
3861                                         case ICMD_LXOR:
3862 #endif /* SUPPORT_LONG_LOGICAL */
3863                                                 COUNT(count_pcmd_op);
3864                                                 OP2_1(TYPE_LNG, TYPE_LNG, TYPE_LNG);
3865                                                 break;
3866
3867                                         case ICMD_LSHL:
3868                                         case ICMD_LSHR:
3869                                         case ICMD_LUSHR:
3870                                                 COUNT(count_pcmd_op);
3871                                                 OP2_1(TYPE_LNG, TYPE_INT, TYPE_LNG);
3872                                                 break;
3873
3874                                         case ICMD_FADD:
3875                                         case ICMD_FSUB:
3876                                         case ICMD_FMUL:
3877                                         case ICMD_FDIV:
3878                                         case ICMD_FREM:
3879                                                 COUNT(count_pcmd_op);
3880                                                 OP2_1(TYPE_FLT, TYPE_FLT, TYPE_FLT);
3881                                                 break;
3882
3883                                         case ICMD_DADD:
3884                                         case ICMD_DSUB:
3885                                         case ICMD_DMUL:
3886                                         case ICMD_DDIV:
3887                                         case ICMD_DREM:
3888                                                 COUNT(count_pcmd_op);
3889                                                 OP2_1(TYPE_DBL, TYPE_DBL, TYPE_DBL);
3890                                                 break;
3891
3892                                         case ICMD_LCMP:
3893                                                 COUNT(count_pcmd_op);
3894 #if SUPPORT_LONG_CMP_CONST
3895                                                 if ((len == 0) || (iptr[1].sx.val.i != 0))
3896                                                         goto normal_LCMP;
3897
3898                                                 switch (iptr[1].opc) {
3899                                                 case ICMD_IFEQ:
3900                                                         iptr->opc = ICMD_IF_LCMPEQ;
3901                                                 icmd_lcmp_if_tail:
3902                                                         iptr->dst.insindex = iptr[1].dst.insindex;
3903                                                         iptr[1].opc = ICMD_NOP;
3904
3905                                                         OP2_BRANCH(TYPE_LNG, TYPE_LNG);
3906                                                         BRANCH(tbptr);
3907
3908                                                         COUNT(count_pcmd_bra);
3909                                                         break;
3910                                                 case ICMD_IFNE:
3911                                                         iptr->opc = ICMD_IF_LCMPNE;
3912                                                         goto icmd_lcmp_if_tail;
3913                                                 case ICMD_IFLT:
3914                                                         iptr->opc = ICMD_IF_LCMPLT;
3915                                                         goto icmd_lcmp_if_tail;
3916                                                 case ICMD_IFGT:
3917                                                         iptr->opc = ICMD_IF_LCMPGT;
3918                                                         goto icmd_lcmp_if_tail;
3919                                                 case ICMD_IFLE:
3920                                                         iptr->opc = ICMD_IF_LCMPLE;
3921                                                         goto icmd_lcmp_if_tail;
3922                                                 case ICMD_IFGE:
3923                                                         iptr->opc = ICMD_IF_LCMPGE;
3924                                                         goto icmd_lcmp_if_tail;
3925                                                 default:
3926                                                         goto normal_LCMP;
3927                                                 }
3928                                                 break;
3929 normal_LCMP:
3930 #endif /* SUPPORT_LONG_CMP_CONST */
3931                                                         OP2_1(TYPE_LNG, TYPE_LNG, TYPE_INT);
3932                                                 break;
3933
3934                                                 /* XXX why is this deactivated? */
3935 #if 0
3936                                         case ICMD_FCMPL:
3937                                                 COUNT(count_pcmd_op);
3938                                                 if ((len == 0) || (iptr[1].sx.val.i != 0))
3939                                                         goto normal_FCMPL;
3940
3941                                                 switch (iptr[1].opc) {
3942                                                 case ICMD_IFEQ:
3943                                                         iptr->opc = ICMD_IF_FCMPEQ;
3944                                                 icmd_if_fcmpl_tail:
3945                                                         iptr->dst.insindex = iptr[1].dst.insindex;
3946                                                         iptr[1].opc = ICMD_NOP;
3947
3948                                                         OP2_BRANCH(TYPE_FLT, TYPE_FLT);
3949                                                         BRANCH(tbptr);
3950
3951                                                         COUNT(count_pcmd_bra);
3952                                                         break;
3953                                                 case ICMD_IFNE:
3954                                                         iptr->opc = ICMD_IF_FCMPNE;
3955                                                         goto icmd_if_fcmpl_tail;
3956                                                 case ICMD_IFLT:
3957                                                         iptr->opc = ICMD_IF_FCMPL_LT;
3958                                                         goto icmd_if_fcmpl_tail;
3959                                                 case ICMD_IFGT:
3960                                                         iptr->opc = ICMD_IF_FCMPL_GT;
3961                                                         goto icmd_if_fcmpl_tail;
3962                                                 case ICMD_IFLE:
3963                                                         iptr->opc = ICMD_IF_FCMPL_LE;
3964                                                         goto icmd_if_fcmpl_tail;
3965                                                 case ICMD_IFGE:
3966                                                         iptr->opc = ICMD_IF_FCMPL_GE;
3967                                                         goto icmd_if_fcmpl_tail;
3968                                                 default:
3969                                                         goto normal_FCMPL;
3970                                                 }
3971                                                 break;
3972
3973 normal_FCMPL:
3974                                                 OPTT2_1(TYPE_FLT, TYPE_FLT, TYPE_INT);
3975                                                 break;
3976
3977                                         case ICMD_FCMPG:
3978                                                 COUNT(count_pcmd_op);
3979                                                 if ((len == 0) || (iptr[1].sx.val.i != 0))
3980                                                         goto normal_FCMPG;
3981
3982                                                 switch (iptr[1].opc) {
3983                                                 case ICMD_IFEQ:
3984                                                         iptr->opc = ICMD_IF_FCMPEQ;
3985                                                 icmd_if_fcmpg_tail:
3986                                                         iptr->dst.insindex = iptr[1].dst.insindex;
3987                                                         iptr[1].opc = ICMD_NOP;
3988
3989                                                         OP2_BRANCH(TYPE_FLT, TYPE_FLT);
3990                                                         BRANCH(tbptr);
3991
3992                                                         COUNT(count_pcmd_bra);
3993                                                         break;
3994                                                 case ICMD_IFNE:
3995                                                         iptr->opc = ICMD_IF_FCMPNE;
3996                                                         goto icmd_if_fcmpg_tail;
3997                                                 case ICMD_IFLT:
3998                                                         iptr->opc = ICMD_IF_FCMPG_LT;
3999                                                         goto icmd_if_fcmpg_tail;
4000                                                 case ICMD_IFGT:
4001                                                         iptr->opc = ICMD_IF_FCMPG_GT;
4002                                                         goto icmd_if_fcmpg_tail;
4003                                                 case ICMD_IFLE:
4004                                                         iptr->opc = ICMD_IF_FCMPG_LE;
4005                                                         goto icmd_if_fcmpg_tail;
4006                                                 case ICMD_IFGE:
4007                                                         iptr->opc = ICMD_IF_FCMPG_GE;
4008                                                         goto icmd_if_fcmpg_tail;
4009                                                 default:
4010                                                         goto normal_FCMPG;
4011                                                 }
4012                                                 break;
4013
4014 normal_FCMPG:
4015                                                 OP2_1(TYPE_FLT, TYPE_FLT, TYPE_INT);
4016                                                 break;
4017
4018                                         case ICMD_DCMPL:
4019                                                 COUNT(count_pcmd_op);
4020                                                 if ((len == 0) || (iptr[1].sx.val.i != 0))
4021                                                         goto normal_DCMPL;
4022
4023                                                 switch (iptr[1].opc) {
4024                                                 case ICMD_IFEQ:
4025                                                         iptr->opc = ICMD_IF_DCMPEQ;
4026                                                 icmd_if_dcmpl_tail:
4027                                                         iptr->dst.insindex = iptr[1].dst.insindex;
4028                                                         iptr[1].opc = ICMD_NOP;
4029
4030                                                         OP2_BRANCH(TYPE_DBL, TYPE_DBL);
4031                                                         BRANCH(tbptr);
4032
4033                                                         COUNT(count_pcmd_bra);
4034                                                         break;
4035                                                 case ICMD_IFNE:
4036                                                         iptr->opc = ICMD_IF_DCMPNE;
4037                                                         goto icmd_if_dcmpl_tail;
4038                                                 case ICMD_IFLT:
4039                                                         iptr->opc = ICMD_IF_DCMPL_LT;
4040                                                         goto icmd_if_dcmpl_tail;
4041                                                 case ICMD_IFGT:
4042                                                         iptr->opc = ICMD_IF_DCMPL_GT;
4043                                                         goto icmd_if_dcmpl_tail;
4044                                                 case ICMD_IFLE:
4045                                                         iptr->opc = ICMD_IF_DCMPL_LE;
4046                                                         goto icmd_if_dcmpl_tail;
4047                                                 case ICMD_IFGE:
4048                                                         iptr->opc = ICMD_IF_DCMPL_GE;
4049                                                         goto icmd_if_dcmpl_tail;
4050                                                 default:
4051                                                         goto normal_DCMPL;
4052                                                 }
4053                                                 break;
4054
4055 normal_DCMPL:
4056                                                 OPTT2_1(TYPE_DBL, TYPE_INT);
4057                                                 break;
4058
4059                                         case ICMD_DCMPG:
4060                                                 COUNT(count_pcmd_op);
4061                                                 if ((len == 0) || (iptr[1].sx.val.i != 0))
4062                                                         goto normal_DCMPG;
4063
4064                                                 switch (iptr[1].opc) {
4065                                                 case ICMD_IFEQ:
4066                                                         iptr->opc = ICMD_IF_DCMPEQ;
4067                                                 icmd_if_dcmpg_tail:
4068                                                         iptr->dst.insindex = iptr[1].dst.insindex;
4069                                                         iptr[1].opc = ICMD_NOP;
4070
4071                                                         OP2_BRANCH(TYPE_DBL, TYPE_DBL);
4072                                                         BRANCH(tbptr);
4073
4074                                                         COUNT(count_pcmd_bra);
4075                                                         break;
4076                                                 case ICMD_IFNE:
4077                                                         iptr->opc = ICMD_IF_DCMPNE;
4078                                                         goto icmd_if_dcmpg_tail;
4079                                                 case ICMD_IFLT:
4080                                                         iptr->opc = ICMD_IF_DCMPG_LT;
4081                                                         goto icmd_if_dcmpg_tail;
4082                                                 case ICMD_IFGT:
4083                                                         iptr->opc = ICMD_IF_DCMPG_GT;
4084                                                         goto icmd_if_dcmpg_tail;
4085                                                 case ICMD_IFLE:
4086                                                         iptr->opc = ICMD_IF_DCMPG_LE;
4087                                                         goto icmd_if_dcmpg_tail;
4088                                                 case ICMD_IFGE:
4089                                                         iptr->opc = ICMD_IF_DCMPG_GE;
4090                                                         goto icmd_if_dcmpg_tail;
4091                                                 default:
4092                                                         goto normal_DCMPG;
4093                                                 }
4094                                                 break;
4095
4096 normal_DCMPG:
4097                                                 OP2_1(TYPE_DBL, TYPE_DBL, TYPE_INT);
4098                                                 break;
4099 #else
4100                                         case ICMD_FCMPL:
4101                                         case ICMD_FCMPG:
4102                                                 COUNT(count_pcmd_op);
4103                                                 OP2_1(TYPE_FLT, TYPE_FLT, TYPE_INT);
4104                                                 break;
4105
4106                                         case ICMD_DCMPL:
4107                                         case ICMD_DCMPG:
4108                                                 COUNT(count_pcmd_op);
4109                                                 OP2_1(TYPE_DBL, TYPE_DBL, TYPE_INT);
4110                                                 break;
4111 #endif
4112
4113                                                 /* pop 1 push 1 */
4114
4115                                         case ICMD_INEG:
4116                                         case ICMD_INT2BYTE:
4117                                         case ICMD_INT2CHAR:
4118                                         case ICMD_INT2SHORT:
4119                                                 COUNT(count_pcmd_op);
4120                                                 OP1_1(TYPE_INT, TYPE_INT);
4121                                                 break;
4122                                         case ICMD_LNEG:
4123                                                 COUNT(count_pcmd_op);
4124                                                 OP1_1(TYPE_LNG, TYPE_LNG);
4125                                                 break;
4126                                         case ICMD_FNEG:
4127                                                 COUNT(count_pcmd_op);
4128                                                 OP1_1(TYPE_FLT, TYPE_FLT);
4129                                                 break;
4130                                         case ICMD_DNEG:
4131                                                 COUNT(count_pcmd_op);
4132                                                 OP1_1(TYPE_DBL, TYPE_DBL);
4133                                                 break;
4134
4135                                         case ICMD_I2L:
4136                                                 COUNT(count_pcmd_op);
4137                                                 OP1_1(TYPE_INT, TYPE_LNG);
4138                                                 break;
4139                                         case ICMD_I2F:
4140                                                 COUNT(count_pcmd_op);
4141                                                 OP1_1(TYPE_INT, TYPE_FLT);
4142                                                 break;
4143                                         case ICMD_I2D:
4144                                                 COUNT(count_pcmd_op);
4145                                                 OP1_1(TYPE_INT, TYPE_DBL);
4146                                                 break;
4147                                         case ICMD_L2I:
4148                                                 COUNT(count_pcmd_op);
4149                                                 OP1_1(TYPE_LNG, TYPE_INT);
4150                                                 break;
4151                                         case ICMD_L2F:
4152                                                 COUNT(count_pcmd_op);
4153                                                 OP1_1(TYPE_LNG, TYPE_FLT);
4154                                                 break;
4155                                         case ICMD_L2D:
4156                                                 COUNT(count_pcmd_op);
4157                                                 OP1_1(TYPE_LNG, TYPE_DBL);
4158                                                 break;
4159                                         case ICMD_F2I:
4160                                                 COUNT(count_pcmd_op);
4161                                                 OP1_1(TYPE_FLT, TYPE_INT);
4162                                                 break;
4163                                         case ICMD_F2L:
4164                                                 COUNT(count_pcmd_op);
4165                                                 OP1_1(TYPE_FLT, TYPE_LNG);
4166                                                 break;
4167                                         case ICMD_F2D:
4168                                                 COUNT(count_pcmd_op);
4169                                                 OP1_1(TYPE_FLT, TYPE_DBL);
4170                                                 break;
4171                                         case ICMD_D2I:
4172                                                 COUNT(count_pcmd_op);
4173                                                 OP1_1(TYPE_DBL, TYPE_INT);
4174                                                 break;
4175                                         case ICMD_D2L:
4176                                                 COUNT(count_pcmd_op);
4177                                                 OP1_1(TYPE_DBL, TYPE_LNG);
4178                                                 break;
4179                                         case ICMD_D2F:
4180                                                 COUNT(count_pcmd_op);
4181                                                 OP1_1(TYPE_DBL, TYPE_FLT);
4182                                                 break;
4183
4184                                         case ICMD_CHECKCAST:
4185                                                 coalescing_boundary = sd.new;
4186                                                 if (iptr->flags.bits & INS_FLAG_ARRAY) {
4187                                                         /* array type cast-check */
4188
4189                                                         bte = builtintable_get_internal(BUILTIN_arraycheckcast);
4190                                                         md = bte->md;
4191
4192                                                         if (md->memuse > rd->memuse)
4193                                                                 rd->memuse = md->memuse;
4194                                                         if (md->argintreguse > rd->argintreguse)
4195                                                                 rd->argintreguse = md->argintreguse;
4196
4197                                                         /* make all stack variables saved */
4198
4199                                                         copy = curstack;
4200                                                         while (copy) {
4201                                                                 sd.var[copy->varnum].flags |= SAVEDVAR;
4202                                                                 copy->flags |= SAVEDVAR;
4203                                                                 copy = copy->prev;
4204                                                         }
4205                                                 }
4206                                                 OP1_1(TYPE_ADR, TYPE_ADR);
4207                                                 break;
4208
4209                                         case ICMD_INSTANCEOF:
4210                                         case ICMD_ARRAYLENGTH:
4211                                                 coalescing_boundary = sd.new;
4212                                                 OP1_1(TYPE_ADR, TYPE_INT);
4213                                                 break;
4214
4215                                         case ICMD_NEWARRAY:
4216                                         case ICMD_ANEWARRAY:
4217                                                 coalescing_boundary = sd.new;
4218                                                 OP1_1(TYPE_INT, TYPE_ADR);
4219                                                 break;
4220
4221                                         case ICMD_GETFIELD:
4222                                                 coalescing_boundary = sd.new;
4223                                                 COUNT(count_check_null);
4224                                                 COUNT(count_pcmd_mem);
4225                                                 INSTRUCTION_GET_FIELDREF(iptr, fmiref);
4226                                                 OP1_1(TYPE_ADR, fmiref->parseddesc.fd->type);
4227                                                 break;
4228
4229                                                 /* pop 0 push 1 */
4230
4231                                         case ICMD_GETSTATIC:
4232                                                 coalescing_boundary = sd.new;
4233                                                 COUNT(count_pcmd_mem);
4234                                                 INSTRUCTION_GET_FIELDREF(iptr, fmiref);
4235                                                 OP0_1(fmiref->parseddesc.fd->type);
4236                                                 break;
4237
4238                                         case ICMD_NEW:
4239                                                 coalescing_boundary = sd.new;
4240                                                 OP0_1(TYPE_ADR);
4241                                                 break;
4242
4243                                         case ICMD_JSR:
4244                                                 OP0_1(TYPE_RET);
4245
4246                                                 tbptr = BLOCK_OF(iptr->sx.s23.s3.jsrtarget.insindex);
4247                                                 tbptr->type = BBTYPE_SBR;
4248
4249                                                 assert(sd.bptr->next);  /* XXX exception */
4250                                                 sd.var[curstack->varnum].vv.retaddr = sd.bptr->next;
4251 #if defined(ENABLE_VERIFIER)
4252                                                 sd.var[curstack->varnum].SBRSTART = (void*) tbptr;
4253 #endif
4254
4255                                                 tbptr = stack_mark_reached(&sd, tbptr, curstack, stackdepth);
4256                                                 if (!tbptr)
4257                                                         return false;
4258
4259                                                 iptr->sx.s23.s3.jsrtarget.block = tbptr;
4260
4261                                                 /* We need to check for overflow right here because
4262                                                  * the pushed value is poped afterwards */
4263                                                 CHECKOVERFLOW;
4264
4265                                                 superblockend = true;
4266                                                 /* XXX should not be marked as interface, as it does not need to be */
4267                                                 /* allocated. Same for the invar of the target. */
4268                                                 break;
4269
4270                                         /* pop many push any */
4271
4272                                         case ICMD_BUILTIN:
4273 icmd_BUILTIN:
4274                                                 bte = iptr->sx.s23.s3.bte;
4275                                                 md = bte->md;
4276                                                 goto _callhandling;
4277
4278                                         case ICMD_INVOKESTATIC:
4279                                         case ICMD_INVOKESPECIAL:
4280                                         case ICMD_INVOKEVIRTUAL:
4281                                         case ICMD_INVOKEINTERFACE:
4282                                                 COUNT(count_pcmd_met);
4283
4284                                                 /* Check for functions to replace with builtin
4285                                                  * functions. */
4286
4287                                                 if (builtintable_replace_function(iptr))
4288                                                         goto icmd_BUILTIN;
4289
4290                                                 INSTRUCTION_GET_METHODDESC(iptr, md);
4291                                                 /* XXX resurrect this COUNT? */
4292 /*                          if (lm->flags & ACC_STATIC) */
4293 /*                              {COUNT(count_check_null);} */
4294
4295                                         _callhandling:
4296
4297                                                 coalescing_boundary = sd.new;
4298
4299                                                 i = md->paramcount;
4300
4301                                                 if (md->memuse > rd->memuse)
4302                                                         rd->memuse = md->memuse;
4303                                                 if (md->argintreguse > rd->argintreguse)
4304                                                         rd->argintreguse = md->argintreguse;
4305                                                 if (md->argfltreguse > rd->argfltreguse)
4306                                                         rd->argfltreguse = md->argfltreguse;
4307
4308                                                 REQUIRE(i);
4309
4310                                                 iptr->s1.argcount = stackdepth;
4311                                                 iptr->sx.s23.s2.args = DMNEW(s4, stackdepth);
4312
4313                                                 copy = curstack;
4314                                                 for (i-- ; i >= 0; i--) {
4315                                                         iptr->sx.s23.s2.args[i] = copy->varnum;
4316
4317                                                         /* do not change STACKVARs or LOCALVARS to ARGVAR*/
4318                                                         /* ->  won't help anyway */
4319                                                         if (!(IS_INOUT(copy) || IS_LOCALVAR(copy))) {
4320
4321 #if defined(SUPPORT_PASS_FLOATARGS_IN_INTREGS)
4322                         /* If we pass float arguments in integer argument registers, we
4323                          * are not allowed to precolor them here. Floats have to be moved
4324                          * to this regs explicitly in codegen().
4325                          * Only arguments that are passed by stack anyway can be precolored
4326                          * (michi 2005/07/24) */
4327                                                         if (!(sd.var[copy->varnum].flags & SAVEDVAR) &&
4328                                                            (!IS_FLT_DBL_TYPE(copy->type) 
4329                                                                 || md->params[i].inmemory)) {
4330 #else
4331                                                         if (!(sd.var[copy->varnum].flags & SAVEDVAR)) {
4332 #endif
4333
4334                                                                 SET_PREALLOC(copy);
4335
4336                                                                 if (md->params[i].inmemory) {
4337                                                                         sd.var[copy->varnum].vv.regoff =
4338                                                                                 md->params[i].regoff;
4339                                                                         sd.var[copy->varnum].flags |= 
4340                                                                                 INMEMORY;
4341                                                                 }
4342                                                                 else {
4343                                                                         if (IS_FLT_DBL_TYPE(copy->type)) {
4344 #if defined(SUPPORT_PASS_FLOATARGS_IN_INTREGS)
4345                                                                                 assert(0); /* XXX is this assert ok? */
4346 #else
4347                                                                                 sd.var[copy->varnum].vv.regoff = 
4348                                                                         rd->argfltregs[md->params[i].regoff];
4349 #endif /* SUPPORT_PASS_FLOATARGS_IN_INTREGS */
4350                                                                         }
4351                                                                         else {
4352 #if defined(SUPPORT_COMBINE_INTEGER_REGISTERS)
4353                                                                                 if (IS_2_WORD_TYPE(copy->type))
4354                                                                                         sd.var[copy->varnum].vv.regoff = 
4355                         PACK_REGS( rd->argintregs[GET_LOW_REG(md->params[i].regoff)],
4356                                            rd->argintregs[GET_HIGH_REG(md->params[i].regoff)]);
4357
4358                                                                                 else
4359 #endif /* SUPPORT_COMBINE_INTEGER_REGISTERS */
4360                                                                                         sd.var[copy->varnum].vv.regoff = 
4361                                                                         rd->argintregs[md->params[i].regoff];
4362                                                                         }
4363                                                                 }
4364                                                         }
4365                                                         }
4366                                                         copy = copy->prev;
4367                                                 }
4368
4369                                                 /* deal with live-through stack slots "under" the */
4370                                                 /* arguments */
4371
4372                                                 i = md->paramcount;
4373
4374                                                 while (copy) {
4375                                                         iptr->sx.s23.s2.args[i++] = copy->varnum;
4376                                                         sd.var[copy->varnum].flags |= SAVEDVAR;
4377                                                         copy->flags |= SAVEDVAR | PASSTHROUGH;
4378                                                         copy = copy->prev;
4379                                                 }
4380
4381                                                 /* pop the arguments */
4382
4383                                                 i = md->paramcount;
4384
4385                                                 stackdepth -= i;
4386                                                 while (--i >= 0) {
4387                                                         POPANY;
4388                                                 }
4389
4390                                                 /* push the return value */
4391
4392                                                 if (md->returntype.type != TYPE_VOID) {
4393                                                         GET_NEW_VAR(sd, new_index, md->returntype.type);
4394                                                         DST(md->returntype.type, new_index);
4395                                                         stackdepth++;
4396                                                 }
4397                                                 break;
4398
4399                                         case ICMD_INLINE_START:
4400                                         case ICMD_INLINE_END:
4401                                                 CLR_S1;
4402                                                 CLR_DST;
4403                                                 break;
4404
4405                                         case ICMD_MULTIANEWARRAY:
4406                                                 coalescing_boundary = sd.new;
4407                                                 if (rd->argintreguse < MIN(3, INT_ARG_CNT))
4408                                                         rd->argintreguse = MIN(3, INT_ARG_CNT);
4409
4410                                                 i = iptr->s1.argcount;
4411
4412                                                 REQUIRE(i);
4413
4414                                                 iptr->sx.s23.s2.args = DMNEW(s4, i);
4415
4416 #if defined(SPECIALMEMUSE)
4417 # if defined(__DARWIN__)
4418                                                 if (rd->memuse < (i + INT_ARG_CNT +LA_SIZE_IN_POINTERS))
4419                                                         rd->memuse = i + LA_SIZE_IN_POINTERS + INT_ARG_CNT;
4420 # else
4421                                                 if (rd->memuse < (i + LA_SIZE_IN_POINTERS + 3))
4422                                                         rd->memuse = i + LA_SIZE_IN_POINTERS + 3;
4423 # endif
4424 #else
4425 # if defined(__I386__)
4426                                                 if (rd->memuse < i + 3)
4427                                                         rd->memuse = i + 3; /* n integer args spilled on stack */
4428 # elif defined(__MIPS__) && SIZEOF_VOID_P == 4
4429                                                 if (rd->memuse < i + 2)
4430                                                         rd->memuse = i + 2; /* 4*4 bytes callee save space */
4431 # else
4432                                                 if (rd->memuse < i)
4433                                                         rd->memuse = i; /* n integer args spilled on stack */
4434 # endif /* defined(__I386__) */
4435 #endif
4436                                                 copy = curstack;
4437                                                 while (--i >= 0) {
4438                                         /* check INT type here? Currently typecheck does this. */
4439                                                         iptr->sx.s23.s2.args[i] = copy->varnum;
4440                                                         if (!(sd.var[copy->varnum].flags & SAVEDVAR)
4441                                                                 && (!IS_INOUT(copy))
4442                                                                 && (!IS_LOCALVAR(copy)) ) {
4443                                                                 copy->varkind = ARGVAR;
4444                                                                 sd.var[copy->varnum].flags |=
4445                                                                         INMEMORY & PREALLOC;
4446 #if defined(SPECIALMEMUSE)
4447 # if defined(__DARWIN__)
4448                                                                 sd.var[copy->varnum].vv.regoff = i + 
4449                                                                         LA_SIZE_IN_POINTERS + INT_ARG_CNT;
4450 # else
4451                                                                 sd.var[copy->varnum].vv.regoff = i + 
4452                                                                         LA_SIZE_IN_POINTERS + 3;
4453 # endif
4454 #else
4455 # if defined(__I386__)
4456                                                                 sd.var[copy->varnum].vv.regoff = i + 3;
4457 # elif defined(__MIPS__) && SIZEOF_VOID_P == 4
4458                                                                 sd.var[copy->varnum].vv.regoff = i + 2;
4459 # else
4460                                                                 sd.var[copy->varnum].vv.regoff = i;
4461 # endif /* defined(__I386__) */
4462 #endif /* defined(SPECIALMEMUSE) */
4463                                                         }
4464                                                         copy = copy->prev;
4465                                                 }
4466                                                 while (copy) {
4467                                                         sd.var[copy->varnum].flags |= SAVEDVAR;
4468                                                         copy->flags |= SAVEDVAR;
4469                                                         copy = copy->prev;
4470                                                 }
4471
4472                                                 i = iptr->s1.argcount;
4473                                                 stackdepth -= i;
4474                                                 while (--i >= 0) {
4475                                                         POPANY;
4476                                                 }
4477                                                 GET_NEW_VAR(sd, new_index, TYPE_ADR);
4478                                                 DST(TYPE_ADR, new_index);
4479                                                 stackdepth++;
4480                                                 break;
4481
4482                                         default:
4483                                                 *exceptionptr =
4484                                                         new_internalerror("Unknown ICMD %d", opcode);
4485                                                 return false;
4486                                         } /* switch */
4487
4488                                         CHECKOVERFLOW;
4489                                         iptr++;
4490                                 } /* while instructions */
4491
4492                                 /* stack slots at basic block end become interfaces */
4493
4494                                 sd.bptr->outdepth = stackdepth;
4495                                 sd.bptr->outvars = DMNEW(s4, stackdepth);
4496
4497                                 i = stackdepth - 1;
4498                                 for (copy = curstack; copy; i--, copy = copy->prev) {
4499                                         varinfo *v;
4500                                         s4 t;
4501
4502                                         /* with the new vars rd->interfaces will be removed */
4503                                         /* and all in and outvars have to be STACKVARS!     */
4504                                         /* in the moment i.e. SWAP with in and out vars can */
4505                                         /* create an unresolvable conflict */
4506
4507                                         SET_TEMPVAR(copy);
4508                                         t = copy->type;
4509                                         if (t == TYPE_RET)
4510                                                 t = TYPE_ADR;
4511
4512                                         v = sd.var + copy->varnum;
4513                                         v->flags |= INOUT;
4514
4515                                         if (jd->interface_map[i*5 + t].flags == UNUSED) {
4516                                                 /* no interface var until now for this depth and */
4517                                                 /* type */
4518                                                 jd->interface_map[i*5 + t].flags = v->flags;
4519                                         }
4520                                         else {
4521                                                 jd->interface_map[i*5 + t].flags |= v->flags;
4522                                         }
4523
4524                                         sd.bptr->outvars[i] = copy->varnum;
4525                                 }
4526
4527                                 /* check if interface slots at basic block begin must be saved */
4528
4529                                 for (i=0; i<sd.bptr->indepth; ++i) {
4530                                         varinfo *v = sd.var + sd.bptr->invars[i];
4531                                         s4 t;
4532
4533                                         t = v->type;
4534                                         if (t == TYPE_RET)
4535                                                 t = TYPE_ADR;
4536
4537                                         if (jd->interface_map[i*5 + t].flags == UNUSED) {
4538                                                 /* no interface var until now for this depth and */
4539                                                 /* type */
4540                                                 jd->interface_map[i*5 + t].flags = v->flags;
4541                                         }
4542                                         else {
4543                                                 jd->interface_map[i*5 + t].flags |= v->flags;
4544                                         }
4545                                 }
4546
4547                                 /* store the number of this block's variables */
4548
4549                                 sd.bptr->varcount = sd.vartop - sd.bptr->varstart;
4550
4551 #if defined(STACK_VERBOSE)
4552                                 stack_verbose_block_exit(&sd, superblockend);
4553 #endif
4554
4555                                 /* reach the following block, if any */
4556
4557                                 if (!superblockend)
4558                                         if (!stack_reach_next_block(&sd))
4559                                                 return false;
4560
4561                 } /* for blocks */
4562
4563         } while (sd.repeat && !deadcode);
4564
4565         /* XXX reset TYPE_RET to TYPE_ADR */
4566
4567         for (i=0; i<sd.vartop; ++i) {
4568                 if (sd.var[i].type == TYPE_RET)
4569                         sd.var[i].type = TYPE_ADR;
4570         }
4571
4572         /* XXX hack to fix up the ranges of the cloned single-block handlers */
4573
4574         ex = jd->exceptiontable;
4575         for (; ex != NULL; ex = ex->down) {
4576                 if (ex->start == ex->end) {
4577                         assert(ex->end->next);
4578                         ex->end = ex->end->next;
4579                 }
4580         }
4581
4582         /* store number of created variables */
4583
4584         jd->vartop = sd.vartop;
4585
4586         /* gather statistics *****************************************************/
4587
4588 #if defined(ENABLE_STATISTICS)
4589         if (opt_stat) {
4590                 if (jd->basicblockcount > count_max_basic_blocks)
4591                         count_max_basic_blocks = jd->basicblockcount;
4592                 count_basic_blocks += jd->basicblockcount;
4593                 if (jd->instructioncount > count_max_javainstr)
4594                         count_max_javainstr = jd->instructioncount;
4595                 count_javainstr += jd->instructioncount;
4596                 if (jd->stackcount > count_upper_bound_new_stack)
4597                         count_upper_bound_new_stack = jd->stackcount;
4598                 if ((sd.new - jd->stack) > count_max_new_stack)
4599                         count_max_new_stack = (sd.new - jd->stack);
4600
4601                 sd.bptr = jd->basicblocks;
4602                 for (; sd.bptr; sd.bptr = sd.bptr->next) {
4603                         if (sd.bptr->flags > BBREACHED) {
4604                                 if (sd.bptr->indepth >= 10)
4605                                         count_block_stack[10]++;
4606                                 else
4607                                         count_block_stack[sd.bptr->indepth]++;
4608                                 len = sd.bptr->icount;
4609                                 if (len < 10)
4610                                         count_block_size_distribution[len]++;
4611                                 else if (len <= 12)
4612                                         count_block_size_distribution[10]++;
4613                                 else if (len <= 14)
4614                                         count_block_size_distribution[11]++;
4615                                 else if (len <= 16)
4616                                         count_block_size_distribution[12]++;
4617                                 else if (len <= 18)
4618                                         count_block_size_distribution[13]++;
4619                                 else if (len <= 20)
4620                                         count_block_size_distribution[14]++;
4621                                 else if (len <= 25)
4622                                         count_block_size_distribution[15]++;
4623                                 else if (len <= 30)
4624                                         count_block_size_distribution[16]++;
4625                                 else
4626                                         count_block_size_distribution[17]++;
4627                         }
4628                 }
4629
4630                 if (iteration_count == 1)
4631                         count_analyse_iterations[0]++;
4632                 else if (iteration_count == 2)
4633                         count_analyse_iterations[1]++;
4634                 else if (iteration_count == 3)
4635                         count_analyse_iterations[2]++;
4636                 else if (iteration_count == 4)
4637                         count_analyse_iterations[3]++;
4638                 else
4639                         count_analyse_iterations[4]++;
4640
4641                 if (jd->basicblockcount <= 5)
4642                         count_method_bb_distribution[0]++;
4643                 else if (jd->basicblockcount <= 10)
4644                         count_method_bb_distribution[1]++;
4645                 else if (jd->basicblockcount <= 15)
4646                         count_method_bb_distribution[2]++;
4647                 else if (jd->basicblockcount <= 20)
4648                         count_method_bb_distribution[3]++;
4649                 else if (jd->basicblockcount <= 30)
4650                         count_method_bb_distribution[4]++;
4651                 else if (jd->basicblockcount <= 40)
4652                         count_method_bb_distribution[5]++;
4653                 else if (jd->basicblockcount <= 50)
4654                         count_method_bb_distribution[6]++;
4655                 else if (jd->basicblockcount <= 75)
4656                         count_method_bb_distribution[7]++;
4657                 else
4658                         count_method_bb_distribution[8]++;
4659         }
4660 #endif /* defined(ENABLE_STATISTICS) */
4661
4662         /* everything's ok *******************************************************/
4663
4664         return true;
4665
4666         /* goto labels for throwing verifier exceptions **************************/
4667
4668 #if defined(ENABLE_VERIFIER)
4669
4670 throw_stack_underflow:
4671         exceptions_throw_verifyerror(m, "Unable to pop operand off an empty stack");
4672         return false;
4673
4674 throw_stack_overflow:
4675         exceptions_throw_verifyerror(m, "Stack size too large");
4676         return false;
4677
4678 throw_stack_type_error:
4679         exceptions_throw_verifyerror_for_stack(m, expectedtype);
4680         return false;
4681
4682 throw_stack_category_error:
4683         exceptions_throw_verifyerror(m, "Attempt to split long or double on the stack");
4684         return false;
4685
4686 #endif
4687 }
4688
4689
4690 /* functions for verbose stack analysis output ********************************/
4691
4692 #if defined(STACK_VERBOSE)
4693 static void stack_verbose_show_varinfo(stackdata_t *sd, varinfo *v)
4694 {
4695         printf("%c", show_jit_type_letters[v->type]);
4696         if (v->type == TYPE_RET)
4697                 printf("{L%03d}", v->vv.retaddr->nr);
4698 }
4699
4700
4701 static void stack_verbose_show_variable(stackdata_t *sd, s4 index)
4702 {
4703         assert(index >= 0 && index < sd->vartop);
4704         stack_verbose_show_varinfo(sd, sd->var + index);
4705 }
4706
4707
4708 static void stack_verbose_show_block(stackdata_t *sd, basicblock *bptr)
4709 {
4710         s4 i;
4711
4712         printf("L%03d type:%d in:%d [", bptr->nr, bptr->type, bptr->indepth);
4713         if (bptr->invars) {
4714                 for (i=0; i<bptr->indepth; ++i) {
4715                         if (i)
4716                                 putchar(' ');
4717                         stack_verbose_show_variable(sd, bptr->invars[i]);
4718                 }
4719         }
4720         else
4721                 putchar('-');
4722         printf("] inlocals [");
4723         if (bptr->inlocals) {
4724                 for (i=0; i<sd->localcount; ++i) {
4725                         if (i)
4726                                 putchar(' ');
4727                         stack_verbose_show_varinfo(sd, bptr->inlocals + i);
4728                 }
4729         }
4730         else
4731                 putchar('-');
4732         printf("] out:%d [", bptr->outdepth);
4733         if (bptr->outvars) {
4734                 for (i=0; i<bptr->outdepth; ++i) {
4735                         if (i)
4736                                 putchar(' ');
4737                         stack_verbose_show_variable(sd, bptr->outvars[i]);
4738                 }
4739         }
4740         else
4741                 putchar('-');
4742         printf("]");
4743
4744         if (bptr->original)
4745                 printf(" (clone of L%03d)", bptr->original->nr);
4746         else {
4747                 basicblock *b = bptr->copied_to;
4748                 if (b) {
4749                         printf(" (copied to ");
4750                         for (; b; b = b->copied_to)
4751                                 printf("L%03d ", b->nr);
4752                         printf(")");
4753                 }
4754         }
4755 }
4756
4757
4758 static void stack_verbose_block_enter(stackdata_t *sd, bool reanalyse)
4759 {
4760         int i;
4761
4762         printf("======================================== STACK %sANALYSE BLOCK ", 
4763                         (reanalyse) ? "RE-" : "");
4764         stack_verbose_show_block(sd, sd->bptr);
4765         printf("\n");
4766
4767         if (sd->handlers[0]) {
4768                 printf("HANDLERS: ");
4769                 for (i=0; sd->handlers[i]; ++i) {
4770                         printf("L%03d ", sd->handlers[i]->handler->nr);
4771                 }
4772                 printf("\n");
4773         }
4774         printf("\n");
4775 }
4776
4777
4778 static void stack_verbose_block_exit(stackdata_t *sd, bool superblockend)
4779 {
4780         printf("%s ", (superblockend) ? "SUPERBLOCKEND" : "END");
4781         stack_verbose_show_block(sd, sd->bptr);
4782         printf("\n");
4783 }
4784 #endif
4785
4786
4787 /*
4788  * These are local overrides for various environment variables in Emacs.
4789  * Please do not remove this and leave it at the end of the file, where
4790  * Emacs will automagically detect them.
4791  * ---------------------------------------------------------------------
4792  * Local variables:
4793  * mode: c
4794  * indent-tabs-mode: t
4795  * c-basic-offset: 4
4796  * tab-width: 4
4797  * End:
4798  * vim:noexpandtab:sw=4:ts=4:
4799  */