blob: 6e1d8dde981cc0622f5ae227c53cb33fb80c566d [file] [log] [blame]
Daniel Dunbare5024332009-11-25 02:13:23 +00001//===- DisassemblerEmitter.cpp - Generate a disassembler ------------------===//
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
Daniel Dunbare5024332009-11-25 02:13:23 +000010#include "CodeGenTarget.h"
Sean Callanan04cc3072009-12-19 02:59:52 +000011#include "X86DisassemblerTables.h"
12#include "X86RecognizableInstr.h"
Peter Collingbourne84c287e2011-10-01 16:41:13 +000013#include "llvm/TableGen/Error.h"
14#include "llvm/TableGen/Record.h"
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +000015#include "llvm/TableGen/TableGenBackend.h"
Johnny Chen7b999ea2010-04-02 22:27:38 +000016
Daniel Dunbare5024332009-11-25 02:13:23 +000017using namespace llvm;
Sean Callanan04cc3072009-12-19 02:59:52 +000018using namespace llvm::X86Disassembler;
19
20/// DisassemblerEmitter - Contains disassembler table emitters for various
21/// architectures.
22
23/// X86 Disassembler Emitter
24///
25/// *** IF YOU'RE HERE TO RESOLVE A "Primary decode conflict", LOOK DOWN NEAR
26/// THE END OF THIS COMMENT!
27///
28/// The X86 disassembler emitter is part of the X86 Disassembler, which is
29/// documented in lib/Target/X86/X86Disassembler.h.
30///
31/// The emitter produces the tables that the disassembler uses to translate
32/// instructions. The emitter generates the following tables:
33///
34/// - One table (CONTEXTS_SYM) that contains a mapping of attribute masks to
35/// instruction contexts. Although for each attribute there are cases where
36/// that attribute determines decoding, in the majority of cases decoding is
37/// the same whether or not an attribute is present. For example, a 64-bit
38/// instruction with an OPSIZE prefix and an XS prefix decodes the same way in
39/// all cases as a 64-bit instruction with only OPSIZE set. (The XS prefix
40/// may have effects on its execution, but does not change the instruction
41/// returned.) This allows considerable space savings in other tables.
Joerg Sonnenbergerfc4789d2011-04-04 16:58:13 +000042/// - Six tables (ONEBYTE_SYM, TWOBYTE_SYM, THREEBYTE38_SYM, THREEBYTE3A_SYM,
43/// THREEBYTEA6_SYM, and THREEBYTEA7_SYM contain the hierarchy that the
44/// decoder traverses while decoding an instruction. At the lowest level of
45/// this hierarchy are instruction UIDs, 16-bit integers that can be used to
46/// uniquely identify the instruction and correspond exactly to its position
47/// in the list of CodeGenInstructions for the target.
Sean Callanan04cc3072009-12-19 02:59:52 +000048/// - One table (INSTRUCTIONS_SYM) contains information about the operands of
49/// each instruction and how to decode them.
50///
51/// During table generation, there may be conflicts between instructions that
52/// occupy the same space in the decode tables. These conflicts are resolved as
53/// follows in setTableFields() (X86DisassemblerTables.cpp)
54///
55/// - If the current context is the native context for one of the instructions
56/// (that is, the attributes specified for it in the LLVM tables specify
57/// precisely the current context), then it has priority.
58/// - If the current context isn't native for either of the instructions, then
59/// the higher-priority context wins (that is, the one that is more specific).
60/// That hierarchy is determined by outranks() (X86DisassemblerTables.cpp)
61/// - If the current context is native for both instructions, then the table
62/// emitter reports a conflict and dies.
63///
64/// *** RESOLUTION FOR "Primary decode conflict"S
65///
66/// If two instructions collide, typically the solution is (in order of
67/// likelihood):
68///
69/// (1) to filter out one of the instructions by editing filter()
70/// (X86RecognizableInstr.cpp). This is the most common resolution, but
71/// check the Intel manuals first to make sure that (2) and (3) are not the
72/// problem.
73/// (2) to fix the tables (X86.td and its subsidiaries) so the opcodes are
74/// accurate. Sometimes they are not.
75/// (3) to fix the tables to reflect the actual context (for example, required
76/// prefixes), and possibly to add a new context by editing
77/// lib/Target/X86/X86DisassemblerDecoderCommon.h. This is unlikely to be
78/// the cause.
79///
80/// DisassemblerEmitter.cpp contains the implementation for the emitter,
81/// which simply pulls out instructions from the CodeGenTarget and pushes them
82/// into X86DisassemblerTables.
83/// X86DisassemblerTables.h contains the interface for the instruction tables,
84/// which manage and emit the structures discussed above.
85/// X86DisassemblerTables.cpp contains the implementation for the instruction
86/// tables.
87/// X86ModRMFilters.h contains filters that can be used to determine which
88/// ModR/M values are valid for a particular instruction. These are used to
89/// populate ModRMDecisions.
90/// X86RecognizableInstr.h contains the interface for a single instruction,
91/// which knows how to translate itself from a CodeGenInstruction and provide
92/// the information necessary for integration into the tables.
93/// X86RecognizableInstr.cpp contains the implementation for a single
94/// instruction.
Daniel Dunbare5024332009-11-25 02:13:23 +000095
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +000096namespace llvm {
Daniel Dunbare5024332009-11-25 02:13:23 +000097
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +000098extern void EmitFixedLenDecoder(RecordKeeper &RK, raw_ostream &OS,
Benjamin Kramerc321e532016-06-08 19:09:22 +000099 const std::string &PredicateNamespace,
100 const std::string &GPrefix,
101 const std::string &GPostfix,
102 const std::string &ROK,
103 const std::string &RFail, const std::string &L);
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +0000104
105void EmitDisassembler(RecordKeeper &Records, raw_ostream &OS) {
106 CodeGenTarget Target(Records);
Matthias Braun4a86d452016-12-04 05:48:16 +0000107 emitSourceFileHeader(" * " + Target.getName().str() + " Disassembler", OS);
Daniel Dunbare5024332009-11-25 02:13:23 +0000108
Sean Callanan04cc3072009-12-19 02:59:52 +0000109 // X86 uses a custom disassembler.
110 if (Target.getName() == "X86") {
111 DisassemblerTables Tables;
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +0000112
Craig Topper28851b62016-02-01 01:33:42 +0000113 ArrayRef<const CodeGenInstruction*> numberedInstructions =
Chris Lattner918be522010-03-19 00:34:35 +0000114 Target.getInstructionsByEnumValue();
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +0000115
Sean Callanan04cc3072009-12-19 02:59:52 +0000116 for (unsigned i = 0, e = numberedInstructions.size(); i != e; ++i)
117 RecognizableInstr::processInstr(Tables, *numberedInstructions[i], i);
118
Craig Topper7d4e01c2014-01-05 01:34:12 +0000119 if (Tables.hasConflicts()) {
120 PrintError(Target.getTargetRecord()->getLoc(), "Primary decode conflict");
121 return;
122 }
Sean Callanan04cc3072009-12-19 02:59:52 +0000123
124 Tables.emit(OS);
125 return;
126 }
127
Owen Andersona4043c42011-08-17 17:44:15 +0000128 // ARM and Thumb have a CHECK() macro to deal with DecodeStatuses.
Tim Northover618850b2014-05-06 14:15:14 +0000129 if (Target.getName() == "ARM" || Target.getName() == "Thumb" ||
130 Target.getName() == "AArch64" || Target.getName() == "ARM64") {
131 std::string PredicateNamespace = Target.getName();
132 if (PredicateNamespace == "Thumb")
133 PredicateNamespace = "ARM";
134
135 EmitFixedLenDecoder(Records, OS, PredicateNamespace,
Petr Pavlu182b0572015-07-15 08:04:27 +0000136 "if (!Check(S, ", "))",
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +0000137 "S", "MCDisassembler::Fail",
138 " MCDisassembler::DecodeStatus S = "
139 "MCDisassembler::Success;\n(void)S;");
Owen Andersona4043c42011-08-17 17:44:15 +0000140 return;
141 }
142
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +0000143 EmitFixedLenDecoder(Records, OS, Target.getName(),
Petr Pavlu182b0572015-07-15 08:04:27 +0000144 "if (", " == MCDisassembler::Fail)",
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +0000145 "MCDisassembler::Success", "MCDisassembler::Fail", "");
Daniel Dunbare5024332009-11-25 02:13:23 +0000146}
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +0000147
148} // End llvm namespace