wasm.h 39 KB

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