2016-12-09 00:32:30 -05:00
|
|
|
/*
|
|
|
|
* This file is part of the MicroPython project, http://micropython.org/
|
|
|
|
*
|
|
|
|
* The MIT License (MIT)
|
|
|
|
*
|
2020-06-03 18:40:05 -04:00
|
|
|
* SPDX-FileCopyrightText: Copyright (c) 2016 Damien P. George
|
2016-12-09 00:32:30 -05:00
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
* THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <assert.h>
|
|
|
|
|
|
|
|
#include "py/mpconfig.h"
|
|
|
|
|
|
|
|
// wrapper around everything in this file
|
py: Add inline Xtensa assembler.
This patch adds the MICROPY_EMIT_INLINE_XTENSA option, which, when
enabled, allows the @micropython.asm_xtensa decorator to be used.
The following opcodes are currently supported (ax is a register, a0-a15):
ret_n()
callx0(ax)
j(label)
jx(ax)
beqz(ax, label)
bnez(ax, label)
mov(ax, ay)
movi(ax, imm) # imm can be full 32-bit, uses l32r if needed
and_(ax, ay, az)
or_(ax, ay, az)
xor(ax, ay, az)
add(ax, ay, az)
sub(ax, ay, az)
mull(ax, ay, az)
l8ui(ax, ay, imm)
l16ui(ax, ay, imm)
l32i(ax, ay, imm)
s8i(ax, ay, imm)
s16i(ax, ay, imm)
s32i(ax, ay, imm)
l16si(ax, ay, imm)
addi(ax, ay, imm)
ball(ax, ay, label)
bany(ax, ay, label)
bbc(ax, ay, label)
bbs(ax, ay, label)
beq(ax, ay, label)
bge(ax, ay, label)
bgeu(ax, ay, label)
blt(ax, ay, label)
bnall(ax, ay, label)
bne(ax, ay, label)
bnone(ax, ay, label)
Upon entry to the assembly function the registers a0, a12, a13, a14 are
pushed to the stack and the stack pointer (a1) decreased by 16. Upon
exit, these registers and the stack pointer are restored, and ret.n is
executed to return to the caller (caller address is in a0).
Note that the ABI for the Xtensa emitters is non-windowing.
2016-12-09 01:03:33 -05:00
|
|
|
#if MICROPY_EMIT_XTENSA || MICROPY_EMIT_INLINE_XTENSA
|
2016-12-09 00:32:30 -05:00
|
|
|
|
|
|
|
#include "py/asmxtensa.h"
|
|
|
|
|
|
|
|
#define WORD_SIZE (4)
|
|
|
|
#define SIGNED_FIT8(x) ((((x) & 0xffffff80) == 0) || (((x) & 0xffffff80) == 0xffffff80))
|
|
|
|
#define SIGNED_FIT12(x) ((((x) & 0xfffff800) == 0) || (((x) & 0xfffff800) == 0xfffff800))
|
2018-09-15 10:43:24 -04:00
|
|
|
#define NUM_REGS_SAVED (5)
|
2016-12-09 00:32:30 -05:00
|
|
|
|
|
|
|
void asm_xtensa_end_pass(asm_xtensa_t *as) {
|
|
|
|
as->num_const = as->cur_const;
|
|
|
|
as->cur_const = 0;
|
|
|
|
|
|
|
|
#if 0
|
|
|
|
// make a hex dump of the machine code
|
|
|
|
if (as->base.pass == MP_ASM_PASS_EMIT) {
|
|
|
|
uint8_t *d = as->base.code_base;
|
|
|
|
printf("XTENSA ASM:");
|
|
|
|
for (int i = 0; i < ((as->base.code_size + 15) & ~15); ++i) {
|
|
|
|
if (i % 16 == 0) {
|
|
|
|
printf("\n%08x:", (uint32_t)&d[i]);
|
|
|
|
}
|
|
|
|
if (i % 2 == 0) {
|
|
|
|
printf(" ");
|
|
|
|
}
|
|
|
|
printf("%02x", d[i]);
|
|
|
|
}
|
|
|
|
printf("\n");
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
void asm_xtensa_entry(asm_xtensa_t *as, int num_locals) {
|
|
|
|
// jump over the constants
|
|
|
|
asm_xtensa_op_j(as, as->num_const * WORD_SIZE + 4 - 4);
|
|
|
|
mp_asm_base_get_cur_to_write_bytes(&as->base, 1); // padding/alignment byte
|
2021-03-15 09:57:36 -04:00
|
|
|
as->const_table = (uint32_t *)mp_asm_base_get_cur_to_write_bytes(&as->base, as->num_const * 4);
|
2016-12-09 00:32:30 -05:00
|
|
|
|
2018-09-15 10:43:24 -04:00
|
|
|
// adjust the stack-pointer to store a0, a12, a13, a14, a15 and locals, 16-byte aligned
|
|
|
|
as->stack_adjust = (((NUM_REGS_SAVED + num_locals) * WORD_SIZE) + 15) & ~15;
|
2018-08-15 23:43:36 -04:00
|
|
|
if (SIGNED_FIT8(-as->stack_adjust)) {
|
|
|
|
asm_xtensa_op_addi(as, ASM_XTENSA_REG_A1, ASM_XTENSA_REG_A1, -as->stack_adjust);
|
|
|
|
} else {
|
|
|
|
asm_xtensa_op_movi(as, ASM_XTENSA_REG_A9, as->stack_adjust);
|
|
|
|
asm_xtensa_op_sub(as, ASM_XTENSA_REG_A1, ASM_XTENSA_REG_A1, ASM_XTENSA_REG_A9);
|
|
|
|
}
|
2016-12-09 00:32:30 -05:00
|
|
|
|
2018-09-15 10:43:24 -04:00
|
|
|
// save return value (a0) and callee-save registers (a12, a13, a14, a15)
|
2016-12-09 00:32:30 -05:00
|
|
|
asm_xtensa_op_s32i_n(as, ASM_XTENSA_REG_A0, ASM_XTENSA_REG_A1, 0);
|
2018-09-15 10:43:24 -04:00
|
|
|
for (int i = 1; i < NUM_REGS_SAVED; ++i) {
|
|
|
|
asm_xtensa_op_s32i_n(as, ASM_XTENSA_REG_A11 + i, ASM_XTENSA_REG_A1, i);
|
|
|
|
}
|
2016-12-09 00:32:30 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void asm_xtensa_exit(asm_xtensa_t *as) {
|
|
|
|
// restore registers
|
2018-09-15 10:43:24 -04:00
|
|
|
for (int i = NUM_REGS_SAVED - 1; i >= 1; --i) {
|
|
|
|
asm_xtensa_op_l32i_n(as, ASM_XTENSA_REG_A11 + i, ASM_XTENSA_REG_A1, i);
|
|
|
|
}
|
2016-12-09 00:32:30 -05:00
|
|
|
asm_xtensa_op_l32i_n(as, ASM_XTENSA_REG_A0, ASM_XTENSA_REG_A1, 0);
|
|
|
|
|
|
|
|
// restore stack-pointer and return
|
2018-08-15 23:43:36 -04:00
|
|
|
if (SIGNED_FIT8(as->stack_adjust)) {
|
|
|
|
asm_xtensa_op_addi(as, ASM_XTENSA_REG_A1, ASM_XTENSA_REG_A1, as->stack_adjust);
|
|
|
|
} else {
|
|
|
|
asm_xtensa_op_movi(as, ASM_XTENSA_REG_A9, as->stack_adjust);
|
2018-08-17 00:53:58 -04:00
|
|
|
asm_xtensa_op_add_n(as, ASM_XTENSA_REG_A1, ASM_XTENSA_REG_A1, ASM_XTENSA_REG_A9);
|
2018-08-15 23:43:36 -04:00
|
|
|
}
|
|
|
|
|
2016-12-09 00:32:30 -05:00
|
|
|
asm_xtensa_op_ret_n(as);
|
|
|
|
}
|
|
|
|
|
|
|
|
STATIC uint32_t get_label_dest(asm_xtensa_t *as, uint label) {
|
|
|
|
assert(label < as->base.max_num_labels);
|
|
|
|
return as->base.label_offsets[label];
|
|
|
|
}
|
|
|
|
|
|
|
|
void asm_xtensa_op16(asm_xtensa_t *as, uint16_t op) {
|
|
|
|
uint8_t *c = mp_asm_base_get_cur_to_write_bytes(&as->base, 2);
|
2016-12-09 06:50:58 -05:00
|
|
|
if (c != NULL) {
|
|
|
|
c[0] = op;
|
|
|
|
c[1] = op >> 8;
|
|
|
|
}
|
2016-12-09 00:32:30 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void asm_xtensa_op24(asm_xtensa_t *as, uint32_t op) {
|
|
|
|
uint8_t *c = mp_asm_base_get_cur_to_write_bytes(&as->base, 3);
|
2016-12-09 06:50:58 -05:00
|
|
|
if (c != NULL) {
|
|
|
|
c[0] = op;
|
|
|
|
c[1] = op >> 8;
|
|
|
|
c[2] = op >> 16;
|
|
|
|
}
|
2016-12-09 00:32:30 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void asm_xtensa_j_label(asm_xtensa_t *as, uint label) {
|
|
|
|
uint32_t dest = get_label_dest(as, label);
|
|
|
|
int32_t rel = dest - as->base.code_offset - 4;
|
|
|
|
// we assume rel, as a signed int, fits in 18-bits
|
|
|
|
asm_xtensa_op_j(as, rel);
|
|
|
|
}
|
|
|
|
|
|
|
|
void asm_xtensa_bccz_reg_label(asm_xtensa_t *as, uint cond, uint reg, uint label) {
|
|
|
|
uint32_t dest = get_label_dest(as, label);
|
|
|
|
int32_t rel = dest - as->base.code_offset - 4;
|
|
|
|
if (as->base.pass == MP_ASM_PASS_EMIT && !SIGNED_FIT12(rel)) {
|
|
|
|
printf("ERROR: xtensa bccz out of range\n");
|
|
|
|
}
|
|
|
|
asm_xtensa_op_bccz(as, cond, reg, rel);
|
|
|
|
}
|
|
|
|
|
|
|
|
void asm_xtensa_bcc_reg_reg_label(asm_xtensa_t *as, uint cond, uint reg1, uint reg2, uint label) {
|
|
|
|
uint32_t dest = get_label_dest(as, label);
|
|
|
|
int32_t rel = dest - as->base.code_offset - 4;
|
|
|
|
if (as->base.pass == MP_ASM_PASS_EMIT && !SIGNED_FIT8(rel)) {
|
|
|
|
printf("ERROR: xtensa bcc out of range\n");
|
|
|
|
}
|
|
|
|
asm_xtensa_op_bcc(as, cond, reg1, reg2, rel);
|
|
|
|
}
|
|
|
|
|
|
|
|
// convenience function; reg_dest must be different from reg_src[12]
|
|
|
|
void asm_xtensa_setcc_reg_reg_reg(asm_xtensa_t *as, uint cond, uint reg_dest, uint reg_src1, uint reg_src2) {
|
|
|
|
asm_xtensa_op_movi_n(as, reg_dest, 1);
|
|
|
|
asm_xtensa_op_bcc(as, cond, reg_src1, reg_src2, 1);
|
|
|
|
asm_xtensa_op_movi_n(as, reg_dest, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
void asm_xtensa_mov_reg_i32(asm_xtensa_t *as, uint reg_dest, uint32_t i32) {
|
|
|
|
if (SIGNED_FIT12(i32)) {
|
|
|
|
asm_xtensa_op_movi(as, reg_dest, i32);
|
|
|
|
} else {
|
|
|
|
// load the constant
|
2021-04-20 01:22:44 -04:00
|
|
|
uint32_t const_table_offset = (uint8_t *)as->const_table - as->base.code_base;
|
2018-09-30 22:34:58 -04:00
|
|
|
asm_xtensa_op_l32r(as, reg_dest, as->base.code_offset, const_table_offset + as->cur_const * WORD_SIZE);
|
2016-12-09 00:32:30 -05:00
|
|
|
// store the constant in the table
|
2016-12-09 06:50:58 -05:00
|
|
|
if (as->const_table != NULL) {
|
2016-12-09 00:32:30 -05:00
|
|
|
as->const_table[as->cur_const] = i32;
|
|
|
|
}
|
|
|
|
++as->cur_const;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void asm_xtensa_mov_local_reg(asm_xtensa_t *as, int local_num, uint reg_src) {
|
2018-09-15 10:43:24 -04:00
|
|
|
asm_xtensa_op_s32i(as, reg_src, ASM_XTENSA_REG_A1, NUM_REGS_SAVED + local_num);
|
2016-12-09 00:32:30 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void asm_xtensa_mov_reg_local(asm_xtensa_t *as, uint reg_dest, int local_num) {
|
2018-09-15 10:43:24 -04:00
|
|
|
asm_xtensa_op_l32i(as, reg_dest, ASM_XTENSA_REG_A1, NUM_REGS_SAVED + local_num);
|
2016-12-09 00:32:30 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void asm_xtensa_mov_reg_local_addr(asm_xtensa_t *as, uint reg_dest, int local_num) {
|
2018-09-15 10:43:24 -04:00
|
|
|
uint off = (NUM_REGS_SAVED + local_num) * WORD_SIZE;
|
2018-08-16 11:11:22 -04:00
|
|
|
if (SIGNED_FIT8(off)) {
|
|
|
|
asm_xtensa_op_addi(as, reg_dest, ASM_XTENSA_REG_A1, off);
|
|
|
|
} else {
|
|
|
|
asm_xtensa_op_movi(as, reg_dest, off);
|
2018-08-17 00:53:58 -04:00
|
|
|
asm_xtensa_op_add_n(as, reg_dest, reg_dest, ASM_XTENSA_REG_A1);
|
2018-08-16 11:11:22 -04:00
|
|
|
}
|
2016-12-09 00:32:30 -05:00
|
|
|
}
|
|
|
|
|
2018-08-15 23:45:24 -04:00
|
|
|
void asm_xtensa_mov_reg_pcrel(asm_xtensa_t *as, uint reg_dest, uint label) {
|
|
|
|
// Get relative offset from PC
|
|
|
|
uint32_t dest = get_label_dest(as, label);
|
|
|
|
int32_t rel = dest - as->base.code_offset;
|
|
|
|
rel -= 3 + 3; // account for 3 bytes of movi instruction, 3 bytes call0 adjustment
|
|
|
|
asm_xtensa_op_movi(as, reg_dest, rel); // imm has 12-bit range
|
|
|
|
|
|
|
|
// Use call0 to get PC+3 into a0
|
|
|
|
// call0 destination must be aligned on 4 bytes:
|
|
|
|
// - code_offset&3=0: off=0, pad=1
|
|
|
|
// - code_offset&3=1: off=0, pad=0
|
|
|
|
// - code_offset&3=2: off=1, pad=3
|
|
|
|
// - code_offset&3=3: off=1, pad=2
|
|
|
|
uint32_t off = as->base.code_offset >> 1 & 1;
|
|
|
|
uint32_t pad = (5 - as->base.code_offset) & 3;
|
|
|
|
asm_xtensa_op_call0(as, off);
|
|
|
|
mp_asm_base_get_cur_to_write_bytes(&as->base, pad);
|
|
|
|
|
|
|
|
// Add PC to relative offset
|
2018-08-17 00:53:58 -04:00
|
|
|
asm_xtensa_op_add_n(as, reg_dest, reg_dest, ASM_XTENSA_REG_A0);
|
2018-08-15 23:45:24 -04:00
|
|
|
}
|
|
|
|
|
2018-09-15 10:43:24 -04:00
|
|
|
void asm_xtensa_call_ind(asm_xtensa_t *as, uint idx) {
|
|
|
|
if (idx < 16) {
|
2018-10-12 23:53:35 -04:00
|
|
|
asm_xtensa_op_l32i_n(as, ASM_XTENSA_REG_A0, ASM_XTENSA_REG_FUN_TABLE, idx);
|
2018-09-15 10:43:24 -04:00
|
|
|
} else {
|
2018-10-12 23:53:35 -04:00
|
|
|
asm_xtensa_op_l32i(as, ASM_XTENSA_REG_A0, ASM_XTENSA_REG_FUN_TABLE, idx);
|
2018-09-15 10:43:24 -04:00
|
|
|
}
|
|
|
|
asm_xtensa_op_callx0(as, ASM_XTENSA_REG_A0);
|
2016-12-09 00:32:30 -05:00
|
|
|
}
|
|
|
|
|
py: Add inline Xtensa assembler.
This patch adds the MICROPY_EMIT_INLINE_XTENSA option, which, when
enabled, allows the @micropython.asm_xtensa decorator to be used.
The following opcodes are currently supported (ax is a register, a0-a15):
ret_n()
callx0(ax)
j(label)
jx(ax)
beqz(ax, label)
bnez(ax, label)
mov(ax, ay)
movi(ax, imm) # imm can be full 32-bit, uses l32r if needed
and_(ax, ay, az)
or_(ax, ay, az)
xor(ax, ay, az)
add(ax, ay, az)
sub(ax, ay, az)
mull(ax, ay, az)
l8ui(ax, ay, imm)
l16ui(ax, ay, imm)
l32i(ax, ay, imm)
s8i(ax, ay, imm)
s16i(ax, ay, imm)
s32i(ax, ay, imm)
l16si(ax, ay, imm)
addi(ax, ay, imm)
ball(ax, ay, label)
bany(ax, ay, label)
bbc(ax, ay, label)
bbs(ax, ay, label)
beq(ax, ay, label)
bge(ax, ay, label)
bgeu(ax, ay, label)
blt(ax, ay, label)
bnall(ax, ay, label)
bne(ax, ay, label)
bnone(ax, ay, label)
Upon entry to the assembly function the registers a0, a12, a13, a14 are
pushed to the stack and the stack pointer (a1) decreased by 16. Upon
exit, these registers and the stack pointer are restored, and ret.n is
executed to return to the caller (caller address is in a0).
Note that the ABI for the Xtensa emitters is non-windowing.
2016-12-09 01:03:33 -05:00
|
|
|
#endif // MICROPY_EMIT_XTENSA || MICROPY_EMIT_INLINE_XTENSA
|