DisassemblerEmitter.cpp revision f65027842e82027dd6e8020586a299aaa548e355
12a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)//===- DisassemblerEmitter.cpp - Generate a disassembler ------------------===//
25821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
35821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//                     The LLVM Compiler Infrastructure
45821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
52a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// This file is distributed under the University of Illinois Open Source
62a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// License. See LICENSE.TXT for details.
75821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
85821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//===----------------------------------------------------------------------===//
95821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
10a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)#include "DisassemblerEmitter.h"
112a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "CodeGenTarget.h"
122a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "Record.h"
135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "X86DisassemblerTables.h"
145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "X86RecognizableInstr.h"
1568043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)using namespace llvm;
1668043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)using namespace llvm::X86Disassembler;
1768043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)
1868043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)/// DisassemblerEmitter - Contains disassembler table emitters for various
192a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)/// architectures.
202a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
215d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)/// X86 Disassembler Emitter
220f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)///
232a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)/// *** IF YOU'RE HERE TO RESOLVE A "Primary decode conflict", LOOK DOWN NEAR
242a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)///     THE END OF THIS COMMENT!
252a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)///
262a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)/// The X86 disassembler emitter is part of the X86 Disassembler, which is
27cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)/// documented in lib/Target/X86/X86Disassembler.h.
282a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)///
292a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)/// The emitter produces the tables that the disassembler uses to translate
305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)/// instructions.  The emitter generates the following tables:
315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)///
325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)/// - One table (CONTEXTS_SYM) that contains a mapping of attribute masks to
335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)///   instruction contexts.  Although for each attribute there are cases where
345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)///   that attribute determines decoding, in the majority of cases decoding is
3568043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)///   the same whether or not an attribute is present.  For example, a 64-bit
3668043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)///   instruction with an OPSIZE prefix and an XS prefix decodes the same way in
3768043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)///   all cases as a 64-bit instruction with only OPSIZE set.  (The XS prefix
3868043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)///   may have effects on its execution, but does not change the instruction
3968043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)///   returned.)  This allows considerable space savings in other tables.
4068043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)/// - Four tables (ONEBYTE_SYM, TWOBYTE_SYM, THREEBYTE38_SYM, and
4168043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)///   THREEBYTE3A_SYM) contain the hierarchy that the decoder traverses while
424e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)///   decoding an instruction.  At the lowest level of this hierarchy are
434e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)///   instruction UIDs, 16-bit integers that can be used to uniquely identify
444e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)///   the instruction and correspond exactly to its position in the list of
454e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)///   CodeGenInstructions for the target.
464e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)/// - One table (INSTRUCTIONS_SYM) contains information about the operands of
474e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)///   each instruction and how to decode them.
484e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)///
495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)/// During table generation, there may be conflicts between instructions that
505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)/// occupy the same space in the decode tables.  These conflicts are resolved as
51f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)/// follows in setTableFields() (X86DisassemblerTables.cpp)
52f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)///
53f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)/// - If the current context is the native context for one of the instructions
54f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)///   (that is, the attributes specified for it in the LLVM tables specify
552a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)///   precisely the current context), then it has priority.
562a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)/// - If the current context isn't native for either of the instructions, then
575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)///   the higher-priority context wins (that is, the one that is more specific).
585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)///   That hierarchy is determined by outranks() (X86DisassemblerTables.cpp)
595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)/// - If the current context is native for both instructions, then the table
605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)///   emitter reports a conflict and dies.
612a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)///
622a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)/// *** RESOLUTION FOR "Primary decode conflict"S
632a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)///
64cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)/// If two instructions collide, typically the solution is (in order of
65cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)/// likelihood):
66cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)///
67cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)/// (1) to filter out one of the instructions by editing filter()
68cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)///     (X86RecognizableInstr.cpp).  This is the most common resolution, but
692a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)///     check the Intel manuals first to make sure that (2) and (3) are not the
701e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)///     problem.
712a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)/// (2) to fix the tables (X86.td and its subsidiaries) so the opcodes are
725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)///     accurate.  Sometimes they are not.
731e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)/// (3) to fix the tables to reflect the actual context (for example, required
741e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)///     prefixes), and possibly to add a new context by editing
751e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)///     lib/Target/X86/X86DisassemblerDecoderCommon.h.  This is unlikely to be
764e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)///     the cause.
775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)///
78cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)/// DisassemblerEmitter.cpp contains the implementation for the emitter,
79cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)///   which simply pulls out instructions from the CodeGenTarget and pushes them
80cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)///   into X86DisassemblerTables.
81cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)/// X86DisassemblerTables.h contains the interface for the instruction tables,
822a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)///   which manage and emit the structures discussed above.
83eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch/// X86DisassemblerTables.cpp contains the implementation for the instruction
841e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)///   tables.
858bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)/// X86ModRMFilters.h contains filters that can be used to determine which
868bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)///   ModR/M values are valid for a particular instruction.  These are used to
878bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)///   populate ModRMDecisions.
881e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)/// X86RecognizableInstr.h contains the interface for a single instruction,
891e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)///   which knows how to translate itself from a CodeGenInstruction and provide
905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)///   the information necessary for integration into the tables.
915d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)/// X86RecognizableInstr.cpp contains the implementation for a single
925d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)///   instruction.
935d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
945d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)void DisassemblerEmitter::run(raw_ostream &OS) {
955d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  CodeGenTarget Target;
965d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
975d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  OS << "/*===- TableGen'erated file "
985d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)     << "---------------------------------------*- C -*-===*\n"
995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)     << " *\n"
1005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)     << " * " << Target.getName() << " Disassembler\n"
1011e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)     << " *\n"
1025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)     << " * Automatically generated file, do not edit!\n"
1035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)     << " *\n"
1045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)     << " *===---------------------------------------------------------------"
1052a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)     << "-------===*/\n";
10668043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)
1075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // X86 uses a custom disassembler.
1085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (Target.getName() == "X86") {
1095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    DisassemblerTables Tables;
1105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1112a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    const std::vector<const CodeGenInstruction*> &numberedInstructions =
11268043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)      Target.getInstructionsByEnumValue();
1135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    for (unsigned i = 0, e = numberedInstructions.size(); i != e; ++i)
11568043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)      RecognizableInstr::processInstr(Tables, *numberedInstructions[i], i);
11668043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)
11768043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)    // FIXME: As long as we are using exceptions, might as well drop this to the
1182a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    // actual conflict site.
1192a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    if (Tables.hasConflicts())
1201e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)      throw TGError(Target.getTargetRecord()->getLoc(),
12168043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)                    "Primary decode conflict");
12268043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)
1231e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    Tables.emit(OS);
1241e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    return;
1251e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  }
1261e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
1271e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  throw TGError(Target.getTargetRecord()->getLoc(),
1281e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)                "Unable to generate disassembler for this target");
1291e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)}
1301e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)