ffi_arm.d 19.8 KB
Newer Older
1
2
/* -*- mode: c -*- */
/*
3
    ffi_arm.c -- Nonportable component of the FFI
4
5
6
*/
/*
    Copyright (c) 2005, Juan Jose Garcia Ripoll.
7
    Copyright (c) 2020, Jean-Claude Beaudoin.
8
9
10
11
12
13
14
15
16
17
18
19
20
21

    MKCL is free software; you can redistribute it and/or
    modify it under the terms of the GNU Lesser General Public
    License as published by the Free Software Foundation; either
    version 3 of the License, or (at your option) any later version.

    See file '../../../Copyright' for full details.
*/

#include <mkcl/mkcl.h>
#include <mkcl/internal.h>
#include <string.h>
#include <sys/mman.h>

22
23
24
25
26
27
28
29
30
31
32

#define MAX_CORE_REGISTERS 4
#define MAX_VFP_REGISTERS 16

struct mkcl_fficall_reg {
  long r[MAX_CORE_REGISTERS];
  int core_register_count;
  union {
    float s[MAX_VFP_REGISTERS];
    double d[MAX_VFP_REGISTERS/2];
  } vfp;
33
34
  bool vfp_register_allocated[MAX_VFP_REGISTERS];
  int vfp_register_least_unallocated;
35
36
37
38
39
40
41
42
43
};

struct mkcl_fficall_reg *
mkcl_fficall_prepare_extra(MKCL, struct mkcl_fficall_reg *registers)
{
  if (registers == 0) {
    registers = mkcl_alloc(env, sizeof(*registers));
  }
  registers->core_register_count = 0;
44
45
46
47
48
  registers->vfp_register_least_unallocated = 0;
  {
    int i = 0;
    for (; i < MAX_VFP_REGISTERS; i++) registers->vfp_register_allocated[i] = FALSE;
  }
49
50
51
  return registers;
}

