LLVM  3.7.0
TargetMachineC.cpp
Go to the documentation of this file.
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"
18 #include "llvm/IR/DataLayout.h"
19 #include "llvm/IR/Module.h"
21 #include "llvm/Support/CodeGen.h"
24 #include "llvm/Support/Host.h"
29 #include <cassert>
30 #include <cstdlib>
31 #include <cstring>
32 
33 using namespace llvm;
34 
36  return reinterpret_cast<TargetMachine*>(P);
37 }
39  return reinterpret_cast<Target*>(P);
40 }
42  return
43  reinterpret_cast<LLVMTargetMachineRef>(const_cast<TargetMachine*>(P));
44 }
45 inline LLVMTargetRef wrap(const Target * P) {
46  return reinterpret_cast<LLVMTargetRef>(const_cast<Target*>(P));
47 }
48 
51  return nullptr;
52  }
53 
54  const Target *target = &*TargetRegistry::targets().begin();
55  return wrap(target);
56 }
58  return wrap(unwrap(T)->getNext());
59 }
60 
62  StringRef NameRef = Name;
63  auto I = std::find_if(
65  [&](const Target &T) { return T.getName() == NameRef; });
66  return I != TargetRegistry::targets().end() ? wrap(&*I) : nullptr;
67 }
68 
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 
86  return unwrap(T)->getName();
87 }
88 
90  return unwrap(T)->getShortDescription();
91 }
92 
94  return unwrap(T)->hasJIT();
95 }
96 
98  return unwrap(T)->hasTargetMachine();
99 }
100 
102  return unwrap(T)->hasMCAsmBackend();
103 }
104 
106  const char* Triple, const char* CPU, const char* Features,
108  LLVMCodeModel CodeModel) {
110  switch (Reloc){
111  case LLVMRelocStatic:
112  RM = Reloc::Static;
113  break;
114  case LLVMRelocPIC:
115  RM = Reloc::PIC_;
116  break;
118  RM = Reloc::DynamicNoPIC;
119  break;
120  default:
121  RM = Reloc::Default;
122  break;
123  }
124 
125  CodeModel::Model CM = unwrap(CodeModel);
126 
128  switch (Level) {
130  OL = CodeGenOpt::None;
131  break;
133  OL = CodeGenOpt::Less;
134  break;
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 
150  delete unwrap(T);
151 }
152 
154  const Target* target = &(unwrap(T)->getTarget());
155  return wrap(target);
156 }
157 
159  std::string StringRep = unwrap(T)->getTargetTriple().str();
160  return strdup(StringRep.c_str());
161 }
162 
164  std::string StringRep = unwrap(T)->getTargetCPU();
165  return strdup(StringRep.c_str());
166 }
167 
169  std::string StringRep = unwrap(T)->getTargetFeatureString();
170  return strdup(StringRep.c_str());
171 }
172 
174  return wrap(unwrap(T)->getDataLayout());
175 }
176 
178  LLVMBool VerboseAsm) {
179  unwrap(T)->Options.MCOptions.AsmVerbose = VerboseAsm;
180 }
181 
183  raw_pwrite_stream &OS,
184  LLVMCodeGenFileType codegen,
185  char **ErrorMessage) {
186  TargetMachine* TM = unwrap(T);
187  Module* Mod = unwrap(M);
188 
190 
191  std::string error;
192 
193  const DataLayout *td = TM->getDataLayout();
194 
195  if (!td) {
196  error = "No DataLayout in TargetMachine";
197  *ErrorMessage = strdup(error.c_str());
198  return true;
199  }
200  Mod->setDataLayout(*td);
201 
203  switch (codegen) {
204  case LLVMAssemblyFile:
206  break;
207  default:
209  break;
210  }
211  if (TM->addPassesToEmitFile(pass, OS, ft)) {
212  error = "TargetMachine can't emit a file of this type";
213  *ErrorMessage = strdup(error.c_str());
214  return true;
215  }
216 
217  pass.run(*Mod);
218 
219  OS.flush();
220  return false;
221 }
222 
224  char* Filename, LLVMCodeGenFileType codegen, char** ErrorMessage) {
225  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());
229  return true;
230  }
231  bool Result = LLVMTargetMachineEmit(T, M, dest, codegen, ErrorMessage);
232  dest.flush();
233  return Result;
234 }
235 
237  LLVMModuleRef M, LLVMCodeGenFileType codegen, char** ErrorMessage,
238  LLVMMemoryBufferRef *OutMemBuf) {
239  SmallString<0> CodeString;
240  raw_svector_ostream OStream(CodeString);
241  bool Result = LLVMTargetMachineEmit(T, M, OStream, codegen, ErrorMessage);
242  OStream.flush();
243 
244  StringRef Data = OStream.str();
245  *OutMemBuf =
247  return Result;
248 }
249 
251  return strdup(sys::getDefaultTargetTriple().c_str());
252 }
253 
255  unwrap(PM)->add(
256  createTargetTransformInfoWrapperPass(unwrap(T)->getTargetIRAnalysis()));
257 }
A parsed version of the target data layout string in and methods for querying it. ...
Definition: DataLayout.h:104
const_iterator end(StringRef path)
Get end iterator over path.
Definition: Path.cpp:240
size_t size() const
size - Get the string size.
Definition: StringRef.h:113
const char * getName() const
getName - Get the target name.
LLVMMemoryBufferRef LLVMCreateMemoryBufferWithMemoryRangeCopy(const char *InputData, size_t InputDataLength, const char *BufferName)
Definition: Core.cpp:2832
A Module instance is used to store all the information related to an LLVM module. ...
Definition: Module.h:114
struct LLVMOpaqueMemoryBuffer * LLVMMemoryBufferRef
Used to pass regions of memory through LLVM interfaces.
Definition: Support.h:36
char * LLVMGetDefaultTargetTriple(void)
Get a triple for the host machine as a string.
LLVMCodeGenOptLevel
std::string getDefaultTargetTriple()
getDefaultTargetTriple() - Return the default target triple the compiler has been configured to produ...
ImmutablePass * createTargetTransformInfoWrapperPass(TargetIRAnalysis TIRA)
Create an analysis pass wrapper around a TTI object.
const FeatureBitset Features
void setDataLayout(StringRef Desc)
Set the data layout.
Definition: Module.cpp:366
A raw_ostream that writes to an SmallVector or SmallString.
Definition: raw_ostream.h:488
const_iterator begin(StringRef path)
Get begin iterator over path.
Definition: Path.cpp:232
static const Target * lookupTarget(const std::string &Triple, std::string &Error)
lookupTarget - Lookup a target based on a target triple.
LLVMTargetRef LLVMGetTargetMachineTarget(LLVMTargetMachineRef T)
Returns the Target used in a TargetMachine.
LLVMBool LLVMTargetHasAsmBackend(LLVMTargetRef T)
Returns if the target as an ASM backend (required for emitting output)
LLVMCodeGenFileType
struct LLVMOpaqueTargetMachine * LLVMTargetMachineRef
void LLVMSetTargetMachineAsmVerbosity(LLVMTargetMachineRef T, LLVMBool VerboseAsm)
Set the target machine's ASM verbosity.
const char * LLVMGetTargetName(LLVMTargetRef T)
Returns the name of a target.
LLVMTargetDataRef wrap(const DataLayout *P)
Definition: DataLayout.h:469
const char * data() const
data - Get a pointer to the start of the string (which may not be null terminated).
Definition: StringRef.h:107
char * LLVMGetTargetMachineFeatureString(LLVMTargetMachineRef T)
Returns the feature string used creating this target machine.
LLVMTargetDataRef LLVMGetTargetMachineData(LLVMTargetMachineRef T)
Deprecated: use LLVMGetDataLayout(LLVMModuleRef M) instead.
SmallString - A SmallString is just a SmallVector with methods and accessors that make it work better...
Definition: SmallString.h:25
static std::error_code error(DiagnosticHandlerFunction DiagnosticHandler, std::error_code EC, const Twine &Message)
static iterator_range< iterator > targets()
LLVMBool LLVMTargetMachineEmitToMemoryBuffer(LLVMTargetMachineRef T, LLVMModuleRef M, LLVMCodeGenFileType codegen, char **ErrorMessage, LLVMMemoryBufferRef *OutMemBuf)
Compile the LLVM IR stored in M and store the result in OutMemBuf.
PassManager manages ModulePassManagers.
#define P(N)
DataLayout * unwrap(LLVMTargetDataRef P)
Definition: DataLayout.h:465
LLVMBool LLVMGetTargetFromTriple(const char *TripleStr, LLVMTargetRef *T, char **ErrorMessage)
Finds the target corresponding to the given triple and stores it in T.
void LLVMDisposeTargetMachine(LLVMTargetMachineRef T)
Dispose the LLVMTargetMachineRef instance generated by LLVMCreateTargetMachine.
struct LLVMTarget * LLVMTargetRef
LLVMCodeModel
LLVMBool LLVMTargetMachineEmitToFile(LLVMTargetMachineRef T, LLVMModuleRef M, char *Filename, LLVMCodeGenFileType codegen, char **ErrorMessage)
Emits an asm or object file for the given module to the filename.
static LLVMBool LLVMTargetMachineEmit(LLVMTargetMachineRef T, LLVMModuleRef M, raw_pwrite_stream &OS, LLVMCodeGenFileType codegen, char **ErrorMessage)
Two Address instruction pass
const char * LLVMGetTargetDescription(LLVMTargetRef T)
Returns the description of a target.
LLVMBool LLVMTargetHasTargetMachine(LLVMTargetRef T)
Returns if the target has a TargetMachine associated.
LLVMTargetRef LLVMGetFirstTarget()
Returns the first llvm::Target in the registered targets list.
bool run(Module &M)
run - Execute all of the passes scheduled for execution.
Triple - Helper class for working with autoconf configuration names.
Definition: Triple.h:44
char * LLVMGetTargetMachineTriple(LLVMTargetMachineRef T)
Returns the triple used creating this target machine.
SmallVectorImpl< T >::const_pointer c_str(SmallVectorImpl< T > &str)
LLVMTargetRef LLVMGetNextTarget(LLVMTargetRef T)
Returns the next llvm::Target given a previous one (or null if there's none)
const DataLayout * getDataLayout() const
Deprecated in 3.7, will be removed in 3.8.
Module.h This file contains the declarations for the Module class.
char * LLVMGetTargetMachineCPU(LLVMTargetMachineRef T)
Returns the cpu used creating this target machine.
Target - Wrapper for Target specific information.
virtual bool addPassesToEmitFile(PassManagerBase &, raw_pwrite_stream &, CodeGenFileType, bool=true, AnalysisID=nullptr, AnalysisID=nullptr, AnalysisID=nullptr, MachineFunctionInitializer *=nullptr)
Add passes to the specified pass manager to get the specified file emitted.
StringRef str()
Flushes the stream contents to the target vector and return a StringRef for the vector contents...
LLVMRelocMode
struct LLVMOpaquePassManager * LLVMPassManagerRef
Definition: Core.h:116
A raw_ostream that writes to a file descriptor.
Definition: raw_ostream.h:345
LLVMBool LLVMTargetHasJIT(LLVMTargetRef T)
Returns if the target has a JIT.
#define I(x, y, z)
Definition: MD5.cpp:54
LLVMTargetMachineRef LLVMCreateTargetMachine(LLVMTargetRef T, const char *Triple, const char *CPU, const char *Features, LLVMCodeGenOptLevel Level, LLVMRelocMode Reloc, LLVMCodeModel CodeModel)
Creates a new llvm::TargetMachine.
struct LLVMOpaqueModule * LLVMModuleRef
The top-level container for all other LLVM Intermediate Representation (IR) objects.
Definition: Core.h:78
An abstract base class for streams implementations that also support a pwrite operation.
Definition: raw_ostream.h:321
Primary interface to the complete machine description for the target machine.
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:40
This pass exposes codegen information to IR-level passes.
void LLVMAddAnalysisPasses(LLVMTargetMachineRef T, LLVMPassManagerRef PM)
Adds the target-specific analysis passes to the pass manager.
LLVMTargetRef LLVMGetTargetFromName(const char *Name)
Finds the target corresponding to the given name and stores it in T.
CodeGenFileType
These enums are meant to be passed into addPassesToEmitFile to indicate what type of file to emit...
struct LLVMOpaqueTargetData * LLVMTargetDataRef
Definition: DataLayout.h:32