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