blob: 532e7307050a9a5c4db7b1a537a0c2ec173f7538 [file] [log] [blame]
Sean Callanan8ed9f512009-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
17#include "X86DisassemblerShared.h"
18#include "X86RecognizableInstr.h"
19#include "X86ModRMFilters.h"
20
21#include "llvm/Support/ErrorHandling.h"
22
23#include <string>
24
25using namespace llvm;
26
Sean Callanan9492be82010-02-12 23:39:46 +000027#define MRM_MAPPING \
28 MAP(C1, 33) \
Chris Lattnera599de22010-02-13 00:41:14 +000029 MAP(C2, 34) \
30 MAP(C3, 35) \
31 MAP(C4, 36) \
32 MAP(C8, 37) \
33 MAP(C9, 38) \
34 MAP(E8, 39) \
35 MAP(F0, 40) \
Duncan Sands34727662010-07-12 08:16:59 +000036 MAP(F8, 41) \
Rafael Espindola87ca0e02011-02-22 00:35:18 +000037 MAP(F9, 42) \
38 MAP(D0, 45) \
39 MAP(D1, 46)
Sean Callanan9492be82010-02-12 23:39:46 +000040
Sean Callanan8ed9f512009-12-19 02:59:52 +000041// A clone of X86 since we can't depend on something that is generated.
42namespace X86Local {
43 enum {
44 Pseudo = 0,
45 RawFrm = 1,
46 AddRegFrm = 2,
47 MRMDestReg = 3,
48 MRMDestMem = 4,
49 MRMSrcReg = 5,
50 MRMSrcMem = 6,
51 MRM0r = 16, MRM1r = 17, MRM2r = 18, MRM3r = 19,
52 MRM4r = 20, MRM5r = 21, MRM6r = 22, MRM7r = 23,
53 MRM0m = 24, MRM1m = 25, MRM2m = 26, MRM3m = 27,
54 MRM4m = 28, MRM5m = 29, MRM6m = 30, MRM7m = 31,
Sean Callanan9492be82010-02-12 23:39:46 +000055 MRMInitReg = 32,
Sean Callanan9492be82010-02-12 23:39:46 +000056#define MAP(from, to) MRM_##from = to,
57 MRM_MAPPING
58#undef MAP
Sean Callanan6aeb2e32010-10-04 22:45:51 +000059 RawFrmImm8 = 43,
60 RawFrmImm16 = 44,
Sean Callanan9492be82010-02-12 23:39:46 +000061 lastMRM
Sean Callanan8ed9f512009-12-19 02:59:52 +000062 };
63
64 enum {
65 TB = 1,
66 REP = 2,
67 D8 = 3, D9 = 4, DA = 5, DB = 6,
68 DC = 7, DD = 8, DE = 9, DF = 10,
69 XD = 11, XS = 12,
Chris Lattner0d8db8e2010-02-12 02:06:33 +000070 T8 = 13, P_TA = 14,
Kevin Enderbyfff64ca2011-08-29 22:06:28 +000071 A6 = 15, A7 = 16, TF = 17
Sean Callanan8ed9f512009-12-19 02:59:52 +000072 };
73}
Sean Callanan9492be82010-02-12 23:39:46 +000074
75// If rows are added to the opcode extension tables, then corresponding entries
76// must be added here.
77//
78// If the row corresponds to a single byte (i.e., 8f), then add an entry for
79// that byte to ONE_BYTE_EXTENSION_TABLES.
80//
81// If the row corresponds to two bytes where the first is 0f, add an entry for
82// the second byte to TWO_BYTE_EXTENSION_TABLES.
83//
84// If the row corresponds to some other set of bytes, you will need to modify
85// the code in RecognizableInstr::emitDecodePath() as well, and add new prefixes
86// to the X86 TD files, except in two cases: if the first two bytes of such a
87// new combination are 0f 38 or 0f 3a, you just have to add maps called
88// THREE_BYTE_38_EXTENSION_TABLES and THREE_BYTE_3A_EXTENSION_TABLES and add a
89// switch(Opcode) just below the case X86Local::T8: or case X86Local::TA: line
90// in RecognizableInstr::emitDecodePath().
91
Sean Callanan8ed9f512009-12-19 02:59:52 +000092#define ONE_BYTE_EXTENSION_TABLES \
93 EXTENSION_TABLE(80) \
94 EXTENSION_TABLE(81) \
95 EXTENSION_TABLE(82) \
96 EXTENSION_TABLE(83) \
97 EXTENSION_TABLE(8f) \
98 EXTENSION_TABLE(c0) \
99 EXTENSION_TABLE(c1) \
100 EXTENSION_TABLE(c6) \
101 EXTENSION_TABLE(c7) \
102 EXTENSION_TABLE(d0) \
103 EXTENSION_TABLE(d1) \
104 EXTENSION_TABLE(d2) \
105 EXTENSION_TABLE(d3) \
106 EXTENSION_TABLE(f6) \
107 EXTENSION_TABLE(f7) \
108 EXTENSION_TABLE(fe) \
109 EXTENSION_TABLE(ff)
110
111#define TWO_BYTE_EXTENSION_TABLES \
112 EXTENSION_TABLE(00) \
113 EXTENSION_TABLE(01) \
114 EXTENSION_TABLE(18) \
115 EXTENSION_TABLE(71) \
116 EXTENSION_TABLE(72) \
117 EXTENSION_TABLE(73) \
118 EXTENSION_TABLE(ae) \
Sean Callanan8ed9f512009-12-19 02:59:52 +0000119 EXTENSION_TABLE(ba) \
120 EXTENSION_TABLE(c7)
Sean Callanan8ed9f512009-12-19 02:59:52 +0000121
122using namespace X86Disassembler;
123
124/// needsModRMForDecode - Indicates whether a particular instruction requires a
125/// ModR/M byte for the instruction to be properly decoded. For example, a
126/// MRMDestReg instruction needs the Mod field in the ModR/M byte to be set to
127/// 0b11.
128///
129/// @param form - The form of the instruction.
130/// @return - true if the form implies that a ModR/M byte is required, false
131/// otherwise.
132static bool needsModRMForDecode(uint8_t form) {
133 if (form == X86Local::MRMDestReg ||
134 form == X86Local::MRMDestMem ||
135 form == X86Local::MRMSrcReg ||
136 form == X86Local::MRMSrcMem ||
137 (form >= X86Local::MRM0r && form <= X86Local::MRM7r) ||
138 (form >= X86Local::MRM0m && form <= X86Local::MRM7m))
139 return true;
140 else
141 return false;
142}
143
144/// isRegFormat - Indicates whether a particular form requires the Mod field of
145/// the ModR/M byte to be 0b11.
146///
147/// @param form - The form of the instruction.
148/// @return - true if the form implies that Mod must be 0b11, false
149/// otherwise.
150static bool isRegFormat(uint8_t form) {
151 if (form == X86Local::MRMDestReg ||
152 form == X86Local::MRMSrcReg ||
153 (form >= X86Local::MRM0r && form <= X86Local::MRM7r))
154 return true;
155 else
156 return false;
157}
158
159/// byteFromBitsInit - Extracts a value at most 8 bits in width from a BitsInit.
160/// Useful for switch statements and the like.
161///
162/// @param init - A reference to the BitsInit to be decoded.
163/// @return - The field, with the first bit in the BitsInit as the lowest
164/// order bit.
David Greene05bce0b2011-07-29 22:43:06 +0000165static uint8_t byteFromBitsInit(BitsInit &init) {
Sean Callanan8ed9f512009-12-19 02:59:52 +0000166 int width = init.getNumBits();
167
168 assert(width <= 8 && "Field is too large for uint8_t!");
169
170 int index;
171 uint8_t mask = 0x01;
172
173 uint8_t ret = 0;
174
175 for (index = 0; index < width; index++) {
David Greene05bce0b2011-07-29 22:43:06 +0000176 if (static_cast<BitInit*>(init.getBit(index))->getValue())
Sean Callanan8ed9f512009-12-19 02:59:52 +0000177 ret |= mask;
178
179 mask <<= 1;
180 }
181
182 return ret;
183}
184
185/// byteFromRec - Extract a value at most 8 bits in with from a Record given the
186/// name of the field.
187///
188/// @param rec - The record from which to extract the value.
189/// @param name - The name of the field in the record.
190/// @return - The field, as translated by byteFromBitsInit().
191static uint8_t byteFromRec(const Record* rec, const std::string &name) {
David Greene05bce0b2011-07-29 22:43:06 +0000192 BitsInit* bits = rec->getValueAsBitsInit(name);
Sean Callanan8ed9f512009-12-19 02:59:52 +0000193 return byteFromBitsInit(*bits);
194}
195
196RecognizableInstr::RecognizableInstr(DisassemblerTables &tables,
197 const CodeGenInstruction &insn,
198 InstrUID uid) {
199 UID = uid;
200
201 Rec = insn.TheDef;
202 Name = Rec->getName();
203 Spec = &tables.specForUID(UID);
204
205 if (!Rec->isSubClassOf("X86Inst")) {
206 ShouldBeEmitted = false;
207 return;
208 }
209
210 Prefix = byteFromRec(Rec, "Prefix");
211 Opcode = byteFromRec(Rec, "Opcode");
212 Form = byteFromRec(Rec, "FormBits");
213 SegOvr = byteFromRec(Rec, "SegOvrBits");
214
215 HasOpSizePrefix = Rec->getValueAsBit("hasOpSizePrefix");
216 HasREX_WPrefix = Rec->getValueAsBit("hasREX_WPrefix");
Sean Callanana21e2ea2011-03-15 01:23:15 +0000217 HasVEXPrefix = Rec->getValueAsBit("hasVEXPrefix");
Bruno Cardoso Lopes99405df2010-06-08 22:51:23 +0000218 HasVEX_4VPrefix = Rec->getValueAsBit("hasVEX_4VPrefix");
Sean Callanana21e2ea2011-03-15 01:23:15 +0000219 HasVEX_WPrefix = Rec->getValueAsBit("hasVEX_WPrefix");
Sean Callanan8ed9f512009-12-19 02:59:52 +0000220 HasLockPrefix = Rec->getValueAsBit("hasLockPrefix");
221 IsCodeGenOnly = Rec->getValueAsBit("isCodeGenOnly");
222
223 Name = Rec->getName();
224 AsmString = Rec->getValueAsString("AsmString");
225
Chris Lattnerc240bb02010-11-01 04:03:32 +0000226 Operands = &insn.Operands.OperandList;
Sean Callanan8ed9f512009-12-19 02:59:52 +0000227
Kevin Enderby98f213c2011-09-02 18:03:03 +0000228 IsSSE = (HasOpSizePrefix && (Name.find("16") == Name.npos)) ||
229 (Name.find("CRC32") != Name.npos);
Sean Callanana21e2ea2011-03-15 01:23:15 +0000230 HasFROperands = hasFROperands();
231 HasVEX_LPrefix = has256BitOperands() || Rec->getValueAsBit("hasVEX_L");
Sean Callanan8ed9f512009-12-19 02:59:52 +0000232
Eli Friedman71052592011-07-16 02:41:28 +0000233 // Check for 64-bit inst which does not require REX
Craig Topper4da632e2011-09-23 06:57:25 +0000234 Is32Bit = false;
Eli Friedman71052592011-07-16 02:41:28 +0000235 Is64Bit = false;
236 // FIXME: Is there some better way to check for In64BitMode?
237 std::vector<Record*> Predicates = Rec->getValueAsListOfDefs("Predicates");
238 for (unsigned i = 0, e = Predicates.size(); i != e; ++i) {
Craig Topper4da632e2011-09-23 06:57:25 +0000239 if (Predicates[i]->getName().find("32Bit") != Name.npos) {
240 Is32Bit = true;
241 break;
242 }
Eli Friedman71052592011-07-16 02:41:28 +0000243 if (Predicates[i]->getName().find("64Bit") != Name.npos) {
244 Is64Bit = true;
245 break;
246 }
247 }
248 // FIXME: These instructions aren't marked as 64-bit in any way
249 Is64Bit |= Rec->getName() == "JMP64pcrel32" ||
250 Rec->getName() == "MASKMOVDQU64" ||
251 Rec->getName() == "POPFS64" ||
252 Rec->getName() == "POPGS64" ||
253 Rec->getName() == "PUSHFS64" ||
254 Rec->getName() == "PUSHGS64" ||
255 Rec->getName() == "REX64_PREFIX" ||
256 Rec->getName().find("VMREAD64") != Name.npos ||
257 Rec->getName().find("VMWRITE64") != Name.npos ||
258 Rec->getName().find("MOV64") != Name.npos ||
259 Rec->getName().find("PUSH64") != Name.npos ||
260 Rec->getName().find("POP64") != Name.npos;
261
Sean Callanan8ed9f512009-12-19 02:59:52 +0000262 ShouldBeEmitted = true;
263}
264
265void RecognizableInstr::processInstr(DisassemblerTables &tables,
Kevin Enderbyfff64ca2011-08-29 22:06:28 +0000266 const CodeGenInstruction &insn,
Sean Callanan8ed9f512009-12-19 02:59:52 +0000267 InstrUID uid)
268{
Daniel Dunbar40728862010-05-20 20:20:32 +0000269 // Ignore "asm parser only" instructions.
270 if (insn.TheDef->getValueAsBit("isAsmParserOnly"))
271 return;
272
Sean Callanan8ed9f512009-12-19 02:59:52 +0000273 RecognizableInstr recogInstr(tables, insn, uid);
274
275 recogInstr.emitInstructionSpecifier(tables);
276
277 if (recogInstr.shouldBeEmitted())
278 recogInstr.emitDecodePath(tables);
279}
280
281InstructionContext RecognizableInstr::insnContext() const {
282 InstructionContext insnContext;
283
Sean Callanana21e2ea2011-03-15 01:23:15 +0000284 if (HasVEX_4VPrefix || HasVEXPrefix) {
285 if (HasOpSizePrefix && HasVEX_LPrefix)
286 insnContext = IC_VEX_L_OPSIZE;
287 else if (HasOpSizePrefix && HasVEX_WPrefix)
288 insnContext = IC_VEX_W_OPSIZE;
289 else if (HasOpSizePrefix)
290 insnContext = IC_VEX_OPSIZE;
291 else if (HasVEX_LPrefix && Prefix == X86Local::XS)
292 insnContext = IC_VEX_L_XS;
293 else if (HasVEX_LPrefix && Prefix == X86Local::XD)
294 insnContext = IC_VEX_L_XD;
295 else if (HasVEX_WPrefix && Prefix == X86Local::XS)
296 insnContext = IC_VEX_W_XS;
297 else if (HasVEX_WPrefix && Prefix == X86Local::XD)
298 insnContext = IC_VEX_W_XD;
299 else if (HasVEX_WPrefix)
300 insnContext = IC_VEX_W;
301 else if (HasVEX_LPrefix)
302 insnContext = IC_VEX_L;
303 else if (Prefix == X86Local::XD)
304 insnContext = IC_VEX_XD;
305 else if (Prefix == X86Local::XS)
306 insnContext = IC_VEX_XS;
307 else
308 insnContext = IC_VEX;
Eli Friedman71052592011-07-16 02:41:28 +0000309 } else if (Is64Bit || HasREX_WPrefix) {
Sean Callanan8ed9f512009-12-19 02:59:52 +0000310 if (HasREX_WPrefix && HasOpSizePrefix)
311 insnContext = IC_64BIT_REXW_OPSIZE;
Craig Toppere1b4a1a2011-10-01 19:54:56 +0000312 else if (HasOpSizePrefix && (Prefix == X86Local::XD || Prefix == X86Local::TF))
313 insnContext = IC_64BIT_XD_OPSIZE;
Sean Callanan8ed9f512009-12-19 02:59:52 +0000314 else if (HasOpSizePrefix)
315 insnContext = IC_64BIT_OPSIZE;
316 else if (HasREX_WPrefix && Prefix == X86Local::XS)
317 insnContext = IC_64BIT_REXW_XS;
Craig Toppere1b4a1a2011-10-01 19:54:56 +0000318 else if (HasREX_WPrefix && (Prefix == X86Local::XD || Prefix == X86Local::TF))
Sean Callanan8ed9f512009-12-19 02:59:52 +0000319 insnContext = IC_64BIT_REXW_XD;
Craig Toppere1b4a1a2011-10-01 19:54:56 +0000320 else if (Prefix == X86Local::XD || Prefix == X86Local::TF)
Sean Callanan8ed9f512009-12-19 02:59:52 +0000321 insnContext = IC_64BIT_XD;
322 else if (Prefix == X86Local::XS)
323 insnContext = IC_64BIT_XS;
324 else if (HasREX_WPrefix)
325 insnContext = IC_64BIT_REXW;
326 else
327 insnContext = IC_64BIT;
328 } else {
Craig Toppere1b4a1a2011-10-01 19:54:56 +0000329 if (HasOpSizePrefix &&
330 (Prefix == X86Local::XD || Prefix == X86Local::TF))
331 insnContext = IC_XD_OPSIZE;
Kevin Enderby98f213c2011-09-02 18:03:03 +0000332 else if (HasOpSizePrefix)
Sean Callanan8ed9f512009-12-19 02:59:52 +0000333 insnContext = IC_OPSIZE;
Craig Toppere1b4a1a2011-10-01 19:54:56 +0000334 else if (Prefix == X86Local::XD || Prefix == X86Local::TF)
Sean Callanan8ed9f512009-12-19 02:59:52 +0000335 insnContext = IC_XD;
Craig Topper842f58f2011-09-11 20:23:20 +0000336 else if (Prefix == X86Local::XS || Prefix == X86Local::REP)
Sean Callanan8ed9f512009-12-19 02:59:52 +0000337 insnContext = IC_XS;
338 else
339 insnContext = IC;
340 }
341
342 return insnContext;
343}
344
345RecognizableInstr::filter_ret RecognizableInstr::filter() const {
Sean Callanana21e2ea2011-03-15 01:23:15 +0000346 ///////////////////
347 // FILTER_STRONG
348 //
349
Sean Callanan8ed9f512009-12-19 02:59:52 +0000350 // Filter out intrinsics
351
352 if (!Rec->isSubClassOf("X86Inst"))
353 return FILTER_STRONG;
354
355 if (Form == X86Local::Pseudo ||
Craig Topper03819792011-09-11 21:41:45 +0000356 (IsCodeGenOnly && Name.find("_REV") == Name.npos))
Sean Callanan8ed9f512009-12-19 02:59:52 +0000357 return FILTER_STRONG;
358
Sean Callanan80443f92010-02-24 02:56:25 +0000359 if (Form == X86Local::MRMInitReg)
360 return FILTER_STRONG;
Sean Callanana21e2ea2011-03-15 01:23:15 +0000361
362
Sean Callanana21e2ea2011-03-15 01:23:15 +0000363 // Filter out artificial instructions
364
365 if (Name.find("TAILJMP") != Name.npos ||
366 Name.find("_Int") != Name.npos ||
367 Name.find("_int") != Name.npos ||
368 Name.find("Int_") != Name.npos ||
369 Name.find("_NOREX") != Name.npos ||
370 Name.find("_TC") != Name.npos ||
371 Name.find("EH_RETURN") != Name.npos ||
372 Name.find("V_SET") != Name.npos ||
373 Name.find("LOCK_") != Name.npos ||
374 Name.find("WIN") != Name.npos ||
375 Name.find("_AVX") != Name.npos ||
376 Name.find("2SDL") != Name.npos)
377 return FILTER_STRONG;
378
379 // Filter out instructions with segment override prefixes.
380 // They're too messy to handle now and we'll special case them if needed.
381
382 if (SegOvr)
383 return FILTER_STRONG;
384
385 // Filter out instructions that can't be printed.
386
387 if (AsmString.size() == 0)
388 return FILTER_STRONG;
389
390 // Filter out instructions with subreg operands.
391
392 if (AsmString.find("subreg") != AsmString.npos)
393 return FILTER_STRONG;
394
395 /////////////////
396 // FILTER_WEAK
397 //
398
399
Sean Callanan8ed9f512009-12-19 02:59:52 +0000400 // Filter out instructions with a LOCK prefix;
401 // prefer forms that do not have the prefix
402 if (HasLockPrefix)
403 return FILTER_WEAK;
Sean Callanan8ed9f512009-12-19 02:59:52 +0000404
Sean Callanana21e2ea2011-03-15 01:23:15 +0000405 // Filter out alternate forms of AVX instructions
406 if (Name.find("_alt") != Name.npos ||
407 Name.find("XrYr") != Name.npos ||
Craig Toppere1b4a1a2011-10-01 19:54:56 +0000408 (Name.find("r64r") != Name.npos && Name.find("r64r64") == Name.npos) ||
Sean Callanana21e2ea2011-03-15 01:23:15 +0000409 Name.find("_64mr") != Name.npos ||
410 Name.find("Xrr") != Name.npos ||
411 Name.find("rr64") != Name.npos)
412 return FILTER_WEAK;
413
414 if (Name == "VMASKMOVDQU64" ||
415 Name == "VEXTRACTPSrr64" ||
416 Name == "VMOVQd64rr" ||
417 Name == "VMOVQs64rr")
418 return FILTER_WEAK;
Sean Callanan8ed9f512009-12-19 02:59:52 +0000419
420 // Special cases.
Dale Johannesen86097c32010-09-07 18:10:56 +0000421
Sean Callanan8ed9f512009-12-19 02:59:52 +0000422 if (Name.find("PCMPISTRI") != Name.npos && Name != "PCMPISTRI")
423 return FILTER_WEAK;
424 if (Name.find("PCMPESTRI") != Name.npos && Name != "PCMPESTRI")
425 return FILTER_WEAK;
426
427 if (Name.find("MOV") != Name.npos && Name.find("r0") != Name.npos)
428 return FILTER_WEAK;
429 if (Name.find("MOVZ") != Name.npos && Name.find("MOVZX") == Name.npos)
430 return FILTER_WEAK;
431 if (Name.find("Fs") != Name.npos)
432 return FILTER_WEAK;
433 if (Name == "MOVLPDrr" ||
434 Name == "MOVLPSrr" ||
435 Name == "PUSHFQ" ||
436 Name == "BSF16rr" ||
437 Name == "BSF16rm" ||
438 Name == "BSR16rr" ||
439 Name == "BSR16rm" ||
440 Name == "MOVSX16rm8" ||
441 Name == "MOVSX16rr8" ||
442 Name == "MOVZX16rm8" ||
443 Name == "MOVZX16rr8" ||
444 Name == "PUSH32i16" ||
445 Name == "PUSH64i16" ||
446 Name == "MOVPQI2QImr" ||
Sean Callanana21e2ea2011-03-15 01:23:15 +0000447 Name == "VMOVPQI2QImr" ||
Sean Callanan8ed9f512009-12-19 02:59:52 +0000448 Name == "MOVSDmr" ||
449 Name == "MOVSDrm" ||
450 Name == "MOVSSmr" ||
451 Name == "MOVSSrm" ||
452 Name == "MMX_MOVD64rrv164" ||
453 Name == "CRC32m16" ||
454 Name == "MOV64ri64i32" ||
455 Name == "CRC32r16")
456 return FILTER_WEAK;
457
Sean Callanan8ed9f512009-12-19 02:59:52 +0000458 if (HasFROperands && Name.find("MOV") != Name.npos &&
459 ((Name.find("2") != Name.npos && Name.find("32") == Name.npos) ||
460 (Name.find("to") != Name.npos)))
461 return FILTER_WEAK;
462
463 return FILTER_NORMAL;
464}
Sean Callanana21e2ea2011-03-15 01:23:15 +0000465
466bool RecognizableInstr::hasFROperands() const {
467 const std::vector<CGIOperandList::OperandInfo> &OperandList = *Operands;
468 unsigned numOperands = OperandList.size();
469
470 for (unsigned operandIndex = 0; operandIndex < numOperands; ++operandIndex) {
471 const std::string &recName = OperandList[operandIndex].Rec->getName();
472
473 if (recName.find("FR") != recName.npos)
474 return true;
475 }
476 return false;
477}
478
479bool RecognizableInstr::has256BitOperands() const {
480 const std::vector<CGIOperandList::OperandInfo> &OperandList = *Operands;
481 unsigned numOperands = OperandList.size();
482
483 for (unsigned operandIndex = 0; operandIndex < numOperands; ++operandIndex) {
484 const std::string &recName = OperandList[operandIndex].Rec->getName();
485
486 if (!recName.compare("VR256") || !recName.compare("f256mem")) {
487 return true;
488 }
489 }
490 return false;
491}
Sean Callanan8ed9f512009-12-19 02:59:52 +0000492
493void RecognizableInstr::handleOperand(
494 bool optional,
495 unsigned &operandIndex,
496 unsigned &physicalOperandIndex,
497 unsigned &numPhysicalOperands,
498 unsigned *operandMapping,
499 OperandEncoding (*encodingFromString)(const std::string&, bool hasOpSizePrefix)) {
500 if (optional) {
501 if (physicalOperandIndex >= numPhysicalOperands)
502 return;
503 } else {
504 assert(physicalOperandIndex < numPhysicalOperands);
505 }
506
507 while (operandMapping[operandIndex] != operandIndex) {
508 Spec->operands[operandIndex].encoding = ENCODING_DUP;
509 Spec->operands[operandIndex].type =
510 (OperandType)(TYPE_DUP0 + operandMapping[operandIndex]);
511 ++operandIndex;
512 }
513
514 const std::string &typeName = (*Operands)[operandIndex].Rec->getName();
Sean Callanana21e2ea2011-03-15 01:23:15 +0000515
Sean Callanan8ed9f512009-12-19 02:59:52 +0000516 Spec->operands[operandIndex].encoding = encodingFromString(typeName,
517 HasOpSizePrefix);
518 Spec->operands[operandIndex].type = typeFromString(typeName,
Sean Callanana21e2ea2011-03-15 01:23:15 +0000519 IsSSE,
520 HasREX_WPrefix,
521 HasOpSizePrefix);
Sean Callanan8ed9f512009-12-19 02:59:52 +0000522
523 ++operandIndex;
524 ++physicalOperandIndex;
525}
526
527void RecognizableInstr::emitInstructionSpecifier(DisassemblerTables &tables) {
528 Spec->name = Name;
529
530 if (!Rec->isSubClassOf("X86Inst"))
531 return;
532
533 switch (filter()) {
534 case FILTER_WEAK:
535 Spec->filtered = true;
536 break;
537 case FILTER_STRONG:
538 ShouldBeEmitted = false;
539 return;
540 case FILTER_NORMAL:
541 break;
542 }
543
544 Spec->insnContext = insnContext();
545
Chris Lattnerc240bb02010-11-01 04:03:32 +0000546 const std::vector<CGIOperandList::OperandInfo> &OperandList = *Operands;
Sean Callanan8ed9f512009-12-19 02:59:52 +0000547
548 unsigned operandIndex;
549 unsigned numOperands = OperandList.size();
550 unsigned numPhysicalOperands = 0;
551
552 // operandMapping maps from operands in OperandList to their originals.
553 // If operandMapping[i] != i, then the entry is a duplicate.
554 unsigned operandMapping[X86_MAX_OPERANDS];
555
556 bool hasFROperands = false;
557
558 assert(numOperands < X86_MAX_OPERANDS && "X86_MAX_OPERANDS is not large enough");
559
560 for (operandIndex = 0; operandIndex < numOperands; ++operandIndex) {
561 if (OperandList[operandIndex].Constraints.size()) {
Chris Lattnerc240bb02010-11-01 04:03:32 +0000562 const CGIOperandList::ConstraintInfo &Constraint =
Chris Lattnera7d479c2010-02-10 01:45:28 +0000563 OperandList[operandIndex].Constraints[0];
564 if (Constraint.isTied()) {
565 operandMapping[operandIndex] = Constraint.getTiedOperand();
Sean Callanan8ed9f512009-12-19 02:59:52 +0000566 } else {
567 ++numPhysicalOperands;
568 operandMapping[operandIndex] = operandIndex;
569 }
570 } else {
571 ++numPhysicalOperands;
572 operandMapping[operandIndex] = operandIndex;
573 }
574
575 const std::string &recName = OperandList[operandIndex].Rec->getName();
576
577 if (recName.find("FR") != recName.npos)
578 hasFROperands = true;
579 }
580
581 if (hasFROperands && Name.find("MOV") != Name.npos &&
582 ((Name.find("2") != Name.npos && Name.find("32") == Name.npos) ||
583 (Name.find("to") != Name.npos)))
584 ShouldBeEmitted = false;
585
586 if (!ShouldBeEmitted)
587 return;
588
589#define HANDLE_OPERAND(class) \
590 handleOperand(false, \
591 operandIndex, \
592 physicalOperandIndex, \
593 numPhysicalOperands, \
594 operandMapping, \
595 class##EncodingFromString);
596
597#define HANDLE_OPTIONAL(class) \
598 handleOperand(true, \
599 operandIndex, \
600 physicalOperandIndex, \
601 numPhysicalOperands, \
602 operandMapping, \
603 class##EncodingFromString);
604
605 // operandIndex should always be < numOperands
606 operandIndex = 0;
607 // physicalOperandIndex should always be < numPhysicalOperands
608 unsigned physicalOperandIndex = 0;
609
610 switch (Form) {
611 case X86Local::RawFrm:
612 // Operand 1 (optional) is an address or immediate.
613 // Operand 2 (optional) is an immediate.
614 assert(numPhysicalOperands <= 2 &&
615 "Unexpected number of operands for RawFrm");
616 HANDLE_OPTIONAL(relocation)
617 HANDLE_OPTIONAL(immediate)
618 break;
619 case X86Local::AddRegFrm:
620 // Operand 1 is added to the opcode.
621 // Operand 2 (optional) is an address.
622 assert(numPhysicalOperands >= 1 && numPhysicalOperands <= 2 &&
623 "Unexpected number of operands for AddRegFrm");
624 HANDLE_OPERAND(opcodeModifier)
625 HANDLE_OPTIONAL(relocation)
626 break;
627 case X86Local::MRMDestReg:
628 // Operand 1 is a register operand in the R/M field.
629 // Operand 2 is a register operand in the Reg/Opcode field.
Craig Topper3daa5c22011-08-30 07:09:35 +0000630 // - In AVX, there is a register operand in the VEX.vvvv field here -
Sean Callanan8ed9f512009-12-19 02:59:52 +0000631 // Operand 3 (optional) is an immediate.
Craig Topper3daa5c22011-08-30 07:09:35 +0000632 if (HasVEX_4VPrefix)
633 assert(numPhysicalOperands >= 3 && numPhysicalOperands <= 4 &&
634 "Unexpected number of operands for MRMDestRegFrm with VEX_4V");
635 else
636 assert(numPhysicalOperands >= 2 && numPhysicalOperands <= 3 &&
637 "Unexpected number of operands for MRMDestRegFrm");
638
Sean Callanan8ed9f512009-12-19 02:59:52 +0000639 HANDLE_OPERAND(rmRegister)
Craig Topper3daa5c22011-08-30 07:09:35 +0000640
641 if (HasVEX_4VPrefix)
642 // FIXME: In AVX, the register below becomes the one encoded
643 // in ModRMVEX and the one above the one in the VEX.VVVV field
644 HANDLE_OPERAND(vvvvRegister)
645
Sean Callanan8ed9f512009-12-19 02:59:52 +0000646 HANDLE_OPERAND(roRegister)
647 HANDLE_OPTIONAL(immediate)
648 break;
649 case X86Local::MRMDestMem:
650 // Operand 1 is a memory operand (possibly SIB-extended)
651 // Operand 2 is a register operand in the Reg/Opcode field.
Craig Topper3daa5c22011-08-30 07:09:35 +0000652 // - In AVX, there is a register operand in the VEX.vvvv field here -
Sean Callanan8ed9f512009-12-19 02:59:52 +0000653 // Operand 3 (optional) is an immediate.
Craig Topper3daa5c22011-08-30 07:09:35 +0000654 if (HasVEX_4VPrefix)
655 assert(numPhysicalOperands >= 3 && numPhysicalOperands <= 4 &&
656 "Unexpected number of operands for MRMDestMemFrm with VEX_4V");
657 else
658 assert(numPhysicalOperands >= 2 && numPhysicalOperands <= 3 &&
659 "Unexpected number of operands for MRMDestMemFrm");
Sean Callanan8ed9f512009-12-19 02:59:52 +0000660 HANDLE_OPERAND(memory)
Craig Topper3daa5c22011-08-30 07:09:35 +0000661
662 if (HasVEX_4VPrefix)
663 // FIXME: In AVX, the register below becomes the one encoded
664 // in ModRMVEX and the one above the one in the VEX.VVVV field
665 HANDLE_OPERAND(vvvvRegister)
666
Sean Callanan8ed9f512009-12-19 02:59:52 +0000667 HANDLE_OPERAND(roRegister)
668 HANDLE_OPTIONAL(immediate)
669 break;
670 case X86Local::MRMSrcReg:
671 // Operand 1 is a register operand in the Reg/Opcode field.
672 // Operand 2 is a register operand in the R/M field.
Sean Callanana21e2ea2011-03-15 01:23:15 +0000673 // - In AVX, there is a register operand in the VEX.vvvv field here -
Sean Callanan8ed9f512009-12-19 02:59:52 +0000674 // Operand 3 (optional) is an immediate.
Bruno Cardoso Lopes99405df2010-06-08 22:51:23 +0000675
676 if (HasVEX_4VPrefix)
Sean Callanana21e2ea2011-03-15 01:23:15 +0000677 assert(numPhysicalOperands >= 3 && numPhysicalOperands <= 4 &&
678 "Unexpected number of operands for MRMSrcRegFrm with VEX_4V");
679 else
680 assert(numPhysicalOperands >= 2 && numPhysicalOperands <= 3 &&
681 "Unexpected number of operands for MRMSrcRegFrm");
682
683 HANDLE_OPERAND(roRegister)
684
685 if (HasVEX_4VPrefix)
Bruno Cardoso Lopesc902a592010-06-11 23:50:47 +0000686 // FIXME: In AVX, the register below becomes the one encoded
687 // in ModRMVEX and the one above the one in the VEX.VVVV field
Sean Callanana21e2ea2011-03-15 01:23:15 +0000688 HANDLE_OPERAND(vvvvRegister)
689
690 HANDLE_OPERAND(rmRegister)
691 HANDLE_OPTIONAL(immediate)
Sean Callanan8ed9f512009-12-19 02:59:52 +0000692 break;
693 case X86Local::MRMSrcMem:
694 // Operand 1 is a register operand in the Reg/Opcode field.
695 // Operand 2 is a memory operand (possibly SIB-extended)
Sean Callanana21e2ea2011-03-15 01:23:15 +0000696 // - In AVX, there is a register operand in the VEX.vvvv field here -
Sean Callanan8ed9f512009-12-19 02:59:52 +0000697 // Operand 3 (optional) is an immediate.
Sean Callanana21e2ea2011-03-15 01:23:15 +0000698
699 if (HasVEX_4VPrefix)
700 assert(numPhysicalOperands >= 3 && numPhysicalOperands <= 4 &&
701 "Unexpected number of operands for MRMSrcMemFrm with VEX_4V");
702 else
703 assert(numPhysicalOperands >= 2 && numPhysicalOperands <= 3 &&
704 "Unexpected number of operands for MRMSrcMemFrm");
705
Sean Callanan8ed9f512009-12-19 02:59:52 +0000706 HANDLE_OPERAND(roRegister)
Bruno Cardoso Lopesc902a592010-06-11 23:50:47 +0000707
708 if (HasVEX_4VPrefix)
709 // FIXME: In AVX, the register below becomes the one encoded
710 // in ModRMVEX and the one above the one in the VEX.VVVV field
Sean Callanana21e2ea2011-03-15 01:23:15 +0000711 HANDLE_OPERAND(vvvvRegister)
Bruno Cardoso Lopesc902a592010-06-11 23:50:47 +0000712
Sean Callanan8ed9f512009-12-19 02:59:52 +0000713 HANDLE_OPERAND(memory)
714 HANDLE_OPTIONAL(immediate)
715 break;
716 case X86Local::MRM0r:
717 case X86Local::MRM1r:
718 case X86Local::MRM2r:
719 case X86Local::MRM3r:
720 case X86Local::MRM4r:
721 case X86Local::MRM5r:
722 case X86Local::MRM6r:
723 case X86Local::MRM7r:
724 // Operand 1 is a register operand in the R/M field.
725 // Operand 2 (optional) is an immediate or relocation.
Sean Callanana21e2ea2011-03-15 01:23:15 +0000726 if (HasVEX_4VPrefix)
727 assert(numPhysicalOperands <= 3 &&
728 "Unexpected number of operands for MRMSrcMemFrm with VEX_4V");
729 else
730 assert(numPhysicalOperands <= 2 &&
731 "Unexpected number of operands for MRMnRFrm");
732 if (HasVEX_4VPrefix)
733 HANDLE_OPERAND(vvvvRegister);
Sean Callanan8ed9f512009-12-19 02:59:52 +0000734 HANDLE_OPTIONAL(rmRegister)
735 HANDLE_OPTIONAL(relocation)
736 break;
737 case X86Local::MRM0m:
738 case X86Local::MRM1m:
739 case X86Local::MRM2m:
740 case X86Local::MRM3m:
741 case X86Local::MRM4m:
742 case X86Local::MRM5m:
743 case X86Local::MRM6m:
744 case X86Local::MRM7m:
745 // Operand 1 is a memory operand (possibly SIB-extended)
746 // Operand 2 (optional) is an immediate or relocation.
747 assert(numPhysicalOperands >= 1 && numPhysicalOperands <= 2 &&
748 "Unexpected number of operands for MRMnMFrm");
749 HANDLE_OPERAND(memory)
750 HANDLE_OPTIONAL(relocation)
751 break;
Sean Callanan6aeb2e32010-10-04 22:45:51 +0000752 case X86Local::RawFrmImm8:
753 // operand 1 is a 16-bit immediate
754 // operand 2 is an 8-bit immediate
755 assert(numPhysicalOperands == 2 &&
756 "Unexpected number of operands for X86Local::RawFrmImm8");
757 HANDLE_OPERAND(immediate)
758 HANDLE_OPERAND(immediate)
759 break;
760 case X86Local::RawFrmImm16:
761 // operand 1 is a 16-bit immediate
762 // operand 2 is a 16-bit immediate
763 HANDLE_OPERAND(immediate)
764 HANDLE_OPERAND(immediate)
765 break;
Sean Callanan8ed9f512009-12-19 02:59:52 +0000766 case X86Local::MRMInitReg:
767 // Ignored.
768 break;
769 }
770
771 #undef HANDLE_OPERAND
772 #undef HANDLE_OPTIONAL
773}
774
775void RecognizableInstr::emitDecodePath(DisassemblerTables &tables) const {
776 // Special cases where the LLVM tables are not complete
777
Sean Callanan9492be82010-02-12 23:39:46 +0000778#define MAP(from, to) \
779 case X86Local::MRM_##from: \
780 filter = new ExactFilter(0x##from); \
781 break;
Sean Callanan8ed9f512009-12-19 02:59:52 +0000782
783 OpcodeType opcodeType = (OpcodeType)-1;
784
785 ModRMFilter* filter = NULL;
786 uint8_t opcodeToSet = 0;
787
788 switch (Prefix) {
789 // Extended two-byte opcodes can start with f2 0f, f3 0f, or 0f
790 case X86Local::XD:
791 case X86Local::XS:
792 case X86Local::TB:
793 opcodeType = TWOBYTE;
794
795 switch (Opcode) {
Sean Callanan95a5a7d2010-02-13 01:48:34 +0000796 default:
797 if (needsModRMForDecode(Form))
798 filter = new ModFilter(isRegFormat(Form));
799 else
800 filter = new DumbFilter();
801 break;
Sean Callanan8ed9f512009-12-19 02:59:52 +0000802#define EXTENSION_TABLE(n) case 0x##n:
803 TWO_BYTE_EXTENSION_TABLES
804#undef EXTENSION_TABLE
805 switch (Form) {
806 default:
807 llvm_unreachable("Unhandled two-byte extended opcode");
808 case X86Local::MRM0r:
809 case X86Local::MRM1r:
810 case X86Local::MRM2r:
811 case X86Local::MRM3r:
812 case X86Local::MRM4r:
813 case X86Local::MRM5r:
814 case X86Local::MRM6r:
815 case X86Local::MRM7r:
816 filter = new ExtendedFilter(true, Form - X86Local::MRM0r);
817 break;
818 case X86Local::MRM0m:
819 case X86Local::MRM1m:
820 case X86Local::MRM2m:
821 case X86Local::MRM3m:
822 case X86Local::MRM4m:
823 case X86Local::MRM5m:
824 case X86Local::MRM6m:
825 case X86Local::MRM7m:
826 filter = new ExtendedFilter(false, Form - X86Local::MRM0m);
827 break;
Sean Callanan9492be82010-02-12 23:39:46 +0000828 MRM_MAPPING
Sean Callanan8ed9f512009-12-19 02:59:52 +0000829 } // switch (Form)
830 break;
Sean Callanan95a5a7d2010-02-13 01:48:34 +0000831 } // switch (Opcode)
Sean Callanan8ed9f512009-12-19 02:59:52 +0000832 opcodeToSet = Opcode;
833 break;
834 case X86Local::T8:
Kevin Enderbyfff64ca2011-08-29 22:06:28 +0000835 case X86Local::TF:
Sean Callanan8ed9f512009-12-19 02:59:52 +0000836 opcodeType = THREEBYTE_38;
837 if (needsModRMForDecode(Form))
838 filter = new ModFilter(isRegFormat(Form));
839 else
840 filter = new DumbFilter();
841 opcodeToSet = Opcode;
842 break;
Chris Lattner0d8db8e2010-02-12 02:06:33 +0000843 case X86Local::P_TA:
Sean Callanan8ed9f512009-12-19 02:59:52 +0000844 opcodeType = THREEBYTE_3A;
845 if (needsModRMForDecode(Form))
846 filter = new ModFilter(isRegFormat(Form));
847 else
848 filter = new DumbFilter();
849 opcodeToSet = Opcode;
850 break;
Joerg Sonnenberger4a8ac8d2011-04-04 16:58:13 +0000851 case X86Local::A6:
852 opcodeType = THREEBYTE_A6;
853 if (needsModRMForDecode(Form))
854 filter = new ModFilter(isRegFormat(Form));
855 else
856 filter = new DumbFilter();
857 opcodeToSet = Opcode;
858 break;
859 case X86Local::A7:
860 opcodeType = THREEBYTE_A7;
861 if (needsModRMForDecode(Form))
862 filter = new ModFilter(isRegFormat(Form));
863 else
864 filter = new DumbFilter();
865 opcodeToSet = Opcode;
866 break;
Sean Callanan8ed9f512009-12-19 02:59:52 +0000867 case X86Local::D8:
868 case X86Local::D9:
869 case X86Local::DA:
870 case X86Local::DB:
871 case X86Local::DC:
872 case X86Local::DD:
873 case X86Local::DE:
874 case X86Local::DF:
875 assert(Opcode >= 0xc0 && "Unexpected opcode for an escape opcode");
876 opcodeType = ONEBYTE;
877 if (Form == X86Local::AddRegFrm) {
878 Spec->modifierType = MODIFIER_MODRM;
879 Spec->modifierBase = Opcode;
880 filter = new AddRegEscapeFilter(Opcode);
881 } else {
882 filter = new EscapeFilter(true, Opcode);
883 }
884 opcodeToSet = 0xd8 + (Prefix - X86Local::D8);
885 break;
Craig Topper842f58f2011-09-11 20:23:20 +0000886 case X86Local::REP:
Sean Callanan8ed9f512009-12-19 02:59:52 +0000887 default:
888 opcodeType = ONEBYTE;
889 switch (Opcode) {
890#define EXTENSION_TABLE(n) case 0x##n:
891 ONE_BYTE_EXTENSION_TABLES
892#undef EXTENSION_TABLE
893 switch (Form) {
894 default:
895 llvm_unreachable("Fell through the cracks of a single-byte "
896 "extended opcode");
897 case X86Local::MRM0r:
898 case X86Local::MRM1r:
899 case X86Local::MRM2r:
900 case X86Local::MRM3r:
901 case X86Local::MRM4r:
902 case X86Local::MRM5r:
903 case X86Local::MRM6r:
904 case X86Local::MRM7r:
905 filter = new ExtendedFilter(true, Form - X86Local::MRM0r);
906 break;
907 case X86Local::MRM0m:
908 case X86Local::MRM1m:
909 case X86Local::MRM2m:
910 case X86Local::MRM3m:
911 case X86Local::MRM4m:
912 case X86Local::MRM5m:
913 case X86Local::MRM6m:
914 case X86Local::MRM7m:
915 filter = new ExtendedFilter(false, Form - X86Local::MRM0m);
916 break;
Sean Callanan9492be82010-02-12 23:39:46 +0000917 MRM_MAPPING
Sean Callanan8ed9f512009-12-19 02:59:52 +0000918 } // switch (Form)
919 break;
920 case 0xd8:
921 case 0xd9:
922 case 0xda:
923 case 0xdb:
924 case 0xdc:
925 case 0xdd:
926 case 0xde:
927 case 0xdf:
928 filter = new EscapeFilter(false, Form - X86Local::MRM0m);
929 break;
930 default:
931 if (needsModRMForDecode(Form))
932 filter = new ModFilter(isRegFormat(Form));
933 else
934 filter = new DumbFilter();
935 break;
936 } // switch (Opcode)
937 opcodeToSet = Opcode;
938 } // switch (Prefix)
939
940 assert(opcodeType != (OpcodeType)-1 &&
941 "Opcode type not set");
942 assert(filter && "Filter not set");
943
944 if (Form == X86Local::AddRegFrm) {
945 if(Spec->modifierType != MODIFIER_MODRM) {
946 assert(opcodeToSet < 0xf9 &&
947 "Not enough room for all ADDREG_FRM operands");
948
949 uint8_t currentOpcode;
950
951 for (currentOpcode = opcodeToSet;
952 currentOpcode < opcodeToSet + 8;
953 ++currentOpcode)
954 tables.setTableFields(opcodeType,
955 insnContext(),
956 currentOpcode,
957 *filter,
Craig Topper4da632e2011-09-23 06:57:25 +0000958 UID, Is32Bit);
Sean Callanan8ed9f512009-12-19 02:59:52 +0000959
960 Spec->modifierType = MODIFIER_OPCODE;
961 Spec->modifierBase = opcodeToSet;
962 } else {
963 // modifierBase was set where MODIFIER_MODRM was set
964 tables.setTableFields(opcodeType,
965 insnContext(),
966 opcodeToSet,
967 *filter,
Craig Topper4da632e2011-09-23 06:57:25 +0000968 UID, Is32Bit);
Sean Callanan8ed9f512009-12-19 02:59:52 +0000969 }
970 } else {
971 tables.setTableFields(opcodeType,
972 insnContext(),
973 opcodeToSet,
974 *filter,
Craig Topper4da632e2011-09-23 06:57:25 +0000975 UID, Is32Bit);
Sean Callanan8ed9f512009-12-19 02:59:52 +0000976
977 Spec->modifierType = MODIFIER_NONE;
978 Spec->modifierBase = opcodeToSet;
979 }
980
981 delete filter;
Sean Callanan9492be82010-02-12 23:39:46 +0000982
983#undef MAP
Sean Callanan8ed9f512009-12-19 02:59:52 +0000984}
985
986#define TYPE(str, type) if (s == str) return type;
987OperandType RecognizableInstr::typeFromString(const std::string &s,
988 bool isSSE,
989 bool hasREX_WPrefix,
990 bool hasOpSizePrefix) {
991 if (isSSE) {
992 // For SSE instructions, we ignore the OpSize prefix and force operand
993 // sizes.
994 TYPE("GR16", TYPE_R16)
995 TYPE("GR32", TYPE_R32)
996 TYPE("GR64", TYPE_R64)
997 }
998 if(hasREX_WPrefix) {
999 // For instructions with a REX_W prefix, a declared 32-bit register encoding
1000 // is special.
1001 TYPE("GR32", TYPE_R32)
1002 }
1003 if(!hasOpSizePrefix) {
1004 // For instructions without an OpSize prefix, a declared 16-bit register or
1005 // immediate encoding is special.
1006 TYPE("GR16", TYPE_R16)
1007 TYPE("i16imm", TYPE_IMM16)
1008 }
1009 TYPE("i16mem", TYPE_Mv)
1010 TYPE("i16imm", TYPE_IMMv)
1011 TYPE("i16i8imm", TYPE_IMMv)
1012 TYPE("GR16", TYPE_Rv)
1013 TYPE("i32mem", TYPE_Mv)
1014 TYPE("i32imm", TYPE_IMMv)
1015 TYPE("i32i8imm", TYPE_IMM32)
Kevin Enderbyc37d4bb2011-07-27 23:01:50 +00001016 TYPE("u32u8imm", TYPE_IMM32)
Sean Callanan8ed9f512009-12-19 02:59:52 +00001017 TYPE("GR32", TYPE_Rv)
1018 TYPE("i64mem", TYPE_Mv)
1019 TYPE("i64i32imm", TYPE_IMM64)
1020 TYPE("i64i8imm", TYPE_IMM64)
1021 TYPE("GR64", TYPE_R64)
1022 TYPE("i8mem", TYPE_M8)
1023 TYPE("i8imm", TYPE_IMM8)
1024 TYPE("GR8", TYPE_R8)
1025 TYPE("VR128", TYPE_XMM128)
1026 TYPE("f128mem", TYPE_M128)
Chris Lattnerb2ef4c12010-09-29 02:57:56 +00001027 TYPE("f256mem", TYPE_M256)
Sean Callanan8ed9f512009-12-19 02:59:52 +00001028 TYPE("FR64", TYPE_XMM64)
1029 TYPE("f64mem", TYPE_M64FP)
Chris Lattnerb2ef4c12010-09-29 02:57:56 +00001030 TYPE("sdmem", TYPE_M64FP)
Sean Callanan8ed9f512009-12-19 02:59:52 +00001031 TYPE("FR32", TYPE_XMM32)
1032 TYPE("f32mem", TYPE_M32FP)
Chris Lattnerb2ef4c12010-09-29 02:57:56 +00001033 TYPE("ssmem", TYPE_M32FP)
Sean Callanan8ed9f512009-12-19 02:59:52 +00001034 TYPE("RST", TYPE_ST)
1035 TYPE("i128mem", TYPE_M128)
Sean Callanana21e2ea2011-03-15 01:23:15 +00001036 TYPE("i256mem", TYPE_M256)
Sean Callanan8ed9f512009-12-19 02:59:52 +00001037 TYPE("i64i32imm_pcrel", TYPE_REL64)
Chris Lattner9fc05222010-07-07 22:27:31 +00001038 TYPE("i16imm_pcrel", TYPE_REL16)
Sean Callanan8ed9f512009-12-19 02:59:52 +00001039 TYPE("i32imm_pcrel", TYPE_REL32)
Sean Callanan5edca812010-04-07 21:42:19 +00001040 TYPE("SSECC", TYPE_IMM3)
Sean Callanan8ed9f512009-12-19 02:59:52 +00001041 TYPE("brtarget", TYPE_RELv)
Owen Andersonc2666002010-12-13 19:31:11 +00001042 TYPE("uncondbrtarget", TYPE_RELv)
Sean Callanan8ed9f512009-12-19 02:59:52 +00001043 TYPE("brtarget8", TYPE_REL8)
1044 TYPE("f80mem", TYPE_M80FP)
Sean Callanan7fb35a22009-12-22 21:12:55 +00001045 TYPE("lea32mem", TYPE_LEA)
1046 TYPE("lea64_32mem", TYPE_LEA)
1047 TYPE("lea64mem", TYPE_LEA)
Sean Callanan8ed9f512009-12-19 02:59:52 +00001048 TYPE("VR64", TYPE_MM64)
1049 TYPE("i64imm", TYPE_IMMv)
1050 TYPE("opaque32mem", TYPE_M1616)
1051 TYPE("opaque48mem", TYPE_M1632)
1052 TYPE("opaque80mem", TYPE_M1664)
1053 TYPE("opaque512mem", TYPE_M512)
1054 TYPE("SEGMENT_REG", TYPE_SEGMENTREG)
1055 TYPE("DEBUG_REG", TYPE_DEBUGREG)
Sean Callanan1a8b7892010-05-06 20:59:00 +00001056 TYPE("CONTROL_REG", TYPE_CONTROLREG)
Sean Callanan8ed9f512009-12-19 02:59:52 +00001057 TYPE("offset8", TYPE_MOFFS8)
1058 TYPE("offset16", TYPE_MOFFS16)
1059 TYPE("offset32", TYPE_MOFFS32)
1060 TYPE("offset64", TYPE_MOFFS64)
Sean Callanana21e2ea2011-03-15 01:23:15 +00001061 TYPE("VR256", TYPE_XMM256)
Sean Callanan8ed9f512009-12-19 02:59:52 +00001062 errs() << "Unhandled type string " << s << "\n";
1063 llvm_unreachable("Unhandled type string");
1064}
1065#undef TYPE
1066
1067#define ENCODING(str, encoding) if (s == str) return encoding;
1068OperandEncoding RecognizableInstr::immediateEncodingFromString
1069 (const std::string &s,
1070 bool hasOpSizePrefix) {
1071 if(!hasOpSizePrefix) {
1072 // For instructions without an OpSize prefix, a declared 16-bit register or
1073 // immediate encoding is special.
1074 ENCODING("i16imm", ENCODING_IW)
1075 }
1076 ENCODING("i32i8imm", ENCODING_IB)
Kevin Enderbyc37d4bb2011-07-27 23:01:50 +00001077 ENCODING("u32u8imm", ENCODING_IB)
Sean Callanan8ed9f512009-12-19 02:59:52 +00001078 ENCODING("SSECC", ENCODING_IB)
1079 ENCODING("i16imm", ENCODING_Iv)
1080 ENCODING("i16i8imm", ENCODING_IB)
1081 ENCODING("i32imm", ENCODING_Iv)
1082 ENCODING("i64i32imm", ENCODING_ID)
1083 ENCODING("i64i8imm", ENCODING_IB)
1084 ENCODING("i8imm", ENCODING_IB)
Sean Callanana21e2ea2011-03-15 01:23:15 +00001085 // This is not a typo. Instructions like BLENDVPD put
1086 // register IDs in 8-bit immediates nowadays.
1087 ENCODING("VR256", ENCODING_IB)
1088 ENCODING("VR128", ENCODING_IB)
Sean Callanan8ed9f512009-12-19 02:59:52 +00001089 errs() << "Unhandled immediate encoding " << s << "\n";
1090 llvm_unreachable("Unhandled immediate encoding");
1091}
1092
1093OperandEncoding RecognizableInstr::rmRegisterEncodingFromString
1094 (const std::string &s,
1095 bool hasOpSizePrefix) {
1096 ENCODING("GR16", ENCODING_RM)
1097 ENCODING("GR32", ENCODING_RM)
1098 ENCODING("GR64", ENCODING_RM)
1099 ENCODING("GR8", ENCODING_RM)
1100 ENCODING("VR128", ENCODING_RM)
1101 ENCODING("FR64", ENCODING_RM)
1102 ENCODING("FR32", ENCODING_RM)
1103 ENCODING("VR64", ENCODING_RM)
Sean Callanana21e2ea2011-03-15 01:23:15 +00001104 ENCODING("VR256", ENCODING_RM)
Sean Callanan8ed9f512009-12-19 02:59:52 +00001105 errs() << "Unhandled R/M register encoding " << s << "\n";
1106 llvm_unreachable("Unhandled R/M register encoding");
1107}
1108
1109OperandEncoding RecognizableInstr::roRegisterEncodingFromString
1110 (const std::string &s,
1111 bool hasOpSizePrefix) {
1112 ENCODING("GR16", ENCODING_REG)
1113 ENCODING("GR32", ENCODING_REG)
1114 ENCODING("GR64", ENCODING_REG)
1115 ENCODING("GR8", ENCODING_REG)
1116 ENCODING("VR128", ENCODING_REG)
1117 ENCODING("FR64", ENCODING_REG)
1118 ENCODING("FR32", ENCODING_REG)
1119 ENCODING("VR64", ENCODING_REG)
1120 ENCODING("SEGMENT_REG", ENCODING_REG)
1121 ENCODING("DEBUG_REG", ENCODING_REG)
Sean Callanan1a8b7892010-05-06 20:59:00 +00001122 ENCODING("CONTROL_REG", ENCODING_REG)
Sean Callanana21e2ea2011-03-15 01:23:15 +00001123 ENCODING("VR256", ENCODING_REG)
Sean Callanan8ed9f512009-12-19 02:59:52 +00001124 errs() << "Unhandled reg/opcode register encoding " << s << "\n";
1125 llvm_unreachable("Unhandled reg/opcode register encoding");
1126}
1127
Sean Callanana21e2ea2011-03-15 01:23:15 +00001128OperandEncoding RecognizableInstr::vvvvRegisterEncodingFromString
1129 (const std::string &s,
1130 bool hasOpSizePrefix) {
1131 ENCODING("FR32", ENCODING_VVVV)
1132 ENCODING("FR64", ENCODING_VVVV)
1133 ENCODING("VR128", ENCODING_VVVV)
1134 ENCODING("VR256", ENCODING_VVVV)
1135 errs() << "Unhandled VEX.vvvv register encoding " << s << "\n";
1136 llvm_unreachable("Unhandled VEX.vvvv register encoding");
1137}
1138
Sean Callanan8ed9f512009-12-19 02:59:52 +00001139OperandEncoding RecognizableInstr::memoryEncodingFromString
1140 (const std::string &s,
1141 bool hasOpSizePrefix) {
1142 ENCODING("i16mem", ENCODING_RM)
1143 ENCODING("i32mem", ENCODING_RM)
1144 ENCODING("i64mem", ENCODING_RM)
1145 ENCODING("i8mem", ENCODING_RM)
Chris Lattnerb2ef4c12010-09-29 02:57:56 +00001146 ENCODING("ssmem", ENCODING_RM)
1147 ENCODING("sdmem", ENCODING_RM)
Sean Callanan8ed9f512009-12-19 02:59:52 +00001148 ENCODING("f128mem", ENCODING_RM)
Chris Lattnerb2ef4c12010-09-29 02:57:56 +00001149 ENCODING("f256mem", ENCODING_RM)
Sean Callanan8ed9f512009-12-19 02:59:52 +00001150 ENCODING("f64mem", ENCODING_RM)
1151 ENCODING("f32mem", ENCODING_RM)
1152 ENCODING("i128mem", ENCODING_RM)
Sean Callanana21e2ea2011-03-15 01:23:15 +00001153 ENCODING("i256mem", ENCODING_RM)
Sean Callanan8ed9f512009-12-19 02:59:52 +00001154 ENCODING("f80mem", ENCODING_RM)
1155 ENCODING("lea32mem", ENCODING_RM)
1156 ENCODING("lea64_32mem", ENCODING_RM)
1157 ENCODING("lea64mem", ENCODING_RM)
1158 ENCODING("opaque32mem", ENCODING_RM)
1159 ENCODING("opaque48mem", ENCODING_RM)
1160 ENCODING("opaque80mem", ENCODING_RM)
1161 ENCODING("opaque512mem", ENCODING_RM)
1162 errs() << "Unhandled memory encoding " << s << "\n";
1163 llvm_unreachable("Unhandled memory encoding");
1164}
1165
1166OperandEncoding RecognizableInstr::relocationEncodingFromString
1167 (const std::string &s,
1168 bool hasOpSizePrefix) {
1169 if(!hasOpSizePrefix) {
1170 // For instructions without an OpSize prefix, a declared 16-bit register or
1171 // immediate encoding is special.
1172 ENCODING("i16imm", ENCODING_IW)
1173 }
1174 ENCODING("i16imm", ENCODING_Iv)
1175 ENCODING("i16i8imm", ENCODING_IB)
1176 ENCODING("i32imm", ENCODING_Iv)
1177 ENCODING("i32i8imm", ENCODING_IB)
1178 ENCODING("i64i32imm", ENCODING_ID)
1179 ENCODING("i64i8imm", ENCODING_IB)
1180 ENCODING("i8imm", ENCODING_IB)
1181 ENCODING("i64i32imm_pcrel", ENCODING_ID)
Chris Lattner9fc05222010-07-07 22:27:31 +00001182 ENCODING("i16imm_pcrel", ENCODING_IW)
Sean Callanan8ed9f512009-12-19 02:59:52 +00001183 ENCODING("i32imm_pcrel", ENCODING_ID)
1184 ENCODING("brtarget", ENCODING_Iv)
1185 ENCODING("brtarget8", ENCODING_IB)
1186 ENCODING("i64imm", ENCODING_IO)
1187 ENCODING("offset8", ENCODING_Ia)
1188 ENCODING("offset16", ENCODING_Ia)
1189 ENCODING("offset32", ENCODING_Ia)
1190 ENCODING("offset64", ENCODING_Ia)
1191 errs() << "Unhandled relocation encoding " << s << "\n";
1192 llvm_unreachable("Unhandled relocation encoding");
1193}
1194
1195OperandEncoding RecognizableInstr::opcodeModifierEncodingFromString
1196 (const std::string &s,
1197 bool hasOpSizePrefix) {
1198 ENCODING("RST", ENCODING_I)
1199 ENCODING("GR32", ENCODING_Rv)
1200 ENCODING("GR64", ENCODING_RO)
1201 ENCODING("GR16", ENCODING_Rv)
1202 ENCODING("GR8", ENCODING_RB)
1203 errs() << "Unhandled opcode modifier encoding " << s << "\n";
1204 llvm_unreachable("Unhandled opcode modifier encoding");
1205}
Daniel Dunbar9e6d1d12009-12-19 04:16:48 +00001206#undef ENCODING