Mon, 09 Jun 2008 11:51:19 -0400
6687581: Make CMS work with compressed oops
Summary: Make FreeChunk read markword instead of LSB in _klass pointer to indicate that it's a FreeChunk for compressed oops.
Reviewed-by: ysr, jmasa
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 int size = binsToHold(method()->code_size());
374 _bb_hdr_bits = NEW_RESOURCE_ARRAY(uintptr_t,size);
375 memset(_bb_hdr_bits, 0, size*sizeof(uintptr_t));
376 }
378 void GenerateOopMap ::set_bbmark_bit(int bci) {
379 int idx = bci >> LogBitsPerWord;
380 uintptr_t bit = (uintptr_t)1 << (bci & (BitsPerWord-1));
381 _bb_hdr_bits[idx] |= bit;
382 }
384 void GenerateOopMap ::clear_bbmark_bit(int bci) {
385 int idx = bci >> LogBitsPerWord;
386 uintptr_t bit = (uintptr_t)1 << (bci & (BitsPerWord-1));
387 _bb_hdr_bits[idx] &= (~bit);
388 }
390 void GenerateOopMap::bb_mark_fct(GenerateOopMap *c, int bci, int *data) {
391 assert(bci>= 0 && bci < c->method()->code_size(), "index out of bounds");
392 if (c->is_bb_header(bci))
393 return;
395 if (TraceNewOopMapGeneration) {
396 tty->print_cr("Basicblock#%d begins at: %d", c->_bb_count, bci);
397 }
398 c->set_bbmark_bit(bci);
399 c->_bb_count++;
400 }
403 void GenerateOopMap::mark_bbheaders_and_count_gc_points() {
404 initialize_bb();
406 bool fellThrough = false; // False to get first BB marked.
408 // First mark all exception handlers as start of a basic-block
409 typeArrayOop excps = method()->exception_table();
410 for(int i = 0; i < excps->length(); i += 4) {
411 int handler_pc_idx = i+2;
412 bb_mark_fct(this, excps->int_at(handler_pc_idx), NULL);
413 }
415 // Then iterate through the code
416 BytecodeStream bcs(_method);
417 Bytecodes::Code bytecode;
419 while( (bytecode = bcs.next()) >= 0) {
420 int bci = bcs.bci();
422 if (!fellThrough)
423 bb_mark_fct(this, bci, NULL);
425 fellThrough = jump_targets_do(&bcs, &GenerateOopMap::bb_mark_fct, NULL);
427 /* We will also mark successors of jsr's as basic block headers. */
428 switch (bytecode) {
429 case Bytecodes::_jsr:
430 assert(!fellThrough, "should not happen");
431 bb_mark_fct(this, bci + Bytecodes::length_for(bytecode), NULL);
432 break;
433 case Bytecodes::_jsr_w:
434 assert(!fellThrough, "should not happen");
435 bb_mark_fct(this, bci + Bytecodes::length_for(bytecode), NULL);
436 break;
437 }
439 if (possible_gc_point(&bcs))
440 _gc_points++;
441 }
442 }
444 void GenerateOopMap::reachable_basicblock(GenerateOopMap *c, int bci, int *data) {
445 assert(bci>= 0 && bci < c->method()->code_size(), "index out of bounds");
446 BasicBlock* bb = c->get_basic_block_at(bci);
447 if (bb->is_dead()) {
448 bb->mark_as_alive();
449 *data = 1; // Mark basicblock as changed
450 }
451 }
454 void GenerateOopMap::mark_reachable_code() {
455 int change = 1; // int to get function pointers to work
457 // Mark entry basic block as alive and all exception handlers
458 _basic_blocks[0].mark_as_alive();
459 typeArrayOop excps = method()->exception_table();
460 for(int i = 0; i < excps->length(); i += 4) {
461 int handler_pc_idx = i+2;
462 BasicBlock *bb = get_basic_block_at(excps->int_at(handler_pc_idx));
463 // If block is not already alive (due to multiple exception handlers to same bb), then
464 // make it alive
465 if (bb->is_dead()) bb->mark_as_alive();
466 }
468 BytecodeStream bcs(_method);
470 // Iterate through all basic blocks until we reach a fixpoint
471 while (change) {
472 change = 0;
474 for (int i = 0; i < _bb_count; i++) {
475 BasicBlock *bb = &_basic_blocks[i];
476 if (bb->is_alive()) {
477 // Position bytecodestream at last bytecode in basicblock
478 bcs.set_start(bb->_end_bci);
479 bcs.next();
480 Bytecodes::Code bytecode = bcs.code();
481 int bci = bcs.bci();
482 assert(bci == bb->_end_bci, "wrong bci");
484 bool fell_through = jump_targets_do(&bcs, &GenerateOopMap::reachable_basicblock, &change);
486 // We will also mark successors of jsr's as alive.
487 switch (bytecode) {
488 case Bytecodes::_jsr:
489 case Bytecodes::_jsr_w:
490 assert(!fell_through, "should not happen");
491 reachable_basicblock(this, bci + Bytecodes::length_for(bytecode), &change);
492 break;
493 }
494 if (fell_through) {
495 // Mark successor as alive
496 if (bb[1].is_dead()) {
497 bb[1].mark_as_alive();
498 change = 1;
499 }
500 }
501 }
502 }
503 }
504 }
506 /* If the current instruction in "c" has no effect on control flow,
507 returns "true". Otherwise, calls "jmpFct" one or more times, with
508 "c", an appropriate "pcDelta", and "data" as arguments, then
509 returns "false". There is one exception: if the current
510 instruction is a "ret", returns "false" without calling "jmpFct".
511 Arrangements for tracking the control flow of a "ret" must be made
512 externally. */
513 bool GenerateOopMap::jump_targets_do(BytecodeStream *bcs, jmpFct_t jmpFct, int *data) {
514 int bci = bcs->bci();
516 switch (bcs->code()) {
517 case Bytecodes::_ifeq:
518 case Bytecodes::_ifne:
519 case Bytecodes::_iflt:
520 case Bytecodes::_ifge:
521 case Bytecodes::_ifgt:
522 case Bytecodes::_ifle:
523 case Bytecodes::_if_icmpeq:
524 case Bytecodes::_if_icmpne:
525 case Bytecodes::_if_icmplt:
526 case Bytecodes::_if_icmpge:
527 case Bytecodes::_if_icmpgt:
528 case Bytecodes::_if_icmple:
529 case Bytecodes::_if_acmpeq:
530 case Bytecodes::_if_acmpne:
531 case Bytecodes::_ifnull:
532 case Bytecodes::_ifnonnull:
533 (*jmpFct)(this, bcs->dest(), data);
534 (*jmpFct)(this, bci + 3, data);
535 break;
537 case Bytecodes::_goto:
538 (*jmpFct)(this, bcs->dest(), data);
539 break;
540 case Bytecodes::_goto_w:
541 (*jmpFct)(this, bcs->dest_w(), data);
542 break;
543 case Bytecodes::_tableswitch:
544 { Bytecode_tableswitch *tableswitch = Bytecode_tableswitch_at(bcs->bcp());
545 int len = tableswitch->length();
547 (*jmpFct)(this, bci + tableswitch->default_offset(), data); /* Default. jump address */
548 while (--len >= 0) {
549 (*jmpFct)(this, bci + tableswitch->dest_offset_at(len), data);
550 }
551 break;
552 }
554 case Bytecodes::_lookupswitch:
555 { Bytecode_lookupswitch *lookupswitch = Bytecode_lookupswitch_at(bcs->bcp());
556 int npairs = lookupswitch->number_of_pairs();
557 (*jmpFct)(this, bci + lookupswitch->default_offset(), data); /* Default. */
558 while(--npairs >= 0) {
559 LookupswitchPair *pair = lookupswitch->pair_at(npairs);
560 (*jmpFct)(this, bci + pair->offset(), data);
561 }
562 break;
563 }
564 case Bytecodes::_jsr:
565 assert(bcs->is_wide()==false, "sanity check");
566 (*jmpFct)(this, bcs->dest(), data);
570 break;
571 case Bytecodes::_jsr_w:
572 (*jmpFct)(this, bcs->dest_w(), data);
573 break;
574 case Bytecodes::_wide:
575 ShouldNotReachHere();
576 return true;
577 break;
578 case Bytecodes::_athrow:
579 case Bytecodes::_ireturn:
580 case Bytecodes::_lreturn:
581 case Bytecodes::_freturn:
582 case Bytecodes::_dreturn:
583 case Bytecodes::_areturn:
584 case Bytecodes::_return:
585 case Bytecodes::_ret:
586 break;
587 default:
588 return true;
589 }
590 return false;
591 }
593 /* Requires "pc" to be the head of a basic block; returns that basic
594 block. */
595 BasicBlock *GenerateOopMap::get_basic_block_at(int bci) const {
596 BasicBlock* bb = get_basic_block_containing(bci);
597 assert(bb->_bci == bci, "should have found BB");
598 return bb;
599 }
601 // Requires "pc" to be the start of an instruction; returns the basic
602 // block containing that instruction. */
603 BasicBlock *GenerateOopMap::get_basic_block_containing(int bci) const {
604 BasicBlock *bbs = _basic_blocks;
605 int lo = 0, hi = _bb_count - 1;
607 while (lo <= hi) {
608 int m = (lo + hi) / 2;
609 int mbci = bbs[m]._bci;
610 int nbci;
612 if ( m == _bb_count-1) {
613 assert( bci >= mbci && bci < method()->code_size(), "sanity check failed");
614 return bbs+m;
615 } else {
616 nbci = bbs[m+1]._bci;
617 }
619 if ( mbci <= bci && bci < nbci) {
620 return bbs+m;
621 } else if (mbci < bci) {
622 lo = m + 1;
623 } else {
624 assert(mbci > bci, "sanity check");
625 hi = m - 1;
626 }
627 }
629 fatal("should have found BB");
630 return NULL;
631 }
633 void GenerateOopMap::restore_state(BasicBlock *bb)
634 {
635 memcpy(_state, bb->_state, _state_len*sizeof(CellTypeState));
636 _stack_top = bb->_stack_top;
637 _monitor_top = bb->_monitor_top;
638 }
640 int GenerateOopMap::next_bb_start_pc(BasicBlock *bb) {
641 int bbNum = bb - _basic_blocks + 1;
642 if (bbNum == _bb_count)
643 return method()->code_size();
645 return _basic_blocks[bbNum]._bci;
646 }
648 //
649 // CellType handling methods
650 //
652 void GenerateOopMap::init_state() {
653 _state_len = _max_locals + _max_stack + _max_monitors;
654 _state = NEW_RESOURCE_ARRAY(CellTypeState, _state_len);
655 memset(_state, 0, _state_len * sizeof(CellTypeState));
656 _state_vec_buf = NEW_RESOURCE_ARRAY(char, MAX3(_max_locals, _max_stack, _max_monitors) + 1/*for null terminator char */);
657 }
659 void GenerateOopMap::make_context_uninitialized() {
660 CellTypeState* vs = vars();
662 for (int i = 0; i < _max_locals; i++)
663 vs[i] = CellTypeState::uninit;
665 _stack_top = 0;
666 _monitor_top = 0;
667 }
669 int GenerateOopMap::methodsig_to_effect(symbolOop signature, bool is_static, CellTypeState* effect) {
670 ComputeEntryStack ces(signature);
671 return ces.compute_for_parameters(is_static, effect);
672 }
674 // Return result of merging cts1 and cts2.
675 CellTypeState CellTypeState::merge(CellTypeState cts, int slot) const {
676 CellTypeState result;
678 assert(!is_bottom() && !cts.is_bottom(),
679 "merge of bottom values is handled elsewhere");
681 result._state = _state | cts._state;
683 // If the top bit is set, we don't need to do any more work.
684 if (!result.is_info_top()) {
685 assert((result.can_be_address() || result.can_be_reference()),
686 "only addresses and references have non-top info");
688 if (!equal(cts)) {
689 // The two values being merged are different. Raise to top.
690 if (result.is_reference()) {
691 result = CellTypeState::make_slot_ref(slot);
692 } else {
693 result._state |= info_conflict;
694 }
695 }
696 }
697 assert(result.is_valid_state(), "checking that CTS merge maintains legal state");
699 return result;
700 }
702 // Merge the variable state for locals and stack from cts into bbts.
703 bool GenerateOopMap::merge_local_state_vectors(CellTypeState* cts,
704 CellTypeState* bbts) {
705 int i;
706 int len = _max_locals + _stack_top;
707 bool change = false;
709 for (i = len - 1; i >= 0; i--) {
710 CellTypeState v = cts[i].merge(bbts[i], i);
711 change = change || !v.equal(bbts[i]);
712 bbts[i] = v;
713 }
715 return change;
716 }
718 // Merge the monitor stack state from cts into bbts.
719 bool GenerateOopMap::merge_monitor_state_vectors(CellTypeState* cts,
720 CellTypeState* bbts) {
721 bool change = false;
722 if (_max_monitors > 0 && _monitor_top != bad_monitors) {
723 // If there are no monitors in the program, or there has been
724 // a monitor matching error before this point in the program,
725 // then we do not merge in the monitor state.
727 int base = _max_locals + _max_stack;
728 int len = base + _monitor_top;
729 for (int i = len - 1; i >= base; i--) {
730 CellTypeState v = cts[i].merge(bbts[i], i);
732 // Can we prove that, when there has been a change, it will already
733 // have been detected at this point? That would make this equal
734 // check here unnecessary.
735 change = change || !v.equal(bbts[i]);
736 bbts[i] = v;
737 }
738 }
740 return change;
741 }
743 void GenerateOopMap::copy_state(CellTypeState *dst, CellTypeState *src) {
744 int len = _max_locals + _stack_top;
745 for (int i = 0; i < len; i++) {
746 if (src[i].is_nonlock_reference()) {
747 dst[i] = CellTypeState::make_slot_ref(i);
748 } else {
749 dst[i] = src[i];
750 }
751 }
752 if (_max_monitors > 0 && _monitor_top != bad_monitors) {
753 int base = _max_locals + _max_stack;
754 len = base + _monitor_top;
755 for (int i = base; i < len; i++) {
756 dst[i] = src[i];
757 }
758 }
759 }
762 // Merge the states for the current block and the next. As long as a
763 // block is reachable the locals and stack must be merged. If the
764 // stack heights don't match then this is a verification error and
765 // it's impossible to interpret the code. Simultaneously monitor
766 // states are being check to see if they nest statically. If monitor
767 // depths match up then their states are merged. Otherwise the
768 // mismatch is simply recorded and interpretation continues since
769 // monitor matching is purely informational and doesn't say anything
770 // about the correctness of the code.
771 void GenerateOopMap::merge_state_into_bb(BasicBlock *bb) {
772 assert(bb->is_alive(), "merging state into a dead basicblock");
774 if (_stack_top == bb->_stack_top) {
775 // always merge local state even if monitors don't match.
776 if (merge_local_state_vectors(_state, bb->_state)) {
777 bb->set_changed(true);
778 }
779 if (_monitor_top == bb->_monitor_top) {
780 // monitors still match so continue merging monitor states.
781 if (merge_monitor_state_vectors(_state, bb->_state)) {
782 bb->set_changed(true);
783 }
784 } else {
785 if (TraceMonitorMismatch) {
786 report_monitor_mismatch("monitor stack height merge conflict");
787 }
788 // When the monitor stacks are not matched, we set _monitor_top to
789 // bad_monitors. This signals that, from here on, the monitor stack cannot
790 // be trusted. In particular, monitorexit bytecodes may throw
791 // exceptions. We mark this block as changed so that the change
792 // propagates properly.
793 bb->_monitor_top = bad_monitors;
794 bb->set_changed(true);
795 _monitor_safe = false;
796 }
797 } else if (!bb->is_reachable()) {
798 // First time we look at this BB
799 copy_state(bb->_state, _state);
800 bb->_stack_top = _stack_top;
801 bb->_monitor_top = _monitor_top;
802 bb->set_changed(true);
803 } else {
804 verify_error("stack height conflict: %d vs. %d", _stack_top, bb->_stack_top);
805 }
806 }
808 void GenerateOopMap::merge_state(GenerateOopMap *gom, int bci, int* data) {
809 gom->merge_state_into_bb(gom->get_basic_block_at(bci));
810 }
812 void GenerateOopMap::set_var(int localNo, CellTypeState cts) {
813 assert(cts.is_reference() || cts.is_value() || cts.is_address(),
814 "wrong celltypestate");
815 if (localNo < 0 || localNo > _max_locals) {
816 verify_error("variable write error: r%d", localNo);
817 return;
818 }
819 vars()[localNo] = cts;
820 }
822 CellTypeState GenerateOopMap::get_var(int localNo) {
823 assert(localNo < _max_locals + _nof_refval_conflicts, "variable read error")
824 if (localNo < 0 || localNo > _max_locals) {
825 verify_error("variable read error: r%d", localNo);
826 return valCTS; // just to pick something;
827 }
828 return vars()[localNo];
829 }
831 CellTypeState GenerateOopMap::pop() {
832 if ( _stack_top <= 0) {
833 verify_error("stack underflow");
834 return valCTS; // just to pick something
835 }
836 return stack()[--_stack_top];
837 }
839 void GenerateOopMap::push(CellTypeState cts) {
840 if ( _stack_top >= _max_stack) {
841 verify_error("stack overflow");
842 return;
843 }
844 stack()[_stack_top++] = cts;
845 }
847 CellTypeState GenerateOopMap::monitor_pop() {
848 assert(_monitor_top != bad_monitors, "monitor_pop called on error monitor stack");
849 if (_monitor_top == 0) {
850 // We have detected a pop of an empty monitor stack.
851 _monitor_safe = false;
852 _monitor_top = bad_monitors;
854 if (TraceMonitorMismatch) {
855 report_monitor_mismatch("monitor stack underflow");
856 }
857 return CellTypeState::ref; // just to keep the analysis going.
858 }
859 return monitors()[--_monitor_top];
860 }
862 void GenerateOopMap::monitor_push(CellTypeState cts) {
863 assert(_monitor_top != bad_monitors, "monitor_push called on error monitor stack");
864 if (_monitor_top >= _max_monitors) {
865 // Some monitorenter is being executed more than once.
866 // This means that the monitor stack cannot be simulated.
867 _monitor_safe = false;
868 _monitor_top = bad_monitors;
870 if (TraceMonitorMismatch) {
871 report_monitor_mismatch("monitor stack overflow");
872 }
873 return;
874 }
875 monitors()[_monitor_top++] = cts;
876 }
878 //
879 // Interpretation handling methods
880 //
882 void GenerateOopMap::do_interpretation()
883 {
884 // "i" is just for debugging, so we can detect cases where this loop is
885 // iterated more than once.
886 int i = 0;
887 do {
888 #ifndef PRODUCT
889 if (TraceNewOopMapGeneration) {
890 tty->print("\n\nIteration #%d of do_interpretation loop, method:\n", i);
891 method()->print_name(tty);
892 tty->print("\n\n");
893 }
894 #endif
895 _conflict = false;
896 _monitor_safe = true;
897 // init_state is now called from init_basic_blocks. The length of a
898 // state vector cannot be determined until we have made a pass through
899 // the bytecodes counting the possible monitor entries.
900 if (!_got_error) init_basic_blocks();
901 if (!_got_error) setup_method_entry_state();
902 if (!_got_error) interp_all();
903 if (!_got_error) rewrite_refval_conflicts();
904 i++;
905 } while (_conflict && !_got_error);
906 }
908 void GenerateOopMap::init_basic_blocks() {
909 // Note: Could consider reserving only the needed space for each BB's state
910 // (entry stack may not be of maximal height for every basic block).
911 // But cumbersome since we don't know the stack heights yet. (Nor the
912 // monitor stack heights...)
914 _basic_blocks = NEW_RESOURCE_ARRAY(BasicBlock, _bb_count);
916 // Make a pass through the bytecodes. Count the number of monitorenters.
917 // This can be used an upper bound on the monitor stack depth in programs
918 // which obey stack discipline with their monitor usage. Initialize the
919 // known information about basic blocks.
920 BytecodeStream j(_method);
921 Bytecodes::Code bytecode;
923 int bbNo = 0;
924 int monitor_count = 0;
925 int prev_bci = -1;
926 while( (bytecode = j.next()) >= 0) {
927 if (j.code() == Bytecodes::_monitorenter) {
928 monitor_count++;
929 }
931 int bci = j.bci();
932 if (is_bb_header(bci)) {
933 // Initialize the basicblock structure
934 BasicBlock *bb = _basic_blocks + bbNo;
935 bb->_bci = bci;
936 bb->_max_locals = _max_locals;
937 bb->_max_stack = _max_stack;
938 bb->set_changed(false);
939 bb->_stack_top = BasicBlock::_dead_basic_block; // Initialize all basicblocks are dead.
940 bb->_monitor_top = bad_monitors;
942 if (bbNo > 0) {
943 _basic_blocks[bbNo - 1]._end_bci = prev_bci;
944 }
946 bbNo++;
947 }
948 // Remember prevous bci.
949 prev_bci = bci;
950 }
951 // Set
952 _basic_blocks[bbNo-1]._end_bci = prev_bci;
955 _max_monitors = monitor_count;
957 // Now that we have a bound on the depth of the monitor stack, we can
958 // initialize the CellTypeState-related information.
959 init_state();
961 // We allocate space for all state-vectors for all basicblocks in one huge chuck.
962 // Then in the next part of the code, we set a pointer in each _basic_block that
963 // points to each piece.
964 CellTypeState *basicBlockState = NEW_RESOURCE_ARRAY(CellTypeState, bbNo * _state_len);
965 memset(basicBlockState, 0, bbNo * _state_len * sizeof(CellTypeState));
967 // Make a pass over the basicblocks and assign their state vectors.
968 for (int blockNum=0; blockNum < bbNo; blockNum++) {
969 BasicBlock *bb = _basic_blocks + blockNum;
970 bb->_state = basicBlockState + blockNum * _state_len;
972 #ifdef ASSERT
973 if (blockNum + 1 < bbNo) {
974 address bcp = _method->bcp_from(bb->_end_bci);
975 int bc_len = Bytecodes::java_length_at(bcp);
976 assert(bb->_end_bci + bc_len == bb[1]._bci, "unmatched bci info in basicblock");
977 }
978 #endif
979 }
980 #ifdef ASSERT
981 { BasicBlock *bb = &_basic_blocks[bbNo-1];
982 address bcp = _method->bcp_from(bb->_end_bci);
983 int bc_len = Bytecodes::java_length_at(bcp);
984 assert(bb->_end_bci + bc_len == _method->code_size(), "wrong end bci");
985 }
986 #endif
988 // Check that the correct number of basicblocks was found
989 if (bbNo !=_bb_count) {
990 if (bbNo < _bb_count) {
991 verify_error("jump into the middle of instruction?");
992 return;
993 } else {
994 verify_error("extra basic blocks - should not happen?");
995 return;
996 }
997 }
999 // Mark all alive blocks
1000 mark_reachable_code();
1001 }
1003 void GenerateOopMap::setup_method_entry_state() {
1005 // Initialize all locals to 'uninit' and set stack-height to 0
1006 make_context_uninitialized();
1008 // Initialize CellState type of arguments
1009 methodsig_to_effect(method()->signature(), method()->is_static(), vars());
1011 // If some references must be pre-assigned to null, then set that up
1012 initialize_vars();
1014 // This is the start state
1015 merge_state_into_bb(&_basic_blocks[0]);
1017 assert(_basic_blocks[0].changed(), "we are not getting off the ground");
1018 }
1020 // The instruction at bci is changing size by "delta". Update the basic blocks.
1021 void GenerateOopMap::update_basic_blocks(int bci, int delta,
1022 int new_method_size) {
1023 assert(new_method_size >= method()->code_size() + delta,
1024 "new method size is too small");
1025 int newWords = binsToHold(new_method_size);
1027 uintptr_t * new_bb_hdr_bits = NEW_RESOURCE_ARRAY(uintptr_t, newWords);
1029 BitMap bb_bits(new_bb_hdr_bits, new_method_size);
1030 bb_bits.clear();
1032 for(int k = 0; k < _bb_count; k++) {
1033 if (_basic_blocks[k]._bci > bci) {
1034 _basic_blocks[k]._bci += delta;
1035 _basic_blocks[k]._end_bci += delta;
1036 }
1037 bb_bits.at_put(_basic_blocks[k]._bci, true);
1038 }
1039 _bb_hdr_bits = new_bb_hdr_bits ;
1040 }
1042 //
1043 // Initvars handling
1044 //
1046 void GenerateOopMap::initialize_vars() {
1047 for (int k = 0; k < _init_vars->length(); k++)
1048 _state[_init_vars->at(k)] = CellTypeState::make_slot_ref(k);
1049 }
1051 void GenerateOopMap::add_to_ref_init_set(int localNo) {
1053 if (TraceNewOopMapGeneration)
1054 tty->print_cr("Added init vars: %d", localNo);
1056 // Is it already in the set?
1057 if (_init_vars->contains(localNo) )
1058 return;
1060 _init_vars->append(localNo);
1061 }
1063 //
1064 // Interpreration code
1065 //
1067 void GenerateOopMap::interp_all() {
1068 bool change = true;
1070 while (change && !_got_error) {
1071 change = false;
1072 for (int i = 0; i < _bb_count && !_got_error; i++) {
1073 BasicBlock *bb = &_basic_blocks[i];
1074 if (bb->changed()) {
1075 if (_got_error) return;
1076 change = true;
1077 bb->set_changed(false);
1078 interp_bb(bb);
1079 }
1080 }
1081 }
1082 }
1084 void GenerateOopMap::interp_bb(BasicBlock *bb) {
1086 // We do not want to do anything in case the basic-block has not been initialized. This
1087 // will happen in the case where there is dead-code hang around in a method.
1088 assert(bb->is_reachable(), "should be reachable or deadcode exist");
1089 restore_state(bb);
1091 BytecodeStream itr(_method);
1093 // Set iterator interval to be the current basicblock
1094 int lim_bci = next_bb_start_pc(bb);
1095 itr.set_interval(bb->_bci, lim_bci);
1096 assert(lim_bci != bb->_bci, "must be at least one instruction in a basicblock");
1097 itr.next(); // read first instruction
1099 // Iterates through all bytecodes except the last in a basic block.
1100 // We handle the last one special, since there is controlflow change.
1101 while(itr.next_bci() < lim_bci && !_got_error) {
1102 if (_has_exceptions || _monitor_top != 0) {
1103 // We do not need to interpret the results of exceptional
1104 // continuation from this instruction when the method has no
1105 // exception handlers and the monitor stack is currently
1106 // empty.
1107 do_exception_edge(&itr);
1108 }
1109 interp1(&itr);
1110 itr.next();
1111 }
1113 // Handle last instruction.
1114 if (!_got_error) {
1115 assert(itr.next_bci() == lim_bci, "must point to end");
1116 if (_has_exceptions || _monitor_top != 0) {
1117 do_exception_edge(&itr);
1118 }
1119 interp1(&itr);
1121 bool fall_through = jump_targets_do(&itr, GenerateOopMap::merge_state, NULL);
1122 if (_got_error) return;
1124 if (itr.code() == Bytecodes::_ret) {
1125 assert(!fall_through, "cannot be set if ret instruction");
1126 // Automatically handles 'wide' ret indicies
1127 ret_jump_targets_do(&itr, GenerateOopMap::merge_state, itr.get_index(), NULL);
1128 } else if (fall_through) {
1129 // Hit end of BB, but the instr. was a fall-through instruction,
1130 // so perform transition as if the BB ended in a "jump".
1131 if (lim_bci != bb[1]._bci) {
1132 verify_error("bytecodes fell through last instruction");
1133 return;
1134 }
1135 merge_state_into_bb(bb + 1);
1136 }
1137 }
1138 }
1140 void GenerateOopMap::do_exception_edge(BytecodeStream* itr) {
1141 // Only check exception edge, if bytecode can trap
1142 if (!Bytecodes::can_trap(itr->code())) return;
1143 switch (itr->code()) {
1144 case Bytecodes::_aload_0:
1145 // These bytecodes can trap for rewriting. We need to assume that
1146 // they do not throw exceptions to make the monitor analysis work.
1147 return;
1149 case Bytecodes::_ireturn:
1150 case Bytecodes::_lreturn:
1151 case Bytecodes::_freturn:
1152 case Bytecodes::_dreturn:
1153 case Bytecodes::_areturn:
1154 case Bytecodes::_return:
1155 // If the monitor stack height is not zero when we leave the method,
1156 // then we are either exiting with a non-empty stack or we have
1157 // found monitor trouble earlier in our analysis. In either case,
1158 // assume an exception could be taken here.
1159 if (_monitor_top == 0) {
1160 return;
1161 }
1162 break;
1164 case Bytecodes::_monitorexit:
1165 // If the monitor stack height is bad_monitors, then we have detected a
1166 // monitor matching problem earlier in the analysis. If the
1167 // monitor stack height is 0, we are about to pop a monitor
1168 // off of an empty stack. In either case, the bytecode
1169 // could throw an exception.
1170 if (_monitor_top != bad_monitors && _monitor_top != 0) {
1171 return;
1172 }
1173 break;
1174 }
1176 if (_has_exceptions) {
1177 int bci = itr->bci();
1178 typeArrayOop exct = method()->exception_table();
1179 for(int i = 0; i< exct->length(); i+=4) {
1180 int start_pc = exct->int_at(i);
1181 int end_pc = exct->int_at(i+1);
1182 int handler_pc = exct->int_at(i+2);
1183 int catch_type = exct->int_at(i+3);
1185 if (start_pc <= bci && bci < end_pc) {
1186 BasicBlock *excBB = get_basic_block_at(handler_pc);
1187 CellTypeState *excStk = excBB->stack();
1188 CellTypeState *cOpStck = stack();
1189 CellTypeState cOpStck_0 = cOpStck[0];
1190 int cOpStackTop = _stack_top;
1192 // Exception stacks are always the same.
1193 assert(method()->max_stack() > 0, "sanity check");
1195 // We remembered the size and first element of "cOpStck"
1196 // above; now we temporarily set them to the appropriate
1197 // values for an exception handler. */
1198 cOpStck[0] = CellTypeState::make_slot_ref(_max_locals);
1199 _stack_top = 1;
1201 merge_state_into_bb(excBB);
1203 // Now undo the temporary change.
1204 cOpStck[0] = cOpStck_0;
1205 _stack_top = cOpStackTop;
1207 // If this is a "catch all" handler, then we do not need to
1208 // consider any additional handlers.
1209 if (catch_type == 0) {
1210 return;
1211 }
1212 }
1213 }
1214 }
1216 // It is possible that none of the exception handlers would have caught
1217 // the exception. In this case, we will exit the method. We must
1218 // ensure that the monitor stack is empty in this case.
1219 if (_monitor_top == 0) {
1220 return;
1221 }
1223 // We pessimistically assume that this exception can escape the
1224 // method. (It is possible that it will always be caught, but
1225 // we don't care to analyse the types of the catch clauses.)
1227 // We don't set _monitor_top to bad_monitors because there are no successors
1228 // to this exceptional exit.
1230 if (TraceMonitorMismatch && _monitor_safe) {
1231 // We check _monitor_safe so that we only report the first mismatched
1232 // exceptional exit.
1233 report_monitor_mismatch("non-empty monitor stack at exceptional exit");
1234 }
1235 _monitor_safe = false;
1237 }
1239 void GenerateOopMap::report_monitor_mismatch(const char *msg) {
1240 #ifndef PRODUCT
1241 tty->print(" Monitor mismatch in method ");
1242 method()->print_short_name(tty);
1243 tty->print_cr(": %s", msg);
1244 #endif
1245 }
1247 void GenerateOopMap::print_states(outputStream *os,
1248 CellTypeState* vec, int num) {
1249 for (int i = 0; i < num; i++) {
1250 vec[i].print(tty);
1251 }
1252 }
1254 // Print the state values at the current bytecode.
1255 void GenerateOopMap::print_current_state(outputStream *os,
1256 BytecodeStream *currentBC,
1257 bool detailed) {
1259 if (detailed) {
1260 os->print(" %4d vars = ", currentBC->bci());
1261 print_states(os, vars(), _max_locals);
1262 os->print(" %s", Bytecodes::name(currentBC->code()));
1263 switch(currentBC->code()) {
1264 case Bytecodes::_invokevirtual:
1265 case Bytecodes::_invokespecial:
1266 case Bytecodes::_invokestatic:
1267 case Bytecodes::_invokeinterface:
1268 int idx = currentBC->get_index_big();
1269 constantPoolOop cp = method()->constants();
1270 int nameAndTypeIdx = cp->name_and_type_ref_index_at(idx);
1271 int signatureIdx = cp->signature_ref_index_at(nameAndTypeIdx);
1272 symbolOop signature = cp->symbol_at(signatureIdx);
1273 os->print("%s", signature->as_C_string());
1274 }
1275 os->cr();
1276 os->print(" stack = ");
1277 print_states(os, stack(), _stack_top);
1278 os->cr();
1279 if (_monitor_top != bad_monitors) {
1280 os->print(" monitors = ");
1281 print_states(os, monitors(), _monitor_top);
1282 } else {
1283 os->print(" [bad monitor stack]");
1284 }
1285 os->cr();
1286 } else {
1287 os->print(" %4d vars = '%s' ", currentBC->bci(), state_vec_to_string(vars(), _max_locals));
1288 os->print(" stack = '%s' ", state_vec_to_string(stack(), _stack_top));
1289 if (_monitor_top != bad_monitors) {
1290 os->print(" monitors = '%s' \t%s", state_vec_to_string(monitors(), _monitor_top), Bytecodes::name(currentBC->code()));
1291 } else {
1292 os->print(" [bad monitor stack]");
1293 }
1294 switch(currentBC->code()) {
1295 case Bytecodes::_invokevirtual:
1296 case Bytecodes::_invokespecial:
1297 case Bytecodes::_invokestatic:
1298 case Bytecodes::_invokeinterface:
1299 int idx = currentBC->get_index_big();
1300 constantPoolOop cp = method()->constants();
1301 int nameAndTypeIdx = cp->name_and_type_ref_index_at(idx);
1302 int signatureIdx = cp->signature_ref_index_at(nameAndTypeIdx);
1303 symbolOop signature = cp->symbol_at(signatureIdx);
1304 os->print("%s", signature->as_C_string());
1305 }
1306 os->cr();
1307 }
1308 }
1310 // Sets the current state to be the state after executing the
1311 // current instruction, starting in the current state.
1312 void GenerateOopMap::interp1(BytecodeStream *itr) {
1313 if (TraceNewOopMapGeneration) {
1314 print_current_state(tty, itr, TraceNewOopMapGenerationDetailed);
1315 }
1317 // Should we report the results? Result is reported *before* the instruction at the current bci is executed.
1318 // However, not for calls. For calls we do not want to include the arguments, so we postpone the reporting until
1319 // they have been popped (in method ppl).
1320 if (_report_result == true) {
1321 switch(itr->code()) {
1322 case Bytecodes::_invokevirtual:
1323 case Bytecodes::_invokespecial:
1324 case Bytecodes::_invokestatic:
1325 case Bytecodes::_invokeinterface:
1326 _itr_send = itr;
1327 _report_result_for_send = true;
1328 break;
1329 default:
1330 fill_stackmap_for_opcodes(itr, vars(), stack(), _stack_top);
1331 break;
1332 }
1333 }
1335 // abstract interpretation of current opcode
1336 switch(itr->code()) {
1337 case Bytecodes::_nop: break;
1338 case Bytecodes::_goto: break;
1339 case Bytecodes::_goto_w: break;
1340 case Bytecodes::_iinc: break;
1341 case Bytecodes::_return: do_return_monitor_check();
1342 break;
1344 case Bytecodes::_aconst_null:
1345 case Bytecodes::_new: ppush1(CellTypeState::make_line_ref(itr->bci()));
1346 break;
1348 case Bytecodes::_iconst_m1:
1349 case Bytecodes::_iconst_0:
1350 case Bytecodes::_iconst_1:
1351 case Bytecodes::_iconst_2:
1352 case Bytecodes::_iconst_3:
1353 case Bytecodes::_iconst_4:
1354 case Bytecodes::_iconst_5:
1355 case Bytecodes::_fconst_0:
1356 case Bytecodes::_fconst_1:
1357 case Bytecodes::_fconst_2:
1358 case Bytecodes::_bipush:
1359 case Bytecodes::_sipush: ppush1(valCTS); break;
1361 case Bytecodes::_lconst_0:
1362 case Bytecodes::_lconst_1:
1363 case Bytecodes::_dconst_0:
1364 case Bytecodes::_dconst_1: ppush(vvCTS); break;
1366 case Bytecodes::_ldc2_w: ppush(vvCTS); break;
1368 case Bytecodes::_ldc: do_ldc(itr->get_index(), itr->bci()); break;
1369 case Bytecodes::_ldc_w: do_ldc(itr->get_index_big(), itr->bci());break;
1371 case Bytecodes::_iload:
1372 case Bytecodes::_fload: ppload(vCTS, itr->get_index()); break;
1374 case Bytecodes::_lload:
1375 case Bytecodes::_dload: ppload(vvCTS,itr->get_index()); break;
1377 case Bytecodes::_aload: ppload(rCTS, itr->get_index()); break;
1379 case Bytecodes::_iload_0:
1380 case Bytecodes::_fload_0: ppload(vCTS, 0); break;
1381 case Bytecodes::_iload_1:
1382 case Bytecodes::_fload_1: ppload(vCTS, 1); break;
1383 case Bytecodes::_iload_2:
1384 case Bytecodes::_fload_2: ppload(vCTS, 2); break;
1385 case Bytecodes::_iload_3:
1386 case Bytecodes::_fload_3: ppload(vCTS, 3); break;
1388 case Bytecodes::_lload_0:
1389 case Bytecodes::_dload_0: ppload(vvCTS, 0); break;
1390 case Bytecodes::_lload_1:
1391 case Bytecodes::_dload_1: ppload(vvCTS, 1); break;
1392 case Bytecodes::_lload_2:
1393 case Bytecodes::_dload_2: ppload(vvCTS, 2); break;
1394 case Bytecodes::_lload_3:
1395 case Bytecodes::_dload_3: ppload(vvCTS, 3); break;
1397 case Bytecodes::_aload_0: ppload(rCTS, 0); break;
1398 case Bytecodes::_aload_1: ppload(rCTS, 1); break;
1399 case Bytecodes::_aload_2: ppload(rCTS, 2); break;
1400 case Bytecodes::_aload_3: ppload(rCTS, 3); break;
1402 case Bytecodes::_iaload:
1403 case Bytecodes::_faload:
1404 case Bytecodes::_baload:
1405 case Bytecodes::_caload:
1406 case Bytecodes::_saload: pp(vrCTS, vCTS); break;
1408 case Bytecodes::_laload: pp(vrCTS, vvCTS); break;
1409 case Bytecodes::_daload: pp(vrCTS, vvCTS); break;
1411 case Bytecodes::_aaload: pp_new_ref(vrCTS, itr->bci()); break;
1413 case Bytecodes::_istore:
1414 case Bytecodes::_fstore: ppstore(vCTS, itr->get_index()); break;
1416 case Bytecodes::_lstore:
1417 case Bytecodes::_dstore: ppstore(vvCTS, itr->get_index()); break;
1419 case Bytecodes::_astore: do_astore(itr->get_index()); break;
1421 case Bytecodes::_istore_0:
1422 case Bytecodes::_fstore_0: ppstore(vCTS, 0); break;
1423 case Bytecodes::_istore_1:
1424 case Bytecodes::_fstore_1: ppstore(vCTS, 1); break;
1425 case Bytecodes::_istore_2:
1426 case Bytecodes::_fstore_2: ppstore(vCTS, 2); break;
1427 case Bytecodes::_istore_3:
1428 case Bytecodes::_fstore_3: ppstore(vCTS, 3); break;
1430 case Bytecodes::_lstore_0:
1431 case Bytecodes::_dstore_0: ppstore(vvCTS, 0); break;
1432 case Bytecodes::_lstore_1:
1433 case Bytecodes::_dstore_1: ppstore(vvCTS, 1); break;
1434 case Bytecodes::_lstore_2:
1435 case Bytecodes::_dstore_2: ppstore(vvCTS, 2); break;
1436 case Bytecodes::_lstore_3:
1437 case Bytecodes::_dstore_3: ppstore(vvCTS, 3); break;
1439 case Bytecodes::_astore_0: do_astore(0); break;
1440 case Bytecodes::_astore_1: do_astore(1); break;
1441 case Bytecodes::_astore_2: do_astore(2); break;
1442 case Bytecodes::_astore_3: do_astore(3); break;
1444 case Bytecodes::_iastore:
1445 case Bytecodes::_fastore:
1446 case Bytecodes::_bastore:
1447 case Bytecodes::_castore:
1448 case Bytecodes::_sastore: ppop(vvrCTS); break;
1449 case Bytecodes::_lastore:
1450 case Bytecodes::_dastore: ppop(vvvrCTS); break;
1451 case Bytecodes::_aastore: ppop(rvrCTS); break;
1453 case Bytecodes::_pop: ppop_any(1); break;
1454 case Bytecodes::_pop2: ppop_any(2); break;
1456 case Bytecodes::_dup: ppdupswap(1, "11"); break;
1457 case Bytecodes::_dup_x1: ppdupswap(2, "121"); break;
1458 case Bytecodes::_dup_x2: ppdupswap(3, "1321"); break;
1459 case Bytecodes::_dup2: ppdupswap(2, "2121"); break;
1460 case Bytecodes::_dup2_x1: ppdupswap(3, "21321"); break;
1461 case Bytecodes::_dup2_x2: ppdupswap(4, "214321"); break;
1462 case Bytecodes::_swap: ppdupswap(2, "12"); break;
1464 case Bytecodes::_iadd:
1465 case Bytecodes::_fadd:
1466 case Bytecodes::_isub:
1467 case Bytecodes::_fsub:
1468 case Bytecodes::_imul:
1469 case Bytecodes::_fmul:
1470 case Bytecodes::_idiv:
1471 case Bytecodes::_fdiv:
1472 case Bytecodes::_irem:
1473 case Bytecodes::_frem:
1474 case Bytecodes::_ishl:
1475 case Bytecodes::_ishr:
1476 case Bytecodes::_iushr:
1477 case Bytecodes::_iand:
1478 case Bytecodes::_ior:
1479 case Bytecodes::_ixor:
1480 case Bytecodes::_l2f:
1481 case Bytecodes::_l2i:
1482 case Bytecodes::_d2f:
1483 case Bytecodes::_d2i:
1484 case Bytecodes::_fcmpl:
1485 case Bytecodes::_fcmpg: pp(vvCTS, vCTS); break;
1487 case Bytecodes::_ladd:
1488 case Bytecodes::_dadd:
1489 case Bytecodes::_lsub:
1490 case Bytecodes::_dsub:
1491 case Bytecodes::_lmul:
1492 case Bytecodes::_dmul:
1493 case Bytecodes::_ldiv:
1494 case Bytecodes::_ddiv:
1495 case Bytecodes::_lrem:
1496 case Bytecodes::_drem:
1497 case Bytecodes::_land:
1498 case Bytecodes::_lor:
1499 case Bytecodes::_lxor: pp(vvvvCTS, vvCTS); break;
1501 case Bytecodes::_ineg:
1502 case Bytecodes::_fneg:
1503 case Bytecodes::_i2f:
1504 case Bytecodes::_f2i:
1505 case Bytecodes::_i2c:
1506 case Bytecodes::_i2s:
1507 case Bytecodes::_i2b: pp(vCTS, vCTS); break;
1509 case Bytecodes::_lneg:
1510 case Bytecodes::_dneg:
1511 case Bytecodes::_l2d:
1512 case Bytecodes::_d2l: pp(vvCTS, vvCTS); break;
1514 case Bytecodes::_lshl:
1515 case Bytecodes::_lshr:
1516 case Bytecodes::_lushr: pp(vvvCTS, vvCTS); break;
1518 case Bytecodes::_i2l:
1519 case Bytecodes::_i2d:
1520 case Bytecodes::_f2l:
1521 case Bytecodes::_f2d: pp(vCTS, vvCTS); break;
1523 case Bytecodes::_lcmp: pp(vvvvCTS, vCTS); break;
1524 case Bytecodes::_dcmpl:
1525 case Bytecodes::_dcmpg: pp(vvvvCTS, vCTS); break;
1527 case Bytecodes::_ifeq:
1528 case Bytecodes::_ifne:
1529 case Bytecodes::_iflt:
1530 case Bytecodes::_ifge:
1531 case Bytecodes::_ifgt:
1532 case Bytecodes::_ifle:
1533 case Bytecodes::_tableswitch: ppop1(valCTS);
1534 break;
1535 case Bytecodes::_ireturn:
1536 case Bytecodes::_freturn: do_return_monitor_check();
1537 ppop1(valCTS);
1538 break;
1539 case Bytecodes::_if_icmpeq:
1540 case Bytecodes::_if_icmpne:
1541 case Bytecodes::_if_icmplt:
1542 case Bytecodes::_if_icmpge:
1543 case Bytecodes::_if_icmpgt:
1544 case Bytecodes::_if_icmple: ppop(vvCTS);
1545 break;
1547 case Bytecodes::_lreturn: do_return_monitor_check();
1548 ppop(vvCTS);
1549 break;
1551 case Bytecodes::_dreturn: do_return_monitor_check();
1552 ppop(vvCTS);
1553 break;
1555 case Bytecodes::_if_acmpeq:
1556 case Bytecodes::_if_acmpne: ppop(rrCTS); break;
1558 case Bytecodes::_jsr: do_jsr(itr->dest()); break;
1559 case Bytecodes::_jsr_w: do_jsr(itr->dest_w()); break;
1561 case Bytecodes::_getstatic: do_field(true, true,
1562 itr->get_index_big(),
1563 itr->bci()); break;
1564 case Bytecodes::_putstatic: do_field(false, true, itr->get_index_big(), itr->bci()); break;
1565 case Bytecodes::_getfield: do_field(true, false, itr->get_index_big(), itr->bci()); break;
1566 case Bytecodes::_putfield: do_field(false, false, itr->get_index_big(), itr->bci()); break;
1568 case Bytecodes::_invokevirtual:
1569 case Bytecodes::_invokespecial: do_method(false, false, itr->get_index_big(), itr->bci()); break;
1570 case Bytecodes::_invokestatic: do_method(true, false, itr->get_index_big(), itr->bci()); break;
1571 case Bytecodes::_invokeinterface: do_method(false, true, itr->get_index_big(), itr->bci()); break;
1572 case Bytecodes::_newarray:
1573 case Bytecodes::_anewarray: pp_new_ref(vCTS, itr->bci()); break;
1574 case Bytecodes::_checkcast: do_checkcast(); break;
1575 case Bytecodes::_arraylength:
1576 case Bytecodes::_instanceof: pp(rCTS, vCTS); break;
1577 case Bytecodes::_monitorenter: do_monitorenter(itr->bci()); break;
1578 case Bytecodes::_monitorexit: do_monitorexit(itr->bci()); break;
1580 case Bytecodes::_athrow: // handled by do_exception_edge() BUT ...
1581 // vlh(apple): do_exception_edge() does not get
1582 // called if method has no exception handlers
1583 if ((!_has_exceptions) && (_monitor_top > 0)) {
1584 _monitor_safe = false;
1585 }
1586 break;
1588 case Bytecodes::_areturn: do_return_monitor_check();
1589 ppop1(refCTS);
1590 break;
1591 case Bytecodes::_ifnull:
1592 case Bytecodes::_ifnonnull: ppop1(refCTS); break;
1593 case Bytecodes::_multianewarray: do_multianewarray(*(itr->bcp()+3), itr->bci()); break;
1595 case Bytecodes::_wide: fatal("Iterator should skip this bytecode"); break;
1596 case Bytecodes::_ret: break;
1598 // Java opcodes
1599 case Bytecodes::_lookupswitch: ppop1(valCTS); break;
1601 default:
1602 tty->print("unexpected opcode: %d\n", itr->code());
1603 ShouldNotReachHere();
1604 break;
1605 }
1606 }
1608 void GenerateOopMap::check_type(CellTypeState expected, CellTypeState actual) {
1609 if (!expected.equal_kind(actual)) {
1610 verify_error("wrong type on stack (found: %c expected: %c)", actual.to_char(), expected.to_char());
1611 }
1612 }
1614 void GenerateOopMap::ppstore(CellTypeState *in, int loc_no) {
1615 while(!(*in).is_bottom()) {
1616 CellTypeState expected =*in++;
1617 CellTypeState actual = pop();
1618 check_type(expected, actual);
1619 assert(loc_no >= 0, "sanity check");
1620 set_var(loc_no++, actual);
1621 }
1622 }
1624 void GenerateOopMap::ppload(CellTypeState *out, int loc_no) {
1625 while(!(*out).is_bottom()) {
1626 CellTypeState out1 = *out++;
1627 CellTypeState vcts = get_var(loc_no);
1628 assert(out1.can_be_reference() || out1.can_be_value(),
1629 "can only load refs. and values.");
1630 if (out1.is_reference()) {
1631 assert(loc_no>=0, "sanity check");
1632 if (!vcts.is_reference()) {
1633 // We were asked to push a reference, but the type of the
1634 // variable can be something else
1635 _conflict = true;
1636 if (vcts.can_be_uninit()) {
1637 // It is a ref-uninit conflict (at least). If there are other
1638 // problems, we'll get them in the next round
1639 add_to_ref_init_set(loc_no);
1640 vcts = out1;
1641 } else {
1642 // It wasn't a ref-uninit conflict. So must be a
1643 // ref-val or ref-pc conflict. Split the variable.
1644 record_refval_conflict(loc_no);
1645 vcts = out1;
1646 }
1647 push(out1); // recover...
1648 } else {
1649 push(vcts); // preserve reference.
1650 }
1651 // Otherwise it is a conflict, but one that verification would
1652 // have caught if illegal. In particular, it can't be a topCTS
1653 // resulting from mergeing two difference pcCTS's since the verifier
1654 // would have rejected any use of such a merge.
1655 } else {
1656 push(out1); // handle val/init conflict
1657 }
1658 loc_no++;
1659 }
1660 }
1662 void GenerateOopMap::ppdupswap(int poplen, const char *out) {
1663 CellTypeState actual[5];
1664 assert(poplen < 5, "this must be less than length of actual vector");
1666 // pop all arguments
1667 for(int i = 0; i < poplen; i++) actual[i] = pop();
1669 // put them back
1670 char push_ch = *out++;
1671 while (push_ch != '\0') {
1672 int idx = push_ch - '1';
1673 assert(idx >= 0 && idx < poplen, "wrong arguments");
1674 push(actual[idx]);
1675 push_ch = *out++;
1676 }
1677 }
1679 void GenerateOopMap::ppop1(CellTypeState out) {
1680 CellTypeState actual = pop();
1681 check_type(out, actual);
1682 }
1684 void GenerateOopMap::ppop(CellTypeState *out) {
1685 while (!(*out).is_bottom()) {
1686 ppop1(*out++);
1687 }
1688 }
1690 void GenerateOopMap::ppush1(CellTypeState in) {
1691 assert(in.is_reference() | in.is_value(), "sanity check");
1692 push(in);
1693 }
1695 void GenerateOopMap::ppush(CellTypeState *in) {
1696 while (!(*in).is_bottom()) {
1697 ppush1(*in++);
1698 }
1699 }
1701 void GenerateOopMap::pp(CellTypeState *in, CellTypeState *out) {
1702 ppop(in);
1703 ppush(out);
1704 }
1706 void GenerateOopMap::pp_new_ref(CellTypeState *in, int bci) {
1707 ppop(in);
1708 ppush1(CellTypeState::make_line_ref(bci));
1709 }
1711 void GenerateOopMap::ppop_any(int poplen) {
1712 if (_stack_top >= poplen) {
1713 _stack_top -= poplen;
1714 } else {
1715 verify_error("stack underflow");
1716 }
1717 }
1719 // Replace all occurences of the state 'match' with the state 'replace'
1720 // in our current state vector.
1721 void GenerateOopMap::replace_all_CTS_matches(CellTypeState match,
1722 CellTypeState replace) {
1723 int i;
1724 int len = _max_locals + _stack_top;
1725 bool change = false;
1727 for (i = len - 1; i >= 0; i--) {
1728 if (match.equal(_state[i])) {
1729 _state[i] = replace;
1730 }
1731 }
1733 if (_monitor_top > 0) {
1734 int base = _max_locals + _max_stack;
1735 len = base + _monitor_top;
1736 for (i = len - 1; i >= base; i--) {
1737 if (match.equal(_state[i])) {
1738 _state[i] = replace;
1739 }
1740 }
1741 }
1742 }
1744 void GenerateOopMap::do_checkcast() {
1745 CellTypeState actual = pop();
1746 check_type(refCTS, actual);
1747 push(actual);
1748 }
1750 void GenerateOopMap::do_monitorenter(int bci) {
1751 CellTypeState actual = pop();
1752 if (_monitor_top == bad_monitors) {
1753 return;
1754 }
1756 // Bail out when we get repeated locks on an identical monitor. This case
1757 // isn't too hard to handle and can be made to work if supporting nested
1758 // redundant synchronized statements becomes a priority.
1759 //
1760 // See also "Note" in do_monitorexit(), below.
1761 if (actual.is_lock_reference()) {
1762 _monitor_top = bad_monitors;
1763 _monitor_safe = false;
1765 if (TraceMonitorMismatch) {
1766 report_monitor_mismatch("nested redundant lock -- bailout...");
1767 }
1768 return;
1769 }
1771 CellTypeState lock = CellTypeState::make_lock_ref(bci);
1772 check_type(refCTS, actual);
1773 if (!actual.is_info_top()) {
1774 replace_all_CTS_matches(actual, lock);
1775 monitor_push(lock);
1776 }
1777 }
1779 void GenerateOopMap::do_monitorexit(int bci) {
1780 CellTypeState actual = pop();
1781 if (_monitor_top == bad_monitors) {
1782 return;
1783 }
1784 check_type(refCTS, actual);
1785 CellTypeState expected = monitor_pop();
1786 if (!actual.is_lock_reference() || !expected.equal(actual)) {
1787 // The monitor we are exiting is not verifiably the one
1788 // on the top of our monitor stack. This causes a monitor
1789 // mismatch.
1790 _monitor_top = bad_monitors;
1791 _monitor_safe = false;
1793 // We need to mark this basic block as changed so that
1794 // this monitorexit will be visited again. We need to
1795 // do this to ensure that we have accounted for the
1796 // possibility that this bytecode will throw an
1797 // exception.
1798 BasicBlock* bb = get_basic_block_containing(bci);
1799 bb->set_changed(true);
1800 bb->_monitor_top = bad_monitors;
1802 if (TraceMonitorMismatch) {
1803 report_monitor_mismatch("improper monitor pair");
1804 }
1805 } else {
1806 // This code is a fix for the case where we have repeated
1807 // locking of the same object in straightline code. We clear
1808 // out the lock when it is popped from the monitor stack
1809 // and replace it with an unobtrusive reference value that can
1810 // be locked again.
1811 //
1812 // Note: when generateOopMap is fixed to properly handle repeated,
1813 // nested, redundant locks on the same object, then this
1814 // fix will need to be removed at that time.
1815 replace_all_CTS_matches(actual, CellTypeState::make_line_ref(bci));
1816 }
1817 }
1819 void GenerateOopMap::do_return_monitor_check() {
1820 if (_monitor_top > 0) {
1821 // The monitor stack must be empty when we leave the method
1822 // for the monitors to be properly matched.
1823 _monitor_safe = false;
1825 // Since there are no successors to the *return bytecode, it
1826 // isn't necessary to set _monitor_top to bad_monitors.
1828 if (TraceMonitorMismatch) {
1829 report_monitor_mismatch("non-empty monitor stack at return");
1830 }
1831 }
1832 }
1834 void GenerateOopMap::do_jsr(int targ_bci) {
1835 push(CellTypeState::make_addr(targ_bci));
1836 }
1840 void GenerateOopMap::do_ldc(int idx, int bci) {
1841 constantPoolOop cp = method()->constants();
1842 constantTag tag = cp->tag_at(idx);
1844 CellTypeState cts = (tag.is_string() || tag.is_unresolved_string() ||
1845 tag.is_klass() || tag.is_unresolved_klass())
1846 ? CellTypeState::make_line_ref(bci) : valCTS;
1847 ppush1(cts);
1848 }
1850 void GenerateOopMap::do_multianewarray(int dims, int bci) {
1851 assert(dims >= 1, "sanity check");
1852 for(int i = dims -1; i >=0; i--) {
1853 ppop1(valCTS);
1854 }
1855 ppush1(CellTypeState::make_line_ref(bci));
1856 }
1858 void GenerateOopMap::do_astore(int idx) {
1859 CellTypeState r_or_p = pop();
1860 if (!r_or_p.is_address() && !r_or_p.is_reference()) {
1861 // We actually expected ref or pc, but we only report that we expected a ref. It does not
1862 // really matter (at least for now)
1863 verify_error("wrong type on stack (found: %c, expected: {pr})", r_or_p.to_char());
1864 return;
1865 }
1866 set_var(idx, r_or_p);
1867 }
1869 // Copies bottom/zero terminated CTS string from "src" into "dst".
1870 // Does NOT terminate with a bottom. Returns the number of cells copied.
1871 int GenerateOopMap::copy_cts(CellTypeState *dst, CellTypeState *src) {
1872 int idx = 0;
1873 while (!src[idx].is_bottom()) {
1874 dst[idx] = src[idx];
1875 idx++;
1876 }
1877 return idx;
1878 }
1880 void GenerateOopMap::do_field(int is_get, int is_static, int idx, int bci) {
1881 // Dig up signature for field in constant pool
1882 constantPoolOop cp = method()->constants();
1883 int nameAndTypeIdx = cp->name_and_type_ref_index_at(idx);
1884 int signatureIdx = cp->signature_ref_index_at(nameAndTypeIdx);
1885 symbolOop signature = cp->symbol_at(signatureIdx);
1887 // Parse signature (espcially simple for fields)
1888 assert(signature->utf8_length() > 0, "field signatures cannot have zero length");
1889 // The signature is UFT8 encoded, but the first char is always ASCII for signatures.
1890 char sigch = (char)*(signature->base());
1891 CellTypeState temp[4];
1892 CellTypeState *eff = sigchar_to_effect(sigch, bci, temp);
1894 CellTypeState in[4];
1895 CellTypeState *out;
1896 int i = 0;
1898 if (is_get) {
1899 out = eff;
1900 } else {
1901 out = epsilonCTS;
1902 i = copy_cts(in, eff);
1903 }
1904 if (!is_static) in[i++] = CellTypeState::ref;
1905 in[i] = CellTypeState::bottom;
1906 assert(i<=3, "sanity check");
1907 pp(in, out);
1908 }
1910 void GenerateOopMap::do_method(int is_static, int is_interface, int idx, int bci) {
1911 // Dig up signature for field in constant pool
1912 constantPoolOop cp = _method->constants();
1913 int nameAndTypeIdx = cp->name_and_type_ref_index_at(idx);
1914 int signatureIdx = cp->signature_ref_index_at(nameAndTypeIdx);
1915 symbolOop signature = cp->symbol_at(signatureIdx);
1917 // Parse method signature
1918 CellTypeState out[4];
1919 CellTypeState in[MAXARGSIZE+1]; // Includes result
1920 ComputeCallStack cse(signature);
1922 // Compute return type
1923 int res_length= cse.compute_for_returntype(out);
1925 // Temporary hack.
1926 if (out[0].equal(CellTypeState::ref) && out[1].equal(CellTypeState::bottom)) {
1927 out[0] = CellTypeState::make_line_ref(bci);
1928 }
1930 assert(res_length<=4, "max value should be vv");
1932 // Compute arguments
1933 int arg_length = cse.compute_for_parameters(is_static != 0, in);
1934 assert(arg_length<=MAXARGSIZE, "too many locals");
1936 // Pop arguments
1937 for (int i = arg_length - 1; i >= 0; i--) ppop1(in[i]);// Do args in reverse order.
1939 // Report results
1940 if (_report_result_for_send == true) {
1941 fill_stackmap_for_opcodes(_itr_send, vars(), stack(), _stack_top);
1942 _report_result_for_send = false;
1943 }
1945 // Push return address
1946 ppush(out);
1947 }
1949 // This is used to parse the signature for fields, since they are very simple...
1950 CellTypeState *GenerateOopMap::sigchar_to_effect(char sigch, int bci, CellTypeState *out) {
1951 // Object and array
1952 if (sigch=='L' || sigch=='[') {
1953 out[0] = CellTypeState::make_line_ref(bci);
1954 out[1] = CellTypeState::bottom;
1955 return out;
1956 }
1957 if (sigch == 'J' || sigch == 'D' ) return vvCTS; // Long and Double
1958 if (sigch == 'V' ) return epsilonCTS; // Void
1959 return vCTS; // Otherwise
1960 }
1962 long GenerateOopMap::_total_byte_count = 0;
1963 elapsedTimer GenerateOopMap::_total_oopmap_time;
1965 // This function assumes "bcs" is at a "ret" instruction and that the vars
1966 // state is valid for that instruction. Furthermore, the ret instruction
1967 // must be the last instruction in "bb" (we store information about the
1968 // "ret" in "bb").
1969 void GenerateOopMap::ret_jump_targets_do(BytecodeStream *bcs, jmpFct_t jmpFct, int varNo, int *data) {
1970 CellTypeState ra = vars()[varNo];
1971 if (!ra.is_good_address()) {
1972 verify_error("ret returns from two jsr subroutines?");
1973 return;
1974 }
1975 int target = ra.get_info();
1977 RetTableEntry* rtEnt = _rt.find_jsrs_for_target(target);
1978 int bci = bcs->bci();
1979 for (int i = 0; i < rtEnt->nof_jsrs(); i++) {
1980 int target_bci = rtEnt->jsrs(i);
1981 // Make sure a jrtRet does not set the changed bit for dead basicblock.
1982 BasicBlock* jsr_bb = get_basic_block_containing(target_bci - 1);
1983 debug_only(BasicBlock* target_bb = &jsr_bb[1];)
1984 assert(target_bb == get_basic_block_at(target_bci), "wrong calc. of successor basicblock");
1985 bool alive = jsr_bb->is_alive();
1986 if (TraceNewOopMapGeneration) {
1987 tty->print("pc = %d, ret -> %d alive: %s\n", bci, target_bci, alive ? "true" : "false");
1988 }
1989 if (alive) jmpFct(this, target_bci, data);
1990 }
1991 }
1993 //
1994 // Debug method
1995 //
1996 char* GenerateOopMap::state_vec_to_string(CellTypeState* vec, int len) {
1997 #ifdef ASSERT
1998 int checklen = MAX3(_max_locals, _max_stack, _max_monitors) + 1;
1999 assert(len < checklen, "state_vec_buf overflow");
2000 #endif
2001 for (int i = 0; i < len; i++) _state_vec_buf[i] = vec[i].to_char();
2002 _state_vec_buf[len] = 0;
2003 return _state_vec_buf;
2004 }
2006 void GenerateOopMap::print_time() {
2007 tty->print_cr ("Accumulated oopmap times:");
2008 tty->print_cr ("---------------------------");
2009 tty->print_cr (" Total : %3.3f sec.", GenerateOopMap::_total_oopmap_time.seconds());
2010 tty->print_cr (" (%3.0f bytecodes per sec) ",
2011 GenerateOopMap::_total_byte_count / GenerateOopMap::_total_oopmap_time.seconds());
2012 }
2014 //
2015 // ============ Main Entry Point ===========
2016 //
2017 GenerateOopMap::GenerateOopMap(methodHandle method) {
2018 // We have to initialize all variables here, that can be queried direcly
2019 _method = method;
2020 _max_locals=0;
2021 _init_vars = NULL;
2023 #ifndef PRODUCT
2024 // If we are doing a detailed trace, include the regular trace information.
2025 if (TraceNewOopMapGenerationDetailed) {
2026 TraceNewOopMapGeneration = true;
2027 }
2028 #endif
2029 }
2031 void GenerateOopMap::compute_map(TRAPS) {
2032 #ifndef PRODUCT
2033 if (TimeOopMap2) {
2034 method()->print_short_name(tty);
2035 tty->print(" ");
2036 }
2037 if (TimeOopMap) {
2038 _total_byte_count += method()->code_size();
2039 }
2040 #endif
2041 TraceTime t_single("oopmap time", TimeOopMap2);
2042 TraceTime t_all(NULL, &_total_oopmap_time, TimeOopMap);
2044 // Initialize values
2045 _got_error = false;
2046 _conflict = false;
2047 _max_locals = method()->max_locals();
2048 _max_stack = method()->max_stack();
2049 _has_exceptions = (method()->exception_table()->length() > 0);
2050 _nof_refval_conflicts = 0;
2051 _init_vars = new GrowableArray<intptr_t>(5); // There are seldom more than 5 init_vars
2052 _report_result = false;
2053 _report_result_for_send = false;
2054 _new_var_map = NULL;
2055 _ret_adr_tos = new GrowableArray<intptr_t>(5); // 5 seems like a good number;
2056 _did_rewriting = false;
2057 _did_relocation = false;
2059 if (TraceNewOopMapGeneration) {
2060 tty->print("Method name: %s\n", method()->name()->as_C_string());
2061 if (Verbose) {
2062 _method->print_codes();
2063 tty->print_cr("Exception table:");
2064 typeArrayOop excps = method()->exception_table();
2065 for(int i = 0; i < excps->length(); i += 4) {
2066 tty->print_cr("[%d - %d] -> %d", excps->int_at(i + 0), excps->int_at(i + 1), excps->int_at(i + 2));
2067 }
2068 }
2069 }
2071 // if no code - do nothing
2072 // compiler needs info
2073 if (method()->code_size() == 0 || _max_locals + method()->max_stack() == 0) {
2074 fill_stackmap_prolog(0);
2075 fill_stackmap_epilog();
2076 return;
2077 }
2078 // Step 1: Compute all jump targets and their return value
2079 if (!_got_error)
2080 _rt.compute_ret_table(_method);
2082 // Step 2: Find all basic blocks and count GC points
2083 if (!_got_error)
2084 mark_bbheaders_and_count_gc_points();
2086 // Step 3: Calculate stack maps
2087 if (!_got_error)
2088 do_interpretation();
2090 // Step 4:Return results
2091 if (!_got_error && report_results())
2092 report_result();
2094 if (_got_error) {
2095 THROW_HANDLE(_exception);
2096 }
2097 }
2099 // Error handling methods
2100 // These methods create an exception for the current thread which is thrown
2101 // at the bottom of the call stack, when it returns to compute_map(). The
2102 // _got_error flag controls execution. NOT TODO: The VM exception propagation
2103 // mechanism using TRAPS/CHECKs could be used here instead but it would need
2104 // to be added as a parameter to every function and checked for every call.
2105 // The tons of extra code it would generate didn't seem worth the change.
2106 //
2107 void GenerateOopMap::error_work(const char *format, va_list ap) {
2108 _got_error = true;
2109 char msg_buffer[512];
2110 vsnprintf(msg_buffer, sizeof(msg_buffer), format, ap);
2111 // Append method name
2112 char msg_buffer2[512];
2113 jio_snprintf(msg_buffer2, sizeof(msg_buffer2), "%s in method %s", msg_buffer, method()->name()->as_C_string());
2114 _exception = Exceptions::new_exception(Thread::current(),
2115 vmSymbols::java_lang_LinkageError(), msg_buffer2);
2116 }
2118 void GenerateOopMap::report_error(const char *format, ...) {
2119 va_list ap;
2120 va_start(ap, format);
2121 error_work(format, ap);
2122 }
2124 void GenerateOopMap::verify_error(const char *format, ...) {
2125 // We do not distinguish between different types of errors for verification
2126 // errors. Let the verifier give a better message.
2127 const char *msg = "Illegal class file encountered. Try running with -Xverify:all";
2128 error_work(msg, NULL);
2129 }
2131 //
2132 // Report result opcodes
2133 //
2134 void GenerateOopMap::report_result() {
2136 if (TraceNewOopMapGeneration) tty->print_cr("Report result pass");
2138 // We now want to report the result of the parse
2139 _report_result = true;
2141 // Prolog code
2142 fill_stackmap_prolog(_gc_points);
2144 // Mark everything changed, then do one interpretation pass.
2145 for (int i = 0; i<_bb_count; i++) {
2146 if (_basic_blocks[i].is_reachable()) {
2147 _basic_blocks[i].set_changed(true);
2148 interp_bb(&_basic_blocks[i]);
2149 }
2150 }
2152 // Note: Since we are skipping dead-code when we are reporting results, then
2153 // the no. of encountered gc-points might be fewer than the previously number
2154 // we have counted. (dead-code is a pain - it should be removed before we get here)
2155 fill_stackmap_epilog();
2157 // Report initvars
2158 fill_init_vars(_init_vars);
2160 _report_result = false;
2161 }
2163 void GenerateOopMap::result_for_basicblock(int bci) {
2164 if (TraceNewOopMapGeneration) tty->print_cr("Report result pass for basicblock");
2166 // We now want to report the result of the parse
2167 _report_result = true;
2169 // Find basicblock and report results
2170 BasicBlock* bb = get_basic_block_containing(bci);
2171 assert(bb->is_reachable(), "getting result from unreachable basicblock");
2172 bb->set_changed(true);
2173 interp_bb(bb);
2174 }
2176 //
2177 // Conflict handling code
2178 //
2180 void GenerateOopMap::record_refval_conflict(int varNo) {
2181 assert(varNo>=0 && varNo< _max_locals, "index out of range");
2183 if (TraceOopMapRewrites) {
2184 tty->print("### Conflict detected (local no: %d)\n", varNo);
2185 }
2187 if (!_new_var_map) {
2188 _new_var_map = NEW_RESOURCE_ARRAY(int, _max_locals);
2189 for (int k = 0; k < _max_locals; k++) _new_var_map[k] = k;
2190 }
2192 if ( _new_var_map[varNo] == varNo) {
2193 // Check if max. number of locals has been reached
2194 if (_max_locals + _nof_refval_conflicts >= MAX_LOCAL_VARS) {
2195 report_error("Rewriting exceeded local variable limit");
2196 return;
2197 }
2198 _new_var_map[varNo] = _max_locals + _nof_refval_conflicts;
2199 _nof_refval_conflicts++;
2200 }
2201 }
2203 void GenerateOopMap::rewrite_refval_conflicts()
2204 {
2205 // We can get here two ways: Either a rewrite conflict was detected, or
2206 // an uninitialize reference was detected. In the second case, we do not
2207 // do any rewriting, we just want to recompute the reference set with the
2208 // new information
2210 int nof_conflicts = 0; // Used for debugging only
2212 if ( _nof_refval_conflicts == 0 )
2213 return;
2215 // Check if rewrites are allowed in this parse.
2216 if (!allow_rewrites() && !IgnoreRewrites) {
2217 fatal("Rewriting method not allowed at this stage");
2218 }
2221 // This following flag is to tempoary supress rewrites. The locals that might conflict will
2222 // all be set to contain values. This is UNSAFE - however, until the rewriting has been completely
2223 // tested it is nice to have.
2224 if (IgnoreRewrites) {
2225 if (Verbose) {
2226 tty->print("rewrites suppressed for local no. ");
2227 for (int l = 0; l < _max_locals; l++) {
2228 if (_new_var_map[l] != l) {
2229 tty->print("%d ", l);
2230 vars()[l] = CellTypeState::value;
2231 }
2232 }
2233 tty->cr();
2234 }
2236 // That was that...
2237 _new_var_map = NULL;
2238 _nof_refval_conflicts = 0;
2239 _conflict = false;
2241 return;
2242 }
2244 // Tracing flag
2245 _did_rewriting = true;
2247 if (TraceOopMapRewrites) {
2248 tty->print_cr("ref/value conflict for method %s - bytecodes are getting rewritten", method()->name()->as_C_string());
2249 method()->print();
2250 method()->print_codes();
2251 }
2253 assert(_new_var_map!=NULL, "nothing to rewrite");
2254 assert(_conflict==true, "We should not be here");
2256 compute_ret_adr_at_TOS();
2257 if (!_got_error) {
2258 for (int k = 0; k < _max_locals && !_got_error; k++) {
2259 if (_new_var_map[k] != k) {
2260 if (TraceOopMapRewrites) {
2261 tty->print_cr("Rewriting: %d -> %d", k, _new_var_map[k]);
2262 }
2263 rewrite_refval_conflict(k, _new_var_map[k]);
2264 if (_got_error) return;
2265 nof_conflicts++;
2266 }
2267 }
2268 }
2270 assert(nof_conflicts == _nof_refval_conflicts, "sanity check");
2272 // Adjust the number of locals
2273 method()->set_max_locals(_max_locals+_nof_refval_conflicts);
2274 _max_locals += _nof_refval_conflicts;
2276 // That was that...
2277 _new_var_map = NULL;
2278 _nof_refval_conflicts = 0;
2279 }
2281 void GenerateOopMap::rewrite_refval_conflict(int from, int to) {
2282 bool startOver;
2283 do {
2284 // Make sure that the BytecodeStream is constructed in the loop, since
2285 // during rewriting a new method oop is going to be used, and the next time
2286 // around we want to use that.
2287 BytecodeStream bcs(_method);
2288 startOver = false;
2290 while( bcs.next() >=0 && !startOver && !_got_error) {
2291 startOver = rewrite_refval_conflict_inst(&bcs, from, to);
2292 }
2293 } while (startOver && !_got_error);
2294 }
2296 /* If the current instruction is one that uses local variable "from"
2297 in a ref way, change it to use "to". There's a subtle reason why we
2298 renumber the ref uses and not the non-ref uses: non-ref uses may be
2299 2 slots wide (double, long) which would necessitate keeping track of
2300 whether we should add one or two variables to the method. If the change
2301 affected the width of some instruction, returns "TRUE"; otherwise, returns "FALSE".
2302 Another reason for moving ref's value is for solving (addr, ref) conflicts, which
2303 both uses aload/astore methods.
2304 */
2305 bool GenerateOopMap::rewrite_refval_conflict_inst(BytecodeStream *itr, int from, int to) {
2306 Bytecodes::Code bc = itr->code();
2307 int index;
2308 int bci = itr->bci();
2310 if (is_aload(itr, &index) && index == from) {
2311 if (TraceOopMapRewrites) {
2312 tty->print_cr("Rewriting aload at bci: %d", bci);
2313 }
2314 return rewrite_load_or_store(itr, Bytecodes::_aload, Bytecodes::_aload_0, to);
2315 }
2317 if (is_astore(itr, &index) && index == from) {
2318 if (!stack_top_holds_ret_addr(bci)) {
2319 if (TraceOopMapRewrites) {
2320 tty->print_cr("Rewriting astore at bci: %d", bci);
2321 }
2322 return rewrite_load_or_store(itr, Bytecodes::_astore, Bytecodes::_astore_0, to);
2323 } else {
2324 if (TraceOopMapRewrites) {
2325 tty->print_cr("Supress rewriting of astore at bci: %d", bci);
2326 }
2327 }
2328 }
2330 return false;
2331 }
2333 // The argument to this method is:
2334 // bc : Current bytecode
2335 // bcN : either _aload or _astore
2336 // bc0 : either _aload_0 or _astore_0
2337 bool GenerateOopMap::rewrite_load_or_store(BytecodeStream *bcs, Bytecodes::Code bcN, Bytecodes::Code bc0, unsigned int varNo) {
2338 assert(bcN == Bytecodes::_astore || bcN == Bytecodes::_aload, "wrong argument (bcN)");
2339 assert(bc0 == Bytecodes::_astore_0 || bc0 == Bytecodes::_aload_0, "wrong argument (bc0)");
2340 int ilen = Bytecodes::length_at(bcs->bcp());
2341 int newIlen;
2343 if (ilen == 4) {
2344 // Original instruction was wide; keep it wide for simplicity
2345 newIlen = 4;
2346 } else if (varNo < 4)
2347 newIlen = 1;
2348 else if (varNo >= 256)
2349 newIlen = 4;
2350 else
2351 newIlen = 2;
2353 // If we need to relocate in order to patch the byte, we
2354 // do the patching in a temp. buffer, that is passed to the reloc.
2355 // The patching of the bytecode stream is then done by the Relocator.
2356 // This is neccesary, since relocating the instruction at a certain bci, might
2357 // also relocate that instruction, e.g., if a _goto before it gets widen to a _goto_w.
2358 // Hence, we do not know which bci to patch after relocation.
2360 assert(newIlen <= 4, "sanity check");
2361 u_char inst_buffer[4]; // Max. instruction size is 4.
2362 address bcp;
2364 if (newIlen != ilen) {
2365 // Relocation needed do patching in temp. buffer
2366 bcp = (address)inst_buffer;
2367 } else {
2368 bcp = _method->bcp_from(bcs->bci());
2369 }
2371 // Patch either directly in methodOop or in temp. buffer
2372 if (newIlen == 1) {
2373 assert(varNo < 4, "varNo too large");
2374 *bcp = bc0 + varNo;
2375 } else if (newIlen == 2) {
2376 assert(varNo < 256, "2-byte index needed!");
2377 *(bcp + 0) = bcN;
2378 *(bcp + 1) = varNo;
2379 } else {
2380 assert(newIlen == 4, "Wrong instruction length");
2381 *(bcp + 0) = Bytecodes::_wide;
2382 *(bcp + 1) = bcN;
2383 Bytes::put_Java_u2(bcp+2, varNo);
2384 }
2386 if (newIlen != ilen) {
2387 expand_current_instr(bcs->bci(), ilen, newIlen, inst_buffer);
2388 }
2391 return (newIlen != ilen);
2392 }
2394 class RelocCallback : public RelocatorListener {
2395 private:
2396 GenerateOopMap* _gom;
2397 public:
2398 RelocCallback(GenerateOopMap* gom) { _gom = gom; };
2400 // Callback method
2401 virtual void relocated(int bci, int delta, int new_code_length) {
2402 _gom->update_basic_blocks (bci, delta, new_code_length);
2403 _gom->update_ret_adr_at_TOS(bci, delta);
2404 _gom->_rt.update_ret_table (bci, delta);
2405 }
2406 };
2408 // Returns true if expanding was succesful. Otherwise, reports an error and
2409 // returns false.
2410 void GenerateOopMap::expand_current_instr(int bci, int ilen, int newIlen, u_char inst_buffer[]) {
2411 Thread *THREAD = Thread::current(); // Could really have TRAPS argument.
2412 RelocCallback rcb(this);
2413 Relocator rc(_method, &rcb);
2414 methodHandle m= rc.insert_space_at(bci, newIlen, inst_buffer, THREAD);
2415 if (m.is_null() || HAS_PENDING_EXCEPTION) {
2416 report_error("could not rewrite method - exception occurred or bytecode buffer overflow");
2417 return;
2418 }
2420 // Relocator returns a new method oop.
2421 _did_relocation = true;
2422 _method = m;
2423 }
2426 bool GenerateOopMap::is_astore(BytecodeStream *itr, int *index) {
2427 Bytecodes::Code bc = itr->code();
2428 switch(bc) {
2429 case Bytecodes::_astore_0:
2430 case Bytecodes::_astore_1:
2431 case Bytecodes::_astore_2:
2432 case Bytecodes::_astore_3:
2433 *index = bc - Bytecodes::_astore_0;
2434 return true;
2435 case Bytecodes::_astore:
2436 *index = itr->get_index();
2437 return true;
2438 }
2439 return false;
2440 }
2442 bool GenerateOopMap::is_aload(BytecodeStream *itr, int *index) {
2443 Bytecodes::Code bc = itr->code();
2444 switch(bc) {
2445 case Bytecodes::_aload_0:
2446 case Bytecodes::_aload_1:
2447 case Bytecodes::_aload_2:
2448 case Bytecodes::_aload_3:
2449 *index = bc - Bytecodes::_aload_0;
2450 return true;
2452 case Bytecodes::_aload:
2453 *index = itr->get_index();
2454 return true;
2455 }
2456 return false;
2457 }
2460 // Return true iff the top of the operand stack holds a return address at
2461 // the current instruction
2462 bool GenerateOopMap::stack_top_holds_ret_addr(int bci) {
2463 for(int i = 0; i < _ret_adr_tos->length(); i++) {
2464 if (_ret_adr_tos->at(i) == bci)
2465 return true;
2466 }
2468 return false;
2469 }
2471 void GenerateOopMap::compute_ret_adr_at_TOS() {
2472 assert(_ret_adr_tos != NULL, "must be initialized");
2473 _ret_adr_tos->clear();
2475 for (int i = 0; i < bb_count(); i++) {
2476 BasicBlock* bb = &_basic_blocks[i];
2478 // Make sure to only check basicblocks that are reachable
2479 if (bb->is_reachable()) {
2481 // For each Basic block we check all instructions
2482 BytecodeStream bcs(_method);
2483 bcs.set_interval(bb->_bci, next_bb_start_pc(bb));
2485 restore_state(bb);
2487 while (bcs.next()>=0 && !_got_error) {
2488 // TDT: should this be is_good_address() ?
2489 if (_stack_top > 0 && stack()[_stack_top-1].is_address()) {
2490 _ret_adr_tos->append(bcs.bci());
2491 if (TraceNewOopMapGeneration) {
2492 tty->print_cr("Ret_adr TOS at bci: %d", bcs.bci());
2493 }
2494 }
2495 interp1(&bcs);
2496 }
2497 }
2498 }
2499 }
2501 void GenerateOopMap::update_ret_adr_at_TOS(int bci, int delta) {
2502 for(int i = 0; i < _ret_adr_tos->length(); i++) {
2503 int v = _ret_adr_tos->at(i);
2504 if (v > bci) _ret_adr_tos->at_put(i, v + delta);
2505 }
2506 }
2508 // ===================================================================
2510 #ifndef PRODUCT
2511 int ResolveOopMapConflicts::_nof_invocations = 0;
2512 int ResolveOopMapConflicts::_nof_rewrites = 0;
2513 int ResolveOopMapConflicts::_nof_relocations = 0;
2514 #endif
2516 methodHandle ResolveOopMapConflicts::do_potential_rewrite(TRAPS) {
2517 compute_map(CHECK_(methodHandle()));
2519 #ifndef PRODUCT
2520 // Tracking and statistics
2521 if (PrintRewrites) {
2522 _nof_invocations++;
2523 if (did_rewriting()) {
2524 _nof_rewrites++;
2525 if (did_relocation()) _nof_relocations++;
2526 tty->print("Method was rewritten %s: ", (did_relocation()) ? "and relocated" : "");
2527 method()->print_value(); tty->cr();
2528 tty->print_cr("Cand.: %d rewrts: %d (%d%%) reloc.: %d (%d%%)",
2529 _nof_invocations,
2530 _nof_rewrites, (_nof_rewrites * 100) / _nof_invocations,
2531 _nof_relocations, (_nof_relocations * 100) / _nof_invocations);
2532 }
2533 }
2534 #endif
2535 return methodHandle(THREAD, method());
2536 }