1 //===-- TargetMachine.cpp -------------------------------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements the LLVM-C part of TargetMachine.h
12 //===----------------------------------------------------------------------===//
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"
21 #include "llvm/Support/FormattedStream.h"
22 #include "llvm/Support/TargetRegistry.h"
23 #include "llvm/Support/raw_ostream.h"
24 #include "llvm/Support/Host.h"
25 #include "llvm/Target/TargetMachine.h"
32 inline DataLayout *unwrap(LLVMTargetDataRef P) {
33 return reinterpret_cast<DataLayout*>(P);
36 inline LLVMTargetDataRef wrap(const DataLayout *P) {
37 return reinterpret_cast<LLVMTargetDataRef>(const_cast<DataLayout*>(P));
40 inline TargetLibraryInfo *unwrap(LLVMTargetLibraryInfoRef P) {
41 return reinterpret_cast<TargetLibraryInfo*>(P);
44 inline LLVMTargetLibraryInfoRef wrap(const TargetLibraryInfo *P) {
45 TargetLibraryInfo *X = const_cast<TargetLibraryInfo*>(P);
46 return reinterpret_cast<LLVMTargetLibraryInfoRef>(X);
49 inline TargetMachine *unwrap(LLVMTargetMachineRef P) {
50 return reinterpret_cast<TargetMachine*>(P);
52 inline Target *unwrap(LLVMTargetRef P) {
53 return reinterpret_cast<Target*>(P);
55 inline LLVMTargetMachineRef wrap(const TargetMachine *P) {
57 reinterpret_cast<LLVMTargetMachineRef>(const_cast<TargetMachine*>(P));
59 inline LLVMTargetRef wrap(const Target * P) {
60 return reinterpret_cast<LLVMTargetRef>(const_cast<Target*>(P));
63 LLVMTargetRef LLVMGetFirstTarget() {
64 if(TargetRegistry::begin() == TargetRegistry::end()) {
68 const Target* target = &*TargetRegistry::begin();
71 LLVMTargetRef LLVMGetNextTarget(LLVMTargetRef T) {
72 return wrap(unwrap(T)->getNext());
75 LLVMTargetRef LLVMGetTargetFromName(const char *Name) {
76 for (TargetRegistry::iterator IT = TargetRegistry::begin(),
77 IE = TargetRegistry::end(); IT != IE; ++IT) {
78 if (IT->getName() == Name)
85 LLVMBool LLVMGetTargetFromTriple(const char* TripleStr, LLVMTargetRef *T,
86 char **ErrorMessage) {
89 *T = wrap(TargetRegistry::lookupTarget(TripleStr, Error));
93 *ErrorMessage = strdup(Error.c_str());
101 const char * LLVMGetTargetName(LLVMTargetRef T) {
102 return unwrap(T)->getName();
105 const char * LLVMGetTargetDescription(LLVMTargetRef T) {
106 return unwrap(T)->getShortDescription();
109 LLVMBool LLVMTargetHasJIT(LLVMTargetRef T) {
110 return unwrap(T)->hasJIT();
113 LLVMBool LLVMTargetHasTargetMachine(LLVMTargetRef T) {
114 return unwrap(T)->hasTargetMachine();
117 LLVMBool LLVMTargetHasAsmBackend(LLVMTargetRef T) {
118 return unwrap(T)->hasMCAsmBackend();
121 LLVMTargetMachineRef LLVMCreateTargetMachine(LLVMTargetRef T, char* Triple,
122 char* CPU, char* Features, LLVMCodeGenOptLevel Level, LLVMRelocMode Reloc,
123 LLVMCodeModel CodeModel) {
126 case LLVMRelocStatic:
132 case LLVMRelocDynamicNoPic:
133 RM = Reloc::DynamicNoPIC;
140 CodeModel::Model CM = unwrap(CodeModel);
142 CodeGenOpt::Level OL;
144 case LLVMCodeGenLevelNone:
145 OL = CodeGenOpt::None;
147 case LLVMCodeGenLevelLess:
148 OL = CodeGenOpt::Less;
150 case LLVMCodeGenLevelAggressive:
151 OL = CodeGenOpt::Aggressive;
154 OL = CodeGenOpt::Default;
159 return wrap(unwrap(T)->createTargetMachine(Triple, CPU, Features, opt, RM,
164 void LLVMDisposeTargetMachine(LLVMTargetMachineRef T) {
168 LLVMTargetRef LLVMGetTargetMachineTarget(LLVMTargetMachineRef T) {
169 const Target* target = &(unwrap(T)->getTarget());
173 char* LLVMGetTargetMachineTriple(LLVMTargetMachineRef T) {
174 std::string StringRep = unwrap(T)->getTargetTriple();
175 return strdup(StringRep.c_str());
178 char* LLVMGetTargetMachineCPU(LLVMTargetMachineRef T) {
179 std::string StringRep = unwrap(T)->getTargetCPU();
180 return strdup(StringRep.c_str());
183 char* LLVMGetTargetMachineFeatureString(LLVMTargetMachineRef T) {
184 std::string StringRep = unwrap(T)->getTargetFeatureString();
185 return strdup(StringRep.c_str());
188 LLVMTargetDataRef LLVMGetTargetMachineData(LLVMTargetMachineRef T) {
189 return wrap(unwrap(T)->getDataLayout());
192 void LLVMSetTargetMachineAsmVerbosity(LLVMTargetMachineRef T,
193 LLVMBool VerboseAsm) {
194 unwrap(T)->setAsmVerbosityDefault(VerboseAsm);
197 static LLVMBool LLVMTargetMachineEmit(LLVMTargetMachineRef T, LLVMModuleRef M,
198 formatted_raw_ostream &OS, LLVMCodeGenFileType codegen, char **ErrorMessage) {
199 TargetMachine* TM = unwrap(T);
200 Module* Mod = unwrap(M);
206 const DataLayout* td = TM->getDataLayout();
209 error = "No DataLayout in TargetMachine";
210 *ErrorMessage = strdup(error.c_str());
213 pass.add(new DataLayout(*td));
215 TargetMachine::CodeGenFileType ft;
217 case LLVMAssemblyFile:
218 ft = TargetMachine::CGFT_AssemblyFile;
221 ft = TargetMachine::CGFT_ObjectFile;
224 if (TM->addPassesToEmitFile(pass, OS, ft)) {
225 error = "TargetMachine can't emit a file of this type";
226 *ErrorMessage = strdup(error.c_str());
236 LLVMBool LLVMTargetMachineEmitToFile(LLVMTargetMachineRef T, LLVMModuleRef M,
237 char* Filename, LLVMCodeGenFileType codegen, char** ErrorMessage) {
239 raw_fd_ostream dest(Filename, error, sys::fs::F_Binary);
240 if (!error.empty()) {
241 *ErrorMessage = strdup(error.c_str());
244 formatted_raw_ostream destf(dest);
245 bool Result = LLVMTargetMachineEmit(T, M, destf, codegen, ErrorMessage);
250 LLVMBool LLVMTargetMachineEmitToMemoryBuffer(LLVMTargetMachineRef T,
251 LLVMModuleRef M, LLVMCodeGenFileType codegen, char** ErrorMessage,
252 LLVMMemoryBufferRef *OutMemBuf) {
253 std::string CodeString;
254 raw_string_ostream OStream(CodeString);
255 formatted_raw_ostream Out(OStream);
256 bool Result = LLVMTargetMachineEmit(T, M, Out, codegen, ErrorMessage);
259 std::string &Data = OStream.str();
260 *OutMemBuf = LLVMCreateMemoryBufferWithMemoryRangeCopy(Data.c_str(),
265 char *LLVMGetDefaultTargetTriple(void) {
266 return strdup(sys::getDefaultTargetTriple().c_str());