52
53
54
void
mkcl_fficall_push_arg(MKCL, union mkcl_ffi_values *data, enum mkcl_ffi_tag type)
{
55
56
57
58
  long word;
  struct mkcl_fficall *fficall = env->fficall;
  struct mkcl_fficall_reg *registers = fficall->registers;

59
  switch (type) {
60
61
62
  case MKCL_FFI_POINTER_VOID: word = (intptr_t) data->pv; goto WORD_WIDE;
  case MKCL_FFI_CSTRING: word = (intptr_t) data->pc; goto WORD_WIDE;
  case MKCL_FFI_OBJECT: word = (intptr_t) data->o; goto WORD_WIDE;
63
64
65
66
67
68
  case MKCL_FFI_CHAR: word = data->c;	goto WORD_WIDE;
  case MKCL_FFI_UNSIGNED_CHAR: word = data->uc; goto WORD_WIDE;
  case MKCL_FFI_BYTE: word = data->b; goto WORD_WIDE;
  case MKCL_FFI_UNSIGNED_BYTE: word = data->ub; goto WORD_WIDE;
  case MKCL_FFI_SHORT: word = data->s; goto WORD_WIDE;
  case MKCL_FFI_UNSIGNED_SHORT: word = data->us; goto WORD_WIDE;
69
70
71
72
  case MKCL_FFI_INT:
  case MKCL_FFI_LONG:
  case MKCL_FFI_UNSIGNED_INT:
  case MKCL_FFI_UNSIGNED_LONG:
73
74
75
76
77
78
79
80
    word = data->l;
  WORD_WIDE:
    if (registers->core_register_count < MAX_CORE_REGISTERS) {
      registers->r[registers->core_register_count++] = word;
    } else {
      mkcl_fficall_align4(env);
      mkcl_fficall_push_bytes(env, &word, sizeof(long));
    }
81
    break;
82
  case MKCL_FFI_LONG_DOUBLE:
83
  case MKCL_FFI_DOUBLE:
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
    if (registers->vfp_register_least_unallocated < (MAX_VFP_REGISTERS - 1)) {
      int least_unallocated_pair = registers->vfp_register_least_unallocated;
      if (least_unallocated_pair & 1) least_unallocated_pair++; /* round up to next pair */
      for (; (least_unallocated_pair < MAX_VFP_REGISTERS)
	     && (registers->vfp_register_allocated[least_unallocated_pair]
		 || registers->vfp_register_allocated[least_unallocated_pair + 1])
	     ; least_unallocated_pair += 2);
      if (least_unallocated_pair < MAX_VFP_REGISTERS) {
	registers->vfp.d[least_unallocated_pair / 2] = data->d;
	registers->vfp_register_allocated[least_unallocated_pair++] = TRUE;
	registers->vfp_register_allocated[least_unallocated_pair++] = TRUE;
	{
	  int i = least_unallocated_pair;

	  for (i++; (i < MAX_VFP_REGISTERS) && registers->vfp_register_allocated[i]; i++);
	  registers->vfp_register_least_unallocated = i;
	}
      } else {
	registers->vfp_register_least_unallocated = MAX_VFP_REGISTERS; /* this ends back-filling */
	mkcl_fficall_align8(env);
	mkcl_fficall_push_bytes(env, &data->d, sizeof(double));
      }
    } else {
      registers->vfp_register_least_unallocated = MAX_VFP_REGISTERS; /* this ends back-filling */
      mkcl_fficall_align8(env);
      mkcl_fficall_push_bytes(env, &data->d, sizeof(double));
    }
111
112
    break;
  case MKCL_FFI_FLOAT:
113
114
115
116
117
118
119
120
121
122
    if (registers->vfp_register_least_unallocated < MAX_VFP_REGISTERS) {
      int i = registers->vfp_register_least_unallocated;
      registers->vfp.s[i] = data->f;
      registers->vfp_register_allocated[i] = TRUE;
      for (i++; (i < MAX_VFP_REGISTERS) && registers->vfp_register_allocated[i]; i++);
      registers->vfp_register_least_unallocated = i;
    } else {
      mkcl_fficall_align4(env);
      mkcl_fficall_push_bytes(env, &data->f, sizeof(float));
    }
123
124
125
    break;
  case MKCL_FFI_UNSIGNED_LONG_LONG:
  case MKCL_FFI_LONG_LONG:
126
127
128
129
130
131
132
    if (registers->core_register_count < (MAX_CORE_REGISTERS - 1)) {
      registers->r[registers->core_register_count++] = data->l2[0];
      registers->r[registers->core_register_count++] = data->l2[1];
    } else {
      mkcl_fficall_align8(env);
      mkcl_fficall_push_bytes(env, &data->ull, sizeof(unsigned long long));
    }
133
134
135
136
137
138
139
140
141
142
143
    break;
  case MKCL_FFI_VOID:
    mkcl_FEerror(env, "VOID is not a valid argument type for a C function", 0);
  default:
    mkcl_FEerror(env, "Unknown argument type for a C function", 0);
  }
}

