comparison src/share/vm/graal/graalEnv.cpp @ 3635:cb1181db8bec

Initial port of ciEnv to graalEnv.
author Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
date Tue, 15 Nov 2011 21:15:26 +0100
parents
children c7d4198a9bce
comparison
equal deleted inserted replaced
3634:076542d505cd 3635:cb1181db8bec
1 /*
2 * Copyright (c) 1999, 2011, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 *
23 */
24
25 #include "precompiled.hpp"
26 #include "graal/graalEnv.hpp"
27 #include "classfile/systemDictionary.hpp"
28 #include "classfile/vmSymbols.hpp"
29 #include "code/scopeDesc.hpp"
30 #include "compiler/compileBroker.hpp"
31 #include "compiler/compileLog.hpp"
32 #include "compiler/compilerOracle.hpp"
33 #include "interpreter/linkResolver.hpp"
34 #include "memory/allocation.inline.hpp"
35 #include "memory/oopFactory.hpp"
36 #include "memory/universe.inline.hpp"
37 #include "oops/methodDataOop.hpp"
38 #include "oops/objArrayKlass.hpp"
39 #include "prims/jvmtiExport.hpp"
40 #include "prims/methodHandleWalk.hpp"
41 #include "runtime/init.hpp"
42 #include "runtime/reflection.hpp"
43 #include "runtime/sharedRuntime.hpp"
44 #include "utilities/dtrace.hpp"
45 #include "c1/c1_Runtime1.hpp"
46
47 // ------------------------------------------------------------------
48 // ciEnv::check_klass_accessiblity
49 //
50 // Note: the logic of this method should mirror the logic of
51 // constantPoolOopDesc::verify_constant_pool_resolve.
52 bool GraalEnv::check_klass_accessibility(klassOop accessing_klass, klassOop resolved_klass) {
53 if (accessing_klass->klass_part()->oop_is_objArray()) {
54 accessing_klass = ((objArrayKlass*)accessing_klass)->bottom_klass();
55 }
56 if (!accessing_klass->klass_part()->oop_is_instance()) {
57 return true;
58 }
59
60 if (resolved_klass->klass_part()->oop_is_objArray()) {
61 // Find the element klass, if this is an array.
62 resolved_klass = objArrayKlass::cast(resolved_klass)->bottom_klass();
63 }
64 if (resolved_klass->klass_part()->oop_is_instance()) {
65 return Reflection::verify_class_access(accessing_klass, resolved_klass, true);
66 }
67 return true;
68 }
69
70 // ------------------------------------------------------------------
71 // ciEnv::get_klass_by_name_impl
72 klassOop GraalEnv::get_klass_by_name_impl(KlassHandle accessing_klass,
73 constantPoolHandle cpool,
74 Symbol* sym,
75 bool require_local) {
76 ASSERT_IN_VM;
77 EXCEPTION_CONTEXT;
78
79 // Now we need to check the SystemDictionary
80 if (sym->byte_at(0) == 'L' &&
81 sym->byte_at(sym->utf8_length()-1) == ';') {
82 // This is a name from a signature. Strip off the trimmings.
83 // Call recursive to keep scope of strippedsym.
84 TempNewSymbol strippedsym = SymbolTable::new_symbol(sym->as_utf8()+1,
85 sym->utf8_length()-2,
86 CHECK_NULL);
87 return get_klass_by_name_impl(accessing_klass, cpool, strippedsym, require_local);
88 }
89
90 Handle loader(THREAD, (oop)NULL);
91 Handle domain(THREAD, (oop)NULL);
92 if (!accessing_klass.is_null()) {
93 loader = Handle(THREAD, accessing_klass->class_loader());
94 domain = Handle(THREAD, accessing_klass->protection_domain());
95 }
96
97 KlassHandle found_klass;
98 {
99 ttyUnlocker ttyul; // release tty lock to avoid ordering problems
100 MutexLocker ml(Compile_lock);
101 klassOop kls;
102 if (!require_local) {
103 kls = SystemDictionary::find_constrained_instance_or_array_klass(sym, loader, CHECK_NULL);
104 } else {
105 kls = SystemDictionary::find_instance_or_array_klass(sym, loader, domain, CHECK_NULL);
106 }
107 found_klass = KlassHandle(THREAD, kls);
108 }
109
110 // If we fail to find an array klass, look again for its element type.
111 // The element type may be available either locally or via constraints.
112 // In either case, if we can find the element type in the system dictionary,
113 // we must build an array type around it. The CI requires array klasses
114 // to be loaded if their element klasses are loaded, except when memory
115 // is exhausted.
116 if (sym->byte_at(0) == '[' &&
117 (sym->byte_at(1) == '[' || sym->byte_at(1) == 'L')) {
118 // We have an unloaded array.
119 // Build it on the fly if the element class exists.
120 TempNewSymbol elem_sym = SymbolTable::new_symbol(sym->as_utf8()+1,
121 sym->utf8_length()-1,
122 CHECK_NULL);
123
124 // Get element ciKlass recursively.
125 KlassHandle elem_klass =
126 get_klass_by_name_impl(accessing_klass,
127 cpool,
128 elem_sym,
129 require_local);
130 if (!elem_klass.is_null()) {
131 // Now make an array for it
132 return elem_klass->array_klass(CHECK_NULL);
133 }
134 }
135
136 if (found_klass.is_null() == NULL && !cpool.is_null() && cpool->has_preresolution()) {
137 // Look inside the constant pool for pre-resolved class entries.
138 for (int i = cpool->length() - 1; i >= 1; i--) {
139 if (cpool->tag_at(i).is_klass()) {
140 klassOop kls = cpool->resolved_klass_at(i);
141 if (Klass::cast(kls)->name() == sym) {
142 return kls;
143 }
144 }
145 }
146 }
147
148 return found_klass();
149 }
150
151 // ------------------------------------------------------------------
152 // ciEnv::get_klass_by_name
153 KlassHandle GraalEnv::get_klass_by_name(KlassHandle accessing_klass,
154 Symbol* klass_name,
155 bool require_local) {
156 return get_klass_by_name_impl(accessing_klass,
157 constantPoolHandle(),
158 klass_name,
159 require_local);
160 }
161
162 // ------------------------------------------------------------------
163 // ciEnv::get_klass_by_index_impl
164 //
165 // Implementation of get_klass_by_index.
166 klassOop GraalEnv::get_klass_by_index_impl(constantPoolHandle cpool,
167 int index,
168 bool& is_accessible,
169 KlassHandle accessor) {
170 EXCEPTION_CONTEXT;
171 KlassHandle klass (THREAD, constantPoolOopDesc::klass_at_if_loaded(cpool, index));
172 Symbol* klass_name = NULL;
173 if (klass.is_null()) {
174 // The klass has not been inserted into the constant pool.
175 // Try to look it up by name.
176 {
177 // We have to lock the cpool to keep the oop from being resolved
178 // while we are accessing it.
179 ObjectLocker ol(cpool, THREAD);
180
181 constantTag tag = cpool->tag_at(index);
182 if (tag.is_klass()) {
183 // The klass has been inserted into the constant pool
184 // very recently.
185 klass = KlassHandle(THREAD, cpool->resolved_klass_at(index));
186 } else if (tag.is_symbol()) {
187 klass_name = cpool->symbol_at(index);
188 } else {
189 assert(cpool->tag_at(index).is_unresolved_klass(), "wrong tag");
190 klass_name = cpool->unresolved_klass_at(index);
191 }
192 }
193 }
194
195 if (klass.is_null()) {
196 // Not found in constant pool. Use the name to do the lookup.
197 KlassHandle k = get_klass_by_name_impl(accessor,
198 cpool,
199 klass_name,
200 false);
201 // Calculate accessibility the hard way.
202 if (k.is_null()) {
203 is_accessible = false;
204 } else if (k->class_loader() != accessor->class_loader() &&
205 get_klass_by_name_impl(accessor, cpool, k->name(), true) == NULL) {
206 // Loaded only remotely. Not linked yet.
207 is_accessible = false;
208 } else {
209 // Linked locally, and we must also check public/private, etc.
210 is_accessible = check_klass_accessibility(accessor(), k());
211 }
212 return k();
213 }
214
215 // Check for prior unloaded klass. The SystemDictionary's answers
216 // can vary over time but the compiler needs consistency.
217 Symbol* name = klass()->klass_part()->name();
218
219 // It is known to be accessible, since it was found in the constant pool.
220 is_accessible = true;
221 return klass();
222 }
223
224 // ------------------------------------------------------------------
225 // ciEnv::get_klass_by_index
226 //
227 // Get a klass from the constant pool.
228 KlassHandle GraalEnv::get_klass_by_index(constantPoolHandle cpool,
229 int index,
230 bool& is_accessible,
231 KlassHandle accessor) {
232 return get_klass_by_index_impl(cpool, index, is_accessible, accessor);
233 }
234
235 // ------------------------------------------------------------------
236 // ciEnv::get_field_by_index_impl
237 //
238 // Implementation of get_field_by_index.
239 //
240 // Implementation note: the results of field lookups are cached
241 // in the accessor klass.
242 void GraalEnv::get_field_by_index_impl(instanceKlassHandle klass, fieldDescriptor& field_desc,
243 int index) {
244 EXCEPTION_CONTEXT;
245
246 assert(klass->get_instanceKlass()->is_linked(), "must be linked before using its constan-pool");
247
248 constantPoolHandle cpool(thread, klass->constants());
249
250 // Get the field's name, signature, and type.
251 Symbol* name = cpool->name_ref_at(index);
252
253 int nt_index = cpool->name_and_type_ref_index_at(index);
254 int sig_index = cpool->signature_ref_index_at(nt_index);
255 Symbol* signature = cpool->symbol_at(sig_index);
256
257 // Get the field's declared holder.
258 //
259 // Note: we actually create a ciInstanceKlass for this klass,
260 // even though we may not need to.
261 int holder_index = cpool->klass_ref_index_at(index);
262 bool holder_is_accessible;
263 KlassHandle declared_holder = get_klass_by_index(cpool, holder_index,
264 holder_is_accessible,
265 klass);
266
267 // The declared holder of this field may not have been loaded.
268 // Bail out with partial field information.
269 if (!holder_is_accessible) {
270 return;
271 }
272
273
274 // Perform the field lookup.
275 klassOop canonical_holder =
276 instanceKlass::cast(declared_holder())->find_field(name, signature, &field_desc);
277 if (canonical_holder == NULL) {
278 return;
279 }
280
281 assert(canonical_holder == field_desc.field_holder(), "just checking");
282 }
283
284 // ------------------------------------------------------------------
285 // ciEnv::get_field_by_index
286 //
287 // Get a field by index from a klass's constant pool.
288 void GraalEnv::get_field_by_index(instanceKlassHandle accessor, fieldDescriptor& fd,
289 int index) {
290 return get_field_by_index_impl(accessor, fd, index);
291 }
292
293 // ------------------------------------------------------------------
294 // ciEnv::lookup_method
295 //
296 // Perform an appropriate method lookup based on accessor, holder,
297 // name, signature, and bytecode.
298 methodOop GraalEnv::lookup_method(instanceKlass* accessor,
299 instanceKlass* holder,
300 Symbol* name,
301 Symbol* sig,
302 Bytecodes::Code bc) {
303 EXCEPTION_CONTEXT;
304 KlassHandle h_accessor(THREAD, accessor);
305 KlassHandle h_holder(THREAD, holder);
306 LinkResolver::check_klass_accessability(h_accessor, h_holder, KILL_COMPILE_ON_FATAL_(NULL));
307 methodHandle dest_method;
308 switch (bc) {
309 case Bytecodes::_invokestatic:
310 dest_method =
311 LinkResolver::resolve_static_call_or_null(h_holder, name, sig, h_accessor);
312 break;
313 case Bytecodes::_invokespecial:
314 dest_method =
315 LinkResolver::resolve_special_call_or_null(h_holder, name, sig, h_accessor);
316 break;
317 case Bytecodes::_invokeinterface:
318 dest_method =
319 LinkResolver::linktime_resolve_interface_method_or_null(h_holder, name, sig,
320 h_accessor, true);
321 break;
322 case Bytecodes::_invokevirtual:
323 dest_method =
324 LinkResolver::linktime_resolve_virtual_method_or_null(h_holder, name, sig,
325 h_accessor, true);
326 break;
327 default: ShouldNotReachHere();
328 }
329
330 return dest_method();
331 }
332
333
334 // ------------------------------------------------------------------
335 // ciEnv::get_method_by_index_impl
336 methodHandle GraalEnv::get_method_by_index_impl(constantPoolHandle cpool,
337 int index, Bytecodes::Code bc,
338 instanceKlass* accessor) {
339 int holder_index = cpool->klass_ref_index_at(index);
340 bool holder_is_accessible;
341 KlassHandle holder = get_klass_by_index_impl(cpool, holder_index, holder_is_accessible, KlassHandle(Thread::current(), accessor));
342 instanceKlass* declared_holder = get_instance_klass_for_declared_method_holder(holder);
343
344 // Get the method's name and signature.
345 Symbol* name_sym = cpool->name_ref_at(index);
346 Symbol* sig_sym = cpool->signature_ref_at(index);
347
348 if (holder_is_accessible) { // Our declared holder is loaded.
349 instanceKlass* lookup = declared_holder;
350 methodOop m = lookup_method(accessor, lookup, name_sym, sig_sym, bc);
351 if (m != NULL &&
352 (bc == Bytecodes::_invokestatic
353 ? instanceKlass::cast(m->method_holder())->is_not_initialized()
354 : !instanceKlass::cast(m->method_holder())->is_loaded())) {
355 m = NULL;
356 }
357 if (m != NULL) {
358 // We found the method.
359 return m;
360 }
361 }
362
363 // Either the declared holder was not loaded, or the method could
364 // not be found. Create a dummy ciMethod to represent the failed
365 // lookup.
366
367 return NULL;
368 }
369
370 // ------------------------------------------------------------------
371 // ciEnv::get_instance_klass_for_declared_method_holder
372 instanceKlass* GraalEnv::get_instance_klass_for_declared_method_holder(KlassHandle method_holder) {
373 // For the case of <array>.clone(), the method holder can be a ciArrayKlass
374 // instead of a ciInstanceKlass. For that case simply pretend that the
375 // declared holder is Object.clone since that's where the call will bottom out.
376 // A more correct fix would trickle out through many interfaces in CI,
377 // requiring ciInstanceKlass* to become ciKlass* and many more places would
378 // require checks to make sure the expected type was found. Given that this
379 // only occurs for clone() the more extensive fix seems like overkill so
380 // instead we simply smear the array type into Object.
381 if (method_holder->oop_is_instance()) {
382 return instanceKlass::cast(method_holder());
383 } else if (method_holder->oop_is_array()) {
384 return instanceKlass::cast(SystemDictionary::Object_klass());
385 } else {
386 ShouldNotReachHere();
387 }
388 return NULL;
389 }
390
391
392 // ------------------------------------------------------------------
393 // ciEnv::get_method_by_index
394 methodHandle GraalEnv::get_method_by_index(constantPoolHandle cpool,
395 int index, Bytecodes::Code bc,
396 instanceKlass* accessor) {
397 assert(bc != Bytecodes::_invokedynamic, "invokedynamic not yet supported");
398 return get_method_by_index_impl(cpool, index, bc, accessor);
399 }
400
401 // ------------------------------------------------------------------
402 // ciEnv::check_for_system_dictionary_modification
403 // Check for changes to the system dictionary during compilation
404 // class loads, evolution, breakpoints
405 bool GraalEnv::check_for_system_dictionary_modification(Dependencies* dependencies) {
406 // Dependencies must be checked when the system dictionary changes.
407 // If logging is enabled all violated dependences will be recorded in
408 // the log. In debug mode check dependencies even if the system
409 // dictionary hasn't changed to verify that no invalid dependencies
410 // were inserted. Any violated dependences in this case are dumped to
411 // the tty.
412
413 // TODO (tw): Always check dependency for now.
414 //bool counter_changed = system_dictionary_modification_counter_changed();
415 //bool test_deps = counter_changed;
416 //DEBUG_ONLY(test_deps = true);
417 //if (!test_deps) return;
418
419 bool print_failures = false;
420 DEBUG_ONLY(print_failures = !counter_changed);
421
422 bool keep_going = (print_failures || xtty != NULL);
423
424 int violated = 0;
425
426 for (Dependencies::DepStream deps(dependencies); deps.next(); ) {
427 klassOop witness = deps.check_dependency();
428 if (witness != NULL) {
429 ++violated;
430 if (print_failures) deps.print_dependency(witness, /*verbose=*/ true);
431 // If there's no log and we're not sanity-checking, we're done.
432 if (!keep_going) break;
433 }
434 }
435
436 return violated == 0;
437 }
438
439 // ------------------------------------------------------------------
440 // ciEnv::register_method
441 nmethod* GraalEnv::register_method(methodHandle method,
442 int entry_bci,
443 CodeOffsets* offsets,
444 int orig_pc_offset,
445 CodeBuffer* code_buffer,
446 int frame_words,
447 OopMapSet* oop_map_set,
448 ExceptionHandlerTable* handler_table,
449 ImplicitExceptionTable* inc_table,
450 AbstractCompiler* compiler,
451 DebugInformationRecorder* debug_info,
452 Dependencies* dependencies,
453 CompileTask* task,
454 int compile_id,
455 bool has_debug_info,
456 bool has_unsafe_access,
457 bool install_code) {
458 VM_ENTRY_MARK;
459 nmethod* nm = NULL;
460 int comp_level = CompLevel_simple;
461 {
462 // To prevent compile queue updates.
463 MutexLocker locker(MethodCompileQueue_lock, THREAD);
464
465 // Prevent SystemDictionary::add_to_hierarchy from running
466 // and invalidating our dependencies until we install this method.
467 MutexLocker ml(Compile_lock);
468
469 // Encode the dependencies now, so we can check them right away.
470 dependencies->encode_content_bytes();
471
472 // Check for {class loads, evolution, breakpoints} during compilation
473 if (!check_for_system_dictionary_modification(dependencies)) {
474 // While not a true deoptimization, it is a preemptive decompile.
475 methodDataOop mdo = method()->method_data();
476 if (mdo != NULL) {
477 mdo->inc_decompile_count();
478 }
479
480 // All buffers in the CodeBuffer are allocated in the CodeCache.
481 // If the code buffer is created on each compile attempt
482 // as in C2, then it must be freed.
483 code_buffer->free_blob();
484 return NULL;
485 }
486
487 assert(offsets->value(CodeOffsets::Deopt) != -1, "must have deopt entry");
488 assert(offsets->value(CodeOffsets::Exceptions) != -1, "must have exception entry");
489
490 nm = nmethod::new_nmethod(method,
491 compile_id,
492 entry_bci,
493 offsets,
494 orig_pc_offset,
495 debug_info, dependencies, code_buffer,
496 frame_words, oop_map_set,
497 handler_table, inc_table,
498 compiler, comp_level);
499
500 // Free codeBlobs
501 code_buffer->free_blob();
502
503 // stress test 6243940 by immediately making the method
504 // non-entrant behind the system's back. This has serious
505 // side effects on the code cache and is not meant for
506 // general stress testing
507 if (nm != NULL && StressNonEntrant) {
508 MutexLockerEx pl(Patching_lock, Mutex::_no_safepoint_check_flag);
509 NativeJump::patch_verified_entry(nm->entry_point(), nm->verified_entry_point(),
510 SharedRuntime::get_handle_wrong_method_stub());
511 }
512
513 if (nm == NULL) {
514 // The CodeCache is full. Print out warning and disable compilation.
515 {
516 MutexUnlocker ml(Compile_lock);
517 MutexUnlocker locker(MethodCompileQueue_lock);
518 CompileBroker::handle_full_code_cache();
519 }
520 } else {
521 NOT_PRODUCT(nm->set_has_debug_info(has_debug_info); )
522 nm->set_has_unsafe_access(has_unsafe_access);
523
524 // Record successful registration.
525 // (Put nm into the task handle *before* publishing to the Java heap.)
526 if (task != NULL) task->set_code(nm);
527
528 if (install_code) {
529 if (entry_bci == InvocationEntryBci) {
530 if (TieredCompilation) {
531 // If there is an old version we're done with it
532 nmethod* old = method->code();
533 if (TraceMethodReplacement && old != NULL) {
534 ResourceMark rm;
535 char *method_name = method->name_and_sig_as_C_string();
536 tty->print_cr("Replacing method %s", method_name);
537 }
538 if (old != NULL ) {
539 old->make_not_entrant();
540 }
541 }
542 if (TraceNMethodInstalls ) {
543 ResourceMark rm;
544 char *method_name = method->name_and_sig_as_C_string();
545 ttyLocker ttyl;
546 tty->print_cr("Installing method (%d) %s ",
547 comp_level,
548 method_name);
549 }
550 // Allow the code to be executed
551 method->set_code(method, nm);
552 } else {
553 if (TraceNMethodInstalls ) {
554 ResourceMark rm;
555 char *method_name = method->name_and_sig_as_C_string();
556 ttyLocker ttyl;
557 tty->print_cr("Installing osr method (%d) %s @ %d",
558 comp_level,
559 method_name,
560 entry_bci);
561 }
562 instanceKlass::cast(method->method_holder())->add_osr_nmethod(nm);
563
564 }
565 }
566 }
567 }
568 // JVMTI -- compiled method notification (must be done outside lock)
569 if (nm != NULL) {
570 nm->post_compiled_method_load_event();
571 }
572
573 return nm;
574 }