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