aoqi@0: /* aoqi@0: * Copyright (c) 1997, 2012, 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: #include "precompiled.hpp" aoqi@0: #include "code/codeCache.hpp" aoqi@0: #include "code/debugInfoRec.hpp" aoqi@0: #include "code/nmethod.hpp" aoqi@0: #include "code/pcDesc.hpp" aoqi@0: #include "code/scopeDesc.hpp" aoqi@0: #include "interpreter/interpreter.hpp" aoqi@0: #include "interpreter/oopMapCache.hpp" aoqi@0: #include "oops/instanceKlass.hpp" aoqi@0: #include "oops/oop.inline.hpp" aoqi@0: #include "runtime/basicLock.hpp" aoqi@0: #include "runtime/handles.inline.hpp" aoqi@0: #include "runtime/monitorChunk.hpp" aoqi@0: #include "runtime/signature.hpp" aoqi@0: #include "runtime/stubRoutines.hpp" aoqi@0: #include "runtime/vframeArray.hpp" aoqi@0: #include "runtime/vframe_hp.hpp" aoqi@0: #ifdef COMPILER2 aoqi@0: #include "opto/matcher.hpp" aoqi@0: #endif aoqi@0: aoqi@0: aoqi@0: // ------------- compiledVFrame -------------- aoqi@0: aoqi@0: StackValueCollection* compiledVFrame::locals() const { aoqi@0: // Natives has no scope aoqi@0: if (scope() == NULL) return new StackValueCollection(0); aoqi@0: GrowableArray* scv_list = scope()->locals(); aoqi@0: if (scv_list == NULL) return new StackValueCollection(0); aoqi@0: aoqi@0: // scv_list is the list of ScopeValues describing the JVM stack state. aoqi@0: // There is one scv_list entry for every JVM stack state in use. aoqi@0: int length = scv_list->length(); aoqi@0: StackValueCollection* result = new StackValueCollection(length); aoqi@0: // In rare instances set_locals may have occurred in which case aoqi@0: // there are local values that are not described by the ScopeValue anymore aoqi@0: GrowableArray* deferred = NULL; aoqi@0: GrowableArray* list = thread()->deferred_locals(); aoqi@0: if (list != NULL ) { aoqi@0: // In real life this never happens or is typically a single element search aoqi@0: for (int i = 0; i < list->length(); i++) { aoqi@0: if (list->at(i)->matches((vframe*)this)) { aoqi@0: deferred = list->at(i)->locals(); aoqi@0: break; aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: for( int i = 0; i < length; i++ ) { aoqi@0: result->add( create_stack_value(scv_list->at(i)) ); aoqi@0: } aoqi@0: aoqi@0: // Replace specified locals with any deferred writes that are present aoqi@0: if (deferred != NULL) { aoqi@0: for ( int l = 0; l < deferred->length() ; l ++) { aoqi@0: jvmtiDeferredLocalVariable* val = deferred->at(l); aoqi@0: switch (val->type()) { aoqi@0: case T_BOOLEAN: aoqi@0: result->set_int_at(val->index(), val->value().z); aoqi@0: break; aoqi@0: case T_CHAR: aoqi@0: result->set_int_at(val->index(), val->value().c); aoqi@0: break; aoqi@0: case T_FLOAT: aoqi@0: result->set_float_at(val->index(), val->value().f); aoqi@0: break; aoqi@0: case T_DOUBLE: aoqi@0: result->set_double_at(val->index(), val->value().d); aoqi@0: break; aoqi@0: case T_BYTE: aoqi@0: result->set_int_at(val->index(), val->value().b); aoqi@0: break; aoqi@0: case T_SHORT: aoqi@0: result->set_int_at(val->index(), val->value().s); aoqi@0: break; aoqi@0: case T_INT: aoqi@0: result->set_int_at(val->index(), val->value().i); aoqi@0: break; aoqi@0: case T_LONG: aoqi@0: result->set_long_at(val->index(), val->value().j); aoqi@0: break; aoqi@0: case T_OBJECT: aoqi@0: { aoqi@0: Handle obj((oop)val->value().l); aoqi@0: result->set_obj_at(val->index(), obj); aoqi@0: } aoqi@0: break; aoqi@0: default: aoqi@0: ShouldNotReachHere(); aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: return result; aoqi@0: } aoqi@0: aoqi@0: aoqi@0: void compiledVFrame::set_locals(StackValueCollection* values) const { aoqi@0: aoqi@0: fatal("Should use update_local for each local update"); aoqi@0: } aoqi@0: aoqi@0: void compiledVFrame::update_local(BasicType type, int index, jvalue value) { aoqi@0: aoqi@0: #ifdef ASSERT aoqi@0: aoqi@0: assert(fr().is_deoptimized_frame(), "frame must be scheduled for deoptimization"); aoqi@0: #endif /* ASSERT */ aoqi@0: GrowableArray* deferred = thread()->deferred_locals(); aoqi@0: if (deferred != NULL ) { aoqi@0: // See if this vframe has already had locals with deferred writes aoqi@0: int f; aoqi@0: for ( f = 0 ; f < deferred->length() ; f++ ) { aoqi@0: if (deferred->at(f)->matches(this)) { aoqi@0: // Matching, vframe now see if the local already had deferred write aoqi@0: GrowableArray* locals = deferred->at(f)->locals(); aoqi@0: int l; aoqi@0: for (l = 0 ; l < locals->length() ; l++ ) { aoqi@0: if (locals->at(l)->index() == index) { aoqi@0: locals->at(l)->set_value(value); aoqi@0: return; aoqi@0: } aoqi@0: } aoqi@0: // No matching local already present. Push a new value onto the deferred collection aoqi@0: locals->push(new jvmtiDeferredLocalVariable(index, type, value)); aoqi@0: return; aoqi@0: } aoqi@0: } aoqi@0: // No matching vframe must push a new vframe aoqi@0: } else { aoqi@0: // No deferred updates pending for this thread. aoqi@0: // allocate in C heap aoqi@0: deferred = new(ResourceObj::C_HEAP, mtCompiler) GrowableArray (1, true); aoqi@0: thread()->set_deferred_locals(deferred); aoqi@0: } aoqi@0: deferred->push(new jvmtiDeferredLocalVariableSet(method(), bci(), fr().id())); aoqi@0: assert(deferred->top()->id() == fr().id(), "Huh? Must match"); aoqi@0: deferred->top()->set_local_at(index, type, value); aoqi@0: } aoqi@0: aoqi@0: StackValueCollection* compiledVFrame::expressions() const { aoqi@0: // Natives has no scope aoqi@0: if (scope() == NULL) return new StackValueCollection(0); aoqi@0: GrowableArray* scv_list = scope()->expressions(); aoqi@0: if (scv_list == NULL) return new StackValueCollection(0); aoqi@0: aoqi@0: // scv_list is the list of ScopeValues describing the JVM stack state. aoqi@0: // There is one scv_list entry for every JVM stack state in use. aoqi@0: int length = scv_list->length(); aoqi@0: StackValueCollection* result = new StackValueCollection(length); aoqi@0: for( int i = 0; i < length; i++ ) aoqi@0: result->add( create_stack_value(scv_list->at(i)) ); aoqi@0: aoqi@0: return result; aoqi@0: } aoqi@0: aoqi@0: aoqi@0: // The implementation of the following two methods was factorized into the aoqi@0: // class StackValue because it is also used from within deoptimization.cpp for aoqi@0: // rematerialization and relocking of non-escaping objects. aoqi@0: aoqi@0: StackValue *compiledVFrame::create_stack_value(ScopeValue *sv) const { aoqi@0: return StackValue::create_stack_value(&_fr, register_map(), sv); aoqi@0: } aoqi@0: aoqi@0: BasicLock* compiledVFrame::resolve_monitor_lock(Location location) const { aoqi@0: return StackValue::resolve_monitor_lock(&_fr, location); aoqi@0: } aoqi@0: aoqi@0: aoqi@0: GrowableArray* compiledVFrame::monitors() const { aoqi@0: // Natives has no scope aoqi@0: if (scope() == NULL) { aoqi@0: nmethod* nm = code(); aoqi@0: Method* method = nm->method(); aoqi@0: assert(method->is_native(), ""); aoqi@0: if (!method->is_synchronized()) { aoqi@0: return new GrowableArray(0); aoqi@0: } aoqi@0: // This monitor is really only needed for UseBiasedLocking, but aoqi@0: // return it in all cases for now as it might be useful for stack aoqi@0: // traces and tools as well aoqi@0: GrowableArray *monitors = new GrowableArray(1); aoqi@0: // Casting away const aoqi@0: frame& fr = (frame&) _fr; aoqi@0: MonitorInfo* info = new MonitorInfo( aoqi@0: fr.get_native_receiver(), fr.get_native_monitor(), false, false); aoqi@0: monitors->push(info); aoqi@0: return monitors; aoqi@0: } aoqi@0: GrowableArray* monitors = scope()->monitors(); aoqi@0: if (monitors == NULL) { aoqi@0: return new GrowableArray(0); aoqi@0: } aoqi@0: GrowableArray* result = new GrowableArray(monitors->length()); aoqi@0: for (int index = 0; index < monitors->length(); index++) { aoqi@0: MonitorValue* mv = monitors->at(index); aoqi@0: ScopeValue* ov = mv->owner(); aoqi@0: StackValue *owner_sv = create_stack_value(ov); // it is an oop aoqi@0: if (ov->is_object() && owner_sv->obj_is_scalar_replaced()) { // The owner object was scalar replaced aoqi@0: assert(mv->eliminated(), "monitor should be eliminated for scalar replaced object"); aoqi@0: // Put klass for scalar replaced object. aoqi@0: ScopeValue* kv = ((ObjectValue *)ov)->klass(); aoqi@0: assert(kv->is_constant_oop(), "klass should be oop constant for scalar replaced object"); aoqi@0: Handle k(((ConstantOopReadValue*)kv)->value()()); aoqi@0: assert(java_lang_Class::is_instance(k()), "must be"); aoqi@0: result->push(new MonitorInfo(k(), resolve_monitor_lock(mv->basic_lock()), aoqi@0: mv->eliminated(), true)); aoqi@0: } else { aoqi@0: result->push(new MonitorInfo(owner_sv->get_obj()(), resolve_monitor_lock(mv->basic_lock()), aoqi@0: mv->eliminated(), false)); aoqi@0: } aoqi@0: } aoqi@0: return result; aoqi@0: } aoqi@0: aoqi@0: aoqi@0: compiledVFrame::compiledVFrame(const frame* fr, const RegisterMap* reg_map, JavaThread* thread, nmethod* nm) aoqi@0: : javaVFrame(fr, reg_map, thread) { aoqi@0: _scope = NULL; aoqi@0: // Compiled method (native stub or Java code) aoqi@0: // native wrappers have no scope data, it is implied aoqi@0: if (!nm->is_native_method()) { aoqi@0: _scope = nm->scope_desc_at(_fr.pc()); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: compiledVFrame::compiledVFrame(const frame* fr, const RegisterMap* reg_map, JavaThread* thread, ScopeDesc* scope) aoqi@0: : javaVFrame(fr, reg_map, thread) { aoqi@0: _scope = scope; aoqi@0: guarantee(_scope != NULL, "scope must be present"); aoqi@0: } aoqi@0: aoqi@0: aoqi@0: bool compiledVFrame::is_top() const { aoqi@0: // FIX IT: Remove this when new native stubs are in place aoqi@0: if (scope() == NULL) return true; aoqi@0: return scope()->is_top(); aoqi@0: } aoqi@0: aoqi@0: aoqi@0: nmethod* compiledVFrame::code() const { aoqi@0: return CodeCache::find_nmethod(_fr.pc()); aoqi@0: } aoqi@0: aoqi@0: aoqi@0: Method* compiledVFrame::method() const { aoqi@0: if (scope() == NULL) { aoqi@0: // native nmethods have no scope the method is implied aoqi@0: nmethod* nm = code(); aoqi@0: assert(nm->is_native_method(), "must be native"); aoqi@0: return nm->method(); aoqi@0: } aoqi@0: return scope()->method(); aoqi@0: } aoqi@0: aoqi@0: aoqi@0: int compiledVFrame::bci() const { aoqi@0: int raw = raw_bci(); aoqi@0: return raw == SynchronizationEntryBCI ? 0 : raw; aoqi@0: } aoqi@0: aoqi@0: aoqi@0: int compiledVFrame::raw_bci() const { aoqi@0: if (scope() == NULL) { aoqi@0: // native nmethods have no scope the method/bci is implied aoqi@0: nmethod* nm = code(); aoqi@0: assert(nm->is_native_method(), "must be native"); aoqi@0: return 0; aoqi@0: } aoqi@0: return scope()->bci(); aoqi@0: } aoqi@0: aoqi@0: bool compiledVFrame::should_reexecute() const { aoqi@0: if (scope() == NULL) { aoqi@0: // native nmethods have no scope the method/bci is implied aoqi@0: nmethod* nm = code(); aoqi@0: assert(nm->is_native_method(), "must be native"); aoqi@0: return false; aoqi@0: } aoqi@0: return scope()->should_reexecute(); aoqi@0: } aoqi@0: aoqi@0: vframe* compiledVFrame::sender() const { aoqi@0: const frame f = fr(); aoqi@0: if (scope() == NULL) { aoqi@0: // native nmethods have no scope the method/bci is implied aoqi@0: nmethod* nm = code(); aoqi@0: assert(nm->is_native_method(), "must be native"); aoqi@0: return vframe::sender(); aoqi@0: } else { aoqi@0: return scope()->is_top() aoqi@0: ? vframe::sender() aoqi@0: : new compiledVFrame(&f, register_map(), thread(), scope()->sender()); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: jvmtiDeferredLocalVariableSet::jvmtiDeferredLocalVariableSet(Method* method, int bci, intptr_t* id) { aoqi@0: _method = method; aoqi@0: _bci = bci; aoqi@0: _id = id; aoqi@0: // Alway will need at least one, must be on C heap aoqi@0: _locals = new(ResourceObj::C_HEAP, mtCompiler) GrowableArray (1, true); aoqi@0: } aoqi@0: aoqi@0: jvmtiDeferredLocalVariableSet::~jvmtiDeferredLocalVariableSet() { aoqi@0: for (int i = 0; i < _locals->length() ; i++ ) { aoqi@0: delete _locals->at(i); aoqi@0: } aoqi@0: // Free growableArray and c heap for elements aoqi@0: delete _locals; aoqi@0: } aoqi@0: aoqi@0: bool jvmtiDeferredLocalVariableSet::matches(vframe* vf) { aoqi@0: if (!vf->is_compiled_frame()) return false; aoqi@0: compiledVFrame* cvf = (compiledVFrame*)vf; aoqi@0: return cvf->fr().id() == id() && cvf->method() == method() && cvf->bci() == bci(); aoqi@0: } aoqi@0: aoqi@0: void jvmtiDeferredLocalVariableSet::set_local_at(int idx, BasicType type, jvalue val) { aoqi@0: int i; aoqi@0: for ( i = 0 ; i < locals()->length() ; i++ ) { aoqi@0: if ( locals()->at(i)->index() == idx) { aoqi@0: assert(locals()->at(i)->type() == type, "Wrong type"); aoqi@0: locals()->at(i)->set_value(val); aoqi@0: return; aoqi@0: } aoqi@0: } aoqi@0: locals()->push(new jvmtiDeferredLocalVariable(idx, type, val)); aoqi@0: } aoqi@0: aoqi@0: void jvmtiDeferredLocalVariableSet::oops_do(OopClosure* f) { aoqi@0: // The Method* is on the stack so a live activation keeps it alive aoqi@0: // either by mirror in interpreter or code in compiled code. aoqi@0: for ( int i = 0; i < locals()->length(); i++ ) { aoqi@0: if ( locals()->at(i)->type() == T_OBJECT) { aoqi@0: f->do_oop(locals()->at(i)->oop_addr()); aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: jvmtiDeferredLocalVariable::jvmtiDeferredLocalVariable(int index, BasicType type, jvalue value) { aoqi@0: _index = index; aoqi@0: _type = type; aoqi@0: _value = value; aoqi@0: } aoqi@0: aoqi@0: aoqi@0: #ifndef PRODUCT aoqi@0: void compiledVFrame::verify() const { aoqi@0: Unimplemented(); aoqi@0: } aoqi@0: #endif // PRODUCT