jit_frontend.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514
  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_inst_reg(JitFrame *frame, uint32 mem_idx);
  170. JitReg
  171. get_cur_page_count_reg(JitFrame *frame, uint32 mem_idx);
  172. JitReg
  173. get_memory_data_reg(JitFrame *frame, uint32 mem_idx);
  174. JitReg
  175. get_memory_data_end_reg(JitFrame *frame, uint32 mem_idx);
  176. JitReg
  177. get_mem_bound_check_1byte_reg(JitFrame *frame, uint32 mem_idx);
  178. JitReg
  179. get_mem_bound_check_2bytes_reg(JitFrame *frame, uint32 mem_idx);
  180. JitReg
  181. get_mem_bound_check_4bytes_reg(JitFrame *frame, uint32 mem_idx);
  182. JitReg
  183. get_mem_bound_check_8bytes_reg(JitFrame *frame, uint32 mem_idx);
  184. JitReg
  185. get_mem_bound_check_16bytes_reg(JitFrame *frame, uint32 mem_idx);
  186. JitReg
  187. get_table_elems_reg(JitFrame *frame, uint32 table_idx);
  188. JitReg
  189. get_table_cur_size_reg(JitFrame *frame, uint32 table_idx);
  190. void
  191. clear_fixed_virtual_regs(JitFrame *frame);
  192. void
  193. clear_memory_regs(JitFrame *frame);
  194. void
  195. clear_table_regs(JitFrame *frame);
  196. /**
  197. * Get the offset from frame pointer to the n-th local variable slot.
  198. *
  199. * @param n the index to the local variable array
  200. *
  201. * @return the offset from frame pointer to the local variable slot
  202. */
  203. static inline unsigned
  204. offset_of_local(unsigned n)
  205. {
  206. return offsetof(WASMInterpFrame, lp) + n * 4;
  207. }
  208. /**
  209. * Generate instruction to load an integer from the frame.
  210. *
  211. * This and the below gen_load_X functions generate instructions to
  212. * load values from the frame into registers if the values have not
  213. * been loaded yet.
  214. *
  215. * @param frame the frame information
  216. * @param n slot index to the local variable array
  217. *
  218. * @return register holding the loaded value
  219. */
  220. JitReg
  221. gen_load_i32(JitFrame *frame, unsigned n);
  222. /**
  223. * Generate instruction to load a i64 integer from the frame.
  224. *
  225. * @param frame the frame information
  226. * @param n slot index to the local variable array
  227. *
  228. * @return register holding the loaded value
  229. */
  230. JitReg
  231. gen_load_i64(JitFrame *frame, unsigned n);
  232. /**
  233. * Generate instruction to load a floating point value from the frame.
  234. *
  235. * @param frame the frame information
  236. * @param n slot index to the local variable array
  237. *
  238. * @return register holding the loaded value
  239. */
  240. JitReg
  241. gen_load_f32(JitFrame *frame, unsigned n);
  242. /**
  243. * Generate instruction to load a double value from the frame.
  244. *
  245. * @param frame the frame information
  246. * @param n slot index to the local variable array
  247. *
  248. * @return register holding the loaded value
  249. */
  250. JitReg
  251. gen_load_f64(JitFrame *frame, unsigned n);
  252. /**
  253. * Generate instructions to commit computation result to the frame.
  254. * The general principle is to only commit values that will be used
  255. * through the frame.
  256. *
  257. * @param frame the frame information
  258. * @param begin the begin value slot to commit
  259. * @param end the end value slot to commit
  260. */
  261. void
  262. gen_commit_values(JitFrame *frame, JitValueSlot *begin, JitValueSlot *end);
  263. /**
  264. * Generate instructions to commit SP and IP pointers to the frame.
  265. *
  266. * @param frame the frame information
  267. */
  268. void
  269. gen_commit_sp_ip(JitFrame *frame);
  270. /**
  271. * Generate commit instructions for the block end.
  272. *
  273. * @param frame the frame information
  274. */
  275. static inline void
  276. gen_commit_for_branch(JitFrame *frame)
  277. {
  278. gen_commit_values(frame, frame->lp, frame->sp);
  279. }
  280. /**
  281. * Generate commit instructions for exception checks.
  282. *
  283. * @param frame the frame information
  284. */
  285. static inline void
  286. gen_commit_for_exception(JitFrame *frame)
  287. {
  288. gen_commit_values(frame, frame->lp, frame->lp + frame->max_locals);
  289. gen_commit_sp_ip(frame);
  290. }
  291. /**
  292. * Generate commit instructions to commit all status.
  293. *
  294. * @param frame the frame information
  295. */
  296. static inline void
  297. gen_commit_for_all(JitFrame *frame)
  298. {
  299. gen_commit_values(frame, frame->lp, frame->sp);
  300. gen_commit_sp_ip(frame);
  301. }
  302. static inline void
  303. clear_values(JitFrame *frame)
  304. {
  305. size_t total_size =
  306. sizeof(JitValueSlot) * (frame->max_locals + frame->max_stacks);
  307. memset(frame->lp, 0, total_size);
  308. frame->committed_sp = NULL;
  309. frame->committed_ip = NULL;
  310. clear_fixed_virtual_regs(frame);
  311. }
  312. static inline void
  313. push_i32(JitFrame *frame, JitReg value)
  314. {
  315. frame->sp->reg = value;
  316. frame->sp->dirty = 1;
  317. frame->sp++;
  318. }
  319. static inline void
  320. push_i64(JitFrame *frame, JitReg value)
  321. {
  322. frame->sp->reg = value;
  323. frame->sp->dirty = 1;
  324. frame->sp++;
  325. frame->sp->reg = value;
  326. frame->sp->dirty = 1;
  327. frame->sp++;
  328. }
  329. static inline void
  330. push_f32(JitFrame *frame, JitReg value)
  331. {
  332. push_i32(frame, value);
  333. }
  334. static inline void
  335. push_f64(JitFrame *frame, JitReg value)
  336. {
  337. push_i64(frame, value);
  338. }
  339. static inline JitReg
  340. pop_i32(JitFrame *frame)
  341. {
  342. frame->sp--;
  343. return gen_load_i32(frame, frame->sp - frame->lp);
  344. }
  345. static inline JitReg
  346. pop_i64(JitFrame *frame)
  347. {
  348. frame->sp -= 2;
  349. return gen_load_i64(frame, frame->sp - frame->lp);
  350. }
  351. static inline JitReg
  352. pop_f32(JitFrame *frame)
  353. {
  354. frame->sp--;
  355. return gen_load_f32(frame, frame->sp - frame->lp);
  356. }
  357. static inline JitReg
  358. pop_f64(JitFrame *frame)
  359. {
  360. frame->sp -= 2;
  361. return gen_load_f64(frame, frame->sp - frame->lp);
  362. }
  363. static inline void
  364. pop(JitFrame *frame, int n)
  365. {
  366. frame->sp -= n;
  367. memset(frame->sp, 0, n * sizeof(*frame->sp));
  368. }
  369. static inline JitReg
  370. local_i32(JitFrame *frame, int n)
  371. {
  372. return gen_load_i32(frame, n);
  373. }
  374. static inline JitReg
  375. local_i64(JitFrame *frame, int n)
  376. {
  377. return gen_load_i64(frame, n);
  378. }
  379. static inline JitReg
  380. local_f32(JitFrame *frame, int n)
  381. {
  382. return gen_load_f32(frame, n);
  383. }
  384. static inline JitReg
  385. local_f64(JitFrame *frame, int n)
  386. {
  387. return gen_load_f64(frame, n);
  388. }
  389. static void
  390. set_local_i32(JitFrame *frame, int n, JitReg val)
  391. {
  392. frame->lp[n].reg = val;
  393. frame->lp[n].dirty = 1;
  394. }
  395. static void
  396. set_local_i64(JitFrame *frame, int n, JitReg val)
  397. {
  398. frame->lp[n].reg = val;
  399. frame->lp[n].dirty = 1;
  400. frame->lp[n + 1].reg = val;
  401. frame->lp[n + 1].dirty = 1;
  402. }
  403. static inline void
  404. set_local_f32(JitFrame *frame, int n, JitReg val)
  405. {
  406. set_local_i32(frame, n, val);
  407. }
  408. static inline void
  409. set_local_f64(JitFrame *frame, int n, JitReg val)
  410. {
  411. set_local_i64(frame, n, val);
  412. }
  413. #define POP(jit_value, value_type) \
  414. do { \
  415. if (!jit_cc_pop_value(cc, value_type, &jit_value)) \
  416. goto fail; \
  417. } while (0)
  418. #define POP_I32(v) POP(v, VALUE_TYPE_I32)
  419. #define POP_I64(v) POP(v, VALUE_TYPE_I64)
  420. #define POP_F32(v) POP(v, VALUE_TYPE_F32)
  421. #define POP_F64(v) POP(v, VALUE_TYPE_F64)
  422. #define POP_FUNCREF(v) POP(v, VALUE_TYPE_FUNCREF)
  423. #define POP_EXTERNREF(v) POP(v, VALUE_TYPE_EXTERNREF)
  424. #define PUSH(jit_value, value_type) \
  425. do { \
  426. if (!jit_value) \
  427. goto fail; \
  428. if (!jit_cc_push_value(cc, value_type, jit_value)) \
  429. goto fail; \
  430. } while (0)
  431. #define PUSH_I32(v) PUSH(v, VALUE_TYPE_I32)
  432. #define PUSH_I64(v) PUSH(v, VALUE_TYPE_I64)
  433. #define PUSH_F32(v) PUSH(v, VALUE_TYPE_F32)
  434. #define PUSH_F64(v) PUSH(v, VALUE_TYPE_F64)
  435. #define PUSH_FUNCREF(v) PUSH(v, VALUE_TYPE_FUNCREF)
  436. #define PUSH_EXTERNREF(v) PUSH(v, VALUE_TYPE_EXTERNREF)
  437. #ifdef __cplusplus
  438. }
  439. #endif
  440. #endif