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/mpstate.h"
37 #include "py/persistentcode.h"
38 #include "py/asmthumb.h"
39 
40 #define UNSIGNED_FIT5(x) ((uint32_t)(x) < 32)
41 #define UNSIGNED_FIT7(x) ((uint32_t)(x) < 128)
42 #define UNSIGNED_FIT8(x) (((x) & 0xffffff00) == 0)
43 #define UNSIGNED_FIT16(x) (((x) & 0xffff0000) == 0)
44 #define SIGNED_FIT8(x) (((x) & 0xffffff80) == 0) || (((x) & 0xffffff80) == 0xffffff80)
45 #define SIGNED_FIT9(x) (((x) & 0xffffff00) == 0) || (((x) & 0xffffff00) == 0xffffff00)
46 #define SIGNED_FIT12(x) (((x) & 0xfffff800) == 0) || (((x) & 0xfffff800) == 0xfffff800)
47 #define SIGNED_FIT23(x) (((x) & 0xffc00000) == 0) || (((x) & 0xffc00000) == 0xffc00000)
48 
49 #if MICROPY_EMIT_THUMB_ARMV7M
50 // Note: these actually take an imm12 but the high-bit is not encoded here
51 #define OP_ADD_W_RRI_HI(reg_src) (0xf200 | (reg_src))
52 #define OP_ADD_W_RRI_LO(reg_dest, imm11) ((imm11 << 4 & 0x7000) | reg_dest << 8 | (imm11 & 0xff))
53 #define OP_SUB_W_RRI_HI(reg_src) (0xf2a0 | (reg_src))
54 #define OP_SUB_W_RRI_LO(reg_dest, imm11) ((imm11 << 4 & 0x7000) | reg_dest << 8 | (imm11 & 0xff))
55 
56 #define OP_LDR_W_HI(reg_base) (0xf8d0 | (reg_base))
57 #define OP_LDR_W_LO(reg_dest, imm12) ((reg_dest) << 12 | (imm12))
58 #endif
59 
asm_thumb_get_cur_to_write_bytes(asm_thumb_t * as,int n)60 static inline byte *asm_thumb_get_cur_to_write_bytes(asm_thumb_t *as, int n) {
61     return mp_asm_base_get_cur_to_write_bytes(&as->base, n);
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 // The number of words must fit in 7 unsigned bits
93 #define OP_ADD_SP(num_words) (0xb000 | (num_words))
94 #define OP_SUB_SP(num_words) (0xb080 | (num_words))
95 
96 // locals:
97 //  - stored on the stack in ascending order
98 //  - numbered 0 through num_locals-1
99 //  - SP points to first local
100 //
101 //  | SP
102 //  v
103 //  l0  l1  l2  ...  l(n-1)
104 //  ^                ^
105 //  | low address    | high address in RAM
106 
asm_thumb_entry(asm_thumb_t * as,int num_locals)107 void asm_thumb_entry(asm_thumb_t *as, int num_locals) {
108     assert(num_locals >= 0);
109 
110     // If this Thumb machine code is run from ARM state then add a prelude
111     // to switch to Thumb state for the duration of the function.
112     #if MICROPY_DYNAMIC_COMPILER || MICROPY_EMIT_ARM || (defined(__arm__) && !defined(__thumb2__) && !defined(__thumb__))
113     #if MICROPY_DYNAMIC_COMPILER
114     if (mp_dynamic_compiler.native_arch == MP_NATIVE_ARCH_ARMV6)
115     #endif
116     {
117         asm_thumb_op32(as, 0x4010, 0xe92d); // push {r4, lr}
118         asm_thumb_op32(as, 0xe009, 0xe28f); // add lr, pc, 8 + 1
119         asm_thumb_op32(as, 0xff3e, 0xe12f); // blx lr
120         asm_thumb_op32(as, 0x4010, 0xe8bd); // pop {r4, lr}
121         asm_thumb_op32(as, 0xff1e, 0xe12f); // bx lr
122     }
123     #endif
124 
125     // work out what to push and how many extra spaces to reserve on stack
126     // so that we have enough for all locals and it's aligned an 8-byte boundary
127     // we push extra regs (r1, r2, r3) to help do the stack adjustment
128     // we probably should just always subtract from sp, since this would be more efficient
129     // for push rlist, lowest numbered register at the lowest address
130     uint reglist;
131     uint stack_adjust;
132     // don't pop r0 because it's used for return value
133     switch (num_locals) {
134         case 0:
135             reglist = 0xf2;
136             stack_adjust = 0;
137             break;
138 
139         case 1:
140             reglist = 0xf2;
141             stack_adjust = 0;
142             break;
143 
144         case 2:
145             reglist = 0xfe;
146             stack_adjust = 0;
147             break;
148 
149         case 3:
150             reglist = 0xfe;
151             stack_adjust = 0;
152             break;
153 
154         default:
155             reglist = 0xfe;
156             stack_adjust = ((num_locals - 3) + 1) & (~1);
157             break;
158     }
159     asm_thumb_op16(as, OP_PUSH_RLIST_LR(reglist));
160     if (stack_adjust > 0) {
161         #if MICROPY_EMIT_THUMB_ARMV7M
162         if (UNSIGNED_FIT7(stack_adjust)) {
163             asm_thumb_op16(as, OP_SUB_SP(stack_adjust));
164         } else {
165             asm_thumb_op32(as, OP_SUB_W_RRI_HI(ASM_THUMB_REG_SP), OP_SUB_W_RRI_LO(ASM_THUMB_REG_SP, stack_adjust * 4));
166         }
167         #else
168         int adj = stack_adjust;
169         // we don't expect the stack_adjust to be massive
170         while (!UNSIGNED_FIT7(adj)) {
171             asm_thumb_op16(as, OP_SUB_SP(127));
172             adj -= 127;
173         }
174         asm_thumb_op16(as, OP_SUB_SP(adj));
175         #endif
176     }
177     as->push_reglist = reglist;
178     as->stack_adjust = stack_adjust;
179 }
180 
asm_thumb_exit(asm_thumb_t * as)181 void asm_thumb_exit(asm_thumb_t *as) {
182     if (as->stack_adjust > 0) {
183         #if MICROPY_EMIT_THUMB_ARMV7M
184         if (UNSIGNED_FIT7(as->stack_adjust)) {
185             asm_thumb_op16(as, OP_ADD_SP(as->stack_adjust));
186         } else {
187             asm_thumb_op32(as, OP_ADD_W_RRI_HI(ASM_THUMB_REG_SP), OP_ADD_W_RRI_LO(ASM_THUMB_REG_SP, as->stack_adjust * 4));
188         }
189         #else
190         int adj = as->stack_adjust;
191         // we don't expect the stack_adjust to be massive
192         while (!UNSIGNED_FIT7(adj)) {
193             asm_thumb_op16(as, OP_ADD_SP(127));
194             adj -= 127;
195         }
196         asm_thumb_op16(as, OP_ADD_SP(adj));
197         #endif
198     }
199     asm_thumb_op16(as, OP_POP_RLIST_PC(as->push_reglist));
200 }
201 
get_label_dest(asm_thumb_t * as,uint label)202 STATIC mp_uint_t get_label_dest(asm_thumb_t *as, uint label) {
203     assert(label < as->base.max_num_labels);
204     return as->base.label_offsets[label];
205 }
206 
asm_thumb_op16(asm_thumb_t * as,uint op)207 void asm_thumb_op16(asm_thumb_t *as, uint op) {
208     byte *c = asm_thumb_get_cur_to_write_bytes(as, 2);
209     if (c != NULL) {
210         // little endian
211         c[0] = op;
212         c[1] = op >> 8;
213     }
214 }
215 
asm_thumb_op32(asm_thumb_t * as,uint op1,uint op2)216 void asm_thumb_op32(asm_thumb_t *as, uint op1, uint op2) {
217     byte *c = asm_thumb_get_cur_to_write_bytes(as, 4);
218     if (c != NULL) {
219         // little endian, op1 then op2
220         c[0] = op1;
221         c[1] = op1 >> 8;
222         c[2] = op2;
223         c[3] = op2 >> 8;
224     }
225 }
226 
227 #define OP_FORMAT_4(op, rlo_dest, rlo_src) ((op) | ((rlo_src) << 3) | (rlo_dest))
228 
asm_thumb_format_4(asm_thumb_t * as,uint op,uint rlo_dest,uint rlo_src)229 void asm_thumb_format_4(asm_thumb_t *as, uint op, uint rlo_dest, uint rlo_src) {
230     assert(rlo_dest < ASM_THUMB_REG_R8);
231     assert(rlo_src < ASM_THUMB_REG_R8);
232     asm_thumb_op16(as, OP_FORMAT_4(op, rlo_dest, rlo_src));
233 }
234 
asm_thumb_mov_reg_reg(asm_thumb_t * as,uint reg_dest,uint reg_src)235 void asm_thumb_mov_reg_reg(asm_thumb_t *as, uint reg_dest, uint reg_src) {
236     uint op_lo;
237     if (reg_src < 8) {
238         op_lo = reg_src << 3;
239     } else {
240         op_lo = 0x40 | ((reg_src - 8) << 3);
241     }
242     if (reg_dest < 8) {
243         op_lo |= reg_dest;
244     } else {
245         op_lo |= 0x80 | (reg_dest - 8);
246     }
247     // mov reg_dest, reg_src
248     asm_thumb_op16(as, 0x4600 | op_lo);
249 }
250 
251 #if MICROPY_EMIT_THUMB_ARMV7M
252 
253 // if loading lo half with movw, the i16 value will be zero extended into the r32 register!
asm_thumb_mov_reg_i16(asm_thumb_t * as,uint mov_op,uint reg_dest,int i16_src)254 size_t asm_thumb_mov_reg_i16(asm_thumb_t *as, uint mov_op, uint reg_dest, int i16_src) {
255     assert(reg_dest < ASM_THUMB_REG_R15);
256     size_t loc = mp_asm_base_get_code_pos(&as->base);
257     // mov[wt] reg_dest, #i16_src
258     asm_thumb_op32(as, mov_op | ((i16_src >> 1) & 0x0400) | ((i16_src >> 12) & 0xf), ((i16_src << 4) & 0x7000) | (reg_dest << 8) | (i16_src & 0xff));
259     return loc;
260 }
261 
262 #else
263 
asm_thumb_mov_rlo_i16(asm_thumb_t * as,uint rlo_dest,int i16_src)264 void asm_thumb_mov_rlo_i16(asm_thumb_t *as, uint rlo_dest, int i16_src) {
265     asm_thumb_mov_rlo_i8(as, rlo_dest, (i16_src >> 8) & 0xff);
266     asm_thumb_lsl_rlo_rlo_i5(as, rlo_dest, rlo_dest, 8);
267     asm_thumb_add_rlo_i8(as, rlo_dest, i16_src & 0xff);
268 }
269 
270 #endif
271 
272 #define OP_B_N(byte_offset) (0xe000 | (((byte_offset) >> 1) & 0x07ff))
273 
asm_thumb_b_n_label(asm_thumb_t * as,uint label)274 bool asm_thumb_b_n_label(asm_thumb_t *as, uint label) {
275     mp_uint_t dest = get_label_dest(as, label);
276     mp_int_t rel = dest - as->base.code_offset;
277     rel -= 4; // account for instruction prefetch, PC is 4 bytes ahead of this instruction
278     asm_thumb_op16(as, OP_B_N(rel));
279     return as->base.pass != MP_ASM_PASS_EMIT || SIGNED_FIT12(rel);
280 }
281 
282 #define OP_BCC_N(cond, byte_offset) (0xd000 | ((cond) << 8) | (((byte_offset) >> 1) & 0x00ff))
283 
284 // all these bit arithmetics need coverage testing!
285 #define OP_BCC_W_HI(cond, byte_offset) (0xf000 | ((cond) << 6) | (((byte_offset) >> 10) & 0x0400) | (((byte_offset) >> 14) & 0x003f))
286 #define OP_BCC_W_LO(byte_offset) (0x8000 | ((byte_offset) & 0x2000) | (((byte_offset) >> 1) & 0x0fff))
287 
asm_thumb_bcc_nw_label(asm_thumb_t * as,int cond,uint label,bool wide)288 bool asm_thumb_bcc_nw_label(asm_thumb_t *as, int cond, uint label, bool wide) {
289     mp_uint_t dest = get_label_dest(as, label);
290     mp_int_t rel = dest - as->base.code_offset;
291     rel -= 4; // account for instruction prefetch, PC is 4 bytes ahead of this instruction
292     if (!wide) {
293         asm_thumb_op16(as, OP_BCC_N(cond, rel));
294         return as->base.pass != MP_ASM_PASS_EMIT || SIGNED_FIT9(rel);
295     } else {
296         #if MICROPY_EMIT_THUMB_ARMV7M
297         asm_thumb_op32(as, OP_BCC_W_HI(cond, rel), OP_BCC_W_LO(rel));
298         return true;
299         #else
300         // this method should not be called for ARMV6M
301         return false;
302         #endif
303     }
304 }
305 
306 #define OP_BL_HI(byte_offset) (0xf000 | (((byte_offset) >> 12) & 0x07ff))
307 #define OP_BL_LO(byte_offset) (0xf800 | (((byte_offset) >> 1) & 0x07ff))
308 
asm_thumb_bl_label(asm_thumb_t * as,uint label)309 bool asm_thumb_bl_label(asm_thumb_t *as, uint label) {
310     mp_uint_t dest = get_label_dest(as, label);
311     mp_int_t rel = dest - as->base.code_offset;
312     rel -= 4; // account for instruction prefetch, PC is 4 bytes ahead of this instruction
313     asm_thumb_op32(as, OP_BL_HI(rel), OP_BL_LO(rel));
314     return as->base.pass != MP_ASM_PASS_EMIT || SIGNED_FIT23(rel);
315 }
316 
asm_thumb_mov_reg_i32(asm_thumb_t * as,uint reg_dest,mp_uint_t i32)317 size_t asm_thumb_mov_reg_i32(asm_thumb_t *as, uint reg_dest, mp_uint_t i32) {
318     // movw, movt does it in 8 bytes
319     // ldr [pc, #], dw does it in 6 bytes, but we might not reach to end of code for dw
320 
321     size_t loc = mp_asm_base_get_code_pos(&as->base);
322 
323     #if MICROPY_EMIT_THUMB_ARMV7M
324     asm_thumb_mov_reg_i16(as, ASM_THUMB_OP_MOVW, reg_dest, i32);
325     asm_thumb_mov_reg_i16(as, ASM_THUMB_OP_MOVT, reg_dest, i32 >> 16);
326     #else
327     // should only be called with lo reg for ARMV6M
328     assert(reg_dest < ASM_THUMB_REG_R8);
329 
330     // sanity check that generated code is aligned
331     assert(!as->base.code_base || !(3u & (uintptr_t)as->base.code_base));
332 
333     // basically:
334     //        (nop)
335     //        ldr reg_dest, _data
336     //        b 1f
337     // _data: .word i32
338     //  1:
339     if (as->base.code_offset & 2u) {
340         asm_thumb_op16(as, ASM_THUMB_OP_NOP);
341     }
342     asm_thumb_ldr_rlo_pcrel_i8(as, reg_dest, 0);
343     asm_thumb_op16(as, OP_B_N(2));
344     asm_thumb_op16(as, i32 & 0xffff);
345     asm_thumb_op16(as, i32 >> 16);
346     #endif
347 
348     return loc;
349 }
350 
asm_thumb_mov_reg_i32_optimised(asm_thumb_t * as,uint reg_dest,int i32)351 void asm_thumb_mov_reg_i32_optimised(asm_thumb_t *as, uint reg_dest, int i32) {
352     if (reg_dest < 8 && UNSIGNED_FIT8(i32)) {
353         asm_thumb_mov_rlo_i8(as, reg_dest, i32);
354     } else {
355         #if MICROPY_EMIT_THUMB_ARMV7M
356         if (UNSIGNED_FIT16(i32)) {
357             asm_thumb_mov_reg_i16(as, ASM_THUMB_OP_MOVW, reg_dest, i32);
358         } else {
359             asm_thumb_mov_reg_i32(as, reg_dest, i32);
360         }
361         #else
362         uint rlo_dest = reg_dest;
363         assert(rlo_dest < ASM_THUMB_REG_R8); // should never be called for ARMV6M
364 
365         bool negate = i32 < 0 && ((i32 + i32) & 0xffffffffu); // don't negate 0x80000000
366         if (negate) {
367             i32 = -i32;
368         }
369 
370         uint clz = __builtin_clz(i32);
371         uint ctz = i32 ? __builtin_ctz(i32) : 0;
372         assert(clz + ctz <= 32);
373         if (clz + ctz >= 24) {
374             asm_thumb_mov_rlo_i8(as, rlo_dest, (i32 >> ctz) & 0xff);
375             asm_thumb_lsl_rlo_rlo_i5(as, rlo_dest, rlo_dest, ctz);
376         } else if (UNSIGNED_FIT16(i32)) {
377             asm_thumb_mov_rlo_i16(as, rlo_dest, i32);
378         } else {
379             if (negate) {
380                 // no point in negating if we're storing in 32 bit anyway
381                 negate = false;
382                 i32 = -i32;
383             }
384             asm_thumb_mov_reg_i32(as, rlo_dest, i32);
385         }
386         if (negate) {
387             asm_thumb_neg_rlo_rlo(as, rlo_dest, rlo_dest);
388         }
389         #endif
390     }
391 }
392 
393 #define OP_STR_TO_SP_OFFSET(rlo_dest, word_offset) (0x9000 | ((rlo_dest) << 8) | ((word_offset) & 0x00ff))
394 #define OP_LDR_FROM_SP_OFFSET(rlo_dest, word_offset) (0x9800 | ((rlo_dest) << 8) | ((word_offset) & 0x00ff))
395 
asm_thumb_mov_local_check(asm_thumb_t * as,int word_offset)396 static void asm_thumb_mov_local_check(asm_thumb_t *as, int word_offset) {
397     if (as->base.pass >= MP_ASM_PASS_EMIT) {
398         assert(word_offset >= 0);
399         if (!UNSIGNED_FIT8(word_offset)) {
400             mp_raise_NotImplementedError(MP_ERROR_TEXT("too many locals for native method"));
401         }
402     }
403 }
404 
asm_thumb_mov_local_reg(asm_thumb_t * as,int local_num,uint rlo_src)405 void asm_thumb_mov_local_reg(asm_thumb_t *as, int local_num, uint rlo_src) {
406     assert(rlo_src < ASM_THUMB_REG_R8);
407     int word_offset = local_num;
408     asm_thumb_mov_local_check(as, word_offset);
409     asm_thumb_op16(as, OP_STR_TO_SP_OFFSET(rlo_src, word_offset));
410 }
411 
asm_thumb_mov_reg_local(asm_thumb_t * as,uint rlo_dest,int local_num)412 void asm_thumb_mov_reg_local(asm_thumb_t *as, uint rlo_dest, int local_num) {
413     assert(rlo_dest < ASM_THUMB_REG_R8);
414     int word_offset = local_num;
415     asm_thumb_mov_local_check(as, word_offset);
416     asm_thumb_op16(as, OP_LDR_FROM_SP_OFFSET(rlo_dest, word_offset));
417 }
418 
419 #define OP_ADD_REG_SP_OFFSET(rlo_dest, word_offset) (0xa800 | ((rlo_dest) << 8) | ((word_offset) & 0x00ff))
420 
asm_thumb_mov_reg_local_addr(asm_thumb_t * as,uint rlo_dest,int local_num)421 void asm_thumb_mov_reg_local_addr(asm_thumb_t *as, uint rlo_dest, int local_num) {
422     assert(rlo_dest < ASM_THUMB_REG_R8);
423     int word_offset = local_num;
424     assert(as->base.pass < MP_ASM_PASS_EMIT || word_offset >= 0);
425     asm_thumb_op16(as, OP_ADD_REG_SP_OFFSET(rlo_dest, word_offset));
426 }
427 
asm_thumb_mov_reg_pcrel(asm_thumb_t * as,uint rlo_dest,uint label)428 void asm_thumb_mov_reg_pcrel(asm_thumb_t *as, uint rlo_dest, uint label) {
429     mp_uint_t dest = get_label_dest(as, label);
430     mp_int_t rel = dest - as->base.code_offset;
431     rel |= 1; // to stay in Thumb state when jumping to this address
432     #if MICROPY_EMIT_THUMB_ARMV7M
433     rel -= 4 + 4; // adjust for mov_reg_i16 and then PC+4 prefetch of add_reg_reg
434     asm_thumb_mov_reg_i16(as, ASM_THUMB_OP_MOVW, rlo_dest, rel); // 4 bytes
435     #else
436     rel -= 8 + 4; // adjust for four instructions and then PC+4 prefetch of add_reg_reg
437     // 6 bytes
438     asm_thumb_mov_rlo_i16(as, rlo_dest, rel);
439     // 2 bytes - not always needed, but we want to keep the size the same
440     asm_thumb_sxth_rlo_rlo(as, rlo_dest, rlo_dest);
441     #endif
442     asm_thumb_add_reg_reg(as, rlo_dest, ASM_THUMB_REG_R15); // 2 bytes
443 }
444 
445 #if MICROPY_EMIT_THUMB_ARMV7M
asm_thumb_ldr_reg_reg_i12(asm_thumb_t * as,uint reg_dest,uint reg_base,uint word_offset)446 static inline void asm_thumb_ldr_reg_reg_i12(asm_thumb_t *as, uint reg_dest, uint reg_base, uint word_offset) {
447     asm_thumb_op32(as, OP_LDR_W_HI(reg_base), OP_LDR_W_LO(reg_dest, word_offset * 4));
448 }
449 #endif
450 
asm_thumb_ldr_reg_reg_i12_optimised(asm_thumb_t * as,uint reg_dest,uint reg_base,uint word_offset)451 void asm_thumb_ldr_reg_reg_i12_optimised(asm_thumb_t *as, uint reg_dest, uint reg_base, uint word_offset) {
452     if (reg_dest < ASM_THUMB_REG_R8 && reg_base < ASM_THUMB_REG_R8 && UNSIGNED_FIT5(word_offset)) {
453         asm_thumb_ldr_rlo_rlo_i5(as, reg_dest, reg_base, word_offset);
454     } else {
455         #if MICROPY_EMIT_THUMB_ARMV7M
456         asm_thumb_ldr_reg_reg_i12(as, reg_dest, reg_base, word_offset);
457         #else
458         word_offset -= 31;
459         if (reg_dest < ASM_THUMB_REG_R8 && reg_base < ASM_THUMB_REG_R8) {
460             if (UNSIGNED_FIT8(word_offset) && (word_offset < 64 || reg_dest != reg_base)) {
461                 if (word_offset < 64) {
462                     if (reg_dest != reg_base) {
463                         asm_thumb_mov_reg_reg(as, reg_dest, reg_base);
464                     }
465                     asm_thumb_add_rlo_i8(as, reg_dest, word_offset * 4);
466                 } else {
467                     asm_thumb_mov_rlo_i8(as, reg_dest, word_offset);
468                     asm_thumb_lsl_rlo_rlo_i5(as, reg_dest, reg_dest, 2);
469                     asm_thumb_add_rlo_rlo_rlo(as, reg_dest, reg_dest, reg_base);
470                 }
471             } else {
472                 if (reg_dest != reg_base) {
473                     asm_thumb_mov_rlo_i16(as, reg_dest, word_offset * 4);
474                     asm_thumb_add_rlo_rlo_rlo(as, reg_dest, reg_dest, reg_dest);
475                 } else {
476                     uint reg_other = reg_dest ^ 7;
477                     asm_thumb_op16(as, OP_PUSH_RLIST((1 << reg_other)));
478                     asm_thumb_mov_rlo_i16(as, reg_other, word_offset * 4);
479                     asm_thumb_add_rlo_rlo_rlo(as, reg_dest, reg_dest, reg_other);
480                     asm_thumb_op16(as, OP_POP_RLIST((1 << reg_other)));
481                 }
482             }
483         } else {
484             assert(0); // should never be called for ARMV6M
485         }
486         asm_thumb_ldr_rlo_rlo_i5(as, reg_dest, reg_dest, 31);
487         #endif
488     }
489 }
490 
491 // this could be wrong, because it should have a range of +/- 16MiB...
492 #define OP_BW_HI(byte_offset) (0xf000 | (((byte_offset) >> 12) & 0x07ff))
493 #define OP_BW_LO(byte_offset) (0xb800 | (((byte_offset) >> 1) & 0x07ff))
494 
asm_thumb_b_label(asm_thumb_t * as,uint label)495 void asm_thumb_b_label(asm_thumb_t *as, uint label) {
496     mp_uint_t dest = get_label_dest(as, label);
497     mp_int_t rel = dest - as->base.code_offset;
498     rel -= 4; // account for instruction prefetch, PC is 4 bytes ahead of this instruction
499     if (dest != (mp_uint_t)-1 && rel <= -4) {
500         // is a backwards jump, so we know the size of the jump on the first pass
501         // calculate rel assuming 12 bit relative jump
502         if (SIGNED_FIT12(rel)) {
503             asm_thumb_op16(as, OP_B_N(rel));
504         } else {
505             goto large_jump;
506         }
507     } else {
508         // is a forwards jump, so need to assume it's large
509     large_jump:
510         #if MICROPY_EMIT_THUMB_ARMV7M
511         asm_thumb_op32(as, OP_BW_HI(rel), OP_BW_LO(rel));
512         #else
513         if (SIGNED_FIT12(rel)) {
514             // this code path has to be the same number of instructions irrespective of rel
515             asm_thumb_op16(as, OP_B_N(rel));
516         } else {
517             asm_thumb_op16(as, ASM_THUMB_OP_NOP);
518             if (dest != (mp_uint_t)-1) {
519                 // we have an actual branch > 12 bits; this is not handled yet
520                 mp_raise_NotImplementedError(MP_ERROR_TEXT("native method too big"));
521             }
522         }
523         #endif
524     }
525 }
526 
asm_thumb_bcc_label(asm_thumb_t * as,int cond,uint label)527 void asm_thumb_bcc_label(asm_thumb_t *as, int cond, uint label) {
528     mp_uint_t dest = get_label_dest(as, label);
529     mp_int_t rel = dest - as->base.code_offset;
530     rel -= 4; // account for instruction prefetch, PC is 4 bytes ahead of this instruction
531     if (dest != (mp_uint_t)-1 && rel <= -4) {
532         // is a backwards jump, so we know the size of the jump on the first pass
533         // calculate rel assuming 9 bit relative jump
534         if (SIGNED_FIT9(rel)) {
535             asm_thumb_op16(as, OP_BCC_N(cond, rel));
536         } else {
537             goto large_jump;
538         }
539     } else {
540         // is a forwards jump, so need to assume it's large
541     large_jump:
542         #if MICROPY_EMIT_THUMB_ARMV7M
543         asm_thumb_op32(as, OP_BCC_W_HI(cond, rel), OP_BCC_W_LO(rel));
544         #else
545         // reverse the sense of the branch to jump over a longer branch
546         asm_thumb_op16(as, OP_BCC_N(cond ^ 1, 0));
547         asm_thumb_b_label(as, label);
548         #endif
549     }
550 }
551 
asm_thumb_bcc_rel9(asm_thumb_t * as,int cond,int rel)552 void asm_thumb_bcc_rel9(asm_thumb_t *as, int cond, int rel) {
553     rel -= 4; // account for instruction prefetch, PC is 4 bytes ahead of this instruction
554     assert(SIGNED_FIT9(rel));
555     asm_thumb_op16(as, OP_BCC_N(cond, rel));
556 }
557 
asm_thumb_b_rel12(asm_thumb_t * as,int rel)558 void asm_thumb_b_rel12(asm_thumb_t *as, int rel) {
559     rel -= 4; // account for instruction prefetch, PC is 4 bytes ahead of this instruction
560     assert(SIGNED_FIT12(rel));
561     asm_thumb_op16(as, OP_B_N(rel));
562 }
563 
564 #define OP_BLX(reg) (0x4780 | ((reg) << 3))
565 #define OP_SVC(arg) (0xdf00 | (arg))
566 
asm_thumb_bl_ind(asm_thumb_t * as,uint fun_id,uint reg_temp)567 void asm_thumb_bl_ind(asm_thumb_t *as, uint fun_id, uint reg_temp) {
568     // Load ptr to function from table, indexed by fun_id, then call it
569     asm_thumb_ldr_reg_reg_i12_optimised(as, reg_temp, ASM_THUMB_REG_FUN_TABLE, fun_id);
570     asm_thumb_op16(as, OP_BLX(reg_temp));
571 }
572 
573 #endif // MICROPY_EMIT_THUMB || MICROPY_EMIT_INLINE_THUMB
574