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