jit_frontend.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508
  1. /*
  2. * Copyright (C) 2021 Intel Corporation. All rights reserved.
  3. * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. */
  5. #ifndef _JIT_FRONTEND_H_
  6. #define _JIT_FRONTEND_H_
  7. #include "jit_utils.h"
  8. #include "jit_ir.h"
  9. #include "../interpreter/wasm_interp.h"
  10. #if WASM_ENABLE_AOT != 0
  11. #include "../aot/aot_runtime.h"
  12. #endif
  13. #ifdef __cplusplus
  14. extern "C" {
  15. #endif
  16. #if WASM_ENABLE_AOT == 0
  17. typedef enum IntCond {
  18. INT_EQZ = 0,
  19. INT_EQ,
  20. INT_NE,
  21. INT_LT_S,
  22. INT_LT_U,
  23. INT_GT_S,
  24. INT_GT_U,
  25. INT_LE_S,
  26. INT_LE_U,
  27. INT_GE_S,
  28. INT_GE_U
  29. } IntCond;
  30. typedef enum FloatCond {
  31. FLOAT_EQ = 0,
  32. FLOAT_NE,
  33. FLOAT_LT,
  34. FLOAT_GT,
  35. FLOAT_LE,
  36. FLOAT_GE,
  37. FLOAT_UNO
  38. } FloatCond;
  39. #else
  40. #define IntCond AOTIntCond
  41. #define FloatCond AOTFloatCond
  42. #endif
  43. typedef enum IntArithmetic {
  44. INT_ADD = 0,
  45. INT_SUB,
  46. INT_MUL,
  47. INT_DIV_S,
  48. INT_DIV_U,
  49. INT_REM_S,
  50. INT_REM_U
  51. } IntArithmetic;
  52. typedef enum V128Arithmetic {
  53. V128_ADD = 0,
  54. V128_SUB,
  55. V128_MUL,
  56. V128_DIV,
  57. V128_NEG,
  58. V128_MIN,
  59. V128_MAX,
  60. } V128Arithmetic;
  61. typedef enum IntBitwise {
  62. INT_AND = 0,
  63. INT_OR,
  64. INT_XOR,
  65. } IntBitwise;
  66. typedef enum V128Bitwise {
  67. V128_NOT,
  68. V128_AND,
  69. V128_ANDNOT,
  70. V128_OR,
  71. V128_XOR,
  72. V128_BITSELECT,
  73. } V128Bitwise;
  74. typedef enum IntShift {
  75. INT_SHL = 0,
  76. INT_SHR_S,
  77. INT_SHR_U,
  78. INT_ROTL,
  79. INT_ROTR
  80. } IntShift;
  81. typedef enum FloatMath {
  82. FLOAT_ABS = 0,
  83. FLOAT_NEG,
  84. FLOAT_CEIL,
  85. FLOAT_FLOOR,
  86. FLOAT_TRUNC,
  87. FLOAT_NEAREST,
  88. FLOAT_SQRT
  89. } FloatMath;
  90. typedef enum FloatArithmetic {
  91. FLOAT_ADD = 0,
  92. FLOAT_SUB,
  93. FLOAT_MUL,
  94. FLOAT_DIV,
  95. FLOAT_MIN,
  96. FLOAT_MAX,
  97. } FloatArithmetic;
  98. #if WASM_ENABLE_SHARED_MEMORY != 0
  99. typedef enum AtomicRMWBinOp {
  100. AtomicRMWBinOpAdd,
  101. AtomicRMWBinOpSub,
  102. AtomicRMWBinOpAnd,
  103. AtomicRMWBinOpOr,
  104. AtomicRMWBinOpXor,
  105. AtomicRMWBinOpXchg
  106. } AtomicRMWBinOp;
  107. #endif
  108. /**
  109. * Translate instructions in a function. The translated block must
  110. * end with a branch instruction whose targets are offsets relating to
  111. * the end bcip of the translated block, which are integral constants.
  112. * If a target of a branch is really a constant value (which should be
  113. * rare), put it into a register and then jump to the register instead
  114. * of using the constant value directly in the target. In the
  115. * translation process, don't create any new labels. The code bcip of
  116. * the begin and end of the translated block is stored in the
  117. * jit_annl_begin_bcip and jit_annl_end_bcip annotations of the label
  118. * of the block, which must be the same as the bcips used in
  119. * profiling.
  120. *
  121. * NOTE: the function must explicitly set SP to correct value when the
  122. * entry's bcip is the function's entry address.
  123. *
  124. * @param cc containing compilation context of generated IR
  125. * @param entry entry of the basic block to be translated. If its
  126. * value is NULL, the function will clean up any pass local data that
  127. * might be created previously.
  128. * @param is_reached a bitmap recording which bytecode has been
  129. * reached as a block entry
  130. *
  131. * @return IR block containing translated instructions if succeeds,
  132. * NULL otherwise
  133. */
  134. JitBasicBlock *
  135. jit_frontend_translate_func(JitCompContext *cc);
  136. /**
  137. * Lower the IR of the given compilation context.
  138. *
  139. * @param cc the compilation context
  140. *
  141. * @return true if succeeds, false otherwise
  142. */
  143. bool
  144. jit_frontend_lower(JitCompContext *cc);
  145. uint32
  146. jit_frontend_get_jitted_return_addr_offset();
  147. uint32
  148. jit_frontend_get_global_data_offset(const WASMModule *module,
  149. uint32 global_idx);
  150. uint32
  151. jit_frontend_get_table_inst_offset(const WASMModule *module, uint32 tbl_idx);
  152. uint32
  153. jit_frontend_get_module_inst_extra_offset(const WASMModule *module);
  154. JitReg
  155. get_module_inst_reg(JitFrame *frame);
  156. JitReg
  157. get_module_reg(JitFrame *frame);
  158. JitReg
  159. get_import_func_ptrs_reg(JitFrame *frame);
  160. JitReg
  161. get_fast_jit_func_ptrs_reg(JitFrame *frame);
  162. JitReg
  163. get_func_type_indexes_reg(JitFrame *frame);
  164. JitReg
  165. get_aux_stack_bound_reg(JitFrame *frame);
  166. JitReg
  167. get_aux_stack_bottom_reg(JitFrame *frame);
  168. JitReg
  169. get_memory_data_reg(JitFrame *frame, uint32 mem_idx);
  170. JitReg
  171. get_memory_data_end_reg(JitFrame *frame, uint32 mem_idx);
  172. JitReg
  173. get_mem_bound_check_1byte_reg(JitFrame *frame, uint32 mem_idx);
  174. JitReg
  175. get_mem_bound_check_2bytes_reg(JitFrame *frame, uint32 mem_idx);
  176. JitReg
  177. get_mem_bound_check_4bytes_reg(JitFrame *frame, uint32 mem_idx);
  178. JitReg
  179. get_mem_bound_check_8bytes_reg(JitFrame *frame, uint32 mem_idx);
  180. JitReg
  181. get_mem_bound_check_16bytes_reg(JitFrame *frame, uint32 mem_idx);
  182. JitReg
  183. get_table_elems_reg(JitFrame *frame, uint32 table_idx);
  184. JitReg
  185. get_table_cur_size_reg(JitFrame *frame, uint32 table_idx);
  186. void
  187. clear_fixed_virtual_regs(JitFrame *frame);
  188. void
  189. clear_memory_regs(JitFrame *frame);
  190. void
  191. clear_table_regs(JitFrame *frame);
  192. /**
  193. * Get the offset from frame pointer to the n-th local variable slot.
  194. *
  195. * @param n the index to the local variable array
  196. *
  197. * @return the offset from frame pointer to the local variable slot
  198. */
  199. static inline unsigned
  200. offset_of_local(unsigned n)
  201. {
  202. return offsetof(WASMInterpFrame, lp) + n * 4;
  203. }
  204. /**
  205. * Generate instruction to load an integer from the frame.
  206. *
  207. * This and the below gen_load_X functions generate instructions to
  208. * load values from the frame into registers if the values have not
  209. * been loaded yet.
  210. *
  211. * @param frame the frame information
  212. * @param n slot index to the local variable array
  213. *
  214. * @return register holding the loaded value
  215. */
  216. JitReg
  217. gen_load_i32(JitFrame *frame, unsigned n);
  218. /**
  219. * Generate instruction to load a i64 integer from the frame.
  220. *
  221. * @param frame the frame information
  222. * @param n slot index to the local variable array
  223. *
  224. * @return register holding the loaded value
  225. */
  226. JitReg
  227. gen_load_i64(JitFrame *frame, unsigned n);
  228. /**
  229. * Generate instruction to load a floating point value from the frame.
  230. *
  231. * @param frame the frame information
  232. * @param n slot index to the local variable array
  233. *
  234. * @return register holding the loaded value
  235. */
  236. JitReg
  237. gen_load_f32(JitFrame *frame, unsigned n);
  238. /**
  239. * Generate instruction to load a double value from the frame.
  240. *
  241. * @param frame the frame information
  242. * @param n slot index to the local variable array
  243. *
  244. * @return register holding the loaded value
  245. */
  246. JitReg
  247. gen_load_f64(JitFrame *frame, unsigned n);
  248. /**
  249. * Generate instructions to commit computation result to the frame.
  250. * The general principle is to only commit values that will be used
  251. * through the frame.
  252. *
  253. * @param frame the frame information
  254. * @param begin the begin value slot to commit
  255. * @param end the end value slot to commit
  256. */
  257. void
  258. gen_commit_values(JitFrame *frame, JitValueSlot *begin, JitValueSlot *end);
  259. /**
  260. * Generate instructions to commit SP and IP pointers to the frame.
  261. *
  262. * @param frame the frame information
  263. */
  264. void
  265. gen_commit_sp_ip(JitFrame *frame);
  266. /**
  267. * Generate commit instructions for the block end.
  268. *
  269. * @param frame the frame information
  270. */
  271. static inline void
  272. gen_commit_for_branch(JitFrame *frame)
  273. {
  274. gen_commit_values(frame, frame->lp, frame->sp);
  275. }
  276. /**
  277. * Generate commit instructions for exception checks.
  278. *
  279. * @param frame the frame information
  280. */
  281. static inline void
  282. gen_commit_for_exception(JitFrame *frame)
  283. {
  284. gen_commit_values(frame, frame->lp, frame->lp + frame->max_locals);
  285. gen_commit_sp_ip(frame);
  286. }
  287. /**
  288. * Generate commit instructions to commit all status.
  289. *
  290. * @param frame the frame information
  291. */
  292. static inline void
  293. gen_commit_for_all(JitFrame *frame)
  294. {
  295. gen_commit_values(frame, frame->lp, frame->sp);
  296. gen_commit_sp_ip(frame);
  297. }
  298. static inline void
  299. clear_values(JitFrame *frame)
  300. {
  301. size_t total_size =
  302. sizeof(JitValueSlot) * (frame->max_locals + frame->max_stacks);
  303. memset(frame->lp, 0, total_size);
  304. frame->committed_sp = NULL;
  305. frame->committed_ip = NULL;
  306. clear_fixed_virtual_regs(frame);
  307. }
  308. static inline void
  309. push_i32(JitFrame *frame, JitReg value)
  310. {
  311. frame->sp->reg = value;
  312. frame->sp->dirty = 1;
  313. frame->sp++;
  314. }
  315. static inline void
  316. push_i64(JitFrame *frame, JitReg value)
  317. {
  318. frame->sp->reg = value;
  319. frame->sp->dirty = 1;
  320. frame->sp++;
  321. frame->sp->reg = value;
  322. frame->sp->dirty = 1;
  323. frame->sp++;
  324. }
  325. static inline void
  326. push_f32(JitFrame *frame, JitReg value)
  327. {
  328. push_i32(frame, value);
  329. }
  330. static inline void
  331. push_f64(JitFrame *frame, JitReg value)
  332. {
  333. push_i64(frame, value);
  334. }
  335. static inline JitReg
  336. pop_i32(JitFrame *frame)
  337. {
  338. frame->sp--;
  339. return gen_load_i32(frame, frame->sp - frame->lp);
  340. }
  341. static inline JitReg
  342. pop_i64(JitFrame *frame)
  343. {
  344. frame->sp -= 2;
  345. return gen_load_i64(frame, frame->sp - frame->lp);
  346. }
  347. static inline JitReg
  348. pop_f32(JitFrame *frame)
  349. {
  350. frame->sp--;
  351. return gen_load_f32(frame, frame->sp - frame->lp);
  352. }
  353. static inline JitReg
  354. pop_f64(JitFrame *frame)
  355. {
  356. frame->sp -= 2;
  357. return gen_load_f64(frame, frame->sp - frame->lp);
  358. }
  359. static inline void
  360. pop(JitFrame *frame, int n)
  361. {
  362. frame->sp -= n;
  363. memset(frame->sp, 0, n * sizeof(*frame->sp));
  364. }
  365. static inline JitReg
  366. local_i32(JitFrame *frame, int n)
  367. {
  368. return gen_load_i32(frame, n);
  369. }
  370. static inline JitReg
  371. local_i64(JitFrame *frame, int n)
  372. {
  373. return gen_load_i64(frame, n);
  374. }
  375. static inline JitReg
  376. local_f32(JitFrame *frame, int n)
  377. {
  378. return gen_load_f32(frame, n);
  379. }
  380. static inline JitReg
  381. local_f64(JitFrame *frame, int n)
  382. {
  383. return gen_load_f64(frame, n);
  384. }
  385. static void
  386. set_local_i32(JitFrame *frame, int n, JitReg val)
  387. {
  388. frame->lp[n].reg = val;
  389. frame->lp[n].dirty = 1;
  390. }
  391. static void
  392. set_local_i64(JitFrame *frame, int n, JitReg val)
  393. {
  394. frame->lp[n].reg = val;
  395. frame->lp[n].dirty = 1;
  396. frame->lp[n + 1].reg = val;
  397. frame->lp[n + 1].dirty = 1;
  398. }
  399. static inline void
  400. set_local_f32(JitFrame *frame, int n, JitReg val)
  401. {
  402. set_local_i32(frame, n, val);
  403. }
  404. static inline void
  405. set_local_f64(JitFrame *frame, int n, JitReg val)
  406. {
  407. set_local_i64(frame, n, val);
  408. }
  409. #define POP(jit_value, value_type) \
  410. do { \
  411. if (!jit_cc_pop_value(cc, value_type, &jit_value)) \
  412. goto fail; \
  413. } while (0)
  414. #define POP_I32(v) POP(v, VALUE_TYPE_I32)
  415. #define POP_I64(v) POP(v, VALUE_TYPE_I64)
  416. #define POP_F32(v) POP(v, VALUE_TYPE_F32)
  417. #define POP_F64(v) POP(v, VALUE_TYPE_F64)
  418. #define POP_FUNCREF(v) POP(v, VALUE_TYPE_FUNCREF)
  419. #define POP_EXTERNREF(v) POP(v, VALUE_TYPE_EXTERNREF)
  420. #define PUSH(jit_value, value_type) \
  421. do { \
  422. if (!jit_value) \
  423. goto fail; \
  424. if (!jit_cc_push_value(cc, value_type, jit_value)) \
  425. goto fail; \
  426. } while (0)
  427. #define PUSH_I32(v) PUSH(v, VALUE_TYPE_I32)
  428. #define PUSH_I64(v) PUSH(v, VALUE_TYPE_I64)
  429. #define PUSH_F32(v) PUSH(v, VALUE_TYPE_F32)
  430. #define PUSH_F64(v) PUSH(v, VALUE_TYPE_F64)
  431. #define PUSH_FUNCREF(v) PUSH(v, VALUE_TYPE_FUNCREF)
  432. #define PUSH_EXTERNREF(v) PUSH(v, VALUE_TYPE_EXTERNREF)
  433. #ifdef __cplusplus
  434. }
  435. #endif
  436. #endif