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