Numworks Epsilon  1.4.1
Graphing Calculator Operating System
asmxtensa.c
Go to the documentation of this file.
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 || MICROPY_EMIT_INLINE_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 
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
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 
100  if (c != NULL) {
101  c[0] = op;
102  c[1] = op >> 8;
103  }
104 }
105 
106 void asm_xtensa_op24(asm_xtensa_t *as, uint32_t op) {
108  if (c != NULL) {
109  c[0] = op;
110  c[1] = op >> 8;
111  c[2] = op >> 16;
112  }
113 }
114 
115 void asm_xtensa_j_label(asm_xtensa_t *as, uint label) {
116  uint32_t dest = get_label_dest(as, label);
117  int32_t rel = dest - as->base.code_offset - 4;
118  // we assume rel, as a signed int, fits in 18-bits
119  asm_xtensa_op_j(as, rel);
120 }
121 
122 void asm_xtensa_bccz_reg_label(asm_xtensa_t *as, uint cond, uint reg, uint label) {
123  uint32_t dest = get_label_dest(as, label);
124  int32_t rel = dest - as->base.code_offset - 4;
125  if (as->base.pass == MP_ASM_PASS_EMIT && !SIGNED_FIT12(rel)) {
126  printf("ERROR: xtensa bccz out of range\n");
127  }
128  asm_xtensa_op_bccz(as, cond, reg, rel);
129 }
130 
131 void asm_xtensa_bcc_reg_reg_label(asm_xtensa_t *as, uint cond, uint reg1, uint reg2, uint label) {
132  uint32_t dest = get_label_dest(as, label);
133  int32_t rel = dest - as->base.code_offset - 4;
134  if (as->base.pass == MP_ASM_PASS_EMIT && !SIGNED_FIT8(rel)) {
135  printf("ERROR: xtensa bcc out of range\n");
136  }
137  asm_xtensa_op_bcc(as, cond, reg1, reg2, rel);
138 }
139 
140 // convenience function; reg_dest must be different from reg_src[12]
141 void asm_xtensa_setcc_reg_reg_reg(asm_xtensa_t *as, uint cond, uint reg_dest, uint reg_src1, uint reg_src2) {
142  asm_xtensa_op_movi_n(as, reg_dest, 1);
143  asm_xtensa_op_bcc(as, cond, reg_src1, reg_src2, 1);
144  asm_xtensa_op_movi_n(as, reg_dest, 0);
145 }
146 
147 void asm_xtensa_mov_reg_i32(asm_xtensa_t *as, uint reg_dest, uint32_t i32) {
148  if (SIGNED_FIT12(i32)) {
149  asm_xtensa_op_movi(as, reg_dest, i32);
150  } else {
151  // load the constant
152  asm_xtensa_op_l32r(as, reg_dest, as->base.code_offset, 4 + as->cur_const * WORD_SIZE);
153  // store the constant in the table
154  if (as->const_table != NULL) {
155  as->const_table[as->cur_const] = i32;
156  }
157  ++as->cur_const;
158  }
159 }
160 
161 void asm_xtensa_mov_local_reg(asm_xtensa_t *as, int local_num, uint reg_src) {
162  asm_xtensa_op_s32i(as, reg_src, ASM_XTENSA_REG_A1, 4 + local_num);
163 }
164 
165 void asm_xtensa_mov_reg_local(asm_xtensa_t *as, uint reg_dest, int local_num) {
166  asm_xtensa_op_l32i(as, reg_dest, ASM_XTENSA_REG_A1, 4 + local_num);
167 }
168 
169 void asm_xtensa_mov_reg_local_addr(asm_xtensa_t *as, uint reg_dest, int local_num) {
170  asm_xtensa_op_mov_n(as, reg_dest, ASM_XTENSA_REG_A1);
171  asm_xtensa_op_addi(as, reg_dest, reg_dest, (4 + local_num) * WORD_SIZE);
172 }
173 
174 #endif // MICROPY_EMIT_XTENSA || MICROPY_EMIT_INLINE_XTENSA
uint8_t * mp_asm_base_get_cur_to_write_bytes(mp_asm_base_t *as, size_t num_bytes_to_write)
size_t code_size
Definition: asmbase.h:38
void asm_xtensa_end_pass(asm_xtensa_t *as)
uint8_t * code_base
Definition: asmbase.h:39
#define assert(e)
Definition: assert.h:9
size_t code_offset
Definition: asmbase.h:37
#define ASM_XTENSA_REG_A0
Definition: asmxtensa.h:39
void asm_xtensa_j_label(asm_xtensa_t *as, uint label)
size_t * label_offsets
Definition: asmbase.h:42
void asm_xtensa_bccz_reg_label(asm_xtensa_t *as, uint cond, uint reg, uint label)
unsigned short uint16_t
Definition: stdint.h:5
uint32_t num_const
Definition: asmxtensa.h:101
void asm_xtensa_mov_reg_local(asm_xtensa_t *as, uint reg_dest, int local_num)
unsigned char uint8_t
Definition: stdint.h:4
void asm_xtensa_op16(asm_xtensa_t *as, uint16_t op)
mp_asm_base_t base
Definition: asmxtensa.h:99
#define MP_ASM_PASS_EMIT
Definition: asmbase.h:33
#define STATIC
Definition: mpconfig.h:1178
void asm_xtensa_op24(asm_xtensa_t *as, uint32_t op)
void asm_xtensa_mov_local_reg(asm_xtensa_t *as, int local_num, uint reg_src)
uint32_t cur_const
Definition: asmxtensa.h:100
c(generic_all_nodes)
void asm_xtensa_entry(asm_xtensa_t *as, int num_locals)
unsigned int uint32_t
Definition: stdint.h:6
#define NULL
Definition: stddef.h:4
void asm_xtensa_setcc_reg_reg_reg(asm_xtensa_t *as, uint cond, uint reg_dest, uint reg_src1, uint reg_src2)
#define ASM_XTENSA_REG_A13
Definition: asmxtensa.h:52
uint32_t * const_table
Definition: asmxtensa.h:102
void asm_xtensa_exit(asm_xtensa_t *as)
#define ASM_XTENSA_REG_A1
Definition: asmxtensa.h:40
uint32_t stack_adjust
Definition: asmxtensa.h:103
void asm_xtensa_mov_reg_i32(asm_xtensa_t *as, uint reg_dest, uint32_t i32)
signed int int32_t
Definition: stdint.h:11
#define ASM_XTENSA_REG_A12
Definition: asmxtensa.h:51
#define ASM_XTENSA_REG_A14
Definition: asmxtensa.h:53
void asm_xtensa_mov_reg_local_addr(asm_xtensa_t *as, uint reg_dest, int local_num)
void asm_xtensa_bcc_reg_reg_label(asm_xtensa_t *as, uint cond, uint reg1, uint reg2, uint label)
unsigned int uint
Definition: misc.h:38