wasm.h 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813
  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. /* optional, only for active */
  281. uint32 table_index;
  282. InitializerExpression base_offset;
  283. uint32 function_count;
  284. uint32 *func_indexes;
  285. } WASMTableSeg;
  286. typedef struct WASMDataSeg {
  287. uint32 memory_index;
  288. InitializerExpression base_offset;
  289. uint32 data_length;
  290. #if WASM_ENABLE_BULK_MEMORY != 0
  291. bool is_passive;
  292. #endif
  293. uint8 *data;
  294. } WASMDataSeg;
  295. typedef struct BlockAddr {
  296. const uint8 *start_addr;
  297. uint8 *else_addr;
  298. uint8 *end_addr;
  299. } BlockAddr;
  300. #if WASM_ENABLE_LIBC_WASI != 0
  301. typedef struct WASIArguments {
  302. const char **dir_list;
  303. uint32 dir_count;
  304. const char **map_dir_list;
  305. uint32 map_dir_count;
  306. const char **env;
  307. uint32 env_count;
  308. /* in CIDR noation */
  309. const char **addr_pool;
  310. uint32 addr_count;
  311. const char **ns_lookup_pool;
  312. uint32 ns_lookup_count;
  313. char **argv;
  314. uint32 argc;
  315. os_raw_file_handle stdio[3];
  316. } WASIArguments;
  317. #endif
  318. typedef struct StringNode {
  319. struct StringNode *next;
  320. char *str;
  321. } StringNode, *StringList;
  322. typedef struct BrTableCache {
  323. struct BrTableCache *next;
  324. /* Address of br_table opcode */
  325. uint8 *br_table_op_addr;
  326. uint32 br_count;
  327. uint32 br_depths[1];
  328. } BrTableCache;
  329. #if WASM_ENABLE_DEBUG_INTERP != 0
  330. typedef struct WASMFastOPCodeNode {
  331. struct WASMFastOPCodeNode *next;
  332. uint64 offset;
  333. uint8 orig_op;
  334. } WASMFastOPCodeNode;
  335. #endif
  336. #if WASM_ENABLE_LOAD_CUSTOM_SECTION != 0
  337. typedef struct WASMCustomSection {
  338. struct WASMCustomSection *next;
  339. /* Start address of the section name */
  340. char *name_addr;
  341. /* Length of the section name decoded from leb */
  342. uint32 name_len;
  343. /* Start address of the content (name len and name skipped) */
  344. uint8 *content_addr;
  345. uint32 content_len;
  346. } WASMCustomSection;
  347. #endif
  348. #if WASM_ENABLE_FAST_JIT != 0 || WASM_ENABLE_JIT != 0
  349. struct AOTCompData;
  350. struct AOTCompContext;
  351. /* Orc JIT thread arguments */
  352. typedef struct OrcJitThreadArg {
  353. #if WASM_ENABLE_JIT != 0
  354. struct AOTCompContext *comp_ctx;
  355. #endif
  356. struct WASMModule *module;
  357. uint32 group_idx;
  358. } OrcJitThreadArg;
  359. #endif
  360. struct WASMModuleInstance;
  361. struct WASMModule {
  362. /* Module type, for module loaded from WASM bytecode binary,
  363. this field is Wasm_Module_Bytecode;
  364. for module loaded from AOT file, this field is
  365. Wasm_Module_AoT, and this structure should be treated as
  366. AOTModule structure. */
  367. uint32 module_type;
  368. uint32 type_count;
  369. uint32 import_count;
  370. uint32 function_count;
  371. uint32 table_count;
  372. uint32 memory_count;
  373. uint32 global_count;
  374. uint32 export_count;
  375. uint32 table_seg_count;
  376. /* data seg count read from data segment section */
  377. uint32 data_seg_count;
  378. #if WASM_ENABLE_BULK_MEMORY != 0
  379. /* data count read from datacount section */
  380. uint32 data_seg_count1;
  381. #endif
  382. uint32 import_function_count;
  383. uint32 import_table_count;
  384. uint32 import_memory_count;
  385. uint32 import_global_count;
  386. WASMImport *import_functions;
  387. WASMImport *import_tables;
  388. WASMImport *import_memories;
  389. WASMImport *import_globals;
  390. WASMType **types;
  391. WASMImport *imports;
  392. WASMFunction **functions;
  393. WASMTable *tables;
  394. WASMMemory *memories;
  395. WASMGlobal *globals;
  396. WASMExport *exports;
  397. WASMTableSeg *table_segments;
  398. WASMDataSeg **data_segments;
  399. uint32 start_function;
  400. /* total global variable size */
  401. uint32 global_data_size;
  402. /* the index of auxiliary __data_end global,
  403. -1 means unexported */
  404. uint32 aux_data_end_global_index;
  405. /* auxiliary __data_end exported by wasm app */
  406. uint32 aux_data_end;
  407. /* the index of auxiliary __heap_base global,
  408. -1 means unexported */
  409. uint32 aux_heap_base_global_index;
  410. /* auxiliary __heap_base exported by wasm app */
  411. uint32 aux_heap_base;
  412. /* the index of auxiliary stack top global,
  413. -1 means unexported */
  414. uint32 aux_stack_top_global_index;
  415. /* auxiliary stack bottom resolved */
  416. uint32 aux_stack_bottom;
  417. /* auxiliary stack size resolved */
  418. uint32 aux_stack_size;
  419. /* the index of malloc/free function,
  420. -1 means unexported */
  421. uint32 malloc_function;
  422. uint32 free_function;
  423. /* the index of __retain function,
  424. -1 means unexported */
  425. uint32 retain_function;
  426. /* Whether there is possible memory grow, e.g. memory.grow opcode */
  427. bool possible_memory_grow;
  428. StringList const_str_list;
  429. #if WASM_ENABLE_FAST_INTERP == 0
  430. bh_list br_table_cache_list_head;
  431. bh_list *br_table_cache_list;
  432. #endif
  433. #if WASM_ENABLE_LIBC_WASI != 0
  434. WASIArguments wasi_args;
  435. bool import_wasi_api;
  436. #endif
  437. #if WASM_ENABLE_MULTI_MODULE != 0
  438. /* TODO: add mutex for mutli-thread? */
  439. bh_list import_module_list_head;
  440. bh_list *import_module_list;
  441. #endif
  442. #if WASM_ENABLE_DEBUG_INTERP != 0 || WASM_ENABLE_DEBUG_AOT != 0
  443. bh_list fast_opcode_list;
  444. uint8 *buf_code;
  445. uint64 buf_code_size;
  446. #endif
  447. #if WASM_ENABLE_DEBUG_INTERP != 0 || WASM_ENABLE_DEBUG_AOT != 0 \
  448. || WASM_ENABLE_FAST_JIT != 0
  449. uint8 *load_addr;
  450. uint64 load_size;
  451. #endif
  452. #if WASM_ENABLE_DEBUG_INTERP != 0 \
  453. || (WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT != 0 \
  454. && WASM_ENABLE_LAZY_JIT != 0)
  455. /**
  456. * List of instances referred to this module. When source debugging
  457. * feature is enabled, the debugger may modify the code section of
  458. * the module, so we need to report a warning if user create several
  459. * instances based on the same module.
  460. *
  461. * Also add the instance to the list for Fast JIT to LLVM JIT
  462. * tier-up, since we need to lazily update the LLVM func pointers
  463. * in the instance.
  464. */
  465. struct WASMModuleInstance *instance_list;
  466. korp_mutex instance_list_lock;
  467. #endif
  468. #if WASM_ENABLE_CUSTOM_NAME_SECTION != 0
  469. const uint8 *name_section_buf;
  470. const uint8 *name_section_buf_end;
  471. #endif
  472. #if WASM_ENABLE_LOAD_CUSTOM_SECTION != 0
  473. WASMCustomSection *custom_section_list;
  474. #endif
  475. #if WASM_ENABLE_FAST_JIT != 0
  476. /**
  477. * func pointers of Fast JITed (un-imported) functions
  478. * for non Multi-Tier JIT mode:
  479. * (1) when lazy jit is disabled, each pointer is set to the compiled
  480. * fast jit jitted code
  481. * (2) when lazy jit is enabled, each pointer is firstly inited as
  482. * jit_global->compile_fast_jit_and_then_call, and then set to the
  483. * compiled fast jit jitted code when it is called (the stub will
  484. * compile the jit function and then update itself)
  485. * for Multi-Tier JIT mode:
  486. * each pointer is firstly inited as compile_fast_jit_and_then_call,
  487. * and then set to the compiled fast jit jitted code when it is called,
  488. * and when the llvm jit func ptr of the same function is compiled, it
  489. * will be set to call_to_llvm_jit_from_fast_jit of this function type
  490. * (tier-up from fast-jit to llvm-jit)
  491. */
  492. void **fast_jit_func_ptrs;
  493. /* locks for Fast JIT lazy compilation */
  494. korp_mutex fast_jit_thread_locks[WASM_ORC_JIT_BACKEND_THREAD_NUM];
  495. bool fast_jit_thread_locks_inited[WASM_ORC_JIT_BACKEND_THREAD_NUM];
  496. #endif
  497. #if WASM_ENABLE_JIT != 0
  498. struct AOTCompData *comp_data;
  499. struct AOTCompContext *comp_ctx;
  500. /**
  501. * func pointers of LLVM JITed (un-imported) functions
  502. * for non Multi-Tier JIT mode:
  503. * each pointer is set to the lookuped llvm jit func ptr, note that it
  504. * is a stub and will trigger the actual compilation when it is called
  505. * for Multi-Tier JIT mode:
  506. * each pointer is inited as call_to_fast_jit code block, when the llvm
  507. * jit func ptr is actually compiled, it is set to the compiled llvm jit
  508. * func ptr
  509. */
  510. void **func_ptrs;
  511. /* whether the func pointers are compiled */
  512. bool *func_ptrs_compiled;
  513. #endif
  514. #if WASM_ENABLE_FAST_JIT != 0 || WASM_ENABLE_JIT != 0
  515. /* backend compilation threads */
  516. korp_tid orcjit_threads[WASM_ORC_JIT_BACKEND_THREAD_NUM];
  517. /* backend thread arguments */
  518. OrcJitThreadArg orcjit_thread_args[WASM_ORC_JIT_BACKEND_THREAD_NUM];
  519. /* whether to stop the compilation of backend threads */
  520. bool orcjit_stop_compiling;
  521. #endif
  522. #if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT != 0 \
  523. && WASM_ENABLE_LAZY_JIT != 0
  524. /* wait lock/cond for the synchronization of
  525. the llvm jit initialization */
  526. korp_mutex tierup_wait_lock;
  527. korp_cond tierup_wait_cond;
  528. bool tierup_wait_lock_inited;
  529. korp_tid llvm_jit_init_thread;
  530. /* whether the llvm jit is initialized */
  531. bool llvm_jit_inited;
  532. /* Whether to enable llvm jit compilation:
  533. it is set to true only when there is a module instance starts to
  534. run with running mode Mode_LLVM_JIT or Mode_Multi_Tier_JIT,
  535. since no need to enable llvm jit compilation for Mode_Interp and
  536. Mode_Fast_JIT, so as to improve performance for them */
  537. bool enable_llvm_jit_compilation;
  538. /* The count of groups which finish compiling the fast jit
  539. functions in that group */
  540. uint32 fast_jit_ready_groups;
  541. #endif
  542. };
  543. typedef struct BlockType {
  544. /* Block type may be expressed in one of two forms:
  545. * either by the type of the single return value or
  546. * by a type index of module.
  547. */
  548. union {
  549. uint8 value_type;
  550. WASMType *type;
  551. } u;
  552. bool is_value_type;
  553. } BlockType;
  554. typedef struct WASMBranchBlock {
  555. uint8 *begin_addr;
  556. uint8 *target_addr;
  557. uint32 *frame_sp;
  558. uint32 cell_num;
  559. } WASMBranchBlock;
  560. /**
  561. * Align an unsigned value on a alignment boundary.
  562. *
  563. * @param v the value to be aligned
  564. * @param b the alignment boundary (2, 4, 8, ...)
  565. *
  566. * @return the aligned value
  567. */
  568. inline static unsigned
  569. align_uint(unsigned v, unsigned b)
  570. {
  571. unsigned m = b - 1;
  572. return (v + m) & ~m;
  573. }
  574. /**
  575. * Check whether a piece of data is out of range
  576. *
  577. * @param offset the offset that the data starts
  578. * @param len the length of the data
  579. * @param max_size the maximum size of the data range
  580. *
  581. * @return true if out of range, false otherwise
  582. */
  583. inline static bool
  584. offset_len_out_of_bounds(uint32 offset, uint32 len, uint32 max_size)
  585. {
  586. if (offset + len < offset /* integer overflow */
  587. || offset + len > max_size)
  588. return true;
  589. return false;
  590. }
  591. /**
  592. * Return the hash value of c string.
  593. */
  594. inline static uint32
  595. wasm_string_hash(const char *str)
  596. {
  597. unsigned h = (unsigned)strlen(str);
  598. const uint8 *p = (uint8 *)str;
  599. const uint8 *end = p + h;
  600. while (p != end)
  601. h = ((h << 5) - h) + *p++;
  602. return h;
  603. }
  604. /**
  605. * Whether two c strings are equal.
  606. */
  607. inline static bool
  608. wasm_string_equal(const char *s1, const char *s2)
  609. {
  610. return strcmp(s1, s2) == 0 ? true : false;
  611. }
  612. /**
  613. * Return the byte size of value type.
  614. *
  615. */
  616. inline static uint32
  617. wasm_value_type_size(uint8 value_type)
  618. {
  619. switch (value_type) {
  620. case VALUE_TYPE_I32:
  621. case VALUE_TYPE_F32:
  622. #if WASM_ENABLE_REF_TYPES != 0
  623. case VALUE_TYPE_FUNCREF:
  624. case VALUE_TYPE_EXTERNREF:
  625. #endif
  626. return sizeof(int32);
  627. case VALUE_TYPE_I64:
  628. case VALUE_TYPE_F64:
  629. return sizeof(int64);
  630. #if WASM_ENABLE_SIMD != 0
  631. case VALUE_TYPE_V128:
  632. return sizeof(int64) * 2;
  633. #endif
  634. case VALUE_TYPE_VOID:
  635. return 0;
  636. default:
  637. bh_assert(0);
  638. }
  639. return 0;
  640. }
  641. inline static uint16
  642. wasm_value_type_cell_num(uint8 value_type)
  643. {
  644. return wasm_value_type_size(value_type) / 4;
  645. }
  646. inline static uint32
  647. wasm_get_cell_num(const uint8 *types, uint32 type_count)
  648. {
  649. uint32 cell_num = 0;
  650. uint32 i;
  651. for (i = 0; i < type_count; i++)
  652. cell_num += wasm_value_type_cell_num(types[i]);
  653. return cell_num;
  654. }
  655. #if WASM_ENABLE_REF_TYPES != 0
  656. inline static uint16
  657. wasm_value_type_cell_num_outside(uint8 value_type)
  658. {
  659. if (VALUE_TYPE_EXTERNREF == value_type) {
  660. return sizeof(uintptr_t) / sizeof(uint32);
  661. }
  662. else {
  663. return wasm_value_type_cell_num(value_type);
  664. }
  665. }
  666. #endif
  667. inline static bool
  668. wasm_type_equal(const WASMType *type1, const WASMType *type2)
  669. {
  670. if (type1 == type2) {
  671. return true;
  672. }
  673. return (type1->param_count == type2->param_count
  674. && type1->result_count == type2->result_count
  675. && memcmp(type1->types, type2->types,
  676. (uint32)(type1->param_count + type1->result_count))
  677. == 0)
  678. ? true
  679. : false;
  680. }
  681. inline static uint32
  682. wasm_get_smallest_type_idx(WASMType **types, uint32 type_count,
  683. uint32 cur_type_idx)
  684. {
  685. uint32 i;
  686. for (i = 0; i < cur_type_idx; i++) {
  687. if (wasm_type_equal(types[cur_type_idx], types[i]))
  688. return i;
  689. }
  690. (void)type_count;
  691. return cur_type_idx;
  692. }
  693. static inline uint32
  694. block_type_get_param_types(BlockType *block_type, uint8 **p_param_types)
  695. {
  696. uint32 param_count = 0;
  697. if (!block_type->is_value_type) {
  698. WASMType *wasm_type = block_type->u.type;
  699. *p_param_types = wasm_type->types;
  700. param_count = wasm_type->param_count;
  701. }
  702. else {
  703. *p_param_types = NULL;
  704. param_count = 0;
  705. }
  706. return param_count;
  707. }
  708. static inline uint32
  709. block_type_get_result_types(BlockType *block_type, uint8 **p_result_types)
  710. {
  711. uint32 result_count = 0;
  712. if (block_type->is_value_type) {
  713. if (block_type->u.value_type != VALUE_TYPE_VOID) {
  714. *p_result_types = &block_type->u.value_type;
  715. result_count = 1;
  716. }
  717. }
  718. else {
  719. WASMType *wasm_type = block_type->u.type;
  720. *p_result_types = wasm_type->types + wasm_type->param_count;
  721. result_count = wasm_type->result_count;
  722. }
  723. return result_count;
  724. }
  725. #ifdef __cplusplus
  726. } /* end of extern "C" */
  727. #endif
  728. #endif /* end of _WASM_H_ */