blob: 623b3e8ca3203c9c2c2071940237b97a29465721 [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 Carruth93dcdc42015-01-31 11:17:59 +000017#include "llvm/Analysis/TargetTransformInfo.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000018#include "llvm/IR/DataLayout.h"
19#include "llvm/IR/Module.h"
Chandler Carruth30d69c22015-02-13 10:01:29 +000020#include "llvm/IR/LegacyPassManager.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000021#include "llvm/Support/CodeGen.h"
Benjamin Kramerd59664f2014-04-29 23:26:49 +000022#include "llvm/Support/FileSystem.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000023#include "llvm/Support/FormattedStream.h"
Chandler Carruth8a8cd2b2014-01-07 11:48:04 +000024#include "llvm/Support/Host.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000025#include "llvm/Support/TargetRegistry.h"
26#include "llvm/Support/raw_ostream.h"
27#include "llvm/Target/TargetMachine.h"
Eric Christopherd9134482014-08-04 21:25:23 +000028#include "llvm/Target/TargetSubtargetInfo.h"
Duncan Sands264d2e72012-04-11 10:25:24 +000029#include <cassert>
30#include <cstdlib>
31#include <cstring>
32
33using namespace llvm;
34
Eric Christopher04d4e932013-04-22 22:47:22 +000035inline TargetMachine *unwrap(LLVMTargetMachineRef P) {
36 return reinterpret_cast<TargetMachine*>(P);
37}
38inline Target *unwrap(LLVMTargetRef P) {
39 return reinterpret_cast<Target*>(P);
40}
41inline LLVMTargetMachineRef wrap(const TargetMachine *P) {
42 return
43 reinterpret_cast<LLVMTargetMachineRef>(const_cast<TargetMachine*>(P));
44}
45inline LLVMTargetRef wrap(const Target * P) {
46 return reinterpret_cast<LLVMTargetRef>(const_cast<Target*>(P));
47}
Duncan Sands264d2e72012-04-11 10:25:24 +000048
49LLVMTargetRef LLVMGetFirstTarget() {
David Blaikie46c561c2015-05-11 22:20:48 +000050 if (TargetRegistry::targets().begin() == TargetRegistry::targets().end()) {
Craig Topper062a2ba2014-04-25 05:30:21 +000051 return nullptr;
Anders Waldenborga89c1e32013-10-17 10:25:24 +000052 }
53
David Blaikie46c561c2015-05-11 22:20:48 +000054 const Target *target = &*TargetRegistry::targets().begin();
Anders Waldenborga89c1e32013-10-17 10:25:24 +000055 return wrap(target);
Duncan Sands264d2e72012-04-11 10:25:24 +000056}
57LLVMTargetRef LLVMGetNextTarget(LLVMTargetRef T) {
58 return wrap(unwrap(T)->getNext());
59}
60
Peter Zotovb2c8b8a2013-11-15 02:51:01 +000061LLVMTargetRef LLVMGetTargetFromName(const char *Name) {
Peter Zotov5a6cfda2013-11-15 02:51:18 +000062 StringRef NameRef = Name;
David Blaikie46c561c2015-05-11 22:20:48 +000063 auto I = std::find_if(
64 TargetRegistry::targets().begin(), TargetRegistry::targets().end(),
65 [&](const Target &T) { return T.getName() == NameRef; });
66 return I != TargetRegistry::targets().end() ? wrap(&*I) : nullptr;
Peter Zotov7b61b752013-11-06 10:25:18 +000067}
68
69LLVMBool LLVMGetTargetFromTriple(const char* TripleStr, LLVMTargetRef *T,
70 char **ErrorMessage) {
71 std::string Error;
72
73 *T = wrap(TargetRegistry::lookupTarget(TripleStr, Error));
74
75 if (!*T) {
76 if (ErrorMessage)
77 *ErrorMessage = strdup(Error.c_str());
78
79 return 1;
80 }
81
82 return 0;
83}
84
Duncan Sands264d2e72012-04-11 10:25:24 +000085const char * LLVMGetTargetName(LLVMTargetRef T) {
86 return unwrap(T)->getName();
87}
88
89const char * LLVMGetTargetDescription(LLVMTargetRef T) {
90 return unwrap(T)->getShortDescription();
91}
92
93LLVMBool LLVMTargetHasJIT(LLVMTargetRef T) {
94 return unwrap(T)->hasJIT();
95}
96
97LLVMBool LLVMTargetHasTargetMachine(LLVMTargetRef T) {
98 return unwrap(T)->hasTargetMachine();
99}
100
101LLVMBool LLVMTargetHasAsmBackend(LLVMTargetRef T) {
102 return unwrap(T)->hasMCAsmBackend();
103}
104
Peter Zotov0e38fc82013-11-15 02:51:12 +0000105LLVMTargetMachineRef LLVMCreateTargetMachine(LLVMTargetRef T,
106 const char* Triple, const char* CPU, const char* Features,
107 LLVMCodeGenOptLevel Level, LLVMRelocMode Reloc,
108 LLVMCodeModel CodeModel) {
Duncan Sands264d2e72012-04-11 10:25:24 +0000109 Reloc::Model RM;
110 switch (Reloc){
111 case LLVMRelocStatic:
112 RM = Reloc::Static;
113 break;
114 case LLVMRelocPIC:
115 RM = Reloc::PIC_;
116 break;
117 case LLVMRelocDynamicNoPic:
118 RM = Reloc::DynamicNoPIC;
119 break;
120 default:
121 RM = Reloc::Default;
122 break;
123 }
124
Filip Pizlo85e0d272013-05-01 22:58:00 +0000125 CodeModel::Model CM = unwrap(CodeModel);
Duncan Sands264d2e72012-04-11 10:25:24 +0000126
Filip Pizlo85e0d272013-05-01 22:58:00 +0000127 CodeGenOpt::Level OL;
Duncan Sands264d2e72012-04-11 10:25:24 +0000128 switch (Level) {
129 case LLVMCodeGenLevelNone:
130 OL = CodeGenOpt::None;
131 break;
132 case LLVMCodeGenLevelLess:
133 OL = CodeGenOpt::Less;
134 break;
135 case LLVMCodeGenLevelAggressive:
136 OL = CodeGenOpt::Aggressive;
137 break;
138 default:
139 OL = CodeGenOpt::Default;
140 break;
141 }
142
143 TargetOptions opt;
144 return wrap(unwrap(T)->createTargetMachine(Triple, CPU, Features, opt, RM,
145 CM, OL));
146}
147
148
149void LLVMDisposeTargetMachine(LLVMTargetMachineRef T) {
150 delete unwrap(T);
151}
152
153LLVMTargetRef LLVMGetTargetMachineTarget(LLVMTargetMachineRef T) {
154 const Target* target = &(unwrap(T)->getTarget());
155 return wrap(target);
156}
157
158char* LLVMGetTargetMachineTriple(LLVMTargetMachineRef T) {
159 std::string StringRep = unwrap(T)->getTargetTriple();
160 return strdup(StringRep.c_str());
161}
162
163char* LLVMGetTargetMachineCPU(LLVMTargetMachineRef T) {
164 std::string StringRep = unwrap(T)->getTargetCPU();
165 return strdup(StringRep.c_str());
166}
167
168char* LLVMGetTargetMachineFeatureString(LLVMTargetMachineRef T) {
169 std::string StringRep = unwrap(T)->getTargetFeatureString();
170 return strdup(StringRep.c_str());
171}
172
173LLVMTargetDataRef LLVMGetTargetMachineData(LLVMTargetMachineRef T) {
Eric Christopher8b770652015-01-26 19:03:15 +0000174 return wrap(unwrap(T)->getDataLayout());
Duncan Sands264d2e72012-04-11 10:25:24 +0000175}
176
Peter Zotov7b61b752013-11-06 10:25:18 +0000177void LLVMSetTargetMachineAsmVerbosity(LLVMTargetMachineRef T,
178 LLVMBool VerboseAsm) {
Rafael Espindola3105fd82015-02-12 21:16:34 +0000179 unwrap(T)->Options.MCOptions.AsmVerbose = VerboseAsm;
Peter Zotov7b61b752013-11-06 10:25:18 +0000180}
181
Tom Stellardec924c52013-04-16 23:12:56 +0000182static LLVMBool LLVMTargetMachineEmit(LLVMTargetMachineRef T, LLVMModuleRef M,
Rafael Espindola5560a4c2015-04-14 22:14:34 +0000183 raw_pwrite_stream &OS,
Rafael Espindolaf546d0f2015-04-09 17:16:25 +0000184 LLVMCodeGenFileType codegen,
185 char **ErrorMessage) {
Duncan Sands264d2e72012-04-11 10:25:24 +0000186 TargetMachine* TM = unwrap(T);
187 Module* Mod = unwrap(M);
188
Chandler Carruth30d69c22015-02-13 10:01:29 +0000189 legacy::PassManager pass;
Duncan Sands264d2e72012-04-11 10:25:24 +0000190
191 std::string error;
192
Eric Christopher8b770652015-01-26 19:03:15 +0000193 const DataLayout *td = TM->getDataLayout();
Duncan Sands264d2e72012-04-11 10:25:24 +0000194
195 if (!td) {
Micah Villmowcdfe20b2012-10-08 16:38:25 +0000196 error = "No DataLayout in TargetMachine";
Duncan Sands264d2e72012-04-11 10:25:24 +0000197 *ErrorMessage = strdup(error.c_str());
198 return true;
199 }
Mehdi Amini46a43552015-03-04 18:43:29 +0000200 Mod->setDataLayout(*td);
Duncan Sands264d2e72012-04-11 10:25:24 +0000201
202 TargetMachine::CodeGenFileType ft;
203 switch (codegen) {
204 case LLVMAssemblyFile:
205 ft = TargetMachine::CGFT_AssemblyFile;
206 break;
207 default:
208 ft = TargetMachine::CGFT_ObjectFile;
209 break;
210 }
Tom Stellardec924c52013-04-16 23:12:56 +0000211 if (TM->addPassesToEmitFile(pass, OS, ft)) {
Nick Lewycky7b287ee2013-03-10 22:01:44 +0000212 error = "TargetMachine can't emit a file of this type";
Duncan Sands264d2e72012-04-11 10:25:24 +0000213 *ErrorMessage = strdup(error.c_str());
214 return true;
215 }
216
217 pass.run(*Mod);
218
Tom Stellardec924c52013-04-16 23:12:56 +0000219 OS.flush();
Duncan Sands264d2e72012-04-11 10:25:24 +0000220 return false;
221}
Tom Stellardec924c52013-04-16 23:12:56 +0000222
223LLVMBool LLVMTargetMachineEmitToFile(LLVMTargetMachineRef T, LLVMModuleRef M,
224 char* Filename, LLVMCodeGenFileType codegen, char** ErrorMessage) {
Rafael Espindola3fd1e992014-08-25 18:16:47 +0000225 std::error_code EC;
226 raw_fd_ostream dest(Filename, EC, sys::fs::F_None);
227 if (EC) {
228 *ErrorMessage = strdup(EC.message().c_str());
Tom Stellardec924c52013-04-16 23:12:56 +0000229 return true;
230 }
Rafael Espindola5682ce22015-04-09 21:06:08 +0000231 bool Result = LLVMTargetMachineEmit(T, M, dest, codegen, ErrorMessage);
Tom Stellardec924c52013-04-16 23:12:56 +0000232 dest.flush();
233 return Result;
234}
235
236LLVMBool LLVMTargetMachineEmitToMemoryBuffer(LLVMTargetMachineRef T,
237 LLVMModuleRef M, LLVMCodeGenFileType codegen, char** ErrorMessage,
238 LLVMMemoryBufferRef *OutMemBuf) {
Rafael Espindolaf546d0f2015-04-09 17:16:25 +0000239 SmallString<0> CodeString;
240 raw_svector_ostream OStream(CodeString);
Rafael Espindola5682ce22015-04-09 21:06:08 +0000241 bool Result = LLVMTargetMachineEmit(T, M, OStream, codegen, ErrorMessage);
Alp Tokere69170a2014-06-26 22:52:05 +0000242 OStream.flush();
Tom Stellardec924c52013-04-16 23:12:56 +0000243
Rafael Espindolaf546d0f2015-04-09 17:16:25 +0000244 StringRef Data = OStream.str();
245 *OutMemBuf =
246 LLVMCreateMemoryBufferWithMemoryRangeCopy(Data.data(), Data.size(), "");
Tom Stellardec924c52013-04-16 23:12:56 +0000247 return Result;
248}
Peter Zotov7b61b752013-11-06 10:25:18 +0000249
250char *LLVMGetDefaultTargetTriple(void) {
251 return strdup(sys::getDefaultTargetTriple().c_str());
252}
Juergen Ributzka5fe955c2014-01-23 19:23:28 +0000253
254void LLVMAddAnalysisPasses(LLVMTargetMachineRef T, LLVMPassManagerRef PM) {
Chandler Carruth5ec2b1d2015-02-01 12:26:09 +0000255 unwrap(PM)->add(
256 createTargetTransformInfoWrapperPass(unwrap(T)->getTargetIRAnalysis()));
Juergen Ributzka5fe955c2014-01-23 19:23:28 +0000257}