ciEnv.hpp revision 0:a61af66fc99e
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
25class CompileTask;
26
27// ciEnv
28//
29// This class is the top level broker for requests from the compiler
30// to the VM.
31class ciEnv : StackObj {
32  CI_PACKAGE_ACCESS_TO
33
34  friend class CompileBroker;
35  friend class Dependencies;  // for get_object, during logging
36
37private:
38  Arena*           _arena;       // Alias for _ciEnv_arena except in init_shared_objects()
39  Arena            _ciEnv_arena;
40  int              _system_dictionary_modification_counter;
41  ciObjectFactory* _factory;
42  OopRecorder*     _oop_recorder;
43  DebugInformationRecorder* _debug_info;
44  Dependencies*    _dependencies;
45  const char*      _failure_reason;
46  int              _compilable;
47  bool             _break_at_compile;
48  int              _num_inlined_bytecodes;
49  CompileTask*     _task;           // faster access to CompilerThread::task
50  CompileLog*      _log;            // faster access to CompilerThread::log
51  void*            _compiler_data;  // compiler-specific stuff, if any
52
53  char* _name_buffer;
54  int   _name_buffer_len;
55
56  // Distinguished instances of certain ciObjects..
57  static ciObject*              _null_object_instance;
58  static ciMethodKlass*         _method_klass_instance;
59  static ciSymbolKlass*         _symbol_klass_instance;
60  static ciKlassKlass*          _klass_klass_instance;
61  static ciInstanceKlassKlass*  _instance_klass_klass_instance;
62  static ciTypeArrayKlassKlass* _type_array_klass_klass_instance;
63  static ciObjArrayKlassKlass*  _obj_array_klass_klass_instance;
64
65  static ciInstanceKlass* _ArrayStoreException;
66  static ciInstanceKlass* _Class;
67  static ciInstanceKlass* _ClassCastException;
68  static ciInstanceKlass* _Object;
69  static ciInstanceKlass* _Throwable;
70  static ciInstanceKlass* _Thread;
71  static ciInstanceKlass* _OutOfMemoryError;
72  static ciInstanceKlass* _String;
73
74  static ciSymbol*        _unloaded_cisymbol;
75  static ciInstanceKlass* _unloaded_ciinstance_klass;
76  static ciObjArrayKlass* _unloaded_ciobjarrayklass;
77
78  static jobject _ArrayIndexOutOfBoundsException_handle;
79  static jobject _ArrayStoreException_handle;
80  static jobject _ClassCastException_handle;
81
82  ciInstance* _NullPointerException_instance;
83  ciInstance* _ArithmeticException_instance;
84  ciInstance* _ArrayIndexOutOfBoundsException_instance;
85  ciInstance* _ArrayStoreException_instance;
86  ciInstance* _ClassCastException_instance;
87
88  // Look up a klass by name from a particular class loader (the accessor's).
89  // If require_local, result must be defined in that class loader, or NULL.
90  // If !require_local, a result from remote class loader may be reported,
91  // if sufficient class loader constraints exist such that initiating
92  // a class loading request from the given loader is bound to return
93  // the class defined in the remote loader (or throw an error).
94  //
95  // Return an unloaded klass if !require_local and no class at all is found.
96  //
97  // The CI treats a klass as loaded if it is consistently defined in
98  // another loader, even if it hasn't yet been loaded in all loaders
99  // that could potentially see it via delegation.
100  ciKlass* get_klass_by_name(ciKlass* accessing_klass,
101                             ciSymbol* klass_name,
102                             bool require_local);
103
104  // Constant pool access.
105  ciKlass*   get_klass_by_index(ciInstanceKlass* loading_klass,
106                                int klass_index,
107                                bool& is_accessible);
108  ciConstant get_constant_by_index(ciInstanceKlass* loading_klass,
109                                   int constant_index);
110  bool       is_unresolved_string(ciInstanceKlass* loading_klass,
111                                   int constant_index) const;
112  bool       is_unresolved_klass(ciInstanceKlass* loading_klass,
113                                   int constant_index) const;
114  ciField*   get_field_by_index(ciInstanceKlass* loading_klass,
115                                int field_index);
116  ciMethod*  get_method_by_index(ciInstanceKlass* loading_klass,
117                                 int method_index, Bytecodes::Code bc);
118
119  // Implementation methods for loading and constant pool access.
120  ciKlass* get_klass_by_name_impl(ciKlass* accessing_klass,
121                                  ciSymbol* klass_name,
122                                  bool require_local);
123  ciKlass*   get_klass_by_index_impl(ciInstanceKlass* loading_klass,
124                                     int klass_index,
125                                     bool& is_accessible);
126  ciConstant get_constant_by_index_impl(ciInstanceKlass* loading_klass,
127                                        int constant_index);
128  bool       is_unresolved_string_impl (instanceKlass* loading_klass,
129                                        int constant_index) const;
130  bool       is_unresolved_klass_impl (instanceKlass* loading_klass,
131                                        int constant_index) const;
132  ciField*   get_field_by_index_impl(ciInstanceKlass* loading_klass,
133                                     int field_index);
134  ciMethod*  get_method_by_index_impl(ciInstanceKlass* loading_klass,
135                                      int method_index, Bytecodes::Code bc);
136
137  // Helper methods
138  bool       check_klass_accessibility(ciKlass* accessing_klass,
139                                      klassOop resolved_klassOop);
140  methodOop  lookup_method(instanceKlass*  accessor,
141                           instanceKlass*  holder,
142                           symbolOop       name,
143                           symbolOop       sig,
144                           Bytecodes::Code bc);
145
146  // Get a ciObject from the object factory.  Ensures uniqueness
147  // of ciObjects.
148  ciObject* get_object(oop o) {
149    if (o == NULL) {
150      return _null_object_instance;
151    } else {
152      return _factory->get(o);
153    }
154  }
155
156  ciMethod* get_method_from_handle(jobject method);
157
158  ciInstance* get_or_create_exception(jobject& handle, symbolHandle name);
159
160  // Get a ciMethod representing either an unfound method or
161  // a method with an unloaded holder.  Ensures uniqueness of
162  // the result.
163  ciMethod* get_unloaded_method(ciInstanceKlass* holder,
164                                ciSymbol*        name,
165                                ciSymbol*        signature) {
166    return _factory->get_unloaded_method(holder, name, signature);
167  }
168
169  // Get a ciKlass representing an unloaded klass.
170  // Ensures uniqueness of the result.
171  ciKlass* get_unloaded_klass(ciKlass* accessing_klass,
172                              ciSymbol* name) {
173    return _factory->get_unloaded_klass(accessing_klass, name, true);
174  }
175
176  // See if we already have an unloaded klass for the given name
177  // or return NULL if not.
178  ciKlass *check_get_unloaded_klass(ciKlass* accessing_klass, ciSymbol* name) {
179    return _factory->get_unloaded_klass(accessing_klass, name, false);
180  }
181
182  // Get a ciReturnAddress corresponding to the given bci.
183  // Ensures uniqueness of the result.
184  ciReturnAddress* get_return_address(int bci) {
185    return _factory->get_return_address(bci);
186  }
187
188  // Get a ciMethodData representing the methodData for a method
189  // with none.
190  ciMethodData* get_empty_methodData() {
191    return _factory->get_empty_methodData();
192  }
193
194  // General utility : get a buffer of some required length.
195  // Used in symbol creation.
196  char* name_buffer(int req_len);
197
198  // Is this thread currently in the VM state?
199  static bool is_in_vm();
200
201  // Helper routine for determining the validity of a compilation
202  // with respect to concurrent class loading.
203  void check_for_system_dictionary_modification(ciMethod* target);
204
205public:
206  enum {
207    MethodCompilable,
208    MethodCompilable_not_at_tier,
209    MethodCompilable_never
210  };
211
212  ciEnv(CompileTask* task, int system_dictionary_modification_counter);
213  // Used only during initialization of the ci
214  ciEnv(Arena* arena);
215  ~ciEnv();
216
217  OopRecorder* oop_recorder() { return _oop_recorder; }
218  void set_oop_recorder(OopRecorder* r) { _oop_recorder = r; }
219
220  DebugInformationRecorder* debug_info() { return _debug_info; }
221  void set_debug_info(DebugInformationRecorder* i) { _debug_info = i; }
222
223  Dependencies* dependencies() { return _dependencies; }
224  void set_dependencies(Dependencies* d) { _dependencies = d; }
225
226  // This is true if the compilation is not going to produce code.
227  // (It is reasonable to retry failed compilations.)
228  bool failing() { return _failure_reason != NULL; }
229
230  // Reason this compilation is failing, such as "too many basic blocks".
231  const char* failure_reason() { return _failure_reason; }
232
233  // Return state of appropriate compilability
234  int compilable() { return _compilable; }
235
236  bool break_at_compile() { return _break_at_compile; }
237  void set_break_at_compile(bool z) { _break_at_compile = z; }
238
239  // The compiler task which has created this env.
240  // May be useful to find out compile_id, comp_level, etc.
241  CompileTask* task() { return _task; }
242  // Handy forwards to the task:
243  int comp_level();   // task()->comp_level()
244  uint compile_id();  // task()->compile_id()
245
246  // Register the result of a compilation.
247  void register_method(ciMethod*                 target,
248                       int                       entry_bci,
249                       CodeOffsets*              offsets,
250                       int                       orig_pc_offset,
251                       CodeBuffer*               code_buffer,
252                       int                       frame_words,
253                       OopMapSet*                oop_map_set,
254                       ExceptionHandlerTable*    handler_table,
255                       ImplicitExceptionTable*   inc_table,
256                       AbstractCompiler*         compiler,
257                       int                       comp_level,
258                       bool                      has_debug_info = true,
259                       bool                      has_unsafe_access = false);
260
261
262  // Access to certain well known ciObjects.
263  ciInstanceKlass* ArrayStoreException_klass() {
264    return _ArrayStoreException;
265  }
266  ciInstanceKlass* Class_klass() {
267    return _Class;
268  }
269  ciInstanceKlass* ClassCastException_klass() {
270    return _ClassCastException;
271  }
272  ciInstanceKlass* Object_klass() {
273    return _Object;
274  }
275  ciInstanceKlass* Throwable_klass() {
276    return _Throwable;
277  }
278  ciInstanceKlass* Thread_klass() {
279    return _Thread;
280  }
281  ciInstanceKlass* OutOfMemoryError_klass() {
282    return _OutOfMemoryError;
283  }
284  ciInstanceKlass* String_klass() {
285    return _String;
286  }
287  ciInstance* NullPointerException_instance() {
288    assert(_NullPointerException_instance != NULL, "initialization problem");
289    return _NullPointerException_instance;
290  }
291  ciInstance* ArithmeticException_instance() {
292    assert(_ArithmeticException_instance != NULL, "initialization problem");
293    return _ArithmeticException_instance;
294  }
295
296  // Lazy constructors:
297  ciInstance* ArrayIndexOutOfBoundsException_instance();
298  ciInstance* ArrayStoreException_instance();
299  ciInstance* ClassCastException_instance();
300
301  static ciSymbol* unloaded_cisymbol() {
302    return _unloaded_cisymbol;
303  }
304  static ciObjArrayKlass* unloaded_ciobjarrayklass() {
305    return _unloaded_ciobjarrayklass;
306  }
307  static ciInstanceKlass* unloaded_ciinstance_klass() {
308    return _unloaded_ciinstance_klass;
309  }
310
311  ciKlass*  find_system_klass(ciSymbol* klass_name);
312  // Note:  To find a class from its name string, use ciSymbol::make,
313  // but consider adding to vmSymbols.hpp instead.
314
315  // Use this to make a holder for non-perm compile time constants.
316  // The resulting array is guaranteed to satisfy "has_encoding".
317  ciArray*  make_array(GrowableArray<ciObject*>* objects);
318
319  // converts the ciKlass* representing the holder of a method into a
320  // ciInstanceKlass*.  This is needed since the holder of a method in
321  // the bytecodes could be an array type.  Basically this converts
322  // array types into java/lang/Object and other types stay as they are.
323  static ciInstanceKlass* get_instance_klass_for_declared_method_holder(ciKlass* klass);
324
325  // Return the machine-level offset of o, which must be an element of a.
326  // This may be used to form constant-loading expressions in lieu of simpler encodings.
327  int       array_element_offset_in_bytes(ciArray* a, ciObject* o);
328
329  // Access to the compile-lifetime allocation arena.
330  Arena*    arena() { return _arena; }
331
332  // What is the current compilation environment?
333  static ciEnv* current() { return CompilerThread::current()->env(); }
334
335  // Overload with current thread argument
336  static ciEnv* current(CompilerThread *thread) { return thread->env(); }
337
338  // Per-compiler data.  (Used by C2 to publish the Compile* pointer.)
339  void* compiler_data() { return _compiler_data; }
340  void set_compiler_data(void* x) { _compiler_data = x; }
341
342  // Notice that a method has been inlined in the current compile;
343  // used only for statistics.
344  void notice_inlined_method(ciMethod* method);
345
346  // Total number of bytecodes in inlined methods in this compile
347  int num_inlined_bytecodes() const;
348
349  // Output stream for logging compilation info.
350  CompileLog* log() { return _log; }
351  void set_log(CompileLog* log) { _log = log; }
352
353  // Check for changes to the system dictionary during compilation
354  bool system_dictionary_modification_counter_changed();
355
356  void record_failure(const char* reason);
357  void record_method_not_compilable(const char* reason, bool all_tiers = true);
358  void record_out_of_memory_failure();
359};
360