Home | History | Annotate | Download | only in VMCore
      1 //===-- LLVMContext.cpp - Implement LLVMContext -----------------------===//
      2 //
      3 //                     The LLVM Compiler Infrastructure
      4 //
      5 // This file is distributed under the University of Illinois Open Source
      6 // License. See LICENSE.TXT for details.
      7 //
      8 //===----------------------------------------------------------------------===//
      9 //
     10 //  This file implements LLVMContext, as a wrapper around the opaque
     11 //  class LLVMContextImpl.
     12 //
     13 //===----------------------------------------------------------------------===//
     14 
     15 #include "llvm/LLVMContext.h"
     16 #include "llvm/Metadata.h"
     17 #include "llvm/Constants.h"
     18 #include "llvm/Instruction.h"
     19 #include "llvm/Support/ManagedStatic.h"
     20 #include "llvm/Support/SourceMgr.h"
     21 #include "LLVMContextImpl.h"
     22 #include <cctype>
     23 using namespace llvm;
     24 
     25 static ManagedStatic<LLVMContext> GlobalContext;
     26 
     27 LLVMContext& llvm::getGlobalContext() {
     28   return *GlobalContext;
     29 }
     30 
     31 LLVMContext::LLVMContext() : pImpl(new LLVMContextImpl(*this)) {
     32   // Create the fixed metadata kinds. This is done in the same order as the
     33   // MD_* enum values so that they correspond.
     34 
     35   // Create the 'dbg' metadata kind.
     36   unsigned DbgID = getMDKindID("dbg");
     37   assert(DbgID == MD_dbg && "dbg kind id drifted"); (void)DbgID;
     38 
     39   // Create the 'tbaa' metadata kind.
     40   unsigned TBAAID = getMDKindID("tbaa");
     41   assert(TBAAID == MD_tbaa && "tbaa kind id drifted"); (void)TBAAID;
     42 
     43   // Create the 'prof' metadata kind.
     44   unsigned ProfID = getMDKindID("prof");
     45   assert(ProfID == MD_prof && "prof kind id drifted"); (void)ProfID;
     46 }
     47 LLVMContext::~LLVMContext() { delete pImpl; }
     48 
     49 void LLVMContext::addModule(Module *M) {
     50   pImpl->OwnedModules.insert(M);
     51 }
     52 
     53 void LLVMContext::removeModule(Module *M) {
     54   pImpl->OwnedModules.erase(M);
     55 }
     56 
     57 //===----------------------------------------------------------------------===//
     58 // Recoverable Backend Errors
     59 //===----------------------------------------------------------------------===//
     60 
     61 void LLVMContext::
     62 setInlineAsmDiagnosticHandler(InlineAsmDiagHandlerTy DiagHandler,
     63                               void *DiagContext) {
     64   pImpl->InlineAsmDiagHandler = DiagHandler;
     65   pImpl->InlineAsmDiagContext = DiagContext;
     66 }
     67 
     68 /// getInlineAsmDiagnosticHandler - Return the diagnostic handler set by
     69 /// setInlineAsmDiagnosticHandler.
     70 LLVMContext::InlineAsmDiagHandlerTy
     71 LLVMContext::getInlineAsmDiagnosticHandler() const {
     72   return pImpl->InlineAsmDiagHandler;
     73 }
     74 
     75 /// getInlineAsmDiagnosticContext - Return the diagnostic context set by
     76 /// setInlineAsmDiagnosticHandler.
     77 void *LLVMContext::getInlineAsmDiagnosticContext() const {
     78   return pImpl->InlineAsmDiagContext;
     79 }
     80 
     81 void LLVMContext::emitError(StringRef ErrorStr) {
     82   emitError(0U, ErrorStr);
     83 }
     84 
     85 void LLVMContext::emitError(const Instruction *I, StringRef ErrorStr) {
     86   unsigned LocCookie = 0;
     87   if (const MDNode *SrcLoc = I->getMetadata("srcloc")) {
     88     if (SrcLoc->getNumOperands() != 0)
     89       if (const ConstantInt *CI = dyn_cast<ConstantInt>(SrcLoc->getOperand(0)))
     90         LocCookie = CI->getZExtValue();
     91   }
     92   return emitError(LocCookie, ErrorStr);
     93 }
     94 
     95 void LLVMContext::emitError(unsigned LocCookie, StringRef ErrorStr) {
     96   // If there is no error handler installed, just print the error and exit.
     97   if (pImpl->InlineAsmDiagHandler == 0) {
     98     errs() << "error: " << ErrorStr << "\n";
     99     exit(1);
    100   }
    101 
    102   // If we do have an error handler, we can report the error and keep going.
    103   SMDiagnostic Diag("", "error: " + ErrorStr.str());
    104 
    105   pImpl->InlineAsmDiagHandler(Diag, pImpl->InlineAsmDiagContext, LocCookie);
    106 }
    107 
    108 //===----------------------------------------------------------------------===//
    109 // Metadata Kind Uniquing
    110 //===----------------------------------------------------------------------===//
    111 
    112 #ifndef NDEBUG
    113 /// isValidName - Return true if Name is a valid custom metadata handler name.
    114 static bool isValidName(StringRef MDName) {
    115   if (MDName.empty())
    116     return false;
    117 
    118   if (!std::isalpha(MDName[0]))
    119     return false;
    120 
    121   for (StringRef::iterator I = MDName.begin() + 1, E = MDName.end(); I != E;
    122        ++I) {
    123     if (!std::isalnum(*I) && *I != '_' && *I != '-' && *I != '.')
    124       return false;
    125   }
    126   return true;
    127 }
    128 #endif
    129 
    130 /// getMDKindID - Return a unique non-zero ID for the specified metadata kind.
    131 unsigned LLVMContext::getMDKindID(StringRef Name) const {
    132   assert(isValidName(Name) && "Invalid MDNode name");
    133 
    134   // If this is new, assign it its ID.
    135   return
    136     pImpl->CustomMDKindNames.GetOrCreateValue(
    137       Name, pImpl->CustomMDKindNames.size()).second;
    138 }
    139 
    140 /// getHandlerNames - Populate client supplied smallvector using custome
    141 /// metadata name and ID.
    142 void LLVMContext::getMDKindNames(SmallVectorImpl<StringRef> &Names) const {
    143   Names.resize(pImpl->CustomMDKindNames.size());
    144   for (StringMap<unsigned>::const_iterator I = pImpl->CustomMDKindNames.begin(),
    145        E = pImpl->CustomMDKindNames.end(); I != E; ++I)
    146     Names[I->second] = I->first();
    147 }
    148