1f22ef01cSRoman Divacky //===- DisassemblerEmitter.cpp - Generate a disassembler ------------------===//
2f22ef01cSRoman Divacky //
3f22ef01cSRoman Divacky //                     The LLVM Compiler Infrastructure
4f22ef01cSRoman Divacky //
5f22ef01cSRoman Divacky // This file is distributed under the University of Illinois Open Source
6f22ef01cSRoman Divacky // License. See LICENSE.TXT for details.
7f22ef01cSRoman Divacky //
8f22ef01cSRoman Divacky //===----------------------------------------------------------------------===//
9f22ef01cSRoman Divacky 
10f22ef01cSRoman Divacky #include "CodeGenTarget.h"
11*4ba319b5SDimitry Andric #include "WebAssemblyDisassemblerEmitter.h"
12f22ef01cSRoman Divacky #include "X86DisassemblerTables.h"
13f22ef01cSRoman Divacky #include "X86RecognizableInstr.h"
146122f3e6SDimitry Andric #include "llvm/TableGen/Error.h"
156122f3e6SDimitry Andric #include "llvm/TableGen/Record.h"
167ae0e2c9SDimitry Andric #include "llvm/TableGen/TableGenBackend.h"
17f22ef01cSRoman Divacky 
18f22ef01cSRoman Divacky using namespace llvm;
19f22ef01cSRoman Divacky using namespace llvm::X86Disassembler;
20f22ef01cSRoman Divacky 
21f22ef01cSRoman Divacky /// DisassemblerEmitter - Contains disassembler table emitters for various
22f22ef01cSRoman Divacky /// architectures.
23f22ef01cSRoman Divacky 
24f22ef01cSRoman Divacky /// X86 Disassembler Emitter
25f22ef01cSRoman Divacky ///
26f22ef01cSRoman Divacky /// *** IF YOU'RE HERE TO RESOLVE A "Primary decode conflict", LOOK DOWN NEAR
27f22ef01cSRoman Divacky ///     THE END OF THIS COMMENT!
28f22ef01cSRoman Divacky ///
29f22ef01cSRoman Divacky /// The X86 disassembler emitter is part of the X86 Disassembler, which is
30f22ef01cSRoman Divacky /// documented in lib/Target/X86/X86Disassembler.h.
31f22ef01cSRoman Divacky ///
32f22ef01cSRoman Divacky /// The emitter produces the tables that the disassembler uses to translate
33f22ef01cSRoman Divacky /// instructions.  The emitter generates the following tables:
34f22ef01cSRoman Divacky ///
35f22ef01cSRoman Divacky /// - One table (CONTEXTS_SYM) that contains a mapping of attribute masks to
36f22ef01cSRoman Divacky ///   instruction contexts.  Although for each attribute there are cases where
37f22ef01cSRoman Divacky ///   that attribute determines decoding, in the majority of cases decoding is
38f22ef01cSRoman Divacky ///   the same whether or not an attribute is present.  For example, a 64-bit
39f22ef01cSRoman Divacky ///   instruction with an OPSIZE prefix and an XS prefix decodes the same way in
40f22ef01cSRoman Divacky ///   all cases as a 64-bit instruction with only OPSIZE set.  (The XS prefix
41f22ef01cSRoman Divacky ///   may have effects on its execution, but does not change the instruction
42f22ef01cSRoman Divacky ///   returned.)  This allows considerable space savings in other tables.
433b0f4066SDimitry Andric /// - Six tables (ONEBYTE_SYM, TWOBYTE_SYM, THREEBYTE38_SYM, THREEBYTE3A_SYM,
443b0f4066SDimitry Andric ///   THREEBYTEA6_SYM, and THREEBYTEA7_SYM contain the hierarchy that the
453b0f4066SDimitry Andric ///   decoder traverses while decoding an instruction.  At the lowest level of
463b0f4066SDimitry Andric ///   this hierarchy are instruction UIDs, 16-bit integers that can be used to
473b0f4066SDimitry Andric ///   uniquely identify the instruction and correspond exactly to its position
483b0f4066SDimitry Andric ///   in the list of CodeGenInstructions for the target.
49f22ef01cSRoman Divacky /// - One table (INSTRUCTIONS_SYM) contains information about the operands of
50f22ef01cSRoman Divacky ///   each instruction and how to decode them.
51f22ef01cSRoman Divacky ///
52f22ef01cSRoman Divacky /// During table generation, there may be conflicts between instructions that
53f22ef01cSRoman Divacky /// occupy the same space in the decode tables.  These conflicts are resolved as
54f22ef01cSRoman Divacky /// follows in setTableFields() (X86DisassemblerTables.cpp)
55f22ef01cSRoman Divacky ///
56f22ef01cSRoman Divacky /// - If the current context is the native context for one of the instructions
57f22ef01cSRoman Divacky ///   (that is, the attributes specified for it in the LLVM tables specify
58f22ef01cSRoman Divacky ///   precisely the current context), then it has priority.
59f22ef01cSRoman Divacky /// - If the current context isn't native for either of the instructions, then
60f22ef01cSRoman Divacky ///   the higher-priority context wins (that is, the one that is more specific).
61f22ef01cSRoman Divacky ///   That hierarchy is determined by outranks() (X86DisassemblerTables.cpp)
62f22ef01cSRoman Divacky /// - If the current context is native for both instructions, then the table
63f22ef01cSRoman Divacky ///   emitter reports a conflict and dies.
64f22ef01cSRoman Divacky ///
65f22ef01cSRoman Divacky /// *** RESOLUTION FOR "Primary decode conflict"S
66f22ef01cSRoman Divacky ///
67f22ef01cSRoman Divacky /// If two instructions collide, typically the solution is (in order of
68f22ef01cSRoman Divacky /// likelihood):
69f22ef01cSRoman Divacky ///
70f22ef01cSRoman Divacky /// (1) to filter out one of the instructions by editing filter()
71f22ef01cSRoman Divacky ///     (X86RecognizableInstr.cpp).  This is the most common resolution, but
72f22ef01cSRoman Divacky ///     check the Intel manuals first to make sure that (2) and (3) are not the
73f22ef01cSRoman Divacky ///     problem.
74f22ef01cSRoman Divacky /// (2) to fix the tables (X86.td and its subsidiaries) so the opcodes are
75f22ef01cSRoman Divacky ///     accurate.  Sometimes they are not.
76f22ef01cSRoman Divacky /// (3) to fix the tables to reflect the actual context (for example, required
77f22ef01cSRoman Divacky ///     prefixes), and possibly to add a new context by editing
78*4ba319b5SDimitry Andric ///     include/llvm/Support/X86DisassemblerDecoderCommon.h.  This is unlikely
79*4ba319b5SDimitry Andric ///     to be the cause.
80f22ef01cSRoman Divacky ///
81f22ef01cSRoman Divacky /// DisassemblerEmitter.cpp contains the implementation for the emitter,
82f22ef01cSRoman Divacky ///   which simply pulls out instructions from the CodeGenTarget and pushes them
83f22ef01cSRoman Divacky ///   into X86DisassemblerTables.
84f22ef01cSRoman Divacky /// X86DisassemblerTables.h contains the interface for the instruction tables,
85f22ef01cSRoman Divacky ///   which manage and emit the structures discussed above.
86f22ef01cSRoman Divacky /// X86DisassemblerTables.cpp contains the implementation for the instruction
87f22ef01cSRoman Divacky ///   tables.
88f22ef01cSRoman Divacky /// X86ModRMFilters.h contains filters that can be used to determine which
89f22ef01cSRoman Divacky ///   ModR/M values are valid for a particular instruction.  These are used to
90f22ef01cSRoman Divacky ///   populate ModRMDecisions.
91f22ef01cSRoman Divacky /// X86RecognizableInstr.h contains the interface for a single instruction,
92f22ef01cSRoman Divacky ///   which knows how to translate itself from a CodeGenInstruction and provide
93f22ef01cSRoman Divacky ///   the information necessary for integration into the tables.
94f22ef01cSRoman Divacky /// X86RecognizableInstr.cpp contains the implementation for a single
95f22ef01cSRoman Divacky ///   instruction.
96f22ef01cSRoman Divacky 
977ae0e2c9SDimitry Andric namespace llvm {
98f22ef01cSRoman Divacky 
997ae0e2c9SDimitry Andric extern void EmitFixedLenDecoder(RecordKeeper &RK, raw_ostream &OS,
1003ca95b02SDimitry Andric                                 const std::string &PredicateNamespace,
1013ca95b02SDimitry Andric                                 const std::string &GPrefix,
1023ca95b02SDimitry Andric                                 const std::string &GPostfix,
1033ca95b02SDimitry Andric                                 const std::string &ROK,
1043ca95b02SDimitry Andric                                 const std::string &RFail, const std::string &L);
1057ae0e2c9SDimitry Andric 
EmitDisassembler(RecordKeeper & Records,raw_ostream & OS)1067ae0e2c9SDimitry Andric void EmitDisassembler(RecordKeeper &Records, raw_ostream &OS) {
1077ae0e2c9SDimitry Andric   CodeGenTarget Target(Records);
108d88c1a5aSDimitry Andric   emitSourceFileHeader(" * " + Target.getName().str() + " Disassembler", OS);
109f22ef01cSRoman Divacky 
110f22ef01cSRoman Divacky   // X86 uses a custom disassembler.
111f22ef01cSRoman Divacky   if (Target.getName() == "X86") {
112f22ef01cSRoman Divacky     DisassemblerTables Tables;
113f22ef01cSRoman Divacky 
1143ca95b02SDimitry Andric     ArrayRef<const CodeGenInstruction*> numberedInstructions =
115f22ef01cSRoman Divacky       Target.getInstructionsByEnumValue();
116f22ef01cSRoman Divacky 
117f22ef01cSRoman Divacky     for (unsigned i = 0, e = numberedInstructions.size(); i != e; ++i)
118f22ef01cSRoman Divacky       RecognizableInstr::processInstr(Tables, *numberedInstructions[i], i);
119f22ef01cSRoman Divacky 
12091bc56edSDimitry Andric     if (Tables.hasConflicts()) {
12191bc56edSDimitry Andric       PrintError(Target.getTargetRecord()->getLoc(), "Primary decode conflict");
12291bc56edSDimitry Andric       return;
12391bc56edSDimitry Andric     }
124f22ef01cSRoman Divacky 
125f22ef01cSRoman Divacky     Tables.emit(OS);
126f22ef01cSRoman Divacky     return;
127f22ef01cSRoman Divacky   }
128f22ef01cSRoman Divacky 
129*4ba319b5SDimitry Andric   // WebAssembly has variable length opcodes, so can't use EmitFixedLenDecoder
130*4ba319b5SDimitry Andric   // below (which depends on a Size table-gen Record), and also uses a custom
131*4ba319b5SDimitry Andric   // disassembler.
132*4ba319b5SDimitry Andric   if (Target.getName() == "WebAssembly") {
133*4ba319b5SDimitry Andric     emitWebAssemblyDisassemblerTables(OS, Target.getInstructionsByEnumValue());
134*4ba319b5SDimitry Andric     return;
135*4ba319b5SDimitry Andric   }
136*4ba319b5SDimitry Andric 
1376122f3e6SDimitry Andric   // ARM and Thumb have a CHECK() macro to deal with DecodeStatuses.
13891bc56edSDimitry Andric   if (Target.getName() == "ARM" || Target.getName() == "Thumb" ||
13991bc56edSDimitry Andric       Target.getName() == "AArch64" || Target.getName() == "ARM64") {
14091bc56edSDimitry Andric     std::string PredicateNamespace = Target.getName();
14191bc56edSDimitry Andric     if (PredicateNamespace == "Thumb")
14291bc56edSDimitry Andric       PredicateNamespace = "ARM";
14391bc56edSDimitry Andric 
14491bc56edSDimitry Andric     EmitFixedLenDecoder(Records, OS, PredicateNamespace,
1457d523365SDimitry Andric                         "if (!Check(S, ", "))",
1466122f3e6SDimitry Andric                         "S", "MCDisassembler::Fail",
1477ae0e2c9SDimitry Andric                         "  MCDisassembler::DecodeStatus S = "
1487ae0e2c9SDimitry Andric                           "MCDisassembler::Success;\n(void)S;");
149f22ef01cSRoman Divacky     return;
150f22ef01cSRoman Divacky   }
151f22ef01cSRoman Divacky 
1527ae0e2c9SDimitry Andric   EmitFixedLenDecoder(Records, OS, Target.getName(),
1537d523365SDimitry Andric                       "if (", " == MCDisassembler::Fail)",
1547ae0e2c9SDimitry Andric                       "MCDisassembler::Success", "MCDisassembler::Fail", "");
155f22ef01cSRoman Divacky }
1567ae0e2c9SDimitry Andric 
1577ae0e2c9SDimitry Andric } // End llvm namespace
158