1 //===-- 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
14 #include "llvm-c/Core.h"
15 #include "llvm-c/Target.h"
16 #include "llvm-c/TargetMachine.h"
17 #include "llvm/Analysis/TargetTransformInfo.h"
18 #include "llvm/IR/DataLayout.h"
19 #include "llvm/IR/LegacyPassManager.h"
20 #include "llvm/IR/Module.h"
21 #include "llvm/MC/SubtargetFeature.h"
22 #include "llvm/Support/FileSystem.h"
23 #include "llvm/Support/FormattedStream.h"
24 #include "llvm/Support/Host.h"
25 #include "llvm/Support/TargetRegistry.h"
26 #include "llvm/Support/raw_ostream.h"
27 #include "llvm/Target/CodeGenCWrappers.h"
28 #include "llvm/Target/TargetMachine.h"
29 #include <cassert>
30 #include <cstdlib>
31 #include <cstring>
32
33 using namespace llvm;
34
unwrap(LLVMTargetMachineRef P)35 static TargetMachine *unwrap(LLVMTargetMachineRef P) {
36 return reinterpret_cast<TargetMachine *>(P);
37 }
unwrap(LLVMTargetRef P)38 static Target *unwrap(LLVMTargetRef P) {
39 return reinterpret_cast<Target*>(P);
40 }
wrap(const TargetMachine * P)41 static LLVMTargetMachineRef wrap(const TargetMachine *P) {
42 return reinterpret_cast<LLVMTargetMachineRef>(const_cast<TargetMachine *>(P));
43 }
wrap(const Target * P)44 static LLVMTargetRef wrap(const Target * P) {
45 return reinterpret_cast<LLVMTargetRef>(const_cast<Target*>(P));
46 }
47
LLVMGetFirstTarget()48 LLVMTargetRef LLVMGetFirstTarget() {
49 if (TargetRegistry::targets().begin() == TargetRegistry::targets().end()) {
50 return nullptr;
51 }
52
53 const Target *target = &*TargetRegistry::targets().begin();
54 return wrap(target);
55 }
LLVMGetNextTarget(LLVMTargetRef T)56 LLVMTargetRef LLVMGetNextTarget(LLVMTargetRef T) {
57 return wrap(unwrap(T)->getNext());
58 }
59
LLVMGetTargetFromName(const char * Name)60 LLVMTargetRef LLVMGetTargetFromName(const char *Name) {
61 StringRef NameRef = Name;
62 auto I = find_if(TargetRegistry::targets(),
63 [&](const Target &T) { return T.getName() == NameRef; });
64 return I != TargetRegistry::targets().end() ? wrap(&*I) : nullptr;
65 }
66
LLVMGetTargetFromTriple(const char * TripleStr,LLVMTargetRef * T,char ** ErrorMessage)67 LLVMBool LLVMGetTargetFromTriple(const char* TripleStr, LLVMTargetRef *T,
68 char **ErrorMessage) {
69 std::string Error;
70
71 *T = wrap(TargetRegistry::lookupTarget(TripleStr, Error));
72
73 if (!*T) {
74 if (ErrorMessage)
75 *ErrorMessage = strdup(Error.c_str());
76
77 return 1;
78 }
79
80 return 0;
81 }
82
LLVMGetTargetName(LLVMTargetRef T)83 const char * LLVMGetTargetName(LLVMTargetRef T) {
84 return unwrap(T)->getName();
85 }
86
LLVMGetTargetDescription(LLVMTargetRef T)87 const char * LLVMGetTargetDescription(LLVMTargetRef T) {
88 return unwrap(T)->getShortDescription();
89 }
90
LLVMTargetHasJIT(LLVMTargetRef T)91 LLVMBool LLVMTargetHasJIT(LLVMTargetRef T) {
92 return unwrap(T)->hasJIT();
93 }
94
LLVMTargetHasTargetMachine(LLVMTargetRef T)95 LLVMBool LLVMTargetHasTargetMachine(LLVMTargetRef T) {
96 return unwrap(T)->hasTargetMachine();
97 }
98
LLVMTargetHasAsmBackend(LLVMTargetRef T)99 LLVMBool LLVMTargetHasAsmBackend(LLVMTargetRef T) {
100 return unwrap(T)->hasMCAsmBackend();
101 }
102
LLVMCreateTargetMachine(LLVMTargetRef T,const char * Triple,const char * CPU,const char * Features,LLVMCodeGenOptLevel Level,LLVMRelocMode Reloc,LLVMCodeModel CodeModel)103 LLVMTargetMachineRef LLVMCreateTargetMachine(LLVMTargetRef T,
104 const char *Triple, const char *CPU, const char *Features,
105 LLVMCodeGenOptLevel Level, LLVMRelocMode Reloc,
106 LLVMCodeModel CodeModel) {
107 Optional<Reloc::Model> RM;
108 switch (Reloc){
109 case LLVMRelocStatic:
110 RM = Reloc::Static;
111 break;
112 case LLVMRelocPIC:
113 RM = Reloc::PIC_;
114 break;
115 case LLVMRelocDynamicNoPic:
116 RM = Reloc::DynamicNoPIC;
117 break;
118 case LLVMRelocROPI:
119 RM = Reloc::ROPI;
120 break;
121 case LLVMRelocRWPI:
122 RM = Reloc::RWPI;
123 break;
124 case LLVMRelocROPI_RWPI:
125 RM = Reloc::ROPI_RWPI;
126 break;
127 default:
128 break;
129 }
130
131 bool JIT;
132 Optional<CodeModel::Model> CM = unwrap(CodeModel, JIT);
133
134 CodeGenOpt::Level OL;
135 switch (Level) {
136 case LLVMCodeGenLevelNone:
137 OL = CodeGenOpt::None;
138 break;
139 case LLVMCodeGenLevelLess:
140 OL = CodeGenOpt::Less;
141 break;
142 case LLVMCodeGenLevelAggressive:
143 OL = CodeGenOpt::Aggressive;
144 break;
145 default:
146 OL = CodeGenOpt::Default;
147 break;
148 }
149
150 TargetOptions opt;
151 return wrap(unwrap(T)->createTargetMachine(Triple, CPU, Features, opt, RM, CM,
152 OL, JIT));
153 }
154
LLVMDisposeTargetMachine(LLVMTargetMachineRef T)155 void LLVMDisposeTargetMachine(LLVMTargetMachineRef T) { delete unwrap(T); }
156
LLVMGetTargetMachineTarget(LLVMTargetMachineRef T)157 LLVMTargetRef LLVMGetTargetMachineTarget(LLVMTargetMachineRef T) {
158 const Target* target = &(unwrap(T)->getTarget());
159 return wrap(target);
160 }
161
LLVMGetTargetMachineTriple(LLVMTargetMachineRef T)162 char* LLVMGetTargetMachineTriple(LLVMTargetMachineRef T) {
163 std::string StringRep = unwrap(T)->getTargetTriple().str();
164 return strdup(StringRep.c_str());
165 }
166
LLVMGetTargetMachineCPU(LLVMTargetMachineRef T)167 char* LLVMGetTargetMachineCPU(LLVMTargetMachineRef T) {
168 std::string StringRep = unwrap(T)->getTargetCPU();
169 return strdup(StringRep.c_str());
170 }
171
LLVMGetTargetMachineFeatureString(LLVMTargetMachineRef T)172 char* LLVMGetTargetMachineFeatureString(LLVMTargetMachineRef T) {
173 std::string StringRep = unwrap(T)->getTargetFeatureString();
174 return strdup(StringRep.c_str());
175 }
176
LLVMSetTargetMachineAsmVerbosity(LLVMTargetMachineRef T,LLVMBool VerboseAsm)177 void LLVMSetTargetMachineAsmVerbosity(LLVMTargetMachineRef T,
178 LLVMBool VerboseAsm) {
179 unwrap(T)->Options.MCOptions.AsmVerbose = VerboseAsm;
180 }
181
LLVMCreateTargetDataLayout(LLVMTargetMachineRef T)182 LLVMTargetDataRef LLVMCreateTargetDataLayout(LLVMTargetMachineRef T) {
183 return wrap(new DataLayout(unwrap(T)->createDataLayout()));
184 }
185
LLVMTargetMachineEmit(LLVMTargetMachineRef T,LLVMModuleRef M,raw_pwrite_stream & OS,LLVMCodeGenFileType codegen,char ** ErrorMessage)186 static LLVMBool LLVMTargetMachineEmit(LLVMTargetMachineRef T, LLVMModuleRef M,
187 raw_pwrite_stream &OS,
188 LLVMCodeGenFileType codegen,
189 char **ErrorMessage) {
190 TargetMachine* TM = unwrap(T);
191 Module* Mod = unwrap(M);
192
193 legacy::PassManager pass;
194
195 std::string error;
196
197 Mod->setDataLayout(TM->createDataLayout());
198
199 TargetMachine::CodeGenFileType ft;
200 switch (codegen) {
201 case LLVMAssemblyFile:
202 ft = TargetMachine::CGFT_AssemblyFile;
203 break;
204 default:
205 ft = TargetMachine::CGFT_ObjectFile;
206 break;
207 }
208 if (TM->addPassesToEmitFile(pass, OS, nullptr, ft)) {
209 error = "TargetMachine can't emit a file of this type";
210 *ErrorMessage = strdup(error.c_str());
211 return true;
212 }
213
214 pass.run(*Mod);
215
216 OS.flush();
217 return false;
218 }
219
LLVMTargetMachineEmitToFile(LLVMTargetMachineRef T,LLVMModuleRef M,char * Filename,LLVMCodeGenFileType codegen,char ** ErrorMessage)220 LLVMBool LLVMTargetMachineEmitToFile(LLVMTargetMachineRef T, LLVMModuleRef M,
221 char* Filename, LLVMCodeGenFileType codegen, char** ErrorMessage) {
222 std::error_code EC;
223 raw_fd_ostream dest(Filename, EC, sys::fs::F_None);
224 if (EC) {
225 *ErrorMessage = strdup(EC.message().c_str());
226 return true;
227 }
228 bool Result = LLVMTargetMachineEmit(T, M, dest, codegen, ErrorMessage);
229 dest.flush();
230 return Result;
231 }
232
LLVMTargetMachineEmitToMemoryBuffer(LLVMTargetMachineRef T,LLVMModuleRef M,LLVMCodeGenFileType codegen,char ** ErrorMessage,LLVMMemoryBufferRef * OutMemBuf)233 LLVMBool LLVMTargetMachineEmitToMemoryBuffer(LLVMTargetMachineRef T,
234 LLVMModuleRef M, LLVMCodeGenFileType codegen, char** ErrorMessage,
235 LLVMMemoryBufferRef *OutMemBuf) {
236 SmallString<0> CodeString;
237 raw_svector_ostream OStream(CodeString);
238 bool Result = LLVMTargetMachineEmit(T, M, OStream, codegen, ErrorMessage);
239
240 StringRef Data = OStream.str();
241 *OutMemBuf =
242 LLVMCreateMemoryBufferWithMemoryRangeCopy(Data.data(), Data.size(), "");
243 return Result;
244 }
245
LLVMGetDefaultTargetTriple(void)246 char *LLVMGetDefaultTargetTriple(void) {
247 return strdup(sys::getDefaultTargetTriple().c_str());
248 }
249
LLVMNormalizeTargetTriple(const char * triple)250 char *LLVMNormalizeTargetTriple(const char* triple) {
251 return strdup(Triple::normalize(StringRef(triple)).c_str());
252 }
253
LLVMGetHostCPUName(void)254 char *LLVMGetHostCPUName(void) {
255 return strdup(sys::getHostCPUName().data());
256 }
257
LLVMGetHostCPUFeatures(void)258 char *LLVMGetHostCPUFeatures(void) {
259 SubtargetFeatures Features;
260 StringMap<bool> HostFeatures;
261
262 if (sys::getHostCPUFeatures(HostFeatures))
263 for (auto &F : HostFeatures)
264 Features.AddFeature(F.first(), F.second);
265
266 return strdup(Features.getString().c_str());
267 }
268
LLVMAddAnalysisPasses(LLVMTargetMachineRef T,LLVMPassManagerRef PM)269 void LLVMAddAnalysisPasses(LLVMTargetMachineRef T, LLVMPassManagerRef PM) {
270 unwrap(PM)->add(
271 createTargetTransformInfoWrapperPass(unwrap(T)->getTargetIRAnalysis()));
272 }
273