gc_export.h 14 KB

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