asmxtensa.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. /*
  2. * This file is part of the MicroPython project, http://micropython.org/
  3. *
  4. * The MIT License (MIT)
  5. *
  6. * Copyright (c) 2016 Damien P. George
  7. *
  8. * Permission is hereby granted, free of charge, to any person obtaining a copy
  9. * of this software and associated documentation files (the "Software"), to deal
  10. * in the Software without restriction, including without limitation the rights
  11. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  12. * copies of the Software, and to permit persons to whom the Software is
  13. * furnished to do so, subject to the following conditions:
  14. *
  15. * The above copyright notice and this permission notice shall be included in
  16. * all copies or substantial portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  21. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  24. * THE SOFTWARE.
  25. */
  26. #ifndef MICROPY_INCLUDED_PY_ASMXTENSA_H
  27. #define MICROPY_INCLUDED_PY_ASMXTENSA_H
  28. #include "py/asmbase.h"
  29. // calling conventions:
  30. // up to 6 args in a2-a7
  31. // return value in a2
  32. // PC stored in a0
  33. // stack pointer is a1, stack full descending, is aligned to 16 bytes
  34. // callee save: a1, a12, a13, a14, a15
  35. // caller save: a3
  36. #define ASM_XTENSA_REG_A0 (0)
  37. #define ASM_XTENSA_REG_A1 (1)
  38. #define ASM_XTENSA_REG_A2 (2)
  39. #define ASM_XTENSA_REG_A3 (3)
  40. #define ASM_XTENSA_REG_A4 (4)
  41. #define ASM_XTENSA_REG_A5 (5)
  42. #define ASM_XTENSA_REG_A6 (6)
  43. #define ASM_XTENSA_REG_A7 (7)
  44. #define ASM_XTENSA_REG_A8 (8)
  45. #define ASM_XTENSA_REG_A9 (9)
  46. #define ASM_XTENSA_REG_A10 (10)
  47. #define ASM_XTENSA_REG_A11 (11)
  48. #define ASM_XTENSA_REG_A12 (12)
  49. #define ASM_XTENSA_REG_A13 (13)
  50. #define ASM_XTENSA_REG_A14 (14)
  51. #define ASM_XTENSA_REG_A15 (15)
  52. // for bccz
  53. #define ASM_XTENSA_CCZ_EQ (0)
  54. #define ASM_XTENSA_CCZ_NE (1)
  55. // for bcc and setcc
  56. #define ASM_XTENSA_CC_NONE (0)
  57. #define ASM_XTENSA_CC_EQ (1)
  58. #define ASM_XTENSA_CC_LT (2)
  59. #define ASM_XTENSA_CC_LTU (3)
  60. #define ASM_XTENSA_CC_ALL (4)
  61. #define ASM_XTENSA_CC_BC (5)
  62. #define ASM_XTENSA_CC_ANY (8)
  63. #define ASM_XTENSA_CC_NE (9)
  64. #define ASM_XTENSA_CC_GE (10)
  65. #define ASM_XTENSA_CC_GEU (11)
  66. #define ASM_XTENSA_CC_NALL (12)
  67. #define ASM_XTENSA_CC_BS (13)
  68. // macros for encoding instructions (little endian versions)
  69. #define ASM_XTENSA_ENCODE_RRR(op0, op1, op2, r, s, t) \
  70. ((((uint32_t)op2) << 20) | (((uint32_t)op1) << 16) | ((r) << 12) | ((s) << 8) | ((t) << 4) | (op0))
  71. #define ASM_XTENSA_ENCODE_RRI4(op0, op1, r, s, t, imm4) \
  72. (((imm4) << 20) | ((op1) << 16) | ((r) << 12) | ((s) << 8) | ((t) << 4) | (op0))
  73. #define ASM_XTENSA_ENCODE_RRI8(op0, r, s, t, imm8) \
  74. ((((uint32_t)imm8) << 16) | ((r) << 12) | ((s) << 8) | ((t) << 4) | (op0))
  75. #define ASM_XTENSA_ENCODE_RI16(op0, t, imm16) \
  76. (((imm16) << 8) | ((t) << 4) | (op0))
  77. #define ASM_XTENSA_ENCODE_RSR(op0, op1, op2, rs, t) \
  78. (((op2) << 20) | ((op1) << 16) | ((rs) << 8) | ((t) << 4) | (op0))
  79. #define ASM_XTENSA_ENCODE_CALL(op0, n, offset) \
  80. (((offset) << 6) | ((n) << 4) | (op0))
  81. #define ASM_XTENSA_ENCODE_CALLX(op0, op1, op2, r, s, m, n) \
  82. ((((uint32_t)op2) << 20) | (((uint32_t)op1) << 16) | ((r) << 12) | ((s) << 8) | ((m) << 6) | ((n) << 4) | (op0))
  83. #define ASM_XTENSA_ENCODE_BRI8(op0, r, s, m, n, imm8) \
  84. (((imm8) << 16) | ((r) << 12) | ((s) << 8) | ((m) << 6) | ((n) << 4) | (op0))
  85. #define ASM_XTENSA_ENCODE_BRI12(op0, s, m, n, imm12) \
  86. (((imm12) << 12) | ((s) << 8) | ((m) << 6) | ((n) << 4) | (op0))
  87. #define ASM_XTENSA_ENCODE_RRRN(op0, r, s, t) \
  88. (((r) << 12) | ((s) << 8) | ((t) << 4) | (op0))
  89. #define ASM_XTENSA_ENCODE_RI7(op0, s, imm7) \
  90. ((((imm7) & 0xf) << 12) | ((s) << 8) | ((imm7) & 0x70) | (op0))
  91. typedef struct _asm_xtensa_t {
  92. mp_asm_base_t base;
  93. uint32_t cur_const;
  94. uint32_t num_const;
  95. uint32_t *const_table;
  96. uint32_t stack_adjust;
  97. } asm_xtensa_t;
  98. void asm_xtensa_end_pass(asm_xtensa_t *as);
  99. void asm_xtensa_entry(asm_xtensa_t *as, int num_locals);
  100. void asm_xtensa_exit(asm_xtensa_t *as);
  101. void asm_xtensa_op16(asm_xtensa_t *as, uint16_t op);
  102. void asm_xtensa_op24(asm_xtensa_t *as, uint32_t op);
  103. // raw instructions
  104. static inline void asm_xtensa_op_add(asm_xtensa_t *as, uint reg_dest, uint reg_src_a, uint reg_src_b) {
  105. asm_xtensa_op24(as, ASM_XTENSA_ENCODE_RRR(0, 0, 8, reg_dest, reg_src_a, reg_src_b));
  106. }
  107. static inline void asm_xtensa_op_addi(asm_xtensa_t *as, uint reg_dest, uint reg_src, int imm8) {
  108. asm_xtensa_op24(as, ASM_XTENSA_ENCODE_RRI8(2, 12, reg_dest, reg_src, imm8 & 0xff));
  109. }
  110. static inline void asm_xtensa_op_and(asm_xtensa_t *as, uint reg_dest, uint reg_src_a, uint reg_src_b) {
  111. asm_xtensa_op24(as, ASM_XTENSA_ENCODE_RRR(0, 0, 1, reg_dest, reg_src_a, reg_src_b));
  112. }
  113. static inline void asm_xtensa_op_bcc(asm_xtensa_t *as, uint cond, uint reg_src1, uint reg_src2, int32_t rel8) {
  114. asm_xtensa_op24(as, ASM_XTENSA_ENCODE_RRI8(7, cond, reg_src1, reg_src2, rel8 & 0xff));
  115. }
  116. static inline void asm_xtensa_op_bccz(asm_xtensa_t *as, uint cond, uint reg_src, int32_t rel12) {
  117. asm_xtensa_op24(as, ASM_XTENSA_ENCODE_BRI12(6, reg_src, cond, 1, rel12 & 0xfff));
  118. }
  119. static inline void asm_xtensa_op_callx0(asm_xtensa_t *as, uint reg) {
  120. asm_xtensa_op24(as, ASM_XTENSA_ENCODE_CALLX(0, 0, 0, 0, reg, 3, 0));
  121. }
  122. static inline void asm_xtensa_op_j(asm_xtensa_t *as, int32_t rel18) {
  123. asm_xtensa_op24(as, ASM_XTENSA_ENCODE_CALL(6, 0, rel18 & 0x3ffff));
  124. }
  125. static inline void asm_xtensa_op_jx(asm_xtensa_t *as, uint reg) {
  126. asm_xtensa_op24(as, ASM_XTENSA_ENCODE_CALLX(0, 0, 0, 0, reg, 2, 2));
  127. }
  128. static inline void asm_xtensa_op_l8ui(asm_xtensa_t *as, uint reg_dest, uint reg_base, uint byte_offset) {
  129. asm_xtensa_op24(as, ASM_XTENSA_ENCODE_RRI8(2, 0, reg_base, reg_dest, byte_offset & 0xff));
  130. }
  131. static inline void asm_xtensa_op_l16ui(asm_xtensa_t *as, uint reg_dest, uint reg_base, uint half_word_offset) {
  132. asm_xtensa_op24(as, ASM_XTENSA_ENCODE_RRI8(2, 1, reg_base, reg_dest, half_word_offset & 0xff));
  133. }
  134. static inline void asm_xtensa_op_l32i(asm_xtensa_t *as, uint reg_dest, uint reg_base, uint word_offset) {
  135. asm_xtensa_op24(as, ASM_XTENSA_ENCODE_RRI8(2, 2, reg_base, reg_dest, word_offset & 0xff));
  136. }
  137. static inline void asm_xtensa_op_l32i_n(asm_xtensa_t *as, uint reg_dest, uint reg_base, uint word_offset) {
  138. asm_xtensa_op16(as, ASM_XTENSA_ENCODE_RRRN(8, word_offset & 0xf, reg_base, reg_dest));
  139. }
  140. static inline void asm_xtensa_op_l32r(asm_xtensa_t *as, uint reg_dest, uint32_t op_off, uint32_t dest_off) {
  141. asm_xtensa_op24(as, ASM_XTENSA_ENCODE_RI16(1, reg_dest, ((dest_off - ((op_off + 3) & ~3)) >> 2) & 0xffff));
  142. }
  143. static inline void asm_xtensa_op_mov_n(asm_xtensa_t *as, uint reg_dest, uint reg_src) {
  144. asm_xtensa_op16(as, ASM_XTENSA_ENCODE_RRRN(13, 0, reg_src, reg_dest));
  145. }
  146. static inline void asm_xtensa_op_movi(asm_xtensa_t *as, uint reg_dest, int32_t imm12) {
  147. asm_xtensa_op24(as, ASM_XTENSA_ENCODE_RRI8(2, 10, (imm12 >> 8) & 0xf, reg_dest, imm12 & 0xff));
  148. }
  149. static inline void asm_xtensa_op_movi_n(asm_xtensa_t *as, uint reg_dest, int imm4) {
  150. asm_xtensa_op16(as, ASM_XTENSA_ENCODE_RI7(12, reg_dest, imm4));
  151. }
  152. static inline void asm_xtensa_op_mull(asm_xtensa_t *as, uint reg_dest, uint reg_src_a, uint reg_src_b) {
  153. asm_xtensa_op24(as, ASM_XTENSA_ENCODE_RRR(0, 2, 8, reg_dest, reg_src_a, reg_src_b));
  154. }
  155. static inline void asm_xtensa_op_or(asm_xtensa_t *as, uint reg_dest, uint reg_src_a, uint reg_src_b) {
  156. asm_xtensa_op24(as, ASM_XTENSA_ENCODE_RRR(0, 0, 2, reg_dest, reg_src_a, reg_src_b));
  157. }
  158. static inline void asm_xtensa_op_ret_n(asm_xtensa_t *as) {
  159. asm_xtensa_op16(as, ASM_XTENSA_ENCODE_RRRN(13, 15, 0, 0));
  160. }
  161. static inline void asm_xtensa_op_s8i(asm_xtensa_t *as, uint reg_src, uint reg_base, uint byte_offset) {
  162. asm_xtensa_op24(as, ASM_XTENSA_ENCODE_RRI8(2, 4, reg_base, reg_src, byte_offset & 0xff));
  163. }
  164. static inline void asm_xtensa_op_s16i(asm_xtensa_t *as, uint reg_src, uint reg_base, uint half_word_offset) {
  165. asm_xtensa_op24(as, ASM_XTENSA_ENCODE_RRI8(2, 5, reg_base, reg_src, half_word_offset & 0xff));
  166. }
  167. static inline void asm_xtensa_op_s32i(asm_xtensa_t *as, uint reg_src, uint reg_base, uint word_offset) {
  168. asm_xtensa_op24(as, ASM_XTENSA_ENCODE_RRI8(2, 6, reg_base, reg_src, word_offset & 0xff));
  169. }
  170. static inline void asm_xtensa_op_s32i_n(asm_xtensa_t *as, uint reg_src, uint reg_base, uint word_offset) {
  171. asm_xtensa_op16(as, ASM_XTENSA_ENCODE_RRRN(9, word_offset & 0xf, reg_base, reg_src));
  172. }
  173. static inline void asm_xtensa_op_sll(asm_xtensa_t *as, uint reg_dest, uint reg_src) {
  174. asm_xtensa_op24(as, ASM_XTENSA_ENCODE_RRR(0, 1, 10, reg_dest, reg_src, 0));
  175. }
  176. static inline void asm_xtensa_op_sra(asm_xtensa_t *as, uint reg_dest, uint reg_src) {
  177. asm_xtensa_op24(as, ASM_XTENSA_ENCODE_RRR(0, 1, 11, reg_dest, 0, reg_src));
  178. }
  179. static inline void asm_xtensa_op_ssl(asm_xtensa_t *as, uint reg_src) {
  180. asm_xtensa_op24(as, ASM_XTENSA_ENCODE_RRR(0, 0, 4, 1, reg_src, 0));
  181. }
  182. static inline void asm_xtensa_op_ssr(asm_xtensa_t *as, uint reg_src) {
  183. asm_xtensa_op24(as, ASM_XTENSA_ENCODE_RRR(0, 0, 4, 0, reg_src, 0));
  184. }
  185. static inline void asm_xtensa_op_sub(asm_xtensa_t *as, uint reg_dest, uint reg_src_a, uint reg_src_b) {
  186. asm_xtensa_op24(as, ASM_XTENSA_ENCODE_RRR(0, 0, 12, reg_dest, reg_src_a, reg_src_b));
  187. }
  188. static inline void asm_xtensa_op_xor(asm_xtensa_t *as, uint reg_dest, uint reg_src_a, uint reg_src_b) {
  189. asm_xtensa_op24(as, ASM_XTENSA_ENCODE_RRR(0, 0, 3, reg_dest, reg_src_a, reg_src_b));
  190. }
  191. // convenience functions
  192. void asm_xtensa_j_label(asm_xtensa_t *as, uint label);
  193. void asm_xtensa_bccz_reg_label(asm_xtensa_t *as, uint cond, uint reg, uint label);
  194. void asm_xtensa_bcc_reg_reg_label(asm_xtensa_t *as, uint cond, uint reg1, uint reg2, uint label);
  195. void asm_xtensa_setcc_reg_reg_reg(asm_xtensa_t *as, uint cond, uint reg_dest, uint reg_src1, uint reg_src2);
  196. void asm_xtensa_mov_reg_i32(asm_xtensa_t *as, uint reg_dest, uint32_t i32);
  197. void asm_xtensa_mov_local_reg(asm_xtensa_t *as, int local_num, uint reg_src);
  198. void asm_xtensa_mov_reg_local(asm_xtensa_t *as, uint reg_dest, int local_num);
  199. void asm_xtensa_mov_reg_local_addr(asm_xtensa_t *as, uint reg_dest, int local_num);
  200. #if GENERIC_ASM_API
  201. // The following macros provide a (mostly) arch-independent API to
  202. // generate native code, and are used by the native emitter.
  203. #define ASM_WORD_SIZE (4)
  204. #define REG_RET ASM_XTENSA_REG_A2
  205. #define REG_ARG_1 ASM_XTENSA_REG_A2
  206. #define REG_ARG_2 ASM_XTENSA_REG_A3
  207. #define REG_ARG_3 ASM_XTENSA_REG_A4
  208. #define REG_ARG_4 ASM_XTENSA_REG_A5
  209. #define REG_ARG_5 ASM_XTENSA_REG_A6
  210. #define REG_TEMP0 ASM_XTENSA_REG_A2
  211. #define REG_TEMP1 ASM_XTENSA_REG_A3
  212. #define REG_TEMP2 ASM_XTENSA_REG_A4
  213. #define REG_LOCAL_1 ASM_XTENSA_REG_A12
  214. #define REG_LOCAL_2 ASM_XTENSA_REG_A13
  215. #define REG_LOCAL_3 ASM_XTENSA_REG_A14
  216. #define REG_LOCAL_NUM (3)
  217. #define ASM_T asm_xtensa_t
  218. #define ASM_END_PASS asm_xtensa_end_pass
  219. #define ASM_ENTRY asm_xtensa_entry
  220. #define ASM_EXIT asm_xtensa_exit
  221. #define ASM_JUMP asm_xtensa_j_label
  222. #define ASM_JUMP_IF_REG_ZERO(as, reg, label) \
  223. asm_xtensa_bccz_reg_label(as, ASM_XTENSA_CCZ_EQ, reg, label)
  224. #define ASM_JUMP_IF_REG_NONZERO(as, reg, label) \
  225. asm_xtensa_bccz_reg_label(as, ASM_XTENSA_CCZ_NE, reg, label)
  226. #define ASM_JUMP_IF_REG_EQ(as, reg1, reg2, label) \
  227. asm_xtensa_bcc_reg_reg_label(as, ASM_XTENSA_CC_EQ, reg1, reg2, label)
  228. #define ASM_CALL_IND(as, ptr, idx) \
  229. do { \
  230. asm_xtensa_mov_reg_i32(as, ASM_XTENSA_REG_A0, (uint32_t)ptr); \
  231. asm_xtensa_op_callx0(as, ASM_XTENSA_REG_A0); \
  232. } while (0)
  233. #define ASM_MOV_LOCAL_REG(as, local_num, reg_src) asm_xtensa_mov_local_reg((as), (local_num), (reg_src))
  234. #define ASM_MOV_REG_IMM(as, reg_dest, imm) asm_xtensa_mov_reg_i32((as), (reg_dest), (imm))
  235. #define ASM_MOV_REG_ALIGNED_IMM(as, reg_dest, imm) asm_xtensa_mov_reg_i32((as), (reg_dest), (imm))
  236. #define ASM_MOV_REG_LOCAL(as, reg_dest, local_num) asm_xtensa_mov_reg_local((as), (reg_dest), (local_num))
  237. #define ASM_MOV_REG_REG(as, reg_dest, reg_src) asm_xtensa_op_mov_n((as), (reg_dest), (reg_src))
  238. #define ASM_MOV_REG_LOCAL_ADDR(as, reg_dest, local_num) asm_xtensa_mov_reg_local_addr((as), (reg_dest), (local_num))
  239. #define ASM_LSL_REG_REG(as, reg_dest, reg_shift) \
  240. do { \
  241. asm_xtensa_op_ssl((as), (reg_shift)); \
  242. asm_xtensa_op_sll((as), (reg_dest), (reg_dest)); \
  243. } while (0)
  244. #define ASM_ASR_REG_REG(as, reg_dest, reg_shift) \
  245. do { \
  246. asm_xtensa_op_ssr((as), (reg_shift)); \
  247. asm_xtensa_op_sra((as), (reg_dest), (reg_dest)); \
  248. } while (0)
  249. #define ASM_OR_REG_REG(as, reg_dest, reg_src) asm_xtensa_op_or((as), (reg_dest), (reg_dest), (reg_src))
  250. #define ASM_XOR_REG_REG(as, reg_dest, reg_src) asm_xtensa_op_xor((as), (reg_dest), (reg_dest), (reg_src))
  251. #define ASM_AND_REG_REG(as, reg_dest, reg_src) asm_xtensa_op_and((as), (reg_dest), (reg_dest), (reg_src))
  252. #define ASM_ADD_REG_REG(as, reg_dest, reg_src) asm_xtensa_op_add((as), (reg_dest), (reg_dest), (reg_src))
  253. #define ASM_SUB_REG_REG(as, reg_dest, reg_src) asm_xtensa_op_sub((as), (reg_dest), (reg_dest), (reg_src))
  254. #define ASM_MUL_REG_REG(as, reg_dest, reg_src) asm_xtensa_op_mull((as), (reg_dest), (reg_dest), (reg_src))
  255. #define ASM_LOAD_REG_REG_OFFSET(as, reg_dest, reg_base, word_offset) asm_xtensa_op_l32i_n((as), (reg_dest), (reg_base), (word_offset))
  256. #define ASM_LOAD8_REG_REG(as, reg_dest, reg_base) asm_xtensa_op_l8ui((as), (reg_dest), (reg_base), 0)
  257. #define ASM_LOAD16_REG_REG(as, reg_dest, reg_base) asm_xtensa_op_l16ui((as), (reg_dest), (reg_base), 0)
  258. #define ASM_LOAD32_REG_REG(as, reg_dest, reg_base) asm_xtensa_op_l32i_n((as), (reg_dest), (reg_base), 0)
  259. #define ASM_STORE_REG_REG_OFFSET(as, reg_dest, reg_base, word_offset) asm_xtensa_op_s32i_n((as), (reg_dest), (reg_base), (word_offset))
  260. #define ASM_STORE8_REG_REG(as, reg_src, reg_base) asm_xtensa_op_s8i((as), (reg_src), (reg_base), 0)
  261. #define ASM_STORE16_REG_REG(as, reg_src, reg_base) asm_xtensa_op_s16i((as), (reg_src), (reg_base), 0)
  262. #define ASM_STORE32_REG_REG(as, reg_src, reg_base) asm_xtensa_op_s32i_n((as), (reg_src), (reg_base), 0)
  263. #endif // GENERIC_ASM_API
  264. #endif // MICROPY_INCLUDED_PY_ASMXTENSA_H