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 // The TargetMachine uses to offer access to a DataLayout member. This is reflected
37 // in the C API. For backward compatibility reason, this structure allows to keep
38 // a DataLayout member accessible to C client that have a handle to a
39 // LLVMTargetMachineRef.
40 struct LLVMOpaqueTargetMachine {
41 std::unique_ptr<TargetMachine> Machine;
46 static TargetMachine *unwrap(LLVMTargetMachineRef P) {
47 return P->Machine.get();
49 static Target *unwrap(LLVMTargetRef P) {
50 return reinterpret_cast<Target*>(P);
52 static LLVMTargetMachineRef wrap(const TargetMachine *P) {
53 return new LLVMOpaqueTargetMachine{ std::unique_ptr<TargetMachine>(const_cast<TargetMachine*>(P)), P->createDataLayout() };
55 static LLVMTargetRef wrap(const Target * P) {
56 return reinterpret_cast<LLVMTargetRef>(const_cast<Target*>(P));
59 LLVMTargetRef LLVMGetFirstTarget() {
60 if (TargetRegistry::targets().begin() == TargetRegistry::targets().end()) {
64 const Target *target = &*TargetRegistry::targets().begin();
67 LLVMTargetRef LLVMGetNextTarget(LLVMTargetRef T) {
68 return wrap(unwrap(T)->getNext());
71 LLVMTargetRef LLVMGetTargetFromName(const char *Name) {
72 StringRef NameRef = Name;
73 auto I = std::find_if(
74 TargetRegistry::targets().begin(), TargetRegistry::targets().end(),
75 [&](const Target &T) { return T.getName() == NameRef; });
76 return I != TargetRegistry::targets().end() ? wrap(&*I) : nullptr;
79 LLVMBool LLVMGetTargetFromTriple(const char* TripleStr, LLVMTargetRef *T,
80 char **ErrorMessage) {
83 *T = wrap(TargetRegistry::lookupTarget(TripleStr, Error));
87 *ErrorMessage = strdup(Error.c_str());
95 const char * LLVMGetTargetName(LLVMTargetRef T) {
96 return unwrap(T)->getName();
99 const char * LLVMGetTargetDescription(LLVMTargetRef T) {
100 return unwrap(T)->getShortDescription();
103 LLVMBool LLVMTargetHasJIT(LLVMTargetRef T) {
104 return unwrap(T)->hasJIT();
107 LLVMBool LLVMTargetHasTargetMachine(LLVMTargetRef T) {
108 return unwrap(T)->hasTargetMachine();
111 LLVMBool LLVMTargetHasAsmBackend(LLVMTargetRef T) {
112 return unwrap(T)->hasMCAsmBackend();
115 LLVMTargetMachineRef LLVMCreateTargetMachine(LLVMTargetRef T,
116 const char* Triple, const char* CPU, const char* Features,
117 LLVMCodeGenOptLevel Level, LLVMRelocMode Reloc,
118 LLVMCodeModel CodeModel) {
121 case LLVMRelocStatic:
127 case LLVMRelocDynamicNoPic:
128 RM = Reloc::DynamicNoPIC;
135 CodeModel::Model CM = unwrap(CodeModel);
137 CodeGenOpt::Level OL;
139 case LLVMCodeGenLevelNone:
140 OL = CodeGenOpt::None;
142 case LLVMCodeGenLevelLess:
143 OL = CodeGenOpt::Less;
145 case LLVMCodeGenLevelAggressive:
146 OL = CodeGenOpt::Aggressive;
149 OL = CodeGenOpt::Default;
154 return wrap(unwrap(T)->createTargetMachine(Triple, CPU, Features, opt, RM,
159 void LLVMDisposeTargetMachine(LLVMTargetMachineRef T) {
163 LLVMTargetRef LLVMGetTargetMachineTarget(LLVMTargetMachineRef T) {
164 const Target* target = &(unwrap(T)->getTarget());
168 char* LLVMGetTargetMachineTriple(LLVMTargetMachineRef T) {
169 std::string StringRep = unwrap(T)->getTargetTriple().str();
170 return strdup(StringRep.c_str());
173 char* LLVMGetTargetMachineCPU(LLVMTargetMachineRef T) {
174 std::string StringRep = unwrap(T)->getTargetCPU();
175 return strdup(StringRep.c_str());
178 char* LLVMGetTargetMachineFeatureString(LLVMTargetMachineRef T) {
179 std::string StringRep = unwrap(T)->getTargetFeatureString();
180 return strdup(StringRep.c_str());
183 /// @deprecated: see "struct LLVMOpaqueTargetMachine" description above
184 LLVMTargetDataRef LLVMGetTargetMachineData(LLVMTargetMachineRef T) {
188 void LLVMSetTargetMachineAsmVerbosity(LLVMTargetMachineRef T,
189 LLVMBool VerboseAsm) {
190 unwrap(T)->Options.MCOptions.AsmVerbose = VerboseAsm;
193 static LLVMBool LLVMTargetMachineEmit(LLVMTargetMachineRef T, LLVMModuleRef M,
194 raw_pwrite_stream &OS,
195 LLVMCodeGenFileType codegen,
196 char **ErrorMessage) {
197 TargetMachine* TM = unwrap(T);
198 Module* Mod = unwrap(M);
200 legacy::PassManager pass;
204 Mod->setDataLayout(TM->createDataLayout());
206 TargetMachine::CodeGenFileType ft;
208 case LLVMAssemblyFile:
209 ft = TargetMachine::CGFT_AssemblyFile;
212 ft = TargetMachine::CGFT_ObjectFile;
215 if (TM->addPassesToEmitFile(pass, OS, ft)) {
216 error = "TargetMachine can't emit a file of this type";
217 *ErrorMessage = strdup(error.c_str());
227 LLVMBool LLVMTargetMachineEmitToFile(LLVMTargetMachineRef T, LLVMModuleRef M,
228 char* Filename, LLVMCodeGenFileType codegen, char** ErrorMessage) {
230 raw_fd_ostream dest(Filename, EC, sys::fs::F_None);
232 *ErrorMessage = strdup(EC.message().c_str());
235 bool Result = LLVMTargetMachineEmit(T, M, dest, codegen, ErrorMessage);
240 LLVMBool LLVMTargetMachineEmitToMemoryBuffer(LLVMTargetMachineRef T,
241 LLVMModuleRef M, LLVMCodeGenFileType codegen, char** ErrorMessage,
242 LLVMMemoryBufferRef *OutMemBuf) {
243 SmallString<0> CodeString;
244 raw_svector_ostream OStream(CodeString);
245 bool Result = LLVMTargetMachineEmit(T, M, OStream, codegen, ErrorMessage);
248 StringRef Data = OStream.str();
250 LLVMCreateMemoryBufferWithMemoryRangeCopy(Data.data(), Data.size(), "");
254 char *LLVMGetDefaultTargetTriple(void) {
255 return strdup(sys::getDefaultTargetTriple().c_str());
258 void LLVMAddAnalysisPasses(LLVMTargetMachineRef T, LLVMPassManagerRef PM) {
260 createTargetTransformInfoWrapperPass(unwrap(T)->getTargetIRAnalysis()));