summaryrefslogtreecommitdiff
path: root/foreign.c
blob: 462195a56a2c1c86933d14e3ad97772351394aa2 (plain)
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
/* WARNING: In this file, you will find crazy macros and dubious usage of avcall. Beware! */

#if COMPILE_TIME_FOREIGN_FN_SUPPORT
#if CHAR_BIT != 8
#error "Compile-time foreign functions can only be used on systems where CHAR_BIT is 8."
#endif

/* avcall has some sign conversion problems on BSD */
/* (the macros it defines cause problems too, which is why this is ignored for so long) */

#ifdef __clang__
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wsign-conversion"
#endif
#include <avcall.h>
#include <dlfcn.h>


#if SCHAR_MAX != 127
#error "Foreign function support requires an 8-bit signed type."
#else
#define av_type_with_limit_127 schar
#endif

#if UCHAR_MAX != 255
#error "Foreign function support requires an 8-bit unsigned type."
#else
#define av_type_with_limit_255 uchar
#endif

#if SHRT_MAX != 32767
#error "Foreign function support requires a 16-bit signed type."
#else
#define av_type_with_limit_32767 short
#endif

#if USHRT_MAX != 65535
#error "Foreign function support requires an 16-bit unsigned type."
#else
#define av_type_with_limit_65535 ushort
#endif

#if INT_MAX == 2147483647
#define av_type_with_limit_2147483647 int
#elif LONG_MAX == 2147483647
#define av_type_with_limit_2147483647 long
#else
#error "Foreign function support requires an 32-bit signed type."
#endif

#if UINT_MAX == 4294967295
#define av_type_with_limit_4294967295 uint
#elif ULONG_MAX == 4294967295
#define av_type_with_limit_4294967295 ulong
#else
#error "Foreign function support requires an 32-bit unsigned type."
#endif

#if LONG_MAX == 9223372036854775807
#define av_type_with_limit_9223372036854775807 long
#elif LLONG_MAX == 9223372036854775807
#define av_type_with_limit_9223372036854775807 longlong
#else
#error "Foreign function support requires a 64-bit signed type."
#endif

#if ULONG_MAX == 18446744073709551615ULL
#define av_type_with_limit_18446744073709551615 ulong
#elif ULLONG_MAX == 18446744073709551615ULL
#define av_type_with_limit_18446744073709551615 ulonglong
#else
#error "Foreign function support requires a 64-bit unsigned type."
#endif



#define av_type_with_limit(x) join(av_type_with_limit_, x)

#define toc_av_start(limit) join(av_start_, av_type_with_limit(limit))
#define toc_av_add(limit) join(av_, av_type_with_limit(limit))

#define toc_av_start_f32 av_start_float
#define toc_av_start_f64 av_start_double
#define toc_av_f32 av_float
#define toc_av_f64 av_double

