001/*
002 * Copyright (c) 2009, 2013, 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.compiler.common.spi;
024
025import java.util.*;
026
027/**
028 * The name and signature of a foreign call. A foreign call differs from a normal compiled Java call
029 * in at least one of these aspects:
030 * <ul>
031 * <li>The call is to C/C++/assembler code.</li>
032 * <li>The call uses different conventions for passing parameters or returning values.</li>
033 * <li>The callee has different register saving semantics. For example, the callee may save all
034 * registers (apart from some specified temporaries) in which case the register allocator doesn't
035 * not need to spill all live registers around the call site.</li>
036 * <li>The call does not occur at an INVOKE* bytecode. Such a call could be transformed into a
037 * standard Java call if the foreign routine is a normal Java method and the runtime supports
038 * linking Java calls at arbitrary bytecodes.</li>
039 * </ul>
040 */
041public class ForeignCallDescriptor {
042
043    private final String name;
044    private final Class<?> resultType;
045    private final Class<?>[] argumentTypes;
046
047    public ForeignCallDescriptor(String name, Class<?> resultType, Class<?>... argumentTypes) {
048        this.name = name;
049        this.resultType = resultType;
050        this.argumentTypes = argumentTypes;
051    }
052
053    /**
054     * Gets the name of this foreign call.
055     */
056    public String getName() {
057        return name;
058    }
059
060    /**
061     * Gets the return type of this foreign call.
062     */
063    public Class<?> getResultType() {
064        return resultType;
065    }
066
067    /**
068     * Gets the argument types of this foreign call.
069     */
070    public Class<?>[] getArgumentTypes() {
071        return argumentTypes.clone();
072    }
073
074    @Override
075    public int hashCode() {
076        return name.hashCode();
077    }
078
079    @Override
080    public boolean equals(Object obj) {
081        if (obj instanceof ForeignCallDescriptor) {
082            ForeignCallDescriptor other = (ForeignCallDescriptor) obj;
083            return other.name.equals(name) && other.resultType.equals(resultType) && Arrays.equals(other.argumentTypes, argumentTypes);
084        }
085        return false;
086    }
087
088    @Override
089    public String toString() {
090        StringBuilder sb = new StringBuilder(name).append('(');
091        String sep = "";
092        for (Class<?> arg : argumentTypes) {
093            sb.append(sep).append(arg.getSimpleName());
094            sep = ",";
095        }
096        return sb.append(')').append(resultType.getSimpleName()).toString();
097    }
098}