void
mkcl_fficall_execute(MKCL, void *f_ptr, struct mkcl_fficall *fficall, enum mkcl_ffi_tag return_type)
{
144
  mkcl_word * const args_buffer = (mkcl_word *) fficall->buffer;
145
146
  char* stack_p;
  size_t bufsize;
147
148
  mkcl_index btop;
  struct mkcl_fficall_reg * regs = fficall->registers;
149
150
151

  mkcl_fficall_align16(env); /* Size of a cache line. */
  bufsize = fficall->buffer_sp - fficall->buffer;
152
  btop = bufsize / sizeof(mkcl_word);
153

154
155
156
  typedef long (*word_integral_ffunc)(long a0, long a1, long a2, long a3,
				      double d0, double d1, double d2, double d3,
				      double d4, double d5, double d6, double d7);
157

158
159
160
  typedef long long (*long_long_ffunc)(long a0, long a1, long a2, long a3,
				       double d0, double d1, double d2, double d3,
				       double d4, double d5, double d6, double d7);
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
  typedef void * (*ptr_ffunc)(long a0, long a1, long a2, long a3,
			      double d0, double d1, double d2, double d3,
			      double d4, double d5, double d6, double d7);

  typedef float (*float_ffunc)(long a0, long a1, long a2, long a3,
			       double d0, double d1, double d2, double d3,
			       double d4, double d5, double d6, double d7);

  typedef double (*double_ffunc)(long a0, long a1, long a2, long a3,
				 double d0, double d1, double d2, double d3,
				 double d4, double d5, double d6, double d7);

  typedef long double (*long_double_ffunc)(long a0, long a1, long a2, long a3,
					   double d0, double d1, double d2, double d3,
					   double d4, double d5, double d6, double d7);

  typedef void (*void_ffunc)(long a0, long a1, long a2, long a3,
			     double d0, double d1, double d2, double d3,
			     double d4, double d5, double d6, double d7);

  /* Save current stack pointer and then push stack based arguments. */
  {
    mkcl_word args_on_stack[btop]; /* VLA */

    for (; btop; btop--)
      args_on_stack[btop] = args_buffer[btop]; /* copy overflow args to the tip of the stack. */

    if (return_type <= MKCL_FFI_UNSIGNED_LONG) {
      fficall->output.l
191
192
193
	= ((word_integral_ffunc) f_ptr) (regs->r[0], regs->r[1], regs->r[2], regs->r[3],
					 regs->vfp.d[0], regs->vfp.d[1], regs->vfp.d[2], regs->vfp.d[3],
					 regs->vfp.d[4], regs->vfp.d[5], regs->vfp.d[6], regs->vfp.d[7]);
194
195
    } else if ((return_type == MKCL_FFI_POINTER_VOID)
	       || (return_type == MKCL_FFI_CSTRING)
196
	       ||  (return_type == MKCL_FFI_OBJECT)) {
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
      fficall->output.pv
	= ((ptr_ffunc) f_ptr) (regs->r[0], regs->r[1], regs->r[2], regs->r[3],
			       regs->vfp.d[0], regs->vfp.d[1], regs->vfp.d[2], regs->vfp.d[3],
			       regs->vfp.d[4], regs->vfp.d[5], regs->vfp.d[6], regs->vfp.d[7]);
    } else if (return_type == MKCL_FFI_FLOAT) {
      fficall->output.f
	= ((float_ffunc) f_ptr) (regs->r[0], regs->r[1], regs->r[2], regs->r[3],
				 regs->vfp.d[0], regs->vfp.d[1], regs->vfp.d[2], regs->vfp.d[3],
				 regs->vfp.d[4], regs->vfp.d[5], regs->vfp.d[6], regs->vfp.d[7]);
    } else if (return_type == MKCL_FFI_DOUBLE) {
      fficall->output.d
	= ((double_ffunc) f_ptr) (regs->r[0], regs->r[1], regs->r[2], regs->r[3],
				  regs->vfp.d[0], regs->vfp.d[1], regs->vfp.d[2], regs->vfp.d[3],
				  regs->vfp.d[4], regs->vfp.d[5], regs->vfp.d[6], regs->vfp.d[7]);
    } else if (return_type == MKCL_FFI_LONG_DOUBLE) {
      fficall->output.ld
	= ((long_double_ffunc) f_ptr) (regs->r[0], regs->r[1], regs->r[2], regs->r[3],
				       regs->vfp.d[0], regs->vfp.d[1], regs->vfp.d[2], regs->vfp.d[3],
				       regs->vfp.d[4], regs->vfp.d[5], regs->vfp.d[6], regs->vfp.d[7]);
    }
    else if (return_type == MKCL_FFI_VOID) {
      ((void_ffunc) f_ptr) (regs->r[0], regs->r[1], regs->r[2], regs->r[3],
			    regs->vfp.d[0], regs->vfp.d[1], regs->vfp.d[2], regs->vfp.d[3],
			    regs->vfp.d[4], regs->vfp.d[5], regs->vfp.d[6], regs->vfp.d[7]);
    }
    else if ((return_type == MKCL_FFI_LONG_LONG)
	     || (return_type == MKCL_FFI_UNSIGNED_LONG_LONG)) {
      fficall->output.ll
	= ((long_long_ffunc) f_ptr) (regs->r[0], regs->r[1], regs->r[2], regs->r[3],
				     regs->vfp.d[0], regs->vfp.d[1], regs->vfp.d[2], regs->vfp.d[3],
				     regs->vfp.d[4], regs->vfp.d[5], regs->vfp.d[6], regs->vfp.d[7]);
    }
    else {
      mkcl_FEerror(env, "Unknown C function return type", 0);
    }
  }
233
234
235
236
237
238
}


