wasm.h 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814
  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. #ifdef __cplusplus
  11. extern "C" {
  12. #endif
  13. /** Value Type */
  14. #define VALUE_TYPE_I32 0x7F
  15. #define VALUE_TYPE_I64 0X7E
  16. #define VALUE_TYPE_F32 0x7D
  17. #define VALUE_TYPE_F64 0x7C
  18. #define VALUE_TYPE_V128 0x7B
  19. #define VALUE_TYPE_FUNCREF 0x70
  20. #define VALUE_TYPE_EXTERNREF 0x6F
  21. #define VALUE_TYPE_VOID 0x40
  22. /* Used by AOT */
  23. #define VALUE_TYPE_I1 0x41
  24. /* Used by loader to represent any type of i32/i64/f32/f64 */
  25. #define VALUE_TYPE_ANY 0x42
  26. #define DEFAULT_NUM_BYTES_PER_PAGE 65536
  27. #define DEFAULT_MAX_PAGES 65536
  28. #define NULL_REF (0xFFFFFFFF)
  29. #define TABLE_MAX_SIZE (1024)
  30. #define INIT_EXPR_TYPE_I32_CONST 0x41
  31. #define INIT_EXPR_TYPE_I64_CONST 0x42
  32. #define INIT_EXPR_TYPE_F32_CONST 0x43
  33. #define INIT_EXPR_TYPE_F64_CONST 0x44
  34. #define INIT_EXPR_TYPE_V128_CONST 0xFD
  35. /* = WASM_OP_REF_FUNC */
  36. #define INIT_EXPR_TYPE_FUNCREF_CONST 0xD2
  37. /* = WASM_OP_REF_NULL */
  38. #define INIT_EXPR_TYPE_REFNULL_CONST 0xD0
  39. #define INIT_EXPR_TYPE_GET_GLOBAL 0x23
  40. #define INIT_EXPR_TYPE_ERROR 0xff
  41. #define WASM_MAGIC_NUMBER 0x6d736100
  42. #define WASM_CURRENT_VERSION 1
  43. #define SECTION_TYPE_USER 0
  44. #define SECTION_TYPE_TYPE 1
  45. #define SECTION_TYPE_IMPORT 2
  46. #define SECTION_TYPE_FUNC 3
  47. #define SECTION_TYPE_TABLE 4
  48. #define SECTION_TYPE_MEMORY 5
  49. #define SECTION_TYPE_GLOBAL 6
  50. #define SECTION_TYPE_EXPORT 7
  51. #define SECTION_TYPE_START 8
  52. #define SECTION_TYPE_ELEM 9
  53. #define SECTION_TYPE_CODE 10
  54. #define SECTION_TYPE_DATA 11
  55. #if WASM_ENABLE_BULK_MEMORY != 0
  56. #define SECTION_TYPE_DATACOUNT 12
  57. #endif
  58. #define SUB_SECTION_TYPE_MODULE 0
  59. #define SUB_SECTION_TYPE_FUNC 1
  60. #define SUB_SECTION_TYPE_LOCAL 2
  61. #define IMPORT_KIND_FUNC 0
  62. #define IMPORT_KIND_TABLE 1
  63. #define IMPORT_KIND_MEMORY 2
  64. #define IMPORT_KIND_GLOBAL 3
  65. #define EXPORT_KIND_FUNC 0
  66. #define EXPORT_KIND_TABLE 1
  67. #define EXPORT_KIND_MEMORY 2
  68. #define EXPORT_KIND_GLOBAL 3
  69. #define LABEL_TYPE_BLOCK 0
  70. #define LABEL_TYPE_LOOP 1
  71. #define LABEL_TYPE_IF 2
  72. #define LABEL_TYPE_FUNCTION 3
  73. typedef struct WASMModule WASMModule;
  74. typedef struct WASMFunction WASMFunction;
  75. typedef struct WASMGlobal WASMGlobal;
  76. typedef union V128 {
  77. int8 i8x16[16];
  78. int16 i16x8[8];
  79. int32 i32x8[4];
  80. int64 i64x2[2];
  81. float32 f32x4[4];
  82. float64 f64x2[2];
  83. } V128;
  84. typedef union WASMValue {
  85. int32 i32;
  86. uint32 u32;
  87. uint32 global_index;
  88. uint32 ref_index;
  89. int64 i64;
  90. uint64 u64;
  91. float32 f32;
  92. float64 f64;
  93. uintptr_t addr;
  94. V128 v128;
  95. } WASMValue;
  96. typedef struct InitializerExpression {
  97. /* type of INIT_EXPR_TYPE_XXX */
  98. /* it actually is instr, in some places, requires constant only */
  99. uint8 init_expr_type;
  100. WASMValue u;
  101. } InitializerExpression;
  102. typedef struct WASMType {
  103. uint16 param_count;
  104. uint16 result_count;
  105. uint16 param_cell_num;
  106. uint16 ret_cell_num;
  107. uint16 ref_count;
  108. #if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT != 0 \
  109. && WASM_ENABLE_LAZY_JIT != 0
  110. /* Code block to call llvm jit functions of this
  111. kind of function type from fast jit jitted code */
  112. void *call_to_llvm_jit_from_fast_jit;
  113. #endif
  114. /* types of params and results */
  115. uint8 types[1];
  116. } WASMType;
  117. typedef struct WASMTable {
  118. uint8 elem_type;
  119. uint32 flags;
  120. uint32 init_size;
  121. /* specified if (flags & 1), else it is 0x10000 */
  122. uint32 max_size;
  123. bool possible_grow;
  124. } WASMTable;
  125. typedef struct WASMMemory {
  126. uint32 flags;
  127. uint32 num_bytes_per_page;
  128. uint32 init_page_count;
  129. uint32 max_page_count;
  130. } WASMMemory;
  131. typedef struct WASMTableImport {
  132. char *module_name;
  133. char *field_name;
  134. uint8 elem_type;
  135. uint32 flags;
  136. uint32 init_size;
  137. /* specified if (flags & 1), else it is 0x10000 */
  138. uint32 max_size;
  139. bool possible_grow;
  140. #if WASM_ENABLE_MULTI_MODULE != 0
  141. WASMModule *import_module;
  142. WASMTable *import_table_linked;
  143. #endif
  144. } WASMTableImport;
  145. typedef struct WASMMemoryImport {
  146. char *module_name;
  147. char *field_name;
  148. uint32 flags;
  149. uint32 num_bytes_per_page;
  150. uint32 init_page_count;
  151. uint32 max_page_count;
  152. #if WASM_ENABLE_MULTI_MODULE != 0
  153. WASMModule *import_module;
  154. WASMMemory *import_memory_linked;
  155. #endif
  156. } WASMMemoryImport;
  157. typedef struct WASMFunctionImport {
  158. char *module_name;
  159. char *field_name;
  160. /* function type */
  161. WASMType *func_type;
  162. /* native function pointer after linked */
  163. void *func_ptr_linked;
  164. /* signature from registered native symbols */
  165. const char *signature;
  166. /* attachment */
  167. void *attachment;
  168. bool call_conv_raw;
  169. #if WASM_ENABLE_MULTI_MODULE != 0
  170. WASMModule *import_module;
  171. WASMFunction *import_func_linked;
  172. #endif
  173. bool call_conv_wasm_c_api;
  174. } WASMFunctionImport;
  175. typedef struct WASMGlobalImport {
  176. char *module_name;
  177. char *field_name;
  178. uint8 type;
  179. bool is_mutable;
  180. /* global data after linked */
  181. WASMValue global_data_linked;
  182. bool is_linked;
  183. #if WASM_ENABLE_MULTI_MODULE != 0
  184. /* imported function pointer after linked */
  185. /* TODO: remove if not needed */
  186. WASMModule *import_module;
  187. WASMGlobal *import_global_linked;
  188. #endif
  189. #if WASM_ENABLE_FAST_JIT != 0
  190. /* The data offset of current global in global data */
  191. uint32 data_offset;
  192. #endif
  193. } WASMGlobalImport;
  194. typedef struct WASMImport {
  195. uint8 kind;
  196. union {
  197. WASMFunctionImport function;
  198. WASMTableImport table;
  199. WASMMemoryImport memory;
  200. WASMGlobalImport global;
  201. struct {
  202. char *module_name;
  203. char *field_name;
  204. } names;
  205. } u;
  206. } WASMImport;
  207. struct WASMFunction {
  208. #if WASM_ENABLE_CUSTOM_NAME_SECTION != 0
  209. char *field_name;
  210. #endif
  211. /* the type of function */
  212. WASMType *func_type;
  213. uint32 local_count;
  214. uint8 *local_types;
  215. /* cell num of parameters */
  216. uint16 param_cell_num;
  217. /* cell num of return type */
  218. uint16 ret_cell_num;
  219. /* cell num of local variables */
  220. uint16 local_cell_num;
  221. /* offset of each local, including function parameters
  222. and local variables */
  223. uint16 *local_offsets;
  224. uint32 max_stack_cell_num;
  225. uint32 max_block_num;
  226. uint32 code_size;
  227. uint8 *code;
  228. #if WASM_ENABLE_FAST_INTERP != 0
  229. uint32 code_compiled_size;
  230. uint8 *code_compiled;
  231. uint8 *consts;
  232. uint32 const_cell_num;
  233. #endif
  234. #if WASM_ENABLE_FAST_JIT != 0 || WASM_ENABLE_JIT != 0 \
  235. || WASM_ENABLE_WAMR_COMPILER != 0
  236. /* Whether function has opcode memory.grow */
  237. bool has_op_memory_grow;
  238. /* Whether function has opcode call or call_indirect */
  239. bool has_op_func_call;
  240. #endif
  241. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  242. /* Whether function has memory operation opcodes */
  243. bool has_memory_operations;
  244. /* Whether function has opcode call_indirect */
  245. bool has_op_call_indirect;
  246. /* Whether function has opcode set_global_aux_stack */
  247. bool has_op_set_global_aux_stack;
  248. #endif
  249. #if WASM_ENABLE_FAST_JIT != 0
  250. /* The compiled fast jit jitted code block of this function */
  251. void *fast_jit_jitted_code;
  252. #if WASM_ENABLE_JIT != 0 && WASM_ENABLE_LAZY_JIT != 0
  253. /* The compiled llvm jit func ptr of this function */
  254. void *llvm_jit_func_ptr;
  255. /* Code block to call fast jit jitted code of this function
  256. from the llvm jit jitted code */
  257. void *call_to_fast_jit_from_llvm_jit;
  258. #endif
  259. #endif
  260. };
  261. struct WASMGlobal {
  262. uint8 type;
  263. bool is_mutable;
  264. InitializerExpression init_expr;
  265. #if WASM_ENABLE_FAST_JIT != 0
  266. /* The data offset of current global in global data */
  267. uint32 data_offset;
  268. #endif
  269. };
  270. typedef struct WASMExport {
  271. char *name;
  272. uint8 kind;
  273. uint32 index;
  274. } WASMExport;
  275. typedef struct WASMTableSeg {
  276. /* 0 to 7 */
  277. uint32 mode;
  278. /* funcref or externref, elemkind will be considered as funcref */
  279. uint32 elem_type;
  280. bool is_dropped;
  281. /* optional, only for active */
  282. uint32 table_index;
  283. InitializerExpression base_offset;
  284. uint32 function_count;
  285. uint32 *func_indexes;
  286. } WASMTableSeg;
  287. typedef struct WASMDataSeg {
  288. uint32 memory_index;
  289. InitializerExpression base_offset;
  290. uint32 data_length;
  291. #if WASM_ENABLE_BULK_MEMORY != 0
  292. bool is_passive;
  293. #endif
  294. uint8 *data;
  295. } WASMDataSeg;
  296. typedef struct BlockAddr {
  297. const uint8 *start_addr;
  298. uint8 *else_addr;
  299. uint8 *end_addr;
  300. } BlockAddr;
  301. #if WASM_ENABLE_LIBC_WASI != 0
  302. typedef struct WASIArguments {
  303. const char **dir_list;
  304. uint32 dir_count;
  305. const char **map_dir_list;
  306. uint32 map_dir_count;
  307. const char **env;
  308. uint32 env_count;
  309. /* in CIDR noation */
  310. const char **addr_pool;
  311. uint32 addr_count;
  312. const char **ns_lookup_pool;
  313. uint32 ns_lookup_count;
  314. char **argv;
  315. uint32 argc;
  316. int stdio[3];
  317. } WASIArguments;
  318. #endif
  319. typedef struct StringNode {
  320. struct StringNode *next;
  321. char *str;
  322. } StringNode, *StringList;
  323. typedef struct BrTableCache {
  324. struct BrTableCache *next;
  325. /* Address of br_table opcode */
  326. uint8 *br_table_op_addr;
  327. uint32 br_count;
  328. uint32 br_depths[1];
  329. } BrTableCache;
  330. #if WASM_ENABLE_DEBUG_INTERP != 0
  331. typedef struct WASMFastOPCodeNode {
  332. struct WASMFastOPCodeNode *next;
  333. uint64 offset;
  334. uint8 orig_op;
  335. } WASMFastOPCodeNode;
  336. #endif
  337. #if WASM_ENABLE_LOAD_CUSTOM_SECTION != 0
  338. typedef struct WASMCustomSection {
  339. struct WASMCustomSection *next;
  340. /* Start address of the section name */
  341. char *name_addr;
  342. /* Length of the section name decoded from leb */
  343. uint32 name_len;
  344. /* Start address of the content (name len and name skipped) */
  345. uint8 *content_addr;
  346. uint32 content_len;
  347. } WASMCustomSection;
  348. #endif
  349. #if WASM_ENABLE_FAST_JIT != 0 || WASM_ENABLE_JIT != 0
  350. struct AOTCompData;
  351. struct AOTCompContext;
  352. /* Orc JIT thread arguments */
  353. typedef struct OrcJitThreadArg {
  354. #if WASM_ENABLE_JIT != 0
  355. struct AOTCompContext *comp_ctx;
  356. #endif
  357. struct WASMModule *module;
  358. uint32 group_idx;
  359. } OrcJitThreadArg;
  360. #endif
  361. struct WASMModuleInstance;
  362. struct WASMModule {
  363. /* Module type, for module loaded from WASM bytecode binary,
  364. this field is Wasm_Module_Bytecode;
  365. for module loaded from AOT file, this field is
  366. Wasm_Module_AoT, and this structure should be treated as
  367. AOTModule structure. */
  368. uint32 module_type;
  369. uint32 type_count;
  370. uint32 import_count;
  371. uint32 function_count;
  372. uint32 table_count;
  373. uint32 memory_count;
  374. uint32 global_count;
  375. uint32 export_count;
  376. uint32 table_seg_count;
  377. /* data seg count read from data segment section */
  378. uint32 data_seg_count;
  379. #if WASM_ENABLE_BULK_MEMORY != 0
  380. /* data count read from datacount section */
  381. uint32 data_seg_count1;
  382. #endif
  383. uint32 import_function_count;
  384. uint32 import_table_count;
  385. uint32 import_memory_count;
  386. uint32 import_global_count;
  387. WASMImport *import_functions;
  388. WASMImport *import_tables;
  389. WASMImport *import_memories;
  390. WASMImport *import_globals;
  391. WASMType **types;
  392. WASMImport *imports;
  393. WASMFunction **functions;
  394. WASMTable *tables;
  395. WASMMemory *memories;
  396. WASMGlobal *globals;
  397. WASMExport *exports;
  398. WASMTableSeg *table_segments;
  399. WASMDataSeg **data_segments;
  400. uint32 start_function;
  401. /* total global variable size */
  402. uint32 global_data_size;
  403. /* the index of auxiliary __data_end global,
  404. -1 means unexported */
  405. uint32 aux_data_end_global_index;
  406. /* auxiliary __data_end exported by wasm app */
  407. uint32 aux_data_end;
  408. /* the index of auxiliary __heap_base global,
  409. -1 means unexported */
  410. uint32 aux_heap_base_global_index;
  411. /* auxiliary __heap_base exported by wasm app */
  412. uint32 aux_heap_base;
  413. /* the index of auxiliary stack top global,
  414. -1 means unexported */
  415. uint32 aux_stack_top_global_index;
  416. /* auxiliary stack bottom resolved */
  417. uint32 aux_stack_bottom;
  418. /* auxiliary stack size resolved */
  419. uint32 aux_stack_size;
  420. /* the index of malloc/free function,
  421. -1 means unexported */
  422. uint32 malloc_function;
  423. uint32 free_function;
  424. /* the index of __retain function,
  425. -1 means unexported */
  426. uint32 retain_function;
  427. /* Whether there is possible memory grow, e.g. memory.grow opcode */
  428. bool possible_memory_grow;
  429. StringList const_str_list;
  430. #if WASM_ENABLE_FAST_INTERP == 0
  431. bh_list br_table_cache_list_head;
  432. bh_list *br_table_cache_list;
  433. #endif
  434. #if WASM_ENABLE_LIBC_WASI != 0
  435. WASIArguments wasi_args;
  436. bool import_wasi_api;
  437. #endif
  438. #if WASM_ENABLE_MULTI_MODULE != 0
  439. /* TODO: add mutex for mutli-thread? */
  440. bh_list import_module_list_head;
  441. bh_list *import_module_list;
  442. #endif
  443. #if WASM_ENABLE_DEBUG_INTERP != 0 || WASM_ENABLE_DEBUG_AOT != 0
  444. bh_list fast_opcode_list;
  445. uint8 *buf_code;
  446. uint64 buf_code_size;
  447. #endif
  448. #if WASM_ENABLE_DEBUG_INTERP != 0 || WASM_ENABLE_DEBUG_AOT != 0 \
  449. || WASM_ENABLE_FAST_JIT != 0
  450. uint8 *load_addr;
  451. uint64 load_size;
  452. #endif
  453. #if WASM_ENABLE_DEBUG_INTERP != 0 \
  454. || (WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT != 0 \
  455. && WASM_ENABLE_LAZY_JIT != 0)
  456. /**
  457. * List of instances referred to this module. When source debugging
  458. * feature is enabled, the debugger may modify the code section of
  459. * the module, so we need to report a warning if user create several
  460. * instances based on the same module.
  461. *
  462. * Also add the instance to the list for Fast JIT to LLVM JIT
  463. * tier-up, since we need to lazily update the LLVM func pointers
  464. * in the instance.
  465. */
  466. struct WASMModuleInstance *instance_list;
  467. korp_mutex instance_list_lock;
  468. #endif
  469. #if WASM_ENABLE_CUSTOM_NAME_SECTION != 0
  470. const uint8 *name_section_buf;
  471. const uint8 *name_section_buf_end;
  472. #endif
  473. #if WASM_ENABLE_LOAD_CUSTOM_SECTION != 0
  474. WASMCustomSection *custom_section_list;
  475. #endif
  476. #if WASM_ENABLE_FAST_JIT != 0
  477. /**
  478. * func pointers of Fast JITed (un-imported) functions
  479. * for non Multi-Tier JIT mode:
  480. * (1) when lazy jit is disabled, each pointer is set to the compiled
  481. * fast jit jitted code
  482. * (2) when lazy jit is enabled, each pointer is firstly inited as
  483. * jit_global->compile_fast_jit_and_then_call, and then set to the
  484. * compiled fast jit jitted code when it is called (the stub will
  485. * compile the jit function and then update itself)
  486. * for Multi-Tier JIT mode:
  487. * each pointer is firstly inited as compile_fast_jit_and_then_call,
  488. * and then set to the compiled fast jit jitted code when it is called,
  489. * and when the llvm jit func ptr of the same function is compiled, it
  490. * will be set to call_to_llvm_jit_from_fast_jit of this function type
  491. * (tier-up from fast-jit to llvm-jit)
  492. */
  493. void **fast_jit_func_ptrs;
  494. /* locks for Fast JIT lazy compilation */
  495. korp_mutex fast_jit_thread_locks[WASM_ORC_JIT_BACKEND_THREAD_NUM];
  496. bool fast_jit_thread_locks_inited[WASM_ORC_JIT_BACKEND_THREAD_NUM];
  497. #endif
  498. #if WASM_ENABLE_JIT != 0
  499. struct AOTCompData *comp_data;
  500. struct AOTCompContext *comp_ctx;
  501. /**
  502. * func pointers of LLVM JITed (un-imported) functions
  503. * for non Multi-Tier JIT mode:
  504. * each pointer is set to the lookuped llvm jit func ptr, note that it
  505. * is a stub and will trigger the actual compilation when it is called
  506. * for Multi-Tier JIT mode:
  507. * each pointer is inited as call_to_fast_jit code block, when the llvm
  508. * jit func ptr is actually compiled, it is set to the compiled llvm jit
  509. * func ptr
  510. */
  511. void **func_ptrs;
  512. /* whether the func pointers are compiled */
  513. bool *func_ptrs_compiled;
  514. #endif
  515. #if WASM_ENABLE_FAST_JIT != 0 || WASM_ENABLE_JIT != 0
  516. /* backend compilation threads */
  517. korp_tid orcjit_threads[WASM_ORC_JIT_BACKEND_THREAD_NUM];
  518. /* backend thread arguments */
  519. OrcJitThreadArg orcjit_thread_args[WASM_ORC_JIT_BACKEND_THREAD_NUM];
  520. /* whether to stop the compilation of backend threads */
  521. bool orcjit_stop_compiling;
  522. #endif
  523. #if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT != 0 \
  524. && WASM_ENABLE_LAZY_JIT != 0
  525. /* wait lock/cond for the synchronization of
  526. the llvm jit initialization */
  527. korp_mutex tierup_wait_lock;
  528. korp_cond tierup_wait_cond;
  529. bool tierup_wait_lock_inited;
  530. korp_tid llvm_jit_init_thread;
  531. /* whether the llvm jit is initialized */
  532. bool llvm_jit_inited;
  533. /* Whether to enable llvm jit compilation:
  534. it is set to true only when there is a module instance starts to
  535. run with running mode Mode_LLVM_JIT or Mode_Multi_Tier_JIT,
  536. since no need to enable llvm jit compilation for Mode_Interp and
  537. Mode_Fast_JIT, so as to improve performance for them */
  538. bool enable_llvm_jit_compilation;
  539. /* The count of groups which finish compiling the fast jit
  540. functions in that group */
  541. uint32 fast_jit_ready_groups;
  542. #endif
  543. };
  544. typedef struct BlockType {
  545. /* Block type may be expressed in one of two forms:
  546. * either by the type of the single return value or
  547. * by a type index of module.
  548. */
  549. union {
  550. uint8 value_type;
  551. WASMType *type;
  552. } u;
  553. bool is_value_type;
  554. } BlockType;
  555. typedef struct WASMBranchBlock {
  556. uint8 *begin_addr;
  557. uint8 *target_addr;
  558. uint32 *frame_sp;
  559. uint32 cell_num;
  560. } WASMBranchBlock;
  561. /**
  562. * Align an unsigned value on a alignment boundary.
  563. *
  564. * @param v the value to be aligned
  565. * @param b the alignment boundary (2, 4, 8, ...)
  566. *
  567. * @return the aligned value
  568. */
  569. inline static unsigned
  570. align_uint(unsigned v, unsigned b)
  571. {
  572. unsigned m = b - 1;
  573. return (v + m) & ~m;
  574. }
  575. /**
  576. * Check whether a piece of data is out of range
  577. *
  578. * @param offset the offset that the data starts
  579. * @param len the length of the data
  580. * @param max_size the maximum size of the data range
  581. *
  582. * @return true if out of range, false otherwise
  583. */
  584. inline static bool
  585. offset_len_out_of_bounds(uint32 offset, uint32 len, uint32 max_size)
  586. {
  587. if (offset + len < offset /* integer overflow */
  588. || offset + len > max_size)
  589. return true;
  590. return false;
  591. }
  592. /**
  593. * Return the hash value of c string.
  594. */
  595. inline static uint32
  596. wasm_string_hash(const char *str)
  597. {
  598. unsigned h = (unsigned)strlen(str);
  599. const uint8 *p = (uint8 *)str;
  600. const uint8 *end = p + h;
  601. while (p != end)
  602. h = ((h << 5) - h) + *p++;
  603. return h;
  604. }
  605. /**
  606. * Whether two c strings are equal.
  607. */
  608. inline static bool
  609. wasm_string_equal(const char *s1, const char *s2)
  610. {
  611. return strcmp(s1, s2) == 0 ? true : false;
  612. }
  613. /**
  614. * Return the byte size of value type.
  615. *
  616. */
  617. inline static uint32
  618. wasm_value_type_size(uint8 value_type)
  619. {
  620. switch (value_type) {
  621. case VALUE_TYPE_I32:
  622. case VALUE_TYPE_F32:
  623. #if WASM_ENABLE_REF_TYPES != 0
  624. case VALUE_TYPE_FUNCREF:
  625. case VALUE_TYPE_EXTERNREF:
  626. #endif
  627. return sizeof(int32);
  628. case VALUE_TYPE_I64:
  629. case VALUE_TYPE_F64:
  630. return sizeof(int64);
  631. #if WASM_ENABLE_SIMD != 0
  632. case VALUE_TYPE_V128:
  633. return sizeof(int64) * 2;
  634. #endif
  635. case VALUE_TYPE_VOID:
  636. return 0;
  637. default:
  638. bh_assert(0);
  639. }
  640. return 0;
  641. }
  642. inline static uint16
  643. wasm_value_type_cell_num(uint8 value_type)
  644. {
  645. return wasm_value_type_size(value_type) / 4;
  646. }
  647. inline static uint32
  648. wasm_get_cell_num(const uint8 *types, uint32 type_count)
  649. {
  650. uint32 cell_num = 0;
  651. uint32 i;
  652. for (i = 0; i < type_count; i++)
  653. cell_num += wasm_value_type_cell_num(types[i]);
  654. return cell_num;
  655. }
  656. #if WASM_ENABLE_REF_TYPES != 0
  657. inline static uint16
  658. wasm_value_type_cell_num_outside(uint8 value_type)
  659. {
  660. if (VALUE_TYPE_EXTERNREF == value_type) {
  661. return sizeof(uintptr_t) / sizeof(uint32);
  662. }
  663. else {
  664. return wasm_value_type_cell_num(value_type);
  665. }
  666. }
  667. #endif
  668. inline static bool
  669. wasm_type_equal(const WASMType *type1, const WASMType *type2)
  670. {
  671. if (type1 == type2) {
  672. return true;
  673. }
  674. return (type1->param_count == type2->param_count
  675. && type1->result_count == type2->result_count
  676. && memcmp(type1->types, type2->types,
  677. (uint32)(type1->param_count + type1->result_count))
  678. == 0)
  679. ? true
  680. : false;
  681. }
  682. inline static uint32
  683. wasm_get_smallest_type_idx(WASMType **types, uint32 type_count,
  684. uint32 cur_type_idx)
  685. {
  686. uint32 i;
  687. for (i = 0; i < cur_type_idx; i++) {
  688. if (wasm_type_equal(types[cur_type_idx], types[i]))
  689. return i;
  690. }
  691. (void)type_count;
  692. return cur_type_idx;
  693. }
  694. static inline uint32
  695. block_type_get_param_types(BlockType *block_type, uint8 **p_param_types)
  696. {
  697. uint32 param_count = 0;
  698. if (!block_type->is_value_type) {
  699. WASMType *wasm_type = block_type->u.type;
  700. *p_param_types = wasm_type->types;
  701. param_count = wasm_type->param_count;
  702. }
  703. else {
  704. *p_param_types = NULL;
  705. param_count = 0;
  706. }
  707. return param_count;
  708. }
  709. static inline uint32
  710. block_type_get_result_types(BlockType *block_type, uint8 **p_result_types)
  711. {
  712. uint32 result_count = 0;
  713. if (block_type->is_value_type) {
  714. if (block_type->u.value_type != VALUE_TYPE_VOID) {
  715. *p_result_types = &block_type->u.value_type;
  716. result_count = 1;
  717. }
  718. }
  719. else {
  720. WASMType *wasm_type = block_type->u.type;
  721. *p_result_types = wasm_type->types + wasm_type->param_count;
  722. result_count = wasm_type->result_count;
  723. }
  724. return result_count;
  725. }
  726. #ifdef __cplusplus
  727. } /* end of extern "C" */
  728. #endif
  729. #endif /* end of _WASM_H_ */