Fix ODR violation. NFC.
[oota-llvm.git] / lib / Target / TargetMachineC.cpp
1 //===-- TargetMachine.cpp -------------------------------------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file implements the LLVM-C part of TargetMachine.h
11 //
12 //===----------------------------------------------------------------------===//
13
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"
29 #include <cassert>
30 #include <cstdlib>
31 #include <cstring>
32
33 using namespace llvm;
34
35
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;
42   DataLayout DL;
43 };
44
45
46 static TargetMachine *unwrap(LLVMTargetMachineRef P) {
47   return P->Machine.get();
48 }
49 static Target *unwrap(LLVMTargetRef P) {
50   return reinterpret_cast<Target*>(P);
51 }
52 static LLVMTargetMachineRef wrap(const TargetMachine *P) {
53   return new LLVMOpaqueTargetMachine{ std::unique_ptr<TargetMachine>(const_cast<TargetMachine*>(P)),  P->createDataLayout() };
54 }
55 static LLVMTargetRef wrap(const Target * P) {
56   return reinterpret_cast<LLVMTargetRef>(const_cast<Target*>(P));
57 }
58
59 LLVMTargetRef LLVMGetFirstTarget() {
60   if (TargetRegistry::targets().begin() == TargetRegistry::targets().end()) {
61     return nullptr;
62   }
63
64   const Target *target = &*TargetRegistry::targets().begin();
65   return wrap(target);
66 }
67 LLVMTargetRef LLVMGetNextTarget(LLVMTargetRef T) {
68   return wrap(unwrap(T)->getNext());
69 }
70
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;
77 }
78
79 LLVMBool LLVMGetTargetFromTriple(const char* TripleStr, LLVMTargetRef *T,
80                                  char **ErrorMessage) {
81   std::string Error;
82   
83   *T = wrap(TargetRegistry::lookupTarget(TripleStr, Error));
84   
85   if (!*T) {
86     if (ErrorMessage)
87       *ErrorMessage = strdup(Error.c_str());
88
89     return 1;
90   }
91   
92   return 0;
93 }
94
95 const char * LLVMGetTargetName(LLVMTargetRef T) {
96   return unwrap(T)->getName();
97 }
98
99 const char * LLVMGetTargetDescription(LLVMTargetRef T) {
100   return unwrap(T)->getShortDescription();
101 }
102
103 LLVMBool LLVMTargetHasJIT(LLVMTargetRef T) {
104   return unwrap(T)->hasJIT();
105 }
106
107 LLVMBool LLVMTargetHasTargetMachine(LLVMTargetRef T) {
108   return unwrap(T)->hasTargetMachine();
109 }
110
111 LLVMBool LLVMTargetHasAsmBackend(LLVMTargetRef T) {
112   return unwrap(T)->hasMCAsmBackend();
113 }
114
115 LLVMTargetMachineRef LLVMCreateTargetMachine(LLVMTargetRef T,
116         const char* Triple, const char* CPU, const char* Features,
117         LLVMCodeGenOptLevel Level, LLVMRelocMode Reloc,
118         LLVMCodeModel CodeModel) {
119   Reloc::Model RM;
120   switch (Reloc){
121     case LLVMRelocStatic:
122       RM = Reloc::Static;
123       break;
124     case LLVMRelocPIC:
125       RM = Reloc::PIC_;
126       break;
127     case LLVMRelocDynamicNoPic:
128       RM = Reloc::DynamicNoPIC;
129       break;
130     default:
131       RM = Reloc::Default;
132       break;
133   }
134
135   CodeModel::Model CM = unwrap(CodeModel);
136
137   CodeGenOpt::Level OL;
138   switch (Level) {
139     case LLVMCodeGenLevelNone:
140       OL = CodeGenOpt::None;
141       break;
142     case LLVMCodeGenLevelLess:
143       OL = CodeGenOpt::Less;
144       break;
145     case LLVMCodeGenLevelAggressive:
146       OL = CodeGenOpt::Aggressive;
147       break;
148     default:
149       OL = CodeGenOpt::Default;
150       break;
151   }
152
153   TargetOptions opt;
154   return wrap(unwrap(T)->createTargetMachine(Triple, CPU, Features, opt, RM,
155     CM, OL));
156 }
157
158
159 void LLVMDisposeTargetMachine(LLVMTargetMachineRef T) {
160   delete T;
161 }
162
163 LLVMTargetRef LLVMGetTargetMachineTarget(LLVMTargetMachineRef T) {
164   const Target* target = &(unwrap(T)->getTarget());
165   return wrap(target);
166 }
167
168 char* LLVMGetTargetMachineTriple(LLVMTargetMachineRef T) {
169   std::string StringRep = unwrap(T)->getTargetTriple().str();
170   return strdup(StringRep.c_str());
171 }
172
173 char* LLVMGetTargetMachineCPU(LLVMTargetMachineRef T) {
174   std::string StringRep = unwrap(T)->getTargetCPU();
175   return strdup(StringRep.c_str());
176 }
177
178 char* LLVMGetTargetMachineFeatureString(LLVMTargetMachineRef T) {
179   std::string StringRep = unwrap(T)->getTargetFeatureString();
180   return strdup(StringRep.c_str());
181 }
182
183 /// @deprecated: see "struct LLVMOpaqueTargetMachine" description above
184 LLVMTargetDataRef LLVMGetTargetMachineData(LLVMTargetMachineRef T) {
185   return wrap(&T->DL);
186 }
187
188 void LLVMSetTargetMachineAsmVerbosity(LLVMTargetMachineRef T,
189                                       LLVMBool VerboseAsm) {
190   unwrap(T)->Options.MCOptions.AsmVerbose = VerboseAsm;
191 }
192
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);
199
200   legacy::PassManager pass;
201
202   std::string error;
203
204   Mod->setDataLayout(TM->createDataLayout());
205
206   TargetMachine::CodeGenFileType ft;
207   switch (codegen) {
208     case LLVMAssemblyFile:
209       ft = TargetMachine::CGFT_AssemblyFile;
210       break;
211     default:
212       ft = TargetMachine::CGFT_ObjectFile;
213       break;
214   }
215   if (TM->addPassesToEmitFile(pass, OS, ft)) {
216     error = "TargetMachine can't emit a file of this type";
217     *ErrorMessage = strdup(error.c_str());
218     return true;
219   }
220
221   pass.run(*Mod);
222
223   OS.flush();
224   return false;
225 }
226
227 LLVMBool LLVMTargetMachineEmitToFile(LLVMTargetMachineRef T, LLVMModuleRef M,
228   char* Filename, LLVMCodeGenFileType codegen, char** ErrorMessage) {
229   std::error_code EC;
230   raw_fd_ostream dest(Filename, EC, sys::fs::F_None);
231   if (EC) {
232     *ErrorMessage = strdup(EC.message().c_str());
233     return true;
234   }
235   bool Result = LLVMTargetMachineEmit(T, M, dest, codegen, ErrorMessage);
236   dest.flush();
237   return Result;
238 }
239
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);
246   OStream.flush();
247
248   StringRef Data = OStream.str();
249   *OutMemBuf =
250       LLVMCreateMemoryBufferWithMemoryRangeCopy(Data.data(), Data.size(), "");
251   return Result;
252 }
253
254 char *LLVMGetDefaultTargetTriple(void) {
255   return strdup(sys::getDefaultTargetTriple().c_str());
256 }
257
258 void LLVMAddAnalysisPasses(LLVMTargetMachineRef T, LLVMPassManagerRef PM) {
259   unwrap(PM)->add(
260       createTargetTransformInfoWrapperPass(unwrap(T)->getTargetIRAnalysis()));
261 }