asmthumb.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413
  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. #include <stdio.h>
  27. #include <assert.h>
  28. #include <string.h>
  29. #include "py/mpconfig.h"
  30. // wrapper around everything in this file
  31. #if MICROPY_EMIT_THUMB || MICROPY_EMIT_INLINE_THUMB
  32. #include "py/mpstate.h"
  33. #include "py/persistentcode.h"
  34. #include "py/mphal.h"
  35. #include "py/asmthumb.h"
  36. #define UNSIGNED_FIT5(x) ((uint32_t)(x) < 32)
  37. #define UNSIGNED_FIT7(x) ((uint32_t)(x) < 128)
  38. #define UNSIGNED_FIT8(x) (((x) & 0xffffff00) == 0)
  39. #define UNSIGNED_FIT16(x) (((x) & 0xffff0000) == 0)
  40. #define SIGNED_FIT8(x) (((x) & 0xffffff80) == 0) || (((x) & 0xffffff80) == 0xffffff80)
  41. #define SIGNED_FIT9(x) (((x) & 0xffffff00) == 0) || (((x) & 0xffffff00) == 0xffffff00)
  42. #define SIGNED_FIT12(x) (((x) & 0xfffff800) == 0) || (((x) & 0xfffff800) == 0xfffff800)
  43. #define SIGNED_FIT23(x) (((x) & 0xffc00000) == 0) || (((x) & 0xffc00000) == 0xffc00000)
  44. // Note: these actually take an imm12 but the high-bit is not encoded here
  45. #define OP_ADD_W_RRI_HI(reg_src) (0xf200 | (reg_src))
  46. #define OP_ADD_W_RRI_LO(reg_dest, imm11) ((imm11 << 4 & 0x7000) | reg_dest << 8 | (imm11 & 0xff))
  47. #define OP_SUB_W_RRI_HI(reg_src) (0xf2a0 | (reg_src))
  48. #define OP_SUB_W_RRI_LO(reg_dest, imm11) ((imm11 << 4 & 0x7000) | reg_dest << 8 | (imm11 & 0xff))
  49. #define OP_LDR_W_HI(reg_base) (0xf8d0 | (reg_base))
  50. #define OP_LDR_W_LO(reg_dest, imm12) ((reg_dest) << 12 | (imm12))
  51. static inline byte *asm_thumb_get_cur_to_write_bytes(asm_thumb_t *as, int n) {
  52. return mp_asm_base_get_cur_to_write_bytes(&as->base, n);
  53. }
  54. void asm_thumb_end_pass(asm_thumb_t *as) {
  55. (void)as;
  56. // could check labels are resolved...
  57. #if __ICACHE_PRESENT == 1
  58. if (as->base.pass == MP_ASM_PASS_EMIT) {
  59. // flush D-cache, so the code emitted is stored in memory
  60. MP_HAL_CLEAN_DCACHE(as->base.code_base, as->base.code_size);
  61. // invalidate I-cache
  62. SCB_InvalidateICache();
  63. }
  64. #endif
  65. }
  66. /*
  67. STATIC void asm_thumb_write_byte_1(asm_thumb_t *as, byte b1) {
  68. byte *c = asm_thumb_get_cur_to_write_bytes(as, 1);
  69. c[0] = b1;
  70. }
  71. */
  72. /*
  73. #define IMM32_L0(x) ((x) & 0xff)
  74. #define IMM32_L1(x) (((x) >> 8) & 0xff)
  75. #define IMM32_L2(x) (((x) >> 16) & 0xff)
  76. #define IMM32_L3(x) (((x) >> 24) & 0xff)
  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. // rlolist is a bit map indicating desired lo-registers
  86. #define OP_PUSH_RLIST(rlolist) (0xb400 | (rlolist))
  87. #define OP_PUSH_RLIST_LR(rlolist) (0xb400 | 0x0100 | (rlolist))
  88. #define OP_POP_RLIST(rlolist) (0xbc00 | (rlolist))
  89. #define OP_POP_RLIST_PC(rlolist) (0xbc00 | 0x0100 | (rlolist))
  90. // The number of words must fit in 7 unsigned bits
  91. #define OP_ADD_SP(num_words) (0xb000 | (num_words))
  92. #define OP_SUB_SP(num_words) (0xb080 | (num_words))
  93. // locals:
  94. // - stored on the stack in ascending order
  95. // - numbered 0 through num_locals-1
  96. // - SP points to first local
  97. //
  98. // | SP
  99. // v
  100. // l0 l1 l2 ... l(n-1)
  101. // ^ ^
  102. // | low address | high address in RAM
  103. void asm_thumb_entry(asm_thumb_t *as, int num_locals) {
  104. assert(num_locals >= 0);
  105. // If this Thumb machine code is run from ARM state then add a prelude
  106. // to switch to Thumb state for the duration of the function.
  107. #if MICROPY_DYNAMIC_COMPILER || MICROPY_EMIT_ARM || (defined(__arm__) && !defined(__thumb2__))
  108. #if MICROPY_DYNAMIC_COMPILER
  109. if (mp_dynamic_compiler.native_arch == MP_NATIVE_ARCH_ARMV6)
  110. #endif
  111. {
  112. asm_thumb_op32(as, 0x4010, 0xe92d); // push {r4, lr}
  113. asm_thumb_op32(as, 0xe009, 0xe28f); // add lr, pc, 8 + 1
  114. asm_thumb_op32(as, 0xff3e, 0xe12f); // blx lr
  115. asm_thumb_op32(as, 0x4010, 0xe8bd); // pop {r4, lr}
  116. asm_thumb_op32(as, 0xff1e, 0xe12f); // bx lr
  117. }
  118. #endif
  119. // work out what to push and how many extra spaces to reserve on stack
  120. // so that we have enough for all locals and it's aligned an 8-byte boundary
  121. // we push extra regs (r1, r2, r3) to help do the stack adjustment
  122. // we probably should just always subtract from sp, since this would be more efficient
  123. // for push rlist, lowest numbered register at the lowest address
  124. uint reglist;
  125. uint stack_adjust;
  126. // don't pop r0 because it's used for return value
  127. switch (num_locals) {
  128. case 0:
  129. reglist = 0xf2;
  130. stack_adjust = 0;
  131. break;
  132. case 1:
  133. reglist = 0xf2;
  134. stack_adjust = 0;
  135. break;
  136. case 2:
  137. reglist = 0xfe;
  138. stack_adjust = 0;
  139. break;
  140. case 3:
  141. reglist = 0xfe;
  142. stack_adjust = 0;
  143. break;
  144. default:
  145. reglist = 0xfe;
  146. stack_adjust = ((num_locals - 3) + 1) & (~1);
  147. break;
  148. }
  149. asm_thumb_op16(as, OP_PUSH_RLIST_LR(reglist));
  150. if (stack_adjust > 0) {
  151. if (UNSIGNED_FIT7(stack_adjust)) {
  152. asm_thumb_op16(as, OP_SUB_SP(stack_adjust));
  153. } else {
  154. 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));
  155. }
  156. }
  157. as->push_reglist = reglist;
  158. as->stack_adjust = stack_adjust;
  159. }
  160. void asm_thumb_exit(asm_thumb_t *as) {
  161. if (as->stack_adjust > 0) {
  162. if (UNSIGNED_FIT7(as->stack_adjust)) {
  163. asm_thumb_op16(as, OP_ADD_SP(as->stack_adjust));
  164. } else {
  165. 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));
  166. }
  167. }
  168. asm_thumb_op16(as, OP_POP_RLIST_PC(as->push_reglist));
  169. }
  170. STATIC mp_uint_t get_label_dest(asm_thumb_t *as, uint label) {
  171. assert(label < as->base.max_num_labels);
  172. return as->base.label_offsets[label];
  173. }
  174. void asm_thumb_op16(asm_thumb_t *as, uint op) {
  175. byte *c = asm_thumb_get_cur_to_write_bytes(as, 2);
  176. if (c != NULL) {
  177. // little endian
  178. c[0] = op;
  179. c[1] = op >> 8;
  180. }
  181. }
  182. void asm_thumb_op32(asm_thumb_t *as, uint op1, uint op2) {
  183. byte *c = asm_thumb_get_cur_to_write_bytes(as, 4);
  184. if (c != NULL) {
  185. // little endian, op1 then op2
  186. c[0] = op1;
  187. c[1] = op1 >> 8;
  188. c[2] = op2;
  189. c[3] = op2 >> 8;
  190. }
  191. }
  192. #define OP_FORMAT_4(op, rlo_dest, rlo_src) ((op) | ((rlo_src) << 3) | (rlo_dest))
  193. void asm_thumb_format_4(asm_thumb_t *as, uint op, uint rlo_dest, uint rlo_src) {
  194. assert(rlo_dest < ASM_THUMB_REG_R8);
  195. assert(rlo_src < ASM_THUMB_REG_R8);
  196. asm_thumb_op16(as, OP_FORMAT_4(op, rlo_dest, rlo_src));
  197. }
  198. void asm_thumb_mov_reg_reg(asm_thumb_t *as, uint reg_dest, uint reg_src) {
  199. uint op_lo;
  200. if (reg_src < 8) {
  201. op_lo = reg_src << 3;
  202. } else {
  203. op_lo = 0x40 | ((reg_src - 8) << 3);
  204. }
  205. if (reg_dest < 8) {
  206. op_lo |= reg_dest;
  207. } else {
  208. op_lo |= 0x80 | (reg_dest - 8);
  209. }
  210. // mov reg_dest, reg_src
  211. asm_thumb_op16(as, 0x4600 | op_lo);
  212. }
  213. // if loading lo half with movw, the i16 value will be zero extended into the r32 register!
  214. size_t asm_thumb_mov_reg_i16(asm_thumb_t *as, uint mov_op, uint reg_dest, int i16_src) {
  215. assert(reg_dest < ASM_THUMB_REG_R15);
  216. size_t loc = mp_asm_base_get_code_pos(&as->base);
  217. // mov[wt] reg_dest, #i16_src
  218. asm_thumb_op32(as, mov_op | ((i16_src >> 1) & 0x0400) | ((i16_src >> 12) & 0xf), ((i16_src << 4) & 0x7000) | (reg_dest << 8) | (i16_src & 0xff));
  219. return loc;
  220. }
  221. #define OP_B_N(byte_offset) (0xe000 | (((byte_offset) >> 1) & 0x07ff))
  222. bool asm_thumb_b_n_label(asm_thumb_t *as, uint label) {
  223. mp_uint_t dest = get_label_dest(as, label);
  224. mp_int_t rel = dest - as->base.code_offset;
  225. rel -= 4; // account for instruction prefetch, PC is 4 bytes ahead of this instruction
  226. asm_thumb_op16(as, OP_B_N(rel));
  227. return as->base.pass != MP_ASM_PASS_EMIT || SIGNED_FIT12(rel);
  228. }
  229. #define OP_BCC_N(cond, byte_offset) (0xd000 | ((cond) << 8) | (((byte_offset) >> 1) & 0x00ff))
  230. // all these bit arithmetics need coverage testing!
  231. #define OP_BCC_W_HI(cond, byte_offset) (0xf000 | ((cond) << 6) | (((byte_offset) >> 10) & 0x0400) | (((byte_offset) >> 14) & 0x003f))
  232. #define OP_BCC_W_LO(byte_offset) (0x8000 | ((byte_offset) & 0x2000) | (((byte_offset) >> 1) & 0x0fff))
  233. bool asm_thumb_bcc_nw_label(asm_thumb_t *as, int cond, uint label, bool wide) {
  234. mp_uint_t dest = get_label_dest(as, label);
  235. mp_int_t rel = dest - as->base.code_offset;
  236. rel -= 4; // account for instruction prefetch, PC is 4 bytes ahead of this instruction
  237. if (!wide) {
  238. asm_thumb_op16(as, OP_BCC_N(cond, rel));
  239. return as->base.pass != MP_ASM_PASS_EMIT || SIGNED_FIT9(rel);
  240. } else {
  241. asm_thumb_op32(as, OP_BCC_W_HI(cond, rel), OP_BCC_W_LO(rel));
  242. return true;
  243. }
  244. }
  245. #define OP_BL_HI(byte_offset) (0xf000 | (((byte_offset) >> 12) & 0x07ff))
  246. #define OP_BL_LO(byte_offset) (0xf800 | (((byte_offset) >> 1) & 0x07ff))
  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. size_t asm_thumb_mov_reg_i32(asm_thumb_t *as, uint reg_dest, mp_uint_t i32) {
  255. // movw, movt does it in 8 bytes
  256. // ldr [pc, #], dw does it in 6 bytes, but we might not reach to end of code for dw
  257. size_t loc = mp_asm_base_get_code_pos(&as->base);
  258. asm_thumb_mov_reg_i16(as, ASM_THUMB_OP_MOVW, reg_dest, i32);
  259. asm_thumb_mov_reg_i16(as, ASM_THUMB_OP_MOVT, reg_dest, i32 >> 16);
  260. return loc;
  261. }
  262. void asm_thumb_mov_reg_i32_optimised(asm_thumb_t *as, uint reg_dest, int i32) {
  263. if (reg_dest < 8 && UNSIGNED_FIT8(i32)) {
  264. asm_thumb_mov_rlo_i8(as, reg_dest, i32);
  265. } else if (UNSIGNED_FIT16(i32)) {
  266. asm_thumb_mov_reg_i16(as, ASM_THUMB_OP_MOVW, reg_dest, i32);
  267. } else {
  268. asm_thumb_mov_reg_i32(as, reg_dest, i32);
  269. }
  270. }
  271. #define OP_STR_TO_SP_OFFSET(rlo_dest, word_offset) (0x9000 | ((rlo_dest) << 8) | ((word_offset) & 0x00ff))
  272. #define OP_LDR_FROM_SP_OFFSET(rlo_dest, word_offset) (0x9800 | ((rlo_dest) << 8) | ((word_offset) & 0x00ff))
  273. void asm_thumb_mov_local_reg(asm_thumb_t *as, int local_num, uint rlo_src) {
  274. assert(rlo_src < ASM_THUMB_REG_R8);
  275. int word_offset = local_num;
  276. assert(as->base.pass < MP_ASM_PASS_EMIT || word_offset >= 0);
  277. asm_thumb_op16(as, OP_STR_TO_SP_OFFSET(rlo_src, word_offset));
  278. }
  279. void asm_thumb_mov_reg_local(asm_thumb_t *as, uint rlo_dest, int local_num) {
  280. assert(rlo_dest < ASM_THUMB_REG_R8);
  281. int word_offset = local_num;
  282. assert(as->base.pass < MP_ASM_PASS_EMIT || word_offset >= 0);
  283. asm_thumb_op16(as, OP_LDR_FROM_SP_OFFSET(rlo_dest, word_offset));
  284. }
  285. #define OP_ADD_REG_SP_OFFSET(rlo_dest, word_offset) (0xa800 | ((rlo_dest) << 8) | ((word_offset) & 0x00ff))
  286. void asm_thumb_mov_reg_local_addr(asm_thumb_t *as, uint rlo_dest, int local_num) {
  287. assert(rlo_dest < ASM_THUMB_REG_R8);
  288. int word_offset = local_num;
  289. assert(as->base.pass < MP_ASM_PASS_EMIT || word_offset >= 0);
  290. asm_thumb_op16(as, OP_ADD_REG_SP_OFFSET(rlo_dest, word_offset));
  291. }
  292. void asm_thumb_mov_reg_pcrel(asm_thumb_t *as, uint rlo_dest, uint label) {
  293. mp_uint_t dest = get_label_dest(as, label);
  294. mp_int_t rel = dest - as->base.code_offset;
  295. rel -= 4 + 4; // adjust for mov_reg_i16 and then PC+4 prefetch of add_reg_reg
  296. rel |= 1; // to stay in Thumb state when jumping to this address
  297. asm_thumb_mov_reg_i16(as, ASM_THUMB_OP_MOVW, rlo_dest, rel); // 4 bytes
  298. asm_thumb_add_reg_reg(as, rlo_dest, ASM_THUMB_REG_R15); // 2 bytes
  299. }
  300. static inline void asm_thumb_ldr_reg_reg_i12(asm_thumb_t *as, uint reg_dest, uint reg_base, uint word_offset) {
  301. asm_thumb_op32(as, OP_LDR_W_HI(reg_base), OP_LDR_W_LO(reg_dest, word_offset * 4));
  302. }
  303. void asm_thumb_ldr_reg_reg_i12_optimised(asm_thumb_t *as, uint reg_dest, uint reg_base, uint word_offset) {
  304. if (reg_dest < ASM_THUMB_REG_R8 && reg_base < ASM_THUMB_REG_R8 && UNSIGNED_FIT5(word_offset)) {
  305. asm_thumb_ldr_rlo_rlo_i5(as, reg_dest, reg_base, word_offset);
  306. } else {
  307. asm_thumb_ldr_reg_reg_i12(as, reg_dest, reg_base, word_offset);
  308. }
  309. }
  310. // this could be wrong, because it should have a range of +/- 16MiB...
  311. #define OP_BW_HI(byte_offset) (0xf000 | (((byte_offset) >> 12) & 0x07ff))
  312. #define OP_BW_LO(byte_offset) (0xb800 | (((byte_offset) >> 1) & 0x07ff))
  313. void asm_thumb_b_label(asm_thumb_t *as, uint label) {
  314. mp_uint_t dest = get_label_dest(as, label);
  315. mp_int_t rel = dest - as->base.code_offset;
  316. rel -= 4; // account for instruction prefetch, PC is 4 bytes ahead of this instruction
  317. if (dest != (mp_uint_t)-1 && rel <= -4) {
  318. // is a backwards jump, so we know the size of the jump on the first pass
  319. // calculate rel assuming 12 bit relative jump
  320. if (SIGNED_FIT12(rel)) {
  321. asm_thumb_op16(as, OP_B_N(rel));
  322. } else {
  323. goto large_jump;
  324. }
  325. } else {
  326. // is a forwards jump, so need to assume it's large
  327. large_jump:
  328. asm_thumb_op32(as, OP_BW_HI(rel), OP_BW_LO(rel));
  329. }
  330. }
  331. void asm_thumb_bcc_label(asm_thumb_t *as, int cond, uint label) {
  332. mp_uint_t dest = get_label_dest(as, label);
  333. mp_int_t rel = dest - as->base.code_offset;
  334. rel -= 4; // account for instruction prefetch, PC is 4 bytes ahead of this instruction
  335. if (dest != (mp_uint_t)-1 && rel <= -4) {
  336. // is a backwards jump, so we know the size of the jump on the first pass
  337. // calculate rel assuming 9 bit relative jump
  338. if (SIGNED_FIT9(rel)) {
  339. asm_thumb_op16(as, OP_BCC_N(cond, rel));
  340. } else {
  341. goto large_jump;
  342. }
  343. } else {
  344. // is a forwards jump, so need to assume it's large
  345. large_jump:
  346. asm_thumb_op32(as, OP_BCC_W_HI(cond, rel), OP_BCC_W_LO(rel));
  347. }
  348. }
  349. #define OP_BLX(reg) (0x4780 | ((reg) << 3))
  350. #define OP_SVC(arg) (0xdf00 | (arg))
  351. void asm_thumb_bl_ind(asm_thumb_t *as, uint fun_id, uint reg_temp) {
  352. // Load ptr to function from table, indexed by fun_id, then call it
  353. asm_thumb_ldr_reg_reg_i12_optimised(as, reg_temp, ASM_THUMB_REG_FUN_TABLE, fun_id);
  354. asm_thumb_op16(as, OP_BLX(reg_temp));
  355. }
  356. #endif // MICROPY_EMIT_THUMB || MICROPY_EMIT_INLINE_THUMB