blob: 43a4ecdf32780f2b97f4f5e3e66ea982277881ae [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()
41 # Filter out Pseudo-op / vex instructions until those are added in insns.dat
Jin Kyu Song088827b2013-08-28 19:15:29 -070042 if not (re.match('vcmp.+[ps][ds]', r) or re.match('vpcmp[^u]+u?[dq]', r)):
Jin Kyu Songf9442f62013-08-20 23:50:26 -070043 d.append(l)
44 d.append(r)
45 recs.append(d)
46 return recs
47
48def commas(recs):
49 replace_tbl = {' PTR':'', '\\':'', 'MM':'', '{':'\{', '}':'\}', 'XWORD':'OWORD'}
50 reccommas = []
51 for insn in recs:
52 new = []
53 byte = '0x' + insn[0].replace(' ', ', 0x')
54 for rep in replace_tbl.keys():
55 insn[1] = insn[1].replace(rep, replace_tbl[rep])
56 mnemonic = insn[1]
57
58 # gas size specifier for gather and scatter insturctions seems wrong. just remove them.
59 if 'gather' in insn[1] or 'scatter' in insn[1]:
60 mnemonic = mnemonic.replace('ZWORD', '')
61
62 new.append(byte)
63 new.append(mnemonic)
64 reccommas.append(new)
65 return reccommas
66
67# The spaces reserved here can be adjusted according to the output string length.
68# maxlen printed out at the end of the process will give a hint for it.
69outstrfmt = "testcase\t{ %-70s }, { %-60s }\n"
70
71macro = "%macro testcase 2\n %ifdef BIN\n db %1\n %endif\n %ifdef SRC\n %2\n %endif\n%endmacro\n\n\n"
72
73def write(data, options):
74 if options.output:
75 with open(options.output, 'wb') as out:
76 out.write(macro)
77 if options.bits:
78 out.write('bits ' + options.bits + '\n\n')
79 for insn in data:
80 outstr = outstrfmt % tuple(insn)
81 out.write(outstr)
82
Jin Kyu Songfe0ee082013-08-23 18:40:49 -070083def write_rawbytes(data, options):
84 if options.raw_output:
85 with open(options.raw_output, 'wb') as out:
86 for insn in data:
87 out.write(insn[0] + '\n')
88
Jin Kyu Songf9442f62013-08-20 23:50:26 -070089if __name__ == "__main__":
90 options = setup()
91 recs = read(options)
Jin Kyu Songf9442f62013-08-20 23:50:26 -070092
Jin Kyu Songfe0ee082013-08-23 18:40:49 -070093 write_rawbytes(recs, options)
94
Jin Kyu Songf9442f62013-08-20 23:50:26 -070095 recs = commas(recs)
96
97 write(recs, options)
98
99 maxlen = [0,0,0,0,0,0,0,0]
100 for insn in recs:
101#print insn[0] + '\t<-\t' + insn[1]
102 print outstrfmt[:-1] % tuple(insn)
103 for i, strstr in enumerate(insn):
104 if maxlen[i] < len(strstr): maxlen[i] = len(strstr)
105
106 print maxlen