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;
109 CodeModel::Model CM = unwrap(CodeModel);
111 CodeGenOpt::Level OL;
113 case LLVMCodeGenLevelNone:
114 OL = CodeGenOpt::None;
116 case LLVMCodeGenLevelLess:
117 OL = CodeGenOpt::Less;
119 case LLVMCodeGenLevelAggressive:
120 OL = CodeGenOpt::Aggressive;
123 OL = CodeGenOpt::Default;
128 return wrap(unwrap(T)->createTargetMachine(Triple, CPU, Features, opt, RM,
133 void LLVMDisposeTargetMachine(LLVMTargetMachineRef T) {
137 LLVMTargetRef LLVMGetTargetMachineTarget(LLVMTargetMachineRef T) {
138 const Target* target = &(unwrap(T)->getTarget());
142 char* LLVMGetTargetMachineTriple(LLVMTargetMachineRef T) {
143 std::string StringRep = unwrap(T)->getTargetTriple();
144 return strdup(StringRep.c_str());
147 char* LLVMGetTargetMachineCPU(LLVMTargetMachineRef T) {
148 std::string StringRep = unwrap(T)->getTargetCPU();
149 return strdup(StringRep.c_str());
152 char* LLVMGetTargetMachineFeatureString(LLVMTargetMachineRef T) {
153 std::string StringRep = unwrap(T)->getTargetFeatureString();
154 return strdup(StringRep.c_str());
157 LLVMTargetDataRef LLVMGetTargetMachineData(LLVMTargetMachineRef T) {
158 return wrap(unwrap(T)->getDataLayout());
161 static LLVMBool LLVMTargetMachineEmit(LLVMTargetMachineRef T, LLVMModuleRef M,
162 formatted_raw_ostream &OS, LLVMCodeGenFileType codegen, char **ErrorMessage) {
163 TargetMachine* TM = unwrap(T);
164 Module* Mod = unwrap(M);
170 const DataLayout* td = TM->getDataLayout();
173 error = "No DataLayout in TargetMachine";
174 *ErrorMessage = strdup(error.c_str());
177 pass.add(new DataLayout(*td));
179 TargetMachine::CodeGenFileType ft;
181 case LLVMAssemblyFile:
182 ft = TargetMachine::CGFT_AssemblyFile;
185 ft = TargetMachine::CGFT_ObjectFile;
188 if (TM->addPassesToEmitFile(pass, OS, ft)) {
189 error = "TargetMachine can't emit a file of this type";
190 *ErrorMessage = strdup(error.c_str());
200 LLVMBool LLVMTargetMachineEmitToFile(LLVMTargetMachineRef T, LLVMModuleRef M,
201 char* Filename, LLVMCodeGenFileType codegen, char** ErrorMessage) {
203 raw_fd_ostream dest(Filename, error, raw_fd_ostream::F_Binary);
204 formatted_raw_ostream destf(dest);
205 if (!error.empty()) {
206 *ErrorMessage = strdup(error.c_str());
209 bool Result = LLVMTargetMachineEmit(T, M, destf, codegen, ErrorMessage);
214 LLVMBool LLVMTargetMachineEmitToMemoryBuffer(LLVMTargetMachineRef T,
215 LLVMModuleRef M, LLVMCodeGenFileType codegen, char** ErrorMessage,
216 LLVMMemoryBufferRef *OutMemBuf) {
217 std::string CodeString;
218 raw_string_ostream OStream(CodeString);
219 formatted_raw_ostream Out(OStream);
220 bool Result = LLVMTargetMachineEmit(T, M, Out, codegen, ErrorMessage);
223 std::string &Data = OStream.str();
224 *OutMemBuf = LLVMCreateMemoryBufferWithMemoryRangeCopy(Data.c_str(),