src/hotspot/share/runtime/relocator.cpp
author erikj
Tue, 12 Sep 2017 19:03:39 +0200
changeset 47216 71c04702a3d5
parent 46630 hotspot/src/share/vm/runtime/relocator.cpp@75aa3e39d02c
child 49359 59f6547e151f
permissions -rw-r--r--
8187443: Forest Consolidation: Move files to unified layout Reviewed-by: darcy, ihse
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
     1
/*
46630
75aa3e39d02c 8182299: Enable disabled clang warnings, build on OSX 10 + Xcode 8
jwilhelm
parents: 38658
diff changeset
     2
 * Copyright (c) 1997, 2017, Oracle and/or its affiliates. All rights reserved.
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
489c9b5090e2 Initial load
duke
parents:
diff changeset
     4
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
489c9b5090e2 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
489c9b5090e2 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
489c9b5090e2 Initial load
duke
parents:
diff changeset
     8
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
489c9b5090e2 Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
489c9b5090e2 Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
489c9b5090e2 Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
489c9b5090e2 Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
489c9b5090e2 Initial load
duke
parents:
diff changeset
    14
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
489c9b5090e2 Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
489c9b5090e2 Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    18
 *
5547
f4b087cbb361 6941466: Oracle rebranding changes for Hotspot repositories
trims
parents: 1
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
f4b087cbb361 6941466: Oracle rebranding changes for Hotspot repositories
trims
parents: 1
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
f4b087cbb361 6941466: Oracle rebranding changes for Hotspot repositories
trims
parents: 1
diff changeset
    21
 * questions.
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    22
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
    23
 */
489c9b5090e2 Initial load
duke
parents:
diff changeset
    24
7397
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 6974
diff changeset
    25
#include "precompiled.hpp"
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 6974
diff changeset
    26
#include "classfile/stackMapTableFormat.hpp"
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 6974
diff changeset
    27
#include "interpreter/bytecodes.hpp"
13728
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents: 13476
diff changeset
    28
#include "memory/metadataFactory.hpp"
7397
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 6974
diff changeset
    29
#include "memory/oopFactory.hpp"
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 6974
diff changeset
    30
#include "memory/universe.inline.hpp"
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 6974
diff changeset
    31
#include "oops/oop.inline.hpp"
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 6974
diff changeset
    32
#include "runtime/handles.inline.hpp"
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 6974
diff changeset
    33
#include "runtime/relocator.hpp"
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    34
489c9b5090e2 Initial load
duke
parents:
diff changeset
    35
#define MAX_METHOD_LENGTH  65535
489c9b5090e2 Initial load
duke
parents:
diff changeset
    36
489c9b5090e2 Initial load
duke
parents:
diff changeset
    37
#define MAX_SHORT ((1 << 15) - 1)
489c9b5090e2 Initial load
duke
parents:
diff changeset
    38
#define MIN_SHORT (- (1 << 15))
489c9b5090e2 Initial load
duke
parents:
diff changeset
    39
489c9b5090e2 Initial load
duke
parents:
diff changeset
    40
// Encapsulates a code change request. There are 3 types.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    41
// General instruction, jump instruction, and table/lookup switches
489c9b5090e2 Initial load
duke
parents:
diff changeset
    42
//
489c9b5090e2 Initial load
duke
parents:
diff changeset
    43
class ChangeItem : public ResourceObj {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    44
  int _bci;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    45
 public:
489c9b5090e2 Initial load
duke
parents:
diff changeset
    46
   ChangeItem(int bci) { _bci = bci; }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    47
   virtual bool handle_code_change(Relocator *r) = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    48
489c9b5090e2 Initial load
duke
parents:
diff changeset
    49
   // type info
489c9b5090e2 Initial load
duke
parents:
diff changeset
    50
   virtual bool is_widen()      { return false; }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    51
   virtual bool is_jump_widen() { return false; }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    52
   virtual bool is_switch_pad() { return false; }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    53
489c9b5090e2 Initial load
duke
parents:
diff changeset
    54
   // accessors
489c9b5090e2 Initial load
duke
parents:
diff changeset
    55
   int bci()    { return _bci; }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    56
   void relocate(int break_bci, int delta) { if (_bci > break_bci) { _bci += delta; } }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    57
489c9b5090e2 Initial load
duke
parents:
diff changeset
    58
   virtual bool adjust(int bci, int delta) { return false; }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    59
489c9b5090e2 Initial load
duke
parents:
diff changeset
    60
   // debug
489c9b5090e2 Initial load
duke
parents:
diff changeset
    61
   virtual void print() = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    62
};
489c9b5090e2 Initial load
duke
parents:
diff changeset
    63
489c9b5090e2 Initial load
duke
parents:
diff changeset
    64
class ChangeWiden : public ChangeItem {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    65
  int              _new_ilen;    // New length of instruction at bci
489c9b5090e2 Initial load
duke
parents:
diff changeset
    66
  u_char*          _inst_buffer; // New bytecodes
489c9b5090e2 Initial load
duke
parents:
diff changeset
    67
 public:
489c9b5090e2 Initial load
duke
parents:
diff changeset
    68
  ChangeWiden(int bci, int new_ilen, u_char* inst_buffer) : ChangeItem(bci) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    69
    _new_ilen = new_ilen;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    70
    _inst_buffer = inst_buffer;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    71
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    72
489c9b5090e2 Initial load
duke
parents:
diff changeset
    73
  // Callback to do instruction
489c9b5090e2 Initial load
duke
parents:
diff changeset
    74
  bool handle_code_change(Relocator *r) { return r->handle_widen(bci(), _new_ilen, _inst_buffer); };
489c9b5090e2 Initial load
duke
parents:
diff changeset
    75
489c9b5090e2 Initial load
duke
parents:
diff changeset
    76
  bool is_widen()              { return true; }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    77
489c9b5090e2 Initial load
duke
parents:
diff changeset
    78
  void print()                 { tty->print_cr("ChangeWiden. bci: %d   New_ilen: %d", bci(), _new_ilen); }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    79
};
489c9b5090e2 Initial load
duke
parents:
diff changeset
    80
489c9b5090e2 Initial load
duke
parents:
diff changeset
    81
class ChangeJumpWiden : public ChangeItem {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    82
  int _delta;  // New length of instruction at bci
489c9b5090e2 Initial load
duke
parents:
diff changeset
    83
 public:
489c9b5090e2 Initial load
duke
parents:
diff changeset
    84
  ChangeJumpWiden(int bci, int delta) : ChangeItem(bci) { _delta = delta; }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    85
489c9b5090e2 Initial load
duke
parents:
diff changeset
    86
  // Callback to do instruction
489c9b5090e2 Initial load
duke
parents:
diff changeset
    87
  bool handle_code_change(Relocator *r) { return r->handle_jump_widen(bci(), _delta); };
489c9b5090e2 Initial load
duke
parents:
diff changeset
    88
489c9b5090e2 Initial load
duke
parents:
diff changeset
    89
  bool is_jump_widen()         { return true; }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    90
489c9b5090e2 Initial load
duke
parents:
diff changeset
    91
  // If the bci matches, adjust the delta in the change jump request.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    92
  bool adjust(int jump_bci, int delta) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    93
    if (bci() == jump_bci) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    94
      if (_delta > 0)
489c9b5090e2 Initial load
duke
parents:
diff changeset
    95
        _delta += delta;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    96
      else
489c9b5090e2 Initial load
duke
parents:
diff changeset
    97
        _delta -= delta;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    98
      return true;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    99
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   100
    return false;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   101
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   102
489c9b5090e2 Initial load
duke
parents:
diff changeset
   103
  void print()                 { tty->print_cr("ChangeJumpWiden. bci: %d   Delta: %d", bci(), _delta); }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   104
};
489c9b5090e2 Initial load
duke
parents:
diff changeset
   105
