1 #! /usr/bin/env python3
3 # Generate test-avx.h from x86.csv
7 from fnmatch
import fnmatch
11 "SSE", "SSE2", "SSE3", "SSSE3", "SSE4_1", "SSE4_2",
14 ignore
= set(["FISTTP",
15 "LDMXCSR", "VLDMXCSR", "STMXCSR", "VSTMXCSR"])
30 'vPCMP[EI]STR*': 0x0f,
41 'vPS[LR][AL][WDQ]': 0x3f,
43 'vROUND[PS][SD]': 0x7,
46 'vAESKEYGENASSIST': 0,
47 'VEXTRACT[FI]128': 0x01,
48 'VINSERT[FI]128': 0x01,
50 'VPERM2[FI]128': 0x33,
57 def strip_comments(x
):
59 if l
!= '' and l
[0] != '#':
71 raise Exception("bad reg_w %d" % w
)
89 return t
+ " PTR 16[rdx]"
93 def __init__(self
, reg
, mw
):
94 if mw
not in [0, 8, 16, 32, 64, 128, 256]:
95 raise Exception("Bad /m width: %s" % w
)
101 return mem_w(self
.mw
)
103 return "%smm%d" % (self
.reg
, n
)
109 return "mm%d" % (n
& 7)
111 def match(op
, pattern
):
112 if pattern
[0] == 'v':
113 return fnmatch(op
, pattern
[1:]) or fnmatch(op
, 'V'+pattern
[1:])
114 return fnmatch(op
, pattern
)
119 def __init__(self
, reg
, w
):
120 if w
not in [32, 64]:
121 raise Exception("Bad vsib width: %s" % w
)
125 reg
= "%smm%d" % (self
.reg
, n
>> 2)
126 return "[rsi + %s * %d]" % (reg
, 1 << (n
& 3))
131 def __init__(self
, op
):
132 for k
, v
in imask
.items():
134 self
.mask
= imask
[k
];
136 raise Exception("Unknown immediate")
143 while (n
& ~mask
) != 0:
149 def __init__(self
, rw
, mw
):
150 if rw
not in [8, 16, 32, 64]:
151 raise Exception("Bad r/w width: %s" % w
)
152 if mw
not in [0, 8, 16, 32, 64]:
153 raise Exception("Bad r/w width: %s" % w
)
159 return mem_w(self
.mw
)
161 return reg_w(self
.rw
)
166 def __init__(self
, w
):
167 if w
not in [8, 16, 32, 64, 128, 256]:
168 raise Exception("Bad mem width: %s" % w
)
173 def ArgGenerator(arg
, op
):
174 if arg
[:3] == 'xmm' or arg
[:3] == "ymm":
176 r
, m
= arg
.split('/')
178 raise Exception("Expected /m: %s", arg
)
179 return XMMArg(arg
[0], int(m
[1:]));
181 return XMMArg(arg
[0], 0);
182 elif arg
[:2] == 'mm':
184 elif arg
[:4] == 'imm8':
186 elif arg
== '<XMM0>':
190 r
, m
= arg
.split('/')
192 raise Exception("Expected /m: %s", arg
)
200 return ArgRM(int(arg
[1:]), 0);
202 return ArgMem(int(arg
[1:]))
203 elif arg
[:2] == 'vm':
204 return ArgVSIB(arg
[-1], int(arg
[2:-1]))
206 raise Exception("Unrecognised arg: %s", arg
)
209 def __init__(self
, op
, args
):
211 if op
[-2:] in ["PS", "PD", "SS", "SD"]:
220 self
.args
= list(ArgGenerator(a
, op
) for a
in args
)
221 if len(self
.args
) > 0 and self
.args
[-1] is None:
222 self
.args
= self
.args
[:-1]
223 except Exception as e
:
224 raise Exception("Bad arg %s: %s" % (op
, e
))
230 nreg
= len(self
.args
)
234 if isinstance(self
.args
[-1], ArgImm8u
):
236 immarg
= self
.args
[-1]
240 for n
, arg
in enumerate(self
.args
):
244 if (self
.op
.startswith("VGATHER") or self
.op
.startswith("VPGATHER")):
245 if "GATHERD" in self
.op
:
250 (dest
, ireg |
0, regs
[0]),
251 (dest
, ireg |
1, regs
[0]),
252 (dest
, ireg |
2, regs
[0]),
253 (dest
, ireg |
3, regs
[0]),
256 raise Exception("vsib with memory: %s" % self
.op
)
258 regset
= [(regs
[0],)]
267 regset
+= [(-1, regs
[0])]
269 regset
+= [(dest
, -1)]
272 (dest
, regs
[0], regs
[1]),
273 (dest
, regs
[0], regs
[0]),
274 (regs
[0], regs
[0], regs
[1]),
275 (regs
[0], regs
[1], regs
[0]),
276 (regs
[0], regs
[0], regs
[0]),
281 (regs
[0], regs
[0], -1),
284 raise Exception("Memarg %d" % memarg
)
287 (dest
, regs
[0], regs
[1], regs
[2]),
288 (dest
, regs
[0], regs
[0], regs
[1]),
289 (dest
, regs
[0], regs
[1], regs
[0]),
290 (dest
, regs
[1], regs
[0], regs
[0]),
291 (dest
, regs
[0], regs
[0], regs
[0]),
292 (regs
[0], regs
[0], regs
[1], regs
[2]),
293 (regs
[0], regs
[1], regs
[0], regs
[2]),
294 (regs
[0], regs
[1], regs
[2], regs
[0]),
295 (regs
[0], regs
[0], regs
[0], regs
[1]),
296 (regs
[0], regs
[0], regs
[1], regs
[0]),
297 (regs
[0], regs
[1], regs
[0], regs
[0]),
298 (regs
[0], regs
[0], regs
[0], regs
[0]),
302 (dest
, regs
[0], -1, regs
[1]),
303 (dest
, regs
[0], -1, regs
[0]),
304 (regs
[0], regs
[0], -1, regs
[1]),
305 (regs
[0], regs
[1], -1, regs
[0]),
306 (regs
[0], regs
[0], -1, regs
[0]),
309 raise Exception("Memarg4 %d" % memarg
)
311 raise Exception("Too many regs: %s(%d)" % (self
.op
, nreg
))
315 for i
in range(nreg
):
317 argstr
.append(arg
.regstr(regv
[i
]))
319 yield self
.op
+ ' ' + ','.join(argstr
)
321 for immval
in immarg
.vals():
322 yield self
.op
+ ' ' + ','.join(argstr
) + ',' + str(immval
)
331 if len(sys
.argv
) != 3:
332 print("Usage: test-avx.py x86.csv test-avx.h")
334 csvfile
= open(sys
.argv
[1], 'r', newline
='')
335 with
open(sys
.argv
[2], "w") as outf
:
336 outf
.write("// Generated by test-avx.py. Do not edit.\n")
337 for row
in csv
.reader(strip_comments(csvfile
)):
338 insn
= row
[0].replace(',', '').split()
339 if insn
[0] in ignore
:
343 g
= InsnGenerator(insn
[0], insn
[1:])
345 outf
.write('TEST(%d, "%s", %s)\n' % (n
, insn
, g
.optype
))
347 outf
.write("#undef TEST\n")
350 if __name__
== "__main__":