-
Notifications
You must be signed in to change notification settings - Fork 0
/
libffi_mips_go_closure.patch
451 lines (421 loc) · 12.4 KB
/
libffi_mips_go_closure.patch
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
diff --git a/src/mips/ffi.c b/src/mips/ffi.c
index 5d0dd70..3ed9b48 100644
--- a/src/mips/ffi.c
+++ b/src/mips/ffi.c
@@ -581,14 +581,15 @@ ffi_status ffi_prep_cif_machdep(ffi_cif *cif)
/* Low level routine for calling O32 functions */
extern int ffi_call_O32(void (*)(char *, extended_cif *, int, int),
extended_cif *, unsigned,
- unsigned, unsigned *, void (*)(void));
+ unsigned, unsigned *, void (*)(void), void *closure);
/* Low level routine for calling N32 functions */
extern int ffi_call_N32(void (*)(char *, extended_cif *, int, int),
extended_cif *, unsigned,
- unsigned, void *, void (*)(void));
+ unsigned, void *, void (*)(void), void *closure);
-void ffi_call(ffi_cif *cif, void (*fn)(void), void *rvalue, void **avalue)
+void ffi_call_int(ffi_cif *cif, void (*fn)(void), void *rvalue,
+ void **avalue, void *closure)
{
extended_cif ecif;
@@ -610,7 +611,7 @@ void ffi_call(ffi_cif *cif, void (*fn)(void), void *rvalue, void **avalue)
case FFI_O32:
case FFI_O32_SOFT_FLOAT:
ffi_call_O32(ffi_prep_args, &ecif, cif->bytes,
- cif->flags, ecif.rvalue, fn);
+ cif->flags, ecif.rvalue, fn, closure);
break;
#endif
@@ -642,7 +643,7 @@ void ffi_call(ffi_cif *cif, void (*fn)(void), void *rvalue, void **avalue)
#endif
}
ffi_call_N32(ffi_prep_args, &ecif, cif->bytes,
- cif->flags, rvalue_copy, fn);
+ cif->flags, rvalue_copy, fn, closure);
if (copy_rvalue)
memcpy(ecif.rvalue, rvalue_copy + copy_offset, cif->rtype->size);
}
@@ -655,11 +656,27 @@ void ffi_call(ffi_cif *cif, void (*fn)(void), void *rvalue, void **avalue)
}
}
+void
+ffi_call(ffi_cif *cif, void (*fn)(void), void *rvalue, void **avalue)
+{
+ ffi_call_int (cif, fn, rvalue, avalue, NULL);
+}
+
+void
+ffi_call_go (ffi_cif *cif, void (*fn)(void), void *rvalue,
+ void **avalue, void *closure)
+{
+ ffi_call_int (cif, fn, rvalue, avalue, closure);
+}
+
+
#if FFI_CLOSURES
#if defined(FFI_MIPS_O32)
extern void ffi_closure_O32(void);
+extern void ffi_go_closure_O32(void);
#else
extern void ffi_closure_N32(void);
+extern void ffi_go_closure_N32(void);
#endif /* FFI_MIPS_O32 */
ffi_status
@@ -762,17 +779,17 @@ ffi_prep_closure_loc (ffi_closure *closure,
* Based on the similar routine for sparc.
*/
int
-ffi_closure_mips_inner_O32 (ffi_closure *closure,
+ffi_closure_mips_inner_O32 (ffi_cif *cif,
+ void (*fun)(ffi_cif*, void*, void**, void*),
+ void *user_data,
void *rvalue, ffi_arg *ar,
double *fpr)
{
- ffi_cif *cif;
void **avaluep;
ffi_arg *avalue;
ffi_type **arg_types;
int i, avn, argn, seen_int;
- cif = closure->cif;
avalue = alloca (cif->nargs * sizeof (ffi_arg));
avaluep = alloca (cif->nargs * sizeof (ffi_arg));
@@ -840,7 +857,7 @@ ffi_closure_mips_inner_O32 (ffi_closure *closure,
}
/* Invoke the closure. */
- (closure->fun) (cif, rvalue, avaluep, closure->user_data);
+ fun(cif, rvalue, avaluep, user_data);
if (cif->abi == FFI_O32_SOFT_FLOAT)
{
@@ -916,11 +933,12 @@ copy_struct_N32(char *target, unsigned offset, ffi_abi abi, ffi_type *type,
*
*/
int
-ffi_closure_mips_inner_N32 (ffi_closure *closure,
+ffi_closure_mips_inner_N32 (ffi_cif *cif,
+ void (*fun)(ffi_cif*, void*, void**, void*),
+ void *user_data,
void *rvalue, ffi_arg *ar,
ffi_arg *fpr)
{
- ffi_cif *cif;
void **avaluep;
ffi_arg *avalue;
ffi_type **arg_types;
@@ -928,7 +946,6 @@ ffi_closure_mips_inner_N32 (ffi_closure *closure,
int soft_float;
ffi_arg *argp;
- cif = closure->cif;
soft_float = cif->abi == FFI_N64_SOFT_FLOAT
|| cif->abi == FFI_N32_SOFT_FLOAT;
avalue = alloca (cif->nargs * sizeof (ffi_arg));
@@ -1040,11 +1057,49 @@ ffi_closure_mips_inner_N32 (ffi_closure *closure,
}
/* Invoke the closure. */
- (closure->fun) (cif, rvalue, avaluep, closure->user_data);
+ fun (cif, rvalue, avaluep, user_data);
return cif->flags >> (FFI_FLAG_BITS * 8);
}
#endif /* FFI_MIPS_N32 */
+#if defined(FFI_MIPS_O32)
+extern void ffi_closure_O32(void);
+extern void ffi_go_closure_O32(void);
+#else
+extern void ffi_closure_N32(void);
+extern void ffi_go_closure_N32(void);
+#endif /* FFI_MIPS_O32 */
+
+ffi_status
+ffi_prep_go_closure (ffi_go_closure* closure, ffi_cif* cif,
+ void (*fun)(ffi_cif*,void*,void**,void*))
+{
+ void * fn;
+
+#if defined(FFI_MIPS_O32)
+ if (cif->abi != FFI_O32 && cif->abi != FFI_O32_SOFT_FLOAT)
+ return FFI_BAD_ABI;
+ fn = ffi_go_closure_O32;
+#else
+#if _MIPS_SIM ==_ABIN32
+ if (cif->abi != FFI_N32
+ && cif->abi != FFI_N32_SOFT_FLOAT)
+ return FFI_BAD_ABI;
+#else
+ if (cif->abi != FFI_N64
+ && cif->abi != FFI_N64_SOFT_FLOAT)
+ return FFI_BAD_ABI;
+#endif
+ fn = ffi_go_closure_N32;
+#endif /* FFI_MIPS_O32 */
+
+ closure->tramp = (void *)fn;
+ closure->cif = cif;
+ closure->fun = fun;
+
+ return FFI_OK;
+}
+
#endif /* FFI_CLOSURES */
diff --git a/src/mips/ffitarget.h b/src/mips/ffitarget.h
index 717d659..cc89a84 100644
--- a/src/mips/ffitarget.h
+++ b/src/mips/ffitarget.h
@@ -231,12 +231,14 @@ typedef enum ffi_abi {
#if defined(FFI_MIPS_O32)
#define FFI_CLOSURES 1
+#define FFI_GO_CLOSURES 1
#define FFI_TRAMPOLINE_SIZE 20
#else
/* N32/N64. */
# define FFI_CLOSURES 1
+#define FFI_GO_CLOSURES 1
#if _MIPS_SIM==_ABI64
-#define FFI_TRAMPOLINE_SIZE 52
+#define FFI_TRAMPOLINE_SIZE 56
#else
#define FFI_TRAMPOLINE_SIZE 20
#endif
diff --git a/src/mips/n32.S b/src/mips/n32.S
index c6985d3..750555e 100644
--- a/src/mips/n32.S
+++ b/src/mips/n32.S
@@ -37,8 +37,12 @@
#define flags a3
#define raddr a4
#define fn a5
+#define closure a6
-#define SIZEOF_FRAME ( 8 * FFI_SIZEOF_ARG )
+/* Note: to keep stack 16 byte aligned we need even number slots
+ used 9 slots here
+*/
+#define SIZEOF_FRAME ( 10 * FFI_SIZEOF_ARG )
#ifdef __GNUC__
.abicalls
@@ -67,6 +71,7 @@ ffi_call_N32:
REG_S flags, 3*FFI_SIZEOF_ARG($fp) # flags
REG_S raddr, 4*FFI_SIZEOF_ARG($fp) # raddr
REG_S fn, 5*FFI_SIZEOF_ARG($fp) # fn
+ REG_S closure, 6*FFI_SIZEOF_ARG($fp) # closure
# Allocate at least 4 words in the argstack
move v0, bytes
@@ -198,6 +203,9 @@ callit:
# Load the function pointer
REG_L t9, 5*FFI_SIZEOF_ARG($fp)
+ # install the static chain(t7=$15)
+ REG_L t7, 6*FFI_SIZEOF_ARG($fp)
+
# If the return value pointer is NULL, assume no return value.
REG_L t5, 4*FFI_SIZEOF_ARG($fp)
beqz t5, noretval
@@ -406,6 +414,38 @@ epilogue:
#define GP_OFF2 (0 * FFI_SIZEOF_ARG)
.align 2
+ .globl ffi_go_closure_N32
+ .ent ffi_go_closure_N32
+ffi_go_closure_N32:
+ .frame $sp, SIZEOF_FRAME2, ra
+ .mask 0x90000000,-(SIZEOF_FRAME2 - RA_OFF2)
+ .fmask 0x00000000,0
+ SUBU $sp, SIZEOF_FRAME2
+
+ .cpsetup t9, GP_OFF2, ffi_go_closure_N32
+ REG_S ra, RA_OFF2($sp) # Save return address
+
+ REG_S a0, A0_OFF2($sp)
+ REG_S a1, A1_OFF2($sp)
+ REG_S a2, A2_OFF2($sp)
+ REG_S a3, A3_OFF2($sp)
+ REG_S a4, A4_OFF2($sp)
+ REG_S a5, A5_OFF2($sp)
+
+ # Call ffi_closure_mips_inner_N32 to do the real work.
+ LA t9, ffi_closure_mips_inner_N32
+ REG_L a0, 8($15) # cif
+ REG_L a1, 16($15) # fun
+ move a2, t7 # userdata=closure
+ ADDU a3, $sp, V0_OFF2 # rvalue
+ ADDU a4, $sp, A0_OFF2 # ar
+ ADDU a5, $sp, F12_OFF2 # fpr
+
+ b $do_closure
+
+ .end ffi_go_closure_N32
+
+ .align 2
.globl ffi_closure_N32
.ent ffi_closure_N32
ffi_closure_N32:
@@ -418,14 +458,25 @@ ffi_closure_N32:
.cpsetup t9, GP_OFF2, ffi_closure_N32
REG_S ra, RA_OFF2($sp) # Save return address
.LCFI6:
- # Store all possible argument registers. If there are more than
- # fit in registers, then they were stored on the stack.
REG_S a0, A0_OFF2($sp)
REG_S a1, A1_OFF2($sp)
REG_S a2, A2_OFF2($sp)
REG_S a3, A3_OFF2($sp)
REG_S a4, A4_OFF2($sp)
REG_S a5, A5_OFF2($sp)
+
+ # Call ffi_closure_mips_inner_N32 to do the real work.
+ LA t9, ffi_closure_mips_inner_N32
+ REG_L a0, 56($12) # cif
+ REG_L a1, 64($12) # fun
+ REG_L a2, 72($12) # user_data
+ ADDU a3, $sp, V0_OFF2
+ ADDU a4, $sp, A0_OFF2
+ ADDU a5, $sp, F12_OFF2
+
+$do_closure:
+ # Store all possible argument registers. If there are more than
+ # fit in registers, then they were stored on the stack.
REG_S a6, A6_OFF2($sp)
REG_S a7, A7_OFF2($sp)
@@ -439,12 +490,6 @@ ffi_closure_N32:
s.d $f18, F18_OFF2($sp)
s.d $f19, F19_OFF2($sp)
- # Call ffi_closure_mips_inner_N32 to do the real work.
- LA t9, ffi_closure_mips_inner_N32
- move a0, $12 # Pointer to the ffi_closure
- ADDU a1, $sp, V0_OFF2
- ADDU a2, $sp, A0_OFF2
- ADDU a3, $sp, F12_OFF2
jalr t9
# Return flags are in v0
diff --git a/src/mips/o32.S b/src/mips/o32.S
index eb27981..8ec6649 100644
--- a/src/mips/o32.S
+++ b/src/mips/o32.S
@@ -132,6 +132,9 @@ pass_f_d:
l.d $f14, 2*FFI_SIZEOF_ARG($sp) # passing double and float
call_it:
+ # Load the static chain pointer
+ REG_L t7, SIZEOF_FRAME + 6*FFI_SIZEOF_ARG($fp)
+
# Load the function pointer
REG_L t9, SIZEOF_FRAME + 5*FFI_SIZEOF_ARG($fp)
@@ -204,13 +207,15 @@ $LFE0:
-8 - f14 (le low, be high)
-9 - f12 (le high, be low)
-10 - f12 (le low, be high)
- -11 - Called function a3 save
- -12 - Called function a2 save
- -13 - Called function a1 save
- -14 - Called function a0 save, our sp and fp point here
+ -11 - Called function a5 save
+ -12 - Called function a4 save
+ -13 - Called function a3 save
+ -14 - Called function a2 save
+ -15 - Called function a1 save
+ -16 - Called function a0 save, our sp and fp point here
*/
-#define SIZEOF_FRAME2 (14 * FFI_SIZEOF_ARG)
+#define SIZEOF_FRAME2 (16 * FFI_SIZEOF_ARG)
#define A3_OFF2 (SIZEOF_FRAME2 + 3 * FFI_SIZEOF_ARG)
#define A2_OFF2 (SIZEOF_FRAME2 + 2 * FFI_SIZEOF_ARG)
#define A1_OFF2 (SIZEOF_FRAME2 + 1 * FFI_SIZEOF_ARG)
@@ -225,8 +230,61 @@ $LFE0:
#define FA_1_0_OFF2 (SIZEOF_FRAME2 - 8 * FFI_SIZEOF_ARG)
#define FA_0_1_OFF2 (SIZEOF_FRAME2 - 9 * FFI_SIZEOF_ARG)
#define FA_0_0_OFF2 (SIZEOF_FRAME2 - 10 * FFI_SIZEOF_ARG)
+#define CALLED_A5_OFF2 (SIZEOF_FRAME2 - 11 * FFI_SIZEOF_ARG)
+#define CALLED_A4_OFF2 (SIZEOF_FRAME2 - 12 * FFI_SIZEOF_ARG)
.text
+
+ .align 2
+ .globl ffi_go_closure_O32
+ .ent ffi_go_closure_O32
+ffi_go_closure_O32:
+ # Prologue
+ .frame $fp, SIZEOF_FRAME2, ra
+ .set noreorder
+ .cpload t9
+ .set reorder
+ SUBU $sp, SIZEOF_FRAME2
+ .cprestore GP_OFF2
+
+ REG_S $16, S0_OFF2($sp) # Save s0
+ REG_S $fp, FP_OFF2($sp) # Save frame pointer
+ REG_S ra, RA_OFF2($sp) # Save return address
+
+ move $fp, $sp
+
+ REG_S a0, A0_OFF2($fp)
+ REG_S a1, A1_OFF2($fp)
+ REG_S a2, A2_OFF2($fp)
+ REG_S a3, A3_OFF2($fp)
+
+ # Load ABI enum to s0
+ REG_L $16, 4($15) # cif
+ REG_L $16, 0($16) # abi is first member.
+
+ li $13, 1 # FFI_O32
+ bne $16, $13, 1f # Skip fp save if FFI_O32_SOFT_FLOAT
+
+ # Store all possible float/double registers.
+ s.d $f12, FA_0_0_OFF2($fp)
+ s.d $f14, FA_1_0_OFF2($fp)
+1:
+ # prepare arguments for ffi_closure_mips_inner_O32
+ REG_L a0, 4($15) # cif
+ REG_L a1, 8($15) # fun
+ move a2, $15 # user_data = go closure
+ addu a3, $fp, V0_OFF2 # rvalue
+
+ addu t9, $fp, A0_OFF2 # ar
+ REG_S t9, CALLED_A4_OFF2($fp)
+
+ addu t9, $fp, FA_0_0_OFF2 #fpr
+ REG_S t9, CALLED_A5_OFF2($fp)
+
+ b $do_closure
+
+ .end ffi_go_closure_O32
+
.align 2
.globl ffi_closure_O32
.ent ffi_closure_O32
@@ -265,12 +323,21 @@ $LCFI7:
s.d $f12, FA_0_0_OFF2($fp)
s.d $f14, FA_1_0_OFF2($fp)
1:
- # Call ffi_closure_mips_inner_O32 to do the work.
+ # prepare arguments for ffi_closure_mips_inner_O32
+ REG_L a0, 20($12) # cif pointer follows tramp.
+ REG_L a1, 24($12) # fun
+ REG_L a2, 28($12) # user_data
+ addu a3, $fp, V0_OFF2 # rvalue
+
+ addu t9, $fp, A0_OFF2 # ar
+ REG_S t9, CALLED_A4_OFF2($fp)
+
+ addu t9, $fp, FA_0_0_OFF2 #fpr
+ REG_S t9, CALLED_A5_OFF2($fp)
+
+$do_closure:
la t9, ffi_closure_mips_inner_O32
- move a0, $12 # Pointer to the ffi_closure
- addu a1, $fp, V0_OFF2
- addu a2, $fp, A0_OFF2
- addu a3, $fp, FA_0_0_OFF2
+ # Call ffi_closure_mips_inner_O32 to do the work.
jalr t9
# Load the return value into the appropriate register.
@@ -358,7 +425,7 @@ $LASFDE1:
.byte 0x4 # DW_CFA_advance_loc4
.4byte $LCFI4-$LFB1
.byte 0xe # DW_CFA_def_cfa_offset
- .uleb128 0x38
+ .uleb128 SIZEOF_FRAME2
.byte 0x4 # DW_CFA_advance_loc4
.4byte $LCFI6-$LCFI4
.byte 0x11 # DW_CFA_offset_extended_sf
@@ -374,7 +441,7 @@ $LASFDE1:
.4byte $LCFI7-$LCFI6
.byte 0xc # DW_CFA_def_cfa
.uleb128 0x1e
- .uleb128 0x38
+ .uleb128 SIZEOF_FRAME2
.align 2
$LEFDE1: