LLVM API Documentation

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
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"
17 #include "llvm/IR/DataLayout.h"
18 #include "llvm/IR/Module.h"
19 #include "llvm/PassManager.h"
20 #include "llvm/Support/CodeGen.h"
24 #include "llvm/Support/Host.h"
26 #include <cassert>
27 #include <cstdlib>
28 #include <cstring>
29 
30 using namespace llvm;
31 
33  return reinterpret_cast<DataLayout*>(P);
34 }
35 
37  return reinterpret_cast<LLVMTargetDataRef>(const_cast<DataLayout*>(P));
38 }
39 
41  return reinterpret_cast<TargetLibraryInfo*>(P);
42 }
43 
45  TargetLibraryInfo *X = const_cast<TargetLibraryInfo*>(P);
46  return reinterpret_cast<LLVMTargetLibraryInfoRef>(X);
47 }
48 
50  return reinterpret_cast<TargetMachine*>(P);
51 }
53  return reinterpret_cast<Target*>(P);
54 }
56  return
57  reinterpret_cast<LLVMTargetMachineRef>(const_cast<TargetMachine*>(P));
58 }
59 inline LLVMTargetRef wrap(const Target * P) {
60  return reinterpret_cast<LLVMTargetRef>(const_cast<Target*>(P));
61 }
62 
65  return NULL;
66  }
67 
68  const Target* target = &*TargetRegistry::begin();
69  return wrap(target);
70 }
72  return wrap(unwrap(T)->getNext());
73 }
74 
76  StringRef NameRef = Name;
78  IE = TargetRegistry::end(); IT != IE; ++IT) {
79  if (IT->getName() == NameRef)
80  return wrap(&*IT);
81  }
82 
83  return NULL;
84 }
85 
87  char **ErrorMessage) {
88  std::string Error;
89 
90  *T = wrap(TargetRegistry::lookupTarget(TripleStr, Error));
91 
92  if (!*T) {
93  if (ErrorMessage)
94  *ErrorMessage = strdup(Error.c_str());
95 
96  return 1;
97  }
98 
99  return 0;
100 }
101 
103  return unwrap(T)->getName();
104 }
105 
107  return unwrap(T)->getShortDescription();
108 }
109 
111  return unwrap(T)->hasJIT();
112 }
113 
115  return unwrap(T)->hasTargetMachine();
116 }
117 
119  return unwrap(T)->hasMCAsmBackend();
120 }
121 
123  const char* Triple, const char* CPU, const char* Features,
125  LLVMCodeModel CodeModel) {
127  switch (Reloc){
128  case LLVMRelocStatic:
129  RM = Reloc::Static;
130  break;
131  case LLVMRelocPIC:
132  RM = Reloc::PIC_;
133  break;
135  RM = Reloc::DynamicNoPIC;
136  break;
137  default:
138  RM = Reloc::Default;
139  break;
140  }
141 
142  CodeModel::Model CM = unwrap(CodeModel);
143 
145  switch (Level) {
147  OL = CodeGenOpt::None;
148  break;
150  OL = CodeGenOpt::Less;
151  break;
154  break;
155  default:
156  OL = CodeGenOpt::Default;
157  break;
158  }
159 
160  TargetOptions opt;
161  return wrap(unwrap(T)->createTargetMachine(Triple, CPU, Features, opt, RM,
162  CM, OL));
163 }
164 
165 
167  delete unwrap(T);
168 }
169 
171  const Target* target = &(unwrap(T)->getTarget());
172  return wrap(target);
173 }
174 
176  std::string StringRep = unwrap(T)->getTargetTriple();
177  return strdup(StringRep.c_str());
178 }
179 
181  std::string StringRep = unwrap(T)->getTargetCPU();
182  return strdup(StringRep.c_str());
183 }
184 
186  std::string StringRep = unwrap(T)->getTargetFeatureString();
187  return strdup(StringRep.c_str());
188 }
189 
191  return wrap(unwrap(T)->getDataLayout());
192 }
193 
195  LLVMBool VerboseAsm) {
196  unwrap(T)->setAsmVerbosityDefault(VerboseAsm);
197 }
198 
200  formatted_raw_ostream &OS, LLVMCodeGenFileType codegen, char **ErrorMessage) {
201  TargetMachine* TM = unwrap(T);
202  Module* Mod = unwrap(M);
203 
204  PassManager pass;
205 
206  std::string error;
207 
208  const DataLayout* td = TM->getDataLayout();
209 
210  if (!td) {
211  error = "No DataLayout in TargetMachine";
212  *ErrorMessage = strdup(error.c_str());
213  return true;
214  }
215  pass.add(new DataLayout(*td));
216 
218  switch (codegen) {
219  case LLVMAssemblyFile:
221  break;
222  default:
224  break;
225  }
226  if (TM->addPassesToEmitFile(pass, OS, ft)) {
227  error = "TargetMachine can't emit a file of this type";
228  *ErrorMessage = strdup(error.c_str());
229  return true;
230  }
231 
232  pass.run(*Mod);
233 
234  OS.flush();
235  return false;
236 }
237 
239  char* Filename, LLVMCodeGenFileType codegen, char** ErrorMessage) {
240  std::string error;
241  raw_fd_ostream dest(Filename, error, sys::fs::F_Binary);
242  if (!error.empty()) {
243  *ErrorMessage = strdup(error.c_str());
244  return true;
245  }
246  formatted_raw_ostream destf(dest);
247  bool Result = LLVMTargetMachineEmit(T, M, destf, codegen, ErrorMessage);
248  dest.flush();
249  return Result;
250 }
251 
253  LLVMModuleRef M, LLVMCodeGenFileType codegen, char** ErrorMessage,
254  LLVMMemoryBufferRef *OutMemBuf) {
255  std::string CodeString;
256  raw_string_ostream OStream(CodeString);
257  formatted_raw_ostream Out(OStream);
258  bool Result = LLVMTargetMachineEmit(T, M, Out, codegen, ErrorMessage);
259  OStream.flush();
260 
261  std::string &Data = OStream.str();
262  *OutMemBuf = LLVMCreateMemoryBufferWithMemoryRangeCopy(Data.c_str(),
263  Data.length(), "");
264  return Result;
265 }
266 
269 }
LLVMMemoryBufferRef LLVMCreateMemoryBufferWithMemoryRangeCopy(const char *InputData, size_t InputDataLength, const char *BufferName)
Definition: Core.cpp:2553
The main container class for the LLVM Intermediate Representation.
Definition: Module.h:112
int LLVMBool
Definition: Core.h:65
char * LLVMGetDefaultTargetTriple(void)
LLVMCodeGenOptLevel
std::string getDefaultTargetTriple()
LLVMContext ** unwrap(LLVMContextRef *Tys)
Definition: LLVMContext.h:119
static const Target * lookupTarget(const std::string &Triple, std::string &Error)
struct LLVMOpaqueTargetData * LLVMTargetDataRef
Definition: Target.h:42
LLVMTargetRef LLVMGetTargetMachineTarget(LLVMTargetMachineRef T)
static iterator end()
LLVMBool LLVMTargetHasAsmBackend(LLVMTargetRef T)
LLVMCodeGenFileType
struct LLVMOpaqueTargetMachine * LLVMTargetMachineRef
static cl::opt< ITMode > IT(cl::desc("IT block support"), cl::Hidden, cl::init(DefaultIT), cl::ZeroOrMore, cl::values(clEnumValN(DefaultIT,"arm-default-it","Generate IT block based on arch"), clEnumValN(RestrictedIT,"arm-restrict-it","Disallow deprecated IT based on ARMv8"), clEnumValN(NoRestrictedIT,"arm-no-restrict-it","Allow IT blocks based on ARMv7"), clEnumValEnd))
struct LLVMOpaqueMemoryBuffer * LLVMMemoryBufferRef
Definition: Core.h:122
static LLVMBool LLVMTargetMachineEmit(LLVMTargetMachineRef T, LLVMModuleRef M, formatted_raw_ostream &OS, LLVMCodeGenFileType codegen, char **ErrorMessage)
void LLVMSetTargetMachineAsmVerbosity(LLVMTargetMachineRef T, LLVMBool VerboseAsm)
const char * LLVMGetTargetName(LLVMTargetRef T)
char * LLVMGetTargetMachineFeatureString(LLVMTargetMachineRef T)
LLVMTargetDataRef LLVMGetTargetMachineData(LLVMTargetMachineRef T)
LLVMBool LLVMTargetMachineEmitToMemoryBuffer(LLVMTargetMachineRef T, LLVMModuleRef M, LLVMCodeGenFileType codegen, char **ErrorMessage, LLVMMemoryBufferRef *OutMemBuf)
#define P(N)
LLVMBool LLVMGetTargetFromTriple(const char *TripleStr, LLVMTargetRef *T, char **ErrorMessage)
void LLVMDisposeTargetMachine(LLVMTargetMachineRef T)
struct LLVMTarget * LLVMTargetRef
LLVMCodeModel
virtual bool addPassesToEmitFile(PassManagerBase &, formatted_raw_ostream &, CodeGenFileType, bool=true, AnalysisID=0, AnalysisID=0)
static iterator begin()
LLVMBool LLVMTargetMachineEmitToFile(LLVMTargetMachineRef T, LLVMModuleRef M, char *Filename, LLVMCodeGenFileType codegen, char **ErrorMessage)
Two Address instruction pass
const char * LLVMGetTargetDescription(LLVMTargetRef T)
LLVMBool LLVMTargetHasTargetMachine(LLVMTargetRef T)
LLVMTargetRef LLVMGetFirstTarget()
std::string & str()
Definition: raw_ostream.h:441
char *strdup(const char *s1);
char * LLVMGetTargetMachineTriple(LLVMTargetMachineRef T)
SmallVectorImpl< T >::const_pointer c_str(SmallVectorImpl< T > &str)
Definition: Windows.h:154
LLVMTargetRef LLVMGetNextTarget(LLVMTargetRef T)
LLVMContextRef * wrap(const LLVMContext **Tys)
Definition: LLVMContext.h:123
struct LLVMOpaqueTargetLibraryInfotData * LLVMTargetLibraryInfoRef
Definition: Target.h:43
char * LLVMGetTargetMachineCPU(LLVMTargetMachineRef T)
LLVMRelocMode
LLVMBool LLVMTargetHasJIT(LLVMTargetRef T)
virtual const DataLayout * getDataLayout() const
LLVMTargetMachineRef LLVMCreateTargetMachine(LLVMTargetRef T, const char *Triple, const char *CPU, const char *Features, LLVMCodeGenOptLevel Level, LLVMRelocMode Reloc, LLVMCodeModel CodeModel)
struct LLVMOpaqueModule * LLVMModuleRef
Definition: Core.h:80
LLVMTargetRef LLVMGetTargetFromName(const char *Name)
static RegisterPass< NVPTXAllocaHoisting > X("alloca-hoisting","Hoisting alloca instructions in non-entry ""blocks to the entry block")