wasm.h 42 KB

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