wasm.h 40 KB

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