Daniel Dunbar | 4058874 | 2009-11-25 02:13:23 +0000 | [diff] [blame] | 1 | //===- 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 Dunbar | 4058874 | 2009-11-25 02:13:23 +0000 | [diff] [blame] | 10 | #include "CodeGenTarget.h" |
Sam Clegg | 04cf0d7 | 2018-05-10 22:16:44 +0000 | [diff] [blame] | 11 | #include "WebAssemblyDisassemblerEmitter.h" |
Sean Callanan | 8ed9f51 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 12 | #include "X86DisassemblerTables.h" |
| 13 | #include "X86RecognizableInstr.h" |
Peter Collingbourne | 7c78888 | 2011-10-01 16:41:13 +0000 | [diff] [blame] | 14 | #include "llvm/TableGen/Error.h" |
| 15 | #include "llvm/TableGen/Record.h" |
Jakob Stoklund Olesen | 6f36fa9 | 2012-06-11 15:37:55 +0000 | [diff] [blame] | 16 | #include "llvm/TableGen/TableGenBackend.h" |
Johnny Chen | b68a3ee | 2010-04-02 22:27:38 +0000 | [diff] [blame] | 17 | |
Daniel Dunbar | 4058874 | 2009-11-25 02:13:23 +0000 | [diff] [blame] | 18 | using namespace llvm; |
Sean Callanan | 8ed9f51 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 19 | using namespace llvm::X86Disassembler; |
| 20 | |
| 21 | /// DisassemblerEmitter - Contains disassembler table emitters for various |
| 22 | /// architectures. |
| 23 | |
| 24 | /// X86 Disassembler Emitter |
| 25 | /// |
| 26 | /// *** IF YOU'RE HERE TO RESOLVE A "Primary decode conflict", LOOK DOWN NEAR |
| 27 | /// THE END OF THIS COMMENT! |
| 28 | /// |
| 29 | /// The X86 disassembler emitter is part of the X86 Disassembler, which is |
| 30 | /// documented in lib/Target/X86/X86Disassembler.h. |
| 31 | /// |
| 32 | /// The emitter produces the tables that the disassembler uses to translate |
| 33 | /// instructions. The emitter generates the following tables: |
| 34 | /// |
| 35 | /// - One table (CONTEXTS_SYM) that contains a mapping of attribute masks to |
| 36 | /// instruction contexts. Although for each attribute there are cases where |
| 37 | /// that attribute determines decoding, in the majority of cases decoding is |
| 38 | /// the same whether or not an attribute is present. For example, a 64-bit |
| 39 | /// instruction with an OPSIZE prefix and an XS prefix decodes the same way in |
| 40 | /// all cases as a 64-bit instruction with only OPSIZE set. (The XS prefix |
| 41 | /// may have effects on its execution, but does not change the instruction |
| 42 | /// returned.) This allows considerable space savings in other tables. |
Joerg Sonnenberger | 4a8ac8d | 2011-04-04 16:58:13 +0000 | [diff] [blame] | 43 | /// - Six tables (ONEBYTE_SYM, TWOBYTE_SYM, THREEBYTE38_SYM, THREEBYTE3A_SYM, |
| 44 | /// THREEBYTEA6_SYM, and THREEBYTEA7_SYM contain the hierarchy that the |
| 45 | /// decoder traverses while decoding an instruction. At the lowest level of |
| 46 | /// this hierarchy are instruction UIDs, 16-bit integers that can be used to |
| 47 | /// uniquely identify the instruction and correspond exactly to its position |
| 48 | /// in the list of CodeGenInstructions for the target. |
Sean Callanan | 8ed9f51 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 49 | /// - One table (INSTRUCTIONS_SYM) contains information about the operands of |
| 50 | /// each instruction and how to decode them. |
| 51 | /// |
| 52 | /// During table generation, there may be conflicts between instructions that |
| 53 | /// occupy the same space in the decode tables. These conflicts are resolved as |
| 54 | /// follows in setTableFields() (X86DisassemblerTables.cpp) |
| 55 | /// |
| 56 | /// - If the current context is the native context for one of the instructions |
| 57 | /// (that is, the attributes specified for it in the LLVM tables specify |
| 58 | /// precisely the current context), then it has priority. |
| 59 | /// - If the current context isn't native for either of the instructions, then |
| 60 | /// the higher-priority context wins (that is, the one that is more specific). |
| 61 | /// That hierarchy is determined by outranks() (X86DisassemblerTables.cpp) |
| 62 | /// - If the current context is native for both instructions, then the table |
| 63 | /// emitter reports a conflict and dies. |
| 64 | /// |
| 65 | /// *** RESOLUTION FOR "Primary decode conflict"S |
| 66 | /// |
| 67 | /// If two instructions collide, typically the solution is (in order of |
| 68 | /// likelihood): |
| 69 | /// |
| 70 | /// (1) to filter out one of the instructions by editing filter() |
| 71 | /// (X86RecognizableInstr.cpp). This is the most common resolution, but |
| 72 | /// check the Intel manuals first to make sure that (2) and (3) are not the |
| 73 | /// problem. |
| 74 | /// (2) to fix the tables (X86.td and its subsidiaries) so the opcodes are |
| 75 | /// accurate. Sometimes they are not. |
| 76 | /// (3) to fix the tables to reflect the actual context (for example, required |
| 77 | /// prefixes), and possibly to add a new context by editing |
David Blaikie | cb6b3af | 2018-03-23 23:58:20 +0000 | [diff] [blame] | 78 | /// include/llvm/Support/X86DisassemblerDecoderCommon.h. This is unlikely |
| 79 | /// to be the cause. |
Sean Callanan | 8ed9f51 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 80 | /// |
| 81 | /// DisassemblerEmitter.cpp contains the implementation for the emitter, |
| 82 | /// which simply pulls out instructions from the CodeGenTarget and pushes them |
| 83 | /// into X86DisassemblerTables. |
| 84 | /// X86DisassemblerTables.h contains the interface for the instruction tables, |
| 85 | /// which manage and emit the structures discussed above. |
| 86 | /// X86DisassemblerTables.cpp contains the implementation for the instruction |
| 87 | /// tables. |
| 88 | /// X86ModRMFilters.h contains filters that can be used to determine which |
| 89 | /// ModR/M values are valid for a particular instruction. These are used to |
| 90 | /// populate ModRMDecisions. |
| 91 | /// X86RecognizableInstr.h contains the interface for a single instruction, |
| 92 | /// which knows how to translate itself from a CodeGenInstruction and provide |
| 93 | /// the information necessary for integration into the tables. |
| 94 | /// X86RecognizableInstr.cpp contains the implementation for a single |
| 95 | /// instruction. |
Daniel Dunbar | 4058874 | 2009-11-25 02:13:23 +0000 | [diff] [blame] | 96 | |
Jakob Stoklund Olesen | 6f36fa9 | 2012-06-11 15:37:55 +0000 | [diff] [blame] | 97 | namespace llvm { |
Daniel Dunbar | 4058874 | 2009-11-25 02:13:23 +0000 | [diff] [blame] | 98 | |
Jakob Stoklund Olesen | 6f36fa9 | 2012-06-11 15:37:55 +0000 | [diff] [blame] | 99 | extern void EmitFixedLenDecoder(RecordKeeper &RK, raw_ostream &OS, |
Benjamin Kramer | 36538ff | 2016-06-08 19:09:22 +0000 | [diff] [blame] | 100 | const std::string &PredicateNamespace, |
| 101 | const std::string &GPrefix, |
| 102 | const std::string &GPostfix, |
| 103 | const std::string &ROK, |
| 104 | const std::string &RFail, const std::string &L); |
Jakob Stoklund Olesen | 6f36fa9 | 2012-06-11 15:37:55 +0000 | [diff] [blame] | 105 | |
| 106 | void EmitDisassembler(RecordKeeper &Records, raw_ostream &OS) { |
| 107 | CodeGenTarget Target(Records); |
Matthias Braun | 0c517c8 | 2016-12-04 05:48:16 +0000 | [diff] [blame] | 108 | emitSourceFileHeader(" * " + Target.getName().str() + " Disassembler", OS); |
Daniel Dunbar | 4058874 | 2009-11-25 02:13:23 +0000 | [diff] [blame] | 109 | |
Sean Callanan | 8ed9f51 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 110 | // X86 uses a custom disassembler. |
| 111 | if (Target.getName() == "X86") { |
| 112 | DisassemblerTables Tables; |
Jakob Stoklund Olesen | 6f36fa9 | 2012-06-11 15:37:55 +0000 | [diff] [blame] | 113 | |
Craig Topper | 3f0462d | 2016-02-01 01:33:42 +0000 | [diff] [blame] | 114 | ArrayRef<const CodeGenInstruction*> numberedInstructions = |
Chris Lattner | f650278 | 2010-03-19 00:34:35 +0000 | [diff] [blame] | 115 | Target.getInstructionsByEnumValue(); |
Jakob Stoklund Olesen | 6f36fa9 | 2012-06-11 15:37:55 +0000 | [diff] [blame] | 116 | |
Sean Callanan | 8ed9f51 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 117 | for (unsigned i = 0, e = numberedInstructions.size(); i != e; ++i) |
| 118 | RecognizableInstr::processInstr(Tables, *numberedInstructions[i], i); |
| 119 | |
Craig Topper | 62fe07a | 2014-01-05 01:34:12 +0000 | [diff] [blame] | 120 | if (Tables.hasConflicts()) { |
| 121 | PrintError(Target.getTargetRecord()->getLoc(), "Primary decode conflict"); |
| 122 | return; |
| 123 | } |
Sean Callanan | 8ed9f51 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 124 | |
| 125 | Tables.emit(OS); |
| 126 | return; |
| 127 | } |
| 128 | |
Sam Clegg | 04cf0d7 | 2018-05-10 22:16:44 +0000 | [diff] [blame] | 129 | // WebAssembly has variable length opcodes, so can't use EmitFixedLenDecoder |
| 130 | // below (which depends on a Size table-gen Record), and also uses a custom |
| 131 | // disassembler. |
| 132 | if (Target.getName() == "WebAssembly") { |
| 133 | emitWebAssemblyDisassemblerTables(OS, Target.getInstructionsByEnumValue()); |
| 134 | return; |
| 135 | } |
| 136 | |
Owen Anderson | 83e3f67 | 2011-08-17 17:44:15 +0000 | [diff] [blame] | 137 | // ARM and Thumb have a CHECK() macro to deal with DecodeStatuses. |
Tim Northover | 3524723 | 2014-05-06 14:15:14 +0000 | [diff] [blame] | 138 | if (Target.getName() == "ARM" || Target.getName() == "Thumb" || |
| 139 | Target.getName() == "AArch64" || Target.getName() == "ARM64") { |
| 140 | std::string PredicateNamespace = Target.getName(); |
| 141 | if (PredicateNamespace == "Thumb") |
| 142 | PredicateNamespace = "ARM"; |
| 143 | |
| 144 | EmitFixedLenDecoder(Records, OS, PredicateNamespace, |
Petr Pavlu | d2e1e42 | 2015-07-15 08:04:27 +0000 | [diff] [blame] | 145 | "if (!Check(S, ", "))", |
Jakob Stoklund Olesen | 6f36fa9 | 2012-06-11 15:37:55 +0000 | [diff] [blame] | 146 | "S", "MCDisassembler::Fail", |
| 147 | " MCDisassembler::DecodeStatus S = " |
| 148 | "MCDisassembler::Success;\n(void)S;"); |
Owen Anderson | 83e3f67 | 2011-08-17 17:44:15 +0000 | [diff] [blame] | 149 | return; |
| 150 | } |
| 151 | |
Jakob Stoklund Olesen | 6f36fa9 | 2012-06-11 15:37:55 +0000 | [diff] [blame] | 152 | EmitFixedLenDecoder(Records, OS, Target.getName(), |
Petr Pavlu | d2e1e42 | 2015-07-15 08:04:27 +0000 | [diff] [blame] | 153 | "if (", " == MCDisassembler::Fail)", |
Jakob Stoklund Olesen | 6f36fa9 | 2012-06-11 15:37:55 +0000 | [diff] [blame] | 154 | "MCDisassembler::Success", "MCDisassembler::Fail", ""); |
Daniel Dunbar | 4058874 | 2009-11-25 02:13:23 +0000 | [diff] [blame] | 155 | } |
Jakob Stoklund Olesen | 6f36fa9 | 2012-06-11 15:37:55 +0000 | [diff] [blame] | 156 | |
| 157 | } // End llvm namespace |