static bool arg_list_start(av_alist *arg_list, void (*fn)(), Value *return_val, Type *return_type, Location where) {
	switch (return_type->kind) {
	case TYPE_UNKNOWN:
		err_print(where, "Cannot call foreign function with unknown return type.");
		return false;
	case TYPE_TUPLE:
		err_print(where, "Cannot call foreign function with tuple return type.");
		return false;
	case TYPE_ARR:
		err_print(where, "Foreign functions cannot return arrays.");
		return false;
	case TYPE_FN:
		warn_print(where, "Foreign function returns function pointer. If it returns a C-style function pointer, it won't be called properly by toc.");
		av_start_ptr(*arg_list, fn, FnExpr *, &return_val->fn);
		break;
	case TYPE_VOID:
		av_start_void(*arg_list, fn);
		break;
	case TYPE_PTR:
		av_start_ptr(*arg_list, fn, void *, &return_val->ptr);
		break;
	case TYPE_BUILTIN:
		switch (return_type->builtin) {
		case BUILTIN_I8:
			toc_av_start(127)(*arg_list, fn, &return_val->i8);
			break;
		case BUILTIN_U8:
			toc_av_start(255)(*arg_list, fn, &return_val->u8);
			break;
		case BUILTIN_I16:
			toc_av_start(32767)(*arg_list, fn, &return_val->i16);
			break;
		case BUILTIN_U16:
			toc_av_start(65535)(*arg_list, fn, &return_val->u16);
			break;
		case BUILTIN_I32:
			toc_av_start(2147483647)(*arg_list, fn, &return_val->i32);
			break;
		case BUILTIN_U32:
			toc_av_start(4294967295)(*arg_list, fn, &return_val->u32);
			break;
		case BUILTIN_I64:
			toc_av_start(9223372036854775807)(*arg_list, fn, &return_val->i64);
			break;
		case BUILTIN_U64:
			toc_av_start(18446744073709551615)(*arg_list, fn, &return_val->u64);
			break;
		case BUILTIN_BOOL:
			/* bool is probably just unsigned char.... hopefully... */
			av_start_uchar(*arg_list, fn, &return_val->u8);
			break;
		case BUILTIN_CHAR:
			av_start_char(*arg_list, fn, &return_val->charv);
			break;
		case BUILTIN_F32:
			toc_av_start_f32(*arg_list, fn, &return_val->f32);
			break;
		case BUILTIN_F64:
			toc_av_start_f64(*arg_list, fn, &return_val->f64);
			break;
		case BUILTIN_TYPE:
			av_start_ptr(*arg_list, fn, Type *, &return_val->type);
			break;
		case BUILTIN_NMS:
			av_start_ptr(*arg_list, fn, Namespace *, &return_val->nms);
			break;
		case BUILTIN_VARARGS:
			assert(0);
			break;
		}
		break;
	case TYPE_STRUCT: {
		size_t struct_size = compiler_sizeof(return_type);
		StructDef *struc = return_type->struc;
		return_val->struc = err_calloc(1, struct_size);
		bool splittable;
		/* hopefully this is right! */
		if (struct_size <= sizeof(long)) {
			splittable = true;
		} else if (struct_size > 2*sizeof(long)) {
			splittable = false;
		} else if (arr_len(struc->fields) > 4) {
			splittable = false;
		} else {
			/* NOTE: this warning is not because splittable is being computed incorrectly! it doesn't handle it right with *either* splittable = 0 or splittable = 1 */
			warn_print(where, "Dynamically calling function which returns a struct. avcall seems to not handle structs of size ~2*sizeof(long) correctly.");
			splittable = true;
			size_t word_size = sizeof(__avword);
			arr_foreach(struc->fields, Field, f) {
				if (f->offset / word_size != (f->offset + compiler_sizeof(&f->type) - 1) / word_size) {
					splittable = false;
					break;
				}
			}
		}
		_av_start_struct(*arg_list, fn, struct_size, splittable, return_val->struc);
	} break;
	case TYPE_SLICE:
#if defined(__GNUC__) && !defined(__clang__)
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wsign-conversion"
#endif	
		av_start_struct(*arg_list, fn, Slice, av_word_splittable_2(I64, void *), &return_val->slice);
#if defined(__GNUC__) && !defined(__clang__)
#pragma GCC diagnostic pop
#endif
		break;
	case TYPE_EXPR:
		assert(0);
		return false;
	}
	return true;
}

