testcase: Remove escape characters - '\'
[platform/upstream/nasm.git] / test / gas2nasm.py
1 #!/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
8 import sys
9 import os
10 import optparse
11 import re
12
13 def 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     parser.add_option('-r', dest='raw_output', action='store',
25             default="",
26             help='Name for raw output bytes in text')
27     (options, args) =  parser.parse_args()
28     return options
29
30 def 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                 d.append(l)
42                 d.append(r)
43                 recs.append(d)
44     return recs
45
46 def 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.
67 outstrfmt = "testcase\t{ %-70s }, { %-60s }\n"
68
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"
70
71 def 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
81 def 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
87 if __name__ == "__main__":
88     options = setup()
89     recs = read(options)
90
91     write_rawbytes(recs, options)
92
93     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