Fix Sparc test which was failing in the presence of new v9 opcodes.
[deliverable/binutils-gdb.git] / opcodes / i386-gen.c
CommitLineData
4b95cf5c 1/* Copyright (C) 2007-2014 Free Software Foundation, Inc.
40b8e679 2
9b201bb5 3 This file is part of the GNU opcodes library.
40b8e679 4
9b201bb5 5 This library is free software; you can redistribute it and/or modify
40b8e679 6 it under the terms of the GNU General Public License as published by
9b201bb5
NC
7 the Free Software Foundation; either version 3, or (at your option)
8 any later version.
40b8e679 9
9b201bb5
NC
10 It is distributed in the hope that it will be useful, but WITHOUT
11 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
13 License for more details.
40b8e679
L
14
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
9b201bb5
NC
17 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
18 MA 02110-1301, USA. */
40b8e679 19
40fb9820 20#include "sysdep.h"
40b8e679 21#include <stdio.h>
40b8e679
L
22#include <errno.h>
23#include "getopt.h"
24#include "libiberty.h"
c587b3f9 25#include "hashtab.h"
40b8e679
L
26#include "safe-ctype.h"
27
28#include "i386-opc.h"
29
30#include <libintl.h>
31#define _(String) gettext (String)
32
33static const char *program_name = NULL;
34static int debug = 0;
35
40fb9820
L
36typedef struct initializer
37{
38 const char *name;
39 const char *init;
40} initializer;
41
8acd5377 42static initializer cpu_flag_init[] =
40fb9820
L
43{
44 { "CPU_UNKNOWN_FLAGS",
7a9068fe 45 "~(CpuL1OM|CpuK1OM)" },
40fb9820
L
46 { "CPU_GENERIC32_FLAGS",
47 "Cpu186|Cpu286|Cpu386" },
29c048b6 48 { "CPU_GENERIC64_FLAGS",
da98bb4c 49 "Cpu186|Cpu286|Cpu386|Cpu486|Cpu586|Cpu686|CpuClflush|Cpu387|Cpu687|CpuNop|CpuMMX|CpuSSE|CpuSSE2|CpuLM" },
40fb9820
L
50 { "CPU_NONE_FLAGS",
51 "0" },
52 { "CPU_I186_FLAGS",
53 "Cpu186" },
54 { "CPU_I286_FLAGS",
55 "Cpu186|Cpu286" },
56 { "CPU_I386_FLAGS",
57 "Cpu186|Cpu286|Cpu386" },
58 { "CPU_I486_FLAGS",
59 "Cpu186|Cpu286|Cpu386|Cpu486" },
60 { "CPU_I586_FLAGS",
309d3373 61 "Cpu186|Cpu286|Cpu386|Cpu486|Cpu586|Cpu387" },
40fb9820 62 { "CPU_I686_FLAGS",
309d3373 63 "Cpu186|Cpu286|Cpu386|Cpu486|Cpu586|Cpu686|Cpu387|Cpu687" },
22109423
L
64 { "CPU_PENTIUMPRO_FLAGS",
65 "Cpu186|Cpu286|Cpu386|Cpu486|Cpu586|Cpu686|Cpu387|Cpu687|CpuNop" },
40fb9820 66 { "CPU_P2_FLAGS",
22109423 67 "Cpu186|Cpu286|Cpu386|Cpu486|Cpu586|Cpu686|Cpu387|Cpu687|CpuNop|CpuMMX" },
40fb9820 68 { "CPU_P3_FLAGS",
22109423 69 "Cpu186|Cpu286|Cpu386|Cpu486|Cpu586|Cpu686|Cpu387|Cpu687|CpuNop|CpuMMX|CpuSSE" },
40fb9820 70 { "CPU_P4_FLAGS",
22109423 71 "Cpu186|Cpu286|Cpu386|Cpu486|Cpu586|Cpu686|CpuClflush|Cpu387|Cpu687|CpuNop|CpuMMX|CpuSSE|CpuSSE2" },
40fb9820 72 { "CPU_NOCONA_FLAGS",
60aa667e 73 "Cpu186|Cpu286|Cpu386|Cpu486|Cpu586|Cpu686|CpuClflush|Cpu387|Cpu687|CpuFISTTP|CpuNop|CpuMMX|CpuSSE|CpuSSE2|CpuSSE3|CpuLM|CpuCX16" },
40fb9820 74 { "CPU_CORE_FLAGS",
60aa667e 75 "Cpu186|Cpu286|Cpu386|Cpu486|Cpu586|Cpu686|CpuClflush|Cpu387|Cpu687|CpuFISTTP|CpuNop|CpuMMX|CpuSSE|CpuSSE2|CpuSSE3|CpuCX16" },
40fb9820 76 { "CPU_CORE2_FLAGS",
60aa667e 77 "Cpu186|Cpu286|Cpu386|Cpu486|Cpu586|Cpu686|CpuClflush|Cpu387|Cpu687|CpuFISTTP|CpuNop|CpuMMX|CpuSSE|CpuSSE2|CpuSSE3|CpuSSSE3|CpuLM|CpuCX16" },
bd5295b2 78 { "CPU_COREI7_FLAGS",
60aa667e 79 "Cpu186|Cpu286|Cpu386|Cpu486|Cpu586|Cpu686|CpuClflush|Cpu387|Cpu687|CpuFISTTP|CpuNop|CpuMMX|CpuSSE|CpuSSE2|CpuSSE3|CpuSSSE3|CpuSSE4_1|CpuSSE4_2|CpuRdtscp|CpuLM|CpuCX16" },
40fb9820 80 { "CPU_K6_FLAGS",
309d3373 81 "Cpu186|Cpu286|Cpu386|Cpu486|Cpu586|CpuSYSCALL|Cpu387|CpuMMX" },
40fb9820 82 { "CPU_K6_2_FLAGS",
d56da83e 83 "Cpu186|Cpu286|Cpu386|Cpu486|Cpu586|CpuSYSCALL|Cpu387|CpuMMX|Cpu3dnow" },
40fb9820 84 { "CPU_ATHLON_FLAGS",
22109423 85 "Cpu186|Cpu286|Cpu386|Cpu486|Cpu586|Cpu686|CpuSYSCALL|Cpu387|Cpu687|CpuNop|CpuMMX|Cpu3dnow|Cpu3dnowA" },
40fb9820 86 { "CPU_K8_FLAGS",
22109423 87 "Cpu186|Cpu286|Cpu386|Cpu486|Cpu586|Cpu686|CpuSYSCALL|CpuRdtscp|Cpu387|Cpu687|CpuNop|CpuMMX|Cpu3dnow|Cpu3dnowA|CpuSSE|CpuSSE2|CpuLM" },
40fb9820 88 { "CPU_AMDFAM10_FLAGS",
22109423 89 "Cpu186|Cpu286|Cpu386|Cpu486|Cpu586|Cpu686|CpuSYSCALL|CpuRdtscp|Cpu387|Cpu687|CpuFISTTP|CpuNop|CpuMMX|Cpu3dnow|Cpu3dnowA|CpuSSE|CpuSSE2|CpuSSE3|CpuSSE4a|CpuABM|CpuLM" },
68339fdf 90 { "CPU_BDVER1_FLAGS",
160a30bb 91 "Cpu186|Cpu286|Cpu386|Cpu486|Cpu586|Cpu686|CpuSYSCALL|CpuRdtscp|Cpu387|Cpu687|CpuFISTTP|CpuNop|CpuMMX|CpuSSE|CpuSSE2|CpuSSE3|CpuSSE4a|CpuABM|CpuLM|CpuFMA4|CpuXOP|CpuLWP|CpuCX16|CpuClflush|CpuSSSE3|CpuSVME|CpuSSE4_1|CpuSSE4_2|CpuXsave|CpuAES|CpuAVX|CpuPCLMUL|CpuLZCNT|CpuPRFCHW" },
4cab4add 92 { "CPU_BDVER2_FLAGS",
160a30bb 93 "Cpu186|Cpu286|Cpu386|Cpu486|Cpu586|Cpu686|CpuSYSCALL|CpuRdtscp|Cpu387|Cpu687|CpuFISTTP|CpuNop|CpuMMX|CpuSSE|CpuSSE2|CpuSSE3|CpuSSE4a|CpuABM|CpuLM|CpuFMA|CpuFMA4|CpuXOP|CpuLWP|CpuBMI|CpuTBM|CpuF16C|CpuCX16|CpuClflush|CpuSSSE3|CpuSVME|CpuSSE4_1|CpuSSE4_2|CpuXsave|CpuAES|CpuAVX|CpuPCLMUL|CpuLZCNT|CpuPRFCHW" },
5e5c50d3 94 { "CPU_BDVER3_FLAGS",
6091d651 95 "Cpu186|Cpu286|Cpu386|Cpu486|Cpu586|Cpu686|CpuSYSCALL|CpuRdtscp|Cpu387|Cpu687|CpuFISTTP|CpuNop|CpuMMX|CpuSSE|CpuSSE2|CpuSSE3|CpuSSE4a|CpuABM|CpuLM|CpuFMA|CpuFMA4|CpuXOP|CpuLWP|CpuBMI|CpuTBM|CpuF16C|CpuCX16|CpuClflush|CpuSSSE3|CpuSVME|CpuSSE4_1|CpuSSE4_2|CpuAES|CpuAVX|CpuPCLMUL|CpuLZCNT|CpuPRFCHW|CpuXsave|CpuXsaveopt|CpuFSGSBase" },
c7b0bd56
SE
96 { "CPU_BDVER4_FLAGS",
97 "Cpu186|Cpu286|Cpu386|Cpu486|Cpu586|Cpu686|CpuSYSCALL|CpuRdtscp|Cpu387|Cpu687|CpuFISTTP|CpuNop|CpuMMX|CpuSSE|CpuSSE2|CpuSSE3|CpuSSE4a|CpuABM|CpuLM|CpuFMA|CpuFMA4|CpuXOP|CpuLWP|CpuBMI|CpuTBM|CpuF16C|CpuCX16|CpuClflush|CpuSSSE3|CpuSVME|CpuSSE4_1|CpuSSE4_2|CpuAES|CpuAVX|CpuPCLMUL|CpuLZCNT|CpuPRFCHW|CpuXsave|CpuXsaveopt|CpuFSGSBase|CpuAVX2|CpuMovbe|CpuBMI2|CpuRdRnd" },
7b458c12 98 { "CPU_BTVER1_FLAGS",
160a30bb 99 "Cpu186|Cpu286|Cpu386|Cpu486|Cpu586|Cpu686|CpuSYSCALL|CpuRdtscp|Cpu387|Cpu687|CpuNop|CpuMMX|CpuSSE|CpuSSE2|CpuSSE3|CpuSSSE3|CpuSSE4a|CpuABM|CpuLM|CpuPRFCHW|CpuCX16|CpuClflush|CpuFISTTP|CpuSVME|CpuLZCNT" },
7b458c12 100 { "CPU_BTVER2_FLAGS",
160a30bb 101 "Cpu186|Cpu286|Cpu386|Cpu486|Cpu586|Cpu686|CpuSYSCALL|CpuRdtscp|Cpu387|Cpu687|CpuNop|CpuMMX|CpuSSE|CpuSSE2|CpuSSE3|CpuSSSE3|CpuSSE4a|CpuSSE4_1|CpuSSE4_2|CpuABM|CpuLM|CpuBMI|CpuF16C|CpuAES|CpuPCLMUL|CpuAVX|CpuMovbe|CpuXsave|CpuXsaveopt|CpuPRFCHW|CpuCX16|CpuClflush|CpuFISTTP|CpuSVME|CpuLZCNT" },
309d3373
JB
102 { "CPU_8087_FLAGS",
103 "Cpu8087" },
104 { "CPU_287_FLAGS",
105 "Cpu287" },
106 { "CPU_387_FLAGS",
107 "Cpu387" },
108 { "CPU_ANY87_FLAGS",
109 "Cpu8087|Cpu287|Cpu387|Cpu687|CpuFISTTP" },
bd5295b2
L
110 { "CPU_CLFLUSH_FLAGS",
111 "CpuClflush" },
22109423
L
112 { "CPU_NOP_FLAGS",
113 "CpuNop" },
bd5295b2
L
114 { "CPU_SYSCALL_FLAGS",
115 "CpuSYSCALL" },
40fb9820
L
116 { "CPU_MMX_FLAGS",
117 "CpuMMX" },
118 { "CPU_SSE_FLAGS",
115c7c25 119 "CpuMMX|CpuSSE" },
40fb9820 120 { "CPU_SSE2_FLAGS",
115c7c25 121 "CpuMMX|CpuSSE|CpuSSE2" },
40fb9820 122 { "CPU_SSE3_FLAGS",
115c7c25 123 "CpuMMX|CpuSSE|CpuSSE2|CpuSSE3" },
40fb9820 124 { "CPU_SSSE3_FLAGS",
115c7c25 125 "CpuMMX|CpuSSE|CpuSSE2|CpuSSE3|CpuSSSE3" },
40fb9820 126 { "CPU_SSE4_1_FLAGS",
115c7c25 127 "CpuMMX|CpuSSE|CpuSSE2|CpuSSE3|CpuSSSE3|CpuSSE4_1" },
40fb9820 128 { "CPU_SSE4_2_FLAGS",
115c7c25 129 "CpuMMX|CpuSSE|CpuSSE2|CpuSSE3|CpuSSSE3|CpuSSE4_1|CpuSSE4_2" },
309d3373 130 { "CPU_ANY_SSE_FLAGS",
43234a1e 131 "CpuSSE|CpuSSE2|CpuSSE3|CpuSSSE3|CpuSSE4_1|CpuSSE4_2|CpuSSE4a|CpuAVX|CpuAVX2|CpuAVX512F|CpuAVX512CD|CpuAVX512ER|CpuAVX512PF" },
6305a203
L
132 { "CPU_VMX_FLAGS",
133 "CpuVMX" },
134 { "CPU_SMX_FLAGS",
135 "CpuSMX" },
f03fe4c1
L
136 { "CPU_XSAVE_FLAGS",
137 "CpuXsave" },
c7b8aa3a
L
138 { "CPU_XSAVEOPT_FLAGS",
139 "CpuXsaveopt" },
c0f3af97
L
140 { "CPU_AES_FLAGS",
141 "CpuMMX|CpuSSE|CpuSSE2|CpuSSE3|CpuSSSE3|CpuSSE4_1|CpuSSE4_2|CpuAES" },
594ab6a3
L
142 { "CPU_PCLMUL_FLAGS",
143 "CpuMMX|CpuSSE|CpuSSE2|CpuSSE3|CpuSSSE3|CpuSSE4_1|CpuSSE4_2|CpuPCLMUL" },
c0f3af97
L
144 { "CPU_FMA_FLAGS",
145 "CpuMMX|CpuSSE|CpuSSE2|CpuSSE3|CpuSSSE3|CpuSSE4_1|CpuSSE4_2|CpuAVX|CpuFMA" },
922d8de8
DR
146 { "CPU_FMA4_FLAGS",
147 "CpuMMX|CpuSSE|CpuSSE2|CpuSSE3|CpuSSSE3|CpuSSE4_1|CpuSSE4_2|CpuAVX|CpuFMA4" },
5dd85c99 148 { "CPU_XOP_FLAGS",
f0ae4a24 149 "CpuMMX|CpuSSE|CpuSSE2|CpuSSE3|CpuSSSE3|CpuSSE4_1|CpuSSE4_2|CpuSSE4a|CpuABM|CpuAVX|CpuFMA4|CpuXOP" },
f88c9eb0
SP
150 { "CPU_LWP_FLAGS",
151 "CpuLWP" },
f12dc422
L
152 { "CPU_BMI_FLAGS",
153 "CpuBMI" },
2a2a0f38
QN
154 { "CPU_TBM_FLAGS",
155 "CpuTBM" },
f1f8f695
L
156 { "CPU_MOVBE_FLAGS",
157 "CpuMovbe" },
60aa667e
L
158 { "CPU_CX16_FLAGS",
159 "CpuCX16" },
1b7f3fb0
L
160 { "CPU_RDTSCP_FLAGS",
161 "CpuRdtscp" },
f1f8f695
L
162 { "CPU_EPT_FLAGS",
163 "CpuEPT" },
c7b8aa3a
L
164 { "CPU_FSGSBASE_FLAGS",
165 "CpuFSGSBase" },
166 { "CPU_RDRND_FLAGS",
167 "CpuRdRnd" },
168 { "CPU_F16C_FLAGS",
169 "CpuF16C" },
6c30d220
L
170 { "CPU_BMI2_FLAGS",
171 "CpuBMI2" },
172 { "CPU_LZCNT_FLAGS",
173 "CpuLZCNT" },
42164a71
L
174 { "CPU_HLE_FLAGS",
175 "CpuHLE" },
176 { "CPU_RTM_FLAGS",
177 "CpuRTM" },
6c30d220
L
178 { "CPU_INVPCID_FLAGS",
179 "CpuINVPCID" },
8729a6f6
L
180 { "CPU_VMFUNC_FLAGS",
181 "CpuVMFUNC" },
40fb9820
L
182 { "CPU_3DNOW_FLAGS",
183 "CpuMMX|Cpu3dnow" },
184 { "CPU_3DNOWA_FLAGS",
115c7c25 185 "CpuMMX|Cpu3dnow|Cpu3dnowA" },
40fb9820
L
186 { "CPU_PADLOCK_FLAGS",
187 "CpuPadLock" },
188 { "CPU_SVME_FLAGS",
189 "CpuSVME" },
190 { "CPU_SSE4A_FLAGS",
115c7c25 191 "CpuMMX|CpuSSE|CpuSSE2|CpuSSE3|CpuSSE4a" },
40fb9820 192 { "CPU_ABM_FLAGS",
3629bb00 193 "CpuABM" },
c0f3af97
L
194 { "CPU_AVX_FLAGS",
195 "CpuMMX|CpuSSE|CpuSSE2|CpuSSE3|CpuSSSE3|CpuSSE4_1|CpuSSE4_2|CpuAVX" },
6c30d220
L
196 { "CPU_AVX2_FLAGS",
197 "CpuMMX|CpuSSE|CpuSSE2|CpuSSE3|CpuSSSE3|CpuSSE4_1|CpuSSE4_2|CpuAVX|CpuAVX2" },
43234a1e
L
198 { "CPU_AVX512F_FLAGS",
199 "CpuMMX|CpuSSE|CpuSSE2|CpuSSE3|CpuSSSE3|CpuSSE4_1|CpuSSE4_2|CpuAVX|CpuAVX2|CpuAVX512F" },
200 { "CPU_AVX512CD_FLAGS",
201 "CpuMMX|CpuSSE|CpuSSE2|CpuSSE3|CpuSSSE3|CpuSSE4_1|CpuSSE4_2|CpuAVX|CpuAVX2|CpuAVX512F|CpuAVX512CD" },
202 { "CPU_AVX512ER_FLAGS",
203 "CpuMMX|CpuSSE|CpuSSE2|CpuSSE3|CpuSSSE3|CpuSSE4_1|CpuSSE4_2|CpuAVX|CpuAVX2|CpuAVX512F|CpuAVX512ER" },
204 { "CPU_AVX512PF_FLAGS",
205 "CpuMMX|CpuSSE|CpuSSE2|CpuSSE3|CpuSSSE3|CpuSSE4_1|CpuSSE4_2|CpuAVX|CpuAVX2|CpuAVX512F|CpuAVX512PF" },
309d3373 206 { "CPU_ANY_AVX_FLAGS",
43234a1e 207 "CpuAVX|CpuAVX2|CpuAVX512F|CpuAVX512CD|CpuAVX512ER|CpuAVX512PF" },
8a9036a4
L
208 { "CPU_L1OM_FLAGS",
209 "unknown" },
7a9068fe
L
210 { "CPU_K1OM_FLAGS",
211 "unknown" },
e2e1fcde
L
212 { "CPU_ADX_FLAGS",
213 "CpuADX" },
214 { "CPU_RDSEED_FLAGS",
215 "CpuRdSeed" },
216 { "CPU_PRFCHW_FLAGS",
217 "CpuPRFCHW" },
5c111e37
L
218 { "CPU_SMAP_FLAGS",
219 "CpuSMAP" },
7e8b059b
L
220 { "CPU_MPX_FLAGS",
221 "CpuMPX" },
a0046408
L
222 { "CPU_SHA_FLAGS",
223 "CpuSHA" },
963f3586
IT
224 { "CPU_CLFLUSHOPT_FLAGS",
225 "CpuClflushOpt" },
226 { "CPU_XSAVES_FLAGS",
227 "CpuXSAVES" },
228 { "CPU_XSAVEC_FLAGS",
229 "CpuXSAVEC" },
dcf893b5
IT
230 { "CPU_PREFETCHWT1_FLAGS",
231 "CpuPREFETCHWT1" },
40fb9820
L
232};
233
8acd5377 234static initializer operand_type_init[] =
40fb9820
L
235{
236 { "OPERAND_TYPE_NONE",
237 "0" },
238 { "OPERAND_TYPE_REG8",
239 "Reg8" },
240 { "OPERAND_TYPE_REG16",
241 "Reg16" },
242 { "OPERAND_TYPE_REG32",
243 "Reg32" },
244 { "OPERAND_TYPE_REG64",
245 "Reg64" },
246 { "OPERAND_TYPE_IMM1",
247 "Imm1" },
248 { "OPERAND_TYPE_IMM8",
249 "Imm8" },
250 { "OPERAND_TYPE_IMM8S",
251 "Imm8S" },
252 { "OPERAND_TYPE_IMM16",
253 "Imm16" },
254 { "OPERAND_TYPE_IMM32",
255 "Imm32" },
256 { "OPERAND_TYPE_IMM32S",
257 "Imm32S" },
258 { "OPERAND_TYPE_IMM64",
259 "Imm64" },
260 { "OPERAND_TYPE_BASEINDEX",
261 "BaseIndex" },
262 { "OPERAND_TYPE_DISP8",
263 "Disp8" },
264 { "OPERAND_TYPE_DISP16",
265 "Disp16" },
266 { "OPERAND_TYPE_DISP32",
267 "Disp32" },
268 { "OPERAND_TYPE_DISP32S",
269 "Disp32S" },
270 { "OPERAND_TYPE_DISP64",
271 "Disp64" },
272 { "OPERAND_TYPE_INOUTPORTREG",
273 "InOutPortReg" },
274 { "OPERAND_TYPE_SHIFTCOUNT",
275 "ShiftCount" },
276 { "OPERAND_TYPE_CONTROL",
277 "Control" },
278 { "OPERAND_TYPE_TEST",
279 "Test" },
280 { "OPERAND_TYPE_DEBUG",
281 "FloatReg" },
282 { "OPERAND_TYPE_FLOATREG",
283 "FloatReg" },
284 { "OPERAND_TYPE_FLOATACC",
285 "FloatAcc" },
286 { "OPERAND_TYPE_SREG2",
287 "SReg2" },
288 { "OPERAND_TYPE_SREG3",
289 "SReg3" },
290 { "OPERAND_TYPE_ACC",
291 "Acc" },
292 { "OPERAND_TYPE_JUMPABSOLUTE",
293 "JumpAbsolute" },
294 { "OPERAND_TYPE_REGMMX",
295 "RegMMX" },
296 { "OPERAND_TYPE_REGXMM",
297 "RegXMM" },
c0f3af97
L
298 { "OPERAND_TYPE_REGYMM",
299 "RegYMM" },
43234a1e
L
300 { "OPERAND_TYPE_REGZMM",
301 "RegZMM" },
302 { "OPERAND_TYPE_REGMASK",
303 "RegMask" },
40fb9820
L
304 { "OPERAND_TYPE_ESSEG",
305 "EsSeg" },
306 { "OPERAND_TYPE_ACC32",
7d5e4556 307 "Reg32|Acc|Dword" },
40fb9820 308 { "OPERAND_TYPE_ACC64",
7d5e4556 309 "Reg64|Acc|Qword" },
65da13b5
L
310 { "OPERAND_TYPE_INOUTPORTREG",
311 "InOutPortReg" },
40fb9820
L
312 { "OPERAND_TYPE_REG16_INOUTPORTREG",
313 "Reg16|InOutPortReg" },
314 { "OPERAND_TYPE_DISP16_32",
315 "Disp16|Disp32" },
316 { "OPERAND_TYPE_ANYDISP",
317 "Disp8|Disp16|Disp32|Disp32S|Disp64" },
318 { "OPERAND_TYPE_IMM16_32",
319 "Imm16|Imm32" },
320 { "OPERAND_TYPE_IMM16_32S",
321 "Imm16|Imm32S" },
322 { "OPERAND_TYPE_IMM16_32_32S",
323 "Imm16|Imm32|Imm32S" },
2f81ff92
L
324 { "OPERAND_TYPE_IMM32_64",
325 "Imm32|Imm64" },
40fb9820
L
326 { "OPERAND_TYPE_IMM32_32S_DISP32",
327 "Imm32|Imm32S|Disp32" },
328 { "OPERAND_TYPE_IMM64_DISP64",
329 "Imm64|Disp64" },
330 { "OPERAND_TYPE_IMM32_32S_64_DISP32",
331 "Imm32|Imm32S|Imm64|Disp32" },
332 { "OPERAND_TYPE_IMM32_32S_64_DISP32_64",
333 "Imm32|Imm32S|Imm64|Disp32|Disp64" },
a683cc34
SP
334 { "OPERAND_TYPE_VEC_IMM4",
335 "Vec_Imm4" },
7e8b059b
L
336 { "OPERAND_TYPE_REGBND",
337 "RegBND" },
43234a1e
L
338 { "OPERAND_TYPE_VEC_DISP8",
339 "Vec_Disp8" },
40fb9820
L
340};
341
342typedef struct bitfield
343{
344 int position;
345 int value;
346 const char *name;
347} bitfield;
348
349#define BITFIELD(n) { n, 0, #n }
350
351static bitfield cpu_flags[] =
352{
353 BITFIELD (Cpu186),
354 BITFIELD (Cpu286),
355 BITFIELD (Cpu386),
356 BITFIELD (Cpu486),
357 BITFIELD (Cpu586),
358 BITFIELD (Cpu686),
bd5295b2 359 BITFIELD (CpuClflush),
22109423 360 BITFIELD (CpuNop),
bd5295b2 361 BITFIELD (CpuSYSCALL),
309d3373
JB
362 BITFIELD (Cpu8087),
363 BITFIELD (Cpu287),
364 BITFIELD (Cpu387),
365 BITFIELD (Cpu687),
366 BITFIELD (CpuFISTTP),
40fb9820 367 BITFIELD (CpuMMX),
40fb9820
L
368 BITFIELD (CpuSSE),
369 BITFIELD (CpuSSE2),
370 BITFIELD (CpuSSE3),
371 BITFIELD (CpuSSSE3),
372 BITFIELD (CpuSSE4_1),
373 BITFIELD (CpuSSE4_2),
c0f3af97 374 BITFIELD (CpuAVX),
6c30d220 375 BITFIELD (CpuAVX2),
43234a1e
L
376 BITFIELD (CpuAVX512F),
377 BITFIELD (CpuAVX512CD),
378 BITFIELD (CpuAVX512ER),
379 BITFIELD (CpuAVX512PF),
8a9036a4 380 BITFIELD (CpuL1OM),
7a9068fe 381 BITFIELD (CpuK1OM),
40fb9820
L
382 BITFIELD (CpuSSE4a),
383 BITFIELD (Cpu3dnow),
384 BITFIELD (Cpu3dnowA),
385 BITFIELD (CpuPadLock),
386 BITFIELD (CpuSVME),
387 BITFIELD (CpuVMX),
47dd174c 388 BITFIELD (CpuSMX),
40fb9820 389 BITFIELD (CpuABM),
475a2301 390 BITFIELD (CpuXsave),
c7b8aa3a 391 BITFIELD (CpuXsaveopt),
c0f3af97 392 BITFIELD (CpuAES),
594ab6a3 393 BITFIELD (CpuPCLMUL),
c0f3af97 394 BITFIELD (CpuFMA),
f88c9eb0 395 BITFIELD (CpuFMA4),
5dd85c99 396 BITFIELD (CpuXOP),
f88c9eb0 397 BITFIELD (CpuLWP),
f12dc422 398 BITFIELD (CpuBMI),
2a2a0f38 399 BITFIELD (CpuTBM),
c0f3af97 400 BITFIELD (CpuLM),
f1f8f695 401 BITFIELD (CpuMovbe),
60aa667e 402 BITFIELD (CpuCX16),
f1f8f695 403 BITFIELD (CpuEPT),
1b7f3fb0 404 BITFIELD (CpuRdtscp),
c7b8aa3a
L
405 BITFIELD (CpuFSGSBase),
406 BITFIELD (CpuRdRnd),
407 BITFIELD (CpuF16C),
6c30d220
L
408 BITFIELD (CpuBMI2),
409 BITFIELD (CpuLZCNT),
42164a71
L
410 BITFIELD (CpuHLE),
411 BITFIELD (CpuRTM),
6c30d220 412 BITFIELD (CpuINVPCID),
8729a6f6 413 BITFIELD (CpuVMFUNC),
e2e1fcde
L
414 BITFIELD (CpuRDSEED),
415 BITFIELD (CpuADX),
416 BITFIELD (CpuPRFCHW),
5c111e37 417 BITFIELD (CpuSMAP),
a0046408 418 BITFIELD (CpuSHA),
43234a1e 419 BITFIELD (CpuVREX),
963f3586
IT
420 BITFIELD (CpuClflushOpt),
421 BITFIELD (CpuXSAVES),
422 BITFIELD (CpuXSAVEC),
dcf893b5 423 BITFIELD (CpuPREFETCHWT1),
40fb9820
L
424 BITFIELD (Cpu64),
425 BITFIELD (CpuNo64),
7e8b059b 426 BITFIELD (CpuMPX),
40fb9820
L
427#ifdef CpuUnused
428 BITFIELD (CpuUnused),
429#endif
430};
431
432static bitfield opcode_modifiers[] =
433{
434 BITFIELD (D),
435 BITFIELD (W),
b6169b20 436 BITFIELD (S),
40fb9820
L
437 BITFIELD (Modrm),
438 BITFIELD (ShortForm),
439 BITFIELD (Jump),
440 BITFIELD (JumpDword),
441 BITFIELD (JumpByte),
442 BITFIELD (JumpInterSegment),
443 BITFIELD (FloatMF),
444 BITFIELD (FloatR),
445 BITFIELD (FloatD),
446 BITFIELD (Size16),
447 BITFIELD (Size32),
448 BITFIELD (Size64),
56ffb741 449 BITFIELD (CheckRegSize),
40fb9820
L
450 BITFIELD (IgnoreSize),
451 BITFIELD (DefaultSize),
452 BITFIELD (No_bSuf),
453 BITFIELD (No_wSuf),
454 BITFIELD (No_lSuf),
455 BITFIELD (No_sSuf),
456 BITFIELD (No_qSuf),
7ce189b3 457 BITFIELD (No_ldSuf),
40fb9820
L
458 BITFIELD (FWait),
459 BITFIELD (IsString),
7e8b059b 460 BITFIELD (BNDPrefixOk),
c32fa91d 461 BITFIELD (IsLockable),
40fb9820 462 BITFIELD (RegKludge),
e2ec9d29 463 BITFIELD (FirstXmm0),
c0f3af97 464 BITFIELD (Implicit1stXmm0),
29c048b6 465 BITFIELD (RepPrefixOk),
42164a71 466 BITFIELD (HLEPrefixOk),
ca61edf2
L
467 BITFIELD (ToDword),
468 BITFIELD (ToQword),
469 BITFIELD (AddrPrefixOp0),
40fb9820
L
470 BITFIELD (IsPrefix),
471 BITFIELD (ImmExt),
472 BITFIELD (NoRex64),
473 BITFIELD (Rex64),
474 BITFIELD (Ugh),
c0f3af97 475 BITFIELD (Vex),
2426c15f 476 BITFIELD (VexVVVV),
1ef99a7b 477 BITFIELD (VexW),
7f399153 478 BITFIELD (VexOpcode),
8cd7925b 479 BITFIELD (VexSources),
c0f3af97 480 BITFIELD (VexImmExt),
6c30d220 481 BITFIELD (VecSIB),
c0f3af97 482 BITFIELD (SSE2AVX),
81f8a913 483 BITFIELD (NoAVX),
43234a1e
L
484 BITFIELD (EVex),
485 BITFIELD (Masking),
486 BITFIELD (VecESize),
487 BITFIELD (Broadcast),
488 BITFIELD (StaticRounding),
489 BITFIELD (SAE),
490 BITFIELD (Disp8MemShift),
491 BITFIELD (NoDefMask),
1efbbeb4
L
492 BITFIELD (OldGcc),
493 BITFIELD (ATTMnemonic),
e1d4d893 494 BITFIELD (ATTSyntax),
5c07affc 495 BITFIELD (IntelSyntax),
40fb9820
L
496};
497
498static bitfield operand_types[] =
499{
500 BITFIELD (Reg8),
501 BITFIELD (Reg16),
502 BITFIELD (Reg32),
503 BITFIELD (Reg64),
504 BITFIELD (FloatReg),
505 BITFIELD (RegMMX),
506 BITFIELD (RegXMM),
c0f3af97 507 BITFIELD (RegYMM),
43234a1e
L
508 BITFIELD (RegZMM),
509 BITFIELD (RegMask),
94ff3a50 510 BITFIELD (Imm1),
40fb9820
L
511 BITFIELD (Imm8),
512 BITFIELD (Imm8S),
513 BITFIELD (Imm16),
514 BITFIELD (Imm32),
515 BITFIELD (Imm32S),
516 BITFIELD (Imm64),
40fb9820
L
517 BITFIELD (BaseIndex),
518 BITFIELD (Disp8),
519 BITFIELD (Disp16),
520 BITFIELD (Disp32),
521 BITFIELD (Disp32S),
522 BITFIELD (Disp64),
523 BITFIELD (InOutPortReg),
524 BITFIELD (ShiftCount),
525 BITFIELD (Control),
526 BITFIELD (Debug),
527 BITFIELD (Test),
528 BITFIELD (SReg2),
529 BITFIELD (SReg3),
530 BITFIELD (Acc),
531 BITFIELD (FloatAcc),
532 BITFIELD (JumpAbsolute),
533 BITFIELD (EsSeg),
534 BITFIELD (RegMem),
5c07affc 535 BITFIELD (Mem),
7d5e4556
L
536 BITFIELD (Byte),
537 BITFIELD (Word),
538 BITFIELD (Dword),
539 BITFIELD (Fword),
540 BITFIELD (Qword),
541 BITFIELD (Tbyte),
542 BITFIELD (Xmmword),
c0f3af97 543 BITFIELD (Ymmword),
43234a1e 544 BITFIELD (Zmmword),
7d5e4556
L
545 BITFIELD (Unspecified),
546 BITFIELD (Anysize),
a683cc34 547 BITFIELD (Vec_Imm4),
7e8b059b 548 BITFIELD (RegBND),
43234a1e 549 BITFIELD (Vec_Disp8),
40fb9820
L
550#ifdef OTUnused
551 BITFIELD (OTUnused),
552#endif
553};
554
3d4d5afa
L
555static const char *filename;
556
40fb9820
L
557static int
558compare (const void *x, const void *y)
559{
560 const bitfield *xp = (const bitfield *) x;
561 const bitfield *yp = (const bitfield *) y;
562 return xp->position - yp->position;
563}
564
40b8e679
L
565static void
566fail (const char *message, ...)
567{
568 va_list args;
29c048b6 569
40b8e679
L
570 va_start (args, message);
571 fprintf (stderr, _("%s: Error: "), program_name);
572 vfprintf (stderr, message, args);
573 va_end (args);
574 xexit (1);
575}
576
72ffa0fb
L
577static void
578process_copyright (FILE *fp)
579{
580 fprintf (fp, "/* This file is automatically generated by i386-gen. Do not edit! */\n\
4b95cf5c 581/* Copyright (C) 2007-2014 Free Software Foundation, Inc.\n\
72ffa0fb
L
582\n\
583 This file is part of the GNU opcodes library.\n\
584\n\
585 This library is free software; you can redistribute it and/or modify\n\
586 it under the terms of the GNU General Public License as published by\n\
587 the Free Software Foundation; either version 3, or (at your option)\n\
588 any later version.\n\
589\n\
590 It is distributed in the hope that it will be useful, but WITHOUT\n\
591 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY\n\
592 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public\n\
593 License for more details.\n\
594\n\
595 You should have received a copy of the GNU General Public License\n\
596 along with this program; if not, write to the Free Software\n\
597 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,\n\
598 MA 02110-1301, USA. */\n");
599}
600
40b8e679
L
601/* Remove leading white spaces. */
602
603static char *
604remove_leading_whitespaces (char *str)
605{
606 while (ISSPACE (*str))
607 str++;
608 return str;
609}
610
611/* Remove trailing white spaces. */
612
613static void
614remove_trailing_whitespaces (char *str)
615{
616 size_t last = strlen (str);
617
618 if (last == 0)
619 return;
620
621 do
622 {
623 last--;
624 if (ISSPACE (str [last]))
625 str[last] = '\0';
626 else
627 break;
628 }
629 while (last != 0);
630}
631
93b1ec2c 632/* Find next field separated by SEP and terminate it. Return a
40b8e679
L
633 pointer to the one after it. */
634
635static char *
c587b3f9 636next_field (char *str, char sep, char **next, char *last)
40b8e679
L
637{
638 char *p;
639
640 p = remove_leading_whitespaces (str);
93b1ec2c 641 for (str = p; *str != sep && *str != '\0'; str++);
40b8e679
L
642
643 *str = '\0';
644 remove_trailing_whitespaces (p);
645
29c048b6 646 *next = str + 1;
40b8e679 647
c587b3f9
L
648 if (p >= last)
649 abort ();
650
40b8e679
L
651 return p;
652}
653
40fb9820 654static void
8a9036a4
L
655set_bitfield (const char *f, bitfield *array, int value,
656 unsigned int size, int lineno)
40fb9820
L
657{
658 unsigned int i;
659
309d3373
JB
660 if (strcmp (f, "CpuFP") == 0)
661 {
8a9036a4
L
662 set_bitfield("Cpu387", array, value, size, lineno);
663 set_bitfield("Cpu287", array, value, size, lineno);
309d3373
JB
664 f = "Cpu8087";
665 }
666 else if (strcmp (f, "Mmword") == 0)
7d5e4556
L
667 f= "Qword";
668 else if (strcmp (f, "Oword") == 0)
669 f= "Xmmword";
40fb9820
L
670
671 for (i = 0; i < size; i++)
672 if (strcasecmp (array[i].name, f) == 0)
673 {
8a9036a4 674 array[i].value = value;
40fb9820
L
675 return;
676 }
677
2bf05e57
L
678 if (value)
679 {
680 const char *v = strchr (f, '=');
681
682 if (v)
683 {
684 size_t n = v - f;
685 char *end;
686
687 for (i = 0; i < size; i++)
688 if (strncasecmp (array[i].name, f, n) == 0)
689 {
690 value = strtol (v + 1, &end, 0);
691 if (*end == '\0')
692 {
693 array[i].value = value;
694 return;
695 }
696 break;
697 }
698 }
699 }
700
bd5295b2
L
701 if (lineno != -1)
702 fail (_("%s: %d: Unknown bitfield: %s\n"), filename, lineno, f);
703 else
704 fail (_("Unknown bitfield: %s\n"), f);
40fb9820
L
705}
706
707static void
708output_cpu_flags (FILE *table, bitfield *flags, unsigned int size,
709 int macro, const char *comma, const char *indent)
710{
711 unsigned int i;
712
713 fprintf (table, "%s{ { ", indent);
714
715 for (i = 0; i < size - 1; i++)
716 {
10632b79
L
717 if (((i + 1) % 20) != 0)
718 fprintf (table, "%d, ", flags[i].value);
719 else
720 fprintf (table, "%d,", flags[i].value);
40fb9820
L
721 if (((i + 1) % 20) == 0)
722 {
723 /* We need \\ for macro. */
724 if (macro)
725 fprintf (table, " \\\n %s", indent);
726 else
727 fprintf (table, "\n %s", indent);
728 }
729 }
730
731 fprintf (table, "%d } }%s\n", flags[i].value, comma);
732}
733
734static void
735process_i386_cpu_flag (FILE *table, char *flag, int macro,
bd5295b2
L
736 const char *comma, const char *indent,
737 int lineno)
40fb9820
L
738{
739 char *str, *next, *last;
8a9036a4 740 unsigned int i;
40fb9820
L
741 bitfield flags [ARRAY_SIZE (cpu_flags)];
742
743 /* Copy the default cpu flags. */
744 memcpy (flags, cpu_flags, sizeof (cpu_flags));
745
746 if (strcasecmp (flag, "unknown") == 0)
747 {
40fb9820 748 /* We turn on everything except for cpu64 in case of
8a9036a4
L
749 CPU_UNKNOWN_FLAGS. */
750 for (i = 0; i < ARRAY_SIZE (flags); i++)
751 if (flags[i].position != Cpu64)
752 flags[i].value = 1;
753 }
754 else if (flag[0] == '~')
755 {
756 last = flag + strlen (flag);
757
758 if (flag[1] == '(')
759 {
760 last -= 1;
761 next = flag + 2;
762 if (*last != ')')
763 fail (_("%s: %d: Missing `)' in bitfield: %s\n"), filename,
764 lineno, flag);
765 *last = '\0';
766 }
767 else
768 next = flag + 1;
769
770 /* First we turn on everything except for cpu64. */
40fb9820
L
771 for (i = 0; i < ARRAY_SIZE (flags); i++)
772 if (flags[i].position != Cpu64)
773 flags[i].value = 1;
8a9036a4
L
774
775 /* Turn off selective bits. */
776 for (; next && next < last; )
777 {
778 str = next_field (next, '|', &next, last);
779 if (str)
780 set_bitfield (str, flags, 0, ARRAY_SIZE (flags), lineno);
781 }
40fb9820
L
782 }
783 else if (strcmp (flag, "0"))
784 {
8a9036a4 785 /* Turn on selective bits. */
40fb9820
L
786 last = flag + strlen (flag);
787 for (next = flag; next && next < last; )
788 {
c587b3f9 789 str = next_field (next, '|', &next, last);
40fb9820 790 if (str)
8a9036a4 791 set_bitfield (str, flags, 1, ARRAY_SIZE (flags), lineno);
40fb9820
L
792 }
793 }
794
795 output_cpu_flags (table, flags, ARRAY_SIZE (flags), macro,
796 comma, indent);
797}
798
799static void
800output_opcode_modifier (FILE *table, bitfield *modifier, unsigned int size)
801{
802 unsigned int i;
803
804 fprintf (table, " { ");
805
806 for (i = 0; i < size - 1; i++)
807 {
10632b79
L
808 if (((i + 1) % 20) != 0)
809 fprintf (table, "%d, ", modifier[i].value);
810 else
811 fprintf (table, "%d,", modifier[i].value);
40fb9820
L
812 if (((i + 1) % 20) == 0)
813 fprintf (table, "\n ");
814 }
815
816 fprintf (table, "%d },\n", modifier[i].value);
817}
818
819static void
bd5295b2 820process_i386_opcode_modifier (FILE *table, char *mod, int lineno)
40fb9820
L
821{
822 char *str, *next, *last;
823 bitfield modifiers [ARRAY_SIZE (opcode_modifiers)];
824
825 /* Copy the default opcode modifier. */
826 memcpy (modifiers, opcode_modifiers, sizeof (modifiers));
827
828 if (strcmp (mod, "0"))
829 {
830 last = mod + strlen (mod);
831 for (next = mod; next && next < last; )
832 {
c587b3f9 833 str = next_field (next, '|', &next, last);
40fb9820 834 if (str)
8a9036a4
L
835 set_bitfield (str, modifiers, 1, ARRAY_SIZE (modifiers),
836 lineno);
40fb9820
L
837 }
838 }
839 output_opcode_modifier (table, modifiers, ARRAY_SIZE (modifiers));
840}
841
842static void
843output_operand_type (FILE *table, bitfield *types, unsigned int size,
844 int macro, const char *indent)
845{
846 unsigned int i;
847
848 fprintf (table, "{ { ");
849
850 for (i = 0; i < size - 1; i++)
851 {
10632b79
L
852 if (((i + 1) % 20) != 0)
853 fprintf (table, "%d, ", types[i].value);
854 else
855 fprintf (table, "%d,", types[i].value);
40fb9820
L
856 if (((i + 1) % 20) == 0)
857 {
858 /* We need \\ for macro. */
859 if (macro)
10632b79 860 fprintf (table, " \\\n%s", indent);
40fb9820
L
861 else
862 fprintf (table, "\n%s", indent);
863 }
864 }
865
866 fprintf (table, "%d } }", types[i].value);
867}
868
869static void
870process_i386_operand_type (FILE *table, char *op, int macro,
bd5295b2 871 const char *indent, int lineno)
40fb9820
L
872{
873 char *str, *next, *last;
874 bitfield types [ARRAY_SIZE (operand_types)];
875
876 /* Copy the default operand type. */
877 memcpy (types, operand_types, sizeof (types));
878
879 if (strcmp (op, "0"))
880 {
881 last = op + strlen (op);
882 for (next = op; next && next < last; )
883 {
c587b3f9 884 str = next_field (next, '|', &next, last);
40fb9820 885 if (str)
8a9036a4 886 set_bitfield (str, types, 1, ARRAY_SIZE (types), lineno);
40fb9820
L
887 }
888 }
889 output_operand_type (table, types, ARRAY_SIZE (types), macro,
890 indent);
891}
892
c587b3f9
L
893static void
894output_i386_opcode (FILE *table, const char *name, char *str,
bd5295b2 895 char *last, int lineno)
c587b3f9
L
896{
897 unsigned int i;
898 char *operands, *base_opcode, *extension_opcode, *opcode_length;
899 char *cpu_flags, *opcode_modifier, *operand_types [MAX_OPERANDS];
900
901 /* Find number of operands. */
902 operands = next_field (str, ',', &str, last);
903
904 /* Find base_opcode. */
905 base_opcode = next_field (str, ',', &str, last);
906
907 /* Find extension_opcode. */
908 extension_opcode = next_field (str, ',', &str, last);
909
910 /* Find opcode_length. */
911 opcode_length = next_field (str, ',', &str, last);
912
913 /* Find cpu_flags. */
914 cpu_flags = next_field (str, ',', &str, last);
915
916 /* Find opcode_modifier. */
917 opcode_modifier = next_field (str, ',', &str, last);
918
919 /* Remove the first {. */
920 str = remove_leading_whitespaces (str);
921 if (*str != '{')
922 abort ();
923 str = remove_leading_whitespaces (str + 1);
924
925 i = strlen (str);
926
927 /* There are at least "X}". */
928 if (i < 2)
929 abort ();
930
931 /* Remove trailing white spaces and }. */
932 do
933 {
934 i--;
935 if (ISSPACE (str[i]) || str[i] == '}')
936 str[i] = '\0';
937 else
938 break;
939 }
940 while (i != 0);
941
942 last = str + i;
943
944 /* Find operand_types. */
945 for (i = 0; i < ARRAY_SIZE (operand_types); i++)
946 {
947 if (str >= last)
948 {
949 operand_types [i] = NULL;
950 break;
951 }
952
953 operand_types [i] = next_field (str, ',', &str, last);
954 if (*operand_types[i] == '0')
955 {
956 if (i != 0)
957 operand_types[i] = NULL;
958 break;
959 }
960 }
961
962 fprintf (table, " { \"%s\", %s, %s, %s, %s,\n",
963 name, operands, base_opcode, extension_opcode,
964 opcode_length);
965
bd5295b2 966 process_i386_cpu_flag (table, cpu_flags, 0, ",", " ", lineno);
c587b3f9 967
bd5295b2 968 process_i386_opcode_modifier (table, opcode_modifier, lineno);
c587b3f9
L
969
970 fprintf (table, " { ");
971
972 for (i = 0; i < ARRAY_SIZE (operand_types); i++)
973 {
974 if (operand_types[i] == NULL || *operand_types[i] == '0')
975 {
976 if (i == 0)
bd5295b2 977 process_i386_operand_type (table, "0", 0, "\t ", lineno);
c587b3f9
L
978 break;
979 }
980
981 if (i != 0)
982 fprintf (table, ",\n ");
983
984 process_i386_operand_type (table, operand_types[i], 0,
bd5295b2 985 "\t ", lineno);
c587b3f9
L
986 }
987 fprintf (table, " } },\n");
988}
989
990struct opcode_hash_entry
991{
992 struct opcode_hash_entry *next;
993 char *name;
994 char *opcode;
bd5295b2 995 int lineno;
c587b3f9
L
996};
997
998/* Calculate the hash value of an opcode hash entry P. */
999
1000static hashval_t
1001opcode_hash_hash (const void *p)
1002{
1003 struct opcode_hash_entry *entry = (struct opcode_hash_entry *) p;
1004 return htab_hash_string (entry->name);
1005}
1006
1007/* Compare a string Q against an opcode hash entry P. */
1008
1009static int
1010opcode_hash_eq (const void *p, const void *q)
1011{
1012 struct opcode_hash_entry *entry = (struct opcode_hash_entry *) p;
1013 const char *name = (const char *) q;
1014 return strcmp (name, entry->name) == 0;
1015}
1016
40b8e679 1017static void
72ffa0fb 1018process_i386_opcodes (FILE *table)
40b8e679 1019{
3d4d5afa 1020 FILE *fp;
40b8e679 1021 char buf[2048];
c587b3f9
L
1022 unsigned int i, j;
1023 char *str, *p, *last, *name;
1024 struct opcode_hash_entry **hash_slot, **entry, *next;
1025 htab_t opcode_hash_table;
1026 struct opcode_hash_entry **opcode_array;
1027 unsigned int opcode_array_size = 1024;
bd5295b2 1028 int lineno = 0;
40b8e679 1029
3d4d5afa
L
1030 filename = "i386-opc.tbl";
1031 fp = fopen (filename, "r");
1032
40b8e679 1033 if (fp == NULL)
34edb9ad 1034 fail (_("can't find i386-opc.tbl for reading, errno = %s\n"),
40fb9820 1035 xstrerror (errno));
40b8e679 1036
c587b3f9
L
1037 i = 0;
1038 opcode_array = (struct opcode_hash_entry **)
1039 xmalloc (sizeof (*opcode_array) * opcode_array_size);
1040
1041 opcode_hash_table = htab_create_alloc (16, opcode_hash_hash,
1042 opcode_hash_eq, NULL,
1043 xcalloc, free);
1044
34edb9ad 1045 fprintf (table, "\n/* i386 opcode table. */\n\n");
d3ce72d0 1046 fprintf (table, "const insn_template i386_optab[] =\n{\n");
40b8e679 1047
c587b3f9 1048 /* Put everything on opcode array. */
40b8e679
L
1049 while (!feof (fp))
1050 {
1051 if (fgets (buf, sizeof (buf), fp) == NULL)
1052 break;
1053
3d4d5afa
L
1054 lineno++;
1055
40b8e679
L
1056 p = remove_leading_whitespaces (buf);
1057
1058 /* Skip comments. */
1059 str = strstr (p, "//");
1060 if (str != NULL)
1061 str[0] = '\0';
1062
1063 /* Remove trailing white spaces. */
1064 remove_trailing_whitespaces (p);
1065
1066 switch (p[0])
1067 {
1068 case '#':
c587b3f9 1069 /* Ignore comments. */
40b8e679
L
1070 case '\0':
1071 continue;
1072 break;
1073 default:
1074 break;
1075 }
1076
1077 last = p + strlen (p);
1078
1079 /* Find name. */
c587b3f9 1080 name = next_field (p, ',', &str, last);
40b8e679 1081
c587b3f9
L
1082 /* Get the slot in hash table. */
1083 hash_slot = (struct opcode_hash_entry **)
1084 htab_find_slot_with_hash (opcode_hash_table, name,
1085 htab_hash_string (name),
1086 INSERT);
40b8e679 1087
c587b3f9 1088 if (*hash_slot == NULL)
40b8e679 1089 {
c587b3f9
L
1090 /* It is the new one. Put it on opcode array. */
1091 if (i >= opcode_array_size)
40b8e679 1092 {
c587b3f9
L
1093 /* Grow the opcode array when needed. */
1094 opcode_array_size += 1024;
1095 opcode_array = (struct opcode_hash_entry **)
1096 xrealloc (opcode_array,
1097 sizeof (*opcode_array) * opcode_array_size);
40b8e679
L
1098 }
1099
c587b3f9
L
1100 opcode_array[i] = (struct opcode_hash_entry *)
1101 xmalloc (sizeof (struct opcode_hash_entry));
1102 opcode_array[i]->next = NULL;
1103 opcode_array[i]->name = xstrdup (name);
1104 opcode_array[i]->opcode = xstrdup (str);
bd5295b2 1105 opcode_array[i]->lineno = lineno;
c587b3f9
L
1106 *hash_slot = opcode_array[i];
1107 i++;
40b8e679 1108 }
c587b3f9 1109 else
40b8e679 1110 {
c587b3f9
L
1111 /* Append it to the existing one. */
1112 entry = hash_slot;
1113 while ((*entry) != NULL)
1114 entry = &(*entry)->next;
1115 *entry = (struct opcode_hash_entry *)
1116 xmalloc (sizeof (struct opcode_hash_entry));
1117 (*entry)->next = NULL;
1118 (*entry)->name = (*hash_slot)->name;
1119 (*entry)->opcode = xstrdup (str);
bd5295b2 1120 (*entry)->lineno = lineno;
c587b3f9
L
1121 }
1122 }
40b8e679 1123
c587b3f9
L
1124 /* Process opcode array. */
1125 for (j = 0; j < i; j++)
1126 {
1127 for (next = opcode_array[j]; next; next = next->next)
1128 {
1129 name = next->name;
1130 str = next->opcode;
bd5295b2 1131 lineno = next->lineno;
c587b3f9 1132 last = str + strlen (str);
bd5295b2 1133 output_i386_opcode (table, name, str, last, lineno);
40b8e679 1134 }
40b8e679
L
1135 }
1136
34edb9ad
L
1137 fclose (fp);
1138
4dffcebc 1139 fprintf (table, " { NULL, 0, 0, 0, 0,\n");
40fb9820 1140
bd5295b2 1141 process_i386_cpu_flag (table, "0", 0, ",", " ", -1);
40fb9820 1142
bd5295b2 1143 process_i386_opcode_modifier (table, "0", -1);
29c048b6 1144
40fb9820 1145 fprintf (table, " { ");
bd5295b2 1146 process_i386_operand_type (table, "0", 0, "\t ", -1);
40fb9820
L
1147 fprintf (table, " } }\n");
1148
34edb9ad 1149 fprintf (table, "};\n");
40b8e679
L
1150}
1151
1152static void
72ffa0fb 1153process_i386_registers (FILE *table)
40b8e679 1154{
3d4d5afa 1155 FILE *fp;
40b8e679
L
1156 char buf[2048];
1157 char *str, *p, *last;
1158 char *reg_name, *reg_type, *reg_flags, *reg_num;
a60de03c 1159 char *dw2_32_num, *dw2_64_num;
bd5295b2 1160 int lineno = 0;
40b8e679 1161
3d4d5afa
L
1162 filename = "i386-reg.tbl";
1163 fp = fopen (filename, "r");
40b8e679 1164 if (fp == NULL)
34edb9ad 1165 fail (_("can't find i386-reg.tbl for reading, errno = %s\n"),
40fb9820 1166 xstrerror (errno));
40b8e679 1167
34edb9ad
L
1168 fprintf (table, "\n/* i386 register table. */\n\n");
1169 fprintf (table, "const reg_entry i386_regtab[] =\n{\n");
40b8e679
L
1170
1171 while (!feof (fp))
1172 {
1173 if (fgets (buf, sizeof (buf), fp) == NULL)
1174 break;
1175
3d4d5afa
L
1176 lineno++;
1177
40b8e679
L
1178 p = remove_leading_whitespaces (buf);
1179
1180 /* Skip comments. */
1181 str = strstr (p, "//");
1182 if (str != NULL)
1183 str[0] = '\0';
1184
1185 /* Remove trailing white spaces. */
1186 remove_trailing_whitespaces (p);
1187
1188 switch (p[0])
1189 {
1190 case '#':
34edb9ad 1191 fprintf (table, "%s\n", p);
40b8e679
L
1192 case '\0':
1193 continue;
1194 break;
1195 default:
1196 break;
1197 }
1198
1199 last = p + strlen (p);
1200
1201 /* Find reg_name. */
c587b3f9 1202 reg_name = next_field (p, ',', &str, last);
40b8e679
L
1203
1204 /* Find reg_type. */
c587b3f9 1205 reg_type = next_field (str, ',', &str, last);
40b8e679
L
1206
1207 /* Find reg_flags. */
c587b3f9 1208 reg_flags = next_field (str, ',', &str, last);
40b8e679
L
1209
1210 /* Find reg_num. */
c587b3f9 1211 reg_num = next_field (str, ',', &str, last);
a60de03c 1212
40fb9820
L
1213 fprintf (table, " { \"%s\",\n ", reg_name);
1214
bd5295b2 1215 process_i386_operand_type (table, reg_type, 0, "\t", lineno);
40fb9820 1216
a60de03c 1217 /* Find 32-bit Dwarf2 register number. */
c587b3f9 1218 dw2_32_num = next_field (str, ',', &str, last);
a60de03c
JB
1219
1220 /* Find 64-bit Dwarf2 register number. */
c587b3f9 1221 dw2_64_num = next_field (str, ',', &str, last);
a60de03c
JB
1222
1223 fprintf (table, ",\n %s, %s, { %s, %s } },\n",
1224 reg_flags, reg_num, dw2_32_num, dw2_64_num);
40b8e679
L
1225 }
1226
34edb9ad
L
1227 fclose (fp);
1228
1229 fprintf (table, "};\n");
40b8e679 1230
34edb9ad 1231 fprintf (table, "\nconst unsigned int i386_regtab_size = ARRAY_SIZE (i386_regtab);\n");
40b8e679
L
1232}
1233
40fb9820
L
1234static void
1235process_i386_initializers (void)
1236{
1237 unsigned int i;
1238 FILE *fp = fopen ("i386-init.h", "w");
1239 char *init;
1240
1241 if (fp == NULL)
1242 fail (_("can't create i386-init.h, errno = %s\n"),
1243 xstrerror (errno));
1244
1245 process_copyright (fp);
1246
1247 for (i = 0; i < ARRAY_SIZE (cpu_flag_init); i++)
1248 {
1249 fprintf (fp, "\n#define %s \\\n", cpu_flag_init[i].name);
1250 init = xstrdup (cpu_flag_init[i].init);
bd5295b2 1251 process_i386_cpu_flag (fp, init, 1, "", " ", -1);
40fb9820
L
1252 free (init);
1253 }
1254
1255 for (i = 0; i < ARRAY_SIZE (operand_type_init); i++)
1256 {
1257 fprintf (fp, "\n\n#define %s \\\n ", operand_type_init[i].name);
1258 init = xstrdup (operand_type_init[i].init);
bd5295b2 1259 process_i386_operand_type (fp, init, 1, " ", -1);
40fb9820
L
1260 free (init);
1261 }
1262 fprintf (fp, "\n");
1263
1264 fclose (fp);
1265}
1266
40b8e679
L
1267/* Program options. */
1268#define OPTION_SRCDIR 200
1269
29c048b6 1270struct option long_options[] =
40b8e679
L
1271{
1272 {"srcdir", required_argument, NULL, OPTION_SRCDIR},
1273 {"debug", no_argument, NULL, 'd'},
1274 {"version", no_argument, NULL, 'V'},
1275 {"help", no_argument, NULL, 'h'},
1276 {0, no_argument, NULL, 0}
1277};
1278
1279static void
1280print_version (void)
1281{
1282 printf ("%s: version 1.0\n", program_name);
1283 xexit (0);
1284}
1285
1286static void
1287usage (FILE * stream, int status)
1288{
1289 fprintf (stream, "Usage: %s [-V | --version] [-d | --debug] [--srcdir=dirname] [--help]\n",
1290 program_name);
1291 xexit (status);
1292}
1293
1294int
1295main (int argc, char **argv)
1296{
1297 extern int chdir (char *);
1298 char *srcdir = NULL;
8b40d594 1299 int c;
72ffa0fb 1300 FILE *table;
29c048b6 1301
40b8e679
L
1302 program_name = *argv;
1303 xmalloc_set_program_name (program_name);
1304
1305 while ((c = getopt_long (argc, argv, "vVdh", long_options, 0)) != EOF)
1306 switch (c)
1307 {
1308 case OPTION_SRCDIR:
1309 srcdir = optarg;
1310 break;
1311 case 'V':
1312 case 'v':
1313 print_version ();
1314 break;
1315 case 'd':
1316 debug = 1;
1317 break;
1318 case 'h':
1319 case '?':
1320 usage (stderr, 0);
1321 default:
1322 case 0:
1323 break;
1324 }
1325
1326 if (optind != argc)
1327 usage (stdout, 1);
1328
29c048b6 1329 if (srcdir != NULL)
40b8e679
L
1330 if (chdir (srcdir) != 0)
1331 fail (_("unable to change directory to \"%s\", errno = %s\n"),
40fb9820
L
1332 srcdir, xstrerror (errno));
1333
1334 /* Check the unused bitfield in i386_cpu_flags. */
1335#ifndef CpuUnused
8b40d594
L
1336 c = CpuNumOfBits - CpuMax - 1;
1337 if (c)
1338 fail (_("%d unused bits in i386_cpu_flags.\n"), c);
40fb9820
L
1339#endif
1340
1341 /* Check the unused bitfield in i386_operand_type. */
1342#ifndef OTUnused
8b40d594
L
1343 c = OTNumOfBits - OTMax - 1;
1344 if (c)
1345 fail (_("%d unused bits in i386_operand_type.\n"), c);
40fb9820
L
1346#endif
1347
1348 qsort (cpu_flags, ARRAY_SIZE (cpu_flags), sizeof (cpu_flags [0]),
1349 compare);
1350
1351 qsort (opcode_modifiers, ARRAY_SIZE (opcode_modifiers),
1352 sizeof (opcode_modifiers [0]), compare);
1353
1354 qsort (operand_types, ARRAY_SIZE (operand_types),
1355 sizeof (operand_types [0]), compare);
40b8e679 1356
34edb9ad
L
1357 table = fopen ("i386-tbl.h", "w");
1358 if (table == NULL)
40fb9820
L
1359 fail (_("can't create i386-tbl.h, errno = %s\n"),
1360 xstrerror (errno));
34edb9ad 1361
72ffa0fb 1362 process_copyright (table);
40b8e679 1363
72ffa0fb
L
1364 process_i386_opcodes (table);
1365 process_i386_registers (table);
40fb9820 1366 process_i386_initializers ();
40b8e679 1367
34edb9ad
L
1368 fclose (table);
1369
40b8e679
L
1370 exit (0);
1371}
This page took 0.494917 seconds and 4 git commands to generate.