lj_cconv.h 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. /*
  2. ** C type conversions.
  3. ** Copyright (C) 2005-2023 Mike Pall. See Copyright Notice in luajit.h
  4. */
  5. #ifndef _LJ_CCONV_H
  6. #define _LJ_CCONV_H
  7. #include "lj_obj.h"
  8. #include "lj_ctype.h"
  9. #if LJ_HASFFI
  10. /* Compressed C type index. ORDER CCX. */
  11. enum {
  12. CCX_B, /* Bool. */
  13. CCX_I, /* Integer. */
  14. CCX_F, /* Floating-point number. */
  15. CCX_C, /* Complex. */
  16. CCX_V, /* Vector. */
  17. CCX_P, /* Pointer. */
  18. CCX_A, /* Refarray. */
  19. CCX_S /* Struct/union. */
  20. };
  21. /* Convert C type info to compressed C type index. ORDER CT. ORDER CCX. */
  22. static LJ_AINLINE uint32_t cconv_idx(CTInfo info)
  23. {
  24. uint32_t idx = ((info >> 26) & 15u); /* Dispatch bits. */
  25. lj_assertX(ctype_type(info) <= CT_MAYCONVERT,
  26. "cannot convert ctype %08x", info);
  27. #if LJ_64
  28. idx = ((uint32_t)(U64x(f436fff5,fff7f021) >> 4*idx) & 15u);
  29. #else
  30. idx = (((idx < 8 ? 0xfff7f021u : 0xf436fff5) >> 4*(idx & 7u)) & 15u);
  31. #endif
  32. lj_assertX(idx < 8, "cannot convert ctype %08x", info);
  33. return idx;
  34. }
  35. #define cconv_idx2(dinfo, sinfo) \
  36. ((cconv_idx((dinfo)) << 3) + cconv_idx((sinfo)))
  37. #define CCX(dst, src) ((CCX_##dst << 3) + CCX_##src)
  38. /* Conversion flags. */
  39. #define CCF_CAST 0x00000001u
  40. #define CCF_FROMTV 0x00000002u
  41. #define CCF_SAME 0x00000004u
  42. #define CCF_IGNQUAL 0x00000008u
  43. #define CCF_ARG_SHIFT 8
  44. #define CCF_ARG(n) ((n) << CCF_ARG_SHIFT)
  45. #define CCF_GETARG(f) ((f) >> CCF_ARG_SHIFT)
  46. LJ_FUNC int lj_cconv_compatptr(CTState *cts, CType *d, CType *s, CTInfo flags);
  47. LJ_FUNC void lj_cconv_ct_ct(CTState *cts, CType *d, CType *s,
  48. uint8_t *dp, uint8_t *sp, CTInfo flags);
  49. LJ_FUNC int lj_cconv_tv_ct(CTState *cts, CType *s, CTypeID sid,
  50. TValue *o, uint8_t *sp);
  51. LJ_FUNC int lj_cconv_tv_bf(CTState *cts, CType *s, TValue *o, uint8_t *sp);
  52. LJ_FUNC void lj_cconv_ct_tv(CTState *cts, CType *d,
  53. uint8_t *dp, TValue *o, CTInfo flags);
  54. LJ_FUNC void lj_cconv_bf_tv(CTState *cts, CType *d, uint8_t *dp, TValue *o);
  55. LJ_FUNC int lj_cconv_multi_init(CTState *cts, CType *d, TValue *o);
  56. LJ_FUNC void lj_cconv_ct_init(CTState *cts, CType *d, CTSize sz,
  57. uint8_t *dp, TValue *o, MSize len);
  58. #endif
  59. #endif