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