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