X-Git-Url: http://demsky.eecs.uci.edu/git/?a=blobdiff_plain;f=lib%2FTarget%2FTargetMachineC.cpp;h=719923558de489f0431347404b9343a2f7567ee7;hb=26be2142324893e254ec9ba91da3a54694936498;hp=79f74bd66127a8b6396814325b21a761dc9d4409;hpb=f3a64c00c189c1961a49aed9ce6f21e133bdde03;p=oota-llvm.git diff --git a/lib/Target/TargetMachineC.cpp b/lib/Target/TargetMachineC.cpp index 79f74bd6612..719923558de 100644 --- a/lib/Target/TargetMachineC.cpp +++ b/lib/Target/TargetMachineC.cpp @@ -14,30 +14,74 @@ #include "llvm-c/TargetMachine.h" #include "llvm-c/Core.h" #include "llvm-c/Target.h" +#include "llvm/Analysis/TargetTransformInfo.h" #include "llvm/IR/DataLayout.h" #include "llvm/IR/Module.h" -#include "llvm/PassManager.h" +#include "llvm/IR/LegacyPassManager.h" #include "llvm/Support/CodeGen.h" +#include "llvm/Support/FileSystem.h" #include "llvm/Support/FormattedStream.h" +#include "llvm/Support/Host.h" #include "llvm/Support/TargetRegistry.h" #include "llvm/Support/raw_ostream.h" #include "llvm/Target/TargetMachine.h" +#include "llvm/Target/TargetSubtargetInfo.h" #include #include #include using namespace llvm; - +inline TargetMachine *unwrap(LLVMTargetMachineRef P) { + return reinterpret_cast(P); +} +inline Target *unwrap(LLVMTargetRef P) { + return reinterpret_cast(P); +} +inline LLVMTargetMachineRef wrap(const TargetMachine *P) { + return + reinterpret_cast(const_cast(P)); +} +inline LLVMTargetRef wrap(const Target * P) { + return reinterpret_cast(const_cast(P)); +} LLVMTargetRef LLVMGetFirstTarget() { - const Target* target = &*TargetRegistry::begin(); - return wrap(target); + if (TargetRegistry::targets().begin() == TargetRegistry::targets().end()) { + return nullptr; + } + + const Target *target = &*TargetRegistry::targets().begin(); + return wrap(target); } LLVMTargetRef LLVMGetNextTarget(LLVMTargetRef T) { return wrap(unwrap(T)->getNext()); } +LLVMTargetRef LLVMGetTargetFromName(const char *Name) { + StringRef NameRef = Name; + auto I = std::find_if( + TargetRegistry::targets().begin(), TargetRegistry::targets().end(), + [&](const Target &T) { return T.getName() == NameRef; }); + return I != TargetRegistry::targets().end() ? wrap(&*I) : nullptr; +} + +LLVMBool LLVMGetTargetFromTriple(const char* TripleStr, LLVMTargetRef *T, + char **ErrorMessage) { + std::string Error; + + *T = wrap(TargetRegistry::lookupTarget(TripleStr, Error)); + + if (!*T) { + if (ErrorMessage) + *ErrorMessage = strdup(Error.c_str()); + + return 1; + } + + return 0; +} + const char * LLVMGetTargetName(LLVMTargetRef T) { return unwrap(T)->getName(); } @@ -58,9 +102,10 @@ LLVMBool LLVMTargetHasAsmBackend(LLVMTargetRef T) { return unwrap(T)->hasMCAsmBackend(); } -LLVMTargetMachineRef LLVMCreateTargetMachine(LLVMTargetRef T, char* Triple, - char* CPU, char* Features, LLVMCodeGenOptLevel Level, LLVMRelocMode Reloc, - LLVMCodeModel CodeModel) { +LLVMTargetMachineRef LLVMCreateTargetMachine(LLVMTargetRef T, + const char* Triple, const char* CPU, const char* Features, + LLVMCodeGenOptLevel Level, LLVMRelocMode Reloc, + LLVMCodeModel CodeModel) { Reloc::Model RM; switch (Reloc){ case LLVMRelocStatic: @@ -77,29 +122,9 @@ LLVMTargetMachineRef LLVMCreateTargetMachine(LLVMTargetRef T, char* Triple, break; } - CodeModel::Model CM; - switch (CodeModel) { - case LLVMCodeModelJITDefault: - CM = CodeModel::JITDefault; - break; - case LLVMCodeModelSmall: - CM = CodeModel::Small; - break; - case LLVMCodeModelKernel: - CM = CodeModel::Kernel; - break; - case LLVMCodeModelMedium: - CM = CodeModel::Medium; - break; - case LLVMCodeModelLarge: - CM = CodeModel::Large; - break; - default: - CM = CodeModel::Default; - break; - } - CodeGenOpt::Level OL; + CodeModel::Model CM = unwrap(CodeModel); + CodeGenOpt::Level OL; switch (Level) { case LLVMCodeGenLevelNone: OL = CodeGenOpt::None; @@ -131,7 +156,7 @@ LLVMTargetRef LLVMGetTargetMachineTarget(LLVMTargetMachineRef T) { } char* LLVMGetTargetMachineTriple(LLVMTargetMachineRef T) { - std::string StringRep = unwrap(T)->getTargetTriple(); + std::string StringRep = unwrap(T)->getTargetTriple().str(); return strdup(StringRep.c_str()); } @@ -149,23 +174,30 @@ LLVMTargetDataRef LLVMGetTargetMachineData(LLVMTargetMachineRef T) { return wrap(unwrap(T)->getDataLayout()); } -LLVMBool LLVMTargetMachineEmitToFile(LLVMTargetMachineRef T, LLVMModuleRef M, - char* Filename, LLVMCodeGenFileType codegen, char** ErrorMessage) { +void LLVMSetTargetMachineAsmVerbosity(LLVMTargetMachineRef T, + LLVMBool VerboseAsm) { + unwrap(T)->Options.MCOptions.AsmVerbose = VerboseAsm; +} + +static LLVMBool LLVMTargetMachineEmit(LLVMTargetMachineRef T, LLVMModuleRef M, + raw_pwrite_stream &OS, + LLVMCodeGenFileType codegen, + char **ErrorMessage) { TargetMachine* TM = unwrap(T); Module* Mod = unwrap(M); - PassManager pass; + legacy::PassManager pass; std::string error; - const DataLayout* td = TM->getDataLayout(); + const DataLayout *td = TM->getDataLayout(); if (!td) { error = "No DataLayout in TargetMachine"; *ErrorMessage = strdup(error.c_str()); return true; } - pass.add(new DataLayout(*td)); + Mod->setDataLayout(*td); TargetMachine::CodeGenFileType ft; switch (codegen) { @@ -176,14 +208,7 @@ LLVMBool LLVMTargetMachineEmitToFile(LLVMTargetMachineRef T, LLVMModuleRef M, ft = TargetMachine::CGFT_ObjectFile; break; } - raw_fd_ostream dest(Filename, error, raw_fd_ostream::F_Binary); - formatted_raw_ostream destf(dest); - if (!error.empty()) { - *ErrorMessage = strdup(error.c_str()); - return true; - } - - if (TM->addPassesToEmitFile(pass, destf, ft)) { + if (TM->addPassesToEmitFile(pass, OS, ft)) { error = "TargetMachine can't emit a file of this type"; *ErrorMessage = strdup(error.c_str()); return true; @@ -191,7 +216,42 @@ LLVMBool LLVMTargetMachineEmitToFile(LLVMTargetMachineRef T, LLVMModuleRef M, pass.run(*Mod); - destf.flush(); - dest.flush(); + OS.flush(); return false; } + +LLVMBool LLVMTargetMachineEmitToFile(LLVMTargetMachineRef T, LLVMModuleRef M, + char* Filename, LLVMCodeGenFileType codegen, char** ErrorMessage) { + std::error_code EC; + raw_fd_ostream dest(Filename, EC, sys::fs::F_None); + if (EC) { + *ErrorMessage = strdup(EC.message().c_str()); + return true; + } + bool Result = LLVMTargetMachineEmit(T, M, dest, codegen, ErrorMessage); + dest.flush(); + return Result; +} + +LLVMBool LLVMTargetMachineEmitToMemoryBuffer(LLVMTargetMachineRef T, + LLVMModuleRef M, LLVMCodeGenFileType codegen, char** ErrorMessage, + LLVMMemoryBufferRef *OutMemBuf) { + SmallString<0> CodeString; + raw_svector_ostream OStream(CodeString); + bool Result = LLVMTargetMachineEmit(T, M, OStream, codegen, ErrorMessage); + OStream.flush(); + + StringRef Data = OStream.str(); + *OutMemBuf = + LLVMCreateMemoryBufferWithMemoryRangeCopy(Data.data(), Data.size(), ""); + return Result; +} + +char *LLVMGetDefaultTargetTriple(void) { + return strdup(sys::getDefaultTargetTriple().c_str()); +} + +void LLVMAddAnalysisPasses(LLVMTargetMachineRef T, LLVMPassManagerRef PM) { + unwrap(PM)->add( + createTargetTransformInfoWrapperPass(unwrap(T)->getTargetIRAnalysis())); +}