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 14 #include "llvm-c/TargetMachine.h" 15 #include "llvm-c/Core.h" 16 #include "llvm-c/Target.h" 17 #include "llvm/Analysis/TargetTransformInfo.h" 18 #include "llvm/IR/DataLayout.h" 19 #include "llvm/IR/Module.h" 20 #include "llvm/IR/LegacyPassManager.h" 21 #include "llvm/Support/CodeGenCWrappers.h" 22 #include "llvm/Support/FileSystem.h" 23 #include "llvm/Support/FormattedStream.h" 24 #include "llvm/Support/Host.h" 25 #include "llvm/Support/TargetRegistry.h" 26 #include "llvm/Support/raw_ostream.h" 27 #include "llvm/Target/TargetMachine.h" 28 #include "llvm/Target/TargetSubtargetInfo.h" 29 #include <cassert> 30 #include <cstdlib> 31 #include <cstring> 32 33 using namespace llvm; 34 35 static TargetMachine *unwrap(LLVMTargetMachineRef P) { 36 return reinterpret_cast<TargetMachine *>(P); 37 } 38 static Target *unwrap(LLVMTargetRef P) { 39 return reinterpret_cast<Target*>(P); 40 } 41 static LLVMTargetMachineRef wrap(const TargetMachine *P) { 42 return reinterpret_cast<LLVMTargetMachineRef>(const_cast<TargetMachine *>(P)); 43 } 44 static LLVMTargetRef wrap(const Target * P) { 45 return reinterpret_cast<LLVMTargetRef>(const_cast<Target*>(P)); 46 } 47 48 LLVMTargetRef LLVMGetFirstTarget() { 49 if (TargetRegistry::targets().begin() == TargetRegistry::targets().end()) { 50 return nullptr; 51 } 52 53 const Target *target = &*TargetRegistry::targets().begin(); 54 return wrap(target); 55 } 56 LLVMTargetRef LLVMGetNextTarget(LLVMTargetRef T) { 57 return wrap(unwrap(T)->getNext()); 58 } 59 60 LLVMTargetRef LLVMGetTargetFromName(const char *Name) { 61 StringRef NameRef = Name; 62 auto I = find_if(TargetRegistry::targets(), 63 [&](const Target &T) { return T.getName() == NameRef; }); 64 return I != TargetRegistry::targets().end() ? wrap(&*I) : nullptr; 65 } 66 67 LLVMBool LLVMGetTargetFromTriple(const char* TripleStr, LLVMTargetRef *T, 68 char **ErrorMessage) { 69 std::string Error; 70 71 *T = wrap(TargetRegistry::lookupTarget(TripleStr, Error)); 72 73 if (!*T) { 74 if (ErrorMessage) 75 *ErrorMessage = strdup(Error.c_str()); 76 77 return 1; 78 } 79 80 return 0; 81 } 82 83 const char * LLVMGetTargetName(LLVMTargetRef T) { 84 return unwrap(T)->getName(); 85 } 86 87 const char * LLVMGetTargetDescription(LLVMTargetRef T) { 88 return unwrap(T)->getShortDescription(); 89 } 90 91 LLVMBool LLVMTargetHasJIT(LLVMTargetRef T) { 92 return unwrap(T)->hasJIT(); 93 } 94 95 LLVMBool LLVMTargetHasTargetMachine(LLVMTargetRef T) { 96 return unwrap(T)->hasTargetMachine(); 97 } 98 99 LLVMBool LLVMTargetHasAsmBackend(LLVMTargetRef T) { 100 return unwrap(T)->hasMCAsmBackend(); 101 } 102 103 LLVMTargetMachineRef LLVMCreateTargetMachine(LLVMTargetRef T, 104 const char* Triple, const char* CPU, const char* Features, 105 LLVMCodeGenOptLevel Level, LLVMRelocMode Reloc, 106 LLVMCodeModel CodeModel) { 107 Optional<Reloc::Model> RM; 108 switch (Reloc){ 109 case LLVMRelocStatic: 110 RM = Reloc::Static; 111 break; 112 case LLVMRelocPIC: 113 RM = Reloc::PIC_; 114 break; 115 case LLVMRelocDynamicNoPic: 116 RM = Reloc::DynamicNoPIC; 117 break; 118 default: 119 break; 120 } 121 122 CodeModel::Model CM = unwrap(CodeModel); 123 124 CodeGenOpt::Level OL; 125 switch (Level) { 126 case LLVMCodeGenLevelNone: 127 OL = CodeGenOpt::None; 128 break; 129 case LLVMCodeGenLevelLess: 130 OL = CodeGenOpt::Less; 131 break; 132 case LLVMCodeGenLevelAggressive: 133 OL = CodeGenOpt::Aggressive; 134 break; 135 default: 136 OL = CodeGenOpt::Default; 137 break; 138 } 139 140 TargetOptions opt; 141 return wrap(unwrap(T)->createTargetMachine(Triple, CPU, Features, opt, RM, 142 CM, OL)); 143 } 144 145 void LLVMDisposeTargetMachine(LLVMTargetMachineRef T) { delete unwrap(T); } 146 147 LLVMTargetRef LLVMGetTargetMachineTarget(LLVMTargetMachineRef T) { 148 const Target* target = &(unwrap(T)->getTarget()); 149 return wrap(target); 150 } 151 152 char* LLVMGetTargetMachineTriple(LLVMTargetMachineRef T) { 153 std::string StringRep = unwrap(T)->getTargetTriple().str(); 154 return strdup(StringRep.c_str()); 155 } 156 157 char* LLVMGetTargetMachineCPU(LLVMTargetMachineRef T) { 158 std::string StringRep = unwrap(T)->getTargetCPU(); 159 return strdup(StringRep.c_str()); 160 } 161 162 char* LLVMGetTargetMachineFeatureString(LLVMTargetMachineRef T) { 163 std::string StringRep = unwrap(T)->getTargetFeatureString(); 164 return strdup(StringRep.c_str()); 165 } 166 167 void LLVMSetTargetMachineAsmVerbosity(LLVMTargetMachineRef T, 168 LLVMBool VerboseAsm) { 169 unwrap(T)->Options.MCOptions.AsmVerbose = VerboseAsm; 170 } 171 172 LLVMTargetDataRef LLVMCreateTargetDataLayout(LLVMTargetMachineRef T) { 173 return wrap(new DataLayout(unwrap(T)->createDataLayout())); 174 } 175 176 static LLVMBool LLVMTargetMachineEmit(LLVMTargetMachineRef T, LLVMModuleRef M, 177 raw_pwrite_stream &OS, 178 LLVMCodeGenFileType codegen, 179 char **ErrorMessage) { 180 TargetMachine* TM = unwrap(T); 181 Module* Mod = unwrap(M); 182 183 legacy::PassManager pass; 184 185 std::string error; 186 187 Mod->setDataLayout(TM->createDataLayout()); 188 189 TargetMachine::CodeGenFileType ft; 190 switch (codegen) { 191 case LLVMAssemblyFile: 192 ft = TargetMachine::CGFT_AssemblyFile; 193 break; 194 default: 195 ft = TargetMachine::CGFT_ObjectFile; 196 break; 197 } 198 if (TM->addPassesToEmitFile(pass, OS, ft)) { 199 error = "TargetMachine can't emit a file of this type"; 200 *ErrorMessage = strdup(error.c_str()); 201 return true; 202 } 203 204 pass.run(*Mod); 205 206 OS.flush(); 207 return false; 208 } 209 210 LLVMBool LLVMTargetMachineEmitToFile(LLVMTargetMachineRef T, LLVMModuleRef M, 211 char* Filename, LLVMCodeGenFileType codegen, char** ErrorMessage) { 212 std::error_code EC; 213 raw_fd_ostream dest(Filename, EC, sys::fs::F_None); 214 if (EC) { 215 *ErrorMessage = strdup(EC.message().c_str()); 216 return true; 217 } 218 bool Result = LLVMTargetMachineEmit(T, M, dest, codegen, ErrorMessage); 219 dest.flush(); 220 return Result; 221 } 222 223 LLVMBool LLVMTargetMachineEmitToMemoryBuffer(LLVMTargetMachineRef T, 224 LLVMModuleRef M, LLVMCodeGenFileType codegen, char** ErrorMessage, 225 LLVMMemoryBufferRef *OutMemBuf) { 226 SmallString<0> CodeString; 227 raw_svector_ostream OStream(CodeString); 228 bool Result = LLVMTargetMachineEmit(T, M, OStream, codegen, ErrorMessage); 229 230 StringRef Data = OStream.str(); 231 *OutMemBuf = 232 LLVMCreateMemoryBufferWithMemoryRangeCopy(Data.data(), Data.size(), ""); 233 return Result; 234 } 235 236 char *LLVMGetDefaultTargetTriple(void) { 237 return strdup(sys::getDefaultTargetTriple().c_str()); 238 } 239 240 void LLVMAddAnalysisPasses(LLVMTargetMachineRef T, LLVMPassManagerRef PM) { 241 unwrap(PM)->add( 242 createTargetTransformInfoWrapperPass(unwrap(T)->getTargetIRAnalysis())); 243 } 244