[Python-checkins] r45624 - in python/trunk/Modules/_ctypes: _ctypes.c libffi/fficonfig.py.in libffi_msvc/ffi.c libffi_msvc/ffi.h libffi_msvc/ffi_common.h libffi_msvc/ffitarget.h libffi_msvc/mingwin32.S libffi_msvc/prep_cif.c libffi_msvc/win32.S

thomas.heller python-checkins at python.org
Fri Apr 21 18:48:57 CEST 2006


Author: thomas.heller
Date: Fri Apr 21 18:48:56 2006
New Revision: 45624

Added:
   python/trunk/Modules/_ctypes/libffi_msvc/mingwin32.S
Modified:
   python/trunk/Modules/_ctypes/_ctypes.c
   python/trunk/Modules/_ctypes/libffi/fficonfig.py.in
   python/trunk/Modules/_ctypes/libffi_msvc/ffi.c
   python/trunk/Modules/_ctypes/libffi_msvc/ffi.h
   python/trunk/Modules/_ctypes/libffi_msvc/ffi_common.h
   python/trunk/Modules/_ctypes/libffi_msvc/ffitarget.h
   python/trunk/Modules/_ctypes/libffi_msvc/prep_cif.c
   python/trunk/Modules/_ctypes/libffi_msvc/win32.S
Log:
Merge in changes from ctypes 0.9.9.6 upstream version.

Modified: python/trunk/Modules/_ctypes/_ctypes.c
==============================================================================
--- python/trunk/Modules/_ctypes/_ctypes.c	(original)
+++ python/trunk/Modules/_ctypes/_ctypes.c	Fri Apr 21 18:48:56 2006
@@ -1223,6 +1223,19 @@
 			return value;
 		}
 	}
