This is the mail archive of the gcc-patches@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[committed 4.1] PA libffi backport


The Debian project has expressed a desire to have bug fixes relevant
to libjava backported to the 4.1 branch.  As Java is non critical, I
backported a number of changes to libffi found in the development of
the hpux port.  These changes fix unaligned exceptions that sometimes
occur in the handling of small structs and doubles.

There are also a few cleanups present in the patch that don't affect
functionality.

The change has been tested on hppa-unknown-linux-gnu.  There are now
no fails in the libffi and libjava testsuites on the test system.

Dave
-- 
J. David Anglin                                  dave.anglin@nrc-cnrc.gc.ca
National Research Council of Canada              (613) 990-0752 (FAX: 952-6602)

2006-06-22  John David Anglin  <dave.anglin@nrc-cnrc.gc.ca>

	Backport from trunk:
	2006-05-18  John David Anglin  <dave.anglin@nrc-cnrc.gc.ca>

	* pa/ffi.c (ffi_prep_args_LINUX): Load floating point arguments
	from stack slot.

	Backport from trunk:
	2006-04-22  Andreas Tobler  <a.tobler@schweiz.ch>

	* src/pa/ffi.c: Remove lint directives.

	Backport from trunk linux relevant portions of following change:
	2006-04-05  John David Anglin  <dave.anglin@nrc-cnrc.gc.ca>
	            Andreas Tobler  <a.tobler@schweiz.ch>

	* src/pa/ffitarget.h: 
	(FFI_TYPE_SMALL_STRUCT2): Define.
	(FFI_TYPE_SMALL_STRUCT4): Likewise.
	(FFI_TYPE_SMALL_STRUCT8): Likewise.
	(FFI_TYPE_SMALL_STRUCT3): Redefine.
	(FFI_TYPE_SMALL_STRUCT5): Likewise.
	(FFI_TYPE_SMALL_STRUCT6): Likewise.
	(FFI_TYPE_SMALL_STRUCT7): Likewise.
	* src/pa/ffi.c (ROUND_DOWN): Delete.
	(fldw, fstw, fldd, fstd): Use '__asm__'.
	(ffi_struct_type): Add support for FFI_TYPE_SMALL_STRUCT2,
	FFI_TYPE_SMALL_STRUCT4 and FFI_TYPE_SMALL_STRUCT8.
	(ffi_prep_args_LINUX): Update comment.
	Simplify incrementing of stack slot variable. Change type of local
	'n' to unsigned int.
	(ffi_closure_inner_LINUX): Change return type to ffi_status.
	Simplify incrementing of stack slot variable.  Reformat debug
	statement.  Add support for FFI_TYPE_SMALL_STRUCT2,
	FFI_TYPE_SMALL_STRUCT4 and FFI_TYPE_SMALL_STRUCT8.
	(ffi_closure_LINUX): Add 'extern' to declaration.
	(ffi_prep_closure): Add nops to cache flush.
	* src/pa/linux.S (ffi_call_LINUX): Localize labels. Add support
	for 2, 4 and 8-byte small structs. Handle unaligned destinations
	in 3, 5, 6 and 7-byte small structs. Order argument type checks
	so that common argument types appear first.

Index: src/pa/linux.S
===================================================================
--- src/pa/linux.S	(revision 114867)
+++ src/pa/linux.S	(working copy)
@@ -73,8 +73,8 @@
 .LCFI13:
 	copy %sp, %r4
 
-	addl %arg2, %r4, %arg0                  /* arg stack */
-	stw %arg3, -48(%r3)                     /* save flags; we need it later */
+	addl %arg2, %r4, %arg0      /* arg stack */
+	stw %arg3, -48(%r3)         /* save flags; we need it later */
 
 	/* Call prep_args:
 	   %arg0(stack) -- set up above
@@ -106,90 +106,139 @@
 
 	/* Store the result according to the return type.  */
 
