changeset 12053:30ca2c3ad590

Merge.
author Doug Simon <doug.simon@oracle.com>
date Fri, 11 Oct 2013 12:20:09 +0200
parents 0afe7370260c (current diff) dcd412a084a2 (diff)
children bba234a1670e
files
diffstat 1 files changed, 160 insertions(+), 42 deletions(-) [+]
line wrap: on
line diff
--- a/graal/com.oracle.graal.graph/src/com/oracle/graal/graph/NodeClass.java	Fri Oct 11 12:19:09 2013 +0200
+++ b/graal/com.oracle.graal.graph/src/com/oracle/graal/graph/NodeClass.java	Fri Oct 11 12:20:09 2013 +0200
@@ -445,22 +445,19 @@
     }
 
     /**
-     * An iterator that will iterate over the fields given in {@link #offsets}. The first
-     * {@link #directCount} offsets are treated as fields of type {@link Node}, while the rest of
-     * the fields are treated as {@link NodeList}s. All elements of these NodeLists will be visited
-     * by the iterator as well. This iterator can be used to iterate over the inputs or successors
-     * of a node.
+     * An iterator that will iterate over the fields given in {@link #getOffsets()}. The first
+     * {@link #getDirectCount()} offsets are treated as fields of type {@link Node}, while the rest
+     * of the fields are treated as {@link NodeList}s. All elements of these NodeLists will be
+     * visited by the iterator as well. This iterator can be used to iterate over the inputs or
+     * successors of a node.
      * 
      * An iterator of this type will not return null values, unless the field values are modified
      * concurrently. Concurrent modifications are detected by an assertion on a best-effort basis.
      */
