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/Wrap.h"
21 #include "llvm/Support/CodeGen.h"
22 #include "llvm/Support/FormattedStream.h"
23 #include "llvm/Support/TargetRegistry.h"
24 #include "llvm/Support/raw_ostream.h"
25 #include "llvm/Target/TargetMachine.h"
32 inline DataLayout *unwrap(LLVMTargetDataRef P) {
33 return reinterpret_cast<DataLayout*>(P);
36 inline LLVMTargetDataRef wrap(const DataLayout *P) {
37 return reinterpret_cast<LLVMTargetDataRef>(const_cast<DataLayout*>(P));
40 inline TargetLibraryInfo *unwrap(LLVMTargetLibraryInfoRef P) {
41 return reinterpret_cast<TargetLibraryInfo*>(P);
44 inline LLVMTargetLibraryInfoRef wrap(const TargetLibraryInfo *P) {
45 TargetLibraryInfo *X = const_cast<TargetLibraryInfo*>(P);
46 return reinterpret_cast<LLVMTargetLibraryInfoRef>(X);
49 inline TargetMachine *unwrap(LLVMTargetMachineRef P) {
50 return reinterpret_cast<TargetMachine*>(P);
52 inline Target *unwrap(LLVMTargetRef P) {
53 return reinterpret_cast<Target*>(P);
55 inline LLVMTargetMachineRef wrap(const TargetMachine *P) {
57 reinterpret_cast<LLVMTargetMachineRef>(const_cast<TargetMachine*>(P));
59 inline LLVMTargetRef wrap(const Target * P) {
60 return reinterpret_cast<LLVMTargetRef>(const_cast<Target*>(P));
63 LLVMTargetRef LLVMGetFirstTarget() {
64 const Target* target = &*TargetRegistry::begin();
67 LLVMTargetRef LLVMGetNextTarget(LLVMTargetRef T) {
68 return wrap(unwrap(T)->getNext());
71 const char * LLVMGetTargetName(LLVMTargetRef T) {
72 return unwrap(T)->getName();
75 const char * LLVMGetTargetDescription(LLVMTargetRef T) {
76 return unwrap(T)->getShortDescription();
79 LLVMBool LLVMTargetHasJIT(LLVMTargetRef T) {
80 return unwrap(T)->hasJIT();
83 LLVMBool LLVMTargetHasTargetMachine(LLVMTargetRef T) {
84 return unwrap(T)->hasTargetMachine();
87 LLVMBool LLVMTargetHasAsmBackend(LLVMTargetRef T) {
88 return unwrap(T)->hasMCAsmBackend();
91 LLVMTargetMachineRef LLVMCreateTargetMachine(LLVMTargetRef T, char* Triple,
92 char* CPU, char* Features, LLVMCodeGenOptLevel Level, LLVMRelocMode Reloc,
93 LLVMCodeModel CodeModel) {
102 case LLVMRelocDynamicNoPic:
103 RM = Reloc::DynamicNoPIC;
112 case LLVMCodeModelJITDefault:
113 CM = CodeModel::JITDefault;
115 case LLVMCodeModelSmall:
116 CM = CodeModel::Small;
118 case LLVMCodeModelKernel:
119 CM = CodeModel::Kernel;
121 case LLVMCodeModelMedium:
122 CM = CodeModel::Medium;
124 case LLVMCodeModelLarge:
125 CM = CodeModel::Large;
128 CM = CodeModel::Default;
131 CodeGenOpt::Level OL;
134 case LLVMCodeGenLevelNone:
135 OL = CodeGenOpt::None;
137 case LLVMCodeGenLevelLess:
138 OL = CodeGenOpt::Less;
140 case LLVMCodeGenLevelAggressive:
141 OL = CodeGenOpt::Aggressive;
144 OL = CodeGenOpt::Default;
149 return wrap(unwrap(T)->createTargetMachine(Triple, CPU, Features, opt, RM,
154 void LLVMDisposeTargetMachine(LLVMTargetMachineRef T) {
158 LLVMTargetRef LLVMGetTargetMachineTarget(LLVMTargetMachineRef T) {
159 const Target* target = &(unwrap(T)->getTarget());
163 char* LLVMGetTargetMachineTriple(LLVMTargetMachineRef T) {
164 std::string StringRep = unwrap(T)->getTargetTriple();
165 return strdup(StringRep.c_str());
168 char* LLVMGetTargetMachineCPU(LLVMTargetMachineRef T) {
169 std::string StringRep = unwrap(T)->getTargetCPU();
170 return strdup(StringRep.c_str());
173 char* LLVMGetTargetMachineFeatureString(LLVMTargetMachineRef T) {
174 std::string StringRep = unwrap(T)->getTargetFeatureString();
175 return strdup(StringRep.c_str());
178 LLVMTargetDataRef LLVMGetTargetMachineData(LLVMTargetMachineRef T) {
179 return wrap(unwrap(T)->getDataLayout());
182 static LLVMBool LLVMTargetMachineEmit(LLVMTargetMachineRef T, LLVMModuleRef M,
183 formatted_raw_ostream &OS, LLVMCodeGenFileType codegen, char **ErrorMessage) {
184 TargetMachine* TM = unwrap(T);
185 Module* Mod = unwrap(M);
191 const DataLayout* td = TM->getDataLayout();
194 error = "No DataLayout in TargetMachine";
195 *ErrorMessage = strdup(error.c_str());
198 pass.add(new DataLayout(*td));
200 TargetMachine::CodeGenFileType ft;
202 case LLVMAssemblyFile:
203 ft = TargetMachine::CGFT_AssemblyFile;
206 ft = TargetMachine::CGFT_ObjectFile;
209 if (TM->addPassesToEmitFile(pass, OS, ft)) {
210 error = "TargetMachine can't emit a file of this type";
211 *ErrorMessage = strdup(error.c_str());
221 LLVMBool LLVMTargetMachineEmitToFile(LLVMTargetMachineRef T, LLVMModuleRef M,
222 char* Filename, LLVMCodeGenFileType codegen, char** ErrorMessage) {
224 raw_fd_ostream dest(Filename, error, raw_fd_ostream::F_Binary);
225 formatted_raw_ostream destf(dest);
226 if (!error.empty()) {
227 *ErrorMessage = strdup(error.c_str());
230 bool Result = LLVMTargetMachineEmit(T, M, destf, codegen, ErrorMessage);
235 LLVMBool LLVMTargetMachineEmitToMemoryBuffer(LLVMTargetMachineRef T,
236 LLVMModuleRef M, LLVMCodeGenFileType codegen, char** ErrorMessage,
237 LLVMMemoryBufferRef *OutMemBuf) {
238 std::string CodeString;
239 raw_string_ostream OStream(CodeString);
240 formatted_raw_ostream Out(OStream);
241 bool Result = LLVMTargetMachineEmit(T, M, Out, codegen, ErrorMessage);
244 std::string &Data = OStream.str();
245 *OutMemBuf = LLVMCreateMemoryBufferWithMemoryRangeCopy(Data.c_str(),