Skip to content

Commit e8d89a0

Browse files
committed
py: Allow mpy-cross to exclude source lines.
This would allow building debug FW without source line data. Signed-off-by: Roman Zeyde <[email protected]>
1 parent c2a5481 commit e8d89a0

4 files changed

Lines changed: 29 additions & 1 deletion

File tree

mpy-cross/main.c

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,10 @@
4444
STATIC uint emit_opt = MP_EMIT_OPT_NONE;
4545
mp_uint_t mp_verbose_flag = 0;
4646

47+
#if MICROPY_ENABLE_SOURCE_LINE
48+
static bool include_source_lines = true;
49+
#endif
50+
4751
// Heap size of GC heap (if enabled)
4852
// Make it larger on a 64 bit machine, because pointers are larger.
4953
long heap_size = 1024 * 1024 * (sizeof(mp_uint_t) / 4);
@@ -126,6 +130,12 @@ STATIC int usage(char **argv) {
126130
" heapsize=<n> -- set the heap size for the GC (default %ld)\n"
127131
, heap_size);
128132
impl_opts_cnt++;
133+
#if MICROPY_ENABLE_SOURCE_LINE
134+
printf(
135+
" source-lines -- include source line numbers (default)\n"
136+
" no-source-lines -- exclude source line numbers\n");
137+
impl_opts_cnt += 2;
138+
#endif
129139

130140
if (impl_opts_cnt == 0) {
131141
printf(" (none)\n");
@@ -149,6 +159,14 @@ STATIC void pre_process_options(int argc, char **argv) {
149159
emit_opt = MP_EMIT_OPT_NATIVE_PYTHON;
150160
} else if (strcmp(argv[a + 1], "emit=viper") == 0) {
151161
emit_opt = MP_EMIT_OPT_VIPER;
162+
#if MICROPY_ENABLE_SOURCE_LINE
163+
} else if (strcmp(argv[a + 1], "source-lines") == 0) {
164+
// Allow excluding source lines for debug builds.
165+
include_source_lines = true;
166+
} else if (strcmp(argv[a + 1], "no-source-lines") == 0) {
167+
// Allow excluding source lines for debug builds.
168+
include_source_lines = false;
169+
#endif
152170
#endif
153171
} else if (strncmp(argv[a + 1], "heapsize=", sizeof("heapsize=") - 1) == 0) {
154172
char *end;
@@ -201,6 +219,10 @@ MP_NOINLINE int main_(int argc, char **argv) {
201219
(void)emit_opt;
202220
#endif
203221

222+
#if MICROPY_ENABLE_SOURCE_LINE
223+
MP_STATE_VM(include_source_lines) = include_source_lines;
224+
#endif
225+
204226
// set default compiler configuration
205227
mp_dynamic_compiler.small_int_bits = 31;
206228
#if defined(__i386__)

py/emitbc.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -419,7 +419,7 @@ void mp_emit_bc_set_source_line(emit_t *emit, mp_uint_t source_line) {
419419
// If we compile with -O3, don't store line numbers.
420420
return;
421421
}
422-
if (source_line > emit->last_source_line) {
422+
if (MP_STATE_VM(include_source_lines) && source_line > emit->last_source_line) {
423423
mp_uint_t bytes_to_skip = emit->bytecode_offset - emit->last_source_line_offset;
424424
mp_uint_t lines_to_skip = source_line - emit->last_source_line;
425425
emit_write_code_info_bytes_lines(emit, bytes_to_skip, lines_to_skip);

py/mpstate.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,9 @@ typedef struct _mp_state_vm_t {
235235
#if MICROPY_EMIT_NATIVE
236236
uint8_t default_emit_opt; // one of MP_EMIT_OPT_xxx
237237
#endif
238+
#if MICROPY_ENABLE_SOURCE_LINE
239+
bool include_source_lines;
240+
#endif
238241
#endif
239242

240243
// size of the emergency exception buf, if it's dynamically allocated

py/runtime.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,9 @@ void mp_init(void) {
9999
#if MICROPY_EMIT_NATIVE
100100
MP_STATE_VM(default_emit_opt) = MP_EMIT_OPT_NONE;
101101
#endif
102+
#if MICROPY_ENABLE_SOURCE_LINE
103+
MP_STATE_VM(include_source_lines) = true;
104+
#endif
102105
#endif
103106

104107
// init global module dict

0 commit comments

Comments
 (0)