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