LLVM API Documentation

TargetMachineC.cpp
Go to the documentation of this file.
00001 //===-- TargetMachine.cpp -------------------------------------------------===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // This file implements the LLVM-C part of TargetMachine.h
00011 //
00012 //===----------------------------------------------------------------------===//
00013 
00014 #include "llvm-c/TargetMachine.h"
00015 #include "llvm-c/Core.h"
00016 #include "llvm-c/Target.h"
00017 #include "llvm/IR/DataLayout.h"
00018 #include "llvm/IR/Module.h"
00019 #include "llvm/PassManager.h"
00020 #include "llvm/Support/CodeGen.h"
00021 #include "llvm/Support/FileSystem.h"
00022 #include "llvm/Support/FormattedStream.h"
00023 #include "llvm/Support/Host.h"
00024 #include "llvm/Support/TargetRegistry.h"
00025 #include "llvm/Support/raw_ostream.h"
00026 #include "llvm/Target/TargetMachine.h"
00027 #include "llvm/Target/TargetSubtargetInfo.h"
00028 #include <cassert>
00029 #include <cstdlib>
00030 #include <cstring>
00031 
00032 using namespace llvm;
00033 
00034 inline TargetMachine *unwrap(LLVMTargetMachineRef P) {
00035   return reinterpret_cast<TargetMachine*>(P);
00036 }
00037 inline Target *unwrap(LLVMTargetRef P) {
00038   return reinterpret_cast<Target*>(P);
00039 }
00040 inline LLVMTargetMachineRef wrap(const TargetMachine *P) {
00041   return
00042     reinterpret_cast<LLVMTargetMachineRef>(const_cast<TargetMachine*>(P));
00043 }
00044 inline LLVMTargetRef wrap(const Target * P) {
00045   return reinterpret_cast<LLVMTargetRef>(const_cast<Target*>(P));
00046 }
00047 
00048 LLVMTargetRef LLVMGetFirstTarget() {
00049   if(TargetRegistry::begin() == TargetRegistry::end()) {
00050     return nullptr;
00051   }
00052 
00053   const Target* target = &*TargetRegistry::begin();
00054   return wrap(target);
00055 }
00056 LLVMTargetRef LLVMGetNextTarget(LLVMTargetRef T) {
00057   return wrap(unwrap(T)->getNext());
00058 }
00059 
00060 LLVMTargetRef LLVMGetTargetFromName(const char *Name) {
00061   StringRef NameRef = Name;
00062   for (TargetRegistry::iterator IT = TargetRegistry::begin(),
00063                                 IE = TargetRegistry::end(); IT != IE; ++IT) {
00064     if (IT->getName() == NameRef)
00065       return wrap(&*IT);
00066   }
00067   
00068   return nullptr;
00069 }
00070 
00071 LLVMBool LLVMGetTargetFromTriple(const char* TripleStr, LLVMTargetRef *T,
00072                                  char **ErrorMessage) {
00073   std::string Error;
00074   
00075   *T = wrap(TargetRegistry::lookupTarget(TripleStr, Error));
00076   
00077   if (!*T) {
00078     if (ErrorMessage)
00079       *ErrorMessage = strdup(Error.c_str());
00080 
00081     return 1;
00082   }
00083   
00084   return 0;
00085 }
00086 
00087 const char * LLVMGetTargetName(LLVMTargetRef T) {
00088   return unwrap(T)->getName();
00089 }
00090 
00091 const char * LLVMGetTargetDescription(LLVMTargetRef T) {
00092   return unwrap(T)->getShortDescription();
00093 }
00094 
00095 LLVMBool LLVMTargetHasJIT(LLVMTargetRef T) {
00096   return unwrap(T)->hasJIT();
00097 }
00098 
00099 LLVMBool LLVMTargetHasTargetMachine(LLVMTargetRef T) {
00100   return unwrap(T)->hasTargetMachine();
00101 }
00102 
00103 LLVMBool LLVMTargetHasAsmBackend(LLVMTargetRef T) {
00104   return unwrap(T)->hasMCAsmBackend();
00105 }
00106 
00107 LLVMTargetMachineRef LLVMCreateTargetMachine(LLVMTargetRef T,
00108         const char* Triple, const char* CPU, const char* Features,
00109         LLVMCodeGenOptLevel Level, LLVMRelocMode Reloc,
00110         LLVMCodeModel CodeModel) {
00111   Reloc::Model RM;
00112   switch (Reloc){
00113     case LLVMRelocStatic:
00114       RM = Reloc::Static;
00115       break;
00116     case LLVMRelocPIC:
00117       RM = Reloc::PIC_;
00118       break;
00119     case LLVMRelocDynamicNoPic:
00120       RM = Reloc::DynamicNoPIC;
00121       break;
00122     default:
00123       RM = Reloc::Default;
00124       break;
00125   }
00126 
00127   CodeModel::Model CM = unwrap(CodeModel);
00128 
00129   CodeGenOpt::Level OL;
00130   switch (Level) {
00131     case LLVMCodeGenLevelNone:
00132       OL = CodeGenOpt::None;
00133       break;
00134     case LLVMCodeGenLevelLess:
00135       OL = CodeGenOpt::Less;
00136       break;
00137     case LLVMCodeGenLevelAggressive:
00138       OL = CodeGenOpt::Aggressive;
00139       break;
00140     default:
00141       OL = CodeGenOpt::Default;
00142       break;
00143   }
00144 
00145   TargetOptions opt;
00146   return wrap(unwrap(T)->createTargetMachine(Triple, CPU, Features, opt, RM,
00147     CM, OL));
00148 }
00149 
00150 
00151 void LLVMDisposeTargetMachine(LLVMTargetMachineRef T) {
00152   delete unwrap(T);
00153 }
00154 
00155 LLVMTargetRef LLVMGetTargetMachineTarget(LLVMTargetMachineRef T) {
00156   const Target* target = &(unwrap(T)->getTarget());
00157   return wrap(target);
00158 }
00159 
00160 char* LLVMGetTargetMachineTriple(LLVMTargetMachineRef T) {
00161   std::string StringRep = unwrap(T)->getTargetTriple();
00162   return strdup(StringRep.c_str());
00163 }
00164 
00165 char* LLVMGetTargetMachineCPU(LLVMTargetMachineRef T) {
00166   std::string StringRep = unwrap(T)->getTargetCPU();
00167   return strdup(StringRep.c_str());
00168 }
00169 
00170 char* LLVMGetTargetMachineFeatureString(LLVMTargetMachineRef T) {
00171   std::string StringRep = unwrap(T)->getTargetFeatureString();
00172   return strdup(StringRep.c_str());
00173 }
00174 
00175 LLVMTargetDataRef LLVMGetTargetMachineData(LLVMTargetMachineRef T) {
00176   return wrap(unwrap(T)->getSubtargetImpl()->getDataLayout());
00177 }
00178 
00179 void LLVMSetTargetMachineAsmVerbosity(LLVMTargetMachineRef T,
00180                                       LLVMBool VerboseAsm) {
00181   unwrap(T)->setAsmVerbosityDefault(VerboseAsm);
00182 }
00183 
00184 static LLVMBool LLVMTargetMachineEmit(LLVMTargetMachineRef T, LLVMModuleRef M,
00185   formatted_raw_ostream &OS, LLVMCodeGenFileType codegen, char **ErrorMessage) {
00186   TargetMachine* TM = unwrap(T);
00187   Module* Mod = unwrap(M);
00188 
00189   PassManager pass;
00190 
00191   std::string error;
00192 
00193   const DataLayout *td = TM->getSubtargetImpl()->getDataLayout();
00194 
00195   if (!td) {
00196     error = "No DataLayout in TargetMachine";
00197     *ErrorMessage = strdup(error.c_str());
00198     return true;
00199   }
00200   Mod->setDataLayout(td);
00201   pass.add(new DataLayoutPass());
00202 
00203   TargetMachine::CodeGenFileType ft;
00204   switch (codegen) {
00205     case LLVMAssemblyFile:
00206       ft = TargetMachine::CGFT_AssemblyFile;
00207       break;
00208     default:
00209       ft = TargetMachine::CGFT_ObjectFile;
00210       break;
00211   }
00212   if (TM->addPassesToEmitFile(pass, OS, ft)) {
00213     error = "TargetMachine can't emit a file of this type";
00214     *ErrorMessage = strdup(error.c_str());
00215     return true;
00216   }
00217 
00218   pass.run(*Mod);
00219 
00220   OS.flush();
00221   return false;
00222 }
00223 
00224 LLVMBool LLVMTargetMachineEmitToFile(LLVMTargetMachineRef T, LLVMModuleRef M,
00225   char* Filename, LLVMCodeGenFileType codegen, char** ErrorMessage) {
00226   std::error_code EC;
00227   raw_fd_ostream dest(Filename, EC, sys::fs::F_None);
00228   if (EC) {
00229     *ErrorMessage = strdup(EC.message().c_str());
00230     return true;
00231   }
00232   formatted_raw_ostream destf(dest);
00233   bool Result = LLVMTargetMachineEmit(T, M, destf, codegen, ErrorMessage);
00234   dest.flush();
00235   return Result;
00236 }
00237 
00238 LLVMBool LLVMTargetMachineEmitToMemoryBuffer(LLVMTargetMachineRef T,
00239   LLVMModuleRef M, LLVMCodeGenFileType codegen, char** ErrorMessage,
00240   LLVMMemoryBufferRef *OutMemBuf) {
00241   std::string CodeString;
00242   raw_string_ostream OStream(CodeString);
00243   formatted_raw_ostream Out(OStream);
00244   bool Result = LLVMTargetMachineEmit(T, M, Out, codegen, ErrorMessage);
00245   OStream.flush();
00246 
00247   std::string &Data = OStream.str();
00248   *OutMemBuf = LLVMCreateMemoryBufferWithMemoryRangeCopy(Data.c_str(),
00249                                                      Data.length(), "");
00250   return Result;
00251 }
00252 
00253 char *LLVMGetDefaultTargetTriple(void) {
00254   return strdup(sys::getDefaultTargetTriple().c_str());
00255 }
00256 
00257 void LLVMAddAnalysisPasses(LLVMTargetMachineRef T, LLVMPassManagerRef PM) {
00258   unwrap(T)->addAnalysisPasses(*unwrap(PM));
00259 }