wasm.h 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425
  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 i32x8[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. uint8 type_idx;
  226. uint32 count;
  227. WASMValue fields[1];
  228. } WASMStructNewInitValues;
  229. typedef struct WASMArrayNewInitValues {
  230. uint8 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 WASMTable {
  430. uint8 elem_type;
  431. /**
  432. * 0: no max size and not shared
  433. * 1: hax 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. /* init expr for the whole table */
  444. InitializerExpression init_expr;
  445. #endif
  446. } WASMTable;
  447. #if WASM_ENABLE_MEMORY64 != 0
  448. typedef uint64 mem_offset_t;
  449. #else
  450. typedef uint32 mem_offset_t;
  451. #endif
  452. typedef struct WASMMemory {
  453. uint32 flags;
  454. uint32 num_bytes_per_page;
  455. uint32 init_page_count;
  456. uint32 max_page_count;
  457. } WASMMemory;
  458. typedef struct WASMTableImport {
  459. char *module_name;
  460. char *field_name;
  461. /* 0: no max size, 1: has max size */
  462. uint8 elem_type;
  463. uint8 flags;
  464. bool possible_grow;
  465. uint32 init_size;
  466. /* specified if (flags & 1), else it is 0x10000 */
  467. uint32 max_size;
  468. #if WASM_ENABLE_GC != 0
  469. WASMRefType *elem_ref_type;
  470. #endif
  471. #if WASM_ENABLE_MULTI_MODULE != 0
  472. WASMModule *import_module;
  473. WASMTable *import_table_linked;
  474. #endif
  475. } WASMTableImport;
  476. typedef struct WASMMemoryImport {
  477. char *module_name;
  478. char *field_name;
  479. uint32 flags;
  480. uint32 num_bytes_per_page;
  481. uint32 init_page_count;
  482. uint32 max_page_count;
  483. #if WASM_ENABLE_MULTI_MODULE != 0
  484. WASMModule *import_module;
  485. WASMMemory *import_memory_linked;
  486. #endif
  487. } WASMMemoryImport;
  488. typedef struct WASMFunctionImport {
  489. char *module_name;
  490. char *field_name;
  491. /* function type */
  492. WASMFuncType *func_type;
  493. /* native function pointer after linked */
  494. void *func_ptr_linked;
  495. /* signature from registered native symbols */
  496. const char *signature;
  497. /* attachment */
  498. void *attachment;
  499. #if WASM_ENABLE_GC != 0
  500. /* the type index of this function's func_type */
  501. uint32 type_idx;
  502. #endif
  503. bool call_conv_raw;
  504. bool call_conv_wasm_c_api;
  505. #if WASM_ENABLE_MULTI_MODULE != 0
  506. WASMModule *import_module;
  507. WASMFunction *import_func_linked;
  508. #endif
  509. } WASMFunctionImport;
  510. #if WASM_ENABLE_TAGS != 0
  511. typedef struct WASMTagImport {
  512. char *module_name;
  513. char *field_name;
  514. uint8 attribute; /* the type of the tag (numerical) */
  515. uint32 type; /* the type of the catch function (numerical)*/
  516. WASMType *tag_type;
  517. void *tag_ptr_linked;
  518. #if WASM_ENABLE_MULTI_MODULE != 0
  519. /* imported tag pointer after linked */
  520. WASMModule *import_module;
  521. WASMTag *import_tag_linked;
  522. uint32 import_tag_index_linked;
  523. #endif
  524. } WASMTagImport;
  525. #endif
  526. typedef struct WASMGlobalImport {
  527. char *module_name;
  528. char *field_name;
  529. uint8 type;
  530. bool is_mutable;
  531. bool is_linked;
  532. /* global data after linked */
  533. WASMValue global_data_linked;
  534. #if WASM_ENABLE_GC != 0
  535. WASMRefType *ref_type;
  536. #endif
  537. #if WASM_ENABLE_MULTI_MODULE != 0
  538. /* imported function pointer after linked */
  539. /* TODO: remove if not needed */
  540. WASMModule *import_module;
  541. WASMGlobal *import_global_linked;
  542. #endif
  543. #if WASM_ENABLE_FAST_JIT != 0
  544. /* The data offset of current global in global data */
  545. uint32 data_offset;
  546. #endif
  547. } WASMGlobalImport;
  548. typedef struct WASMImport {
  549. uint8 kind;
  550. union {
  551. WASMFunctionImport function;
  552. WASMTableImport table;
  553. WASMMemoryImport memory;
  554. #if WASM_ENABLE_TAGS != 0
  555. WASMTagImport tag;
  556. #endif
  557. WASMGlobalImport global;
  558. struct {
  559. char *module_name;
  560. char *field_name;
  561. } names;
  562. } u;
  563. } WASMImport;
  564. struct WASMFunction {
  565. #if WASM_ENABLE_CUSTOM_NAME_SECTION != 0
  566. char *field_name;
  567. #endif
  568. /* the type of function */
  569. WASMFuncType *func_type;
  570. uint32 local_count;
  571. uint8 *local_types;
  572. #if WASM_ENABLE_GC != 0
  573. uint16 local_ref_type_map_count;
  574. WASMRefTypeMap *local_ref_type_maps;
  575. #endif
  576. /* cell num of parameters */
  577. uint16 param_cell_num;
  578. /* cell num of return type */
  579. uint16 ret_cell_num;
  580. /* cell num of local variables */
  581. uint16 local_cell_num;
  582. /* offset of each local, including function parameters
  583. and local variables */
  584. uint16 *local_offsets;
  585. uint32 max_stack_cell_num;
  586. uint32 max_block_num;
  587. uint32 code_size;
  588. uint8 *code;
  589. #if WASM_ENABLE_FAST_INTERP != 0
  590. uint32 code_compiled_size;
  591. uint8 *code_compiled;
  592. uint8 *consts;
  593. uint32 const_cell_num;
  594. #endif
  595. #if WASM_ENABLE_GC != 0
  596. /* the type index of this function's func_type */
  597. uint32 type_idx;
  598. #endif
  599. #if WASM_ENABLE_EXCE_HANDLING != 0
  600. uint32 exception_handler_count;
  601. #endif
  602. #if WASM_ENABLE_FAST_JIT != 0 || WASM_ENABLE_JIT != 0 \
  603. || WASM_ENABLE_WAMR_COMPILER != 0
  604. /* Whether function has opcode memory.grow */
  605. bool has_op_memory_grow;
  606. /* Whether function has opcode call or call_indirect */
  607. bool has_op_func_call;
  608. #endif
  609. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  610. /* Whether function has memory operation opcodes */
  611. bool has_memory_operations;
  612. /* Whether function has opcode call_indirect */
  613. bool has_op_call_indirect;
  614. /* Whether function has opcode set_global_aux_stack */
  615. bool has_op_set_global_aux_stack;
  616. #endif
  617. #if WASM_ENABLE_FAST_JIT != 0
  618. /* The compiled fast jit jitted code block of this function */
  619. void *fast_jit_jitted_code;
  620. #if WASM_ENABLE_JIT != 0 && WASM_ENABLE_LAZY_JIT != 0
  621. /* The compiled llvm jit func ptr of this function */
  622. void *llvm_jit_func_ptr;
  623. /* Code block to call fast jit jitted code of this function
  624. from the llvm jit jitted code */
  625. void *call_to_fast_jit_from_llvm_jit;
  626. #endif
  627. #endif
  628. };
  629. #if WASM_ENABLE_TAGS != 0
  630. struct WASMTag {
  631. uint8 attribute; /* the attribute property of the tag (expected to be 0) */
  632. uint32 type; /* the type of the tag (expected valid inden in type table) */
  633. WASMType *tag_type;
  634. };
  635. #endif
  636. struct WASMGlobal {
  637. uint8 type;
  638. bool is_mutable;
  639. #if WASM_ENABLE_GC != 0
  640. WASMRefType *ref_type;
  641. #endif
  642. InitializerExpression init_expr;
  643. #if WASM_ENABLE_FAST_JIT != 0
  644. /* The data offset of current global in global data */
  645. uint32 data_offset;
  646. #endif
  647. };
  648. typedef struct WASMExport {
  649. char *name;
  650. uint8 kind;
  651. uint32 index;
  652. } WASMExport;
  653. typedef struct WASMTableSeg {
  654. /* 0 to 7 */
  655. uint32 mode;
  656. /* funcref or externref, elemkind will be considered as funcref */
  657. uint32 elem_type;
  658. #if WASM_ENABLE_GC != 0
  659. WASMRefType *elem_ref_type;
  660. #endif
  661. /* optional, only for active */
  662. uint32 table_index;
  663. InitializerExpression base_offset;
  664. uint32 value_count;
  665. InitializerExpression *init_values;
  666. } WASMTableSeg;
  667. typedef struct WASMDataSeg {
  668. uint32 memory_index;
  669. InitializerExpression base_offset;
  670. uint32 data_length;
  671. #if WASM_ENABLE_BULK_MEMORY != 0
  672. bool is_passive;
  673. #endif
  674. uint8 *data;
  675. } WASMDataSeg;
  676. typedef struct BlockAddr {
  677. const uint8 *start_addr;
  678. uint8 *else_addr;
  679. uint8 *end_addr;
  680. } BlockAddr;
  681. #if WASM_ENABLE_LIBC_WASI != 0
  682. typedef struct WASIArguments {
  683. const char **dir_list;
  684. uint32 dir_count;
  685. const char **map_dir_list;
  686. uint32 map_dir_count;
  687. const char **env;
  688. uint32 env_count;
  689. /* in CIDR notation */
  690. const char **addr_pool;
  691. uint32 addr_count;
  692. const char **ns_lookup_pool;
  693. uint32 ns_lookup_count;
  694. char **argv;
  695. uint32 argc;
  696. os_raw_file_handle stdio[3];
  697. } WASIArguments;
  698. #endif
  699. typedef struct StringNode {
  700. struct StringNode *next;
  701. char *str;
  702. } StringNode, *StringList;
  703. typedef struct BrTableCache {
  704. struct BrTableCache *next;
  705. /* Address of br_table opcode */
  706. uint8 *br_table_op_addr;
  707. uint32 br_count;
  708. uint32 br_depths[1];
  709. } BrTableCache;
  710. #if WASM_ENABLE_DEBUG_INTERP != 0
  711. typedef struct WASMFastOPCodeNode {
  712. struct WASMFastOPCodeNode *next;
  713. uint64 offset;
  714. uint8 orig_op;
  715. } WASMFastOPCodeNode;
  716. #endif
  717. #if WASM_ENABLE_LOAD_CUSTOM_SECTION != 0
  718. typedef struct WASMCustomSection {
  719. struct WASMCustomSection *next;
  720. /* Start address of the section name */
  721. char *name_addr;
  722. /* Length of the section name decoded from leb */
  723. uint32 name_len;
  724. /* Start address of the content (name len and name skipped) */
  725. uint8 *content_addr;
  726. uint32 content_len;
  727. } WASMCustomSection;
  728. #endif
  729. #if WASM_ENABLE_FAST_JIT != 0 || WASM_ENABLE_JIT != 0
  730. struct AOTCompData;
  731. struct AOTCompContext;
  732. /* Orc JIT thread arguments */
  733. typedef struct OrcJitThreadArg {
  734. #if WASM_ENABLE_JIT != 0
  735. struct AOTCompContext *comp_ctx;
  736. #endif
  737. struct WASMModule *module;
  738. uint32 group_idx;
  739. } OrcJitThreadArg;
  740. #endif
  741. struct WASMModuleInstance;
  742. struct WASMModule {
  743. /* Module type, for module loaded from WASM bytecode binary,
  744. this field is Wasm_Module_Bytecode;
  745. for module loaded from AOT file, this field is
  746. Wasm_Module_AoT, and this structure should be treated as
  747. AOTModule structure. */
  748. uint32 module_type;
  749. uint32 type_count;
  750. uint32 import_count;
  751. uint32 function_count;
  752. uint32 table_count;
  753. uint32 memory_count;
  754. #if WASM_ENABLE_TAGS != 0
  755. uint32 tag_count;
  756. #endif
  757. uint32 global_count;
  758. uint32 export_count;
  759. uint32 table_seg_count;
  760. /* data seg count read from data segment section */
  761. uint32 data_seg_count;
  762. #if WASM_ENABLE_BULK_MEMORY != 0
  763. /* data count read from datacount section */
  764. uint32 data_seg_count1;
  765. #endif
  766. #if WASM_ENABLE_GC != 0
  767. #if WASM_ENABLE_STRINGREF != 0
  768. uint32 string_literal_count;
  769. uint32 *string_literal_lengths;
  770. const uint8 **string_literal_ptrs;
  771. #endif
  772. #endif
  773. uint32 import_function_count;
  774. uint32 import_table_count;
  775. uint32 import_memory_count;
  776. #if WASM_ENABLE_TAGS != 0
  777. uint32 import_tag_count;
  778. #endif
  779. uint32 import_global_count;
  780. WASMImport *import_functions;
  781. WASMImport *import_tables;
  782. WASMImport *import_memories;
  783. #if WASM_ENABLE_TAGS != 0
  784. WASMImport *import_tags;
  785. #endif
  786. WASMImport *import_globals;
  787. WASMType **types;
  788. WASMImport *imports;
  789. WASMFunction **functions;
  790. WASMTable *tables;
  791. WASMMemory *memories;
  792. #if WASM_ENABLE_TAGS != 0
  793. WASMTag **tags;
  794. #endif
  795. WASMGlobal *globals;
  796. WASMExport *exports;
  797. WASMTableSeg *table_segments;
  798. WASMDataSeg **data_segments;
  799. uint32 start_function;
  800. /* total global variable size */
  801. uint32 global_data_size;
  802. /* the index of auxiliary __data_end global,
  803. -1 means unexported */
  804. uint32 aux_data_end_global_index;
  805. /* auxiliary __data_end exported by wasm app */
  806. uint64 aux_data_end;
  807. /* the index of auxiliary __heap_base global,
  808. -1 means unexported */
  809. uint32 aux_heap_base_global_index;
  810. /* auxiliary __heap_base exported by wasm app */
  811. uint64 aux_heap_base;
  812. /* the index of auxiliary stack top global,
  813. -1 means unexported */
  814. uint32 aux_stack_top_global_index;
  815. /* auxiliary stack bottom resolved */
  816. uint64 aux_stack_bottom;
  817. /* auxiliary stack size resolved */
  818. uint32 aux_stack_size;
  819. /* the index of malloc/free function,
  820. -1 means unexported */
  821. uint32 malloc_function;
  822. uint32 free_function;
  823. /* the index of __retain function,
  824. -1 means unexported */
  825. uint32 retain_function;
  826. /* Whether there is possible memory grow, e.g. memory.grow opcode */
  827. bool possible_memory_grow;
  828. StringList const_str_list;
  829. #if WASM_ENABLE_FAST_INTERP == 0
  830. bh_list br_table_cache_list_head;
  831. bh_list *br_table_cache_list;
  832. #endif
  833. #if WASM_ENABLE_LIBC_WASI != 0
  834. WASIArguments wasi_args;
  835. bool import_wasi_api;
  836. #endif
  837. #if WASM_ENABLE_MULTI_MODULE != 0
  838. /* TODO: add mutex for mutli-thread? */
  839. bh_list import_module_list_head;
  840. bh_list *import_module_list;
  841. #endif
  842. #if WASM_ENABLE_GC != 0
  843. /* Ref types hash set */
  844. HashMap *ref_type_set;
  845. struct WASMRttType **rtt_types;
  846. korp_mutex rtt_type_lock;
  847. #if WASM_ENABLE_STRINGREF != 0
  848. /* special rtts for stringref types
  849. - stringref
  850. - stringview_wtf8
  851. - stringview_wtf16
  852. - stringview_iter
  853. */
  854. struct WASMRttType *stringref_rtts[4];
  855. #endif
  856. #endif
  857. #if WASM_ENABLE_DEBUG_INTERP != 0 || WASM_ENABLE_DEBUG_AOT != 0
  858. bh_list fast_opcode_list;
  859. uint8 *buf_code;
  860. uint64 buf_code_size;
  861. #endif
  862. #if WASM_ENABLE_DEBUG_INTERP != 0 || WASM_ENABLE_FAST_JIT != 0 \
  863. || WASM_ENABLE_DUMP_CALL_STACK != 0 || WASM_ENABLE_JIT != 0
  864. uint8 *load_addr;
  865. uint64 load_size;
  866. #endif
  867. #if WASM_ENABLE_DEBUG_INTERP != 0 \
  868. || (WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT != 0 \
  869. && WASM_ENABLE_LAZY_JIT != 0)
  870. /**
  871. * List of instances referred to this module. When source debugging
  872. * feature is enabled, the debugger may modify the code section of
  873. * the module, so we need to report a warning if user create several
  874. * instances based on the same module.
  875. *
  876. * Also add the instance to the list for Fast JIT to LLVM JIT
  877. * tier-up, since we need to lazily update the LLVM func pointers
  878. * in the instance.
  879. */
  880. struct WASMModuleInstance *instance_list;
  881. korp_mutex instance_list_lock;
  882. #endif
  883. #if WASM_ENABLE_CUSTOM_NAME_SECTION != 0
  884. const uint8 *name_section_buf;
  885. const uint8 *name_section_buf_end;
  886. #endif
  887. #if WASM_ENABLE_LOAD_CUSTOM_SECTION != 0
  888. WASMCustomSection *custom_section_list;
  889. #endif
  890. #if WASM_ENABLE_FAST_JIT != 0
  891. /**
  892. * func pointers of Fast JITed (un-imported) functions
  893. * for non Multi-Tier JIT mode:
  894. * (1) when lazy jit is disabled, each pointer is set to the compiled
  895. * fast jit jitted code
  896. * (2) when lazy jit is enabled, each pointer is firstly inited as
  897. * jit_global->compile_fast_jit_and_then_call, and then set to the
  898. * compiled fast jit jitted code when it is called (the stub will
  899. * compile the jit function and then update itself)
  900. * for Multi-Tier JIT mode:
  901. * each pointer is firstly inited as compile_fast_jit_and_then_call,
  902. * and then set to the compiled fast jit jitted code when it is called,
  903. * and when the llvm jit func ptr of the same function is compiled, it
  904. * will be set to call_to_llvm_jit_from_fast_jit of this function type
  905. * (tier-up from fast-jit to llvm-jit)
  906. */
  907. void **fast_jit_func_ptrs;
  908. /* locks for Fast JIT lazy compilation */
  909. korp_mutex fast_jit_thread_locks[WASM_ORC_JIT_BACKEND_THREAD_NUM];
  910. bool fast_jit_thread_locks_inited[WASM_ORC_JIT_BACKEND_THREAD_NUM];
  911. #endif
  912. #if WASM_ENABLE_JIT != 0
  913. struct AOTCompData *comp_data;
  914. struct AOTCompContext *comp_ctx;
  915. /**
  916. * func pointers of LLVM JITed (un-imported) functions
  917. * for non Multi-Tier JIT mode:
  918. * each pointer is set to the looked up llvm jit func ptr, note that it
  919. * is a stub and will trigger the actual compilation when it is called
  920. * for Multi-Tier JIT mode:
  921. * each pointer is inited as call_to_fast_jit code block, when the llvm
  922. * jit func ptr is actually compiled, it is set to the compiled llvm jit
  923. * func ptr
  924. */
  925. void **func_ptrs;
  926. /* whether the func pointers are compiled */
  927. bool *func_ptrs_compiled;
  928. #endif
  929. #if WASM_ENABLE_FAST_JIT != 0 || WASM_ENABLE_JIT != 0
  930. /* backend compilation threads */
  931. korp_tid orcjit_threads[WASM_ORC_JIT_BACKEND_THREAD_NUM];
  932. /* backend thread arguments */
  933. OrcJitThreadArg orcjit_thread_args[WASM_ORC_JIT_BACKEND_THREAD_NUM];
  934. /* whether to stop the compilation of backend threads */
  935. bool orcjit_stop_compiling;
  936. #endif
  937. #if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT != 0 \
  938. && WASM_ENABLE_LAZY_JIT != 0
  939. /* wait lock/cond for the synchronization of
  940. the llvm jit initialization */
  941. korp_mutex tierup_wait_lock;
  942. korp_cond tierup_wait_cond;
  943. bool tierup_wait_lock_inited;
  944. korp_tid llvm_jit_init_thread;
  945. /* whether the llvm jit is initialized */
  946. bool llvm_jit_inited;
  947. /* Whether to enable llvm jit compilation:
  948. it is set to true only when there is a module instance starts to
  949. run with running mode Mode_LLVM_JIT or Mode_Multi_Tier_JIT,
  950. since no need to enable llvm jit compilation for Mode_Interp and
  951. Mode_Fast_JIT, so as to improve performance for them */
  952. bool enable_llvm_jit_compilation;
  953. /* The count of groups which finish compiling the fast jit
  954. functions in that group */
  955. uint32 fast_jit_ready_groups;
  956. #endif
  957. #if WASM_ENABLE_WAMR_COMPILER != 0
  958. bool is_simd_used;
  959. bool is_ref_types_used;
  960. bool is_bulk_memory_used;
  961. #endif
  962. /* user defined name */
  963. char *name;
  964. };
  965. typedef struct BlockType {
  966. /* Block type may be expressed in one of two forms:
  967. * either by the type of the single return value or
  968. * by a type index of module.
  969. */
  970. union {
  971. struct {
  972. uint8 type;
  973. #if WASM_ENABLE_GC != 0
  974. WASMRefTypeMap ref_type_map;
  975. #endif
  976. } value_type;
  977. WASMFuncType *type;
  978. } u;
  979. bool is_value_type;
  980. } BlockType;
  981. typedef struct WASMBranchBlock {
  982. uint8 *begin_addr;
  983. uint8 *target_addr;
  984. uint32 *frame_sp;
  985. uint32 cell_num;
  986. #if WASM_ENABLE_EXCE_HANDLING != 0
  987. /* in exception handling, label_type needs to be stored to lookup exception
  988. * handlers */
  989. uint8 label_type;
  990. #endif
  991. } WASMBranchBlock;
  992. /**
  993. * Align an unsigned value on a alignment boundary.
  994. *
  995. * @param v the value to be aligned
  996. * @param b the alignment boundary (2, 4, 8, ...)
  997. *
  998. * @return the aligned value
  999. */
  1000. inline static unsigned
  1001. align_uint(unsigned v, unsigned b)
  1002. {
  1003. unsigned m = b - 1;
  1004. return (v + m) & ~m;
  1005. }
  1006. /**
  1007. * Align an 64 bit unsigned value on a alignment boundary.
  1008. *
  1009. * @param v the value to be aligned
  1010. * @param b the alignment boundary (2, 4, 8, ...)
  1011. *
  1012. * @return the aligned value
  1013. */
  1014. inline static uint64
  1015. align_uint64(uint64 v, uint64 b)
  1016. {
  1017. uint64 m = b - 1;
  1018. return (v + m) & ~m;
  1019. }
  1020. /**
  1021. * Check whether a piece of data is out of range
  1022. *
  1023. * @param offset the offset that the data starts
  1024. * @param len the length of the data
  1025. * @param max_size the maximum size of the data range
  1026. *
  1027. * @return true if out of range, false otherwise
  1028. */
  1029. inline static bool
  1030. offset_len_out_of_bounds(uint32 offset, uint32 len, uint32 max_size)
  1031. {
  1032. if (offset + len < offset /* integer overflow */
  1033. || offset + len > max_size)
  1034. return true;
  1035. return false;
  1036. }
  1037. /**
  1038. * Return the hash value of c string.
  1039. */
  1040. inline static uint32
  1041. wasm_string_hash(const char *str)
  1042. {
  1043. unsigned h = (unsigned)strlen(str);
  1044. const uint8 *p = (uint8 *)str;
  1045. const uint8 *end = p + h;
  1046. while (p != end)
  1047. h = ((h << 5) - h) + *p++;
  1048. return h;
  1049. }
  1050. /**
  1051. * Whether two c strings are equal.
  1052. */
  1053. inline static bool
  1054. wasm_string_equal(const char *s1, const char *s2)
  1055. {
  1056. return strcmp(s1, s2) == 0 ? true : false;
  1057. }
  1058. /**
  1059. * Return the byte size of value type with specific pointer size.
  1060. *
  1061. * Note: Please use wasm_value_type_size for interpreter, only aot compiler
  1062. * can use this API directly to calculate type size for different target
  1063. */
  1064. inline static uint32
  1065. wasm_value_type_size_internal(uint8 value_type, uint8 pointer_size)
  1066. {
  1067. if (value_type == VALUE_TYPE_VOID)
  1068. return 0;
  1069. else if (value_type == VALUE_TYPE_I32 || value_type == VALUE_TYPE_F32
  1070. || value_type == VALUE_TYPE_ANY)
  1071. return sizeof(int32);
  1072. else if (value_type == VALUE_TYPE_I64 || value_type == VALUE_TYPE_F64)
  1073. return sizeof(int64);
  1074. #if WASM_ENABLE_SIMD != 0
  1075. else if (value_type == VALUE_TYPE_V128)
  1076. return sizeof(int64) * 2;
  1077. #endif
  1078. #if WASM_ENABLE_GC == 0 && WASM_ENABLE_REF_TYPES != 0
  1079. else if (value_type == VALUE_TYPE_FUNCREF
  1080. || value_type == VALUE_TYPE_EXTERNREF)
  1081. return sizeof(uint32);
  1082. #elif WASM_ENABLE_GC != 0
  1083. else if ((value_type >= (uint8)REF_TYPE_ARRAYREF /* 0x6A */
  1084. && value_type <= (uint8)REF_TYPE_NULLFUNCREF) /* 0x73 */
  1085. || (value_type >= (uint8)REF_TYPE_HT_NULLABLE /* 0x63 */
  1086. && value_type <= (uint8)REF_TYPE_HT_NON_NULLABLE) /* 0x64 */
  1087. #if WASM_ENABLE_STRINGREF != 0
  1088. || (value_type >= (uint8)REF_TYPE_STRINGVIEWWTF8 /* 0x66 */
  1089. && value_type <= (uint8)REF_TYPE_STRINGREF) /* 0x67 */
  1090. || (value_type >= (uint8)REF_TYPE_STRINGVIEWITER /* 0x61 */
  1091. && value_type <= (uint8)REF_TYPE_STRINGVIEWWTF16) /* 0x62 */
  1092. #endif
  1093. )
  1094. return pointer_size;
  1095. else if (value_type == PACKED_TYPE_I8)
  1096. return sizeof(int8);
  1097. else if (value_type == PACKED_TYPE_I16)
  1098. return sizeof(int16);
  1099. #endif
  1100. else {
  1101. bh_assert(0);
  1102. }
  1103. return 0;
  1104. }
  1105. /**
  1106. * Return the cell num of value type with specific pointer size.
  1107. *
  1108. * Note: Please use wasm_value_type_cell_num for interpreter, only aot compiler
  1109. * can use this API directly to calculate type cell num for different target
  1110. */
  1111. inline static uint16
  1112. wasm_value_type_cell_num_internal(uint8 value_type, uint8 pointer_size)
  1113. {
  1114. return wasm_value_type_size_internal(value_type, pointer_size) / 4;
  1115. }
  1116. /**
  1117. * Return the byte size of value type.
  1118. */
  1119. inline static uint32
  1120. wasm_value_type_size(uint8 value_type)
  1121. {
  1122. return wasm_value_type_size_internal(value_type, sizeof(uintptr_t));
  1123. }
  1124. inline static uint16
  1125. wasm_value_type_cell_num(uint8 value_type)
  1126. {
  1127. return wasm_value_type_size(value_type) / 4;
  1128. }
  1129. inline static uint32
  1130. wasm_get_cell_num(const uint8 *types, uint32 type_count)
  1131. {
  1132. uint32 cell_num = 0;
  1133. uint32 i;
  1134. for (i = 0; i < type_count; i++)
  1135. cell_num += wasm_value_type_cell_num(types[i]);
  1136. return cell_num;
  1137. }
  1138. #if WASM_ENABLE_REF_TYPES != 0
  1139. inline static uint16
  1140. wasm_value_type_cell_num_outside(uint8 value_type)
  1141. {
  1142. if (VALUE_TYPE_EXTERNREF == value_type) {
  1143. return sizeof(uintptr_t) / sizeof(uint32);
  1144. }
  1145. else {
  1146. return wasm_value_type_cell_num(value_type);
  1147. }
  1148. }
  1149. #endif
  1150. #if WASM_ENABLE_GC == 0
  1151. inline static bool
  1152. wasm_type_equal(const WASMType *type1, const WASMType *type2,
  1153. const WASMTypePtr *types, uint32 type_count)
  1154. {
  1155. const WASMFuncType *func_type1 = (const WASMFuncType *)type1;
  1156. const WASMFuncType *func_type2 = (const WASMFuncType *)type2;
  1157. if (type1 == type2) {
  1158. return true;
  1159. }
  1160. return (func_type1->param_count == func_type2->param_count
  1161. && func_type1->result_count == func_type2->result_count
  1162. && memcmp(
  1163. func_type1->types, func_type2->types,
  1164. (uint32)(func_type1->param_count + func_type1->result_count))
  1165. == 0)
  1166. ? true
  1167. : false;
  1168. (void)types;
  1169. (void)type_count;
  1170. }
  1171. #else
  1172. /* implemented in gc_type.c */
  1173. bool
  1174. wasm_type_equal(const WASMType *type1, const WASMType *type2,
  1175. const WASMTypePtr *types, uint32 type_count);
  1176. #endif
  1177. inline static uint32
  1178. wasm_get_smallest_type_idx(const WASMTypePtr *types, uint32 type_count,
  1179. uint32 cur_type_idx)
  1180. {
  1181. uint32 i;
  1182. for (i = 0; i < cur_type_idx; i++) {
  1183. if (wasm_type_equal(types[cur_type_idx], types[i], types, type_count))
  1184. return i;
  1185. }
  1186. return cur_type_idx;
  1187. }
  1188. #if WASM_ENABLE_GC == 0
  1189. static inline uint32
  1190. block_type_get_param_types(BlockType *block_type, uint8 **p_param_types)
  1191. #else
  1192. static inline uint32
  1193. block_type_get_param_types(BlockType *block_type, uint8 **p_param_types,
  1194. WASMRefTypeMap **p_param_reftype_maps,
  1195. uint32 *p_param_reftype_map_count)
  1196. #endif
  1197. {
  1198. uint32 param_count = 0;
  1199. if (!block_type->is_value_type) {
  1200. WASMFuncType *func_type = block_type->u.type;
  1201. *p_param_types = func_type->types;
  1202. param_count = func_type->param_count;
  1203. #if WASM_ENABLE_GC != 0
  1204. *p_param_reftype_maps = func_type->ref_type_maps;
  1205. *p_param_reftype_map_count = (uint32)(func_type->result_ref_type_maps
  1206. - func_type->ref_type_maps);
  1207. #endif
  1208. }
  1209. else {
  1210. *p_param_types = NULL;
  1211. param_count = 0;
  1212. #if WASM_ENABLE_GC != 0
  1213. *p_param_reftype_maps = NULL;
  1214. *p_param_reftype_map_count = 0;
  1215. #endif
  1216. }
  1217. return param_count;
  1218. }
  1219. #if WASM_ENABLE_GC == 0
  1220. static inline uint32
  1221. block_type_get_result_types(BlockType *block_type, uint8 **p_result_types)
  1222. #else
  1223. static inline uint32
  1224. block_type_get_result_types(BlockType *block_type, uint8 **p_result_types,
  1225. WASMRefTypeMap **p_result_reftype_maps,
  1226. uint32 *p_result_reftype_map_count)
  1227. #endif
  1228. {
  1229. uint32 result_count = 0;
  1230. uint8 *result_types = NULL;
  1231. #if WASM_ENABLE_GC != 0
  1232. uint8 type;
  1233. uint32 result_reftype_map_count = 0;
  1234. WASMRefTypeMap *result_reftype_maps = NULL;
  1235. #endif
  1236. if (block_type->is_value_type) {
  1237. if (block_type->u.value_type.type != VALUE_TYPE_VOID) {
  1238. result_types = &block_type->u.value_type.type;
  1239. result_count = 1;
  1240. #if WASM_ENABLE_GC != 0
  1241. type = block_type->u.value_type.type;
  1242. if (type == (uint8)REF_TYPE_HT_NULLABLE
  1243. || type == (uint8)REF_TYPE_HT_NON_NULLABLE) {
  1244. result_reftype_maps = &block_type->u.value_type.ref_type_map;
  1245. result_reftype_map_count = 1;
  1246. }
  1247. #endif
  1248. }
  1249. }
  1250. else {
  1251. WASMFuncType *func_type = block_type->u.type;
  1252. result_types = func_type->types + func_type->param_count;
  1253. result_count = func_type->result_count;
  1254. #if WASM_ENABLE_GC != 0
  1255. result_reftype_maps = func_type->result_ref_type_maps;
  1256. result_reftype_map_count = (uint32)(func_type->ref_type_map_count
  1257. - (func_type->result_ref_type_maps
  1258. - func_type->ref_type_maps));
  1259. #endif
  1260. }
  1261. *p_result_types = result_types;
  1262. #if WASM_ENABLE_GC != 0
  1263. *p_result_reftype_maps = result_reftype_maps;
  1264. *p_result_reftype_map_count = result_reftype_map_count;
  1265. #endif
  1266. return result_count;
  1267. }
  1268. static inline uint32
  1269. block_type_get_arity(const BlockType *block_type, uint8 label_type)
  1270. {
  1271. if (label_type == LABEL_TYPE_LOOP) {
  1272. if (block_type->is_value_type)
  1273. return 0;
  1274. else
  1275. return block_type->u.type->param_count;
  1276. }
  1277. else {
  1278. if (block_type->is_value_type) {
  1279. return block_type->u.value_type.type != VALUE_TYPE_VOID ? 1 : 0;
  1280. }
  1281. else
  1282. return block_type->u.type->result_count;
  1283. }
  1284. return 0;
  1285. }
  1286. #ifdef __cplusplus
  1287. } /* end of extern "C" */
  1288. #endif
  1289. #endif /* end of _WASM_H_ */