1 //===-- LLVMTargetMachine.cpp - Implement the LLVMTargetMachine class -----===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements the LLVMTargetMachine class.
12 //===----------------------------------------------------------------------===//
14 #include "llvm/Target/TargetMachine.h"
15 #include "llvm/PassManager.h"
16 #include "llvm/CodeGen/AsmPrinter.h"
17 #include "llvm/CodeGen/Passes.h"
18 #include "llvm/Target/TargetOptions.h"
19 #include "llvm/MC/MCAsmInfo.h"
20 #include "llvm/MC/MCContext.h"
21 #include "llvm/MC/MCInstrInfo.h"
22 #include "llvm/MC/MCStreamer.h"
23 #include "llvm/MC/MCSubtargetInfo.h"
24 #include "llvm/Target/TargetInstrInfo.h"
25 #include "llvm/Target/TargetSubtargetInfo.h"
26 #include "llvm/ADT/OwningPtr.h"
27 #include "llvm/Support/CommandLine.h"
28 #include "llvm/Support/FormattedStream.h"
29 #include "llvm/Support/ErrorHandling.h"
30 #include "llvm/Support/TargetRegistry.h"
33 static cl::opt<bool> ShowMCEncoding("show-mc-encoding", cl::Hidden,
34 cl::desc("Show encoding in .s output"));
35 static cl::opt<bool> ShowMCInst("show-mc-inst", cl::Hidden,
36 cl::desc("Show instruction structure in .s output"));
38 static cl::opt<cl::boolOrDefault>
39 AsmVerbose("asm-verbose", cl::desc("Add comments to directives."),
40 cl::init(cl::BOU_UNSET));
42 static bool getVerboseAsm() {
44 case cl::BOU_UNSET: return TargetMachine::getAsmVerbosityDefault();
45 case cl::BOU_TRUE: return true;
46 case cl::BOU_FALSE: return false;
48 llvm_unreachable("Invalid verbose asm state");
51 LLVMTargetMachine::LLVMTargetMachine(const Target &T, StringRef Triple,
52 StringRef CPU, StringRef FS,
53 TargetOptions Options,
54 Reloc::Model RM, CodeModel::Model CM,
56 : TargetMachine(T, Triple, CPU, FS, Options) {
57 CodeGenInfo = T.createMCCodeGenInfo(Triple, RM, CM, OL);
58 AsmInfo = T.createMCAsmInfo(Triple);
59 // TargetSelect.h moved to a different directory between LLVM 2.9 and 3.0,
60 // and if the old one gets included then MCAsmInfo will be NULL and
62 // Provide the user with a useful error message about what's wrong.
63 assert(AsmInfo && "MCAsmInfo not initialized."
64 "Make sure you include the correct TargetSelect.h"
65 "and that InitializeAllTargetMCs() is being invoked!");
68 bool LLVMTargetMachine::addPassesToEmitFile(PassManagerBase &PM,
69 formatted_raw_ostream &Out,
70 CodeGenFileType FileType,
72 // Add common CodeGen passes.
73 MCContext *Context = 0;
74 TargetPassConfig *PassConfig = createPassConfig(PM, DisableVerify);
76 if (PassConfig->addCodeGenPasses(Context))
78 assert(Context != 0 && "Failed to get MCContext");
80 if (hasMCSaveTempLabels())
81 Context->setAllowTemporaryLabels(false);
83 const MCAsmInfo &MAI = *getMCAsmInfo();
84 const MCSubtargetInfo &STI = getSubtarget<MCSubtargetInfo>();
85 OwningPtr<MCStreamer> AsmStreamer;
88 case CGFT_AssemblyFile: {
89 MCInstPrinter *InstPrinter =
90 getTarget().createMCInstPrinter(MAI.getAssemblerDialect(), MAI, STI);
92 // Create a code emitter if asked to show the encoding.
93 MCCodeEmitter *MCE = 0;
94 MCAsmBackend *MAB = 0;
96 const MCSubtargetInfo &STI = getSubtarget<MCSubtargetInfo>();
97 MCE = getTarget().createMCCodeEmitter(*getInstrInfo(), STI, *Context);
98 MAB = getTarget().createMCAsmBackend(getTargetTriple());
101 MCStreamer *S = getTarget().createAsmStreamer(*Context, Out,
105 hasMCUseDwarfDirectory(),
109 AsmStreamer.reset(S);
112 case CGFT_ObjectFile: {
113 // Create the code emitter for the target if it exists. If not, .o file
115 MCCodeEmitter *MCE = getTarget().createMCCodeEmitter(*getInstrInfo(), STI,
117 MCAsmBackend *MAB = getTarget().createMCAsmBackend(getTargetTriple());
118 if (MCE == 0 || MAB == 0)
121 AsmStreamer.reset(getTarget().createMCObjectStreamer(getTargetTriple(),
123 MCE, hasMCRelaxAll(),
124 hasMCNoExecStack()));
125 AsmStreamer.get()->InitSections();
129 // The Null output is intended for use for performance analysis and testing,
131 AsmStreamer.reset(createNullStreamer(*Context));
135 // Create the AsmPrinter, which takes ownership of AsmStreamer if successful.
136 FunctionPass *Printer = getTarget().createAsmPrinter(*this, *AsmStreamer);
140 // If successful, createAsmPrinter took ownership of AsmStreamer.
145 PM.add(createGCInfoDeleter());
149 /// addPassesToEmitMachineCode - Add passes to the specified pass manager to
150 /// get machine code emitted. This uses a JITCodeEmitter object to handle
151 /// actually outputting the machine code and resolving things like the address
152 /// of functions. This method should returns true if machine code emission is
155 bool LLVMTargetMachine::addPassesToEmitMachineCode(PassManagerBase &PM,
157 bool DisableVerify) {
158 // Add common CodeGen passes.
160 OwningPtr<TargetPassConfig> PassConfig(createPassConfig(PM, DisableVerify));
161 if (PassConfig->addCodeGenPasses(Ctx))
164 addCodeEmitter(PM, JCE);
165 PM.add(createGCInfoDeleter());
167 return false; // success!
170 /// addPassesToEmitMC - Add passes to the specified pass manager to get
171 /// machine code emitted with the MCJIT. This method returns true if machine
172 /// code is not supported. It fills the MCContext Ctx pointer which can be
173 /// used to build custom MCStreamer.
175 bool LLVMTargetMachine::addPassesToEmitMC(PassManagerBase &PM,
178 bool DisableVerify) {
179 // Add common CodeGen passes.
180 OwningPtr<TargetPassConfig> PassConfig(createPassConfig(PM, DisableVerify));
181 if (PassConfig->addCodeGenPasses(Ctx))
184 if (hasMCSaveTempLabels())
185 Ctx->setAllowTemporaryLabels(false);
187 // Create the code emitter for the target if it exists. If not, .o file
189 const MCSubtargetInfo &STI = getSubtarget<MCSubtargetInfo>();
190 MCCodeEmitter *MCE = getTarget().createMCCodeEmitter(*getInstrInfo(),STI, *Ctx);
191 MCAsmBackend *MAB = getTarget().createMCAsmBackend(getTargetTriple());
192 if (MCE == 0 || MAB == 0)
195 OwningPtr<MCStreamer> AsmStreamer;
196 AsmStreamer.reset(getTarget().createMCObjectStreamer(getTargetTriple(), *Ctx,
199 hasMCNoExecStack()));
200 AsmStreamer.get()->InitSections();
202 // Create the AsmPrinter, which takes ownership of AsmStreamer if successful.
203 FunctionPass *Printer = getTarget().createAsmPrinter(*this, *AsmStreamer);
207 // If successful, createAsmPrinter took ownership of AsmStreamer.
212 return false; // success!