package org.graalvm.compiler.printer;
import java.io.Closeable;
import java.io.IOException;
import java.lang.reflect.Array;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import org.graalvm.compiler.api.replacements.SnippetReflectionProvider;
import org.graalvm.compiler.debug.DebugContext;
import org.graalvm.compiler.debug.DebugContext.Scope;
import org.graalvm.compiler.graph.Graph;
import org.graalvm.compiler.nodes.ConstantNode;
import org.graalvm.compiler.nodes.StructuredGraph;
import org.graalvm.compiler.nodes.util.JavaConstantFormatter;
import org.graalvm.compiler.phases.schedule.SchedulePhase;
import org.graalvm.compiler.serviceprovider.JDK9Method;
import jdk.vm.ci.meta.JavaConstant;
import jdk.vm.ci.meta.JavaKind;
import jdk.vm.ci.meta.JavaType;
import jdk.vm.ci.meta.MetaUtil;
import jdk.vm.ci.meta.ResolvedJavaMethod;
import jdk.vm.ci.runtime.JVMCI;
import jdk.vm.ci.services.Services;
interface GraphPrinter extends Closeable, JavaConstantFormatter {
void beginGroup(DebugContext debug, String name, String shortName, ResolvedJavaMethod method, int bci, Map<Object, Object> properties) throws IOException;
void print(DebugContext debug, Graph graph, Map<Object, Object> properties, int id, String format, Object... args) throws IOException;
SnippetReflectionProvider getSnippetReflectionProvider();
void endGroup() throws IOException;
@Override
void close();
String JVMCI_RUNTIME_PACKAGE = JVMCI.class.getPackage().getName();
Object JVMCI_MODULE = JDK9Method.JAVA_SPECIFICATION_VERSION < 9 ? null : JDK9Method.getModule.invoke(Services.class);
List<Class<?>> TRUSTED_CLASSES = Arrays.asList(
String.class,
Class.class,
Boolean.class,
Byte.class,
Character.class,
Short.class,
Integer.class,
Float.class,
Long.class,
Double.class);
int MAX_CONSTANT_TO_STRING_LENGTH = 50;
static boolean isToStringTrusted(Class<?> c) {
if (TRUSTED_CLASSES.contains(c)) {
return true;
}
if (JDK9Method.JAVA_SPECIFICATION_VERSION < 9) {
if (c.getClassLoader() == Services.class.getClassLoader()) {
return true;
}
} else {
Object module = JDK9Method.getModule.invoke(c);
if (JVMCI_MODULE == module || (Boolean) JDK9Method.isOpenTo.invoke(JVMCI_MODULE, JVMCI_RUNTIME_PACKAGE, module)) {
return true;
}
}
if (c.getClassLoader() == GraphPrinter.class.getClassLoader()) {
return true;
}
return false;
}
@Override
default String format(JavaConstant constant) {
SnippetReflectionProvider snippetReflection = getSnippetReflectionProvider();
if (snippetReflection != null) {
if (constant.getJavaKind() == JavaKind.Object) {
Object obj = snippetReflection.asObject(Object.class, constant);
if (obj != null) {
return GraphPrinter.constantToString(obj);
}
}
}
return constant.toString();
}
default void updateStringPropertiesForConstant(Map<Object, Object> props, ConstantNode cn) {
if (cn.isJavaConstant() && cn.getStackKind().isObject()) {
String toString = format(cn.asJavaConstant());
String rawvalue = GraphPrinter.truncate(toString);
props.put("rawvalue", rawvalue);
if (!rawvalue.equals(toString)) {
props.put("toString", toString);
}
}
}
default Object[] simplifyClassArgs(Object... args) {
Object[] res = args;
for (int i = 0; i < args.length; i++) {
Object arg = args[i];
if (arg instanceof JavaType) {
if (args == res) {
res = new Object[args.length];
for (int a = 0; a < i; a++) {
res[a] = args[a];
}
}
res[i] = ((JavaType) arg).getUnqualifiedName();
} else {
res[i] = arg;
}
}
return res;
}
static String truncate(String s) {
if (s.length() > MAX_CONSTANT_TO_STRING_LENGTH) {
return s.substring(0, MAX_CONSTANT_TO_STRING_LENGTH - 3) + "...";
}
return s;
}
static String constantToString(Object value) {
Class<?> c = value.getClass();
String suffix = "";
if (c.isArray()) {
return constantArrayToString(value);
} else if (value instanceof Enum) {
return ((Enum<?>) value).name();
} else if (isToStringTrusted(c)) {
try {
return value.toString();
} catch (Throwable t) {
suffix = "[toString error: " + t.getClass().getName() + "]";
if (isToStringTrusted(t.getClass())) {
try {
suffix = "[toString error: " + t + "]";
} catch (Throwable t2) {
}
}
}
}
return MetaUtil.getSimpleName(c, true) + "@" + Integer.toHexString(System.identityHashCode(value)) + suffix;
}
static String constantArrayToString(Object array) {
Class<?> componentType = array.getClass().getComponentType();
assert componentType != null;
int arrayLength = Array.getLength(array);
StringBuilder buf = new StringBuilder(MetaUtil.getSimpleName(componentType, true)).append('[').append(arrayLength).append("]{");
int length = arrayLength;
boolean primitive = componentType.isPrimitive();
for (int i = 0; i < length; i++) {
if (primitive) {
buf.append(Array.get(array, i));
} else {
Object o = ((Object[]) array)[i];
buf.append(o == null ? "null" : constantToString(o));
}
if (i != length - 1) {
buf.append(", ");
}
}
return buf.append('}').toString();
}
@SuppressWarnings("try")
static StructuredGraph.ScheduleResult getScheduleOrNull(Graph graph) {
if (graph instanceof StructuredGraph) {
StructuredGraph sgraph = (StructuredGraph) graph;
StructuredGraph.ScheduleResult scheduleResult = sgraph.getLastSchedule();
if (scheduleResult == null) {
DebugContext debug = graph.getDebug();
try (Scope scope = debug.disable()) {
SchedulePhase schedule = new SchedulePhase(graph.getOptions());
schedule.apply(sgraph);
scheduleResult = sgraph.getLastSchedule();
} catch (Throwable t) {
}
}
return scheduleResult;
}
return null;
}
}