emitinlinethumb.c 7.24 KB
Newer Older
1
2
3
4
5
6
7
8
#include <unistd.h>
#include <stdlib.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include <assert.h>

#include "misc.h"
9
#include "mpconfig.h"
10
#include "qstr.h"
11
12
13
#include "lexer.h"
#include "parse.h"
#include "scope.h"
14
#include "runtime0.h"
15
16
17
#include "emit.h"
#include "asmthumb.h"

18
#if MICROPY_EMIT_INLINE_THUMB
19
20
21
22

struct _emit_inline_asm_t {
    int pass;
    scope_t *scope;
23
    uint max_num_labels;
24
25
26
27
28
    qstr *label_lookup;
    asm_thumb_t *as;
};

emit_inline_asm_t *emit_inline_thumb_new(uint max_num_labels) {
29
    emit_inline_asm_t *emit = m_new_obj(emit_inline_asm_t);
30
31
32
33
34
35
36
    emit->max_num_labels = max_num_labels;
    emit->label_lookup = m_new(qstr, max_num_labels);
    memset(emit->label_lookup, 0, emit->max_num_labels * sizeof(qstr));
    emit->as = asm_thumb_new(max_num_labels);
    return emit;
}

37
38
39
40
41
42
void emit_inline_thumb_free(emit_inline_asm_t *emit) {
    m_del(qstr, emit->label_lookup, emit->max_num_labels);
    asm_thumb_free(emit->as, false);
    m_del_obj(emit_inline_asm_t, emit);
}

43
44
45
46
47
48
49
50
51
52
53
54
static void emit_inline_thumb_start_pass(emit_inline_asm_t *emit, pass_kind_t pass, scope_t *scope) {
    emit->pass = pass;
    emit->scope = scope;
    asm_thumb_start_pass(emit->as, pass);
    asm_thumb_entry(emit->as, 0);
}

static void emit_inline_thumb_end_pass(emit_inline_asm_t *emit) {
    asm_thumb_exit(emit->as);
    asm_thumb_end_pass(emit->as);

    if (emit->pass == PASS_3) {
55
        void *f = asm_thumb_get_code(emit->as);
56
57
58
59
        rt_assign_inline_asm_code(emit->scope->unique_code_id, f, asm_thumb_get_code_size(emit->as), emit->scope->num_params);
    }
}

60
static int emit_inline_thumb_count_params(emit_inline_asm_t *emit, int n_params, mp_parse_node_t *pn_params) {
61
    if (n_params > 4) {
Damien's avatar
Damien committed
62
        printf("SyntaxError: can only have up to 4 parameters to inline thumb assembly\n");
63
64
65
        return 0;
    }
    for (int i = 0; i < n_params; i++) {
66
        if (!MP_PARSE_NODE_IS_ID(pn_params[i])) {
67
            printf("SyntaxError: parameter to inline assembler must be an identifier\n");
68
69
            return 0;
        }
70
        const char *p = qstr_str(MP_PARSE_NODE_LEAF_ARG(pn_params[i]));
71
        if (!(strlen(p) == 2 && p[0] == 'r' && p[1] == '0' + i)) {
Damien's avatar
Damien committed
72
            printf("SyntaxError: parameter %d to inline assembler must be r%d\n", i + 1, i);
73
74
75
76
77
78
            return 0;
        }
    }
    return n_params;
}

79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
static void emit_inline_thumb_label(emit_inline_asm_t *emit, int label_num, qstr label_id) {
    assert(label_num < emit->max_num_labels);
    emit->label_lookup[label_num] = label_id;
    asm_thumb_label_assign(emit->as, label_num);
}

static bool check_n_arg(qstr op, int n_args, int wanted_n_args) {
    if (wanted_n_args == n_args) {
        return true;
    } else {
        printf("SyntaxError: '%s' expects %d arguments'\n", qstr_str(op), wanted_n_args);
        return false;
    }
}

94
95
static uint get_arg_rlo(qstr op, mp_parse_node_t *pn_args, int wanted_arg_num) {
    if (!MP_PARSE_NODE_IS_ID(pn_args[wanted_arg_num])) {
96
97
98
        printf("SyntaxError: '%s' expects a register in position %d\n", qstr_str(op), wanted_arg_num);
        return 0;
    }
99
    qstr reg_qstr = MP_PARSE_NODE_LEAF_ARG(pn_args[wanted_arg_num]);
100
101
102
103
104
105
106
107
    const char *reg_str = qstr_str(reg_qstr);
    if (!(strlen(reg_str) == 2 && reg_str[0] == 'r' && ('0' <= reg_str[1] && reg_str[1] <= '7'))) {
        printf("SyntaxError: '%s' expects a register in position %d\n", qstr_str(op), wanted_arg_num);
        return 0;
    }
    return reg_str[1] - '0';
}

108
109
static int get_arg_i(qstr op, mp_parse_node_t *pn_args, int wanted_arg_num, int fit_mask) {
    if (!MP_PARSE_NODE_IS_SMALL_INT(pn_args[wanted_arg_num])) {
110
111
112
        printf("SyntaxError: '%s' expects an integer in position %d\n", qstr_str(op), wanted_arg_num);
        return 0;
    }
113
    int i = MP_PARSE_NODE_LEAF_ARG(pn_args[wanted_arg_num]);
114
115
116
117
118
119
120
    if ((i & (~fit_mask)) != 0) {
        printf("SyntaxError: '%s' integer 0x%x does not fit in mask 0x%x\n", qstr_str(op), i, fit_mask);
        return 0;
    }
    return i;
}

