Commit 222abd0c65babe2174b21753217145f5031a8b91

Anthony Green 2023-02-02T07:04:55

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.

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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
diff --git a/src/pa/ffi.c b/src/pa/ffi.c
index b99a668..f601239 100644
--- 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:
diff --git a/src/pa/ffitarget.h b/src/pa/ffitarget.h
index 6a2c5dc..dae854a 100644
--- 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
diff --git a/src/pa/hpux32.S b/src/pa/hpux32.S
index d0e5f69..1629c03 100644
--- a/src/pa/hpux32.S
+++ b/src/pa/hpux32.S
@@ -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 */
diff --git a/src/pa/linux.S b/src/pa/linux.S
index 33ef0b1..2d3b036 100644
--- 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 */