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