1/*
2 * Copyright (c) 1997, 2016, 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 "classfile/javaClasses.inline.hpp"
27#include "code/codeCache.hpp"
28#include "code/debugInfoRec.hpp"
29#include "code/nmethod.hpp"
30#include "code/pcDesc.hpp"
31#include "code/scopeDesc.hpp"
32#include "interpreter/interpreter.hpp"
33#include "interpreter/oopMapCache.hpp"
34#include "oops/instanceKlass.hpp"
35#include "oops/oop.inline.hpp"
36#include "runtime/basicLock.hpp"
37#include "runtime/handles.inline.hpp"
38#include "runtime/monitorChunk.hpp"
39#include "runtime/signature.hpp"
40#include "runtime/stubRoutines.hpp"
41#include "runtime/vframeArray.hpp"
42#include "runtime/vframe_hp.hpp"
43#ifdef COMPILER2
44#include "opto/matcher.hpp"
45#endif
46
47
48// ------------- compiledVFrame --------------
49
50StackValueCollection* compiledVFrame::locals() const {
51  // Natives has no scope
52  if (scope() == NULL) return new StackValueCollection(0);
53  GrowableArray<ScopeValue*>*  scv_list = scope()->locals();
54  if (scv_list == NULL) return new StackValueCollection(0);
55
56  // scv_list is the list of ScopeValues describing the JVM stack state.
57  // There is one scv_list entry for every JVM stack state in use.
58  int length = scv_list->length();
59  StackValueCollection* result = new StackValueCollection(length);
60  // In rare instances set_locals may have occurred in which case
61  // there are local values that are not described by the ScopeValue anymore
62  GrowableArray<jvmtiDeferredLocalVariable*>* deferred = NULL;
63  GrowableArray<jvmtiDeferredLocalVariableSet*>* list = thread()->deferred_locals();
64  if (list != NULL ) {
65    // In real life this never happens or is typically a single element search
66    for (int i = 0; i < list->length(); i++) {
67      if (list->at(i)->matches((vframe*)this)) {
68        deferred = list->at(i)->locals();
69        break;
70      }
71    }
72  }
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(), vframe_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  _vframe_id = 0;
247  // Compiled method (native stub or Java code)
248  // native wrappers have no scope data, it is implied
249  if (!nm->is_compiled() || !nm->as_compiled_method()->is_native_method()) {
250      _scope  = nm->scope_desc_at(_fr.pc());
251  }
252}
253
254compiledVFrame::compiledVFrame(const frame* fr, const RegisterMap* reg_map, JavaThread* thread, ScopeDesc* scope, int vframe_id)
255: javaVFrame(fr, reg_map, thread) {
256  _scope  = scope;
257  _vframe_id = vframe_id;
258  guarantee(_scope != NULL, "scope must be present");
259}
260
261
262bool compiledVFrame::is_top() const {
263  // FIX IT: Remove this when new native stubs are in place
264  if (scope() == NULL) return true;
265  return scope()->is_top();
266}
267
268
269CompiledMethod* compiledVFrame::code() const {
270  return CodeCache::find_compiled(_fr.pc());
271}
272
273
274Method* compiledVFrame::method() const {
275  if (scope() == NULL) {
276    // native nmethods have no scope the method is implied
277    nmethod* nm = code()->as_nmethod();
278    assert(nm->is_native_method(), "must be native");
279    return nm->method();
280  }
281  return scope()->method();
282}
283
284
285int compiledVFrame::bci() const {
286  int raw = raw_bci();
287  return raw == SynchronizationEntryBCI ? 0 : raw;
288}
289
290
291int compiledVFrame::raw_bci() const {
292  if (scope() == NULL) {
293    // native nmethods have no scope the method/bci is implied
294    nmethod* nm = code()->as_nmethod();
295    assert(nm->is_native_method(), "must be native");
296    return 0;
297  }
298  return scope()->bci();
299}
300
301bool compiledVFrame::should_reexecute() const {
302  if (scope() == NULL) {
303    // native nmethods have no scope the method/bci is implied
304    nmethod* nm = code()->as_nmethod();
305    assert(nm->is_native_method(), "must be native");
306    return false;
307  }
308  return scope()->should_reexecute();
309}
310
311vframe* compiledVFrame::sender() const {
312  const frame f = fr();
313  if (scope() == NULL) {
314    // native nmethods have no scope the method/bci is implied
315    nmethod* nm = code()->as_nmethod();
316    assert(nm->is_native_method(), "must be native");
317    return vframe::sender();
318  } else {
319    return scope()->is_top()
320      ? vframe::sender()
321      : new compiledVFrame(&f, register_map(), thread(), scope()->sender(), vframe_id() + 1);
322  }
323}
324
325jvmtiDeferredLocalVariableSet::jvmtiDeferredLocalVariableSet(Method* method, int bci, intptr_t* id, int vframe_id) {
326  _method = method;
327  _bci = bci;
328  _id = id;
329  _vframe_id = vframe_id;
330  // Alway will need at least one, must be on C heap
331  _locals = new(ResourceObj::C_HEAP, mtCompiler) GrowableArray<jvmtiDeferredLocalVariable*> (1, true);
332}
333
334jvmtiDeferredLocalVariableSet::~jvmtiDeferredLocalVariableSet() {
335  for (int i = 0; i < _locals->length() ; i++ ) {
336    delete _locals->at(i);
337  }
338  // Free growableArray and c heap for elements
339  delete _locals;
340}
341
342bool jvmtiDeferredLocalVariableSet::matches(vframe* vf) {
343  if (!vf->is_compiled_frame()) return false;
344  compiledVFrame* cvf = (compiledVFrame*)vf;
345  if (cvf->fr().id() == id() && cvf->vframe_id() == vframe_id()) {
346    assert(cvf->method() == method() && cvf->bci() == bci(), "must agree");
347    return true;
348  }
349  return false;
350}
351
352void jvmtiDeferredLocalVariableSet::set_local_at(int idx, BasicType type, jvalue val) {
353  int i;
354  for ( i = 0 ; i < locals()->length() ; i++ ) {
355    if ( locals()->at(i)->index() == idx) {
356      assert(locals()->at(i)->type() == type, "Wrong type");
357      locals()->at(i)->set_value(val);
358      return;
359    }
360  }
361  locals()->push(new jvmtiDeferredLocalVariable(idx, type, val));
362}
363
364void jvmtiDeferredLocalVariableSet::oops_do(OopClosure* f) {
365  // The Method* is on the stack so a live activation keeps it alive
366  // either by mirror in interpreter or code in compiled code.
367  for ( int i = 0; i < locals()->length(); i++ ) {
368    if ( locals()->at(i)->type() == T_OBJECT) {
369      f->do_oop(locals()->at(i)->oop_addr());
370    }
371  }
372}
373
374jvmtiDeferredLocalVariable::jvmtiDeferredLocalVariable(int index, BasicType type, jvalue value) {
375  _index = index;
376  _type = type;
377  _value = value;
378}
379
380
381#ifndef PRODUCT
382void compiledVFrame::verify() const {
383  Unimplemented();
384}
385#endif // PRODUCT
386