comparison graal/com.oracle.truffle.api/src/com/oracle/truffle/api/nodes/NodeUtil.java @ 13811:641f22b1c6b8

Truffle: further fixes to the new CallNode.
author Christian Humer <christian.humer@gmail.com>
date Wed, 29 Jan 2014 20:49:09 +0100
parents d9c34e8337f4
children f46cab39a9a2
comparison
equal deleted inserted replaced
13810:44bcfc983adb 13811:641f22b1c6b8
449 } 449 }
450 } 450 }
451 return null; 451 return null;
452 } 452 }
453 453
454 /**
455 * Returns the outermost not inlined {@link RootNode} which is a parent of this node.
456 *
457 * @see RootNode#getParentInlinedCall()
458 * @param node to search
459 * @return the outermost {@link RootNode}
460 */
461 public static RootNode findOutermostRootNode(Node node) {
462 Node parent = node;
463 while (parent != null) {
464 if (parent instanceof RootNode) {
465 RootNode root = (RootNode) parent;
466 Node next = root.getParentInlinedCall();
467 if (next != null) {
468 parent = next;
469 } else {
470 return root;
471 }
472 } else {
473 parent = parent.getParent();
474 }
475 }
476 return null;
477 }
478
454 public static <T> T findParent(Node start, Class<T> clazz) { 479 public static <T> T findParent(Node start, Class<T> clazz) {
455 Node parent = start.getParent(); 480 Node parent = start.getParent();
456 if (parent == null) { 481 if (parent == null) {
457 return null; 482 return null;
458 } else if (clazz.isInstance(parent)) { 483 } else if (clazz.isInstance(parent)) {
569 }); 594 });
570 return nodeList; 595 return nodeList;
571 } 596 }
572 597
573 public static int countNodes(Node root) { 598 public static int countNodes(Node root) {
574 return countNodes(root, null); 599 return countNodes(root, null, false);
575 } 600 }
576 601
577 public static int countNodes(Node root, Class<?> clazz) { 602 public static int countNodes(Node root, Class<?> clazz, boolean countInlinedCallNodes) {
578 NodeCountVisitor nodeCount = new NodeCountVisitor(root, clazz); 603 NodeCountVisitor nodeCount = new NodeCountVisitor(root, clazz, countInlinedCallNodes);
579 root.accept(nodeCount); 604 root.accept(nodeCount);
580 return nodeCount.nodeCount; 605 return nodeCount.nodeCount;
581 } 606 }
582 607
583 private static final class NodeCountVisitor implements NodeVisitor { 608 private static final class NodeCountVisitor implements NodeVisitor {
584 609
610 private Node root;
611 private boolean inspectInlinedCalls;
585 int nodeCount; 612 int nodeCount;
586 private final Node root;
587 private final Class<?> clazz; 613 private final Class<?> clazz;
588 614
589 private NodeCountVisitor(Node root, Class<?> clazz) { 615 private NodeCountVisitor(Node root, Class<?> clazz, boolean inspectInlinedCalls) {
590 this.root = root; 616 this.root = root;
591 this.clazz = clazz; 617 this.clazz = clazz;
618 this.inspectInlinedCalls = inspectInlinedCalls;
592 } 619 }
593 620
594 @Override 621 @Override
595 public boolean visit(Node node) { 622 public boolean visit(Node node) {
596 if (node instanceof RootNode && node != root) { 623 if (node instanceof RootNode && node != root) {
597 return false; 624 return false;
598 } 625 }
626
599 if (clazz == null || clazz.isInstance(node)) { 627 if (clazz == null || clazz.isInstance(node)) {
600 nodeCount++; 628 nodeCount++;
601 } 629 }
630
631 if (inspectInlinedCalls && node instanceof CallNode) {
632 CallNode call = (CallNode) node;
633 if (call.isInlined()) {
634 call.getInlinedRoot().getChildren().iterator().next().accept(this);
635 }
636 }
637
602 return true; 638 return true;
603 } 639 }
604 } 640 }
605 641
606 public static String printCompactTreeToString(Node node) { 642 public static String printCompactTreeToString(Node node) {