Path: blob/main/contrib/llvm-project/llvm/utils/TableGen/DisassemblerEmitter.cpp
35258 views
//===- DisassemblerEmitter.cpp - Generate a disassembler ------------------===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//78#include "Common/CodeGenTarget.h"9#include "TableGenBackends.h"10#include "WebAssemblyDisassemblerEmitter.h"11#include "X86DisassemblerTables.h"12#include "X86RecognizableInstr.h"13#include "llvm/TableGen/Error.h"14#include "llvm/TableGen/Record.h"15#include "llvm/TableGen/TableGenBackend.h"1617using namespace llvm;18using namespace llvm::X86Disassembler;1920/// DisassemblerEmitter - Contains disassembler table emitters for various21/// architectures.2223/// X86 Disassembler Emitter24///25/// *** IF YOU'RE HERE TO RESOLVE A "Primary decode conflict", LOOK DOWN NEAR26/// THE END OF THIS COMMENT!27///28/// The X86 disassembler emitter is part of the X86 Disassembler, which is29/// documented in lib/Target/X86/X86Disassembler.h.30///31/// The emitter produces the tables that the disassembler uses to translate32/// instructions. The emitter generates the following tables:33///34/// - One table (CONTEXTS_SYM) that contains a mapping of attribute masks to35/// instruction contexts. Although for each attribute there are cases where36/// that attribute determines decoding, in the majority of cases decoding is37/// the same whether or not an attribute is present. For example, a 64-bit38/// instruction with an OPSIZE prefix and an XS prefix decodes the same way in39/// all cases as a 64-bit instruction with only OPSIZE set. (The XS prefix40/// may have effects on its execution, but does not change the instruction41/// returned.) This allows considerable space savings in other tables.42/// - Six tables (ONEBYTE_SYM, TWOBYTE_SYM, THREEBYTE38_SYM, THREEBYTE3A_SYM,43/// THREEBYTEA6_SYM, and THREEBYTEA7_SYM contain the hierarchy that the44/// decoder traverses while decoding an instruction. At the lowest level of45/// this hierarchy are instruction UIDs, 16-bit integers that can be used to46/// uniquely identify the instruction and correspond exactly to its position47/// in the list of CodeGenInstructions for the target.48/// - One table (INSTRUCTIONS_SYM) contains information about the operands of49/// each instruction and how to decode them.50///51/// During table generation, there may be conflicts between instructions that52/// occupy the same space in the decode tables. These conflicts are resolved as53/// follows in setTableFields() (X86DisassemblerTables.cpp)54///55/// - If the current context is the native context for one of the instructions56/// (that is, the attributes specified for it in the LLVM tables specify57/// precisely the current context), then it has priority.58/// - If the current context isn't native for either of the instructions, then59/// 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 table62/// emitter reports a conflict and dies.63///64/// *** RESOLUTION FOR "Primary decode conflict"S65///66/// If two instructions collide, typically the solution is (in order of67/// likelihood):68///69/// (1) to filter out one of the instructions by editing filter()70/// (X86RecognizableInstr.cpp). This is the most common resolution, but71/// check the Intel manuals first to make sure that (2) and (3) are not the72/// problem.73/// (2) to fix the tables (X86.td and its subsidiaries) so the opcodes are74/// accurate. Sometimes they are not.75/// (3) to fix the tables to reflect the actual context (for example, required76/// prefixes), and possibly to add a new context by editing77/// include/llvm/Support/X86DisassemblerDecoderCommon.h. This is unlikely78/// to be the cause.79///80/// DisassemblerEmitter.cpp contains the implementation for the emitter,81/// which simply pulls out instructions from the CodeGenTarget and pushes them82/// 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 instruction86/// tables.87/// X86ModRMFilters.h contains filters that can be used to determine which88/// ModR/M values are valid for a particular instruction. These are used to89/// populate ModRMDecisions.90/// X86RecognizableInstr.h contains the interface for a single instruction,91/// which knows how to translate itself from a CodeGenInstruction and provide92/// the information necessary for integration into the tables.93/// X86RecognizableInstr.cpp contains the implementation for a single94/// instruction.9596static void EmitDisassembler(RecordKeeper &Records, raw_ostream &OS) {97CodeGenTarget Target(Records);98emitSourceFileHeader(" * " + Target.getName().str() + " Disassembler", OS);99100// X86 uses a custom disassembler.101if (Target.getName() == "X86") {102DisassemblerTables Tables;103104ArrayRef<const CodeGenInstruction *> numberedInstructions =105Target.getInstructionsByEnumValue();106107for (unsigned i = 0, e = numberedInstructions.size(); i != e; ++i)108RecognizableInstr::processInstr(Tables, *numberedInstructions[i], i);109110if (Tables.hasConflicts()) {111PrintError(Target.getTargetRecord()->getLoc(), "Primary decode conflict");112return;113}114115Tables.emit(OS);116return;117}118119// WebAssembly has variable length opcodes, so can't use EmitFixedLenDecoder120// below (which depends on a Size table-gen Record), and also uses a custom121// disassembler.122if (Target.getName() == "WebAssembly") {123emitWebAssemblyDisassemblerTables(OS, Target.getInstructionsByEnumValue());124return;125}126127std::string PredicateNamespace = std::string(Target.getName());128if (PredicateNamespace == "Thumb")129PredicateNamespace = "ARM";130EmitDecoder(Records, OS, PredicateNamespace);131}132133cl::OptionCategory DisassemblerEmitterCat("Options for -gen-disassembler");134135static TableGen::Emitter::Opt X("gen-disassembler", EmitDisassembler,136"Generate disassembler");137138139