Debian Codegraph @

src/ikarus-ffi.c

raw
1
2 #include "ikarus-data.h"
3 #include "config.h"
4 #include <sys/errno.h>
5
6 #if ENABLE_LIBFFI
7 #include <ffi.h>
8 #include <stdlib.h>
9 #include <strings.h>
10
11 #undef DEBUG_FFI
12
13 #ifdef HACK_FFI
14 #include <sys/mman.h>
15 #endif
16
17 static void*
18 alloc(size_t n, int m) {
19 void* x = calloc(n, m);
20 if (x == NULL) {
21 fprintf(stderr, "ERROR (ikarus): calloc failed!\n");
22 exit(-1);
23 }
24 return x;
25 }
26
27 static ffi_type* scheme_to_ffi_type_cast(ikptr nptr);
28
29 static ffi_type*
30 scheme_to_ffi_record_type_cast(ikptr vec){
31 ikptr lenptr = ref(vec, -vector_tag);
32 if (! is_fixnum(lenptr)) {
33 fprintf(stderr, "NOT A VECTOR 0x%016lx\n", vec);
34 exit(-1);
35 }
36 long n = unfix(lenptr);
37 ffi_type* t = alloc(sizeof(ffi_type), 1);
38 ffi_type** ts = alloc(sizeof(ffi_type*), n+1);
39 t->size = 0;
40 t->alignment = 0;
41 t->type = FFI_TYPE_STRUCT;
42 t->elements = ts;
43 long i;
44 for(i=0; i<n; i++){
45 ts[i] = scheme_to_ffi_type_cast(ref(vec, off_vector_data + i*wordsize));
46 }
47 ts[n] = 0;
48 return t;
49 }
50
51 static ffi_type*
52 scheme_to_ffi_type_cast(ikptr nptr){
53 if (tagof(nptr) == vector_tag) {
54 return scheme_to_ffi_record_type_cast(nptr);
55 } else if (is_fixnum(nptr)) {
56 long n = unfix(nptr);
57 switch (n & 0xF) {
58 case 1: return &ffi_type_void;
59 case 2: return &ffi_type_uint8;
60 case 3: return &ffi_type_sint8;
61 case 4: return &ffi_type_uint16;
62 case 5: return &ffi_type_sint16;
63 case 6: return &ffi_type_uint32;
64 case 7: return &ffi_type_sint32;
65 case 8: return (sizeof(long)==4)?&ffi_type_uint32:&ffi_type_uint64;
66 case 9: return (sizeof(long)==4)?&ffi_type_sint32:&ffi_type_sint64;
67 case 10: return &ffi_type_uint64;
68 case 11: return &ffi_type_sint64;
69 case 12: return &ffi_type_float;
70 case 13: return &ffi_type_double;
71 case 14: return &ffi_type_pointer;
72 default:
73 fprintf(stderr, "INVALID ARG %ld", n);
74 exit(-1);
75 }
76 } else {
77 fprintf(stderr, "INVALID ARG %ld", nptr);
78 exit(-1);
79 }
80 }
81
82 static void*
83 alloc_room_for_type(ffi_type* t){
84 return alloc(t->size, 1);
85 }
86
87 extern long extract_num(ikptr x);
88 extern long long extract_num_longlong(ikptr x);
89 extern ikptr sll_to_number(signed long long n, ikpcb* pcb);
90 extern ikptr ull_to_number(unsigned long long n, ikpcb* pcb);
91
92 static void scheme_to_ffi_value_cast(ffi_type*, ikptr, ikptr, void*);
93
94 static void
95 scheme_to_ffi_record_value_cast(ffi_type* t, ikptr nptr, ikptr p, void* r) {
96 if (t->type != FFI_TYPE_STRUCT) {
97 fprintf(stderr, "not a struct type\n");
98 exit(-1);
99 }
100 ffi_type** ts = t->elements;
101 char* buf = r;
102 ikptr lenptr = ref(nptr, off_vector_length);
103 int n = unfix(lenptr);
104 int i;
105 for(i=0; i<n; i++) {
106 ffi_type* at = ts[i];
107 ikptr argt = ref(nptr, off_vector_data + i*wordsize);
108 ikptr arg = ref(p, off_vector_data + i*wordsize);
109 scheme_to_ffi_value_cast(at, argt, arg, buf);
110 buf += at->size;
111 }
112 }
113
114 static void
115 scheme_to_ffi_value_cast(ffi_type* t, ikptr nptr, ikptr p, void* r) {
116 if (tagof(nptr) == vector_tag) {
117 scheme_to_ffi_record_value_cast(t, nptr, p, r);
118 } else if (is_fixnum(nptr)) {
119 long n = unfix(nptr);
120 switch (n & 0xF) {
121 case 1: { return; }
122 case 2: // ffi_type_uint8;
123 case 3:
124 { *((char*)r) = extract_num(p); return; }
125 case 4: // ffi_type_uint16;
126 case 5:
127 { *((short*)r) = extract_num(p); return; }
128 case 6: // ffi_type_uint32;
129 case 7:
130 { *((int*)r) = extract_num(p); return; }
131 case 8: // ffi_type_uint64;
132 case 9:
133 { *((long*)r) = extract_num(p); return; }
134 case 10:
135 case 11:
136 { *((long long*)r) = extract_num_longlong(p); return; }
137 case 12: //return &ffi_type_float;
138 { *((float*)r) = flonum_data(p); return; }
139 case 13: //return &ffi_type_double;
140 { *((double*)r) = flonum_data(p); return; }
141 case 14: //return &ffi_type_pointer;
142 { *((void**)r) = (void*)ref(p, off_pointer_data); return; }
143 default:
144 fprintf(stderr, "INVALID ARG %ld", n);
145 exit(-1);
146 }
147 } else {
148 fprintf(stderr, "INVALID TYPE 0x%016lx\n", nptr);
149 exit(-1);
150 }
151 }
152
153
154 static ikptr
155 ffi_to_scheme_value_cast(int n, void* p, ikpcb* pcb) {
156 switch (n & 0xF) {
157 case 1: return void_object;
158 case 2: return u_to_number(*((unsigned char*)p), pcb);
159 case 3: return s_to_number(*((signed char*)p), pcb);
160 case 4: return u_to_number(*((unsigned short*)p), pcb);
161 case 5: return s_to_number(*((signed short*)p), pcb);
162 case 6: return u_to_number(*((unsigned int*)p), pcb);
163 case 7: return s_to_number(*((signed int*)p), pcb);
164 case 8: return u_to_number(*((unsigned long*)p), pcb);
165 case 9: return s_to_number(*((signed long*)p), pcb);
166 case 10: return ull_to_number(*((unsigned long long*)p), pcb);
167 case 11: return sll_to_number(*((signed long long*)p), pcb);
168 case 12: return d_to_number(*((float*)p), pcb);
169 case 13: return d_to_number(*((double*)p), pcb);
170 case 14: return make_pointer((long)*((void**)p), pcb);
171 default:
172 fprintf(stderr, "INVALID ARG %d", n);
173 exit(-1);
174 }
175 }
176
177 ikptr
178 ikrt_ffi_prep_cif(ikptr rtptr, ikptr argstptr, ikpcb* pcb) {
179 ffi_cif* cif = alloc(sizeof(ffi_cif), 1);
180 ffi_abi abi = FFI_DEFAULT_ABI;
181 int nargs = unfix(ref(argstptr, off_vector_length));
182 ffi_type** argtypes = alloc(sizeof(ffi_type*), nargs+1);
183 int i;
184 for(i=0; i<nargs; i++){
185 ikptr argt = ref(argstptr, off_vector_data + i*wordsize);
186 argtypes[i] = scheme_to_ffi_type_cast(argt);
187 }
188 argtypes[nargs] = NULL;
189 ffi_type* rtype = scheme_to_ffi_type_cast(rtptr);
190 ffi_status s = ffi_prep_cif(cif, abi, nargs, rtype, argtypes);
191 if (s == FFI_OK) {
192 ikptr r = ik_safe_alloc(pcb, pointer_size);
193 ref(r, 0) = pointer_tag;
194 ref(r, wordsize) = (ikptr)cif;
195 return r + vector_tag;
196 } else {
197 return false_object;
198 }
199 }
200
201
202 #ifdef DEBUG_FFI
203 static void
204 dump_stack(ikpcb* pcb, char* msg) {
205 fprintf(stderr, "==================== %s\n", msg);
206 ikptr frame_base = pcb->frame_base;
207 ikptr frame_pointer = pcb->frame_pointer;
208 ikptr p = frame_pointer;
209 fprintf(stderr, "fp=0x%016lx base=0x%016lx\n", frame_pointer, frame_base);
210 while(p < frame_base) {
211 fprintf(stderr, "*0x%016lx = 0x%016lx\n", p, ref(p, 0));
212 p += wordsize;
213 }
214 }
215 #endif
216
217
218 /* FIXME: handle stack overflow */
219 ikptr
220 ikrt_seal_scheme_stack(ikpcb* pcb) {
221 #if 0
222 | |
223 | |
224 | |
225 | |
226 +--------------+
227 | underflow | <--------- new frame pointer
228 +--------------+
229 | return point | <--------- old frame pointer, new frame base
230 +--------------+
231 | . |
232 | . |
233 | . |
234 | |
235 +--------------+
236 | underflow | <--------- old frame base
237 +--------------+
238 #endif
239 ikptr frame_base = pcb->frame_base;
240 ikptr frame_pointer = pcb->frame_pointer;
241 #ifdef DEBUG_FFI
242 dump_stack(pcb, "BEFORE SEALING");
243 fprintf(stderr, "old base=0x%016lx fp=0x%016lx\n", pcb->frame_base,
244 pcb->frame_pointer);
245 #endif
246 if ((frame_base - wordsize) != frame_pointer) {
247 ikptr underflow_handler = ref(frame_base, -wordsize);
248 cont* k = (cont*) pcb->next_k;
249 cont* nk = (cont*) ik_unsafe_alloc(pcb, sizeof(cont));
250 nk->tag = continuation_tag;
251 nk->next = (ikptr) k;
252 nk->top = frame_pointer;
253 #ifdef DEBUG_FFI
254 fprintf(stderr, "rp=0x%016lx\n",
255 ref(frame_pointer, 0));
256 #endif
257 nk->size = frame_base - frame_pointer - wordsize;
258 #ifdef DEBUG_FFI
259 fprintf(stderr, "frame size=%ld\n", nk->size);
260 #endif
261 pcb->next_k = vector_tag + (ikptr)nk;
262 pcb->frame_base = frame_pointer;
263 pcb->frame_pointer = pcb->frame_base - wordsize;
264 #ifdef DEBUG_FFI
265 fprintf(stderr, "new base=0x%016lx fp=0x%016lx\n", pcb->frame_base,
266 pcb->frame_pointer);
267 fprintf(stderr, "uf=0x%016lx\n", underflow_handler);
268 #endif
269 ref(pcb->frame_pointer, 0) = underflow_handler;
270 } else {
271 #ifdef DEBUG_FFI
272 fprintf(stderr, "already sealed\n");
273 #endif
274 }
275 #ifdef DEBUG_FFI
276 dump_stack(pcb, "AFTER SEALING");
277 #endif
278 return void_object;
279 }
280
281 ikptr
282 ikrt_call_back(ikptr proc, ikpcb* pcb) {
283 ikrt_seal_scheme_stack(pcb);
284
285 ikptr sk = ik_unsafe_alloc(pcb, system_continuation_size);
286 ref(sk, 0) = system_continuation_tag;
287 ref(sk, disp_system_continuation_top) = pcb->system_stack;
288 ref(sk, disp_system_continuation_next) = pcb->next_k;
289 pcb->next_k = sk + vector_tag;
290 ikptr entry_point = ref(proc, off_closure_code);
291 #ifdef DEBUG_FFI
292 fprintf(stderr, "system_stack = 0x%016lx\n", pcb->system_stack);
293 #endif
294 ikptr code_ptr = entry_point - off_code_data;
295 pcb->frame_pointer = pcb->frame_base;
296 ikptr rv = ik_exec_code(pcb, code_ptr, 0, proc);
297 #ifdef DEBUG_FFI
298 fprintf(stderr, "system_stack = 0x%016lx\n", pcb->system_stack);
299 #endif
300 #ifdef DEBUG_FFI
301 fprintf(stderr, "rv=0x%016lx\n", rv);
302 #endif
303 sk = pcb->next_k - vector_tag;
304 if (ref(sk, 0) != system_continuation_tag) {
305 fprintf(stderr, "ikarus internal error: invalid system cont\n");
306 exit(-1);
307 }
308 pcb->next_k = ref(sk, disp_system_continuation_next);
309 ref(sk, disp_system_continuation_next) = pcb->next_k;
310 pcb->system_stack = ref(sk, disp_system_continuation_top);
311 pcb->frame_pointer = pcb->frame_base - wordsize;
312 #ifdef DEBUG_FFI
313 fprintf(stderr, "rp=0x%016lx\n", ref(pcb->frame_pointer, 0));
314 #endif
315 return rv;
316 }
317
318
319
320 ikptr
321 ikrt_ffi_call(ikptr data, ikptr argsvec, ikpcb* pcb) {
322
323 ikrt_seal_scheme_stack(pcb);
324 ikptr sk = ik_unsafe_alloc(pcb, system_continuation_size);
325 ref(sk, 0) = system_continuation_tag;
326 ref(sk, disp_system_continuation_top) = pcb->system_stack;
327 ref(sk, disp_system_continuation_next) = pcb->next_k;
328 pcb->next_k = sk + vector_tag;
329
330
331 ikptr cifptr = ref(data, off_vector_data + 0 * wordsize);
332 ikptr funptr = ref(data, off_vector_data + 1 * wordsize);
333 ikptr typevec = ref(data, off_vector_data + 2 * wordsize);
334 ikptr rtype = ref(data, off_vector_data + 3 * wordsize);
335 ffi_cif* cif = (ffi_cif*) ref(cifptr, off_pointer_data);
336 void(*fn)() = (void (*)()) ref(funptr, off_pointer_data);
337 int n = unfix(ref(argsvec, off_vector_length));
338 void** avalues = alloc(sizeof(void*), n+1);
339 int i;
340 for(i=0; i<n; i++){
341 ffi_type* t = cif->arg_types[i];
342 ikptr at = ref(typevec, off_vector_data + i * wordsize);
343 ikptr v = ref(argsvec, off_vector_data + i * wordsize);
344 void* p = alloc_room_for_type(t);
345 avalues[i] = p;
346 scheme_to_ffi_value_cast(t, at, v, p);
347 }
348 avalues[n] = NULL;
349 void* rvalue = alloc_room_for_type(cif->rtype);
350 ffi_call(cif, fn, rvalue, avalues);
351 pcb->last_errno = errno;
352 ikptr val = ffi_to_scheme_value_cast(unfix(rtype), rvalue, pcb);
353 for(i=0; i<n; i++){
354 free(avalues[i]);
355 }
356 #ifdef DEBUG_FFI
357 fprintf(stderr, "DONE WITH CALL, RV=0x%016lx\n", (long)val);
358 #endif
359 free(avalues);
360 free(rvalue);
361
362 pcb->frame_pointer = pcb->frame_base - wordsize;
363
364 sk = pcb->next_k - vector_tag;
365 if (ref(sk, 0) != system_continuation_tag) {
366 fprintf(stderr, "ikarus internal error: invalid system cont\n");
367 exit(-1);
368 }
369 pcb->next_k = ref(sk, disp_system_continuation_next);
370 pcb->system_stack = ref(sk, disp_system_continuation_top);
371
372 return val;
373 }
374
375
376 ikptr ikrt_has_ffi(/*ikpcb* pcb*/){
377 return true_object;
378 }
379
380 /*
381
382 ffi_status ffi_prep_cif (
383 ffi_cif *cif,
384 ffi_abi abi,
385 unsigned int nargs,
386 ffi_type *rtype,
387 ffi_type **argtypes)
388
389 void *ffi_closure_alloc (size_t size, void **code)
390
391 void ffi_closure_free (void *writable)
392
393 ffi_status ffi_prep_closure_loc (
394 ffi_closure *closure,
395 ffi_cif *cif,
396 void (*fun) (ffi_cif *cif, void *ret, void **args, void *user_data),
397 void *user_data,
398 void *codeloc)
399
400 */
401
402 extern ikpcb* the_pcb;
403 static void
404 generic_callback(ffi_cif *cif, void *ret, void **args, void *user_data){
405 /* convert args according to cif to scheme values */
406 /* call into scheme, get the return value */
407 /* convert the return value to C */
408 /* put the C return value in *ret */
409 /* done */
410 ikptr data = ((callback_locative*)user_data)->data;
411 ikptr proc = ref(data, off_vector_data + 1 * wordsize);
412 ikptr argtypes_conv = ref(data, off_vector_data + 2 * wordsize);
413 ikptr rtype_conv = ref(data, off_vector_data + 3 * wordsize);
414 int n = unfix(ref(argtypes_conv, off_vector_length));
415
416 ikpcb* pcb = the_pcb;
417 ikptr code_entry = ref(proc, off_closure_code);
418 ikptr code_ptr = code_entry - off_code_data;
419
420 pcb->frame_pointer = pcb->frame_base;
421 int i;
422 for(i = 0; i < n; i++){
423 ikptr argt = ref(argtypes_conv, off_vector_data + i*wordsize);
424 void* argp = args[i];
425 ref(pcb->frame_pointer, -2*wordsize - i*wordsize) =
426 ffi_to_scheme_value_cast(unfix(argt), argp, pcb);
427 }
428 ikptr rv = ik_exec_code(pcb, code_ptr, fix(-n), proc);
429 #ifdef DEBUG_FFI
430 fprintf(stderr, "and back with rv=0x%016lx!\n", rv);
431 #endif
432 scheme_to_ffi_value_cast(cif->rtype, rtype_conv, rv, ret);
433 return;
434 }
435
436 ikptr
437 ikrt_prepare_callback(ikptr data, ikpcb* pcb){
438 #if FFI_CLOSURES
439 ikptr cifptr = ref(data, off_vector_data + 0 * wordsize);
440 void* codeloc;
441 ffi_closure* closure = ffi_closure_alloc(sizeof(ffi_closure), &codeloc);
442
443 #ifdef HACK_FFI
444 {
445 long code_start = align_to_prev_page(codeloc);
446 long code_end =
447 align_to_next_page(FFI_TRAMPOLINE_SIZE+(-1)+(long)codeloc);
448 int rv = mprotect((void*)code_start, code_end - code_start,
449 PROT_READ|PROT_WRITE|PROT_EXEC);
450 if(rv) {
451 fprintf(stderr, "Error mprotecting code page!\n");
452 }
453 }
454 #endif
455
456 ffi_cif* cif = (ffi_cif*) ref(cifptr, off_pointer_data);
457
458 callback_locative* loc = malloc(sizeof(callback_locative));
459 if(!loc) {
460 fprintf(stderr, "ERROR: ikarus malloc error\n");
461 exit(-1);
462 }
463
464 ffi_status st =
465 ffi_prep_closure_loc(closure, cif, generic_callback, loc, codeloc);
466
467 if (st != FFI_OK) {
468 free(loc);
469 return false_object;
470 }
471
472 loc->data = data;
473 loc->next = pcb->callbacks;
474 pcb->callbacks = loc;
475
476 ikptr p = ik_safe_alloc(pcb, pointer_size);
477 ref(p, 0) = pointer_tag;
478 ref(p, wordsize) = (ikptr) codeloc;
479 return p+vector_tag;
480 #else
481 return false_object
482 #endif
483 }
484
485 int ho (int(*f)(int), int n) {
486 // fprintf(stderr, "HO HO 0x%016lx!\n", (long)f);
487 int n0 = f(n);
488 // fprintf(stderr, "GOT N0\n");
489 return n0 + f(n);
490 }
491
492
493 int ho2 (ikptr fptr, ikptr nptr) {
494 int (*f)(int) = (int(*)(int)) ref(fptr, off_pointer_data);
495 int n = unfix(nptr);
496 // fprintf(stderr, "HO2 HO2 0x%016lx!\n", (long)f);
497 int n0 = f(n);
498 // fprintf(stderr, "GOT N0\n");
499 return n0 + f(n);
500 }
501
502
503 int test_I_I (int(*f)(int), int n0) {
504 return f(n0);
505 }
506
507 int test_I_II (int(*f)(int,int), int n0, int n1) {
508 return f(n0,n1);
509 }
510
511 int test_I_III (int(*f)(int,int,int), int n0, int n1, int n2) {
512 return f(n0,n1,n2);
513 }
514
515 int add_I_I(int n0) {
516 return n0;
517 }
518 int add_I_II(int n0, int n1) {
519 return n0+n1;
520 }
521 int add_I_III(int n0, int n1, int n2) {
522 return n0+n1+n2;
523 }
524
525
526
527 struct Point{
528 float x;
529 float y;
530 };
531
532 struct Rect{
533 struct Point tl;
534 struct Point br;
535 };
536
537 float test_area_F_R(struct Rect r) {
538 float dx = r.br.x - r.tl.x;
539 float dy = r.br.y - r.tl.y;
540 return dx * dy;
541 }
542
543 double test_D_D (double(*f)(double), double n0) {
544 return f(n0);
545 }
546
547 double test_D_DD (double(*f)(double,double), double n0, double n1) {
548 return f(n0,n1);
549 }
550
551 double test_D_DDD (double(*f)(double,double,double), double n0, double n1, double n2) {
552 return f(n0,n1,n2);
553 }
554
555 double add_D_D(double n0) {
556 return n0;
557 }
558 double add_D_DD(double n0, double n1) {
559 return n0+n1;
560 }
561 double add_D_DDD(double n0, double n1, double n2) {
562 return n0+n1+n2;
563 }
564
565
566
567
568
569 int cadd1 (int n) {
570 return n+1;
571 }
572
573 void hello_world(int n) {
574 while(n > 0) {
575 fprintf(stderr, "Hello World\n");
576 n--;
577 }
578 }
579
580 #else
581 ikptr ikrt_ffi_prep_cif() { return false_object; }
582 ikptr ikrt_ffi_call() { return false_object; }
583 ikptr ikrt_prepare_callback() { return false_object; }
584 ikptr ikrt_has_ffi() { return false_object; }
585
586
587 #endif
588
589
590
591
592