src/share/vm/runtime/relocator.cpp

Mon, 06 Aug 2012 15:54:45 -0400

author
kamg
date
Mon, 06 Aug 2012 15:54:45 -0400
changeset 3992
4ee06e614636
parent 3917
8150fa46d2ed
child 4037
da91efe96a93
permissions
-rw-r--r--

7116786: RFE: Detailed information on VerifyErrors
Summary: Provide additional detail in VerifyError messages
Reviewed-by: sspitsyn, acorn

     1 /*
     2  * Copyright (c) 1997, 2012, 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  */
    25 #include "precompiled.hpp"
    26 #include "classfile/stackMapTableFormat.hpp"
    27 #include "interpreter/bytecodes.hpp"
    28 #include "memory/oopFactory.hpp"
    29 #include "memory/universe.inline.hpp"
    30 #include "oops/oop.inline.hpp"
    31 #include "runtime/handles.inline.hpp"
    32 #include "runtime/relocator.hpp"
    34 #define MAX_METHOD_LENGTH  65535
    36 #define MAX_SHORT ((1 << 15) - 1)
    37 #define MIN_SHORT (- (1 << 15))
    39 // Encapsulates a code change request. There are 3 types.
    40 // General instruction, jump instruction, and table/lookup switches
    41 //
    42 class ChangeItem : public ResourceObj {
    43   int _bci;
    44  public:
    45    ChangeItem(int bci) { _bci = bci; }
    46    virtual bool handle_code_change(Relocator *r) = 0;
    48    // type info
    49    virtual bool is_widen()      { return false; }
    50    virtual bool is_jump_widen() { return false; }
    51    virtual bool is_switch_pad() { return false; }
    53    // accessors
    54    int bci()    { return _bci; }
    55    void relocate(int break_bci, int delta) { if (_bci > break_bci) { _bci += delta; } }
    57    virtual bool adjust(int bci, int delta) { return false; }
    59    // debug
    60    virtual void print() = 0;
    61 };
    63 class ChangeWiden : public ChangeItem {
    64   int              _new_ilen;    // New length of instruction at bci
    65   u_char*          _inst_buffer; // New bytecodes
    66  public:
    67   ChangeWiden(int bci, int new_ilen, u_char* inst_buffer) : ChangeItem(bci) {
    68     _new_ilen = new_ilen;
    69     _inst_buffer = inst_buffer;
    70   }
    72   // Callback to do instruction
    73   bool handle_code_change(Relocator *r) { return r->handle_widen(bci(), _new_ilen, _inst_buffer); };
    75   bool is_widen()              { return true; }
    77   void print()                 { tty->print_cr("ChangeWiden. bci: %d   New_ilen: %d", bci(), _new_ilen); }
    78 };
    80 class ChangeJumpWiden : public ChangeItem {
    81   int _delta;  // New length of instruction at bci
    82  public:
    83   ChangeJumpWiden(int bci, int delta) : ChangeItem(bci) { _delta = delta; }
    85   // Callback to do instruction
    86   bool handle_code_change(Relocator *r) { return r->handle_jump_widen(bci(), _delta); };
    88   bool is_jump_widen()         { return true; }
    90   // If the bci matches, adjust the delta in the change jump request.
    91   bool adjust(int jump_bci, int delta) {
    92     if (bci() == jump_bci) {
    93       if (_delta > 0)
    94         _delta += delta;
    95       else
    96         _delta -= delta;
    97       return true;
    98     }
    99     return false;
   100   }
   102   void print()                 { tty->print_cr("ChangeJumpWiden. bci: %d   Delta: %d", bci(), _delta); }
   103 };
   105 class ChangeSwitchPad : public ChangeItem {
   106   int  _padding;
   107   bool _is_lookup_switch;
   108  public:
   109    ChangeSwitchPad(int bci, int padding, bool is_lookup_switch) : ChangeItem(bci) {
   110      _padding = padding;
   111      _is_lookup_switch = is_lookup_switch;
   112    }
   114    // Callback to do instruction
   115    bool handle_code_change(Relocator *r) { return r->handle_switch_pad(bci(), _padding, _is_lookup_switch); };
   117    bool is_switch_pad()        { return true; }
   118    int  padding()              { return _padding;  }
   119    bool is_lookup_switch()     { return _is_lookup_switch; }
   121    void print()                { tty->print_cr("ChangeSwitchPad. bci: %d   Padding: %d  IsLookupSwitch: %d", bci(), _padding, _is_lookup_switch); }
   122 };
   124 //-----------------------------------------------------------------------------------------------------------
   125 // Relocator code
   127 Relocator::Relocator(methodHandle m, RelocatorListener* listener) {
   128   set_method(m);
   129   set_code_length(method()->code_size());
   130   set_code_array(NULL);
   131   // Allocate code array and copy bytecodes
   132   if (!expand_code_array(0)) {
   133     // Should have at least MAX_METHOD_LENGTH available or the verifier
   134     // would have failed.
   135     ShouldNotReachHere();
   136   }
   137   set_compressed_line_number_table(NULL);
   138   set_compressed_line_number_table_size(0);
   139   _listener = listener;
   140 }
   142 // size is the new size of the instruction at bci. Hence, if size is less than the current
   143 // instruction sice, we will shrink the code.
   144 methodHandle Relocator::insert_space_at(int bci, int size, u_char inst_buffer[], TRAPS) {
   145   _changes = new GrowableArray<ChangeItem*> (10);
   146   _changes->push(new ChangeWiden(bci, size, inst_buffer));
   148   if (TraceRelocator) {
   149     tty->print_cr("Space at: %d Size: %d", bci, size);
   150     _method->print();
   151     _method->print_codes();
   152     tty->print_cr("-------------------------------------------------");
   153   }
   155   if (!handle_code_changes()) return methodHandle();
   157     // Construct the new method
   158   methodHandle new_method = methodOopDesc::clone_with_new_data(method(),
   159                               code_array(), code_length(),
   160                               compressed_line_number_table(),
   161                               compressed_line_number_table_size(),
   162                               CHECK_(methodHandle()));
   163     set_method(new_method);
   165   if (TraceRelocator) {
   166     tty->print_cr("-------------------------------------------------");
   167     tty->print_cr("new method");
   168     _method->print_codes();
   169   }
   171   return new_method;
   172 }
   175 bool Relocator::handle_code_changes() {
   176   assert(_changes != NULL, "changes vector must be initialized");
   178   while (!_changes->is_empty()) {
   179     // Inv: everything is aligned.
   180     ChangeItem* ci = _changes->first();
   182     if (TraceRelocator) {
   183       ci->print();
   184     }
   186     // Execute operation
   187     if (!ci->handle_code_change(this)) return false;
   189     // Shuffel items up
   190     for (int index = 1; index < _changes->length(); index++) {
   191       _changes->at_put(index-1, _changes->at(index));
   192     }
   193     _changes->pop();
   194   }
   195   return true;
   196 }
   199 bool Relocator::is_opcode_lookupswitch(Bytecodes::Code bc) {
   200   switch (bc) {
   201     case Bytecodes::_tableswitch:       return false;
   202     case Bytecodes::_lookupswitch:                   // not rewritten on ia64
   203     case Bytecodes::_fast_linearswitch:              // rewritten _lookupswitch
   204     case Bytecodes::_fast_binaryswitch: return true; // rewritten _lookupswitch
   205     default: ShouldNotReachHere();
   206   }
   207   return true; // dummy
   208 }
   210 // We need a special instruction size method, since lookupswitches and tableswitches might not be
   211 // properly alligned during relocation
   212 int Relocator::rc_instr_len(int bci) {
   213   Bytecodes::Code bc= code_at(bci);
   214   switch (bc) {
   215     // In the case of switch instructions, see if we have the original
   216     // padding recorded.
   217     case Bytecodes::_tableswitch:
   218     case Bytecodes::_lookupswitch:
   219     case Bytecodes::_fast_linearswitch:
   220     case Bytecodes::_fast_binaryswitch:
   221     {
   222       int pad = get_orig_switch_pad(bci, is_opcode_lookupswitch(bc));
   223       if (pad == -1) {
   224         return instruction_length_at(bci);
   225       }
   226       // Otherwise, depends on the switch type.
   227       switch (bc) {
   228         case Bytecodes::_tableswitch: {
   229           int lo = int_at(bci + 1 + pad + 4 * 1);
   230           int hi = int_at(bci + 1 + pad + 4 * 2);
   231           int n = hi - lo + 1;
   232           return 1 + pad + 4*(3 + n);
   233         }
   234         case Bytecodes::_lookupswitch:
   235         case Bytecodes::_fast_linearswitch:
   236         case Bytecodes::_fast_binaryswitch: {
   237           int npairs = int_at(bci + 1 + pad + 4 * 1);
   238           return 1 + pad + 4*(2 + 2*npairs);
   239         }
   240         default:
   241           ShouldNotReachHere();
   242       }
   243     }
   244   }
   245   return instruction_length_at(bci);
   246 }
   248 // If a change item is recorded for "pc", with type "ct", returns the
   249 // associated padding, else -1.
   250 int Relocator::get_orig_switch_pad(int bci, bool is_lookup_switch) {
   251   for (int k = 0; k < _changes->length(); k++) {
   252     ChangeItem* ci = _changes->at(k);
   253     if (ci->is_switch_pad()) {
   254       ChangeSwitchPad* csp = (ChangeSwitchPad*)ci;
   255       if (csp->is_lookup_switch() == is_lookup_switch && csp->bci() == bci) {
   256         return csp->padding();
   257       }
   258     }
   259   }
   260   return -1;
   261 }
   264 // Push a ChangeJumpWiden if it doesn't already exist on the work queue,
   265 // otherwise adjust the item already there by delta.  The calculation for
   266 // new_delta is wrong for this because it uses the offset stored in the
   267 // code stream itself which wasn't fixed when item was pushed on the work queue.
   268 void Relocator::push_jump_widen(int bci, int delta, int new_delta) {
   269   for (int j = 0; j < _changes->length(); j++) {
   270     ChangeItem* ci = _changes->at(j);
   271     if (ci->adjust(bci, delta)) return;
   272   }
   273   _changes->push(new ChangeJumpWiden(bci, new_delta));
   274 }
   277 // The current instruction of "c" is a jump; one of its offset starts
   278 // at "offset" and is a short if "isShort" is "TRUE",
   279 // and an integer otherwise.  If the jump crosses "breakPC", change
   280 // the span of the jump by "delta".
   281 void Relocator::change_jump(int bci, int offset, bool is_short, int break_bci, int delta) {
   282   int bci_delta = (is_short) ? short_at(offset) : int_at(offset);
   283   int targ = bci + bci_delta;
   285   if ((bci <= break_bci && targ >  break_bci) ||
   286       (bci >  break_bci && targ <= break_bci)) {
   287     int new_delta;
   288     if (bci_delta > 0)
   289       new_delta = bci_delta + delta;
   290     else
   291       new_delta = bci_delta - delta;
   293     if (is_short && ((new_delta > MAX_SHORT) || new_delta < MIN_SHORT)) {
   294       push_jump_widen(bci, delta, new_delta);
   295     } else if (is_short) {
   296       short_at_put(offset, new_delta);
   297     } else {
   298       int_at_put(offset, new_delta);
   299     }
   300   }
   301 }
   304 // Changes all jumps crossing "break_bci" by "delta".  May enqueue things
   305 // on "rc->changes"
   306 void Relocator::change_jumps(int break_bci, int delta) {
   307   int bci = 0;
   308   Bytecodes::Code bc;
   309   // Now, adjust any affected instructions.
   310   while (bci < code_length()) {
   311     switch (bc= code_at(bci)) {
   312       case Bytecodes::_ifeq:
   313       case Bytecodes::_ifne:
   314       case Bytecodes::_iflt:
   315       case Bytecodes::_ifge:
   316       case Bytecodes::_ifgt:
   317       case Bytecodes::_ifle:
   318       case Bytecodes::_if_icmpeq:
   319       case Bytecodes::_if_icmpne:
   320       case Bytecodes::_if_icmplt:
   321       case Bytecodes::_if_icmpge:
   322       case Bytecodes::_if_icmpgt:
   323       case Bytecodes::_if_icmple:
   324       case Bytecodes::_if_acmpeq:
   325       case Bytecodes::_if_acmpne:
   326       case Bytecodes::_ifnull:
   327       case Bytecodes::_ifnonnull:
   328       case Bytecodes::_goto:
   329       case Bytecodes::_jsr:
   330         change_jump(bci, bci+1, true, break_bci, delta);
   331         break;
   332       case Bytecodes::_goto_w:
   333       case Bytecodes::_jsr_w:
   334         change_jump(bci, bci+1, false, break_bci, delta);
   335         break;
   336       case Bytecodes::_tableswitch:
   337       case Bytecodes::_lookupswitch:
   338       case Bytecodes::_fast_linearswitch:
   339       case Bytecodes::_fast_binaryswitch: {
   340         int recPad = get_orig_switch_pad(bci, (bc != Bytecodes::_tableswitch));
   341         int oldPad = (recPad != -1) ? recPad : align(bci+1) - (bci+1);
   342         if (bci > break_bci) {
   343           int new_bci = bci + delta;
   344           int newPad = align(new_bci+1) - (new_bci+1);
   345           // Do we need to check the padding?
   346           if (newPad != oldPad) {
   347             if (recPad == -1) {
   348               _changes->push(new ChangeSwitchPad(bci, oldPad, (bc != Bytecodes::_tableswitch)));
   349             }
   350           }
   351         }
   353         // Then the rest, which depend on the kind of switch.
   354         switch (bc) {
   355           case Bytecodes::_tableswitch: {
   356             change_jump(bci, bci +1 + oldPad, false, break_bci, delta);
   357             // We cannot use the Bytecode_tableswitch abstraction, since the padding might not be correct.
   358             int lo = int_at(bci + 1 + oldPad + 4 * 1);
   359             int hi = int_at(bci + 1 + oldPad + 4 * 2);
   360             int n = hi - lo + 1;
   361             for (int k = 0; k < n; k++) {
   362               change_jump(bci, bci +1 + oldPad + 4*(k+3), false, break_bci, delta);
   363             }
   364             // Special next-bci calculation here...
   365             bci += 1 + oldPad + (n+3)*4;
   366             continue;
   367           }
   368           case Bytecodes::_lookupswitch:
   369           case Bytecodes::_fast_linearswitch:
   370           case Bytecodes::_fast_binaryswitch: {
   371             change_jump(bci, bci +1 + oldPad, false, break_bci, delta);
   372             // We cannot use the Bytecode_lookupswitch abstraction, since the padding might not be correct.
   373             int npairs = int_at(bci + 1 + oldPad + 4 * 1);
   374             for (int k = 0; k < npairs; k++) {
   375               change_jump(bci, bci + 1 + oldPad + 4*(2 + 2*k + 1), false, break_bci, delta);
   376             }
   377             /* Special next-bci calculation here... */
   378             bci += 1 + oldPad + (2 + (npairs*2))*4;
   379             continue;
   380           }
   381           default:
   382             ShouldNotReachHere();
   383         }
   384       }
   385       default:
   386         break;
   387     }
   388     bci += rc_instr_len(bci);
   389   }
   390 }
   392 // The width of instruction at "pc" is changing by "delta".  Adjust the
   393 // exception table, if any, of "rc->mb".
   394 void Relocator::adjust_exception_table(int bci, int delta) {
   395   ExceptionTable table(_method());
   396   for (int index = 0; index < table.length(); index ++) {
   397     if (table.start_pc(index) > bci) {
   398       table.set_start_pc(index, table.start_pc(index) + delta);
   399       table.set_end_pc(index, table.end_pc(index) + delta);
   400     } else if (bci < table.end_pc(index)) {
   401       table.set_end_pc(index, table.end_pc(index) + delta);
   402     }
   403     if (table.handler_pc(index) > bci)
   404       table.set_handler_pc(index, table.handler_pc(index) + delta);
   405   }
   406 }
   409 // The width of instruction at "bci" is changing by "delta".  Adjust the line number table.
   410 void Relocator::adjust_line_no_table(int bci, int delta) {
   411   if (method()->has_linenumber_table()) {
   412     CompressedLineNumberReadStream  reader(method()->compressed_linenumber_table());
   413     CompressedLineNumberWriteStream writer(64);  // plenty big for most line number tables
   414     while (reader.read_pair()) {
   415       int adjustment = (reader.bci() > bci) ? delta : 0;
   416       writer.write_pair(reader.bci() + adjustment, reader.line());
   417     }
   418     writer.write_terminator();
   419     set_compressed_line_number_table(writer.buffer());
   420     set_compressed_line_number_table_size(writer.position());
   421   }
   422 }
   425 // The width of instruction at "bci" is changing by "delta".  Adjust the local variable table.
   426 void Relocator::adjust_local_var_table(int bci, int delta) {
   427   int localvariable_table_length = method()->localvariable_table_length();
   428   if (localvariable_table_length > 0) {
   429     LocalVariableTableElement* table = method()->localvariable_table_start();
   430     for (int i = 0; i < localvariable_table_length; i++) {
   431       u2 current_bci = table[i].start_bci;
   432       if (current_bci > bci) {
   433         table[i].start_bci = current_bci + delta;
   434       } else {
   435         u2 current_length = table[i].length;
   436         if (current_bci + current_length > bci) {
   437           table[i].length = current_length + delta;
   438         }
   439       }
   440     }
   441   }
   442 }
   444 // Create a new array, copying the src array but adding a hole at
   445 // the specified location
   446 static typeArrayOop insert_hole_at(
   447     size_t where, int hole_sz, typeArrayOop src) {
   448   Thread* THREAD = Thread::current();
   449   Handle src_hnd(THREAD, src);
   450   typeArrayOop dst =
   451       oopFactory::new_permanent_byteArray(src->length() + hole_sz, CHECK_NULL);
   452   src = (typeArrayOop)src_hnd();
   454   address src_addr = (address)src->byte_at_addr(0);
   455   address dst_addr = (address)dst->byte_at_addr(0);
   457   memcpy(dst_addr, src_addr, where);
   458   memcpy(dst_addr + where + hole_sz,
   459          src_addr + where, src->length() - where);
   460   return dst;
   461 }
   463 // The width of instruction at "bci" is changing by "delta".  Adjust the stack
   464 // map frames.
   465 void Relocator::adjust_stack_map_table(int bci, int delta) {
   466   if (method()->has_stackmap_table()) {
   467     typeArrayOop data = method()->stackmap_data();
   468     // The data in the array is a classfile representation of the stackmap table
   469     stack_map_table* sm_table =
   470         stack_map_table::at((address)data->byte_at_addr(0));
   472     int count = sm_table->number_of_entries();
   473     stack_map_frame* frame = sm_table->entries();
   474     int bci_iter = -1;
   475     bool offset_adjusted = false; // only need to adjust one offset
   477     for (int i = 0; i < count; ++i) {
   478       int offset_delta = frame->offset_delta();
   479       bci_iter += offset_delta;
   481       if (!offset_adjusted && bci_iter > bci) {
   482         int new_offset_delta = offset_delta + delta;
   484         if (frame->is_valid_offset(new_offset_delta)) {
   485           frame->set_offset_delta(new_offset_delta);
   486         } else {
   487           assert(frame->is_same_frame() ||
   488                  frame->is_same_locals_1_stack_item_frame(),
   489                  "Frame must be one of the compressed forms");
   490           // The new delta exceeds the capacity of the 'same_frame' or
   491           // 'same_frame_1_stack_item_frame' frame types.  We need to
   492           // convert these frames to the extended versions, but the extended
   493           // version is bigger and requires more room.  So we allocate a
   494           // new array and copy the data, being sure to leave u2-sized hole
   495           // right after the 'frame_type' for the new offset field.
   496           //
   497           // We can safely ignore the reverse situation as a small delta
   498           // can still be used in an extended version of the frame.
   500           size_t frame_offset = (address)frame - (address)data->byte_at_addr(0);
   502           data = insert_hole_at(frame_offset + 1, 2, data);
   503           if (data == NULL) {
   504             return; // out-of-memory?
   505           }
   507           address frame_addr = (address)(data->byte_at_addr(0) + frame_offset);
   508           frame = stack_map_frame::at(frame_addr);
   511           // Now convert the frames in place
   512           if (frame->is_same_frame()) {
   513             same_frame_extended::create_at(frame_addr, new_offset_delta);
   514           } else {
   515             same_locals_1_stack_item_extended::create_at(
   516               frame_addr, new_offset_delta, NULL);
   517             // the verification_info_type should already be at the right spot
   518           }
   519         }
   520         offset_adjusted = true; // needs to be done only once, since subsequent
   521                                 // values are offsets from the current
   522       }
   524       // The stack map frame may contain verification types, if so we need to
   525       // check and update any Uninitialized type's bci (no matter where it is).
   526       int number_of_types = frame->number_of_types();
   527       verification_type_info* types = frame->types();
   529       for (int i = 0; i < number_of_types; ++i) {
   530         if (types->is_uninitialized() && types->bci() > bci) {
   531           types->set_bci(types->bci() + delta);
   532         }
   533         types = types->next();
   534       }
   536       // Full frame has stack values too
   537       full_frame* ff = frame->as_full_frame();
   538       if (ff != NULL) {
   539         address eol = (address)types;
   540         number_of_types = ff->stack_slots(eol);
   541         types = ff->stack(eol);
   542         for (int i = 0; i < number_of_types; ++i) {
   543           if (types->is_uninitialized() && types->bci() > bci) {
   544             types->set_bci(types->bci() + delta);
   545           }
   546           types = types->next();
   547         }
   548       }
   550       frame = frame->next();
   551     }
   553     method()->set_stackmap_data(data); // in case it has changed
   554   }
   555 }
   558 bool Relocator::expand_code_array(int delta) {
   559   int length = MAX2(code_length() + delta, code_length() * (100+code_slop_pct()) / 100);
   561   if (length > MAX_METHOD_LENGTH) {
   562     if (delta == 0 && code_length() <= MAX_METHOD_LENGTH) {
   563       length = MAX_METHOD_LENGTH;
   564     } else {
   565       return false;
   566     }
   567   }
   569   unsigned char* new_code_array = NEW_RESOURCE_ARRAY(unsigned char, length);
   570   if (!new_code_array) return false;
   572   // Expanding current array
   573   if (code_array() != NULL) {
   574     memcpy(new_code_array, code_array(), code_length());
   575   } else {
   576     // Initial copy. Copy directly from methodOop
   577     memcpy(new_code_array, method()->code_base(), code_length());
   578   }
   580   set_code_array(new_code_array);
   581   set_code_array_length(length);
   583   return true;
   584 }
   587 // The instruction at "bci", whose size is "ilen", is changing size by
   588 // "delta".  Reallocate, move code, recalculate jumps, and enqueue
   589 // change items as necessary.
   590 bool Relocator::relocate_code(int bci, int ilen, int delta) {
   591   int next_bci = bci + ilen;
   592   if (delta > 0 && code_length() + delta > code_array_length())  {
   593     // Expand allocated code space, if necessary.
   594     if (!expand_code_array(delta)) {
   595           return false;
   596     }
   597   }
   599   // We require 4-byte alignment of code arrays.
   600   assert(((intptr_t)code_array() & 3) == 0, "check code alignment");
   601   // Change jumps before doing the copying; this routine requires aligned switches.
   602   change_jumps(bci, delta);
   604   // In case we have shrunken a tableswitch/lookupswitch statement, we store the last
   605   // bytes that get overwritten. We have to copy the bytes after the change_jumps method
   606   // has been called, since it is likly to update last offset in a tableswitch/lookupswitch
   607   if (delta < 0) {
   608     assert(delta>=-3, "we cannot overwrite more than 3 bytes");
   609     memcpy(_overwrite, addr_at(bci + ilen + delta), -delta);
   610   }
   612   memmove(addr_at(next_bci + delta), addr_at(next_bci), code_length() - next_bci);
   613   set_code_length(code_length() + delta);
   614   // Also adjust exception tables...
   615   adjust_exception_table(bci, delta);
   616   // Line number tables...
   617   adjust_line_no_table(bci, delta);
   618   // And local variable table...
   619   adjust_local_var_table(bci, delta);
   621   // Adjust stack maps
   622   adjust_stack_map_table(bci, delta);
   624   // Relocate the pending change stack...
   625   for (int j = 0; j < _changes->length(); j++) {
   626     ChangeItem* ci = _changes->at(j);
   627     ci->relocate(bci, delta);
   628   }
   630   // Notify any listeners about code relocation
   631   notify(bci, delta, code_length());
   633   return true;
   634 }
   636 // relocate a general instruction. Called by ChangeWiden class
   637 bool Relocator::handle_widen(int bci, int new_ilen, u_char inst_buffer[]) {
   638   int ilen = rc_instr_len(bci);
   639   if (!relocate_code(bci, ilen, new_ilen - ilen))
   640     return false;
   642   // Insert new bytecode(s)
   643   for(int k = 0; k < new_ilen; k++) {
   644     code_at_put(bci + k, (Bytecodes::Code)inst_buffer[k]);
   645   }
   647   return true;
   648 }
   650 // handle jump_widen instruction. Called be ChangeJumpWiden class
   651 bool Relocator::handle_jump_widen(int bci, int delta) {
   652   int ilen = rc_instr_len(bci);
   654   Bytecodes::Code bc = code_at(bci);
   655   switch (bc) {
   656     case Bytecodes::_ifeq:
   657     case Bytecodes::_ifne:
   658     case Bytecodes::_iflt:
   659     case Bytecodes::_ifge:
   660     case Bytecodes::_ifgt:
   661     case Bytecodes::_ifle:
   662     case Bytecodes::_if_icmpeq:
   663     case Bytecodes::_if_icmpne:
   664     case Bytecodes::_if_icmplt:
   665     case Bytecodes::_if_icmpge:
   666     case Bytecodes::_if_icmpgt:
   667     case Bytecodes::_if_icmple:
   668     case Bytecodes::_if_acmpeq:
   669     case Bytecodes::_if_acmpne:
   670     case Bytecodes::_ifnull:
   671     case Bytecodes::_ifnonnull: {
   672       const int goto_length   = Bytecodes::length_for(Bytecodes::_goto);
   674       // If 'if' points to the next bytecode after goto, it's already handled.
   675       // it shouldn't be.
   676       assert (short_at(bci+1) != ilen+goto_length, "if relocation already handled");
   677       assert(ilen == 3, "check length");
   679       // Convert to 0 if <cond> goto 6
   680       //            3 _goto 11
   681       //            6 _goto_w <wide delta offset>
   682       //            11 <else code>
   683       const int goto_w_length = Bytecodes::length_for(Bytecodes::_goto_w);
   684       const int add_bci = goto_length + goto_w_length;
   686       if (!relocate_code(bci, 3, /*delta*/add_bci)) return false;
   688       // if bytecode points to goto_w instruction
   689       short_at_put(bci + 1, ilen + goto_length);
   691       int cbci = bci + ilen;
   692       // goto around
   693       code_at_put(cbci, Bytecodes::_goto);
   694       short_at_put(cbci + 1, add_bci);
   695       // goto_w <wide delta>
   696       cbci = cbci + goto_length;
   697       code_at_put(cbci, Bytecodes::_goto_w);
   698       if (delta > 0) {
   699         delta += 2;                 // goto_w is 2 bytes more than "if" code
   700       } else {
   701         delta -= ilen+goto_length;  // branch starts at goto_w offset
   702       }
   703       int_at_put(cbci + 1, delta);
   704       break;
   706       }
   707     case Bytecodes::_goto:
   708     case Bytecodes::_jsr:
   709       assert(ilen == 3, "check length");
   711       if (!relocate_code(bci, 3, 2)) return false;
   712       if (bc == Bytecodes::_goto)
   713         code_at_put(bci, Bytecodes::_goto_w);
   714       else
   715         code_at_put(bci, Bytecodes::_jsr_w);
   717       // If it's a forward jump, add 2 for the widening.
   718       if (delta > 0) delta += 2;
   719       int_at_put(bci + 1, delta);
   720       break;
   722     default: ShouldNotReachHere();
   723   }
   725   return true;
   726 }
   728 // handle lookup/table switch instructions.  Called be ChangeSwitchPad class
   729 bool Relocator::handle_switch_pad(int bci, int old_pad, bool is_lookup_switch) {
   730   int ilen = rc_instr_len(bci);
   731   int new_pad = align(bci+1) - (bci+1);
   732   int pad_delta = new_pad - old_pad;
   733   if (pad_delta != 0) {
   734     int len;
   735     if (!is_lookup_switch) {
   736       int low  = int_at(bci+1+old_pad+4);
   737       int high = int_at(bci+1+old_pad+8);
   738       len = high-low+1 + 3; // 3 for default, hi, lo.
   739     } else {
   740       int npairs = int_at(bci+1+old_pad+4);
   741       len = npairs*2 + 2; // 2 for default, npairs.
   742     }
   743     // Because "relocateCode" does a "changeJumps" loop,
   744     // which parses instructions to determine their length,
   745     // we need to call that before messing with the current
   746     // instruction.  Since it may also overwrite the current
   747     // instruction when moving down, remember the possibly
   748     // overwritten part.
   750     // Move the code following the instruction...
   751     if (!relocate_code(bci, ilen, pad_delta)) return false;
   753     if (pad_delta < 0) {
   754       // Move the shrunken instruction down.
   755       memmove(addr_at(bci + 1 + new_pad),
   756               addr_at(bci + 1 + old_pad),
   757               len * 4 + pad_delta);
   758       memmove(addr_at(bci + 1 + new_pad + len*4 + pad_delta),
   759               _overwrite, -pad_delta);
   760     } else {
   761       assert(pad_delta > 0, "check");
   762       // Move the expanded instruction up.
   763       memmove(addr_at(bci +1 + new_pad),
   764               addr_at(bci +1 + old_pad),
   765               len * 4);
   766       memset(addr_at(bci + 1), 0, new_pad); // pad must be 0
   767     }
   768   }
   769   return true;
   770 }

mercurial