src/share/vm/oops/method.cpp

changeset 4037
da91efe96a93
parent 3970
977007096840
child 4047
aed758eda82a
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/share/vm/oops/method.cpp	Sat Sep 01 13:25:18 2012 -0400
     1.3 @@ -0,0 +1,1897 @@
     1.4 +/*
     1.5 + * Copyright (c) 1997, 2012, Oracle and/or its affiliates. All rights reserved.
     1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     1.7 + *
     1.8 + * This code is free software; you can redistribute it and/or modify it
     1.9 + * under the terms of the GNU General Public License version 2 only, as
    1.10 + * published by the Free Software Foundation.
    1.11 + *
    1.12 + * This code is distributed in the hope that it will be useful, but WITHOUT
    1.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    1.14 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    1.15 + * version 2 for more details (a copy is included in the LICENSE file that
    1.16 + * accompanied this code).
    1.17 + *
    1.18 + * You should have received a copy of the GNU General Public License version
    1.19 + * 2 along with this work; if not, write to the Free Software Foundation,
    1.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    1.21 + *
    1.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    1.23 + * or visit www.oracle.com if you need additional information or have any
    1.24 + * questions.
    1.25 + *
    1.26 + */
    1.27 +
    1.28 +#include "precompiled.hpp"
    1.29 +#include "classfile/systemDictionary.hpp"
    1.30 +#include "code/debugInfoRec.hpp"
    1.31 +#include "gc_interface/collectedHeap.inline.hpp"
    1.32 +#include "interpreter/bytecodeStream.hpp"
    1.33 +#include "interpreter/bytecodeTracer.hpp"
    1.34 +#include "interpreter/bytecodes.hpp"
    1.35 +#include "interpreter/interpreter.hpp"
    1.36 +#include "interpreter/oopMapCache.hpp"
    1.37 +#include "memory/gcLocker.hpp"
    1.38 +#include "memory/generation.hpp"
    1.39 +#include "memory/metadataFactory.hpp"
    1.40 +#include "memory/oopFactory.hpp"
    1.41 +#include "oops/methodData.hpp"
    1.42 +#include "oops/method.hpp"
    1.43 +#include "oops/oop.inline.hpp"
    1.44 +#include "oops/symbol.hpp"
    1.45 +#include "prims/jvmtiExport.hpp"
    1.46 +#include "prims/jvmtiRedefineClasses.hpp"
    1.47 +#include "prims/methodHandles.hpp"
    1.48 +#include "prims/nativeLookup.hpp"
    1.49 +#include "runtime/arguments.hpp"
    1.50 +#include "runtime/compilationPolicy.hpp"
    1.51 +#include "runtime/frame.inline.hpp"
    1.52 +#include "runtime/handles.inline.hpp"
    1.53 +#include "runtime/relocator.hpp"
    1.54 +#include "runtime/sharedRuntime.hpp"
    1.55 +#include "runtime/signature.hpp"
    1.56 +#include "utilities/quickSort.hpp"
    1.57 +#include "utilities/xmlstream.hpp"
    1.58 +
    1.59 +
    1.60 +// Implementation of Method
    1.61 +
    1.62 +Method* Method::allocate(ClassLoaderData* loader_data,
    1.63 +                            int byte_code_size,
    1.64 +                            AccessFlags access_flags,
    1.65 +                            int compressed_line_number_size,
    1.66 +                            int localvariable_table_length,
    1.67 +                            int exception_table_length,
    1.68 +                            int checked_exceptions_length,
    1.69 +                            TRAPS) {
    1.70 +  assert(!access_flags.is_native() || byte_code_size == 0,
    1.71 +         "native methods should not contain byte codes");
    1.72 +  ConstMethod* cm = ConstMethod::allocate(loader_data,
    1.73 +                                      byte_code_size,
    1.74 +                                      compressed_line_number_size,
    1.75 +                                      localvariable_table_length,
    1.76 +                                      exception_table_length,
    1.77 +                                      checked_exceptions_length,
    1.78 +                                      CHECK_NULL);
    1.79 +
    1.80 +  int size = Method::size(access_flags.is_native());
    1.81 +
    1.82 +  return new (loader_data, size, false, THREAD) Method(cm, access_flags, size);
    1.83 +}
    1.84 +
    1.85 +Method::Method(ConstMethod* xconst,
    1.86 +                             AccessFlags access_flags, int size) {
    1.87 +  No_Safepoint_Verifier no_safepoint;
    1.88 +  set_constMethod(xconst);
    1.89 +  set_access_flags(access_flags);
    1.90 +  set_method_size(size);
    1.91 +  set_name_index(0);
    1.92 +  set_signature_index(0);
    1.93 +#ifdef CC_INTERP
    1.94 +  set_result_index(T_VOID);
    1.95 +#endif
    1.96 +  set_constants(NULL);
    1.97 +  set_max_stack(0);
    1.98 +  set_max_locals(0);
    1.99 +  set_intrinsic_id(vmIntrinsics::_none);
   1.100 +  set_jfr_towrite(false);
   1.101 +  set_method_data(NULL);
   1.102 +  set_interpreter_throwout_count(0);
   1.103 +  set_vtable_index(Method::garbage_vtable_index);
   1.104 +
   1.105 +  // Fix and bury in Method*
   1.106 +  set_interpreter_entry(NULL); // sets i2i entry and from_int
   1.107 +  set_adapter_entry(NULL);
   1.108 +  clear_code(); // from_c/from_i get set to c2i/i2i
   1.109 +
   1.110 +  if (access_flags.is_native()) {
   1.111 +    clear_native_function();
   1.112 +    set_signature_handler(NULL);
   1.113 +  }
   1.114 +
   1.115 +  NOT_PRODUCT(set_compiled_invocation_count(0);)
   1.116 +  set_interpreter_invocation_count(0);
   1.117 +  invocation_counter()->init();
   1.118 +  backedge_counter()->init();
   1.119 +  clear_number_of_breakpoints();
   1.120 +
   1.121 +#ifdef TIERED
   1.122 +  set_rate(0);
   1.123 +  set_prev_event_count(0);
   1.124 +  set_prev_time(0);
   1.125 +#endif
   1.126 +}
   1.127 +
   1.128 +// Release Method*.  The nmethod will be gone when we get here because
   1.129 +// we've walked the code cache.
   1.130 +void Method::deallocate_contents(ClassLoaderData* loader_data) {
   1.131 +  MetadataFactory::free_metadata(loader_data, constMethod());
   1.132 +  set_constMethod(NULL);
   1.133 +  MetadataFactory::free_metadata(loader_data, method_data());
   1.134 +  set_method_data(NULL);
   1.135 +  // The nmethod will be gone when we get here.
   1.136 +  if (code() != NULL) _code = NULL;
   1.137 +}
   1.138 +
   1.139 +address Method::get_i2c_entry() {
   1.140 +  assert(_adapter != NULL, "must have");
   1.141 +  return _adapter->get_i2c_entry();
   1.142 +}
   1.143 +
   1.144 +address Method::get_c2i_entry() {
   1.145 +  assert(_adapter != NULL, "must have");
   1.146 +  return _adapter->get_c2i_entry();
   1.147 +}
   1.148 +
   1.149 +address Method::get_c2i_unverified_entry() {
   1.150 +  assert(_adapter != NULL, "must have");
   1.151 +  return _adapter->get_c2i_unverified_entry();
   1.152 +}
   1.153 +
   1.154 +char* Method::name_and_sig_as_C_string() const {
   1.155 +  return name_and_sig_as_C_string(Klass::cast(constants()->pool_holder()), name(), signature());
   1.156 +}
   1.157 +
   1.158 +char* Method::name_and_sig_as_C_string(char* buf, int size) const {
   1.159 +  return name_and_sig_as_C_string(Klass::cast(constants()->pool_holder()), name(), signature(), buf, size);
   1.160 +}
   1.161 +
   1.162 +char* Method::name_and_sig_as_C_string(Klass* klass, Symbol* method_name, Symbol* signature) {
   1.163 +  const char* klass_name = klass->external_name();
   1.164 +  int klass_name_len  = (int)strlen(klass_name);
   1.165 +  int method_name_len = method_name->utf8_length();
   1.166 +  int len             = klass_name_len + 1 + method_name_len + signature->utf8_length();
   1.167 +  char* dest          = NEW_RESOURCE_ARRAY(char, len + 1);
   1.168 +  strcpy(dest, klass_name);
   1.169 +  dest[klass_name_len] = '.';
   1.170 +  strcpy(&dest[klass_name_len + 1], method_name->as_C_string());
   1.171 +  strcpy(&dest[klass_name_len + 1 + method_name_len], signature->as_C_string());
   1.172 +  dest[len] = 0;
   1.173 +  return dest;
   1.174 +}
   1.175 +
   1.176 +char* Method::name_and_sig_as_C_string(Klass* klass, Symbol* method_name, Symbol* signature, char* buf, int size) {
   1.177 +  Symbol* klass_name = klass->name();
   1.178 +  klass_name->as_klass_external_name(buf, size);
   1.179 +  int len = (int)strlen(buf);
   1.180 +
   1.181 +  if (len < size - 1) {
   1.182 +    buf[len++] = '.';
   1.183 +
   1.184 +    method_name->as_C_string(&(buf[len]), size - len);
   1.185 +    len = (int)strlen(buf);
   1.186 +
   1.187 +    signature->as_C_string(&(buf[len]), size - len);
   1.188 +  }
   1.189 +
   1.190 +  return buf;
   1.191 +}
   1.192 +
   1.193 +int  Method::fast_exception_handler_bci_for(KlassHandle ex_klass, int throw_bci, TRAPS) {
   1.194 +  // exception table holds quadruple entries of the form (beg_bci, end_bci, handler_bci, klass_index)
   1.195 +  // access exception table
   1.196 +  ExceptionTable table(this);
   1.197 +  int length = table.length();
   1.198 +  // iterate through all entries sequentially
   1.199 +  constantPoolHandle pool(THREAD, constants());
   1.200 +  for (int i = 0; i < length; i ++) {
   1.201 +    //reacquire the table in case a GC happened
   1.202 +    ExceptionTable table(this);
   1.203 +    int beg_bci = table.start_pc(i);
   1.204 +    int end_bci = table.end_pc(i);
   1.205 +    assert(beg_bci <= end_bci, "inconsistent exception table");
   1.206 +    if (beg_bci <= throw_bci && throw_bci < end_bci) {
   1.207 +      // exception handler bci range covers throw_bci => investigate further
   1.208 +      int handler_bci = table.handler_pc(i);
   1.209 +      int klass_index = table.catch_type_index(i);
   1.210 +      if (klass_index == 0) {
   1.211 +        return handler_bci;
   1.212 +      } else if (ex_klass.is_null()) {
   1.213 +        return handler_bci;
   1.214 +      } else {
   1.215 +        // we know the exception class => get the constraint class
   1.216 +        // this may require loading of the constraint class; if verification
   1.217 +        // fails or some other exception occurs, return handler_bci
   1.218 +        Klass* k = pool->klass_at(klass_index, CHECK_(handler_bci));
   1.219 +        KlassHandle klass = KlassHandle(THREAD, k);
   1.220 +        assert(klass.not_null(), "klass not loaded");
   1.221 +        if (ex_klass->is_subtype_of(klass())) {
   1.222 +          return handler_bci;
   1.223 +        }
   1.224 +      }
   1.225 +    }
   1.226 +  }
   1.227 +
   1.228 +  return -1;
   1.229 +}
   1.230 +
   1.231 +void Method::mask_for(int bci, InterpreterOopMap* mask) {
   1.232 +
   1.233 +  Thread* myThread    = Thread::current();
   1.234 +  methodHandle h_this(myThread, this);
   1.235 +#ifdef ASSERT
   1.236 +  bool has_capability = myThread->is_VM_thread() ||
   1.237 +                        myThread->is_ConcurrentGC_thread() ||
   1.238 +                        myThread->is_GC_task_thread();
   1.239 +
   1.240 +  if (!has_capability) {
   1.241 +    if (!VerifyStack && !VerifyLastFrame) {
   1.242 +      // verify stack calls this outside VM thread
   1.243 +      warning("oopmap should only be accessed by the "
   1.244 +              "VM, GC task or CMS threads (or during debugging)");
   1.245 +      InterpreterOopMap local_mask;
   1.246 +      InstanceKlass::cast(method_holder())->mask_for(h_this, bci, &local_mask);
   1.247 +      local_mask.print();
   1.248 +    }
   1.249 +  }
   1.250 +#endif
   1.251 +  InstanceKlass::cast(method_holder())->mask_for(h_this, bci, mask);
   1.252 +  return;
   1.253 +}
   1.254 +
   1.255 +
   1.256 +int Method::bci_from(address bcp) const {
   1.257 +  assert(is_native() && bcp == code_base() || contains(bcp) || is_error_reported(),
   1.258 +         err_msg("bcp doesn't belong to this method: bcp: " INTPTR_FORMAT ", method: %s", bcp, name_and_sig_as_C_string()));
   1.259 +  return bcp - code_base();
   1.260 +}
   1.261 +
   1.262 +
   1.263 +// Return (int)bcx if it appears to be a valid BCI.
   1.264 +// Return bci_from((address)bcx) if it appears to be a valid BCP.
   1.265 +// Return -1 otherwise.
   1.266 +// Used by profiling code, when invalid data is a possibility.
   1.267 +// The caller is responsible for validating the Method* itself.
   1.268 +int Method::validate_bci_from_bcx(intptr_t bcx) const {
   1.269 +  // keep bci as -1 if not a valid bci
   1.270 +  int bci = -1;
   1.271 +  if (bcx == 0 || (address)bcx == code_base()) {
   1.272 +    // code_size() may return 0 and we allow 0 here
   1.273 +    // the method may be native
   1.274 +    bci = 0;
   1.275 +  } else if (frame::is_bci(bcx)) {
   1.276 +    if (bcx < code_size()) {
   1.277 +      bci = (int)bcx;
   1.278 +    }
   1.279 +  } else if (contains((address)bcx)) {
   1.280 +    bci = (address)bcx - code_base();
   1.281 +  }
   1.282 +  // Assert that if we have dodged any asserts, bci is negative.
   1.283 +  assert(bci == -1 || bci == bci_from(bcp_from(bci)), "sane bci if >=0");
   1.284 +  return bci;
   1.285 +}
   1.286 +
   1.287 +address Method::bcp_from(int bci) const {
   1.288 +  assert((is_native() && bci == 0)  || (!is_native() && 0 <= bci && bci < code_size()), "illegal bci");
   1.289 +  address bcp = code_base() + bci;
   1.290 +  assert(is_native() && bcp == code_base() || contains(bcp), "bcp doesn't belong to this method");
   1.291 +  return bcp;
   1.292 +}
   1.293 +
   1.294 +
   1.295 +int Method::size(bool is_native) {
   1.296 +  // If native, then include pointers for native_function and signature_handler
   1.297 +  int extra_bytes = (is_native) ? 2*sizeof(address*) : 0;
   1.298 +  int extra_words = align_size_up(extra_bytes, BytesPerWord) / BytesPerWord;
   1.299 +  return align_object_size(header_size() + extra_words);
   1.300 +}
   1.301 +
   1.302 +
   1.303 +Symbol* Method::klass_name() const {
   1.304 +  Klass* k = method_holder();
   1.305 +  assert(k->is_klass(), "must be klass");
   1.306 +  InstanceKlass* ik = (InstanceKlass*) k;
   1.307 +  return ik->name();
   1.308 +}
   1.309 +
   1.310 +
   1.311 +void Method::set_interpreter_kind() {
   1.312 +  int kind = Interpreter::method_kind(this);
   1.313 +  assert(kind != Interpreter::invalid,
   1.314 +         "interpreter entry must be valid");
   1.315 +  set_interpreter_kind(kind);
   1.316 +}
   1.317 +
   1.318 +
   1.319 +// Attempt to return method oop to original state.  Clear any pointers
   1.320 +// (to objects outside the shared spaces).  We won't be able to predict
   1.321 +// where they should point in a new JVM.  Further initialize some
   1.322 +// entries now in order allow them to be write protected later.
   1.323 +
   1.324 +void Method::remove_unshareable_info() {
   1.325 +  unlink_method();
   1.326 +  set_interpreter_kind();
   1.327 +}
   1.328 +
   1.329 +
   1.330 +bool Method::was_executed_more_than(int n) {
   1.331 +  // Invocation counter is reset when the Method* is compiled.
   1.332 +  // If the method has compiled code we therefore assume it has
   1.333 +  // be excuted more than n times.
   1.334 +  if (is_accessor() || is_empty_method() || (code() != NULL)) {
   1.335 +    // interpreter doesn't bump invocation counter of trivial methods
   1.336 +    // compiler does not bump invocation counter of compiled methods
   1.337 +    return true;
   1.338 +  }
   1.339 +  else if (_invocation_counter.carry() || (method_data() != NULL && method_data()->invocation_counter()->carry())) {
   1.340 +    // The carry bit is set when the counter overflows and causes
   1.341 +    // a compilation to occur.  We don't know how many times
   1.342 +    // the counter has been reset, so we simply assume it has
   1.343 +    // been executed more than n times.
   1.344 +    return true;
   1.345 +  } else {
   1.346 +    return invocation_count() > n;
   1.347 +  }
   1.348 +}
   1.349 +
   1.350 +#ifndef PRODUCT
   1.351 +void Method::print_invocation_count() {
   1.352 +  if (is_static()) tty->print("static ");
   1.353 +  if (is_final()) tty->print("final ");
   1.354 +  if (is_synchronized()) tty->print("synchronized ");
   1.355 +  if (is_native()) tty->print("native ");
   1.356 +  method_holder()->name()->print_symbol_on(tty);
   1.357 +  tty->print(".");
   1.358 +  name()->print_symbol_on(tty);
   1.359 +  signature()->print_symbol_on(tty);
   1.360 +
   1.361 +  if (WizardMode) {
   1.362 +    // dump the size of the byte codes
   1.363 +    tty->print(" {%d}", code_size());
   1.364 +  }
   1.365 +  tty->cr();
   1.366 +
   1.367 +  tty->print_cr ("  interpreter_invocation_count: %8d ", interpreter_invocation_count());
   1.368 +  tty->print_cr ("  invocation_counter:           %8d ", invocation_count());
   1.369 +  tty->print_cr ("  backedge_counter:             %8d ", backedge_count());
   1.370 +  if (CountCompiledCalls) {
   1.371 +    tty->print_cr ("  compiled_invocation_count: %8d ", compiled_invocation_count());
   1.372 +  }
   1.373 +
   1.374 +}
   1.375 +#endif
   1.376 +
   1.377 +// Build a MethodData* object to hold information about this method
   1.378 +// collected in the interpreter.
   1.379 +void Method::build_interpreter_method_data(methodHandle method, TRAPS) {
   1.380 +  // Do not profile method if current thread holds the pending list lock,
   1.381 +  // which avoids deadlock for acquiring the MethodData_lock.
   1.382 +  if (instanceRefKlass::owns_pending_list_lock((JavaThread*)THREAD)) {
   1.383 +    return;
   1.384 +  }
   1.385 +
   1.386 +  // Grab a lock here to prevent multiple
   1.387 +  // MethodData*s from being created.
   1.388 +  MutexLocker ml(MethodData_lock, THREAD);
   1.389 +  if (method->method_data() == NULL) {
   1.390 +    ClassLoaderData* loader_data = method->method_holder()->class_loader_data();
   1.391 +    MethodData* method_data = MethodData::allocate(loader_data, method, CHECK);
   1.392 +    method->set_method_data(method_data);
   1.393 +    if (PrintMethodData && (Verbose || WizardMode)) {
   1.394 +      ResourceMark rm(THREAD);
   1.395 +      tty->print("build_interpreter_method_data for ");
   1.396 +      method->print_name(tty);
   1.397 +      tty->cr();
   1.398 +      // At the end of the run, the MDO, full of data, will be dumped.
   1.399 +    }
   1.400 +  }
   1.401 +}
   1.402 +
   1.403 +void Method::cleanup_inline_caches() {
   1.404 +  // The current system doesn't use inline caches in the interpreter
   1.405 +  // => nothing to do (keep this method around for future use)
   1.406 +}
   1.407 +
   1.408 +
   1.409 +int Method::extra_stack_words() {
   1.410 +  // not an inline function, to avoid a header dependency on Interpreter
   1.411 +  return extra_stack_entries() * Interpreter::stackElementSize;
   1.412 +}
   1.413 +
   1.414 +
   1.415 +void Method::compute_size_of_parameters(Thread *thread) {
   1.416 +  ArgumentSizeComputer asc(signature());
   1.417 +  set_size_of_parameters(asc.size() + (is_static() ? 0 : 1));
   1.418 +}
   1.419 +
   1.420 +#ifdef CC_INTERP
   1.421 +void Method::set_result_index(BasicType type)          {
   1.422 +  _result_index = Interpreter::BasicType_as_index(type);
   1.423 +}
   1.424 +#endif
   1.425 +
   1.426 +BasicType Method::result_type() const {
   1.427 +  ResultTypeFinder rtf(signature());
   1.428 +  return rtf.type();
   1.429 +}
   1.430 +
   1.431 +
   1.432 +bool Method::is_empty_method() const {
   1.433 +  return  code_size() == 1
   1.434 +      && *code_base() == Bytecodes::_return;
   1.435 +}
   1.436 +
   1.437 +
   1.438 +bool Method::is_vanilla_constructor() const {
   1.439 +  // Returns true if this method is a vanilla constructor, i.e. an "<init>" "()V" method
   1.440 +  // which only calls the superclass vanilla constructor and possibly does stores of
   1.441 +  // zero constants to local fields:
   1.442 +  //
   1.443 +  //   aload_0
   1.444 +  //   invokespecial
   1.445 +  //   indexbyte1
   1.446 +  //   indexbyte2
   1.447 +  //
   1.448 +  // followed by an (optional) sequence of:
   1.449 +  //
   1.450 +  //   aload_0
   1.451 +  //   aconst_null / iconst_0 / fconst_0 / dconst_0
   1.452 +  //   putfield
   1.453 +  //   indexbyte1
   1.454 +  //   indexbyte2
   1.455 +  //
   1.456 +  // followed by:
   1.457 +  //
   1.458 +  //   return
   1.459 +
   1.460 +  assert(name() == vmSymbols::object_initializer_name(),    "Should only be called for default constructors");
   1.461 +  assert(signature() == vmSymbols::void_method_signature(), "Should only be called for default constructors");
   1.462 +  int size = code_size();
   1.463 +  // Check if size match
   1.464 +  if (size == 0 || size % 5 != 0) return false;
   1.465 +  address cb = code_base();
   1.466 +  int last = size - 1;
   1.467 +  if (cb[0] != Bytecodes::_aload_0 || cb[1] != Bytecodes::_invokespecial || cb[last] != Bytecodes::_return) {
   1.468 +    // Does not call superclass default constructor
   1.469 +    return false;
   1.470 +  }
   1.471 +  // Check optional sequence
   1.472 +  for (int i = 4; i < last; i += 5) {
   1.473 +    if (cb[i] != Bytecodes::_aload_0) return false;
   1.474 +    if (!Bytecodes::is_zero_const(Bytecodes::cast(cb[i+1]))) return false;
   1.475 +    if (cb[i+2] != Bytecodes::_putfield) return false;
   1.476 +  }
   1.477 +  return true;
   1.478 +}
   1.479 +
   1.480 +
   1.481 +bool Method::compute_has_loops_flag() {
   1.482 +  BytecodeStream bcs(this);
   1.483 +  Bytecodes::Code bc;
   1.484 +
   1.485 +  while ((bc = bcs.next()) >= 0) {
   1.486 +    switch( bc ) {
   1.487 +      case Bytecodes::_ifeq:
   1.488 +      case Bytecodes::_ifnull:
   1.489 +      case Bytecodes::_iflt:
   1.490 +      case Bytecodes::_ifle:
   1.491 +      case Bytecodes::_ifne:
   1.492 +      case Bytecodes::_ifnonnull:
   1.493 +      case Bytecodes::_ifgt:
   1.494 +      case Bytecodes::_ifge:
   1.495 +      case Bytecodes::_if_icmpeq:
   1.496 +      case Bytecodes::_if_icmpne:
   1.497 +      case Bytecodes::_if_icmplt:
   1.498 +      case Bytecodes::_if_icmpgt:
   1.499 +      case Bytecodes::_if_icmple:
   1.500 +      case Bytecodes::_if_icmpge:
   1.501 +      case Bytecodes::_if_acmpeq:
   1.502 +      case Bytecodes::_if_acmpne:
   1.503 +      case Bytecodes::_goto:
   1.504 +      case Bytecodes::_jsr:
   1.505 +        if( bcs.dest() < bcs.next_bci() ) _access_flags.set_has_loops();
   1.506 +        break;
   1.507 +
   1.508 +      case Bytecodes::_goto_w:
   1.509 +      case Bytecodes::_jsr_w:
   1.510 +        if( bcs.dest_w() < bcs.next_bci() ) _access_flags.set_has_loops();
   1.511 +        break;
   1.512 +    }
   1.513 +  }
   1.514 +  _access_flags.set_loops_flag_init();
   1.515 +  return _access_flags.has_loops();
   1.516 +}
   1.517 +
   1.518 +
   1.519 +bool Method::is_final_method() const {
   1.520 +  // %%% Should return true for private methods also,
   1.521 +  // since there is no way to override them.
   1.522 +  return is_final() || Klass::cast(method_holder())->is_final();
   1.523 +}
   1.524 +
   1.525 +
   1.526 +bool Method::is_strict_method() const {
   1.527 +  return is_strict();
   1.528 +}
   1.529 +
   1.530 +
   1.531 +bool Method::can_be_statically_bound() const {
   1.532 +  if (is_final_method())  return true;
   1.533 +  return vtable_index() == nonvirtual_vtable_index;
   1.534 +}
   1.535 +
   1.536 +
   1.537 +bool Method::is_accessor() const {
   1.538 +  if (code_size() != 5) return false;
   1.539 +  if (size_of_parameters() != 1) return false;
   1.540 +  if (java_code_at(0) != Bytecodes::_aload_0 ) return false;
   1.541 +  if (java_code_at(1) != Bytecodes::_getfield) return false;
   1.542 +  if (java_code_at(4) != Bytecodes::_areturn &&
   1.543 +      java_code_at(4) != Bytecodes::_ireturn ) return false;
   1.544 +  return true;
   1.545 +}
   1.546 +
   1.547 +
   1.548 +bool Method::is_initializer() const {
   1.549 +  return name() == vmSymbols::object_initializer_name() || is_static_initializer();
   1.550 +}
   1.551 +
   1.552 +bool Method::has_valid_initializer_flags() const {
   1.553 +  return (is_static() ||
   1.554 +          InstanceKlass::cast(method_holder())->major_version() < 51);
   1.555 +}
   1.556 +
   1.557 +bool Method::is_static_initializer() const {
   1.558 +  // For classfiles version 51 or greater, ensure that the clinit method is
   1.559 +  // static.  Non-static methods with the name "<clinit>" are not static
   1.560 +  // initializers. (older classfiles exempted for backward compatibility)
   1.561 +  return name() == vmSymbols::class_initializer_name() &&
   1.562 +         has_valid_initializer_flags();
   1.563 +}
   1.564 +
   1.565 +
   1.566 +objArrayHandle Method::resolved_checked_exceptions_impl(Method* this_oop, TRAPS) {
   1.567 +  int length = this_oop->checked_exceptions_length();
   1.568 +  if (length == 0) {  // common case
   1.569 +    return objArrayHandle(THREAD, Universe::the_empty_class_klass_array());
   1.570 +  } else {
   1.571 +    methodHandle h_this(THREAD, this_oop);
   1.572 +    objArrayOop m_oop = oopFactory::new_objArray(SystemDictionary::Class_klass(), length, CHECK_(objArrayHandle()));
   1.573 +    objArrayHandle mirrors (THREAD, m_oop);
   1.574 +    for (int i = 0; i < length; i++) {
   1.575 +      CheckedExceptionElement* table = h_this->checked_exceptions_start(); // recompute on each iteration, not gc safe
   1.576 +      Klass* k = h_this->constants()->klass_at(table[i].class_cp_index, CHECK_(objArrayHandle()));
   1.577 +      assert(Klass::cast(k)->is_subclass_of(SystemDictionary::Throwable_klass()), "invalid exception class");
   1.578 +      mirrors->obj_at_put(i, Klass::cast(k)->java_mirror());
   1.579 +    }
   1.580 +    return mirrors;
   1.581 +  }
   1.582 +};
   1.583 +
   1.584 +
   1.585 +int Method::line_number_from_bci(int bci) const {
   1.586 +  if (bci == SynchronizationEntryBCI) bci = 0;
   1.587 +  assert(bci == 0 || 0 <= bci && bci < code_size(), "illegal bci");
   1.588 +  int best_bci  =  0;
   1.589 +  int best_line = -1;
   1.590 +
   1.591 +  if (has_linenumber_table()) {
   1.592 +    // The line numbers are a short array of 2-tuples [start_pc, line_number].
   1.593 +    // Not necessarily sorted and not necessarily one-to-one.
   1.594 +    CompressedLineNumberReadStream stream(compressed_linenumber_table());
   1.595 +    while (stream.read_pair()) {
   1.596 +      if (stream.bci() == bci) {
   1.597 +        // perfect match
   1.598 +        return stream.line();
   1.599 +      } else {
   1.600 +        // update best_bci/line
   1.601 +        if (stream.bci() < bci && stream.bci() >= best_bci) {
   1.602 +          best_bci  = stream.bci();
   1.603 +          best_line = stream.line();
   1.604 +        }
   1.605 +      }
   1.606 +    }
   1.607 +  }
   1.608 +  return best_line;
   1.609 +}
   1.610 +
   1.611 +
   1.612 +bool Method::is_klass_loaded_by_klass_index(int klass_index) const {
   1.613 +  if( constants()->tag_at(klass_index).is_unresolved_klass() ) {
   1.614 +    Thread *thread = Thread::current();
   1.615 +    Symbol* klass_name = constants()->klass_name_at(klass_index);
   1.616 +    Handle loader(thread, InstanceKlass::cast(method_holder())->class_loader());
   1.617 +    Handle prot  (thread, Klass::cast(method_holder())->protection_domain());
   1.618 +    return SystemDictionary::find(klass_name, loader, prot, thread) != NULL;
   1.619 +  } else {
   1.620 +    return true;
   1.621 +  }
   1.622 +}
   1.623 +
   1.624 +
   1.625 +bool Method::is_klass_loaded(int refinfo_index, bool must_be_resolved) const {
   1.626 +  int klass_index = constants()->klass_ref_index_at(refinfo_index);
   1.627 +  if (must_be_resolved) {
   1.628 +    // Make sure klass is resolved in constantpool.
   1.629 +    if (constants()->tag_at(klass_index).is_unresolved_klass()) return false;
   1.630 +  }
   1.631 +  return is_klass_loaded_by_klass_index(klass_index);
   1.632 +}
   1.633 +
   1.634 +
   1.635 +void Method::set_native_function(address function, bool post_event_flag) {
   1.636 +  assert(function != NULL, "use clear_native_function to unregister natives");
   1.637 +  assert(!is_method_handle_intrinsic() || function == SharedRuntime::native_method_throw_unsatisfied_link_error_entry(), "");
   1.638 +  address* native_function = native_function_addr();
   1.639 +
   1.640 +  // We can see racers trying to place the same native function into place. Once
   1.641 +  // is plenty.
   1.642 +  address current = *native_function;
   1.643 +  if (current == function) return;
   1.644 +  if (post_event_flag && JvmtiExport::should_post_native_method_bind() &&
   1.645 +      function != NULL) {
   1.646 +    // native_method_throw_unsatisfied_link_error_entry() should only
   1.647 +    // be passed when post_event_flag is false.
   1.648 +    assert(function !=
   1.649 +      SharedRuntime::native_method_throw_unsatisfied_link_error_entry(),
   1.650 +      "post_event_flag mis-match");
   1.651 +
   1.652 +    // post the bind event, and possible change the bind function
   1.653 +    JvmtiExport::post_native_method_bind(this, &function);
   1.654 +  }
   1.655 +  *native_function = function;
   1.656 +  // This function can be called more than once. We must make sure that we always
   1.657 +  // use the latest registered method -> check if a stub already has been generated.
   1.658 +  // If so, we have to make it not_entrant.
   1.659 +  nmethod* nm = code(); // Put it into local variable to guard against concurrent updates
   1.660 +  if (nm != NULL) {
   1.661 +    nm->make_not_entrant();
   1.662 +  }
   1.663 +}
   1.664 +
   1.665 +
   1.666 +bool Method::has_native_function() const {
   1.667 +  if (is_method_handle_intrinsic())
   1.668 +    return false;  // special-cased in SharedRuntime::generate_native_wrapper
   1.669 +  address func = native_function();
   1.670 +  return (func != NULL && func != SharedRuntime::native_method_throw_unsatisfied_link_error_entry());
   1.671 +}
   1.672 +
   1.673 +
   1.674 +void Method::clear_native_function() {
   1.675 +  // Note: is_method_handle_intrinsic() is allowed here.
   1.676 +  set_native_function(
   1.677 +    SharedRuntime::native_method_throw_unsatisfied_link_error_entry(),
   1.678 +    !native_bind_event_is_interesting);
   1.679 +  clear_code();
   1.680 +}
   1.681 +
   1.682 +address Method::critical_native_function() {
   1.683 +  methodHandle mh(this);
   1.684 +  return NativeLookup::lookup_critical_entry(mh);
   1.685 +}
   1.686 +
   1.687 +
   1.688 +void Method::set_signature_handler(address handler) {
   1.689 +  address* signature_handler =  signature_handler_addr();
   1.690 +  *signature_handler = handler;
   1.691 +}
   1.692 +
   1.693 +
   1.694 +bool Method::is_not_compilable(int comp_level) const {
   1.695 +  if (number_of_breakpoints() > 0) {
   1.696 +    return true;
   1.697 +  }
   1.698 +  if (is_method_handle_intrinsic()) {
   1.699 +    return !is_synthetic();  // the generated adapters must be compiled
   1.700 +  }
   1.701 +  if (comp_level == CompLevel_any) {
   1.702 +    return is_not_c1_compilable() || is_not_c2_compilable();
   1.703 +  }
   1.704 +  if (is_c1_compile(comp_level)) {
   1.705 +    return is_not_c1_compilable();
   1.706 +  }
   1.707 +  if (is_c2_compile(comp_level)) {
   1.708 +    return is_not_c2_compilable();
   1.709 +  }
   1.710 +  return false;
   1.711 +}
   1.712 +
   1.713 +// call this when compiler finds that this method is not compilable
   1.714 +void Method::set_not_compilable(int comp_level, bool report) {
   1.715 +  if (PrintCompilation && report) {
   1.716 +    ttyLocker ttyl;
   1.717 +    tty->print("made not compilable ");
   1.718 +    this->print_short_name(tty);
   1.719 +    int size = this->code_size();
   1.720 +    if (size > 0)
   1.721 +      tty->print(" (%d bytes)", size);
   1.722 +    tty->cr();
   1.723 +  }
   1.724 +  if ((TraceDeoptimization || LogCompilation) && (xtty != NULL)) {
   1.725 +    ttyLocker ttyl;
   1.726 +    xtty->begin_elem("make_not_compilable thread='%d'", (int) os::current_thread_id());
   1.727 +    xtty->method(this);
   1.728 +    xtty->stamp();
   1.729 +    xtty->end_elem();
   1.730 +  }
   1.731 +  if (comp_level == CompLevel_all) {
   1.732 +    set_not_c1_compilable();
   1.733 +    set_not_c2_compilable();
   1.734 +  } else {
   1.735 +    if (is_c1_compile(comp_level)) {
   1.736 +      set_not_c1_compilable();
   1.737 +    } else
   1.738 +      if (is_c2_compile(comp_level)) {
   1.739 +        set_not_c2_compilable();
   1.740 +      }
   1.741 +  }
   1.742 +  CompilationPolicy::policy()->disable_compilation(this);
   1.743 +}
   1.744 +
   1.745 +// Revert to using the interpreter and clear out the nmethod
   1.746 +void Method::clear_code() {
   1.747 +
   1.748 +  // this may be NULL if c2i adapters have not been made yet
   1.749 +  // Only should happen at allocate time.
   1.750 +  if (_adapter == NULL) {
   1.751 +    _from_compiled_entry    = NULL;
   1.752 +  } else {
   1.753 +    _from_compiled_entry    = _adapter->get_c2i_entry();
   1.754 +  }
   1.755 +  OrderAccess::storestore();
   1.756 +  _from_interpreted_entry = _i2i_entry;
   1.757 +  OrderAccess::storestore();
   1.758 +  _code = NULL;
   1.759 +}
   1.760 +
   1.761 +// Called by class data sharing to remove any entry points (which are not shared)
   1.762 +void Method::unlink_method() {
   1.763 +  _code = NULL;
   1.764 +  _i2i_entry = NULL;
   1.765 +  _from_interpreted_entry = NULL;
   1.766 +  if (is_native()) {
   1.767 +    *native_function_addr() = NULL;
   1.768 +    set_signature_handler(NULL);
   1.769 +  }
   1.770 +  NOT_PRODUCT(set_compiled_invocation_count(0);)
   1.771 +  invocation_counter()->reset();
   1.772 +  backedge_counter()->reset();
   1.773 +  _adapter = NULL;
   1.774 +  _from_compiled_entry = NULL;
   1.775 +  assert(_method_data == NULL, "unexpected method data?");
   1.776 +  set_method_data(NULL);
   1.777 +  set_interpreter_throwout_count(0);
   1.778 +  set_interpreter_invocation_count(0);
   1.779 +}
   1.780 +
   1.781 +// Called when the method_holder is getting linked. Setup entrypoints so the method
   1.782 +// is ready to be called from interpreter, compiler, and vtables.
   1.783 +void Method::link_method(methodHandle h_method, TRAPS) {
   1.784 +  // If the code cache is full, we may reenter this function for the
   1.785 +  // leftover methods that weren't linked.
   1.786 +  if (_i2i_entry != NULL) return;
   1.787 +
   1.788 +  assert(_adapter == NULL, "init'd to NULL" );
   1.789 +  assert( _code == NULL, "nothing compiled yet" );
   1.790 +
   1.791 +  // Setup interpreter entrypoint
   1.792 +  assert(this == h_method(), "wrong h_method()" );
   1.793 +  address entry = Interpreter::entry_for_method(h_method);
   1.794 +  assert(entry != NULL, "interpreter entry must be non-null");
   1.795 +  // Sets both _i2i_entry and _from_interpreted_entry
   1.796 +  set_interpreter_entry(entry);
   1.797 +  if (is_native() && !is_method_handle_intrinsic()) {
   1.798 +    set_native_function(
   1.799 +      SharedRuntime::native_method_throw_unsatisfied_link_error_entry(),
   1.800 +      !native_bind_event_is_interesting);
   1.801 +  }
   1.802 +
   1.803 +  // Setup compiler entrypoint.  This is made eagerly, so we do not need
   1.804 +  // special handling of vtables.  An alternative is to make adapters more
   1.805 +  // lazily by calling make_adapter() from from_compiled_entry() for the
   1.806 +  // normal calls.  For vtable calls life gets more complicated.  When a
   1.807 +  // call-site goes mega-morphic we need adapters in all methods which can be
   1.808 +  // called from the vtable.  We need adapters on such methods that get loaded
   1.809 +  // later.  Ditto for mega-morphic itable calls.  If this proves to be a
   1.810 +  // problem we'll make these lazily later.
   1.811 +  (void) make_adapters(h_method, CHECK);
   1.812 +
   1.813 +  // ONLY USE the h_method now as make_adapter may have blocked
   1.814 +
   1.815 +}
   1.816 +
   1.817 +address Method::make_adapters(methodHandle mh, TRAPS) {
   1.818 +  // Adapters for compiled code are made eagerly here.  They are fairly
   1.819 +  // small (generally < 100 bytes) and quick to make (and cached and shared)
   1.820 +  // so making them eagerly shouldn't be too expensive.
   1.821 +  AdapterHandlerEntry* adapter = AdapterHandlerLibrary::get_adapter(mh);
   1.822 +  if (adapter == NULL ) {
   1.823 +    THROW_MSG_NULL(vmSymbols::java_lang_VirtualMachineError(), "out of space in CodeCache for adapters");
   1.824 +  }
   1.825 +
   1.826 +  mh->set_adapter_entry(adapter);
   1.827 +  mh->_from_compiled_entry = adapter->get_c2i_entry();
   1.828 +  return adapter->get_c2i_entry();
   1.829 +}
   1.830 +
   1.831 +// The verified_code_entry() must be called when a invoke is resolved
   1.832 +// on this method.
   1.833 +
   1.834 +// It returns the compiled code entry point, after asserting not null.
   1.835 +// This function is called after potential safepoints so that nmethod
   1.836 +// or adapter that it points to is still live and valid.
   1.837 +// This function must not hit a safepoint!
   1.838 +address Method::verified_code_entry() {
   1.839 +  debug_only(No_Safepoint_Verifier nsv;)
   1.840 +  nmethod *code = (nmethod *)OrderAccess::load_ptr_acquire(&_code);
   1.841 +  if (code == NULL && UseCodeCacheFlushing) {
   1.842 +    nmethod *saved_code = CodeCache::find_and_remove_saved_code(this);
   1.843 +    if (saved_code != NULL) {
   1.844 +      methodHandle method(this);
   1.845 +      assert( ! saved_code->is_osr_method(), "should not get here for osr" );
   1.846 +      set_code( method, saved_code );
   1.847 +    }
   1.848 +  }
   1.849 +
   1.850 +  assert(_from_compiled_entry != NULL, "must be set");
   1.851 +  return _from_compiled_entry;
   1.852 +}
   1.853 +
   1.854 +// Check that if an nmethod ref exists, it has a backlink to this or no backlink at all
   1.855 +// (could be racing a deopt).
   1.856 +// Not inline to avoid circular ref.
   1.857 +bool Method::check_code() const {
   1.858 +  // cached in a register or local.  There's a race on the value of the field.
   1.859 +  nmethod *code = (nmethod *)OrderAccess::load_ptr_acquire(&_code);
   1.860 +  return code == NULL || (code->method() == NULL) || (code->method() == (Method*)this && !code->is_osr_method());
   1.861 +}
   1.862 +
   1.863 +// Install compiled code.  Instantly it can execute.
   1.864 +void Method::set_code(methodHandle mh, nmethod *code) {
   1.865 +  assert( code, "use clear_code to remove code" );
   1.866 +  assert( mh->check_code(), "" );
   1.867 +
   1.868 +  guarantee(mh->adapter() != NULL, "Adapter blob must already exist!");
   1.869 +
   1.870 +  // These writes must happen in this order, because the interpreter will
   1.871 +  // directly jump to from_interpreted_entry which jumps to an i2c adapter
   1.872 +  // which jumps to _from_compiled_entry.
   1.873 +  mh->_code = code;             // Assign before allowing compiled code to exec
   1.874 +
   1.875 +  int comp_level = code->comp_level();
   1.876 +  // In theory there could be a race here. In practice it is unlikely
   1.877 +  // and not worth worrying about.
   1.878 +  if (comp_level > mh->highest_comp_level()) {
   1.879 +    mh->set_highest_comp_level(comp_level);
   1.880 +  }
   1.881 +
   1.882 +  OrderAccess::storestore();
   1.883 +#ifdef SHARK
   1.884 +  mh->_from_interpreted_entry = code->insts_begin();
   1.885 +#else //!SHARK
   1.886 +  mh->_from_compiled_entry = code->verified_entry_point();
   1.887 +  OrderAccess::storestore();
   1.888 +  // Instantly compiled code can execute.
   1.889 +  if (!mh->is_method_handle_intrinsic())
   1.890 +    mh->_from_interpreted_entry = mh->get_i2c_entry();
   1.891 +#endif //!SHARK
   1.892 +}
   1.893 +
   1.894 +
   1.895 +bool Method::is_overridden_in(Klass* k) const {
   1.896 +  InstanceKlass* ik = InstanceKlass::cast(k);
   1.897 +
   1.898 +  if (ik->is_interface()) return false;
   1.899 +
   1.900 +  // If method is an interface, we skip it - except if it
   1.901 +  // is a miranda method
   1.902 +  if (InstanceKlass::cast(method_holder())->is_interface()) {
   1.903 +    // Check that method is not a miranda method
   1.904 +    if (ik->lookup_method(name(), signature()) == NULL) {
   1.905 +      // No implementation exist - so miranda method
   1.906 +      return false;
   1.907 +    }
   1.908 +    return true;
   1.909 +  }
   1.910 +
   1.911 +  assert(ik->is_subclass_of(method_holder()), "should be subklass");
   1.912 +  assert(ik->vtable() != NULL, "vtable should exist");
   1.913 +  if (vtable_index() == nonvirtual_vtable_index) {
   1.914 +    return false;
   1.915 +  } else {
   1.916 +    Method* vt_m = ik->method_at_vtable(vtable_index());
   1.917 +    return vt_m != this;
   1.918 +  }
   1.919 +}
   1.920 +
   1.921 +
   1.922 +// give advice about whether this Method* should be cached or not
   1.923 +bool Method::should_not_be_cached() const {
   1.924 +  if (is_old()) {
   1.925 +    // This method has been redefined. It is either EMCP or obsolete
   1.926 +    // and we don't want to cache it because that would pin the method
   1.927 +    // down and prevent it from being collectible if and when it
   1.928 +    // finishes executing.
   1.929 +    return true;
   1.930 +  }
   1.931 +
   1.932 +  // caching this method should be just fine
   1.933 +  return false;
   1.934 +}
   1.935 +
   1.936 +// Constant pool structure for invoke methods:
   1.937 +enum {
   1.938 +  _imcp_invoke_name = 1,        // utf8: 'invokeExact', etc.
   1.939 +  _imcp_invoke_signature,       // utf8: (variable Symbol*)
   1.940 +  _imcp_limit
   1.941 +};
   1.942 +
   1.943 +// Test if this method is an MH adapter frame generated by Java code.
   1.944 +// Cf. java/lang/invoke/InvokerBytecodeGenerator
   1.945 +bool Method::is_compiled_lambda_form() const {
   1.946 +  return intrinsic_id() == vmIntrinsics::_compiledLambdaForm;
   1.947 +}
   1.948 +
   1.949 +// Test if this method is an internal MH primitive method.
   1.950 +bool Method::is_method_handle_intrinsic() const {
   1.951 +  vmIntrinsics::ID iid = intrinsic_id();
   1.952 +  return (MethodHandles::is_signature_polymorphic(iid) &&
   1.953 +          MethodHandles::is_signature_polymorphic_intrinsic(iid));
   1.954 +}
   1.955 +
   1.956 +bool Method::has_member_arg() const {
   1.957 +  vmIntrinsics::ID iid = intrinsic_id();
   1.958 +  return (MethodHandles::is_signature_polymorphic(iid) &&
   1.959 +          MethodHandles::has_member_arg(iid));
   1.960 +}
   1.961 +
   1.962 +// Make an instance of a signature-polymorphic internal MH primitive.
   1.963 +methodHandle Method::make_method_handle_intrinsic(vmIntrinsics::ID iid,
   1.964 +                                                         Symbol* signature,
   1.965 +                                                         TRAPS) {
   1.966 +  ResourceMark rm;
   1.967 +  methodHandle empty;
   1.968 +
   1.969 +  KlassHandle holder = SystemDictionary::MethodHandle_klass();
   1.970 +  Symbol* name = MethodHandles::signature_polymorphic_intrinsic_name(iid);
   1.971 +  assert(iid == MethodHandles::signature_polymorphic_name_id(name), "");
   1.972 +  if (TraceMethodHandles) {
   1.973 +    tty->print_cr("make_method_handle_intrinsic MH.%s%s", name->as_C_string(), signature->as_C_string());
   1.974 +  }
   1.975 +
   1.976 +  // invariant:   cp->symbol_at_put is preceded by a refcount increment (more usually a lookup)
   1.977 +  name->increment_refcount();
   1.978 +  signature->increment_refcount();
   1.979 +
   1.980 +  int cp_length = _imcp_limit;
   1.981 +  ClassLoaderData* loader_data = holder->class_loader_data();
   1.982 +  constantPoolHandle cp;
   1.983 +  {
   1.984 +    ConstantPool* cp_oop = ConstantPool::allocate(loader_data, cp_length, CHECK_(empty));
   1.985 +    cp = constantPoolHandle(THREAD, cp_oop);
   1.986 +  }
   1.987 +  cp->set_pool_holder(holder());
   1.988 +  cp->symbol_at_put(_imcp_invoke_name,       name);
   1.989 +  cp->symbol_at_put(_imcp_invoke_signature,  signature);
   1.990 +  cp->set_preresolution();
   1.991 +
   1.992 +  // decide on access bits:  public or not?
   1.993 +  int flags_bits = (JVM_ACC_NATIVE | JVM_ACC_SYNTHETIC | JVM_ACC_FINAL);
   1.994 +  bool must_be_static = MethodHandles::is_signature_polymorphic_static(iid);
   1.995 +  if (must_be_static)  flags_bits |= JVM_ACC_STATIC;
   1.996 +  assert((flags_bits & JVM_ACC_PUBLIC) == 0, "do not expose these methods");
   1.997 +
   1.998 +  methodHandle m;
   1.999 +  {
  1.1000 +    Method* m_oop = Method::allocate(loader_data, 0, accessFlags_from(flags_bits),
  1.1001 +                                              0, 0, 0, 0, CHECK_(empty));
  1.1002 +    m = methodHandle(THREAD, m_oop);
  1.1003 +  }
  1.1004 +  m->set_constants(cp());
  1.1005 +  m->set_name_index(_imcp_invoke_name);
  1.1006 +  m->set_signature_index(_imcp_invoke_signature);
  1.1007 +  assert(MethodHandles::is_signature_polymorphic_name(m->name()), "");
  1.1008 +  assert(m->signature() == signature, "");
  1.1009 +#ifdef CC_INTERP
  1.1010 +  ResultTypeFinder rtf(signature);
  1.1011 +  m->set_result_index(rtf.type());
  1.1012 +#endif
  1.1013 +  m->compute_size_of_parameters(THREAD);
  1.1014 +  m->init_intrinsic_id();
  1.1015 +  assert(m->is_method_handle_intrinsic(), "");
  1.1016 +#ifdef ASSERT
  1.1017 +  if (!MethodHandles::is_signature_polymorphic(m->intrinsic_id()))  m->print();
  1.1018 +  assert(MethodHandles::is_signature_polymorphic(m->intrinsic_id()), "must be an invoker");
  1.1019 +  assert(m->intrinsic_id() == iid, "correctly predicted iid");
  1.1020 +#endif //ASSERT
  1.1021 +
  1.1022 +  // Finally, set up its entry points.
  1.1023 +  assert(m->can_be_statically_bound(), "");
  1.1024 +  m->set_vtable_index(Method::nonvirtual_vtable_index);
  1.1025 +  m->link_method(m, CHECK_(empty));
  1.1026 +
  1.1027 +  if (TraceMethodHandles && (Verbose || WizardMode))
  1.1028 +    m->print_on(tty);
  1.1029 +
  1.1030 +  return m;
  1.1031 +}
  1.1032 +
  1.1033 +Klass* Method::check_non_bcp_klass(Klass* klass) {
  1.1034 +  if (klass != NULL && Klass::cast(klass)->class_loader() != NULL) {
  1.1035 +    if (Klass::cast(klass)->oop_is_objArray())
  1.1036 +      klass = objArrayKlass::cast(klass)->bottom_klass();
  1.1037 +    return klass;
  1.1038 +  }
  1.1039 +  return NULL;
  1.1040 +}
  1.1041 +
  1.1042 +
  1.1043 +methodHandle Method::clone_with_new_data(methodHandle m, u_char* new_code, int new_code_length,
  1.1044 +                                                u_char* new_compressed_linenumber_table, int new_compressed_linenumber_size, TRAPS) {
  1.1045 +  // Code below does not work for native methods - they should never get rewritten anyway
  1.1046 +  assert(!m->is_native(), "cannot rewrite native methods");
  1.1047 +  // Allocate new Method*
  1.1048 +  AccessFlags flags = m->access_flags();
  1.1049 +  int checked_exceptions_len = m->checked_exceptions_length();
  1.1050 +  int localvariable_len = m->localvariable_table_length();
  1.1051 +  int exception_table_len = m->exception_table_length();
  1.1052 +
  1.1053 +  ClassLoaderData* loader_data = m()->method_holder()->class_loader_data();
  1.1054 +  Method* newm_oop = Method::allocate(loader_data,
  1.1055 +                                               new_code_length,
  1.1056 +                                              flags,
  1.1057 +                                              new_compressed_linenumber_size,
  1.1058 +                                              localvariable_len,
  1.1059 +                                              exception_table_len,
  1.1060 +                                              checked_exceptions_len,
  1.1061 +                                              CHECK_(methodHandle()));
  1.1062 +  methodHandle newm (THREAD, newm_oop);
  1.1063 +  int new_method_size = newm->method_size();
  1.1064 +
  1.1065 +  // Create a shallow copy of Method part, but be careful to preserve the new ConstMethod*
  1.1066 +  ConstMethod* newcm = newm->constMethod();
  1.1067 +  int new_const_method_size = newm->constMethod()->size();
  1.1068 +
  1.1069 +  memcpy(newm(), m(), sizeof(Method));
  1.1070 +
  1.1071 +  // Create shallow copy of ConstMethod.
  1.1072 +  memcpy(newcm, m->constMethod(), sizeof(ConstMethod));
  1.1073 +
  1.1074 +  // Reset correct method/const method, method size, and parameter info
  1.1075 +  newm->set_constMethod(newcm);
  1.1076 +  newm->constMethod()->set_code_size(new_code_length);
  1.1077 +  newm->constMethod()->set_constMethod_size(new_const_method_size);
  1.1078 +  newm->set_method_size(new_method_size);
  1.1079 +  assert(newm->code_size() == new_code_length, "check");
  1.1080 +  assert(newm->checked_exceptions_length() == checked_exceptions_len, "check");
  1.1081 +  assert(newm->exception_table_length() == exception_table_len, "check");
  1.1082 +  assert(newm->localvariable_table_length() == localvariable_len, "check");
  1.1083 +  // Copy new byte codes
  1.1084 +  memcpy(newm->code_base(), new_code, new_code_length);
  1.1085 +  // Copy line number table
  1.1086 +  if (new_compressed_linenumber_size > 0) {
  1.1087 +    memcpy(newm->compressed_linenumber_table(),
  1.1088 +           new_compressed_linenumber_table,
  1.1089 +           new_compressed_linenumber_size);
  1.1090 +  }
  1.1091 +  // Copy checked_exceptions
  1.1092 +  if (checked_exceptions_len > 0) {
  1.1093 +    memcpy(newm->checked_exceptions_start(),
  1.1094 +           m->checked_exceptions_start(),
  1.1095 +           checked_exceptions_len * sizeof(CheckedExceptionElement));
  1.1096 +  }
  1.1097 +  // Copy exception table
  1.1098 +  if (exception_table_len > 0) {
  1.1099 +    memcpy(newm->exception_table_start(),
  1.1100 +           m->exception_table_start(),
  1.1101 +           exception_table_len * sizeof(ExceptionTableElement));
  1.1102 +  }
  1.1103 +  // Copy local variable number table
  1.1104 +  if (localvariable_len > 0) {
  1.1105 +    memcpy(newm->localvariable_table_start(),
  1.1106 +           m->localvariable_table_start(),
  1.1107 +           localvariable_len * sizeof(LocalVariableTableElement));
  1.1108 +  }
  1.1109 +  // Copy stackmap table
  1.1110 +  if (m->has_stackmap_table()) {
  1.1111 +    int code_attribute_length = m->stackmap_data()->length();
  1.1112 +    Array<u1>* stackmap_data =
  1.1113 +      MetadataFactory::new_array<u1>(loader_data, code_attribute_length, 0, CHECK_NULL);
  1.1114 +    memcpy((void*)stackmap_data->adr_at(0),
  1.1115 +           (void*)m->stackmap_data()->adr_at(0), code_attribute_length);
  1.1116 +    newm->set_stackmap_data(stackmap_data);
  1.1117 +  }
  1.1118 +
  1.1119 +  return newm;
  1.1120 +}
  1.1121 +
  1.1122 +vmSymbols::SID Method::klass_id_for_intrinsics(Klass* holder) {
  1.1123 +  // if loader is not the default loader (i.e., != NULL), we can't know the intrinsics
  1.1124 +  // because we are not loading from core libraries
  1.1125 +  if (InstanceKlass::cast(holder)->class_loader() != NULL)
  1.1126 +    return vmSymbols::NO_SID;   // regardless of name, no intrinsics here
  1.1127 +
  1.1128 +  // see if the klass name is well-known:
  1.1129 +  Symbol* klass_name = InstanceKlass::cast(holder)->name();
  1.1130 +  return vmSymbols::find_sid(klass_name);
  1.1131 +}
  1.1132 +
  1.1133 +void Method::init_intrinsic_id() {
  1.1134 +  assert(_intrinsic_id == vmIntrinsics::_none, "do this just once");
  1.1135 +  const uintptr_t max_id_uint = right_n_bits((int)(sizeof(_intrinsic_id) * BitsPerByte));
  1.1136 +  assert((uintptr_t)vmIntrinsics::ID_LIMIT <= max_id_uint, "else fix size");
  1.1137 +  assert(intrinsic_id_size_in_bytes() == sizeof(_intrinsic_id), "");
  1.1138 +
  1.1139 +  // the klass name is well-known:
  1.1140 +  vmSymbols::SID klass_id = klass_id_for_intrinsics(method_holder());
  1.1141 +  assert(klass_id != vmSymbols::NO_SID, "caller responsibility");
  1.1142 +
  1.1143 +  // ditto for method and signature:
  1.1144 +  vmSymbols::SID  name_id = vmSymbols::find_sid(name());
  1.1145 +  if (klass_id != vmSymbols::VM_SYMBOL_ENUM_NAME(java_lang_invoke_MethodHandle)
  1.1146 +      && name_id == vmSymbols::NO_SID)
  1.1147 +    return;
  1.1148 +  vmSymbols::SID   sig_id = vmSymbols::find_sid(signature());
  1.1149 +  if (klass_id != vmSymbols::VM_SYMBOL_ENUM_NAME(java_lang_invoke_MethodHandle)
  1.1150 +      && sig_id == vmSymbols::NO_SID)  return;
  1.1151 +  jshort flags = access_flags().as_short();
  1.1152 +
  1.1153 +  vmIntrinsics::ID id = vmIntrinsics::find_id(klass_id, name_id, sig_id, flags);
  1.1154 +  if (id != vmIntrinsics::_none) {
  1.1155 +    set_intrinsic_id(id);
  1.1156 +    return;
  1.1157 +  }
  1.1158 +
  1.1159 +  // A few slightly irregular cases:
  1.1160 +  switch (klass_id) {
  1.1161 +  case vmSymbols::VM_SYMBOL_ENUM_NAME(java_lang_StrictMath):
  1.1162 +    // Second chance: check in regular Math.
  1.1163 +    switch (name_id) {
  1.1164 +    case vmSymbols::VM_SYMBOL_ENUM_NAME(min_name):
  1.1165 +    case vmSymbols::VM_SYMBOL_ENUM_NAME(max_name):
  1.1166 +    case vmSymbols::VM_SYMBOL_ENUM_NAME(sqrt_name):
  1.1167 +      // pretend it is the corresponding method in the non-strict class:
  1.1168 +      klass_id = vmSymbols::VM_SYMBOL_ENUM_NAME(java_lang_Math);
  1.1169 +      id = vmIntrinsics::find_id(klass_id, name_id, sig_id, flags);
  1.1170 +      break;
  1.1171 +    }
  1.1172 +    break;
  1.1173 +
  1.1174 +  // Signature-polymorphic methods: MethodHandle.invoke*, InvokeDynamic.*.
  1.1175 +  case vmSymbols::VM_SYMBOL_ENUM_NAME(java_lang_invoke_MethodHandle):
  1.1176 +    if (!is_native())  break;
  1.1177 +    id = MethodHandles::signature_polymorphic_name_id(method_holder(), name());
  1.1178 +    if (is_static() != MethodHandles::is_signature_polymorphic_static(id))
  1.1179 +      id = vmIntrinsics::_none;
  1.1180 +    break;
  1.1181 +  }
  1.1182 +
  1.1183 +  if (id != vmIntrinsics::_none) {
  1.1184 +    // Set up its iid.  It is an alias method.
  1.1185 +    set_intrinsic_id(id);
  1.1186 +    return;
  1.1187 +  }
  1.1188 +}
  1.1189 +
  1.1190 +// These two methods are static since a GC may move the Method
  1.1191 +bool Method::load_signature_classes(methodHandle m, TRAPS) {
  1.1192 +  if (THREAD->is_Compiler_thread()) {
  1.1193 +    // There is nothing useful this routine can do from within the Compile thread.
  1.1194 +    // Hopefully, the signature contains only well-known classes.
  1.1195 +    // We could scan for this and return true/false, but the caller won't care.
  1.1196 +    return false;
  1.1197 +  }
  1.1198 +  bool sig_is_loaded = true;
  1.1199 +  Handle class_loader(THREAD, InstanceKlass::cast(m->method_holder())->class_loader());
  1.1200 +  Handle protection_domain(THREAD, Klass::cast(m->method_holder())->protection_domain());
  1.1201 +  ResourceMark rm(THREAD);
  1.1202 +  Symbol*  signature = m->signature();
  1.1203 +  for(SignatureStream ss(signature); !ss.is_done(); ss.next()) {
  1.1204 +    if (ss.is_object()) {
  1.1205 +      Symbol* sym = ss.as_symbol(CHECK_(false));
  1.1206 +      Symbol*  name  = sym;
  1.1207 +      Klass* klass = SystemDictionary::resolve_or_null(name, class_loader,
  1.1208 +                                             protection_domain, THREAD);
  1.1209 +      // We are loading classes eagerly. If a ClassNotFoundException or
  1.1210 +      // a LinkageError was generated, be sure to ignore it.
  1.1211 +      if (HAS_PENDING_EXCEPTION) {
  1.1212 +        if (PENDING_EXCEPTION->is_a(SystemDictionary::ClassNotFoundException_klass()) ||
  1.1213 +            PENDING_EXCEPTION->is_a(SystemDictionary::LinkageError_klass())) {
  1.1214 +          CLEAR_PENDING_EXCEPTION;
  1.1215 +        } else {
  1.1216 +          return false;
  1.1217 +        }
  1.1218 +      }
  1.1219 +      if( klass == NULL) { sig_is_loaded = false; }
  1.1220 +    }
  1.1221 +  }
  1.1222 +  return sig_is_loaded;
  1.1223 +}
  1.1224 +
  1.1225 +bool Method::has_unloaded_classes_in_signature(methodHandle m, TRAPS) {
  1.1226 +  Handle class_loader(THREAD, InstanceKlass::cast(m->method_holder())->class_loader());
  1.1227 +  Handle protection_domain(THREAD, Klass::cast(m->method_holder())->protection_domain());
  1.1228 +  ResourceMark rm(THREAD);
  1.1229 +  Symbol*  signature = m->signature();
  1.1230 +  for(SignatureStream ss(signature); !ss.is_done(); ss.next()) {
  1.1231 +    if (ss.type() == T_OBJECT) {
  1.1232 +      Symbol* name = ss.as_symbol_or_null();
  1.1233 +      if (name == NULL) return true;
  1.1234 +      Klass* klass = SystemDictionary::find(name, class_loader, protection_domain, THREAD);
  1.1235 +      if (klass == NULL) return true;
  1.1236 +    }
  1.1237 +  }
  1.1238 +  return false;
  1.1239 +}
  1.1240 +
  1.1241 +// Exposed so field engineers can debug VM
  1.1242 +void Method::print_short_name(outputStream* st) {
  1.1243 +  ResourceMark rm;
  1.1244 +#ifdef PRODUCT
  1.1245 +  st->print(" %s::", method_holder()->external_name());
  1.1246 +#else
  1.1247 +  st->print(" %s::", method_holder()->internal_name());
  1.1248 +#endif
  1.1249 +  name()->print_symbol_on(st);
  1.1250 +  if (WizardMode) signature()->print_symbol_on(st);
  1.1251 +  else if (MethodHandles::is_signature_polymorphic(intrinsic_id()))
  1.1252 +    MethodHandles::print_as_basic_type_signature_on(st, signature(), true);
  1.1253 +}
  1.1254 +
  1.1255 +// This is only done during class loading, so it is OK to assume method_idnum matches the methods() array
  1.1256 +static void reorder_based_on_method_index(Array<Method*>* methods,
  1.1257 +                                          Array<AnnotationArray*>* annotations,
  1.1258 +                                          GrowableArray<AnnotationArray*>* temp_array) {
  1.1259 +  if (annotations == NULL) {
  1.1260 +    return;
  1.1261 +  }
  1.1262 +
  1.1263 +  int length = methods->length();
  1.1264 +  int i;
  1.1265 +  // Copy to temp array
  1.1266 +  temp_array->clear();
  1.1267 +  for (i = 0; i < length; i++) {
  1.1268 +    temp_array->append(annotations->at(i));
  1.1269 +  }
  1.1270 +
  1.1271 +  // Copy back using old method indices
  1.1272 +  for (i = 0; i < length; i++) {
  1.1273 +    Method* m = methods->at(i);
  1.1274 +    annotations->at_put(i, temp_array->at(m->method_idnum()));
  1.1275 +  }
  1.1276 +}
  1.1277 +
  1.1278 +// Comparer for sorting an object array containing
  1.1279 +// Method*s.
  1.1280 +static int method_comparator(Method* a, Method* b) {
  1.1281 +  return a->name()->fast_compare(b->name());
  1.1282 +}
  1.1283 +
  1.1284 +// This is only done during class loading, so it is OK to assume method_idnum matches the methods() array
  1.1285 +void Method::sort_methods(Array<Method*>* methods,
  1.1286 +                                 Array<AnnotationArray*>* methods_annotations,
  1.1287 +                                 Array<AnnotationArray*>* methods_parameter_annotations,
  1.1288 +                                 Array<AnnotationArray*>* methods_default_annotations,
  1.1289 +                                 bool idempotent) {
  1.1290 +  int length = methods->length();
  1.1291 +  if (length > 1) {
  1.1292 +    bool do_annotations = false;
  1.1293 +    if (methods_annotations != NULL ||
  1.1294 +        methods_parameter_annotations != NULL ||
  1.1295 +        methods_default_annotations != NULL) {
  1.1296 +      do_annotations = true;
  1.1297 +    }
  1.1298 +    if (do_annotations) {
  1.1299 +      // Remember current method ordering so we can reorder annotations
  1.1300 +      for (int i = 0; i < length; i++) {
  1.1301 +        Method* m = methods->at(i);
  1.1302 +        m->set_method_idnum(i);
  1.1303 +      }
  1.1304 +    }
  1.1305 +    {
  1.1306 +      No_Safepoint_Verifier nsv;
  1.1307 +      QuickSort::sort<Method*>(methods->data(), length, method_comparator, idempotent);
  1.1308 +    }
  1.1309 +
  1.1310 +    // Sort annotations if necessary
  1.1311 +    assert(methods_annotations == NULL           || methods_annotations->length() == methods->length(), "");
  1.1312 +    assert(methods_parameter_annotations == NULL || methods_parameter_annotations->length() == methods->length(), "");
  1.1313 +    assert(methods_default_annotations == NULL   || methods_default_annotations->length() == methods->length(), "");
  1.1314 +    if (do_annotations) {
  1.1315 +      ResourceMark rm;
  1.1316 +      // Allocate temporary storage
  1.1317 +      GrowableArray<AnnotationArray*>* temp_array = new GrowableArray<AnnotationArray*>(length);
  1.1318 +      reorder_based_on_method_index(methods, methods_annotations, temp_array);
  1.1319 +      reorder_based_on_method_index(methods, methods_parameter_annotations, temp_array);
  1.1320 +      reorder_based_on_method_index(methods, methods_default_annotations, temp_array);
  1.1321 +    }
  1.1322 +
  1.1323 +    // Reset method ordering
  1.1324 +    for (int i = 0; i < length; i++) {
  1.1325 +      Method* m = methods->at(i);
  1.1326 +      m->set_method_idnum(i);
  1.1327 +    }
  1.1328 +  }
  1.1329 +}
  1.1330 +
  1.1331 +
  1.1332 +//-----------------------------------------------------------------------------------
  1.1333 +// Non-product code
  1.1334 +
  1.1335 +#ifndef PRODUCT
  1.1336 +class SignatureTypePrinter : public SignatureTypeNames {
  1.1337 + private:
  1.1338 +  outputStream* _st;
  1.1339 +  bool _use_separator;
  1.1340 +
  1.1341 +  void type_name(const char* name) {
  1.1342 +    if (_use_separator) _st->print(", ");
  1.1343 +    _st->print(name);
  1.1344 +    _use_separator = true;
  1.1345 +  }
  1.1346 +
  1.1347 + public:
  1.1348 +  SignatureTypePrinter(Symbol* signature, outputStream* st) : SignatureTypeNames(signature) {
  1.1349 +    _st = st;
  1.1350 +    _use_separator = false;
  1.1351 +  }
  1.1352 +
  1.1353 +  void print_parameters()              { _use_separator = false; iterate_parameters(); }
  1.1354 +  void print_returntype()              { _use_separator = false; iterate_returntype(); }
  1.1355 +};
  1.1356 +
  1.1357 +
  1.1358 +void Method::print_name(outputStream* st) {
  1.1359 +  Thread *thread = Thread::current();
  1.1360 +  ResourceMark rm(thread);
  1.1361 +  SignatureTypePrinter sig(signature(), st);
  1.1362 +  st->print("%s ", is_static() ? "static" : "virtual");
  1.1363 +  sig.print_returntype();
  1.1364 +  st->print(" %s.", method_holder()->internal_name());
  1.1365 +  name()->print_symbol_on(st);
  1.1366 +  st->print("(");
  1.1367 +  sig.print_parameters();
  1.1368 +  st->print(")");
  1.1369 +}
  1.1370 +
  1.1371 +
  1.1372 +void Method::print_codes_on(outputStream* st) const {
  1.1373 +  print_codes_on(0, code_size(), st);
  1.1374 +}
  1.1375 +
  1.1376 +void Method::print_codes_on(int from, int to, outputStream* st) const {
  1.1377 +  Thread *thread = Thread::current();
  1.1378 +  ResourceMark rm(thread);
  1.1379 +  methodHandle mh (thread, (Method*)this);
  1.1380 +  BytecodeStream s(mh);
  1.1381 +  s.set_interval(from, to);
  1.1382 +  BytecodeTracer::set_closure(BytecodeTracer::std_closure());
  1.1383 +  while (s.next() >= 0) BytecodeTracer::trace(mh, s.bcp(), st);
  1.1384 +}
  1.1385 +#endif // not PRODUCT
  1.1386 +
  1.1387 +
  1.1388 +// Simple compression of line number tables. We use a regular compressed stream, except that we compress deltas
  1.1389 +// between (bci,line) pairs since they are smaller. If (bci delta, line delta) fits in (5-bit unsigned, 3-bit unsigned)
  1.1390 +// we save it as one byte, otherwise we write a 0xFF escape character and use regular compression. 0x0 is used
  1.1391 +// as end-of-stream terminator.
  1.1392 +
  1.1393 +void CompressedLineNumberWriteStream::write_pair_regular(int bci_delta, int line_delta) {
  1.1394 +  // bci and line number does not compress into single byte.
  1.1395 +  // Write out escape character and use regular compression for bci and line number.
  1.1396 +  write_byte((jubyte)0xFF);
  1.1397 +  write_signed_int(bci_delta);
  1.1398 +  write_signed_int(line_delta);
  1.1399 +}
  1.1400 +
  1.1401 +// See comment in method.hpp which explains why this exists.
  1.1402 +#if defined(_M_AMD64) && _MSC_VER >= 1400
  1.1403 +#pragma optimize("", off)
  1.1404 +void CompressedLineNumberWriteStream::write_pair(int bci, int line) {
  1.1405 +  write_pair_inline(bci, line);
  1.1406 +}
  1.1407 +#pragma optimize("", on)
  1.1408 +#endif
  1.1409 +
  1.1410 +CompressedLineNumberReadStream::CompressedLineNumberReadStream(u_char* buffer) : CompressedReadStream(buffer) {
  1.1411 +  _bci = 0;
  1.1412 +  _line = 0;
  1.1413 +};
  1.1414 +
  1.1415 +
  1.1416 +bool CompressedLineNumberReadStream::read_pair() {
  1.1417 +  jubyte next = read_byte();
  1.1418 +  // Check for terminator
  1.1419 +  if (next == 0) return false;
  1.1420 +  if (next == 0xFF) {
  1.1421 +    // Escape character, regular compression used
  1.1422 +    _bci  += read_signed_int();
  1.1423 +    _line += read_signed_int();
  1.1424 +  } else {
  1.1425 +    // Single byte compression used
  1.1426 +    _bci  += next >> 3;
  1.1427 +    _line += next & 0x7;
  1.1428 +  }
  1.1429 +  return true;
  1.1430 +}
  1.1431 +
  1.1432 +
  1.1433 +Bytecodes::Code Method::orig_bytecode_at(int bci) const {
  1.1434 +  BreakpointInfo* bp = InstanceKlass::cast(method_holder())->breakpoints();
  1.1435 +  for (; bp != NULL; bp = bp->next()) {
  1.1436 +    if (bp->match(this, bci)) {
  1.1437 +      return bp->orig_bytecode();
  1.1438 +    }
  1.1439 +  }
  1.1440 +  ShouldNotReachHere();
  1.1441 +  return Bytecodes::_shouldnotreachhere;
  1.1442 +}
  1.1443 +
  1.1444 +void Method::set_orig_bytecode_at(int bci, Bytecodes::Code code) {
  1.1445 +  assert(code != Bytecodes::_breakpoint, "cannot patch breakpoints this way");
  1.1446 +  BreakpointInfo* bp = InstanceKlass::cast(method_holder())->breakpoints();
  1.1447 +  for (; bp != NULL; bp = bp->next()) {
  1.1448 +    if (bp->match(this, bci)) {
  1.1449 +      bp->set_orig_bytecode(code);
  1.1450 +      // and continue, in case there is more than one
  1.1451 +    }
  1.1452 +  }
  1.1453 +}
  1.1454 +
  1.1455 +void Method::set_breakpoint(int bci) {
  1.1456 +  InstanceKlass* ik = InstanceKlass::cast(method_holder());
  1.1457 +  BreakpointInfo *bp = new BreakpointInfo(this, bci);
  1.1458 +  bp->set_next(ik->breakpoints());
  1.1459 +  ik->set_breakpoints(bp);
  1.1460 +  // do this last:
  1.1461 +  bp->set(this);
  1.1462 +}
  1.1463 +
  1.1464 +static void clear_matches(Method* m, int bci) {
  1.1465 +  InstanceKlass* ik = InstanceKlass::cast(m->method_holder());
  1.1466 +  BreakpointInfo* prev_bp = NULL;
  1.1467 +  BreakpointInfo* next_bp;
  1.1468 +  for (BreakpointInfo* bp = ik->breakpoints(); bp != NULL; bp = next_bp) {
  1.1469 +    next_bp = bp->next();
  1.1470 +    // bci value of -1 is used to delete all breakpoints in method m (ex: clear_all_breakpoint).
  1.1471 +    if (bci >= 0 ? bp->match(m, bci) : bp->match(m)) {
  1.1472 +      // do this first:
  1.1473 +      bp->clear(m);
  1.1474 +      // unhook it
  1.1475 +      if (prev_bp != NULL)
  1.1476 +        prev_bp->set_next(next_bp);
  1.1477 +      else
  1.1478 +        ik->set_breakpoints(next_bp);
  1.1479 +      delete bp;
  1.1480 +      // When class is redefined JVMTI sets breakpoint in all versions of EMCP methods
  1.1481 +      // at same location. So we have multiple matching (method_index and bci)
  1.1482 +      // BreakpointInfo nodes in BreakpointInfo list. We should just delete one
  1.1483 +      // breakpoint for clear_breakpoint request and keep all other method versions
  1.1484 +      // BreakpointInfo for future clear_breakpoint request.
  1.1485 +      // bcivalue of -1 is used to clear all breakpoints (see clear_all_breakpoints)
  1.1486 +      // which is being called when class is unloaded. We delete all the Breakpoint
  1.1487 +      // information for all versions of method. We may not correctly restore the original
  1.1488 +      // bytecode in all method versions, but that is ok. Because the class is being unloaded
  1.1489 +      // so these methods won't be used anymore.
  1.1490 +      if (bci >= 0) {
  1.1491 +        break;
  1.1492 +      }
  1.1493 +    } else {
  1.1494 +      // This one is a keeper.
  1.1495 +      prev_bp = bp;
  1.1496 +    }
  1.1497 +  }
  1.1498 +}
  1.1499 +
  1.1500 +void Method::clear_breakpoint(int bci) {
  1.1501 +  assert(bci >= 0, "");
  1.1502 +  clear_matches(this, bci);
  1.1503 +}
  1.1504 +
  1.1505 +void Method::clear_all_breakpoints() {
  1.1506 +  clear_matches(this, -1);
  1.1507 +}
  1.1508 +
  1.1509 +
  1.1510 +int Method::invocation_count() {
  1.1511 +  if (TieredCompilation) {
  1.1512 +    MethodData* const mdo = method_data();
  1.1513 +    if (invocation_counter()->carry() || ((mdo != NULL) ? mdo->invocation_counter()->carry() : false)) {
  1.1514 +      return InvocationCounter::count_limit;
  1.1515 +    } else {
  1.1516 +      return invocation_counter()->count() + ((mdo != NULL) ? mdo->invocation_counter()->count() : 0);
  1.1517 +    }
  1.1518 +  } else {
  1.1519 +    return invocation_counter()->count();
  1.1520 +  }
  1.1521 +}
  1.1522 +
  1.1523 +int Method::backedge_count() {
  1.1524 +  if (TieredCompilation) {
  1.1525 +    MethodData* const mdo = method_data();
  1.1526 +    if (backedge_counter()->carry() || ((mdo != NULL) ? mdo->backedge_counter()->carry() : false)) {
  1.1527 +      return InvocationCounter::count_limit;
  1.1528 +    } else {
  1.1529 +      return backedge_counter()->count() + ((mdo != NULL) ? mdo->backedge_counter()->count() : 0);
  1.1530 +    }
  1.1531 +  } else {
  1.1532 +    return backedge_counter()->count();
  1.1533 +  }
  1.1534 +}
  1.1535 +
  1.1536 +int Method::highest_comp_level() const {
  1.1537 +  MethodData* mdo = method_data();
  1.1538 +  if (mdo != NULL) {
  1.1539 +    return mdo->highest_comp_level();
  1.1540 +  } else {
  1.1541 +    return CompLevel_none;
  1.1542 +  }
  1.1543 +}
  1.1544 +
  1.1545 +int Method::highest_osr_comp_level() const {
  1.1546 +  MethodData* mdo = method_data();
  1.1547 +  if (mdo != NULL) {
  1.1548 +    return mdo->highest_osr_comp_level();
  1.1549 +  } else {
  1.1550 +    return CompLevel_none;
  1.1551 +  }
  1.1552 +}
  1.1553 +
  1.1554 +void Method::set_highest_comp_level(int level) {
  1.1555 +  MethodData* mdo = method_data();
  1.1556 +  if (mdo != NULL) {
  1.1557 +    mdo->set_highest_comp_level(level);
  1.1558 +  }
  1.1559 +}
  1.1560 +
  1.1561 +void Method::set_highest_osr_comp_level(int level) {
  1.1562 +  MethodData* mdo = method_data();
  1.1563 +  if (mdo != NULL) {
  1.1564 +    mdo->set_highest_osr_comp_level(level);
  1.1565 +  }
  1.1566 +}
  1.1567 +
  1.1568 +BreakpointInfo::BreakpointInfo(Method* m, int bci) {
  1.1569 +  _bci = bci;
  1.1570 +  _name_index = m->name_index();
  1.1571 +  _signature_index = m->signature_index();
  1.1572 +  _orig_bytecode = (Bytecodes::Code) *m->bcp_from(_bci);
  1.1573 +  if (_orig_bytecode == Bytecodes::_breakpoint)
  1.1574 +    _orig_bytecode = m->orig_bytecode_at(_bci);
  1.1575 +  _next = NULL;
  1.1576 +}
  1.1577 +
  1.1578 +void BreakpointInfo::set(Method* method) {
  1.1579 +#ifdef ASSERT
  1.1580 +  {
  1.1581 +    Bytecodes::Code code = (Bytecodes::Code) *method->bcp_from(_bci);
  1.1582 +    if (code == Bytecodes::_breakpoint)
  1.1583 +      code = method->orig_bytecode_at(_bci);
  1.1584 +    assert(orig_bytecode() == code, "original bytecode must be the same");
  1.1585 +  }
  1.1586 +#endif
  1.1587 +  *method->bcp_from(_bci) = Bytecodes::_breakpoint;
  1.1588 +  method->incr_number_of_breakpoints();
  1.1589 +  SystemDictionary::notice_modification();
  1.1590 +  {
  1.1591 +    // Deoptimize all dependents on this method
  1.1592 +    Thread *thread = Thread::current();
  1.1593 +    HandleMark hm(thread);
  1.1594 +    methodHandle mh(thread, method);
  1.1595 +    Universe::flush_dependents_on_method(mh);
  1.1596 +  }
  1.1597 +}
  1.1598 +
  1.1599 +void BreakpointInfo::clear(Method* method) {
  1.1600 +  *method->bcp_from(_bci) = orig_bytecode();
  1.1601 +  assert(method->number_of_breakpoints() > 0, "must not go negative");
  1.1602 +  method->decr_number_of_breakpoints();
  1.1603 +}
  1.1604 +
  1.1605 +// jmethodID handling
  1.1606 +
  1.1607 +// This is a block allocating object, sort of like JNIHandleBlock, only a
  1.1608 +// lot simpler.  There aren't many of these, they aren't long, they are rarely
  1.1609 +// deleted and so we can do some suboptimal things.
  1.1610 +// It's allocated on the CHeap because once we allocate a jmethodID, we can
  1.1611 +// never get rid of it.
  1.1612 +// It would be nice to be able to parameterize the number of methods for
  1.1613 +// the null_class_loader but then we'd have to turn this and ClassLoaderData
  1.1614 +// into templates.
  1.1615 +
  1.1616 +// I feel like this brain dead class should exist somewhere in the STL
  1.1617 +
  1.1618 +class JNIMethodBlock : public CHeapObj<mtClass> {
  1.1619 +  enum { number_of_methods = 8 };
  1.1620 +
  1.1621 +  Method*         _methods[number_of_methods];
  1.1622 +  int             _top;
  1.1623 +  JNIMethodBlock* _next;
  1.1624 + public:
  1.1625 +  static Method* const _free_method;
  1.1626 +
  1.1627 +  JNIMethodBlock() : _next(NULL), _top(0) {
  1.1628 +    for (int i = 0; i< number_of_methods; i++) _methods[i] = _free_method;
  1.1629 +  }
  1.1630 +
  1.1631 +  Method** add_method(Method* m) {
  1.1632 +    if (_top < number_of_methods) {
  1.1633 +      // top points to the next free entry.
  1.1634 +      int i = _top;
  1.1635 +      _methods[i] = m;
  1.1636 +      _top++;
  1.1637 +      return &_methods[i];
  1.1638 +    } else if (_top == number_of_methods) {
  1.1639 +      // if the next free entry ran off the block see if there's a free entry
  1.1640 +      for (int i = 0; i< number_of_methods; i++) {
  1.1641 +        if (_methods[i] == _free_method) {
  1.1642 +          _methods[i] = m;
  1.1643 +          return &_methods[i];
  1.1644 +        }
  1.1645 +      }
  1.1646 +      // Only check each block once for frees.  They're very unlikely.
  1.1647 +      // Increment top past the end of the block.
  1.1648 +      _top++;
  1.1649 +    }
  1.1650 +    // need to allocate a next block.
  1.1651 +    if (_next == NULL) {
  1.1652 +      _next = new JNIMethodBlock();
  1.1653 +    }
  1.1654 +    return _next->add_method(m);
  1.1655 +  }
  1.1656 +
  1.1657 +  bool contains(Method** m) {
  1.1658 +    for (JNIMethodBlock* b = this; b != NULL; b = b->_next) {
  1.1659 +      for (int i = 0; i< number_of_methods; i++) {
  1.1660 +        if (&(b->_methods[i]) == m) {
  1.1661 +          return true;
  1.1662 +        }
  1.1663 +      }
  1.1664 +    }
  1.1665 +    return false;  // not found
  1.1666 +  }
  1.1667 +
  1.1668 +  // Doesn't really destroy it, just marks it as free so it can be reused.
  1.1669 +  void destroy_method(Method** m) {
  1.1670 +#ifdef ASSERT
  1.1671 +    assert(contains(m), "should be a methodID");
  1.1672 +#endif // ASSERT
  1.1673 +    *m = _free_method;
  1.1674 +  }
  1.1675 +
  1.1676 +  // During class unloading the methods are cleared, which is different
  1.1677 +  // than freed.
  1.1678 +  void clear_all_methods() {
  1.1679 +    for (JNIMethodBlock* b = this; b != NULL; b = b->_next) {
  1.1680 +      for (int i = 0; i< number_of_methods; i++) {
  1.1681 +        _methods[i] = NULL;
  1.1682 +      }
  1.1683 +    }
  1.1684 +  }
  1.1685 +#ifndef PRODUCT
  1.1686 +  int count_methods() {
  1.1687 +    // count all allocated methods
  1.1688 +    int count = 0;
  1.1689 +    for (JNIMethodBlock* b = this; b != NULL; b = b->_next) {
  1.1690 +      for (int i = 0; i< number_of_methods; i++) {
  1.1691 +        if (_methods[i] != _free_method) count++;
  1.1692 +      }
  1.1693 +    }
  1.1694 +    return count;
  1.1695 +  }
  1.1696 +#endif // PRODUCT
  1.1697 +};
  1.1698 +
  1.1699 +// Something that can't be mistaken for an address or a markOop
  1.1700 +Method* const JNIMethodBlock::_free_method = (Method*)55;
  1.1701 +
  1.1702 +// Add a method id to the jmethod_ids
  1.1703 +jmethodID Method::make_jmethod_id(ClassLoaderData* loader_data, Method* m) {
  1.1704 +  ClassLoaderData* cld = loader_data;
  1.1705 +
  1.1706 +  if (!SafepointSynchronize::is_at_safepoint()) {
  1.1707 +    // Have to add jmethod_ids() to class loader data thread-safely.
  1.1708 +    // Also have to add the method to the list safely, which the cld lock
  1.1709 +    // protects as well.
  1.1710 +    MutexLockerEx ml(cld->metaspace_lock(),  Mutex::_no_safepoint_check_flag);
  1.1711 +    if (cld->jmethod_ids() == NULL) {
  1.1712 +      cld->set_jmethod_ids(new JNIMethodBlock());
  1.1713 +    }
  1.1714 +    // jmethodID is a pointer to Method*
  1.1715 +    return (jmethodID)cld->jmethod_ids()->add_method(m);
  1.1716 +  } else {
  1.1717 +    // At safepoint, we are single threaded and can set this.
  1.1718 +    if (cld->jmethod_ids() == NULL) {
  1.1719 +      cld->set_jmethod_ids(new JNIMethodBlock());
  1.1720 +    }
  1.1721 +    // jmethodID is a pointer to Method*
  1.1722 +    return (jmethodID)cld->jmethod_ids()->add_method(m);
  1.1723 +  }
  1.1724 +}
  1.1725 +
  1.1726 +// Mark a jmethodID as free.  This is called when there is a data race in
  1.1727 +// InstanceKlass while creating the jmethodID cache.
  1.1728 +void Method::destroy_jmethod_id(ClassLoaderData* loader_data, jmethodID m) {
  1.1729 +  ClassLoaderData* cld = loader_data;
  1.1730 +  Method** ptr = (Method**)m;
  1.1731 +  assert(cld->jmethod_ids() != NULL, "should have method handles");
  1.1732 +  cld->jmethod_ids()->destroy_method(ptr);
  1.1733 +}
  1.1734 +
  1.1735 +void Method::change_method_associated_with_jmethod_id(jmethodID jmid, Method* new_method) {
  1.1736 +  // Can't assert the method_holder is the same because the new method has the
  1.1737 +  // scratch method holder.
  1.1738 +  assert(resolve_jmethod_id(jmid)->method_holder()->class_loader()
  1.1739 +           == new_method->method_holder()->class_loader(),
  1.1740 +         "changing to a different class loader");
  1.1741 +  // Just change the method in place, jmethodID pointer doesn't change.
  1.1742 +  *((Method**)jmid) = new_method;
  1.1743 +}
  1.1744 +
  1.1745 +bool Method::is_method_id(jmethodID mid) {
  1.1746 +  Method* m = resolve_jmethod_id(mid);
  1.1747 +  assert(m != NULL, "should be called with non-null method");
  1.1748 +  InstanceKlass* ik = InstanceKlass::cast(m->method_holder());
  1.1749 +  ClassLoaderData* cld = ik->class_loader_data();
  1.1750 +  if (cld->jmethod_ids() == NULL) return false;
  1.1751 +  return (cld->jmethod_ids()->contains((Method**)mid));
  1.1752 +}
  1.1753 +
  1.1754 +Method* Method::checked_resolve_jmethod_id(jmethodID mid) {
  1.1755 +  if (mid == NULL) return NULL;
  1.1756 +  Method* o = resolve_jmethod_id(mid);
  1.1757 +  if (o == NULL || o == JNIMethodBlock::_free_method || !((Metadata*)o)->is_method()) {
  1.1758 +    return NULL;
  1.1759 +  }
  1.1760 +  return o;
  1.1761 +};
  1.1762 +
  1.1763 +void Method::set_on_stack(const bool value) {
  1.1764 +  // Set both the method itself and its constant pool.  The constant pool
  1.1765 +  // on stack means some method referring to it is also on the stack.
  1.1766 +  _access_flags.set_on_stack(value);
  1.1767 +  constants()->set_on_stack(value);
  1.1768 +  if (value) MetadataOnStackMark::record(this);
  1.1769 +}
  1.1770 +
  1.1771 +// Called when the class loader is unloaded to make all methods weak.
  1.1772 +void Method::clear_jmethod_ids(ClassLoaderData* loader_data) {
  1.1773 +  loader_data->jmethod_ids()->clear_all_methods();
  1.1774 +}
  1.1775 +
  1.1776 +#ifndef PRODUCT
  1.1777 +void Method::print_jmethod_ids(ClassLoaderData* loader_data, outputStream* out) {
  1.1778 +  out->print_cr("jni_method_id count = %d", loader_data->jmethod_ids()->count_methods());
  1.1779 +}
  1.1780 +#endif // PRODUCT
  1.1781 +
  1.1782 +
  1.1783 +// Printing
  1.1784 +
  1.1785 +#ifndef PRODUCT
  1.1786 +
  1.1787 +void Method::print_on(outputStream* st) const {
  1.1788 +  ResourceMark rm;
  1.1789 +  assert(is_method(), "must be method");
  1.1790 +  st->print_cr(internal_name());
  1.1791 +  // get the effect of PrintOopAddress, always, for methods:
  1.1792 +  st->print_cr(" - this oop:          "INTPTR_FORMAT, (intptr_t)this);
  1.1793 +  st->print   (" - method holder:     "); method_holder()->print_value_on(st); st->cr();
  1.1794 +  st->print   (" - constants:         "INTPTR_FORMAT" ", (address)constants());
  1.1795 +  constants()->print_value_on(st); st->cr();
  1.1796 +  st->print   (" - access:            0x%x  ", access_flags().as_int()); access_flags().print_on(st); st->cr();
  1.1797 +  st->print   (" - name:              ");    name()->print_value_on(st); st->cr();
  1.1798 +  st->print   (" - signature:         ");    signature()->print_value_on(st); st->cr();
  1.1799 +  st->print_cr(" - max stack:         %d",   max_stack());
  1.1800 +  st->print_cr(" - max locals:        %d",   max_locals());
  1.1801 +  st->print_cr(" - size of params:    %d",   size_of_parameters());
  1.1802 +  st->print_cr(" - method size:       %d",   method_size());
  1.1803 +  if (intrinsic_id() != vmIntrinsics::_none)
  1.1804 +    st->print_cr(" - intrinsic id:      %d %s", intrinsic_id(), vmIntrinsics::name_at(intrinsic_id()));
  1.1805 +  if (highest_comp_level() != CompLevel_none)
  1.1806 +    st->print_cr(" - highest level:     %d", highest_comp_level());
  1.1807 +  st->print_cr(" - vtable index:      %d",   _vtable_index);
  1.1808 +  st->print_cr(" - i2i entry:         " INTPTR_FORMAT, interpreter_entry());
  1.1809 +  st->print(   " - adapters:          ");
  1.1810 +  AdapterHandlerEntry* a = ((Method*)this)->adapter();
  1.1811 +  if (a == NULL)
  1.1812 +    st->print_cr(INTPTR_FORMAT, a);
  1.1813 +  else
  1.1814 +    a->print_adapter_on(st);
  1.1815 +  st->print_cr(" - compiled entry     " INTPTR_FORMAT, from_compiled_entry());
  1.1816 +  st->print_cr(" - code size:         %d",   code_size());
  1.1817 +  if (code_size() != 0) {
  1.1818 +    st->print_cr(" - code start:        " INTPTR_FORMAT, code_base());
  1.1819 +    st->print_cr(" - code end (excl):   " INTPTR_FORMAT, code_base() + code_size());
  1.1820 +  }
  1.1821 +  if (method_data() != NULL) {
  1.1822 +    st->print_cr(" - method data:       " INTPTR_FORMAT, (address)method_data());
  1.1823 +  }
  1.1824 +  st->print_cr(" - checked ex length: %d",   checked_exceptions_length());
  1.1825 +  if (checked_exceptions_length() > 0) {
  1.1826 +    CheckedExceptionElement* table = checked_exceptions_start();
  1.1827 +    st->print_cr(" - checked ex start:  " INTPTR_FORMAT, table);
  1.1828 +    if (Verbose) {
  1.1829 +      for (int i = 0; i < checked_exceptions_length(); i++) {
  1.1830 +        st->print_cr("   - throws %s", constants()->printable_name_at(table[i].class_cp_index));
  1.1831 +      }
  1.1832 +    }
  1.1833 +  }
  1.1834 +  if (has_linenumber_table()) {
  1.1835 +    u_char* table = compressed_linenumber_table();
  1.1836 +    st->print_cr(" - linenumber start:  " INTPTR_FORMAT, table);
  1.1837 +    if (Verbose) {
  1.1838 +      CompressedLineNumberReadStream stream(table);
  1.1839 +      while (stream.read_pair()) {
  1.1840 +        st->print_cr("   - line %d: %d", stream.line(), stream.bci());
  1.1841 +      }
  1.1842 +    }
  1.1843 +  }
  1.1844 +  st->print_cr(" - localvar length:   %d",   localvariable_table_length());
  1.1845 +  if (localvariable_table_length() > 0) {
  1.1846 +    LocalVariableTableElement* table = localvariable_table_start();
  1.1847 +    st->print_cr(" - localvar start:    " INTPTR_FORMAT, table);
  1.1848 +    if (Verbose) {
  1.1849 +      for (int i = 0; i < localvariable_table_length(); i++) {
  1.1850 +        int bci = table[i].start_bci;
  1.1851 +        int len = table[i].length;
  1.1852 +        const char* name = constants()->printable_name_at(table[i].name_cp_index);
  1.1853 +        const char* desc = constants()->printable_name_at(table[i].descriptor_cp_index);
  1.1854 +        int slot = table[i].slot;
  1.1855 +        st->print_cr("   - %s %s bci=%d len=%d slot=%d", desc, name, bci, len, slot);
  1.1856 +      }
  1.1857 +    }
  1.1858 +  }
  1.1859 +  if (code() != NULL) {
  1.1860 +    st->print   (" - compiled code: ");
  1.1861 +    code()->print_value_on(st);
  1.1862 +  }
  1.1863 +  if (is_native()) {
  1.1864 +    st->print_cr(" - native function:   " INTPTR_FORMAT, native_function());
  1.1865 +    st->print_cr(" - signature handler: " INTPTR_FORMAT, signature_handler());
  1.1866 +  }
  1.1867 +}
  1.1868 +
  1.1869 +#endif //PRODUCT
  1.1870 +
  1.1871 +void Method::print_value_on(outputStream* st) const {
  1.1872 +  assert(is_method(), "must be method");
  1.1873 +  st->print_cr(internal_name());
  1.1874 +  print_address_on(st);
  1.1875 +  st->print(" ");
  1.1876 +  name()->print_value_on(st);
  1.1877 +  st->print(" ");
  1.1878 +  signature()->print_value_on(st);
  1.1879 +  st->print(" in ");
  1.1880 +  method_holder()->print_value_on(st);
  1.1881 +  if (WizardMode) st->print("[%d,%d]", size_of_parameters(), max_locals());
  1.1882 +  if (WizardMode && code() != NULL) st->print(" ((nmethod*)%p)", code());
  1.1883 +}
  1.1884 +
  1.1885 +
  1.1886 +// Verification
  1.1887 +
  1.1888 +void Method::verify_on(outputStream* st) {
  1.1889 +  guarantee(is_method(), "object must be method");
  1.1890 +  guarantee(is_metadata(),  "should be metadata");
  1.1891 +  guarantee(constants()->is_constantPool(), "should be constant pool");
  1.1892 +  guarantee(constants()->is_metadata(), "should be metadata");
  1.1893 +  guarantee(constMethod()->is_constMethod(), "should be ConstMethod*");
  1.1894 +  guarantee(constMethod()->is_metadata(), "should be metadata");
  1.1895 +  MethodData* md = method_data();
  1.1896 +  guarantee(md == NULL ||
  1.1897 +      md->is_metadata(), "should be in permspace");
  1.1898 +  guarantee(md == NULL ||
  1.1899 +      md->is_methodData(), "should be method data");
  1.1900 +}

mercurial