-    public static final class NodeClassIterator implements Iterator<Node> {
+    public abstract static class NodeClassIterator implements Iterator<Node> {
 
         private final NodeClass nodeClass;
-        private final Node node;
-        private final int modCount;
-        private final int directCount;
-        private final long[] offsets;
+        protected final Node node;
         private int index;
         private int subIndex;
 
@@ -468,26 +465,19 @@
          * Creates an iterator that will iterate over fields in the given node.
          * 
          * @param node the node which contains the fields.
-         * @param offsets the offsets of the fields.
-         * @param directCount the number of fields that should be treated as fields of type
-         *            {@link Node}, the rest are treated as {@link NodeList}s.
          */
-        private NodeClassIterator(Node node, long[] offsets, int directCount) {
+        NodeClassIterator(Node node) {
             this.node = node;
             this.nodeClass = node.getNodeClass();
-            this.modCount = MODIFICATION_COUNTS_ENABLED ? node.modCount() : 0;
-            this.offsets = offsets;
-            this.directCount = directCount;
             index = NOT_ITERABLE;
             subIndex = 0;
-            forward();
         }
 
-        private void forward() {
-            if (index < directCount) {
+        void forward() {
+            if (index < getDirectCount()) {
                 index++;
-                while (index < directCount) {
-                    Node element = getNode(node, offsets[index]);
+                while (index < getDirectCount()) {
+                    Node element = getNode(node, getOffsets()[index]);
                     if (element != null) {
                         return;
                     }
@@ -496,8 +486,8 @@
             } else {
                 subIndex++;
             }
-            while (index < offsets.length) {
-                NodeList<Node> list = getNodeList(node, offsets[index]);
+            while (index < getOffsets().length) {
+                NodeList<Node> list = getNodeList(node, getOffsets()[index]);
                 while (subIndex < list.size()) {
                     if (list.get(subIndex) != null) {
                         return;
@@ -510,10 +500,10 @@
         }
 
         private Node nextElement() {
-            if (index < directCount) {
-                return getNode(node, offsets[index]);
-            } else if (index < offsets.length) {
-                NodeList<Node> list = getNodeList(node, offsets[index]);
+            if (index < getDirectCount()) {
+                return getNode(node, getOffsets()[index]);
+            } else if (index < getOffsets().length) {
+                NodeList<Node> list = getNodeList(node, getOffsets()[index]);
                 return list.get(subIndex);
             }
             throw new NoSuchElementException();
@@ -521,8 +511,77 @@
 
         @Override
         public boolean hasNext() {
+            return index < getOffsets().length;
+        }
+
+        @Override
+        public Node next() {
             try {
-                return index < offsets.length;
+                return nextElement();
+            } finally {
+                forward();
+            }
+        }
+
+        public Position nextPosition() {
+            try {
+                if (index < getDirectCount()) {
+                    return new Position(getOffsets() == nodeClass.inputOffsets, index, NOT_ITERABLE);
+                } else {
+                    return new Position(getOffsets() == nodeClass.inputOffsets, index, subIndex);
+                }
+            } finally {
+                forward();
+            }
+        }
+
+        @Override
+        public void remove() {
+            throw new UnsupportedOperationException();
+        }
+
+        protected abstract int getDirectCount();
+
+        protected abstract long[] getOffsets();
+    }
+
+    private class NodeClassInputsIterator extends NodeClassIterator {
+        NodeClassInputsIterator(Node node) {
+            this(node, true);
+        }
+
+        NodeClassInputsIterator(Node node, boolean forward) {
+            super(node);
+            if (forward) {
+                forward();
+            }
+        }
+
+        @Override
+        protected int getDirectCount() {
+            return directInputCount;
+        }
+
+        @Override
+        protected long[] getOffsets() {
+            return inputOffsets;
+        }
+    }
+
+    private final class NodeClassInputsWithModCountIterator extends NodeClassInputsIterator {
+        private final int modCount;
+
+        private NodeClassInputsWithModCountIterator(Node node) {
+            super(node, false);
+            assert MODIFICATION_COUNTS_ENABLED;
+            this.modCount = node.modCount();
+            forward();
+        }
+
+        @Override
+        public boolean hasNext() {
+            try {
+                return super.hasNext();
             } finally {
                 assert modCount == node.modCount() : "must not be modified";
             }
@@ -531,29 +590,80 @@
         @Override
         public Node next() {
             try {
-                return nextElement();
+                return super.next();
             } finally {
-                forward();
-                assert modCount == node.modCount();
+                assert modCount == node.modCount() : "must not be modified";
             }
         }
 
+        @Override
         public Position nextPosition() {
             try {
-                if (index < directCount) {
-                    return new Position(offsets == nodeClass.inputOffsets, index, NOT_ITERABLE);
-                } else {
-                    return new Position(offsets == nodeClass.inputOffsets, index, subIndex);
-                }
+                return super.nextPosition();
             } finally {
+                assert modCount == node.modCount();
+            }
+        }
+    }
+
+    private class NodeClassSuccessorsIterator extends NodeClassIterator {
+        NodeClassSuccessorsIterator(Node node) {
+            this(node, true);
+        }
+
+        NodeClassSuccessorsIterator(Node node, boolean forward) {
+            super(node);
+            if (forward) {
                 forward();
-                assert modCount == node.modCount();
             }
         }
 
         @Override
-        public void remove() {
-            throw new UnsupportedOperationException();
+        protected int getDirectCount() {
+            return directSuccessorCount;
+        }
+
+        @Override
+        protected long[] getOffsets() {
+            return successorOffsets;
+        }
+    }
+
+    private final class NodeClassSuccessorsWithModCountIterator extends NodeClassSuccessorsIterator {
+        private final int modCount;
+
+        private NodeClassSuccessorsWithModCountIterator(Node node) {
+            super(node, false);
+            assert MODIFICATION_COUNTS_ENABLED;
+            this.modCount = node.modCount();
+            forward();
+        }
+
+        @Override
+        public boolean hasNext() {
+            try {
+                return super.hasNext();
+            } finally {
+                assert modCount == node.modCount() : "must not be modified";
+            }
+        }
+
+        @Override
+        public Node next() {
+            try {
+                return super.next();
+            } finally {
+                assert modCount == node.modCount() : "must not be modified";
+            }
+        }
+
+        @Override
+        public Position nextPosition() {
+            try {
+                return super.nextPosition();
+            } finally {
+                assert modCount == node.modCount();
+            }
         }
     }
 
@@ -808,7 +918,11 @@
 
             @Override
             public NodeClassIterator iterator() {
-                return new NodeClassIterator(node, inputOffsets, directInputCount);
+                if (MODIFICATION_COUNTS_ENABLED) {
+                    return new NodeClassInputsWithModCountIterator(node);
+                } else {
+                    return new NodeClassInputsIterator(node);
+                }
             }
 
             @Override
@@ -824,7 +938,11 @@
 
             @Override
             public NodeClassIterator iterator() {
-                return new NodeClassIterator(node, successorOffsets, directSuccessorCount);
+                if (MODIFICATION_COUNTS_ENABLED) {
+                    return new NodeClassSuccessorsWithModCountIterator(node);
+                } else {
+                    return new NodeClassSuccessorsIterator(node);
+                }
             }
 
             @Override