wasm.h 42 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517
  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. #if WASM_ENABLE_BRANCH_HINTS != 0
  679. uint8 *code_body_begin;
  680. #endif
  681. };
  682. #if WASM_ENABLE_TAGS != 0
  683. struct WASMTag {
  684. uint8 attribute; /* the attribute property of the tag (expected to be 0) */
  685. uint32 type; /* the type of the tag (expected valid inden in type table) */
  686. WASMFuncType *tag_type;
  687. };
  688. #endif
  689. #if WASM_ENABLE_BRANCH_HINTS != 0
  690. enum WASMCompilationHintType {
  691. DUMMY = 0,
  692. WASM_COMPILATION_BRANCH_HINT = 0,
  693. };
  694. struct WASMCompilationHint {
  695. struct WASMCompilationHint *next;
  696. enum WASMCompilationHintType type;
  697. };
  698. struct WASMCompilationHintBranchHint {
  699. struct WASMCompilationHint *next;
  700. enum WASMCompilationHintType type;
  701. uint32 offset;
  702. bool is_likely;
  703. };
  704. #endif
  705. struct WASMGlobal {
  706. WASMGlobalType type;
  707. #if WASM_ENABLE_GC != 0
  708. WASMRefType *ref_type;
  709. #endif
  710. InitializerExpression init_expr;
  711. #if WASM_ENABLE_FAST_JIT != 0
  712. /* The data offset of current global in global data */
  713. uint32 data_offset;
  714. #endif
  715. };
  716. typedef struct WASMExport {
  717. char *name;
  718. uint8 kind;
  719. uint32 index;
  720. } WASMExport;
  721. typedef struct WASMTableSeg {
  722. /* 0 to 7 */
  723. uint32 mode;
  724. /* funcref or externref, elemkind will be considered as funcref */
  725. uint32 elem_type;
  726. #if WASM_ENABLE_GC != 0
  727. WASMRefType *elem_ref_type;
  728. #endif
  729. /* optional, only for active */
  730. uint32 table_index;
  731. InitializerExpression base_offset;
  732. uint32 value_count;
  733. InitializerExpression *init_values;
  734. } WASMTableSeg;
  735. typedef struct WASMDataSeg {
  736. uint32 memory_index;
  737. InitializerExpression base_offset;
  738. uint32 data_length;
  739. #if WASM_ENABLE_BULK_MEMORY != 0
  740. bool is_passive;
  741. #endif
  742. uint8 *data;
  743. bool is_data_cloned;
  744. } WASMDataSeg;
  745. typedef struct BlockAddr {
  746. const uint8 *start_addr;
  747. uint8 *else_addr;
  748. uint8 *end_addr;
  749. } BlockAddr;
  750. #if WASM_ENABLE_LIBC_WASI != 0
  751. typedef struct WASIArguments {
  752. const char **dir_list;
  753. uint32 dir_count;
  754. const char **map_dir_list;
  755. uint32 map_dir_count;
  756. const char **env;
  757. uint32 env_count;
  758. /* in CIDR notation */
  759. const char **addr_pool;
  760. uint32 addr_count;
  761. const char **ns_lookup_pool;
  762. uint32 ns_lookup_count;
  763. char **argv;
  764. uint32 argc;
  765. os_raw_file_handle stdio[3];
  766. } WASIArguments;
  767. #endif
  768. typedef struct StringNode {
  769. struct StringNode *next;
  770. char *str;
  771. } StringNode, *StringList;
  772. typedef struct BrTableCache {
  773. struct BrTableCache *next;
  774. /* Address of br_table opcode */
  775. uint8 *br_table_op_addr;
  776. uint32 br_count;
  777. uint32 br_depths[1];
  778. } BrTableCache;
  779. #if WASM_ENABLE_DEBUG_INTERP != 0
  780. typedef struct WASMFastOPCodeNode {
  781. struct WASMFastOPCodeNode *next;
  782. uint64 offset;
  783. uint8 orig_op;
  784. } WASMFastOPCodeNode;
  785. #endif
  786. #if WASM_ENABLE_LOAD_CUSTOM_SECTION != 0
  787. typedef struct WASMCustomSection {
  788. struct WASMCustomSection *next;
  789. /* Start address of the section name */
  790. char *name_addr;
  791. /* Length of the section name decoded from leb */
  792. uint32 name_len;
  793. /* Start address of the content (name len and name skipped) */
  794. uint8 *content_addr;
  795. uint32 content_len;
  796. } WASMCustomSection;
  797. #endif
  798. #if WASM_ENABLE_FAST_JIT != 0 || WASM_ENABLE_JIT != 0
  799. struct AOTCompData;
  800. struct AOTCompContext;
  801. /* Orc JIT thread arguments */
  802. typedef struct OrcJitThreadArg {
  803. #if WASM_ENABLE_JIT != 0
  804. struct AOTCompContext *comp_ctx;
  805. #endif
  806. struct WASMModule *module;
  807. uint32 group_idx;
  808. } OrcJitThreadArg;
  809. #endif
  810. struct WASMModuleInstance;
  811. struct WASMModule {
  812. /* Module type, for module loaded from WASM bytecode binary,
  813. this field is Wasm_Module_Bytecode;
  814. for module loaded from AOT file, this field is
  815. Wasm_Module_AoT, and this structure should be treated as
  816. AOTModule structure. */
  817. uint32 module_type;
  818. /* the package version read from the WASM file */
  819. uint32 package_version;
  820. uint32 type_count;
  821. uint32 import_count;
  822. uint32 function_count;
  823. uint32 table_count;
  824. uint32 memory_count;
  825. #if WASM_ENABLE_TAGS != 0
  826. uint32 tag_count;
  827. #endif
  828. uint32 global_count;
  829. uint32 export_count;
  830. uint32 table_seg_count;
  831. /* data seg count read from data segment section */
  832. uint32 data_seg_count;
  833. #if WASM_ENABLE_BULK_MEMORY != 0
  834. /* data count read from datacount section */
  835. uint32 data_seg_count1;
  836. #endif
  837. #if WASM_ENABLE_GC != 0
  838. #if WASM_ENABLE_STRINGREF != 0
  839. uint32 string_literal_count;
  840. uint32 *string_literal_lengths;
  841. const uint8 **string_literal_ptrs;
  842. #endif
  843. #endif
  844. uint32 import_function_count;
  845. uint32 import_table_count;
  846. uint32 import_memory_count;
  847. #if WASM_ENABLE_TAGS != 0
  848. uint32 import_tag_count;
  849. #endif
  850. uint32 import_global_count;
  851. WASMImport *import_functions;
  852. WASMImport *import_tables;
  853. WASMImport *import_memories;
  854. #if WASM_ENABLE_TAGS != 0
  855. WASMImport *import_tags;
  856. #endif
  857. WASMImport *import_globals;
  858. WASMType **types;
  859. WASMImport *imports;
  860. WASMFunction **functions;
  861. WASMTable *tables;
  862. WASMMemory *memories;
  863. #if WASM_ENABLE_TAGS != 0
  864. WASMTag **tags;
  865. #endif
  866. WASMGlobal *globals;
  867. WASMExport *exports;
  868. WASMTableSeg *table_segments;
  869. WASMDataSeg **data_segments;
  870. uint32 start_function;
  871. /* total global variable size */
  872. uint32 global_data_size;
  873. /* the index of auxiliary __data_end global,
  874. -1 means unexported */
  875. uint32 aux_data_end_global_index;
  876. /* auxiliary __data_end exported by wasm app */
  877. uint64 aux_data_end;
  878. /* the index of auxiliary __heap_base global,
  879. -1 means unexported */
  880. uint32 aux_heap_base_global_index;
  881. /* auxiliary __heap_base exported by wasm app */
  882. uint64 aux_heap_base;
  883. /* the index of auxiliary stack top global,
  884. -1 means unexported */
  885. uint32 aux_stack_top_global_index;
  886. /* auxiliary stack bottom resolved */
  887. uint64 aux_stack_bottom;
  888. /* auxiliary stack size resolved */
  889. uint32 aux_stack_size;
  890. /* the index of malloc/free function,
  891. -1 means unexported */
  892. uint32 malloc_function;
  893. uint32 free_function;
  894. /* the index of __retain function,
  895. -1 means unexported */
  896. uint32 retain_function;
  897. /* Whether there is possible memory grow, e.g. memory.grow opcode */
  898. bool possible_memory_grow;
  899. StringList const_str_list;
  900. #if WASM_ENABLE_FAST_INTERP == 0
  901. bh_list br_table_cache_list_head;
  902. bh_list *br_table_cache_list;
  903. #endif
  904. #if WASM_ENABLE_LIBC_WASI != 0
  905. WASIArguments wasi_args;
  906. bool import_wasi_api;
  907. #endif
  908. #if WASM_ENABLE_MULTI_MODULE != 0
  909. /* TODO: add mutex for mutli-thread? */
  910. bh_list import_module_list_head;
  911. bh_list *import_module_list;
  912. #endif
  913. #if WASM_ENABLE_GC != 0
  914. /* Ref types hash set */
  915. HashMap *ref_type_set;
  916. struct WASMRttType **rtt_types;
  917. korp_mutex rtt_type_lock;
  918. #if WASM_ENABLE_STRINGREF != 0
  919. /* special rtts for stringref types
  920. - stringref
  921. - stringview_wtf8
  922. - stringview_wtf16
  923. - stringview_iter
  924. */
  925. struct WASMRttType *stringref_rtts[4];
  926. #endif
  927. #endif
  928. #if WASM_ENABLE_DEBUG_INTERP != 0 || WASM_ENABLE_DEBUG_AOT != 0
  929. bh_list fast_opcode_list;
  930. uint8 *buf_code;
  931. uint64 buf_code_size;
  932. #endif
  933. #if WASM_ENABLE_DEBUG_INTERP != 0 || WASM_ENABLE_FAST_JIT != 0 \
  934. || WASM_ENABLE_DUMP_CALL_STACK != 0 || WASM_ENABLE_JIT != 0 \
  935. || WASM_ENABLE_WAMR_COMPILER != 0
  936. uint8 *load_addr;
  937. uint64 load_size;
  938. #endif
  939. #if WASM_ENABLE_DEBUG_INTERP != 0 \
  940. || (WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT != 0 \
  941. && WASM_ENABLE_LAZY_JIT != 0)
  942. /**
  943. * List of instances referred to this module. When source debugging
  944. * feature is enabled, the debugger may modify the code section of
  945. * the module, so we need to report a warning if user create several
  946. * instances based on the same module.
  947. *
  948. * Also add the instance to the list for Fast JIT to LLVM JIT
  949. * tier-up, since we need to lazily update the LLVM func pointers
  950. * in the instance.
  951. */
  952. struct WASMModuleInstance *instance_list;
  953. korp_mutex instance_list_lock;
  954. #endif
  955. #if WASM_ENABLE_CUSTOM_NAME_SECTION != 0
  956. const uint8 *name_section_buf;
  957. const uint8 *name_section_buf_end;
  958. #endif
  959. #if WASM_ENABLE_BRANCH_HINTS != 0
  960. struct WASMCompilationHint **function_hints;
  961. #endif
  962. #if WASM_ENABLE_LOAD_CUSTOM_SECTION != 0
  963. WASMCustomSection *custom_section_list;
  964. #endif
  965. #if WASM_ENABLE_FAST_JIT != 0
  966. /**
  967. * func pointers of Fast JITed (un-imported) functions
  968. * for non Multi-Tier JIT mode:
  969. * (1) when lazy jit is disabled, each pointer is set to the compiled
  970. * fast jit jitted code
  971. * (2) when lazy jit is enabled, each pointer is firstly inited as
  972. * jit_global->compile_fast_jit_and_then_call, and then set to the
  973. * compiled fast jit jitted code when it is called (the stub will
  974. * compile the jit function and then update itself)
  975. * for Multi-Tier JIT mode:
  976. * each pointer is firstly inited as compile_fast_jit_and_then_call,
  977. * and then set to the compiled fast jit jitted code when it is called,
  978. * and when the llvm jit func ptr of the same function is compiled, it
  979. * will be set to call_to_llvm_jit_from_fast_jit of this function type
  980. * (tier-up from fast-jit to llvm-jit)
  981. */
  982. void **fast_jit_func_ptrs;
  983. /* locks for Fast JIT lazy compilation */
  984. korp_mutex fast_jit_thread_locks[WASM_ORC_JIT_BACKEND_THREAD_NUM];
  985. bool fast_jit_thread_locks_inited[WASM_ORC_JIT_BACKEND_THREAD_NUM];
  986. #endif
  987. #if WASM_ENABLE_JIT != 0
  988. struct AOTCompData *comp_data;
  989. struct AOTCompContext *comp_ctx;
  990. /**
  991. * func pointers of LLVM JITed (un-imported) functions
  992. * for non Multi-Tier JIT mode:
  993. * each pointer is set to the looked up llvm jit func ptr, note that it
  994. * is a stub and will trigger the actual compilation when it is called
  995. * for Multi-Tier JIT mode:
  996. * each pointer is inited as call_to_fast_jit code block, when the llvm
  997. * jit func ptr is actually compiled, it is set to the compiled llvm jit
  998. * func ptr
  999. */
  1000. void **func_ptrs;
  1001. /* whether the func pointers are compiled */
  1002. bool *func_ptrs_compiled;
  1003. #endif
  1004. #if WASM_ENABLE_FAST_JIT != 0 || WASM_ENABLE_JIT != 0
  1005. /* backend compilation threads */
  1006. korp_tid orcjit_threads[WASM_ORC_JIT_BACKEND_THREAD_NUM];
  1007. /* backend thread arguments */
  1008. OrcJitThreadArg orcjit_thread_args[WASM_ORC_JIT_BACKEND_THREAD_NUM];
  1009. /* whether to stop the compilation of backend threads */
  1010. bool orcjit_stop_compiling;
  1011. #endif
  1012. #if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT != 0 \
  1013. && WASM_ENABLE_LAZY_JIT != 0
  1014. /* wait lock/cond for the synchronization of
  1015. the llvm jit initialization */
  1016. korp_mutex tierup_wait_lock;
  1017. korp_cond tierup_wait_cond;
  1018. bool tierup_wait_lock_inited;
  1019. korp_tid llvm_jit_init_thread;
  1020. /* whether the llvm jit is initialized */
  1021. bool llvm_jit_inited;
  1022. /* Whether to enable llvm jit compilation:
  1023. it is set to true only when there is a module instance starts to
  1024. run with running mode Mode_LLVM_JIT or Mode_Multi_Tier_JIT,
  1025. since no need to enable llvm jit compilation for Mode_Interp and
  1026. Mode_Fast_JIT, so as to improve performance for them */
  1027. bool enable_llvm_jit_compilation;
  1028. /* The count of groups which finish compiling the fast jit
  1029. functions in that group */
  1030. uint32 fast_jit_ready_groups;
  1031. #endif
  1032. #if WASM_ENABLE_WAMR_COMPILER != 0
  1033. bool is_simd_used;
  1034. bool is_ref_types_used;
  1035. bool is_bulk_memory_used;
  1036. #endif
  1037. /* user defined name */
  1038. char *name;
  1039. /* Whether the underlying wasm binary buffer can be freed */
  1040. bool is_binary_freeable;
  1041. };
  1042. typedef struct BlockType {
  1043. /* Block type may be expressed in one of two forms:
  1044. * either by the type of the single return value or
  1045. * by a type index of module.
  1046. */
  1047. union {
  1048. struct {
  1049. uint8 type;
  1050. #if WASM_ENABLE_GC != 0
  1051. WASMRefTypeMap ref_type_map;
  1052. #endif
  1053. } value_type;
  1054. WASMFuncType *type;
  1055. } u;
  1056. bool is_value_type;
  1057. } BlockType;
  1058. typedef struct WASMBranchBlock {
  1059. uint8 *begin_addr;
  1060. uint8 *target_addr;
  1061. uint32 *frame_sp;
  1062. uint32 cell_num;
  1063. #if WASM_ENABLE_EXCE_HANDLING != 0
  1064. /* in exception handling, label_type needs to be stored to lookup exception
  1065. * handlers */
  1066. uint8 label_type;
  1067. #endif
  1068. } WASMBranchBlock;
  1069. /**
  1070. * Align an unsigned value on a alignment boundary.
  1071. *
  1072. * @param v the value to be aligned
  1073. * @param b the alignment boundary (2, 4, 8, ...)
  1074. *
  1075. * @return the aligned value
  1076. */
  1077. inline static unsigned
  1078. align_uint(unsigned v, unsigned b)
  1079. {
  1080. unsigned m = b - 1;
  1081. return (v + m) & ~m;
  1082. }
  1083. /**
  1084. * Align an 64 bit unsigned value on a alignment boundary.
  1085. *
  1086. * @param v the value to be aligned
  1087. * @param b the alignment boundary (2, 4, 8, ...)
  1088. *
  1089. * @return the aligned value
  1090. */
  1091. inline static uint64
  1092. align_uint64(uint64 v, uint64 b)
  1093. {
  1094. uint64 m = b - 1;
  1095. return (v + m) & ~m;
  1096. }
  1097. /**
  1098. * Check whether a piece of data is out of range
  1099. *
  1100. * @param offset the offset that the data starts
  1101. * @param len the length of the data
  1102. * @param max_size the maximum size of the data range
  1103. *
  1104. * @return true if out of range, false otherwise
  1105. */
  1106. inline static bool
  1107. offset_len_out_of_bounds(uint32 offset, uint32 len, uint32 max_size)
  1108. {
  1109. if (offset + len < offset /* integer overflow */
  1110. || offset + len > max_size)
  1111. return true;
  1112. return false;
  1113. }
  1114. /**
  1115. * Return the hash value of c string.
  1116. */
  1117. inline static uint32
  1118. wasm_string_hash(const char *str)
  1119. {
  1120. unsigned h = (unsigned)strlen(str);
  1121. const uint8 *p = (uint8 *)str;
  1122. const uint8 *end = p + h;
  1123. while (p != end)
  1124. h = ((h << 5) - h) + *p++;
  1125. return h;
  1126. }
  1127. /**
  1128. * Whether two c strings are equal.
  1129. */
  1130. inline static bool
  1131. wasm_string_equal(const char *s1, const char *s2)
  1132. {
  1133. return strcmp(s1, s2) == 0 ? true : false;
  1134. }
  1135. /**
  1136. * Return the byte size of value type with specific pointer size.
  1137. *
  1138. * Note: Please use wasm_value_type_size for interpreter, only aot compiler
  1139. * can use this API directly to calculate type size for different target
  1140. */
  1141. inline static uint32
  1142. wasm_value_type_size_internal(uint8 value_type, uint8 pointer_size)
  1143. {
  1144. if (value_type == VALUE_TYPE_VOID)
  1145. return 0;
  1146. else if (value_type == VALUE_TYPE_I32 || value_type == VALUE_TYPE_F32
  1147. || value_type == VALUE_TYPE_ANY)
  1148. return sizeof(int32);
  1149. else if (value_type == VALUE_TYPE_I64 || value_type == VALUE_TYPE_F64)
  1150. return sizeof(int64);
  1151. #if WASM_ENABLE_SIMD != 0
  1152. else if (value_type == VALUE_TYPE_V128)
  1153. return sizeof(int64) * 2;
  1154. #endif
  1155. #if WASM_ENABLE_GC == 0 && WASM_ENABLE_REF_TYPES != 0
  1156. else if (value_type == VALUE_TYPE_FUNCREF
  1157. || value_type == VALUE_TYPE_EXTERNREF)
  1158. return sizeof(uint32);
  1159. #elif WASM_ENABLE_GC != 0
  1160. else if ((value_type >= (uint8)REF_TYPE_ARRAYREF /* 0x6A */
  1161. && value_type <= (uint8)REF_TYPE_NULLFUNCREF) /* 0x73 */
  1162. || (value_type >= (uint8)REF_TYPE_HT_NULLABLE /* 0x63 */
  1163. && value_type <= (uint8)REF_TYPE_HT_NON_NULLABLE) /* 0x64 */
  1164. #if WASM_ENABLE_STRINGREF != 0
  1165. || (value_type >= (uint8)REF_TYPE_STRINGVIEWWTF8 /* 0x66 */
  1166. && value_type <= (uint8)REF_TYPE_STRINGREF) /* 0x67 */
  1167. || (value_type >= (uint8)REF_TYPE_STRINGVIEWITER /* 0x61 */
  1168. && value_type <= (uint8)REF_TYPE_STRINGVIEWWTF16) /* 0x62 */
  1169. #endif
  1170. )
  1171. return pointer_size;
  1172. else if (value_type == PACKED_TYPE_I8)
  1173. return sizeof(int8);
  1174. else if (value_type == PACKED_TYPE_I16)
  1175. return sizeof(int16);
  1176. #endif
  1177. else {
  1178. bh_assert(0 && "Unknown value type. It should be handled ahead.");
  1179. }
  1180. #if WASM_ENABLE_GC == 0
  1181. (void)pointer_size;
  1182. #endif
  1183. return 0;
  1184. }
  1185. /**
  1186. * Return the cell num of value type with specific pointer size.
  1187. *
  1188. * Note: Please use wasm_value_type_cell_num for interpreter, only aot compiler
  1189. * can use this API directly to calculate type cell num for different target
  1190. */
  1191. inline static uint16
  1192. wasm_value_type_cell_num_internal(uint8 value_type, uint8 pointer_size)
  1193. {
  1194. return wasm_value_type_size_internal(value_type, pointer_size) / 4;
  1195. }
  1196. /**
  1197. * Return the byte size of value type.
  1198. */
  1199. inline static uint32
  1200. wasm_value_type_size(uint8 value_type)
  1201. {
  1202. return wasm_value_type_size_internal(value_type, sizeof(uintptr_t));
  1203. }
  1204. inline static uint16
  1205. wasm_value_type_cell_num(uint8 value_type)
  1206. {
  1207. return wasm_value_type_size(value_type) / 4;
  1208. }
  1209. inline static uint32
  1210. wasm_get_cell_num(const uint8 *types, uint32 type_count)
  1211. {
  1212. uint32 cell_num = 0;
  1213. uint32 i;
  1214. for (i = 0; i < type_count; i++)
  1215. cell_num += wasm_value_type_cell_num(types[i]);
  1216. return cell_num;
  1217. }
  1218. #if WASM_ENABLE_REF_TYPES != 0
  1219. inline static uint16
  1220. wasm_value_type_cell_num_outside(uint8 value_type)
  1221. {
  1222. if (VALUE_TYPE_EXTERNREF == value_type) {
  1223. return sizeof(uintptr_t) / sizeof(uint32);
  1224. }
  1225. else {
  1226. return wasm_value_type_cell_num(value_type);
  1227. }
  1228. }
  1229. #endif
  1230. #if WASM_ENABLE_GC == 0
  1231. inline static bool
  1232. wasm_type_equal(const WASMType *type1, const WASMType *type2,
  1233. const WASMTypePtr *types, uint32 type_count)
  1234. {
  1235. const WASMFuncType *func_type1 = (const WASMFuncType *)type1;
  1236. const WASMFuncType *func_type2 = (const WASMFuncType *)type2;
  1237. if (type1 == type2) {
  1238. return true;
  1239. }
  1240. return (func_type1->param_count == func_type2->param_count
  1241. && func_type1->result_count == func_type2->result_count
  1242. && memcmp(
  1243. func_type1->types, func_type2->types,
  1244. (uint32)(func_type1->param_count + func_type1->result_count))
  1245. == 0)
  1246. ? true
  1247. : false;
  1248. (void)types;
  1249. (void)type_count;
  1250. }
  1251. #else
  1252. /* implemented in gc_type.c */
  1253. bool
  1254. wasm_type_equal(const WASMType *type1, const WASMType *type2,
  1255. const WASMTypePtr *types, uint32 type_count);
  1256. #endif
  1257. inline static uint32
  1258. wasm_get_smallest_type_idx(const WASMTypePtr *types, uint32 type_count,
  1259. uint32 cur_type_idx)
  1260. {
  1261. uint32 i;
  1262. for (i = 0; i < cur_type_idx; i++) {
  1263. if (wasm_type_equal(types[cur_type_idx], types[i], types, type_count))
  1264. return i;
  1265. }
  1266. return cur_type_idx;
  1267. }
  1268. #if WASM_ENABLE_GC == 0
  1269. static inline uint32
  1270. block_type_get_param_types(BlockType *block_type, uint8 **p_param_types)
  1271. #else
  1272. static inline uint32
  1273. block_type_get_param_types(BlockType *block_type, uint8 **p_param_types,
  1274. WASMRefTypeMap **p_param_reftype_maps,
  1275. uint32 *p_param_reftype_map_count)
  1276. #endif
  1277. {
  1278. uint32 param_count = 0;
  1279. if (!block_type->is_value_type) {
  1280. WASMFuncType *func_type = block_type->u.type;
  1281. *p_param_types = func_type->types;
  1282. param_count = func_type->param_count;
  1283. #if WASM_ENABLE_GC != 0
  1284. *p_param_reftype_maps = func_type->ref_type_maps;
  1285. *p_param_reftype_map_count = (uint32)(func_type->result_ref_type_maps
  1286. - func_type->ref_type_maps);
  1287. #endif
  1288. }
  1289. else {
  1290. *p_param_types = NULL;
  1291. param_count = 0;
  1292. #if WASM_ENABLE_GC != 0
  1293. *p_param_reftype_maps = NULL;
  1294. *p_param_reftype_map_count = 0;
  1295. #endif
  1296. }
  1297. return param_count;
  1298. }
  1299. #if WASM_ENABLE_GC == 0
  1300. static inline uint32
  1301. block_type_get_result_types(BlockType *block_type, uint8 **p_result_types)
  1302. #else
  1303. static inline uint32
  1304. block_type_get_result_types(BlockType *block_type, uint8 **p_result_types,
  1305. WASMRefTypeMap **p_result_reftype_maps,
  1306. uint32 *p_result_reftype_map_count)
  1307. #endif
  1308. {
  1309. uint32 result_count = 0;
  1310. uint8 *result_types = NULL;
  1311. #if WASM_ENABLE_GC != 0
  1312. uint8 type;
  1313. uint32 result_reftype_map_count = 0;
  1314. WASMRefTypeMap *result_reftype_maps = NULL;
  1315. #endif
  1316. if (block_type->is_value_type) {
  1317. if (block_type->u.value_type.type != VALUE_TYPE_VOID) {
  1318. result_types = &block_type->u.value_type.type;
  1319. result_count = 1;
  1320. #if WASM_ENABLE_GC != 0
  1321. type = block_type->u.value_type.type;
  1322. if (type == (uint8)REF_TYPE_HT_NULLABLE
  1323. || type == (uint8)REF_TYPE_HT_NON_NULLABLE) {
  1324. result_reftype_maps = &block_type->u.value_type.ref_type_map;
  1325. result_reftype_map_count = 1;
  1326. }
  1327. #endif
  1328. }
  1329. }
  1330. else {
  1331. WASMFuncType *func_type = block_type->u.type;
  1332. result_types = func_type->types + func_type->param_count;
  1333. result_count = func_type->result_count;
  1334. #if WASM_ENABLE_GC != 0
  1335. result_reftype_maps = func_type->result_ref_type_maps;
  1336. result_reftype_map_count = (uint32)(func_type->ref_type_map_count
  1337. - (func_type->result_ref_type_maps
  1338. - func_type->ref_type_maps));
  1339. #endif
  1340. }
  1341. *p_result_types = result_types;
  1342. #if WASM_ENABLE_GC != 0
  1343. *p_result_reftype_maps = result_reftype_maps;
  1344. *p_result_reftype_map_count = result_reftype_map_count;
  1345. #endif
  1346. return result_count;
  1347. }
  1348. static inline uint32
  1349. block_type_get_arity(const BlockType *block_type, uint8 label_type)
  1350. {
  1351. if (label_type == LABEL_TYPE_LOOP) {
  1352. if (block_type->is_value_type)
  1353. return 0;
  1354. else
  1355. return block_type->u.type->param_count;
  1356. }
  1357. else {
  1358. if (block_type->is_value_type) {
  1359. return block_type->u.value_type.type != VALUE_TYPE_VOID ? 1 : 0;
  1360. }
  1361. else
  1362. return block_type->u.type->result_count;
  1363. }
  1364. return 0;
  1365. }
  1366. #ifdef __cplusplus
  1367. } /* end of extern "C" */
  1368. #endif
  1369. #endif /* end of _WASM_H_ */