vframe_hp.cpp revision 12408:777aaa19c4b1
131183Speter/*
231183Speter * Copyright (c) 1997, 2016, Oracle and/or its affiliates. All rights reserved.
331183Speter * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
431183Speter *
553024Sguido * This code is free software; you can redistribute it and/or modify it
631183Speter * under the terms of the GNU General Public License version 2 only, as
731183Speter * published by the Free Software Foundation.
831183Speter *
931183Speter * This code is distributed in the hope that it will be useful, but WITHOUT
1031183Speter * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
1131183Speter * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
1231183Speter * version 2 for more details (a copy is included in the LICENSE file that
1331183Speter * accompanied this code).
1431183Speter *
1531183Speter * You should have received a copy of the GNU General Public License version
1631183Speter * 2 along with this work; if not, write to the Free Software Foundation,
1731183Speter * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
1831183Speter *
1931183Speter * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2031183Speter * or visit www.oracle.com if you need additional information or have any
2131183Speter * questions.
2231183Speter *
2331183Speter */
2431183Speter
2531183Speter#include "precompiled.hpp"
2631183Speter#include "classfile/javaClasses.inline.hpp"
2731183Speter#include "code/codeCache.hpp"
2831183Speter#include "code/debugInfoRec.hpp"
2931183Speter#include "code/nmethod.hpp"
3031183Speter#include "code/pcDesc.hpp"
3131183Speter#include "code/scopeDesc.hpp"
3231183Speter#include "interpreter/interpreter.hpp"
3331183Speter#include "interpreter/oopMapCache.hpp"
3431183Speter#include "oops/instanceKlass.hpp"
3531183Speter#include "oops/oop.inline.hpp"
3637074Speter#include "runtime/basicLock.hpp"
3731183Speter#include "runtime/handles.inline.hpp"
3831183Speter#include "runtime/monitorChunk.hpp"
3931183Speter#include "runtime/signature.hpp"
4031183Speter#include "runtime/stubRoutines.hpp"
4131183Speter#include "runtime/vframeArray.hpp"
4231183Speter#include "runtime/vframe_hp.hpp"
4331183Speter#ifdef COMPILER2
4431183Speter#include "opto/matcher.hpp"
4531183Speter#endif
4631183Speter
4731183Speter
4831183Speter// ------------- compiledVFrame --------------
4931183Speter
5031183SpeterStackValueCollection* compiledVFrame::locals() const {
5131183Speter  // Natives has no scope
5231183Speter  if (scope() == NULL) return new StackValueCollection(0);
5331183Speter  GrowableArray<ScopeValue*>*  scv_list = scope()->locals();
5431183Speter  if (scv_list == NULL) return new StackValueCollection(0);
5531183Speter
5631183Speter  // scv_list is the list of ScopeValues describing the JVM stack state.
5731183Speter  // There is one scv_list entry for every JVM stack state in use.
5831183Speter  int length = scv_list->length();
59255332Scy  StackValueCollection* result = new StackValueCollection(length);
6031183Speter  // In rare instances set_locals may have occurred in which case
6131183Speter  // there are local values that are not described by the ScopeValue anymore
6231183Speter  GrowableArray<jvmtiDeferredLocalVariable*>* deferred = NULL;
6331183Speter  GrowableArray<jvmtiDeferredLocalVariableSet*>* list = thread()->deferred_locals();
6431183Speter  if (list != NULL ) {
6531183Speter    // In real life this never happens or is typically a single element search
6631183Speter    for (int i = 0; i < list->length(); i++) {
6731183Speter      if (list->at(i)->matches((vframe*)this)) {
6831183Speter        deferred = list->at(i)->locals();
6931183Speter        break;
7031183Speter      }
7131183Speter    }
7231183Speter  }
73
74  for( int i = 0; i < length; i++ ) {
75    result->add( create_stack_value(scv_list->at(i)) );
76  }
77
78  // Replace specified locals with any deferred writes that are present
79  if (deferred != NULL) {
80    for ( int l = 0;  l < deferred->length() ; l ++) {
81      jvmtiDeferredLocalVariable* val = deferred->at(l);
82      switch (val->type()) {
83      case T_BOOLEAN:
84        result->set_int_at(val->index(), val->value().z);
85        break;
86      case T_CHAR:
87        result->set_int_at(val->index(), val->value().c);
88        break;
89      case T_FLOAT:
90        result->set_float_at(val->index(), val->value().f);
91        break;
92      case T_DOUBLE:
93        result->set_double_at(val->index(), val->value().d);
94        break;
95      case T_BYTE:
96        result->set_int_at(val->index(), val->value().b);
97        break;
98      case T_SHORT:
99        result->set_int_at(val->index(), val->value().s);
100        break;
101      case T_INT:
102        result->set_int_at(val->index(), val->value().i);
103        break;
104      case T_LONG:
105        result->set_long_at(val->index(), val->value().j);
106        break;
107      case T_OBJECT:
108        {
109          Handle obj((oop)val->value().l);
110          result->set_obj_at(val->index(), obj);
111        }
112        break;
113      default:
114        ShouldNotReachHere();
115      }
116    }
117  }
118
119  return result;
120}
121
122
123void compiledVFrame::set_locals(StackValueCollection* values) const {
124
125  fatal("Should use update_local for each local update");
126}
127
128void compiledVFrame::update_local(BasicType type, int index, jvalue value) {
129
130#ifdef ASSERT
131
132  assert(fr().is_deoptimized_frame(), "frame must be scheduled for deoptimization");
133#endif /* ASSERT */
134  GrowableArray<jvmtiDeferredLocalVariableSet*>* deferred = thread()->deferred_locals();
135  if (deferred != NULL ) {
136    // See if this vframe has already had locals with deferred writes
137    int f;
138    for ( f = 0 ; f < deferred->length() ; f++ ) {
139      if (deferred->at(f)->matches(this)) {
140        // Matching, vframe now see if the local already had deferred write
141        GrowableArray<jvmtiDeferredLocalVariable*>* locals = deferred->at(f)->locals();
142        int l;
143        for (l = 0 ; l < locals->length() ; l++ ) {
144          if (locals->at(l)->index() == index) {
145            locals->at(l)->set_value(value);
146            return;
147          }
148        }
149        // No matching local already present. Push a new value onto the deferred collection
150        locals->push(new jvmtiDeferredLocalVariable(index, type, value));
151        return;
152      }
153    }
154    // No matching vframe must push a new vframe
155  } else {
156    // No deferred updates pending for this thread.
157    // allocate in C heap
158    deferred =  new(ResourceObj::C_HEAP, mtCompiler) GrowableArray<jvmtiDeferredLocalVariableSet*> (1, true);
159    thread()->set_deferred_locals(deferred);
160  }
161  deferred->push(new jvmtiDeferredLocalVariableSet(method(), bci(), fr().id()));
162  assert(deferred->top()->id() == fr().id(), "Huh? Must match");
163  deferred->top()->set_local_at(index, type, value);
164}
165
166StackValueCollection* compiledVFrame::expressions() const {
167  // Natives has no scope
168  if (scope() == NULL) return new StackValueCollection(0);
169  GrowableArray<ScopeValue*>*  scv_list = scope()->expressions();
170  if (scv_list == NULL) return new StackValueCollection(0);
171
172  // scv_list is the list of ScopeValues describing the JVM stack state.
173  // There is one scv_list entry for every JVM stack state in use.
174  int length = scv_list->length();
175  StackValueCollection* result = new StackValueCollection(length);
176  for( int i = 0; i < length; i++ )
177    result->add( create_stack_value(scv_list->at(i)) );
178
179  return result;
180}
181
182
183// The implementation of the following two methods was factorized into the
184// class StackValue because it is also used from within deoptimization.cpp for
185// rematerialization and relocking of non-escaping objects.
186
187StackValue *compiledVFrame::create_stack_value(ScopeValue *sv) const {
188  return StackValue::create_stack_value(&_fr, register_map(), sv);
189}
190
191BasicLock* compiledVFrame::resolve_monitor_lock(Location location) const {
192  return StackValue::resolve_monitor_lock(&_fr, location);
193}
194
195
196GrowableArray<MonitorInfo*>* compiledVFrame::monitors() const {
197  // Natives has no scope
198  if (scope() == NULL) {
199    CompiledMethod* nm = code();
200    Method* method = nm->method();
201    assert(method->is_native() || nm->is_aot(), "Expect a native method or precompiled method");
202    if (!method->is_synchronized()) {
203      return new GrowableArray<MonitorInfo*>(0);
204    }
205    // This monitor is really only needed for UseBiasedLocking, but
206    // return it in all cases for now as it might be useful for stack
207    // traces and tools as well
208    GrowableArray<MonitorInfo*> *monitors = new GrowableArray<MonitorInfo*>(1);
209    // Casting away const
210    frame& fr = (frame&) _fr;
211    MonitorInfo* info = new MonitorInfo(
212        fr.get_native_receiver(), fr.get_native_monitor(), false, false);
213    monitors->push(info);
214    return monitors;
215  }
216  GrowableArray<MonitorValue*>* monitors = scope()->monitors();
217  if (monitors == NULL) {
218    return new GrowableArray<MonitorInfo*>(0);
219  }
220  GrowableArray<MonitorInfo*>* result = new GrowableArray<MonitorInfo*>(monitors->length());
221  for (int index = 0; index < monitors->length(); index++) {
222    MonitorValue* mv = monitors->at(index);
223    ScopeValue*   ov = mv->owner();
224    StackValue *owner_sv = create_stack_value(ov); // it is an oop
225    if (ov->is_object() && owner_sv->obj_is_scalar_replaced()) { // The owner object was scalar replaced
226      assert(mv->eliminated(), "monitor should be eliminated for scalar replaced object");
227      // Put klass for scalar replaced object.
228      ScopeValue* kv = ((ObjectValue *)ov)->klass();
229      assert(kv->is_constant_oop(), "klass should be oop constant for scalar replaced object");
230      Handle k(((ConstantOopReadValue*)kv)->value()());
231      assert(java_lang_Class::is_instance(k()), "must be");
232      result->push(new MonitorInfo(k(), resolve_monitor_lock(mv->basic_lock()),
233                                   mv->eliminated(), true));
234    } else {
235      result->push(new MonitorInfo(owner_sv->get_obj()(), resolve_monitor_lock(mv->basic_lock()),
236                                   mv->eliminated(), false));
237    }
238  }
239  return result;
240}
241
242
243compiledVFrame::compiledVFrame(const frame* fr, const RegisterMap* reg_map, JavaThread* thread, CompiledMethod* nm)
244: javaVFrame(fr, reg_map, thread) {
245  _scope  = NULL;
246  // Compiled method (native stub or Java code)
247  // native wrappers have no scope data, it is implied
248  if (!nm->is_compiled() || !nm->as_compiled_method()->is_native_method()) {
249      _scope  = nm->scope_desc_at(_fr.pc());
250  }
251}
252
253compiledVFrame::compiledVFrame(const frame* fr, const RegisterMap* reg_map, JavaThread* thread, ScopeDesc* scope)
254: javaVFrame(fr, reg_map, thread) {
255  _scope  = scope;
256  guarantee(_scope != NULL, "scope must be present");
257}
258
259
260bool compiledVFrame::is_top() const {
261  // FIX IT: Remove this when new native stubs are in place
262  if (scope() == NULL) return true;
263  return scope()->is_top();
264}
265
266
267CompiledMethod* compiledVFrame::code() const {
268  return CodeCache::find_compiled(_fr.pc());
269}
270
271
272Method* compiledVFrame::method() const {
273  if (scope() == NULL) {
274    // native nmethods have no scope the method is implied
275    nmethod* nm = code()->as_nmethod();
276    assert(nm->is_native_method(), "must be native");
277    return nm->method();
278  }
279  return scope()->method();
280}
281
282
283int compiledVFrame::bci() const {
284  int raw = raw_bci();
285  return raw == SynchronizationEntryBCI ? 0 : raw;
286}
287
288
289int compiledVFrame::raw_bci() const {
290  if (scope() == NULL) {
291    // native nmethods have no scope the method/bci is implied
292    nmethod* nm = code()->as_nmethod();
293    assert(nm->is_native_method(), "must be native");
294    return 0;
295  }
296  return scope()->bci();
297}
298
299bool compiledVFrame::should_reexecute() const {
300  if (scope() == NULL) {
301    // native nmethods have no scope the method/bci is implied
302    nmethod* nm = code()->as_nmethod();
303    assert(nm->is_native_method(), "must be native");
304    return false;
305  }
306  return scope()->should_reexecute();
307}
308
309vframe* compiledVFrame::sender() const {
310  const frame f = fr();
311  if (scope() == NULL) {
312    // native nmethods have no scope the method/bci is implied
313    nmethod* nm = code()->as_nmethod();
314    assert(nm->is_native_method(), "must be native");
315    return vframe::sender();
316  } else {
317    return scope()->is_top()
318      ? vframe::sender()
319      : new compiledVFrame(&f, register_map(), thread(), scope()->sender());
320  }
321}
322
323jvmtiDeferredLocalVariableSet::jvmtiDeferredLocalVariableSet(Method* method, int bci, intptr_t* id) {
324  _method = method;
325  _bci = bci;
326  _id = id;
327  // Alway will need at least one, must be on C heap
328  _locals = new(ResourceObj::C_HEAP, mtCompiler) GrowableArray<jvmtiDeferredLocalVariable*> (1, true);
329}
330
331jvmtiDeferredLocalVariableSet::~jvmtiDeferredLocalVariableSet() {
332  for (int i = 0; i < _locals->length() ; i++ ) {
333    delete _locals->at(i);
334  }
335  // Free growableArray and c heap for elements
336  delete _locals;
337}
338
339bool jvmtiDeferredLocalVariableSet::matches(vframe* vf) {
340  if (!vf->is_compiled_frame()) return false;
341  compiledVFrame* cvf = (compiledVFrame*)vf;
342  return cvf->fr().id() == id() && cvf->method() == method() && cvf->bci() == bci();
343}
344
345void jvmtiDeferredLocalVariableSet::set_local_at(int idx, BasicType type, jvalue val) {
346  int i;
347  for ( i = 0 ; i < locals()->length() ; i++ ) {
348    if ( locals()->at(i)->index() == idx) {
349      assert(locals()->at(i)->type() == type, "Wrong type");
350      locals()->at(i)->set_value(val);
351      return;
352    }
353  }
354  locals()->push(new jvmtiDeferredLocalVariable(idx, type, val));
355}
356
357void jvmtiDeferredLocalVariableSet::oops_do(OopClosure* f) {
358  // The Method* is on the stack so a live activation keeps it alive
359  // either by mirror in interpreter or code in compiled code.
360  for ( int i = 0; i < locals()->length(); i++ ) {
361    if ( locals()->at(i)->type() == T_OBJECT) {
362      f->do_oop(locals()->at(i)->oop_addr());
363    }
364  }
365}
366
367jvmtiDeferredLocalVariable::jvmtiDeferredLocalVariable(int index, BasicType type, jvalue value) {
368  _index = index;
369  _type = type;
370  _value = value;
371}
372
373
374#ifndef PRODUCT
375void compiledVFrame::verify() const {
376  Unimplemented();
377}
378#endif // PRODUCT
379