blob: b2bd8fae423edc5a1741a6336266aecb104079f6 [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
Mehdi Amini31ebf03c2015-08-26 19:24:59 +000035
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.
40struct LLVMOpaqueTargetMachine {
41 std::unique_ptr<TargetMachine> Machine;
42 DataLayout DL;
43};
44
45
Diego Novillocd973c42015-07-27 18:27:23 +000046static TargetMachine *unwrap(LLVMTargetMachineRef P) {
Mehdi Amini31ebf03c2015-08-26 19:24:59 +000047 return P->Machine.get();
Eric Christopher04d4e932013-04-22 22:47:22 +000048}
Diego Novillocd973c42015-07-27 18:27:23 +000049static Target *unwrap(LLVMTargetRef P) {
Eric Christopher04d4e932013-04-22 22:47:22 +000050 return reinterpret_cast<Target*>(P);
51}
Diego Novillocd973c42015-07-27 18:27:23 +000052static LLVMTargetMachineRef wrap(const TargetMachine *P) {
Mehdi Amini31ebf03c2015-08-26 19:24:59 +000053 return new LLVMOpaqueTargetMachine{ std::unique_ptr<TargetMachine>(const_cast<TargetMachine*>(P)), P->createDataLayout() };
Eric Christopher04d4e932013-04-22 22:47:22 +000054}
Diego Novillocd973c42015-07-27 18:27:23 +000055static LLVMTargetRef wrap(const Target * P) {
Eric Christopher04d4e932013-04-22 22:47:22 +000056 return reinterpret_cast<LLVMTargetRef>(const_cast<Target*>(P));
57}
Duncan Sands264d2e72012-04-11 10:25:24 +000058
59LLVMTargetRef LLVMGetFirstTarget() {
David Blaikie46c561c2015-05-11 22:20:48 +000060 if (TargetRegistry::targets().begin() == TargetRegistry::targets().end()) {
Craig Topper062a2ba2014-04-25 05:30:21 +000061 return nullptr;
Anders Waldenborga89c1e32013-10-17 10:25:24 +000062 }
63
David Blaikie46c561c2015-05-11 22:20:48 +000064 const Target *target = &*TargetRegistry::targets().begin();
Anders Waldenborga89c1e32013-10-17 10:25:24 +000065 return wrap(target);
Duncan Sands264d2e72012-04-11 10:25:24 +000066}
67LLVMTargetRef LLVMGetNextTarget(LLVMTargetRef T) {
68 return wrap(unwrap(T)->getNext());
69}
70
Peter Zotovb2c8b8a2013-11-15 02:51:01 +000071LLVMTargetRef LLVMGetTargetFromName(const char *Name) {
Peter Zotov5a6cfda2013-11-15 02:51:18 +000072 StringRef NameRef = Name;
David Blaikie46c561c2015-05-11 22:20:48 +000073 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;
Peter Zotov7b61b752013-11-06 10:25:18 +000077}
78
79LLVMBool LLVMGetTargetFromTriple(const char* TripleStr, LLVMTargetRef *T,
80 char **ErrorMessage) {
81 std::string Error;
Mehdi Amini31ebf03c2015-08-26 19:24:59 +000082
Peter Zotov7b61b752013-11-06 10:25:18 +000083 *T = wrap(TargetRegistry::lookupTarget(TripleStr, Error));
Mehdi Amini31ebf03c2015-08-26 19:24:59 +000084
Peter Zotov7b61b752013-11-06 10:25:18 +000085 if (!*T) {
86 if (ErrorMessage)
87 *ErrorMessage = strdup(Error.c_str());
88
89 return 1;
90 }
Mehdi Amini31ebf03c2015-08-26 19:24:59 +000091
Peter Zotov7b61b752013-11-06 10:25:18 +000092 return 0;
93}
94
Duncan Sands264d2e72012-04-11 10:25:24 +000095const char * LLVMGetTargetName(LLVMTargetRef T) {
96 return unwrap(T)->getName();
97}
98
99const char * LLVMGetTargetDescription(LLVMTargetRef T) {
100 return unwrap(T)->getShortDescription();
101}
102
103LLVMBool LLVMTargetHasJIT(LLVMTargetRef T) {
104 return unwrap(T)->hasJIT();
105}
106
107LLVMBool LLVMTargetHasTargetMachine(LLVMTargetRef T) {
108 return unwrap(T)->hasTargetMachine();
109}
110
111LLVMBool LLVMTargetHasAsmBackend(LLVMTargetRef T) {
112 return unwrap(T)->hasMCAsmBackend();
113}
114
Peter Zotov0e38fc82013-11-15 02:51:12 +0000115LLVMTargetMachineRef LLVMCreateTargetMachine(LLVMTargetRef T,
116 const char* Triple, const char* CPU, const char* Features,
117 LLVMCodeGenOptLevel Level, LLVMRelocMode Reloc,
118 LLVMCodeModel CodeModel) {
Duncan Sands264d2e72012-04-11 10:25:24 +0000119 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
Filip Pizlo85e0d272013-05-01 22:58:00 +0000135 CodeModel::Model CM = unwrap(CodeModel);
Duncan Sands264d2e72012-04-11 10:25:24 +0000136
Filip Pizlo85e0d272013-05-01 22:58:00 +0000137 CodeGenOpt::Level OL;
Duncan Sands264d2e72012-04-11 10:25:24 +0000138 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
Mehdi Amini31ebf03c2015-08-26 19:24:59 +0000158
159void LLVMDisposeTargetMachine(LLVMTargetMachineRef T) {
160 delete T;
161}
Duncan Sands264d2e72012-04-11 10:25:24 +0000162
163LLVMTargetRef LLVMGetTargetMachineTarget(LLVMTargetMachineRef T) {
164 const Target* target = &(unwrap(T)->getTarget());
165 return wrap(target);
166}
167
168char* LLVMGetTargetMachineTriple(LLVMTargetMachineRef T) {
Daniel Sanders335487a2015-06-16 13:15:50 +0000169 std::string StringRep = unwrap(T)->getTargetTriple().str();
Duncan Sands264d2e72012-04-11 10:25:24 +0000170 return strdup(StringRep.c_str());
171}
172
173char* LLVMGetTargetMachineCPU(LLVMTargetMachineRef T) {
174 std::string StringRep = unwrap(T)->getTargetCPU();
175 return strdup(StringRep.c_str());
176}
177
178char* LLVMGetTargetMachineFeatureString(LLVMTargetMachineRef T) {
179 std::string StringRep = unwrap(T)->getTargetFeatureString();
180 return strdup(StringRep.c_str());
181}
182
Mehdi Amini31ebf03c2015-08-26 19:24:59 +0000183/// @deprecated: see "struct LLVMOpaqueTargetMachine" description above
Duncan Sands264d2e72012-04-11 10:25:24 +0000184LLVMTargetDataRef LLVMGetTargetMachineData(LLVMTargetMachineRef T) {
Mehdi Amini31ebf03c2015-08-26 19:24:59 +0000185 return wrap(&T->DL);
Duncan Sands264d2e72012-04-11 10:25:24 +0000186}
187
Peter Zotov7b61b752013-11-06 10:25:18 +0000188void LLVMSetTargetMachineAsmVerbosity(LLVMTargetMachineRef T,
189 LLVMBool VerboseAsm) {
Rafael Espindola3105fd82015-02-12 21:16:34 +0000190 unwrap(T)->Options.MCOptions.AsmVerbose = VerboseAsm;
Peter Zotov7b61b752013-11-06 10:25:18 +0000191}
192
Tom Stellardec924c52013-04-16 23:12:56 +0000193static LLVMBool LLVMTargetMachineEmit(LLVMTargetMachineRef T, LLVMModuleRef M,
Rafael Espindola5560a4c2015-04-14 22:14:34 +0000194 raw_pwrite_stream &OS,
Rafael Espindolaf546d0f2015-04-09 17:16:25 +0000195 LLVMCodeGenFileType codegen,
196 char **ErrorMessage) {
Duncan Sands264d2e72012-04-11 10:25:24 +0000197 TargetMachine* TM = unwrap(T);
198 Module* Mod = unwrap(M);
199
Chandler Carruth30d69c22015-02-13 10:01:29 +0000200 legacy::PassManager pass;
Duncan Sands264d2e72012-04-11 10:25:24 +0000201
202 std::string error;
203
Mehdi Amini26d48132015-07-24 16:04:22 +0000204 Mod->setDataLayout(TM->createDataLayout());
Duncan Sands264d2e72012-04-11 10:25:24 +0000205
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 }
Tom Stellardec924c52013-04-16 23:12:56 +0000215 if (TM->addPassesToEmitFile(pass, OS, ft)) {
Nick Lewycky7b287ee2013-03-10 22:01:44 +0000216 error = "TargetMachine can't emit a file of this type";
Duncan Sands264d2e72012-04-11 10:25:24 +0000217 *ErrorMessage = strdup(error.c_str());
218 return true;
219 }
220
221 pass.run(*Mod);
222
Tom Stellardec924c52013-04-16 23:12:56 +0000223 OS.flush();
Duncan Sands264d2e72012-04-11 10:25:24 +0000224 return false;
225}
Tom Stellardec924c52013-04-16 23:12:56 +0000226
227LLVMBool LLVMTargetMachineEmitToFile(LLVMTargetMachineRef T, LLVMModuleRef M,
228 char* Filename, LLVMCodeGenFileType codegen, char** ErrorMessage) {
Rafael Espindola3fd1e992014-08-25 18:16:47 +0000229 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());
Tom Stellardec924c52013-04-16 23:12:56 +0000233 return true;
234 }
Rafael Espindola5682ce22015-04-09 21:06:08 +0000235 bool Result = LLVMTargetMachineEmit(T, M, dest, codegen, ErrorMessage);
Tom Stellardec924c52013-04-16 23:12:56 +0000236 dest.flush();
237 return Result;
238}
239
240LLVMBool LLVMTargetMachineEmitToMemoryBuffer(LLVMTargetMachineRef T,
241 LLVMModuleRef M, LLVMCodeGenFileType codegen, char** ErrorMessage,
242 LLVMMemoryBufferRef *OutMemBuf) {
Rafael Espindolaf546d0f2015-04-09 17:16:25 +0000243 SmallString<0> CodeString;
244 raw_svector_ostream OStream(CodeString);
Rafael Espindola5682ce22015-04-09 21:06:08 +0000245 bool Result = LLVMTargetMachineEmit(T, M, OStream, codegen, ErrorMessage);
Tom Stellardec924c52013-04-16 23:12:56 +0000246
Rafael Espindolaf546d0f2015-04-09 17:16:25 +0000247 StringRef Data = OStream.str();
248 *OutMemBuf =
249 LLVMCreateMemoryBufferWithMemoryRangeCopy(Data.data(), Data.size(), "");
Tom Stellardec924c52013-04-16 23:12:56 +0000250 return Result;
251}
Peter Zotov7b61b752013-11-06 10:25:18 +0000252
253char *LLVMGetDefaultTargetTriple(void) {
254 return strdup(sys::getDefaultTargetTriple().c_str());
255}
Juergen Ributzka5fe955c2014-01-23 19:23:28 +0000256
257void LLVMAddAnalysisPasses(LLVMTargetMachineRef T, LLVMPassManagerRef PM) {
Chandler Carruth5ec2b1d2015-02-01 12:26:09 +0000258 unwrap(PM)->add(
259 createTargetTransformInfoWrapperPass(unwrap(T)->getTargetIRAnalysis()));
Juergen Ributzka5fe955c2014-01-23 19:23:28 +0000260}