static const mkcl_base_string_object(mkcl_dynamic_callback_import_thread_name__obj_, "mkcl_dynamic_callback_execute");
static const mkcl_object mkcl_dynamic_callback_import_thread_name = (mkcl_object) &mkcl_dynamic_callback_import_thread_name__obj_;

239
static union mkcl_ffi_values
240
mkcl_dynamic_callback_execute(long a1, long a2, long a3, long a4,
241
242
243
			      double d0, double d1, double d2, double d3,
			      double d4, double d5, double d6, double d7,
			      mkcl_object cbk_info, char *arg_buffer)
244
245
246
247
248
249
{
  char stack_mark = 0;
  mkcl_object result;
  mkcl_env env = MKCL_ENV();
  mkcl_env imported_env = NULL;

250
251
252
253
  mkcl_object fun = MKCL_CAR(cbk_info);
  mkcl_object rtype = MKCL_CADR(cbk_info);
  mkcl_object argtypes = MKCL_CADDR(cbk_info);

254
255
256
257
  struct mkcl_fficall_reg registers;

  mkcl_fficall_prepare_extra(env, &registers);

258
259
  arg_buffer += sizeof(long); /* Skip saved LR (a.k.a.: return address) */
  arg_buffer += sizeof(long); /* Skip saved r4 */
260

261
262
263
264
265
  if (env == NULL)
    {
      env = imported_env = mkcl_import_current_thread(mkcl_dynamic_callback_import_thread_name, mk_cl_Cnil, NULL, NULL);
      if (imported_env == NULL)
        /* In the normal case we always set errno = 0, so this is clearly an error situation. */
266
	{ union mkcl_ffi_values val; val.ll = 0; errno = ENOMEM; return val; }
267
268
269
270
    }

  MKCL_BUILD_TEMP_STACK_FRAME(env, frame, aux);

271
272
273
  for (; !mkcl_endp(env, argtypes); argtypes = MKCL_CDR(argtypes)) {
    enum mkcl_ffi_tag tag = mkcl_foreign_type_code(env, MKCL_CAR(argtypes));
    mkcl_index size = mkcl_fixnum_to_word(mk_si_size_of_foreign_elt_type(env, MKCL_CAR(argtypes)));
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
    if ((tag == MKCL_FFI_LONG_LONG) || (tag == MKCL_FFI_UNSIGNED_LONG_LONG))
      {
	if (registers.core_register_count < (MAX_CORE_REGISTERS - 1)) {
	  result = mkcl_foreign_ref_elt(env, &registers.r[registers.core_register_count], tag);
	  registers.core_register_count += 2;
	} else
	  goto ARG_FROM_STACK;
      }
    else if (tag <= MKCL_FFI_OBJECT) /* word sized integral */
      if (registers.core_register_count < MAX_CORE_REGISTERS)
	result = mkcl_foreign_ref_elt(env, &registers.r[registers.core_register_count++], tag);
      else
	goto ARG_FROM_STACK;
    else if (tag == MKCL_FFI_FLOAT)
      if (registers.vfp_register_least_unallocated < MAX_VFP_REGISTERS) {
	int i = registers.vfp_register_least_unallocated;
	result = mkcl_foreign_ref_elt(env, &registers.vfp.s[i], tag);
	registers.vfp_register_allocated[i] = TRUE;
	for (i++; (i < MAX_VFP_REGISTERS) && registers.vfp_register_allocated[i]; i++);
	registers.vfp_register_least_unallocated = i;
      } else
	goto ARG_FROM_STACK;
    else if ((tag == MKCL_FFI_DOUBLE) || (tag == MKCL_FFI_LONG_DOUBLE))
      if (registers.vfp_register_least_unallocated < (MAX_VFP_REGISTERS - 1)) {
	int least_unallocated_pair = registers.vfp_register_least_unallocated;
	if (least_unallocated_pair & 1) least_unallocated_pair++; /* round up to next pair */
	for (; (least_unallocated_pair < MAX_VFP_REGISTERS)
	       && (registers.vfp_register_allocated[least_unallocated_pair]
		   || registers.vfp_register_allocated[least_unallocated_pair + 1])
	       ; least_unallocated_pair += 2);
	if (least_unallocated_pair < MAX_VFP_REGISTERS) {
	  result = mkcl_foreign_ref_elt(env, &registers.vfp.d[least_unallocated_pair / 2], tag);
	  registers.vfp_register_allocated[least_unallocated_pair++] = TRUE;
	  registers.vfp_register_allocated[least_unallocated_pair++] = TRUE;
	  {
	    int i = least_unallocated_pair;

	    for (i++; (i < MAX_VFP_REGISTERS) && registers.vfp_register_allocated[i]; i++);
	    registers.vfp_register_least_unallocated = i;
	  }
	} else {
	  registers.vfp_register_least_unallocated = MAX_VFP_REGISTERS; /* this ends back-filling */
	  goto ARG_FROM_STACK;
	}
      } else {
	registers.vfp_register_least_unallocated = MAX_VFP_REGISTERS; /* this ends back-filling */
	goto ARG_FROM_STACK;
      }
    else if (tag >= MKCL_FFI_VOID)
      mkcl_FEerror(env, "Invalid C function callback argument type", 0);
    else
    ARG_FROM_STACK:
      {
	mkcl_index size_on_stack = (size + 0x07) & ~((mkcl_index) 0x07);

	result = mkcl_foreign_ref_elt(env, arg_buffer, tag);
	arg_buffer += size_on_stack;
      }
332
    mkcl_temp_stack_frame_push(env, frame, result);
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
  }

  if (imported_env)
    {
      mkcl_object thread = env->own_thread;

      MKCL_CATCH_ALL_BEGIN(env) {
	MKCL_SETUP_CALL_STACK_ROOT_GUARD(env);
	mkcl_setup_thread_lisp_context(env, &stack_mark);
	mkcl_register_thread_as_active(env, thread);
	mkcl_enable_interrupts(env);

	result = mkcl_apply_from_temp_stack_frame(env, frame, fun);

	mkcl_disable_interrupts(env);
	mkcl_cleanup_thread_lisp_context(env);
      } MKCL_CATCH_ALL_END;
      thread->thread.status = mkcl_thread_done;
    }
  else
    result = mkcl_apply_from_temp_stack_frame(env, frame, fun);
  mkcl_temp_stack_frame_close(env, frame);

356
357
358
  {
    union mkcl_ffi_values output;
    enum mkcl_ffi_tag tag = mkcl_foreign_type_code(env, rtype);
359

360
361
    memset(&output, 0, sizeof(output));
    mkcl_foreign_set_elt(env, &output, tag, result);
362

363
364
    if (imported_env)
      mkcl_release_current_thread(imported_env);
365

366
367
    errno = 0; /* See above for the necessity of this. */
    return output;
368
369
370
  }
}

