* src/vm/jit/stack.h (REQUIRE_1, REQUIRE_2, REQUIRE_3, REQUIRE_4):
[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 5441 2006-09-09 14:41:03Z edwin $
34
35 */
36
37
38 #include "config.h"
39
40 #include <assert.h>
41 #include <stdio.h>
42 #include <string.h>
43
44 #include "vm/types.h"
45
46 #include "arch.h"
47 #include "md-abi.h"
48
49 #include "mm/memory.h"
50 #include "native/native.h"
51 #include "toolbox/logging.h"
52 #include "vm/global.h"
53 #include "vm/builtin.h"
54 #include "vm/options.h"
55 #include "vm/resolve.h"
56 #include "vm/statistics.h"
57 #include "vm/stringlocal.h"
58 #include "vm/jit/cfg.h"
59 #include "vm/jit/codegen-common.h"
60 #include "vm/jit/abi.h"
61 #include "vm/jit/show.h"
62
63 #if defined(ENABLE_DISASSEMBLER)
64 # include "vm/jit/disass.h"
65 #endif
66
67 #include "vm/jit/jit.h"
68 #include "vm/jit/stack.h"
69
70 #if defined(ENABLE_LSRA)
71 # include "vm/jit/allocator/lsra.h"
72 #endif
73
74 /*#define STACK_VERBOSE*/
75
76
77 /* macro for saving #ifdefs ***************************************************/
78
79 #if defined(ENABLE_INTRP)
80 #define IF_INTRP(x) if (opt_intrp) { x }
81 #define IF_NO_INTRP(x) if (!opt_intrp) { x }
82 #else
83 #define IF_INTRP(x)
84 #define IF_NO_INTRP(x) { x }
85 #endif
86
87 #if defined(ENABLE_INTRP)
88 #if defined(ENABLE_JIT)
89 #define IF_JIT(x) if (!opt_intrp) { x }
90 #else
91 #define IF_JIT(x)
92 #endif
93 #else /* !defined(ENABLE_INTRP) */
94 #define IF_JIT(x) { x }
95 #endif /* defined(ENABLE_INTRP) */
96
97 #if defined(ENABLE_STATISTICS)
98 #define STATISTICS_STACKDEPTH_DISTRIBUTION(distr)                    \
99     do {                                                             \
100         if (opt_stat) {                                              \
101             if (stackdepth >= 10)                                    \
102                 count_store_depth[10]++;                             \
103             else                                                     \
104                 count_store_depth[stackdepth]++;                     \
105         }                                                            \
106     } while (0)
107 #else /* !defined(ENABLE_STATISTICS) */
108 #define STATISTICS_STACKDEPTH_DISTRIBUTION(distr)
109 #endif
110
111 /* stackdata_t ****************************************************************/
112
113 typedef struct stackdata_t stackdata_t;
114
115 struct stackdata_t {
116     basicblock *bptr;
117     stackptr new;
118     s4 vartop;
119     s4 localcount;
120     s4 varcount;
121     varinfo *var;
122 };
123
124
125 /* macros for allocating/releasing variable indices */
126
127 #define GET_NEW_INDEX(sd, new_varindex)                              \
128     do {                                                             \
129         assert((sd).vartop < (sd).varcount);                         \
130         (new_varindex) = ((sd).vartop)++;                            \
131     } while (0)
132
133 /* not implemented now, can be used to reuse varindices          */
134 /* pay attention to not release a localvar once implementing it! */
135 #define RELEASE_INDEX(sd, varindex)
136
137 #define GET_NEW_VAR(sd, new_varindex, newtype)                       \
138     do {                                                             \
139         GET_NEW_INDEX((sd), (new_varindex));                         \
140         (sd).var[new_index].type = (newtype);                        \
141     } while (0)
142
143 /* macros for querying variable properties **************************/
144
145 #define IS_OUTVAR(sp)                                                \
146     (sd.var[(sp)->varnum].flags & OUTVAR)
147
148 #define IS_PREALLOC(sp)                                              \
149     (sd.var[(sp)->varnum].flags & PREALLOC)
150
151 #define IS_TEMPVAR(sp)                                               \
152     ( ((sp)->varnum >= sd.localcount)                                \
153       && !(sd.var[(sp)->varnum].flags & (OUTVAR | PREALLOC)) )
154
155 #define IS_LOCALVAR_SD(sd, sp)                                       \
156          ((sp)->varnum < (sd).localcount)
157
158 #define IS_LOCALVAR(sp)                                              \
159     IS_LOCALVAR_SD(sd, (sp))
160
161
162 /* macros for setting variable properties ****************************/
163
164 #define SET_TEMPVAR(sp)                                              \
165     do {                                                             \
166         if (IS_LOCALVAR((sp))) {                                     \
167             GET_NEW_VAR(sd, new_index, (sp)->type);                  \
168             sd.var[new_index].flags = (sp)->flags;                   \
169             (sp)->varnum = new_index;                                \
170                         (sp)->varkind = TEMPVAR;                                 \
171                         if ((sp)->creator)                                       \
172                                 (sp)->creator->dst.varindex = new_index;             \
173         }                                                            \
174         sd.var[(sp)->varnum].flags &= ~(OUTVAR | PREALLOC);          \
175     } while (0);
176
177 #define SET_PREALLOC(sp)                                             \
178     do {                                                             \
179         assert(!IS_LOCALVAR((sp)));                                  \
180         sd.var[(sp)->varnum].flags |= PREALLOC;                      \
181     } while (0);
182
183 /* macros for source operands ***************************************/
184
185 #define CLR_S1                                                       \
186     (iptr->s1.varindex = -1)
187
188 #define USE_S1_LOCAL(type1)
189
190 #define USE_S1(type1)                                                \
191     do {                                                             \
192         REQUIRE(1);                                                   \
193         CHECK_BASIC_TYPE(type1, curstack->type);                     \
194         iptr->s1.varindex = curstack->varnum;                        \
195     } while (0)
196
197 #define USE_S1_ANY                                                   \
198     do {                                                             \
199         REQUIRE(1);                                                   \
200         iptr->s1.varindex = curstack->varnum;                        \
201     } while (0)
202
203 #define USE_S1_S2(type1, type2)                                      \
204     do {                                                             \
205         REQUIRE(2);                                                   \
206         CHECK_BASIC_TYPE(type1, curstack->prev->type);               \
207         CHECK_BASIC_TYPE(type2, curstack->type);                     \
208         iptr->sx.s23.s2.varindex = curstack->varnum;                 \
209         iptr->s1.varindex = curstack->prev->varnum;                  \
210     } while (0)
211
212 #define USE_S1_S2_ANY_ANY                                            \
213     do {                                                             \
214         REQUIRE(2);                                                   \
215         iptr->sx.s23.s2.varindex = curstack->varnum;                 \
216         iptr->s1.varindex = curstack->prev->varnum;                  \
217     } while (0)
218
219 #define USE_S1_S2_S3(type1, type2, type3)                            \
220     do {                                                             \
221         REQUIRE(3);                                                   \
222         CHECK_BASIC_TYPE(type1, curstack->prev->prev->type);         \
223         CHECK_BASIC_TYPE(type2, curstack->prev->type);               \
224         CHECK_BASIC_TYPE(type3, curstack->type);                     \
225         iptr->sx.s23.s3.varindex = curstack->varnum;                 \
226         iptr->sx.s23.s2.varindex = curstack->prev->varnum;           \
227         iptr->s1.varindex = curstack->prev->prev->varnum;            \
228     } while (0)
229
230 /* The POPANY macro does NOT check stackdepth, or set stackdepth!   */
231 #define POPANY                                                       \
232     do {                                                             \
233         if (curstack->varkind == UNDEFVAR)                           \
234             curstack->varkind = TEMPVAR;                             \
235         curstack = curstack->prev;                                   \
236     } while (0)
237
238 #define POP_S1(type1)                                                \
239     do {                                                             \
240         USE_S1(type1);                                               \
241         if (curstack->varkind == UNDEFVAR)                           \
242             curstack->varkind = TEMPVAR;                             \
243         curstack = curstack->prev;                                   \
244     } while (0)
245
246 #define POP_S1_ANY                                                   \
247     do {                                                             \
248         USE_S1_ANY;                                                  \
249         if (curstack->varkind == UNDEFVAR)                           \
250             curstack->varkind = TEMPVAR;                             \
251         curstack = curstack->prev;                                   \
252     } while (0)
253
254 #define POP_S1_S2(type1, type2)                                      \
255     do {                                                             \
256         USE_S1_S2(type1, type2);                                     \
257         if (curstack->varkind == UNDEFVAR)                           \
258             curstack->varkind = TEMPVAR;                             \
259         if (curstack->prev->varkind == UNDEFVAR)                     \
260             curstack->prev->varkind = TEMPVAR;                       \
261         curstack = curstack->prev->prev;                             \
262     } while (0)
263
264 #define POP_S1_S2_ANY_ANY                                            \
265     do {                                                             \
266         USE_S1_S2_ANY_ANY;                                           \
267         if (curstack->varkind == UNDEFVAR)                           \
268             curstack->varkind = TEMPVAR;                             \
269         if (curstack->prev->varkind == UNDEFVAR)                     \
270             curstack->prev->varkind = TEMPVAR;                       \
271         curstack = curstack->prev->prev;                             \
272     } while (0)
273
274 #define POP_S1_S2_S3(type1, type2, type3)                            \
275     do {                                                             \
276         USE_S1_S2_S3(type1, type2, type3);                           \
277         if (curstack->varkind == UNDEFVAR)                           \
278             curstack->varkind = TEMPVAR;                             \
279         if (curstack->prev->varkind == UNDEFVAR)                     \
280             curstack->prev->varkind = TEMPVAR;                       \
281         if (curstack->prev->prev->varkind == UNDEFVAR)               \
282             curstack->prev->prev->varkind = TEMPVAR;                 \
283         curstack = curstack->prev->prev->prev;                       \
284     } while (0)
285
286 #define CLR_SX                                                       \
287     (iptr->sx.val.l = 0)
288
289
290 /* macros for setting the destination operand ***********************/
291
292 #define CLR_DST                                                      \
293     (iptr->dst.varindex = -1)
294
295 #define DST(typed, index)                                            \
296     do {                                                             \
297         NEWSTACKn((typed),(index));                                  \
298         curstack->creator = iptr;                                    \
299         iptr->dst.varindex = (index);                                \
300     } while (0)
301
302 #define DST_LOCALVAR(typed, index)                                   \
303     do {                                                             \
304         NEWSTACK((typed), LOCALVAR, (index));                        \
305         curstack->creator = iptr;                                    \
306         iptr->dst.varindex = (index);                                \
307     } while (0)
308
309
310 /* stack modelling macros *******************************************/
311
312 #define OP0_1(typed)                                                 \
313     do {                                                             \
314         CLR_S1;                                                      \
315         GET_NEW_VAR(sd, new_index, (typed));                         \
316         DST(typed, new_index);                                       \
317         stackdepth++;                                                \
318     } while (0)
319
320 #define OP1_0_ANY                                                    \
321     do {                                                             \
322         POP_S1_ANY;                                                  \
323         CLR_DST;                                                     \
324         stackdepth--;                                                \
325     } while (0)
326
327 #define OP1_BRANCH(type1)                                            \
328     do {                                                             \
329         POP_S1(type1);                                               \
330         stackdepth--;                                                \
331     } while (0)
332
333 #define OP1_1(type1, typed)                                          \
334     do {                                                             \
335         POP_S1(type1);                                               \
336         GET_NEW_VAR(sd, new_index, (typed));                         \
337         DST(typed, new_index);                                       \
338     } while (0)
339
340 #define OP2_1(type1, type2, typed)                                   \
341     do {                                                             \
342         POP_S1_S2(type1, type2);                                     \
343         GET_NEW_VAR(sd, new_index, (typed));                         \
344         DST(typed, new_index);                                       \
345         stackdepth--;                                                \
346     } while (0)
347
348 #define OP0_0                                                        \
349     do {                                                             \
350         CLR_S1;                                                      \
351         CLR_DST;                                                     \
352     } while (0)
353
354 #define OP0_BRANCH                                                   \
355     do {                                                             \
356         CLR_S1;                                                      \
357     } while (0)
358
359 #define OP1_0(type1)                                                 \
360     do {                                                             \
361         POP_S1(type1);                                               \
362         CLR_DST;                                                     \
363         stackdepth--;                                                \
364     } while (0)
365
366 #define OP2_0(type1, type2)                                          \
367     do {                                                             \
368         POP_S1_S2(type1, type2);                                     \
369         CLR_DST;                                                     \
370         stackdepth -= 2;                                             \
371     } while (0)
372
373 #define OP2_BRANCH(type1, type2)                                     \
374     do {                                                             \
375         POP_S1_S2(type1, type2);                                     \
376         stackdepth -= 2;                                             \
377     } while (0)
378
379 #define OP2_0_ANY_ANY                                                \
380     do {                                                             \
381         POP_S1_S2_ANY_ANY;                                           \
382         CLR_DST;                                                     \
383         stackdepth -= 2;                                             \
384     } while (0)
385
386 #define OP3_0(type1, type2, type3)                                   \
387     do {                                                             \
388         POP_S1_S2_S3(type1, type2, type3);                           \
389         CLR_DST;                                                     \
390         stackdepth -= 3;                                             \
391     } while (0)
392
393 #define LOAD(type1, index)                                           \
394     do {                                                             \
395         DST_LOCALVAR(type1, index);                                  \
396         stackdepth++;                                                \
397     } while (0)
398
399 #define STORE(type1, index)                                          \
400     do {                                                             \
401         POP_S1(type1);                                               \
402         stackdepth--;                                                \
403     } while (0)
404
405
406 /* macros for DUP elimination ***************************************/
407
408 /* XXX turn off coalescing */
409 #if 0
410 #define DUP_SLOT(sp)                                                 \
411     do {                                                             \
412         if ((sp)->varkind != TEMPVAR) {                              \
413             GET_NEW_VAR(sd, new_index, (sp)->type);                  \
414             NEWSTACK((sp)->type, TEMPVAR, new_index);                \
415         }                                                            \
416         else                                                         \
417             NEWSTACK((sp)->type, (sp)->varkind, (sp)->varnum);       \
418     } while(0)
419 #else
420 #define DUP_SLOT(sp)                                                 \
421     do {                                                             \
422             GET_NEW_VAR(sd, new_index, (sp)->type);                  \
423             NEWSTACK((sp)->type, TEMPVAR, new_index);                \
424     } while(0)
425 #endif
426
427 /* does not check input stackdepth */
428 #define MOVE_UP(sp)                                                  \
429     do {                                                             \
430         iptr->opc = ICMD_MOVE;                                       \
431         iptr->s1.varindex = (sp)->varnum;                            \
432         DUP_SLOT(sp);                                                \
433         curstack->creator = iptr;                                    \
434         iptr->dst.varindex = curstack->varnum;                       \
435         stackdepth++;                                                \
436     } while (0)
437
438 /* does not check input stackdepth */
439 #define COPY_UP(sp)                                                  \
440     do {                                                             \
441         SET_TEMPVAR((sp));                                           \
442         iptr->opc = ICMD_COPY;                                       \
443         iptr->s1.varindex = (sp)->varnum;                            \
444         DUP_SLOT(sp);                                                \
445         curstack->creator = iptr;                                    \
446         iptr->dst.varindex = curstack->varnum;                       \
447         stackdepth++;                                                \
448     } while (0)
449
450 #define COPY_DOWN(s, d)                                              \
451     do {                                                             \
452         SET_TEMPVAR((s));                                            \
453         iptr->opc = ICMD_COPY;                                       \
454         iptr->s1.varindex = (s)->varnum;                             \
455         iptr->dst.varindex = (d)->varnum;                            \
456         (d)->creator = iptr;                                         \
457     } while (0)
458
459
460 /* macros for branching / reaching basic blocks *********************/
461
462 #if defined(ENABLE_VERIFIER)
463 #define MARKREACHED(b, c)                                            \
464     do {                                                             \
465         if (!stack_mark_reached(&sd, (b), curstack, stackdepth))     \
466             return false;                                            \
467     } while (0)
468 #else
469 #define MARKREACHED(b, c)                                            \
470     do {                                                             \
471         (void) stack_mark_reached(&sd, (b), curstack, stackdepth);   \
472     } while (0)
473 #endif
474
475 #define BRANCH_TARGET(bt, tempbptr, tempsp)                          \
476     do {                                                             \
477         (bt).block = tempbptr = BLOCK_OF((bt).insindex);             \
478         MARKREACHED(tempbptr, tempsp);                               \
479     } while (0)
480
481 #define BRANCH(tempbptr, tempsp)                                     \
482     do {                                                             \
483         iptr->dst.block = tempbptr = BLOCK_OF(iptr->dst.insindex);   \
484         MARKREACHED(tempbptr, tempsp);                               \
485     } while (0)
486
487
488 /* stack_init ******************************************************************
489
490    Initialized the stack analysis subsystem (called by jit_init).
491
492 *******************************************************************************/
493
494 bool stack_init(void)
495 {
496         return true;
497 }
498
499
500 /* MARKREACHED marks the destination block <b> as reached. If this
501  * block has been reached before we check if stack depth and types
502  * match. Otherwise the destination block receives a copy of the
503  * current stack as its input stack.
504  *
505  * b...destination block
506  * c...current stack
507  */
508
509 static bool stack_mark_reached(stackdata_t *sd, basicblock *b, stackptr curstack, int stackdepth) 
510 {
511         stackptr sp, tsp;
512         int i;
513         s4 new_index;
514 #if defined(ENABLE_VERIFIER)
515         int           expectedtype;   /* used by CHECK_BASIC_TYPE                 */
516 #endif
517
518         if (b <= sd->bptr)
519                 b->bitflags |= BBFLAG_REPLACEMENT;
520
521         if (b->flags < BBREACHED) {
522                 /* b is reached for the first time. Create its instack */
523                 COPYCURSTACK(*sd, sp);
524                 b->flags = BBREACHED;
525                 b->instack = sp;
526                 b->indepth = stackdepth;
527                 b->invars = DMNEW(s4, stackdepth);
528                 for (i = stackdepth; i--; sp = sp->prev) { 
529                         b->invars[i] = sp->varnum;
530                         sd->var[sp->varnum].flags |= OUTVAR;
531                 }
532         } 
533         else {
534                 /* b has been reached before. Check that its instack matches */
535                 sp = curstack;
536                 tsp = b->instack;
537                 CHECK_STACK_DEPTH(b->indepth, stackdepth);
538                 while (sp) {
539                         CHECK_BASIC_TYPE(sp->type,tsp->type);
540                         sp = sp->prev;
541                         tsp = tsp->prev;
542                 }
543         }
544
545         return true;
546
547 #if defined(ENABLE_VERIFIER)
548 throw_stack_depth_error:
549         exceptions_throw_verifyerror(m,"Stack depth mismatch");
550         return false;
551
552 throw_stack_type_error:
553         exceptions_throw_verifyerror_for_stack(m, expectedtype);
554         return false;
555 #endif
556 }
557
558
559 /* stack_analyse ***************************************************************
560
561    Analyse_stack uses the intermediate code created by parse.c to
562    build a model of the JVM operand stack for the current method.
563    
564    The following checks are performed:
565      - check for operand stack underflow (before each instruction)
566      - check for operand stack overflow (after[1] each instruction)
567      - check for matching stack depth at merging points
568      - check for matching basic types[2] at merging points
569      - check basic types for instruction input (except for BUILTIN*
570            opcodes, INVOKE* opcodes and MULTIANEWARRAY)
571    
572    [1]) Checking this after the instruction should be ok. parse.c
573    counts the number of required stack slots in such a way that it is
574    only vital that we don't exceed `maxstack` at basic block
575    boundaries.
576    
577    [2]) 'basic types' means the distinction between INT, LONG, FLOAT,
578    DOUBLE and ADDRESS types. Subtypes of INT and different ADDRESS
579    types are not discerned.
580
581 *******************************************************************************/
582
583 bool new_stack_analyse(jitdata *jd)
584 {
585         methodinfo   *m;              /* method being analyzed                    */
586         codeinfo     *code;
587         codegendata  *cd;
588         registerdata *rd;
589         stackdata_t   sd;
590         int           b_count;        /* basic block counter                      */
591         int           b_index;        /* basic block index                        */
592         int           stackdepth;
593         stackptr      curstack;       /* current stack top                        */
594         stackptr      copy;
595         int           opcode;         /* opcode of current instruction            */
596         int           i, j;
597         int           javaindex;
598         int           len;            /* # of instructions after the current one  */
599         bool          superblockend;  /* if true, no fallthrough to next block    */
600         bool          repeat;         /* if true, outermost loop must run again   */
601         bool          deadcode;       /* true if no live code has been reached    */
602         instruction  *iptr;           /* the current instruction                  */
603         basicblock   *tbptr;
604
605         stackptr     *last_store_boundary;
606         stackptr      coalescing_boundary;
607
608         stackptr      src1, src2, src3, src4, dst1, dst2;
609
610         branch_target_t *table;
611         lookup_target_t *lookup;
612 #if defined(ENABLE_VERIFIER)
613         int           expectedtype;   /* used by CHECK_BASIC_TYPE                 */
614 #endif
615         builtintable_entry *bte;
616         methoddesc         *md;
617         constant_FMIref    *fmiref;
618 #if defined(ENABLE_STATISTICS)
619         int           iteration_count;  /* number of iterations of analysis       */
620 #endif
621         int           new_index; /* used to get a new var index with GET_NEW_INDEX*/
622
623 #if defined(STACK_VERBOSE)
624         new_show_method(jd, SHOW_PARSE);
625 #endif
626
627         /* get required compiler data - initialization */
628
629         m    = jd->m;
630         code = jd->code;
631         cd   = jd->cd;
632         rd   = jd->rd;
633
634         /* initialize the stackdata_t struct */
635
636         sd.varcount = jd->varcount;
637         sd.vartop =  jd->vartop;
638         sd.localcount = jd->localcount;
639         sd.var = jd->var;
640         sd.new = jd->new_stack;
641
642 #if defined(ENABLE_LSRA)
643         m->maxlifetimes = 0;
644 #endif
645
646 #if defined(ENABLE_STATISTICS)
647         iteration_count = 0;
648 #endif
649
650         /* init jd->interface_map */
651
652         jd->interface_map = DMNEW(interface_info, m->maxstack * 5);
653         for (i = 0; i < m->maxstack * 5; i++)
654                 jd->interface_map[i].flags = UNUSED;
655
656         last_store_boundary = DMNEW(stackptr, cd->maxlocals);
657
658         /* initialize in-stack of first block */
659
660         jd->new_basicblocks[0].flags = BBREACHED;
661         jd->new_basicblocks[0].instack = NULL;
662         jd->new_basicblocks[0].invars = NULL;
663         jd->new_basicblocks[0].indepth = 0;
664
665         /* initialize in-stack of exception handlers */
666
667         for (i = 0; i < cd->exceptiontablelength; i++) {
668                 sd.bptr = BLOCK_OF(cd->exceptiontable[i].handlerpc);
669                 sd.bptr->flags = BBREACHED;
670                 sd.bptr->type = BBTYPE_EXH;
671                 sd.bptr->instack = sd.new;
672                 sd.bptr->indepth = 1;
673                 sd.bptr->predecessorcount = CFG_UNKNOWN_PREDECESSORS;
674                 curstack = NULL; stackdepth = 0;
675                 GET_NEW_VAR(sd, new_index, TYPE_ADR);
676                 sd.bptr->invars = DMNEW(s4, 1);
677                 sd.bptr->invars[0] = new_index;
678                 sd.var[new_index].flags |= OUTVAR;
679                 NEWSTACK(TYPE_ADR, STACKVAR, new_index);
680                 curstack->creator = NULL;
681
682                 jd->interface_map[0 * 5 + TYPE_ADR].flags = 0;
683         }
684
685         /* stack analysis loop (until fixpoint reached) **************************/
686
687         do {
688 #if defined(ENABLE_STATISTICS)
689                 iteration_count++;
690 #endif
691
692                 /* initialize loop over basic blocks */
693
694                 b_count = jd->new_basicblockcount;
695                 sd.bptr = jd->new_basicblocks;
696                 superblockend = true;
697                 repeat = false;
698                 curstack = NULL; stackdepth = 0;
699                 deadcode = true;
700
701                 /* iterate over basic blocks *****************************************/
702
703                 while (--b_count >= 0) {
704 #if defined(STACK_VERBOSE)
705                         printf("----\nANALYZING BLOCK L%03d ", sd.bptr->nr);
706                         if (sd.bptr->type == BBTYPE_EXH) printf("EXH\n");
707                         else if (sd.bptr->type == BBTYPE_SBR) printf("SBR\n");
708                         else printf("STD\n");
709                            
710 #endif
711
712                         if (sd.bptr->flags == BBDELETED) {
713                                 /* This block has been deleted - do nothing. */
714                         }
715                         else if (superblockend && (sd.bptr->flags < BBREACHED)) {
716                                 /* This block has not been reached so far, and we      */
717                                 /* don't fall into it, so we'll have to iterate again. */
718                                 repeat = true;
719                         }
720                         else if (sd.bptr->flags <= BBREACHED) {
721                                 if (superblockend) {
722                                         /* We know that sd.bptr->flags == BBREACHED. */
723                                         /* This block has been reached before.    */
724                                         stackdepth = sd.bptr->indepth;
725                                 }
726                                 else if (sd.bptr->flags < BBREACHED) {
727                                         /* This block is reached for the first time now */
728                                         /* by falling through from the previous block.  */
729                                         /* Create the instack (propagated).             */
730                                         COPYCURSTACK(sd, copy);
731                                         sd.bptr->instack = copy;
732
733                                         sd.bptr->invars = DMNEW(s4, stackdepth);
734                                         for (i=stackdepth; i--; copy = copy->prev)
735                                                 sd.bptr->invars[i] = copy->varnum;
736                                         sd.bptr->indepth = stackdepth;
737                                 }
738                                 else {
739                                         /* This block has been reached before. now we are */
740                                         /* falling into it from the previous block.       */
741                                         /* Check that stack depth is well-defined.        */
742                                         CHECK_STACK_DEPTH(sd.bptr->indepth, stackdepth);
743
744                                         /* XXX check stack types? */
745                                 }
746
747                                 /* set up local variables for analyzing this block */
748
749                                 curstack = sd.bptr->instack;
750                                 deadcode = false;
751                                 superblockend = false;
752                                 len = sd.bptr->icount;
753                                 iptr = sd.bptr->iinstr;
754                                 b_index = sd.bptr - jd->new_basicblocks;
755
756                                 /* mark the block as analysed */
757
758                                 sd.bptr->flags = BBFINISHED;
759
760                                 /* reset variables for dependency checking */
761
762                                 coalescing_boundary = sd.new;
763                                 for( i = 0; i < cd->maxlocals; i++)
764                                         last_store_boundary[i] = sd.new;
765
766 #if defined(STACK_VERBOSE)
767                                 printf("INVARS\n");
768                                         for( copy = sd.bptr->instack; copy; copy = copy->prev ) {
769                                                 printf("%2d(%d", copy->varnum, copy->type);
770                                                 if (IS_OUTVAR(copy))
771                                                         printf("S");
772                                                 if (IS_PREALLOC(copy))
773                                                         printf("A");
774                                                 printf(") ");
775                                         }
776                                         printf("\n");
777
778                                         printf("INVARS - indices:\t\n");
779                                         for (i=0; i<sd.bptr->indepth; ++i) {
780                                                 printf("%d ", sd.bptr->invars[i]);
781                                         }
782                                         printf("\n\n");
783
784 #endif
785
786                                 /* iterate over ICMDs ****************************************/
787
788                                 while (--len >= 0)  {
789
790 #if defined(STACK_VERBOSE)
791                                         new_show_icmd(jd, iptr, false, SHOW_PARSE); printf("\n");
792                                         for( copy = curstack; copy; copy = copy->prev ) {
793                                                 printf("%2d(%d", copy->varnum, copy->type);
794                                                 if (IS_OUTVAR(copy))
795                                                         printf("S");
796                                                 if (IS_PREALLOC(copy))
797                                                         printf("A");
798                                                 printf(") ");
799                                         }
800                                         printf("\n");
801 #endif
802
803                                         /* fetch the current opcode */
804
805                                         opcode = iptr->opc;
806
807                                         /* automatically replace some ICMDs with builtins */
808
809 #if defined(USEBUILTINTABLE)
810                                         IF_NO_INTRP(
811                                                 bte = builtintable_get_automatic(opcode);
812
813                                                 if (bte && bte->opcode == opcode) {
814                                                         iptr->opc           = ICMD_BUILTIN;
815                                                         iptr->flags.bits    = 0;
816                                                         iptr->sx.s23.s3.bte = bte;
817                                                         /* iptr->line is already set */
818                                                         jd->isleafmethod = false;
819                                                         goto icmd_BUILTIN;
820                                                 }
821                                         );
822 #endif /* defined(USEBUILTINTABLE) */
823
824                                         /* main opcode switch *************************************/
825
826                                         switch (opcode) {
827
828                                                 /* pop 0 push 0 */
829
830                                         case ICMD_NOP:
831 icmd_NOP:
832                                                 CLR_SX;
833                                                 OP0_0;
834                                                 break;
835
836                                         case ICMD_CHECKNULL:
837                                                 coalescing_boundary = sd.new;
838                                                 COUNT(count_check_null);
839                                                 USE_S1(TYPE_ADR);
840                                                 CLR_SX;
841                                                 CLR_DST; /* XXX live through? */
842                                                 break;
843
844                                         case ICMD_RET:
845                                                 iptr->s1.varindex = 
846                                                         jd->local_map[iptr->s1.varindex * 5 + TYPE_ADR];
847                 
848                                                 USE_S1_LOCAL(TYPE_ADR);
849                                                 CLR_SX;
850                                                 CLR_DST;
851 #if 0
852                                                 IF_NO_INTRP( rd->locals[iptr->s1.localindex/*XXX invalid here*/][TYPE_ADR].type = TYPE_ADR; );
853 #endif
854                                                 superblockend = true;
855                                                 break;
856
857                                         case ICMD_RETURN:
858                                                 COUNT(count_pcmd_return);
859                                                 CLR_SX;
860                                                 OP0_0;
861                                                 superblockend = true;
862                                                 break;
863
864
865                                                 /* pop 0 push 1 const */
866
867         /************************** ICONST OPTIMIZATIONS **************************/
868
869                                         case ICMD_ICONST:
870                                                 COUNT(count_pcmd_load);
871                                                 if (len == 0)
872                                                         goto normal_ICONST;
873
874                                                 switch (iptr[1].opc) {
875                                                         case ICMD_IADD:
876                                                                 iptr->opc = ICMD_IADDCONST;
877                                                                 /* FALLTHROUGH */
878
879                                                         icmd_iconst_tail:
880                                                                 iptr[1].opc = ICMD_NOP;
881                                                                 OP1_1(TYPE_INT, TYPE_INT);
882                                                                 COUNT(count_pcmd_op);
883                                                                 break;
884
885                                                         case ICMD_ISUB:
886                                                                 iptr->opc = ICMD_ISUBCONST;
887                                                                 goto icmd_iconst_tail;
888 #if SUPPORT_CONST_MUL
889                                                         case ICMD_IMUL:
890                                                                 iptr->opc = ICMD_IMULCONST;
891                                                                 goto icmd_iconst_tail;
892 #else /* SUPPORT_CONST_MUL */
893                                                         case ICMD_IMUL:
894                                                                 if (iptr->sx.val.i == 0x00000002)
895                                                                         iptr->sx.val.i = 1;
896                                                                 else if (iptr->sx.val.i == 0x00000004)
897                                                                         iptr->sx.val.i = 2;
898                                                                 else if (iptr->sx.val.i == 0x00000008)
899                                                                         iptr->sx.val.i = 3;
900                                                                 else if (iptr->sx.val.i == 0x00000010)
901                                                                         iptr->sx.val.i = 4;
902                                                                 else if (iptr->sx.val.i == 0x00000020)
903                                                                         iptr->sx.val.i = 5;
904                                                                 else if (iptr->sx.val.i == 0x00000040)
905                                                                         iptr->sx.val.i = 6;
906                                                                 else if (iptr->sx.val.i == 0x00000080)
907                                                                         iptr->sx.val.i = 7;
908                                                                 else if (iptr->sx.val.i == 0x00000100)
909                                                                         iptr->sx.val.i = 8;
910                                                                 else if (iptr->sx.val.i == 0x00000200)
911                                                                         iptr->sx.val.i = 9;
912                                                                 else if (iptr->sx.val.i == 0x00000400)
913                                                                         iptr->sx.val.i = 10;
914                                                                 else if (iptr->sx.val.i == 0x00000800)
915                                                                         iptr->sx.val.i = 11;
916                                                                 else if (iptr->sx.val.i == 0x00001000)
917                                                                         iptr->sx.val.i = 12;
918                                                                 else if (iptr->sx.val.i == 0x00002000)
919                                                                         iptr->sx.val.i = 13;
920                                                                 else if (iptr->sx.val.i == 0x00004000)
921                                                                         iptr->sx.val.i = 14;
922                                                                 else if (iptr->sx.val.i == 0x00008000)
923                                                                         iptr->sx.val.i = 15;
924                                                                 else if (iptr->sx.val.i == 0x00010000)
925                                                                         iptr->sx.val.i = 16;
926                                                                 else if (iptr->sx.val.i == 0x00020000)
927                                                                         iptr->sx.val.i = 17;
928                                                                 else if (iptr->sx.val.i == 0x00040000)
929                                                                         iptr->sx.val.i = 18;
930                                                                 else if (iptr->sx.val.i == 0x00080000)
931                                                                         iptr->sx.val.i = 19;
932                                                                 else if (iptr->sx.val.i == 0x00100000)
933                                                                         iptr->sx.val.i = 20;
934                                                                 else if (iptr->sx.val.i == 0x00200000)
935                                                                         iptr->sx.val.i = 21;
936                                                                 else if (iptr->sx.val.i == 0x00400000)
937                                                                         iptr->sx.val.i = 22;
938                                                                 else if (iptr->sx.val.i == 0x00800000)
939                                                                         iptr->sx.val.i = 23;
940                                                                 else if (iptr->sx.val.i == 0x01000000)
941                                                                         iptr->sx.val.i = 24;
942                                                                 else if (iptr->sx.val.i == 0x02000000)
943                                                                         iptr->sx.val.i = 25;
944                                                                 else if (iptr->sx.val.i == 0x04000000)
945                                                                         iptr->sx.val.i = 26;
946                                                                 else if (iptr->sx.val.i == 0x08000000)
947                                                                         iptr->sx.val.i = 27;
948                                                                 else if (iptr->sx.val.i == 0x10000000)
949                                                                         iptr->sx.val.i = 28;
950                                                                 else if (iptr->sx.val.i == 0x20000000)
951                                                                         iptr->sx.val.i = 29;
952                                                                 else if (iptr->sx.val.i == 0x40000000)
953                                                                         iptr->sx.val.i = 30;
954                                                                 else if (iptr->sx.val.i == 0x80000000)
955                                                                         iptr->sx.val.i = 31;
956                                                                 else
957                                                                         goto normal_ICONST;
958
959                                                                 iptr->opc = ICMD_IMULPOW2;
960                                                                 goto icmd_iconst_tail;
961 #endif /* SUPPORT_CONST_MUL */
962                                                         case ICMD_IDIV:
963                                                                 if (iptr->sx.val.i == 0x00000002)
964                                                                         iptr->sx.val.i = 1;
965                                                                 else if (iptr->sx.val.i == 0x00000004)
966                                                                         iptr->sx.val.i = 2;
967                                                                 else if (iptr->sx.val.i == 0x00000008)
968                                                                         iptr->sx.val.i = 3;
969                                                                 else if (iptr->sx.val.i == 0x00000010)
970                                                                         iptr->sx.val.i = 4;
971                                                                 else if (iptr->sx.val.i == 0x00000020)
972                                                                         iptr->sx.val.i = 5;
973                                                                 else if (iptr->sx.val.i == 0x00000040)
974                                                                         iptr->sx.val.i = 6;
975                                                                 else if (iptr->sx.val.i == 0x00000080)
976                                                                         iptr->sx.val.i = 7;
977                                                                 else if (iptr->sx.val.i == 0x00000100)
978                                                                         iptr->sx.val.i = 8;
979                                                                 else if (iptr->sx.val.i == 0x00000200)
980                                                                         iptr->sx.val.i = 9;
981                                                                 else if (iptr->sx.val.i == 0x00000400)
982                                                                         iptr->sx.val.i = 10;
983                                                                 else if (iptr->sx.val.i == 0x00000800)
984                                                                         iptr->sx.val.i = 11;
985                                                                 else if (iptr->sx.val.i == 0x00001000)
986                                                                         iptr->sx.val.i = 12;
987                                                                 else if (iptr->sx.val.i == 0x00002000)
988                                                                         iptr->sx.val.i = 13;
989                                                                 else if (iptr->sx.val.i == 0x00004000)
990                                                                         iptr->sx.val.i = 14;
991                                                                 else if (iptr->sx.val.i == 0x00008000)
992                                                                         iptr->sx.val.i = 15;
993                                                                 else if (iptr->sx.val.i == 0x00010000)
994                                                                         iptr->sx.val.i = 16;
995                                                                 else if (iptr->sx.val.i == 0x00020000)
996                                                                         iptr->sx.val.i = 17;
997                                                                 else if (iptr->sx.val.i == 0x00040000)
998                                                                         iptr->sx.val.i = 18;
999                                                                 else if (iptr->sx.val.i == 0x00080000)
1000                                                                         iptr->sx.val.i = 19;
1001                                                                 else if (iptr->sx.val.i == 0x00100000)
1002                                                                         iptr->sx.val.i = 20;
1003                                                                 else if (iptr->sx.val.i == 0x00200000)
1004                                                                         iptr->sx.val.i = 21;
1005                                                                 else if (iptr->sx.val.i == 0x00400000)
1006                                                                         iptr->sx.val.i = 22;
1007                                                                 else if (iptr->sx.val.i == 0x00800000)
1008                                                                         iptr->sx.val.i = 23;
1009                                                                 else if (iptr->sx.val.i == 0x01000000)
1010                                                                         iptr->sx.val.i = 24;
1011                                                                 else if (iptr->sx.val.i == 0x02000000)
1012                                                                         iptr->sx.val.i = 25;
1013                                                                 else if (iptr->sx.val.i == 0x04000000)
1014                                                                         iptr->sx.val.i = 26;
1015                                                                 else if (iptr->sx.val.i == 0x08000000)
1016                                                                         iptr->sx.val.i = 27;
1017                                                                 else if (iptr->sx.val.i == 0x10000000)
1018                                                                         iptr->sx.val.i = 28;
1019                                                                 else if (iptr->sx.val.i == 0x20000000)
1020                                                                         iptr->sx.val.i = 29;
1021                                                                 else if (iptr->sx.val.i == 0x40000000)
1022                                                                         iptr->sx.val.i = 30;
1023                                                                 else if (iptr->sx.val.i == 0x80000000)
1024                                                                         iptr->sx.val.i = 31;
1025                                                                 else
1026                                                                         goto normal_ICONST;
1027
1028                                                                 iptr->opc = ICMD_IDIVPOW2;
1029                                                                 goto icmd_iconst_tail;
1030
1031                                                         case ICMD_IREM:
1032                                                                 /*log_text("stack.c: ICMD_ICONST/ICMD_IREM");*/
1033                                                                 if ((iptr->sx.val.i == 0x00000002) ||
1034                                                                         (iptr->sx.val.i == 0x00000004) ||
1035                                                                         (iptr->sx.val.i == 0x00000008) ||
1036                                                                         (iptr->sx.val.i == 0x00000010) ||
1037                                                                         (iptr->sx.val.i == 0x00000020) ||
1038                                                                         (iptr->sx.val.i == 0x00000040) ||
1039                                                                         (iptr->sx.val.i == 0x00000080) ||
1040                                                                         (iptr->sx.val.i == 0x00000100) ||
1041                                                                         (iptr->sx.val.i == 0x00000200) ||
1042                                                                         (iptr->sx.val.i == 0x00000400) ||
1043                                                                         (iptr->sx.val.i == 0x00000800) ||
1044                                                                         (iptr->sx.val.i == 0x00001000) ||
1045                                                                         (iptr->sx.val.i == 0x00002000) ||
1046                                                                         (iptr->sx.val.i == 0x00004000) ||
1047                                                                         (iptr->sx.val.i == 0x00008000) ||
1048                                                                         (iptr->sx.val.i == 0x00010000) ||
1049                                                                         (iptr->sx.val.i == 0x00020000) ||
1050                                                                         (iptr->sx.val.i == 0x00040000) ||
1051                                                                         (iptr->sx.val.i == 0x00080000) ||
1052                                                                         (iptr->sx.val.i == 0x00100000) ||
1053                                                                         (iptr->sx.val.i == 0x00200000) ||
1054                                                                         (iptr->sx.val.i == 0x00400000) ||
1055                                                                         (iptr->sx.val.i == 0x00800000) ||
1056                                                                         (iptr->sx.val.i == 0x01000000) ||
1057                                                                         (iptr->sx.val.i == 0x02000000) ||
1058                                                                         (iptr->sx.val.i == 0x04000000) ||
1059                                                                         (iptr->sx.val.i == 0x08000000) ||
1060                                                                         (iptr->sx.val.i == 0x10000000) ||
1061                                                                         (iptr->sx.val.i == 0x20000000) ||
1062                                                                         (iptr->sx.val.i == 0x40000000) ||
1063                                                                         (iptr->sx.val.i == 0x80000000))
1064                                                                 {
1065                                                                         iptr->opc = ICMD_IREMPOW2;
1066                                                                         iptr->sx.val.i -= 1;
1067                                                                         goto icmd_iconst_tail;
1068                                                                 }
1069                                                                 goto normal_ICONST;
1070 #if SUPPORT_CONST_LOGICAL
1071                                                         case ICMD_IAND:
1072                                                                 iptr->opc = ICMD_IANDCONST;
1073                                                                 goto icmd_iconst_tail;
1074
1075                                                         case ICMD_IOR:
1076                                                                 iptr->opc = ICMD_IORCONST;
1077                                                                 goto icmd_iconst_tail;
1078
1079                                                         case ICMD_IXOR:
1080                                                                 iptr->opc = ICMD_IXORCONST;
1081                                                                 goto icmd_iconst_tail;
1082
1083 #endif /* SUPPORT_CONST_LOGICAL */
1084                                                         case ICMD_ISHL:
1085                                                                 iptr->opc = ICMD_ISHLCONST;
1086                                                                 goto icmd_iconst_tail;
1087
1088                                                         case ICMD_ISHR:
1089                                                                 iptr->opc = ICMD_ISHRCONST;
1090                                                                 goto icmd_iconst_tail;
1091
1092                                                         case ICMD_IUSHR:
1093                                                                 iptr->opc = ICMD_IUSHRCONST;
1094                                                                 goto icmd_iconst_tail;
1095 #if SUPPORT_LONG_SHIFT
1096                                                         case ICMD_LSHL:
1097                                                                 iptr->opc = ICMD_LSHLCONST;
1098                                                                 goto icmd_lconst_tail;
1099
1100                                                         case ICMD_LSHR:
1101                                                                 iptr->opc = ICMD_LSHRCONST;
1102                                                                 goto icmd_lconst_tail;
1103
1104                                                         case ICMD_LUSHR:
1105                                                                 iptr->opc = ICMD_LUSHRCONST;
1106                                                                 goto icmd_lconst_tail;
1107 #endif /* SUPPORT_LONG_SHIFT */
1108                                                         case ICMD_IF_ICMPEQ:
1109                                                                 iptr[1].opc = ICMD_IFEQ;
1110                                                                 /* FALLTHROUGH */
1111
1112                                                         icmd_if_icmp_tail:
1113                                                                 /* set the constant for the following icmd */
1114                                                                 iptr[1].sx.val.i = iptr->sx.val.i;
1115
1116                                                                 /* this instruction becomes a nop */
1117                                                                 iptr->opc = ICMD_NOP;
1118                                                                 goto icmd_NOP;
1119
1120                                                         case ICMD_IF_ICMPLT:
1121                                                                 iptr[1].opc = ICMD_IFLT;
1122                                                                 goto icmd_if_icmp_tail;
1123
1124                                                         case ICMD_IF_ICMPLE:
1125                                                                 iptr[1].opc = ICMD_IFLE;
1126                                                                 goto icmd_if_icmp_tail;
1127
1128                                                         case ICMD_IF_ICMPNE:
1129                                                                 iptr[1].opc = ICMD_IFNE;
1130                                                                 goto icmd_if_icmp_tail;
1131
1132                                                         case ICMD_IF_ICMPGT:
1133                                                                 iptr[1].opc = ICMD_IFGT;
1134                                                                 goto icmd_if_icmp_tail;
1135
1136                                                         case ICMD_IF_ICMPGE:
1137                                                                 iptr[1].opc = ICMD_IFGE;
1138                                                                 goto icmd_if_icmp_tail;
1139
1140 #if SUPPORT_CONST_STORE
1141                                                         case ICMD_IASTORE:
1142                                                         case ICMD_BASTORE:
1143                                                         case ICMD_CASTORE:
1144                                                         case ICMD_SASTORE:
1145                                                                 IF_INTRP( goto normal_ICONST; )
1146 # if SUPPORT_CONST_STORE_ZERO_ONLY
1147                                                                 if (iptr->sx.val.i != 0)
1148                                                                         goto normal_ICONST;
1149 # endif
1150                                                                 switch (iptr[1].opc) {
1151                                                                         case ICMD_IASTORE:
1152                                                                                 iptr->opc = ICMD_IASTORECONST;
1153                                                                                 iptr->flags.bits |= INS_FLAG_CHECK;
1154                                                                                 break;
1155                                                                         case ICMD_BASTORE:
1156                                                                                 iptr->opc = ICMD_BASTORECONST;
1157                                                                                 iptr->flags.bits |= INS_FLAG_CHECK;
1158                                                                                 break;
1159                                                                         case ICMD_CASTORE:
1160                                                                                 iptr->opc = ICMD_CASTORECONST;
1161                                                                                 iptr->flags.bits |= INS_FLAG_CHECK;
1162                                                                                 break;
1163                                                                         case ICMD_SASTORE:
1164                                                                                 iptr->opc = ICMD_SASTORECONST;
1165                                                                                 iptr->flags.bits |= INS_FLAG_CHECK;
1166                                                                                 break;
1167                                                                 }
1168
1169                                                                 iptr[1].opc = ICMD_NOP;
1170
1171                                                                 /* copy the constant to s3 */
1172                                                                 /* XXX constval -> astoreconstval? */
1173                                                                 iptr->sx.s23.s3.constval = iptr->sx.val.i;
1174                                                                 OP2_0(TYPE_ADR, TYPE_INT);
1175                                                                 COUNT(count_pcmd_op);
1176                                                                 break;
1177
1178                                                         case ICMD_PUTSTATIC:
1179                                                         case ICMD_PUTFIELD:
1180                                                                 IF_INTRP( goto normal_ICONST; )
1181 # if SUPPORT_CONST_STORE_ZERO_ONLY
1182                                                                 if (iptr->sx.val.i != 0)
1183                                                                         goto normal_ICONST;
1184 # endif
1185                                                                 /* XXX check field type? */
1186
1187                                                                 /* copy the constant to s2 */
1188                                                                 /* XXX constval -> fieldconstval? */
1189                                                                 iptr->sx.s23.s2.constval = iptr->sx.val.i;
1190
1191 putconst_tail:
1192                                                                 /* set the field reference (s3) */
1193                                                                 if (iptr[1].flags.bits & INS_FLAG_UNRESOLVED) {
1194                                                                         iptr->sx.s23.s3.uf = iptr[1].sx.s23.s3.uf;
1195                                                                         iptr->flags.bits |= INS_FLAG_UNRESOLVED;
1196                                                                 }
1197                                                                 else {
1198                                                                         iptr->sx.s23.s3.fmiref = iptr[1].sx.s23.s3.fmiref;
1199                                                                 }
1200                                                                 
1201                                                                 switch (iptr[1].opc) {
1202                                                                         case ICMD_PUTSTATIC:
1203                                                                                 iptr->opc = ICMD_PUTSTATICCONST;
1204                                                                                 OP0_0;
1205                                                                                 break;
1206                                                                         case ICMD_PUTFIELD:
1207                                                                                 iptr->opc = ICMD_PUTFIELDCONST;
1208                                                                                 OP1_0(TYPE_ADR);
1209                                                                                 break;
1210                                                                 }
1211
1212                                                                 iptr[1].opc = ICMD_NOP;
1213                                                                 COUNT(count_pcmd_op);
1214                                                                 break;
1215 #endif /* SUPPORT_CONST_STORE */
1216
1217                                                         default:
1218                                                                 goto normal_ICONST;
1219                                                 }
1220
1221                                                 /* if we get here, the ICONST has been optimized */
1222                                                 break;
1223
1224 normal_ICONST:
1225                                                 /* normal case of an unoptimized ICONST */
1226                                                 OP0_1(TYPE_INT);
1227                                                 break;
1228
1229         /************************** LCONST OPTIMIZATIONS **************************/
1230
1231                                         case ICMD_LCONST:
1232                                                 COUNT(count_pcmd_load);
1233                                                 if (len == 0)
1234                                                         goto normal_LCONST;
1235
1236                                                 /* switch depending on the following instruction */
1237
1238                                                 switch (iptr[1].opc) {
1239 #if SUPPORT_LONG_ADD
1240                                                         case ICMD_LADD:
1241                                                                 iptr->opc = ICMD_LADDCONST;
1242                                                                 /* FALLTHROUGH */
1243
1244                                                         icmd_lconst_tail:
1245                                                                 /* instruction of type LONG -> LONG */
1246                                                                 iptr[1].opc = ICMD_NOP;
1247                                                                 OP1_1(TYPE_LNG, TYPE_LNG);
1248                                                                 COUNT(count_pcmd_op);
1249                                                                 break;
1250
1251                                                         case ICMD_LSUB:
1252                                                                 iptr->opc = ICMD_LSUBCONST;
1253                                                                 goto icmd_lconst_tail;
1254
1255 #endif /* SUPPORT_LONG_ADD */
1256 #if SUPPORT_LONG_MUL && SUPPORT_CONST_MUL
1257                                                         case ICMD_LMUL:
1258                                                                 iptr->opc = ICMD_LMULCONST;
1259                                                                 goto icmd_lconst_tail;
1260 #else /* SUPPORT_LONG_MUL && SUPPORT_CONST_MUL */
1261 # if SUPPORT_LONG_SHIFT
1262                                                         case ICMD_LMUL:
1263                                                                 if (iptr->sx.val.l == 0x00000002)
1264                                                                         iptr->sx.val.i = 1;
1265                                                                 else if (iptr->sx.val.l == 0x00000004)
1266                                                                         iptr->sx.val.i = 2;
1267                                                                 else if (iptr->sx.val.l == 0x00000008)
1268                                                                         iptr->sx.val.i = 3;
1269                                                                 else if (iptr->sx.val.l == 0x00000010)
1270                                                                         iptr->sx.val.i = 4;
1271                                                                 else if (iptr->sx.val.l == 0x00000020)
1272                                                                         iptr->sx.val.i = 5;
1273                                                                 else if (iptr->sx.val.l == 0x00000040)
1274                                                                         iptr->sx.val.i = 6;
1275                                                                 else if (iptr->sx.val.l == 0x00000080)
1276                                                                         iptr->sx.val.i = 7;
1277                                                                 else if (iptr->sx.val.l == 0x00000100)
1278                                                                         iptr->sx.val.i = 8;
1279                                                                 else if (iptr->sx.val.l == 0x00000200)
1280                                                                         iptr->sx.val.i = 9;
1281                                                                 else if (iptr->sx.val.l == 0x00000400)
1282                                                                         iptr->sx.val.i = 10;
1283                                                                 else if (iptr->sx.val.l == 0x00000800)
1284                                                                         iptr->sx.val.i = 11;
1285                                                                 else if (iptr->sx.val.l == 0x00001000)
1286                                                                         iptr->sx.val.i = 12;
1287                                                                 else if (iptr->sx.val.l == 0x00002000)
1288                                                                         iptr->sx.val.i = 13;
1289                                                                 else if (iptr->sx.val.l == 0x00004000)
1290                                                                         iptr->sx.val.i = 14;
1291                                                                 else if (iptr->sx.val.l == 0x00008000)
1292                                                                         iptr->sx.val.i = 15;
1293                                                                 else if (iptr->sx.val.l == 0x00010000)
1294                                                                         iptr->sx.val.i = 16;
1295                                                                 else if (iptr->sx.val.l == 0x00020000)
1296                                                                         iptr->sx.val.i = 17;
1297                                                                 else if (iptr->sx.val.l == 0x00040000)
1298                                                                         iptr->sx.val.i = 18;
1299                                                                 else if (iptr->sx.val.l == 0x00080000)
1300                                                                         iptr->sx.val.i = 19;
1301                                                                 else if (iptr->sx.val.l == 0x00100000)
1302                                                                         iptr->sx.val.i = 20;
1303                                                                 else if (iptr->sx.val.l == 0x00200000)
1304                                                                         iptr->sx.val.i = 21;
1305                                                                 else if (iptr->sx.val.l == 0x00400000)
1306                                                                         iptr->sx.val.i = 22;
1307                                                                 else if (iptr->sx.val.l == 0x00800000)
1308                                                                         iptr->sx.val.i = 23;
1309                                                                 else if (iptr->sx.val.l == 0x01000000)
1310                                                                         iptr->sx.val.i = 24;
1311                                                                 else if (iptr->sx.val.l == 0x02000000)
1312                                                                         iptr->sx.val.i = 25;
1313                                                                 else if (iptr->sx.val.l == 0x04000000)
1314                                                                         iptr->sx.val.i = 26;
1315                                                                 else if (iptr->sx.val.l == 0x08000000)
1316                                                                         iptr->sx.val.i = 27;
1317                                                                 else if (iptr->sx.val.l == 0x10000000)
1318                                                                         iptr->sx.val.i = 28;
1319                                                                 else if (iptr->sx.val.l == 0x20000000)
1320                                                                         iptr->sx.val.i = 29;
1321                                                                 else if (iptr->sx.val.l == 0x40000000)
1322                                                                         iptr->sx.val.i = 30;
1323                                                                 else if (iptr->sx.val.l == 0x80000000)
1324                                                                         iptr->sx.val.i = 31;
1325                                                                 else {
1326                                                                         goto normal_LCONST;
1327                                                                 }
1328                                                                 iptr->opc = ICMD_LMULPOW2;
1329                                                                 goto icmd_lconst_tail;
1330 # endif /* SUPPORT_LONG_SHIFT */
1331 #endif /* SUPPORT_LONG_MUL && SUPPORT_CONST_MUL */
1332 #if SUPPORT_LONG_DIV_POW2
1333                                                         case ICMD_LDIV:
1334                                                                 if (iptr->sx.val.l == 0x00000002)
1335                                                                         iptr->sx.val.i = 1;
1336                                                                 else if (iptr->sx.val.l == 0x00000004)
1337                                                                         iptr->sx.val.i = 2;
1338                                                                 else if (iptr->sx.val.l == 0x00000008)
1339                                                                         iptr->sx.val.i = 3;
1340                                                                 else if (iptr->sx.val.l == 0x00000010)
1341                                                                         iptr->sx.val.i = 4;
1342                                                                 else if (iptr->sx.val.l == 0x00000020)
1343                                                                         iptr->sx.val.i = 5;
1344                                                                 else if (iptr->sx.val.l == 0x00000040)
1345                                                                         iptr->sx.val.i = 6;
1346                                                                 else if (iptr->sx.val.l == 0x00000080)
1347                                                                         iptr->sx.val.i = 7;
1348                                                                 else if (iptr->sx.val.l == 0x00000100)
1349                                                                         iptr->sx.val.i = 8;
1350                                                                 else if (iptr->sx.val.l == 0x00000200)
1351                                                                         iptr->sx.val.i = 9;
1352                                                                 else if (iptr->sx.val.l == 0x00000400)
1353                                                                         iptr->sx.val.i = 10;
1354                                                                 else if (iptr->sx.val.l == 0x00000800)
1355                                                                         iptr->sx.val.i = 11;
1356                                                                 else if (iptr->sx.val.l == 0x00001000)
1357                                                                         iptr->sx.val.i = 12;
1358                                                                 else if (iptr->sx.val.l == 0x00002000)
1359                                                                         iptr->sx.val.i = 13;
1360                                                                 else if (iptr->sx.val.l == 0x00004000)
1361                                                                         iptr->sx.val.i = 14;
1362                                                                 else if (iptr->sx.val.l == 0x00008000)
1363                                                                         iptr->sx.val.i = 15;
1364                                                                 else if (iptr->sx.val.l == 0x00010000)
1365                                                                         iptr->sx.val.i = 16;
1366                                                                 else if (iptr->sx.val.l == 0x00020000)
1367                                                                         iptr->sx.val.i = 17;
1368                                                                 else if (iptr->sx.val.l == 0x00040000)
1369                                                                         iptr->sx.val.i = 18;
1370                                                                 else if (iptr->sx.val.l == 0x00080000)
1371                                                                         iptr->sx.val.i = 19;
1372                                                                 else if (iptr->sx.val.l == 0x00100000)
1373                                                                         iptr->sx.val.i = 20;
1374                                                                 else if (iptr->sx.val.l == 0x00200000)
1375                                                                         iptr->sx.val.i = 21;
1376                                                                 else if (iptr->sx.val.l == 0x00400000)
1377                                                                         iptr->sx.val.i = 22;
1378                                                                 else if (iptr->sx.val.l == 0x00800000)
1379                                                                         iptr->sx.val.i = 23;
1380                                                                 else if (iptr->sx.val.l == 0x01000000)
1381                                                                         iptr->sx.val.i = 24;
1382                                                                 else if (iptr->sx.val.l == 0x02000000)
1383                                                                         iptr->sx.val.i = 25;
1384                                                                 else if (iptr->sx.val.l == 0x04000000)
1385                                                                         iptr->sx.val.i = 26;
1386                                                                 else if (iptr->sx.val.l == 0x08000000)
1387                                                                         iptr->sx.val.i = 27;
1388                                                                 else if (iptr->sx.val.l == 0x10000000)
1389                                                                         iptr->sx.val.i = 28;
1390                                                                 else if (iptr->sx.val.l == 0x20000000)
1391                                                                         iptr->sx.val.i = 29;
1392                                                                 else if (iptr->sx.val.l == 0x40000000)
1393                                                                         iptr->sx.val.i = 30;
1394                                                                 else if (iptr->sx.val.l == 0x80000000)
1395                                                                         iptr->sx.val.i = 31;
1396                                                                 else {
1397                                                                         goto normal_LCONST;
1398                                                                 }
1399                                                                 iptr->opc = ICMD_LDIVPOW2;
1400                                                                 goto icmd_lconst_tail;
1401 #endif /* SUPPORT_LONG_DIV_POW2 */
1402
1403 #if SUPPORT_LONG_REM_POW2
1404                                                         case ICMD_LREM:
1405                                                                 if ((iptr->sx.val.l == 0x00000002) ||
1406                                                                         (iptr->sx.val.l == 0x00000004) ||
1407                                                                         (iptr->sx.val.l == 0x00000008) ||
1408                                                                         (iptr->sx.val.l == 0x00000010) ||
1409                                                                         (iptr->sx.val.l == 0x00000020) ||
1410                                                                         (iptr->sx.val.l == 0x00000040) ||
1411                                                                         (iptr->sx.val.l == 0x00000080) ||
1412                                                                         (iptr->sx.val.l == 0x00000100) ||
1413                                                                         (iptr->sx.val.l == 0x00000200) ||
1414                                                                         (iptr->sx.val.l == 0x00000400) ||
1415                                                                         (iptr->sx.val.l == 0x00000800) ||
1416                                                                         (iptr->sx.val.l == 0x00001000) ||
1417                                                                         (iptr->sx.val.l == 0x00002000) ||
1418                                                                         (iptr->sx.val.l == 0x00004000) ||
1419                                                                         (iptr->sx.val.l == 0x00008000) ||
1420                                                                         (iptr->sx.val.l == 0x00010000) ||
1421                                                                         (iptr->sx.val.l == 0x00020000) ||
1422                                                                         (iptr->sx.val.l == 0x00040000) ||
1423                                                                         (iptr->sx.val.l == 0x00080000) ||
1424                                                                         (iptr->sx.val.l == 0x00100000) ||
1425                                                                         (iptr->sx.val.l == 0x00200000) ||
1426                                                                         (iptr->sx.val.l == 0x00400000) ||
1427                                                                         (iptr->sx.val.l == 0x00800000) ||
1428                                                                         (iptr->sx.val.l == 0x01000000) ||
1429                                                                         (iptr->sx.val.l == 0x02000000) ||
1430                                                                         (iptr->sx.val.l == 0x04000000) ||
1431                                                                         (iptr->sx.val.l == 0x08000000) ||
1432                                                                         (iptr->sx.val.l == 0x10000000) ||
1433                                                                         (iptr->sx.val.l == 0x20000000) ||
1434                                                                         (iptr->sx.val.l == 0x40000000) ||
1435                                                                         (iptr->sx.val.l == 0x80000000))
1436                                                                 {
1437                                                                         iptr->opc = ICMD_LREMPOW2;
1438                                                                         iptr->sx.val.l -= 1;
1439                                                                         goto icmd_lconst_tail;
1440                                                                 }
1441                                                                 goto normal_LCONST;
1442 #endif /* SUPPORT_LONG_REM_POW2 */
1443
1444 #if SUPPORT_LONG_LOGICAL && SUPPORT_CONST_LOGICAL
1445
1446                                                         case ICMD_LAND:
1447                                                                 iptr->opc = ICMD_LANDCONST;
1448                                                                 goto icmd_lconst_tail;
1449
1450                                                         case ICMD_LOR:
1451                                                                 iptr->opc = ICMD_LORCONST;
1452                                                                 goto icmd_lconst_tail;
1453
1454                                                         case ICMD_LXOR:
1455                                                                 iptr->opc = ICMD_LXORCONST;
1456                                                                 goto icmd_lconst_tail;
1457 #endif /* SUPPORT_LONG_LOGICAL && SUPPORT_CONST_LOGICAL */
1458
1459 #if SUPPORT_LONG_CMP_CONST
1460                                                         case ICMD_LCMP:
1461                                                                 if ((len <= 1) || (iptr[2].sx.val.i != 0))
1462                                                                         goto normal_LCONST;
1463
1464                                                                 /* switch on the instruction after LCONST - LCMP */
1465
1466                                                                 switch (iptr[2].opc) {
1467                                                                         case ICMD_IFEQ:
1468                                                                                 iptr->opc = ICMD_IF_LEQ;
1469                                                                                 /* FALLTHROUGH */
1470
1471                                                                         icmd_lconst_lcmp_tail:
1472                                                                                 /* convert LCONST, LCMP, IFXX to IF_LXX */
1473                                                                                 iptr->dst.insindex = iptr[2].dst.insindex;
1474                                                                                 iptr[1].opc = ICMD_NOP;
1475                                                                                 iptr[2].opc = ICMD_NOP;
1476
1477                                                                                 OP1_BRANCH(TYPE_LNG);
1478                                                                                 BRANCH(tbptr, copy);
1479                                                                                 COUNT(count_pcmd_bra);
1480                                                                                 COUNT(count_pcmd_op);
1481                                                                                 break;
1482
1483                                                                         case ICMD_IFNE:
1484                                                                                 iptr->opc = ICMD_IF_LNE;
1485                                                                                 goto icmd_lconst_lcmp_tail;
1486
1487                                                                         case ICMD_IFLT:
1488                                                                                 iptr->opc = ICMD_IF_LLT;
1489                                                                                 goto icmd_lconst_lcmp_tail;
1490
1491                                                                         case ICMD_IFGT:
1492                                                                                 iptr->opc = ICMD_IF_LGT;
1493                                                                                 goto icmd_lconst_lcmp_tail;
1494
1495                                                                         case ICMD_IFLE:
1496                                                                                 iptr->opc = ICMD_IF_LLE;
1497                                                                                 goto icmd_lconst_lcmp_tail;
1498
1499                                                                         case ICMD_IFGE:
1500                                                                                 iptr->opc = ICMD_IF_LGE;
1501                                                                                 goto icmd_lconst_lcmp_tail;
1502
1503                                                                         default:
1504                                                                                 goto normal_LCONST;
1505                                                                 } /* end switch on opcode after LCONST - LCMP */
1506                                                                 break;
1507 #endif /* SUPPORT_LONG_CMP_CONST */
1508
1509 #if SUPPORT_CONST_STORE
1510                                                         case ICMD_LASTORE:
1511                                                                 IF_INTRP( goto normal_LCONST; )
1512 # if SUPPORT_CONST_STORE_ZERO_ONLY
1513                                                                 if (iptr->sx.val.l != 0)
1514                                                                         goto normal_LCONST;
1515 # endif
1516 #if SIZEOF_VOID_P == 4
1517                                                                 /* the constant must fit into a ptrint */
1518                                                                 if (iptr->sx.val.l < -0x80000000L || iptr->sx.val.l >= 0x80000000L)
1519                                                                         goto normal_LCONST;
1520 #endif
1521                                                                 /* move the constant to s3 */
1522                                                                 iptr->sx.s23.s3.constval = iptr->sx.val.l;
1523
1524                                                                 iptr->opc = ICMD_LASTORECONST;
1525                                                                 iptr->flags.bits |= INS_FLAG_CHECK;
1526                                                                 OP2_0(TYPE_ADR, TYPE_INT);
1527
1528                                                                 iptr[1].opc = ICMD_NOP;
1529                                                                 COUNT(count_pcmd_op);
1530                                                                 break;
1531
1532                                                         case ICMD_PUTSTATIC:
1533                                                         case ICMD_PUTFIELD:
1534                                                                 IF_INTRP( goto normal_LCONST; )
1535 # if SUPPORT_CONST_STORE_ZERO_ONLY
1536                                                                 if (iptr->sx.val.l != 0)
1537                                                                         goto normal_LCONST;
1538 # endif
1539 #if SIZEOF_VOID_P == 4
1540                                                                 /* the constant must fit into a ptrint */
1541                                                                 if (iptr->sx.val.l < -0x80000000L || iptr->sx.val.l >= 0x80000000L)
1542                                                                         goto normal_LCONST;
1543 #endif
1544                                                                 /* XXX check field type? */
1545
1546                                                                 /* copy the constant to s2 */
1547                                                                 /* XXX constval -> fieldconstval? */
1548                                                                 iptr->sx.s23.s2.constval = iptr->sx.val.l;
1549
1550                                                                 goto putconst_tail;
1551
1552 #endif /* SUPPORT_CONST_STORE */
1553
1554                                                         default:
1555                                                                 goto normal_LCONST;
1556                                                 } /* end switch opcode after LCONST */
1557
1558                                                 /* if we get here, the LCONST has been optimized */
1559                                                 break;
1560
1561 normal_LCONST:
1562                                                 /* the normal case of an unoptimized LCONST */
1563                                                 OP0_1(TYPE_LNG);
1564                                                 break;
1565
1566         /************************ END OF LCONST OPTIMIZATIONS *********************/
1567
1568                                         case ICMD_FCONST:
1569                                                 COUNT(count_pcmd_load);
1570                                                 OP0_1(TYPE_FLT);
1571                                                 break;
1572
1573                                         case ICMD_DCONST:
1574                                                 COUNT(count_pcmd_load);
1575                                                 OP0_1(TYPE_DBL);
1576                                                 break;
1577
1578         /************************** ACONST OPTIMIZATIONS **************************/
1579
1580                                         case ICMD_ACONST:
1581                                                 coalescing_boundary = sd.new;
1582                                                 COUNT(count_pcmd_load);
1583 #if SUPPORT_CONST_STORE
1584                                                 IF_INTRP( goto normal_ACONST; )
1585
1586                                                 /* We can only optimize if the ACONST is resolved
1587                                                  * and there is an instruction after it. */
1588
1589                                                 if ((len == 0) || (iptr->flags.bits & INS_FLAG_UNRESOLVED))
1590                                                         goto normal_ACONST;
1591
1592                                                 switch (iptr[1].opc) {
1593                                                         case ICMD_AASTORE:
1594                                                                 /* We can only optimize for NULL values
1595                                                                  * here because otherwise a checkcast is
1596                                                                  * required. */
1597                                                                 if (iptr->sx.val.anyptr != NULL)
1598                                                                         goto normal_ACONST;
1599
1600                                                                 /* copy the constant (NULL) to s3 */
1601                                                                 iptr->sx.s23.s3.constval = 0;
1602                                                                 iptr->opc = ICMD_AASTORECONST;
1603                                                                 iptr->flags.bits |= INS_FLAG_CHECK;
1604                                                                 OP2_0(TYPE_ADR, TYPE_INT);
1605
1606                                                                 iptr[1].opc = ICMD_NOP;
1607                                                                 COUNT(count_pcmd_op);
1608                                                                 break;
1609
1610                                                         case ICMD_PUTSTATIC:
1611                                                         case ICMD_PUTFIELD:
1612 # if SUPPORT_CONST_STORE_ZERO_ONLY
1613                                                                 if (iptr->sx.val.anyptr != NULL)
1614                                                                         goto normal_ACONST;
1615 # endif
1616                                                                 /* XXX check field type? */
1617                                                                 /* copy the constant to s2 */
1618                                                                 /* XXX constval -> fieldconstval? */
1619                                                                 iptr->sx.s23.s2.constval = (ptrint) iptr->sx.val.anyptr;
1620
1621                                                                 goto putconst_tail;
1622
1623                                                         default:
1624                                                                 goto normal_ACONST;
1625                                                 }
1626
1627                                                 /* if we get here the ACONST has been optimized */
1628                                                 break;
1629
1630 normal_ACONST:
1631 #endif /* SUPPORT_CONST_STORE */
1632                                                 OP0_1(TYPE_ADR);
1633                                                 break;
1634
1635
1636                                                 /* pop 0 push 1 load */
1637
1638                                         case ICMD_ILOAD:
1639                                         case ICMD_LLOAD:
1640                                         case ICMD_FLOAD:
1641                                         case ICMD_DLOAD:
1642                                         case ICMD_ALOAD:
1643                                                 COUNT(count_load_instruction);
1644                                                 i = opcode - ICMD_ILOAD; /* type */
1645
1646                                                 iptr->s1.varindex = 
1647                                                         jd->local_map[iptr->s1.varindex * 5 + i];
1648                 
1649                                                 LOAD(i, iptr->s1.varindex);
1650                                                 break;
1651
1652                                                 /* pop 2 push 1 */
1653
1654                                         case ICMD_LALOAD:
1655                                         case ICMD_FALOAD:
1656                                         case ICMD_DALOAD:
1657                                         case ICMD_AALOAD:
1658                                                 coalescing_boundary = sd.new;
1659                                                 iptr->flags.bits |= INS_FLAG_CHECK;
1660                                                 COUNT(count_check_null);
1661                                                 COUNT(count_check_bound);
1662                                                 COUNT(count_pcmd_mem);
1663                                                 OP2_1(TYPE_ADR, TYPE_INT, opcode - ICMD_IALOAD);
1664                                                 break;
1665
1666                                         case ICMD_IALOAD:
1667                                         case ICMD_BALOAD:
1668                                         case ICMD_CALOAD:
1669                                         case ICMD_SALOAD:
1670                                                 coalescing_boundary = sd.new;
1671                                                 iptr->flags.bits |= INS_FLAG_CHECK;
1672                                                 COUNT(count_check_null);
1673                                                 COUNT(count_check_bound);
1674                                                 COUNT(count_pcmd_mem);
1675                                                 OP2_1(TYPE_ADR, TYPE_INT, TYPE_INT);
1676                                                 break;
1677
1678                                                 /* pop 0 push 0 iinc */
1679
1680                                         case ICMD_IINC:
1681                                                 STATISTICS_STACKDEPTH_DISTRIBUTION(count_store_depth);
1682
1683                                                 last_store_boundary[iptr->s1.varindex] = sd.new;
1684
1685                                                 iptr->s1.varindex = 
1686                                                         jd->local_map[iptr->s1.varindex * 5 + TYPE_INT];
1687
1688                                                 copy = curstack;
1689                                                 i = stackdepth - 1;
1690                                                 while (copy) {
1691                                                         if ((copy->varkind == LOCALVAR) &&
1692                                                                 (copy->varnum == iptr->s1.varindex))
1693                                                         {
1694                                                                 assert(IS_LOCALVAR(copy));
1695                                                                 SET_TEMPVAR(copy);
1696                                                         }
1697                                                         i--;
1698                                                         copy = copy->prev;
1699                                                 }
1700
1701                                                 iptr->dst.varindex = iptr->s1.varindex;
1702                                                 break;
1703
1704                                                 /* pop 1 push 0 store */
1705
1706                                         case ICMD_ISTORE:
1707                                         case ICMD_LSTORE:
1708                                         case ICMD_FSTORE:
1709                                         case ICMD_DSTORE:
1710                                         case ICMD_ASTORE:
1711                                                 REQUIRE(1);
1712
1713                                                 i = opcode - ICMD_ISTORE; /* type */
1714                                                 javaindex = iptr->dst.varindex;
1715                                                 j = iptr->dst.varindex = 
1716                                                         jd->local_map[javaindex * 5 + i];
1717
1718
1719 #if defined(ENABLE_STATISTICS)
1720                                                 if (opt_stat) {
1721                                                         count_pcmd_store++;
1722                                                         i = sd.new - curstack;
1723                                                         if (i >= 20)
1724                                                                 count_store_length[20]++;
1725                                                         else
1726                                                                 count_store_length[i]++;
1727                                                         i = stackdepth - 1;
1728                                                         if (i >= 10)
1729                                                                 count_store_depth[10]++;
1730                                                         else
1731                                                                 count_store_depth[i]++;
1732                                                 }
1733 #endif
1734                                                 /* check for conflicts as described in Figure 5.2 */
1735
1736                                                 copy = curstack->prev;
1737                                                 i = stackdepth - 2;
1738                                                 while (copy) {
1739                                                         if ((copy->varkind == LOCALVAR) &&
1740                                                                 (copy->varnum == j))
1741                                                         {
1742                                                                 copy->varkind = TEMPVAR;
1743                                                                 assert(IS_LOCALVAR(copy));
1744                                                                 SET_TEMPVAR(copy);
1745                                                         }
1746                                                         i--;
1747                                                         copy = copy->prev;
1748                                                 }
1749
1750                                                 /* if the variable is already coalesced, don't bother */
1751
1752                                                 if (IS_OUTVAR(curstack)
1753                                                         || (curstack->varkind == LOCALVAR 
1754                                                                 && curstack->varnum != j))
1755                                                         goto store_tail;
1756
1757                                                 /* there is no STORE Lj while curstack is live */
1758
1759                                                 if (curstack < last_store_boundary[javaindex])
1760                                                         goto assume_conflict;
1761
1762                                                 /* curstack must be after the coalescing boundary */
1763
1764                                                 if (curstack < coalescing_boundary)
1765                                                         goto assume_conflict;
1766
1767                                                 /* there is no DEF LOCALVAR(j) while curstack is live */
1768
1769                                                 copy = sd.new; /* most recent stackslot created + 1 */
1770                                                 while (--copy > curstack) {
1771                                                         if (copy->varkind == LOCALVAR && copy->varnum == j)
1772                                                                 goto assume_conflict;
1773                                                 }
1774
1775                                                 /* coalesce the temporary variable with Lj */
1776                                                 assert( (CURKIND == TEMPVAR) || (CURKIND == UNDEFVAR));
1777                                                 assert(!IS_LOCALVAR(curstack));
1778                                                 assert(!IS_OUTVAR(curstack));
1779                                                 assert(!IS_PREALLOC(curstack));
1780
1781                                                 assert(curstack->creator);
1782                                                 assert(curstack->creator->dst.varindex == curstack->varnum);
1783                                                 RELEASE_INDEX(sd, curstack);
1784                                                 curstack->varkind = LOCALVAR;
1785                                                 curstack->varnum = j;
1786                                                 curstack->creator->dst.varindex = j;
1787                                                 goto store_tail;
1788
1789                                                 /* revert the coalescing, if it has been done earlier */
1790 assume_conflict:
1791                                                 if ((curstack->varkind == LOCALVAR)
1792                                                         && (curstack->varnum == j))
1793                                                 {
1794                                                         assert(IS_LOCALVAR(curstack));
1795                                                         SET_TEMPVAR(curstack);
1796                                                 }
1797
1798                                                 /* remember the stack boundary at this store */
1799 store_tail:
1800                                                 last_store_boundary[javaindex] = sd.new;
1801
1802                                                 STORE(opcode - ICMD_ISTORE, j);
1803                                                 break;
1804
1805                                         /* pop 3 push 0 */
1806
1807                                         case ICMD_AASTORE:
1808                                                 coalescing_boundary = sd.new;
1809                                                 iptr->flags.bits |= INS_FLAG_CHECK;
1810                                                 COUNT(count_check_null);
1811                                                 COUNT(count_check_bound);
1812                                                 COUNT(count_pcmd_mem);
1813
1814                                                 bte = builtintable_get_internal(BUILTIN_canstore);
1815                                                 md = bte->md;
1816
1817                                                 if (md->memuse > rd->memuse)
1818                                                         rd->memuse = md->memuse;
1819                                                 if (md->argintreguse > rd->argintreguse)
1820                                                         rd->argintreguse = md->argintreguse;
1821                                                 /* XXX non-leaf method? */
1822
1823                                                 /* make all stack variables saved */
1824
1825                                                 copy = curstack;
1826                                                 while (copy) {
1827                                                         sd.var[copy->varnum].flags |= SAVEDVAR;
1828                                                         /* in case copy->varnum is/will be a LOCALVAR */
1829                                                         /* once and set back to a non LOCALVAR        */
1830                                                         /* the correct SAVEDVAR flag has to be        */
1831                                                         /* remembered in copy->flags, too             */
1832                                                         copy->flags |= SAVEDVAR;
1833                                                         copy = copy->prev;
1834                                                 }
1835
1836                                                 OP3_0(TYPE_ADR, TYPE_INT, TYPE_ADR);
1837                                                 break;
1838
1839
1840                                         case ICMD_LASTORE:
1841                                         case ICMD_FASTORE:
1842                                         case ICMD_DASTORE:
1843                                                 coalescing_boundary = sd.new;
1844                                                 iptr->flags.bits |= INS_FLAG_CHECK;
1845                                                 COUNT(count_check_null);
1846                                                 COUNT(count_check_bound);
1847                                                 COUNT(count_pcmd_mem);
1848                                                 OP3_0(TYPE_ADR, TYPE_INT, opcode - ICMD_IASTORE);
1849                                                 break;
1850
1851                                         case ICMD_IASTORE:
1852                                         case ICMD_BASTORE:
1853                                         case ICMD_CASTORE:
1854                                         case ICMD_SASTORE:
1855                                                 coalescing_boundary = sd.new;
1856                                                 iptr->flags.bits |= INS_FLAG_CHECK;
1857                                                 COUNT(count_check_null);
1858                                                 COUNT(count_check_bound);
1859                                                 COUNT(count_pcmd_mem);
1860                                                 OP3_0(TYPE_ADR, TYPE_INT, TYPE_INT);
1861                                                 break;
1862
1863                                                 /* pop 1 push 0 */
1864
1865                                         case ICMD_POP:
1866 #ifdef ENABLE_VERIFIER
1867                                                 if (opt_verify) {
1868                                                         REQUIRE(1);
1869                                                         if (IS_2_WORD_TYPE(curstack->type))
1870                                                                 goto throw_stack_category_error;
1871                                                 }
1872 #endif
1873                                                 OP1_0_ANY;
1874                                                 break;
1875
1876                                         case ICMD_IRETURN:
1877                                         case ICMD_LRETURN:
1878                                         case ICMD_FRETURN:
1879                                         case ICMD_DRETURN:
1880                                         case ICMD_ARETURN:
1881                                                 coalescing_boundary = sd.new;
1882                                                 IF_JIT( md_return_alloc(jd, curstack); )
1883                                                 COUNT(count_pcmd_return);
1884                                                 OP1_0(opcode - ICMD_IRETURN);
1885                                                 superblockend = true;
1886                                                 break;
1887
1888                                         case ICMD_ATHROW:
1889                                                 coalescing_boundary = sd.new;
1890                                                 COUNT(count_check_null);
1891                                                 OP1_0(TYPE_ADR);
1892                                                 curstack = NULL; stackdepth = 0;
1893                                                 superblockend = true;
1894                                                 break;
1895
1896                                         case ICMD_PUTSTATIC:
1897                                                 coalescing_boundary = sd.new;
1898                                                 COUNT(count_pcmd_mem);
1899                                                 INSTRUCTION_GET_FIELDREF(iptr, fmiref);
1900                                                 OP1_0(fmiref->parseddesc.fd->type);
1901                                                 break;
1902
1903                                                 /* pop 1 push 0 branch */
1904
1905                                         case ICMD_IFNULL:
1906                                         case ICMD_IFNONNULL:
1907                                                 COUNT(count_pcmd_bra);
1908                                                 OP1_BRANCH(TYPE_ADR);
1909                                                 BRANCH(tbptr, copy);
1910                                                 break;
1911
1912                                         case ICMD_IFEQ:
1913                                         case ICMD_IFNE:
1914                                         case ICMD_IFLT:
1915                                         case ICMD_IFGE:
1916                                         case ICMD_IFGT:
1917                                         case ICMD_IFLE:
1918                                                 COUNT(count_pcmd_bra);
1919                                                 /* iptr->sx.val.i is set implicitly in parse by
1920                                                    clearing the memory or from IF_ICMPxx
1921                                                    optimization. */
1922
1923                                                 OP1_BRANCH(TYPE_INT);
1924 /*                                              iptr->sx.val.i = 0; */
1925                                                 BRANCH(tbptr, copy);
1926                                                 break;
1927
1928                                                 /* pop 0 push 0 branch */
1929
1930                                         case ICMD_GOTO:
1931                                                 COUNT(count_pcmd_bra);
1932                                                 OP0_BRANCH;
1933                                                 BRANCH(tbptr, copy);
1934                                                 superblockend = true;
1935                                                 break;
1936
1937                                                 /* pop 1 push 0 table branch */
1938
1939                                         case ICMD_TABLESWITCH:
1940                                                 COUNT(count_pcmd_table);
1941                                                 OP1_BRANCH(TYPE_INT);
1942
1943                                                 table = iptr->dst.table;
1944                                                 BRANCH_TARGET(*table, tbptr, copy);
1945                                                 table++;
1946
1947                                                 i = iptr->sx.s23.s3.tablehigh
1948                                                   - iptr->sx.s23.s2.tablelow + 1;
1949
1950                                                 while (--i >= 0) {
1951                                                         BRANCH_TARGET(*table, tbptr, copy);
1952                                                         table++;
1953                                                 }
1954                                                 superblockend = true;
1955                                                 break;
1956
1957                                                 /* pop 1 push 0 table branch */
1958
1959                                         case ICMD_LOOKUPSWITCH:
1960                                                 COUNT(count_pcmd_table);
1961                                                 OP1_BRANCH(TYPE_INT);
1962
1963                                                 BRANCH_TARGET(iptr->sx.s23.s3.lookupdefault, tbptr, copy);
1964
1965                                                 lookup = iptr->dst.lookup;
1966
1967                                                 i = iptr->sx.s23.s2.lookupcount;
1968
1969                                                 while (--i >= 0) {
1970                                                         BRANCH_TARGET(lookup->target, tbptr, copy);
1971                                                         lookup++;
1972                                                 }
1973                                                 superblockend = true;
1974                                                 break;
1975
1976                                         case ICMD_MONITORENTER:
1977                                         case ICMD_MONITOREXIT:
1978                                                 coalescing_boundary = sd.new;
1979                                                 COUNT(count_check_null);
1980                                                 OP1_0(TYPE_ADR);
1981                                                 break;
1982
1983                                                 /* pop 2 push 0 branch */
1984
1985                                         case ICMD_IF_ICMPEQ:
1986                                         case ICMD_IF_ICMPNE:
1987                                         case ICMD_IF_ICMPLT:
1988                                         case ICMD_IF_ICMPGE:
1989                                         case ICMD_IF_ICMPGT:
1990                                         case ICMD_IF_ICMPLE:
1991                                                 COUNT(count_pcmd_bra);
1992                                                 OP2_BRANCH(TYPE_INT, TYPE_INT);
1993                                                 BRANCH(tbptr, copy);
1994                                                 break;
1995
1996                                         case ICMD_IF_ACMPEQ:
1997                                         case ICMD_IF_ACMPNE:
1998                                                 COUNT(count_pcmd_bra);
1999                                                 OP2_BRANCH(TYPE_ADR, TYPE_ADR);
2000                                                 BRANCH(tbptr, copy);
2001                                                 break;
2002
2003                                                 /* pop 2 push 0 */
2004
2005                                         case ICMD_PUTFIELD:
2006                                                 coalescing_boundary = sd.new;
2007                                                 COUNT(count_check_null);
2008                                                 COUNT(count_pcmd_mem);
2009                                                 INSTRUCTION_GET_FIELDREF(iptr, fmiref);
2010                                                 OP2_0(TYPE_ADR, fmiref->parseddesc.fd->type);
2011                                                 break;
2012
2013                                         case ICMD_POP2:
2014                                                 REQUIRE(1);
2015                                                 if (!IS_2_WORD_TYPE(curstack->type)) {
2016                                                         /* ..., cat1 */
2017 #ifdef ENABLE_VERIFIER
2018                                                         if (opt_verify) {
2019                                                                 REQUIRE(2);
2020                                                                 if (IS_2_WORD_TYPE(curstack->prev->type))
2021                                                                         goto throw_stack_category_error;
2022                                                         }
2023 #endif
2024                                                         OP2_0_ANY_ANY; /* pop two slots */
2025                                                 }
2026                                                 else {
2027                                                         iptr->opc = ICMD_POP;
2028                                                         OP1_0_ANY; /* pop one (two-word) slot */
2029                                                 }
2030                                                 break;
2031
2032                                                 /* pop 0 push 1 dup */
2033
2034                                         case ICMD_DUP:
2035 #ifdef ENABLE_VERIFIER
2036                                                 if (opt_verify) {
2037                                                         REQUIRE(1);
2038                                                         if (IS_2_WORD_TYPE(curstack->type))
2039                                                                 goto throw_stack_category_error;
2040                                                 }
2041 #endif
2042                                                 COUNT(count_dup_instruction);
2043
2044 icmd_DUP:
2045                                                 src1 = curstack;
2046
2047                                                 COPY_UP(src1);
2048                                                 coalescing_boundary = sd.new - 1;
2049                                                 break;
2050
2051                                         case ICMD_DUP2:
2052                                                 REQUIRE(1);
2053                                                 if (IS_2_WORD_TYPE(curstack->type)) {
2054                                                         /* ..., cat2 */
2055                                                         iptr->opc = ICMD_DUP;
2056                                                         goto icmd_DUP;
2057                                                 }
2058                                                 else {
2059                                                         REQUIRE(2);
2060                                                         /* ..., ????, cat1 */
2061 #ifdef ENABLE_VERIFIER
2062                                                         if (opt_verify) {
2063                                                                 if (IS_2_WORD_TYPE(curstack->prev->type))
2064                                                                         goto throw_stack_category_error;
2065                                                         }
2066 #endif
2067                                                         src1 = curstack->prev;
2068                                                         src2 = curstack;
2069
2070                                                         COPY_UP(src1); iptr++; len--;
2071                                                         COPY_UP(src2);
2072
2073                                                         coalescing_boundary = sd.new;
2074                                                 }
2075                                                 break;
2076
2077                                                 /* pop 2 push 3 dup */
2078
2079                                         case ICMD_DUP_X1:
2080 #ifdef ENABLE_VERIFIER
2081                                                 if (opt_verify) {
2082                                                         REQUIRE(2);
2083                                                         if (IS_2_WORD_TYPE(curstack->type) ||
2084                                                                 IS_2_WORD_TYPE(curstack->prev->type))
2085                                                                         goto throw_stack_category_error;
2086                                                 }
2087 #endif
2088
2089 icmd_DUP_X1:
2090                                                 src1 = curstack->prev;
2091                                                 src2 = curstack;
2092                                                 POPANY; POPANY;
2093                                                 stackdepth -= 2;
2094
2095                                                 DUP_SLOT(src2); dst1 = curstack; stackdepth++;
2096
2097                                                 MOVE_UP(src1); iptr++; len--;
2098                                                 MOVE_UP(src2); iptr++; len--;
2099
2100                                                 COPY_DOWN(curstack, dst1);
2101
2102                                                 coalescing_boundary = sd.new;
2103                                                 break;
2104
2105                                         case ICMD_DUP2_X1:
2106                                                 REQUIRE(2);
2107                                                 if (IS_2_WORD_TYPE(curstack->type)) {
2108                                                         /* ..., ????, cat2 */
2109 #ifdef ENABLE_VERIFIER
2110                                                         if (opt_verify) {
2111                                                                 if (IS_2_WORD_TYPE(curstack->prev->type))
2112                                                                         goto throw_stack_category_error;
2113                                                         }
2114 #endif
2115                                                         iptr->opc = ICMD_DUP_X1;
2116                                                         goto icmd_DUP_X1;
2117                                                 }
2118                                                 else {
2119                                                         /* ..., ????, cat1 */
2120 #ifdef ENABLE_VERIFIER
2121                                                         if (opt_verify) {
2122                                                                 REQUIRE(3);
2123                                                                 if (IS_2_WORD_TYPE(curstack->prev->type)
2124                                                                         || IS_2_WORD_TYPE(curstack->prev->prev->type))
2125                                                                                 goto throw_stack_category_error;
2126                                                         }
2127 #endif
2128
2129 icmd_DUP2_X1:
2130                                                         src1 = curstack->prev->prev;
2131                                                         src2 = curstack->prev;
2132                                                         src3 = curstack;
2133                                                         POPANY; POPANY; POPANY;
2134                                                         stackdepth -= 3;
2135
2136                                                         DUP_SLOT(src2); dst1 = curstack; stackdepth++;
2137                                                         DUP_SLOT(src3); dst2 = curstack; stackdepth++;
2138
2139                                                         MOVE_UP(src1); iptr++; len--;
2140                                                         MOVE_UP(src2); iptr++; len--;
2141                                                         MOVE_UP(src3); iptr++; len--;
2142
2143                                                         COPY_DOWN(curstack, dst2); iptr++; len--;
2144                                                         COPY_DOWN(curstack->prev, dst1);
2145
2146                                                         coalescing_boundary = sd.new;
2147                                                 }
2148                                                 break;
2149
2150                                                 /* pop 3 push 4 dup */
2151
2152                                         case ICMD_DUP_X2:
2153                                                 REQUIRE(2);
2154                                                 if (IS_2_WORD_TYPE(curstack->prev->type)) {
2155                                                         /* ..., cat2, ???? */
2156 #ifdef ENABLE_VERIFIER
2157                                                         if (opt_verify) {
2158                                                                 if (IS_2_WORD_TYPE(curstack->type))
2159                                                                         goto throw_stack_category_error;
2160                                                         }
2161 #endif
2162                                                         iptr->opc = ICMD_DUP_X1;
2163                                                         goto icmd_DUP_X1;
2164                                                 }
2165                                                 else {
2166                                                         /* ..., cat1, ???? */
2167 #ifdef ENABLE_VERIFIER
2168                                                         if (opt_verify) {
2169                                                                 REQUIRE(3);
2170                                                                 if (IS_2_WORD_TYPE(curstack->type)
2171                                                                         || IS_2_WORD_TYPE(curstack->prev->prev->type))
2172                                                                                         goto throw_stack_category_error;
2173                                                         }
2174 #endif
2175 icmd_DUP_X2:
2176                                                         src1 = curstack->prev->prev;
2177                                                         src2 = curstack->prev;
2178                                                         src3 = curstack;
2179                                                         POPANY; POPANY; POPANY;
2180                                                         stackdepth -= 3;
2181
2182                                                         DUP_SLOT(src3); dst1 = curstack; stackdepth++;
2183
2184                                                         MOVE_UP(src1); iptr++; len--;
2185                                                         MOVE_UP(src2); iptr++; len--;
2186                                                         MOVE_UP(src3); iptr++; len--;
2187
2188                                                         COPY_DOWN(curstack, dst1);
2189
2190                                                         coalescing_boundary = sd.new;
2191                                                 }
2192                                                 break;
2193
2194                                         case ICMD_DUP2_X2:
2195                                                 REQUIRE(2);
2196                                                 if (IS_2_WORD_TYPE(curstack->type)) {
2197                                                         /* ..., ????, cat2 */
2198                                                         if (IS_2_WORD_TYPE(curstack->prev->type)) {
2199                                                                 /* ..., cat2, cat2 */
2200                                                                 iptr->opc = ICMD_DUP_X1;
2201                                                                 goto icmd_DUP_X1;
2202                                                         }
2203                                                         else {
2204                                                                 /* ..., cat1, cat2 */
2205 #ifdef ENABLE_VERIFIER
2206                                                                 if (opt_verify) {
2207                                                                         REQUIRE(3);
2208                                                                         if (IS_2_WORD_TYPE(curstack->prev->prev->type))
2209                                                                                         goto throw_stack_category_error;
2210                                                                 }
2211 #endif
2212                                                                 iptr->opc = ICMD_DUP_X2;
2213                                                                 goto icmd_DUP_X2;
2214                                                         }
2215                                                 }
2216
2217                                                 REQUIRE(3);
2218                                                 /* ..., ????, ????, cat1 */
2219
2220                                                 if (IS_2_WORD_TYPE(curstack->prev->prev->type)) {
2221                                                         /* ..., cat2, ????, cat1 */
2222 #ifdef ENABLE_VERIFIER
2223                                                         if (opt_verify) {
2224                                                                 if (IS_2_WORD_TYPE(curstack->prev->type))
2225                                                                         goto throw_stack_category_error;
2226                                                         }
2227 #endif
2228                                                         iptr->opc = ICMD_DUP2_X1;
2229                                                         goto icmd_DUP2_X1;
2230                                                 }
2231                                                 else {
2232                                                         /* ..., cat1, ????, cat1 */
2233 #ifdef ENABLE_VERIFIER
2234                                                         if (opt_verify) {
2235                                                                 REQUIRE(4);
2236                                                                 if (IS_2_WORD_TYPE(curstack->prev->type)
2237                                                                         || IS_2_WORD_TYPE(curstack->prev->prev->prev->type))
2238                                                                         goto throw_stack_category_error;
2239                                                         }
2240 #endif
2241
2242                                                         src1 = curstack->prev->prev->prev;
2243                                                         src2 = curstack->prev->prev;
2244                                                         src3 = curstack->prev;
2245                                                         src4 = curstack;
2246                                                         POPANY; POPANY; POPANY; POPANY;
2247                                                         stackdepth -= 4;
2248
2249                                                         DUP_SLOT(src3); dst1 = curstack; stackdepth++;
2250                                                         DUP_SLOT(src4); dst2 = curstack; stackdepth++;
2251
2252                                                         MOVE_UP(src1); iptr++; len--;
2253                                                         MOVE_UP(src2); iptr++; len--;
2254                                                         MOVE_UP(src3); iptr++; len--;
2255                                                         MOVE_UP(src4); iptr++; len--;
2256
2257                                                         COPY_DOWN(curstack, dst2); iptr++; len--;
2258                                                         COPY_DOWN(curstack->prev, dst1);
2259
2260                                                         coalescing_boundary = sd.new;
2261                                                 }
2262                                                 break;
2263
2264                                                 /* pop 2 push 2 swap */
2265
2266                                         case ICMD_SWAP:
2267 #ifdef ENABLE_VERIFIER
2268                                                 if (opt_verify) {
2269                                                         REQUIRE(2);
2270                                                         if (IS_2_WORD_TYPE(curstack->type)
2271                                                                 || IS_2_WORD_TYPE(curstack->prev->type))
2272                                                                 goto throw_stack_category_error;
2273                                                 }
2274 #endif
2275
2276                                                 src1 = curstack->prev;
2277                                                 src2 = curstack;
2278                                                 POPANY; POPANY;
2279                                                 stackdepth -= 2;
2280
2281                                                 MOVE_UP(src2); iptr++; len--;
2282                                                 MOVE_UP(src1);
2283
2284                                                 coalescing_boundary = sd.new;
2285                                                 break;
2286
2287                                                 /* pop 2 push 1 */
2288
2289                                         case ICMD_IDIV:
2290                                         case ICMD_IREM:
2291                                                 coalescing_boundary = sd.new;
2292 #if !SUPPORT_DIVISION
2293                                                 bte = iptr->sx.s23.s3.bte;
2294                                                 md = bte->md;
2295
2296                                                 if (md->memuse > rd->memuse)
2297                                                         rd->memuse = md->memuse;
2298                                                 if (md->argintreguse > rd->argintreguse)
2299                                                         rd->argintreguse = md->argintreguse;
2300
2301                                                 /* make all stack variables saved */
2302
2303                                                 copy = curstack;
2304                                                 while (copy) {
2305                                                         sd.var[copy->varnum].flags |= SAVEDVAR;
2306                                                         copy->flags |= SAVEDVAR;
2307                                                         copy = copy->prev;
2308                                                 }
2309                                                 /* FALLTHROUGH */
2310
2311 #endif /* !SUPPORT_DIVISION */
2312
2313                                         case ICMD_ISHL:
2314                                         case ICMD_ISHR:
2315                                         case ICMD_IUSHR:
2316                                         case ICMD_IADD:
2317                                         case ICMD_ISUB:
2318                                         case ICMD_IMUL:
2319                                         case ICMD_IAND:
2320                                         case ICMD_IOR:
2321                                         case ICMD_IXOR:
2322                                                 COUNT(count_pcmd_op);
2323                                                 OP2_1(TYPE_INT, TYPE_INT, TYPE_INT);
2324                                                 break;
2325
2326                                         case ICMD_LDIV:
2327                                         case ICMD_LREM:
2328                                                 coalescing_boundary = sd.new;
2329 #if !(SUPPORT_DIVISION && SUPPORT_LONG && SUPPORT_LONG_DIV)
2330                                                 bte = iptr->sx.s23.s3.bte;
2331                                                 md = bte->md;
2332
2333                                                 if (md->memuse > rd->memuse)
2334                                                         rd->memuse = md->memuse;
2335                                                 if (md->argintreguse > rd->argintreguse)
2336                                                         rd->argintreguse = md->argintreguse;
2337                                                 /* XXX non-leaf method? */
2338
2339                                                 /* make all stack variables saved */
2340
2341                                                 copy = curstack;
2342                                                 while (copy) {
2343                                                         sd.var[copy->varnum].flags |= SAVEDVAR;
2344                                                         copy->flags |= SAVEDVAR;
2345                                                         copy = copy->prev;
2346                                                 }
2347                                                 /* FALLTHROUGH */
2348
2349 #endif /* !(SUPPORT_DIVISION && SUPPORT_LONG && SUPPORT_LONG_DIV) */
2350
2351                                         case ICMD_LMUL:
2352                                         case ICMD_LADD:
2353                                         case ICMD_LSUB:
2354 #if SUPPORT_LONG_LOGICAL
2355                                         case ICMD_LAND:
2356                                         case ICMD_LOR:
2357                                         case ICMD_LXOR:
2358 #endif /* SUPPORT_LONG_LOGICAL */
2359                                                 COUNT(count_pcmd_op);
2360                                                 OP2_1(TYPE_LNG, TYPE_LNG, TYPE_LNG);
2361                                                 break;
2362
2363                                         case ICMD_LSHL:
2364                                         case ICMD_LSHR:
2365                                         case ICMD_LUSHR:
2366                                                 COUNT(count_pcmd_op);
2367                                                 OP2_1(TYPE_LNG, TYPE_INT, TYPE_LNG);
2368                                                 break;
2369
2370                                         case ICMD_FADD:
2371                                         case ICMD_FSUB:
2372                                         case ICMD_FMUL:
2373                                         case ICMD_FDIV:
2374                                         case ICMD_FREM:
2375                                                 COUNT(count_pcmd_op);
2376                                                 OP2_1(TYPE_FLT, TYPE_FLT, TYPE_FLT);
2377                                                 break;
2378
2379                                         case ICMD_DADD:
2380                                         case ICMD_DSUB:
2381                                         case ICMD_DMUL:
2382                                         case ICMD_DDIV:
2383                                         case ICMD_DREM:
2384                                                 COUNT(count_pcmd_op);
2385                                                 OP2_1(TYPE_DBL, TYPE_DBL, TYPE_DBL);
2386                                                 break;
2387
2388                                         case ICMD_LCMP:
2389                                                 COUNT(count_pcmd_op);
2390 #if SUPPORT_LONG_CMP_CONST
2391                                                 if ((len == 0) || (iptr[1].sx.val.i != 0))
2392                                                         goto normal_LCMP;
2393
2394                                                 switch (iptr[1].opc) {
2395                                                 case ICMD_IFEQ:
2396                                                         iptr->opc = ICMD_IF_LCMPEQ;
2397                                                 icmd_lcmp_if_tail:
2398                                                         iptr->dst.insindex = iptr[1].dst.insindex;
2399                                                         iptr[1].opc = ICMD_NOP;
2400
2401                                                         OP2_BRANCH(TYPE_LNG, TYPE_LNG);
2402                                                         BRANCH(tbptr, copy);
2403
2404                                                         COUNT(count_pcmd_bra);
2405                                                         break;
2406                                                 case ICMD_IFNE:
2407                                                         iptr->opc = ICMD_IF_LCMPNE;
2408                                                         goto icmd_lcmp_if_tail;
2409                                                 case ICMD_IFLT:
2410                                                         iptr->opc = ICMD_IF_LCMPLT;
2411                                                         goto icmd_lcmp_if_tail;
2412                                                 case ICMD_IFGT:
2413                                                         iptr->opc = ICMD_IF_LCMPGT;
2414                                                         goto icmd_lcmp_if_tail;
2415                                                 case ICMD_IFLE:
2416                                                         iptr->opc = ICMD_IF_LCMPLE;
2417                                                         goto icmd_lcmp_if_tail;
2418                                                 case ICMD_IFGE:
2419                                                         iptr->opc = ICMD_IF_LCMPGE;
2420                                                         goto icmd_lcmp_if_tail;
2421                                                 default:
2422                                                         goto normal_LCMP;
2423                                                 }
2424                                                 break;
2425 normal_LCMP:
2426 #endif /* SUPPORT_LONG_CMP_CONST */
2427                                                         OP2_1(TYPE_LNG, TYPE_LNG, TYPE_INT);
2428                                                 break;
2429
2430                                                 /* XXX why is this deactivated? */
2431 #if 0
2432                                         case ICMD_FCMPL:
2433                                                 COUNT(count_pcmd_op);
2434                                                 if ((len == 0) || (iptr[1].sx.val.i != 0))
2435                                                         goto normal_FCMPL;
2436
2437                                                 switch (iptr[1].opc) {
2438                                                 case ICMD_IFEQ:
2439                                                         iptr->opc = ICMD_IF_FCMPEQ;
2440                                                 icmd_if_fcmpl_tail:
2441                                                         iptr->dst.insindex = iptr[1].dst.insindex;
2442                                                         iptr[1].opc = ICMD_NOP;
2443
2444                                                         OP2_BRANCH(TYPE_FLT, TYPE_FLT);
2445                                                         BRANCH(tbptr, copy);
2446
2447                                                         COUNT(count_pcmd_bra);
2448                                                         break;
2449                                                 case ICMD_IFNE:
2450                                                         iptr->opc = ICMD_IF_FCMPNE;
2451                                                         goto icmd_if_fcmpl_tail;
2452                                                 case ICMD_IFLT:
2453                                                         iptr->opc = ICMD_IF_FCMPL_LT;
2454                                                         goto icmd_if_fcmpl_tail;
2455                                                 case ICMD_IFGT:
2456                                                         iptr->opc = ICMD_IF_FCMPL_GT;
2457                                                         goto icmd_if_fcmpl_tail;
2458                                                 case ICMD_IFLE:
2459                                                         iptr->opc = ICMD_IF_FCMPL_LE;
2460                                                         goto icmd_if_fcmpl_tail;
2461                                                 case ICMD_IFGE:
2462                                                         iptr->opc = ICMD_IF_FCMPL_GE;
2463                                                         goto icmd_if_fcmpl_tail;
2464                                                 default:
2465                                                         goto normal_FCMPL;
2466                                                 }
2467                                                 break;
2468
2469 normal_FCMPL:
2470                                                 OPTT2_1(TYPE_FLT, TYPE_FLT, TYPE_INT);
2471                                                 break;
2472
2473                                         case ICMD_FCMPG:
2474                                                 COUNT(count_pcmd_op);
2475                                                 if ((len == 0) || (iptr[1].sx.val.i != 0))
2476                                                         goto normal_FCMPG;
2477
2478                                                 switch (iptr[1].opc) {
2479                                                 case ICMD_IFEQ:
2480                                                         iptr->opc = ICMD_IF_FCMPEQ;
2481                                                 icmd_if_fcmpg_tail:
2482                                                         iptr->dst.insindex = iptr[1].dst.insindex;
2483                                                         iptr[1].opc = ICMD_NOP;
2484
2485                                                         OP2_BRANCH(TYPE_FLT, TYPE_FLT);
2486                                                         BRANCH(tbptr, copy);
2487
2488                                                         COUNT(count_pcmd_bra);
2489                                                         break;
2490                                                 case ICMD_IFNE:
2491                                                         iptr->opc = ICMD_IF_FCMPNE;
2492                                                         goto icmd_if_fcmpg_tail;
2493                                                 case ICMD_IFLT:
2494                                                         iptr->opc = ICMD_IF_FCMPG_LT;
2495                                                         goto icmd_if_fcmpg_tail;
2496                                                 case ICMD_IFGT:
2497                                                         iptr->opc = ICMD_IF_FCMPG_GT;
2498                                                         goto icmd_if_fcmpg_tail;
2499                                                 case ICMD_IFLE:
2500                                                         iptr->opc = ICMD_IF_FCMPG_LE;
2501                                                         goto icmd_if_fcmpg_tail;
2502                                                 case ICMD_IFGE:
2503                                                         iptr->opc = ICMD_IF_FCMPG_GE;
2504                                                         goto icmd_if_fcmpg_tail;
2505                                                 default:
2506                                                         goto normal_FCMPG;
2507                                                 }
2508                                                 break;
2509
2510 normal_FCMPG:
2511                                                 OP2_1(TYPE_FLT, TYPE_FLT, TYPE_INT);
2512                                                 break;
2513
2514                                         case ICMD_DCMPL:
2515                                                 COUNT(count_pcmd_op);
2516                                                 if ((len == 0) || (iptr[1].sx.val.i != 0))
2517                                                         goto normal_DCMPL;
2518
2519                                                 switch (iptr[1].opc) {
2520                                                 case ICMD_IFEQ:
2521                                                         iptr->opc = ICMD_IF_DCMPEQ;
2522                                                 icmd_if_dcmpl_tail:
2523                                                         iptr->dst.insindex = iptr[1].dst.insindex;
2524                                                         iptr[1].opc = ICMD_NOP;
2525
2526                                                         OP2_BRANCH(TYPE_DBL, TYPE_DBL);
2527                                                         BRANCH(tbptr, copy);
2528
2529                                                         COUNT(count_pcmd_bra);
2530                                                         break;
2531                                                 case ICMD_IFNE:
2532                                                         iptr->opc = ICMD_IF_DCMPNE;
2533                                                         goto icmd_if_dcmpl_tail;
2534                                                 case ICMD_IFLT:
2535                                                         iptr->opc = ICMD_IF_DCMPL_LT;
2536                                                         goto icmd_if_dcmpl_tail;
2537                                                 case ICMD_IFGT:
2538                                                         iptr->opc = ICMD_IF_DCMPL_GT;
2539                                                         goto icmd_if_dcmpl_tail;
2540                                                 case ICMD_IFLE:
2541                                                         iptr->opc = ICMD_IF_DCMPL_LE;
2542                                                         goto icmd_if_dcmpl_tail;
2543                                                 case ICMD_IFGE:
2544                                                         iptr->opc = ICMD_IF_DCMPL_GE;
2545                                                         goto icmd_if_dcmpl_tail;
2546                                                 default:
2547                                                         goto normal_DCMPL;
2548                                                 }
2549                                                 break;
2550
2551 normal_DCMPL:
2552                                                 OPTT2_1(TYPE_DBL, TYPE_INT);
2553                                                 break;
2554
2555                                         case ICMD_DCMPG:
2556                                                 COUNT(count_pcmd_op);
2557                                                 if ((len == 0) || (iptr[1].sx.val.i != 0))
2558                                                         goto normal_DCMPG;
2559
2560                                                 switch (iptr[1].opc) {
2561                                                 case ICMD_IFEQ:
2562                                                         iptr->opc = ICMD_IF_DCMPEQ;
2563                                                 icmd_if_dcmpg_tail:
2564                                                         iptr->dst.insindex = iptr[1].dst.insindex;
2565                                                         iptr[1].opc = ICMD_NOP;
2566
2567                                                         OP2_BRANCH(TYPE_DBL, TYPE_DBL);
2568                                                         BRANCH(tbptr, copy);
2569
2570                                                         COUNT(count_pcmd_bra);
2571                                                         break;
2572                                                 case ICMD_IFNE:
2573                                                         iptr->opc = ICMD_IF_DCMPNE;
2574                                                         goto icmd_if_dcmpg_tail;
2575                                                 case ICMD_IFLT:
2576                                                         iptr->opc = ICMD_IF_DCMPG_LT;
2577                                                         goto icmd_if_dcmpg_tail;
2578                                                 case ICMD_IFGT:
2579                                                         iptr->opc = ICMD_IF_DCMPG_GT;
2580                                                         goto icmd_if_dcmpg_tail;
2581                                                 case ICMD_IFLE:
2582                                                         iptr->opc = ICMD_IF_DCMPG_LE;
2583                                                         goto icmd_if_dcmpg_tail;
2584                                                 case ICMD_IFGE:
2585                                                         iptr->opc = ICMD_IF_DCMPG_GE;
2586                                                         goto icmd_if_dcmpg_tail;
2587                                                 default:
2588                                                         goto normal_DCMPG;
2589                                                 }
2590                                                 break;
2591
2592 normal_DCMPG:
2593                                                 OP2_1(TYPE_DBL, TYPE_DBL, TYPE_INT);
2594                                                 break;
2595 #else
2596                                         case ICMD_FCMPL:
2597                                         case ICMD_FCMPG:
2598                                                 COUNT(count_pcmd_op);
2599                                                 OP2_1(TYPE_FLT, TYPE_FLT, TYPE_INT);
2600                                                 break;
2601
2602                                         case ICMD_DCMPL:
2603                                         case ICMD_DCMPG:
2604                                                 COUNT(count_pcmd_op);
2605                                                 OP2_1(TYPE_DBL, TYPE_DBL, TYPE_INT);
2606                                                 break;
2607 #endif
2608
2609                                                 /* pop 1 push 1 */
2610
2611                                         case ICMD_INEG:
2612                                         case ICMD_INT2BYTE:
2613                                         case ICMD_INT2CHAR:
2614                                         case ICMD_INT2SHORT:
2615                                                 COUNT(count_pcmd_op);
2616                                                 OP1_1(TYPE_INT, TYPE_INT);
2617                                                 break;
2618                                         case ICMD_LNEG:
2619                                                 COUNT(count_pcmd_op);
2620                                                 OP1_1(TYPE_LNG, TYPE_LNG);
2621                                                 break;
2622                                         case ICMD_FNEG:
2623                                                 COUNT(count_pcmd_op);
2624                                                 OP1_1(TYPE_FLT, TYPE_FLT);
2625                                                 break;
2626                                         case ICMD_DNEG:
2627                                                 COUNT(count_pcmd_op);
2628                                                 OP1_1(TYPE_DBL, TYPE_DBL);
2629                                                 break;
2630
2631                                         case ICMD_I2L:
2632                                                 COUNT(count_pcmd_op);
2633                                                 OP1_1(TYPE_INT, TYPE_LNG);
2634                                                 break;
2635                                         case ICMD_I2F:
2636                                                 COUNT(count_pcmd_op);
2637                                                 OP1_1(TYPE_INT, TYPE_FLT);
2638                                                 break;
2639                                         case ICMD_I2D:
2640                                                 COUNT(count_pcmd_op);
2641                                                 OP1_1(TYPE_INT, TYPE_DBL);
2642                                                 break;
2643                                         case ICMD_L2I:
2644                                                 COUNT(count_pcmd_op);
2645                                                 OP1_1(TYPE_LNG, TYPE_INT);
2646                                                 break;
2647                                         case ICMD_L2F:
2648                                                 COUNT(count_pcmd_op);
2649                                                 OP1_1(TYPE_LNG, TYPE_FLT);
2650                                                 break;
2651                                         case ICMD_L2D:
2652                                                 COUNT(count_pcmd_op);
2653                                                 OP1_1(TYPE_LNG, TYPE_DBL);
2654                                                 break;
2655                                         case ICMD_F2I:
2656                                                 COUNT(count_pcmd_op);
2657                                                 OP1_1(TYPE_FLT, TYPE_INT);
2658                                                 break;
2659                                         case ICMD_F2L:
2660                                                 COUNT(count_pcmd_op);
2661                                                 OP1_1(TYPE_FLT, TYPE_LNG);
2662                                                 break;
2663                                         case ICMD_F2D:
2664                                                 COUNT(count_pcmd_op);
2665                                                 OP1_1(TYPE_FLT, TYPE_DBL);
2666                                                 break;
2667                                         case ICMD_D2I:
2668                                                 COUNT(count_pcmd_op);
2669                                                 OP1_1(TYPE_DBL, TYPE_INT);
2670                                                 break;
2671                                         case ICMD_D2L:
2672                                                 COUNT(count_pcmd_op);
2673                                                 OP1_1(TYPE_DBL, TYPE_LNG);
2674                                                 break;
2675                                         case ICMD_D2F:
2676                                                 COUNT(count_pcmd_op);
2677                                                 OP1_1(TYPE_DBL, TYPE_FLT);
2678                                                 break;
2679
2680                                         case ICMD_CHECKCAST:
2681                                                 coalescing_boundary = sd.new;
2682                                                 if (iptr->flags.bits & INS_FLAG_ARRAY) {
2683                                                         /* array type cast-check */
2684
2685                                                         bte = builtintable_get_internal(BUILTIN_arraycheckcast);
2686                                                         md = bte->md;
2687
2688                                                         if (md->memuse > rd->memuse)
2689                                                                 rd->memuse = md->memuse;
2690                                                         if (md->argintreguse > rd->argintreguse)
2691                                                                 rd->argintreguse = md->argintreguse;
2692
2693                                                         /* make all stack variables saved */
2694
2695                                                         copy = curstack;
2696                                                         while (copy) {
2697                                                                 sd.var[copy->varnum].flags |= SAVEDVAR;
2698                                                                 copy->flags |= SAVEDVAR;
2699                                                                 copy = copy->prev;
2700                                                         }
2701                                                 }
2702                                                 OP1_1(TYPE_ADR, TYPE_ADR);
2703                                                 break;
2704
2705                                         case ICMD_INSTANCEOF:
2706                                         case ICMD_ARRAYLENGTH:
2707                                                 coalescing_boundary = sd.new;
2708                                                 OP1_1(TYPE_ADR, TYPE_INT);
2709                                                 break;
2710
2711                                         case ICMD_NEWARRAY:
2712                                         case ICMD_ANEWARRAY:
2713                                                 coalescing_boundary = sd.new;
2714                                                 OP1_1(TYPE_INT, TYPE_ADR);
2715                                                 break;
2716
2717                                         case ICMD_GETFIELD:
2718                                                 coalescing_boundary = sd.new;
2719                                                 COUNT(count_check_null);
2720                                                 COUNT(count_pcmd_mem);
2721                                                 INSTRUCTION_GET_FIELDREF(iptr, fmiref);
2722                                                 OP1_1(TYPE_ADR, fmiref->parseddesc.fd->type);
2723                                                 break;
2724
2725                                                 /* pop 0 push 1 */
2726
2727                                         case ICMD_GETSTATIC:
2728                                                 coalescing_boundary = sd.new;
2729                                                 COUNT(count_pcmd_mem);
2730                                                 INSTRUCTION_GET_FIELDREF(iptr, fmiref);
2731                                                 OP0_1(fmiref->parseddesc.fd->type);
2732                                                 break;
2733
2734                                         case ICMD_NEW:
2735                                                 coalescing_boundary = sd.new;
2736                                                 OP0_1(TYPE_ADR);
2737                                                 break;
2738
2739                                         case ICMD_JSR:
2740                                                 OP0_1(TYPE_ADR);
2741
2742                                                 BRANCH_TARGET(iptr->sx.s23.s3.jsrtarget, tbptr, copy);
2743
2744                                                 tbptr->type = BBTYPE_SBR;
2745
2746                                                 /* We need to check for overflow right here because
2747                                                  * the pushed value is poped afterwards */
2748                                                 CHECKOVERFLOW;
2749
2750                                                 /* calculate stack after return */
2751                                                 POPANY;
2752                                                 stackdepth--;
2753                                                 break;
2754
2755                                         /* pop many push any */
2756
2757                                         case ICMD_BUILTIN:
2758 icmd_BUILTIN:
2759                                                 bte = iptr->sx.s23.s3.bte;
2760                                                 md = bte->md;
2761                                                 goto _callhandling;
2762
2763                                         case ICMD_INVOKESTATIC:
2764                                         case ICMD_INVOKESPECIAL:
2765                                         case ICMD_INVOKEVIRTUAL:
2766                                         case ICMD_INVOKEINTERFACE:
2767                                                 COUNT(count_pcmd_met);
2768
2769                                                 /* Check for functions to replace with builtin
2770                                                  * functions. */
2771
2772                                                 if (builtintable_replace_function(iptr))
2773                                                         goto icmd_BUILTIN;
2774
2775                                                 INSTRUCTION_GET_METHODDESC(iptr, md);
2776                                                 /* XXX resurrect this COUNT? */
2777 /*                          if (lm->flags & ACC_STATIC) */
2778 /*                              {COUNT(count_check_null);} */
2779
2780                                         _callhandling:
2781
2782                                                 coalescing_boundary = sd.new;
2783
2784                                                 i = md->paramcount;
2785
2786                                                 if (md->memuse > rd->memuse)
2787                                                         rd->memuse = md->memuse;
2788                                                 if (md->argintreguse > rd->argintreguse)
2789                                                         rd->argintreguse = md->argintreguse;
2790                                                 if (md->argfltreguse > rd->argfltreguse)
2791                                                         rd->argfltreguse = md->argfltreguse;
2792
2793                                                 REQUIRE(i);
2794
2795                                                 /* XXX optimize for <= 2 args */
2796                                                 /* XXX not for ICMD_BUILTIN */
2797                                                 iptr->s1.argcount = stackdepth;
2798                                                 iptr->sx.s23.s2.args = DMNEW(s4, stackdepth);
2799
2800                                                 copy = curstack;
2801                                                 for (i-- ; i >= 0; i--) {
2802                                                         iptr->sx.s23.s2.args[i] = copy->varnum;
2803
2804                                                         /* do not change STACKVARs or LOCALVARS to ARGVAR*/
2805                                                         /* ->  won't help anyway */
2806                                                         if (!(IS_OUTVAR(copy) || IS_LOCALVAR(copy))) {
2807
2808 #if defined(SUPPORT_PASS_FLOATARGS_IN_INTREGS)
2809                         /* If we pass float arguments in integer argument registers, we
2810                          * are not allowed to precolor them here. Floats have to be moved
2811                          * to this regs explicitly in codegen().
2812                          * Only arguments that are passed by stack anyway can be precolored
2813                          * (michi 2005/07/24) */
2814                                                         if (!(sd.var[copy->varnum].flags & SAVEDVAR) &&
2815                                                            (!IS_FLT_DBL_TYPE(copy->type) 
2816                                                                 || md->params[i].inmemory)) {
2817 #else
2818                                                         if (!(sd.var[copy->varnum].flags & SAVEDVAR)) {
2819 #endif
2820
2821                                                                 SET_PREALLOC(copy);
2822
2823 #if defined(ENABLE_INTRP)
2824                                                                 if (!opt_intrp) {
2825 #endif
2826                                                                         if (md->params[i].inmemory) {
2827                                                                                 sd.var[copy->varnum].regoff =
2828                                                                                         md->params[i].regoff;
2829                                                                                 sd.var[copy->varnum].flags |= 
2830                                                                                         INMEMORY;
2831                                                                         }
2832                                                                         else {
2833                                                                                 if (IS_FLT_DBL_TYPE(copy->type)) {
2834 #if defined(SUPPORT_PASS_FLOATARGS_IN_INTREGS)
2835                                                                                         assert(0); /* XXX is this assert ok? */
2836 #else
2837                                                                                         sd.var[copy->varnum].regoff = 
2838                                                                                 rd->argfltregs[md->params[i].regoff];
2839 #endif /* SUPPORT_PASS_FLOATARGS_IN_INTREGS */
2840                                                                                 }
2841                                                                                 else {
2842 #if defined(SUPPORT_COMBINE_INTEGER_REGISTERS)
2843                                                                                         if (IS_2_WORD_TYPE(copy->type))
2844                                                                                                 sd.var[copy->varnum].regoff = 
2845                                 PACK_REGS( rd->argintregs[GET_LOW_REG(md->params[i].regoff)],
2846                                                    rd->argintregs[GET_HIGH_REG(md->params[i].regoff)]);
2847
2848                                                                                         else
2849 #endif /* SUPPORT_COMBINE_INTEGER_REGISTERS */
2850                                                                                                 sd.var[copy->varnum].regoff = 
2851                                                                                 rd->argintregs[md->params[i].regoff];
2852                                                                                 }
2853                                                                         }
2854 #if defined(ENABLE_INTRP)
2855                                                                 } /* end if (!opt_intrp) */
2856 #endif
2857                                                         }
2858                                                         }
2859                                                         copy = copy->prev;
2860                                                 }
2861
2862                                                 /* deal with live-through stack slots "under" the */
2863                                                 /* arguments */
2864                                                 /* XXX not for ICMD_BUILTIN */
2865
2866                                                 i = md->paramcount;
2867
2868                                                 while (copy) {
2869                                                         SET_TEMPVAR(copy);
2870                                                         iptr->sx.s23.s2.args[i++] = copy->varnum;
2871                                                         sd.var[copy->varnum].flags |= SAVEDVAR;
2872                                                         copy = copy->prev;
2873                                                 }
2874
2875                                                 /* pop the arguments */
2876
2877                                                 i = md->paramcount;
2878
2879                                                 stackdepth -= i;
2880                                                 while (--i >= 0) {
2881                                                         POPANY;
2882                                                 }
2883
2884                                                 /* push the return value */
2885
2886                                                 if (md->returntype.type != TYPE_VOID) {
2887                                                         GET_NEW_VAR(sd, new_index, md->returntype.type);
2888                                                         DST(md->returntype.type, new_index);
2889                                                         stackdepth++;
2890                                                 }
2891                                                 break;
2892
2893                                         case ICMD_INLINE_START:
2894                                         case ICMD_INLINE_END:
2895                                                 CLR_S1;
2896                                                 CLR_DST;
2897                                                 break;
2898
2899                                         case ICMD_MULTIANEWARRAY:
2900                                                 coalescing_boundary = sd.new;
2901                                                 if (rd->argintreguse < 3)
2902                                                         rd->argintreguse = 3;
2903
2904                                                 i = iptr->s1.argcount;
2905
2906                                                 REQUIRE(i);
2907
2908                                                 iptr->sx.s23.s2.args = DMNEW(s4, i);
2909
2910 #if defined(SPECIALMEMUSE)
2911 # if defined(__DARWIN__)
2912                                                 if (rd->memuse < (i + INT_ARG_CNT +LA_SIZE_IN_POINTERS))
2913                                                         rd->memuse = i + LA_SIZE_IN_POINTERS + INT_ARG_CNT;
2914 # else
2915                                                 if (rd->memuse < (i + LA_SIZE_IN_POINTERS + 3))
2916                                                         rd->memuse = i + LA_SIZE_IN_POINTERS + 3;
2917 # endif
2918 #else
2919 # if defined(__I386__)
2920                                                 if (rd->memuse < i + 3)
2921                                                         rd->memuse = i + 3; /* n integer args spilled on stack */
2922 # elif defined(__MIPS__) && SIZEOF_VOID_P == 4
2923                                                 if (rd->memuse < i + 2)
2924                                                         rd->memuse = i + 2; /* 4*4 bytes callee save space */
2925 # else
2926                                                 if (rd->memuse < i)
2927                                                         rd->memuse = i; /* n integer args spilled on stack */
2928 # endif /* defined(__I386__) */
2929 #endif
2930                                                 copy = curstack;
2931                                                 while (--i >= 0) {
2932                                         /* check INT type here? Currently typecheck does this. */
2933                                                         iptr->sx.s23.s2.args[i] = copy->varnum;
2934                                                         if (!(sd.var[copy->varnum].flags & SAVEDVAR)
2935                                                                 && (!IS_OUTVAR(copy))
2936                                                                 && (!IS_LOCALVAR(copy)) ) {
2937                                                                 copy->varkind = ARGVAR;
2938                                                                 sd.var[copy->varnum].flags |=
2939                                                                         INMEMORY & PREALLOC;
2940 #if defined(SPECIALMEMUSE)
2941 # if defined(__DARWIN__)
2942                                                                 sd.var[copy->varnum].regoff = i + 
2943                                                                         LA_SIZE_IN_POINTERS + INT_ARG_CNT;
2944 # else
2945                                                                 sd.var[copy->varnum].regoff = i + 
2946                                                                         LA_SIZE_IN_POINTERS + 3;
2947 # endif
2948 #else
2949 # if defined(__I386__)
2950                                                                 sd.var[copy->varnum].regoff = i + 3;
2951 # elif defined(__MIPS__) && SIZEOF_VOID_P == 4
2952                                                                 sd.var[copy->varnum].regoff = i + 2;
2953 # else
2954                                                                 sd.var[copy->varnum].regoff = i;
2955 # endif /* defined(__I386__) */
2956 #endif /* defined(SPECIALMEMUSE) */
2957                                                         }
2958                                                         copy = copy->prev;
2959                                                 }
2960                                                 while (copy) {
2961                                                         sd.var[copy->varnum].flags |= SAVEDVAR;
2962                                                         copy = copy->prev;
2963                                                 }
2964
2965                                                 i = iptr->s1.argcount;
2966                                                 stackdepth -= i;
2967                                                 while (--i >= 0) {
2968                                                         POPANY;
2969                                                 }
2970                                                 GET_NEW_VAR(sd, new_index, TYPE_ADR);
2971                                                 DST(TYPE_ADR, new_index);
2972                                                 stackdepth++;
2973                                                 break;
2974
2975                                         default:
2976                                                 *exceptionptr =
2977                                                         new_internalerror("Unknown ICMD %d", opcode);
2978                                                 return false;
2979                                         } /* switch */
2980
2981                                         CHECKOVERFLOW;
2982                                         iptr++;
2983                                 } /* while instructions */
2984
2985                                 /* stack slots at basic block end become interfaces */
2986
2987                                 sd.bptr->outstack = curstack;
2988                                 sd.bptr->outdepth = stackdepth;
2989                                 sd.bptr->outvars = DMNEW(s4, stackdepth);
2990
2991                                 i = stackdepth - 1;
2992                                 for (copy = curstack; copy; i--, copy = copy->prev) {
2993                                         varinfo *v;
2994
2995                                         /* with the new vars rd->interfaces will be removed */
2996                                         /* and all in and outvars have to be STACKVARS!     */
2997                                         /* in the moment i.e. SWAP with in and out vars can */
2998                                         /* create an unresolvable conflict */
2999
3000                                         SET_TEMPVAR(copy);
3001
3002                                         v = sd.var + copy->varnum;
3003                                         v->flags |= OUTVAR;
3004
3005                                         if (jd->interface_map[i*5 + copy->type].flags == UNUSED) {
3006                                                 /* no interface var until now for this depth and */
3007                                                 /* type */
3008                                                 jd->interface_map[i*5 + copy->type].flags = v->flags;
3009                                         }
3010                                         else {
3011                                                 jd->interface_map[i*5 + copy->type].flags |= v->flags;
3012                                         }
3013
3014                                         sd.bptr->outvars[i] = copy->varnum;
3015                                 }
3016
3017                                 /* check if interface slots at basic block begin must be saved */
3018                                 IF_NO_INTRP(
3019                                         for (i=0; i<sd.bptr->indepth; ++i) {
3020                                                 varinfo *v = sd.var + sd.bptr->invars[i];
3021
3022                                                 if (jd->interface_map[i*5 + v->type].flags == UNUSED) {
3023                                                         /* no interface var until now for this depth and */
3024                                                         /* type */
3025                                                         jd->interface_map[i*5 + v->type].flags = v->flags;
3026                                                 }
3027                                                 else {
3028                                                         jd->interface_map[i*5 + v->type].flags |= v->flags;
3029                                                 }
3030                                         }
3031                                 );
3032
3033 #if defined(STACK_VERBOSE)
3034                                 printf("OUTVARS\n");
3035                                 for( copy = sd.bptr->outstack; copy; copy = copy->prev ) {
3036                                         printf("%2d(%d", copy->varnum, copy->type);
3037                                         if (IS_OUTVAR(copy))
3038                                                 printf("S");
3039                                         if (IS_PREALLOC(copy))
3040                                                 printf("A");
3041                                         printf(") ");
3042                                 }
3043                                 printf("\n");
3044 #endif
3045                     } /* if */
3046                         else
3047                                 superblockend = true;
3048
3049                         sd.bptr++;
3050                 } /* while blocks */
3051         } while (repeat && !deadcode);
3052
3053         /* gather statistics *****************************************************/
3054
3055 #if defined(ENABLE_STATISTICS)
3056         if (opt_stat) {
3057                 if (jd->new_basicblockcount > count_max_basic_blocks)
3058                         count_max_basic_blocks = jd->new_basicblockcount;
3059                 count_basic_blocks += jd->new_basicblockcount;
3060                 if (jd->new_instructioncount > count_max_javainstr)
3061                         count_max_javainstr = jd->new_instructioncount;
3062                 count_javainstr += jd->new_instructioncount;
3063                 if (jd->new_stackcount > count_upper_bound_new_stack)
3064                         count_upper_bound_new_stack = jd->new_stackcount;
3065                 if ((sd.new - jd->new_stack) > count_max_new_stack)
3066                         count_max_new_stack = (sd.new - jd->new_stack);
3067
3068                 b_count = jd->new_basicblockcount;
3069                 sd.bptr = jd->new_basicblocks;
3070                 while (--b_count >= 0) {
3071                         if (sd.bptr->flags > BBREACHED) {
3072                                 if (sd.bptr->indepth >= 10)
3073                                         count_block_stack[10]++;
3074                                 else
3075                                         count_block_stack[sd.bptr->indepth]++;
3076                                 len = sd.bptr->icount;
3077                                 if (len < 10)
3078                                         count_block_size_distribution[len]++;
3079                                 else if (len <= 12)
3080                                         count_block_size_distribution[10]++;
3081                                 else if (len <= 14)
3082                                         count_block_size_distribution[11]++;
3083                                 else if (len <= 16)
3084                                         count_block_size_distribution[12]++;
3085                                 else if (len <= 18)
3086                                         count_block_size_distribution[13]++;
3087                                 else if (len <= 20)
3088                                         count_block_size_distribution[14]++;
3089                                 else if (len <= 25)
3090                                         count_block_size_distribution[15]++;
3091                                 else if (len <= 30)
3092                                         count_block_size_distribution[16]++;
3093                                 else
3094                                         count_block_size_distribution[17]++;
3095                         }
3096                         sd.bptr++;
3097                 }
3098
3099                 if (iteration_count == 1)
3100                         count_analyse_iterations[0]++;
3101                 else if (iteration_count == 2)
3102                         count_analyse_iterations[1]++;
3103                 else if (iteration_count == 3)
3104                         count_analyse_iterations[2]++;
3105                 else if (iteration_count == 4)
3106                         count_analyse_iterations[3]++;
3107                 else
3108                         count_analyse_iterations[4]++;
3109
3110                 if (jd->new_basicblockcount <= 5)
3111                         count_method_bb_distribution[0]++;
3112                 else if (jd->new_basicblockcount <= 10)
3113                         count_method_bb_distribution[1]++;
3114                 else if (jd->new_basicblockcount <= 15)
3115                         count_method_bb_distribution[2]++;
3116                 else if (jd->new_basicblockcount <= 20)
3117                         count_method_bb_distribution[3]++;
3118                 else if (jd->new_basicblockcount <= 30)
3119                         count_method_bb_distribution[4]++;
3120                 else if (jd->new_basicblockcount <= 40)
3121                         count_method_bb_distribution[5]++;
3122                 else if (jd->new_basicblockcount <= 50)
3123                         count_method_bb_distribution[6]++;
3124                 else if (jd->new_basicblockcount <= 75)
3125                         count_method_bb_distribution[7]++;
3126                 else
3127                         count_method_bb_distribution[8]++;
3128         }
3129 #endif /* defined(ENABLE_STATISTICS) */
3130
3131         /* everything's ok *******************************************************/
3132
3133         return true;
3134
3135         /* goto labels for throwing verifier exceptions **************************/
3136
3137 #if defined(ENABLE_VERIFIER)
3138
3139 throw_stack_underflow:
3140         exceptions_throw_verifyerror(m, "Unable to pop operand off an empty stack");
3141         return false;
3142
3143 throw_stack_overflow:
3144         exceptions_throw_verifyerror(m, "Stack size too large");
3145         return false;
3146
3147 throw_stack_depth_error:
3148         exceptions_throw_verifyerror(m,"Stack depth mismatch");
3149         return false;
3150
3151 throw_stack_type_error:
3152         exceptions_throw_verifyerror_for_stack(m, expectedtype);
3153         return false;
3154
3155 throw_stack_category_error:
3156         exceptions_throw_verifyerror(m, "Attempt to split long or double on the stack");
3157         return false;
3158
3159 #endif
3160 }
3161
3162
3163 /*
3164  * These are local overrides for various environment variables in Emacs.
3165  * Please do not remove this and leave it at the end of the file, where
3166  * Emacs will automagically detect them.
3167  * ---------------------------------------------------------------------
3168  * Local variables:
3169  * mode: c
3170  * indent-tabs-mode: t
3171  * c-basic-offset: 4
3172  * tab-width: 4
3173  * End:
3174  * vim:noexpandtab:sw=4:ts=4:
3175  */