489c9b5090e2 Initial load
duke
parents:
diff changeset
   106
class ChangeSwitchPad : public ChangeItem {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   107
  int  _padding;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   108
  bool _is_lookup_switch;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   109
 public:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   110
   ChangeSwitchPad(int bci, int padding, bool is_lookup_switch) : ChangeItem(bci) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   111
     _padding = padding;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   112
     _is_lookup_switch = is_lookup_switch;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   113
   }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   114
489c9b5090e2 Initial load
duke
parents:
diff changeset
   115
   // Callback to do instruction
489c9b5090e2 Initial load
duke
parents:
diff changeset
   116
   bool handle_code_change(Relocator *r) { return r->handle_switch_pad(bci(), _padding, _is_lookup_switch); };
489c9b5090e2 Initial load
duke
parents:
diff changeset
   117
489c9b5090e2 Initial load
duke
parents:
diff changeset
   118
   bool is_switch_pad()        { return true; }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   119
   int  padding()              { return _padding;  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   120
   bool is_lookup_switch()     { return _is_lookup_switch; }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   121
489c9b5090e2 Initial load
duke
parents:
diff changeset
   122
   void print()                { tty->print_cr("ChangeSwitchPad. bci: %d   Padding: %d  IsLookupSwitch: %d", bci(), _padding, _is_lookup_switch); }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   123
};
489c9b5090e2 Initial load
duke
parents:
diff changeset
   124
489c9b5090e2 Initial load
duke
parents:
diff changeset
   125
//-----------------------------------------------------------------------------------------------------------
489c9b5090e2 Initial load
duke
parents:
diff changeset
   126
// Relocator code
489c9b5090e2 Initial load
duke
parents:
diff changeset
   127
33593
60764a78fa5c 8140274: methodHandles and constantPoolHandles should be passed as const references
coleenp
parents: 22551
diff changeset
   128
