blob: f2ac93e76847232774038c3ac24286c1f6254b17 [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"
Benjamin Kramerd59664f2014-04-29 23:26:49 +000021#include "llvm/Support/FileSystem.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000022#include "llvm/Support/FormattedStream.h"
Chandler Carruth8a8cd2b2014-01-07 11:48:04 +000023#include "llvm/Support/Host.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000024#include "llvm/Support/TargetRegistry.h"
25#include "llvm/Support/raw_ostream.h"
26#include "llvm/Target/TargetMachine.h"
Duncan Sands264d2e72012-04-11 10:25:24 +000027#include <cassert>
28#include <cstdlib>
29#include <cstring>
30
31using namespace llvm;
32
Eric Christopher04d4e932013-04-22 22:47:22 +000033inline DataLayout *unwrap(LLVMTargetDataRef P) {
34 return reinterpret_cast<DataLayout*>(P);
35}
Duncan Sands264d2e72012-04-11 10:25:24 +000036
Eric Christopher04d4e932013-04-22 22:47:22 +000037inline LLVMTargetDataRef wrap(const DataLayout *P) {
38 return reinterpret_cast<LLVMTargetDataRef>(const_cast<DataLayout*>(P));
39}
40
41inline TargetLibraryInfo *unwrap(LLVMTargetLibraryInfoRef P) {
42 return reinterpret_cast<TargetLibraryInfo*>(P);
43}
44
45inline LLVMTargetLibraryInfoRef wrap(const TargetLibraryInfo *P) {
46 TargetLibraryInfo *X = const_cast<TargetLibraryInfo*>(P);
47 return reinterpret_cast<LLVMTargetLibraryInfoRef>(X);
48}
49
50inline TargetMachine *unwrap(LLVMTargetMachineRef P) {
51 return reinterpret_cast<TargetMachine*>(P);
52}
53inline Target *unwrap(LLVMTargetRef P) {
54 return reinterpret_cast<Target*>(P);
55}
56inline LLVMTargetMachineRef wrap(const TargetMachine *P) {
57 return
58 reinterpret_cast<LLVMTargetMachineRef>(const_cast<TargetMachine*>(P));
59}
60inline LLVMTargetRef wrap(const Target * P) {
61 return reinterpret_cast<LLVMTargetRef>(const_cast<Target*>(P));
62}
Duncan Sands264d2e72012-04-11 10:25:24 +000063
64LLVMTargetRef LLVMGetFirstTarget() {
Anders Waldenborga89c1e32013-10-17 10:25:24 +000065 if(TargetRegistry::begin() == TargetRegistry::end()) {
Craig Topper062a2ba2014-04-25 05:30:21 +000066 return nullptr;
Anders Waldenborga89c1e32013-10-17 10:25:24 +000067 }
68
69 const Target* target = &*TargetRegistry::begin();
70 return wrap(target);
Duncan Sands264d2e72012-04-11 10:25:24 +000071}
72LLVMTargetRef LLVMGetNextTarget(LLVMTargetRef T) {
73 return wrap(unwrap(T)->getNext());
74}
75
Peter Zotovb2c8b8a2013-11-15 02:51:01 +000076LLVMTargetRef LLVMGetTargetFromName(const char *Name) {
Peter Zotov5a6cfda2013-11-15 02:51:18 +000077 StringRef NameRef = Name;
Peter Zotov7b61b752013-11-06 10:25:18 +000078 for (TargetRegistry::iterator IT = TargetRegistry::begin(),
79 IE = TargetRegistry::end(); IT != IE; ++IT) {
Peter Zotov5a6cfda2013-11-15 02:51:18 +000080 if (IT->getName() == NameRef)
Peter Zotovb2c8b8a2013-11-15 02:51:01 +000081 return wrap(&*IT);
Peter Zotov7b61b752013-11-06 10:25:18 +000082 }
83
Craig Topper062a2ba2014-04-25 05:30:21 +000084 return nullptr;
Peter Zotov7b61b752013-11-06 10:25:18 +000085}
86
87LLVMBool LLVMGetTargetFromTriple(const char* TripleStr, LLVMTargetRef *T,
88 char **ErrorMessage) {
89 std::string Error;
90
91 *T = wrap(TargetRegistry::lookupTarget(TripleStr, Error));
92
93 if (!*T) {
94 if (ErrorMessage)
95 *ErrorMessage = strdup(Error.c_str());
96
97 return 1;
98 }
99
100 return 0;
101}
102
Duncan Sands264d2e72012-04-11 10:25:24 +0000103const char * LLVMGetTargetName(LLVMTargetRef T) {
104 return unwrap(T)->getName();
105}
106
107const char * LLVMGetTargetDescription(LLVMTargetRef T) {
108 return unwrap(T)->getShortDescription();
109}
110
111LLVMBool LLVMTargetHasJIT(LLVMTargetRef T) {
112 return unwrap(T)->hasJIT();
113}
114
115LLVMBool LLVMTargetHasTargetMachine(LLVMTargetRef T) {
116 return unwrap(T)->hasTargetMachine();
117}
118
119LLVMBool LLVMTargetHasAsmBackend(LLVMTargetRef T) {
120 return unwrap(T)->hasMCAsmBackend();
121}
122
Peter Zotov0e38fc82013-11-15 02:51:12 +0000123LLVMTargetMachineRef LLVMCreateTargetMachine(LLVMTargetRef T,
124 const char* Triple, const char* CPU, const char* Features,
125 LLVMCodeGenOptLevel Level, LLVMRelocMode Reloc,
126 LLVMCodeModel CodeModel) {
Duncan Sands264d2e72012-04-11 10:25:24 +0000127 Reloc::Model RM;
128 switch (Reloc){
129 case LLVMRelocStatic:
130 RM = Reloc::Static;
131 break;
132 case LLVMRelocPIC:
133 RM = Reloc::PIC_;
134 break;
135 case LLVMRelocDynamicNoPic:
136 RM = Reloc::DynamicNoPIC;
137 break;
138 default:
139 RM = Reloc::Default;
140 break;
141 }
142
Filip Pizlo85e0d272013-05-01 22:58:00 +0000143 CodeModel::Model CM = unwrap(CodeModel);
Duncan Sands264d2e72012-04-11 10:25:24 +0000144
Filip Pizlo85e0d272013-05-01 22:58:00 +0000145 CodeGenOpt::Level OL;
Duncan Sands264d2e72012-04-11 10:25:24 +0000146 switch (Level) {
147 case LLVMCodeGenLevelNone:
148 OL = CodeGenOpt::None;
149 break;
150 case LLVMCodeGenLevelLess:
151 OL = CodeGenOpt::Less;
152 break;
153 case LLVMCodeGenLevelAggressive:
154 OL = CodeGenOpt::Aggressive;
155 break;
156 default:
157 OL = CodeGenOpt::Default;
158 break;
159 }
160
161 TargetOptions opt;
162 return wrap(unwrap(T)->createTargetMachine(Triple, CPU, Features, opt, RM,
163 CM, OL));
164}
165
166
167void LLVMDisposeTargetMachine(LLVMTargetMachineRef T) {
168 delete unwrap(T);
169}
170
171LLVMTargetRef LLVMGetTargetMachineTarget(LLVMTargetMachineRef T) {
172 const Target* target = &(unwrap(T)->getTarget());
173 return wrap(target);
174}
175
176char* LLVMGetTargetMachineTriple(LLVMTargetMachineRef T) {
177 std::string StringRep = unwrap(T)->getTargetTriple();
178 return strdup(StringRep.c_str());
179}
180
181char* LLVMGetTargetMachineCPU(LLVMTargetMachineRef T) {
182 std::string StringRep = unwrap(T)->getTargetCPU();
183 return strdup(StringRep.c_str());
184}
185
186char* LLVMGetTargetMachineFeatureString(LLVMTargetMachineRef T) {
187 std::string StringRep = unwrap(T)->getTargetFeatureString();
188 return strdup(StringRep.c_str());
189}
190
191LLVMTargetDataRef LLVMGetTargetMachineData(LLVMTargetMachineRef T) {
Micah Villmowcdfe20b2012-10-08 16:38:25 +0000192 return wrap(unwrap(T)->getDataLayout());
Duncan Sands264d2e72012-04-11 10:25:24 +0000193}
194
Peter Zotov7b61b752013-11-06 10:25:18 +0000195void LLVMSetTargetMachineAsmVerbosity(LLVMTargetMachineRef T,
196 LLVMBool VerboseAsm) {
197 unwrap(T)->setAsmVerbosityDefault(VerboseAsm);
198}
199
Tom Stellardec924c52013-04-16 23:12:56 +0000200static LLVMBool LLVMTargetMachineEmit(LLVMTargetMachineRef T, LLVMModuleRef M,
201 formatted_raw_ostream &OS, LLVMCodeGenFileType codegen, char **ErrorMessage) {
Duncan Sands264d2e72012-04-11 10:25:24 +0000202 TargetMachine* TM = unwrap(T);
203 Module* Mod = unwrap(M);
204
205 PassManager pass;
206
207 std::string error;
208
Micah Villmowcdfe20b2012-10-08 16:38:25 +0000209 const DataLayout* td = TM->getDataLayout();
Duncan Sands264d2e72012-04-11 10:25:24 +0000210
211 if (!td) {
Micah Villmowcdfe20b2012-10-08 16:38:25 +0000212 error = "No DataLayout in TargetMachine";
Duncan Sands264d2e72012-04-11 10:25:24 +0000213 *ErrorMessage = strdup(error.c_str());
214 return true;
215 }
Rafael Espindola339430f2014-02-25 23:25:17 +0000216 Mod->setDataLayout(td);
217 pass.add(new DataLayoutPass(Mod));
Duncan Sands264d2e72012-04-11 10:25:24 +0000218
219 TargetMachine::CodeGenFileType ft;
220 switch (codegen) {
221 case LLVMAssemblyFile:
222 ft = TargetMachine::CGFT_AssemblyFile;
223 break;
224 default:
225 ft = TargetMachine::CGFT_ObjectFile;
226 break;
227 }
Tom Stellardec924c52013-04-16 23:12:56 +0000228 if (TM->addPassesToEmitFile(pass, OS, ft)) {
Nick Lewycky7b287ee2013-03-10 22:01:44 +0000229 error = "TargetMachine can't emit a file of this type";
Duncan Sands264d2e72012-04-11 10:25:24 +0000230 *ErrorMessage = strdup(error.c_str());
231 return true;
232 }
233
234 pass.run(*Mod);
235
Tom Stellardec924c52013-04-16 23:12:56 +0000236 OS.flush();
Duncan Sands264d2e72012-04-11 10:25:24 +0000237 return false;
238}
Tom Stellardec924c52013-04-16 23:12:56 +0000239
240LLVMBool LLVMTargetMachineEmitToFile(LLVMTargetMachineRef T, LLVMModuleRef M,
241 char* Filename, LLVMCodeGenFileType codegen, char** ErrorMessage) {
242 std::string error;
Rafael Espindola90c7f1c2014-02-24 18:20:12 +0000243 raw_fd_ostream dest(Filename, error, sys::fs::F_None);
Tom Stellardec924c52013-04-16 23:12:56 +0000244 if (!error.empty()) {
245 *ErrorMessage = strdup(error.c_str());
246 return true;
247 }
Anders Waldenborg39f5d7d2013-10-17 10:39:35 +0000248 formatted_raw_ostream destf(dest);
Tom Stellardec924c52013-04-16 23:12:56 +0000249 bool Result = LLVMTargetMachineEmit(T, M, destf, codegen, ErrorMessage);
250 dest.flush();
251 return Result;
252}
253
254LLVMBool LLVMTargetMachineEmitToMemoryBuffer(LLVMTargetMachineRef T,
255 LLVMModuleRef M, LLVMCodeGenFileType codegen, char** ErrorMessage,
256 LLVMMemoryBufferRef *OutMemBuf) {
257 std::string CodeString;
258 raw_string_ostream OStream(CodeString);
259 formatted_raw_ostream Out(OStream);
260 bool Result = LLVMTargetMachineEmit(T, M, Out, codegen, ErrorMessage);
261 OStream.flush();
262
263 std::string &Data = OStream.str();
264 *OutMemBuf = LLVMCreateMemoryBufferWithMemoryRangeCopy(Data.c_str(),
265 Data.length(), "");
266 return Result;
267}
Peter Zotov7b61b752013-11-06 10:25:18 +0000268
269char *LLVMGetDefaultTargetTriple(void) {
270 return strdup(sys::getDefaultTargetTriple().c_str());
271}
Juergen Ributzka5fe955c2014-01-23 19:23:28 +0000272
273void LLVMAddAnalysisPasses(LLVMTargetMachineRef T, LLVMPassManagerRef PM) {
274 unwrap(T)->addAnalysisPasses(*unwrap(PM));
275}