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/Host.h"
23 #include "llvm/Support/TargetRegistry.h"
24 #include "llvm/Support/raw_ostream.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 StringRef NameRef = Name;
77 for (TargetRegistry::iterator IT = TargetRegistry::begin(),
78 IE = TargetRegistry::end(); IT != IE; ++IT) {
79 if (IT->getName() == NameRef)
86 LLVMBool LLVMGetTargetFromTriple(const char* TripleStr, LLVMTargetRef *T,
87 char **ErrorMessage) {
90 *T = wrap(TargetRegistry::lookupTarget(TripleStr, Error));
94 *ErrorMessage = strdup(Error.c_str());
102 const char * LLVMGetTargetName(LLVMTargetRef T) {
103 return unwrap(T)->getName();
106 const char * LLVMGetTargetDescription(LLVMTargetRef T) {
107 return unwrap(T)->getShortDescription();
110 LLVMBool LLVMTargetHasJIT(LLVMTargetRef T) {
111 return unwrap(T)->hasJIT();
114 LLVMBool LLVMTargetHasTargetMachine(LLVMTargetRef T) {
115 return unwrap(T)->hasTargetMachine();
118 LLVMBool LLVMTargetHasAsmBackend(LLVMTargetRef T) {
119 return unwrap(T)->hasMCAsmBackend();
122 LLVMTargetMachineRef LLVMCreateTargetMachine(LLVMTargetRef T,
123 const char* Triple, const char* CPU, const char* Features,
124 LLVMCodeGenOptLevel Level, LLVMRelocMode Reloc,
125 LLVMCodeModel CodeModel) {
128 case LLVMRelocStatic:
134 case LLVMRelocDynamicNoPic:
135 RM = Reloc::DynamicNoPIC;
142 CodeModel::Model CM = unwrap(CodeModel);
144 CodeGenOpt::Level OL;
146 case LLVMCodeGenLevelNone:
147 OL = CodeGenOpt::None;
149 case LLVMCodeGenLevelLess:
150 OL = CodeGenOpt::Less;
152 case LLVMCodeGenLevelAggressive:
153 OL = CodeGenOpt::Aggressive;
156 OL = CodeGenOpt::Default;
161 return wrap(unwrap(T)->createTargetMachine(Triple, CPU, Features, opt, RM,
166 void LLVMDisposeTargetMachine(LLVMTargetMachineRef T) {
170 LLVMTargetRef LLVMGetTargetMachineTarget(LLVMTargetMachineRef T) {
171 const Target* target = &(unwrap(T)->getTarget());
175 char* LLVMGetTargetMachineTriple(LLVMTargetMachineRef T) {
176 std::string StringRep = unwrap(T)->getTargetTriple();
177 return strdup(StringRep.c_str());
180 char* LLVMGetTargetMachineCPU(LLVMTargetMachineRef T) {
181 std::string StringRep = unwrap(T)->getTargetCPU();
182 return strdup(StringRep.c_str());
185 char* LLVMGetTargetMachineFeatureString(LLVMTargetMachineRef T) {
186 std::string StringRep = unwrap(T)->getTargetFeatureString();
187 return strdup(StringRep.c_str());
190 LLVMTargetDataRef LLVMGetTargetMachineData(LLVMTargetMachineRef T) {
191 return wrap(unwrap(T)->getDataLayout());
194 void LLVMSetTargetMachineAsmVerbosity(LLVMTargetMachineRef T,
195 LLVMBool VerboseAsm) {
196 unwrap(T)->setAsmVerbosityDefault(VerboseAsm);
199 static LLVMBool LLVMTargetMachineEmit(LLVMTargetMachineRef T, LLVMModuleRef M,
200 formatted_raw_ostream &OS, LLVMCodeGenFileType codegen, char **ErrorMessage) {
201 TargetMachine* TM = unwrap(T);
202 Module* Mod = unwrap(M);
208 const DataLayout* td = TM->getDataLayout();
211 error = "No DataLayout in TargetMachine";
212 *ErrorMessage = strdup(error.c_str());
215 pass.add(new DataLayout(*td));
217 TargetMachine::CodeGenFileType ft;
219 case LLVMAssemblyFile:
220 ft = TargetMachine::CGFT_AssemblyFile;
223 ft = TargetMachine::CGFT_ObjectFile;
226 if (TM->addPassesToEmitFile(pass, OS, ft)) {
227 error = "TargetMachine can't emit a file of this type";
228 *ErrorMessage = strdup(error.c_str());
238 LLVMBool LLVMTargetMachineEmitToFile(LLVMTargetMachineRef T, LLVMModuleRef M,
239 char* Filename, LLVMCodeGenFileType codegen, char** ErrorMessage) {
241 raw_fd_ostream dest(Filename, error, sys::fs::F_Binary);
242 if (!error.empty()) {
243 *ErrorMessage = strdup(error.c_str());
246 formatted_raw_ostream destf(dest);
247 bool Result = LLVMTargetMachineEmit(T, M, destf, codegen, ErrorMessage);
252 LLVMBool LLVMTargetMachineEmitToMemoryBuffer(LLVMTargetMachineRef T,
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);
261 std::string &Data = OStream.str();
262 *OutMemBuf = LLVMCreateMemoryBufferWithMemoryRangeCopy(Data.c_str(),
267 char *LLVMGetDefaultTargetTriple(void) {
268 return strdup(sys::getDefaultTargetTriple().c_str());