Relocator::Relocator(const methodHandle& m, RelocatorListener* listener) {
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   129
  set_method(m);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   130
  set_code_length(method()->code_size());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   131
  set_code_array(NULL);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   132
  // Allocate code array and copy bytecodes
489c9b5090e2 Initial load
duke
parents:
diff changeset
   133
  if (!expand_code_array(0)) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   134
    // Should have at least MAX_METHOD_LENGTH available or the verifier
489c9b5090e2 Initial load
duke
parents:
diff changeset
   135
    // would have failed.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   136
    ShouldNotReachHere();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   137
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   138
  set_compressed_line_number_table(NULL);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   139
  set_compressed_line_number_table_size(0);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   140
  _listener = listener;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   141
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   142
489c9b5090e2 Initial load
duke
parents:
diff changeset
   143
// size is the new size of the instruction at bci. Hence, if size is less than the current
22551
9bf46d16dcc6 8025856: Fix typos in the GC code
jwilhelm
parents: 13728
diff changeset
   144
// instruction size, we will shrink the code.
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   145
methodHandle Relocator::insert_space_at(int bci, int size, u_char inst_buffer[], TRAPS) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   146
  _changes = new GrowableArray<ChangeItem*> (10);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   147
  _changes->push(new ChangeWiden(bci, size, inst_buffer));
489c9b5090e2 Initial load
duke
parents:
diff changeset
   148
489c9b5090e2 Initial load
duke
parents:
diff changeset
   149
  if (TraceRelocator) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   150
    tty->print_cr("Space at: %d Size: %d", bci, size);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   151
    _method->print();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   152
    _method->print_codes();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   153
    tty->print_cr("-------------------------------------------------");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   154
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   155
489c9b5090e2 Initial load
duke
parents:
diff changeset
   156
  if (!handle_code_changes()) return methodHandle();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   157
489c9b5090e2 Initial load
duke
parents:
diff changeset
   158
    // Construct the new method
13728
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents: 13476
diff changeset
   159
  methodHandle new_method = Method::clone_with_new_data(method(),
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   160
                              code_array(), code_length(),
489c9b5090e2 Initial load
duke
parents:
diff changeset
   161
                              compressed_line_number_table(),
489c9b5090e2 Initial load
duke
parents:
diff changeset
   162
                              compressed_line_number_table_size(),
489c9b5090e2 Initial load
duke
parents:
diff changeset
   163
                              CHECK_(methodHandle()));
13728
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents: 13476
diff changeset
   164
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents: 13476
diff changeset
   165
  // Deallocate the old Method* from metadata
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents: 13476
diff changeset
   166
  ClassLoaderData* loader_data = method()->method_holder()->class_loader_data();
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents: 13476
diff changeset
   167
  loader_data->add_to_deallocate_list(method()());
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents: 13476
diff changeset
   168
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   169
    set_method(new_method);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   170
489c9b5090e2 Initial load
duke
parents:
diff changeset
   171
  if (TraceRelocator) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   172
    tty->print_cr("-------------------------------------------------");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   173
    tty->print_cr("new method");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   174
    _method->print_codes();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   175
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   176
489c9b5090e2 Initial load
duke
parents:
diff changeset
   177
  return new_method;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   178
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   179
489c9b5090e2 Initial load
duke
parents:
diff changeset
   180
489c9b5090e2 Initial load
duke
parents:
diff changeset
   181
bool Relocator::handle_code_changes() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   182
  assert(_changes != NULL, "changes vector must be initialized");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   183
489c9b5090e2 Initial load
duke
parents:
diff changeset
   184
  while (!_changes->is_empty()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   185
    // Inv: everything is aligned.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   186
    ChangeItem* ci = _changes->first();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   187
489c9b5090e2 Initial load
duke
parents:
diff changeset
   188
    if (TraceRelocator) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   189
      ci->print();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   190
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   191
489c9b5090e2 Initial load
duke
parents:
diff changeset
   192
    // Execute operation
489c9b5090e2 Initial load
duke
parents:
diff changeset
   193
    if (!ci->handle_code_change(this)) return false;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   194
22551
9bf46d16dcc6 8025856: Fix typos in the GC code
jwilhelm
parents: 13728
diff changeset
   195
    // Shuffle items up
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   196
    for (int index = 1; index < _changes->length(); index++) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   197
      _changes->at_put(index-1, _changes->at(index));
489c9b5090e2 Initial load
duke
parents:
diff changeset
   198
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   199
    _changes->pop();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   200
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   201
  return true;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   202
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   203
489c9b5090e2 Initial load
duke
parents:
diff changeset
   204
489c9b5090e2 Initial load
duke
parents:
diff changeset
   205
bool Relocator::is_opcode_lookupswitch(Bytecodes::Code bc) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   206
  switch (bc) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   207
    case Bytecodes::_tableswitch:       return false;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   208
    case Bytecodes::_lookupswitch:                   // not rewritten on ia64
489c9b5090e2 Initial load
duke
parents:
diff changeset
   209
    case Bytecodes::_fast_linearswitch:              // rewritten _lookupswitch
489c9b5090e2 Initial load
duke
parents:
diff changeset
   210
    case Bytecodes::_fast_binaryswitch: return true; // rewritten _lookupswitch
489c9b5090e2 Initial load
duke
parents:
diff changeset
   211
    default: ShouldNotReachHere();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   212
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   213
  return true; // dummy
489c9b5090e2 Initial load
duke
parents:
diff changeset
   214
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   215
489c9b5090e2 Initial load
duke
parents:
diff changeset
   216
// We need a special instruction size method, since lookupswitches and tableswitches might not be
22551
9bf46d16dcc6 8025856: Fix typos in the GC code
jwilhelm
parents: 13728
diff changeset
   217
// properly aligned during relocation
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   218
int Relocator::rc_instr_len(int bci) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   219
  Bytecodes::Code bc= code_at(bci);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   220
  switch (bc) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   221
    // In the case of switch instructions, see if we have the original
489c9b5090e2 Initial load
duke
parents:
diff changeset
   222
    // padding recorded.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   223
    case Bytecodes::_tableswitch:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   224
    case Bytecodes::_lookupswitch:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   225
    case Bytecodes::_fast_linearswitch:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   226
    case Bytecodes::_fast_binaryswitch:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   227
    {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   228
      int pad = get_orig_switch_pad(bci, is_opcode_lookupswitch(bc));
489c9b5090e2 Initial load
duke
parents:
diff changeset
   229
      if (pad == -1) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   230
        return instruction_length_at(bci);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   231
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   232
      // Otherwise, depends on the switch type.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   233
      switch (bc) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   234
        case Bytecodes::_tableswitch: {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   235
          int lo = int_at(bci + 1 + pad + 4 * 1);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   236
          int hi = int_at(bci + 1 + pad + 4 * 2);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   237
          int n = hi - lo + 1;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   238
          return 1 + pad + 4*(3 + n);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   239
        }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   240
        case Bytecodes::_lookupswitch:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   241
        case Bytecodes::_fast_linearswitch:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   242
        case Bytecodes::_fast_binaryswitch: {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   243
          int npairs = int_at(bci + 1 + pad + 4 * 1);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   244
          return 1 + pad + 4*(2 + 2*npairs);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   245
        }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   246
        default:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   247
          ShouldNotReachHere();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   248
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   249
    }
46630
75aa3e39d02c 8182299: Enable disabled clang warnings, build on OSX 10 + Xcode 8
jwilhelm
parents: 38658
diff changeset
   250
    default:
75aa3e39d02c 8182299: Enable disabled clang warnings, build on OSX 10 + Xcode 8
jwilhelm
parents: 38658
diff changeset
   251
      break;
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   252
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   253
  return instruction_length_at(bci);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   254
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   255
489c9b5090e2 Initial load
duke
parents:
diff changeset
   256
// If a change item is recorded for "pc", with type "ct", returns the
489c9b5090e2 Initial load
duke
parents:
diff changeset
   257
// associated padding, else -1.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   258
int Relocator::get_orig_switch_pad(int bci, bool is_lookup_switch) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   259
  for (int k = 0; k < _changes->length(); k++) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   260
    ChangeItem* ci = _changes->at(k);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   261
    if (ci->is_switch_pad()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   262
      ChangeSwitchPad* csp = (ChangeSwitchPad*)ci;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   263
      if (csp->is_lookup_switch() == is_lookup_switch && csp->bci() == bci) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   264
        return csp->padding();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   265
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   266
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   267
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   268
  return -1;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   269
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   270
489c9b5090e2 Initial load
duke
parents:
diff changeset
   271
489c9b5090e2 Initial load
duke
parents:
diff changeset
   272
// Push a ChangeJumpWiden if it doesn't already exist on the work queue,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   273
// otherwise adjust the item already there by delta.  The calculation for
489c9b5090e2 Initial load
duke
parents:
diff changeset
   274
// new_delta is wrong for this because it uses the offset stored in the
489c9b5090e2 Initial load
duke
parents:
diff changeset
   275
// code stream itself which wasn't fixed when item was pushed on the work queue.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   276
void Relocator::push_jump_widen(int bci, int delta, int new_delta) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   277
  for (int j = 0; j < _changes->length(); j++) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   278
    ChangeItem* ci = _changes->at(j);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   279
    if (ci->adjust(bci, delta)) return;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   280
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   281
  _changes->push(new ChangeJumpWiden(bci, new_delta));
489c9b5090e2 Initial load
duke
parents:
diff changeset
   282
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   283
489c9b5090e2 Initial load
duke
parents:
diff changeset
   284
489c9b5090e2 Initial load
duke
parents:
diff changeset
   285
// The current instruction of "c" is a jump; one of its offset starts
489c9b5090e2 Initial load
duke
parents:
diff changeset
   286
// at "offset" and is a short if "isShort" is "TRUE",
489c9b5090e2 Initial load
duke
parents:
diff changeset
   287
// and an integer otherwise.  If the jump crosses "breakPC", change
489c9b5090e2 Initial load
duke
parents:
diff changeset
   288
// the span of the jump by "delta".
489c9b5090e2 Initial load
duke
parents:
diff changeset
   289
void Relocator::change_jump(int bci, int offset, bool is_short, int break_bci, int delta) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   290
  int bci_delta = (is_short) ? short_at(offset) : int_at(offset);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   291
  int targ = bci + bci_delta;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   292
489c9b5090e2 Initial load
duke
parents:
diff changeset
   293
  if ((bci <= break_bci && targ >  break_bci) ||
489c9b5090e2 Initial load
duke
parents:
diff changeset
   294
      (bci >  break_bci && targ <= break_bci)) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   295
    int new_delta;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   296
    if (bci_delta > 0)
489c9b5090e2 Initial load
duke
parents:
diff changeset
   297
      new_delta = bci_delta + delta;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   298
    else
489c9b5090e2 Initial load
duke
parents:
diff changeset
   299
      new_delta = bci_delta - delta;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   300
489c9b5090e2 Initial load
duke
parents:
diff changeset
   301
    if (is_short && ((new_delta > MAX_SHORT) || new_delta < MIN_SHORT)) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   302
      push_jump_widen(bci, delta, new_delta);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   303
    } else if (is_short) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   304
      short_at_put(offset, new_delta);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   305
    } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   306
      int_at_put(offset, new_delta);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   307
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   308
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   309
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   310
489c9b5090e2 Initial load
duke
parents:
diff changeset
   311
489c9b5090e2 Initial load
duke
parents:
diff changeset
   312
// Changes all jumps crossing "break_bci" by "delta".  May enqueue things
489c9b5090e2 Initial load
duke
parents:
diff changeset
   313
// on "rc->changes"
489c9b5090e2 Initial load
duke
parents:
diff changeset
   314
void Relocator::change_jumps(int break_bci, int delta) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   315
  int bci = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   316
  Bytecodes::Code bc;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   317
  // Now, adjust any affected instructions.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   318
  while (bci < code_length()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   319
    switch (bc= code_at(bci)) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   320
      case Bytecodes::_ifeq:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   321
      case Bytecodes::_ifne:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   322
      case Bytecodes::_iflt:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   323
      case Bytecodes::_ifge:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   324
      case Bytecodes::_ifgt:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   325
      case Bytecodes::_ifle:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   326
      case Bytecodes::_if_icmpeq:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   327
      case Bytecodes::_if_icmpne:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   328
      case Bytecodes::_if_icmplt:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   329
      case Bytecodes::_if_icmpge:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   330
      case Bytecodes::_if_icmpgt:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   331
      case Bytecodes::_if_icmple:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   332
      case Bytecodes::_if_acmpeq:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   333
      case Bytecodes::_if_acmpne:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   334
      case Bytecodes::_ifnull:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   335
      case Bytecodes::_ifnonnull:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   336
      case Bytecodes::_goto:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   337
      case Bytecodes::_jsr:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   338
        change_jump(bci, bci+1, true, break_bci, delta);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   339
        break;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   340
      case Bytecodes::_goto_w:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   341
      case Bytecodes::_jsr_w:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   342
        change_jump(bci, bci+1, false, break_bci, delta);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   343
        break;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   344
      case Bytecodes::_tableswitch:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   345
      case Bytecodes::_lookupswitch:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   346
      case Bytecodes::_fast_linearswitch:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   347
      case Bytecodes::_fast_binaryswitch: {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   348
        int recPad = get_orig_switch_pad(bci, (bc != Bytecodes::_tableswitch));
489c9b5090e2 Initial load
duke
parents:
diff changeset
   349
        int oldPad = (recPad != -1) ? recPad : align(bci+1) - (bci+1);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   350
        if (bci > break_bci) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   351
          int new_bci = bci + delta;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   352
          int newPad = align(new_bci+1) - (new_bci+1);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   353
          // Do we need to check the padding?
489c9b5090e2 Initial load
duke
parents:
diff changeset
   354
          if (newPad != oldPad) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   355
            if (recPad == -1) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   356
              _changes->push(new ChangeSwitchPad(bci, oldPad, (bc != Bytecodes::_tableswitch)));
489c9b5090e2 Initial load
duke
parents:
diff changeset
   357
            }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   358
          }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   359
        }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   360
