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