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