Cleanup coff-dump.py

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@113926 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Michael J. Spencer 2010-09-15 03:58:24 +00:00
parent c2be84fd49
commit aeb83c4d40

View File

@ -17,14 +17,14 @@ def string_table_entry (offset):
def secname(value): def secname(value):
if value[0] == '/': if value[0] == '/':
return string_table_entry (value [1:].rstrip('\0')) return string_table_entry(value[1:].rstrip('\0'))
else: else:
return '%s' return '%s'
def symname(value): def symname(value):
parts = struct.unpack("<2L", value) parts = struct.unpack("<2L", value)
if parts [0] == 0: if parts[0] == 0:
return string_table_entry (parts [1]) return string_table_entry(parts[1])
else: else:
return '%s' return '%s'
@ -261,16 +261,16 @@ def write(input):
if char == '\n': if char == '\n':
NewLine = True NewLine = True
sys.stdout.write (output) sys.stdout.write(output)
def read(format): def read(format):
return struct.unpack (format, Input.read(struct.calcsize(format))) return struct.unpack(format, Input.read(struct.calcsize(format)))
def read_cstr (): def read_cstr():
output = "" output = ""
while True: while True:
char = Input.read (1) char = Input.read(1)
if len (char) == 0: if len(char) == 0:
raise RuntimeError ("EOF while reading cstr") raise RuntimeError ("EOF while reading cstr")
if char == '\0': if char == '\0':
break break
@ -278,14 +278,14 @@ def read_cstr ():
return output return output
def push_pos(seek_to = None): def push_pos(seek_to = None):
Stack [0:0] = [Input.tell ()] Stack [0:0] = [Input.tell()]
if seek_to: if seek_to:
Input.seek (seek_to) Input.seek(seek_to)
def pop_pos(): def pop_pos():
assert(len (Stack) > 0) assert(len(Stack) > 0)
Input.seek (Stack [0]) Input.seek(Stack[0])
del Stack [0] del Stack[0]
def print_binary_data(size): def print_binary_data(size):
value = "" value = ""
@ -299,14 +299,14 @@ def print_binary_data(size):
value += data value += data
bytes = "" bytes = ""
text = "" text = ""
for index in xrange (16): for index in xrange(16):
if index < len (data): if index < len(data):
if index == 8: if index == 8:
bytes += "- " bytes += "- "
ch = ord (data [index]) ch = ord(data[index])
bytes += "%02X " % ch bytes += "%02X " % ch
if ch >= 0x20 and ch <= 0x7F: if ch >= 0x20 and ch <= 0x7F:
text += data [index] text += data[index]
else: else:
text += "." text += "."
else: else:
@ -314,47 +314,47 @@ def print_binary_data(size):
bytes += " " bytes += " "
bytes += " " bytes += " "
write ("%s|%s|\n" % (bytes, text)) write("%s|%s|\n" % (bytes, text))
return value return value
idlit = re.compile ("[a-zA-Z][a-zA-Z0-9_-]*") idlit = re.compile("[a-zA-Z][a-zA-Z0-9_-]*")
numlit = re.compile ("[0-9]+") numlit = re.compile("[0-9]+")
def read_value(expr): def read_value(expr):
input = iter (expr.split ()) input = iter(expr.split())
def eval(): def eval():
token = input.next () token = input.next()
if expr == 'cstr': if expr == 'cstr':
return read_cstr () return read_cstr()
if expr == 'true': if expr == 'true':
return True return True
if expr == 'false': if expr == 'false':
return False return False
if len (token) > 1 and token [0] in ('=', '@', '<', '!', '>'): if len(token) > 1 and token[0] in ('=', '@', '<', '!', '>'):
val = read(expr) val = read(expr)
assert (len (val) == 1) assert(len(val) == 1)
return val [0] return val[0]
if token == '+': if token == '+':
return eval () + eval () return eval() + eval()
if token == '-': if token == '-':
return eval () - eval () return eval() - eval()
if token == '*': if token == '*':
return eval () * eval () return eval() * eval()
if token == '/': if token == '/':
return eval () / eval () return eval() / eval()
if idlit.match (token): if idlit.match(token):
return Fields [token] return Fields[token]
if numlit.match (token): if numlit.match(token):
return int (token) return int(token)
raise RuntimeError ("unexpected token %s" % repr(token)) raise RuntimeError("unexpected token %s" % repr(token))
value = eval () value = eval ()
@ -365,88 +365,88 @@ def read_value(expr):
raise RuntimeError("unexpected input at end of expression") raise RuntimeError("unexpected input at end of expression")
def write_value(format,value): def write_value(format,value):
format_type = type (format) format_type = type(format)
if format_type is types.StringType: if format_type is types.StringType:
write (format%value) write(format % value)
elif format_type is types.FunctionType: elif format_type is types.FunctionType:
write_value (format (value), value) write_value(format(value), value)
elif format_type is types.TupleType: elif format_type is types.TupleType:
Fields ['this'] = value Fields['this'] = value
handle_element (format) handle_element(format)
else: else:
raise RuntimeError("unexpected type: %s" % repr(format_type)) raise RuntimeError("unexpected type: %s" % repr(format_type))
def handle_scalar(entry): def handle_scalar(entry):
iformat = entry [1] iformat = entry[1]
oformat = entry [2] oformat = entry[2]
value = read_value (iformat) value = read_value(iformat)
write_value (oformat, value) write_value(oformat, value)
return value return value
def handle_enum(entry): def handle_enum(entry):
iformat = entry [1] iformat = entry[1]
oformat = entry [2] oformat = entry[2]
definitions = entry [3] definitions = entry[3]
value = read_value (iformat) value = read_value(iformat)
if type (definitions) is types.TupleType: if type(definitions) is types.TupleType:
selector = read_value (definitions [0]) selector = read_value(definitions[0])
definitions = definitions [1] [selector] definitions = definitions[1][selector]
if value in definitions: if value in definitions:
description = definitions[value] description = definitions[value]
else: else:
description = "unknown" description = "unknown"
write ("%s (" % description) write("%s (" % description)
write_value (oformat, value) write_value(oformat, value)
write (")") write(")")
return value return value
def handle_flags(entry): def handle_flags(entry):
iformat = entry [1] iformat = entry[1]
oformat = entry [2] oformat = entry[2]
definitions = entry [3] definitions = entry[3]
value = read_value (iformat) value = read_value(iformat)
write_value (oformat, value) write_value(oformat, value)
indent () indent()
for entry in definitions: for entry in definitions:
mask = entry [0] mask = entry[0]
name = entry [1] name = entry[1]
if len (entry) == 3: if len (entry) == 3:
map = entry [2] map = entry[2]
selection = value & mask selection = value & mask
if selection in map: if selection in map:
write("\n%s" % map[selection]) write("\n%s" % map[selection])
else: else:
write("\n%s <%d>" % (name, selection)) write("\n%s <%d>" % (name, selection))
elif len (entry) == 2: elif len(entry) == 2:
if value & mask != 0: if value & mask != 0:
write("\n%s" % name) write("\n%s" % name)
dedent () dedent()
return value return value
def handle_struct(entry): def handle_struct(entry):
global Fields global Fields
members = entry [1] members = entry[1]
newFields = {} newFields = {}
write ("{\n"); write("{\n");
indent () indent()
for member in members: for member in members:
name = member [0] name = member[0]
type = member [1] type = member[1]
write("%s = "%name.ljust(24)) write("%s = "%name.ljust(24))
@ -454,90 +454,90 @@ def handle_struct(entry):
write("\n") write("\n")
Fields [name] = value Fields[name] = value
newFields [name] = value newFields[name] = value
dedent () dedent()
write ("}") write("}")
return newFields return newFields
def handle_array(entry): def handle_array(entry):
length = entry [1] length = entry[1]
element = entry [2] element = entry[2]
newItems = [] newItems = []
write ("[\n") write("[\n")
indent () indent()
value = read_value (length) value = read_value(length)
for index in xrange (value): for index in xrange(value):
write ("%d = "%index) write("%d = "%index)
value = handle_element(element) value = handle_element(element)
write ("\n") write("\n")
newItems.append (value) newItems.append(value)
dedent () dedent()
write ("]") write("]")
return newItems return newItems
def handle_byte_array(entry): def handle_byte_array(entry):
length = entry [1] length = entry[1]
element = entry [2] element = entry[2]
newItems = [] newItems = []
write ("[\n") write("[\n")
indent () indent()
value = read_value (length) value = read_value(length)
end_of_array = Input.tell () + value end_of_array = Input.tell() + value
index = 0 index = 0
while Input.tell () < end_of_array: while Input.tell() < end_of_array:
write ("%d = "%index) write("%d = "%index)
value = handle_element(element) value = handle_element(element)
write ("\n") write("\n")
newItems.append (value) newItems.append(value)
index += 1 index += 1
dedent () dedent()
write ("]") write("]")
return newItems return newItems
def handle_ptr(entry): def handle_ptr(entry):
offset = entry[1] offset = entry[1]
element = entry [2] element = entry[2]
value = None value = None
offset = read_value (offset) offset = read_value(offset)
if offset != 0: if offset != 0:
push_pos (offset) push_pos(offset)
value = handle_element (element) value = handle_element(element)
pop_pos () pop_pos()
else: else:
write ("None") write("None")
return value return value
def handle_blob(entry): def handle_blob(entry):
length = entry [1] length = entry[1]
write ("\n") write("\n")
indent () indent()
value = print_binary_data (read_value (length)) value = print_binary_data(read_value(length))
dedent () dedent()
return value return value
@ -553,14 +553,14 @@ def handle_element(entry):
'byte-array': handle_byte_array, 'byte-array': handle_byte_array,
} }
if not entry [0] in handlers: if not entry[0] in handlers:
raise RuntimeError ("unexpected type '%s'" % str (entry[0])) raise RuntimeError ("unexpected type '%s'" % str (entry[0]))
return handlers [entry [0]] (entry) return handlers[entry[0]](entry)
Input = open (sys.argv [1], "rb") Input = open (sys.argv[1], "rb")
try: try:
handle_element (file) handle_element(file)
finally: finally:
Input.close () Input.close()
Input = None Input = None