489c9b5090e2 Initial load
duke
parents:
diff changeset
   361
        // Then the rest, which depend on the kind of switch.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   362
        switch (bc) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   363
          case Bytecodes::_tableswitch: {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   364
            change_jump(bci, bci +1 + oldPad, false, break_bci, delta);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   365
            // We cannot use the Bytecode_tableswitch abstraction, since the padding might not be correct.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   366
            int lo = int_at(bci + 1 + oldPad + 4 * 1);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   367
            int hi = int_at(bci + 1 + oldPad + 4 * 2);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   368
            int n = hi - lo + 1;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   369
            for (int k = 0; k < n; k++) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   370
              change_jump(bci, bci +1 + oldPad + 4*(k+3), false, break_bci, delta);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   371
            }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   372
            // Special next-bci calculation here...
489c9b5090e2 Initial load
duke
parents:
diff changeset
   373
            bci += 1 + oldPad + (n+3)*4;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   374
            continue;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   375
          }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   376
          case Bytecodes::_lookupswitch:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   377
          case Bytecodes::_fast_linearswitch:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   378
          case Bytecodes::_fast_binaryswitch: {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   379
            change_jump(bci, bci +1 + oldPad, false, break_bci, delta);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   380
            // We cannot use the Bytecode_lookupswitch abstraction, since the padding might not be correct.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   381
            int npairs = int_at(bci + 1 + oldPad + 4 * 1);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   382
            for (int k = 0; k < npairs; k++) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   383
              change_jump(bci, bci + 1 + oldPad + 4*(2 + 2*k + 1), false, break_bci, delta);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   384
            }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   385
            /* Special next-bci calculation here... */
489c9b5090e2 Initial load
duke
parents:
diff changeset
   386
            bci += 1 + oldPad + (2 + (npairs*2))*4;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   387
            continue;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   388
          }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   389
          default:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   390
            ShouldNotReachHere();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   391
        }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   392
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   393
      default:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   394
        break;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   395
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   396
    bci += rc_instr_len(bci);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   397
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   398
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   399
489c9b5090e2 Initial load
duke
parents:
diff changeset
   400
// The width of instruction at "pc" is changing by "delta".  Adjust the
489c9b5090e2 Initial load
duke
parents:
diff changeset
   401
// exception table, if any, of "rc->mb".
489c9b5090e2 Initial load
duke
parents:
diff changeset
   402
