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