001/*
002 * Copyright (c) 2013, 2015, 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.hotspot.replacements;
024
025import jdk.internal.jvmci.common.*;
026import jdk.internal.jvmci.hotspot.*;
027import jdk.internal.jvmci.meta.*;
028
029import com.oracle.graal.graph.*;
030import com.oracle.graal.graph.spi.*;
031import com.oracle.graal.nodeinfo.*;
032import com.oracle.graal.nodes.CallTargetNode.InvokeKind;
033import com.oracle.graal.nodes.*;
034import com.oracle.graal.nodes.spi.*;
035import com.oracle.graal.replacements.nodes.*;
036
037@NodeInfo
038public final class ReflectionGetCallerClassNode extends MacroStateSplitNode implements Canonicalizable, Lowerable {
039
040    public static final NodeClass<ReflectionGetCallerClassNode> TYPE = NodeClass.create(ReflectionGetCallerClassNode.class);
041
042    public ReflectionGetCallerClassNode(InvokeKind invokeKind, ResolvedJavaMethod targetMethod, int bci, JavaType returnType, ValueNode... arguments) {
043        super(TYPE, invokeKind, targetMethod, bci, returnType, arguments);
044    }
045
046    @Override
047    public Node canonical(CanonicalizerTool tool) {
048        ConstantNode callerClassNode = getCallerClassNode(tool.getMetaAccess());
049        if (callerClassNode != null) {
050            return callerClassNode;
051        }
052        return this;
053    }
054
055    @Override
056    public void lower(LoweringTool tool) {
057        ConstantNode callerClassNode = getCallerClassNode(tool.getMetaAccess());
058
059        if (callerClassNode != null) {
060            graph().replaceFixedWithFloating(this, graph().addOrUniqueWithInputs(callerClassNode));
061        } else {
062            InvokeNode invoke = createInvoke();
063            graph().replaceFixedWithFixed(this, invoke);
064            invoke.lower(tool);
065        }
066    }
067
068    /**
069     * If inlining is deep enough this method returns a {@link ConstantNode} of the caller class by
070     * walking the the stack.
071     *
072     * @param metaAccess
073     * @return ConstantNode of the caller class, or null
074     */
075    private ConstantNode getCallerClassNode(MetaAccessProvider metaAccess) {
076        // Walk back up the frame states to find the caller at the required depth.
077        FrameState state = stateAfter();
078
079        // Cf. JVM_GetCallerClass
080        // NOTE: Start the loop at depth 1 because the current frame state does
081        // not include the Reflection.getCallerClass() frame.
082        for (int n = 1; state != null; state = state.outerFrameState(), n++) {
083            HotSpotResolvedJavaMethod method = (HotSpotResolvedJavaMethod) state.method();
084            switch (n) {
085                case 0:
086                    throw JVMCIError.shouldNotReachHere("current frame state does not include the Reflection.getCallerClass frame");
087                case 1:
088                    // Frame 0 and 1 must be caller sensitive (see JVM_GetCallerClass).
089                    if (!method.isCallerSensitive()) {
090                        return null;  // bail-out; let JVM_GetCallerClass do the work
091                    }
092                    break;
093                default:
094                    if (!method.ignoredBySecurityStackWalk()) {
095                        // We have reached the desired frame; return the holder class.
096                        HotSpotResolvedObjectType callerClass = method.getDeclaringClass();
097                        return ConstantNode.forConstant(callerClass.getJavaClass(), metaAccess);
098                    }
099                    break;
100            }
101        }
102        return null;  // bail-out; let JVM_GetCallerClass do the work
103    }
104
105}