371
static void
372
mkcl_void_dynamic_callback_execute(long a1, long a2, long a3, long a4,
373
374
375
376
				   double d0, double d1, double d2, double d3,
				   double d4, double d5, double d6, double d7,
				   mkcl_object cbk_info, char *arg_buffer)
{
377
  (void) mkcl_dynamic_callback_execute(a1, a2, a3, a4, d0, d1, d2, d3, d4, d5, d6, d7, cbk_info, arg_buffer);
378
379
380
}

static mkcl_int8_t
381
mkcl_byte_dynamic_callback_execute(long a1, long a2, long a3, long a4,
382
383
384
385
386
				   double d0, double d1, double d2, double d3,
				   double d4, double d5, double d6, double d7,
				   mkcl_object cbk_info, char *arg_buffer)
{
  union mkcl_ffi_values val
387
    = mkcl_dynamic_callback_execute(a1, a2, a3, a4, d0, d1, d2, d3, d4, d5, d6, d7, cbk_info, arg_buffer);
388
389
390
391
  return val.b;
}

static short
392
mkcl_short_dynamic_callback_execute(long a1, long a2, long a3, long a4,
393
394
395
396
397
				   double d0, double d1, double d2, double d3,
				   double d4, double d5, double d6, double d7,
				   mkcl_object cbk_info, char *arg_buffer)
{
  union mkcl_ffi_values val
398
    = mkcl_dynamic_callback_execute(a1, a2, a3, a4, d0, d1, d2, d3, d4, d5, d6, d7, cbk_info, arg_buffer);
399
400
401
402
  return val.s;
}

