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