aot.h 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  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. WASMGlobalType type;
  160. uint32 size;
  161. /* The data offset of current global in global data */
  162. uint32 data_offset;
  163. #if WASM_ENABLE_WAMR_COMPILER != 0 || WASM_ENABLE_JIT != 0
  164. /*
  165. * The data size and data offset of a wasm global may vary
  166. * in 32-bit target and 64-bit target, e.g., the size of a
  167. * GC obj is 4 bytes in the former and 8 bytes in the
  168. * latter, the AOT compiler needs to use the correct data
  169. * offset according to the target info.
  170. */
  171. uint32 size_64bit;
  172. uint32 size_32bit;
  173. uint32 data_offset_64bit;
  174. uint32 data_offset_32bit;
  175. #endif
  176. /* global data after linked */
  177. WASMValue global_data_linked;
  178. bool is_linked;
  179. } AOTImportGlobal;
  180. /**
  181. * Global variable
  182. */
  183. typedef struct AOTGlobal {
  184. WASMGlobalType type;
  185. uint32 size;
  186. /* The data offset of current global in global data */
  187. uint32 data_offset;
  188. #if WASM_ENABLE_WAMR_COMPILER != 0 || WASM_ENABLE_JIT != 0
  189. /* See comments in AOTImportGlobal */
  190. uint32 size_64bit;
  191. uint32 size_32bit;
  192. uint32 data_offset_64bit;
  193. uint32 data_offset_32bit;
  194. #endif
  195. AOTInitExpr init_expr;
  196. } AOTGlobal;
  197. /**
  198. * Import function
  199. */
  200. typedef struct AOTImportFunc {
  201. char *module_name;
  202. char *func_name;
  203. AOTFuncType *func_type;
  204. uint32 func_type_index;
  205. /* function pointer after linked */
  206. void *func_ptr_linked;
  207. /* signature from registered native symbols */
  208. const char *signature;
  209. /* attachment */
  210. void *attachment;
  211. bool call_conv_raw;
  212. bool call_conv_wasm_c_api;
  213. bool wasm_c_api_with_env;
  214. } AOTImportFunc;
  215. /**
  216. * Function
  217. */
  218. typedef struct AOTFunc {
  219. AOTFuncType *func_type;
  220. uint32 func_type_index;
  221. uint32 local_count;
  222. uint8 *local_types_wp;
  223. uint16 param_cell_num;
  224. uint16 local_cell_num;
  225. uint32 max_stack_cell_num;
  226. uint32 code_size;
  227. uint8 *code;
  228. /* offset of each local, including function parameters
  229. and local variables */
  230. uint16 *local_offsets;
  231. } AOTFunc;
  232. typedef struct AOTCompData {
  233. /* Import memories */
  234. uint32 import_memory_count;
  235. AOTImportMemory *import_memories;
  236. /* Memories */
  237. uint32 memory_count;
  238. AOTMemory *memories;
  239. /* Memory init data info */
  240. uint32 mem_init_data_count;
  241. AOTMemInitData **mem_init_data_list;
  242. /* Import tables */
  243. uint32 import_table_count;
  244. AOTImportTable *import_tables;
  245. /* Tables */
  246. uint32 table_count;
  247. AOTTable *tables;
  248. /* Table init data info */
  249. uint32 table_init_data_count;
  250. AOTTableInitData **table_init_data_list;
  251. /* Import globals */
  252. uint32 import_global_count;
  253. AOTImportGlobal *import_globals;
  254. /* Globals */
  255. uint32 global_count;
  256. AOTGlobal *globals;
  257. /* Function types */
  258. uint32 type_count;
  259. AOTType **types;
  260. /* Import functions */
  261. uint32 import_func_count;
  262. AOTImportFunc *import_funcs;
  263. /* Functions */
  264. uint32 func_count;
  265. AOTFunc **funcs;
  266. /* Custom name sections */
  267. const uint8 *name_section_buf;
  268. const uint8 *name_section_buf_end;
  269. uint8 *aot_name_section_buf;
  270. uint32 aot_name_section_size;
  271. uint32 global_data_size_64bit;
  272. uint32 global_data_size_32bit;
  273. uint32 start_func_index;
  274. uint32 malloc_func_index;
  275. uint32 free_func_index;
  276. uint32 retain_func_index;
  277. uint32 aux_data_end_global_index;
  278. uint64 aux_data_end;
  279. uint32 aux_heap_base_global_index;
  280. uint64 aux_heap_base;
  281. uint32 aux_stack_top_global_index;
  282. uint64 aux_stack_bottom;
  283. uint32 aux_stack_size;
  284. #if WASM_ENABLE_STRINGREF != 0
  285. uint32 string_literal_count;
  286. uint32 *string_literal_lengths_wp;
  287. const uint8 **string_literal_ptrs_wp;
  288. #endif
  289. WASMModule *wasm_module;
  290. #if WASM_ENABLE_DEBUG_AOT != 0
  291. dwarf_extractor_handle_t extractor;
  292. #endif
  293. } AOTCompData;
  294. typedef struct AOTNativeSymbol {
  295. bh_list_link link;
  296. char symbol[32];
  297. int32 index;
  298. } AOTNativeSymbol;
  299. AOTCompData *
  300. aot_create_comp_data(WASMModule *module, const char *target_arch,
  301. bool gc_enabled);
  302. void
  303. aot_destroy_comp_data(AOTCompData *comp_data);
  304. char *
  305. aot_get_last_error();
  306. void
  307. aot_set_last_error(const char *error);
  308. void
  309. aot_set_last_error_v(const char *format, ...);
  310. #if BH_DEBUG != 0
  311. #define HANDLE_FAILURE(callee) \
  312. do { \
  313. aot_set_last_error_v("call %s failed in %s:%d", (callee), \
  314. __FUNCTION__, __LINE__); \
  315. } while (0)
  316. #else
  317. #define HANDLE_FAILURE(callee) \
  318. do { \
  319. aot_set_last_error_v("call %s failed", (callee)); \
  320. } while (0)
  321. #endif
  322. static inline uint32
  323. aot_get_imp_tbl_data_slots(const AOTImportTable *tbl, bool is_jit_mode)
  324. {
  325. #if WASM_ENABLE_MULTI_MODULE != 0
  326. if (is_jit_mode)
  327. return tbl->table_max_size;
  328. #else
  329. (void)is_jit_mode;
  330. #endif
  331. return tbl->possible_grow ? tbl->table_max_size : tbl->table_init_size;
  332. }
  333. static inline uint32
  334. aot_get_tbl_data_slots(const AOTTable *tbl, bool is_jit_mode)
  335. {
  336. #if WASM_ENABLE_MULTI_MODULE != 0
  337. if (is_jit_mode)
  338. return tbl->table_max_size;
  339. #else
  340. (void)is_jit_mode;
  341. #endif
  342. return tbl->possible_grow ? tbl->table_max_size : tbl->table_init_size;
  343. }
  344. #ifdef __cplusplus
  345. } /* end of extern "C" */
  346. #endif
  347. #endif /* end of _AOT_H_ */