1 //===-- TargetMachine.cpp -------------------------------------------------===//
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 LLVM-C part of TargetMachine.h
12 //===----------------------------------------------------------------------===//
14 #include "llvm-c/TargetMachine.h"
15 #include "llvm-c/Core.h"
16 #include "llvm-c/Target.h"
17 #include "llvm/Analysis/TargetTransformInfo.h"
18 #include "llvm/IR/DataLayout.h"
19 #include "llvm/IR/Module.h"
20 #include "llvm/IR/LegacyPassManager.h"
21 #include "llvm/Support/CodeGen.h"
22 #include "llvm/Support/FileSystem.h"
23 #include "llvm/Support/FormattedStream.h"
24 #include "llvm/Support/Host.h"
25 #include "llvm/Support/TargetRegistry.h"
26 #include "llvm/Support/raw_ostream.h"
27 #include "llvm/Target/TargetMachine.h"
28 #include "llvm/Target/TargetSubtargetInfo.h"
36 // Friend to the TargetMachine, access legacy API that are made private in C++
37 struct C_API_PRIVATE_ACCESS {
38 static const DataLayout &getDataLayout(const TargetMachine &T) {
39 return T.getDataLayout();
44 static TargetMachine *unwrap(LLVMTargetMachineRef P) {
45 return reinterpret_cast<TargetMachine *>(P);
47 static Target *unwrap(LLVMTargetRef P) {
48 return reinterpret_cast<Target*>(P);
50 static LLVMTargetMachineRef wrap(const TargetMachine *P) {
51 return reinterpret_cast<LLVMTargetMachineRef>(const_cast<TargetMachine *>(P));
53 static LLVMTargetRef wrap(const Target * P) {
54 return reinterpret_cast<LLVMTargetRef>(const_cast<Target*>(P));
57 LLVMTargetRef LLVMGetFirstTarget() {
58 if (TargetRegistry::targets().begin() == TargetRegistry::targets().end()) {
62 const Target *target = &*TargetRegistry::targets().begin();
65 LLVMTargetRef LLVMGetNextTarget(LLVMTargetRef T) {
66 return wrap(unwrap(T)->getNext());
69 LLVMTargetRef LLVMGetTargetFromName(const char *Name) {
70 StringRef NameRef = Name;
71 auto I = std::find_if(
72 TargetRegistry::targets().begin(), TargetRegistry::targets().end(),
73 [&](const Target &T) { return T.getName() == NameRef; });
74 return I != TargetRegistry::targets().end() ? wrap(&*I) : nullptr;
77 LLVMBool LLVMGetTargetFromTriple(const char* TripleStr, LLVMTargetRef *T,
78 char **ErrorMessage) {
81 *T = wrap(TargetRegistry::lookupTarget(TripleStr, Error));
85 *ErrorMessage = strdup(Error.c_str());
93 const char * LLVMGetTargetName(LLVMTargetRef T) {
94 return unwrap(T)->getName();
97 const char * LLVMGetTargetDescription(LLVMTargetRef T) {
98 return unwrap(T)->getShortDescription();
101 LLVMBool LLVMTargetHasJIT(LLVMTargetRef T) {
102 return unwrap(T)->hasJIT();
105 LLVMBool LLVMTargetHasTargetMachine(LLVMTargetRef T) {
106 return unwrap(T)->hasTargetMachine();
109 LLVMBool LLVMTargetHasAsmBackend(LLVMTargetRef T) {
110 return unwrap(T)->hasMCAsmBackend();
113 LLVMTargetMachineRef LLVMCreateTargetMachine(LLVMTargetRef T,
114 const char* Triple, const char* CPU, const char* Features,
115 LLVMCodeGenOptLevel Level, LLVMRelocMode Reloc,
116 LLVMCodeModel CodeModel) {
119 case LLVMRelocStatic:
125 case LLVMRelocDynamicNoPic:
126 RM = Reloc::DynamicNoPIC;
133 CodeModel::Model CM = unwrap(CodeModel);
135 CodeGenOpt::Level OL;
137 case LLVMCodeGenLevelNone:
138 OL = CodeGenOpt::None;
140 case LLVMCodeGenLevelLess:
141 OL = CodeGenOpt::Less;
143 case LLVMCodeGenLevelAggressive:
144 OL = CodeGenOpt::Aggressive;
147 OL = CodeGenOpt::Default;
152 return wrap(unwrap(T)->createTargetMachine(Triple, CPU, Features, opt, RM,
156 void LLVMDisposeTargetMachine(LLVMTargetMachineRef T) { delete unwrap(T); }
158 LLVMTargetRef LLVMGetTargetMachineTarget(LLVMTargetMachineRef T) {
159 const Target* target = &(unwrap(T)->getTarget());
163 char* LLVMGetTargetMachineTriple(LLVMTargetMachineRef T) {
164 std::string StringRep = unwrap(T)->getTargetTriple().str();
165 return strdup(StringRep.c_str());
168 char* LLVMGetTargetMachineCPU(LLVMTargetMachineRef T) {
169 std::string StringRep = unwrap(T)->getTargetCPU();
170 return strdup(StringRep.c_str());
173 char* LLVMGetTargetMachineFeatureString(LLVMTargetMachineRef T) {
174 std::string StringRep = unwrap(T)->getTargetFeatureString();
175 return strdup(StringRep.c_str());
178 /** Deprecated: use LLVMGetDataLayout(LLVMModuleRef M) instead. */
179 LLVMTargetDataRef LLVMGetTargetMachineData(LLVMTargetMachineRef T) {
180 return wrap(&C_API_PRIVATE_ACCESS::getDataLayout(*unwrap(T)));
183 void LLVMSetTargetMachineAsmVerbosity(LLVMTargetMachineRef T,
184 LLVMBool VerboseAsm) {
185 unwrap(T)->Options.MCOptions.AsmVerbose = VerboseAsm;
188 static LLVMBool LLVMTargetMachineEmit(LLVMTargetMachineRef T, LLVMModuleRef M,
189 raw_pwrite_stream &OS,
190 LLVMCodeGenFileType codegen,
191 char **ErrorMessage) {
192 TargetMachine* TM = unwrap(T);
193 Module* Mod = unwrap(M);
195 legacy::PassManager pass;
199 Mod->setDataLayout(TM->createDataLayout());
201 TargetMachine::CodeGenFileType ft;
203 case LLVMAssemblyFile:
204 ft = TargetMachine::CGFT_AssemblyFile;
207 ft = TargetMachine::CGFT_ObjectFile;
210 if (TM->addPassesToEmitFile(pass, OS, ft)) {
211 error = "TargetMachine can't emit a file of this type";
212 *ErrorMessage = strdup(error.c_str());
222 LLVMBool LLVMTargetMachineEmitToFile(LLVMTargetMachineRef T, LLVMModuleRef M,
223 char* Filename, LLVMCodeGenFileType codegen, char** ErrorMessage) {
225 raw_fd_ostream dest(Filename, EC, sys::fs::F_None);
227 *ErrorMessage = strdup(EC.message().c_str());
230 bool Result = LLVMTargetMachineEmit(T, M, dest, codegen, ErrorMessage);
235 LLVMBool LLVMTargetMachineEmitToMemoryBuffer(LLVMTargetMachineRef T,
236 LLVMModuleRef M, LLVMCodeGenFileType codegen, char** ErrorMessage,
237 LLVMMemoryBufferRef *OutMemBuf) {
238 SmallString<0> CodeString;
239 raw_svector_ostream OStream(CodeString);
240 bool Result = LLVMTargetMachineEmit(T, M, OStream, codegen, ErrorMessage);
242 StringRef Data = OStream.str();
244 LLVMCreateMemoryBufferWithMemoryRangeCopy(Data.data(), Data.size(), "");
248 char *LLVMGetDefaultTargetTriple(void) {
249 return strdup(sys::getDefaultTargetTriple().c_str());
252 void LLVMAddAnalysisPasses(LLVMTargetMachineRef T, LLVMPassManagerRef PM) {
254 createTargetTransformInfoWrapperPass(unwrap(T)->getTargetIRAnalysis()));