1 /*
   2  * Copyright (c) 2014, 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.core.match;
  26 
  27 import static org.graalvm.compiler.debug.DebugOptions.LogVerbose;
  28 
  29 import java.util.List;
  30 
  31 import org.graalvm.compiler.core.gen.NodeLIRBuilder;
  32 import org.graalvm.compiler.core.match.MatchPattern.MatchResultCode;
  33 import org.graalvm.compiler.core.match.MatchPattern.Result;
  34 import org.graalvm.compiler.debug.CounterKey;
  35 import org.graalvm.compiler.debug.DebugContext;
  36 import org.graalvm.compiler.graph.GraalGraphError;
  37 import org.graalvm.compiler.graph.Node;
  38 import org.graalvm.compiler.nodeinfo.Verbosity;
  39 
  40 import jdk.vm.ci.meta.Value;
  41 
  42 /**
  43  * A named {@link MatchPattern} along with a {@link MatchGenerator} that can be evaluated to replace
  44  * one or more {@link Node}s with a single {@link Value}.
  45  */
  46 
  47 public class MatchStatement {
  48     private static final CounterKey MatchStatementSuccess = DebugContext.counter("MatchStatementSuccess");
  49 
  50     /**
  51      * A printable name for this statement. Usually it's just the name of the method doing the
  52      * emission.
  53      */
  54     private final String name;
  55 
  56     /**
  57      * The actual match pattern.
  58      */
  59     private final MatchPattern pattern;
  60 
  61     /**
  62      * The method in the {@link NodeLIRBuilder} subclass that will actually do the code emission.
  63      */
  64     private MatchGenerator generatorMethod;
  65 
  66     /**
  67      * The name of arguments in the order they are expected to be passed to the generator method.
  68      */
  69     private String[] arguments;
  70 
  71     public MatchStatement(String name, MatchPattern pattern, MatchGenerator generator, String[] arguments) {
  72         this.name = name;
  73         this.pattern = pattern;
  74         this.generatorMethod = generator;
  75         this.arguments = arguments;
  76     }
  77 
  78     /**
  79      * Attempt to match the current statement against a Node.
  80      *
  81      * @param builder the current builder instance.
  82      * @param node the node to be matched
  83      * @param nodes the nodes in the current block
  84      * @return true if the statement matched something and set a {@link ComplexMatchResult} to be
  85      *         evaluated by the NodeLIRBuilder.
  86      */
  87     public boolean generate(NodeLIRBuilder builder, int index, Node node, List<Node> nodes) {
  88         DebugContext debug = node.getDebug();
  89         assert index == nodes.indexOf(node);
  90         // Check that the basic shape matches
  91         Result result = pattern.matchShape(node, this);
  92         if (result != Result.OK) {
  93             return false;
  94         }
  95         // Now ensure that the other safety constraints are matched.
  96         MatchContext context = new MatchContext(builder, this, index, node, nodes);
  97         result = pattern.matchUsage(node, context);
  98         if (result == Result.OK) {
  99             // Invoke the generator method and set the result if it's non null.
 100             ComplexMatchResult value = generatorMethod.match(builder.getNodeMatchRules(), buildArgList(context));
 101             if (value != null) {
 102                 context.setResult(value);
 103                 MatchStatementSuccess.increment(debug);
 104                 DebugContext.counter("MatchStatement[%s]", getName()).increment(debug);
 105                 return true;
 106             }
 107             // The pattern matched but some other code generation constraint disallowed code
 108             // generation for the pattern.
 109             if (LogVerbose.getValue(node.getOptions())) {
 110                 debug.log("while matching %s|%s %s %s returned null", context.getRoot().toString(Verbosity.Id), context.getRoot().getClass().getSimpleName(), getName(), generatorMethod.getName());
 111                 debug.log("with nodes %s", formatMatch(node));
 112             }
 113         } else {
 114             if (LogVerbose.getValue(node.getOptions()) && result.code != MatchResultCode.WRONG_CLASS) {
 115                 debug.log("while matching %s|%s %s %s", context.getRoot().toString(Verbosity.Id), context.getRoot().getClass().getSimpleName(), getName(), result);
 116             }
 117         }
 118         return false;
 119     }
 120 
 121     /**
 122      * @param context
 123      * @return the Nodes captured by the match rule in the order expected by the generatorMethod
 124      */
 125     private Object[] buildArgList(MatchContext context) {
 126         Object[] result = new Object[arguments.length];
 127         for (int i = 0; i < arguments.length; i++) {
 128             if ("root".equals(arguments[i])) {
 129                 result[i] = context.getRoot();
 130             } else {
 131                 result[i] = context.namedNode(arguments[i]);
 132                 if (result[i] == null) {
 133                     throw new GraalGraphError("Can't find named node %s", arguments[i]);
 134                 }
 135             }
 136         }
 137         return result;
 138     }
 139 
 140     public String formatMatch(Node root) {
 141         return pattern.formatMatch(root);
 142     }
 143 
 144     public MatchPattern getPattern() {
 145         return pattern;
 146     }
 147 
 148     public String getName() {
 149         return name;
 150     }
 151 
 152     @Override
 153     public String toString() {
 154         return pattern.toString();
 155     }
 156 }