Move more code back to 2.5 APIs.
[oota-llvm.git] / lib / VMCore / LLVMContext.cpp
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/Constants.h"
17 #include "llvm/DerivedTypes.h"
18 #include "llvm/Instruction.h"
19 #include "llvm/Metadata.h"
20 #include "llvm/Support/ManagedStatic.h"
21 #include "LLVMContextImpl.h"
22 #include <cstdarg>
23
24 using namespace llvm;
25
26 static ManagedStatic<LLVMContext> GlobalContext;
27
28 LLVMContext& llvm::getGlobalContext() {
29   return *GlobalContext;
30 }
31
32 LLVMContext::LLVMContext() : pImpl(new LLVMContextImpl(*this)) { }
33 LLVMContext::~LLVMContext() { delete pImpl; }
34
35 // Constant accessors
36
37 // Constructor to create a '0' constant of arbitrary type...
38 static const uint64_t zero[2] = {0, 0};
39 Constant* LLVMContext::getNullValue(const Type* Ty) {
40   switch (Ty->getTypeID()) {
41   case Type::IntegerTyID:
42     return ConstantInt::get(Ty, 0);
43   case Type::FloatTyID:
44     return ConstantFP::get(Ty->getContext(), APFloat(APInt(32, 0)));
45   case Type::DoubleTyID:
46     return ConstantFP::get(Ty->getContext(), APFloat(APInt(64, 0)));
47   case Type::X86_FP80TyID:
48     return ConstantFP::get(Ty->getContext(), APFloat(APInt(80, 2, zero)));
49   case Type::FP128TyID:
50     return ConstantFP::get(Ty->getContext(),
51                            APFloat(APInt(128, 2, zero), true));
52   case Type::PPC_FP128TyID:
53     return ConstantFP::get(Ty->getContext(), APFloat(APInt(128, 2, zero)));
54   case Type::PointerTyID:
55     return ConstantPointerNull::get(cast<PointerType>(Ty));
56   case Type::StructTyID:
57   case Type::ArrayTyID:
58   case Type::VectorTyID:
59     return ConstantAggregateZero::get(Ty);
60   default:
61     // Function, Label, or Opaque type?
62     assert(!"Cannot create a null constant of that type!");
63     return 0;
64   }
65 }
66
67 Constant* LLVMContext::getAllOnesValue(const Type* Ty) {
68   if (const IntegerType* ITy = dyn_cast<IntegerType>(Ty))
69     return ConstantInt::get(*this, APInt::getAllOnesValue(ITy->getBitWidth()));
70   
71   std::vector<Constant*> Elts;
72   const VectorType* VTy = cast<VectorType>(Ty);
73   Elts.resize(VTy->getNumElements(), getAllOnesValue(VTy->getElementType()));
74   assert(Elts[0] && "Not a vector integer type!");
75   return cast<ConstantVector>(ConstantVector::get(Elts));
76 }
77
78 // ConstantInt accessors.
79 ConstantInt* LLVMContext::getTrue() {
80   assert(this && "Context not initialized!");
81   assert(pImpl && "Context not initialized!");
82   return pImpl->getTrue();
83 }
84
85 ConstantInt* LLVMContext::getFalse() {
86   assert(this && "Context not initialized!");
87   assert(pImpl && "Context not initialized!");
88   return pImpl->getFalse();
89 }
90
91 // MDNode accessors
92 MDNode* LLVMContext::getMDNode(Value* const* Vals, unsigned NumVals) {
93   return pImpl->getMDNode(Vals, NumVals);
94 }
95
96 // MDString accessors
97 MDString* LLVMContext::getMDString(const StringRef &Str) {
98   return pImpl->getMDString(Str.data(), Str.size());
99 }
100
101 void LLVMContext::erase(MDString *M) {
102   pImpl->erase(M);
103 }
104
105 void LLVMContext::erase(MDNode *M) {
106   pImpl->erase(M);
107 }