Skip to content

Commit 6607c3b

Browse files
theotherjimmyadbridge
authored andcommitted
Format memap
1 parent 2e5fd74 commit 6607c3b

File tree

1 file changed

+13
-54
lines changed

1 file changed

+13
-54
lines changed

tools/memap.py

Lines changed: 13 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -50,12 +50,11 @@ class MemapParser(object):
5050
sections = ('.text', '.data', '.bss', '.heap', '.stack')
5151

5252
def __init__(self):
53-
""" General initialization
54-
"""
55-
5653
# list of all modules and their sections
57-
self.modules = dict() # full list - doesn't change with depth
58-
self.short_modules = dict() # short version with specific depth
54+
# full list - doesn't change with depth
55+
self.modules = dict()
56+
# short version with specific depth
57+
self.short_modules = dict()
5958

6059
# sections must be defined in this order to take irrelevant out
6160
self.all_sections = self.sections + self.other_sections + \
@@ -64,27 +63,27 @@ def __init__(self):
6463
# Memory report (sections + summary)
6564
self.mem_report = []
6665

67-
# Just the memory summary section
66+
# Memory summary
6867
self.mem_summary = dict()
6968

69+
# Totals of ".text", ".data" and ".bss"
7070
self.subtotal = dict()
7171

72+
# Flash no associated with a module
7273
self.misc_flash_mem = 0
7374

7475
# Modules passed to the linker on the command line
7576
# this is a dict because modules are looked up by their basename
7677
self.cmd_modules = {}
7778

78-
7979
def module_add(self, object_name, size, section):
80-
""" Adds a module / section to the list
80+
""" Adds a module or section to the list
8181
8282
Positional arguments:
8383
object_name - name of the entry to add
8484
size - the size of the module being added
8585
section - the section the module contributes to
8686
"""
87-
8887
if not object_name or not size or not section:
8988
return
9089

@@ -117,7 +116,6 @@ def check_new_section_gcc(self, line):
117116
Positional arguments:
118117
line - the line to check for a new section
119118
"""
120-
121119
for i in self.all_sections:
122120
if line.startswith(i):
123121
# should name of the section (assuming it's a known one)
@@ -135,11 +133,9 @@ def parse_object_name_gcc(self, line):
135133
Positional arguments:
136134
txt - the path to parse the object and module name from
137135
"""
138-
139136
test_re_mbed_os_name = re.match(RE_OBJECT_FILE_GCC, line)
140137

141138
if test_re_mbed_os_name:
142-
143139
object_name = test_re_mbed_os_name.group(1)
144140

145141
# corner case: certain objects are provided by the GCC toolchain
@@ -148,15 +144,12 @@ def parse_object_name_gcc(self, line):
148144
return object_name
149145

150146
else:
151-
152147
test_re_obj_name = re.match(RE_LIBRARY_OBJECT_GCC, line)
153148

154149
if test_re_obj_name:
155150
object_name = os.path.join(test_re_obj_name.group(1),
156151
test_re_obj_name.group(2))
157-
158152
return os.path.join('[lib]', object_name)
159-
160153
else:
161154
print "Unknown object name found in GCC map file: %s" % line
162155
return '[misc]'
@@ -171,7 +164,6 @@ def parse_section_gcc(self, line):
171164
Positional arguments:
172165
line - the line to parse a section from
173166
"""
174-
175167
is_fill = re.match(RE_FILL_SECTION_GCC, line)
176168
if is_fill:
177169
o_name = '[fill]'
@@ -193,7 +185,6 @@ def parse_map_file_gcc(self, file_desc):
193185
Positional arguments:
194186
file_desc - a stream object to parse as a gcc map file
195187
"""
196-
197188
current_section = 'unknown'
198189

199190
with file_desc as infile:
@@ -211,7 +202,6 @@ def parse_map_file_gcc(self, file_desc):
211202
current_section = next_section
212203

213204
object_name, object_size = self.parse_section_gcc(line)
214-
215205
self.module_add(object_name, object_size, current_section)
216206

217207
common_prefix = os.path.dirname(os.path.commonprefix([
@@ -232,9 +222,7 @@ def parse_object_name_armcc(self, line):
232222
Positional arguments:
233223
line - the line containing the object or library
234224
"""
235-
236-
# simple object (not library)
237-
if line[-2] == '.' and line[-1] == 'o':
225+
if line.endswith(".o"):
238226
return line
239227

240228
else:
@@ -247,8 +235,6 @@ def parse_object_name_armcc(self, line):
247235
print "Malformed input found when parsing ARMCC map: %s" % line
248236
return '[misc]'
249237

250-
251-
252238
def parse_section_armcc(self, line):
253239
""" Parse data from an armcc map file
254240
@@ -260,11 +246,9 @@ def parse_section_armcc(self, line):
260246
Positional arguments:
261247
line - the line to parse the section data from
262248
"""
263-
264249
test_re_armcc = re.match(RE_ARMCC, line)
265250

266251
if test_re_armcc:
267-
268252
size = int(test_re_armcc.group(2), 16)
269253

270254
if test_re_armcc.group(4) == 'RO':
@@ -283,7 +267,7 @@ def parse_section_armcc(self, line):
283267
return ["", 0, ""]
284268

