blob: 19e78b83a74c4b003f0e03f763dea7e822b8d046 [file] [log] [blame]
Duncan Sandsd6b7b8f2012-04-11 10:25:24 +00001//===-- TargetMachine.cpp -------------------------------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the LLVM-C part of TargetMachine.h
11//
12//===----------------------------------------------------------------------===//
13
Chandler Carruthd04a8d42012-12-03 16:50:05 +000014#include "llvm-c/TargetMachine.h"
Duncan Sandsd6b7b8f2012-04-11 10:25:24 +000015#include "llvm-c/Core.h"
16#include "llvm-c/Target.h"
Chandler Carruth0b8c9a82013-01-02 11:36:10 +000017#include "llvm/IR/DataLayout.h"
18#include "llvm/IR/Module.h"
Duncan Sandsd6b7b8f2012-04-11 10:25:24 +000019#include "llvm/PassManager.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000020#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"
Duncan Sandsd6b7b8f2012-04-11 10:25:24 +000025#include <cassert>
26#include <cstdlib>
27#include <cstring>
28
29using namespace llvm;
30
Eric Christopher3e397312013-04-22 22:47:22 +000031inline DataLayout *unwrap(LLVMTargetDataRef P) {
32 return reinterpret_cast<DataLayout*>(P);
33}
Duncan Sandsd6b7b8f2012-04-11 10:25:24 +000034
Eric Christopher3e397312013-04-22 22:47:22 +000035inline LLVMTargetDataRef wrap(const DataLayout *P) {
36 return reinterpret_cast<LLVMTargetDataRef>(const_cast<DataLayout*>(P));
37}
38
39inline TargetLibraryInfo *unwrap(LLVMTargetLibraryInfoRef P) {
40 return reinterpret_cast<TargetLibraryInfo*>(P);
41}
42
43inline LLVMTargetLibraryInfoRef wrap(const TargetLibraryInfo *P) {
44 TargetLibraryInfo *X = const_cast<TargetLibraryInfo*>(P);
45 return reinterpret_cast<LLVMTargetLibraryInfoRef>(X);
46}
47
48inline TargetMachine *unwrap(LLVMTargetMachineRef P) {
49 return reinterpret_cast<TargetMachine*>(P);
50}
51inline Target *unwrap(LLVMTargetRef P) {
52 return reinterpret_cast<Target*>(P);
53}
54inline LLVMTargetMachineRef wrap(const TargetMachine *P) {
55 return
56 reinterpret_cast<LLVMTargetMachineRef>(const_cast<TargetMachine*>(P));
57}
58inline LLVMTargetRef wrap(const Target * P) {
59 return reinterpret_cast<LLVMTargetRef>(const_cast<Target*>(P));
60}
Duncan Sandsd6b7b8f2012-04-11 10:25:24 +000061
62LLVMTargetRef LLVMGetFirstTarget() {
63 const Target* target = &*TargetRegistry::begin();
64 return wrap(target);
65}
66LLVMTargetRef LLVMGetNextTarget(LLVMTargetRef T) {
67 return wrap(unwrap(T)->getNext());
68}
69
70const char * LLVMGetTargetName(LLVMTargetRef T) {
71 return unwrap(T)->getName();
72}
73
74const char * LLVMGetTargetDescription(LLVMTargetRef T) {
75 return unwrap(T)->getShortDescription();
76}
77
78LLVMBool LLVMTargetHasJIT(LLVMTargetRef T) {
79 return unwrap(T)->hasJIT();
80}
81
82LLVMBool LLVMTargetHasTargetMachine(LLVMTargetRef T) {
83 return unwrap(T)->hasTargetMachine();
84}
85
86LLVMBool LLVMTargetHasAsmBackend(LLVMTargetRef T) {
87 return unwrap(T)->hasMCAsmBackend();
88}
89
90LLVMTargetMachineRef LLVMCreateTargetMachine(LLVMTargetRef T, char* Triple,
91 char* CPU, char* Features, LLVMCodeGenOptLevel Level, LLVMRelocMode Reloc,
92 LLVMCodeModel CodeModel) {
93 Reloc::Model RM;
94 switch (Reloc){
95 case LLVMRelocStatic:
96 RM = Reloc::Static;
97 break;
98 case LLVMRelocPIC:
99 RM = Reloc::PIC_;
100 break;
101 case LLVMRelocDynamicNoPic:
102 RM = Reloc::DynamicNoPIC;
103 break;
104 default:
105 RM = Reloc::Default;
106 break;
107 }
108
109 CodeModel::Model CM;
110 switch (CodeModel) {
111 case LLVMCodeModelJITDefault:
112 CM = CodeModel::JITDefault;
113 break;
114 case LLVMCodeModelSmall:
115 CM = CodeModel::Small;
116 break;
117 case LLVMCodeModelKernel:
118 CM = CodeModel::Kernel;
119 break;
120 case LLVMCodeModelMedium:
121 CM = CodeModel::Medium;
122 break;
123 case LLVMCodeModelLarge:
124 CM = CodeModel::Large;
125 break;
126 default:
127 CM = CodeModel::Default;
128 break;
129 }
130 CodeGenOpt::Level OL;
131
132 switch (Level) {
133 case LLVMCodeGenLevelNone:
134 OL = CodeGenOpt::None;
135 break;
136 case LLVMCodeGenLevelLess:
137 OL = CodeGenOpt::Less;
138 break;
139 case LLVMCodeGenLevelAggressive:
140 OL = CodeGenOpt::Aggressive;
141 break;
142 default:
143 OL = CodeGenOpt::Default;
144 break;
145 }
146
147 TargetOptions opt;
148 return wrap(unwrap(T)->createTargetMachine(Triple, CPU, Features, opt, RM,
149 CM, OL));
150}
151
152
153void LLVMDisposeTargetMachine(LLVMTargetMachineRef T) {
154 delete unwrap(T);
155}
156
157LLVMTargetRef LLVMGetTargetMachineTarget(LLVMTargetMachineRef T) {
158 const Target* target = &(unwrap(T)->getTarget());
159 return wrap(target);
160}
161
162char* LLVMGetTargetMachineTriple(LLVMTargetMachineRef T) {
163 std::string StringRep = unwrap(T)->getTargetTriple();
164 return strdup(StringRep.c_str());
165}
166
167char* LLVMGetTargetMachineCPU(LLVMTargetMachineRef T) {
168 std::string StringRep = unwrap(T)->getTargetCPU();
169 return strdup(StringRep.c_str());
170}
171
172char* LLVMGetTargetMachineFeatureString(LLVMTargetMachineRef T) {
173 std::string StringRep = unwrap(T)->getTargetFeatureString();
174 return strdup(StringRep.c_str());
175}
176
177LLVMTargetDataRef LLVMGetTargetMachineData(LLVMTargetMachineRef T) {
Micah Villmow3574eca2012-10-08 16:38:25 +0000178 return wrap(unwrap(T)->getDataLayout());
Duncan Sandsd6b7b8f2012-04-11 10:25:24 +0000179}
180
Tom Stellardad74f332013-04-16 23:12:56 +0000181static LLVMBool LLVMTargetMachineEmit(LLVMTargetMachineRef T, LLVMModuleRef M,
182 formatted_raw_ostream &OS, LLVMCodeGenFileType codegen, char **ErrorMessage) {
Duncan Sandsd6b7b8f2012-04-11 10:25:24 +0000183 TargetMachine* TM = unwrap(T);
184 Module* Mod = unwrap(M);
185
186 PassManager pass;
187
188 std::string error;
189
Micah Villmow3574eca2012-10-08 16:38:25 +0000190 const DataLayout* td = TM->getDataLayout();
Duncan Sandsd6b7b8f2012-04-11 10:25:24 +0000191
192 if (!td) {
Micah Villmow3574eca2012-10-08 16:38:25 +0000193 error = "No DataLayout in TargetMachine";
Duncan Sandsd6b7b8f2012-04-11 10:25:24 +0000194 *ErrorMessage = strdup(error.c_str());
195 return true;
196 }
Micah Villmow3574eca2012-10-08 16:38:25 +0000197 pass.add(new DataLayout(*td));
Duncan Sandsd6b7b8f2012-04-11 10:25:24 +0000198
199 TargetMachine::CodeGenFileType ft;
200 switch (codegen) {
201 case LLVMAssemblyFile:
202 ft = TargetMachine::CGFT_AssemblyFile;
203 break;
204 default:
205 ft = TargetMachine::CGFT_ObjectFile;
206 break;
207 }
Tom Stellardad74f332013-04-16 23:12:56 +0000208 if (TM->addPassesToEmitFile(pass, OS, ft)) {
Nick Lewyckyf3a64c02013-03-10 22:01:44 +0000209 error = "TargetMachine can't emit a file of this type";
Duncan Sandsd6b7b8f2012-04-11 10:25:24 +0000210 *ErrorMessage = strdup(error.c_str());
211 return true;
212 }
213
214 pass.run(*Mod);
215
Tom Stellardad74f332013-04-16 23:12:56 +0000216 OS.flush();
Duncan Sandsd6b7b8f2012-04-11 10:25:24 +0000217 return false;
218}
Tom Stellardad74f332013-04-16 23:12:56 +0000219
220LLVMBool LLVMTargetMachineEmitToFile(LLVMTargetMachineRef T, LLVMModuleRef M,
221 char* Filename, LLVMCodeGenFileType codegen, char** ErrorMessage) {
222 std::string error;
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());
227 return true;
228 }
229 bool Result = LLVMTargetMachineEmit(T, M, destf, codegen, ErrorMessage);
230 dest.flush();
231 return Result;
232}
233
234LLVMBool 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);
241 OStream.flush();
242
243 std::string &Data = OStream.str();
244 *OutMemBuf = LLVMCreateMemoryBufferWithMemoryRangeCopy(Data.c_str(),
245 Data.length(), "");
246 return Result;
247}