src/jdk.internal.vm.ci/share/classes/jdk.vm.ci.hotspot/src/jdk/vm/ci/hotspot/HotSpotNmethod.java
Index Unified diffs Context diffs Sdiffs Frames Patch New Old Previous File Next File open Sdiff src/jdk.internal.vm.ci/share/classes/jdk.vm.ci.hotspot/src/jdk/vm/ci/hotspot

src/jdk.internal.vm.ci/share/classes/jdk.vm.ci.hotspot/src/jdk/vm/ci/hotspot/HotSpotNmethod.java

Print this page


   1 /*
   2  * Copyright (c) 2011, 2015, 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 package jdk.vm.ci.hotspot;
  24 
  25 import static jdk.vm.ci.hotspot.CompilerToVM.compilerToVM;


  26 import jdk.vm.ci.code.InstalledCode;
  27 import jdk.vm.ci.code.InvalidInstalledCodeException;
  28 import jdk.vm.ci.meta.JavaKind;
  29 import jdk.vm.ci.meta.JavaType;
  30 import jdk.vm.ci.meta.ResolvedJavaMethod;
  31 
  32 /**
  33  * Implementation of {@link InstalledCode} for code installed as an nmethod.
  34  *
  35  * When a {@link HotSpotNmethod} dies, it triggers unloading of the nmethod unless
  36  * {@link #isDefault() == true}.
  37  */
  38 public class HotSpotNmethod extends HotSpotInstalledCode {
  39 
  40     /**
  41      * This (indirect) Method* reference is safe since class redefinition preserves all methods
  42      * associated with nmethods in the code cache.
  43      */
  44     private final HotSpotResolvedJavaMethod method;
  45 







  46     private final boolean isDefault;
  47 
  48     public HotSpotNmethod(HotSpotResolvedJavaMethod method, String name, boolean isDefault) {























  49         super(name);
  50         this.method = method;
  51         this.isDefault = isDefault;



  52     }
  53 
  54     /**
  55      * Determines if the nmethod associated with this object is the compiled entry point for
  56      * {@link #getMethod()}. If {@code false}, then the nmethod is unloaded when the VM determines
  57      * this object has died.
  58      */
  59     public boolean isDefault() {
  60         return isDefault;
  61     }
  62 








  63     public ResolvedJavaMethod getMethod() {
  64         return method;
  65     }
  66 
  67     @Override
  68     public void invalidate() {
  69         compilerToVM().invalidateInstalledCode(this);
  70     }
  71 
  72     @Override
  73     public String toString() {
  74         return String.format("InstalledNmethod[method=%s, codeBlob=0x%x, isDefault=%b, name=%s]", method, getAddress(), isDefault, name);



  75     }
  76 
  77     protected boolean checkThreeObjectArgs() {
  78         assert method.getSignature().getParameterCount(!method.isStatic()) == 3;
  79         assert method.getSignature().getParameterKind(0) == JavaKind.Object;
  80         assert method.getSignature().getParameterKind(1) == JavaKind.Object;
  81         assert !method.isStatic() || method.getSignature().getParameterKind(2) == JavaKind.Object;
  82         return true;






  83     }
  84 
  85     private boolean checkArgs(Object... args) {
  86         JavaType[] sig = method.toParameterTypes();
  87         assert args.length == sig.length : method.format("%H.%n(%p): expected ") + sig.length + " args, got " + args.length;
  88         for (int i = 0; i < sig.length; i++) {
  89             Object arg = args[i];
  90             if (arg == null) {
  91                 assert sig[i].getJavaKind() == JavaKind.Object : method.format("%H.%n(%p): expected arg ") + i + " to be Object, not " + sig[i];
  92             } else if (sig[i].getJavaKind() != JavaKind.Object) {
  93                 assert sig[i].getJavaKind().toBoxedJavaClass() == arg.getClass() : method.format("%H.%n(%p): expected arg ") + i + " to be " + sig[i] + ", not " + arg.getClass();
  94             }
  95         }
  96         return true;
  97     }
  98 
  99     @Override
 100     public Object executeVarargs(Object... args) throws InvalidInstalledCodeException {



 101         assert checkArgs(args);
 102         return compilerToVM().executeInstalledCode(args, this);
 103     }
 104 
 105     @Override
 106     public long getStart() {
 107         return isValid() ? super.getStart() : 0;
 108     }
 109 }
   1 /*
   2  * Copyright (c) 2011, 2019, 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 package jdk.vm.ci.hotspot;
  24 
  25 import static jdk.vm.ci.hotspot.CompilerToVM.compilerToVM;
  26 import static jdk.vm.ci.services.Services.IS_IN_NATIVE_IMAGE;
  27 
  28 import jdk.vm.ci.code.InstalledCode;
  29 import jdk.vm.ci.code.InvalidInstalledCodeException;
  30 import jdk.vm.ci.meta.JavaKind;
  31 import jdk.vm.ci.meta.JavaType;
  32 import jdk.vm.ci.meta.ResolvedJavaMethod;
  33 
  34 /**
  35  * Implementation of {@link InstalledCode} for code installed as an {@code nmethod}. The address of
  36  * the {@code nmethod} is stored in {@link InstalledCode#address} and the value of
  37  * {@code nmethod::verified_entry_point()} is in {@link InstalledCode#entryPoint}.

  38  */
  39 public class HotSpotNmethod extends HotSpotInstalledCode {
  40 
  41     /**
  42      * This (indirect) {@code Method*} reference is safe since class redefinition preserves all
  43      * methods associated with nmethods in the code cache.
  44      */
  45     private final HotSpotResolvedJavaMethodImpl method;
  46 
  47     /**
  48      * Specifies whether the {@code nmethod} associated with this object is the code executed by
  49      * default HotSpot linkage when a normal Java call to {@link #method} is made. That is, does
  50      * {@code this.method.metadataHandle->_code} == {@code this.address}. If not, then the
  51      * {@code nmethod} can only be invoked via a reference to this object. An example of this is the
  52      * trampoline mechanism used by Truffle: https://goo.gl/LX88rZ.
  53      */
  54     private final boolean isDefault;
  55 
  56     /**
  57      * Determines whether this object is in the oops table of the nmethod.
  58      * <p>
  59      * If this object is in the oops table, the VM uses the oops table entry to update this object's
  60      * {@link #address} and {@link #entryPoint} fields when the state of the nmethod changes. The
  61      * nmethod will be unloadable when this object dies.
  62      * <p>
  63      * Otherwise, the nmethod's unloadability is not changed when this object dies.
  64      */
  65     boolean inOopsTable() {
  66         return compileIdSnapshot != 0;
  67     }
  68 
  69     /**
  70      * If this field is 0, this object is in the oops table of the nmethod. Otherwise, the value of
  71      * the field records the nmethod's compile identifier. This value is used to confirm an entry in
  72      * the code cache retrieved by {@link #address} is indeed the nmethod represented by this
  73      * object.
  74      *
  75      * @see #inOopsTable
  76      */
  77     private final long compileIdSnapshot;
  78 
  79     HotSpotNmethod(HotSpotResolvedJavaMethodImpl method, String name, boolean isDefault, long compileId) {
  80         super(name);
  81         this.method = method;
  82         this.isDefault = isDefault;
  83         boolean inOopsTable = !IS_IN_NATIVE_IMAGE && !isDefault;
  84         this.compileIdSnapshot = inOopsTable ? 0L : compileId;
  85         assert inOopsTable || compileId != 0L : this;
  86     }
  87 
  88     /**
  89      * Determines if the nmethod associated with this object is the compiled entry point for
  90      * {@link #getMethod()}.

  91      */
  92     public boolean isDefault() {
  93         return isDefault;
  94     }
  95 
  96     @Override
  97     public boolean isValid() {
  98         if (compileIdSnapshot != 0L) {
  99             compilerToVM().updateHotSpotNmethod(this);
 100         }
 101         return super.isValid();
 102     }
 103 
 104     public ResolvedJavaMethod getMethod() {
 105         return method;
 106     }
 107 
 108     @Override
 109     public void invalidate() {
 110         compilerToVM().invalidateHotSpotNmethod(this);
 111     }
 112 
 113     @Override
 114     public long getAddress() {
 115         if (compileIdSnapshot != 0L) {
 116             compilerToVM().updateHotSpotNmethod(this);
 117         }
 118         return super.getAddress();
 119     }
 120 
 121     @Override
 122     public long getEntryPoint() {
 123         if (compileIdSnapshot != 0L) {
 124             return 0;
 125         }
 126         return super.getEntryPoint();
 127     }
 128 
 129     @Override
 130     public String toString() {
 131         return String.format("HotSpotNmethod[method=%s, codeBlob=0x%x, isDefault=%b, name=%s, inOopsTable=%s]",
 132                         method, getAddress(), isDefault, name, inOopsTable());
 133     }
 134 
 135     private boolean checkArgs(Object... args) {
 136         JavaType[] sig = method.toParameterTypes();
 137         assert args.length == sig.length : method.format("%H.%n(%p): expected ") + sig.length + " args, got " + args.length;
 138         for (int i = 0; i < sig.length; i++) {
 139             Object arg = args[i];
 140             if (arg == null) {
 141                 assert sig[i].getJavaKind() == JavaKind.Object : method.format("%H.%n(%p): expected arg ") + i + " to be Object, not " + sig[i];
 142             } else if (sig[i].getJavaKind() != JavaKind.Object) {
 143                 assert sig[i].getJavaKind().toBoxedJavaClass() == arg.getClass() : method.format("%H.%n(%p): expected arg ") + i + " to be " + sig[i] + ", not " + arg.getClass();
 144             }
 145         }
 146         return true;
 147     }
 148 
 149     @Override
 150     public Object executeVarargs(Object... args) throws InvalidInstalledCodeException {
 151         if (IS_IN_NATIVE_IMAGE) {
 152             throw new HotSpotJVMCIUnsupportedOperationError("Cannot execute nmethod via mirror in native image");
 153         }
 154         assert checkArgs(args);
 155         return compilerToVM().executeHotSpotNmethod(args, this);
 156     }
 157 
 158     @Override
 159     public long getStart() {
 160         return isValid() ? super.getStart() : 0;
 161     }
 162 }
src/jdk.internal.vm.ci/share/classes/jdk.vm.ci.hotspot/src/jdk/vm/ci/hotspot/HotSpotNmethod.java
Index Unified diffs Context diffs Sdiffs Frames Patch New Old Previous File Next File