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