wasm.h 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174
  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 0x7A
  27. #define PACKED_TYPE_I16 0x79
  28. /* Reference Types */
  29. #define REF_TYPE_FUNCREF VALUE_TYPE_FUNCREF
  30. #define REF_TYPE_EXTERNREF VALUE_TYPE_EXTERNREF
  31. #define REF_TYPE_ANYREF 0x6E
  32. #define REF_TYPE_EQREF 0x6D
  33. #define REF_TYPE_HT_NULLABLE 0x6C
  34. #define REF_TYPE_HT_NON_NULLABLE 0x6B
  35. #define REF_TYPE_I31REF 0x6A
  36. #define REF_TYPE_NULLFUNCREF 0x69
  37. #define REF_TYPE_NULLEXTERNREF 0x68
  38. #define REF_TYPE_STRUCTREF 0x67
  39. #define REF_TYPE_ARRAYREF 0x66
  40. #define REF_TYPE_NULLREF 0x65
  41. /* Heap Types */
  42. #define HEAP_TYPE_FUNC (-0x10)
  43. #define HEAP_TYPE_EXTERN (-0x11)
  44. #define HEAP_TYPE_ANY (-0x12)
  45. #define HEAP_TYPE_EQ (-0x13)
  46. #define HEAP_TYPE_I31 (-0x16)
  47. #define HEAP_TYPE_NOFUNC (-0x17)
  48. #define HEAP_TYPE_NOEXTERN (-0x18)
  49. #define HEAP_TYPE_STRUCT (-0x19)
  50. #define HEAP_TYPE_ARRAY (-0x1A)
  51. #define HEAP_TYPE_NONE (-0x1B)
  52. /* Defined Types */
  53. #define DEFINED_TYPE_FUNC 0x60
  54. #define DEFINED_TYPE_STRUCT 0x5F
  55. #define DEFINED_TYPE_ARRAY 0x5E
  56. #define DEFINED_TYPE_SUB 0x50
  57. #define DEFINED_TYPE_REC 0x4F
  58. #define DEFINED_TYPE_SUB_FINAL 0x4E
  59. /* Used by AOT */
  60. #define VALUE_TYPE_I1 0x41
  61. /**
  62. * Used by loader to represent any type of i32/i64/f32/f64/v128
  63. * and ref types, including funcref, externref, anyref, eqref,
  64. * (ref null $ht), (ref $ht), i31ref, structref, arrayref,
  65. * nullfuncref, nullexternref and nullref
  66. */
  67. #define VALUE_TYPE_ANY 0x42
  68. #define DEFAULT_NUM_BYTES_PER_PAGE 65536
  69. #define DEFAULT_MAX_PAGES 65536
  70. #if WASM_ENABLE_GC == 0
  71. typedef uintptr_t table_elem_type_t;
  72. #define NULL_REF (0xFFFFFFFF)
  73. #else
  74. typedef void *table_elem_type_t;
  75. #define NULL_REF (NULL)
  76. #define REF_CELL_NUM ((uint32)sizeof(uintptr_t) / sizeof(uint32))
  77. #endif
  78. #define TABLE_MAX_SIZE (1024)
  79. #define INIT_EXPR_TYPE_I32_CONST 0x41
  80. #define INIT_EXPR_TYPE_I64_CONST 0x42
  81. #define INIT_EXPR_TYPE_F32_CONST 0x43
  82. #define INIT_EXPR_TYPE_F64_CONST 0x44
  83. #define INIT_EXPR_TYPE_V128_CONST 0xFD
  84. #define INIT_EXPR_TYPE_GET_GLOBAL 0x23
  85. #define INIT_EXPR_TYPE_REFNULL_CONST 0xD0
  86. #define INIT_EXPR_TYPE_FUNCREF_CONST 0xD2
  87. #define INIT_EXPR_TYPE_STRUCT_NEW_CANON 0xD3
  88. #define INIT_EXPR_TYPE_STRUCT_NEW_CANON_DEFAULT 0xD4
  89. #define INIT_EXPR_TYPE_ARRAY_NEW_CANON 0xD5
  90. #define INIT_EXPR_TYPE_ARRAY_NEW_CANON_DEFAULT 0xD6
  91. #define INIT_EXPR_TYPE_ARRAY_NEW_CANON_FIXED 0xD7
  92. #define INIT_EXPR_TYPE_I31_NEW 0xD8
  93. #define INIT_EXPR_TYPE_EXTERN_INTERNALIZE 0xD9
  94. #define INIT_EXPR_TYPE_EXTERN_EXTERNALIZE 0xDA
  95. /* TODO: const initial expression of struct/array new */
  96. #define INIT_EXPR_TYPE_ERROR 0xff
  97. #define WASM_MAGIC_NUMBER 0x6d736100
  98. #define WASM_CURRENT_VERSION 1
  99. #define SECTION_TYPE_USER 0
  100. #define SECTION_TYPE_TYPE 1
  101. #define SECTION_TYPE_IMPORT 2
  102. #define SECTION_TYPE_FUNC 3
  103. #define SECTION_TYPE_TABLE 4
  104. #define SECTION_TYPE_MEMORY 5
  105. #define SECTION_TYPE_GLOBAL 6
  106. #define SECTION_TYPE_EXPORT 7
  107. #define SECTION_TYPE_START 8
  108. #define SECTION_TYPE_ELEM 9
  109. #define SECTION_TYPE_CODE 10
  110. #define SECTION_TYPE_DATA 11
  111. #if WASM_ENABLE_BULK_MEMORY != 0
  112. #define SECTION_TYPE_DATACOUNT 12
  113. #endif
  114. #define SUB_SECTION_TYPE_MODULE 0
  115. #define SUB_SECTION_TYPE_FUNC 1
  116. #define SUB_SECTION_TYPE_LOCAL 2
  117. #define IMPORT_KIND_FUNC 0
  118. #define IMPORT_KIND_TABLE 1
  119. #define IMPORT_KIND_MEMORY 2
  120. #define IMPORT_KIND_GLOBAL 3
  121. #define EXPORT_KIND_FUNC 0
  122. #define EXPORT_KIND_TABLE 1
  123. #define EXPORT_KIND_MEMORY 2
  124. #define EXPORT_KIND_GLOBAL 3
  125. #define LABEL_TYPE_BLOCK 0
  126. #define LABEL_TYPE_LOOP 1
  127. #define LABEL_TYPE_IF 2
  128. #define LABEL_TYPE_FUNCTION 3
  129. #define WASM_TYPE_FUNC 0
  130. #define WASM_TYPE_STRUCT 1
  131. #define WASM_TYPE_ARRAY 2
  132. typedef struct WASMModule WASMModule;
  133. typedef struct WASMFunction WASMFunction;
  134. typedef struct WASMGlobal WASMGlobal;
  135. #ifndef WASM_VALUE_DEFINED
  136. #define WASM_VALUE_DEFINED
  137. typedef union V128 {
  138. int8 i8x16[16];
  139. int16 i16x8[8];
  140. int32 i32x8[4];
  141. int64 i64x2[2];
  142. float32 f32x4[4];
  143. float64 f64x2[2];
  144. } V128;
  145. typedef union WASMValue {
  146. int32 i32;
  147. uint32 u32;
  148. uint32 global_index;
  149. uint32 ref_index;
  150. int64 i64;
  151. uint64 u64;
  152. float32 f32;
  153. float64 f64;
  154. V128 v128;
  155. #if WASM_ENABLE_GC != 0
  156. wasm_obj_t gc_obj;
  157. uint32 type_index;
  158. struct {
  159. uint32 type_index;
  160. uint32 N;
  161. } array_new_canon_fixed;
  162. #endif
  163. } WASMValue;
  164. #endif /* end of WASM_VALUE_DEFINED */
  165. typedef struct InitializerExpression {
  166. /* type of INIT_EXPR_TYPE_XXX, which is an instruction of
  167. constant expression */
  168. uint8 init_expr_type;
  169. WASMValue u;
  170. } InitializerExpression;
  171. #if WASM_ENABLE_GC != 0
  172. /**
  173. * Reference type of (ref null ht) or (ref ht),
  174. * and heap type is defined type (type i), i >= 0
  175. */
  176. typedef struct RefHeapType_TypeIdx {
  177. /* ref_type is REF_TYPE_HT_NULLABLE or
  178. REF_TYPE_HT_NON_NULLABLE, (0x6C or 0x6B) */
  179. uint8 ref_type;
  180. /* true if ref_type is REF_TYPE_HT_NULLABLE */
  181. bool nullable;
  182. /* heap type is defined type: type_index >= 0 */
  183. int32 type_idx;
  184. } RefHeapType_TypeIdx;
  185. /**
  186. * Reference type of (ref null ht) or (ref ht),
  187. * and heap type is non-defined type
  188. */
  189. typedef struct RefHeapType_Common {
  190. /* ref_type is REF_TYPE_HT_NULLABLE or
  191. REF_TYPE_HT_NON_NULLABLE (0x6C or 0x6B) */
  192. uint8 ref_type;
  193. /* true if ref_type is REF_TYPE_HT_NULLABLE */
  194. bool nullable;
  195. /* Common heap type (not defined type):
  196. -0x10 (func), -0x11 (extern), -0x12 (any), -0x13 (eq),
  197. -0x16 (i31), -0x17 (nofunc), -0x18 (noextern),
  198. -0x19 (struct), -0x20 (array), -0x21 (none) */
  199. int32 heap_type;
  200. } RefHeapType_Common;
  201. /**
  202. * Reference type
  203. */
  204. typedef union WASMRefType {
  205. uint8 ref_type;
  206. RefHeapType_TypeIdx ref_ht_typeidx;
  207. RefHeapType_Common ref_ht_common;
  208. } WASMRefType;
  209. typedef struct WASMRefTypeMap {
  210. /**
  211. * The type index of a type array, which only stores
  212. * the first byte of the type, e.g. WASMFuncType.types,
  213. * WASMStructType.fields
  214. */
  215. uint16 index;
  216. /* The full type info if the type cannot be described
  217. with one byte */
  218. WASMRefType *ref_type;
  219. } WASMRefTypeMap;
  220. #endif /* end of WASM_ENABLE_GC */
  221. #if WASM_ENABLE_GC == 0
  222. typedef struct WASMFuncType WASMType;
  223. typedef WASMType *WASMTypePtr;
  224. #else
  225. /**
  226. * Common type, store the same fields of
  227. * WASMFuncType, WASMStructType and WASMArrayType
  228. */
  229. typedef struct WASMType {
  230. /**
  231. * type_flag must be WASM_TYPE_FUNC/STRUCT/ARRAY to
  232. * denote that it is a WASMFuncType, WASMStructType or
  233. * WASMArrayType
  234. */
  235. uint16 type_flag;
  236. bool is_sub_final;
  237. /* The inheritance depth */
  238. uint32 inherit_depth;
  239. /* The root type */
  240. struct WASMType *root_type;
  241. /* The parent type */
  242. struct WASMType *parent_type;
  243. uint32 parent_type_idx;
  244. } WASMType, *WASMTypePtr;
  245. #endif /* end of WASM_ENABLE_GC */
  246. /* Function type */
  247. typedef struct WASMFuncType {
  248. #if WASM_ENABLE_GC != 0
  249. WASMType base_type;
  250. #endif
  251. uint16 param_count;
  252. uint16 result_count;
  253. uint16 param_cell_num;
  254. uint16 ret_cell_num;
  255. #if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT != 0 \
  256. && WASM_ENABLE_LAZY_JIT != 0
  257. /* Code block to call llvm jit functions of this
  258. kind of function type from fast jit jitted code */
  259. void *call_to_llvm_jit_from_fast_jit;
  260. #endif
  261. #if WASM_ENABLE_GC != 0
  262. uint16 ref_type_map_count;
  263. WASMRefTypeMap *ref_type_maps;
  264. WASMRefTypeMap *result_ref_type_maps;
  265. /* minimal type index of the type equal to this type,
  266. used in type equal check in call_indirect opcode */
  267. uint32 min_type_idx_normalized;
  268. #else
  269. uint16 ref_count;
  270. #endif
  271. /* types of params and results, only store the first byte
  272. * of the type, if it cannot be described with one byte,
  273. * then the full type info is stored in ref_type_maps */
  274. uint8 types[1];
  275. } WASMFuncType;
  276. #if WASM_ENABLE_GC != 0
  277. typedef struct WASMStructFieldType {
  278. uint16 field_flags;
  279. uint8 field_type;
  280. uint8 field_size;
  281. uint32 field_offset;
  282. } WASMStructFieldType;
  283. typedef struct WASMStructType {
  284. WASMType base_type;
  285. /* total size of this struct object */
  286. uint32 total_size;
  287. uint16 field_count;
  288. uint16 ref_type_map_count;
  289. WASMRefTypeMap *ref_type_maps;
  290. /* Offsets of reference fields that need to be traced during GC.
  291. The first element of the table is the number of such offsets. */
  292. uint16 *reference_table;
  293. /* Field info, note that fields[i]->field_type only stores
  294. * the first byte of the field type, if it cannot be described
  295. * with one byte, then the full field type info is stored in
  296. * ref_type_maps */
  297. WASMStructFieldType fields[1];
  298. } WASMStructType;
  299. typedef struct WASMArrayType {
  300. WASMType base_type;
  301. uint16 elem_flags;
  302. uint8 elem_type;
  303. /* The full elem type info if the elem type cannot be
  304. described with one byte */
  305. WASMRefType *elem_ref_type;
  306. } WASMArrayType;
  307. #endif /* end of WASM_ENABLE_GC != 0 */
  308. typedef struct WASMTable {
  309. uint8 elem_type;
  310. /**
  311. * 0: no max size and not shared
  312. * 1: hax max size
  313. * 2: shared
  314. */
  315. uint8 flags;
  316. bool possible_grow;
  317. uint32 init_size;
  318. /* specified if (flags & 1), else it is 0x10000 */
  319. uint32 max_size;
  320. #if WASM_ENABLE_GC != 0
  321. WASMRefType *elem_ref_type;
  322. #endif
  323. } WASMTable;
  324. typedef struct WASMMemory {
  325. uint32 flags;
  326. uint32 num_bytes_per_page;
  327. uint32 init_page_count;
  328. uint32 max_page_count;
  329. } WASMMemory;
  330. typedef struct WASMTableImport {
  331. char *module_name;
  332. char *field_name;
  333. /* 0: no max size, 1: has max size */
  334. uint8 elem_type;
  335. uint8 flags;
  336. bool possible_grow;
  337. uint32 init_size;
  338. /* specified if (flags & 1), else it is 0x10000 */
  339. uint32 max_size;
  340. #if WASM_ENABLE_GC != 0
  341. WASMRefType *elem_ref_type;
  342. #endif
  343. #if WASM_ENABLE_MULTI_MODULE != 0
  344. WASMModule *import_module;
  345. WASMTable *import_table_linked;
  346. #endif
  347. } WASMTableImport;
  348. typedef struct WASMMemoryImport {
  349. char *module_name;
  350. char *field_name;
  351. uint32 flags;
  352. uint32 num_bytes_per_page;
  353. uint32 init_page_count;
  354. uint32 max_page_count;
  355. #if WASM_ENABLE_MULTI_MODULE != 0
  356. WASMModule *import_module;
  357. WASMMemory *import_memory_linked;
  358. #endif
  359. } WASMMemoryImport;
  360. typedef struct WASMFunctionImport {
  361. char *module_name;
  362. char *field_name;
  363. /* function type */
  364. WASMFuncType *func_type;
  365. /* native function pointer after linked */
  366. void *func_ptr_linked;
  367. /* signature from registered native symbols */
  368. const char *signature;
  369. /* attachment */
  370. void *attachment;
  371. #if WASM_ENABLE_GC != 0
  372. /* the type index of this function's func_type */
  373. uint32 type_idx;
  374. #endif
  375. bool call_conv_raw;
  376. bool call_conv_wasm_c_api;
  377. #if WASM_ENABLE_MULTI_MODULE != 0
  378. WASMModule *import_module;
  379. WASMFunction *import_func_linked;
  380. #endif
  381. } WASMFunctionImport;
  382. typedef struct WASMGlobalImport {
  383. char *module_name;
  384. char *field_name;
  385. uint8 type;
  386. bool is_mutable;
  387. bool is_linked;
  388. /* global data after linked */
  389. WASMValue global_data_linked;
  390. #if WASM_ENABLE_GC != 0
  391. WASMRefType *ref_type;
  392. #endif
  393. #if WASM_ENABLE_MULTI_MODULE != 0
  394. /* imported function pointer after linked */
  395. /* TODO: remove if not needed */
  396. WASMModule *import_module;
  397. WASMGlobal *import_global_linked;
  398. #endif
  399. #if WASM_ENABLE_FAST_JIT != 0
  400. /* The data offset of current global in global data */
  401. uint32 data_offset;
  402. #endif
  403. } WASMGlobalImport;
  404. typedef struct WASMImport {
  405. uint8 kind;
  406. union {
  407. WASMFunctionImport function;
  408. WASMTableImport table;
  409. WASMMemoryImport memory;
  410. WASMGlobalImport global;
  411. struct {
  412. char *module_name;
  413. char *field_name;
  414. } names;
  415. } u;
  416. } WASMImport;
  417. struct WASMFunction {
  418. #if WASM_ENABLE_CUSTOM_NAME_SECTION != 0
  419. char *field_name;
  420. #endif
  421. /* the type of function */
  422. WASMFuncType *func_type;
  423. uint32 local_count;
  424. uint8 *local_types;
  425. #if WASM_ENABLE_GC != 0
  426. uint16 local_ref_type_map_count;
  427. WASMRefTypeMap *local_ref_type_maps;
  428. #endif
  429. /* cell num of parameters */
  430. uint16 param_cell_num;
  431. /* cell num of return type */
  432. uint16 ret_cell_num;
  433. /* cell num of local variables */
  434. uint16 local_cell_num;
  435. /* offset of each local, including function parameters
  436. and local variables */
  437. uint16 *local_offsets;
  438. uint32 max_stack_cell_num;
  439. uint32 max_block_num;
  440. uint32 code_size;
  441. uint8 *code;
  442. #if WASM_ENABLE_FAST_INTERP != 0
  443. uint32 code_compiled_size;
  444. uint8 *code_compiled;
  445. uint8 *consts;
  446. uint32 const_cell_num;
  447. #endif
  448. #if WASM_ENABLE_GC != 0
  449. /* the type index of this function's func_type */
  450. uint32 type_idx;
  451. #endif
  452. #if WASM_ENABLE_FAST_JIT != 0 || WASM_ENABLE_JIT != 0 \
  453. || WASM_ENABLE_WAMR_COMPILER != 0
  454. /* Whether function has opcode memory.grow */
  455. bool has_op_memory_grow;
  456. /* Whether function has opcode call or call_indirect */
  457. bool has_op_func_call;
  458. #endif
  459. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  460. /* Whether function has memory operation opcodes */
  461. bool has_memory_operations;
  462. /* Whether function has opcode call_indirect */
  463. bool has_op_call_indirect;
  464. /* Whether function has opcode set_global_aux_stack */
  465. bool has_op_set_global_aux_stack;
  466. #endif
  467. #if WASM_ENABLE_FAST_JIT != 0
  468. /* The compiled fast jit jitted code block of this function */
  469. void *fast_jit_jitted_code;
  470. #if WASM_ENABLE_JIT != 0 && WASM_ENABLE_LAZY_JIT != 0
  471. /* The compiled llvm jit func ptr of this function */
  472. void *llvm_jit_func_ptr;
  473. /* Code block to call fast jit jitted code of this function
  474. from the llvm jit jitted code */
  475. void *call_to_fast_jit_from_llvm_jit;
  476. #endif
  477. #endif
  478. };
  479. struct WASMGlobal {
  480. uint8 type;
  481. bool is_mutable;
  482. #if WASM_ENABLE_GC != 0
  483. WASMRefType *ref_type;
  484. #endif
  485. InitializerExpression init_expr;
  486. #if WASM_ENABLE_FAST_JIT != 0
  487. /* The data offset of current global in global data */
  488. uint32 data_offset;
  489. #endif
  490. };
  491. typedef struct WASMExport {
  492. char *name;
  493. uint8 kind;
  494. uint32 index;
  495. } WASMExport;
  496. typedef struct WASMTableSeg {
  497. /* 0 to 7 */
  498. uint32 mode;
  499. /* funcref or externref, elemkind will be considered as funcref */
  500. uint32 elem_type;
  501. #if WASM_ENABLE_GC != 0
  502. WASMRefType *elem_ref_type;
  503. #endif
  504. bool is_dropped;
  505. /* optional, only for active */
  506. uint32 table_index;
  507. InitializerExpression base_offset;
  508. uint32 function_count;
  509. uintptr_t *func_indexes;
  510. } WASMTableSeg;
  511. typedef struct WASMDataSeg {
  512. uint32 memory_index;
  513. InitializerExpression base_offset;
  514. uint32 data_length;
  515. #if WASM_ENABLE_BULK_MEMORY != 0
  516. bool is_passive;
  517. #endif
  518. uint8 *data;
  519. } WASMDataSeg;
  520. typedef struct BlockAddr {
  521. const uint8 *start_addr;
  522. uint8 *else_addr;
  523. uint8 *end_addr;
  524. } BlockAddr;
  525. #if WASM_ENABLE_LIBC_WASI != 0
  526. typedef struct WASIArguments {
  527. const char **dir_list;
  528. uint32 dir_count;
  529. const char **map_dir_list;
  530. uint32 map_dir_count;
  531. const char **env;
  532. uint32 env_count;
  533. /* in CIDR noation */
  534. const char **addr_pool;
  535. uint32 addr_count;
  536. const char **ns_lookup_pool;
  537. uint32 ns_lookup_count;
  538. char **argv;
  539. uint32 argc;
  540. int stdio[3];
  541. } WASIArguments;
  542. #endif
  543. typedef struct StringNode {
  544. struct StringNode *next;
  545. char *str;
  546. } StringNode, *StringList;
  547. typedef struct BrTableCache {
  548. struct BrTableCache *next;
  549. /* Address of br_table opcode */
  550. uint8 *br_table_op_addr;
  551. uint32 br_count;
  552. uint32 br_depths[1];
  553. } BrTableCache;
  554. #if WASM_ENABLE_DEBUG_INTERP != 0
  555. typedef struct WASMFastOPCodeNode {
  556. struct WASMFastOPCodeNode *next;
  557. uint64 offset;
  558. uint8 orig_op;
  559. } WASMFastOPCodeNode;
  560. #endif
  561. #if WASM_ENABLE_LOAD_CUSTOM_SECTION != 0
  562. typedef struct WASMCustomSection {
  563. struct WASMCustomSection *next;
  564. /* Start address of the section name */
  565. char *name_addr;
  566. /* Length of the section name decoded from leb */
  567. uint32 name_len;
  568. /* Start address of the content (name len and name skipped) */
  569. uint8 *content_addr;
  570. uint32 content_len;
  571. } WASMCustomSection;
  572. #endif
  573. #if WASM_ENABLE_FAST_JIT != 0 || WASM_ENABLE_JIT != 0
  574. struct AOTCompData;
  575. struct AOTCompContext;
  576. /* Orc JIT thread arguments */
  577. typedef struct OrcJitThreadArg {
  578. #if WASM_ENABLE_JIT != 0
  579. struct AOTCompContext *comp_ctx;
  580. #endif
  581. struct WASMModule *module;
  582. uint32 group_idx;
  583. } OrcJitThreadArg;
  584. #endif
  585. struct WASMModuleInstance;
  586. struct WASMModule {
  587. /* Module type, for module loaded from WASM bytecode binary,
  588. this field is Wasm_Module_Bytecode;
  589. for module loaded from AOT file, this field is
  590. Wasm_Module_AoT, and this structure should be treated as
  591. AOTModule structure. */
  592. uint32 module_type;
  593. uint32 type_count;
  594. uint32 import_count;
  595. uint32 function_count;
  596. uint32 table_count;
  597. uint32 memory_count;
  598. uint32 global_count;
  599. uint32 export_count;
  600. uint32 table_seg_count;
  601. /* data seg count read from data segment section */
  602. uint32 data_seg_count;
  603. #if WASM_ENABLE_BULK_MEMORY != 0
  604. /* data count read from datacount section */
  605. uint32 data_seg_count1;
  606. #endif
  607. uint32 import_function_count;
  608. uint32 import_table_count;
  609. uint32 import_memory_count;
  610. uint32 import_global_count;
  611. WASMImport *import_functions;
  612. WASMImport *import_tables;
  613. WASMImport *import_memories;
  614. WASMImport *import_globals;
  615. WASMType **types;
  616. WASMImport *imports;
  617. WASMFunction **functions;
  618. WASMTable *tables;
  619. WASMMemory *memories;
  620. WASMGlobal *globals;
  621. WASMExport *exports;
  622. WASMTableSeg *table_segments;
  623. WASMDataSeg **data_segments;
  624. uint32 start_function;
  625. /* total global variable size */
  626. uint32 global_data_size;
  627. /* the index of auxiliary __data_end global,
  628. -1 means unexported */
  629. uint32 aux_data_end_global_index;
  630. /* auxiliary __data_end exported by wasm app */
  631. uint32 aux_data_end;
  632. /* the index of auxiliary __heap_base global,
  633. -1 means unexported */
  634. uint32 aux_heap_base_global_index;
  635. /* auxiliary __heap_base exported by wasm app */
  636. uint32 aux_heap_base;
  637. /* the index of auxiliary stack top global,
  638. -1 means unexported */
  639. uint32 aux_stack_top_global_index;
  640. /* auxiliary stack bottom resolved */
  641. uint32 aux_stack_bottom;
  642. /* auxiliary stack size resolved */
  643. uint32 aux_stack_size;
  644. /* the index of malloc/free function,
  645. -1 means unexported */
  646. uint32 malloc_function;
  647. uint32 free_function;
  648. /* the index of __retain function,
  649. -1 means unexported */
  650. uint32 retain_function;
  651. /* Whether there is possible memory grow, e.g. memory.grow opcode */
  652. bool possible_memory_grow;
  653. StringList const_str_list;
  654. #if WASM_ENABLE_FAST_INTERP == 0
  655. bh_list br_table_cache_list_head;
  656. bh_list *br_table_cache_list;
  657. #endif
  658. #if WASM_ENABLE_LIBC_WASI != 0
  659. WASIArguments wasi_args;
  660. bool import_wasi_api;
  661. #endif
  662. #if WASM_ENABLE_MULTI_MODULE != 0
  663. /* TODO: add mutex for mutli-thread? */
  664. bh_list import_module_list_head;
  665. bh_list *import_module_list;
  666. #endif
  667. #if WASM_ENABLE_GC != 0
  668. /* Ref types hash set */
  669. HashMap *ref_type_set;
  670. struct WASMRttType **rtt_types;
  671. korp_mutex rtt_type_lock;
  672. #endif
  673. #if WASM_ENABLE_DEBUG_INTERP != 0 || WASM_ENABLE_DEBUG_AOT != 0
  674. bh_list fast_opcode_list;
  675. uint8 *buf_code;
  676. uint64 buf_code_size;
  677. #endif
  678. #if WASM_ENABLE_DEBUG_INTERP != 0 || WASM_ENABLE_DEBUG_AOT != 0 \
  679. || WASM_ENABLE_FAST_JIT != 0
  680. uint8 *load_addr;
  681. uint64 load_size;
  682. #endif
  683. #if WASM_ENABLE_DEBUG_INTERP != 0 \
  684. || (WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT != 0 \
  685. && WASM_ENABLE_LAZY_JIT != 0)
  686. /**
  687. * List of instances referred to this module. When source debugging
  688. * feature is enabled, the debugger may modify the code section of
  689. * the module, so we need to report a warning if user create several
  690. * instances based on the same module.
  691. *
  692. * Also add the instance to the list for Fast JIT to LLVM JIT
  693. * tier-up, since we need to lazily update the LLVM func pointers
  694. * in the instance.
  695. */
  696. struct WASMModuleInstance *instance_list;
  697. korp_mutex instance_list_lock;
  698. #endif
  699. #if WASM_ENABLE_CUSTOM_NAME_SECTION != 0
  700. const uint8 *name_section_buf;
  701. const uint8 *name_section_buf_end;
  702. #endif
  703. #if WASM_ENABLE_LOAD_CUSTOM_SECTION != 0
  704. WASMCustomSection *custom_section_list;
  705. #endif
  706. #if WASM_ENABLE_FAST_JIT != 0
  707. /**
  708. * func pointers of Fast JITed (un-imported) functions
  709. * for non Multi-Tier JIT mode:
  710. * (1) when lazy jit is disabled, each pointer is set to the compiled
  711. * fast jit jitted code
  712. * (2) when lazy jit is enabled, each pointer is firstly inited as
  713. * jit_global->compile_fast_jit_and_then_call, and then set to the
  714. * compiled fast jit jitted code when it is called (the stub will
  715. * compile the jit function and then update itself)
  716. * for Multi-Tier JIT mode:
  717. * each pointer is firstly inited as compile_fast_jit_and_then_call,
  718. * and then set to the compiled fast jit jitted code when it is called,
  719. * and when the llvm jit func ptr of the same function is compiled, it
  720. * will be set to call_to_llvm_jit_from_fast_jit of this function type
  721. * (tier-up from fast-jit to llvm-jit)
  722. */
  723. void **fast_jit_func_ptrs;
  724. /* locks for Fast JIT lazy compilation */
  725. korp_mutex fast_jit_thread_locks[WASM_ORC_JIT_BACKEND_THREAD_NUM];
  726. bool fast_jit_thread_locks_inited[WASM_ORC_JIT_BACKEND_THREAD_NUM];
  727. #endif
  728. #if WASM_ENABLE_JIT != 0
  729. struct AOTCompData *comp_data;
  730. struct AOTCompContext *comp_ctx;
  731. /**
  732. * func pointers of LLVM JITed (un-imported) functions
  733. * for non Multi-Tier JIT mode:
  734. * each pointer is set to the lookuped llvm jit func ptr, note that it
  735. * is a stub and will trigger the actual compilation when it is called
  736. * for Multi-Tier JIT mode:
  737. * each pointer is inited as call_to_fast_jit code block, when the llvm
  738. * jit func ptr is actually compiled, it is set to the compiled llvm jit
  739. * func ptr
  740. */
  741. void **func_ptrs;
  742. /* whether the func pointers are compiled */
  743. bool *func_ptrs_compiled;
  744. #endif
  745. #if WASM_ENABLE_FAST_JIT != 0 || WASM_ENABLE_JIT != 0
  746. /* backend compilation threads */
  747. korp_tid orcjit_threads[WASM_ORC_JIT_BACKEND_THREAD_NUM];
  748. /* backend thread arguments */
  749. OrcJitThreadArg orcjit_thread_args[WASM_ORC_JIT_BACKEND_THREAD_NUM];
  750. /* whether to stop the compilation of backend threads */
  751. bool orcjit_stop_compiling;
  752. #endif
  753. #if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT != 0 \
  754. && WASM_ENABLE_LAZY_JIT != 0
  755. /* wait lock/cond for the synchronization of
  756. the llvm jit initialization */
  757. korp_mutex tierup_wait_lock;
  758. korp_cond tierup_wait_cond;
  759. bool tierup_wait_lock_inited;
  760. korp_tid llvm_jit_init_thread;
  761. /* whether the llvm jit is initialized */
  762. bool llvm_jit_inited;
  763. /* Whether to enable llvm jit compilation:
  764. it is set to true only when there is a module instance starts to
  765. run with running mode Mode_LLVM_JIT or Mode_Multi_Tier_JIT,
  766. since no need to enable llvm jit compilation for Mode_Interp and
  767. Mode_Fast_JIT, so as to improve performance for them */
  768. bool enable_llvm_jit_compilation;
  769. /* The count of groups which finish compiling the fast jit
  770. functions in that group */
  771. uint32 fast_jit_ready_groups;
  772. #endif
  773. };
  774. typedef struct BlockType {
  775. /* Block type may be expressed in one of two forms:
  776. * either by the type of the single return value or
  777. * by a type index of module.
  778. */
  779. union {
  780. struct {
  781. uint8 type;
  782. #if WASM_ENABLE_GC != 0
  783. WASMRefTypeMap ref_type_map;
  784. #endif
  785. } value_type;
  786. WASMFuncType *type;
  787. } u;
  788. bool is_value_type;
  789. } BlockType;
  790. typedef struct WASMBranchBlock {
  791. uint8 *begin_addr;
  792. uint8 *target_addr;
  793. uint32 *frame_sp;
  794. uint32 cell_num;
  795. } WASMBranchBlock;
  796. /**
  797. * Align an unsigned value on a alignment boundary.
  798. *
  799. * @param v the value to be aligned
  800. * @param b the alignment boundary (2, 4, 8, ...)
  801. *
  802. * @return the aligned value
  803. */
  804. inline static unsigned
  805. align_uint(unsigned v, unsigned b)
  806. {
  807. unsigned m = b - 1;
  808. return (v + m) & ~m;
  809. }
  810. /**
  811. * Check whether a piece of data is out of range
  812. *
  813. * @param offset the offset that the data starts
  814. * @param len the length of the data
  815. * @param max_size the maximum size of the data range
  816. *
  817. * @return true if out of range, false otherwise
  818. */
  819. inline static bool
  820. offset_len_out_of_bounds(uint32 offset, uint32 len, uint32 max_size)
  821. {
  822. if (offset + len < offset /* integer overflow */
  823. || offset + len > max_size)
  824. return true;
  825. return false;
  826. }
  827. /**
  828. * Return the hash value of c string.
  829. */
  830. inline static uint32
  831. wasm_string_hash(const char *str)
  832. {
  833. unsigned h = (unsigned)strlen(str);
  834. const uint8 *p = (uint8 *)str;
  835. const uint8 *end = p + h;
  836. while (p != end)
  837. h = ((h << 5) - h) + *p++;
  838. return h;
  839. }
  840. /**
  841. * Whether two c strings are equal.
  842. */
  843. inline static bool
  844. wasm_string_equal(const char *s1, const char *s2)
  845. {
  846. return strcmp(s1, s2) == 0 ? true : false;
  847. }
  848. /**
  849. * Return the byte size of value type.
  850. */
  851. inline static uint32
  852. wasm_value_type_size_ex(uint8 value_type, bool gc_enabled)
  853. {
  854. if (value_type == VALUE_TYPE_VOID)
  855. return 0;
  856. else if (value_type == VALUE_TYPE_I32 || value_type == VALUE_TYPE_F32
  857. || value_type == VALUE_TYPE_ANY)
  858. return sizeof(int32);
  859. else if (value_type == VALUE_TYPE_I64 || value_type == VALUE_TYPE_F64)
  860. return sizeof(int64);
  861. #if WASM_ENABLE_SIMD != 0
  862. else if (value_type == VALUE_TYPE_V128)
  863. return sizeof(int64) * 2;
  864. #endif
  865. else if (value_type >= (uint8)REF_TYPE_NULLREF /* 0x65 */
  866. && value_type <= (uint8)REF_TYPE_FUNCREF /* 0x70 */ && gc_enabled)
  867. return sizeof(uintptr_t);
  868. else if (value_type == VALUE_TYPE_FUNCREF
  869. || value_type == VALUE_TYPE_EXTERNREF)
  870. return sizeof(uint32);
  871. else {
  872. bh_assert(0);
  873. }
  874. return 0;
  875. }
  876. /**
  877. * Return the byte size of value type.
  878. */
  879. inline static uint32
  880. wasm_value_type_size(uint8 value_type)
  881. {
  882. return wasm_value_type_size_ex(value_type,
  883. #if WASM_ENABLE_GC != 0
  884. true
  885. #else
  886. false
  887. #endif
  888. );
  889. }
  890. inline static uint16
  891. wasm_value_type_cell_num(uint8 value_type)
  892. {
  893. return wasm_value_type_size(value_type) / 4;
  894. }
  895. inline static uint32
  896. wasm_get_cell_num(const uint8 *types, uint32 type_count)
  897. {
  898. uint32 cell_num = 0;
  899. uint32 i;
  900. for (i = 0; i < type_count; i++)
  901. cell_num += wasm_value_type_cell_num(types[i]);
  902. return cell_num;
  903. }
  904. #if WASM_ENABLE_REF_TYPES != 0
  905. inline static uint16
  906. wasm_value_type_cell_num_outside(uint8 value_type)
  907. {
  908. if (VALUE_TYPE_EXTERNREF == value_type) {
  909. return sizeof(uintptr_t) / sizeof(uint32);
  910. }
  911. else {
  912. return wasm_value_type_cell_num(value_type);
  913. }
  914. }
  915. #endif
  916. #if WASM_ENABLE_GC == 0
  917. inline static bool
  918. wasm_type_equal(const WASMType *type1, const WASMType *type2,
  919. const WASMTypePtr *types, uint32 type_count)
  920. {
  921. const WASMFuncType *func_type1 = (const WASMFuncType *)type1;
  922. const WASMFuncType *func_type2 = (const WASMFuncType *)type2;
  923. if (type1 == type2) {
  924. return true;
  925. }
  926. return (func_type1->param_count == func_type2->param_count
  927. && func_type1->result_count == func_type2->result_count
  928. && memcmp(
  929. func_type1->types, func_type2->types,
  930. (uint32)(func_type1->param_count + func_type1->result_count))
  931. == 0)
  932. ? true
  933. : false;
  934. (void)types;
  935. (void)type_count;
  936. }
  937. #else
  938. /* implemented in gc_type.c */
  939. bool
  940. wasm_type_equal(const WASMType *type1, const WASMType *type2,
  941. const WASMTypePtr *types, uint32 type_count);
  942. #endif
  943. inline static uint32
  944. wasm_get_smallest_type_idx(const WASMTypePtr *types, uint32 type_count,
  945. uint32 cur_type_idx)
  946. {
  947. uint32 i;
  948. for (i = 0; i < cur_type_idx; i++) {
  949. if (wasm_type_equal(types[cur_type_idx], types[i], types, type_count))
  950. return i;
  951. }
  952. return cur_type_idx;
  953. }
  954. #if WASM_ENABLE_GC == 0
  955. static inline uint32
  956. block_type_get_param_types(BlockType *block_type, uint8 **p_param_types)
  957. #else
  958. static inline uint32
  959. block_type_get_param_types(BlockType *block_type, uint8 **p_param_types,
  960. WASMRefTypeMap **p_param_reftype_maps,
  961. uint32 *p_param_reftype_map_count)
  962. #endif
  963. {
  964. uint32 param_count = 0;
  965. if (!block_type->is_value_type) {
  966. WASMFuncType *func_type = block_type->u.type;
  967. *p_param_types = func_type->types;
  968. param_count = func_type->param_count;
  969. #if WASM_ENABLE_GC != 0
  970. *p_param_reftype_maps = func_type->ref_type_maps;
  971. *p_param_reftype_map_count =
  972. func_type->result_ref_type_maps - func_type->ref_type_maps;
  973. #endif
  974. }
  975. else {
  976. *p_param_types = NULL;
  977. param_count = 0;
  978. #if WASM_ENABLE_GC != 0
  979. *p_param_reftype_maps = NULL;
  980. *p_param_reftype_map_count = 0;
  981. #endif
  982. }
  983. return param_count;
  984. }
  985. #if WASM_ENABLE_GC == 0
  986. static inline uint32
  987. block_type_get_result_types(BlockType *block_type, uint8 **p_result_types)
  988. #else
  989. static inline uint32
  990. block_type_get_result_types(BlockType *block_type, uint8 **p_result_types,
  991. WASMRefTypeMap **p_result_reftype_maps,
  992. uint32 *p_result_reftype_map_count)
  993. #endif
  994. {
  995. uint32 result_count = 0;
  996. uint8 *result_types = NULL;
  997. #if WASM_ENABLE_GC != 0
  998. uint8 type;
  999. uint32 result_reftype_map_count = 0;
  1000. WASMRefTypeMap *result_reftype_maps = NULL;
  1001. #endif
  1002. if (block_type->is_value_type) {
  1003. if (block_type->u.value_type.type != VALUE_TYPE_VOID) {
  1004. result_types = &block_type->u.value_type.type;
  1005. result_count = 1;
  1006. #if WASM_ENABLE_GC != 0
  1007. type = block_type->u.value_type.type;
  1008. if (type == (uint8)REF_TYPE_HT_NULLABLE
  1009. || type == (uint8)REF_TYPE_HT_NON_NULLABLE) {
  1010. result_reftype_maps = &block_type->u.value_type.ref_type_map;
  1011. result_reftype_map_count = 1;
  1012. }
  1013. #endif
  1014. }
  1015. }
  1016. else {
  1017. WASMFuncType *func_type = block_type->u.type;
  1018. result_types = func_type->types + func_type->param_count;
  1019. result_count = func_type->result_count;
  1020. #if WASM_ENABLE_GC != 0
  1021. result_reftype_maps = func_type->result_ref_type_maps;
  1022. result_reftype_map_count = (uint32)(func_type->ref_type_map_count
  1023. - (func_type->result_ref_type_maps
  1024. - func_type->ref_type_maps));
  1025. #endif
  1026. }
  1027. *p_result_types = result_types;
  1028. #if WASM_ENABLE_GC != 0
  1029. *p_result_reftype_maps = result_reftype_maps;
  1030. *p_result_reftype_map_count = result_reftype_map_count;
  1031. #endif
  1032. return result_count;
  1033. }
  1034. static inline uint32
  1035. block_type_get_arity(const BlockType *block_type, uint8 label_type)
  1036. {
  1037. if (label_type == LABEL_TYPE_LOOP) {
  1038. if (block_type->is_value_type)
  1039. return 0;
  1040. else
  1041. return block_type->u.type->param_count;
  1042. }
  1043. else {
  1044. if (block_type->is_value_type) {
  1045. return block_type->u.value_type.type != VALUE_TYPE_VOID ? 1 : 0;
  1046. }
  1047. else
  1048. return block_type->u.type->result_count;
  1049. }
  1050. return 0;
  1051. }
  1052. #ifdef __cplusplus
  1053. } /* end of extern "C" */
  1054. #endif
  1055. #endif /* end of _WASM_H_ */