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/FileSystem.h"
22 #include "llvm/Support/FormattedStream.h"
23 #include "llvm/Support/Host.h"
24 #include "llvm/Support/TargetRegistry.h"
25 #include "llvm/Support/raw_ostream.h"
26 #include "llvm/Target/TargetMachine.h"
27 #include "llvm/Target/TargetSubtargetInfo.h"
34 inline TargetMachine *unwrap(LLVMTargetMachineRef P) {
35 return reinterpret_cast<TargetMachine*>(P);
37 inline Target *unwrap(LLVMTargetRef P) {
38 return reinterpret_cast<Target*>(P);
40 inline LLVMTargetMachineRef wrap(const TargetMachine *P) {
42 reinterpret_cast<LLVMTargetMachineRef>(const_cast<TargetMachine*>(P));
44 inline LLVMTargetRef wrap(const Target * P) {
45 return reinterpret_cast<LLVMTargetRef>(const_cast<Target*>(P));
48 LLVMTargetRef LLVMGetFirstTarget() {
49 if(TargetRegistry::begin() == TargetRegistry::end()) {
53 const Target* target = &*TargetRegistry::begin();
56 LLVMTargetRef LLVMGetNextTarget(LLVMTargetRef T) {
57 return wrap(unwrap(T)->getNext());
60 LLVMTargetRef LLVMGetTargetFromName(const char *Name) {
61 StringRef NameRef = Name;
62 for (TargetRegistry::iterator IT = TargetRegistry::begin(),
63 IE = TargetRegistry::end(); IT != IE; ++IT) {
64 if (IT->getName() == NameRef)
71 LLVMBool LLVMGetTargetFromTriple(const char* TripleStr, LLVMTargetRef *T,
72 char **ErrorMessage) {
75 *T = wrap(TargetRegistry::lookupTarget(TripleStr, Error));
79 *ErrorMessage = strdup(Error.c_str());
87 const char * LLVMGetTargetName(LLVMTargetRef T) {
88 return unwrap(T)->getName();
91 const char * LLVMGetTargetDescription(LLVMTargetRef T) {
92 return unwrap(T)->getShortDescription();
95 LLVMBool LLVMTargetHasJIT(LLVMTargetRef T) {
96 return unwrap(T)->hasJIT();
99 LLVMBool LLVMTargetHasTargetMachine(LLVMTargetRef T) {
100 return unwrap(T)->hasTargetMachine();
103 LLVMBool LLVMTargetHasAsmBackend(LLVMTargetRef T) {
104 return unwrap(T)->hasMCAsmBackend();
107 LLVMTargetMachineRef LLVMCreateTargetMachine(LLVMTargetRef T,
108 const char* Triple, const char* CPU, const char* Features,
109 LLVMCodeGenOptLevel Level, LLVMRelocMode Reloc,
110 LLVMCodeModel CodeModel) {
113 case LLVMRelocStatic:
119 case LLVMRelocDynamicNoPic:
120 RM = Reloc::DynamicNoPIC;
127 CodeModel::Model CM = unwrap(CodeModel);
129 CodeGenOpt::Level OL;
131 case LLVMCodeGenLevelNone:
132 OL = CodeGenOpt::None;
134 case LLVMCodeGenLevelLess:
135 OL = CodeGenOpt::Less;
137 case LLVMCodeGenLevelAggressive:
138 OL = CodeGenOpt::Aggressive;
141 OL = CodeGenOpt::Default;
146 return wrap(unwrap(T)->createTargetMachine(Triple, CPU, Features, opt, RM,
151 void LLVMDisposeTargetMachine(LLVMTargetMachineRef T) {
155 LLVMTargetRef LLVMGetTargetMachineTarget(LLVMTargetMachineRef T) {
156 const Target* target = &(unwrap(T)->getTarget());
160 char* LLVMGetTargetMachineTriple(LLVMTargetMachineRef T) {
161 std::string StringRep = unwrap(T)->getTargetTriple();
162 return strdup(StringRep.c_str());
165 char* LLVMGetTargetMachineCPU(LLVMTargetMachineRef T) {
166 std::string StringRep = unwrap(T)->getTargetCPU();
167 return strdup(StringRep.c_str());
170 char* LLVMGetTargetMachineFeatureString(LLVMTargetMachineRef T) {
171 std::string StringRep = unwrap(T)->getTargetFeatureString();
172 return strdup(StringRep.c_str());
175 LLVMTargetDataRef LLVMGetTargetMachineData(LLVMTargetMachineRef T) {
176 return wrap(unwrap(T)->getSubtargetImpl()->getDataLayout());
179 void LLVMSetTargetMachineAsmVerbosity(LLVMTargetMachineRef T,
180 LLVMBool VerboseAsm) {
181 unwrap(T)->setAsmVerbosityDefault(VerboseAsm);
184 static LLVMBool LLVMTargetMachineEmit(LLVMTargetMachineRef T, LLVMModuleRef M,
185 formatted_raw_ostream &OS, LLVMCodeGenFileType codegen, char **ErrorMessage) {
186 TargetMachine* TM = unwrap(T);
187 Module* Mod = unwrap(M);
193 const DataLayout *td = TM->getSubtargetImpl()->getDataLayout();
196 error = "No DataLayout in TargetMachine";
197 *ErrorMessage = strdup(error.c_str());
200 Mod->setDataLayout(td);
201 pass.add(new DataLayoutPass(Mod));
203 TargetMachine::CodeGenFileType ft;
205 case LLVMAssemblyFile:
206 ft = TargetMachine::CGFT_AssemblyFile;
209 ft = TargetMachine::CGFT_ObjectFile;
212 if (TM->addPassesToEmitFile(pass, OS, ft)) {
213 error = "TargetMachine can't emit a file of this type";
214 *ErrorMessage = strdup(error.c_str());
224 LLVMBool LLVMTargetMachineEmitToFile(LLVMTargetMachineRef T, LLVMModuleRef M,
225 char* Filename, LLVMCodeGenFileType codegen, char** ErrorMessage) {
227 raw_fd_ostream dest(Filename, error, sys::fs::F_None);
228 if (!error.empty()) {
229 *ErrorMessage = strdup(error.c_str());
232 formatted_raw_ostream destf(dest);
233 bool Result = LLVMTargetMachineEmit(T, M, destf, codegen, ErrorMessage);
238 LLVMBool LLVMTargetMachineEmitToMemoryBuffer(LLVMTargetMachineRef T,
239 LLVMModuleRef M, LLVMCodeGenFileType codegen, char** ErrorMessage,
240 LLVMMemoryBufferRef *OutMemBuf) {
241 std::string CodeString;
242 raw_string_ostream OStream(CodeString);
243 formatted_raw_ostream Out(OStream);
244 bool Result = LLVMTargetMachineEmit(T, M, Out, codegen, ErrorMessage);
247 std::string &Data = OStream.str();
248 *OutMemBuf = LLVMCreateMemoryBufferWithMemoryRangeCopy(Data.c_str(),
253 char *LLVMGetDefaultTargetTriple(void) {
254 return strdup(sys::getDefaultTargetTriple().c_str());
257 void LLVMAddAnalysisPasses(LLVMTargetMachineRef T, LLVMPassManagerRef PM) {
258 unwrap(T)->addAnalysisPasses(*unwrap(PM));