001/*
002 * Copyright (c) 2014, 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.truffle.hotspot.nfi;
024
025import java.util.*;
026
027import jdk.internal.jvmci.code.*;
028import jdk.internal.jvmci.common.*;
029import com.oracle.graal.debug.*;
030import com.oracle.graal.debug.Debug.*;
031import jdk.internal.jvmci.meta.*;
032
033import com.oracle.nfi.api.*;
034
035public class HotSpotNativeFunctionHandle implements NativeFunctionHandle {
036
037    private final InstalledCode code;
038    private final String name;
039    private final Class<?>[] argumentTypes;
040
041    public HotSpotNativeFunctionHandle(InstalledCode code, String name, Class<?>... argumentTypes) {
042        this.argumentTypes = argumentTypes;
043        this.name = name;
044        this.code = code;
045    }
046
047    private void traceCall(Object... args) {
048        try (Scope s = Debug.scope("GNFI")) {
049            if (Debug.isLogEnabled()) {
050                Debug.log("[GNFI] %s%s", name, Arrays.toString(args));
051            }
052        }
053    }
054
055    private void traceResult(Object result) {
056        try (Scope s = Debug.scope("GNFI")) {
057            if (Debug.isLogEnabled()) {
058                Debug.log("[GNFI] %s --> %s", name, result);
059            }
060        }
061    }
062
063    @Override
064    public Object call(Object... args) {
065        assert checkArgs(args);
066        try {
067            traceCall(args);
068            Object res = code.executeVarargs(args, null, null);
069            traceResult(res);
070            return res;
071        } catch (InvalidInstalledCodeException e) {
072            throw JVMCIError.shouldNotReachHere("Execution of GNFI Callstub failed: " + name);
073        }
074    }
075
076    private boolean checkArgs(Object... args) {
077        assert args.length == argumentTypes.length : this + " expected " + argumentTypes.length + " args, got " + args.length;
078        for (int i = 0; i < argumentTypes.length; i++) {
079            Object arg = args[i];
080            assert arg != null;
081            Class<?> expectedType = argumentTypes[i];
082            if (expectedType.isPrimitive()) {
083                Kind kind = Kind.fromJavaClass(expectedType);
084                expectedType = kind.toBoxedJavaClass();
085            }
086            assert expectedType == arg.getClass() : this + " expected arg " + i + " to be " + expectedType.getName() + ", not " + arg.getClass().getName();
087
088        }
089        return true;
090    }
091
092    @Override
093    public String toString() {
094        return name + Arrays.toString(argumentTypes);
095    }
096}