Damien George | 04b9147 | 2014-05-03 23:27:38 +0100 | [diff] [blame] | 1 | /* |
| 2 | * This file is part of the Micro Python 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 | |
Damien | 826005c | 2013-10-05 23:17:28 +0100 | [diff] [blame] | 27 | #include <stdint.h> |
| 28 | #include <stdio.h> |
| 29 | #include <string.h> |
Damien George | a26dc50 | 2014-04-12 17:54:52 +0100 | [diff] [blame] | 30 | #include <stdarg.h> |
Damien | 826005c | 2013-10-05 23:17:28 +0100 | [diff] [blame] | 31 | #include <assert.h> |
| 32 | |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 33 | #include "mpconfig.h" |
Paul Sokolovsky | 59c675a | 2014-06-21 22:43:22 +0300 | [diff] [blame] | 34 | #include "misc.h" |
Damien George | 55baff4 | 2014-01-21 21:40:13 +0000 | [diff] [blame] | 35 | #include "qstr.h" |
Damien | 826005c | 2013-10-05 23:17:28 +0100 | [diff] [blame] | 36 | #include "lexer.h" |
Damien | 826005c | 2013-10-05 23:17:28 +0100 | [diff] [blame] | 37 | #include "parse.h" |
Damien George | df8127a | 2014-04-13 11:04:33 +0100 | [diff] [blame] | 38 | #include "obj.h" |
| 39 | #include "emitglue.h" |
Damien | 826005c | 2013-10-05 23:17:28 +0100 | [diff] [blame] | 40 | #include "scope.h" |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 41 | #include "runtime0.h" |
Damien | 826005c | 2013-10-05 23:17:28 +0100 | [diff] [blame] | 42 | #include "emit.h" |
| 43 | #include "asmthumb.h" |
| 44 | |
Damien | 3ef4abb | 2013-10-12 16:53:13 +0100 | [diff] [blame] | 45 | #if MICROPY_EMIT_INLINE_THUMB |
Damien | 826005c | 2013-10-05 23:17:28 +0100 | [diff] [blame] | 46 | |
Damien George | 8721087 | 2014-04-13 00:30:32 +0100 | [diff] [blame] | 47 | typedef enum { |
| 48 | PN_none = 0, |
| 49 | #define DEF_RULE(rule, comp, kind, ...) PN_##rule, |
| 50 | #include "grammar.h" |
| 51 | #undef DEF_RULE |
| 52 | PN_maximum_number_of, |
| 53 | } pn_kind_t; |
| 54 | |
Damien | 826005c | 2013-10-05 23:17:28 +0100 | [diff] [blame] | 55 | struct _emit_inline_asm_t { |
Damien George | a26dc50 | 2014-04-12 17:54:52 +0100 | [diff] [blame] | 56 | uint16_t pass; |
| 57 | uint16_t success; |
Damien | 826005c | 2013-10-05 23:17:28 +0100 | [diff] [blame] | 58 | scope_t *scope; |
Damien George | 7ff996c | 2014-09-08 23:05:16 +0100 | [diff] [blame] | 59 | mp_uint_t max_num_labels; |
Damien | 826005c | 2013-10-05 23:17:28 +0100 | [diff] [blame] | 60 | qstr *label_lookup; |
| 61 | asm_thumb_t *as; |
| 62 | }; |
| 63 | |
Damien George | a26dc50 | 2014-04-12 17:54:52 +0100 | [diff] [blame] | 64 | void emit_inline_thumb_error(emit_inline_asm_t *emit, const char *fmt, ...) { |
| 65 | printf("SyntaxError: "); |
| 66 | emit->success = false; |
| 67 | va_list ap; |
| 68 | va_start(ap, fmt); |
| 69 | vprintf(fmt, ap); |
| 70 | va_end(ap); |
| 71 | } |
| 72 | |
Damien George | 7ff996c | 2014-09-08 23:05:16 +0100 | [diff] [blame] | 73 | emit_inline_asm_t *emit_inline_thumb_new(mp_uint_t max_num_labels) { |
Damien George | 41d02b6 | 2014-01-24 22:42:28 +0000 | [diff] [blame] | 74 | emit_inline_asm_t *emit = m_new_obj(emit_inline_asm_t); |
Damien | 826005c | 2013-10-05 23:17:28 +0100 | [diff] [blame] | 75 | emit->max_num_labels = max_num_labels; |
| 76 | emit->label_lookup = m_new(qstr, max_num_labels); |
| 77 | memset(emit->label_lookup, 0, emit->max_num_labels * sizeof(qstr)); |
| 78 | emit->as = asm_thumb_new(max_num_labels); |
| 79 | return emit; |
| 80 | } |
| 81 | |
Damien George | 41d02b6 | 2014-01-24 22:42:28 +0000 | [diff] [blame] | 82 | void emit_inline_thumb_free(emit_inline_asm_t *emit) { |
| 83 | m_del(qstr, emit->label_lookup, emit->max_num_labels); |
| 84 | asm_thumb_free(emit->as, false); |
| 85 | m_del_obj(emit_inline_asm_t, emit); |
| 86 | } |
| 87 | |
Paul Sokolovsky | 520e2f5 | 2014-02-12 18:31:30 +0200 | [diff] [blame] | 88 | STATIC void emit_inline_thumb_start_pass(emit_inline_asm_t *emit, pass_kind_t pass, scope_t *scope) { |
Damien | 826005c | 2013-10-05 23:17:28 +0100 | [diff] [blame] | 89 | emit->pass = pass; |
Damien George | a26dc50 | 2014-04-12 17:54:52 +0100 | [diff] [blame] | 90 | emit->success = true; |
Damien | 826005c | 2013-10-05 23:17:28 +0100 | [diff] [blame] | 91 | emit->scope = scope; |
Damien George | 36db6bc | 2014-05-07 17:24:22 +0100 | [diff] [blame] | 92 | asm_thumb_start_pass(emit->as, pass == MP_PASS_EMIT ? ASM_THUMB_PASS_EMIT : ASM_THUMB_PASS_COMPUTE); |
Damien | 826005c | 2013-10-05 23:17:28 +0100 | [diff] [blame] | 93 | asm_thumb_entry(emit->as, 0); |
| 94 | } |
| 95 | |
Damien George | a26dc50 | 2014-04-12 17:54:52 +0100 | [diff] [blame] | 96 | STATIC bool emit_inline_thumb_end_pass(emit_inline_asm_t *emit) { |
Damien | 826005c | 2013-10-05 23:17:28 +0100 | [diff] [blame] | 97 | asm_thumb_exit(emit->as); |
| 98 | asm_thumb_end_pass(emit->as); |
| 99 | |
Damien George | 36db6bc | 2014-05-07 17:24:22 +0100 | [diff] [blame] | 100 | if (emit->pass == MP_PASS_EMIT) { |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 101 | void *f = asm_thumb_get_code(emit->as); |
Damien George | 2ac4af6 | 2014-08-15 16:45:41 +0100 | [diff] [blame] | 102 | mp_emit_glue_assign_native(emit->scope->raw_code, MP_CODE_NATIVE_ASM, f, asm_thumb_get_code_size(emit->as), emit->scope->num_pos_args, 0); |
Damien | 826005c | 2013-10-05 23:17:28 +0100 | [diff] [blame] | 103 | } |
Damien George | a26dc50 | 2014-04-12 17:54:52 +0100 | [diff] [blame] | 104 | |
| 105 | return emit->success; |
Damien | 826005c | 2013-10-05 23:17:28 +0100 | [diff] [blame] | 106 | } |
| 107 | |
Damien George | 7ff996c | 2014-09-08 23:05:16 +0100 | [diff] [blame] | 108 | 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) { |
Damien | a2f2f7d | 2013-10-06 00:14:13 +0100 | [diff] [blame] | 109 | if (n_params > 4) { |
Damien George | a26dc50 | 2014-04-12 17:54:52 +0100 | [diff] [blame] | 110 | emit_inline_thumb_error(emit, "can only have up to 4 parameters to inline thumb assembly\n"); |
Damien | a2f2f7d | 2013-10-06 00:14:13 +0100 | [diff] [blame] | 111 | return 0; |
| 112 | } |
Damien George | 7ff996c | 2014-09-08 23:05:16 +0100 | [diff] [blame] | 113 | for (mp_uint_t i = 0; i < n_params; i++) { |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 114 | if (!MP_PARSE_NODE_IS_ID(pn_params[i])) { |
Damien George | a26dc50 | 2014-04-12 17:54:52 +0100 | [diff] [blame] | 115 | emit_inline_thumb_error(emit, "parameter to inline assembler must be an identifier\n"); |
Damien | a2f2f7d | 2013-10-06 00:14:13 +0100 | [diff] [blame] | 116 | return 0; |
| 117 | } |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 118 | const char *p = qstr_str(MP_PARSE_NODE_LEAF_ARG(pn_params[i])); |
Damien | a2f2f7d | 2013-10-06 00:14:13 +0100 | [diff] [blame] | 119 | if (!(strlen(p) == 2 && p[0] == 'r' && p[1] == '0' + i)) { |
Damien George | a26dc50 | 2014-04-12 17:54:52 +0100 | [diff] [blame] | 120 | emit_inline_thumb_error(emit, "parameter %d to inline assembler must be r%d\n", i + 1, i); |
Damien | a2f2f7d | 2013-10-06 00:14:13 +0100 | [diff] [blame] | 121 | return 0; |
| 122 | } |
| 123 | } |
| 124 | return n_params; |
| 125 | } |
| 126 | |
Damien George | 7ff996c | 2014-09-08 23:05:16 +0100 | [diff] [blame] | 127 | STATIC void emit_inline_thumb_label(emit_inline_asm_t *emit, mp_uint_t label_num, qstr label_id) { |
Damien | 826005c | 2013-10-05 23:17:28 +0100 | [diff] [blame] | 128 | assert(label_num < emit->max_num_labels); |
| 129 | emit->label_lookup[label_num] = label_id; |
| 130 | asm_thumb_label_assign(emit->as, label_num); |
| 131 | } |
| 132 | |
Damien George | 7ff996c | 2014-09-08 23:05:16 +0100 | [diff] [blame] | 133 | STATIC void emit_inline_thumb_align(emit_inline_asm_t *emit, mp_uint_t align) { |
Damien George | e5f8a77 | 2014-04-21 13:33:15 +0100 | [diff] [blame] | 134 | asm_thumb_align(emit->as, align); |
| 135 | } |
| 136 | |
Damien George | 7ff996c | 2014-09-08 23:05:16 +0100 | [diff] [blame] | 137 | STATIC void emit_inline_thumb_data(emit_inline_asm_t *emit, mp_uint_t bytesize, mp_uint_t val) { |
Damien George | e5f8a77 | 2014-04-21 13:33:15 +0100 | [diff] [blame] | 138 | asm_thumb_data(emit->as, bytesize, val); |
| 139 | } |
| 140 | |
Damien George | a26dc50 | 2014-04-12 17:54:52 +0100 | [diff] [blame] | 141 | typedef struct _reg_name_t { byte reg; byte name[3]; } reg_name_t; |
| 142 | STATIC const reg_name_t reg_name_table[] = { |
| 143 | {0, "r0\0"}, |
| 144 | {1, "r1\0"}, |
| 145 | {2, "r2\0"}, |
| 146 | {3, "r3\0"}, |
| 147 | {4, "r4\0"}, |
| 148 | {5, "r5\0"}, |
| 149 | {6, "r6\0"}, |
| 150 | {7, "r7\0"}, |
| 151 | {8, "r8\0"}, |
| 152 | {9, "r9\0"}, |
| 153 | {10, "r10"}, |
| 154 | {11, "r11"}, |
| 155 | {12, "r12"}, |
| 156 | {13, "r13"}, |
| 157 | {14, "r14"}, |
| 158 | {15, "r15"}, |
| 159 | {10, "sl\0"}, |
| 160 | {11, "fp\0"}, |
| 161 | {13, "sp\0"}, |
| 162 | {14, "lr\0"}, |
| 163 | {15, "pc\0"}, |
| 164 | }; |
| 165 | |
Damien George | 7ff996c | 2014-09-08 23:05:16 +0100 | [diff] [blame] | 166 | 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) { |
Damien George | 8721087 | 2014-04-13 00:30:32 +0100 | [diff] [blame] | 167 | if (MP_PARSE_NODE_IS_ID(pn)) { |
| 168 | qstr reg_qstr = MP_PARSE_NODE_LEAF_ARG(pn); |
Damien George | a26dc50 | 2014-04-12 17:54:52 +0100 | [diff] [blame] | 169 | const char *reg_str = qstr_str(reg_qstr); |
Damien George | 7ff996c | 2014-09-08 23:05:16 +0100 | [diff] [blame] | 170 | for (mp_uint_t i = 0; i < MP_ARRAY_SIZE(reg_name_table); i++) { |
Damien George | a26dc50 | 2014-04-12 17:54:52 +0100 | [diff] [blame] | 171 | const reg_name_t *r = ®_name_table[i]; |
| 172 | if (reg_str[0] == r->name[0] && reg_str[1] == r->name[1] && reg_str[2] == r->name[2] && (reg_str[2] == '\0' || reg_str[3] == '\0')) { |
| 173 | if (r->reg > max_reg) { |
Damien George | 8721087 | 2014-04-13 00:30:32 +0100 | [diff] [blame] | 174 | emit_inline_thumb_error(emit, "'%s' expects at most r%d\n", op, max_reg); |
Damien George | a26dc50 | 2014-04-12 17:54:52 +0100 | [diff] [blame] | 175 | return 0; |
| 176 | } else { |
| 177 | return r->reg; |
| 178 | } |
| 179 | } |
| 180 | } |
Damien | 826005c | 2013-10-05 23:17:28 +0100 | [diff] [blame] | 181 | } |
Damien George | 8721087 | 2014-04-13 00:30:32 +0100 | [diff] [blame] | 182 | emit_inline_thumb_error(emit, "'%s' expects a register\n", op); |
Damien George | a26dc50 | 2014-04-12 17:54:52 +0100 | [diff] [blame] | 183 | return 0; |
Damien | 826005c | 2013-10-05 23:17:28 +0100 | [diff] [blame] | 184 | } |
| 185 | |
Damien George | 8721087 | 2014-04-13 00:30:32 +0100 | [diff] [blame] | 186 | STATIC int get_arg_i(emit_inline_asm_t *emit, const char *op, mp_parse_node_t pn, int fit_mask) { |
| 187 | if (!MP_PARSE_NODE_IS_SMALL_INT(pn)) { |
| 188 | emit_inline_thumb_error(emit, "'%s' expects an integer\n", op); |
Damien | 826005c | 2013-10-05 23:17:28 +0100 | [diff] [blame] | 189 | return 0; |
| 190 | } |
Damien George | 8721087 | 2014-04-13 00:30:32 +0100 | [diff] [blame] | 191 | int i = MP_PARSE_NODE_LEAF_SMALL_INT(pn); |
Damien | 826005c | 2013-10-05 23:17:28 +0100 | [diff] [blame] | 192 | if ((i & (~fit_mask)) != 0) { |
Damien George | a26dc50 | 2014-04-12 17:54:52 +0100 | [diff] [blame] | 193 | emit_inline_thumb_error(emit, "'%s' integer 0x%x does not fit in mask 0x%x\n", op, i, fit_mask); |
Damien | 826005c | 2013-10-05 23:17:28 +0100 | [diff] [blame] | 194 | return 0; |
| 195 | } |
| 196 | return i; |
| 197 | } |
| 198 | |
Damien George | 8721087 | 2014-04-13 00:30:32 +0100 | [diff] [blame] | 199 | 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) { |
| 200 | if (!MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_atom_bracket)) { |
| 201 | goto bad_arg; |
| 202 | } |
| 203 | mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn; |
| 204 | if (!MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_comp)) { |
| 205 | goto bad_arg; |
| 206 | } |
| 207 | pns = (mp_parse_node_struct_t*)pns->nodes[0]; |
| 208 | if (MP_PARSE_NODE_STRUCT_NUM_NODES(pns) != 2) { |
| 209 | goto bad_arg; |
| 210 | } |
| 211 | |
| 212 | *pn_base = pns->nodes[0]; |
| 213 | *pn_offset = pns->nodes[1]; |
| 214 | return true; |
| 215 | |
| 216 | bad_arg: |
| 217 | emit_inline_thumb_error(emit, "'%s' expects an address of the form [a, b]\n", op); |
| 218 | return false; |
| 219 | } |
| 220 | |
| 221 | STATIC int get_arg_label(emit_inline_asm_t *emit, const char *op, mp_parse_node_t pn) { |
| 222 | if (!MP_PARSE_NODE_IS_ID(pn)) { |
| 223 | emit_inline_thumb_error(emit, "'%s' expects a label\n", op); |
Damien | 826005c | 2013-10-05 23:17:28 +0100 | [diff] [blame] | 224 | return 0; |
| 225 | } |
Damien George | 8721087 | 2014-04-13 00:30:32 +0100 | [diff] [blame] | 226 | qstr label_qstr = MP_PARSE_NODE_LEAF_ARG(pn); |
Damien | 826005c | 2013-10-05 23:17:28 +0100 | [diff] [blame] | 227 | for (int i = 0; i < emit->max_num_labels; i++) { |
| 228 | if (emit->label_lookup[i] == label_qstr) { |
| 229 | return i; |
| 230 | } |
| 231 | } |
Damien | dc83382 | 2013-10-06 01:01:01 +0100 | [diff] [blame] | 232 | // only need to have the labels on the last pass |
Damien George | 36db6bc | 2014-05-07 17:24:22 +0100 | [diff] [blame] | 233 | if (emit->pass == MP_PASS_EMIT) { |
Damien George | a26dc50 | 2014-04-12 17:54:52 +0100 | [diff] [blame] | 234 | emit_inline_thumb_error(emit, "label '%s' not defined\n", qstr_str(label_qstr)); |
Damien | dc83382 | 2013-10-06 01:01:01 +0100 | [diff] [blame] | 235 | } |
Damien | 826005c | 2013-10-05 23:17:28 +0100 | [diff] [blame] | 236 | return 0; |
| 237 | } |
| 238 | |
Damien George | 47e1b85 | 2014-04-08 18:28:33 +0100 | [diff] [blame] | 239 | typedef struct _cc_name_t { byte cc; byte name[2]; } cc_name_t; |
| 240 | STATIC const cc_name_t cc_name_table[] = { |
Damien George | 0b610de | 2014-09-29 16:25:04 +0100 | [diff] [blame] | 241 | { ASM_THUMB_CC_EQ, "eq" }, |
| 242 | { ASM_THUMB_CC_NE, "ne" }, |
| 243 | { ASM_THUMB_CC_CS, "cs" }, |
| 244 | { ASM_THUMB_CC_CC, "cc" }, |
| 245 | { ASM_THUMB_CC_MI, "mi" }, |
| 246 | { ASM_THUMB_CC_PL, "pl" }, |
| 247 | { ASM_THUMB_CC_VS, "vs" }, |
| 248 | { ASM_THUMB_CC_VC, "vc" }, |
| 249 | { ASM_THUMB_CC_HI, "hi" }, |
| 250 | { ASM_THUMB_CC_LS, "ls" }, |
| 251 | { ASM_THUMB_CC_GE, "ge" }, |
| 252 | { ASM_THUMB_CC_LT, "lt" }, |
| 253 | { ASM_THUMB_CC_GT, "gt" }, |
| 254 | { ASM_THUMB_CC_LE, "le" }, |
Damien George | 47e1b85 | 2014-04-08 18:28:33 +0100 | [diff] [blame] | 255 | }; |
| 256 | |
Damien George | 7ff996c | 2014-09-08 23:05:16 +0100 | [diff] [blame] | 257 | STATIC void emit_inline_thumb_op(emit_inline_asm_t *emit, qstr op, mp_uint_t n_args, mp_parse_node_t *pn_args) { |
Damien | 826005c | 2013-10-05 23:17:28 +0100 | [diff] [blame] | 258 | // TODO perhaps make two tables: |
Damien | 03d4124 | 2013-10-06 00:36:05 +0100 | [diff] [blame] | 259 | // one_args = |
| 260 | // "b", LAB, asm_thumb_b_n, |
| 261 | // "bgt", LAB, asm_thumb_bgt_n, |
Damien | 826005c | 2013-10-05 23:17:28 +0100 | [diff] [blame] | 262 | // two_args = |
| 263 | // "movs", RLO, I8, asm_thumb_movs_reg_i8 |
| 264 | // "movw", REG, REG, asm_thumb_movw_reg_i16 |
| 265 | // three_args = |
| 266 | // "subs", RLO, RLO, I3, asm_thumb_subs_reg_reg_i3 |
| 267 | |
Damien George | 47e1b85 | 2014-04-08 18:28:33 +0100 | [diff] [blame] | 268 | const char *op_str = qstr_str(op); |
Damien George | 7ff996c | 2014-09-08 23:05:16 +0100 | [diff] [blame] | 269 | mp_uint_t op_len = strlen(op_str); |
Damien | 826005c | 2013-10-05 23:17:28 +0100 | [diff] [blame] | 270 | |
Damien George | 47e1b85 | 2014-04-08 18:28:33 +0100 | [diff] [blame] | 271 | if (n_args == 0) { |
Damien George | 90edf9e | 2014-04-18 16:56:54 +0100 | [diff] [blame] | 272 | if (strcmp(op_str, "nop") == 0) { |
| 273 | asm_thumb_op16(emit->as, ASM_THUMB_OP_NOP); |
| 274 | } else if (strcmp(op_str, "wfi") == 0) { |
| 275 | asm_thumb_op16(emit->as, ASM_THUMB_OP_WFI); |
| 276 | } else if (strcmp(op_str, "ite.ge") == 0) { // TODO correct name for this op? |
Damien George | 851f15f | 2014-09-29 10:05:32 +0100 | [diff] [blame] | 277 | asm_thumb_op16(emit->as, ASM_THUMB_OP_ITE_GE); |
Damien George | 47e1b85 | 2014-04-08 18:28:33 +0100 | [diff] [blame] | 278 | } else { |
| 279 | goto unknown_op; |
Damien | 826005c | 2013-10-05 23:17:28 +0100 | [diff] [blame] | 280 | } |
Damien | 826005c | 2013-10-05 23:17:28 +0100 | [diff] [blame] | 281 | |
Damien George | 47e1b85 | 2014-04-08 18:28:33 +0100 | [diff] [blame] | 282 | } else if (n_args == 1) { |
| 283 | if (strcmp(op_str, "b") == 0) { |
Damien George | 8721087 | 2014-04-13 00:30:32 +0100 | [diff] [blame] | 284 | int label_num = get_arg_label(emit, op_str, pn_args[0]); |
Damien George | 47e1b85 | 2014-04-08 18:28:33 +0100 | [diff] [blame] | 285 | // TODO check that this succeeded, ie branch was within range |
| 286 | asm_thumb_b_n(emit->as, label_num); |
| 287 | } else if (op_str[0] == 'b' && op_len == 3) { |
Damien George | 7ff996c | 2014-09-08 23:05:16 +0100 | [diff] [blame] | 288 | mp_uint_t cc = -1; |
| 289 | for (mp_uint_t i = 0; i < MP_ARRAY_SIZE(cc_name_table); i++) { |
Damien George | 47e1b85 | 2014-04-08 18:28:33 +0100 | [diff] [blame] | 290 | if (op_str[1] == cc_name_table[i].name[0] && op_str[2] == cc_name_table[i].name[1]) { |
| 291 | cc = cc_name_table[i].cc; |
| 292 | } |
| 293 | } |
| 294 | if (cc == -1) { |
| 295 | goto unknown_op; |
| 296 | } |
Damien George | 8721087 | 2014-04-13 00:30:32 +0100 | [diff] [blame] | 297 | int label_num = get_arg_label(emit, op_str, pn_args[0]); |
Damien George | 47e1b85 | 2014-04-08 18:28:33 +0100 | [diff] [blame] | 298 | // TODO check that this succeeded, ie branch was within range |
| 299 | asm_thumb_bcc_n(emit->as, cc, label_num); |
Damien George | 90edf9e | 2014-04-18 16:56:54 +0100 | [diff] [blame] | 300 | } else if (strcmp(op_str, "cpsid")) { |
| 301 | // TODO check pn_args[0] == i |
| 302 | asm_thumb_op16(emit->as, ASM_THUMB_OP_CPSID_I); |
| 303 | } else if (strcmp(op_str, "cpsie")) { |
| 304 | // TODO check pn_args[0] == i |
| 305 | asm_thumb_op16(emit->as, ASM_THUMB_OP_CPSIE_I); |
Damien George | 47e1b85 | 2014-04-08 18:28:33 +0100 | [diff] [blame] | 306 | } else { |
| 307 | goto unknown_op; |
Damien | 826005c | 2013-10-05 23:17:28 +0100 | [diff] [blame] | 308 | } |
Damien | 826005c | 2013-10-05 23:17:28 +0100 | [diff] [blame] | 309 | |
Damien George | 47e1b85 | 2014-04-08 18:28:33 +0100 | [diff] [blame] | 310 | } else if (n_args == 2) { |
Damien George | 8721087 | 2014-04-13 00:30:32 +0100 | [diff] [blame] | 311 | if (MP_PARSE_NODE_IS_ID(pn_args[1])) { |
| 312 | // second arg is a register (or should be) |
Damien George | 7ff996c | 2014-09-08 23:05:16 +0100 | [diff] [blame] | 313 | mp_uint_t op_code; |
Damien George | 8721087 | 2014-04-13 00:30:32 +0100 | [diff] [blame] | 314 | if (strcmp(op_str, "mov") == 0) { |
Damien George | 7ff996c | 2014-09-08 23:05:16 +0100 | [diff] [blame] | 315 | mp_uint_t reg_dest = get_arg_reg(emit, op_str, pn_args[0], 15); |
| 316 | mp_uint_t reg_src = get_arg_reg(emit, op_str, pn_args[1], 15); |
Damien George | 8721087 | 2014-04-13 00:30:32 +0100 | [diff] [blame] | 317 | asm_thumb_mov_reg_reg(emit->as, reg_dest, reg_src); |
Damien George | a4022c9 | 2014-07-17 12:37:56 +0100 | [diff] [blame] | 318 | } else if (strcmp(op_str, "and_") == 0) { |
Damien George | 8721087 | 2014-04-13 00:30:32 +0100 | [diff] [blame] | 319 | op_code = ASM_THUMB_FORMAT_4_AND; |
Damien George | 7ff996c | 2014-09-08 23:05:16 +0100 | [diff] [blame] | 320 | mp_uint_t reg_dest, reg_src; |
Damien George | 8721087 | 2014-04-13 00:30:32 +0100 | [diff] [blame] | 321 | op_format_4: |
| 322 | reg_dest = get_arg_reg(emit, op_str, pn_args[0], 7); |
| 323 | reg_src = get_arg_reg(emit, op_str, pn_args[1], 7); |
| 324 | asm_thumb_format_4(emit->as, op_code, reg_dest, reg_src); |
| 325 | // TODO probably uses less ROM if these ops are in a lookup table |
Damien George | 8721087 | 2014-04-13 00:30:32 +0100 | [diff] [blame] | 326 | } else if (strcmp(op_str, "eor") == 0) { op_code = ASM_THUMB_FORMAT_4_EOR; goto op_format_4; |
| 327 | } else if (strcmp(op_str, "lsl") == 0) { op_code = ASM_THUMB_FORMAT_4_LSL; goto op_format_4; |
| 328 | } else if (strcmp(op_str, "lsr") == 0) { op_code = ASM_THUMB_FORMAT_4_LSR; goto op_format_4; |
| 329 | } else if (strcmp(op_str, "asr") == 0) { op_code = ASM_THUMB_FORMAT_4_ASR; goto op_format_4; |
| 330 | } else if (strcmp(op_str, "adc") == 0) { op_code = ASM_THUMB_FORMAT_4_ADC; goto op_format_4; |
| 331 | } else if (strcmp(op_str, "sbc") == 0) { op_code = ASM_THUMB_FORMAT_4_SBC; goto op_format_4; |
| 332 | } else if (strcmp(op_str, "ror") == 0) { op_code = ASM_THUMB_FORMAT_4_ROR; goto op_format_4; |
| 333 | } else if (strcmp(op_str, "tst") == 0) { op_code = ASM_THUMB_FORMAT_4_TST; goto op_format_4; |
| 334 | } else if (strcmp(op_str, "neg") == 0) { op_code = ASM_THUMB_FORMAT_4_NEG; goto op_format_4; |
| 335 | } else if (strcmp(op_str, "cmp") == 0) { op_code = ASM_THUMB_FORMAT_4_CMP; goto op_format_4; |
| 336 | } else if (strcmp(op_str, "cmn") == 0) { op_code = ASM_THUMB_FORMAT_4_CMN; goto op_format_4; |
| 337 | } else if (strcmp(op_str, "orr") == 0) { op_code = ASM_THUMB_FORMAT_4_ORR; goto op_format_4; |
| 338 | } else if (strcmp(op_str, "mul") == 0) { op_code = ASM_THUMB_FORMAT_4_MUL; goto op_format_4; |
| 339 | } else if (strcmp(op_str, "bic") == 0) { op_code = ASM_THUMB_FORMAT_4_BIC; goto op_format_4; |
| 340 | } else if (strcmp(op_str, "mvn") == 0) { op_code = ASM_THUMB_FORMAT_4_MVN; goto op_format_4; |
| 341 | } else { |
| 342 | goto unknown_op; |
| 343 | } |
Damien George | 47e1b85 | 2014-04-08 18:28:33 +0100 | [diff] [blame] | 344 | } else { |
Damien George | 8721087 | 2014-04-13 00:30:32 +0100 | [diff] [blame] | 345 | // second arg is not a register |
Damien George | 7ff996c | 2014-09-08 23:05:16 +0100 | [diff] [blame] | 346 | mp_uint_t op_code; |
Damien George | 8721087 | 2014-04-13 00:30:32 +0100 | [diff] [blame] | 347 | if (strcmp(op_str, "mov") == 0) { |
| 348 | op_code = ASM_THUMB_FORMAT_3_MOV; |
Damien George | 7ff996c | 2014-09-08 23:05:16 +0100 | [diff] [blame] | 349 | mp_uint_t rlo_dest, i8_src; |
Damien George | 8721087 | 2014-04-13 00:30:32 +0100 | [diff] [blame] | 350 | op_format_3: |
| 351 | rlo_dest = get_arg_reg(emit, op_str, pn_args[0], 7); |
| 352 | i8_src = get_arg_i(emit, op_str, pn_args[1], 0xff); |
| 353 | asm_thumb_format_3(emit->as, op_code, rlo_dest, i8_src); |
| 354 | } else if (strcmp(op_str, "cmp") == 0) { |
| 355 | op_code = ASM_THUMB_FORMAT_3_CMP; |
| 356 | goto op_format_3; |
| 357 | } else if (strcmp(op_str, "add") == 0) { |
| 358 | op_code = ASM_THUMB_FORMAT_3_ADD; |
| 359 | goto op_format_3; |
| 360 | } else if (strcmp(op_str, "sub") == 0) { |
| 361 | op_code = ASM_THUMB_FORMAT_3_SUB; |
| 362 | goto op_format_3; |
| 363 | } else if (strcmp(op_str, "movw") == 0) { |
Damien George | 7ff996c | 2014-09-08 23:05:16 +0100 | [diff] [blame] | 364 | mp_uint_t reg_dest = get_arg_reg(emit, op_str, pn_args[0], 15); |
Damien George | 8721087 | 2014-04-13 00:30:32 +0100 | [diff] [blame] | 365 | int i_src = get_arg_i(emit, op_str, pn_args[1], 0xffff); |
| 366 | asm_thumb_movw_reg_i16(emit->as, reg_dest, i_src); |
| 367 | } else if (strcmp(op_str, "movt") == 0) { |
Damien George | 7ff996c | 2014-09-08 23:05:16 +0100 | [diff] [blame] | 368 | mp_uint_t reg_dest = get_arg_reg(emit, op_str, pn_args[0], 15); |
Damien George | 8721087 | 2014-04-13 00:30:32 +0100 | [diff] [blame] | 369 | int i_src = get_arg_i(emit, op_str, pn_args[1], 0xffff); |
| 370 | asm_thumb_movt_reg_i16(emit->as, reg_dest, i_src); |
| 371 | } else if (strcmp(op_str, "movwt") == 0) { |
| 372 | // this is a convenience instruction |
| 373 | // we clear the MSB since it might be set from extracting the small int value |
Damien George | 7ff996c | 2014-09-08 23:05:16 +0100 | [diff] [blame] | 374 | mp_uint_t reg_dest = get_arg_reg(emit, op_str, pn_args[0], 15); |
Damien George | 8721087 | 2014-04-13 00:30:32 +0100 | [diff] [blame] | 375 | int i_src = get_arg_i(emit, op_str, pn_args[1], 0xffffffff); |
| 376 | asm_thumb_movw_reg_i16(emit->as, reg_dest, i_src & 0xffff); |
| 377 | asm_thumb_movt_reg_i16(emit->as, reg_dest, (i_src >> 16) & 0x7fff); |
| 378 | } else if (strcmp(op_str, "ldr") == 0) { |
| 379 | op_code = ASM_THUMB_FORMAT_9_LDR | ASM_THUMB_FORMAT_9_WORD_TRANSFER; |
Damien George | 7ff996c | 2014-09-08 23:05:16 +0100 | [diff] [blame] | 380 | mp_uint_t rlo_dest, rlo_base, i5; |
Damien George | 8721087 | 2014-04-13 00:30:32 +0100 | [diff] [blame] | 381 | mp_parse_node_t pn_base, pn_offset; |
| 382 | op_format_9_10: |
| 383 | rlo_dest = get_arg_reg(emit, op_str, pn_args[0], 7); |
| 384 | if (get_arg_addr(emit, op_str, pn_args[1], &pn_base, &pn_offset)) { |
| 385 | rlo_base = get_arg_reg(emit, op_str, pn_base, 7); |
| 386 | if (op_code & ASM_THUMB_FORMAT_9_BYTE_TRANSFER) { |
| 387 | i5 = get_arg_i(emit, op_str, pn_offset, 0x1f); |
| 388 | } else if (op_code & ASM_THUMB_FORMAT_10_STRH) { // also catches LDRH |
| 389 | i5 = get_arg_i(emit, op_str, pn_offset, 0x3e) >> 1; |
| 390 | } else { |
| 391 | i5 = get_arg_i(emit, op_str, pn_offset, 0x7c) >> 2; |
| 392 | } |
| 393 | asm_thumb_format_9_10(emit->as, op_code, rlo_dest, rlo_base, i5); |
| 394 | } |
| 395 | } else if (strcmp(op_str, "ldrb") == 0) { |
| 396 | op_code = ASM_THUMB_FORMAT_9_LDR | ASM_THUMB_FORMAT_9_BYTE_TRANSFER; |
| 397 | goto op_format_9_10; |
| 398 | } else if (strcmp(op_str, "ldrh") == 0) { |
| 399 | op_code = ASM_THUMB_FORMAT_10_LDRH; |
| 400 | goto op_format_9_10; |
| 401 | } else if (strcmp(op_str, "str") == 0) { |
| 402 | op_code = ASM_THUMB_FORMAT_9_STR | ASM_THUMB_FORMAT_9_WORD_TRANSFER; |
| 403 | goto op_format_9_10; |
| 404 | } else if (strcmp(op_str, "strb") == 0) { |
| 405 | op_code = ASM_THUMB_FORMAT_9_STR | ASM_THUMB_FORMAT_9_BYTE_TRANSFER; |
| 406 | goto op_format_9_10; |
| 407 | } else if (strcmp(op_str, "strh") == 0) { |
| 408 | op_code = ASM_THUMB_FORMAT_10_STRH; |
| 409 | goto op_format_9_10; |
| 410 | } else { |
| 411 | goto unknown_op; |
| 412 | } |
Damien George | 47e1b85 | 2014-04-08 18:28:33 +0100 | [diff] [blame] | 413 | } |
| 414 | |
| 415 | } else if (n_args == 3) { |
Damien George | 7ff996c | 2014-09-08 23:05:16 +0100 | [diff] [blame] | 416 | mp_uint_t op_code; |
Damien George | 47e1b85 | 2014-04-08 18:28:33 +0100 | [diff] [blame] | 417 | if (strcmp(op_str, "add") == 0) { |
Damien George | 8721087 | 2014-04-13 00:30:32 +0100 | [diff] [blame] | 418 | op_code = ASM_THUMB_FORMAT_2_ADD; |
Damien George | 7ff996c | 2014-09-08 23:05:16 +0100 | [diff] [blame] | 419 | mp_uint_t rlo_dest, rlo_src; |
Damien George | 8721087 | 2014-04-13 00:30:32 +0100 | [diff] [blame] | 420 | op_format_2: |
| 421 | rlo_dest = get_arg_reg(emit, op_str, pn_args[0], 7); |
| 422 | rlo_src = get_arg_reg(emit, op_str, pn_args[1], 7); |
| 423 | int src_b; |
| 424 | if (MP_PARSE_NODE_IS_ID(pn_args[2])) { |
| 425 | op_code |= ASM_THUMB_FORMAT_2_REG_OPERAND; |
| 426 | src_b = get_arg_reg(emit, op_str, pn_args[2], 7); |
| 427 | } else { |
| 428 | op_code |= ASM_THUMB_FORMAT_2_IMM_OPERAND; |
| 429 | src_b = get_arg_i(emit, op_str, pn_args[2], 0x7); |
| 430 | } |
| 431 | asm_thumb_format_2(emit->as, op_code, rlo_dest, rlo_src, src_b); |
| 432 | } else if (strcmp(op_str, "sub") == 0) { |
| 433 | op_code = ASM_THUMB_FORMAT_2_SUB; |
| 434 | goto op_format_2; |
Damien George | 47e1b85 | 2014-04-08 18:28:33 +0100 | [diff] [blame] | 435 | } else { |
| 436 | goto unknown_op; |
| 437 | } |
| 438 | |
Damien | 826005c | 2013-10-05 23:17:28 +0100 | [diff] [blame] | 439 | } else { |
Damien George | 47e1b85 | 2014-04-08 18:28:33 +0100 | [diff] [blame] | 440 | goto unknown_op; |
Damien | 826005c | 2013-10-05 23:17:28 +0100 | [diff] [blame] | 441 | } |
Damien George | 47e1b85 | 2014-04-08 18:28:33 +0100 | [diff] [blame] | 442 | |
| 443 | return; |
| 444 | |
| 445 | unknown_op: |
Damien George | a26dc50 | 2014-04-12 17:54:52 +0100 | [diff] [blame] | 446 | emit_inline_thumb_error(emit, "unsupported Thumb instruction '%s' with %d arguments\n", op_str, n_args); |
Damien | 826005c | 2013-10-05 23:17:28 +0100 | [diff] [blame] | 447 | } |
| 448 | |
| 449 | const emit_inline_asm_method_table_t emit_inline_thumb_method_table = { |
| 450 | emit_inline_thumb_start_pass, |
| 451 | emit_inline_thumb_end_pass, |
Damien | a2f2f7d | 2013-10-06 00:14:13 +0100 | [diff] [blame] | 452 | emit_inline_thumb_count_params, |
Damien | 826005c | 2013-10-05 23:17:28 +0100 | [diff] [blame] | 453 | emit_inline_thumb_label, |
Damien George | e5f8a77 | 2014-04-21 13:33:15 +0100 | [diff] [blame] | 454 | emit_inline_thumb_align, |
| 455 | emit_inline_thumb_data, |
Damien | 826005c | 2013-10-05 23:17:28 +0100 | [diff] [blame] | 456 | emit_inline_thumb_op, |
| 457 | }; |
| 458 | |
Damien | 3ef4abb | 2013-10-12 16:53:13 +0100 | [diff] [blame] | 459 | #endif // MICROPY_EMIT_INLINE_THUMB |