blob: e3cad1aaa89fc29da7b40e45e423eb2fd428bf02 [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,
Sean Callanan9492be82010-02-12 23:39:46 +000054#define MAP(from, to) MRM_##from = to,
55 MRM_MAPPING
56#undef MAP
Sean Callanan6aeb2e32010-10-04 22:45:51 +000057 RawFrmImm8 = 43,
58 RawFrmImm16 = 44,
Sean Callanan9492be82010-02-12 23:39:46 +000059 lastMRM
Sean Callanan8ed9f512009-12-19 02:59:52 +000060 };
61
62 enum {
63 TB = 1,
64 REP = 2,
65 D8 = 3, D9 = 4, DA = 5, DB = 6,
66 DC = 7, DD = 8, DE = 9, DF = 10,
67 XD = 11, XS = 12,
Chris Lattner0d8db8e2010-02-12 02:06:33 +000068 T8 = 13, P_TA = 14,
69 P_0F_AE = 16, P_0F_01 = 17
Sean Callanan8ed9f512009-12-19 02:59:52 +000070 };
71}
Sean Callanan9492be82010-02-12 23:39:46 +000072
73// If rows are added to the opcode extension tables, then corresponding entries
74// must be added here.
75//
76// If the row corresponds to a single byte (i.e., 8f), then add an entry for
77// that byte to ONE_BYTE_EXTENSION_TABLES.
78//
79// If the row corresponds to two bytes where the first is 0f, add an entry for
80// the second byte to TWO_BYTE_EXTENSION_TABLES.
81//
82// If the row corresponds to some other set of bytes, you will need to modify
83// the code in RecognizableInstr::emitDecodePath() as well, and add new prefixes
84// to the X86 TD files, except in two cases: if the first two bytes of such a
85// new combination are 0f 38 or 0f 3a, you just have to add maps called
86// THREE_BYTE_38_EXTENSION_TABLES and THREE_BYTE_3A_EXTENSION_TABLES and add a
87// switch(Opcode) just below the case X86Local::T8: or case X86Local::TA: line
88// in RecognizableInstr::emitDecodePath().
89
Sean Callanan8ed9f512009-12-19 02:59:52 +000090#define ONE_BYTE_EXTENSION_TABLES \
91 EXTENSION_TABLE(80) \
92 EXTENSION_TABLE(81) \
93 EXTENSION_TABLE(82) \
94 EXTENSION_TABLE(83) \
95 EXTENSION_TABLE(8f) \
96 EXTENSION_TABLE(c0) \
97 EXTENSION_TABLE(c1) \
98 EXTENSION_TABLE(c6) \
99 EXTENSION_TABLE(c7) \
100 EXTENSION_TABLE(d0) \
101 EXTENSION_TABLE(d1) \
102 EXTENSION_TABLE(d2) \
103 EXTENSION_TABLE(d3) \
104 EXTENSION_TABLE(f6) \
105 EXTENSION_TABLE(f7) \
106 EXTENSION_TABLE(fe) \
107 EXTENSION_TABLE(ff)
108
109#define TWO_BYTE_EXTENSION_TABLES \
110 EXTENSION_TABLE(00) \
111 EXTENSION_TABLE(01) \
112 EXTENSION_TABLE(18) \
113 EXTENSION_TABLE(71) \
114 EXTENSION_TABLE(72) \
115 EXTENSION_TABLE(73) \
116 EXTENSION_TABLE(ae) \
Sean Callanan8ed9f512009-12-19 02:59:52 +0000117 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
Chris Lattnerc240bb02010-11-01 04:03:32 +0000222 Operands = &insn.Operands.OperandList;
Sean Callanan8ed9f512009-12-19 02:59:52 +0000223
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
371 return FILTER_NORMAL;
372}
373
374void RecognizableInstr::handleOperand(
375 bool optional,
376 unsigned &operandIndex,
377 unsigned &physicalOperandIndex,
378 unsigned &numPhysicalOperands,
379 unsigned *operandMapping,
380 OperandEncoding (*encodingFromString)(const std::string&, bool hasOpSizePrefix)) {
381 if (optional) {
382 if (physicalOperandIndex >= numPhysicalOperands)
383 return;
384 } else {
385 assert(physicalOperandIndex < numPhysicalOperands);
386 }
387
388 while (operandMapping[operandIndex] != operandIndex) {
389 Spec->operands[operandIndex].encoding = ENCODING_DUP;
390 Spec->operands[operandIndex].type =
391 (OperandType)(TYPE_DUP0 + operandMapping[operandIndex]);
392 ++operandIndex;
393 }
394
395 const std::string &typeName = (*Operands)[operandIndex].Rec->getName();
396
397 Spec->operands[operandIndex].encoding = encodingFromString(typeName,
398 HasOpSizePrefix);
399 Spec->operands[operandIndex].type = typeFromString(typeName,
400 IsSSE,
401 HasREX_WPrefix,
402 HasOpSizePrefix);
403
404 ++operandIndex;
405 ++physicalOperandIndex;
406}
407
408void RecognizableInstr::emitInstructionSpecifier(DisassemblerTables &tables) {
409 Spec->name = Name;
410
411 if (!Rec->isSubClassOf("X86Inst"))
412 return;
413
414 switch (filter()) {
415 case FILTER_WEAK:
416 Spec->filtered = true;
417 break;
418 case FILTER_STRONG:
419 ShouldBeEmitted = false;
420 return;
421 case FILTER_NORMAL:
422 break;
423 }
424
425 Spec->insnContext = insnContext();
426
Chris Lattnerc240bb02010-11-01 04:03:32 +0000427 const std::vector<CGIOperandList::OperandInfo> &OperandList = *Operands;
Sean Callanan8ed9f512009-12-19 02:59:52 +0000428
429 unsigned operandIndex;
430 unsigned numOperands = OperandList.size();
431 unsigned numPhysicalOperands = 0;
432
433 // operandMapping maps from operands in OperandList to their originals.
434 // If operandMapping[i] != i, then the entry is a duplicate.
435 unsigned operandMapping[X86_MAX_OPERANDS];
436
437 bool hasFROperands = false;
438
439 assert(numOperands < X86_MAX_OPERANDS && "X86_MAX_OPERANDS is not large enough");
440
441 for (operandIndex = 0; operandIndex < numOperands; ++operandIndex) {
442 if (OperandList[operandIndex].Constraints.size()) {
Chris Lattnerc240bb02010-11-01 04:03:32 +0000443 const CGIOperandList::ConstraintInfo &Constraint =
Chris Lattnera7d479c2010-02-10 01:45:28 +0000444 OperandList[operandIndex].Constraints[0];
445 if (Constraint.isTied()) {
446 operandMapping[operandIndex] = Constraint.getTiedOperand();
Sean Callanan8ed9f512009-12-19 02:59:52 +0000447 } else {
448 ++numPhysicalOperands;
449 operandMapping[operandIndex] = operandIndex;
450 }
451 } else {
452 ++numPhysicalOperands;
453 operandMapping[operandIndex] = operandIndex;
454 }
455
456 const std::string &recName = OperandList[operandIndex].Rec->getName();
457
458 if (recName.find("FR") != recName.npos)
459 hasFROperands = true;
460 }
461
462 if (hasFROperands && Name.find("MOV") != Name.npos &&
463 ((Name.find("2") != Name.npos && Name.find("32") == Name.npos) ||
464 (Name.find("to") != Name.npos)))
465 ShouldBeEmitted = false;
466
467 if (!ShouldBeEmitted)
468 return;
469
470#define HANDLE_OPERAND(class) \
471 handleOperand(false, \
472 operandIndex, \
473 physicalOperandIndex, \
474 numPhysicalOperands, \
475 operandMapping, \
476 class##EncodingFromString);
477
478#define HANDLE_OPTIONAL(class) \
479 handleOperand(true, \
480 operandIndex, \
481 physicalOperandIndex, \
482 numPhysicalOperands, \
483 operandMapping, \
484 class##EncodingFromString);
485
486 // operandIndex should always be < numOperands
487 operandIndex = 0;
488 // physicalOperandIndex should always be < numPhysicalOperands
489 unsigned physicalOperandIndex = 0;
490
491 switch (Form) {
492 case X86Local::RawFrm:
493 // Operand 1 (optional) is an address or immediate.
494 // Operand 2 (optional) is an immediate.
495 assert(numPhysicalOperands <= 2 &&
496 "Unexpected number of operands for RawFrm");
497 HANDLE_OPTIONAL(relocation)
498 HANDLE_OPTIONAL(immediate)
499 break;
500 case X86Local::AddRegFrm:
501 // Operand 1 is added to the opcode.
502 // Operand 2 (optional) is an address.
503 assert(numPhysicalOperands >= 1 && numPhysicalOperands <= 2 &&
504 "Unexpected number of operands for AddRegFrm");
505 HANDLE_OPERAND(opcodeModifier)
506 HANDLE_OPTIONAL(relocation)
507 break;
508 case X86Local::MRMDestReg:
509 // Operand 1 is a register operand in the R/M field.
510 // Operand 2 is a register operand in the Reg/Opcode field.
511 // Operand 3 (optional) is an immediate.
512 assert(numPhysicalOperands >= 2 && numPhysicalOperands <= 3 &&
513 "Unexpected number of operands for MRMDestRegFrm");
514 HANDLE_OPERAND(rmRegister)
515 HANDLE_OPERAND(roRegister)
516 HANDLE_OPTIONAL(immediate)
517 break;
518 case X86Local::MRMDestMem:
519 // Operand 1 is a memory operand (possibly SIB-extended)
520 // Operand 2 is a register operand in the Reg/Opcode field.
521 // Operand 3 (optional) is an immediate.
522 assert(numPhysicalOperands >= 2 && numPhysicalOperands <= 3 &&
523 "Unexpected number of operands for MRMDestMemFrm");
524 HANDLE_OPERAND(memory)
525 HANDLE_OPERAND(roRegister)
526 HANDLE_OPTIONAL(immediate)
527 break;
528 case X86Local::MRMSrcReg:
529 // Operand 1 is a register operand in the Reg/Opcode field.
530 // Operand 2 is a register operand in the R/M field.
531 // Operand 3 (optional) is an immediate.
532 assert(numPhysicalOperands >= 2 && numPhysicalOperands <= 3 &&
533 "Unexpected number of operands for MRMSrcRegFrm");
534 HANDLE_OPERAND(roRegister)
535 HANDLE_OPERAND(rmRegister)
Bruno Cardoso Lopes99405df2010-06-08 22:51:23 +0000536
537 if (HasVEX_4VPrefix)
Bruno Cardoso Lopesc902a592010-06-11 23:50:47 +0000538 // FIXME: In AVX, the register below becomes the one encoded
539 // in ModRMVEX and the one above the one in the VEX.VVVV field
Bruno Cardoso Lopes99405df2010-06-08 22:51:23 +0000540 HANDLE_OPTIONAL(rmRegister)
541 else
542 HANDLE_OPTIONAL(immediate)
Sean Callanan8ed9f512009-12-19 02:59:52 +0000543 break;
544 case X86Local::MRMSrcMem:
545 // Operand 1 is a register operand in the Reg/Opcode field.
546 // Operand 2 is a memory operand (possibly SIB-extended)
547 // Operand 3 (optional) is an immediate.
548 assert(numPhysicalOperands >= 2 && numPhysicalOperands <= 3 &&
549 "Unexpected number of operands for MRMSrcMemFrm");
550 HANDLE_OPERAND(roRegister)
Bruno Cardoso Lopesc902a592010-06-11 23:50:47 +0000551
552 if (HasVEX_4VPrefix)
553 // FIXME: In AVX, the register below becomes the one encoded
554 // in ModRMVEX and the one above the one in the VEX.VVVV field
555 HANDLE_OPTIONAL(rmRegister)
556
Sean Callanan8ed9f512009-12-19 02:59:52 +0000557 HANDLE_OPERAND(memory)
558 HANDLE_OPTIONAL(immediate)
559 break;
560 case X86Local::MRM0r:
561 case X86Local::MRM1r:
562 case X86Local::MRM2r:
563 case X86Local::MRM3r:
564 case X86Local::MRM4r:
565 case X86Local::MRM5r:
566 case X86Local::MRM6r:
567 case X86Local::MRM7r:
568 // Operand 1 is a register operand in the R/M field.
569 // Operand 2 (optional) is an immediate or relocation.
570 assert(numPhysicalOperands <= 2 &&
571 "Unexpected number of operands for MRMnRFrm");
572 HANDLE_OPTIONAL(rmRegister)
573 HANDLE_OPTIONAL(relocation)
574 break;
575 case X86Local::MRM0m:
576 case X86Local::MRM1m:
577 case X86Local::MRM2m:
578 case X86Local::MRM3m:
579 case X86Local::MRM4m:
580 case X86Local::MRM5m:
581 case X86Local::MRM6m:
582 case X86Local::MRM7m:
583 // Operand 1 is a memory operand (possibly SIB-extended)
584 // Operand 2 (optional) is an immediate or relocation.
585 assert(numPhysicalOperands >= 1 && numPhysicalOperands <= 2 &&
586 "Unexpected number of operands for MRMnMFrm");
587 HANDLE_OPERAND(memory)
588 HANDLE_OPTIONAL(relocation)
589 break;
Sean Callanan6aeb2e32010-10-04 22:45:51 +0000590 case X86Local::RawFrmImm8:
591 // operand 1 is a 16-bit immediate
592 // operand 2 is an 8-bit immediate
593 assert(numPhysicalOperands == 2 &&
594 "Unexpected number of operands for X86Local::RawFrmImm8");
595 HANDLE_OPERAND(immediate)
596 HANDLE_OPERAND(immediate)
597 break;
598 case X86Local::RawFrmImm16:
599 // operand 1 is a 16-bit immediate
600 // operand 2 is a 16-bit immediate
601 HANDLE_OPERAND(immediate)
602 HANDLE_OPERAND(immediate)
603 break;
Sean Callanan8ed9f512009-12-19 02:59:52 +0000604 case X86Local::MRMInitReg:
605 // Ignored.
606 break;
607 }
608
609 #undef HANDLE_OPERAND
610 #undef HANDLE_OPTIONAL
611}
612
613void RecognizableInstr::emitDecodePath(DisassemblerTables &tables) const {
614 // Special cases where the LLVM tables are not complete
615
Sean Callanan9492be82010-02-12 23:39:46 +0000616#define MAP(from, to) \
617 case X86Local::MRM_##from: \
618 filter = new ExactFilter(0x##from); \
619 break;
Sean Callanan8ed9f512009-12-19 02:59:52 +0000620
621 OpcodeType opcodeType = (OpcodeType)-1;
622
623 ModRMFilter* filter = NULL;
624 uint8_t opcodeToSet = 0;
625
626 switch (Prefix) {
627 // Extended two-byte opcodes can start with f2 0f, f3 0f, or 0f
628 case X86Local::XD:
629 case X86Local::XS:
630 case X86Local::TB:
631 opcodeType = TWOBYTE;
632
633 switch (Opcode) {
Sean Callanan95a5a7d2010-02-13 01:48:34 +0000634 default:
635 if (needsModRMForDecode(Form))
636 filter = new ModFilter(isRegFormat(Form));
637 else
638 filter = new DumbFilter();
639 break;
Sean Callanan8ed9f512009-12-19 02:59:52 +0000640#define EXTENSION_TABLE(n) case 0x##n:
641 TWO_BYTE_EXTENSION_TABLES
642#undef EXTENSION_TABLE
643 switch (Form) {
644 default:
645 llvm_unreachable("Unhandled two-byte extended opcode");
646 case X86Local::MRM0r:
647 case X86Local::MRM1r:
648 case X86Local::MRM2r:
649 case X86Local::MRM3r:
650 case X86Local::MRM4r:
651 case X86Local::MRM5r:
652 case X86Local::MRM6r:
653 case X86Local::MRM7r:
654 filter = new ExtendedFilter(true, Form - X86Local::MRM0r);
655 break;
656 case X86Local::MRM0m:
657 case X86Local::MRM1m:
658 case X86Local::MRM2m:
659 case X86Local::MRM3m:
660 case X86Local::MRM4m:
661 case X86Local::MRM5m:
662 case X86Local::MRM6m:
663 case X86Local::MRM7m:
664 filter = new ExtendedFilter(false, Form - X86Local::MRM0m);
665 break;
Sean Callanan9492be82010-02-12 23:39:46 +0000666 MRM_MAPPING
Sean Callanan8ed9f512009-12-19 02:59:52 +0000667 } // switch (Form)
668 break;
Sean Callanan95a5a7d2010-02-13 01:48:34 +0000669 } // switch (Opcode)
Sean Callanan8ed9f512009-12-19 02:59:52 +0000670 opcodeToSet = Opcode;
671 break;
672 case X86Local::T8:
673 opcodeType = THREEBYTE_38;
674 if (needsModRMForDecode(Form))
675 filter = new ModFilter(isRegFormat(Form));
676 else
677 filter = new DumbFilter();
678 opcodeToSet = Opcode;
679 break;
Chris Lattner0d8db8e2010-02-12 02:06:33 +0000680 case X86Local::P_TA:
Sean Callanan8ed9f512009-12-19 02:59:52 +0000681 opcodeType = THREEBYTE_3A;
682 if (needsModRMForDecode(Form))
683 filter = new ModFilter(isRegFormat(Form));
684 else
685 filter = new DumbFilter();
686 opcodeToSet = Opcode;
687 break;
688 case X86Local::D8:
689 case X86Local::D9:
690 case X86Local::DA:
691 case X86Local::DB:
692 case X86Local::DC:
693 case X86Local::DD:
694 case X86Local::DE:
695 case X86Local::DF:
696 assert(Opcode >= 0xc0 && "Unexpected opcode for an escape opcode");
697 opcodeType = ONEBYTE;
698 if (Form == X86Local::AddRegFrm) {
699 Spec->modifierType = MODIFIER_MODRM;
700 Spec->modifierBase = Opcode;
701 filter = new AddRegEscapeFilter(Opcode);
702 } else {
703 filter = new EscapeFilter(true, Opcode);
704 }
705 opcodeToSet = 0xd8 + (Prefix - X86Local::D8);
706 break;
707 default:
708 opcodeType = ONEBYTE;
709 switch (Opcode) {
710#define EXTENSION_TABLE(n) case 0x##n:
711 ONE_BYTE_EXTENSION_TABLES
712#undef EXTENSION_TABLE
713 switch (Form) {
714 default:
715 llvm_unreachable("Fell through the cracks of a single-byte "
716 "extended opcode");
717 case X86Local::MRM0r:
718 case X86Local::MRM1r:
719 case X86Local::MRM2r:
720 case X86Local::MRM3r:
721 case X86Local::MRM4r:
722 case X86Local::MRM5r:
723 case X86Local::MRM6r:
724 case X86Local::MRM7r:
725 filter = new ExtendedFilter(true, Form - X86Local::MRM0r);
726 break;
727 case X86Local::MRM0m:
728 case X86Local::MRM1m:
729 case X86Local::MRM2m:
730 case X86Local::MRM3m:
731 case X86Local::MRM4m:
732 case X86Local::MRM5m:
733 case X86Local::MRM6m:
734 case X86Local::MRM7m:
735 filter = new ExtendedFilter(false, Form - X86Local::MRM0m);
736 break;
Sean Callanan9492be82010-02-12 23:39:46 +0000737 MRM_MAPPING
Sean Callanan8ed9f512009-12-19 02:59:52 +0000738 } // switch (Form)
739 break;
740 case 0xd8:
741 case 0xd9:
742 case 0xda:
743 case 0xdb:
744 case 0xdc:
745 case 0xdd:
746 case 0xde:
747 case 0xdf:
748 filter = new EscapeFilter(false, Form - X86Local::MRM0m);
749 break;
750 default:
751 if (needsModRMForDecode(Form))
752 filter = new ModFilter(isRegFormat(Form));
753 else
754 filter = new DumbFilter();
755 break;
756 } // switch (Opcode)
757 opcodeToSet = Opcode;
758 } // switch (Prefix)
759
760 assert(opcodeType != (OpcodeType)-1 &&
761 "Opcode type not set");
762 assert(filter && "Filter not set");
763
764 if (Form == X86Local::AddRegFrm) {
765 if(Spec->modifierType != MODIFIER_MODRM) {
766 assert(opcodeToSet < 0xf9 &&
767 "Not enough room for all ADDREG_FRM operands");
768
769 uint8_t currentOpcode;
770
771 for (currentOpcode = opcodeToSet;
772 currentOpcode < opcodeToSet + 8;
773 ++currentOpcode)
774 tables.setTableFields(opcodeType,
775 insnContext(),
776 currentOpcode,
777 *filter,
778 UID);
779
780 Spec->modifierType = MODIFIER_OPCODE;
781 Spec->modifierBase = opcodeToSet;
782 } else {
783 // modifierBase was set where MODIFIER_MODRM was set
784 tables.setTableFields(opcodeType,
785 insnContext(),
786 opcodeToSet,
787 *filter,
788 UID);
789 }
790 } else {
791 tables.setTableFields(opcodeType,
792 insnContext(),
793 opcodeToSet,
794 *filter,
795 UID);
796
797 Spec->modifierType = MODIFIER_NONE;
798 Spec->modifierBase = opcodeToSet;
799 }
800
801 delete filter;
Sean Callanan9492be82010-02-12 23:39:46 +0000802
803#undef MAP
Sean Callanan8ed9f512009-12-19 02:59:52 +0000804}
805
806#define TYPE(str, type) if (s == str) return type;
807OperandType RecognizableInstr::typeFromString(const std::string &s,
808 bool isSSE,
809 bool hasREX_WPrefix,
810 bool hasOpSizePrefix) {
811 if (isSSE) {
812 // For SSE instructions, we ignore the OpSize prefix and force operand
813 // sizes.
814 TYPE("GR16", TYPE_R16)
815 TYPE("GR32", TYPE_R32)
816 TYPE("GR64", TYPE_R64)
817 }
818 if(hasREX_WPrefix) {
819 // For instructions with a REX_W prefix, a declared 32-bit register encoding
820 // is special.
821 TYPE("GR32", TYPE_R32)
822 }
823 if(!hasOpSizePrefix) {
824 // For instructions without an OpSize prefix, a declared 16-bit register or
825 // immediate encoding is special.
826 TYPE("GR16", TYPE_R16)
827 TYPE("i16imm", TYPE_IMM16)
828 }
829 TYPE("i16mem", TYPE_Mv)
830 TYPE("i16imm", TYPE_IMMv)
831 TYPE("i16i8imm", TYPE_IMMv)
832 TYPE("GR16", TYPE_Rv)
833 TYPE("i32mem", TYPE_Mv)
834 TYPE("i32imm", TYPE_IMMv)
835 TYPE("i32i8imm", TYPE_IMM32)
836 TYPE("GR32", TYPE_Rv)
837 TYPE("i64mem", TYPE_Mv)
838 TYPE("i64i32imm", TYPE_IMM64)
839 TYPE("i64i8imm", TYPE_IMM64)
840 TYPE("GR64", TYPE_R64)
841 TYPE("i8mem", TYPE_M8)
842 TYPE("i8imm", TYPE_IMM8)
843 TYPE("GR8", TYPE_R8)
844 TYPE("VR128", TYPE_XMM128)
845 TYPE("f128mem", TYPE_M128)
Chris Lattnerb2ef4c12010-09-29 02:57:56 +0000846 TYPE("f256mem", TYPE_M256)
Sean Callanan8ed9f512009-12-19 02:59:52 +0000847 TYPE("FR64", TYPE_XMM64)
848 TYPE("f64mem", TYPE_M64FP)
Chris Lattnerb2ef4c12010-09-29 02:57:56 +0000849 TYPE("sdmem", TYPE_M64FP)
Sean Callanan8ed9f512009-12-19 02:59:52 +0000850 TYPE("FR32", TYPE_XMM32)
851 TYPE("f32mem", TYPE_M32FP)
Chris Lattnerb2ef4c12010-09-29 02:57:56 +0000852 TYPE("ssmem", TYPE_M32FP)
Sean Callanan8ed9f512009-12-19 02:59:52 +0000853 TYPE("RST", TYPE_ST)
854 TYPE("i128mem", TYPE_M128)
855 TYPE("i64i32imm_pcrel", TYPE_REL64)
Chris Lattner9fc05222010-07-07 22:27:31 +0000856 TYPE("i16imm_pcrel", TYPE_REL16)
Sean Callanan8ed9f512009-12-19 02:59:52 +0000857 TYPE("i32imm_pcrel", TYPE_REL32)
Sean Callanan5edca812010-04-07 21:42:19 +0000858 TYPE("SSECC", TYPE_IMM3)
Sean Callanan8ed9f512009-12-19 02:59:52 +0000859 TYPE("brtarget", TYPE_RELv)
860 TYPE("brtarget8", TYPE_REL8)
861 TYPE("f80mem", TYPE_M80FP)
Sean Callanan7fb35a22009-12-22 21:12:55 +0000862 TYPE("lea32mem", TYPE_LEA)
863 TYPE("lea64_32mem", TYPE_LEA)
864 TYPE("lea64mem", TYPE_LEA)
Sean Callanan8ed9f512009-12-19 02:59:52 +0000865 TYPE("VR64", TYPE_MM64)
866 TYPE("i64imm", TYPE_IMMv)
867 TYPE("opaque32mem", TYPE_M1616)
868 TYPE("opaque48mem", TYPE_M1632)
869 TYPE("opaque80mem", TYPE_M1664)
870 TYPE("opaque512mem", TYPE_M512)
871 TYPE("SEGMENT_REG", TYPE_SEGMENTREG)
872 TYPE("DEBUG_REG", TYPE_DEBUGREG)
Sean Callanan1a8b7892010-05-06 20:59:00 +0000873 TYPE("CONTROL_REG", TYPE_CONTROLREG)
Sean Callanan8ed9f512009-12-19 02:59:52 +0000874 TYPE("offset8", TYPE_MOFFS8)
875 TYPE("offset16", TYPE_MOFFS16)
876 TYPE("offset32", TYPE_MOFFS32)
877 TYPE("offset64", TYPE_MOFFS64)
878 errs() << "Unhandled type string " << s << "\n";
879 llvm_unreachable("Unhandled type string");
880}
881#undef TYPE
882
883#define ENCODING(str, encoding) if (s == str) return encoding;
884OperandEncoding RecognizableInstr::immediateEncodingFromString
885 (const std::string &s,
886 bool hasOpSizePrefix) {
887 if(!hasOpSizePrefix) {
888 // For instructions without an OpSize prefix, a declared 16-bit register or
889 // immediate encoding is special.
890 ENCODING("i16imm", ENCODING_IW)
891 }
892 ENCODING("i32i8imm", ENCODING_IB)
893 ENCODING("SSECC", ENCODING_IB)
894 ENCODING("i16imm", ENCODING_Iv)
895 ENCODING("i16i8imm", ENCODING_IB)
896 ENCODING("i32imm", ENCODING_Iv)
897 ENCODING("i64i32imm", ENCODING_ID)
898 ENCODING("i64i8imm", ENCODING_IB)
899 ENCODING("i8imm", ENCODING_IB)
900 errs() << "Unhandled immediate encoding " << s << "\n";
901 llvm_unreachable("Unhandled immediate encoding");
902}
903
904OperandEncoding RecognizableInstr::rmRegisterEncodingFromString
905 (const std::string &s,
906 bool hasOpSizePrefix) {
907 ENCODING("GR16", ENCODING_RM)
908 ENCODING("GR32", ENCODING_RM)
909 ENCODING("GR64", ENCODING_RM)
910 ENCODING("GR8", ENCODING_RM)
911 ENCODING("VR128", ENCODING_RM)
912 ENCODING("FR64", ENCODING_RM)
913 ENCODING("FR32", ENCODING_RM)
914 ENCODING("VR64", ENCODING_RM)
915 errs() << "Unhandled R/M register encoding " << s << "\n";
916 llvm_unreachable("Unhandled R/M register encoding");
917}
918
919OperandEncoding RecognizableInstr::roRegisterEncodingFromString
920 (const std::string &s,
921 bool hasOpSizePrefix) {
922 ENCODING("GR16", ENCODING_REG)
923 ENCODING("GR32", ENCODING_REG)
924 ENCODING("GR64", ENCODING_REG)
925 ENCODING("GR8", ENCODING_REG)
926 ENCODING("VR128", ENCODING_REG)
927 ENCODING("FR64", ENCODING_REG)
928 ENCODING("FR32", ENCODING_REG)
929 ENCODING("VR64", ENCODING_REG)
930 ENCODING("SEGMENT_REG", ENCODING_REG)
931 ENCODING("DEBUG_REG", ENCODING_REG)
Sean Callanan1a8b7892010-05-06 20:59:00 +0000932 ENCODING("CONTROL_REG", ENCODING_REG)
Sean Callanan8ed9f512009-12-19 02:59:52 +0000933 errs() << "Unhandled reg/opcode register encoding " << s << "\n";
934 llvm_unreachable("Unhandled reg/opcode register encoding");
935}
936
937OperandEncoding RecognizableInstr::memoryEncodingFromString
938 (const std::string &s,
939 bool hasOpSizePrefix) {
940 ENCODING("i16mem", ENCODING_RM)
941 ENCODING("i32mem", ENCODING_RM)
942 ENCODING("i64mem", ENCODING_RM)
943 ENCODING("i8mem", ENCODING_RM)
Chris Lattnerb2ef4c12010-09-29 02:57:56 +0000944 ENCODING("ssmem", ENCODING_RM)
945 ENCODING("sdmem", ENCODING_RM)
Sean Callanan8ed9f512009-12-19 02:59:52 +0000946 ENCODING("f128mem", ENCODING_RM)
Chris Lattnerb2ef4c12010-09-29 02:57:56 +0000947 ENCODING("f256mem", ENCODING_RM)
Sean Callanan8ed9f512009-12-19 02:59:52 +0000948 ENCODING("f64mem", ENCODING_RM)
949 ENCODING("f32mem", ENCODING_RM)
950 ENCODING("i128mem", ENCODING_RM)
951 ENCODING("f80mem", ENCODING_RM)
952 ENCODING("lea32mem", ENCODING_RM)
953 ENCODING("lea64_32mem", ENCODING_RM)
954 ENCODING("lea64mem", ENCODING_RM)
955 ENCODING("opaque32mem", ENCODING_RM)
956 ENCODING("opaque48mem", ENCODING_RM)
957 ENCODING("opaque80mem", ENCODING_RM)
958 ENCODING("opaque512mem", ENCODING_RM)
959 errs() << "Unhandled memory encoding " << s << "\n";
960 llvm_unreachable("Unhandled memory encoding");
961}
962
963OperandEncoding RecognizableInstr::relocationEncodingFromString
964 (const std::string &s,
965 bool hasOpSizePrefix) {
966 if(!hasOpSizePrefix) {
967 // For instructions without an OpSize prefix, a declared 16-bit register or
968 // immediate encoding is special.
969 ENCODING("i16imm", ENCODING_IW)
970 }
971 ENCODING("i16imm", ENCODING_Iv)
972 ENCODING("i16i8imm", ENCODING_IB)
973 ENCODING("i32imm", ENCODING_Iv)
974 ENCODING("i32i8imm", ENCODING_IB)
975 ENCODING("i64i32imm", ENCODING_ID)
976 ENCODING("i64i8imm", ENCODING_IB)
977 ENCODING("i8imm", ENCODING_IB)
978 ENCODING("i64i32imm_pcrel", ENCODING_ID)
Chris Lattner9fc05222010-07-07 22:27:31 +0000979 ENCODING("i16imm_pcrel", ENCODING_IW)
Sean Callanan8ed9f512009-12-19 02:59:52 +0000980 ENCODING("i32imm_pcrel", ENCODING_ID)
981 ENCODING("brtarget", ENCODING_Iv)
982 ENCODING("brtarget8", ENCODING_IB)
983 ENCODING("i64imm", ENCODING_IO)
984 ENCODING("offset8", ENCODING_Ia)
985 ENCODING("offset16", ENCODING_Ia)
986 ENCODING("offset32", ENCODING_Ia)
987 ENCODING("offset64", ENCODING_Ia)
988 errs() << "Unhandled relocation encoding " << s << "\n";
989 llvm_unreachable("Unhandled relocation encoding");
990}
991
992OperandEncoding RecognizableInstr::opcodeModifierEncodingFromString
993 (const std::string &s,
994 bool hasOpSizePrefix) {
995 ENCODING("RST", ENCODING_I)
996 ENCODING("GR32", ENCODING_Rv)
997 ENCODING("GR64", ENCODING_RO)
998 ENCODING("GR16", ENCODING_Rv)
999 ENCODING("GR8", ENCODING_RB)
1000 errs() << "Unhandled opcode modifier encoding " << s << "\n";
1001 llvm_unreachable("Unhandled opcode modifier encoding");
1002}
Daniel Dunbar9e6d1d12009-12-19 04:16:48 +00001003#undef ENCODING