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