11/*
2- * Copyright (c) 2014, 2021 , Oracle and/or its affiliates. All rights reserved.
2+ * Copyright (c) 2014, 2022 , Oracle and/or its affiliates. All rights reserved.
33 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44 *
55 * This code is free software; you can redistribute it and/or modify it
3131import com .oracle .truffle .api .dsl .ImportStatic ;
3232import com .oracle .truffle .api .dsl .Specialization ;
3333import com .oracle .truffle .api .frame .VirtualFrame ;
34- import com .oracle .truffle .api .interop .InteropException ;
3534import com .oracle .truffle .api .interop .InteropLibrary ;
3635import com .oracle .truffle .api .interop .TruffleObject ;
3736import com .oracle .truffle .api .library .CachedLibrary ;
3837import com .oracle .truffle .api .nodes .Node ;
38+ import com .oracle .truffle .nfi .api .SignatureLibrary ;
3939import com .oracle .truffle .r .ffi .impl .nfi .TruffleNFI_CallFactory .TruffleNFI_InvokeCallNodeGen ;
4040import com .oracle .truffle .r .runtime .DSLConfig ;
4141import com .oracle .truffle .r .runtime .RInternalError ;
4949
5050public class TruffleNFI_Call implements CallRFFI {
5151
52- private static String getSignatureForArity (int arity ) {
53- CompilerAsserts .neverPartOfCompilation ();
54- StringBuilder str = new StringBuilder (10 * (arity + 2 )).append ("(" );
55- for (int i = 0 ; i < arity + 1 ; i ++) {
56- str .append (i > 0 ? ", " : "" );
57- str .append ("pointer" );
58- }
59- return str .append ("): pointer" ).toString ();
60- }
52+ abstract static class NodeAdapter extends Node {
53+ @ Child private SignatureLibrary signatures = SignatureLibrary .getFactory ().createDispatched (DSLConfig .getInteropLibraryCacheSize ());
6154
62- private abstract static class NodeAdapter extends Node {
63- @ Child private InteropLibrary interop = InteropLibrary .getFactory ().createDispatched (DSLConfig .getInteropLibraryCacheSize ());
55+ static Object getSignatureForArity (int arity ) {
56+ CompilerAsserts .neverPartOfCompilation ();
57+ StringBuilder str = new StringBuilder (10 * (arity + 2 )).append ("(" );
58+ for (int i = 0 ; i < arity + 1 ; i ++) {
59+ str .append (i > 0 ? ", " : "" );
60+ str .append ("pointer" );
61+ }
62+ String signature = str .append ("): pointer" ).toString ();
63+ return TruffleNFI_Context .parseSignature (signature );
64+ }
6465
6566 @ TruffleBoundary
66- protected TruffleObject getFunction (String name , String signature ) {
67+ protected TruffleObject getFunction (String name , Object signature ) {
6768 DLL .SymbolHandle symbolHandle = DLL .findSymbol (name , TruffleNFI_Context .getInstance ().getRLibDLLInfo ());
68- try {
69- return (TruffleObject ) interop .invokeMember (symbolHandle .asTruffleObject (), "bind" , signature );
70- } catch (InteropException ex ) {
71- throw RInternalError .shouldNotReachHere (ex );
72- }
69+ return (TruffleObject ) signatures .bind (signature , symbolHandle .asTruffleObject ());
7370 }
7471 }
7572
@@ -78,7 +75,12 @@ public abstract static class TruffleNFI_InvokeCallNode extends NodeAdapter imple
7875
7976 @ TruffleBoundary
8077 protected TruffleObject getFunction (int arity ) {
81- return getFunction ("dot_call" + arity , getSignatureForArity (arity ));
78+ return getFunction (arity , getSignatureForArity (arity ));
79+ }
80+
81+ @ TruffleBoundary
82+ protected TruffleObject getFunction (int arity , Object signature ) {
83+ return getFunction ("dot_call" + arity , signature );
8284 }
8385
8486 @ Specialization (guards = {"args.length == cachedArgsLength" , "nativeCallInfo.address.asTruffleObject() == cachedAddress" })
@@ -99,9 +101,10 @@ protected Object invokeCallCachedLength(NativeCallInfo nativeCallInfo, Object[]
99101 @ Cached ("createMaterializeNodess(cachedArgsLength)" ) FFIMaterializeNode [] ffiMaterializeNode ,
100102 @ Cached ("createWrapperNodes(cachedArgsLength)" ) FFIToNativeMirrorNode [] ffiToNativeMirrorNodes ,
101103 @ Cached ("create()" ) FFIUnwrapNode unwrap ,
104+ @ Cached ("getSignatureForArity(cachedArgsLength)" ) Object cachedSignature ,
102105 @ CachedLibrary (limit = "getInteropLibraryCacheSize()" ) InteropLibrary interop ) {
103- return doInvoke (nativeCallInfo , nativeCallInfo .address .asTruffleObject (), getFunction (cachedArgsLength ), args , cachedArgsLength , ffiMaterializeNode , ffiToNativeMirrorNodes , interop ,
104- unwrap );
106+ return doInvoke (nativeCallInfo , nativeCallInfo .address .asTruffleObject (), getFunction (cachedArgsLength , cachedSignature ), args , cachedArgsLength , ffiMaterializeNode ,
107+ ffiToNativeMirrorNodes , interop , unwrap );
105108 }
106109
107110 private static Object doInvoke (NativeCallInfo nativeCallInfo , TruffleObject address , TruffleObject function , Object [] args , int cachedArgsLength , FFIMaterializeNode [] ffiMaterializeNode ,
@@ -151,13 +154,13 @@ public void execute(VirtualFrame frame, NativeCallInfo nativeCallInfo, Object[]
151154 switch (args .length ) {
152155 case 0 :
153156 logCall (nativeCallInfo .name , args );
154- TruffleObject callVoid0Function = getFunction ("dot_call_void0" , CallVoid0Sig );
157+ TruffleObject callVoid0Function = getFunction ("dot_call_void0" , TruffleNFI_Context . parseSignature ( CallVoid0Sig ) );
155158 execute0Interop .execute (callVoid0Function , nativeCallInfo .address .asTruffleObject ());
156159 break ;
157160 case 1 :
158161 logCall (nativeCallInfo .name , args );
159162 wrappedArgs = ffiWrap .wrapAll (args , ffiMaterialize1 , ffiWrapper1 );
160- TruffleObject callVoid1Function = getFunction ("dot_call_void1" , CallVoid1Sig );
163+ TruffleObject callVoid1Function = getFunction ("dot_call_void1" , TruffleNFI_Context . parseSignature ( CallVoid1Sig ) );
161164 execute1Interop .execute (callVoid1Function , nativeCallInfo .address .asTruffleObject (), wrappedArgs [0 ]);
162165 break ;
163166 default :
0 commit comments