wasm.h 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  1. /*
  2. * Copyright (C) 2019 Intel Corporation. All rights reserved.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #ifndef _WASM_H_
  17. #define _WASM_H_
  18. #include "wasm_platform.h"
  19. #include "wasm_hashmap.h"
  20. #include "wasm_assert.h"
  21. #ifdef __cplusplus
  22. extern "C" {
  23. #endif
  24. /** Value Type */
  25. #define VALUE_TYPE_I32 0x7F
  26. #define VALUE_TYPE_I64 0X7E
  27. #define VALUE_TYPE_F32 0x7D
  28. #define VALUE_TYPE_F64 0x7C
  29. #define VALUE_TYPE_VOID 0x00
  30. /* Table Element Type */
  31. #define TABLE_ELEM_TYPE_ANY_FUNC 0x70
  32. #define MaxMemoryPages 65536
  33. #define MaxTableElems UINT32_MAX
  34. #define NumBytesPerPage 65536
  35. #define NumBytesPerPageLog2 16
  36. #define MaxReturnValues 16
  37. #define INIT_EXPR_TYPE_I32_CONST 0x41
  38. #define INIT_EXPR_TYPE_I64_CONST 0x42
  39. #define INIT_EXPR_TYPE_F32_CONST 0x43
  40. #define INIT_EXPR_TYPE_F64_CONST 0x44
  41. #define INIT_EXPR_TYPE_GET_GLOBAL 0x23
  42. #define INIT_EXPR_TYPE_ERROR 0xff
  43. #define WASM_MAGIC_NUMBER 0x6d736100
  44. #define WASM_CURRENT_VERSION 1
  45. #define SECTION_TYPE_USER 0
  46. #define SECTION_TYPE_TYPE 1
  47. #define SECTION_TYPE_IMPORT 2
  48. #define SECTION_TYPE_FUNC 3
  49. #define SECTION_TYPE_TABLE 4
  50. #define SECTION_TYPE_MEMORY 5
  51. #define SECTION_TYPE_GLOBAL 6
  52. #define SECTION_TYPE_EXPORT 7
  53. #define SECTION_TYPE_START 8
  54. #define SECTION_TYPE_ELEM 9
  55. #define SECTION_TYPE_CODE 10
  56. #define SECTION_TYPE_DATA 11
  57. #define IMPORT_KIND_FUNC 0
  58. #define IMPORT_KIND_TABLE 1
  59. #define IMPORT_KIND_MEMORY 2
  60. #define IMPORT_KIND_GLOBAL 3
  61. #define EXPORT_KIND_FUNC 0
  62. #define EXPORT_KIND_TABLE 1
  63. #define EXPORT_KIND_MEMORY 2
  64. #define EXPORT_KIND_GLOBAL 3
  65. #define BLOCK_TYPE_BLOCK 0
  66. #define BLOCK_TYPE_LOOP 1
  67. #define BLOCK_TYPE_IF 2
  68. #define BLOCK_TYPE_FUNCTION 3
  69. #define CALL_TYPE_WRAPPER 0
  70. #define CALL_TYPE_C_INTRINSIC 1
  71. typedef union WASMValue {
  72. int32 i32;
  73. uint32 u32;
  74. int64 i64;
  75. uint64 u64;
  76. float32 f32;
  77. float64 f64;
  78. uintptr_t addr;
  79. } WASMValue;
  80. typedef struct InitializerExpression {
  81. /* type of INIT_EXPR_TYPE_XXX */
  82. uint8 init_expr_type;
  83. union {
  84. int32 i32;
  85. int64 i64;
  86. float32 f32;
  87. float64 f64;
  88. uint32 global_index;
  89. } u;
  90. } InitializerExpression;
  91. typedef struct WASMType {
  92. uint32 param_count;
  93. /* only one result is supported currently */
  94. uint32 result_count;
  95. /* types of params and results */
  96. uint8 types[1];
  97. } WASMType;
  98. typedef struct WASMTable {
  99. uint8 elem_type;
  100. uint32 flags;
  101. uint32 init_size;
  102. /* specified if (flags & 1), else it is 0x10000 */
  103. uint32 max_size;
  104. } WASMTable;
  105. typedef struct WASMMemory {
  106. uint32 flags;
  107. /* 64 kbytes one page by default */
  108. uint32 init_page_count;
  109. uint32 max_page_count;
  110. } WASMMemory;
  111. typedef struct WASMTableImport {
  112. char *module_name;
  113. char *field_name;
  114. uint8 elem_type;
  115. uint32 flags;
  116. uint32 init_size;
  117. /* specified if (flags & 1), else it is 0x10000 */
  118. uint32 max_size;
  119. } WASMTableImport;
  120. typedef struct WASMMemoryImport {
  121. char *module_name;
  122. char *field_name;
  123. uint32 flags;
  124. /* 64 kbytes one page by default */
  125. uint32 init_page_count;
  126. uint32 max_page_count;
  127. } WASMMemoryImport;
  128. typedef struct WASMFunctionImport {
  129. char *module_name;
  130. char *field_name;
  131. /* function type */
  132. WASMType *func_type;
  133. /* c intrinsic function or wrapper function */
  134. uint32 call_type;
  135. /* function pointer after linked */
  136. void *func_ptr_linked;
  137. } WASMFunctionImport;
  138. typedef struct WASMGlobalImport {
  139. char *module_name;
  140. char *field_name;
  141. uint8 type;
  142. bool is_mutable;
  143. bool is_addr;
  144. /* global data after linked */
  145. WASMValue global_data_linked;
  146. } WASMGlobalImport;
  147. typedef struct WASMImport {
  148. uint8 kind;
  149. union {
  150. WASMFunctionImport function;
  151. WASMTableImport table;
  152. WASMMemoryImport memory;
  153. WASMGlobalImport global;
  154. struct {
  155. char *module_name;
  156. char *field_name;
  157. } names;
  158. } u;
  159. } WASMImport;
  160. typedef struct WASMFunction {
  161. /* the type of function */
  162. WASMType *func_type;
  163. uint32 local_count;
  164. uint8 *local_types;
  165. uint32 max_stack_cell_num;
  166. uint32 max_block_num;
  167. uint32 code_size;
  168. uint8 *code;
  169. } WASMFunction;
  170. typedef struct WASMGlobal {
  171. uint8 type;
  172. bool is_mutable;
  173. bool is_addr;
  174. InitializerExpression init_expr;
  175. } WASMGlobal;
  176. typedef struct WASMExport {
  177. char *name;
  178. uint8 kind;
  179. uint32 index;
  180. } WASMExport;
  181. typedef struct WASMTableSeg {
  182. uint32 table_index;
  183. InitializerExpression base_offset;
  184. uint32 function_count;
  185. uint32 *func_indexes;
  186. } WASMTableSeg;
  187. typedef struct WASMDataSeg {
  188. uint32 memory_index;
  189. InitializerExpression base_offset;
  190. uint32 data_length;
  191. uint8 *data;
  192. } WASMDataSeg;
  193. typedef struct WASMModule {
  194. uint32 type_count;
  195. uint32 import_count;
  196. uint32 function_count;
  197. uint32 table_count;
  198. uint32 memory_count;
  199. uint32 global_count;
  200. uint32 export_count;
  201. uint32 table_seg_count;
  202. uint32 data_seg_count;
  203. uint32 import_function_count;
  204. uint32 import_table_count;
  205. uint32 import_memory_count;
  206. uint32 import_global_count;
  207. WASMImport *import_functions;
  208. WASMImport *import_tables;
  209. WASMImport *import_memories;
  210. WASMImport *import_globals;
  211. WASMType **types;
  212. WASMImport *imports;
  213. WASMFunction **functions;
  214. WASMTable *tables;
  215. WASMMemory *memories;
  216. WASMGlobal *globals;
  217. WASMExport *exports;
  218. WASMTableSeg *table_segments;
  219. WASMDataSeg **data_segments;
  220. uint32 start_function;
  221. HashMap *const_str_set;
  222. HashMap *branch_set;
  223. } WASMModule;
  224. typedef struct WASMBranchBlock {
  225. uint8 block_type;
  226. uint8 return_type;
  227. uint8 *start_addr;
  228. uint8 *else_addr;
  229. uint8 *end_addr;
  230. uint32 *frame_sp;
  231. uint8 *frame_ref;
  232. } WASMBranchBlock;
  233. typedef struct WASMSection {
  234. struct WASMSection *next;
  235. /* section type */
  236. int section_type;
  237. /* section body, not include type and size */
  238. const uint8_t *section_body;
  239. /* section body size */
  240. uint32_t section_body_size;
  241. } WASMSection;
  242. /* Execution environment, e.g. stack info */
  243. /**
  244. * Align an unsigned value on a alignment boundary.
  245. *
  246. * @param v the value to be aligned
  247. * @param b the alignment boundary (2, 4, 8, ...)
  248. *
  249. * @return the aligned value
  250. */
  251. inline static unsigned
  252. align_uint (unsigned v, unsigned b)
  253. {
  254. unsigned m = b - 1;
  255. return (v + m) & ~m;
  256. }
  257. /**
  258. * Return the hash value of c string.
  259. */
  260. inline static uint32
  261. wasm_string_hash(const char *str)
  262. {
  263. unsigned h = strlen(str);
  264. const uint8 *p = (uint8*)str;
  265. const uint8 *end = p + h;
  266. while (p != end)
  267. h = ((h << 5) - h) + *p++;
  268. return h;
  269. }
  270. /**
  271. * Whether two c strings are equal.
  272. */
  273. inline static bool
  274. wasm_string_equal(const char *s1, const char *s2)
  275. {
  276. return strcmp(s1, s2) == 0 ? true : false;
  277. }
  278. /**
  279. * Return the byte size of value type.
  280. *
  281. */
  282. inline static uint32
  283. wasm_value_type_size(uint8 value_type)
  284. {
  285. switch (value_type) {
  286. case VALUE_TYPE_I32:
  287. case VALUE_TYPE_F32:
  288. return sizeof(int32);
  289. case VALUE_TYPE_I64:
  290. case VALUE_TYPE_F64:
  291. return sizeof(int64);
  292. default:
  293. wasm_assert(0);
  294. }
  295. return 0;
  296. }
  297. inline static uint16
  298. wasm_value_type_cell_num(uint8 value_type)
  299. {
  300. switch (value_type) {
  301. case VALUE_TYPE_I32:
  302. case VALUE_TYPE_F32:
  303. return 1;
  304. case VALUE_TYPE_I64:
  305. case VALUE_TYPE_F64:
  306. return 2;
  307. default:
  308. wasm_assert(0);
  309. }
  310. return 0;
  311. }
  312. inline static uint16
  313. wasm_get_cell_num(const uint8 *types, uint32 type_count)
  314. {
  315. uint16 cell_num = 0;
  316. uint32 i;
  317. for (i = 0; i < type_count; i++)
  318. cell_num += wasm_value_type_cell_num(types[i]);
  319. return cell_num;
  320. }
  321. inline static uint16
  322. wasm_type_param_cell_num(const WASMType *type)
  323. {
  324. return wasm_get_cell_num(type->types, type->param_count);
  325. }
  326. inline static uint16
  327. wasm_type_return_cell_num(const WASMType *type)
  328. {
  329. return wasm_get_cell_num(type->types + type->param_count,
  330. type->result_count);
  331. }
  332. inline static bool
  333. wasm_type_equal(const WASMType *type1, const WASMType *type2)
  334. {
  335. return (type1->param_count == type2->param_count
  336. && type1->result_count == type2->result_count
  337. && memcmp(type1->types, type2->types,
  338. type1->param_count + type1->result_count) == 0)
  339. ? true : false;
  340. }
  341. #ifdef __cplusplus
  342. } /* end of extern "C" */
  343. #endif
  344. #endif /* end of _WASM_H_ */