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