import os
import sys
from xml.dom import minidom
class UdOptableXmlParser:
def parseDef( self, node ):
ven = ''
pfx = []
opc = []
opr = []
for def_node in node.childNodes:
if not def_node.localName:
continue
if def_node.localName == 'pfx':
pfx = def_node.firstChild.data.split();
elif def_node.localName == 'opc':
opc = def_node.firstChild.data.split();
elif def_node.localName == 'opr':
opr = def_node.firstChild.data.split();
elif def_node.localName == 'mode':
pfx.extend( def_node.firstChild.data.split() );
elif def_node.localName == 'syn':
pfx.extend( def_node.firstChild.data.split() );
elif def_node.localName == 'vendor':
ven = ( def_node.firstChild.data );
else:
print "warning: invalid node - %s" % def_node.localName
continue
return ( pfx, opc, opr, ven )
def parse( self, xml, fn ):
xmlDoc = minidom.parse( xml )
self.TlNode = xmlDoc.firstChild
while self.TlNode and self.TlNode.localName != "x86optable":
self.TlNode = self.TlNode.nextSibling
for insnNode in self.TlNode.childNodes:
if not insnNode.localName:
continue
if insnNode.localName != "instruction":
print "warning: invalid insn node - %s" % insnNode.localName
continue
mnemonic = insnNode.getElementsByTagName( 'mnemonic' )[ 0 ].firstChild.data
vendor = ''
for node in insnNode.childNodes:
if node.localName == 'vendor':
vendor = node.firstChild.data
elif node.localName == 'def':
( prefixes, opcodes, operands, local_vendor ) = \
self.parseDef( node )
if ( len( local_vendor ) ):
vendor = local_vendor
fn( prefixes, mnemonic, opcodes, operands, vendor )
def printFn( pfx, mnm, opc, opr, ven ):
print 'def: ',
if len( pfx ):
print ' '.join( pfx ),
print "%s %s %s %s" % \
( mnm, ' '.join( opc ), ' '.join( opr ), ven )
def parse( xml, callback ):
parser = UdOptableXmlParser()
parser.parse( xml, callback )
def main():
parser = UdOptableXmlParser()
parser.parse( sys.argv[ 1 ], printFn )
if __name__ == "__main__":
main()