summaryrefslogtreecommitdiff
path: root/dev-libs/libffi/files/libffi-3.4.4-hppa-jump-table.patch
blob: 822a7eb893ec45c832e20ea77eff682fc9659208 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
https://github.com/libffi/libffi/commit/222abd0c65babe2174b21753217145f5031a8b91

From 222abd0c65babe2174b21753217145f5031a8b91 Mon Sep 17 00:00:00 2001
From: Anthony Green <green@moxielogic.com>
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 */