-checksmst3:
-	comib,<>,n FFI_TYPE_SMALL_STRUCT3, %r21, checksmst567
-	/* 3-byte structs are returned in ret0 as ??xxyyzz.  Shift
-	   left 8 bits to write to the result structure.  */
-	zdep %ret0, 23, 24, %r22
-	b done
-	stw %r22, 0(%r20)
-
-checksmst567:
-	/* 5-7 byte values are returned right justified:
+.Lcheckint:
+	comib,<>,n FFI_TYPE_INT, %r21, .Lcheckint8
+	b	.Ldone
+	stw	%ret0, 0(%r20)
+
+.Lcheckint8:
+	comib,<>,n FFI_TYPE_UINT8, %r21, .Lcheckint16
+	b	.Ldone
+	stb	%ret0, 0(%r20)
+
+.Lcheckint16:
+	comib,<>,n FFI_TYPE_UINT16, %r21, .Lcheckdbl
+	b	.Ldone
+	sth	%ret0, 0(%r20)
+
+.Lcheckdbl:
+	comib,<>,n FFI_TYPE_DOUBLE, %r21, .Lcheckfloat
+	b	.Ldone
+	fstd	%fr4,0(%r20)
+
+.Lcheckfloat:
+	comib,<>,n FFI_TYPE_FLOAT, %r21, .Lcheckll
+	b	.Ldone
+	fstw	%fr4L,0(%r20)
+
+.Lcheckll:
+	comib,<>,n FFI_TYPE_UINT64, %r21, .Lchecksmst2
+	stw	%ret0, 0(%r20)
+	b	.Ldone
+	stw	%ret1, 4(%r20)
+
+.Lchecksmst2:
+	comib,<>,n FFI_TYPE_SMALL_STRUCT2, %r21, .Lchecksmst3
+	/* 2-byte structs are returned in ret0 as ????xxyy.  */
+	extru	%ret0, 23, 8, %r22
+	stbs,ma	%r22, 1(%r20)
+	b	.Ldone
+	stb	%ret0, 0(%r20)
+
+.Lchecksmst3:
+	comib,<>,n FFI_TYPE_SMALL_STRUCT3, %r21, .Lchecksmst4
+	/* 3-byte structs are returned in ret0 as ??xxyyzz.  */
+	extru	%ret0, 15, 8, %r22
+	stbs,ma	%r22, 1(%r20)
+	extru	%ret0, 23, 8, %r22
+	stbs,ma	%r22, 1(%r20)
+	b	.Ldone
+	stb	%ret0, 0(%r20)
+
+.Lchecksmst4:
+	comib,<>,n FFI_TYPE_SMALL_STRUCT4, %r21, .Lchecksmst5
+	/* 4-byte structs are returned in ret0 as wwxxyyzz.  */
+	extru	%ret0, 7, 8, %r22
+	stbs,ma	%r22, 1(%r20)
+	extru	%ret0, 15, 8, %r22
+	stbs,ma	%r22, 1(%r20)
+	extru	%ret0, 23, 8, %r22
+	stbs,ma	%r22, 1(%r20)
+	b	.Ldone
+	stb	%ret0, 0(%r20)
+
+.Lchecksmst5:
+	comib,<>,n FFI_TYPE_SMALL_STRUCT5, %r21, .Lchecksmst6
+	/* 5 byte values are returned right justified:
+	      ret0     ret1
+	   5: ??????aa bbccddee */
+	stbs,ma	%ret0, 1(%r20)
+	extru	%ret1, 7, 8, %r22
+	stbs,ma	%r22, 1(%r20)
+	extru	%ret1, 15, 8, %r22
+	stbs,ma	%r22, 1(%r20)
+	extru	%ret1, 23, 8, %r22
+	stbs,ma	%r22, 1(%r20)
+	b	.Ldone
+	stb	%ret1, 0(%r20)
+
+.Lchecksmst6:
+	comib,<>,n FFI_TYPE_SMALL_STRUCT6, %r21, .Lchecksmst7
+	/* 6 byte values are returned right justified:
+	      ret0     ret1
+	   6: ????aabb ccddeeff */
+	extru	%ret0, 23, 8, %r22
+	stbs,ma	%r22, 1(%r20)
+	stbs,ma	%ret0, 1(%r20)
+	extru	%ret1, 7, 8, %r22
+	stbs,ma	%r22, 1(%r20)
+	extru	%ret1, 15, 8, %r22
+	stbs,ma	%r22, 1(%r20)
+	extru	%ret1, 23, 8, %r22
+	stbs,ma	%r22, 1(%r20)
+	b	.Ldone
+	stb	%ret1, 0(%r20)
+
+.Lchecksmst7:
+	comib,<>,n FFI_TYPE_SMALL_STRUCT7, %r21, .Lchecksmst8
+	/* 7 byte values are returned right justified:
 	      ret0     ret1
-	   5: ??????aa bbccddee
-	   6: ????aabb ccddeeff
-	   7: ??aabbcc ddeeffgg
-
-	   To store this in the result, write the first 4 bytes into a temp
-	   register using shrpw (t1 = aabbccdd), followed by a rotation of
-	   ret1:
-
-	      ret0     ret1	   ret1
-	   5: ??????aa bbccddee -> eebbccdd (rotate 8)
-	   6: ????aabb ccddeeff -> eeffccdd (rotate 16)
-	   7: ??aabbcc ddeeffgg -> eeffggdd (rotate 24)
-
-	   then we write (t1, ret1) into the result.  */
-
-	addi,<> -FFI_TYPE_SMALL_STRUCT5,%r21,%r0
-	ldi 8, %r22
-	addi,<> -FFI_TYPE_SMALL_STRUCT6,%r21,%r0
-	ldi 16, %r22
-	addi,<> -FFI_TYPE_SMALL_STRUCT7,%r21,%r0
-	ldi 24, %r22
-
-	/* This relies on all the FFI_TYPE_*_STRUCT* defines being <0 */
-	cmpib,<=,n 0, %r21, checkint8
-	mtsar %r22
-
-	shrpw %ret0, %ret1, %sar, %ret0  /* ret0 = aabbccdd */
-	shrpw %ret1, %ret1, %sar, %ret1  /* rotate ret1 */
-	
-	stw %ret0, 0(%r20)
-	b done
-	stw %ret1, 4(%r20)
-
-checkint8:
-	comib,<>,n FFI_TYPE_UINT8, %r21, checkint16
-	b done
-	stb %ret0, 0(%r20)
-
-checkint16:
-	comib,<>,n FFI_TYPE_UINT16, %r21, checkint32
-	b done
-	sth %ret0, 0(%r20)
-
-checkint32:
-	comib,<>,n FFI_TYPE_UINT32, %r21, checkint
-	b done
-	stw %ret0, 0(%r20)
-
-checkint:
-	comib,<>,n FFI_TYPE_INT, %r21, checkll
-	b done
-	stw %ret0, 0(%r20)
-
-checkll:
-	comib,<>,n FFI_TYPE_UINT64, %r21, checkdbl
-	stw %ret0, 0(%r20)
-	b done
-	stw %ret1, 4(%r20)
-
-checkdbl:
-	comib,<>,n FFI_TYPE_DOUBLE, %r21, checkfloat
-	b done
-	fstd %fr4,0(%r20)
-
-checkfloat:
-	comib,<>,n FFI_TYPE_FLOAT, %r21, done
-	fstw %fr4L,0(%r20)
-
-	/* structure returns are either handled by one of the
-	   INT/UINT64 cases above, or, if passed by pointer,
-	   is handled by the callee.  */
+	   7: ??aabbcc ddeeffgg */
+	extru	%ret0, 15, 8, %r22
+	stbs,ma	%r22, 1(%r20)
+	extru	%ret0, 23, 8, %r22
+	stbs,ma	%r22, 1(%r20)
+	stbs,ma	%ret0, 1(%r20)
+	extru	%ret1, 7, 8, %r22
+	stbs,ma	%r22, 1(%r20)
+	extru	%ret1, 15, 8, %r22
+	stbs,ma	%r22, 1(%r20)
+	extru	%ret1, 23, 8, %r22
+	stbs,ma	%r22, 1(%r20)
+	b	.Ldone
+	stb	%ret1, 0(%r20)
+
+.Lchecksmst8:
+	comib,<>,n FFI_TYPE_SMALL_STRUCT8, %r21, .Ldone
+	/* 8 byte values are returned right justified:
+	      ret0     ret1
+	   8: aabbccdd eeffgghh */
+	extru	%ret0, 7, 8, %r22
+	stbs,ma	%r22, 1(%r20)
+	extru	%ret0, 15, 8, %r22
+	stbs,ma	%r22, 1(%r20)
+	extru	%ret0, 23, 8, %r22
+	stbs,ma	%r22, 1(%r20)
+	stbs,ma	%ret0, 1(%r20)
+	extru	%ret1, 7, 8, %r22
+	stbs,ma	%r22, 1(%r20)
+	extru	%ret1, 15, 8, %r22
+	stbs,ma	%r22, 1(%r20)
+	extru	%ret1, 23, 8, %r22
+	stbs,ma	%r22, 1(%r20)
+	stb	%ret1, 0(%r20)
 
-done:
+.Ldone:
 	/* all done, return */
 	copy %r4, %sp                           /* pop arg stack */
 	ldw 12(%r3), %r4
@@ -299,7 +348,7 @@
 	.sleb128 -5
 
 	.byte   0x4     ;# DW_CFA_advance_loc4
-	.word   .LCFI12-.LCFI11
+	.word   .LCFI22-.LCFI21
 	.byte   0xd     ;# DW_CFA_def_cfa_register = r3
 	.uleb128 0x3
 
Index: src/pa/ffitarget.h
===================================================================
--- src/pa/ffitarget.h	(revision 114867)
+++ src/pa/ffitarget.h	(working copy)
@@ -51,9 +51,11 @@
 
 #define FFI_TRAMPOLINE_SIZE 32
 
-#define FFI_TYPE_SMALL_STRUCT3 -1
-#define FFI_TYPE_SMALL_STRUCT5 -2
-#define FFI_TYPE_SMALL_STRUCT6 -3
-#define FFI_TYPE_SMALL_STRUCT7 -4
+#define FFI_TYPE_SMALL_STRUCT2 -1
+#define FFI_TYPE_SMALL_STRUCT3 -2
+#define FFI_TYPE_SMALL_STRUCT4 -3
+#define FFI_TYPE_SMALL_STRUCT5 -4
+#define FFI_TYPE_SMALL_STRUCT6 -5
+#define FFI_TYPE_SMALL_STRUCT7 -6
+#define FFI_TYPE_SMALL_STRUCT8 -7
 #endif
