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