blob: de1674565f3f1c2e7749b7f8873f52f378ebece3 [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.')
24 (options, args) = parser.parse_args()
25 return options
26
27def read(options):
28 with open(options.input, 'rb') as f:
29 recs = []
30 for line in f:
31 if line[0] == '[':
32 d = []
33 strr = line[16:].partition(' ')
34 if strr[1] == '':
35 strr = line[16:].partition('\t')
36 l = strr[0].strip()
37 r = strr[2].strip()
38 # Filter out Pseudo-op / vex instructions until those are added in insns.dat
39 if not (re.match('vcmp.+[ps][ds]', r) or re.match('vpcmp[^u]+u?[dq]', r) or r[0] == 'k'):
40 d.append(l)
41 d.append(r)
42 recs.append(d)
43 return recs
44
45def commas(recs):
46 replace_tbl = {' PTR':'', '\\':'', 'MM':'', '{':'\{', '}':'\}', 'XWORD':'OWORD'}
47 reccommas = []
48 for insn in recs:
49 new = []
50 byte = '0x' + insn[0].replace(' ', ', 0x')
51 for rep in replace_tbl.keys():
52 insn[1] = insn[1].replace(rep, replace_tbl[rep])
53 mnemonic = insn[1]
54
55 # gas size specifier for gather and scatter insturctions seems wrong. just remove them.
56 if 'gather' in insn[1] or 'scatter' in insn[1]:
57 mnemonic = mnemonic.replace('ZWORD', '')
58
59 new.append(byte)
60 new.append(mnemonic)
61 reccommas.append(new)
62 return reccommas
63
64# The spaces reserved here can be adjusted according to the output string length.
65# maxlen printed out at the end of the process will give a hint for it.
66outstrfmt = "testcase\t{ %-70s }, { %-60s }\n"
67
68macro = "%macro testcase 2\n %ifdef BIN\n db %1\n %endif\n %ifdef SRC\n %2\n %endif\n%endmacro\n\n\n"
69
70def write(data, options):
71 if options.output:
72 with open(options.output, 'wb') as out:
73 out.write(macro)
74 if options.bits:
75 out.write('bits ' + options.bits + '\n\n')
76 for insn in data:
77 outstr = outstrfmt % tuple(insn)
78 out.write(outstr)
79
80if __name__ == "__main__":
81 options = setup()
82 recs = read(options)
83 print "AVX3.1 instructions"
84
85 recs = commas(recs)
86
87 write(recs, options)
88
89 maxlen = [0,0,0,0,0,0,0,0]
90 for insn in recs:
91#print insn[0] + '\t<-\t' + insn[1]
92 print outstrfmt[:-1] % tuple(insn)
93 for i, strstr in enumerate(insn):
94 if maxlen[i] < len(strstr): maxlen[i] = len(strstr)
95
96 print maxlen