gc_object.h 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  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 adddress 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. * if both bits[0] and bits[1] are 0, then this object header must
  18. * be a pointer of a WASMRttType, denotes that the object is a
  19. * struct object, or an array object, or a function object
  20. */
  21. typedef uintptr_t WASMObjectHeader;
  22. #define WASM_OBJ_HEADER_MASK (~((uintptr_t)7))
  23. #define WASM_OBJ_EXTERNREF_OBJ_FLAG (((uintptr_t)1) << 0)
  24. #define WASM_OBJ_ANYREF_OBJ_FLAG (((uintptr_t)1) << 1)
  25. /* Representation of WASM objects */
  26. typedef struct WASMObject {
  27. WASMObjectHeader header;
  28. } WASMObject, *WASMObjectRef;
  29. /* Representation of WASM rtt types */
  30. typedef struct WASMRttType {
  31. /* type_flag must be WASM_TYPE_FUNC/STRUCT/ARRAY to
  32. denote an object of func, struct or array */
  33. uint32 type_flag;
  34. uint32 inherit_depth;
  35. WASMType *defined_type;
  36. WASMType *root_type;
  37. } WASMRttType, *WASMRttTypeRef;
  38. /* Representation of WASM externref objects */
  39. typedef struct WASMExternrefObject {
  40. /* bits[0] must be 1, denotes an externref object */
  41. WASMObjectHeader header;
  42. /* an object of WASMAnyrefObjectRef which encapsulates the external
  43. object passed from host, or other internal objects passed to
  44. opcode extern.externalize */
  45. WASMObjectRef internal_obj;
  46. } WASMExternrefObject, *WASMExternrefObjectRef;
  47. /* Representation of WASM anyref objects which encapsulate the
  48. external object passed from host */
  49. typedef struct WASMAnyrefObject {
  50. /* bits[1] must be 1, denotes an anyref object */
  51. WASMObjectHeader header;
  52. const void *host_obj;
  53. } WASMAnyrefObject, *WASMAnyrefObjectRef;
  54. /**
  55. * Representation of WASM i31 objects, the lowest bit is 1:
  56. * for a pointer of WASMObjectRef, if the lowest bit is 1, then it is an
  57. * i31 object and bits[1..31] stores the actual i31 value, otherwise
  58. * it is a normal object of rtt/externref/struct/array/func */
  59. typedef uintptr_t WASMI31ObjectRef;
  60. /* Representation of WASM struct objects */
  61. typedef struct WASMStructObject {
  62. /* Must be pointer of WASMRttObject of struct type */
  63. WASMObjectHeader header;
  64. uint8 field_data[1];
  65. } WASMStructObject, *WASMStructObjectRef;
  66. /* Representation of WASM array objects */
  67. typedef struct WASMArrayObject {
  68. /* Must be pointer of WASMRttObject of array type */
  69. WASMObjectHeader header;
  70. /* (<array length> << 2) | <array element size>,
  71. * elem_count = lenght >> 2
  72. * elem_size = 2 ^ (length & 0x3)
  73. */
  74. uint32 length;
  75. uint8 elem_data[1];
  76. } WASMArrayObject, *WASMArrayObjectRef;
  77. #define WASM_ARRAY_LENGTH_SHIFT 2
  78. #define WASM_ARRAY_ELEM_SIZE_MASK 3
  79. /* Representation of WASM function objects */
  80. typedef struct WASMFuncObject {
  81. /* must be pointer of WASMRttObject of func type */
  82. WASMObjectHeader header;
  83. uint32 func_idx_bound;
  84. } WASMFuncObject, *WASMFuncObjectRef;
  85. struct WASMExecEnv;
  86. inline static WASMObjectHeader
  87. wasm_object_header(const WASMObjectRef obj)
  88. {
  89. return (obj->header & WASM_OBJ_HEADER_MASK);
  90. }
  91. WASMRttTypeRef
  92. wasm_rtt_type_new(WASMType *defined_type, uint32 defined_type_idx,
  93. WASMRttType **rtt_types, uint32 rtt_type_count,
  94. korp_mutex *rtt_type_lock);
  95. inline static WASMType *
  96. wasm_rtt_type_get_defined_type(const WASMRttTypeRef rtt_type)
  97. {
  98. return rtt_type->defined_type;
  99. }
  100. WASMStructObjectRef
  101. wasm_struct_obj_new(struct WASMExecEnv *exec_env, WASMRttTypeRef rtt_type);
  102. void
  103. wasm_struct_obj_set_field(WASMStructObjectRef struct_obj, uint32 field_idx,
  104. const WASMValue *value);
  105. void
  106. wasm_struct_obj_get_field(const WASMStructObjectRef struct_obj,
  107. uint32 field_idx, bool sign_extend, WASMValue *value);
  108. WASMArrayObjectRef
  109. wasm_array_obj_new(struct WASMExecEnv *exec_env, WASMRttTypeRef rtt_type,
  110. uint32 length, WASMValue *init_value);
  111. void
  112. wasm_array_obj_set_elem(WASMArrayObjectRef array_obj, uint32 elem_idx,
  113. const WASMValue *value);
  114. void
  115. wasm_array_obj_get_elem(const WASMArrayObjectRef array_obj, uint32 elem_idx,
  116. bool sign_extend, WASMValue *value);
  117. void
  118. wasm_array_obj_copy(WASMArrayObjectRef dst_obj, uint32 dst_idx,
  119. WASMArrayObjectRef src_obj, uint32 src_idx, uint32 len);
  120. /**
  121. * Return the logarithm of the size of array element.
  122. *
  123. * @param array the WASM array object
  124. *
  125. * @return log(size of the array element)
  126. */
  127. inline static uint32
  128. wasm_array_obj_elem_size_log(const WASMArrayObjectRef array_obj)
  129. {
  130. return (array_obj->length & WASM_ARRAY_ELEM_SIZE_MASK);
  131. }
  132. /**
  133. * Return the length of the array.
  134. *
  135. * @param array_obj the WASM array object
  136. *
  137. * @return the length of the array
  138. */
  139. uint32
  140. wasm_array_obj_length(const WASMArrayObjectRef array_obj);
  141. /**
  142. * Return the address of the first element of an array object.
  143. *
  144. * @param array_obj the WASM array object
  145. *
  146. * @return the address of the first element of the array object
  147. */
  148. void *
  149. wasm_array_obj_first_elem_addr(const WASMArrayObjectRef array_obj);
  150. /**
  151. * Return the address of the i-th element of an array object.
  152. *
  153. * @param array_obj the WASM array object
  154. * @param index the index of the element
  155. *
  156. * @return the address of the i-th element of the array object
  157. */
  158. void *
  159. wasm_array_obj_elem_addr(const WASMArrayObjectRef array_obj, uint32 elem_idx);
  160. WASMFuncObjectRef
  161. wasm_func_obj_new_internal(void *heap_handle, WASMRttTypeRef rtt_type,
  162. uint32 func_idx_bound);
  163. WASMFuncObjectRef
  164. wasm_func_obj_new(struct WASMExecEnv *exec_env, WASMRttTypeRef rtt_type,
  165. uint32 func_idx_bound);
  166. uint32
  167. wasm_func_obj_get_func_idx_bound(const WASMFuncObjectRef func_obj);
  168. WASMFuncType *
  169. wasm_func_obj_get_func_type(const WASMFuncObjectRef func_obj);
  170. WASMExternrefObjectRef
  171. wasm_externref_obj_new(struct WASMExecEnv *exec_env, const void *host_obj);
  172. WASMAnyrefObjectRef
  173. wasm_anyref_obj_new(struct WASMExecEnv *exec_env, const void *host_obj);
  174. /* Implementation of opcode extern.internalize */
  175. WASMObjectRef
  176. wasm_externref_obj_to_internal_obj(const WASMExternrefObjectRef externref_obj);
  177. /* Implementation of opcode extern.externalize */
  178. WASMExternrefObjectRef
  179. wasm_internal_obj_to_externref_obj(struct WASMExecEnv *exec_env,
  180. const WASMObjectRef internal_obj);
  181. const void *
  182. wasm_anyref_obj_get_value(const WASMAnyrefObjectRef anyref_obj);
  183. const void *
  184. wasm_externref_obj_get_value(const WASMExternrefObjectRef externref_obj);
  185. WASMI31ObjectRef
  186. wasm_i31_obj_new(uint32 i31_value);
  187. uint32
  188. wasm_i31_obj_get_value(WASMI31ObjectRef i31_obj, bool sign_extend);
  189. bool
  190. wasm_obj_is_i31_obj(WASMObjectRef obj);
  191. bool
  192. wasm_obj_is_externref_obj(WASMObjectRef obj);
  193. bool
  194. wasm_obj_is_anyref_obj(WASMObjectRef obj);
  195. bool
  196. wasm_obj_is_i31_externref_or_anyref_obj(WASMObjectRef obj);
  197. bool
  198. wasm_obj_is_struct_obj(WASMObjectRef obj);
  199. bool
  200. wasm_obj_is_array_obj(WASMObjectRef obj);
  201. bool
  202. wasm_obj_is_func_obj(WASMObjectRef obj);
  203. bool
  204. wasm_obj_is_internal_obj(WASMObjectRef obj);
  205. bool
  206. wasm_obj_is_eq_obj(WASMObjectRef obj);
  207. inline static bool
  208. wasm_obj_is_null_obj(WASMObjectRef obj)
  209. {
  210. return obj == NULL_REF ? true : false;
  211. }
  212. bool
  213. wasm_obj_is_created_from_heap(WASMObjectRef obj);
  214. bool
  215. wasm_obj_is_instance_of(WASMObjectRef obj, uint32 type_idx, WASMType **types,
  216. uint32 type_count);
  217. bool
  218. wasm_obj_is_type_of(WASMObjectRef obj, int32 heap_type);
  219. bool
  220. wasm_obj_equal(WASMObjectRef obj1, WASMObjectRef obj2);
  221. bool
  222. wasm_object_get_ref_list(WASMObjectRef obj, bool *p_is_compact_mode,
  223. uint32 *p_ref_num, uint16 **p_ref_list,
  224. uint32 *p_ref_start_offset);
  225. #ifdef __cplusplus
  226. } /* end of extern "C" */
  227. #endif
  228. #endif /* end of _GC_OBJECT_H_ */