Mercurial > hg > truffle
comparison graal/com.oracle.truffle.api/src/com/oracle/truffle/api/nodes/NodeUtil.java @ 14565:9c01fabfb167
Truffle: Removed deprecated API; Added NodeFilter to customize filter when counting nodes.
author | Christian Humer <christian.humer@gmail.com> |
---|---|
date | Mon, 17 Mar 2014 14:29:45 +0100 |
parents | 5d1308c78ddc |
children | 5e04917e6616 |
comparison
equal
deleted
inserted
replaced
14564:5d1308c78ddc | 14565:9c01fabfb167 |
---|---|
449 } | 449 } |
450 } | 450 } |
451 return null; | 451 return null; |
452 } | 452 } |
453 | 453 |
454 /** | |
455 * @deprecated will be removed, exposed Truffle runtime specific functionality. | |
456 */ | |
457 @Deprecated | |
458 public static List<CallTarget> findOutermostCallTargets(Node node) { | |
459 RootNode root = node.getRootNode(); | |
460 if (root == null) { | |
461 return Collections.emptyList(); | |
462 } | |
463 List<CallTarget> roots = new ArrayList<>(); | |
464 roots.add(root.getCallTarget()); | |
465 for (CallNode callNode : root.getCachedCallNodes()) { | |
466 if (callNode.isInlined()) { | |
467 roots.addAll(findOutermostCallTargets(callNode)); | |
468 } | |
469 } | |
470 return roots; | |
471 } | |
472 | |
473 /** | |
474 * @deprecated will be removed, exposed Truffle runtime specific functionality. | |
475 */ | |
476 @Deprecated | |
477 public static RootNode findOutermostRootNode(Node node) { | |
478 Node parent = node; | |
479 while (parent != null) { | |
480 if (parent instanceof RootNode) { | |
481 RootNode root = (RootNode) parent; | |
482 @SuppressWarnings("deprecation") | |
483 Node next = root.getParentInlinedCall(); | |
484 if (next != null) { | |
485 parent = next; | |
486 } else { | |
487 return root; | |
488 } | |
489 } else { | |
490 parent = parent.getParent(); | |
491 } | |
492 } | |
493 return null; | |
494 } | |
495 | |
496 public static <T> T findParent(Node start, Class<T> clazz) { | 454 public static <T> T findParent(Node start, Class<T> clazz) { |
497 Node parent = start.getParent(); | 455 Node parent = start.getParent(); |
498 if (parent == null) { | 456 if (parent == null) { |
499 return null; | 457 return null; |
500 } else if (clazz.isInstance(parent)) { | 458 } else if (clazz.isInstance(parent)) { |
611 }); | 569 }); |
612 return nodeList; | 570 return nodeList; |
613 } | 571 } |
614 | 572 |
615 public static int countNodes(Node root) { | 573 public static int countNodes(Node root) { |
616 return countNodes(root, null, null, false); | 574 return countNodes(root, null, false); |
617 } | 575 } |
618 | 576 |
619 public static int countNodes(Node root, Class<?> clazz, NodeCost nodeKind, boolean countInlinedCallNodes) { | 577 public static int countNodes(Node root, NodeFilter filter) { |
620 NodeCountVisitor nodeCount = new NodeCountVisitor(clazz, nodeKind, countInlinedCallNodes); | 578 return countNodes(root, filter, false); |
579 } | |
580 | |
581 public static int countNodes(Node root, NodeFilter filter, boolean visitInlinedCallNodes) { | |
582 NodeCountVisitor nodeCount = new NodeCountVisitor(filter, visitInlinedCallNodes); | |
621 root.accept(nodeCount); | 583 root.accept(nodeCount); |
622 return nodeCount.nodeCount; | 584 return nodeCount.nodeCount; |
623 } | 585 } |
624 | 586 |
625 public static int countNodes(Node root, Class<?> clazz, boolean countInlinedCallNodes) { | 587 public interface NodeFilter { |
626 return countNodes(root, clazz, null, countInlinedCallNodes); | 588 |
589 boolean isFiltered(Node node); | |
590 | |
627 } | 591 } |
628 | 592 |
629 private static final class NodeCountVisitor implements NodeVisitor { | 593 private static final class NodeCountVisitor implements NodeVisitor { |
630 | 594 |
631 private final boolean inspectInlinedCalls; | 595 private final boolean visitInlinedCallNodes; |
632 int nodeCount; | 596 int nodeCount; |
633 private final NodeCost cost; | 597 private final NodeFilter filter; |
634 private final Class<?> clazz; | 598 |
635 | 599 private NodeCountVisitor(NodeFilter filter, boolean visitInlinedCallNodes) { |
636 private NodeCountVisitor(Class<?> clazz, NodeCost cost, boolean inspectInlinedCalls) { | 600 this.filter = filter; |
637 this.clazz = clazz; | 601 this.visitInlinedCallNodes = visitInlinedCallNodes; |
638 this.cost = cost; | |
639 this.inspectInlinedCalls = inspectInlinedCalls; | |
640 } | 602 } |
641 | 603 |
642 @Override | 604 @Override |
643 public boolean visit(Node node) { | 605 public boolean visit(Node node) { |
644 if ((clazz == null || clazz.isInstance(node)) && (cost == null || isKind(node))) { | 606 if (filter == null || !filter.isFiltered(node)) { |
645 nodeCount++; | 607 nodeCount++; |
646 } | 608 } |
647 | 609 |
648 if (inspectInlinedCalls && node instanceof CallNode) { | 610 if (visitInlinedCallNodes && node instanceof CallNode) { |
649 CallNode call = (CallNode) node; | 611 CallNode call = (CallNode) node; |
650 if (call.isInlined()) { | 612 if (call.isInlined()) { |
651 Node target = ((RootCallTarget) call.getCurrentCallTarget()).getRootNode(); | 613 Node target = ((RootCallTarget) call.getCurrentCallTarget()).getRootNode(); |
652 if (target != null) { | 614 if (target != null) { |
653 target.accept(this); | 615 target.accept(this); |
656 } | 618 } |
657 | 619 |
658 return true; | 620 return true; |
659 } | 621 } |
660 | 622 |
661 private boolean isKind(Node n) { | |
662 return cost == n.getCost(); | |
663 } | |
664 } | 623 } |
665 | 624 |
666 public static void printInliningTree(final PrintStream stream, RootNode root) { | 625 public static void printInliningTree(final PrintStream stream, RootNode root) { |
667 printRootNode(stream, 0, root); | 626 printRootNode(stream, 0, root); |
668 root.accept(new NodeVisitor() { | 627 root.accept(new NodeVisitor() { |