Skip to content

Commit cbd2f74

Browse files
committed
py: Add module/function/class name to exceptions.
Exceptions know source file, line and block name. Also tidy up some debug printing functions and provide a global flag to enable/disable them.
1 parent e02b2d4 commit cbd2f74

17 files changed

Lines changed: 83 additions & 83 deletions

File tree

py/bc.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
mp_obj_t mp_execute_byte_code(const byte *code, const mp_obj_t *args, uint n_args, uint n_state);
22
bool mp_execute_byte_code_2(const byte *code_info, const byte **ip_in_out, mp_obj_t *fastn, mp_obj_t **sp_in_out);
3+
void mp_byte_code_print(const byte *code, int len);

py/compile.c

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ typedef enum {
3939
#define EMIT_OPT_ASM_THUMB (4)
4040

4141
typedef struct _compiler_t {
42+
qstr source_file;
4243
bool is_repl;
4344
pass_kind_t pass;
4445
bool had_error; // try to keep compiler clean from nlr
@@ -189,7 +190,7 @@ static int comp_next_label(compiler_t *comp) {
189190
}
190191

191192
static scope_t *scope_new_and_link(compiler_t *comp, scope_kind_t kind, mp_parse_node_t pn, uint emit_options) {
192-
scope_t *scope = scope_new(kind, pn, rt_get_unique_code_id(), emit_options);
193+
scope_t *scope = scope_new(kind, pn, comp->source_file, rt_get_unique_code_id(), emit_options);
193194
scope->parent = comp->scope_cur;
194195
scope->next = NULL;
195196
if (comp->scope_head == NULL) {
@@ -2509,7 +2510,9 @@ void compile_node(compiler_t *comp, mp_parse_node_t pn) {
25092510
compile_function_t f = compile_function[MP_PARSE_NODE_STRUCT_KIND(pns)];
25102511
if (f == NULL) {
25112512
printf("node %u cannot be compiled\n", (uint)MP_PARSE_NODE_STRUCT_KIND(pns));
2512-
mp_parse_node_show(pn, 0);
2513+
#if MICROPY_DEBUG_PRINTERS
2514+
mp_parse_node_print(pn, 0);
2515+
#endif
25132516
assert(0);
25142517
} else {
25152518
f(comp, pns);
@@ -2875,10 +2878,12 @@ void compile_scope_inline_asm(compiler_t *comp, scope_t *scope, pass_kind_t pass
28752878
mp_parse_node_t *nodes;
28762879
int num = list_get(&pn_body, PN_suite_block_stmts, &nodes);
28772880

2881+
/*
28782882
if (comp->pass == PASS_3) {
28792883
//printf("----\n");
28802884
scope_print_info(scope);
28812885
}
2886+
*/
28822887

28832888
for (int i = 0; i < num; i++) {
28842889
assert(MP_PARSE_NODE_IS_STRUCT(nodes[i]));
@@ -3028,6 +3033,7 @@ void compile_scope_compute_things(compiler_t *comp, scope_t *scope) {
30283033
mp_obj_t mp_compile(mp_parse_node_t pn, qstr source_file, bool is_repl) {
30293034
compiler_t *comp = m_new(compiler_t, 1);
30303035

3036+
comp->source_file = source_file;
30313037
comp->is_repl = is_repl;
30323038
comp->had_error = false;
30333039

@@ -3132,7 +3138,7 @@ mp_obj_t mp_compile(mp_parse_node_t pn, qstr source_file, bool is_repl) {
31323138

31333139
default:
31343140
if (emit_bc == NULL) {
3135-
emit_bc = emit_bc_new(source_file, max_num_labels);
3141+
emit_bc = emit_bc_new(max_num_labels);
31363142
}
31373143
comp->emit = emit_bc;
31383144
comp->emit_method_table = &emit_bc_method_table;

py/emit.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ extern const emit_method_table_t emit_native_thumb_method_table;
120120
emit_t *emit_pass1_new(qstr qstr___class__);
121121
void emit_pass1_free(emit_t *emit);
122122
emit_t *emit_cpython_new(uint max_num_labels);
123-
emit_t *emit_bc_new(qstr source_file, uint max_num_labels);
123+
emit_t *emit_bc_new(uint max_num_labels);
124124
emit_t *emit_native_x64_new(uint max_num_labels);
125125
emit_t *emit_native_thumb_new(uint max_num_labels);
126126

py/emitbc.c

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ struct _emit_t {
2121

2222
scope_t *scope;
2323

24-
qstr source_file;
2524
uint last_source_line_offset;
2625
uint last_source_line;
2726

@@ -36,9 +35,8 @@ struct _emit_t {
3635
byte dummy_data[8];
3736
};
3837

39-
emit_t *emit_bc_new(qstr source_file, uint max_num_labels) {
38+
emit_t *emit_bc_new(uint max_num_labels) {
4039
emit_t *emit = m_new0(emit_t, 1);
41-
emit->source_file = source_file;
4240
emit->max_num_labels = max_num_labels;
4341
emit->label_offsets = m_new(uint, emit->max_num_labels);
4442
return emit;
@@ -187,7 +185,8 @@ static void emit_bc_start_pass(emit_t *emit, pass_kind_t pass, scope_t *scope) {
187185
}
188186

189187
// code info
190-
emit_write_code_info_qstr(emit, emit->source_file);
188+
emit_write_code_info_qstr(emit, scope->source_file);
189+
emit_write_code_info_qstr(emit, scope->simple_name);
191190

192191
// prelude for initialising closed over variables
193192
int num_cell = 0;
@@ -239,6 +238,7 @@ static void emit_bc_set_stack_size(emit_t *emit, int size) {
239238
}
240239

241240
static void emit_bc_set_source_line(emit_t *emit, int source_line) {
241+
//printf("source: line %d -> %d offset %d -> %d\n", emit->last_source_line, source_line, emit->last_source_line_offset, emit->byte_code_offset);
242242
if (source_line > emit->last_source_line) {
243243
int bytes_to_skip = emit->byte_code_offset - emit->last_source_line_offset;
244244
for (; bytes_to_skip > 255; bytes_to_skip -= 255) {
@@ -249,6 +249,7 @@ static void emit_bc_set_source_line(emit_t *emit, int source_line) {
249249
emit_write_code_info_byte_byte(emit, 0, 255);
250250
}
251251
emit_write_code_info_byte_byte(emit, bytes_to_skip, lines_to_skip);
252+
//printf(" %d %d\n", bytes_to_skip, lines_to_skip);
252253
emit->last_source_line_offset = emit->byte_code_offset;
253254
emit->last_source_line = source_line;
254255
}

py/mpconfig.h

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,11 @@
3939
#define MICROPY_MEM_STATS (0)
4040
#endif
4141

42-
// Whether to build code to show byte code
43-
#ifndef MICROPY_SHOW_BC
44-
#define MICROPY_SHOW_BC (0)
42+
// Whether to build functions that print debugging info:
43+
// mp_byte_code_print
44+
// mp_parse_node_print
45+
#ifndef MICROPY_DEBUG_PRINTERS
46+
#define MICROPY_DEBUG_PRINTERS (0)
4547
#endif
4648

4749
/*****************************************************************************/

py/obj.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -273,8 +273,8 @@ machine_int_t mp_obj_int_get_checked(mp_obj_t self_in);
273273
// exception
274274
extern const mp_obj_type_t exception_type;
275275
qstr mp_obj_exception_get_type(mp_obj_t self_in);
276-
void mp_obj_exception_set_source_info(mp_obj_t self_in, qstr file, machine_uint_t line);
277-
void mp_obj_exception_get_source_info(mp_obj_t self_in, qstr *file, machine_uint_t *line);
276+
void mp_obj_exception_set_source_info(mp_obj_t self_in, qstr file, machine_uint_t line, qstr block);
277+
void mp_obj_exception_get_source_info(mp_obj_t self_in, qstr *file, machine_uint_t *line, qstr *block);
278278

279279
// str
280280
extern const mp_obj_type_t str_type;

py/objexcept.c

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ typedef struct mp_obj_exception_t {
1919
mp_obj_base_t base;
2020
qstr source_file;
2121
machine_uint_t source_line;
22+
qstr source_block;
2223
qstr id;
2324
qstr msg;
2425
mp_obj_tuple_t args;
@@ -114,7 +115,7 @@ qstr mp_obj_exception_get_type(mp_obj_t self_in) {
114115
return self->id;
115116
}
116117

117-
void mp_obj_exception_set_source_info(mp_obj_t self_in, qstr file, machine_uint_t line) {
118+
void mp_obj_exception_set_source_info(mp_obj_t self_in, qstr file, machine_uint_t line, qstr block) {
118119
assert(MP_OBJ_IS_TYPE(self_in, &exception_type));
119120
mp_obj_exception_t *self = self_in;
120121
// TODO make a list of file/line pairs for the traceback
@@ -125,11 +126,15 @@ void mp_obj_exception_set_source_info(mp_obj_t self_in, qstr file, machine_uint_
125126
if (line != 0 && self->source_line == 0) {
126127
self->source_line = line;
127128
}
129+
if (block != 0 && self->source_block == 0) {
130+
self->source_block = block;
131+
}
128132
}
129133

130-
void mp_obj_exception_get_source_info(mp_obj_t self_in, qstr *file, machine_uint_t *line) {
134+
void mp_obj_exception_get_source_info(mp_obj_t self_in, qstr *file, machine_uint_t *line, qstr *block) {
131135
assert(MP_OBJ_IS_TYPE(self_in, &exception_type));
132136
mp_obj_exception_t *self = self_in;
133137
*file = self->source_file;
134138
*line = self->source_line;
139+
*block = self->source_block;
135140
}

py/parse.c

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,8 @@ mp_parse_node_struct_t *parse_node_new_struct(int src_line, int rule_id, int num
135135
return pn;
136136
}
137137

138-
void mp_parse_node_show(mp_parse_node_t pn, int indent) {
138+
#if MICROPY_DEBUG_PRINTERS
139+
void mp_parse_node_print(mp_parse_node_t pn, int indent) {
139140
if (MP_PARSE_NODE_IS_STRUCT(pn)) {
140141
printf("[% 4d] ", (int)((mp_parse_node_struct_t*)pn)->source_line);
141142
} else {
@@ -167,16 +168,17 @@ void mp_parse_node_show(mp_parse_node_t pn, int indent) {
167168
printf("rule(%u) (n=%d)\n", (uint)MP_PARSE_NODE_STRUCT_KIND(pns2), n);
168169
#endif
169170
for (int i = 0; i < n; i++) {
170-
mp_parse_node_show(pns2->nodes[i], indent + 2);
171+
mp_parse_node_print(pns2->nodes[i], indent + 2);
171172
}
172173
}
173174
}
175+
#endif // MICROPY_DEBUG_PRINTERS
174176

175177
/*
176178
static void result_stack_show(parser_t *parser) {
177179
printf("result stack, most recent first\n");
178180
for (int i = parser->result_stack_top - 1; i >= 0; i--) {
179-
mp_parse_node_show(parser->result_stack[i], 0);
181+
mp_parse_node_print(parser->result_stack[i], 0);
180182
}
181183
}
182184
*/

py/parse.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ typedef struct _mp_parse_node_struct_t {
5454

5555
mp_parse_node_t mp_parse_node_new_leaf(machine_int_t kind, machine_int_t arg);
5656

57-
void mp_parse_node_show(mp_parse_node_t pn, int indent);
57+
void mp_parse_node_print(mp_parse_node_t pn, int indent);
5858

5959
typedef enum {
6060
MP_PARSE_SINGLE_INPUT,

py/runtime.c

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -215,9 +215,8 @@ void rt_assign_byte_code(int unique_code_id, byte *code, uint len, int n_args, i
215215
DEBUG_printf(" %02x", code[i]);
216216
}
217217
DEBUG_printf("\n");
218-
#if MICROPY_SHOW_BC
219-
extern void mp_show_byte_code(const byte *code, int len);
220-
mp_show_byte_code(code, len);
218+
#if MICROPY_DEBUG_PRINTERS
219+
mp_byte_code_print(code, len);
221220
#endif
222221

223222
#ifdef WRITE_CODE

0 commit comments

Comments
 (0)