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