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