1 /*
   2  * Copyright 2000-2005 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 
  30 import sun.jvm.hotspot.asm.x86.*;
  31 import sun.jvm.hotspot.compiler.*;
  32 import sun.jvm.hotspot.debugger.*;
  33 import sun.jvm.hotspot.runtime.*;
  34 import sun.jvm.hotspot.types.*;
  35 import sun.jvm.hotspot.utilities.*;
  36 
  37 public class CodeBlob extends VMObject {
  38   private static AddressField  nameField;
  39   private static CIntegerField sizeField;
  40   private static CIntegerField headerSizeField;
  41   private static CIntegerField relocationSizeField;
  42   private static CIntegerField instructionsOffsetField;
  43   private static CIntegerField frameCompleteOffsetField;
  44   private static CIntegerField dataOffsetField;
  45   private static CIntegerField oopsOffsetField;
  46   private static CIntegerField oopsLengthField;
  47   private static CIntegerField frameSizeField;
  48   private static AddressField  oopMapsField;
  49 
  50   // Only used by server compiler on x86; computed over in SA rather
  51   // than relying on computation in target VM
  52   private static final int     NOT_YET_COMPUTED = -2;
  53   private static final int     UNDEFINED        = -1;
  54   private              int     linkOffset       = NOT_YET_COMPUTED;
  55   private static       int     matcherInterpreterFramePointerReg;
  56 
  57   static {
  58     VM.registerVMInitializedObserver(new Observer() {
  59         public void update(Observable o, Object data) {
  60           initialize(VM.getVM().getTypeDataBase());
  61         }
  62       });
  63   }
  64 
  65   private static void initialize(TypeDataBase db) {
  66     Type type = db.lookupType("CodeBlob");
  67 
  68     nameField                = type.getAddressField("_name");
  69     sizeField                = type.getCIntegerField("_size");
  70     headerSizeField          = type.getCIntegerField("_header_size");
  71     relocationSizeField      = type.getCIntegerField("_relocation_size");
  72     frameCompleteOffsetField = type.getCIntegerField("_frame_complete_offset");
  73     instructionsOffsetField  = type.getCIntegerField("_instructions_offset");
  74     dataOffsetField          = type.getCIntegerField("_data_offset");
  75     oopsOffsetField          = type.getCIntegerField("_oops_offset");
  76     oopsLengthField          = type.getCIntegerField("_oops_length");
  77     frameSizeField           = type.getCIntegerField("_frame_size");
  78     oopMapsField             = type.getAddressField("_oop_maps");
  79 
  80     if (VM.getVM().isServerCompiler()) {
  81       matcherInterpreterFramePointerReg =
  82         db.lookupIntConstant("Matcher::interpreter_frame_pointer_reg").intValue();
  83     }
  84   }
  85 
  86   public CodeBlob(Address addr) {
  87     super(addr);
  88   }
  89 
  90   // Typing
  91   public boolean isBufferBlob()         { return false; }
  92   public boolean isNMethod()            { return false; }
  93   public boolean isRuntimeStub()        { return false; }
  94   public boolean isDeoptimizationStub() { return false; }
  95   public boolean isUncommonTrapStub()   { return false; }
  96   public boolean isExceptionStub()      { return false; }
  97   public boolean isSafepointStub()      { return false; }
  98 
  99   // Fine grain nmethod support: isNmethod() == isJavaMethod() || isNativeMethod() || isOSRMethod()
 100   public boolean isJavaMethod()         { return false; }
 101   public boolean isNativeMethod()       { return false; }
 102   /** On-Stack Replacement method */
 103   public boolean isOSRMethod()          { return false; }
 104 
 105   // Boundaries
 106   public Address headerBegin() {
 107     return addr;
 108   }
 109 
 110   public Address headerEnd() {
 111     return addr.addOffsetTo(headerSizeField.getValue(addr));
 112   }
 113 
 114   // FIXME: add RelocInfo
 115   //  public RelocInfo relocationBegin();
 116   //  public RelocInfo relocationEnd();
 117 
 118   public Address instructionsBegin() {
 119     return headerBegin().addOffsetTo(instructionsOffsetField.getValue(addr));
 120   }
 121 
 122   public Address instructionsEnd() {
 123     return headerBegin().addOffsetTo(dataOffsetField.getValue(addr));
 124   }
 125 
 126   public Address dataBegin() {
 127     return headerBegin().addOffsetTo(dataOffsetField.getValue(addr));
 128   }
 129 
 130   public Address dataEnd() {
 131     return headerBegin().addOffsetTo(sizeField.getValue(addr));
 132   }
 133 
 134   public Address oopsBegin() {
 135     return headerBegin().addOffsetTo(oopsOffsetField.getValue(addr));
 136   }
 137 
 138   public Address oopsEnd() {
 139     return oopsBegin().addOffsetTo(getOopsLength());
 140   }
 141 
 142   // Offsets
 143   public int getRelocationOffset()   { return (int) headerSizeField.getValue(addr);         }
 144   public int getInstructionsOffset() { return (int) instructionsOffsetField.getValue(addr); }
 145   public int getDataOffset()         { return (int) dataOffsetField.getValue(addr);         }
 146   public int getOopsOffset()         { return (int) oopsOffsetField.getValue(addr);         }
 147 
 148   // Sizes
 149   public int getSize()             { return (int) sizeField.getValue(addr);                     }
 150   public int getHeaderSize()       { return (int) headerSizeField.getValue(addr);               }
 151   // FIXME: add getRelocationSize()
 152   public int getInstructionsSize() { return (int) instructionsEnd().minus(instructionsBegin()); }
 153   public int getDataSize()         { return (int) dataEnd().minus(dataBegin());                 }
 154 
 155   // Containment
 156   public boolean blobContains(Address addr)         { return headerBegin().lessThanOrEqual(addr) && dataEnd().greaterThan(addr);               }
 157   // FIXME: add relocationContains
 158   public boolean instructionsContains(Address addr) { return instructionsBegin().lessThanOrEqual(addr) && instructionsEnd().greaterThan(addr); }
 159   public boolean dataContains(Address addr)         { return dataBegin().lessThanOrEqual(addr) && dataEnd().greaterThan(addr);                 }
 160   public boolean oopsContains(Address addr)         { return oopsBegin().lessThanOrEqual(addr) && oopsEnd().greaterThan(addr);                 }
 161   public boolean contains(Address addr)             { return instructionsContains(addr);                                                       }
 162   public boolean isFrameCompleteAt(Address a)       { return instructionsContains(a) && a.minus(instructionsBegin()) >= frameCompleteOffsetField.getValue(addr); }
 163 
 164   /** Support for oops in scopes and relocs. Note: index 0 is reserved for null. */
 165   public OopHandle getOopAt(int index) {
 166     if (index == 0) return null;
 167     if (Assert.ASSERTS_ENABLED) {
 168       Assert.that(index > 0 && index <= getOopsLength(), "must be a valid non-zero index");
 169     }
 170     return oopsBegin().getOopHandleAt((index - 1) * VM.getVM().getOopSize());
 171   }
 172 
 173   // Reclamation support (really only used by the nmethods, but in order to get asserts to work
 174   // in the CodeCache they are defined virtual here)
 175   public boolean isZombie()             { return false; }
 176   public boolean isLockedByVM()         { return false; }
 177 
 178   /** OopMap for frame; can return null if none available */
 179   public OopMapSet getOopMaps() {
 180     Address oopMapsAddr = oopMapsField.getValue(addr);
 181     if (oopMapsAddr == null) {
 182       return null;
 183     }
 184     return new OopMapSet(oopMapsAddr);
 185   }
 186   // FIXME: not yet implementable
 187   //  void set_oop_maps(OopMapSet* p);
 188 
 189   public OopMap getOopMapForReturnAddress(Address returnAddress, boolean debugging) {
 190     Address pc = returnAddress;
 191     if (Assert.ASSERTS_ENABLED) {
 192       Assert.that(getOopMaps() != null, "nope");
 193     }
 194     return getOopMaps().findMapAtOffset(pc.minus(instructionsBegin()), debugging);
 195   }
 196 
 197   //  virtual void preserve_callee_argument_oops(frame fr, const RegisterMap* reg_map, void f(oop*)) { ShouldNotReachHere(); }
 198   //  FIXME;
 199 
 200   /** NOTE: this returns a size in BYTES in this system! */
 201   public long getFrameSize() {
 202     return VM.getVM().getAddressSize() * frameSizeField.getValue(addr);
 203   }
 204 
 205   // Returns true, if the next frame is responsible for GC'ing oops passed as arguments
 206   public boolean callerMustGCArguments(JavaThread thread) { return false; }
 207 
 208   public String getName() {
 209     return CStringUtilities.getString(nameField.getValue(addr));
 210   }
 211 
 212   // FIXME: NOT FINISHED
 213 
 214   // FIXME: add more accessors
 215 
 216   public void print() {
 217     printOn(System.out);
 218   }
 219 
 220   public void printOn(PrintStream tty) {
 221     tty.print(getName());
 222     printComponentsOn(tty);
 223   }
 224 
 225   protected void printComponentsOn(PrintStream tty) {
 226     // FIXME: add relocation information
 227     tty.println(" instructions: [" + instructionsBegin() + ", " + instructionsEnd() + "), " +
 228                 " data: [" + dataBegin() + ", " + dataEnd() + "), " +
 229                 " oops: [" + oopsBegin() + ", " + oopsEnd() + "), " +
 230                 " frame size: " + getFrameSize());
 231   }
 232 
 233   //--------------------------------------------------------------------------------
 234   // Internals only below this point
 235   //
 236 
 237   private int getOopsLength() {
 238     return (int) oopsLengthField.getValue(addr);
 239   }
 240 }