1 /*
   2  * Copyright 1999-2007 Sun Microsystems, Inc.  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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
  20  * CA 95054 USA or visit www.sun.com if you need additional information or
  21  * have any questions.
  22  *
  23  */
  24 
  25 #include "incls/_precompiled.incl"
  26 #include "incls/_ciEnv.cpp.incl"
  27 
  28 // ciEnv
  29 //
  30 // This class is the top level broker for requests from the compiler
  31 // to the VM.
  32 
  33 ciObject*              ciEnv::_null_object_instance;
  34 ciMethodKlass*         ciEnv::_method_klass_instance;
  35 ciSymbolKlass*         ciEnv::_symbol_klass_instance;
  36 ciKlassKlass*          ciEnv::_klass_klass_instance;
  37 ciInstanceKlassKlass*  ciEnv::_instance_klass_klass_instance;
  38 ciTypeArrayKlassKlass* ciEnv::_type_array_klass_klass_instance;
  39 ciObjArrayKlassKlass*  ciEnv::_obj_array_klass_klass_instance;
  40 
  41 ciInstanceKlass* ciEnv::_ArrayStoreException;
  42 ciInstanceKlass* ciEnv::_Class;
  43 ciInstanceKlass* ciEnv::_ClassCastException;
  44 ciInstanceKlass* ciEnv::_Object;
  45 ciInstanceKlass* ciEnv::_Throwable;
  46 ciInstanceKlass* ciEnv::_Thread;
  47 ciInstanceKlass* ciEnv::_OutOfMemoryError;
  48 ciInstanceKlass* ciEnv::_String;
  49 
  50 ciSymbol*        ciEnv::_unloaded_cisymbol = NULL;
  51 ciInstanceKlass* ciEnv::_unloaded_ciinstance_klass = NULL;
  52 ciObjArrayKlass* ciEnv::_unloaded_ciobjarrayklass = NULL;
  53 
  54 jobject ciEnv::_ArrayIndexOutOfBoundsException_handle = NULL;
  55 jobject ciEnv::_ArrayStoreException_handle = NULL;
  56 jobject ciEnv::_ClassCastException_handle = NULL;
  57 
  58 #ifndef PRODUCT
  59 static bool firstEnv = true;
  60 #endif /* PRODUCT */
  61 
  62 // ------------------------------------------------------------------
  63 // ciEnv::ciEnv
  64 ciEnv::ciEnv(CompileTask* task, int system_dictionary_modification_counter) {
  65   VM_ENTRY_MARK;
  66 
  67   // Set up ciEnv::current immediately, for the sake of ciObjectFactory, etc.
  68   thread->set_env(this);
  69   assert(ciEnv::current() == this, "sanity");
  70 
  71   _oop_recorder = NULL;
  72   _debug_info = NULL;
  73   _dependencies = NULL;
  74   _failure_reason = NULL;
  75   _compilable = MethodCompilable;
  76   _break_at_compile = false;
  77   _compiler_data = NULL;
  78 #ifndef PRODUCT
  79   assert(!firstEnv, "not initialized properly");
  80 #endif /* !PRODUCT */
  81 
  82   _system_dictionary_modification_counter = system_dictionary_modification_counter;
  83   _num_inlined_bytecodes = 0;
  84   assert(task == NULL || thread->task() == task, "sanity");
  85   _task = task;
  86   _log = NULL;
  87 
  88   // Temporary buffer for creating symbols and such.
  89   _name_buffer = NULL;
  90   _name_buffer_len = 0;
  91 
  92   _arena   = &_ciEnv_arena;
  93   _factory = new (_arena) ciObjectFactory(_arena, 128);
  94 
  95   // Preload commonly referenced system ciObjects.
  96 
  97   // During VM initialization, these instances have not yet been created.
  98   // Assertions ensure that these instances are not accessed before
  99   // their initialization.
 100 
 101   assert(Universe::is_fully_initialized(), "should be complete");
 102 
 103   oop o = Universe::null_ptr_exception_instance();
 104   assert(o != NULL, "should have been initialized");
 105   _NullPointerException_instance = get_object(o)->as_instance();
 106   o = Universe::arithmetic_exception_instance();
 107   assert(o != NULL, "should have been initialized");
 108   _ArithmeticException_instance = get_object(o)->as_instance();
 109 
 110   _ArrayIndexOutOfBoundsException_instance = NULL;
 111   _ArrayStoreException_instance = NULL;
 112   _ClassCastException_instance = NULL;
 113 }
 114 
 115 ciEnv::ciEnv(Arena* arena) {
 116   ASSERT_IN_VM;
 117 
 118   // Set up ciEnv::current immediately, for the sake of ciObjectFactory, etc.
 119   CompilerThread* current_thread = CompilerThread::current();
 120   assert(current_thread->env() == NULL, "must be");
 121   current_thread->set_env(this);
 122   assert(ciEnv::current() == this, "sanity");
 123 
 124   _oop_recorder = NULL;
 125   _debug_info = NULL;
 126   _dependencies = NULL;
 127   _failure_reason = NULL;
 128   _compilable = MethodCompilable_never;
 129   _break_at_compile = false;
 130   _compiler_data = NULL;
 131 #ifndef PRODUCT
 132   assert(firstEnv, "must be first");
 133   firstEnv = false;
 134 #endif /* !PRODUCT */
 135 
 136   _system_dictionary_modification_counter = 0;
 137   _num_inlined_bytecodes = 0;
 138   _task = NULL;
 139   _log = NULL;
 140 
 141   // Temporary buffer for creating symbols and such.
 142   _name_buffer = NULL;
 143   _name_buffer_len = 0;
 144 
 145   _arena   = arena;
 146   _factory = new (_arena) ciObjectFactory(_arena, 128);
 147 
 148   // Preload commonly referenced system ciObjects.
 149 
 150   // During VM initialization, these instances have not yet been created.
 151   // Assertions ensure that these instances are not accessed before
 152   // their initialization.
 153 
 154   assert(Universe::is_fully_initialized(), "must be");
 155 
 156   oop o = Universe::null_ptr_exception_instance();
 157   assert(o != NULL, "should have been initialized");
 158   _NullPointerException_instance = get_object(o)->as_instance();
 159   o = Universe::arithmetic_exception_instance();
 160   assert(o != NULL, "should have been initialized");
 161   _ArithmeticException_instance = get_object(o)->as_instance();
 162 
 163   _ArrayIndexOutOfBoundsException_instance = NULL;
 164   _ArrayStoreException_instance = NULL;
 165   _ClassCastException_instance = NULL;
 166 }
 167 
 168 ciEnv::~ciEnv() {
 169   CompilerThread* current_thread = CompilerThread::current();
 170   current_thread->set_env(NULL);
 171 }
 172 
 173 // ------------------------------------------------------------------
 174 // helper for lazy exception creation
 175 ciInstance* ciEnv::get_or_create_exception(jobject& handle, symbolHandle name) {
 176   VM_ENTRY_MARK;
 177   if (handle == NULL) {
 178     // Cf. universe.cpp, creation of Universe::_null_ptr_exception_instance.
 179     klassOop k = SystemDictionary::find(name, Handle(), Handle(), THREAD);
 180     jobject objh = NULL;
 181     if (!HAS_PENDING_EXCEPTION && k != NULL) {
 182       oop obj = instanceKlass::cast(k)->allocate_permanent_instance(THREAD);
 183       if (!HAS_PENDING_EXCEPTION)
 184         objh = JNIHandles::make_global(obj);
 185     }
 186     if (HAS_PENDING_EXCEPTION) {
 187       CLEAR_PENDING_EXCEPTION;
 188     } else {
 189       handle = objh;
 190     }
 191   }
 192   oop obj = JNIHandles::resolve(handle);
 193   return obj == NULL? NULL: get_object(obj)->as_instance();
 194 }
 195 
 196 // ------------------------------------------------------------------
 197 // ciEnv::ArrayIndexOutOfBoundsException_instance, etc.
 198 ciInstance* ciEnv::ArrayIndexOutOfBoundsException_instance() {
 199   if (_ArrayIndexOutOfBoundsException_instance == NULL) {
 200     _ArrayIndexOutOfBoundsException_instance
 201           = get_or_create_exception(_ArrayIndexOutOfBoundsException_handle,
 202           vmSymbolHandles::java_lang_ArrayIndexOutOfBoundsException());
 203   }
 204   return _ArrayIndexOutOfBoundsException_instance;
 205 }
 206 ciInstance* ciEnv::ArrayStoreException_instance() {
 207   if (_ArrayStoreException_instance == NULL) {
 208     _ArrayStoreException_instance
 209           = get_or_create_exception(_ArrayStoreException_handle,
 210           vmSymbolHandles::java_lang_ArrayStoreException());
 211   }
 212   return _ArrayStoreException_instance;
 213 }
 214 ciInstance* ciEnv::ClassCastException_instance() {
 215   if (_ClassCastException_instance == NULL) {
 216     _ClassCastException_instance
 217           = get_or_create_exception(_ClassCastException_handle,
 218           vmSymbolHandles::java_lang_ClassCastException());
 219   }
 220   return _ClassCastException_instance;
 221 }
 222 
 223 // ------------------------------------------------------------------
 224 // ciEnv::get_method_from_handle
 225 ciMethod* ciEnv::get_method_from_handle(jobject method) {
 226   VM_ENTRY_MARK;
 227   return get_object(JNIHandles::resolve(method))->as_method();
 228 }
 229 
 230 // ------------------------------------------------------------------
 231 // ciEnv::make_array
 232 ciArray* ciEnv::make_array(GrowableArray<ciObject*>* objects) {
 233   VM_ENTRY_MARK;
 234   int length = objects->length();
 235   objArrayOop a = oopFactory::new_system_objArray(length, THREAD);
 236   if (HAS_PENDING_EXCEPTION) {
 237     CLEAR_PENDING_EXCEPTION;
 238     record_out_of_memory_failure();
 239     return NULL;
 240   }
 241   for (int i = 0; i < length; i++) {
 242     a->obj_at_put(i, objects->at(i)->get_oop());
 243   }
 244   assert(a->is_perm(), "");
 245   return get_object(a)->as_array();
 246 }
 247 
 248 
 249 // ------------------------------------------------------------------
 250 // ciEnv::array_element_offset_in_bytes
 251 int ciEnv::array_element_offset_in_bytes(ciArray* a_h, ciObject* o_h) {
 252   VM_ENTRY_MARK;
 253   objArrayOop a = (objArrayOop)a_h->get_oop();
 254   assert(a->is_objArray(), "");
 255   int length = a->length();
 256   oop o = o_h->get_oop();
 257   for (int i = 0; i < length; i++) {
 258     if (a->obj_at(i) == o)  return i;
 259   }
 260   return -1;
 261 }
 262 
 263 
 264 // ------------------------------------------------------------------
 265 // ciEnv::check_klass_accessiblity
 266 //
 267 // Note: the logic of this method should mirror the logic of
 268 // constantPoolOopDesc::verify_constant_pool_resolve.
 269 bool ciEnv::check_klass_accessibility(ciKlass* accessing_klass,
 270                                       klassOop resolved_klass) {
 271   if (accessing_klass == NULL || !accessing_klass->is_loaded()) {
 272     return true;
 273   }
 274   if (accessing_klass->is_obj_array()) {
 275     accessing_klass = accessing_klass->as_obj_array_klass()->base_element_klass();
 276   }
 277   if (!accessing_klass->is_instance_klass()) {
 278     return true;
 279   }
 280 
 281   if (resolved_klass->klass_part()->oop_is_objArray()) {
 282     // Find the element klass, if this is an array.
 283     resolved_klass = objArrayKlass::cast(resolved_klass)->bottom_klass();
 284   }
 285   if (resolved_klass->klass_part()->oop_is_instance()) {
 286     return Reflection::verify_class_access(accessing_klass->get_klassOop(),
 287                                            resolved_klass,
 288                                            true);
 289   }
 290   return true;
 291 }
 292 
 293 // ------------------------------------------------------------------
 294 // ciEnv::get_klass_by_name_impl
 295 ciKlass* ciEnv::get_klass_by_name_impl(ciKlass* accessing_klass,
 296                                        ciSymbol* name,
 297                                        bool require_local) {
 298   ASSERT_IN_VM;
 299   EXCEPTION_CONTEXT;
 300 
 301   // Now we need to check the SystemDictionary
 302   symbolHandle sym(THREAD, name->get_symbolOop());
 303   if (sym->byte_at(0) == 'L' &&
 304     sym->byte_at(sym->utf8_length()-1) == ';') {
 305     // This is a name from a signature.  Strip off the trimmings.
 306     sym = oopFactory::new_symbol_handle(sym->as_utf8()+1,
 307                                         sym->utf8_length()-2,
 308                                         KILL_COMPILE_ON_FATAL_(_unloaded_ciinstance_klass));
 309     name = get_object(sym())->as_symbol();
 310   }
 311 
 312   // Check for prior unloaded klass.  The SystemDictionary's answers
 313   // can vary over time but the compiler needs consistency.
 314   ciKlass* unloaded_klass = check_get_unloaded_klass(accessing_klass, name);
 315   if (unloaded_klass != NULL) {
 316     if (require_local)  return NULL;
 317     return unloaded_klass;
 318   }
 319 
 320   Handle loader(THREAD, (oop)NULL);
 321   Handle domain(THREAD, (oop)NULL);
 322   if (accessing_klass != NULL) {
 323     loader = Handle(THREAD, accessing_klass->loader());
 324     domain = Handle(THREAD, accessing_klass->protection_domain());
 325   }
 326 
 327   // setup up the proper type to return on OOM
 328   ciKlass* fail_type;
 329   if (sym->byte_at(0) == '[') {
 330     fail_type = _unloaded_ciobjarrayklass;
 331   } else {
 332     fail_type = _unloaded_ciinstance_klass;
 333   }
 334   klassOop found_klass;
 335   if (!require_local) {
 336     found_klass =
 337       SystemDictionary::find_constrained_instance_or_array_klass(sym, loader,
 338                                                                  KILL_COMPILE_ON_FATAL_(fail_type));
 339   } else {
 340     found_klass =
 341       SystemDictionary::find_instance_or_array_klass(sym, loader, domain,
 342                                                      KILL_COMPILE_ON_FATAL_(fail_type));
 343   }
 344 
 345   if (found_klass != NULL) {
 346     // Found it.  Build a CI handle.
 347     return get_object(found_klass)->as_klass();
 348   }
 349 
 350   // If we fail to find an array klass, look again for its element type.
 351   // The element type may be available either locally or via constraints.
 352   // In either case, if we can find the element type in the system dictionary,
 353   // we must build an array type around it.  The CI requires array klasses
 354   // to be loaded if their element klasses are loaded, except when memory
 355   // is exhausted.
 356   if (sym->byte_at(0) == '[' &&
 357       (sym->byte_at(1) == '[' || sym->byte_at(1) == 'L')) {
 358     // We have an unloaded array.
 359     // Build it on the fly if the element class exists.
 360     symbolOop elem_sym = oopFactory::new_symbol(sym->as_utf8()+1,
 361                                                 sym->utf8_length()-1,
 362                                                 KILL_COMPILE_ON_FATAL_(fail_type));
 363     // Get element ciKlass recursively.
 364     ciKlass* elem_klass =
 365       get_klass_by_name_impl(accessing_klass,
 366                              get_object(elem_sym)->as_symbol(),
 367                              require_local);
 368     if (elem_klass != NULL && elem_klass->is_loaded()) {
 369       // Now make an array for it
 370       return ciObjArrayKlass::make_impl(elem_klass);
 371     }
 372   }
 373 
 374   if (require_local)  return NULL;
 375   // Not yet loaded into the VM, or not governed by loader constraints.
 376   // Make a CI representative for it.
 377   return get_unloaded_klass(accessing_klass, name);
 378 }
 379 
 380 // ------------------------------------------------------------------
 381 // ciEnv::get_klass_by_name
 382 ciKlass* ciEnv::get_klass_by_name(ciKlass* accessing_klass,
 383                                   ciSymbol* klass_name,
 384                                   bool require_local) {
 385   GUARDED_VM_ENTRY(return get_klass_by_name_impl(accessing_klass,
 386                                                  klass_name,
 387                                                  require_local);)
 388 }
 389 
 390 // ------------------------------------------------------------------
 391 // ciEnv::get_klass_by_index_impl
 392 //
 393 // Implementation of get_klass_by_index.
 394 ciKlass* ciEnv::get_klass_by_index_impl(ciInstanceKlass* accessor,
 395                                         int index,
 396                                         bool& is_accessible) {
 397   assert(accessor->get_instanceKlass()->is_linked(), "must be linked before accessing constant pool");
 398   EXCEPTION_CONTEXT;
 399   constantPoolHandle cpool(THREAD, accessor->get_instanceKlass()->constants());
 400   KlassHandle klass (THREAD, constantPoolOopDesc::klass_at_if_loaded(cpool, index));
 401   symbolHandle klass_name;
 402   if (klass.is_null()) {
 403     // The klass has not been inserted into the constant pool.
 404     // Try to look it up by name.
 405     {
 406       // We have to lock the cpool to keep the oop from being resolved
 407       // while we are accessing it.
 408       ObjectLocker ol(cpool, THREAD);
 409 
 410       constantTag tag = cpool->tag_at(index);
 411       if (tag.is_klass()) {
 412         // The klass has been inserted into the constant pool
 413         // very recently.
 414         klass = KlassHandle(THREAD, cpool->resolved_klass_at(index));
 415       } else if (tag.is_symbol()) {
 416         klass_name = symbolHandle(THREAD, cpool->symbol_at(index));
 417       } else {
 418         assert(cpool->tag_at(index).is_unresolved_klass(), "wrong tag");
 419         klass_name = symbolHandle(THREAD, cpool->unresolved_klass_at(index));
 420       }
 421     }
 422   }
 423 
 424   if (klass.is_null()) {
 425     // Not found in constant pool.  Use the name to do the lookup.
 426     ciKlass* k = get_klass_by_name_impl(accessor,
 427                                         get_object(klass_name())->as_symbol(),
 428                                         false);
 429     // Calculate accessibility the hard way.
 430     if (!k->is_loaded()) {
 431       is_accessible = false;
 432     } else if (k->loader() != accessor->loader() &&
 433                get_klass_by_name_impl(accessor, k->name(), true) == NULL) {
 434       // Loaded only remotely.  Not linked yet.
 435       is_accessible = false;
 436     } else {
 437       // Linked locally, and we must also check public/private, etc.
 438       is_accessible = check_klass_accessibility(accessor, k->get_klassOop());
 439     }
 440     return k;
 441   }
 442 
 443   // Check for prior unloaded klass.  The SystemDictionary's answers
 444   // can vary over time but the compiler needs consistency.
 445   ciSymbol* name = get_object(klass()->klass_part()->name())->as_symbol();
 446   ciKlass* unloaded_klass = check_get_unloaded_klass(accessor, name);
 447   if (unloaded_klass != NULL) {
 448     is_accessible = false;
 449     return unloaded_klass;
 450   }
 451 
 452   // It is known to be accessible, since it was found in the constant pool.
 453   is_accessible = true;
 454   return get_object(klass())->as_klass();
 455 }
 456 
 457 // ------------------------------------------------------------------
 458 // ciEnv::get_klass_by_index
 459 //
 460 // Get a klass from the constant pool.
 461 ciKlass* ciEnv::get_klass_by_index(ciInstanceKlass* accessor,
 462                                    int index,
 463                                    bool& is_accessible) {
 464   GUARDED_VM_ENTRY(return get_klass_by_index_impl(accessor, index, is_accessible);)
 465 }
 466 
 467 // ------------------------------------------------------------------
 468 // ciEnv::get_constant_by_index_impl
 469 //
 470 // Implementation of get_constant_by_index().
 471 ciConstant ciEnv::get_constant_by_index_impl(ciInstanceKlass* accessor,
 472                                              int index) {
 473   EXCEPTION_CONTEXT;
 474   instanceKlass* ik_accessor = accessor->get_instanceKlass();
 475   assert(ik_accessor->is_linked(), "must be linked before accessing constant pool");
 476   constantPoolOop cpool = ik_accessor->constants();
 477   constantTag tag = cpool->tag_at(index);
 478   if (tag.is_int()) {
 479     return ciConstant(T_INT, (jint)cpool->int_at(index));
 480   } else if (tag.is_long()) {
 481     return ciConstant((jlong)cpool->long_at(index));
 482   } else if (tag.is_float()) {
 483     return ciConstant((jfloat)cpool->float_at(index));
 484   } else if (tag.is_double()) {
 485     return ciConstant((jdouble)cpool->double_at(index));
 486   } else if (tag.is_string() || tag.is_unresolved_string()) {
 487     oop string = cpool->string_at(index, THREAD);
 488     if (HAS_PENDING_EXCEPTION) {
 489       CLEAR_PENDING_EXCEPTION;
 490       record_out_of_memory_failure();
 491       return ciConstant();
 492     }
 493     ciObject* constant = get_object(string);
 494     assert (constant->is_instance(), "must be an instance, or not? ");
 495     return ciConstant(T_OBJECT, constant);
 496   } else if (tag.is_klass() || tag.is_unresolved_klass()) {
 497     // 4881222: allow ldc to take a class type
 498     bool ignore;
 499     ciKlass* klass = get_klass_by_index_impl(accessor, index, ignore);
 500     if (HAS_PENDING_EXCEPTION) {
 501       CLEAR_PENDING_EXCEPTION;
 502       record_out_of_memory_failure();
 503       return ciConstant();
 504     }
 505     assert (klass->is_instance_klass() || klass->is_array_klass(),
 506             "must be an instance or array klass ");
 507     return ciConstant(T_OBJECT, klass);
 508   } else {
 509     ShouldNotReachHere();
 510     return ciConstant();
 511   }
 512 }
 513 
 514 // ------------------------------------------------------------------
 515 // ciEnv::is_unresolved_string_impl
 516 //
 517 // Implementation of is_unresolved_string().
 518 bool ciEnv::is_unresolved_string_impl(instanceKlass* accessor, int index) const {
 519   EXCEPTION_CONTEXT;
 520   assert(accessor->is_linked(), "must be linked before accessing constant pool");
 521   constantPoolOop cpool = accessor->constants();
 522   constantTag tag = cpool->tag_at(index);
 523   return tag.is_unresolved_string();
 524 }
 525 
 526 // ------------------------------------------------------------------
 527 // ciEnv::is_unresolved_klass_impl
 528 //
 529 // Implementation of is_unresolved_klass().
 530 bool ciEnv::is_unresolved_klass_impl(instanceKlass* accessor, int index) const {
 531   EXCEPTION_CONTEXT;
 532   assert(accessor->is_linked(), "must be linked before accessing constant pool");
 533   constantPoolOop cpool = accessor->constants();
 534   constantTag tag = cpool->tag_at(index);
 535   return tag.is_unresolved_klass();
 536 }
 537 
 538 // ------------------------------------------------------------------
 539 // ciEnv::get_constant_by_index
 540 //
 541 // Pull a constant out of the constant pool.  How appropriate.
 542 //
 543 // Implementation note: this query is currently in no way cached.
 544 ciConstant ciEnv::get_constant_by_index(ciInstanceKlass* accessor,
 545                                         int index) {
 546   GUARDED_VM_ENTRY(return get_constant_by_index_impl(accessor, index); )
 547 }
 548 
 549 // ------------------------------------------------------------------
 550 // ciEnv::is_unresolved_string
 551 //
 552 // Check constant pool
 553 //
 554 // Implementation note: this query is currently in no way cached.
 555 bool ciEnv::is_unresolved_string(ciInstanceKlass* accessor,
 556                                         int index) const {
 557   GUARDED_VM_ENTRY(return is_unresolved_string_impl(accessor->get_instanceKlass(), index); )
 558 }
 559 
 560 // ------------------------------------------------------------------
 561 // ciEnv::is_unresolved_klass
 562 //
 563 // Check constant pool
 564 //
 565 // Implementation note: this query is currently in no way cached.
 566 bool ciEnv::is_unresolved_klass(ciInstanceKlass* accessor,
 567                                         int index) const {
 568   GUARDED_VM_ENTRY(return is_unresolved_klass_impl(accessor->get_instanceKlass(), index); )
 569 }
 570 
 571 // ------------------------------------------------------------------
 572 // ciEnv::get_field_by_index_impl
 573 //
 574 // Implementation of get_field_by_index.
 575 //
 576 // Implementation note: the results of field lookups are cached
 577 // in the accessor klass.
 578 ciField* ciEnv::get_field_by_index_impl(ciInstanceKlass* accessor,
 579                                         int index) {
 580   ciConstantPoolCache* cache = accessor->field_cache();
 581   if (cache == NULL) {
 582     ciField* field = new (arena()) ciField(accessor, index);
 583     return field;
 584   } else {
 585     ciField* field = (ciField*)cache->get(index);
 586     if (field == NULL) {
 587       field = new (arena()) ciField(accessor, index);
 588       cache->insert(index, field);
 589     }
 590     return field;
 591   }
 592 }
 593 
 594 // ------------------------------------------------------------------
 595 // ciEnv::get_field_by_index
 596 //
 597 // Get a field by index from a klass's constant pool.
 598 ciField* ciEnv::get_field_by_index(ciInstanceKlass* accessor,
 599                                    int index) {
 600   GUARDED_VM_ENTRY(return get_field_by_index_impl(accessor, index);)
 601 }
 602 
 603 // ------------------------------------------------------------------
 604 // ciEnv::lookup_method
 605 //
 606 // Perform an appropriate method lookup based on accessor, holder,
 607 // name, signature, and bytecode.
 608 methodOop ciEnv::lookup_method(instanceKlass*  accessor,
 609                                instanceKlass*  holder,
 610                                symbolOop       name,
 611                                symbolOop       sig,
 612                                Bytecodes::Code bc) {
 613   EXCEPTION_CONTEXT;
 614   KlassHandle h_accessor(THREAD, accessor);
 615   KlassHandle h_holder(THREAD, holder);
 616   symbolHandle h_name(THREAD, name);
 617   symbolHandle h_sig(THREAD, sig);
 618   LinkResolver::check_klass_accessability(h_accessor, h_holder, KILL_COMPILE_ON_FATAL_(NULL));
 619   methodHandle dest_method;
 620   switch (bc) {
 621   case Bytecodes::_invokestatic:
 622     dest_method =
 623       LinkResolver::resolve_static_call_or_null(h_holder, h_name, h_sig, h_accessor);
 624     break;
 625   case Bytecodes::_invokespecial:
 626     dest_method =
 627       LinkResolver::resolve_special_call_or_null(h_holder, h_name, h_sig, h_accessor);
 628     break;
 629   case Bytecodes::_invokeinterface:
 630     dest_method =
 631       LinkResolver::linktime_resolve_interface_method_or_null(h_holder, h_name, h_sig,
 632                                                               h_accessor, true);
 633     break;
 634   case Bytecodes::_invokevirtual:
 635     dest_method =
 636       LinkResolver::linktime_resolve_virtual_method_or_null(h_holder, h_name, h_sig,
 637                                                             h_accessor, true);
 638     break;
 639   default: ShouldNotReachHere();
 640   }
 641 
 642   return dest_method();
 643 }
 644 
 645 
 646 // ------------------------------------------------------------------
 647 // ciEnv::get_method_by_index_impl
 648 ciMethod* ciEnv::get_method_by_index_impl(ciInstanceKlass* accessor,
 649                                      int index, Bytecodes::Code bc) {
 650   // Get the method's declared holder.
 651 
 652   assert(accessor->get_instanceKlass()->is_linked(), "must be linked before accessing constant pool");
 653   constantPoolHandle cpool = accessor->get_instanceKlass()->constants();
 654   int holder_index = cpool->klass_ref_index_at(index);
 655   bool holder_is_accessible;
 656   ciKlass* holder = get_klass_by_index_impl(accessor, holder_index, holder_is_accessible);
 657   ciInstanceKlass* declared_holder = get_instance_klass_for_declared_method_holder(holder);
 658 
 659   // Get the method's name and signature.
 660   int nt_index = cpool->name_and_type_ref_index_at(index);
 661   int sig_index = cpool->signature_ref_index_at(nt_index);
 662   symbolOop name_sym = cpool->name_ref_at(index);
 663   symbolOop sig_sym = cpool->symbol_at(sig_index);
 664 
 665   if (holder_is_accessible) { // Our declared holder is loaded.
 666     instanceKlass* lookup = declared_holder->get_instanceKlass();
 667     methodOop m = lookup_method(accessor->get_instanceKlass(), lookup, name_sym, sig_sym, bc);
 668     if (m != NULL) {
 669       // We found the method.
 670       return get_object(m)->as_method();
 671     }
 672   }
 673 
 674   // Either the declared holder was not loaded, or the method could
 675   // not be found.  Create a dummy ciMethod to represent the failed
 676   // lookup.
 677 
 678   return get_unloaded_method(declared_holder,
 679                              get_object(name_sym)->as_symbol(),
 680                              get_object(sig_sym)->as_symbol());
 681 }
 682 
 683 
 684 // ------------------------------------------------------------------
 685 // ciEnv::get_instance_klass_for_declared_method_holder
 686 ciInstanceKlass* ciEnv::get_instance_klass_for_declared_method_holder(ciKlass* method_holder) {
 687   // For the case of <array>.clone(), the method holder can be a ciArrayKlass
 688   // instead of a ciInstanceKlass.  For that case simply pretend that the
 689   // declared holder is Object.clone since that's where the call will bottom out.
 690   // A more correct fix would trickle out through many interfaces in CI,
 691   // requiring ciInstanceKlass* to become ciKlass* and many more places would
 692   // require checks to make sure the expected type was found.  Given that this
 693   // only occurs for clone() the more extensive fix seems like overkill so
 694   // instead we simply smear the array type into Object.
 695   if (method_holder->is_instance_klass()) {
 696     return method_holder->as_instance_klass();
 697   } else if (method_holder->is_array_klass()) {
 698     return current()->Object_klass();
 699   } else {
 700     ShouldNotReachHere();
 701   }
 702   return NULL;
 703 }
 704 
 705 
 706 
 707 
 708 // ------------------------------------------------------------------
 709 // ciEnv::get_method_by_index
 710 ciMethod* ciEnv::get_method_by_index(ciInstanceKlass* accessor,
 711                                      int index, Bytecodes::Code bc) {
 712   GUARDED_VM_ENTRY(return get_method_by_index_impl(accessor, index, bc);)
 713 }
 714 
 715 // ------------------------------------------------------------------
 716 // ciEnv::name_buffer
 717 char *ciEnv::name_buffer(int req_len) {
 718   if (_name_buffer_len < req_len) {
 719     if (_name_buffer == NULL) {
 720       _name_buffer = (char*)arena()->Amalloc(sizeof(char)*req_len);
 721       _name_buffer_len = req_len;
 722     } else {
 723       _name_buffer =
 724         (char*)arena()->Arealloc(_name_buffer, _name_buffer_len, req_len);
 725       _name_buffer_len = req_len;
 726     }
 727   }
 728   return _name_buffer;
 729 }
 730 
 731 // ------------------------------------------------------------------
 732 // ciEnv::is_in_vm
 733 bool ciEnv::is_in_vm() {
 734   return JavaThread::current()->thread_state() == _thread_in_vm;
 735 }
 736 
 737 bool ciEnv::system_dictionary_modification_counter_changed() {
 738   return _system_dictionary_modification_counter != SystemDictionary::number_of_modifications();
 739 }
 740 
 741 // ------------------------------------------------------------------
 742 // ciEnv::check_for_system_dictionary_modification
 743 // Check for changes to the system dictionary during compilation
 744 // class loads, evolution, breakpoints
 745 void ciEnv::check_for_system_dictionary_modification(ciMethod* target) {
 746   if (failing())  return;  // no need for further checks
 747 
 748   // Dependencies must be checked when the system dictionary changes.
 749   // If logging is enabled all violated dependences will be recorded in
 750   // the log.  In debug mode check dependencies even if the system
 751   // dictionary hasn't changed to verify that no invalid dependencies
 752   // were inserted.  Any violated dependences in this case are dumped to
 753   // the tty.
 754 
 755   bool counter_changed = system_dictionary_modification_counter_changed();
 756   bool test_deps = counter_changed;
 757   DEBUG_ONLY(test_deps = true);
 758   if (!test_deps)  return;
 759 
 760   bool print_failures = false;
 761   DEBUG_ONLY(print_failures = !counter_changed);
 762 
 763   bool keep_going = (print_failures || xtty != NULL);
 764 
 765   int violated = 0;
 766 
 767   for (Dependencies::DepStream deps(dependencies()); deps.next(); ) {
 768     klassOop witness = deps.check_dependency();
 769     if (witness != NULL) {
 770       ++violated;
 771       if (print_failures)  deps.print_dependency(witness, /*verbose=*/ true);
 772       // If there's no log and we're not sanity-checking, we're done.
 773       if (!keep_going)     break;
 774     }
 775   }
 776 
 777   if (violated != 0) {
 778     assert(counter_changed, "failed dependencies, but counter didn't change");
 779     record_failure("concurrent class loading");
 780   }
 781 }
 782 
 783 // ------------------------------------------------------------------
 784 // ciEnv::register_method
 785 void ciEnv::register_method(ciMethod* target,
 786                             int entry_bci,
 787                             CodeOffsets* offsets,
 788                             int orig_pc_offset,
 789                             CodeBuffer* code_buffer,
 790                             int frame_words,
 791                             OopMapSet* oop_map_set,
 792                             ExceptionHandlerTable* handler_table,
 793                             ImplicitExceptionTable* inc_table,
 794                             AbstractCompiler* compiler,
 795                             int comp_level,
 796                             bool has_debug_info,
 797                             bool has_unsafe_access) {
 798   VM_ENTRY_MARK;
 799   nmethod* nm = NULL;
 800   {
 801     // To prevent compile queue updates.
 802     MutexLocker locker(MethodCompileQueue_lock, THREAD);
 803 
 804     // Prevent SystemDictionary::add_to_hierarchy from running
 805     // and invalidating our dependencies until we install this method.
 806     MutexLocker ml(Compile_lock);
 807 
 808     if (log() != NULL) {
 809       // Log the dependencies which this compilation declares.
 810       dependencies()->log_all_dependencies();
 811     }
 812 
 813     // Encode the dependencies now, so we can check them right away.
 814     dependencies()->encode_content_bytes();
 815 
 816     // Check for {class loads, evolution, breakpoints} during compilation
 817     check_for_system_dictionary_modification(target);
 818 
 819     methodHandle method(THREAD, target->get_methodOop());
 820 
 821     if (failing()) {
 822       // While not a true deoptimization, it is a preemptive decompile.
 823       methodDataOop mdo = method()->method_data();
 824       if (mdo != NULL) {
 825         mdo->inc_decompile_count();
 826       }
 827 
 828       // All buffers in the CodeBuffer are allocated in the CodeCache.
 829       // If the code buffer is created on each compile attempt
 830       // as in C2, then it must be freed.
 831       code_buffer->free_blob();
 832       return;
 833     }
 834 
 835     assert(offsets->value(CodeOffsets::Deopt) != -1, "must have deopt entry");
 836     assert(offsets->value(CodeOffsets::Exceptions) != -1, "must have exception entry");
 837 
 838     nm =  nmethod::new_nmethod(method,
 839                                compile_id(),
 840                                entry_bci,
 841                                offsets,
 842                                orig_pc_offset,
 843                                debug_info(), dependencies(), code_buffer,
 844                                frame_words, oop_map_set,
 845                                handler_table, inc_table,
 846                                compiler, comp_level);
 847 
 848     // Free codeBlobs
 849     code_buffer->free_blob();
 850 
 851     // stress test 6243940 by immediately making the method
 852     // non-entrant behind the system's back. This has serious
 853     // side effects on the code cache and is not meant for
 854     // general stress testing
 855     if (nm != NULL && StressNonEntrant) {
 856       MutexLockerEx pl(Patching_lock, Mutex::_no_safepoint_check_flag);
 857       NativeJump::patch_verified_entry(nm->entry_point(), nm->verified_entry_point(),
 858                   SharedRuntime::get_handle_wrong_method_stub());
 859     }
 860 
 861     if (nm == NULL) {
 862       // The CodeCache is full.  Print out warning and disable compilation.
 863       record_failure("code cache is full");
 864       UseInterpreter = true;
 865       if (UseCompiler || AlwaysCompileLoopMethods ) {
 866 #ifndef PRODUCT
 867         warning("CodeCache is full. Compiler has been disabled");
 868         if (CompileTheWorld || ExitOnFullCodeCache) {
 869           before_exit(JavaThread::current());
 870           exit_globals(); // will delete tty
 871           vm_direct_exit(CompileTheWorld ? 0 : 1);
 872         }
 873 #endif
 874         UseCompiler               = false;
 875         AlwaysCompileLoopMethods  = false;
 876       }
 877     } else {
 878       NOT_PRODUCT(nm->set_has_debug_info(has_debug_info); )
 879       nm->set_has_unsafe_access(has_unsafe_access);
 880 
 881       // Record successful registration.
 882       // (Put nm into the task handle *before* publishing to the Java heap.)
 883       if (task() != NULL)  task()->set_code(nm);
 884 
 885       if (entry_bci == InvocationEntryBci) {
 886 #ifdef TIERED
 887         // If there is an old version we're done with it
 888         nmethod* old = method->code();
 889         if (TraceMethodReplacement && old != NULL) {
 890           ResourceMark rm;
 891           char *method_name = method->name_and_sig_as_C_string();
 892           tty->print_cr("Replacing method %s", method_name);
 893         }
 894         if (old != NULL ) {
 895           old->make_not_entrant();
 896         }
 897 #endif // TIERED
 898         if (TraceNMethodInstalls ) {
 899           ResourceMark rm;
 900           char *method_name = method->name_and_sig_as_C_string();
 901           ttyLocker ttyl;
 902           tty->print_cr("Installing method (%d) %s ",
 903                         comp_level,
 904                         method_name);
 905         }
 906         // Allow the code to be executed
 907         method->set_code(method, nm);
 908       } else {
 909         if (TraceNMethodInstalls ) {
 910           ResourceMark rm;
 911           char *method_name = method->name_and_sig_as_C_string();
 912           ttyLocker ttyl;
 913           tty->print_cr("Installing osr method (%d) %s @ %d",
 914                         comp_level,
 915                         method_name,
 916                         entry_bci);
 917         }
 918         instanceKlass::cast(method->method_holder())->add_osr_nmethod(nm);
 919 
 920       }
 921     }
 922   }
 923   // JVMTI -- compiled method notification (must be done outside lock)
 924   if (nm != NULL) {
 925     nm->post_compiled_method_load_event();
 926   }
 927 
 928 }
 929 
 930 
 931 // ------------------------------------------------------------------
 932 // ciEnv::find_system_klass
 933 ciKlass* ciEnv::find_system_klass(ciSymbol* klass_name) {
 934   VM_ENTRY_MARK;
 935   return get_klass_by_name_impl(NULL, klass_name, false);
 936 }
 937 
 938 // ------------------------------------------------------------------
 939 // ciEnv::comp_level
 940 int ciEnv::comp_level() {
 941   if (task() == NULL)  return CompLevel_full_optimization;
 942   return task()->comp_level();
 943 }
 944 
 945 // ------------------------------------------------------------------
 946 // ciEnv::compile_id
 947 uint ciEnv::compile_id() {
 948   if (task() == NULL)  return 0;
 949   return task()->compile_id();
 950 }
 951 
 952 // ------------------------------------------------------------------
 953 // ciEnv::notice_inlined_method()
 954 void ciEnv::notice_inlined_method(ciMethod* method) {
 955   _num_inlined_bytecodes += method->code_size();
 956 }
 957 
 958 // ------------------------------------------------------------------
 959 // ciEnv::num_inlined_bytecodes()
 960 int ciEnv::num_inlined_bytecodes() const {
 961   return _num_inlined_bytecodes;
 962 }
 963 
 964 // ------------------------------------------------------------------
 965 // ciEnv::record_failure()
 966 void ciEnv::record_failure(const char* reason) {
 967   if (log() != NULL) {
 968     log()->elem("failure reason='%s'", reason);
 969   }
 970   if (_failure_reason == NULL) {
 971     // Record the first failure reason.
 972     _failure_reason = reason;
 973   }
 974 }
 975 
 976 // ------------------------------------------------------------------
 977 // ciEnv::record_method_not_compilable()
 978 void ciEnv::record_method_not_compilable(const char* reason, bool all_tiers) {
 979   int new_compilable =
 980     all_tiers ? MethodCompilable_never : MethodCompilable_not_at_tier ;
 981 
 982   // Only note transitions to a worse state
 983   if (new_compilable > _compilable) {
 984     if (log() != NULL) {
 985       if (all_tiers) {
 986         log()->elem("method_not_compilable");
 987       } else {
 988         log()->elem("method_not_compilable_at_tier");
 989       }
 990     }
 991     _compilable = new_compilable;
 992 
 993     // Reset failure reason; this one is more important.
 994     _failure_reason = NULL;
 995     record_failure(reason);
 996   }
 997 }
 998 
 999 // ------------------------------------------------------------------
1000 // ciEnv::record_out_of_memory_failure()
1001 void ciEnv::record_out_of_memory_failure() {
1002   // If memory is low, we stop compiling methods.
1003   record_method_not_compilable("out of memory");
1004 }