Commit 52b5d76a authored by Damien George's avatar Damien George
Browse files

py: Free non-interned strings in the parser when not needed.

mp_parse_node_free now frees the memory associated with non-interned
strings.  And the parser calls mp_parse_node_free when discarding a
non-used node (such as a doc string).

Also, the compiler now frees the parse tree explicitly just before it
exits (as opposed to relying on the caller to do this).

Addresses issue #708 as best we can.
parent d6230f62
......@@ -35,7 +35,6 @@ void do_str(const char *src) {
qstr source_name = mp_lexer_source_name(lex);
mp_lexer_free(lex);
mp_obj_t module_fun = mp_compile(pn, source_name, MP_EMIT_OPT_NONE, true);
mp_parse_node_free(pn);
if (module_fun == mp_const_none) {
// compile error
......
......@@ -63,7 +63,6 @@ STATIC mp_obj_t parse_compile_execute(mp_obj_t o_in, mp_parse_input_kind_t parse
// compile the string
mp_obj_t module_fun = mp_compile(pn, source_name, MP_EMIT_OPT_NONE, false);
mp_parse_node_free(pn);
if (module_fun == mp_const_none) {
// TODO handle compile error correctly
......
......@@ -141,7 +141,6 @@ STATIC void do_load(mp_obj_t module_obj, vstr_t *file) {
// compile the imported script
mp_obj_t module_fun = mp_compile(pn, source_name, MP_EMIT_OPT_NONE, false);
mp_parse_node_free(pn);
if (module_fun == mp_const_none) {
// TODO handle compile error correctly
......
......@@ -3710,6 +3710,9 @@ mp_obj_t mp_compile(mp_parse_node_t pn, qstr source_file, uint emit_opt, bool is
#endif
#endif // !MICROPY_EMIT_CPYTHON
// free the parse tree
mp_parse_node_free(pn);
// free the scopes
mp_raw_code_t *outer_raw_code = module_scope->raw_code;
for (scope_t *s = module_scope; s;) {
......
......@@ -33,4 +33,5 @@ enum {
MP_EMIT_OPT_ASM_THUMB,
};
// the compiler will free the parse tree (pn) before it returns
mp_obj_t mp_compile(mp_parse_node_t pn, qstr source_file, uint emit_opt, bool is_repl);
......@@ -179,6 +179,7 @@ void mp_parse_node_free(mp_parse_node_t pn) {
mp_uint_t n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
mp_uint_t rule_id = MP_PARSE_NODE_STRUCT_KIND(pns);
if (rule_id == RULE_string) {
m_del(char, (char*)pns->nodes[0], (mp_uint_t)pns->nodes[1]);
return;
}
bool adjust = ADD_BLANK_NODE(rule_id);
......@@ -562,8 +563,8 @@ mp_parse_node_t mp_parse(mp_lexer_t *lex, mp_parse_input_kind_t input_kind, mp_p
if (input_kind != MP_PARSE_SINGLE_INPUT && rule->rule_id == RULE_expr_stmt && peek_result(&parser, 0) == MP_PARSE_NODE_NULL) {
mp_parse_node_t p = peek_result(&parser, 1);
if ((MP_PARSE_NODE_IS_LEAF(p) && !MP_PARSE_NODE_IS_ID(p)) || MP_PARSE_NODE_IS_STRUCT_KIND(p, RULE_string)) {
pop_result(&parser);
pop_result(&parser);
pop_result(&parser); // MP_PARSE_NODE_NULL
mp_parse_node_free(pop_result(&parser)); // RULE_string
push_result_rule(&parser, rule_src_line, rules[RULE_pass_stmt], 0);
break;
}
......
......@@ -35,7 +35,6 @@ void do_str(const char *src) {
qstr source_name = mp_lexer_source_name(lex);
mp_lexer_free(lex);
mp_obj_t module_fun = mp_compile(pn, source_name, MP_EMIT_OPT_NONE, true);
mp_parse_node_free(pn);
if (module_fun == mp_const_none) {
// compile error
......
......@@ -38,7 +38,6 @@ inline void do_str(const char *src) {
qstr source_name = mp_lexer_source_name(lex);
mp_lexer_free(lex);
mp_obj_t module_fun = mp_compile(pn, source_name, MP_EMIT_OPT_NONE, true);
mp_parse_node_free(pn);
if (module_fun == mp_const_none) {
tt_abort_msg("Computer error");
......
......@@ -71,7 +71,6 @@ bool parse_compile_execute(mp_lexer_t *lex, mp_parse_input_kind_t input_kind, bo
mp_lexer_free(lex);
mp_obj_t module_fun = mp_compile(pn, source_name, MP_EMIT_OPT_NONE, is_repl);
mp_parse_node_free(pn);
if (module_fun == mp_const_none) {
return false;
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment