1 /*
   2  * Copyright (c) 2015, 2018, 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 
  24 
  25 package org.graalvm.compiler.hotspot.meta;
  26 
  27 import java.lang.reflect.Method;
  28 import java.util.function.Supplier;
  29 
  30 import org.graalvm.compiler.core.common.type.ObjectStamp;
  31 import org.graalvm.compiler.core.common.type.Stamp;
  32 import org.graalvm.compiler.core.common.type.StampFactory;
  33 import org.graalvm.compiler.debug.GraalError;
  34 import org.graalvm.compiler.hotspot.nodes.aot.InitializeKlassNode;
  35 import org.graalvm.compiler.hotspot.nodes.aot.ResolveConstantNode;
  36 import org.graalvm.compiler.nodes.ConstantNode;
  37 import org.graalvm.compiler.nodes.DeoptimizingFixedWithNextNode;
  38 import org.graalvm.compiler.nodes.FrameState;
  39 import org.graalvm.compiler.nodes.ValueNode;
  40 import org.graalvm.compiler.nodes.graphbuilderconf.ClassInitializationPlugin;
  41 import org.graalvm.compiler.nodes.graphbuilderconf.GraphBuilderContext;
  42 
  43 import jdk.vm.ci.hotspot.HotSpotResolvedObjectType;
  44 import jdk.vm.ci.meta.ConstantPool;
  45 import jdk.vm.ci.meta.ResolvedJavaMethod;
  46 import jdk.vm.ci.meta.ResolvedJavaType;
  47 
  48 public final class HotSpotClassInitializationPlugin implements ClassInitializationPlugin {
  49     private static boolean shouldApply(GraphBuilderContext builder, ResolvedJavaType type) {
  50         if (!builder.parsingIntrinsic()) {
  51             if (!type.isArray()) {
  52                 ResolvedJavaMethod method = builder.getGraph().method();
  53                 ResolvedJavaType methodHolder = method.getDeclaringClass();
  54                 // We can elide initialization nodes if type >=: methodHolder.
  55                 // The type is already initialized by either "new" or "invokestatic".
  56 
  57                 // Emit initialization node if type is an interface since:
  58                 // JLS 12.4: Before a class is initialized, its direct superclass must be
  59                 // initialized, but interfaces implemented by the class are not
  60                 // initialized and a class or interface type T will be initialized
  61                 // immediately before the first occurrence of accesses listed
  62                 // in JLS 12.4.1.
  63 
  64                 return !type.isAssignableFrom(methodHolder) || type.isInterface();
  65             } else if (!type.getComponentType().isPrimitive()) {
  66                 // Always apply to object array types
  67                 return true;
  68             }
  69         }
  70         return false;
  71     }
  72 
  73     @Override
  74     public boolean apply(GraphBuilderContext builder, ResolvedJavaType type, Supplier<FrameState> frameState, ValueNode[] classInit) {
  75         if (shouldApply(builder, type)) {
  76             Stamp hubStamp = builder.getStampProvider().createHubStamp((ObjectStamp) StampFactory.objectNonNull());
  77             ConstantNode hub = builder.append(ConstantNode.forConstant(hubStamp, ((HotSpotResolvedObjectType) type).klass(), builder.getMetaAccess(), builder.getGraph()));
  78             DeoptimizingFixedWithNextNode result = builder.append(type.isArray() ? new ResolveConstantNode(hub) : new InitializeKlassNode(hub));
  79             result.setStateBefore(frameState.get());
  80             if (classInit != null) {
  81                 classInit[0] = result;
  82             }
  83             return true;
  84         }
  85         return false;
  86     }
  87 
  88     private static final Class<? extends ConstantPool> hscp;
  89     private static final Method loadReferencedTypeIIZMH;
  90 
  91     static {
  92         Method m = null;
  93         Class<? extends ConstantPool> c = null;
  94         try {
  95             c = Class.forName("jdk.vm.ci.hotspot.HotSpotConstantPool").asSubclass(ConstantPool.class);
  96             m = c.getDeclaredMethod("loadReferencedType", int.class, int.class, boolean.class);
  97         } catch (Exception e) {
  98             throw GraalError.shouldNotReachHere(e);
  99         }
 100         loadReferencedTypeIIZMH = m;
 101         hscp = c;
 102     }
 103 
 104     private static boolean isHotSpotConstantPool(ConstantPool cp) {
 105         // jdk.vm.ci.hotspot.HotSpotConstantPool is final, so we can
 106         // directly compare Classes.
 107         return cp.getClass() == hscp;
 108     }
 109 
 110     @Override
 111     public boolean supportsLazyInitialization(ConstantPool cp) {
 112         if (loadReferencedTypeIIZMH != null && isHotSpotConstantPool(cp)) {
 113             return true;
 114         }
 115         return false;
 116     }
 117 
 118     @Override
 119     public void loadReferencedType(GraphBuilderContext builder, ConstantPool cp, int cpi, int opcode) {
 120         if (loadReferencedTypeIIZMH != null && isHotSpotConstantPool(cp)) {
 121             try {
 122                 loadReferencedTypeIIZMH.invoke(cp, cpi, opcode, false);
 123             } catch (Throwable t) {
 124                 throw GraalError.shouldNotReachHere(t);
 125             }
 126         } else {
 127             cp.loadReferencedType(cpi, opcode);
 128         }
 129     }
 130 
 131 }