src/share/vm/ci/ciEnv.cpp

Thu, 24 May 2018 18:41:44 +0800

author
aoqi
date
Thu, 24 May 2018 18:41:44 +0800
changeset 8856
ac27a9c85bea
parent 7535
7ae4e26cb1e0
child 9931
fd44df5e3bc3
permissions
-rw-r--r--

Merge

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

mercurial