1 /*
   2  * Copyright (c) 1999, 2018, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  *
  23  */
  24 
  25 #include "precompiled.hpp"
  26 #include "jvm.h"
  27 #include "ci/ciConstant.hpp"
  28 #include "ci/ciEnv.hpp"
  29 #include "ci/ciField.hpp"
  30 #include "ci/ciInstance.hpp"
  31 #include "ci/ciInstanceKlass.hpp"
  32 #include "ci/ciMethod.hpp"
  33 #include "ci/ciNullObject.hpp"
  34 #include "ci/ciReplay.hpp"
  35 #include "ci/ciUtilities.inline.hpp"
  36 #include "ci/ciValueKlass.hpp"
  37 #include "classfile/systemDictionary.hpp"
  38 #include "classfile/vmSymbols.hpp"
  39 #include "code/codeCache.hpp"
  40 #include "code/scopeDesc.hpp"
  41 #include "compiler/compileBroker.hpp"
  42 #include "compiler/compileLog.hpp"
  43 #include "compiler/disassembler.hpp"
  44 #include "gc/shared/collectedHeap.inline.hpp"
  45 #include "interpreter/linkResolver.hpp"
  46 #include "jfr/jfrEvents.hpp"
  47 #include "memory/allocation.inline.hpp"
  48 #include "memory/oopFactory.hpp"
  49 #include "memory/resourceArea.hpp"
  50 #include "memory/universe.hpp"
  51 #include "oops/constantPool.inline.hpp"
  52 #include "oops/cpCache.inline.hpp"
  53 #include "oops/method.inline.hpp"
  54 #include "oops/methodData.hpp"
  55 #include "oops/objArrayKlass.hpp"
  56 #include "oops/objArrayOop.inline.hpp"
  57 #include "oops/oop.inline.hpp"
  58 #include "prims/jvmtiExport.hpp"
  59 #include "runtime/init.hpp"
  60 #include "runtime/reflection.hpp"
  61 #include "runtime/jniHandles.inline.hpp"
  62 #include "runtime/safepointVerifiers.hpp"
  63 #include "runtime/sharedRuntime.hpp"
  64 #include "runtime/thread.inline.hpp"
  65 #include "utilities/dtrace.hpp"
  66 #include "utilities/macros.hpp"
  67 #ifdef COMPILER1
  68 #include "c1/c1_Runtime1.hpp"
  69 #endif
  70 #ifdef COMPILER2
  71 #include "opto/runtime.hpp"
  72 #endif
  73 
  74 // ciEnv
  75 //
  76 // This class is the top level broker for requests from the compiler
  77 // to the VM.
  78 
  79 ciObject*              ciEnv::_null_object_instance;
  80 
  81 #define WK_KLASS_DEFN(name, ignore_s, ignore_o) ciInstanceKlass* ciEnv::_##name = NULL;
  82 WK_KLASSES_DO(WK_KLASS_DEFN)
  83 #undef WK_KLASS_DEFN
  84 
  85 ciSymbol*        ciEnv::_unloaded_cisymbol = NULL;
  86 ciInstanceKlass* ciEnv::_unloaded_ciinstance_klass = NULL;
  87 ciObjArrayKlass* ciEnv::_unloaded_ciobjarrayklass = NULL;
  88 
  89 jobject ciEnv::_ArrayIndexOutOfBoundsException_handle = NULL;
  90 jobject ciEnv::_ArrayStoreException_handle = NULL;
  91 jobject ciEnv::_ClassCastException_handle = NULL;
  92 
  93 #ifndef PRODUCT
  94 static bool firstEnv = true;
  95 #endif /* PRODUCT */
  96 
  97 // ------------------------------------------------------------------
  98 // ciEnv::ciEnv
  99 ciEnv::ciEnv(CompileTask* task, int system_dictionary_modification_counter)
 100   : _ciEnv_arena(mtCompiler) {
 101   VM_ENTRY_MARK;
 102 
 103   // Set up ciEnv::current immediately, for the sake of ciObjectFactory, etc.
 104   thread->set_env(this);
 105   assert(ciEnv::current() == this, "sanity");
 106 
 107   _oop_recorder = NULL;
 108   _debug_info = NULL;
 109   _dependencies = NULL;
 110   _failure_reason = NULL;
 111   _inc_decompile_count_on_failure = true;
 112   _compilable = MethodCompilable;
 113   _break_at_compile = false;
 114   _compiler_data = NULL;
 115 #ifndef PRODUCT
 116   assert(!firstEnv, "not initialized properly");
 117 #endif /* !PRODUCT */
 118 
 119   _system_dictionary_modification_counter = system_dictionary_modification_counter;
 120   _num_inlined_bytecodes = 0;
 121   assert(task == NULL || thread->task() == task, "sanity");
 122   _task = task;
 123   _log = NULL;
 124 
 125   // Temporary buffer for creating symbols and such.
 126   _name_buffer = NULL;
 127   _name_buffer_len = 0;
 128 
 129   _arena   = &_ciEnv_arena;
 130   _factory = new (_arena) ciObjectFactory(_arena, 128);
 131 
 132   // Preload commonly referenced system ciObjects.
 133 
 134   // During VM initialization, these instances have not yet been created.
 135   // Assertions ensure that these instances are not accessed before
 136   // their initialization.
 137 
 138   assert(Universe::is_fully_initialized(), "should be complete");
 139 
 140   oop o = Universe::null_ptr_exception_instance();
 141   assert(o != NULL, "should have been initialized");
 142   _NullPointerException_instance = get_object(o)->as_instance();
 143   o = Universe::arithmetic_exception_instance();
 144   assert(o != NULL, "should have been initialized");
 145   _ArithmeticException_instance = get_object(o)->as_instance();
 146 
 147   _ArrayIndexOutOfBoundsException_instance = NULL;
 148   _ArrayStoreException_instance = NULL;
 149   _ClassCastException_instance = NULL;
 150   _the_null_string = NULL;
 151   _the_min_jint_string = NULL;
 152 
 153   _jvmti_can_hotswap_or_post_breakpoint = false;
 154   _jvmti_can_access_local_variables = false;
 155   _jvmti_can_post_on_exceptions = false;
 156   _jvmti_can_pop_frame = false;
 157 }
 158 
 159 ciEnv::ciEnv(Arena* arena) : _ciEnv_arena(mtCompiler) {
 160   ASSERT_IN_VM;
 161 
 162   // Set up ciEnv::current immediately, for the sake of ciObjectFactory, etc.
 163   CompilerThread* current_thread = CompilerThread::current();
 164   assert(current_thread->env() == NULL, "must be");
 165   current_thread->set_env(this);
 166   assert(ciEnv::current() == this, "sanity");
 167 
 168   _oop_recorder = NULL;
 169   _debug_info = NULL;
 170   _dependencies = NULL;
 171   _failure_reason = NULL;
 172   _inc_decompile_count_on_failure = true;
 173   _compilable = MethodCompilable_never;
 174   _break_at_compile = false;
 175   _compiler_data = NULL;
 176 #ifndef PRODUCT
 177   assert(firstEnv, "must be first");
 178   firstEnv = false;
 179 #endif /* !PRODUCT */
 180 
 181   _system_dictionary_modification_counter = 0;
 182   _num_inlined_bytecodes = 0;
 183   _task = NULL;
 184   _log = NULL;
 185 
 186   // Temporary buffer for creating symbols and such.
 187   _name_buffer = NULL;
 188   _name_buffer_len = 0;
 189 
 190   _arena   = arena;
 191   _factory = new (_arena) ciObjectFactory(_arena, 128);
 192 
 193   // Preload commonly referenced system ciObjects.
 194 
 195   // During VM initialization, these instances have not yet been created.
 196   // Assertions ensure that these instances are not accessed before
 197   // their initialization.
 198 
 199   assert(Universe::is_fully_initialized(), "must be");
 200 
 201   _NullPointerException_instance = NULL;
 202   _ArithmeticException_instance = NULL;
 203   _ArrayIndexOutOfBoundsException_instance = NULL;
 204   _ArrayStoreException_instance = NULL;
 205   _ClassCastException_instance = NULL;
 206   _the_null_string = NULL;
 207   _the_min_jint_string = NULL;
 208 
 209   _jvmti_can_hotswap_or_post_breakpoint = false;
 210   _jvmti_can_access_local_variables = false;
 211   _jvmti_can_post_on_exceptions = false;
 212   _jvmti_can_pop_frame = false;
 213 }
 214 
 215 ciEnv::~ciEnv() {
 216   GUARDED_VM_ENTRY(
 217       CompilerThread* current_thread = CompilerThread::current();
 218       _factory->remove_symbols();
 219       // Need safepoint to clear the env on the thread.  RedefineClasses might
 220       // be reading it.
 221       current_thread->set_env(NULL);
 222   )
 223 }
 224 
 225 // ------------------------------------------------------------------
 226 // Cache Jvmti state
 227 void ciEnv::cache_jvmti_state() {
 228   VM_ENTRY_MARK;
 229   // Get Jvmti capabilities under lock to get consistant values.
 230   MutexLocker mu(JvmtiThreadState_lock);
 231   _jvmti_can_hotswap_or_post_breakpoint = JvmtiExport::can_hotswap_or_post_breakpoint();
 232   _jvmti_can_access_local_variables     = JvmtiExport::can_access_local_variables();
 233   _jvmti_can_post_on_exceptions         = JvmtiExport::can_post_on_exceptions();
 234   _jvmti_can_pop_frame                  = JvmtiExport::can_pop_frame();
 235 }
 236 
 237 bool ciEnv::should_retain_local_variables() const {
 238   return _jvmti_can_access_local_variables || _jvmti_can_pop_frame;
 239 }
 240 
 241 bool ciEnv::jvmti_state_changed() const {
 242   if (!_jvmti_can_access_local_variables &&
 243       JvmtiExport::can_access_local_variables()) {
 244     return true;
 245   }
 246   if (!_jvmti_can_hotswap_or_post_breakpoint &&
 247       JvmtiExport::can_hotswap_or_post_breakpoint()) {
 248     return true;
 249   }
 250   if (!_jvmti_can_post_on_exceptions &&
 251       JvmtiExport::can_post_on_exceptions()) {
 252     return true;
 253   }
 254   if (!_jvmti_can_pop_frame &&
 255       JvmtiExport::can_pop_frame()) {
 256     return true;
 257   }
 258   return false;
 259 }
 260 
 261 // ------------------------------------------------------------------
 262 // Cache DTrace flags
 263 void ciEnv::cache_dtrace_flags() {
 264   // Need lock?
 265   _dtrace_extended_probes = ExtendedDTraceProbes;
 266   if (_dtrace_extended_probes) {
 267     _dtrace_monitor_probes  = true;
 268     _dtrace_method_probes   = true;
 269     _dtrace_alloc_probes    = true;
 270   } else {
 271     _dtrace_monitor_probes  = DTraceMonitorProbes;
 272     _dtrace_method_probes   = DTraceMethodProbes;
 273     _dtrace_alloc_probes    = DTraceAllocProbes;
 274   }
 275 }
 276 
 277 // ------------------------------------------------------------------
 278 // helper for lazy exception creation
 279 ciInstance* ciEnv::get_or_create_exception(jobject& handle, Symbol* name) {
 280   VM_ENTRY_MARK;
 281   if (handle == NULL) {
 282     // Cf. universe.cpp, creation of Universe::_null_ptr_exception_instance.
 283     Klass* k = SystemDictionary::find(name, Handle(), Handle(), THREAD);
 284     jobject objh = NULL;
 285     if (!HAS_PENDING_EXCEPTION && k != NULL) {
 286       oop obj = InstanceKlass::cast(k)->allocate_instance(THREAD);
 287       if (!HAS_PENDING_EXCEPTION)
 288         objh = JNIHandles::make_global(Handle(THREAD, obj));
 289     }
 290     if (HAS_PENDING_EXCEPTION) {
 291       CLEAR_PENDING_EXCEPTION;
 292     } else {
 293       handle = objh;
 294     }
 295   }
 296   oop obj = JNIHandles::resolve(handle);
 297   return obj == NULL? NULL: get_object(obj)->as_instance();
 298 }
 299 
 300 ciInstance* ciEnv::ArrayIndexOutOfBoundsException_instance() {
 301   if (_ArrayIndexOutOfBoundsException_instance == NULL) {
 302     _ArrayIndexOutOfBoundsException_instance
 303           = get_or_create_exception(_ArrayIndexOutOfBoundsException_handle,
 304           vmSymbols::java_lang_ArrayIndexOutOfBoundsException());
 305   }
 306   return _ArrayIndexOutOfBoundsException_instance;
 307 }
 308 ciInstance* ciEnv::ArrayStoreException_instance() {
 309   if (_ArrayStoreException_instance == NULL) {
 310     _ArrayStoreException_instance
 311           = get_or_create_exception(_ArrayStoreException_handle,
 312           vmSymbols::java_lang_ArrayStoreException());
 313   }
 314   return _ArrayStoreException_instance;
 315 }
 316 ciInstance* ciEnv::ClassCastException_instance() {
 317   if (_ClassCastException_instance == NULL) {
 318     _ClassCastException_instance
 319           = get_or_create_exception(_ClassCastException_handle,
 320           vmSymbols::java_lang_ClassCastException());
 321   }
 322   return _ClassCastException_instance;
 323 }
 324 
 325 ciInstance* ciEnv::the_null_string() {
 326   if (_the_null_string == NULL) {
 327     VM_ENTRY_MARK;
 328     _the_null_string = get_object(Universe::the_null_string())->as_instance();
 329   }
 330   return _the_null_string;
 331 }
 332 
 333 ciInstance* ciEnv::the_min_jint_string() {
 334   if (_the_min_jint_string == NULL) {
 335     VM_ENTRY_MARK;
 336     _the_min_jint_string = get_object(Universe::the_min_jint_string())->as_instance();
 337   }
 338   return _the_min_jint_string;
 339 }
 340 
 341 // ------------------------------------------------------------------
 342 // ciEnv::get_method_from_handle
 343 ciMethod* ciEnv::get_method_from_handle(Method* method) {
 344   VM_ENTRY_MARK;
 345   return get_metadata(method)->as_method();
 346 }
 347 
 348 // ------------------------------------------------------------------
 349 // ciEnv::array_element_offset_in_bytes
 350 int ciEnv::array_element_offset_in_bytes(ciArray* a_h, ciObject* o_h) {
 351   VM_ENTRY_MARK;
 352   objArrayOop a = (objArrayOop)a_h->get_oop();
 353   assert(a->is_objArray(), "");
 354   int length = a->length();
 355   oop o = o_h->get_oop();
 356   for (int i = 0; i < length; i++) {
 357     if (a->obj_at(i) == o)  return i;
 358   }
 359   return -1;
 360 }
 361 
 362 
 363 // ------------------------------------------------------------------
 364 // ciEnv::check_klass_accessiblity
 365 //
 366 // Note: the logic of this method should mirror the logic of
 367 // ConstantPool::verify_constant_pool_resolve.
 368 bool ciEnv::check_klass_accessibility(ciKlass* accessing_klass,
 369                                       Klass* resolved_klass) {
 370   if (accessing_klass == NULL || !accessing_klass->is_loaded()) {
 371     return true;
 372   }
 373   if (accessing_klass->is_obj_array_klass()) {
 374     accessing_klass = accessing_klass->as_obj_array_klass()->base_element_klass();
 375   }
 376   if (!accessing_klass->is_instance_klass()) {
 377     return true;
 378   }
 379 
 380   if (resolved_klass->is_objArray_klass()) {
 381     // Find the element klass, if this is an array.
 382     resolved_klass = ObjArrayKlass::cast(resolved_klass)->bottom_klass();
 383   }
 384   if (resolved_klass->is_instance_klass()) {
 385     return (Reflection::verify_class_access(accessing_klass->get_Klass(),
 386                                             InstanceKlass::cast(resolved_klass),
 387                                             true) == Reflection::ACCESS_OK);
 388   }
 389   return true;
 390 }
 391 
 392 // ------------------------------------------------------------------
 393 // ciEnv::get_klass_by_name_impl
 394 ciKlass* ciEnv::get_klass_by_name_impl(ciKlass* accessing_klass,
 395                                        const constantPoolHandle& cpool,
 396                                        ciSymbol* name,
 397                                        bool require_local,
 398                                        bool is_value_type) {
 399   ASSERT_IN_VM;
 400   EXCEPTION_CONTEXT;
 401 
 402   // Now we need to check the SystemDictionary
 403   Symbol* sym = name->get_symbol();
 404   if ((sym->byte_at(0) == 'L' || sym->byte_at(0) == 'Q') &&
 405     sym->byte_at(sym->utf8_length()-1) == ';') {
 406     // This is a name from a signature.  Strip off the trimmings.
 407     // Call recursive to keep scope of strippedsym.
 408     TempNewSymbol strippedsym = SymbolTable::new_symbol(sym->as_utf8()+1,
 409                     sym->utf8_length()-2,
 410                     KILL_COMPILE_ON_FATAL_(_unloaded_ciinstance_klass));
 411     ciSymbol* strippedname = get_symbol(strippedsym);
 412     is_value_type = (sym->byte_at(0) == 'Q');
 413     return get_klass_by_name_impl(accessing_klass, cpool, strippedname, require_local, is_value_type);
 414   }
 415 
 416   // Check for prior unloaded klass.  The SystemDictionary's answers
 417   // can vary over time but the compiler needs consistency.
 418   ciKlass* unloaded_klass = check_get_unloaded_klass(accessing_klass, name);
 419   if (unloaded_klass != NULL) {
 420     if (require_local)  return NULL;
 421     return unloaded_klass;
 422   }
 423 
 424   Handle loader(THREAD, (oop)NULL);
 425   Handle domain(THREAD, (oop)NULL);
 426   if (accessing_klass != NULL) {
 427     loader = Handle(THREAD, accessing_klass->loader());
 428     domain = Handle(THREAD, accessing_klass->protection_domain());
 429   }
 430 
 431   // setup up the proper type to return on OOM
 432   ciKlass* fail_type;
 433   if (sym->byte_at(0) == '[') {
 434     fail_type = _unloaded_ciobjarrayklass;
 435   } else {
 436     fail_type = _unloaded_ciinstance_klass;
 437   }
 438   Klass* found_klass;
 439   {
 440     ttyUnlocker ttyul;  // release tty lock to avoid ordering problems
 441     MutexLocker ml(Compile_lock);
 442     Klass* kls;
 443     if (!require_local) {
 444       kls = SystemDictionary::find_constrained_instance_or_array_klass(sym, loader,
 445                                                                        KILL_COMPILE_ON_FATAL_(fail_type));
 446     } else {
 447       kls = SystemDictionary::find_instance_or_array_klass(sym, loader, domain,
 448                                                            KILL_COMPILE_ON_FATAL_(fail_type));
 449     }
 450     found_klass = kls;
 451   }
 452 
 453   // If we fail to find an array klass, look again for its element type.
 454   // The element type may be available either locally or via constraints.
 455   // In either case, if we can find the element type in the system dictionary,
 456   // we must build an array type around it.  The CI requires array klasses
 457   // to be loaded if their element klasses are loaded, except when memory
 458   // is exhausted.
 459   if (sym->byte_at(0) == '[' &&
 460       (sym->byte_at(1) == '[' || sym->byte_at(1) == 'L' || sym->byte_at(1) == 'Q')) {
 461     // We have an unloaded array.
 462     // Build it on the fly if the element class exists.
 463     TempNewSymbol elem_sym = SymbolTable::new_symbol(sym->as_utf8()+1,
 464                                                  sym->utf8_length()-1,
 465                                                  KILL_COMPILE_ON_FATAL_(fail_type));
 466 
 467     // Get element ciKlass recursively.
 468     is_value_type = (sym->byte_at(1) == 'Q');
 469     ciKlass* elem_klass =
 470       get_klass_by_name_impl(accessing_klass,
 471                              cpool,
 472                              get_symbol(elem_sym),
 473                              require_local, is_value_type);
 474     if (elem_klass != NULL && elem_klass->is_loaded()) {
 475       // Now make an array for it
 476       if (elem_klass->is_valuetype() && elem_klass->as_value_klass()->flatten_array()) {
 477         return ciValueArrayKlass::make_impl(elem_klass);
 478       } else {
 479         return ciObjArrayKlass::make_impl(elem_klass);
 480       }
 481     }
 482   }
 483 
 484   if (found_klass == NULL && !cpool.is_null() && cpool->has_preresolution()) {
 485     // Look inside the constant pool for pre-resolved class entries.
 486     for (int i = cpool->length() - 1; i >= 1; i--) {
 487       if (cpool->tag_at(i).is_klass()) {
 488         Klass* kls = cpool->resolved_klass_at(i);
 489         if (kls->name() == sym) {
 490           found_klass = kls;
 491           break;
 492         }
 493       }
 494     }
 495   }
 496 
 497   if (found_klass != NULL) {
 498     // Found it.  Build a CI handle.
 499     return get_klass(found_klass);
 500   }
 501 
 502   if (require_local)  return NULL;
 503 
 504   // Not yet loaded into the VM, or not governed by loader constraints.
 505   // Make a CI representative for it.
 506   int i = 0;
 507   while (sym->byte_at(i) == '[') {
 508     i++;
 509   }
 510   if (i > 0 && sym->byte_at(i) == 'Q') {
 511     if (EnableValhallaC1) {
 512       return get_unloaded_klass(accessing_klass, name, true);
 513     } else {
 514       // FIXME - C2 can't handle unloaded ciValueKlass
 515       
 516       // An unloaded array class of value types is an ObjArrayKlass, an
 517       // unloaded value type class is an InstanceKlass. For consistency,
 518       // make the signature of the unloaded array of value type use L
 519       // rather than Q.
 520       char *new_name = CURRENT_THREAD_ENV->name_buffer(sym->utf8_length()+1);
 521       strncpy(new_name, (char*)sym->base(), sym->utf8_length());
 522       new_name[i] = 'L';
 523       new_name[sym->utf8_length()] = '\0';
 524       return get_unloaded_klass(accessing_klass, ciSymbol::make(new_name), false);
 525     }
 526   }
 527   return get_unloaded_klass(accessing_klass, name, is_value_type);
 528 }
 529 
 530 // ------------------------------------------------------------------
 531 // ciEnv::get_klass_by_name
 532 ciKlass* ciEnv::get_klass_by_name(ciKlass* accessing_klass,
 533                                   ciSymbol* klass_name,
 534                                   bool require_local) {
 535   GUARDED_VM_ENTRY(return get_klass_by_name_impl(accessing_klass,
 536                                                  constantPoolHandle(),
 537                                                  klass_name,
 538                                                  require_local);)
 539 }
 540 
 541 // ------------------------------------------------------------------
 542 // ciEnv::get_klass_by_index_impl
 543 //
 544 // Implementation of get_klass_by_index.
 545 ciKlass* ciEnv::get_klass_by_index_impl(const constantPoolHandle& cpool,
 546                                         int index,
 547                                         bool& is_accessible,
 548                                         ciInstanceKlass* accessor) {
 549   EXCEPTION_CONTEXT;
 550   Klass* klass = NULL;
 551   Symbol* klass_name = NULL;
 552 
 553   if (cpool->tag_at(index).is_symbol()) {
 554     klass_name = cpool->symbol_at(index);
 555   } else {
 556     // Check if it's resolved if it's not a symbol constant pool entry.
 557     klass =  ConstantPool::klass_at_if_loaded(cpool, index);
 558     // Try to look it up by name.
 559     if (klass == NULL) {
 560       klass_name = cpool->klass_name_at(index);
 561     }
 562   }
 563 
 564   if (klass == NULL) {
 565     // Not found in constant pool.  Use the name to do the lookup.
 566     ciKlass* k = get_klass_by_name_impl(accessor,
 567                                         cpool,
 568                                         get_symbol(klass_name),
 569                                         false);
 570     // Calculate accessibility the hard way.
 571     if (!k->is_loaded()) {
 572       is_accessible = false;
 573     } else if (!oopDesc::equals(k->loader(), accessor->loader()) &&
 574                get_klass_by_name_impl(accessor, cpool, k->name(), true) == NULL) {
 575       // Loaded only remotely.  Not linked yet.
 576       is_accessible = false;
 577     } else {
 578       // Linked locally, and we must also check public/private, etc.
 579       is_accessible = check_klass_accessibility(accessor, k->get_Klass());
 580     }
 581     return k;
 582   }
 583 
 584   // Check for prior unloaded klass.  The SystemDictionary's answers
 585   // can vary over time but the compiler needs consistency.
 586   ciSymbol* name = get_symbol(klass->name());
 587   ciKlass* unloaded_klass = check_get_unloaded_klass(accessor, name);
 588   if (unloaded_klass != NULL) {
 589     is_accessible = false;
 590     return unloaded_klass;
 591   }
 592 
 593   // It is known to be accessible, since it was found in the constant pool.
 594   is_accessible = true;
 595   return get_klass(klass);
 596 }
 597 
 598 // ------------------------------------------------------------------
 599 // ciEnv::get_klass_by_index
 600 //
 601 // Get a klass from the constant pool.
 602 ciKlass* ciEnv::get_klass_by_index(const constantPoolHandle& cpool,
 603                                    int index,
 604                                    bool& is_accessible,
 605                                    ciInstanceKlass* accessor) {
 606   GUARDED_VM_ENTRY(return get_klass_by_index_impl(cpool, index, is_accessible, accessor);)
 607 }
 608 
 609 // ------------------------------------------------------------------
 610 // ciEnv::get_never_null_impl
 611 //
 612 // Implementation of get_never_null.
 613 bool ciEnv::get_never_null_impl(const constantPoolHandle& cpool, int index) {
 614   Symbol* klass_name = cpool->klass_name_at(index);
 615   return klass_name->is_Q_signature();
 616 }
 617 
 618 // ------------------------------------------------------------------
 619 // ciEnv::get_never_null
 620 //
 621 // Get information about nullability from the constant pool.
 622 bool ciEnv::get_never_null(const constantPoolHandle& cpool, int index) {
 623   GUARDED_VM_ENTRY(return get_never_null_impl(cpool, index);)
 624 }
 625 
 626 // ------------------------------------------------------------------
 627 // ciEnv::get_constant_by_index_impl
 628 //
 629 // Implementation of get_constant_by_index().
 630 ciConstant ciEnv::get_constant_by_index_impl(const constantPoolHandle& cpool,
 631                                              int pool_index, int cache_index,
 632                                              ciInstanceKlass* accessor) {
 633   bool ignore_will_link;
 634   EXCEPTION_CONTEXT;
 635   int index = pool_index;
 636   if (cache_index >= 0) {
 637     assert(index < 0, "only one kind of index at a time");
 638     index = cpool->object_to_cp_index(cache_index);
 639     oop obj = cpool->resolved_references()->obj_at(cache_index);
 640     if (obj != NULL) {
 641       if (oopDesc::equals(obj, Universe::the_null_sentinel())) {
 642         return ciConstant(T_OBJECT, get_object(NULL));
 643       }
 644       BasicType bt = T_OBJECT;
 645       if (cpool->tag_at(index).is_dynamic_constant())
 646         bt = FieldType::basic_type(cpool->uncached_signature_ref_at(index));
 647       if (is_reference_type(bt)) {
 648       } else {
 649         // we have to unbox the primitive value
 650         if (!is_java_primitive(bt))  return ciConstant();
 651         jvalue value;
 652         BasicType bt2 = java_lang_boxing_object::get_value(obj, &value);
 653         assert(bt2 == bt, "");
 654         switch (bt2) {
 655         case T_DOUBLE:  return ciConstant(value.d);
 656         case T_FLOAT:   return ciConstant(value.f);
 657         case T_LONG:    return ciConstant(value.j);
 658         case T_INT:     return ciConstant(bt2, value.i);
 659         case T_SHORT:   return ciConstant(bt2, value.s);
 660         case T_BYTE:    return ciConstant(bt2, value.b);
 661         case T_CHAR:    return ciConstant(bt2, value.c);
 662         case T_BOOLEAN: return ciConstant(bt2, value.z);
 663         default:  return ciConstant();
 664         }
 665       }
 666       ciObject* ciobj = get_object(obj);
 667       if (ciobj->is_array()) {
 668         return ciConstant(T_ARRAY, ciobj);
 669       } else {
 670         assert(ciobj->is_instance(), "should be an instance");
 671         return ciConstant(T_OBJECT, ciobj);
 672       }
 673     }
 674   }
 675   constantTag tag = cpool->tag_at(index);
 676   if (tag.is_int()) {
 677     return ciConstant(T_INT, (jint)cpool->int_at(index));
 678   } else if (tag.is_long()) {
 679     return ciConstant((jlong)cpool->long_at(index));
 680   } else if (tag.is_float()) {
 681     return ciConstant((jfloat)cpool->float_at(index));
 682   } else if (tag.is_double()) {
 683     return ciConstant((jdouble)cpool->double_at(index));
 684   } else if (tag.is_string()) {
 685     oop string = NULL;
 686     assert(cache_index >= 0, "should have a cache index");
 687     if (cpool->is_pseudo_string_at(index)) {
 688       string = cpool->pseudo_string_at(index, cache_index);
 689     } else {
 690       string = cpool->string_at(index, cache_index, THREAD);
 691       if (HAS_PENDING_EXCEPTION) {
 692         CLEAR_PENDING_EXCEPTION;
 693         record_out_of_memory_failure();
 694         return ciConstant();
 695       }
 696     }
 697     ciObject* constant = get_object(string);
 698     if (constant->is_array()) {
 699       return ciConstant(T_ARRAY, constant);
 700     } else {
 701       assert (constant->is_instance(), "must be an instance, or not? ");
 702       return ciConstant(T_OBJECT, constant);
 703     }
 704   } else if (tag.is_klass() || tag.is_unresolved_klass()) {
 705     // 4881222: allow ldc to take a class type
 706     ciKlass* klass = get_klass_by_index_impl(cpool, index, ignore_will_link, accessor);
 707     if (HAS_PENDING_EXCEPTION) {
 708       CLEAR_PENDING_EXCEPTION;
 709       record_out_of_memory_failure();
 710       return ciConstant();
 711     }
 712     assert (klass->is_instance_klass() || klass->is_array_klass(),
 713             "must be an instance or array klass ");
 714     return ciConstant(T_OBJECT, klass->java_mirror());
 715   } else if (tag.is_method_type()) {
 716     // must execute Java code to link this CP entry into cache[i].f1
 717     ciSymbol* signature = get_symbol(cpool->method_type_signature_at(index));
 718     ciObject* ciobj = get_unloaded_method_type_constant(signature);
 719     return ciConstant(T_OBJECT, ciobj);
 720   } else if (tag.is_method_handle()) {
 721     // must execute Java code to link this CP entry into cache[i].f1
 722     int ref_kind        = cpool->method_handle_ref_kind_at(index);
 723     int callee_index    = cpool->method_handle_klass_index_at(index);
 724     ciKlass* callee     = get_klass_by_index_impl(cpool, callee_index, ignore_will_link, accessor);
 725     ciSymbol* name      = get_symbol(cpool->method_handle_name_ref_at(index));
 726     ciSymbol* signature = get_symbol(cpool->method_handle_signature_ref_at(index));
 727     ciObject* ciobj     = get_unloaded_method_handle_constant(callee, name, signature, ref_kind);
 728     return ciConstant(T_OBJECT, ciobj);
 729   } else if (tag.is_dynamic_constant()) {
 730     return ciConstant();
 731   } else {
 732     ShouldNotReachHere();
 733     return ciConstant();
 734   }
 735 }
 736 
 737 // ------------------------------------------------------------------
 738 // ciEnv::get_constant_by_index
 739 //
 740 // Pull a constant out of the constant pool.  How appropriate.
 741 //
 742 // Implementation note: this query is currently in no way cached.
 743 ciConstant ciEnv::get_constant_by_index(const constantPoolHandle& cpool,
 744                                         int pool_index, int cache_index,
 745                                         ciInstanceKlass* accessor) {
 746   GUARDED_VM_ENTRY(return get_constant_by_index_impl(cpool, pool_index, cache_index, accessor);)
 747 }
 748 
 749 // ------------------------------------------------------------------
 750 // ciEnv::get_field_by_index_impl
 751 //
 752 // Implementation of get_field_by_index.
 753 //
 754 // Implementation note: the results of field lookups are cached
 755 // in the accessor klass.
 756 ciField* ciEnv::get_field_by_index_impl(ciInstanceKlass* accessor,
 757                                         int index) {
 758   ciConstantPoolCache* cache = accessor->field_cache();
 759   if (cache == NULL) {
 760     ciField* field = new (arena()) ciField(accessor, index);
 761     return field;
 762   } else {
 763     ciField* field = (ciField*)cache->get(index);
 764     if (field == NULL) {
 765       field = new (arena()) ciField(accessor, index);
 766       cache->insert(index, field);
 767     }
 768     return field;
 769   }
 770 }
 771 
 772 // ------------------------------------------------------------------
 773 // ciEnv::get_field_by_index
 774 //
 775 // Get a field by index from a klass's constant pool.
 776 ciField* ciEnv::get_field_by_index(ciInstanceKlass* accessor,
 777                                    int index) {
 778   GUARDED_VM_ENTRY(return get_field_by_index_impl(accessor, index);)
 779 }
 780 
 781 // ------------------------------------------------------------------
 782 // ciEnv::lookup_method
 783 //
 784 // Perform an appropriate method lookup based on accessor, holder,
 785 // name, signature, and bytecode.
 786 Method* ciEnv::lookup_method(ciInstanceKlass* accessor,
 787                              ciKlass*         holder,
 788                              Symbol*          name,
 789                              Symbol*          sig,
 790                              Bytecodes::Code  bc,
 791                              constantTag      tag) {
 792   // Accessibility checks are performed in ciEnv::get_method_by_index_impl.
 793   assert(check_klass_accessibility(accessor, holder->get_Klass()), "holder not accessible");
 794 
 795   InstanceKlass* accessor_klass = accessor->get_instanceKlass();
 796   Klass* holder_klass = holder->get_Klass();
 797   methodHandle dest_method;
 798   LinkInfo link_info(holder_klass, name, sig, accessor_klass, LinkInfo::needs_access_check, tag);
 799   switch (bc) {
 800   case Bytecodes::_invokestatic:
 801     dest_method =
 802       LinkResolver::resolve_static_call_or_null(link_info);
 803     break;
 804   case Bytecodes::_invokespecial:
 805     dest_method =
 806       LinkResolver::resolve_special_call_or_null(link_info);
 807     break;
 808   case Bytecodes::_invokeinterface:
 809     dest_method =
 810       LinkResolver::linktime_resolve_interface_method_or_null(link_info);
 811     break;
 812   case Bytecodes::_invokevirtual:
 813     dest_method =
 814       LinkResolver::linktime_resolve_virtual_method_or_null(link_info);
 815     break;
 816   default: ShouldNotReachHere();
 817   }
 818 
 819   return dest_method();
 820 }
 821 
 822 
 823 // ------------------------------------------------------------------
 824 // ciEnv::get_method_by_index_impl
 825 ciMethod* ciEnv::get_method_by_index_impl(const constantPoolHandle& cpool,
 826                                           int index, Bytecodes::Code bc,
 827                                           ciInstanceKlass* accessor) {
 828   if (bc == Bytecodes::_invokedynamic) {
 829     ConstantPoolCacheEntry* cpce = cpool->invokedynamic_cp_cache_entry_at(index);
 830     bool is_resolved = !cpce->is_f1_null();
 831     // FIXME: code generation could allow for null (unlinked) call site
 832     // The call site could be made patchable as follows:
 833     // Load the appendix argument from the constant pool.
 834     // Test the appendix argument and jump to a known deopt routine if it is null.
 835     // Jump through a patchable call site, which is initially a deopt routine.
 836     // Patch the call site to the nmethod entry point of the static compiled lambda form.
 837     // As with other two-component call sites, both values must be independently verified.
 838 
 839     if (is_resolved) {
 840       // Get the invoker Method* from the constant pool.
 841       // (The appendix argument, if any, will be noted in the method's signature.)
 842       Method* adapter = cpce->f1_as_method();
 843       return get_method(adapter);
 844     }
 845 
 846     // Fake a method that is equivalent to a declared method.
 847     ciInstanceKlass* holder    = get_instance_klass(SystemDictionary::MethodHandle_klass());
 848     ciSymbol*        name      = ciSymbol::invokeBasic_name();
 849     ciSymbol*        signature = get_symbol(cpool->signature_ref_at(index));
 850     return get_unloaded_method(holder, name, signature, accessor);
 851   } else {
 852     const int holder_index = cpool->klass_ref_index_at(index);
 853     bool holder_is_accessible;
 854     ciKlass* holder = get_klass_by_index_impl(cpool, holder_index, holder_is_accessible, accessor);
 855 
 856     // Get the method's name and signature.
 857     Symbol* name_sym = cpool->name_ref_at(index);
 858     Symbol* sig_sym  = cpool->signature_ref_at(index);
 859 
 860     if (cpool->has_preresolution()
 861         || ((holder == ciEnv::MethodHandle_klass() || holder == ciEnv::VarHandle_klass()) &&
 862             MethodHandles::is_signature_polymorphic_name(holder->get_Klass(), name_sym))) {
 863       // Short-circuit lookups for JSR 292-related call sites.
 864       // That is, do not rely only on name-based lookups, because they may fail
 865       // if the names are not resolvable in the boot class loader (7056328).
 866       switch (bc) {
 867       case Bytecodes::_invokevirtual:
 868       case Bytecodes::_invokeinterface:
 869       case Bytecodes::_invokespecial:
 870       case Bytecodes::_invokestatic:
 871         {
 872           Method* m = ConstantPool::method_at_if_loaded(cpool, index);
 873           if (m != NULL) {
 874             return get_method(m);
 875           }
 876         }
 877         break;
 878       default:
 879         break;
 880       }
 881     }
 882 
 883     if (holder_is_accessible) {  // Our declared holder is loaded.
 884       constantTag tag = cpool->tag_ref_at(index);
 885       assert(accessor->get_instanceKlass() == cpool->pool_holder(), "not the pool holder?");
 886       Method* m = lookup_method(accessor, holder, name_sym, sig_sym, bc, tag);
 887       if (m != NULL &&
 888           (bc == Bytecodes::_invokestatic
 889            ?  m->method_holder()->is_not_initialized()
 890            : !m->method_holder()->is_loaded())) {
 891         m = NULL;
 892       }
 893 #ifdef ASSERT
 894       if (m != NULL && ReplayCompiles && !ciReplay::is_loaded(m)) {
 895         m = NULL;
 896       }
 897 #endif
 898       if (m != NULL) {
 899         // We found the method.
 900         return get_method(m);
 901       }
 902     }
 903 
 904     // Either the declared holder was not loaded, or the method could
 905     // not be found.  Create a dummy ciMethod to represent the failed
 906     // lookup.
 907     ciSymbol* name      = get_symbol(name_sym);
 908     ciSymbol* signature = get_symbol(sig_sym);
 909     return get_unloaded_method(holder, name, signature, accessor);
 910   }
 911 }
 912 
 913 
 914 // ------------------------------------------------------------------
 915 // ciEnv::get_instance_klass_for_declared_method_holder
 916 ciInstanceKlass* ciEnv::get_instance_klass_for_declared_method_holder(ciKlass* method_holder) {
 917   // For the case of <array>.clone(), the method holder can be a ciArrayKlass
 918   // instead of a ciInstanceKlass.  For that case simply pretend that the
 919   // declared holder is Object.clone since that's where the call will bottom out.
 920   // A more correct fix would trickle out through many interfaces in CI,
 921   // requiring ciInstanceKlass* to become ciKlass* and many more places would
 922   // require checks to make sure the expected type was found.  Given that this
 923   // only occurs for clone() the more extensive fix seems like overkill so
 924   // instead we simply smear the array type into Object.
 925   guarantee(method_holder != NULL, "no method holder");
 926   if (method_holder->is_instance_klass()) {
 927     return method_holder->as_instance_klass();
 928   } else if (method_holder->is_array_klass()) {
 929     return current()->Object_klass();
 930   } else {
 931     ShouldNotReachHere();
 932   }
 933   return NULL;
 934 }
 935 
 936 
 937 // ------------------------------------------------------------------
 938 // ciEnv::get_method_by_index
 939 ciMethod* ciEnv::get_method_by_index(const constantPoolHandle& cpool,
 940                                      int index, Bytecodes::Code bc,
 941                                      ciInstanceKlass* accessor) {
 942   GUARDED_VM_ENTRY(return get_method_by_index_impl(cpool, index, bc, accessor);)
 943 }
 944 
 945 
 946 // ------------------------------------------------------------------
 947 // ciEnv::name_buffer
 948 char *ciEnv::name_buffer(int req_len) {
 949   if (_name_buffer_len < req_len) {
 950     if (_name_buffer == NULL) {
 951       _name_buffer = (char*)arena()->Amalloc(sizeof(char)*req_len);
 952       _name_buffer_len = req_len;
 953     } else {
 954       _name_buffer =
 955         (char*)arena()->Arealloc(_name_buffer, _name_buffer_len, req_len);
 956       _name_buffer_len = req_len;
 957     }
 958   }
 959   return _name_buffer;
 960 }
 961 
 962 // ------------------------------------------------------------------
 963 // ciEnv::is_in_vm
 964 bool ciEnv::is_in_vm() {
 965   return JavaThread::current()->thread_state() == _thread_in_vm;
 966 }
 967 
 968 bool ciEnv::system_dictionary_modification_counter_changed() {
 969   return _system_dictionary_modification_counter != SystemDictionary::number_of_modifications();
 970 }
 971 
 972 // ------------------------------------------------------------------
 973 // ciEnv::validate_compile_task_dependencies
 974 //
 975 // Check for changes during compilation (e.g. class loads, evolution,
 976 // breakpoints, call site invalidation).
 977 void ciEnv::validate_compile_task_dependencies(ciMethod* target) {
 978   if (failing())  return;  // no need for further checks
 979 
 980   bool counter_changed = system_dictionary_modification_counter_changed();
 981   Dependencies::DepType result = dependencies()->validate_dependencies(_task, counter_changed);
 982   if (result != Dependencies::end_marker) {
 983     if (result == Dependencies::call_site_target_value) {
 984       _inc_decompile_count_on_failure = false;
 985       record_failure("call site target change");
 986     } else if (Dependencies::is_klass_type(result)) {
 987       record_failure("concurrent class loading");
 988     } else {
 989       record_failure("invalid non-klass dependency");
 990     }
 991   }
 992 }
 993 
 994 // ------------------------------------------------------------------
 995 // ciEnv::register_method
 996 void ciEnv::register_method(ciMethod* target,
 997                             int entry_bci,
 998                             CodeOffsets* offsets,
 999                             int orig_pc_offset,
1000                             CodeBuffer* code_buffer,
1001                             int frame_words,
1002                             OopMapSet* oop_map_set,
1003                             ExceptionHandlerTable* handler_table,
1004                             ImplicitExceptionTable* inc_table,
1005                             AbstractCompiler* compiler,
1006                             bool has_unsafe_access,
1007                             bool has_wide_vectors,
1008                             RTMState  rtm_state) {
1009   VM_ENTRY_MARK;
1010   nmethod* nm = NULL;
1011   {
1012     // To prevent compile queue updates.
1013     MutexLocker locker(MethodCompileQueue_lock, THREAD);
1014 
1015     // Prevent SystemDictionary::add_to_hierarchy from running
1016     // and invalidating our dependencies until we install this method.
1017     // No safepoints are allowed. Otherwise, class redefinition can occur in between.
1018     MutexLocker ml(Compile_lock);
1019     NoSafepointVerifier nsv;
1020 
1021     // Change in Jvmti state may invalidate compilation.
1022     if (!failing() && jvmti_state_changed()) {
1023       record_failure("Jvmti state change invalidated dependencies");
1024     }
1025 
1026     // Change in DTrace flags may invalidate compilation.
1027     if (!failing() &&
1028         ( (!dtrace_extended_probes() && ExtendedDTraceProbes) ||
1029           (!dtrace_method_probes() && DTraceMethodProbes) ||
1030           (!dtrace_alloc_probes() && DTraceAllocProbes) )) {
1031       record_failure("DTrace flags change invalidated dependencies");
1032     }
1033 
1034     if (!failing()) {
1035       if (log() != NULL) {
1036         // Log the dependencies which this compilation declares.
1037         dependencies()->log_all_dependencies();
1038       }
1039 
1040       // Encode the dependencies now, so we can check them right away.
1041       dependencies()->encode_content_bytes();
1042 
1043       // Check for {class loads, evolution, breakpoints, ...} during compilation
1044       validate_compile_task_dependencies(target);
1045     }
1046 
1047     methodHandle method(THREAD, target->get_Method());
1048 
1049 #if INCLUDE_RTM_OPT
1050     if (!failing() && (rtm_state != NoRTM) &&
1051         (method()->method_data() != NULL) &&
1052         (method()->method_data()->rtm_state() != rtm_state)) {
1053       // Preemptive decompile if rtm state was changed.
1054       record_failure("RTM state change invalidated rtm code");
1055     }
1056 #endif
1057 
1058     if (failing()) {
1059       // While not a true deoptimization, it is a preemptive decompile.
1060       MethodData* mdo = method()->method_data();
1061       if (mdo != NULL && _inc_decompile_count_on_failure) {
1062         mdo->inc_decompile_count();
1063       }
1064 
1065       // All buffers in the CodeBuffer are allocated in the CodeCache.
1066       // If the code buffer is created on each compile attempt
1067       // as in C2, then it must be freed.
1068       code_buffer->free_blob();
1069       return;
1070     }
1071 
1072     assert(offsets->value(CodeOffsets::Deopt) != -1, "must have deopt entry");
1073     assert(offsets->value(CodeOffsets::Exceptions) != -1, "must have exception entry");
1074 
1075     nm =  nmethod::new_nmethod(method,
1076                                compile_id(),
1077                                entry_bci,
1078                                offsets,
1079                                orig_pc_offset,
1080                                debug_info(), dependencies(), code_buffer,
1081                                frame_words, oop_map_set,
1082                                handler_table, inc_table,
1083                                compiler, task()->comp_level());
1084 
1085     // Free codeBlobs
1086     code_buffer->free_blob();
1087 
1088     if (nm != NULL) {
1089       nm->set_has_unsafe_access(has_unsafe_access);
1090       nm->set_has_wide_vectors(has_wide_vectors);
1091 #if INCLUDE_RTM_OPT
1092       nm->set_rtm_state(rtm_state);
1093 #endif
1094 
1095       // Record successful registration.
1096       // (Put nm into the task handle *before* publishing to the Java heap.)
1097       if (task() != NULL) {
1098         task()->set_code(nm);
1099       }
1100 
1101       if (entry_bci == InvocationEntryBci) {
1102         if (TieredCompilation) {
1103           // If there is an old version we're done with it
1104           CompiledMethod* old = method->code();
1105           if (TraceMethodReplacement && old != NULL) {
1106             ResourceMark rm;
1107             char *method_name = method->name_and_sig_as_C_string();
1108             tty->print_cr("Replacing method %s", method_name);
1109           }
1110           if (old != NULL) {
1111             old->make_not_used();
1112           }
1113         }
1114         if (TraceNMethodInstalls) {
1115           ResourceMark rm;
1116           char *method_name = method->name_and_sig_as_C_string();
1117           ttyLocker ttyl;
1118           tty->print_cr("Installing method (%d) %s ",
1119                         task()->comp_level(),
1120                         method_name);
1121         }
1122         // Allow the code to be executed
1123         method->set_code(method, nm);
1124       } else {
1125         if (TraceNMethodInstalls) {
1126           ResourceMark rm;
1127           char *method_name = method->name_and_sig_as_C_string();
1128           ttyLocker ttyl;
1129           tty->print_cr("Installing osr method (%d) %s @ %d",
1130                         task()->comp_level(),
1131                         method_name,
1132                         entry_bci);
1133         }
1134         method->method_holder()->add_osr_nmethod(nm);
1135       }
1136       nm->make_in_use();
1137     }
1138   }  // safepoints are allowed again
1139 
1140   if (nm != NULL) {
1141     // JVMTI -- compiled method notification (must be done outside lock)
1142     nm->post_compiled_method_load_event();
1143   } else {
1144     // The CodeCache is full.
1145     record_failure("code cache is full");
1146   }
1147 }
1148 
1149 
1150 // ------------------------------------------------------------------
1151 // ciEnv::find_system_klass
1152 ciKlass* ciEnv::find_system_klass(ciSymbol* klass_name) {
1153   VM_ENTRY_MARK;
1154   return get_klass_by_name_impl(NULL, constantPoolHandle(), klass_name, false);
1155 }
1156 
1157 // ------------------------------------------------------------------
1158 // ciEnv::comp_level
1159 int ciEnv::comp_level() {
1160   if (task() == NULL)  return CompLevel_highest_tier;
1161   return task()->comp_level();
1162 }
1163 
1164 // ------------------------------------------------------------------
1165 // ciEnv::compile_id
1166 uint ciEnv::compile_id() {
1167   if (task() == NULL)  return 0;
1168   return task()->compile_id();
1169 }
1170 
1171 // ------------------------------------------------------------------
1172 // ciEnv::notice_inlined_method()
1173 void ciEnv::notice_inlined_method(ciMethod* method) {
1174   _num_inlined_bytecodes += method->code_size_for_inlining();
1175 }
1176 
1177 // ------------------------------------------------------------------
1178 // ciEnv::num_inlined_bytecodes()
1179 int ciEnv::num_inlined_bytecodes() const {
1180   return _num_inlined_bytecodes;
1181 }
1182 
1183 // ------------------------------------------------------------------
1184 // ciEnv::record_failure()
1185 void ciEnv::record_failure(const char* reason) {
1186   if (_failure_reason == NULL) {
1187     // Record the first failure reason.
1188     _failure_reason = reason;
1189   }
1190 }
1191 
1192 void ciEnv::report_failure(const char* reason) {
1193   EventCompilationFailure event;
1194   if (event.should_commit()) {
1195     event.set_compileId(compile_id());
1196     event.set_failureMessage(reason);
1197     event.commit();
1198   }
1199 }
1200 
1201 // ------------------------------------------------------------------
1202 // ciEnv::record_method_not_compilable()
1203 void ciEnv::record_method_not_compilable(const char* reason, bool all_tiers) {
1204   int new_compilable =
1205     all_tiers ? MethodCompilable_never : MethodCompilable_not_at_tier ;
1206 
1207   // Only note transitions to a worse state
1208   if (new_compilable > _compilable) {
1209     if (log() != NULL) {
1210       if (all_tiers) {
1211         log()->elem("method_not_compilable");
1212       } else {
1213         log()->elem("method_not_compilable_at_tier level='%d'",
1214                     current()->task()->comp_level());
1215       }
1216     }
1217     _compilable = new_compilable;
1218 
1219     // Reset failure reason; this one is more important.
1220     _failure_reason = NULL;
1221     record_failure(reason);
1222   }
1223 }
1224 
1225 // ------------------------------------------------------------------
1226 // ciEnv::record_out_of_memory_failure()
1227 void ciEnv::record_out_of_memory_failure() {
1228   // If memory is low, we stop compiling methods.
1229   record_method_not_compilable("out of memory");
1230 }
1231 
1232 ciInstance* ciEnv::unloaded_ciinstance() {
1233   GUARDED_VM_ENTRY(return _factory->get_unloaded_object_constant();)
1234 }
1235 
1236 // ------------------------------------------------------------------
1237 // ciEnv::dump_replay_data*
1238 
1239 // Don't change thread state and acquire any locks.
1240 // Safe to call from VM error reporter.
1241 
1242 void ciEnv::dump_compile_data(outputStream* out) {
1243   CompileTask* task = this->task();
1244   if (task) {
1245     Method* method = task->method();
1246     int entry_bci = task->osr_bci();
1247     int comp_level = task->comp_level();
1248     out->print("compile %s %s %s %d %d",
1249                method->klass_name()->as_quoted_ascii(),
1250                method->name()->as_quoted_ascii(),
1251                method->signature()->as_quoted_ascii(),
1252                entry_bci, comp_level);
1253     if (compiler_data() != NULL) {
1254       if (is_c2_compile(comp_level)) {
1255 #ifdef COMPILER2
1256         // Dump C2 inlining data.
1257         ((Compile*)compiler_data())->dump_inline_data(out);
1258 #endif
1259       } else if (is_c1_compile(comp_level)) {
1260 #ifdef COMPILER1
1261         // Dump C1 inlining data.
1262         ((Compilation*)compiler_data())->dump_inline_data(out);
1263 #endif
1264       }
1265     }
1266     out->cr();
1267   }
1268 }
1269 
1270 void ciEnv::dump_replay_data_unsafe(outputStream* out) {
1271   ResourceMark rm;
1272 #if INCLUDE_JVMTI
1273   out->print_cr("JvmtiExport can_access_local_variables %d",     _jvmti_can_access_local_variables);
1274   out->print_cr("JvmtiExport can_hotswap_or_post_breakpoint %d", _jvmti_can_hotswap_or_post_breakpoint);
1275   out->print_cr("JvmtiExport can_post_on_exceptions %d",         _jvmti_can_post_on_exceptions);
1276 #endif // INCLUDE_JVMTI
1277 
1278   GrowableArray<ciMetadata*>* objects = _factory->get_ci_metadata();
1279   out->print_cr("# %d ciObject found", objects->length());
1280   for (int i = 0; i < objects->length(); i++) {
1281     objects->at(i)->dump_replay_data(out);
1282   }
1283   dump_compile_data(out);
1284   out->flush();
1285 }
1286 
1287 void ciEnv::dump_replay_data(outputStream* out) {
1288   GUARDED_VM_ENTRY(
1289     MutexLocker ml(Compile_lock);
1290     dump_replay_data_unsafe(out);
1291   )
1292 }
1293 
1294 void ciEnv::dump_replay_data(int compile_id) {
1295   static char buffer[O_BUFLEN];
1296   int ret = jio_snprintf(buffer, O_BUFLEN, "replay_pid%p_compid%d.log", os::current_process_id(), compile_id);
1297   if (ret > 0) {
1298     int fd = open(buffer, O_RDWR | O_CREAT | O_TRUNC, 0666);
1299     if (fd != -1) {
1300       FILE* replay_data_file = os::open(fd, "w");
1301       if (replay_data_file != NULL) {
1302         fileStream replay_data_stream(replay_data_file, /*need_close=*/true);
1303         dump_replay_data(&replay_data_stream);
1304         tty->print_cr("# Compiler replay data is saved as: %s", buffer);
1305       } else {
1306         tty->print_cr("# Can't open file to dump replay data.");
1307       }
1308     }
1309   }
1310 }
1311 
1312 void ciEnv::dump_inline_data(int compile_id) {
1313   static char buffer[O_BUFLEN];
1314   int ret = jio_snprintf(buffer, O_BUFLEN, "inline_pid%p_compid%d.log", os::current_process_id(), compile_id);
1315   if (ret > 0) {
1316     int fd = open(buffer, O_RDWR | O_CREAT | O_TRUNC, 0666);
1317     if (fd != -1) {
1318       FILE* inline_data_file = os::open(fd, "w");
1319       if (inline_data_file != NULL) {
1320         fileStream replay_data_stream(inline_data_file, /*need_close=*/true);
1321         GUARDED_VM_ENTRY(
1322           MutexLocker ml(Compile_lock);
1323           dump_compile_data(&replay_data_stream);
1324         )
1325         replay_data_stream.flush();
1326         tty->print("# Compiler inline data is saved as: ");
1327         tty->print_cr("%s", buffer);
1328       } else {
1329         tty->print_cr("# Can't open file to dump inline data.");
1330       }
1331     }
1332   }
1333 }