From Dave Anglin:
authorAnthony Green <green@moxielogic.com>
Thu, 2 Feb 2023 12:04:55 +0000 (07:04 -0500)
committerAnthony Green <green@moxielogic.com>
Thu, 2 Feb 2023 12:04:55 +0000 (07:04 -0500)
This patch is derived from the work done in implementing libffi for 64-bit hppa64-hpux target. Currently, the 32-bit hppa targets do a linear search for the return type of an ffi_call. This is slow and inefficient. A jump table can used to jump directly to the code used to process the return value. In most common cases, the return value can be processed in the jump table itself.

The patch also fixes return handling for FFI_TYPE_UINT8, FFI_TYPE_SINT8, FFI_TYPE_UINT16 and FFI_TYPE_SINT16.

src/pa/ffi.c
src/pa/ffitarget.h
src/pa/hpux32.S
src/pa/linux.S

index b99a668e201856b82527f9dc9d99c226f3593846..f6012397e767354c7ffb013a3179fd661cf3f95a 100644 (file)
@@ -56,27 +56,12 @@ static inline int ffi_struct_type(ffi_type *t)
   size_t sz = t->size;
 
   /* Small structure results are passed in registers,
-     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_SMALL_STRUCT2;
-  else if (sz == 3)
-    return FFI_TYPE_SMALL_STRUCT3;
-  else if (sz == 4)
-    return FFI_TYPE_SMALL_STRUCT4;
-  else if (sz == 5)
-    return FFI_TYPE_SMALL_STRUCT5;
-  else if (sz == 6)
-    return FFI_TYPE_SMALL_STRUCT6;
-  else if (sz == 7)
-    return FFI_TYPE_SMALL_STRUCT7;
-  else if (sz <= 8)
-    return FFI_TYPE_SMALL_STRUCT8;
+     larger ones are passed by pointer.  Note that small
+     structures differ from the corresponding integer
+     types in that they have different alignment requirements.  */
+
+  if (sz <= 8)
+    return -sz;
   else
     return FFI_TYPE_STRUCT; /* else, we pass it by pointer.  */
 }
