-
Notifications
You must be signed in to change notification settings - Fork 1
/
function.c
673 lines (620 loc) · 16 KB
/
function.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
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <stdbool.h>
#include <ffi.h>
#include "util.h"
#include "function.h"
value_t empty_array = {
type: TYPE_ARRAY,
};
value_t void_value = {
type: TYPE_VOID,
};
typedef struct {
int size;
} array_internal_t;
typedef struct _c_function_def {
void*runtime;
const char*name;
fptr_t call;
void*context;
char*params;
char*ret;
} c_function_def_t;
typedef struct _function_signature {
int num_params;
type_t*param;
type_t ret;
} function_signature_t;
int count_function_defs(c_function_def_t*methods)
{
int i = 0;
while(methods[i].call) {
i++;
}
return i;
}
static int _parse_type(const char*s, type_t*type)
{
const char*start = s;
switch(*s) {
case '\0': *type = TYPE_VOID; break;
case 'b': *type = TYPE_BOOLEAN; break;
case 'i': *type = TYPE_INT32; break;
case 'f': *type = TYPE_FLOAT32; break;
default:
case 's': *type = TYPE_STRING; break;
case '[': *type = TYPE_ARRAY; break;
}
s++;
return s - start;
}
int function_count_args(c_function_def_t*method)
{
const char*a;
int count = 0;
for(a=method->params; *a; a++) {
type_t type;
count += _parse_type(a, &type);
}
return count;
}
const char* type_to_string(type_t type)
{
switch(type) {
case TYPE_VOID:
return "void";
break;
case TYPE_FLOAT32:
return "float32";
break;
case TYPE_INT32:
return "int32";
break;
case TYPE_BOOLEAN:
return "boolean";
break;
case TYPE_STRING:
return "string";
break;
case TYPE_ARRAY:
return "array";
break;
default:
return "<unknown>";
break;
}
}
value_t* value_clone(const value_t*src)
{
switch(src->type) {
case TYPE_VOID:
break;
case TYPE_FLOAT32:
return value_new_float32(src->f32);
break;
case TYPE_INT32:
return value_new_int32(src->i32);
break;
case TYPE_BOOLEAN:
return value_new_boolean(src->b);
break;
case TYPE_STRING:
return value_new_string(src->str);
break;
case TYPE_ARRAY: {
value_t*array = array_new();
int i;
for(i=0;i<src->length;i++) {
array_append(array, value_clone(src->data[i]));
}
return array;
}
break;
}
return NULL;
}
static ffi_type* _type_to_ffi_type(type_t type)
{
switch(type) {
case TYPE_VOID:
return &ffi_type_void;
break;
case TYPE_FLOAT32:
return &ffi_type_float;
break;
case TYPE_INT32:
return &ffi_type_sint32;
break;
case TYPE_BOOLEAN:
return &ffi_type_uint8;
break;
case TYPE_STRING:
case TYPE_ARRAY:
return &ffi_type_pointer;
break;
default:
assert(0);
break;
}
}
ffi_type * function_ffi_rtype(c_function_def_t*method)
{
type_t type;
_parse_type(method->ret, &type);
log_dbg("[ffi] ret type: \"%s\" %s", method->ret, type_to_string(type));
return _type_to_ffi_type(type);
}
ffi_type ** function_ffi_args_plus_one(c_function_def_t*method)
{
int num_params = function_count_args(method) + 1;
ffi_type **atypes = malloc(sizeof(ffi_type*) * num_params);
const char*a = method->params;
atypes[0] = &ffi_type_pointer;
int i = 1;
while(*a) {
type_t type;
a += _parse_type(a, &type);
atypes[i] = _type_to_ffi_type(type);
i++;
}
assert(i == num_params);
return atypes;
}
function_signature_t* function_get_signature(c_function_def_t*f)
{
function_signature_t*sig = malloc(sizeof(function_signature_t));
const char*a;
int i;
a = f->params;
sig->num_params = 0;
while(*a) {
type_t type;
a += _parse_type(a, &type);
sig->num_params++;
}
sig->param = malloc(sizeof(type_t)*sig->num_params);
a = f->params;
i = 0;
while(*a) {
type_t type;
a += _parse_type(a, &sig->param[i++]);
}
_parse_type(f->ret, &sig->ret);
return sig;
}
void function_signature_dump(function_signature_t*sig)
{
printf("sig: (");
int i;
for(i=0;i<sig->num_params;i++) {
if(i>0)
printf(", ");
printf("%s", type_to_string(sig->param[i]));
}
printf("):%s\n", type_to_string(sig->ret));
}
void function_signature_destroy(function_signature_t*sig)
{
free(sig->param);
free(sig);
}
static const char* _ffi_arg_type(const ffi_type*t)
{
if(t == &ffi_type_void)
return "void";
else if(t == &ffi_type_void)
return "void";
else if(t == &ffi_type_uint8)
return "uint8";
else if(t == &ffi_type_sint8)
return "sint8";
else if(t == &ffi_type_uint16)
return "uint16";
else if(t == &ffi_type_sint16)
return "sint16";
else if(t == &ffi_type_uint32)
return "uint32";
else if(t == &ffi_type_sint32)
return "sint32";
else if(t == &ffi_type_uint64)
return "uint64";
else if(t == &ffi_type_sint64)
return "sint64";
else if(t == &ffi_type_float)
return "float";
else if(t == &ffi_type_double)
return "double";
else if(t == &ffi_type_pointer)
return "pointer";
else
return "<unknown>";
}
static void dump_ffi_call(const ffi_cif*cif)
{
printf("(");
int i;
for(i=0;i<cif->nargs;i++) {
if(i>0)
printf(", ");
printf("%s", _ffi_arg_type(cif->arg_types[i]));
}
printf("):%s\n", _ffi_arg_type(cif->rtype));
}
value_t* cfunction_call(value_t*self, value_t*_args)
{
c_function_def_t*f = self->internal;
function_signature_t*sig = function_get_signature(f);
ffi_cif cif;
ffi_type **atypes = function_ffi_args_plus_one(f);
ffi_status status = ffi_prep_cif(&cif, FFI_DEFAULT_ABI, sig->num_params + 1,
function_ffi_rtype(f),
atypes);
if(status != FFI_OK) {
return NULL;
}
if(_args->type != TYPE_ARRAY) {
language_error(f->runtime, "%s: function parameters must be an array\n", f->name);
return NULL;
}
if(sig->num_params != _args->length) {
language_error(f->runtime, "%s: wrong number of arguments: expected %d, got %d\n", f->name, sig->num_params, _args->length);
return NULL;
}
#ifdef DEBUG
printf("[ffi] ");function_signature_dump(sig);
printf("[ffi] args: ");value_dump(_args);printf("\n");
#endif
union {
int32_t i32;
float f32;
bool b;
void*ptr;
#define TMP_STR_SIZE 32
char tmp_str[TMP_STR_SIZE];
} args_data[_args->length+1], ret_raw;
void**ffi_args = alloca(sizeof(void*) * (_args->length + 1));
int i;
ffi_args[0] = &f->context;
for(i=0;i<_args->length;i++) {
value_t*o = _args->data[i];
type_t t = sig->param[i];
ffi_args[i+1] = &args_data[i+1];
bool error = false;
switch(_args->data[i]->type) {
case TYPE_FLOAT32: {
float v = o->f32;
if(t == TYPE_FLOAT32) {
args_data[i+1].f32 = v;
} else if(t == TYPE_INT32) {
args_data[i+1].i32 = (int)v;
} else if(t == TYPE_BOOLEAN) {
args_data[i+1].b = (int)v;
} else if(t == TYPE_STRING) {
char*str = args_data[i+i].tmp_str;
snprintf(str, TMP_STR_SIZE, "%f", v);
args_data[i+1].ptr = str;
} else {
error = true;
}
}
break;
case TYPE_INT32: {
int v = o->i32;
if(t == TYPE_FLOAT32) {
args_data[i+1].f32 = v;
} else if(t == TYPE_INT32) {
args_data[i+1].i32 = v;
} else if(t == TYPE_BOOLEAN) {
args_data[i+1].b = v;
} else if(t == TYPE_STRING) {
char*str = args_data[i+i].tmp_str;
snprintf(str, TMP_STR_SIZE, "%d", v);
args_data[i+1].ptr = str;
} else {
error = true;
}
}
break;
case TYPE_BOOLEAN: {
bool v = o->b;
if(t == TYPE_FLOAT32) {
args_data[i+1].f32 = v;
} else if(t == TYPE_INT32) {
args_data[i+1].i32 = v;
} else if(t == TYPE_BOOLEAN) {
args_data[i+1].b = v;
} else if(t == TYPE_STRING) {
args_data[i+1].ptr = v?"true":"false";
} else {
error = true;
}
}
break;
case TYPE_STRING: {
char* v = o->str;
if(t == TYPE_STRING) {
args_data[i+1].ptr = v;
} else {
error = true;
}
}
break;
case TYPE_VOID: {
if(t == TYPE_VOID) {
args_data[i+1].ptr = NULL;
} else {
error = true;
}
}
break;
case TYPE_ARRAY: {
value_t* v = o;
if(t == TYPE_ARRAY) {
args_data[i+1].ptr = v;
} else if(t == TYPE_STRING) {
char*str = args_data[i+i].tmp_str;
snprintf(str, TMP_STR_SIZE, "<array, %d items>", v->length);
args_data[i+1].ptr = str;
} else {
error = true;
}
}
break;
default: {
error = true;
}
break;
}
if(error) {
language_error(f->runtime, "%s: Can't convert parameter %d from %s to %s\n",
f->name,
i+1,
type_to_string(o->type),
type_to_string(t));
function_signature_destroy(sig);
free(atypes);
return NULL;
}
}
#ifdef DEBUG
printf("[ffi] call: "); dump_ffi_call(&cif);
#endif
ffi_call(&cif, f->call, &ret_raw, ffi_args);
free(atypes);
type_t ret_type = sig->ret;
value_t* ret = NULL;
switch(ret_type) {
case TYPE_VOID:
ret = value_new_void();
break;
case TYPE_FLOAT32:
ret = value_new_float32(ret_raw.f32);
break;
case TYPE_INT32:
ret = value_new_int32(ret_raw.i32);
break;
case TYPE_BOOLEAN:
ret = value_new_boolean(ret_raw.b);
break;
case TYPE_STRING:
ret = value_new_string(ret_raw.ptr);
break;
case TYPE_ARRAY:
ret = (value_t*)ret_raw.ptr;
break;
default:
assert(0);
break;
}
#ifdef DEBUG
printf("[ffi] call returning: "); value_dump(ret);
printf("\n");
#endif
function_signature_destroy(sig);
return ret;
}
void value_dump(value_t*v)
{
if(v == NULL) {
printf("NULL value (error)");
return;
}
switch(v->type) {
case TYPE_VOID:
printf("void");
break;
case TYPE_FLOAT32:
printf("(f32)%f", v->f32);
break;
case TYPE_INT32:
printf("(i32)%d", v->i32);
break;
case TYPE_BOOLEAN:
printf("(bool)%d", v->b);
break;
case TYPE_STRING:
printf("\"%s\"", v->str);
break;
case TYPE_ARRAY: {
int i;
printf("[");
for(i=0;i<v->length;i++) {
if(i>0)
printf(", ");
value_dump(v->data[i]);
}
printf("]");
}
break;
default: {
printf("type<%d>", v->type);
}
break;
}
}
void array_append(value_t*array, value_t* value)
{
assert(array->type == TYPE_ARRAY);
array_internal_t*internal = array->internal;
if(internal->size <= array->length) {
internal->size |= 3;
internal->size <<= 1;
internal->size += 1;
if(!array->data) {
array->data = malloc(internal->size * sizeof(void*));
} else {
array->data = realloc(array->data, internal->size * sizeof(void*));
}
}
array->data[array->length++] = value;
}
void array_append_int32(value_t*array, int32_t i32)
{
array_append(array, value_new_int32(i32));
}
void array_append_float32(value_t*array, float f32)
{
array_append(array, value_new_float32(f32));
}
void array_append_string(value_t*array, char* str)
{
array_append(array, value_new_string(str));
}
void array_append_boolean(value_t*array, bool b)
{
array_append(array, value_new_boolean(b));
}
void array_destroy(value_t*array)
{
assert(array->type == TYPE_ARRAY);
value_destroy(array);
}
void value_destroy(value_t*v)
{
if(v->destroy) {
v->destroy(v);
} else {
free(v);
}
}
static void value_destroy_simple(value_t*v)
{
free(v);
}
static void value_destroy_string(value_t*v)
{
free(v->str);
free(v);
}
static void value_destroy_array(value_t*v)
{
array_internal_t*internal = v->internal;
int i;
for(i=0;i<v->length;i++) {
value_destroy(v->data[i]);
v->data[i] = NULL;
}
free(v->data);
free(v->internal);
free(v);
}
static void value_destroy_cfunction(value_t*v)
{
c_function_def_t*f = (c_function_def_t*)v->internal;
free(f->params);
free(f->ret);
free(v->internal);
free(v);
}
value_t* value_new_int32(int32_t i32)
{
value_t*v = calloc(sizeof(value_t),1);
v->destroy = value_destroy_simple;
v->type = TYPE_INT32;
v->i32 = i32;
return v;
}
value_t* value_new_float32(float f32)
{
value_t*v = calloc(sizeof(value_t),1);
v->destroy = value_destroy_simple;
v->type = TYPE_FLOAT32;
v->f32 = f32;
return v;
}
value_t* value_new_boolean(bool b)
{
value_t*v = calloc(sizeof(value_t),1);
v->destroy = value_destroy_simple;
v->type = TYPE_BOOLEAN;
v->b = b;
return v;
}
value_t* value_new_string(const char* s)
{
value_t*v = calloc(sizeof(value_t),1);
v->destroy = value_destroy_string;
v->type = TYPE_STRING;
v->str = strdup(s);
return v;
}
value_t* value_new_void()
{
value_t*v = calloc(sizeof(value_t),1);
v->destroy = value_destroy_simple;
v->type = TYPE_VOID;
return v;
}
value_t* value_new_array()
{
value_t*v = calloc(sizeof(value_t),1);
v->type = TYPE_ARRAY;
v->internal = calloc(sizeof(array_internal_t),1);
v->destroy = value_destroy_array;
return v;
}
value_t* array_new()
{
return value_new_array();
}
value_t* value_new_cfunction(void*runtime, const char*name, fptr_t call, void*context, const char*params, const char*ret)
{
c_function_def_t*f = calloc(sizeof(c_function_def_t), 1);
f->runtime = runtime;
f->name = name;
f->call = call;
f->context = context;
f->params = (char*)strdup(params);
f->ret = (char*)strdup(ret);
value_t*v = calloc(sizeof(value_t),1);
v->destroy = value_destroy_cfunction;
v->type = TYPE_FUNCTION;
v->internal = f;
v->call = cfunction_call;
v->num_params = function_count_args(f);
return v;
}
int value_to_int(value_t*v)
{
switch(v->type) {
case TYPE_VOID:
return 0;
break;
case TYPE_FLOAT32:
return (int)v->f32;
break;
case TYPE_INT32:
return v->i32;
break;
case TYPE_BOOLEAN:
return v->b;
break;
case TYPE_STRING:
return atoi(v->str);
break;
default:
return -1;
}
}