# HG changeset patch # User Tom Rodriguez # Date 1400224368 25200 # Node ID 98423229008c43d0a0866efd841e9b4d7a257a41 # Parent 423bf61c9c32dc75166e104b66b95b8b85792dff allow overriding the NodeClass lookup when building MatchStatements diff -r 423bf61c9c32 -r 98423229008c graal/com.oracle.graal.compiler/src/com/oracle/graal/compiler/match/MatchPattern.java --- a/graal/com.oracle.graal.compiler/src/com/oracle/graal/compiler/match/MatchPattern.java Fri May 16 00:12:41 2014 -0700 +++ b/graal/com.oracle.graal.compiler/src/com/oracle/graal/compiler/match/MatchPattern.java Fri May 16 00:12:48 2014 -0700 @@ -22,7 +22,6 @@ */ package com.oracle.graal.compiler.match; -import com.oracle.graal.compiler.common.*; import com.oracle.graal.debug.*; import com.oracle.graal.graph.Node.Verbosity; import com.oracle.graal.graph.*; @@ -197,36 +196,6 @@ return result; } - /** - * Convert a list of field names into {@link com.oracle.graal.graph.NodeClass.Position} objects - * that can be used to read them during a match. The names should already have been confirmed to - * exist in the type. - * - * @param theClass - * @param names - * @return an array of Position objects corresponding to the named fields. - */ - public static NodeClass.Position[] findPositions(Class theClass, String[] names) { - NodeClass.Position[] result = new NodeClass.Position[names.length]; - NodeClass nodeClass = NodeClass.get(theClass); - for (int i = 0; i < names.length; i++) { - for (NodeClass.Position position : nodeClass.getFirstLevelInputPositions()) { - String name = nodeClass.getName(position); - if (name.endsWith("#NDF")) { - name = name.substring(0, name.length() - 4); - } - if (name.equals(names[i])) { - result[i] = position; - break; - } - } - if (result[i] == null) { - throw new GraalInternalError("unknown field \"%s\" in class %s", names[i], theClass); - } - } - return result; - } - private Result matchUsage(ValueNode node, MatchContext context, boolean atRoot) { Result result = matchType(node); if (result != Result.OK) { diff -r 423bf61c9c32 -r 98423229008c graal/com.oracle.graal.compiler/src/com/oracle/graal/compiler/match/MatchProcessor.java --- a/graal/com.oracle.graal.compiler/src/com/oracle/graal/compiler/match/MatchProcessor.java Fri May 16 00:12:41 2014 -0700 +++ b/graal/com.oracle.graal.compiler/src/com/oracle/graal/compiler/match/MatchProcessor.java Fri May 16 00:12:48 2014 -0700 @@ -456,7 +456,7 @@ } String generatePositionDeclaration() { - return String.format("private static final NodeClass.Position[] %s_positions = MatchPattern.findPositions(%s.class, new String[]{\"%s\"});", nodeType.nodeClass, nodeType.nodeClass, + return String.format("NodeClass.Position[] %s_positions = MatchRuleRegistry.findPositions(lookup, %s.class, new String[]{\"%s\"});", nodeType.nodeClass, nodeType.nodeClass, String.join("\", \"", nodeType.inputs)); } } @@ -528,33 +528,36 @@ } - for (String positionDeclaration : info.positionDeclarations) { - out.println(" " + positionDeclaration); - } - out.println(); - String desc = MatchStatement.class.getSimpleName(); - out.println(" // CheckStyle: stop line length check"); - out.println(" private static final List<" + desc + "> statements = Collections.unmodifiableList(Arrays.asList("); - - int i = 0; - for (MatchRuleItem matchRule : info.matchRules) { - String comma = i == info.matchRules.size() - 1 ? "" : ","; - out.printf(" %s%s\n", matchRule.ruleBuilder(), comma); - i++; - } - out.println(" ));"); - out.println(" // CheckStyle: resume line length check"); - out.println(); out.println(" public Class forClass() {"); out.println(" return " + topDeclaringClass + ".class;"); out.println(" }"); out.println(); out.println(" @Override"); - out.println(" public List<" + desc + "> statements() {"); + out.println(" public List<" + desc + "> statements(MatchRuleRegistry.NodeClassLookup lookup) {"); + + for (String positionDeclaration : info.positionDeclarations) { + out.println(" " + positionDeclaration); + } + out.println(); + + out.println(" // CheckStyle: stop line length check"); + out.println(" List<" + desc + "> statements = Collections.unmodifiableList(Arrays.asList("); + + int i = 0; + for (MatchRuleItem matchRule : info.matchRules) { + String comma = i == info.matchRules.size() - 1 ? "" : ","; + out.printf(" %s%s\n", matchRule.ruleBuilder(), comma); + i++; + } + out.println(" ));"); + out.println(" // CheckStyle: resume line length check"); out.println(" return statements;"); out.println(" }"); + + out.println(); + out.println("}"); } diff -r 423bf61c9c32 -r 98423229008c graal/com.oracle.graal.compiler/src/com/oracle/graal/compiler/match/MatchRuleRegistry.java --- a/graal/com.oracle.graal.compiler/src/com/oracle/graal/compiler/match/MatchRuleRegistry.java Fri May 16 00:12:41 2014 -0700 +++ b/graal/com.oracle.graal.compiler/src/com/oracle/graal/compiler/match/MatchRuleRegistry.java Fri May 16 00:12:48 2014 -0700 @@ -27,13 +27,61 @@ import java.util.*; import java.util.Map.Entry; +import com.oracle.graal.compiler.common.*; import com.oracle.graal.compiler.gen.*; import com.oracle.graal.debug.*; import com.oracle.graal.debug.Debug.Scope; +import com.oracle.graal.graph.*; import com.oracle.graal.nodes.*; public class MatchRuleRegistry { + /** + * Helper interface for mapping between Class and NodeClass. In static compilation environments, + * the current NodeClass might not be the same NodeClass used in the target so this provides a + * level of indirection. + */ + public static interface NodeClassLookup { + NodeClass get(Class theClass); + + } + + static class DefaultNodeClassLookup implements NodeClassLookup { + public NodeClass get(Class theClass) { + return NodeClass.get(theClass); + } + } + + /** + * Convert a list of field names into {@link com.oracle.graal.graph.NodeClass.Position} objects + * that can be used to read them during a match. The names should already have been confirmed to + * exist in the type. + * + * @param theClass + * @param names + * @return an array of Position objects corresponding to the named fields. + */ + public static NodeClass.Position[] findPositions(NodeClassLookup lookup, Class theClass, String[] names) { + NodeClass.Position[] result = new NodeClass.Position[names.length]; + NodeClass nodeClass = lookup.get(theClass); + for (int i = 0; i < names.length; i++) { + for (NodeClass.Position position : nodeClass.getFirstLevelInputPositions()) { + String name = nodeClass.getName(position); + if (name.endsWith("#NDF")) { + name = name.substring(0, name.length() - 4); + } + if (name.equals(names[i])) { + result[i] = position; + break; + } + } + if (result[i] == null) { + throw new GraalInternalError("unknown field \"%s\" in class %s", names[i], theClass); + } + } + return result; + } + private static final HashMap, Map, List>> registry = new HashMap<>(); /** @@ -46,10 +94,11 @@ Map, List> result = registry.get(theClass); if (result == null) { + NodeClassLookup lookup = new DefaultNodeClassLookup(); HashMap, List> localRules = new HashMap<>(); ServiceLoader sl = ServiceLoader.loadInstalled(MatchStatementSet.class); for (MatchStatementSet rules : sl) { - localRules.put(rules.forClass(), rules.statements()); + localRules.put(rules.forClass(), rules.statements(lookup)); } // Walk the class hierarchy collecting lists and merge them together. The subclass diff -r 423bf61c9c32 -r 98423229008c graal/com.oracle.graal.compiler/src/com/oracle/graal/compiler/match/MatchStatementSet.java --- a/graal/com.oracle.graal.compiler/src/com/oracle/graal/compiler/match/MatchStatementSet.java Fri May 16 00:12:41 2014 -0700 +++ b/graal/com.oracle.graal.compiler/src/com/oracle/graal/compiler/match/MatchStatementSet.java Fri May 16 00:12:48 2014 -0700 @@ -36,5 +36,5 @@ /** * @return the {@link MatchStatement}s available for this {@link NodeLIRBuilder} subclass. */ - public List statements(); + public List statements(MatchRuleRegistry.NodeClassLookup lookup); }