aot.h 8.4 KB

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