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