static long
403
mkcl_long_dynamic_callback_execute(long a1, long a2, long a3, long a4,
404
405
406
407
408
				   double d0, double d1, double d2, double d3,
				   double d4, double d5, double d6, double d7,
				   mkcl_object cbk_info, char *arg_buffer)
{
  union mkcl_ffi_values val
409
    = mkcl_dynamic_callback_execute(a1, a2, a3, a4, d0, d1, d2, d3, d4, d5, d6, d7, cbk_info, arg_buffer);
410
411
412
413
  return val.l;
}

static long long
414
mkcl_long_long_dynamic_callback_execute(long a1, long a2, long a3, long a4,
415
416
417
418
419
				   double d0, double d1, double d2, double d3,
				   double d4, double d5, double d6, double d7,
				   mkcl_object cbk_info, char *arg_buffer)
{
  union mkcl_ffi_values val
420
    = mkcl_dynamic_callback_execute(a1, a2, a3, a4, d0, d1, d2, d3, d4, d5, d6, d7, cbk_info, arg_buffer);
421
422
423
424
  return val.ll;
}

static float
425
mkcl_float_dynamic_callback_execute(long a1, long a2, long a3, long a4,
426
427
428
429
430
				   double d0, double d1, double d2, double d3,
				   double d4, double d5, double d6, double d7,
				   mkcl_object cbk_info, char *arg_buffer)
{
  union mkcl_ffi_values val
431
    = mkcl_dynamic_callback_execute(a1, a2, a3, a4, d0, d1, d2, d3, d4, d5, d6, d7, cbk_info, arg_buffer);
432
433
434
435
  return val.f;
}

static double
436
mkcl_double_dynamic_callback_execute(long a1, long a2, long a3, long a4,
437
438
439
440
441
				   double d0, double d1, double d2, double d3,
				   double d4, double d5, double d6, double d7,
				   mkcl_object cbk_info, char *arg_buffer)
{
  union mkcl_ffi_values val
442
    = mkcl_dynamic_callback_execute(a1, a2, a3, a4, d0, d1, d2, d3, d4, d5, d6, d7, cbk_info, arg_buffer);
443
444
445
446
  return val.d;
}

static long double
447
mkcl_long_double_dynamic_callback_execute(long a1, long a2, long a3, long a4,
448
449
450
451
452
				   double d0, double d1, double d2, double d3,
				   double d4, double d5, double d6, double d7,
				   mkcl_object cbk_info, char *arg_buffer)
{
  union mkcl_ffi_values val
453
    = mkcl_dynamic_callback_execute(a1, a2, a3, a4, d0, d1, d2, d3, d4, d5, d6, d7, cbk_info, arg_buffer);
454
455
456
457
458
  return val.ld;
}



459
460
461
462
463
464
465
466
467
468
469
470
471
472

