aoqi@0: /* aoqi@0: * Copyright (c) 1999, 2013, Oracle and/or its affiliates. All rights reserved. aoqi@0: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. aoqi@0: * aoqi@0: * This code is free software; you can redistribute it and/or modify it aoqi@0: * under the terms of the GNU General Public License version 2 only, as aoqi@0: * published by the Free Software Foundation. aoqi@0: * aoqi@0: * This code is distributed in the hope that it will be useful, but WITHOUT aoqi@0: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or aoqi@0: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License aoqi@0: * version 2 for more details (a copy is included in the LICENSE file that aoqi@0: * accompanied this code). aoqi@0: * aoqi@0: * You should have received a copy of the GNU General Public License version aoqi@0: * 2 along with this work; if not, write to the Free Software Foundation, aoqi@0: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. aoqi@0: * aoqi@0: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA aoqi@0: * or visit www.oracle.com if you need additional information or have any aoqi@0: * questions. aoqi@0: * aoqi@0: */ aoqi@0: aoqi@0: #ifndef SHARE_VM_CI_CIENV_HPP aoqi@0: #define SHARE_VM_CI_CIENV_HPP aoqi@0: aoqi@0: #include "ci/ciClassList.hpp" aoqi@0: #include "ci/ciObjectFactory.hpp" aoqi@0: #include "classfile/systemDictionary.hpp" aoqi@0: #include "code/debugInfoRec.hpp" aoqi@0: #include "code/dependencies.hpp" aoqi@0: #include "code/exceptionHandlerTable.hpp" aoqi@0: #include "compiler/oopMap.hpp" aoqi@0: #include "runtime/thread.hpp" aoqi@0: aoqi@0: class CompileTask; aoqi@0: aoqi@0: // ciEnv aoqi@0: // aoqi@0: // This class is the top level broker for requests from the compiler aoqi@0: // to the VM. aoqi@0: class ciEnv : StackObj { aoqi@0: CI_PACKAGE_ACCESS_TO aoqi@0: aoqi@0: friend class CompileBroker; aoqi@0: friend class Dependencies; // for get_object, during logging aoqi@0: aoqi@0: private: aoqi@0: Arena* _arena; // Alias for _ciEnv_arena except in init_shared_objects() aoqi@0: Arena _ciEnv_arena; aoqi@0: int _system_dictionary_modification_counter; aoqi@0: ciObjectFactory* _factory; aoqi@0: OopRecorder* _oop_recorder; aoqi@0: DebugInformationRecorder* _debug_info; aoqi@0: Dependencies* _dependencies; aoqi@0: const char* _failure_reason; aoqi@0: int _compilable; aoqi@0: bool _break_at_compile; aoqi@0: int _num_inlined_bytecodes; aoqi@0: CompileTask* _task; // faster access to CompilerThread::task aoqi@0: CompileLog* _log; // faster access to CompilerThread::log aoqi@0: void* _compiler_data; // compiler-specific stuff, if any aoqi@0: aoqi@0: char* _name_buffer; aoqi@0: int _name_buffer_len; aoqi@0: aoqi@0: // Cache Jvmti state aoqi@0: bool _jvmti_can_hotswap_or_post_breakpoint; aoqi@0: bool _jvmti_can_access_local_variables; aoqi@0: bool _jvmti_can_post_on_exceptions; aoqi@0: aoqi@0: // Cache DTrace flags aoqi@0: bool _dtrace_extended_probes; aoqi@0: bool _dtrace_monitor_probes; aoqi@0: bool _dtrace_method_probes; aoqi@0: bool _dtrace_alloc_probes; aoqi@0: aoqi@0: // Distinguished instances of certain ciObjects.. aoqi@0: static ciObject* _null_object_instance; aoqi@0: aoqi@0: #define WK_KLASS_DECL(name, ignore_s, ignore_o) static ciInstanceKlass* _##name; aoqi@0: WK_KLASSES_DO(WK_KLASS_DECL) aoqi@0: #undef WK_KLASS_DECL aoqi@0: aoqi@0: static ciSymbol* _unloaded_cisymbol; aoqi@0: static ciInstanceKlass* _unloaded_ciinstance_klass; aoqi@0: static ciObjArrayKlass* _unloaded_ciobjarrayklass; aoqi@0: aoqi@0: static jobject _ArrayIndexOutOfBoundsException_handle; aoqi@0: static jobject _ArrayStoreException_handle; aoqi@0: static jobject _ClassCastException_handle; aoqi@0: aoqi@0: ciInstance* _NullPointerException_instance; aoqi@0: ciInstance* _ArithmeticException_instance; aoqi@0: ciInstance* _ArrayIndexOutOfBoundsException_instance; aoqi@0: ciInstance* _ArrayStoreException_instance; aoqi@0: ciInstance* _ClassCastException_instance; aoqi@0: aoqi@0: ciInstance* _the_null_string; // The Java string "null" aoqi@0: ciInstance* _the_min_jint_string; // The Java string "-2147483648" aoqi@0: aoqi@0: // Look up a klass by name from a particular class loader (the accessor's). aoqi@0: // If require_local, result must be defined in that class loader, or NULL. aoqi@0: // If !require_local, a result from remote class loader may be reported, aoqi@0: // if sufficient class loader constraints exist such that initiating aoqi@0: // a class loading request from the given loader is bound to return aoqi@0: // the class defined in the remote loader (or throw an error). aoqi@0: // aoqi@0: // Return an unloaded klass if !require_local and no class at all is found. aoqi@0: // aoqi@0: // The CI treats a klass as loaded if it is consistently defined in aoqi@0: // another loader, even if it hasn't yet been loaded in all loaders aoqi@0: // that could potentially see it via delegation. aoqi@0: ciKlass* get_klass_by_name(ciKlass* accessing_klass, aoqi@0: ciSymbol* klass_name, aoqi@0: bool require_local); aoqi@0: aoqi@0: // Constant pool access. aoqi@0: ciKlass* get_klass_by_index(constantPoolHandle cpool, aoqi@0: int klass_index, aoqi@0: bool& is_accessible, aoqi@0: ciInstanceKlass* loading_klass); aoqi@0: ciConstant get_constant_by_index(constantPoolHandle cpool, aoqi@0: int pool_index, int cache_index, aoqi@0: ciInstanceKlass* accessor); aoqi@0: ciField* get_field_by_index(ciInstanceKlass* loading_klass, aoqi@0: int field_index); aoqi@0: ciMethod* get_method_by_index(constantPoolHandle cpool, aoqi@0: int method_index, Bytecodes::Code bc, aoqi@0: ciInstanceKlass* loading_klass); aoqi@0: aoqi@0: // Implementation methods for loading and constant pool access. aoqi@0: ciKlass* get_klass_by_name_impl(ciKlass* accessing_klass, aoqi@0: constantPoolHandle cpool, aoqi@0: ciSymbol* klass_name, aoqi@0: bool require_local); aoqi@0: ciKlass* get_klass_by_index_impl(constantPoolHandle cpool, aoqi@0: int klass_index, aoqi@0: bool& is_accessible, aoqi@0: ciInstanceKlass* loading_klass); aoqi@0: ciConstant get_constant_by_index_impl(constantPoolHandle cpool, aoqi@0: int pool_index, int cache_index, aoqi@0: ciInstanceKlass* loading_klass); aoqi@0: ciField* get_field_by_index_impl(ciInstanceKlass* loading_klass, aoqi@0: int field_index); aoqi@0: ciMethod* get_method_by_index_impl(constantPoolHandle cpool, aoqi@0: int method_index, Bytecodes::Code bc, aoqi@0: ciInstanceKlass* loading_klass); aoqi@0: aoqi@0: // Helper methods aoqi@0: bool check_klass_accessibility(ciKlass* accessing_klass, aoqi@0: Klass* resolved_klass); aoqi@0: Method* lookup_method(InstanceKlass* accessor, aoqi@0: InstanceKlass* holder, aoqi@0: Symbol* name, aoqi@0: Symbol* sig, aoqi@0: Bytecodes::Code bc); aoqi@0: aoqi@0: // Get a ciObject from the object factory. Ensures uniqueness aoqi@0: // of ciObjects. aoqi@0: ciObject* get_object(oop o) { aoqi@0: if (o == NULL) { aoqi@0: return _null_object_instance; aoqi@0: } else { aoqi@0: return _factory->get(o); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: ciSymbol* get_symbol(Symbol* o) { aoqi@0: if (o == NULL) { aoqi@0: ShouldNotReachHere(); aoqi@0: return NULL; aoqi@0: } else { aoqi@0: return _factory->get_symbol(o); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: ciMetadata* get_metadata(Metadata* o) { aoqi@0: if (o == NULL) { aoqi@0: return NULL; aoqi@0: } else { aoqi@0: return _factory->get_metadata(o); aoqi@0: } aoqi@0: } aoqi@0: stefank@6992: void ensure_metadata_alive(ciMetadata* m) { stefank@6992: _factory->ensure_metadata_alive(m); stefank@6992: } stefank@6992: aoqi@0: ciInstance* get_instance(oop o) { aoqi@0: if (o == NULL) return NULL; aoqi@0: return get_object(o)->as_instance(); aoqi@0: } aoqi@0: ciObjArrayKlass* get_obj_array_klass(Klass* o) { aoqi@0: if (o == NULL) return NULL; aoqi@0: return get_metadata(o)->as_obj_array_klass(); aoqi@0: } aoqi@0: ciTypeArrayKlass* get_type_array_klass(Klass* o) { aoqi@0: if (o == NULL) return NULL; aoqi@0: return get_metadata(o)->as_type_array_klass(); aoqi@0: } aoqi@0: ciKlass* get_klass(Klass* o) { aoqi@0: if (o == NULL) return NULL; aoqi@0: return get_metadata(o)->as_klass(); aoqi@0: } aoqi@0: ciInstanceKlass* get_instance_klass(Klass* o) { aoqi@0: if (o == NULL) return NULL; aoqi@0: return get_metadata(o)->as_instance_klass(); aoqi@0: } aoqi@0: ciMethod* get_method(Method* o) { aoqi@0: if (o == NULL) return NULL; aoqi@0: return get_metadata(o)->as_method(); aoqi@0: } aoqi@0: ciMethodData* get_method_data(MethodData* o) { aoqi@0: if (o == NULL) return NULL; aoqi@0: return get_metadata(o)->as_method_data(); aoqi@0: } aoqi@0: aoqi@0: ciMethod* get_method_from_handle(Method* method); aoqi@0: aoqi@0: ciInstance* get_or_create_exception(jobject& handle, Symbol* name); aoqi@0: aoqi@0: // Get a ciMethod representing either an unfound method or aoqi@0: // a method with an unloaded holder. Ensures uniqueness of aoqi@0: // the result. aoqi@0: ciMethod* get_unloaded_method(ciInstanceKlass* holder, aoqi@0: ciSymbol* name, aoqi@0: ciSymbol* signature, aoqi@0: ciInstanceKlass* accessor) { aoqi@0: return _factory->get_unloaded_method(holder, name, signature, accessor); aoqi@0: } aoqi@0: aoqi@0: // Get a ciKlass representing an unloaded klass. aoqi@0: // Ensures uniqueness of the result. aoqi@0: ciKlass* get_unloaded_klass(ciKlass* accessing_klass, aoqi@0: ciSymbol* name) { aoqi@0: return _factory->get_unloaded_klass(accessing_klass, name, true); aoqi@0: } aoqi@0: aoqi@0: // Get a ciKlass representing an unloaded klass mirror. aoqi@0: // Result is not necessarily unique, but will be unloaded. aoqi@0: ciInstance* get_unloaded_klass_mirror(ciKlass* type) { aoqi@0: return _factory->get_unloaded_klass_mirror(type); aoqi@0: } aoqi@0: aoqi@0: // Get a ciInstance representing an unresolved method handle constant. aoqi@0: ciInstance* get_unloaded_method_handle_constant(ciKlass* holder, aoqi@0: ciSymbol* name, aoqi@0: ciSymbol* signature, aoqi@0: int ref_kind) { aoqi@0: return _factory->get_unloaded_method_handle_constant(holder, name, signature, ref_kind); aoqi@0: } aoqi@0: aoqi@0: // Get a ciInstance representing an unresolved method type constant. aoqi@0: ciInstance* get_unloaded_method_type_constant(ciSymbol* signature) { aoqi@0: return _factory->get_unloaded_method_type_constant(signature); aoqi@0: } aoqi@0: aoqi@0: // See if we already have an unloaded klass for the given name aoqi@0: // or return NULL if not. aoqi@0: ciKlass *check_get_unloaded_klass(ciKlass* accessing_klass, ciSymbol* name) { aoqi@0: return _factory->get_unloaded_klass(accessing_klass, name, false); aoqi@0: } aoqi@0: aoqi@0: // Get a ciReturnAddress corresponding to the given bci. aoqi@0: // Ensures uniqueness of the result. aoqi@0: ciReturnAddress* get_return_address(int bci) { aoqi@0: return _factory->get_return_address(bci); aoqi@0: } aoqi@0: aoqi@0: // Get a ciMethodData representing the methodData for a method aoqi@0: // with none. aoqi@0: ciMethodData* get_empty_methodData() { aoqi@0: return _factory->get_empty_methodData(); aoqi@0: } aoqi@0: aoqi@0: // General utility : get a buffer of some required length. aoqi@0: // Used in symbol creation. aoqi@0: char* name_buffer(int req_len); aoqi@0: aoqi@0: // Is this thread currently in the VM state? aoqi@0: static bool is_in_vm(); aoqi@0: aoqi@0: // Helper routine for determining the validity of a compilation with aoqi@0: // respect to method dependencies (e.g. concurrent class loading). aoqi@0: void validate_compile_task_dependencies(ciMethod* target); aoqi@0: aoqi@0: public: aoqi@0: enum { aoqi@0: MethodCompilable, aoqi@0: MethodCompilable_not_at_tier, aoqi@0: MethodCompilable_never aoqi@0: }; aoqi@0: aoqi@0: ciEnv(CompileTask* task, int system_dictionary_modification_counter); aoqi@0: // Used only during initialization of the ci aoqi@0: ciEnv(Arena* arena); aoqi@0: ~ciEnv(); aoqi@0: aoqi@0: OopRecorder* oop_recorder() { return _oop_recorder; } aoqi@0: void set_oop_recorder(OopRecorder* r) { _oop_recorder = r; } aoqi@0: aoqi@0: DebugInformationRecorder* debug_info() { return _debug_info; } aoqi@0: void set_debug_info(DebugInformationRecorder* i) { _debug_info = i; } aoqi@0: aoqi@0: Dependencies* dependencies() { return _dependencies; } aoqi@0: void set_dependencies(Dependencies* d) { _dependencies = d; } aoqi@0: aoqi@0: // This is true if the compilation is not going to produce code. aoqi@0: // (It is reasonable to retry failed compilations.) aoqi@0: bool failing() { return _failure_reason != NULL; } aoqi@0: aoqi@0: // Reason this compilation is failing, such as "too many basic blocks". aoqi@0: const char* failure_reason() { return _failure_reason; } aoqi@0: aoqi@0: // Return state of appropriate compilability aoqi@0: int compilable() { return _compilable; } aoqi@0: aoqi@0: const char* retry_message() const { aoqi@0: switch (_compilable) { aoqi@0: case ciEnv::MethodCompilable_not_at_tier: aoqi@0: return "retry at different tier"; aoqi@0: case ciEnv::MethodCompilable_never: aoqi@0: return "not retryable"; aoqi@0: case ciEnv::MethodCompilable: aoqi@0: return NULL; aoqi@0: default: aoqi@0: ShouldNotReachHere(); aoqi@0: return NULL; aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: bool break_at_compile() { return _break_at_compile; } aoqi@0: void set_break_at_compile(bool z) { _break_at_compile = z; } aoqi@0: aoqi@0: // Cache Jvmti state aoqi@0: void cache_jvmti_state(); aoqi@0: bool jvmti_can_hotswap_or_post_breakpoint() const { return _jvmti_can_hotswap_or_post_breakpoint; } aoqi@0: bool jvmti_can_access_local_variables() const { return _jvmti_can_access_local_variables; } aoqi@0: bool jvmti_can_post_on_exceptions() const { return _jvmti_can_post_on_exceptions; } aoqi@0: aoqi@0: // Cache DTrace flags aoqi@0: void cache_dtrace_flags(); aoqi@0: bool dtrace_extended_probes() const { return _dtrace_extended_probes; } aoqi@0: bool dtrace_monitor_probes() const { return _dtrace_monitor_probes; } aoqi@0: bool dtrace_method_probes() const { return _dtrace_method_probes; } aoqi@0: bool dtrace_alloc_probes() const { return _dtrace_alloc_probes; } aoqi@0: aoqi@0: // The compiler task which has created this env. aoqi@0: // May be useful to find out compile_id, comp_level, etc. aoqi@0: CompileTask* task() { return _task; } aoqi@0: // Handy forwards to the task: aoqi@0: int comp_level(); // task()->comp_level() aoqi@0: uint compile_id(); // task()->compile_id() aoqi@0: aoqi@0: // Register the result of a compilation. aoqi@0: void register_method(ciMethod* target, aoqi@0: int entry_bci, aoqi@0: CodeOffsets* offsets, aoqi@0: int orig_pc_offset, aoqi@0: CodeBuffer* code_buffer, aoqi@0: int frame_words, aoqi@0: OopMapSet* oop_map_set, aoqi@0: ExceptionHandlerTable* handler_table, aoqi@0: ImplicitExceptionTable* inc_table, aoqi@0: AbstractCompiler* compiler, aoqi@0: int comp_level, aoqi@0: bool has_unsafe_access, aoqi@0: bool has_wide_vectors, aoqi@0: RTMState rtm_state = NoRTM); aoqi@0: aoqi@0: aoqi@0: // Access to certain well known ciObjects. aoqi@0: #define WK_KLASS_FUNC(name, ignore_s, ignore_o) \ aoqi@0: ciInstanceKlass* name() { \ aoqi@0: return _##name;\ aoqi@0: } aoqi@0: WK_KLASSES_DO(WK_KLASS_FUNC) aoqi@0: #undef WK_KLASS_FUNC aoqi@0: aoqi@0: ciInstance* NullPointerException_instance() { aoqi@0: assert(_NullPointerException_instance != NULL, "initialization problem"); aoqi@0: return _NullPointerException_instance; aoqi@0: } aoqi@0: ciInstance* ArithmeticException_instance() { aoqi@0: assert(_ArithmeticException_instance != NULL, "initialization problem"); aoqi@0: return _ArithmeticException_instance; aoqi@0: } aoqi@0: aoqi@0: // Lazy constructors: aoqi@0: ciInstance* ArrayIndexOutOfBoundsException_instance(); aoqi@0: ciInstance* ArrayStoreException_instance(); aoqi@0: ciInstance* ClassCastException_instance(); aoqi@0: aoqi@0: ciInstance* the_null_string(); aoqi@0: ciInstance* the_min_jint_string(); aoqi@0: aoqi@0: static ciSymbol* unloaded_cisymbol() { aoqi@0: return _unloaded_cisymbol; aoqi@0: } aoqi@0: static ciObjArrayKlass* unloaded_ciobjarrayklass() { aoqi@0: return _unloaded_ciobjarrayklass; aoqi@0: } aoqi@0: static ciInstanceKlass* unloaded_ciinstance_klass() { aoqi@0: return _unloaded_ciinstance_klass; aoqi@0: } aoqi@0: ciInstance* unloaded_ciinstance(); aoqi@0: aoqi@0: ciKlass* find_system_klass(ciSymbol* klass_name); aoqi@0: // Note: To find a class from its name string, use ciSymbol::make, aoqi@0: // but consider adding to vmSymbols.hpp instead. aoqi@0: aoqi@0: // converts the ciKlass* representing the holder of a method into a aoqi@0: // ciInstanceKlass*. This is needed since the holder of a method in aoqi@0: // the bytecodes could be an array type. Basically this converts aoqi@0: // array types into java/lang/Object and other types stay as they are. aoqi@0: static ciInstanceKlass* get_instance_klass_for_declared_method_holder(ciKlass* klass); aoqi@0: aoqi@0: // Return the machine-level offset of o, which must be an element of a. aoqi@0: // This may be used to form constant-loading expressions in lieu of simpler encodings. aoqi@0: int array_element_offset_in_bytes(ciArray* a, ciObject* o); aoqi@0: aoqi@0: // Access to the compile-lifetime allocation arena. aoqi@0: Arena* arena() { return _arena; } aoqi@0: aoqi@0: // What is the current compilation environment? aoqi@0: static ciEnv* current() { return CompilerThread::current()->env(); } aoqi@0: aoqi@0: // Overload with current thread argument aoqi@0: static ciEnv* current(CompilerThread *thread) { return thread->env(); } aoqi@0: aoqi@0: // Per-compiler data. (Used by C2 to publish the Compile* pointer.) aoqi@0: void* compiler_data() { return _compiler_data; } aoqi@0: void set_compiler_data(void* x) { _compiler_data = x; } aoqi@0: aoqi@0: // Notice that a method has been inlined in the current compile; aoqi@0: // used only for statistics. aoqi@0: void notice_inlined_method(ciMethod* method); aoqi@0: aoqi@0: // Total number of bytecodes in inlined methods in this compile aoqi@0: int num_inlined_bytecodes() const; aoqi@0: aoqi@0: // Output stream for logging compilation info. aoqi@0: CompileLog* log() { return _log; } aoqi@0: void set_log(CompileLog* log) { _log = log; } aoqi@0: aoqi@0: // Check for changes to the system dictionary during compilation aoqi@0: bool system_dictionary_modification_counter_changed(); aoqi@0: aoqi@0: void record_failure(const char* reason); aoqi@0: void record_method_not_compilable(const char* reason, bool all_tiers = true); aoqi@0: void record_out_of_memory_failure(); aoqi@0: aoqi@0: // RedefineClasses support aoqi@0: void metadata_do(void f(Metadata*)) { _factory->metadata_do(f); } aoqi@0: aoqi@0: // Dump the compilation replay data for the ciEnv to the stream. aoqi@0: void dump_replay_data(int compile_id); aoqi@0: void dump_inline_data(int compile_id); aoqi@0: void dump_replay_data(outputStream* out); aoqi@0: void dump_replay_data_unsafe(outputStream* out); aoqi@0: void dump_compile_data(outputStream* out); aoqi@0: }; aoqi@0: aoqi@0: #endif // SHARE_VM_CI_CIENV_HPP