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 static TargetMachine *unwrap(LLVMTargetMachineRef P) {
36 return reinterpret_cast<TargetMachine *>(P);
38 static Target *unwrap(LLVMTargetRef P) {
39 return reinterpret_cast<Target*>(P);
41 static LLVMTargetMachineRef wrap(const TargetMachine *P) {
42 return reinterpret_cast<LLVMTargetMachineRef>(const_cast<TargetMachine *>(P));
44 static LLVMTargetRef wrap(const Target * P) {
45 return reinterpret_cast<LLVMTargetRef>(const_cast<Target*>(P));
48 LLVMTargetRef LLVMGetFirstTarget() {
49 if (TargetRegistry::targets().begin() == TargetRegistry::targets().end()) {
53 const Target *target = &*TargetRegistry::targets().begin();
56 LLVMTargetRef LLVMGetNextTarget(LLVMTargetRef T) {
57 return wrap(unwrap(T)->getNext());
60 LLVMTargetRef LLVMGetTargetFromName(const char *Name) {
61 StringRef NameRef = Name;
62 auto I = std::find_if(
63 TargetRegistry::targets().begin(), TargetRegistry::targets().end(),
64 [&](const Target &T) { return T.getName() == NameRef; });
65 return I != TargetRegistry::targets().end() ? wrap(&*I) : nullptr;
68 LLVMBool LLVMGetTargetFromTriple(const char* TripleStr, LLVMTargetRef *T,
69 char **ErrorMessage) {
72 *T = wrap(TargetRegistry::lookupTarget(TripleStr, Error));
76 *ErrorMessage = strdup(Error.c_str());
84 const char * LLVMGetTargetName(LLVMTargetRef T) {
85 return unwrap(T)->getName();
88 const char * LLVMGetTargetDescription(LLVMTargetRef T) {
89 return unwrap(T)->getShortDescription();
92 LLVMBool LLVMTargetHasJIT(LLVMTargetRef T) {
93 return unwrap(T)->hasJIT();
96 LLVMBool LLVMTargetHasTargetMachine(LLVMTargetRef T) {
97 return unwrap(T)->hasTargetMachine();
100 LLVMBool LLVMTargetHasAsmBackend(LLVMTargetRef T) {
101 return unwrap(T)->hasMCAsmBackend();
104 LLVMTargetMachineRef LLVMCreateTargetMachine(LLVMTargetRef T,
105 const char* Triple, const char* CPU, const char* Features,
106 LLVMCodeGenOptLevel Level, LLVMRelocMode Reloc,
107 LLVMCodeModel CodeModel) {
110 case LLVMRelocStatic:
116 case LLVMRelocDynamicNoPic:
117 RM = Reloc::DynamicNoPIC;
124 CodeModel::Model CM = unwrap(CodeModel);
126 CodeGenOpt::Level OL;
128 case LLVMCodeGenLevelNone:
129 OL = CodeGenOpt::None;
131 case LLVMCodeGenLevelLess:
132 OL = CodeGenOpt::Less;
134 case LLVMCodeGenLevelAggressive:
135 OL = CodeGenOpt::Aggressive;
138 OL = CodeGenOpt::Default;
143 return wrap(unwrap(T)->createTargetMachine(Triple, CPU, Features, opt, RM,
147 void LLVMDisposeTargetMachine(LLVMTargetMachineRef T) { delete unwrap(T); }
149 LLVMTargetRef LLVMGetTargetMachineTarget(LLVMTargetMachineRef T) {
150 const Target* target = &(unwrap(T)->getTarget());
154 char* LLVMGetTargetMachineTriple(LLVMTargetMachineRef T) {
155 std::string StringRep = unwrap(T)->getTargetTriple().str();
156 return strdup(StringRep.c_str());
159 char* LLVMGetTargetMachineCPU(LLVMTargetMachineRef T) {
160 std::string StringRep = unwrap(T)->getTargetCPU();
161 return strdup(StringRep.c_str());
164 char* LLVMGetTargetMachineFeatureString(LLVMTargetMachineRef T) {
165 std::string StringRep = unwrap(T)->getTargetFeatureString();
166 return strdup(StringRep.c_str());
169 /** Deprecated: use LLVMGetDataLayout(LLVMModuleRef M) instead. */
170 LLVMTargetDataRef LLVMGetTargetMachineData(LLVMTargetMachineRef T) {
171 return wrap(&unwrap(T)->getDataLayout());
174 void LLVMSetTargetMachineAsmVerbosity(LLVMTargetMachineRef T,
175 LLVMBool VerboseAsm) {
176 unwrap(T)->Options.MCOptions.AsmVerbose = VerboseAsm;
179 static LLVMBool LLVMTargetMachineEmit(LLVMTargetMachineRef T, LLVMModuleRef M,
180 raw_pwrite_stream &OS,
181 LLVMCodeGenFileType codegen,
182 char **ErrorMessage) {
183 TargetMachine* TM = unwrap(T);
184 Module* Mod = unwrap(M);
186 legacy::PassManager pass;
190 Mod->setDataLayout(TM->createDataLayout());
192 TargetMachine::CodeGenFileType ft;
194 case LLVMAssemblyFile:
195 ft = TargetMachine::CGFT_AssemblyFile;
198 ft = TargetMachine::CGFT_ObjectFile;
201 if (TM->addPassesToEmitFile(pass, OS, ft)) {
202 error = "TargetMachine can't emit a file of this type";
203 *ErrorMessage = strdup(error.c_str());
213 LLVMBool LLVMTargetMachineEmitToFile(LLVMTargetMachineRef T, LLVMModuleRef M,
214 char* Filename, LLVMCodeGenFileType codegen, char** ErrorMessage) {
216 raw_fd_ostream dest(Filename, EC, sys::fs::F_None);
218 *ErrorMessage = strdup(EC.message().c_str());
221 bool Result = LLVMTargetMachineEmit(T, M, dest, codegen, ErrorMessage);
226 LLVMBool LLVMTargetMachineEmitToMemoryBuffer(LLVMTargetMachineRef T,
227 LLVMModuleRef M, LLVMCodeGenFileType codegen, char** ErrorMessage,
228 LLVMMemoryBufferRef *OutMemBuf) {
229 SmallString<0> CodeString;
230 raw_svector_ostream OStream(CodeString);
231 bool Result = LLVMTargetMachineEmit(T, M, OStream, codegen, ErrorMessage);
233 StringRef Data = OStream.str();
235 LLVMCreateMemoryBufferWithMemoryRangeCopy(Data.data(), Data.size(), "");
239 char *LLVMGetDefaultTargetTriple(void) {
240 return strdup(sys::getDefaultTargetTriple().c_str());
243 void LLVMAddAnalysisPasses(LLVMTargetMachineRef T, LLVMPassManagerRef PM) {
245 createTargetTransformInfoWrapperPass(unwrap(T)->getTargetIRAnalysis()));