tools/mpy-tool.py: Improve generated frozen identifiers.

Frozen identifiers now include their full name hierarchy, eg their class
name.  This makes it easier to understand the generated code.

Signed-off-by: Damien George <damien@micropython.org>
This commit is contained in:
Damien George 2022-06-07 15:00:00 +10:00
parent 1d143cec63
commit 2fb413b265

View File

@ -849,7 +849,7 @@ class RawCode(object):
MP_CODE_NATIVE_ASM: "MP_CODE_NATIVE_ASM", MP_CODE_NATIVE_ASM: "MP_CODE_NATIVE_ASM",
} }
def __init__(self, cm_escaped_name, qstr_table, fun_data, prelude_offset, code_kind): def __init__(self, parent_name, qstr_table, fun_data, prelude_offset, code_kind):
self.qstr_table = qstr_table self.qstr_table = qstr_table
self.fun_data = fun_data self.fun_data = fun_data
self.prelude_offset = prelude_offset self.prelude_offset = prelude_offset
@ -872,7 +872,7 @@ class RawCode(object):
else: else:
self.simple_name = self.qstr_table[0] self.simple_name = self.qstr_table[0]
escaped_name = cm_escaped_name + "_" + self.simple_name.qstr_esc escaped_name = parent_name + "_" + self.simple_name.qstr_esc
# make sure the escaped name is unique # make sure the escaped name is unique
i = 2 i = 2
@ -956,10 +956,10 @@ class RawCode(object):
class RawCodeBytecode(RawCode): class RawCodeBytecode(RawCode):
def __init__(self, cm_escaped_name, qstr_table, obj_table, fun_data): def __init__(self, parent_name, qstr_table, obj_table, fun_data):
self.obj_table = obj_table self.obj_table = obj_table
super(RawCodeBytecode, self).__init__( super(RawCodeBytecode, self).__init__(
cm_escaped_name, qstr_table, fun_data, 0, MP_CODE_BYTECODE parent_name, qstr_table, fun_data, 0, MP_CODE_BYTECODE
) )
def disassemble(self): def disassemble(self):
@ -1035,7 +1035,7 @@ class RawCodeBytecode(RawCode):
class RawCodeNative(RawCode): class RawCodeNative(RawCode):
def __init__( def __init__(
self, self,
cm_escaped_name, parent_name,
qstr_table, qstr_table,
kind, kind,
fun_data, fun_data,
@ -1045,7 +1045,7 @@ class RawCodeNative(RawCode):
type_sig, type_sig,
): ):
super(RawCodeNative, self).__init__( super(RawCodeNative, self).__init__(
cm_escaped_name, qstr_table, fun_data, prelude_offset, kind parent_name, qstr_table, fun_data, prelude_offset, kind
) )
if kind in (MP_CODE_NATIVE_VIPER, MP_CODE_NATIVE_ASM): if kind in (MP_CODE_NATIVE_VIPER, MP_CODE_NATIVE_ASM):
@ -1241,7 +1241,7 @@ def read_obj(reader, segments):
return obj return obj
def read_raw_code(reader, cm_escaped_name, qstr_table, obj_table, segments): def read_raw_code(reader, parent_name, qstr_table, obj_table, segments):
# Read raw code header. # Read raw code header.
kind_len = reader.read_uint() kind_len = reader.read_uint()
kind = (kind_len & 3) + MP_CODE_BYTECODE kind = (kind_len & 3) + MP_CODE_BYTECODE
@ -1255,7 +1255,7 @@ def read_raw_code(reader, cm_escaped_name, qstr_table, obj_table, segments):
if kind == MP_CODE_BYTECODE: if kind == MP_CODE_BYTECODE:
# Create bytecode raw code. # Create bytecode raw code.
rc = RawCodeBytecode(cm_escaped_name, qstr_table, obj_table, fun_data) rc = RawCodeBytecode(parent_name, qstr_table, obj_table, fun_data)
else: else:
# Create native raw code. # Create native raw code.
native_scope_flags = 0 native_scope_flags = 0
@ -1290,7 +1290,7 @@ def read_raw_code(reader, cm_escaped_name, qstr_table, obj_table, segments):
native_type_sig = reader.read_uint() native_type_sig = reader.read_uint()
rc = RawCodeNative( rc = RawCodeNative(
cm_escaped_name, parent_name,
qstr_table, qstr_table,
kind, kind,
fun_data, fun_data,
@ -1309,11 +1309,14 @@ def read_raw_code(reader, cm_escaped_name, qstr_table, obj_table, segments):
# Read children, if there are any. # Read children, if there are any.
rc.children = [] rc.children = []
if has_children: if has_children:
# Make a pretty parent name (otherwise all identifiers will include _lt_module_gt_).
if not rc.escaped_name.endswith("_lt_module_gt_"):
parent_name = rc.escaped_name
# Read all the child raw codes.
n_children = reader.read_uint() n_children = reader.read_uint()
for _ in range(n_children): for _ in range(n_children):
rc.children.append( rc.children.append(read_raw_code(reader, parent_name, qstr_table, obj_table, segments))
read_raw_code(reader, cm_escaped_name, qstr_table, obj_table, segments)
)
return rc return rc