X-Git-Url: http://demsky.eecs.uci.edu/git/?a=blobdiff_plain;f=lib%2FVMCore%2FLLVMContext.cpp;h=2446ec996d043d63607b42782add9ab6fe7ac890;hb=4903c15b7d92802a4f0f28928a89bb4c0d5e212f;hp=e38986eb3df9b2f55eb8f46ebab4b82b0196e1c2;hpb=baf3c404409d5e47b13984a7f95bfbd6d1f2e79e;p=oota-llvm.git diff --git a/lib/VMCore/LLVMContext.cpp b/lib/VMCore/LLVMContext.cpp index e38986eb3df..2446ec996d0 100644 --- a/lib/VMCore/LLVMContext.cpp +++ b/lib/VMCore/LLVMContext.cpp @@ -8,19 +8,18 @@ //===----------------------------------------------------------------------===// // // This file implements LLVMContext, as a wrapper around the opaque -// class LLVMContextImpl. +// class LLVMContextImpl. // //===----------------------------------------------------------------------===// #include "llvm/LLVMContext.h" +#include "llvm/Metadata.h" #include "llvm/Constants.h" -#include "llvm/DerivedTypes.h" #include "llvm/Instruction.h" -#include "llvm/Metadata.h" #include "llvm/Support/ManagedStatic.h" +#include "llvm/Support/SourceMgr.h" #include "LLVMContextImpl.h" -#include - +#include using namespace llvm; static ManagedStatic GlobalContext; @@ -29,181 +28,135 @@ LLVMContext& llvm::getGlobalContext() { return *GlobalContext; } -LLVMContext::LLVMContext() : pImpl(new LLVMContextImpl(*this)) { } -LLVMContext::~LLVMContext() { delete pImpl; } +LLVMContext::LLVMContext() : pImpl(new LLVMContextImpl(*this)) { + // Create the fixed metadata kinds. This is done in the same order as the + // MD_* enum values so that they correspond. -// Constant accessors - -// Constructor to create a '0' constant of arbitrary type... -static const uint64_t zero[2] = {0, 0}; -Constant* LLVMContext::getNullValue(const Type* Ty) { - switch (Ty->getTypeID()) { - case Type::IntegerTyID: - return ConstantInt::get(Ty, 0); - case Type::FloatTyID: - return ConstantFP::get(Ty->getContext(), APFloat(APInt(32, 0))); - case Type::DoubleTyID: - return ConstantFP::get(Ty->getContext(), APFloat(APInt(64, 0))); - case Type::X86_FP80TyID: - return ConstantFP::get(Ty->getContext(), APFloat(APInt(80, 2, zero))); - case Type::FP128TyID: - return ConstantFP::get(Ty->getContext(), - APFloat(APInt(128, 2, zero), true)); - case Type::PPC_FP128TyID: - return ConstantFP::get(Ty->getContext(), APFloat(APInt(128, 2, zero))); - case Type::PointerTyID: - return getConstantPointerNull(cast(Ty)); - case Type::StructTyID: - case Type::ArrayTyID: - case Type::VectorTyID: - return getConstantAggregateZero(Ty); - default: - // Function, Label, or Opaque type? - assert(!"Cannot create a null constant of that type!"); - return 0; - } -} + // Create the 'dbg' metadata kind. + unsigned DbgID = getMDKindID("dbg"); + assert(DbgID == MD_dbg && "dbg kind id drifted"); (void)DbgID; -Constant* LLVMContext::getAllOnesValue(const Type* Ty) { - if (const IntegerType* ITy = dyn_cast(Ty)) - return ConstantInt::get(*this, APInt::getAllOnesValue(ITy->getBitWidth())); - - std::vector Elts; - const VectorType* VTy = cast(Ty); - Elts.resize(VTy->getNumElements(), getAllOnesValue(VTy->getElementType())); - assert(Elts[0] && "Not a vector integer type!"); - return cast(ConstantVector::get(Elts)); -} + // Create the 'tbaa' metadata kind. + unsigned TBAAID = getMDKindID("tbaa"); + assert(TBAAID == MD_tbaa && "tbaa kind id drifted"); (void)TBAAID; -// UndefValue accessors. -UndefValue* LLVMContext::getUndef(const Type* Ty) { - return UndefValue::get(Ty); -} + // Create the 'prof' metadata kind. + unsigned ProfID = getMDKindID("prof"); + assert(ProfID == MD_prof && "prof kind id drifted"); (void)ProfID; -// ConstantInt accessors. -ConstantInt* LLVMContext::getTrue() { - assert(this && "Context not initialized!"); - assert(pImpl && "Context not initialized!"); - return pImpl->getTrue(); -} + // Create the 'fpmath' metadata kind. + unsigned FPAccuracyID = getMDKindID("fpmath"); + assert(FPAccuracyID == MD_fpmath && "fpmath kind id drifted"); + (void)FPAccuracyID; -ConstantInt* LLVMContext::getFalse() { - assert(this && "Context not initialized!"); - assert(pImpl && "Context not initialized!"); - return pImpl->getFalse(); -} + // Create the 'range' metadata kind. + unsigned RangeID = getMDKindID("range"); + assert(RangeID == MD_range && "range kind id drifted"); + (void)RangeID; -// ConstantPointerNull accessors. -ConstantPointerNull* LLVMContext::getConstantPointerNull(const PointerType* T) { - return ConstantPointerNull::get(T); + // Create the 'tbaa.struct' metadata kind. + unsigned TBAAStructID = getMDKindID("tbaa.struct"); + assert(TBAAStructID == MD_tbaa_struct && "tbaa.struct kind id drifted"); + (void)TBAAStructID; } +LLVMContext::~LLVMContext() { delete pImpl; } -// ConstantAggregateZero accessors. -ConstantAggregateZero* LLVMContext::getConstantAggregateZero(const Type* Ty) { - return pImpl->getConstantAggregateZero(Ty); +void LLVMContext::addModule(Module *M) { + pImpl->OwnedModules.insert(M); } -// MDNode accessors -MDNode* LLVMContext::getMDNode(Value* const* Vals, unsigned NumVals) { - return pImpl->getMDNode(Vals, NumVals); +void LLVMContext::removeModule(Module *M) { + pImpl->OwnedModules.erase(M); } -// MDString accessors -MDString* LLVMContext::getMDString(const StringRef &Str) { - return pImpl->getMDString(Str.data(), Str.size()); -} +//===----------------------------------------------------------------------===// +// Recoverable Backend Errors +//===----------------------------------------------------------------------===// -// FunctionType accessors -FunctionType* LLVMContext::getFunctionType(const Type* Result, bool isVarArg) { - return FunctionType::get(Result, isVarArg); +void LLVMContext:: +setInlineAsmDiagnosticHandler(InlineAsmDiagHandlerTy DiagHandler, + void *DiagContext) { + pImpl->InlineAsmDiagHandler = DiagHandler; + pImpl->InlineAsmDiagContext = DiagContext; } -FunctionType* LLVMContext::getFunctionType(const Type* Result, - const std::vector& Params, - bool isVarArg) { - return FunctionType::get(Result, Params, isVarArg); -} - -// IntegerType accessors -const IntegerType* LLVMContext::getIntegerType(unsigned NumBits) { - return IntegerType::get(NumBits); -} - -// OpaqueType accessors -OpaqueType* LLVMContext::getOpaqueType() { - return OpaqueType::get(); +/// getInlineAsmDiagnosticHandler - Return the diagnostic handler set by +/// setInlineAsmDiagnosticHandler. +LLVMContext::InlineAsmDiagHandlerTy +LLVMContext::getInlineAsmDiagnosticHandler() const { + return pImpl->InlineAsmDiagHandler; } -// StructType accessors -StructType* LLVMContext::getStructType(bool isPacked) { - return StructType::get(isPacked); +/// getInlineAsmDiagnosticContext - Return the diagnostic context set by +/// setInlineAsmDiagnosticHandler. +void *LLVMContext::getInlineAsmDiagnosticContext() const { + return pImpl->InlineAsmDiagContext; } -StructType* LLVMContext::getStructType(const std::vector& Params, - bool isPacked) { - return StructType::get(Params, isPacked); +void LLVMContext::emitError(const Twine &ErrorStr) { + emitError(0U, ErrorStr); } -StructType *LLVMContext::getStructType(const Type *type, ...) { - va_list ap; - std::vector StructFields; - va_start(ap, type); - while (type) { - StructFields.push_back(type); - type = va_arg(ap, llvm::Type*); +void LLVMContext::emitError(const Instruction *I, const Twine &ErrorStr) { + unsigned LocCookie = 0; + if (const MDNode *SrcLoc = I->getMetadata("srcloc")) { + if (SrcLoc->getNumOperands() != 0) + if (const ConstantInt *CI = dyn_cast(SrcLoc->getOperand(0))) + LocCookie = CI->getZExtValue(); } - return StructType::get(StructFields); + return emitError(LocCookie, ErrorStr); } -// ArrayType accessors -ArrayType* LLVMContext::getArrayType(const Type* ElementType, - uint64_t NumElements) { - return ArrayType::get(ElementType, NumElements); -} - -// PointerType accessors -PointerType* LLVMContext::getPointerType(const Type* ElementType, - unsigned AddressSpace) { - return PointerType::get(ElementType, AddressSpace); -} +void LLVMContext::emitError(unsigned LocCookie, const Twine &ErrorStr) { + // If there is no error handler installed, just print the error and exit. + if (pImpl->InlineAsmDiagHandler == 0) { + errs() << "error: " << ErrorStr << "\n"; + exit(1); + } -PointerType* LLVMContext::getPointerTypeUnqual(const Type* ElementType) { - return PointerType::getUnqual(ElementType); -} - -// VectorType accessors -VectorType* LLVMContext::getVectorType(const Type* ElementType, - unsigned NumElements) { - return VectorType::get(ElementType, NumElements); -} + // If we do have an error handler, we can report the error and keep going. + SMDiagnostic Diag("", SourceMgr::DK_Error, ErrorStr.str()); -VectorType* LLVMContext::getVectorTypeInteger(const VectorType* VTy) { - return VectorType::getInteger(VTy); + pImpl->InlineAsmDiagHandler(Diag, pImpl->InlineAsmDiagContext, LocCookie); } -VectorType* LLVMContext::getVectorTypeExtendedElement(const VectorType* VTy) { - return VectorType::getExtendedElementVectorType(VTy); -} +//===----------------------------------------------------------------------===// +// Metadata Kind Uniquing +//===----------------------------------------------------------------------===// -VectorType* LLVMContext::getVectorTypeTruncatedElement(const VectorType* VTy) { - return VectorType::getTruncatedElementVectorType(VTy); -} +#ifndef NDEBUG +/// isValidName - Return true if Name is a valid custom metadata handler name. +static bool isValidName(StringRef MDName) { + if (MDName.empty()) + return false; + + if (!std::isalpha(MDName[0])) + return false; -const Type* LLVMContext::makeCmpResultType(const Type* opnd_type) { - if (const VectorType* vt = dyn_cast(opnd_type)) { - return getVectorType(Type::Int1Ty, vt->getNumElements()); + for (StringRef::iterator I = MDName.begin() + 1, E = MDName.end(); I != E; + ++I) { + if (!std::isalnum(*I) && *I != '_' && *I != '-' && *I != '.') + return false; } - return Type::Int1Ty; + return true; } +#endif -void LLVMContext::erase(MDString *M) { - pImpl->erase(M); -} +/// getMDKindID - Return a unique non-zero ID for the specified metadata kind. +unsigned LLVMContext::getMDKindID(StringRef Name) const { + assert(isValidName(Name) && "Invalid MDNode name"); -void LLVMContext::erase(MDNode *M) { - pImpl->erase(M); + // If this is new, assign it its ID. + return + pImpl->CustomMDKindNames.GetOrCreateValue( + Name, pImpl->CustomMDKindNames.size()).second; } -void LLVMContext::erase(ConstantAggregateZero *Z) { - pImpl->erase(Z); +/// getHandlerNames - Populate client supplied smallvector using custome +/// metadata name and ID. +void LLVMContext::getMDKindNames(SmallVectorImpl &Names) const { + Names.resize(pImpl->CustomMDKindNames.size()); + for (StringMap::const_iterator I = pImpl->CustomMDKindNames.begin(), + E = pImpl->CustomMDKindNames.end(); I != E; ++I) + Names[I->second] = I->first(); }