blob: 061d0e9e18995bc7f83f36f7af5797137deb12eb [file] [log] [blame]
Duncan Sands264d2e72012-04-11 10:25:24 +00001//===-- 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
Chandler Carruthed0881b2012-12-03 16:50:05 +000014#include "llvm-c/TargetMachine.h"
Duncan Sands264d2e72012-04-11 10:25:24 +000015#include "llvm-c/Core.h"
16#include "llvm-c/Target.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000017#include "llvm/IR/DataLayout.h"
18#include "llvm/IR/Module.h"
Duncan Sands264d2e72012-04-11 10:25:24 +000019#include "llvm/PassManager.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000020#include "llvm/Support/CodeGen.h"
21#include "llvm/Support/FormattedStream.h"
22#include "llvm/Support/TargetRegistry.h"
23#include "llvm/Support/raw_ostream.h"
Peter Zotov7b61b752013-11-06 10:25:18 +000024#include "llvm/Support/Host.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000025#include "llvm/Target/TargetMachine.h"
Duncan Sands264d2e72012-04-11 10:25:24 +000026#include <cassert>
27#include <cstdlib>
28#include <cstring>
29
30using namespace llvm;
31
Eric Christopher04d4e932013-04-22 22:47:22 +000032inline DataLayout *unwrap(LLVMTargetDataRef P) {
33 return reinterpret_cast<DataLayout*>(P);
34}
Duncan Sands264d2e72012-04-11 10:25:24 +000035
Eric Christopher04d4e932013-04-22 22:47:22 +000036inline LLVMTargetDataRef wrap(const DataLayout *P) {
37 return reinterpret_cast<LLVMTargetDataRef>(const_cast<DataLayout*>(P));
38}
39
40inline TargetLibraryInfo *unwrap(LLVMTargetLibraryInfoRef P) {
41 return reinterpret_cast<TargetLibraryInfo*>(P);
42}
43
44inline LLVMTargetLibraryInfoRef wrap(const TargetLibraryInfo *P) {
45 TargetLibraryInfo *X = const_cast<TargetLibraryInfo*>(P);
46 return reinterpret_cast<LLVMTargetLibraryInfoRef>(X);
47}
48
49inline TargetMachine *unwrap(LLVMTargetMachineRef P) {
50 return reinterpret_cast<TargetMachine*>(P);
51}
52inline Target *unwrap(LLVMTargetRef P) {
53 return reinterpret_cast<Target*>(P);
54}
55inline LLVMTargetMachineRef wrap(const TargetMachine *P) {
56 return
57 reinterpret_cast<LLVMTargetMachineRef>(const_cast<TargetMachine*>(P));
58}
59inline LLVMTargetRef wrap(const Target * P) {
60 return reinterpret_cast<LLVMTargetRef>(const_cast<Target*>(P));
61}
Duncan Sands264d2e72012-04-11 10:25:24 +000062
63LLVMTargetRef LLVMGetFirstTarget() {
Anders Waldenborga89c1e32013-10-17 10:25:24 +000064 if(TargetRegistry::begin() == TargetRegistry::end()) {
65 return NULL;
66 }
67
68 const Target* target = &*TargetRegistry::begin();
69 return wrap(target);
Duncan Sands264d2e72012-04-11 10:25:24 +000070}
71LLVMTargetRef LLVMGetNextTarget(LLVMTargetRef T) {
72 return wrap(unwrap(T)->getNext());
73}
74
Peter Zotovb2c8b8a2013-11-15 02:51:01 +000075LLVMTargetRef LLVMGetTargetFromName(const char *Name) {
Peter Zotov7b61b752013-11-06 10:25:18 +000076 for (TargetRegistry::iterator IT = TargetRegistry::begin(),
77 IE = TargetRegistry::end(); IT != IE; ++IT) {
Peter Zotovb2c8b8a2013-11-15 02:51:01 +000078 if (IT->getName() == Name)
79 return wrap(&*IT);
Peter Zotov7b61b752013-11-06 10:25:18 +000080 }
81
Peter Zotovb2c8b8a2013-11-15 02:51:01 +000082 return NULL;
Peter Zotov7b61b752013-11-06 10:25:18 +000083}
84
85LLVMBool LLVMGetTargetFromTriple(const char* TripleStr, LLVMTargetRef *T,
86 char **ErrorMessage) {
87 std::string Error;
88
89 *T = wrap(TargetRegistry::lookupTarget(TripleStr, Error));
90
91 if (!*T) {
92 if (ErrorMessage)
93 *ErrorMessage = strdup(Error.c_str());
94
95 return 1;
96 }
97
98 return 0;
99}
100
Duncan Sands264d2e72012-04-11 10:25:24 +0000101const char * LLVMGetTargetName(LLVMTargetRef T) {
102 return unwrap(T)->getName();
103}
104
105const char * LLVMGetTargetDescription(LLVMTargetRef T) {
106 return unwrap(T)->getShortDescription();
107}
108
109LLVMBool LLVMTargetHasJIT(LLVMTargetRef T) {
110 return unwrap(T)->hasJIT();
111}
112
113LLVMBool LLVMTargetHasTargetMachine(LLVMTargetRef T) {
114 return unwrap(T)->hasTargetMachine();
115}
116
117LLVMBool LLVMTargetHasAsmBackend(LLVMTargetRef T) {
118 return unwrap(T)->hasMCAsmBackend();
119}
120
121LLVMTargetMachineRef LLVMCreateTargetMachine(LLVMTargetRef T, char* Triple,
122 char* CPU, char* Features, LLVMCodeGenOptLevel Level, LLVMRelocMode Reloc,
123 LLVMCodeModel CodeModel) {
124 Reloc::Model RM;
125 switch (Reloc){
126 case LLVMRelocStatic:
127 RM = Reloc::Static;
128 break;
129 case LLVMRelocPIC:
130 RM = Reloc::PIC_;
131 break;
132 case LLVMRelocDynamicNoPic:
133 RM = Reloc::DynamicNoPIC;
134 break;
135 default:
136 RM = Reloc::Default;
137 break;
138 }
139
Filip Pizlo85e0d272013-05-01 22:58:00 +0000140 CodeModel::Model CM = unwrap(CodeModel);
Duncan Sands264d2e72012-04-11 10:25:24 +0000141
Filip Pizlo85e0d272013-05-01 22:58:00 +0000142 CodeGenOpt::Level OL;
Duncan Sands264d2e72012-04-11 10:25:24 +0000143 switch (Level) {
144 case LLVMCodeGenLevelNone:
145 OL = CodeGenOpt::None;
146 break;
147 case LLVMCodeGenLevelLess:
148 OL = CodeGenOpt::Less;
149 break;
150 case LLVMCodeGenLevelAggressive:
151 OL = CodeGenOpt::Aggressive;
152 break;
153 default:
154 OL = CodeGenOpt::Default;
155 break;
156 }
157
158 TargetOptions opt;
159 return wrap(unwrap(T)->createTargetMachine(Triple, CPU, Features, opt, RM,
160 CM, OL));
161}
162
163
164void LLVMDisposeTargetMachine(LLVMTargetMachineRef T) {
165 delete unwrap(T);
166}
167
168LLVMTargetRef LLVMGetTargetMachineTarget(LLVMTargetMachineRef T) {
169 const Target* target = &(unwrap(T)->getTarget());
170 return wrap(target);
171}
172
173char* LLVMGetTargetMachineTriple(LLVMTargetMachineRef T) {
174 std::string StringRep = unwrap(T)->getTargetTriple();
175 return strdup(StringRep.c_str());
176}
177
178char* LLVMGetTargetMachineCPU(LLVMTargetMachineRef T) {
179 std::string StringRep = unwrap(T)->getTargetCPU();
180 return strdup(StringRep.c_str());
181}
182
183char* LLVMGetTargetMachineFeatureString(LLVMTargetMachineRef T) {
184 std::string StringRep = unwrap(T)->getTargetFeatureString();
185 return strdup(StringRep.c_str());
186}
187
188LLVMTargetDataRef LLVMGetTargetMachineData(LLVMTargetMachineRef T) {
Micah Villmowcdfe20b2012-10-08 16:38:25 +0000189 return wrap(unwrap(T)->getDataLayout());
Duncan Sands264d2e72012-04-11 10:25:24 +0000190}
191
Peter Zotov7b61b752013-11-06 10:25:18 +0000192void LLVMSetTargetMachineAsmVerbosity(LLVMTargetMachineRef T,
193 LLVMBool VerboseAsm) {
194 unwrap(T)->setAsmVerbosityDefault(VerboseAsm);
195}
196
Tom Stellardec924c52013-04-16 23:12:56 +0000197static LLVMBool LLVMTargetMachineEmit(LLVMTargetMachineRef T, LLVMModuleRef M,
198 formatted_raw_ostream &OS, LLVMCodeGenFileType codegen, char **ErrorMessage) {
Duncan Sands264d2e72012-04-11 10:25:24 +0000199 TargetMachine* TM = unwrap(T);
200 Module* Mod = unwrap(M);
201
202 PassManager pass;
203
204 std::string error;
205
Micah Villmowcdfe20b2012-10-08 16:38:25 +0000206 const DataLayout* td = TM->getDataLayout();
Duncan Sands264d2e72012-04-11 10:25:24 +0000207
208 if (!td) {
Micah Villmowcdfe20b2012-10-08 16:38:25 +0000209 error = "No DataLayout in TargetMachine";
Duncan Sands264d2e72012-04-11 10:25:24 +0000210 *ErrorMessage = strdup(error.c_str());
211 return true;
212 }
Micah Villmowcdfe20b2012-10-08 16:38:25 +0000213 pass.add(new DataLayout(*td));
Duncan Sands264d2e72012-04-11 10:25:24 +0000214
215 TargetMachine::CodeGenFileType ft;
216 switch (codegen) {
217 case LLVMAssemblyFile:
218 ft = TargetMachine::CGFT_AssemblyFile;
219 break;
220 default:
221 ft = TargetMachine::CGFT_ObjectFile;
222 break;
223 }
Tom Stellardec924c52013-04-16 23:12:56 +0000224 if (TM->addPassesToEmitFile(pass, OS, ft)) {
Nick Lewycky7b287ee2013-03-10 22:01:44 +0000225 error = "TargetMachine can't emit a file of this type";
Duncan Sands264d2e72012-04-11 10:25:24 +0000226 *ErrorMessage = strdup(error.c_str());
227 return true;
228 }
229
230 pass.run(*Mod);
231
Tom Stellardec924c52013-04-16 23:12:56 +0000232 OS.flush();
Duncan Sands264d2e72012-04-11 10:25:24 +0000233 return false;
234}
Tom Stellardec924c52013-04-16 23:12:56 +0000235
236LLVMBool LLVMTargetMachineEmitToFile(LLVMTargetMachineRef T, LLVMModuleRef M,
237 char* Filename, LLVMCodeGenFileType codegen, char** ErrorMessage) {
238 std::string error;
Rafael Espindola6d354812013-07-16 19:44:17 +0000239 raw_fd_ostream dest(Filename, error, sys::fs::F_Binary);
Tom Stellardec924c52013-04-16 23:12:56 +0000240 if (!error.empty()) {
241 *ErrorMessage = strdup(error.c_str());
242 return true;
243 }
Anders Waldenborg39f5d7d2013-10-17 10:39:35 +0000244 formatted_raw_ostream destf(dest);
Tom Stellardec924c52013-04-16 23:12:56 +0000245 bool Result = LLVMTargetMachineEmit(T, M, destf, codegen, ErrorMessage);
246 dest.flush();
247 return Result;
248}
249
250LLVMBool LLVMTargetMachineEmitToMemoryBuffer(LLVMTargetMachineRef T,
251 LLVMModuleRef M, LLVMCodeGenFileType codegen, char** ErrorMessage,
252 LLVMMemoryBufferRef *OutMemBuf) {
253 std::string CodeString;
254 raw_string_ostream OStream(CodeString);
255 formatted_raw_ostream Out(OStream);
256 bool Result = LLVMTargetMachineEmit(T, M, Out, codegen, ErrorMessage);
257 OStream.flush();
258
259 std::string &Data = OStream.str();
260 *OutMemBuf = LLVMCreateMemoryBufferWithMemoryRangeCopy(Data.c_str(),
261 Data.length(), "");
262 return Result;
263}
Peter Zotov7b61b752013-11-06 10:25:18 +0000264
265char *LLVMGetDefaultTargetTriple(void) {
266 return strdup(sys::getDefaultTargetTriple().c_str());
267}