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