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