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