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