asmxtensa.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  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. #include <stdio.h>
  27. #include <assert.h>
  28. #include "py/runtime.h"
  29. // wrapper around everything in this file
  30. #if MICROPY_EMIT_XTENSA || MICROPY_EMIT_INLINE_XTENSA || MICROPY_EMIT_XTENSAWIN
  31. #include "py/asmxtensa.h"
  32. #define WORD_SIZE (4)
  33. #define SIGNED_FIT8(x) ((((x) & 0xffffff80) == 0) || (((x) & 0xffffff80) == 0xffffff80))
  34. #define SIGNED_FIT12(x) ((((x) & 0xfffff800) == 0) || (((x) & 0xfffff800) == 0xfffff800))
  35. void asm_xtensa_end_pass(asm_xtensa_t *as) {
  36. as->num_const = as->cur_const;
  37. as->cur_const = 0;
  38. #if 0
  39. // make a hex dump of the machine code
  40. if (as->base.pass == MP_ASM_PASS_EMIT) {
  41. uint8_t *d = as->base.code_base;
  42. printf("XTENSA ASM:");
  43. for (int i = 0; i < ((as->base.code_size + 15) & ~15); ++i) {
  44. if (i % 16 == 0) {
  45. printf("\n%08x:", (uint32_t)&d[i]);
  46. }
  47. if (i % 2 == 0) {
  48. printf(" ");
  49. }
  50. printf("%02x", d[i]);
  51. }
  52. printf("\n");
  53. }
  54. #endif
  55. }
  56. void asm_xtensa_entry(asm_xtensa_t *as, int num_locals) {
  57. // jump over the constants
  58. asm_xtensa_op_j(as, as->num_const * WORD_SIZE + 4 - 4);
  59. mp_asm_base_get_cur_to_write_bytes(&as->base, 1); // padding/alignment byte
  60. as->const_table = (uint32_t *)mp_asm_base_get_cur_to_write_bytes(&as->base, as->num_const * 4);
  61. // adjust the stack-pointer to store a0, a12, a13, a14, a15 and locals, 16-byte aligned
  62. as->stack_adjust = (((ASM_XTENSA_NUM_REGS_SAVED + num_locals) * WORD_SIZE) + 15) & ~15;
  63. if (SIGNED_FIT8(-as->stack_adjust)) {
  64. asm_xtensa_op_addi(as, ASM_XTENSA_REG_A1, ASM_XTENSA_REG_A1, -as->stack_adjust);
  65. } else {
  66. asm_xtensa_op_movi(as, ASM_XTENSA_REG_A9, as->stack_adjust);
  67. asm_xtensa_op_sub(as, ASM_XTENSA_REG_A1, ASM_XTENSA_REG_A1, ASM_XTENSA_REG_A9);
  68. }
  69. // save return value (a0) and callee-save registers (a12, a13, a14, a15)
  70. asm_xtensa_op_s32i_n(as, ASM_XTENSA_REG_A0, ASM_XTENSA_REG_A1, 0);
  71. for (int i = 1; i < ASM_XTENSA_NUM_REGS_SAVED; ++i) {
  72. asm_xtensa_op_s32i_n(as, ASM_XTENSA_REG_A11 + i, ASM_XTENSA_REG_A1, i);
  73. }
  74. }
  75. void asm_xtensa_exit(asm_xtensa_t *as) {
  76. // restore registers
  77. for (int i = ASM_XTENSA_NUM_REGS_SAVED - 1; i >= 1; --i) {
  78. asm_xtensa_op_l32i_n(as, ASM_XTENSA_REG_A11 + i, ASM_XTENSA_REG_A1, i);
  79. }
  80. asm_xtensa_op_l32i_n(as, ASM_XTENSA_REG_A0, ASM_XTENSA_REG_A1, 0);
  81. // restore stack-pointer and return
  82. if (SIGNED_FIT8(as->stack_adjust)) {
  83. asm_xtensa_op_addi(as, ASM_XTENSA_REG_A1, ASM_XTENSA_REG_A1, as->stack_adjust);
  84. } else {
  85. asm_xtensa_op_movi(as, ASM_XTENSA_REG_A9, as->stack_adjust);
  86. asm_xtensa_op_add_n(as, ASM_XTENSA_REG_A1, ASM_XTENSA_REG_A1, ASM_XTENSA_REG_A9);
  87. }
  88. asm_xtensa_op_ret_n(as);
  89. }
  90. void asm_xtensa_entry_win(asm_xtensa_t *as, int num_locals) {
  91. // jump over the constants
  92. asm_xtensa_op_j(as, as->num_const * WORD_SIZE + 4 - 4);
  93. mp_asm_base_get_cur_to_write_bytes(&as->base, 1); // padding/alignment byte
  94. as->const_table = (uint32_t *)mp_asm_base_get_cur_to_write_bytes(&as->base, as->num_const * 4);
  95. as->stack_adjust = 32 + ((((ASM_XTENSA_NUM_REGS_SAVED_WIN + num_locals) * WORD_SIZE) + 15) & ~15);
  96. asm_xtensa_op_entry(as, ASM_XTENSA_REG_A1, as->stack_adjust);
  97. asm_xtensa_op_s32i_n(as, ASM_XTENSA_REG_A0, ASM_XTENSA_REG_A1, 0);
  98. }
  99. void asm_xtensa_exit_win(asm_xtensa_t *as) {
  100. asm_xtensa_op_l32i_n(as, ASM_XTENSA_REG_A0, ASM_XTENSA_REG_A1, 0);
  101. asm_xtensa_op_retw_n(as);
  102. }
  103. static uint32_t get_label_dest(asm_xtensa_t *as, uint label) {
  104. assert(label < as->base.max_num_labels);
  105. return as->base.label_offsets[label];
  106. }
  107. void asm_xtensa_op16(asm_xtensa_t *as, uint16_t op) {
  108. uint8_t *c = mp_asm_base_get_cur_to_write_bytes(&as->base, 2);
  109. if (c != NULL) {
  110. c[0] = op;
  111. c[1] = op >> 8;
  112. }
  113. }
  114. void asm_xtensa_op24(asm_xtensa_t *as, uint32_t op) {
  115. uint8_t *c = mp_asm_base_get_cur_to_write_bytes(&as->base, 3);
  116. if (c != NULL) {
  117. c[0] = op;
  118. c[1] = op >> 8;
  119. c[2] = op >> 16;
  120. }
  121. }
  122. void asm_xtensa_j_label(asm_xtensa_t *as, uint label) {
  123. uint32_t dest = get_label_dest(as, label);
  124. int32_t rel = dest - as->base.code_offset - 4;
  125. // we assume rel, as a signed int, fits in 18-bits
  126. asm_xtensa_op_j(as, rel);
  127. }
  128. void asm_xtensa_bccz_reg_label(asm_xtensa_t *as, uint cond, uint reg, uint label) {
  129. uint32_t dest = get_label_dest(as, label);
  130. int32_t rel = dest - as->base.code_offset - 4;
  131. if (as->base.pass == MP_ASM_PASS_EMIT && !SIGNED_FIT12(rel)) {
  132. printf("ERROR: xtensa bccz out of range\n");
  133. }
  134. asm_xtensa_op_bccz(as, cond, reg, rel);
  135. }
  136. void asm_xtensa_bcc_reg_reg_label(asm_xtensa_t *as, uint cond, uint reg1, uint reg2, uint label) {
  137. uint32_t dest = get_label_dest(as, label);
  138. int32_t rel = dest - as->base.code_offset - 4;
  139. if (as->base.pass == MP_ASM_PASS_EMIT && !SIGNED_FIT8(rel)) {
  140. printf("ERROR: xtensa bcc out of range\n");
  141. }
  142. asm_xtensa_op_bcc(as, cond, reg1, reg2, rel);
  143. }
  144. // convenience function; reg_dest must be different from reg_src[12]
  145. void asm_xtensa_setcc_reg_reg_reg(asm_xtensa_t *as, uint cond, uint reg_dest, uint reg_src1, uint reg_src2) {
  146. asm_xtensa_op_movi_n(as, reg_dest, 1);
  147. asm_xtensa_op_bcc(as, cond, reg_src1, reg_src2, 1);
  148. asm_xtensa_op_movi_n(as, reg_dest, 0);
  149. }
  150. size_t asm_xtensa_mov_reg_i32(asm_xtensa_t *as, uint reg_dest, uint32_t i32) {
  151. // load the constant
  152. uint32_t const_table_offset = (uint8_t *)as->const_table - as->base.code_base;
  153. size_t loc = const_table_offset + as->cur_const * WORD_SIZE;
  154. asm_xtensa_op_l32r(as, reg_dest, as->base.code_offset, loc);
  155. // store the constant in the table
  156. if (as->const_table != NULL) {
  157. as->const_table[as->cur_const] = i32;
  158. }
  159. ++as->cur_const;
  160. return loc;
  161. }
  162. void asm_xtensa_mov_reg_i32_optimised(asm_xtensa_t *as, uint reg_dest, uint32_t i32) {
  163. if (-32 <= (int)i32 && (int)i32 <= 95) {
  164. asm_xtensa_op_movi_n(as, reg_dest, i32);
  165. } else if (SIGNED_FIT12(i32)) {
  166. asm_xtensa_op_movi(as, reg_dest, i32);
  167. } else {
  168. asm_xtensa_mov_reg_i32(as, reg_dest, i32);
  169. }
  170. }
  171. void asm_xtensa_mov_local_reg(asm_xtensa_t *as, int local_num, uint reg_src) {
  172. asm_xtensa_op_s32i(as, reg_src, ASM_XTENSA_REG_A1, local_num);
  173. }
  174. void asm_xtensa_mov_reg_local(asm_xtensa_t *as, uint reg_dest, int local_num) {
  175. asm_xtensa_op_l32i(as, reg_dest, ASM_XTENSA_REG_A1, local_num);
  176. }
  177. void asm_xtensa_mov_reg_local_addr(asm_xtensa_t *as, uint reg_dest, int local_num) {
  178. uint off = local_num * WORD_SIZE;
  179. if (SIGNED_FIT8(off)) {
  180. asm_xtensa_op_addi(as, reg_dest, ASM_XTENSA_REG_A1, off);
  181. } else {
  182. asm_xtensa_op_movi(as, reg_dest, off);
  183. asm_xtensa_op_add_n(as, reg_dest, reg_dest, ASM_XTENSA_REG_A1);
  184. }
  185. }
  186. void asm_xtensa_mov_reg_pcrel(asm_xtensa_t *as, uint reg_dest, uint label) {
  187. // Get relative offset from PC
  188. uint32_t dest = get_label_dest(as, label);
  189. int32_t rel = dest - as->base.code_offset;
  190. rel -= 3 + 3; // account for 3 bytes of movi instruction, 3 bytes call0 adjustment
  191. asm_xtensa_op_movi(as, reg_dest, rel); // imm has 12-bit range
  192. // Use call0 to get PC+3 into a0
  193. // call0 destination must be aligned on 4 bytes:
  194. // - code_offset&3=0: off=0, pad=1
  195. // - code_offset&3=1: off=0, pad=0
  196. // - code_offset&3=2: off=1, pad=3
  197. // - code_offset&3=3: off=1, pad=2
  198. uint32_t off = as->base.code_offset >> 1 & 1;
  199. uint32_t pad = (5 - as->base.code_offset) & 3;
  200. asm_xtensa_op_call0(as, off);
  201. mp_asm_base_get_cur_to_write_bytes(&as->base, pad);
  202. // Add PC to relative offset
  203. asm_xtensa_op_add_n(as, reg_dest, reg_dest, ASM_XTENSA_REG_A0);
  204. }
  205. void asm_xtensa_l32i_optimised(asm_xtensa_t *as, uint reg_dest, uint reg_base, uint word_offset) {
  206. if (word_offset < 16) {
  207. asm_xtensa_op_l32i_n(as, reg_dest, reg_base, word_offset);
  208. } else if (word_offset < 256) {
  209. asm_xtensa_op_l32i(as, reg_dest, reg_base, word_offset);
  210. } else {
  211. mp_raise_msg(&mp_type_RuntimeError, MP_ERROR_TEXT("asm overflow"));
  212. }
  213. }
  214. void asm_xtensa_s32i_optimised(asm_xtensa_t *as, uint reg_src, uint reg_base, uint word_offset) {
  215. if (word_offset < 16) {
  216. asm_xtensa_op_s32i_n(as, reg_src, reg_base, word_offset);
  217. } else if (word_offset < 256) {
  218. asm_xtensa_op_s32i(as, reg_src, reg_base, word_offset);
  219. } else {
  220. mp_raise_msg(&mp_type_RuntimeError, MP_ERROR_TEXT("asm overflow"));
  221. }
  222. }
  223. void asm_xtensa_call_ind(asm_xtensa_t *as, uint idx) {
  224. asm_xtensa_l32i_optimised(as, ASM_XTENSA_REG_A0, ASM_XTENSA_REG_FUN_TABLE, idx);
  225. asm_xtensa_op_callx0(as, ASM_XTENSA_REG_A0);
  226. }
  227. void asm_xtensa_call_ind_win(asm_xtensa_t *as, uint idx) {
  228. asm_xtensa_l32i_optimised(as, ASM_XTENSA_REG_A8, ASM_XTENSA_REG_FUN_TABLE_WIN, idx);
  229. asm_xtensa_op_callx8(as, ASM_XTENSA_REG_A8);
  230. }
  231. #endif // MICROPY_EMIT_XTENSA || MICROPY_EMIT_INLINE_XTENSA || MICROPY_EMIT_XTENSAWIN