1#!/usr/bin/env python3 2# SPDX-License-Identifier: GPL-2.0-or-later 3 4import os, sys, xml.etree.ElementTree 5 6def writeliteral(indent, bytes): 7 sys.stdout.write(' ' * indent) 8 sys.stdout.write('"') 9 quoted = True 10 11 for c in bytes: 12 if not quoted: 13 sys.stdout.write('\n') 14 sys.stdout.write(' ' * indent) 15 sys.stdout.write('"') 16 quoted = True 17 18 if c == b'"'[0]: 19 sys.stdout.write('\\"') 20 elif c == b'\\'[0]: 21 sys.stdout.write('\\\\') 22 elif c == b'\n'[0]: 23 sys.stdout.write('\\n"') 24 quoted = False 25 elif c >= 32 and c < 127: 26 sys.stdout.write(c.to_bytes(1, 'big').decode()) 27 else: 28 sys.stdout.write(f'\{c:03o}') 29 30 if quoted: 31 sys.stdout.write('"') 32 33sys.stdout.write('#include "qemu/osdep.h"\n' \ 34 '#include "exec/gdbstub.h"\n' \ 35 '\n' 36 'const GDBFeature gdb_static_features[] = {\n') 37 38for input in sys.argv[1:]: 39 with open(input, 'rb') as file: 40 read = file.read() 41 42 parser = xml.etree.ElementTree.XMLPullParser(['start', 'end']) 43 parser.feed(read) 44 events = parser.read_events() 45 event, element = next(events) 46 if event != 'start': 47 sys.stderr.write(f'unexpected event: {event}\n') 48 exit(1) 49 if element.tag != 'feature': 50 sys.stderr.write(f'unexpected start tag: {element.tag}\n') 51 exit(1) 52 53 regnum = 0 54 regnums = [] 55 tags = ['feature'] 56 for event, element in events: 57 if event == 'end': 58 if element.tag != tags[len(tags) - 1]: 59 sys.stderr.write(f'unexpected end tag: {element.tag}\n') 60 exit(1) 61 62 tags.pop() 63 if element.tag == 'feature': 64 break 65 elif event == 'start': 66 if len(tags) < 2 and element.tag == 'reg': 67 if 'regnum' in element.attrib: 68 regnum = int(element.attrib['regnum']) 69 70 regnums.append(regnum) 71 regnum += 1 72 73 tags.append(element.tag) 74 else: 75 raise Exception(f'unexpected event: {event}\n') 76 77 if len(tags): 78 sys.stderr.write('unterminated feature tag\n') 79 exit(1) 80 81 base_reg = min(regnums) 82 num_regs = max(regnums) - base_reg + 1 if len(regnums) else 0 83 84 sys.stdout.write(' {\n') 85 writeliteral(8, bytes(os.path.basename(input), 'utf-8')) 86 sys.stdout.write(',\n') 87 writeliteral(8, read) 88 sys.stdout.write(f',\n {num_regs},\n }},\n') 89 90sys.stdout.write(' { NULL }\n};\n') 91