Skip to content

Instantly share code, notes, and snippets.

@vegard
Created November 14, 2013 21:18
Show Gist options
  • Save vegard/7474502 to your computer and use it in GitHub Desktop.
Save vegard/7474502 to your computer and use it in GitHub Desktop.
Script for converting udis86 optable.xml to YAML
import sys
from lxml import etree
parser = etree.XMLParser(remove_comments=False)
tree = etree.parse('docs/x86/optable.xml', parser=parser)
root = tree.getroot()
for e in root:
if e.tag == 'instruction':
mnemonics = [m for m in e.iterchildren(tag='mnemonic')]
assert len(mnemonics) == 1
mnemonic = mnemonics[0].text
print "%s:" % mnemonic
for d in e.iterchildren(tag='def'):
start = "-"
vendor = [x for x in d.iterchildren(tag='vendor')] or [x for x in e.iterchildren(tag='vendor')]
assert len(vendor) <= 1
if vendor:
print " %s vendor: %s" % (start, vendor[0].text)
start = " "
pfxs = [x for x in d.iterchildren(tag='pfx')]
assert len(pfxs) <= 1
if len(pfxs) == 1:
print " %s pfx: %s" % (start, pfxs[0].text)
start = " "
opc = [x for x in d.iterchildren(tag='opc')]
assert len(opc) == 1
#print " %s opc: \"%s\"" % (start, opc[0].text)
print " %s opc: %s" % (start, opc[0].text)
start = " "
oprs = [x for x in d.iterchildren(tag='opr')]
if len(oprs) >= 2:
print >>sys.stderr, "warning: opcode %s has more than one set of operands" % mnemonic
if len(oprs) >= 1:
print " %s opr: %s" % (start, oprs[-1].text)
start = " "
modes = [x for x in d.iterchildren(tag='mode')]
assert len(modes) <= 1
if len(modes) == 1:
print " %s mode: %s" % (start, modes[0].text)
start = " "
syns = [x for x in d.iterchildren(tag='syn')]
assert len(syns) <= 1
if len(syns) == 1:
print " %s syn: %s" % (start, syns[0].text)
start = " "
cpuid = [x for x in d.iterchildren(tag='cpuid')] or [x for x in e.iterchildren(tag='cpuid')]
assert len(cpuid) <= 1, "%s cpuids: %s" % (mnemonic, ", ".join([x.text for x in cpuid]))
if cpuid:
print " cpuid: %s" % cpuid[0].text
elif e.tag is etree.Comment:
for line in e.text.splitlines():
print "# %s" % line
else:
raise "Unhandled tag"
print
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment