wasm.h 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189
  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 uint32 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. uint32 data[1];
  245. } WASMType, *WASMTypePtr;
  246. #endif /* end of WASM_ENABLE_GC */
  247. /* Function type */
  248. typedef struct WASMFuncType {
  249. #if WASM_ENABLE_GC != 0
  250. /**
  251. * type_flag must be WASM_TYPE_FUNC for WASMFuncType,
  252. * otherwise this structure must be treated as WASMStructType
  253. * or WASMArrayType
  254. */
  255. uint16 type_flag;
  256. bool is_sub_final;
  257. /* The inheritance depth */
  258. uint32 inherit_depth;
  259. /* The root type */
  260. WASMType *root_type;
  261. /* The parent type */
  262. WASMType *parent_type;
  263. uint32 parent_type_idx;
  264. #endif
  265. uint16 param_count;
  266. uint16 result_count;
  267. uint16 param_cell_num;
  268. uint16 ret_cell_num;
  269. #if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT != 0 \
  270. && WASM_ENABLE_LAZY_JIT != 0
  271. /* Code block to call llvm jit functions of this
  272. kind of function type from fast jit jitted code */
  273. void *call_to_llvm_jit_from_fast_jit;
  274. #endif
  275. #if WASM_ENABLE_GC != 0
  276. uint16 ref_type_map_count;
  277. WASMRefTypeMap *ref_type_maps;
  278. WASMRefTypeMap *result_ref_type_maps;
  279. /* minimal type index of the type equal to this type,
  280. used in type equal check in call_indirect opcode */
  281. uint32 min_type_idx_normalized;
  282. #else
  283. uint16 ref_count;
  284. #endif
  285. /* types of params and results, only store the first byte
  286. * of the type, if it cannot be described with one byte,
  287. * then the full type info is stored in ref_type_maps */
  288. uint8 types[1];
  289. } WASMFuncType;
  290. #if WASM_ENABLE_GC != 0
  291. typedef struct WASMStructFieldType {
  292. uint16 field_flags;
  293. uint8 field_type;
  294. uint8 field_size;
  295. uint32 field_offset;
  296. } WASMStructFieldType;
  297. typedef struct WASMStructType {
  298. /**
  299. * type_flag must be WASM_TYPE_STRUCT for WASMStructType,
  300. * otherwise this structure must be treated as WASMFuncType
  301. * or WASMArrayType
  302. */
  303. uint16 type_flag;
  304. bool is_sub_final;
  305. /* The inheritance depth */
  306. uint32 inherit_depth;
  307. /* The root type */
  308. WASMType *root_type;
  309. /* The parent type */
  310. WASMType *parent_type;
  311. uint32 parent_type_idx;
  312. /* total size of this struct object */
  313. uint32 total_size;
  314. uint16 field_count;
  315. uint16 ref_type_map_count;
  316. WASMRefTypeMap *ref_type_maps;
  317. /* Offsets of reference fields that need to be traced during GC.
  318. The first element of the table is the number of such offsets. */
  319. uint16 *reference_table;
  320. /* Field info, note that fields[i]->field_type only stores
  321. * the first byte of the field type, if it cannot be described
  322. * with one byte, then the full field type info is stored in
  323. * ref_type_maps */
  324. WASMStructFieldType fields[1];
  325. } WASMStructType;
  326. typedef struct WASMArrayType {
  327. /**
  328. * type_flag must be WASM_TYPE_ARRAY for WASMArrayType
  329. * or this structure must be treated as WASMFuncType or
  330. * WASMStructType
  331. */
  332. uint16 type_flag;
  333. bool is_sub_final;
  334. /* The inheritance depth */
  335. uint32 inherit_depth;
  336. /* The root type */
  337. WASMType *root_type;
  338. /* The parent type */
  339. WASMType *parent_type;
  340. uint32 parent_type_idx;
  341. uint16 elem_flags;
  342. uint8 elem_type;
  343. /* The full elem type info if the elem type cannot be
  344. described with one byte */
  345. WASMRefType *elem_ref_type;
  346. } WASMArrayType;
  347. #endif /* end of WASM_ENABLE_GC != 0 */
  348. typedef struct WASMTable {
  349. uint8 elem_type;
  350. /**
  351. * 0: no max size and not shared
  352. * 1: hax max size
  353. * 2: shared
  354. */
  355. uint8 flags;
  356. bool possible_grow;
  357. uint32 init_size;
  358. /* specified if (flags & 1), else it is 0x10000 */
  359. uint32 max_size;
  360. #if WASM_ENABLE_GC != 0
  361. WASMRefType *elem_ref_type;
  362. #endif
  363. } WASMTable;
  364. typedef struct WASMMemory {
  365. uint32 flags;
  366. uint32 num_bytes_per_page;
  367. uint32 init_page_count;
  368. uint32 max_page_count;
  369. } WASMMemory;
  370. typedef struct WASMTableImport {
  371. char *module_name;
  372. char *field_name;
  373. /* 0: no max size, 1: has max size */
  374. uint8 elem_type;
  375. uint8 flags;
  376. bool possible_grow;
  377. uint32 init_size;
  378. /* specified if (flags & 1), else it is 0x10000 */
  379. uint32 max_size;
  380. #if WASM_ENABLE_GC != 0
  381. WASMRefType *elem_ref_type;
  382. #endif
  383. #if WASM_ENABLE_MULTI_MODULE != 0
  384. WASMModule *import_module;
  385. WASMTable *import_table_linked;
  386. #endif
  387. } WASMTableImport;
  388. typedef struct WASMMemoryImport {
  389. char *module_name;
  390. char *field_name;
  391. uint32 flags;
  392. uint32 num_bytes_per_page;
  393. uint32 init_page_count;
  394. uint32 max_page_count;
  395. #if WASM_ENABLE_MULTI_MODULE != 0
  396. WASMModule *import_module;
  397. WASMMemory *import_memory_linked;
  398. #endif
  399. } WASMMemoryImport;
  400. typedef struct WASMFunctionImport {
  401. char *module_name;
  402. char *field_name;
  403. /* function type */
  404. WASMFuncType *func_type;
  405. /* native function pointer after linked */
  406. void *func_ptr_linked;
  407. /* signature from registered native symbols */
  408. const char *signature;
  409. /* attachment */
  410. void *attachment;
  411. #if WASM_ENABLE_GC != 0
  412. /* the type index of this function's func_type */
  413. uint32 type_idx;
  414. #endif
  415. bool call_conv_raw;
  416. bool call_conv_wasm_c_api;
  417. #if WASM_ENABLE_MULTI_MODULE != 0
  418. WASMModule *import_module;
  419. WASMFunction *import_func_linked;
  420. #endif
  421. } WASMFunctionImport;
  422. typedef struct WASMGlobalImport {
  423. char *module_name;
  424. char *field_name;
  425. uint8 type;
  426. bool is_mutable;
  427. bool is_linked;
  428. /* global data after linked */
  429. WASMValue global_data_linked;
  430. #if WASM_ENABLE_GC != 0
  431. WASMRefType *ref_type;
  432. #endif
  433. #if WASM_ENABLE_MULTI_MODULE != 0
  434. /* imported function pointer after linked */
  435. /* TODO: remove if not needed */
  436. WASMModule *import_module;
  437. WASMGlobal *import_global_linked;
  438. #endif
  439. #if WASM_ENABLE_FAST_JIT != 0
  440. /* The data offset of current global in global data */
  441. uint32 data_offset;
  442. #endif
  443. } WASMGlobalImport;
  444. typedef struct WASMImport {
  445. uint8 kind;
  446. union {
  447. WASMFunctionImport function;
  448. WASMTableImport table;
  449. WASMMemoryImport memory;
  450. WASMGlobalImport global;
  451. struct {
  452. char *module_name;
  453. char *field_name;
  454. } names;
  455. } u;
  456. } WASMImport;
  457. struct WASMFunction {
  458. #if WASM_ENABLE_CUSTOM_NAME_SECTION != 0
  459. char *field_name;
  460. #endif
  461. /* the type of function */
  462. WASMFuncType *func_type;
  463. uint32 local_count;
  464. uint8 *local_types;
  465. #if WASM_ENABLE_GC != 0
  466. uint16 local_ref_type_map_count;
  467. WASMRefTypeMap *local_ref_type_maps;
  468. #endif
  469. /* cell num of parameters */
  470. uint16 param_cell_num;
  471. /* cell num of return type */
  472. uint16 ret_cell_num;
  473. /* cell num of local variables */
  474. uint16 local_cell_num;
  475. /* offset of each local, including function parameters
  476. and local variables */
  477. uint16 *local_offsets;
  478. uint32 max_stack_cell_num;
  479. uint32 max_block_num;
  480. uint32 code_size;
  481. uint8 *code;
  482. #if WASM_ENABLE_FAST_INTERP != 0
  483. uint32 code_compiled_size;
  484. uint8 *code_compiled;
  485. uint8 *consts;
  486. uint32 const_cell_num;
  487. #endif
  488. #if WASM_ENABLE_GC != 0
  489. /* the type index of this function's func_type */
  490. uint32 type_idx;
  491. #endif
  492. #if WASM_ENABLE_FAST_JIT != 0 || WASM_ENABLE_JIT != 0 \
  493. || WASM_ENABLE_WAMR_COMPILER != 0
  494. /* Whether function has opcode memory.grow */
  495. bool has_op_memory_grow;
  496. /* Whether function has opcode call or call_indirect */
  497. bool has_op_func_call;
  498. #endif
  499. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  500. /* Whether function has memory operation opcodes */
  501. bool has_memory_operations;
  502. /* Whether function has opcode call_indirect */
  503. bool has_op_call_indirect;
  504. /* Whether function has opcode set_global_aux_stack */
  505. bool has_op_set_global_aux_stack;
  506. #endif
  507. #if WASM_ENABLE_FAST_JIT != 0
  508. /* The compiled fast jit jitted code block of this function */
  509. void *fast_jit_jitted_code;
  510. #if WASM_ENABLE_JIT != 0 && WASM_ENABLE_LAZY_JIT != 0
  511. /* The compiled llvm jit func ptr of this function */
  512. void *llvm_jit_func_ptr;
  513. /* Code block to call fast jit jitted code of this function
  514. from the llvm jit jitted code */
  515. void *call_to_fast_jit_from_llvm_jit;
  516. #endif
  517. #endif
  518. };
  519. struct WASMGlobal {
  520. uint8 type;
  521. bool is_mutable;
  522. #if WASM_ENABLE_GC != 0
  523. WASMRefType *ref_type;
  524. #endif
  525. InitializerExpression init_expr;
  526. #if WASM_ENABLE_FAST_JIT != 0
  527. /* The data offset of current global in global data */
  528. uint32 data_offset;
  529. #endif
  530. };
  531. typedef struct WASMExport {
  532. char *name;
  533. uint8 kind;
  534. uint32 index;
  535. } WASMExport;
  536. typedef struct WASMTableSeg {
  537. /* 0 to 7 */
  538. uint32 mode;
  539. /* funcref or externref, elemkind will be considered as funcref */
  540. uint32 elem_type;
  541. #if WASM_ENABLE_GC != 0
  542. WASMRefType *elem_ref_type;
  543. #endif
  544. bool is_dropped;
  545. /* optional, only for active */
  546. uint32 table_index;
  547. InitializerExpression base_offset;
  548. uint32 function_count;
  549. uint32 *func_indexes;
  550. } WASMTableSeg;
  551. typedef struct WASMDataSeg {
  552. uint32 memory_index;
  553. InitializerExpression base_offset;
  554. uint32 data_length;
  555. #if WASM_ENABLE_BULK_MEMORY != 0
  556. bool is_passive;
  557. #endif
  558. uint8 *data;
  559. } WASMDataSeg;
  560. typedef struct BlockAddr {
  561. const uint8 *start_addr;
  562. uint8 *else_addr;
  563. uint8 *end_addr;
  564. } BlockAddr;
  565. #if WASM_ENABLE_LIBC_WASI != 0
  566. typedef struct WASIArguments {
  567. const char **dir_list;
  568. uint32 dir_count;
  569. const char **map_dir_list;
  570. uint32 map_dir_count;
  571. const char **env;
  572. uint32 env_count;
  573. /* in CIDR noation */
  574. const char **addr_pool;
  575. uint32 addr_count;
  576. const char **ns_lookup_pool;
  577. uint32 ns_lookup_count;
  578. char **argv;
  579. uint32 argc;
  580. int stdio[3];
  581. } WASIArguments;
  582. #endif
  583. typedef struct StringNode {
  584. struct StringNode *next;
  585. char *str;
  586. } StringNode, *StringList;
  587. typedef struct BrTableCache {
  588. struct BrTableCache *next;
  589. /* Address of br_table opcode */
  590. uint8 *br_table_op_addr;
  591. uint32 br_count;
  592. uint32 br_depths[1];
  593. } BrTableCache;
  594. #if WASM_ENABLE_DEBUG_INTERP != 0
  595. typedef struct WASMFastOPCodeNode {
  596. struct WASMFastOPCodeNode *next;
  597. uint64 offset;
  598. uint8 orig_op;
  599. } WASMFastOPCodeNode;
  600. #endif
  601. #if WASM_ENABLE_LOAD_CUSTOM_SECTION != 0
  602. typedef struct WASMCustomSection {
  603. struct WASMCustomSection *next;
  604. /* Start address of the section name */
  605. char *name_addr;
  606. /* Length of the section name decoded from leb */
  607. uint32 name_len;
  608. /* Start address of the content (name len and name skipped) */
  609. uint8 *content_addr;
  610. uint32 content_len;
  611. } WASMCustomSection;
  612. #endif
  613. #if WASM_ENABLE_FAST_JIT != 0 || WASM_ENABLE_JIT != 0
  614. struct AOTCompData;
  615. struct AOTCompContext;
  616. /* Orc JIT thread arguments */
  617. typedef struct OrcJitThreadArg {
  618. #if WASM_ENABLE_JIT != 0
  619. struct AOTCompContext *comp_ctx;
  620. #endif
  621. struct WASMModule *module;
  622. uint32 group_idx;
  623. } OrcJitThreadArg;
  624. #endif
  625. struct WASMModuleInstance;
  626. struct WASMModule {
  627. /* Module type, for module loaded from WASM bytecode binary,
  628. this field is Wasm_Module_Bytecode;
  629. for module loaded from AOT file, this field is
  630. Wasm_Module_AoT, and this structure should be treated as
  631. AOTModule structure. */
  632. uint32 module_type;
  633. uint32 type_count;
  634. uint32 import_count;
  635. uint32 function_count;
  636. uint32 table_count;
  637. uint32 memory_count;
  638. uint32 global_count;
  639. uint32 export_count;
  640. uint32 table_seg_count;
  641. /* data seg count read from data segment section */
  642. uint32 data_seg_count;
  643. #if WASM_ENABLE_BULK_MEMORY != 0
  644. /* data count read from datacount section */
  645. uint32 data_seg_count1;
  646. #endif
  647. uint32 import_function_count;
  648. uint32 import_table_count;
  649. uint32 import_memory_count;
  650. uint32 import_global_count;
  651. WASMImport *import_functions;
  652. WASMImport *import_tables;
  653. WASMImport *import_memories;
  654. WASMImport *import_globals;
  655. WASMType **types;
  656. WASMImport *imports;
  657. WASMFunction **functions;
  658. WASMTable *tables;
  659. WASMMemory *memories;
  660. WASMGlobal *globals;
  661. WASMExport *exports;
  662. WASMTableSeg *table_segments;
  663. WASMDataSeg **data_segments;
  664. uint32 start_function;
  665. /* total global variable size */
  666. uint32 global_data_size;
  667. /* the index of auxiliary __data_end global,
  668. -1 means unexported */
  669. uint32 aux_data_end_global_index;
  670. /* auxiliary __data_end exported by wasm app */
  671. uint32 aux_data_end;
  672. /* the index of auxiliary __heap_base global,
  673. -1 means unexported */
  674. uint32 aux_heap_base_global_index;
  675. /* auxiliary __heap_base exported by wasm app */
  676. uint32 aux_heap_base;
  677. /* the index of auxiliary stack top global,
  678. -1 means unexported */
  679. uint32 aux_stack_top_global_index;
  680. /* auxiliary stack bottom resolved */
  681. uint32 aux_stack_bottom;
  682. /* auxiliary stack size resolved */
  683. uint32 aux_stack_size;
  684. /* the index of malloc/free function,
  685. -1 means unexported */
  686. uint32 malloc_function;
  687. uint32 free_function;
  688. /* the index of __retain function,
  689. -1 means unexported */
  690. uint32 retain_function;
  691. /* Whether there is possible memory grow, e.g. memory.grow opcode */
  692. bool possible_memory_grow;
  693. StringList const_str_list;
  694. #if WASM_ENABLE_FAST_INTERP == 0
  695. bh_list br_table_cache_list_head;
  696. bh_list *br_table_cache_list;
  697. #endif
  698. #if WASM_ENABLE_LIBC_WASI != 0
  699. WASIArguments wasi_args;
  700. bool import_wasi_api;
  701. #endif
  702. #if WASM_ENABLE_MULTI_MODULE != 0
  703. /* TODO: add mutex for mutli-thread? */
  704. bh_list import_module_list_head;
  705. bh_list *import_module_list;
  706. #endif
  707. #if WASM_ENABLE_GC != 0
  708. /* Ref types hash set */
  709. HashMap *ref_type_set;
  710. struct WASMRttType **rtt_types;
  711. korp_mutex rtt_type_lock;
  712. #endif
  713. #if WASM_ENABLE_DEBUG_INTERP != 0 || WASM_ENABLE_DEBUG_AOT != 0
  714. bh_list fast_opcode_list;
  715. uint8 *buf_code;
  716. uint64 buf_code_size;
  717. #endif
  718. #if WASM_ENABLE_DEBUG_INTERP != 0 || WASM_ENABLE_DEBUG_AOT != 0 \
  719. || WASM_ENABLE_FAST_JIT != 0
  720. uint8 *load_addr;
  721. uint64 load_size;
  722. #endif
  723. #if WASM_ENABLE_DEBUG_INTERP != 0 \
  724. || (WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT != 0 \
  725. && WASM_ENABLE_LAZY_JIT != 0)
  726. /**
  727. * List of instances referred to this module. When source debugging
  728. * feature is enabled, the debugger may modify the code section of
  729. * the module, so we need to report a warning if user create several
  730. * instances based on the same module.
  731. *
  732. * Also add the instance to the list for Fast JIT to LLVM JIT
  733. * tier-up, since we need to lazily update the LLVM func pointers
  734. * in the instance.
  735. */
  736. struct WASMModuleInstance *instance_list;
  737. korp_mutex instance_list_lock;
  738. #endif
  739. #if WASM_ENABLE_CUSTOM_NAME_SECTION != 0
  740. const uint8 *name_section_buf;
  741. const uint8 *name_section_buf_end;
  742. #endif
  743. #if WASM_ENABLE_LOAD_CUSTOM_SECTION != 0
  744. WASMCustomSection *custom_section_list;
  745. #endif
  746. #if WASM_ENABLE_FAST_JIT != 0
  747. /**
  748. * func pointers of Fast JITed (un-imported) functions
  749. * for non Multi-Tier JIT mode:
  750. * (1) when lazy jit is disabled, each pointer is set to the compiled
  751. * fast jit jitted code
  752. * (2) when lazy jit is enabled, each pointer is firstly inited as
  753. * jit_global->compile_fast_jit_and_then_call, and then set to the
  754. * compiled fast jit jitted code when it is called (the stub will
  755. * compile the jit function and then update itself)
  756. * for Multi-Tier JIT mode:
  757. * each pointer is firstly inited as compile_fast_jit_and_then_call,
  758. * and then set to the compiled fast jit jitted code when it is called,
  759. * and when the llvm jit func ptr of the same function is compiled, it
  760. * will be set to call_to_llvm_jit_from_fast_jit of this function type
  761. * (tier-up from fast-jit to llvm-jit)
  762. */
  763. void **fast_jit_func_ptrs;
  764. /* locks for Fast JIT lazy compilation */
  765. korp_mutex fast_jit_thread_locks[WASM_ORC_JIT_BACKEND_THREAD_NUM];
  766. bool fast_jit_thread_locks_inited[WASM_ORC_JIT_BACKEND_THREAD_NUM];
  767. #endif
  768. #if WASM_ENABLE_JIT != 0
  769. struct AOTCompData *comp_data;
  770. struct AOTCompContext *comp_ctx;
  771. /**
  772. * func pointers of LLVM JITed (un-imported) functions
  773. * for non Multi-Tier JIT mode:
  774. * each pointer is set to the lookuped llvm jit func ptr, note that it
  775. * is a stub and will trigger the actual compilation when it is called
  776. * for Multi-Tier JIT mode:
  777. * each pointer is inited as call_to_fast_jit code block, when the llvm
  778. * jit func ptr is actually compiled, it is set to the compiled llvm jit
  779. * func ptr
  780. */
  781. void **func_ptrs;
  782. /* whether the func pointers are compiled */
  783. bool *func_ptrs_compiled;
  784. #endif
  785. #if WASM_ENABLE_FAST_JIT != 0 || WASM_ENABLE_JIT != 0
  786. /* backend compilation threads */
  787. korp_tid orcjit_threads[WASM_ORC_JIT_BACKEND_THREAD_NUM];
  788. /* backend thread arguments */
  789. OrcJitThreadArg orcjit_thread_args[WASM_ORC_JIT_BACKEND_THREAD_NUM];
  790. /* whether to stop the compilation of backend threads */
  791. bool orcjit_stop_compiling;
  792. #endif
  793. #if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT != 0 \
  794. && WASM_ENABLE_LAZY_JIT != 0
  795. /* wait lock/cond for the synchronization of
  796. the llvm jit initialization */
  797. korp_mutex tierup_wait_lock;
  798. korp_cond tierup_wait_cond;
  799. bool tierup_wait_lock_inited;
  800. korp_tid llvm_jit_init_thread;
  801. /* whether the llvm jit is initialized */
  802. bool llvm_jit_inited;
  803. /* Whether to enable llvm jit compilation:
  804. it is set to true only when there is a module instance starts to
  805. run with running mode Mode_LLVM_JIT or Mode_Multi_Tier_JIT,
  806. since no need to enable llvm jit compilation for Mode_Interp and
  807. Mode_Fast_JIT, so as to improve performance for them */
  808. bool enable_llvm_jit_compilation;
  809. /* The count of groups which finish compiling the fast jit
  810. functions in that group */
  811. uint32 fast_jit_ready_groups;
  812. #endif
  813. };
  814. typedef struct BlockType {
  815. /* Block type may be expressed in one of two forms:
  816. * either by the type of the single return value or
  817. * by a type index of module.
  818. */
  819. union {
  820. struct {
  821. uint8 type;
  822. #if WASM_ENABLE_GC != 0
  823. WASMRefTypeMap ref_type_map;
  824. #endif
  825. } value_type;
  826. WASMFuncType *type;
  827. } u;
  828. bool is_value_type;
  829. } BlockType;
  830. typedef struct WASMBranchBlock {
  831. uint8 *begin_addr;
  832. uint8 *target_addr;
  833. uint32 *frame_sp;
  834. uint32 cell_num;
  835. } WASMBranchBlock;
  836. /* Execution environment, e.g. stack info */
  837. /**
  838. * Align an unsigned value on a alignment boundary.
  839. *
  840. * @param v the value to be aligned
  841. * @param b the alignment boundary (2, 4, 8, ...)
  842. *
  843. * @return the aligned value
  844. */
  845. inline static unsigned
  846. align_uint(unsigned v, unsigned b)
  847. {
  848. unsigned m = b - 1;
  849. return (v + m) & ~m;
  850. }
  851. /**
  852. * Return the hash value of c string.
  853. */
  854. inline static uint32
  855. wasm_string_hash(const char *str)
  856. {
  857. unsigned h = (unsigned)strlen(str);
  858. const uint8 *p = (uint8 *)str;
  859. const uint8 *end = p + h;
  860. while (p != end)
  861. h = ((h << 5) - h) + *p++;
  862. return h;
  863. }
  864. /**
  865. * Whether two c strings are equal.
  866. */
  867. inline static bool
  868. wasm_string_equal(const char *s1, const char *s2)
  869. {
  870. return strcmp(s1, s2) == 0 ? true : false;
  871. }
  872. /**
  873. * Return the byte size of value type.
  874. */
  875. inline static uint32
  876. wasm_value_type_size(uint8 value_type)
  877. {
  878. if (value_type == VALUE_TYPE_VOID)
  879. return 0;
  880. else if (value_type == VALUE_TYPE_I32 || value_type == VALUE_TYPE_F32
  881. || value_type == VALUE_TYPE_ANY)
  882. return sizeof(int32);
  883. else if (value_type == VALUE_TYPE_I64 || value_type == VALUE_TYPE_F64)
  884. return sizeof(int64);
  885. #if WASM_ENABLE_SIMD != 0
  886. else if (value_type == VALUE_TYPE_V128)
  887. return sizeof(int64) * 2;
  888. #endif
  889. #if WASM_ENABLE_GC != 0
  890. else if (value_type >= (uint8)REF_TYPE_NULLREF /* 0x65 */
  891. && value_type <= (uint8)REF_TYPE_FUNCREF /* 0x70 */)
  892. return sizeof(uintptr_t);
  893. #elif WASM_ENABLE_REF_TYPES != 0
  894. else if (value_type == VALUE_TYPE_FUNCREF
  895. || value_type == VALUE_TYPE_EXTERNREF)
  896. return sizeof(uint32);
  897. #endif
  898. else {
  899. bh_assert(0);
  900. }
  901. return 0;
  902. }
  903. inline static uint16
  904. wasm_value_type_cell_num(uint8 value_type)
  905. {
  906. return wasm_value_type_size(value_type) / 4;
  907. }
  908. inline static uint32
  909. wasm_get_cell_num(const uint8 *types, uint32 type_count)
  910. {
  911. uint32 cell_num = 0;
  912. uint32 i;
  913. for (i = 0; i < type_count; i++)
  914. cell_num += wasm_value_type_cell_num(types[i]);
  915. return cell_num;
  916. }
  917. #if WASM_ENABLE_REF_TYPES != 0
  918. inline static uint16
  919. wasm_value_type_cell_num_outside(uint8 value_type)
  920. {
  921. if (VALUE_TYPE_EXTERNREF == value_type) {
  922. return sizeof(uintptr_t) / sizeof(uint32);
  923. }
  924. else {
  925. return wasm_value_type_cell_num(value_type);
  926. }
  927. }
  928. #endif
  929. #if WASM_ENABLE_GC == 0
  930. inline static bool
  931. wasm_type_equal(const WASMType *type1, const WASMType *type2,
  932. const WASMTypePtr *types, uint32 type_count)
  933. {
  934. const WASMFuncType *func_type1 = (const WASMFuncType *)type1;
  935. const WASMFuncType *func_type2 = (const WASMFuncType *)type2;
  936. if (type1 == type2) {
  937. return true;
  938. }
  939. return (func_type1->param_count == func_type2->param_count
  940. && func_type1->result_count == func_type2->result_count
  941. && memcmp(
  942. func_type1->types, func_type2->types,
  943. (uint32)(func_type1->param_count + func_type1->result_count))
  944. == 0)
  945. ? true
  946. : false;
  947. (void)types;
  948. (void)type_count;
  949. }
  950. #else
  951. /* implemented in gc_type.c */
  952. bool
  953. wasm_type_equal(const WASMType *type1, const WASMType *type2,
  954. const WASMTypePtr *types, uint32 type_count);
  955. #endif
  956. inline static uint32
  957. wasm_get_smallest_type_idx(const WASMTypePtr *types, uint32 type_count,
  958. uint32 cur_type_idx)
  959. {
  960. uint32 i;
  961. for (i = 0; i < cur_type_idx; i++) {
  962. if (wasm_type_equal(types[cur_type_idx], types[i], types, type_count))
  963. return i;
  964. }
  965. return cur_type_idx;
  966. }
  967. #if WASM_ENABLE_GC == 0
  968. static inline uint32
  969. block_type_get_param_types(BlockType *block_type, uint8 **p_param_types)
  970. #else
  971. static inline uint32
  972. block_type_get_param_types(BlockType *block_type, uint8 **p_param_types,
  973. WASMRefTypeMap **p_param_reftype_maps,
  974. uint32 *p_param_reftype_map_count)
  975. #endif
  976. {
  977. uint32 param_count = 0;
  978. if (!block_type->is_value_type) {
  979. WASMFuncType *func_type = block_type->u.type;
  980. *p_param_types = func_type->types;
  981. param_count = func_type->param_count;
  982. #if WASM_ENABLE_GC != 0
  983. *p_param_reftype_maps = func_type->ref_type_maps;
  984. *p_param_reftype_map_count =
  985. func_type->result_ref_type_maps - func_type->ref_type_maps;
  986. #endif
  987. }
  988. else {
  989. *p_param_types = NULL;
  990. param_count = 0;
  991. #if WASM_ENABLE_GC != 0
  992. *p_param_reftype_maps = NULL;
  993. *p_param_reftype_map_count = 0;
  994. #endif
  995. }
  996. return param_count;
  997. }
  998. #if WASM_ENABLE_GC == 0
  999. static inline uint32
  1000. block_type_get_result_types(BlockType *block_type, uint8 **p_result_types)
  1001. #else
  1002. static inline uint32
  1003. block_type_get_result_types(BlockType *block_type, uint8 **p_result_types,
  1004. WASMRefTypeMap **p_result_reftype_maps,
  1005. uint32 *p_result_reftype_map_count)
  1006. #endif
  1007. {
  1008. uint32 result_count = 0;
  1009. uint8 *result_types = NULL;
  1010. #if WASM_ENABLE_GC != 0
  1011. uint8 type;
  1012. uint32 result_reftype_map_count = 0;
  1013. WASMRefTypeMap *result_reftype_maps = NULL;
  1014. #endif
  1015. if (block_type->is_value_type) {
  1016. if (block_type->u.value_type.type != VALUE_TYPE_VOID) {
  1017. result_types = &block_type->u.value_type.type;
  1018. result_count = 1;
  1019. #if WASM_ENABLE_GC != 0
  1020. type = block_type->u.value_type.type;
  1021. if (type == (uint8)REF_TYPE_HT_NULLABLE
  1022. || type == (uint8)REF_TYPE_HT_NON_NULLABLE) {
  1023. result_reftype_maps = &block_type->u.value_type.ref_type_map;
  1024. result_reftype_map_count = 1;
  1025. }
  1026. #endif
  1027. }
  1028. }
  1029. else {
  1030. WASMFuncType *func_type = block_type->u.type;
  1031. result_types = func_type->types + func_type->param_count;
  1032. result_count = func_type->result_count;
  1033. #if WASM_ENABLE_GC != 0
  1034. result_reftype_maps = func_type->result_ref_type_maps;
  1035. result_reftype_map_count = (uint32)(func_type->ref_type_map_count
  1036. - (func_type->result_ref_type_maps
  1037. - func_type->ref_type_maps));
  1038. #endif
  1039. }
  1040. *p_result_types = result_types;
  1041. #if WASM_ENABLE_GC != 0
  1042. *p_result_reftype_maps = result_reftype_maps;
  1043. *p_result_reftype_map_count = result_reftype_map_count;
  1044. #endif
  1045. return result_count;
  1046. }
  1047. static inline uint32
  1048. block_type_get_arity(const BlockType *block_type, uint8 label_type)
  1049. {
  1050. if (label_type == LABEL_TYPE_LOOP) {
  1051. if (block_type->is_value_type)
  1052. return 0;
  1053. else
  1054. return block_type->u.type->param_count;
  1055. }
  1056. else {
  1057. if (block_type->is_value_type) {
  1058. return block_type->u.value_type.type != VALUE_TYPE_VOID ? 1 : 0;
  1059. }
  1060. else
  1061. return block_type->u.type->result_count;
  1062. }
  1063. return 0;
  1064. }
  1065. #ifdef __cplusplus
  1066. } /* end of extern "C" */
  1067. #endif
  1068. #endif /* end of _WASM_H_ */