aot.h 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  1. /*
  2. * Copyright (C) 2019 Intel Corporation. All rights reserved.
  3. * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. */
  5. #ifndef _AOT_H_
  6. #define _AOT_H_
  7. #include "bh_platform.h"
  8. #include "bh_assert.h"
  9. #include "../common/wasm_runtime_common.h"
  10. #include "../interpreter/wasm.h"
  11. #ifdef __cplusplus
  12. extern "C" {
  13. #endif
  14. #ifndef AOT_FUNC_PREFIX
  15. #define AOT_FUNC_PREFIX "aot_func#"
  16. #endif
  17. #ifndef AOT_FUNC_INTERNAL_PREFIX
  18. #define AOT_FUNC_INTERNAL_PREFIX "aot_func_internal#"
  19. #endif
  20. #ifndef AOT_STACK_SIZES_NAME
  21. #define AOT_STACK_SIZES_NAME "aot_stack_sizes"
  22. #endif
  23. extern const char *aot_stack_sizes_name;
  24. #ifndef AOT_STACK_SIZES_ALIAS_NAME
  25. #define AOT_STACK_SIZES_ALIAS_NAME "aot_stack_sizes_alias"
  26. #endif
  27. extern const char *aot_stack_sizes_alias_name;
  28. #ifndef AOT_STACK_SIZES_SECTION_NAME
  29. #define AOT_STACK_SIZES_SECTION_NAME ".aot_stack_sizes"
  30. #endif
  31. extern const char *aot_stack_sizes_section_name;
  32. typedef InitializerExpression AOTInitExpr;
  33. typedef WASMType AOTType;
  34. typedef WASMFuncType AOTFuncType;
  35. #if WASM_ENABLE_GC != 0
  36. typedef WASMStructType AOTStructType;
  37. typedef WASMArrayType AOTArrayType;
  38. #endif
  39. typedef WASMExport AOTExport;
  40. #if WASM_ENABLE_DEBUG_AOT != 0
  41. typedef void *dwarf_extractor_handle_t;
  42. #endif
  43. typedef enum AOTIntCond {
  44. INT_EQZ = 0,
  45. INT_EQ,
  46. INT_NE,
  47. INT_LT_S,
  48. INT_LT_U,
  49. INT_GT_S,
  50. INT_GT_U,
  51. INT_LE_S,
  52. INT_LE_U,
  53. INT_GE_S,
  54. INT_GE_U
  55. } AOTIntCond;
  56. typedef enum AOTFloatCond {
  57. FLOAT_EQ = 0,
  58. FLOAT_NE,
  59. FLOAT_LT,
  60. FLOAT_GT,
  61. FLOAT_LE,
  62. FLOAT_GE,
  63. FLOAT_UNO
  64. } AOTFloatCond;
  65. /**
  66. * Import memory
  67. */
  68. typedef struct AOTImportMemory {
  69. char *module_name;
  70. char *memory_name;
  71. uint32 memory_flags;
  72. uint32 num_bytes_per_page;
  73. uint32 mem_init_page_count;
  74. uint32 mem_max_page_count;
  75. } AOTImportMemory;
  76. /**
  77. * Memory information
  78. */
  79. typedef struct AOTMemory {
  80. /* memory info */
  81. uint32 memory_flags;
  82. uint32 num_bytes_per_page;
  83. uint32 mem_init_page_count;
  84. uint32 mem_max_page_count;
  85. } AOTMemory;
  86. /**
  87. * A segment of memory init data
  88. */
  89. typedef struct AOTMemInitData {
  90. #if WASM_ENABLE_BULK_MEMORY != 0
  91. /* Passive flag */
  92. bool is_passive;
  93. /* memory index */
  94. uint32 memory_index;
  95. #endif
  96. /* Start address of init data */
  97. AOTInitExpr offset;
  98. /* Byte count */
  99. uint32 byte_count;
  100. /* Byte array */
  101. uint8 bytes[1];
  102. } AOTMemInitData;
  103. /**
  104. * Import table
  105. */
  106. typedef struct AOTImportTable {
  107. char *module_name;
  108. char *table_name;
  109. uint8 elem_type;
  110. uint8 table_flags;
  111. bool possible_grow;
  112. uint32 table_init_size;
  113. uint32 table_max_size;
  114. #if WASM_ENABLE_GC != 0
  115. WASMRefType *elem_ref_type;
  116. #endif
  117. } AOTImportTable;
  118. /**
  119. * Table
  120. */
  121. typedef struct AOTTable {
  122. uint8 elem_type;
  123. uint8 table_flags;
  124. bool possible_grow;
  125. uint32 table_init_size;
  126. uint32 table_max_size;
  127. #if WASM_ENABLE_GC != 0
  128. WASMRefType *elem_ref_type;
  129. /* init expr for the whole table */
  130. InitializerExpression init_expr;
  131. #endif
  132. } AOTTable;
  133. /**
  134. * A segment of table init data
  135. */
  136. typedef struct AOTTableInitData {
  137. /* 0 to 7 */
  138. uint32 mode;
  139. /* funcref or externref, elemkind will be considered as funcref */
  140. uint32 elem_type;
  141. #if WASM_ENABLE_GC != 0
  142. WASMRefType *elem_ref_type;
  143. #endif
  144. /* optional, only for active */
  145. uint32 table_index;
  146. /* Start address of init data */
  147. AOTInitExpr offset;
  148. /* Function index count */
  149. uint32 value_count;
  150. /* Function index array */
  151. InitializerExpression init_values[1];
  152. } AOTTableInitData;
  153. /**
  154. * Import global variable
  155. */
  156. typedef struct AOTImportGlobal {
  157. char *module_name;
  158. char *global_name;
  159. /* VALUE_TYPE_I32/I64/F32/F64 */
  160. uint8 type;
  161. bool is_mutable;
  162. uint32 size;
  163. /* The data offset of current global in global data */
  164. uint32 data_offset;
  165. #if WASM_ENABLE_WAMR_COMPILER != 0 || WASM_ENABLE_JIT != 0
  166. /*
  167. * The data size and data offset of a wasm global may vary
  168. * in 32-bit target and 64-bit target, e.g., the size of a
  169. * GC obj is 4 bytes in the former and 8 bytes in the
  170. * latter, the AOT compiler needs to use the correct data
  171. * offset according to the target info.
  172. */
  173. uint32 size_64bit;
  174. uint32 size_32bit;
  175. uint32 data_offset_64bit;
  176. uint32 data_offset_32bit;
  177. #endif
  178. /* global data after linked */
  179. WASMValue global_data_linked;
  180. bool is_linked;
  181. } AOTImportGlobal;
  182. /**
  183. * Global variable
  184. */
  185. typedef struct AOTGlobal {
  186. /* VALUE_TYPE_I32/I64/F32/F64 */
  187. uint8 type;
  188. bool is_mutable;
  189. uint32 size;
  190. /* The data offset of current global in global data */
  191. uint32 data_offset;
  192. #if WASM_ENABLE_WAMR_COMPILER != 0 || WASM_ENABLE_JIT != 0
  193. /* See comments in AOTImportGlobal */
  194. uint32 size_64bit;
  195. uint32 size_32bit;
  196. uint32 data_offset_64bit;
  197. uint32 data_offset_32bit;
  198. #endif
  199. AOTInitExpr init_expr;
  200. } AOTGlobal;
  201. /**
  202. * Import function
  203. */
  204. typedef struct AOTImportFunc {
  205. char *module_name;
  206. char *func_name;
  207. AOTFuncType *func_type;
  208. uint32 func_type_index;
  209. /* function pointer after linked */
  210. void *func_ptr_linked;
  211. /* signature from registered native symbols */
  212. const char *signature;
  213. /* attachment */
  214. void *attachment;
  215. bool call_conv_raw;
  216. bool call_conv_wasm_c_api;
  217. bool wasm_c_api_with_env;
  218. } AOTImportFunc;
  219. /**
  220. * Function
  221. */
  222. typedef struct AOTFunc {
  223. AOTFuncType *func_type;
  224. uint32 func_type_index;
  225. uint32 local_count;
  226. uint8 *local_types_wp;
  227. uint16 param_cell_num;
  228. uint16 local_cell_num;
  229. uint32 max_stack_cell_num;
  230. uint32 code_size;
  231. uint8 *code;
  232. /* offset of each local, including function parameters
  233. and local variables */
  234. uint16 *local_offsets;
  235. } AOTFunc;
  236. typedef struct AOTCompData {
  237. /* Import memories */
  238. uint32 import_memory_count;
  239. AOTImportMemory *import_memories;
  240. /* Memories */
  241. uint32 memory_count;
  242. AOTMemory *memories;
  243. /* Memory init data info */
  244. uint32 mem_init_data_count;
  245. AOTMemInitData **mem_init_data_list;
  246. /* Import tables */
  247. uint32 import_table_count;
  248. AOTImportTable *import_tables;
  249. /* Tables */
  250. uint32 table_count;
  251. AOTTable *tables;
  252. /* Table init data info */
  253. uint32 table_init_data_count;
  254. AOTTableInitData **table_init_data_list;
  255. /* Import globals */
  256. uint32 import_global_count;
  257. AOTImportGlobal *import_globals;
  258. /* Globals */
  259. uint32 global_count;
  260. AOTGlobal *globals;
  261. /* Function types */
  262. uint32 type_count;
  263. AOTType **types;
  264. /* Import functions */
  265. uint32 import_func_count;
  266. AOTImportFunc *import_funcs;
  267. /* Functions */
  268. uint32 func_count;
  269. AOTFunc **funcs;
  270. /* Custom name sections */
  271. const uint8 *name_section_buf;
  272. const uint8 *name_section_buf_end;
  273. uint8 *aot_name_section_buf;
  274. uint32 aot_name_section_size;
  275. uint32 global_data_size_64bit;
  276. uint32 global_data_size_32bit;
  277. uint32 start_func_index;
  278. uint32 malloc_func_index;
  279. uint32 free_func_index;
  280. uint32 retain_func_index;
  281. uint32 aux_data_end_global_index;
  282. uint64 aux_data_end;
  283. uint32 aux_heap_base_global_index;
  284. uint64 aux_heap_base;
  285. uint32 aux_stack_top_global_index;
  286. uint64 aux_stack_bottom;
  287. uint32 aux_stack_size;
  288. #if WASM_ENABLE_STRINGREF != 0
  289. uint32 string_literal_count;
  290. uint32 *string_literal_lengths_wp;
  291. const uint8 **string_literal_ptrs_wp;
  292. #endif
  293. WASMModule *wasm_module;
  294. #if WASM_ENABLE_DEBUG_AOT != 0
  295. dwarf_extractor_handle_t extractor;
  296. #endif
  297. } AOTCompData;
  298. typedef struct AOTNativeSymbol {
  299. bh_list_link link;
  300. char symbol[32];
  301. int32 index;
  302. } AOTNativeSymbol;
  303. AOTCompData *
  304. aot_create_comp_data(WASMModule *module, const char *target_arch,
  305. bool gc_enabled);
  306. void
  307. aot_destroy_comp_data(AOTCompData *comp_data);
  308. char *
  309. aot_get_last_error();
  310. void
  311. aot_set_last_error(const char *error);
  312. void
  313. aot_set_last_error_v(const char *format, ...);
  314. #if BH_DEBUG != 0
  315. #define HANDLE_FAILURE(callee) \
  316. do { \
  317. aot_set_last_error_v("call %s failed in %s:%d", (callee), \
  318. __FUNCTION__, __LINE__); \
  319. } while (0)
  320. #else
  321. #define HANDLE_FAILURE(callee) \
  322. do { \
  323. aot_set_last_error_v("call %s failed", (callee)); \
  324. } while (0)
  325. #endif
  326. static inline uint32
  327. aot_get_imp_tbl_data_slots(const AOTImportTable *tbl, bool is_jit_mode)
  328. {
  329. #if WASM_ENABLE_MULTI_MODULE != 0
  330. if (is_jit_mode)
  331. return tbl->table_max_size;
  332. #else
  333. (void)is_jit_mode;
  334. #endif
  335. return tbl->possible_grow ? tbl->table_max_size : tbl->table_init_size;
  336. }
  337. static inline uint32
  338. aot_get_tbl_data_slots(const AOTTable *tbl, bool is_jit_mode)
  339. {
  340. #if WASM_ENABLE_MULTI_MODULE != 0
  341. if (is_jit_mode)
  342. return tbl->table_max_size;
  343. #else
  344. (void)is_jit_mode;
  345. #endif
  346. return tbl->possible_grow ? tbl->table_max_size : tbl->table_init_size;
  347. }
  348. #ifdef __cplusplus
  349. } /* end of extern "C" */
  350. #endif
  351. #endif /* end of _AOT_H_ */