blob: d3427207b3cd71842d0a6c9ba08f5f619b3547fd [file] [log] [blame]
Sean Callanan04cc3072009-12-19 02:59:52 +00001//===- X86RecognizableInstr.cpp - Disassembler instruction spec --*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file is part of the X86 Disassembler Emitter.
11// It contains the implementation of a single recognizable instruction.
12// Documentation for the disassembler emitter in general can be found in
13// X86DisasemblerEmitter.h.
14//
15//===----------------------------------------------------------------------===//
16
Sean Callanan04cc3072009-12-19 02:59:52 +000017#include "X86RecognizableInstr.h"
Chandler Carruth91d19d82012-12-04 10:37:14 +000018#include "X86DisassemblerShared.h"
Sean Callanan04cc3072009-12-19 02:59:52 +000019#include "X86ModRMFilters.h"
Sean Callanan04cc3072009-12-19 02:59:52 +000020#include "llvm/Support/ErrorHandling.h"
Sean Callanan04cc3072009-12-19 02:59:52 +000021#include <string>
22
23using namespace llvm;
24
Sean Callanandde9c122010-02-12 23:39:46 +000025#define MRM_MAPPING \
26 MAP(C1, 33) \
Chris Lattner140caa72010-02-13 00:41:14 +000027 MAP(C2, 34) \
28 MAP(C3, 35) \
29 MAP(C4, 36) \
30 MAP(C8, 37) \
31 MAP(C9, 38) \
Michael Liao95d944032013-04-11 04:52:28 +000032 MAP(CA, 39) \
33 MAP(CB, 40) \
34 MAP(E8, 41) \
35 MAP(F0, 42) \
36 MAP(F8, 45) \
37 MAP(F9, 46) \
38 MAP(D0, 47) \
39 MAP(D1, 48) \
40 MAP(D4, 49) \
41 MAP(D5, 50) \
42 MAP(D6, 51) \
43 MAP(D8, 52) \
44 MAP(D9, 53) \
45 MAP(DA, 54) \
46 MAP(DB, 55) \
47 MAP(DC, 56) \
48 MAP(DD, 57) \
49 MAP(DE, 58) \
50 MAP(DF, 59)
Sean Callanandde9c122010-02-12 23:39:46 +000051
Sean Callanan04cc3072009-12-19 02:59:52 +000052// A clone of X86 since we can't depend on something that is generated.
53namespace X86Local {
54 enum {
55 Pseudo = 0,
56 RawFrm = 1,
57 AddRegFrm = 2,
58 MRMDestReg = 3,
59 MRMDestMem = 4,
60 MRMSrcReg = 5,
61 MRMSrcMem = 6,
Craig Topperac172e22012-07-30 04:48:12 +000062 MRM0r = 16, MRM1r = 17, MRM2r = 18, MRM3r = 19,
Sean Callanan04cc3072009-12-19 02:59:52 +000063 MRM4r = 20, MRM5r = 21, MRM6r = 22, MRM7r = 23,
64 MRM0m = 24, MRM1m = 25, MRM2m = 26, MRM3m = 27,
65 MRM4m = 28, MRM5m = 29, MRM6m = 30, MRM7m = 31,
Sean Callanandde9c122010-02-12 23:39:46 +000066 MRMInitReg = 32,
Richard Trieu9208abd2012-07-18 23:04:22 +000067 RawFrmImm8 = 43,
68 RawFrmImm16 = 44,
Sean Callanandde9c122010-02-12 23:39:46 +000069#define MAP(from, to) MRM_##from = to,
70 MRM_MAPPING
71#undef MAP
72 lastMRM
Sean Callanan04cc3072009-12-19 02:59:52 +000073 };
Craig Topperac172e22012-07-30 04:48:12 +000074
Sean Callanan04cc3072009-12-19 02:59:52 +000075 enum {
76 TB = 1,
77 REP = 2,
78 D8 = 3, D9 = 4, DA = 5, DB = 6,
79 DC = 7, DD = 8, DE = 9, DF = 10,
80 XD = 11, XS = 12,
Chris Lattnerf7477e52010-02-12 02:06:33 +000081 T8 = 13, P_TA = 14,
Craig Topper9e3e38a2013-10-03 05:17:48 +000082 A6 = 15, A7 = 16, T8XD = 17, T8XS = 18, TAXD = 19,
83 XOP8 = 20, XOP9 = 21, XOPA = 22
Sean Callanan04cc3072009-12-19 02:59:52 +000084 };
85}
Sean Callanandde9c122010-02-12 23:39:46 +000086
87// If rows are added to the opcode extension tables, then corresponding entries
Craig Topperac172e22012-07-30 04:48:12 +000088// must be added here.
Sean Callanandde9c122010-02-12 23:39:46 +000089//
90// If the row corresponds to a single byte (i.e., 8f), then add an entry for
91// that byte to ONE_BYTE_EXTENSION_TABLES.
92//
Craig Topperac172e22012-07-30 04:48:12 +000093// If the row corresponds to two bytes where the first is 0f, add an entry for
Sean Callanandde9c122010-02-12 23:39:46 +000094// the second byte to TWO_BYTE_EXTENSION_TABLES.
95//
96// If the row corresponds to some other set of bytes, you will need to modify
97// the code in RecognizableInstr::emitDecodePath() as well, and add new prefixes
Craig Topperac172e22012-07-30 04:48:12 +000098// to the X86 TD files, except in two cases: if the first two bytes of such a
Sean Callanandde9c122010-02-12 23:39:46 +000099// new combination are 0f 38 or 0f 3a, you just have to add maps called
100// THREE_BYTE_38_EXTENSION_TABLES and THREE_BYTE_3A_EXTENSION_TABLES and add a
101// switch(Opcode) just below the case X86Local::T8: or case X86Local::TA: line
102// in RecognizableInstr::emitDecodePath().
103
Sean Callanan04cc3072009-12-19 02:59:52 +0000104#define ONE_BYTE_EXTENSION_TABLES \
105 EXTENSION_TABLE(80) \
106 EXTENSION_TABLE(81) \
107 EXTENSION_TABLE(82) \
108 EXTENSION_TABLE(83) \
109 EXTENSION_TABLE(8f) \
110 EXTENSION_TABLE(c0) \
111 EXTENSION_TABLE(c1) \
112 EXTENSION_TABLE(c6) \
113 EXTENSION_TABLE(c7) \
114 EXTENSION_TABLE(d0) \
115 EXTENSION_TABLE(d1) \
116 EXTENSION_TABLE(d2) \
117 EXTENSION_TABLE(d3) \
118 EXTENSION_TABLE(f6) \
119 EXTENSION_TABLE(f7) \
120 EXTENSION_TABLE(fe) \
121 EXTENSION_TABLE(ff)
Craig Topperac172e22012-07-30 04:48:12 +0000122
Sean Callanan04cc3072009-12-19 02:59:52 +0000123#define TWO_BYTE_EXTENSION_TABLES \
124 EXTENSION_TABLE(00) \
125 EXTENSION_TABLE(01) \
Kay Tiong Khooab588ef2013-02-12 00:19:12 +0000126 EXTENSION_TABLE(0d) \
Sean Callanan04cc3072009-12-19 02:59:52 +0000127 EXTENSION_TABLE(18) \
128 EXTENSION_TABLE(71) \
129 EXTENSION_TABLE(72) \
130 EXTENSION_TABLE(73) \
131 EXTENSION_TABLE(ae) \
Sean Callanan04cc3072009-12-19 02:59:52 +0000132 EXTENSION_TABLE(ba) \
133 EXTENSION_TABLE(c7)
Sean Callanan04cc3072009-12-19 02:59:52 +0000134
Craig Topper27ad1252011-10-15 20:46:47 +0000135#define THREE_BYTE_38_EXTENSION_TABLES \
136 EXTENSION_TABLE(F3)
137
Craig Topper9e3e38a2013-10-03 05:17:48 +0000138#define XOP9_MAP_EXTENSION_TABLES \
139 EXTENSION_TABLE(01) \
140 EXTENSION_TABLE(02)
141
Sean Callanan04cc3072009-12-19 02:59:52 +0000142using namespace X86Disassembler;
143
144/// needsModRMForDecode - Indicates whether a particular instruction requires a
Craig Topperac172e22012-07-30 04:48:12 +0000145/// ModR/M byte for the instruction to be properly decoded. For example, a
Sean Callanan04cc3072009-12-19 02:59:52 +0000146/// MRMDestReg instruction needs the Mod field in the ModR/M byte to be set to
147/// 0b11.
148///
149/// @param form - The form of the instruction.
150/// @return - true if the form implies that a ModR/M byte is required, false
151/// otherwise.
152static bool needsModRMForDecode(uint8_t form) {
153 if (form == X86Local::MRMDestReg ||
154 form == X86Local::MRMDestMem ||
155 form == X86Local::MRMSrcReg ||
156 form == X86Local::MRMSrcMem ||
157 (form >= X86Local::MRM0r && form <= X86Local::MRM7r) ||
158 (form >= X86Local::MRM0m && form <= X86Local::MRM7m))
159 return true;
160 else
161 return false;
162}
163
164/// isRegFormat - Indicates whether a particular form requires the Mod field of
165/// the ModR/M byte to be 0b11.
166///
167/// @param form - The form of the instruction.
168/// @return - true if the form implies that Mod must be 0b11, false
169/// otherwise.
170static bool isRegFormat(uint8_t form) {
171 if (form == X86Local::MRMDestReg ||
172 form == X86Local::MRMSrcReg ||
173 (form >= X86Local::MRM0r && form <= X86Local::MRM7r))
174 return true;
175 else
176 return false;
177}
178
179/// byteFromBitsInit - Extracts a value at most 8 bits in width from a BitsInit.
180/// Useful for switch statements and the like.
181///
182/// @param init - A reference to the BitsInit to be decoded.
183/// @return - The field, with the first bit in the BitsInit as the lowest
184/// order bit.
David Greeneaf8ee2c2011-07-29 22:43:06 +0000185static uint8_t byteFromBitsInit(BitsInit &init) {
Sean Callanan04cc3072009-12-19 02:59:52 +0000186 int width = init.getNumBits();
187
188 assert(width <= 8 && "Field is too large for uint8_t!");
189
190 int index;
191 uint8_t mask = 0x01;
192
193 uint8_t ret = 0;
194
195 for (index = 0; index < width; index++) {
David Greeneaf8ee2c2011-07-29 22:43:06 +0000196 if (static_cast<BitInit*>(init.getBit(index))->getValue())
Sean Callanan04cc3072009-12-19 02:59:52 +0000197 ret |= mask;
198
199 mask <<= 1;
200 }
201
202 return ret;
203}
204
205/// byteFromRec - Extract a value at most 8 bits in with from a Record given the
206/// name of the field.
207///
208/// @param rec - The record from which to extract the value.
209/// @param name - The name of the field in the record.
210/// @return - The field, as translated by byteFromBitsInit().
211static uint8_t byteFromRec(const Record* rec, const std::string &name) {
David Greeneaf8ee2c2011-07-29 22:43:06 +0000212 BitsInit* bits = rec->getValueAsBitsInit(name);
Sean Callanan04cc3072009-12-19 02:59:52 +0000213 return byteFromBitsInit(*bits);
214}
215
216RecognizableInstr::RecognizableInstr(DisassemblerTables &tables,
217 const CodeGenInstruction &insn,
218 InstrUID uid) {
219 UID = uid;
220
221 Rec = insn.TheDef;
222 Name = Rec->getName();
223 Spec = &tables.specForUID(UID);
Craig Topperac172e22012-07-30 04:48:12 +0000224
Sean Callanan04cc3072009-12-19 02:59:52 +0000225 if (!Rec->isSubClassOf("X86Inst")) {
226 ShouldBeEmitted = false;
227 return;
228 }
Craig Topperac172e22012-07-30 04:48:12 +0000229
Sean Callanan04cc3072009-12-19 02:59:52 +0000230 Prefix = byteFromRec(Rec, "Prefix");
231 Opcode = byteFromRec(Rec, "Opcode");
232 Form = byteFromRec(Rec, "FormBits");
233 SegOvr = byteFromRec(Rec, "SegOvrBits");
Craig Topperac172e22012-07-30 04:48:12 +0000234
Sean Callanan04cc3072009-12-19 02:59:52 +0000235 HasOpSizePrefix = Rec->getValueAsBit("hasOpSizePrefix");
Craig Topper6491c802012-02-27 01:54:29 +0000236 HasAdSizePrefix = Rec->getValueAsBit("hasAdSizePrefix");
Sean Callanan04cc3072009-12-19 02:59:52 +0000237 HasREX_WPrefix = Rec->getValueAsBit("hasREX_WPrefix");
Sean Callananc3fd5232011-03-15 01:23:15 +0000238 HasVEXPrefix = Rec->getValueAsBit("hasVEXPrefix");
Bruno Cardoso Lopesc2f87b72010-06-08 22:51:23 +0000239 HasVEX_4VPrefix = Rec->getValueAsBit("hasVEX_4VPrefix");
Craig Topperaea148c2011-10-16 07:55:05 +0000240 HasVEX_4VOp3Prefix = Rec->getValueAsBit("hasVEX_4VOp3Prefix");
Sean Callananc3fd5232011-03-15 01:23:15 +0000241 HasVEX_WPrefix = Rec->getValueAsBit("hasVEX_WPrefix");
Craig Topper03a0bed2011-12-30 05:20:36 +0000242 HasMemOp4Prefix = Rec->getValueAsBit("hasMemOp4Prefix");
Craig Topperf18c8962011-10-04 06:30:42 +0000243 IgnoresVEX_L = Rec->getValueAsBit("ignoresVEX_L");
Elena Demikhovsky003e7d72013-07-28 08:28:38 +0000244 HasEVEXPrefix = Rec->getValueAsBit("hasEVEXPrefix");
245 HasEVEX_L2Prefix = Rec->getValueAsBit("hasEVEX_L2");
246 HasEVEX_K = Rec->getValueAsBit("hasEVEX_K");
247 HasEVEX_B = Rec->getValueAsBit("hasEVEX_B");
Sean Callanan04cc3072009-12-19 02:59:52 +0000248 HasLockPrefix = Rec->getValueAsBit("hasLockPrefix");
249 IsCodeGenOnly = Rec->getValueAsBit("isCodeGenOnly");
Craig Topperac172e22012-07-30 04:48:12 +0000250
Sean Callanan04cc3072009-12-19 02:59:52 +0000251 Name = Rec->getName();
252 AsmString = Rec->getValueAsString("AsmString");
Craig Topperac172e22012-07-30 04:48:12 +0000253
Chris Lattnerd8adec72010-11-01 04:03:32 +0000254 Operands = &insn.Operands.OperandList;
Craig Topperac172e22012-07-30 04:48:12 +0000255
Kevin Enderby54e09b42011-09-02 18:03:03 +0000256 IsSSE = (HasOpSizePrefix && (Name.find("16") == Name.npos)) ||
257 (Name.find("CRC32") != Name.npos);
Sean Callananc3fd5232011-03-15 01:23:15 +0000258 HasFROperands = hasFROperands();
Craig Topper3f23c1a2012-09-19 06:37:45 +0000259 HasVEX_LPrefix = Rec->getValueAsBit("hasVEX_L");
Craig Topper25ea4e52011-10-16 03:51:13 +0000260
Eli Friedman03180362011-07-16 02:41:28 +0000261 // Check for 64-bit inst which does not require REX
Craig Topper526adab2011-09-23 06:57:25 +0000262 Is32Bit = false;
Eli Friedman03180362011-07-16 02:41:28 +0000263 Is64Bit = false;
264 // FIXME: Is there some better way to check for In64BitMode?
265 std::vector<Record*> Predicates = Rec->getValueAsListOfDefs("Predicates");
266 for (unsigned i = 0, e = Predicates.size(); i != e; ++i) {
Craig Topper526adab2011-09-23 06:57:25 +0000267 if (Predicates[i]->getName().find("32Bit") != Name.npos) {
268 Is32Bit = true;
269 break;
270 }
Eli Friedman03180362011-07-16 02:41:28 +0000271 if (Predicates[i]->getName().find("64Bit") != Name.npos) {
272 Is64Bit = true;
273 break;
274 }
275 }
276 // FIXME: These instructions aren't marked as 64-bit in any way
Craig Topperac172e22012-07-30 04:48:12 +0000277 Is64Bit |= Rec->getName() == "JMP64pcrel32" ||
278 Rec->getName() == "MASKMOVDQU64" ||
279 Rec->getName() == "POPFS64" ||
280 Rec->getName() == "POPGS64" ||
281 Rec->getName() == "PUSHFS64" ||
Eli Friedman03180362011-07-16 02:41:28 +0000282 Rec->getName() == "PUSHGS64" ||
283 Rec->getName() == "REX64_PREFIX" ||
Craig Topperac172e22012-07-30 04:48:12 +0000284 Rec->getName().find("MOV64") != Name.npos ||
Eli Friedman03180362011-07-16 02:41:28 +0000285 Rec->getName().find("PUSH64") != Name.npos ||
286 Rec->getName().find("POP64") != Name.npos;
287
Sean Callanan04cc3072009-12-19 02:59:52 +0000288 ShouldBeEmitted = true;
289}
Craig Topperac172e22012-07-30 04:48:12 +0000290
Sean Callanan04cc3072009-12-19 02:59:52 +0000291void RecognizableInstr::processInstr(DisassemblerTables &tables,
Craig Topperf7755df2012-07-12 06:52:41 +0000292 const CodeGenInstruction &insn,
293 InstrUID uid)
Sean Callanan04cc3072009-12-19 02:59:52 +0000294{
Daniel Dunbar5661c0c2010-05-20 20:20:32 +0000295 // Ignore "asm parser only" instructions.
296 if (insn.TheDef->getValueAsBit("isAsmParserOnly"))
297 return;
Craig Topperac172e22012-07-30 04:48:12 +0000298
Sean Callanan04cc3072009-12-19 02:59:52 +0000299 RecognizableInstr recogInstr(tables, insn, uid);
Craig Topperac172e22012-07-30 04:48:12 +0000300
Sean Callanan04cc3072009-12-19 02:59:52 +0000301 recogInstr.emitInstructionSpecifier(tables);
Craig Topperac172e22012-07-30 04:48:12 +0000302
Sean Callanan04cc3072009-12-19 02:59:52 +0000303 if (recogInstr.shouldBeEmitted())
304 recogInstr.emitDecodePath(tables);
305}
306
Elena Demikhovsky003e7d72013-07-28 08:28:38 +0000307#define EVEX_KB(n) (HasEVEX_K && HasEVEX_B? n##_K_B : \
308 (HasEVEX_K? n##_K : (HasEVEX_B ? n##_B : n)))
309
Sean Callanan04cc3072009-12-19 02:59:52 +0000310InstructionContext RecognizableInstr::insnContext() const {
311 InstructionContext insnContext;
312
Elena Demikhovsky003e7d72013-07-28 08:28:38 +0000313 if (HasEVEXPrefix) {
314 if (HasVEX_LPrefix && HasEVEX_L2Prefix) {
Craig Topper9469e902013-07-28 21:28:02 +0000315 errs() << "Don't support VEX.L if EVEX_L2 is enabled: " << Name << "\n";
316 llvm_unreachable("Don't support VEX.L if EVEX_L2 is enabled");
Elena Demikhovsky003e7d72013-07-28 08:28:38 +0000317 }
318 // VEX_L & VEX_W
319 if (HasVEX_LPrefix && HasVEX_WPrefix) {
320 if (HasOpSizePrefix)
321 insnContext = EVEX_KB(IC_EVEX_L_W_OPSIZE);
322 else if (Prefix == X86Local::XS || Prefix == X86Local::T8XS)
323 insnContext = EVEX_KB(IC_EVEX_L_W_XS);
324 else if (Prefix == X86Local::XD || Prefix == X86Local::T8XD ||
325 Prefix == X86Local::TAXD)
326 insnContext = EVEX_KB(IC_EVEX_L_W_XD);
327 else
328 insnContext = EVEX_KB(IC_EVEX_L_W);
329 } else if (HasVEX_LPrefix) {
330 // VEX_L
331 if (HasOpSizePrefix)
332 insnContext = EVEX_KB(IC_EVEX_L_OPSIZE);
333 else if (Prefix == X86Local::XS || Prefix == X86Local::T8XS)
334 insnContext = EVEX_KB(IC_EVEX_L_XS);
335 else if (Prefix == X86Local::XD || Prefix == X86Local::T8XD ||
336 Prefix == X86Local::TAXD)
337 insnContext = EVEX_KB(IC_EVEX_L_XD);
338 else
339 insnContext = EVEX_KB(IC_EVEX_L);
340 }
341 else if (HasEVEX_L2Prefix && HasVEX_WPrefix) {
342 // EVEX_L2 & VEX_W
343 if (HasOpSizePrefix)
344 insnContext = EVEX_KB(IC_EVEX_L2_W_OPSIZE);
345 else if (Prefix == X86Local::XS || Prefix == X86Local::T8XS)
346 insnContext = EVEX_KB(IC_EVEX_L2_W_XS);
347 else if (Prefix == X86Local::XD || Prefix == X86Local::T8XD ||
348 Prefix == X86Local::TAXD)
349 insnContext = EVEX_KB(IC_EVEX_L2_W_XD);
350 else
351 insnContext = EVEX_KB(IC_EVEX_L2_W);
352 } else if (HasEVEX_L2Prefix) {
353 // EVEX_L2
354 if (HasOpSizePrefix)
355 insnContext = EVEX_KB(IC_EVEX_L2_OPSIZE);
356 else if (Prefix == X86Local::XD || Prefix == X86Local::T8XD ||
357 Prefix == X86Local::TAXD)
358 insnContext = EVEX_KB(IC_EVEX_L2_XD);
359 else if (Prefix == X86Local::XS || Prefix == X86Local::T8XS)
360 insnContext = EVEX_KB(IC_EVEX_L2_XS);
361 else
362 insnContext = EVEX_KB(IC_EVEX_L2);
363 }
364 else if (HasVEX_WPrefix) {
365 // VEX_W
366 if (HasOpSizePrefix)
367 insnContext = EVEX_KB(IC_EVEX_W_OPSIZE);
368 else if (Prefix == X86Local::XS || Prefix == X86Local::T8XS)
369 insnContext = EVEX_KB(IC_EVEX_W_XS);
370 else if (Prefix == X86Local::XD || Prefix == X86Local::T8XD ||
371 Prefix == X86Local::TAXD)
372 insnContext = EVEX_KB(IC_EVEX_W_XD);
373 else
374 insnContext = EVEX_KB(IC_EVEX_W);
375 }
376 // No L, no W
377 else if (HasOpSizePrefix)
378 insnContext = EVEX_KB(IC_EVEX_OPSIZE);
379 else if (Prefix == X86Local::XD || Prefix == X86Local::T8XD ||
380 Prefix == X86Local::TAXD)
381 insnContext = EVEX_KB(IC_EVEX_XD);
382 else if (Prefix == X86Local::XS || Prefix == X86Local::T8XS)
383 insnContext = EVEX_KB(IC_EVEX_XS);
384 else
385 insnContext = EVEX_KB(IC_EVEX);
386 /// eof EVEX
387 } else if (HasVEX_4VPrefix || HasVEX_4VOp3Prefix|| HasVEXPrefix) {
Craig Topperf01f1b52011-11-06 23:04:08 +0000388 if (HasVEX_LPrefix && HasVEX_WPrefix) {
389 if (HasOpSizePrefix)
390 insnContext = IC_VEX_L_W_OPSIZE;
Elena Demikhovsky003e7d72013-07-28 08:28:38 +0000391 else if (Prefix == X86Local::XS || Prefix == X86Local::T8XS)
392 insnContext = IC_VEX_L_W_XS;
393 else if (Prefix == X86Local::XD || Prefix == X86Local::T8XD ||
394 Prefix == X86Local::TAXD)
395 insnContext = IC_VEX_L_W_XD;
Craig Topperf01f1b52011-11-06 23:04:08 +0000396 else
Elena Demikhovsky003e7d72013-07-28 08:28:38 +0000397 insnContext = IC_VEX_L_W;
Craig Topperf01f1b52011-11-06 23:04:08 +0000398 } else if (HasOpSizePrefix && HasVEX_LPrefix)
Sean Callananc3fd5232011-03-15 01:23:15 +0000399 insnContext = IC_VEX_L_OPSIZE;
400 else if (HasOpSizePrefix && HasVEX_WPrefix)
401 insnContext = IC_VEX_W_OPSIZE;
402 else if (HasOpSizePrefix)
403 insnContext = IC_VEX_OPSIZE;
Craig Topper96fa5972011-10-16 16:50:08 +0000404 else if (HasVEX_LPrefix &&
405 (Prefix == X86Local::XS || Prefix == X86Local::T8XS))
Sean Callananc3fd5232011-03-15 01:23:15 +0000406 insnContext = IC_VEX_L_XS;
Craig Topper980d5982011-10-23 07:34:00 +0000407 else if (HasVEX_LPrefix && (Prefix == X86Local::XD ||
408 Prefix == X86Local::T8XD ||
409 Prefix == X86Local::TAXD))
Sean Callananc3fd5232011-03-15 01:23:15 +0000410 insnContext = IC_VEX_L_XD;
Craig Topper96fa5972011-10-16 16:50:08 +0000411 else if (HasVEX_WPrefix &&
412 (Prefix == X86Local::XS || Prefix == X86Local::T8XS))
Sean Callananc3fd5232011-03-15 01:23:15 +0000413 insnContext = IC_VEX_W_XS;
Craig Topper980d5982011-10-23 07:34:00 +0000414 else if (HasVEX_WPrefix && (Prefix == X86Local::XD ||
415 Prefix == X86Local::T8XD ||
416 Prefix == X86Local::TAXD))
Sean Callananc3fd5232011-03-15 01:23:15 +0000417 insnContext = IC_VEX_W_XD;
418 else if (HasVEX_WPrefix)
419 insnContext = IC_VEX_W;
420 else if (HasVEX_LPrefix)
421 insnContext = IC_VEX_L;
Craig Topper980d5982011-10-23 07:34:00 +0000422 else if (Prefix == X86Local::XD || Prefix == X86Local::T8XD ||
423 Prefix == X86Local::TAXD)
Sean Callananc3fd5232011-03-15 01:23:15 +0000424 insnContext = IC_VEX_XD;
Craig Topper96fa5972011-10-16 16:50:08 +0000425 else if (Prefix == X86Local::XS || Prefix == X86Local::T8XS)
Sean Callananc3fd5232011-03-15 01:23:15 +0000426 insnContext = IC_VEX_XS;
427 else
428 insnContext = IC_VEX;
Eli Friedman03180362011-07-16 02:41:28 +0000429 } else if (Is64Bit || HasREX_WPrefix) {
Sean Callanan04cc3072009-12-19 02:59:52 +0000430 if (HasREX_WPrefix && HasOpSizePrefix)
431 insnContext = IC_64BIT_REXW_OPSIZE;
Craig Topper980d5982011-10-23 07:34:00 +0000432 else if (HasOpSizePrefix && (Prefix == X86Local::XD ||
433 Prefix == X86Local::T8XD ||
434 Prefix == X86Local::TAXD))
Craig Topper88cb33e2011-10-01 19:54:56 +0000435 insnContext = IC_64BIT_XD_OPSIZE;
Craig Topper96fa5972011-10-16 16:50:08 +0000436 else if (HasOpSizePrefix &&
437 (Prefix == X86Local::XS || Prefix == X86Local::T8XS))
Craig Toppera6978522011-10-11 04:34:23 +0000438 insnContext = IC_64BIT_XS_OPSIZE;
Sean Callanan04cc3072009-12-19 02:59:52 +0000439 else if (HasOpSizePrefix)
440 insnContext = IC_64BIT_OPSIZE;
Craig Topper6491c802012-02-27 01:54:29 +0000441 else if (HasAdSizePrefix)
442 insnContext = IC_64BIT_ADSIZE;
Craig Topper96fa5972011-10-16 16:50:08 +0000443 else if (HasREX_WPrefix &&
444 (Prefix == X86Local::XS || Prefix == X86Local::T8XS))
Sean Callanan04cc3072009-12-19 02:59:52 +0000445 insnContext = IC_64BIT_REXW_XS;
Craig Topper980d5982011-10-23 07:34:00 +0000446 else if (HasREX_WPrefix && (Prefix == X86Local::XD ||
447 Prefix == X86Local::T8XD ||
448 Prefix == X86Local::TAXD))
Sean Callanan04cc3072009-12-19 02:59:52 +0000449 insnContext = IC_64BIT_REXW_XD;
Craig Topper980d5982011-10-23 07:34:00 +0000450 else if (Prefix == X86Local::XD || Prefix == X86Local::T8XD ||
451 Prefix == X86Local::TAXD)
Sean Callanan04cc3072009-12-19 02:59:52 +0000452 insnContext = IC_64BIT_XD;
Craig Topper96fa5972011-10-16 16:50:08 +0000453 else if (Prefix == X86Local::XS || Prefix == X86Local::T8XS)
Sean Callanan04cc3072009-12-19 02:59:52 +0000454 insnContext = IC_64BIT_XS;
455 else if (HasREX_WPrefix)
456 insnContext = IC_64BIT_REXW;
457 else
458 insnContext = IC_64BIT;
459 } else {
Craig Topper980d5982011-10-23 07:34:00 +0000460 if (HasOpSizePrefix && (Prefix == X86Local::XD ||
461 Prefix == X86Local::T8XD ||
462 Prefix == X86Local::TAXD))
Craig Topper88cb33e2011-10-01 19:54:56 +0000463 insnContext = IC_XD_OPSIZE;
Craig Topper96fa5972011-10-16 16:50:08 +0000464 else if (HasOpSizePrefix &&
465 (Prefix == X86Local::XS || Prefix == X86Local::T8XS))
Craig Toppera6978522011-10-11 04:34:23 +0000466 insnContext = IC_XS_OPSIZE;
Kevin Enderby54e09b42011-09-02 18:03:03 +0000467 else if (HasOpSizePrefix)
Sean Callanan04cc3072009-12-19 02:59:52 +0000468 insnContext = IC_OPSIZE;
Craig Topper6491c802012-02-27 01:54:29 +0000469 else if (HasAdSizePrefix)
470 insnContext = IC_ADSIZE;
Craig Topper980d5982011-10-23 07:34:00 +0000471 else if (Prefix == X86Local::XD || Prefix == X86Local::T8XD ||
472 Prefix == X86Local::TAXD)
Sean Callanan04cc3072009-12-19 02:59:52 +0000473 insnContext = IC_XD;
Craig Topper96fa5972011-10-16 16:50:08 +0000474 else if (Prefix == X86Local::XS || Prefix == X86Local::T8XS ||
475 Prefix == X86Local::REP)
Sean Callanan04cc3072009-12-19 02:59:52 +0000476 insnContext = IC_XS;
477 else
478 insnContext = IC;
479 }
480
481 return insnContext;
482}
Craig Topperac172e22012-07-30 04:48:12 +0000483
Sean Callanan04cc3072009-12-19 02:59:52 +0000484RecognizableInstr::filter_ret RecognizableInstr::filter() const {
Sean Callananc3fd5232011-03-15 01:23:15 +0000485 ///////////////////
486 // FILTER_STRONG
487 //
Craig Topperac172e22012-07-30 04:48:12 +0000488
Sean Callanan04cc3072009-12-19 02:59:52 +0000489 // Filter out intrinsics
Craig Topperac172e22012-07-30 04:48:12 +0000490
Craig Topper6f4ad802012-07-30 05:39:34 +0000491 assert(Rec->isSubClassOf("X86Inst") && "Can only filter X86 instructions");
Craig Topperac172e22012-07-30 04:48:12 +0000492
Sean Callanan04cc3072009-12-19 02:59:52 +0000493 if (Form == X86Local::Pseudo ||
Craig Topper2658d892013-10-07 04:28:06 +0000494 (IsCodeGenOnly && Name.find("_REV") == Name.npos &&
495 Name.find("INC32") == Name.npos && Name.find("DEC32") == Name.npos))
Sean Callanan04cc3072009-12-19 02:59:52 +0000496 return FILTER_STRONG;
Craig Topperac172e22012-07-30 04:48:12 +0000497
Craig Topperac172e22012-07-30 04:48:12 +0000498
Kevin Enderby014e1cd2012-03-09 17:52:49 +0000499 // Filter out artificial instructions but leave in the LOCK_PREFIX so it is
500 // printed as a separate "instruction".
Craig Topperac172e22012-07-30 04:48:12 +0000501
Craig Topper75ffc5f2011-11-19 05:48:20 +0000502 if (Name.find("_Int") != Name.npos ||
Craig Topperc6b7ef62012-07-30 06:48:11 +0000503 Name.find("Int_") != Name.npos)
Sean Callananc3fd5232011-03-15 01:23:15 +0000504 return FILTER_STRONG;
505
506 // Filter out instructions with segment override prefixes.
507 // They're too messy to handle now and we'll special case them if needed.
Craig Topperac172e22012-07-30 04:48:12 +0000508
Sean Callananc3fd5232011-03-15 01:23:15 +0000509 if (SegOvr)
510 return FILTER_STRONG;
Craig Topperac172e22012-07-30 04:48:12 +0000511
Sean Callananc3fd5232011-03-15 01:23:15 +0000512
513 /////////////////
514 // FILTER_WEAK
515 //
516
Craig Topperac172e22012-07-30 04:48:12 +0000517
Sean Callanan04cc3072009-12-19 02:59:52 +0000518 // Filter out instructions with a LOCK prefix;
519 // prefer forms that do not have the prefix
520 if (HasLockPrefix)
521 return FILTER_WEAK;
Sean Callanan04cc3072009-12-19 02:59:52 +0000522
Sean Callananc3fd5232011-03-15 01:23:15 +0000523 // Filter out alternate forms of AVX instructions
524 if (Name.find("_alt") != Name.npos ||
525 Name.find("XrYr") != Name.npos ||
Craig Topper88cb33e2011-10-01 19:54:56 +0000526 (Name.find("r64r") != Name.npos && Name.find("r64r64") == Name.npos) ||
Sean Callananc3fd5232011-03-15 01:23:15 +0000527 Name.find("_64mr") != Name.npos ||
528 Name.find("Xrr") != Name.npos ||
529 Name.find("rr64") != Name.npos)
530 return FILTER_WEAK;
Sean Callanan04cc3072009-12-19 02:59:52 +0000531
532 // Special cases.
Dale Johannesen605acfe2010-09-07 18:10:56 +0000533
Sean Callanan04cc3072009-12-19 02:59:52 +0000534 if (Name.find("PCMPISTRI") != Name.npos && Name != "PCMPISTRI")
535 return FILTER_WEAK;
536 if (Name.find("PCMPESTRI") != Name.npos && Name != "PCMPESTRI")
537 return FILTER_WEAK;
538
539 if (Name.find("MOV") != Name.npos && Name.find("r0") != Name.npos)
540 return FILTER_WEAK;
Craig Topper07ad1b22013-10-07 07:19:47 +0000541 if (Name.find("MOVZ") != Name.npos && Name.find("MOVZX") == Name.npos &&
542 Name != "MOVZPQILo2PQIrr")
Sean Callanan04cc3072009-12-19 02:59:52 +0000543 return FILTER_WEAK;
544 if (Name.find("Fs") != Name.npos)
545 return FILTER_WEAK;
Craig Topper75ffc5f2011-11-19 05:48:20 +0000546 if (Name == "PUSH64i16" ||
Sean Callanan04cc3072009-12-19 02:59:52 +0000547 Name == "MOVPQI2QImr" ||
Sean Callananc3fd5232011-03-15 01:23:15 +0000548 Name == "VMOVPQI2QImr" ||
Sean Callanan04cc3072009-12-19 02:59:52 +0000549 Name == "MMX_MOVD64rrv164" ||
Sean Callanan04cc3072009-12-19 02:59:52 +0000550 Name == "MOV64ri64i32" ||
Craig Topper75ffc5f2011-11-19 05:48:20 +0000551 Name == "VMASKMOVDQU64" ||
552 Name == "VEXTRACTPSrr64" ||
553 Name == "VMOVQd64rr" ||
554 Name == "VMOVQs64rr")
Sean Callanan04cc3072009-12-19 02:59:52 +0000555 return FILTER_WEAK;
556
Stefanus Du Toit8811ad42013-06-18 17:08:10 +0000557 // XACQUIRE and XRELEASE reuse REPNE and REP respectively.
558 // For now, just prefer the REP versions.
559 if (Name == "XACQUIRE_PREFIX" ||
560 Name == "XRELEASE_PREFIX")
561 return FILTER_WEAK;
562
Sean Callanan04cc3072009-12-19 02:59:52 +0000563 if (HasFROperands && Name.find("MOV") != Name.npos &&
Craig Topperac172e22012-07-30 04:48:12 +0000564 ((Name.find("2") != Name.npos && Name.find("32") == Name.npos) ||
Sean Callanan04cc3072009-12-19 02:59:52 +0000565 (Name.find("to") != Name.npos)))
Craig Topperb58dc172012-07-30 05:10:05 +0000566 return FILTER_STRONG;
Sean Callanan04cc3072009-12-19 02:59:52 +0000567
568 return FILTER_NORMAL;
569}
Sean Callananc3fd5232011-03-15 01:23:15 +0000570
571bool RecognizableInstr::hasFROperands() const {
572 const std::vector<CGIOperandList::OperandInfo> &OperandList = *Operands;
573 unsigned numOperands = OperandList.size();
574
575 for (unsigned operandIndex = 0; operandIndex < numOperands; ++operandIndex) {
576 const std::string &recName = OperandList[operandIndex].Rec->getName();
Craig Topperac172e22012-07-30 04:48:12 +0000577
Sean Callananc3fd5232011-03-15 01:23:15 +0000578 if (recName.find("FR") != recName.npos)
579 return true;
580 }
581 return false;
582}
583
Craig Topperf7755df2012-07-12 06:52:41 +0000584void RecognizableInstr::handleOperand(bool optional, unsigned &operandIndex,
585 unsigned &physicalOperandIndex,
586 unsigned &numPhysicalOperands,
587 const unsigned *operandMapping,
588 OperandEncoding (*encodingFromString)
589 (const std::string&,
590 bool hasOpSizePrefix)) {
Sean Callanan04cc3072009-12-19 02:59:52 +0000591 if (optional) {
592 if (physicalOperandIndex >= numPhysicalOperands)
593 return;
594 } else {
595 assert(physicalOperandIndex < numPhysicalOperands);
596 }
Craig Topperac172e22012-07-30 04:48:12 +0000597
Sean Callanan04cc3072009-12-19 02:59:52 +0000598 while (operandMapping[operandIndex] != operandIndex) {
599 Spec->operands[operandIndex].encoding = ENCODING_DUP;
600 Spec->operands[operandIndex].type =
601 (OperandType)(TYPE_DUP0 + operandMapping[operandIndex]);
602 ++operandIndex;
603 }
Craig Topperac172e22012-07-30 04:48:12 +0000604
Sean Callanan04cc3072009-12-19 02:59:52 +0000605 const std::string &typeName = (*Operands)[operandIndex].Rec->getName();
Sean Callananc3fd5232011-03-15 01:23:15 +0000606
Sean Callanan04cc3072009-12-19 02:59:52 +0000607 Spec->operands[operandIndex].encoding = encodingFromString(typeName,
608 HasOpSizePrefix);
Craig Topperac172e22012-07-30 04:48:12 +0000609 Spec->operands[operandIndex].type = typeFromString(typeName,
Sean Callananc3fd5232011-03-15 01:23:15 +0000610 IsSSE,
611 HasREX_WPrefix,
612 HasOpSizePrefix);
Craig Topperac172e22012-07-30 04:48:12 +0000613
Sean Callanan04cc3072009-12-19 02:59:52 +0000614 ++operandIndex;
615 ++physicalOperandIndex;
616}
617
618void RecognizableInstr::emitInstructionSpecifier(DisassemblerTables &tables) {
619 Spec->name = Name;
Craig Topperac172e22012-07-30 04:48:12 +0000620
Craig Topper6f4ad802012-07-30 05:39:34 +0000621 if (!ShouldBeEmitted)
Sean Callanan04cc3072009-12-19 02:59:52 +0000622 return;
Craig Topperac172e22012-07-30 04:48:12 +0000623
Sean Callanan04cc3072009-12-19 02:59:52 +0000624 switch (filter()) {
625 case FILTER_WEAK:
626 Spec->filtered = true;
627 break;
628 case FILTER_STRONG:
629 ShouldBeEmitted = false;
630 return;
631 case FILTER_NORMAL:
632 break;
633 }
Craig Topperac172e22012-07-30 04:48:12 +0000634
Sean Callanan04cc3072009-12-19 02:59:52 +0000635 Spec->insnContext = insnContext();
Craig Topperac172e22012-07-30 04:48:12 +0000636
Chris Lattnerd8adec72010-11-01 04:03:32 +0000637 const std::vector<CGIOperandList::OperandInfo> &OperandList = *Operands;
Craig Topperac172e22012-07-30 04:48:12 +0000638
Sean Callanan04cc3072009-12-19 02:59:52 +0000639 unsigned numOperands = OperandList.size();
640 unsigned numPhysicalOperands = 0;
Craig Topperac172e22012-07-30 04:48:12 +0000641
Sean Callanan04cc3072009-12-19 02:59:52 +0000642 // operandMapping maps from operands in OperandList to their originals.
643 // If operandMapping[i] != i, then the entry is a duplicate.
644 unsigned operandMapping[X86_MAX_OPERANDS];
Craig Topper2ba766a2011-12-30 06:23:39 +0000645 assert(numOperands <= X86_MAX_OPERANDS && "X86_MAX_OPERANDS is not large enough");
Craig Topperac172e22012-07-30 04:48:12 +0000646
Craig Topperf7755df2012-07-12 06:52:41 +0000647 for (unsigned operandIndex = 0; operandIndex < numOperands; ++operandIndex) {
Sean Callanan04cc3072009-12-19 02:59:52 +0000648 if (OperandList[operandIndex].Constraints.size()) {
Chris Lattnerd8adec72010-11-01 04:03:32 +0000649 const CGIOperandList::ConstraintInfo &Constraint =
Chris Lattnera9dfb1b2010-02-10 01:45:28 +0000650 OperandList[operandIndex].Constraints[0];
651 if (Constraint.isTied()) {
Craig Topperf7755df2012-07-12 06:52:41 +0000652 operandMapping[operandIndex] = operandIndex;
653 operandMapping[Constraint.getTiedOperand()] = operandIndex;
Sean Callanan04cc3072009-12-19 02:59:52 +0000654 } else {
655 ++numPhysicalOperands;
656 operandMapping[operandIndex] = operandIndex;
657 }
658 } else {
659 ++numPhysicalOperands;
660 operandMapping[operandIndex] = operandIndex;
661 }
Sean Callanan04cc3072009-12-19 02:59:52 +0000662 }
Craig Topperac172e22012-07-30 04:48:12 +0000663
Sean Callanan04cc3072009-12-19 02:59:52 +0000664#define HANDLE_OPERAND(class) \
665 handleOperand(false, \
666 operandIndex, \
667 physicalOperandIndex, \
668 numPhysicalOperands, \
669 operandMapping, \
670 class##EncodingFromString);
Craig Topperac172e22012-07-30 04:48:12 +0000671
Sean Callanan04cc3072009-12-19 02:59:52 +0000672#define HANDLE_OPTIONAL(class) \
673 handleOperand(true, \
674 operandIndex, \
675 physicalOperandIndex, \
676 numPhysicalOperands, \
677 operandMapping, \
678 class##EncodingFromString);
Craig Topperac172e22012-07-30 04:48:12 +0000679
Sean Callanan04cc3072009-12-19 02:59:52 +0000680 // operandIndex should always be < numOperands
Craig Topperf7755df2012-07-12 06:52:41 +0000681 unsigned operandIndex = 0;
Sean Callanan04cc3072009-12-19 02:59:52 +0000682 // physicalOperandIndex should always be < numPhysicalOperands
683 unsigned physicalOperandIndex = 0;
Craig Topperac172e22012-07-30 04:48:12 +0000684
Sean Callanan04cc3072009-12-19 02:59:52 +0000685 switch (Form) {
686 case X86Local::RawFrm:
687 // Operand 1 (optional) is an address or immediate.
688 // Operand 2 (optional) is an immediate.
Craig Topperac172e22012-07-30 04:48:12 +0000689 assert(numPhysicalOperands <= 2 &&
Sean Callanan04cc3072009-12-19 02:59:52 +0000690 "Unexpected number of operands for RawFrm");
691 HANDLE_OPTIONAL(relocation)
692 HANDLE_OPTIONAL(immediate)
693 break;
694 case X86Local::AddRegFrm:
695 // Operand 1 is added to the opcode.
696 // Operand 2 (optional) is an address.
697 assert(numPhysicalOperands >= 1 && numPhysicalOperands <= 2 &&
698 "Unexpected number of operands for AddRegFrm");
699 HANDLE_OPERAND(opcodeModifier)
700 HANDLE_OPTIONAL(relocation)
701 break;
702 case X86Local::MRMDestReg:
703 // Operand 1 is a register operand in the R/M field.
704 // Operand 2 is a register operand in the Reg/Opcode field.
Craig Topper4f2fba12011-08-30 07:09:35 +0000705 // - In AVX, there is a register operand in the VEX.vvvv field here -
Sean Callanan04cc3072009-12-19 02:59:52 +0000706 // Operand 3 (optional) is an immediate.
Craig Topper4f2fba12011-08-30 07:09:35 +0000707 if (HasVEX_4VPrefix)
708 assert(numPhysicalOperands >= 3 && numPhysicalOperands <= 4 &&
709 "Unexpected number of operands for MRMDestRegFrm with VEX_4V");
710 else
711 assert(numPhysicalOperands >= 2 && numPhysicalOperands <= 3 &&
712 "Unexpected number of operands for MRMDestRegFrm");
Craig Topperac172e22012-07-30 04:48:12 +0000713
Sean Callanan04cc3072009-12-19 02:59:52 +0000714 HANDLE_OPERAND(rmRegister)
Craig Topper4f2fba12011-08-30 07:09:35 +0000715
716 if (HasVEX_4VPrefix)
717 // FIXME: In AVX, the register below becomes the one encoded
718 // in ModRMVEX and the one above the one in the VEX.VVVV field
719 HANDLE_OPERAND(vvvvRegister)
Craig Topperac172e22012-07-30 04:48:12 +0000720
Sean Callanan04cc3072009-12-19 02:59:52 +0000721 HANDLE_OPERAND(roRegister)
722 HANDLE_OPTIONAL(immediate)
723 break;
724 case X86Local::MRMDestMem:
725 // Operand 1 is a memory operand (possibly SIB-extended)
726 // Operand 2 is a register operand in the Reg/Opcode field.
Craig Topper4f2fba12011-08-30 07:09:35 +0000727 // - In AVX, there is a register operand in the VEX.vvvv field here -
Sean Callanan04cc3072009-12-19 02:59:52 +0000728 // Operand 3 (optional) is an immediate.
Craig Topper4f2fba12011-08-30 07:09:35 +0000729 if (HasVEX_4VPrefix)
730 assert(numPhysicalOperands >= 3 && numPhysicalOperands <= 4 &&
731 "Unexpected number of operands for MRMDestMemFrm with VEX_4V");
732 else
733 assert(numPhysicalOperands >= 2 && numPhysicalOperands <= 3 &&
734 "Unexpected number of operands for MRMDestMemFrm");
Sean Callanan04cc3072009-12-19 02:59:52 +0000735 HANDLE_OPERAND(memory)
Craig Topper4f2fba12011-08-30 07:09:35 +0000736
Elena Demikhovsky003e7d72013-07-28 08:28:38 +0000737 if (HasEVEX_K)
738 HANDLE_OPERAND(writemaskRegister)
739
Craig Topper4f2fba12011-08-30 07:09:35 +0000740 if (HasVEX_4VPrefix)
741 // FIXME: In AVX, the register below becomes the one encoded
742 // in ModRMVEX and the one above the one in the VEX.VVVV field
743 HANDLE_OPERAND(vvvvRegister)
Craig Topperac172e22012-07-30 04:48:12 +0000744
Sean Callanan04cc3072009-12-19 02:59:52 +0000745 HANDLE_OPERAND(roRegister)
746 HANDLE_OPTIONAL(immediate)
747 break;
748 case X86Local::MRMSrcReg:
749 // Operand 1 is a register operand in the Reg/Opcode field.
750 // Operand 2 is a register operand in the R/M field.
Sean Callananc3fd5232011-03-15 01:23:15 +0000751 // - In AVX, there is a register operand in the VEX.vvvv field here -
Sean Callanan04cc3072009-12-19 02:59:52 +0000752 // Operand 3 (optional) is an immediate.
Benjamin Krameref479ea2012-05-29 19:05:25 +0000753 // Operand 4 (optional) is an immediate.
Bruno Cardoso Lopesc2f87b72010-06-08 22:51:23 +0000754
Craig Topperaea148c2011-10-16 07:55:05 +0000755 if (HasVEX_4VPrefix || HasVEX_4VOp3Prefix)
Craig Topper2ba766a2011-12-30 06:23:39 +0000756 assert(numPhysicalOperands >= 3 && numPhysicalOperands <= 5 &&
Craig Topperac172e22012-07-30 04:48:12 +0000757 "Unexpected number of operands for MRMSrcRegFrm with VEX_4V");
Sean Callananc3fd5232011-03-15 01:23:15 +0000758 else
Benjamin Krameref479ea2012-05-29 19:05:25 +0000759 assert(numPhysicalOperands >= 2 && numPhysicalOperands <= 4 &&
Sean Callananc3fd5232011-03-15 01:23:15 +0000760 "Unexpected number of operands for MRMSrcRegFrm");
Craig Topperac172e22012-07-30 04:48:12 +0000761
Sean Callananc3fd5232011-03-15 01:23:15 +0000762 HANDLE_OPERAND(roRegister)
Craig Topper25ea4e52011-10-16 03:51:13 +0000763
Elena Demikhovsky003e7d72013-07-28 08:28:38 +0000764 if (HasEVEX_K)
765 HANDLE_OPERAND(writemaskRegister)
766
Craig Topperaea148c2011-10-16 07:55:05 +0000767 if (HasVEX_4VPrefix)
Bruno Cardoso Lopesfd5458d2010-06-11 23:50:47 +0000768 // FIXME: In AVX, the register below becomes the one encoded
769 // in ModRMVEX and the one above the one in the VEX.VVVV field
Sean Callananc3fd5232011-03-15 01:23:15 +0000770 HANDLE_OPERAND(vvvvRegister)
Craig Topper25ea4e52011-10-16 03:51:13 +0000771
Craig Topper03a0bed2011-12-30 05:20:36 +0000772 if (HasMemOp4Prefix)
773 HANDLE_OPERAND(immediate)
774
Sean Callananc3fd5232011-03-15 01:23:15 +0000775 HANDLE_OPERAND(rmRegister)
Craig Topper25ea4e52011-10-16 03:51:13 +0000776
Craig Topperaea148c2011-10-16 07:55:05 +0000777 if (HasVEX_4VOp3Prefix)
Craig Topper25ea4e52011-10-16 03:51:13 +0000778 HANDLE_OPERAND(vvvvRegister)
779
Craig Topper2ba766a2011-12-30 06:23:39 +0000780 if (!HasMemOp4Prefix)
781 HANDLE_OPTIONAL(immediate)
782 HANDLE_OPTIONAL(immediate) // above might be a register in 7:4
Benjamin Krameref479ea2012-05-29 19:05:25 +0000783 HANDLE_OPTIONAL(immediate)
Sean Callanan04cc3072009-12-19 02:59:52 +0000784 break;
785 case X86Local::MRMSrcMem:
786 // Operand 1 is a register operand in the Reg/Opcode field.
787 // Operand 2 is a memory operand (possibly SIB-extended)
Sean Callananc3fd5232011-03-15 01:23:15 +0000788 // - In AVX, there is a register operand in the VEX.vvvv field here -
Sean Callanan04cc3072009-12-19 02:59:52 +0000789 // Operand 3 (optional) is an immediate.
Craig Topperaea148c2011-10-16 07:55:05 +0000790
791 if (HasVEX_4VPrefix || HasVEX_4VOp3Prefix)
Craig Topper2ba766a2011-12-30 06:23:39 +0000792 assert(numPhysicalOperands >= 3 && numPhysicalOperands <= 5 &&
Craig Topperac172e22012-07-30 04:48:12 +0000793 "Unexpected number of operands for MRMSrcMemFrm with VEX_4V");
Sean Callananc3fd5232011-03-15 01:23:15 +0000794 else
795 assert(numPhysicalOperands >= 2 && numPhysicalOperands <= 3 &&
796 "Unexpected number of operands for MRMSrcMemFrm");
Craig Topperac172e22012-07-30 04:48:12 +0000797
Sean Callanan04cc3072009-12-19 02:59:52 +0000798 HANDLE_OPERAND(roRegister)
Bruno Cardoso Lopesfd5458d2010-06-11 23:50:47 +0000799
Elena Demikhovsky003e7d72013-07-28 08:28:38 +0000800 if (HasEVEX_K)
801 HANDLE_OPERAND(writemaskRegister)
802
Craig Topperaea148c2011-10-16 07:55:05 +0000803 if (HasVEX_4VPrefix)
Bruno Cardoso Lopesfd5458d2010-06-11 23:50:47 +0000804 // FIXME: In AVX, the register below becomes the one encoded
805 // in ModRMVEX and the one above the one in the VEX.VVVV field
Sean Callananc3fd5232011-03-15 01:23:15 +0000806 HANDLE_OPERAND(vvvvRegister)
Bruno Cardoso Lopesfd5458d2010-06-11 23:50:47 +0000807
Craig Topper03a0bed2011-12-30 05:20:36 +0000808 if (HasMemOp4Prefix)
809 HANDLE_OPERAND(immediate)
810
Sean Callanan04cc3072009-12-19 02:59:52 +0000811 HANDLE_OPERAND(memory)
Craig Topper25ea4e52011-10-16 03:51:13 +0000812
Craig Topperaea148c2011-10-16 07:55:05 +0000813 if (HasVEX_4VOp3Prefix)
Craig Topper25ea4e52011-10-16 03:51:13 +0000814 HANDLE_OPERAND(vvvvRegister)
815
Craig Topper2ba766a2011-12-30 06:23:39 +0000816 if (!HasMemOp4Prefix)
817 HANDLE_OPTIONAL(immediate)
818 HANDLE_OPTIONAL(immediate) // above might be a register in 7:4
Sean Callanan04cc3072009-12-19 02:59:52 +0000819 break;
820 case X86Local::MRM0r:
821 case X86Local::MRM1r:
822 case X86Local::MRM2r:
823 case X86Local::MRM3r:
824 case X86Local::MRM4r:
825 case X86Local::MRM5r:
826 case X86Local::MRM6r:
827 case X86Local::MRM7r:
Elena Demikhovskyc35219e2013-08-22 12:18:28 +0000828 {
829 // Operand 1 is a register operand in the R/M field.
830 // Operand 2 (optional) is an immediate or relocation.
831 // Operand 3 (optional) is an immediate.
832 unsigned kOp = (HasEVEX_K) ? 1:0;
833 unsigned Op4v = (HasVEX_4VPrefix) ? 1:0;
834 if (numPhysicalOperands > 3 + kOp + Op4v)
835 llvm_unreachable("Unexpected number of operands for MRMnr");
836 }
Sean Callananc3fd5232011-03-15 01:23:15 +0000837 if (HasVEX_4VPrefix)
Craig Topper27ad1252011-10-15 20:46:47 +0000838 HANDLE_OPERAND(vvvvRegister)
Elena Demikhovskyc35219e2013-08-22 12:18:28 +0000839
840 if (HasEVEX_K)
841 HANDLE_OPERAND(writemaskRegister)
Sean Callanan04cc3072009-12-19 02:59:52 +0000842 HANDLE_OPTIONAL(rmRegister)
843 HANDLE_OPTIONAL(relocation)
Benjamin Krameref479ea2012-05-29 19:05:25 +0000844 HANDLE_OPTIONAL(immediate)
Sean Callanan04cc3072009-12-19 02:59:52 +0000845 break;
846 case X86Local::MRM0m:
847 case X86Local::MRM1m:
848 case X86Local::MRM2m:
849 case X86Local::MRM3m:
850 case X86Local::MRM4m:
851 case X86Local::MRM5m:
852 case X86Local::MRM6m:
853 case X86Local::MRM7m:
Elena Demikhovskyc35219e2013-08-22 12:18:28 +0000854 {
855 // Operand 1 is a memory operand (possibly SIB-extended)
856 // Operand 2 (optional) is an immediate or relocation.
857 unsigned kOp = (HasEVEX_K) ? 1:0;
858 unsigned Op4v = (HasVEX_4VPrefix) ? 1:0;
859 if (numPhysicalOperands < 1 + kOp + Op4v ||
860 numPhysicalOperands > 2 + kOp + Op4v)
861 llvm_unreachable("Unexpected number of operands for MRMnm");
862 }
Craig Topper27ad1252011-10-15 20:46:47 +0000863 if (HasVEX_4VPrefix)
864 HANDLE_OPERAND(vvvvRegister)
Elena Demikhovskyc35219e2013-08-22 12:18:28 +0000865 if (HasEVEX_K)
866 HANDLE_OPERAND(writemaskRegister)
Sean Callanan04cc3072009-12-19 02:59:52 +0000867 HANDLE_OPERAND(memory)
868 HANDLE_OPTIONAL(relocation)
869 break;
Sean Callanan8d302b22010-10-04 22:45:51 +0000870 case X86Local::RawFrmImm8:
871 // operand 1 is a 16-bit immediate
872 // operand 2 is an 8-bit immediate
873 assert(numPhysicalOperands == 2 &&
874 "Unexpected number of operands for X86Local::RawFrmImm8");
875 HANDLE_OPERAND(immediate)
876 HANDLE_OPERAND(immediate)
877 break;
878 case X86Local::RawFrmImm16:
879 // operand 1 is a 16-bit immediate
880 // operand 2 is a 16-bit immediate
881 HANDLE_OPERAND(immediate)
882 HANDLE_OPERAND(immediate)
883 break;
Kevin Enderbyf15856e2013-03-11 21:17:13 +0000884 case X86Local::MRM_F8:
885 if (Opcode == 0xc6) {
886 assert(numPhysicalOperands == 1 &&
887 "Unexpected number of operands for X86Local::MRM_F8");
888 HANDLE_OPERAND(immediate)
889 } else if (Opcode == 0xc7) {
890 assert(numPhysicalOperands == 1 &&
891 "Unexpected number of operands for X86Local::MRM_F8");
892 HANDLE_OPERAND(relocation)
893 }
894 break;
Sean Callanan04cc3072009-12-19 02:59:52 +0000895 case X86Local::MRMInitReg:
896 // Ignored.
897 break;
898 }
Craig Topperac172e22012-07-30 04:48:12 +0000899
Sean Callanan04cc3072009-12-19 02:59:52 +0000900 #undef HANDLE_OPERAND
901 #undef HANDLE_OPTIONAL
902}
903
904void RecognizableInstr::emitDecodePath(DisassemblerTables &tables) const {
905 // Special cases where the LLVM tables are not complete
906
Sean Callanandde9c122010-02-12 23:39:46 +0000907#define MAP(from, to) \
908 case X86Local::MRM_##from: \
909 filter = new ExactFilter(0x##from); \
910 break;
Sean Callanan04cc3072009-12-19 02:59:52 +0000911
912 OpcodeType opcodeType = (OpcodeType)-1;
Craig Topperac172e22012-07-30 04:48:12 +0000913
914 ModRMFilter* filter = NULL;
Sean Callanan04cc3072009-12-19 02:59:52 +0000915 uint8_t opcodeToSet = 0;
916
917 switch (Prefix) {
Craig Topper9e3e38a2013-10-03 05:17:48 +0000918 default: llvm_unreachable("Invalid prefix!");
Sean Callanan04cc3072009-12-19 02:59:52 +0000919 // Extended two-byte opcodes can start with f2 0f, f3 0f, or 0f
920 case X86Local::XD:
921 case X86Local::XS:
922 case X86Local::TB:
923 opcodeType = TWOBYTE;
924
925 switch (Opcode) {
Sean Callanan44232af2010-02-13 01:48:34 +0000926 default:
927 if (needsModRMForDecode(Form))
928 filter = new ModFilter(isRegFormat(Form));
929 else
930 filter = new DumbFilter();
931 break;
Sean Callanan04cc3072009-12-19 02:59:52 +0000932#define EXTENSION_TABLE(n) case 0x##n:
933 TWO_BYTE_EXTENSION_TABLES
934#undef EXTENSION_TABLE
935 switch (Form) {
936 default:
937 llvm_unreachable("Unhandled two-byte extended opcode");
938 case X86Local::MRM0r:
939 case X86Local::MRM1r:
940 case X86Local::MRM2r:
941 case X86Local::MRM3r:
942 case X86Local::MRM4r:
943 case X86Local::MRM5r:
944 case X86Local::MRM6r:
945 case X86Local::MRM7r:
946 filter = new ExtendedFilter(true, Form - X86Local::MRM0r);
947 break;
948 case X86Local::MRM0m:
949 case X86Local::MRM1m:
950 case X86Local::MRM2m:
951 case X86Local::MRM3m:
952 case X86Local::MRM4m:
953 case X86Local::MRM5m:
954 case X86Local::MRM6m:
955 case X86Local::MRM7m:
956 filter = new ExtendedFilter(false, Form - X86Local::MRM0m);
957 break;
Sean Callanandde9c122010-02-12 23:39:46 +0000958 MRM_MAPPING
Sean Callanan04cc3072009-12-19 02:59:52 +0000959 } // switch (Form)
960 break;
Sean Callanan44232af2010-02-13 01:48:34 +0000961 } // switch (Opcode)
Sean Callanan04cc3072009-12-19 02:59:52 +0000962 opcodeToSet = Opcode;
963 break;
964 case X86Local::T8:
Craig Topper96fa5972011-10-16 16:50:08 +0000965 case X86Local::T8XD:
966 case X86Local::T8XS:
Sean Callanan04cc3072009-12-19 02:59:52 +0000967 opcodeType = THREEBYTE_38;
Craig Topper27ad1252011-10-15 20:46:47 +0000968 switch (Opcode) {
969 default:
970 if (needsModRMForDecode(Form))
971 filter = new ModFilter(isRegFormat(Form));
972 else
973 filter = new DumbFilter();
974 break;
975#define EXTENSION_TABLE(n) case 0x##n:
976 THREE_BYTE_38_EXTENSION_TABLES
977#undef EXTENSION_TABLE
978 switch (Form) {
979 default:
980 llvm_unreachable("Unhandled two-byte extended opcode");
981 case X86Local::MRM0r:
982 case X86Local::MRM1r:
983 case X86Local::MRM2r:
984 case X86Local::MRM3r:
985 case X86Local::MRM4r:
986 case X86Local::MRM5r:
987 case X86Local::MRM6r:
988 case X86Local::MRM7r:
989 filter = new ExtendedFilter(true, Form - X86Local::MRM0r);
990 break;
991 case X86Local::MRM0m:
992 case X86Local::MRM1m:
993 case X86Local::MRM2m:
994 case X86Local::MRM3m:
995 case X86Local::MRM4m:
996 case X86Local::MRM5m:
997 case X86Local::MRM6m:
998 case X86Local::MRM7m:
999 filter = new ExtendedFilter(false, Form - X86Local::MRM0m);
1000 break;
1001 MRM_MAPPING
1002 } // switch (Form)
1003 break;
1004 } // switch (Opcode)
Sean Callanan04cc3072009-12-19 02:59:52 +00001005 opcodeToSet = Opcode;
1006 break;
Chris Lattnerf7477e52010-02-12 02:06:33 +00001007 case X86Local::P_TA:
Craig Topper980d5982011-10-23 07:34:00 +00001008 case X86Local::TAXD:
Sean Callanan04cc3072009-12-19 02:59:52 +00001009 opcodeType = THREEBYTE_3A;
1010 if (needsModRMForDecode(Form))
1011 filter = new ModFilter(isRegFormat(Form));
1012 else
1013 filter = new DumbFilter();
1014 opcodeToSet = Opcode;
1015 break;
Joerg Sonnenbergerfc4789d2011-04-04 16:58:13 +00001016 case X86Local::A6:
1017 opcodeType = THREEBYTE_A6;
1018 if (needsModRMForDecode(Form))
1019 filter = new ModFilter(isRegFormat(Form));
1020 else
1021 filter = new DumbFilter();
1022 opcodeToSet = Opcode;
1023 break;
1024 case X86Local::A7:
1025 opcodeType = THREEBYTE_A7;
1026 if (needsModRMForDecode(Form))
1027 filter = new ModFilter(isRegFormat(Form));
1028 else
1029 filter = new DumbFilter();
1030 opcodeToSet = Opcode;
1031 break;
Craig Topper9e3e38a2013-10-03 05:17:48 +00001032 case X86Local::XOP8:
1033 opcodeType = XOP8_MAP;
1034 if (needsModRMForDecode(Form))
1035 filter = new ModFilter(isRegFormat(Form));
1036 else
1037 filter = new DumbFilter();
1038 opcodeToSet = Opcode;
1039 break;
1040 case X86Local::XOP9:
1041 opcodeType = XOP9_MAP;
1042 switch (Opcode) {
1043 default:
1044 if (needsModRMForDecode(Form))
1045 filter = new ModFilter(isRegFormat(Form));
1046 else
1047 filter = new DumbFilter();
1048 break;
1049#define EXTENSION_TABLE(n) case 0x##n:
1050 XOP9_MAP_EXTENSION_TABLES
1051#undef EXTENSION_TABLE
1052 switch (Form) {
1053 default:
1054 llvm_unreachable("Unhandled XOP9 extended opcode");
1055 case X86Local::MRM0r:
1056 case X86Local::MRM1r:
1057 case X86Local::MRM2r:
1058 case X86Local::MRM3r:
1059 case X86Local::MRM4r:
1060 case X86Local::MRM5r:
1061 case X86Local::MRM6r:
1062 case X86Local::MRM7r:
1063 filter = new ExtendedFilter(true, Form - X86Local::MRM0r);
1064 break;
1065 case X86Local::MRM0m:
1066 case X86Local::MRM1m:
1067 case X86Local::MRM2m:
1068 case X86Local::MRM3m:
1069 case X86Local::MRM4m:
1070 case X86Local::MRM5m:
1071 case X86Local::MRM6m:
1072 case X86Local::MRM7m:
1073 filter = new ExtendedFilter(false, Form - X86Local::MRM0m);
1074 break;
1075 MRM_MAPPING
1076 } // switch (Form)
1077 break;
1078 } // switch (Opcode)
1079 opcodeToSet = Opcode;
1080 break;
1081 case X86Local::XOPA:
1082 opcodeType = XOPA_MAP;
1083 if (needsModRMForDecode(Form))
1084 filter = new ModFilter(isRegFormat(Form));
1085 else
1086 filter = new DumbFilter();
1087 opcodeToSet = Opcode;
1088 break;
Sean Callanan04cc3072009-12-19 02:59:52 +00001089 case X86Local::D8:
1090 case X86Local::D9:
1091 case X86Local::DA:
1092 case X86Local::DB:
1093 case X86Local::DC:
1094 case X86Local::DD:
1095 case X86Local::DE:
1096 case X86Local::DF:
1097 assert(Opcode >= 0xc0 && "Unexpected opcode for an escape opcode");
1098 opcodeType = ONEBYTE;
1099 if (Form == X86Local::AddRegFrm) {
1100 Spec->modifierType = MODIFIER_MODRM;
1101 Spec->modifierBase = Opcode;
1102 filter = new AddRegEscapeFilter(Opcode);
1103 } else {
1104 filter = new EscapeFilter(true, Opcode);
1105 }
1106 opcodeToSet = 0xd8 + (Prefix - X86Local::D8);
1107 break;
Craig Toppera948cb92011-09-11 20:23:20 +00001108 case X86Local::REP:
Craig Topper9e3e38a2013-10-03 05:17:48 +00001109 case 0:
Sean Callanan04cc3072009-12-19 02:59:52 +00001110 opcodeType = ONEBYTE;
1111 switch (Opcode) {
1112#define EXTENSION_TABLE(n) case 0x##n:
1113 ONE_BYTE_EXTENSION_TABLES
1114#undef EXTENSION_TABLE
1115 switch (Form) {
1116 default:
1117 llvm_unreachable("Fell through the cracks of a single-byte "
1118 "extended opcode");
1119 case X86Local::MRM0r:
1120 case X86Local::MRM1r:
1121 case X86Local::MRM2r:
1122 case X86Local::MRM3r:
1123 case X86Local::MRM4r:
1124 case X86Local::MRM5r:
1125 case X86Local::MRM6r:
1126 case X86Local::MRM7r:
1127 filter = new ExtendedFilter(true, Form - X86Local::MRM0r);
1128 break;
1129 case X86Local::MRM0m:
1130 case X86Local::MRM1m:
1131 case X86Local::MRM2m:
1132 case X86Local::MRM3m:
1133 case X86Local::MRM4m:
1134 case X86Local::MRM5m:
1135 case X86Local::MRM6m:
1136 case X86Local::MRM7m:
1137 filter = new ExtendedFilter(false, Form - X86Local::MRM0m);
1138 break;
Sean Callanandde9c122010-02-12 23:39:46 +00001139 MRM_MAPPING
Sean Callanan04cc3072009-12-19 02:59:52 +00001140 } // switch (Form)
1141 break;
1142 case 0xd8:
1143 case 0xd9:
1144 case 0xda:
1145 case 0xdb:
1146 case 0xdc:
1147 case 0xdd:
1148 case 0xde:
1149 case 0xdf:
1150 filter = new EscapeFilter(false, Form - X86Local::MRM0m);
1151 break;
1152 default:
1153 if (needsModRMForDecode(Form))
1154 filter = new ModFilter(isRegFormat(Form));
1155 else
1156 filter = new DumbFilter();
1157 break;
1158 } // switch (Opcode)
1159 opcodeToSet = Opcode;
1160 } // switch (Prefix)
1161
1162 assert(opcodeType != (OpcodeType)-1 &&
1163 "Opcode type not set");
1164 assert(filter && "Filter not set");
1165
1166 if (Form == X86Local::AddRegFrm) {
1167 if(Spec->modifierType != MODIFIER_MODRM) {
1168 assert(opcodeToSet < 0xf9 &&
1169 "Not enough room for all ADDREG_FRM operands");
Craig Topperac172e22012-07-30 04:48:12 +00001170
Sean Callanan04cc3072009-12-19 02:59:52 +00001171 uint8_t currentOpcode;
1172
1173 for (currentOpcode = opcodeToSet;
1174 currentOpcode < opcodeToSet + 8;
1175 ++currentOpcode)
Craig Topperac172e22012-07-30 04:48:12 +00001176 tables.setTableFields(opcodeType,
1177 insnContext(),
1178 currentOpcode,
1179 *filter,
Craig Topperf18c8962011-10-04 06:30:42 +00001180 UID, Is32Bit, IgnoresVEX_L);
Craig Topperac172e22012-07-30 04:48:12 +00001181
Sean Callanan04cc3072009-12-19 02:59:52 +00001182 Spec->modifierType = MODIFIER_OPCODE;
1183 Spec->modifierBase = opcodeToSet;
1184 } else {
1185 // modifierBase was set where MODIFIER_MODRM was set
Craig Topperac172e22012-07-30 04:48:12 +00001186 tables.setTableFields(opcodeType,
1187 insnContext(),
1188 opcodeToSet,
1189 *filter,
Craig Topperf18c8962011-10-04 06:30:42 +00001190 UID, Is32Bit, IgnoresVEX_L);
Sean Callanan04cc3072009-12-19 02:59:52 +00001191 }
1192 } else {
1193 tables.setTableFields(opcodeType,
1194 insnContext(),
1195 opcodeToSet,
1196 *filter,
Craig Topperf18c8962011-10-04 06:30:42 +00001197 UID, Is32Bit, IgnoresVEX_L);
Craig Topperac172e22012-07-30 04:48:12 +00001198
Sean Callanan04cc3072009-12-19 02:59:52 +00001199 Spec->modifierType = MODIFIER_NONE;
1200 Spec->modifierBase = opcodeToSet;
1201 }
Craig Topperac172e22012-07-30 04:48:12 +00001202
Sean Callanan04cc3072009-12-19 02:59:52 +00001203 delete filter;
Craig Topperac172e22012-07-30 04:48:12 +00001204
Sean Callanandde9c122010-02-12 23:39:46 +00001205#undef MAP
Sean Callanan04cc3072009-12-19 02:59:52 +00001206}
1207
1208#define TYPE(str, type) if (s == str) return type;
1209OperandType RecognizableInstr::typeFromString(const std::string &s,
1210 bool isSSE,
1211 bool hasREX_WPrefix,
1212 bool hasOpSizePrefix) {
1213 if (isSSE) {
Craig Topperac172e22012-07-30 04:48:12 +00001214 // For SSE instructions, we ignore the OpSize prefix and force operand
Sean Callanan04cc3072009-12-19 02:59:52 +00001215 // sizes.
1216 TYPE("GR16", TYPE_R16)
1217 TYPE("GR32", TYPE_R32)
1218 TYPE("GR64", TYPE_R64)
1219 }
1220 if(hasREX_WPrefix) {
1221 // For instructions with a REX_W prefix, a declared 32-bit register encoding
1222 // is special.
1223 TYPE("GR32", TYPE_R32)
1224 }
1225 if(!hasOpSizePrefix) {
1226 // For instructions without an OpSize prefix, a declared 16-bit register or
1227 // immediate encoding is special.
1228 TYPE("GR16", TYPE_R16)
1229 TYPE("i16imm", TYPE_IMM16)
1230 }
1231 TYPE("i16mem", TYPE_Mv)
1232 TYPE("i16imm", TYPE_IMMv)
1233 TYPE("i16i8imm", TYPE_IMMv)
1234 TYPE("GR16", TYPE_Rv)
1235 TYPE("i32mem", TYPE_Mv)
1236 TYPE("i32imm", TYPE_IMMv)
1237 TYPE("i32i8imm", TYPE_IMM32)
Kevin Enderby5ef6c452011-07-27 23:01:50 +00001238 TYPE("u32u8imm", TYPE_IMM32)
Sean Callanan04cc3072009-12-19 02:59:52 +00001239 TYPE("GR32", TYPE_Rv)
1240 TYPE("i64mem", TYPE_Mv)
1241 TYPE("i64i32imm", TYPE_IMM64)
1242 TYPE("i64i8imm", TYPE_IMM64)
1243 TYPE("GR64", TYPE_R64)
1244 TYPE("i8mem", TYPE_M8)
1245 TYPE("i8imm", TYPE_IMM8)
1246 TYPE("GR8", TYPE_R8)
1247 TYPE("VR128", TYPE_XMM128)
Elena Demikhovsky003e7d72013-07-28 08:28:38 +00001248 TYPE("VR128X", TYPE_XMM128)
Sean Callanan04cc3072009-12-19 02:59:52 +00001249 TYPE("f128mem", TYPE_M128)
Chris Lattnerf60062f2010-09-29 02:57:56 +00001250 TYPE("f256mem", TYPE_M256)
Elena Demikhovsky003e7d72013-07-28 08:28:38 +00001251 TYPE("f512mem", TYPE_M512)
Sean Callanan04cc3072009-12-19 02:59:52 +00001252 TYPE("FR64", TYPE_XMM64)
Elena Demikhovsky003e7d72013-07-28 08:28:38 +00001253 TYPE("FR64X", TYPE_XMM64)
Sean Callanan04cc3072009-12-19 02:59:52 +00001254 TYPE("f64mem", TYPE_M64FP)
Chris Lattnerf60062f2010-09-29 02:57:56 +00001255 TYPE("sdmem", TYPE_M64FP)
Sean Callanan04cc3072009-12-19 02:59:52 +00001256 TYPE("FR32", TYPE_XMM32)
Elena Demikhovsky003e7d72013-07-28 08:28:38 +00001257 TYPE("FR32X", TYPE_XMM32)
Sean Callanan04cc3072009-12-19 02:59:52 +00001258 TYPE("f32mem", TYPE_M32FP)
Chris Lattnerf60062f2010-09-29 02:57:56 +00001259 TYPE("ssmem", TYPE_M32FP)
Sean Callanan04cc3072009-12-19 02:59:52 +00001260 TYPE("RST", TYPE_ST)
1261 TYPE("i128mem", TYPE_M128)
Sean Callananc3fd5232011-03-15 01:23:15 +00001262 TYPE("i256mem", TYPE_M256)
Elena Demikhovsky003e7d72013-07-28 08:28:38 +00001263 TYPE("i512mem", TYPE_M512)
Sean Callanan04cc3072009-12-19 02:59:52 +00001264 TYPE("i64i32imm_pcrel", TYPE_REL64)
Chris Lattnerac588122010-07-07 22:27:31 +00001265 TYPE("i16imm_pcrel", TYPE_REL16)
Sean Callanan04cc3072009-12-19 02:59:52 +00001266 TYPE("i32imm_pcrel", TYPE_REL32)
Sean Callanan1efe6612010-04-07 21:42:19 +00001267 TYPE("SSECC", TYPE_IMM3)
Craig Topper7629d632012-04-03 05:20:24 +00001268 TYPE("AVXCC", TYPE_IMM5)
Sean Callanan04cc3072009-12-19 02:59:52 +00001269 TYPE("brtarget", TYPE_RELv)
Owen Anderson578074b2010-12-13 19:31:11 +00001270 TYPE("uncondbrtarget", TYPE_RELv)
Sean Callanan04cc3072009-12-19 02:59:52 +00001271 TYPE("brtarget8", TYPE_REL8)
1272 TYPE("f80mem", TYPE_M80FP)
Sean Callanan36eab802009-12-22 21:12:55 +00001273 TYPE("lea32mem", TYPE_LEA)
1274 TYPE("lea64_32mem", TYPE_LEA)
1275 TYPE("lea64mem", TYPE_LEA)
Sean Callanan04cc3072009-12-19 02:59:52 +00001276 TYPE("VR64", TYPE_MM64)
1277 TYPE("i64imm", TYPE_IMMv)
1278 TYPE("opaque32mem", TYPE_M1616)
1279 TYPE("opaque48mem", TYPE_M1632)
1280 TYPE("opaque80mem", TYPE_M1664)
1281 TYPE("opaque512mem", TYPE_M512)
1282 TYPE("SEGMENT_REG", TYPE_SEGMENTREG)
1283 TYPE("DEBUG_REG", TYPE_DEBUGREG)
Sean Callanane7e1cf92010-05-06 20:59:00 +00001284 TYPE("CONTROL_REG", TYPE_CONTROLREG)
Sean Callanan04cc3072009-12-19 02:59:52 +00001285 TYPE("offset8", TYPE_MOFFS8)
1286 TYPE("offset16", TYPE_MOFFS16)
1287 TYPE("offset32", TYPE_MOFFS32)
1288 TYPE("offset64", TYPE_MOFFS64)
Sean Callananc3fd5232011-03-15 01:23:15 +00001289 TYPE("VR256", TYPE_XMM256)
Elena Demikhovsky003e7d72013-07-28 08:28:38 +00001290 TYPE("VR256X", TYPE_XMM256)
1291 TYPE("VR512", TYPE_XMM512)
1292 TYPE("VK8", TYPE_VK8)
1293 TYPE("VK8WM", TYPE_VK8)
1294 TYPE("VK16", TYPE_VK16)
1295 TYPE("VK16WM", TYPE_VK16)
Craig Topper23eb4682011-10-06 06:44:41 +00001296 TYPE("GR16_NOAX", TYPE_Rv)
1297 TYPE("GR32_NOAX", TYPE_Rv)
1298 TYPE("GR64_NOAX", TYPE_R64)
Craig Topper01deb5f2012-07-18 04:11:12 +00001299 TYPE("vx32mem", TYPE_M32)
1300 TYPE("vy32mem", TYPE_M32)
Elena Demikhovsky003e7d72013-07-28 08:28:38 +00001301 TYPE("vz32mem", TYPE_M32)
Craig Topper01deb5f2012-07-18 04:11:12 +00001302 TYPE("vx64mem", TYPE_M64)
1303 TYPE("vy64mem", TYPE_M64)
Elena Demikhovsky003e7d72013-07-28 08:28:38 +00001304 TYPE("vy64xmem", TYPE_M64)
1305 TYPE("vz64mem", TYPE_M64)
Sean Callanan04cc3072009-12-19 02:59:52 +00001306 errs() << "Unhandled type string " << s << "\n";
1307 llvm_unreachable("Unhandled type string");
1308}
1309#undef TYPE
1310
1311#define ENCODING(str, encoding) if (s == str) return encoding;
1312OperandEncoding RecognizableInstr::immediateEncodingFromString
1313 (const std::string &s,
1314 bool hasOpSizePrefix) {
1315 if(!hasOpSizePrefix) {
1316 // For instructions without an OpSize prefix, a declared 16-bit register or
1317 // immediate encoding is special.
1318 ENCODING("i16imm", ENCODING_IW)
1319 }
1320 ENCODING("i32i8imm", ENCODING_IB)
Kevin Enderby5ef6c452011-07-27 23:01:50 +00001321 ENCODING("u32u8imm", ENCODING_IB)
Sean Callanan04cc3072009-12-19 02:59:52 +00001322 ENCODING("SSECC", ENCODING_IB)
Craig Topper7629d632012-04-03 05:20:24 +00001323 ENCODING("AVXCC", ENCODING_IB)
Sean Callanan04cc3072009-12-19 02:59:52 +00001324 ENCODING("i16imm", ENCODING_Iv)
1325 ENCODING("i16i8imm", ENCODING_IB)
1326 ENCODING("i32imm", ENCODING_Iv)
1327 ENCODING("i64i32imm", ENCODING_ID)
1328 ENCODING("i64i8imm", ENCODING_IB)
1329 ENCODING("i8imm", ENCODING_IB)
Sean Callananc3fd5232011-03-15 01:23:15 +00001330 // This is not a typo. Instructions like BLENDVPD put
1331 // register IDs in 8-bit immediates nowadays.
Craig Topperc30fdbc2012-08-31 15:40:30 +00001332 ENCODING("FR32", ENCODING_IB)
1333 ENCODING("FR64", ENCODING_IB)
Elena Demikhovsky003e7d72013-07-28 08:28:38 +00001334 ENCODING("VR128", ENCODING_IB)
1335 ENCODING("VR256", ENCODING_IB)
1336 ENCODING("FR32X", ENCODING_IB)
1337 ENCODING("FR64X", ENCODING_IB)
1338 ENCODING("VR128X", ENCODING_IB)
1339 ENCODING("VR256X", ENCODING_IB)
1340 ENCODING("VR512", ENCODING_IB)
Sean Callanan04cc3072009-12-19 02:59:52 +00001341 errs() << "Unhandled immediate encoding " << s << "\n";
1342 llvm_unreachable("Unhandled immediate encoding");
1343}
1344
1345OperandEncoding RecognizableInstr::rmRegisterEncodingFromString
1346 (const std::string &s,
1347 bool hasOpSizePrefix) {
1348 ENCODING("GR16", ENCODING_RM)
1349 ENCODING("GR32", ENCODING_RM)
1350 ENCODING("GR64", ENCODING_RM)
1351 ENCODING("GR8", ENCODING_RM)
1352 ENCODING("VR128", ENCODING_RM)
Elena Demikhovsky003e7d72013-07-28 08:28:38 +00001353 ENCODING("VR128X", ENCODING_RM)
Sean Callanan04cc3072009-12-19 02:59:52 +00001354 ENCODING("FR64", ENCODING_RM)
1355 ENCODING("FR32", ENCODING_RM)
Elena Demikhovsky003e7d72013-07-28 08:28:38 +00001356 ENCODING("FR64X", ENCODING_RM)
1357 ENCODING("FR32X", ENCODING_RM)
Sean Callanan04cc3072009-12-19 02:59:52 +00001358 ENCODING("VR64", ENCODING_RM)
Sean Callananc3fd5232011-03-15 01:23:15 +00001359 ENCODING("VR256", ENCODING_RM)
Elena Demikhovsky003e7d72013-07-28 08:28:38 +00001360 ENCODING("VR256X", ENCODING_RM)
1361 ENCODING("VR512", ENCODING_RM)
1362 ENCODING("VK8", ENCODING_RM)
1363 ENCODING("VK16", ENCODING_RM)
Sean Callanan04cc3072009-12-19 02:59:52 +00001364 errs() << "Unhandled R/M register encoding " << s << "\n";
1365 llvm_unreachable("Unhandled R/M register encoding");
1366}
1367
1368OperandEncoding RecognizableInstr::roRegisterEncodingFromString
1369 (const std::string &s,
1370 bool hasOpSizePrefix) {
1371 ENCODING("GR16", ENCODING_REG)
1372 ENCODING("GR32", ENCODING_REG)
1373 ENCODING("GR64", ENCODING_REG)
1374 ENCODING("GR8", ENCODING_REG)
1375 ENCODING("VR128", ENCODING_REG)
1376 ENCODING("FR64", ENCODING_REG)
1377 ENCODING("FR32", ENCODING_REG)
1378 ENCODING("VR64", ENCODING_REG)
1379 ENCODING("SEGMENT_REG", ENCODING_REG)
1380 ENCODING("DEBUG_REG", ENCODING_REG)
Sean Callanane7e1cf92010-05-06 20:59:00 +00001381 ENCODING("CONTROL_REG", ENCODING_REG)
Sean Callananc3fd5232011-03-15 01:23:15 +00001382 ENCODING("VR256", ENCODING_REG)
Elena Demikhovsky003e7d72013-07-28 08:28:38 +00001383 ENCODING("VR256X", ENCODING_REG)
1384 ENCODING("VR128X", ENCODING_REG)
1385 ENCODING("FR64X", ENCODING_REG)
1386 ENCODING("FR32X", ENCODING_REG)
1387 ENCODING("VR512", ENCODING_REG)
1388 ENCODING("VK8", ENCODING_REG)
1389 ENCODING("VK16", ENCODING_REG)
1390 ENCODING("VK8WM", ENCODING_REG)
1391 ENCODING("VK16WM", ENCODING_REG)
Sean Callanan04cc3072009-12-19 02:59:52 +00001392 errs() << "Unhandled reg/opcode register encoding " << s << "\n";
1393 llvm_unreachable("Unhandled reg/opcode register encoding");
1394}
1395
Sean Callananc3fd5232011-03-15 01:23:15 +00001396OperandEncoding RecognizableInstr::vvvvRegisterEncodingFromString
1397 (const std::string &s,
1398 bool hasOpSizePrefix) {
Craig Topper965de2c2011-10-14 07:06:56 +00001399 ENCODING("GR32", ENCODING_VVVV)
1400 ENCODING("GR64", ENCODING_VVVV)
Sean Callananc3fd5232011-03-15 01:23:15 +00001401 ENCODING("FR32", ENCODING_VVVV)
1402 ENCODING("FR64", ENCODING_VVVV)
1403 ENCODING("VR128", ENCODING_VVVV)
1404 ENCODING("VR256", ENCODING_VVVV)
Elena Demikhovsky003e7d72013-07-28 08:28:38 +00001405 ENCODING("FR32X", ENCODING_VVVV)
1406 ENCODING("FR64X", ENCODING_VVVV)
1407 ENCODING("VR128X", ENCODING_VVVV)
1408 ENCODING("VR256X", ENCODING_VVVV)
1409 ENCODING("VR512", ENCODING_VVVV)
1410 ENCODING("VK8", ENCODING_VVVV)
1411 ENCODING("VK16", ENCODING_VVVV)
Sean Callananc3fd5232011-03-15 01:23:15 +00001412 errs() << "Unhandled VEX.vvvv register encoding " << s << "\n";
1413 llvm_unreachable("Unhandled VEX.vvvv register encoding");
1414}
1415
Elena Demikhovsky003e7d72013-07-28 08:28:38 +00001416OperandEncoding RecognizableInstr::writemaskRegisterEncodingFromString
1417 (const std::string &s,
1418 bool hasOpSizePrefix) {
1419 ENCODING("VK8WM", ENCODING_WRITEMASK)
1420 ENCODING("VK16WM", ENCODING_WRITEMASK)
1421 errs() << "Unhandled mask register encoding " << s << "\n";
1422 llvm_unreachable("Unhandled mask register encoding");
1423}
1424
Sean Callanan04cc3072009-12-19 02:59:52 +00001425OperandEncoding RecognizableInstr::memoryEncodingFromString
1426 (const std::string &s,
1427 bool hasOpSizePrefix) {
1428 ENCODING("i16mem", ENCODING_RM)
1429 ENCODING("i32mem", ENCODING_RM)
1430 ENCODING("i64mem", ENCODING_RM)
1431 ENCODING("i8mem", ENCODING_RM)
Chris Lattnerf60062f2010-09-29 02:57:56 +00001432 ENCODING("ssmem", ENCODING_RM)
1433 ENCODING("sdmem", ENCODING_RM)
Sean Callanan04cc3072009-12-19 02:59:52 +00001434 ENCODING("f128mem", ENCODING_RM)
Chris Lattnerf60062f2010-09-29 02:57:56 +00001435 ENCODING("f256mem", ENCODING_RM)
Elena Demikhovsky003e7d72013-07-28 08:28:38 +00001436 ENCODING("f512mem", ENCODING_RM)
Sean Callanan04cc3072009-12-19 02:59:52 +00001437 ENCODING("f64mem", ENCODING_RM)
1438 ENCODING("f32mem", ENCODING_RM)
1439 ENCODING("i128mem", ENCODING_RM)
Sean Callananc3fd5232011-03-15 01:23:15 +00001440 ENCODING("i256mem", ENCODING_RM)
Elena Demikhovsky003e7d72013-07-28 08:28:38 +00001441 ENCODING("i512mem", ENCODING_RM)
Sean Callanan04cc3072009-12-19 02:59:52 +00001442 ENCODING("f80mem", ENCODING_RM)
1443 ENCODING("lea32mem", ENCODING_RM)
1444 ENCODING("lea64_32mem", ENCODING_RM)
1445 ENCODING("lea64mem", ENCODING_RM)
1446 ENCODING("opaque32mem", ENCODING_RM)
1447 ENCODING("opaque48mem", ENCODING_RM)
1448 ENCODING("opaque80mem", ENCODING_RM)
1449 ENCODING("opaque512mem", ENCODING_RM)
Craig Topper01deb5f2012-07-18 04:11:12 +00001450 ENCODING("vx32mem", ENCODING_RM)
1451 ENCODING("vy32mem", ENCODING_RM)
Elena Demikhovsky003e7d72013-07-28 08:28:38 +00001452 ENCODING("vz32mem", ENCODING_RM)
Craig Topper01deb5f2012-07-18 04:11:12 +00001453 ENCODING("vx64mem", ENCODING_RM)
1454 ENCODING("vy64mem", ENCODING_RM)
Elena Demikhovsky003e7d72013-07-28 08:28:38 +00001455 ENCODING("vy64xmem", ENCODING_RM)
1456 ENCODING("vz64mem", ENCODING_RM)
Sean Callanan04cc3072009-12-19 02:59:52 +00001457 errs() << "Unhandled memory encoding " << s << "\n";
1458 llvm_unreachable("Unhandled memory encoding");
1459}
1460
1461OperandEncoding RecognizableInstr::relocationEncodingFromString
1462 (const std::string &s,
1463 bool hasOpSizePrefix) {
1464 if(!hasOpSizePrefix) {
1465 // For instructions without an OpSize prefix, a declared 16-bit register or
1466 // immediate encoding is special.
1467 ENCODING("i16imm", ENCODING_IW)
1468 }
1469 ENCODING("i16imm", ENCODING_Iv)
1470 ENCODING("i16i8imm", ENCODING_IB)
1471 ENCODING("i32imm", ENCODING_Iv)
1472 ENCODING("i32i8imm", ENCODING_IB)
1473 ENCODING("i64i32imm", ENCODING_ID)
1474 ENCODING("i64i8imm", ENCODING_IB)
1475 ENCODING("i8imm", ENCODING_IB)
1476 ENCODING("i64i32imm_pcrel", ENCODING_ID)
Chris Lattnerac588122010-07-07 22:27:31 +00001477 ENCODING("i16imm_pcrel", ENCODING_IW)
Sean Callanan04cc3072009-12-19 02:59:52 +00001478 ENCODING("i32imm_pcrel", ENCODING_ID)
1479 ENCODING("brtarget", ENCODING_Iv)
1480 ENCODING("brtarget8", ENCODING_IB)
1481 ENCODING("i64imm", ENCODING_IO)
1482 ENCODING("offset8", ENCODING_Ia)
1483 ENCODING("offset16", ENCODING_Ia)
1484 ENCODING("offset32", ENCODING_Ia)
1485 ENCODING("offset64", ENCODING_Ia)
1486 errs() << "Unhandled relocation encoding " << s << "\n";
1487 llvm_unreachable("Unhandled relocation encoding");
1488}
1489
1490OperandEncoding RecognizableInstr::opcodeModifierEncodingFromString
1491 (const std::string &s,
1492 bool hasOpSizePrefix) {
1493 ENCODING("RST", ENCODING_I)
1494 ENCODING("GR32", ENCODING_Rv)
1495 ENCODING("GR64", ENCODING_RO)
1496 ENCODING("GR16", ENCODING_Rv)
1497 ENCODING("GR8", ENCODING_RB)
Craig Topper23eb4682011-10-06 06:44:41 +00001498 ENCODING("GR16_NOAX", ENCODING_Rv)
1499 ENCODING("GR32_NOAX", ENCODING_Rv)
1500 ENCODING("GR64_NOAX", ENCODING_RO)
Sean Callanan04cc3072009-12-19 02:59:52 +00001501 errs() << "Unhandled opcode modifier encoding " << s << "\n";
1502 llvm_unreachable("Unhandled opcode modifier encoding");
1503}
Daniel Dunbarf008ea52009-12-19 04:16:48 +00001504#undef ENCODING