1 /*
   2  * Copyright 2000-2009 Sun Microsystems, Inc.  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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
  20  * CA 95054 USA or visit www.sun.com if you need additional information or
  21  * have any questions.
  22  *
  23  */
  24 
  25 package sun.jvm.hotspot.code;
  26 
  27 import java.io.*;
  28 import java.util.*;
  29 import sun.jvm.hotspot.debugger.*;
  30 import sun.jvm.hotspot.memory.*;
  31 import sun.jvm.hotspot.oops.*;
  32 import sun.jvm.hotspot.runtime.*;
  33 import sun.jvm.hotspot.types.*;
  34 import sun.jvm.hotspot.utilities.*;
  35 
  36 public class NMethod extends CodeBlob {
  37   private static long          pcDescSize;
  38   private static CIntegerField zombieInstructionSizeField;
  39   private static sun.jvm.hotspot.types.OopField methodField;
  40   /** != InvocationEntryBci if this nmethod is an on-stack replacement method */
  41   private static CIntegerField entryBCIField;
  42   /** To support simple linked-list chaining of nmethods */
  43   private static AddressField  linkField;
  44   /** Offsets for different nmethod parts */
  45   private static CIntegerField exceptionOffsetField;
  46   private static CIntegerField deoptOffsetField;
  47   private static CIntegerField origPCOffsetField;
  48   private static CIntegerField stubOffsetField;
  49   private static CIntegerField scopesDataOffsetField;
  50   private static CIntegerField scopesPCsOffsetField;
  51   private static CIntegerField dependenciesOffsetField;
  52   private static CIntegerField handlerTableOffsetField;
  53   private static CIntegerField nulChkTableOffsetField;
  54   private static CIntegerField nmethodEndOffsetField;
  55 
  56   /** Offsets for entry points */
  57   /** Entry point with class check */
  58   private static AddressField  entryPointField;
  59   /** Entry point without class check */
  60   private static AddressField  verifiedEntryPointField;
  61   /** Entry point for on stack replacement */
  62   private static AddressField  osrEntryPointField;
  63 
  64   // FIXME: add access to flags (how?)
  65 
  66   /** NMethod Flushing lock (if non-zero, then the nmethod is not removed) */
  67   private static JIntField     lockCountField;
  68 
  69   /** not_entrant method removal. Each mark_sweep pass will update
  70       this mark to current sweep invocation count if it is seen on the
  71       stack.  An not_entrant method can be removed when there is no
  72       more activations, i.e., when the _stack_traversal_mark is less than
  73       current sweep traversal index. */
  74   private static CIntegerField stackTraversalMarkField;
  75 
  76   static {
  77     VM.registerVMInitializedObserver(new Observer() {
  78         public void update(Observable o, Object data) {
  79           initialize(VM.getVM().getTypeDataBase());
  80         }
  81       });
  82   }
  83 
  84   private static void initialize(TypeDataBase db) {
  85     Type type = db.lookupType("nmethod");
  86 
  87     zombieInstructionSizeField  = type.getCIntegerField("_zombie_instruction_size");
  88     methodField                 = type.getOopField("_method");
  89     entryBCIField               = type.getCIntegerField("_entry_bci");
  90     linkField                   = type.getAddressField("_link");
  91     exceptionOffsetField        = type.getCIntegerField("_exception_offset");
  92     deoptOffsetField            = type.getCIntegerField("_deoptimize_offset");
  93     origPCOffsetField           = type.getCIntegerField("_orig_pc_offset");
  94     stubOffsetField             = type.getCIntegerField("_stub_offset");
  95     scopesDataOffsetField       = type.getCIntegerField("_scopes_data_offset");
  96     scopesPCsOffsetField        = type.getCIntegerField("_scopes_pcs_offset");
  97     dependenciesOffsetField     = type.getCIntegerField("_dependencies_offset");
  98     handlerTableOffsetField     = type.getCIntegerField("_handler_table_offset");
  99     nulChkTableOffsetField      = type.getCIntegerField("_nul_chk_table_offset");
 100     nmethodEndOffsetField       = type.getCIntegerField("_nmethod_end_offset");
 101     entryPointField             = type.getAddressField("_entry_point");
 102     verifiedEntryPointField     = type.getAddressField("_verified_entry_point");
 103     osrEntryPointField          = type.getAddressField("_osr_entry_point");
 104     lockCountField              = type.getJIntField("_lock_count");
 105     stackTraversalMarkField     = type.getCIntegerField("_stack_traversal_mark");
 106 
 107     pcDescSize = db.lookupType("PcDesc").getSize();
 108   }
 109 
 110   public NMethod(Address addr) {
 111     super(addr);
 112   }
 113 
 114 
 115   // Accessors
 116   public Address getAddress() {
 117     return addr;
 118   }
 119 
 120   public Method getMethod() {
 121     return (Method) VM.getVM().getObjectHeap().newOop(methodField.getValue(addr));
 122   }
 123 
 124   // Type info
 125   public boolean isNMethod()      { return true;                    }
 126   public boolean isJavaMethod()   { return !getMethod().isNative(); }
 127   public boolean isNativeMethod() { return getMethod().isNative();  }
 128   public boolean isOSRMethod()    { return getEntryBCI() != VM.getVM().getInvocationEntryBCI(); }
 129 
 130   /** Boundaries for different parts */
 131   public Address constantsBegin()       { return instructionsBegin();                                }
 132   public Address constantsEnd()         { return getEntryPoint();                                    }
 133   public Address codeBegin()            { return getEntryPoint();                                    }
 134   public Address codeEnd()              { return headerBegin().addOffsetTo(getStubOffset());         }
 135   public Address exceptionBegin()       { return headerBegin().addOffsetTo(getExceptionOffset());    }
 136   public Address deoptBegin()           { return headerBegin().addOffsetTo(getDeoptOffset());        }
 137   public Address stubBegin()            { return headerBegin().addOffsetTo(getStubOffset());         }
 138   public Address stubEnd()              { return headerBegin().addOffsetTo(getScopesDataOffset());   }
 139   public Address scopesDataBegin()      { return headerBegin().addOffsetTo(getScopesDataOffset());   }
 140   public Address scopesDataEnd()        { return headerBegin().addOffsetTo(getScopesPCsOffset());    }
 141   public Address scopesPCsBegin()       { return headerBegin().addOffsetTo(getScopesPCsOffset());    }
 142   public Address scopesPCsEnd()         { return headerBegin().addOffsetTo(getDependenciesOffset()); }
 143   public Address dependenciesBegin()    { return headerBegin().addOffsetTo(getDependenciesOffset()); }
 144   public Address dependenciesEnd()      { return headerBegin().addOffsetTo(getHandlerTableOffset()); }
 145   public Address handlerTableBegin()    { return headerBegin().addOffsetTo(getHandlerTableOffset()); }
 146   public Address handlerTableEnd()      { return headerBegin().addOffsetTo(getNulChkTableOffset());  }
 147   public Address nulChkTableBegin()     { return headerBegin().addOffsetTo(getNulChkTableOffset());  }
 148   public Address nulChkTableEnd()       { return headerBegin().addOffsetTo(getNMethodEndOffset());   }
 149 
 150   public int constantsSize()            { return (int) constantsEnd()   .minus(constantsBegin());    }
 151   public int codeSize()                 { return (int) codeEnd()        .minus(codeBegin());         }
 152   public int stubSize()                 { return (int) stubEnd()        .minus(stubBegin());         }
 153   public int scopesDataSize()           { return (int) scopesDataEnd()  .minus(scopesDataBegin());   }
 154   public int scopesPCsSize()            { return (int) scopesPCsEnd()   .minus(scopesPCsBegin());    }
 155   public int dependenciesSize()         { return (int) dependenciesEnd().minus(dependenciesBegin()); }
 156   public int handlerTableSize()         { return (int) handlerTableEnd().minus(handlerTableBegin()); }
 157   public int nulChkTableSize()          { return (int) nulChkTableEnd() .minus(nulChkTableBegin());  }
 158   public int origPCOffset()             { return (int) origPCOffsetField.getValue(addr);             }
 159 
 160   public int totalSize() {
 161     return
 162       constantsSize()    +
 163       codeSize()         +
 164       stubSize()         +
 165       scopesDataSize()   +
 166       scopesPCsSize()    +
 167       dependenciesSize() +
 168       handlerTableSize() +
 169       nulChkTableSize();
 170   }
 171 
 172   public boolean constantsContains   (Address addr) { return constantsBegin()   .lessThanOrEqual(addr) && constantsEnd()   .greaterThan(addr); }
 173   public boolean codeContains        (Address addr) { return codeBegin()        .lessThanOrEqual(addr) && codeEnd()        .greaterThan(addr); }
 174   public boolean stubContains        (Address addr) { return stubBegin()        .lessThanOrEqual(addr) && stubEnd()        .greaterThan(addr); }
 175   public boolean scopesDataContains  (Address addr) { return scopesDataBegin()  .lessThanOrEqual(addr) && scopesDataEnd()  .greaterThan(addr); }
 176   public boolean scopesPCsContains   (Address addr) { return scopesPCsBegin()   .lessThanOrEqual(addr) && scopesPCsEnd()   .greaterThan(addr); }
 177   public boolean handlerTableContains(Address addr) { return handlerTableBegin().lessThanOrEqual(addr) && handlerTableEnd().greaterThan(addr); }
 178   public boolean nulChkTableContains (Address addr) { return nulChkTableBegin() .lessThanOrEqual(addr) && nulChkTableEnd() .greaterThan(addr); }
 179 
 180   /** Entry points */
 181   public Address getEntryPoint()         { return entryPointField.getValue(addr);         }
 182   public Address getVerifiedEntryPoint() { return verifiedEntryPointField.getValue(addr); }
 183 
 184   // FIXME: add interpreter_entry_point()
 185   // FIXME: add lazy_interpreter_entry_point() for C2
 186 
 187   // **********
 188   // * FIXME: * ADD ACCESS TO FLAGS!!!!
 189   // **********
 190   // public boolean isInUse();
 191   // public boolean isAlive();
 192   // public boolean isNotEntrant();
 193   // public boolean isZombie();
 194 
 195   // ********************************
 196   // * MAJOR FIXME: MAJOR HACK HERE *
 197   // ********************************
 198   public boolean isZombie() { return false; }
 199 
 200   // public boolean isUnloaded();
 201   // public boolean isYoung();
 202   // public boolean isOld();
 203   // public int     age();
 204   // public boolean isMarkedForDeoptimization();
 205   // public boolean isMarkedForUnloading();
 206   // public boolean isMarkedForReclamation();
 207   // public int     level();
 208   // public int     version();
 209 
 210   // FIXME: add mutators for above
 211   // FIXME: add exception cache access?
 212 
 213   /** On-stack replacement support */
 214   // FIXME: add mutators
 215   public int getOSREntryBCI() {
 216     if (Assert.ASSERTS_ENABLED) {
 217       Assert.that(getEntryBCI() != VM.getVM().getInvocationEntryBCI(), "wrong kind of nmethod");
 218     }
 219     return getEntryBCI();
 220   }
 221 
 222   public NMethod getLink() {
 223     return (NMethod) VMObjectFactory.newObject(NMethod.class, linkField.getValue(addr));
 224   }
 225 
 226   /** Tells whether frames described by this nmethod can be
 227       deoptimized. Note: native wrappers cannot be deoptimized. */
 228   public boolean canBeDeoptimized() { return isJavaMethod(); }
 229 
 230   // FIXME: add inline cache support
 231   // FIXME: add flush()
 232 
 233   public boolean isLockedByVM() { return lockCountField.getValue(addr) > 0; }
 234 
 235   // FIXME: add mark_as_seen_on_stack
 236   // FIXME: add can_not_entrant_be_converted
 237 
 238   // FIXME: add GC support
 239   //  void follow_roots_or_mark_for_unloading(bool unloading_occurred, bool& marked_for_unloading);
 240   //  void follow_root_or_mark_for_unloading(oop* root, bool unloading_occurred, bool& marked_for_unloading);
 241   //  void preserve_callee_argument_oops(frame fr, const RegisterMap *reg_map, void f(oop*));
 242   //  void adjust_pointers();
 243 
 244   /** Finds a PCDesc with real-pc equal to "pc" */
 245   public PCDesc getPCDescAt(Address pc) {
 246     // FIXME: consider adding cache like the one down in the VM
 247     for (Address p = scopesPCsBegin(); p.lessThan(scopesPCsEnd()); p = p.addOffsetTo(pcDescSize)) {
 248       PCDesc pcDesc = new PCDesc(p);
 249       if (pcDesc.getRealPC(this).equals(pc)) {
 250         return pcDesc;
 251       }
 252     }
 253     return null;
 254   }
 255 
 256   /** ScopeDesc for an instruction */
 257   public ScopeDesc getScopeDescAt(Address pc) {
 258     PCDesc pd = getPCDescAt(pc);
 259     if (Assert.ASSERTS_ENABLED) {
 260       Assert.that(pd != null, "scope must be present");
 261     }
 262     return new ScopeDesc(this, pd.getScopeDecodeOffset(), pd.getReexecute());
 263   }
 264 
 265   /** This is only for use by the debugging system, and is only
 266       intended for use in the topmost frame, where we are not
 267       guaranteed to be at a PC for which we have a PCDesc. It finds
 268       the PCDesc with realPC closest to the current PC. */
 269   public PCDesc getPCDescNearDbg(Address pc) {
 270     PCDesc bestGuessPCDesc = null;
 271     long bestDistance = 0;
 272     for (Address p = scopesPCsBegin(); p.lessThan(scopesPCsEnd()); p = p.addOffsetTo(pcDescSize)) {
 273       PCDesc pcDesc = new PCDesc(p);
 274       // In case pc is null
 275       long distance = -pcDesc.getRealPC(this).minus(pc);
 276       if ((bestGuessPCDesc == null) ||
 277           ((distance >= 0) && (distance < bestDistance))) {
 278         bestGuessPCDesc = pcDesc;
 279         bestDistance    = distance;
 280       }
 281     }
 282     return bestGuessPCDesc;
 283   }
 284 
 285   /** This is only for use by the debugging system, and is only
 286       intended for use in the topmost frame, where we are not
 287       guaranteed to be at a PC for which we have a PCDesc. It finds
 288       the ScopeDesc closest to the current PC. NOTE that this may
 289       return NULL for compiled methods which don't have any
 290       ScopeDescs! */
 291   public ScopeDesc getScopeDescNearDbg(Address pc) {
 292     PCDesc pd = getPCDescNearDbg(pc);
 293     if (pd == null) return null;
 294     return new ScopeDesc(this, pd.getScopeDecodeOffset(), pd.getReexecute());
 295   }
 296 
 297   public Map/*<Address, PcDesc>*/ getSafepoints() {
 298     Map safepoints = new HashMap(); // Map<Address, PcDesc>
 299     sun.jvm.hotspot.debugger.Address p = null;
 300     for (p = scopesPCsBegin(); p.lessThan(scopesPCsEnd());
 301          p = p.addOffsetTo(pcDescSize)) {
 302        PCDesc pcDesc = new PCDesc(p);
 303        sun.jvm.hotspot.debugger.Address pc = pcDesc.getRealPC(this);
 304        safepoints.put(pc, pcDesc);
 305     }
 306     return safepoints;
 307   }
 308 
 309   // FIXME: add getPCOffsetForBCI()
 310   // FIXME: add embeddedOopAt()
 311   // FIXME: add isDependentOn()
 312   // FIXME: add isPatchableAt()
 313 
 314   /** Support for code generation. Only here for proof-of-concept. */
 315   public static int getEntryPointOffset()            { return (int) entryPointField.getOffset();            }
 316   public static int getVerifiedEntryPointOffset()    { return (int) verifiedEntryPointField.getOffset();    }
 317   public static int getOSREntryPointOffset()         { return (int) osrEntryPointField.getOffset();         }
 318   public static int getEntryBCIOffset()              { return (int) entryBCIField.getOffset();              }
 319   /** NOTE: renamed from "method_offset_in_bytes" */
 320   public static int getMethodOffset()                { return (int) methodField.getOffset();                }
 321 
 322   public void print() {
 323     printOn(System.out);
 324   }
 325 
 326   public String toString() {
 327     Method method = getMethod();
 328     return "NMethod for " +
 329             method.getMethodHolder().getName().asString() + "." +
 330             method.getName().asString() + method.getSignature().asString() + "==>n" +
 331             super.toString();
 332   }
 333 
 334   public String flagsToString() {
 335     // FIXME need access to flags...
 336     return "";
 337   }
 338 
 339   public String getName() {
 340     Method method = getMethod();
 341     return "NMethod for " +
 342            method.getMethodHolder().getName().asString() + "." +
 343            method.getName().asString() +
 344            method.getSignature().asString();
 345   }
 346 
 347   //--------------------------------------------------------------------------------
 348   // Internals only below this point
 349   //
 350 
 351   private int getEntryBCI()           { return (int) entryBCIField          .getValue(addr); }
 352   private int getExceptionOffset()    { return (int) exceptionOffsetField   .getValue(addr); }
 353   private int getDeoptOffset()        { return (int) deoptOffsetField       .getValue(addr); }
 354   private int getStubOffset()         { return (int) stubOffsetField        .getValue(addr); }
 355   private int getScopesDataOffset()   { return (int) scopesDataOffsetField  .getValue(addr); }
 356   private int getScopesPCsOffset()    { return (int) scopesPCsOffsetField   .getValue(addr); }
 357   private int getDependenciesOffset() { return (int) dependenciesOffsetField.getValue(addr); }
 358   private int getHandlerTableOffset() { return (int) handlerTableOffsetField.getValue(addr); }
 359   private int getNulChkTableOffset()  { return (int) nulChkTableOffsetField .getValue(addr); }
 360   private int getNMethodEndOffset()   { return (int) nmethodEndOffsetField  .getValue(addr); }
 361 }