106f9d401SRoman Divacky //===- DisassemblerEmitter.cpp - Generate a disassembler ------------------===//
206f9d401SRoman Divacky //
3e6d15924SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4e6d15924SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
5e6d15924SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
606f9d401SRoman Divacky //
706f9d401SRoman Divacky //===----------------------------------------------------------------------===//
806f9d401SRoman Divacky
9ac9a064cSDimitry Andric #include "Common/CodeGenTarget.h"
107fa27ce4SDimitry Andric #include "TableGenBackends.h"
11eb11fae6SDimitry Andric #include "WebAssemblyDisassemblerEmitter.h"
121e7804dbSRoman Divacky #include "X86DisassemblerTables.h"
131e7804dbSRoman Divacky #include "X86RecognizableInstr.h"
1430815c53SDimitry Andric #include "llvm/TableGen/Error.h"
1530815c53SDimitry Andric #include "llvm/TableGen/Record.h"
1658b69754SDimitry Andric #include "llvm/TableGen/TableGenBackend.h"
17b5efedafSRoman Divacky
1806f9d401SRoman Divacky using namespace llvm;
191e7804dbSRoman Divacky using namespace llvm::X86Disassembler;
201e7804dbSRoman Divacky
211e7804dbSRoman Divacky /// DisassemblerEmitter - Contains disassembler table emitters for various
221e7804dbSRoman Divacky /// architectures.
231e7804dbSRoman Divacky
241e7804dbSRoman Divacky /// X86 Disassembler Emitter
251e7804dbSRoman Divacky ///
261e7804dbSRoman Divacky /// *** IF YOU'RE HERE TO RESOLVE A "Primary decode conflict", LOOK DOWN NEAR
271e7804dbSRoman Divacky /// THE END OF THIS COMMENT!
281e7804dbSRoman Divacky ///
291e7804dbSRoman Divacky /// The X86 disassembler emitter is part of the X86 Disassembler, which is
301e7804dbSRoman Divacky /// documented in lib/Target/X86/X86Disassembler.h.
311e7804dbSRoman Divacky ///
321e7804dbSRoman Divacky /// The emitter produces the tables that the disassembler uses to translate
331e7804dbSRoman Divacky /// instructions. The emitter generates the following tables:
341e7804dbSRoman Divacky ///
351e7804dbSRoman Divacky /// - One table (CONTEXTS_SYM) that contains a mapping of attribute masks to
361e7804dbSRoman Divacky /// instruction contexts. Although for each attribute there are cases where
371e7804dbSRoman Divacky /// that attribute determines decoding, in the majority of cases decoding is
381e7804dbSRoman Divacky /// the same whether or not an attribute is present. For example, a 64-bit
391e7804dbSRoman Divacky /// instruction with an OPSIZE prefix and an XS prefix decodes the same way in
401e7804dbSRoman Divacky /// all cases as a 64-bit instruction with only OPSIZE set. (The XS prefix
411e7804dbSRoman Divacky /// may have effects on its execution, but does not change the instruction
421e7804dbSRoman Divacky /// returned.) This allows considerable space savings in other tables.
436b943ff3SDimitry Andric /// - Six tables (ONEBYTE_SYM, TWOBYTE_SYM, THREEBYTE38_SYM, THREEBYTE3A_SYM,
446b943ff3SDimitry Andric /// THREEBYTEA6_SYM, and THREEBYTEA7_SYM contain the hierarchy that the
456b943ff3SDimitry Andric /// decoder traverses while decoding an instruction. At the lowest level of
466b943ff3SDimitry Andric /// this hierarchy are instruction UIDs, 16-bit integers that can be used to
476b943ff3SDimitry Andric /// uniquely identify the instruction and correspond exactly to its position
486b943ff3SDimitry Andric /// in the list of CodeGenInstructions for the target.
491e7804dbSRoman Divacky /// - One table (INSTRUCTIONS_SYM) contains information about the operands of
501e7804dbSRoman Divacky /// each instruction and how to decode them.
511e7804dbSRoman Divacky ///
521e7804dbSRoman Divacky /// During table generation, there may be conflicts between instructions that
531e7804dbSRoman Divacky /// occupy the same space in the decode tables. These conflicts are resolved as
541e7804dbSRoman Divacky /// follows in setTableFields() (X86DisassemblerTables.cpp)
551e7804dbSRoman Divacky ///
561e7804dbSRoman Divacky /// - If the current context is the native context for one of the instructions
571e7804dbSRoman Divacky /// (that is, the attributes specified for it in the LLVM tables specify
581e7804dbSRoman Divacky /// precisely the current context), then it has priority.
591e7804dbSRoman Divacky /// - If the current context isn't native for either of the instructions, then
601e7804dbSRoman Divacky /// the higher-priority context wins (that is, the one that is more specific).
611e7804dbSRoman Divacky /// That hierarchy is determined by outranks() (X86DisassemblerTables.cpp)
621e7804dbSRoman Divacky /// - If the current context is native for both instructions, then the table
631e7804dbSRoman Divacky /// emitter reports a conflict and dies.
641e7804dbSRoman Divacky ///
651e7804dbSRoman Divacky /// *** RESOLUTION FOR "Primary decode conflict"S
661e7804dbSRoman Divacky ///
671e7804dbSRoman Divacky /// If two instructions collide, typically the solution is (in order of
681e7804dbSRoman Divacky /// likelihood):
691e7804dbSRoman Divacky ///
701e7804dbSRoman Divacky /// (1) to filter out one of the instructions by editing filter()
711e7804dbSRoman Divacky /// (X86RecognizableInstr.cpp). This is the most common resolution, but
721e7804dbSRoman Divacky /// check the Intel manuals first to make sure that (2) and (3) are not the
731e7804dbSRoman Divacky /// problem.
741e7804dbSRoman Divacky /// (2) to fix the tables (X86.td and its subsidiaries) so the opcodes are
751e7804dbSRoman Divacky /// accurate. Sometimes they are not.
761e7804dbSRoman Divacky /// (3) to fix the tables to reflect the actual context (for example, required
771e7804dbSRoman Divacky /// prefixes), and possibly to add a new context by editing
78eb11fae6SDimitry Andric /// include/llvm/Support/X86DisassemblerDecoderCommon.h. This is unlikely
79eb11fae6SDimitry Andric /// to be the cause.
801e7804dbSRoman Divacky ///
811e7804dbSRoman Divacky /// DisassemblerEmitter.cpp contains the implementation for the emitter,
821e7804dbSRoman Divacky /// which simply pulls out instructions from the CodeGenTarget and pushes them
831e7804dbSRoman Divacky /// into X86DisassemblerTables.
841e7804dbSRoman Divacky /// X86DisassemblerTables.h contains the interface for the instruction tables,
851e7804dbSRoman Divacky /// which manage and emit the structures discussed above.
861e7804dbSRoman Divacky /// X86DisassemblerTables.cpp contains the implementation for the instruction
871e7804dbSRoman Divacky /// tables.
881e7804dbSRoman Divacky /// X86ModRMFilters.h contains filters that can be used to determine which
891e7804dbSRoman Divacky /// ModR/M values are valid for a particular instruction. These are used to
901e7804dbSRoman Divacky /// populate ModRMDecisions.
911e7804dbSRoman Divacky /// X86RecognizableInstr.h contains the interface for a single instruction,
921e7804dbSRoman Divacky /// which knows how to translate itself from a CodeGenInstruction and provide
931e7804dbSRoman Divacky /// the information necessary for integration into the tables.
941e7804dbSRoman Divacky /// X86RecognizableInstr.cpp contains the implementation for a single
951e7804dbSRoman Divacky /// instruction.
9606f9d401SRoman Divacky
EmitDisassembler(RecordKeeper & Records,raw_ostream & OS)977fa27ce4SDimitry Andric static void EmitDisassembler(RecordKeeper &Records, raw_ostream &OS) {
9858b69754SDimitry Andric CodeGenTarget Target(Records);
99b915e9e0SDimitry Andric emitSourceFileHeader(" * " + Target.getName().str() + " Disassembler", OS);
10006f9d401SRoman Divacky
1011e7804dbSRoman Divacky // X86 uses a custom disassembler.
1021e7804dbSRoman Divacky if (Target.getName() == "X86") {
1031e7804dbSRoman Divacky DisassemblerTables Tables;
1041e7804dbSRoman Divacky
10501095a5dSDimitry Andric ArrayRef<const CodeGenInstruction *> numberedInstructions =
1062f12f10aSRoman Divacky Target.getInstructionsByEnumValue();
1071e7804dbSRoman Divacky
1081e7804dbSRoman Divacky for (unsigned i = 0, e = numberedInstructions.size(); i != e; ++i)
1091e7804dbSRoman Divacky RecognizableInstr::processInstr(Tables, *numberedInstructions[i], i);
1101e7804dbSRoman Divacky
1115ca98fd9SDimitry Andric if (Tables.hasConflicts()) {
1125ca98fd9SDimitry Andric PrintError(Target.getTargetRecord()->getLoc(), "Primary decode conflict");
1135ca98fd9SDimitry Andric return;
1145ca98fd9SDimitry Andric }
1151e7804dbSRoman Divacky
1161e7804dbSRoman Divacky Tables.emit(OS);
1171e7804dbSRoman Divacky return;
1181e7804dbSRoman Divacky }
1191e7804dbSRoman Divacky
120eb11fae6SDimitry Andric // WebAssembly has variable length opcodes, so can't use EmitFixedLenDecoder
121eb11fae6SDimitry Andric // below (which depends on a Size table-gen Record), and also uses a custom
122eb11fae6SDimitry Andric // disassembler.
123eb11fae6SDimitry Andric if (Target.getName() == "WebAssembly") {
124eb11fae6SDimitry Andric emitWebAssemblyDisassemblerTables(OS, Target.getInstructionsByEnumValue());
125eb11fae6SDimitry Andric return;
126eb11fae6SDimitry Andric }
127eb11fae6SDimitry Andric
128cfca06d7SDimitry Andric std::string PredicateNamespace = std::string(Target.getName());
1295ca98fd9SDimitry Andric if (PredicateNamespace == "Thumb")
1305ca98fd9SDimitry Andric PredicateNamespace = "ARM";
131e3b55780SDimitry Andric EmitDecoder(Records, OS, PredicateNamespace);
13206f9d401SRoman Divacky }
13358b69754SDimitry Andric
134ac9a064cSDimitry Andric cl::OptionCategory DisassemblerEmitterCat("Options for -gen-disassembler");
135ac9a064cSDimitry Andric
1367fa27ce4SDimitry Andric static TableGen::Emitter::Opt X("gen-disassembler", EmitDisassembler,
1377fa27ce4SDimitry Andric "Generate disassembler");
138