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