wasm.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561
  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_VOID 0x40
  20. /* Used by AOT */
  21. #define VALUE_TYPE_I1 0x41
  22. /* Used by loader to represent any type of i32/i64/f32/f64 */
  23. #define VALUE_TYPE_ANY 0x42
  24. /* Table Element Type */
  25. #define TABLE_ELEM_TYPE_ANY_FUNC 0x70
  26. #define DEFAULT_NUM_BYTES_PER_PAGE 65536
  27. #define INIT_EXPR_TYPE_I32_CONST 0x41
  28. #define INIT_EXPR_TYPE_I64_CONST 0x42
  29. #define INIT_EXPR_TYPE_F32_CONST 0x43
  30. #define INIT_EXPR_TYPE_F64_CONST 0x44
  31. #define INIT_EXPR_TYPE_V128_CONST 0xFD
  32. #define INIT_EXPR_TYPE_GET_GLOBAL 0x23
  33. #define INIT_EXPR_TYPE_ERROR 0xff
  34. #define WASM_MAGIC_NUMBER 0x6d736100
  35. #define WASM_CURRENT_VERSION 1
  36. #define SECTION_TYPE_USER 0
  37. #define SECTION_TYPE_TYPE 1
  38. #define SECTION_TYPE_IMPORT 2
  39. #define SECTION_TYPE_FUNC 3
  40. #define SECTION_TYPE_TABLE 4
  41. #define SECTION_TYPE_MEMORY 5
  42. #define SECTION_TYPE_GLOBAL 6
  43. #define SECTION_TYPE_EXPORT 7
  44. #define SECTION_TYPE_START 8
  45. #define SECTION_TYPE_ELEM 9
  46. #define SECTION_TYPE_CODE 10
  47. #define SECTION_TYPE_DATA 11
  48. #if WASM_ENABLE_BULK_MEMORY != 0
  49. #define SECTION_TYPE_DATACOUNT 12
  50. #endif
  51. #define SUB_SECTION_TYPE_MODULE 0
  52. #define SUB_SECTION_TYPE_FUNC 1
  53. #define SUB_SECTION_TYPE_LOCAL 2
  54. #define IMPORT_KIND_FUNC 0
  55. #define IMPORT_KIND_TABLE 1
  56. #define IMPORT_KIND_MEMORY 2
  57. #define IMPORT_KIND_GLOBAL 3
  58. #define EXPORT_KIND_FUNC 0
  59. #define EXPORT_KIND_TABLE 1
  60. #define EXPORT_KIND_MEMORY 2
  61. #define EXPORT_KIND_GLOBAL 3
  62. #define LABEL_TYPE_BLOCK 0
  63. #define LABEL_TYPE_LOOP 1
  64. #define LABEL_TYPE_IF 2
  65. #define LABEL_TYPE_FUNCTION 3
  66. typedef struct WASMModule WASMModule;
  67. typedef struct WASMFunction WASMFunction;
  68. typedef struct WASMGlobal WASMGlobal;
  69. typedef union V128 {
  70. int8 i8x16[16];
  71. int16 i16x8[8];
  72. int32 i32x8[4];
  73. int64 i64x2[2];
  74. float32 f32x4[4];
  75. float64 f64x2[2];
  76. } V128;
  77. typedef union WASMValue {
  78. int32 i32;
  79. uint32 u32;
  80. int64 i64;
  81. uint64 u64;
  82. float32 f32;
  83. float64 f64;
  84. uintptr_t addr;
  85. V128 v128;
  86. } WASMValue;
  87. typedef struct InitializerExpression {
  88. /* type of INIT_EXPR_TYPE_XXX */
  89. uint8 init_expr_type;
  90. union {
  91. int32 i32;
  92. int64 i64;
  93. float32 f32;
  94. float64 f64;
  95. uint32 global_index;
  96. V128 v128;
  97. } u;
  98. } InitializerExpression;
  99. typedef struct WASMType {
  100. uint16 param_count;
  101. uint16 result_count;
  102. uint16 param_cell_num;
  103. uint16 ret_cell_num;
  104. /* types of params and results */
  105. uint8 types[1];
  106. } WASMType;
  107. typedef struct WASMTable {
  108. uint8 elem_type;
  109. uint32 flags;
  110. uint32 init_size;
  111. /* specified if (flags & 1), else it is 0x10000 */
  112. uint32 max_size;
  113. } WASMTable;
  114. typedef struct WASMMemory {
  115. uint32 flags;
  116. uint32 num_bytes_per_page;
  117. uint32 init_page_count;
  118. uint32 max_page_count;
  119. } WASMMemory;
  120. typedef struct WASMTableImport {
  121. char *module_name;
  122. char *field_name;
  123. uint8 elem_type;
  124. uint32 flags;
  125. uint32 init_size;
  126. /* specified if (flags & 1), else it is 0x10000 */
  127. uint32 max_size;
  128. #if WASM_ENABLE_MULTI_MODULE != 0
  129. WASMModule *import_module;
  130. WASMTable *import_table_linked;
  131. #endif
  132. } WASMTableImport;
  133. typedef struct WASMMemoryImport {
  134. char *module_name;
  135. char *field_name;
  136. uint32 flags;
  137. uint32 num_bytes_per_page;
  138. uint32 init_page_count;
  139. uint32 max_page_count;
  140. #if WASM_ENABLE_MULTI_MODULE != 0
  141. WASMModule *import_module;
  142. WASMMemory *import_memory_linked;
  143. #endif
  144. } WASMMemoryImport;
  145. typedef struct WASMFunctionImport {
  146. char *module_name;
  147. char *field_name;
  148. /* function type */
  149. WASMType *func_type;
  150. /* native function pointer after linked */
  151. void *func_ptr_linked;
  152. /* signature from registered native symbols */
  153. const char *signature;
  154. /* attachment */
  155. void *attachment;
  156. bool call_conv_raw;
  157. #if WASM_ENABLE_MULTI_MODULE != 0
  158. WASMModule *import_module;
  159. WASMFunction *import_func_linked;
  160. #endif
  161. } WASMFunctionImport;
  162. typedef struct WASMGlobalImport {
  163. char *module_name;
  164. char *field_name;
  165. uint8 type;
  166. bool is_mutable;
  167. /* global data after linked */
  168. WASMValue global_data_linked;
  169. bool is_linked;
  170. #if WASM_ENABLE_MULTI_MODULE != 0
  171. /* imported function pointer after linked */
  172. /* TODO: remove if not needed */
  173. WASMModule *import_module;
  174. WASMGlobal *import_global_linked;
  175. #endif
  176. } WASMGlobalImport;
  177. typedef struct WASMImport {
  178. uint8 kind;
  179. union {
  180. WASMFunctionImport function;
  181. WASMTableImport table;
  182. WASMMemoryImport memory;
  183. WASMGlobalImport global;
  184. struct {
  185. char *module_name;
  186. char *field_name;
  187. } names;
  188. } u;
  189. } WASMImport;
  190. struct WASMFunction {
  191. #if WASM_ENABLE_CUSTOM_NAME_SECTION != 0
  192. char *field_name;
  193. #endif
  194. /* the type of function */
  195. WASMType *func_type;
  196. uint32 local_count;
  197. uint8 *local_types;
  198. /* cell num of parameters */
  199. uint16 param_cell_num;
  200. /* cell num of return type */
  201. uint16 ret_cell_num;
  202. /* cell num of local variables */
  203. uint16 local_cell_num;
  204. /* offset of each local, including function parameters
  205. and local variables */
  206. uint16 *local_offsets;
  207. uint32 max_stack_cell_num;
  208. uint32 max_block_num;
  209. /* Whether function has opcode memory.grow */
  210. bool has_op_memory_grow;
  211. /* Whether function has opcode call or
  212. call_indirect */
  213. bool has_op_func_call;
  214. uint32 code_size;
  215. uint8 *code;
  216. #if WASM_ENABLE_FAST_INTERP != 0
  217. uint32 code_compiled_size;
  218. uint8 *code_compiled;
  219. uint8 *consts;
  220. uint32 const_cell_num;
  221. #endif
  222. };
  223. struct WASMGlobal {
  224. uint8 type;
  225. bool is_mutable;
  226. InitializerExpression init_expr;
  227. };
  228. typedef struct WASMExport {
  229. char *name;
  230. uint8 kind;
  231. uint32 index;
  232. } WASMExport;
  233. typedef struct WASMTableSeg {
  234. uint32 table_index;
  235. InitializerExpression base_offset;
  236. uint32 function_count;
  237. uint32 *func_indexes;
  238. } WASMTableSeg;
  239. typedef struct WASMDataSeg {
  240. uint32 memory_index;
  241. InitializerExpression base_offset;
  242. uint32 data_length;
  243. #if WASM_ENABLE_BULK_MEMORY != 0
  244. bool is_passive;
  245. #endif
  246. uint8 *data;
  247. } WASMDataSeg;
  248. typedef struct BlockAddr {
  249. const uint8 *start_addr;
  250. uint8 *else_addr;
  251. uint8 *end_addr;
  252. } BlockAddr;
  253. #if WASM_ENABLE_LIBC_WASI != 0
  254. typedef struct WASIArguments {
  255. const char **dir_list;
  256. uint32 dir_count;
  257. const char **map_dir_list;
  258. uint32 map_dir_count;
  259. const char **env;
  260. uint32 env_count;
  261. char **argv;
  262. uint32 argc;
  263. } WASIArguments;
  264. #endif
  265. typedef struct StringNode {
  266. struct StringNode *next;
  267. char *str;
  268. } StringNode, *StringList;
  269. struct WASMModule {
  270. /* Module type, for module loaded from WASM bytecode binary,
  271. this field is Wasm_Module_Bytecode;
  272. for module loaded from AOT file, this field is
  273. Wasm_Module_AoT, and this structure should be treated as
  274. AOTModule structure. */
  275. uint32 module_type;
  276. uint32 type_count;
  277. uint32 import_count;
  278. uint32 function_count;
  279. uint32 table_count;
  280. uint32 memory_count;
  281. uint32 global_count;
  282. uint32 export_count;
  283. uint32 table_seg_count;
  284. /* data seg count read from data segment section */
  285. uint32 data_seg_count;
  286. #if WASM_ENABLE_BULK_MEMORY != 0
  287. /* data count read from datacount section */
  288. uint32 data_seg_count1;
  289. #endif
  290. uint32 import_function_count;
  291. uint32 import_table_count;
  292. uint32 import_memory_count;
  293. uint32 import_global_count;
  294. WASMImport *import_functions;
  295. WASMImport *import_tables;
  296. WASMImport *import_memories;
  297. WASMImport *import_globals;
  298. WASMType **types;
  299. WASMImport *imports;
  300. WASMFunction **functions;
  301. WASMTable *tables;
  302. WASMMemory *memories;
  303. WASMGlobal *globals;
  304. WASMExport *exports;
  305. WASMTableSeg *table_segments;
  306. WASMDataSeg **data_segments;
  307. uint32 start_function;
  308. /* the index of auxiliary __data_end global,
  309. -1 means unexported */
  310. uint32 aux_data_end_global_index;
  311. /* auxiliary __data_end exported by wasm app */
  312. uint32 aux_data_end;
  313. /* the index of auxiliary __heap_base global,
  314. -1 means unexported */
  315. uint32 aux_heap_base_global_index;
  316. /* auxiliary __heap_base exported by wasm app */
  317. uint32 aux_heap_base;
  318. /* the index of auxiliary stack top global,
  319. -1 means unexported */
  320. uint32 aux_stack_top_global_index;
  321. /* auxiliary stack bottom resolved */
  322. uint32 aux_stack_bottom;
  323. /* auxiliary stack size resolved */
  324. uint32 aux_stack_size;
  325. /* the index of malloc/free function,
  326. -1 means unexported */
  327. uint32 malloc_function;
  328. uint32 free_function;
  329. /* the index of __retain function,
  330. -1 means unexported */
  331. uint32 retain_function;
  332. /* Whether there is possible memory grow, e.g. memory.grow opcode */
  333. bool possible_memory_grow;
  334. StringList const_str_list;
  335. #if WASM_ENABLE_LIBC_WASI != 0
  336. WASIArguments wasi_args;
  337. bool is_wasi_module;
  338. #endif
  339. #if WASM_ENABLE_MULTI_MODULE != 0
  340. /* TODO: add mutex for mutli-thread? */
  341. bh_list import_module_list_head;
  342. bh_list *import_module_list;
  343. #endif
  344. };
  345. typedef struct BlockType {
  346. /* Block type may be expressed in one of two forms:
  347. * either by the type of the single return value or
  348. * by a type index of module.
  349. */
  350. union {
  351. uint8 value_type;
  352. WASMType *type;
  353. } u;
  354. bool is_value_type;
  355. } BlockType;
  356. typedef struct WASMBranchBlock {
  357. uint8 label_type;
  358. uint32 cell_num;
  359. uint8 *target_addr;
  360. uint32 *frame_sp;
  361. } WASMBranchBlock;
  362. /* Execution environment, e.g. stack info */
  363. /**
  364. * Align an unsigned value on a alignment boundary.
  365. *
  366. * @param v the value to be aligned
  367. * @param b the alignment boundary (2, 4, 8, ...)
  368. *
  369. * @return the aligned value
  370. */
  371. inline static unsigned
  372. align_uint (unsigned v, unsigned b)
  373. {
  374. unsigned m = b - 1;
  375. return (v + m) & ~m;
  376. }
  377. /**
  378. * Return the hash value of c string.
  379. */
  380. inline static uint32
  381. wasm_string_hash(const char *str)
  382. {
  383. unsigned h = (unsigned)strlen(str);
  384. const uint8 *p = (uint8*)str;
  385. const uint8 *end = p + h;
  386. while (p != end)
  387. h = ((h << 5) - h) + *p++;
  388. return h;
  389. }
  390. /**
  391. * Whether two c strings are equal.
  392. */
  393. inline static bool
  394. wasm_string_equal(const char *s1, const char *s2)
  395. {
  396. return strcmp(s1, s2) == 0 ? true : false;
  397. }
  398. /**
  399. * Return the byte size of value type.
  400. *
  401. */
  402. inline static uint32
  403. wasm_value_type_size(uint8 value_type)
  404. {
  405. switch (value_type) {
  406. case VALUE_TYPE_I32:
  407. case VALUE_TYPE_F32:
  408. return sizeof(int32);
  409. case VALUE_TYPE_I64:
  410. case VALUE_TYPE_F64:
  411. return sizeof(int64);
  412. #if WASM_ENABLE_SIMD != 0
  413. case VALUE_TYPE_V128:
  414. return sizeof(int64) * 2;
  415. #endif
  416. default:
  417. bh_assert(0);
  418. }
  419. return 0;
  420. }
  421. inline static uint16
  422. wasm_value_type_cell_num(uint8 value_type)
  423. {
  424. if (value_type == VALUE_TYPE_VOID)
  425. return 0;
  426. else if (value_type == VALUE_TYPE_I32
  427. || value_type == VALUE_TYPE_F32)
  428. return 1;
  429. else if (value_type == VALUE_TYPE_I64
  430. || value_type == VALUE_TYPE_F64)
  431. return 2;
  432. #if WASM_ENABLE_SIMD != 0
  433. else if (value_type == VALUE_TYPE_V128)
  434. return 4;
  435. #endif
  436. else {
  437. bh_assert(0);
  438. }
  439. return 0;
  440. }
  441. inline static uint32
  442. wasm_get_cell_num(const uint8 *types, uint32 type_count)
  443. {
  444. uint32 cell_num = 0;
  445. uint32 i;
  446. for (i = 0; i < type_count; i++)
  447. cell_num += wasm_value_type_cell_num(types[i]);
  448. return cell_num;
  449. }
  450. inline static bool
  451. wasm_type_equal(const WASMType *type1, const WASMType *type2)
  452. {
  453. return (type1->param_count == type2->param_count
  454. && type1->result_count == type2->result_count
  455. && memcmp(type1->types, type2->types,
  456. (uint32)(type1->param_count
  457. + type1->result_count)) == 0)
  458. ? true : false;
  459. }
  460. static inline uint32
  461. block_type_get_param_types(BlockType *block_type,
  462. uint8 **p_param_types)
  463. {
  464. uint32 param_count = 0;
  465. if (!block_type->is_value_type) {
  466. WASMType *wasm_type = block_type->u.type;
  467. *p_param_types = wasm_type->types;
  468. param_count = wasm_type->param_count;
  469. }
  470. else {
  471. *p_param_types = NULL;
  472. param_count = 0;
  473. }
  474. return param_count;
  475. }
  476. static inline uint32
  477. block_type_get_result_types(BlockType *block_type,
  478. uint8 **p_result_types)
  479. {
  480. uint32 result_count = 0;
  481. if (block_type->is_value_type) {
  482. if (block_type->u.value_type != VALUE_TYPE_VOID) {
  483. *p_result_types = &block_type->u.value_type;
  484. result_count = 1;
  485. }
  486. }
  487. else {
  488. WASMType *wasm_type = block_type->u.type;
  489. *p_result_types = wasm_type->types + wasm_type->param_count;
  490. result_count = wasm_type->result_count;
  491. }
  492. return result_count;
  493. }
  494. #ifdef __cplusplus
  495. } /* end of extern "C" */
  496. #endif
  497. #endif /* end of _WASM_H_ */