285269
# check name of object or library
286-
object_name = self.parse_object_name_armcc(\
270+
object_name = self.parse_object_name_armcc(
287271
test_re_armcc.group(6))
288272

289273
return [object_name, size, section]
@@ -297,8 +281,6 @@ def parse_object_name_iar(self, object_name):
297281
Positional arguments:
298282
line - the line containing the object or library
299283
"""
300-
301-
# simple object (not library)
302284
if object_name.endswith(".o"):
303285
try:
304286
return self.cmd_modules[object_name]
@@ -307,7 +289,6 @@ def parse_object_name_iar(self, object_name):
307289
else:
308290
return '[misc]'
309291

310-
311292
def parse_section_iar(self, line):
312293
""" Parse data from an IAR map file
313294
@@ -325,13 +306,8 @@ def parse_section_iar(self, line):
325306
Positional_arguments:
326307
line - the line to parse section data from
327308
"""
328-
329309
test_re_iar = re.match(RE_IAR, line)
330-
331310
if test_re_iar:
332-
333-
size = int(test_re_iar.group(4), 16)
334-
335311
if (test_re_iar.group(2) == 'const' or
336312
test_re_iar.group(2) == 'ro code'):
337313
section = '.text'
@@ -348,24 +324,24 @@ def parse_section_iar(self, line):
348324
section = '.data'
349325
else:
350326
print "Malformed input found when parsing IAR map: %s" % line
327+
return ["", 0, ""]
351328

352329
# lookup object in dictionary and return module name
353330
object_name = self.parse_object_name_iar(test_re_iar.group(5))
354331

332+
size = int(test_re_iar.group(4), 16)
355333
return [object_name, size, section]
356334

357335
else:
358-
return ["", 0, ""] # no valid entry
336+
return ["", 0, ""]
359337

360338
def parse_map_file_armcc(self, file_desc):
361339
""" Main logic to decode armc5 map files
362340
363341
Positional arguments:
364342
file_desc - a file like object to parse as an armc5 map file
365343
"""
366-
367344
with file_desc as infile:
368-
369345
# Search area to parse
370346
for line in infile:
371347
if line.startswith(' Base Addr Size'):
@@ -387,18 +363,13 @@ def parse_map_file_armcc(self, file_desc):
387363
new_modules[name] = stats
388364
self.modules = new_modules
389365

390-
391-
392366
def check_new_library_iar(self, line):
393367
"""
394368
Searches for libraries and returns name. Example:
395369
m7M_tls.a: [43]
396370
397371
"""
398-
399-
400372
test_address_line = re.match(RE_LIBRARY_IAR, line)
401-
402373
if test_address_line:
403374
return test_address_line.group(1)
404375
else:
@@ -415,9 +386,7 @@ def check_new_object_lib_iar(self, line):
415386
I64DivZer.o 2
416387
417388
"""
418-
419389
test_address_line = re.match(RE_OBJECT_LIBRARY_IAR, line)
420-
421390
if test_address_line:
422391
return test_address_line.group(1)
423392
else:
@@ -447,7 +416,6 @@ def parse_map_file_iar(self, file_desc):
447416
Positional arguments:
448417
file_desc - a file like object to parse as an IAR map file
449418
"""
450-
451419
with file_desc as infile:
452420
self.parse_iar_command_line(infile)
453421

@@ -463,7 +431,6 @@ def parse_map_file_iar(self, file_desc):
463431

464432
current_library = ""
465433
for line in infile:
466-
467434
library = self.check_new_library_iar(line)
468435

469436
if library:
@@ -475,7 +442,6 @@ def parse_map_file_iar(self, file_desc):
475442
temp = os.path.join('[lib]', current_library, object_name)
476443
self.module_replace(object_name, temp)
477444

478-
479445
def reduce_depth(self, depth):
480446
"""
481447
populates the short_modules attribute with a truncated module list
@@ -504,7 +470,6 @@ def reduce_depth(self, depth):
504470
self.short_modules[new_name].setdefault(section_idx, 0)
505471
self.short_modules[new_name][section_idx] += self.modules[module_name][section_idx]
506472

507-
508473
export_formats = ["json", "csv-ci", "table"]
509474

510475
def generate_output(self, export_format, depth, file_output=None):
@@ -519,10 +484,8 @@ def generate_output(self, export_format, depth, file_output=None):
519484
520485
Returns: generated string for the 'table' format, otherwise None
521486
"""
522-
523487
self.reduce_depth(depth)
524488
self.compute_report()
525-
526489
try:
527490
if file_output:
528491
file_desc = open(file_output, 'wb')
@@ -550,7 +513,6 @@ def generate_json(self, file_desc):
550513
"""
551514
file_desc.write(json.dumps(self.mem_report, indent=4))
552515
file_desc.write('\n')
553-
554516
return None
555517

556518
def generate_csv(self, file_desc):
@@ -577,7 +539,6 @@ def generate_csv(self, file_desc):
577539

578540
csv_writer.writerow(csv_module_section)
579541
csv_writer.writerow(csv_sizes)
580-
581542
return None
582543

583544
def generate_table(self, file_desc):
@@ -659,7 +620,6 @@ def parse(self, mapfile, toolchain):
659620
mapfile - the file name of the memory map file
660621
toolchain - the toolchain used to create the file
661622
"""
662-
663623
result = True
664624
try:
665625
with open(mapfile, 'r') as file_input:
@@ -679,7 +639,6 @@ def parse(self, mapfile, toolchain):
679639

680640
def main():
681641
"""Entry Point"""
682-
683642
version = '0.4.0'
684643

685644
# Parser handling

0 commit comments

Comments
 (0)