e38986eb3df9b2f55eb8f46ebab4b82b0196e1c2
[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 getConstantPointerNull(cast<PointerType>(Ty));
56   case Type::StructTyID:
57   case Type::ArrayTyID:
58   case Type::VectorTyID:
59     return getConstantAggregateZero(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 // UndefValue accessors.
79 UndefValue* LLVMContext::getUndef(const Type* Ty) {
80   return UndefValue::get(Ty);
81 }
82
83 // ConstantInt accessors.
84 ConstantInt* LLVMContext::getTrue() {
85   assert(this && "Context not initialized!");
86   assert(pImpl && "Context not initialized!");
87   return pImpl->getTrue();
88 }
89
90 ConstantInt* LLVMContext::getFalse() {
91   assert(this && "Context not initialized!");
92   assert(pImpl && "Context not initialized!");
93   return pImpl->getFalse();
94 }
95
96 // ConstantPointerNull accessors.
97 ConstantPointerNull* LLVMContext::getConstantPointerNull(const PointerType* T) {
98   return ConstantPointerNull::get(T);
99 }
100
101 // ConstantAggregateZero accessors.
102 ConstantAggregateZero* LLVMContext::getConstantAggregateZero(const Type* Ty) {
103   return pImpl->getConstantAggregateZero(Ty);
104 }
105
106 // MDNode accessors
107 MDNode* LLVMContext::getMDNode(Value* const* Vals, unsigned NumVals) {
108   return pImpl->getMDNode(Vals, NumVals);
109 }
110
111 // MDString accessors
112 MDString* LLVMContext::getMDString(const StringRef &Str) {
113   return pImpl->getMDString(Str.data(), Str.size());
114 }
115
116 // FunctionType accessors
117 FunctionType* LLVMContext::getFunctionType(const Type* Result, bool isVarArg) {
118   return FunctionType::get(Result, isVarArg);
119 }
120
121 FunctionType* LLVMContext::getFunctionType(const Type* Result,
122                                          const std::vector<const Type*>& Params,
123                                          bool isVarArg) {
124   return FunctionType::get(Result, Params, isVarArg);
125 }
126                                 
127 // IntegerType accessors
128 const IntegerType* LLVMContext::getIntegerType(unsigned NumBits) {
129   return IntegerType::get(NumBits);
130 }
131   
132 // OpaqueType accessors
133 OpaqueType* LLVMContext::getOpaqueType() {
134   return OpaqueType::get();
135 }
136
137 // StructType accessors
138 StructType* LLVMContext::getStructType(bool isPacked) {
139   return StructType::get(isPacked);
140 }
141
142 StructType* LLVMContext::getStructType(const std::vector<const Type*>& Params,
143                                        bool isPacked) {
144   return StructType::get(Params, isPacked);
145 }
146
147 StructType *LLVMContext::getStructType(const Type *type, ...) {
148   va_list ap;
149   std::vector<const llvm::Type*> StructFields;
150   va_start(ap, type);
151   while (type) {
152     StructFields.push_back(type);
153     type = va_arg(ap, llvm::Type*);
154   }
155   return StructType::get(StructFields);
156 }
157
158 // ArrayType accessors
159 ArrayType* LLVMContext::getArrayType(const Type* ElementType,
160                                      uint64_t NumElements) {
161   return ArrayType::get(ElementType, NumElements);
162 }
163   
164 // PointerType accessors
165 PointerType* LLVMContext::getPointerType(const Type* ElementType,
166                                          unsigned AddressSpace) {
167   return PointerType::get(ElementType, AddressSpace);
168 }
169
170 PointerType* LLVMContext::getPointerTypeUnqual(const Type* ElementType) {
171   return PointerType::getUnqual(ElementType);
172 }
173   
174 // VectorType accessors
175 VectorType* LLVMContext::getVectorType(const Type* ElementType,
176                                        unsigned NumElements) {
177   return VectorType::get(ElementType, NumElements);
178 }
179
180 VectorType* LLVMContext::getVectorTypeInteger(const VectorType* VTy) {
181   return VectorType::getInteger(VTy);  
182 }
183
184 VectorType* LLVMContext::getVectorTypeExtendedElement(const VectorType* VTy) {
185   return VectorType::getExtendedElementVectorType(VTy);
186 }
187
188 VectorType* LLVMContext::getVectorTypeTruncatedElement(const VectorType* VTy) {
189   return VectorType::getTruncatedElementVectorType(VTy);
190 }
191
192 const Type* LLVMContext::makeCmpResultType(const Type* opnd_type) {
193   if (const VectorType* vt = dyn_cast<const VectorType>(opnd_type)) {
194     return getVectorType(Type::Int1Ty, vt->getNumElements());
195   }
196   return Type::Int1Ty;
197 }
198
199 void LLVMContext::erase(MDString *M) {
200   pImpl->erase(M);
201 }
202
203 void LLVMContext::erase(MDNode *M) {
204   pImpl->erase(M);
205 }
206
207 void LLVMContext::erase(ConstantAggregateZero *Z) {
208   pImpl->erase(Z);
209 }