asmxtensa.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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/mpconfig.h"
  29. // wrapper around everything in this file
  30. #if MICROPY_EMIT_XTENSA || MICROPY_EMIT_INLINE_XTENSA
  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 and locals, 16-byte aligned
  62. as->stack_adjust = (((4 + num_locals) * WORD_SIZE) + 15) & ~15;
  63. asm_xtensa_op_addi(as, ASM_XTENSA_REG_A1, ASM_XTENSA_REG_A1, -as->stack_adjust);
  64. // save return value (a0) and callee-save registers (a12, a13, a14)
  65. asm_xtensa_op_s32i_n(as, ASM_XTENSA_REG_A0, ASM_XTENSA_REG_A1, 0);
  66. asm_xtensa_op_s32i_n(as, ASM_XTENSA_REG_A12, ASM_XTENSA_REG_A1, 1);
  67. asm_xtensa_op_s32i_n(as, ASM_XTENSA_REG_A13, ASM_XTENSA_REG_A1, 2);
  68. asm_xtensa_op_s32i_n(as, ASM_XTENSA_REG_A14, ASM_XTENSA_REG_A1, 3);
  69. }
  70. void asm_xtensa_exit(asm_xtensa_t *as) {
  71. // restore registers
  72. asm_xtensa_op_l32i_n(as, ASM_XTENSA_REG_A14, ASM_XTENSA_REG_A1, 3);
  73. asm_xtensa_op_l32i_n(as, ASM_XTENSA_REG_A13, ASM_XTENSA_REG_A1, 2);
  74. asm_xtensa_op_l32i_n(as, ASM_XTENSA_REG_A12, ASM_XTENSA_REG_A1, 1);
  75. asm_xtensa_op_l32i_n(as, ASM_XTENSA_REG_A0, ASM_XTENSA_REG_A1, 0);
  76. // restore stack-pointer and return
  77. asm_xtensa_op_addi(as, ASM_XTENSA_REG_A1, ASM_XTENSA_REG_A1, as->stack_adjust);
  78. asm_xtensa_op_ret_n(as);
  79. }
  80. STATIC uint32_t get_label_dest(asm_xtensa_t *as, uint label) {
  81. assert(label < as->base.max_num_labels);
  82. return as->base.label_offsets[label];
  83. }
  84. void asm_xtensa_op16(asm_xtensa_t *as, uint16_t op) {
  85. uint8_t *c = mp_asm_base_get_cur_to_write_bytes(&as->base, 2);
  86. if (c != NULL) {
  87. c[0] = op;
  88. c[1] = op >> 8;
  89. }
  90. }
  91. void asm_xtensa_op24(asm_xtensa_t *as, uint32_t op) {
  92. uint8_t *c = mp_asm_base_get_cur_to_write_bytes(&as->base, 3);
  93. if (c != NULL) {
  94. c[0] = op;
  95. c[1] = op >> 8;
  96. c[2] = op >> 16;
  97. }
  98. }
  99. void asm_xtensa_j_label(asm_xtensa_t *as, uint label) {
  100. uint32_t dest = get_label_dest(as, label);
  101. int32_t rel = dest - as->base.code_offset - 4;
  102. // we assume rel, as a signed int, fits in 18-bits
  103. asm_xtensa_op_j(as, rel);
  104. }
  105. void asm_xtensa_bccz_reg_label(asm_xtensa_t *as, uint cond, uint reg, uint label) {
  106. uint32_t dest = get_label_dest(as, label);
  107. int32_t rel = dest - as->base.code_offset - 4;
  108. if (as->base.pass == MP_ASM_PASS_EMIT && !SIGNED_FIT12(rel)) {
  109. printf("ERROR: xtensa bccz out of range\n");
  110. }
  111. asm_xtensa_op_bccz(as, cond, reg, rel);
  112. }
  113. void asm_xtensa_bcc_reg_reg_label(asm_xtensa_t *as, uint cond, uint reg1, uint reg2, uint label) {
  114. uint32_t dest = get_label_dest(as, label);
  115. int32_t rel = dest - as->base.code_offset - 4;
  116. if (as->base.pass == MP_ASM_PASS_EMIT && !SIGNED_FIT8(rel)) {
  117. printf("ERROR: xtensa bcc out of range\n");
  118. }
  119. asm_xtensa_op_bcc(as, cond, reg1, reg2, rel);
  120. }
  121. // convenience function; reg_dest must be different from reg_src[12]
  122. void asm_xtensa_setcc_reg_reg_reg(asm_xtensa_t *as, uint cond, uint reg_dest, uint reg_src1, uint reg_src2) {
  123. asm_xtensa_op_movi_n(as, reg_dest, 1);
  124. asm_xtensa_op_bcc(as, cond, reg_src1, reg_src2, 1);
  125. asm_xtensa_op_movi_n(as, reg_dest, 0);
  126. }
  127. void asm_xtensa_mov_reg_i32(asm_xtensa_t *as, uint reg_dest, uint32_t i32) {
  128. if (SIGNED_FIT12(i32)) {
  129. asm_xtensa_op_movi(as, reg_dest, i32);
  130. } else {
  131. // load the constant
  132. asm_xtensa_op_l32r(as, reg_dest, as->base.code_offset, 4 + as->cur_const * WORD_SIZE);
  133. // store the constant in the table
  134. if (as->const_table != NULL) {
  135. as->const_table[as->cur_const] = i32;
  136. }
  137. ++as->cur_const;
  138. }
  139. }
  140. void asm_xtensa_mov_local_reg(asm_xtensa_t *as, int local_num, uint reg_src) {
  141. asm_xtensa_op_s32i(as, reg_src, ASM_XTENSA_REG_A1, 4 + local_num);
  142. }
  143. void asm_xtensa_mov_reg_local(asm_xtensa_t *as, uint reg_dest, int local_num) {
  144. asm_xtensa_op_l32i(as, reg_dest, ASM_XTENSA_REG_A1, 4 + local_num);
  145. }
  146. void asm_xtensa_mov_reg_local_addr(asm_xtensa_t *as, uint reg_dest, int local_num) {
  147. asm_xtensa_op_mov_n(as, reg_dest, ASM_XTENSA_REG_A1);
  148. asm_xtensa_op_addi(as, reg_dest, reg_dest, (4 + local_num) * WORD_SIZE);
  149. }
  150. #endif // MICROPY_EMIT_XTENSA || MICROPY_EMIT_INLINE_XTENSA