Damien | 826005c | 2013-10-05 23:17:28 +0100 | [diff] [blame] | 1 | #include <unistd.h> |
| 2 | #include <stdlib.h> |
| 3 | #include <stdint.h> |
| 4 | #include <stdio.h> |
| 5 | #include <string.h> |
| 6 | #include <assert.h> |
| 7 | |
| 8 | #include "misc.h" |
Damien | c025ebb | 2013-10-12 14:30:21 +0100 | [diff] [blame^] | 9 | #include "mpyconfig.h" |
Damien | 826005c | 2013-10-05 23:17:28 +0100 | [diff] [blame] | 10 | #include "lexer.h" |
Damien | 826005c | 2013-10-05 23:17:28 +0100 | [diff] [blame] | 11 | #include "parse.h" |
| 12 | #include "scope.h" |
| 13 | #include "runtime.h" |
| 14 | #include "emit.h" |
| 15 | #include "asmthumb.h" |
| 16 | |
Damien | c025ebb | 2013-10-12 14:30:21 +0100 | [diff] [blame^] | 17 | #ifdef MICROPY_EMIT_ENABLE_INLINE_THUMB |
Damien | 826005c | 2013-10-05 23:17:28 +0100 | [diff] [blame] | 18 | |
| 19 | struct _emit_inline_asm_t { |
| 20 | int pass; |
| 21 | scope_t *scope; |
| 22 | int max_num_labels; |
| 23 | qstr *label_lookup; |
| 24 | asm_thumb_t *as; |
| 25 | }; |
| 26 | |
| 27 | emit_inline_asm_t *emit_inline_thumb_new(uint max_num_labels) { |
| 28 | emit_inline_asm_t *emit = m_new(emit_inline_asm_t, 1); |
| 29 | emit->max_num_labels = max_num_labels; |
| 30 | emit->label_lookup = m_new(qstr, max_num_labels); |
| 31 | memset(emit->label_lookup, 0, emit->max_num_labels * sizeof(qstr)); |
| 32 | emit->as = asm_thumb_new(max_num_labels); |
| 33 | return emit; |
| 34 | } |
| 35 | |
| 36 | static void emit_inline_thumb_start_pass(emit_inline_asm_t *emit, pass_kind_t pass, scope_t *scope) { |
| 37 | emit->pass = pass; |
| 38 | emit->scope = scope; |
| 39 | asm_thumb_start_pass(emit->as, pass); |
| 40 | asm_thumb_entry(emit->as, 0); |
| 41 | } |
| 42 | |
| 43 | static void emit_inline_thumb_end_pass(emit_inline_asm_t *emit) { |
| 44 | asm_thumb_exit(emit->as); |
| 45 | asm_thumb_end_pass(emit->as); |
| 46 | |
| 47 | if (emit->pass == PASS_3) { |
| 48 | py_fun_t f = asm_thumb_get_code(emit->as); |
| 49 | rt_assign_inline_asm_code(emit->scope->unique_code_id, f, asm_thumb_get_code_size(emit->as), emit->scope->num_params); |
| 50 | } |
| 51 | } |
| 52 | |
Damien | a2f2f7d | 2013-10-06 00:14:13 +0100 | [diff] [blame] | 53 | static int emit_inline_thumb_count_params(emit_inline_asm_t *emit, int n_params, py_parse_node_t *pn_params) { |
| 54 | if (n_params > 4) { |
Damien | 03d4124 | 2013-10-06 00:36:05 +0100 | [diff] [blame] | 55 | printf("SyntaxError: can only have up to 4 parameters to inline thumb assembly\n"); |
Damien | a2f2f7d | 2013-10-06 00:14:13 +0100 | [diff] [blame] | 56 | return 0; |
| 57 | } |
| 58 | for (int i = 0; i < n_params; i++) { |
| 59 | if (!PY_PARSE_NODE_IS_ID(pn_params[i])) { |
Damien | dc83382 | 2013-10-06 01:01:01 +0100 | [diff] [blame] | 60 | printf("SyntaxError: parameter to inline assembler must be an identifier\n"); |
Damien | a2f2f7d | 2013-10-06 00:14:13 +0100 | [diff] [blame] | 61 | return 0; |
| 62 | } |
| 63 | const char *p = qstr_str(PY_PARSE_NODE_LEAF_ARG(pn_params[i])); |
| 64 | if (!(strlen(p) == 2 && p[0] == 'r' && p[1] == '0' + i)) { |
Damien | 03d4124 | 2013-10-06 00:36:05 +0100 | [diff] [blame] | 65 | printf("SyntaxError: parameter %d to inline assembler must be r%d\n", i + 1, i); |
Damien | a2f2f7d | 2013-10-06 00:14:13 +0100 | [diff] [blame] | 66 | return 0; |
| 67 | } |
| 68 | } |
| 69 | return n_params; |
| 70 | } |
| 71 | |
Damien | 826005c | 2013-10-05 23:17:28 +0100 | [diff] [blame] | 72 | static void emit_inline_thumb_label(emit_inline_asm_t *emit, int label_num, qstr label_id) { |
| 73 | assert(label_num < emit->max_num_labels); |
| 74 | emit->label_lookup[label_num] = label_id; |
| 75 | asm_thumb_label_assign(emit->as, label_num); |
| 76 | } |
| 77 | |
| 78 | static bool check_n_arg(qstr op, int n_args, int wanted_n_args) { |
| 79 | if (wanted_n_args == n_args) { |
| 80 | return true; |
| 81 | } else { |
| 82 | printf("SyntaxError: '%s' expects %d arguments'\n", qstr_str(op), wanted_n_args); |
| 83 | return false; |
| 84 | } |
| 85 | } |
| 86 | |
Damien | a2f2f7d | 2013-10-06 00:14:13 +0100 | [diff] [blame] | 87 | static uint get_arg_rlo(qstr op, py_parse_node_t *pn_args, int wanted_arg_num) { |
| 88 | if (!PY_PARSE_NODE_IS_ID(pn_args[wanted_arg_num])) { |
Damien | 826005c | 2013-10-05 23:17:28 +0100 | [diff] [blame] | 89 | printf("SyntaxError: '%s' expects a register in position %d\n", qstr_str(op), wanted_arg_num); |
| 90 | return 0; |
| 91 | } |
Damien | a2f2f7d | 2013-10-06 00:14:13 +0100 | [diff] [blame] | 92 | qstr reg_qstr = PY_PARSE_NODE_LEAF_ARG(pn_args[wanted_arg_num]); |
Damien | 826005c | 2013-10-05 23:17:28 +0100 | [diff] [blame] | 93 | const char *reg_str = qstr_str(reg_qstr); |
| 94 | if (!(strlen(reg_str) == 2 && reg_str[0] == 'r' && ('0' <= reg_str[1] && reg_str[1] <= '7'))) { |
| 95 | printf("SyntaxError: '%s' expects a register in position %d\n", qstr_str(op), wanted_arg_num); |
| 96 | return 0; |
| 97 | } |
| 98 | return reg_str[1] - '0'; |
| 99 | } |
| 100 | |
Damien | a2f2f7d | 2013-10-06 00:14:13 +0100 | [diff] [blame] | 101 | static int get_arg_i(qstr op, py_parse_node_t *pn_args, int wanted_arg_num, int fit_mask) { |
| 102 | if (!PY_PARSE_NODE_IS_SMALL_INT(pn_args[wanted_arg_num])) { |
Damien | 826005c | 2013-10-05 23:17:28 +0100 | [diff] [blame] | 103 | printf("SyntaxError: '%s' expects an integer in position %d\n", qstr_str(op), wanted_arg_num); |
| 104 | return 0; |
| 105 | } |
Damien | a2f2f7d | 2013-10-06 00:14:13 +0100 | [diff] [blame] | 106 | int i = PY_PARSE_NODE_LEAF_ARG(pn_args[wanted_arg_num]); |
Damien | 826005c | 2013-10-05 23:17:28 +0100 | [diff] [blame] | 107 | if ((i & (~fit_mask)) != 0) { |
| 108 | printf("SyntaxError: '%s' integer 0x%x does not fit in mask 0x%x\n", qstr_str(op), i, fit_mask); |
| 109 | return 0; |
| 110 | } |
| 111 | return i; |
| 112 | } |
| 113 | |
Damien | a2f2f7d | 2013-10-06 00:14:13 +0100 | [diff] [blame] | 114 | static int get_arg_label(emit_inline_asm_t *emit, qstr op, py_parse_node_t *pn_args, int wanted_arg_num) { |
| 115 | if (!PY_PARSE_NODE_IS_ID(pn_args[wanted_arg_num])) { |
Damien | 826005c | 2013-10-05 23:17:28 +0100 | [diff] [blame] | 116 | printf("SyntaxError: '%s' expects a label in position %d\n", qstr_str(op), wanted_arg_num); |
| 117 | return 0; |
| 118 | } |
Damien | a2f2f7d | 2013-10-06 00:14:13 +0100 | [diff] [blame] | 119 | qstr label_qstr = PY_PARSE_NODE_LEAF_ARG(pn_args[wanted_arg_num]); |
Damien | 826005c | 2013-10-05 23:17:28 +0100 | [diff] [blame] | 120 | for (int i = 0; i < emit->max_num_labels; i++) { |
| 121 | if (emit->label_lookup[i] == label_qstr) { |
| 122 | return i; |
| 123 | } |
| 124 | } |
Damien | dc83382 | 2013-10-06 01:01:01 +0100 | [diff] [blame] | 125 | // only need to have the labels on the last pass |
| 126 | if (emit->pass == PASS_3) { |
| 127 | printf("SyntaxError: label '%s' not defined\n", qstr_str(label_qstr)); |
| 128 | } |
Damien | 826005c | 2013-10-05 23:17:28 +0100 | [diff] [blame] | 129 | return 0; |
| 130 | } |
| 131 | |
Damien | a2f2f7d | 2013-10-06 00:14:13 +0100 | [diff] [blame] | 132 | static void emit_inline_thumb_op(emit_inline_asm_t *emit, qstr op, int n_args, py_parse_node_t *pn_args) { |
Damien | 826005c | 2013-10-05 23:17:28 +0100 | [diff] [blame] | 133 | // TODO perhaps make two tables: |
Damien | 03d4124 | 2013-10-06 00:36:05 +0100 | [diff] [blame] | 134 | // one_args = |
| 135 | // "b", LAB, asm_thumb_b_n, |
| 136 | // "bgt", LAB, asm_thumb_bgt_n, |
Damien | 826005c | 2013-10-05 23:17:28 +0100 | [diff] [blame] | 137 | // two_args = |
| 138 | // "movs", RLO, I8, asm_thumb_movs_reg_i8 |
| 139 | // "movw", REG, REG, asm_thumb_movw_reg_i16 |
| 140 | // three_args = |
| 141 | // "subs", RLO, RLO, I3, asm_thumb_subs_reg_reg_i3 |
| 142 | |
| 143 | // 1 arg |
Damien | 03d4124 | 2013-10-06 00:36:05 +0100 | [diff] [blame] | 144 | if (strcmp(qstr_str(op), "b") == 0) { |
| 145 | if (!check_n_arg(op, n_args, 1)) { |
| 146 | return; |
| 147 | } |
| 148 | int label_num = get_arg_label(emit, op, pn_args, 0); |
| 149 | // TODO check that this succeeded, ie branch was within range |
| 150 | asm_thumb_b_n(emit->as, label_num); |
| 151 | } else if (strcmp(qstr_str(op), "bgt") == 0) { |
Damien | 826005c | 2013-10-05 23:17:28 +0100 | [diff] [blame] | 152 | if (!check_n_arg(op, n_args, 1)) { |
| 153 | return; |
| 154 | } |
Damien | a2f2f7d | 2013-10-06 00:14:13 +0100 | [diff] [blame] | 155 | int label_num = get_arg_label(emit, op, pn_args, 0); |
| 156 | // TODO check that this succeeded, ie branch was within range |
Damien | 826005c | 2013-10-05 23:17:28 +0100 | [diff] [blame] | 157 | asm_thumb_bgt_n(emit->as, label_num); |
| 158 | |
| 159 | // 2 args |
| 160 | } else if (strcmp(qstr_str(op), "movs") == 0) { |
| 161 | if (!check_n_arg(op, n_args, 2)) { |
| 162 | return; |
| 163 | } |
Damien | a2f2f7d | 2013-10-06 00:14:13 +0100 | [diff] [blame] | 164 | uint rlo_dest = get_arg_rlo(op, pn_args, 0); |
| 165 | int i_src = get_arg_i(op, pn_args, 1, 0xff); |
Damien | 826005c | 2013-10-05 23:17:28 +0100 | [diff] [blame] | 166 | asm_thumb_movs_rlo_i8(emit->as, rlo_dest, i_src); |
| 167 | } else if (strcmp(qstr_str(op), "movw") == 0) { |
| 168 | if (!check_n_arg(op, n_args, 2)) { |
| 169 | return; |
| 170 | } |
Damien | a2f2f7d | 2013-10-06 00:14:13 +0100 | [diff] [blame] | 171 | uint rlo_dest = get_arg_rlo(op, pn_args, 0); // TODO can be reg lo or hi |
| 172 | int i_src = get_arg_i(op, pn_args, 1, 0xffff); |
Damien | 826005c | 2013-10-05 23:17:28 +0100 | [diff] [blame] | 173 | asm_thumb_movw_reg_i16(emit->as, rlo_dest, i_src); |
| 174 | } else if (strcmp(qstr_str(op), "cmp") == 0) { |
| 175 | if (!check_n_arg(op, n_args, 2)) { |
| 176 | return; |
| 177 | } |
Damien | a2f2f7d | 2013-10-06 00:14:13 +0100 | [diff] [blame] | 178 | uint rlo = get_arg_rlo(op, pn_args, 0); |
| 179 | int i8 = get_arg_i(op, pn_args, 1, 0xff); |
Damien | 826005c | 2013-10-05 23:17:28 +0100 | [diff] [blame] | 180 | asm_thumb_cmp_rlo_i8(emit->as, rlo, i8); |
| 181 | |
| 182 | // 3 args |
| 183 | } else if (strcmp(qstr_str(op), "subs") == 0) { |
| 184 | if (!check_n_arg(op, n_args, 3)) { |
| 185 | return; |
| 186 | } |
Damien | a2f2f7d | 2013-10-06 00:14:13 +0100 | [diff] [blame] | 187 | uint rlo_dest = get_arg_rlo(op, pn_args, 0); |
| 188 | uint rlo_src = get_arg_rlo(op, pn_args, 1); |
| 189 | int i3_src = get_arg_i(op, pn_args, 2, 0x7); |
Damien | 826005c | 2013-10-05 23:17:28 +0100 | [diff] [blame] | 190 | asm_thumb_subs_rlo_rlo_i3(emit->as, rlo_dest, rlo_src, i3_src); |
| 191 | |
| 192 | // unknown op |
| 193 | } else { |
| 194 | printf("SyntaxError: unsupported ARM Thumb instruction '%s'\n", qstr_str(op)); |
| 195 | return; |
| 196 | } |
| 197 | } |
| 198 | |
| 199 | const emit_inline_asm_method_table_t emit_inline_thumb_method_table = { |
| 200 | emit_inline_thumb_start_pass, |
| 201 | emit_inline_thumb_end_pass, |
Damien | a2f2f7d | 2013-10-06 00:14:13 +0100 | [diff] [blame] | 202 | emit_inline_thumb_count_params, |
Damien | 826005c | 2013-10-05 23:17:28 +0100 | [diff] [blame] | 203 | emit_inline_thumb_label, |
| 204 | emit_inline_thumb_op, |
| 205 | }; |
| 206 | |
Damien | c025ebb | 2013-10-12 14:30:21 +0100 | [diff] [blame^] | 207 | #endif // MICROPY_EMIT_ENABLE_INLINE_THUMB |