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