wasm.h 40 KB

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