#!/usr/bin/env python

import sys
import string
import os

if len(sys.argv) > 2:
	sys.exit(1)
elif len(sys.argv) == 2:
	input = os.popen("avr-nm --size-sort --print-size " + sys.argv[1])
else:
	input = sys.stdin.readlines()

# see http://sourceware.org/binutils/docs-2.17/binutils/nm.html
# T  functions (.text)
# D  data (initialized data section)
# B  data (uninitialized data section (.bss))
# C  common symbols (multiple common symbols may appear with the same name)
# R  read only data section
#
# If lowercase, the symbol is local; if uppercase, the symbol is global (external)

# create array which will be filled
libcontent = {'T':[], 'B':[], 'D':[], 'C':[], 'R':[]}

# collect values
for line in input:
	array = line.rstrip('\n').split(' ')
	
	if len(array) != 4:
		continue
	
	# 00000000 00000038 T rccp_read_attribute
	
	size = int(array[1], 16)
	
	# we don't care if the symbol is local or global
	type = str(array[2]).upper()
	name = array[3]
	
	if type in libcontent:
		libcontent[type].append([size, name])
	else:
		print "Type \"%s\" not in List!" % type

# remove duplicatet symbols in the C section
content = []
for x in libcontent['C']:
	if x not in content:
		content.append(x)
libcontent['C'] = content

# calculate size of the diffrent symbols
libsize = dict()
for key in libcontent:
	libcontent[key].sort()
	
	size = 0
	for value in libcontent[key]:
		size += value[0]
	libsize[key] = size
	
	#print "%s: %6d" % (key, size)

# output section size
print "Program: %7d bytes" % (libsize['T'] + libsize['D'] + libsize['C'] + libsize['R'])
print "Data:    %7d bytes" % (libsize['B'] + libsize['D'] + libsize['C'])
	