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