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