1 /*
2 ** FFI C call handling.
3 ** Copyright (C) 2005-2016 Mike Pall. See Copyright Notice in luajit.h
4 */
5
6 #include "lj_obj.h"
7
8 #if LJ_HASFFI
9
10 #include "lj_gc.h"
11 #include "lj_err.h"
12 #include "lj_tab.h"
13 #include "lj_ctype.h"
14 #include "lj_cconv.h"
15 #include "lj_cdata.h"
16 #include "lj_ccall.h"
17 #include "lj_trace.h"
18
19 /* Target-specific handling of register arguments. */
20 #if LJ_TARGET_X86
21 /* -- x86 calling conventions --------------------------------------------- */
22
23 #if LJ_ABI_WIN
24
25 #define CCALL_HANDLE_STRUCTRET \
26 /* Return structs bigger than 8 by reference (on stack only). */ \
27 cc->retref = (sz > 8); \
28 if (cc->retref) cc->stack[nsp++] = (GPRArg)dp;
29
30 #define CCALL_HANDLE_COMPLEXRET CCALL_HANDLE_STRUCTRET
31
32 #else
33
34 #if LJ_TARGET_OSX
35
36 #define CCALL_HANDLE_STRUCTRET \
37 /* Return structs of size 1, 2, 4 or 8 in registers. */ \
38 cc->retref = !(sz == 1 || sz == 2 || sz == 4 || sz == 8); \
39 if (cc->retref) { \
40 if (ngpr < maxgpr) \
41 cc->gpr[ngpr++] = (GPRArg)dp; \
42 else \
43 cc->stack[nsp++] = (GPRArg)dp; \
44 } else { /* Struct with single FP field ends up in FPR. */ \
45 cc->resx87 = ccall_classify_struct(cts, ctr); \
46 }
47
48 #define CCALL_HANDLE_STRUCTRET2 \
49 if (cc->resx87) sp = (uint8_t *)&cc->fpr[0]; \
50 memcpy(dp, sp, ctr->size);
51
52 #else
53
54 #define CCALL_HANDLE_STRUCTRET \
55 cc->retref = 1; /* Return all structs by reference (in reg or on stack). */ \
56 if (ngpr < maxgpr) \
57 cc->gpr[ngpr++] = (GPRArg)dp; \
58 else \
59 cc->stack[nsp++] = (GPRArg)dp;
60
61 #endif
62
63 #define CCALL_HANDLE_COMPLEXRET \
64 /* Return complex float in GPRs and complex double by reference. */ \
65 cc->retref = (sz > 8); \
66 if (cc->retref) { \
67 if (ngpr < maxgpr) \
68 cc->gpr[ngpr++] = (GPRArg)dp; \
69 else \
70 cc->stack[nsp++] = (GPRArg)dp; \
71 }
72
73 #endif
74
75 #define CCALL_HANDLE_COMPLEXRET2 \
76 if (!cc->retref) \
77 *(int64_t *)dp = *(int64_t *)sp; /* Copy complex float from GPRs. */
78
79 #define CCALL_HANDLE_STRUCTARG \
80 ngpr = maxgpr; /* Pass all structs by value on the stack. */
81
82 #define CCALL_HANDLE_COMPLEXARG \
83 isfp = 1; /* Pass complex by value on stack. */
84
85 #define CCALL_HANDLE_REGARG \
86 if (!isfp) { /* Only non-FP values may be passed in registers. */ \
87 if (n > 1) { /* Anything > 32 bit is passed on the stack. */ \
88 if (!LJ_ABI_WIN) ngpr = maxgpr; /* Prevent reordering. */ \
89 } else if (ngpr + 1 <= maxgpr) { \
90 dp = &cc->gpr[ngpr]; \
91 ngpr += n; \
92 goto done; \
93 } \
94 }
95
96 #elif LJ_TARGET_X64 && LJ_ABI_WIN
97 /* -- Windows/x64 calling conventions ------------------------------------- */
98
99 #define CCALL_HANDLE_STRUCTRET \
100 /* Return structs of size 1, 2, 4 or 8 in a GPR. */ \
101 cc->retref = !(sz == 1 || sz == 2 || sz == 4 || sz == 8); \
102 if (cc->retref) cc->gpr[ngpr++] = (GPRArg)dp;
103
104 #define CCALL_HANDLE_COMPLEXRET CCALL_HANDLE_STRUCTRET
105
106 #define CCALL_HANDLE_COMPLEXRET2 \
107 if (!cc->retref) \
108 *(int64_t *)dp = *(int64_t *)sp; /* Copy complex float from GPRs. */
109
110 #define CCALL_HANDLE_STRUCTARG \
111 /* Pass structs of size 1, 2, 4 or 8 in a GPR by value. */ \
112 if (!(sz == 1 || sz == 2 || sz == 4 || sz == 8)) { \
113 rp = cdataptr(lj_cdata_new(cts, did, sz)); \
114 sz = CTSIZE_PTR; /* Pass all other structs by reference. */ \
115 }
116
117 #define CCALL_HANDLE_COMPLEXARG \
118 /* Pass complex float in a GPR and complex double by reference. */ \
119 if (sz != 2*sizeof(float)) { \
120 rp = cdataptr(lj_cdata_new(cts, did, sz)); \
121 sz = CTSIZE_PTR; \
122 }
123
124 /* Windows/x64 argument registers are strictly positional (use ngpr). */
125 #define CCALL_HANDLE_REGARG \
126 if (isfp) { \
127 if (ngpr < maxgpr) { dp = &cc->fpr[ngpr++]; nfpr = ngpr; goto done; } \
128 } else { \
129 if (ngpr < maxgpr) { dp = &cc->gpr[ngpr++]; goto done; } \
130 }
131
132 #elif LJ_TARGET_X64
133 /* -- POSIX/x64 calling conventions --------------------------------------- */
134
135 #define CCALL_HANDLE_STRUCTRET \
136 int rcl[2]; rcl[0] = rcl[1] = 0; \
137 if (ccall_classify_struct(cts, ctr, rcl, 0)) { \
138 cc->retref = 1; /* Return struct by reference. */ \
139 cc->gpr[ngpr++] = (GPRArg)dp; \
140 } else { \
141 cc->retref = 0; /* Return small structs in registers. */ \
142 }
143
144 #define CCALL_HANDLE_STRUCTRET2 \
145 int rcl[2]; rcl[0] = rcl[1] = 0; \
146 ccall_classify_struct(cts, ctr, rcl, 0); \
147 ccall_struct_ret(cc, rcl, dp, ctr->size);
148
149 #define CCALL_HANDLE_COMPLEXRET \
150 /* Complex values are returned in one or two FPRs. */ \
151 cc->retref = 0;
152
153 #define CCALL_HANDLE_COMPLEXRET2 \
154 if (ctr->size == 2*sizeof(float)) { /* Copy complex float from FPR. */ \
155 *(int64_t *)dp = cc->fpr[0].l[0]; \
156 } else { /* Copy non-contiguous complex double from FPRs. */ \
157 ((int64_t *)dp)[0] = cc->fpr[0].l[0]; \
158 ((int64_t *)dp)[1] = cc->fpr[1].l[0]; \
159 }
160
161 #define CCALL_HANDLE_STRUCTARG \
162 int rcl[2]; rcl[0] = rcl[1] = 0; \
163 if (!ccall_classify_struct(cts, d, rcl, 0)) { \
164 cc->nsp = nsp; cc->ngpr = ngpr; cc->nfpr = nfpr; \
165 if (ccall_struct_arg(cc, cts, d, rcl, o, narg)) goto err_nyi; \
166 nsp = cc->nsp; ngpr = cc->ngpr; nfpr = cc->nfpr; \
167 continue; \
168 } /* Pass all other structs by value on stack. */
169
170 #define CCALL_HANDLE_COMPLEXARG \
171 isfp = 2; /* Pass complex in FPRs or on stack. Needs postprocessing. */
172
173 #define CCALL_HANDLE_REGARG \
174 if (isfp) { /* Try to pass argument in FPRs. */ \
175 int n2 = ctype_isvector(d->info) ? 1 : n; \
176 if (nfpr + n2 <= CCALL_NARG_FPR) { \
177 dp = &cc->fpr[nfpr]; \
178 nfpr += n2; \
179 goto done; \
180 } \
181 } else { /* Try to pass argument in GPRs. */ \
182 /* Note that reordering is explicitly allowed in the x64 ABI. */ \
183 if (n <= 2 && ngpr + n <= maxgpr) { \
184 dp = &cc->gpr[ngpr]; \
185 ngpr += n; \
186 goto done; \
187 } \
188 }
189
190 #elif LJ_TARGET_ARM
191 /* -- ARM calling conventions --------------------------------------------- */
192
193 #if LJ_ABI_SOFTFP
194
195 #define CCALL_HANDLE_STRUCTRET \
196 /* Return structs of size <= 4 in a GPR. */ \
197 cc->retref = !(sz <= 4); \
198 if (cc->retref) cc->gpr[ngpr++] = (GPRArg)dp;
199
200 #define CCALL_HANDLE_COMPLEXRET \
201 cc->retref = 1; /* Return all complex values by reference. */ \
202 cc->gpr[ngpr++] = (GPRArg)dp;
203
204 #define CCALL_HANDLE_COMPLEXRET2 \
205 UNUSED(dp); /* Nothing to do. */
206
207 #define CCALL_HANDLE_STRUCTARG \
208 /* Pass all structs by value in registers and/or on the stack. */
209
210 #define CCALL_HANDLE_COMPLEXARG \
211 /* Pass complex by value in 2 or 4 GPRs. */
212
213 #define CCALL_HANDLE_REGARG_FP1
214 #define CCALL_HANDLE_REGARG_FP2
215
216 #else
217
218 #define CCALL_HANDLE_STRUCTRET \
219 cc->retref = !ccall_classify_struct(cts, ctr, ct); \
220 if (cc->retref) cc->gpr[ngpr++] = (GPRArg)dp;
221
222 #define CCALL_HANDLE_STRUCTRET2 \
223 if (ccall_classify_struct(cts, ctr, ct) > 1) sp = (uint8_t *)&cc->fpr[0]; \
224 memcpy(dp, sp, ctr->size);
225
226 #define CCALL_HANDLE_COMPLEXRET \
227 if (!(ct->info & CTF_VARARG)) cc->retref = 0; /* Return complex in FPRs. */
228
229 #define CCALL_HANDLE_COMPLEXRET2 \
230 if (!(ct->info & CTF_VARARG)) memcpy(dp, &cc->fpr[0], ctr->size);
231
232 #define CCALL_HANDLE_STRUCTARG \
233 isfp = (ccall_classify_struct(cts, d, ct) > 1);
234 /* Pass all structs by value in registers and/or on the stack. */
235
236 #define CCALL_HANDLE_COMPLEXARG \
237 isfp = 1; /* Pass complex by value in FPRs or on stack. */
238
239 #define CCALL_HANDLE_REGARG_FP1 \
240 if (isfp && !(ct->info & CTF_VARARG)) { \
241 if ((d->info & CTF_ALIGN) > CTALIGN_PTR) { \
242 if (nfpr + (n >> 1) <= CCALL_NARG_FPR) { \
243 dp = &cc->fpr[nfpr]; \
244 nfpr += (n >> 1); \
245 goto done; \
246 } \
247 } else { \
248 if (sz > 1 && fprodd != nfpr) fprodd = 0; \
249 if (fprodd) { \
250 if (2*nfpr+n <= 2*CCALL_NARG_FPR+1) { \
251 dp = (void *)&cc->fpr[fprodd-1].f[1]; \
252 nfpr += (n >> 1); \
253 if ((n & 1)) fprodd = 0; else fprodd = nfpr-1; \
254 goto done; \
255 } \
256 } else { \
257 if (2*nfpr+n <= 2*CCALL_NARG_FPR) { \
258 dp = (void *)&cc->fpr[nfpr]; \
259 nfpr += (n >> 1); \
260 if ((n & 1)) fprodd = ++nfpr; else fprodd = 0; \
261 goto done; \
262 } \
263 } \
264 } \
265 fprodd = 0; /* No reordering after the first FP value is on stack. */ \
266 } else {
267
268 #define CCALL_HANDLE_REGARG_FP2 }
269
270 #endif
271
272 #define CCALL_HANDLE_REGARG \
273 CCALL_HANDLE_REGARG_FP1 \
274 if ((d->info & CTF_ALIGN) > CTALIGN_PTR) { \
275 if (ngpr < maxgpr) \
276 ngpr = (ngpr + 1u) & ~1u; /* Align to regpair. */ \
277 } \
278 if (ngpr < maxgpr) { \
279 dp = &cc->gpr[ngpr]; \
280 if (ngpr + n > maxgpr) { \
281 nsp += ngpr + n - maxgpr; /* Assumes contiguous gpr/stack fields. */ \
282 if (nsp > CCALL_MAXSTACK) goto err_nyi; /* Too many arguments. */ \
283 ngpr = maxgpr; \
284 } else { \
285 ngpr += n; \
286 } \
287 goto done; \
288 } CCALL_HANDLE_REGARG_FP2
289
290 #define CCALL_HANDLE_RET \
291 if ((ct->info & CTF_VARARG)) sp = (uint8_t *)&cc->gpr[0];
292
293 #elif LJ_TARGET_ARM64
294 /* -- ARM64 calling conventions ------------------------------------------- */
295
296 #define CCALL_HANDLE_STRUCTRET \
297 cc->retref = !ccall_classify_struct(cts, ctr); \
298 if (cc->retref) cc->retp = dp;
299
300 #define CCALL_HANDLE_STRUCTRET2 \
301 unsigned int cl = ccall_classify_struct(cts, ctr); \
302 if ((cl & 4)) { /* Combine float HFA from separate registers. */ \
303 CTSize i = (cl >> 8) - 1; \
304 do { ((uint32_t *)dp)[i] = cc->fpr[i].u32; } while (i--); \
305 } else { \
306 if (cl > 1) sp = (uint8_t *)&cc->fpr[0]; \
307 memcpy(dp, sp, ctr->size); \
308 }
309
310 #define CCALL_HANDLE_COMPLEXRET \
311 /* Complex values are returned in one or two FPRs. */ \
312 cc->retref = 0;
313
314 #define CCALL_HANDLE_COMPLEXRET2 \
315 if (ctr->size == 2*sizeof(float)) { /* Copy complex float from FPRs. */ \
316 ((float *)dp)[0] = cc->fpr[0].f; \
317 ((float *)dp)[1] = cc->fpr[1].f; \
318 } else { /* Copy complex double from FPRs. */ \
319 ((double *)dp)[0] = cc->fpr[0].d; \
320 ((double *)dp)[1] = cc->fpr[1].d; \
321 }
322
323 #define CCALL_HANDLE_STRUCTARG \
324 unsigned int cl = ccall_classify_struct(cts, d); \
325 if (cl == 0) { /* Pass struct by reference. */ \
326 rp = cdataptr(lj_cdata_new(cts, did, sz)); \
327 sz = CTSIZE_PTR; \
328 } else if (cl > 1) { /* Pass struct in FPRs or on stack. */ \
329 isfp = (cl & 4) ? 2 : 1; \
330 } /* else: Pass struct in GPRs or on stack. */
331
332 #define CCALL_HANDLE_COMPLEXARG \
333 /* Pass complex by value in separate (!) FPRs or on stack. */ \
334 isfp = ctr->size == 2*sizeof(float) ? 2 : 1;
335
336 #define CCALL_HANDLE_REGARG \
337 if (LJ_TARGET_IOS && isva) { \
338 /* IOS: All variadic arguments are on the stack. */ \
339 } else if (isfp) { /* Try to pass argument in FPRs. */ \
340 int n2 = ctype_isvector(d->info) ? 1 : n*isfp; \
341 if (nfpr + n2 <= CCALL_NARG_FPR) { \
342 dp = &cc->fpr[nfpr]; \
343 nfpr += n2; \
344 goto done; \
345 } else { \
346 nfpr = CCALL_NARG_FPR; /* Prevent reordering. */ \
347 if (LJ_TARGET_IOS && d->size < 8) goto err_nyi; \
348 } \
349 } else { /* Try to pass argument in GPRs. */ \
350 if (!LJ_TARGET_IOS && (d->info & CTF_ALIGN) > CTALIGN_PTR) \
351 ngpr = (ngpr + 1u) & ~1u; /* Align to regpair. */ \
352 if (ngpr + n <= maxgpr) { \
353 dp = &cc->gpr[ngpr]; \
354 ngpr += n; \
355 goto done; \
356 } else { \
357 ngpr = maxgpr; /* Prevent reordering. */ \
358 if (LJ_TARGET_IOS && d->size < 8) goto err_nyi; \
359 } \
360 }
361
362 #elif LJ_TARGET_PPC
363 /* -- PPC calling conventions --------------------------------------------- */
364
365 #define CCALL_HANDLE_STRUCTRET \
366 cc->retref = 1; /* Return all structs by reference. */ \
367 cc->gpr[ngpr++] = (GPRArg)dp;
368
369 #define CCALL_HANDLE_COMPLEXRET \
370 /* Complex values are returned in 2 or 4 GPRs. */ \
371 cc->retref = 0;
372
373 #define CCALL_HANDLE_COMPLEXRET2 \
374 memcpy(dp, sp, ctr->size); /* Copy complex from GPRs. */
375
376 #define CCALL_HANDLE_STRUCTARG \
377 rp = cdataptr(lj_cdata_new(cts, did, sz)); \
378 sz = CTSIZE_PTR; /* Pass all structs by reference. */
379
380 #define CCALL_HANDLE_COMPLEXARG \
381 /* Pass complex by value in 2 or 4 GPRs. */
382
383 #define CCALL_HANDLE_REGARG \
384 if (isfp) { /* Try to pass argument in FPRs. */ \
385 if (nfpr + 1 <= CCALL_NARG_FPR) { \
386 dp = &cc->fpr[nfpr]; \
387 nfpr += 1; \
388 d = ctype_get(cts, CTID_DOUBLE); /* FPRs always hold doubles. */ \
389 goto done; \
390 } \
391 } else { /* Try to pass argument in GPRs. */ \
392 if (n > 1) { \
393 lua_assert(n == 2 || n == 4); /* int64_t or complex (float). */ \
394 if (ctype_isinteger(d->info)) \
395 ngpr = (ngpr + 1u) & ~1u; /* Align int64_t to regpair. */ \
396 else if (ngpr + n > maxgpr) \
397 ngpr = maxgpr; /* Prevent reordering. */ \
398 } \
399 if (ngpr + n <= maxgpr) { \
400 dp = &cc->gpr[ngpr]; \
401 ngpr += n; \
402 goto done; \
403 } \
404 }
405
406 #define CCALL_HANDLE_RET \
407 if (ctype_isfp(ctr->info) && ctr->size == sizeof(float)) \
408 ctr = ctype_get(cts, CTID_DOUBLE); /* FPRs always hold doubles. */
409
410 #elif LJ_TARGET_MIPS
411 /* -- MIPS calling conventions -------------------------------------------- */
412
413 #define CCALL_HANDLE_STRUCTRET \
414 cc->retref = 1; /* Return all structs by reference. */ \
415 cc->gpr[ngpr++] = (GPRArg)dp;
416
417 #define CCALL_HANDLE_COMPLEXRET \
418 /* Complex values are returned in 1 or 2 FPRs. */ \
419 cc->retref = 0;
420
421 #if LJ_ABI_SOFTFP
422 #define CCALL_HANDLE_COMPLEXRET2 \
423 if (ctr->size == 2*sizeof(float)) { /* Copy complex float from GPRs. */ \
424 ((intptr_t *)dp)[0] = cc->gpr[0]; \
425 ((intptr_t *)dp)[1] = cc->gpr[1]; \
426 } else { /* Copy complex double from GPRs. */ \
427 ((intptr_t *)dp)[0] = cc->gpr[0]; \
428 ((intptr_t *)dp)[1] = cc->gpr[1]; \
429 ((intptr_t *)dp)[2] = cc->gpr[2]; \
430 ((intptr_t *)dp)[3] = cc->gpr[3]; \
431 }
432 #else
433 #define CCALL_HANDLE_COMPLEXRET2 \
434 if (ctr->size == 2*sizeof(float)) { /* Copy complex float from FPRs. */ \
435 ((float *)dp)[0] = cc->fpr[0].f; \
436 ((float *)dp)[1] = cc->fpr[1].f; \
437 } else { /* Copy complex double from FPRs. */ \
438 ((double *)dp)[0] = cc->fpr[0].d; \
439 ((double *)dp)[1] = cc->fpr[1].d; \
440 }
441 #endif
442
443 #define CCALL_HANDLE_STRUCTARG \
444 /* Pass all structs by value in registers and/or on the stack. */
445
446 #define CCALL_HANDLE_COMPLEXARG \
447 /* Pass complex by value in 2 or 4 GPRs. */
448
449 #define CCALL_HANDLE_GPR \
450 if ((d->info & CTF_ALIGN) > CTALIGN_PTR) \
451 ngpr = (ngpr + 1u) & ~1u; /* Align to regpair. */ \
452 if (ngpr < maxgpr) { \
453 dp = &cc->gpr[ngpr]; \
454 if (ngpr + n > maxgpr) { \
455 nsp += ngpr + n - maxgpr; /* Assumes contiguous gpr/stack fields. */ \
456 if (nsp > CCALL_MAXSTACK) goto err_nyi; /* Too many arguments. */ \
457 ngpr = maxgpr; \
458 } else { \
459 ngpr += n; \
460 } \
461 goto done; \
462 }
463
464 #if !LJ_ABI_SOFTFP /* MIPS32 hard-float */
465 #define CCALL_HANDLE_REGARG \
466 if (isfp && nfpr < CCALL_NARG_FPR && !(ct->info & CTF_VARARG)) { \
467 /* Try to pass argument in FPRs. */ \
468 dp = n == 1 ? (void *)&cc->fpr[nfpr].f : (void *)&cc->fpr[nfpr].d; \
469 nfpr++; ngpr += n; \
470 goto done; \
471 } else { /* Try to pass argument in GPRs. */ \
472 nfpr = CCALL_NARG_FPR; \
473 CCALL_HANDLE_GPR \
474 }
475 #else /* MIPS32 soft-float */
476 #define CCALL_HANDLE_REGARG CCALL_HANDLE_GPR
477 #endif
478
479 #if !LJ_ABI_SOFTFP
480 /* On MIPS64 soft-float, position of float return values is endian-dependant. */
481 #define CCALL_HANDLE_RET \
482 if (ctype_isfp(ctr->info) && ctr->size == sizeof(float)) \
483 sp = (uint8_t *)&cc->fpr[0].f;
484 #endif
485
486 #else
487 #error "Missing calling convention definitions for this architecture"
488 #endif
489
490 #ifndef CCALL_HANDLE_STRUCTRET2
491 #define CCALL_HANDLE_STRUCTRET2 \
492 memcpy(dp, sp, ctr->size); /* Copy struct return value from GPRs. */
493 #endif
494
495 /* -- x86 OSX ABI struct classification ----------------------------------- */
496
497 #if LJ_TARGET_X86 && LJ_TARGET_OSX
498
499 /* Check for struct with single FP field. */
ccall_classify_struct(CTState * cts,CType * ct)500 static int ccall_classify_struct(CTState *cts, CType *ct)
501 {
502 CTSize sz = ct->size;
503 if (!(sz == sizeof(float) || sz == sizeof(double))) return 0;
504 if ((ct->info & CTF_UNION)) return 0;
505 while (ct->sib) {
506 ct = ctype_get(cts, ct->sib);
507 if (ctype_isfield(ct->info)) {
508 CType *sct = ctype_rawchild(cts, ct);
509 if (ctype_isfp(sct->info)) {
510 if (sct->size == sz)
511 return (sz >> 2); /* Return 1 for float or 2 for double. */
512 } else if (ctype_isstruct(sct->info)) {
513 if (sct->size)
514 return ccall_classify_struct(cts, sct);
515 } else {
516 break;
517 }
518 } else if (ctype_isbitfield(ct->info)) {
519 break;
520 } else if (ctype_isxattrib(ct->info, CTA_SUBTYPE)) {
521 CType *sct = ctype_rawchild(cts, ct);
522 if (sct->size)
523 return ccall_classify_struct(cts, sct);
524 }
525 }
526 return 0;
527 }
528
529 #endif
530
531 /* -- x64 struct classification ------------------------------------------- */
532
533 #if LJ_TARGET_X64 && !LJ_ABI_WIN
534
535 /* Register classes for x64 struct classification. */
536 #define CCALL_RCL_INT 1
537 #define CCALL_RCL_SSE 2
538 #define CCALL_RCL_MEM 4
539 /* NYI: classify vectors. */
540
541 static int ccall_classify_struct(CTState *cts, CType *ct, int *rcl, CTSize ofs);
542
543 /* Classify a C type. */
ccall_classify_ct(CTState * cts,CType * ct,int * rcl,CTSize ofs)544 static void ccall_classify_ct(CTState *cts, CType *ct, int *rcl, CTSize ofs)
545 {
546 if (ctype_isarray(ct->info)) {
547 CType *cct = ctype_rawchild(cts, ct);
548 CTSize eofs, esz = cct->size, asz = ct->size;
549 for (eofs = 0; eofs < asz; eofs += esz)
550 ccall_classify_ct(cts, cct, rcl, ofs+eofs);
551 } else if (ctype_isstruct(ct->info)) {
552 ccall_classify_struct(cts, ct, rcl, ofs);
553 } else {
554 int cl = ctype_isfp(ct->info) ? CCALL_RCL_SSE : CCALL_RCL_INT;
555 lua_assert(ctype_hassize(ct->info));
556 if ((ofs & (ct->size-1))) cl = CCALL_RCL_MEM; /* Unaligned. */
557 rcl[(ofs >= 8)] |= cl;
558 }
559 }
560
561 /* Recursively classify a struct based on its fields. */
ccall_classify_struct(CTState * cts,CType * ct,int * rcl,CTSize ofs)562 static int ccall_classify_struct(CTState *cts, CType *ct, int *rcl, CTSize ofs)
563 {
564 if (ct->size > 16) return CCALL_RCL_MEM; /* Too big, gets memory class. */
565 while (ct->sib) {
566 CTSize fofs;
567 ct = ctype_get(cts, ct->sib);
568 fofs = ofs+ct->size;
569 if (ctype_isfield(ct->info))
570 ccall_classify_ct(cts, ctype_rawchild(cts, ct), rcl, fofs);
571 else if (ctype_isbitfield(ct->info))
572 rcl[(fofs >= 8)] |= CCALL_RCL_INT; /* NYI: unaligned bitfields? */
573 else if (ctype_isxattrib(ct->info, CTA_SUBTYPE))
574 ccall_classify_struct(cts, ctype_rawchild(cts, ct), rcl, fofs);
575 }
576 return ((rcl[0]|rcl[1]) & CCALL_RCL_MEM); /* Memory class? */
577 }
578
579 /* Try to split up a small struct into registers. */
ccall_struct_reg(CCallState * cc,GPRArg * dp,int * rcl)580 static int ccall_struct_reg(CCallState *cc, GPRArg *dp, int *rcl)
581 {
582 MSize ngpr = cc->ngpr, nfpr = cc->nfpr;
583 uint32_t i;
584 for (i = 0; i < 2; i++) {
585 lua_assert(!(rcl[i] & CCALL_RCL_MEM));
586 if ((rcl[i] & CCALL_RCL_INT)) { /* Integer class takes precedence. */
587 if (ngpr >= CCALL_NARG_GPR) return 1; /* Register overflow. */
588 cc->gpr[ngpr++] = dp[i];
589 } else if ((rcl[i] & CCALL_RCL_SSE)) {
590 if (nfpr >= CCALL_NARG_FPR) return 1; /* Register overflow. */
591 cc->fpr[nfpr++].l[0] = dp[i];
592 }
593 }
594 cc->ngpr = ngpr; cc->nfpr = nfpr;
595 return 0; /* Ok. */
596 }
597
598 /* Pass a small struct argument. */
ccall_struct_arg(CCallState * cc,CTState * cts,CType * d,int * rcl,TValue * o,int narg)599 static int ccall_struct_arg(CCallState *cc, CTState *cts, CType *d, int *rcl,
600 TValue *o, int narg)
601 {
602 GPRArg dp[2];
603 dp[0] = dp[1] = 0;
604 /* Convert to temp. struct. */
605 lj_cconv_ct_tv(cts, d, (uint8_t *)dp, o, CCF_ARG(narg));
606 if (ccall_struct_reg(cc, dp, rcl)) { /* Register overflow? Pass on stack. */
607 MSize nsp = cc->nsp, n = rcl[1] ? 2 : 1;
608 if (nsp + n > CCALL_MAXSTACK) return 1; /* Too many arguments. */
609 cc->nsp = nsp + n;
610 memcpy(&cc->stack[nsp], dp, n*CTSIZE_PTR);
611 }
612 return 0; /* Ok. */
613 }
614
615 /* Combine returned small struct. */
ccall_struct_ret(CCallState * cc,int * rcl,uint8_t * dp,CTSize sz)616 static void ccall_struct_ret(CCallState *cc, int *rcl, uint8_t *dp, CTSize sz)
617 {
618 GPRArg sp[2];
619 MSize ngpr = 0, nfpr = 0;
620 uint32_t i;
621 for (i = 0; i < 2; i++) {
622 if ((rcl[i] & CCALL_RCL_INT)) { /* Integer class takes precedence. */
623 sp[i] = cc->gpr[ngpr++];
624 } else if ((rcl[i] & CCALL_RCL_SSE)) {
625 sp[i] = cc->fpr[nfpr++].l[0];
626 }
627 }
628 memcpy(dp, sp, sz);
629 }
630 #endif
631
632 /* -- ARM hard-float ABI struct classification ---------------------------- */
633
634 #if LJ_TARGET_ARM && !LJ_ABI_SOFTFP
635
636 /* Classify a struct based on its fields. */
ccall_classify_struct(CTState * cts,CType * ct,CType * ctf)637 static unsigned int ccall_classify_struct(CTState *cts, CType *ct, CType *ctf)
638 {
639 CTSize sz = ct->size;
640 unsigned int r = 0, n = 0, isu = (ct->info & CTF_UNION);
641 if ((ctf->info & CTF_VARARG)) goto noth;
642 while (ct->sib) {
643 CType *sct;
644 ct = ctype_get(cts, ct->sib);
645 if (ctype_isfield(ct->info)) {
646 sct = ctype_rawchild(cts, ct);
647 if (ctype_isfp(sct->info)) {
648 r |= sct->size;
649 if (!isu) n++; else if (n == 0) n = 1;
650 } else if (ctype_iscomplex(sct->info)) {
651 r |= (sct->size >> 1);
652 if (!isu) n += 2; else if (n < 2) n = 2;
653 } else if (ctype_isstruct(sct->info)) {
654 goto substruct;
655 } else {
656 goto noth;
657 }
658 } else if (ctype_isbitfield(ct->info)) {
659 goto noth;
660 } else if (ctype_isxattrib(ct->info, CTA_SUBTYPE)) {
661 sct = ctype_rawchild(cts, ct);
662 substruct:
663 if (sct->size > 0) {
664 unsigned int s = ccall_classify_struct(cts, sct, ctf);
665 if (s <= 1) goto noth;
666 r |= (s & 255);
667 if (!isu) n += (s >> 8); else if (n < (s >>8)) n = (s >> 8);
668 }
669 }
670 }
671 if ((r == 4 || r == 8) && n <= 4)
672 return r + (n << 8);
673 noth: /* Not a homogeneous float/double aggregate. */
674 return (sz <= 4); /* Return structs of size <= 4 in a GPR. */
675 }
676
677 #endif
678
679 /* -- ARM64 ABI struct classification ------------------------------------- */
680
681 #if LJ_TARGET_ARM64
682
683 /* Classify a struct based on its fields. */
ccall_classify_struct(CTState * cts,CType * ct)684 static unsigned int ccall_classify_struct(CTState *cts, CType *ct)
685 {
686 CTSize sz = ct->size;
687 unsigned int r = 0, n = 0, isu = (ct->info & CTF_UNION);
688 while (ct->sib) {
689 CType *sct;
690 ct = ctype_get(cts, ct->sib);
691 if (ctype_isfield(ct->info)) {
692 sct = ctype_rawchild(cts, ct);
693 if (ctype_isfp(sct->info)) {
694 r |= sct->size;
695 if (!isu) n++; else if (n == 0) n = 1;
696 } else if (ctype_iscomplex(sct->info)) {
697 r |= (sct->size >> 1);
698 if (!isu) n += 2; else if (n < 2) n = 2;
699 } else if (ctype_isstruct(sct->info)) {
700 goto substruct;
701 } else {
702 goto noth;
703 }
704 } else if (ctype_isbitfield(ct->info)) {
705 goto noth;
706 } else if (ctype_isxattrib(ct->info, CTA_SUBTYPE)) {
707 sct = ctype_rawchild(cts, ct);
708 substruct:
709 if (sct->size > 0) {
710 unsigned int s = ccall_classify_struct(cts, sct);
711 if (s <= 1) goto noth;
712 r |= (s & 255);
713 if (!isu) n += (s >> 8); else if (n < (s >>8)) n = (s >> 8);
714 }
715 }
716 }
717 if ((r == 4 || r == 8) && n <= 4)
718 return r + (n << 8);
719 noth: /* Not a homogeneous float/double aggregate. */
720 return (sz <= 16); /* Return structs of size <= 16 in GPRs. */
721 }
722
723 #endif
724
725 /* -- Common C call handling ---------------------------------------------- */
726
727 /* Infer the destination CTypeID for a vararg argument. */
lj_ccall_ctid_vararg(CTState * cts,cTValue * o)728 CTypeID lj_ccall_ctid_vararg(CTState *cts, cTValue *o)
729 {
730 if (tvisnumber(o)) {
731 return CTID_DOUBLE;
732 } else if (tviscdata(o)) {
733 CTypeID id = cdataV(o)->ctypeid;
734 CType *s = ctype_get(cts, id);
735 if (ctype_isrefarray(s->info)) {
736 return lj_ctype_intern(cts,
737 CTINFO(CT_PTR, CTALIGN_PTR|ctype_cid(s->info)), CTSIZE_PTR);
738 } else if (ctype_isstruct(s->info) || ctype_isfunc(s->info)) {
739 /* NYI: how to pass a struct by value in a vararg argument? */
740 return lj_ctype_intern(cts, CTINFO(CT_PTR, CTALIGN_PTR|id), CTSIZE_PTR);
741 } else if (ctype_isfp(s->info) && s->size == sizeof(float)) {
742 return CTID_DOUBLE;
743 } else {
744 return id;
745 }
746 } else if (tvisstr(o)) {
747 return CTID_P_CCHAR;
748 } else if (tvisbool(o)) {
749 return CTID_BOOL;
750 } else {
751 return CTID_P_VOID;
752 }
753 }
754
755 /* Setup arguments for C call. */
ccall_set_args(lua_State * L,CTState * cts,CType * ct,CCallState * cc)756 static int ccall_set_args(lua_State *L, CTState *cts, CType *ct,
757 CCallState *cc)
758 {
759 int gcsteps = 0;
760 TValue *o, *top = L->top;
761 CTypeID fid;
762 CType *ctr;
763 MSize maxgpr, ngpr = 0, nsp = 0, narg;
764 #if CCALL_NARG_FPR
765 MSize nfpr = 0;
766 #if LJ_TARGET_ARM
767 MSize fprodd = 0;
768 #endif
769 #endif
770
771 /* Clear unused regs to get some determinism in case of misdeclaration. */
772 memset(cc->gpr, 0, sizeof(cc->gpr));
773 #if CCALL_NUM_FPR
774 memset(cc->fpr, 0, sizeof(cc->fpr));
775 #endif
776
777 #if LJ_TARGET_X86
778 /* x86 has several different calling conventions. */
779 cc->resx87 = 0;
780 switch (ctype_cconv(ct->info)) {
781 case CTCC_FASTCALL: maxgpr = 2; break;
782 case CTCC_THISCALL: maxgpr = 1; break;
783 default: maxgpr = 0; break;
784 }
785 #else
786 maxgpr = CCALL_NARG_GPR;
787 #endif
788
789 /* Perform required setup for some result types. */
790 ctr = ctype_rawchild(cts, ct);
791 if (ctype_isvector(ctr->info)) {
792 if (!(CCALL_VECTOR_REG && (ctr->size == 8 || ctr->size == 16)))
793 goto err_nyi;
794 } else if (ctype_iscomplex(ctr->info) || ctype_isstruct(ctr->info)) {
795 /* Preallocate cdata object and anchor it after arguments. */
796 CTSize sz = ctr->size;
797 GCcdata *cd = lj_cdata_new(cts, ctype_cid(ct->info), sz);
798 void *dp = cdataptr(cd);
799 setcdataV(L, L->top++, cd);
800 if (ctype_isstruct(ctr->info)) {
801 CCALL_HANDLE_STRUCTRET
802 } else {
803 CCALL_HANDLE_COMPLEXRET
804 }
805 #if LJ_TARGET_X86
806 } else if (ctype_isfp(ctr->info)) {
807 cc->resx87 = ctr->size == sizeof(float) ? 1 : 2;
808 #endif
809 }
810
811 /* Skip initial attributes. */
812 fid = ct->sib;
813 while (fid) {
814 CType *ctf = ctype_get(cts, fid);
815 if (!ctype_isattrib(ctf->info)) break;
816 fid = ctf->sib;
817 }
818
819 /* Walk through all passed arguments. */
820 for (o = L->base+1, narg = 1; o < top; o++, narg++) {
821 CTypeID did;
822 CType *d;
823 CTSize sz;
824 MSize n, isfp = 0, isva = 0;
825 void *dp, *rp = NULL;
826
827 if (fid) { /* Get argument type from field. */
828 CType *ctf = ctype_get(cts, fid);
829 fid = ctf->sib;
830 lua_assert(ctype_isfield(ctf->info));
831 did = ctype_cid(ctf->info);
832 } else {
833 if (!(ct->info & CTF_VARARG))
834 lj_err_caller(L, LJ_ERR_FFI_NUMARG); /* Too many arguments. */
835 did = lj_ccall_ctid_vararg(cts, o); /* Infer vararg type. */
836 isva = 1;
837 }
838 d = ctype_raw(cts, did);
839 sz = d->size;
840
841 /* Find out how (by value/ref) and where (GPR/FPR) to pass an argument. */
842 if (ctype_isnum(d->info)) {
843 if (sz > 8) goto err_nyi;
844 if ((d->info & CTF_FP))
845 isfp = 1;
846 } else if (ctype_isvector(d->info)) {
847 if (CCALL_VECTOR_REG && (sz == 8 || sz == 16))
848 isfp = 1;
849 else
850 goto err_nyi;
851 } else if (ctype_isstruct(d->info)) {
852 CCALL_HANDLE_STRUCTARG
853 } else if (ctype_iscomplex(d->info)) {
854 CCALL_HANDLE_COMPLEXARG
855 } else {
856 sz = CTSIZE_PTR;
857 }
858 sz = (sz + CTSIZE_PTR-1) & ~(CTSIZE_PTR-1);
859 n = sz / CTSIZE_PTR; /* Number of GPRs or stack slots needed. */
860
861 CCALL_HANDLE_REGARG /* Handle register arguments. */
862
863 /* Otherwise pass argument on stack. */
864 if (CCALL_ALIGN_STACKARG && !rp && (d->info & CTF_ALIGN) > CTALIGN_PTR) {
865 MSize align = (1u << ctype_align(d->info-CTALIGN_PTR)) -1;
866 nsp = (nsp + align) & ~align; /* Align argument on stack. */
867 }
868 if (nsp + n > CCALL_MAXSTACK) { /* Too many arguments. */
869 err_nyi:
870 lj_err_caller(L, LJ_ERR_FFI_NYICALL);
871 }
872 dp = &cc->stack[nsp];
873 nsp += n;
874 isva = 0;
875
876 done:
877 if (rp) { /* Pass by reference. */
878 gcsteps++;
879 *(void **)dp = rp;
880 dp = rp;
881 }
882 lj_cconv_ct_tv(cts, d, (uint8_t *)dp, o, CCF_ARG(narg));
883 /* Extend passed integers to 32 bits at least. */
884 if (ctype_isinteger_or_bool(d->info) && d->size < 4) {
885 if (d->info & CTF_UNSIGNED)
886 *(uint32_t *)dp = d->size == 1 ? (uint32_t)*(uint8_t *)dp :
887 (uint32_t)*(uint16_t *)dp;
888 else
889 *(int32_t *)dp = d->size == 1 ? (int32_t)*(int8_t *)dp :
890 (int32_t)*(int16_t *)dp;
891 }
892 #if LJ_TARGET_X64 && LJ_ABI_WIN
893 if (isva) { /* Windows/x64 mirrors varargs in both register sets. */
894 if (nfpr == ngpr)
895 cc->gpr[ngpr-1] = cc->fpr[ngpr-1].l[0];
896 else
897 cc->fpr[ngpr-1].l[0] = cc->gpr[ngpr-1];
898 }
899 #else
900 UNUSED(isva);
901 #endif
902 #if LJ_TARGET_X64 && !LJ_ABI_WIN
903 if (isfp == 2 && n == 2 && (uint8_t *)dp == (uint8_t *)&cc->fpr[nfpr-2]) {
904 cc->fpr[nfpr-1].d[0] = cc->fpr[nfpr-2].d[1]; /* Split complex double. */
905 cc->fpr[nfpr-2].d[1] = 0;
906 }
907 #elif LJ_TARGET_ARM64
908 if (isfp == 2 && (uint8_t *)dp < (uint8_t *)cc->stack) {
909 /* Split float HFA or complex float into separate registers. */
910 CTSize i = (sz >> 2) - 1;
911 do { ((uint64_t *)dp)[i] = ((uint32_t *)dp)[i]; } while (i--);
912 }
913 #else
914 UNUSED(isfp);
915 #endif
916 }
917 if (fid) lj_err_caller(L, LJ_ERR_FFI_NUMARG); /* Too few arguments. */
918
919 #if LJ_TARGET_X64 || LJ_TARGET_PPC
920 cc->nfpr = nfpr; /* Required for vararg functions. */
921 #endif
922 cc->nsp = nsp;
923 cc->spadj = (CCALL_SPS_FREE + CCALL_SPS_EXTRA)*CTSIZE_PTR;
924 if (nsp > CCALL_SPS_FREE)
925 cc->spadj += (((nsp-CCALL_SPS_FREE)*CTSIZE_PTR + 15u) & ~15u);
926 return gcsteps;
927 }
928
929 /* Get results from C call. */
ccall_get_results(lua_State * L,CTState * cts,CType * ct,CCallState * cc,int * ret)930 static int ccall_get_results(lua_State *L, CTState *cts, CType *ct,
931 CCallState *cc, int *ret)
932 {
933 CType *ctr = ctype_rawchild(cts, ct);
934 uint8_t *sp = (uint8_t *)&cc->gpr[0];
935 if (ctype_isvoid(ctr->info)) {
936 *ret = 0; /* Zero results. */
937 return 0; /* No additional GC step. */
938 }
939 *ret = 1; /* One result. */
940 if (ctype_isstruct(ctr->info)) {
941 /* Return cdata object which is already on top of stack. */
942 if (!cc->retref) {
943 void *dp = cdataptr(cdataV(L->top-1)); /* Use preallocated object. */
944 CCALL_HANDLE_STRUCTRET2
945 }
946 return 1; /* One GC step. */
947 }
948 if (ctype_iscomplex(ctr->info)) {
949 /* Return cdata object which is already on top of stack. */
950 void *dp = cdataptr(cdataV(L->top-1)); /* Use preallocated object. */
951 CCALL_HANDLE_COMPLEXRET2
952 return 1; /* One GC step. */
953 }
954 if (LJ_BE && ctype_isinteger_or_bool(ctr->info) && ctr->size < CTSIZE_PTR)
955 sp += (CTSIZE_PTR - ctr->size);
956 #if CCALL_NUM_FPR
957 if (ctype_isfp(ctr->info) || ctype_isvector(ctr->info))
958 sp = (uint8_t *)&cc->fpr[0];
959 #endif
960 #ifdef CCALL_HANDLE_RET
961 CCALL_HANDLE_RET
962 #endif
963 /* No reference types end up here, so there's no need for the CTypeID. */
964 lua_assert(!(ctype_isrefarray(ctr->info) || ctype_isstruct(ctr->info)));
965 return lj_cconv_tv_ct(cts, ctr, 0, L->top-1, sp);
966 }
967
968 /* Call C function. */
lj_ccall_func(lua_State * L,GCcdata * cd)969 int lj_ccall_func(lua_State *L, GCcdata *cd)
970 {
971 CTState *cts = ctype_cts(L);
972 CType *ct = ctype_raw(cts, cd->ctypeid);
973 CTSize sz = CTSIZE_PTR;
974 if (ctype_isptr(ct->info)) {
975 sz = ct->size;
976 ct = ctype_rawchild(cts, ct);
977 }
978 if (ctype_isfunc(ct->info)) {
979 CCallState cc;
980 int gcsteps, ret;
981 cc.func = (void (*)(void))cdata_getptr(cdataptr(cd), sz);
982 gcsteps = ccall_set_args(L, cts, ct, &cc);
983 ct = (CType *)((intptr_t)ct-(intptr_t)cts->tab);
984 cts->cb.slot = ~0u;
985 lj_vm_ffi_call(&cc);
986 if (cts->cb.slot != ~0u) { /* Blacklist function that called a callback. */
987 TValue tv;
988 setlightudV(&tv, (void *)cc.func);
989 setboolV(lj_tab_set(L, cts->miscmap, &tv), 1);
990 }
991 ct = (CType *)((intptr_t)ct+(intptr_t)cts->tab); /* May be reallocated. */
992 gcsteps += ccall_get_results(L, cts, ct, &cc, &ret);
993 #if LJ_TARGET_X86 && LJ_ABI_WIN
994 /* Automatically detect __stdcall and fix up C function declaration. */
995 if (cc.spadj && ctype_cconv(ct->info) == CTCC_CDECL) {
996 CTF_INSERT(ct->info, CCONV, CTCC_STDCALL);
997 lj_trace_abort(G(L));
998 }
999 #endif
1000 while (gcsteps-- > 0)
1001 lj_gc_check(L);
1002 return ret;
1003 }
1004 return -1; /* Not a function. */
1005 }
1006
1007 #endif
1008