summaryrefslogtreecommitdiff
path: root/test/gas2nasm.py
blob: 7b67f1bdfd4e37437781956e0d5be4c381f5cc67 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#!/usr/bin/env python -tt
# -*- python -*-
# Convert gas testsuite file to NASM test asm file
# usage >
# python gas2nasm.py -i input_gas_file -o output_nasm_file -b bits
# e.g. python gas2nasm.py -i x86-64-avx512f-intel.d -o avx512f.asm -b 64

import sys
import os
import optparse
import re

def setup():
    parser = optparse.OptionParser()
    parser.add_option('-i', dest='input', action='store',
            default="",
            help='Name for input gas testsuite file.')
    parser.add_option('-o', dest='output', action='store',
            default="",
            help='Name for output NASM test asm file.')
    parser.add_option('-b', dest='bits', action='store',
            default="",
            help='Bits for output ASM file.')
    parser.add_option('-r', dest='raw_output', action='store',
            default="",
            help='Name for raw output bytes in text')
    (options, args) =  parser.parse_args()
    return options

def read(options):
    with open(options.input, 'rb') as f:
        recs = []
        for line in f:
            if line[0] == '[':
                d = []
                strr = line[16:].partition('   ')
                if strr[1] == '':
                    strr = line[16:].partition('\t')
                l = strr[0].strip()
                r = strr[2].strip()
                d.append(l)
                d.append(r)
                recs.append(d)
    return recs

def commas(recs):
    replace_tbl = {' PTR':'', '\\':'', 'MM':'', '{':'\{', '}':'\}', 'XWORD':'OWORD'}
    reccommas = []
    for insn in recs:
        new = []
        byte = '0x' + insn[0].replace(' ', ', 0x')
        for rep in replace_tbl.keys():
            insn[1] = insn[1].replace(rep, replace_tbl[rep])
        mnemonic = insn[1]

        # gas size specifier for gather and scatter insturctions seems wrong. just remove them.
        if 'gather' in insn[1] or 'scatter' in insn[1]:
            mnemonic = mnemonic.replace('ZWORD', '')

        new.append(byte)
        new.append(mnemonic)
        reccommas.append(new)
    return reccommas

# The spaces reserved here can be adjusted according to the output string length.
# maxlen printed out at the end of the process will give a hint for it.
outstrfmt = "testcase\t{ %-70s }, { %-60s }\n"

macro = "%macro testcase 2\n %ifdef BIN\n  db %1\n %endif\n %ifdef SRC\n  %2\n %endif\n%endmacro\n\n\n"

def write(data, options):
    if options.output:
        with open(options.output, 'wb') as out:
            out.write(macro)
            if options.bits:
                out.write('bits ' + options.bits + '\n\n')
            for insn in data:
                outstr = outstrfmt % tuple(insn)
                out.write(outstr)

def write_rawbytes(data, options):
    if options.raw_output:
        with open(options.raw_output, 'wb') as out:
            for insn in data:
                out.write(insn[0] + '\n')

if __name__ == "__main__":
    options = setup()
    recs = read(options)

    write_rawbytes(recs, options)

    recs = commas(recs)

    write(recs, options)

    maxlen = [0,0,0,0,0,0,0,0]
    for insn in recs:
#print insn[0] + '\t<-\t' + insn[1]
        print outstrfmt[:-1] % tuple(insn)
        for i, strstr in enumerate(insn):
            if maxlen[i] < len(strstr): maxlen[i] = len(strstr)

    print maxlen