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