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