001/*
002 * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.
003 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004 *
005 * This code is free software; you can redistribute it and/or modify it
006 * under the terms of the GNU General Public License version 2 only, as
007 * published by the Free Software Foundation.
008 *
009 * This code is distributed in the hope that it will be useful, but WITHOUT
010 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
011 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
012 * version 2 for more details (a copy is included in the LICENSE file that
013 * accompanied this code).
014 *
015 * You should have received a copy of the GNU General Public License version
016 * 2 along with this work; if not, write to the Free Software Foundation,
017 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
018 *
019 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
020 * or visit www.oracle.com if you need additional information or have any
021 * questions.
022 */
023package com.oracle.graal.truffle.debug;
024
025import static com.oracle.graal.truffle.TruffleCompilerOptions.*;
026
027import java.util.*;
028
029import jdk.internal.jvmci.code.*;
030
031import com.oracle.graal.nodes.*;
032import com.oracle.graal.truffle.*;
033import com.oracle.graal.truffle.TruffleInlining.CallTreeNodeVisitor;
034import com.oracle.truffle.api.nodes.*;
035
036public final class TraceCompilationASTListener extends AbstractDebugCompilationListener {
037
038    public static void install(GraalTruffleRuntime runtime) {
039        if (TraceTruffleCompilationAST.getValue()) {
040            runtime.addCompilationListener(new TraceCompilationASTListener());
041        }
042    }
043
044    @Override
045    public void notifyCompilationSuccess(OptimizedCallTarget target, StructuredGraph graph, CompilationResult result) {
046        log(target, 0, "opt AST", target.toString(), target.getDebugProperties());
047        printCompactTree(target);
048    }
049
050    private static void printCompactTree(OptimizedCallTarget target) {
051        target.accept(new CallTreeNodeVisitor() {
052
053            public boolean visit(List<TruffleInlining> decisionStack, Node node) {
054                if (node == null) {
055                    return true;
056                }
057                int level = CallTreeNodeVisitor.getNodeDepth(decisionStack, node);
058                StringBuilder indent = new StringBuilder();
059                for (int i = 0; i < level; i++) {
060                    indent.append("  ");
061                }
062                Node parent = node.getParent();
063
064                if (parent == null) {
065                    target.log(String.format("%s%s", indent, node.getClass().getSimpleName()));
066                } else {
067                    String fieldName = "unknownField";
068                    NodeFieldAccessor[] fields = NodeClass.get(parent).getFields();
069                    for (NodeFieldAccessor field : fields) {
070                        Object value = field.loadValue(parent);
071                        if (value == node) {
072                            fieldName = field.getName();
073                            break;
074                        } else if (value instanceof Node[]) {
075                            int index = 0;
076                            for (Node arrayNode : (Node[]) value) {
077                                if (arrayNode == node) {
078                                    fieldName = field.getName() + "[" + index + "]";
079                                    break;
080                                }
081                                index++;
082                            }
083                        }
084                    }
085                    target.log(String.format("%s%s = %s", indent, fieldName, node.getClass().getSimpleName()));
086                }
087                return true;
088            }
089
090        }, true);
091    }
092}