https://github.com/libffi/libffi/commit/222abd0c65babe2174b21753217145f5031a8b91 From 222abd0c65babe2174b21753217145f5031a8b91 Mon Sep 17 00:00:00 2001 From: Anthony Green Date: Thu, 2 Feb 2023 07:04:55 -0500 Subject: [PATCH] From Dave Anglin: 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. --- a/src/pa/ffi.c +++ b/src/pa/ffi.c @@ -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: --- a/src/pa/ffitarget.h +++ b/src/pa/ffitarget.h @@ -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 --- a/src/pa/linux.S +++ b/src/pa/linux.S @@ -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 */