static bool arg_list_add(av_alist *arg_list, Value val, Type *type, Location where) {
	switch (type->kind) {
	case TYPE_VOID:
	case TYPE_TUPLE:
	case TYPE_UNKNOWN:
	case TYPE_ARR: { /* TODO: maybe just pass pointer for arr? */
		char *s = type_to_str(type);
		err_print(where, "Cannot pass type %s to foreign function.", s);
		free(s);
		return false;
	}
	case TYPE_PTR:
		av_ptr(*arg_list, void *, val.ptr);
		break;
	case TYPE_FN:
		warn_print(where, "Passing toc function pointer to foreign function. This will not work if the function expects a C-style function pointer.");
		av_ptr(*arg_list, FnExpr *, val.fn);
		break;
	case TYPE_BUILTIN:
		switch (type->builtin) {
		case BUILTIN_I8:
			toc_av_add(127)(*arg_list, val.i8);
			break;
		case BUILTIN_U8:
			toc_av_add(255)(*arg_list, val.u8);
			break;
		case BUILTIN_I16:
			toc_av_add(32767)(*arg_list, val.i16);
			break;
		case BUILTIN_U16:
			toc_av_add(65535)(*arg_list, val.u16);
			break;
		case BUILTIN_I32:
			toc_av_add(2147483647)(*arg_list, val.i32);
			break;
		case BUILTIN_U32:
			toc_av_add(4294967295)(*arg_list, val.u32);
			break;
		case BUILTIN_I64:
			toc_av_add(9223372036854775807)(*arg_list, val.i64);
			break;
		case BUILTIN_U64:
			toc_av_add(18446744073709551615)(*arg_list, val.u64);
			break;
		case BUILTIN_CHAR:
			av_char(*arg_list, val.charv);
			break;
		case BUILTIN_BOOL:
			av_uchar(*arg_list, val.boolv);
			break;
		case BUILTIN_F32:
			toc_av_f32(*arg_list, val.f32);
			break;
		case BUILTIN_F64:
			toc_av_f64(*arg_list, val.f64);
			break;
		case BUILTIN_TYPE:
			av_ptr(*arg_list, Type *, val.type);
			break;
		case BUILTIN_NMS:
			av_ptr(*arg_list, Namespace *, val.nms);
			break;
		case BUILTIN_VARARGS:
			arr_foreach(val.varargs, VarArg, arg) {
			    arg_list_add(arg_list, arg->val, arg->type, where);
			}
			break;
		}
		break;
	case TYPE_SLICE:
		av_struct(*arg_list, Slice, val.slice);
		break;
	case TYPE_STRUCT:
		_av_struct(*arg_list, compiler_sizeof(type), compiler_alignof(type), val.struc);
		break;
	case TYPE_EXPR:
		assert(0);
		return false;
	}
	return true;
}

#ifdef __clang__
#pragma clang diagnostic pop
#endif

static void ffmgr_create(ForeignFnManager *ffmgr, Allocator *allocr) {
	str_hash_table_create(&ffmgr->libs_loaded, sizeof(Library), allocr);
}

static bool foreign_call(ForeignFnManager *ffmgr, FnExpr *fn, Type *fn_type, Value *args, Location call_where, Value *ret) {
	void (*fn_ptr)() = fn->foreign.fn_ptr;
	if (!fn_ptr) {
		assert(fn->flags & FN_EXPR_FOREIGN);
		const char *libname = fn->foreign.lib;
		if (!libname) {
			err_print(call_where, "Attempt to call function at compile time which does not have an associated library.");
			info_print(fn->where, "Function was declared here.");
			return false;
		}
		Library *lib = str_hash_table_get(&ffmgr->libs_loaded, libname, strlen(libname));
		if (!lib) {
			void *handle = dlopen(libname, RTLD_LAZY);
			if (!handle) {
				err_print(call_where, "Could not open dynamic library: %s.", libname);
				return false;
			}
			lib = str_hash_table_insert(&ffmgr->libs_loaded, libname, strlen(libname));
			lib->handle = handle;
		}
		const char *name = fn->foreign.name;
#ifdef __GNUC__
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wpedantic"
#endif
		fn_ptr = dlsym(lib->handle, name);
#ifdef __GNUC__
#pragma GCC diagnostic pop
#endif
		
		if (!fn_ptr) {
			err_print(call_where, "Could not get function from dynamic library: %s.", name);
			return false;
		}
		fn->foreign.fn_ptr = fn_ptr;		
	}

	av_alist arg_list;
	if (!arg_list_start(&arg_list, fn_ptr, ret, &fn_type->fn.types[0], call_where))
		return false;
	size_t nparams = arr_len(fn_type->fn.types)-1;
	for (size_t i = 0; i < nparams; ++i) {
		if (!arg_list_add(&arg_list, args[i], &fn_type->fn.types[i+1], call_where))
			return false;
	}
	av_call(arg_list);
	
	(void)fn_type; (void)args;	
	return true;
}

#else
static void ffmgr_create(ForeignFnManager *ffmgr, Allocator *allocr) {
	(void)ffmgr;
	(void)allocr;
}

static bool foreign_call(ForeignFnManager *ffmgr, FnExpr *fn, Type *fn_type, Value *args, Location call_where, Value *ret) {
	(void)ffmgr; (void)fn; (void)fn_type; (void)args; (void)ret;
	err_print(call_where, "You have not compiled toc with compile time foreign function support.");
	return false;
}

#endif