1 #! /usr/bin/env python3
3 # Generate test-avx.h from x86.csv
7 from fnmatch
import fnmatch
9 ignore
= set(["EMMS", "FEMMS", "FISTTP",
10 "LDMXCSR", "VLDMXCSR", "STMXCSR", "VSTMXCSR"])
24 'PS[LR][AL][WDQ]': 0x3f,
27 def strip_comments(x
):
29 if l
!= '' and l
[0] != '#':
41 raise Exception("bad reg_w %d" % w
)
55 return t
+ " PTR 32[rdx]"
60 def __init__(self
, mw
):
61 if mw
not in [0, 32, 64]:
62 raise Exception("Bad /m width: %s" % w
)
71 def match(op
, pattern
):
72 return fnmatch(op
, pattern
)
77 def __init__(self
, op
):
78 for k
, v
in imask
.items():
82 raise Exception("Unknown immediate")
89 while (n
& ~mask
) != 0:
95 def __init__(self
, rw
, mw
):
96 if rw
not in [8, 16, 32, 64]:
97 raise Exception("Bad r/w width: %s" % w
)
98 if mw
not in [0, 8, 16, 32, 64]:
99 raise Exception("Bad r/w width: %s" % w
)
105 return mem_w(self
.mw
)
107 return reg_w(self
.rw
)
112 def __init__(self
, w
):
113 if w
not in [8, 16, 32, 64, 128, 256]:
114 raise Exception("Bad mem width: %s" % w
)
119 class SkipInstruction(Exception):
122 def ArgGenerator(arg
, op
):
125 r
, m
= arg
.split('/')
127 raise Exception("Expected /m: %s", arg
)
128 return MMArg(int(m
[1:]));
131 elif arg
[:4] == 'imm8':
135 r
, m
= arg
.split('/')
137 raise Exception("Expected /m: %s", arg
)
145 return ArgRM(int(arg
[1:]), 0);
147 return ArgMem(int(arg
[1:]))
149 raise SkipInstruction
152 def __init__(self
, op
, args
):
160 self
.args
= list(ArgGenerator(a
, op
) for a
in args
)
161 if len(self
.args
) > 0 and self
.args
[-1] is None:
162 self
.args
= self
.args
[:-1]
163 except SkipInstruction
:
165 except Exception as e
:
166 raise Exception("Bad arg %s: %s" % (op
, e
))
172 nreg
= len(self
.args
)
176 if isinstance(self
.args
[-1], ArgImm8u
):
178 immarg
= self
.args
[-1]
182 for n
, arg
in enumerate(self
.args
):
187 regset
= [(regs
[0],)]
196 regset
+= [(-1, regs
[0])]
198 regset
+= [(dest
, -1)]
200 raise Exception("Too many regs: %s(%d)" % (self
.op
, nreg
))
204 for i
in range(nreg
):
206 argstr
.append(arg
.regstr(regv
[i
]))
208 yield self
.op
+ ' ' + ','.join(argstr
)
210 for immval
in immarg
.vals():
211 yield self
.op
+ ' ' + ','.join(argstr
) + ',' + str(immval
)
220 if len(sys
.argv
) <= 3:
221 print("Usage: test-mmx.py x86.csv test-mmx.h CPUID...")
223 csvfile
= open(sys
.argv
[1], 'r', newline
='')
225 with
open(sys
.argv
[2], "w") as outf
:
226 outf
.write("// Generated by test-mmx.py. Do not edit.\n")
227 for row
in csv
.reader(strip_comments(csvfile
)):
228 insn
= row
[0].replace(',', '').split()
229 if insn
[0] in ignore
:
234 g
= InsnGenerator(insn
[0], insn
[1:])
236 outf
.write('TEST(%d, "%s", %s)\n' % (n
, insn
, g
.optype
))
238 except SkipInstruction
:
240 outf
.write("#undef TEST\n")
243 if __name__
== "__main__":