001/*
002 * Copyright (c) 2009, 2011, 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.compiler.gen;
024
025import static com.oracle.graal.compiler.gen.InstructionPrinter.InstructionLineColumn.*;
026
027import com.oracle.graal.debug.*;
028import com.oracle.graal.nodes.*;
029
030/**
031 * A utility for {@linkplain #printInstruction(ValueNode) printing} a node as an expression or
032 * statement.
033 */
034public class InstructionPrinter {
035
036    /**
037     * The columns printed in a tabulated instruction
038     * {@linkplain InstructionPrinter#printInstructionListing(ValueNode) listing}.
039     */
040    public enum InstructionLineColumn {
041        /**
042         * The instruction's bytecode index.
043         */
044        BCI(2, "bci"),
045
046        /**
047         * The instruction's use count.
048         */
049        USE(7, "use"),
050
051        /**
052         * The instruction as a value.
053         */
054        VALUE(12, "tid"),
055
056        /**
057         * The instruction formatted as an expression or statement.
058         */
059        INSTRUCTION(19, "instr"),
060
061        END(60, "");
062
063        final int position;
064        final String label;
065
066        private InstructionLineColumn(int position, String label) {
067            this.position = position;
068            this.label = label;
069        }
070
071        /**
072         * Prints this column's label to a given stream after padding the stream with '_' characters
073         * until its {@linkplain LogStream#position() position} is equal to this column's position.
074         *
075         * @param out the print stream
076         */
077        public void printLabel(LogStream out) {
078            out.fillTo(position + out.indentationLevel(), '_');
079            out.print(label);
080        }
081
082        /**
083         * Prints space characters to a given stream until its {@linkplain LogStream#position()
084         * position} is equal to this column's position.
085         *
086         * @param out the print stream
087         */
088        public void advance(LogStream out) {
089            out.fillTo(position + out.indentationLevel(), ' ');
090        }
091    }
092
093    private final LogStream out;
094
095    public InstructionPrinter(LogStream out) {
096        this.out = out;
097    }
098
099    public LogStream out() {
100        return out;
101    }
102
103    /**
104     * Prints a header for the tabulated data printed by {@link #printInstructionListing(ValueNode)}
105     * .
106     */
107    public void printInstructionListingHeader() {
108        BCI.printLabel(out);
109        USE.printLabel(out);
110        VALUE.printLabel(out);
111        INSTRUCTION.printLabel(out);
112        END.printLabel(out);
113        out.println();
114    }
115
116    /**
117     * Prints an instruction listing on one line. The instruction listing is composed of the columns
118     * specified by {@link InstructionLineColumn}.
119     *
120     * @param instruction the instruction to print
121     */
122    public void printInstructionListing(ValueNode instruction) {
123        int indentation = out.indentationLevel();
124        out.fillTo(BCI.position + indentation, ' ').print(0).fillTo(USE.position + indentation, ' ').print("0").fillTo(VALUE.position + indentation, ' ').print(ValueNodeUtil.valueString(instruction)).fillTo(
125                        INSTRUCTION.position + indentation, ' ');
126        printInstruction(instruction);
127        if (instruction instanceof StateSplit) {
128            out.print("  [state: " + ((StateSplit) instruction).stateAfter() + "]");
129        }
130        out.println();
131    }
132
133    public void printInstruction(ValueNode node) {
134        out.print(node.toString());
135    }
136}