< prev index next >

src/jdk.internal.vm.compiler/share/classes/org.graalvm.compiler.printer/src/org/graalvm/compiler/printer/GraphPrinter.java

Print this page




  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 package org.graalvm.compiler.printer;
  24 
  25 import java.io.Closeable;
  26 import java.io.IOException;
  27 import java.lang.reflect.Array;
  28 import java.util.Arrays;
  29 import java.util.List;
  30 import java.util.Map;
  31 
  32 import org.graalvm.compiler.api.replacements.SnippetReflectionProvider;
  33 import org.graalvm.compiler.debug.DebugContext;
  34 import org.graalvm.compiler.debug.DebugContext.Scope;
  35 import org.graalvm.compiler.graph.Graph;
  36 import org.graalvm.compiler.nodes.ConstantNode;
  37 import org.graalvm.compiler.nodes.StructuredGraph;

  38 import org.graalvm.compiler.phases.schedule.SchedulePhase;
  39 import org.graalvm.compiler.serviceprovider.JDK9Method;
  40 
  41 import jdk.vm.ci.meta.JavaConstant;
  42 import jdk.vm.ci.meta.JavaKind;
  43 import jdk.vm.ci.meta.JavaType;
  44 import jdk.vm.ci.meta.MetaUtil;
  45 import jdk.vm.ci.meta.ResolvedJavaMethod;
  46 import jdk.vm.ci.runtime.JVMCI;
  47 import jdk.vm.ci.services.Services;
  48 
  49 interface GraphPrinter extends Closeable {
  50 
  51     /**
  52      * Starts a new group of graphs with the given name, short name and method byte code index (BCI)
  53      * as properties.
  54      */
  55     void beginGroup(DebugContext debug, String name, String shortName, ResolvedJavaMethod method, int bci, Map<Object, Object> properties) throws IOException;
  56 
  57     /**
  58      * Prints an entire {@link Graph} with the specified title, optionally using short names for
  59      * nodes.
  60      */
  61     void print(DebugContext debug, Graph graph, Map<Object, Object> properties, int id, String format, Object... args) throws IOException;
  62 
  63     SnippetReflectionProvider getSnippetReflectionProvider();
  64 
  65     /**
  66      * Ends the current group.
  67      */
  68     void endGroup() throws IOException;
  69 


  99     /**
 100      * Determines if invoking {@link Object#toString()} on an instance of {@code c} will only run
 101      * trusted code.
 102      */
 103     static boolean isToStringTrusted(Class<?> c) {
 104         if (TRUSTED_CLASSES.contains(c)) {
 105             return true;
 106         }
 107         if (JDK9Method.JAVA_SPECIFICATION_VERSION < 9) {
 108             if (c.getClassLoader() == Services.class.getClassLoader()) {
 109                 // Loaded by the JVMCI class loader
 110                 return true;
 111             }
 112         } else {
 113             Object module = JDK9Method.getModule.invoke(c);
 114             if (JVMCI_MODULE == module || (Boolean) JDK9Method.isOpenTo.invoke(JVMCI_MODULE, JVMCI_RUNTIME_PACKAGE, module)) {
 115                 // Can access non-statically-exported package in JVMCI
 116                 return true;
 117             }
 118         }



 119         return false;
 120     }
 121 
 122     /**
 123      * Sets or updates the {@code "rawvalue"} and {@code "toString"} properties in {@code props} for
 124      * {@code cn} if it's a boxed Object value and {@code snippetReflection} can access the raw
 125      * value.
 126      */
 127     default void updateStringPropertiesForConstant(Map<Object, Object> props, ConstantNode cn) {

 128         SnippetReflectionProvider snippetReflection = getSnippetReflectionProvider();
 129         if (snippetReflection != null && cn.getValue() instanceof JavaConstant) {
 130             JavaConstant constant = (JavaConstant) cn.getValue();
 131             if (constant.getJavaKind() == JavaKind.Object) {
 132                 Object obj = snippetReflection.asObject(Object.class, constant);
 133                 if (obj != null) {
 134                     String toString = GraphPrinter.constantToString(obj);














 135                     String rawvalue = GraphPrinter.truncate(toString);
 136                     // Overwrite the value inserted by
 137                     // ConstantNode.getDebugProperties()
 138                     props.put("rawvalue", rawvalue);
 139                     if (!rawvalue.equals(toString)) {
 140                         props.put("toString", toString);
 141                     }
 142                 }
 143             }
 144         }
 145     }
 146 
 147     /**
 148      * Replaces all {@link JavaType} elements in {@code args} with the result of
 149      * {@link JavaType#getUnqualifiedName()}.
 150      *
 151      * @return a copy of {@code args} with the above mentioned substitutions or {@code args} if no
 152      *         substitutions were performed
 153      */
 154     default Object[] simplifyClassArgs(Object... args) {
 155         Object[] res = args;
 156         for (int i = 0; i < args.length; i++) {
 157             Object arg = args[i];
 158             if (arg instanceof JavaType) {
 159                 if (args == res) {
 160                     res = new Object[args.length];
 161                     for (int a = 0; a < i; a++) {
 162                         res[a] = args[a];




  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 package org.graalvm.compiler.printer;
  24 
  25 import java.io.Closeable;
  26 import java.io.IOException;
  27 import java.lang.reflect.Array;
  28 import java.util.Arrays;
  29 import java.util.List;
  30 import java.util.Map;
  31 
  32 import org.graalvm.compiler.api.replacements.SnippetReflectionProvider;
  33 import org.graalvm.compiler.debug.DebugContext;
  34 import org.graalvm.compiler.debug.DebugContext.Scope;
  35 import org.graalvm.compiler.graph.Graph;
  36 import org.graalvm.compiler.nodes.ConstantNode;
  37 import org.graalvm.compiler.nodes.StructuredGraph;
  38 import org.graalvm.compiler.nodes.util.JavaConstantFormatter;
  39 import org.graalvm.compiler.phases.schedule.SchedulePhase;
  40 import org.graalvm.compiler.serviceprovider.JDK9Method;
  41 
  42 import jdk.vm.ci.meta.JavaConstant;
  43 import jdk.vm.ci.meta.JavaKind;
  44 import jdk.vm.ci.meta.JavaType;
  45 import jdk.vm.ci.meta.MetaUtil;
  46 import jdk.vm.ci.meta.ResolvedJavaMethod;
  47 import jdk.vm.ci.runtime.JVMCI;
  48 import jdk.vm.ci.services.Services;
  49 
  50 interface GraphPrinter extends Closeable, JavaConstantFormatter {
  51 
  52     /**
  53      * Starts a new group of graphs with the given name, short name and method byte code index (BCI)
  54      * as properties.
  55      */
  56     void beginGroup(DebugContext debug, String name, String shortName, ResolvedJavaMethod method, int bci, Map<Object, Object> properties) throws IOException;
  57 
  58     /**
  59      * Prints an entire {@link Graph} with the specified title, optionally using short names for
  60      * nodes.
  61      */
  62     void print(DebugContext debug, Graph graph, Map<Object, Object> properties, int id, String format, Object... args) throws IOException;
  63 
  64     SnippetReflectionProvider getSnippetReflectionProvider();
  65 
  66     /**
  67      * Ends the current group.
  68      */
  69     void endGroup() throws IOException;
  70 


 100     /**
 101      * Determines if invoking {@link Object#toString()} on an instance of {@code c} will only run
 102      * trusted code.
 103      */
 104     static boolean isToStringTrusted(Class<?> c) {
 105         if (TRUSTED_CLASSES.contains(c)) {
 106             return true;
 107         }
 108         if (JDK9Method.JAVA_SPECIFICATION_VERSION < 9) {
 109             if (c.getClassLoader() == Services.class.getClassLoader()) {
 110                 // Loaded by the JVMCI class loader
 111                 return true;
 112             }
 113         } else {
 114             Object module = JDK9Method.getModule.invoke(c);
 115             if (JVMCI_MODULE == module || (Boolean) JDK9Method.isOpenTo.invoke(JVMCI_MODULE, JVMCI_RUNTIME_PACKAGE, module)) {
 116                 // Can access non-statically-exported package in JVMCI
 117                 return true;
 118             }
 119         }
 120         if (c.getClassLoader() == GraphPrinter.class.getClassLoader()) {
 121             return true;
 122         }
 123         return false;
 124     }
 125 
 126     /**
 127      * Use the real {@link Object#toString()} method for {@link JavaConstant JavaConstants} that are
 128      * wrapping trusted types, other just return the results of {@link JavaConstant#toString()}.

 129      */
 130     @Override
 131     default String format(JavaConstant constant) {
 132         SnippetReflectionProvider snippetReflection = getSnippetReflectionProvider();
 133         if (snippetReflection != null) {

 134             if (constant.getJavaKind() == JavaKind.Object) {
 135                 Object obj = snippetReflection.asObject(Object.class, constant);
 136                 if (obj != null) {
 137                     return GraphPrinter.constantToString(obj);
 138                 }
 139             }
 140         }
 141         return constant.toString();
 142     }
 143 
 144     /**
 145      * Sets or updates the {@code "rawvalue"} and {@code "toString"} properties in {@code props} for
 146      * {@code cn} if it's a boxed Object value and {@code snippetReflection} can access the raw
 147      * value.
 148      */
 149     default void updateStringPropertiesForConstant(Map<Object, Object> props, ConstantNode cn) {
 150         if (cn.isJavaConstant() && cn.getStackKind().isObject()) {
 151             String toString = format(cn.asJavaConstant());
 152             String rawvalue = GraphPrinter.truncate(toString);
 153             // Overwrite the value inserted by
 154             // ConstantNode.getDebugProperties()
 155             props.put("rawvalue", rawvalue);
 156             if (!rawvalue.equals(toString)) {
 157                 props.put("toString", toString);


 158             }
 159         }
 160     }
 161 
 162     /**
 163      * Replaces all {@link JavaType} elements in {@code args} with the result of
 164      * {@link JavaType#getUnqualifiedName()}.
 165      *
 166      * @return a copy of {@code args} with the above mentioned substitutions or {@code args} if no
 167      *         substitutions were performed
 168      */
 169     default Object[] simplifyClassArgs(Object... args) {
 170         Object[] res = args;
 171         for (int i = 0; i < args.length; i++) {
 172             Object arg = args[i];
 173             if (arg instanceof JavaType) {
 174                 if (args == res) {
 175                     res = new Object[args.length];
 176                     for (int a = 0; a < i; a++) {
 177                         res[a] = args[a];


< prev index next >