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