blob: 7aab9b28724d7208e3a49a965c012d09f45ab7b4 [file] [log] [blame]
Sean Callanan8ed9f512009-12-19 02:59:52 +00001//===- X86DisassemblerTables.cpp - Disassembler tables ----------*- 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 the disassembler tables.
12// Documentation for the disassembler emitter in general can be found in
13// X86DisasemblerEmitter.h.
14//
15//===----------------------------------------------------------------------===//
16
17#include "X86DisassemblerShared.h"
18#include "X86DisassemblerTables.h"
19
20#include "TableGenBackend.h"
21#include "llvm/Support/ErrorHandling.h"
22#include "llvm/Support/Format.h"
23
Sean Callanan8ed9f512009-12-19 02:59:52 +000024using namespace llvm;
25using namespace X86Disassembler;
26
27/// inheritsFrom - Indicates whether all instructions in one class also belong
28/// to another class.
29///
30/// @param child - The class that may be the subset
31/// @param parent - The class that may be the superset
32/// @return - True if child is a subset of parent, false otherwise.
33static inline bool inheritsFrom(InstructionContext child,
34 InstructionContext parent) {
35 if (child == parent)
36 return true;
37
38 switch (parent) {
39 case IC:
40 return true;
41 case IC_64BIT:
42 return(inheritsFrom(child, IC_64BIT_REXW) ||
43 inheritsFrom(child, IC_64BIT_OPSIZE) ||
44 inheritsFrom(child, IC_64BIT_XD) ||
45 inheritsFrom(child, IC_64BIT_XS));
46 case IC_OPSIZE:
47 return(inheritsFrom(child, IC_64BIT_OPSIZE));
48 case IC_XD:
49 return(inheritsFrom(child, IC_64BIT_XD));
50 case IC_XS:
51 return(inheritsFrom(child, IC_64BIT_XS));
52 case IC_64BIT_REXW:
53 return(inheritsFrom(child, IC_64BIT_REXW_XS) ||
54 inheritsFrom(child, IC_64BIT_REXW_XD) ||
55 inheritsFrom(child, IC_64BIT_REXW_OPSIZE));
56 case IC_64BIT_OPSIZE:
57 return(inheritsFrom(child, IC_64BIT_REXW_OPSIZE));
58 case IC_64BIT_XD:
59 return(inheritsFrom(child, IC_64BIT_REXW_XD));
60 case IC_64BIT_XS:
61 return(inheritsFrom(child, IC_64BIT_REXW_XS));
62 case IC_64BIT_REXW_XD:
63 return false;
64 case IC_64BIT_REXW_XS:
65 return false;
66 case IC_64BIT_REXW_OPSIZE:
67 return false;
68 default:
69 return false;
70 }
71}
72
73/// outranks - Indicates whether, if an instruction has two different applicable
74/// classes, which class should be preferred when performing decode. This
75/// imposes a total ordering (ties are resolved toward "lower")
76///
77/// @param upper - The class that may be preferable
78/// @param lower - The class that may be less preferable
79/// @return - True if upper is to be preferred, false otherwise.
80static inline bool outranks(InstructionContext upper,
81 InstructionContext lower) {
82 assert(upper < IC_max);
83 assert(lower < IC_max);
84
85#define ENUM_ENTRY(n, r, d) r,
86 static int ranks[IC_max] = {
87 INSTRUCTION_CONTEXTS
88 };
89#undef ENUM_ENTRY
90
91 return (ranks[upper] > ranks[lower]);
92}
93
94/// stringForContext - Returns a string containing the name of a particular
95/// InstructionContext, usually for diagnostic purposes.
96///
97/// @param insnContext - The instruction class to transform to a string.
98/// @return - A statically-allocated string constant that contains the
99/// name of the instruction class.
100static inline const char* stringForContext(InstructionContext insnContext) {
101 switch (insnContext) {
102 default:
103 llvm_unreachable("Unhandled instruction class");
104#define ENUM_ENTRY(n, r, d) case n: return #n; break;
105 INSTRUCTION_CONTEXTS
106#undef ENUM_ENTRY
107 }
108}
109
110/// stringForOperandType - Like stringForContext, but for OperandTypes.
111static inline const char* stringForOperandType(OperandType type) {
112 switch (type) {
113 default:
114 llvm_unreachable("Unhandled type");
115#define ENUM_ENTRY(i, d) case i: return #i;
116 TYPES
117#undef ENUM_ENTRY
118 }
119}
120
121/// stringForOperandEncoding - like stringForContext, but for
122/// OperandEncodings.
123static inline const char* stringForOperandEncoding(OperandEncoding encoding) {
124 switch (encoding) {
125 default:
126 llvm_unreachable("Unhandled encoding");
127#define ENUM_ENTRY(i, d) case i: return #i;
128 ENCODINGS
129#undef ENUM_ENTRY
130 }
131}
132
133void DisassemblerTables::emitOneID(raw_ostream &o,
134 uint32_t &i,
135 InstrUID id,
136 bool addComma) const {
137 if (id)
138 o.indent(i * 2) << format("0x%hx", id);
139 else
140 o.indent(i * 2) << 0;
141
142 if (addComma)
143 o << ", ";
144 else
145 o << " ";
146
147 o << "/* ";
148 o << InstructionSpecifiers[id].name;
149 o << "*/";
150
151 o << "\n";
152}
153
154/// emitEmptyTable - Emits the modRMEmptyTable, which is used as a ID table by
155/// all ModR/M decisions for instructions that are invalid for all possible
156/// ModR/M byte values.
157///
158/// @param o - The output stream on which to emit the table.
159/// @param i - The indentation level for that output stream.
160static void emitEmptyTable(raw_ostream &o, uint32_t &i)
161{
162 o.indent(i * 2) << "InstrUID modRMEmptyTable[1] = { 0 };" << "\n";
163 o << "\n";
164}
165
166/// getDecisionType - Determines whether a ModRM decision with 255 entries can
167/// be compacted by eliminating redundant information.
168///
169/// @param decision - The decision to be compacted.
170/// @return - The compactest available representation for the decision.
171static ModRMDecisionType getDecisionType(ModRMDecision &decision)
172{
173 bool satisfiesOneEntry = true;
174 bool satisfiesSplitRM = true;
175
176 uint16_t index;
177
178 for (index = 0; index < 256; ++index) {
179 if (decision.instructionIDs[index] != decision.instructionIDs[0])
180 satisfiesOneEntry = false;
181
182 if (((index & 0xc0) == 0xc0) &&
183 (decision.instructionIDs[index] != decision.instructionIDs[0xc0]))
184 satisfiesSplitRM = false;
185
186 if (((index & 0xc0) != 0xc0) &&
187 (decision.instructionIDs[index] != decision.instructionIDs[0x00]))
188 satisfiesSplitRM = false;
189 }
190
191 if (satisfiesOneEntry)
192 return MODRM_ONEENTRY;
193
194 if (satisfiesSplitRM)
195 return MODRM_SPLITRM;
196
197 return MODRM_FULL;
198}
199
200/// stringForDecisionType - Returns a statically-allocated string corresponding
201/// to a particular decision type.
202///
203/// @param dt - The decision type.
204/// @return - A pointer to the statically-allocated string (e.g.,
205/// "MODRM_ONEENTRY" for MODRM_ONEENTRY).
206static const char* stringForDecisionType(ModRMDecisionType dt)
207{
208#define ENUM_ENTRY(n) case n: return #n;
209 switch (dt) {
210 default:
211 llvm_unreachable("Unknown decision type");
212 MODRMTYPES
213 };
214#undef ENUM_ENTRY
215}
216
217/// stringForModifierType - Returns a statically-allocated string corresponding
218/// to an opcode modifier type.
219///
220/// @param mt - The modifier type.
221/// @return - A pointer to the statically-allocated string (e.g.,
222/// "MODIFIER_NONE" for MODIFIER_NONE).
223static const char* stringForModifierType(ModifierType mt)
224{
225#define ENUM_ENTRY(n) case n: return #n;
226 switch(mt) {
227 default:
228 llvm_unreachable("Unknown modifier type");
229 MODIFIER_TYPES
230 };
231#undef ENUM_ENTRY
232}
233
234DisassemblerTables::DisassemblerTables() {
235 unsigned i;
236
237 for (i = 0; i < 4; i++) {
238 Tables[i] = new ContextDecision;
Daniel Dunbar87830872009-12-19 04:16:57 +0000239 memset(Tables[i], 0, sizeof(ContextDecision));
Sean Callanan8ed9f512009-12-19 02:59:52 +0000240 }
241
242 HasConflicts = false;
243}
244
245DisassemblerTables::~DisassemblerTables() {
246 unsigned i;
247
248 for (i = 0; i < 4; i++)
249 delete Tables[i];
250}
251
252void DisassemblerTables::emitModRMDecision(raw_ostream &o1,
253 raw_ostream &o2,
254 uint32_t &i1,
255 uint32_t &i2,
256 ModRMDecision &decision)
257 const {
258 static uint64_t sTableNumber = 0;
259 uint64_t thisTableNumber = sTableNumber;
260 ModRMDecisionType dt = getDecisionType(decision);
261 uint16_t index;
262
263 if (dt == MODRM_ONEENTRY && decision.instructionIDs[0] == 0)
264 {
265 o2.indent(i2) << "{ /* ModRMDecision */" << "\n";
266 i2++;
267
268 o2.indent(i2) << stringForDecisionType(dt) << "," << "\n";
269 o2.indent(i2) << "modRMEmptyTable";
270
271 i2--;
272 o2.indent(i2) << "}";
273 return;
274 }
275
276 o1.indent(i1) << "InstrUID modRMTable" << thisTableNumber;
277
278 switch (dt) {
279 default:
280 llvm_unreachable("Unknown decision type");
281 case MODRM_ONEENTRY:
282 o1 << "[1]";
283 break;
284 case MODRM_SPLITRM:
285 o1 << "[2]";
286 break;
287 case MODRM_FULL:
288 o1 << "[256]";
289 break;
290 }
291
292 o1 << " = {" << "\n";
293 i1++;
294
295 switch (dt) {
296 default:
297 llvm_unreachable("Unknown decision type");
298 case MODRM_ONEENTRY:
299 emitOneID(o1, i1, decision.instructionIDs[0], false);
300 break;
301 case MODRM_SPLITRM:
302 emitOneID(o1, i1, decision.instructionIDs[0x00], true); // mod = 0b00
303 emitOneID(o1, i1, decision.instructionIDs[0xc0], false); // mod = 0b11
304 break;
305 case MODRM_FULL:
306 for (index = 0; index < 256; ++index)
307 emitOneID(o1, i1, decision.instructionIDs[index], index < 255);
308 break;
309 }
310
311 i1--;
312 o1.indent(i1) << "};" << "\n";
313 o1 << "\n";
314
315 o2.indent(i2) << "{ /* struct ModRMDecision */" << "\n";
316 i2++;
317
318 o2.indent(i2) << stringForDecisionType(dt) << "," << "\n";
319 o2.indent(i2) << "modRMTable" << sTableNumber << "\n";
320
321 i2--;
322 o2.indent(i2) << "}";
323
324 ++sTableNumber;
325}
326
327void DisassemblerTables::emitOpcodeDecision(
328 raw_ostream &o1,
329 raw_ostream &o2,
330 uint32_t &i1,
331 uint32_t &i2,
332 OpcodeDecision &decision) const {
333 uint16_t index;
334
335 o2.indent(i2) << "{ /* struct OpcodeDecision */" << "\n";
336 i2++;
337 o2.indent(i2) << "{" << "\n";
338 i2++;
339
340 for (index = 0; index < 256; ++index) {
341 o2.indent(i2);
342
343 o2 << "/* 0x" << format("%02hhx", index) << " */" << "\n";
344
345 emitModRMDecision(o1, o2, i1, i2, decision.modRMDecisions[index]);
346
347 if (index < 255)
348 o2 << ",";
349
350 o2 << "\n";
351 }
352
353 i2--;
354 o2.indent(i2) << "}" << "\n";
355 i2--;
356 o2.indent(i2) << "}" << "\n";
357}
358
359void DisassemblerTables::emitContextDecision(
360 raw_ostream &o1,
361 raw_ostream &o2,
362 uint32_t &i1,
363 uint32_t &i2,
364 ContextDecision &decision,
365 const char* name) const {
366 o2.indent(i2) << "struct ContextDecision " << name << " = {" << "\n";
367 i2++;
368 o2.indent(i2) << "{ /* opcodeDecisions */" << "\n";
369 i2++;
370
371 unsigned index;
372
373 for (index = 0; index < IC_max; ++index) {
374 o2.indent(i2) << "/* ";
375 o2 << stringForContext((InstructionContext)index);
376 o2 << " */";
377 o2 << "\n";
378
379 emitOpcodeDecision(o1, o2, i1, i2, decision.opcodeDecisions[index]);
380
381 if (index + 1 < IC_max)
382 o2 << ", ";
383 }
384
385 i2--;
386 o2.indent(i2) << "}" << "\n";
387 i2--;
388 o2.indent(i2) << "};" << "\n";
389}
390
391void DisassemblerTables::emitInstructionInfo(raw_ostream &o, uint32_t &i)
392 const {
393 o.indent(i * 2) << "struct InstructionSpecifier ";
394 o << INSTRUCTIONS_STR << "[";
395 o << InstructionSpecifiers.size();
396 o << "] = {" << "\n";
397
398 i++;
399
400 uint16_t numInstructions = InstructionSpecifiers.size();
401 uint16_t index, operandIndex;
402
403 for (index = 0; index < numInstructions; ++index) {
404 o.indent(i * 2) << "{ /* " << index << " */" << "\n";
405 i++;
406
407 o.indent(i * 2) <<
408 stringForModifierType(InstructionSpecifiers[index].modifierType);
409 o << "," << "\n";
410
411 o.indent(i * 2) << "0x";
412 o << format("%02hhx", (uint16_t)InstructionSpecifiers[index].modifierBase);
413 o << "," << "\n";
414
415 o.indent(i * 2) << "{" << "\n";
416 i++;
417
418 for (operandIndex = 0; operandIndex < X86_MAX_OPERANDS; ++operandIndex) {
419 o.indent(i * 2) << "{ ";
420 o << stringForOperandEncoding(InstructionSpecifiers[index]
421 .operands[operandIndex]
422 .encoding);
423 o << ", ";
424 o << stringForOperandType(InstructionSpecifiers[index]
425 .operands[operandIndex]
426 .type);
427 o << " }";
428
429 if (operandIndex < X86_MAX_OPERANDS - 1)
430 o << ",";
431
432 o << "\n";
433 }
434
435 i--;
436 o.indent(i * 2) << "}," << "\n";
437
438 o.indent(i * 2) << "\"" << InstructionSpecifiers[index].name << "\"";
439 o << "\n";
440
441 i--;
442 o.indent(i * 2) << "}";
443
444 if (index + 1 < numInstructions)
445 o << ",";
446
447 o << "\n";
448 }
449
450 i--;
451 o.indent(i * 2) << "};" << "\n";
452}
453
454void DisassemblerTables::emitContextTable(raw_ostream &o, uint32_t &i) const {
455 uint16_t index;
456
457 o.indent(i * 2) << "InstructionContext ";
458 o << CONTEXTS_STR << "[256] = {" << "\n";
459 i++;
460
461 for (index = 0; index < 256; ++index) {
462 o.indent(i * 2);
463
464 if ((index & ATTR_64BIT) && (index & ATTR_REXW) && (index & ATTR_XS))
465 o << "IC_64BIT_REXW_XS";
466 else if ((index & ATTR_64BIT) && (index & ATTR_REXW) && (index & ATTR_XD))
467 o << "IC_64BIT_REXW_XD";
468 else if ((index & ATTR_64BIT) && (index & ATTR_REXW) &&
469 (index & ATTR_OPSIZE))
470 o << "IC_64BIT_REXW_OPSIZE";
471 else if ((index & ATTR_64BIT) && (index & ATTR_XS))
472 o << "IC_64BIT_XS";
473 else if ((index & ATTR_64BIT) && (index & ATTR_XD))
474 o << "IC_64BIT_XD";
475 else if ((index & ATTR_64BIT) && (index & ATTR_OPSIZE))
476 o << "IC_64BIT_OPSIZE";
477 else if ((index & ATTR_64BIT) && (index & ATTR_REXW))
478 o << "IC_64BIT_REXW";
479 else if ((index & ATTR_64BIT))
480 o << "IC_64BIT";
481 else if (index & ATTR_XS)
482 o << "IC_XS";
483 else if (index & ATTR_XD)
484 o << "IC_XD";
485 else if (index & ATTR_OPSIZE)
486 o << "IC_OPSIZE";
487 else
488 o << "IC";
489
490 if (index < 255)
491 o << ",";
492 else
493 o << " ";
494
495 o << " /* " << index << " */";
496
497 o << "\n";
498 }
499
500 i--;
501 o.indent(i * 2) << "};" << "\n";
502}
503
504void DisassemblerTables::emitContextDecisions(raw_ostream &o1,
505 raw_ostream &o2,
506 uint32_t &i1,
507 uint32_t &i2)
508 const {
509 emitContextDecision(o1, o2, i1, i2, *Tables[0], ONEBYTE_STR);
510 emitContextDecision(o1, o2, i1, i2, *Tables[1], TWOBYTE_STR);
511 emitContextDecision(o1, o2, i1, i2, *Tables[2], THREEBYTE38_STR);
512 emitContextDecision(o1, o2, i1, i2, *Tables[3], THREEBYTE3A_STR);
513}
514
515void DisassemblerTables::emit(raw_ostream &o) const {
516 uint32_t i1 = 0;
517 uint32_t i2 = 0;
518
519 std::string s1;
520 std::string s2;
521
522 raw_string_ostream o1(s1);
523 raw_string_ostream o2(s2);
524
525 emitInstructionInfo(o, i2);
526 o << "\n";
527
528 emitContextTable(o, i2);
529 o << "\n";
530
531 emitEmptyTable(o1, i1);
532 emitContextDecisions(o1, o2, i1, i2);
533
534 o << o1.str();
535 o << "\n";
536 o << o2.str();
537 o << "\n";
538 o << "\n";
539}
540
541void DisassemblerTables::setTableFields(ModRMDecision &decision,
542 const ModRMFilter &filter,
543 InstrUID uid,
544 uint8_t opcode) {
545 unsigned index;
546
547 for (index = 0; index < 256; ++index) {
548 if (filter.accepts(index)) {
549 if (decision.instructionIDs[index] == uid)
550 continue;
551
552 if (decision.instructionIDs[index] != 0) {
553 InstructionSpecifier &newInfo =
554 InstructionSpecifiers[uid];
555 InstructionSpecifier &previousInfo =
556 InstructionSpecifiers[decision.instructionIDs[index]];
557
558 if(newInfo.filtered)
559 continue; // filtered instructions get lowest priority
560
561 if(previousInfo.name == "NOOP")
562 continue; // special case for XCHG32ar and NOOP
563
564 if (outranks(previousInfo.insnContext, newInfo.insnContext))
565 continue;
566
567 if (previousInfo.insnContext == newInfo.insnContext &&
568 !previousInfo.filtered) {
569 errs() << "Error: Primary decode conflict: ";
570 errs() << newInfo.name << " would overwrite " << previousInfo.name;
571 errs() << "\n";
572 errs() << "ModRM " << index << "\n";
573 errs() << "Opcode " << (uint16_t)opcode << "\n";
574 errs() << "Context " << stringForContext(newInfo.insnContext) << "\n";
575 HasConflicts = true;
576 }
577 }
578
579 decision.instructionIDs[index] = uid;
580 }
581 }
582}
583
584void DisassemblerTables::setTableFields(OpcodeType type,
585 InstructionContext insnContext,
586 uint8_t opcode,
587 const ModRMFilter &filter,
588 InstrUID uid) {
589 unsigned index;
590
591 ContextDecision &decision = *Tables[type];
592
593 for (index = 0; index < IC_max; ++index) {
594 if (inheritsFrom((InstructionContext)index,
595 InstructionSpecifiers[uid].insnContext))
596 setTableFields(decision.opcodeDecisions[index].modRMDecisions[opcode],
597 filter,
598 uid,
599 opcode);
600 }
601}