generateOopMap.cpp revision 1472:c18cbe5936b8
1/* 2 * Copyright (c) 1997, 2009, Oracle and/or its affiliates. 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 * 23 */ 24 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) 87 88#include "incls/_precompiled.incl" 89#include "incls/_generateOopMap.cpp.incl" 90 91// ComputeCallStack 92// 93// Specialization of SignatureIterator - compute the effects of a call 94// 95class ComputeCallStack : public SignatureIterator { 96 CellTypeState *_effect; 97 int _idx; 98 99 void setup(); 100 void set(CellTypeState state) { _effect[_idx++] = state; } 101 int length() { return _idx; }; 102 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); }; 112 113 void do_double() { set(CellTypeState::value); 114 set(CellTypeState::value); } 115 void do_long () { set(CellTypeState::value); 116 set(CellTypeState::value); } 117 118public: 119 ComputeCallStack(symbolOop signature) : SignatureIterator(signature) {}; 120 121 // Compute methods 122 int compute_for_parameters(bool is_static, CellTypeState *effect) { 123 _idx = 0; 124 _effect = effect; 125 126 if (!is_static) 127 effect[_idx++] = CellTypeState::ref; 128 129 iterate_parameters(); 130 131 return length(); 132 }; 133 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 139 140 return length(); 141 } 142}; 143 144//========================================================================================= 145// ComputeEntryStack 146// 147// Specialization of SignatureIterator - in order to set up first stack frame 148// 149class ComputeEntryStack : public SignatureIterator { 150 CellTypeState *_effect; 151 int _idx; 152 153 void setup(); 154 void set(CellTypeState state) { _effect[_idx++] = state; } 155 int length() { return _idx; }; 156 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)); } 166 167 void do_double() { set(CellTypeState::value); 168 set(CellTypeState::value); } 169 void do_long () { set(CellTypeState::value); 170 set(CellTypeState::value); } 171 172public: 173 ComputeEntryStack(symbolOop signature) : SignatureIterator(signature) {}; 174 175 // Compute methods 176 int compute_for_parameters(bool is_static, CellTypeState *effect) { 177 _idx = 0; 178 _effect = effect; 179 180 if (!is_static) 181 effect[_idx++] = CellTypeState::make_slot_ref(0); 182 183 iterate_parameters(); 184 185 return length(); 186 }; 187 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 193 194 return length(); 195 } 196}; 197 198//===================================================================================== 199// 200// Implementation of RetTable/RetTableEntry 201// 202// Contains function to itereate through all bytecodes 203// and find all return entry points 204// 205int RetTable::_init_nof_entries = 10; 206int RetTableEntry::_init_nof_jsrs = 5; 207 208void RetTableEntry::add_delta(int bci, int delta) { 209 if (_target_bci > bci) _target_bci += delta; 210 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} 216 217void RetTable::compute_ret_table(methodHandle method) { 218 BytecodeStream i(method); 219 Bytecodes::Code bytecode; 220 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} 232 233void RetTable::add_jsr(int return_bci, int target_bci) { 234 RetTableEntry* entry = _first; 235 236 // Scan table for entry 237 for (;entry && entry->target_bci() != target_bci; entry = entry->next()); 238 239 if (!entry) { 240 // Allocate new entry and put in list 241 entry = new RetTableEntry(target_bci, _first); 242 _first = entry; 243 } 244 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} 249 250RetTableEntry* RetTable::find_jsrs_for_target(int targBci) { 251 RetTableEntry *cur = _first; 252 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} 261 262// The instruction at bci is changing size by "delta". Update the return map. 263void 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} 270 271// 272// Celltype state 273// 274 275CellTypeState CellTypeState::bottom = CellTypeState::make_bottom(); 276CellTypeState CellTypeState::uninit = CellTypeState::make_any(uninit_value); 277CellTypeState CellTypeState::ref = CellTypeState::make_any(ref_conflict); 278CellTypeState CellTypeState::value = CellTypeState::make_any(val_value); 279CellTypeState CellTypeState::refUninit = CellTypeState::make_any(ref_conflict | uninit_value); 280CellTypeState CellTypeState::top = CellTypeState::make_top(); 281CellTypeState CellTypeState::addr = CellTypeState::make_any(addr_conflict); 282 283// Commonly used constants 284static CellTypeState epsilonCTS[1] = { CellTypeState::bottom }; 285static CellTypeState refCTS = CellTypeState::ref; 286static CellTypeState valCTS = CellTypeState::value; 287static CellTypeState vCTS[2] = { CellTypeState::value, CellTypeState::bottom }; 288static CellTypeState rCTS[2] = { CellTypeState::ref, CellTypeState::bottom }; 289static CellTypeState rrCTS[3] = { CellTypeState::ref, CellTypeState::ref, CellTypeState::bottom }; 290static CellTypeState vrCTS[3] = { CellTypeState::value, CellTypeState::ref, CellTypeState::bottom }; 291static CellTypeState vvCTS[3] = { CellTypeState::value, CellTypeState::value, CellTypeState::bottom }; 292static CellTypeState rvrCTS[4] = { CellTypeState::ref, CellTypeState::value, CellTypeState::ref, CellTypeState::bottom }; 293static CellTypeState vvrCTS[4] = { CellTypeState::value, CellTypeState::value, CellTypeState::ref, CellTypeState::bottom }; 294static CellTypeState vvvCTS[4] = { CellTypeState::value, CellTypeState::value, CellTypeState::value, CellTypeState::bottom }; 295static CellTypeState vvvrCTS[5] = { CellTypeState::value, CellTypeState::value, CellTypeState::value, CellTypeState::ref, CellTypeState::bottom }; 296static CellTypeState vvvvCTS[5] = { CellTypeState::value, CellTypeState::value, CellTypeState::value, CellTypeState::value, CellTypeState::bottom }; 297 298char 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} 313 314 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. 318void 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} 365 366// 367// Basicblock handling methods 368// 369 370void 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} 376 377void 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; 381 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} 388 389 390void GenerateOopMap::mark_bbheaders_and_count_gc_points() { 391 initialize_bb(); 392 393 bool fellThrough = false; // False to get first BB marked. 394 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 } 401 402 // Then iterate through the code 403 BytecodeStream bcs(_method); 404 Bytecodes::Code bytecode; 405 406 while( (bytecode = bcs.next()) >= 0) { 407 int bci = bcs.bci(); 408 409 if (!fellThrough) 410 bb_mark_fct(this, bci, NULL); 411 412 fellThrough = jump_targets_do(&bcs, &GenerateOopMap::bb_mark_fct, NULL); 413 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 } 425 426 if (possible_gc_point(&bcs)) 427 _gc_points++; 428 } 429} 430 431void 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} 439 440 441void GenerateOopMap::mark_reachable_code() { 442 int change = 1; // int to get function pointers to work 443 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 } 454 455 BytecodeStream bcs(_method); 456 457 // Iterate through all basic blocks until we reach a fixpoint 458 while (change) { 459 change = 0; 460 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"); 470 471 bool fell_through = jump_targets_do(&bcs, &GenerateOopMap::reachable_basicblock, &change); 472 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} 492 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. */ 500bool GenerateOopMap::jump_targets_do(BytecodeStream *bcs, jmpFct_t jmpFct, int *data) { 501 int bci = bcs->bci(); 502 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; 523 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(); 533 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 } 540 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); 554 555 556 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} 579 580/* Requires "pc" to be the head of a basic block; returns that basic 581 block. */ 582BasicBlock *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} 587 588// Requires "pc" to be the start of an instruction; returns the basic 589// block containing that instruction. */ 590BasicBlock *GenerateOopMap::get_basic_block_containing(int bci) const { 591 BasicBlock *bbs = _basic_blocks; 592 int lo = 0, hi = _bb_count - 1; 593 594 while (lo <= hi) { 595 int m = (lo + hi) / 2; 596 int mbci = bbs[m]._bci; 597 int nbci; 598 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 } 605 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 } 615 616 fatal("should have found BB"); 617 return NULL; 618} 619 620void 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} 626 627int GenerateOopMap::next_bb_start_pc(BasicBlock *bb) { 628 int bbNum = bb - _basic_blocks + 1; 629 if (bbNum == _bb_count) 630 return method()->code_size(); 631 632 return _basic_blocks[bbNum]._bci; 633} 634 635// 636// CellType handling methods 637// 638 639void 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} 645 646void GenerateOopMap::make_context_uninitialized() { 647 CellTypeState* vs = vars(); 648 649 for (int i = 0; i < _max_locals; i++) 650 vs[i] = CellTypeState::uninit; 651 652 _stack_top = 0; 653 _monitor_top = 0; 654} 655 656int 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} 660 661// Return result of merging cts1 and cts2. 662CellTypeState CellTypeState::merge(CellTypeState cts, int slot) const { 663 CellTypeState result; 664 665 assert(!is_bottom() && !cts.is_bottom(), 666 "merge of bottom values is handled elsewhere"); 667 668 result._state = _state | cts._state; 669 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"); 674 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"); 685 686 return result; 687} 688 689// Merge the variable state for locals and stack from cts into bbts. 690bool GenerateOopMap::merge_local_state_vectors(CellTypeState* cts, 691 CellTypeState* bbts) { 692 int i; 693 int len = _max_locals + _stack_top; 694 bool change = false; 695 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 } 701 702 return change; 703} 704 705// Merge the monitor stack state from cts into bbts. 706bool 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. 713 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); 718 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 } 726 727 return change; 728} 729 730void 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} 747 748 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. 758void GenerateOopMap::merge_state_into_bb(BasicBlock *bb) { 759 assert(bb->is_alive(), "merging state into a dead basicblock"); 760 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} 794 795void GenerateOopMap::merge_state(GenerateOopMap *gom, int bci, int* data) { 796 gom->merge_state_into_bb(gom->get_basic_block_at(bci)); 797} 798 799void 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} 808 809CellTypeState 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} 817 818CellTypeState 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} 825 826void GenerateOopMap::push(CellTypeState cts) { 827 if ( _stack_top >= _max_stack) { 828 verify_error("stack overflow"); 829 return; 830 } 831 stack()[_stack_top++] = cts; 832} 833 834CellTypeState 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; 840 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} 848 849void 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; 856 857 if (TraceMonitorMismatch) { 858 report_monitor_mismatch("monitor stack overflow"); 859 } 860 return; 861 } 862 monitors()[_monitor_top++] = cts; 863} 864 865// 866// Interpretation handling methods 867// 868 869void 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} 894 895void 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...) 900 901 _basic_blocks = NEW_RESOURCE_ARRAY(BasicBlock, _bb_count); 902 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; 909 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 } 917 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; 928 929 if (bbNo > 0) { 930 _basic_blocks[bbNo - 1]._end_bci = prev_bci; 931 } 932 933 bbNo++; 934 } 935 // Remember prevous bci. 936 prev_bci = bci; 937 } 938 // Set 939 _basic_blocks[bbNo-1]._end_bci = prev_bci; 940 941 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 } 952 953 _max_monitors = monitor_count; 954 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(); 958 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)); 964 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; 969 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 985 986 // Mark all alive blocks 987 mark_reachable_code(); 988} 989 990void GenerateOopMap::setup_method_entry_state() { 991 992 // Initialize all locals to 'uninit' and set stack-height to 0 993 make_context_uninitialized(); 994 995 // Initialize CellState type of arguments 996 methodsig_to_effect(method()->signature(), method()->is_static(), vars()); 997 998 // If some references must be pre-assigned to null, then set that up 999 initialize_vars(); 1000 1001 // This is the start state 1002 merge_state_into_bb(&_basic_blocks[0]); 1003 1004 assert(_basic_blocks[0].changed(), "we are not getting off the ground"); 1005} 1006 1007// The instruction at bci is changing size by "delta". Update the basic blocks. 1008void 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"); 1012 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(); 1019 1020 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} 1029 1030// 1031// Initvars handling 1032// 1033 1034void 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} 1038 1039void GenerateOopMap::add_to_ref_init_set(int localNo) { 1040 1041 if (TraceNewOopMapGeneration) 1042 tty->print_cr("Added init vars: %d", localNo); 1043 1044 // Is it already in the set? 1045 if (_init_vars->contains(localNo) ) 1046 return; 1047 1048 _init_vars->append(localNo); 1049} 1050 1051// 1052// Interpreration code 1053// 1054 1055void GenerateOopMap::interp_all() { 1056 bool change = true; 1057 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} 1071 1072void GenerateOopMap::interp_bb(BasicBlock *bb) { 1073 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); 1078 1079 BytecodeStream itr(_method); 1080 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 1086 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 } 1100 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); 1108 1109 bool fall_through = jump_targets_do(&itr, GenerateOopMap::merge_state, NULL); 1110 if (_got_error) return; 1111 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} 1127 1128void 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; 1136 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; 1151 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 } 1163 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); 1172 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; 1179 1180 // Exception stacks are always the same. 1181 assert(method()->max_stack() > 0, "sanity check"); 1182 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; 1188 1189 merge_state_into_bb(excBB); 1190 1191 // Now undo the temporary change. 1192 cOpStck[0] = cOpStck_0; 1193 _stack_top = cOpStackTop; 1194 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 } 1203 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 } 1210 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.) 1214 1215 // We don't set _monitor_top to bad_monitors because there are no successors 1216 // to this exceptional exit. 1217 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; 1224 1225} 1226 1227void 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} 1234 1235void 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} 1241 1242// Print the state values at the current bytecode. 1243void GenerateOopMap::print_current_state(outputStream *os, 1244 BytecodeStream *currentBC, 1245 bool detailed) { 1246 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::_invokedynamic: 1256 case Bytecodes::_invokeinterface: 1257 int idx = currentBC->get_index_int(); 1258 constantPoolOop cp = method()->constants(); 1259 int nameAndTypeIdx = cp->name_and_type_ref_index_at(idx); 1260 int signatureIdx = cp->signature_ref_index_at(nameAndTypeIdx); 1261 symbolOop signature = cp->symbol_at(signatureIdx); 1262 os->print("%s", signature->as_C_string()); 1263 } 1264 os->cr(); 1265 os->print(" stack = "); 1266 print_states(os, stack(), _stack_top); 1267 os->cr(); 1268 if (_monitor_top != bad_monitors) { 1269 os->print(" monitors = "); 1270 print_states(os, monitors(), _monitor_top); 1271 } else { 1272 os->print(" [bad monitor stack]"); 1273 } 1274 os->cr(); 1275 } else { 1276 os->print(" %4d vars = '%s' ", currentBC->bci(), state_vec_to_string(vars(), _max_locals)); 1277 os->print(" stack = '%s' ", state_vec_to_string(stack(), _stack_top)); 1278 if (_monitor_top != bad_monitors) { 1279 os->print(" monitors = '%s' \t%s", state_vec_to_string(monitors(), _monitor_top), Bytecodes::name(currentBC->code())); 1280 } else { 1281 os->print(" [bad monitor stack]"); 1282 } 1283 switch(currentBC->code()) { 1284 case Bytecodes::_invokevirtual: 1285 case Bytecodes::_invokespecial: 1286 case Bytecodes::_invokestatic: 1287 case Bytecodes::_invokedynamic: 1288 case Bytecodes::_invokeinterface: 1289 int idx = currentBC->get_index_int(); 1290 constantPoolOop cp = method()->constants(); 1291 int nameAndTypeIdx = cp->name_and_type_ref_index_at(idx); 1292 int signatureIdx = cp->signature_ref_index_at(nameAndTypeIdx); 1293 symbolOop signature = cp->symbol_at(signatureIdx); 1294 os->print("%s", signature->as_C_string()); 1295 } 1296 os->cr(); 1297 } 1298} 1299 1300// Sets the current state to be the state after executing the 1301// current instruction, starting in the current state. 1302void GenerateOopMap::interp1(BytecodeStream *itr) { 1303 if (TraceNewOopMapGeneration) { 1304 print_current_state(tty, itr, TraceNewOopMapGenerationDetailed); 1305 } 1306 1307 // Should we report the results? Result is reported *before* the instruction at the current bci is executed. 1308 // However, not for calls. For calls we do not want to include the arguments, so we postpone the reporting until 1309 // they have been popped (in method ppl). 1310 if (_report_result == true) { 1311 switch(itr->code()) { 1312 case Bytecodes::_invokevirtual: 1313 case Bytecodes::_invokespecial: 1314 case Bytecodes::_invokestatic: 1315 case Bytecodes::_invokedynamic: 1316 case Bytecodes::_invokeinterface: 1317 _itr_send = itr; 1318 _report_result_for_send = true; 1319 break; 1320 default: 1321 fill_stackmap_for_opcodes(itr, vars(), stack(), _stack_top); 1322 break; 1323 } 1324 } 1325 1326 // abstract interpretation of current opcode 1327 switch(itr->code()) { 1328 case Bytecodes::_nop: break; 1329 case Bytecodes::_goto: break; 1330 case Bytecodes::_goto_w: break; 1331 case Bytecodes::_iinc: break; 1332 case Bytecodes::_return: do_return_monitor_check(); 1333 break; 1334 1335 case Bytecodes::_aconst_null: 1336 case Bytecodes::_new: ppush1(CellTypeState::make_line_ref(itr->bci())); 1337 break; 1338 1339 case Bytecodes::_iconst_m1: 1340 case Bytecodes::_iconst_0: 1341 case Bytecodes::_iconst_1: 1342 case Bytecodes::_iconst_2: 1343 case Bytecodes::_iconst_3: 1344 case Bytecodes::_iconst_4: 1345 case Bytecodes::_iconst_5: 1346 case Bytecodes::_fconst_0: 1347 case Bytecodes::_fconst_1: 1348 case Bytecodes::_fconst_2: 1349 case Bytecodes::_bipush: 1350 case Bytecodes::_sipush: ppush1(valCTS); break; 1351 1352 case Bytecodes::_lconst_0: 1353 case Bytecodes::_lconst_1: 1354 case Bytecodes::_dconst_0: 1355 case Bytecodes::_dconst_1: ppush(vvCTS); break; 1356 1357 case Bytecodes::_ldc2_w: ppush(vvCTS); break; 1358 1359 case Bytecodes::_ldc: do_ldc(itr->get_index(), itr->bci()); break; 1360 case Bytecodes::_ldc_w: do_ldc(itr->get_index_big(), itr->bci());break; 1361 1362 case Bytecodes::_iload: 1363 case Bytecodes::_fload: ppload(vCTS, itr->get_index()); break; 1364 1365 case Bytecodes::_lload: 1366 case Bytecodes::_dload: ppload(vvCTS,itr->get_index()); break; 1367 1368 case Bytecodes::_aload: ppload(rCTS, itr->get_index()); break; 1369 1370 case Bytecodes::_iload_0: 1371 case Bytecodes::_fload_0: ppload(vCTS, 0); break; 1372 case Bytecodes::_iload_1: 1373 case Bytecodes::_fload_1: ppload(vCTS, 1); break; 1374 case Bytecodes::_iload_2: 1375 case Bytecodes::_fload_2: ppload(vCTS, 2); break; 1376 case Bytecodes::_iload_3: 1377 case Bytecodes::_fload_3: ppload(vCTS, 3); break; 1378 1379 case Bytecodes::_lload_0: 1380 case Bytecodes::_dload_0: ppload(vvCTS, 0); break; 1381 case Bytecodes::_lload_1: 1382 case Bytecodes::_dload_1: ppload(vvCTS, 1); break; 1383 case Bytecodes::_lload_2: 1384 case Bytecodes::_dload_2: ppload(vvCTS, 2); break; 1385 case Bytecodes::_lload_3: 1386 case Bytecodes::_dload_3: ppload(vvCTS, 3); break; 1387 1388 case Bytecodes::_aload_0: ppload(rCTS, 0); break; 1389 case Bytecodes::_aload_1: ppload(rCTS, 1); break; 1390 case Bytecodes::_aload_2: ppload(rCTS, 2); break; 1391 case Bytecodes::_aload_3: ppload(rCTS, 3); break; 1392 1393 case Bytecodes::_iaload: 1394 case Bytecodes::_faload: 1395 case Bytecodes::_baload: 1396 case Bytecodes::_caload: 1397 case Bytecodes::_saload: pp(vrCTS, vCTS); break; 1398 1399 case Bytecodes::_laload: pp(vrCTS, vvCTS); break; 1400 case Bytecodes::_daload: pp(vrCTS, vvCTS); break; 1401 1402 case Bytecodes::_aaload: pp_new_ref(vrCTS, itr->bci()); break; 1403 1404 case Bytecodes::_istore: 1405 case Bytecodes::_fstore: ppstore(vCTS, itr->get_index()); break; 1406 1407 case Bytecodes::_lstore: 1408 case Bytecodes::_dstore: ppstore(vvCTS, itr->get_index()); break; 1409 1410 case Bytecodes::_astore: do_astore(itr->get_index()); break; 1411 1412 case Bytecodes::_istore_0: 1413 case Bytecodes::_fstore_0: ppstore(vCTS, 0); break; 1414 case Bytecodes::_istore_1: 1415 case Bytecodes::_fstore_1: ppstore(vCTS, 1); break; 1416 case Bytecodes::_istore_2: 1417 case Bytecodes::_fstore_2: ppstore(vCTS, 2); break; 1418 case Bytecodes::_istore_3: 1419 case Bytecodes::_fstore_3: ppstore(vCTS, 3); break; 1420 1421 case Bytecodes::_lstore_0: 1422 case Bytecodes::_dstore_0: ppstore(vvCTS, 0); break; 1423 case Bytecodes::_lstore_1: 1424 case Bytecodes::_dstore_1: ppstore(vvCTS, 1); break; 1425 case Bytecodes::_lstore_2: 1426 case Bytecodes::_dstore_2: ppstore(vvCTS, 2); break; 1427 case Bytecodes::_lstore_3: 1428 case Bytecodes::_dstore_3: ppstore(vvCTS, 3); break; 1429 1430 case Bytecodes::_astore_0: do_astore(0); break; 1431 case Bytecodes::_astore_1: do_astore(1); break; 1432 case Bytecodes::_astore_2: do_astore(2); break; 1433 case Bytecodes::_astore_3: do_astore(3); break; 1434 1435 case Bytecodes::_iastore: 1436 case Bytecodes::_fastore: 1437 case Bytecodes::_bastore: 1438 case Bytecodes::_castore: 1439 case Bytecodes::_sastore: ppop(vvrCTS); break; 1440 case Bytecodes::_lastore: 1441 case Bytecodes::_dastore: ppop(vvvrCTS); break; 1442 case Bytecodes::_aastore: ppop(rvrCTS); break; 1443 1444 case Bytecodes::_pop: ppop_any(1); break; 1445 case Bytecodes::_pop2: ppop_any(2); break; 1446 1447 case Bytecodes::_dup: ppdupswap(1, "11"); break; 1448 case Bytecodes::_dup_x1: ppdupswap(2, "121"); break; 1449 case Bytecodes::_dup_x2: ppdupswap(3, "1321"); break; 1450 case Bytecodes::_dup2: ppdupswap(2, "2121"); break; 1451 case Bytecodes::_dup2_x1: ppdupswap(3, "21321"); break; 1452 case Bytecodes::_dup2_x2: ppdupswap(4, "214321"); break; 1453 case Bytecodes::_swap: ppdupswap(2, "12"); break; 1454 1455 case Bytecodes::_iadd: 1456 case Bytecodes::_fadd: 1457 case Bytecodes::_isub: 1458 case Bytecodes::_fsub: 1459 case Bytecodes::_imul: 1460 case Bytecodes::_fmul: 1461 case Bytecodes::_idiv: 1462 case Bytecodes::_fdiv: 1463 case Bytecodes::_irem: 1464 case Bytecodes::_frem: 1465 case Bytecodes::_ishl: 1466 case Bytecodes::_ishr: 1467 case Bytecodes::_iushr: 1468 case Bytecodes::_iand: 1469 case Bytecodes::_ior: 1470 case Bytecodes::_ixor: 1471 case Bytecodes::_l2f: 1472 case Bytecodes::_l2i: 1473 case Bytecodes::_d2f: 1474 case Bytecodes::_d2i: 1475 case Bytecodes::_fcmpl: 1476 case Bytecodes::_fcmpg: pp(vvCTS, vCTS); break; 1477 1478 case Bytecodes::_ladd: 1479 case Bytecodes::_dadd: 1480 case Bytecodes::_lsub: 1481 case Bytecodes::_dsub: 1482 case Bytecodes::_lmul: 1483 case Bytecodes::_dmul: 1484 case Bytecodes::_ldiv: 1485 case Bytecodes::_ddiv: 1486 case Bytecodes::_lrem: 1487 case Bytecodes::_drem: 1488 case Bytecodes::_land: 1489 case Bytecodes::_lor: 1490 case Bytecodes::_lxor: pp(vvvvCTS, vvCTS); break; 1491 1492 case Bytecodes::_ineg: 1493 case Bytecodes::_fneg: 1494 case Bytecodes::_i2f: 1495 case Bytecodes::_f2i: 1496 case Bytecodes::_i2c: 1497 case Bytecodes::_i2s: 1498 case Bytecodes::_i2b: pp(vCTS, vCTS); break; 1499 1500 case Bytecodes::_lneg: 1501 case Bytecodes::_dneg: 1502 case Bytecodes::_l2d: 1503 case Bytecodes::_d2l: pp(vvCTS, vvCTS); break; 1504 1505 case Bytecodes::_lshl: 1506 case Bytecodes::_lshr: 1507 case Bytecodes::_lushr: pp(vvvCTS, vvCTS); break; 1508 1509 case Bytecodes::_i2l: 1510 case Bytecodes::_i2d: 1511 case Bytecodes::_f2l: 1512 case Bytecodes::_f2d: pp(vCTS, vvCTS); break; 1513 1514 case Bytecodes::_lcmp: pp(vvvvCTS, vCTS); break; 1515 case Bytecodes::_dcmpl: 1516 case Bytecodes::_dcmpg: pp(vvvvCTS, vCTS); break; 1517 1518 case Bytecodes::_ifeq: 1519 case Bytecodes::_ifne: 1520 case Bytecodes::_iflt: 1521 case Bytecodes::_ifge: 1522 case Bytecodes::_ifgt: 1523 case Bytecodes::_ifle: 1524 case Bytecodes::_tableswitch: ppop1(valCTS); 1525 break; 1526 case Bytecodes::_ireturn: 1527 case Bytecodes::_freturn: do_return_monitor_check(); 1528 ppop1(valCTS); 1529 break; 1530 case Bytecodes::_if_icmpeq: 1531 case Bytecodes::_if_icmpne: 1532 case Bytecodes::_if_icmplt: 1533 case Bytecodes::_if_icmpge: 1534 case Bytecodes::_if_icmpgt: 1535 case Bytecodes::_if_icmple: ppop(vvCTS); 1536 break; 1537 1538 case Bytecodes::_lreturn: do_return_monitor_check(); 1539 ppop(vvCTS); 1540 break; 1541 1542 case Bytecodes::_dreturn: do_return_monitor_check(); 1543 ppop(vvCTS); 1544 break; 1545 1546 case Bytecodes::_if_acmpeq: 1547 case Bytecodes::_if_acmpne: ppop(rrCTS); break; 1548 1549 case Bytecodes::_jsr: do_jsr(itr->dest()); break; 1550 case Bytecodes::_jsr_w: do_jsr(itr->dest_w()); break; 1551 1552 case Bytecodes::_getstatic: do_field(true, true, 1553 itr->get_index_big(), 1554 itr->bci()); break; 1555 case Bytecodes::_putstatic: do_field(false, true, itr->get_index_big(), itr->bci()); break; 1556 case Bytecodes::_getfield: do_field(true, false, itr->get_index_big(), itr->bci()); break; 1557 case Bytecodes::_putfield: do_field(false, false, itr->get_index_big(), itr->bci()); break; 1558 1559 case Bytecodes::_invokevirtual: 1560 case Bytecodes::_invokespecial: do_method(false, false, itr->get_index_big(), itr->bci()); break; 1561 case Bytecodes::_invokestatic: do_method(true, false, itr->get_index_big(), itr->bci()); break; 1562 case Bytecodes::_invokedynamic: do_method(true, false, itr->get_index_int(), itr->bci()); break; 1563 case Bytecodes::_invokeinterface: do_method(false, true, itr->get_index_big(), itr->bci()); break; 1564 case Bytecodes::_newarray: 1565 case Bytecodes::_anewarray: pp_new_ref(vCTS, itr->bci()); break; 1566 case Bytecodes::_checkcast: do_checkcast(); break; 1567 case Bytecodes::_arraylength: 1568 case Bytecodes::_instanceof: pp(rCTS, vCTS); break; 1569 case Bytecodes::_monitorenter: do_monitorenter(itr->bci()); break; 1570 case Bytecodes::_monitorexit: do_monitorexit(itr->bci()); break; 1571 1572 case Bytecodes::_athrow: // handled by do_exception_edge() BUT ... 1573 // vlh(apple): do_exception_edge() does not get 1574 // called if method has no exception handlers 1575 if ((!_has_exceptions) && (_monitor_top > 0)) { 1576 _monitor_safe = false; 1577 } 1578 break; 1579 1580 case Bytecodes::_areturn: do_return_monitor_check(); 1581 ppop1(refCTS); 1582 break; 1583 case Bytecodes::_ifnull: 1584 case Bytecodes::_ifnonnull: ppop1(refCTS); break; 1585 case Bytecodes::_multianewarray: do_multianewarray(*(itr->bcp()+3), itr->bci()); break; 1586 1587 case Bytecodes::_wide: fatal("Iterator should skip this bytecode"); break; 1588 case Bytecodes::_ret: break; 1589 1590 // Java opcodes 1591 case Bytecodes::_lookupswitch: ppop1(valCTS); break; 1592 1593 default: 1594 tty->print("unexpected opcode: %d\n", itr->code()); 1595 ShouldNotReachHere(); 1596 break; 1597 } 1598} 1599 1600void GenerateOopMap::check_type(CellTypeState expected, CellTypeState actual) { 1601 if (!expected.equal_kind(actual)) { 1602 verify_error("wrong type on stack (found: %c expected: %c)", actual.to_char(), expected.to_char()); 1603 } 1604} 1605 1606void GenerateOopMap::ppstore(CellTypeState *in, int loc_no) { 1607 while(!(*in).is_bottom()) { 1608 CellTypeState expected =*in++; 1609 CellTypeState actual = pop(); 1610 check_type(expected, actual); 1611 assert(loc_no >= 0, "sanity check"); 1612 set_var(loc_no++, actual); 1613 } 1614} 1615 1616void GenerateOopMap::ppload(CellTypeState *out, int loc_no) { 1617 while(!(*out).is_bottom()) { 1618 CellTypeState out1 = *out++; 1619 CellTypeState vcts = get_var(loc_no); 1620 assert(out1.can_be_reference() || out1.can_be_value(), 1621 "can only load refs. and values."); 1622 if (out1.is_reference()) { 1623 assert(loc_no>=0, "sanity check"); 1624 if (!vcts.is_reference()) { 1625 // We were asked to push a reference, but the type of the 1626 // variable can be something else 1627 _conflict = true; 1628 if (vcts.can_be_uninit()) { 1629 // It is a ref-uninit conflict (at least). If there are other 1630 // problems, we'll get them in the next round 1631 add_to_ref_init_set(loc_no); 1632 vcts = out1; 1633 } else { 1634 // It wasn't a ref-uninit conflict. So must be a 1635 // ref-val or ref-pc conflict. Split the variable. 1636 record_refval_conflict(loc_no); 1637 vcts = out1; 1638 } 1639 push(out1); // recover... 1640 } else { 1641 push(vcts); // preserve reference. 1642 } 1643 // Otherwise it is a conflict, but one that verification would 1644 // have caught if illegal. In particular, it can't be a topCTS 1645 // resulting from mergeing two difference pcCTS's since the verifier 1646 // would have rejected any use of such a merge. 1647 } else { 1648 push(out1); // handle val/init conflict 1649 } 1650 loc_no++; 1651 } 1652} 1653 1654void GenerateOopMap::ppdupswap(int poplen, const char *out) { 1655 CellTypeState actual[5]; 1656 assert(poplen < 5, "this must be less than length of actual vector"); 1657 1658 // pop all arguments 1659 for(int i = 0; i < poplen; i++) actual[i] = pop(); 1660 1661 // put them back 1662 char push_ch = *out++; 1663 while (push_ch != '\0') { 1664 int idx = push_ch - '1'; 1665 assert(idx >= 0 && idx < poplen, "wrong arguments"); 1666 push(actual[idx]); 1667 push_ch = *out++; 1668 } 1669} 1670 1671void GenerateOopMap::ppop1(CellTypeState out) { 1672 CellTypeState actual = pop(); 1673 check_type(out, actual); 1674} 1675 1676void GenerateOopMap::ppop(CellTypeState *out) { 1677 while (!(*out).is_bottom()) { 1678 ppop1(*out++); 1679 } 1680} 1681 1682void GenerateOopMap::ppush1(CellTypeState in) { 1683 assert(in.is_reference() | in.is_value(), "sanity check"); 1684 push(in); 1685} 1686 1687void GenerateOopMap::ppush(CellTypeState *in) { 1688 while (!(*in).is_bottom()) { 1689 ppush1(*in++); 1690 } 1691} 1692 1693void GenerateOopMap::pp(CellTypeState *in, CellTypeState *out) { 1694 ppop(in); 1695 ppush(out); 1696} 1697 1698void GenerateOopMap::pp_new_ref(CellTypeState *in, int bci) { 1699 ppop(in); 1700 ppush1(CellTypeState::make_line_ref(bci)); 1701} 1702 1703void GenerateOopMap::ppop_any(int poplen) { 1704 if (_stack_top >= poplen) { 1705 _stack_top -= poplen; 1706 } else { 1707 verify_error("stack underflow"); 1708 } 1709} 1710 1711// Replace all occurences of the state 'match' with the state 'replace' 1712// in our current state vector. 1713void GenerateOopMap::replace_all_CTS_matches(CellTypeState match, 1714 CellTypeState replace) { 1715 int i; 1716 int len = _max_locals + _stack_top; 1717 bool change = false; 1718 1719 for (i = len - 1; i >= 0; i--) { 1720 if (match.equal(_state[i])) { 1721 _state[i] = replace; 1722 } 1723 } 1724 1725 if (_monitor_top > 0) { 1726 int base = _max_locals + _max_stack; 1727 len = base + _monitor_top; 1728 for (i = len - 1; i >= base; i--) { 1729 if (match.equal(_state[i])) { 1730 _state[i] = replace; 1731 } 1732 } 1733 } 1734} 1735 1736void GenerateOopMap::do_checkcast() { 1737 CellTypeState actual = pop(); 1738 check_type(refCTS, actual); 1739 push(actual); 1740} 1741 1742void GenerateOopMap::do_monitorenter(int bci) { 1743 CellTypeState actual = pop(); 1744 if (_monitor_top == bad_monitors) { 1745 return; 1746 } 1747 1748 // Bail out when we get repeated locks on an identical monitor. This case 1749 // isn't too hard to handle and can be made to work if supporting nested 1750 // redundant synchronized statements becomes a priority. 1751 // 1752 // See also "Note" in do_monitorexit(), below. 1753 if (actual.is_lock_reference()) { 1754 _monitor_top = bad_monitors; 1755 _monitor_safe = false; 1756 1757 if (TraceMonitorMismatch) { 1758 report_monitor_mismatch("nested redundant lock -- bailout..."); 1759 } 1760 return; 1761 } 1762 1763 CellTypeState lock = CellTypeState::make_lock_ref(bci); 1764 check_type(refCTS, actual); 1765 if (!actual.is_info_top()) { 1766 replace_all_CTS_matches(actual, lock); 1767 monitor_push(lock); 1768 } 1769} 1770 1771void GenerateOopMap::do_monitorexit(int bci) { 1772 CellTypeState actual = pop(); 1773 if (_monitor_top == bad_monitors) { 1774 return; 1775 } 1776 check_type(refCTS, actual); 1777 CellTypeState expected = monitor_pop(); 1778 if (!actual.is_lock_reference() || !expected.equal(actual)) { 1779 // The monitor we are exiting is not verifiably the one 1780 // on the top of our monitor stack. This causes a monitor 1781 // mismatch. 1782 _monitor_top = bad_monitors; 1783 _monitor_safe = false; 1784 1785 // We need to mark this basic block as changed so that 1786 // this monitorexit will be visited again. We need to 1787 // do this to ensure that we have accounted for the 1788 // possibility that this bytecode will throw an 1789 // exception. 1790 BasicBlock* bb = get_basic_block_containing(bci); 1791 bb->set_changed(true); 1792 bb->_monitor_top = bad_monitors; 1793 1794 if (TraceMonitorMismatch) { 1795 report_monitor_mismatch("improper monitor pair"); 1796 } 1797 } else { 1798 // This code is a fix for the case where we have repeated 1799 // locking of the same object in straightline code. We clear 1800 // out the lock when it is popped from the monitor stack 1801 // and replace it with an unobtrusive reference value that can 1802 // be locked again. 1803 // 1804 // Note: when generateOopMap is fixed to properly handle repeated, 1805 // nested, redundant locks on the same object, then this 1806 // fix will need to be removed at that time. 1807 replace_all_CTS_matches(actual, CellTypeState::make_line_ref(bci)); 1808 } 1809} 1810 1811void GenerateOopMap::do_return_monitor_check() { 1812 if (_monitor_top > 0) { 1813 // The monitor stack must be empty when we leave the method 1814 // for the monitors to be properly matched. 1815 _monitor_safe = false; 1816 1817 // Since there are no successors to the *return bytecode, it 1818 // isn't necessary to set _monitor_top to bad_monitors. 1819 1820 if (TraceMonitorMismatch) { 1821 report_monitor_mismatch("non-empty monitor stack at return"); 1822 } 1823 } 1824} 1825 1826void GenerateOopMap::do_jsr(int targ_bci) { 1827 push(CellTypeState::make_addr(targ_bci)); 1828} 1829 1830 1831 1832void GenerateOopMap::do_ldc(int idx, int bci) { 1833 constantPoolOop cp = method()->constants(); 1834 CellTypeState cts = cp->is_pointer_entry(idx) ? CellTypeState::make_line_ref(bci) : valCTS; 1835 ppush1(cts); 1836} 1837 1838void 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} 1845 1846void 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} 1856 1857// Copies bottom/zero terminated CTS string from "src" into "dst". 1858// Does NOT terminate with a bottom. Returns the number of cells copied. 1859int 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} 1867 1868void 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); 1874 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); 1881 1882 CellTypeState in[4]; 1883 CellTypeState *out; 1884 int i = 0; 1885 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} 1897 1898void 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 symbolOop signature = cp->signature_ref_at(idx); 1902 1903 // Parse method signature 1904 CellTypeState out[4]; 1905 CellTypeState in[MAXARGSIZE+1]; // Includes result 1906 ComputeCallStack cse(signature); 1907 1908 // Compute return type 1909 int res_length= cse.compute_for_returntype(out); 1910 1911 // Temporary hack. 1912 if (out[0].equal(CellTypeState::ref) && out[1].equal(CellTypeState::bottom)) { 1913 out[0] = CellTypeState::make_line_ref(bci); 1914 } 1915 1916 assert(res_length<=4, "max value should be vv"); 1917 1918 // Compute arguments 1919 int arg_length = cse.compute_for_parameters(is_static != 0, in); 1920 assert(arg_length<=MAXARGSIZE, "too many locals"); 1921 1922 // Pop arguments 1923 for (int i = arg_length - 1; i >= 0; i--) ppop1(in[i]);// Do args in reverse order. 1924 1925 // Report results 1926 if (_report_result_for_send == true) { 1927 fill_stackmap_for_opcodes(_itr_send, vars(), stack(), _stack_top); 1928 _report_result_for_send = false; 1929 } 1930 1931 // Push return address 1932 ppush(out); 1933} 1934 1935// This is used to parse the signature for fields, since they are very simple... 1936CellTypeState *GenerateOopMap::sigchar_to_effect(char sigch, int bci, CellTypeState *out) { 1937 // Object and array 1938 if (sigch=='L' || sigch=='[') { 1939 out[0] = CellTypeState::make_line_ref(bci); 1940 out[1] = CellTypeState::bottom; 1941 return out; 1942 } 1943 if (sigch == 'J' || sigch == 'D' ) return vvCTS; // Long and Double 1944 if (sigch == 'V' ) return epsilonCTS; // Void 1945 return vCTS; // Otherwise 1946} 1947 1948long GenerateOopMap::_total_byte_count = 0; 1949elapsedTimer GenerateOopMap::_total_oopmap_time; 1950 1951// This function assumes "bcs" is at a "ret" instruction and that the vars 1952// state is valid for that instruction. Furthermore, the ret instruction 1953// must be the last instruction in "bb" (we store information about the 1954// "ret" in "bb"). 1955void GenerateOopMap::ret_jump_targets_do(BytecodeStream *bcs, jmpFct_t jmpFct, int varNo, int *data) { 1956 CellTypeState ra = vars()[varNo]; 1957 if (!ra.is_good_address()) { 1958 verify_error("ret returns from two jsr subroutines?"); 1959 return; 1960 } 1961 int target = ra.get_info(); 1962 1963 RetTableEntry* rtEnt = _rt.find_jsrs_for_target(target); 1964 int bci = bcs->bci(); 1965 for (int i = 0; i < rtEnt->nof_jsrs(); i++) { 1966 int target_bci = rtEnt->jsrs(i); 1967 // Make sure a jrtRet does not set the changed bit for dead basicblock. 1968 BasicBlock* jsr_bb = get_basic_block_containing(target_bci - 1); 1969 debug_only(BasicBlock* target_bb = &jsr_bb[1];) 1970 assert(target_bb == get_basic_block_at(target_bci), "wrong calc. of successor basicblock"); 1971 bool alive = jsr_bb->is_alive(); 1972 if (TraceNewOopMapGeneration) { 1973 tty->print("pc = %d, ret -> %d alive: %s\n", bci, target_bci, alive ? "true" : "false"); 1974 } 1975 if (alive) jmpFct(this, target_bci, data); 1976 } 1977} 1978 1979// 1980// Debug method 1981// 1982char* GenerateOopMap::state_vec_to_string(CellTypeState* vec, int len) { 1983#ifdef ASSERT 1984 int checklen = MAX3(_max_locals, _max_stack, _max_monitors) + 1; 1985 assert(len < checklen, "state_vec_buf overflow"); 1986#endif 1987 for (int i = 0; i < len; i++) _state_vec_buf[i] = vec[i].to_char(); 1988 _state_vec_buf[len] = 0; 1989 return _state_vec_buf; 1990} 1991 1992void GenerateOopMap::print_time() { 1993 tty->print_cr ("Accumulated oopmap times:"); 1994 tty->print_cr ("---------------------------"); 1995 tty->print_cr (" Total : %3.3f sec.", GenerateOopMap::_total_oopmap_time.seconds()); 1996 tty->print_cr (" (%3.0f bytecodes per sec) ", 1997 GenerateOopMap::_total_byte_count / GenerateOopMap::_total_oopmap_time.seconds()); 1998} 1999 2000// 2001// ============ Main Entry Point =========== 2002// 2003GenerateOopMap::GenerateOopMap(methodHandle method) { 2004 // We have to initialize all variables here, that can be queried directly 2005 _method = method; 2006 _max_locals=0; 2007 _init_vars = NULL; 2008 2009#ifndef PRODUCT 2010 // If we are doing a detailed trace, include the regular trace information. 2011 if (TraceNewOopMapGenerationDetailed) { 2012 TraceNewOopMapGeneration = true; 2013 } 2014#endif 2015} 2016 2017void GenerateOopMap::compute_map(TRAPS) { 2018#ifndef PRODUCT 2019 if (TimeOopMap2) { 2020 method()->print_short_name(tty); 2021 tty->print(" "); 2022 } 2023 if (TimeOopMap) { 2024 _total_byte_count += method()->code_size(); 2025 } 2026#endif 2027 TraceTime t_single("oopmap time", TimeOopMap2); 2028 TraceTime t_all(NULL, &_total_oopmap_time, TimeOopMap); 2029 2030 // Initialize values 2031 _got_error = false; 2032 _conflict = false; 2033 _max_locals = method()->max_locals(); 2034 _max_stack = method()->max_stack(); 2035 _has_exceptions = (method()->exception_table()->length() > 0); 2036 _nof_refval_conflicts = 0; 2037 _init_vars = new GrowableArray<intptr_t>(5); // There are seldom more than 5 init_vars 2038 _report_result = false; 2039 _report_result_for_send = false; 2040 _new_var_map = NULL; 2041 _ret_adr_tos = new GrowableArray<intptr_t>(5); // 5 seems like a good number; 2042 _did_rewriting = false; 2043 _did_relocation = false; 2044 2045 if (TraceNewOopMapGeneration) { 2046 tty->print("Method name: %s\n", method()->name()->as_C_string()); 2047 if (Verbose) { 2048 _method->print_codes(); 2049 tty->print_cr("Exception table:"); 2050 typeArrayOop excps = method()->exception_table(); 2051 for(int i = 0; i < excps->length(); i += 4) { 2052 tty->print_cr("[%d - %d] -> %d", excps->int_at(i + 0), excps->int_at(i + 1), excps->int_at(i + 2)); 2053 } 2054 } 2055 } 2056 2057 // if no code - do nothing 2058 // compiler needs info 2059 if (method()->code_size() == 0 || _max_locals + method()->max_stack() == 0) { 2060 fill_stackmap_prolog(0); 2061 fill_stackmap_epilog(); 2062 return; 2063 } 2064 // Step 1: Compute all jump targets and their return value 2065 if (!_got_error) 2066 _rt.compute_ret_table(_method); 2067 2068 // Step 2: Find all basic blocks and count GC points 2069 if (!_got_error) 2070 mark_bbheaders_and_count_gc_points(); 2071 2072 // Step 3: Calculate stack maps 2073 if (!_got_error) 2074 do_interpretation(); 2075 2076 // Step 4:Return results 2077 if (!_got_error && report_results()) 2078 report_result(); 2079 2080 if (_got_error) { 2081 THROW_HANDLE(_exception); 2082 } 2083} 2084 2085// Error handling methods 2086// These methods create an exception for the current thread which is thrown 2087// at the bottom of the call stack, when it returns to compute_map(). The 2088// _got_error flag controls execution. NOT TODO: The VM exception propagation 2089// mechanism using TRAPS/CHECKs could be used here instead but it would need 2090// to be added as a parameter to every function and checked for every call. 2091// The tons of extra code it would generate didn't seem worth the change. 2092// 2093void GenerateOopMap::error_work(const char *format, va_list ap) { 2094 _got_error = true; 2095 char msg_buffer[512]; 2096 vsnprintf(msg_buffer, sizeof(msg_buffer), format, ap); 2097 // Append method name 2098 char msg_buffer2[512]; 2099 jio_snprintf(msg_buffer2, sizeof(msg_buffer2), "%s in method %s", msg_buffer, method()->name()->as_C_string()); 2100 _exception = Exceptions::new_exception(Thread::current(), 2101 vmSymbols::java_lang_LinkageError(), msg_buffer2); 2102} 2103 2104void GenerateOopMap::report_error(const char *format, ...) { 2105 va_list ap; 2106 va_start(ap, format); 2107 error_work(format, ap); 2108} 2109 2110void GenerateOopMap::verify_error(const char *format, ...) { 2111 // We do not distinguish between different types of errors for verification 2112 // errors. Let the verifier give a better message. 2113 const char *msg = "Illegal class file encountered. Try running with -Xverify:all"; 2114 error_work(msg, NULL); 2115} 2116 2117// 2118// Report result opcodes 2119// 2120void GenerateOopMap::report_result() { 2121 2122 if (TraceNewOopMapGeneration) tty->print_cr("Report result pass"); 2123 2124 // We now want to report the result of the parse 2125 _report_result = true; 2126 2127 // Prolog code 2128 fill_stackmap_prolog(_gc_points); 2129 2130 // Mark everything changed, then do one interpretation pass. 2131 for (int i = 0; i<_bb_count; i++) { 2132 if (_basic_blocks[i].is_reachable()) { 2133 _basic_blocks[i].set_changed(true); 2134 interp_bb(&_basic_blocks[i]); 2135 } 2136 } 2137 2138 // Note: Since we are skipping dead-code when we are reporting results, then 2139 // the no. of encountered gc-points might be fewer than the previously number 2140 // we have counted. (dead-code is a pain - it should be removed before we get here) 2141 fill_stackmap_epilog(); 2142 2143 // Report initvars 2144 fill_init_vars(_init_vars); 2145 2146 _report_result = false; 2147} 2148 2149void GenerateOopMap::result_for_basicblock(int bci) { 2150 if (TraceNewOopMapGeneration) tty->print_cr("Report result pass for basicblock"); 2151 2152 // We now want to report the result of the parse 2153 _report_result = true; 2154 2155 // Find basicblock and report results 2156 BasicBlock* bb = get_basic_block_containing(bci); 2157 assert(bb->is_reachable(), "getting result from unreachable basicblock"); 2158 bb->set_changed(true); 2159 interp_bb(bb); 2160} 2161 2162// 2163// Conflict handling code 2164// 2165 2166void GenerateOopMap::record_refval_conflict(int varNo) { 2167 assert(varNo>=0 && varNo< _max_locals, "index out of range"); 2168 2169 if (TraceOopMapRewrites) { 2170 tty->print("### Conflict detected (local no: %d)\n", varNo); 2171 } 2172 2173 if (!_new_var_map) { 2174 _new_var_map = NEW_RESOURCE_ARRAY(int, _max_locals); 2175 for (int k = 0; k < _max_locals; k++) _new_var_map[k] = k; 2176 } 2177 2178 if ( _new_var_map[varNo] == varNo) { 2179 // Check if max. number of locals has been reached 2180 if (_max_locals + _nof_refval_conflicts >= MAX_LOCAL_VARS) { 2181 report_error("Rewriting exceeded local variable limit"); 2182 return; 2183 } 2184 _new_var_map[varNo] = _max_locals + _nof_refval_conflicts; 2185 _nof_refval_conflicts++; 2186 } 2187} 2188 2189void GenerateOopMap::rewrite_refval_conflicts() 2190{ 2191 // We can get here two ways: Either a rewrite conflict was detected, or 2192 // an uninitialize reference was detected. In the second case, we do not 2193 // do any rewriting, we just want to recompute the reference set with the 2194 // new information 2195 2196 int nof_conflicts = 0; // Used for debugging only 2197 2198 if ( _nof_refval_conflicts == 0 ) 2199 return; 2200 2201 // Check if rewrites are allowed in this parse. 2202 if (!allow_rewrites() && !IgnoreRewrites) { 2203 fatal("Rewriting method not allowed at this stage"); 2204 } 2205 2206 2207 // This following flag is to tempoary supress rewrites. The locals that might conflict will 2208 // all be set to contain values. This is UNSAFE - however, until the rewriting has been completely 2209 // tested it is nice to have. 2210 if (IgnoreRewrites) { 2211 if (Verbose) { 2212 tty->print("rewrites suppressed for local no. "); 2213 for (int l = 0; l < _max_locals; l++) { 2214 if (_new_var_map[l] != l) { 2215 tty->print("%d ", l); 2216 vars()[l] = CellTypeState::value; 2217 } 2218 } 2219 tty->cr(); 2220 } 2221 2222 // That was that... 2223 _new_var_map = NULL; 2224 _nof_refval_conflicts = 0; 2225 _conflict = false; 2226 2227 return; 2228 } 2229 2230 // Tracing flag 2231 _did_rewriting = true; 2232 2233 if (TraceOopMapRewrites) { 2234 tty->print_cr("ref/value conflict for method %s - bytecodes are getting rewritten", method()->name()->as_C_string()); 2235 method()->print(); 2236 method()->print_codes(); 2237 } 2238 2239 assert(_new_var_map!=NULL, "nothing to rewrite"); 2240 assert(_conflict==true, "We should not be here"); 2241 2242 compute_ret_adr_at_TOS(); 2243 if (!_got_error) { 2244 for (int k = 0; k < _max_locals && !_got_error; k++) { 2245 if (_new_var_map[k] != k) { 2246 if (TraceOopMapRewrites) { 2247 tty->print_cr("Rewriting: %d -> %d", k, _new_var_map[k]); 2248 } 2249 rewrite_refval_conflict(k, _new_var_map[k]); 2250 if (_got_error) return; 2251 nof_conflicts++; 2252 } 2253 } 2254 } 2255 2256 assert(nof_conflicts == _nof_refval_conflicts, "sanity check"); 2257 2258 // Adjust the number of locals 2259 method()->set_max_locals(_max_locals+_nof_refval_conflicts); 2260 _max_locals += _nof_refval_conflicts; 2261 2262 // That was that... 2263 _new_var_map = NULL; 2264 _nof_refval_conflicts = 0; 2265} 2266 2267void GenerateOopMap::rewrite_refval_conflict(int from, int to) { 2268 bool startOver; 2269 do { 2270 // Make sure that the BytecodeStream is constructed in the loop, since 2271 // during rewriting a new method oop is going to be used, and the next time 2272 // around we want to use that. 2273 BytecodeStream bcs(_method); 2274 startOver = false; 2275 2276 while( bcs.next() >=0 && !startOver && !_got_error) { 2277 startOver = rewrite_refval_conflict_inst(&bcs, from, to); 2278 } 2279 } while (startOver && !_got_error); 2280} 2281 2282/* If the current instruction is one that uses local variable "from" 2283 in a ref way, change it to use "to". There's a subtle reason why we 2284 renumber the ref uses and not the non-ref uses: non-ref uses may be 2285 2 slots wide (double, long) which would necessitate keeping track of 2286 whether we should add one or two variables to the method. If the change 2287 affected the width of some instruction, returns "TRUE"; otherwise, returns "FALSE". 2288 Another reason for moving ref's value is for solving (addr, ref) conflicts, which 2289 both uses aload/astore methods. 2290*/ 2291bool GenerateOopMap::rewrite_refval_conflict_inst(BytecodeStream *itr, int from, int to) { 2292 Bytecodes::Code bc = itr->code(); 2293 int index; 2294 int bci = itr->bci(); 2295 2296 if (is_aload(itr, &index) && index == from) { 2297 if (TraceOopMapRewrites) { 2298 tty->print_cr("Rewriting aload at bci: %d", bci); 2299 } 2300 return rewrite_load_or_store(itr, Bytecodes::_aload, Bytecodes::_aload_0, to); 2301 } 2302 2303 if (is_astore(itr, &index) && index == from) { 2304 if (!stack_top_holds_ret_addr(bci)) { 2305 if (TraceOopMapRewrites) { 2306 tty->print_cr("Rewriting astore at bci: %d", bci); 2307 } 2308 return rewrite_load_or_store(itr, Bytecodes::_astore, Bytecodes::_astore_0, to); 2309 } else { 2310 if (TraceOopMapRewrites) { 2311 tty->print_cr("Supress rewriting of astore at bci: %d", bci); 2312 } 2313 } 2314 } 2315 2316 return false; 2317} 2318 2319// The argument to this method is: 2320// bc : Current bytecode 2321// bcN : either _aload or _astore 2322// bc0 : either _aload_0 or _astore_0 2323bool GenerateOopMap::rewrite_load_or_store(BytecodeStream *bcs, Bytecodes::Code bcN, Bytecodes::Code bc0, unsigned int varNo) { 2324 assert(bcN == Bytecodes::_astore || bcN == Bytecodes::_aload, "wrong argument (bcN)"); 2325 assert(bc0 == Bytecodes::_astore_0 || bc0 == Bytecodes::_aload_0, "wrong argument (bc0)"); 2326 int ilen = Bytecodes::length_at(bcs->bcp()); 2327 int newIlen; 2328 2329 if (ilen == 4) { 2330 // Original instruction was wide; keep it wide for simplicity 2331 newIlen = 4; 2332 } else if (varNo < 4) 2333 newIlen = 1; 2334 else if (varNo >= 256) 2335 newIlen = 4; 2336 else 2337 newIlen = 2; 2338 2339 // If we need to relocate in order to patch the byte, we 2340 // do the patching in a temp. buffer, that is passed to the reloc. 2341 // The patching of the bytecode stream is then done by the Relocator. 2342 // This is neccesary, since relocating the instruction at a certain bci, might 2343 // also relocate that instruction, e.g., if a _goto before it gets widen to a _goto_w. 2344 // Hence, we do not know which bci to patch after relocation. 2345 2346 assert(newIlen <= 4, "sanity check"); 2347 u_char inst_buffer[4]; // Max. instruction size is 4. 2348 address bcp; 2349 2350 if (newIlen != ilen) { 2351 // Relocation needed do patching in temp. buffer 2352 bcp = (address)inst_buffer; 2353 } else { 2354 bcp = _method->bcp_from(bcs->bci()); 2355 } 2356 2357 // Patch either directly in methodOop or in temp. buffer 2358 if (newIlen == 1) { 2359 assert(varNo < 4, "varNo too large"); 2360 *bcp = bc0 + varNo; 2361 } else if (newIlen == 2) { 2362 assert(varNo < 256, "2-byte index needed!"); 2363 *(bcp + 0) = bcN; 2364 *(bcp + 1) = varNo; 2365 } else { 2366 assert(newIlen == 4, "Wrong instruction length"); 2367 *(bcp + 0) = Bytecodes::_wide; 2368 *(bcp + 1) = bcN; 2369 Bytes::put_Java_u2(bcp+2, varNo); 2370 } 2371 2372 if (newIlen != ilen) { 2373 expand_current_instr(bcs->bci(), ilen, newIlen, inst_buffer); 2374 } 2375 2376 2377 return (newIlen != ilen); 2378} 2379 2380class RelocCallback : public RelocatorListener { 2381 private: 2382 GenerateOopMap* _gom; 2383 public: 2384 RelocCallback(GenerateOopMap* gom) { _gom = gom; }; 2385 2386 // Callback method 2387 virtual void relocated(int bci, int delta, int new_code_length) { 2388 _gom->update_basic_blocks (bci, delta, new_code_length); 2389 _gom->update_ret_adr_at_TOS(bci, delta); 2390 _gom->_rt.update_ret_table (bci, delta); 2391 } 2392}; 2393 2394// Returns true if expanding was succesful. Otherwise, reports an error and 2395// returns false. 2396void GenerateOopMap::expand_current_instr(int bci, int ilen, int newIlen, u_char inst_buffer[]) { 2397 Thread *THREAD = Thread::current(); // Could really have TRAPS argument. 2398 RelocCallback rcb(this); 2399 Relocator rc(_method, &rcb); 2400 methodHandle m= rc.insert_space_at(bci, newIlen, inst_buffer, THREAD); 2401 if (m.is_null() || HAS_PENDING_EXCEPTION) { 2402 report_error("could not rewrite method - exception occurred or bytecode buffer overflow"); 2403 return; 2404 } 2405 2406 // Relocator returns a new method oop. 2407 _did_relocation = true; 2408 _method = m; 2409} 2410 2411 2412bool GenerateOopMap::is_astore(BytecodeStream *itr, int *index) { 2413 Bytecodes::Code bc = itr->code(); 2414 switch(bc) { 2415 case Bytecodes::_astore_0: 2416 case Bytecodes::_astore_1: 2417 case Bytecodes::_astore_2: 2418 case Bytecodes::_astore_3: 2419 *index = bc - Bytecodes::_astore_0; 2420 return true; 2421 case Bytecodes::_astore: 2422 *index = itr->get_index(); 2423 return true; 2424 } 2425 return false; 2426} 2427 2428bool GenerateOopMap::is_aload(BytecodeStream *itr, int *index) { 2429 Bytecodes::Code bc = itr->code(); 2430 switch(bc) { 2431 case Bytecodes::_aload_0: 2432 case Bytecodes::_aload_1: 2433 case Bytecodes::_aload_2: 2434 case Bytecodes::_aload_3: 2435 *index = bc - Bytecodes::_aload_0; 2436 return true; 2437 2438 case Bytecodes::_aload: 2439 *index = itr->get_index(); 2440 return true; 2441 } 2442 return false; 2443} 2444 2445 2446// Return true iff the top of the operand stack holds a return address at 2447// the current instruction 2448bool GenerateOopMap::stack_top_holds_ret_addr(int bci) { 2449 for(int i = 0; i < _ret_adr_tos->length(); i++) { 2450 if (_ret_adr_tos->at(i) == bci) 2451 return true; 2452 } 2453 2454 return false; 2455} 2456 2457void GenerateOopMap::compute_ret_adr_at_TOS() { 2458 assert(_ret_adr_tos != NULL, "must be initialized"); 2459 _ret_adr_tos->clear(); 2460 2461 for (int i = 0; i < bb_count(); i++) { 2462 BasicBlock* bb = &_basic_blocks[i]; 2463 2464 // Make sure to only check basicblocks that are reachable 2465 if (bb->is_reachable()) { 2466 2467 // For each Basic block we check all instructions 2468 BytecodeStream bcs(_method); 2469 bcs.set_interval(bb->_bci, next_bb_start_pc(bb)); 2470 2471 restore_state(bb); 2472 2473 while (bcs.next()>=0 && !_got_error) { 2474 // TDT: should this be is_good_address() ? 2475 if (_stack_top > 0 && stack()[_stack_top-1].is_address()) { 2476 _ret_adr_tos->append(bcs.bci()); 2477 if (TraceNewOopMapGeneration) { 2478 tty->print_cr("Ret_adr TOS at bci: %d", bcs.bci()); 2479 } 2480 } 2481 interp1(&bcs); 2482 } 2483 } 2484 } 2485} 2486 2487void GenerateOopMap::update_ret_adr_at_TOS(int bci, int delta) { 2488 for(int i = 0; i < _ret_adr_tos->length(); i++) { 2489 int v = _ret_adr_tos->at(i); 2490 if (v > bci) _ret_adr_tos->at_put(i, v + delta); 2491 } 2492} 2493 2494// =================================================================== 2495 2496#ifndef PRODUCT 2497int ResolveOopMapConflicts::_nof_invocations = 0; 2498int ResolveOopMapConflicts::_nof_rewrites = 0; 2499int ResolveOopMapConflicts::_nof_relocations = 0; 2500#endif 2501 2502methodHandle ResolveOopMapConflicts::do_potential_rewrite(TRAPS) { 2503 compute_map(CHECK_(methodHandle())); 2504 2505#ifndef PRODUCT 2506 // Tracking and statistics 2507 if (PrintRewrites) { 2508 _nof_invocations++; 2509 if (did_rewriting()) { 2510 _nof_rewrites++; 2511 if (did_relocation()) _nof_relocations++; 2512 tty->print("Method was rewritten %s: ", (did_relocation()) ? "and relocated" : ""); 2513 method()->print_value(); tty->cr(); 2514 tty->print_cr("Cand.: %d rewrts: %d (%d%%) reloc.: %d (%d%%)", 2515 _nof_invocations, 2516 _nof_rewrites, (_nof_rewrites * 100) / _nof_invocations, 2517 _nof_relocations, (_nof_relocations * 100) / _nof_invocations); 2518 } 2519 } 2520#endif 2521 return methodHandle(THREAD, method()); 2522} 2523