105105import com .oracle .graal .python .builtins .modules .ctypes .CFieldBuiltins .GetFuncNode ;
106106import com .oracle .graal .python .builtins .modules .ctypes .CtypesModuleBuiltinsClinicProviders .DyldSharedCacheContainsPathClinicProviderGen ;
107107import com .oracle .graal .python .builtins .modules .ctypes .CtypesNodes .PyTypeCheck ;
108+ import com .oracle .graal .python .builtins .modules .ctypes .FFIType .FFI_TYPES ;
108109import com .oracle .graal .python .builtins .modules .ctypes .FFIType .FieldGet ;
109110import com .oracle .graal .python .builtins .modules .ctypes .StgDictBuiltins .PyObjectStgDictNode ;
110111import com .oracle .graal .python .builtins .modules .ctypes .StgDictBuiltins .PyTypeStgDictNode ;
@@ -1138,7 +1139,7 @@ static Object check(VirtualFrame frame, int hresult,
11381139 }
11391140 }
11401141
1141- protected static final class argument {
1142+ protected static final class CTypesCallArgument {
11421143 FFIType ffi_type ;
11431144 StgDictObject stgDict ;
11441145 /*
@@ -1188,7 +1189,7 @@ Object _ctypes_callproc(VirtualFrame frame, NativeFunction pProc, Object[] argar
11881189 throw raiseNode .get (inliningTarget ).raise (ArgError , TOO_MANY_ARGUMENTS_D_MAXIMUM_IS_D , argcount , CTYPES_MAX_ARGCOUNT );
11891190 }
11901191
1191- argument [] args = new argument [argcount ];
1192+ CTypesCallArgument [] args = new CTypesCallArgument [argcount ];
11921193 Object [] avalues = new Object [argcount ];
11931194 FFIType [] atypes = new FFIType [argcount ];
11941195 int argtype_count = argtypes != null ? argtypes .length : 0 ;
@@ -1203,7 +1204,7 @@ Object _ctypes_callproc(VirtualFrame frame, NativeFunction pProc, Object[] argar
12031204 }
12041205 }
12051206 for (int i = 0 ; i < argcount ; ++i ) {
1206- args [i ] = new argument ();
1207+ args [i ] = new CTypesCallArgument ();
12071208 Object arg = argarray [i ];
12081209 /*
12091210 * For cdecl functions, we allow more actual arguments than the length of the
@@ -1223,7 +1224,7 @@ Object _ctypes_callproc(VirtualFrame frame, NativeFunction pProc, Object[] argar
12231224 if (arg instanceof PyCFuncPtrObject ) {
12241225 atypes [i ] = new FFIType (atypes [i ], ((PyCFuncPtrObject ) arg ).thunk );
12251226 }
1226- avalues [i ] = convertParameterToBackendValueNode .execute (inliningTarget , args [i ]. valuePointer , args [ i ]. ffi_type , args [ i ]. stgDict , mode );
1227+ avalues [i ] = convertParameterToBackendValueNode .execute (inliningTarget , args [i ], mode );
12271228 }
12281229
12291230 FFIType rtype = FFIType .ffi_type_sint ;
@@ -1481,14 +1482,14 @@ protected static StgDictObject getStgDict(Node inliningTarget, Object restype, P
14811482 @ SuppressWarnings ("truffle-inlining" ) // footprint reduction 124 -> 106
14821483 protected abstract static class ConvParamNode extends Node {
14831484
1484- final void execute (VirtualFrame frame , Object obj , int index , argument pa ) {
1485+ final void execute (VirtualFrame frame , Object obj , int index , CTypesCallArgument pa ) {
14851486 execute (frame , obj , index , pa , true );
14861487 }
14871488
1488- protected abstract void execute (VirtualFrame frame , Object obj , int index , argument pa , boolean allowRecursion );
1489+ protected abstract void execute (VirtualFrame frame , Object obj , int index , CTypesCallArgument pa , boolean allowRecursion );
14891490
14901491 @ Specialization
1491- void convParam (VirtualFrame frame , Object obj , int index , argument pa , boolean allowRecursion ,
1492+ void convParam (VirtualFrame frame , Object obj , int index , CTypesCallArgument pa , boolean allowRecursion ,
14921493 @ Bind ("this" ) Node inliningTarget ,
14931494 @ CachedLibrary (limit = "1" ) PythonBufferAccessLibrary bufferLib ,
14941495 @ Cached PyLongCheckNode longCheckNode ,
@@ -1589,95 +1590,88 @@ enum BackendMode {
15891590 @ GenerateCached (false )
15901591 @ ImportStatic ({FFIType .FFI_TYPES .class , BackendMode .class })
15911592 abstract static class ConvertParameterToBackendValueNode extends Node {
1592- public abstract Object execute (Node inliningTarget , Pointer ptr , FFIType ffiType , StgDictObject dict , BackendMode mode );
1593+ public abstract Object execute (Node inliningTarget , CTypesCallArgument arg , BackendMode mode );
15931594
1594- @ Specialization (guards = "ffiType.type == FFI_TYPE_SINT8 || ffiType.type == FFI_TYPE_UINT8" )
1595- static byte doByte (Node inliningTarget , Pointer pointer , @ SuppressWarnings ("unused" ) FFIType ffiType , @ SuppressWarnings ("unused" ) StgDictObject dict ,
1596- @ SuppressWarnings ("unused" ) BackendMode mode ,
1595+ @ Specialization (guards = "isFFIType(arg, FFI_TYPE_SINT8) || isFFIType(arg, FFI_TYPE_UINT8)" )
1596+ static byte doByte (Node inliningTarget , CTypesCallArgument arg , @ SuppressWarnings ("unused" ) BackendMode mode ,
15971597 @ Cached PointerNodes .ReadByteNode readByteNode ) {
1598- return readByteNode .execute (inliningTarget , pointer );
1598+ return readByteNode .execute (inliningTarget , arg . valuePointer );
15991599 }
16001600
1601- @ Specialization (guards = "ffiType.type == FFI_TYPE_SINT16 || ffiType.type == FFI_TYPE_UINT16" )
1602- static short doShort (Node inliningTarget , Pointer pointer , @ SuppressWarnings ("unused" ) FFIType ffiType , @ SuppressWarnings ("unused" ) StgDictObject dict ,
1603- @ SuppressWarnings ("unused" ) BackendMode mode ,
1601+ @ Specialization (guards = "isFFIType(arg, FFI_TYPE_SINT16) || isFFIType(arg, FFI_TYPE_UINT16)" )
1602+ static short doShort (Node inliningTarget , CTypesCallArgument arg , @ SuppressWarnings ("unused" ) BackendMode mode ,
16041603 @ Cached PointerNodes .ReadShortNode readShortNode ) {
1605- return readShortNode .execute (inliningTarget , pointer );
1604+ return readShortNode .execute (inliningTarget , arg . valuePointer );
16061605 }
16071606
1608- @ Specialization (guards = "ffiType.type == FFI_TYPE_SINT32 || ffiType.type == FFI_TYPE_UINT32" )
1609- static int doInt (Node inliningTarget , Pointer pointer , @ SuppressWarnings ("unused" ) FFIType ffiType , @ SuppressWarnings ("unused" ) StgDictObject dict ,
1610- @ SuppressWarnings ("unused" ) BackendMode mode ,
1607+ @ Specialization (guards = "isFFIType(arg, FFI_TYPE_SINT32) || isFFIType(arg, FFI_TYPE_UINT32)" )
1608+ static int doInt (Node inliningTarget , CTypesCallArgument arg , @ SuppressWarnings ("unused" ) BackendMode mode ,
16111609 @ Shared @ Cached PointerNodes .ReadIntNode readIntNode ) {
1612- return readIntNode .execute (inliningTarget , pointer );
1610+ return readIntNode .execute (inliningTarget , arg . valuePointer );
16131611 }
16141612
1615- @ Specialization (guards = "ffiType.type == FFI_TYPE_SINT64 || ffiType.type == FFI_TYPE_UINT64" )
1616- static long doLong (Node inliningTarget , Pointer pointer , @ SuppressWarnings ("unused" ) FFIType ffiType , @ SuppressWarnings ("unused" ) StgDictObject dict ,
1617- @ SuppressWarnings ("unused" ) BackendMode mode ,
1613+ @ Specialization (guards = "isFFIType(arg, FFI_TYPE_SINT64) || isFFIType(arg, FFI_TYPE_UINT64)" )
1614+ static long doLong (Node inliningTarget , CTypesCallArgument arg , @ SuppressWarnings ("unused" ) BackendMode mode ,
16181615 @ Shared @ Cached PointerNodes .ReadLongNode readLongNode ) {
1619- return readLongNode .execute (inliningTarget , pointer );
1616+ return readLongNode .execute (inliningTarget , arg . valuePointer );
16201617 }
16211618
1622- @ Specialization (guards = "ffiType.type == FFI_TYPE_FLOAT" )
1623- static float doFloat (Node inliningTarget , Pointer pointer , @ SuppressWarnings ("unused" ) FFIType ffiType , @ SuppressWarnings ("unused" ) StgDictObject dict ,
1624- @ SuppressWarnings ("unused" ) BackendMode mode ,
1619+ @ Specialization (guards = "isFFIType(arg, FFI_TYPE_FLOAT)" )
1620+ static float doFloat (Node inliningTarget , CTypesCallArgument arg , @ SuppressWarnings ("unused" ) BackendMode mode ,
16251621 @ Shared @ Cached PointerNodes .ReadIntNode readIntNode ) {
1626- return Float .intBitsToFloat (readIntNode .execute (inliningTarget , pointer ));
1622+ return Float .intBitsToFloat (readIntNode .execute (inliningTarget , arg . valuePointer ));
16271623 }
16281624
1629- @ Specialization (guards = "ffiType.type == FFI_TYPE_DOUBLE" )
1630- static double doDouble (Node inliningTarget , Pointer pointer , @ SuppressWarnings ("unused" ) FFIType ffiType , @ SuppressWarnings ("unused" ) StgDictObject dict ,
1631- @ SuppressWarnings ("unused" ) BackendMode mode ,
1625+ @ Specialization (guards = "isFFIType(arg, FFI_TYPE_DOUBLE)" )
1626+ static double doDouble (Node inliningTarget , CTypesCallArgument arg , @ SuppressWarnings ("unused" ) BackendMode mode ,
16321627 @ Shared @ Cached PointerNodes .ReadLongNode readLongNode ) {
1633- return Double .longBitsToDouble (readLongNode .execute (inliningTarget , pointer ));
1628+ return Double .longBitsToDouble (readLongNode .execute (inliningTarget , arg . valuePointer ));
16341629 }
16351630
1636- @ Specialization (guards = {"mode == NFI" , "ffiType.type == FFI_TYPE_POINTER" })
1637- static Object doNFIPointer (Node inliningTarget , Pointer pointer , @ SuppressWarnings ("unused" ) FFIType ffiType , @ SuppressWarnings ("unused" ) StgDictObject dict ,
1638- @ SuppressWarnings ("unused" ) BackendMode mode ,
1631+ @ Specialization (guards = {"mode == NFI" , "isFFIType(arg, FFI_TYPE_POINTER)" })
1632+ static Object doNFIPointer (Node inliningTarget , CTypesCallArgument arg , @ SuppressWarnings ("unused" ) BackendMode mode ,
16391633 @ Shared @ Cached PointerNodes .ReadPointerNode readPointerNode ,
16401634 @ Shared @ Cached PointerNodes .GetPointerValueAsObjectNode toNativeNode ) {
1641- Pointer value = readPointerNode .execute (inliningTarget , pointer );
1635+ Pointer value = readPointerNode .execute (inliningTarget , arg . valuePointer );
16421636 return toNativeNode .execute (inliningTarget , value );
16431637 }
16441638
1645- @ Specialization (guards = {"mode == LLVM" , "ffiType.type == FFI_TYPE_POINTER" })
1646- static Object doLLVMPointer (Node inliningTarget , Pointer pointer , @ SuppressWarnings ("unused" ) FFIType ffiType , @ SuppressWarnings ("unused" ) StgDictObject dict ,
1647- @ SuppressWarnings ("unused" ) BackendMode mode ,
1639+ @ Specialization (guards = {"mode == LLVM" , "isFFIType(arg, FFI_TYPE_POINTER)" })
1640+ static Object doLLVMPointer (Node inliningTarget , CTypesCallArgument arg , @ SuppressWarnings ("unused" ) BackendMode mode ,
16481641 @ Shared @ Cached PointerNodes .ReadPointerNode readPointerNode ,
16491642 @ Shared @ Cached PointerNodes .GetPointerValueAsObjectNode toNativeNode ) {
1650- Pointer value = readPointerNode .execute (inliningTarget , pointer );
1643+ Pointer value = readPointerNode .execute (inliningTarget , arg . valuePointer );
16511644 /*
16521645 * TODO this is currently the same as NFI, but here we have an opportunity to pass
16531646 * interop objects instead of allocating native memory.
16541647 */
16551648 return toNativeNode .execute (inliningTarget , value );
16561649 }
16571650
1658- @ Specialization (guards = {"mode == INTRINSIC" , "ffiType.type == FFI_TYPE_POINTER" })
1659- static Object doIntrinsicPointer (Node inliningTarget , Pointer pointer , @ SuppressWarnings ("unused" ) FFIType ffiType , @ SuppressWarnings ("unused" ) StgDictObject dict ,
1660- @ SuppressWarnings ("unused" ) BackendMode mode ,
1651+ @ Specialization (guards = {"mode == INTRINSIC" , "isFFIType(arg, FFI_TYPE_POINTER)" })
1652+ static Object doIntrinsicPointer (Node inliningTarget , CTypesCallArgument arg , @ SuppressWarnings ("unused" ) BackendMode mode ,
16611653 @ Shared @ Cached PointerNodes .ReadPointerNode readPointerNode ) {
1662- return readPointerNode .execute (inliningTarget , pointer );
1654+ return readPointerNode .execute (inliningTarget , arg . valuePointer );
16631655 }
16641656
1665- @ Specialization (guards = {"mode == NFI" , "ffiType.type == FFI_TYPE_STRUCT" })
1657+ @ Specialization (guards = {"mode == NFI" , "isFFIType(arg, FFI_TYPE_STRUCT) " })
16661658 @ SuppressWarnings ("unused" )
1667- static Object doNFIStruct (Node inliningTarget , Pointer pointer , @ SuppressWarnings ("unused" ) FFIType ffiType , @ SuppressWarnings ("unused" ) StgDictObject dict ,
1668- @ SuppressWarnings ("unused" ) BackendMode mode ,
1659+ static Object doNFIStruct (Node inliningTarget , CTypesCallArgument arg , @ SuppressWarnings ("unused" ) BackendMode mode ,
16691660 @ Cached (inline = false ) PRaiseNode raiseNode ) {
16701661 throw raiseNode .raise (PythonBuiltinClassType .NotImplementedError , ErrorMessages .PASSING_STRUCTS_BY_VALUE_NOT_SUPPORTED );
16711662 }
16721663
1673- @ Specialization (guards = {"mode == LLVM" , "ffiType.type == FFI_TYPE_STRUCT" })
1674- static Object doLLVMStruct (Node inliningTarget , Pointer pointer , @ SuppressWarnings ("unused" ) FFIType ffiType , StgDictObject dict ,
1675- @ SuppressWarnings ("unused" ) BackendMode mode ,
1664+ @ Specialization (guards = {"mode == LLVM" , "isFFIType(arg, FFI_TYPE_STRUCT)" })
1665+ static Object doLLVMStruct (Node inliningTarget , CTypesCallArgument arg , @ SuppressWarnings ("unused" ) BackendMode mode ,
16761666 @ Cached PointerNodes .ReadBytesNode readBytesNode ) {
1677- byte [] bytes = new byte [dict .size ];
1667+ byte [] bytes = new byte [arg . stgDict .size ];
16781668 // TODO avoid copying the bytes if possible
1679- readBytesNode .execute (inliningTarget , bytes , 0 , pointer , dict .size );
1680- return CDataObject .createWrapper (dict , bytes );
1669+ readBytesNode .execute (inliningTarget , bytes , 0 , arg .valuePointer , arg .stgDict .size );
1670+ return CDataObject .createWrapper (arg .keep , arg .stgDict , bytes );
1671+ }
1672+
1673+ static boolean isFFIType (CTypesCallArgument arg , FFI_TYPES expected ) {
1674+ return arg .ffi_type .type == expected ;
16811675 }
16821676 }
16831677
0 commit comments