Fri, 27 Feb 2009 13:27:09 -0800
6810672: Comment typos
Summary: I have collected some typos I have found while looking at the code.
Reviewed-by: kvn, never
1 /*
2 * Copyright 1997-2005 Sun Microsystems, Inc. All Rights Reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
20 * CA 95054 USA or visit www.sun.com if you need additional information or
21 * have any questions.
22 *
23 */
25 //
26 //
27 // Compute stack layouts for each instruction in method.
28 //
29 // Problems:
30 // - What to do about jsr with different types of local vars?
31 // Need maps that are conditional on jsr path?
32 // - Jsr and exceptions should be done more efficiently (the retAddr stuff)
33 //
34 // Alternative:
35 // - Could extend verifier to provide this information.
36 // For: one fewer abstract interpreter to maintain. Against: the verifier
37 // solves a bigger problem so slower (undesirable to force verification of
38 // everything?).
39 //
40 // Algorithm:
41 // Partition bytecodes into basic blocks
42 // For each basic block: store entry state (vars, stack). For instructions
43 // inside basic blocks we do not store any state (instead we recompute it
44 // from state produced by previous instruction).
45 //
46 // Perform abstract interpretation of bytecodes over this lattice:
47 //
48 // _--'#'--_
49 // / / \ \
50 // / / \ \
51 // / | | \
52 // 'r' 'v' 'p' ' '
53 // \ | | /
54 // \ \ / /
55 // \ \ / /
56 // -- '@' --
57 //
58 // '#' top, result of conflict merge
59 // 'r' reference type
60 // 'v' value type
61 // 'p' pc type for jsr/ret
62 // ' ' uninitialized; never occurs on operand stack in Java
63 // '@' bottom/unexecuted; initial state each bytecode.
64 //
65 // Basic block headers are the only merge points. We use this iteration to
66 // compute the information:
67 //
68 // find basic blocks;
69 // initialize them with uninitialized state;
70 // initialize first BB according to method signature;
71 // mark first BB changed
72 // while (some BB is changed) do {
73 // perform abstract interpration of all bytecodes in BB;
74 // merge exit state of BB into entry state of all successor BBs,
75 // noting if any of these change;
76 // }
77 //
78 // One additional complication is necessary. The jsr instruction pushes
79 // a return PC on the stack (a 'p' type in the abstract interpretation).
80 // To be able to process "ret" bytecodes, we keep track of these return
81 // PC's in a 'retAddrs' structure in abstract interpreter context (when
82 // processing a "ret" bytecodes, it is not sufficient to know that it gets
83 // an argument of the right type 'p'; we need to know which address it
84 // returns to).
85 //
86 // (Note this comment is borrowed form the original author of the algorithm)
88 #include "incls/_precompiled.incl"
89 #include "incls/_generateOopMap.cpp.incl"
91 // ComputeCallStack
92 //
93 // Specialization of SignatureIterator - compute the effects of a call
94 //
95 class ComputeCallStack : public SignatureIterator {
96 CellTypeState *_effect;
97 int _idx;
99 void setup();
100 void set(CellTypeState state) { _effect[_idx++] = state; }
101 int length() { return _idx; };
103 virtual void do_bool () { set(CellTypeState::value); };
104 virtual void do_char () { set(CellTypeState::value); };
105 virtual void do_float () { set(CellTypeState::value); };
106 virtual void do_byte () { set(CellTypeState::value); };
107 virtual void do_short () { set(CellTypeState::value); };
108 virtual void do_int () { set(CellTypeState::value); };
109 virtual void do_void () { set(CellTypeState::bottom);};
110 virtual void do_object(int begin, int end) { set(CellTypeState::ref); };
111 virtual void do_array (int begin, int end) { set(CellTypeState::ref); };
113 void do_double() { set(CellTypeState::value);
114 set(CellTypeState::value); }
115 void do_long () { set(CellTypeState::value);
116 set(CellTypeState::value); }
118 public:
119 ComputeCallStack(symbolOop signature) : SignatureIterator(signature) {};
121 // Compute methods
122 int compute_for_parameters(bool is_static, CellTypeState *effect) {
123 _idx = 0;
124 _effect = effect;
126 if (!is_static)
127 effect[_idx++] = CellTypeState::ref;
129 iterate_parameters();
131 return length();
132 };
134 int compute_for_returntype(CellTypeState *effect) {
135 _idx = 0;
136 _effect = effect;
137 iterate_returntype();
138 set(CellTypeState::bottom); // Always terminate with a bottom state, so ppush works
140 return length();
141 }
142 };
144 //=========================================================================================
145 // ComputeEntryStack
146 //
147 // Specialization of SignatureIterator - in order to set up first stack frame
148 //
149 class ComputeEntryStack : public SignatureIterator {
150 CellTypeState *_effect;
151 int _idx;
153 void setup();
154 void set(CellTypeState state) { _effect[_idx++] = state; }
155 int length() { return _idx; };
157 virtual void do_bool () { set(CellTypeState::value); };
158 virtual void do_char () { set(CellTypeState::value); };
159 virtual void do_float () { set(CellTypeState::value); };
160 virtual void do_byte () { set(CellTypeState::value); };
161 virtual void do_short () { set(CellTypeState::value); };
162 virtual void do_int () { set(CellTypeState::value); };
163 virtual void do_void () { set(CellTypeState::bottom);};
164 virtual void do_object(int begin, int end) { set(CellTypeState::make_slot_ref(_idx)); }
165 virtual void do_array (int begin, int end) { set(CellTypeState::make_slot_ref(_idx)); }
167 void do_double() { set(CellTypeState::value);
168 set(CellTypeState::value); }
169 void do_long () { set(CellTypeState::value);
170 set(CellTypeState::value); }
172 public:
173 ComputeEntryStack(symbolOop signature) : SignatureIterator(signature) {};
175 // Compute methods
176 int compute_for_parameters(bool is_static, CellTypeState *effect) {
177 _idx = 0;
178 _effect = effect;
180 if (!is_static)
181 effect[_idx++] = CellTypeState::make_slot_ref(0);
183 iterate_parameters();
185 return length();
186 };
188 int compute_for_returntype(CellTypeState *effect) {
189 _idx = 0;
190 _effect = effect;
191 iterate_returntype();
192 set(CellTypeState::bottom); // Always terminate with a bottom state, so ppush works
194 return length();
195 }
196 };
198 //=====================================================================================
199 //
200 // Implementation of RetTable/RetTableEntry
201 //
202 // Contains function to itereate through all bytecodes
203 // and find all return entry points
204 //
205 int RetTable::_init_nof_entries = 10;
206 int RetTableEntry::_init_nof_jsrs = 5;
208 void RetTableEntry::add_delta(int bci, int delta) {
209 if (_target_bci > bci) _target_bci += delta;
211 for (int k = 0; k < _jsrs->length(); k++) {
212 int jsr = _jsrs->at(k);
213 if (jsr > bci) _jsrs->at_put(k, jsr+delta);
214 }
215 }
217 void RetTable::compute_ret_table(methodHandle method) {
218 BytecodeStream i(method);
219 Bytecodes::Code bytecode;
221 while( (bytecode = i.next()) >= 0) {
222 switch (bytecode) {
223 case Bytecodes::_jsr:
224 add_jsr(i.next_bci(), i.dest());
225 break;
226 case Bytecodes::_jsr_w:
227 add_jsr(i.next_bci(), i.dest_w());
228 break;
229 }
230 }
231 }
233 void RetTable::add_jsr(int return_bci, int target_bci) {
234 RetTableEntry* entry = _first;
236 // Scan table for entry
237 for (;entry && entry->target_bci() != target_bci; entry = entry->next());
239 if (!entry) {
240 // Allocate new entry and put in list
241 entry = new RetTableEntry(target_bci, _first);
242 _first = entry;
243 }
245 // Now "entry" is set. Make sure that the entry is initialized
246 // and has room for the new jsr.
247 entry->add_jsr(return_bci);
248 }
250 RetTableEntry* RetTable::find_jsrs_for_target(int targBci) {
251 RetTableEntry *cur = _first;
253 while(cur) {
254 assert(cur->target_bci() != -1, "sanity check");
255 if (cur->target_bci() == targBci) return cur;
256 cur = cur->next();
257 }
258 ShouldNotReachHere();
259 return NULL;
260 }
262 // The instruction at bci is changing size by "delta". Update the return map.
263 void RetTable::update_ret_table(int bci, int delta) {
264 RetTableEntry *cur = _first;
265 while(cur) {
266 cur->add_delta(bci, delta);
267 cur = cur->next();
268 }
269 }
271 //
272 // Celltype state
273 //
275 CellTypeState CellTypeState::bottom = CellTypeState::make_bottom();
276 CellTypeState CellTypeState::uninit = CellTypeState::make_any(uninit_value);
277 CellTypeState CellTypeState::ref = CellTypeState::make_any(ref_conflict);
278 CellTypeState CellTypeState::value = CellTypeState::make_any(val_value);
279 CellTypeState CellTypeState::refUninit = CellTypeState::make_any(ref_conflict | uninit_value);
280 CellTypeState CellTypeState::top = CellTypeState::make_top();
281 CellTypeState CellTypeState::addr = CellTypeState::make_any(addr_conflict);
283 // Commonly used constants
284 static CellTypeState epsilonCTS[1] = { CellTypeState::bottom };
285 static CellTypeState refCTS = CellTypeState::ref;
286 static CellTypeState valCTS = CellTypeState::value;
287 static CellTypeState vCTS[2] = { CellTypeState::value, CellTypeState::bottom };
288 static CellTypeState rCTS[2] = { CellTypeState::ref, CellTypeState::bottom };
289 static CellTypeState rrCTS[3] = { CellTypeState::ref, CellTypeState::ref, CellTypeState::bottom };
290 static CellTypeState vrCTS[3] = { CellTypeState::value, CellTypeState::ref, CellTypeState::bottom };
291 static CellTypeState vvCTS[3] = { CellTypeState::value, CellTypeState::value, CellTypeState::bottom };
292 static CellTypeState rvrCTS[4] = { CellTypeState::ref, CellTypeState::value, CellTypeState::ref, CellTypeState::bottom };
293 static CellTypeState vvrCTS[4] = { CellTypeState::value, CellTypeState::value, CellTypeState::ref, CellTypeState::bottom };
294 static CellTypeState vvvCTS[4] = { CellTypeState::value, CellTypeState::value, CellTypeState::value, CellTypeState::bottom };
295 static CellTypeState vvvrCTS[5] = { CellTypeState::value, CellTypeState::value, CellTypeState::value, CellTypeState::ref, CellTypeState::bottom };
296 static CellTypeState vvvvCTS[5] = { CellTypeState::value, CellTypeState::value, CellTypeState::value, CellTypeState::value, CellTypeState::bottom };
298 char CellTypeState::to_char() const {
299 if (can_be_reference()) {
300 if (can_be_value() || can_be_address())
301 return '#'; // Conflict that needs to be rewritten
302 else
303 return 'r';
304 } else if (can_be_value())
305 return 'v';
306 else if (can_be_address())
307 return 'p';
308 else if (can_be_uninit())
309 return ' ';
310 else
311 return '@';
312 }
315 // Print a detailed CellTypeState. Indicate all bits that are set. If
316 // the CellTypeState represents an address or a reference, print the
317 // value of the additional information.
318 void CellTypeState::print(outputStream *os) {
319 if (can_be_address()) {
320 os->print("(p");
321 } else {
322 os->print("( ");
323 }
324 if (can_be_reference()) {
325 os->print("r");
326 } else {
327 os->print(" ");
328 }
329 if (can_be_value()) {
330 os->print("v");
331 } else {
332 os->print(" ");
333 }
334 if (can_be_uninit()) {
335 os->print("u|");
336 } else {
337 os->print(" |");
338 }
339 if (is_info_top()) {
340 os->print("Top)");
341 } else if (is_info_bottom()) {
342 os->print("Bot)");
343 } else {
344 if (is_reference()) {
345 int info = get_info();
346 int data = info & ~(ref_not_lock_bit | ref_slot_bit);
347 if (info & ref_not_lock_bit) {
348 // Not a monitor lock reference.
349 if (info & ref_slot_bit) {
350 // slot
351 os->print("slot%d)", data);
352 } else {
353 // line
354 os->print("line%d)", data);
355 }
356 } else {
357 // lock
358 os->print("lock%d)", data);
359 }
360 } else {
361 os->print("%d)", get_info());
362 }
363 }
364 }
366 //
367 // Basicblock handling methods
368 //
370 void GenerateOopMap ::initialize_bb() {
371 _gc_points = 0;
372 _bb_count = 0;
373 _bb_hdr_bits.clear();
374 _bb_hdr_bits.resize(method()->code_size());
375 }
377 void GenerateOopMap::bb_mark_fct(GenerateOopMap *c, int bci, int *data) {
378 assert(bci>= 0 && bci < c->method()->code_size(), "index out of bounds");
379 if (c->is_bb_header(bci))
380 return;
382 if (TraceNewOopMapGeneration) {
383 tty->print_cr("Basicblock#%d begins at: %d", c->_bb_count, bci);
384 }
385 c->set_bbmark_bit(bci);
386 c->_bb_count++;
387 }
390 void GenerateOopMap::mark_bbheaders_and_count_gc_points() {
391 initialize_bb();
393 bool fellThrough = false; // False to get first BB marked.
395 // First mark all exception handlers as start of a basic-block
396 typeArrayOop excps = method()->exception_table();
397 for(int i = 0; i < excps->length(); i += 4) {
398 int handler_pc_idx = i+2;
399 bb_mark_fct(this, excps->int_at(handler_pc_idx), NULL);
400 }
402 // Then iterate through the code
403 BytecodeStream bcs(_method);
404 Bytecodes::Code bytecode;
406 while( (bytecode = bcs.next()) >= 0) {
407 int bci = bcs.bci();
409 if (!fellThrough)
410 bb_mark_fct(this, bci, NULL);
412 fellThrough = jump_targets_do(&bcs, &GenerateOopMap::bb_mark_fct, NULL);
414 /* We will also mark successors of jsr's as basic block headers. */
415 switch (bytecode) {
416 case Bytecodes::_jsr:
417 assert(!fellThrough, "should not happen");
418 bb_mark_fct(this, bci + Bytecodes::length_for(bytecode), NULL);
419 break;
420 case Bytecodes::_jsr_w:
421 assert(!fellThrough, "should not happen");
422 bb_mark_fct(this, bci + Bytecodes::length_for(bytecode), NULL);
423 break;
424 }
426 if (possible_gc_point(&bcs))
427 _gc_points++;
428 }
429 }
431 void GenerateOopMap::reachable_basicblock(GenerateOopMap *c, int bci, int *data) {
432 assert(bci>= 0 && bci < c->method()->code_size(), "index out of bounds");
433 BasicBlock* bb = c->get_basic_block_at(bci);
434 if (bb->is_dead()) {
435 bb->mark_as_alive();
436 *data = 1; // Mark basicblock as changed
437 }
438 }
441 void GenerateOopMap::mark_reachable_code() {
442 int change = 1; // int to get function pointers to work
444 // Mark entry basic block as alive and all exception handlers
445 _basic_blocks[0].mark_as_alive();
446 typeArrayOop excps = method()->exception_table();
447 for(int i = 0; i < excps->length(); i += 4) {
448 int handler_pc_idx = i+2;
449 BasicBlock *bb = get_basic_block_at(excps->int_at(handler_pc_idx));
450 // If block is not already alive (due to multiple exception handlers to same bb), then
451 // make it alive
452 if (bb->is_dead()) bb->mark_as_alive();
453 }
455 BytecodeStream bcs(_method);
457 // Iterate through all basic blocks until we reach a fixpoint
458 while (change) {
459 change = 0;
461 for (int i = 0; i < _bb_count; i++) {
462 BasicBlock *bb = &_basic_blocks[i];
463 if (bb->is_alive()) {
464 // Position bytecodestream at last bytecode in basicblock
465 bcs.set_start(bb->_end_bci);
466 bcs.next();
467 Bytecodes::Code bytecode = bcs.code();
468 int bci = bcs.bci();
469 assert(bci == bb->_end_bci, "wrong bci");
471 bool fell_through = jump_targets_do(&bcs, &GenerateOopMap::reachable_basicblock, &change);
473 // We will also mark successors of jsr's as alive.
474 switch (bytecode) {
475 case Bytecodes::_jsr:
476 case Bytecodes::_jsr_w:
477 assert(!fell_through, "should not happen");
478 reachable_basicblock(this, bci + Bytecodes::length_for(bytecode), &change);
479 break;
480 }
481 if (fell_through) {
482 // Mark successor as alive
483 if (bb[1].is_dead()) {
484 bb[1].mark_as_alive();
485 change = 1;
486 }
487 }
488 }
489 }
490 }
491 }
493 /* If the current instruction in "c" has no effect on control flow,
494 returns "true". Otherwise, calls "jmpFct" one or more times, with
495 "c", an appropriate "pcDelta", and "data" as arguments, then
496 returns "false". There is one exception: if the current
497 instruction is a "ret", returns "false" without calling "jmpFct".
498 Arrangements for tracking the control flow of a "ret" must be made
499 externally. */
500 bool GenerateOopMap::jump_targets_do(BytecodeStream *bcs, jmpFct_t jmpFct, int *data) {
501 int bci = bcs->bci();
503 switch (bcs->code()) {
504 case Bytecodes::_ifeq:
505 case Bytecodes::_ifne:
506 case Bytecodes::_iflt:
507 case Bytecodes::_ifge:
508 case Bytecodes::_ifgt:
509 case Bytecodes::_ifle:
510 case Bytecodes::_if_icmpeq:
511 case Bytecodes::_if_icmpne:
512 case Bytecodes::_if_icmplt:
513 case Bytecodes::_if_icmpge:
514 case Bytecodes::_if_icmpgt:
515 case Bytecodes::_if_icmple:
516 case Bytecodes::_if_acmpeq:
517 case Bytecodes::_if_acmpne:
518 case Bytecodes::_ifnull:
519 case Bytecodes::_ifnonnull:
520 (*jmpFct)(this, bcs->dest(), data);
521 (*jmpFct)(this, bci + 3, data);
522 break;
524 case Bytecodes::_goto:
525 (*jmpFct)(this, bcs->dest(), data);
526 break;
527 case Bytecodes::_goto_w:
528 (*jmpFct)(this, bcs->dest_w(), data);
529 break;
530 case Bytecodes::_tableswitch:
531 { Bytecode_tableswitch *tableswitch = Bytecode_tableswitch_at(bcs->bcp());
532 int len = tableswitch->length();
534 (*jmpFct)(this, bci + tableswitch->default_offset(), data); /* Default. jump address */
535 while (--len >= 0) {
536 (*jmpFct)(this, bci + tableswitch->dest_offset_at(len), data);
537 }
538 break;
539 }
541 case Bytecodes::_lookupswitch:
542 { Bytecode_lookupswitch *lookupswitch = Bytecode_lookupswitch_at(bcs->bcp());
543 int npairs = lookupswitch->number_of_pairs();
544 (*jmpFct)(this, bci + lookupswitch->default_offset(), data); /* Default. */
545 while(--npairs >= 0) {
546 LookupswitchPair *pair = lookupswitch->pair_at(npairs);
547 (*jmpFct)(this, bci + pair->offset(), data);
548 }
549 break;
550 }
551 case Bytecodes::_jsr:
552 assert(bcs->is_wide()==false, "sanity check");
553 (*jmpFct)(this, bcs->dest(), data);
557 break;
558 case Bytecodes::_jsr_w:
559 (*jmpFct)(this, bcs->dest_w(), data);
560 break;
561 case Bytecodes::_wide:
562 ShouldNotReachHere();
563 return true;
564 break;
565 case Bytecodes::_athrow:
566 case Bytecodes::_ireturn:
567 case Bytecodes::_lreturn:
568 case Bytecodes::_freturn:
569 case Bytecodes::_dreturn:
570 case Bytecodes::_areturn:
571 case Bytecodes::_return:
572 case Bytecodes::_ret:
573 break;
574 default:
575 return true;
576 }
577 return false;
578 }
580 /* Requires "pc" to be the head of a basic block; returns that basic
581 block. */
582 BasicBlock *GenerateOopMap::get_basic_block_at(int bci) const {
583 BasicBlock* bb = get_basic_block_containing(bci);
584 assert(bb->_bci == bci, "should have found BB");
585 return bb;
586 }
588 // Requires "pc" to be the start of an instruction; returns the basic
589 // block containing that instruction. */
590 BasicBlock *GenerateOopMap::get_basic_block_containing(int bci) const {
591 BasicBlock *bbs = _basic_blocks;
592 int lo = 0, hi = _bb_count - 1;
594 while (lo <= hi) {
595 int m = (lo + hi) / 2;
596 int mbci = bbs[m]._bci;
597 int nbci;
599 if ( m == _bb_count-1) {
600 assert( bci >= mbci && bci < method()->code_size(), "sanity check failed");
601 return bbs+m;
602 } else {
603 nbci = bbs[m+1]._bci;
604 }
606 if ( mbci <= bci && bci < nbci) {
607 return bbs+m;
608 } else if (mbci < bci) {
609 lo = m + 1;
610 } else {
611 assert(mbci > bci, "sanity check");
612 hi = m - 1;
613 }
614 }
616 fatal("should have found BB");
617 return NULL;
618 }
620 void GenerateOopMap::restore_state(BasicBlock *bb)
621 {
622 memcpy(_state, bb->_state, _state_len*sizeof(CellTypeState));
623 _stack_top = bb->_stack_top;
624 _monitor_top = bb->_monitor_top;
625 }
627 int GenerateOopMap::next_bb_start_pc(BasicBlock *bb) {
628 int bbNum = bb - _basic_blocks + 1;
629 if (bbNum == _bb_count)
630 return method()->code_size();
632 return _basic_blocks[bbNum]._bci;
633 }
635 //
636 // CellType handling methods
637 //
639 void GenerateOopMap::init_state() {
640 _state_len = _max_locals + _max_stack + _max_monitors;
641 _state = NEW_RESOURCE_ARRAY(CellTypeState, _state_len);
642 memset(_state, 0, _state_len * sizeof(CellTypeState));
643 _state_vec_buf = NEW_RESOURCE_ARRAY(char, MAX3(_max_locals, _max_stack, _max_monitors) + 1/*for null terminator char */);
644 }
646 void GenerateOopMap::make_context_uninitialized() {
647 CellTypeState* vs = vars();
649 for (int i = 0; i < _max_locals; i++)
650 vs[i] = CellTypeState::uninit;
652 _stack_top = 0;
653 _monitor_top = 0;
654 }
656 int GenerateOopMap::methodsig_to_effect(symbolOop signature, bool is_static, CellTypeState* effect) {
657 ComputeEntryStack ces(signature);
658 return ces.compute_for_parameters(is_static, effect);
659 }
661 // Return result of merging cts1 and cts2.
662 CellTypeState CellTypeState::merge(CellTypeState cts, int slot) const {
663 CellTypeState result;
665 assert(!is_bottom() && !cts.is_bottom(),
666 "merge of bottom values is handled elsewhere");
668 result._state = _state | cts._state;
670 // If the top bit is set, we don't need to do any more work.
671 if (!result.is_info_top()) {
672 assert((result.can_be_address() || result.can_be_reference()),
673 "only addresses and references have non-top info");
675 if (!equal(cts)) {
676 // The two values being merged are different. Raise to top.
677 if (result.is_reference()) {
678 result = CellTypeState::make_slot_ref(slot);
679 } else {
680 result._state |= info_conflict;
681 }
682 }
683 }
684 assert(result.is_valid_state(), "checking that CTS merge maintains legal state");
686 return result;
687 }
689 // Merge the variable state for locals and stack from cts into bbts.
690 bool GenerateOopMap::merge_local_state_vectors(CellTypeState* cts,
691 CellTypeState* bbts) {
692 int i;
693 int len = _max_locals + _stack_top;
694 bool change = false;
696 for (i = len - 1; i >= 0; i--) {
697 CellTypeState v = cts[i].merge(bbts[i], i);
698 change = change || !v.equal(bbts[i]);
699 bbts[i] = v;
700 }
702 return change;
703 }
705 // Merge the monitor stack state from cts into bbts.
706 bool GenerateOopMap::merge_monitor_state_vectors(CellTypeState* cts,
707 CellTypeState* bbts) {
708 bool change = false;
709 if (_max_monitors > 0 && _monitor_top != bad_monitors) {
710 // If there are no monitors in the program, or there has been
711 // a monitor matching error before this point in the program,
712 // then we do not merge in the monitor state.
714 int base = _max_locals + _max_stack;
715 int len = base + _monitor_top;
716 for (int i = len - 1; i >= base; i--) {
717 CellTypeState v = cts[i].merge(bbts[i], i);
719 // Can we prove that, when there has been a change, it will already
720 // have been detected at this point? That would make this equal
721 // check here unnecessary.
722 change = change || !v.equal(bbts[i]);
723 bbts[i] = v;
724 }
725 }
727 return change;
728 }
730 void GenerateOopMap::copy_state(CellTypeState *dst, CellTypeState *src) {
731 int len = _max_locals + _stack_top;
732 for (int i = 0; i < len; i++) {
733 if (src[i].is_nonlock_reference()) {
734 dst[i] = CellTypeState::make_slot_ref(i);
735 } else {
736 dst[i] = src[i];
737 }
738 }
739 if (_max_monitors > 0 && _monitor_top != bad_monitors) {
740 int base = _max_locals + _max_stack;
741 len = base + _monitor_top;
742 for (int i = base; i < len; i++) {
743 dst[i] = src[i];
744 }
745 }
746 }
749 // Merge the states for the current block and the next. As long as a
750 // block is reachable the locals and stack must be merged. If the
751 // stack heights don't match then this is a verification error and
752 // it's impossible to interpret the code. Simultaneously monitor
753 // states are being check to see if they nest statically. If monitor
754 // depths match up then their states are merged. Otherwise the
755 // mismatch is simply recorded and interpretation continues since
756 // monitor matching is purely informational and doesn't say anything
757 // about the correctness of the code.
758 void GenerateOopMap::merge_state_into_bb(BasicBlock *bb) {
759 assert(bb->is_alive(), "merging state into a dead basicblock");
761 if (_stack_top == bb->_stack_top) {
762 // always merge local state even if monitors don't match.
763 if (merge_local_state_vectors(_state, bb->_state)) {
764 bb->set_changed(true);
765 }
766 if (_monitor_top == bb->_monitor_top) {
767 // monitors still match so continue merging monitor states.
768 if (merge_monitor_state_vectors(_state, bb->_state)) {
769 bb->set_changed(true);
770 }
771 } else {
772 if (TraceMonitorMismatch) {
773 report_monitor_mismatch("monitor stack height merge conflict");
774 }
775 // When the monitor stacks are not matched, we set _monitor_top to
776 // bad_monitors. This signals that, from here on, the monitor stack cannot
777 // be trusted. In particular, monitorexit bytecodes may throw
778 // exceptions. We mark this block as changed so that the change
779 // propagates properly.
780 bb->_monitor_top = bad_monitors;
781 bb->set_changed(true);
782 _monitor_safe = false;
783 }
784 } else if (!bb->is_reachable()) {
785 // First time we look at this BB
786 copy_state(bb->_state, _state);
787 bb->_stack_top = _stack_top;
788 bb->_monitor_top = _monitor_top;
789 bb->set_changed(true);
790 } else {
791 verify_error("stack height conflict: %d vs. %d", _stack_top, bb->_stack_top);
792 }
793 }
795 void GenerateOopMap::merge_state(GenerateOopMap *gom, int bci, int* data) {
796 gom->merge_state_into_bb(gom->get_basic_block_at(bci));
797 }
799 void GenerateOopMap::set_var(int localNo, CellTypeState cts) {
800 assert(cts.is_reference() || cts.is_value() || cts.is_address(),
801 "wrong celltypestate");
802 if (localNo < 0 || localNo > _max_locals) {
803 verify_error("variable write error: r%d", localNo);
804 return;
805 }
806 vars()[localNo] = cts;
807 }
809 CellTypeState GenerateOopMap::get_var(int localNo) {
810 assert(localNo < _max_locals + _nof_refval_conflicts, "variable read error")
811 if (localNo < 0 || localNo > _max_locals) {
812 verify_error("variable read error: r%d", localNo);
813 return valCTS; // just to pick something;
814 }
815 return vars()[localNo];
816 }
818 CellTypeState GenerateOopMap::pop() {
819 if ( _stack_top <= 0) {
820 verify_error("stack underflow");
821 return valCTS; // just to pick something
822 }
823 return stack()[--_stack_top];
824 }
826 void GenerateOopMap::push(CellTypeState cts) {
827 if ( _stack_top >= _max_stack) {
828 verify_error("stack overflow");
829 return;
830 }
831 stack()[_stack_top++] = cts;
832 }
834 CellTypeState GenerateOopMap::monitor_pop() {
835 assert(_monitor_top != bad_monitors, "monitor_pop called on error monitor stack");
836 if (_monitor_top == 0) {
837 // We have detected a pop of an empty monitor stack.
838 _monitor_safe = false;
839 _monitor_top = bad_monitors;
841 if (TraceMonitorMismatch) {
842 report_monitor_mismatch("monitor stack underflow");
843 }
844 return CellTypeState::ref; // just to keep the analysis going.
845 }
846 return monitors()[--_monitor_top];
847 }
849 void GenerateOopMap::monitor_push(CellTypeState cts) {
850 assert(_monitor_top != bad_monitors, "monitor_push called on error monitor stack");
851 if (_monitor_top >= _max_monitors) {
852 // Some monitorenter is being executed more than once.
853 // This means that the monitor stack cannot be simulated.
854 _monitor_safe = false;
855 _monitor_top = bad_monitors;
857 if (TraceMonitorMismatch) {
858 report_monitor_mismatch("monitor stack overflow");
859 }
860 return;
861 }
862 monitors()[_monitor_top++] = cts;
863 }
865 //
866 // Interpretation handling methods
867 //
869 void GenerateOopMap::do_interpretation()
870 {
871 // "i" is just for debugging, so we can detect cases where this loop is
872 // iterated more than once.
873 int i = 0;
874 do {
875 #ifndef PRODUCT
876 if (TraceNewOopMapGeneration) {
877 tty->print("\n\nIteration #%d of do_interpretation loop, method:\n", i);
878 method()->print_name(tty);
879 tty->print("\n\n");
880 }
881 #endif
882 _conflict = false;
883 _monitor_safe = true;
884 // init_state is now called from init_basic_blocks. The length of a
885 // state vector cannot be determined until we have made a pass through
886 // the bytecodes counting the possible monitor entries.
887 if (!_got_error) init_basic_blocks();
888 if (!_got_error) setup_method_entry_state();
889 if (!_got_error) interp_all();
890 if (!_got_error) rewrite_refval_conflicts();
891 i++;
892 } while (_conflict && !_got_error);
893 }
895 void GenerateOopMap::init_basic_blocks() {
896 // Note: Could consider reserving only the needed space for each BB's state
897 // (entry stack may not be of maximal height for every basic block).
898 // But cumbersome since we don't know the stack heights yet. (Nor the
899 // monitor stack heights...)
901 _basic_blocks = NEW_RESOURCE_ARRAY(BasicBlock, _bb_count);
903 // Make a pass through the bytecodes. Count the number of monitorenters.
904 // This can be used an upper bound on the monitor stack depth in programs
905 // which obey stack discipline with their monitor usage. Initialize the
906 // known information about basic blocks.
907 BytecodeStream j(_method);
908 Bytecodes::Code bytecode;
910 int bbNo = 0;
911 int monitor_count = 0;
912 int prev_bci = -1;
913 while( (bytecode = j.next()) >= 0) {
914 if (j.code() == Bytecodes::_monitorenter) {
915 monitor_count++;
916 }
918 int bci = j.bci();
919 if (is_bb_header(bci)) {
920 // Initialize the basicblock structure
921 BasicBlock *bb = _basic_blocks + bbNo;
922 bb->_bci = bci;
923 bb->_max_locals = _max_locals;
924 bb->_max_stack = _max_stack;
925 bb->set_changed(false);
926 bb->_stack_top = BasicBlock::_dead_basic_block; // Initialize all basicblocks are dead.
927 bb->_monitor_top = bad_monitors;
929 if (bbNo > 0) {
930 _basic_blocks[bbNo - 1]._end_bci = prev_bci;
931 }
933 bbNo++;
934 }
935 // Remember prevous bci.
936 prev_bci = bci;
937 }
938 // Set
939 _basic_blocks[bbNo-1]._end_bci = prev_bci;
942 // Check that the correct number of basicblocks was found
943 if (bbNo !=_bb_count) {
944 if (bbNo < _bb_count) {
945 verify_error("jump into the middle of instruction?");
946 return;
947 } else {
948 verify_error("extra basic blocks - should not happen?");
949 return;
950 }
951 }
953 _max_monitors = monitor_count;
955 // Now that we have a bound on the depth of the monitor stack, we can
956 // initialize the CellTypeState-related information.
957 init_state();
959 // We allocate space for all state-vectors for all basicblocks in one huge chuck.
960 // Then in the next part of the code, we set a pointer in each _basic_block that
961 // points to each piece.
962 CellTypeState *basicBlockState = NEW_RESOURCE_ARRAY(CellTypeState, bbNo * _state_len);
963 memset(basicBlockState, 0, bbNo * _state_len * sizeof(CellTypeState));
965 // Make a pass over the basicblocks and assign their state vectors.
966 for (int blockNum=0; blockNum < bbNo; blockNum++) {
967 BasicBlock *bb = _basic_blocks + blockNum;
968 bb->_state = basicBlockState + blockNum * _state_len;
970 #ifdef ASSERT
971 if (blockNum + 1 < bbNo) {
972 address bcp = _method->bcp_from(bb->_end_bci);
973 int bc_len = Bytecodes::java_length_at(bcp);
974 assert(bb->_end_bci + bc_len == bb[1]._bci, "unmatched bci info in basicblock");
975 }
976 #endif
977 }
978 #ifdef ASSERT
979 { BasicBlock *bb = &_basic_blocks[bbNo-1];
980 address bcp = _method->bcp_from(bb->_end_bci);
981 int bc_len = Bytecodes::java_length_at(bcp);
982 assert(bb->_end_bci + bc_len == _method->code_size(), "wrong end bci");
983 }
984 #endif
986 // Mark all alive blocks
987 mark_reachable_code();
988 }
990 void GenerateOopMap::setup_method_entry_state() {
992 // Initialize all locals to 'uninit' and set stack-height to 0
993 make_context_uninitialized();
995 // Initialize CellState type of arguments
996 methodsig_to_effect(method()->signature(), method()->is_static(), vars());
998 // If some references must be pre-assigned to null, then set that up
999 initialize_vars();
1001 // This is the start state
1002 merge_state_into_bb(&_basic_blocks[0]);
1004 assert(_basic_blocks[0].changed(), "we are not getting off the ground");
1005 }
1007 // The instruction at bci is changing size by "delta". Update the basic blocks.
1008 void GenerateOopMap::update_basic_blocks(int bci, int delta,
1009 int new_method_size) {
1010 assert(new_method_size >= method()->code_size() + delta,
1011 "new method size is too small");
1013 BitMap::bm_word_t* new_bb_hdr_bits =
1014 NEW_RESOURCE_ARRAY(BitMap::bm_word_t,
1015 BitMap::word_align_up(new_method_size));
1016 _bb_hdr_bits.set_map(new_bb_hdr_bits);
1017 _bb_hdr_bits.set_size(new_method_size);
1018 _bb_hdr_bits.clear();
1021 for(int k = 0; k < _bb_count; k++) {
1022 if (_basic_blocks[k]._bci > bci) {
1023 _basic_blocks[k]._bci += delta;
1024 _basic_blocks[k]._end_bci += delta;
1025 }
1026 _bb_hdr_bits.at_put(_basic_blocks[k]._bci, true);
1027 }
1028 }
1030 //
1031 // Initvars handling
1032 //
1034 void GenerateOopMap::initialize_vars() {
1035 for (int k = 0; k < _init_vars->length(); k++)
1036 _state[_init_vars->at(k)] = CellTypeState::make_slot_ref(k);
1037 }
1039 void GenerateOopMap::add_to_ref_init_set(int localNo) {
1041 if (TraceNewOopMapGeneration)
1042 tty->print_cr("Added init vars: %d", localNo);
1044 // Is it already in the set?
1045 if (_init_vars->contains(localNo) )
1046 return;
1048 _init_vars->append(localNo);
1049 }
1051 //
1052 // Interpreration code
1053 //
1055 void GenerateOopMap::interp_all() {
1056 bool change = true;
1058 while (change && !_got_error) {
1059 change = false;
1060 for (int i = 0; i < _bb_count && !_got_error; i++) {
1061 BasicBlock *bb = &_basic_blocks[i];
1062 if (bb->changed()) {
1063 if (_got_error) return;
1064 change = true;
1065 bb->set_changed(false);
1066 interp_bb(bb);
1067 }
1068 }
1069 }
1070 }
1072 void GenerateOopMap::interp_bb(BasicBlock *bb) {
1074 // We do not want to do anything in case the basic-block has not been initialized. This
1075 // will happen in the case where there is dead-code hang around in a method.
1076 assert(bb->is_reachable(), "should be reachable or deadcode exist");
1077 restore_state(bb);
1079 BytecodeStream itr(_method);
1081 // Set iterator interval to be the current basicblock
1082 int lim_bci = next_bb_start_pc(bb);
1083 itr.set_interval(bb->_bci, lim_bci);
1084 assert(lim_bci != bb->_bci, "must be at least one instruction in a basicblock");
1085 itr.next(); // read first instruction
1087 // Iterates through all bytecodes except the last in a basic block.
1088 // We handle the last one special, since there is controlflow change.
1089 while(itr.next_bci() < lim_bci && !_got_error) {
1090 if (_has_exceptions || _monitor_top != 0) {
1091 // We do not need to interpret the results of exceptional
1092 // continuation from this instruction when the method has no
1093 // exception handlers and the monitor stack is currently
1094 // empty.
1095 do_exception_edge(&itr);
1096 }
1097 interp1(&itr);
1098 itr.next();
1099 }
1101 // Handle last instruction.
1102 if (!_got_error) {
1103 assert(itr.next_bci() == lim_bci, "must point to end");
1104 if (_has_exceptions || _monitor_top != 0) {
1105 do_exception_edge(&itr);
1106 }
1107 interp1(&itr);
1109 bool fall_through = jump_targets_do(&itr, GenerateOopMap::merge_state, NULL);
1110 if (_got_error) return;
1112 if (itr.code() == Bytecodes::_ret) {
1113 assert(!fall_through, "cannot be set if ret instruction");
1114 // Automatically handles 'wide' ret indicies
1115 ret_jump_targets_do(&itr, GenerateOopMap::merge_state, itr.get_index(), NULL);
1116 } else if (fall_through) {
1117 // Hit end of BB, but the instr. was a fall-through instruction,
1118 // so perform transition as if the BB ended in a "jump".
1119 if (lim_bci != bb[1]._bci) {
1120 verify_error("bytecodes fell through last instruction");
1121 return;
1122 }
1123 merge_state_into_bb(bb + 1);
1124 }
1125 }
1126 }
1128 void GenerateOopMap::do_exception_edge(BytecodeStream* itr) {
1129 // Only check exception edge, if bytecode can trap
1130 if (!Bytecodes::can_trap(itr->code())) return;
1131 switch (itr->code()) {
1132 case Bytecodes::_aload_0:
1133 // These bytecodes can trap for rewriting. We need to assume that
1134 // they do not throw exceptions to make the monitor analysis work.
1135 return;
1137 case Bytecodes::_ireturn:
1138 case Bytecodes::_lreturn:
1139 case Bytecodes::_freturn:
1140 case Bytecodes::_dreturn:
1141 case Bytecodes::_areturn:
1142 case Bytecodes::_return:
1143 // If the monitor stack height is not zero when we leave the method,
1144 // then we are either exiting with a non-empty stack or we have
1145 // found monitor trouble earlier in our analysis. In either case,
1146 // assume an exception could be taken here.
1147 if (_monitor_top == 0) {
1148 return;
1149 }
1150 break;
1152 case Bytecodes::_monitorexit:
1153 // If the monitor stack height is bad_monitors, then we have detected a
1154 // monitor matching problem earlier in the analysis. If the
1155 // monitor stack height is 0, we are about to pop a monitor
1156 // off of an empty stack. In either case, the bytecode
1157 // could throw an exception.
1158 if (_monitor_top != bad_monitors && _monitor_top != 0) {
1159 return;
1160 }
1161 break;
1162 }
1164 if (_has_exceptions) {
1165 int bci = itr->bci();
1166 typeArrayOop exct = method()->exception_table();
1167 for(int i = 0; i< exct->length(); i+=4) {
1168 int start_pc = exct->int_at(i);
1169 int end_pc = exct->int_at(i+1);
1170 int handler_pc = exct->int_at(i+2);
1171 int catch_type = exct->int_at(i+3);
1173 if (start_pc <= bci && bci < end_pc) {
1174 BasicBlock *excBB = get_basic_block_at(handler_pc);
1175 CellTypeState *excStk = excBB->stack();
1176 CellTypeState *cOpStck = stack();
1177 CellTypeState cOpStck_0 = cOpStck[0];
1178 int cOpStackTop = _stack_top;
1180 // Exception stacks are always the same.
1181 assert(method()->max_stack() > 0, "sanity check");
1183 // We remembered the size and first element of "cOpStck"
1184 // above; now we temporarily set them to the appropriate
1185 // values for an exception handler. */
1186 cOpStck[0] = CellTypeState::make_slot_ref(_max_locals);
1187 _stack_top = 1;
1189 merge_state_into_bb(excBB);
1191 // Now undo the temporary change.
1192 cOpStck[0] = cOpStck_0;
1193 _stack_top = cOpStackTop;
1195 // If this is a "catch all" handler, then we do not need to
1196 // consider any additional handlers.
1197 if (catch_type == 0) {
1198 return;
1199 }
1200 }
1201 }
1202 }
1204 // It is possible that none of the exception handlers would have caught
1205 // the exception. In this case, we will exit the method. We must
1206 // ensure that the monitor stack is empty in this case.
1207 if (_monitor_top == 0) {
1208 return;
1209 }
1211 // We pessimistically assume that this exception can escape the
1212 // method. (It is possible that it will always be caught, but
1213 // we don't care to analyse the types of the catch clauses.)
1215 // We don't set _monitor_top to bad_monitors because there are no successors
1216 // to this exceptional exit.
1218 if (TraceMonitorMismatch && _monitor_safe) {
1219 // We check _monitor_safe so that we only report the first mismatched
1220 // exceptional exit.
1221 report_monitor_mismatch("non-empty monitor stack at exceptional exit");
1222 }
1223 _monitor_safe = false;
1225 }
1227 void GenerateOopMap::report_monitor_mismatch(const char *msg) {
1228 #ifndef PRODUCT
1229 tty->print(" Monitor mismatch in method ");
1230 method()->print_short_name(tty);
1231 tty->print_cr(": %s", msg);
1232 #endif
1233 }
1235 void GenerateOopMap::print_states(outputStream *os,
1236 CellTypeState* vec, int num) {
1237 for (int i = 0; i < num; i++) {
1238 vec[i].print(tty);
1239 }
1240 }
1242 // Print the state values at the current bytecode.
1243 void GenerateOopMap::print_current_state(outputStream *os,
1244 BytecodeStream *currentBC,
1245 bool detailed) {
1247 if (detailed) {
1248 os->print(" %4d vars = ", currentBC->bci());
1249 print_states(os, vars(), _max_locals);
1250 os->print(" %s", Bytecodes::name(currentBC->code()));
1251 switch(currentBC->code()) {
1252 case Bytecodes::_invokevirtual:
1253 case Bytecodes::_invokespecial:
1254 case Bytecodes::_invokestatic:
1255 case Bytecodes::_invokeinterface:
1256 int idx = currentBC->get_index_big();
1257 constantPoolOop cp = method()->constants();
1258 int nameAndTypeIdx = cp->name_and_type_ref_index_at(idx);
1259 int signatureIdx = cp->signature_ref_index_at(nameAndTypeIdx);
1260 symbolOop signature = cp->symbol_at(signatureIdx);
1261 os->print("%s", signature->as_C_string());
1262 }
1263 os->cr();
1264 os->print(" stack = ");
1265 print_states(os, stack(), _stack_top);
1266 os->cr();
1267 if (_monitor_top != bad_monitors) {
1268 os->print(" monitors = ");
1269 print_states(os, monitors(), _monitor_top);
1270 } else {
1271 os->print(" [bad monitor stack]");
1272 }
1273 os->cr();
1274 } else {
1275 os->print(" %4d vars = '%s' ", currentBC->bci(), state_vec_to_string(vars(), _max_locals));
1276 os->print(" stack = '%s' ", state_vec_to_string(stack(), _stack_top));
1277 if (_monitor_top != bad_monitors) {
1278 os->print(" monitors = '%s' \t%s", state_vec_to_string(monitors(), _monitor_top), Bytecodes::name(currentBC->code()));
1279 } else {
1280 os->print(" [bad monitor stack]");
1281 }
1282 switch(currentBC->code()) {
1283 case Bytecodes::_invokevirtual:
1284 case Bytecodes::_invokespecial:
1285 case Bytecodes::_invokestatic:
1286 case Bytecodes::_invokeinterface:
1287 int idx = currentBC->get_index_big();
1288 constantPoolOop cp = method()->constants();
1289 int nameAndTypeIdx = cp->name_and_type_ref_index_at(idx);
1290 int signatureIdx = cp->signature_ref_index_at(nameAndTypeIdx);
1291 symbolOop signature = cp->symbol_at(signatureIdx);
1292 os->print("%s", signature->as_C_string());
1293 }
1294 os->cr();
1295 }
1296 }
1298 // Sets the current state to be the state after executing the
1299 // current instruction, starting in the current state.
1300 void GenerateOopMap::interp1(BytecodeStream *itr) {
1301 if (TraceNewOopMapGeneration) {
1302 print_current_state(tty, itr, TraceNewOopMapGenerationDetailed);
1303 }
1305 // Should we report the results? Result is reported *before* the instruction at the current bci is executed.
1306 // However, not for calls. For calls we do not want to include the arguments, so we postpone the reporting until
1307 // they have been popped (in method ppl).
1308 if (_report_result == true) {
1309 switch(itr->code()) {
1310 case Bytecodes::_invokevirtual:
1311 case Bytecodes::_invokespecial:
1312 case Bytecodes::_invokestatic:
1313 case Bytecodes::_invokeinterface:
1314 _itr_send = itr;
1315 _report_result_for_send = true;
1316 break;
1317 default:
1318 fill_stackmap_for_opcodes(itr, vars(), stack(), _stack_top);
1319 break;
1320 }
1321 }
1323 // abstract interpretation of current opcode
1324 switch(itr->code()) {
1325 case Bytecodes::_nop: break;
1326 case Bytecodes::_goto: break;
1327 case Bytecodes::_goto_w: break;
1328 case Bytecodes::_iinc: break;
1329 case Bytecodes::_return: do_return_monitor_check();
1330 break;
1332 case Bytecodes::_aconst_null:
1333 case Bytecodes::_new: ppush1(CellTypeState::make_line_ref(itr->bci()));
1334 break;
1336 case Bytecodes::_iconst_m1:
1337 case Bytecodes::_iconst_0:
1338 case Bytecodes::_iconst_1:
1339 case Bytecodes::_iconst_2:
1340 case Bytecodes::_iconst_3:
1341 case Bytecodes::_iconst_4:
1342 case Bytecodes::_iconst_5:
1343 case Bytecodes::_fconst_0:
1344 case Bytecodes::_fconst_1:
1345 case Bytecodes::_fconst_2:
1346 case Bytecodes::_bipush:
1347 case Bytecodes::_sipush: ppush1(valCTS); break;
1349 case Bytecodes::_lconst_0:
1350 case Bytecodes::_lconst_1:
1351 case Bytecodes::_dconst_0:
1352 case Bytecodes::_dconst_1: ppush(vvCTS); break;
1354 case Bytecodes::_ldc2_w: ppush(vvCTS); break;
1356 case Bytecodes::_ldc: do_ldc(itr->get_index(), itr->bci()); break;
1357 case Bytecodes::_ldc_w: do_ldc(itr->get_index_big(), itr->bci());break;
1359 case Bytecodes::_iload:
1360 case Bytecodes::_fload: ppload(vCTS, itr->get_index()); break;
1362 case Bytecodes::_lload:
1363 case Bytecodes::_dload: ppload(vvCTS,itr->get_index()); break;
1365 case Bytecodes::_aload: ppload(rCTS, itr->get_index()); break;
1367 case Bytecodes::_iload_0:
1368 case Bytecodes::_fload_0: ppload(vCTS, 0); break;
1369 case Bytecodes::_iload_1:
1370 case Bytecodes::_fload_1: ppload(vCTS, 1); break;
1371 case Bytecodes::_iload_2:
1372 case Bytecodes::_fload_2: ppload(vCTS, 2); break;
1373 case Bytecodes::_iload_3:
1374 case Bytecodes::_fload_3: ppload(vCTS, 3); break;
1376 case Bytecodes::_lload_0:
1377 case Bytecodes::_dload_0: ppload(vvCTS, 0); break;
1378 case Bytecodes::_lload_1:
1379 case Bytecodes::_dload_1: ppload(vvCTS, 1); break;
1380 case Bytecodes::_lload_2:
1381 case Bytecodes::_dload_2: ppload(vvCTS, 2); break;
1382 case Bytecodes::_lload_3:
1383 case Bytecodes::_dload_3: ppload(vvCTS, 3); break;
1385 case Bytecodes::_aload_0: ppload(rCTS, 0); break;
1386 case Bytecodes::_aload_1: ppload(rCTS, 1); break;
1387 case Bytecodes::_aload_2: ppload(rCTS, 2); break;
1388 case Bytecodes::_aload_3: ppload(rCTS, 3); break;
1390 case Bytecodes::_iaload:
1391 case Bytecodes::_faload:
1392 case Bytecodes::_baload:
1393 case Bytecodes::_caload:
1394 case Bytecodes::_saload: pp(vrCTS, vCTS); break;
1396 case Bytecodes::_laload: pp(vrCTS, vvCTS); break;
1397 case Bytecodes::_daload: pp(vrCTS, vvCTS); break;
1399 case Bytecodes::_aaload: pp_new_ref(vrCTS, itr->bci()); break;
1401 case Bytecodes::_istore:
1402 case Bytecodes::_fstore: ppstore(vCTS, itr->get_index()); break;
1404 case Bytecodes::_lstore:
1405 case Bytecodes::_dstore: ppstore(vvCTS, itr->get_index()); break;
1407 case Bytecodes::_astore: do_astore(itr->get_index()); break;
1409 case Bytecodes::_istore_0:
1410 case Bytecodes::_fstore_0: ppstore(vCTS, 0); break;
1411 case Bytecodes::_istore_1:
1412 case Bytecodes::_fstore_1: ppstore(vCTS, 1); break;
1413 case Bytecodes::_istore_2:
1414 case Bytecodes::_fstore_2: ppstore(vCTS, 2); break;
1415 case Bytecodes::_istore_3:
1416 case Bytecodes::_fstore_3: ppstore(vCTS, 3); break;
1418 case Bytecodes::_lstore_0:
1419 case Bytecodes::_dstore_0: ppstore(vvCTS, 0); break;
1420 case Bytecodes::_lstore_1:
1421 case Bytecodes::_dstore_1: ppstore(vvCTS, 1); break;
1422 case Bytecodes::_lstore_2:
1423 case Bytecodes::_dstore_2: ppstore(vvCTS, 2); break;
1424 case Bytecodes::_lstore_3:
1425 case Bytecodes::_dstore_3: ppstore(vvCTS, 3); break;
1427 case Bytecodes::_astore_0: do_astore(0); break;
1428 case Bytecodes::_astore_1: do_astore(1); break;
1429 case Bytecodes::_astore_2: do_astore(2); break;
1430 case Bytecodes::_astore_3: do_astore(3); break;
1432 case Bytecodes::_iastore:
1433 case Bytecodes::_fastore:
1434 case Bytecodes::_bastore:
1435 case Bytecodes::_castore:
1436 case Bytecodes::_sastore: ppop(vvrCTS); break;
1437 case Bytecodes::_lastore:
1438 case Bytecodes::_dastore: ppop(vvvrCTS); break;
1439 case Bytecodes::_aastore: ppop(rvrCTS); break;
1441 case Bytecodes::_pop: ppop_any(1); break;
1442 case Bytecodes::_pop2: ppop_any(2); break;
1444 case Bytecodes::_dup: ppdupswap(1, "11"); break;
1445 case Bytecodes::_dup_x1: ppdupswap(2, "121"); break;
1446 case Bytecodes::_dup_x2: ppdupswap(3, "1321"); break;
1447 case Bytecodes::_dup2: ppdupswap(2, "2121"); break;
1448 case Bytecodes::_dup2_x1: ppdupswap(3, "21321"); break;
1449 case Bytecodes::_dup2_x2: ppdupswap(4, "214321"); break;
1450 case Bytecodes::_swap: ppdupswap(2, "12"); break;
1452 case Bytecodes::_iadd:
1453 case Bytecodes::_fadd:
1454 case Bytecodes::_isub:
1455 case Bytecodes::_fsub:
1456 case Bytecodes::_imul:
1457 case Bytecodes::_fmul:
1458 case Bytecodes::_idiv:
1459 case Bytecodes::_fdiv:
1460 case Bytecodes::_irem:
1461 case Bytecodes::_frem:
1462 case Bytecodes::_ishl:
1463 case Bytecodes::_ishr:
1464 case Bytecodes::_iushr:
1465 case Bytecodes::_iand:
1466 case Bytecodes::_ior:
1467 case Bytecodes::_ixor:
1468 case Bytecodes::_l2f:
1469 case Bytecodes::_l2i:
1470 case Bytecodes::_d2f:
1471 case Bytecodes::_d2i:
1472 case Bytecodes::_fcmpl:
1473 case Bytecodes::_fcmpg: pp(vvCTS, vCTS); break;
1475 case Bytecodes::_ladd:
1476 case Bytecodes::_dadd:
1477 case Bytecodes::_lsub:
1478 case Bytecodes::_dsub:
1479 case Bytecodes::_lmul:
1480 case Bytecodes::_dmul:
1481 case Bytecodes::_ldiv:
1482 case Bytecodes::_ddiv:
1483 case Bytecodes::_lrem:
1484 case Bytecodes::_drem:
1485 case Bytecodes::_land:
1486 case Bytecodes::_lor:
1487 case Bytecodes::_lxor: pp(vvvvCTS, vvCTS); break;
1489 case Bytecodes::_ineg:
1490 case Bytecodes::_fneg:
1491 case Bytecodes::_i2f:
1492 case Bytecodes::_f2i:
1493 case Bytecodes::_i2c:
1494 case Bytecodes::_i2s:
1495 case Bytecodes::_i2b: pp(vCTS, vCTS); break;
1497 case Bytecodes::_lneg:
1498 case Bytecodes::_dneg:
1499 case Bytecodes::_l2d:
1500 case Bytecodes::_d2l: pp(vvCTS, vvCTS); break;
1502 case Bytecodes::_lshl:
1503 case Bytecodes::_lshr:
1504 case Bytecodes::_lushr: pp(vvvCTS, vvCTS); break;
1506 case Bytecodes::_i2l:
1507 case Bytecodes::_i2d:
1508 case Bytecodes::_f2l:
1509 case Bytecodes::_f2d: pp(vCTS, vvCTS); break;
1511 case Bytecodes::_lcmp: pp(vvvvCTS, vCTS); break;
1512 case Bytecodes::_dcmpl:
1513 case Bytecodes::_dcmpg: pp(vvvvCTS, vCTS); break;
1515 case Bytecodes::_ifeq:
1516 case Bytecodes::_ifne:
1517 case Bytecodes::_iflt:
1518 case Bytecodes::_ifge:
1519 case Bytecodes::_ifgt:
1520 case Bytecodes::_ifle:
1521 case Bytecodes::_tableswitch: ppop1(valCTS);
1522 break;
1523 case Bytecodes::_ireturn:
1524 case Bytecodes::_freturn: do_return_monitor_check();
1525 ppop1(valCTS);
1526 break;
1527 case Bytecodes::_if_icmpeq:
1528 case Bytecodes::_if_icmpne:
1529 case Bytecodes::_if_icmplt:
1530 case Bytecodes::_if_icmpge:
1531 case Bytecodes::_if_icmpgt:
1532 case Bytecodes::_if_icmple: ppop(vvCTS);
1533 break;
1535 case Bytecodes::_lreturn: do_return_monitor_check();
1536 ppop(vvCTS);
1537 break;
1539 case Bytecodes::_dreturn: do_return_monitor_check();
1540 ppop(vvCTS);
1541 break;
1543 case Bytecodes::_if_acmpeq:
1544 case Bytecodes::_if_acmpne: ppop(rrCTS); break;
1546 case Bytecodes::_jsr: do_jsr(itr->dest()); break;
1547 case Bytecodes::_jsr_w: do_jsr(itr->dest_w()); break;
1549 case Bytecodes::_getstatic: do_field(true, true,
1550 itr->get_index_big(),
1551 itr->bci()); break;
1552 case Bytecodes::_putstatic: do_field(false, true, itr->get_index_big(), itr->bci()); break;
1553 case Bytecodes::_getfield: do_field(true, false, itr->get_index_big(), itr->bci()); break;
1554 case Bytecodes::_putfield: do_field(false, false, itr->get_index_big(), itr->bci()); break;
1556 case Bytecodes::_invokevirtual:
1557 case Bytecodes::_invokespecial: do_method(false, false, itr->get_index_big(), itr->bci()); break;
1558 case Bytecodes::_invokestatic: do_method(true, false, itr->get_index_big(), itr->bci()); break;
1559 case Bytecodes::_invokeinterface: do_method(false, true, itr->get_index_big(), itr->bci()); break;
1560 case Bytecodes::_newarray:
1561 case Bytecodes::_anewarray: pp_new_ref(vCTS, itr->bci()); break;
1562 case Bytecodes::_checkcast: do_checkcast(); break;
1563 case Bytecodes::_arraylength:
1564 case Bytecodes::_instanceof: pp(rCTS, vCTS); break;
1565 case Bytecodes::_monitorenter: do_monitorenter(itr->bci()); break;
1566 case Bytecodes::_monitorexit: do_monitorexit(itr->bci()); break;
1568 case Bytecodes::_athrow: // handled by do_exception_edge() BUT ...
1569 // vlh(apple): do_exception_edge() does not get
1570 // called if method has no exception handlers
1571 if ((!_has_exceptions) && (_monitor_top > 0)) {
1572 _monitor_safe = false;
1573 }
1574 break;
1576 case Bytecodes::_areturn: do_return_monitor_check();
1577 ppop1(refCTS);
1578 break;
1579 case Bytecodes::_ifnull:
1580 case Bytecodes::_ifnonnull: ppop1(refCTS); break;
1581 case Bytecodes::_multianewarray: do_multianewarray(*(itr->bcp()+3), itr->bci()); break;
1583 case Bytecodes::_wide: fatal("Iterator should skip this bytecode"); break;
1584 case Bytecodes::_ret: break;
1586 // Java opcodes
1587 case Bytecodes::_lookupswitch: ppop1(valCTS); break;
1589 default:
1590 tty->print("unexpected opcode: %d\n", itr->code());
1591 ShouldNotReachHere();
1592 break;
1593 }
1594 }
1596 void GenerateOopMap::check_type(CellTypeState expected, CellTypeState actual) {
1597 if (!expected.equal_kind(actual)) {
1598 verify_error("wrong type on stack (found: %c expected: %c)", actual.to_char(), expected.to_char());
1599 }
1600 }
1602 void GenerateOopMap::ppstore(CellTypeState *in, int loc_no) {
1603 while(!(*in).is_bottom()) {
1604 CellTypeState expected =*in++;
1605 CellTypeState actual = pop();
1606 check_type(expected, actual);
1607 assert(loc_no >= 0, "sanity check");
1608 set_var(loc_no++, actual);
1609 }
1610 }
1612 void GenerateOopMap::ppload(CellTypeState *out, int loc_no) {
1613 while(!(*out).is_bottom()) {
1614 CellTypeState out1 = *out++;
1615 CellTypeState vcts = get_var(loc_no);
1616 assert(out1.can_be_reference() || out1.can_be_value(),
1617 "can only load refs. and values.");
1618 if (out1.is_reference()) {
1619 assert(loc_no>=0, "sanity check");
1620 if (!vcts.is_reference()) {
1621 // We were asked to push a reference, but the type of the
1622 // variable can be something else
1623 _conflict = true;
1624 if (vcts.can_be_uninit()) {
1625 // It is a ref-uninit conflict (at least). If there are other
1626 // problems, we'll get them in the next round
1627 add_to_ref_init_set(loc_no);
1628 vcts = out1;
1629 } else {
1630 // It wasn't a ref-uninit conflict. So must be a
1631 // ref-val or ref-pc conflict. Split the variable.
1632 record_refval_conflict(loc_no);
1633 vcts = out1;
1634 }
1635 push(out1); // recover...
1636 } else {
1637 push(vcts); // preserve reference.
1638 }
1639 // Otherwise it is a conflict, but one that verification would
1640 // have caught if illegal. In particular, it can't be a topCTS
1641 // resulting from mergeing two difference pcCTS's since the verifier
1642 // would have rejected any use of such a merge.
1643 } else {
1644 push(out1); // handle val/init conflict
1645 }
1646 loc_no++;
1647 }
1648 }
1650 void GenerateOopMap::ppdupswap(int poplen, const char *out) {
1651 CellTypeState actual[5];
1652 assert(poplen < 5, "this must be less than length of actual vector");
1654 // pop all arguments
1655 for(int i = 0; i < poplen; i++) actual[i] = pop();
1657 // put them back
1658 char push_ch = *out++;
1659 while (push_ch != '\0') {
1660 int idx = push_ch - '1';
1661 assert(idx >= 0 && idx < poplen, "wrong arguments");
1662 push(actual[idx]);
1663 push_ch = *out++;
1664 }
1665 }
1667 void GenerateOopMap::ppop1(CellTypeState out) {
1668 CellTypeState actual = pop();
1669 check_type(out, actual);
1670 }
1672 void GenerateOopMap::ppop(CellTypeState *out) {
1673 while (!(*out).is_bottom()) {
1674 ppop1(*out++);
1675 }
1676 }
1678 void GenerateOopMap::ppush1(CellTypeState in) {
1679 assert(in.is_reference() | in.is_value(), "sanity check");
1680 push(in);
1681 }
1683 void GenerateOopMap::ppush(CellTypeState *in) {
1684 while (!(*in).is_bottom()) {
1685 ppush1(*in++);
1686 }
1687 }
1689 void GenerateOopMap::pp(CellTypeState *in, CellTypeState *out) {
1690 ppop(in);
1691 ppush(out);
1692 }
1694 void GenerateOopMap::pp_new_ref(CellTypeState *in, int bci) {
1695 ppop(in);
1696 ppush1(CellTypeState::make_line_ref(bci));
1697 }
1699 void GenerateOopMap::ppop_any(int poplen) {
1700 if (_stack_top >= poplen) {
1701 _stack_top -= poplen;
1702 } else {
1703 verify_error("stack underflow");
1704 }
1705 }
1707 // Replace all occurences of the state 'match' with the state 'replace'
1708 // in our current state vector.
1709 void GenerateOopMap::replace_all_CTS_matches(CellTypeState match,
1710 CellTypeState replace) {
1711 int i;
1712 int len = _max_locals + _stack_top;
1713 bool change = false;
1715 for (i = len - 1; i >= 0; i--) {
1716 if (match.equal(_state[i])) {
1717 _state[i] = replace;
1718 }
1719 }
1721 if (_monitor_top > 0) {
1722 int base = _max_locals + _max_stack;
1723 len = base + _monitor_top;
1724 for (i = len - 1; i >= base; i--) {
1725 if (match.equal(_state[i])) {
1726 _state[i] = replace;
1727 }
1728 }
1729 }
1730 }
1732 void GenerateOopMap::do_checkcast() {
1733 CellTypeState actual = pop();
1734 check_type(refCTS, actual);
1735 push(actual);
1736 }
1738 void GenerateOopMap::do_monitorenter(int bci) {
1739 CellTypeState actual = pop();
1740 if (_monitor_top == bad_monitors) {
1741 return;
1742 }
1744 // Bail out when we get repeated locks on an identical monitor. This case
1745 // isn't too hard to handle and can be made to work if supporting nested
1746 // redundant synchronized statements becomes a priority.
1747 //
1748 // See also "Note" in do_monitorexit(), below.
1749 if (actual.is_lock_reference()) {
1750 _monitor_top = bad_monitors;
1751 _monitor_safe = false;
1753 if (TraceMonitorMismatch) {
1754 report_monitor_mismatch("nested redundant lock -- bailout...");
1755 }
1756 return;
1757 }
1759 CellTypeState lock = CellTypeState::make_lock_ref(bci);
1760 check_type(refCTS, actual);
1761 if (!actual.is_info_top()) {
1762 replace_all_CTS_matches(actual, lock);
1763 monitor_push(lock);
1764 }
1765 }
1767 void GenerateOopMap::do_monitorexit(int bci) {
1768 CellTypeState actual = pop();
1769 if (_monitor_top == bad_monitors) {
1770 return;
1771 }
1772 check_type(refCTS, actual);
1773 CellTypeState expected = monitor_pop();
1774 if (!actual.is_lock_reference() || !expected.equal(actual)) {
1775 // The monitor we are exiting is not verifiably the one
1776 // on the top of our monitor stack. This causes a monitor
1777 // mismatch.
1778 _monitor_top = bad_monitors;
1779 _monitor_safe = false;
1781 // We need to mark this basic block as changed so that
1782 // this monitorexit will be visited again. We need to
1783 // do this to ensure that we have accounted for the
1784 // possibility that this bytecode will throw an
1785 // exception.
1786 BasicBlock* bb = get_basic_block_containing(bci);
1787 bb->set_changed(true);
1788 bb->_monitor_top = bad_monitors;
1790 if (TraceMonitorMismatch) {
1791 report_monitor_mismatch("improper monitor pair");
1792 }
1793 } else {
1794 // This code is a fix for the case where we have repeated
1795 // locking of the same object in straightline code. We clear
1796 // out the lock when it is popped from the monitor stack
1797 // and replace it with an unobtrusive reference value that can
1798 // be locked again.
1799 //
1800 // Note: when generateOopMap is fixed to properly handle repeated,
1801 // nested, redundant locks on the same object, then this
1802 // fix will need to be removed at that time.
1803 replace_all_CTS_matches(actual, CellTypeState::make_line_ref(bci));
1804 }
1805 }
1807 void GenerateOopMap::do_return_monitor_check() {
1808 if (_monitor_top > 0) {
1809 // The monitor stack must be empty when we leave the method
1810 // for the monitors to be properly matched.
1811 _monitor_safe = false;
1813 // Since there are no successors to the *return bytecode, it
1814 // isn't necessary to set _monitor_top to bad_monitors.
1816 if (TraceMonitorMismatch) {
1817 report_monitor_mismatch("non-empty monitor stack at return");
1818 }
1819 }
1820 }
1822 void GenerateOopMap::do_jsr(int targ_bci) {
1823 push(CellTypeState::make_addr(targ_bci));
1824 }
1828 void GenerateOopMap::do_ldc(int idx, int bci) {
1829 constantPoolOop cp = method()->constants();
1830 constantTag tag = cp->tag_at(idx);
1832 CellTypeState cts = (tag.is_string() || tag.is_unresolved_string() ||
1833 tag.is_klass() || tag.is_unresolved_klass())
1834 ? CellTypeState::make_line_ref(bci) : valCTS;
1835 ppush1(cts);
1836 }
1838 void GenerateOopMap::do_multianewarray(int dims, int bci) {
1839 assert(dims >= 1, "sanity check");
1840 for(int i = dims -1; i >=0; i--) {
1841 ppop1(valCTS);
1842 }
1843 ppush1(CellTypeState::make_line_ref(bci));
1844 }
1846 void GenerateOopMap::do_astore(int idx) {
1847 CellTypeState r_or_p = pop();
1848 if (!r_or_p.is_address() && !r_or_p.is_reference()) {
1849 // We actually expected ref or pc, but we only report that we expected a ref. It does not
1850 // really matter (at least for now)
1851 verify_error("wrong type on stack (found: %c, expected: {pr})", r_or_p.to_char());
1852 return;
1853 }
1854 set_var(idx, r_or_p);
1855 }
1857 // Copies bottom/zero terminated CTS string from "src" into "dst".
1858 // Does NOT terminate with a bottom. Returns the number of cells copied.
1859 int GenerateOopMap::copy_cts(CellTypeState *dst, CellTypeState *src) {
1860 int idx = 0;
1861 while (!src[idx].is_bottom()) {
1862 dst[idx] = src[idx];
1863 idx++;
1864 }
1865 return idx;
1866 }
1868 void GenerateOopMap::do_field(int is_get, int is_static, int idx, int bci) {
1869 // Dig up signature for field in constant pool
1870 constantPoolOop cp = method()->constants();
1871 int nameAndTypeIdx = cp->name_and_type_ref_index_at(idx);
1872 int signatureIdx = cp->signature_ref_index_at(nameAndTypeIdx);
1873 symbolOop signature = cp->symbol_at(signatureIdx);
1875 // Parse signature (espcially simple for fields)
1876 assert(signature->utf8_length() > 0, "field signatures cannot have zero length");
1877 // The signature is UFT8 encoded, but the first char is always ASCII for signatures.
1878 char sigch = (char)*(signature->base());
1879 CellTypeState temp[4];
1880 CellTypeState *eff = sigchar_to_effect(sigch, bci, temp);
1882 CellTypeState in[4];
1883 CellTypeState *out;
1884 int i = 0;
1886 if (is_get) {
1887 out = eff;
1888 } else {
1889 out = epsilonCTS;
1890 i = copy_cts(in, eff);
1891 }
1892 if (!is_static) in[i++] = CellTypeState::ref;
1893 in[i] = CellTypeState::bottom;
1894 assert(i<=3, "sanity check");
1895 pp(in, out);
1896 }
1898 void GenerateOopMap::do_method(int is_static, int is_interface, int idx, int bci) {
1899 // Dig up signature for field in constant pool
1900 constantPoolOop cp = _method->constants();
1901 int nameAndTypeIdx = cp->name_and_type_ref_index_at(idx);
1902 int signatureIdx = cp->signature_ref_index_at(nameAndTypeIdx);
1903 symbolOop signature = cp->symbol_at(signatureIdx);
1905 // Parse method signature
1906 CellTypeState out[4];
1907 CellTypeState in[MAXARGSIZE+1]; // Includes result
1908 ComputeCallStack cse(signature);
1910 // Compute return type
1911 int res_length= cse.compute_for_returntype(out);
1913 // Temporary hack.
1914 if (out[0].equal(CellTypeState::ref) && out[1].equal(CellTypeState::bottom)) {
1915 out[0] = CellTypeState::make_line_ref(bci);
1916 }
1918 assert(res_length<=4, "max value should be vv");
1920 // Compute arguments
1921 int arg_length = cse.compute_for_parameters(is_static != 0, in);
1922 assert(arg_length<=MAXARGSIZE, "too many locals");
1924 // Pop arguments
1925 for (int i = arg_length - 1; i >= 0; i--) ppop1(in[i]);// Do args in reverse order.
1927 // Report results
1928 if (_report_result_for_send == true) {
1929 fill_stackmap_for_opcodes(_itr_send, vars(), stack(), _stack_top);
1930 _report_result_for_send = false;
1931 }
1933 // Push return address
1934 ppush(out);
1935 }
1937 // This is used to parse the signature for fields, since they are very simple...
1938 CellTypeState *GenerateOopMap::sigchar_to_effect(char sigch, int bci, CellTypeState *out) {
1939 // Object and array
1940 if (sigch=='L' || sigch=='[') {
1941 out[0] = CellTypeState::make_line_ref(bci);
1942 out[1] = CellTypeState::bottom;
1943 return out;
1944 }
1945 if (sigch == 'J' || sigch == 'D' ) return vvCTS; // Long and Double
1946 if (sigch == 'V' ) return epsilonCTS; // Void
1947 return vCTS; // Otherwise
1948 }
1950 long GenerateOopMap::_total_byte_count = 0;
1951 elapsedTimer GenerateOopMap::_total_oopmap_time;
1953 // This function assumes "bcs" is at a "ret" instruction and that the vars
1954 // state is valid for that instruction. Furthermore, the ret instruction
1955 // must be the last instruction in "bb" (we store information about the
1956 // "ret" in "bb").
1957 void GenerateOopMap::ret_jump_targets_do(BytecodeStream *bcs, jmpFct_t jmpFct, int varNo, int *data) {
1958 CellTypeState ra = vars()[varNo];
1959 if (!ra.is_good_address()) {
1960 verify_error("ret returns from two jsr subroutines?");
1961 return;
1962 }
1963 int target = ra.get_info();
1965 RetTableEntry* rtEnt = _rt.find_jsrs_for_target(target);
1966 int bci = bcs->bci();
1967 for (int i = 0; i < rtEnt->nof_jsrs(); i++) {
1968 int target_bci = rtEnt->jsrs(i);
1969 // Make sure a jrtRet does not set the changed bit for dead basicblock.
1970 BasicBlock* jsr_bb = get_basic_block_containing(target_bci - 1);
1971 debug_only(BasicBlock* target_bb = &jsr_bb[1];)
1972 assert(target_bb == get_basic_block_at(target_bci), "wrong calc. of successor basicblock");
1973 bool alive = jsr_bb->is_alive();
1974 if (TraceNewOopMapGeneration) {
1975 tty->print("pc = %d, ret -> %d alive: %s\n", bci, target_bci, alive ? "true" : "false");
1976 }
1977 if (alive) jmpFct(this, target_bci, data);
1978 }
1979 }
1981 //
1982 // Debug method
1983 //
1984 char* GenerateOopMap::state_vec_to_string(CellTypeState* vec, int len) {
1985 #ifdef ASSERT
1986 int checklen = MAX3(_max_locals, _max_stack, _max_monitors) + 1;
1987 assert(len < checklen, "state_vec_buf overflow");
1988 #endif
1989 for (int i = 0; i < len; i++) _state_vec_buf[i] = vec[i].to_char();
1990 _state_vec_buf[len] = 0;
1991 return _state_vec_buf;
1992 }
1994 void GenerateOopMap::print_time() {
1995 tty->print_cr ("Accumulated oopmap times:");
1996 tty->print_cr ("---------------------------");
1997 tty->print_cr (" Total : %3.3f sec.", GenerateOopMap::_total_oopmap_time.seconds());
1998 tty->print_cr (" (%3.0f bytecodes per sec) ",
1999 GenerateOopMap::_total_byte_count / GenerateOopMap::_total_oopmap_time.seconds());
2000 }
2002 //
2003 // ============ Main Entry Point ===========
2004 //
2005 GenerateOopMap::GenerateOopMap(methodHandle method) {
2006 // We have to initialize all variables here, that can be queried directly
2007 _method = method;
2008 _max_locals=0;
2009 _init_vars = NULL;
2011 #ifndef PRODUCT
2012 // If we are doing a detailed trace, include the regular trace information.
2013 if (TraceNewOopMapGenerationDetailed) {
2014 TraceNewOopMapGeneration = true;
2015 }
2016 #endif
2017 }
2019 void GenerateOopMap::compute_map(TRAPS) {
2020 #ifndef PRODUCT
2021 if (TimeOopMap2) {
2022 method()->print_short_name(tty);
2023 tty->print(" ");
2024 }
2025 if (TimeOopMap) {
2026 _total_byte_count += method()->code_size();
2027 }
2028 #endif
2029 TraceTime t_single("oopmap time", TimeOopMap2);
2030 TraceTime t_all(NULL, &_total_oopmap_time, TimeOopMap);
2032 // Initialize values
2033 _got_error = false;
2034 _conflict = false;
2035 _max_locals = method()->max_locals();
2036 _max_stack = method()->max_stack();
2037 _has_exceptions = (method()->exception_table()->length() > 0);
2038 _nof_refval_conflicts = 0;
2039 _init_vars = new GrowableArray<intptr_t>(5); // There are seldom more than 5 init_vars
2040 _report_result = false;
2041 _report_result_for_send = false;
2042 _new_var_map = NULL;
2043 _ret_adr_tos = new GrowableArray<intptr_t>(5); // 5 seems like a good number;
2044 _did_rewriting = false;
2045 _did_relocation = false;
2047 if (TraceNewOopMapGeneration) {
2048 tty->print("Method name: %s\n", method()->name()->as_C_string());
2049 if (Verbose) {
2050 _method->print_codes();
2051 tty->print_cr("Exception table:");
2052 typeArrayOop excps = method()->exception_table();
2053 for(int i = 0; i < excps->length(); i += 4) {
2054 tty->print_cr("[%d - %d] -> %d", excps->int_at(i + 0), excps->int_at(i + 1), excps->int_at(i + 2));
2055 }
2056 }
2057 }
2059 // if no code - do nothing
2060 // compiler needs info
2061 if (method()->code_size() == 0 || _max_locals + method()->max_stack() == 0) {
2062 fill_stackmap_prolog(0);
2063 fill_stackmap_epilog();
2064 return;
2065 }
2066 // Step 1: Compute all jump targets and their return value
2067 if (!_got_error)
2068 _rt.compute_ret_table(_method);
2070 // Step 2: Find all basic blocks and count GC points
2071 if (!_got_error)
2072 mark_bbheaders_and_count_gc_points();
2074 // Step 3: Calculate stack maps
2075 if (!_got_error)
2076 do_interpretation();
2078 // Step 4:Return results
2079 if (!_got_error && report_results())
2080 report_result();
2082 if (_got_error) {
2083 THROW_HANDLE(_exception);
2084 }
2085 }
2087 // Error handling methods
2088 // These methods create an exception for the current thread which is thrown
2089 // at the bottom of the call stack, when it returns to compute_map(). The
2090 // _got_error flag controls execution. NOT TODO: The VM exception propagation
2091 // mechanism using TRAPS/CHECKs could be used here instead but it would need
2092 // to be added as a parameter to every function and checked for every call.
2093 // The tons of extra code it would generate didn't seem worth the change.
2094 //
2095 void GenerateOopMap::error_work(const char *format, va_list ap) {
2096 _got_error = true;
2097 char msg_buffer[512];
2098 vsnprintf(msg_buffer, sizeof(msg_buffer), format, ap);
2099 // Append method name
2100 char msg_buffer2[512];
2101 jio_snprintf(msg_buffer2, sizeof(msg_buffer2), "%s in method %s", msg_buffer, method()->name()->as_C_string());
2102 _exception = Exceptions::new_exception(Thread::current(),
2103 vmSymbols::java_lang_LinkageError(), msg_buffer2);
2104 }
2106 void GenerateOopMap::report_error(const char *format, ...) {
2107 va_list ap;
2108 va_start(ap, format);
2109 error_work(format, ap);
2110 }
2112 void GenerateOopMap::verify_error(const char *format, ...) {
2113 // We do not distinguish between different types of errors for verification
2114 // errors. Let the verifier give a better message.
2115 const char *msg = "Illegal class file encountered. Try running with -Xverify:all";
2116 error_work(msg, NULL);
2117 }
2119 //
2120 // Report result opcodes
2121 //
2122 void GenerateOopMap::report_result() {
2124 if (TraceNewOopMapGeneration) tty->print_cr("Report result pass");
2126 // We now want to report the result of the parse
2127 _report_result = true;
2129 // Prolog code
2130 fill_stackmap_prolog(_gc_points);
2132 // Mark everything changed, then do one interpretation pass.
2133 for (int i = 0; i<_bb_count; i++) {
2134 if (_basic_blocks[i].is_reachable()) {
2135 _basic_blocks[i].set_changed(true);
2136 interp_bb(&_basic_blocks[i]);
2137 }
2138 }
2140 // Note: Since we are skipping dead-code when we are reporting results, then
2141 // the no. of encountered gc-points might be fewer than the previously number
2142 // we have counted. (dead-code is a pain - it should be removed before we get here)
2143 fill_stackmap_epilog();
2145 // Report initvars
2146 fill_init_vars(_init_vars);
2148 _report_result = false;
2149 }
2151 void GenerateOopMap::result_for_basicblock(int bci) {
2152 if (TraceNewOopMapGeneration) tty->print_cr("Report result pass for basicblock");
2154 // We now want to report the result of the parse
2155 _report_result = true;
2157 // Find basicblock and report results
2158 BasicBlock* bb = get_basic_block_containing(bci);
2159 assert(bb->is_reachable(), "getting result from unreachable basicblock");
2160 bb->set_changed(true);
2161 interp_bb(bb);
2162 }
2164 //
2165 // Conflict handling code
2166 //
2168 void GenerateOopMap::record_refval_conflict(int varNo) {
2169 assert(varNo>=0 && varNo< _max_locals, "index out of range");
2171 if (TraceOopMapRewrites) {
2172 tty->print("### Conflict detected (local no: %d)\n", varNo);
2173 }
2175 if (!_new_var_map) {
2176 _new_var_map = NEW_RESOURCE_ARRAY(int, _max_locals);
2177 for (int k = 0; k < _max_locals; k++) _new_var_map[k] = k;
2178 }
2180 if ( _new_var_map[varNo] == varNo) {
2181 // Check if max. number of locals has been reached
2182 if (_max_locals + _nof_refval_conflicts >= MAX_LOCAL_VARS) {
2183 report_error("Rewriting exceeded local variable limit");
2184 return;
2185 }
2186 _new_var_map[varNo] = _max_locals + _nof_refval_conflicts;
2187 _nof_refval_conflicts++;
2188 }
2189 }
2191 void GenerateOopMap::rewrite_refval_conflicts()
2192 {
2193 // We can get here two ways: Either a rewrite conflict was detected, or
2194 // an uninitialize reference was detected. In the second case, we do not
2195 // do any rewriting, we just want to recompute the reference set with the
2196 // new information
2198 int nof_conflicts = 0; // Used for debugging only
2200 if ( _nof_refval_conflicts == 0 )
2201 return;
2203 // Check if rewrites are allowed in this parse.
2204 if (!allow_rewrites() && !IgnoreRewrites) {
2205 fatal("Rewriting method not allowed at this stage");
2206 }
2209 // This following flag is to tempoary supress rewrites. The locals that might conflict will
2210 // all be set to contain values. This is UNSAFE - however, until the rewriting has been completely
2211 // tested it is nice to have.
2212 if (IgnoreRewrites) {
2213 if (Verbose) {
2214 tty->print("rewrites suppressed for local no. ");
2215 for (int l = 0; l < _max_locals; l++) {
2216 if (_new_var_map[l] != l) {
2217 tty->print("%d ", l);
2218 vars()[l] = CellTypeState::value;
2219 }
2220 }
2221 tty->cr();
2222 }
2224 // That was that...
2225 _new_var_map = NULL;
2226 _nof_refval_conflicts = 0;
2227 _conflict = false;
2229 return;
2230 }
2232 // Tracing flag
2233 _did_rewriting = true;
2235 if (TraceOopMapRewrites) {
2236 tty->print_cr("ref/value conflict for method %s - bytecodes are getting rewritten", method()->name()->as_C_string());
2237 method()->print();
2238 method()->print_codes();
2239 }
2241 assert(_new_var_map!=NULL, "nothing to rewrite");
2242 assert(_conflict==true, "We should not be here");
2244 compute_ret_adr_at_TOS();
2245 if (!_got_error) {
2246 for (int k = 0; k < _max_locals && !_got_error; k++) {
2247 if (_new_var_map[k] != k) {
2248 if (TraceOopMapRewrites) {
2249 tty->print_cr("Rewriting: %d -> %d", k, _new_var_map[k]);
2250 }
2251 rewrite_refval_conflict(k, _new_var_map[k]);
2252 if (_got_error) return;
2253 nof_conflicts++;
2254 }
2255 }
2256 }
2258 assert(nof_conflicts == _nof_refval_conflicts, "sanity check");
2260 // Adjust the number of locals
2261 method()->set_max_locals(_max_locals+_nof_refval_conflicts);
2262 _max_locals += _nof_refval_conflicts;
2264 // That was that...
2265 _new_var_map = NULL;
2266 _nof_refval_conflicts = 0;
2267 }
2269 void GenerateOopMap::rewrite_refval_conflict(int from, int to) {
2270 bool startOver;
2271 do {
2272 // Make sure that the BytecodeStream is constructed in the loop, since
2273 // during rewriting a new method oop is going to be used, and the next time
2274 // around we want to use that.
2275 BytecodeStream bcs(_method);
2276 startOver = false;
2278 while( bcs.next() >=0 && !startOver && !_got_error) {
2279 startOver = rewrite_refval_conflict_inst(&bcs, from, to);
2280 }
2281 } while (startOver && !_got_error);
2282 }
2284 /* If the current instruction is one that uses local variable "from"
2285 in a ref way, change it to use "to". There's a subtle reason why we
2286 renumber the ref uses and not the non-ref uses: non-ref uses may be
2287 2 slots wide (double, long) which would necessitate keeping track of
2288 whether we should add one or two variables to the method. If the change
2289 affected the width of some instruction, returns "TRUE"; otherwise, returns "FALSE".
2290 Another reason for moving ref's value is for solving (addr, ref) conflicts, which
2291 both uses aload/astore methods.
2292 */
2293 bool GenerateOopMap::rewrite_refval_conflict_inst(BytecodeStream *itr, int from, int to) {
2294 Bytecodes::Code bc = itr->code();
2295 int index;
2296 int bci = itr->bci();
2298 if (is_aload(itr, &index) && index == from) {
2299 if (TraceOopMapRewrites) {
2300 tty->print_cr("Rewriting aload at bci: %d", bci);
2301 }
2302 return rewrite_load_or_store(itr, Bytecodes::_aload, Bytecodes::_aload_0, to);
2303 }
2305 if (is_astore(itr, &index) && index == from) {
2306 if (!stack_top_holds_ret_addr(bci)) {
2307 if (TraceOopMapRewrites) {
2308 tty->print_cr("Rewriting astore at bci: %d", bci);
2309 }
2310 return rewrite_load_or_store(itr, Bytecodes::_astore, Bytecodes::_astore_0, to);
2311 } else {
2312 if (TraceOopMapRewrites) {
2313 tty->print_cr("Supress rewriting of astore at bci: %d", bci);
2314 }
2315 }
2316 }
2318 return false;
2319 }
2321 // The argument to this method is:
2322 // bc : Current bytecode
2323 // bcN : either _aload or _astore
2324 // bc0 : either _aload_0 or _astore_0
2325 bool GenerateOopMap::rewrite_load_or_store(BytecodeStream *bcs, Bytecodes::Code bcN, Bytecodes::Code bc0, unsigned int varNo) {
2326 assert(bcN == Bytecodes::_astore || bcN == Bytecodes::_aload, "wrong argument (bcN)");
2327 assert(bc0 == Bytecodes::_astore_0 || bc0 == Bytecodes::_aload_0, "wrong argument (bc0)");
2328 int ilen = Bytecodes::length_at(bcs->bcp());
2329 int newIlen;
2331 if (ilen == 4) {
2332 // Original instruction was wide; keep it wide for simplicity
2333 newIlen = 4;
2334 } else if (varNo < 4)
2335 newIlen = 1;
2336 else if (varNo >= 256)
2337 newIlen = 4;
2338 else
2339 newIlen = 2;
2341 // If we need to relocate in order to patch the byte, we
2342 // do the patching in a temp. buffer, that is passed to the reloc.
2343 // The patching of the bytecode stream is then done by the Relocator.
2344 // This is neccesary, since relocating the instruction at a certain bci, might
2345 // also relocate that instruction, e.g., if a _goto before it gets widen to a _goto_w.
2346 // Hence, we do not know which bci to patch after relocation.
2348 assert(newIlen <= 4, "sanity check");
2349 u_char inst_buffer[4]; // Max. instruction size is 4.
2350 address bcp;
2352 if (newIlen != ilen) {
2353 // Relocation needed do patching in temp. buffer
2354 bcp = (address)inst_buffer;
2355 } else {
2356 bcp = _method->bcp_from(bcs->bci());
2357 }
2359 // Patch either directly in methodOop or in temp. buffer
2360 if (newIlen == 1) {
2361 assert(varNo < 4, "varNo too large");
2362 *bcp = bc0 + varNo;
2363 } else if (newIlen == 2) {
2364 assert(varNo < 256, "2-byte index needed!");
2365 *(bcp + 0) = bcN;
2366 *(bcp + 1) = varNo;
2367 } else {
2368 assert(newIlen == 4, "Wrong instruction length");
2369 *(bcp + 0) = Bytecodes::_wide;
2370 *(bcp + 1) = bcN;
2371 Bytes::put_Java_u2(bcp+2, varNo);
2372 }
2374 if (newIlen != ilen) {
2375 expand_current_instr(bcs->bci(), ilen, newIlen, inst_buffer);
2376 }
2379 return (newIlen != ilen);
2380 }
2382 class RelocCallback : public RelocatorListener {
2383 private:
2384 GenerateOopMap* _gom;
2385 public:
2386 RelocCallback(GenerateOopMap* gom) { _gom = gom; };
2388 // Callback method
2389 virtual void relocated(int bci, int delta, int new_code_length) {
2390 _gom->update_basic_blocks (bci, delta, new_code_length);
2391 _gom->update_ret_adr_at_TOS(bci, delta);
2392 _gom->_rt.update_ret_table (bci, delta);
2393 }
2394 };
2396 // Returns true if expanding was succesful. Otherwise, reports an error and
2397 // returns false.
2398 void GenerateOopMap::expand_current_instr(int bci, int ilen, int newIlen, u_char inst_buffer[]) {
2399 Thread *THREAD = Thread::current(); // Could really have TRAPS argument.
2400 RelocCallback rcb(this);
2401 Relocator rc(_method, &rcb);
2402 methodHandle m= rc.insert_space_at(bci, newIlen, inst_buffer, THREAD);
2403 if (m.is_null() || HAS_PENDING_EXCEPTION) {
2404 report_error("could not rewrite method - exception occurred or bytecode buffer overflow");
2405 return;
2406 }
2408 // Relocator returns a new method oop.
2409 _did_relocation = true;
2410 _method = m;
2411 }
2414 bool GenerateOopMap::is_astore(BytecodeStream *itr, int *index) {
2415 Bytecodes::Code bc = itr->code();
2416 switch(bc) {
2417 case Bytecodes::_astore_0:
2418 case Bytecodes::_astore_1:
2419 case Bytecodes::_astore_2:
2420 case Bytecodes::_astore_3:
2421 *index = bc - Bytecodes::_astore_0;
2422 return true;
2423 case Bytecodes::_astore:
2424 *index = itr->get_index();
2425 return true;
2426 }
2427 return false;
2428 }
2430 bool GenerateOopMap::is_aload(BytecodeStream *itr, int *index) {
2431 Bytecodes::Code bc = itr->code();
2432 switch(bc) {
2433 case Bytecodes::_aload_0:
2434 case Bytecodes::_aload_1:
2435 case Bytecodes::_aload_2:
2436 case Bytecodes::_aload_3:
2437 *index = bc - Bytecodes::_aload_0;
2438 return true;
2440 case Bytecodes::_aload:
2441 *index = itr->get_index();
2442 return true;
2443 }
2444 return false;
2445 }
2448 // Return true iff the top of the operand stack holds a return address at
2449 // the current instruction
2450 bool GenerateOopMap::stack_top_holds_ret_addr(int bci) {
2451 for(int i = 0; i < _ret_adr_tos->length(); i++) {
2452 if (_ret_adr_tos->at(i) == bci)
2453 return true;
2454 }
2456 return false;
2457 }
2459 void GenerateOopMap::compute_ret_adr_at_TOS() {
2460 assert(_ret_adr_tos != NULL, "must be initialized");
2461 _ret_adr_tos->clear();
2463 for (int i = 0; i < bb_count(); i++) {
2464 BasicBlock* bb = &_basic_blocks[i];
2466 // Make sure to only check basicblocks that are reachable
2467 if (bb->is_reachable()) {
2469 // For each Basic block we check all instructions
2470 BytecodeStream bcs(_method);
2471 bcs.set_interval(bb->_bci, next_bb_start_pc(bb));
2473 restore_state(bb);
2475 while (bcs.next()>=0 && !_got_error) {
2476 // TDT: should this be is_good_address() ?
2477 if (_stack_top > 0 && stack()[_stack_top-1].is_address()) {
2478 _ret_adr_tos->append(bcs.bci());
2479 if (TraceNewOopMapGeneration) {
2480 tty->print_cr("Ret_adr TOS at bci: %d", bcs.bci());
2481 }
2482 }
2483 interp1(&bcs);
2484 }
2485 }
2486 }
2487 }
2489 void GenerateOopMap::update_ret_adr_at_TOS(int bci, int delta) {
2490 for(int i = 0; i < _ret_adr_tos->length(); i++) {
2491 int v = _ret_adr_tos->at(i);
2492 if (v > bci) _ret_adr_tos->at_put(i, v + delta);
2493 }
2494 }
2496 // ===================================================================
2498 #ifndef PRODUCT
2499 int ResolveOopMapConflicts::_nof_invocations = 0;
2500 int ResolveOopMapConflicts::_nof_rewrites = 0;
2501 int ResolveOopMapConflicts::_nof_relocations = 0;
2502 #endif
2504 methodHandle ResolveOopMapConflicts::do_potential_rewrite(TRAPS) {
2505 compute_map(CHECK_(methodHandle()));
2507 #ifndef PRODUCT
2508 // Tracking and statistics
2509 if (PrintRewrites) {
2510 _nof_invocations++;
2511 if (did_rewriting()) {
2512 _nof_rewrites++;
2513 if (did_relocation()) _nof_relocations++;
2514 tty->print("Method was rewritten %s: ", (did_relocation()) ? "and relocated" : "");
2515 method()->print_value(); tty->cr();
2516 tty->print_cr("Cand.: %d rewrts: %d (%d%%) reloc.: %d (%d%%)",
2517 _nof_invocations,
2518 _nof_rewrites, (_nof_rewrites * 100) / _nof_invocations,
2519 _nof_relocations, (_nof_relocations * 100) / _nof_invocations);
2520 }
2521 }
2522 #endif
2523 return methodHandle(THREAD, method());
2524 }