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/TargetMachine.h"
15 #include "llvm-c/Core.h"
16 #include "llvm-c/Target.h"
17 #include "llvm/Analysis/TargetTransformInfo.h"
18 #include "llvm/IR/DataLayout.h"
19 #include "llvm/IR/Module.h"
20 #include "llvm/IR/LegacyPassManager.h"
21 #include "llvm/Support/CodeGenCWrappers.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/TargetMachine.h"
28 #include "llvm/Target/TargetSubtargetInfo.h"
29 #include <cassert>
30 #include <cstdlib>
31 #include <cstring>
32 
33 using namespace llvm;
34 
35 static TargetMachine *unwrap(LLVMTargetMachineRef P) {
36   return reinterpret_cast<TargetMachine *>(P);
37 }
38 static Target *unwrap(LLVMTargetRef P) {
39   return reinterpret_cast<Target*>(P);
40 }
41 static LLVMTargetMachineRef wrap(const TargetMachine *P) {
42   return reinterpret_cast<LLVMTargetMachineRef>(const_cast<TargetMachine *>(P));
43 }
44 static LLVMTargetRef wrap(const Target * P) {
45   return reinterpret_cast<LLVMTargetRef>(const_cast<Target*>(P));
46 }
47 
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 }
56 LLVMTargetRef LLVMGetNextTarget(LLVMTargetRef T) {
57   return wrap(unwrap(T)->getNext());
58 }
59 
60 LLVMTargetRef LLVMGetTargetFromName(const char *Name) {
61   StringRef NameRef = Name;
62   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;
66 }
67 
68 LLVMBool LLVMGetTargetFromTriple(const char* TripleStr, LLVMTargetRef *T,
69                                  char **ErrorMessage) {
70   std::string Error;
71 
72   *T = wrap(TargetRegistry::lookupTarget(TripleStr, Error));
73 
74   if (!*T) {
75     if (ErrorMessage)
76       *ErrorMessage = strdup(Error.c_str());
77 
78     return 1;
79   }
80 
81   return 0;
82 }
83 
84 const char * LLVMGetTargetName(LLVMTargetRef T) {
85   return unwrap(T)->getName();
86 }
87 
88 const char * LLVMGetTargetDescription(LLVMTargetRef T) {
89   return unwrap(T)->getShortDescription();
90 }
91 
92 LLVMBool LLVMTargetHasJIT(LLVMTargetRef T) {
93   return unwrap(T)->hasJIT();
94 }
95 
96 LLVMBool LLVMTargetHasTargetMachine(LLVMTargetRef T) {
97   return unwrap(T)->hasTargetMachine();
98 }
99 
100 LLVMBool LLVMTargetHasAsmBackend(LLVMTargetRef T) {
101   return unwrap(T)->hasMCAsmBackend();
102 }
103 
104 LLVMTargetMachineRef LLVMCreateTargetMachine(LLVMTargetRef T,
105         const char* Triple, const char* CPU, const char* Features,
106         LLVMCodeGenOptLevel Level, LLVMRelocMode Reloc,
107         LLVMCodeModel CodeModel) {
108   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 
124   CodeModel::Model CM = unwrap(CodeModel);
125 
126   CodeGenOpt::Level OL;
127   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 
147 void LLVMDisposeTargetMachine(LLVMTargetMachineRef T) { delete unwrap(T); }
148 
149 LLVMTargetRef LLVMGetTargetMachineTarget(LLVMTargetMachineRef T) {
150   const Target* target = &(unwrap(T)->getTarget());
151   return wrap(target);
152 }
153 
154 char* LLVMGetTargetMachineTriple(LLVMTargetMachineRef T) {
155   std::string StringRep = unwrap(T)->getTargetTriple().str();
156   return strdup(StringRep.c_str());
157 }
158 
159 char* LLVMGetTargetMachineCPU(LLVMTargetMachineRef T) {
160   std::string StringRep = unwrap(T)->getTargetCPU();
161   return strdup(StringRep.c_str());
162 }
163 
164 char* LLVMGetTargetMachineFeatureString(LLVMTargetMachineRef T) {
165   std::string StringRep = unwrap(T)->getTargetFeatureString();
166   return strdup(StringRep.c_str());
167 }
168 
169 void LLVMSetTargetMachineAsmVerbosity(LLVMTargetMachineRef T,
170                                       LLVMBool VerboseAsm) {
171   unwrap(T)->Options.MCOptions.AsmVerbose = VerboseAsm;
172 }
173 
174 LLVMTargetDataRef LLVMCreateTargetDataLayout(LLVMTargetMachineRef T) {
175   return wrap(new DataLayout(unwrap(T)->createDataLayout()));
176 }
177 
178 static LLVMBool LLVMTargetMachineEmit(LLVMTargetMachineRef T, LLVMModuleRef M,
179                                       raw_pwrite_stream &OS,
180                                       LLVMCodeGenFileType codegen,
181                                       char **ErrorMessage) {
182   TargetMachine* TM = unwrap(T);
183   Module* Mod = unwrap(M);
184 
185   legacy::PassManager pass;
186 
187   std::string error;
188 
189   Mod->setDataLayout(TM->createDataLayout());
190 
191   TargetMachine::CodeGenFileType ft;
192   switch (codegen) {
193     case LLVMAssemblyFile:
194       ft = TargetMachine::CGFT_AssemblyFile;
195       break;
196     default:
197       ft = TargetMachine::CGFT_ObjectFile;
198       break;
199   }
200   if (TM->addPassesToEmitFile(pass, OS, ft)) {
201     error = "TargetMachine can't emit a file of this type";
202     *ErrorMessage = strdup(error.c_str());
203     return true;
204   }
205 
206   pass.run(*Mod);
207 
208   OS.flush();
209   return false;
210 }
211 
212 LLVMBool LLVMTargetMachineEmitToFile(LLVMTargetMachineRef T, LLVMModuleRef M,
213   char* Filename, LLVMCodeGenFileType codegen, char** ErrorMessage) {
214   std::error_code EC;
215   raw_fd_ostream dest(Filename, EC, sys::fs::F_None);
216   if (EC) {
217     *ErrorMessage = strdup(EC.message().c_str());
218     return true;
219   }
220   bool Result = LLVMTargetMachineEmit(T, M, dest, codegen, ErrorMessage);
221   dest.flush();
222   return Result;
223 }
224 
225 LLVMBool LLVMTargetMachineEmitToMemoryBuffer(LLVMTargetMachineRef T,
226   LLVMModuleRef M, LLVMCodeGenFileType codegen, char** ErrorMessage,
227   LLVMMemoryBufferRef *OutMemBuf) {
228   SmallString<0> CodeString;
229   raw_svector_ostream OStream(CodeString);
230   bool Result = LLVMTargetMachineEmit(T, M, OStream, codegen, ErrorMessage);
231 
232   StringRef Data = OStream.str();
233   *OutMemBuf =
234       LLVMCreateMemoryBufferWithMemoryRangeCopy(Data.data(), Data.size(), "");
235   return Result;
236 }
237 
238 char *LLVMGetDefaultTargetTriple(void) {
239   return strdup(sys::getDefaultTargetTriple().c_str());
240 }
241 
242 void LLVMAddAnalysisPasses(LLVMTargetMachineRef T, LLVMPassManagerRef PM) {
243   unwrap(PM)->add(
244       createTargetTransformInfoWrapperPass(unwrap(T)->getTargetIRAnalysis()));
245 }
246