blob: 9405ba7f295d00b8bb5113c1ea40fe1b99104f5c [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"
Duncan P. N. Exon Smith3f7f8832016-02-13 22:58:43 +000021#include "llvm/Support/CodeGenCWrappers.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
Diego Novillocd973c42015-07-27 18:27:23 +000035static TargetMachine *unwrap(LLVMTargetMachineRef P) {
Mehdi Amini0ab4b5b2015-08-26 21:16:29 +000036 return reinterpret_cast<TargetMachine *>(P);
Eric Christopher04d4e932013-04-22 22:47:22 +000037}
Diego Novillocd973c42015-07-27 18:27:23 +000038static Target *unwrap(LLVMTargetRef P) {
Eric Christopher04d4e932013-04-22 22:47:22 +000039 return reinterpret_cast<Target*>(P);
40}
Diego Novillocd973c42015-07-27 18:27:23 +000041static LLVMTargetMachineRef wrap(const TargetMachine *P) {
Mehdi Amini0ab4b5b2015-08-26 21:16:29 +000042 return reinterpret_cast<LLVMTargetMachineRef>(const_cast<TargetMachine *>(P));
Eric Christopher04d4e932013-04-22 22:47:22 +000043}
Diego Novillocd973c42015-07-27 18:27:23 +000044static LLVMTargetRef wrap(const Target * P) {
Eric Christopher04d4e932013-04-22 22:47:22 +000045 return reinterpret_cast<LLVMTargetRef>(const_cast<Target*>(P));
46}
Duncan Sands264d2e72012-04-11 10:25:24 +000047
48LLVMTargetRef LLVMGetFirstTarget() {
David Blaikie46c561c2015-05-11 22:20:48 +000049 if (TargetRegistry::targets().begin() == TargetRegistry::targets().end()) {
Craig Topper062a2ba2014-04-25 05:30:21 +000050 return nullptr;
Anders Waldenborga89c1e32013-10-17 10:25:24 +000051 }
52
David Blaikie46c561c2015-05-11 22:20:48 +000053 const Target *target = &*TargetRegistry::targets().begin();
Anders Waldenborga89c1e32013-10-17 10:25:24 +000054 return wrap(target);
Duncan Sands264d2e72012-04-11 10:25:24 +000055}
56LLVMTargetRef LLVMGetNextTarget(LLVMTargetRef T) {
57 return wrap(unwrap(T)->getNext());
58}
59
Peter Zotovb2c8b8a2013-11-15 02:51:01 +000060LLVMTargetRef LLVMGetTargetFromName(const char *Name) {
Peter Zotov5a6cfda2013-11-15 02:51:18 +000061 StringRef NameRef = Name;
David Blaikie46c561c2015-05-11 22:20:48 +000062 auto I = std::find_if(
63 TargetRegistry::targets().begin(), TargetRegistry::targets().end(),
64 [&](const Target &T) { return T.getName() == NameRef; });
65 return I != TargetRegistry::targets().end() ? wrap(&*I) : nullptr;
Peter Zotov7b61b752013-11-06 10:25:18 +000066}
67
68LLVMBool LLVMGetTargetFromTriple(const char* TripleStr, LLVMTargetRef *T,
69 char **ErrorMessage) {
70 std::string Error;
Mehdi Amini0ab4b5b2015-08-26 21:16:29 +000071
Peter Zotov7b61b752013-11-06 10:25:18 +000072 *T = wrap(TargetRegistry::lookupTarget(TripleStr, Error));
Mehdi Amini0ab4b5b2015-08-26 21:16:29 +000073
Peter Zotov7b61b752013-11-06 10:25:18 +000074 if (!*T) {
75 if (ErrorMessage)
76 *ErrorMessage = strdup(Error.c_str());
77
78 return 1;
79 }
Mehdi Amini0ab4b5b2015-08-26 21:16:29 +000080
Peter Zotov7b61b752013-11-06 10:25:18 +000081 return 0;
82}
83
Duncan Sands264d2e72012-04-11 10:25:24 +000084const char * LLVMGetTargetName(LLVMTargetRef T) {
85 return unwrap(T)->getName();
86}
87
88const char * LLVMGetTargetDescription(LLVMTargetRef T) {
89 return unwrap(T)->getShortDescription();
90}
91
92LLVMBool LLVMTargetHasJIT(LLVMTargetRef T) {
93 return unwrap(T)->hasJIT();
94}
95
96LLVMBool LLVMTargetHasTargetMachine(LLVMTargetRef T) {
97 return unwrap(T)->hasTargetMachine();
98}
99
100LLVMBool LLVMTargetHasAsmBackend(LLVMTargetRef T) {
101 return unwrap(T)->hasMCAsmBackend();
102}
103
Peter Zotov0e38fc82013-11-15 02:51:12 +0000104LLVMTargetMachineRef LLVMCreateTargetMachine(LLVMTargetRef T,
105 const char* Triple, const char* CPU, const char* Features,
106 LLVMCodeGenOptLevel Level, LLVMRelocMode Reloc,
107 LLVMCodeModel CodeModel) {
Duncan Sands264d2e72012-04-11 10:25:24 +0000108 Reloc::Model RM;
109 switch (Reloc){
110 case LLVMRelocStatic:
111 RM = Reloc::Static;
112 break;
113 case LLVMRelocPIC:
114 RM = Reloc::PIC_;
115 break;
116 case LLVMRelocDynamicNoPic:
117 RM = Reloc::DynamicNoPIC;
118 break;
119 default:
120 RM = Reloc::Default;
121 break;
122 }
123
Filip Pizlo85e0d272013-05-01 22:58:00 +0000124 CodeModel::Model CM = unwrap(CodeModel);
Duncan Sands264d2e72012-04-11 10:25:24 +0000125
Filip Pizlo85e0d272013-05-01 22:58:00 +0000126 CodeGenOpt::Level OL;
Duncan Sands264d2e72012-04-11 10:25:24 +0000127 switch (Level) {
128 case LLVMCodeGenLevelNone:
129 OL = CodeGenOpt::None;
130 break;
131 case LLVMCodeGenLevelLess:
132 OL = CodeGenOpt::Less;
133 break;
134 case LLVMCodeGenLevelAggressive:
135 OL = CodeGenOpt::Aggressive;
136 break;
137 default:
138 OL = CodeGenOpt::Default;
139 break;
140 }
141
142 TargetOptions opt;
143 return wrap(unwrap(T)->createTargetMachine(Triple, CPU, Features, opt, RM,
144 CM, OL));
145}
146
Mehdi Amini0ab4b5b2015-08-26 21:16:29 +0000147void LLVMDisposeTargetMachine(LLVMTargetMachineRef T) { delete unwrap(T); }
Duncan Sands264d2e72012-04-11 10:25:24 +0000148
149LLVMTargetRef LLVMGetTargetMachineTarget(LLVMTargetMachineRef T) {
150 const Target* target = &(unwrap(T)->getTarget());
151 return wrap(target);
152}
153
154char* LLVMGetTargetMachineTriple(LLVMTargetMachineRef T) {
Daniel Sanders335487a2015-06-16 13:15:50 +0000155 std::string StringRep = unwrap(T)->getTargetTriple().str();
Duncan Sands264d2e72012-04-11 10:25:24 +0000156 return strdup(StringRep.c_str());
157}
158
159char* LLVMGetTargetMachineCPU(LLVMTargetMachineRef T) {
160 std::string StringRep = unwrap(T)->getTargetCPU();
161 return strdup(StringRep.c_str());
162}
163
164char* LLVMGetTargetMachineFeatureString(LLVMTargetMachineRef T) {
165 std::string StringRep = unwrap(T)->getTargetFeatureString();
166 return strdup(StringRep.c_str());
167}
168
Peter Zotov7b61b752013-11-06 10:25:18 +0000169void LLVMSetTargetMachineAsmVerbosity(LLVMTargetMachineRef T,
170 LLVMBool VerboseAsm) {
Rafael Espindola3105fd82015-02-12 21:16:34 +0000171 unwrap(T)->Options.MCOptions.AsmVerbose = VerboseAsm;
Peter Zotov7b61b752013-11-06 10:25:18 +0000172}
173
Tom Stellardec924c52013-04-16 23:12:56 +0000174static LLVMBool LLVMTargetMachineEmit(LLVMTargetMachineRef T, LLVMModuleRef M,
Rafael Espindola5560a4c2015-04-14 22:14:34 +0000175 raw_pwrite_stream &OS,
Rafael Espindolaf546d0f2015-04-09 17:16:25 +0000176 LLVMCodeGenFileType codegen,
177 char **ErrorMessage) {
Duncan Sands264d2e72012-04-11 10:25:24 +0000178 TargetMachine* TM = unwrap(T);
179 Module* Mod = unwrap(M);
180
Chandler Carruth30d69c22015-02-13 10:01:29 +0000181 legacy::PassManager pass;
Duncan Sands264d2e72012-04-11 10:25:24 +0000182
183 std::string error;
184
Mehdi Amini26d48132015-07-24 16:04:22 +0000185 Mod->setDataLayout(TM->createDataLayout());
Duncan Sands264d2e72012-04-11 10:25:24 +0000186
187 TargetMachine::CodeGenFileType ft;
188 switch (codegen) {
189 case LLVMAssemblyFile:
190 ft = TargetMachine::CGFT_AssemblyFile;
191 break;
192 default:
193 ft = TargetMachine::CGFT_ObjectFile;
194 break;
195 }
Tom Stellardec924c52013-04-16 23:12:56 +0000196 if (TM->addPassesToEmitFile(pass, OS, ft)) {
Nick Lewycky7b287ee2013-03-10 22:01:44 +0000197 error = "TargetMachine can't emit a file of this type";
Duncan Sands264d2e72012-04-11 10:25:24 +0000198 *ErrorMessage = strdup(error.c_str());
199 return true;
200 }
201
202 pass.run(*Mod);
203
Tom Stellardec924c52013-04-16 23:12:56 +0000204 OS.flush();
Duncan Sands264d2e72012-04-11 10:25:24 +0000205 return false;
206}
Tom Stellardec924c52013-04-16 23:12:56 +0000207
208LLVMBool LLVMTargetMachineEmitToFile(LLVMTargetMachineRef T, LLVMModuleRef M,
209 char* Filename, LLVMCodeGenFileType codegen, char** ErrorMessage) {
Rafael Espindola3fd1e992014-08-25 18:16:47 +0000210 std::error_code EC;
211 raw_fd_ostream dest(Filename, EC, sys::fs::F_None);
212 if (EC) {
213 *ErrorMessage = strdup(EC.message().c_str());
Tom Stellardec924c52013-04-16 23:12:56 +0000214 return true;
215 }
Rafael Espindola5682ce22015-04-09 21:06:08 +0000216 bool Result = LLVMTargetMachineEmit(T, M, dest, codegen, ErrorMessage);
Tom Stellardec924c52013-04-16 23:12:56 +0000217 dest.flush();
218 return Result;
219}
220
221LLVMBool LLVMTargetMachineEmitToMemoryBuffer(LLVMTargetMachineRef T,
222 LLVMModuleRef M, LLVMCodeGenFileType codegen, char** ErrorMessage,
223 LLVMMemoryBufferRef *OutMemBuf) {
Rafael Espindolaf546d0f2015-04-09 17:16:25 +0000224 SmallString<0> CodeString;
225 raw_svector_ostream OStream(CodeString);
Rafael Espindola5682ce22015-04-09 21:06:08 +0000226 bool Result = LLVMTargetMachineEmit(T, M, OStream, codegen, ErrorMessage);
Tom Stellardec924c52013-04-16 23:12:56 +0000227
Rafael Espindolaf546d0f2015-04-09 17:16:25 +0000228 StringRef Data = OStream.str();
229 *OutMemBuf =
230 LLVMCreateMemoryBufferWithMemoryRangeCopy(Data.data(), Data.size(), "");
Tom Stellardec924c52013-04-16 23:12:56 +0000231 return Result;
232}
Peter Zotov7b61b752013-11-06 10:25:18 +0000233
234char *LLVMGetDefaultTargetTriple(void) {
235 return strdup(sys::getDefaultTargetTriple().c_str());
236}
Juergen Ributzka5fe955c2014-01-23 19:23:28 +0000237
238void LLVMAddAnalysisPasses(LLVMTargetMachineRef T, LLVMPassManagerRef PM) {
Chandler Carruth5ec2b1d2015-02-01 12:26:09 +0000239 unwrap(PM)->add(
240 createTargetTransformInfoWrapperPass(unwrap(T)->getTargetIRAnalysis()));
Juergen Ributzka5fe955c2014-01-23 19:23:28 +0000241}