void Relocator::adjust_exception_table(int bci, int delta) {
13282
9872915dd78d 7178145: Change constMethodOop::_exception_table to optionally inlined u2 table.
jiangli
parents: 7397
diff changeset
   403
  ExceptionTable table(_method());
9872915dd78d 7178145: Change constMethodOop::_exception_table to optionally inlined u2 table.
jiangli
parents: 7397
diff changeset
   404
  for (int index = 0; index < table.length(); index ++) {
9872915dd78d 7178145: Change constMethodOop::_exception_table to optionally inlined u2 table.
jiangli
parents: 7397
diff changeset
   405
    if (table.start_pc(index) > bci) {
9872915dd78d 7178145: Change constMethodOop::_exception_table to optionally inlined u2 table.
jiangli
parents: 7397
diff changeset
   406
      table.set_start_pc(index, table.start_pc(index) + delta);
9872915dd78d 7178145: Change constMethodOop::_exception_table to optionally inlined u2 table.
jiangli
parents: 7397
diff changeset
   407
      table.set_end_pc(index, table.end_pc(index) + delta);
9872915dd78d 7178145: Change constMethodOop::_exception_table to optionally inlined u2 table.
jiangli
parents: 7397
diff changeset
   408
    } else if (bci < table.end_pc(index)) {
9872915dd78d 7178145: Change constMethodOop::_exception_table to optionally inlined u2 table.
jiangli
parents: 7397
diff changeset
   409
      table.set_end_pc(index, table.end_pc(index) + delta);
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   410
    }
13282
9872915dd78d 7178145: Change constMethodOop::_exception_table to optionally inlined u2 table.
jiangli
parents: 7397
diff changeset
   411
    if (table.handler_pc(index) > bci)
9872915dd78d 7178145: Change constMethodOop::_exception_table to optionally inlined u2 table.
jiangli
parents: 7397
diff changeset
   412
      table.set_handler_pc(index, table.handler_pc(index) + delta);
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   413
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   414
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   415
489c9b5090e2 Initial load
duke
parents:
diff changeset
   416
489c9b5090e2 Initial load
duke
parents:
diff changeset
   417
// The width of instruction at "bci" is changing by "delta".  Adjust the line number table.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   418
void Relocator::adjust_line_no_table(int bci, int delta) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   419
  if (method()->has_linenumber_table()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   420
    CompressedLineNumberReadStream  reader(method()->compressed_linenumber_table());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   421
    CompressedLineNumberWriteStream writer(64);  // plenty big for most line number tables
489c9b5090e2 Initial load
duke
parents:
diff changeset
   422
    while (reader.read_pair()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   423
      int adjustment = (reader.bci() > bci) ? delta : 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   424
      writer.write_pair(reader.bci() + adjustment, reader.line());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   425
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   426
    writer.write_terminator();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   427
    set_compressed_line_number_table(writer.buffer());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   428
    set_compressed_line_number_table_size(writer.position());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   429
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   430
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   431
489c9b5090e2 Initial load
duke
parents:
diff changeset
   432
489c9b5090e2 Initial load
duke
parents:
diff changeset
   433
// The width of instruction at "bci" is changing by "delta".  Adjust the local variable table.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   434
void Relocator::adjust_local_var_table(int bci, int delta) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   435
  int localvariable_table_length = method()->localvariable_table_length();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   436
  if (localvariable_table_length > 0) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   437
    LocalVariableTableElement* table = method()->localvariable_table_start();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   438
    for (int i = 0; i < localvariable_table_length; i++) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   439
      u2 current_bci = table[i].start_bci;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   440
      if (current_bci > bci) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   441
        table[i].start_bci = current_bci + delta;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   442
      } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   443
        u2 current_length = table[i].length;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   444
        if (current_bci + current_length > bci) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   445
          table[i].length = current_length + delta;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   446
        }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   447
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   448
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   449
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   450
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   451
6974
a013f1c533a5 6991315: RedefineClasses fails with java.lang.VerifyError
kamg
parents: 5547
diff changeset
   452
// Create a new array, copying the src array but adding a hole at
a013f1c533a5 6991315: RedefineClasses fails with java.lang.VerifyError
kamg
parents: 5547
diff changeset
   453
// the specified location
13728
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents: 13476
diff changeset
   454
static Array<u1>* insert_hole_at(ClassLoaderData* loader_data,
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents: 13476
diff changeset
   455
    size_t where, int hole_sz, Array<u1>* src) {
6974
a013f1c533a5 6991315: RedefineClasses fails with java.lang.VerifyError
kamg
parents: 5547
diff changeset
   456
  Thread* THREAD = Thread::current();
13728
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents: 13476
diff changeset
   457
  Array<u1>* dst =
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents: 13476
diff changeset
   458
      MetadataFactory::new_array<u1>(loader_data, src->length() + hole_sz, 0, CHECK_NULL);
6974
a013f1c533a5 6991315: RedefineClasses fails with java.lang.VerifyError
kamg
parents: 5547
diff changeset
   459
13728
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents: 13476
diff changeset
   460
  address src_addr = (address)src->adr_at(0);
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents: 13476
diff changeset
   461
  address dst_addr = (address)dst->adr_at(0);
6974
a013f1c533a5 6991315: RedefineClasses fails with java.lang.VerifyError
kamg
parents: 5547
diff changeset
   462
a013f1c533a5 6991315: RedefineClasses fails with java.lang.VerifyError
kamg
parents: 5547
diff changeset
   463
  memcpy(dst_addr, src_addr, where);
a013f1c533a5 6991315: RedefineClasses fails with java.lang.VerifyError
kamg
parents: 5547
diff changeset
   464
  memcpy(dst_addr + where + hole_sz,
a013f1c533a5 6991315: RedefineClasses fails with java.lang.VerifyError
kamg
parents: 5547
diff changeset
   465
         src_addr + where, src->length() - where);
a013f1c533a5 6991315: RedefineClasses fails with java.lang.VerifyError
kamg
parents: 5547
diff changeset
   466
  return dst;
a013f1c533a5 6991315: RedefineClasses fails with java.lang.VerifyError
kamg
parents: 5547
diff changeset
   467
}
a013f1c533a5 6991315: RedefineClasses fails with java.lang.VerifyError
kamg
parents: 5547
diff changeset
   468
a013f1c533a5 6991315: RedefineClasses fails with java.lang.VerifyError
kamg
parents: 5547
diff changeset
   469
// The width of instruction at "bci" is changing by "delta".  Adjust the stack
a013f1c533a5 6991315: RedefineClasses fails with java.lang.VerifyError
kamg
parents: 5547
diff changeset
   470
// map frames.
a013f1c533a5 6991315: RedefineClasses fails with java.lang.VerifyError
kamg
parents: 5547
diff changeset
   471
void Relocator::adjust_stack_map_table(int bci, int delta) {
a013f1c533a5 6991315: RedefineClasses fails with java.lang.VerifyError
kamg
parents: 5547
diff changeset
   472
  if (method()->has_stackmap_table()) {
13728
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents: 13476
diff changeset
   473
    Array<u1>* data = method()->stackmap_data();
13476
471200fb94fd 7116786: RFE: Detailed information on VerifyErrors
kamg
parents: 13282
diff changeset
   474
    // The data in the array is a classfile representation of the stackmap table
471200fb94fd 7116786: RFE: Detailed information on VerifyErrors
kamg
parents: 13282
diff changeset
   475
    stack_map_table* sm_table =
13728
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents: 13476
diff changeset
   476
        stack_map_table::at((address)data->adr_at(0));
6974
a013f1c533a5 6991315: RedefineClasses fails with java.lang.VerifyError
kamg
parents: 5547
diff changeset
   477
13476
471200fb94fd 7116786: RFE: Detailed information on VerifyErrors
kamg
parents: 13282
diff changeset
   478
    int count = sm_table->number_of_entries();
471200fb94fd 7116786: RFE: Detailed information on VerifyErrors
kamg
parents: 13282
diff changeset
   479
    stack_map_frame* frame = sm_table->entries();
6974
a013f1c533a5 6991315: RedefineClasses fails with java.lang.VerifyError
kamg
parents: 5547
diff changeset
   480
    int bci_iter = -1;
a013f1c533a5 6991315: RedefineClasses fails with java.lang.VerifyError
kamg
parents: 5547
diff changeset
   481
    bool offset_adjusted = false; // only need to adjust one offset
a013f1c533a5 6991315: RedefineClasses fails with java.lang.VerifyError
kamg
parents: 5547
diff changeset
   482
a013f1c533a5 6991315: RedefineClasses fails with java.lang.VerifyError
kamg
parents: 5547
diff changeset
   483
    for (int i = 0; i < count; ++i) {
a013f1c533a5 6991315: RedefineClasses fails with java.lang.VerifyError
kamg
parents: 5547
diff changeset
   484
      int offset_delta = frame->offset_delta();
a013f1c533a5 6991315: RedefineClasses fails with java.lang.VerifyError
kamg
parents: 5547
diff changeset
   485
      bci_iter += offset_delta;
a013f1c533a5 6991315: RedefineClasses fails with java.lang.VerifyError
kamg
parents: 5547
diff changeset
   486
a013f1c533a5 6991315: RedefineClasses fails with java.lang.VerifyError
kamg
parents: 5547
diff changeset
   487
      if (!offset_adjusted && bci_iter > bci) {
a013f1c533a5 6991315: RedefineClasses fails with java.lang.VerifyError
kamg
parents: 5547
diff changeset
   488
        int new_offset_delta = offset_delta + delta;
a013f1c533a5 6991315: RedefineClasses fails with java.lang.VerifyError
kamg
parents: 5547
diff changeset
   489
a013f1c533a5 6991315: RedefineClasses fails with java.lang.VerifyError
kamg
parents: 5547
diff changeset
   490
        if (frame->is_valid_offset(new_offset_delta)) {
a013f1c533a5 6991315: RedefineClasses fails with java.lang.VerifyError
kamg
parents: 5547
diff changeset
   491
          frame->set_offset_delta(new_offset_delta);
a013f1c533a5 6991315: RedefineClasses fails with java.lang.VerifyError
kamg
parents: 5547
diff changeset
   492
        } else {
a013f1c533a5 6991315: RedefineClasses fails with java.lang.VerifyError
kamg
parents: 5547
diff changeset
   493
          assert(frame->is_same_frame() ||
13476
471200fb94fd 7116786: RFE: Detailed information on VerifyErrors
kamg
parents: 13282
diff changeset
   494
                 frame->is_same_locals_1_stack_item_frame(),
6974
a013f1c533a5 6991315: RedefineClasses fails with java.lang.VerifyError
kamg
parents: 5547
diff changeset
   495
                 "Frame must be one of the compressed forms");
a013f1c533a5 6991315: RedefineClasses fails with java.lang.VerifyError
kamg
parents: 5547
diff changeset
   496
          // The new delta exceeds the capacity of the 'same_frame' or
a013f1c533a5 6991315: RedefineClasses fails with java.lang.VerifyError
kamg
parents: 5547
diff changeset
   497
          // 'same_frame_1_stack_item_frame' frame types.  We need to
a013f1c533a5 6991315: RedefineClasses fails with java.lang.VerifyError
kamg
parents: 5547
diff changeset
   498
          // convert these frames to the extended versions, but the extended
a013f1c533a5 6991315: RedefineClasses fails with java.lang.VerifyError
kamg
parents: 5547
diff changeset
   499
          // version is bigger and requires more room.  So we allocate a
a013f1c533a5 6991315: RedefineClasses fails with java.lang.VerifyError
kamg
parents: 5547
diff changeset
   500
          // new array and copy the data, being sure to leave u2-sized hole
a013f1c533a5 6991315: RedefineClasses fails with java.lang.VerifyError
kamg
parents: 5547
diff changeset
   501
          // right after the 'frame_type' for the new offset field.
a013f1c533a5 6991315: RedefineClasses fails with java.lang.VerifyError
kamg
parents: 5547
diff changeset
   502
          //
a013f1c533a5 6991315: RedefineClasses fails with java.lang.VerifyError
kamg
parents: 5547
diff changeset
   503
          // We can safely ignore the reverse situation as a small delta
a013f1c533a5 6991315: RedefineClasses fails with java.lang.VerifyError
kamg
parents: 5547
diff changeset
   504
          // can still be used in an extended version of the frame.
a013f1c533a5 6991315: RedefineClasses fails with java.lang.VerifyError
kamg
parents: 5547
diff changeset
   505
13728
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents: 13476
diff changeset
   506
          size_t frame_offset = (address)frame - (address)data->adr_at(0);
6974
a013f1c533a5 6991315: RedefineClasses fails with java.lang.VerifyError
kamg
parents: 5547
diff changeset
   507
13728
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents: 13476
diff changeset
   508
          ClassLoaderData* loader_data = method()->method_holder()->class_loader_data();
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents: 13476
diff changeset
   509
          Array<u1>* new_data = insert_hole_at(loader_data, frame_offset + 1, 2, data);
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents: 13476
diff changeset
   510
          if (new_data == NULL) {
6974
a013f1c533a5 6991315: RedefineClasses fails with java.lang.VerifyError
kamg
parents: 5547
diff changeset
   511
            return; // out-of-memory?
a013f1c533a5 6991315: RedefineClasses fails with java.lang.VerifyError
kamg
parents: 5547
diff changeset
   512
          }
13728
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents: 13476
diff changeset
   513
          // Deallocate old data
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents: 13476
diff changeset
   514
          MetadataFactory::free_array<u1>(loader_data, data);
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents: 13476
diff changeset
   515
          data = new_data;
6974
a013f1c533a5 6991315: RedefineClasses fails with java.lang.VerifyError
kamg
parents: 5547
diff changeset
   516
13728
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents: 13476
diff changeset
   517
          address frame_addr = (address)(data->adr_at(0) + frame_offset);
6974
a013f1c533a5 6991315: RedefineClasses fails with java.lang.VerifyError
kamg
parents: 5547
diff changeset
   518
          frame = stack_map_frame::at(frame_addr);
a013f1c533a5 6991315: RedefineClasses fails with java.lang.VerifyError
kamg
parents: 5547
diff changeset
   519
a013f1c533a5 6991315: RedefineClasses fails with java.lang.VerifyError
kamg
parents: 5547
diff changeset
   520
a013f1c533a5 6991315: RedefineClasses fails with java.lang.VerifyError
kamg
parents: 5547
diff changeset
   521
          // Now convert the frames in place
a013f1c533a5 6991315: RedefineClasses fails with java.lang.VerifyError
kamg
parents: 5547
diff changeset
   522
          if (frame->is_same_frame()) {
a013f1c533a5 6991315: RedefineClasses fails with java.lang.VerifyError
kamg
parents: 5547
diff changeset
   523
            same_frame_extended::create_at(frame_addr, new_offset_delta);
a013f1c533a5 6991315: RedefineClasses fails with java.lang.VerifyError
kamg
parents: 5547
diff changeset
   524
          } else {
13476
471200fb94fd 7116786: RFE: Detailed information on VerifyErrors
kamg
parents: 13282
diff changeset
   525
            same_locals_1_stack_item_extended::create_at(
6974
a013f1c533a5 6991315: RedefineClasses fails with java.lang.VerifyError
kamg
parents: 5547
diff changeset
   526
              frame_addr, new_offset_delta, NULL);
a013f1c533a5 6991315: RedefineClasses fails with java.lang.VerifyError
kamg
parents: 5547
diff changeset
   527
            // the verification_info_type should already be at the right spot
a013f1c533a5 6991315: RedefineClasses fails with java.lang.VerifyError
kamg
parents: 5547
diff changeset
   528
          }
a013f1c533a5 6991315: RedefineClasses fails with java.lang.VerifyError
kamg
parents: 5547
diff changeset
   529
        }
a013f1c533a5 6991315: RedefineClasses fails with java.lang.VerifyError
kamg
parents: 5547
diff changeset
   530
        offset_adjusted = true; // needs to be done only once, since subsequent
a013f1c533a5 6991315: RedefineClasses fails with java.lang.VerifyError
kamg
parents: 5547
diff changeset
   531
                                // values are offsets from the current
a013f1c533a5 6991315: RedefineClasses fails with java.lang.VerifyError
kamg
parents: 5547
diff changeset
   532
      }
a013f1c533a5 6991315: RedefineClasses fails with java.lang.VerifyError
kamg
parents: 5547
diff changeset
   533
a013f1c533a5 6991315: RedefineClasses fails with java.lang.VerifyError
kamg
parents: 5547
diff changeset
   534
      // The stack map frame may contain verification types, if so we need to
a013f1c533a5 6991315: RedefineClasses fails with java.lang.VerifyError
kamg
parents: 5547
diff changeset
   535
      // check and update any Uninitialized type's bci (no matter where it is).
a013f1c533a5 6991315: RedefineClasses fails with java.lang.VerifyError
kamg
parents: 5547
diff changeset
   536
      int number_of_types = frame->number_of_types();
a013f1c533a5 6991315: RedefineClasses fails with java.lang.VerifyError
kamg
parents: 5547
diff changeset
   537
      verification_type_info* types = frame->types();
a013f1c533a5 6991315: RedefineClasses fails with java.lang.VerifyError
kamg
parents: 5547
diff changeset
   538
a013f1c533a5 6991315: RedefineClasses fails with java.lang.VerifyError
kamg
parents: 5547
diff changeset
   539
      for (int i = 0; i < number_of_types; ++i) {
a013f1c533a5 6991315: RedefineClasses fails with java.lang.VerifyError
kamg
parents: 5547
diff changeset
   540
        if (types->is_uninitialized() && types->bci() > bci) {
a013f1c533a5 6991315: RedefineClasses fails with java.lang.VerifyError
kamg
parents: 5547
diff changeset
   541
          types->set_bci(types->bci() + delta);
a013f1c533a5 6991315: RedefineClasses fails with java.lang.VerifyError
kamg
parents: 5547
diff changeset
   542
        }
a013f1c533a5 6991315: RedefineClasses fails with java.lang.VerifyError
kamg
parents: 5547
diff changeset
   543
        types = types->next();
a013f1c533a5 6991315: RedefineClasses fails with java.lang.VerifyError
kamg
parents: 5547
diff changeset
   544
      }
a013f1c533a5 6991315: RedefineClasses fails with java.lang.VerifyError
kamg
parents: 5547
diff changeset
   545
a013f1c533a5 6991315: RedefineClasses fails with java.lang.VerifyError
kamg
parents: 5547
diff changeset
   546
      // Full frame has stack values too
a013f1c533a5 6991315: RedefineClasses fails with java.lang.VerifyError
kamg
parents: 5547
diff changeset
   547
      full_frame* ff = frame->as_full_frame();
a013f1c533a5 6991315: RedefineClasses fails with java.lang.VerifyError
kamg
parents: 5547
diff changeset
   548
      if (ff != NULL) {
a013f1c533a5 6991315: RedefineClasses fails with java.lang.VerifyError
kamg
parents: 5547
diff changeset
   549
        address eol = (address)types;
a013f1c533a5 6991315: RedefineClasses fails with java.lang.VerifyError
kamg
parents: 5547
diff changeset
   550
        number_of_types = ff->stack_slots(eol);
a013f1c533a5 6991315: RedefineClasses fails with java.lang.VerifyError
kamg
parents: 5547
diff changeset
   551
        types = ff->stack(eol);
a013f1c533a5 6991315: RedefineClasses fails with java.lang.VerifyError
kamg
parents: 5547
diff changeset
   552
        for (int i = 0; i < number_of_types; ++i) {
a013f1c533a5 6991315: RedefineClasses fails with java.lang.VerifyError
kamg
parents: 5547
diff changeset
   553
          if (types->is_uninitialized() && types->bci() > bci) {
a013f1c533a5 6991315: RedefineClasses fails with java.lang.VerifyError
kamg
parents: 5547
diff changeset
   554
            types->set_bci(types->bci() + delta);
a013f1c533a5 6991315: RedefineClasses fails with java.lang.VerifyError
kamg
parents: 5547
diff changeset
   555
          }
a013f1c533a5 6991315: RedefineClasses fails with java.lang.VerifyError
kamg
parents: 5547
diff changeset
   556
          types = types->next();
a013f1c533a5 6991315: RedefineClasses fails with java.lang.VerifyError
kamg
parents: 5547
diff changeset
   557
        }
a013f1c533a5 6991315: RedefineClasses fails with java.lang.VerifyError
kamg
parents: 5547
diff changeset
   558
      }
a013f1c533a5 6991315: RedefineClasses fails with java.lang.VerifyError
kamg
parents: 5547
diff changeset
   559
a013f1c533a5 6991315: RedefineClasses fails with java.lang.VerifyError
kamg
parents: 5547
diff changeset
   560
      frame = frame->next();
a013f1c533a5 6991315: RedefineClasses fails with java.lang.VerifyError
kamg
parents: 5547
diff changeset
   561
    }
a013f1c533a5 6991315: RedefineClasses fails with java.lang.VerifyError
kamg
parents: 5547
diff changeset
   562
a013f1c533a5 6991315: RedefineClasses fails with java.lang.VerifyError
kamg
parents: 5547
diff changeset
   563
    method()->set_stackmap_data(data); // in case it has changed
a013f1c533a5 6991315: RedefineClasses fails with java.lang.VerifyError
kamg
parents: 5547
diff changeset
   564
  }
a013f1c533a5 6991315: RedefineClasses fails with java.lang.VerifyError
kamg
parents: 5547
diff changeset
   565
}
a013f1c533a5 6991315: RedefineClasses fails with java.lang.VerifyError
kamg
parents: 5547
diff changeset
   566
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   567
489c9b5090e2 Initial load
duke
parents:
diff changeset
   568
bool Relocator::expand_code_array(int delta) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   569
  int length = MAX2(code_length() + delta, code_length() * (100+code_slop_pct()) / 100);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   570
489c9b5090e2 Initial load
duke
parents:
diff changeset
   571
  if (length > MAX_METHOD_LENGTH) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   572
    if (delta == 0 && code_length() <= MAX_METHOD_LENGTH) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   573
      length = MAX_METHOD_LENGTH;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   574
    } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   575
      return false;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   576
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   577
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   578
489c9b5090e2 Initial load
duke
parents:
diff changeset
   579
  unsigned char* new_code_array = NEW_RESOURCE_ARRAY(unsigned char, length);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   580
  if (!new_code_array) return false;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   581
489c9b5090e2 Initial load
duke
parents:
diff changeset
   582
  // Expanding current array
489c9b5090e2 Initial load
duke
parents:
diff changeset
   583
  if (code_array() != NULL) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   584
    memcpy(new_code_array, code_array(), code_length());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   585
  } else {
13728
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents: 13476
diff changeset
   586
    // Initial copy. Copy directly from Method*
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   587
    memcpy(new_code_array, method()->code_base(), code_length());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   588
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   589
489c9b5090e2 Initial load
duke
parents:
diff changeset
   590
  set_code_array(new_code_array);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   591
  set_code_array_length(length);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   592
489c9b5090e2 Initial load
duke
parents:
diff changeset
   593
  return true;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   594
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   595
489c9b5090e2 Initial load
duke
parents:
diff changeset
   596
489c9b5090e2 Initial load
duke
parents:
diff changeset
   597
// The instruction at "bci", whose size is "ilen", is changing size by
489c9b5090e2 Initial load
duke
parents:
diff changeset
   598
// "delta".  Reallocate, move code, recalculate jumps, and enqueue
489c9b5090e2 Initial load
duke
parents:
diff changeset
   599
// change items as necessary.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   600
bool Relocator::relocate_code(int bci, int ilen, int delta) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   601
  int next_bci = bci + ilen;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   602
  if (delta > 0 && code_length() + delta > code_array_length())  {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   603
    // Expand allocated code space, if necessary.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   604
    if (!expand_code_array(delta)) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   605
          return false;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   606
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   607
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   608
489c9b5090e2 Initial load
duke
parents:
diff changeset
   609
  // We require 4-byte alignment of code arrays.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   610
  assert(((intptr_t)code_array() & 3) == 0, "check code alignment");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   611
  // Change jumps before doing the copying; this routine requires aligned switches.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   612
  change_jumps(bci, delta);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   613
489c9b5090e2 Initial load
duke
parents:
diff changeset
   614
  // In case we have shrunken a tableswitch/lookupswitch statement, we store the last
489c9b5090e2 Initial load
duke
parents:
diff changeset
   615
  // bytes that get overwritten. We have to copy the bytes after the change_jumps method
22551
9bf46d16dcc6 8025856: Fix typos in the GC code
jwilhelm
parents: 13728
diff changeset
   616
  // has been called, since it is likely to update last offset in a tableswitch/lookupswitch
38658
34f9c45625d8 8140594: Various minor code improvements (compiler)
goetz
parents: 33593
diff changeset
   617
  assert(delta >= -3, "We cannot overwrite more than 3 bytes.");
34f9c45625d8 8140594: Various minor code improvements (compiler)
goetz
parents: 33593
diff changeset
   618
  if (delta < 0 && delta >= -3) {
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   619
    memcpy(_overwrite, addr_at(bci + ilen + delta), -delta);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   620
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   621
489c9b5090e2 Initial load
duke
parents:
diff changeset
   622
  memmove(addr_at(next_bci + delta), addr_at(next_bci), code_length() - next_bci);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   623
  set_code_length(code_length() + delta);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   624
  // Also adjust exception tables...
489c9b5090e2 Initial load
duke
parents:
diff changeset
   625
  adjust_exception_table(bci, delta);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   626
  // Line number tables...
489c9b5090e2 Initial load
duke
parents:
diff changeset
   627
  adjust_line_no_table(bci, delta);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   628
  // And local variable table...
489c9b5090e2 Initial load
duke
parents:
diff changeset
   629
  adjust_local_var_table(bci, delta);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   630
6974
a013f1c533a5 6991315: RedefineClasses fails with java.lang.VerifyError
kamg
parents: 5547
diff changeset
   631
  // Adjust stack maps
a013f1c533a5 6991315: RedefineClasses fails with java.lang.VerifyError
kamg
parents: 5547
diff changeset
   632
  adjust_stack_map_table(bci, delta);
a013f1c533a5 6991315: RedefineClasses fails with java.lang.VerifyError
kamg
parents: 5547
diff changeset
   633
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   634
  // Relocate the pending change stack...
489c9b5090e2 Initial load
duke
parents:
diff changeset
   635
  for (int j = 0; j < _changes->length(); j++) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   636
    ChangeItem* ci = _changes->at(j);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   637
    ci->relocate(bci, delta);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   638
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   639
489c9b5090e2 Initial load
duke
parents:
diff changeset
   640
  // Notify any listeners about code relocation
489c9b5090e2 Initial load
duke
parents:
diff changeset
   641
  notify(bci, delta, code_length());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   642
489c9b5090e2 Initial load
duke
parents:
diff changeset
   643
  return true;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   644
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   645
489c9b5090e2 Initial load
duke
parents:
diff changeset
   646
// relocate a general instruction. Called by ChangeWiden class
489c9b5090e2 Initial load
duke
parents:
diff changeset
   647
bool Relocator::handle_widen(int bci, int new_ilen, u_char inst_buffer[]) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   648
  int ilen = rc_instr_len(bci);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   649
  if (!relocate_code(bci, ilen, new_ilen - ilen))
489c9b5090e2 Initial load
duke
parents:
diff changeset
   650
    return false;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   651
489c9b5090e2 Initial load
duke
parents:
diff changeset
   652
  // Insert new bytecode(s)
489c9b5090e2 Initial load
duke
parents:
diff changeset
   653
  for(int k = 0; k < new_ilen; k++) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   654
    code_at_put(bci + k, (Bytecodes::Code)inst_buffer[k]);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   655
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   656
489c9b5090e2 Initial load
duke
parents:
diff changeset
   657
  return true;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   658
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   659
489c9b5090e2 Initial load
duke
parents:
diff changeset
   660
// handle jump_widen instruction. Called be ChangeJumpWiden class
489c9b5090e2 Initial load
duke
parents:
diff changeset
   661
bool Relocator::handle_jump_widen(int bci, int delta) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   662
  int ilen = rc_instr_len(bci);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   663
489c9b5090e2 Initial load
duke
parents:
diff changeset
   664
  Bytecodes::Code bc = code_at(bci);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   665
  switch (bc) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   666
    case Bytecodes::_ifeq:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   667
    case Bytecodes::_ifne:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   668
    case Bytecodes::_iflt:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   669
    case Bytecodes::_ifge:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   670
    case Bytecodes::_ifgt:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   671
    case Bytecodes::_ifle:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   672
    case Bytecodes::_if_icmpeq:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   673
    case Bytecodes::_if_icmpne:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   674
    case Bytecodes::_if_icmplt:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   675
    case Bytecodes::_if_icmpge:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   676
    case Bytecodes::_if_icmpgt:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   677
    case Bytecodes::_if_icmple:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   678
    case Bytecodes::_if_acmpeq:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   679
    case Bytecodes::_if_acmpne:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   680
    case Bytecodes::_ifnull:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   681
    case Bytecodes::_ifnonnull: {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   682
      const int goto_length   = Bytecodes::length_for(Bytecodes::_goto);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   683
489c9b5090e2 Initial load
duke
parents:
diff changeset
   684
      // If 'if' points to the next bytecode after goto, it's already handled.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   685
      // it shouldn't be.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   686
      assert (short_at(bci+1) != ilen+goto_length, "if relocation already handled");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   687
      assert(ilen == 3, "check length");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   688
489c9b5090e2 Initial load
duke
parents:
diff changeset
   689
      // Convert to 0 if <cond> goto 6
489c9b5090e2 Initial load
duke
parents:
diff changeset
   690
      //            3 _goto 11
489c9b5090e2 Initial load
duke
parents:
diff changeset
   691
      //            6 _goto_w <wide delta offset>
489c9b5090e2 Initial load
duke
parents:
diff changeset
   692
      //            11 <else code>
489c9b5090e2 Initial load
duke
parents:
diff changeset
   693
      const int goto_w_length = Bytecodes::length_for(Bytecodes::_goto_w);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   694
      const int add_bci = goto_length + goto_w_length;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   695
489c9b5090e2 Initial load
duke
parents:
diff changeset
   696
      if (!relocate_code(bci, 3, /*delta*/add_bci)) return false;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   697
489c9b5090e2 Initial load
duke
parents:
diff changeset
   698
      // if bytecode points to goto_w instruction
489c9b5090e2 Initial load
duke
parents:
diff changeset
   699
      short_at_put(bci + 1, ilen + goto_length);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   700
489c9b5090e2 Initial load
duke
parents:
diff changeset
   701
      int cbci = bci + ilen;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   702
      // goto around
489c9b5090e2 Initial load
duke
parents:
diff changeset
   703
      code_at_put(cbci, Bytecodes::_goto);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   704
      short_at_put(cbci + 1, add_bci);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   705
      // goto_w <wide delta>
489c9b5090e2 Initial load
duke
parents:
diff changeset
   706
      cbci = cbci + goto_length;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   707
      code_at_put(cbci, Bytecodes::_goto_w);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   708
      if (delta > 0) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   709
        delta += 2;                 // goto_w is 2 bytes more than "if" code
489c9b5090e2 Initial load
duke
parents:
diff changeset
   710
      } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   711
        delta -= ilen+goto_length;  // branch starts at goto_w offset
489c9b5090e2 Initial load
duke
parents:
diff changeset
   712
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   713
      int_at_put(cbci + 1, delta);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   714
      break;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   715
489c9b5090e2 Initial load
duke
parents:
diff changeset
   716
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   717
    case Bytecodes::_goto:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   718
    case Bytecodes::_jsr:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   719
      assert(ilen == 3, "check length");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   720
489c9b5090e2 Initial load
duke
parents:
diff changeset
   721
      if (!relocate_code(bci, 3, 2)) return false;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   722
      if (bc == Bytecodes::_goto)
489c9b5090e2 Initial load
duke
parents:
diff changeset
   723
        code_at_put(bci, Bytecodes::_goto_w);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   724
      else
489c9b5090e2 Initial load
duke
parents:
diff changeset
   725
        code_at_put(bci, Bytecodes::_jsr_w);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   726
489c9b5090e2 Initial load
duke
parents:
diff changeset
   727
      // If it's a forward jump, add 2 for the widening.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   728
      if (delta > 0) delta += 2;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   729
      int_at_put(bci + 1, delta);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   730
      break;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   731
489c9b5090e2 Initial load
duke
parents:
diff changeset
   732
    default: ShouldNotReachHere();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   733
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   734
489c9b5090e2 Initial load
duke
parents:
diff changeset
   735
  return true;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   736
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   737
489c9b5090e2 Initial load
duke
parents:
diff changeset
   738
// handle lookup/table switch instructions.  Called be ChangeSwitchPad class
489c9b5090e2 Initial load
duke
parents:
diff changeset
   739
bool Relocator::handle_switch_pad(int bci, int old_pad, bool is_lookup_switch) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   740
  int ilen = rc_instr_len(bci);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   741
  int new_pad = align(bci+1) - (bci+1);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   742
  int pad_delta = new_pad - old_pad;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   743
  if (pad_delta != 0) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   744
    int len;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   745
    if (!is_lookup_switch) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   746
      int low  = int_at(bci+1+old_pad+4);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   747
      int high = int_at(bci+1+old_pad+8);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   748
      len = high-low+1 + 3; // 3 for default, hi, lo.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   749
    } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   750
      int npairs = int_at(bci+1+old_pad+4);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   751
      len = npairs*2 + 2; // 2 for default, npairs.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   752
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   753
    // Because "relocateCode" does a "changeJumps" loop,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   754
    // which parses instructions to determine their length,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   755
    // we need to call that before messing with the current
489c9b5090e2 Initial load
duke
parents:
diff changeset
   756
    // instruction.  Since it may also overwrite the current
489c9b5090e2 Initial load
duke
parents:
diff changeset
   757
    // instruction when moving down, remember the possibly
489c9b5090e2 Initial load
duke
parents:
diff changeset
   758
    // overwritten part.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   759
489c9b5090e2 Initial load
duke
parents:
diff changeset
   760
    // Move the code following the instruction...
489c9b5090e2 Initial load
duke
parents:
diff changeset
   761
    if (!relocate_code(bci, ilen, pad_delta)) return false;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   762
489c9b5090e2 Initial load
duke
parents:
diff changeset
   763
    if (pad_delta < 0) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   764
      // Move the shrunken instruction down.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   765
      memmove(addr_at(bci + 1 + new_pad),
489c9b5090e2 Initial load
duke
parents:
diff changeset
   766
              addr_at(bci + 1 + old_pad),
489c9b5090e2 Initial load
duke
parents:
diff changeset
   767
              len * 4 + pad_delta);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   768
      memmove(addr_at(bci + 1 + new_pad + len*4 + pad_delta),
489c9b5090e2 Initial load
duke
parents:
diff changeset
   769
              _overwrite, -pad_delta);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   770
    } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   771
      assert(pad_delta > 0, "check");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   772
      // Move the expanded instruction up.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   773
      memmove(addr_at(bci +1 + new_pad),
489c9b5090e2 Initial load
duke
parents:
diff changeset
   774
              addr_at(bci +1 + old_pad),
489c9b5090e2 Initial load
duke
parents:
diff changeset
   775
              len * 4);
6974
a013f1c533a5 6991315: RedefineClasses fails with java.lang.VerifyError
kamg
parents: 5547
diff changeset
   776
      memset(addr_at(bci + 1), 0, new_pad); // pad must be 0
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   777
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   778
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   779
  return true;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   780
}