gc_export.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521
  1. /*
  2. * Copyright (C) 2019 Intel Corporation. All rights reserved.
  3. * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. */
  5. #ifndef _GC_EXPORT_H
  6. #define _GC_EXPORT_H
  7. #include "wasm_export.h"
  8. #ifdef __cplusplus
  9. extern "C" {
  10. #endif
  11. typedef uint8_t wasm_value_type_t;
  12. typedef enum wasm_value_type_enum {
  13. VALUE_TYPE_I32 = 0x7F,
  14. VALUE_TYPE_I64 = 0x7E,
  15. VALUE_TYPE_F32 = 0x7D,
  16. VALUE_TYPE_F64 = 0x7C,
  17. VALUE_TYPE_V128 = 0x7B,
  18. VALUE_TYPE_FUNCREF = 0x70,
  19. VALUE_TYPE_EXTERNREF = 0x6F,
  20. VALUE_TYPE_ANYREF = 0x6E,
  21. VALUE_TYPE_EQREF = 0x6D,
  22. VALUE_TYPE_HT_NULLABLE_REF = 0x6C,
  23. VALUE_TYPE_HT_NON_NULLABLE_REF = 0x6B,
  24. VALUE_TYPE_I31REF = 0x6A,
  25. VALUE_TYPE_NULLFUNCREF = 0x69,
  26. VALUE_TYPE_NULLEXTERNREF = 0x68,
  27. VALUE_TYPE_STRUCTREF = 0x67,
  28. VALUE_TYPE_ARRAYREF = 0x66,
  29. VALUE_TYPE_NULLREF = 0x65,
  30. } wasm_value_type_enum;
  31. typedef int32_t wasm_heap_type_t;
  32. enum wasm_heap_type_enum {
  33. HEAP_TYPE_FUNC = -0x10,
  34. HEAP_TYPE_EXTERN = -0x11,
  35. HEAP_TYPE_ANY = -0x12,
  36. HEAP_TYPE_EQ = -0x13,
  37. HEAP_TYPE_I31 = -0x16,
  38. HEAP_TYPE_NOFUNC = -0x17,
  39. HEAP_TYPE_NOEXTERN = -0x18,
  40. HEAP_TYPE_STRUCT = -0x19,
  41. HEAP_TYPE_ARRAY = -0x1A,
  42. HEAP_TYPE_NONE = -0x1B
  43. } wasm_heap_type_enum;
  44. struct WASMObject;
  45. typedef struct WASMObject *wasm_obj_t;
  46. #ifndef WASM_VALUE_DEFINED
  47. #define WASM_VALUE_DEFINED
  48. typedef union V128 {
  49. int8_t i8x16[16];
  50. int16_t i16x8[8];
  51. int32_t i32x8[4];
  52. int64_t i64x2[2];
  53. float f32x4[4];
  54. double f64x2[2];
  55. } V128;
  56. typedef union WASMValue {
  57. int32_t i32;
  58. uint32_t u32;
  59. uint32_t global_index;
  60. uint32_t ref_index;
  61. int64_t i64;
  62. uint64_t u64;
  63. float f32;
  64. double f64;
  65. V128 v128;
  66. wasm_obj_t gc_obj;
  67. uint32_t type_index;
  68. struct {
  69. uint32_t type_index;
  70. uint32_t N;
  71. } array_new_canon_fixed;
  72. } WASMValue;
  73. #endif /* end of WASM_VALUE_DEFINED */
  74. typedef union WASMValue wasm_value_t;
  75. /* Reference type, the layout is same as WasmRefType in wasm.h */
  76. typedef struct wasm_ref_type_t {
  77. wasm_value_type_t value_type;
  78. bool nullable;
  79. int32_t heap_type;
  80. } wasm_ref_type_t;
  81. /**
  82. * Local object reference that can be traced when GC occurs. All
  83. * native functions that need to hold WASM objects which may not be
  84. * referenced from other elements of GC root set may be hold with
  85. * this type of variable so that they can be traced when GC occurs.
  86. * Before using such a variable, it must be pushed onto the stack
  87. * (implemented as a chain) of such variables, and before leaving the
  88. * frame of the variables, they must be popped from the stack.
  89. */
  90. typedef struct WASMLocalObjectRef {
  91. /* Previous local object reference variable on the stack */
  92. struct WASMLocalObjectRef *prev;
  93. /* The reference of WASM object hold by this variable */
  94. wasm_obj_t val;
  95. } WASMLocalObjectRef, wasm_local_obj_ref_t;
  96. struct WASMType;
  97. struct WASMFuncType;
  98. struct WASMStructType;
  99. struct WASMArrayType;
  100. typedef struct WASMType *wasm_defined_type_t;
  101. typedef struct WASMFuncType *wasm_func_type_t;
  102. typedef struct WASMStructType *wasm_struct_type_t;
  103. typedef struct WASMArrayType *wasm_array_type_t;
  104. struct WASMExternrefObject;
  105. struct WASMAnyrefObject;
  106. struct WASMStructObject;
  107. struct WASMArrayObject;
  108. struct WASMFuncObject;
  109. typedef struct WASMExternrefObject *wasm_externref_obj_t;
  110. typedef struct WASMAnyrefObject *wasm_anyref_obj_t;
  111. typedef struct WASMStructObject *wasm_struct_obj_t;
  112. typedef struct WASMArrayObject *wasm_array_obj_t;
  113. typedef struct WASMFuncObject *wasm_func_obj_t;
  114. typedef uintptr_t wasm_i31_obj_t;
  115. /* Defined type related operations */
  116. /**
  117. * Get the defined type count of a WASM module
  118. */
  119. WASM_RUNTIME_API_EXTERN uint32_t
  120. wasm_get_defined_type_count(const wasm_module_t module);
  121. /**
  122. * Get the specified defined type of a WASM module
  123. */
  124. WASM_RUNTIME_API_EXTERN wasm_defined_type_t
  125. wasm_get_defined_type(const wasm_module_t module, uint32_t index);
  126. /**
  127. * Check whether a defined type is a function type
  128. */
  129. WASM_RUNTIME_API_EXTERN bool
  130. wasm_defined_type_is_func_type(const wasm_defined_type_t def_type);
  131. /**
  132. * Check whether a defined type is a struct type
  133. */
  134. WASM_RUNTIME_API_EXTERN bool
  135. wasm_defined_type_is_struct_type(const wasm_defined_type_t def_type);
  136. /**
  137. * Check whether a defined type is an array type
  138. */
  139. WASM_RUNTIME_API_EXTERN bool
  140. wasm_defined_type_is_array_type(const wasm_defined_type_t def_type);
  141. /**
  142. * Get parameter count of a function type
  143. */
  144. WASM_RUNTIME_API_EXTERN uint32_t
  145. wasm_func_type_get_param_count(const wasm_func_type_t func_type);
  146. /**
  147. * Get type of a specified parameter of a function type
  148. */
  149. WASM_RUNTIME_API_EXTERN wasm_ref_type_t
  150. wasm_func_type_get_param_type(const wasm_func_type_t func_type,
  151. uint32_t param_idx);
  152. /**
  153. * Get result count of a function type
  154. */
  155. WASM_RUNTIME_API_EXTERN uint32_t
  156. wasm_func_type_get_result_count(const wasm_func_type_t func_type);
  157. /**
  158. * Get type of a specified result of a function type
  159. */
  160. WASM_RUNTIME_API_EXTERN wasm_ref_type_t
  161. wasm_func_type_get_result_type(const wasm_func_type_t func_type,
  162. uint32_t result_idx);
  163. /**
  164. * Get field count of a struct type
  165. */
  166. WASM_RUNTIME_API_EXTERN uint32_t
  167. wasm_struct_type_get_field_count(const wasm_struct_type_t struct_type);
  168. /**
  169. * Get type of a specified field of a struct type
  170. */
  171. WASM_RUNTIME_API_EXTERN wasm_ref_type_t
  172. wasm_struct_type_get_field_type(const wasm_struct_type_t struct_type,
  173. uint32_t field_idx, bool *p_is_mutable);
  174. /**
  175. * Get element type of an array type
  176. */
  177. WASM_RUNTIME_API_EXTERN wasm_ref_type_t
  178. wasm_array_type_get_elem_type(const wasm_array_type_t array_type,
  179. bool *p_is_mutable);
  180. /**
  181. * Check whether two defined types are equal
  182. */
  183. WASM_RUNTIME_API_EXTERN bool
  184. wasm_defined_type_equal(const wasm_defined_type_t def_type1,
  185. const wasm_defined_type_t def_type2,
  186. const wasm_module_t module);
  187. /**
  188. * Check whether def_type1 is subtype of def_type2
  189. */
  190. WASM_RUNTIME_API_EXTERN bool
  191. wasm_defined_type_is_subtype_of(const wasm_defined_type_t def_type1,
  192. const wasm_defined_type_t def_type2,
  193. const wasm_module_t module);
  194. /* ref type related operations */
  195. /**
  196. * Set the ref_type to be (ref null? type_idx)
  197. */
  198. WASM_RUNTIME_API_EXTERN void
  199. wasm_ref_type_set_type_idx(wasm_ref_type_t *ref_type, bool nullable,
  200. int32_t type_idx);
  201. /**
  202. * Set the ref_type to be (ref null? func/extern/any/eq/i31/struct/array/..)
  203. */
  204. WASM_RUNTIME_API_EXTERN void
  205. wasm_ref_type_set_heap_type(wasm_ref_type_t *ref_type, bool nullable,
  206. int32_t heap_type);
  207. /**
  208. * Check whether two ref types are equal
  209. */
  210. WASM_RUNTIME_API_EXTERN bool
  211. wasm_ref_type_equal(const wasm_ref_type_t *ref_type1,
  212. const wasm_ref_type_t *ref_type2,
  213. const wasm_module_t module);
  214. /**
  215. * Check whether ref_type1 is subtype of ref_type2
  216. */
  217. WASM_RUNTIME_API_EXTERN bool
  218. wasm_ref_type_is_subtype_of(const wasm_ref_type_t *ref_type1,
  219. const wasm_ref_type_t *ref_type2,
  220. const wasm_module_t module);
  221. /* wasm object related operations */
  222. /**
  223. * Create a struct object with the index of defined type
  224. */
  225. WASM_RUNTIME_API_EXTERN wasm_struct_obj_t
  226. wasm_struct_obj_new_with_typeidx(wasm_exec_env_t exec_env, uint32_t type_idx);
  227. /**
  228. * Create a struct object with the struct type
  229. */
  230. WASM_RUNTIME_API_EXTERN wasm_struct_obj_t
  231. wasm_struct_obj_new_with_type(wasm_exec_env_t exec_env,
  232. const wasm_struct_type_t type);
  233. /**
  234. * Set the field value of a struct object
  235. */
  236. WASM_RUNTIME_API_EXTERN void
  237. wasm_struct_obj_set_field(wasm_struct_obj_t obj, uint32_t field_idx,
  238. const wasm_value_t *value);
  239. /**
  240. * Get the field value of a struct object
  241. */
  242. WASM_RUNTIME_API_EXTERN void
  243. wasm_struct_obj_get_field(const wasm_struct_obj_t obj, uint32_t field_idx,
  244. bool sign_extend, wasm_value_t *value);
  245. /**
  246. * Create an array object with the index of defined type
  247. */
  248. WASM_RUNTIME_API_EXTERN wasm_array_obj_t
  249. wasm_array_obj_new_with_typeidx(wasm_exec_env_t exec_env, uint32_t type_idx);
  250. /**
  251. * Create an array object with the array type
  252. */
  253. WASM_RUNTIME_API_EXTERN wasm_array_obj_t
  254. wasm_array_obj_new_with_type(wasm_exec_env_t exec_env,
  255. const wasm_array_type_t type);
  256. /**
  257. * Set the specified element's value of an array object
  258. */
  259. WASM_RUNTIME_API_EXTERN void
  260. wasm_array_obj_set_elem(wasm_array_obj_t array_obj, uint32_t elem_idx,
  261. const wasm_value_t *value);
  262. /**
  263. * Get the specified element's value of an array object
  264. */
  265. WASM_RUNTIME_API_EXTERN void
  266. wasm_array_obj_get_elem(const wasm_array_obj_t array_obj, uint32_t elem_idx,
  267. bool sign_extend, wasm_value_t *value);
  268. /**
  269. * Copy elements from one array to another
  270. */
  271. WASM_RUNTIME_API_EXTERN void
  272. wasm_array_obj_copy(wasm_array_obj_t dst_obj, uint32_t dst_idx,
  273. const wasm_array_obj_t src_obj, uint32_t src_idx,
  274. uint32_t len);
  275. /**
  276. * Return the length of an array object
  277. */
  278. WASM_RUNTIME_API_EXTERN uint32_t
  279. wasm_array_obj_length(const wasm_array_obj_t array_obj);
  280. /**
  281. * Get the address of the first element of an array object
  282. */
  283. WASM_RUNTIME_API_EXTERN void *
  284. wasm_array_obj_first_elem_addr(const wasm_array_obj_t array_obj);
  285. /**
  286. * Get the address of the i-th element of an array object
  287. */
  288. WASM_RUNTIME_API_EXTERN void *
  289. wasm_array_obj_elem_addr(const wasm_array_obj_t array_obj, uint32_t elem_idx);
  290. /**
  291. * Create a function object with the index of defined type
  292. */
  293. WASM_RUNTIME_API_EXTERN wasm_func_obj_t
  294. wasm_func_obj_new_with_typeidx(wasm_exec_env_t exec_env, uint32_t type_idx);
  295. /**
  296. * Create a function object with the function type
  297. */
  298. WASM_RUNTIME_API_EXTERN wasm_func_obj_t
  299. wasm_func_obj_new_with_type(wasm_exec_env_t exec_env, wasm_func_type_t type);
  300. /**
  301. * Get the function index bound of a function object
  302. */
  303. WASM_RUNTIME_API_EXTERN uint32_t
  304. wasm_func_obj_get_func_idx_bound(const wasm_func_obj_t func_obj);
  305. /**
  306. * Get the function type of a function object
  307. */
  308. WASM_RUNTIME_API_EXTERN wasm_func_type_t
  309. wasm_func_obj_get_func_type(const wasm_func_obj_t func_obj);
  310. /**
  311. * Create an externref object with host object
  312. */
  313. WASM_RUNTIME_API_EXTERN wasm_externref_obj_t
  314. wasm_externref_obj_new(wasm_exec_env_t exec_env, const void *host_obj);
  315. /**
  316. * Get the host value of an externref object
  317. */
  318. WASM_RUNTIME_API_EXTERN const void *
  319. wasm_externref_obj_get_value(const wasm_externref_obj_t externref_obj);
  320. /**
  321. * Create an anyref object with host object
  322. */
  323. WASM_RUNTIME_API_EXTERN wasm_anyref_obj_t
  324. wasm_anyref_obj_new(wasm_exec_env_t exec_env, const void *host_obj);
  325. /**
  326. * Get the host object value of an anyref object
  327. */
  328. WASM_RUNTIME_API_EXTERN const void *
  329. wasm_anyref_obj_get_value(const wasm_anyref_obj_t anyref_obj);
  330. /**
  331. * Get the internal object inside the externref object, same as
  332. * the operation of opcode extern.internalize
  333. */
  334. WASM_RUNTIME_API_EXTERN wasm_obj_t
  335. wasm_externref_obj_to_internal_obj(const wasm_externref_obj_t externref_obj);
  336. /**
  337. * Create an externref object from an internal object, same as
  338. * the operation of opcode extern.externalize
  339. */
  340. WASM_RUNTIME_API_EXTERN wasm_externref_obj_t
  341. wasm_internal_obj_to_externref_obj(wasm_exec_env_t exec_env,
  342. const wasm_obj_t internal_obj);
  343. /**
  344. * Create an i31 object
  345. */
  346. WASM_RUNTIME_API_EXTERN wasm_i31_obj_t
  347. wasm_i31_obj_new(uint32_t i31_value);
  348. WASM_RUNTIME_API_EXTERN uint32_t
  349. wasm_i31_obj_get_value(wasm_i31_obj_t i31_obj, bool sign_extend);
  350. /**
  351. * Pin an object to make it traced during GC
  352. */
  353. WASM_RUNTIME_API_EXTERN bool
  354. wasm_runtime_pin_object(wasm_exec_env_t exec_env, wasm_obj_t obj);
  355. /**
  356. * Unpin an object
  357. */
  358. WASM_RUNTIME_API_EXTERN bool
  359. wasm_runtime_unpin_object(wasm_exec_env_t exec_env, wasm_obj_t obj);
  360. /**
  361. * Check whether an object is a struct object
  362. */
  363. WASM_RUNTIME_API_EXTERN bool
  364. wasm_obj_is_struct_obj(const wasm_obj_t obj);
  365. /**
  366. * Check whether an object is an array object
  367. */
  368. WASM_RUNTIME_API_EXTERN bool
  369. wasm_obj_is_array_obj(const wasm_obj_t obj);
  370. /**
  371. * Check whether an object is a function object
  372. */
  373. WASM_RUNTIME_API_EXTERN bool
  374. wasm_obj_is_func_obj(const wasm_obj_t obj);
  375. /**
  376. * Check whether an object is an i31 object
  377. */
  378. WASM_RUNTIME_API_EXTERN bool
  379. wasm_obj_is_i31_obj(const wasm_obj_t obj);
  380. /**
  381. * Check whether an object is an externref object
  382. */
  383. WASM_RUNTIME_API_EXTERN bool
  384. wasm_obj_is_externref_obj(const wasm_obj_t obj);
  385. /**
  386. * Check whether an object is an anyref object
  387. */
  388. WASM_RUNTIME_API_EXTERN bool
  389. wasm_obj_is_anyref_obj(const wasm_obj_t obj);
  390. /**
  391. * Check whether an object is a struct object, or, an i31/struct/array object
  392. */
  393. WASM_RUNTIME_API_EXTERN bool
  394. wasm_obj_is_internal_obj(const wasm_obj_t obj);
  395. /**
  396. * Check whether an object is an eq object
  397. */
  398. WASM_RUNTIME_API_EXTERN bool
  399. wasm_obj_is_eq_obj(const wasm_obj_t obj);
  400. /**
  401. * Check whether an object is an instance of a defined type
  402. */
  403. WASM_RUNTIME_API_EXTERN bool
  404. wasm_obj_is_instance_of_defined_type(const wasm_obj_t obj,
  405. const wasm_defined_type_t defined_type,
  406. const wasm_module_t module);
  407. /**
  408. * Check whether an object is an instance of a defined type with
  409. * index type_idx
  410. */
  411. WASM_RUNTIME_API_EXTERN bool
  412. wasm_obj_is_instance_of_type_idx(const wasm_obj_t obj, uint32_t type_idx,
  413. const wasm_module_t module);
  414. /**
  415. * Check whether an object is an instance of a ref type
  416. */
  417. WASM_RUNTIME_API_EXTERN bool
  418. wasm_obj_is_instance_of_ref_type(const wasm_obj_t obj,
  419. const wasm_ref_type_t *ref_type);
  420. /**
  421. * Push a local object ref into stack, note that we should set its value
  422. * after pushing to retain it during GC, and should pop it from stack
  423. * before returning from the current function
  424. */
  425. WASM_RUNTIME_API_EXTERN void
  426. wasm_runtime_push_local_object_ref(wasm_exec_env_t exec_env,
  427. wasm_local_obj_ref_t *local_obj_ref);
  428. /**
  429. * Pop a local object ref from stack
  430. */
  431. WASM_RUNTIME_API_EXTERN wasm_local_obj_ref_t *
  432. wasm_runtime_pop_local_object_ref(wasm_exec_env_t exec_env);
  433. /**
  434. * Pop n local object refs from stack
  435. */
  436. WASM_RUNTIME_API_EXTERN void
  437. wasm_runtime_pop_local_object_refs(wasm_exec_env_t exec_env, uint32_t n);
  438. #ifdef __cplusplus
  439. }
  440. #endif
  441. #endif /* end of _GC_EXPORT_H */