-
Notifications
You must be signed in to change notification settings - Fork 5
/
libffi-3.4.2-e2k.patch
714 lines (706 loc) · 19.9 KB
/
libffi-3.4.2-e2k.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
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
From 4eea0aa2937de72d6909d1804579e7d422359881 Mon Sep 17 00:00:00 2001
From: Ilya Kurdyukov <[email protected]>
Date: Wed, 24 Aug 2022 14:05:43 +0700
Subject: [PATCH] libffi-3.4.2 e2k support
LCC >= 1.25.18 or LCC >= 1.26.04 is required
for __clear_cache() to work properly
---
Makefile.am | 3 +
configure.host | 5 +
src/e2k/ffi.c | 540 +++++++++++++++++++++++++++
src/e2k/ffitarget.h | 63 ++++
testsuite/lib/libffi.exp | 7 +
testsuite/libffi.complex/complex.exp | 7 +
testsuite/libffi.go/static-chain.h | 2 +
7 files changed, 627 insertions(+)
create mode 100644 src/e2k/ffi.c
create mode 100644 src/e2k/ffitarget.h
diff --git a/Makefile.am b/Makefile.am
index 1b18198..6d008f5 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -98,6 +98,9 @@ EXTRA_libffi_la_SOURCES = src/aarch64/ffi.c src/aarch64/sysv.S \
TARGET_OBJ = @TARGET_OBJ@
libffi_la_LIBADD = $(TARGET_OBJ)
+noinst_HEADERS += src/e2k/ffitarget.h
+EXTRA_libffi_la_SOURCES += src/e2k/ffi.c
+
libffi_convenience_la_SOURCES = $(libffi_la_SOURCES)
EXTRA_libffi_convenience_la_SOURCES = $(EXTRA_libffi_la_SOURCES)
libffi_convenience_la_LIBADD = $(libffi_la_LIBADD)
diff --git a/configure.host b/configure.host
index 2682671..38eb214 100644
--- a/configure.host
+++ b/configure.host
@@ -261,6 +261,11 @@ case "${host}" in
TARGET=XTENSA; TARGETDIR=xtensa
SOURCES="ffi.c sysv.S"
;;
+
+ e2k*-*)
+ TARGET=E2K; TARGETDIR=e2k
+ SOURCES="ffi.c"
+ ;;
esac
# ... but some of the cases above share configury.
diff --git a/src/e2k/ffi.c b/src/e2k/ffi.c
new file mode 100644
index 0000000..9fd246a
--- /dev/null
+++ b/src/e2k/ffi.c
@@ -0,0 +1,540 @@
+/* -----------------------------------------------------------------------
+ ffi.c - Elbrus 2000 Foreign Function Interface
+
+ Copyright 2021-2022 Ilya Kurdyukov <[email protected]> for BaseALT, Ltd
+
+ Permission is hereby granted, free of charge, to any person obtaining
+ a copy of this software and associated documentation files (the
+ ``Software''), to deal in the Software without restriction, including
+ without limitation the rights to use, copy, modify, merge, publish,
+ distribute, sublicense, and/or sell copies of the Software, and to
+ permit persons to whom the Software is furnished to do so, subject to
+ the following conditions:
+
+ The above copyright notice and this permission notice shall be included
+ in all copies or substantial portions of the Software.
+
+ THE SOFTWARE IS PROVIDED ``AS IS'', WITHOUT WARRANTY OF ANY KIND,
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
+ HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
+ WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+ DEALINGS IN THE SOFTWARE.
+ ----------------------------------------------------------------------- */
+
+#include <ffi.h>
+#include <ffi_common.h>
+
+#include <stdlib.h>
+#include <stdarg.h>
+#include <stdint.h>
+
+#ifdef __e2k__
+
+#ifndef __ptr64__
+#error
+#endif
+
+#define MAX(a, b) ((a) > (b) ? (a) : (b))
+#define ALIGN(a, b) (((a) + (b) - 1) & -(b))
+
+#define ARG_REGS 8
+#define ALLOCA_EXTRA 4
+typedef struct { long r[ARG_REGS]; } regs_t;
+
+ffi_status ffi_prep_cif_machdep(ffi_cif *cif)
+{
+ long bytes = 0, i, size, align, nargs = cif->nargs;
+ ffi_type **arg_types = cif->arg_types;
+
+ for (i = 0; i < nargs; i++)
+ {
+ size = arg_types[i]->size;
+ align = size > 8 ? 16 : 8;
+ bytes = ALIGN(bytes, align) + size;
+ }
+ cif->bytes = ALIGN(bytes, 8);
+
+ size = cif->rtype->size;
+ bytes = MAX(bytes, size);
+ bytes = MAX(bytes, sizeof(regs_t));
+ bytes = ALIGN(bytes, 16) - ALLOCA_EXTRA * sizeof(long);
+
+ switch (cif->rtype->type)
+ {
+ case FFI_TYPE_INT:
+ case FFI_TYPE_SINT8:
+ case FFI_TYPE_UINT8:
+ case FFI_TYPE_SINT16:
+ case FFI_TYPE_UINT16:
+ case FFI_TYPE_SINT32:
+ case FFI_TYPE_UINT32:
+ bytes |= 1;
+ }
+
+ cif->flags = bytes;
+
+ if (cif->abi != FFI_DEFAULT_ABI)
+ return FFI_BAD_ABI;
+
+ return FFI_OK;
+}
+
+#define REP8(X) X(0) X(1) X(2) X(3) X(4) X(5) X(6) X(7)
+#define MAKE_CALL(extra1, extra2) \
+ if (!rvalue) rsize = 0; \
+ { \
+ extra1; \
+ ffi_call_e2k(fn, rsize > sizeof(regs_t) ? 0 : rsize, rvalue); \
+ extra2; \
+ } \
+ if (rsize > sizeof(regs_t)) { \
+ long i, i2, i1; uint32_t t4, t2, t1; \
+ _Pragma("ivdep") \
+ for (i = 0; i < rsize - 7; i += 8) \
+ *(uint64_t*)((char*)rvalue + i) = *(uint64_t*)((char*)frame + i); \
+ i2 = i + (rsize & 4); \
+ i1 = i + (rsize & 6); \
+ if (rsize & 4) t4 = *(uint32_t*)((char*)frame + i); \
+ if (rsize & 2) t2 = *(uint16_t*)((char*)frame + i2); \
+ if (rsize & 1) t1 = *(uint8_t*)((char*)frame + i1); \
+ if (rsize & 4) *(uint32_t*)((char*)rvalue + i) = t4; \
+ if (rsize & 2) *(uint16_t*)((char*)rvalue + i2) = t2; \
+ if (rsize & 1) *(uint8_t*)((char*)rvalue + i1) = t1; \
+ }
+
+#define ASM_FN_START(name) \
+ "\t.global " #name "\n" \
+ "\t.type " #name ",@function\n" \
+ "\t.align 8\n" \
+ #name ":\n"
+
+#define ASM_FN_END(name) \
+ "\t.size " #name ", .- " #name "\n"
+
+#define ASM_FFI_CALL_E2K(a, b, c, d) \
+ ASM_FN_START(ffi_call_e2k) a b d \
+ ASM_FN_END(ffi_call_e2k) \
+ ASM_FN_START(ffi_call_e2k_noret) a c d \
+ ASM_FN_END(ffi_call_e2k_noret) \
+
+asm(
+ASM_FN_START(ffi_call_e2k)
+"\t{\n"
+"\t setwd wsz = 8, nfx = 1, dbl = 0\n"
+"\t setbn rbs = 4, rsz = 3, rcur = 0\n"
+"\t movtd,sm %r0, %ctpr1; ipd 2\n"
+"\t shld 1, 5, %r3\n"
+"\t}\n"
+"\t{\n"
+"\t nop 1\n"
+"\t getsp,0 0, %r0\n"
+"\t andnd %r1, 7, %r4\n"
+"\t andnd %r1, 3, %r5\n"
+"\t andnd %r1, 1, %r6\n"
+"\t}\n"
+"\t{\n"
+"\t ldd,sm 0, %r0, %b[0]\n"
+"\t ldd,sm 8, %r0, %b[1]\n"
+"\t ldd,sm 16, %r0, %b[2]\n"
+"\t ldd,sm 24, %r0, %b[3]\n"
+"\t addd %r0, %r3, %r7\n"
+"\t}\n"
+"\t{\n"
+"\t ldd,sm 0, %r7, %b[4]\n"
+"\t ldd,sm 8, %r7, %b[5]\n"
+"\t ldd,sm 16, %r7, %b[6]\n"
+"\t ldd,sm 24, %r7, %b[7]\n"
+"\t subd %r1, %r3, %r0\n"
+"\t}\n"
+"\t{\n"
+"\t cmpldb 7, %r1, %pred0\n"
+"\t cmpldb 15, %r1, %pred1\n"
+"\t cmpldb 23, %r1, %pred2\n"
+"\t cmpldb 31, %r1, %pred3\n"
+"\t andd %r1, 6, %r7\n"
+"\t}\n"
+"\t{\n"
+"\t nop 2\n"
+"\t cmpldb 7, %r0, %pred4\n"
+"\t cmpldb 15, %r0, %pred5\n"
+"\t cmpldb 23, %r0, %pred6\n"
+"\t cmpldb 31, %r0, %pred7\n"
+"\t shld %r7, 3, %r7\n"
+"\t}\n"
+"\t{\n"
+"\t call %ctpr1, wbs = 4; ipd 3\n"
+"\t addd %r2, %r3, %r0\n"
+"\t}\n"
+"\t{\n"
+"\t return %ctpr3; ipd 2\n"
+"\t addd,1,sm %b[6], 0, %b[7] ? ~%pred6\n"
+"\t std,sm 24, %r0, %b[7] ? %pred7\n"
+"\t std,sm 16, %r0, %b[6] ? %pred6\n"
+"\t}\n"
+"\t{\n"
+"\t cmpandesb %r1, 4, %pred9\n"
+"\t addd,0,sm %b[5], 0, %b[7] ? ~%pred5\n"
+"\t addd,1,sm %b[4], 0, %b[7] ? ~%pred4\n"
+"\t std,sm 8, %r0, %b[5] ? %pred5\n"
+"\t std,sm 0, %r0, %b[4] ? %pred4\n"
+"\t}\n"
+"\t{\n"
+"\t cmpandesb %r1, 2, %pred10\n"
+"\t addd,0,sm %b[3], 0, %b[7] ? ~%pred3\n"
+"\t addd,1,sm %b[2], 0, %b[7] ? ~%pred2\n"
+"\t std,sm 24, %r2, %b[3] ? %pred3\n"
+"\t std,sm 16, %r2, %b[2] ? %pred2\n"
+"\t}\n"
+"\t{\n"
+"\t cmpandesb %r1, 1, %pred11\n"
+"\t addd,0,sm %b[1], 0, %b[7] ? ~%pred1\n"
+"\t addd,1,sm %b[0], 0, %b[7] ? ~%pred0\n"
+"\t std,sm 8, %r2, %b[1] ? %pred1\n"
+"\t std,sm 0, %r2, %b[0] ? %pred0\n"
+"\t}\n"
+"\t{\n"
+"\t shrd,sm %b[7], %r7, %r7\n"
+"\t stw,sm %r2, %r4, %b[7] ? ~%pred9\n"
+"\t shrd,sm %b[7], %r3, %b[7] ? ~%pred9\n"
+"\t}\n"
+"\t{\n"
+"\t sth,sm %r2, %r5, %b[7] ? ~%pred10\n"
+"\t stb,sm %r2, %r6, %r7 ? ~%pred11\n"
+"\t}\n"
+"\t{\n"
+"\t ct %ctpr3; ipd 3\n"
+"\t}\n"
+ASM_FN_END(ffi_call_e2k)
+);
+
+void ffi_call_e2k(void (*fn)(void), long rsize, void *rvalue);
+
+void ffi_call(ffi_cif *cif, void (*fn)(void), void *rvalue, void **avalue)
+{
+ long *frame, *dst, i, size, nargs = cif->nargs, asize = cif->flags;
+ long rsize = asize & 1 ? sizeof(ffi_arg) : cif->rtype->size;
+ ffi_type **arg_types = cif->arg_types;
+
+ dst = frame = (long*)alloca(asize & -16) - ALLOCA_EXTRA;
+
+ for (i = 0; i < nargs; i++)
+ {
+ ffi_type *arg_type = arg_types[i];
+ void *src = avalue[i];
+ size = arg_type->size;
+ dst = (long*)FFI_ALIGN(dst, 8);
+
+ switch (arg_type->type)
+ {
+ case FFI_TYPE_SINT8:
+ *dst = *(SINT8*)src;
+ break;
+ case FFI_TYPE_UINT8:
+ *dst = *(UINT8*)src;
+ break;
+ case FFI_TYPE_SINT16:
+ *dst = *(SINT16*)src;
+ break;
+ case FFI_TYPE_UINT16:
+ *dst = *(UINT16*)src;
+ break;
+ case FFI_TYPE_SINT32:
+ *dst = *(SINT32*)src;
+ break;
+ case FFI_TYPE_UINT32:
+ case FFI_TYPE_FLOAT:
+ *dst = *(UINT32*)src;
+ break;
+ case FFI_TYPE_SINT64:
+ case FFI_TYPE_UINT64:
+ case FFI_TYPE_DOUBLE:
+ *dst = *(UINT64*)src;
+ break;
+ case FFI_TYPE_POINTER:
+ *dst = *(uintptr_t*)src;
+ break;
+ default:
+ if (size > 8) dst = (long*)FFI_ALIGN(dst, 16);
+ if (size & 7)
+ *(long*)(((uintptr_t)dst + size) & -8) = 0;
+ memcpy(dst, src, size);
+ }
+ dst = (long*)((uintptr_t)dst + size);
+ }
+
+ MAKE_CALL((void)0, (void)0)
+}
+
+static regs_t ffi_closure_e2k()
+{
+ register void *env __asm__("g16");
+ register void *sarg __asm__("g17");
+ ffi_closure *closure = env;
+ ffi_cif *cif = closure->cif;
+ regs_t *ret; void **args; char *ptr;
+ long i, size, align, *dst = sarg, nargs;
+ ffi_type **arg_types;
+
+ args = alloca(cif->nargs * sizeof(void*));
+ size = cif->rtype->size;
+ if (size < sizeof(*ret)) size = sizeof(*ret);
+ ret = alloca(size);
+
+ if (size & 7)
+ *(long*)(((uintptr_t)ret + size) & -8) = 0;
+
+ nargs = cif->nargs;
+ arg_types = cif->arg_types;
+ for (i = 0; i < nargs; i++)
+ {
+ ffi_type *arg_type = arg_types[i];
+ size = arg_type->size;
+ align = size > 8 ? 16 : 8;
+ ptr = (char*)FFI_ALIGN(dst, align);
+ dst = (long*)(ptr + size);
+
+ if ((uintptr_t)dst > (uintptr_t)sarg + 64)
+ ptr += 64;
+
+ args[i] = ptr;
+ }
+ /* g17 may be changed during the call */
+ dst = sarg;
+
+ closure->fun(cif, ret, args, closure->user_data);
+
+ switch (cif->rtype->type)
+ {
+ case FFI_TYPE_SINT8:
+ ret->r[0] = *(SINT8*)ret;
+ break;
+ case FFI_TYPE_SINT16:
+ ret->r[0] = *(SINT16*)ret;
+ break;
+ case FFI_TYPE_SINT32:
+ ret->r[0] = *(SINT32*)ret;
+ break;
+ default:
+ size = cif->rtype->size;
+ if (size > sizeof(*ret))
+ {
+ ptr = (char*)(dst + 8);
+ for (i = 0; i < size; i += sizeof(long))
+ *(long*)(ptr + i) = *(long*)((char*)ret + i);
+ }
+ }
+
+ return *ret;
+}
+
+ffi_status ffi_prep_closure_loc(ffi_closure* closure, ffi_cif* cif,
+ void (*fun)(ffi_cif*, void*, void**, void*),
+ void *user_data, void *codeloc)
+{
+ char *function = closure->tramp;
+ void *address = ffi_closure_e2k, *data = closure;
+
+ if (cif->abi != FFI_E2K)
+ return FFI_BAD_ABI;
+
+#define swaphilo(x) ((x) << 32 | (x) >> 32)
+ /* function:
+ * movtd,sm <address>, %ctpr1; ipd 2
+ * addd 0, <data>, %g16
+ *
+ * nop 1
+ * getsp -64, %g17
+ *
+ * std,sm %g17, 0, %r0
+ * std,sm %g17, 8, %r1
+ *
+ * std,sm %g17, 16, %r2
+ * std,sm %g17, 24, %r3
+ *
+ * std,sm %g17, 32, %r4
+ * std,sm %g17, 40, %r5
+ *
+ * nop 2
+ * std,sm %g17, 48, %r6
+ * std,sm %g17, 56, %r7
+ *
+ * ct %ctpr1
+ */
+ {
+ long *p = (long*)function;
+ p[0] = 0x800000000C001033;
+ p[1] = 0x11C0DCF0E1C0DED1;
+ p[2] = swaphilo((unsigned long) address);
+ p[3] = swaphilo((unsigned long) data);
+ p[4] = 0x58ECD4F104100091;
+ p[5] = 0xFFC0000001C00000;
+ p += 6;
+ p[0] = 0xA7F1D48090000012;
+ p[1] = 0x00000008A7F1D081;
+ p[2] = 0xA7F1D48290000012;
+ p[3] = 0x00100018A7F1D083;
+ p[4] = 0xA7F1D48490000012;
+ p[5] = 0x00200028A7F1D085;
+ p[6] = 0xA7F1D48690000112;
+ p[7] = 0x00300038A7F1D087;
+ p[8] = 0xC000042000001001;
+ /* size = (6+9)*8 = 120 */
+ }
+
+ closure->cif = cif;
+ closure->fun = fun;
+ closure->user_data = user_data;
+
+ __clear_cache(function, function + FFI_TRAMPOLINE_SIZE);
+
+ return FFI_OK;
+}
+
+#ifdef FFI_GO_CLOSURES
+/* set g16 = closure before calling fn() */
+void ffi_call_go(ffi_cif *cif, void (*fn)(void), void *rvalue,
+ void **avalue, void *closure)
+{
+ /* copy from ffi_call */
+ long *frame, *dst, i, size, nargs = cif->nargs, asize = cif->flags;
+ long rsize = asize & 1 ? sizeof(ffi_arg) : cif->rtype->size;
+ ffi_type **arg_types = cif->arg_types;
+
+ dst = frame = (long*)alloca(asize & -16) - ALLOCA_EXTRA;
+
+ for (i = 0; i < nargs; i++)
+ {
+ ffi_type *arg_type = arg_types[i];
+ void *src = avalue[i];
+ size = arg_type->size;
+ dst = (long*)FFI_ALIGN(dst, 8);
+
+ switch (arg_type->type)
+ {
+ case FFI_TYPE_SINT8:
+ *dst = *(SINT8*)src;
+ break;
+ case FFI_TYPE_UINT8:
+ *dst = *(UINT8*)src;
+ break;
+ case FFI_TYPE_SINT16:
+ *dst = *(SINT16*)src;
+ break;
+ case FFI_TYPE_UINT16:
+ *dst = *(UINT16*)src;
+ break;
+ case FFI_TYPE_SINT32:
+ *dst = *(SINT32*)src;
+ break;
+ case FFI_TYPE_UINT32:
+ case FFI_TYPE_FLOAT:
+ *dst = *(UINT32*)src;
+ break;
+ case FFI_TYPE_SINT64:
+ case FFI_TYPE_UINT64:
+ case FFI_TYPE_DOUBLE:
+ *dst = *(UINT64*)src;
+ break;
+ case FFI_TYPE_POINTER:
+ *dst = *(uintptr_t*)src;
+ break;
+ default:
+ if (size > 8) dst = (long*)FFI_ALIGN(dst, 16);
+ if (size & 7)
+ *(long*)(((uintptr_t)dst + size) & -8) = 0;
+ memcpy(dst, src, size);
+ }
+ dst = (long*)((uintptr_t)dst + size);
+ }
+ /* copy end */
+ MAKE_CALL(
+ register void *chain_reg __asm__("g16") = closure,
+ __asm__ __volatile__ ("" : : "r"(chain_reg) : "memory")
+ )
+}
+
+/* with ffi_go_closure, user_data = closure */
+static regs_t ffi_go_closure_e2k()
+{
+ register void *env __asm__("g16");
+ register long *sarg __asm__("g17");
+ ffi_go_closure *closure = env;
+ /* copy from ffi_closure */
+ ffi_cif *cif = closure->cif;
+ regs_t *ret; void **args; char *ptr;
+ long i, size, align, *dst = sarg, nargs;
+ ffi_type **arg_types;
+
+ args = alloca(cif->nargs * sizeof(void*));
+ size = cif->rtype->size;
+ if (size < sizeof(*ret)) size = sizeof(*ret);
+ ret = alloca(size);
+
+ if (size & 7)
+ *(long*)(((uintptr_t)ret + size) & -8) = 0;
+
+ nargs = cif->nargs;
+ arg_types = cif->arg_types;
+ for (i = 0; i < nargs; i++)
+ {
+ ffi_type *arg_type = arg_types[i];
+ size = arg_type->size;
+ align = size > 8 ? 16 : 8;
+ ptr = (char*)FFI_ALIGN(dst, align);
+ dst = (long*)(ptr + size);
+
+ if ((uintptr_t)dst > (uintptr_t)sarg + 64)
+ ptr += 64;
+
+ args[i] = ptr;
+ }
+ /* g17 may be changed during the call */
+ dst = sarg;
+ /* copy end */
+
+ closure->fun(cif, ret, args, closure);
+
+ /* copy from ffi_closure */
+ switch (cif->rtype->type)
+ {
+ case FFI_TYPE_SINT8:
+ ret->r[0] = *(SINT8*)ret;
+ break;
+ case FFI_TYPE_SINT16:
+ ret->r[0] = *(SINT16*)ret;
+ break;
+ case FFI_TYPE_SINT32:
+ ret->r[0] = *(SINT32*)ret;
+ break;
+ default:
+ size = cif->rtype->size;
+ if (size > sizeof(*ret))
+ {
+ ptr = (char*)(dst + 8);
+ for (i = 0; i < size; i += sizeof(long))
+ *(long*)(ptr + i) = *(long*)((char*)ret + i);
+ }
+ }
+
+ return *ret;
+ /* copy end */
+}
+
+ffi_status ffi_prep_go_closure(ffi_go_closure* closure, ffi_cif* cif,
+ void (*fun)(ffi_cif*, void*, void**, void*))
+{
+ closure->tramp = ffi_go_closure_e2k;
+ closure->cif = cif;
+ closure->fun = fun;
+
+ if (cif->abi != FFI_DEFAULT_ABI)
+ return FFI_BAD_ABI;
+
+ return FFI_OK;
+}
+#endif
+
+#endif /* __e2k__ */
diff --git a/src/e2k/ffitarget.h b/src/e2k/ffitarget.h
new file mode 100644
index 0000000..5e14e95
--- /dev/null
+++ b/src/e2k/ffitarget.h
@@ -0,0 +1,63 @@
+/* -----------------------------------------------------------------*-C-*-
+ ffitarget.h - Target configuration macros for Elbrus 2000.
+
+ Copyright 2021 Ilya Kurdyukov <[email protected]> for BaseALT, Ltd
+
+ Permission is hereby granted, free of charge, to any person obtaining
+ a copy of this software and associated documentation files (the
+ ``Software''), to deal in the Software without restriction, including
+ without limitation the rights to use, copy, modify, merge, publish,
+ distribute, sublicense, and/or sell copies of the Software, and to
+ permit persons to whom the Software is furnished to do so, subject to
+ the following conditions:
+
+ The above copyright notice and this permission notice shall be included
+ in all copies or substantial portions of the Software.
+
+ THE SOFTWARE IS PROVIDED ``AS IS'', WITHOUT WARRANTY OF ANY KIND,
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
+ HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
+ WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+ DEALINGS IN THE SOFTWARE.
+
+ ----------------------------------------------------------------------- */
+
+#ifndef LIBFFI_TARGET_H
+#define LIBFFI_TARGET_H
+
+#ifndef LIBFFI_H
+#error "Please do not include ffitarget.h directly into your source. Use ffi.h instead."
+#endif
+
+/* ---- System specific configurations ----------------------------------- */
+
+#define FFI_SIZEOF_ARG 8
+#define FFI_TARGET_SPECIFIC_STACK_SPACE_ALLOCATION
+#define FFI_TARGET_HAS_COMPLEX_TYPE
+
+/* ---- Generic type definitions ----------------------------------------- */
+
+#ifndef LIBFFI_ASM
+typedef unsigned long ffi_arg;
+typedef signed long ffi_sarg;
+
+typedef enum ffi_abi {
+ FFI_FIRST_ABI = 0,
+ FFI_E2K,
+ FFI_LAST_ABI,
+ FFI_DEFAULT_ABI = FFI_E2K
+} ffi_abi;
+#endif
+
+/* ---- Definitions for closures ----------------------------------------- */
+
+#define FFI_CLOSURES 1
+#define FFI_GO_CLOSURES 1
+#define FFI_TRAMPOLINE_SIZE 120
+#define FFI_NATIVE_RAW_API 0
+
+#endif
+
diff --git a/testsuite/lib/libffi.exp b/testsuite/lib/libffi.exp
index 4f4dd48..4750e1d 100644
--- a/testsuite/lib/libffi.exp
+++ b/testsuite/lib/libffi.exp
@@ -652,6 +652,13 @@ if { [info procs saved-dg-test] == [list] } {
}
}
+if { [istarget "e2k-*-*"] } {
+ # --print-multi-lib option not unsupported
+ proc get_multilibs { args } {
+ return ""
+ }
+}
+
# Local Variables:
# tcl-indent-level:4
# End:
diff --git a/testsuite/libffi.complex/complex.exp b/testsuite/libffi.complex/complex.exp
index 4631db2..c7441e6 100644
--- a/testsuite/libffi.complex/complex.exp
+++ b/testsuite/libffi.complex/complex.exp
@@ -21,6 +21,13 @@ global srcdir subdir
set tlist [lsort [glob -nocomplain -- $srcdir/$subdir/*.{c,cc}]]
+if { [libffi_feature_test "#ifdef __e2k__"] } {
+ # _Complex can only be used with floating-point types
+ unsupported "$srcdir/$subdir/complex_int.c"
+ set idx [lsearch -exact $tlist "$srcdir/$subdir/complex_int.c"]
+ set tlist [lreplace $tlist $idx $idx]
+}
+
if { [libffi_feature_test "#ifdef FFI_TARGET_HAS_COMPLEX_TYPE"] } {
run-many-tests $tlist ""
} else {
diff --git a/testsuite/libffi.go/static-chain.h b/testsuite/libffi.go/static-chain.h
index 3675b40..ce056c0 100644
--- a/testsuite/libffi.go/static-chain.h
+++ b/testsuite/libffi.go/static-chain.h
@@ -16,4 +16,6 @@
# ifndef ABI_NUM
# define STATIC_CHAIN_REG "ecx" /* FFI_DEFAULT_ABI only */
# endif
+#elif defined(__e2k__)
+# define STATIC_CHAIN_REG "g16"
#endif
--
2.34.1