-
Index: src/pa/ffi.c
===================================================================
--- src/pa/ffi.c	(revision 114867)
+++ src/pa/ffi.c	(working copy)
@@ -30,15 +30,19 @@
 #include <stdio.h>
 
 #define ROUND_UP(v, a)  (((size_t)(v) + (a) - 1) & ~((a) - 1))
-#define ROUND_DOWN(v, a)  (((size_t)(v) - (a) + 1) & ~((a) - 1))
+
 #define MIN_STACK_SIZE  64
 #define FIRST_ARG_SLOT  9
 #define DEBUG_LEVEL   0
 
-#define fldw(addr, fpreg) asm volatile ("fldw 0(%0), %%" #fpreg "L" : : "r"(addr) : #fpreg)
-#define fstw(fpreg, addr) asm volatile ("fstw %%" #fpreg "L, 0(%0)" : : "r"(addr))
-#define fldd(addr, fpreg) asm volatile ("fldd 0(%0), %%" #fpreg : : "r"(addr) : #fpreg)
-#define fstd(fpreg, addr) asm volatile ("fstd %%" #fpreg "L, 0(%0)" : : "r"(addr))
+#define fldw(addr, fpreg) \
+  __asm__ volatile ("fldw 0(%0), %%" #fpreg "L" : : "r"(addr) : #fpreg)
+#define fstw(fpreg, addr) \
+  __asm__ volatile ("fstw %%" #fpreg "L, 0(%0)" : : "r"(addr))
+#define fldd(addr, fpreg) \
+  __asm__ volatile ("fldd 0(%0), %%" #fpreg : : "r"(addr) : #fpreg)
+#define fstd(fpreg, addr) \
+  __asm__ volatile ("fstd %%" #fpreg "L, 0(%0)" : : "r"(addr))
 
 #define debug(lvl, x...) do { if (lvl <= DEBUG_LEVEL) { printf(x); } } while (0)
 
@@ -47,16 +51,19 @@
   size_t sz = t->size;
 
   /* Small structure results are passed in registers,
-     larger ones are passed by pointer.  */
+     larger ones are passed by pointer.  Note that
+     small structures of size 2, 4 and 8 differ from
+     the corresponding integer types in that they have
+     different alignment requirements.  */
 
   if (sz <= 1)
     return FFI_TYPE_UINT8;
   else if (sz == 2)
-    return FFI_TYPE_UINT16;
+    return FFI_TYPE_SMALL_STRUCT2;
   else if (sz == 3)
     return FFI_TYPE_SMALL_STRUCT3;
   else if (sz == 4)
-    return FFI_TYPE_UINT32;
+    return FFI_TYPE_SMALL_STRUCT4;
   else if (sz == 5)
     return FFI_TYPE_SMALL_STRUCT5;
   else if (sz == 6)
@@ -64,61 +71,80 @@
   else if (sz == 7)
     return FFI_TYPE_SMALL_STRUCT7;
   else if (sz <= 8)
-    return FFI_TYPE_UINT64;
+    return FFI_TYPE_SMALL_STRUCT8;
   else
     return FFI_TYPE_STRUCT; /* else, we pass it by pointer.  */
 }
 
 /* PA has a downward growing stack, which looks like this:
-  
+
    Offset
-        [ Variable args ]
+	[ Variable args ]
    SP = (4*(n+9))       arg word N
    ...
    SP-52                arg word 4
-        [ Fixed args ]
+	[ Fixed args ]
    SP-48                arg word 3
    SP-44                arg word 2
    SP-40                arg word 1
    SP-36                arg word 0
-        [ Frame marker ]
+	[ Frame marker ]
    ...
    SP-20                RP
    SP-4                 previous SP
-  
-   First 4 non-FP 32-bit args are passed in gr26, gr25, gr24 and gr23
-   First 2 non-FP 64-bit args are passed in register pairs, starting
-     on an even numbered register (i.e. r26/r25 and r24+r23)
-   First 4 FP 32-bit arguments are passed in fr4L, fr5L, fr6L and fr7L
-   First 2 FP 64-bit arguments are passed in fr5 and fr7
-   The rest are passed on the stack starting at SP-52, but 64-bit
-     arguments need to be aligned to an 8-byte boundary
-  
+
+   The first four argument words on the stack are reserved for use by
+   the callee.  Instead, the general and floating registers replace
+   the first four argument slots.  Non FP arguments are passed solely
+   in the general registers.  FP arguments are passed in both general
+   and floating registers when using libffi.
+
+   Non-FP 32-bit args are passed in gr26, gr25, gr24 and gr23.
+   Non-FP 64-bit args are passed in register pairs, starting
+   on an odd numbered register (i.e. r25+r26 and r23+r24).
+   FP 32-bit arguments are passed in fr4L, fr5L, fr6L and fr7L.
+   FP 64-bit arguments are passed in fr5 and fr7.
+
+   The registers are allocated in the same manner as stack slots.
+   This allows the callee to save its arguments on the stack if
+   necessary:
+
+   arg word 3 -> gr23 or fr7L
+   arg word 2 -> gr24 or fr6L or fr7R
+   arg word 1 -> gr25 or fr5L
+   arg word 0 -> gr26 or fr4L or fr5R
+
+   Note that fr4R and fr6R are never used for arguments (i.e.,
+   doubles are not passed in fr4 or fr6).
+
+   The rest of the arguments are passed on the stack starting at SP-52,
+   but 64-bit arguments need to be aligned to an 8-byte boundary
+
    This means we can have holes either in the register allocation,
    or in the stack.  */
 
 /* ffi_prep_args is called by the assembly routine once stack space
    has been allocated for the function's arguments
-  
+
    The following code will put everything into the stack frame
    (which was allocated by the asm routine), and on return
    the asm routine will load the arguments that should be
    passed by register into the appropriate registers
-  
+
    NOTE: We load floating point args in this function... that means we
    assume gcc will not mess with fp regs in here.  */
 
-/*@-exportheader@*/
 void ffi_prep_args_LINUX(UINT32 *stack, extended_cif *ecif, unsigned bytes)
-/*@=exportheader@*/
 {
   register unsigned int i;
   register ffi_type **p_arg;
   register void **p_argv;
-  unsigned int slot = FIRST_ARG_SLOT - 1;
+  unsigned int slot = FIRST_ARG_SLOT;
   char *dest_cpy;
+  size_t len;
 
-  debug(1, "%s: stack = %p, ecif = %p, bytes = %u\n", __FUNCTION__, stack, ecif, bytes);
+  debug(1, "%s: stack = %p, ecif = %p, bytes = %u\n", __FUNCTION__, stack,
+	ecif, bytes);
 
   p_arg = ecif->cif->arg_types;
   p_argv = ecif->avalue;
@@ -130,73 +156,60 @@
       switch (type)
 	{
 	case FFI_TYPE_SINT8:
-	  slot++;
 	  *(SINT32 *)(stack - slot) = *(SINT8 *)(*p_argv);
 	  break;
 
 	case FFI_TYPE_UINT8:
-	  slot++;
 	  *(UINT32 *)(stack - slot) = *(UINT8 *)(*p_argv);
 	  break;
 
 	case FFI_TYPE_SINT16:
-	  slot++;
 	  *(SINT32 *)(stack - slot) = *(SINT16 *)(*p_argv);
 	  break;
 
 	case FFI_TYPE_UINT16:
-	  slot++;
 	  *(UINT32 *)(stack - slot) = *(UINT16 *)(*p_argv);
 	  break;
 
 	case FFI_TYPE_UINT32:
 	case FFI_TYPE_SINT32:
 	case FFI_TYPE_POINTER:
-	  slot++;
-	  debug(3, "Storing UINT32 %u in slot %u\n", *(UINT32 *)(*p_argv), slot);
+	  debug(3, "Storing UINT32 %u in slot %u\n", *(UINT32 *)(*p_argv),
+		slot);
 	  *(UINT32 *)(stack - slot) = *(UINT32 *)(*p_argv);
 	  break;
 
 	case FFI_TYPE_UINT64:
 	case FFI_TYPE_SINT64:
-	  slot += 2;
-	  if (slot & 1)
-	    slot++;
-
-	  *(UINT32 *)(stack - slot) = (*(UINT64 *)(*p_argv)) >> 32;
-	  *(UINT32 *)(stack - slot + 1) = (*(UINT64 *)(*p_argv)) & 0xffffffffUL;
+	  /* Align slot for 64-bit type.  */
+	  slot += (slot & 1) ? 1 : 2;
+	  *(UINT64 *)(stack - slot) = *(UINT64 *)(*p_argv);
 	  break;
 
 	case FFI_TYPE_FLOAT:
-	  /* First 4 args go in fr4L - fr7L */
-	  slot++;
+	  /* First 4 args go in fr4L - fr7L.  */
+	  debug(3, "Storing UINT32(float) in slot %u\n", slot);
+	  *(UINT32 *)(stack - slot) = *(UINT32 *)(*p_argv);
 	  switch (slot - FIRST_ARG_SLOT)
 	    {
-	    case 0: fldw(*p_argv, fr4); break;
-	    case 1: fldw(*p_argv, fr5); break;
-	    case 2: fldw(*p_argv, fr6); break;
-	    case 3: fldw(*p_argv, fr7); break;
-	    default:
-	      /* Other ones are just passed on the stack.  */
-	      debug(3, "Storing UINT32(float) in slot %u\n", slot);
-	      *(UINT32 *)(stack - slot) = *(UINT32 *)(*p_argv);
-	      break;
+	    /* First 4 args go in fr4L - fr7L.  */
+	    case 0: fldw(stack - slot, fr4); break;
+	    case 1: fldw(stack - slot, fr5); break;
+	    case 2: fldw(stack - slot, fr6); break;
+	    case 3: fldw(stack - slot, fr7); break;
 	    }
-	    break;
+	  break;
 
 	case FFI_TYPE_DOUBLE:
-	  slot += 2;
-	  if (slot & 1)
-	    slot++;
-	  switch (slot - FIRST_ARG_SLOT + 1)
+	  /* Align slot for 64-bit type.  */
+	  slot += (slot & 1) ? 1 : 2;
+	  debug(3, "Storing UINT64(double) at slot %u\n", slot);
+	  *(UINT64 *)(stack - slot) = *(UINT64 *)(*p_argv);
+	  switch (slot - FIRST_ARG_SLOT)
 	    {
-	      /* First 2 args go in fr5, fr7 */
-	      case 2: fldd(*p_argv, fr5); break;
-	      case 4: fldd(*p_argv, fr7); break;
-	      default:
-	        debug(3, "Storing UINT64(double) at slot %u\n", slot);
-	        *(UINT64 *)(stack - slot) = *(UINT64 *)(*p_argv);
-	        break;
+	      /* First 2 args go in fr5, fr7.  */
+	      case 1: fldd(stack - slot, fr5); break;
+	      case 3: fldd(stack - slot, fr7); break;
 	    }
 	  break;
 
@@ -206,40 +219,34 @@
 	     register. Structs smaller or equal 8 bytes are passed in two
 	     registers. Larger structures are passed by pointer.  */
 
-	  if((*p_arg)->size <= 4) 
+	  len = (*p_arg)->size;
+	  if (len <= 4)
 	    {
-	      slot++;
-	      dest_cpy = (char *)(stack - slot);
-	      dest_cpy += 4 - (*p_arg)->size;
-	      memcpy((char *)dest_cpy, (char *)*p_argv, (*p_arg)->size);
+	      dest_cpy = (char *)(stack - slot) + 4 - len;
+	      memcpy(dest_cpy, (char *)*p_argv, len);
 	    }
-	  else if ((*p_arg)->size <= 8) 
-	    {
-	      slot += 2;
-	      if (slot & 1)
-	        slot++;
-	      dest_cpy = (char *)(stack - slot);
-	      dest_cpy += 8 - (*p_arg)->size;
-	      memcpy((char *)dest_cpy, (char *)*p_argv, (*p_arg)->size);
-	    } 
-	  else 
+	  else if (len <= 8)
 	    {
-	      slot++;
-	      *(UINT32 *)(stack - slot) = (UINT32)(*p_argv);
+	      slot += (slot & 1) ? 1 : 2;
+	      dest_cpy = (char *)(stack - slot) + 8 - len;
+	      memcpy(dest_cpy, (char *)*p_argv, len);
 	    }
+	  else
+	    *(UINT32 *)(stack - slot) = (UINT32)(*p_argv);
 	  break;
 
 	default:
 	  FFI_ASSERT(0);
 	}
 
+      slot++;
       p_arg++;
       p_argv++;
     }
 
   /* Make sure we didn't mess up and scribble on the stack.  */
   {
-    int n;
+    unsigned int n;
 
     debug(5, "Stack setup:\n");
     for (n = 0; n < (bytes + 3) / 4; n++)
@@ -339,20 +346,11 @@
   return FFI_OK;
 }
 
-/*@-declundef@*/
-/*@-exportheader@*/
 extern void ffi_call_LINUX(void (*)(UINT32 *, extended_cif *, unsigned),
-			   /*@out@*/ extended_cif *,
-			   unsigned, unsigned,
-			   /*@out@*/ unsigned *,
-			   void (*fn)());
-/*@=declundef@*/
-/*@=exportheader@*/
-
-void ffi_call(/*@dependent@*/ ffi_cif *cif,
-	      void (*fn)(),
-	      /*@out@*/ void *rvalue,
-	      /*@dependent@*/ void **avalue)
+			  extended_cif *, unsigned, unsigned, unsigned *,
+			  void (*fn)());
+
+void ffi_call(ffi_cif *cif, void (*fn)(), void *rvalue, void **avalue)
 {
   extended_cif ecif;
 
@@ -362,13 +360,8 @@
   /* If the return value is a struct and we don't have a return
      value address then we need to make one.  */
 
-  if ((rvalue == NULL) &&
-      (cif->rtype->type == FFI_TYPE_STRUCT))
-    {
-      /*@-sysunrecog@*/
-      ecif.rvalue = alloca(cif->rtype->size);
-      /*@=sysunrecog@*/
-    }
+  if (rvalue == NULL && cif->rtype->type == FFI_TYPE_STRUCT)
+    ecif.rvalue = alloca(cif->rtype->size);
   else
     ecif.rvalue = rvalue;
 
@@ -376,11 +369,9 @@
   switch (cif->abi)
     {
     case FFI_LINUX:
-      /*@-usedef@*/
-      debug(2, "Calling ffi_call_LINUX: ecif=%p, bytes=%u, flags=%u, rvalue=%p, fn=%p\n", &ecif, cif->bytes, cif->flags, ecif.rvalue, (void *)fn);
+      debug(3, "Calling ffi_call_LINUX: ecif=%p, bytes=%u, flags=%u, rvalue=%p, fn=%p\n", &ecif, cif->bytes, cif->flags, ecif.rvalue, (void *)fn);
       ffi_call_LINUX(ffi_prep_args_LINUX, &ecif, cif->bytes,
 		     cif->flags, ecif.rvalue, fn);
-      /*@=usedef@*/
       break;
 
     default:
@@ -394,7 +385,7 @@
    the stack, and we need to fill them into a cif structure and invoke
    the user function. This really ought to be in asm to make sure
    the compiler doesn't do things we don't expect.  */
-UINT32 ffi_closure_inner_LINUX(ffi_closure *closure, UINT32 *stack)
+ffi_status ffi_closure_inner_LINUX(ffi_closure *closure, UINT32 *stack)
 {
   ffi_cif *cif;
   void **avalue;
@@ -402,7 +393,8 @@
   UINT32 ret[2]; /* function can return up to 64-bits in registers */
   ffi_type **p_arg;
   char *tmp;
-  int i, avn, slot = FIRST_ARG_SLOT - 1;
+  int i, avn;
+  unsigned int slot = FIRST_ARG_SLOT;
   register UINT32 r28 asm("r28");
 
   cif = closure->cif;
@@ -430,20 +422,22 @@
 	case FFI_TYPE_SINT32:
 	case FFI_TYPE_UINT32:
 	case FFI_TYPE_POINTER:
-	  slot++;
 	  avalue[i] = (char *)(stack - slot) + sizeof(UINT32) - (*p_arg)->size;
 	  break;
 
 	case FFI_TYPE_SINT64:
 	case FFI_TYPE_UINT64:
-	  slot += 2;
-	  if (slot & 1)
-	    slot++;
+	  slot += (slot & 1) ? 1 : 2;
 	  avalue[i] = (void *)(stack - slot);
 	  break;
 
 	case FFI_TYPE_FLOAT:
-	  slot++;
+	  /* The closure call is indirect.  In Linux, floating point
+	     arguments in indirect calls with a prototype are passed
+	     in the floating point registers instead of the general
+	     registers.  So, we need to replace what was previously
+	     stored in the current slot with the value in the
+	     corresponding floating point register.  */
 	  switch (slot - FIRST_ARG_SLOT)
 	    {
 	    case 0: fstw(fr4, (void *)(stack - slot)); break;
@@ -455,13 +449,12 @@
 	  break;
 
 	case FFI_TYPE_DOUBLE:
-	  slot += 2;
-	  if (slot & 1)
-	    slot++;
-	  switch (slot - FIRST_ARG_SLOT + 1)
+	  slot += (slot & 1) ? 1 : 2;
+	  /* See previous comment for FFI_TYPE_FLOAT.  */
+	  switch (slot - FIRST_ARG_SLOT)
 	    {
-	    case 2: fstd(fr5, (void *)(stack - slot)); break;
-	    case 4: fstd(fr7, (void *)(stack - slot)); break;
+	    case 1: fstd(fr5, (void *)(stack - slot)); break;
+	    case 3: fstd(fr7, (void *)(stack - slot)); break;
 	    }
 	  avalue[i] = (void *)(stack - slot);
 	  break;
@@ -470,35 +463,36 @@
 	  /* Structs smaller or equal than 4 bytes are passed in one
 	     register. Structs smaller or equal 8 bytes are passed in two
 	     registers. Larger structures are passed by pointer.  */
-	  if((*p_arg)->size <= 4) {
-	    slot++;
-	    avalue[i] = (void *)(stack - slot) + sizeof(UINT32) -
-	      (*p_arg)->size;
-	  } else if ((*p_arg)->size <= 8) {
-	    slot += 2;
-	    if (slot & 1)
-	      slot++;
-	    avalue[i] = (void *)(stack - slot) + sizeof(UINT64) -
-	      (*p_arg)->size;
-	  } else {
-	    slot++;
+	  if((*p_arg)->size <= 4)
+	    {
+	      avalue[i] = (void *)(stack - slot) + sizeof(UINT32) -
+		(*p_arg)->size;
+	    }
+	  else if ((*p_arg)->size <= 8)
+	    {
+	      slot += (slot & 1) ? 1 : 2;
+	      avalue[i] = (void *)(stack - slot) + sizeof(UINT64) -
+		(*p_arg)->size;
+	    }
+	  else
 	    avalue[i] = (void *) *(stack - slot);
-	  }
 	  break;
 
 	default:
 	  FFI_ASSERT(0);
 	}
 
+      slot++;
       p_arg++;
     }
 
   /* Invoke the closure.  */
   (closure->fun) (cif, rvalue, avalue, closure->user_data);
 
-  debug(3, "after calling function, ret[0] = %08x, ret[1] = %08x\n", ret[0], ret[1]);
+  debug(3, "after calling function, ret[0] = %08x, ret[1] = %08x\n", ret[0],
+	ret[1]);
 
-  /* Store the result */
+  /* Store the result using the lower 2 bytes of the flags.  */
   switch (cif->flags)
     {
     case FFI_TYPE_UINT8:
@@ -536,7 +530,9 @@
       /* Don't need a return value, done by caller.  */
       break;
 
+    case FFI_TYPE_SMALL_STRUCT2:
     case FFI_TYPE_SMALL_STRUCT3:
+    case FFI_TYPE_SMALL_STRUCT4:
       tmp = (void*)(stack -  FIRST_ARG_SLOT);
       tmp += 4 - cif->rtype->size;
       memcpy((void*)tmp, &ret[0], cif->rtype->size);
@@ -545,6 +541,7 @@
     case FFI_TYPE_SMALL_STRUCT5:
     case FFI_TYPE_SMALL_STRUCT6:
     case FFI_TYPE_SMALL_STRUCT7:
+    case FFI_TYPE_SMALL_STRUCT8:
       {
 	unsigned int ret2[2];
 	int off;
@@ -582,7 +579,7 @@
    cif specifies the argument and result types for fun.
    The cif must already be prep'ed.  */
 
-void ffi_closure_LINUX(void);
+extern void ffi_closure_LINUX(void);
 
 ffi_status
 ffi_prep_closure (ffi_closure* closure,
@@ -597,24 +594,34 @@
   /* Make a small trampoline that will branch to our
      handler function. Use PC-relative addressing.  */
 
-  tramp[0] = 0xeaa00000; /* b,l  .+8, %r21      ; %r21 <- pc+8 */
-  tramp[1] = 0xd6a01c1e; /* depi 0,31,2, %r21   ; mask priv bits */
-  tramp[2] = 0x4aa10028; /* ldw  20(%r21), %r1  ; load plabel */
-  tramp[3] = 0x36b53ff1; /* ldo  -8(%r21), %r21 ; get closure addr */
-  tramp[4] = 0x0c201096; /* ldw  0(%r1), %r22   ; address of handler */
-  tramp[5] = 0xeac0c000; /* bv	 %r0(%r22)      ; branch to handler */
-  tramp[6] = 0x0c281093; /* ldw  4(%r1), %r19   ; GP of handler */
+  tramp[0] = 0xeaa00000; /* b,l .+8,%r21        ; %r21 <- pc+8 */
+  tramp[1] = 0xd6a01c1e; /* depi 0,31,2,%r21    ; mask priv bits */
+  tramp[2] = 0x4aa10028; /* ldw 20(%r21),%r1    ; load plabel */
+  tramp[3] = 0x36b53ff1; /* ldo -8(%r21),%r21   ; get closure addr */
+  tramp[4] = 0x0c201096; /* ldw 0(%r1),%r22     ; address of handler */
+  tramp[5] = 0xeac0c000; /* bv%r0(%r22)         ; branch to handler */
+  tramp[6] = 0x0c281093; /* ldw 4(%r1),%r19     ; GP of handler */
   tramp[7] = ((UINT32)(ffi_closure_LINUX) & ~2);
 
   /* Flush d/icache -- have to flush up 2 two lines because of
      alignment.  */
-  asm volatile (
-		"fdc 0(%0)\n"
-		"fdc %1(%0)\n"
-		"fic 0(%%sr4, %0)\n"
-		"fic %1(%%sr4, %0)\n"
-		"sync\n"
-		: : "r"((unsigned long)tramp & ~31), "r"(32 /* stride */));
+  __asm__ volatile(
+		   "fdc 0(%0)\n\t"
+		   "fdc %1(%0)\n\t"
+		   "fic 0(%%sr4, %0)\n\t"
+		   "fic %1(%%sr4, %0)\n\t"
+		   "sync\n\t"
+		   "nop\n\t"
+		   "nop\n\t"
+		   "nop\n\t"
+		   "nop\n\t"
+		   "nop\n\t"
+		   "nop\n\t"
+		   "nop\n"
+		   :
+		   : "r"((unsigned long)tramp & ~31),
+		     "r"(32 /* stride */)
+		   : "memory");
 
   closure->cif  = cif;
   closure->user_data = user_data;


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]