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