001/*
002 * Copyright (c) 2012, 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.replacements;
024
025import java.io.*;
026
027import jdk.internal.jvmci.meta.*;
028
029import com.oracle.graal.graph.Node.ConstantNodeParameter;
030import com.oracle.graal.graph.Node.NodeIntrinsic;
031import com.oracle.graal.nodes.extended.*;
032import com.oracle.graal.compiler.common.spi.*;
033
034//JaCoCo Exclude
035
036/**
037 * Provides {@link PrintStream}-like logging facility.
038 */
039public final class Log {
040
041    public static final ForeignCallDescriptor LOG_PRIMITIVE = new ForeignCallDescriptor("logPrimitive", void.class, int.class, long.class, boolean.class);
042    public static final ForeignCallDescriptor LOG_OBJECT = new ForeignCallDescriptor("logObject", void.class, Object.class, int.class);
043    public static final ForeignCallDescriptor LOG_PRINTF = new ForeignCallDescriptor("logPrintf", void.class, Object.class, long.class, long.class, long.class);
044
045    // Note: Must be kept in sync with constants in jvmciRuntime.hpp
046    private static final int LOG_OBJECT_NEWLINE = 0x01;
047    private static final int LOG_OBJECT_STRING = 0x02;
048    private static final int LOG_OBJECT_ADDRESS = 0x04;
049
050    @NodeIntrinsic(ForeignCallNode.class)
051    private static native void log(@ConstantNodeParameter ForeignCallDescriptor logObject, Object object, int flags);
052
053    @NodeIntrinsic(ForeignCallNode.class)
054    private static native void log(@ConstantNodeParameter ForeignCallDescriptor logPrimitive, int typeChar, long value, boolean newline);
055
056    @NodeIntrinsic(ForeignCallNode.class)
057    private static native void printf(@ConstantNodeParameter ForeignCallDescriptor logPrintf, String format, long v1, long v2, long v3);
058
059    public static void print(boolean value) {
060        log(LOG_PRIMITIVE, Kind.Boolean.getTypeChar(), value ? 1L : 0L, false);
061    }
062
063    public static void print(byte value) {
064        log(LOG_PRIMITIVE, Kind.Byte.getTypeChar(), value, false);
065    }
066
067    public static void print(char value) {
068        log(LOG_PRIMITIVE, Kind.Char.getTypeChar(), value, false);
069    }
070
071    public static void print(short value) {
072        log(LOG_PRIMITIVE, Kind.Short.getTypeChar(), value, false);
073    }
074
075    public static void print(int value) {
076        log(LOG_PRIMITIVE, Kind.Int.getTypeChar(), value, false);
077    }
078
079    public static void print(long value) {
080        log(LOG_PRIMITIVE, Kind.Long.getTypeChar(), value, false);
081    }
082
083    /**
084     * Prints a formatted string to the log stream.
085     *
086     * @param format a C style printf format value that can contain at most one conversion specifier
087     *            (i.e., a sequence of characters starting with '%').
088     * @param value the value associated with the conversion specifier
089     */
090    public static void printf(String format, long value) {
091        printf(LOG_PRINTF, format, value, 0L, 0L);
092    }
093
094    public static void printf(String format, long v1, long v2) {
095        printf(LOG_PRINTF, format, v1, v2, 0L);
096    }
097
098    public static void printf(String format, long v1, long v2, long v3) {
099        printf(LOG_PRINTF, format, v1, v2, v3);
100    }
101
102    public static void print(float value) {
103        if (Float.isNaN(value)) {
104            print("NaN");
105        } else if (value == Float.POSITIVE_INFINITY) {
106            print("Infinity");
107        } else if (value == Float.NEGATIVE_INFINITY) {
108            print("-Infinity");
109        } else {
110            log(LOG_PRIMITIVE, Kind.Float.getTypeChar(), Float.floatToRawIntBits(value), false);
111        }
112    }
113
114    public static void print(double value) {
115        if (Double.isNaN(value)) {
116            print("NaN");
117        } else if (value == Double.POSITIVE_INFINITY) {
118            print("Infinity");
119        } else if (value == Double.NEGATIVE_INFINITY) {
120            print("-Infinity");
121        } else {
122            log(LOG_PRIMITIVE, Kind.Double.getTypeChar(), Double.doubleToRawLongBits(value), false);
123        }
124    }
125
126    public static void print(String value) {
127        log(LOG_OBJECT, value, LOG_OBJECT_STRING);
128    }
129
130    public static void printAddress(Object o) {
131        log(LOG_OBJECT, o, LOG_OBJECT_ADDRESS);
132    }
133
134    public static void printObject(Object o) {
135        log(LOG_OBJECT, o, 0);
136    }
137
138    public static void println(boolean value) {
139        log(LOG_PRIMITIVE, Kind.Boolean.getTypeChar(), value ? 1L : 0L, true);
140    }
141
142    public static void println(byte value) {
143        log(LOG_PRIMITIVE, Kind.Byte.getTypeChar(), value, true);
144    }
145
146    public static void println(char value) {
147        log(LOG_PRIMITIVE, Kind.Char.getTypeChar(), value, true);
148    }
149
150    public static void println(short value) {
151        log(LOG_PRIMITIVE, Kind.Short.getTypeChar(), value, true);
152    }
153
154    public static void println(int value) {
155        log(LOG_PRIMITIVE, Kind.Int.getTypeChar(), value, true);
156    }
157
158    public static void println(long value) {
159        log(LOG_PRIMITIVE, Kind.Long.getTypeChar(), value, true);
160    }
161
162    public static void println(float value) {
163        if (Float.isNaN(value)) {
164            println("NaN");
165        } else if (value == Float.POSITIVE_INFINITY) {
166            println("Infinity");
167        } else if (value == Float.NEGATIVE_INFINITY) {
168            println("-Infinity");
169        } else {
170            log(LOG_PRIMITIVE, Kind.Float.getTypeChar(), Float.floatToRawIntBits(value), true);
171        }
172    }
173
174    public static void println(double value) {
175        if (Double.isNaN(value)) {
176            println("NaN");
177        } else if (value == Double.POSITIVE_INFINITY) {
178            println("Infinity");
179        } else if (value == Double.NEGATIVE_INFINITY) {
180            println("-Infinity");
181        } else {
182            log(LOG_PRIMITIVE, Kind.Double.getTypeChar(), Double.doubleToRawLongBits(value), true);
183        }
184    }
185
186    public static void println(String value) {
187        log(LOG_OBJECT, value, LOG_OBJECT_NEWLINE | LOG_OBJECT_STRING);
188    }
189
190    public static void printlnAddress(Object o) {
191        log(LOG_OBJECT, o, LOG_OBJECT_NEWLINE | LOG_OBJECT_ADDRESS);
192    }
193
194    public static void printlnObject(Object o) {
195        log(LOG_OBJECT, o, LOG_OBJECT_NEWLINE);
196    }
197
198    public static void println() {
199        println("");
200    }
201}