-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpartial.c
1077 lines (982 loc) · 27.4 KB
/
partial.c
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
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
Copyright (c) 2015, Sky Leonard
All rights reserved.
For more info see COPYING or http://opensource.org/licenses/BSD-3-Clause
*/
#include "partial.h"
#include <stdlib.h>
#include <ctype.h>
#include "ffi.h"
/**************************************
* Default values
**************************************/
static char * DEFAULT_STRUCT_NAME = "Unknown";
static char * DEFAULT_FUNCTION_NAME = "function";
static char * INDENT = " ";
static char * PRIMITIVE_NAMES[] = {
"void",
"uint8_t",
"int8_t",
"uint16_t",
"int16_t",
"uint32_t",
"int32_t",
"uint64_t",
"int64_t",
"float",
"double",
"unsigned char",
"signed char",
"unsigned short",
"signed short",
"unsigned int",
"signed int",
"unsigned long",
"signed long",
"long double",
"void *",
"size_t",
};
static ffi_type * FFI_TYPES[] = {
&ffi_type_void,
&ffi_type_uint8,
&ffi_type_sint8,
&ffi_type_uint16,
&ffi_type_sint16,
&ffi_type_uint32,
&ffi_type_sint32,
&ffi_type_uint64,
&ffi_type_sint64,
&ffi_type_float,
&ffi_type_double,
&ffi_type_uchar,
&ffi_type_schar,
&ffi_type_ushort,
&ffi_type_sshort,
&ffi_type_uint,
&ffi_type_sint,
&ffi_type_ulong,
&ffi_type_slong,
&ffi_type_pointer,
#define CHECK_SIZE_T
#if SIZE_MAX && SIZE_MAX == UINT16_MAX
&ffi_type_uint16,
#elif SIZE_MAX && SIZE_MAX == UINT32_MAX
&ffi_type_uint32,
#elif SIZE_MAX && SIZE_MAX == UINT64_MAX
&ffi_type_uint64,
#else
NULL,
# undef CHECK_SIZE_T
# define CHECK_SIZE_T if (FFI_TYPES[PartialType_size_t] == NULL){\
if (sizeof(size_t) == sizeof(uint16_t)){\
FFI_TYPES[PartialType_size_t] = &ffi_type_uint16;\
} else if (sizeof(size_t) == sizeof(uint32_t)){\
FFI_TYPES[PartialType_size_t] = &ffi_type_uint32;\
} else if (sizeof(size_t) == sizeof(uint64_t)){\
FFI_TYPES[PartialType_size_t] = &ffi_type_uint64;\
}\
}
#endif
};
/**************************************
* Types
**************************************/
struct Partial {
struct PartialFDef * targetdef;
int manage;
void * target;
size_t nargs;
union {
uint64_t s;
uint8_t * d;
} argsset;
union PartialArg * args;
struct PartialFDef * extdef;
void ** ffi_args;
void * ffi_writable;
void * ffi_exc;
};
struct PartialSDef {
char * name;
size_t nmemb;
size_t imemb;
enum PartialType * members;
struct PartialSDef ** struct_members;
ffi_type ffi_type_;
};
struct PartialFDef {
char * name;
enum PartialType returns;
struct PartialSDef * returns_struct;
size_t nargs;
size_t iargs;
enum PartialType * args;
struct PartialSDef ** struct_args;
int ffi_setup;
ffi_cif ffi_cif_;
ffi_type * ffi_rtype;
ffi_type ** ffi_argtypes;
};
/**************************************
* Utility
**************************************/
#define ONE64 ((uint64_t) 1)
#define PARTIALSET_IN(p, n) ((p->nargs > 64) ?\
(p->argsset.d[(n) >> 3] & (1 << ((n) & 0x7))) :\
(p->argsset.s & (ONE64 << (n))))
#define PARTIALSET_SET(p, n) if (p->nargs > 64){\
p->argsset.d[(n) >> 8] |= (1 << ((n) & 0xff));\
} else {\
p->argsset.s |= (ONE64 << (n));\
}
static int types_compatible(enum PartialType a, enum PartialType b){
return (FFI_TYPES[a] == FFI_TYPES[b]);
}
static enum PartialType char2type(char c){
switch (c){
case 'v':
return PartialType_void;
case 'B':
return PartialType_uint8;
case 'b':
return PartialType_sint8;
case 'H':
return PartialType_uint16;
case 'h':
return PartialType_sint16;
case 'J':
return PartialType_uint32;
case 'j':
return PartialType_sint32;
case 'Q':
return PartialType_uint64;
case 'q':
return PartialType_sint64;
case 'f':
return PartialType_float;
case 'd':
return PartialType_double;
case 'C':
return PartialType_uchar;
case 'c':
return PartialType_schar;
case 'S':
return PartialType_ushort;
case 's':
return PartialType_sshort;
case 'I':
return PartialType_uint;
case 'i':
return PartialType_sint;
case 'L':
return PartialType_ulong;
case 'l':
return PartialType_slong;
case 'p':
return PartialType_pointer;
case 'T':
return PartialType_size_t;
default:
return PartialType_struct; // used as error marker
}
}
/**************************************
* Structure definition operations
**************************************/
struct PartialSDef * partial_sdef(size_t nmemb, ...){
va_list members;
struct PartialSDef * res;
va_start(members, nmemb);
res = partial_sdef_va(nmemb, &members);
va_end(members);
return res;
}
struct PartialSDef * partial_sdef_va(
size_t nmemb, va_list * members){
struct PartialSDef * self;
enum PartialType type;
struct PartialSDef * structmemb;
size_t i;
self = partial_sdef_empty(nmemb);
if (self != NULL){
for (i = 0; i < nmemb; ++i){
type = va_arg(*members, enum PartialType);
switch (type) {
case PartialType_void:
partial_sdef_free(self);
return NULL;
case PartialType_struct:
structmemb = va_arg(*members, struct PartialSDef *);
partial_sdef_nextmemb_struct(self, structmemb);
break;
default:
partial_sdef_nextmemb(self, type);
break;
}
}
}
return self;
}
struct PartialSDef * partial_sdef_empty(size_t nmemb){
struct PartialSDef * self;
enum PartialType * members;
struct PartialSDef ** struct_members;
ffi_type ** ffi_elements;
self = malloc(sizeof(struct PartialSDef));
if (self == NULL){
return NULL;
}
if (nmemb != 0){
members = malloc(nmemb * sizeof(enum PartialType));
if (members == NULL){
free(self);
return NULL;
}
struct_members = malloc(nmemb * sizeof(struct PartialSDef *));
if (struct_members == NULL){
free(members);
free(self);
return NULL;
}
}
ffi_elements = malloc((nmemb + 1) * sizeof(ffi_type *));
if (ffi_elements == NULL){
free(struct_members);
free(members);
free(self);
return NULL;
}
ffi_elements[nmemb] = NULL;
self->name = DEFAULT_STRUCT_NAME;
self->nmemb = nmemb;
self->imemb = 0;
self->members = members;
self->struct_members = struct_members;
self->ffi_type_.size = 0;
self->ffi_type_.alignment = 0;
self->ffi_type_.type = FFI_TYPE_STRUCT;
self->ffi_type_.elements = ffi_elements;
return self;
}
struct PartialSDef * partial_sdef_fromstr(char * format){
struct PartialSDef * self;
char c;
char * fmt;
size_t nmemb = 0;
enum PartialType type;
fmt = format;
while ((c = *fmt++)){
nmemb += 1;
}
self = partial_sdef_empty(nmemb);
if (self != NULL){
fmt = format;
while ((c = *fmt++)){
if (isspace(c)){
continue;
}
type = char2type(c);
switch (type) {
case PartialType_struct:
case PartialType_void:
partial_sdef_free(self);
return NULL;
default:
partial_sdef_nextmemb(self, type);
break;
}
}
}
return self;
}
void partial_sdef_free(struct PartialSDef * self){
free(self->ffi_type_.elements);
if (self->nmemb != 0){
free(self->struct_members);
free(self->members);
}
free(self);
}
void partial_sdef_nextmemb(
struct PartialSDef * self, enum PartialType type){
CHECK_SIZE_T;
self->members[self->imemb] = type;
self->ffi_type_.elements[self->imemb] = FFI_TYPES[type];
self->imemb += 1;
}
void partial_sdef_nextmemb_struct(
struct PartialSDef * self, struct PartialSDef * type){
self->members[self->imemb] = PartialType_struct;
self->struct_members[self->imemb] = type;
self->ffi_type_.elements[self->imemb] = &(type->ffi_type_);
self->imemb += 1;
}
int partial_sdef_equal(
struct PartialSDef * a, struct PartialSDef * b){
size_t i;
if (a->nmemb != b->nmemb){
return 0;
}
for (i = 0; i < a->nmemb; ++i){
if (!types_compatible(a->members[i], b->members[i])){
return 0;
}
if (a->members[i] == PartialType_struct){
if (!partial_sdef_equal(
a->struct_members[i], b->struct_members[i])){
return 0;
}
}
}
return 1;
}
void partial_sdef_setname(struct PartialSDef * self, char * name){
self->name = name;
}
void partial_sdef_print(struct PartialSDef * self){
// TODO: maybe make this not a pass-through?
partial_sdef_fprint(stdout, self);
}
void partial_sdef_fprint(FILE * stream, struct PartialSDef * self){
size_t i;
enum PartialType type;
fprintf(stream, "struct %s {\n", self->name);
for (i = 0; i < self->nmemb; ++i){
type = self->members[i];
if (type == PartialType_struct){
fprintf(stream, "%sstruct %s;\n",
INDENT, self->struct_members[i]->name);
} else {
fprintf(stream, "%s%s;\n", INDENT, PRIMITIVE_NAMES[type]);
}
}
fputs("};\n", stream);
}
void partial_sdef_snprint(
char * buffer, size_t n, struct PartialSDef * self){
// TODO
}
/**************************************
* Function definition operations
**************************************/
struct PartialFDef * partial_fdef(
enum PartialType returns, size_t nargs, ...){
va_list args;
struct PartialFDef * res;
va_start(args, nargs);
res = partial_fdef_va(returns, nargs, &args);
va_end(args);
return res;
}
struct PartialFDef * partial_fdef_va(
enum PartialType returns, size_t nargs, va_list * args){
struct PartialFDef * self;
enum PartialType type;
struct PartialSDef * structarg;
size_t i;
if (returns == PartialType_struct){
structarg = va_arg(*args, struct PartialSDef *);
self = partial_fdef_empty_struct(structarg, nargs);
} else {
self = partial_fdef_empty(returns, nargs);
}
if (self != NULL){
for (i = 0; i < nargs; ++i) {
type = va_arg(*args, enum PartialType);
switch (type) {
case PartialType_void:
partial_fdef_free(self);
return NULL;
case PartialType_struct:
structarg = va_arg(*args, struct PartialSDef *);
partial_fdef_nextarg_struct(self, structarg);
break;
default:
partial_fdef_nextarg(self, type);
break;
}
}
}
return self;
}
static struct PartialFDef * function_def_empty_base(size_t nargs){
struct PartialFDef * self;
enum PartialType * args;
struct PartialSDef ** struct_args;
ffi_type ** ffi_argtypes;
self = malloc(sizeof(struct PartialFDef));
if (self == NULL){
return NULL;
}
if (nargs != 0){
args = malloc(nargs * sizeof(enum PartialType));
if (args == NULL){
free(self);
return NULL;
}
struct_args = malloc(nargs * sizeof(struct PartialSDef *));
if (struct_args == NULL){
free(args);
free(self);
return NULL;
}
ffi_argtypes = malloc(nargs * sizeof(ffi_type *));
if (ffi_argtypes == NULL){
free(struct_args);
free(args);
free(self);
return NULL;
}
self->args = args;
self->struct_args = struct_args;
self->ffi_argtypes = ffi_argtypes;
}
self->name = DEFAULT_FUNCTION_NAME;
self->nargs = nargs;
self->iargs = 0;
self->ffi_setup = 0;
return self;
}
struct PartialFDef * partial_fdef_empty(
enum PartialType returns, size_t nargs){
struct PartialFDef * self;
CHECK_SIZE_T;
if ((self = function_def_empty_base(nargs)) != NULL){
self->returns = returns;
self->ffi_rtype = FFI_TYPES[returns];
}
return self;
}
struct PartialFDef * partial_fdef_empty_struct(
struct PartialSDef * returns, size_t nargs){
struct PartialFDef * self;
if ((self = function_def_empty_base(nargs)) != NULL){
self->returns = PartialType_struct;
self->returns_struct = returns;
self->ffi_rtype = &(returns->ffi_type_);
}
return self;
}
struct PartialFDef * partial_fdef_fromstr(char * format){
struct PartialFDef * self;
char c;
char * fmt;
size_t nargs = 0;
enum PartialType type;
c = *format++;
type = char2type(c);
if (type == PartialType_struct) {
return NULL;
}
fmt = format;
while ((c = *fmt++)){
if (!isspace(c)){
nargs += 1;
}
}
self = partial_fdef_empty(type, nargs);
if (self != NULL){
fmt = format;
while ((c = *fmt++)){
if (isspace(c)){
continue;
}
type = char2type(c);
switch (type) {
case PartialType_struct:
case PartialType_void:
partial_fdef_free(self);
return NULL;
default:
partial_fdef_nextarg(self, type);
break;
}
}
}
return self;
}
void partial_fdef_free(struct PartialFDef * self){
if (self->nargs != 0){
free(self->ffi_argtypes);
free(self->struct_args);
free(self->args);
}
free(self);
}
void partial_fdef_nextarg(
struct PartialFDef * self, enum PartialType type){
CHECK_SIZE_T;
self->args[self->iargs] = type;
self->ffi_argtypes[self->iargs] = FFI_TYPES[type];
self->iargs += 1;
}
void partial_fdef_nextarg_struct(
struct PartialFDef * self, struct PartialSDef * type){
self->args[self->iargs] = PartialType_struct;
self->struct_args[self->iargs] = type;
self->ffi_argtypes[self->iargs] = &(type->ffi_type_);
self->iargs += 1;
}
int partial_fdef_equal(
struct PartialFDef * a, struct PartialFDef * b){
size_t i;
if (a->returns != b->returns || a->nargs != b->nargs){
return 0;
}
if (a->returns == PartialType_struct
&& !partial_sdef_equal(
a->returns_struct, b->returns_struct)){
return 0;
}
for (i = 0; i < a->nargs; ++i){
if (!types_compatible(a->args[i], b->args[i])){
return 0;
}
if (a->args[i] == PartialType_struct){
if (!partial_sdef_equal(
a->struct_args[i], b->struct_args[i])){
return 0;
}
}
}
return 1;
}
void partial_fdef_setname(
struct PartialFDef * self, char * name){
self->name = name;
}
void partial_fdef_print(struct PartialFDef * self){
// TODO: maybe make this not a pass-through?
partial_fdef_fprint(stdout, self);
}
void partial_fdef_fprint(
FILE * stream, struct PartialFDef * self){
size_t i;
enum PartialType type;
if (self->returns == PartialType_struct){
printf("struct %s %s(", self->returns_struct->name, self->name);
} else {
printf("%s %s(", PRIMITIVE_NAMES[self->returns], self->name);
}
i = 0;
if (i < self->nargs){
type = self->args[i];
if (type == PartialType_struct){
fprintf(stream, "struct %s", self->struct_args[i]->name);
} else {
fprintf(stream, "%s", PRIMITIVE_NAMES[type]);
}
}
for (i += 1; i < self->nargs; ++i){
type = self->args[i];
if (type == PartialType_struct){
fprintf(stream, ", struct %s", self->struct_args[i]->name);
} else {
fprintf(stream, ", %s", PRIMITIVE_NAMES[type]);
}
}
fputs(");\n", stream);
}
void partial_fdef_snprint(
char * buffer, size_t n, struct PartialFDef * self){
// TODO
}
void partial_fdef_print_indent(struct PartialFDef * self){
// TODO: maybe make this not a pass-through?
partial_fdef_fprint_indent(stdout, self);
}
void partial_fdef_fprint_indent(
FILE * stream, struct PartialFDef * self){
size_t i;
enum PartialType type;
if (self->returns == PartialType_struct){
printf("struct %s %s(", self->returns_struct->name, self->name);
} else {
printf("%s %s(", PRIMITIVE_NAMES[self->returns], self->name);
}
i = 0;
if (i < self->nargs){
type = self->args[i];
if (type == PartialType_struct){
fprintf(stream, "\n%sstruct %s",
INDENT, self->struct_args[i]->name);
} else {
fprintf(stream, "\n%s%s", INDENT, PRIMITIVE_NAMES[type]);
}
}
for (i += 1; i < self->nargs; ++i){
type = self->args[i];
if (type == PartialType_struct){
fprintf(stream, ",\n%sstruct %s",
INDENT, self->struct_args[i]->name);
} else {
fprintf(stream, ",\n%s%s", INDENT, PRIMITIVE_NAMES[type]);
}
}
fputs(");\n", stream);
}
void partial_fdef_snprint_indent(
char * buffer, size_t n, struct PartialFDef * self){
// TODO
}
static int function_def_prep_cif(struct PartialFDef * self){
ffi_status status;
// TODO: ABI??
status = ffi_prep_cif(
&(self->ffi_cif_),
FFI_DEFAULT_ABI,
self->nargs,
self->ffi_rtype,
self->ffi_argtypes);
if (status == FFI_OK){
self->ffi_setup = 1;
return 0;
} else {
return -1;
}
}
/**************************************
* Partial operations
**************************************/
struct Partial * partial(
struct PartialFDef * targetdef, void * target){
struct Partial * self;
size_t nargs;
uint8_t * argsset;
self = malloc(sizeof(struct Partial));
if (self == NULL){
return NULL;
}
self->targetdef = targetdef;
self->manage = 0;
self->target = target;
self->nargs = nargs = targetdef->nargs;
if (nargs != 0){
self->args = malloc(nargs * sizeof(union PartialArg));
if (self->args == NULL){
free(self);
return NULL;
}
self->ffi_args = malloc(nargs * sizeof(void *));
if (self->ffi_args == NULL){
free(self->args);
free(self);
return NULL;
}
}
if (nargs > 64){
argsset = calloc(((nargs >> 3) + 1), sizeof(uint8_t));
if (argsset == NULL){
free(self->ffi_args);
free(self->args);
free(self);
return NULL;
}
self->argsset.d = argsset;
} else {
self->argsset.s = 0;
}
self->extdef = NULL;
self->ffi_writable = NULL;
return self;
}
struct Partial * partial_manage(
struct PartialFDef * targetdef, void * target){
struct Partial * self;
self = partial(targetdef, target);
if (self == NULL){
partial_fdef_free(targetdef);
return NULL;
}
self->manage = 1;
return self;
}
struct Partial * partial_fromstr(char * fdef, void * target){
struct PartialFDef * targetdef;
targetdef = partial_fdef_fromstr(fdef);
if (targetdef == NULL){
return NULL;
}
return partial_manage(targetdef, target);
}
void partial_free(struct Partial * self){
if (self->nargs > 64){
free(self->argsset.d);
}
if (self->manage){
partial_fdef_free(self->targetdef);
}
if (self->extdef != NULL){
partial_fdef_free(self->extdef);
}
if (self->ffi_writable != NULL){
ffi_closure_free(self->ffi_writable);
}
if (self->nargs != 0){
free(self->args);
free(self->ffi_args);
}
free(self);
}
void partial_set_args(struct Partial * self, size_t start, size_t nargs, ...){
va_list args;
va_start(args, nargs);
partial_set_args_va(self, start, nargs, &args);
va_end(args);
}
#define sav_case(ct, pt) case PartialType_##pt:\
value.a_##pt = va_arg(*args, ct);\
break;
void partial_set_args_va(
struct Partial * self, size_t start, size_t nargs, va_list * args){
union PartialArg value;
size_t i;
for (i = start; i < start + nargs; ++i){
switch (self->targetdef->args[i]){
sav_case(int, uint8);
sav_case(int, sint8);
sav_case(int, uint16);
sav_case(int, sint16);
sav_case(uint32_t, uint32);
sav_case(int32_t, sint32);
sav_case(uint64_t, uint64);
sav_case(int64_t, sint64);
sav_case(double, float);
sav_case(double, double);
sav_case(int, uchar);
sav_case(int, schar);
sav_case(int, ushort);
sav_case(int, sshort);
sav_case(unsigned int, uint);
sav_case(signed int, sint);
sav_case(unsigned long, ulong);
sav_case(signed long, slong);
sav_case(void *, pointer);
sav_case(size_t, size_t);
case PartialType_struct:
value.a_struct = va_arg(*args, void *);
self->args[i] = value;
self->ffi_args[i] = value.a_struct;
continue;
default:
// TODO: idk
break;
}
PARTIALSET_SET(self, i);
self->args[i] = value;
self->ffi_args[i] = &(self->args[i]);
}
}
void partial_set_arg(
struct Partial * self, size_t arg, union PartialArg value){
PARTIALSET_SET(self, arg);
self->args[arg] = value;
if (self->targetdef->args[arg] == PartialType_struct){
self->ffi_args[arg] = value.a_struct;
} else {
self->ffi_args[arg] = &(self->args[arg]);
}
}
struct PartialFDef * partial_getdef(struct Partial * self){
struct PartialFDef * def;
size_t i;
size_t nargs;
enum PartialType type;
if (self->extdef != NULL){
return self->extdef;
}
nargs = 0;
for (i = 0; i < self->nargs; ++i){
if (!PARTIALSET_IN(self, i)){
nargs += 1;
}
}
def = partial_fdef_empty(self->targetdef->returns, nargs);
if (def == NULL){
return NULL;
}
for (i = 0; i < self->nargs; ++i){
if (!PARTIALSET_IN(self, i)) {
type = self->targetdef->args[i];
if (type == PartialType_struct){
partial_fdef_nextarg_struct(
def, self->targetdef->struct_args[i]);
} else {
partial_fdef_nextarg(def, type);
}
}
}
self->extdef = def;
return def;
}
static void closure_cb(ffi_cif * cif, void * ret, void ** args, void * ud){
struct Partial * self;
size_t i, argsi = 0;
self = ud;
for (i = 0; i < self->nargs; ++i){
if (!PARTIALSET_IN(self, i)){
self->ffi_args[i] = args[argsi++];
}
}
ffi_call(
&(self->targetdef->ffi_cif_),
self->target,
ret,
self->ffi_args);
}
void * partial_compile(struct Partial * self){
struct PartialFDef * extdef;
void * writable, * code;
if (!(self->targetdef->ffi_setup
|| !function_def_prep_cif(self->targetdef))){
return NULL;
}
extdef = partial_getdef(self);
if (extdef == NULL
|| !(extdef->ffi_setup || !function_def_prep_cif(extdef))){
return NULL;
}
// TODO need more size?
writable = ffi_closure_alloc(sizeof(ffi_closure), &code);
if (writable == NULL){
return NULL;
}
self->ffi_writable = writable;
self->ffi_exc = code;
ffi_prep_closure_loc(
writable, &(extdef->ffi_cif_), &closure_cb, self, code);
return code;
}
int partial_quick(
union PartialArg * returnval,
char * passfmt, void * pass,
char * funcfmt, void * func,
size_t nargs, ...){
va_list args;
int res;
va_start(args, nargs);
res = partial_quick_va(
returnval, passfmt, pass, funcfmt, func, nargs, &args);
va_end(args);
return res;
}
int partial_quick_va(
union PartialArg * returnval,
char * passfmt, void * pass,
char * funcfmt, void * func,
size_t nargs, va_list * args){
struct PartialFDef * passdef = NULL;
struct PartialFDef * funcdef = NULL;
struct Partial * passp = NULL;
struct Partial * funcp = NULL;
size_t passargifunc, passnargs;
int paif_set;
char * fmt, c;
enum PartialType type;
union PartialArg value;
int res;
void * func_exc;
void * pass_exc;
struct PartialFDef * pass_exc_def;
funcdef = partial_fdef_fromstr(funcfmt);
if (funcdef == NULL){
goto error;
}
c = *passfmt++;
type = char2type(c);
if (type == PartialType_struct) {
goto error;
}
fmt = passfmt;
passnargs = 0;
while ((c = *fmt++)){
if (!isspace(c)){
passnargs += 1;
}
}
passdef = partial_fdef_empty(type, passnargs);
if (passdef == NULL){
goto error;
}
fmt = passfmt;
passargifunc = 0;
paif_set = 0;
while ((c = *fmt++)){
if (isspace(c)){
continue;