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