emitinlinethumb.c 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822
  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 <stdint.h>
  27. #include <stdio.h>
  28. #include <string.h>
  29. #include <stdarg.h>
  30. #include <assert.h>
  31. #include "py/emit.h"
  32. #include "py/asmthumb.h"
  33. #if MICROPY_EMIT_INLINE_THUMB
  34. typedef enum {
  35. // define rules with a compile function
  36. #define DEF_RULE(rule, comp, kind, ...) PN_##rule,
  37. #define DEF_RULE_NC(rule, kind, ...)
  38. #include "py/grammar.h"
  39. #undef DEF_RULE
  40. #undef DEF_RULE_NC
  41. PN_const_object, // special node for a constant, generic Python object
  42. // define rules without a compile function
  43. #define DEF_RULE(rule, comp, kind, ...)
  44. #define DEF_RULE_NC(rule, kind, ...) PN_##rule,
  45. #include "py/grammar.h"
  46. #undef DEF_RULE
  47. #undef DEF_RULE_NC
  48. } pn_kind_t;
  49. struct _emit_inline_asm_t {
  50. asm_thumb_t as;
  51. uint16_t pass;
  52. mp_obj_t *error_slot;
  53. mp_uint_t max_num_labels;
  54. qstr *label_lookup;
  55. };
  56. STATIC void emit_inline_thumb_error_msg(emit_inline_asm_t *emit, const char *msg) {
  57. *emit->error_slot = mp_obj_new_exception_msg(&mp_type_SyntaxError, msg);
  58. }
  59. STATIC void emit_inline_thumb_error_exc(emit_inline_asm_t *emit, mp_obj_t exc) {
  60. *emit->error_slot = exc;
  61. }
  62. emit_inline_asm_t *emit_inline_thumb_new(mp_uint_t max_num_labels) {
  63. emit_inline_asm_t *emit = m_new_obj(emit_inline_asm_t);
  64. memset(&emit->as, 0, sizeof(emit->as));
  65. mp_asm_base_init(&emit->as.base, max_num_labels);
  66. emit->max_num_labels = max_num_labels;
  67. emit->label_lookup = m_new(qstr, max_num_labels);
  68. return emit;
  69. }
  70. void emit_inline_thumb_free(emit_inline_asm_t *emit) {
  71. m_del(qstr, emit->label_lookup, emit->max_num_labels);
  72. mp_asm_base_deinit(&emit->as.base, false);
  73. m_del_obj(emit_inline_asm_t, emit);
  74. }
  75. STATIC void emit_inline_thumb_start_pass(emit_inline_asm_t *emit, pass_kind_t pass, mp_obj_t *error_slot) {
  76. emit->pass = pass;
  77. emit->error_slot = error_slot;
  78. if (emit->pass == MP_PASS_CODE_SIZE) {
  79. memset(emit->label_lookup, 0, emit->max_num_labels * sizeof(qstr));
  80. }
  81. mp_asm_base_start_pass(&emit->as.base, pass == MP_PASS_EMIT ? MP_ASM_PASS_EMIT : MP_ASM_PASS_COMPUTE);
  82. asm_thumb_entry(&emit->as, 0);
  83. }
  84. STATIC void emit_inline_thumb_end_pass(emit_inline_asm_t *emit, mp_uint_t type_sig) {
  85. asm_thumb_exit(&emit->as);
  86. asm_thumb_end_pass(&emit->as);
  87. }
  88. STATIC mp_uint_t emit_inline_thumb_count_params(emit_inline_asm_t *emit, mp_uint_t n_params, mp_parse_node_t *pn_params) {
  89. if (n_params > 4) {
  90. emit_inline_thumb_error_msg(emit, "can only have up to 4 parameters to Thumb assembly");
  91. return 0;
  92. }
  93. for (mp_uint_t i = 0; i < n_params; i++) {
  94. if (!MP_PARSE_NODE_IS_ID(pn_params[i])) {
  95. emit_inline_thumb_error_msg(emit, "parameters must be registers in sequence r0 to r3");
  96. return 0;
  97. }
  98. const char *p = qstr_str(MP_PARSE_NODE_LEAF_ARG(pn_params[i]));
  99. if (!(strlen(p) == 2 && p[0] == 'r' && p[1] == '0' + i)) {
  100. emit_inline_thumb_error_msg(emit, "parameters must be registers in sequence r0 to r3");
  101. return 0;
  102. }
  103. }
  104. return n_params;
  105. }
  106. STATIC bool emit_inline_thumb_label(emit_inline_asm_t *emit, mp_uint_t label_num, qstr label_id) {
  107. assert(label_num < emit->max_num_labels);
  108. if (emit->pass == MP_PASS_CODE_SIZE) {
  109. // check for duplicate label on first pass
  110. for (uint i = 0; i < emit->max_num_labels; i++) {
  111. if (emit->label_lookup[i] == label_id) {
  112. return false;
  113. }
  114. }
  115. }
  116. emit->label_lookup[label_num] = label_id;
  117. mp_asm_base_label_assign(&emit->as.base, label_num);
  118. return true;
  119. }
  120. typedef struct _reg_name_t { byte reg; byte name[3]; } reg_name_t;
  121. STATIC const reg_name_t reg_name_table[] = {
  122. {0, "r0\0"},
  123. {1, "r1\0"},
  124. {2, "r2\0"},
  125. {3, "r3\0"},
  126. {4, "r4\0"},
  127. {5, "r5\0"},
  128. {6, "r6\0"},
  129. {7, "r7\0"},
  130. {8, "r8\0"},
  131. {9, "r9\0"},
  132. {10, "r10"},
  133. {11, "r11"},
  134. {12, "r12"},
  135. {13, "r13"},
  136. {14, "r14"},
  137. {15, "r15"},
  138. {10, "sl\0"},
  139. {11, "fp\0"},
  140. {13, "sp\0"},
  141. {14, "lr\0"},
  142. {15, "pc\0"},
  143. };
  144. #define MAX_SPECIAL_REGISTER_NAME_LENGTH 7
  145. typedef struct _special_reg_name_t { byte reg; char name[MAX_SPECIAL_REGISTER_NAME_LENGTH + 1]; } special_reg_name_t;
  146. STATIC const special_reg_name_t special_reg_name_table[] = {
  147. {5, "IPSR"},
  148. {17, "BASEPRI"},
  149. };
  150. // return empty string in case of error, so we can attempt to parse the string
  151. // without a special check if it was in fact a string
  152. STATIC const char *get_arg_str(mp_parse_node_t pn) {
  153. if (MP_PARSE_NODE_IS_ID(pn)) {
  154. qstr qst = MP_PARSE_NODE_LEAF_ARG(pn);
  155. return qstr_str(qst);
  156. } else {
  157. return "";
  158. }
  159. }
  160. STATIC mp_uint_t get_arg_reg(emit_inline_asm_t *emit, const char *op, mp_parse_node_t pn, mp_uint_t max_reg) {
  161. const char *reg_str = get_arg_str(pn);
  162. for (mp_uint_t i = 0; i < MP_ARRAY_SIZE(reg_name_table); i++) {
  163. const reg_name_t *r = &reg_name_table[i];
  164. if (reg_str[0] == r->name[0]
  165. && reg_str[1] == r->name[1]
  166. && reg_str[2] == r->name[2]
  167. && (reg_str[2] == '\0' || reg_str[3] == '\0')) {
  168. if (r->reg > max_reg) {
  169. emit_inline_thumb_error_exc(emit,
  170. mp_obj_new_exception_msg_varg(&mp_type_SyntaxError,
  171. "'%s' expects at most r%d", op, max_reg));
  172. return 0;
  173. } else {
  174. return r->reg;
  175. }
  176. }
  177. }
  178. emit_inline_thumb_error_exc(emit,
  179. mp_obj_new_exception_msg_varg(&mp_type_SyntaxError,
  180. "'%s' expects a register", op));
  181. return 0;
  182. }
  183. STATIC mp_uint_t get_arg_special_reg(emit_inline_asm_t *emit, const char *op, mp_parse_node_t pn) {
  184. const char *reg_str = get_arg_str(pn);
  185. for (mp_uint_t i = 0; i < MP_ARRAY_SIZE(special_reg_name_table); i++) {
  186. const special_reg_name_t *r = &special_reg_name_table[i];
  187. if (strcmp(r->name, reg_str) == 0) {
  188. return r->reg;
  189. }
  190. }
  191. emit_inline_thumb_error_exc(emit,
  192. mp_obj_new_exception_msg_varg(&mp_type_SyntaxError,
  193. "'%s' expects a special register", op));
  194. return 0;
  195. }
  196. #if MICROPY_EMIT_INLINE_THUMB_FLOAT
  197. STATIC mp_uint_t get_arg_vfpreg(emit_inline_asm_t *emit, const char *op, mp_parse_node_t pn) {
  198. const char *reg_str = get_arg_str(pn);
  199. if (reg_str[0] == 's' && reg_str[1] != '\0') {
  200. mp_uint_t regno = 0;
  201. for (++reg_str; *reg_str; ++reg_str) {
  202. mp_uint_t v = *reg_str;
  203. if (!('0' <= v && v <= '9')) {
  204. goto malformed;
  205. }
  206. regno = 10 * regno + v - '0';
  207. }
  208. if (regno > 31) {
  209. emit_inline_thumb_error_exc(emit,
  210. mp_obj_new_exception_msg_varg(&mp_type_SyntaxError,
  211. "'%s' expects at most r%d", op, 31));
  212. return 0;
  213. } else {
  214. return regno;
  215. }
  216. }
  217. malformed:
  218. emit_inline_thumb_error_exc(emit,
  219. mp_obj_new_exception_msg_varg(&mp_type_SyntaxError,
  220. "'%s' expects an FPU register", op));
  221. return 0;
  222. }
  223. #endif
  224. STATIC mp_uint_t get_arg_reglist(emit_inline_asm_t *emit, const char *op, mp_parse_node_t pn) {
  225. // a register list looks like {r0, r1, r2} and is parsed as a Python set
  226. if (!MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_atom_brace)) {
  227. goto bad_arg;
  228. }
  229. mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
  230. assert(MP_PARSE_NODE_STRUCT_NUM_NODES(pns) == 1); // should always be
  231. pn = pns->nodes[0];
  232. mp_uint_t reglist = 0;
  233. if (MP_PARSE_NODE_IS_ID(pn)) {
  234. // set with one element
  235. reglist |= 1 << get_arg_reg(emit, op, pn, 15);
  236. } else if (MP_PARSE_NODE_IS_STRUCT(pn)) {
  237. pns = (mp_parse_node_struct_t*)pn;
  238. if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_dictorsetmaker) {
  239. assert(MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])); // should succeed
  240. mp_parse_node_struct_t *pns1 = (mp_parse_node_struct_t*)pns->nodes[1];
  241. if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_dictorsetmaker_list) {
  242. // set with multiple elements
  243. // get first element of set (we rely on get_arg_reg to catch syntax errors)
  244. reglist |= 1 << get_arg_reg(emit, op, pns->nodes[0], 15);
  245. // get tail elements (2nd, 3rd, ...)
  246. mp_parse_node_t *nodes;
  247. int n = mp_parse_node_extract_list(&pns1->nodes[0], PN_dictorsetmaker_list2, &nodes);
  248. // process rest of elements
  249. for (int i = 0; i < n; i++) {
  250. reglist |= 1 << get_arg_reg(emit, op, nodes[i], 15);
  251. }
  252. } else {
  253. goto bad_arg;
  254. }
  255. } else {
  256. goto bad_arg;
  257. }
  258. } else {
  259. goto bad_arg;
  260. }
  261. return reglist;
  262. bad_arg:
  263. emit_inline_thumb_error_exc(emit, mp_obj_new_exception_msg_varg(&mp_type_SyntaxError, "'%s' expects {r0, r1, ...}", op));
  264. return 0;
  265. }
  266. STATIC uint32_t get_arg_i(emit_inline_asm_t *emit, const char *op, mp_parse_node_t pn, uint32_t fit_mask) {
  267. mp_obj_t o;
  268. if (!mp_parse_node_get_int_maybe(pn, &o)) {
  269. emit_inline_thumb_error_exc(emit, mp_obj_new_exception_msg_varg(&mp_type_SyntaxError, "'%s' expects an integer", op));
  270. return 0;
  271. }
  272. uint32_t i = mp_obj_get_int_truncated(o);
  273. if ((i & (~fit_mask)) != 0) {
  274. emit_inline_thumb_error_exc(emit, mp_obj_new_exception_msg_varg(&mp_type_SyntaxError, "'%s' integer 0x%x does not fit in mask 0x%x", op, i, fit_mask));
  275. return 0;
  276. }
  277. return i;
  278. }
  279. STATIC bool get_arg_addr(emit_inline_asm_t *emit, const char *op, mp_parse_node_t pn, mp_parse_node_t *pn_base, mp_parse_node_t *pn_offset) {
  280. if (!MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_atom_bracket)) {
  281. goto bad_arg;
  282. }
  283. mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
  284. if (!MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_comp)) {
  285. goto bad_arg;
  286. }
  287. pns = (mp_parse_node_struct_t*)pns->nodes[0];
  288. if (MP_PARSE_NODE_STRUCT_NUM_NODES(pns) != 2) {
  289. goto bad_arg;
  290. }
  291. *pn_base = pns->nodes[0];
  292. *pn_offset = pns->nodes[1];
  293. return true;
  294. bad_arg:
  295. emit_inline_thumb_error_exc(emit, mp_obj_new_exception_msg_varg(&mp_type_SyntaxError, "'%s' expects an address of the form [a, b]", op));
  296. return false;
  297. }
  298. STATIC int get_arg_label(emit_inline_asm_t *emit, const char *op, mp_parse_node_t pn) {
  299. if (!MP_PARSE_NODE_IS_ID(pn)) {
  300. emit_inline_thumb_error_exc(emit, mp_obj_new_exception_msg_varg(&mp_type_SyntaxError, "'%s' expects a label", op));
  301. return 0;
  302. }
  303. qstr label_qstr = MP_PARSE_NODE_LEAF_ARG(pn);
  304. for (uint i = 0; i < emit->max_num_labels; i++) {
  305. if (emit->label_lookup[i] == label_qstr) {
  306. return i;
  307. }
  308. }
  309. // only need to have the labels on the last pass
  310. if (emit->pass == MP_PASS_EMIT) {
  311. emit_inline_thumb_error_exc(emit, mp_obj_new_exception_msg_varg(&mp_type_SyntaxError, "label '%q' not defined", label_qstr));
  312. }
  313. return 0;
  314. }
  315. typedef struct _cc_name_t { byte cc; byte name[2]; } cc_name_t;
  316. STATIC const cc_name_t cc_name_table[] = {
  317. { ASM_THUMB_CC_EQ, "eq" },
  318. { ASM_THUMB_CC_NE, "ne" },
  319. { ASM_THUMB_CC_CS, "cs" },
  320. { ASM_THUMB_CC_CC, "cc" },
  321. { ASM_THUMB_CC_MI, "mi" },
  322. { ASM_THUMB_CC_PL, "pl" },
  323. { ASM_THUMB_CC_VS, "vs" },
  324. { ASM_THUMB_CC_VC, "vc" },
  325. { ASM_THUMB_CC_HI, "hi" },
  326. { ASM_THUMB_CC_LS, "ls" },
  327. { ASM_THUMB_CC_GE, "ge" },
  328. { ASM_THUMB_CC_LT, "lt" },
  329. { ASM_THUMB_CC_GT, "gt" },
  330. { ASM_THUMB_CC_LE, "le" },
  331. };
  332. typedef struct _format_4_op_t { byte op; char name[3]; } format_4_op_t;
  333. #define X(x) (((x) >> 4) & 0xff) // only need 1 byte to distinguish these ops
  334. STATIC const format_4_op_t format_4_op_table[] = {
  335. { X(ASM_THUMB_FORMAT_4_EOR), "eor" },
  336. { X(ASM_THUMB_FORMAT_4_LSL), "lsl" },
  337. { X(ASM_THUMB_FORMAT_4_LSR), "lsr" },
  338. { X(ASM_THUMB_FORMAT_4_ASR), "asr" },
  339. { X(ASM_THUMB_FORMAT_4_ADC), "adc" },
  340. { X(ASM_THUMB_FORMAT_4_SBC), "sbc" },
  341. { X(ASM_THUMB_FORMAT_4_ROR), "ror" },
  342. { X(ASM_THUMB_FORMAT_4_TST), "tst" },
  343. { X(ASM_THUMB_FORMAT_4_NEG), "neg" },
  344. { X(ASM_THUMB_FORMAT_4_CMP), "cmp" },
  345. { X(ASM_THUMB_FORMAT_4_CMN), "cmn" },
  346. { X(ASM_THUMB_FORMAT_4_ORR), "orr" },
  347. { X(ASM_THUMB_FORMAT_4_MUL), "mul" },
  348. { X(ASM_THUMB_FORMAT_4_BIC), "bic" },
  349. { X(ASM_THUMB_FORMAT_4_MVN), "mvn" },
  350. };
  351. #undef X
  352. // name is actually a qstr, which should fit in 16 bits
  353. typedef struct _format_9_10_op_t { uint16_t op; uint16_t name; } format_9_10_op_t;
  354. #define X(x) (x)
  355. STATIC const format_9_10_op_t format_9_10_op_table[] = {
  356. { X(ASM_THUMB_FORMAT_9_LDR | ASM_THUMB_FORMAT_9_WORD_TRANSFER), MP_QSTR_ldr },
  357. { X(ASM_THUMB_FORMAT_9_LDR | ASM_THUMB_FORMAT_9_BYTE_TRANSFER), MP_QSTR_ldrb },
  358. { X(ASM_THUMB_FORMAT_10_LDRH), MP_QSTR_ldrh },
  359. { X(ASM_THUMB_FORMAT_9_STR | ASM_THUMB_FORMAT_9_WORD_TRANSFER), MP_QSTR_str },
  360. { X(ASM_THUMB_FORMAT_9_STR | ASM_THUMB_FORMAT_9_BYTE_TRANSFER), MP_QSTR_strb },
  361. { X(ASM_THUMB_FORMAT_10_STRH), MP_QSTR_strh },
  362. };
  363. #undef X
  364. #if MICROPY_EMIT_INLINE_THUMB_FLOAT
  365. // actual opcodes are: 0xee00 | op.hi_nibble, 0x0a00 | op.lo_nibble
  366. typedef struct _format_vfp_op_t { byte op; char name[3]; } format_vfp_op_t;
  367. STATIC const format_vfp_op_t format_vfp_op_table[] = {
  368. { 0x30, "add" },
  369. { 0x34, "sub" },
  370. { 0x20, "mul" },
  371. { 0x80, "div" },
  372. };
  373. #endif
  374. // shorthand alias for whether we allow ARMv7-M instructions
  375. #define ARMV7M MICROPY_EMIT_INLINE_THUMB_ARMV7M
  376. STATIC void emit_inline_thumb_op(emit_inline_asm_t *emit, qstr op, mp_uint_t n_args, mp_parse_node_t *pn_args) {
  377. // TODO perhaps make two tables:
  378. // one_args =
  379. // "b", LAB, asm_thumb_b_n,
  380. // "bgt", LAB, asm_thumb_bgt_n,
  381. // two_args =
  382. // "movs", RLO, I8, asm_thumb_movs_reg_i8
  383. // "movw", REG, REG, asm_thumb_movw_reg_i16
  384. // three_args =
  385. // "subs", RLO, RLO, I3, asm_thumb_subs_reg_reg_i3
  386. size_t op_len;
  387. const char *op_str = (const char*)qstr_data(op, &op_len);
  388. #if MICROPY_EMIT_INLINE_THUMB_FLOAT
  389. if (op_str[0] == 'v') {
  390. // floating point operations
  391. if (n_args == 2) {
  392. mp_uint_t op_code = 0x0ac0, op_code_hi;
  393. if (op == MP_QSTR_vcmp) {
  394. op_code_hi = 0xeeb4;
  395. op_vfp_twoargs:;
  396. mp_uint_t vd = get_arg_vfpreg(emit, op_str, pn_args[0]);
  397. mp_uint_t vm = get_arg_vfpreg(emit, op_str, pn_args[1]);
  398. asm_thumb_op32(&emit->as,
  399. op_code_hi | ((vd & 1) << 6),
  400. op_code | ((vd & 0x1e) << 11) | ((vm & 1) << 5) | (vm & 0x1e) >> 1);
  401. } else if (op == MP_QSTR_vsqrt) {
  402. op_code_hi = 0xeeb1;
  403. goto op_vfp_twoargs;
  404. } else if (op == MP_QSTR_vneg) {
  405. op_code_hi = 0xeeb1;
  406. op_code = 0x0a40;
  407. goto op_vfp_twoargs;
  408. } else if (op == MP_QSTR_vcvt_f32_s32) {
  409. op_code_hi = 0xeeb8; // int to float
  410. goto op_vfp_twoargs;
  411. } else if (op == MP_QSTR_vcvt_s32_f32) {
  412. op_code_hi = 0xeebd; // float to int
  413. goto op_vfp_twoargs;
  414. } else if (op == MP_QSTR_vmrs) {
  415. mp_uint_t reg_dest;
  416. const char *reg_str0 = get_arg_str(pn_args[0]);
  417. if (strcmp(reg_str0, "APSR_nzcv") == 0) {
  418. reg_dest = 15;
  419. } else {
  420. reg_dest = get_arg_reg(emit, op_str, pn_args[0], 15);
  421. }
  422. const char *reg_str1 = get_arg_str(pn_args[1]);
  423. if (strcmp(reg_str1, "FPSCR") == 0) {
  424. // FP status to ARM reg
  425. asm_thumb_op32(&emit->as, 0xeef1, 0x0a10 | (reg_dest << 12));
  426. } else {
  427. goto unknown_op;
  428. }
  429. } else if (op == MP_QSTR_vmov) {
  430. op_code_hi = 0xee00;
  431. mp_uint_t r_arm, vm;
  432. const char *reg_str = get_arg_str(pn_args[0]);
  433. if (reg_str[0] == 'r') {
  434. r_arm = get_arg_reg(emit, op_str, pn_args[0], 15);
  435. vm = get_arg_vfpreg(emit, op_str, pn_args[1]);
  436. op_code_hi |= 0x10;
  437. } else {
  438. vm = get_arg_vfpreg(emit, op_str, pn_args[0]);
  439. r_arm = get_arg_reg(emit, op_str, pn_args[1], 15);
  440. }
  441. asm_thumb_op32(&emit->as,
  442. op_code_hi | ((vm & 0x1e) >> 1),
  443. 0x0a10 | (r_arm << 12) | ((vm & 1) << 7));
  444. } else if (op == MP_QSTR_vldr) {
  445. op_code_hi = 0xed90;
  446. op_vldr_vstr:;
  447. mp_uint_t vd = get_arg_vfpreg(emit, op_str, pn_args[0]);
  448. mp_parse_node_t pn_base, pn_offset;
  449. if (get_arg_addr(emit, op_str, pn_args[1], &pn_base, &pn_offset)) {
  450. mp_uint_t rlo_base = get_arg_reg(emit, op_str, pn_base, 7);
  451. mp_uint_t i8;
  452. i8 = get_arg_i(emit, op_str, pn_offset, 0x3fc) >> 2;
  453. asm_thumb_op32(&emit->as,
  454. op_code_hi | rlo_base | ((vd & 1) << 6),
  455. 0x0a00 | ((vd & 0x1e) << 11) | i8);
  456. }
  457. } else if (op == MP_QSTR_vstr) {
  458. op_code_hi = 0xed80;
  459. goto op_vldr_vstr;
  460. } else {
  461. goto unknown_op;
  462. }
  463. } else if (n_args == 3) {
  464. // search table for arith ops
  465. for (mp_uint_t i = 0; i < MP_ARRAY_SIZE(format_vfp_op_table); i++) {
  466. if (strncmp(op_str + 1, format_vfp_op_table[i].name, 3) == 0 && op_str[4] == '\0') {
  467. mp_uint_t op_code_hi = 0xee00 | (format_vfp_op_table[i].op & 0xf0);
  468. mp_uint_t op_code = 0x0a00 | ((format_vfp_op_table[i].op & 0x0f) << 4);
  469. mp_uint_t vd = get_arg_vfpreg(emit, op_str, pn_args[0]);
  470. mp_uint_t vn = get_arg_vfpreg(emit, op_str, pn_args[1]);
  471. mp_uint_t vm = get_arg_vfpreg(emit, op_str, pn_args[2]);
  472. asm_thumb_op32(&emit->as,
  473. op_code_hi | ((vd & 1) << 6) | (vn >> 1),
  474. op_code | (vm >> 1) | ((vm & 1) << 5) | ((vd & 0x1e) << 11) | ((vn & 1) << 7));
  475. return;
  476. }
  477. }
  478. goto unknown_op;
  479. } else {
  480. goto unknown_op;
  481. }
  482. } else
  483. #endif
  484. if (n_args == 0) {
  485. if (op == MP_QSTR_nop) {
  486. asm_thumb_op16(&emit->as, ASM_THUMB_OP_NOP);
  487. } else if (op == MP_QSTR_wfi) {
  488. asm_thumb_op16(&emit->as, ASM_THUMB_OP_WFI);
  489. } else {
  490. goto unknown_op;
  491. }
  492. } else if (n_args == 1) {
  493. if (op == MP_QSTR_b) {
  494. int label_num = get_arg_label(emit, op_str, pn_args[0]);
  495. if (!asm_thumb_b_n_label(&emit->as, label_num)) {
  496. goto branch_not_in_range;
  497. }
  498. } else if (op == MP_QSTR_bl) {
  499. int label_num = get_arg_label(emit, op_str, pn_args[0]);
  500. if (!asm_thumb_bl_label(&emit->as, label_num)) {
  501. goto branch_not_in_range;
  502. }
  503. } else if (op == MP_QSTR_bx) {
  504. mp_uint_t r = get_arg_reg(emit, op_str, pn_args[0], 15);
  505. asm_thumb_op16(&emit->as, 0x4700 | (r << 3));
  506. } else if (op_str[0] == 'b' && (op_len == 3
  507. || (op_len == 5 && op_str[3] == '_'
  508. && (op_str[4] == 'n' || (ARMV7M && op_str[4] == 'w'))))) {
  509. mp_uint_t cc = -1;
  510. for (mp_uint_t i = 0; i < MP_ARRAY_SIZE(cc_name_table); i++) {
  511. if (op_str[1] == cc_name_table[i].name[0] && op_str[2] == cc_name_table[i].name[1]) {
  512. cc = cc_name_table[i].cc;
  513. }
  514. }
  515. if (cc == (mp_uint_t)-1) {
  516. goto unknown_op;
  517. }
  518. int label_num = get_arg_label(emit, op_str, pn_args[0]);
  519. if (!asm_thumb_bcc_nw_label(&emit->as, cc, label_num, op_len == 5 && op_str[4] == 'w')) {
  520. goto branch_not_in_range;
  521. }
  522. } else if (ARMV7M && op_str[0] == 'i' && op_str[1] == 't') {
  523. const char *arg_str = get_arg_str(pn_args[0]);
  524. mp_uint_t cc = -1;
  525. for (mp_uint_t i = 0; i < MP_ARRAY_SIZE(cc_name_table); i++) {
  526. if (arg_str[0] == cc_name_table[i].name[0]
  527. && arg_str[1] == cc_name_table[i].name[1]
  528. && arg_str[2] == '\0') {
  529. cc = cc_name_table[i].cc;
  530. break;
  531. }
  532. }
  533. if (cc == (mp_uint_t)-1) {
  534. goto unknown_op;
  535. }
  536. const char *os = op_str + 2;
  537. while (*os != '\0') {
  538. os++;
  539. }
  540. if (os > op_str + 5) {
  541. goto unknown_op;
  542. }
  543. mp_uint_t it_mask = 8;
  544. while (--os >= op_str + 2) {
  545. it_mask >>= 1;
  546. if (*os == 't') {
  547. it_mask |= (cc & 1) << 3;
  548. } else if (*os == 'e') {
  549. it_mask |= ((~cc) & 1) << 3;
  550. } else {
  551. goto unknown_op;
  552. }
  553. }
  554. asm_thumb_it_cc(&emit->as, cc, it_mask);
  555. } else if (op == MP_QSTR_cpsid) {
  556. // TODO check pn_args[0] == i
  557. asm_thumb_op16(&emit->as, ASM_THUMB_OP_CPSID_I);
  558. } else if (op == MP_QSTR_cpsie) {
  559. // TODO check pn_args[0] == i
  560. asm_thumb_op16(&emit->as, ASM_THUMB_OP_CPSIE_I);
  561. } else if (op == MP_QSTR_push) {
  562. mp_uint_t reglist = get_arg_reglist(emit, op_str, pn_args[0]);
  563. if ((reglist & 0xff00) == 0) {
  564. asm_thumb_op16(&emit->as, 0xb400 | reglist);
  565. } else {
  566. if (!ARMV7M) {
  567. goto unknown_op;
  568. }
  569. asm_thumb_op32(&emit->as, 0xe92d, reglist);
  570. }
  571. } else if (op == MP_QSTR_pop) {
  572. mp_uint_t reglist = get_arg_reglist(emit, op_str, pn_args[0]);
  573. if ((reglist & 0xff00) == 0) {
  574. asm_thumb_op16(&emit->as, 0xbc00 | reglist);
  575. } else {
  576. if (!ARMV7M) {
  577. goto unknown_op;
  578. }
  579. asm_thumb_op32(&emit->as, 0xe8bd, reglist);
  580. }
  581. } else {
  582. goto unknown_op;
  583. }
  584. } else if (n_args == 2) {
  585. if (MP_PARSE_NODE_IS_ID(pn_args[1])) {
  586. // second arg is a register (or should be)
  587. mp_uint_t op_code, op_code_hi;
  588. if (op == MP_QSTR_mov) {
  589. mp_uint_t reg_dest = get_arg_reg(emit, op_str, pn_args[0], 15);
  590. mp_uint_t reg_src = get_arg_reg(emit, op_str, pn_args[1], 15);
  591. asm_thumb_mov_reg_reg(&emit->as, reg_dest, reg_src);
  592. } else if (ARMV7M && op == MP_QSTR_clz) {
  593. op_code_hi = 0xfab0;
  594. op_code = 0xf080;
  595. mp_uint_t rd, rm;
  596. op_clz_rbit:
  597. rd = get_arg_reg(emit, op_str, pn_args[0], 15);
  598. rm = get_arg_reg(emit, op_str, pn_args[1], 15);
  599. asm_thumb_op32(&emit->as, op_code_hi | rm, op_code | (rd << 8) | rm);
  600. } else if (ARMV7M && op == MP_QSTR_rbit) {
  601. op_code_hi = 0xfa90;
  602. op_code = 0xf0a0;
  603. goto op_clz_rbit;
  604. } else if (ARMV7M && op == MP_QSTR_mrs){
  605. mp_uint_t reg_dest = get_arg_reg(emit, op_str, pn_args[0], 12);
  606. mp_uint_t reg_src = get_arg_special_reg(emit, op_str, pn_args[1]);
  607. asm_thumb_op32(&emit->as, 0xf3ef, 0x8000 | (reg_dest << 8) | reg_src);
  608. } else {
  609. if (op == MP_QSTR_and_) {
  610. op_code = ASM_THUMB_FORMAT_4_AND;
  611. mp_uint_t reg_dest, reg_src;
  612. op_format_4:
  613. reg_dest = get_arg_reg(emit, op_str, pn_args[0], 7);
  614. reg_src = get_arg_reg(emit, op_str, pn_args[1], 7);
  615. asm_thumb_format_4(&emit->as, op_code, reg_dest, reg_src);
  616. return;
  617. }
  618. // search table for ALU ops
  619. for (mp_uint_t i = 0; i < MP_ARRAY_SIZE(format_4_op_table); i++) {
  620. if (strncmp(op_str, format_4_op_table[i].name, 3) == 0 && op_str[3] == '\0') {
  621. op_code = 0x4000 | (format_4_op_table[i].op << 4);
  622. goto op_format_4;
  623. }
  624. }
  625. goto unknown_op;
  626. }
  627. } else {
  628. // second arg is not a register
  629. mp_uint_t op_code;
  630. if (op == MP_QSTR_mov) {
  631. op_code = ASM_THUMB_FORMAT_3_MOV;
  632. mp_uint_t rlo_dest, i8_src;
  633. op_format_3:
  634. rlo_dest = get_arg_reg(emit, op_str, pn_args[0], 7);
  635. i8_src = get_arg_i(emit, op_str, pn_args[1], 0xff);
  636. asm_thumb_format_3(&emit->as, op_code, rlo_dest, i8_src);
  637. } else if (op == MP_QSTR_cmp) {
  638. op_code = ASM_THUMB_FORMAT_3_CMP;
  639. goto op_format_3;
  640. } else if (op == MP_QSTR_add) {
  641. op_code = ASM_THUMB_FORMAT_3_ADD;
  642. goto op_format_3;
  643. } else if (op == MP_QSTR_sub) {
  644. op_code = ASM_THUMB_FORMAT_3_SUB;
  645. goto op_format_3;
  646. } else if (ARMV7M && op == MP_QSTR_movw) {
  647. op_code = ASM_THUMB_OP_MOVW;
  648. mp_uint_t reg_dest;
  649. op_movw_movt:
  650. reg_dest = get_arg_reg(emit, op_str, pn_args[0], 15);
  651. int i_src = get_arg_i(emit, op_str, pn_args[1], 0xffff);
  652. asm_thumb_mov_reg_i16(&emit->as, op_code, reg_dest, i_src);
  653. } else if (ARMV7M && op == MP_QSTR_movt) {
  654. op_code = ASM_THUMB_OP_MOVT;
  655. goto op_movw_movt;
  656. } else if (ARMV7M && op == MP_QSTR_movwt) {
  657. // this is a convenience instruction
  658. mp_uint_t reg_dest = get_arg_reg(emit, op_str, pn_args[0], 15);
  659. uint32_t i_src = get_arg_i(emit, op_str, pn_args[1], 0xffffffff);
  660. asm_thumb_mov_reg_i16(&emit->as, ASM_THUMB_OP_MOVW, reg_dest, i_src & 0xffff);
  661. asm_thumb_mov_reg_i16(&emit->as, ASM_THUMB_OP_MOVT, reg_dest, (i_src >> 16) & 0xffff);
  662. } else if (ARMV7M && op == MP_QSTR_ldrex) {
  663. mp_uint_t r_dest = get_arg_reg(emit, op_str, pn_args[0], 15);
  664. mp_parse_node_t pn_base, pn_offset;
  665. if (get_arg_addr(emit, op_str, pn_args[1], &pn_base, &pn_offset)) {
  666. mp_uint_t r_base = get_arg_reg(emit, op_str, pn_base, 15);
  667. mp_uint_t i8 = get_arg_i(emit, op_str, pn_offset, 0xff) >> 2;
  668. asm_thumb_op32(&emit->as, 0xe850 | r_base, 0x0f00 | (r_dest << 12) | i8);
  669. }
  670. } else {
  671. // search table for ldr/str instructions
  672. for (mp_uint_t i = 0; i < MP_ARRAY_SIZE(format_9_10_op_table); i++) {
  673. if (op == format_9_10_op_table[i].name) {
  674. op_code = format_9_10_op_table[i].op;
  675. mp_parse_node_t pn_base, pn_offset;
  676. mp_uint_t rlo_dest = get_arg_reg(emit, op_str, pn_args[0], 7);
  677. if (get_arg_addr(emit, op_str, pn_args[1], &pn_base, &pn_offset)) {
  678. mp_uint_t rlo_base = get_arg_reg(emit, op_str, pn_base, 7);
  679. mp_uint_t i5;
  680. if (op_code & ASM_THUMB_FORMAT_9_BYTE_TRANSFER) {
  681. i5 = get_arg_i(emit, op_str, pn_offset, 0x1f);
  682. } else if (op_code & ASM_THUMB_FORMAT_10_STRH) { // also catches LDRH
  683. i5 = get_arg_i(emit, op_str, pn_offset, 0x3e) >> 1;
  684. } else {
  685. i5 = get_arg_i(emit, op_str, pn_offset, 0x7c) >> 2;
  686. }
  687. asm_thumb_format_9_10(&emit->as, op_code, rlo_dest, rlo_base, i5);
  688. return;
  689. }
  690. break;
  691. }
  692. }
  693. goto unknown_op;
  694. }
  695. }
  696. } else if (n_args == 3) {
  697. mp_uint_t op_code;
  698. if (op == MP_QSTR_lsl) {
  699. op_code = ASM_THUMB_FORMAT_1_LSL;
  700. mp_uint_t rlo_dest, rlo_src, i5;
  701. op_format_1:
  702. rlo_dest = get_arg_reg(emit, op_str, pn_args[0], 7);
  703. rlo_src = get_arg_reg(emit, op_str, pn_args[1], 7);
  704. i5 = get_arg_i(emit, op_str, pn_args[2], 0x1f);
  705. asm_thumb_format_1(&emit->as, op_code, rlo_dest, rlo_src, i5);
  706. } else if (op == MP_QSTR_lsr) {
  707. op_code = ASM_THUMB_FORMAT_1_LSR;
  708. goto op_format_1;
  709. } else if (op == MP_QSTR_asr) {
  710. op_code = ASM_THUMB_FORMAT_1_ASR;
  711. goto op_format_1;
  712. } else if (op == MP_QSTR_add) {
  713. op_code = ASM_THUMB_FORMAT_2_ADD;
  714. mp_uint_t rlo_dest, rlo_src;
  715. op_format_2:
  716. rlo_dest = get_arg_reg(emit, op_str, pn_args[0], 7);
  717. rlo_src = get_arg_reg(emit, op_str, pn_args[1], 7);
  718. int src_b;
  719. if (MP_PARSE_NODE_IS_ID(pn_args[2])) {
  720. op_code |= ASM_THUMB_FORMAT_2_REG_OPERAND;
  721. src_b = get_arg_reg(emit, op_str, pn_args[2], 7);
  722. } else {
  723. op_code |= ASM_THUMB_FORMAT_2_IMM_OPERAND;
  724. src_b = get_arg_i(emit, op_str, pn_args[2], 0x7);
  725. }
  726. asm_thumb_format_2(&emit->as, op_code, rlo_dest, rlo_src, src_b);
  727. } else if (ARMV7M && op == MP_QSTR_sdiv) {
  728. op_code = 0xfb90; // sdiv high part
  729. mp_uint_t rd, rn, rm;
  730. op_sdiv_udiv:
  731. rd = get_arg_reg(emit, op_str, pn_args[0], 15);
  732. rn = get_arg_reg(emit, op_str, pn_args[1], 15);
  733. rm = get_arg_reg(emit, op_str, pn_args[2], 15);
  734. asm_thumb_op32(&emit->as, op_code | rn, 0xf0f0 | (rd << 8) | rm);
  735. } else if (ARMV7M && op == MP_QSTR_udiv) {
  736. op_code = 0xfbb0; // udiv high part
  737. goto op_sdiv_udiv;
  738. } else if (op == MP_QSTR_sub) {
  739. op_code = ASM_THUMB_FORMAT_2_SUB;
  740. goto op_format_2;
  741. } else if (ARMV7M && op == MP_QSTR_strex) {
  742. mp_uint_t r_dest = get_arg_reg(emit, op_str, pn_args[0], 15);
  743. mp_uint_t r_src = get_arg_reg(emit, op_str, pn_args[1], 15);
  744. mp_parse_node_t pn_base, pn_offset;
  745. if (get_arg_addr(emit, op_str, pn_args[2], &pn_base, &pn_offset)) {
  746. mp_uint_t r_base = get_arg_reg(emit, op_str, pn_base, 15);
  747. mp_uint_t i8 = get_arg_i(emit, op_str, pn_offset, 0xff) >> 2;
  748. asm_thumb_op32(&emit->as, 0xe840 | r_base, (r_src << 12) | (r_dest << 8) | i8);
  749. }
  750. } else {
  751. goto unknown_op;
  752. }
  753. } else {
  754. goto unknown_op;
  755. }
  756. return;
  757. unknown_op:
  758. emit_inline_thumb_error_exc(emit, mp_obj_new_exception_msg_varg(&mp_type_SyntaxError, "unsupported Thumb instruction '%s' with %d arguments", op_str, n_args));
  759. return;
  760. branch_not_in_range:
  761. emit_inline_thumb_error_msg(emit, "branch not in range");
  762. return;
  763. }
  764. const emit_inline_asm_method_table_t emit_inline_thumb_method_table = {
  765. emit_inline_thumb_start_pass,
  766. emit_inline_thumb_end_pass,
  767. emit_inline_thumb_count_params,
  768. emit_inline_thumb_label,
  769. emit_inline_thumb_op,
  770. };
  771. #endif // MICROPY_EMIT_INLINE_THUMB