blob: 7b67f1bdfd4e37437781956e0d5be4c381f5cc67 [file] [log] [blame]
Jin Kyu Songf9442f62013-08-20 23:50:26 -07001#!/usr/bin/env python -tt
2# -*- python -*-
3# Convert gas testsuite file to NASM test asm file
4# usage >
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
7
8import sys
9import os
10import optparse
11import re
12
13def setup():
14 parser = optparse.OptionParser()
15 parser.add_option('-i', dest='input', action='store',
16 default="",
17 help='Name for input gas testsuite file.')
18 parser.add_option('-o', dest='output', action='store',
19 default="",
20 help='Name for output NASM test asm file.')
21 parser.add_option('-b', dest='bits', action='store',
22 default="",
23 help='Bits for output ASM file.')
Jin Kyu Songfe0ee082013-08-23 18:40:49 -070024 parser.add_option('-r', dest='raw_output', action='store',
25 default="",
26 help='Name for raw output bytes in text')
Jin Kyu Songf9442f62013-08-20 23:50:26 -070027 (options, args) = parser.parse_args()
28 return options
29
30def read(options):
31 with open(options.input, 'rb') as f:
32 recs = []
33 for line in f:
34 if line[0] == '[':
35 d = []
36 strr = line[16:].partition(' ')
37 if strr[1] == '':
38 strr = line[16:].partition('\t')
39 l = strr[0].strip()
40 r = strr[2].strip()
Jin Kyu Songc257bb62013-09-06 21:22:18 -070041 d.append(l)
42 d.append(r)
43 recs.append(d)
Jin Kyu Songf9442f62013-08-20 23:50:26 -070044 return recs
45
46def commas(recs):
47 replace_tbl = {' PTR':'', '\\':'', 'MM':'', '{':'\{', '}':'\}', 'XWORD':'OWORD'}
48 reccommas = []
49 for insn in recs:
50 new = []
51 byte = '0x' + insn[0].replace(' ', ', 0x')
52 for rep in replace_tbl.keys():
53 insn[1] = insn[1].replace(rep, replace_tbl[rep])
54 mnemonic = insn[1]
55
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', '')
59
60 new.append(byte)
61 new.append(mnemonic)
62 reccommas.append(new)
63 return reccommas
64
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.
67outstrfmt = "testcase\t{ %-70s }, { %-60s }\n"
68
69macro = "%macro testcase 2\n %ifdef BIN\n db %1\n %endif\n %ifdef SRC\n %2\n %endif\n%endmacro\n\n\n"
70
71def write(data, options):
72 if options.output:
73 with open(options.output, 'wb') as out:
74 out.write(macro)
75 if options.bits:
76 out.write('bits ' + options.bits + '\n\n')
77 for insn in data:
78 outstr = outstrfmt % tuple(insn)
79 out.write(outstr)
80
Jin Kyu Songfe0ee082013-08-23 18:40:49 -070081def write_rawbytes(data, options):
82 if options.raw_output:
83 with open(options.raw_output, 'wb') as out:
84 for insn in data:
85 out.write(insn[0] + '\n')
86
Jin Kyu Songf9442f62013-08-20 23:50:26 -070087if __name__ == "__main__":
88 options = setup()
89 recs = read(options)
Jin Kyu Songf9442f62013-08-20 23:50:26 -070090
Jin Kyu Songfe0ee082013-08-23 18:40:49 -070091 write_rawbytes(recs, options)
92
Jin Kyu Songf9442f62013-08-20 23:50:26 -070093 recs = commas(recs)
94
95 write(recs, options)
96
97 maxlen = [0,0,0,0,0,0,0,0]
98 for insn in recs:
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)
103
104 print maxlen