annotate graal/com.oracle.graal.truffle.hotspot/src/com/oracle/graal/truffle/hotspot/nfi/HotSpotNativeFunctionInterface.java @ 21554:b1530a6cce8c

renamed com.oracle.graal.[debug|options|hotspotvmconfig]* modules to com.oracle.jvmci.[debug|options|hotspotvmconfig]* modules (JBS:GRAAL-53)
author Doug Simon <doug.simon@oracle.com>
date Tue, 26 May 2015 23:21:15 +0200
parents 5324104ac4f3
children 48c1ebd24120
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
13837
ed3a1471e133 Add the Graal Native Function Interface
Matthias Grimmer <grimmer@ssw.jku.at>
parents:
diff changeset
1 /*
ed3a1471e133 Add the Graal Native Function Interface
Matthias Grimmer <grimmer@ssw.jku.at>
parents:
diff changeset
2 * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.
ed3a1471e133 Add the Graal Native Function Interface
Matthias Grimmer <grimmer@ssw.jku.at>
parents:
diff changeset
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
ed3a1471e133 Add the Graal Native Function Interface
Matthias Grimmer <grimmer@ssw.jku.at>
parents:
diff changeset
4 *
ed3a1471e133 Add the Graal Native Function Interface
Matthias Grimmer <grimmer@ssw.jku.at>
parents:
diff changeset
5 * This code is free software; you can redistribute it and/or modify it
ed3a1471e133 Add the Graal Native Function Interface
Matthias Grimmer <grimmer@ssw.jku.at>
parents:
diff changeset
6 * under the terms of the GNU General Public License version 2 only, as
ed3a1471e133 Add the Graal Native Function Interface
Matthias Grimmer <grimmer@ssw.jku.at>
parents:
diff changeset
7 * published by the Free Software Foundation.
ed3a1471e133 Add the Graal Native Function Interface
Matthias Grimmer <grimmer@ssw.jku.at>
parents:
diff changeset
8 *
ed3a1471e133 Add the Graal Native Function Interface
Matthias Grimmer <grimmer@ssw.jku.at>
parents:
diff changeset
9 * This code is distributed in the hope that it will be useful, but WITHOUT
ed3a1471e133 Add the Graal Native Function Interface
Matthias Grimmer <grimmer@ssw.jku.at>
parents:
diff changeset
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
ed3a1471e133 Add the Graal Native Function Interface
Matthias Grimmer <grimmer@ssw.jku.at>
parents:
diff changeset
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
ed3a1471e133 Add the Graal Native Function Interface
Matthias Grimmer <grimmer@ssw.jku.at>
parents:
diff changeset
12 * version 2 for more details (a copy is included in the LICENSE file that
ed3a1471e133 Add the Graal Native Function Interface
Matthias Grimmer <grimmer@ssw.jku.at>
parents:
diff changeset
13 * accompanied this code).
ed3a1471e133 Add the Graal Native Function Interface
Matthias Grimmer <grimmer@ssw.jku.at>
parents:
diff changeset
14 *
ed3a1471e133 Add the Graal Native Function Interface
Matthias Grimmer <grimmer@ssw.jku.at>
parents:
diff changeset
15 * You should have received a copy of the GNU General Public License version
ed3a1471e133 Add the Graal Native Function Interface
Matthias Grimmer <grimmer@ssw.jku.at>
parents:
diff changeset
16 * 2 along with this work; if not, write to the Free Software Foundation,
ed3a1471e133 Add the Graal Native Function Interface
Matthias Grimmer <grimmer@ssw.jku.at>
parents:
diff changeset
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
ed3a1471e133 Add the Graal Native Function Interface
Matthias Grimmer <grimmer@ssw.jku.at>
parents:
diff changeset
18 *
ed3a1471e133 Add the Graal Native Function Interface
Matthias Grimmer <grimmer@ssw.jku.at>
parents:
diff changeset
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
ed3a1471e133 Add the Graal Native Function Interface
Matthias Grimmer <grimmer@ssw.jku.at>
parents:
diff changeset
20 * or visit www.oracle.com if you need additional information or have any
ed3a1471e133 Add the Graal Native Function Interface
Matthias Grimmer <grimmer@ssw.jku.at>
parents:
diff changeset
21 * questions.
ed3a1471e133 Add the Graal Native Function Interface
Matthias Grimmer <grimmer@ssw.jku.at>
parents:
diff changeset
22 */
20839
b1700db197c7 Move com.oracle.nfi implementation to graal.truffle.hotspot since it implements an interface that does not live in the graal class-loader
Gilles Duboscq <gilles.m.duboscq@oracle.com>
parents: 20068
diff changeset
23 package com.oracle.graal.truffle.hotspot.nfi;
13837
ed3a1471e133 Add the Graal Native Function Interface
Matthias Grimmer <grimmer@ssw.jku.at>
parents:
diff changeset
24
13895
4731c1a0b1f3 consolidated GNFI code into graal.hotspot project and cleaned up the documentation and code
Doug Simon <doug.simon@oracle.com>
parents: 13872
diff changeset
25 import static com.oracle.graal.api.code.CodeUtil.*;
20839
b1700db197c7 Move com.oracle.nfi implementation to graal.truffle.hotspot since it implements an interface that does not live in the graal class-loader
Gilles Duboscq <gilles.m.duboscq@oracle.com>
parents: 20068
diff changeset
26 import static com.oracle.graal.truffle.hotspot.nfi.NativeCallStubGraphBuilder.*;
21541
5e868236654f moved UnsafeAccess to com.oracle.jvmci.common (JBS:GRAAL-53)
Doug Simon <doug.simon@oracle.com>
parents: 21526
diff changeset
27 import static com.oracle.jvmci.common.UnsafeAccess.*;
13837
ed3a1471e133 Add the Graal Native Function Interface
Matthias Grimmer <grimmer@ssw.jku.at>
parents:
diff changeset
28
ed3a1471e133 Add the Graal Native Function Interface
Matthias Grimmer <grimmer@ssw.jku.at>
parents:
diff changeset
29 import com.oracle.graal.api.code.*;
13905
3089e9a7cf44 fixed bug in passing primitive arrays through native function handles
Doug Simon <doug.simon@oracle.com>
parents: 13895
diff changeset
30 import com.oracle.graal.api.code.CallingConvention.Type;
13895
4731c1a0b1f3 consolidated GNFI code into graal.hotspot project and cleaned up the documentation and code
Doug Simon <doug.simon@oracle.com>
parents: 13872
diff changeset
31 import com.oracle.graal.api.meta.*;
4731c1a0b1f3 consolidated GNFI code into graal.hotspot project and cleaned up the documentation and code
Doug Simon <doug.simon@oracle.com>
parents: 13872
diff changeset
32 import com.oracle.graal.compiler.*;
13837
ed3a1471e133 Add the Graal Native Function Interface
Matthias Grimmer <grimmer@ssw.jku.at>
parents:
diff changeset
33 import com.oracle.graal.compiler.target.*;
ed3a1471e133 Add the Graal Native Function Interface
Matthias Grimmer <grimmer@ssw.jku.at>
parents:
diff changeset
34 import com.oracle.graal.hotspot.meta.*;
13895
4731c1a0b1f3 consolidated GNFI code into graal.hotspot project and cleaned up the documentation and code
Doug Simon <doug.simon@oracle.com>
parents: 13872
diff changeset
35 import com.oracle.graal.lir.asm.*;
19232
66c60942c06c GraalCompiler.emitLowLevel: use LowLevelSuites instead of LowLevelCompilerConfiguration.
Josef Eisl <josef.eisl@jku.at>
parents: 19047
diff changeset
36 import com.oracle.graal.lir.phases.*;
13895
4731c1a0b1f3 consolidated GNFI code into graal.hotspot project and cleaned up the documentation and code
Doug Simon <doug.simon@oracle.com>
parents: 13872
diff changeset
37 import com.oracle.graal.nodes.*;
4731c1a0b1f3 consolidated GNFI code into graal.hotspot project and cleaned up the documentation and code
Doug Simon <doug.simon@oracle.com>
parents: 13872
diff changeset
38 import com.oracle.graal.phases.*;
4731c1a0b1f3 consolidated GNFI code into graal.hotspot project and cleaned up the documentation and code
Doug Simon <doug.simon@oracle.com>
parents: 13872
diff changeset
39 import com.oracle.graal.phases.tiers.*;
21554
b1530a6cce8c renamed com.oracle.graal.[debug|options|hotspotvmconfig]* modules to com.oracle.jvmci.[debug|options|hotspotvmconfig]* modules (JBS:GRAAL-53)
Doug Simon <doug.simon@oracle.com>
parents: 21551
diff changeset
40 import com.oracle.jvmci.debug.*;
b1530a6cce8c renamed com.oracle.graal.[debug|options|hotspotvmconfig]* modules to com.oracle.jvmci.[debug|options|hotspotvmconfig]* modules (JBS:GRAAL-53)
Doug Simon <doug.simon@oracle.com>
parents: 21551
diff changeset
41 import com.oracle.jvmci.debug.Debug.Scope;
21551
5324104ac4f3 moved com.oracle.graal.hotspot.jvmci classes to com.oracle.jvmci.hotspot module (JBS:GRAAL-53)
Doug Simon <doug.simon@oracle.com>
parents: 21541
diff changeset
42 import com.oracle.jvmci.hotspot.*;
16691
dd8449afc086 GNFI: move GNFI interfaces to oracle.nfi - interface does not depend on graal
Matthias Grimmer <grimmer@ssw.jku.at>
parents: 15291
diff changeset
43 import com.oracle.nfi.api.*;
13837
ed3a1471e133 Add the Graal Native Function Interface
Matthias Grimmer <grimmer@ssw.jku.at>
parents:
diff changeset
44
13895
4731c1a0b1f3 consolidated GNFI code into graal.hotspot project and cleaned up the documentation and code
Doug Simon <doug.simon@oracle.com>
parents: 13872
diff changeset
45 public class HotSpotNativeFunctionInterface implements NativeFunctionInterface {
13837
ed3a1471e133 Add the Graal Native Function Interface
Matthias Grimmer <grimmer@ssw.jku.at>
parents:
diff changeset
46
13895
4731c1a0b1f3 consolidated GNFI code into graal.hotspot project and cleaned up the documentation and code
Doug Simon <doug.simon@oracle.com>
parents: 13872
diff changeset
47 private final HotSpotProviders providers;
4731c1a0b1f3 consolidated GNFI code into graal.hotspot project and cleaned up the documentation and code
Doug Simon <doug.simon@oracle.com>
parents: 13872
diff changeset
48 private final Backend backend;
4731c1a0b1f3 consolidated GNFI code into graal.hotspot project and cleaned up the documentation and code
Doug Simon <doug.simon@oracle.com>
parents: 13872
diff changeset
49 private final HotSpotNativeLibraryHandle rtldDefault;
4731c1a0b1f3 consolidated GNFI code into graal.hotspot project and cleaned up the documentation and code
Doug Simon <doug.simon@oracle.com>
parents: 13872
diff changeset
50 private final HotSpotNativeFunctionPointer libraryLoadFunctionPointer;
4731c1a0b1f3 consolidated GNFI code into graal.hotspot project and cleaned up the documentation and code
Doug Simon <doug.simon@oracle.com>
parents: 13872
diff changeset
51 private final HotSpotNativeFunctionPointer functionLookupFunctionPointer;
4731c1a0b1f3 consolidated GNFI code into graal.hotspot project and cleaned up the documentation and code
Doug Simon <doug.simon@oracle.com>
parents: 13872
diff changeset
52 private final RawNativeCallNodeFactory factory;
13837
ed3a1471e133 Add the Graal Native Function Interface
Matthias Grimmer <grimmer@ssw.jku.at>
parents:
diff changeset
53
13895
4731c1a0b1f3 consolidated GNFI code into graal.hotspot project and cleaned up the documentation and code
Doug Simon <doug.simon@oracle.com>
parents: 13872
diff changeset
54 private HotSpotNativeFunctionHandle libraryLookupFunctionHandle;
4731c1a0b1f3 consolidated GNFI code into graal.hotspot project and cleaned up the documentation and code
Doug Simon <doug.simon@oracle.com>
parents: 13872
diff changeset
55 private HotSpotNativeFunctionHandle dllLookupFunctionHandle;
13869
e2db5c351ef3 GNFI: cache lookup handles
Matthias Grimmer <grimmer@ssw.jku.at>
parents: 13837
diff changeset
56
13895
4731c1a0b1f3 consolidated GNFI code into graal.hotspot project and cleaned up the documentation and code
Doug Simon <doug.simon@oracle.com>
parents: 13872
diff changeset
57 public HotSpotNativeFunctionInterface(HotSpotProviders providers, RawNativeCallNodeFactory factory, Backend backend, long dlopen, long dlsym, long rtldDefault) {
4731c1a0b1f3 consolidated GNFI code into graal.hotspot project and cleaned up the documentation and code
Doug Simon <doug.simon@oracle.com>
parents: 13872
diff changeset
58 this.rtldDefault = rtldDefault == HotSpotVMConfig.INVALID_RTLD_DEFAULT_HANDLE ? null : new HotSpotNativeLibraryHandle("RTLD_DEFAULT", rtldDefault);
13837
ed3a1471e133 Add the Graal Native Function Interface
Matthias Grimmer <grimmer@ssw.jku.at>
parents:
diff changeset
59 this.providers = providers;
19823
97da0b90e0f3 Use host backend when generating a new instance for HotSpotNativeFunctionInterface.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents: 19717
diff changeset
60 assert backend != null;
13837
ed3a1471e133 Add the Graal Native Function Interface
Matthias Grimmer <grimmer@ssw.jku.at>
parents:
diff changeset
61 this.backend = backend;
13895
4731c1a0b1f3 consolidated GNFI code into graal.hotspot project and cleaned up the documentation and code
Doug Simon <doug.simon@oracle.com>
parents: 13872
diff changeset
62 this.factory = factory;
4731c1a0b1f3 consolidated GNFI code into graal.hotspot project and cleaned up the documentation and code
Doug Simon <doug.simon@oracle.com>
parents: 13872
diff changeset
63 this.libraryLoadFunctionPointer = new HotSpotNativeFunctionPointer(dlopen, "os::dll_load");
4731c1a0b1f3 consolidated GNFI code into graal.hotspot project and cleaned up the documentation and code
Doug Simon <doug.simon@oracle.com>
parents: 13872
diff changeset
64 this.functionLookupFunctionPointer = new HotSpotNativeFunctionPointer(dlsym, "os::dll_lookup");
13837
ed3a1471e133 Add the Graal Native Function Interface
Matthias Grimmer <grimmer@ssw.jku.at>
parents:
diff changeset
65 }
ed3a1471e133 Add the Graal Native Function Interface
Matthias Grimmer <grimmer@ssw.jku.at>
parents:
diff changeset
66
ed3a1471e133 Add the Graal Native Function Interface
Matthias Grimmer <grimmer@ssw.jku.at>
parents:
diff changeset
67 @Override
13895
4731c1a0b1f3 consolidated GNFI code into graal.hotspot project and cleaned up the documentation and code
Doug Simon <doug.simon@oracle.com>
parents: 13872
diff changeset
68 public HotSpotNativeLibraryHandle getLibraryHandle(String libPath) {
13869
e2db5c351ef3 GNFI: cache lookup handles
Matthias Grimmer <grimmer@ssw.jku.at>
parents: 13837
diff changeset
69 if (libraryLookupFunctionHandle == null) {
13895
4731c1a0b1f3 consolidated GNFI code into graal.hotspot project and cleaned up the documentation and code
Doug Simon <doug.simon@oracle.com>
parents: 13872
diff changeset
70 libraryLookupFunctionHandle = createHandle(libraryLoadFunctionPointer, long.class, long.class, long.class, int.class);
13869
e2db5c351ef3 GNFI: cache lookup handles
Matthias Grimmer <grimmer@ssw.jku.at>
parents: 13837
diff changeset
71 }
13837
ed3a1471e133 Add the Graal Native Function Interface
Matthias Grimmer <grimmer@ssw.jku.at>
parents:
diff changeset
72
13895
4731c1a0b1f3 consolidated GNFI code into graal.hotspot project and cleaned up the documentation and code
Doug Simon <doug.simon@oracle.com>
parents: 13872
diff changeset
73 int ebufLen = 1024;
4731c1a0b1f3 consolidated GNFI code into graal.hotspot project and cleaned up the documentation and code
Doug Simon <doug.simon@oracle.com>
parents: 13872
diff changeset
74 // Allocating a single chunk for both the error message buffer and the
4731c1a0b1f3 consolidated GNFI code into graal.hotspot project and cleaned up the documentation and code
Doug Simon <doug.simon@oracle.com>
parents: 13872
diff changeset
75 // file name simplifies deallocation below.
4731c1a0b1f3 consolidated GNFI code into graal.hotspot project and cleaned up the documentation and code
Doug Simon <doug.simon@oracle.com>
parents: 13872
diff changeset
76 long buffer = unsafe.allocateMemory(ebufLen + libPath.length() + 1);
4731c1a0b1f3 consolidated GNFI code into graal.hotspot project and cleaned up the documentation and code
Doug Simon <doug.simon@oracle.com>
parents: 13872
diff changeset
77 long ebuf = buffer;
4731c1a0b1f3 consolidated GNFI code into graal.hotspot project and cleaned up the documentation and code
Doug Simon <doug.simon@oracle.com>
parents: 13872
diff changeset
78 long libPathCString = writeCString(libPath, buffer + ebufLen);
13837
ed3a1471e133 Add the Graal Native Function Interface
Matthias Grimmer <grimmer@ssw.jku.at>
parents:
diff changeset
79 try {
13895
4731c1a0b1f3 consolidated GNFI code into graal.hotspot project and cleaned up the documentation and code
Doug Simon <doug.simon@oracle.com>
parents: 13872
diff changeset
80 long handle = (long) libraryLookupFunctionHandle.call(libPathCString, ebuf, ebufLen);
4731c1a0b1f3 consolidated GNFI code into graal.hotspot project and cleaned up the documentation and code
Doug Simon <doug.simon@oracle.com>
parents: 13872
diff changeset
81 if (handle == 0) {
4731c1a0b1f3 consolidated GNFI code into graal.hotspot project and cleaned up the documentation and code
Doug Simon <doug.simon@oracle.com>
parents: 13872
diff changeset
82 throw new UnsatisfiedLinkError(libPath);
4731c1a0b1f3 consolidated GNFI code into graal.hotspot project and cleaned up the documentation and code
Doug Simon <doug.simon@oracle.com>
parents: 13872
diff changeset
83 }
4731c1a0b1f3 consolidated GNFI code into graal.hotspot project and cleaned up the documentation and code
Doug Simon <doug.simon@oracle.com>
parents: 13872
diff changeset
84 return new HotSpotNativeLibraryHandle(libPath, handle);
4731c1a0b1f3 consolidated GNFI code into graal.hotspot project and cleaned up the documentation and code
Doug Simon <doug.simon@oracle.com>
parents: 13872
diff changeset
85 } finally {
4731c1a0b1f3 consolidated GNFI code into graal.hotspot project and cleaned up the documentation and code
Doug Simon <doug.simon@oracle.com>
parents: 13872
diff changeset
86 unsafe.freeMemory(buffer);
13837
ed3a1471e133 Add the Graal Native Function Interface
Matthias Grimmer <grimmer@ssw.jku.at>
parents:
diff changeset
87 }
ed3a1471e133 Add the Graal Native Function Interface
Matthias Grimmer <grimmer@ssw.jku.at>
parents:
diff changeset
88 }
ed3a1471e133 Add the Graal Native Function Interface
Matthias Grimmer <grimmer@ssw.jku.at>
parents:
diff changeset
89
ed3a1471e133 Add the Graal Native Function Interface
Matthias Grimmer <grimmer@ssw.jku.at>
parents:
diff changeset
90 @Override
15040
288c23143d47 Fix most raw type references.
Josef Eisl <josef.eisl@jku.at>
parents: 14143
diff changeset
91 public HotSpotNativeFunctionHandle getFunctionHandle(NativeLibraryHandle library, String name, Class<?> returnType, Class<?>... argumentTypes) {
16874
3044a847fe91 NFI: never throw an unsatisfiedLinkError - better return null
Matthias Grimmer <grimmer@ssw.jku.at>
parents: 16691
diff changeset
92 HotSpotNativeFunctionPointer functionPointer = lookupFunctionPointer(name, library, false);
13905
3089e9a7cf44 fixed bug in passing primitive arrays through native function handles
Doug Simon <doug.simon@oracle.com>
parents: 13895
diff changeset
93 return createHandle(functionPointer, returnType, argumentTypes);
13837
ed3a1471e133 Add the Graal Native Function Interface
Matthias Grimmer <grimmer@ssw.jku.at>
parents:
diff changeset
94 }
ed3a1471e133 Add the Graal Native Function Interface
Matthias Grimmer <grimmer@ssw.jku.at>
parents:
diff changeset
95
ed3a1471e133 Add the Graal Native Function Interface
Matthias Grimmer <grimmer@ssw.jku.at>
parents:
diff changeset
96 @Override
15040
288c23143d47 Fix most raw type references.
Josef Eisl <josef.eisl@jku.at>
parents: 14143
diff changeset
97 public HotSpotNativeFunctionHandle getFunctionHandle(NativeLibraryHandle[] libraries, String name, Class<?> returnType, Class<?>... argumentTypes) {
13895
4731c1a0b1f3 consolidated GNFI code into graal.hotspot project and cleaned up the documentation and code
Doug Simon <doug.simon@oracle.com>
parents: 13872
diff changeset
98 HotSpotNativeFunctionPointer functionPointer = null;
4731c1a0b1f3 consolidated GNFI code into graal.hotspot project and cleaned up the documentation and code
Doug Simon <doug.simon@oracle.com>
parents: 13872
diff changeset
99 for (NativeLibraryHandle libraryHandle : libraries) {
4731c1a0b1f3 consolidated GNFI code into graal.hotspot project and cleaned up the documentation and code
Doug Simon <doug.simon@oracle.com>
parents: 13872
diff changeset
100 functionPointer = lookupFunctionPointer(name, libraryHandle, false);
4731c1a0b1f3 consolidated GNFI code into graal.hotspot project and cleaned up the documentation and code
Doug Simon <doug.simon@oracle.com>
parents: 13872
diff changeset
101 if (functionPointer != null) {
4731c1a0b1f3 consolidated GNFI code into graal.hotspot project and cleaned up the documentation and code
Doug Simon <doug.simon@oracle.com>
parents: 13872
diff changeset
102 return createHandle(functionPointer, returnType, argumentTypes);
13837
ed3a1471e133 Add the Graal Native Function Interface
Matthias Grimmer <grimmer@ssw.jku.at>
parents:
diff changeset
103 }
ed3a1471e133 Add the Graal Native Function Interface
Matthias Grimmer <grimmer@ssw.jku.at>
parents:
diff changeset
104 }
13895
4731c1a0b1f3 consolidated GNFI code into graal.hotspot project and cleaned up the documentation and code
Doug Simon <doug.simon@oracle.com>
parents: 13872
diff changeset
105 // Fall back to default library path
4731c1a0b1f3 consolidated GNFI code into graal.hotspot project and cleaned up the documentation and code
Doug Simon <doug.simon@oracle.com>
parents: 13872
diff changeset
106 return getFunctionHandle(name, returnType, argumentTypes);
13837
ed3a1471e133 Add the Graal Native Function Interface
Matthias Grimmer <grimmer@ssw.jku.at>
parents:
diff changeset
107 }
ed3a1471e133 Add the Graal Native Function Interface
Matthias Grimmer <grimmer@ssw.jku.at>
parents:
diff changeset
108
ed3a1471e133 Add the Graal Native Function Interface
Matthias Grimmer <grimmer@ssw.jku.at>
parents:
diff changeset
109 @Override
15040
288c23143d47 Fix most raw type references.
Josef Eisl <josef.eisl@jku.at>
parents: 14143
diff changeset
110 public HotSpotNativeFunctionHandle getFunctionHandle(String name, Class<?> returnType, Class<?>... argumentTypes) {
13895
4731c1a0b1f3 consolidated GNFI code into graal.hotspot project and cleaned up the documentation and code
Doug Simon <doug.simon@oracle.com>
parents: 13872
diff changeset
111 if (rtldDefault == null) {
4731c1a0b1f3 consolidated GNFI code into graal.hotspot project and cleaned up the documentation and code
Doug Simon <doug.simon@oracle.com>
parents: 13872
diff changeset
112 throw new UnsatisfiedLinkError(name);
13837
ed3a1471e133 Add the Graal Native Function Interface
Matthias Grimmer <grimmer@ssw.jku.at>
parents:
diff changeset
113 }
13895
4731c1a0b1f3 consolidated GNFI code into graal.hotspot project and cleaned up the documentation and code
Doug Simon <doug.simon@oracle.com>
parents: 13872
diff changeset
114 return getFunctionHandle(rtldDefault, name, returnType, argumentTypes);
13837
ed3a1471e133 Add the Graal Native Function Interface
Matthias Grimmer <grimmer@ssw.jku.at>
parents:
diff changeset
115 }
ed3a1471e133 Add the Graal Native Function Interface
Matthias Grimmer <grimmer@ssw.jku.at>
parents:
diff changeset
116
13895
4731c1a0b1f3 consolidated GNFI code into graal.hotspot project and cleaned up the documentation and code
Doug Simon <doug.simon@oracle.com>
parents: 13872
diff changeset
117 private HotSpotNativeFunctionPointer lookupFunctionPointer(String name, NativeLibraryHandle library, boolean linkageErrorIfMissing) {
4731c1a0b1f3 consolidated GNFI code into graal.hotspot project and cleaned up the documentation and code
Doug Simon <doug.simon@oracle.com>
parents: 13872
diff changeset
118 if (name == null || library == null) {
4731c1a0b1f3 consolidated GNFI code into graal.hotspot project and cleaned up the documentation and code
Doug Simon <doug.simon@oracle.com>
parents: 13872
diff changeset
119 throw new NullPointerException();
4731c1a0b1f3 consolidated GNFI code into graal.hotspot project and cleaned up the documentation and code
Doug Simon <doug.simon@oracle.com>
parents: 13872
diff changeset
120 }
13905
3089e9a7cf44 fixed bug in passing primitive arrays through native function handles
Doug Simon <doug.simon@oracle.com>
parents: 13895
diff changeset
121
13895
4731c1a0b1f3 consolidated GNFI code into graal.hotspot project and cleaned up the documentation and code
Doug Simon <doug.simon@oracle.com>
parents: 13872
diff changeset
122 if (dllLookupFunctionHandle == null) {
4731c1a0b1f3 consolidated GNFI code into graal.hotspot project and cleaned up the documentation and code
Doug Simon <doug.simon@oracle.com>
parents: 13872
diff changeset
123 dllLookupFunctionHandle = createHandle(functionLookupFunctionPointer, long.class, long.class, long.class);
13837
ed3a1471e133 Add the Graal Native Function Interface
Matthias Grimmer <grimmer@ssw.jku.at>
parents:
diff changeset
124 }
13895
4731c1a0b1f3 consolidated GNFI code into graal.hotspot project and cleaned up the documentation and code
Doug Simon <doug.simon@oracle.com>
parents: 13872
diff changeset
125 long nameCString = createCString(name);
13837
ed3a1471e133 Add the Graal Native Function Interface
Matthias Grimmer <grimmer@ssw.jku.at>
parents:
diff changeset
126 try {
13895
4731c1a0b1f3 consolidated GNFI code into graal.hotspot project and cleaned up the documentation and code
Doug Simon <doug.simon@oracle.com>
parents: 13872
diff changeset
127 long functionPointer = (long) dllLookupFunctionHandle.call(((HotSpotNativeLibraryHandle) library).value, nameCString);
4731c1a0b1f3 consolidated GNFI code into graal.hotspot project and cleaned up the documentation and code
Doug Simon <doug.simon@oracle.com>
parents: 13872
diff changeset
128 if (functionPointer == 0L) {
4731c1a0b1f3 consolidated GNFI code into graal.hotspot project and cleaned up the documentation and code
Doug Simon <doug.simon@oracle.com>
parents: 13872
diff changeset
129 if (!linkageErrorIfMissing) {
4731c1a0b1f3 consolidated GNFI code into graal.hotspot project and cleaned up the documentation and code
Doug Simon <doug.simon@oracle.com>
parents: 13872
diff changeset
130 return null;
4731c1a0b1f3 consolidated GNFI code into graal.hotspot project and cleaned up the documentation and code
Doug Simon <doug.simon@oracle.com>
parents: 13872
diff changeset
131 }
4731c1a0b1f3 consolidated GNFI code into graal.hotspot project and cleaned up the documentation and code
Doug Simon <doug.simon@oracle.com>
parents: 13872
diff changeset
132 throw new UnsatisfiedLinkError(name);
4731c1a0b1f3 consolidated GNFI code into graal.hotspot project and cleaned up the documentation and code
Doug Simon <doug.simon@oracle.com>
parents: 13872
diff changeset
133 }
4731c1a0b1f3 consolidated GNFI code into graal.hotspot project and cleaned up the documentation and code
Doug Simon <doug.simon@oracle.com>
parents: 13872
diff changeset
134 return new HotSpotNativeFunctionPointer(functionPointer, name);
4731c1a0b1f3 consolidated GNFI code into graal.hotspot project and cleaned up the documentation and code
Doug Simon <doug.simon@oracle.com>
parents: 13872
diff changeset
135 } finally {
4731c1a0b1f3 consolidated GNFI code into graal.hotspot project and cleaned up the documentation and code
Doug Simon <doug.simon@oracle.com>
parents: 13872
diff changeset
136 unsafe.freeMemory(nameCString);
13837
ed3a1471e133 Add the Graal Native Function Interface
Matthias Grimmer <grimmer@ssw.jku.at>
parents:
diff changeset
137 }
ed3a1471e133 Add the Graal Native Function Interface
Matthias Grimmer <grimmer@ssw.jku.at>
parents:
diff changeset
138 }
ed3a1471e133 Add the Graal Native Function Interface
Matthias Grimmer <grimmer@ssw.jku.at>
parents:
diff changeset
139
ed3a1471e133 Add the Graal Native Function Interface
Matthias Grimmer <grimmer@ssw.jku.at>
parents:
diff changeset
140 @Override
15040
288c23143d47 Fix most raw type references.
Josef Eisl <josef.eisl@jku.at>
parents: 14143
diff changeset
141 public HotSpotNativeFunctionHandle getFunctionHandle(NativeFunctionPointer functionPointer, Class<?> returnType, Class<?>... argumentTypes) {
13895
4731c1a0b1f3 consolidated GNFI code into graal.hotspot project and cleaned up the documentation and code
Doug Simon <doug.simon@oracle.com>
parents: 13872
diff changeset
142 if (!(functionPointer instanceof HotSpotNativeFunctionPointer)) {
4731c1a0b1f3 consolidated GNFI code into graal.hotspot project and cleaned up the documentation and code
Doug Simon <doug.simon@oracle.com>
parents: 13872
diff changeset
143 throw new UnsatisfiedLinkError(functionPointer.getName());
13837
ed3a1471e133 Add the Graal Native Function Interface
Matthias Grimmer <grimmer@ssw.jku.at>
parents:
diff changeset
144 }
13895
4731c1a0b1f3 consolidated GNFI code into graal.hotspot project and cleaned up the documentation and code
Doug Simon <doug.simon@oracle.com>
parents: 13872
diff changeset
145 return createHandle(functionPointer, returnType, argumentTypes);
4731c1a0b1f3 consolidated GNFI code into graal.hotspot project and cleaned up the documentation and code
Doug Simon <doug.simon@oracle.com>
parents: 13872
diff changeset
146 }
4731c1a0b1f3 consolidated GNFI code into graal.hotspot project and cleaned up the documentation and code
Doug Simon <doug.simon@oracle.com>
parents: 13872
diff changeset
147
15040
288c23143d47 Fix most raw type references.
Josef Eisl <josef.eisl@jku.at>
parents: 14143
diff changeset
148 private HotSpotNativeFunctionHandle createHandle(NativeFunctionPointer functionPointer, Class<?> returnType, Class<?>... argumentTypes) {
13895
4731c1a0b1f3 consolidated GNFI code into graal.hotspot project and cleaned up the documentation and code
Doug Simon <doug.simon@oracle.com>
parents: 13872
diff changeset
149 HotSpotNativeFunctionPointer hs = (HotSpotNativeFunctionPointer) functionPointer;
16964
41f66e61b39a NFI: fix FunctionHandle resolving
Matthias Grimmer <grimmer@ssw.jku.at>
parents: 16874
diff changeset
150 if (hs != null) {
41f66e61b39a NFI: fix FunctionHandle resolving
Matthias Grimmer <grimmer@ssw.jku.at>
parents: 16874
diff changeset
151 InstalledCode code = installNativeFunctionStub(hs.value, returnType, argumentTypes);
41f66e61b39a NFI: fix FunctionHandle resolving
Matthias Grimmer <grimmer@ssw.jku.at>
parents: 16874
diff changeset
152 return new HotSpotNativeFunctionHandle(code, hs.name, argumentTypes);
41f66e61b39a NFI: fix FunctionHandle resolving
Matthias Grimmer <grimmer@ssw.jku.at>
parents: 16874
diff changeset
153 } else {
41f66e61b39a NFI: fix FunctionHandle resolving
Matthias Grimmer <grimmer@ssw.jku.at>
parents: 16874
diff changeset
154 return null;
41f66e61b39a NFI: fix FunctionHandle resolving
Matthias Grimmer <grimmer@ssw.jku.at>
parents: 16874
diff changeset
155 }
13895
4731c1a0b1f3 consolidated GNFI code into graal.hotspot project and cleaned up the documentation and code
Doug Simon <doug.simon@oracle.com>
parents: 13872
diff changeset
156 }
4731c1a0b1f3 consolidated GNFI code into graal.hotspot project and cleaned up the documentation and code
Doug Simon <doug.simon@oracle.com>
parents: 13872
diff changeset
157
4731c1a0b1f3 consolidated GNFI code into graal.hotspot project and cleaned up the documentation and code
Doug Simon <doug.simon@oracle.com>
parents: 13872
diff changeset
158 /**
4731c1a0b1f3 consolidated GNFI code into graal.hotspot project and cleaned up the documentation and code
Doug Simon <doug.simon@oracle.com>
parents: 13872
diff changeset
159 * Creates and installs a stub for calling a native function.
4731c1a0b1f3 consolidated GNFI code into graal.hotspot project and cleaned up the documentation and code
Doug Simon <doug.simon@oracle.com>
parents: 13872
diff changeset
160 */
15040
288c23143d47 Fix most raw type references.
Josef Eisl <josef.eisl@jku.at>
parents: 14143
diff changeset
161 private InstalledCode installNativeFunctionStub(long functionPointer, Class<?> returnType, Class<?>... argumentTypes) {
13895
4731c1a0b1f3 consolidated GNFI code into graal.hotspot project and cleaned up the documentation and code
Doug Simon <doug.simon@oracle.com>
parents: 13872
diff changeset
162 StructuredGraph g = getGraph(providers, factory, functionPointer, returnType, argumentTypes);
4731c1a0b1f3 consolidated GNFI code into graal.hotspot project and cleaned up the documentation and code
Doug Simon <doug.simon@oracle.com>
parents: 13872
diff changeset
163 Suites suites = providers.getSuites().createSuites();
19270
292442bed972 Rename LowLevelSuites to LIRSuites.
Josef Eisl <josef.eisl@jku.at>
parents: 19232
diff changeset
164 LIRSuites lirSuites = providers.getSuites().createLIRSuites();
13895
4731c1a0b1f3 consolidated GNFI code into graal.hotspot project and cleaned up the documentation and code
Doug Simon <doug.simon@oracle.com>
parents: 13872
diff changeset
165 PhaseSuite<HighTierContext> phaseSuite = backend.getSuites().getDefaultGraphBuilderSuite().copy();
4731c1a0b1f3 consolidated GNFI code into graal.hotspot project and cleaned up the documentation and code
Doug Simon <doug.simon@oracle.com>
parents: 13872
diff changeset
166 CallingConvention cc = getCallingConvention(providers.getCodeCache(), Type.JavaCallee, g.method(), false);
20068
cc3131ff7ce2 Remove GraphCache option.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents: 19823
diff changeset
167 CompilationResult compResult = GraalCompiler.compileGraph(g, cc, g.method(), providers, backend, backend.getTarget(), phaseSuite, OptimisticOptimizations.ALL,
19270
292442bed972 Rename LowLevelSuites to LIRSuites.
Josef Eisl <josef.eisl@jku.at>
parents: 19232
diff changeset
168 DefaultProfilingInfo.get(TriState.UNKNOWN), null, suites, lirSuites, new CompilationResult(), CompilationResultBuilderFactory.Default);
13895
4731c1a0b1f3 consolidated GNFI code into graal.hotspot project and cleaned up the documentation and code
Doug Simon <doug.simon@oracle.com>
parents: 13872
diff changeset
169 InstalledCode installedCode;
4731c1a0b1f3 consolidated GNFI code into graal.hotspot project and cleaned up the documentation and code
Doug Simon <doug.simon@oracle.com>
parents: 13872
diff changeset
170 try (Scope s = Debug.scope("CodeInstall", providers.getCodeCache(), g.method())) {
15162
dbb1fe8348e6 Allow to supply predefined installed code object when adding a method to the code cache.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents: 15040
diff changeset
171 installedCode = providers.getCodeCache().addMethod(g.method(), compResult, null, null);
17211
1ac6b4879443 Enforce catch-blocks for Debug.Scopes with context objects.
Josef Eisl <josef.eisl@jku.at>
parents: 16964
diff changeset
172 } catch (Throwable e) {
1ac6b4879443 Enforce catch-blocks for Debug.Scopes with context objects.
Josef Eisl <josef.eisl@jku.at>
parents: 16964
diff changeset
173 throw Debug.handle(e);
13895
4731c1a0b1f3 consolidated GNFI code into graal.hotspot project and cleaned up the documentation and code
Doug Simon <doug.simon@oracle.com>
parents: 13872
diff changeset
174 }
4731c1a0b1f3 consolidated GNFI code into graal.hotspot project and cleaned up the documentation and code
Doug Simon <doug.simon@oracle.com>
parents: 13872
diff changeset
175 return installedCode;
13837
ed3a1471e133 Add the Graal Native Function Interface
Matthias Grimmer <grimmer@ssw.jku.at>
parents:
diff changeset
176 }
ed3a1471e133 Add the Graal Native Function Interface
Matthias Grimmer <grimmer@ssw.jku.at>
parents:
diff changeset
177
ed3a1471e133 Add the Graal Native Function Interface
Matthias Grimmer <grimmer@ssw.jku.at>
parents:
diff changeset
178 @Override
13895
4731c1a0b1f3 consolidated GNFI code into graal.hotspot project and cleaned up the documentation and code
Doug Simon <doug.simon@oracle.com>
parents: 13872
diff changeset
179 public HotSpotNativeFunctionPointer getFunctionPointer(NativeLibraryHandle[] libraries, String name) {
4731c1a0b1f3 consolidated GNFI code into graal.hotspot project and cleaned up the documentation and code
Doug Simon <doug.simon@oracle.com>
parents: 13872
diff changeset
180 for (NativeLibraryHandle libraryHandle : libraries) {
4731c1a0b1f3 consolidated GNFI code into graal.hotspot project and cleaned up the documentation and code
Doug Simon <doug.simon@oracle.com>
parents: 13872
diff changeset
181 HotSpotNativeFunctionPointer functionPointer = lookupFunctionPointer(name, libraryHandle, false);
4731c1a0b1f3 consolidated GNFI code into graal.hotspot project and cleaned up the documentation and code
Doug Simon <doug.simon@oracle.com>
parents: 13872
diff changeset
182 if (functionPointer != null) {
13837
ed3a1471e133 Add the Graal Native Function Interface
Matthias Grimmer <grimmer@ssw.jku.at>
parents:
diff changeset
183 return functionPointer;
ed3a1471e133 Add the Graal Native Function Interface
Matthias Grimmer <grimmer@ssw.jku.at>
parents:
diff changeset
184 }
ed3a1471e133 Add the Graal Native Function Interface
Matthias Grimmer <grimmer@ssw.jku.at>
parents:
diff changeset
185 }
13895
4731c1a0b1f3 consolidated GNFI code into graal.hotspot project and cleaned up the documentation and code
Doug Simon <doug.simon@oracle.com>
parents: 13872
diff changeset
186 // Fall back to default library path
4731c1a0b1f3 consolidated GNFI code into graal.hotspot project and cleaned up the documentation and code
Doug Simon <doug.simon@oracle.com>
parents: 13872
diff changeset
187 if (rtldDefault == null) {
4731c1a0b1f3 consolidated GNFI code into graal.hotspot project and cleaned up the documentation and code
Doug Simon <doug.simon@oracle.com>
parents: 13872
diff changeset
188 throw new UnsatisfiedLinkError(name);
4731c1a0b1f3 consolidated GNFI code into graal.hotspot project and cleaned up the documentation and code
Doug Simon <doug.simon@oracle.com>
parents: 13872
diff changeset
189 }
16874
3044a847fe91 NFI: never throw an unsatisfiedLinkError - better return null
Matthias Grimmer <grimmer@ssw.jku.at>
parents: 16691
diff changeset
190 return lookupFunctionPointer(name, rtldDefault, false);
13837
ed3a1471e133 Add the Graal Native Function Interface
Matthias Grimmer <grimmer@ssw.jku.at>
parents:
diff changeset
191 }
ed3a1471e133 Add the Graal Native Function Interface
Matthias Grimmer <grimmer@ssw.jku.at>
parents:
diff changeset
192
13909
fac51a64fda0 made NativeFunctionInterfaceTest pass on Windows
Doug Simon <doug.simon@oracle.com>
parents: 13905
diff changeset
193 public boolean isDefaultLibrarySearchSupported() {
fac51a64fda0 made NativeFunctionInterfaceTest pass on Windows
Doug Simon <doug.simon@oracle.com>
parents: 13905
diff changeset
194 return rtldDefault != null;
fac51a64fda0 made NativeFunctionInterfaceTest pass on Windows
Doug Simon <doug.simon@oracle.com>
parents: 13905
diff changeset
195 }
fac51a64fda0 made NativeFunctionInterfaceTest pass on Windows
Doug Simon <doug.simon@oracle.com>
parents: 13905
diff changeset
196
13837
ed3a1471e133 Add the Graal Native Function Interface
Matthias Grimmer <grimmer@ssw.jku.at>
parents:
diff changeset
197 @Override
ed3a1471e133 Add the Graal Native Function Interface
Matthias Grimmer <grimmer@ssw.jku.at>
parents:
diff changeset
198 public NativeFunctionPointer getNativeFunctionPointerFromRawValue(long rawValue) {
13895
4731c1a0b1f3 consolidated GNFI code into graal.hotspot project and cleaned up the documentation and code
Doug Simon <doug.simon@oracle.com>
parents: 13872
diff changeset
199 return new HotSpotNativeFunctionPointer(rawValue, null);
13837
ed3a1471e133 Add the Graal Native Function Interface
Matthias Grimmer <grimmer@ssw.jku.at>
parents:
diff changeset
200 }
ed3a1471e133 Add the Graal Native Function Interface
Matthias Grimmer <grimmer@ssw.jku.at>
parents:
diff changeset
201 }