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