X-Git-Url: http://plrg.eecs.uci.edu/git/?a=blobdiff_plain;f=lib%2FTarget%2FTargetMachineC.cpp;h=f82566c37baa43c4d237fdffd8f720514ac526d1;hb=30e7d038e978a442d0f791fc151c0088559f5ec8;hp=cbfb914d9dcc81ae66c21719553caa00121817f5;hpb=23295f613bd73f7899b3e1f4c433d7a518dd4297;p=oota-llvm.git diff --git a/lib/Target/TargetMachineC.cpp b/lib/Target/TargetMachineC.cpp index cbfb914d9dc..f82566c37ba 100644 --- a/lib/Target/TargetMachineC.cpp +++ b/lib/Target/TargetMachineC.cpp @@ -32,26 +32,34 @@ using namespace llvm; -inline TargetMachine *unwrap(LLVMTargetMachineRef P) { - return reinterpret_cast(P); +namespace llvm { +// Friend to the TargetMachine, access legacy API that are made private in C++ +struct C_API_PRIVATE_ACCESS { + static const DataLayout &getDataLayout(const TargetMachine &T) { + return T.getDataLayout(); + } +}; +} + +static TargetMachine *unwrap(LLVMTargetMachineRef P) { + return reinterpret_cast(P); } -inline Target *unwrap(LLVMTargetRef P) { +static Target *unwrap(LLVMTargetRef P) { return reinterpret_cast(P); } -inline LLVMTargetMachineRef wrap(const TargetMachine *P) { - return - reinterpret_cast(const_cast(P)); +static LLVMTargetMachineRef wrap(const TargetMachine *P) { + return reinterpret_cast(const_cast(P)); } -inline LLVMTargetRef wrap(const Target * P) { +static LLVMTargetRef wrap(const Target * P) { return reinterpret_cast(const_cast(P)); } LLVMTargetRef LLVMGetFirstTarget() { - if(TargetRegistry::begin() == TargetRegistry::end()) { + if (TargetRegistry::targets().begin() == TargetRegistry::targets().end()) { return nullptr; } - const Target* target = &*TargetRegistry::begin(); + const Target *target = &*TargetRegistry::targets().begin(); return wrap(target); } LLVMTargetRef LLVMGetNextTarget(LLVMTargetRef T) { @@ -60,28 +68,25 @@ LLVMTargetRef LLVMGetNextTarget(LLVMTargetRef T) { LLVMTargetRef LLVMGetTargetFromName(const char *Name) { StringRef NameRef = Name; - for (TargetRegistry::iterator IT = TargetRegistry::begin(), - IE = TargetRegistry::end(); IT != IE; ++IT) { - if (IT->getName() == NameRef) - return wrap(&*IT); - } - - return nullptr; + 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; } @@ -148,10 +153,7 @@ LLVMTargetMachineRef LLVMCreateTargetMachine(LLVMTargetRef T, CM, OL)); } - -void LLVMDisposeTargetMachine(LLVMTargetMachineRef T) { - delete unwrap(T); -} +void LLVMDisposeTargetMachine(LLVMTargetMachineRef T) { delete unwrap(T); } LLVMTargetRef LLVMGetTargetMachineTarget(LLVMTargetMachineRef T) { const Target* target = &(unwrap(T)->getTarget()); @@ -159,7 +161,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()); } @@ -173,8 +175,9 @@ char* LLVMGetTargetMachineFeatureString(LLVMTargetMachineRef T) { return strdup(StringRep.c_str()); } +/** Deprecated: use LLVMGetDataLayout(LLVMModuleRef M) instead. */ LLVMTargetDataRef LLVMGetTargetMachineData(LLVMTargetMachineRef T) { - return wrap(unwrap(T)->getDataLayout()); + return wrap(&C_API_PRIVATE_ACCESS::getDataLayout(*unwrap(T))); } void LLVMSetTargetMachineAsmVerbosity(LLVMTargetMachineRef T, @@ -183,7 +186,7 @@ void LLVMSetTargetMachineAsmVerbosity(LLVMTargetMachineRef T, } static LLVMBool LLVMTargetMachineEmit(LLVMTargetMachineRef T, LLVMModuleRef M, - raw_ostream &OS, + raw_pwrite_stream &OS, LLVMCodeGenFileType codegen, char **ErrorMessage) { TargetMachine* TM = unwrap(T); @@ -193,14 +196,7 @@ static LLVMBool LLVMTargetMachineEmit(LLVMTargetMachineRef T, LLVMModuleRef M, std::string error; - const DataLayout *td = TM->getDataLayout(); - - if (!td) { - error = "No DataLayout in TargetMachine"; - *ErrorMessage = strdup(error.c_str()); - return true; - } - Mod->setDataLayout(*td); + Mod->setDataLayout(TM->createDataLayout()); TargetMachine::CodeGenFileType ft; switch (codegen) { @@ -231,8 +227,7 @@ LLVMBool LLVMTargetMachineEmitToFile(LLVMTargetMachineRef T, LLVMModuleRef M, *ErrorMessage = strdup(EC.message().c_str()); return true; } - formatted_raw_ostream destf(dest); - bool Result = LLVMTargetMachineEmit(T, M, destf, codegen, ErrorMessage); + bool Result = LLVMTargetMachineEmit(T, M, dest, codegen, ErrorMessage); dest.flush(); return Result; } @@ -240,15 +235,13 @@ LLVMBool LLVMTargetMachineEmitToFile(LLVMTargetMachineRef T, LLVMModuleRef M, LLVMBool LLVMTargetMachineEmitToMemoryBuffer(LLVMTargetMachineRef T, LLVMModuleRef M, LLVMCodeGenFileType codegen, char** ErrorMessage, LLVMMemoryBufferRef *OutMemBuf) { - std::string CodeString; - raw_string_ostream OStream(CodeString); - formatted_raw_ostream Out(OStream); - bool Result = LLVMTargetMachineEmit(T, M, Out, codegen, ErrorMessage); - OStream.flush(); - - std::string &Data = OStream.str(); - *OutMemBuf = LLVMCreateMemoryBufferWithMemoryRangeCopy(Data.c_str(), - Data.length(), ""); + SmallString<0> CodeString; + raw_svector_ostream OStream(CodeString); + bool Result = LLVMTargetMachineEmit(T, M, OStream, codegen, ErrorMessage); + + StringRef Data = OStream.str(); + *OutMemBuf = + LLVMCreateMemoryBufferWithMemoryRangeCopy(Data.data(), Data.size(), ""); return Result; }