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"
35 inline TargetMachine *unwrap(LLVMTargetMachineRef P) {
36 return reinterpret_cast<TargetMachine*>(P);
38 inline Target *unwrap(LLVMTargetRef P) {
39 return reinterpret_cast<Target*>(P);
41 inline LLVMTargetMachineRef wrap(const TargetMachine *P) {
43 reinterpret_cast<LLVMTargetMachineRef>(const_cast<TargetMachine*>(P));
45 inline LLVMTargetRef wrap(const Target * P) {
46 return reinterpret_cast<LLVMTargetRef>(const_cast<Target*>(P));
49 LLVMTargetRef LLVMGetFirstTarget() {
50 if (TargetRegistry::targets().begin() == TargetRegistry::targets().end()) {
54 const Target *target = &*TargetRegistry::targets().begin();
57 LLVMTargetRef LLVMGetNextTarget(LLVMTargetRef T) {
58 return wrap(unwrap(T)->getNext());
61 LLVMTargetRef LLVMGetTargetFromName(const char *Name) {
62 StringRef NameRef = Name;
63 auto I = std::find_if(
64 TargetRegistry::targets().begin(), TargetRegistry::targets().end(),
65 [&](const Target &T) { return T.getName() == NameRef; });
66 return I != TargetRegistry::targets().end() ? wrap(&*I) : nullptr;
69 LLVMBool LLVMGetTargetFromTriple(const char* TripleStr, LLVMTargetRef *T,
70 char **ErrorMessage) {
73 *T = wrap(TargetRegistry::lookupTarget(TripleStr, Error));
77 *ErrorMessage = strdup(Error.c_str());
85 const char * LLVMGetTargetName(LLVMTargetRef T) {
86 return unwrap(T)->getName();
89 const char * LLVMGetTargetDescription(LLVMTargetRef T) {
90 return unwrap(T)->getShortDescription();
93 LLVMBool LLVMTargetHasJIT(LLVMTargetRef T) {
94 return unwrap(T)->hasJIT();
97 LLVMBool LLVMTargetHasTargetMachine(LLVMTargetRef T) {
98 return unwrap(T)->hasTargetMachine();
101 LLVMBool LLVMTargetHasAsmBackend(LLVMTargetRef T) {
102 return unwrap(T)->hasMCAsmBackend();
105 LLVMTargetMachineRef LLVMCreateTargetMachine(LLVMTargetRef T,
106 const char* Triple, const char* CPU, const char* Features,
107 LLVMCodeGenOptLevel Level, LLVMRelocMode Reloc,
108 LLVMCodeModel CodeModel) {
111 case LLVMRelocStatic:
117 case LLVMRelocDynamicNoPic:
118 RM = Reloc::DynamicNoPIC;
125 CodeModel::Model CM = unwrap(CodeModel);
127 CodeGenOpt::Level OL;
129 case LLVMCodeGenLevelNone:
130 OL = CodeGenOpt::None;
132 case LLVMCodeGenLevelLess:
133 OL = CodeGenOpt::Less;
135 case LLVMCodeGenLevelAggressive:
136 OL = CodeGenOpt::Aggressive;
139 OL = CodeGenOpt::Default;
144 return wrap(unwrap(T)->createTargetMachine(Triple, CPU, Features, opt, RM,
149 void LLVMDisposeTargetMachine(LLVMTargetMachineRef T) {
153 LLVMTargetRef LLVMGetTargetMachineTarget(LLVMTargetMachineRef T) {
154 const Target* target = &(unwrap(T)->getTarget());
158 char* LLVMGetTargetMachineTriple(LLVMTargetMachineRef T) {
159 std::string StringRep = unwrap(T)->getTargetTriple().str();
160 return strdup(StringRep.c_str());
163 char* LLVMGetTargetMachineCPU(LLVMTargetMachineRef T) {
164 std::string StringRep = unwrap(T)->getTargetCPU();
165 return strdup(StringRep.c_str());
168 char* LLVMGetTargetMachineFeatureString(LLVMTargetMachineRef T) {
169 std::string StringRep = unwrap(T)->getTargetFeatureString();
170 return strdup(StringRep.c_str());
173 LLVMTargetDataRef LLVMGetTargetMachineData(LLVMTargetMachineRef T) {
174 return wrap(unwrap(T)->getDataLayout());
177 void LLVMSetTargetMachineAsmVerbosity(LLVMTargetMachineRef T,
178 LLVMBool VerboseAsm) {
179 unwrap(T)->Options.MCOptions.AsmVerbose = VerboseAsm;
182 static LLVMBool LLVMTargetMachineEmit(LLVMTargetMachineRef T, LLVMModuleRef M,
183 raw_pwrite_stream &OS,
184 LLVMCodeGenFileType codegen,
185 char **ErrorMessage) {
186 TargetMachine* TM = unwrap(T);
187 Module* Mod = unwrap(M);
189 legacy::PassManager pass;
193 const DataLayout *td = TM->getDataLayout();
196 error = "No DataLayout in TargetMachine";
197 *ErrorMessage = strdup(error.c_str());
200 Mod->setDataLayout(*td);
202 TargetMachine::CodeGenFileType ft;
204 case LLVMAssemblyFile:
205 ft = TargetMachine::CGFT_AssemblyFile;
208 ft = TargetMachine::CGFT_ObjectFile;
211 if (TM->addPassesToEmitFile(pass, OS, ft)) {
212 error = "TargetMachine can't emit a file of this type";
213 *ErrorMessage = strdup(error.c_str());
223 LLVMBool LLVMTargetMachineEmitToFile(LLVMTargetMachineRef T, LLVMModuleRef M,
224 char* Filename, LLVMCodeGenFileType codegen, char** ErrorMessage) {
226 raw_fd_ostream dest(Filename, EC, sys::fs::F_None);
228 *ErrorMessage = strdup(EC.message().c_str());
231 bool Result = LLVMTargetMachineEmit(T, M, dest, codegen, ErrorMessage);
236 LLVMBool LLVMTargetMachineEmitToMemoryBuffer(LLVMTargetMachineRef T,
237 LLVMModuleRef M, LLVMCodeGenFileType codegen, char** ErrorMessage,
238 LLVMMemoryBufferRef *OutMemBuf) {
239 SmallString<0> CodeString;
240 raw_svector_ostream OStream(CodeString);
241 bool Result = LLVMTargetMachineEmit(T, M, OStream, codegen, ErrorMessage);
244 StringRef Data = OStream.str();
246 LLVMCreateMemoryBufferWithMemoryRangeCopy(Data.data(), Data.size(), "");
250 char *LLVMGetDefaultTargetTriple(void) {
251 return strdup(sys::getDefaultTargetTriple().c_str());
254 void LLVMAddAnalysisPasses(LLVMTargetMachineRef T, LLVMPassManagerRef PM) {
256 createTargetTransformInfoWrapperPass(unwrap(T)->getTargetIRAnalysis()));