43972
|
1 |
/*
|
|
2 |
* Copyright (c) 2014, 2015, 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 |
package org.graalvm.compiler.core.match;
|
|
24 |
|
46640
|
25 |
import static org.graalvm.compiler.debug.DebugOptions.LogVerbose;
|
|
26 |
|
43972
|
27 |
import java.util.ArrayList;
|
|
28 |
import java.util.List;
|
|
29 |
|
48861
|
30 |
import org.graalvm.collections.EconomicMap;
|
|
31 |
import org.graalvm.collections.Equivalence;
|
|
32 |
import org.graalvm.collections.MapCursor;
|
43972
|
33 |
import org.graalvm.compiler.core.gen.NodeMatchRules;
|
46640
|
34 |
import org.graalvm.compiler.debug.DebugContext;
|
43972
|
35 |
import org.graalvm.compiler.debug.GraalError;
|
|
36 |
import org.graalvm.compiler.graph.Edges;
|
|
37 |
import org.graalvm.compiler.graph.Node;
|
|
38 |
import org.graalvm.compiler.graph.NodeClass;
|
|
39 |
import org.graalvm.compiler.graph.Position;
|
46344
|
40 |
import org.graalvm.compiler.options.OptionValues;
|
43972
|
41 |
import org.graalvm.compiler.serviceprovider.GraalServices;
|
|
42 |
|
|
43 |
public class MatchRuleRegistry {
|
|
44 |
|
|
45 |
/**
|
|
46 |
* Convert a list of field names into {@link org.graalvm.compiler.graph.Position} objects that
|
|
47 |
* can be used to read them during a match. The names should already have been confirmed to
|
|
48 |
* exist in the type.
|
|
49 |
*
|
|
50 |
* @param nodeClass
|
|
51 |
* @param names
|
|
52 |
* @return an array of Position objects corresponding to the named fields.
|
|
53 |
*/
|
|
54 |
public static Position[] findPositions(NodeClass<? extends Node> nodeClass, String[] names) {
|
|
55 |
Position[] result = new Position[names.length];
|
|
56 |
for (int i = 0; i < names.length; i++) {
|
|
57 |
Edges edges = nodeClass.getInputEdges();
|
|
58 |
for (int e = 0; e < edges.getDirectCount(); e++) {
|
|
59 |
if (names[i].equals(edges.getName(e))) {
|
|
60 |
result[i] = new Position(edges, e, Node.NOT_ITERABLE);
|
|
61 |
}
|
|
62 |
}
|
|
63 |
if (result[i] == null) {
|
|
64 |
throw new GraalError("unknown field \"%s\" in class %s", names[i], nodeClass);
|
|
65 |
}
|
|
66 |
}
|
|
67 |
return result;
|
|
68 |
}
|
|
69 |
|
46344
|
70 |
private static final EconomicMap<Class<? extends NodeMatchRules>, EconomicMap<Class<? extends Node>, List<MatchStatement>>> registry = EconomicMap.create(Equivalence.IDENTITY);
|
43972
|
71 |
|
|
72 |
/**
|
|
73 |
* Collect all the {@link MatchStatement}s defined by the superclass chain of theClass.
|
|
74 |
*
|
|
75 |
* @param theClass
|
46344
|
76 |
* @param options
|
43972
|
77 |
* @return the set of {@link MatchStatement}s applicable to theClass.
|
|
78 |
*/
|
|
79 |
@SuppressWarnings("try")
|
46640
|
80 |
public static synchronized EconomicMap<Class<? extends Node>, List<MatchStatement>> lookup(Class<? extends NodeMatchRules> theClass, OptionValues options, DebugContext debug) {
|
46344
|
81 |
EconomicMap<Class<? extends Node>, List<MatchStatement>> result = registry.get(theClass);
|
43972
|
82 |
|
|
83 |
if (result == null) {
|
46344
|
84 |
EconomicMap<Class<? extends Node>, List<MatchStatement>> rules = createRules(theClass);
|
43972
|
85 |
registry.put(theClass, rules);
|
|
86 |
assert registry.get(theClass) == rules;
|
|
87 |
result = rules;
|
|
88 |
|
46344
|
89 |
if (LogVerbose.getValue(options)) {
|
46640
|
90 |
try (DebugContext.Scope s = debug.scope("MatchComplexExpressions")) {
|
|
91 |
debug.log("Match rules for %s", theClass.getSimpleName());
|
46344
|
92 |
MapCursor<Class<? extends Node>, List<MatchStatement>> cursor = result.getEntries();
|
|
93 |
while (cursor.advance()) {
|
46640
|
94 |
debug.log(" For node class: %s", cursor.getKey());
|
46344
|
95 |
for (MatchStatement statement : cursor.getValue()) {
|
46640
|
96 |
debug.log(" %s", statement.getPattern());
|
43972
|
97 |
}
|
|
98 |
}
|
|
99 |
}
|
|
100 |
}
|
|
101 |
}
|
|
102 |
|
|
103 |
if (result.size() == 0) {
|
|
104 |
return null;
|
|
105 |
}
|
|
106 |
return result;
|
|
107 |
}
|
|
108 |
|
|
109 |
/*
|
|
110 |
* This is a separate, public method so that external clients can create rules with a custom
|
|
111 |
* lookup and without the default caching behavior.
|
|
112 |
*/
|
46344
|
113 |
@SuppressWarnings("unchecked")
|
|
114 |
public static EconomicMap<Class<? extends Node>, List<MatchStatement>> createRules(Class<? extends NodeMatchRules> theClass) {
|
|
115 |
EconomicMap<Class<? extends NodeMatchRules>, MatchStatementSet> matchSets = EconomicMap.create(Equivalence.IDENTITY);
|
43972
|
116 |
Iterable<MatchStatementSet> sl = GraalServices.load(MatchStatementSet.class);
|
|
117 |
for (MatchStatementSet rules : sl) {
|
|
118 |
matchSets.put(rules.forClass(), rules);
|
|
119 |
}
|
|
120 |
|
|
121 |
// Walk the class hierarchy collecting lists and merge them together. The subclass
|
|
122 |
// rules are first which gives them preference over earlier rules.
|
46344
|
123 |
EconomicMap<Class<? extends Node>, List<MatchStatement>> rules = EconomicMap.create(Equivalence.IDENTITY);
|
|
124 |
Class<? extends NodeMatchRules> currentClass = theClass;
|
43972
|
125 |
do {
|
|
126 |
MatchStatementSet matchSet = matchSets.get(currentClass);
|
|
127 |
if (matchSet != null) {
|
|
128 |
List<MatchStatement> statements = matchSet.statements();
|
|
129 |
for (MatchStatement statement : statements) {
|
|
130 |
Class<? extends Node> nodeClass = statement.getPattern().nodeClass();
|
|
131 |
List<MatchStatement> current = rules.get(nodeClass);
|
|
132 |
if (current == null) {
|
|
133 |
current = new ArrayList<>();
|
|
134 |
rules.put(nodeClass, current);
|
|
135 |
}
|
|
136 |
current.add(statement);
|
|
137 |
}
|
|
138 |
}
|
46344
|
139 |
currentClass = (Class<? extends NodeMatchRules>) currentClass.getSuperclass();
|
43972
|
140 |
} while (currentClass != NodeMatchRules.class);
|
|
141 |
return rules;
|
|
142 |
}
|
|
143 |
}
|