← index #9842Issue #3445
Off-topic · high · value 0.778
QUERY · ISSUE

mpy-ld.py: preprocess c source before trying to find qstr-s

openby TsXoropened 2022-11-05updated 2022-11-05
enhancement

Currently, mpy-ld.py only directly search for MP_QSTR_* keywords in the source file declared in Makefile, in some situations, it will fail to find MP_QSTR_* keywords and make it fail to build:

  • It's too ugly to put all my source code in only one file, so I put some code which uses MP_QSTR_* in another file and includes it.
  • MP_QSTR_* expression is constructed with a macro.

I think expanding users' includes and macros with something like pcpp may help.

CANDIDATE · ISSUE

freezing an .mpy with mpy-cross -s suffix = *.mpy results in frozen_mpy.c having a DOT prepended to "__lt_module_gt_"

openby adritiumopened 2017-11-17updated 2024-09-29
  1. Create empty main.py
  2. "../../mpy-cross/mpy-cross.exe" -o main.mpy -s main.**mpy** main.py
    Note the -s param suffix is .mpy, not the to-be-expected .py
  3. python "../../tools/mpy-tool.py" -f -q build\genhdr\qstrdefs.preprocessed.h main.mpy > frozen_mpy.c
    will result in a dot placed where it shouldn't be i.e. search for .__lt_module_gt_ in the below frozen_mpy.c (it's in 4 places)

I understand you'd want to have -s with .py usually and I wasn't trying to break mpy-cross. If for some reason mpy-cross -s *.mpy isn't allowed because it unnecessarily increases difficulty in mpy-tool, then mpy-cross should err out and simply not produce an .mpy

But the idea place to fix this is in mpy-tool.py.


#include "py/mpconfig.h"
#include "py/objint.h"
#include "py/objstr.h"
#include "py/emitglue.h"

#if MICROPY_OPT_CACHE_MAP_LOOKUP_IN_BYTECODE != 0
#error "incompatible MICROPY_OPT_CACHE_MAP_LOOKUP_IN_BYTECODE"
#endif

#if MICROPY_LONGINT_IMPL != 2
#error "incompatible MICROPY_LONGINT_IMPL"
#endif

#if MPZ_DIG_SIZE != 16
#error "incompatible MPZ_DIG_SIZE"
#endif

#if MICROPY_PY_BUILTINS_FLOAT
typedef struct _mp_obj_float_t {
    mp_obj_base_t base;
    mp_float_t value;
} mp_obj_float_t;
#endif

#if MICROPY_PY_BUILTINS_COMPLEX
typedef struct _mp_obj_complex_t {
    mp_obj_base_t base;
    mp_float_t real;
    mp_float_t imag;
} mp_obj_complex_t;
#endif

enum {
    MP_QSTR_main_dot_mpy = MP_QSTRnumber_of,
};

extern const qstr_pool_t mp_qstr_const_pool;
const qstr_pool_t mp_qstr_frozen_const_pool = {
    (qstr_pool_t*)&mp_qstr_const_pool, // previous pool
    MP_QSTRnumber_of, // previous pool size
    1, // allocated entries
    1, // used entries
    {
        (const byte*)"\xc4\x08" "main.mpy",
    },
};

// frozen bytecode for file main.mpy, scope main._<module>
STATIC const byte bytecode_data_main.__lt_module_gt_[16] = {
    0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x07,
    MP_QSTR__lt_module_gt_ & 0xff, MP_QSTR__lt_module_gt_ >> 8,
    MP_QSTR_main_dot_mpy & 0xff, MP_QSTR_main_dot_mpy >> 8,
    0x00, 0x00, 0xff,
    0x11, 
    0x5b, 
};
const mp_raw_code_t raw_code_main.__lt_module_gt_ = {
    .kind = MP_CODE_BYTECODE,
    .scope_flags = 0x00,
    .n_pos_args = 0,
    .data.u_byte = {
        .bytecode = bytecode_data_main.__lt_module_gt_,
        .const_table = NULL,
        #if MICROPY_PERSISTENT_CODE_SAVE
        .bc_len = 16,
        .n_obj = 0,
        .n_raw_code = 0,
        #endif
    },
};

const char mp_frozen_mpy_names[] = {
"main.mpy\0"
"\0"};
const mp_raw_code_t *const mp_frozen_mpy_content[] = {
    &raw_code_main.__lt_module_gt_,
};

Keyboard

j / / n
next pair
k / / p
previous pair
1 / / h
show query pane
2 / / l
show candidate pane
c
copy suggested comment
r
toggle reasoning
g i
go to index
?
show this help
esc
close overlays

press ? or esc to close

copied