Numworks Epsilon  1.4.1
Graphing Calculator Operating System
asmthumb.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) 2013, 2014 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 #include <string.h>
30 
31 #include "py/mpconfig.h"
32 
33 // wrapper around everything in this file
34 #if MICROPY_EMIT_THUMB || MICROPY_EMIT_INLINE_THUMB
35 
36 #include "py/mphal.h"
37 #include "py/asmthumb.h"
38 
39 #define UNSIGNED_FIT8(x) (((x) & 0xffffff00) == 0)
40 #define UNSIGNED_FIT16(x) (((x) & 0xffff0000) == 0)
41 #define SIGNED_FIT8(x) (((x) & 0xffffff80) == 0) || (((x) & 0xffffff80) == 0xffffff80)
42 #define SIGNED_FIT9(x) (((x) & 0xffffff00) == 0) || (((x) & 0xffffff00) == 0xffffff00)
43 #define SIGNED_FIT12(x) (((x) & 0xfffff800) == 0) || (((x) & 0xfffff800) == 0xfffff800)
44 #define SIGNED_FIT23(x) (((x) & 0xffc00000) == 0) || (((x) & 0xffc00000) == 0xffc00000)
45 
46 static inline byte *asm_thumb_get_cur_to_write_bytes(asm_thumb_t *as, int n) {
48 }
49 
51  (void)as;
52  // could check labels are resolved...
53 
54  #if defined(MCU_SERIES_F7)
55  if (as->base.pass == MP_ASM_PASS_EMIT) {
56  // flush D-cache, so the code emitted is stored in memory
57  MP_HAL_CLEAN_DCACHE(as->base.code_base, as->base.code_size);
58  // invalidate I-cache
59  SCB_InvalidateICache();
60  }
61  #endif
62 }
63 
64 /*
65 STATIC void asm_thumb_write_byte_1(asm_thumb_t *as, byte b1) {
66  byte *c = asm_thumb_get_cur_to_write_bytes(as, 1);
67  c[0] = b1;
68 }
69 */
70 
71 /*
72 #define IMM32_L0(x) ((x) & 0xff)
73 #define IMM32_L1(x) (((x) >> 8) & 0xff)
74 #define IMM32_L2(x) (((x) >> 16) & 0xff)
75 #define IMM32_L3(x) (((x) >> 24) & 0xff)
76 
77 STATIC void asm_thumb_write_word32(asm_thumb_t *as, int w32) {
78  byte *c = asm_thumb_get_cur_to_write_bytes(as, 4);
79  c[0] = IMM32_L0(w32);
80  c[1] = IMM32_L1(w32);
81  c[2] = IMM32_L2(w32);
82  c[3] = IMM32_L3(w32);
83 }
84 */
85 
86 // rlolist is a bit map indicating desired lo-registers
87 #define OP_PUSH_RLIST(rlolist) (0xb400 | (rlolist))
88 #define OP_PUSH_RLIST_LR(rlolist) (0xb400 | 0x0100 | (rlolist))
89 #define OP_POP_RLIST(rlolist) (0xbc00 | (rlolist))
90 #define OP_POP_RLIST_PC(rlolist) (0xbc00 | 0x0100 | (rlolist))
91 
92 #define OP_ADD_SP(num_words) (0xb000 | (num_words))
93 #define OP_SUB_SP(num_words) (0xb080 | (num_words))
94 
95 // locals:
96 // - stored on the stack in ascending order
97 // - numbered 0 through num_locals-1
98 // - SP points to first local
99 //
100 // | SP
101 // v
102 // l0 l1 l2 ... l(n-1)
103 // ^ ^
104 // | low address | high address in RAM
105 
106 void asm_thumb_entry(asm_thumb_t *as, int num_locals) {
107  // work out what to push and how many extra spaces to reserve on stack
108  // so that we have enough for all locals and it's aligned an 8-byte boundary
109  // we push extra regs (r1, r2, r3) to help do the stack adjustment
110  // we probably should just always subtract from sp, since this would be more efficient
111  // for push rlist, lowest numbered register at the lowest address
112  uint reglist;
113  uint stack_adjust;
114  if (num_locals < 0) {
115  num_locals = 0;
116  }
117  // don't pop r0 because it's used for return value
118  switch (num_locals) {
119  case 0:
120  reglist = 0xf2;
121  stack_adjust = 0;
122  break;
123 
124  case 1:
125  reglist = 0xf2;
126  stack_adjust = 0;
127  break;
128 
129  case 2:
130  reglist = 0xfe;
131  stack_adjust = 0;
132  break;
133 
134  case 3:
135  reglist = 0xfe;
136  stack_adjust = 0;
137  break;
138 
139  default:
140  reglist = 0xfe;
141  stack_adjust = ((num_locals - 3) + 1) & (~1);
142  break;
143  }
144  asm_thumb_op16(as, OP_PUSH_RLIST_LR(reglist));
145  if (stack_adjust > 0) {
146  asm_thumb_op16(as, OP_SUB_SP(stack_adjust));
147  }
148  as->push_reglist = reglist;
149  as->stack_adjust = stack_adjust;
150 }
151 
152 void asm_thumb_exit(asm_thumb_t *as) {
153  if (as->stack_adjust > 0) {
154  asm_thumb_op16(as, OP_ADD_SP(as->stack_adjust));
155  }
156  asm_thumb_op16(as, OP_POP_RLIST_PC(as->push_reglist));
157 }
158 
159 STATIC mp_uint_t get_label_dest(asm_thumb_t *as, uint label) {
160  assert(label < as->base.max_num_labels);
161  return as->base.label_offsets[label];
162 }
163 
164 void asm_thumb_op16(asm_thumb_t *as, uint op) {
165  byte *c = asm_thumb_get_cur_to_write_bytes(as, 2);
166  if (c != NULL) {
167  // little endian
168  c[0] = op;
169  c[1] = op >> 8;
170  }
171 }
172 
173 void asm_thumb_op32(asm_thumb_t *as, uint op1, uint op2) {
174  byte *c = asm_thumb_get_cur_to_write_bytes(as, 4);
175  if (c != NULL) {
176  // little endian, op1 then op2
177  c[0] = op1;
178  c[1] = op1 >> 8;
179  c[2] = op2;
180  c[3] = op2 >> 8;
181  }
182 }
183 
184 #define OP_FORMAT_4(op, rlo_dest, rlo_src) ((op) | ((rlo_src) << 3) | (rlo_dest))
185 
186 void asm_thumb_format_4(asm_thumb_t *as, uint op, uint rlo_dest, uint rlo_src) {
187  assert(rlo_dest < ASM_THUMB_REG_R8);
188  assert(rlo_src < ASM_THUMB_REG_R8);
189  asm_thumb_op16(as, OP_FORMAT_4(op, rlo_dest, rlo_src));
190 }
191 
192 void asm_thumb_mov_reg_reg(asm_thumb_t *as, uint reg_dest, uint reg_src) {
193  uint op_lo;
194  if (reg_src < 8) {
195  op_lo = reg_src << 3;
196  } else {
197  op_lo = 0x40 | ((reg_src - 8) << 3);
198  }
199  if (reg_dest < 8) {
200  op_lo |= reg_dest;
201  } else {
202  op_lo |= 0x80 | (reg_dest - 8);
203  }
204  // mov reg_dest, reg_src
205  asm_thumb_op16(as, 0x4600 | op_lo);
206 }
207 
208 // if loading lo half with movw, the i16 value will be zero extended into the r32 register!
209 void asm_thumb_mov_reg_i16(asm_thumb_t *as, uint mov_op, uint reg_dest, int i16_src) {
210  assert(reg_dest < ASM_THUMB_REG_R15);
211  // mov[wt] reg_dest, #i16_src
212  asm_thumb_op32(as, mov_op | ((i16_src >> 1) & 0x0400) | ((i16_src >> 12) & 0xf), ((i16_src << 4) & 0x7000) | (reg_dest << 8) | (i16_src & 0xff));
213 }
214 
215 #define OP_B_N(byte_offset) (0xe000 | (((byte_offset) >> 1) & 0x07ff))
216 
217 bool asm_thumb_b_n_label(asm_thumb_t *as, uint label) {
218  mp_uint_t dest = get_label_dest(as, label);
219  mp_int_t rel = dest - as->base.code_offset;
220  rel -= 4; // account for instruction prefetch, PC is 4 bytes ahead of this instruction
221  asm_thumb_op16(as, OP_B_N(rel));
222  return as->base.pass != MP_ASM_PASS_EMIT || SIGNED_FIT12(rel);
223 }
224 
225 #define OP_BCC_N(cond, byte_offset) (0xd000 | ((cond) << 8) | (((byte_offset) >> 1) & 0x00ff))
226 
227 // all these bit arithmetics need coverage testing!
228 #define OP_BCC_W_HI(cond, byte_offset) (0xf000 | ((cond) << 6) | (((byte_offset) >> 10) & 0x0400) | (((byte_offset) >> 14) & 0x003f))
229 #define OP_BCC_W_LO(byte_offset) (0x8000 | ((byte_offset) & 0x2000) | (((byte_offset) >> 1) & 0x0fff))
230 
231 bool asm_thumb_bcc_nw_label(asm_thumb_t *as, int cond, uint label, bool wide) {
232  mp_uint_t dest = get_label_dest(as, label);
233  mp_int_t rel = dest - as->base.code_offset;
234  rel -= 4; // account for instruction prefetch, PC is 4 bytes ahead of this instruction
235  if (!wide) {
236  asm_thumb_op16(as, OP_BCC_N(cond, rel));
237  return as->base.pass != MP_ASM_PASS_EMIT || SIGNED_FIT9(rel);
238  } else {
239  asm_thumb_op32(as, OP_BCC_W_HI(cond, rel), OP_BCC_W_LO(rel));
240  return true;
241  }
242 }
243 
244 #define OP_BL_HI(byte_offset) (0xf000 | (((byte_offset) >> 12) & 0x07ff))
245 #define OP_BL_LO(byte_offset) (0xf800 | (((byte_offset) >> 1) & 0x07ff))
246 
247 bool asm_thumb_bl_label(asm_thumb_t *as, uint label) {
248  mp_uint_t dest = get_label_dest(as, label);
249  mp_int_t rel = dest - as->base.code_offset;
250  rel -= 4; // account for instruction prefetch, PC is 4 bytes ahead of this instruction
251  asm_thumb_op32(as, OP_BL_HI(rel), OP_BL_LO(rel));
252  return as->base.pass != MP_ASM_PASS_EMIT || SIGNED_FIT23(rel);
253 }
254 
255 void asm_thumb_mov_reg_i32(asm_thumb_t *as, uint reg_dest, mp_uint_t i32) {
256  // movw, movt does it in 8 bytes
257  // ldr [pc, #], dw does it in 6 bytes, but we might not reach to end of code for dw
258 
259  asm_thumb_mov_reg_i16(as, ASM_THUMB_OP_MOVW, reg_dest, i32);
260  asm_thumb_mov_reg_i16(as, ASM_THUMB_OP_MOVT, reg_dest, i32 >> 16);
261 }
262 
263 void asm_thumb_mov_reg_i32_optimised(asm_thumb_t *as, uint reg_dest, int i32) {
264  if (reg_dest < 8 && UNSIGNED_FIT8(i32)) {
265  asm_thumb_mov_rlo_i8(as, reg_dest, i32);
266  } else if (UNSIGNED_FIT16(i32)) {
267  asm_thumb_mov_reg_i16(as, ASM_THUMB_OP_MOVW, reg_dest, i32);
268  } else {
269  asm_thumb_mov_reg_i32(as, reg_dest, i32);
270  }
271 }
272 
273 // i32 is stored as a full word in the code, and aligned to machine-word boundary
274 // TODO this is very inefficient, improve it!
275 void asm_thumb_mov_reg_i32_aligned(asm_thumb_t *as, uint reg_dest, int i32) {
276  // align on machine-word + 2
277  if ((as->base.code_offset & 3) == 0) {
279  }
280  // jump over the i32 value (instruction prefetch adds 2 to PC)
281  asm_thumb_op16(as, OP_B_N(2));
282  // store i32 on machine-word aligned boundary
283  mp_asm_base_data(&as->base, 4, i32);
284  // do the actual load of the i32 value
285  asm_thumb_mov_reg_i32_optimised(as, reg_dest, i32);
286 }
287 
288 #define OP_STR_TO_SP_OFFSET(rlo_dest, word_offset) (0x9000 | ((rlo_dest) << 8) | ((word_offset) & 0x00ff))
289 #define OP_LDR_FROM_SP_OFFSET(rlo_dest, word_offset) (0x9800 | ((rlo_dest) << 8) | ((word_offset) & 0x00ff))
290 
291 void asm_thumb_mov_local_reg(asm_thumb_t *as, int local_num, uint rlo_src) {
292  assert(rlo_src < ASM_THUMB_REG_R8);
293  int word_offset = local_num;
294  assert(as->base.pass < MP_ASM_PASS_EMIT || word_offset >= 0);
295  asm_thumb_op16(as, OP_STR_TO_SP_OFFSET(rlo_src, word_offset));
296 }
297 
298 void asm_thumb_mov_reg_local(asm_thumb_t *as, uint rlo_dest, int local_num) {
299  assert(rlo_dest < ASM_THUMB_REG_R8);
300  int word_offset = local_num;
301  assert(as->base.pass < MP_ASM_PASS_EMIT || word_offset >= 0);
302  asm_thumb_op16(as, OP_LDR_FROM_SP_OFFSET(rlo_dest, word_offset));
303 }
304 
305 #define OP_ADD_REG_SP_OFFSET(rlo_dest, word_offset) (0xa800 | ((rlo_dest) << 8) | ((word_offset) & 0x00ff))
306 
307 void asm_thumb_mov_reg_local_addr(asm_thumb_t *as, uint rlo_dest, int local_num) {
308  assert(rlo_dest < ASM_THUMB_REG_R8);
309  int word_offset = local_num;
310  assert(as->base.pass < MP_ASM_PASS_EMIT || word_offset >= 0);
311  asm_thumb_op16(as, OP_ADD_REG_SP_OFFSET(rlo_dest, word_offset));
312 }
313 
314 // this could be wrong, because it should have a range of +/- 16MiB...
315 #define OP_BW_HI(byte_offset) (0xf000 | (((byte_offset) >> 12) & 0x07ff))
316 #define OP_BW_LO(byte_offset) (0xb800 | (((byte_offset) >> 1) & 0x07ff))
317 
318 void asm_thumb_b_label(asm_thumb_t *as, uint label) {
319  mp_uint_t dest = get_label_dest(as, label);
320  mp_int_t rel = dest - as->base.code_offset;
321  rel -= 4; // account for instruction prefetch, PC is 4 bytes ahead of this instruction
322  if (dest != (mp_uint_t)-1 && rel <= -4) {
323  // is a backwards jump, so we know the size of the jump on the first pass
324  // calculate rel assuming 12 bit relative jump
325  if (SIGNED_FIT12(rel)) {
326  asm_thumb_op16(as, OP_B_N(rel));
327  } else {
328  goto large_jump;
329  }
330  } else {
331  // is a forwards jump, so need to assume it's large
332  large_jump:
333  asm_thumb_op32(as, OP_BW_HI(rel), OP_BW_LO(rel));
334  }
335 }
336 
337 void asm_thumb_bcc_label(asm_thumb_t *as, int cond, uint label) {
338  mp_uint_t dest = get_label_dest(as, label);
339  mp_int_t rel = dest - as->base.code_offset;
340  rel -= 4; // account for instruction prefetch, PC is 4 bytes ahead of this instruction
341  if (dest != (mp_uint_t)-1 && rel <= -4) {
342  // is a backwards jump, so we know the size of the jump on the first pass
343  // calculate rel assuming 9 bit relative jump
344  if (SIGNED_FIT9(rel)) {
345  asm_thumb_op16(as, OP_BCC_N(cond, rel));
346  } else {
347  goto large_jump;
348  }
349  } else {
350  // is a forwards jump, so need to assume it's large
351  large_jump:
352  asm_thumb_op32(as, OP_BCC_W_HI(cond, rel), OP_BCC_W_LO(rel));
353  }
354 }
355 
356 #define OP_BLX(reg) (0x4780 | ((reg) << 3))
357 #define OP_SVC(arg) (0xdf00 | (arg))
358 
359 void asm_thumb_bl_ind(asm_thumb_t *as, void *fun_ptr, uint fun_id, uint reg_temp) {
360  /* TODO make this use less bytes
361  uint rlo_base = ASM_THUMB_REG_R3;
362  uint rlo_dest = ASM_THUMB_REG_R7;
363  uint word_offset = 4;
364  asm_thumb_op16(as, 0x0000);
365  asm_thumb_op16(as, 0x6800 | (word_offset << 6) | (rlo_base << 3) | rlo_dest); // ldr rlo_dest, [rlo_base, #offset]
366  asm_thumb_op16(as, 0x4780 | (ASM_THUMB_REG_R9 << 3)); // blx reg
367  */
368 
369  if (fun_id < 32) {
370  // load ptr to function from table, indexed by fun_id (must be in range 0-31); 4 bytes
372  asm_thumb_op16(as, OP_BLX(reg_temp));
373  } else {
374  // load ptr to function into register using immediate; 6 bytes
375  asm_thumb_mov_reg_i32(as, reg_temp, (mp_uint_t)fun_ptr);
376  asm_thumb_op16(as, OP_BLX(reg_temp));
377  }
378 }
379 
380 #endif // MICROPY_EMIT_THUMB || MICROPY_EMIT_INLINE_THUMB
uint32_t push_reglist
Definition: asmthumb.h:67
void asm_thumb_format_4(asm_thumb_t *as, uint op, uint rlo_dest, uint rlo_src)
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
intptr_t mp_int_t
Definition: mpconfigport.h:73
uintptr_t mp_uint_t
Definition: mpconfigport.h:74
#define ASM_THUMB_FORMAT_9_WORD_TRANSFER
Definition: asmthumb.h:191
uint8_t * code_base
Definition: asmbase.h:39
#define assert(e)
Definition: assert.h:9
#define ASM_THUMB_REG_R8
Definition: asmthumb.h:40
size_t code_offset
Definition: asmbase.h:37
#define ASM_THUMB_FORMAT_9_LDR
Definition: asmthumb.h:190
void asm_thumb_mov_reg_local_addr(asm_thumb_t *as, uint rlo_dest, int local_num)
size_t * label_offsets
Definition: asmbase.h:42
#define MP_ASM_PASS_EMIT
Definition: asmbase.h:33
#define ASM_THUMB_REG_R7
Definition: asmthumb.h:39
#define STATIC
Definition: mpconfig.h:1178
#define ASM_THUMB_FORMAT_9_10_ENCODE(op, rlo_dest, rlo_base, offset)
Definition: asmthumb.h:197
#define ASM_THUMB_REG_R15
Definition: asmthumb.h:47
void asm_thumb_mov_reg_local(asm_thumb_t *as, uint rlo_dest, int local_num)
bool asm_thumb_b_n_label(asm_thumb_t *as, uint label)
void asm_thumb_mov_reg_i32(asm_thumb_t *as, uint reg_dest, mp_uint_t i32_src)
void mp_asm_base_data(mp_asm_base_t *as, unsigned int bytesize, uintptr_t val)
c(generic_all_nodes)
void asm_thumb_mov_reg_i32_optimised(asm_thumb_t *as, uint reg_dest, int i32_src)
uint32_t stack_adjust
Definition: asmthumb.h:68
#define NULL
Definition: stddef.h:4
void asm_thumb_mov_reg_reg(asm_thumb_t *as, uint reg_dest, uint reg_src)
bool asm_thumb_bcc_nw_label(asm_thumb_t *as, int cond, uint label, bool wide)
void asm_thumb_mov_reg_i32_aligned(asm_thumb_t *as, uint reg_dest, int i32)
void asm_thumb_exit(asm_thumb_t *as)
void asm_thumb_mov_reg_i16(asm_thumb_t *as, uint mov_op, uint reg_dest, int i16_src)
bool asm_thumb_bl_label(asm_thumb_t *as, uint label)
void asm_thumb_bcc_label(asm_thumb_t *as, int cc, uint label)
#define ASM_THUMB_OP_MOVW
Definition: asmthumb.h:218
unsigned char byte
Definition: misc.h:37
void asm_thumb_op16(asm_thumb_t *as, uint op)
#define ASM_THUMB_OP_NOP
Definition: asmthumb.h:88
void asm_thumb_mov_local_reg(asm_thumb_t *as, int local_num_dest, uint rlo_src)
void asm_thumb_op32(asm_thumb_t *as, uint op1, uint op2)
void asm_thumb_b_label(asm_thumb_t *as, uint label)
void asm_thumb_end_pass(asm_thumb_t *as)
void asm_thumb_entry(asm_thumb_t *as, int num_locals)
mp_asm_base_t base
Definition: asmthumb.h:66
void asm_thumb_bl_ind(asm_thumb_t *as, void *fun_ptr, uint fun_id, uint reg_temp)
#define ASM_THUMB_OP_MOVT
Definition: asmthumb.h:219
unsigned int uint
Definition: misc.h:38