1 /*
   2  * Copyright (c) 1997, 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 "interpreter/interpreter.hpp"
  27 #include "oops/oop.inline.hpp"
  28 #include "oops/symbol.hpp"
  29 #include "runtime/frame.inline.hpp"
  30 #include "runtime/rframe.hpp"
  31 #include "runtime/vframe.hpp"
  32 #include "runtime/vframe_hp.hpp"
  33 
  34 
  35 static RFrame*const  noCaller    = (RFrame*) 0x1;               // no caller (i.e., initial frame)
  36 static RFrame*const  noCallerYet = (RFrame*) 0x0;               // caller not yet computed
  37 
  38 RFrame::RFrame(frame fr, JavaThread* thread, RFrame*const callee) :
  39   _fr(fr), _thread(thread), _callee(callee), _num(callee ? callee->num() + 1 : 0) {
  40   _caller = (RFrame*)noCallerYet;
  41   _invocations = 0;
  42   _distance = 0;
  43 }
  44 
  45 void RFrame::set_distance(int d) {
  46   assert(is_compiled() || d >= 0, "should be positive");
  47   _distance = d;
  48 }
  49 
  50 InterpretedRFrame::InterpretedRFrame(frame fr, JavaThread* thread, RFrame*const callee)
  51 : RFrame(fr, thread, callee) {
  52   RegisterMap map(thread, false);
  53   _vf     = javaVFrame::cast(vframe::new_vframe(&_fr, &map, thread));
  54   _method = methodHandle(thread, _vf->method());
  55   assert(   _vf->is_interpreted_frame(), "must be interpreted");
  56   init();
  57 }
  58 
  59 InterpretedRFrame::InterpretedRFrame(frame fr, JavaThread* thread, methodHandle m)
  60 : RFrame(fr, thread, NULL) {
  61   RegisterMap map(thread, false);
  62   _vf     = javaVFrame::cast(vframe::new_vframe(&_fr, &map, thread));
  63   _method = m;
  64 
  65   assert(   _vf->is_interpreted_frame(),  "must be interpreted");
  66   init();
  67 }
  68 
  69 CompiledRFrame::CompiledRFrame(frame fr, JavaThread* thread, RFrame*const  callee)
  70 : RFrame(fr, thread, callee) {
  71   init();
  72 }
  73 
  74 CompiledRFrame::CompiledRFrame(frame fr, JavaThread* thread)
  75 : RFrame(fr, thread, NULL) {
  76   init();
  77 }
  78 
  79 DeoptimizedRFrame::DeoptimizedRFrame(frame fr, JavaThread* thread, RFrame*const  callee)
  80 : InterpretedRFrame(fr, thread, callee) {}
  81 
  82 RFrame* RFrame::new_RFrame(frame fr, JavaThread* thread, RFrame*const  callee) {
  83   RFrame* rf;
  84   int dist = callee ? callee->distance() : -1;
  85   if (fr.is_interpreted_frame()) {
  86     rf = new InterpretedRFrame(fr, thread, callee);
  87     dist++;
  88   } else if (fr.is_compiled_frame()) {
  89     // Even deopted frames look compiled because the deopt
  90     // is invisible until it happens.
  91     rf = new CompiledRFrame(fr, thread, callee);
  92   } else {
  93     assert(false, "Unhandled frame type");
  94   }
  95   rf->set_distance(dist);
  96   rf->init();
  97   return rf;
  98 }
  99 
 100 RFrame* RFrame::caller() {
 101   if (_caller != noCallerYet) return (_caller == noCaller) ? NULL : _caller;    // already computed caller
 102 
 103   // caller not yet computed; do it now
 104   if (_fr.is_first_java_frame()) {
 105     _caller = (RFrame*)noCaller;
 106     return NULL;
 107   }
 108 
 109   RegisterMap map(_thread, false);
 110   frame sender = _fr.real_sender(&map);
 111   if (sender.is_java_frame()) {
 112     _caller = new_RFrame(sender, thread(), this);
 113     return _caller;
 114   }
 115 
 116   // Real caller is not java related
 117   _caller = (RFrame*)noCaller;
 118   return NULL;
 119 }
 120 
 121 int InterpretedRFrame::cost() const {
 122   return _method->code_size();    // fix this
 123   //return _method->estimated_inline_cost(_receiverKlass);
 124 }
 125 
 126 int CompiledRFrame::cost() const {
 127   nmethod* nm = top_method()->code();
 128   if (nm != NULL) {
 129     return nm->insts_size();
 130   } else {
 131     return top_method()->code_size();
 132   }
 133 }
 134 
 135 void CompiledRFrame::init() {
 136   RegisterMap map(thread(), false);
 137   vframe* vf = vframe::new_vframe(&_fr, &map, thread());
 138   assert(vf->is_compiled_frame(), "must be compiled");
 139   _nm = compiledVFrame::cast(vf)->code();
 140   vf = vf->top();
 141   _vf = javaVFrame::cast(vf);
 142   _method = methodHandle(thread(), CodeCache::find_nmethod(_fr.pc())->method());
 143   assert(_method(), "should have found a method");
 144 #ifndef PRODUCT
 145   _invocations = _method->compiled_invocation_count();
 146 #endif
 147 }
 148 
 149 void InterpretedRFrame::init() {
 150   _invocations = _method->invocation_count() + _method->backedge_count();
 151 }
 152 
 153 void RFrame::print(const char* kind) {
 154 #ifndef PRODUCT
 155 #ifdef COMPILER2
 156   int cnt = top_method()->interpreter_invocation_count();
 157 #else
 158   int cnt = top_method()->invocation_count();
 159 #endif
 160   tty->print("%3d %s ", _num, is_interpreted() ? "I" : "C");
 161   top_method()->print_short_name(tty);
 162   tty->print_cr(": inv=%5d(%d) cst=%4d", _invocations, cnt, cost());
 163 #endif
 164 }
 165 
 166 void CompiledRFrame::print() {
 167   RFrame::print("comp");
 168 }
 169 
 170 void InterpretedRFrame::print() {
 171   RFrame::print("int.");
 172 }
 173 
 174 void DeoptimizedRFrame::print() {
 175   RFrame::print("deopt.");
 176 }