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