aot_compiler.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  1. /*
  2. * Copyright (C) 2019 Intel Corporation. All rights reserved.
  3. * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. */
  5. #ifndef _AOT_COMPILER_H_
  6. #define _AOT_COMPILER_H_
  7. #include "aot.h"
  8. #include "aot_llvm.h"
  9. #ifdef __cplusplus
  10. extern "C" {
  11. #endif
  12. typedef enum IntCond {
  13. INT_EQZ = 0,
  14. INT_EQ,
  15. INT_NE,
  16. INT_LT_S,
  17. INT_LT_U,
  18. INT_GT_S,
  19. INT_GT_U,
  20. INT_LE_S,
  21. INT_LE_U,
  22. INT_GE_S,
  23. INT_GE_U
  24. } IntCond;
  25. typedef enum FloatCond {
  26. FLOAT_EQ = 0,
  27. FLOAT_NE,
  28. FLOAT_LT,
  29. FLOAT_GT,
  30. FLOAT_LE,
  31. FLOAT_GE
  32. } FloatCond;
  33. typedef enum IntArithmetic {
  34. INT_ADD = 0,
  35. INT_SUB,
  36. INT_MUL,
  37. INT_DIV_S,
  38. INT_DIV_U,
  39. INT_REM_S,
  40. INT_REM_U
  41. } IntArithmetic;
  42. typedef enum V128Arithmetic {
  43. V128_ADD = 0,
  44. V128_ADD_SATURATE_S,
  45. V128_ADD_SATURATE_U,
  46. V128_SUB,
  47. V128_SUB_SATURATE_S,
  48. V128_SUB_SATURATE_U,
  49. V128_MUL,
  50. V128_DIV,
  51. V128_NEG,
  52. V128_MIN,
  53. V128_MAX,
  54. } V128Arithmetic;
  55. typedef enum IntBitwise {
  56. INT_AND = 0,
  57. INT_OR,
  58. INT_XOR,
  59. } IntBitwise;
  60. typedef enum V128Bitwise {
  61. V128_NOT,
  62. V128_AND,
  63. V128_ANDNOT,
  64. V128_OR,
  65. V128_XOR,
  66. V128_BITSELECT
  67. } V128Bitwise;
  68. typedef enum IntShift {
  69. INT_SHL = 0,
  70. INT_SHR_S,
  71. INT_SHR_U,
  72. INT_ROTL,
  73. INT_ROTR
  74. } IntShift;
  75. typedef enum FloatMath {
  76. FLOAT_ABS = 0,
  77. FLOAT_NEG,
  78. FLOAT_CEIL,
  79. FLOAT_FLOOR,
  80. FLOAT_TRUNC,
  81. FLOAT_NEAREST,
  82. FLOAT_SQRT
  83. } FloatMath;
  84. typedef enum FloatArithmetic {
  85. FLOAT_ADD = 0,
  86. FLOAT_SUB,
  87. FLOAT_MUL,
  88. FLOAT_DIV,
  89. FLOAT_MIN,
  90. FLOAT_MAX
  91. } FloatArithmetic;
  92. static inline bool
  93. check_type_compatible(uint8 src_type, uint8 dst_type)
  94. {
  95. if (src_type == dst_type) {
  96. return true;
  97. }
  98. /* ext i1 to i32 */
  99. if (src_type == VALUE_TYPE_I1 && dst_type == VALUE_TYPE_I32) {
  100. return true;
  101. }
  102. /* i32 <==> func.ref, i32 <==> extern.ref */
  103. if (src_type == VALUE_TYPE_I32
  104. && (dst_type == VALUE_TYPE_EXTERNREF
  105. || dst_type == VALUE_TYPE_FUNCREF)) {
  106. return true;
  107. }
  108. if (dst_type == VALUE_TYPE_I32
  109. && (src_type == VALUE_TYPE_FUNCREF
  110. || src_type == VALUE_TYPE_EXTERNREF)) {
  111. return true;
  112. }
  113. return false;
  114. }
  115. #define CHECK_STACK() do { \
  116. if (!func_ctx->block_stack.block_list_end) { \
  117. aot_set_last_error("WASM block stack underflow."); \
  118. goto fail; \
  119. } \
  120. if (!func_ctx->block_stack.block_list_end-> \
  121. value_stack.value_list_end) { \
  122. aot_set_last_error("WASM data stack underflow."); \
  123. goto fail; \
  124. } \
  125. } while (0)
  126. #define POP(llvm_value, value_type) do { \
  127. AOTValue *aot_value; \
  128. CHECK_STACK(); \
  129. aot_value = aot_value_stack_pop \
  130. (&func_ctx->block_stack.block_list_end->value_stack); \
  131. if (!check_type_compatible(aot_value->type, \
  132. value_type)) { \
  133. aot_set_last_error("invalid WASM stack data type."); \
  134. wasm_runtime_free(aot_value); \
  135. goto fail; \
  136. } \
  137. if (aot_value->type == value_type) \
  138. llvm_value = aot_value->value; \
  139. else { \
  140. if (aot_value->type == VALUE_TYPE_I1) { \
  141. if (!(llvm_value = LLVMBuildZExt(comp_ctx->builder, \
  142. aot_value->value, I32_TYPE, "i1toi32"))) { \
  143. aot_set_last_error("invalid WASM stack " \
  144. "data type."); \
  145. wasm_runtime_free(aot_value); \
  146. goto fail; \
  147. } \
  148. } \
  149. else { \
  150. bh_assert(aot_value->type == VALUE_TYPE_I32 \
  151. || aot_value->type == VALUE_TYPE_FUNCREF \
  152. || aot_value->type == VALUE_TYPE_EXTERNREF); \
  153. bh_assert(value_type == VALUE_TYPE_I32 \
  154. || value_type == VALUE_TYPE_FUNCREF \
  155. || value_type == VALUE_TYPE_EXTERNREF); \
  156. llvm_value = aot_value->value; \
  157. } \
  158. } \
  159. wasm_runtime_free(aot_value); \
  160. } while (0)
  161. #define POP_I32(v) POP(v, VALUE_TYPE_I32)
  162. #define POP_I64(v) POP(v, VALUE_TYPE_I64)
  163. #define POP_F32(v) POP(v, VALUE_TYPE_F32)
  164. #define POP_F64(v) POP(v, VALUE_TYPE_F64)
  165. #define POP_V128(v) POP(v, VALUE_TYPE_V128)
  166. #define POP_FUNCREF(v) POP(v, VALUE_TYPE_FUNCREF)
  167. #define POP_EXTERNREF(v) POP(v, VALUE_TYPE_EXTERNREF)
  168. #define POP_COND(llvm_value) do { \
  169. AOTValue *aot_value; \
  170. CHECK_STACK(); \
  171. aot_value = aot_value_stack_pop \
  172. (&func_ctx->block_stack.block_list_end->value_stack); \
  173. if (aot_value->type != VALUE_TYPE_I1 \
  174. && aot_value->type != VALUE_TYPE_I32) { \
  175. aot_set_last_error("invalid WASM stack data type."); \
  176. wasm_runtime_free(aot_value); \
  177. goto fail; \
  178. } \
  179. if (aot_value->type == VALUE_TYPE_I1) \
  180. llvm_value = aot_value->value; \
  181. else { \
  182. if (!(llvm_value = LLVMBuildICmp(comp_ctx->builder, \
  183. LLVMIntNE, aot_value->value, I32_ZERO, \
  184. "i1_cond"))){ \
  185. aot_set_last_error("llvm build trunc failed."); \
  186. wasm_runtime_free(aot_value); \
  187. goto fail; \
  188. } \
  189. } \
  190. wasm_runtime_free(aot_value); \
  191. } while (0)
  192. #define PUSH(llvm_value, value_type) do { \
  193. AOTValue *aot_value; \
  194. if (!func_ctx->block_stack.block_list_end) { \
  195. aot_set_last_error("WASM block stack underflow."); \
  196. goto fail; \
  197. } \
  198. aot_value = wasm_runtime_malloc(sizeof(AOTValue)); \
  199. if (!aot_value) { \
  200. aot_set_last_error("allocate memory failed."); \
  201. goto fail; \
  202. } \
  203. memset(aot_value, 0, sizeof(AOTValue)); \
  204. aot_value->type = value_type; \
  205. aot_value->value = llvm_value; \
  206. aot_value_stack_push \
  207. (&func_ctx->block_stack.block_list_end->value_stack,\
  208. aot_value); \
  209. } while (0)
  210. #define PUSH_I32(v) PUSH(v, VALUE_TYPE_I32)
  211. #define PUSH_I64(v) PUSH(v, VALUE_TYPE_I64)
  212. #define PUSH_F32(v) PUSH(v, VALUE_TYPE_F32)
  213. #define PUSH_F64(v) PUSH(v, VALUE_TYPE_F64)
  214. #define PUSH_V128(v) PUSH(v, VALUE_TYPE_V128)
  215. #define PUSH_COND(v) PUSH(v, VALUE_TYPE_I1)
  216. #define PUSH_FUNCREF(v) PUSH(v, VALUE_TYPE_FUNCREF)
  217. #define PUSH_EXTERNREF(v) PUSH(v, VALUE_TYPE_EXTERNREF)
  218. #define TO_LLVM_TYPE(wasm_type) \
  219. wasm_type_to_llvm_type(&comp_ctx->basic_types, wasm_type)
  220. #define I32_TYPE comp_ctx->basic_types.int32_type
  221. #define I64_TYPE comp_ctx->basic_types.int64_type
  222. #define F32_TYPE comp_ctx->basic_types.float32_type
  223. #define F64_TYPE comp_ctx->basic_types.float64_type
  224. #define VOID_TYPE comp_ctx->basic_types.void_type
  225. #define INT1_TYPE comp_ctx->basic_types.int1_type
  226. #define INT8_TYPE comp_ctx->basic_types.int8_type
  227. #define INT16_TYPE comp_ctx->basic_types.int16_type
  228. #define MD_TYPE comp_ctx->basic_types.meta_data_type
  229. #define INT8_PTR_TYPE comp_ctx->basic_types.int8_ptr_type
  230. #define INT16_PTR_TYPE comp_ctx->basic_types.int16_ptr_type
  231. #define INT32_PTR_TYPE comp_ctx->basic_types.int32_ptr_type
  232. #define INT64_PTR_TYPE comp_ctx->basic_types.int64_ptr_type
  233. #define F32_PTR_TYPE comp_ctx->basic_types.float32_ptr_type
  234. #define F64_PTR_TYPE comp_ctx->basic_types.float64_ptr_type
  235. #define FUNC_REF_TYPE comp_ctx->basic_types.funcref_type
  236. #define EXTERN_REF_TYPE comp_ctx->basic_types.externref_type
  237. #define I32_CONST(v) LLVMConstInt(I32_TYPE, v, true)
  238. #define I64_CONST(v) LLVMConstInt(I64_TYPE, v, true)
  239. #define F32_CONST(v) LLVMConstReal(F32_TYPE, v)
  240. #define F64_CONST(v) LLVMConstReal(F64_TYPE, v)
  241. #define I8_CONST(v) LLVMConstInt(INT8_TYPE, v, true)
  242. #define I8_ZERO (comp_ctx->llvm_consts.i8_zero)
  243. #define I32_ZERO (comp_ctx->llvm_consts.i32_zero)
  244. #define I64_ZERO (comp_ctx->llvm_consts.i64_zero)
  245. #define F32_ZERO (comp_ctx->llvm_consts.f32_zero)
  246. #define F64_ZERO (comp_ctx->llvm_consts.f64_zero)
  247. #define I32_ONE (comp_ctx->llvm_consts.i32_one)
  248. #define I32_TWO (comp_ctx->llvm_consts.i32_two)
  249. #define I32_THREE (comp_ctx->llvm_consts.i32_three)
  250. #define I32_FOUR (comp_ctx->llvm_consts.i32_four)
  251. #define I32_FIVE (comp_ctx->llvm_consts.i32_five)
  252. #define I32_SIX (comp_ctx->llvm_consts.i32_six)
  253. #define I32_SEVEN (comp_ctx->llvm_consts.i32_seven)
  254. #define I32_EIGHT (comp_ctx->llvm_consts.i32_eight)
  255. #define I32_NEG_ONE (comp_ctx->llvm_consts.i32_neg_one)
  256. #define I64_NEG_ONE (comp_ctx->llvm_consts.i64_neg_one)
  257. #define I32_MIN (comp_ctx->llvm_consts.i32_min)
  258. #define I64_MIN (comp_ctx->llvm_consts.i64_min)
  259. #define I32_31 (comp_ctx->llvm_consts.i32_31)
  260. #define I32_32 (comp_ctx->llvm_consts.i32_32)
  261. #define I64_63 (comp_ctx->llvm_consts.i64_63)
  262. #define I64_64 (comp_ctx->llvm_consts.i64_64)
  263. #define V128_TYPE comp_ctx->basic_types.v128_type
  264. #define V128_PTR_TYPE comp_ctx->basic_types.v128_ptr_type
  265. #define V128_i8x16_TYPE comp_ctx->basic_types.i8x16_vec_type
  266. #define V128_i16x8_TYPE comp_ctx->basic_types.i16x8_vec_type
  267. #define V128_i32x4_TYPE comp_ctx->basic_types.i32x4_vec_type
  268. #define V128_i64x2_TYPE comp_ctx->basic_types.i64x2_vec_type
  269. #define V128_f32x4_TYPE comp_ctx->basic_types.f32x4_vec_type
  270. #define V128_f64x2_TYPE comp_ctx->basic_types.f64x2_vec_type
  271. #define V128_ZERO (comp_ctx->llvm_consts.v128_zero)
  272. #define V128_i8x16_ZERO (comp_ctx->llvm_consts.i8x16_vec_zero)
  273. #define V128_i16x8_ZERO (comp_ctx->llvm_consts.i16x8_vec_zero)
  274. #define V128_i32x4_ZERO (comp_ctx->llvm_consts.i32x4_vec_zero)
  275. #define V128_i64x2_ZERO (comp_ctx->llvm_consts.i64x2_vec_zero)
  276. #define V128_f32x4_ZERO (comp_ctx->llvm_consts.f32x4_vec_zero)
  277. #define V128_f64x2_ZERO (comp_ctx->llvm_consts.f64x2_vec_zero)
  278. #define REF_NULL (comp_ctx->llvm_consts.i32_neg_one)
  279. #define TO_V128_i8x16(v) LLVMBuildBitCast(comp_ctx->builder, v, \
  280. V128_i8x16_TYPE, "i8x16_val")
  281. #define TO_V128_i16x8(v) LLVMBuildBitCast(comp_ctx->builder, v, \
  282. V128_i16x8_TYPE, "i16x8_val")
  283. #define TO_V128_i32x4(v) LLVMBuildBitCast(comp_ctx->builder, v, \
  284. V128_i32x4_TYPE, "i32x4_val")
  285. #define TO_V128_i64x2(v) LLVMBuildBitCast(comp_ctx->builder, v, \
  286. V128_i64x2_TYPE, "i64x2_val")
  287. #define TO_V128_f32x4(v) LLVMBuildBitCast(comp_ctx->builder, v, \
  288. V128_f32x4_TYPE, "f32x4_val")
  289. #define TO_V128_f64x2(v) LLVMBuildBitCast(comp_ctx->builder, v, \
  290. V128_f64x2_TYPE, "f64x2_val")
  291. #define CHECK_LLVM_CONST(v) do { \
  292. if (!v) { \
  293. aot_set_last_error("create llvm const failed."); \
  294. goto fail; \
  295. } \
  296. } while (0)
  297. #define GET_AOT_FUNCTION(name, argc) do { \
  298. if (!(func_type = LLVMFunctionType(ret_type, param_types, \
  299. argc, false))) { \
  300. aot_set_last_error("llvm add function type failed."); \
  301. return false; \
  302. } \
  303. if (comp_ctx->is_jit_mode) { \
  304. /* JIT mode, call the function directly */ \
  305. if (!(func_ptr_type = LLVMPointerType(func_type, 0))) { \
  306. aot_set_last_error("llvm add pointer type failed."); \
  307. return false; \
  308. } \
  309. if (!(value = I64_CONST((uint64)(uintptr_t)name)) \
  310. || !(func = LLVMConstIntToPtr(value, func_ptr_type))) { \
  311. aot_set_last_error("create LLVM value failed."); \
  312. return false; \
  313. } \
  314. } \
  315. else { \
  316. char *func_name = #name; \
  317. /* AOT mode, delcare the function */ \
  318. if (!(func = LLVMGetNamedFunction(comp_ctx->module, func_name)) \
  319. && !(func = LLVMAddFunction(comp_ctx->module, \
  320. func_name, func_type))) { \
  321. aot_set_last_error("llvm add function failed."); \
  322. return false; \
  323. } \
  324. } \
  325. } while (0)
  326. bool
  327. aot_compile_wasm(AOTCompContext *comp_ctx);
  328. bool
  329. aot_emit_llvm_file(AOTCompContext *comp_ctx, const char *file_name);
  330. bool
  331. aot_emit_aot_file(AOTCompContext *comp_ctx,
  332. AOTCompData *comp_data,
  333. const char *file_name);
  334. uint8*
  335. aot_emit_aot_file_buf(AOTCompContext *comp_ctx,
  336. AOTCompData *comp_data,
  337. uint32 *p_aot_file_size);
  338. bool
  339. aot_emit_object_file(AOTCompContext *comp_ctx, char *file_name);
  340. uint8*
  341. aot_compile_wasm_file(const uint8 *wasm_file_buf, uint32 wasm_file_size,
  342. uint32 opt_level, uint32 size_level,
  343. char *error_buf, uint32 error_buf_size,
  344. uint32 *p_aot_file_size);
  345. #ifdef __cplusplus
  346. } /* end of extern "C" */
  347. #endif
  348. #endif /* end of _AOT_COMPILER_H_ */