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