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.HotSpotJVMCIRuntime.*; 26 27 import jdk.vm.ci.code.*; 28 import jdk.vm.ci.meta.*; 29 30 /** 31 * Implementation of {@link InstalledCode} for code installed as an nmethod. The nmethod stores a 32 * weak reference to an instance of this class. This is necessary to keep the nmethod from being 33 * unloaded while the associated {@link HotSpotNmethod} instance is alive. 34 * <p> 35 * Note that there is no (current) way for the reference from an nmethod to a {@link HotSpotNmethod} 36 * instance to be anything but weak. This is due to the fact that HotSpot does not treat nmethods as 37 * strong GC roots. 38 */ 39 public class HotSpotNmethod extends HotSpotInstalledCode { 40 41 /** 42 * This (indirect) Method* reference is safe since class redefinition preserves all methods 43 * associated with nmethods in the code cache. 44 */ 45 private final HotSpotResolvedJavaMethod method; 46 47 private final boolean isDefault; 48 private final boolean isExternal; 49 50 public HotSpotNmethod(HotSpotResolvedJavaMethod method, String name, boolean isDefault) { 51 this(method, name, isDefault, false); 52 } 53 54 public HotSpotNmethod(HotSpotResolvedJavaMethod method, String name, boolean isDefault, boolean isExternal) { 55 super(name); 56 this.method = method; 57 this.isDefault = isDefault; 58 this.isExternal = isExternal; 59 } 60 61 public boolean isDefault() { 62 return isDefault; 63 } 64 65 public boolean isExternal() { 66 return isExternal; 67 } 68 69 public ResolvedJavaMethod getMethod() { 70 return method; 71 } 72 73 @Override 74 public void invalidate() { 75 runtime().getCompilerToVM().invalidateInstalledCode(this); 76 } 77 78 @Override 79 public String toString() { 80 return String.format("InstalledNmethod[method=%s, codeBlob=0x%x, isDefault=%b, name=%s]", method, getAddress(), isDefault, name); 81 } 82 83 protected boolean checkThreeObjectArgs() { 84 assert method.getSignature().getParameterCount(!method.isStatic()) == 3; 85 assert method.getSignature().getParameterKind(0) == JavaKind.Object; 86 assert method.getSignature().getParameterKind(1) == JavaKind.Object; 87 assert !method.isStatic() || method.getSignature().getParameterKind(2) == JavaKind.Object; 88 return true; 89 } 90 91 private boolean checkArgs(Object... args) { 92 JavaType[] sig = method.toParameterTypes(); 93 assert args.length == sig.length : method.format("%H.%n(%p): expected ") + sig.length + " args, got " + args.length; 94 for (int i = 0; i < sig.length; i++) { 95 Object arg = args[i]; 96 if (arg == null) { 97 assert sig[i].getJavaKind() == JavaKind.Object : method.format("%H.%n(%p): expected arg ") + i + " to be Object, not " + sig[i]; 98 } else if (sig[i].getJavaKind() != JavaKind.Object) { 99 assert sig[i].getJavaKind().toBoxedJavaClass() == arg.getClass() : method.format("%H.%n(%p): expected arg ") + i + " to be " + sig[i] + ", not " + arg.getClass(); 100 } 101 } 102 return true; 103 } 104 105 @Override 106 public Object executeVarargs(Object... args) throws InvalidInstalledCodeException { 107 assert checkArgs(args); 108 assert !isExternal(); 109 return runtime().getCompilerToVM().executeInstalledCode(args, this); 110 } 111 112 @Override 113 public long getStart() { 114 return isValid() ? super.getStart() : 0; 115 } 116 }