php_ffi.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. /*
  2. +----------------------------------------------------------------------+
  3. | Copyright (c) The PHP Group |
  4. +----------------------------------------------------------------------+
  5. | This source file is subject to version 3.01 of the PHP license, |
  6. | that is bundled with this package in the file LICENSE, and is |
  7. | available through the world-wide-web at the following url: |
  8. | https://www.php.net/license/3_01.txt |
  9. | If you did not receive a copy of the PHP license and are unable to |
  10. | obtain it through the world-wide-web, please send a note to |
  11. | license@php.net so we can mail you a copy immediately. |
  12. +----------------------------------------------------------------------+
  13. | Author: Dmitry Stogov <dmitry@zend.com> |
  14. +----------------------------------------------------------------------+
  15. */
  16. #ifndef PHP_FFI_H
  17. #define PHP_FFI_H
  18. extern zend_module_entry ffi_module_entry;
  19. #define phpext_ffi_ptr &ffi_module_entry
  20. typedef enum _zend_ffi_api_restriction {
  21. ZEND_FFI_DISABLED = 0, /* completely disabled */
  22. ZEND_FFI_ENABLED = 1, /* enabled everywhere */
  23. ZEND_FFI_PRELOAD = 2, /* enabled only in preloaded scripts and CLI */
  24. } zend_ffi_api_restriction;
  25. typedef struct _zend_ffi_type zend_ffi_type;
  26. ZEND_BEGIN_MODULE_GLOBALS(ffi)
  27. zend_ffi_api_restriction restriction;
  28. bool is_cli;
  29. /* predefined ffi_types */
  30. HashTable types;
  31. /* preloading */
  32. char *preload;
  33. HashTable *scopes; /* list of preloaded scopes */
  34. /* callbacks */
  35. HashTable *callbacks;
  36. /* weak type references */
  37. HashTable *weak_types;
  38. /* ffi_parser */
  39. JMP_BUF bailout;
  40. unsigned const char *buf;
  41. unsigned const char *end;
  42. unsigned const char *pos;
  43. unsigned const char *text;
  44. int line;
  45. HashTable *symbols;
  46. HashTable *tags;
  47. bool allow_vla;
  48. bool attribute_parsing;
  49. bool persistent;
  50. uint32_t default_type_attr;
  51. ZEND_END_MODULE_GLOBALS(ffi)
  52. ZEND_EXTERN_MODULE_GLOBALS(ffi)
  53. #ifdef PHP_WIN32
  54. # define PHP_FFI_API __declspec(dllexport)
  55. #elif defined(__GNUC__) && __GNUC__ >= 4
  56. # define PHP_FFI_API __attribute__ ((visibility("default")))
  57. #else
  58. # define PHP_FFI_API
  59. #endif
  60. #define FFI_G(v) ZEND_MODULE_GLOBALS_ACCESSOR(ffi, v)
  61. #define ZEND_FFI_DCL_VOID (1<<0)
  62. #define ZEND_FFI_DCL_CHAR (1<<1)
  63. #define ZEND_FFI_DCL_SHORT (1<<2)
  64. #define ZEND_FFI_DCL_INT (1<<3)
  65. #define ZEND_FFI_DCL_LONG (1<<4)
  66. #define ZEND_FFI_DCL_LONG_LONG (1<<5)
  67. #define ZEND_FFI_DCL_FLOAT (1<<6)
  68. #define ZEND_FFI_DCL_DOUBLE (1<<7)
  69. #define ZEND_FFI_DCL_SIGNED (1<<8)
  70. #define ZEND_FFI_DCL_UNSIGNED (1<<9)
  71. #define ZEND_FFI_DCL_BOOL (1<<10)
  72. #define ZEND_FFI_DCL_COMPLEX (1<<11)
  73. #define ZEND_FFI_DCL_STRUCT (1<<12)
  74. #define ZEND_FFI_DCL_UNION (1<<13)
  75. #define ZEND_FFI_DCL_ENUM (1<<14)
  76. #define ZEND_FFI_DCL_TYPEDEF_NAME (1<<15)
  77. #define ZEND_FFI_DCL_TYPE_SPECIFIERS \
  78. (ZEND_FFI_DCL_VOID|ZEND_FFI_DCL_CHAR|ZEND_FFI_DCL_SHORT \
  79. |ZEND_FFI_DCL_INT|ZEND_FFI_DCL_LONG|ZEND_FFI_DCL_LONG_LONG \
  80. |ZEND_FFI_DCL_FLOAT|ZEND_FFI_DCL_DOUBLE|ZEND_FFI_DCL_SIGNED \
  81. |ZEND_FFI_DCL_UNSIGNED|ZEND_FFI_DCL_BOOL|ZEND_FFI_DCL_COMPLEX \
  82. |ZEND_FFI_DCL_STRUCT|ZEND_FFI_DCL_UNION|ZEND_FFI_DCL_ENUM \
  83. |ZEND_FFI_DCL_TYPEDEF_NAME)
  84. #define ZEND_FFI_DCL_TYPEDEF (1<<16)
  85. #define ZEND_FFI_DCL_EXTERN (1<<17)
  86. #define ZEND_FFI_DCL_STATIC (1<<18)
  87. #define ZEND_FFI_DCL_AUTO (1<<19)
  88. #define ZEND_FFI_DCL_REGISTER (1<<20)
  89. #define ZEND_FFI_DCL_STORAGE_CLASS \
  90. (ZEND_FFI_DCL_TYPEDEF|ZEND_FFI_DCL_EXTERN|ZEND_FFI_DCL_STATIC \
  91. |ZEND_FFI_DCL_AUTO|ZEND_FFI_DCL_REGISTER)
  92. #define ZEND_FFI_DCL_CONST (1<<21)
  93. #define ZEND_FFI_DCL_RESTRICT (1<<22)
  94. #define ZEND_FFI_DCL_VOLATILE (1<<23)
  95. #define ZEND_FFI_DCL_ATOMIC (1<<24)
  96. #define ZEND_FFI_DCL_TYPE_QUALIFIERS \
  97. (ZEND_FFI_DCL_CONST|ZEND_FFI_DCL_RESTRICT|ZEND_FFI_DCL_VOLATILE \
  98. |ZEND_FFI_DCL_ATOMIC)
  99. #define ZEND_FFI_DCL_INLINE (1<<25)
  100. #define ZEND_FFI_DCL_NO_RETURN (1<<26)
  101. #define ZEND_FFI_ABI_DEFAULT 0
  102. #define ZEND_FFI_ABI_CDECL 1 // FFI_DEFAULT_ABI
  103. #define ZEND_FFI_ABI_FASTCALL 2 // FFI_FASTCALL
  104. #define ZEND_FFI_ABI_THISCALL 3 // FFI_THISCALL
  105. #define ZEND_FFI_ABI_STDCALL 4 // FFI_STDCALL
  106. #define ZEND_FFI_ABI_PASCAL 5 // FFI_PASCAL
  107. #define ZEND_FFI_ABI_REGISTER 6 // FFI_REGISTER
  108. #define ZEND_FFI_ABI_MS 7 // FFI_MS_CDECL
  109. #define ZEND_FFI_ABI_SYSV 8 // FFI_SYSV
  110. #define ZEND_FFI_ABI_VECTORCALL 9 // FFI_VECTORCALL
  111. #define ZEND_FFI_ATTR_CONST (1<<0)
  112. #define ZEND_FFI_ATTR_INCOMPLETE_TAG (1<<1)
  113. #define ZEND_FFI_ATTR_VARIADIC (1<<2)
  114. #define ZEND_FFI_ATTR_INCOMPLETE_ARRAY (1<<3)
  115. #define ZEND_FFI_ATTR_VLA (1<<4)
  116. #define ZEND_FFI_ATTR_UNION (1<<5)
  117. #define ZEND_FFI_ATTR_PACKED (1<<6)
  118. #define ZEND_FFI_ATTR_MS_STRUCT (1<<7)
  119. #define ZEND_FFI_ATTR_GCC_STRUCT (1<<8)
  120. #define ZEND_FFI_ATTR_PERSISTENT (1<<9)
  121. #define ZEND_FFI_ATTR_STORED (1<<10)
  122. #define ZEND_FFI_STRUCT_ATTRS \
  123. (ZEND_FFI_ATTR_UNION|ZEND_FFI_ATTR_PACKED|ZEND_FFI_ATTR_MS_STRUCT \
  124. |ZEND_FFI_ATTR_GCC_STRUCT)
  125. #define ZEND_FFI_ENUM_ATTRS \
  126. (ZEND_FFI_ATTR_PACKED)
  127. #define ZEND_FFI_ARRAY_ATTRS \
  128. (ZEND_FFI_ATTR_CONST|ZEND_FFI_ATTR_VLA|ZEND_FFI_ATTR_INCOMPLETE_ARRAY)
  129. #define ZEND_FFI_FUNC_ATTRS \
  130. (ZEND_FFI_ATTR_VARIADIC)
  131. #define ZEND_FFI_POINTER_ATTRS \
  132. (ZEND_FFI_ATTR_CONST)
  133. typedef struct _zend_ffi_dcl {
  134. uint32_t flags;
  135. uint32_t align;
  136. uint16_t attr;
  137. uint16_t abi;
  138. zend_ffi_type *type;
  139. } zend_ffi_dcl;
  140. #define ZEND_FFI_ATTR_INIT {0, 0, 0, 0, NULL}
  141. typedef enum _zend_ffi_val_kind {
  142. ZEND_FFI_VAL_EMPTY,
  143. ZEND_FFI_VAL_ERROR,
  144. ZEND_FFI_VAL_INT32,
  145. ZEND_FFI_VAL_INT64,
  146. ZEND_FFI_VAL_UINT32,
  147. ZEND_FFI_VAL_UINT64,
  148. ZEND_FFI_VAL_FLOAT,
  149. ZEND_FFI_VAL_DOUBLE,
  150. ZEND_FFI_VAL_LONG_DOUBLE,
  151. ZEND_FFI_VAL_CHAR,
  152. ZEND_FFI_VAL_STRING,
  153. ZEND_FFI_VAL_NAME, /* attribute value */
  154. } zend_ffi_val_kind;
  155. #ifdef HAVE_LONG_DOUBLE
  156. typedef long double zend_ffi_double;
  157. #else
  158. typedef double zend_ffi_double;
  159. #endif
  160. typedef struct _zend_ffi_val {
  161. zend_ffi_val_kind kind;
  162. union {
  163. uint64_t u64;
  164. int64_t i64;
  165. zend_ffi_double d;
  166. signed char ch;
  167. struct {
  168. const char *str;
  169. size_t len;
  170. };
  171. };
  172. } zend_ffi_val;
  173. int zend_ffi_parse_decl(const char *str, size_t len);
  174. int zend_ffi_parse_type(const char *str, size_t len, zend_ffi_dcl *dcl);
  175. /* parser callbacks */
  176. void ZEND_NORETURN zend_ffi_parser_error(const char *msg, ...);
  177. int zend_ffi_is_typedef_name(const char *name, size_t name_len);
  178. void zend_ffi_resolve_typedef(const char *name, size_t name_len, zend_ffi_dcl *dcl);
  179. void zend_ffi_resolve_const(const char *name, size_t name_len, zend_ffi_val *val);
  180. void zend_ffi_declare_tag(const char *name, size_t name_len, zend_ffi_dcl *dcl, bool incomplete);
  181. void zend_ffi_make_enum_type(zend_ffi_dcl *dcl);
  182. void zend_ffi_add_enum_val(zend_ffi_dcl *enum_dcl, const char *name, size_t name_len, zend_ffi_val *val, int64_t *min, int64_t *max, int64_t *last);
  183. void zend_ffi_make_struct_type(zend_ffi_dcl *dcl);
  184. void zend_ffi_add_field(zend_ffi_dcl *struct_dcl, const char *name, size_t name_len, zend_ffi_dcl *field_dcl);
  185. void zend_ffi_add_anonymous_field(zend_ffi_dcl *struct_dcl, zend_ffi_dcl *field_dcl);
  186. void zend_ffi_add_bit_field(zend_ffi_dcl *struct_dcl, const char *name, size_t name_len, zend_ffi_dcl *field_dcl, zend_ffi_val *bits);
  187. void zend_ffi_adjust_struct_size(zend_ffi_dcl *dcl);
  188. void zend_ffi_make_pointer_type(zend_ffi_dcl *dcl);
  189. void zend_ffi_make_array_type(zend_ffi_dcl *dcl, zend_ffi_val *len);
  190. void zend_ffi_make_func_type(zend_ffi_dcl *dcl, HashTable *args, zend_ffi_dcl *nested_dcl);
  191. void zend_ffi_add_arg(HashTable **args, const char *name, size_t name_len, zend_ffi_dcl *arg_dcl);
  192. void zend_ffi_declare(const char *name, size_t name_len, zend_ffi_dcl *dcl);
  193. void zend_ffi_add_attribute(zend_ffi_dcl *dcl, const char *name, size_t name_len);
  194. void zend_ffi_add_attribute_value(zend_ffi_dcl *dcl, const char *name, size_t name_len, int n, zend_ffi_val *val);
  195. void zend_ffi_add_msvc_attribute_value(zend_ffi_dcl *dcl, const char *name, size_t name_len, zend_ffi_val *val);
  196. void zend_ffi_set_abi(zend_ffi_dcl *dcl, uint16_t abi);
  197. void zend_ffi_nested_declaration(zend_ffi_dcl *dcl, zend_ffi_dcl *nested_dcl);
  198. void zend_ffi_align_as_type(zend_ffi_dcl *dcl, zend_ffi_dcl *align_dcl);
  199. void zend_ffi_align_as_val(zend_ffi_dcl *dcl, zend_ffi_val *align_val);
  200. void zend_ffi_validate_type_name(zend_ffi_dcl *dcl);
  201. void zend_ffi_expr_conditional(zend_ffi_val *val, zend_ffi_val *op2, zend_ffi_val *op3);
  202. void zend_ffi_expr_bool_or(zend_ffi_val *val, zend_ffi_val *op2);
  203. void zend_ffi_expr_bool_and(zend_ffi_val *val, zend_ffi_val *op2);
  204. void zend_ffi_expr_bw_or(zend_ffi_val *val, zend_ffi_val *op2);
  205. void zend_ffi_expr_bw_xor(zend_ffi_val *val, zend_ffi_val *op2);
  206. void zend_ffi_expr_bw_and(zend_ffi_val *val, zend_ffi_val *op2);
  207. void zend_ffi_expr_is_equal(zend_ffi_val *val, zend_ffi_val *op2);
  208. void zend_ffi_expr_is_not_equal(zend_ffi_val *val, zend_ffi_val *op2);
  209. void zend_ffi_expr_is_less(zend_ffi_val *val, zend_ffi_val *op2);
  210. void zend_ffi_expr_is_greater(zend_ffi_val *val, zend_ffi_val *op2);
  211. void zend_ffi_expr_is_less_or_equal(zend_ffi_val *val, zend_ffi_val *op2);
  212. void zend_ffi_expr_is_greater_or_equal(zend_ffi_val *val, zend_ffi_val *op2);
  213. void zend_ffi_expr_shift_left(zend_ffi_val *val, zend_ffi_val *op2);
  214. void zend_ffi_expr_shift_right(zend_ffi_val *val, zend_ffi_val *op2);
  215. void zend_ffi_expr_add(zend_ffi_val *val, zend_ffi_val *op2);
  216. void zend_ffi_expr_sub(zend_ffi_val *val, zend_ffi_val *op2);
  217. void zend_ffi_expr_mul(zend_ffi_val *val, zend_ffi_val *op2);
  218. void zend_ffi_expr_div(zend_ffi_val *val, zend_ffi_val *op2);
  219. void zend_ffi_expr_mod(zend_ffi_val *val, zend_ffi_val *op2);
  220. void zend_ffi_expr_cast(zend_ffi_val *val, zend_ffi_dcl *dcl);
  221. void zend_ffi_expr_plus(zend_ffi_val *val);
  222. void zend_ffi_expr_neg(zend_ffi_val *val);
  223. void zend_ffi_expr_bw_not(zend_ffi_val *val);
  224. void zend_ffi_expr_bool_not(zend_ffi_val *val);
  225. void zend_ffi_expr_sizeof_val(zend_ffi_val *val);
  226. void zend_ffi_expr_sizeof_type(zend_ffi_val *val, zend_ffi_dcl *dcl);
  227. void zend_ffi_expr_alignof_val(zend_ffi_val *val);
  228. void zend_ffi_expr_alignof_type(zend_ffi_val *val, zend_ffi_dcl *dcl);
  229. static zend_always_inline void zend_ffi_val_error(zend_ffi_val *val) /* {{{ */
  230. {
  231. val->kind = ZEND_FFI_VAL_ERROR;
  232. }
  233. /* }}} */
  234. void zend_ffi_val_number(zend_ffi_val *val, int base, const char *str, size_t str_len);
  235. void zend_ffi_val_float_number(zend_ffi_val *val, const char *str, size_t str_len);
  236. void zend_ffi_val_string(zend_ffi_val *val, const char *str, size_t str_len);
  237. void zend_ffi_val_character(zend_ffi_val *val, const char *str, size_t str_len);
  238. #endif /* PHP_FFI_H */