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