void *
mkcl_dynamic_callback_make(MKCL, mkcl_object data, enum mkcl_ffi_calling_convention cc_type)
{
  unsigned char * buf = mkcl_alloc_callback_block(env);
  unsigned char * ip = buf; /* the instruction pointer (ip) */
  union { unsigned char b[4]; void * p; unsigned long l; unsigned short s; } imm; /* a staging buffer for immediate data */

#define i(byte) *(ip++) = (byte)
#define immed_ptr(val_ptr) imm.p = (val_ptr); i(imm.b[0]); i(imm.b[1]); i(imm.b[2]); i(imm.b[3]);
#define immed16(val_short) imm.s = (val_short);	i(imm.b[0]); i(imm.b[1]);
#define immed32(val_long) imm.l = (val_long); i(imm.b[0]); i(imm.b[1]); i(imm.b[2]); i(imm.b[3]);
    

473
474
  enum mkcl_ffi_tag tag = mkcl_foreign_type_code(env, MKCL_CADR(data));
  void * fptr = 0;
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
  switch (tag) {
  case MKCL_FFI_CHAR:
  case MKCL_FFI_UNSIGNED_CHAR:
  case MKCL_FFI_BYTE:
  case MKCL_FFI_UNSIGNED_BYTE:
    fptr = mkcl_byte_dynamic_callback_execute;
    break;
  case MKCL_FFI_SHORT:
  case MKCL_FFI_UNSIGNED_SHORT:
    fptr = mkcl_short_dynamic_callback_execute;
    break;
  case MKCL_FFI_POINTER_VOID:
  case MKCL_FFI_OBJECT:
  case MKCL_FFI_CSTRING:
  case MKCL_FFI_INT:
  case MKCL_FFI_UNSIGNED_INT:
  case MKCL_FFI_LONG:
  case MKCL_FFI_UNSIGNED_LONG:
    fptr = mkcl_long_dynamic_callback_execute;
    break;
  case MKCL_FFI_LONG_LONG:
  case MKCL_FFI_UNSIGNED_LONG_LONG:
    fptr = mkcl_long_long_dynamic_callback_execute;
    break;
  case MKCL_FFI_DOUBLE:
    fptr = mkcl_double_dynamic_callback_execute;
    break;
  case MKCL_FFI_LONG_DOUBLE:
    fptr = mkcl_long_double_dynamic_callback_execute;
    break;
  case MKCL_FFI_FLOAT:
    fptr = mkcl_float_dynamic_callback_execute;
    break;
  case MKCL_FFI_VOID:
    fptr = mkcl_void_dynamic_callback_execute;
    break;
  default:
    mkcl_FEerror(env, "Invalid C function callback return type", 0);
514
  }
515

516
517
518
519
520
521
522
523
524
525
526
527
528
  i(0x10); i(0xB5);	/* push	{r4, lr}                               */
  i(0x6C); i(0x46);	/* mov	r4, sp                                 */
  i(0x10); i(0xB4);	/* push    {r4}           @ push stack_marker  */
  i(0x03); i(0x4C);	/* ldr	r4, =0xdeadbeef   @ get cbk_info       */
  i(0x10); i(0xB4);	/* push    {r4}           @ push cbk_info      */
  i(0x03); i(0x4C);	/* ldr	r4, =0xbeefdead   @ get pointer to callback frame provider */
  i(0xA0); i(0x47);	/* blx	r4                @ call through r4    */
  i(0x02); i(0xB0);	/* add	sp, sp, #8        @ remove pushed args */
  i(0x10); i(0xBD);	/* pop	{r4, pc}                               */
  i(0x00); i(0xBF);	/* .align 2                                    */
  immed_ptr(data);
  immed_ptr(fptr);

529
530
531
532
533

  {
    int rc = mprotect(buf, mkcl_core.pagesize, PROT_READ | /* PROT_WRITE | */ PROT_EXEC);
    if (rc)
      mkcl_FElibc_error(env, "mkcl_dynamic_callback_make() failed on mprotect()", 0);
534
535
536
537
  }

  return buf;
}