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.nodes.extended;
024
025import jdk.internal.jvmci.common.*;
026import jdk.internal.jvmci.meta.*;
027import jdk.internal.jvmci.meta.Assumptions.*;
028
029import com.oracle.graal.compiler.common.type.*;
030import com.oracle.graal.graph.*;
031import com.oracle.graal.graph.spi.*;
032import com.oracle.graal.nodeinfo.*;
033import com.oracle.graal.nodes.*;
034import com.oracle.graal.nodes.spi.*;
035import com.oracle.graal.nodes.type.*;
036
037/**
038 * Loads a method from the virtual method table of a given hub.
039 */
040@NodeInfo
041public final class LoadMethodNode extends FixedWithNextNode implements Lowerable, Canonicalizable {
042
043    public static final NodeClass<LoadMethodNode> TYPE = NodeClass.create(LoadMethodNode.class);
044    @Input ValueNode hub;
045    protected final ResolvedJavaMethod method;
046    protected final ResolvedJavaType receiverType;
047
048    public ValueNode getHub() {
049        return hub;
050    }
051
052    public LoadMethodNode(@InjectedNodeParameter Stamp stamp, ResolvedJavaMethod method, ResolvedJavaType receiverType, ValueNode hub) {
053        super(TYPE, stamp);
054        this.receiverType = receiverType;
055        this.hub = hub;
056        this.method = method;
057        assert method.isConcrete() : "Cannot load abstract method from a hub";
058        assert method.hasReceiver() : "Cannot load a static method from a hub";
059        if (!method.isInVirtualMethodTable(receiverType)) {
060            throw new JVMCIError("%s does not have a vtable entry in type %s", method, receiverType);
061        }
062    }
063
064    @Override
065    public void lower(LoweringTool tool) {
066        tool.getLowerer().lower(this, tool);
067    }
068
069    @Override
070    public Node canonical(CanonicalizerTool tool) {
071        if (hub instanceof LoadHubNode) {
072            ValueNode object = ((LoadHubNode) hub).getValue();
073            ResolvedJavaType type = StampTool.typeOrNull(object);
074            if (StampTool.isExactType(object)) {
075                return resolveExactMethod(tool, type);
076            }
077            Assumptions assumptions = graph().getAssumptions();
078            if (type != null && assumptions != null) {
079                AssumptionResult<ResolvedJavaMethod> resolvedMethod = type.findUniqueConcreteMethod(method);
080                if (resolvedMethod != null && !type.isInterface() && method.getDeclaringClass().isAssignableFrom(type)) {
081                    assumptions.record(resolvedMethod);
082                    return ConstantNode.forConstant(stamp(), resolvedMethod.getResult().getEncoding(), tool.getMetaAccess());
083                }
084            }
085        }
086        if (hub.isConstant()) {
087            return resolveExactMethod(tool, tool.getConstantReflection().asJavaType(hub.asConstant()));
088        }
089
090        return this;
091    }
092
093    /**
094     * Find the method which would be loaded.
095     *
096     * @param tool
097     * @param type the exact type of object being loaded from
098     * @return the method which would be invoked for {@code type} or null if it doesn't implement
099     *         the method
100     */
101    private Node resolveExactMethod(CanonicalizerTool tool, ResolvedJavaType type) {
102        ResolvedJavaMethod newMethod = type.resolveConcreteMethod(method, type);
103        if (newMethod == null) {
104            /*
105             * This really represent a misuse of LoadMethod since we're loading from a class which
106             * isn't known to implement the original method but for now at least fold it away.
107             */
108            return ConstantNode.forConstant(stamp(), JavaConstant.NULL_POINTER, null);
109        } else {
110            return ConstantNode.forConstant(stamp(), newMethod.getEncoding(), tool.getMetaAccess());
111        }
112    }
113
114    public ResolvedJavaMethod getMethod() {
115        return method;
116    }
117
118    public ResolvedJavaType getReceiverType() {
119        return receiverType;
120    }
121}