asmthumb.h 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  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. #ifndef MICROPY_INCLUDED_PY_ASMTHUMB_H
  27. #define MICROPY_INCLUDED_PY_ASMTHUMB_H
  28. #include <assert.h>
  29. #include "py/misc.h"
  30. #include "py/asmbase.h"
  31. #include "py/persistentcode.h"
  32. #define ASM_THUMB_REG_R0 (0)
  33. #define ASM_THUMB_REG_R1 (1)
  34. #define ASM_THUMB_REG_R2 (2)
  35. #define ASM_THUMB_REG_R3 (3)
  36. #define ASM_THUMB_REG_R4 (4)
  37. #define ASM_THUMB_REG_R5 (5)
  38. #define ASM_THUMB_REG_R6 (6)
  39. #define ASM_THUMB_REG_R7 (7)
  40. #define ASM_THUMB_REG_R8 (8)
  41. #define ASM_THUMB_REG_R9 (9)
  42. #define ASM_THUMB_REG_R10 (10)
  43. #define ASM_THUMB_REG_R11 (11)
  44. #define ASM_THUMB_REG_R12 (12)
  45. #define ASM_THUMB_REG_R13 (13)
  46. #define ASM_THUMB_REG_R14 (14)
  47. #define ASM_THUMB_REG_R15 (15)
  48. #define ASM_THUMB_REG_SP (ASM_THUMB_REG_R13)
  49. #define ASM_THUMB_REG_LR (REG_R14)
  50. #define ASM_THUMB_CC_EQ (0x0)
  51. #define ASM_THUMB_CC_NE (0x1)
  52. #define ASM_THUMB_CC_CS (0x2)
  53. #define ASM_THUMB_CC_CC (0x3)
  54. #define ASM_THUMB_CC_MI (0x4)
  55. #define ASM_THUMB_CC_PL (0x5)
  56. #define ASM_THUMB_CC_VS (0x6)
  57. #define ASM_THUMB_CC_VC (0x7)
  58. #define ASM_THUMB_CC_HI (0x8)
  59. #define ASM_THUMB_CC_LS (0x9)
  60. #define ASM_THUMB_CC_GE (0xa)
  61. #define ASM_THUMB_CC_LT (0xb)
  62. #define ASM_THUMB_CC_GT (0xc)
  63. #define ASM_THUMB_CC_LE (0xd)
  64. typedef struct _asm_thumb_t {
  65. mp_asm_base_t base;
  66. uint32_t push_reglist;
  67. uint32_t stack_adjust;
  68. } asm_thumb_t;
  69. #if MICROPY_DYNAMIC_COMPILER
  70. static inline bool asm_thumb_allow_armv7m(asm_thumb_t *as) {
  71. return MP_NATIVE_ARCH_ARMV7M <= mp_dynamic_compiler.native_arch
  72. && mp_dynamic_compiler.native_arch <= MP_NATIVE_ARCH_ARMV7EMDP;
  73. }
  74. #else
  75. static inline bool asm_thumb_allow_armv7m(asm_thumb_t *as) {
  76. return MICROPY_EMIT_THUMB_ARMV7M;
  77. }
  78. #endif
  79. static inline void asm_thumb_end_pass(asm_thumb_t *as) {
  80. (void)as;
  81. }
  82. void asm_thumb_entry(asm_thumb_t *as, int num_locals);
  83. void asm_thumb_exit(asm_thumb_t *as);
  84. // argument order follows ARM, in general dest is first
  85. // note there is a difference between movw and mov.w, and many others!
  86. #define ASM_THUMB_OP_IT (0xbf00)
  87. #define ASM_THUMB_OP_ITE_EQ (0xbf0c)
  88. #define ASM_THUMB_OP_ITE_NE (0xbf14)
  89. #define ASM_THUMB_OP_ITE_CS (0xbf2c)
  90. #define ASM_THUMB_OP_ITE_CC (0xbf34)
  91. #define ASM_THUMB_OP_ITE_MI (0xbf4c)
  92. #define ASM_THUMB_OP_ITE_PL (0xbf54)
  93. #define ASM_THUMB_OP_ITE_VS (0xbf6c)
  94. #define ASM_THUMB_OP_ITE_VC (0xbf74)
  95. #define ASM_THUMB_OP_ITE_HI (0xbf8c)
  96. #define ASM_THUMB_OP_ITE_LS (0xbf94)
  97. #define ASM_THUMB_OP_ITE_GE (0xbfac)
  98. #define ASM_THUMB_OP_ITE_LT (0xbfb4)
  99. #define ASM_THUMB_OP_ITE_GT (0xbfcc)
  100. #define ASM_THUMB_OP_ITE_LE (0xbfd4)
  101. #define ASM_THUMB_OP_NOP (0xbf00)
  102. #define ASM_THUMB_OP_WFI (0xbf30)
  103. #define ASM_THUMB_OP_CPSID_I (0xb672) // cpsid i, disable irq
  104. #define ASM_THUMB_OP_CPSIE_I (0xb662) // cpsie i, enable irq
  105. void asm_thumb_op16(asm_thumb_t *as, uint op);
  106. void asm_thumb_op32(asm_thumb_t *as, uint op1, uint op2);
  107. static inline void asm_thumb_it_cc(asm_thumb_t *as, uint cc, uint mask) {
  108. asm_thumb_op16(as, ASM_THUMB_OP_IT | (cc << 4) | mask);
  109. }
  110. // FORMAT 1: move shifted register
  111. #define ASM_THUMB_FORMAT_1_LSL (0x0000)
  112. #define ASM_THUMB_FORMAT_1_LSR (0x0800)
  113. #define ASM_THUMB_FORMAT_1_ASR (0x1000)
  114. #define ASM_THUMB_FORMAT_1_ENCODE(op, rlo_dest, rlo_src, offset) \
  115. ((op) | ((offset) << 6) | ((rlo_src) << 3) | (rlo_dest))
  116. static inline void asm_thumb_format_1(asm_thumb_t *as, uint op, uint rlo_dest, uint rlo_src, uint offset) {
  117. assert(rlo_dest < ASM_THUMB_REG_R8);
  118. assert(rlo_src < ASM_THUMB_REG_R8);
  119. asm_thumb_op16(as, ASM_THUMB_FORMAT_1_ENCODE(op, rlo_dest, rlo_src, offset));
  120. }
  121. // FORMAT 2: add/subtract
  122. #define ASM_THUMB_FORMAT_2_ADD (0x1800)
  123. #define ASM_THUMB_FORMAT_2_SUB (0x1a00)
  124. #define ASM_THUMB_FORMAT_2_REG_OPERAND (0x0000)
  125. #define ASM_THUMB_FORMAT_2_IMM_OPERAND (0x0400)
  126. #define ASM_THUMB_FORMAT_2_ENCODE(op, rlo_dest, rlo_src, src_b) \
  127. ((op) | ((src_b) << 6) | ((rlo_src) << 3) | (rlo_dest))
  128. static inline void asm_thumb_format_2(asm_thumb_t *as, uint op, uint rlo_dest, uint rlo_src, int src_b) {
  129. assert(rlo_dest < ASM_THUMB_REG_R8);
  130. assert(rlo_src < ASM_THUMB_REG_R8);
  131. asm_thumb_op16(as, ASM_THUMB_FORMAT_2_ENCODE(op, rlo_dest, rlo_src, src_b));
  132. }
  133. static inline void asm_thumb_add_rlo_rlo_rlo(asm_thumb_t *as, uint rlo_dest, uint rlo_src_a, uint rlo_src_b) {
  134. asm_thumb_format_2(as, ASM_THUMB_FORMAT_2_ADD | ASM_THUMB_FORMAT_2_REG_OPERAND, rlo_dest, rlo_src_a, rlo_src_b);
  135. }
  136. static inline void asm_thumb_add_rlo_rlo_i3(asm_thumb_t *as, uint rlo_dest, uint rlo_src_a, int i3_src) {
  137. asm_thumb_format_2(as, ASM_THUMB_FORMAT_2_ADD | ASM_THUMB_FORMAT_2_IMM_OPERAND, rlo_dest, rlo_src_a, i3_src);
  138. }
  139. static inline void asm_thumb_sub_rlo_rlo_rlo(asm_thumb_t *as, uint rlo_dest, uint rlo_src_a, uint rlo_src_b) {
  140. asm_thumb_format_2(as, ASM_THUMB_FORMAT_2_SUB | ASM_THUMB_FORMAT_2_REG_OPERAND, rlo_dest, rlo_src_a, rlo_src_b);
  141. }
  142. static inline void asm_thumb_sub_rlo_rlo_i3(asm_thumb_t *as, uint rlo_dest, uint rlo_src_a, int i3_src) {
  143. asm_thumb_format_2(as, ASM_THUMB_FORMAT_2_SUB | ASM_THUMB_FORMAT_2_IMM_OPERAND, rlo_dest, rlo_src_a, i3_src);
  144. }
  145. // FORMAT 3: move/compare/add/subtract immediate
  146. // These instructions all do zero extension of the i8 value
  147. #define ASM_THUMB_FORMAT_3_MOV (0x2000)
  148. #define ASM_THUMB_FORMAT_3_CMP (0x2800)
  149. #define ASM_THUMB_FORMAT_3_ADD (0x3000)
  150. #define ASM_THUMB_FORMAT_3_SUB (0x3800)
  151. #define ASM_THUMB_FORMAT_3_LDR (0x4800)
  152. #define ASM_THUMB_FORMAT_3_ENCODE(op, rlo, i8) ((op) | ((rlo) << 8) | (i8))
  153. static inline void asm_thumb_format_3(asm_thumb_t *as, uint op, uint rlo, int i8) {
  154. assert(rlo < ASM_THUMB_REG_R8);
  155. asm_thumb_op16(as, ASM_THUMB_FORMAT_3_ENCODE(op, rlo, i8));
  156. }
  157. static inline void asm_thumb_mov_rlo_i8(asm_thumb_t *as, uint rlo, int i8) {
  158. asm_thumb_format_3(as, ASM_THUMB_FORMAT_3_MOV, rlo, i8);
  159. }
  160. static inline void asm_thumb_cmp_rlo_i8(asm_thumb_t *as, uint rlo, int i8) {
  161. asm_thumb_format_3(as, ASM_THUMB_FORMAT_3_CMP, rlo, i8);
  162. }
  163. static inline void asm_thumb_add_rlo_i8(asm_thumb_t *as, uint rlo, int i8) {
  164. asm_thumb_format_3(as, ASM_THUMB_FORMAT_3_ADD, rlo, i8);
  165. }
  166. static inline void asm_thumb_sub_rlo_i8(asm_thumb_t *as, uint rlo, int i8) {
  167. asm_thumb_format_3(as, ASM_THUMB_FORMAT_3_SUB, rlo, i8);
  168. }
  169. static inline void asm_thumb_ldr_rlo_pcrel_i8(asm_thumb_t *as, uint rlo, uint i8) {
  170. asm_thumb_format_3(as, ASM_THUMB_FORMAT_3_LDR, rlo, i8);
  171. }
  172. // FORMAT 4: ALU operations
  173. #define ASM_THUMB_FORMAT_4_AND (0x4000)
  174. #define ASM_THUMB_FORMAT_4_EOR (0x4040)
  175. #define ASM_THUMB_FORMAT_4_LSL (0x4080)
  176. #define ASM_THUMB_FORMAT_4_LSR (0x40c0)
  177. #define ASM_THUMB_FORMAT_4_ASR (0x4100)
  178. #define ASM_THUMB_FORMAT_4_ADC (0x4140)
  179. #define ASM_THUMB_FORMAT_4_SBC (0x4180)
  180. #define ASM_THUMB_FORMAT_4_ROR (0x41c0)
  181. #define ASM_THUMB_FORMAT_4_TST (0x4200)
  182. #define ASM_THUMB_FORMAT_4_NEG (0x4240)
  183. #define ASM_THUMB_FORMAT_4_CMP (0x4280)
  184. #define ASM_THUMB_FORMAT_4_CMN (0x42c0)
  185. #define ASM_THUMB_FORMAT_4_ORR (0x4300)
  186. #define ASM_THUMB_FORMAT_4_MUL (0x4340)
  187. #define ASM_THUMB_FORMAT_4_BIC (0x4380)
  188. #define ASM_THUMB_FORMAT_4_MVN (0x43c0)
  189. void asm_thumb_format_4(asm_thumb_t *as, uint op, uint rlo_dest, uint rlo_src);
  190. static inline void asm_thumb_cmp_rlo_rlo(asm_thumb_t *as, uint rlo_dest, uint rlo_src) {
  191. asm_thumb_format_4(as, ASM_THUMB_FORMAT_4_CMP, rlo_dest, rlo_src);
  192. }
  193. static inline void asm_thumb_mvn_rlo_rlo(asm_thumb_t *as, uint rlo_dest, uint rlo_src) {
  194. asm_thumb_format_4(as, ASM_THUMB_FORMAT_4_MVN, rlo_dest, rlo_src);
  195. }
  196. static inline void asm_thumb_neg_rlo_rlo(asm_thumb_t *as, uint rlo_dest, uint rlo_src) {
  197. asm_thumb_format_4(as, ASM_THUMB_FORMAT_4_NEG, rlo_dest, rlo_src);
  198. }
  199. // FORMAT 5: hi register operations (add, cmp, mov, bx)
  200. // For add/cmp/mov, at least one of the args must be a high register
  201. #define ASM_THUMB_FORMAT_5_ADD (0x4400)
  202. #define ASM_THUMB_FORMAT_5_BX (0x4700)
  203. #define ASM_THUMB_FORMAT_5_ENCODE(op, r_dest, r_src) \
  204. ((op) | ((r_dest) << 4 & 0x0080) | ((r_src) << 3) | ((r_dest) & 0x0007))
  205. static inline void asm_thumb_format_5(asm_thumb_t *as, uint op, uint r_dest, uint r_src) {
  206. asm_thumb_op16(as, ASM_THUMB_FORMAT_5_ENCODE(op, r_dest, r_src));
  207. }
  208. static inline void asm_thumb_add_reg_reg(asm_thumb_t *as, uint r_dest, uint r_src) {
  209. asm_thumb_format_5(as, ASM_THUMB_FORMAT_5_ADD, r_dest, r_src);
  210. }
  211. static inline void asm_thumb_bx_reg(asm_thumb_t *as, uint r_src) {
  212. asm_thumb_format_5(as, ASM_THUMB_FORMAT_5_BX, 0, r_src);
  213. }
  214. // FORMAT 9: load/store with immediate offset
  215. // For word transfers the offset must be aligned, and >>2
  216. // FORMAT 10: load/store halfword
  217. // The offset must be aligned, and >>1
  218. // The load is zero extended into the register
  219. #define ASM_THUMB_FORMAT_9_STR (0x6000)
  220. #define ASM_THUMB_FORMAT_9_LDR (0x6800)
  221. #define ASM_THUMB_FORMAT_9_WORD_TRANSFER (0x0000)
  222. #define ASM_THUMB_FORMAT_9_BYTE_TRANSFER (0x1000)
  223. #define ASM_THUMB_FORMAT_10_STRH (0x8000)
  224. #define ASM_THUMB_FORMAT_10_LDRH (0x8800)
  225. #define ASM_THUMB_FORMAT_9_10_ENCODE(op, rlo_dest, rlo_base, offset) \
  226. ((op) | (((offset) << 6) & 0x07c0) | ((rlo_base) << 3) | (rlo_dest))
  227. static inline void asm_thumb_format_9_10(asm_thumb_t *as, uint op, uint rlo_dest, uint rlo_base, uint offset) {
  228. asm_thumb_op16(as, ASM_THUMB_FORMAT_9_10_ENCODE(op, rlo_dest, rlo_base, offset));
  229. }
  230. static inline void asm_thumb_str_rlo_rlo_i5(asm_thumb_t *as, uint rlo_src, uint rlo_base, uint word_offset) {
  231. asm_thumb_format_9_10(as, ASM_THUMB_FORMAT_9_STR | ASM_THUMB_FORMAT_9_WORD_TRANSFER, rlo_src, rlo_base, word_offset);
  232. }
  233. static inline void asm_thumb_strb_rlo_rlo_i5(asm_thumb_t *as, uint rlo_src, uint rlo_base, uint byte_offset) {
  234. asm_thumb_format_9_10(as, ASM_THUMB_FORMAT_9_STR | ASM_THUMB_FORMAT_9_BYTE_TRANSFER, rlo_src, rlo_base, byte_offset);
  235. }
  236. static inline void asm_thumb_strh_rlo_rlo_i5(asm_thumb_t *as, uint rlo_src, uint rlo_base, uint uint16_offset) {
  237. asm_thumb_format_9_10(as, ASM_THUMB_FORMAT_10_STRH, rlo_src, rlo_base, uint16_offset);
  238. }
  239. static inline void asm_thumb_ldr_rlo_rlo_i5(asm_thumb_t *as, uint rlo_dest, uint rlo_base, uint word_offset) {
  240. asm_thumb_format_9_10(as, ASM_THUMB_FORMAT_9_LDR | ASM_THUMB_FORMAT_9_WORD_TRANSFER, rlo_dest, rlo_base, word_offset);
  241. }
  242. static inline void asm_thumb_ldrb_rlo_rlo_i5(asm_thumb_t *as, uint rlo_dest, uint rlo_base, uint byte_offset) {
  243. asm_thumb_format_9_10(as, ASM_THUMB_FORMAT_9_LDR | ASM_THUMB_FORMAT_9_BYTE_TRANSFER, rlo_dest, rlo_base, byte_offset);
  244. }
  245. static inline void asm_thumb_ldrh_rlo_rlo_i5(asm_thumb_t *as, uint rlo_dest, uint rlo_base, uint uint16_offset) {
  246. asm_thumb_format_9_10(as, ASM_THUMB_FORMAT_10_LDRH, rlo_dest, rlo_base, uint16_offset);
  247. }
  248. static inline void asm_thumb_lsl_rlo_rlo_i5(asm_thumb_t *as, uint rlo_dest, uint rlo_src, uint shift) {
  249. asm_thumb_format_1(as, ASM_THUMB_FORMAT_1_LSL, rlo_dest, rlo_src, shift);
  250. }
  251. static inline void asm_thumb_asr_rlo_rlo_i5(asm_thumb_t *as, uint rlo_dest, uint rlo_src, uint shift) {
  252. asm_thumb_format_1(as, ASM_THUMB_FORMAT_1_ASR, rlo_dest, rlo_src, shift);
  253. }
  254. // FORMAT 11: sign/zero extend
  255. #define ASM_THUMB_FORMAT_11_ENCODE(op, rlo_dest, rlo_src) \
  256. ((op) | ((rlo_src) << 3) | (rlo_dest))
  257. #define ASM_THUMB_FORMAT_11_SXTH (0xb200)
  258. #define ASM_THUMB_FORMAT_11_SXTB (0xb240)
  259. #define ASM_THUMB_FORMAT_11_UXTH (0xb280)
  260. #define ASM_THUMB_FORMAT_11_UXTB (0xb2c0)
  261. static inline void asm_thumb_format_11(asm_thumb_t *as, uint op, uint rlo_dest, uint rlo_src) {
  262. assert(rlo_dest < ASM_THUMB_REG_R8);
  263. assert(rlo_src < ASM_THUMB_REG_R8);
  264. asm_thumb_op16(as, ASM_THUMB_FORMAT_11_ENCODE(op, rlo_dest, rlo_src));
  265. }
  266. static inline void asm_thumb_sxth_rlo_rlo(asm_thumb_t *as, uint rlo_dest, uint rlo_src) {
  267. asm_thumb_format_11(as, ASM_THUMB_FORMAT_11_SXTH, rlo_dest, rlo_src);
  268. }
  269. // TODO convert these to above format style
  270. #define ASM_THUMB_OP_MOVW (0xf240)
  271. #define ASM_THUMB_OP_MOVT (0xf2c0)
  272. void asm_thumb_mov_reg_reg(asm_thumb_t *as, uint reg_dest, uint reg_src);
  273. void asm_thumb_mov_reg_i16(asm_thumb_t *as, uint mov_op, uint reg_dest, int i16_src);
  274. // these return true if the destination is in range, false otherwise
  275. bool asm_thumb_b_n_label(asm_thumb_t *as, uint label);
  276. bool asm_thumb_bcc_nw_label(asm_thumb_t *as, int cond, uint label, bool wide);
  277. bool asm_thumb_bl_label(asm_thumb_t *as, uint label);
  278. size_t asm_thumb_mov_reg_i32(asm_thumb_t *as, uint reg_dest, mp_uint_t i32_src); // convenience
  279. void asm_thumb_mov_reg_i32_optimised(asm_thumb_t *as, uint reg_dest, int i32_src); // convenience
  280. void asm_thumb_mov_local_reg(asm_thumb_t *as, int local_num_dest, uint rlo_src); // convenience
  281. void asm_thumb_mov_reg_local(asm_thumb_t *as, uint rlo_dest, int local_num); // convenience
  282. void asm_thumb_mov_reg_local_addr(asm_thumb_t *as, uint rlo_dest, int local_num); // convenience
  283. void asm_thumb_mov_reg_pcrel(asm_thumb_t *as, uint rlo_dest, uint label);
  284. void asm_thumb_ldr_reg_reg_i12_optimised(asm_thumb_t *as, uint reg_dest, uint reg_base, uint word_offset); // convenience
  285. void asm_thumb_ldrh_reg_reg_i12_optimised(asm_thumb_t *as, uint reg_dest, uint reg_base, uint uint16_offset); // convenience
  286. void asm_thumb_b_label(asm_thumb_t *as, uint label); // convenience: picks narrow or wide branch
  287. void asm_thumb_bcc_label(asm_thumb_t *as, int cc, uint label); // convenience: picks narrow or wide branch
  288. void asm_thumb_bl_ind(asm_thumb_t *as, uint fun_id, uint reg_temp); // convenience
  289. void asm_thumb_bcc_rel9(asm_thumb_t *as, int cc, int rel);
  290. void asm_thumb_b_rel12(asm_thumb_t *as, int rel);
  291. // Holds a pointer to mp_fun_table
  292. #define ASM_THUMB_REG_FUN_TABLE ASM_THUMB_REG_R7
  293. #if GENERIC_ASM_API
  294. // The following macros provide a (mostly) arch-independent API to
  295. // generate native code, and are used by the native emitter.
  296. #define ASM_WORD_SIZE (4)
  297. #define REG_RET ASM_THUMB_REG_R0
  298. #define REG_ARG_1 ASM_THUMB_REG_R0
  299. #define REG_ARG_2 ASM_THUMB_REG_R1
  300. #define REG_ARG_3 ASM_THUMB_REG_R2
  301. #define REG_ARG_4 ASM_THUMB_REG_R3
  302. // rest of args go on stack
  303. #define REG_TEMP0 ASM_THUMB_REG_R0
  304. #define REG_TEMP1 ASM_THUMB_REG_R1
  305. #define REG_TEMP2 ASM_THUMB_REG_R2
  306. #define REG_LOCAL_1 ASM_THUMB_REG_R4
  307. #define REG_LOCAL_2 ASM_THUMB_REG_R5
  308. #define REG_LOCAL_3 ASM_THUMB_REG_R6
  309. #define REG_LOCAL_NUM (3)
  310. #define REG_FUN_TABLE ASM_THUMB_REG_FUN_TABLE
  311. #define ASM_T asm_thumb_t
  312. #define ASM_END_PASS asm_thumb_end_pass
  313. #define ASM_ENTRY asm_thumb_entry
  314. #define ASM_EXIT asm_thumb_exit
  315. #define ASM_JUMP asm_thumb_b_label
  316. #define ASM_JUMP_IF_REG_ZERO(as, reg, label, bool_test) \
  317. do { \
  318. asm_thumb_cmp_rlo_i8(as, reg, 0); \
  319. asm_thumb_bcc_label(as, ASM_THUMB_CC_EQ, label); \
  320. } while (0)
  321. #define ASM_JUMP_IF_REG_NONZERO(as, reg, label, bool_test) \
  322. do { \
  323. asm_thumb_cmp_rlo_i8(as, reg, 0); \
  324. asm_thumb_bcc_label(as, ASM_THUMB_CC_NE, label); \
  325. } while (0)
  326. #define ASM_JUMP_IF_REG_EQ(as, reg1, reg2, label) \
  327. do { \
  328. asm_thumb_cmp_rlo_rlo(as, reg1, reg2); \
  329. asm_thumb_bcc_label(as, ASM_THUMB_CC_EQ, label); \
  330. } while (0)
  331. #define ASM_JUMP_REG(as, reg) asm_thumb_bx_reg((as), (reg))
  332. #define ASM_CALL_IND(as, idx) asm_thumb_bl_ind(as, idx, ASM_THUMB_REG_R3)
  333. #define ASM_MOV_LOCAL_REG(as, local_num, reg) asm_thumb_mov_local_reg((as), (local_num), (reg))
  334. #define ASM_MOV_REG_IMM(as, reg_dest, imm) asm_thumb_mov_reg_i32_optimised((as), (reg_dest), (imm))
  335. #define ASM_MOV_REG_LOCAL(as, reg_dest, local_num) asm_thumb_mov_reg_local((as), (reg_dest), (local_num))
  336. #define ASM_MOV_REG_REG(as, reg_dest, reg_src) asm_thumb_mov_reg_reg((as), (reg_dest), (reg_src))
  337. #define ASM_MOV_REG_LOCAL_ADDR(as, reg_dest, local_num) asm_thumb_mov_reg_local_addr((as), (reg_dest), (local_num))
  338. #define ASM_MOV_REG_PCREL(as, rlo_dest, label) asm_thumb_mov_reg_pcrel((as), (rlo_dest), (label))
  339. #define ASM_NOT_REG(as, reg_dest) asm_thumb_mvn_rlo_rlo((as), (reg_dest), (reg_dest))
  340. #define ASM_NEG_REG(as, reg_dest) asm_thumb_neg_rlo_rlo((as), (reg_dest), (reg_dest))
  341. #define ASM_LSL_REG_REG(as, reg_dest, reg_shift) asm_thumb_format_4((as), ASM_THUMB_FORMAT_4_LSL, (reg_dest), (reg_shift))
  342. #define ASM_LSR_REG_REG(as, reg_dest, reg_shift) asm_thumb_format_4((as), ASM_THUMB_FORMAT_4_LSR, (reg_dest), (reg_shift))
  343. #define ASM_ASR_REG_REG(as, reg_dest, reg_shift) asm_thumb_format_4((as), ASM_THUMB_FORMAT_4_ASR, (reg_dest), (reg_shift))
  344. #define ASM_OR_REG_REG(as, reg_dest, reg_src) asm_thumb_format_4((as), ASM_THUMB_FORMAT_4_ORR, (reg_dest), (reg_src))
  345. #define ASM_XOR_REG_REG(as, reg_dest, reg_src) asm_thumb_format_4((as), ASM_THUMB_FORMAT_4_EOR, (reg_dest), (reg_src))
  346. #define ASM_AND_REG_REG(as, reg_dest, reg_src) asm_thumb_format_4((as), ASM_THUMB_FORMAT_4_AND, (reg_dest), (reg_src))
  347. #define ASM_ADD_REG_REG(as, reg_dest, reg_src) asm_thumb_add_rlo_rlo_rlo((as), (reg_dest), (reg_dest), (reg_src))
  348. #define ASM_SUB_REG_REG(as, reg_dest, reg_src) asm_thumb_sub_rlo_rlo_rlo((as), (reg_dest), (reg_dest), (reg_src))
  349. #define ASM_MUL_REG_REG(as, reg_dest, reg_src) asm_thumb_format_4((as), ASM_THUMB_FORMAT_4_MUL, (reg_dest), (reg_src))
  350. #define ASM_LOAD_REG_REG(as, reg_dest, reg_base) asm_thumb_ldr_rlo_rlo_i5((as), (reg_dest), (reg_base), 0)
  351. #define ASM_LOAD_REG_REG_OFFSET(as, reg_dest, reg_base, word_offset) asm_thumb_ldr_reg_reg_i12_optimised((as), (reg_dest), (reg_base), (word_offset))
  352. #define ASM_LOAD8_REG_REG(as, reg_dest, reg_base) asm_thumb_ldrb_rlo_rlo_i5((as), (reg_dest), (reg_base), 0)
  353. #define ASM_LOAD16_REG_REG(as, reg_dest, reg_base) asm_thumb_ldrh_rlo_rlo_i5((as), (reg_dest), (reg_base), 0)
  354. #define ASM_LOAD16_REG_REG_OFFSET(as, reg_dest, reg_base, uint16_offset) asm_thumb_ldrh_reg_reg_i12_optimised((as), (reg_dest), (reg_base), (uint16_offset))
  355. #define ASM_LOAD32_REG_REG(as, reg_dest, reg_base) asm_thumb_ldr_rlo_rlo_i5((as), (reg_dest), (reg_base), 0)
  356. #define ASM_STORE_REG_REG(as, reg_src, reg_base) asm_thumb_str_rlo_rlo_i5((as), (reg_src), (reg_base), 0)
  357. #define ASM_STORE_REG_REG_OFFSET(as, reg_src, reg_base, word_offset) asm_thumb_str_rlo_rlo_i5((as), (reg_src), (reg_base), (word_offset))
  358. #define ASM_STORE8_REG_REG(as, reg_src, reg_base) asm_thumb_strb_rlo_rlo_i5((as), (reg_src), (reg_base), 0)
  359. #define ASM_STORE16_REG_REG(as, reg_src, reg_base) asm_thumb_strh_rlo_rlo_i5((as), (reg_src), (reg_base), 0)
  360. #define ASM_STORE32_REG_REG(as, reg_src, reg_base) asm_thumb_str_rlo_rlo_i5((as), (reg_src), (reg_base), 0)
  361. #endif // GENERIC_ASM_API
  362. #endif // MICROPY_INCLUDED_PY_ASMTHUMB_H