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