1 /* 2 * Copyright (c) 2013, 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.hotspot.replacements; 24 25 import static org.graalvm.compiler.nodeinfo.NodeCycles.CYCLES_UNKNOWN; 26 import static org.graalvm.compiler.nodeinfo.NodeSize.SIZE_20; 27 28 import org.graalvm.compiler.core.common.type.StampPair; 29 import org.graalvm.compiler.debug.GraalError; 30 import org.graalvm.compiler.graph.Node; 31 import org.graalvm.compiler.graph.NodeClass; 32 import org.graalvm.compiler.graph.spi.Canonicalizable; 33 import org.graalvm.compiler.graph.spi.CanonicalizerTool; 34 import org.graalvm.compiler.nodeinfo.NodeInfo; 35 import org.graalvm.compiler.nodes.CallTargetNode.InvokeKind; 36 import org.graalvm.compiler.nodes.ConstantNode; 37 import org.graalvm.compiler.nodes.FrameState; 38 import org.graalvm.compiler.nodes.InvokeNode; 39 import org.graalvm.compiler.nodes.ValueNode; 40 import org.graalvm.compiler.nodes.spi.Lowerable; 41 import org.graalvm.compiler.nodes.spi.LoweringTool; 42 import org.graalvm.compiler.replacements.nodes.MacroStateSplitNode; 43 44 import jdk.vm.ci.hotspot.HotSpotResolvedJavaMethod; 45 import jdk.vm.ci.hotspot.HotSpotResolvedObjectType; 46 import jdk.vm.ci.meta.ConstantReflectionProvider; 47 import jdk.vm.ci.meta.MetaAccessProvider; 48 import jdk.vm.ci.meta.ResolvedJavaMethod; 49 50 @NodeInfo(cycles = CYCLES_UNKNOWN, cyclesRationale = "This node can be lowered to a call", size = SIZE_20) 51 public final class ReflectionGetCallerClassNode extends MacroStateSplitNode implements Canonicalizable, Lowerable { 52 53 public static final NodeClass<ReflectionGetCallerClassNode> TYPE = NodeClass.create(ReflectionGetCallerClassNode.class); 54 55 public ReflectionGetCallerClassNode(InvokeKind invokeKind, ResolvedJavaMethod targetMethod, int bci, StampPair returnStamp, ValueNode... arguments) { 56 super(TYPE, invokeKind, targetMethod, bci, returnStamp, arguments); 57 } 58 59 @Override 60 public Node canonical(CanonicalizerTool tool) { 61 ConstantNode callerClassNode = getCallerClassNode(tool.getMetaAccess(), tool.getConstantReflection()); 62 if (callerClassNode != null) { 63 return callerClassNode; 64 } 65 return this; 66 } 67 68 @Override 69 public void lower(LoweringTool tool) { 70 ConstantNode callerClassNode = getCallerClassNode(tool.getMetaAccess(), tool.getConstantReflection()); 71 72 if (callerClassNode != null) { 73 graph().replaceFixedWithFloating(this, graph().addOrUniqueWithInputs(callerClassNode)); 74 } else { 75 InvokeNode invoke = createInvoke(); 76 graph().replaceFixedWithFixed(this, invoke); 77 invoke.lower(tool); 78 } 79 } 80 81 /** 82 * If inlining is deep enough this method returns a {@link ConstantNode} of the caller class by 83 * walking the the stack. 84 * 85 * @param metaAccess 86 * @return ConstantNode of the caller class, or null 87 */ 88 private ConstantNode getCallerClassNode(MetaAccessProvider metaAccess, ConstantReflectionProvider constantReflection) { 89 // Walk back up the frame states to find the caller at the required depth. 90 FrameState state = stateAfter(); 91 92 // Cf. JVM_GetCallerClass 93 // NOTE: Start the loop at depth 1 because the current frame state does 94 // not include the Reflection.getCallerClass() frame. 95 for (int n = 1; state != null; state = state.outerFrameState(), n++) { 96 HotSpotResolvedJavaMethod method = (HotSpotResolvedJavaMethod) state.getMethod(); 97 switch (n) { 98 case 0: 99 throw GraalError.shouldNotReachHere("current frame state does not include the Reflection.getCallerClass frame"); 100 case 1: 101 // Frame 0 and 1 must be caller sensitive (see JVM_GetCallerClass). 102 if (!method.isCallerSensitive()) { 103 return null; // bail-out; let JVM_GetCallerClass do the work 104 } 105 break; 106 default: 107 if (!method.ignoredBySecurityStackWalk()) { 108 // We have reached the desired frame; return the holder class. 109 HotSpotResolvedObjectType callerClass = method.getDeclaringClass(); 110 return ConstantNode.forConstant(constantReflection.asJavaClass(callerClass), metaAccess); 111 } 112 break; 113 } 114 } 115 return null; // bail-out; let JVM_GetCallerClass do the work 116 } 117 118 }