asmarm.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  1. /*
  2. * This file is part of the MicroPython project, http://micropython.org/
  3. *
  4. * The MIT License (MIT)
  5. *
  6. * Copyright (c) 2014 Fabian Vogt
  7. * Copyright (c) 2013, 2014 Damien P. George
  8. *
  9. * Permission is hereby granted, free of charge, to any person obtaining a copy
  10. * of this software and associated documentation files (the "Software"), to deal
  11. * in the Software without restriction, including without limitation the rights
  12. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  13. * copies of the Software, and to permit persons to whom the Software is
  14. * furnished to do so, subject to the following conditions:
  15. *
  16. * The above copyright notice and this permission notice shall be included in
  17. * all copies or substantial portions of the Software.
  18. *
  19. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  20. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  22. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  23. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  24. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  25. * THE SOFTWARE.
  26. */
  27. #include <stdio.h>
  28. #include <assert.h>
  29. #include <string.h>
  30. #include "py/mpconfig.h"
  31. // wrapper around everything in this file
  32. #if MICROPY_EMIT_ARM
  33. #include "py/asmarm.h"
  34. #define SIGNED_FIT24(x) (((x) & 0xff800000) == 0) || (((x) & 0xff000000) == 0xff000000)
  35. void asm_arm_end_pass(asm_arm_t *as) {
  36. if (as->base.pass == MP_ASM_PASS_EMIT) {
  37. #if defined(__linux__) && defined(__GNUC__)
  38. char *start = mp_asm_base_get_code(&as->base);
  39. char *end = start + mp_asm_base_get_code_size(&as->base);
  40. __builtin___clear_cache(start, end);
  41. #elif defined(__arm__)
  42. // flush I- and D-cache
  43. asm volatile(
  44. "0:"
  45. "mrc p15, 0, r15, c7, c10, 3\n"
  46. "bne 0b\n"
  47. "mov r0, #0\n"
  48. "mcr p15, 0, r0, c7, c7, 0\n"
  49. : : : "r0", "cc");
  50. #endif
  51. }
  52. }
  53. // Insert word into instruction flow
  54. STATIC void emit(asm_arm_t *as, uint op) {
  55. uint8_t *c = mp_asm_base_get_cur_to_write_bytes(&as->base, 4);
  56. if (c != NULL) {
  57. *(uint32_t*)c = op;
  58. }
  59. }
  60. // Insert word into instruction flow, add "ALWAYS" condition code
  61. STATIC void emit_al(asm_arm_t *as, uint op) {
  62. emit(as, op | ASM_ARM_CC_AL);
  63. }
  64. // Basic instructions without condition code
  65. STATIC uint asm_arm_op_push(uint reglist) {
  66. // stmfd sp!, {reglist}
  67. return 0x92d0000 | (reglist & 0xFFFF);
  68. }
  69. STATIC uint asm_arm_op_pop(uint reglist) {
  70. // ldmfd sp!, {reglist}
  71. return 0x8bd0000 | (reglist & 0xFFFF);
  72. }
  73. STATIC uint asm_arm_op_mov_reg(uint rd, uint rn) {
  74. // mov rd, rn
  75. return 0x1a00000 | (rd << 12) | rn;
  76. }
  77. STATIC uint asm_arm_op_mov_imm(uint rd, uint imm) {
  78. // mov rd, #imm
  79. return 0x3a00000 | (rd << 12) | imm;
  80. }
  81. STATIC uint asm_arm_op_mvn_imm(uint rd, uint imm) {
  82. // mvn rd, #imm
  83. return 0x3e00000 | (rd << 12) | imm;
  84. }
  85. STATIC uint asm_arm_op_add_imm(uint rd, uint rn, uint imm) {
  86. // add rd, rn, #imm
  87. return 0x2800000 | (rn << 16) | (rd << 12) | (imm & 0xFF);
  88. }
  89. STATIC uint asm_arm_op_add_reg(uint rd, uint rn, uint rm) {
  90. // add rd, rn, rm
  91. return 0x0800000 | (rn << 16) | (rd << 12) | rm;
  92. }
  93. STATIC uint asm_arm_op_sub_imm(uint rd, uint rn, uint imm) {
  94. // sub rd, rn, #imm
  95. return 0x2400000 | (rn << 16) | (rd << 12) | (imm & 0xFF);
  96. }
  97. STATIC uint asm_arm_op_sub_reg(uint rd, uint rn, uint rm) {
  98. // sub rd, rn, rm
  99. return 0x0400000 | (rn << 16) | (rd << 12) | rm;
  100. }
  101. STATIC uint asm_arm_op_mul_reg(uint rd, uint rm, uint rs) {
  102. // mul rd, rm, rs
  103. assert(rd != rm);
  104. return 0x0000090 | (rd << 16) | (rs << 8) | rm;
  105. }
  106. STATIC uint asm_arm_op_and_reg(uint rd, uint rn, uint rm) {
  107. // and rd, rn, rm
  108. return 0x0000000 | (rn << 16) | (rd << 12) | rm;
  109. }
  110. STATIC uint asm_arm_op_eor_reg(uint rd, uint rn, uint rm) {
  111. // eor rd, rn, rm
  112. return 0x0200000 | (rn << 16) | (rd << 12) | rm;
  113. }
  114. STATIC uint asm_arm_op_orr_reg(uint rd, uint rn, uint rm) {
  115. // orr rd, rn, rm
  116. return 0x1800000 | (rn << 16) | (rd << 12) | rm;
  117. }
  118. void asm_arm_bkpt(asm_arm_t *as) {
  119. // bkpt #0
  120. emit_al(as, 0x1200070);
  121. }
  122. // locals:
  123. // - stored on the stack in ascending order
  124. // - numbered 0 through num_locals-1
  125. // - SP points to first local
  126. //
  127. // | SP
  128. // v
  129. // l0 l1 l2 ... l(n-1)
  130. // ^ ^
  131. // | low address | high address in RAM
  132. void asm_arm_entry(asm_arm_t *as, int num_locals) {
  133. assert(num_locals >= 0);
  134. as->stack_adjust = 0;
  135. as->push_reglist = 1 << ASM_ARM_REG_R1
  136. | 1 << ASM_ARM_REG_R2
  137. | 1 << ASM_ARM_REG_R3
  138. | 1 << ASM_ARM_REG_R4
  139. | 1 << ASM_ARM_REG_R5
  140. | 1 << ASM_ARM_REG_R6
  141. | 1 << ASM_ARM_REG_R7
  142. | 1 << ASM_ARM_REG_R8;
  143. // Only adjust the stack if there are more locals than usable registers
  144. if (num_locals > 3) {
  145. as->stack_adjust = num_locals * 4;
  146. // Align stack to 8 bytes
  147. if (num_locals & 1) {
  148. as->stack_adjust += 4;
  149. }
  150. }
  151. emit_al(as, asm_arm_op_push(as->push_reglist | 1 << ASM_ARM_REG_LR));
  152. if (as->stack_adjust > 0) {
  153. emit_al(as, asm_arm_op_sub_imm(ASM_ARM_REG_SP, ASM_ARM_REG_SP, as->stack_adjust));
  154. }
  155. }
  156. void asm_arm_exit(asm_arm_t *as) {
  157. if (as->stack_adjust > 0) {
  158. emit_al(as, asm_arm_op_add_imm(ASM_ARM_REG_SP, ASM_ARM_REG_SP, as->stack_adjust));
  159. }
  160. emit_al(as, asm_arm_op_pop(as->push_reglist | (1 << ASM_ARM_REG_PC)));
  161. }
  162. void asm_arm_push(asm_arm_t *as, uint reglist) {
  163. emit_al(as, asm_arm_op_push(reglist));
  164. }
  165. void asm_arm_pop(asm_arm_t *as, uint reglist) {
  166. emit_al(as, asm_arm_op_pop(reglist));
  167. }
  168. void asm_arm_mov_reg_reg(asm_arm_t *as, uint reg_dest, uint reg_src) {
  169. emit_al(as, asm_arm_op_mov_reg(reg_dest, reg_src));
  170. }
  171. size_t asm_arm_mov_reg_i32(asm_arm_t *as, uint rd, int imm) {
  172. // Insert immediate into code and jump over it
  173. emit_al(as, 0x59f0000 | (rd << 12)); // ldr rd, [pc]
  174. emit_al(as, 0xa000000); // b pc
  175. size_t loc = mp_asm_base_get_code_pos(&as->base);
  176. emit(as, imm);
  177. return loc;
  178. }
  179. void asm_arm_mov_reg_i32_optimised(asm_arm_t *as, uint rd, int imm) {
  180. // TODO: There are more variants of immediate values
  181. if ((imm & 0xFF) == imm) {
  182. emit_al(as, asm_arm_op_mov_imm(rd, imm));
  183. } else if (imm < 0 && imm >= -256) {
  184. // mvn is "move not", not "move negative"
  185. emit_al(as, asm_arm_op_mvn_imm(rd, ~imm));
  186. } else {
  187. asm_arm_mov_reg_i32(as, rd, imm);
  188. }
  189. }
  190. void asm_arm_mov_local_reg(asm_arm_t *as, int local_num, uint rd) {
  191. // str rd, [sp, #local_num*4]
  192. emit_al(as, 0x58d0000 | (rd << 12) | (local_num << 2));
  193. }
  194. void asm_arm_mov_reg_local(asm_arm_t *as, uint rd, int local_num) {
  195. // ldr rd, [sp, #local_num*4]
  196. emit_al(as, 0x59d0000 | (rd << 12) | (local_num << 2));
  197. }
  198. void asm_arm_cmp_reg_i8(asm_arm_t *as, uint rd, int imm) {
  199. // cmp rd, #imm
  200. emit_al(as, 0x3500000 | (rd << 16) | (imm & 0xFF));
  201. }
  202. void asm_arm_cmp_reg_reg(asm_arm_t *as, uint rd, uint rn) {
  203. // cmp rd, rn
  204. emit_al(as, 0x1500000 | (rd << 16) | rn);
  205. }
  206. void asm_arm_setcc_reg(asm_arm_t *as, uint rd, uint cond) {
  207. emit(as, asm_arm_op_mov_imm(rd, 1) | cond); // movCOND rd, #1
  208. emit(as, asm_arm_op_mov_imm(rd, 0) | (cond ^ (1 << 28))); // mov!COND rd, #0
  209. }
  210. void asm_arm_add_reg_reg_reg(asm_arm_t *as, uint rd, uint rn, uint rm) {
  211. // add rd, rn, rm
  212. emit_al(as, asm_arm_op_add_reg(rd, rn, rm));
  213. }
  214. void asm_arm_sub_reg_reg_reg(asm_arm_t *as, uint rd, uint rn, uint rm) {
  215. // sub rd, rn, rm
  216. emit_al(as, asm_arm_op_sub_reg(rd, rn, rm));
  217. }
  218. void asm_arm_mul_reg_reg_reg(asm_arm_t *as, uint rd, uint rs, uint rm) {
  219. // rs and rm are swapped because of restriction rd!=rm
  220. // mul rd, rm, rs
  221. emit_al(as, asm_arm_op_mul_reg(rd, rm, rs));
  222. }
  223. void asm_arm_and_reg_reg_reg(asm_arm_t *as, uint rd, uint rn, uint rm) {
  224. // and rd, rn, rm
  225. emit_al(as, asm_arm_op_and_reg(rd, rn, rm));
  226. }
  227. void asm_arm_eor_reg_reg_reg(asm_arm_t *as, uint rd, uint rn, uint rm) {
  228. // eor rd, rn, rm
  229. emit_al(as, asm_arm_op_eor_reg(rd, rn, rm));
  230. }
  231. void asm_arm_orr_reg_reg_reg(asm_arm_t *as, uint rd, uint rn, uint rm) {
  232. // orr rd, rn, rm
  233. emit_al(as, asm_arm_op_orr_reg(rd, rn, rm));
  234. }
  235. void asm_arm_mov_reg_local_addr(asm_arm_t *as, uint rd, int local_num) {
  236. // add rd, sp, #local_num*4
  237. emit_al(as, asm_arm_op_add_imm(rd, ASM_ARM_REG_SP, local_num << 2));
  238. }
  239. void asm_arm_mov_reg_pcrel(asm_arm_t *as, uint reg_dest, uint label) {
  240. assert(label < as->base.max_num_labels);
  241. mp_uint_t dest = as->base.label_offsets[label];
  242. mp_int_t rel = dest - as->base.code_offset;
  243. rel -= 12 + 8; // adjust for load of rel, and then PC+8 prefetch of add_reg_reg_reg
  244. // To load rel int reg_dest, insert immediate into code and jump over it
  245. emit_al(as, 0x59f0000 | (reg_dest << 12)); // ldr rd, [pc]
  246. emit_al(as, 0xa000000); // b pc
  247. emit(as, rel);
  248. // Do reg_dest += PC
  249. asm_arm_add_reg_reg_reg(as, reg_dest, reg_dest, ASM_ARM_REG_PC);
  250. }
  251. void asm_arm_lsl_reg_reg(asm_arm_t *as, uint rd, uint rs) {
  252. // mov rd, rd, lsl rs
  253. emit_al(as, 0x1a00010 | (rd << 12) | (rs << 8) | rd);
  254. }
  255. void asm_arm_asr_reg_reg(asm_arm_t *as, uint rd, uint rs) {
  256. // mov rd, rd, asr rs
  257. emit_al(as, 0x1a00050 | (rd << 12) | (rs << 8) | rd);
  258. }
  259. void asm_arm_ldr_reg_reg(asm_arm_t *as, uint rd, uint rn, uint byte_offset) {
  260. // ldr rd, [rn, #off]
  261. emit_al(as, 0x5900000 | (rn << 16) | (rd << 12) | byte_offset);
  262. }
  263. void asm_arm_ldrh_reg_reg(asm_arm_t *as, uint rd, uint rn) {
  264. // ldrh rd, [rn]
  265. emit_al(as, 0x1d000b0 | (rn << 16) | (rd << 12));
  266. }
  267. void asm_arm_ldrb_reg_reg(asm_arm_t *as, uint rd, uint rn) {
  268. // ldrb rd, [rn]
  269. emit_al(as, 0x5d00000 | (rn << 16) | (rd << 12));
  270. }
  271. void asm_arm_str_reg_reg(asm_arm_t *as, uint rd, uint rm, uint byte_offset) {
  272. // str rd, [rm, #off]
  273. emit_al(as, 0x5800000 | (rm << 16) | (rd << 12) | byte_offset);
  274. }
  275. void asm_arm_strh_reg_reg(asm_arm_t *as, uint rd, uint rm) {
  276. // strh rd, [rm]
  277. emit_al(as, 0x1c000b0 | (rm << 16) | (rd << 12));
  278. }
  279. void asm_arm_strb_reg_reg(asm_arm_t *as, uint rd, uint rm) {
  280. // strb rd, [rm]
  281. emit_al(as, 0x5c00000 | (rm << 16) | (rd << 12));
  282. }
  283. void asm_arm_str_reg_reg_reg(asm_arm_t *as, uint rd, uint rm, uint rn) {
  284. // str rd, [rm, rn, lsl #2]
  285. emit_al(as, 0x7800100 | (rm << 16) | (rd << 12) | rn);
  286. }
  287. void asm_arm_strh_reg_reg_reg(asm_arm_t *as, uint rd, uint rm, uint rn) {
  288. // strh doesn't support scaled register index
  289. emit_al(as, 0x1a00080 | (ASM_ARM_REG_R8 << 12) | rn); // mov r8, rn, lsl #1
  290. emit_al(as, 0x18000b0 | (rm << 16) | (rd << 12) | ASM_ARM_REG_R8); // strh rd, [rm, r8]
  291. }
  292. void asm_arm_strb_reg_reg_reg(asm_arm_t *as, uint rd, uint rm, uint rn) {
  293. // strb rd, [rm, rn]
  294. emit_al(as, 0x7c00000 | (rm << 16) | (rd << 12) | rn);
  295. }
  296. void asm_arm_bcc_label(asm_arm_t *as, int cond, uint label) {
  297. assert(label < as->base.max_num_labels);
  298. mp_uint_t dest = as->base.label_offsets[label];
  299. mp_int_t rel = dest - as->base.code_offset;
  300. rel -= 8; // account for instruction prefetch, PC is 8 bytes ahead of this instruction
  301. rel >>= 2; // in ARM mode the branch target is 32-bit aligned, so the 2 LSB are omitted
  302. if (SIGNED_FIT24(rel)) {
  303. emit(as, cond | 0xa000000 | (rel & 0xffffff));
  304. } else {
  305. printf("asm_arm_bcc: branch does not fit in 24 bits\n");
  306. }
  307. }
  308. void asm_arm_b_label(asm_arm_t *as, uint label) {
  309. asm_arm_bcc_label(as, ASM_ARM_CC_AL, label);
  310. }
  311. void asm_arm_bl_ind(asm_arm_t *as, uint fun_id, uint reg_temp) {
  312. // The table offset should fit into the ldr instruction
  313. assert(fun_id < (0x1000 / 4));
  314. emit_al(as, asm_arm_op_mov_reg(ASM_ARM_REG_LR, ASM_ARM_REG_PC)); // mov lr, pc
  315. emit_al(as, 0x597f000 | (fun_id << 2)); // ldr pc, [r7, #fun_id*4]
  316. }
  317. void asm_arm_bx_reg(asm_arm_t *as, uint reg_src) {
  318. emit_al(as, 0x012fff10 | reg_src);
  319. }
  320. #endif // MICROPY_EMIT_ARM