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/Core.h"
15 #include "llvm-c/Target.h"
16 #include "llvm-c/TargetMachine.h"
17 #include "llvm/Analysis/TargetTransformInfo.h"
18 #include "llvm/IR/DataLayout.h"
19 #include "llvm/IR/LegacyPassManager.h"
20 #include "llvm/IR/Module.h"
21 #include "llvm/MC/SubtargetFeature.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/CodeGenCWrappers.h"
28 #include "llvm/Target/TargetMachine.h"
35 static TargetMachine *unwrap(LLVMTargetMachineRef P) {
36 return reinterpret_cast<TargetMachine *>(P);
38 static Target *unwrap(LLVMTargetRef P) {
39 return reinterpret_cast<Target*>(P);
41 static LLVMTargetMachineRef wrap(const TargetMachine *P) {
42 return reinterpret_cast<LLVMTargetMachineRef>(const_cast<TargetMachine *>(P));
44 static LLVMTargetRef wrap(const Target * P) {
45 return reinterpret_cast<LLVMTargetRef>(const_cast<Target*>(P));
48 LLVMTargetRef LLVMGetFirstTarget() {
49 if (TargetRegistry::targets().begin() == TargetRegistry::targets().end()) {
53 const Target *target = &*TargetRegistry::targets().begin();
56 LLVMTargetRef LLVMGetNextTarget(LLVMTargetRef T) {
57 return wrap(unwrap(T)->getNext());
60 LLVMTargetRef LLVMGetTargetFromName(const char *Name) {
61 StringRef NameRef = Name;
62 auto I = find_if(TargetRegistry::targets(),
63 [&](const Target &T) { return T.getName() == NameRef; });
64 return I != TargetRegistry::targets().end() ? wrap(&*I) : nullptr;
67 LLVMBool LLVMGetTargetFromTriple(const char* TripleStr, LLVMTargetRef *T,
68 char **ErrorMessage) {
71 *T = wrap(TargetRegistry::lookupTarget(TripleStr, Error));
75 *ErrorMessage = strdup(Error.c_str());
83 const char * LLVMGetTargetName(LLVMTargetRef T) {
84 return unwrap(T)->getName();
87 const char * LLVMGetTargetDescription(LLVMTargetRef T) {
88 return unwrap(T)->getShortDescription();
91 LLVMBool LLVMTargetHasJIT(LLVMTargetRef T) {
92 return unwrap(T)->hasJIT();
95 LLVMBool LLVMTargetHasTargetMachine(LLVMTargetRef T) {
96 return unwrap(T)->hasTargetMachine();
99 LLVMBool LLVMTargetHasAsmBackend(LLVMTargetRef T) {
100 return unwrap(T)->hasMCAsmBackend();
103 LLVMTargetMachineRef LLVMCreateTargetMachine(LLVMTargetRef T,
104 const char *Triple, const char *CPU, const char *Features,
105 LLVMCodeGenOptLevel Level, LLVMRelocMode Reloc,
106 LLVMCodeModel CodeModel) {
107 Optional<Reloc::Model> RM;
109 case LLVMRelocStatic:
115 case LLVMRelocDynamicNoPic:
116 RM = Reloc::DynamicNoPIC;
124 case LLVMRelocROPI_RWPI:
125 RM = Reloc::ROPI_RWPI;
132 Optional<CodeModel::Model> CM = unwrap(CodeModel, JIT);
134 CodeGenOpt::Level OL;
136 case LLVMCodeGenLevelNone:
137 OL = CodeGenOpt::None;
139 case LLVMCodeGenLevelLess:
140 OL = CodeGenOpt::Less;
142 case LLVMCodeGenLevelAggressive:
143 OL = CodeGenOpt::Aggressive;
146 OL = CodeGenOpt::Default;
151 return wrap(unwrap(T)->createTargetMachine(Triple, CPU, Features, opt, RM, CM,
155 void LLVMDisposeTargetMachine(LLVMTargetMachineRef T) { delete unwrap(T); }
157 LLVMTargetRef LLVMGetTargetMachineTarget(LLVMTargetMachineRef T) {
158 const Target* target = &(unwrap(T)->getTarget());
162 char* LLVMGetTargetMachineTriple(LLVMTargetMachineRef T) {
163 std::string StringRep = unwrap(T)->getTargetTriple().str();
164 return strdup(StringRep.c_str());
167 char* LLVMGetTargetMachineCPU(LLVMTargetMachineRef T) {
168 std::string StringRep = unwrap(T)->getTargetCPU();
169 return strdup(StringRep.c_str());
172 char* LLVMGetTargetMachineFeatureString(LLVMTargetMachineRef T) {
173 std::string StringRep = unwrap(T)->getTargetFeatureString();
174 return strdup(StringRep.c_str());
177 void LLVMSetTargetMachineAsmVerbosity(LLVMTargetMachineRef T,
178 LLVMBool VerboseAsm) {
179 unwrap(T)->Options.MCOptions.AsmVerbose = VerboseAsm;
182 LLVMTargetDataRef LLVMCreateTargetDataLayout(LLVMTargetMachineRef T) {
183 return wrap(new DataLayout(unwrap(T)->createDataLayout()));
186 static LLVMBool LLVMTargetMachineEmit(LLVMTargetMachineRef T, LLVMModuleRef M,
187 raw_pwrite_stream &OS,
188 LLVMCodeGenFileType codegen,
189 char **ErrorMessage) {
190 TargetMachine* TM = unwrap(T);
191 Module* Mod = unwrap(M);
193 legacy::PassManager pass;
197 Mod->setDataLayout(TM->createDataLayout());
199 TargetMachine::CodeGenFileType ft;
201 case LLVMAssemblyFile:
202 ft = TargetMachine::CGFT_AssemblyFile;
205 ft = TargetMachine::CGFT_ObjectFile;
208 if (TM->addPassesToEmitFile(pass, OS, nullptr, ft)) {
209 error = "TargetMachine can't emit a file of this type";
210 *ErrorMessage = strdup(error.c_str());
220 LLVMBool LLVMTargetMachineEmitToFile(LLVMTargetMachineRef T, LLVMModuleRef M,
221 char* Filename, LLVMCodeGenFileType codegen, char** ErrorMessage) {
223 raw_fd_ostream dest(Filename, EC, sys::fs::F_None);
225 *ErrorMessage = strdup(EC.message().c_str());
228 bool Result = LLVMTargetMachineEmit(T, M, dest, codegen, ErrorMessage);
233 LLVMBool LLVMTargetMachineEmitToMemoryBuffer(LLVMTargetMachineRef T,
234 LLVMModuleRef M, LLVMCodeGenFileType codegen, char** ErrorMessage,
235 LLVMMemoryBufferRef *OutMemBuf) {
236 SmallString<0> CodeString;
237 raw_svector_ostream OStream(CodeString);
238 bool Result = LLVMTargetMachineEmit(T, M, OStream, codegen, ErrorMessage);
240 StringRef Data = OStream.str();
242 LLVMCreateMemoryBufferWithMemoryRangeCopy(Data.data(), Data.size(), "");
246 char *LLVMGetDefaultTargetTriple(void) {
247 return strdup(sys::getDefaultTargetTriple().c_str());
250 char *LLVMNormalizeTargetTriple(const char* triple) {
251 return strdup(Triple::normalize(StringRef(triple)).c_str());
254 char *LLVMGetHostCPUName(void) {
255 return strdup(sys::getHostCPUName().data());
258 char *LLVMGetHostCPUFeatures(void) {
259 SubtargetFeatures Features;
260 StringMap<bool> HostFeatures;
262 if (sys::getHostCPUFeatures(HostFeatures))
263 for (auto &F : HostFeatures)
264 Features.AddFeature(F.first(), F.second);
266 return strdup(Features.getString().c_str());
269 void LLVMAddAnalysisPasses(LLVMTargetMachineRef T, LLVMPassManagerRef PM) {
271 createTargetTransformInfoWrapperPass(unwrap(T)->getTargetIRAnalysis()));