wasm.h 23 KB

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