llvm.org GIT mirror llvm / 9899f70
Fixed a nasty layering violation in the edis source code. It used to #include the enhanced disassembly information for the targets it supported straight out of lib/Target/{X86,ARM,...} but now it uses a new interface provided by MCDisassembler, and (so far) implemented by X86 and ARM. Also removed hacky #define-controlled initialization of targets in edis. If clients only want edis to initialize a limited set of targets, they can set --enable-targets on the configure command line. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@101179 91177308-0d34-0410-b5e6-96231b3b80d8 Sean Callanan 9 years ago
13 changed file(s) with 161 addition(s) and 124 deletion(s). Raw diff Collapse all Expand all
0 //===-- llvm/MC/EDInstInfo.h - EDis instruction info ------------*- C++ -*-===//
1 //
2 // The LLVM Compiler Infrastructure
3 //
4 // This file is distributed under the University of Illinois Open Source
5 // License. See LICENSE.TXT for details.
6 //
7 //===----------------------------------------------------------------------===//
8 #ifndef EDINSTINFO_H
9 #define EDINSTINFO_H
10
11 #include "llvm/System/DataTypes.h"
12
13 namespace llvm {
14
15 #define EDIS_MAX_OPERANDS 13
16 #define EDIS_MAX_SYNTAXES 2
17
18 struct EDInstInfo {
19 uint8_t instructionType;
20 uint8_t numOperands;
21 uint8_t operandTypes[EDIS_MAX_OPERANDS];
22 uint8_t operandFlags[EDIS_MAX_OPERANDS];
23 const char operandOrders[EDIS_MAX_SYNTAXES][EDIS_MAX_OPERANDS];
24 };
25
26 } // namespace llvm
27
28 #endif
1515 class MCInst;
1616 class MemoryObject;
1717 class raw_ostream;
18
19 struct EDInstInfo;
1820
1921 /// MCDisassembler - Superclass for all disassemblers. Consumes a memory region
2022 /// and provides an array of assembly instructions.
4244 const MemoryObject ®ion,
4345 uint64_t address,
4446 raw_ostream &vStream) const = 0;
45 };
47
48 /// getEDInfo - Returns the enhanced insturction information corresponding to
49 /// the disassembler.
50 ///
51 /// @return - An array of instruction information, with one entry for
52 /// each MCInst opcode this disassembler returns.
53 /// NULL if there is no info for this target.
54 virtual EDInstInfo *getEDInfo() const { return NULL; }
55 };
4656
4757 } // namespace llvm
4858
1717 #include "ARMDisassembler.h"
1818 #include "ARMDisassemblerCore.h"
1919
20 #include "llvm/MC/EDInstInfo.h"
2021 #include "llvm/MC/MCInst.h"
2122 #include "llvm/Target/TargetRegistry.h"
2223 #include "llvm/Support/Debug.h"
3738 ///
3839 #include "../ARMGenDecoderTables.inc"
3940
40 namespace llvm {
41 #include "../ARMGenEDInfo.inc"
42
43 using namespace llvm;
4144
4245 /// showBitVector - Use the raw_ostream to log a diagnostic message describing
4346 /// the inidividual bits of the instruction.
546549 createThumbDisassembler);
547550 }
548551
549 } // namespace llvm
552 EDInstInfo *ARMDisassembler::getEDInfo() const {
553 return instInfoARM;
554 }
555
556 EDInstInfo *ThumbDisassembler::getEDInfo() const {
557 return instInfoARM;
558 }
2323 class MemoryObject;
2424 class raw_ostream;
2525
26 struct EDInstInfo;
27
2628 /// ARMDisassembler - ARM disassembler for all ARM platforms.
2729 class ARMDisassembler : public MCDisassembler {
2830 public:
4143 const MemoryObject ®ion,
4244 uint64_t address,
4345 raw_ostream &vStream) const;
46
47 /// getEDInfo - See MCDisassembler.
48 EDInstInfo *getEDInfo() const;
4449 private:
4550 };
4651
8186 const MemoryObject ®ion,
8287 uint64_t address,
8388 raw_ostream &vStream) const;
89
90 /// getEDInfo - See MCDisassembler.
91 EDInstInfo *getEDInfo() const;
8492 private:
8593 Session SO;
8694 };
1616 #include "X86Disassembler.h"
1717 #include "X86DisassemblerDecoder.h"
1818
19 #include "llvm/MC/EDInstInfo.h"
1920 #include "llvm/MC/MCDisassembler.h"
2021 #include "llvm/MC/MCDisassembler.h"
2122 #include "llvm/MC/MCInst.h"
2526 #include "llvm/Support/raw_ostream.h"
2627
2728 #include "X86GenRegisterNames.inc"
29 #include "X86GenEDInfo.inc"
2830
2931 using namespace llvm;
3032 using namespace llvm::X86Disassembler;
6668 }
6769
6870 X86GenericDisassembler::~X86GenericDisassembler() {
71 }
72
73 EDInstInfo *X86GenericDisassembler::getEDInfo() const {
74 return instInfoX86;
6975 }
7076
7177 /// regionReader - a callback function that wraps the readByte method from
9393 class MCInst;
9494 class MemoryObject;
9595 class raw_ostream;
96
97 struct EDInstInfo;
9698
9799 namespace X86Disassembler {
98100
114116 const MemoryObject ®ion,
115117 uint64_t address,
116118 raw_ostream &vStream) const;
119
120 /// getEDInfo - See MCDisassembler.
121 EDInstInfo *getEDInfo() const;
117122 private:
118123 DisassemblerMode fMode;
119124 };
1717
1818 #include "llvm/ADT/OwningPtr.h"
1919 #include "llvm/ADT/SmallVector.h"
20 #include "llvm/MC/EDInstInfo.h"
2021 #include "llvm/MC/MCAsmInfo.h"
2122 #include "llvm/MC/MCContext.h"
2223 #include "llvm/MC/MCDisassembler.h"
3839 #include "llvm/Target/TargetRegisterInfo.h"
3940 #include "llvm/Target/TargetSelect.h"
4041
41 #ifdef EDIS_X86
42 #include "../../lib/Target/X86/X86GenEDInfo.inc"
43 #endif
44
45 #ifdef EDIS_ARM
46 #include "../../lib/Target/ARM/ARMGenEDInfo.inc"
47 #endif
48
4942 using namespace llvm;
5043
5144 bool EDDisassembler::sInitialized = false;
5245 EDDisassembler::DisassemblerMap_t EDDisassembler::sDisassemblers;
5346
54 struct InfoMap {
47 struct TripleMap {
5548 Triple::ArchType Arch;
5649 const char *String;
57 const InstInfo *Info;
5850 };
5951
60 static struct InfoMap infomap[] = {
61 #ifdef EDIS_X86
62 { Triple::x86, "i386-unknown-unknown", instInfoX86 },
63 { Triple::x86_64, "x86_64-unknown-unknown", instInfoX86 },
64 #endif
65 #ifdef EDIS_ARM
66 { Triple::arm, "arm-unknown-unknown", instInfoARM },
67 { Triple::thumb, "thumb-unknown-unknown", instInfoARM },
68 #endif
69 { Triple::InvalidArch, NULL, NULL }
52 static struct TripleMap triplemap[] = {
53 { Triple::x86, "i386-unknown-unknown" },
54 { Triple::x86_64, "x86_64-unknown-unknown" },
55 { Triple::arm, "arm-unknown-unknown" },
56 { Triple::thumb, "thumb-unknown-unknown" },
57 { Triple::InvalidArch, NULL, }
7058 };
7159
72 /// infoFromArch - Returns the InfoMap corresponding to a given architecture,
60 /// infoFromArch - Returns the TripleMap corresponding to a given architecture,
7361 /// or NULL if there is an error
7462 ///
7563 /// @arg arch - The Triple::ArchType for the desired architecture
76 static const InfoMap *infoFromArch(Triple::ArchType arch) {
64 static const char *tripleFromArch(Triple::ArchType arch) {
7765 unsigned int infoIndex;
7866
79 for (infoIndex = 0; infomap[infoIndex].String != NULL; ++infoIndex) {
80 if (arch == infomap[infoIndex].Arch)
81 return &infomap[infoIndex];
67 for (infoIndex = 0; triplemap[infoIndex].String != NULL; ++infoIndex) {
68 if (arch == triplemap[infoIndex].Arch)
69 return triplemap[infoIndex].String;
8270 }
8371
8472 return NULL;
114102 }
115103 }
116104
117 #define BRINGUP_TARGET(tgt) \
118 LLVMInitialize##tgt##TargetInfo(); \
119 LLVMInitialize##tgt##Target(); \
120 LLVMInitialize##tgt##AsmPrinter(); \
121 LLVMInitialize##tgt##AsmParser(); \
122 LLVMInitialize##tgt##Disassembler();
123
124105 void EDDisassembler::initialize() {
125106 if (sInitialized)
126107 return;
127108
128109 sInitialized = true;
129110
130 #ifdef EDIS_X86
131 BRINGUP_TARGET(X86)
132 #endif
133 #ifdef EDIS_ARM
134 BRINGUP_TARGET(ARM)
135 #endif
111 InitializeAllTargetInfos();
112 InitializeAllTargets();
113 InitializeAllAsmPrinters();
114 InitializeAllAsmParsers();
115 InitializeAllDisassemblers();
136116 }
137117
138118 #undef BRINGUP_TARGET
174154 HasSemantics(false),
175155 ErrorStream(nulls()),
176156 Key(key) {
177 const InfoMap *infoMap = infoFromArch(key.Arch);
178
179 if (!infoMap)
180 return;
181
182 InstInfos = infoMap->Info;
183
184 const char *triple = infoMap->String;
157 const char *triple = tripleFromArch(key.Arch);
158
159 if (!triple)
160 return;
185161
186162 LLVMSyntaxVariant = getLLVMSyntaxVariant(key.Arch, key.Syntax);
187163
219195
220196 if (!Disassembler)
221197 return;
198
199 InstInfos = Disassembler->getEDInfo();
222200
223201 InstString.reset(new std::string);
224202 InstStream.reset(new raw_string_ostream(*InstString));
230208 GenericAsmLexer.reset(new AsmLexer(*AsmInfo));
231209 SpecificAsmLexer.reset(Tgt->createAsmLexer(*AsmInfo));
232210 SpecificAsmLexer->InstallLexer(*GenericAsmLexer);
233
234 InstInfos = infoMap->Info;
235211
236212 initMaps(*targetMachine->getRegisterInfo());
237213
284260 delete inst;
285261 return NULL;
286262 } else {
287 const InstInfo *thisInstInfo;
263 const llvm::EDInstInfo *thisInstInfo;
288264
289265 thisInstInfo = &InstInfos[inst->getOpcode()];
290266
307283 switch (Key.Arch) {
308284 default:
309285 break;
310 #ifdef EDIS_X86
311286 case Triple::x86:
312287 case Triple::x86_64:
313288 stackPointers.insert(registerIDWithName("SP"));
318293 programCounters.insert(registerIDWithName("EIP"));
319294 programCounters.insert(registerIDWithName("RIP"));
320295 break;
321 #endif
322 #ifdef EDIS_ARM
323296 case Triple::arm:
324297 case Triple::thumb:
325298 stackPointers.insert(registerIDWithName("SP"));
326299
327300 programCounters.insert(registerIDWithName("PC"));
328301 break;
329 #endif
330302 }
331303 }
332304
4747 class SourceMgr;
4848 class Target;
4949 class TargetRegisterInfo;
50
51 struct EDInstInfo;
5052 }
5153
5254 /// EDDisassembler - Encapsulates a disassembler for a single architecture and
142144 llvm::sys::Mutex PrinterMutex;
143145 /// The array of instruction information provided by the TableGen backend for
144146 /// the target architecture
145 const InstInfo *InstInfos;
147 const llvm::EDInstInfo *InstInfos;
146148 /// The target-specific lexer for use in tokenizing strings, in
147149 /// target-independent and target-specific portions
148150 llvm::OwningPtr GenericAsmLexer;
1717 #include "EDOperand.h"
1818 #include "EDToken.h"
1919
20 #include "llvm/MC/EDInstInfo.h"
2021 #include "llvm/MC/MCInst.h"
2122
2223 using namespace llvm;
2425 EDInst::EDInst(llvm::MCInst *inst,
2526 uint64_t byteSize,
2627 EDDisassembler &disassembler,
27 const InstInfo *info) :
28 const llvm::EDInstInfo *info) :
2829 Disassembler(disassembler),
2930 Inst(inst),
3031 ThisInstInfo(info),
2121
2222 #include
2323 #include
24
25 namespace llvm {
26 struct EDInstInfo;
27 }
2428
2529 /// CachedResult - Encapsulates the result of a function along with the validity
2630 /// of that result, so that slow functions don't need to run twice
5357 /// The containing MCInst
5458 llvm::MCInst *Inst;
5559 /// The instruction information provided by TableGen for this instruction
56 const InstInfo *ThisInstInfo;
60 const llvm::EDInstInfo *ThisInstInfo;
5761 /// The number of bytes for the machine code representation of the instruction
5862 uint64_t ByteSize;
5963
9498 EDInst(llvm::MCInst *inst,
9599 uint64_t byteSize,
96100 EDDisassembler &disassembler,
97 const InstInfo *instInfo);
101 const llvm::EDInstInfo *instInfo);
98102 ~EDInst();
99103
100104 /// byteSize - returns the number of bytes consumed by the machine code
1616 #include "EDInst.h"
1717 #include "EDOperand.h"
1818
19 #include "llvm/MC/EDInstInfo.h"
1920 #include "llvm/MC/MCInst.h"
2021
2122 using namespace llvm;
4848 endif
4949 endif
5050
51 EDIS_DEFINES =
52
53 ifneq (,$(findstring X86,$(TARGETS_TO_BUILD)))
54 EDIS_DEFINES := $(EDIS_DEFINES) -DEDIS_X86
55 endif
56
57 ifneq (,$(findstring ARM,$(TARGETS_TO_BUILD)))
58 EDIS_DEFINES := $(EDIS_DEFINES) -DEDIS_ARM
59 endif
60
61 CXXFLAGS := $(CXXFLAGS) $(EDIS_DEFINES)
62
6351 EDInfo.inc: $(TBLGEN)
6452 $(Echo) "Building semantic information header"
6553 $(Verb) $(TableGen) -o $(call SYSPATH, $@) -gen-enhanced-disassembly-header /dev/null
1818 #include "CodeGenTarget.h"
1919 #include "Record.h"
2020
21 #include "llvm/MC/EDInstInfo.h"
2122 #include "llvm/Support/ErrorHandling.h"
2223 #include "llvm/Support/Format.h"
2324 #include "llvm/Support/raw_ostream.h"
2526 #include
2627 #include
2728 #include
28
29 #define MAX_OPERANDS 13
30 #define MAX_SYNTAXES 2
3129
3230 using namespace llvm;
3331
375373 /// @operandFlags - A reference the array of operand flag objects
376374 /// @inst - The instruction to use as a source of information
377375 static void X86PopulateOperands(
378 LiteralConstantEmitter *(&operandTypes)[MAX_OPERANDS],
376 LiteralConstantEmitter *(&operandTypes)[EDIS_MAX_OPERANDS],
379377 const CodeGenInstruction &inst) {
380378 if (!inst.TheDef->isSubClassOf("X86Inst"))
381379 return;
405403 /// @opName - The name of the operand
406404 /// @flag - The name of the flag to add
407405 static inline void decorate1(
408 FlagsConstantEmitter *(&operandFlags)[MAX_OPERANDS],
406 FlagsConstantEmitter *(&operandFlags)[EDIS_MAX_OPERANDS],
409407 const CodeGenInstruction &inst,
410408 const char *opName,
411409 const char *opFlag) {
457455 /// @arg inst - A reference to the original instruction
458456 static void X86ExtractSemantics(
459457 LiteralConstantEmitter &instType,
460 FlagsConstantEmitter *(&operandFlags)[MAX_OPERANDS],
458 FlagsConstantEmitter *(&operandFlags)[EDIS_MAX_OPERANDS],
461459 const CodeGenInstruction &inst) {
462460 const std::string &name = inst.TheDef->getName();
463461
654652 /// @operandFlags - A reference the array of operand flag objects
655653 /// @inst - The instruction to use as a source of information
656654 static void ARMPopulateOperands(
657 LiteralConstantEmitter *(&operandTypes)[MAX_OPERANDS],
655 LiteralConstantEmitter *(&operandTypes)[EDIS_MAX_OPERANDS],
658656 const CodeGenInstruction &inst) {
659657 if (!inst.TheDef->isSubClassOf("InstARM") &&
660658 !inst.TheDef->isSubClassOf("InstThumb"))
663661 unsigned int index;
664662 unsigned int numOperands = inst.OperandList.size();
665663
666 if (numOperands > MAX_OPERANDS) {
667 errs() << "numOperands == " << numOperands << " > " << MAX_OPERANDS << '\n';
664 if (numOperands > EDIS_MAX_OPERANDS) {
665 errs() << "numOperands == " << numOperands << " > " <<
666 EDIS_MAX_OPERANDS << '\n';
668667 llvm_unreachable("Too many operands");
669668 }
670669
697696 /// @arg inst - A reference to the original instruction
698697 static void ARMExtractSemantics(
699698 LiteralConstantEmitter &instType,
700 LiteralConstantEmitter *(&operandTypes)[MAX_OPERANDS],
701 FlagsConstantEmitter *(&operandFlags)[MAX_OPERANDS],
699 LiteralConstantEmitter *(&operandTypes)[EDIS_MAX_OPERANDS],
700 FlagsConstantEmitter *(&operandFlags)[EDIS_MAX_OPERANDS],
702701 const CodeGenInstruction &inst) {
703702 const std::string &name = inst.TheDef->getName();
704703
758757 CompoundConstantEmitter *operandTypeArray = new CompoundConstantEmitter;
759758 infoStruct->addEntry(operandTypeArray);
760759
761 LiteralConstantEmitter *operandTypes[MAX_OPERANDS];
760 LiteralConstantEmitter *operandTypes[EDIS_MAX_OPERANDS];
762761
763762 CompoundConstantEmitter *operandFlagArray = new CompoundConstantEmitter;
764763 infoStruct->addEntry(operandFlagArray);
765764
766 FlagsConstantEmitter *operandFlags[MAX_OPERANDS];
765 FlagsConstantEmitter *operandFlags[EDIS_MAX_OPERANDS];
767766
768767 for (unsigned operandIndex = 0;
769 operandIndex < MAX_OPERANDS;
768 operandIndex < EDIS_MAX_OPERANDS;
770769 ++operandIndex) {
771770 operandTypes[operandIndex] = new LiteralConstantEmitter;
772771 operandTypeArray->addEntry(operandTypes[operandIndex]);
792791
793792 infoStruct->addEntry(operandOrderArray);
794793
795 for (unsigned syntaxIndex = 0; syntaxIndex < MAX_SYNTAXES; ++syntaxIndex) {
794 for (unsigned syntaxIndex = 0;
795 syntaxIndex < EDIS_MAX_SYNTAXES;
796 ++syntaxIndex) {
796797 CompoundConstantEmitter *operandOrder =
797 new CompoundConstantEmitter(MAX_OPERANDS);
798 new CompoundConstantEmitter(EDIS_MAX_OPERANDS);
798799
799800 operandOrderArray->addEntry(operandOrder);
800801
807808 }
808809 }
809810
810 void EDEmitter::run(raw_ostream &o) {
811 unsigned int i = 0;
812
813 CompoundConstantEmitter infoArray;
814 CodeGenTarget target;
815
816 populateInstInfo(infoArray, target);
817
818 o << "InstInfo instInfo" << target.getName().c_str() << "[] = ";
819 infoArray.emit(o, i);
820 o << ";" << "\n";
821 }
822
823 void EDEmitter::runHeader(raw_ostream &o) {
824 EmitSourceFileHeader("Enhanced Disassembly Info Header", o);
825
826 o << "#ifndef EDInfo_" << "\n";
827 o << "#define EDInfo_" << "\n";
828 o << "\n";
829 o << "#include " << "\n";
830 o << "\n";
831 o << "#define MAX_OPERANDS " << format("%d", MAX_OPERANDS) << "\n";
832 o << "#define MAX_SYNTAXES " << format("%d", MAX_SYNTAXES) << "\n";
833 o << "\n";
834
835 unsigned int i = 0;
836
811 static void emitCommonEnums(raw_ostream &o, unsigned int &i) {
837812 EnumEmitter operandTypes("OperandTypes");
838813 operandTypes.addEntry("kOperandTypeNone");
839814 operandTypes.addEntry("kOperandTypeImmediate");
871846 operandTypes.addEntry("kOperandTypeThumb2AddrModeSoReg");
872847 operandTypes.addEntry("kOperandTypeThumb2AddrModeImm8s4");
873848 operandTypes.addEntry("kOperandTypeThumb2AddrModeImm8s4Offset");
874
875849 operandTypes.emit(o, i);
876850
877851 o << "\n";
894868 instructionTypes.emit(o, i);
895869
896870 o << "\n";
897
898 StructEmitter instInfo("InstInfo");
899 instInfo.addMember("uint8_t", "instructionType");
900 instInfo.addMember("uint8_t", "numOperands");
901 instInfo.addMember("uint8_t", "operandTypes[MAX_OPERANDS]");
902 instInfo.addMember("uint8_t", "operandFlags[MAX_OPERANDS]");
903 instInfo.addMember("const char", "operandOrders[MAX_SYNTAXES][MAX_OPERANDS]");
904 instInfo.emit(o, i);
871 }
872
873 void EDEmitter::run(raw_ostream &o) {
874 unsigned int i = 0;
875
876 CompoundConstantEmitter infoArray;
877 CodeGenTarget target;
878
879 populateInstInfo(infoArray, target);
880
881 emitCommonEnums(o, i);
882
883 o << "namespace {\n";
884
885 o << "llvm::EDInstInfo instInfo" << target.getName().c_str() << "[] = ";
886 infoArray.emit(o, i);
887 o << ";" << "\n";
888
889 o << "}\n";
890 }
891
892 void EDEmitter::runHeader(raw_ostream &o) {
893 EmitSourceFileHeader("Enhanced Disassembly Info Header", o);
894
895 o << "#ifndef EDInfo_" << "\n";
896 o << "#define EDInfo_" << "\n";
897 o << "\n";
898 o << "#include " << "\n";
899 o << "\n";
900 o << "#define EDIS_MAX_OPERANDS " << format("%d", EDIS_MAX_OPERANDS) << "\n";
901 o << "#define EDIS_MAX_SYNTAXES " << format("%d", EDIS_MAX_SYNTAXES) << "\n";
902 o << "\n";
903
904 unsigned int i = 0;
905
906 emitCommonEnums(o, i);
905907
906908 o << "\n";
907909 o << "#endif" << "\n";