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