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/FormattedStream.h"
00022 #include "llvm/Support/TargetRegistry.h"
00023 #include "llvm/Support/raw_ostream.h"
00024 #include "llvm/Target/TargetMachine.h"
00025 #include <cassert>
00026 #include <cstdlib>
00027 #include <cstring>
00028 
00029 using namespace llvm;
00030 
00031 inline DataLayout *unwrap(LLVMTargetDataRef P) {
00032   return reinterpret_cast<DataLayout*>(P);
00033 }
00034 
00035 inline LLVMTargetDataRef wrap(const DataLayout *P) {
00036   return reinterpret_cast<LLVMTargetDataRef>(const_cast<DataLayout*>(P));
00037 }
00038 
00039 inline TargetLibraryInfo *unwrap(LLVMTargetLibraryInfoRef P) {
00040   return reinterpret_cast<TargetLibraryInfo*>(P);
00041 }
00042 
00043 inline LLVMTargetLibraryInfoRef wrap(const TargetLibraryInfo *P) {
00044   TargetLibraryInfo *X = const_cast<TargetLibraryInfo*>(P);
00045   return reinterpret_cast<LLVMTargetLibraryInfoRef>(X);
00046 }
00047 
00048 inline TargetMachine *unwrap(LLVMTargetMachineRef P) {
00049   return reinterpret_cast<TargetMachine*>(P);
00050 }
00051 inline Target *unwrap(LLVMTargetRef P) {
00052   return reinterpret_cast<Target*>(P);
00053 }
00054 inline LLVMTargetMachineRef wrap(const TargetMachine *P) {
00055   return
00056     reinterpret_cast<LLVMTargetMachineRef>(const_cast<TargetMachine*>(P));
00057 }
00058 inline LLVMTargetRef wrap(const Target * P) {
00059   return reinterpret_cast<LLVMTargetRef>(const_cast<Target*>(P));
00060 }
00061 
00062 LLVMTargetRef LLVMGetFirstTarget() {
00063    const Target* target = &*TargetRegistry::begin();
00064    return wrap(target);
00065 }
00066 LLVMTargetRef LLVMGetNextTarget(LLVMTargetRef T) {
00067   return wrap(unwrap(T)->getNext());
00068 }
00069 
00070 const char * LLVMGetTargetName(LLVMTargetRef T) {
00071   return unwrap(T)->getName();
00072 }
00073 
00074 const char * LLVMGetTargetDescription(LLVMTargetRef T) {
00075   return unwrap(T)->getShortDescription();
00076 }
00077 
00078 LLVMBool LLVMTargetHasJIT(LLVMTargetRef T) {
00079   return unwrap(T)->hasJIT();
00080 }
00081 
00082 LLVMBool LLVMTargetHasTargetMachine(LLVMTargetRef T) {
00083   return unwrap(T)->hasTargetMachine();
00084 }
00085 
00086 LLVMBool LLVMTargetHasAsmBackend(LLVMTargetRef T) {
00087   return unwrap(T)->hasMCAsmBackend();
00088 }
00089 
00090 LLVMTargetMachineRef LLVMCreateTargetMachine(LLVMTargetRef T, char* Triple,
00091   char* CPU, char* Features, LLVMCodeGenOptLevel Level, LLVMRelocMode Reloc,
00092   LLVMCodeModel CodeModel) {
00093   Reloc::Model RM;
00094   switch (Reloc){
00095     case LLVMRelocStatic:
00096       RM = Reloc::Static;
00097       break;
00098     case LLVMRelocPIC:
00099       RM = Reloc::PIC_;
00100       break;
00101     case LLVMRelocDynamicNoPic:
00102       RM = Reloc::DynamicNoPIC;
00103       break;
00104     default:
00105       RM = Reloc::Default;
00106       break;
00107   }
00108 
00109   CodeModel::Model CM = unwrap(CodeModel);
00110 
00111   CodeGenOpt::Level OL;
00112   switch (Level) {
00113     case LLVMCodeGenLevelNone:
00114       OL = CodeGenOpt::None;
00115       break;
00116     case LLVMCodeGenLevelLess:
00117       OL = CodeGenOpt::Less;
00118       break;
00119     case LLVMCodeGenLevelAggressive:
00120       OL = CodeGenOpt::Aggressive;
00121       break;
00122     default:
00123       OL = CodeGenOpt::Default;
00124       break;
00125   }
00126 
00127   TargetOptions opt;
00128   return wrap(unwrap(T)->createTargetMachine(Triple, CPU, Features, opt, RM,
00129     CM, OL));
00130 }
00131 
00132 
00133 void LLVMDisposeTargetMachine(LLVMTargetMachineRef T) {
00134   delete unwrap(T);
00135 }
00136 
00137 LLVMTargetRef LLVMGetTargetMachineTarget(LLVMTargetMachineRef T) {
00138   const Target* target = &(unwrap(T)->getTarget());
00139   return wrap(target);
00140 }
00141 
00142 char* LLVMGetTargetMachineTriple(LLVMTargetMachineRef T) {
00143   std::string StringRep = unwrap(T)->getTargetTriple();
00144   return strdup(StringRep.c_str());
00145 }
00146 
00147 char* LLVMGetTargetMachineCPU(LLVMTargetMachineRef T) {
00148   std::string StringRep = unwrap(T)->getTargetCPU();
00149   return strdup(StringRep.c_str());
00150 }
00151 
00152 char* LLVMGetTargetMachineFeatureString(LLVMTargetMachineRef T) {
00153   std::string StringRep = unwrap(T)->getTargetFeatureString();
00154   return strdup(StringRep.c_str());
00155 }
00156 
00157 LLVMTargetDataRef LLVMGetTargetMachineData(LLVMTargetMachineRef T) {
00158   return wrap(unwrap(T)->getDataLayout());
00159 }
00160 
00161 static LLVMBool LLVMTargetMachineEmit(LLVMTargetMachineRef T, LLVMModuleRef M,
00162   formatted_raw_ostream &OS, LLVMCodeGenFileType codegen, char **ErrorMessage) {
00163   TargetMachine* TM = unwrap(T);
00164   Module* Mod = unwrap(M);
00165 
00166   PassManager pass;
00167 
00168   std::string error;
00169 
00170   const DataLayout* td = TM->getDataLayout();
00171 
00172   if (!td) {
00173     error = "No DataLayout in TargetMachine";
00174     *ErrorMessage = strdup(error.c_str());
00175     return true;
00176   }
00177   pass.add(new DataLayout(*td));
00178 
00179   TargetMachine::CodeGenFileType ft;
00180   switch (codegen) {
00181     case LLVMAssemblyFile:
00182       ft = TargetMachine::CGFT_AssemblyFile;
00183       break;
00184     default:
00185       ft = TargetMachine::CGFT_ObjectFile;
00186       break;
00187   }
00188   if (TM->addPassesToEmitFile(pass, OS, ft)) {
00189     error = "TargetMachine can't emit a file of this type";
00190     *ErrorMessage = strdup(error.c_str());
00191     return true;
00192   }
00193 
00194   pass.run(*Mod);
00195 
00196   OS.flush();
00197   return false;
00198 }
00199 
00200 LLVMBool LLVMTargetMachineEmitToFile(LLVMTargetMachineRef T, LLVMModuleRef M,
00201   char* Filename, LLVMCodeGenFileType codegen, char** ErrorMessage) {
00202   std::string error;
00203   raw_fd_ostream dest(Filename, error, raw_fd_ostream::F_Binary);
00204   formatted_raw_ostream destf(dest);
00205   if (!error.empty()) {
00206     *ErrorMessage = strdup(error.c_str());
00207     return true;
00208   }
00209   bool Result = LLVMTargetMachineEmit(T, M, destf, codegen, ErrorMessage);
00210   dest.flush();
00211   return Result;
00212 }
00213 
00214 LLVMBool LLVMTargetMachineEmitToMemoryBuffer(LLVMTargetMachineRef T,
00215   LLVMModuleRef M, LLVMCodeGenFileType codegen, char** ErrorMessage,
00216   LLVMMemoryBufferRef *OutMemBuf) {
00217   std::string CodeString;
00218   raw_string_ostream OStream(CodeString);
00219   formatted_raw_ostream Out(OStream);
00220   bool Result = LLVMTargetMachineEmit(T, M, Out, codegen, ErrorMessage);
00221   OStream.flush();
00222 
00223   std::string &Data = OStream.str();
00224   *OutMemBuf = LLVMCreateMemoryBufferWithMemoryRangeCopy(Data.c_str(),
00225                                                      Data.length(), "");
00226   return Result;
00227 }