wasm.h 40 KB

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