wasm.h 39 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436
  1. /*
  2. * Copyright (C) 2019 Intel Corporation. All rights reserved.
  3. * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. */
  5. #ifndef _WASM_H_
  6. #define _WASM_H_
  7. #include "bh_platform.h"
  8. #include "bh_hashmap.h"
  9. #include "bh_assert.h"
  10. #if WASM_ENABLE_GC != 0
  11. #include "gc_export.h"
  12. #endif
  13. #ifdef __cplusplus
  14. extern "C" {
  15. #endif
  16. /* Value Type */
  17. #define VALUE_TYPE_I32 0x7F
  18. #define VALUE_TYPE_I64 0X7E
  19. #define VALUE_TYPE_F32 0x7D
  20. #define VALUE_TYPE_F64 0x7C
  21. #define VALUE_TYPE_V128 0x7B
  22. #define VALUE_TYPE_FUNCREF 0x70
  23. #define VALUE_TYPE_EXTERNREF 0x6F
  24. #define VALUE_TYPE_VOID 0x40
  25. /* Packed Types */
  26. #define PACKED_TYPE_I8 0x78
  27. #define PACKED_TYPE_I16 0x77
  28. /* Reference Types */
  29. #define REF_TYPE_NULLFUNCREF 0x73
  30. #define REF_TYPE_NULLEXTERNREF 0x72
  31. #define REF_TYPE_NULLREF 0x71
  32. #define REF_TYPE_FUNCREF VALUE_TYPE_FUNCREF /* 0x70 */
  33. #define REF_TYPE_EXTERNREF VALUE_TYPE_EXTERNREF /* 0x6F */
  34. #define REF_TYPE_ANYREF 0x6E
  35. #define REF_TYPE_EQREF 0x6D
  36. #define REF_TYPE_I31REF 0x6C
  37. #define REF_TYPE_STRUCTREF 0x6B
  38. #define REF_TYPE_ARRAYREF 0x6A
  39. #define REF_TYPE_HT_NON_NULLABLE 0x64
  40. #define REF_TYPE_HT_NULLABLE 0x63
  41. #define REF_TYPE_STRINGREF VALUE_TYPE_STRINGREF /* 0x67 */
  42. #define REF_TYPE_STRINGVIEWWTF8 VALUE_TYPE_STRINGVIEWWTF8 /* 0x66 */
  43. #define REF_TYPE_STRINGVIEWWTF16 VALUE_TYPE_STRINGVIEWWTF16 /* 0x62 */
  44. #define REF_TYPE_STRINGVIEWITER VALUE_TYPE_STRINGVIEWITER /* 0x61 */
  45. /* Heap Types */
  46. #define HEAP_TYPE_NOFUNC (-0x0D)
  47. #define HEAP_TYPE_NOEXTERN (-0x0E)
  48. #define HEAP_TYPE_NONE (-0x0F)
  49. #define HEAP_TYPE_FUNC (-0x10)
  50. #define HEAP_TYPE_EXTERN (-0x11)
  51. #define HEAP_TYPE_ANY (-0x12)
  52. #define HEAP_TYPE_EQ (-0x13)
  53. #define HEAP_TYPE_I31 (-0x14)
  54. #define HEAP_TYPE_STRUCT (-0x15)
  55. #define HEAP_TYPE_ARRAY (-0x16)
  56. #define HEAP_TYPE_STRINGREF (-0x19)
  57. #define HEAP_TYPE_STRINGVIEWWTF8 (-0x1A)
  58. #define HEAP_TYPE_STRINGVIEWWTF16 (-0x1E)
  59. #define HEAP_TYPE_STRINGVIEWITER (-0x1F)
  60. /* Defined Types */
  61. #define DEFINED_TYPE_FUNC 0x60
  62. #define DEFINED_TYPE_STRUCT 0x5F
  63. #define DEFINED_TYPE_ARRAY 0x5E
  64. #define DEFINED_TYPE_SUB 0x50
  65. #define DEFINED_TYPE_SUB_FINAL 0x4F
  66. #define DEFINED_TYPE_REC 0x4E
  67. /* Used by AOT */
  68. #define VALUE_TYPE_I1 0x41
  69. /**
  70. * Used by loader to represent any type of i32/i64/f32/f64/v128
  71. * and ref types, including funcref, externref, anyref, eqref,
  72. * (ref null $ht), (ref $ht), i31ref, structref, arrayref,
  73. * nullfuncref, nullexternref, nullref and stringref
  74. */
  75. #define VALUE_TYPE_ANY 0x42
  76. /**
  77. * Used by wamr compiler to represent object ref types,
  78. * including func object ref, externref object ref,
  79. * internal object ref, eq object ref, i31 object ref,
  80. * struct object ref, array object ref
  81. */
  82. #define VALUE_TYPE_GC_REF 0x43
  83. #define MAX_PAGE_COUNT_FLAG 0x01
  84. #define SHARED_MEMORY_FLAG 0x02
  85. #define MEMORY64_FLAG 0x04
  86. #define DEFAULT_NUM_BYTES_PER_PAGE 65536
  87. #define DEFAULT_MAX_PAGES 65536
  88. #define DEFAULT_MEM64_MAX_PAGES UINT32_MAX
  89. /* Max size of linear memory */
  90. #define MAX_LINEAR_MEMORY_SIZE (4 * (uint64)BH_GB)
  91. /* Roughly 274 TB */
  92. #define MAX_LINEAR_MEM64_MEMORY_SIZE \
  93. (DEFAULT_MEM64_MAX_PAGES * (uint64)64 * (uint64)BH_KB)
  94. /* Macro to check memory flag and return appropriate memory size */
  95. #define GET_MAX_LINEAR_MEMORY_SIZE(is_memory64) \
  96. (is_memory64 ? MAX_LINEAR_MEM64_MEMORY_SIZE : MAX_LINEAR_MEMORY_SIZE)
  97. #if WASM_ENABLE_GC == 0
  98. typedef uintptr_t table_elem_type_t;
  99. #define NULL_REF (0xFFFFFFFF)
  100. #else
  101. typedef void *table_elem_type_t;
  102. #define NULL_REF (NULL)
  103. #define REF_CELL_NUM ((uint32)sizeof(uintptr_t) / sizeof(uint32))
  104. #endif
  105. #define INIT_EXPR_NONE 0x00
  106. #define INIT_EXPR_TYPE_I32_CONST 0x41
  107. #define INIT_EXPR_TYPE_I64_CONST 0x42
  108. #define INIT_EXPR_TYPE_F32_CONST 0x43
  109. #define INIT_EXPR_TYPE_F64_CONST 0x44
  110. #define INIT_EXPR_TYPE_V128_CONST 0xFD
  111. #define INIT_EXPR_TYPE_GET_GLOBAL 0x23
  112. #define INIT_EXPR_TYPE_REFNULL_CONST 0xD0
  113. #define INIT_EXPR_TYPE_FUNCREF_CONST 0xD2
  114. #define INIT_EXPR_TYPE_STRUCT_NEW 0xD3
  115. #define INIT_EXPR_TYPE_STRUCT_NEW_DEFAULT 0xD4
  116. #define INIT_EXPR_TYPE_ARRAY_NEW 0xD5
  117. #define INIT_EXPR_TYPE_ARRAY_NEW_DEFAULT 0xD6
  118. #define INIT_EXPR_TYPE_ARRAY_NEW_FIXED 0xD7
  119. #define INIT_EXPR_TYPE_I31_NEW 0xD8
  120. #define INIT_EXPR_TYPE_ANY_CONVERT_EXTERN 0xD9
  121. #define INIT_EXPR_TYPE_EXTERN_CONVERT_ANY 0xDA
  122. #define WASM_MAGIC_NUMBER 0x6d736100
  123. #define WASM_CURRENT_VERSION 1
  124. #define SECTION_TYPE_USER 0
  125. #define SECTION_TYPE_TYPE 1
  126. #define SECTION_TYPE_IMPORT 2
  127. #define SECTION_TYPE_FUNC 3
  128. #define SECTION_TYPE_TABLE 4
  129. #define SECTION_TYPE_MEMORY 5
  130. #define SECTION_TYPE_GLOBAL 6
  131. #define SECTION_TYPE_EXPORT 7
  132. #define SECTION_TYPE_START 8
  133. #define SECTION_TYPE_ELEM 9
  134. #define SECTION_TYPE_CODE 10
  135. #define SECTION_TYPE_DATA 11
  136. #if WASM_ENABLE_BULK_MEMORY != 0
  137. #define SECTION_TYPE_DATACOUNT 12
  138. #endif
  139. #if WASM_ENABLE_TAGS != 0
  140. #define SECTION_TYPE_TAG 13
  141. #endif
  142. #if WASM_ENABLE_STRINGREF != 0
  143. #define SECTION_TYPE_STRINGREF 14
  144. #endif
  145. #define SUB_SECTION_TYPE_MODULE 0
  146. #define SUB_SECTION_TYPE_FUNC 1
  147. #define SUB_SECTION_TYPE_LOCAL 2
  148. #define IMPORT_KIND_FUNC 0
  149. #define IMPORT_KIND_TABLE 1
  150. #define IMPORT_KIND_MEMORY 2
  151. #define IMPORT_KIND_GLOBAL 3
  152. #if WASM_ENABLE_TAGS != 0
  153. #define IMPORT_KIND_TAG 4
  154. #endif
  155. #define EXPORT_KIND_FUNC 0
  156. #define EXPORT_KIND_TABLE 1
  157. #define EXPORT_KIND_MEMORY 2
  158. #define EXPORT_KIND_GLOBAL 3
  159. #if WASM_ENABLE_TAGS != 0
  160. #define EXPORT_KIND_TAG 4
  161. #endif
  162. #define LABEL_TYPE_BLOCK 0
  163. #define LABEL_TYPE_LOOP 1
  164. #define LABEL_TYPE_IF 2
  165. #define LABEL_TYPE_FUNCTION 3
  166. #if WASM_ENABLE_EXCE_HANDLING != 0
  167. #define LABEL_TYPE_TRY 4
  168. #define LABEL_TYPE_CATCH 5
  169. #define LABEL_TYPE_CATCH_ALL 6
  170. #endif
  171. #define WASM_TYPE_FUNC 0
  172. #define WASM_TYPE_STRUCT 1
  173. #define WASM_TYPE_ARRAY 2
  174. #if WASM_ENABLE_STRINGREF != 0
  175. #define WASM_TYPE_STRINGREF 3
  176. #define WASM_TYPE_STRINGVIEWWTF8 4
  177. #define WASM_TYPE_STRINGVIEWWTF16 5
  178. #define WASM_TYPE_STRINGVIEWITER 6
  179. #endif
  180. /* In WasmGC, a table can start with [0x40 0x00] to indicate it has an
  181. * initializer */
  182. #define TABLE_INIT_EXPR_FLAG 0x40
  183. typedef struct WASMModule WASMModule;
  184. typedef struct WASMFunction WASMFunction;
  185. typedef struct WASMGlobal WASMGlobal;
  186. #if WASM_ENABLE_TAGS != 0
  187. typedef struct WASMTag WASMTag;
  188. #endif
  189. #ifndef WASM_VALUE_DEFINED
  190. #define WASM_VALUE_DEFINED
  191. typedef union V128 {
  192. int8 i8x16[16];
  193. int16 i16x8[8];
  194. int32 i32x4[4];
  195. int64 i64x2[2];
  196. float32 f32x4[4];
  197. float64 f64x2[2];
  198. } V128;
  199. typedef union WASMValue {
  200. int32 i32;
  201. uint32 u32;
  202. uint32 global_index;
  203. uint32 ref_index;
  204. int64 i64;
  205. uint64 u64;
  206. float32 f32;
  207. float64 f64;
  208. V128 v128;
  209. #if WASM_ENABLE_GC != 0
  210. wasm_obj_t gc_obj;
  211. uint32 type_index;
  212. struct {
  213. uint32 type_index;
  214. uint32 length;
  215. } array_new_default;
  216. /* pointer to a memory space holding more data, current usage:
  217. * struct.new init value: WASMStructNewInitValues *
  218. * array.new init value: WASMArrayNewInitValues *
  219. */
  220. void *data;
  221. #endif
  222. } WASMValue;
  223. #endif /* end of WASM_VALUE_DEFINED */
  224. typedef struct WASMStructNewInitValues {
  225. uint32 type_idx;
  226. uint32 count;
  227. WASMValue fields[1];
  228. } WASMStructNewInitValues;
  229. typedef struct WASMArrayNewInitValues {
  230. uint32 type_idx;
  231. uint32 length;
  232. WASMValue elem_data[1];
  233. } WASMArrayNewInitValues;
  234. typedef struct InitializerExpression {
  235. /* type of INIT_EXPR_TYPE_XXX, which is an instruction of
  236. constant expression */
  237. uint8 init_expr_type;
  238. WASMValue u;
  239. } InitializerExpression;
  240. #if WASM_ENABLE_GC != 0
  241. /**
  242. * Reference type of (ref null ht) or (ref ht),
  243. * and heap type is defined type (type i), i >= 0
  244. */
  245. typedef struct RefHeapType_TypeIdx {
  246. /* ref_type is REF_TYPE_HT_NULLABLE or
  247. REF_TYPE_HT_NON_NULLABLE, (0x63 or 0x64) */
  248. uint8 ref_type;
  249. /* true if ref_type is REF_TYPE_HT_NULLABLE */
  250. bool nullable;
  251. /* heap type is defined type: type_index >= 0 */
  252. int32 type_idx;
  253. } RefHeapType_TypeIdx;
  254. /**
  255. * Reference type of (ref null ht) or (ref ht),
  256. * and heap type is non-defined type
  257. */
  258. typedef struct RefHeapType_Common {
  259. /* ref_type is REF_TYPE_HT_NULLABLE or
  260. REF_TYPE_HT_NON_NULLABLE (0x63 or 0x64) */
  261. uint8 ref_type;
  262. /* true if ref_type is REF_TYPE_HT_NULLABLE */
  263. bool nullable;
  264. /* Common heap type (not defined type):
  265. -0x10 (func), -0x11 (extern), -0x12 (any), -0x13 (eq),
  266. -0x16 (i31), -0x17 (nofunc), -0x18 (noextern),
  267. -0x19 (struct), -0x20 (array), -0x21 (none) */
  268. int32 heap_type;
  269. } RefHeapType_Common;
  270. /**
  271. * Reference type
  272. */
  273. typedef union WASMRefType {
  274. uint8 ref_type;
  275. RefHeapType_TypeIdx ref_ht_typeidx;
  276. RefHeapType_Common ref_ht_common;
  277. } WASMRefType;
  278. typedef struct WASMRefTypeMap {
  279. /**
  280. * The type index of a type array, which only stores
  281. * the first byte of the type, e.g. WASMFuncType.types,
  282. * WASMStructType.fields
  283. */
  284. uint16 index;
  285. /* The full type info if the type cannot be described
  286. with one byte */
  287. WASMRefType *ref_type;
  288. } WASMRefTypeMap;
  289. #endif /* end of WASM_ENABLE_GC */
  290. #if WASM_ENABLE_GC == 0
  291. typedef struct WASMFuncType WASMType;
  292. typedef WASMType *WASMTypePtr;
  293. #else
  294. /**
  295. * Common type, store the same fields of
  296. * WASMFuncType, WASMStructType and WASMArrayType
  297. */
  298. typedef struct WASMType {
  299. /**
  300. * type_flag must be WASM_TYPE_FUNC/STRUCT/ARRAY to
  301. * denote that it is a WASMFuncType, WASMStructType or
  302. * WASMArrayType
  303. */
  304. uint16 type_flag;
  305. bool is_sub_final;
  306. /* How many types are referring to this type */
  307. uint16 ref_count;
  308. /* The inheritance depth */
  309. uint16 inherit_depth;
  310. /* The root type */
  311. struct WASMType *root_type;
  312. /* The parent type */
  313. struct WASMType *parent_type;
  314. uint32 parent_type_idx;
  315. /* The number of internal types in the current rec group, and if
  316. the type is not in a recursive group, rec_count is 1 since a
  317. single type definition is reinterpreted as a short-hand for a
  318. recursive group containing just one type */
  319. uint16 rec_count;
  320. uint16 rec_idx;
  321. /* The index of the begin type of this group */
  322. uint32 rec_begin_type_idx;
  323. } WASMType, *WASMTypePtr;
  324. #endif /* end of WASM_ENABLE_GC */
  325. /* Function type */
  326. typedef struct WASMFuncType {
  327. #if WASM_ENABLE_GC != 0
  328. WASMType base_type;
  329. #endif
  330. uint16 param_count;
  331. uint16 result_count;
  332. uint16 param_cell_num;
  333. uint16 ret_cell_num;
  334. #if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT != 0 \
  335. && WASM_ENABLE_LAZY_JIT != 0
  336. /* Code block to call llvm jit functions of this
  337. kind of function type from fast jit jitted code */
  338. void *call_to_llvm_jit_from_fast_jit;
  339. #endif
  340. #if WASM_ENABLE_GC != 0
  341. uint16 ref_type_map_count;
  342. WASMRefTypeMap *ref_type_maps;
  343. WASMRefTypeMap *result_ref_type_maps;
  344. #else
  345. uint16 ref_count;
  346. #endif
  347. #if WASM_ENABLE_QUICK_AOT_ENTRY != 0
  348. /* Quick AOT/JIT entry of this func type */
  349. void *quick_aot_entry;
  350. #endif
  351. /* types of params and results, only store the first byte
  352. * of the type, if it cannot be described with one byte,
  353. * then the full type info is stored in ref_type_maps */
  354. uint8 types[1];
  355. } WASMFuncType;
  356. #if WASM_ENABLE_GC != 0
  357. typedef struct WASMStructFieldType {
  358. uint16 field_flags;
  359. uint8 field_type;
  360. uint8 field_size;
  361. uint32 field_offset;
  362. #if WASM_ENABLE_WAMR_COMPILER != 0 || WASM_ENABLE_JIT != 0
  363. /*
  364. * The field size and field offset of a wasm struct may vary
  365. * in 32-bit target and 64-bit target, e.g., the size of a
  366. * GC reference is 4 bytes in the former and 8 bytes in the
  367. * latter, the AOT compiler needs to use the correct field
  368. * offset according to the target info.
  369. */
  370. uint8 field_size_64bit;
  371. uint8 field_size_32bit;
  372. uint32 field_offset_64bit;
  373. uint32 field_offset_32bit;
  374. #endif
  375. } WASMStructFieldType;
  376. typedef struct WASMStructType {
  377. WASMType base_type;
  378. /* total size of this struct object */
  379. uint32 total_size;
  380. uint16 field_count;
  381. uint16 ref_type_map_count;
  382. WASMRefTypeMap *ref_type_maps;
  383. /* Offsets of reference fields that need to be traced during GC.
  384. The first element of the table is the number of such offsets. */
  385. uint16 *reference_table;
  386. /* Field info, note that fields[i]->field_type only stores
  387. * the first byte of the field type, if it cannot be described
  388. * with one byte, then the full field type info is stored in
  389. * ref_type_maps */
  390. WASMStructFieldType fields[1];
  391. } WASMStructType;
  392. typedef struct WASMArrayType {
  393. WASMType base_type;
  394. uint16 elem_flags;
  395. uint8 elem_type;
  396. /* The full elem type info if the elem type cannot be
  397. described with one byte */
  398. WASMRefType *elem_ref_type;
  399. } WASMArrayType;
  400. #if WASM_ENABLE_STRINGREF != 0
  401. /* stringref representation, we define it as a void * pointer here, the
  402. * stringref implementation can use any structure */
  403. /*
  404. WasmGC heap
  405. +-----------------------+
  406. | |
  407. | stringref |
  408. | +----------+ | external string representation
  409. | | host_ptr |--------o------+----->+------------+
  410. | +----------+ | | | |
  411. | | | +------------+
  412. | stringview_wtf8/16 | |
  413. | +----------+ | |
  414. | | host_ptr |--------o------+
  415. | +----------+ | |
  416. | | |
  417. | stringview_iter | |
  418. | +----------+ | |
  419. | | host_ptr |--------o------+
  420. | +----------+ |
  421. | | pos | |
  422. | +----------+ |
  423. | |
  424. +-----------------------+
  425. */
  426. typedef void *WASMString;
  427. #endif /* end of WASM_ENABLE_STRINGREF != 0 */
  428. #endif /* end of WASM_ENABLE_GC != 0 */
  429. typedef struct WASMTableType {
  430. uint8 elem_type;
  431. /**
  432. * 0: no max size and not shared
  433. * 1: has max size
  434. * 2: shared
  435. */
  436. uint8 flags;
  437. bool possible_grow;
  438. uint32 init_size;
  439. /* specified if (flags & 1), else it is 0x10000 */
  440. uint32 max_size;
  441. #if WASM_ENABLE_GC != 0
  442. WASMRefType *elem_ref_type;
  443. #endif
  444. } WASMTableType;
  445. typedef struct WASMTable {
  446. WASMTableType table_type;
  447. #if WASM_ENABLE_GC != 0
  448. /* init expr for the whole table */
  449. InitializerExpression init_expr;
  450. #endif
  451. } WASMTable;
  452. #if WASM_ENABLE_MEMORY64 != 0
  453. typedef uint64 mem_offset_t;
  454. #define PR_MEM_OFFSET PRIu64
  455. #else
  456. typedef uint32 mem_offset_t;
  457. #define PR_MEM_OFFSET PRIu32
  458. #endif
  459. typedef struct WASMMemory {
  460. uint32 flags;
  461. uint32 num_bytes_per_page;
  462. uint32 init_page_count;
  463. uint32 max_page_count;
  464. } WASMMemory;
  465. #ifndef WASM_MEMORY_T_DEFINED
  466. #define WASM_MEMORY_T_DEFINED
  467. typedef struct WASMMemory WASMMemoryType;
  468. #endif
  469. typedef struct WASMTableImport {
  470. char *module_name;
  471. char *field_name;
  472. WASMTableType table_type;
  473. #if WASM_ENABLE_MULTI_MODULE != 0
  474. WASMModule *import_module;
  475. WASMTable *import_table_linked;
  476. #endif
  477. } WASMTableImport;
  478. typedef struct WASMMemoryImport {
  479. char *module_name;
  480. char *field_name;
  481. WASMMemoryType mem_type;
  482. #if WASM_ENABLE_MULTI_MODULE != 0
  483. WASMModule *import_module;
  484. WASMMemory *import_memory_linked;
  485. #endif
  486. } WASMMemoryImport;
  487. typedef struct WASMFunctionImport {
  488. char *module_name;
  489. char *field_name;
  490. /* function type */
  491. WASMFuncType *func_type;
  492. /* native function pointer after linked */
  493. void *func_ptr_linked;
  494. /* signature from registered native symbols */
  495. const char *signature;
  496. /* attachment */
  497. void *attachment;
  498. #if WASM_ENABLE_GC != 0
  499. /* the type index of this function's func_type */
  500. uint32 type_idx;
  501. #endif
  502. bool call_conv_raw;
  503. bool call_conv_wasm_c_api;
  504. #if WASM_ENABLE_MULTI_MODULE != 0
  505. WASMModule *import_module;
  506. WASMFunction *import_func_linked;
  507. #endif
  508. } WASMFunctionImport;
  509. #if WASM_ENABLE_TAGS != 0
  510. typedef struct WASMTagImport {
  511. char *module_name;
  512. char *field_name;
  513. uint8 attribute; /* the type of the tag (numerical) */
  514. uint32 type; /* the type of the catch function (numerical)*/
  515. WASMFuncType *tag_type;
  516. void *tag_ptr_linked;
  517. #if WASM_ENABLE_MULTI_MODULE != 0
  518. /* imported tag pointer after linked */
  519. WASMModule *import_module;
  520. WASMTag *import_tag_linked;
  521. uint32 import_tag_index_linked;
  522. #endif
  523. } WASMTagImport;
  524. #endif
  525. typedef struct WASMGlobalType {
  526. uint8 val_type;
  527. bool is_mutable;
  528. } WASMGlobalType;
  529. typedef struct WASMGlobalImport {
  530. char *module_name;
  531. char *field_name;
  532. WASMGlobalType type;
  533. bool is_linked;
  534. /* global data after linked */
  535. WASMValue global_data_linked;
  536. #if WASM_ENABLE_GC != 0
  537. WASMRefType *ref_type;
  538. #endif
  539. #if WASM_ENABLE_MULTI_MODULE != 0
  540. /* imported function pointer after linked */
  541. /* TODO: remove if not needed */
  542. WASMModule *import_module;
  543. WASMGlobal *import_global_linked;
  544. #endif
  545. #if WASM_ENABLE_FAST_JIT != 0
  546. /* The data offset of current global in global data */
  547. uint32 data_offset;
  548. #endif
  549. } WASMGlobalImport;
  550. typedef struct WASMImport {
  551. uint8 kind;
  552. union {
  553. WASMFunctionImport function;
  554. WASMTableImport table;
  555. WASMMemoryImport memory;
  556. #if WASM_ENABLE_TAGS != 0
  557. WASMTagImport tag;
  558. #endif
  559. WASMGlobalImport global;
  560. struct {
  561. char *module_name;
  562. char *field_name;
  563. } names;
  564. } u;
  565. } WASMImport;
  566. struct WASMFunction {
  567. #if WASM_ENABLE_CUSTOM_NAME_SECTION != 0
  568. char *field_name;
  569. #endif
  570. /* the type of function */
  571. WASMFuncType *func_type;
  572. uint32 local_count;
  573. uint8 *local_types;
  574. #if WASM_ENABLE_GC != 0
  575. uint16 local_ref_type_map_count;
  576. WASMRefTypeMap *local_ref_type_maps;
  577. #endif
  578. /* cell num of parameters */
  579. uint16 param_cell_num;
  580. /* cell num of return type */
  581. uint16 ret_cell_num;
  582. /* cell num of local variables */
  583. uint16 local_cell_num;
  584. /* offset of each local, including function parameters
  585. and local variables */
  586. uint16 *local_offsets;
  587. uint32 max_stack_cell_num;
  588. uint32 max_block_num;
  589. uint32 code_size;
  590. uint8 *code;
  591. #if WASM_ENABLE_FAST_INTERP != 0
  592. uint32 code_compiled_size;
  593. uint8 *code_compiled;
  594. uint8 *consts;
  595. uint32 const_cell_num;
  596. #endif
  597. #if WASM_ENABLE_GC != 0
  598. /* the type index of this function's func_type */
  599. uint32 type_idx;
  600. #endif
  601. #if WASM_ENABLE_EXCE_HANDLING != 0
  602. uint32 exception_handler_count;
  603. #endif
  604. #if WASM_ENABLE_FAST_JIT != 0 || WASM_ENABLE_JIT != 0 \
  605. || WASM_ENABLE_WAMR_COMPILER != 0
  606. /* Whether function has opcode memory.grow */
  607. bool has_op_memory_grow;
  608. /* Whether function has opcode call or call_indirect */
  609. bool has_op_func_call;
  610. #endif
  611. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  612. /* Whether function has memory operation opcodes */
  613. bool has_memory_operations;
  614. /* Whether function has opcode call_indirect */
  615. bool has_op_call_indirect;
  616. /* Whether function has opcode set_global_aux_stack */
  617. bool has_op_set_global_aux_stack;
  618. #endif
  619. #if WASM_ENABLE_FAST_JIT != 0
  620. /* The compiled fast jit jitted code block of this function */
  621. void *fast_jit_jitted_code;
  622. #if WASM_ENABLE_JIT != 0 && WASM_ENABLE_LAZY_JIT != 0
  623. /* The compiled llvm jit func ptr of this function */
  624. void *llvm_jit_func_ptr;
  625. /* Code block to call fast jit jitted code of this function
  626. from the llvm jit jitted code */
  627. void *call_to_fast_jit_from_llvm_jit;
  628. #endif
  629. #endif
  630. };
  631. #if WASM_ENABLE_TAGS != 0
  632. struct WASMTag {
  633. uint8 attribute; /* the attribute property of the tag (expected to be 0) */
  634. uint32 type; /* the type of the tag (expected valid inden in type table) */
  635. WASMFuncType *tag_type;
  636. };
  637. #endif
  638. struct WASMGlobal {
  639. WASMGlobalType type;
  640. #if WASM_ENABLE_GC != 0
  641. WASMRefType *ref_type;
  642. #endif
  643. InitializerExpression init_expr;
  644. #if WASM_ENABLE_FAST_JIT != 0
  645. /* The data offset of current global in global data */
  646. uint32 data_offset;
  647. #endif
  648. };
  649. typedef struct WASMExport {
  650. char *name;
  651. uint8 kind;
  652. uint32 index;
  653. } WASMExport;
  654. typedef struct WASMTableSeg {
  655. /* 0 to 7 */
  656. uint32 mode;
  657. /* funcref or externref, elemkind will be considered as funcref */
  658. uint32 elem_type;
  659. #if WASM_ENABLE_GC != 0
  660. WASMRefType *elem_ref_type;
  661. #endif
  662. /* optional, only for active */
  663. uint32 table_index;
  664. InitializerExpression base_offset;
  665. uint32 value_count;
  666. InitializerExpression *init_values;
  667. } WASMTableSeg;
  668. typedef struct WASMDataSeg {
  669. uint32 memory_index;
  670. InitializerExpression base_offset;
  671. uint32 data_length;
  672. #if WASM_ENABLE_BULK_MEMORY != 0
  673. bool is_passive;
  674. #endif
  675. uint8 *data;
  676. bool is_data_cloned;
  677. } WASMDataSeg;
  678. typedef struct BlockAddr {
  679. const uint8 *start_addr;
  680. uint8 *else_addr;
  681. uint8 *end_addr;
  682. } BlockAddr;
  683. #if WASM_ENABLE_LIBC_WASI != 0
  684. typedef struct WASIArguments {
  685. const char **dir_list;
  686. uint32 dir_count;
  687. const char **map_dir_list;
  688. uint32 map_dir_count;
  689. const char **env;
  690. uint32 env_count;
  691. /* in CIDR notation */
  692. const char **addr_pool;
  693. uint32 addr_count;
  694. const char **ns_lookup_pool;
  695. uint32 ns_lookup_count;
  696. char **argv;
  697. uint32 argc;
  698. os_raw_file_handle stdio[3];
  699. } WASIArguments;
  700. #endif
  701. typedef struct StringNode {
  702. struct StringNode *next;
  703. char *str;
  704. } StringNode, *StringList;
  705. typedef struct BrTableCache {
  706. struct BrTableCache *next;
  707. /* Address of br_table opcode */
  708. uint8 *br_table_op_addr;
  709. uint32 br_count;
  710. uint32 br_depths[1];
  711. } BrTableCache;
  712. #if WASM_ENABLE_DEBUG_INTERP != 0
  713. typedef struct WASMFastOPCodeNode {
  714. struct WASMFastOPCodeNode *next;
  715. uint64 offset;
  716. uint8 orig_op;
  717. } WASMFastOPCodeNode;
  718. #endif
  719. #if WASM_ENABLE_LOAD_CUSTOM_SECTION != 0
  720. typedef struct WASMCustomSection {
  721. struct WASMCustomSection *next;
  722. /* Start address of the section name */
  723. char *name_addr;
  724. /* Length of the section name decoded from leb */
  725. uint32 name_len;
  726. /* Start address of the content (name len and name skipped) */
  727. uint8 *content_addr;
  728. uint32 content_len;
  729. } WASMCustomSection;
  730. #endif
  731. #if WASM_ENABLE_FAST_JIT != 0 || WASM_ENABLE_JIT != 0
  732. struct AOTCompData;
  733. struct AOTCompContext;
  734. /* Orc JIT thread arguments */
  735. typedef struct OrcJitThreadArg {
  736. #if WASM_ENABLE_JIT != 0
  737. struct AOTCompContext *comp_ctx;
  738. #endif
  739. struct WASMModule *module;
  740. uint32 group_idx;
  741. } OrcJitThreadArg;
  742. #endif
  743. struct WASMModuleInstance;
  744. struct WASMModule {
  745. /* Module type, for module loaded from WASM bytecode binary,
  746. this field is Wasm_Module_Bytecode;
  747. for module loaded from AOT file, this field is
  748. Wasm_Module_AoT, and this structure should be treated as
  749. AOTModule structure. */
  750. uint32 module_type;
  751. /* the package version read from the WASM file */
  752. uint32 package_version;
  753. uint32 type_count;
  754. uint32 import_count;
  755. uint32 function_count;
  756. uint32 table_count;
  757. uint32 memory_count;
  758. #if WASM_ENABLE_TAGS != 0
  759. uint32 tag_count;
  760. #endif
  761. uint32 global_count;
  762. uint32 export_count;
  763. uint32 table_seg_count;
  764. /* data seg count read from data segment section */
  765. uint32 data_seg_count;
  766. #if WASM_ENABLE_BULK_MEMORY != 0
  767. /* data count read from datacount section */
  768. uint32 data_seg_count1;
  769. #endif
  770. #if WASM_ENABLE_GC != 0
  771. #if WASM_ENABLE_STRINGREF != 0
  772. uint32 string_literal_count;
  773. uint32 *string_literal_lengths;
  774. const uint8 **string_literal_ptrs;
  775. #endif
  776. #endif
  777. uint32 import_function_count;
  778. uint32 import_table_count;
  779. uint32 import_memory_count;
  780. #if WASM_ENABLE_TAGS != 0
  781. uint32 import_tag_count;
  782. #endif
  783. uint32 import_global_count;
  784. WASMImport *import_functions;
  785. WASMImport *import_tables;
  786. WASMImport *import_memories;
  787. #if WASM_ENABLE_TAGS != 0
  788. WASMImport *import_tags;
  789. #endif
  790. WASMImport *import_globals;
  791. WASMType **types;
  792. WASMImport *imports;
  793. WASMFunction **functions;
  794. WASMTable *tables;
  795. WASMMemory *memories;
  796. #if WASM_ENABLE_TAGS != 0
  797. WASMTag **tags;
  798. #endif
  799. WASMGlobal *globals;
  800. WASMExport *exports;
  801. WASMTableSeg *table_segments;
  802. WASMDataSeg **data_segments;
  803. uint32 start_function;
  804. /* total global variable size */
  805. uint32 global_data_size;
  806. /* the index of auxiliary __data_end global,
  807. -1 means unexported */
  808. uint32 aux_data_end_global_index;
  809. /* auxiliary __data_end exported by wasm app */
  810. uint64 aux_data_end;
  811. /* the index of auxiliary __heap_base global,
  812. -1 means unexported */
  813. uint32 aux_heap_base_global_index;
  814. /* auxiliary __heap_base exported by wasm app */
  815. uint64 aux_heap_base;
  816. /* the index of auxiliary stack top global,
  817. -1 means unexported */
  818. uint32 aux_stack_top_global_index;
  819. /* auxiliary stack bottom resolved */
  820. uint64 aux_stack_bottom;
  821. /* auxiliary stack size resolved */
  822. uint32 aux_stack_size;
  823. /* the index of malloc/free function,
  824. -1 means unexported */
  825. uint32 malloc_function;
  826. uint32 free_function;
  827. /* the index of __retain function,
  828. -1 means unexported */
  829. uint32 retain_function;
  830. /* Whether there is possible memory grow, e.g. memory.grow opcode */
  831. bool possible_memory_grow;
  832. StringList const_str_list;
  833. #if WASM_ENABLE_FAST_INTERP == 0
  834. bh_list br_table_cache_list_head;
  835. bh_list *br_table_cache_list;
  836. #endif
  837. #if WASM_ENABLE_LIBC_WASI != 0
  838. WASIArguments wasi_args;
  839. bool import_wasi_api;
  840. #endif
  841. #if WASM_ENABLE_MULTI_MODULE != 0
  842. /* TODO: add mutex for mutli-thread? */
  843. bh_list import_module_list_head;
  844. bh_list *import_module_list;
  845. #endif
  846. #if WASM_ENABLE_GC != 0
  847. /* Ref types hash set */
  848. HashMap *ref_type_set;
  849. struct WASMRttType **rtt_types;
  850. korp_mutex rtt_type_lock;
  851. #if WASM_ENABLE_STRINGREF != 0
  852. /* special rtts for stringref types
  853. - stringref
  854. - stringview_wtf8
  855. - stringview_wtf16
  856. - stringview_iter
  857. */
  858. struct WASMRttType *stringref_rtts[4];
  859. #endif
  860. #endif
  861. #if WASM_ENABLE_DEBUG_INTERP != 0 || WASM_ENABLE_DEBUG_AOT != 0
  862. bh_list fast_opcode_list;
  863. uint8 *buf_code;
  864. uint64 buf_code_size;
  865. #endif
  866. #if WASM_ENABLE_DEBUG_INTERP != 0 || WASM_ENABLE_FAST_JIT != 0 \
  867. || WASM_ENABLE_DUMP_CALL_STACK != 0 || WASM_ENABLE_JIT != 0
  868. uint8 *load_addr;
  869. uint64 load_size;
  870. #endif
  871. #if WASM_ENABLE_DEBUG_INTERP != 0 \
  872. || (WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT != 0 \
  873. && WASM_ENABLE_LAZY_JIT != 0)
  874. /**
  875. * List of instances referred to this module. When source debugging
  876. * feature is enabled, the debugger may modify the code section of
  877. * the module, so we need to report a warning if user create several
  878. * instances based on the same module.
  879. *
  880. * Also add the instance to the list for Fast JIT to LLVM JIT
  881. * tier-up, since we need to lazily update the LLVM func pointers
  882. * in the instance.
  883. */
  884. struct WASMModuleInstance *instance_list;
  885. korp_mutex instance_list_lock;
  886. #endif
  887. #if WASM_ENABLE_CUSTOM_NAME_SECTION != 0
  888. const uint8 *name_section_buf;
  889. const uint8 *name_section_buf_end;
  890. #endif
  891. #if WASM_ENABLE_LOAD_CUSTOM_SECTION != 0
  892. WASMCustomSection *custom_section_list;
  893. #endif
  894. #if WASM_ENABLE_FAST_JIT != 0
  895. /**
  896. * func pointers of Fast JITed (un-imported) functions
  897. * for non Multi-Tier JIT mode:
  898. * (1) when lazy jit is disabled, each pointer is set to the compiled
  899. * fast jit jitted code
  900. * (2) when lazy jit is enabled, each pointer is firstly inited as
  901. * jit_global->compile_fast_jit_and_then_call, and then set to the
  902. * compiled fast jit jitted code when it is called (the stub will
  903. * compile the jit function and then update itself)
  904. * for Multi-Tier JIT mode:
  905. * each pointer is firstly inited as compile_fast_jit_and_then_call,
  906. * and then set to the compiled fast jit jitted code when it is called,
  907. * and when the llvm jit func ptr of the same function is compiled, it
  908. * will be set to call_to_llvm_jit_from_fast_jit of this function type
  909. * (tier-up from fast-jit to llvm-jit)
  910. */
  911. void **fast_jit_func_ptrs;
  912. /* locks for Fast JIT lazy compilation */
  913. korp_mutex fast_jit_thread_locks[WASM_ORC_JIT_BACKEND_THREAD_NUM];
  914. bool fast_jit_thread_locks_inited[WASM_ORC_JIT_BACKEND_THREAD_NUM];
  915. #endif
  916. #if WASM_ENABLE_JIT != 0
  917. struct AOTCompData *comp_data;
  918. struct AOTCompContext *comp_ctx;
  919. /**
  920. * func pointers of LLVM JITed (un-imported) functions
  921. * for non Multi-Tier JIT mode:
  922. * each pointer is set to the looked up llvm jit func ptr, note that it
  923. * is a stub and will trigger the actual compilation when it is called
  924. * for Multi-Tier JIT mode:
  925. * each pointer is inited as call_to_fast_jit code block, when the llvm
  926. * jit func ptr is actually compiled, it is set to the compiled llvm jit
  927. * func ptr
  928. */
  929. void **func_ptrs;
  930. /* whether the func pointers are compiled */
  931. bool *func_ptrs_compiled;
  932. #endif
  933. #if WASM_ENABLE_FAST_JIT != 0 || WASM_ENABLE_JIT != 0
  934. /* backend compilation threads */
  935. korp_tid orcjit_threads[WASM_ORC_JIT_BACKEND_THREAD_NUM];
  936. /* backend thread arguments */
  937. OrcJitThreadArg orcjit_thread_args[WASM_ORC_JIT_BACKEND_THREAD_NUM];
  938. /* whether to stop the compilation of backend threads */
  939. bool orcjit_stop_compiling;
  940. #endif
  941. #if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT != 0 \
  942. && WASM_ENABLE_LAZY_JIT != 0
  943. /* wait lock/cond for the synchronization of
  944. the llvm jit initialization */
  945. korp_mutex tierup_wait_lock;
  946. korp_cond tierup_wait_cond;
  947. bool tierup_wait_lock_inited;
  948. korp_tid llvm_jit_init_thread;
  949. /* whether the llvm jit is initialized */
  950. bool llvm_jit_inited;
  951. /* Whether to enable llvm jit compilation:
  952. it is set to true only when there is a module instance starts to
  953. run with running mode Mode_LLVM_JIT or Mode_Multi_Tier_JIT,
  954. since no need to enable llvm jit compilation for Mode_Interp and
  955. Mode_Fast_JIT, so as to improve performance for them */
  956. bool enable_llvm_jit_compilation;
  957. /* The count of groups which finish compiling the fast jit
  958. functions in that group */
  959. uint32 fast_jit_ready_groups;
  960. #endif
  961. #if WASM_ENABLE_WAMR_COMPILER != 0
  962. bool is_simd_used;
  963. bool is_ref_types_used;
  964. bool is_bulk_memory_used;
  965. #endif
  966. /* user defined name */
  967. char *name;
  968. /* Whether the underlying wasm binary buffer can be freed */
  969. bool is_binary_freeable;
  970. };
  971. typedef struct BlockType {
  972. /* Block type may be expressed in one of two forms:
  973. * either by the type of the single return value or
  974. * by a type index of module.
  975. */
  976. union {
  977. struct {
  978. uint8 type;
  979. #if WASM_ENABLE_GC != 0
  980. WASMRefTypeMap ref_type_map;
  981. #endif
  982. } value_type;
  983. WASMFuncType *type;
  984. } u;
  985. bool is_value_type;
  986. } BlockType;
  987. typedef struct WASMBranchBlock {
  988. uint8 *begin_addr;
  989. uint8 *target_addr;
  990. uint32 *frame_sp;
  991. uint32 cell_num;
  992. #if WASM_ENABLE_EXCE_HANDLING != 0
  993. /* in exception handling, label_type needs to be stored to lookup exception
  994. * handlers */
  995. uint8 label_type;
  996. #endif
  997. } WASMBranchBlock;
  998. /**
  999. * Align an unsigned value on a alignment boundary.
  1000. *
  1001. * @param v the value to be aligned
  1002. * @param b the alignment boundary (2, 4, 8, ...)
  1003. *
  1004. * @return the aligned value
  1005. */
  1006. inline static unsigned
  1007. align_uint(unsigned v, unsigned b)
  1008. {
  1009. unsigned m = b - 1;
  1010. return (v + m) & ~m;
  1011. }
  1012. /**
  1013. * Align an 64 bit unsigned value on a alignment boundary.
  1014. *
  1015. * @param v the value to be aligned
  1016. * @param b the alignment boundary (2, 4, 8, ...)
  1017. *
  1018. * @return the aligned value
  1019. */
  1020. inline static uint64
  1021. align_uint64(uint64 v, uint64 b)
  1022. {
  1023. uint64 m = b - 1;
  1024. return (v + m) & ~m;
  1025. }
  1026. /**
  1027. * Check whether a piece of data is out of range
  1028. *
  1029. * @param offset the offset that the data starts
  1030. * @param len the length of the data
  1031. * @param max_size the maximum size of the data range
  1032. *
  1033. * @return true if out of range, false otherwise
  1034. */
  1035. inline static bool
  1036. offset_len_out_of_bounds(uint32 offset, uint32 len, uint32 max_size)
  1037. {
  1038. if (offset + len < offset /* integer overflow */
  1039. || offset + len > max_size)
  1040. return true;
  1041. return false;
  1042. }
  1043. /**
  1044. * Return the hash value of c string.
  1045. */
  1046. inline static uint32
  1047. wasm_string_hash(const char *str)
  1048. {
  1049. unsigned h = (unsigned)strlen(str);
  1050. const uint8 *p = (uint8 *)str;
  1051. const uint8 *end = p + h;
  1052. while (p != end)
  1053. h = ((h << 5) - h) + *p++;
  1054. return h;
  1055. }
  1056. /**
  1057. * Whether two c strings are equal.
  1058. */
  1059. inline static bool
  1060. wasm_string_equal(const char *s1, const char *s2)
  1061. {
  1062. return strcmp(s1, s2) == 0 ? true : false;
  1063. }
  1064. /**
  1065. * Return the byte size of value type with specific pointer size.
  1066. *
  1067. * Note: Please use wasm_value_type_size for interpreter, only aot compiler
  1068. * can use this API directly to calculate type size for different target
  1069. */
  1070. inline static uint32
  1071. wasm_value_type_size_internal(uint8 value_type, uint8 pointer_size)
  1072. {
  1073. if (value_type == VALUE_TYPE_VOID)
  1074. return 0;
  1075. else if (value_type == VALUE_TYPE_I32 || value_type == VALUE_TYPE_F32
  1076. || value_type == VALUE_TYPE_ANY)
  1077. return sizeof(int32);
  1078. else if (value_type == VALUE_TYPE_I64 || value_type == VALUE_TYPE_F64)
  1079. return sizeof(int64);
  1080. #if WASM_ENABLE_SIMD != 0
  1081. else if (value_type == VALUE_TYPE_V128)
  1082. return sizeof(int64) * 2;
  1083. #endif
  1084. #if WASM_ENABLE_GC == 0 && WASM_ENABLE_REF_TYPES != 0
  1085. else if (value_type == VALUE_TYPE_FUNCREF
  1086. || value_type == VALUE_TYPE_EXTERNREF)
  1087. return sizeof(uint32);
  1088. #elif WASM_ENABLE_GC != 0
  1089. else if ((value_type >= (uint8)REF_TYPE_ARRAYREF /* 0x6A */
  1090. && value_type <= (uint8)REF_TYPE_NULLFUNCREF) /* 0x73 */
  1091. || (value_type >= (uint8)REF_TYPE_HT_NULLABLE /* 0x63 */
  1092. && value_type <= (uint8)REF_TYPE_HT_NON_NULLABLE) /* 0x64 */
  1093. #if WASM_ENABLE_STRINGREF != 0
  1094. || (value_type >= (uint8)REF_TYPE_STRINGVIEWWTF8 /* 0x66 */
  1095. && value_type <= (uint8)REF_TYPE_STRINGREF) /* 0x67 */
  1096. || (value_type >= (uint8)REF_TYPE_STRINGVIEWITER /* 0x61 */
  1097. && value_type <= (uint8)REF_TYPE_STRINGVIEWWTF16) /* 0x62 */
  1098. #endif
  1099. )
  1100. return pointer_size;
  1101. else if (value_type == PACKED_TYPE_I8)
  1102. return sizeof(int8);
  1103. else if (value_type == PACKED_TYPE_I16)
  1104. return sizeof(int16);
  1105. #endif
  1106. else {
  1107. bh_assert(0);
  1108. }
  1109. return 0;
  1110. }
  1111. /**
  1112. * Return the cell num of value type with specific pointer size.
  1113. *
  1114. * Note: Please use wasm_value_type_cell_num for interpreter, only aot compiler
  1115. * can use this API directly to calculate type cell num for different target
  1116. */
  1117. inline static uint16
  1118. wasm_value_type_cell_num_internal(uint8 value_type, uint8 pointer_size)
  1119. {
  1120. return wasm_value_type_size_internal(value_type, pointer_size) / 4;
  1121. }
  1122. /**
  1123. * Return the byte size of value type.
  1124. */
  1125. inline static uint32
  1126. wasm_value_type_size(uint8 value_type)
  1127. {
  1128. return wasm_value_type_size_internal(value_type, sizeof(uintptr_t));
  1129. }
  1130. inline static uint16
  1131. wasm_value_type_cell_num(uint8 value_type)
  1132. {
  1133. return wasm_value_type_size(value_type) / 4;
  1134. }
  1135. inline static uint32
  1136. wasm_get_cell_num(const uint8 *types, uint32 type_count)
  1137. {
  1138. uint32 cell_num = 0;
  1139. uint32 i;
  1140. for (i = 0; i < type_count; i++)
  1141. cell_num += wasm_value_type_cell_num(types[i]);
  1142. return cell_num;
  1143. }
  1144. #if WASM_ENABLE_REF_TYPES != 0
  1145. inline static uint16
  1146. wasm_value_type_cell_num_outside(uint8 value_type)
  1147. {
  1148. if (VALUE_TYPE_EXTERNREF == value_type) {
  1149. return sizeof(uintptr_t) / sizeof(uint32);
  1150. }
  1151. else {
  1152. return wasm_value_type_cell_num(value_type);
  1153. }
  1154. }
  1155. #endif
  1156. #if WASM_ENABLE_GC == 0
  1157. inline static bool
  1158. wasm_type_equal(const WASMType *type1, const WASMType *type2,
  1159. const WASMTypePtr *types, uint32 type_count)
  1160. {
  1161. const WASMFuncType *func_type1 = (const WASMFuncType *)type1;
  1162. const WASMFuncType *func_type2 = (const WASMFuncType *)type2;
  1163. if (type1 == type2) {
  1164. return true;
  1165. }
  1166. return (func_type1->param_count == func_type2->param_count
  1167. && func_type1->result_count == func_type2->result_count
  1168. && memcmp(
  1169. func_type1->types, func_type2->types,
  1170. (uint32)(func_type1->param_count + func_type1->result_count))
  1171. == 0)
  1172. ? true
  1173. : false;
  1174. (void)types;
  1175. (void)type_count;
  1176. }
  1177. #else
  1178. /* implemented in gc_type.c */
  1179. bool
  1180. wasm_type_equal(const WASMType *type1, const WASMType *type2,
  1181. const WASMTypePtr *types, uint32 type_count);
  1182. #endif
  1183. inline static uint32
  1184. wasm_get_smallest_type_idx(const WASMTypePtr *types, uint32 type_count,
  1185. uint32 cur_type_idx)
  1186. {
  1187. uint32 i;
  1188. for (i = 0; i < cur_type_idx; i++) {
  1189. if (wasm_type_equal(types[cur_type_idx], types[i], types, type_count))
  1190. return i;
  1191. }
  1192. return cur_type_idx;
  1193. }
  1194. #if WASM_ENABLE_GC == 0
  1195. static inline uint32
  1196. block_type_get_param_types(BlockType *block_type, uint8 **p_param_types)
  1197. #else
  1198. static inline uint32
  1199. block_type_get_param_types(BlockType *block_type, uint8 **p_param_types,
  1200. WASMRefTypeMap **p_param_reftype_maps,
  1201. uint32 *p_param_reftype_map_count)
  1202. #endif
  1203. {
  1204. uint32 param_count = 0;
  1205. if (!block_type->is_value_type) {
  1206. WASMFuncType *func_type = block_type->u.type;
  1207. *p_param_types = func_type->types;
  1208. param_count = func_type->param_count;
  1209. #if WASM_ENABLE_GC != 0
  1210. *p_param_reftype_maps = func_type->ref_type_maps;
  1211. *p_param_reftype_map_count = (uint32)(func_type->result_ref_type_maps
  1212. - func_type->ref_type_maps);
  1213. #endif
  1214. }
  1215. else {
  1216. *p_param_types = NULL;
  1217. param_count = 0;
  1218. #if WASM_ENABLE_GC != 0
  1219. *p_param_reftype_maps = NULL;
  1220. *p_param_reftype_map_count = 0;
  1221. #endif
  1222. }
  1223. return param_count;
  1224. }
  1225. #if WASM_ENABLE_GC == 0
  1226. static inline uint32
  1227. block_type_get_result_types(BlockType *block_type, uint8 **p_result_types)
  1228. #else
  1229. static inline uint32
  1230. block_type_get_result_types(BlockType *block_type, uint8 **p_result_types,
  1231. WASMRefTypeMap **p_result_reftype_maps,
  1232. uint32 *p_result_reftype_map_count)
  1233. #endif
  1234. {
  1235. uint32 result_count = 0;
  1236. uint8 *result_types = NULL;
  1237. #if WASM_ENABLE_GC != 0
  1238. uint8 type;
  1239. uint32 result_reftype_map_count = 0;
  1240. WASMRefTypeMap *result_reftype_maps = NULL;
  1241. #endif
  1242. if (block_type->is_value_type) {
  1243. if (block_type->u.value_type.type != VALUE_TYPE_VOID) {
  1244. result_types = &block_type->u.value_type.type;
  1245. result_count = 1;
  1246. #if WASM_ENABLE_GC != 0
  1247. type = block_type->u.value_type.type;
  1248. if (type == (uint8)REF_TYPE_HT_NULLABLE
  1249. || type == (uint8)REF_TYPE_HT_NON_NULLABLE) {
  1250. result_reftype_maps = &block_type->u.value_type.ref_type_map;
  1251. result_reftype_map_count = 1;
  1252. }
  1253. #endif
  1254. }
  1255. }
  1256. else {
  1257. WASMFuncType *func_type = block_type->u.type;
  1258. result_types = func_type->types + func_type->param_count;
  1259. result_count = func_type->result_count;
  1260. #if WASM_ENABLE_GC != 0
  1261. result_reftype_maps = func_type->result_ref_type_maps;
  1262. result_reftype_map_count = (uint32)(func_type->ref_type_map_count
  1263. - (func_type->result_ref_type_maps
  1264. - func_type->ref_type_maps));
  1265. #endif
  1266. }
  1267. *p_result_types = result_types;
  1268. #if WASM_ENABLE_GC != 0
  1269. *p_result_reftype_maps = result_reftype_maps;
  1270. *p_result_reftype_map_count = result_reftype_map_count;
  1271. #endif
  1272. return result_count;
  1273. }
  1274. static inline uint32
  1275. block_type_get_arity(const BlockType *block_type, uint8 label_type)
  1276. {
  1277. if (label_type == LABEL_TYPE_LOOP) {
  1278. if (block_type->is_value_type)
  1279. return 0;
  1280. else
  1281. return block_type->u.type->param_count;
  1282. }
  1283. else {
  1284. if (block_type->is_value_type) {
  1285. return block_type->u.value_type.type != VALUE_TYPE_VOID ? 1 : 0;
  1286. }
  1287. else
  1288. return block_type->u.type->result_count;
  1289. }
  1290. return 0;
  1291. }
  1292. #ifdef __cplusplus
  1293. } /* end of extern "C" */
  1294. #endif
  1295. #endif /* end of _WASM_H_ */