@@ -556,16 +541,16 @@ ffi_status ffi_closure_inner_pa32(ffi_closure *closure, UINT32 *stack)
   switch (cif->flags)
     {
     case FFI_TYPE_UINT8:
-      *(stack - FIRST_ARG_SLOT) = (UINT8)(u.ret[0] >> 24);
+      *(stack - FIRST_ARG_SLOT) = (UINT8)u.ret[0];
       break;
     case FFI_TYPE_SINT8:
-      *(stack - FIRST_ARG_SLOT) = (SINT8)(u.ret[0] >> 24);
+      *(stack - FIRST_ARG_SLOT) = (SINT8)u.ret[0];
       break;
     case FFI_TYPE_UINT16:
-      *(stack - FIRST_ARG_SLOT) = (UINT16)(u.ret[0] >> 16);
+      *(stack - FIRST_ARG_SLOT) = (UINT16)u.ret[0];
       break;
     case FFI_TYPE_SINT16:
-      *(stack - FIRST_ARG_SLOT) = (SINT16)(u.ret[0] >> 16);
+      *(stack - FIRST_ARG_SLOT) = (SINT16)u.ret[0];
       break;
     case FFI_TYPE_INT:
     case FFI_TYPE_SINT32:
@@ -590,6 +575,7 @@ ffi_status ffi_closure_inner_pa32(ffi_closure *closure, UINT32 *stack)
       /* Don't need a return value, done by caller.  */
       break;
 
+    case FFI_TYPE_SMALL_STRUCT1:
     case FFI_TYPE_SMALL_STRUCT2:
     case FFI_TYPE_SMALL_STRUCT3:
     case FFI_TYPE_SMALL_STRUCT4:
index 6a2c5dc53b9237c230e7b2cf07d97a1919eed86d..dae854a695bb30e6e87ebaee47e462f29c128e63 100644 (file)
@@ -73,11 +73,22 @@ typedef enum ffi_abi {
 #define FFI_TRAMPOLINE_SIZE 12
 #endif
 
-#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
+#define FFI_TYPE_SMALL_STRUCT1 -1
+#define FFI_TYPE_SMALL_STRUCT2 -2
+#define FFI_TYPE_SMALL_STRUCT3 -3
+#define FFI_TYPE_SMALL_STRUCT4 -4
+#define FFI_TYPE_SMALL_STRUCT5 -5
+#define FFI_TYPE_SMALL_STRUCT6 -6
+#define FFI_TYPE_SMALL_STRUCT7 -7
+#define FFI_TYPE_SMALL_STRUCT8 -8
+
+/* linux.S and hpux32.S expect FFI_TYPE_COMPLEX is the last generic type.  */
+#define FFI_PA_TYPE_LAST FFI_TYPE_COMPLEX
+
+/* If new generic types are added, the jump tables in linux.S and hpux32.S
+   likely need updating.  */
+#if FFI_TYPE_LAST != FFI_PA_TYPE_LAST
+# error "You likely have broken jump tables"
+#endif
+
 #endif
index d0e5f695297bb837f3a360e5af27b0c4010da508..1629c0356b439d9ddc6ad8a9d874d9aa4b17aa1d 100644 (file)
@@ -109,52 +109,104 @@ L$CFI13
 
        /* Prepare to store the result; we need to recover flags and rvalue.  */
        ldw     -48(%r3), %r21          ; r21 <- flags
-       ldw     -52(%r3), %r20          ; r20 <- rvalue
 
-       /* Store the result according to the return type.  The most
-          likely types should come first.  */
+       /* Adjust flags range from [-8, 15] to  [0, 23].  */
+       addi    8, %r21, %r21
 
-L$checkint
-       comib,<>,n FFI_TYPE_INT, %r21, L$checkint8
-       b       L$done
-       stw     %ret0, 0(%r20)
+       blr     %r21, %r0
+       ldw     -52(%r3), %r20          ; r20 <- rvalue
 
-L$checkint8
-       comib,<>,n FFI_TYPE_UINT8, %r21, L$checkint16
+       /* Giant jump table */
+       /* 8-byte small struct */
+       b,n     L$smst8
+       nop
+       /* 7-byte small struct */
+       b,n     L$smst7
+       nop
+       /* 6-byte small struct */
+       b,n     L$smst6
+       nop
+       /* 5-byte small struct */
+       b,n     L$smst5
+       nop
+       /* 4-byte small struct */
+       b,n     L$smst4
+       nop
+       /* 3-byte small struct */
+       b,n     L$smst3
+       nop
+       /* 2-byte small struct */
+       b,n     L$smst2
+       nop
+       /* 1-byte small struct */
        b       L$done
        stb     %ret0, 0(%r20)
-
-L$checkint16
-       comib,<>,n FFI_TYPE_UINT16, %r21, L$checkdbl
+       /* void */
+       b,n     L$done
+       nop
+       /* int */
        b       L$done
-       sth     %ret0, 0(%r20)
-
-L$checkdbl
-       comib,<>,n FFI_TYPE_DOUBLE, %r21, L$checkfloat
+       stw     %ret0, 0(%r20)
+       /* float */
+       b       L$done
+       fstw    %fr4L,0(%r20)
+       /* double */
        b       L$done
        fstd    %fr4,0(%r20)
-
-L$checkfloat
-       comib,<>,n FFI_TYPE_FLOAT, %r21, L$checkll
+       /* long double */
+       b,n     L$done
+       nop
+       /* unsigned int8 */
        b       L$done
-       fstw    %fr4L,0(%r20)
+       stw     %ret0, 0(%r20)
+       /* signed int8 */
+       b       L$done
+       stw     %ret0, 0(%r20)
+       /* unsigned int16 */
+       b       L$done
+       stw     %ret0, 0(%r20)
+       /* signed int16 */
+       b       L$done
+       stw     %ret0, 0(%r20)
+       /* unsigned int32 */
+       b       L$done
+       stw     %ret0, 0(%r20)
+       /* signed int32 */
+       b       L$done
+       stw     %ret0, 0(%r20)
+       /* unsigned int64 */
+       b,n     L$uint64
+       nop
+       /* signed int64 */
+       b,n     L$sint64
+       nop
+       /* large struct */
+       b,n     L$done
+       nop
+       /* pointer */
+       b       L$done
+       stw     %ret0, 0(%r20)
+       /* complex */
+       b,n     L$done
+       nop
+
+       /* Store the result according to the return type.  The most
+          likely types should come first.  */
 
-L$checkll
-       comib,<>,n FFI_TYPE_UINT64, %r21, L$checksmst2
+L$uint64
+L$sint64
        stw     %ret0, 0(%r20)
        b       L$done
        stw     %ret1, 4(%r20)
 
-L$checksmst2
-       comib,<>,n FFI_TYPE_SMALL_STRUCT2, %r21, L$checksmst3
+L$smst2
        /* 2-byte structs are returned in ret0 as ????xxyy.  */
        extru   %ret0, 23, 8, %r22
        stbs,ma %r22, 1(%r20)
        b       L$done
        stb     %ret0, 0(%r20)
 
-L$checksmst3
-       comib,<>,n FFI_TYPE_SMALL_STRUCT3, %r21, L$checksmst4
+L$smst3
        /* 3-byte structs are returned in ret0 as ??xxyyzz.  */
        extru   %ret0, 15, 8, %r22
        stbs,ma %r22, 1(%r20)
@@ -163,8 +215,7 @@ L$checksmst3
        b       L$done
        stb     %ret0, 0(%r20)
 
-L$checksmst4
-       comib,<>,n FFI_TYPE_SMALL_STRUCT4, %r21, L$checksmst5
+L$smst4
        /* 4-byte structs are returned in ret0 as wwxxyyzz.  */
        extru   %ret0, 7, 8, %r22
        stbs,ma %r22, 1(%r20)
@@ -175,8 +226,7 @@ L$checksmst4
        b       L$done
        stb     %ret0, 0(%r20)
 
-L$checksmst5
-       comib,<>,n FFI_TYPE_SMALL_STRUCT5, %r21, L$checksmst6
+L$smst5
        /* 5 byte values are returned right justified:
              ret0     ret1
           5: ??????aa bbccddee */
@@ -190,8 +240,7 @@ L$checksmst5
        b       L$done
        stb     %ret1, 0(%r20)
 
-L$checksmst6
-       comib,<>,n FFI_TYPE_SMALL_STRUCT6, %r21, L$checksmst7
+L$smst6
        /* 6 byte values are returned right justified:
              ret0     ret1
           6: ????aabb ccddeeff */
@@ -207,8 +256,7 @@ L$checksmst6
        b       L$done
        stb     %ret1, 0(%r20)
 
-L$checksmst7
-       comib,<>,n FFI_TYPE_SMALL_STRUCT7, %r21, L$checksmst8
+L$smst7
        /* 7 byte values are returned right justified:
              ret0     ret1
           7: ??aabbcc ddeeffgg */
@@ -226,8 +274,7 @@ L$checksmst7
        b       L$done
        stb     %ret1, 0(%r20)
 
-L$checksmst8
-       comib,<>,n FFI_TYPE_SMALL_STRUCT8, %r21, L$done
+L$smst8
        /* 8 byte values are returned right justified:
              ret0     ret1
           8: aabbccdd eeffgghh */
index 33ef0b137a4118bcfce9bc1e4b1dc4335cb4f027..2d3b03604eb186f5f62166942f9ad6cf507bd2ac 100644 (file)
@@ -103,51 +103,103 @@ ffi_call_pa32:
 
        /* Prepare to store the result; we need to recover flags and rvalue.  */
        ldw -48(%r3), %r21                      /* r21 <- flags */
-       ldw -52(%r3), %r20                      /* r20 <- rvalue */
 
-       /* Store the result according to the return type.  */
+       /* Adjust flags range from [-8, 15] to  [0, 23].  */
+       addi 8, %r21, %r21
 
-.Lcheckint:
-       comib,<>,n FFI_TYPE_INT, %r21, .Lcheckint8
-       b       .Ldone
-       stw     %ret0, 0(%r20)
+       blr %r21, %r0
+       ldw -52(%r3), %r20                      /* r20 <- rvalue */
 
-.Lcheckint8:
-       comib,<>,n FFI_TYPE_UINT8, %r21, .Lcheckint16
+       /* Giant jump table */
+       /* 8-byte small struct */
+       b,n     .Lsmst8
+       nop
+       /* 7-byte small struct */
+       b,n     .Lsmst7
+       nop
+       /* 6-byte small struct */
+       b,n     .Lsmst6
+       nop
+       /* 5-byte small struct */
+       b,n     .Lsmst5
+       nop
+       /* 4-byte small struct */
+       b,n     .Lsmst4
+       nop
+       /* 3-byte small struct */
+       b,n     .Lsmst3
+       nop
+       /* 2-byte small struct */
+       b,n     .Lsmst2
+       nop
+       /* 1-byte small struct */
        b       .Ldone
        stb     %ret0, 0(%r20)
-
-.Lcheckint16:
-       comib,<>,n FFI_TYPE_UINT16, %r21, .Lcheckdbl
+       /* void */
+       b,n     .Ldone
+       nop
+       /* int */
        b       .Ldone
-       sth     %ret0, 0(%r20)
-
-.Lcheckdbl:
-       comib,<>,n FFI_TYPE_DOUBLE, %r21, .Lcheckfloat
+       stw     %ret0, 0(%r20)
+       /* float */
+       b       .Ldone
+       fstw    %fr4L,0(%r20)
+       /* double */
        b       .Ldone
        fstd    %fr4,0(%r20)
-
-.Lcheckfloat:
-       comib,<>,n FFI_TYPE_FLOAT, %r21, .Lcheckll
+       /* long double */
        b       .Ldone
-       fstw    %fr4L,0(%r20)
+       fstd    %fr4,0(%r20)
+       /* unsigned int8 */
+       b       .Ldone
+       stw     %ret0, 0(%r20)
+       /* sint8 */
+       b       .Ldone
+       stw     %ret0, 0(%r20)
+       /* unsigned int16 */
+       b       .Ldone
+       stw     %ret0, 0(%r20)
+       /* sint16 */
+       b       .Ldone
+       stw     %ret0, 0(%r20)
+       /* unsigned int32 */
+       b       .Ldone
+       stw     %ret0, 0(%r20)
+       /* sint32 */
+       b       .Ldone
+       stw     %ret0, 0(%r20)
+       /* unsigned int64 */
+       b,n     .Luint64
+       nop
+       /* signed int64 */
+       b,n     .Lsint64
+       nop
+       /* large struct */
+       b,n     .Ldone
+       nop
+       /* pointer */
+       b       .Ldone
+       stw     %ret0, 0(%r20)
+       /* complex */
+       b,n     .Ldone
+       nop
+
+       /* Store the result according to the return type.  */
 
-.Lcheckll:
-       comib,<>,n FFI_TYPE_UINT64, %r21, .Lchecksmst2
+.Luint64:
+.Lsint64:
        stw     %ret0, 0(%r20)
        b       .Ldone
        stw     %ret1, 4(%r20)
 
-.Lchecksmst2:
-       comib,<>,n FFI_TYPE_SMALL_STRUCT2, %r21, .Lchecksmst3
+.Lsmst2:
        /* 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
+.Lsmst3:
        /* 3-byte structs are returned in ret0 as ??xxyyzz.  */
        extru   %ret0, 15, 8, %r22
        stbs,ma %r22, 1(%r20)
@@ -156,8 +208,7 @@ ffi_call_pa32:
        b       .Ldone
        stb     %ret0, 0(%r20)
 
-.Lchecksmst4:
-       comib,<>,n FFI_TYPE_SMALL_STRUCT4, %r21, .Lchecksmst5
+.Lsmst4:
        /* 4-byte structs are returned in ret0 as wwxxyyzz.  */
        extru   %ret0, 7, 8, %r22
        stbs,ma %r22, 1(%r20)
@@ -168,8 +219,7 @@ ffi_call_pa32:
        b       .Ldone
        stb     %ret0, 0(%r20)
 
-.Lchecksmst5:
-       comib,<>,n FFI_TYPE_SMALL_STRUCT5, %r21, .Lchecksmst6
+.Lsmst5:
        /* 5 byte values are returned right justified:
              ret0     ret1
           5: ??????aa bbccddee */
@@ -183,8 +233,7 @@ ffi_call_pa32:
        b       .Ldone
        stb     %ret1, 0(%r20)
 
-.Lchecksmst6:
-       comib,<>,n FFI_TYPE_SMALL_STRUCT6, %r21, .Lchecksmst7
+.Lsmst6:
        /* 6 byte values are returned right justified:
              ret0     ret1
           6: ????aabb ccddeeff */
@@ -200,8 +249,7 @@ ffi_call_pa32:
        b       .Ldone
        stb     %ret1, 0(%r20)
 
-.Lchecksmst7:
-       comib,<>,n FFI_TYPE_SMALL_STRUCT7, %r21, .Lchecksmst8
+.Lsmst7:
        /* 7 byte values are returned right justified:
              ret0     ret1
           7: ??aabbcc ddeeffgg */
@@ -219,8 +267,7 @@ ffi_call_pa32:
        b       .Ldone
        stb     %ret1, 0(%r20)
 
-.Lchecksmst8:
-       comib,<>,n FFI_TYPE_SMALL_STRUCT8, %r21, .Ldone
+.Lsmst8:
        /* 8 byte values are returned right justified:
              ret0     ret1
           8: aabbccdd eeffgghh */