|
| 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 | + |
| 27 | +#include <stdio.h> |
| 28 | +#include <assert.h> |
| 29 | + |
| 30 | +#include "py/mpconfig.h" |
| 31 | + |
| 32 | +// wrapper around everything in this file |
| 33 | +#if MICROPY_EMIT_XTENSA |
| 34 | + |
| 35 | +#include "py/asmxtensa.h" |
| 36 | + |
| 37 | +#define WORD_SIZE (4) |
| 38 | +#define SIGNED_FIT8(x) ((((x) & 0xffffff80) == 0) || (((x) & 0xffffff80) == 0xffffff80)) |
| 39 | +#define SIGNED_FIT12(x) ((((x) & 0xfffff800) == 0) || (((x) & 0xfffff800) == 0xfffff800)) |
| 40 | + |
| 41 | +void asm_xtensa_end_pass(asm_xtensa_t *as) { |
| 42 | + as->num_const = as->cur_const; |
| 43 | + as->cur_const = 0; |
| 44 | + |
| 45 | + #if 0 |
| 46 | + // make a hex dump of the machine code |
| 47 | + if (as->base.pass == MP_ASM_PASS_EMIT) { |
| 48 | + uint8_t *d = as->base.code_base; |
| 49 | + printf("XTENSA ASM:"); |
| 50 | + for (int i = 0; i < ((as->base.code_size + 15) & ~15); ++i) { |
| 51 | + if (i % 16 == 0) { |
| 52 | + printf("\n%08x:", (uint32_t)&d[i]); |
| 53 | + } |
| 54 | + if (i % 2 == 0) { |
| 55 | + printf(" "); |
| 56 | + } |
| 57 | + printf("%02x", d[i]); |
| 58 | + } |
| 59 | + printf("\n"); |
| 60 | + } |
| 61 | + #endif |
| 62 | +} |
| 63 | + |
| 64 | +void asm_xtensa_entry(asm_xtensa_t *as, int num_locals) { |
| 65 | + // jump over the constants |
| 66 | + asm_xtensa_op_j(as, as->num_const * WORD_SIZE + 4 - 4); |
| 67 | + mp_asm_base_get_cur_to_write_bytes(&as->base, 1); // padding/alignment byte |
| 68 | + as->const_table = (uint32_t*)mp_asm_base_get_cur_to_write_bytes(&as->base, as->num_const * 4); |
| 69 | + |
| 70 | + // adjust the stack-pointer to store a0, a12, a13, a14 and locals, 16-byte aligned |
| 71 | + as->stack_adjust = (((4 + num_locals) * WORD_SIZE) + 15) & ~15; |
| 72 | + asm_xtensa_op_addi(as, ASM_XTENSA_REG_A1, ASM_XTENSA_REG_A1, -as->stack_adjust); |
| 73 | + |
| 74 | + // save return value (a0) and callee-save registers (a12, a13, a14) |
| 75 | + asm_xtensa_op_s32i_n(as, ASM_XTENSA_REG_A0, ASM_XTENSA_REG_A1, 0); |
| 76 | + asm_xtensa_op_s32i_n(as, ASM_XTENSA_REG_A12, ASM_XTENSA_REG_A1, 1); |
| 77 | + asm_xtensa_op_s32i_n(as, ASM_XTENSA_REG_A13, ASM_XTENSA_REG_A1, 2); |
| 78 | + asm_xtensa_op_s32i_n(as, ASM_XTENSA_REG_A14, ASM_XTENSA_REG_A1, 3); |
| 79 | +} |
| 80 | + |
| 81 | +void asm_xtensa_exit(asm_xtensa_t *as) { |
| 82 | + // restore registers |
| 83 | + asm_xtensa_op_l32i_n(as, ASM_XTENSA_REG_A14, ASM_XTENSA_REG_A1, 3); |
| 84 | + asm_xtensa_op_l32i_n(as, ASM_XTENSA_REG_A13, ASM_XTENSA_REG_A1, 2); |
| 85 | + asm_xtensa_op_l32i_n(as, ASM_XTENSA_REG_A12, ASM_XTENSA_REG_A1, 1); |
| 86 | + asm_xtensa_op_l32i_n(as, ASM_XTENSA_REG_A0, ASM_XTENSA_REG_A1, 0); |
| 87 | + |
| 88 | + // restore stack-pointer and return |
| 89 | + asm_xtensa_op_addi(as, ASM_XTENSA_REG_A1, ASM_XTENSA_REG_A1, as->stack_adjust); |
| 90 | + asm_xtensa_op_ret_n(as); |
| 91 | +} |
| 92 | + |
| 93 | +STATIC uint32_t get_label_dest(asm_xtensa_t *as, uint label) { |
| 94 | + assert(label < as->base.max_num_labels); |
| 95 | + return as->base.label_offsets[label]; |
| 96 | +} |
| 97 | + |
| 98 | +void asm_xtensa_op16(asm_xtensa_t *as, uint16_t op) { |
| 99 | + uint8_t *c = mp_asm_base_get_cur_to_write_bytes(&as->base, 2); |
| 100 | + c[0] = op; |
| 101 | + c[1] = op >> 8; |
| 102 | +} |
| 103 | + |
| 104 | +void asm_xtensa_op24(asm_xtensa_t *as, uint32_t op) { |
| 105 | + uint8_t *c = mp_asm_base_get_cur_to_write_bytes(&as->base, 3); |
| 106 | + c[0] = op; |
| 107 | + c[1] = op >> 8; |
| 108 | + c[2] = op >> 16; |
| 109 | +} |
| 110 | + |
| 111 | +void asm_xtensa_j_label(asm_xtensa_t *as, uint label) { |
| 112 | + uint32_t dest = get_label_dest(as, label); |
| 113 | + int32_t rel = dest - as->base.code_offset - 4; |
| 114 | + // we assume rel, as a signed int, fits in 18-bits |
| 115 | + asm_xtensa_op_j(as, rel); |
| 116 | +} |
| 117 | + |
| 118 | +void asm_xtensa_bccz_reg_label(asm_xtensa_t *as, uint cond, uint reg, uint label) { |
| 119 | + uint32_t dest = get_label_dest(as, label); |
| 120 | + int32_t rel = dest - as->base.code_offset - 4; |
| 121 | + if (as->base.pass == MP_ASM_PASS_EMIT && !SIGNED_FIT12(rel)) { |
| 122 | + printf("ERROR: xtensa bccz out of range\n"); |
| 123 | + } |
| 124 | + asm_xtensa_op_bccz(as, cond, reg, rel); |
| 125 | +} |
| 126 | + |
| 127 | +void asm_xtensa_bcc_reg_reg_label(asm_xtensa_t *as, uint cond, uint reg1, uint reg2, uint label) { |
| 128 | + uint32_t dest = get_label_dest(as, label); |
| 129 | + int32_t rel = dest - as->base.code_offset - 4; |
| 130 | + if (as->base.pass == MP_ASM_PASS_EMIT && !SIGNED_FIT8(rel)) { |
| 131 | + printf("ERROR: xtensa bcc out of range\n"); |
| 132 | + } |
| 133 | + asm_xtensa_op_bcc(as, cond, reg1, reg2, rel); |
| 134 | +} |
| 135 | + |
| 136 | +// convenience function; reg_dest must be different from reg_src[12] |
| 137 | +void asm_xtensa_setcc_reg_reg_reg(asm_xtensa_t *as, uint cond, uint reg_dest, uint reg_src1, uint reg_src2) { |
| 138 | + asm_xtensa_op_movi_n(as, reg_dest, 1); |
| 139 | + asm_xtensa_op_bcc(as, cond, reg_src1, reg_src2, 1); |
| 140 | + asm_xtensa_op_movi_n(as, reg_dest, 0); |
| 141 | +} |
| 142 | + |
| 143 | +void asm_xtensa_mov_reg_i32(asm_xtensa_t *as, uint reg_dest, uint32_t i32) { |
| 144 | + if (SIGNED_FIT12(i32)) { |
| 145 | + asm_xtensa_op_movi(as, reg_dest, i32); |
| 146 | + } else { |
| 147 | + // load the constant |
| 148 | + asm_xtensa_op_l32r(as, reg_dest, as->base.code_offset, 4 + as->cur_const * WORD_SIZE); |
| 149 | + // store the constant in the table |
| 150 | + if (as->base.pass == MP_ASM_PASS_EMIT) { |
| 151 | + as->const_table[as->cur_const] = i32; |
| 152 | + } |
| 153 | + ++as->cur_const; |
| 154 | + } |
| 155 | +} |
| 156 | + |
| 157 | +void asm_xtensa_mov_local_reg(asm_xtensa_t *as, int local_num, uint reg_src) { |
| 158 | + asm_xtensa_op_s32i(as, reg_src, ASM_XTENSA_REG_A1, 4 + local_num); |
| 159 | +} |
| 160 | + |
| 161 | +void asm_xtensa_mov_reg_local(asm_xtensa_t *as, uint reg_dest, int local_num) { |
| 162 | + asm_xtensa_op_l32i(as, reg_dest, ASM_XTENSA_REG_A1, 4 + local_num); |
| 163 | +} |
| 164 | + |
| 165 | +void asm_xtensa_mov_reg_local_addr(asm_xtensa_t *as, uint reg_dest, int local_num) { |
| 166 | + asm_xtensa_op_mov_n(as, reg_dest, ASM_XTENSA_REG_A1); |
| 167 | + asm_xtensa_op_addi(as, reg_dest, reg_dest, (4 + local_num) * WORD_SIZE); |
| 168 | +} |
| 169 | + |
| 170 | +#endif // MICROPY_EMIT_XTENSA |
0 commit comments