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/Analysis/TargetTransformInfo.h"
18 #include "llvm/IR/DataLayout.h"
19 #include "llvm/IR/Module.h"
20 #include "llvm/IR/LegacyPassManager.h"
21 #include "llvm/Support/CodeGen.h"
22 #include "llvm/Support/FileSystem.h"
23 #include "llvm/Support/FormattedStream.h"
24 #include "llvm/Support/Host.h"
25 #include "llvm/Support/TargetRegistry.h"
26 #include "llvm/Support/raw_ostream.h"
27 #include "llvm/Target/TargetMachine.h"
28 #include "llvm/Target/TargetSubtargetInfo.h"
35 inline TargetMachine *unwrap(LLVMTargetMachineRef P) {
36 return reinterpret_cast<TargetMachine*>(P);
38 inline Target *unwrap(LLVMTargetRef P) {
39 return reinterpret_cast<Target*>(P);
41 inline LLVMTargetMachineRef wrap(const TargetMachine *P) {
43 reinterpret_cast<LLVMTargetMachineRef>(const_cast<TargetMachine*>(P));
45 inline LLVMTargetRef wrap(const Target * P) {
46 return reinterpret_cast<LLVMTargetRef>(const_cast<Target*>(P));
49 LLVMTargetRef LLVMGetFirstTarget() {
50 if(TargetRegistry::begin() == TargetRegistry::end()) {
54 const Target* target = &*TargetRegistry::begin();
57 LLVMTargetRef LLVMGetNextTarget(LLVMTargetRef T) {
58 return wrap(unwrap(T)->getNext());
61 LLVMTargetRef LLVMGetTargetFromName(const char *Name) {
62 StringRef NameRef = Name;
63 for (TargetRegistry::iterator IT = TargetRegistry::begin(),
64 IE = TargetRegistry::end(); IT != IE; ++IT) {
65 if (IT->getName() == NameRef)
72 LLVMBool LLVMGetTargetFromTriple(const char* TripleStr, LLVMTargetRef *T,
73 char **ErrorMessage) {
76 *T = wrap(TargetRegistry::lookupTarget(TripleStr, Error));
80 *ErrorMessage = strdup(Error.c_str());
88 const char * LLVMGetTargetName(LLVMTargetRef T) {
89 return unwrap(T)->getName();
92 const char * LLVMGetTargetDescription(LLVMTargetRef T) {
93 return unwrap(T)->getShortDescription();
96 LLVMBool LLVMTargetHasJIT(LLVMTargetRef T) {
97 return unwrap(T)->hasJIT();
100 LLVMBool LLVMTargetHasTargetMachine(LLVMTargetRef T) {
101 return unwrap(T)->hasTargetMachine();
104 LLVMBool LLVMTargetHasAsmBackend(LLVMTargetRef T) {
105 return unwrap(T)->hasMCAsmBackend();
108 LLVMTargetMachineRef LLVMCreateTargetMachine(LLVMTargetRef T,
109 const char* Triple, const char* CPU, const char* Features,
110 LLVMCodeGenOptLevel Level, LLVMRelocMode Reloc,
111 LLVMCodeModel CodeModel) {
114 case LLVMRelocStatic:
120 case LLVMRelocDynamicNoPic:
121 RM = Reloc::DynamicNoPIC;
128 CodeModel::Model CM = unwrap(CodeModel);
130 CodeGenOpt::Level OL;
132 case LLVMCodeGenLevelNone:
133 OL = CodeGenOpt::None;
135 case LLVMCodeGenLevelLess:
136 OL = CodeGenOpt::Less;
138 case LLVMCodeGenLevelAggressive:
139 OL = CodeGenOpt::Aggressive;
142 OL = CodeGenOpt::Default;
147 return wrap(unwrap(T)->createTargetMachine(Triple, CPU, Features, opt, RM,
152 void LLVMDisposeTargetMachine(LLVMTargetMachineRef T) {
156 LLVMTargetRef LLVMGetTargetMachineTarget(LLVMTargetMachineRef T) {
157 const Target* target = &(unwrap(T)->getTarget());
161 char* LLVMGetTargetMachineTriple(LLVMTargetMachineRef T) {
162 std::string StringRep = unwrap(T)->getTargetTriple();
163 return strdup(StringRep.c_str());
166 char* LLVMGetTargetMachineCPU(LLVMTargetMachineRef T) {
167 std::string StringRep = unwrap(T)->getTargetCPU();
168 return strdup(StringRep.c_str());
171 char* LLVMGetTargetMachineFeatureString(LLVMTargetMachineRef T) {
172 std::string StringRep = unwrap(T)->getTargetFeatureString();
173 return strdup(StringRep.c_str());
176 LLVMTargetDataRef LLVMGetTargetMachineData(LLVMTargetMachineRef T) {
177 return wrap(unwrap(T)->getDataLayout());
180 void LLVMSetTargetMachineAsmVerbosity(LLVMTargetMachineRef T,
181 LLVMBool VerboseAsm) {
182 unwrap(T)->Options.MCOptions.AsmVerbose = VerboseAsm;
185 static LLVMBool LLVMTargetMachineEmit(LLVMTargetMachineRef T, LLVMModuleRef M,
186 formatted_raw_ostream &OS, LLVMCodeGenFileType codegen, char **ErrorMessage) {
187 TargetMachine* TM = unwrap(T);
188 Module* Mod = unwrap(M);
190 legacy::PassManager pass;
194 const DataLayout *td = TM->getDataLayout();
197 error = "No DataLayout in TargetMachine";
198 *ErrorMessage = strdup(error.c_str());
201 Mod->setDataLayout(*td);
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, EC, sys::fs::F_None);
229 *ErrorMessage = strdup(EC.message().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) {
259 createTargetTransformInfoWrapperPass(unwrap(T)->getTargetIRAnalysis()));