wasm.h 40 KB

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