1 /*
   2  * Copyright (c) 2009, 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.java;
  26 
  27 import org.graalvm.compiler.core.common.spi.ConstantFieldProvider;
  28 import org.graalvm.compiler.nodes.StructuredGraph;
  29 import org.graalvm.compiler.nodes.graphbuilderconf.GraphBuilderConfiguration;
  30 import org.graalvm.compiler.nodes.graphbuilderconf.IntrinsicContext;
  31 import org.graalvm.compiler.nodes.spi.StampProvider;
  32 import org.graalvm.compiler.phases.BasePhase;
  33 import org.graalvm.compiler.phases.OptimisticOptimizations;
  34 import org.graalvm.compiler.phases.tiers.HighTierContext;
  35 
  36 import jdk.vm.ci.meta.ConstantReflectionProvider;
  37 import jdk.vm.ci.meta.MetaAccessProvider;
  38 import jdk.vm.ci.meta.ResolvedJavaMethod;
  39 
  40 /**
  41  * Parses the bytecodes of a method and builds the IR graph.
  42  */
  43 public class GraphBuilderPhase extends BasePhase<HighTierContext> {
  44 
  45     private final GraphBuilderConfiguration graphBuilderConfig;
  46 
  47     public GraphBuilderPhase(GraphBuilderConfiguration config) {
  48         this.graphBuilderConfig = config;
  49     }
  50 
  51     @Override
  52     public boolean checkContract() {
  53         return false;
  54     }
  55 
  56     @Override
  57     protected void run(StructuredGraph graph, HighTierContext context) {
  58         new Instance(context.getMetaAccess(), context.getStampProvider(), context.getConstantReflection(), context.getConstantFieldProvider(), graphBuilderConfig, context.getOptimisticOptimizations(),
  59                         null).run(graph);
  60     }
  61 
  62     public GraphBuilderConfiguration getGraphBuilderConfig() {
  63         return graphBuilderConfig;
  64     }
  65 
  66     // Fully qualified name is a workaround for JDK-8056066
  67     public static class Instance extends org.graalvm.compiler.phases.Phase {
  68 
  69         protected final MetaAccessProvider metaAccess;
  70         protected final StampProvider stampProvider;
  71         protected final ConstantReflectionProvider constantReflection;
  72         protected final ConstantFieldProvider constantFieldProvider;
  73         protected final GraphBuilderConfiguration graphBuilderConfig;
  74         protected final OptimisticOptimizations optimisticOpts;
  75         private final IntrinsicContext initialIntrinsicContext;
  76 
  77         public Instance(MetaAccessProvider metaAccess, StampProvider stampProvider, ConstantReflectionProvider constantReflection, ConstantFieldProvider constantFieldProvider,
  78                         GraphBuilderConfiguration graphBuilderConfig, OptimisticOptimizations optimisticOpts, IntrinsicContext initialIntrinsicContext) {
  79             this.graphBuilderConfig = graphBuilderConfig;
  80             this.optimisticOpts = optimisticOpts;
  81             this.metaAccess = metaAccess;
  82             this.stampProvider = stampProvider;
  83             this.constantReflection = constantReflection;
  84             this.constantFieldProvider = constantFieldProvider;
  85             this.initialIntrinsicContext = initialIntrinsicContext;
  86         }
  87 
  88         @Override
  89         public boolean checkContract() {
  90             return false;
  91         }
  92 
  93         @Override
  94         protected void run(StructuredGraph graph) {
  95             createBytecodeParser(graph, null, graph.method(), graph.getEntryBCI(), initialIntrinsicContext).buildRootMethod();
  96         }
  97 
  98         /* Hook for subclasses of Instance to provide a subclass of BytecodeParser. */
  99         protected BytecodeParser createBytecodeParser(StructuredGraph graph, BytecodeParser parent, ResolvedJavaMethod method, int entryBCI, IntrinsicContext intrinsicContext) {
 100             return new BytecodeParser(this, graph, parent, method, entryBCI, intrinsicContext);
 101         }
 102     }
 103 }