asmthumb.c 14 KB

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