+/* function pointer */
+	if (CFuncPtrObject_Check(value)) {
+		PyCArgObject *parg;
+		CFuncPtrObject *func;
+		func = (CFuncPtrObject *)value;
+		parg = new_CArgObject();
+		parg->pffi_type = &ffi_type_pointer;
+		parg->tag = 'P';
+		Py_INCREF(value);
+		parg->value.p = *(void **)func->b_ptr;
+		parg->obj = value;
+		return (PyObject *)parg;
+	}
 /* c_char_p, c_wchar_p */
 	stgd = PyObject_stgdict(value);
 	if (stgd && CDataObject_Check(value) && stgd->proto && PyString_Check(stgd->proto)) {
@@ -4407,6 +4420,8 @@
 
 	if (PointerTypeObject_Check(arg))
 		return 1;
+	if (CFuncPtrTypeObject_Check(arg))
+		return 1;
 	dict = PyType_stgdict(arg);
 	if (dict) {
 		if (PyString_Check(dict->proto)
@@ -4566,7 +4581,7 @@
 #endif
 	PyModule_AddObject(m, "FUNCFLAG_CDECL", PyInt_FromLong(FUNCFLAG_CDECL));
 	PyModule_AddObject(m, "FUNCFLAG_PYTHONAPI", PyInt_FromLong(FUNCFLAG_PYTHONAPI));
-	PyModule_AddStringConstant(m, "__version__", "0.9.9.4");
+	PyModule_AddStringConstant(m, "__version__", "0.9.9.6");
 
 	PyModule_AddObject(m, "_memmove_addr", PyLong_FromVoidPtr(memmove));
 	PyModule_AddObject(m, "_memset_addr", PyLong_FromVoidPtr(memset));

Modified: python/trunk/Modules/_ctypes/libffi/fficonfig.py.in
==============================================================================
--- python/trunk/Modules/_ctypes/libffi/fficonfig.py.in	(original)
+++ python/trunk/Modules/_ctypes/libffi/fficonfig.py.in	Fri Apr 21 18:48:56 2006
@@ -31,5 +31,6 @@
 ffi_sources = [os.path.join('@srcdir@', f) for f in ffi_sources]
 
 ffi_cflags = '@CFLAGS@'
+# I think this may no longer be needed:
 if sys.platform == "openbsd3":
     ffi_cflags += " -fno-stack-protector"

Modified: python/trunk/Modules/_ctypes/libffi_msvc/ffi.c
==============================================================================
--- python/trunk/Modules/_ctypes/libffi_msvc/ffi.c	(original)
+++ python/trunk/Modules/_ctypes/libffi_msvc/ffi.c	Fri Apr 21 18:48:56 2006
@@ -26,8 +26,6 @@
    OTHER DEALINGS IN THE SOFTWARE.
    ----------------------------------------------------------------------- */
 
-#ifndef __x86_64__
-
 #include <ffi.h>
 #include <ffi_common.h>
 
@@ -143,11 +141,7 @@
 
 /*@-declundef@*/
 /*@-exportheader@*/
-#ifdef _MSC_VER
 extern int
-#else
-extern void
-#endif
 ffi_call_SYSV(void (*)(char *, extended_cif *), 
 	      /*@out@*/ extended_cif *, 
 	      unsigned, unsigned, 
@@ -156,14 +150,9 @@
 /*@=declundef@*/
 /*@=exportheader@*/
 
-#if defined(X86_WIN32) || defined(_MSC_VER)
 /*@-declundef@*/
 /*@-exportheader@*/
-#ifdef _MSC_VER
 extern int
-#else
-extern void
-#endif
 ffi_call_STDCALL(void (*)(char *, extended_cif *),
 		 /*@out@*/ extended_cif *,
 		 unsigned, unsigned,
@@ -171,13 +160,8 @@
 		 void (*fn)());
 /*@=declundef@*/
 /*@=exportheader@*/
-#endif /* X86_WIN32 || _MSC_VER*/
 
-#ifdef _MSC_VER
 int
-#else
-void
-#endif
 ffi_call(/*@dependent@*/ ffi_cif *cif, 
 	 void (*fn)(), 
 	 /*@out@*/ void *rvalue, 
@@ -206,24 +190,18 @@
     {
     case FFI_SYSV:
       /*@-usedef@*/
-#ifdef _MSC_VER
-      return
-#endif
-	      ffi_call_SYSV(ffi_prep_args, &ecif, cif->bytes, 
-			    cif->flags, ecif.rvalue, fn);
+      return ffi_call_SYSV(ffi_prep_args, &ecif, cif->bytes, 
+			   cif->flags, ecif.rvalue, fn);
       /*@=usedef@*/
       break;
-#if defined(X86_WIN32) || defined(_MSC_VER)
+
     case FFI_STDCALL:
       /*@-usedef@*/
-#ifdef _MSC_VER
-      return
-#endif
-	      ffi_call_STDCALL(ffi_prep_args, &ecif, cif->bytes,
-			       cif->flags, ecif.rvalue, fn);
+      return ffi_call_STDCALL(ffi_prep_args, &ecif, cif->bytes,
+			      cif->flags, ecif.rvalue, fn);
       /*@=usedef@*/
       break;
-#endif /* X86_WIN32 */
+
     default:
       FFI_ASSERT(0);
       break;
@@ -236,23 +214,10 @@
 
 static void ffi_prep_incoming_args_SYSV (char *stack, void **ret,
 					 void** args, ffi_cif* cif);
-#ifndef _MSC_VER
-static void ffi_closure_SYSV (ffi_closure *)
-     __attribute__ ((regparm(1)));
-static void ffi_closure_raw_SYSV (ffi_raw_closure *)
-     __attribute__ ((regparm(1)));
-#endif
-
 /* This function is jumped to by the trampoline */
 
-#ifdef _MSC_VER
 static void __fastcall
 ffi_closure_SYSV (ffi_closure *closure, int *argp)
-#else
-static void
-ffi_closure_SYSV (closure)
-     ffi_closure *closure;
-#endif
 {
   // this is our return value storage
   long double    res;
@@ -262,11 +227,11 @@
   void         **arg_area;
   unsigned short rtype;
   void          *resp = (void*)&res;
-#ifdef _MSC_VER
+//#ifdef _MSC_VER
   void *args = &argp[1];
-#else
-  void *args = __builtin_dwarf_cfa ();
-#endif
+//#else
+//  void *args = __builtin_dwarf_cfa ();
+//#endif
 
   cif         = closure->cif;
   arg_area    = (void**) alloca (cif->nargs * sizeof (void*));  
@@ -390,7 +355,7 @@
 
 /* MOV EDX, ESP is 0x8b 0xd4 */
 
-#ifdef _MSC_VER
+//#ifdef _MSC_VER
 
 #define FFI_INIT_TRAMPOLINE(TRAMP,FUN,CTX,BYTES) \
 { unsigned char *__tramp = (unsigned char*)(TRAMP); \
@@ -407,18 +372,18 @@
    *(unsigned short*) &__tramp[13] = BYTES; \
  }
 
-#else
-#define FFI_INIT_TRAMPOLINE(TRAMP,FUN,CTX,BYTES) \
-({ unsigned char *__tramp = (unsigned char*)(TRAMP); \
-   unsigned int  __fun = (unsigned int)(FUN); \
-   unsigned int  __ctx = (unsigned int)(CTX); \
-   unsigned int  __dis = __fun - ((unsigned int) __tramp + FFI_TRAMPOLINE_SIZE); \
-   *(unsigned char*) &__tramp[0] = 0xb8; \
-   *(unsigned int*)  &__tramp[1] = __ctx; /* movl __ctx, %eax */ \
-   *(unsigned char *)  &__tramp[5] = 0xe9; \
-   *(unsigned int*)  &__tramp[6] = __dis; /* jmp __fun  */ \
- })
-#endif
+//#else
+//#define FFI_INIT_TRAMPOLINE(TRAMP,FUN,CTX,BYTES) \
+//({ unsigned char *__tramp = (unsigned char*)(TRAMP); \
+//   unsigned int  __fun = (unsigned int)(FUN); \
+//   unsigned int  __ctx = (unsigned int)(CTX); \
+//   unsigned int  __dis = __fun - ((unsigned int) __tramp + FFI_TRAMPOLINE_SIZE); \
+//   *(unsigned char*) &__tramp[0] = 0xb8; \
+//   *(unsigned int*)  &__tramp[1] = __ctx; /* movl __ctx, %eax */ \
+//   *(unsigned char *)  &__tramp[5] = 0xe9; \
+//   *(unsigned int*)  &__tramp[6] = __dis; /* jmp __fun  */ \
+// })
+//#endif
 
 /* the cif must already be prep'ed */
 
@@ -433,10 +398,8 @@
   
   if (cif->abi == FFI_SYSV)
     bytes = 0;
-#ifdef _MSC_VER
   else if (cif->abi == FFI_STDCALL)
     bytes = cif->bytes;
-#endif
   else
     return FFI_BAD_ABI;
 
@@ -450,5 +413,3 @@
 
   return FFI_OK;
 }
-
-#endif /* __x86_64__  */

Modified: python/trunk/Modules/_ctypes/libffi_msvc/ffi.h
==============================================================================
--- python/trunk/Modules/_ctypes/libffi_msvc/ffi.h	(original)
+++ python/trunk/Modules/_ctypes/libffi_msvc/ffi.h	Fri Apr 21 18:48:56 2006
@@ -272,11 +272,7 @@
 			/*@dependent@*/ /*@out@*/ /*@partial@*/ ffi_type *rtype, 
 			/*@dependent@*/ ffi_type **atypes);
 
-#ifdef _MSC_VER
 int
-#else
-void
-#endif
 ffi_call(/*@dependent@*/ ffi_cif *cif, 
 	 void (*fn)(), 
 	 /*@out@*/ void *rvalue, 

Modified: python/trunk/Modules/_ctypes/libffi_msvc/ffi_common.h
==============================================================================
--- python/trunk/Modules/_ctypes/libffi_msvc/ffi_common.h	(original)
+++ python/trunk/Modules/_ctypes/libffi_msvc/ffi_common.h	Fri Apr 21 18:48:56 2006
@@ -13,24 +13,7 @@
 #endif
 
 #include <fficonfig.h>
-
-/* Do not move this. Some versions of AIX are very picky about where
-   this is positioned. */
-#ifdef __GNUC__
-# define alloca __builtin_alloca
-#else
-# if HAVE_ALLOCA_H
-#  include <alloca.h>
-# else
-#  ifdef _AIX
- #pragma alloca
-#  else
-#   ifndef alloca /* predefined by HP cc +Olibcalls */
-char *alloca ();
-#   endif
-#  endif
-# endif
-#endif
+#include <malloc.h>
 
 /* Check for the existence of memcpy. */
 #if STDC_HEADERS

Modified: python/trunk/Modules/_ctypes/libffi_msvc/ffitarget.h
==============================================================================
--- python/trunk/Modules/_ctypes/libffi_msvc/ffitarget.h	(original)
+++ python/trunk/Modules/_ctypes/libffi_msvc/ffitarget.h	Fri Apr 21 18:48:56 2006
@@ -43,23 +43,21 @@
   FFI_FIRST_ABI = 0,
 
   /* ---- Intel x86 Win32 ---------- */
-#if defined(X86_WIN32) || defined(_MSC_VER)
   FFI_SYSV,
   FFI_STDCALL,
   /* TODO: Add fastcall support for the sake of completeness */
   FFI_DEFAULT_ABI = FFI_SYSV,
-#endif
 
   /* ---- Intel x86 and AMD x86-64 - */
-#if !defined(X86_WIN32) && (defined(__i386__) || defined(__x86_64__))
-  FFI_SYSV,
-  FFI_UNIX64,   /* Unix variants all use the same ABI for x86-64  */
-#ifdef __i386__
-  FFI_DEFAULT_ABI = FFI_SYSV,
-#else
-  FFI_DEFAULT_ABI = FFI_UNIX64,
-#endif
-#endif
+/* #if !defined(X86_WIN32) && (defined(__i386__) || defined(__x86_64__)) */
+/*   FFI_SYSV, */
+/*   FFI_UNIX64,*/   /* Unix variants all use the same ABI for x86-64  */
+/* #ifdef __i386__ */
+/*   FFI_DEFAULT_ABI = FFI_SYSV, */
+/* #else */
+/*   FFI_DEFAULT_ABI = FFI_UNIX64, */
+/* #endif */
+/* #endif */
 
   FFI_LAST_ABI = FFI_DEFAULT_ABI + 1
 } ffi_abi;

Added: python/trunk/Modules/_ctypes/libffi_msvc/mingwin32.S
==============================================================================
--- (empty file)
+++ python/trunk/Modules/_ctypes/libffi_msvc/mingwin32.S	Fri Apr 21 18:48:56 2006
@@ -0,0 +1,228 @@
+/* -----------------------------------------------------------------------
+   win32.S - Copyright (c) 1996, 1998, 2001, 2002  Red Hat, Inc.
+	     Copyright (c) 2001  John Beniton
+	     Copyright (c) 2002  Ranjit Mathew
+			
+ 
+   X86 Foreign Function Interface
+ 
+   Permission is hereby granted, free of charge, to any person obtaining
+   a copy of this software and associated documentation files (the
+   ``Software''), to deal in the Software without restriction, including
+   without limitation the rights to use, copy, modify, merge, publish,
+   distribute, sublicense, and/or sell copies of the Software, and to
+   permit persons to whom the Software is furnished to do so, subject to
+   the following conditions:
+ 
+   The above copyright notice and this permission notice shall be included
+   in all copies or substantial portions of the Software.
+ 
+   THE SOFTWARE IS PROVIDED ``AS IS'', WITHOUT WARRANTY OF ANY KIND, EXPRESS
+   OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+   MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
+   IN NO EVENT SHALL CYGNUS SOLUTIONS BE LIABLE FOR ANY CLAIM, DAMAGES OR
+   OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
+   ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
+   OTHER DEALINGS IN THE SOFTWARE.
+   ----------------------------------------------------------------------- */
+ 
+#define LIBFFI_ASM
+#include <fficonfig.h>
+#include <ffi.h>
+ 
+.text
+ 
+.globl ffi_prep_args
+ 
+        # This assumes we are using gas.
+        .balign 16
+.globl _ffi_call_SYSV
+ 
+_ffi_call_SYSV:
+        pushl %ebp
+        movl  %esp,%ebp
+ 
+        # Make room for all of the new args.
+        movl  16(%ebp),%ecx                                                     
+        subl  %ecx,%esp
+ 
+        movl  %esp,%eax
+ 
+        # Place all of the ffi_prep_args in position
+        pushl 12(%ebp)
+        pushl %eax
+        call  *8(%ebp)
+ 
+        # Return stack to previous state and call the function
+        addl  $8,%esp
+ 
+        # FIXME: Align the stack to a 128-bit boundary to avoid
+        # potential performance hits.
+
+	call  *28(%ebp)
+ 
+        # Remove the space we pushed for the args
+        movl  16(%ebp),%ecx
+        addl  %ecx,%esp
+ 
+        # Load %ecx with the return type code
+        movl  20(%ebp),%ecx
+ 
+        # If the return value pointer is NULL, assume no return value.
+        cmpl  $0,24(%ebp)
+        jne   retint
+ 
+        # Even if there is no space for the return value, we are
+        # obliged to handle floating-point values.
+        cmpl  $2,%ecx  # Float_type
+        jne   noretval
+        fstp  %st(0)
+ 
+        jmp   epilogue
+ 
+retint:
+        cmpl  $1,%ecx    # Int_type
+        jne   retfloat
+        # Load %ecx with the pointer to storage for the return value
+        movl  24(%ebp),%ecx
+        movl  %eax,0(%ecx)
+        jmp   epilogue
+ 
+retfloat:
+        cmpl  $2,%ecx   # Float_type
+        jne   retdouble   
+         # Load %ecx with the pointer to storage for the return value
+        movl  24(%ebp),%ecx
+        fstps (%ecx)
+        jmp   epilogue
+ 
+retdouble:
+        cmpl  $3,%ecx     # Double_type
+        jne   retlongdouble
+        # Load %ecx with the pointer to storage for the return value
+        movl  24(%ebp),%ecx
+        fstpl (%ecx)
+        jmp   epilogue
+ 
+retlongdouble:
+        cmpl  $4,%ecx     # Longdouble_type
+        jne   retint64
+        # Load %ecx with the pointer to storage for the return value
+        movl  24(%ebp),%ecx
+        fstpt (%ecx)
+        jmp   epilogue
+ 
+retint64:
+        cmpl  $12,%ecx      # SINT64_type
+        jne   retstruct
+        # Load %ecx with the pointer to storage for the return value
+        movl  24(%ebp),%ecx
+        movl  %eax,0(%ecx)
+        movl  %edx,4(%ecx)
+ 
+retstruct:
+        # Nothing to do!
+ 
+noretval:
+epilogue:
+        movl %ebp,%esp
+        popl %ebp
+        ret
+ 
+.ffi_call_SYSV_end:
+
+        # This assumes we are using gas.
+        .balign 16
+.globl _ffi_call_STDCALL
+
+_ffi_call_STDCALL:
+        pushl %ebp
+        movl  %esp,%ebp
+
+        # Make room for all of the new args.
+        movl  16(%ebp),%ecx 
+        subl  %ecx,%esp
+
+        movl  %esp,%eax
+
+        # Place all of the ffi_prep_args in position
+        pushl 12(%ebp)
+        pushl %eax
+        call  *8(%ebp)
+
+        # Return stack to previous state and call the function
+        addl  $8,%esp
+
+        # FIXME: Align the stack to a 128-bit boundary to avoid
+        # potential performance hits.
+
+        call  *28(%ebp)
+
+        # stdcall functions pop arguments off the stack themselves
+
+        # Load %ecx with the return type code
+        movl  20(%ebp),%ecx
+
+        # If the return value pointer is NULL, assume no return value.
+        cmpl  $0,24(%ebp)
+        jne   sc_retint
+
+        # Even if there is no space for the return value, we are
+        # obliged to handle floating-point values.
+        cmpl  $2,%ecx      # Float_type
+        jne   sc_noretval
+        fstp  %st(0)
+
+        jmp   sc_epilogue
+
+sc_retint:
+        cmpl  $1,%ecx       # Int_type
+        jne   sc_retfloat
+        # Load %ecx with the pointer to storage for the return value
+        movl  24(%ebp),%ecx
+        movl  %eax,0(%ecx)
+        jmp   sc_epilogue
+
+sc_retfloat:
+        cmpl  $2,%ecx       # Float_type
+        jne   sc_retdouble
+         # Load %ecx with the pointer to storage for the return value
+        movl  24(%ebp),%ecx
+        fstps (%ecx)
+        jmp   sc_epilogue
+
+sc_retdouble:
+        cmpl  $2,%ecx       # Double_type
+        jne   sc_retlongdouble
+        # Load %ecx with the pointer to storage for the return value
+        movl  24(%ebp),%ecx
+        fstpl (%ecx)
+        jmp   sc_epilogue
+
+sc_retlongdouble:
+        cmpl  $4,%ecx      # Longdouble_type 
+        jne   sc_retint64
+        # Load %ecx with the pointer to storage for the return value
+        movl  24(%ebp),%ecx
+        fstpt (%ecx)
+        jmp   sc_epilogue
+
+sc_retint64:
+        cmpl  $12,%ecx      # SINT64_Type
+        jne   sc_retstruct
+        # Load %ecx with the pointer to storage for the return value
+        movl  24(%ebp),%ecx
+        movl  %eax,0(%ecx)
+        movl  %edx,4(%ecx)
+
+sc_retstruct:
+        # Nothing to do!
+
+sc_noretval:
+sc_epilogue:
+        movl %ebp,%esp
+        popl %ebp
+        ret
+
+.ffi_call_STDCALL_end:
+

Modified: python/trunk/Modules/_ctypes/libffi_msvc/prep_cif.c
==============================================================================
--- python/trunk/Modules/_ctypes/libffi_msvc/prep_cif.c	(original)
+++ python/trunk/Modules/_ctypes/libffi_msvc/prep_cif.c	Fri Apr 21 18:48:56 2006
@@ -147,7 +147,7 @@
       else
 #endif
 	{
-#ifndef _MSC_VER
+#if !defined(_MSC_VER) && !defined(__MINGW32__)
 		/* Don't know if this is a libffi bug or not.  At least on
 		   Windows with MSVC, function call parameters are *not*
 		   aligned in the same way as structure fields are, they are

Modified: python/trunk/Modules/_ctypes/libffi_msvc/win32.S
==============================================================================
--- python/trunk/Modules/_ctypes/libffi_msvc/win32.S	(original)
+++ python/trunk/Modules/_ctypes/libffi_msvc/win32.S	Fri Apr 21 18:48:56 2006
@@ -41,7 +41,11 @@
 _ffi_call_SYSV:
         pushl %ebp
         movl  %esp,%ebp
- 
+
+	#THe: save previous %esi, and store the current stack pointer in %esi
+	pushl %esi
+	movl %esp,%esi
+
         # Make room for all of the new args.
         movl  16(%ebp),%ecx                                                     
         subl  %ecx,%esp
@@ -64,7 +68,9 @@
         # Remove the space we pushed for the args
         movl  16(%ebp),%ecx
         addl  %ecx,%esp
- 
+
+	sub %esp,%esi # calculate stack pointer difference
+
         # Load %ecx with the return type code
         movl  20(%ebp),%ecx
  
@@ -125,6 +131,8 @@
  
 noretval:
 epilogue:
+	movl %esi,%eax # return the stack pointer detlta in %eax
+	popl %esi # restore previous %esi
         movl %ebp,%esp
         popl %ebp
         ret
@@ -139,6 +147,10 @@
         pushl %ebp
         movl  %esp,%ebp
 
+	#THe: save previous %esi, and store the current stack pointer in %esi
+	pushl %esi
+	movl %esp,%esi
+	
         # Make room for all of the new args.
         movl  16(%ebp),%ecx 
         subl  %ecx,%esp
@@ -158,6 +170,8 @@
 
         call  *28(%ebp)
 
+	sub %esp,%esi # difference in stack
+
         # stdcall functions pop arguments off the stack themselves
 
         # Load %ecx with the return type code
@@ -220,6 +234,8 @@
 
 sc_noretval:
 sc_epilogue:
+        movl %esi,%eax # return the stack difference
+	popl %esi # restore previous %esi value
         movl %ebp,%esp
         popl %ebp
         ret


More information about the Python-checkins mailing list