blob: 7f502cc561004969163122dfb312a01df8ea4655 [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) \
Sean Callanancebe9552010-02-13 02:06:11 +000037 MAP(F9, 42)
Sean Callanan9492be82010-02-12 23:39:46 +000038
Sean Callanan8ed9f512009-12-19 02:59:52 +000039// A clone of X86 since we can't depend on something that is generated.
40namespace X86Local {
41 enum {
42 Pseudo = 0,
43 RawFrm = 1,
44 AddRegFrm = 2,
45 MRMDestReg = 3,
46 MRMDestMem = 4,
47 MRMSrcReg = 5,
48 MRMSrcMem = 6,
49 MRM0r = 16, MRM1r = 17, MRM2r = 18, MRM3r = 19,
50 MRM4r = 20, MRM5r = 21, MRM6r = 22, MRM7r = 23,
51 MRM0m = 24, MRM1m = 25, MRM2m = 26, MRM3m = 27,
52 MRM4m = 28, MRM5m = 29, MRM6m = 30, MRM7m = 31,
Sean Callanan9492be82010-02-12 23:39:46 +000053 MRMInitReg = 32,
54
55#define MAP(from, to) MRM_##from = to,
56 MRM_MAPPING
57#undef MAP
58 lastMRM
Sean Callanan8ed9f512009-12-19 02:59:52 +000059 };
60
61 enum {
62 TB = 1,
63 REP = 2,
64 D8 = 3, D9 = 4, DA = 5, DB = 6,
65 DC = 7, DD = 8, DE = 9, DF = 10,
66 XD = 11, XS = 12,
Chris Lattner0d8db8e2010-02-12 02:06:33 +000067 T8 = 13, P_TA = 14,
68 P_0F_AE = 16, P_0F_01 = 17
Sean Callanan8ed9f512009-12-19 02:59:52 +000069 };
70}
Sean Callanan9492be82010-02-12 23:39:46 +000071
72// If rows are added to the opcode extension tables, then corresponding entries
73// must be added here.
74//
75// If the row corresponds to a single byte (i.e., 8f), then add an entry for
76// that byte to ONE_BYTE_EXTENSION_TABLES.
77//
78// If the row corresponds to two bytes where the first is 0f, add an entry for
79// the second byte to TWO_BYTE_EXTENSION_TABLES.
80//
81// If the row corresponds to some other set of bytes, you will need to modify
82// the code in RecognizableInstr::emitDecodePath() as well, and add new prefixes
83// to the X86 TD files, except in two cases: if the first two bytes of such a
84// new combination are 0f 38 or 0f 3a, you just have to add maps called
85// THREE_BYTE_38_EXTENSION_TABLES and THREE_BYTE_3A_EXTENSION_TABLES and add a
86// switch(Opcode) just below the case X86Local::T8: or case X86Local::TA: line
87// in RecognizableInstr::emitDecodePath().
88
Sean Callanan8ed9f512009-12-19 02:59:52 +000089#define ONE_BYTE_EXTENSION_TABLES \
90 EXTENSION_TABLE(80) \
91 EXTENSION_TABLE(81) \
92 EXTENSION_TABLE(82) \
93 EXTENSION_TABLE(83) \
94 EXTENSION_TABLE(8f) \
95 EXTENSION_TABLE(c0) \
96 EXTENSION_TABLE(c1) \
97 EXTENSION_TABLE(c6) \
98 EXTENSION_TABLE(c7) \
99 EXTENSION_TABLE(d0) \
100 EXTENSION_TABLE(d1) \
101 EXTENSION_TABLE(d2) \
102 EXTENSION_TABLE(d3) \
103 EXTENSION_TABLE(f6) \
104 EXTENSION_TABLE(f7) \
105 EXTENSION_TABLE(fe) \
106 EXTENSION_TABLE(ff)
107
108#define TWO_BYTE_EXTENSION_TABLES \
109 EXTENSION_TABLE(00) \
110 EXTENSION_TABLE(01) \
111 EXTENSION_TABLE(18) \
112 EXTENSION_TABLE(71) \
113 EXTENSION_TABLE(72) \
114 EXTENSION_TABLE(73) \
115 EXTENSION_TABLE(ae) \
116 EXTENSION_TABLE(b9) \
117 EXTENSION_TABLE(ba) \
118 EXTENSION_TABLE(c7)
Sean Callanan8ed9f512009-12-19 02:59:52 +0000119
120using namespace X86Disassembler;
121
122/// needsModRMForDecode - Indicates whether a particular instruction requires a
123/// ModR/M byte for the instruction to be properly decoded. For example, a
124/// MRMDestReg instruction needs the Mod field in the ModR/M byte to be set to
125/// 0b11.
126///
127/// @param form - The form of the instruction.
128/// @return - true if the form implies that a ModR/M byte is required, false
129/// otherwise.
130static bool needsModRMForDecode(uint8_t form) {
131 if (form == X86Local::MRMDestReg ||
132 form == X86Local::MRMDestMem ||
133 form == X86Local::MRMSrcReg ||
134 form == X86Local::MRMSrcMem ||
135 (form >= X86Local::MRM0r && form <= X86Local::MRM7r) ||
136 (form >= X86Local::MRM0m && form <= X86Local::MRM7m))
137 return true;
138 else
139 return false;
140}
141
142/// isRegFormat - Indicates whether a particular form requires the Mod field of
143/// the ModR/M byte to be 0b11.
144///
145/// @param form - The form of the instruction.
146/// @return - true if the form implies that Mod must be 0b11, false
147/// otherwise.
148static bool isRegFormat(uint8_t form) {
149 if (form == X86Local::MRMDestReg ||
150 form == X86Local::MRMSrcReg ||
151 (form >= X86Local::MRM0r && form <= X86Local::MRM7r))
152 return true;
153 else
154 return false;
155}
156
157/// byteFromBitsInit - Extracts a value at most 8 bits in width from a BitsInit.
158/// Useful for switch statements and the like.
159///
160/// @param init - A reference to the BitsInit to be decoded.
161/// @return - The field, with the first bit in the BitsInit as the lowest
162/// order bit.
163static uint8_t byteFromBitsInit(BitsInit &init) {
164 int width = init.getNumBits();
165
166 assert(width <= 8 && "Field is too large for uint8_t!");
167
168 int index;
169 uint8_t mask = 0x01;
170
171 uint8_t ret = 0;
172
173 for (index = 0; index < width; index++) {
174 if (static_cast<BitInit*>(init.getBit(index))->getValue())
175 ret |= mask;
176
177 mask <<= 1;
178 }
179
180 return ret;
181}
182
183/// byteFromRec - Extract a value at most 8 bits in with from a Record given the
184/// name of the field.
185///
186/// @param rec - The record from which to extract the value.
187/// @param name - The name of the field in the record.
188/// @return - The field, as translated by byteFromBitsInit().
189static uint8_t byteFromRec(const Record* rec, const std::string &name) {
190 BitsInit* bits = rec->getValueAsBitsInit(name);
191 return byteFromBitsInit(*bits);
192}
193
194RecognizableInstr::RecognizableInstr(DisassemblerTables &tables,
195 const CodeGenInstruction &insn,
196 InstrUID uid) {
197 UID = uid;
198
199 Rec = insn.TheDef;
200 Name = Rec->getName();
201 Spec = &tables.specForUID(UID);
202
203 if (!Rec->isSubClassOf("X86Inst")) {
204 ShouldBeEmitted = false;
205 return;
206 }
207
208 Prefix = byteFromRec(Rec, "Prefix");
209 Opcode = byteFromRec(Rec, "Opcode");
210 Form = byteFromRec(Rec, "FormBits");
211 SegOvr = byteFromRec(Rec, "SegOvrBits");
212
213 HasOpSizePrefix = Rec->getValueAsBit("hasOpSizePrefix");
214 HasREX_WPrefix = Rec->getValueAsBit("hasREX_WPrefix");
Bruno Cardoso Lopes99405df2010-06-08 22:51:23 +0000215 HasVEX_4VPrefix = Rec->getValueAsBit("hasVEX_4VPrefix");
Sean Callanan8ed9f512009-12-19 02:59:52 +0000216 HasLockPrefix = Rec->getValueAsBit("hasLockPrefix");
217 IsCodeGenOnly = Rec->getValueAsBit("isCodeGenOnly");
218
219 Name = Rec->getName();
220 AsmString = Rec->getValueAsString("AsmString");
221
222 Operands = &insn.OperandList;
223
224 IsSSE = HasOpSizePrefix && (Name.find("16") == Name.npos);
225 HasFROperands = false;
226
227 ShouldBeEmitted = true;
228}
229
230void RecognizableInstr::processInstr(DisassemblerTables &tables,
231 const CodeGenInstruction &insn,
232 InstrUID uid)
233{
Daniel Dunbar40728862010-05-20 20:20:32 +0000234 // Ignore "asm parser only" instructions.
235 if (insn.TheDef->getValueAsBit("isAsmParserOnly"))
236 return;
237
Sean Callanan8ed9f512009-12-19 02:59:52 +0000238 RecognizableInstr recogInstr(tables, insn, uid);
239
240 recogInstr.emitInstructionSpecifier(tables);
241
242 if (recogInstr.shouldBeEmitted())
243 recogInstr.emitDecodePath(tables);
244}
245
246InstructionContext RecognizableInstr::insnContext() const {
247 InstructionContext insnContext;
248
249 if (Name.find("64") != Name.npos || HasREX_WPrefix) {
250 if (HasREX_WPrefix && HasOpSizePrefix)
251 insnContext = IC_64BIT_REXW_OPSIZE;
252 else if (HasOpSizePrefix)
253 insnContext = IC_64BIT_OPSIZE;
254 else if (HasREX_WPrefix && Prefix == X86Local::XS)
255 insnContext = IC_64BIT_REXW_XS;
256 else if (HasREX_WPrefix && Prefix == X86Local::XD)
257 insnContext = IC_64BIT_REXW_XD;
258 else if (Prefix == X86Local::XD)
259 insnContext = IC_64BIT_XD;
260 else if (Prefix == X86Local::XS)
261 insnContext = IC_64BIT_XS;
262 else if (HasREX_WPrefix)
263 insnContext = IC_64BIT_REXW;
264 else
265 insnContext = IC_64BIT;
266 } else {
267 if (HasOpSizePrefix)
268 insnContext = IC_OPSIZE;
269 else if (Prefix == X86Local::XD)
270 insnContext = IC_XD;
271 else if (Prefix == X86Local::XS)
272 insnContext = IC_XS;
273 else
274 insnContext = IC;
275 }
276
277 return insnContext;
278}
279
280RecognizableInstr::filter_ret RecognizableInstr::filter() const {
281 // Filter out intrinsics
282
283 if (!Rec->isSubClassOf("X86Inst"))
284 return FILTER_STRONG;
285
286 if (Form == X86Local::Pseudo ||
287 IsCodeGenOnly)
288 return FILTER_STRONG;
289
Sean Callanan80443f92010-02-24 02:56:25 +0000290 if (Form == X86Local::MRMInitReg)
291 return FILTER_STRONG;
292
293
Sean Callanan8ed9f512009-12-19 02:59:52 +0000294 // Filter out instructions with a LOCK prefix;
295 // prefer forms that do not have the prefix
296 if (HasLockPrefix)
297 return FILTER_WEAK;
298
299 // Filter out artificial instructions
300
301 if (Name.find("TAILJMP") != Name.npos ||
302 Name.find("_Int") != Name.npos ||
303 Name.find("_int") != Name.npos ||
304 Name.find("Int_") != Name.npos ||
305 Name.find("_NOREX") != Name.npos ||
Evan Cheng5e817162010-03-14 05:15:39 +0000306 Name.find("_TC") != Name.npos ||
Sean Callanan8ed9f512009-12-19 02:59:52 +0000307 Name.find("EH_RETURN") != Name.npos ||
308 Name.find("V_SET") != Name.npos ||
309 Name.find("LOCK_") != Name.npos ||
310 Name.find("WIN") != Name.npos)
311 return FILTER_STRONG;
312
313 // Special cases.
Dale Johannesen86097c32010-09-07 18:10:56 +0000314
Sean Callanan8ed9f512009-12-19 02:59:52 +0000315 if (Name.find("PCMPISTRI") != Name.npos && Name != "PCMPISTRI")
316 return FILTER_WEAK;
317 if (Name.find("PCMPESTRI") != Name.npos && Name != "PCMPESTRI")
318 return FILTER_WEAK;
319
320 if (Name.find("MOV") != Name.npos && Name.find("r0") != Name.npos)
321 return FILTER_WEAK;
322 if (Name.find("MOVZ") != Name.npos && Name.find("MOVZX") == Name.npos)
323 return FILTER_WEAK;
324 if (Name.find("Fs") != Name.npos)
325 return FILTER_WEAK;
326 if (Name == "MOVLPDrr" ||
327 Name == "MOVLPSrr" ||
328 Name == "PUSHFQ" ||
329 Name == "BSF16rr" ||
330 Name == "BSF16rm" ||
331 Name == "BSR16rr" ||
332 Name == "BSR16rm" ||
333 Name == "MOVSX16rm8" ||
334 Name == "MOVSX16rr8" ||
335 Name == "MOVZX16rm8" ||
336 Name == "MOVZX16rr8" ||
337 Name == "PUSH32i16" ||
338 Name == "PUSH64i16" ||
339 Name == "MOVPQI2QImr" ||
340 Name == "MOVSDmr" ||
341 Name == "MOVSDrm" ||
342 Name == "MOVSSmr" ||
343 Name == "MOVSSrm" ||
344 Name == "MMX_MOVD64rrv164" ||
345 Name == "CRC32m16" ||
346 Name == "MOV64ri64i32" ||
347 Name == "CRC32r16")
348 return FILTER_WEAK;
349
350 // Filter out instructions with segment override prefixes.
351 // They're too messy to handle now and we'll special case them if needed.
352
353 if (SegOvr)
354 return FILTER_STRONG;
355
356 // Filter out instructions that can't be printed.
357
358 if (AsmString.size() == 0)
359 return FILTER_STRONG;
360
361 // Filter out instructions with subreg operands.
362
363 if (AsmString.find("subreg") != AsmString.npos)
364 return FILTER_STRONG;
365
Sean Callanan8ed9f512009-12-19 02:59:52 +0000366 if (HasFROperands && Name.find("MOV") != Name.npos &&
367 ((Name.find("2") != Name.npos && Name.find("32") == Name.npos) ||
368 (Name.find("to") != Name.npos)))
369 return FILTER_WEAK;
370
Dale Johannesen86097c32010-09-07 18:10:56 +0000371 // Filter out the intrinsic form of instructions that also have an llvm
372 // operator form. FIXME this is temporary.
373 if (Name.find("irm") != Name.npos ||
374 Name.find("irr") != Name.npos)
375 return FILTER_WEAK;
376
Sean Callanan8ed9f512009-12-19 02:59:52 +0000377 return FILTER_NORMAL;
378}
379
380void RecognizableInstr::handleOperand(
381 bool optional,
382 unsigned &operandIndex,
383 unsigned &physicalOperandIndex,
384 unsigned &numPhysicalOperands,
385 unsigned *operandMapping,
386 OperandEncoding (*encodingFromString)(const std::string&, bool hasOpSizePrefix)) {
387 if (optional) {
388 if (physicalOperandIndex >= numPhysicalOperands)
389 return;
390 } else {
391 assert(physicalOperandIndex < numPhysicalOperands);
392 }
393
394 while (operandMapping[operandIndex] != operandIndex) {
395 Spec->operands[operandIndex].encoding = ENCODING_DUP;
396 Spec->operands[operandIndex].type =
397 (OperandType)(TYPE_DUP0 + operandMapping[operandIndex]);
398 ++operandIndex;
399 }
400
401 const std::string &typeName = (*Operands)[operandIndex].Rec->getName();
402
403 Spec->operands[operandIndex].encoding = encodingFromString(typeName,
404 HasOpSizePrefix);
405 Spec->operands[operandIndex].type = typeFromString(typeName,
406 IsSSE,
407 HasREX_WPrefix,
408 HasOpSizePrefix);
409
410 ++operandIndex;
411 ++physicalOperandIndex;
412}
413
414void RecognizableInstr::emitInstructionSpecifier(DisassemblerTables &tables) {
415 Spec->name = Name;
416
417 if (!Rec->isSubClassOf("X86Inst"))
418 return;
419
420 switch (filter()) {
421 case FILTER_WEAK:
422 Spec->filtered = true;
423 break;
424 case FILTER_STRONG:
425 ShouldBeEmitted = false;
426 return;
427 case FILTER_NORMAL:
428 break;
429 }
430
431 Spec->insnContext = insnContext();
432
433 const std::vector<CodeGenInstruction::OperandInfo> &OperandList = *Operands;
434
435 unsigned operandIndex;
436 unsigned numOperands = OperandList.size();
437 unsigned numPhysicalOperands = 0;
438
439 // operandMapping maps from operands in OperandList to their originals.
440 // If operandMapping[i] != i, then the entry is a duplicate.
441 unsigned operandMapping[X86_MAX_OPERANDS];
442
443 bool hasFROperands = false;
444
445 assert(numOperands < X86_MAX_OPERANDS && "X86_MAX_OPERANDS is not large enough");
446
447 for (operandIndex = 0; operandIndex < numOperands; ++operandIndex) {
448 if (OperandList[operandIndex].Constraints.size()) {
Chris Lattnera7d479c2010-02-10 01:45:28 +0000449 const CodeGenInstruction::ConstraintInfo &Constraint =
450 OperandList[operandIndex].Constraints[0];
451 if (Constraint.isTied()) {
452 operandMapping[operandIndex] = Constraint.getTiedOperand();
Sean Callanan8ed9f512009-12-19 02:59:52 +0000453 } else {
454 ++numPhysicalOperands;
455 operandMapping[operandIndex] = operandIndex;
456 }
457 } else {
458 ++numPhysicalOperands;
459 operandMapping[operandIndex] = operandIndex;
460 }
461
462 const std::string &recName = OperandList[operandIndex].Rec->getName();
463
464 if (recName.find("FR") != recName.npos)
465 hasFROperands = true;
466 }
467
468 if (hasFROperands && Name.find("MOV") != Name.npos &&
469 ((Name.find("2") != Name.npos && Name.find("32") == Name.npos) ||
470 (Name.find("to") != Name.npos)))
471 ShouldBeEmitted = false;
472
473 if (!ShouldBeEmitted)
474 return;
475
476#define HANDLE_OPERAND(class) \
477 handleOperand(false, \
478 operandIndex, \
479 physicalOperandIndex, \
480 numPhysicalOperands, \
481 operandMapping, \
482 class##EncodingFromString);
483
484#define HANDLE_OPTIONAL(class) \
485 handleOperand(true, \
486 operandIndex, \
487 physicalOperandIndex, \
488 numPhysicalOperands, \
489 operandMapping, \
490 class##EncodingFromString);
491
492 // operandIndex should always be < numOperands
493 operandIndex = 0;
494 // physicalOperandIndex should always be < numPhysicalOperands
495 unsigned physicalOperandIndex = 0;
496
497 switch (Form) {
498 case X86Local::RawFrm:
499 // Operand 1 (optional) is an address or immediate.
500 // Operand 2 (optional) is an immediate.
501 assert(numPhysicalOperands <= 2 &&
502 "Unexpected number of operands for RawFrm");
503 HANDLE_OPTIONAL(relocation)
504 HANDLE_OPTIONAL(immediate)
505 break;
506 case X86Local::AddRegFrm:
507 // Operand 1 is added to the opcode.
508 // Operand 2 (optional) is an address.
509 assert(numPhysicalOperands >= 1 && numPhysicalOperands <= 2 &&
510 "Unexpected number of operands for AddRegFrm");
511 HANDLE_OPERAND(opcodeModifier)
512 HANDLE_OPTIONAL(relocation)
513 break;
514 case X86Local::MRMDestReg:
515 // Operand 1 is a register operand in the R/M field.
516 // Operand 2 is a register operand in the Reg/Opcode field.
517 // Operand 3 (optional) is an immediate.
518 assert(numPhysicalOperands >= 2 && numPhysicalOperands <= 3 &&
519 "Unexpected number of operands for MRMDestRegFrm");
520 HANDLE_OPERAND(rmRegister)
521 HANDLE_OPERAND(roRegister)
522 HANDLE_OPTIONAL(immediate)
523 break;
524 case X86Local::MRMDestMem:
525 // Operand 1 is a memory operand (possibly SIB-extended)
526 // Operand 2 is a register operand in the Reg/Opcode field.
527 // Operand 3 (optional) is an immediate.
528 assert(numPhysicalOperands >= 2 && numPhysicalOperands <= 3 &&
529 "Unexpected number of operands for MRMDestMemFrm");
530 HANDLE_OPERAND(memory)
531 HANDLE_OPERAND(roRegister)
532 HANDLE_OPTIONAL(immediate)
533 break;
534 case X86Local::MRMSrcReg:
535 // Operand 1 is a register operand in the Reg/Opcode field.
536 // Operand 2 is a register operand in the R/M field.
537 // Operand 3 (optional) is an immediate.
538 assert(numPhysicalOperands >= 2 && numPhysicalOperands <= 3 &&
539 "Unexpected number of operands for MRMSrcRegFrm");
540 HANDLE_OPERAND(roRegister)
541 HANDLE_OPERAND(rmRegister)
Bruno Cardoso Lopes99405df2010-06-08 22:51:23 +0000542
543 if (HasVEX_4VPrefix)
Bruno Cardoso Lopesc902a592010-06-11 23:50:47 +0000544 // FIXME: In AVX, the register below becomes the one encoded
545 // in ModRMVEX and the one above the one in the VEX.VVVV field
Bruno Cardoso Lopes99405df2010-06-08 22:51:23 +0000546 HANDLE_OPTIONAL(rmRegister)
547 else
548 HANDLE_OPTIONAL(immediate)
Sean Callanan8ed9f512009-12-19 02:59:52 +0000549 break;
550 case X86Local::MRMSrcMem:
551 // Operand 1 is a register operand in the Reg/Opcode field.
552 // Operand 2 is a memory operand (possibly SIB-extended)
553 // Operand 3 (optional) is an immediate.
554 assert(numPhysicalOperands >= 2 && numPhysicalOperands <= 3 &&
555 "Unexpected number of operands for MRMSrcMemFrm");
556 HANDLE_OPERAND(roRegister)
Bruno Cardoso Lopesc902a592010-06-11 23:50:47 +0000557
558 if (HasVEX_4VPrefix)
559 // FIXME: In AVX, the register below becomes the one encoded
560 // in ModRMVEX and the one above the one in the VEX.VVVV field
561 HANDLE_OPTIONAL(rmRegister)
562
Sean Callanan8ed9f512009-12-19 02:59:52 +0000563 HANDLE_OPERAND(memory)
564 HANDLE_OPTIONAL(immediate)
565 break;
566 case X86Local::MRM0r:
567 case X86Local::MRM1r:
568 case X86Local::MRM2r:
569 case X86Local::MRM3r:
570 case X86Local::MRM4r:
571 case X86Local::MRM5r:
572 case X86Local::MRM6r:
573 case X86Local::MRM7r:
574 // Operand 1 is a register operand in the R/M field.
575 // Operand 2 (optional) is an immediate or relocation.
576 assert(numPhysicalOperands <= 2 &&
577 "Unexpected number of operands for MRMnRFrm");
578 HANDLE_OPTIONAL(rmRegister)
579 HANDLE_OPTIONAL(relocation)
580 break;
581 case X86Local::MRM0m:
582 case X86Local::MRM1m:
583 case X86Local::MRM2m:
584 case X86Local::MRM3m:
585 case X86Local::MRM4m:
586 case X86Local::MRM5m:
587 case X86Local::MRM6m:
588 case X86Local::MRM7m:
589 // Operand 1 is a memory operand (possibly SIB-extended)
590 // Operand 2 (optional) is an immediate or relocation.
591 assert(numPhysicalOperands >= 1 && numPhysicalOperands <= 2 &&
592 "Unexpected number of operands for MRMnMFrm");
593 HANDLE_OPERAND(memory)
594 HANDLE_OPTIONAL(relocation)
595 break;
596 case X86Local::MRMInitReg:
597 // Ignored.
598 break;
599 }
600
601 #undef HANDLE_OPERAND
602 #undef HANDLE_OPTIONAL
603}
604
605void RecognizableInstr::emitDecodePath(DisassemblerTables &tables) const {
606 // Special cases where the LLVM tables are not complete
607
Sean Callanan9492be82010-02-12 23:39:46 +0000608#define MAP(from, to) \
609 case X86Local::MRM_##from: \
610 filter = new ExactFilter(0x##from); \
611 break;
Sean Callanan8ed9f512009-12-19 02:59:52 +0000612
613 OpcodeType opcodeType = (OpcodeType)-1;
614
615 ModRMFilter* filter = NULL;
616 uint8_t opcodeToSet = 0;
617
618 switch (Prefix) {
619 // Extended two-byte opcodes can start with f2 0f, f3 0f, or 0f
620 case X86Local::XD:
621 case X86Local::XS:
622 case X86Local::TB:
623 opcodeType = TWOBYTE;
624
625 switch (Opcode) {
Sean Callanan95a5a7d2010-02-13 01:48:34 +0000626 default:
627 if (needsModRMForDecode(Form))
628 filter = new ModFilter(isRegFormat(Form));
629 else
630 filter = new DumbFilter();
631 break;
Sean Callanan8ed9f512009-12-19 02:59:52 +0000632#define EXTENSION_TABLE(n) case 0x##n:
633 TWO_BYTE_EXTENSION_TABLES
634#undef EXTENSION_TABLE
635 switch (Form) {
636 default:
637 llvm_unreachable("Unhandled two-byte extended opcode");
638 case X86Local::MRM0r:
639 case X86Local::MRM1r:
640 case X86Local::MRM2r:
641 case X86Local::MRM3r:
642 case X86Local::MRM4r:
643 case X86Local::MRM5r:
644 case X86Local::MRM6r:
645 case X86Local::MRM7r:
646 filter = new ExtendedFilter(true, Form - X86Local::MRM0r);
647 break;
648 case X86Local::MRM0m:
649 case X86Local::MRM1m:
650 case X86Local::MRM2m:
651 case X86Local::MRM3m:
652 case X86Local::MRM4m:
653 case X86Local::MRM5m:
654 case X86Local::MRM6m:
655 case X86Local::MRM7m:
656 filter = new ExtendedFilter(false, Form - X86Local::MRM0m);
657 break;
Sean Callanan9492be82010-02-12 23:39:46 +0000658 MRM_MAPPING
Sean Callanan8ed9f512009-12-19 02:59:52 +0000659 } // switch (Form)
660 break;
Sean Callanan95a5a7d2010-02-13 01:48:34 +0000661 } // switch (Opcode)
Sean Callanan8ed9f512009-12-19 02:59:52 +0000662 opcodeToSet = Opcode;
663 break;
664 case X86Local::T8:
665 opcodeType = THREEBYTE_38;
666 if (needsModRMForDecode(Form))
667 filter = new ModFilter(isRegFormat(Form));
668 else
669 filter = new DumbFilter();
670 opcodeToSet = Opcode;
671 break;
Chris Lattner0d8db8e2010-02-12 02:06:33 +0000672 case X86Local::P_TA:
Sean Callanan8ed9f512009-12-19 02:59:52 +0000673 opcodeType = THREEBYTE_3A;
674 if (needsModRMForDecode(Form))
675 filter = new ModFilter(isRegFormat(Form));
676 else
677 filter = new DumbFilter();
678 opcodeToSet = Opcode;
679 break;
680 case X86Local::D8:
681 case X86Local::D9:
682 case X86Local::DA:
683 case X86Local::DB:
684 case X86Local::DC:
685 case X86Local::DD:
686 case X86Local::DE:
687 case X86Local::DF:
688 assert(Opcode >= 0xc0 && "Unexpected opcode for an escape opcode");
689 opcodeType = ONEBYTE;
690 if (Form == X86Local::AddRegFrm) {
691 Spec->modifierType = MODIFIER_MODRM;
692 Spec->modifierBase = Opcode;
693 filter = new AddRegEscapeFilter(Opcode);
694 } else {
695 filter = new EscapeFilter(true, Opcode);
696 }
697 opcodeToSet = 0xd8 + (Prefix - X86Local::D8);
698 break;
699 default:
700 opcodeType = ONEBYTE;
701 switch (Opcode) {
702#define EXTENSION_TABLE(n) case 0x##n:
703 ONE_BYTE_EXTENSION_TABLES
704#undef EXTENSION_TABLE
705 switch (Form) {
706 default:
707 llvm_unreachable("Fell through the cracks of a single-byte "
708 "extended opcode");
709 case X86Local::MRM0r:
710 case X86Local::MRM1r:
711 case X86Local::MRM2r:
712 case X86Local::MRM3r:
713 case X86Local::MRM4r:
714 case X86Local::MRM5r:
715 case X86Local::MRM6r:
716 case X86Local::MRM7r:
717 filter = new ExtendedFilter(true, Form - X86Local::MRM0r);
718 break;
719 case X86Local::MRM0m:
720 case X86Local::MRM1m:
721 case X86Local::MRM2m:
722 case X86Local::MRM3m:
723 case X86Local::MRM4m:
724 case X86Local::MRM5m:
725 case X86Local::MRM6m:
726 case X86Local::MRM7m:
727 filter = new ExtendedFilter(false, Form - X86Local::MRM0m);
728 break;
Sean Callanan9492be82010-02-12 23:39:46 +0000729 MRM_MAPPING
Sean Callanan8ed9f512009-12-19 02:59:52 +0000730 } // switch (Form)
731 break;
732 case 0xd8:
733 case 0xd9:
734 case 0xda:
735 case 0xdb:
736 case 0xdc:
737 case 0xdd:
738 case 0xde:
739 case 0xdf:
740 filter = new EscapeFilter(false, Form - X86Local::MRM0m);
741 break;
742 default:
743 if (needsModRMForDecode(Form))
744 filter = new ModFilter(isRegFormat(Form));
745 else
746 filter = new DumbFilter();
747 break;
748 } // switch (Opcode)
749 opcodeToSet = Opcode;
750 } // switch (Prefix)
751
752 assert(opcodeType != (OpcodeType)-1 &&
753 "Opcode type not set");
754 assert(filter && "Filter not set");
755
756 if (Form == X86Local::AddRegFrm) {
757 if(Spec->modifierType != MODIFIER_MODRM) {
758 assert(opcodeToSet < 0xf9 &&
759 "Not enough room for all ADDREG_FRM operands");
760
761 uint8_t currentOpcode;
762
763 for (currentOpcode = opcodeToSet;
764 currentOpcode < opcodeToSet + 8;
765 ++currentOpcode)
766 tables.setTableFields(opcodeType,
767 insnContext(),
768 currentOpcode,
769 *filter,
770 UID);
771
772 Spec->modifierType = MODIFIER_OPCODE;
773 Spec->modifierBase = opcodeToSet;
774 } else {
775 // modifierBase was set where MODIFIER_MODRM was set
776 tables.setTableFields(opcodeType,
777 insnContext(),
778 opcodeToSet,
779 *filter,
780 UID);
781 }
782 } else {
783 tables.setTableFields(opcodeType,
784 insnContext(),
785 opcodeToSet,
786 *filter,
787 UID);
788
789 Spec->modifierType = MODIFIER_NONE;
790 Spec->modifierBase = opcodeToSet;
791 }
792
793 delete filter;
Sean Callanan9492be82010-02-12 23:39:46 +0000794
795#undef MAP
Sean Callanan8ed9f512009-12-19 02:59:52 +0000796}
797
798#define TYPE(str, type) if (s == str) return type;
799OperandType RecognizableInstr::typeFromString(const std::string &s,
800 bool isSSE,
801 bool hasREX_WPrefix,
802 bool hasOpSizePrefix) {
803 if (isSSE) {
804 // For SSE instructions, we ignore the OpSize prefix and force operand
805 // sizes.
806 TYPE("GR16", TYPE_R16)
807 TYPE("GR32", TYPE_R32)
808 TYPE("GR64", TYPE_R64)
809 }
810 if(hasREX_WPrefix) {
811 // For instructions with a REX_W prefix, a declared 32-bit register encoding
812 // is special.
813 TYPE("GR32", TYPE_R32)
814 }
815 if(!hasOpSizePrefix) {
816 // For instructions without an OpSize prefix, a declared 16-bit register or
817 // immediate encoding is special.
818 TYPE("GR16", TYPE_R16)
819 TYPE("i16imm", TYPE_IMM16)
820 }
821 TYPE("i16mem", TYPE_Mv)
822 TYPE("i16imm", TYPE_IMMv)
823 TYPE("i16i8imm", TYPE_IMMv)
824 TYPE("GR16", TYPE_Rv)
825 TYPE("i32mem", TYPE_Mv)
826 TYPE("i32imm", TYPE_IMMv)
827 TYPE("i32i8imm", TYPE_IMM32)
828 TYPE("GR32", TYPE_Rv)
829 TYPE("i64mem", TYPE_Mv)
830 TYPE("i64i32imm", TYPE_IMM64)
831 TYPE("i64i8imm", TYPE_IMM64)
832 TYPE("GR64", TYPE_R64)
833 TYPE("i8mem", TYPE_M8)
834 TYPE("i8imm", TYPE_IMM8)
835 TYPE("GR8", TYPE_R8)
836 TYPE("VR128", TYPE_XMM128)
837 TYPE("f128mem", TYPE_M128)
Chris Lattnerb2ef4c12010-09-29 02:57:56 +0000838 TYPE("f256mem", TYPE_M256)
Sean Callanan8ed9f512009-12-19 02:59:52 +0000839 TYPE("FR64", TYPE_XMM64)
840 TYPE("f64mem", TYPE_M64FP)
Chris Lattnerb2ef4c12010-09-29 02:57:56 +0000841 TYPE("sdmem", TYPE_M64FP)
Sean Callanan8ed9f512009-12-19 02:59:52 +0000842 TYPE("FR32", TYPE_XMM32)
843 TYPE("f32mem", TYPE_M32FP)
Chris Lattnerb2ef4c12010-09-29 02:57:56 +0000844 TYPE("ssmem", TYPE_M32FP)
Sean Callanan8ed9f512009-12-19 02:59:52 +0000845 TYPE("RST", TYPE_ST)
846 TYPE("i128mem", TYPE_M128)
847 TYPE("i64i32imm_pcrel", TYPE_REL64)
Chris Lattner9fc05222010-07-07 22:27:31 +0000848 TYPE("i16imm_pcrel", TYPE_REL16)
Sean Callanan8ed9f512009-12-19 02:59:52 +0000849 TYPE("i32imm_pcrel", TYPE_REL32)
Sean Callanan5edca812010-04-07 21:42:19 +0000850 TYPE("SSECC", TYPE_IMM3)
Sean Callanan8ed9f512009-12-19 02:59:52 +0000851 TYPE("brtarget", TYPE_RELv)
852 TYPE("brtarget8", TYPE_REL8)
853 TYPE("f80mem", TYPE_M80FP)
Sean Callanan7fb35a22009-12-22 21:12:55 +0000854 TYPE("lea32mem", TYPE_LEA)
855 TYPE("lea64_32mem", TYPE_LEA)
856 TYPE("lea64mem", TYPE_LEA)
Sean Callanan8ed9f512009-12-19 02:59:52 +0000857 TYPE("VR64", TYPE_MM64)
858 TYPE("i64imm", TYPE_IMMv)
859 TYPE("opaque32mem", TYPE_M1616)
860 TYPE("opaque48mem", TYPE_M1632)
861 TYPE("opaque80mem", TYPE_M1664)
862 TYPE("opaque512mem", TYPE_M512)
863 TYPE("SEGMENT_REG", TYPE_SEGMENTREG)
864 TYPE("DEBUG_REG", TYPE_DEBUGREG)
Sean Callanan1a8b7892010-05-06 20:59:00 +0000865 TYPE("CONTROL_REG", TYPE_CONTROLREG)
Sean Callanan8ed9f512009-12-19 02:59:52 +0000866 TYPE("offset8", TYPE_MOFFS8)
867 TYPE("offset16", TYPE_MOFFS16)
868 TYPE("offset32", TYPE_MOFFS32)
869 TYPE("offset64", TYPE_MOFFS64)
870 errs() << "Unhandled type string " << s << "\n";
871 llvm_unreachable("Unhandled type string");
872}
873#undef TYPE
874
875#define ENCODING(str, encoding) if (s == str) return encoding;
876OperandEncoding RecognizableInstr::immediateEncodingFromString
877 (const std::string &s,
878 bool hasOpSizePrefix) {
879 if(!hasOpSizePrefix) {
880 // For instructions without an OpSize prefix, a declared 16-bit register or
881 // immediate encoding is special.
882 ENCODING("i16imm", ENCODING_IW)
883 }
884 ENCODING("i32i8imm", ENCODING_IB)
885 ENCODING("SSECC", ENCODING_IB)
886 ENCODING("i16imm", ENCODING_Iv)
887 ENCODING("i16i8imm", ENCODING_IB)
888 ENCODING("i32imm", ENCODING_Iv)
889 ENCODING("i64i32imm", ENCODING_ID)
890 ENCODING("i64i8imm", ENCODING_IB)
891 ENCODING("i8imm", ENCODING_IB)
892 errs() << "Unhandled immediate encoding " << s << "\n";
893 llvm_unreachable("Unhandled immediate encoding");
894}
895
896OperandEncoding RecognizableInstr::rmRegisterEncodingFromString
897 (const std::string &s,
898 bool hasOpSizePrefix) {
899 ENCODING("GR16", ENCODING_RM)
900 ENCODING("GR32", ENCODING_RM)
901 ENCODING("GR64", ENCODING_RM)
902 ENCODING("GR8", ENCODING_RM)
903 ENCODING("VR128", ENCODING_RM)
904 ENCODING("FR64", ENCODING_RM)
905 ENCODING("FR32", ENCODING_RM)
906 ENCODING("VR64", ENCODING_RM)
907 errs() << "Unhandled R/M register encoding " << s << "\n";
908 llvm_unreachable("Unhandled R/M register encoding");
909}
910
911OperandEncoding RecognizableInstr::roRegisterEncodingFromString
912 (const std::string &s,
913 bool hasOpSizePrefix) {
914 ENCODING("GR16", ENCODING_REG)
915 ENCODING("GR32", ENCODING_REG)
916 ENCODING("GR64", ENCODING_REG)
917 ENCODING("GR8", ENCODING_REG)
918 ENCODING("VR128", ENCODING_REG)
919 ENCODING("FR64", ENCODING_REG)
920 ENCODING("FR32", ENCODING_REG)
921 ENCODING("VR64", ENCODING_REG)
922 ENCODING("SEGMENT_REG", ENCODING_REG)
923 ENCODING("DEBUG_REG", ENCODING_REG)
Sean Callanan1a8b7892010-05-06 20:59:00 +0000924 ENCODING("CONTROL_REG", ENCODING_REG)
Sean Callanan8ed9f512009-12-19 02:59:52 +0000925 errs() << "Unhandled reg/opcode register encoding " << s << "\n";
926 llvm_unreachable("Unhandled reg/opcode register encoding");
927}
928
929OperandEncoding RecognizableInstr::memoryEncodingFromString
930 (const std::string &s,
931 bool hasOpSizePrefix) {
932 ENCODING("i16mem", ENCODING_RM)
933 ENCODING("i32mem", ENCODING_RM)
934 ENCODING("i64mem", ENCODING_RM)
935 ENCODING("i8mem", ENCODING_RM)
Chris Lattnerb2ef4c12010-09-29 02:57:56 +0000936 ENCODING("ssmem", ENCODING_RM)
937 ENCODING("sdmem", ENCODING_RM)
Sean Callanan8ed9f512009-12-19 02:59:52 +0000938 ENCODING("f128mem", ENCODING_RM)
Chris Lattnerb2ef4c12010-09-29 02:57:56 +0000939 ENCODING("f256mem", ENCODING_RM)
Sean Callanan8ed9f512009-12-19 02:59:52 +0000940 ENCODING("f64mem", ENCODING_RM)
941 ENCODING("f32mem", ENCODING_RM)
942 ENCODING("i128mem", ENCODING_RM)
943 ENCODING("f80mem", ENCODING_RM)
944 ENCODING("lea32mem", ENCODING_RM)
945 ENCODING("lea64_32mem", ENCODING_RM)
946 ENCODING("lea64mem", ENCODING_RM)
947 ENCODING("opaque32mem", ENCODING_RM)
948 ENCODING("opaque48mem", ENCODING_RM)
949 ENCODING("opaque80mem", ENCODING_RM)
950 ENCODING("opaque512mem", ENCODING_RM)
951 errs() << "Unhandled memory encoding " << s << "\n";
952 llvm_unreachable("Unhandled memory encoding");
953}
954
955OperandEncoding RecognizableInstr::relocationEncodingFromString
956 (const std::string &s,
957 bool hasOpSizePrefix) {
958 if(!hasOpSizePrefix) {
959 // For instructions without an OpSize prefix, a declared 16-bit register or
960 // immediate encoding is special.
961 ENCODING("i16imm", ENCODING_IW)
962 }
963 ENCODING("i16imm", ENCODING_Iv)
964 ENCODING("i16i8imm", ENCODING_IB)
965 ENCODING("i32imm", ENCODING_Iv)
966 ENCODING("i32i8imm", ENCODING_IB)
967 ENCODING("i64i32imm", ENCODING_ID)
968 ENCODING("i64i8imm", ENCODING_IB)
969 ENCODING("i8imm", ENCODING_IB)
970 ENCODING("i64i32imm_pcrel", ENCODING_ID)
Chris Lattner9fc05222010-07-07 22:27:31 +0000971 ENCODING("i16imm_pcrel", ENCODING_IW)
Sean Callanan8ed9f512009-12-19 02:59:52 +0000972 ENCODING("i32imm_pcrel", ENCODING_ID)
973 ENCODING("brtarget", ENCODING_Iv)
974 ENCODING("brtarget8", ENCODING_IB)
975 ENCODING("i64imm", ENCODING_IO)
976 ENCODING("offset8", ENCODING_Ia)
977 ENCODING("offset16", ENCODING_Ia)
978 ENCODING("offset32", ENCODING_Ia)
979 ENCODING("offset64", ENCODING_Ia)
980 errs() << "Unhandled relocation encoding " << s << "\n";
981 llvm_unreachable("Unhandled relocation encoding");
982}
983
984OperandEncoding RecognizableInstr::opcodeModifierEncodingFromString
985 (const std::string &s,
986 bool hasOpSizePrefix) {
987 ENCODING("RST", ENCODING_I)
988 ENCODING("GR32", ENCODING_Rv)
989 ENCODING("GR64", ENCODING_RO)
990 ENCODING("GR16", ENCODING_Rv)
991 ENCODING("GR8", ENCODING_RB)
992 errs() << "Unhandled opcode modifier encoding " << s << "\n";
993 llvm_unreachable("Unhandled opcode modifier encoding");
994}
Daniel Dunbar9e6d1d12009-12-19 04:16:48 +0000995#undef ENCODING