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