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