jit_frontend.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497
  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. /**
  99. * Translate instructions in a function. The translated block must
  100. * end with a branch instruction whose targets are offsets relating to
  101. * the end bcip of the translated block, which are integral constants.
  102. * If a target of a branch is really a constant value (which should be
  103. * rare), put it into a register and then jump to the register instead
  104. * of using the constant value directly in the target. In the
  105. * translation process, don't create any new labels. The code bcip of
  106. * the begin and end of the translated block is stored in the
  107. * jit_annl_begin_bcip and jit_annl_end_bcip annotations of the label
  108. * of the block, which must be the same as the bcips used in
  109. * profiling.
  110. *
  111. * NOTE: the function must explicitly set SP to correct value when the
  112. * entry's bcip is the function's entry address.
  113. *
  114. * @param cc containing compilation context of generated IR
  115. * @param entry entry of the basic block to be translated. If its
  116. * value is NULL, the function will clean up any pass local data that
  117. * might be created previously.
  118. * @param is_reached a bitmap recording which bytecode has been
  119. * reached as a block entry
  120. *
  121. * @return IR block containing translated instructions if succeeds,
  122. * NULL otherwise
  123. */
  124. JitBasicBlock *
  125. jit_frontend_translate_func(JitCompContext *cc);
  126. /**
  127. * Lower the IR of the given compilation context.
  128. *
  129. * @param cc the compilation context
  130. *
  131. * @return true if succeeds, false otherwise
  132. */
  133. bool
  134. jit_frontend_lower(JitCompContext *cc);
  135. uint32
  136. jit_frontend_get_jitted_return_addr_offset();
  137. uint32
  138. jit_frontend_get_global_data_offset(const WASMModule *module,
  139. uint32 global_idx);
  140. uint32
  141. jit_frontend_get_table_inst_offset(const WASMModule *module, uint32 tbl_idx);
  142. uint32
  143. jit_frontend_get_module_inst_extra_offset(const WASMModule *module);
  144. JitReg
  145. get_module_inst_reg(JitFrame *frame);
  146. JitReg
  147. get_module_reg(JitFrame *frame);
  148. JitReg
  149. get_import_func_ptrs_reg(JitFrame *frame);
  150. JitReg
  151. get_fast_jit_func_ptrs_reg(JitFrame *frame);
  152. JitReg
  153. get_func_type_indexes_reg(JitFrame *frame);
  154. JitReg
  155. get_aux_stack_bound_reg(JitFrame *frame);
  156. JitReg
  157. get_aux_stack_bottom_reg(JitFrame *frame);
  158. JitReg
  159. get_memory_data_reg(JitFrame *frame, uint32 mem_idx);
  160. JitReg
  161. get_memory_data_end_reg(JitFrame *frame, uint32 mem_idx);
  162. JitReg
  163. get_mem_bound_check_1byte_reg(JitFrame *frame, uint32 mem_idx);
  164. JitReg
  165. get_mem_bound_check_2bytes_reg(JitFrame *frame, uint32 mem_idx);
  166. JitReg
  167. get_mem_bound_check_4bytes_reg(JitFrame *frame, uint32 mem_idx);
  168. JitReg
  169. get_mem_bound_check_8bytes_reg(JitFrame *frame, uint32 mem_idx);
  170. JitReg
  171. get_mem_bound_check_16bytes_reg(JitFrame *frame, uint32 mem_idx);
  172. JitReg
  173. get_table_elems_reg(JitFrame *frame, uint32 table_idx);
  174. JitReg
  175. get_table_cur_size_reg(JitFrame *frame, uint32 table_idx);
  176. void
  177. clear_fixed_virtual_regs(JitFrame *frame);
  178. void
  179. clear_memory_regs(JitFrame *frame);
  180. void
  181. clear_table_regs(JitFrame *frame);
  182. /**
  183. * Get the offset from frame pointer to the n-th local variable slot.
  184. *
  185. * @param n the index to the local variable array
  186. *
  187. * @return the offset from frame pointer to the local variable slot
  188. */
  189. static inline unsigned
  190. offset_of_local(unsigned n)
  191. {
  192. return offsetof(WASMInterpFrame, lp) + n * 4;
  193. }
  194. /**
  195. * Generate instruction to load an integer from the frame.
  196. *
  197. * This and the below gen_load_X functions generate instructions to
  198. * load values from the frame into registers if the values have not
  199. * been loaded yet.
  200. *
  201. * @param frame the frame information
  202. * @param n slot index to the local variable array
  203. *
  204. * @return register holding the loaded value
  205. */
  206. JitReg
  207. gen_load_i32(JitFrame *frame, unsigned n);
  208. /**
  209. * Generate instruction to load a i64 integer from the frame.
  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_i64(JitFrame *frame, unsigned n);
  218. /**
  219. * Generate instruction to load a floating point value 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_f32(JitFrame *frame, unsigned n);
  228. /**
  229. * Generate instruction to load a double 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_f64(JitFrame *frame, unsigned n);
  238. /**
  239. * Generate instructions to commit computation result to the frame.
  240. * The general principle is to only commit values that will be used
  241. * through the frame.
  242. *
  243. * @param frame the frame information
  244. * @param begin the begin value slot to commit
  245. * @param end the end value slot to commit
  246. */
  247. void
  248. gen_commit_values(JitFrame *frame, JitValueSlot *begin, JitValueSlot *end);
  249. /**
  250. * Generate instructions to commit SP and IP pointers to the frame.
  251. *
  252. * @param frame the frame information
  253. */
  254. void
  255. gen_commit_sp_ip(JitFrame *frame);
  256. /**
  257. * Generate commit instructions for the block end.
  258. *
  259. * @param frame the frame information
  260. */
  261. static inline void
  262. gen_commit_for_branch(JitFrame *frame)
  263. {
  264. gen_commit_values(frame, frame->lp, frame->sp);
  265. }
  266. /**
  267. * Generate commit instructions for exception checks.
  268. *
  269. * @param frame the frame information
  270. */
  271. static inline void
  272. gen_commit_for_exception(JitFrame *frame)
  273. {
  274. gen_commit_values(frame, frame->lp, frame->lp + frame->max_locals);
  275. gen_commit_sp_ip(frame);
  276. }
  277. /**
  278. * Generate commit instructions to commit all status.
  279. *
  280. * @param frame the frame information
  281. */
  282. static inline void
  283. gen_commit_for_all(JitFrame *frame)
  284. {
  285. gen_commit_values(frame, frame->lp, frame->sp);
  286. gen_commit_sp_ip(frame);
  287. }
  288. static inline void
  289. clear_values(JitFrame *frame)
  290. {
  291. size_t total_size =
  292. sizeof(JitValueSlot) * (frame->max_locals + frame->max_stacks);
  293. memset(frame->lp, 0, total_size);
  294. frame->committed_sp = NULL;
  295. frame->committed_ip = NULL;
  296. clear_fixed_virtual_regs(frame);
  297. }
  298. static inline void
  299. push_i32(JitFrame *frame, JitReg value)
  300. {
  301. frame->sp->reg = value;
  302. frame->sp->dirty = 1;
  303. frame->sp++;
  304. }
  305. static inline void
  306. push_i64(JitFrame *frame, JitReg value)
  307. {
  308. frame->sp->reg = value;
  309. frame->sp->dirty = 1;
  310. frame->sp++;
  311. frame->sp->reg = value;
  312. frame->sp->dirty = 1;
  313. frame->sp++;
  314. }
  315. static inline void
  316. push_f32(JitFrame *frame, JitReg value)
  317. {
  318. push_i32(frame, value);
  319. }
  320. static inline void
  321. push_f64(JitFrame *frame, JitReg value)
  322. {
  323. push_i64(frame, value);
  324. }
  325. static inline JitReg
  326. pop_i32(JitFrame *frame)
  327. {
  328. frame->sp--;
  329. return gen_load_i32(frame, frame->sp - frame->lp);
  330. }
  331. static inline JitReg
  332. pop_i64(JitFrame *frame)
  333. {
  334. frame->sp -= 2;
  335. return gen_load_i64(frame, frame->sp - frame->lp);
  336. }
  337. static inline JitReg
  338. pop_f32(JitFrame *frame)
  339. {
  340. frame->sp--;
  341. return gen_load_f32(frame, frame->sp - frame->lp);
  342. }
  343. static inline JitReg
  344. pop_f64(JitFrame *frame)
  345. {
  346. frame->sp -= 2;
  347. return gen_load_f64(frame, frame->sp - frame->lp);
  348. }
  349. static inline void
  350. pop(JitFrame *frame, int n)
  351. {
  352. frame->sp -= n;
  353. memset(frame->sp, 0, n * sizeof(*frame->sp));
  354. }
  355. static inline JitReg
  356. local_i32(JitFrame *frame, int n)
  357. {
  358. return gen_load_i32(frame, n);
  359. }
  360. static inline JitReg
  361. local_i64(JitFrame *frame, int n)
  362. {
  363. return gen_load_i64(frame, n);
  364. }
  365. static inline JitReg
  366. local_f32(JitFrame *frame, int n)
  367. {
  368. return gen_load_f32(frame, n);
  369. }
  370. static inline JitReg
  371. local_f64(JitFrame *frame, int n)
  372. {
  373. return gen_load_f64(frame, n);
  374. }
  375. static void
  376. set_local_i32(JitFrame *frame, int n, JitReg val)
  377. {
  378. frame->lp[n].reg = val;
  379. frame->lp[n].dirty = 1;
  380. }
  381. static void
  382. set_local_i64(JitFrame *frame, int n, JitReg val)
  383. {
  384. frame->lp[n].reg = val;
  385. frame->lp[n].dirty = 1;
  386. frame->lp[n + 1].reg = val;
  387. frame->lp[n + 1].dirty = 1;
  388. }
  389. static inline void
  390. set_local_f32(JitFrame *frame, int n, JitReg val)
  391. {
  392. set_local_i32(frame, n, val);
  393. }
  394. static inline void
  395. set_local_f64(JitFrame *frame, int n, JitReg val)
  396. {
  397. set_local_i64(frame, n, val);
  398. }
  399. #define POP(jit_value, value_type) \
  400. do { \
  401. if (!jit_cc_pop_value(cc, value_type, &jit_value)) \
  402. goto fail; \
  403. } while (0)
  404. #define POP_I32(v) POP(v, VALUE_TYPE_I32)
  405. #define POP_I64(v) POP(v, VALUE_TYPE_I64)
  406. #define POP_F32(v) POP(v, VALUE_TYPE_F32)
  407. #define POP_F64(v) POP(v, VALUE_TYPE_F64)
  408. #define POP_FUNCREF(v) POP(v, VALUE_TYPE_FUNCREF)
  409. #define POP_EXTERNREF(v) POP(v, VALUE_TYPE_EXTERNREF)
  410. #define PUSH(jit_value, value_type) \
  411. do { \
  412. if (!jit_value) \
  413. goto fail; \
  414. if (!jit_cc_push_value(cc, value_type, jit_value)) \
  415. goto fail; \
  416. } while (0)
  417. #define PUSH_I32(v) PUSH(v, VALUE_TYPE_I32)
  418. #define PUSH_I64(v) PUSH(v, VALUE_TYPE_I64)
  419. #define PUSH_F32(v) PUSH(v, VALUE_TYPE_F32)
  420. #define PUSH_F64(v) PUSH(v, VALUE_TYPE_F64)
  421. #define PUSH_FUNCREF(v) PUSH(v, VALUE_TYPE_FUNCREF)
  422. #define PUSH_EXTERNREF(v) PUSH(v, VALUE_TYPE_EXTERNREF)
  423. #ifdef __cplusplus
  424. }
  425. #endif
  426. #endif