1 #!/usr/bin/env python -tt
3 # Convert gas testsuite file to NASM test asm file
5 # python gas2nasm.py -i input_gas_file -o output_nasm_file -b bits
6 # e.g. python gas2nasm.py -i x86-64-avx512f-intel.d -o avx512f.asm -b 64
14 parser = optparse.OptionParser()
15 parser.add_option('-i', dest='input', action='store',
17 help='Name for input gas testsuite file.')
18 parser.add_option('-o', dest='output', action='store',
20 help='Name for output NASM test asm file.')
21 parser.add_option('-b', dest='bits', action='store',
23 help='Bits for output ASM file.')
24 parser.add_option('-r', dest='raw_output', action='store',
26 help='Name for raw output bytes in text')
27 (options, args) = parser.parse_args()
31 with open(options.input, 'rb') as f:
36 strr = line[16:].partition(' ')
38 strr = line[16:].partition('\t')
47 replace_tbl = {' PTR':'', '\\':'', 'MM':'', 'XWORD':'OWORD'}
51 byte = '0x' + insn[0].replace(' ', ', 0x')
52 for rep in replace_tbl.keys():
53 insn[1] = insn[1].replace(rep, replace_tbl[rep])
56 # gas size specifier for gather and scatter insturctions seems wrong. just remove them.
57 if 'gather' in insn[1] or 'scatter' in insn[1]:
58 mnemonic = mnemonic.replace('ZWORD', '')
65 # The spaces reserved here can be adjusted according to the output string length.
66 # maxlen printed out at the end of the process will give a hint for it.
67 outstrfmt = "testcase\t{ %-70s }, { %-60s }\n"
69 macro = "%macro testcase 2\n %ifdef BIN\n db %1\n %endif\n %ifdef SRC\n %2\n %endif\n%endmacro\n\n\n"
71 def write(data, options):
73 with open(options.output, 'wb') as out:
76 out.write('bits ' + options.bits + '\n\n')
78 outstr = outstrfmt % tuple(insn)
81 def write_rawbytes(data, options):
82 if options.raw_output:
83 with open(options.raw_output, 'wb') as out:
85 out.write(insn[0] + '\n')
87 if __name__ == "__main__":
91 write_rawbytes(recs, options)
97 maxlen = [0,0,0,0,0,0,0,0]
99 #print insn[0] + '\t<-\t' + insn[1]
100 print outstrfmt[:-1] % tuple(insn)
101 for i, strstr in enumerate(insn):
102 if maxlen[i] < len(strstr): maxlen[i] = len(strstr)