1 /*
   2  * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  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.nodes.debug.instrumentation;
  24 
  25 import static org.graalvm.compiler.nodeinfo.NodeCycles.CYCLES_IGNORED;
  26 import static org.graalvm.compiler.nodeinfo.NodeSize.SIZE_IGNORED;
  27 
  28 import org.graalvm.compiler.core.common.LocationIdentity;
  29 import org.graalvm.compiler.core.common.type.Stamp;
  30 import org.graalvm.compiler.graph.NodeClass;
  31 import org.graalvm.compiler.nodeinfo.NodeInfo;
  32 import org.graalvm.compiler.nodes.FixedWithNextNode;
  33 import org.graalvm.compiler.nodes.NamedLocationIdentity;
  34 import org.graalvm.compiler.nodes.StructuredGraph;
  35 import org.graalvm.compiler.nodes.debug.StringToBytesNode;
  36 import org.graalvm.compiler.nodes.memory.MemoryCheckpoint;
  37 import org.graalvm.compiler.nodes.spi.Lowerable;
  38 import org.graalvm.compiler.nodes.spi.LoweringTool;
  39 
  40 import jdk.vm.ci.meta.JavaKind;
  41 import jdk.vm.ci.meta.ResolvedJavaMethod;
  42 
  43 /**
  44  * The {@code RootNameNode} represents the name of the compilation root.
  45  */
  46 @NodeInfo(cycles = CYCLES_IGNORED, size = SIZE_IGNORED)
  47 public final class RootNameNode extends FixedWithNextNode implements Lowerable, InstrumentationInliningCallback, MemoryCheckpoint.Single {
  48 
  49     public static final NodeClass<RootNameNode> TYPE = NodeClass.create(RootNameNode.class);
  50 
  51     public RootNameNode(Stamp stamp) {
  52         super(TYPE, stamp);
  53     }
  54 
  55     /**
  56      * resolve this node and replace with a {@link StringToBytesNode} that constructs the root
  57      * method name in the compiled code. To ensure the correct result, this method should be invoked
  58      * after inlining.
  59      */
  60     private void resolve(StructuredGraph graph) {
  61         ResolvedJavaMethod method = graph.method();
  62         String rootName = method == null ? "<unresolved method>" : (method.getDeclaringClass().toJavaName() + "." + method.getName() + method.getSignature().toMethodDescriptor());
  63         StringToBytesNode stringToByteNode = graph().add(new StringToBytesNode(rootName, stamp()));
  64         graph().replaceFixedWithFixed(this, stringToByteNode);
  65     }
  66 
  67     @Override
  68     public void lower(LoweringTool tool) {
  69         resolve(graph());
  70     }
  71 
  72     @Override
  73     public void preInlineInstrumentation(InstrumentationNode instrumentation) {
  74         // resolve to the method name of the original graph
  75         resolve(instrumentation.graph());
  76     }
  77 
  78     @Override
  79     public void postInlineInstrumentation(InstrumentationNode instrumentation) {
  80     }
  81 
  82     @Override
  83     public LocationIdentity getLocationIdentity() {
  84         return NamedLocationIdentity.getArrayLocation(JavaKind.Byte);
  85     }
  86 
  87 }