001/*
002 * Copyright (c) 2011, 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.nodes;
024
025import java.util.*;
026
027import jdk.internal.jvmci.meta.*;
028
029import com.oracle.graal.compiler.common.spi.*;
030import com.oracle.graal.compiler.common.type.*;
031import com.oracle.graal.graph.*;
032import com.oracle.graal.hotspot.replacements.*;
033import com.oracle.graal.nodeinfo.*;
034import com.oracle.graal.nodes.*;
035import com.oracle.graal.nodes.memory.*;
036import com.oracle.graal.nodes.spi.*;
037
038/**
039 * Node for a {@linkplain ForeignCallDescriptor foreign} call from within a stub.
040 */
041@NodeInfo(nameTemplate = "StubForeignCall#{p#descriptor/s}", allowedUsageTypes = {InputType.Memory})
042public final class StubForeignCallNode extends FixedWithNextNode implements LIRLowerable, MemoryCheckpoint.Multi {
043
044    public static final NodeClass<StubForeignCallNode> TYPE = NodeClass.create(StubForeignCallNode.class);
045    @Input NodeInputList<ValueNode> arguments;
046    protected final ForeignCallsProvider foreignCalls;
047
048    protected final ForeignCallDescriptor descriptor;
049
050    public StubForeignCallNode(@InjectedNodeParameter ForeignCallsProvider foreignCalls, ForeignCallDescriptor descriptor, ValueNode... arguments) {
051        super(TYPE, StampFactory.forKind(Kind.fromJavaClass(descriptor.getResultType())));
052        this.arguments = new NodeInputList<>(this, arguments);
053        this.descriptor = descriptor;
054        this.foreignCalls = foreignCalls;
055    }
056
057    public ForeignCallDescriptor getDescriptor() {
058        return descriptor;
059    }
060
061    @Override
062    public LocationIdentity[] getLocationIdentities() {
063        LocationIdentity[] killedLocations = foreignCalls.getKilledLocations(descriptor);
064        killedLocations = Arrays.copyOf(killedLocations, killedLocations.length + 1);
065        killedLocations[killedLocations.length - 1] = HotSpotReplacementsUtil.PENDING_EXCEPTION_LOCATION;
066        return killedLocations;
067    }
068
069    protected Value[] operands(NodeLIRBuilderTool gen) {
070        Value[] operands = new Value[arguments.size()];
071        for (int i = 0; i < operands.length; i++) {
072            operands[i] = gen.operand(arguments.get(i));
073        }
074        return operands;
075    }
076
077    @Override
078    public void generate(NodeLIRBuilderTool gen) {
079        assert graph().start() instanceof StubStartNode;
080        ForeignCallLinkage linkage = foreignCalls.lookupForeignCall(descriptor);
081        Value[] operands = operands(gen);
082        Value result = gen.getLIRGeneratorTool().emitForeignCall(linkage, null, operands);
083        if (result != null) {
084            gen.setResult(this, result);
085        }
086    }
087
088    @Override
089    public String toString(Verbosity verbosity) {
090        if (verbosity == Verbosity.Name) {
091            return super.toString(verbosity) + "#" + descriptor;
092        }
093        return super.toString(verbosity);
094    }
095}