2015-06-23 Richard Biener <rguenther@suse.de>
[official-gcc.git] / libffi / src / nios2 / ffi.c
blob2efa033f9cd61f15164f3f45230bed715e486492
1 /* libffi support for Altera Nios II.
3 Copyright (c) 2013 Mentor Graphics.
5 Permission is hereby granted, free of charge, to any person obtaining
6 a copy of this software and associated documentation files (the
7 ``Software''), to deal in the Software without restriction, including
8 without limitation the rights to use, copy, modify, merge, publish,
9 distribute, sublicense, and/or sell copies of the Software, and to
10 permit persons to whom the Software is furnished to do so, subject to
11 the following conditions:
13 The above copyright notice and this permission notice shall be
14 included in all copies or substantial portions of the Software.
16 THE SOFTWARE IS PROVIDED ``AS IS'', WITHOUT WARRANTY OF ANY KIND,
17 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19 IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20 CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
21 TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
22 SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
25 #include <ffi.h>
26 #include <ffi_common.h>
28 #include <stdlib.h>
30 /* The Nios II Processor Reference Handbook defines the procedure call
31 ABI as follows.
33 Arguments are passed as if a structure containing the types of
34 the arguments were constructed. The first 16 bytes are passed in r4
35 through r7, the remainder on the stack. The first 16 bytes of a function
36 taking variable arguments are passed in r4-r7 in the same way.
38 Return values of types up to 8 bytes are returned in r2 and r3. For
39 return values greater than 8 bytes, the caller must allocate memory for
40 the result and pass the address as if it were argument 0.
42 While this isn't specified explicitly in the ABI documentation, GCC
43 promotes integral arguments smaller than int size to 32 bits.
45 Also of note, the ABI specifies that all structure objects are
46 aligned to 32 bits even if all their fields have a smaller natural
47 alignment. See FFI_AGGREGATE_ALIGNMENT. */
50 /* Declare the assembly language hooks. */
52 extern UINT64 ffi_call_sysv (void (*) (char *, extended_cif *),
53 extended_cif *,
54 unsigned,
55 void (*fn) (void));
56 extern void ffi_closure_sysv (void);
58 /* Perform machine-dependent cif processing. */
60 ffi_status ffi_prep_cif_machdep (ffi_cif *cif)
62 /* We always want at least 16 bytes in the parameter block since it
63 simplifies the low-level call function. Also round the parameter
64 block size up to a multiple of 4 bytes to preserve
65 32-bit alignment of the stack pointer. */
66 if (cif->bytes < 16)
67 cif->bytes = 16;
68 else
69 cif->bytes = (cif->bytes + 3) & ~3;
71 return FFI_OK;
75 /* ffi_prep_args is called by the assembly routine to transfer arguments
76 to the stack using the pointers in the ecif array.
77 Note that the stack buffer is big enough to fit all the arguments,
78 but the first 16 bytes will be copied to registers for the actual
79 call. */
81 void ffi_prep_args (char *stack, extended_cif *ecif)
83 char *argp = stack;
84 unsigned int i;
86 /* The implicit return value pointer is passed as if it were a hidden
87 first argument. */
88 if (ecif->cif->rtype->type == FFI_TYPE_STRUCT
89 && ecif->cif->rtype->size > 8)
91 (*(void **) argp) = ecif->rvalue;
92 argp += 4;
95 for (i = 0; i < ecif->cif->nargs; i++)
97 void *avalue = ecif->avalue[i];
98 ffi_type *atype = ecif->cif->arg_types[i];
99 size_t size = atype->size;
100 size_t alignment = atype->alignment;
102 /* Align argp as appropriate for the argument type. */
103 if ((alignment - 1) & (unsigned) argp)
104 argp = (char *) ALIGN (argp, alignment);
106 /* Copy the argument, promoting integral types smaller than a
107 word to word size. */
108 if (size < sizeof (int))
110 size = sizeof (int);
111 switch (atype->type)
113 case FFI_TYPE_SINT8:
114 *(signed int *) argp = (signed int) *(SINT8 *) avalue;
115 break;
117 case FFI_TYPE_UINT8:
118 *(unsigned int *) argp = (unsigned int) *(UINT8 *) avalue;
119 break;
121 case FFI_TYPE_SINT16:
122 *(signed int *) argp = (signed int) *(SINT16 *) avalue;
123 break;
125 case FFI_TYPE_UINT16:
126 *(unsigned int *) argp = (unsigned int) *(UINT16 *) avalue;
127 break;
129 case FFI_TYPE_STRUCT:
130 memcpy (argp, avalue, atype->size);
131 break;
133 default:
134 FFI_ASSERT(0);
137 else if (size == sizeof (int))
138 *(unsigned int *) argp = (unsigned int) *(UINT32 *) avalue;
139 else
140 memcpy (argp, avalue, size);
141 argp += size;
146 /* Call FN using the prepared CIF. RVALUE points to space allocated by
147 the caller for the return value, and AVALUE is an array of argument
148 pointers. */
150 void ffi_call (ffi_cif *cif, void (*fn) (void), void *rvalue, void **avalue)
153 extended_cif ecif;
154 UINT64 result;
156 /* If bigret is true, this is the case where a return value of larger
157 than 8 bytes is handled by being passed by reference as an implicit
158 argument. */
159 int bigret = (cif->rtype->type == FFI_TYPE_STRUCT
160 && cif->rtype->size > 8);
162 ecif.cif = cif;
163 ecif.avalue = avalue;
165 /* Allocate space for return value if this is the pass-by-reference case
166 and the caller did not provide a buffer. */
167 if (rvalue == NULL && bigret)
168 ecif.rvalue = alloca (cif->rtype->size);
169 else
170 ecif.rvalue = rvalue;
172 result = ffi_call_sysv (ffi_prep_args, &ecif, cif->bytes, fn);
174 /* Now result contains the 64 bit contents returned from fn in
175 r2 and r3. Copy the value of the appropriate size to the user-provided
176 rvalue buffer. */
177 if (rvalue && !bigret)
178 switch (cif->rtype->size)
180 case 1:
181 *(UINT8 *)rvalue = (UINT8) result;
182 break;
183 case 2:
184 *(UINT16 *)rvalue = (UINT16) result;
185 break;
186 case 4:
187 *(UINT32 *)rvalue = (UINT32) result;
188 break;
189 case 8:
190 *(UINT64 *)rvalue = (UINT64) result;
191 break;
192 default:
193 memcpy (rvalue, (void *)&result, cif->rtype->size);
194 break;
198 /* This function is invoked from the closure trampoline to invoke
199 CLOSURE with argument block ARGS. Parse ARGS according to
200 CLOSURE->cfi and invoke CLOSURE->fun. */
202 static UINT64
203 ffi_closure_helper (unsigned char *args,
204 ffi_closure *closure)
206 ffi_cif *cif = closure->cif;
207 unsigned char *argp = args;
208 void **parsed_args = alloca (cif->nargs * sizeof (void *));
209 UINT64 result;
210 void *retptr;
211 unsigned int i;
213 /* First figure out what to do about the return type. If this is the
214 big-structure-return case, the first arg is the hidden return buffer
215 allocated by the caller. */
216 if (cif->rtype->type == FFI_TYPE_STRUCT
217 && cif->rtype->size > 8)
219 retptr = *((void **) argp);
220 argp += 4;
222 else
223 retptr = (void *) &result;
225 /* Fill in the array of argument pointers. */
226 for (i = 0; i < cif->nargs; i++)
228 size_t size = cif->arg_types[i]->size;
229 size_t alignment = cif->arg_types[i]->alignment;
231 /* Align argp as appropriate for the argument type. */
232 if ((alignment - 1) & (unsigned) argp)
233 argp = (char *) ALIGN (argp, alignment);
235 /* Arguments smaller than an int are promoted to int. */
236 if (size < sizeof (int))
237 size = sizeof (int);
239 /* Store the pointer. */
240 parsed_args[i] = argp;
241 argp += size;
244 /* Call the user-supplied function. */
245 (closure->fun) (cif, retptr, parsed_args, closure->user_data);
246 return result;
250 /* Initialize CLOSURE with a trampoline to call FUN with
251 CIF and USER_DATA. */
252 ffi_status
253 ffi_prep_closure_loc (ffi_closure* closure,
254 ffi_cif* cif,
255 void (*fun) (ffi_cif*, void*, void**, void*),
256 void *user_data,
257 void *codeloc)
259 unsigned int *tramp = (unsigned int *) &closure->tramp[0];
260 int i;
262 if (cif->abi != FFI_SYSV)
263 return FFI_BAD_ABI;
265 /* The trampoline looks like:
266 movhi r8, %hi(ffi_closure_sysv)
267 ori r8, r8, %lo(ffi_closure_sysv)
268 movhi r9, %hi(ffi_closure_helper)
269 ori r0, r9, %lo(ffi_closure_helper)
270 movhi r10, %hi(closure)
271 ori r10, r10, %lo(closure)
272 jmp r8
273 and then ffi_closure_sysv retrieves the closure pointer out of r10
274 in addition to the arguments passed in the normal way for the call,
275 and invokes ffi_closure_helper. We encode the pointer to
276 ffi_closure_helper in the trampoline because making a PIC call
277 to it in ffi_closure_sysv would be messy (it would have to indirect
278 through the GOT). */
280 #define HI(x) ((((unsigned int) (x)) >> 16) & 0xffff)
281 #define LO(x) (((unsigned int) (x)) & 0xffff)
282 tramp[0] = (0 << 27) | (8 << 22) | (HI (ffi_closure_sysv) << 6) | 0x34;
283 tramp[1] = (8 << 27) | (8 << 22) | (LO (ffi_closure_sysv) << 6) | 0x14;
284 tramp[2] = (0 << 27) | (9 << 22) | (HI (ffi_closure_helper) << 6) | 0x34;
285 tramp[3] = (9 << 27) | (9 << 22) | (LO (ffi_closure_helper) << 6) | 0x14;
286 tramp[4] = (0 << 27) | (10 << 22) | (HI (closure) << 6) | 0x34;
287 tramp[5] = (10 << 27) | (10 << 22) | (LO (closure) << 6) | 0x14;
288 tramp[6] = (8 << 27) | (0x0d << 11) | 0x3a;
289 #undef HI
290 #undef LO
292 /* Flush the caches.
293 See Example 9-4 in the Nios II Software Developer's Handbook. */
294 for (i = 0; i < 7; i++)
295 asm volatile ("flushd 0(%0); flushi %0" :: "r"(tramp + i) : "memory");
296 asm volatile ("flushp" ::: "memory");
298 closure->cif = cif;
299 closure->fun = fun;
300 closure->user_data = user_data;
302 return FFI_OK;