[PM] Remove the old 'PassManager.h' header file at the top level of
[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 inline TargetMachine *unwrap(LLVMTargetMachineRef P) {
36   return reinterpret_cast<TargetMachine*>(P);
37 }
38 inline Target *unwrap(LLVMTargetRef P) {
39   return reinterpret_cast<Target*>(P);
40 }
41 inline LLVMTargetMachineRef wrap(const TargetMachine *P) {
42   return
43     reinterpret_cast<LLVMTargetMachineRef>(const_cast<TargetMachine*>(P));
44 }
45 inline LLVMTargetRef wrap(const Target * P) {
46   return reinterpret_cast<LLVMTargetRef>(const_cast<Target*>(P));
47 }
48
49 LLVMTargetRef LLVMGetFirstTarget() {
50   if(TargetRegistry::begin() == TargetRegistry::end()) {
51     return nullptr;
52   }
53
54   const Target* target = &*TargetRegistry::begin();
55   return wrap(target);
56 }
57 LLVMTargetRef LLVMGetNextTarget(LLVMTargetRef T) {
58   return wrap(unwrap(T)->getNext());
59 }
60
61 LLVMTargetRef LLVMGetTargetFromName(const char *Name) {
62   StringRef NameRef = Name;
63   for (TargetRegistry::iterator IT = TargetRegistry::begin(),
64                                 IE = TargetRegistry::end(); IT != IE; ++IT) {
65     if (IT->getName() == NameRef)
66       return wrap(&*IT);
67   }
68   
69   return nullptr;
70 }
71
72 LLVMBool LLVMGetTargetFromTriple(const char* TripleStr, LLVMTargetRef *T,
73                                  char **ErrorMessage) {
74   std::string Error;
75   
76   *T = wrap(TargetRegistry::lookupTarget(TripleStr, Error));
77   
78   if (!*T) {
79     if (ErrorMessage)
80       *ErrorMessage = strdup(Error.c_str());
81
82     return 1;
83   }
84   
85   return 0;
86 }
87
88 const char * LLVMGetTargetName(LLVMTargetRef T) {
89   return unwrap(T)->getName();
90 }
91
92 const char * LLVMGetTargetDescription(LLVMTargetRef T) {
93   return unwrap(T)->getShortDescription();
94 }
95
96 LLVMBool LLVMTargetHasJIT(LLVMTargetRef T) {
97   return unwrap(T)->hasJIT();
98 }
99
100 LLVMBool LLVMTargetHasTargetMachine(LLVMTargetRef T) {
101   return unwrap(T)->hasTargetMachine();
102 }
103
104 LLVMBool LLVMTargetHasAsmBackend(LLVMTargetRef T) {
105   return unwrap(T)->hasMCAsmBackend();
106 }
107
108 LLVMTargetMachineRef LLVMCreateTargetMachine(LLVMTargetRef T,
109         const char* Triple, const char* CPU, const char* Features,
110         LLVMCodeGenOptLevel Level, LLVMRelocMode Reloc,
111         LLVMCodeModel CodeModel) {
112   Reloc::Model RM;
113   switch (Reloc){
114     case LLVMRelocStatic:
115       RM = Reloc::Static;
116       break;
117     case LLVMRelocPIC:
118       RM = Reloc::PIC_;
119       break;
120     case LLVMRelocDynamicNoPic:
121       RM = Reloc::DynamicNoPIC;
122       break;
123     default:
124       RM = Reloc::Default;
125       break;
126   }
127
128   CodeModel::Model CM = unwrap(CodeModel);
129
130   CodeGenOpt::Level OL;
131   switch (Level) {
132     case LLVMCodeGenLevelNone:
133       OL = CodeGenOpt::None;
134       break;
135     case LLVMCodeGenLevelLess:
136       OL = CodeGenOpt::Less;
137       break;
138     case LLVMCodeGenLevelAggressive:
139       OL = CodeGenOpt::Aggressive;
140       break;
141     default:
142       OL = CodeGenOpt::Default;
143       break;
144   }
145
146   TargetOptions opt;
147   return wrap(unwrap(T)->createTargetMachine(Triple, CPU, Features, opt, RM,
148     CM, OL));
149 }
150
151
152 void LLVMDisposeTargetMachine(LLVMTargetMachineRef T) {
153   delete unwrap(T);
154 }
155
156 LLVMTargetRef LLVMGetTargetMachineTarget(LLVMTargetMachineRef T) {
157   const Target* target = &(unwrap(T)->getTarget());
158   return wrap(target);
159 }
160
161 char* LLVMGetTargetMachineTriple(LLVMTargetMachineRef T) {
162   std::string StringRep = unwrap(T)->getTargetTriple();
163   return strdup(StringRep.c_str());
164 }
165
166 char* LLVMGetTargetMachineCPU(LLVMTargetMachineRef T) {
167   std::string StringRep = unwrap(T)->getTargetCPU();
168   return strdup(StringRep.c_str());
169 }
170
171 char* LLVMGetTargetMachineFeatureString(LLVMTargetMachineRef T) {
172   std::string StringRep = unwrap(T)->getTargetFeatureString();
173   return strdup(StringRep.c_str());
174 }
175
176 LLVMTargetDataRef LLVMGetTargetMachineData(LLVMTargetMachineRef T) {
177   return wrap(unwrap(T)->getDataLayout());
178 }
179
180 void LLVMSetTargetMachineAsmVerbosity(LLVMTargetMachineRef T,
181                                       LLVMBool VerboseAsm) {
182   unwrap(T)->Options.MCOptions.AsmVerbose = VerboseAsm;
183 }
184
185 static LLVMBool LLVMTargetMachineEmit(LLVMTargetMachineRef T, LLVMModuleRef M,
186   formatted_raw_ostream &OS, LLVMCodeGenFileType codegen, char **ErrorMessage) {
187   TargetMachine* TM = unwrap(T);
188   Module* Mod = unwrap(M);
189
190   legacy::PassManager pass;
191
192   std::string error;
193
194   const DataLayout *td = TM->getDataLayout();
195
196   if (!td) {
197     error = "No DataLayout in TargetMachine";
198     *ErrorMessage = strdup(error.c_str());
199     return true;
200   }
201   Mod->setDataLayout(td);
202   pass.add(new DataLayoutPass());
203
204   TargetMachine::CodeGenFileType ft;
205   switch (codegen) {
206     case LLVMAssemblyFile:
207       ft = TargetMachine::CGFT_AssemblyFile;
208       break;
209     default:
210       ft = TargetMachine::CGFT_ObjectFile;
211       break;
212   }
213   if (TM->addPassesToEmitFile(pass, OS, ft)) {
214     error = "TargetMachine can't emit a file of this type";
215     *ErrorMessage = strdup(error.c_str());
216     return true;
217   }
218
219   pass.run(*Mod);
220
221   OS.flush();
222   return false;
223 }
224
225 LLVMBool LLVMTargetMachineEmitToFile(LLVMTargetMachineRef T, LLVMModuleRef M,
226   char* Filename, LLVMCodeGenFileType codegen, char** ErrorMessage) {
227   std::error_code EC;
228   raw_fd_ostream dest(Filename, EC, sys::fs::F_None);
229   if (EC) {
230     *ErrorMessage = strdup(EC.message().c_str());
231     return true;
232   }
233   formatted_raw_ostream destf(dest);
234   bool Result = LLVMTargetMachineEmit(T, M, destf, codegen, ErrorMessage);
235   dest.flush();
236   return Result;
237 }
238
239 LLVMBool LLVMTargetMachineEmitToMemoryBuffer(LLVMTargetMachineRef T,
240   LLVMModuleRef M, LLVMCodeGenFileType codegen, char** ErrorMessage,
241   LLVMMemoryBufferRef *OutMemBuf) {
242   std::string CodeString;
243   raw_string_ostream OStream(CodeString);
244   formatted_raw_ostream Out(OStream);
245   bool Result = LLVMTargetMachineEmit(T, M, Out, codegen, ErrorMessage);
246   OStream.flush();
247
248   std::string &Data = OStream.str();
249   *OutMemBuf = LLVMCreateMemoryBufferWithMemoryRangeCopy(Data.c_str(),
250                                                      Data.length(), "");
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 }