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  /* relocate live stackvars after the current instruction */
1979 
1980 #if defined(ENABLE_COMPILER2)
1982  s4 *stackvar = iptr->stack_after;
1983  for (s4 i = 0; i < iptr->stackdepth_after; i++) {
1984  RELOCATE(*stackvar);
1985  stackvar++;
1986  }
1987  }
1988 #endif
1989 
1990 #if defined(STACK_VERBOSE)
1991  show_icmd(sd->jd, iptr, false, SHOW_STACK);
1992  printf("\n");
1993 #endif
1994  }
1995 
1996  /* relocate outvars */
1997 
1998  for (i=0; i<b->outdepth; ++i) {
1999  RELOCATE(b->outvars[i]);
2000  }
2001 
2002 #if defined(STACK_VERBOSE)
2003  stack_verbose_block_exit(sd, superblockend);
2004 #endif
2005 
2006  /* propagate to the next block */
2007 
2008  if (!superblockend)
2009  if (!stack_reach_next_block(sd))
2010  return false;
2011 
2012  return true;
2013 }
2014 
2015 
2016 /* stack_change_to_tempvar *****************************************************
2017 
2018  Change the given stackslot to a TEMPVAR. This includes creating a new
2019  temporary variable and changing the dst.varindex of the creator of the
2020  stacklot to the new variable index. If this stackslot has been passed
2021  through ICMDs between the point of its creation and the current point,
2022  then the variable index is also changed in these ICMDs.
2023 
2024  IN:
2025  sd...........stack analysis data
2026  sp...........stackslot to change
2027  ilimit.......instruction up to which to look for ICMDs passing-through
2028  the stackslot (exclusive). This may point exactly after the
2029  last instruction, in which case the search is done to the
2030  basic block end.
2031 
2032 *******************************************************************************/
2033 
2035  instruction *ilimit)
2036 {
2037  s4 newindex;
2038  s4 oldindex;
2039  instruction *iptr;
2040  s4 depth;
2041  s4 i;
2042 
2043  oldindex = sp->varnum;
2044 
2045  /* create a new temporary variable */
2046 
2047  GET_NEW_VAR(*sd, newindex, sp->type);
2048 
2049  sd->var[newindex].flags = sp->flags;
2050 
2051  /* change the stackslot */
2052 
2053  sp->varnum = newindex;
2054  sp->varkind = TEMPVAR;
2055 
2056  /* change the dst.varindex of the stackslot's creator */
2057 
2058  if (sp->creator)
2059  sp->creator->dst.varindex = newindex;
2060 
2061  /* handle ICMDs this stackslot passed through, if any */
2062 
2063  if (sp->flags & PASSTHROUGH) {
2064  iptr = (sp->creator) ? (sp->creator + 1) : sd->bptr->iinstr;
2065 
2066  /* assert that the limit points to an ICMD, or after the last one */
2067 
2068  assert(ilimit >= sd->bptr->iinstr);
2069  assert(ilimit <= sd->bptr->iinstr + sd->bptr->icount);
2070 
2071  /* find the stackdepth under sp plus one */
2072  /* Note: This number is usually known when this function is called, */
2073  /* but calculating it here is less error-prone and should not be */
2074  /* a performance problem. */
2075 
2076  for (depth = 0; sp != NULL; sp = sp->prev)
2077  depth++;
2078 
2079  /* iterate over all instructions in the range and replace */
2080 
2081  for (; iptr < ilimit; ++iptr) {
2082  switch (iptr->opc) {
2083  case ICMD_INVOKESTATIC:
2084  case ICMD_INVOKESPECIAL:
2085  case ICMD_INVOKEVIRTUAL:
2086  case ICMD_INVOKEINTERFACE:
2087  case ICMD_BUILTIN:
2088  i = iptr->s1.argcount - depth;
2089  if (iptr->sx.s23.s2.args[i] == oldindex) {
2090  iptr->sx.s23.s2.args[i] = newindex;
2091  }
2092  break;
2093  /* IMPORTANT: If any ICMD sets the PASSTHROUGH flag of a */
2094  /* stackslot, it must be added in this switch! */
2095  default:
2096  break;
2097  }
2098  }
2099  }
2100 }
2101 
2102 
2103 /* stack_init_javalocals *******************************************************
2104 
2105  Initialize the mapping from Java locals to cacao variables at method entry.
2106 
2107  IN:
2108  sd...........stack analysis data
2109 
2110 *******************************************************************************/
2111 
2113 {
2114  jitdata *jd = sd->jd;
2115  s4 *jl = DMNEW(s4, sd->maxlocals);
2116 
2117  jd->basicblocks[0].javalocals = jl;
2118 
2119  for (int i=0; i<sd->maxlocals; ++i)
2120  jl[i] = jitdata::UNUSED;
2121 
2122  methoddesc *md = jd->m->parseddesc;
2123  for (int i = 0, j = 0; i<md->paramcount; ++i) {
2124  Type type = md->paramtypes[i].type;
2125  jl[j] = jd->local_map[5*j + type];
2126 
2127  j += IS_2_WORD_TYPE(type) ? 2 : 1;
2128  }
2129 }
2130 
2131 
2132 /* stack_analyse ***************************************************************
2133 
2134  Analyse_stack uses the intermediate code created by parse.c to
2135  build a model of the JVM operand stack for the current method.
2136 
2137  The following checks are performed:
2138  - check for operand stack underflow (before each instruction)
2139  - check for operand stack overflow (after[1] each instruction)
2140  - check for matching stack depth at merging points
2141  - check for matching basic types[2] at merging points
2142  - check basic types for instruction input (except for BUILTIN*
2143  opcodes, INVOKE* opcodes and MULTIANEWARRAY)
2144 
2145  [1]) Checking this after the instruction should be ok. parse.c
2146  counts the number of required stack slots in such a way that it is
2147  only vital that we don't exceed `maxstack` at basic block
2148  boundaries.
2149 
2150  [2]) 'basic types' means the distinction between INT, LONG, FLOAT,
2151  DOUBLE and ADDRESS types. Subtypes of INT and different ADDRESS
2152  types are not discerned.
2153 
2154 *******************************************************************************/
2155 
2157  stackdata_t sd;
2158 
2159  return sd.analyse(jd);
2160 }
2161 
2163 {
2164  stackdata_t& sd = *this;
2165 
2166  methodinfo *m; /* method being analyzed */
2167  codeinfo *code;
2168  registerdata *rd;
2169  int stackdepth;
2170  stackelement_t *curstack; /* current stack top */
2171  stackelement_t *copy;
2172  ICMD opcode; /* opcode of current instruction */
2173  int varindex;
2174  int javaindex;
2175  Type type; /* operand type */
2176  int len; /* # of instructions after the current one */
2177  bool superblockend; /* if true, no fallthrough to next block */
2178  bool deadcode; /* true if no live code has been reached */
2179  instruction *iptr; /* the current instruction */
2180  basicblock *tbptr;
2181  basicblock *original;
2182 
2183  stackelement_t **last_store_boundary;
2184  stackelement_t *coalescing_boundary;
2185 
2186  stackelement_t *src1, *src2, *src3, *src4, *dst1, *dst2;
2187 
2188  branch_target_t *table;
2189  lookup_target_t *lookup;
2190  builtintable_entry *bte;
2191  methoddesc *md;
2192 #if defined(ENABLE_STATISTICS)
2193  int iteration_count; /* number of iterations of analysis */
2194 #endif
2195  int new_index; /* used to get a new var index with GET_NEW_INDEX*/
2196 
2197 #if defined(STACK_VERBOSE)
2198  show_method(jd, SHOW_PARSE);
2199 #endif
2200 
2201  /* get required compiler data - initialization */
2202 
2203  m = jd->m;
2204  code = jd->code;
2205  rd = jd->rd;
2206 
2207  /* initialize the stackdata_t struct */
2208 
2209  sd.m = m;
2210  sd.jd = jd;
2211  sd.varcount = jd->varcount;
2212  sd.vartop = jd->vartop;
2213  sd.localcount = jd->localcount;
2214  sd.var = jd->var;
2215  sd.varsallocated = sd.varcount;
2216  sd.maxlocals = m->maxlocals;
2217  sd.javalocals = DMNEW(s4, sd.maxlocals);
2219 
2220  /* prepare the variable for exception handler stacks */
2221  /* (has been reserved by STACK_EXTRA_VARS, or VERIFIER_EXTRA_VARS) */
2222 
2223  sd.exstack.type = TYPE_ADR;
2224  sd.exstack.prev = NULL;
2225  sd.exstack.varnum = sd.localcount;
2226  sd.var[sd.exstack.varnum].type = TYPE_ADR;
2227 
2228 #if defined(ENABLE_STATISTICS)
2229  iteration_count = 0;
2230 #endif
2231 
2232  /* find the last real basic block */
2233 
2234  sd.last_real_block = NULL;
2235  tbptr = jd->basicblocks;
2236  while (tbptr->next) {
2237  sd.last_real_block = tbptr;
2238  tbptr = tbptr->next;
2239  }
2240  assert(sd.last_real_block);
2241 
2242  /* find the last exception handler */
2243 
2244  if (jd->exceptiontablelength)
2245  sd.extableend = jd->exceptiontable + jd->exceptiontablelength - 1;
2246  else
2247  sd.extableend = NULL;
2248 
2249  /* init jd->interface_map */
2250 
2251  jd->maxinterfaces = m->maxstack;
2252  jd->interface_map = DMNEW(interface_info, m->maxstack * 5);
2253  for (int i = 0; i < m->maxstack * 5; i++)
2255 
2256  last_store_boundary = DMNEW(stackelement_t *, m->maxlocals);
2257 
2258  /* initialize state and invars (none) of first block */
2259 
2261  jd->basicblocks[0].invars = NULL;
2262  jd->basicblocks[0].indepth = 0;
2263  jd->basicblocks[0].inlocals =
2265  MCOPY(jd->basicblocks[0].inlocals, jd->var, varinfo,
2267 
2268  /* initialize java local mapping of first block */
2269 
2270  stack_init_javalocals(&sd);
2271 
2272  /* stack analysis loop (until fixpoint reached) **************************/
2273 
2274  do {
2275 #if defined(ENABLE_STATISTICS)
2276  iteration_count++;
2277 #endif
2278 
2279  /* initialize loop over basic blocks */
2280 
2281  sd.bptr = jd->basicblocks;
2282  superblockend = true;
2283  sd.repeat = false;
2284  curstack = NULL;
2285  stackdepth = 0;
2286  deadcode = true;
2287 
2288  /* iterate over basic blocks *****************************************/
2289 
2290  for (; sd.bptr; sd.bptr = sd.bptr->next) {
2291  if (sd.bptr->state == basicblock::DELETED) {
2292  /* This block has been deleted - do nothing. */
2293 
2294  continue;
2295  }
2296 
2298  /* re-analyse a block because its input changed */
2299 
2300  deadcode = false;
2301 
2302  if (!stack_reanalyse_block(&sd))
2303  return false;
2304 
2305  superblockend = true; /* XXX */
2306  continue;
2307  }
2308 
2309  if (superblockend && (sd.bptr->state < basicblock::REACHED)) {
2310  /* This block has not been reached so far, and we
2311  don't fall into it, so we'll have to iterate
2312  again. */
2313 
2314  sd.repeat = true;
2315  continue;
2316  }
2317 
2318  if (sd.bptr->state > basicblock::REACHED) {
2319  /* This block is already finished. */
2320 
2321  superblockend = true;
2322  continue;
2323  }
2324 
2325  if (sd.bptr->original && sd.bptr->original->state < basicblock::FINISHED) {
2326  /* This block is a clone and the original has not been
2327  analysed, yet. Analyse it on the next
2328  iteration. */
2329 
2330  sd.repeat = true;
2331  /* XXX superblockend? */
2332  continue;
2333  }
2334 
2335  /* This block has to be analysed now. */
2336 
2337  deadcode = false;
2338 
2339  /* XXX The rest of this block is still indented one level too */
2340  /* much in order to avoid a giant diff by changing that. */
2341 
2342  /* We know that sd.bptr->state == BBREACHED. */
2343  /* This block has been reached before. */
2344 
2345  assert(sd.bptr->state == basicblock::REACHED);
2346  stackdepth = sd.bptr->indepth;
2347 
2348  /* find exception handlers for this block */
2349 
2350  /* determine the active exception handlers for this block */
2351  /* XXX could use a faster algorithm with sorted lists or */
2352  /* something? */
2353 
2354  original = (sd.bptr->original) ? sd.bptr->original : sd.bptr;
2355 
2356  len = 0;
2357  for (exception_entry *ex = jd->exceptiontable; ex != NULL; ex = ex->down) {
2358  if ((ex->start <= original) && (ex->end > original)) {
2359  sd.handlers[len++] = ex;
2360  }
2361  }
2362  sd.handlers[len] = NULL;
2363 
2364 
2365  /* reanalyse cloned block */
2366 
2367  if (sd.bptr->original) {
2368  if (!stack_reanalyse_block(&sd))
2369  return false;
2370  continue;
2371  }
2372 
2373  /* reset the new pointer for allocating stackslots */
2374 
2375  sd.new_elem = jd->stack;
2376 
2377  /* create the instack of this block */
2378 
2379  curstack = stack_create_instack(&sd);
2380 
2381  /* initialize locals at the start of this block */
2382 
2383  if (sd.bptr->inlocals)
2384  MCOPY(sd.var, sd.bptr->inlocals, varinfo, sd.localcount);
2385 
2386  MCOPY(sd.javalocals, sd.bptr->javalocals, s4, sd.maxlocals);
2387 
2388  /* set up local variables for analyzing this block */
2389 
2390  superblockend = false;
2391  len = sd.bptr->icount;
2392  iptr = sd.bptr->iinstr;
2393 
2394  /* mark the block as analysed */
2395 
2397 
2398  /* reset variables for dependency checking */
2399 
2400  coalescing_boundary = sd.new_elem;
2401  for(int i = 0; i < m->maxlocals; i++)
2402  last_store_boundary[i] = sd.new_elem;
2403 
2404  /* remember the start of this block's variables */
2405 
2406  sd.bptr->varstart = sd.vartop;
2407 
2408 #if defined(STACK_VERBOSE)
2409  stack_verbose_block_enter(&sd, false);
2410 #endif
2411 
2412  /* reach exception handlers for this block */
2413 
2414  if (!stack_reach_handlers(&sd))
2415  return false;
2416 
2417  /* iterate over ICMDs ****************************************/
2418 
2419  while (--len >= 0) {
2420 
2421 #if defined(STACK_VERBOSE)
2422  stack_verbose_show_state(&sd, iptr, curstack);
2423 #endif
2424 
2425  /* fetch the current opcode */
2426 
2427  opcode = iptr->opc;
2428 
2429  /* automatically replace some ICMDs with builtins */
2430 
2431  bte = builtintable_get_automatic(opcode);
2432 
2433  if ((bte != NULL) && (bte->opcode == opcode)) {
2434  iptr->opc = ICMD_BUILTIN;
2435  iptr->flags.bits &= INS_FLAG_ID_MASK;
2436  iptr->sx.s23.s3.bte = bte;
2437  /* iptr->line is already set */
2438  code_unflag_leafmethod(code);
2439  goto icmd_BUILTIN;
2440  }
2441 
2442  /* main opcode switch *************************************/
2443 
2444  switch (opcode) {
2445 
2446  /* pop 0 push 0 */
2447 
2448  case ICMD_NOP:
2449 icmd_NOP:
2450  CLR_SX;
2451  OP0_0;
2452  break;
2453 
2454  case ICMD_CHECKNULL:
2455  coalescing_boundary = sd.new_elem;
2456  STATISTICS(count_check_null++);
2457  USE_S1(TYPE_ADR);
2458  CLR_SX;
2459  iptr->dst.varindex = iptr->s1.varindex;
2460  break;
2461 
2462  case ICMD_RET:
2463  varindex = iptr->s1.varindex =
2464  jd->local_map[iptr->s1.varindex * 5 + TYPE_ADR];
2465 
2466 #if defined(ENABLE_VERIFIER)
2467  if (sd.var[varindex].type != TYPE_RET) {
2468  exceptions_throw_verifyerror(m, "RET with non-returnAddress value");
2469  return false;
2470  }
2471 #endif
2472 
2473  CLR_SX;
2474 
2475  iptr->dst.block = stack_mark_reached(&sd, sd.var[varindex].vv.retaddr, curstack, stackdepth);
2476  superblockend = true;
2477  break;
2478 
2479  case ICMD_RETURN:
2480  STATISTICS(count_pcmd_return++);
2481  CLR_SX;
2482  OP0_0;
2483  superblockend = true;
2484  sd.jd->returncount++;
2485  sd.jd->returnblock = sd.bptr;
2486  break;
2487 
2488  case ICMD_BREAKPOINT:
2489  OP0_0;
2490  break;
2491 
2492 
2493  /* pop 0 push 1 const */
2494 
2495  /************************** ICONST OPTIMIZATIONS **************************/
2496 
2497  case ICMD_ICONST:
2498  STATISTICS(count_pcmd_load++);
2499  if (len == 0)
2500  goto normal_ICONST;
2501 
2502  switch (iptr[1].opc) {
2503  case ICMD_IADD:
2504  iptr->opc = ICMD_IADDCONST;
2505  /* FALLTHROUGH */
2506 
2507  icmd_iconst_tail:
2508  iptr[1].opc = ICMD_NOP;
2510  STATISTICS(count_pcmd_op++);
2511  break;
2512 
2513  case ICMD_ISUB:
2514  iptr->opc = ICMD_ISUBCONST;
2515  goto icmd_iconst_tail;
2516 #if SUPPORT_CONST_MUL
2517  case ICMD_IMUL:
2518  iptr->opc = ICMD_IMULCONST;
2519  goto icmd_iconst_tail;
2520 #else /* SUPPORT_CONST_MUL */
2521  case ICMD_IMUL:
2522  if (iptr->sx.val.i == 0x00000002)
2523  iptr->sx.val.i = 1;
2524  else if (iptr->sx.val.i == 0x00000004)
2525  iptr->sx.val.i = 2;
2526  else if (iptr->sx.val.i == 0x00000008)
2527  iptr->sx.val.i = 3;
2528  else if (iptr->sx.val.i == 0x00000010)
2529  iptr->sx.val.i = 4;
2530  else if (iptr->sx.val.i == 0x00000020)
2531  iptr->sx.val.i = 5;
2532  else if (iptr->sx.val.i == 0x00000040)
2533  iptr->sx.val.i = 6;
2534  else if (iptr->sx.val.i == 0x00000080)
2535  iptr->sx.val.i = 7;
2536  else if (iptr->sx.val.i == 0x00000100)
2537  iptr->sx.val.i = 8;
2538  else if (iptr->sx.val.i == 0x00000200)
2539  iptr->sx.val.i = 9;
2540  else if (iptr->sx.val.i == 0x00000400)
2541  iptr->sx.val.i = 10;
2542  else if (iptr->sx.val.i == 0x00000800)
2543  iptr->sx.val.i = 11;
2544  else if (iptr->sx.val.i == 0x00001000)
2545  iptr->sx.val.i = 12;
2546  else if (iptr->sx.val.i == 0x00002000)
2547  iptr->sx.val.i = 13;
2548  else if (iptr->sx.val.i == 0x00004000)
2549  iptr->sx.val.i = 14;
2550  else if (iptr->sx.val.i == 0x00008000)
2551  iptr->sx.val.i = 15;
2552  else if (iptr->sx.val.i == 0x00010000)
2553  iptr->sx.val.i = 16;
2554  else if (iptr->sx.val.i == 0x00020000)
2555  iptr->sx.val.i = 17;
2556  else if (iptr->sx.val.i == 0x00040000)
2557  iptr->sx.val.i = 18;
2558  else if (iptr->sx.val.i == 0x00080000)
2559  iptr->sx.val.i = 19;
2560  else if (iptr->sx.val.i == 0x00100000)
2561  iptr->sx.val.i = 20;
2562  else if (iptr->sx.val.i == 0x00200000)
2563  iptr->sx.val.i = 21;
2564  else if (iptr->sx.val.i == 0x00400000)
2565  iptr->sx.val.i = 22;
2566  else if (iptr->sx.val.i == 0x00800000)
2567  iptr->sx.val.i = 23;
2568  else if (iptr->sx.val.i == 0x01000000)
2569  iptr->sx.val.i = 24;
2570  else if (iptr->sx.val.i == 0x02000000)
2571  iptr->sx.val.i = 25;
2572  else if (iptr->sx.val.i == 0x04000000)
2573  iptr->sx.val.i = 26;
2574  else if (iptr->sx.val.i == 0x08000000)
2575  iptr->sx.val.i = 27;
2576  else if (iptr->sx.val.i == 0x10000000)
2577  iptr->sx.val.i = 28;
2578  else if (iptr->sx.val.i == 0x20000000)
2579  iptr->sx.val.i = 29;
2580  else if (iptr->sx.val.i == 0x40000000)
2581  iptr->sx.val.i = 30;
2582  else if (iptr->sx.val.u == 0x80000000)
2583  iptr->sx.val.i = 31;
2584  else
2585  goto normal_ICONST;
2586 
2587  iptr->opc = ICMD_IMULPOW2;
2588  goto icmd_iconst_tail;
2589 #endif /* SUPPORT_CONST_MUL */
2590  case ICMD_IDIV:
2591  if (iptr->sx.val.i == 0x00000002)
2592  iptr->sx.val.i = 1;
2593  else if (iptr->sx.val.i == 0x00000004)
2594  iptr->sx.val.i = 2;
2595  else if (iptr->sx.val.i == 0x00000008)
2596  iptr->sx.val.i = 3;
2597  else if (iptr->sx.val.i == 0x00000010)
2598  iptr->sx.val.i = 4;
2599  else if (iptr->sx.val.i == 0x00000020)
2600  iptr->sx.val.i = 5;
2601  else if (iptr->sx.val.i == 0x00000040)
2602  iptr->sx.val.i = 6;
2603  else if (iptr->sx.val.i == 0x00000080)
2604  iptr->sx.val.i = 7;
2605  else if (iptr->sx.val.i == 0x00000100)
2606  iptr->sx.val.i = 8;
2607  else if (iptr->sx.val.i == 0x00000200)
2608  iptr->sx.val.i = 9;
2609  else if (iptr->sx.val.i == 0x00000400)
2610  iptr->sx.val.i = 10;
2611  else if (iptr->sx.val.i == 0x00000800)
2612  iptr->sx.val.i = 11;
2613  else if (iptr->sx.val.i == 0x00001000)
2614  iptr->sx.val.i = 12;
2615  else if (iptr->sx.val.i == 0x00002000)
2616  iptr->sx.val.i = 13;
2617  else if (iptr->sx.val.i == 0x00004000)
2618  iptr->sx.val.i = 14;
2619  else if (iptr->sx.val.i == 0x00008000)
2620  iptr->sx.val.i = 15;
2621  else if (iptr->sx.val.i == 0x00010000)
2622  iptr->sx.val.i = 16;
2623  else if (iptr->sx.val.i == 0x00020000)
2624  iptr->sx.val.i = 17;
2625  else if (iptr->sx.val.i == 0x00040000)
2626  iptr->sx.val.i = 18;
2627  else if (iptr->sx.val.i == 0x00080000)
2628  iptr->sx.val.i = 19;
2629  else if (iptr->sx.val.i == 0x00100000)
2630  iptr->sx.val.i = 20;
2631  else if (iptr->sx.val.i == 0x00200000)
2632  iptr->sx.val.i = 21;
2633  else if (iptr->sx.val.i == 0x00400000)
2634  iptr->sx.val.i = 22;
2635  else if (iptr->sx.val.i == 0x00800000)
2636  iptr->sx.val.i = 23;
2637  else if (iptr->sx.val.i == 0x01000000)
2638  iptr->sx.val.i = 24;
2639  else if (iptr->sx.val.i == 0x02000000)
2640  iptr->sx.val.i = 25;
2641  else if (iptr->sx.val.i == 0x04000000)
2642  iptr->sx.val.i = 26;
2643  else if (iptr->sx.val.i == 0x08000000)
2644  iptr->sx.val.i = 27;
2645  else if (iptr->sx.val.i == 0x10000000)
2646  iptr->sx.val.i = 28;
2647  else if (iptr->sx.val.i == 0x20000000)
2648  iptr->sx.val.i = 29;
2649  else if (iptr->sx.val.i == 0x40000000)
2650  iptr->sx.val.i = 30;
2651  else if (iptr->sx.val.u == 0x80000000)
2652  iptr->sx.val.i = 31;
2653  else
2654  goto normal_ICONST;
2655 
2656  iptr->opc = ICMD_IDIVPOW2;
2657  goto icmd_iconst_tail;
2658 
2659  case ICMD_IREM:
2660  /*log_text("stack.c: ICMD_ICONST/ICMD_IREM");*/
2661  if ((iptr->sx.val.i == 0x00000002) ||
2662  (iptr->sx.val.i == 0x00000004) ||
2663  (iptr->sx.val.i == 0x00000008) ||
2664  (iptr->sx.val.i == 0x00000010) ||
2665  (iptr->sx.val.i == 0x00000020) ||
2666  (iptr->sx.val.i == 0x00000040) ||
2667  (iptr->sx.val.i == 0x00000080) ||
2668  (iptr->sx.val.i == 0x00000100) ||
2669  (iptr->sx.val.i == 0x00000200) ||
2670  (iptr->sx.val.i == 0x00000400) ||
2671  (iptr->sx.val.i == 0x00000800) ||
2672  (iptr->sx.val.i == 0x00001000) ||
2673  (iptr->sx.val.i == 0x00002000) ||
2674  (iptr->sx.val.i == 0x00004000) ||
2675  (iptr->sx.val.i == 0x00008000) ||
2676  (iptr->sx.val.i == 0x00010000) ||
2677  (iptr->sx.val.i == 0x00020000) ||
2678  (iptr->sx.val.i == 0x00040000) ||
2679  (iptr->sx.val.i == 0x00080000) ||
2680  (iptr->sx.val.i == 0x00100000) ||
2681  (iptr->sx.val.i == 0x00200000) ||
2682  (iptr->sx.val.i == 0x00400000) ||
2683  (iptr->sx.val.i == 0x00800000) ||
2684  (iptr->sx.val.i == 0x01000000) ||
2685  (iptr->sx.val.i == 0x02000000) ||
2686  (iptr->sx.val.i == 0x04000000) ||
2687  (iptr->sx.val.i == 0x08000000) ||
2688  (iptr->sx.val.i == 0x10000000) ||
2689  (iptr->sx.val.i == 0x20000000) ||
2690  (iptr->sx.val.i == 0x40000000) ||
2691  (iptr->sx.val.u == 0x80000000))
2692  {
2693  iptr->opc = ICMD_IREMPOW2;
2694  iptr->sx.val.i -= 1;
2695  goto icmd_iconst_tail;
2696  }
2697  goto normal_ICONST;
2698 #if SUPPORT_CONST_LOGICAL
2699  case ICMD_IAND:
2700  iptr->opc = ICMD_IANDCONST;
2701  goto icmd_iconst_tail;
2702 
2703  case ICMD_IOR:
2704  iptr->opc = ICMD_IORCONST;
2705  goto icmd_iconst_tail;
2706 
2707  case ICMD_IXOR:
2708  iptr->opc = ICMD_IXORCONST;
2709  goto icmd_iconst_tail;
2710 
2711 #endif /* SUPPORT_CONST_LOGICAL */
2712  case ICMD_ISHL:
2713  iptr->opc = ICMD_ISHLCONST;
2714  goto icmd_iconst_tail;
2715 
2716  case ICMD_ISHR:
2717  iptr->opc = ICMD_ISHRCONST;
2718  goto icmd_iconst_tail;
2719 
2720  case ICMD_IUSHR:
2721  iptr->opc = ICMD_IUSHRCONST;
2722  goto icmd_iconst_tail;
2723 #if SUPPORT_LONG_SHIFT
2724  case ICMD_LSHL:
2725  iptr->opc = ICMD_LSHLCONST;
2726  goto icmd_lconst_tail;
2727 
2728  case ICMD_LSHR:
2729  iptr->opc = ICMD_LSHRCONST;
2730  goto icmd_lconst_tail;
2731 
2732  case ICMD_LUSHR:
2733  iptr->opc = ICMD_LUSHRCONST;
2734  goto icmd_lconst_tail;
2735 #endif /* SUPPORT_LONG_SHIFT */
2736  case ICMD_IF_ICMPEQ:
2737  iptr[1].opc = ICMD_IFEQ;
2738  /* FALLTHROUGH */
2739 
2740  icmd_if_icmp_tail:
2741  /* set the constant for the following icmd */
2742  iptr[1].sx.val.i = iptr->sx.val.i;
2743 
2744  /* this instruction becomes a nop */
2745  iptr->opc = ICMD_NOP;
2746  goto icmd_NOP;
2747 
2748  case ICMD_IF_ICMPLT:
2749  iptr[1].opc = ICMD_IFLT;
2750  goto icmd_if_icmp_tail;
2751 
2752  case ICMD_IF_ICMPLE:
2753  iptr[1].opc = ICMD_IFLE;
2754  goto icmd_if_icmp_tail;
2755 
2756  case ICMD_IF_ICMPNE:
2757  iptr[1].opc = ICMD_IFNE;
2758  goto icmd_if_icmp_tail;
2759 
2760  case ICMD_IF_ICMPGT:
2761  iptr[1].opc = ICMD_IFGT;
2762  goto icmd_if_icmp_tail;
2763 
2764  case ICMD_IF_ICMPGE:
2765  iptr[1].opc = ICMD_IFGE;
2766  goto icmd_if_icmp_tail;
2767 
2768 #if SUPPORT_CONST_STORE
2769  case ICMD_IASTORE:
2770  case ICMD_BASTORE:
2771  case ICMD_CASTORE:
2772  case ICMD_SASTORE:
2773 # if SUPPORT_CONST_STORE_ZERO_ONLY
2774  if (iptr->sx.val.i != 0)
2775  goto normal_ICONST;
2776 # endif
2777  switch (iptr[1].opc) {
2778  case ICMD_IASTORE:
2779  iptr->opc = ICMD_IASTORECONST;
2780  iptr->flags.bits |= INS_FLAG_CHECK;
2781  break;
2782  case ICMD_BASTORE:
2783  iptr->opc = ICMD_BASTORECONST;
2784  iptr->flags.bits |= INS_FLAG_CHECK;
2785  break;
2786  case ICMD_CASTORE:
2787  iptr->opc = ICMD_CASTORECONST;
2788  iptr->flags.bits |= INS_FLAG_CHECK;
2789  break;
2790  case ICMD_SASTORE:
2791  iptr->opc = ICMD_SASTORECONST;
2792  iptr->flags.bits |= INS_FLAG_CHECK;
2793  break;
2794  default:
2795  break;
2796  }
2797 
2798  iptr[1].opc = ICMD_NOP;
2799 
2800  /* copy the constant to s3 */
2801  /* XXX constval -> astoreconstval? */
2802  iptr->sx.s23.s3.constval = iptr->sx.val.i;
2804  STATISTICS(count_pcmd_op++);
2805  break;
2806 
2807  case ICMD_PUTSTATIC:
2808  case ICMD_PUTFIELD:
2809 # if SUPPORT_CONST_STORE_ZERO_ONLY
2810  if (iptr->sx.val.i != 0)
2811  goto normal_ICONST;
2812 # endif
2813  /* XXX check field type? */
2814 
2815  /* copy the constant to s2 */
2816  /* XXX constval -> fieldconstval? */
2817  iptr->sx.s23.s2.constval = iptr->sx.val.i;
2818  // fallthrough!
2819 
2820  putconst_tail: {
2821  constant_FMIref *fmiref;
2822 
2823  /* set the field reference (s3) */
2824  if (iptr[1].flags.bits & INS_FLAG_UNRESOLVED) {
2825  iptr->sx.s23.s3.uf = iptr[1].sx.s23.s3.uf;
2826  iptr->flags.bits |= INS_FLAG_UNRESOLVED;
2827  fmiref = iptr->sx.s23.s3.uf->fieldref;
2828  }
2829  else {
2830  fmiref = iptr[1].sx.s23.s3.fmiref;
2831  iptr->sx.s23.s3.fmiref = fmiref;
2832  }
2833 
2834 #if defined(ENABLE_VERIFIER)
2835  Type expectedtype = fmiref->parseddesc.fd->type;
2836  switch (iptr[0].opc) {
2837  case ICMD_ICONST:
2838  if (expectedtype != TYPE_INT)
2839  return throw_stack_type_error(expectedtype);
2840  break;
2841  case ICMD_LCONST:
2842  if (expectedtype != TYPE_LNG)
2843  return throw_stack_type_error(expectedtype);
2844  break;
2845  case ICMD_ACONST:
2846  if (expectedtype != TYPE_ADR)
2847  return throw_stack_type_error(expectedtype);
2848  break;
2849  default:
2850  assert(false);
2851  break;
2852  }
2853 #endif /* defined(ENABLE_VERIFIER) */
2854 
2855  switch (iptr[1].opc) {
2856  case ICMD_PUTSTATIC:
2857  iptr->opc = ICMD_PUTSTATICCONST;
2858  OP0_0;
2859  break;
2860  case ICMD_PUTFIELD:
2861  iptr->opc = ICMD_PUTFIELDCONST;
2862  OP1_0(TYPE_ADR);
2863  break;
2864  default:
2865  break;
2866  }
2867 
2868  iptr[1].opc = ICMD_NOP;
2869  STATISTICS(count_pcmd_op++);
2870  break;
2871  }
2872 #endif /* SUPPORT_CONST_STORE */
2873 
2874  default:
2875  goto normal_ICONST;
2876  }
2877 
2878  /* if we get here, the ICONST has been optimized */
2879  break;
2880 
2881 normal_ICONST:
2882  /* normal case of an unoptimized ICONST */
2883  OP0_1(TYPE_INT);
2884  break;
2885 
2886  /************************** LCONST OPTIMIZATIONS **************************/
2887 
2888  case ICMD_LCONST:
2889  STATISTICS(count_pcmd_load++);
2890  if (len == 0)
2891  goto normal_LCONST;
2892 
2893  /* switch depending on the following instruction */
2894 
2895  switch (iptr[1].opc) {
2896 #if SUPPORT_LONG_ADD
2897  case ICMD_LADD:
2898  iptr->opc = ICMD_LADDCONST;
2899  /* FALLTHROUGH */
2900 
2901  icmd_lconst_tail:
2902  /* instruction of type LONG -> LONG */
2903  iptr[1].opc = ICMD_NOP;
2905  STATISTICS(count_pcmd_op++);
2906  break;
2907 
2908  case ICMD_LSUB:
2909  iptr->opc = ICMD_LSUBCONST;
2910  goto icmd_lconst_tail;
2911 
2912 #endif /* SUPPORT_LONG_ADD */
2913 #if SUPPORT_LONG_MUL && SUPPORT_CONST_MUL
2914  case ICMD_LMUL:
2915  iptr->opc = ICMD_LMULCONST;
2916  goto icmd_lconst_tail;
2917 #else /* SUPPORT_LONG_MUL && SUPPORT_CONST_MUL */
2918 # if SUPPORT_LONG_SHIFT
2919  case ICMD_LMUL:
2920  if (iptr->sx.val.l == 0x00000002)
2921  iptr->sx.val.i = 1;
2922  else if (iptr->sx.val.l == 0x00000004)
2923  iptr->sx.val.i = 2;
2924  else if (iptr->sx.val.l == 0x00000008)
2925  iptr->sx.val.i = 3;
2926  else if (iptr->sx.val.l == 0x00000010)
2927  iptr->sx.val.i = 4;
2928  else if (iptr->sx.val.l == 0x00000020)
2929  iptr->sx.val.i = 5;
2930  else if (iptr->sx.val.l == 0x00000040)
2931  iptr->sx.val.i = 6;
2932  else if (iptr->sx.val.l == 0x00000080)
2933  iptr->sx.val.i = 7;
2934  else if (iptr->sx.val.l == 0x00000100)
2935  iptr->sx.val.i = 8;
2936  else if (iptr->sx.val.l == 0x00000200)
2937  iptr->sx.val.i = 9;
2938  else if (iptr->sx.val.l == 0x00000400)
2939  iptr->sx.val.i = 10;
2940  else if (iptr->sx.val.l == 0x00000800)
2941  iptr->sx.val.i = 11;
2942  else if (iptr->sx.val.l == 0x00001000)
2943  iptr->sx.val.i = 12;
2944  else if (iptr->sx.val.l == 0x00002000)
2945  iptr->sx.val.i = 13;
2946  else if (iptr->sx.val.l == 0x00004000)
2947  iptr->sx.val.i = 14;
2948  else if (iptr->sx.val.l == 0x00008000)
2949  iptr->sx.val.i = 15;
2950  else if (iptr->sx.val.l == 0x00010000)
2951  iptr->sx.val.i = 16;
2952  else if (iptr->sx.val.l == 0x00020000)
2953  iptr->sx.val.i = 17;
2954  else if (iptr->sx.val.l == 0x00040000)
2955  iptr->sx.val.i = 18;
2956  else if (iptr->sx.val.l == 0x00080000)
2957  iptr->sx.val.i = 19;
2958  else if (iptr->sx.val.l == 0x00100000)
2959  iptr->sx.val.i = 20;
2960  else if (iptr->sx.val.l == 0x00200000)
2961  iptr->sx.val.i = 21;
2962  else if (iptr->sx.val.l == 0x00400000)
2963  iptr->sx.val.i = 22;
2964  else if (iptr->sx.val.l == 0x00800000)
2965  iptr->sx.val.i = 23;
2966  else if (iptr->sx.val.l == 0x01000000)
2967  iptr->sx.val.i = 24;
2968  else if (iptr->sx.val.l == 0x02000000)
2969  iptr->sx.val.i = 25;
2970  else if (iptr->sx.val.l == 0x04000000)
2971  iptr->sx.val.i = 26;
2972  else if (iptr->sx.val.l == 0x08000000)
2973  iptr->sx.val.i = 27;
2974  else if (iptr->sx.val.l == 0x10000000)
2975  iptr->sx.val.i = 28;
2976  else if (iptr->sx.val.l == 0x20000000)
2977  iptr->sx.val.i = 29;
2978  else if (iptr->sx.val.l == 0x40000000)
2979  iptr->sx.val.i = 30;
2980  else if (iptr->sx.val.l == 0x80000000)
2981  iptr->sx.val.i = 31;
2982  else {
2983  goto normal_LCONST;
2984  }
2985  iptr->opc = ICMD_LMULPOW2;
2986  goto icmd_lconst_tail;
2987 # endif /* SUPPORT_LONG_SHIFT */
2988 #endif /* SUPPORT_LONG_MUL && SUPPORT_CONST_MUL */
2989 #if SUPPORT_LONG_DIV_POW2
2990  case ICMD_LDIV:
2991  if (iptr->sx.val.l == 0x00000002)
2992  iptr->sx.val.i = 1;
2993  else if (iptr->sx.val.l == 0x00000004)
2994  iptr->sx.val.i = 2;
2995  else if (iptr->sx.val.l == 0x00000008)
2996  iptr->sx.val.i = 3;
2997  else if (iptr->sx.val.l == 0x00000010)
2998  iptr->sx.val.i = 4;
2999  else if (iptr->sx.val.l == 0x00000020)
3000  iptr->sx.val.i = 5;
3001  else if (iptr->sx.val.l == 0x00000040)
3002  iptr->sx.val.i = 6;
3003  else if (iptr->sx.val.l == 0x00000080)
3004  iptr->sx.val.i = 7;
3005  else if (iptr->sx.val.l == 0x00000100)
3006  iptr->sx.val.i = 8;
3007  else if (iptr->sx.val.l == 0x00000200)
3008  iptr->sx.val.i = 9;
3009  else if (iptr->sx.val.l == 0x00000400)
3010  iptr->sx.val.i = 10;
3011  else if (iptr->sx.val.l == 0x00000800)
3012  iptr->sx.val.i = 11;
3013  else if (iptr->sx.val.l == 0x00001000)
3014  iptr->sx.val.i = 12;
3015  else if (iptr->sx.val.l == 0x00002000)
3016  iptr->sx.val.i = 13;
3017  else if (iptr->sx.val.l == 0x00004000)
3018  iptr->sx.val.i = 14;
3019  else if (iptr->sx.val.l == 0x00008000)
3020  iptr->sx.val.i = 15;
3021  else if (iptr->sx.val.l == 0x00010000)
3022  iptr->sx.val.i = 16;
3023  else if (iptr->sx.val.l == 0x00020000)
3024  iptr->sx.val.i = 17;
3025  else if (iptr->sx.val.l == 0x00040000)
3026  iptr->sx.val.i = 18;
3027  else if (iptr->sx.val.l == 0x00080000)
3028  iptr->sx.val.i = 19;
3029  else if (iptr->sx.val.l == 0x00100000)
3030  iptr->sx.val.i = 20;
3031  else if (iptr->sx.val.l == 0x00200000)
3032  iptr->sx.val.i = 21;
3033  else if (iptr->sx.val.l == 0x00400000)
3034  iptr->sx.val.i = 22;
3035  else if (iptr->sx.val.l == 0x00800000)
3036  iptr->sx.val.i = 23;
3037  else if (iptr->sx.val.l == 0x01000000)
3038  iptr->sx.val.i = 24;
3039  else if (iptr->sx.val.l == 0x02000000)
3040  iptr->sx.val.i = 25;
3041  else if (iptr->sx.val.l == 0x04000000)
3042  iptr->sx.val.i = 26;
3043  else if (iptr->sx.val.l == 0x08000000)
3044  iptr->sx.val.i = 27;
3045  else if (iptr->sx.val.l == 0x10000000)
3046  iptr->sx.val.i = 28;
3047  else if (iptr->sx.val.l == 0x20000000)
3048  iptr->sx.val.i = 29;
3049  else if (iptr->sx.val.l == 0x40000000)
3050  iptr->sx.val.i = 30;
3051  else if (iptr->sx.val.l == 0x80000000)
3052  iptr->sx.val.i = 31;
3053  else {
3054  goto normal_LCONST;
3055  }
3056  iptr->opc = ICMD_LDIVPOW2;
3057  goto icmd_lconst_tail;
3058 #endif /* SUPPORT_LONG_DIV_POW2 */
3059 
3060 #if SUPPORT_LONG_REM_POW2
3061  case ICMD_LREM:
3062  if ((iptr->sx.val.l == 0x00000002) ||
3063  (iptr->sx.val.l == 0x00000004) ||
3064  (iptr->sx.val.l == 0x00000008) ||
3065  (iptr->sx.val.l == 0x00000010) ||
3066  (iptr->sx.val.l == 0x00000020) ||
3067  (iptr->sx.val.l == 0x00000040) ||
3068  (iptr->sx.val.l == 0x00000080) ||
3069  (iptr->sx.val.l == 0x00000100) ||
3070  (iptr->sx.val.l == 0x00000200) ||
3071  (iptr->sx.val.l == 0x00000400) ||
3072  (iptr->sx.val.l == 0x00000800) ||
3073  (iptr->sx.val.l == 0x00001000) ||
3074  (iptr->sx.val.l == 0x00002000) ||
3075  (iptr->sx.val.l == 0x00004000) ||
3076  (iptr->sx.val.l == 0x00008000) ||
3077  (iptr->sx.val.l == 0x00010000) ||
3078  (iptr->sx.val.l == 0x00020000) ||
3079  (iptr->sx.val.l == 0x00040000) ||
3080  (iptr->sx.val.l == 0x00080000) ||
3081  (iptr->sx.val.l == 0x00100000) ||
3082  (iptr->sx.val.l == 0x00200000) ||
3083  (iptr->sx.val.l == 0x00400000) ||
3084  (iptr->sx.val.l == 0x00800000) ||
3085  (iptr->sx.val.l == 0x01000000) ||
3086  (iptr->sx.val.l == 0x02000000) ||
3087  (iptr->sx.val.l == 0x04000000) ||
3088  (iptr->sx.val.l == 0x08000000) ||
3089  (iptr->sx.val.l == 0x10000000) ||
3090  (iptr->sx.val.l == 0x20000000) ||
3091  (iptr->sx.val.l == 0x40000000) ||
3092  (iptr->sx.val.l == 0x80000000))
3093  {
3094  iptr->opc = ICMD_LREMPOW2;
3095  iptr->sx.val.l -= 1;
3096  goto icmd_lconst_tail;
3097  }
3098  goto normal_LCONST;
3099 #endif /* SUPPORT_LONG_REM_POW2 */
3100 
3101 #if SUPPORT_CONST_LOGICAL
3102 
3103  case ICMD_LAND:
3104  iptr->opc = ICMD_LANDCONST;
3105  goto icmd_lconst_tail;
3106 
3107  case ICMD_LOR:
3108  iptr->opc = ICMD_LORCONST;
3109  goto icmd_lconst_tail;
3110 
3111  case ICMD_LXOR:
3112  iptr->opc = ICMD_LXORCONST;
3113  goto icmd_lconst_tail;
3114 #endif
3115 
3116  case ICMD_LCMP:
3117  if ((len <= 1) || (iptr[2].sx.val.i != 0))
3118  goto normal_LCONST;
3119 
3120  /* switch on the instruction after LCONST - LCMP */
3121 
3122  switch (iptr[2].opc) {
3123  case ICMD_IFEQ:
3124  iptr->opc = ICMD_IF_LEQ;
3125  /* FALLTHROUGH */
3126 
3127  icmd_lconst_lcmp_tail:
3128  /* convert LCONST, LCMP, IFXX to IF_LXX */
3129  iptr->dst.block = iptr[2].dst.block;
3130  iptr[1].opc = ICMD_NOP;
3131  iptr[2].opc = ICMD_NOP;
3132 
3134  BRANCH(tbptr);
3135  STATISTICS(count_pcmd_bra++);
3136  STATISTICS(count_pcmd_op++);
3137  break;
3138 
3139  case ICMD_IFNE:
3140  iptr->opc = ICMD_IF_LNE;
3141  goto icmd_lconst_lcmp_tail;
3142 
3143  case ICMD_IFLT:
3144  iptr->opc = ICMD_IF_LLT;
3145  goto icmd_lconst_lcmp_tail;
3146 
3147  case ICMD_IFGT:
3148  iptr->opc = ICMD_IF_LGT;
3149  goto icmd_lconst_lcmp_tail;
3150 
3151  case ICMD_IFLE:
3152  iptr->opc = ICMD_IF_LLE;
3153  goto icmd_lconst_lcmp_tail;
3154 
3155  case ICMD_IFGE:
3156  iptr->opc = ICMD_IF_LGE;
3157  goto icmd_lconst_lcmp_tail;
3158 
3159  default:
3160  goto normal_LCONST;
3161  } /* end switch on opcode after LCONST - LCMP */
3162  break;
3163 
3164 #if SUPPORT_CONST_STORE
3165  case ICMD_LASTORE:
3166 # if SUPPORT_CONST_STORE_ZERO_ONLY
3167  if (iptr->sx.val.l != 0)
3168  goto normal_LCONST;
3169 # endif
3170 #if SIZEOF_VOID_P == 4
3171  /* the constant must fit into a ptrint */
3172  if (iptr->sx.val.l < -0x80000000L || iptr->sx.val.l >= 0x80000000L)
3173  goto normal_LCONST;
3174 #endif
3175  /* move the constant to s3 */
3176  iptr->sx.s23.s3.constval = iptr->sx.val.l;
3177 
3178  iptr->opc = ICMD_LASTORECONST;
3179  iptr->flags.bits |= INS_FLAG_CHECK;
3181 
3182  iptr[1].opc = ICMD_NOP;
3183  STATISTICS(count_pcmd_op++);
3184  break;
3185 
3186  case ICMD_PUTSTATIC:
3187  case ICMD_PUTFIELD:
3188 # if SUPPORT_CONST_STORE_ZERO_ONLY
3189  if (iptr->sx.val.l != 0)
3190  goto normal_LCONST;
3191 # endif
3192 #if SIZEOF_VOID_P == 4
3193  /* the constant must fit into a ptrint */
3194  if (iptr->sx.val.l < -0x80000000L || iptr->sx.val.l >= 0x80000000L)
3195  goto normal_LCONST;
3196 #endif
3197  /* XXX check field type? */
3198 
3199  /* copy the constant to s2 */
3200  /* XXX constval -> fieldconstval? */
3201  iptr->sx.s23.s2.constval = iptr->sx.val.l;
3202 
3203  goto putconst_tail;
3204 
3205 #endif /* SUPPORT_CONST_STORE */
3206 
3207  default:
3208  goto normal_LCONST;
3209  } /* end switch opcode after LCONST */
3210 
3211  /* if we get here, the LCONST has been optimized */
3212  break;
3213 
3214 normal_LCONST:
3215  /* the normal case of an unoptimized LCONST */
3216  OP0_1(TYPE_LNG);
3217  break;
3218 
3219  /************************ END OF LCONST OPTIMIZATIONS *********************/
3220 
3221  case ICMD_FCONST:
3222  STATISTICS(count_pcmd_load++);
3223  OP0_1(TYPE_FLT);
3224  break;
3225 
3226  case ICMD_DCONST:
3227  STATISTICS(count_pcmd_load++);
3228  OP0_1(TYPE_DBL);
3229  break;
3230 
3231  /************************** ACONST OPTIMIZATIONS **************************/
3232 
3233  case ICMD_ACONST:
3234  coalescing_boundary = sd.new_elem;
3235  STATISTICS(count_pcmd_load++);
3236 #if SUPPORT_CONST_STORE
3237  /* We can only optimize if the ACONST is resolved
3238  * and there is an instruction after it. */
3239 
3240  if ((len == 0) || (iptr->flags.bits & INS_FLAG_UNRESOLVED))
3241  goto normal_ACONST;
3242 
3243  switch (iptr[1].opc) {
3244  case ICMD_AASTORE:
3245  /* We can only optimize for NULL values
3246  * here because otherwise a checkcast is
3247  * required. */
3248  if (iptr->sx.val.anyptr != NULL)
3249  goto normal_ACONST;
3250 
3251  /* copy the constant (NULL) to s3 */
3252  iptr->sx.s23.s3.constval = 0;
3253  iptr->opc = ICMD_AASTORECONST;
3254  iptr->flags.bits |= INS_FLAG_CHECK;
3256 
3257  iptr[1].opc = ICMD_NOP;
3258  STATISTICS(count_pcmd_op++);
3259  break;
3260 
3261  case ICMD_PUTSTATIC:
3262  case ICMD_PUTFIELD:
3263 # if SUPPORT_CONST_STORE_ZERO_ONLY
3264  if (iptr->sx.val.anyptr != NULL)
3265  goto normal_ACONST;
3266 # endif
3267  /* XXX check field type? */
3268  /* copy the constant to s2 */
3269  /* XXX constval -> fieldconstval? */
3270  iptr->sx.s23.s2.constval = (ptrint) iptr->sx.val.anyptr;
3271 
3272  goto putconst_tail;
3273 
3274  default:
3275  goto normal_ACONST;
3276  }
3277 
3278  /* if we get here the ACONST has been optimized */
3279  break;
3280 
3281 normal_ACONST:
3282 #endif /* SUPPORT_CONST_STORE */
3283  OP0_1(TYPE_ADR);
3284  break;
3285 
3286 
3287  /* pop 0 push 1 load */
3288 
3289  case ICMD_ILOAD:
3290  case ICMD_LLOAD:
3291  case ICMD_FLOAD:
3292  case ICMD_DLOAD:
3293  case ICMD_ALOAD:
3294  STATISTICS(count_load_instruction++);
3295  type = (Type) (opcode - ICMD_ILOAD);
3296 
3297  varindex = iptr->s1.varindex =
3298  jd->local_map[iptr->s1.varindex * 5 + type];
3299 
3300 #if defined(ENABLE_VERIFIER)
3301  if (sd.var[varindex].type == TYPE_RET) {
3302  exceptions_throw_verifyerror(m, "forbidden load of returnAddress");
3303  return false;
3304  }
3305 #endif
3306  LOAD(type, varindex);
3307  break;
3308 
3309  /* pop 2 push 1 */
3310 
3311  case ICMD_LALOAD:
3312  case ICMD_FALOAD:
3313  case ICMD_DALOAD:
3314  case ICMD_AALOAD:
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++);
3320  OP2_1(TYPE_ADR, TYPE_INT, opcode - ICMD_IALOAD);
3321  break;
3322 
3323  case ICMD_IALOAD:
3324  case ICMD_BALOAD:
3325  case ICMD_CALOAD:
3326  case ICMD_SALOAD:
3327  coalescing_boundary = sd.new_elem;
3328  iptr->flags.bits |= INS_FLAG_CHECK;
3329  STATISTICS(count_check_null++);
3330  STATISTICS(count_check_bound++);
3331  STATISTICS(count_pcmd_mem++);
3333  break;
3334 
3335  /* pop 0 push 0 iinc */
3336 
3337  case ICMD_IINC: {
3338  STATISTICS(count_store_depth[stackdepth]++);
3339  javaindex = iptr->s1.varindex;
3340  last_store_boundary[javaindex] = sd.new_elem;
3341 
3342  iptr->s1.varindex =
3343  jd->local_map[javaindex * 5 + TYPE_INT];
3344 
3345  copy = curstack;
3346  for (int i = stackdepth - 1; copy; i--, copy = copy->prev) {
3347  if ((copy->varkind == LOCALVAR) &&
3348  (jd->reverselocalmap[copy->varnum] == javaindex))
3349  {
3350  assert(IS_LOCALVAR(copy));
3351  SET_TEMPVAR(copy);
3352  }
3353  }
3354 
3355  iptr->dst.varindex = iptr->s1.varindex;
3356  break;
3357  }
3358 
3359  /* pop 1 push 0 store */
3360 
3361  case ICMD_ISTORE:
3362  case ICMD_LSTORE:
3363  case ICMD_FSTORE:
3364  case ICMD_DSTORE:
3365  case ICMD_ASTORE:
3366  REQUIRE(1);
3367 
3368  type = (Type) (opcode - ICMD_ISTORE);
3369  javaindex = iptr->dst.varindex;
3370  varindex = iptr->dst.varindex =
3371  jd->local_map[javaindex * 5 + type];
3372 
3373  COPY_VAL_AND_TYPE(sd, curstack->varnum, varindex);
3374 
3375  iptr->sx.s23.s3.javaindex = javaindex;
3376 
3377  if (curstack->type == TYPE_RET) {
3378  iptr->flags.bits |= INS_FLAG_RETADDR;
3379  iptr->sx.s23.s2.retaddrnr =
3380  JAVALOCAL_FROM_RETADDR(sd.var[varindex].vv.retaddr->nr);
3381  sd.javalocals[javaindex] = iptr->sx.s23.s2.retaddrnr;
3382  }
3383  else
3384  sd.javalocals[javaindex] = varindex;
3385 
3386  /* invalidate the following javalocal for 2-word types */
3387 
3388  if (IS_2_WORD_TYPE(type)) {
3389  sd.javalocals[javaindex+1] = jitdata::UNUSED;
3390  iptr->flags.bits |= INS_FLAG_KILL_NEXT;
3391  }
3392 
3393  /* invalidate 2-word types if second half was overwritten */
3394 
3395  if (javaindex > 0) {
3396  int i = sd.javalocals[javaindex-1];
3397 
3398  if (i >= 0 && IS_2_WORD_TYPE(sd.var[i].type)) {
3399  sd.javalocals[javaindex-1] = jitdata::UNUSED;
3400  iptr->flags.bits |= INS_FLAG_KILL_PREV;
3401  }
3402  }
3403 
3404  STATISTICS(count_pcmd_store++);
3405  STATISTICS(count_store_depth[stackdepth-1]++);
3406  STATISTICS(count_store_length[sd.new_elem - curstack]++);
3407 
3408  /* check for conflicts as described in Figure 5.2 */
3409 
3410  copy = curstack->prev;
3411  for (int i = stackdepth - 2; copy; i--, copy = copy->prev) {
3412  if ((copy->varkind == LOCALVAR) &&
3413  (jd->reverselocalmap[copy->varnum] == javaindex))
3414  {
3415  assert(IS_LOCALVAR(copy));
3416  SET_TEMPVAR(copy);
3417  }
3418  }
3419 
3420  /* if the variable is already coalesced, don't bother */
3421 
3422  /* We do not need to check against INOUT, as invars */
3423  /* are always before the coalescing boundary. */
3424 
3425  if (curstack->varkind == LOCALVAR)
3426  goto store_tail;
3427 
3428  /* there is no STORE Lj while curstack is live */
3429 
3430  if (curstack < last_store_boundary[javaindex])
3431  goto assume_conflict;
3432 
3433  /* curstack must be after the coalescing boundary */
3434 
3435  if (curstack < coalescing_boundary)
3436  goto assume_conflict;
3437 
3438  /* there is no DEF LOCALVAR(varindex) while curstack is live */
3439 
3440  copy = sd.new_elem; /* most recent stackslot created + 1 */
3441  while (--copy > curstack) {
3442  if (copy->varkind == LOCALVAR && jd->reverselocalmap[copy->varnum] == javaindex)
3443  goto assume_conflict;
3444  }
3445 
3446  /* coalesce the temporary variable with Lj */
3447  assert((curstack->varkind == TEMPVAR)
3448  || (curstack->varkind == UNDEFVAR));
3449  assert(!IS_LOCALVAR(curstack)); /* XXX correct? */
3450  assert(!IS_INOUT(curstack));
3451  assert(!IS_PREALLOC(curstack));
3452 
3453  assert(curstack->creator);
3454  assert(curstack->creator->dst.varindex == curstack->varnum);
3455  assert(!(curstack->flags & PASSTHROUGH));
3456  RELEASE_INDEX(sd, curstack);
3457  curstack->varkind = LOCALVAR;
3458  curstack->varnum = varindex;
3459  curstack->creator->dst.varindex = varindex;
3460  goto store_tail;
3461 
3462  /* revert the coalescing, if it has been done earlier */
3463 assume_conflict:
3464  if ((curstack->varkind == LOCALVAR)
3465  && (jd->reverselocalmap[curstack->varnum] == javaindex))
3466  {
3467  assert(IS_LOCALVAR(curstack));
3468  SET_TEMPVAR(curstack);
3469  }
3470 
3471  /* remember the stack boundary at this store */
3472 store_tail:
3473  last_store_boundary[javaindex] = sd.new_elem;
3474 
3475  if (opcode == ICMD_ASTORE && curstack->type == TYPE_RET)
3476  STORE(TYPE_RET, varindex);
3477  else
3478  STORE(opcode - ICMD_ISTORE, varindex);
3479  break;
3480 
3481  /* pop 3 push 0 */
3482 
3483  case ICMD_AASTORE:
3484  coalescing_boundary = sd.new_elem;
3485  iptr->flags.bits |= INS_FLAG_CHECK;
3486  STATISTICS(count_check_null++);
3487  STATISTICS(count_check_bound++);
3488  STATISTICS(count_pcmd_mem++);
3489 
3491  md = bte->md;
3492 
3493  if (md->memuse > rd->memuse)
3494  rd->memuse = md->memuse;
3495  if (md->argintreguse > rd->argintreguse)
3496  rd->argintreguse = md->argintreguse;
3497  /* XXX non-leaf method? */
3498 
3499  /* make all stack variables saved */
3500 
3501  copy = curstack;
3502  while (copy) {
3503  sd.var[copy->varnum].flags |= SAVEDVAR;
3504  /* in case copy->varnum is/will be a LOCALVAR */
3505  /* once and set back to a non LOCALVAR */
3506  /* the correct SAVEDVAR flag has to be */
3507  /* remembered in copy->flags, too */
3508  copy->flags |= SAVEDVAR;
3509  copy = copy->prev;
3510  }
3511 
3513  break;
3514 
3515 
3516  case ICMD_LASTORE:
3517  case ICMD_FASTORE:
3518  case ICMD_DASTORE:
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++);
3524  OP3_0(TYPE_ADR, TYPE_INT, opcode - ICMD_IASTORE);
3525  break;
3526 
3527  case ICMD_IASTORE:
3528  case ICMD_BASTORE:
3529  case ICMD_CASTORE:
3530  case ICMD_SASTORE:
3531  coalescing_boundary = sd.new_elem;
3532  iptr->flags.bits |= INS_FLAG_CHECK;
3533  STATISTICS(count_check_null++);
3534  STATISTICS(count_check_bound++);
3535  STATISTICS(count_pcmd_mem++);
3537  break;
3538 
3539  /* pop 1 push 0 */
3540 
3541  case ICMD_POP:
3542 #ifdef ENABLE_VERIFIER
3543  if (opt_verify) {
3544  REQUIRE(1);
3545  if (IS_2_WORD_TYPE(curstack->type))
3546  return throw_stack_category_error();
3547  }
3548 #endif
3549  OP1_0_ANY;
3550  break;
3551 
3552  case ICMD_IRETURN:
3553  case ICMD_LRETURN:
3554  case ICMD_FRETURN:
3555  case ICMD_DRETURN:
3556  case ICMD_ARETURN:
3557  coalescing_boundary = sd.new_elem;
3558  /* Assert here that no LOCAL or INOUTS get */
3559  /* preallocated, since tha macros are not */
3560  /* available in md-abi.c! */
3561  if (IS_TEMPVAR(curstack))
3562  md_return_alloc(jd, curstack);
3563  STATISTICS(count_pcmd_return++);
3564  OP1_0(opcode - ICMD_IRETURN);
3565  superblockend = true;
3566  sd.jd->returncount++;
3567  sd.jd->returnblock = sd.bptr;
3568  break;
3569 
3570  case ICMD_ATHROW:
3571  coalescing_boundary = sd.new_elem;
3572  STATISTICS(count_check_null++);
3573  OP1_0(TYPE_ADR);
3574  curstack = NULL; stackdepth = 0;
3575  superblockend = true;
3576  break;
3577 
3578  case ICMD_PUTSTATIC: {
3579  coalescing_boundary = sd.new_elem;
3580  STATISTICS(count_pcmd_mem++);
3581  constant_FMIref *fmiref;
3582  INSTRUCTION_GET_FIELDREF(iptr, fmiref);
3583  OP1_0(fmiref->parseddesc.fd->type);
3584  break;
3585  }
3586 
3587  /* pop 1 push 0 branch */
3588 
3589  case ICMD_IFNULL:
3590  case ICMD_IFNONNULL:
3591  STATISTICS(count_pcmd_bra++);
3593  BRANCH(tbptr);
3594  break;
3595 
3596  case ICMD_IFEQ:
3597  case ICMD_IFNE:
3598  case ICMD_IFLT:
3599  case ICMD_IFGE:
3600  case ICMD_IFGT:
3601  case ICMD_IFLE:
3602  STATISTICS(count_pcmd_bra++);
3603  /* iptr->sx.val.i is set implicitly in parse by
3604  clearing the memory or from IF_ICMPxx
3605  optimization. */
3606 
3608 /* iptr->sx.val.i = 0; */
3609  BRANCH(tbptr);
3610  break;
3611 
3612  /* pop 0 push 0 branch */
3613 
3614  case ICMD_GOTO:
3615  STATISTICS(count_pcmd_bra++);
3616  OP0_BRANCH;
3617  BRANCH(tbptr);
3618  superblockend = true;
3619  break;
3620 
3621  /* pop 1 push 0 table branch */
3622 
3623  case ICMD_TABLESWITCH: {
3624  STATISTICS(count_pcmd_table++);
3626 
3627  table = iptr->dst.table;
3628  BRANCH_TARGET(*table, tbptr);
3629  table++;
3630 
3631  int i = iptr->sx.s23.s3.tablehigh
3632  - iptr->sx.s23.s2.tablelow + 1;
3633 
3634  while (--i >= 0) {
3635  BRANCH_TARGET(*table, tbptr);
3636  table++;
3637  }
3638  superblockend = true;
3639  break;
3640  }
3641 
3642  /* pop 1 push 0 table branch */
3643 
3644  case ICMD_LOOKUPSWITCH: {
3645  STATISTICS(count_pcmd_table++);
3647 
3648  BRANCH_TARGET(iptr->sx.s23.s3.lookupdefault, tbptr);
3649 
3650  lookup = iptr->dst.lookup;
3651 
3652  int i = iptr->sx.s23.s2.lookupcount;
3653 
3654  while (--i >= 0) {
3655  BRANCH_TARGET(lookup->target, tbptr);
3656  lookup++;
3657  }
3658  superblockend = true;
3659  break;
3660  }
3661 
3662  case ICMD_MONITORENTER:
3663  case ICMD_MONITOREXIT:
3664  coalescing_boundary = sd.new_elem;
3665  STATISTICS(count_check_null++);
3666  OP1_0(TYPE_ADR);
3667  break;
3668 
3669  /* pop 2 push 0 branch */
3670 
3671  case ICMD_IF_ICMPEQ:
3672  case ICMD_IF_ICMPNE:
3673  case ICMD_IF_ICMPLT:
3674  case ICMD_IF_ICMPGE:
3675  case ICMD_IF_ICMPGT:
3676  case ICMD_IF_ICMPLE:
3677  STATISTICS(count_pcmd_bra++);
3679  BRANCH(tbptr);
3680  break;
3681 
3682  case ICMD_IF_ACMPEQ:
3683  case ICMD_IF_ACMPNE:
3684  STATISTICS(count_pcmd_bra++);
3686  BRANCH(tbptr);
3687  break;
3688 
3689  /* pop 2 push 0 */
3690 
3691  case ICMD_PUTFIELD: {
3692  coalescing_boundary = sd.new_elem;
3693  STATISTICS(count_check_null++);
3694  STATISTICS(count_pcmd_mem++);
3695  constant_FMIref *fmiref;
3696  INSTRUCTION_GET_FIELDREF(iptr, fmiref);
3697  OP2_0(TYPE_ADR, fmiref->parseddesc.fd->type);
3698  break;
3699  }
3700 
3701  case ICMD_POP2:
3702  REQUIRE(1);
3703  if (!IS_2_WORD_TYPE(curstack->type)) {
3704  /* ..., cat1 */
3705 #ifdef ENABLE_VERIFIER
3706  if (opt_verify) {
3707  REQUIRE(2);
3708  if (IS_2_WORD_TYPE(curstack->prev->type))
3709  return throw_stack_category_error();
3710  }
3711 #endif
3712  OP2_0_ANY_ANY; /* pop two slots */
3713  }
3714  else {
3715  iptr->opc = ICMD_POP;
3716  OP1_0_ANY; /* pop one (two-word) slot */
3717  }
3718  break;
3719 
3720  /* pop 0 push 1 dup */
3721 
3722  case ICMD_DUP:
3723 #ifdef ENABLE_VERIFIER
3724  if (opt_verify) {
3725  REQUIRE(1);
3726  if (IS_2_WORD_TYPE(curstack->type))
3727  return throw_stack_category_error();
3728  }
3729 #endif
3730  STATISTICS(count_dup_instruction++);
3731 
3732 icmd_DUP:
3733  src1 = curstack;
3734 
3735  COPY_UP(src1);
3736  coalescing_boundary = sd.new_elem - 1;
3737  break;
3738 
3739  case ICMD_DUP2:
3740  REQUIRE(1);
3741  if (IS_2_WORD_TYPE(curstack->type)) {
3742  /* ..., cat2 */
3743  iptr->opc = ICMD_DUP;
3744  goto icmd_DUP;
3745  }
3746  else {
3747  REQUIRE(2);
3748  /* ..., ????, cat1 */
3749 #ifdef ENABLE_VERIFIER
3750  if (opt_verify) {
3751  if (IS_2_WORD_TYPE(curstack->prev->type))
3752  return throw_stack_category_error();
3753  }
3754 #endif
3755  src1 = curstack->prev;
3756  src2 = curstack;
3757 
3758  COPY_UP(src1); iptr++; len--;
3759  COPY_UP(src2);
3760 
3761  coalescing_boundary = sd.new_elem;
3762  }
3763  break;
3764 
3765  /* pop 2 push 3 dup */
3766 
3767  case ICMD_DUP_X1:
3768 #ifdef ENABLE_VERIFIER
3769  if (opt_verify) {
3770  REQUIRE(2);
3771  if (IS_2_WORD_TYPE(curstack->type) ||
3772  IS_2_WORD_TYPE(curstack->prev->type))
3773  return throw_stack_category_error();
3774  }
3775 #endif
3776 
3777 icmd_DUP_X1:
3778  src1 = curstack->prev;
3779  src2 = curstack;
3780  POPANY; POPANY;
3781  stackdepth -= 2;
3782 
3783  /* move non-temporary sources out of the way */
3784  if (!IS_TEMPVAR(src2)) {
3785  MOVE_TO_TEMP(src2); iptr++; len--;
3786  }
3787 
3788  DUP_SLOT(src2); dst1 = curstack; stackdepth++;
3789 
3790  MOVE_UP(src1); iptr++; len--;
3791  MOVE_UP(src2); iptr++; len--;
3792 
3793  COPY_DOWN(curstack, dst1);
3794 
3795  coalescing_boundary = sd.new_elem;
3796  break;
3797 
3798  case ICMD_DUP2_X1:
3799  REQUIRE(2);
3800  if (IS_2_WORD_TYPE(curstack->type)) {
3801  /* ..., ????, cat2 */
3802 #ifdef ENABLE_VERIFIER
3803  if (opt_verify) {
3804  if (IS_2_WORD_TYPE(curstack->prev->type))
3805  return throw_stack_category_error();
3806  }
3807 #endif
3808  iptr->opc = ICMD_DUP_X1;
3809  goto icmd_DUP_X1;
3810  }
3811  else {
3812  /* ..., ????, cat1 */
3813 #ifdef ENABLE_VERIFIER
3814  if (opt_verify) {
3815  REQUIRE(3);
3816  if (IS_2_WORD_TYPE(curstack->prev->type)
3817  || IS_2_WORD_TYPE(curstack->prev->prev->type))
3818  return throw_stack_category_error();
3819  }
3820 #endif
3821 
3822 icmd_DUP2_X1:
3823  src1 = curstack->prev->prev;
3824  src2 = curstack->prev;
3825  src3 = curstack;
3826  POPANY; POPANY; POPANY;
3827  stackdepth -= 3;
3828 
3829  /* move non-temporary sources out of the way */
3830  if (!IS_TEMPVAR(src2)) {
3831  MOVE_TO_TEMP(src2); iptr++; len--;
3832  }
3833  if (!IS_TEMPVAR(src3)) {
3834  MOVE_TO_TEMP(src3); iptr++; len--;
3835  }
3836 
3837  DUP_SLOT(src2); dst1 = curstack; stackdepth++;
3838  DUP_SLOT(src3); dst2 = curstack; stackdepth++;
3839 
3840  MOVE_UP(src1); iptr++; len--;
3841  MOVE_UP(src2); iptr++; len--;
3842  MOVE_UP(src3); iptr++; len--;
3843 
3844  COPY_DOWN(curstack, dst2); iptr++; len--;
3845  COPY_DOWN(curstack->prev, dst1);
3846 
3847  coalescing_boundary = sd.new_elem;
3848  }
3849  break;
3850 
3851  /* pop 3 push 4 dup */
3852 
3853  case ICMD_DUP_X2:
3854  REQUIRE(2);
3855  if (IS_2_WORD_TYPE(curstack->prev->type)) {
3856  /* ..., cat2, ???? */
3857 #ifdef ENABLE_VERIFIER
3858  if (opt_verify) {
3859  if (IS_2_WORD_TYPE(curstack->type))
3860  return throw_stack_category_error();
3861  }
3862 #endif
3863  iptr->opc = ICMD_DUP_X1;
3864  goto icmd_DUP_X1;
3865  }
3866  else {
3867  /* ..., cat1, ???? */
3868 #ifdef ENABLE_VERIFIER
3869  if (opt_verify) {
3870  REQUIRE(3);
3871  if (IS_2_WORD_TYPE(curstack->type)
3872  || IS_2_WORD_TYPE(curstack->prev->prev->type))
3873  return throw_stack_category_error();
3874  }
3875 #endif
3876 icmd_DUP_X2:
3877  src1 = curstack->prev->prev;
3878  src2 = curstack->prev;
3879  src3 = curstack;
3880  POPANY; POPANY; POPANY;
3881  stackdepth -= 3;
3882 
3883  /* move non-temporary sources out of the way */
3884  if (!IS_TEMPVAR(src2)) {
3885  MOVE_TO_TEMP(src2); iptr++; len--;
3886  }
3887  if (!IS_TEMPVAR(src3)) {
3888  MOVE_TO_TEMP(src3); iptr++; len--;
3889  }
3890 
3891  DUP_SLOT(src3); dst1 = curstack; stackdepth++;
3892 
3893  MOVE_UP(src1); iptr++; len--;
3894  MOVE_UP(src2); iptr++; len--;
3895  MOVE_UP(src3); iptr++; len--;
3896 
3897  COPY_DOWN(curstack, dst1);
3898 
3899  coalescing_boundary = sd.new_elem;
3900  }
3901  break;
3902 
3903  case ICMD_DUP2_X2:
3904  REQUIRE(2);
3905  if (IS_2_WORD_TYPE(curstack->type)) {
3906  /* ..., ????, cat2 */
3907  if (IS_2_WORD_TYPE(curstack->prev->type)) {
3908  /* ..., cat2, cat2 */
3909  iptr->opc = ICMD_DUP_X1;
3910  goto icmd_DUP_X1;
3911  }
3912  else {
3913  /* ..., cat1, cat2 */
3914 #ifdef ENABLE_VERIFIER
3915  if (opt_verify) {
3916  REQUIRE(3);
3917  if (IS_2_WORD_TYPE(curstack->prev->prev->type))
3918  return throw_stack_category_error();
3919  }
3920 #endif
3921  iptr->opc = ICMD_DUP_X2;
3922  goto icmd_DUP_X2;
3923  }
3924  }
3925 
3926  REQUIRE(3);
3927  /* ..., ????, ????, cat1 */
3928 
3929  if (IS_2_WORD_TYPE(curstack->prev->prev->type)) {
3930  /* ..., cat2, ????, cat1 */
3931 #ifdef ENABLE_VERIFIER
3932  if (opt_verify) {
3933  if (IS_2_WORD_TYPE(curstack->prev->type))
3934  return throw_stack_category_error();
3935  }
3936 #endif
3937  iptr->opc = ICMD_DUP2_X1;
3938  goto icmd_DUP2_X1;
3939  }
3940  else {
3941  /* ..., cat1, ????, cat1 */
3942 #ifdef ENABLE_VERIFIER
3943  if (opt_verify) {
3944  REQUIRE(4);
3945  if (IS_2_WORD_TYPE(curstack->prev->type)
3946  || IS_2_WORD_TYPE(curstack->prev->prev->prev->type))
3947  return throw_stack_category_error();
3948  }
3949 #endif
3950 
3951  src1 = curstack->prev->prev->prev;
3952  src2 = curstack->prev->prev;
3953  src3 = curstack->prev;
3954  src4 = curstack;
3956  stackdepth -= 4;
3957 
3958  /* move non-temporary sources out of the way */
3959  if (!IS_TEMPVAR(src2)) {
3960  MOVE_TO_TEMP(src2); iptr++; len--;
3961  }
3962  if (!IS_TEMPVAR(src3)) {
3963  MOVE_TO_TEMP(src3); iptr++; len--;
3964  }
3965  if (!IS_TEMPVAR(src4)) {
3966  MOVE_TO_TEMP(src4); iptr++; len--;
3967  }
3968 
3969  DUP_SLOT(src3); dst1 = curstack; stackdepth++;
3970  DUP_SLOT(src4); dst2 = curstack; stackdepth++;
3971 
3972  MOVE_UP(src1); iptr++; len--;
3973  MOVE_UP(src2); iptr++; len--;
3974  MOVE_UP(src3); iptr++; len--;
3975  MOVE_UP(src4); iptr++; len--;
3976 
3977  COPY_DOWN(curstack, dst2); iptr++; len--;
3978  COPY_DOWN(curstack->prev, dst1);
3979 
3980  coalescing_boundary = sd.new_elem;
3981  }
3982  break;
3983 
3984  /* pop 2 push 2 swap */
3985 
3986  case ICMD_SWAP:
3987 #ifdef ENABLE_VERIFIER
3988  if (opt_verify) {
3989  REQUIRE(2);
3990  if (IS_2_WORD_TYPE(curstack->type)
3991  || IS_2_WORD_TYPE(curstack->prev->type))
3992  return throw_stack_category_error();
3993  }
3994 #endif
3995 
3996  src1 = curstack->prev;
3997  src2 = curstack;
3998  POPANY; POPANY;
3999  stackdepth -= 2;
4000 
4001  /* move non-temporary sources out of the way */
4002  if (!IS_TEMPVAR(src1)) {
4003  MOVE_TO_TEMP(src1); iptr++; len--;
4004  }
4005 
4006  MOVE_UP(src2); iptr++; len--;
4007  MOVE_UP(src1);
4008 
4009  coalescing_boundary = sd.new_elem;
4010  break;
4011 
4012  /* pop 2 push 1 */
4013 
4014  case ICMD_IDIV:
4015  case ICMD_IREM:
4016  coalescing_boundary = sd.new_elem;
4017 #if !SUPPORT_DIVISION
4018  bte = iptr->sx.s23.s3.bte;
4019  md = bte->md;
4020 
4021  if (md->memuse > rd->memuse)
4022  rd->memuse = md->memuse;
4023  if (md->argintreguse > rd->argintreguse)
4024  rd->argintreguse = md->argintreguse;
4025 
4026  /* make all stack variables saved */
4027 
4028  copy = curstack;
4029  while (copy) {
4030  sd.var[copy->varnum].flags |= SAVEDVAR;
4031  copy->flags |= SAVEDVAR;
4032  copy = copy->prev;
4033  }
4034  /* FALLTHROUGH */
4035 
4036 #endif /* !SUPPORT_DIVISION */
4037 
4038  case ICMD_ISHL:
4039  case ICMD_ISHR:
4040  case ICMD_IUSHR:
4041  case ICMD_IADD:
4042  case ICMD_ISUB:
4043  case ICMD_IMUL:
4044  case ICMD_IAND:
4045  case ICMD_IOR:
4046  case ICMD_IXOR:
4047  STATISTICS(count_pcmd_op++);
4049  break;
4050 
4051  case ICMD_LDIV:
4052  case ICMD_LREM:
4053  coalescing_boundary = sd.new_elem;
4054 #if !(SUPPORT_DIVISION && SUPPORT_LONG_DIV)
4055  bte = iptr->sx.s23.s3.bte;
4056  md = bte->md;
4057 
4058  if (md->memuse > rd->memuse)
4059  rd->memuse = md->memuse;
4060  if (md->argintreguse > rd->argintreguse)
4061  rd->argintreguse = md->argintreguse;
4062  /* XXX non-leaf method? */
4063 
4064  /* make all stack variables saved */
4065 
4066  copy = curstack;
4067  while (copy) {
4068  sd.var[copy->varnum].flags |= SAVEDVAR;
4069  copy->flags |= SAVEDVAR;
4070  copy = copy->prev;
4071  }
4072  /* FALLTHROUGH */
4073 
4074 #endif
4075 
4076  case ICMD_LMUL:
4077  case ICMD_LADD:
4078  case ICMD_LSUB:
4079  case ICMD_LAND:
4080  case ICMD_LOR:
4081  case ICMD_LXOR:
4082  STATISTICS(count_pcmd_op++);
4084  break;
4085 
4086  case ICMD_LSHL:
4087  case ICMD_LSHR:
4088  case ICMD_LUSHR:
4089  STATISTICS(count_pcmd_op++);
4091  break;
4092 
4093  case ICMD_FADD:
4094  case ICMD_FSUB:
4095  case ICMD_FMUL:
4096  case ICMD_FDIV:
4097  case ICMD_FREM:
4098  STATISTICS(count_pcmd_op++);
4100  break;
4101 
4102  case ICMD_DADD:
4103  case ICMD_DSUB:
4104  case ICMD_DMUL:
4105  case ICMD_DDIV:
4106  case ICMD_DREM:
4107  STATISTICS(count_pcmd_op++);
4109  break;
4110 
4111  case ICMD_LCMP:
4112  STATISTICS(count_pcmd_op++);
4113  if ((len == 0) || (iptr[1].sx.val.i != 0))
4114  goto normal_LCMP;
4115 
4116  switch (iptr[1].opc) {
4117  case ICMD_IFEQ:
4118  iptr->opc = ICMD_IF_LCMPEQ;
4119  icmd_lcmp_if_tail:
4120  iptr->dst.block = iptr[1].dst.block;
4121  iptr[1].opc = ICMD_NOP;
4122 
4124  BRANCH(tbptr);
4125 
4126  STATISTICS(count_pcmd_bra++);
4127  break;
4128  case ICMD_IFNE:
4129  iptr->opc = ICMD_IF_LCMPNE;
4130  goto icmd_lcmp_if_tail;
4131  case ICMD_IFLT:
4132  iptr->opc = ICMD_IF_LCMPLT;
4133  goto icmd_lcmp_if_tail;
4134  case ICMD_IFGT:
4135  iptr->opc = ICMD_IF_LCMPGT;
4136  goto icmd_lcmp_if_tail;
4137  case ICMD_IFLE:
4138  iptr->opc = ICMD_IF_LCMPLE;
4139  goto icmd_lcmp_if_tail;
4140  case ICMD_IFGE:
4141  iptr->opc = ICMD_IF_LCMPGE;
4142  goto icmd_lcmp_if_tail;
4143  default:
4144  goto normal_LCMP;
4145  }
4146  break;
4147 normal_LCMP:
4149 
4150  iptr->opc = ICMD_BUILTIN;
4151  iptr->flags.bits &= INS_FLAG_ID_MASK;
4152  iptr->sx.s23.s3.bte = bte;
4153  /* iptr->line is already set */
4154  code_unflag_leafmethod(code);
4155  goto icmd_BUILTIN;
4156 
4157  break;
4158 
4159  case ICMD_FCMPL:
4160  case ICMD_FCMPG:
4161  STATISTICS(count_pcmd_op++);
4163  break;
4164 
4165  case ICMD_DCMPL:
4166  case ICMD_DCMPG:
4167  STATISTICS(count_pcmd_op++);
4169  break;
4170 
4171  /* pop 1 push 1 */
4172 
4173  case ICMD_INEG:
4174  case ICMD_INT2BYTE:
4175  case ICMD_INT2CHAR:
4176  case ICMD_INT2SHORT:
4177  STATISTICS(count_pcmd_op++);
4179  break;
4180  case ICMD_LNEG:
4181  STATISTICS(count_pcmd_op++);
4183  break;
4184  case ICMD_FNEG:
4185  STATISTICS(count_pcmd_op++);
4187  break;
4188  case ICMD_DNEG:
4189  STATISTICS(count_pcmd_op++);
4191  break;
4192 
4193  case ICMD_I2L:
4194  STATISTICS(count_pcmd_op++);
4196  break;
4197  case ICMD_I2F:
4198  STATISTICS(count_pcmd_op++);
4200  break;
4201  case ICMD_I2D:
4202  STATISTICS(count_pcmd_op++);
4204  break;
4205  case ICMD_L2I:
4206  STATISTICS(count_pcmd_op++);
4208  break;
4209  case ICMD_L2F:
4210  STATISTICS(count_pcmd_op++);
4212  break;
4213  case ICMD_L2D:
4214  STATISTICS(count_pcmd_op++);
4216  break;
4217  case ICMD_F2I:
4218  STATISTICS(count_pcmd_op++);
4220  break;
4221  case ICMD_F2L:
4222  STATISTICS(count_pcmd_op++);
4224  break;
4225  case ICMD_F2D:
4226  STATISTICS(count_pcmd_op++);
4228  break;
4229  case ICMD_D2I:
4230  STATISTICS(count_pcmd_op++);
4232  break;
4233  case ICMD_D2L:
4234  STATISTICS(count_pcmd_op++);
4236  break;
4237  case ICMD_D2F:
4238  STATISTICS(count_pcmd_op++);
4240  break;
4241 
4242  case ICMD_CHECKCAST:
4243  coalescing_boundary = sd.new_elem;
4244  if (iptr->flags.bits & INS_FLAG_ARRAY) {
4245  /* array type cast-check */
4246 
4248  md = bte->md;
4249 
4250  if (md->memuse > rd->memuse)
4251  rd->memuse = md->memuse;
4252  if (md->argintreguse > rd->argintreguse)
4253  rd->argintreguse = md->argintreguse;
4254 
4255  /* make all stack variables saved */
4256 
4257  copy = curstack;
4258  while (copy) {
4259  sd.var[copy->varnum].flags |= SAVEDVAR;
4260  copy->flags |= SAVEDVAR;
4261  copy = copy->prev;
4262  }
4263  }
4265  break;
4266 
4267  case ICMD_INSTANCEOF:
4268  case ICMD_ARRAYLENGTH:
4269  coalescing_boundary = sd.new_elem;
4271  break;
4272 
4273  case ICMD_NEWARRAY:
4274  case ICMD_ANEWARRAY:
4275  coalescing_boundary = sd.new_elem;
4277  break;
4278 
4279  case ICMD_GETFIELD: {
4280  coalescing_boundary = sd.new_elem;
4281  STATISTICS(count_check_null++);
4282  STATISTICS(count_pcmd_mem++);
4283  constant_FMIref *fmiref;
4284  INSTRUCTION_GET_FIELDREF(iptr, fmiref);
4285  OP1_1(TYPE_ADR, fmiref->parseddesc.fd->type);
4286  break;
4287  }
4288 
4289  /* pop 0 push 1 */
4290 
4291  case ICMD_GETSTATIC: {
4292  coalescing_boundary = sd.new_elem;
4293  STATISTICS(count_pcmd_mem++);
4294  constant_FMIref *fmiref;
4295  INSTRUCTION_GET_FIELDREF(iptr, fmiref);
4296  OP0_1(fmiref->parseddesc.fd->type);
4297  break;
4298  }
4299 
4300  case ICMD_NEW:
4301  coalescing_boundary = sd.new_elem;
4302  OP0_1(TYPE_ADR);
4303  break;
4304 
4305  case ICMD_JSR:
4306  OP0_1(TYPE_RET);
4307 
4308  tbptr = iptr->sx.s23.s3.jsrtarget.block;
4309  tbptr->type = basicblock::TYPE_SBR;
4310 
4311  assert(sd.bptr->next); /* XXX exception */
4312  sd.var[curstack->varnum].vv.retaddr = sd.bptr->next;
4313 #if defined(ENABLE_VERIFIER)
4314  sd.var[curstack->varnum].SBRSTART = (void*) tbptr;
4315 #endif
4316 
4317  tbptr = stack_mark_reached(&sd, tbptr, curstack, stackdepth);
4318  if (tbptr == NULL)
4319  return false;
4320 
4321  iptr->sx.s23.s3.jsrtarget.block = tbptr;
4322 
4323  /* We need to check for overflow right here because
4324  * the pushed value is poped afterwards */
4325  CHECKOVERFLOW;
4326 
4327  superblockend = true;
4328  /* XXX should not be marked as interface, as it does not need to be */
4329  /* allocated. Same for the invar of the target. */
4330  break;
4331 
4332  /* pop many push any */
4333 
4334  case ICMD_BUILTIN:
4335  icmd_BUILTIN: {
4336  builtintable_entry *bte = iptr->sx.s23.s3.bte;
4337  md = bte->md;
4338  goto _callhandling;
4339  }
4340 
4341  case ICMD_INVOKESTATIC:
4342  case ICMD_INVOKESPECIAL:
4343  case ICMD_INVOKEVIRTUAL:
4344  case ICMD_INVOKEINTERFACE:
4345  STATISTICS(count_pcmd_met++);
4346 
4347  /* Check for functions to replace with builtin
4348  * functions. */
4349 
4351  goto icmd_BUILTIN;
4352 
4353  INSTRUCTION_GET_METHODDESC(iptr, md);
4354  /* XXX resurrect this COUNT? */
4355 /* if (lm->flags & ACC_STATIC) */
4356 /* {COUNT(count_check_null);} */
4357 
4358  _callhandling: {
4359 
4360  coalescing_boundary = sd.new_elem;
4361 
4362  const int paramcount = md->paramcount;
4363 
4364  if (md->memuse > rd->memuse)
4365  rd->memuse = md->memuse;
4366  if (md->argintreguse > rd->argintreguse)
4367  rd->argintreguse = md->argintreguse;
4368  if (md->argfltreguse > rd->argfltreguse)
4369  rd->argfltreguse = md->argfltreguse;
4370 
4371  REQUIRE(paramcount);
4372 
4373  iptr->s1.argcount = stackdepth;
4374  iptr->sx.s23.s2.args = DMNEW(s4, stackdepth);
4375 
4376  copy = curstack;
4377  for (int i = paramcount - 1; i >= 0; i--) {
4378  iptr->sx.s23.s2.args[i] = copy->varnum;
4379 
4380  /* do not change STACKVARs or LOCALVARS to ARGVAR*/
4381  /* -> won't help anyway */
4382  if (!(IS_INOUT(copy) || IS_LOCALVAR(copy))) {
4383 
4384 #if defined(SUPPORT_PASS_FLOATARGS_IN_INTREGS)
4385  /* If we pass float arguments in integer argument registers, we
4386  * are not allowed to precolor them here. Floats have to be moved
4387  * to this regs explicitly in codegen().
4388  * Only arguments that are passed by stack anyway can be precolored
4389  * (michi 2005/07/24) */
4390  if (!(sd.var[copy->varnum].flags & SAVEDVAR) &&
4391  (!IS_FLT_DBL_TYPE(copy->type)
4392  || md->params[i].inmemory)) {
4393 #else
4394  if (!(sd.var[copy->varnum].flags & SAVEDVAR)) {
4395 #endif
4396 
4397  SET_PREALLOC(copy);
4398 
4399  if (md->params[i].inmemory) {
4400  sd.var[copy->varnum].vv.regoff =
4401  md->params[i].regoff;
4402  sd.var[copy->varnum].flags |=
4403  INMEMORY;
4404  }
4405  else {
4406  if (IS_FLT_DBL_TYPE(copy->type)) {
4407 #if defined(SUPPORT_PASS_FLOATARGS_IN_INTREGS)
4408  assert(0); /* XXX is this assert ok? */
4409 #else
4410  sd.var[copy->varnum].vv.regoff =
4411  md->params[i].regoff;
4412 #endif /* SUPPORT_PASS_FLOATARGS_IN_INTREGS */
4413  }
4414  else {
4415 #if defined(SUPPORT_COMBINE_INTEGER_REGISTERS)
4416  if (IS_2_WORD_TYPE(copy->type))
4417  sd.var[copy->varnum].vv.regoff =
4419  GET_HIGH_REG(md->params[i].regoff));
4420 
4421  else
4422 #endif /* SUPPORT_COMBINE_INTEGER_REGISTERS */
4423  sd.var[copy->varnum].vv.regoff =
4424  md->params[i].regoff;
4425  }
4426  }
4427  }
4428  }
4429  copy = copy->prev;
4430  }
4431 
4432  /* deal with live-through stack slots "under" the */
4433  /* arguments */
4434 
4435  for (int i = paramcount; copy; i++) {
4436  iptr->sx.s23.s2.args[i] = copy->varnum;
4437  sd.var[copy->varnum].flags |= SAVEDVAR;
4438  copy->flags |= SAVEDVAR | PASSTHROUGH;
4439  copy = copy->prev;
4440  }
4441 
4442  /* pop the arguments */
4443 
4444  int i = paramcount;
4445 
4446  stackdepth -= i;
4447  while (--i >= 0) {
4448  POPANY;
4449  }
4450 
4451  /* push the return value */
4452 
4453  if (md->returntype.type != TYPE_VOID) {
4454  GET_NEW_VAR(sd, new_index, md->returntype.type);
4455  DST(md->returntype.type, new_index);
4456  stackdepth++;
4457  }
4458  break;
4459  }
4460 
4461  case ICMD_MULTIANEWARRAY: {
4462  coalescing_boundary = sd.new_elem;
4463  if (rd->argintreguse < MIN(3, INT_ARG_CNT))
4464  rd->argintreguse = MIN(3, INT_ARG_CNT);
4465 
4466  int i = iptr->s1.argcount;
4467 
4468  REQUIRE(i);
4469 
4470  iptr->sx.s23.s2.args = DMNEW(s4, i);
4471 
4472 #if defined(SPECIALMEMUSE)
4473 # if defined(__DARWIN__)
4474  if (rd->memuse < (i + INT_ARG_CNT +LA_SIZE_IN_POINTERS))
4476 # else
4477  if (rd->memuse < (i + LA_SIZE_IN_POINTERS + 3))
4478  rd->memuse = i + LA_SIZE_IN_POINTERS + 3;
4479 # endif
4480 #else
4481 # if defined(__I386__)
4482  if (rd->memuse < i + 3)
4483  rd->memuse = i + 3; /* n integer args spilled on stack */
4484 # elif defined(__MIPS__) && SIZEOF_VOID_P == 4
4485  if (rd->memuse < i + 2)
4486  rd->memuse = i + 2; /* 4*4 bytes callee save space */
4487 # else
4488  if (rd->memuse < i)
4489  rd->memuse = i; /* n integer args spilled on stack */
4490 # endif /* defined(__I386__) */
4491 #endif
4492  copy = curstack;
4493  while (--i >= 0) {
4494  /* check INT type here? Currently typecheck does this. */
4495  iptr->sx.s23.s2.args[i] = copy->varnum;
4496  if (!(sd.var[copy->varnum].flags & SAVEDVAR)
4497  && (!IS_INOUT(copy))
4498  && (!IS_LOCALVAR(copy)) ) {
4499  copy->varkind = ARGVAR;
4500  sd.var[copy->varnum].flags |=
4501  INMEMORY & PREALLOC;
4502 #if defined(SPECIALMEMUSE)
4503 # if defined(__DARWIN__)
4504  sd.var[copy->varnum].vv.regoff = i +
4505  LA_SIZE_IN_POINTERS + INT_ARG_CNT;
4506 # else
4507  sd.var[copy->varnum].vv.regoff = i +
4508  LA_SIZE_IN_POINTERS + 3;
4509 # endif
4510 #else
4511 # if defined(__I386__)
4512  sd.var[copy->varnum].vv.regoff = i + 3;
4513 # elif defined(__MIPS__) && SIZEOF_VOID_P == 4
4514  sd.var[copy->varnum].vv.regoff = i + 2;
4515 # else
4516  sd.var[copy->varnum].vv.regoff = i;
4517 # endif /* defined(__I386__) */
4518 #endif /* defined(SPECIALMEMUSE) */
4519  }
4520  copy = copy->prev;
4521  }
4522  while (copy) {
4523  sd.var[copy->varnum].flags |= SAVEDVAR;
4524  copy->flags |= SAVEDVAR;
4525  copy = copy->prev;
4526  }
4527 
4528  i = iptr->s1.argcount;
4529  stackdepth -= i;
4530  while (--i >= 0) {
4531  POPANY;
4532  }
4533  GET_NEW_VAR(sd, new_index, TYPE_ADR);
4534  DST(TYPE_ADR, new_index);
4535  stackdepth++;
4536  break;
4537  }
4538 
4539  default:
4540  exceptions_throw_internalerror("Unknown ICMD %d during stack analysis",
4541  opcode);
4542  return false;
4543  } /* switch */
4544 
4545  CHECKOVERFLOW;
4546 
4547  /* record live stackvars after the current instruction */
4548 
4549 #if defined(ENABLE_COMPILER2)
4550  if (iptr != NULL && instruction_has_side_effects(iptr)
4552  assert(stackdepth >= 0);
4553  assert((stackdepth == 0) == (curstack == NULL));
4554 
4555  iptr->stack_after = DMNEW(s4, stackdepth);
4556  iptr->stackdepth_after = stackdepth;
4557 
4558  stackelement_t *current_elem = curstack;
4559  for (int i = stackdepth - 1; current_elem; i--) {
4560  iptr->stack_after[i] = current_elem->varnum;
4561  current_elem = current_elem->prev;
4562  }
4563  }
4564 #endif
4565 
4566  iptr++;
4567  } /* while instructions */
4568 
4569  /* show state after last instruction */
4570 
4571 #if defined(STACK_VERBOSE)
4572  stack_verbose_show_state(&sd, NULL, curstack);
4573 #endif
4574 
4575  /* stack slots at basic block end become interfaces */
4576 
4577  sd.bptr->outdepth = stackdepth;
4578  sd.bptr->outvars = DMNEW(s4, stackdepth);
4579 
4580  int i = stackdepth - 1;
4581  for (copy = curstack; copy; i--, copy = copy->prev) {
4582  varinfo *v;
4583 
4584  /* with the new vars rd->interfaces will be removed */
4585  /* and all in and outvars have to be STACKVARS! */
4586  /* in the moment i.e. SWAP with in and out vars can */
4587  /* create an unresolvable conflict */
4588 
4589  SET_TEMPVAR(copy);
4590  type = copy->type;
4591 
4592  v = sd.var + copy->varnum;
4593  v->flags |= INOUT;
4594 
4595  /* do not allocate variables for returnAddresses */
4596 
4597  if (type != TYPE_RET) {
4598  if (jd->interface_map[i*5 + type].flags == jitdata::UNUSED) {
4599  /* no interface var until now for this depth and */
4600  /* type */
4601  jd->interface_map[i*5 + type].flags = v->flags;
4602  }
4603  else {
4604  jd->interface_map[i*5 + type].flags |= v->flags;
4605  }
4606  }
4607 
4608  sd.bptr->outvars[i] = copy->varnum;
4609  }
4610 
4611  /* check if interface slots at basic block begin must be saved */
4612 
4613  for (int i=0; i<sd.bptr->indepth; ++i) {
4614  varinfo *v = sd.var + sd.bptr->invars[i];
4615 
4616  type = v->type;
4617 
4618  if (type != TYPE_RET) {
4619  if (jd->interface_map[i*5 + type].flags == jitdata::UNUSED) {
4620  /* no interface var until now for this depth and */
4621  /* type */
4622  jd->interface_map[i*5 + type].flags = v->flags;
4623  }
4624  else {
4625  jd->interface_map[i*5 + type].flags |= v->flags;
4626  }
4627  }
4628  }
4629 
4630  /* store the number of this block's variables */
4631 
4632  sd.bptr->varcount = sd.vartop - sd.bptr->varstart;
4633 
4634 #if defined(STACK_VERBOSE)
4635  stack_verbose_block_exit(&sd, superblockend);
4636 #endif
4637 
4638  /* reach the following block, if any */
4639 
4640  if (!superblockend)
4641  if (!stack_reach_next_block(&sd))
4642  return false;
4643 
4644  } /* for blocks */
4645 
4646  } while (sd.repeat && !deadcode);
4647 
4648  /* reset locals of TYPE_RET|VOID to TYPE_ADR */
4649 
4650  /* A local variable may be used as both a returnAddress and a reference */
4651  /* type variable, as we do not split variables between these types when */
4652  /* renaming locals. While returnAddresses have been eliminated now, we */
4653  /* must assume that the variable is still used as TYPE_ADR. */
4654  /* The only way that a local can be TYPE_VOID at this point, is that it */
4655  /* was a TYPE_RET variable for which incompatible returnAddresses were */
4656  /* merged. Thus we must treat TYPE_VOID in the same way as TYPE_RET */
4657  /* here. */
4658  /* XXX: It would be nice to remove otherwise unused returnAddress */
4659  /* variables from the local variable array, so they are not */
4660  /* allocated by simplereg. (For LSRA this is not needed). */
4661 
4662  for (int i=0; i<sd.localcount; ++i) {
4663  if (sd.var[i].type == TYPE_RET || sd.var[i].type == TYPE_VOID)
4664  sd.var[i].type = TYPE_ADR;
4665  }
4666 
4667  /* mark temporaries of TYPE_RET as PREALLOC to avoid allocation */
4668 
4669  for (int i=sd.localcount; i<sd.vartop; ++i) {
4670  if (sd.var[i].type == TYPE_RET)
4671  sd.var[i].flags |= PREALLOC;
4672  }
4673 
4674  /* XXX hack to fix up the ranges of the cloned single-block handlers */
4675 
4676  for (exception_entry *ex = jd->exceptiontable; ex != NULL; ex = ex->down) {
4677  if (ex->start == ex->end) {
4678  assert(ex->end->next);
4679  ex->end = ex->end->next;
4680  }
4681  }
4682 
4683  /* store number of created variables */
4684 
4685  jd->vartop = sd.vartop;
4686 
4687  /* gather statistics *****************************************************/
4688 
4689  STATISTICS(count_max_basic_blocks.max(jd->basicblockcount));
4690  STATISTICS(count_basic_blocks += jd->basicblockcount);
4691  STATISTICS(count_max_javainstr.max(jd->instructioncount));
4692  STATISTICS(count_javainstr += jd->instructioncount);
4693  STATISTICS(count_upper_bound_new_stack.max(jd->stackcount));
4694  STATISTICS(count_max_new_stack.max(sd.new_elem - jd->stack));
4695 
4696  STATISTICS(count_analyse_iterations[iteration_count]++);
4697  STATISTICS(count_method_bb_distribution[jd->basicblockcount]++);
4698 #if defined(ENABLE_STATISTICS)
4699  sd.bptr = jd->basicblocks;
4700  for (; sd.bptr; sd.bptr = sd.bptr->next) {
4701  if (sd.bptr->state > basicblock::REACHED) {
4702  STATISTICS(count_block_stack[sd.bptr->indepth]++);
4703  STATISTICS(count_block_size_distribution[sd.bptr->icount]++);
4704  }
4705  }
4706 #endif /* defined(ENABLE_STATISTICS) */
4707 
4708  /* everything's ok *******************************************************/
4709 
4710  return true;
4711 }
4712 
4713 
4714 /* stack_javalocals_store ******************************************************
4715 
4716  Model the effect of a ?STORE instruction upon the given javalocals array.
4717 
4718  IN:
4719  iptr.............the ?STORE instruction
4720  javalocals.......the javalocals array to modify
4721 
4722 *******************************************************************************/
4723 
4724 void stack_javalocals_store(instruction *iptr, s4 *javalocals)
4725 {
4726  s4 varindex = iptr->dst.varindex; // index into the jd->var array
4727  s4 javaindex = iptr->sx.s23.s3.javaindex; // java local index
4728 
4729  if (javaindex != jitdata::UNUSED) {
4730  assert(javaindex >= 0);
4731  if (iptr->flags.bits & INS_FLAG_RETADDR)
4732  javalocals[javaindex] = iptr->sx.s23.s2.retaddrnr;
4733  else
4734  javalocals[javaindex] = varindex;
4735 
4736  if (iptr->flags.bits & INS_FLAG_KILL_PREV)
4737  javalocals[javaindex-1] = jitdata::UNUSED;
4738 
4739  if (iptr->flags.bits & INS_FLAG_KILL_NEXT)
4740  javalocals[javaindex+1] = jitdata::UNUSED;
4741  }
4742 }
4743 
4744 
4745 /* functions for verbose stack analysis output ********************************/
4746 
4747 #if defined(STACK_VERBOSE)
4748 static void stack_verbose_show_varinfo(stackdata_t *sd, varinfo *v)
4749 {
4750  printf("%c", show_jit_type_letters[v->type]);
4751  if (v->type == TYPE_RET) {
4752  printf("{L%03d}", v->vv.retaddr->nr);
4753 #if defined(ENABLE_VERIFIER)
4754  printf("{start=L%03d}", ((basicblock *)v->SBRSTART)->nr);
4755 #endif
4756  }
4757 }
4758 
4759 
4760 static void stack_verbose_show_variable(stackdata_t *sd, s4 index)
4761 {
4762  assert(index >= 0 && index < sd->vartop);
4763  stack_verbose_show_varinfo(sd, sd->var + index);
4764 }
4765 
4766 
4767 static void stack_verbose_show_block(stackdata_t *sd, basicblock *bptr)
4768 {
4769  s4 i;
4770 
4771  printf("L%03d type:%d in:%d [", bptr->nr, bptr->type, bptr->indepth);
4772  if (bptr->invars) {
4773  for (i=0; i<bptr->indepth; ++i) {
4774  if (i)
4775  putchar(' ');
4776  stack_verbose_show_variable(sd, bptr->invars[i]);
4777  }
4778  }
4779  else
4780  putchar('-');
4781  printf("] javalocals ");
4783  printf(" inlocals [");
4784  if (bptr->inlocals) {
4785  for (i=0; i<sd->localcount; ++i) {
4786  if (i)
4787  putchar(' ');
4788  stack_verbose_show_varinfo(sd, bptr->inlocals + i);
4789  }
4790  }
4791  else
4792  putchar('-');
4793  printf("] out:%d [", bptr->outdepth);
4794  if (bptr->outvars) {
4795  for (i=0; i<bptr->outdepth; ++i) {
4796  if (i)
4797  putchar(' ');
4798  stack_verbose_show_variable(sd, bptr->outvars[i]);
4799  }
4800  }
4801  else
4802  putchar('-');
4803  printf("]");
4804 
4805  if (bptr->original)
4806  printf(" (clone of L%03d)", bptr->original->nr);
4807  else {
4808  basicblock *b = bptr->copied_to;
4809  if (b) {
4810  printf(" (copied to ");
4811  for (; b; b = b->copied_to)
4812  printf("L%03d ", b->nr);
4813  printf(")");
4814  }
4815  }
4816 }
4817 
4818 
4819 static void stack_verbose_block_enter(stackdata_t *sd, bool reanalyse)
4820 {
4821  int i;
4822 
4823  printf("======================================== STACK %sANALYSE BLOCK ",
4824  (reanalyse) ? ((sd->bptr->iinstr == NULL) ? "CLONE-" : "RE-") : "");
4825  stack_verbose_show_block(sd, sd->bptr);
4826  printf("\n");
4827 
4828  if (sd->handlers[0]) {
4829  printf("HANDLERS: ");
4830  for (i=0; sd->handlers[i]; ++i) {
4831  printf("L%03d ", sd->handlers[i]->handler->nr);
4832  }
4833  printf("\n");
4834  }
4835  printf("\n");
4836 }
4837 
4838 
4839 static void stack_verbose_block_exit(stackdata_t *sd, bool superblockend)
4840 {
4841  printf("%s ", (superblockend) ? "SUPERBLOCKEND" : "END");
4842  stack_verbose_show_block(sd, sd->bptr);
4843  printf("\n");
4844 }
4845 
4846 static void stack_verbose_show_state(stackdata_t *sd, instruction *iptr, stackelement_t *curstack)
4847 {
4848  stackelement_t *sp;
4849  s4 i;
4850  s4 depth;
4851  varinfo *v;
4853 
4854  printf(" javalocals ");
4856  printf(" stack [");
4857 
4858  for(i = 0, sp = curstack; sp; sp = sp->prev)
4859  i++;
4860  depth = i;
4861 
4862  stack = MNEW(stackelement_t *, depth);
4863  for(sp = curstack; sp; sp = sp->prev)
4864  stack[--i] = sp;
4865 
4866  for(i=0; i<depth; ++i) {
4867  if (i)
4868  putchar(' ');
4869  sp = stack[i];
4870  v = &(sd->var[sp->varnum]);
4871 
4872  if (v->flags & INOUT)
4873  putchar('I');
4874  if (v->flags & PREALLOC)
4875  putchar('A');
4876  printf("%d:%c", sp->varnum, show_jit_type_letters[sp->type]);
4877  if (v->type == TYPE_RET) {
4878  printf("(L%03d)", v->vv.retaddr->nr);
4879  }
4880  }
4881  printf("] ... ");
4882  if (iptr)
4883  show_icmd(sd->jd, iptr, false, SHOW_PARSE);
4884  printf("\n");
4885 }
4886 #endif
4887 
4888 
4889 /*
4890  * These are local overrides for various environment variables in Emacs.
4891  * Please do not remove this and leave it at the end of the file, where
4892  * Emacs will automagically detect them.
4893  * ---------------------------------------------------------------------
4894  * Local variables:
4895  * mode: c++
4896  * indent-tabs-mode: t
4897  * c-basic-offset: 4
4898  * tab-width: 4
4899  * End:
4900  * vim:noexpandtab:sw=4:ts=4:
4901  */
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:903
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
static bool instruction_has_side_effects(const instruction *iptr)
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:321
State state
Definition: jit.hpp:320
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:330
basicblock * next
Definition: jit.hpp:344
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:333
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:2034
s4 varsallocated
Definition: stack.cpp:163
#define JITDATA_HAS_FLAG_DEOPTIMIZE(jd)
Definition: jit.hpp:225
stackelement_t * prev
Definition: stack.hpp:64
#define BBFLAG_REPLACEMENT
Definition: jit.hpp:292
#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:160
Type type
Definition: reg.hpp:44
varinfo * inlocals
Definition: jit.hpp:328
#define GET_NEW_VAR(sd, newvarindex, newtype)
Definition: stack.cpp:261
s4 varcount
Definition: jit.hpp:335
lookup_target_t * lookup
instruction * iinstr
Definition: jit.hpp:326
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:325
#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:329
typedesc paramtypes[1]
Definition: descriptor.hpp:167
#define MCOPY(dest, src, type, num)
Definition: memory.hpp:103
#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:2156
basicblock * start
Definition: jit.hpp:241
alloc::list< PassInfo::IDTy >::type & stack
This file contains the statistics framework.
s4 predecessorcount
Definition: jit.hpp:337
#define INSTRUCTION_GET_FIELDREF(iptr, fref)
basicblock * handler
Definition: jit.hpp:243
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
#define MZERO(ptr, type, num)
Definition: memory.hpp:105
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:76
static basicblock * stack_mark_reached_from_outvars(stackdata_t *sd, basicblock *b)
Definition: stack.cpp:1303
classref_or_classinfo catchtype
Definition: jit.hpp:244
#define OP3_0(type1, type2, type3)
Definition: stack.cpp:530
s4 indepth
Definition: jit.hpp:332
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:345
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:331
#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:247
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:334
void stack_javalocals_store(instruction *iptr, s4 *javalocals)
Definition: stack.cpp:4724
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
#define MNEW(type, num)
Definition: memory.hpp:96
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:908
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
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:242
#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:319
s4 basicblockcount
Definition: jit.hpp:144
#define DMNEW(type, num)
Definition: dumpmemory.hpp:371
basicblock * original
Definition: jit.hpp:347
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:423
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:240
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:322
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:2112
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:2162
const char show_jit_type_letters[]
Definition: show.cpp:116
#define IS_PREALLOC(sp)
Definition: stack.cpp:273