Duncan Sands | d6b7b8f | 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 | d04a8d4 | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 14 | #include "llvm-c/TargetMachine.h" |
Duncan Sands | d6b7b8f | 2012-04-11 10:25:24 +0000 | [diff] [blame] | 15 | #include "llvm-c/Core.h" |
| 16 | #include "llvm-c/Target.h" |
Chandler Carruth | 0b8c9a8 | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 17 | #include "llvm/IR/DataLayout.h" |
| 18 | #include "llvm/IR/Module.h" |
Duncan Sands | d6b7b8f | 2012-04-11 10:25:24 +0000 | [diff] [blame] | 19 | #include "llvm/PassManager.h" |
Eric Christopher | 3e39731 | 2013-04-22 22:47:22 +0000 | [diff] [blame^] | 20 | #include "llvm/Wrap.h" |
Chandler Carruth | d04a8d4 | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 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" |
Duncan Sands | d6b7b8f | 2012-04-11 10:25:24 +0000 | [diff] [blame] | 26 | #include <cassert> |
| 27 | #include <cstdlib> |
| 28 | #include <cstring> |
| 29 | |
| 30 | using namespace llvm; |
| 31 | |
Eric Christopher | 3e39731 | 2013-04-22 22:47:22 +0000 | [diff] [blame^] | 32 | inline DataLayout *unwrap(LLVMTargetDataRef P) { |
| 33 | return reinterpret_cast<DataLayout*>(P); |
| 34 | } |
Duncan Sands | d6b7b8f | 2012-04-11 10:25:24 +0000 | [diff] [blame] | 35 | |
Eric Christopher | 3e39731 | 2013-04-22 22:47:22 +0000 | [diff] [blame^] | 36 | inline LLVMTargetDataRef wrap(const DataLayout *P) { |
| 37 | return reinterpret_cast<LLVMTargetDataRef>(const_cast<DataLayout*>(P)); |
| 38 | } |
| 39 | |
| 40 | inline TargetLibraryInfo *unwrap(LLVMTargetLibraryInfoRef P) { |
| 41 | return reinterpret_cast<TargetLibraryInfo*>(P); |
| 42 | } |
| 43 | |
| 44 | inline LLVMTargetLibraryInfoRef wrap(const TargetLibraryInfo *P) { |
| 45 | TargetLibraryInfo *X = const_cast<TargetLibraryInfo*>(P); |
| 46 | return reinterpret_cast<LLVMTargetLibraryInfoRef>(X); |
| 47 | } |
| 48 | |
| 49 | inline TargetMachine *unwrap(LLVMTargetMachineRef P) { |
| 50 | return reinterpret_cast<TargetMachine*>(P); |
| 51 | } |
| 52 | inline Target *unwrap(LLVMTargetRef P) { |
| 53 | return reinterpret_cast<Target*>(P); |
| 54 | } |
| 55 | inline LLVMTargetMachineRef wrap(const TargetMachine *P) { |
| 56 | return |
| 57 | reinterpret_cast<LLVMTargetMachineRef>(const_cast<TargetMachine*>(P)); |
| 58 | } |
| 59 | inline LLVMTargetRef wrap(const Target * P) { |
| 60 | return reinterpret_cast<LLVMTargetRef>(const_cast<Target*>(P)); |
| 61 | } |
Duncan Sands | d6b7b8f | 2012-04-11 10:25:24 +0000 | [diff] [blame] | 62 | |
| 63 | LLVMTargetRef LLVMGetFirstTarget() { |
| 64 | const Target* target = &*TargetRegistry::begin(); |
| 65 | return wrap(target); |
| 66 | } |
| 67 | LLVMTargetRef LLVMGetNextTarget(LLVMTargetRef T) { |
| 68 | return wrap(unwrap(T)->getNext()); |
| 69 | } |
| 70 | |
| 71 | const char * LLVMGetTargetName(LLVMTargetRef T) { |
| 72 | return unwrap(T)->getName(); |
| 73 | } |
| 74 | |
| 75 | const char * LLVMGetTargetDescription(LLVMTargetRef T) { |
| 76 | return unwrap(T)->getShortDescription(); |
| 77 | } |
| 78 | |
| 79 | LLVMBool LLVMTargetHasJIT(LLVMTargetRef T) { |
| 80 | return unwrap(T)->hasJIT(); |
| 81 | } |
| 82 | |
| 83 | LLVMBool LLVMTargetHasTargetMachine(LLVMTargetRef T) { |
| 84 | return unwrap(T)->hasTargetMachine(); |
| 85 | } |
| 86 | |
| 87 | LLVMBool LLVMTargetHasAsmBackend(LLVMTargetRef T) { |
| 88 | return unwrap(T)->hasMCAsmBackend(); |
| 89 | } |
| 90 | |
| 91 | LLVMTargetMachineRef LLVMCreateTargetMachine(LLVMTargetRef T, char* Triple, |
| 92 | char* CPU, char* Features, LLVMCodeGenOptLevel Level, LLVMRelocMode Reloc, |
| 93 | LLVMCodeModel CodeModel) { |
| 94 | Reloc::Model RM; |
| 95 | switch (Reloc){ |
| 96 | case LLVMRelocStatic: |
| 97 | RM = Reloc::Static; |
| 98 | break; |
| 99 | case LLVMRelocPIC: |
| 100 | RM = Reloc::PIC_; |
| 101 | break; |
| 102 | case LLVMRelocDynamicNoPic: |
| 103 | RM = Reloc::DynamicNoPIC; |
| 104 | break; |
| 105 | default: |
| 106 | RM = Reloc::Default; |
| 107 | break; |
| 108 | } |
| 109 | |
| 110 | CodeModel::Model CM; |
| 111 | switch (CodeModel) { |
| 112 | case LLVMCodeModelJITDefault: |
| 113 | CM = CodeModel::JITDefault; |
| 114 | break; |
| 115 | case LLVMCodeModelSmall: |
| 116 | CM = CodeModel::Small; |
| 117 | break; |
| 118 | case LLVMCodeModelKernel: |
| 119 | CM = CodeModel::Kernel; |
| 120 | break; |
| 121 | case LLVMCodeModelMedium: |
| 122 | CM = CodeModel::Medium; |
| 123 | break; |
| 124 | case LLVMCodeModelLarge: |
| 125 | CM = CodeModel::Large; |
| 126 | break; |
| 127 | default: |
| 128 | CM = CodeModel::Default; |
| 129 | break; |
| 130 | } |
| 131 | CodeGenOpt::Level OL; |
| 132 | |
| 133 | switch (Level) { |
| 134 | case LLVMCodeGenLevelNone: |
| 135 | OL = CodeGenOpt::None; |
| 136 | break; |
| 137 | case LLVMCodeGenLevelLess: |
| 138 | OL = CodeGenOpt::Less; |
| 139 | break; |
| 140 | case LLVMCodeGenLevelAggressive: |
| 141 | OL = CodeGenOpt::Aggressive; |
| 142 | break; |
| 143 | default: |
| 144 | OL = CodeGenOpt::Default; |
| 145 | break; |
| 146 | } |
| 147 | |
| 148 | TargetOptions opt; |
| 149 | return wrap(unwrap(T)->createTargetMachine(Triple, CPU, Features, opt, RM, |
| 150 | CM, OL)); |
| 151 | } |
| 152 | |
| 153 | |
| 154 | void LLVMDisposeTargetMachine(LLVMTargetMachineRef T) { |
| 155 | delete unwrap(T); |
| 156 | } |
| 157 | |
| 158 | LLVMTargetRef LLVMGetTargetMachineTarget(LLVMTargetMachineRef T) { |
| 159 | const Target* target = &(unwrap(T)->getTarget()); |
| 160 | return wrap(target); |
| 161 | } |
| 162 | |
| 163 | char* LLVMGetTargetMachineTriple(LLVMTargetMachineRef T) { |
| 164 | std::string StringRep = unwrap(T)->getTargetTriple(); |
| 165 | return strdup(StringRep.c_str()); |
| 166 | } |
| 167 | |
| 168 | char* LLVMGetTargetMachineCPU(LLVMTargetMachineRef T) { |
| 169 | std::string StringRep = unwrap(T)->getTargetCPU(); |
| 170 | return strdup(StringRep.c_str()); |
| 171 | } |
| 172 | |
| 173 | char* LLVMGetTargetMachineFeatureString(LLVMTargetMachineRef T) { |
| 174 | std::string StringRep = unwrap(T)->getTargetFeatureString(); |
| 175 | return strdup(StringRep.c_str()); |
| 176 | } |
| 177 | |
| 178 | LLVMTargetDataRef LLVMGetTargetMachineData(LLVMTargetMachineRef T) { |
Micah Villmow | 3574eca | 2012-10-08 16:38:25 +0000 | [diff] [blame] | 179 | return wrap(unwrap(T)->getDataLayout()); |
Duncan Sands | d6b7b8f | 2012-04-11 10:25:24 +0000 | [diff] [blame] | 180 | } |
| 181 | |
Tom Stellard | ad74f33 | 2013-04-16 23:12:56 +0000 | [diff] [blame] | 182 | static LLVMBool LLVMTargetMachineEmit(LLVMTargetMachineRef T, LLVMModuleRef M, |
| 183 | formatted_raw_ostream &OS, LLVMCodeGenFileType codegen, char **ErrorMessage) { |
Duncan Sands | d6b7b8f | 2012-04-11 10:25:24 +0000 | [diff] [blame] | 184 | TargetMachine* TM = unwrap(T); |
| 185 | Module* Mod = unwrap(M); |
| 186 | |
| 187 | PassManager pass; |
| 188 | |
| 189 | std::string error; |
| 190 | |
Micah Villmow | 3574eca | 2012-10-08 16:38:25 +0000 | [diff] [blame] | 191 | const DataLayout* td = TM->getDataLayout(); |
Duncan Sands | d6b7b8f | 2012-04-11 10:25:24 +0000 | [diff] [blame] | 192 | |
| 193 | if (!td) { |
Micah Villmow | 3574eca | 2012-10-08 16:38:25 +0000 | [diff] [blame] | 194 | error = "No DataLayout in TargetMachine"; |
Duncan Sands | d6b7b8f | 2012-04-11 10:25:24 +0000 | [diff] [blame] | 195 | *ErrorMessage = strdup(error.c_str()); |
| 196 | return true; |
| 197 | } |
Micah Villmow | 3574eca | 2012-10-08 16:38:25 +0000 | [diff] [blame] | 198 | pass.add(new DataLayout(*td)); |
Duncan Sands | d6b7b8f | 2012-04-11 10:25:24 +0000 | [diff] [blame] | 199 | |
| 200 | TargetMachine::CodeGenFileType ft; |
| 201 | switch (codegen) { |
| 202 | case LLVMAssemblyFile: |
| 203 | ft = TargetMachine::CGFT_AssemblyFile; |
| 204 | break; |
| 205 | default: |
| 206 | ft = TargetMachine::CGFT_ObjectFile; |
| 207 | break; |
| 208 | } |
Tom Stellard | ad74f33 | 2013-04-16 23:12:56 +0000 | [diff] [blame] | 209 | if (TM->addPassesToEmitFile(pass, OS, ft)) { |
Nick Lewycky | f3a64c0 | 2013-03-10 22:01:44 +0000 | [diff] [blame] | 210 | error = "TargetMachine can't emit a file of this type"; |
Duncan Sands | d6b7b8f | 2012-04-11 10:25:24 +0000 | [diff] [blame] | 211 | *ErrorMessage = strdup(error.c_str()); |
| 212 | return true; |
| 213 | } |
| 214 | |
| 215 | pass.run(*Mod); |
| 216 | |
Tom Stellard | ad74f33 | 2013-04-16 23:12:56 +0000 | [diff] [blame] | 217 | OS.flush(); |
Duncan Sands | d6b7b8f | 2012-04-11 10:25:24 +0000 | [diff] [blame] | 218 | return false; |
| 219 | } |
Tom Stellard | ad74f33 | 2013-04-16 23:12:56 +0000 | [diff] [blame] | 220 | |
| 221 | LLVMBool LLVMTargetMachineEmitToFile(LLVMTargetMachineRef T, LLVMModuleRef M, |
| 222 | char* Filename, LLVMCodeGenFileType codegen, char** ErrorMessage) { |
| 223 | std::string error; |
| 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()); |
| 228 | return true; |
| 229 | } |
| 230 | bool Result = LLVMTargetMachineEmit(T, M, destf, codegen, ErrorMessage); |
| 231 | dest.flush(); |
| 232 | return Result; |
| 233 | } |
| 234 | |
| 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); |
| 242 | OStream.flush(); |
| 243 | |
| 244 | std::string &Data = OStream.str(); |
| 245 | *OutMemBuf = LLVMCreateMemoryBufferWithMemoryRangeCopy(Data.c_str(), |
| 246 | Data.length(), ""); |
| 247 | return Result; |
| 248 | } |