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