lj_ccall.c 36 KB

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