From 0e054b84279d64bb435a916fa7f459558a1b26b3 Mon Sep 17 00:00:00 2001 From: Damien George Date: Fri, 5 Jun 2026 16:46:37 +1000 Subject: [PATCH 1/2] mpy-cross/main: Remove unused mp_verbose_flag. This variable does nothing in mpy-cross because: - `MICROPY_DEBUG_PRINTERS` is not enabled, so the relevant code in `py/compile.c` that uses `mp_verbose_flag` is unused. - Even if `MICROPY_DEBUG_PRINTERS` was enabled, nothing happens because mpy-cross has `MP_PLAT_PRINT_STRN` defined to do nothing. - `MICROPY_PY_MICROPYTHON_MEM_INFO` is not enabled. So remove it. But keep the `-v` CLI option to not break existing uses of mpy-cross, and keep it compatible with the CLI options of the unix port. Signed-off-by: Damien George --- mpy-cross/main.c | 9 +-------- 1 file changed, 1 insertion(+), 8 deletions(-) diff --git a/mpy-cross/main.c b/mpy-cross/main.c index c5ef86acd6bbc..a89151a1c7a59 100644 --- a/mpy-cross/main.c +++ b/mpy-cross/main.c @@ -49,7 +49,6 @@ static asm_rv32_backend_options_t rv32_options = { 0 }; // Command line options, with their defaults static uint emit_opt = MP_EMIT_OPT_NONE; -mp_uint_t mp_verbose_flag = 0; #if MICROPY_ENABLE_SOURCE_LINE static bool include_source_lines = true; @@ -340,7 +339,7 @@ MP_NOINLINE int main_(int argc, char **argv) { "; mpy-cross emitting mpy v" MP_STRINGIFY(MPY_VERSION) "." MP_STRINGIFY(MPY_SUB_VERSION) "\n"); return 0; } else if (strcmp(argv[a], "-v") == 0) { - mp_verbose_flag++; + // This verbose option doesn't currently do anything. } else if (strncmp(argv[a], "-O", 2) == 0) { if (unichar_isdigit(argv[a][2])) { MP_STATE_VM(mp_optimise_value) = argv[a][2] & 0xf; @@ -482,12 +481,6 @@ MP_NOINLINE int main_(int argc, char **argv) { int ret = compile_and_save(input_file, output_file, source_file); - #if MICROPY_PY_MICROPYTHON_MEM_INFO - if (mp_verbose_flag) { - mp_micropython_mem_info(0, NULL); - } - #endif - mp_deinit(); return ret & 0xff; From a4bfad95945ea39b9b29977e3301fadaedb5a86a Mon Sep 17 00:00:00 2001 From: Damien George Date: Fri, 5 Jun 2026 16:50:07 +1000 Subject: [PATCH 2/2] py/mpstate: Move mp_verbose_flag to MP_STATE_VM struct. This stray global variable doesn't really belong in `py/emitglue.c` because it's currently only used by `py/compile.c`. So move it to the state context struct alongside the related compiler settings. Signed-off-by: Damien George --- ports/unix/main.c | 6 +++--- py/compile.c | 2 +- py/emitglue.c | 4 ---- py/misc.h | 2 -- py/mpstate.h | 3 +++ py/runtime.c | 3 +++ shared/runtime/pyexec.c | 2 +- 7 files changed, 11 insertions(+), 11 deletions(-) diff --git a/ports/unix/main.c b/ports/unix/main.c index 9e9704aa801db..8f42d4f56dce2 100644 --- a/ports/unix/main.c +++ b/ports/unix/main.c @@ -142,7 +142,7 @@ static int execute_from_lexer(int source_kind, const void *source, mp_parse_inpu #if defined(MICROPY_UNIX_COVERAGE) // allow to print the parse tree in the coverage build - if (mp_verbose_flag >= 3) { + if (MP_STATE_VM(mp_verbose_flag) >= 3) { printf("----------------\n"); mp_parse_node_print(&mp_plat_print, parse_tree.root, 0); printf("----------------\n"); @@ -661,7 +661,7 @@ MP_NOINLINE int main_(int argc, char **argv) { a += 1; #if MICROPY_DEBUG_PRINTERS } else if (strcmp(argv[a], "-v") == 0) { - mp_verbose_flag++; + MP_STATE_VM(mp_verbose_flag)++; #endif } else if (strncmp(argv[a], "-O", 2) == 0) { if (unichar_isdigit(argv[a][2])) { @@ -720,7 +720,7 @@ MP_NOINLINE int main_(int argc, char **argv) { #endif #if MICROPY_PY_MICROPYTHON_MEM_INFO - if (mp_verbose_flag) { + if (MP_STATE_VM(mp_verbose_flag)) { mp_micropython_mem_info(0, NULL); } #endif diff --git a/py/compile.c b/py/compile.c index 37a3e6d32c4e1..263f456767847 100644 --- a/py/compile.c +++ b/py/compile.c @@ -3654,7 +3654,7 @@ void mp_compile_to_raw_code(mp_parse_tree_t *parse_tree, qstr source_file, bool #if MICROPY_DEBUG_PRINTERS // now that the module context is valid, the raw codes can be printed - if (mp_verbose_flag >= 2) { + if (MP_STATE_VM(mp_verbose_flag) >= 2) { for (scope_t *s = comp->scope_head; s != NULL; s = s->next) { mp_raw_code_t *rc = s->raw_code; if (rc->kind == MP_CODE_BYTECODE) { diff --git a/py/emitglue.c b/py/emitglue.c index 9526acac805a3..390f5f93a1a3c 100644 --- a/py/emitglue.c +++ b/py/emitglue.c @@ -48,10 +48,6 @@ #define DEBUG_OP_printf(...) (void)0 #endif -#if MICROPY_DEBUG_PRINTERS -mp_uint_t mp_verbose_flag = 0; -#endif - mp_raw_code_t *mp_emit_glue_new_raw_code(void) { mp_raw_code_t *rc = m_new0(mp_raw_code_t, 1); rc->kind = MP_CODE_RESERVED; diff --git a/py/misc.h b/py/misc.h index 2fe0f11796b3e..7afa01789bcad 100644 --- a/py/misc.h +++ b/py/misc.h @@ -262,8 +262,6 @@ void vstr_vprintf(vstr_t *vstr, const char *fmt, va_list ap); int DEBUG_printf(const char *fmt, ...); -extern mp_uint_t mp_verbose_flag; - /** float internals *************/ #if MICROPY_PY_BUILTINS_FLOAT diff --git a/py/mpstate.h b/py/mpstate.h index 7662813b72ad4..004593496e2c5 100644 --- a/py/mpstate.h +++ b/py/mpstate.h @@ -234,6 +234,9 @@ typedef struct _mp_state_vm_t { #if MICROPY_EMIT_NATIVE uint8_t default_emit_opt; // one of MP_EMIT_OPT_xxx #endif + #if MICROPY_DEBUG_PRINTERS + mp_uint_t mp_verbose_flag; + #endif #endif // size of the emergency exception buf, if it's dynamically allocated diff --git a/py/runtime.c b/py/runtime.c index 618e9b5ae41cf..d614bc21f9b9f 100644 --- a/py/runtime.c +++ b/py/runtime.c @@ -105,6 +105,9 @@ void mp_init(void) { #if MICROPY_EMIT_NATIVE MP_STATE_VM(default_emit_opt) = MP_EMIT_OPT_NONE; #endif + #if MICROPY_DEBUG_PRINTERS + MP_STATE_VM(mp_verbose_flag) = 0; + #endif #endif // init global module dict diff --git a/shared/runtime/pyexec.c b/shared/runtime/pyexec.c index b217f301118c5..112ae8761b47b 100644 --- a/shared/runtime/pyexec.c +++ b/shared/runtime/pyexec.c @@ -111,7 +111,7 @@ static int parse_compile_execute(const void *source, mp_parse_input_kind_t input mp_parse_tree_t parse_tree = mp_parse(lex, input_kind); #if defined(MICROPY_UNIX_COVERAGE) // allow to print the parse tree in the coverage build - if (mp_verbose_flag >= 3) { + if (MP_STATE_VM(mp_verbose_flag) >= 3) { printf("----------------\n"); mp_parse_node_print(&mp_plat_print, parse_tree.root, 0); printf("----------------\n");