llvm.org GIT mirror llvm / b950585
Refactor instprinter and mcdisassembler to take a SubtargetInfo. Add -mattr= handling to llvm-mc. Reviewed by Owen Anderson. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@139237 91177308-0d34-0410-b5e6-96231b3b80d8 James Molloy 9 years ago
24 changed file(s) with 137 addition(s) and 61 deletion(s). Raw diff Collapse all Expand all
1414 namespace llvm {
1515
1616 class MCInst;
17 class MCSubtargetInfo;
1718 class MemoryObject;
1819 class raw_ostream;
1920 class MCContext;
5354 };
5455
5556 /// Constructor - Performs initial setup for the disassembler.
56 MCDisassembler() : GetOpInfo(0), DisInfo(0), Ctx(0) {}
57 MCDisassembler(const MCSubtargetInfo &STI) : GetOpInfo(0), DisInfo(0), Ctx(0), STI(STI) {}
5758
5859 virtual ~MCDisassembler();
5960
9798 // The assembly context for creating symbols and MCExprs in place of
9899 // immediate operands when there is symbolic information.
99100 MCContext *Ctx;
101 protected:
102 // Subtarget information, for instruction decoding predicates if required.
103 const MCSubtargetInfo &STI;
100104
101105 public:
102106 void setupForSymbolicDisassembly(LLVMOpInfoCallback getOpInfo,
9393 const MCAsmInfo &MAI);
9494 typedef MCTargetAsmParser *(*MCAsmParserCtorTy)(MCSubtargetInfo &STI,
9595 MCAsmParser &P);
96 typedef MCDisassembler *(*MCDisassemblerCtorTy)(const Target &T);
96 typedef MCDisassembler *(*MCDisassemblerCtorTy)(const Target &T,
97 const MCSubtargetInfo &STI);
9798 typedef MCInstPrinter *(*MCInstPrinterCtorTy)(const Target &T,
9899 unsigned SyntaxVariant,
99 const MCAsmInfo &MAI);
100 const MCAsmInfo &MAI,
101 const MCSubtargetInfo &STI);
100102 typedef MCCodeEmitter *(*MCCodeEmitterCtorTy)(const MCInstrInfo &II,
101103 const MCSubtargetInfo &STI,
102104 MCContext &Ctx);
372374 return AsmPrinterCtorFn(TM, Streamer);
373375 }
374376
375 MCDisassembler *createMCDisassembler() const {
377 MCDisassembler *createMCDisassembler(const MCSubtargetInfo &STI) const {
376378 if (!MCDisassemblerCtorFn)
377379 return 0;
378 return MCDisassemblerCtorFn(*this);
380 return MCDisassemblerCtorFn(*this, STI);
379381 }
380382
381383 MCInstPrinter *createMCInstPrinter(unsigned SyntaxVariant,
382 const MCAsmInfo &MAI) const {
384 const MCAsmInfo &MAI,
385 const MCSubtargetInfo &STI) const {
383386 if (!MCInstPrinterCtorFn)
384387 return 0;
385 return MCInstPrinterCtorFn(*this, SyntaxVariant, MAI);
388 return MCInstPrinterCtorFn(*this, SyntaxVariant, MAI, STI);
386389 }
387390
388391
130130 Context->setAllowTemporaryLabels(false);
131131
132132 const MCAsmInfo &MAI = *getMCAsmInfo();
133 const MCSubtargetInfo &STI = getSubtarget();
133134 OwningPtr AsmStreamer;
134135
135136 switch (FileType) {
136137 default: return true;
137138 case CGFT_AssemblyFile: {
138139 MCInstPrinter *InstPrinter =
139 getTarget().createMCInstPrinter(MAI.getAssemblerDialect(), MAI);
140 getTarget().createMCInstPrinter(MAI.getAssemblerDialect(), MAI, STI);
140141
141142 // Create a code emitter if asked to show the encoding.
142143 MCCodeEmitter *MCE = 0;
160161 case CGFT_ObjectFile: {
161162 // Create the code emitter for the target if it exists. If not, .o file
162163 // emission fails.
163 const MCSubtargetInfo &STI = getSubtarget();
164164 MCCodeEmitter *MCE = getTarget().createMCCodeEmitter(*getInstrInfo(), STI,
165165 *Context);
166166 MCAsmBackend *MAB = getTarget().createMCAsmBackend(getTargetTriple());
5656 std::string FeaturesStr;
5757 std::string CPU;
5858
59 const MCSubtargetInfo *STI = TheTarget->createMCSubtargetInfo(TripleName, CPU,
60 FeaturesStr);
61 assert(STI && "Unable to create subtarget info!");
62
5963 // Set up the MCContext for creating symbols and MCExpr's.
6064 MCContext *Ctx = new MCContext(*MAI, *MRI, 0);
6165 assert(Ctx && "Unable to create MCContext!");
6266
6367 // Set up disassembler.
64 MCDisassembler *DisAsm = TheTarget->createMCDisassembler();
68 MCDisassembler *DisAsm = TheTarget->createMCDisassembler(*STI);
6569 assert(DisAsm && "Unable to create disassembler!");
6670 DisAsm->setupForSymbolicDisassembly(GetOpInfo, DisInfo, Ctx);
6771
6872 // Set up the instruction printer.
6973 int AsmPrinterVariant = MAI->getAssemblerDialect();
7074 MCInstPrinter *IP = TheTarget->createMCInstPrinter(AsmPrinterVariant,
71 *MAI);
75 *MAI, *STI);
7276 assert(IP && "Unable to create instruction printer!");
7377
7478 LLVMDisasmContext *DC = new LLVMDisasmContext(TripleName, DisInfo, TagType,
177177 if (!AsmInfo)
178178 return;
179179
180 Disassembler.reset(Tgt->createMCDisassembler());
180 STI.reset(Tgt->createMCSubtargetInfo(tripleString, "", ""));
181
182 if (!STI)
183 return;
184
185 Disassembler.reset(Tgt->createMCDisassembler(*STI));
181186
182187 if (!Disassembler)
183188 return;
186191
187192 InstString.reset(new std::string);
188193 InstStream.reset(new raw_string_ostream(*InstString));
189 InstPrinter.reset(Tgt->createMCInstPrinter(LLVMSyntaxVariant, *AsmInfo));
194 InstPrinter.reset(Tgt->createMCInstPrinter(LLVMSyntaxVariant, *AsmInfo, *STI));
190195
191196 if (!InstPrinter)
192197 return;
136136 const llvm::Target *Tgt;
137137 /// The assembly information for the target architecture
138138 llvm::OwningPtr AsmInfo;
139 /// The subtarget information for the target architecture
140 llvm::OwningPtr STI;
139141 // The register information for the target architecture.
140142 llvm::OwningPtr MRI;
141143 /// The disassembler for the target architecture
1010
1111 #include "ARM.h"
1212 #include "ARMRegisterInfo.h"
13 #include "ARMSubtarget.h"
1314 #include "MCTargetDesc/ARMAddressingModes.h"
1415 #include "MCTargetDesc/ARMBaseInfo.h"
1516 #include "llvm/MC/EDInstInfo.h"
3334 public:
3435 /// Constructor - Initializes the disassembler.
3536 ///
36 ARMDisassembler() :
37 MCDisassembler() {
37 ARMDisassembler(const MCSubtargetInfo &STI) :
38 MCDisassembler(STI) {
3839 }
3940
4041 ~ARMDisassembler() {
5758 public:
5859 /// Constructor - Initializes the disassembler.
5960 ///
60 ThumbDisassembler() :
61 MCDisassembler() {
61 ThumbDisassembler(const MCSubtargetInfo &STI) :
62 MCDisassembler(STI) {
6263 }
6364
6465 ~ThumbDisassembler() {
295296 #include "ARMGenInstrInfo.inc"
296297 #include "ARMGenEDInfo.inc"
297298
298 static MCDisassembler *createARMDisassembler(const Target &T) {
299 return new ARMDisassembler;
300 }
301
302 static MCDisassembler *createThumbDisassembler(const Target &T) {
303 return new ThumbDisassembler;
299 static MCDisassembler *createARMDisassembler(const Target &T, const MCSubtargetInfo &STI) {
300 return new ARMDisassembler(STI);
301 }
302
303 static MCDisassembler *createThumbDisassembler(const Target &T, const MCSubtargetInfo &STI) {
304 return new ThumbDisassembler(STI);
304305 }
305306
306307 EDInstInfo *ARMDisassembler::getEDInfo() const {
3232 if (imm == 0)
3333 return 32;
3434 return imm;
35 }
36
37
38 ARMInstPrinter::ARMInstPrinter(const MCAsmInfo &MAI,
39 const MCSubtargetInfo &STI) :
40 MCInstPrinter(MAI) {
41 // Initialize the set of available features.
42 setAvailableFeatures(STI.getFeatureBits());
3543 }
3644
3745 StringRef ARMInstPrinter::getOpcodeName(unsigned Opcode) const {
1414 #define ARMINSTPRINTER_H
1515
1616 #include "llvm/MC/MCInstPrinter.h"
17 #include "llvm/MC/MCSubtargetInfo.h"
1718
1819 namespace llvm {
1920
2122
2223 class ARMInstPrinter : public MCInstPrinter {
2324 public:
24 ARMInstPrinter(const MCAsmInfo &MAI)
25 : MCInstPrinter(MAI) {}
25 ARMInstPrinter(const MCAsmInfo &MAI, const MCSubtargetInfo &STI);
2626
2727 virtual void printInst(const MCInst *MI, raw_ostream &O);
2828 virtual StringRef getOpcodeName(unsigned Opcode) const;
163163
164164 static MCInstPrinter *createARMMCInstPrinter(const Target &T,
165165 unsigned SyntaxVariant,
166 const MCAsmInfo &MAI) {
166 const MCAsmInfo &MAI,
167 const MCSubtargetInfo &STI) {
167168 if (SyntaxVariant == 0)
168 return new ARMInstPrinter(MAI);
169 return new ARMInstPrinter(MAI, STI);
169170 return 0;
170171 }
171172
695695 return Success;
696696 }
697697
698 static MCDisassembler *createMBlazeDisassembler(const Target &T) {
699 return new MBlazeDisassembler;
698 static MCDisassembler *createMBlazeDisassembler(const Target &T,
699 const MCSubtargetInfo &STI) {
700 return new MBlazeDisassembler(STI);
700701 }
701702
702703 extern "C" void LLVMInitializeMBlazeDisassembler() {
3131 public:
3232 /// Constructor - Initializes the disassembler.
3333 ///
34 MBlazeDisassembler() :
35 MCDisassembler() {
34 MBlazeDisassembler(const MCSubtargetInfo &STI) :
35 MCDisassembler(STI) {
3636 }
3737
3838 ~MBlazeDisassembler() {
9494
9595 static MCInstPrinter *createMBlazeMCInstPrinter(const Target &T,
9696 unsigned SyntaxVariant,
97 const MCAsmInfo &MAI) {
97 const MCAsmInfo &MAI,
98 const MCSubtargetInfo &STI) {
9899 if (SyntaxVariant == 0)
99100 return new MBlazeInstPrinter(MAI);
100101 return 0;
2121 class MSP430InstPrinter : public MCInstPrinter {
2222 public:
2323 MSP430InstPrinter(const MCAsmInfo &MAI)
24 : MCInstPrinter(MAI) {}
24 : MCInstPrinter(MAI) {}
2525
2626 virtual void printInst(const MCInst *MI, raw_ostream &O);
2727
5858
5959 static MCInstPrinter *createMSP430MCInstPrinter(const Target &T,
6060 unsigned SyntaxVariant,
61 const MCAsmInfo &MAI) {
61 const MCAsmInfo &MAI,
62 const MCSubtargetInfo &STI) {
6263 if (SyntaxVariant == 0)
6364 return new MSP430InstPrinter(MAI);
6465 return 0;
7777
7878 static MCInstPrinter *createMipsMCInstPrinter(const Target &T,
7979 unsigned SyntaxVariant,
80 const MCAsmInfo &MAI) {
80 const MCAsmInfo &MAI,
81 const MCSubtargetInfo &STI) {
8182 return new MipsInstPrinter(MAI);
8283 }
8384
104104
105105 static MCInstPrinter *createPPCMCInstPrinter(const Target &T,
106106 unsigned SyntaxVariant,
107 const MCAsmInfo &MAI) {
107 const MCAsmInfo &MAI,
108 const MCSubtargetInfo &STI) {
108109 return new PPCInstPrinter(MAI, SyntaxVariant);
109110 }
110111
2020 #include "llvm/MC/MCDisassembler.h"
2121 #include "llvm/MC/MCDisassembler.h"
2222 #include "llvm/MC/MCInst.h"
23 #include "llvm/MC/MCSubtargetInfo.h"
2324 #include "llvm/Support/Debug.h"
2425 #include "llvm/Support/MemoryObject.h"
2526 #include "llvm/Support/TargetRegistry.h"
6566 static bool translateInstruction(MCInst &target,
6667 InternalInstruction &source);
6768
68 X86GenericDisassembler::X86GenericDisassembler(DisassemblerMode mode) :
69 MCDisassembler(),
69 X86GenericDisassembler::X86GenericDisassembler(const MCSubtargetInfo &STI, DisassemblerMode mode) :
70 MCDisassembler(STI),
7071 fMode(mode) {
7172 }
7273
577578 return false;
578579 }
579580
580 static MCDisassembler *createX86_32Disassembler(const Target &T) {
581 return new X86Disassembler::X86_32Disassembler;
582 }
583
584 static MCDisassembler *createX86_64Disassembler(const Target &T) {
585 return new X86Disassembler::X86_64Disassembler;
581 static MCDisassembler *createX86_32Disassembler(const Target &T, const MCSubtargetInfo &STI) {
582 return new X86Disassembler::X86_32Disassembler(STI);
583 }
584
585 static MCDisassembler *createX86_64Disassembler(const Target &T, const MCSubtargetInfo &STI) {
586 return new X86Disassembler::X86_64Disassembler(STI);
586587 }
587588
588589 extern "C" void LLVMInitializeX86Disassembler() {
9191 namespace llvm {
9292
9393 class MCInst;
94 class MCSubtargetInfo;
9495 class MemoryObject;
9596 class raw_ostream;
9697
106107 /// Constructor - Initializes the disassembler.
107108 ///
108109 /// @param mode - The X86 architecture mode to decode for.
109 X86GenericDisassembler(DisassemblerMode mode);
110 X86GenericDisassembler(const MCSubtargetInfo &STI, DisassemblerMode mode);
110111 public:
111112 ~X86GenericDisassembler();
112113
126127 /// X86_16Disassembler - 16-bit X86 disassembler.
127128 class X86_16Disassembler : public X86GenericDisassembler {
128129 public:
129 X86_16Disassembler() :
130 X86GenericDisassembler(MODE_16BIT) {
130 X86_16Disassembler(const MCSubtargetInfo &STI) :
131 X86GenericDisassembler(STI, MODE_16BIT) {
131132 }
132133 };
133134
134135 /// X86_16Disassembler - 32-bit X86 disassembler.
135136 class X86_32Disassembler : public X86GenericDisassembler {
136137 public:
137 X86_32Disassembler() :
138 X86GenericDisassembler(MODE_32BIT) {
138 X86_32Disassembler(const MCSubtargetInfo &STI) :
139 X86GenericDisassembler(STI, MODE_32BIT) {
139140 }
140141 };
141142
142143 /// X86_16Disassembler - 64-bit X86 disassembler.
143144 class X86_64Disassembler : public X86GenericDisassembler {
144145 public:
145 X86_64Disassembler() :
146 X86GenericDisassembler(MODE_64BIT) {
146 X86_64Disassembler(const MCSubtargetInfo &STI) :
147 X86GenericDisassembler(STI, MODE_64BIT) {
147148 }
148149 };
149150
393393
394394 static MCInstPrinter *createX86MCInstPrinter(const Target &T,
395395 unsigned SyntaxVariant,
396 const MCAsmInfo &MAI) {
396 const MCAsmInfo &MAI,
397 const MCSubtargetInfo &STI) {
397398 if (SyntaxVariant == 0)
398399 return new X86ATTInstPrinter(MAI);
399400 if (SyntaxVariant == 1)
2020 #include "llvm/MC/MCDisassembler.h"
2121 #include "llvm/MC/MCInst.h"
2222 #include "llvm/MC/MCInstPrinter.h"
23 #include "llvm/MC/MCSubtargetInfo.h"
2324 #include "llvm/ADT/OwningPtr.h"
2425 #include "llvm/ADT/Triple.h"
2526 #include "llvm/ADT/Twine.h"
139140
140141 int Disassembler::disassemble(const Target &T,
141142 const std::string &Triple,
143 const std::string &Cpu,
144 const std::string &FeaturesStr,
142145 MemoryBuffer &Buffer,
143146 raw_ostream &Out) {
144147 // Set up disassembler.
149152 return -1;
150153 }
151154
152 OwningPtrDisassembler> DisAsm(T.createMCDisassembler());
155 OwningPtrSubtargetInfo> STI(T.createMCSubtargetInfo(Triple, Cpu, FeaturesStr));
156 if (!STI) {
157 errs() << "error: no subtarget info for target " << Triple << "\n";
158 return -1;
159 }
160
161 OwningPtr DisAsm(T.createMCDisassembler(*STI));
153162 if (!DisAsm) {
154163 errs() << "error: no disassembler for target " << Triple << "\n";
155164 return -1;
157166
158167 int AsmPrinterVariant = AsmInfo->getAssemblerDialect();
159168 OwningPtr IP(T.createMCInstPrinter(AsmPrinterVariant,
160 *AsmInfo));
169 *AsmInfo, *STI));
161170 if (!IP) {
162171 errs() << "error: no instruction printer for target " << Triple << '\n';
163172 return -1;
2626 public:
2727 static int disassemble(const Target &target,
2828 const std::string &tripleString,
29 const std::string &Cpu,
30 const std::string &FeaturesStr,
2931 MemoryBuffer &buffer,
3032 raw_ostream &Out);
3133
106106 cl::desc("Target a specific cpu type (-mcpu=help for details)"),
107107 cl::value_desc("cpu-name"),
108108 cl::init(""));
109
110 static cl::list
111 MAttrs("mattr",
112 cl::CommaSeparated,
113 cl::desc("Target specific attributes (-mattr=help for details)"),
114 cl::value_desc("a1,+a2,-a3,..."));
109115
110116 static cl::opt
111117 RelocModel("relocation-model",
360366 llvm::OwningPtr MRI(TheTarget->createMCRegInfo(TripleName));
361367 assert(MRI && "Unable to create target register info!");
362368
363 // Package up features to be passed to target/subtarget
364 std::string FeaturesStr;
365
366369 // FIXME: This is not pretty. MCContext has a ptr to MCObjectFileInfo and
367370 // MCObjectFileInfo needs a MCContext reference in order to initialize itself.
368371 OwningPtr MOFI(new MCObjectFileInfo());
372375 if (SaveTempLabels)
373376 Ctx.setAllowTemporaryLabels(false);
374377
378 // Package up features to be passed to target/subtarget
379 std::string FeaturesStr;
380 if (MAttrs.size()) {
381 SubtargetFeatures Features;
382 for (unsigned i = 0; i != MAttrs.size(); ++i)
383 Features.AddFeature(MAttrs[i]);
384 FeaturesStr = Features.getString();
385 }
386
375387 OwningPtr Out(GetOutputStream());
376388 if (!Out)
377389 return 1;
386398 // FIXME: There is a bit of code duplication with addPassesToEmitFile.
387399 if (FileType == OFT_AssemblyFile) {
388400 MCInstPrinter *IP =
389 TheTarget->createMCInstPrinter(OutputAsmVariant, *MAI);
401 TheTarget->createMCInstPrinter(OutputAsmVariant, *MAI, *STI);
390402 MCCodeEmitter *CE = 0;
391403 MCAsmBackend *MAB = 0;
392404 if (ShowEncoding) {
452464 Res =
453465 Disassembler::disassembleEnhanced(TripleName, *Buffer.take(), Out->os());
454466 } else {
455 Res = Disassembler::disassemble(*TheTarget, TripleName,
467 // Package up features to be passed to target/subtarget
468 std::string FeaturesStr;
469 if (MAttrs.size()) {
470 SubtargetFeatures Features;
471 for (unsigned i = 0; i != MAttrs.size(); ++i)
472 Features.AddFeature(MAttrs[i]);
473 FeaturesStr = Features.getString();
474 }
475
476 Res = Disassembler::disassemble(*TheTarget, TripleName, MCPU, FeaturesStr,
456477 *Buffer.take(), Out->os());
457478 }
458479
2424 #include "llvm/MC/MCInstrAnalysis.h"
2525 #include "llvm/MC/MCInstrDesc.h"
2626 #include "llvm/MC/MCInstrInfo.h"
27 #include "llvm/MC/MCSubtargetInfo.h"
2728 #include "llvm/Support/CommandLine.h"
2829 #include "llvm/Support/Debug.h"
2930 #include "llvm/Support/Format.h"
216217 return;
217218 }
218219
219 OwningPtrDisassembler> DisAsm(TheTarget->createMCDisassembler());
220 OwningPtrSubtargetInfo> STI(TheTarget->createMCSubtargetInfo(TripleName, "", ""));
221
222 if (!STI) {
223 errs() << "error: no subtarget info for target " << TripleName << "\n";
224 return;
225 }
226
227 OwningPtr DisAsm(TheTarget->createMCDisassembler(*STI));
220228 if (!DisAsm) {
221229 errs() << "error: no disassembler for target " << TripleName << "\n";
222230 return;
224232
225233 int AsmPrinterVariant = AsmInfo->getAssemblerDialect();
226234 OwningPtr IP(TheTarget->createMCInstPrinter(
227 AsmPrinterVariant, *AsmInfo));
235 AsmPrinterVariant, *AsmInfo, *STI));
228236 if (!IP) {
229237 errs() << "error: no instruction printer for target " << TripleName << '\n';
230238 return;