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