121
122
static int get_arg_label(emit_inline_asm_t *emit, qstr op, mp_parse_node_t *pn_args, int wanted_arg_num) {
    if (!MP_PARSE_NODE_IS_ID(pn_args[wanted_arg_num])) {
123
124
125
        printf("SyntaxError: '%s' expects a label in position %d\n", qstr_str(op), wanted_arg_num);
        return 0;
    }
126
    qstr label_qstr = MP_PARSE_NODE_LEAF_ARG(pn_args[wanted_arg_num]);
127
128
129
130
131
    for (int i = 0; i < emit->max_num_labels; i++) {
        if (emit->label_lookup[i] == label_qstr) {
            return i;
        }
    }
132
133
134
135
    // only need to have the labels on the last pass
    if (emit->pass == PASS_3) {
        printf("SyntaxError: label '%s' not defined\n", qstr_str(label_qstr));
    }
136
137
138
    return 0;
}

139
static void emit_inline_thumb_op(emit_inline_asm_t *emit, qstr op, int n_args, mp_parse_node_t *pn_args) {
140
    // TODO perhaps make two tables:
Damien's avatar
Damien committed
141
142
143
    // one_args =
    // "b", LAB, asm_thumb_b_n,
    // "bgt", LAB, asm_thumb_bgt_n,
144
145
146
147
148
149
150
    // two_args =
    // "movs", RLO, I8, asm_thumb_movs_reg_i8
    // "movw", REG, REG, asm_thumb_movw_reg_i16
    // three_args =
    // "subs", RLO, RLO, I3, asm_thumb_subs_reg_reg_i3

    // 1 arg
Damien's avatar
Damien committed
151
152
153
154
155
156
157
158
    if (strcmp(qstr_str(op), "b") == 0) {
        if (!check_n_arg(op, n_args, 1)) {
            return;
        }
        int label_num = get_arg_label(emit, op, pn_args, 0);
        // TODO check that this succeeded, ie branch was within range
        asm_thumb_b_n(emit->as, label_num);
    } else if (strcmp(qstr_str(op), "bgt") == 0) {
159
160
161
        if (!check_n_arg(op, n_args, 1)) {
            return;
        }
162
163
        int label_num = get_arg_label(emit, op, pn_args, 0);
        // TODO check that this succeeded, ie branch was within range
164
        asm_thumb_bcc_n(emit->as, THUMB_CC_GT, label_num);
165
166
167
168
169
170

    // 2 args
    } else if (strcmp(qstr_str(op), "movs") == 0) {
        if (!check_n_arg(op, n_args, 2)) {
            return;
        }
171
172
        uint rlo_dest = get_arg_rlo(op, pn_args, 0);
        int i_src = get_arg_i(op, pn_args, 1, 0xff);
173
174
175
176
177
        asm_thumb_movs_rlo_i8(emit->as, rlo_dest, i_src);
    } else if (strcmp(qstr_str(op), "movw") == 0) {
        if (!check_n_arg(op, n_args, 2)) {
            return;
        }
178
179
        uint rlo_dest = get_arg_rlo(op, pn_args, 0); // TODO can be reg lo or hi
        int i_src = get_arg_i(op, pn_args, 1, 0xffff);
180
181
182
183
184
        asm_thumb_movw_reg_i16(emit->as, rlo_dest, i_src);
    } else if (strcmp(qstr_str(op), "cmp") == 0) {
        if (!check_n_arg(op, n_args, 2)) {
            return;
        }
185
186
        uint rlo = get_arg_rlo(op, pn_args, 0);
        int i8 = get_arg_i(op, pn_args, 1, 0xff);
187
188
189
190
191
192
193
        asm_thumb_cmp_rlo_i8(emit->as, rlo, i8);

    // 3 args
    } else if (strcmp(qstr_str(op), "subs") == 0) {
        if (!check_n_arg(op, n_args, 3)) {
            return;
        }
194
195
196
        uint rlo_dest = get_arg_rlo(op, pn_args, 0);
        uint rlo_src = get_arg_rlo(op, pn_args, 1);
        int i3_src = get_arg_i(op, pn_args, 2, 0x7);
197
198
199
200
201
202
203
204
205
206
207
208
        asm_thumb_subs_rlo_rlo_i3(emit->as, rlo_dest, rlo_src, i3_src);

    // unknown op
    } else {
        printf("SyntaxError: unsupported ARM Thumb instruction '%s'\n", qstr_str(op));
        return;
    }
}

const emit_inline_asm_method_table_t emit_inline_thumb_method_table = {
    emit_inline_thumb_start_pass,
    emit_inline_thumb_end_pass,
209
    emit_inline_thumb_count_params,
210
211
212
213
    emit_inline_thumb_label,
    emit_inline_thumb_op,
};

214
#endif // MICROPY_EMIT_INLINE_THUMB