gc_object.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  1. /*
  2. * Copyright (C) 2019 Intel Corporation. All rights reserved.
  3. * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. */
  5. #ifndef _GC_OBJECT_H_
  6. #define _GC_OBJECT_H_
  7. #include "gc_type.h"
  8. #ifdef __cplusplus
  9. extern "C" {
  10. #endif
  11. /**
  12. * Object header of a WASM object, as the address of allocated memory
  13. * must be 8-byte aligned, the lowest 3 bits are zero, we use them to
  14. * mark the object:
  15. * bits[0] is 1: the object is an externref object
  16. * bits[1] is 1: the object is an anyref object
  17. * bits[2] is 1: the object has extra information
  18. * if both bits[0] and bits[1] are 0, then this object header must
  19. * be a pointer of a WASMRttType, denotes that the object is a
  20. * struct object, or an array object, or a function object
  21. */
  22. typedef uintptr_t WASMObjectHeader;
  23. #define WASM_OBJ_HEADER_MASK (~((uintptr_t)7))
  24. #define WASM_OBJ_EXTERNREF_OBJ_FLAG (((uintptr_t)1) << 0)
  25. #define WASM_OBJ_ANYREF_OBJ_FLAG (((uintptr_t)1) << 1)
  26. #define WASM_OBJ_EXTRA_INFO_FLAG (((uintptr_t)1) << 2)
  27. /* Representation of WASM objects */
  28. typedef struct WASMObject {
  29. WASMObjectHeader header;
  30. } WASMObject, *WASMObjectRef;
  31. /* Representation of WASM rtt types */
  32. typedef struct WASMRttType {
  33. /* type_flag must be WASM_TYPE_FUNC/STRUCT/ARRAY to
  34. denote an object of func, struct or array */
  35. uint32 type_flag;
  36. uint32 inherit_depth;
  37. WASMType *defined_type;
  38. WASMType *root_type;
  39. } WASMRttType, *WASMRttTypeRef;
  40. /* Representation of WASM externref objects */
  41. typedef struct WASMExternrefObject {
  42. /* bits[0] must be 1, denotes an externref object */
  43. WASMObjectHeader header;
  44. /* an object of WASMAnyrefObjectRef which encapsulates the external
  45. object passed from host, or other internal objects passed to
  46. opcode extern.externalize */
  47. WASMObjectRef internal_obj;
  48. } WASMExternrefObject, *WASMExternrefObjectRef;
  49. /* Representation of WASM anyref objects which encapsulate the
  50. external object passed from host */
  51. typedef struct WASMAnyrefObject {
  52. /* bits[1] must be 1, denotes an anyref object */
  53. WASMObjectHeader header;
  54. const void *host_obj;
  55. } WASMAnyrefObject, *WASMAnyrefObjectRef;
  56. /**
  57. * Representation of WASM i31 objects, the lowest bit is 1:
  58. * for a pointer of WASMObjectRef, if the lowest bit is 1, then it is an
  59. * i31 object and bits[1..31] stores the actual i31 value, otherwise
  60. * it is a normal object of rtt/externref/struct/array/func */
  61. typedef uintptr_t WASMI31ObjectRef;
  62. /* Representation of WASM struct objects */
  63. typedef struct WASMStructObject {
  64. /* Must be pointer of WASMRttObject of struct type */
  65. WASMObjectHeader header;
  66. uint8 field_data[1];
  67. } WASMStructObject, *WASMStructObjectRef;
  68. /* Representation of WASM array objects */
  69. typedef struct WASMArrayObject {
  70. /* Must be pointer of WASMRttObject of array type */
  71. WASMObjectHeader header;
  72. /* (<array length> << 2) | <array element size>,
  73. * elem_count = length >> 2
  74. * elem_size = 2 ^ (length & 0x3)
  75. */
  76. uint32 length;
  77. uint8 elem_data[1];
  78. } WASMArrayObject, *WASMArrayObjectRef;
  79. #define WASM_ARRAY_LENGTH_SHIFT 2
  80. #define WASM_ARRAY_ELEM_SIZE_MASK 3
  81. /* Representation of WASM function objects */
  82. typedef struct WASMFuncObject {
  83. /* must be pointer of WASMRttObject of func type */
  84. WASMObjectHeader header;
  85. uint32 func_idx_bound;
  86. } WASMFuncObject, *WASMFuncObjectRef;
  87. /* Representation of WASM stringref objects */
  88. typedef struct WASMStringrefObject {
  89. WASMObjectHeader header;
  90. const void *str_obj;
  91. } WASMStringrefObject, *WASMStringrefObjectRef;
  92. typedef struct WASMStringviewWTF8Object {
  93. WASMObjectHeader header;
  94. const void *str_obj;
  95. } WASMStringviewWTF8Object, *WASMStringviewWTF8ObjectRef;
  96. typedef struct WASMStringviewWTF16Object {
  97. WASMObjectHeader header;
  98. const void *str_obj;
  99. } WASMStringviewWTF16Object, *WASMStringviewWTF16ObjectRef;
  100. typedef struct WASMStringviewIterObject {
  101. WASMObjectHeader header;
  102. const void *str_obj;
  103. int32 pos;
  104. } WASMStringviewIterObject, *WASMStringviewIterObjectRef;
  105. struct WASMExecEnv;
  106. inline static WASMObjectHeader
  107. wasm_object_header(const WASMObjectRef obj)
  108. {
  109. return (obj->header & WASM_OBJ_HEADER_MASK);
  110. }
  111. WASMRttTypeRef
  112. wasm_rtt_type_new(WASMType *defined_type, uint32 defined_type_idx,
  113. WASMRttType **rtt_types, uint32 rtt_type_count,
  114. korp_mutex *rtt_type_lock);
  115. inline static WASMType *
  116. wasm_rtt_type_get_defined_type(const WASMRttTypeRef rtt_type)
  117. {
  118. return rtt_type->defined_type;
  119. }
  120. WASMStructObjectRef
  121. wasm_struct_obj_new_internal(void *heap_handle, WASMRttTypeRef rtt_type);
  122. WASMStructObjectRef
  123. wasm_struct_obj_new(struct WASMExecEnv *exec_env, WASMRttTypeRef rtt_type);
  124. void
  125. wasm_struct_obj_set_field(WASMStructObjectRef struct_obj, uint32 field_idx,
  126. const WASMValue *value);
  127. void
  128. wasm_struct_obj_get_field(const WASMStructObjectRef struct_obj,
  129. uint32 field_idx, bool sign_extend, WASMValue *value);
  130. /**
  131. * Return the field count of the WASM struct object.
  132. *
  133. * @param struct_obj the WASM struct object
  134. *
  135. * @return the field count of the WASM struct object
  136. */
  137. uint32
  138. wasm_struct_obj_get_field_count(const WASMStructObjectRef struct_obj);
  139. WASMArrayObjectRef
  140. wasm_array_obj_new_internal(void *heap_handle, WASMRttTypeRef rtt_type,
  141. uint32 length, WASMValue *init_value);
  142. WASMArrayObjectRef
  143. wasm_array_obj_new(struct WASMExecEnv *exec_env, WASMRttTypeRef rtt_type,
  144. uint32 length, WASMValue *init_value);
  145. void
  146. wasm_array_obj_set_elem(WASMArrayObjectRef array_obj, uint32 elem_idx,
  147. const WASMValue *value);
  148. void
  149. wasm_array_obj_get_elem(const WASMArrayObjectRef array_obj, uint32 elem_idx,
  150. bool sign_extend, WASMValue *value);
  151. void
  152. wasm_array_obj_fill(const WASMArrayObjectRef array_obj, uint32 elem_idx,
  153. uint32 len, WASMValue *value);
  154. void
  155. wasm_array_obj_copy(WASMArrayObjectRef dst_obj, uint32 dst_idx,
  156. WASMArrayObjectRef src_obj, uint32 src_idx, uint32 len);
  157. /**
  158. * Return the logarithm of the size of array element.
  159. *
  160. * @param array the WASM array object
  161. *
  162. * @return log(size of the array element)
  163. */
  164. inline static uint32
  165. wasm_array_obj_elem_size_log(const WASMArrayObjectRef array_obj)
  166. {
  167. return (array_obj->length & WASM_ARRAY_ELEM_SIZE_MASK);
  168. }
  169. /**
  170. * Return the length of the array.
  171. *
  172. * @param array_obj the WASM array object
  173. *
  174. * @return the length of the array
  175. */
  176. uint32
  177. wasm_array_obj_length(const WASMArrayObjectRef array_obj);
  178. /**
  179. * Return the address of the first element of an array object.
  180. *
  181. * @param array_obj the WASM array object
  182. *
  183. * @return the address of the first element of the array object
  184. */
  185. void *
  186. wasm_array_obj_first_elem_addr(const WASMArrayObjectRef array_obj);
  187. /**
  188. * Return the address of the i-th element of an array object.
  189. *
  190. * @param array_obj the WASM array object
  191. * @param index the index of the element
  192. *
  193. * @return the address of the i-th element of the array object
  194. */
  195. void *
  196. wasm_array_obj_elem_addr(const WASMArrayObjectRef array_obj, uint32 elem_idx);
  197. WASMFuncObjectRef
  198. wasm_func_obj_new_internal(void *heap_handle, WASMRttTypeRef rtt_type,
  199. uint32 func_idx_bound);
  200. WASMFuncObjectRef
  201. wasm_func_obj_new(struct WASMExecEnv *exec_env, WASMRttTypeRef rtt_type,
  202. uint32 func_idx_bound);
  203. uint32
  204. wasm_func_obj_get_func_idx_bound(const WASMFuncObjectRef func_obj);
  205. WASMFuncType *
  206. wasm_func_obj_get_func_type(const WASMFuncObjectRef func_obj);
  207. WASMExternrefObjectRef
  208. wasm_externref_obj_new(struct WASMExecEnv *exec_env, const void *host_obj);
  209. WASMAnyrefObjectRef
  210. wasm_anyref_obj_new(struct WASMExecEnv *exec_env, const void *host_obj);
  211. /* Implementation of opcode extern.internalize */
  212. WASMObjectRef
  213. wasm_externref_obj_to_internal_obj(const WASMExternrefObjectRef externref_obj);
  214. /* Implementation of opcode extern.externalize */
  215. WASMExternrefObjectRef
  216. wasm_internal_obj_to_externref_obj(struct WASMExecEnv *exec_env,
  217. const WASMObjectRef internal_obj);
  218. const void *
  219. wasm_anyref_obj_get_value(const WASMAnyrefObjectRef anyref_obj);
  220. const void *
  221. wasm_externref_obj_get_value(const WASMExternrefObjectRef externref_obj);
  222. WASMI31ObjectRef
  223. wasm_i31_obj_new(uint32 i31_value);
  224. uint32
  225. wasm_i31_obj_get_value(WASMI31ObjectRef i31_obj, bool sign_extend);
  226. bool
  227. wasm_obj_is_i31_obj(WASMObjectRef obj);
  228. bool
  229. wasm_obj_is_externref_obj(WASMObjectRef obj);
  230. bool
  231. wasm_obj_is_anyref_obj(WASMObjectRef obj);
  232. bool
  233. wasm_obj_is_i31_externref_or_anyref_obj(WASMObjectRef obj);
  234. bool
  235. wasm_obj_is_struct_obj(WASMObjectRef obj);
  236. bool
  237. wasm_obj_is_array_obj(WASMObjectRef obj);
  238. bool
  239. wasm_obj_is_func_obj(WASMObjectRef obj);
  240. bool
  241. wasm_obj_is_internal_obj(WASMObjectRef obj);
  242. bool
  243. wasm_obj_is_eq_obj(WASMObjectRef obj);
  244. inline static bool
  245. wasm_obj_is_null_obj(WASMObjectRef obj)
  246. {
  247. return obj == NULL_REF ? true : false;
  248. }
  249. inline static bool
  250. wasm_obj_is_created_from_heap(WASMObjectRef obj)
  251. {
  252. if (obj == NULL || (((uintptr_t)obj) & 1))
  253. /* null object or i31 object */
  254. return false;
  255. return true;
  256. }
  257. bool
  258. wasm_obj_is_instance_of(WASMObjectRef obj, uint32 type_idx, WASMType **types,
  259. uint32 type_count);
  260. bool
  261. wasm_obj_is_type_of(WASMObjectRef obj, int32 heap_type);
  262. bool
  263. wasm_obj_equal(WASMObjectRef obj1, WASMObjectRef obj2);
  264. bool
  265. wasm_object_get_ref_list(WASMObjectRef obj, bool *p_is_compact_mode,
  266. uint32 *p_ref_num, uint16 **p_ref_list,
  267. uint32 *p_ref_start_offset);
  268. #if WASM_ENABLE_STRINGREF != 0
  269. WASMStringrefObjectRef
  270. wasm_stringref_obj_new(struct WASMExecEnv *exec_env, const void *str_obj);
  271. WASMStringviewWTF8ObjectRef
  272. wasm_stringview_wtf8_obj_new(struct WASMExecEnv *exec_env, const void *str_obj);
  273. WASMStringviewWTF16ObjectRef
  274. wasm_stringview_wtf16_obj_new(struct WASMExecEnv *exec_env,
  275. const void *str_obj);
  276. WASMStringviewIterObjectRef
  277. wasm_stringview_iter_obj_new(struct WASMExecEnv *exec_env, const void *str_obj,
  278. int32 pos);
  279. const void *
  280. wasm_stringref_obj_get_value(WASMStringrefObjectRef stringref_obj);
  281. const void *
  282. wasm_stringview_wtf8_obj_get_value(
  283. WASMStringviewWTF8ObjectRef stringview_wtf8_obj);
  284. const void *
  285. wasm_stringview_wtf16_obj_get_value(
  286. WASMStringviewWTF16ObjectRef stringview_wtf16_obj);
  287. const void *
  288. wasm_stringview_iter_obj_get_value(
  289. WASMStringviewIterObjectRef stringview_iter_obj);
  290. int32
  291. wasm_stringview_iter_obj_get_pos(
  292. WASMStringviewIterObjectRef stringview_iter_obj);
  293. void
  294. wasm_stringview_iter_obj_update_pos(
  295. WASMStringviewIterObjectRef stringview_iter_obj, int32 pos);
  296. bool
  297. wasm_obj_is_stringref_obj(WASMObjectRef obj);
  298. bool
  299. wasm_obj_is_stringview_wtf8_obj(WASMObjectRef obj);
  300. bool
  301. wasm_obj_is_stringview_wtf16_obj(WASMObjectRef obj);
  302. #endif /* end of WASM_ENABLE_STRINGREF != 0 */
  303. #ifdef __cplusplus
  304. } /* end of extern "C" */
  305. #endif
  306. #endif /* end of _GC_OBJECT_H_ */