Skip to content

Commit ad297a1

Browse files
committed
py: Allow inline-assembler emitter to be generic.
This patch refactors some code so that it is easier to integrate new inline assemblers for different architectures other than ARM Thumb.
1 parent 45a6156 commit ad297a1

7 files changed

Lines changed: 41 additions & 32 deletions

File tree

py/asmbase.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
#include "py/misc.h"
3232
#include "py/asmbase.h"
3333

34-
#if MICROPY_EMIT_NATIVE || MICROPY_EMIT_INLINE_THUMB
34+
#if MICROPY_EMIT_NATIVE || MICROPY_EMIT_INLINE_ASM
3535

3636
void mp_asm_base_init(mp_asm_base_t *as, size_t max_num_labels) {
3737
as->max_num_labels = max_num_labels;
@@ -101,4 +101,4 @@ void mp_asm_base_data(mp_asm_base_t* as, unsigned int bytesize, uintptr_t val) {
101101
}
102102
}
103103

104-
#endif // MICROPY_EMIT_NATIVE || MICROPY_EMIT_INLINE_THUMB
104+
#endif // MICROPY_EMIT_NATIVE || MICROPY_EMIT_INLINE_ASM

py/compile.c

Lines changed: 30 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,16 @@ typedef enum {
8686
#endif
8787
#endif
8888

89+
#if MICROPY_EMIT_INLINE_ASM
90+
// define macros for inline assembler
91+
#if MICROPY_EMIT_INLINE_THUMB
92+
#define ASM_DECORATOR_QSTR MP_QSTR_asm_thumb
93+
#define ASM_EMITTER(f) emit_inline_thumb_##f
94+
#else
95+
#error "unknown asm emitter"
96+
#endif
97+
#endif
98+
8999
#define EMIT_INLINE_ASM(fun) (comp->emit_inline_asm_method_table->fun(comp->emit_inline_asm))
90100
#define EMIT_INLINE_ASM_ARG(fun, ...) (comp->emit_inline_asm_method_table->fun(comp->emit_inline_asm, __VA_ARGS__))
91101

@@ -120,7 +130,7 @@ typedef struct _compiler_t {
120130
const emit_method_table_t *emit_method_table; // current emit method table
121131
#endif
122132

123-
#if MICROPY_EMIT_INLINE_THUMB
133+
#if MICROPY_EMIT_INLINE_ASM
124134
emit_inline_asm_t *emit_inline_asm; // current emitter for inline asm
125135
const emit_inline_asm_method_table_t *emit_inline_asm_method_table; // current emit method table for inline asm
126136
#endif
@@ -767,10 +777,10 @@ STATIC bool compile_built_in_decorator(compiler_t *comp, int name_len, mp_parse_
767777
} else if (attr == MP_QSTR_viper) {
768778
*emit_options = MP_EMIT_OPT_VIPER;
769779
#endif
770-
#if MICROPY_EMIT_INLINE_THUMB
771-
} else if (attr == MP_QSTR_asm_thumb) {
772-
*emit_options = MP_EMIT_OPT_ASM_THUMB;
773-
#endif
780+
#if MICROPY_EMIT_INLINE_ASM
781+
} else if (attr == ASM_DECORATOR_QSTR) {
782+
*emit_options = MP_EMIT_OPT_ASM;
783+
#endif
774784
} else {
775785
compile_syntax_error(comp, name_nodes[1], "invalid micropython decorator");
776786
}
@@ -3100,7 +3110,7 @@ STATIC void compile_scope(compiler_t *comp, scope_t *scope, pass_kind_t pass) {
31003110
assert(comp->cur_except_level == 0);
31013111
}
31023112

3103-
#if MICROPY_EMIT_INLINE_THUMB
3113+
#if MICROPY_EMIT_INLINE_ASM
31043114
// requires 3 passes: SCOPE, CODE_SIZE, EMIT
31053115
STATIC void compile_scope_inline_asm(compiler_t *comp, scope_t *scope, pass_kind_t pass) {
31063116
comp->pass = pass;
@@ -3357,10 +3367,10 @@ mp_raw_code_t *mp_compile_to_raw_code(mp_parse_tree_t *parse_tree, qstr source_f
33573367
uint max_num_labels = 0;
33583368
for (scope_t *s = comp->scope_head; s != NULL && comp->compile_error == MP_OBJ_NULL; s = s->next) {
33593369
if (false) {
3360-
#if MICROPY_EMIT_INLINE_THUMB
3361-
} else if (s->emit_options == MP_EMIT_OPT_ASM_THUMB) {
3370+
#if MICROPY_EMIT_INLINE_ASM
3371+
} else if (s->emit_options == MP_EMIT_OPT_ASM) {
33623372
compile_scope_inline_asm(comp, s, MP_PASS_SCOPE);
3363-
#endif
3373+
#endif
33643374
} else {
33653375
compile_scope(comp, s, MP_PASS_SCOPE);
33663376
}
@@ -3382,28 +3392,24 @@ mp_raw_code_t *mp_compile_to_raw_code(mp_parse_tree_t *parse_tree, qstr source_f
33823392
// compile pass 2 and 3
33833393
#if MICROPY_EMIT_NATIVE
33843394
emit_t *emit_native = NULL;
3385-
#endif
3386-
#if MICROPY_EMIT_INLINE_THUMB
3387-
emit_inline_asm_t *emit_inline_thumb = NULL;
33883395
#endif
33893396
for (scope_t *s = comp->scope_head; s != NULL && comp->compile_error == MP_OBJ_NULL; s = s->next) {
33903397
if (false) {
33913398
// dummy
33923399

3393-
#if MICROPY_EMIT_INLINE_THUMB
3394-
} else if (s->emit_options == MP_EMIT_OPT_ASM_THUMB) {
3395-
// inline assembly for thumb
3396-
if (emit_inline_thumb == NULL) {
3397-
emit_inline_thumb = emit_inline_thumb_new(max_num_labels);
3400+
#if MICROPY_EMIT_INLINE_ASM
3401+
} else if (s->emit_options == MP_EMIT_OPT_ASM) {
3402+
// inline assembly
3403+
if (comp->emit_inline_asm == NULL) {
3404+
comp->emit_inline_asm = ASM_EMITTER(new)(max_num_labels);
33983405
}
33993406
comp->emit = NULL;
3400-
comp->emit_inline_asm = emit_inline_thumb;
3401-
comp->emit_inline_asm_method_table = &emit_inline_thumb_method_table;
3407+
comp->emit_inline_asm_method_table = &ASM_EMITTER(method_table);
34023408
compile_scope_inline_asm(comp, s, MP_PASS_CODE_SIZE);
34033409
if (comp->compile_error == MP_OBJ_NULL) {
34043410
compile_scope_inline_asm(comp, s, MP_PASS_EMIT);
34053411
}
3406-
#endif
3412+
#endif
34073413

34083414
} else {
34093415

@@ -3463,11 +3469,11 @@ mp_raw_code_t *mp_compile_to_raw_code(mp_parse_tree_t *parse_tree, qstr source_f
34633469
NATIVE_EMITTER(free)(emit_native);
34643470
}
34653471
#endif
3466-
#if MICROPY_EMIT_INLINE_THUMB
3467-
if (emit_inline_thumb != NULL) {
3468-
emit_inline_thumb_free(emit_inline_thumb);
3472+
#if MICROPY_EMIT_INLINE_ASM
3473+
if (comp->emit_inline_asm != NULL) {
3474+
ASM_EMITTER(free)(comp->emit_inline_asm);
34693475
}
3470-
#endif
3476+
#endif
34713477

34723478
// free the parse tree
34733479
mp_parse_tree_clear(parse_tree);

py/compile.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ enum {
3636
MP_EMIT_OPT_BYTECODE,
3737
MP_EMIT_OPT_NATIVE_PYTHON,
3838
MP_EMIT_OPT_VIPER,
39-
MP_EMIT_OPT_ASM_THUMB,
39+
MP_EMIT_OPT_ASM,
4040
};
4141

4242
// the compiler will raise an exception if an error occurred

py/emitglue.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ void mp_emit_glue_assign_bytecode(mp_raw_code_t *rc, const byte *code, mp_uint_t
8282
#endif
8383
}
8484

85-
#if MICROPY_EMIT_NATIVE || MICROPY_EMIT_INLINE_THUMB
85+
#if MICROPY_EMIT_NATIVE || MICROPY_EMIT_INLINE_ASM
8686
void mp_emit_glue_assign_native(mp_raw_code_t *rc, mp_raw_code_kind_t kind, void *fun_data, mp_uint_t fun_len, const mp_uint_t *const_table, mp_uint_t n_pos_args, mp_uint_t scope_flags, mp_uint_t type_sig) {
8787
assert(kind == MP_CODE_NATIVE_PY || kind == MP_CODE_NATIVE_VIPER || kind == MP_CODE_NATIVE_ASM);
8888
rc->kind = kind;
@@ -138,7 +138,7 @@ mp_obj_t mp_make_function_from_raw_code(const mp_raw_code_t *rc, mp_obj_t def_ar
138138
fun = mp_obj_new_fun_viper(rc->n_pos_args, rc->data.u_native.fun_data, rc->data.u_native.type_sig);
139139
break;
140140
#endif
141-
#if MICROPY_EMIT_INLINE_THUMB
141+
#if MICROPY_EMIT_INLINE_ASM
142142
case MP_CODE_NATIVE_ASM:
143143
fun = mp_obj_new_fun_asm(rc->n_pos_args, rc->data.u_native.fun_data, rc->data.u_native.type_sig);
144144
break;

py/mpconfig.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,9 @@
296296
// Convenience definition for whether any native emitter is enabled
297297
#define MICROPY_EMIT_NATIVE (MICROPY_EMIT_X64 || MICROPY_EMIT_X86 || MICROPY_EMIT_THUMB || MICROPY_EMIT_ARM || MICROPY_EMIT_XTENSA)
298298

299+
// Convenience definition for whether any inline assembler emitter is enabled
300+
#define MICROPY_EMIT_INLINE_ASM (MICROPY_EMIT_INLINE_THUMB)
301+
299302
/*****************************************************************************/
300303
/* Compiler configuration */
301304

py/nativeglue.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ mp_uint_t mp_convert_obj_to_native(mp_obj_t obj, mp_uint_t type) {
6464

6565
#endif
6666

67-
#if MICROPY_EMIT_NATIVE || MICROPY_EMIT_INLINE_THUMB
67+
#if MICROPY_EMIT_NATIVE || MICROPY_EMIT_INLINE_ASM
6868

6969
// convert a native value to a Micro Python object based on type
7070
mp_obj_t mp_convert_native_to_obj(mp_uint_t val, mp_uint_t type) {

py/objfun.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -471,7 +471,7 @@ mp_obj_t mp_obj_new_fun_viper(mp_uint_t n_args, void *fun_data, mp_uint_t type_s
471471
/******************************************************************************/
472472
/* inline assembler functions */
473473

474-
#if MICROPY_EMIT_INLINE_THUMB
474+
#if MICROPY_EMIT_INLINE_ASM
475475

476476
typedef struct _mp_obj_fun_asm_t {
477477
mp_obj_base_t base;
@@ -582,4 +582,4 @@ mp_obj_t mp_obj_new_fun_asm(mp_uint_t n_args, void *fun_data, mp_uint_t type_sig
582582
return o;
583583
}
584584

585-
#endif // MICROPY_EMIT_INLINE_THUMB
585+
#endif // MICROPY_EMIT_INLINE_ASM

0 commit comments

Comments
 (0)