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