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