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/Target/TargetMachine.h"
31 inline DataLayout *unwrap(LLVMTargetDataRef P) {
32 return reinterpret_cast<DataLayout*>(P);
35 inline LLVMTargetDataRef wrap(const DataLayout *P) {
36 return reinterpret_cast<LLVMTargetDataRef>(const_cast<DataLayout*>(P));
39 inline TargetLibraryInfo *unwrap(LLVMTargetLibraryInfoRef P) {
40 return reinterpret_cast<TargetLibraryInfo*>(P);
43 inline LLVMTargetLibraryInfoRef wrap(const TargetLibraryInfo *P) {
44 TargetLibraryInfo *X = const_cast<TargetLibraryInfo*>(P);
45 return reinterpret_cast<LLVMTargetLibraryInfoRef>(X);
48 inline TargetMachine *unwrap(LLVMTargetMachineRef P) {
49 return reinterpret_cast<TargetMachine*>(P);
51 inline Target *unwrap(LLVMTargetRef P) {
52 return reinterpret_cast<Target*>(P);
54 inline LLVMTargetMachineRef wrap(const TargetMachine *P) {
56 reinterpret_cast<LLVMTargetMachineRef>(const_cast<TargetMachine*>(P));
58 inline LLVMTargetRef wrap(const Target * P) {
59 return reinterpret_cast<LLVMTargetRef>(const_cast<Target*>(P));
62 LLVMTargetRef LLVMGetFirstTarget() {
63 const Target* target = &*TargetRegistry::begin();
66 LLVMTargetRef LLVMGetNextTarget(LLVMTargetRef T) {
67 return wrap(unwrap(T)->getNext());
70 const char * LLVMGetTargetName(LLVMTargetRef T) {
71 return unwrap(T)->getName();
74 const char * LLVMGetTargetDescription(LLVMTargetRef T) {
75 return unwrap(T)->getShortDescription();
78 LLVMBool LLVMTargetHasJIT(LLVMTargetRef T) {
79 return unwrap(T)->hasJIT();
82 LLVMBool LLVMTargetHasTargetMachine(LLVMTargetRef T) {
83 return unwrap(T)->hasTargetMachine();
86 LLVMBool LLVMTargetHasAsmBackend(LLVMTargetRef T) {
87 return unwrap(T)->hasMCAsmBackend();
90 LLVMTargetMachineRef LLVMCreateTargetMachine(LLVMTargetRef T, char* Triple,
91 char* CPU, char* Features, LLVMCodeGenOptLevel Level, LLVMRelocMode Reloc,
92 LLVMCodeModel CodeModel) {
101 case LLVMRelocDynamicNoPic:
102 RM = Reloc::DynamicNoPIC;
111 case LLVMCodeModelJITDefault:
112 CM = CodeModel::JITDefault;
114 case LLVMCodeModelSmall:
115 CM = CodeModel::Small;
117 case LLVMCodeModelKernel:
118 CM = CodeModel::Kernel;
120 case LLVMCodeModelMedium:
121 CM = CodeModel::Medium;
123 case LLVMCodeModelLarge:
124 CM = CodeModel::Large;
127 CM = CodeModel::Default;
130 CodeGenOpt::Level OL;
133 case LLVMCodeGenLevelNone:
134 OL = CodeGenOpt::None;
136 case LLVMCodeGenLevelLess:
137 OL = CodeGenOpt::Less;
139 case LLVMCodeGenLevelAggressive:
140 OL = CodeGenOpt::Aggressive;
143 OL = CodeGenOpt::Default;
148 return wrap(unwrap(T)->createTargetMachine(Triple, CPU, Features, opt, RM,
153 void LLVMDisposeTargetMachine(LLVMTargetMachineRef T) {
157 LLVMTargetRef LLVMGetTargetMachineTarget(LLVMTargetMachineRef T) {
158 const Target* target = &(unwrap(T)->getTarget());
162 char* LLVMGetTargetMachineTriple(LLVMTargetMachineRef T) {
163 std::string StringRep = unwrap(T)->getTargetTriple();
164 return strdup(StringRep.c_str());
167 char* LLVMGetTargetMachineCPU(LLVMTargetMachineRef T) {
168 std::string StringRep = unwrap(T)->getTargetCPU();
169 return strdup(StringRep.c_str());
172 char* LLVMGetTargetMachineFeatureString(LLVMTargetMachineRef T) {
173 std::string StringRep = unwrap(T)->getTargetFeatureString();
174 return strdup(StringRep.c_str());
177 LLVMTargetDataRef LLVMGetTargetMachineData(LLVMTargetMachineRef T) {
178 return wrap(unwrap(T)->getDataLayout());
181 static LLVMBool LLVMTargetMachineEmit(LLVMTargetMachineRef T, LLVMModuleRef M,
182 formatted_raw_ostream &OS, LLVMCodeGenFileType codegen, char **ErrorMessage) {
183 TargetMachine* TM = unwrap(T);
184 Module* Mod = unwrap(M);
190 const DataLayout* td = TM->getDataLayout();
193 error = "No DataLayout in TargetMachine";
194 *ErrorMessage = strdup(error.c_str());
197 pass.add(new DataLayout(*td));
199 TargetMachine::CodeGenFileType ft;
201 case LLVMAssemblyFile:
202 ft = TargetMachine::CGFT_AssemblyFile;
205 ft = TargetMachine::CGFT_ObjectFile;
208 if (TM->addPassesToEmitFile(pass, OS, ft)) {
209 error = "TargetMachine can't emit a file of this type";
210 *ErrorMessage = strdup(error.c_str());
220 LLVMBool LLVMTargetMachineEmitToFile(LLVMTargetMachineRef T, LLVMModuleRef M,
221 char* Filename, LLVMCodeGenFileType codegen, char** ErrorMessage) {
223 raw_fd_ostream dest(Filename, error, raw_fd_ostream::F_Binary);
224 formatted_raw_ostream destf(dest);
225 if (!error.empty()) {
226 *ErrorMessage = strdup(error.c_str());
229 bool Result = LLVMTargetMachineEmit(T, M, destf, codegen, ErrorMessage);
234 LLVMBool LLVMTargetMachineEmitToMemoryBuffer(LLVMTargetMachineRef T,
235 LLVMModuleRef M, LLVMCodeGenFileType codegen, char** ErrorMessage,
236 LLVMMemoryBufferRef *OutMemBuf) {
237 std::string CodeString;
238 raw_string_ostream OStream(CodeString);
239 formatted_raw_ostream Out(OStream);
240 bool Result = LLVMTargetMachineEmit(T, M, Out, codegen, ErrorMessage);
243 std::string &Data = OStream.str();
244 *OutMemBuf = LLVMCreateMemoryBufferWithMemoryRangeCopy(Data.c_str(),