Duncan Sands | 264d2e7 | 2012-04-11 10:25:24 +0000 | [diff] [blame] | 1 | //===-- 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 Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 14 | #include "llvm-c/TargetMachine.h" |
Duncan Sands | 264d2e7 | 2012-04-11 10:25:24 +0000 | [diff] [blame] | 15 | #include "llvm-c/Core.h" |
| 16 | #include "llvm-c/Target.h" |
Chandler Carruth | 93dcdc4 | 2015-01-31 11:17:59 +0000 | [diff] [blame] | 17 | #include "llvm/Analysis/TargetTransformInfo.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 18 | #include "llvm/IR/DataLayout.h" |
| 19 | #include "llvm/IR/Module.h" |
Chandler Carruth | 30d69c2 | 2015-02-13 10:01:29 +0000 | [diff] [blame] | 20 | #include "llvm/IR/LegacyPassManager.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 21 | #include "llvm/Support/CodeGen.h" |
Benjamin Kramer | d59664f | 2014-04-29 23:26:49 +0000 | [diff] [blame] | 22 | #include "llvm/Support/FileSystem.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 23 | #include "llvm/Support/FormattedStream.h" |
Chandler Carruth | 8a8cd2b | 2014-01-07 11:48:04 +0000 | [diff] [blame] | 24 | #include "llvm/Support/Host.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 25 | #include "llvm/Support/TargetRegistry.h" |
| 26 | #include "llvm/Support/raw_ostream.h" |
| 27 | #include "llvm/Target/TargetMachine.h" |
Eric Christopher | d913448 | 2014-08-04 21:25:23 +0000 | [diff] [blame] | 28 | #include "llvm/Target/TargetSubtargetInfo.h" |
Duncan Sands | 264d2e7 | 2012-04-11 10:25:24 +0000 | [diff] [blame] | 29 | #include <cassert> |
| 30 | #include <cstdlib> |
| 31 | #include <cstring> |
| 32 | |
| 33 | using namespace llvm; |
| 34 | |
Eric Christopher | 04d4e93 | 2013-04-22 22:47:22 +0000 | [diff] [blame] | 35 | inline TargetMachine *unwrap(LLVMTargetMachineRef P) { |
| 36 | return reinterpret_cast<TargetMachine*>(P); |
| 37 | } |
| 38 | inline Target *unwrap(LLVMTargetRef P) { |
| 39 | return reinterpret_cast<Target*>(P); |
| 40 | } |
| 41 | inline LLVMTargetMachineRef wrap(const TargetMachine *P) { |
| 42 | return |
| 43 | reinterpret_cast<LLVMTargetMachineRef>(const_cast<TargetMachine*>(P)); |
| 44 | } |
| 45 | inline LLVMTargetRef wrap(const Target * P) { |
| 46 | return reinterpret_cast<LLVMTargetRef>(const_cast<Target*>(P)); |
| 47 | } |
Duncan Sands | 264d2e7 | 2012-04-11 10:25:24 +0000 | [diff] [blame] | 48 | |
| 49 | LLVMTargetRef LLVMGetFirstTarget() { |
David Blaikie | 46c561c | 2015-05-11 22:20:48 +0000 | [diff] [blame] | 50 | if (TargetRegistry::targets().begin() == TargetRegistry::targets().end()) { |
Craig Topper | 062a2ba | 2014-04-25 05:30:21 +0000 | [diff] [blame] | 51 | return nullptr; |
Anders Waldenborg | a89c1e3 | 2013-10-17 10:25:24 +0000 | [diff] [blame] | 52 | } |
| 53 | |
David Blaikie | 46c561c | 2015-05-11 22:20:48 +0000 | [diff] [blame] | 54 | const Target *target = &*TargetRegistry::targets().begin(); |
Anders Waldenborg | a89c1e3 | 2013-10-17 10:25:24 +0000 | [diff] [blame] | 55 | return wrap(target); |
Duncan Sands | 264d2e7 | 2012-04-11 10:25:24 +0000 | [diff] [blame] | 56 | } |
| 57 | LLVMTargetRef LLVMGetNextTarget(LLVMTargetRef T) { |
| 58 | return wrap(unwrap(T)->getNext()); |
| 59 | } |
| 60 | |
Peter Zotov | b2c8b8a | 2013-11-15 02:51:01 +0000 | [diff] [blame] | 61 | LLVMTargetRef LLVMGetTargetFromName(const char *Name) { |
Peter Zotov | 5a6cfda | 2013-11-15 02:51:18 +0000 | [diff] [blame] | 62 | StringRef NameRef = Name; |
David Blaikie | 46c561c | 2015-05-11 22:20:48 +0000 | [diff] [blame] | 63 | auto I = std::find_if( |
| 64 | TargetRegistry::targets().begin(), TargetRegistry::targets().end(), |
| 65 | [&](const Target &T) { return T.getName() == NameRef; }); |
| 66 | return I != TargetRegistry::targets().end() ? wrap(&*I) : nullptr; |
Peter Zotov | 7b61b75 | 2013-11-06 10:25:18 +0000 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | LLVMBool LLVMGetTargetFromTriple(const char* TripleStr, LLVMTargetRef *T, |
| 70 | char **ErrorMessage) { |
| 71 | std::string Error; |
| 72 | |
| 73 | *T = wrap(TargetRegistry::lookupTarget(TripleStr, Error)); |
| 74 | |
| 75 | if (!*T) { |
| 76 | if (ErrorMessage) |
| 77 | *ErrorMessage = strdup(Error.c_str()); |
| 78 | |
| 79 | return 1; |
| 80 | } |
| 81 | |
| 82 | return 0; |
| 83 | } |
| 84 | |
Duncan Sands | 264d2e7 | 2012-04-11 10:25:24 +0000 | [diff] [blame] | 85 | const char * LLVMGetTargetName(LLVMTargetRef T) { |
| 86 | return unwrap(T)->getName(); |
| 87 | } |
| 88 | |
| 89 | const char * LLVMGetTargetDescription(LLVMTargetRef T) { |
| 90 | return unwrap(T)->getShortDescription(); |
| 91 | } |
| 92 | |
| 93 | LLVMBool LLVMTargetHasJIT(LLVMTargetRef T) { |
| 94 | return unwrap(T)->hasJIT(); |
| 95 | } |
| 96 | |
| 97 | LLVMBool LLVMTargetHasTargetMachine(LLVMTargetRef T) { |
| 98 | return unwrap(T)->hasTargetMachine(); |
| 99 | } |
| 100 | |
| 101 | LLVMBool LLVMTargetHasAsmBackend(LLVMTargetRef T) { |
| 102 | return unwrap(T)->hasMCAsmBackend(); |
| 103 | } |
| 104 | |
Peter Zotov | 0e38fc8 | 2013-11-15 02:51:12 +0000 | [diff] [blame] | 105 | LLVMTargetMachineRef LLVMCreateTargetMachine(LLVMTargetRef T, |
| 106 | const char* Triple, const char* CPU, const char* Features, |
| 107 | LLVMCodeGenOptLevel Level, LLVMRelocMode Reloc, |
| 108 | LLVMCodeModel CodeModel) { |
Duncan Sands | 264d2e7 | 2012-04-11 10:25:24 +0000 | [diff] [blame] | 109 | Reloc::Model RM; |
| 110 | switch (Reloc){ |
| 111 | case LLVMRelocStatic: |
| 112 | RM = Reloc::Static; |
| 113 | break; |
| 114 | case LLVMRelocPIC: |
| 115 | RM = Reloc::PIC_; |
| 116 | break; |
| 117 | case LLVMRelocDynamicNoPic: |
| 118 | RM = Reloc::DynamicNoPIC; |
| 119 | break; |
| 120 | default: |
| 121 | RM = Reloc::Default; |
| 122 | break; |
| 123 | } |
| 124 | |
Filip Pizlo | 85e0d27 | 2013-05-01 22:58:00 +0000 | [diff] [blame] | 125 | CodeModel::Model CM = unwrap(CodeModel); |
Duncan Sands | 264d2e7 | 2012-04-11 10:25:24 +0000 | [diff] [blame] | 126 | |
Filip Pizlo | 85e0d27 | 2013-05-01 22:58:00 +0000 | [diff] [blame] | 127 | CodeGenOpt::Level OL; |
Duncan Sands | 264d2e7 | 2012-04-11 10:25:24 +0000 | [diff] [blame] | 128 | switch (Level) { |
| 129 | case LLVMCodeGenLevelNone: |
| 130 | OL = CodeGenOpt::None; |
| 131 | break; |
| 132 | case LLVMCodeGenLevelLess: |
| 133 | OL = CodeGenOpt::Less; |
| 134 | break; |
| 135 | case LLVMCodeGenLevelAggressive: |
| 136 | OL = CodeGenOpt::Aggressive; |
| 137 | break; |
| 138 | default: |
| 139 | OL = CodeGenOpt::Default; |
| 140 | break; |
| 141 | } |
| 142 | |
| 143 | TargetOptions opt; |
| 144 | return wrap(unwrap(T)->createTargetMachine(Triple, CPU, Features, opt, RM, |
| 145 | CM, OL)); |
| 146 | } |
| 147 | |
| 148 | |
| 149 | void LLVMDisposeTargetMachine(LLVMTargetMachineRef T) { |
| 150 | delete unwrap(T); |
| 151 | } |
| 152 | |
| 153 | LLVMTargetRef LLVMGetTargetMachineTarget(LLVMTargetMachineRef T) { |
| 154 | const Target* target = &(unwrap(T)->getTarget()); |
| 155 | return wrap(target); |
| 156 | } |
| 157 | |
| 158 | char* LLVMGetTargetMachineTriple(LLVMTargetMachineRef T) { |
| 159 | std::string StringRep = unwrap(T)->getTargetTriple(); |
| 160 | return strdup(StringRep.c_str()); |
| 161 | } |
| 162 | |
| 163 | char* LLVMGetTargetMachineCPU(LLVMTargetMachineRef T) { |
| 164 | std::string StringRep = unwrap(T)->getTargetCPU(); |
| 165 | return strdup(StringRep.c_str()); |
| 166 | } |
| 167 | |
| 168 | char* LLVMGetTargetMachineFeatureString(LLVMTargetMachineRef T) { |
| 169 | std::string StringRep = unwrap(T)->getTargetFeatureString(); |
| 170 | return strdup(StringRep.c_str()); |
| 171 | } |
| 172 | |
| 173 | LLVMTargetDataRef LLVMGetTargetMachineData(LLVMTargetMachineRef T) { |
Eric Christopher | 8b77065 | 2015-01-26 19:03:15 +0000 | [diff] [blame] | 174 | return wrap(unwrap(T)->getDataLayout()); |
Duncan Sands | 264d2e7 | 2012-04-11 10:25:24 +0000 | [diff] [blame] | 175 | } |
| 176 | |
Peter Zotov | 7b61b75 | 2013-11-06 10:25:18 +0000 | [diff] [blame] | 177 | void LLVMSetTargetMachineAsmVerbosity(LLVMTargetMachineRef T, |
| 178 | LLVMBool VerboseAsm) { |
Rafael Espindola | 3105fd8 | 2015-02-12 21:16:34 +0000 | [diff] [blame] | 179 | unwrap(T)->Options.MCOptions.AsmVerbose = VerboseAsm; |
Peter Zotov | 7b61b75 | 2013-11-06 10:25:18 +0000 | [diff] [blame] | 180 | } |
| 181 | |
Tom Stellard | ec924c5 | 2013-04-16 23:12:56 +0000 | [diff] [blame] | 182 | static LLVMBool LLVMTargetMachineEmit(LLVMTargetMachineRef T, LLVMModuleRef M, |
Rafael Espindola | 5560a4c | 2015-04-14 22:14:34 +0000 | [diff] [blame] | 183 | raw_pwrite_stream &OS, |
Rafael Espindola | f546d0f | 2015-04-09 17:16:25 +0000 | [diff] [blame] | 184 | LLVMCodeGenFileType codegen, |
| 185 | char **ErrorMessage) { |
Duncan Sands | 264d2e7 | 2012-04-11 10:25:24 +0000 | [diff] [blame] | 186 | TargetMachine* TM = unwrap(T); |
| 187 | Module* Mod = unwrap(M); |
| 188 | |
Chandler Carruth | 30d69c2 | 2015-02-13 10:01:29 +0000 | [diff] [blame] | 189 | legacy::PassManager pass; |
Duncan Sands | 264d2e7 | 2012-04-11 10:25:24 +0000 | [diff] [blame] | 190 | |
| 191 | std::string error; |
| 192 | |
Eric Christopher | 8b77065 | 2015-01-26 19:03:15 +0000 | [diff] [blame] | 193 | const DataLayout *td = TM->getDataLayout(); |
Duncan Sands | 264d2e7 | 2012-04-11 10:25:24 +0000 | [diff] [blame] | 194 | |
| 195 | if (!td) { |
Micah Villmow | cdfe20b | 2012-10-08 16:38:25 +0000 | [diff] [blame] | 196 | error = "No DataLayout in TargetMachine"; |
Duncan Sands | 264d2e7 | 2012-04-11 10:25:24 +0000 | [diff] [blame] | 197 | *ErrorMessage = strdup(error.c_str()); |
| 198 | return true; |
| 199 | } |
Mehdi Amini | 46a4355 | 2015-03-04 18:43:29 +0000 | [diff] [blame] | 200 | Mod->setDataLayout(*td); |
Duncan Sands | 264d2e7 | 2012-04-11 10:25:24 +0000 | [diff] [blame] | 201 | |
| 202 | TargetMachine::CodeGenFileType ft; |
| 203 | switch (codegen) { |
| 204 | case LLVMAssemblyFile: |
| 205 | ft = TargetMachine::CGFT_AssemblyFile; |
| 206 | break; |
| 207 | default: |
| 208 | ft = TargetMachine::CGFT_ObjectFile; |
| 209 | break; |
| 210 | } |
Tom Stellard | ec924c5 | 2013-04-16 23:12:56 +0000 | [diff] [blame] | 211 | if (TM->addPassesToEmitFile(pass, OS, ft)) { |
Nick Lewycky | 7b287ee | 2013-03-10 22:01:44 +0000 | [diff] [blame] | 212 | error = "TargetMachine can't emit a file of this type"; |
Duncan Sands | 264d2e7 | 2012-04-11 10:25:24 +0000 | [diff] [blame] | 213 | *ErrorMessage = strdup(error.c_str()); |
| 214 | return true; |
| 215 | } |
| 216 | |
| 217 | pass.run(*Mod); |
| 218 | |
Tom Stellard | ec924c5 | 2013-04-16 23:12:56 +0000 | [diff] [blame] | 219 | OS.flush(); |
Duncan Sands | 264d2e7 | 2012-04-11 10:25:24 +0000 | [diff] [blame] | 220 | return false; |
| 221 | } |
Tom Stellard | ec924c5 | 2013-04-16 23:12:56 +0000 | [diff] [blame] | 222 | |
| 223 | LLVMBool LLVMTargetMachineEmitToFile(LLVMTargetMachineRef T, LLVMModuleRef M, |
| 224 | char* Filename, LLVMCodeGenFileType codegen, char** ErrorMessage) { |
Rafael Espindola | 3fd1e99 | 2014-08-25 18:16:47 +0000 | [diff] [blame] | 225 | std::error_code EC; |
| 226 | raw_fd_ostream dest(Filename, EC, sys::fs::F_None); |
| 227 | if (EC) { |
| 228 | *ErrorMessage = strdup(EC.message().c_str()); |
Tom Stellard | ec924c5 | 2013-04-16 23:12:56 +0000 | [diff] [blame] | 229 | return true; |
| 230 | } |
Rafael Espindola | 5682ce2 | 2015-04-09 21:06:08 +0000 | [diff] [blame] | 231 | bool Result = LLVMTargetMachineEmit(T, M, dest, codegen, ErrorMessage); |
Tom Stellard | ec924c5 | 2013-04-16 23:12:56 +0000 | [diff] [blame] | 232 | dest.flush(); |
| 233 | return Result; |
| 234 | } |
| 235 | |
| 236 | LLVMBool LLVMTargetMachineEmitToMemoryBuffer(LLVMTargetMachineRef T, |
| 237 | LLVMModuleRef M, LLVMCodeGenFileType codegen, char** ErrorMessage, |
| 238 | LLVMMemoryBufferRef *OutMemBuf) { |
Rafael Espindola | f546d0f | 2015-04-09 17:16:25 +0000 | [diff] [blame] | 239 | SmallString<0> CodeString; |
| 240 | raw_svector_ostream OStream(CodeString); |
Rafael Espindola | 5682ce2 | 2015-04-09 21:06:08 +0000 | [diff] [blame] | 241 | bool Result = LLVMTargetMachineEmit(T, M, OStream, codegen, ErrorMessage); |
Alp Toker | e69170a | 2014-06-26 22:52:05 +0000 | [diff] [blame] | 242 | OStream.flush(); |
Tom Stellard | ec924c5 | 2013-04-16 23:12:56 +0000 | [diff] [blame] | 243 | |
Rafael Espindola | f546d0f | 2015-04-09 17:16:25 +0000 | [diff] [blame] | 244 | StringRef Data = OStream.str(); |
| 245 | *OutMemBuf = |
| 246 | LLVMCreateMemoryBufferWithMemoryRangeCopy(Data.data(), Data.size(), ""); |
Tom Stellard | ec924c5 | 2013-04-16 23:12:56 +0000 | [diff] [blame] | 247 | return Result; |
| 248 | } |
Peter Zotov | 7b61b75 | 2013-11-06 10:25:18 +0000 | [diff] [blame] | 249 | |
| 250 | char *LLVMGetDefaultTargetTriple(void) { |
| 251 | return strdup(sys::getDefaultTargetTriple().c_str()); |
| 252 | } |
Juergen Ributzka | 5fe955c | 2014-01-23 19:23:28 +0000 | [diff] [blame] | 253 | |
| 254 | void LLVMAddAnalysisPasses(LLVMTargetMachineRef T, LLVMPassManagerRef PM) { |
Chandler Carruth | 5ec2b1d | 2015-02-01 12:26:09 +0000 | [diff] [blame] | 255 | unwrap(PM)->add( |
| 256 | createTargetTransformInfoWrapperPass(unwrap(T)->getTargetIRAnalysis())); |
Juergen Ributzka | 5fe955c | 2014-01-23 19:23:28 +0000 | [diff] [blame] | 257 | } |