blob: 9fccfcd9e22517c265b6f7e791d99223ed4ecb2b [file] [log] [blame]
Duncan Sands264d2e72012-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 Carruthed0881b2012-12-03 16:50:05 +000014#include "llvm-c/TargetMachine.h"
Duncan Sands264d2e72012-04-11 10:25:24 +000015#include "llvm-c/Core.h"
16#include "llvm-c/Target.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000017#include "llvm/IR/DataLayout.h"
18#include "llvm/IR/Module.h"
Duncan Sands264d2e72012-04-11 10:25:24 +000019#include "llvm/PassManager.h"
Chandler Carruthed0881b2012-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 Sands264d2e72012-04-11 10:25:24 +000025#include <cassert>
26#include <cstdlib>
27#include <cstring>
28
29using namespace llvm;
30
Eric Christopher04d4e932013-04-22 22:47:22 +000031inline DataLayout *unwrap(LLVMTargetDataRef P) {
32 return reinterpret_cast<DataLayout*>(P);
33}
Duncan Sands264d2e72012-04-11 10:25:24 +000034
Eric Christopher04d4e932013-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 Sands264d2e72012-04-11 10:25:24 +000061
62LLVMTargetRef LLVMGetFirstTarget() {
Anders Waldenborga89c1e32013-10-17 10:25:24 +000063 if(TargetRegistry::begin() == TargetRegistry::end()) {
64 return NULL;
65 }
66
67 const Target* target = &*TargetRegistry::begin();
68 return wrap(target);
Duncan Sands264d2e72012-04-11 10:25:24 +000069}
70LLVMTargetRef LLVMGetNextTarget(LLVMTargetRef T) {
71 return wrap(unwrap(T)->getNext());
72}
73
74const char * LLVMGetTargetName(LLVMTargetRef T) {
75 return unwrap(T)->getName();
76}
77
78const char * LLVMGetTargetDescription(LLVMTargetRef T) {
79 return unwrap(T)->getShortDescription();
80}
81
82LLVMBool LLVMTargetHasJIT(LLVMTargetRef T) {
83 return unwrap(T)->hasJIT();
84}
85
86LLVMBool LLVMTargetHasTargetMachine(LLVMTargetRef T) {
87 return unwrap(T)->hasTargetMachine();
88}
89
90LLVMBool LLVMTargetHasAsmBackend(LLVMTargetRef T) {
91 return unwrap(T)->hasMCAsmBackend();
92}
93
94LLVMTargetMachineRef LLVMCreateTargetMachine(LLVMTargetRef T, char* Triple,
95 char* CPU, char* Features, LLVMCodeGenOptLevel Level, LLVMRelocMode Reloc,
96 LLVMCodeModel CodeModel) {
97 Reloc::Model RM;
98 switch (Reloc){
99 case LLVMRelocStatic:
100 RM = Reloc::Static;
101 break;
102 case LLVMRelocPIC:
103 RM = Reloc::PIC_;
104 break;
105 case LLVMRelocDynamicNoPic:
106 RM = Reloc::DynamicNoPIC;
107 break;
108 default:
109 RM = Reloc::Default;
110 break;
111 }
112
Filip Pizlo85e0d272013-05-01 22:58:00 +0000113 CodeModel::Model CM = unwrap(CodeModel);
Duncan Sands264d2e72012-04-11 10:25:24 +0000114
Filip Pizlo85e0d272013-05-01 22:58:00 +0000115 CodeGenOpt::Level OL;
Duncan Sands264d2e72012-04-11 10:25:24 +0000116 switch (Level) {
117 case LLVMCodeGenLevelNone:
118 OL = CodeGenOpt::None;
119 break;
120 case LLVMCodeGenLevelLess:
121 OL = CodeGenOpt::Less;
122 break;
123 case LLVMCodeGenLevelAggressive:
124 OL = CodeGenOpt::Aggressive;
125 break;
126 default:
127 OL = CodeGenOpt::Default;
128 break;
129 }
130
131 TargetOptions opt;
132 return wrap(unwrap(T)->createTargetMachine(Triple, CPU, Features, opt, RM,
133 CM, OL));
134}
135
136
137void LLVMDisposeTargetMachine(LLVMTargetMachineRef T) {
138 delete unwrap(T);
139}
140
141LLVMTargetRef LLVMGetTargetMachineTarget(LLVMTargetMachineRef T) {
142 const Target* target = &(unwrap(T)->getTarget());
143 return wrap(target);
144}
145
146char* LLVMGetTargetMachineTriple(LLVMTargetMachineRef T) {
147 std::string StringRep = unwrap(T)->getTargetTriple();
148 return strdup(StringRep.c_str());
149}
150
151char* LLVMGetTargetMachineCPU(LLVMTargetMachineRef T) {
152 std::string StringRep = unwrap(T)->getTargetCPU();
153 return strdup(StringRep.c_str());
154}
155
156char* LLVMGetTargetMachineFeatureString(LLVMTargetMachineRef T) {
157 std::string StringRep = unwrap(T)->getTargetFeatureString();
158 return strdup(StringRep.c_str());
159}
160
161LLVMTargetDataRef LLVMGetTargetMachineData(LLVMTargetMachineRef T) {
Micah Villmowcdfe20b2012-10-08 16:38:25 +0000162 return wrap(unwrap(T)->getDataLayout());
Duncan Sands264d2e72012-04-11 10:25:24 +0000163}
164
Tom Stellardec924c52013-04-16 23:12:56 +0000165static LLVMBool LLVMTargetMachineEmit(LLVMTargetMachineRef T, LLVMModuleRef M,
166 formatted_raw_ostream &OS, LLVMCodeGenFileType codegen, char **ErrorMessage) {
Duncan Sands264d2e72012-04-11 10:25:24 +0000167 TargetMachine* TM = unwrap(T);
168 Module* Mod = unwrap(M);
169
170 PassManager pass;
171
172 std::string error;
173
Micah Villmowcdfe20b2012-10-08 16:38:25 +0000174 const DataLayout* td = TM->getDataLayout();
Duncan Sands264d2e72012-04-11 10:25:24 +0000175
176 if (!td) {
Micah Villmowcdfe20b2012-10-08 16:38:25 +0000177 error = "No DataLayout in TargetMachine";
Duncan Sands264d2e72012-04-11 10:25:24 +0000178 *ErrorMessage = strdup(error.c_str());
179 return true;
180 }
Micah Villmowcdfe20b2012-10-08 16:38:25 +0000181 pass.add(new DataLayout(*td));
Duncan Sands264d2e72012-04-11 10:25:24 +0000182
183 TargetMachine::CodeGenFileType ft;
184 switch (codegen) {
185 case LLVMAssemblyFile:
186 ft = TargetMachine::CGFT_AssemblyFile;
187 break;
188 default:
189 ft = TargetMachine::CGFT_ObjectFile;
190 break;
191 }
Tom Stellardec924c52013-04-16 23:12:56 +0000192 if (TM->addPassesToEmitFile(pass, OS, ft)) {
Nick Lewycky7b287ee2013-03-10 22:01:44 +0000193 error = "TargetMachine can't emit a file of this type";
Duncan Sands264d2e72012-04-11 10:25:24 +0000194 *ErrorMessage = strdup(error.c_str());
195 return true;
196 }
197
198 pass.run(*Mod);
199
Tom Stellardec924c52013-04-16 23:12:56 +0000200 OS.flush();
Duncan Sands264d2e72012-04-11 10:25:24 +0000201 return false;
202}
Tom Stellardec924c52013-04-16 23:12:56 +0000203
204LLVMBool LLVMTargetMachineEmitToFile(LLVMTargetMachineRef T, LLVMModuleRef M,
205 char* Filename, LLVMCodeGenFileType codegen, char** ErrorMessage) {
206 std::string error;
Rafael Espindola6d354812013-07-16 19:44:17 +0000207 raw_fd_ostream dest(Filename, error, sys::fs::F_Binary);
Tom Stellardec924c52013-04-16 23:12:56 +0000208 formatted_raw_ostream destf(dest);
209 if (!error.empty()) {
210 *ErrorMessage = strdup(error.c_str());
211 return true;
212 }
213 bool Result = LLVMTargetMachineEmit(T, M, destf, codegen, ErrorMessage);
214 dest.flush();
215 return Result;
216}
217
218LLVMBool LLVMTargetMachineEmitToMemoryBuffer(LLVMTargetMachineRef T,
219 LLVMModuleRef M, LLVMCodeGenFileType codegen, char** ErrorMessage,
220 LLVMMemoryBufferRef *OutMemBuf) {
221 std::string CodeString;
222 raw_string_ostream OStream(CodeString);
223 formatted_raw_ostream Out(OStream);
224 bool Result = LLVMTargetMachineEmit(T, M, Out, codegen, ErrorMessage);
225 OStream.flush();
226
227 std::string &Data = OStream.str();
228 *OutMemBuf = LLVMCreateMemoryBufferWithMemoryRangeCopy(Data.c_str(),
229 Data.length(), "");
230 return Result;
231}