Daily bump.
[official-gcc.git] / libffi / src / m88k / ffi.c
blob68df494955e49f3b2c6c2e60e311c4bca495226e
1 /*
2 * Copyright (c) 2013 Miodrag Vallat. <miod@openbsd.org>
4 * Permission is hereby granted, free of charge, to any person obtaining
5 * a copy of this software and associated documentation files (the
6 * ``Software''), to deal in the Software without restriction, including
7 * without limitation the rights to use, copy, modify, merge, publish,
8 * distribute, sublicense, and/or sell copies of the Software, and to
9 * permit persons to whom the Software is furnished to do so, subject to
10 * the following conditions:
12 * The above copyright notice and this permission notice shall be included
13 * in all copies or substantial portions of the Software.
15 * THE SOFTWARE IS PROVIDED ``AS IS'', WITHOUT WARRANTY OF ANY KIND,
16 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
18 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
19 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
20 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
21 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 * m88k Foreign Function Interface
27 * This file attempts to provide all the FFI entry points which can reliably
28 * be implemented in C.
30 * Only OpenBSD/m88k is currently supported; other platforms (such as
31 * Motorola's SysV/m88k) could be supported with the following tweaks:
33 * - non-OpenBSD systems use an `outgoing parameter area' as part of the
34 * 88BCS calling convention, which is not supported under OpenBSD from
35 * release 3.6 onwards. Supporting it should be as easy as taking it
36 * into account when adjusting the stack, in the assembly code.
38 * - the logic deciding whether a function argument gets passed through
39 * registers, or on the stack, has changed several times in OpenBSD in
40 * edge cases (especially for structs larger than 32 bytes being passed
41 * by value). The code below attemps to match the logic used by the
42 * system compiler of OpenBSD 5.3, i.e. gcc 3.3.6 with many m88k backend
43 * fixes.
46 #include <ffi.h>
47 #include <ffi_common.h>
49 #include <stdlib.h>
50 #include <unistd.h>
52 void ffi_call_OBSD (unsigned int, extended_cif *, unsigned int, void *,
53 void (*fn) ());
54 void *ffi_prep_args (void *, extended_cif *);
55 void ffi_closure_OBSD (ffi_closure *);
56 void ffi_closure_struct_OBSD (ffi_closure *);
57 unsigned int ffi_closure_OBSD_inner (ffi_closure *, void *, unsigned int *,
58 char *);
59 void ffi_cacheflush_OBSD (unsigned int, unsigned int);
61 #define CIF_FLAGS_INT (1 << 0)
62 #define CIF_FLAGS_DINT (1 << 1)
65 * Foreign Function Interface API
68 /* ffi_prep_args is called by the assembly routine once stack space has
69 been allocated for the function's arguments. */
71 void *
72 ffi_prep_args (void *stack, extended_cif *ecif)
74 unsigned int i;
75 void **p_argv;
76 char *argp, *stackp;
77 unsigned int *regp;
78 unsigned int regused;
79 ffi_type **p_arg;
80 void *struct_value_ptr;
82 regp = (unsigned int *)stack;
83 stackp = (char *)(regp + 8);
84 regused = 0;
86 if (ecif->cif->rtype->type == FFI_TYPE_STRUCT
87 && !ecif->cif->flags)
88 struct_value_ptr = ecif->rvalue;
89 else
90 struct_value_ptr = NULL;
92 p_argv = ecif->avalue;
94 for (i = ecif->cif->nargs, p_arg = ecif->cif->arg_types; i != 0; i--, p_arg++)
96 size_t z;
97 unsigned short t, a;
99 z = (*p_arg)->size;
100 t = (*p_arg)->type;
101 a = (*p_arg)->alignment;
104 * Figure out whether the argument can be passed through registers
105 * or on the stack.
106 * The rule is that registers can only receive simple types not larger
107 * than 64 bits, or structs the exact size of a register and aligned to
108 * the size of a register.
110 if (t == FFI_TYPE_STRUCT)
112 if (z == sizeof (int) && a == sizeof (int) && regused < 8)
113 argp = (char *)regp;
114 else
115 argp = stackp;
117 else
119 if (z > sizeof (int) && regused < 8 - 1)
121 /* align to an even register pair */
122 if (regused & 1)
124 regp++;
125 regused++;
128 if (regused < 8)
129 argp = (char *)regp;
130 else
131 argp = stackp;
134 /* Enforce proper stack alignment of 64-bit types */
135 if (argp == stackp && a > sizeof (int))
137 stackp = (char *) ALIGN(stackp, a);
138 argp = stackp;
141 switch (t)
143 case FFI_TYPE_SINT8:
144 *(signed int *) argp = (signed int) *(SINT8 *) *p_argv;
145 break;
147 case FFI_TYPE_UINT8:
148 *(unsigned int *) argp = (unsigned int) *(UINT8 *) *p_argv;
149 break;
151 case FFI_TYPE_SINT16:
152 *(signed int *) argp = (signed int) *(SINT16 *) *p_argv;
153 break;
155 case FFI_TYPE_UINT16:
156 *(unsigned int *) argp = (unsigned int) *(UINT16 *) *p_argv;
157 break;
159 case FFI_TYPE_INT:
160 case FFI_TYPE_FLOAT:
161 case FFI_TYPE_UINT32:
162 case FFI_TYPE_SINT32:
163 case FFI_TYPE_POINTER:
164 *(unsigned int *) argp = *(unsigned int *) *p_argv;
165 break;
167 case FFI_TYPE_DOUBLE:
168 case FFI_TYPE_UINT64:
169 case FFI_TYPE_SINT64:
170 case FFI_TYPE_STRUCT:
171 memcpy (argp, *p_argv, z);
172 break;
174 default:
175 FFI_ASSERT (0);
178 /* Align if necessary. */
179 if ((sizeof (int) - 1) & z)
180 z = ALIGN(z, sizeof (int));
182 p_argv++;
184 /* Be careful, once all registers are filled, and about to continue
185 on stack, regp == stackp. Therefore the check for regused as well. */
186 if (argp == (char *)regp && regused < 8)
188 regp += z / sizeof (int);
189 regused += z / sizeof (int);
191 else
192 stackp += z;
195 return struct_value_ptr;
198 /* Perform machine dependent cif processing */
199 ffi_status
200 ffi_prep_cif_machdep (ffi_cif *cif)
202 /* Set the return type flag */
203 switch (cif->rtype->type)
205 case FFI_TYPE_VOID:
206 cif->flags = 0;
207 break;
209 case FFI_TYPE_STRUCT:
210 if (cif->rtype->size == sizeof (int) &&
211 cif->rtype->alignment == sizeof (int))
212 cif->flags = CIF_FLAGS_INT;
213 else
214 cif->flags = 0;
215 break;
217 case FFI_TYPE_DOUBLE:
218 case FFI_TYPE_SINT64:
219 case FFI_TYPE_UINT64:
220 cif->flags = CIF_FLAGS_DINT;
221 break;
223 default:
224 cif->flags = CIF_FLAGS_INT;
225 break;
228 return FFI_OK;
231 void
232 ffi_call (ffi_cif *cif, void (*fn) (), void *rvalue, void **avalue)
234 extended_cif ecif;
236 ecif.cif = cif;
237 ecif.avalue = avalue;
239 /* If the return value is a struct and we don't have a return value
240 address then we need to make one. */
242 if (rvalue == NULL
243 && cif->rtype->type == FFI_TYPE_STRUCT
244 && (cif->rtype->size != sizeof (int)
245 || cif->rtype->alignment != sizeof (int)))
246 ecif.rvalue = alloca (cif->rtype->size);
247 else
248 ecif.rvalue = rvalue;
250 switch (cif->abi)
252 case FFI_OBSD:
253 ffi_call_OBSD (cif->bytes, &ecif, cif->flags, ecif.rvalue, fn);
254 break;
256 default:
257 FFI_ASSERT (0);
258 break;
263 * Closure API
266 static void
267 ffi_prep_closure_args_OBSD (ffi_cif *cif, void **avalue, unsigned int *regp,
268 char *stackp)
270 unsigned int i;
271 void **p_argv;
272 char *argp;
273 unsigned int regused;
274 ffi_type **p_arg;
276 regused = 0;
278 p_argv = avalue;
280 for (i = cif->nargs, p_arg = cif->arg_types; i != 0; i--, p_arg++)
282 size_t z;
283 unsigned short t, a;
285 z = (*p_arg)->size;
286 t = (*p_arg)->type;
287 a = (*p_arg)->alignment;
290 * Figure out whether the argument has been passed through registers
291 * or on the stack.
292 * The rule is that registers can only receive simple types not larger
293 * than 64 bits, or structs the exact size of a register and aligned to
294 * the size of a register.
296 if (t == FFI_TYPE_STRUCT)
298 if (z == sizeof (int) && a == sizeof (int) && regused < 8)
299 argp = (char *)regp;
300 else
301 argp = stackp;
303 else
305 if (z > sizeof (int) && regused < 8 - 1)
307 /* align to an even register pair */
308 if (regused & 1)
310 regp++;
311 regused++;
314 if (regused < 8)
315 argp = (char *)regp;
316 else
317 argp = stackp;
320 /* Enforce proper stack alignment of 64-bit types */
321 if (argp == stackp && a > sizeof (int))
323 stackp = (char *) ALIGN(stackp, a);
324 argp = stackp;
327 if (z < sizeof (int) && t != FFI_TYPE_STRUCT)
328 *p_argv = (void *) (argp + sizeof (int) - z);
329 else
330 *p_argv = (void *) argp;
332 /* Align if necessary */
333 if ((sizeof (int) - 1) & z)
334 z = ALIGN(z, sizeof (int));
336 p_argv++;
338 /* Be careful, once all registers are exhausted, and about to fetch from
339 stack, regp == stackp. Therefore the check for regused as well. */
340 if (argp == (char *)regp && regused < 8)
342 regp += z / sizeof (int);
343 regused += z / sizeof (int);
345 else
346 stackp += z;
350 unsigned int
351 ffi_closure_OBSD_inner (ffi_closure *closure, void *resp, unsigned int *regp,
352 char *stackp)
354 ffi_cif *cif;
355 void **arg_area;
357 cif = closure->cif;
358 arg_area = (void**) alloca (cif->nargs * sizeof (void *));
360 ffi_prep_closure_args_OBSD(cif, arg_area, regp, stackp);
362 (closure->fun) (cif, resp, arg_area, closure->user_data);
364 return cif->flags;
367 ffi_status
368 ffi_prep_closure_loc (ffi_closure* closure, ffi_cif* cif,
369 void (*fun)(ffi_cif*,void*,void**,void*),
370 void *user_data, void *codeloc)
372 unsigned int *tramp = (unsigned int *) codeloc;
373 void *fn;
375 FFI_ASSERT (cif->abi == FFI_OBSD);
377 if (cif->rtype->type == FFI_TYPE_STRUCT && !cif->flags)
378 fn = &ffi_closure_struct_OBSD;
379 else
380 fn = &ffi_closure_OBSD;
382 /* or.u %r10, %r0, %hi16(fn) */
383 tramp[0] = 0x5d400000 | (((unsigned int)fn) >> 16);
384 /* or.u %r13, %r0, %hi16(closure) */
385 tramp[1] = 0x5da00000 | ((unsigned int)closure >> 16);
386 /* or %r10, %r10, %lo16(fn) */
387 tramp[2] = 0x594a0000 | (((unsigned int)fn) & 0xffff);
388 /* jmp.n %r10 */
389 tramp[3] = 0xf400c40a;
390 /* or %r13, %r13, %lo16(closure) */
391 tramp[4] = 0x59ad0000 | ((unsigned int)closure & 0xffff);
393 ffi_cacheflush_OBSD((unsigned int)codeloc, FFI_TRAMPOLINE_SIZE);
395 closure->cif = cif;
396 closure->user_data = user_data;
397 closure->fun = fun;
399 return FFI_OK;