wasm.h 41 KB

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