1 /*
   2  * Copyright (c) 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 
  24 
  25 package org.graalvm.compiler.hotspot.meta;
  26 
  27 import static org.graalvm.compiler.bytecode.Bytecodes.GETSTATIC;
  28 import static org.graalvm.compiler.bytecode.Bytecodes.INVOKESTATIC;
  29 import static org.graalvm.compiler.bytecode.Bytecodes.NEW;
  30 import static org.graalvm.compiler.bytecode.Bytecodes.PUTSTATIC;
  31 
  32 import java.util.function.Supplier;
  33 
  34 import org.graalvm.compiler.hotspot.nodes.KlassBeingInitializedCheckNode;
  35 import org.graalvm.compiler.hotspot.nodes.type.KlassPointerStamp;
  36 import org.graalvm.compiler.nodes.ConstantNode;
  37 import org.graalvm.compiler.nodes.FrameState;
  38 import org.graalvm.compiler.nodes.ValueNode;
  39 import org.graalvm.compiler.nodes.graphbuilderconf.ClassInitializationPlugin;
  40 import org.graalvm.compiler.nodes.graphbuilderconf.GraphBuilderContext;
  41 
  42 import jdk.vm.ci.hotspot.HotSpotResolvedObjectType;
  43 import jdk.vm.ci.meta.ConstantPool;
  44 import jdk.vm.ci.meta.ResolvedJavaType;
  45 
  46 public final class HotSpotJITClassInitializationPlugin implements ClassInitializationPlugin {
  47     @Override
  48     public boolean apply(GraphBuilderContext builder, ResolvedJavaType type, Supplier<FrameState> frameState, ValueNode[] classInit) {
  49         if (!type.isInitialized() && (type.isInstanceClass() || type.isInterface())) {
  50             int code = builder.getCode().getCode()[builder.bci()] & 0xff;
  51             switch (code) {
  52                 case INVOKESTATIC:
  53                 case GETSTATIC:
  54                 case PUTSTATIC:
  55                 case NEW:
  56                     ValueNode typeConstant = ConstantNode.forConstant(KlassPointerStamp.klass(), ((HotSpotResolvedObjectType) type).klass(), builder.getMetaAccess());
  57                     builder.add(new KlassBeingInitializedCheckNode(typeConstant));
  58                     return true;
  59                 default:
  60 
  61             }
  62         }
  63         return false;
  64     }
  65 
  66     @Override
  67     public boolean supportsLazyInitialization(ConstantPool cp) {
  68         return false;
  69     }
  70 
  71     @Override
  72     public void loadReferencedType(GraphBuilderContext builder, ConstantPool cp, int cpi, int opcode) {
  73         cp.loadReferencedType(cpi, opcode);
  74     }
  75 
  76 }