Anders Carlsson | 3b1d57b | 2008-01-26 01:36:00 +0000 | [diff] [blame] | 1 | //===--- CGExprConstant.cpp - Emit LLVM Code from Constant Expressions ----===// |
| 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 contains code to emit Constant Expr nodes as LLVM code. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "CodeGenFunction.h" |
| 15 | #include "CodeGenModule.h" |
Daniel Dunbar | af2f62c | 2008-08-13 00:59:25 +0000 | [diff] [blame] | 16 | #include "CGObjCRuntime.h" |
Chris Lattner | f6b24ea | 2008-10-06 05:59:01 +0000 | [diff] [blame] | 17 | #include "clang/AST/APValue.h" |
Daniel Dunbar | de7fb84 | 2008-08-11 05:00:27 +0000 | [diff] [blame] | 18 | #include "clang/AST/ASTContext.h" |
| 19 | #include "clang/AST/StmtVisitor.h" |
Chris Lattner | 1b63e4f | 2009-06-14 01:54:56 +0000 | [diff] [blame] | 20 | #include "clang/Basic/Builtins.h" |
Anders Carlsson | 3b1d57b | 2008-01-26 01:36:00 +0000 | [diff] [blame] | 21 | #include "llvm/Constants.h" |
| 22 | #include "llvm/Function.h" |
| 23 | #include "llvm/GlobalVariable.h" |
| 24 | #include "llvm/Support/Compiler.h" |
Eli Friedman | a823400 | 2008-05-30 10:24:46 +0000 | [diff] [blame] | 25 | #include "llvm/Target/TargetData.h" |
Anders Carlsson | 3b1d57b | 2008-01-26 01:36:00 +0000 | [diff] [blame] | 26 | using namespace clang; |
| 27 | using namespace CodeGen; |
| 28 | |
| 29 | namespace { |
Anders Carlsson | 84005b4 | 2008-01-26 04:30:23 +0000 | [diff] [blame] | 30 | class VISIBILITY_HIDDEN ConstExprEmitter : |
| 31 | public StmtVisitor<ConstExprEmitter, llvm::Constant*> { |
Anders Carlsson | 3b1d57b | 2008-01-26 01:36:00 +0000 | [diff] [blame] | 32 | CodeGenModule &CGM; |
Lauro Ramos Venancio | 8137335 | 2008-02-26 21:41:45 +0000 | [diff] [blame] | 33 | CodeGenFunction *CGF; |
Anders Carlsson | 3b1d57b | 2008-01-26 01:36:00 +0000 | [diff] [blame] | 34 | public: |
Lauro Ramos Venancio | 8137335 | 2008-02-26 21:41:45 +0000 | [diff] [blame] | 35 | ConstExprEmitter(CodeGenModule &cgm, CodeGenFunction *cgf) |
| 36 | : CGM(cgm), CGF(cgf) { |
Anders Carlsson | 3b1d57b | 2008-01-26 01:36:00 +0000 | [diff] [blame] | 37 | } |
| 38 | |
| 39 | //===--------------------------------------------------------------------===// |
| 40 | // Visitor Methods |
| 41 | //===--------------------------------------------------------------------===// |
| 42 | |
| 43 | llvm::Constant *VisitStmt(Stmt *S) { |
Anders Carlsson | 069880e | 2009-03-03 16:43:34 +0000 | [diff] [blame] | 44 | return 0; |
Anders Carlsson | 3b1d57b | 2008-01-26 01:36:00 +0000 | [diff] [blame] | 45 | } |
| 46 | |
| 47 | llvm::Constant *VisitParenExpr(ParenExpr *PE) { |
| 48 | return Visit(PE->getSubExpr()); |
| 49 | } |
Anders Carlsson | bcf6225 | 2008-11-22 02:34:39 +0000 | [diff] [blame] | 50 | |
Anders Carlsson | 3b1d57b | 2008-01-26 01:36:00 +0000 | [diff] [blame] | 51 | llvm::Constant *VisitCompoundLiteralExpr(CompoundLiteralExpr *E) { |
| 52 | return Visit(E->getInitializer()); |
| 53 | } |
| 54 | |
Argyrios Kyrtzidis | 0835a3c | 2008-08-18 23:01:59 +0000 | [diff] [blame] | 55 | llvm::Constant *VisitCastExpr(CastExpr* E) { |
Nuno Lopes | 81e51e2 | 2009-01-17 00:48:48 +0000 | [diff] [blame] | 56 | // GCC cast to union extension |
| 57 | if (E->getType()->isUnionType()) { |
| 58 | const llvm::Type *Ty = ConvertType(E->getType()); |
Anders Carlsson | e9352cc | 2009-04-08 04:48:15 +0000 | [diff] [blame] | 59 | Expr *SubExpr = E->getSubExpr(); |
| 60 | return EmitUnion(CGM.EmitConstantExpr(SubExpr, SubExpr->getType(), CGF), |
| 61 | Ty); |
Nuno Lopes | 81e51e2 | 2009-01-17 00:48:48 +0000 | [diff] [blame] | 62 | } |
Eli Friedman | 944af71 | 2009-04-30 07:03:22 +0000 | [diff] [blame] | 63 | // Explicit and implicit no-op casts |
| 64 | QualType Ty = E->getType(), SubTy = E->getSubExpr()->getType(); |
| 65 | if (CGM.getContext().hasSameUnqualifiedType(Ty, SubTy)) { |
Eli Friedman | ddd2b6e | 2009-02-22 07:29:04 +0000 | [diff] [blame] | 66 | return Visit(E->getSubExpr()); |
| 67 | } |
| 68 | return 0; |
Anders Carlsson | 3b1d57b | 2008-01-26 01:36:00 +0000 | [diff] [blame] | 69 | } |
Devang Patel | eae1560 | 2008-02-05 02:39:50 +0000 | [diff] [blame] | 70 | |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 71 | llvm::Constant *VisitCXXDefaultArgExpr(CXXDefaultArgExpr *DAE) { |
| 72 | return Visit(DAE->getExpr()); |
| 73 | } |
| 74 | |
Eli Friedman | a823400 | 2008-05-30 10:24:46 +0000 | [diff] [blame] | 75 | llvm::Constant *EmitArrayInitialization(InitListExpr *ILE) { |
Eli Friedman | 4d1c018 | 2008-02-21 17:45:41 +0000 | [diff] [blame] | 76 | std::vector<llvm::Constant*> Elts; |
Eli Friedman | a823400 | 2008-05-30 10:24:46 +0000 | [diff] [blame] | 77 | const llvm::ArrayType *AType = |
| 78 | cast<llvm::ArrayType>(ConvertType(ILE->getType())); |
Eli Friedman | 4d1c018 | 2008-02-21 17:45:41 +0000 | [diff] [blame] | 79 | unsigned NumInitElements = ILE->getNumInits(); |
| 80 | // FIXME: Check for wide strings |
Chris Lattner | eaf2bb8 | 2009-02-24 22:18:39 +0000 | [diff] [blame] | 81 | // FIXME: Check for NumInitElements exactly equal to 1?? |
| 82 | if (NumInitElements > 0 && |
| 83 | (isa<StringLiteral>(ILE->getInit(0)) || |
| 84 | isa<ObjCEncodeExpr>(ILE->getInit(0))) && |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 85 | ILE->getType()->getArrayElementTypeNoTypeQual()->isCharType()) |
Eli Friedman | 4d1c018 | 2008-02-21 17:45:41 +0000 | [diff] [blame] | 86 | return Visit(ILE->getInit(0)); |
Devang Patel | eae1560 | 2008-02-05 02:39:50 +0000 | [diff] [blame] | 87 | const llvm::Type *ElemTy = AType->getElementType(); |
| 88 | unsigned NumElements = AType->getNumElements(); |
| 89 | |
| 90 | // Initialising an array requires us to automatically |
| 91 | // initialise any elements that have not been initialised explicitly |
| 92 | unsigned NumInitableElts = std::min(NumInitElements, NumElements); |
| 93 | |
| 94 | // Copy initializer elements. |
| 95 | unsigned i = 0; |
Eli Friedman | a04a153 | 2008-05-30 19:58:50 +0000 | [diff] [blame] | 96 | bool RewriteType = false; |
Devang Patel | eae1560 | 2008-02-05 02:39:50 +0000 | [diff] [blame] | 97 | for (; i < NumInitableElts; ++i) { |
Anders Carlsson | e9352cc | 2009-04-08 04:48:15 +0000 | [diff] [blame] | 98 | Expr *Init = ILE->getInit(i); |
| 99 | llvm::Constant *C = CGM.EmitConstantExpr(Init, Init->getType(), CGF); |
Daniel Dunbar | d60f2fb | 2009-02-17 18:43:32 +0000 | [diff] [blame] | 100 | if (!C) |
| 101 | return 0; |
Eli Friedman | a04a153 | 2008-05-30 19:58:50 +0000 | [diff] [blame] | 102 | RewriteType |= (C->getType() != ElemTy); |
Devang Patel | eae1560 | 2008-02-05 02:39:50 +0000 | [diff] [blame] | 103 | Elts.push_back(C); |
| 104 | } |
Eli Friedman | a04a153 | 2008-05-30 19:58:50 +0000 | [diff] [blame] | 105 | |
Devang Patel | eae1560 | 2008-02-05 02:39:50 +0000 | [diff] [blame] | 106 | // Initialize remaining array elements. |
Eli Friedman | 0f59312 | 2009-04-13 21:47:26 +0000 | [diff] [blame] | 107 | // FIXME: This doesn't handle member pointers correctly! |
Devang Patel | eae1560 | 2008-02-05 02:39:50 +0000 | [diff] [blame] | 108 | for (; i < NumElements; ++i) |
| 109 | Elts.push_back(llvm::Constant::getNullValue(ElemTy)); |
| 110 | |
Eli Friedman | a04a153 | 2008-05-30 19:58:50 +0000 | [diff] [blame] | 111 | if (RewriteType) { |
| 112 | // FIXME: Try to avoid packing the array |
| 113 | std::vector<const llvm::Type*> Types; |
| 114 | for (unsigned i = 0; i < Elts.size(); ++i) |
| 115 | Types.push_back(Elts[i]->getType()); |
| 116 | const llvm::StructType *SType = llvm::StructType::get(Types, true); |
| 117 | return llvm::ConstantStruct::get(SType, Elts); |
| 118 | } |
| 119 | |
Devang Patel | eae1560 | 2008-02-05 02:39:50 +0000 | [diff] [blame] | 120 | return llvm::ConstantArray::get(AType, Elts); |
| 121 | } |
| 122 | |
Eli Friedman | a823400 | 2008-05-30 10:24:46 +0000 | [diff] [blame] | 123 | void InsertBitfieldIntoStruct(std::vector<llvm::Constant*>& Elts, |
| 124 | FieldDecl* Field, Expr* E) { |
| 125 | // Calculate the value to insert |
Anders Carlsson | e9352cc | 2009-04-08 04:48:15 +0000 | [diff] [blame] | 126 | llvm::Constant *C = CGM.EmitConstantExpr(E, Field->getType(), CGF); |
Daniel Dunbar | d60f2fb | 2009-02-17 18:43:32 +0000 | [diff] [blame] | 127 | if (!C) |
| 128 | return; |
| 129 | |
Eli Friedman | a823400 | 2008-05-30 10:24:46 +0000 | [diff] [blame] | 130 | llvm::ConstantInt *CI = dyn_cast<llvm::ConstantInt>(C); |
| 131 | if (!CI) { |
Daniel Dunbar | 488e993 | 2008-08-16 00:56:44 +0000 | [diff] [blame] | 132 | CGM.ErrorUnsupported(E, "bitfield initialization"); |
Eli Friedman | a823400 | 2008-05-30 10:24:46 +0000 | [diff] [blame] | 133 | return; |
| 134 | } |
| 135 | llvm::APInt V = CI->getValue(); |
Devang Patel | eae1560 | 2008-02-05 02:39:50 +0000 | [diff] [blame] | 136 | |
Eli Friedman | a823400 | 2008-05-30 10:24:46 +0000 | [diff] [blame] | 137 | // Calculate information about the relevant field |
| 138 | const llvm::Type* Ty = CI->getType(); |
Daniel Dunbar | 9ca8bb0 | 2008-08-10 07:00:24 +0000 | [diff] [blame] | 139 | const llvm::TargetData &TD = CGM.getTypes().getTargetData(); |
Duncan Sands | 9408c45 | 2009-05-09 07:08:47 +0000 | [diff] [blame] | 140 | unsigned size = TD.getTypeAllocSizeInBits(Ty); |
Eli Friedman | a823400 | 2008-05-30 10:24:46 +0000 | [diff] [blame] | 141 | unsigned fieldOffset = CGM.getTypes().getLLVMFieldNo(Field) * size; |
| 142 | CodeGenTypes::BitFieldInfo bitFieldInfo = |
| 143 | CGM.getTypes().getBitFieldInfo(Field); |
| 144 | fieldOffset += bitFieldInfo.Begin; |
| 145 | |
| 146 | // Find where to start the insertion |
| 147 | // FIXME: This is O(n^2) in the number of bit-fields! |
| 148 | // FIXME: This won't work if the struct isn't completely packed! |
| 149 | unsigned offset = 0, i = 0; |
| 150 | while (offset < (fieldOffset & -8)) |
Duncan Sands | 9408c45 | 2009-05-09 07:08:47 +0000 | [diff] [blame] | 151 | offset += TD.getTypeAllocSizeInBits(Elts[i++]->getType()); |
Daniel Dunbar | 9ca8bb0 | 2008-08-10 07:00:24 +0000 | [diff] [blame] | 152 | |
| 153 | // Advance over 0 sized elements (must terminate in bounds since |
| 154 | // the bitfield must have a size). |
Duncan Sands | 9408c45 | 2009-05-09 07:08:47 +0000 | [diff] [blame] | 155 | while (TD.getTypeAllocSizeInBits(Elts[i]->getType()) == 0) |
Daniel Dunbar | 9ca8bb0 | 2008-08-10 07:00:24 +0000 | [diff] [blame] | 156 | ++i; |
Eli Friedman | a823400 | 2008-05-30 10:24:46 +0000 | [diff] [blame] | 157 | |
Daniel Dunbar | 1f3846e | 2008-07-17 21:07:48 +0000 | [diff] [blame] | 158 | // Promote the size of V if necessary |
Mike Stump | f5408fe | 2009-05-16 07:57:57 +0000 | [diff] [blame] | 159 | // FIXME: This should never occur, but currently it can because initializer |
| 160 | // constants are cast to bool, and because clang is not enforcing bitfield |
| 161 | // width limits. |
Daniel Dunbar | 1f3846e | 2008-07-17 21:07:48 +0000 | [diff] [blame] | 162 | if (bitFieldInfo.Size > V.getBitWidth()) |
| 163 | V.zext(bitFieldInfo.Size); |
| 164 | |
Eli Friedman | a823400 | 2008-05-30 10:24:46 +0000 | [diff] [blame] | 165 | // Insert the bits into the struct |
| 166 | // FIXME: This algorthm is only correct on X86! |
| 167 | // FIXME: THis algorthm assumes bit-fields only have byte-size elements! |
| 168 | unsigned bitsToInsert = bitFieldInfo.Size; |
| 169 | unsigned curBits = std::min(8 - (fieldOffset & 7), bitsToInsert); |
| 170 | unsigned byte = V.getLoBits(curBits).getZExtValue() << (fieldOffset & 7); |
| 171 | do { |
| 172 | llvm::Constant* byteC = llvm::ConstantInt::get(llvm::Type::Int8Ty, byte); |
| 173 | Elts[i] = llvm::ConstantExpr::getOr(Elts[i], byteC); |
| 174 | ++i; |
| 175 | V = V.lshr(curBits); |
| 176 | bitsToInsert -= curBits; |
| 177 | |
| 178 | if (!bitsToInsert) |
| 179 | break; |
| 180 | |
| 181 | curBits = bitsToInsert > 8 ? 8 : bitsToInsert; |
| 182 | byte = V.getLoBits(curBits).getZExtValue(); |
| 183 | } while (true); |
| 184 | } |
| 185 | |
| 186 | llvm::Constant *EmitStructInitialization(InitListExpr *ILE) { |
| 187 | const llvm::StructType *SType = |
| 188 | cast<llvm::StructType>(ConvertType(ILE->getType())); |
Eli Friedman | 017cbdf | 2008-05-20 00:11:07 +0000 | [diff] [blame] | 189 | RecordDecl *RD = ILE->getType()->getAsRecordType()->getDecl(); |
Devang Patel | eae1560 | 2008-02-05 02:39:50 +0000 | [diff] [blame] | 190 | std::vector<llvm::Constant*> Elts; |
Devang Patel | eae1560 | 2008-02-05 02:39:50 +0000 | [diff] [blame] | 191 | |
Eli Friedman | 017cbdf | 2008-05-20 00:11:07 +0000 | [diff] [blame] | 192 | // Initialize the whole structure to zero. |
Eli Friedman | 0f59312 | 2009-04-13 21:47:26 +0000 | [diff] [blame] | 193 | // FIXME: This doesn't handle member pointers correctly! |
Eli Friedman | 017cbdf | 2008-05-20 00:11:07 +0000 | [diff] [blame] | 194 | for (unsigned i = 0; i < SType->getNumElements(); ++i) { |
Devang Patel | eae1560 | 2008-02-05 02:39:50 +0000 | [diff] [blame] | 195 | const llvm::Type *FieldTy = SType->getElementType(i); |
| 196 | Elts.push_back(llvm::Constant::getNullValue(FieldTy)); |
| 197 | } |
Eli Friedman | 017cbdf | 2008-05-20 00:11:07 +0000 | [diff] [blame] | 198 | |
| 199 | // Copy initializer elements. Skip padding fields. |
| 200 | unsigned EltNo = 0; // Element no in ILE |
Eli Friedman | a04a153 | 2008-05-30 19:58:50 +0000 | [diff] [blame] | 201 | bool RewriteType = false; |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 202 | for (RecordDecl::field_iterator Field = RD->field_begin(), |
| 203 | FieldEnd = RD->field_end(); |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 204 | EltNo < ILE->getNumInits() && Field != FieldEnd; ++Field) { |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 205 | if (Field->isBitField()) { |
Eli Friedman | 4c4c527 | 2009-06-14 21:41:37 +0000 | [diff] [blame] | 206 | if (!Field->getIdentifier()) |
| 207 | continue; |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 208 | InsertBitfieldIntoStruct(Elts, *Field, ILE->getInit(EltNo)); |
Eli Friedman | 017cbdf | 2008-05-20 00:11:07 +0000 | [diff] [blame] | 209 | } else { |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 210 | unsigned FieldNo = CGM.getTypes().getLLVMFieldNo(*Field); |
Anders Carlsson | e9352cc | 2009-04-08 04:48:15 +0000 | [diff] [blame] | 211 | llvm::Constant *C = CGM.EmitConstantExpr(ILE->getInit(EltNo), |
| 212 | Field->getType(), CGF); |
Daniel Dunbar | d60f2fb | 2009-02-17 18:43:32 +0000 | [diff] [blame] | 213 | if (!C) return 0; |
Eli Friedman | a04a153 | 2008-05-30 19:58:50 +0000 | [diff] [blame] | 214 | RewriteType |= (C->getType() != Elts[FieldNo]->getType()); |
| 215 | Elts[FieldNo] = C; |
Eli Friedman | 017cbdf | 2008-05-20 00:11:07 +0000 | [diff] [blame] | 216 | } |
| 217 | EltNo++; |
| 218 | } |
| 219 | |
Eli Friedman | a04a153 | 2008-05-30 19:58:50 +0000 | [diff] [blame] | 220 | if (RewriteType) { |
| 221 | // FIXME: Make this work for non-packed structs |
| 222 | assert(SType->isPacked() && "Cannot recreate unpacked structs"); |
| 223 | std::vector<const llvm::Type*> Types; |
| 224 | for (unsigned i = 0; i < Elts.size(); ++i) |
| 225 | Types.push_back(Elts[i]->getType()); |
| 226 | SType = llvm::StructType::get(Types, true); |
| 227 | } |
| 228 | |
Devang Patel | eae1560 | 2008-02-05 02:39:50 +0000 | [diff] [blame] | 229 | return llvm::ConstantStruct::get(SType, Elts); |
| 230 | } |
| 231 | |
Nuno Lopes | 81e51e2 | 2009-01-17 00:48:48 +0000 | [diff] [blame] | 232 | llvm::Constant *EmitUnion(llvm::Constant *C, const llvm::Type *Ty) { |
Daniel Dunbar | d60f2fb | 2009-02-17 18:43:32 +0000 | [diff] [blame] | 233 | if (!C) |
| 234 | return 0; |
| 235 | |
Nuno Lopes | 81e51e2 | 2009-01-17 00:48:48 +0000 | [diff] [blame] | 236 | // Build a struct with the union sub-element as the first member, |
| 237 | // and padded to the appropriate size |
| 238 | std::vector<llvm::Constant*> Elts; |
| 239 | std::vector<const llvm::Type*> Types; |
| 240 | Elts.push_back(C); |
| 241 | Types.push_back(C->getType()); |
Duncan Sands | 9408c45 | 2009-05-09 07:08:47 +0000 | [diff] [blame] | 242 | unsigned CurSize = CGM.getTargetData().getTypeAllocSize(C->getType()); |
| 243 | unsigned TotalSize = CGM.getTargetData().getTypeAllocSize(Ty); |
Nuno Lopes | 81e51e2 | 2009-01-17 00:48:48 +0000 | [diff] [blame] | 244 | while (CurSize < TotalSize) { |
| 245 | Elts.push_back(llvm::Constant::getNullValue(llvm::Type::Int8Ty)); |
| 246 | Types.push_back(llvm::Type::Int8Ty); |
| 247 | CurSize++; |
| 248 | } |
| 249 | |
| 250 | // This always generates a packed struct |
| 251 | // FIXME: Try to generate an unpacked struct when we can |
| 252 | llvm::StructType* STy = llvm::StructType::get(Types, true); |
| 253 | return llvm::ConstantStruct::get(STy, Elts); |
| 254 | } |
| 255 | |
Eli Friedman | a823400 | 2008-05-30 10:24:46 +0000 | [diff] [blame] | 256 | llvm::Constant *EmitUnionInitialization(InitListExpr *ILE) { |
Eli Friedman | a823400 | 2008-05-30 10:24:46 +0000 | [diff] [blame] | 257 | const llvm::Type *Ty = ConvertType(ILE->getType()); |
| 258 | |
Douglas Gregor | 0bb7689 | 2009-01-29 16:53:55 +0000 | [diff] [blame] | 259 | FieldDecl* curField = ILE->getInitializedFieldInUnion(); |
| 260 | if (!curField) { |
Douglas Gregor | 0bb7689 | 2009-01-29 16:53:55 +0000 | [diff] [blame] | 261 | // There's no field to initialize, so value-initialize the union. |
| 262 | #ifndef NDEBUG |
| 263 | // Make sure that it's really an empty and not a failure of |
| 264 | // semantic analysis. |
Douglas Gregor | 71d8174 | 2009-02-11 16:17:49 +0000 | [diff] [blame] | 265 | RecordDecl *RD = ILE->getType()->getAsRecordType()->getDecl(); |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 266 | for (RecordDecl::field_iterator Field = RD->field_begin(), |
| 267 | FieldEnd = RD->field_end(); |
Douglas Gregor | 0bb7689 | 2009-01-29 16:53:55 +0000 | [diff] [blame] | 268 | Field != FieldEnd; ++Field) |
| 269 | assert(Field->isUnnamedBitfield() && "Only unnamed bitfields allowed"); |
| 270 | #endif |
Eli Friedman | a823400 | 2008-05-30 10:24:46 +0000 | [diff] [blame] | 271 | return llvm::Constant::getNullValue(Ty); |
Douglas Gregor | 0bb7689 | 2009-01-29 16:53:55 +0000 | [diff] [blame] | 272 | } |
Eli Friedman | a823400 | 2008-05-30 10:24:46 +0000 | [diff] [blame] | 273 | |
| 274 | if (curField->isBitField()) { |
| 275 | // Create a dummy struct for bit-field insertion |
Duncan Sands | 9408c45 | 2009-05-09 07:08:47 +0000 | [diff] [blame] | 276 | unsigned NumElts = CGM.getTargetData().getTypeAllocSize(Ty); |
Eli Friedman | a823400 | 2008-05-30 10:24:46 +0000 | [diff] [blame] | 277 | llvm::Constant* NV = llvm::Constant::getNullValue(llvm::Type::Int8Ty); |
| 278 | std::vector<llvm::Constant*> Elts(NumElts, NV); |
| 279 | |
| 280 | InsertBitfieldIntoStruct(Elts, curField, ILE->getInit(0)); |
| 281 | const llvm::ArrayType *RetTy = |
| 282 | llvm::ArrayType::get(NV->getType(), NumElts); |
| 283 | return llvm::ConstantArray::get(RetTy, Elts); |
| 284 | } |
| 285 | |
Eli Friedman | 0f59312 | 2009-04-13 21:47:26 +0000 | [diff] [blame] | 286 | llvm::Constant *InitElem; |
| 287 | if (ILE->getNumInits() > 0) { |
| 288 | Expr *Init = ILE->getInit(0); |
| 289 | InitElem = CGM.EmitConstantExpr(Init, Init->getType(), CGF); |
| 290 | } else { |
| 291 | InitElem = CGM.EmitNullConstant(curField->getType()); |
| 292 | } |
| 293 | return EmitUnion(InitElem, Ty); |
Eli Friedman | a823400 | 2008-05-30 10:24:46 +0000 | [diff] [blame] | 294 | } |
| 295 | |
| 296 | llvm::Constant *EmitVectorInitialization(InitListExpr *ILE) { |
| 297 | const llvm::VectorType *VType = |
| 298 | cast<llvm::VectorType>(ConvertType(ILE->getType())); |
Eli Friedman | cfb313b | 2008-05-30 12:02:14 +0000 | [diff] [blame] | 299 | const llvm::Type *ElemTy = VType->getElementType(); |
| 300 | std::vector<llvm::Constant*> Elts; |
Devang Patel | eae1560 | 2008-02-05 02:39:50 +0000 | [diff] [blame] | 301 | unsigned NumElements = VType->getNumElements(); |
Eli Friedman | cfb313b | 2008-05-30 12:02:14 +0000 | [diff] [blame] | 302 | unsigned NumInitElements = ILE->getNumInits(); |
Devang Patel | eae1560 | 2008-02-05 02:39:50 +0000 | [diff] [blame] | 303 | |
Eli Friedman | cfb313b | 2008-05-30 12:02:14 +0000 | [diff] [blame] | 304 | unsigned NumInitableElts = std::min(NumInitElements, NumElements); |
| 305 | |
Devang Patel | eae1560 | 2008-02-05 02:39:50 +0000 | [diff] [blame] | 306 | // Copy initializer elements. |
| 307 | unsigned i = 0; |
Eli Friedman | cfb313b | 2008-05-30 12:02:14 +0000 | [diff] [blame] | 308 | for (; i < NumInitableElts; ++i) { |
Anders Carlsson | e9352cc | 2009-04-08 04:48:15 +0000 | [diff] [blame] | 309 | Expr *Init = ILE->getInit(i); |
| 310 | llvm::Constant *C = CGM.EmitConstantExpr(Init, Init->getType(), CGF); |
Daniel Dunbar | d60f2fb | 2009-02-17 18:43:32 +0000 | [diff] [blame] | 311 | if (!C) |
| 312 | return 0; |
Devang Patel | eae1560 | 2008-02-05 02:39:50 +0000 | [diff] [blame] | 313 | Elts.push_back(C); |
| 314 | } |
| 315 | |
Eli Friedman | cfb313b | 2008-05-30 12:02:14 +0000 | [diff] [blame] | 316 | for (; i < NumElements; ++i) |
| 317 | Elts.push_back(llvm::Constant::getNullValue(ElemTy)); |
| 318 | |
Devang Patel | eae1560 | 2008-02-05 02:39:50 +0000 | [diff] [blame] | 319 | return llvm::ConstantVector::get(VType, Elts); |
| 320 | } |
Anders Carlsson | 7d801ea | 2009-01-30 06:13:25 +0000 | [diff] [blame] | 321 | |
| 322 | llvm::Constant *VisitImplicitValueInitExpr(ImplicitValueInitExpr* E) { |
Eli Friedman | 0f59312 | 2009-04-13 21:47:26 +0000 | [diff] [blame] | 323 | return CGM.EmitNullConstant(E->getType()); |
Anders Carlsson | 7d801ea | 2009-01-30 06:13:25 +0000 | [diff] [blame] | 324 | } |
| 325 | |
Anders Carlsson | 3b1d57b | 2008-01-26 01:36:00 +0000 | [diff] [blame] | 326 | llvm::Constant *VisitInitListExpr(InitListExpr *ILE) { |
Eli Friedman | a823400 | 2008-05-30 10:24:46 +0000 | [diff] [blame] | 327 | if (ILE->getType()->isScalarType()) { |
| 328 | // We have a scalar in braces. Just use the first element. |
Anders Carlsson | e9352cc | 2009-04-08 04:48:15 +0000 | [diff] [blame] | 329 | if (ILE->getNumInits() > 0) { |
| 330 | Expr *Init = ILE->getInit(0); |
| 331 | return CGM.EmitConstantExpr(Init, Init->getType(), CGF); |
| 332 | } |
Eli Friedman | 0f59312 | 2009-04-13 21:47:26 +0000 | [diff] [blame] | 333 | return CGM.EmitNullConstant(ILE->getType()); |
Anders Carlsson | 3b1d57b | 2008-01-26 01:36:00 +0000 | [diff] [blame] | 334 | } |
Chris Lattner | be20bb5 | 2008-10-26 23:53:12 +0000 | [diff] [blame] | 335 | |
Eli Friedman | a823400 | 2008-05-30 10:24:46 +0000 | [diff] [blame] | 336 | if (ILE->getType()->isArrayType()) |
| 337 | return EmitArrayInitialization(ILE); |
Devang Patel | 47fb697 | 2008-01-29 23:23:18 +0000 | [diff] [blame] | 338 | |
Eli Friedman | a823400 | 2008-05-30 10:24:46 +0000 | [diff] [blame] | 339 | if (ILE->getType()->isStructureType()) |
| 340 | return EmitStructInitialization(ILE); |
| 341 | |
| 342 | if (ILE->getType()->isUnionType()) |
| 343 | return EmitUnionInitialization(ILE); |
| 344 | |
| 345 | if (ILE->getType()->isVectorType()) |
| 346 | return EmitVectorInitialization(ILE); |
| 347 | |
Devang Patel | eae1560 | 2008-02-05 02:39:50 +0000 | [diff] [blame] | 348 | assert(0 && "Unable to handle InitListExpr"); |
Chris Lattner | 2b94fe3 | 2008-03-01 08:45:05 +0000 | [diff] [blame] | 349 | // Get rid of control reaches end of void function warning. |
| 350 | // Not reached. |
| 351 | return 0; |
Anders Carlsson | 3b1d57b | 2008-01-26 01:36:00 +0000 | [diff] [blame] | 352 | } |
Eli Friedman | 3b707e7 | 2008-02-21 17:57:49 +0000 | [diff] [blame] | 353 | |
Anders Carlsson | 3b1d57b | 2008-01-26 01:36:00 +0000 | [diff] [blame] | 354 | llvm::Constant *VisitStringLiteral(StringLiteral *E) { |
Chris Lattner | a7ad98f | 2008-02-11 00:02:17 +0000 | [diff] [blame] | 355 | assert(!E->getType()->isPointerType() && "Strings are always arrays"); |
Anders Carlsson | 3b1d57b | 2008-01-26 01:36:00 +0000 | [diff] [blame] | 356 | |
Chris Lattner | eaf2bb8 | 2009-02-24 22:18:39 +0000 | [diff] [blame] | 357 | // This must be a string initializing an array in a static initializer. |
| 358 | // Don't emit it as the address of the string, emit the string data itself |
| 359 | // as an inline array. |
Daniel Dunbar | 6143293 | 2008-08-13 23:20:05 +0000 | [diff] [blame] | 360 | return llvm::ConstantArray::get(CGM.GetStringForStringLiteral(E), false); |
Anders Carlsson | 3b1d57b | 2008-01-26 01:36:00 +0000 | [diff] [blame] | 361 | } |
| 362 | |
Chris Lattner | eaf2bb8 | 2009-02-24 22:18:39 +0000 | [diff] [blame] | 363 | llvm::Constant *VisitObjCEncodeExpr(ObjCEncodeExpr *E) { |
| 364 | // This must be an @encode initializing an array in a static initializer. |
| 365 | // Don't emit it as the address of the string, emit the string data itself |
| 366 | // as an inline array. |
| 367 | std::string Str; |
| 368 | CGM.getContext().getObjCEncodingForType(E->getEncodedType(), Str); |
| 369 | const ConstantArrayType *CAT = cast<ConstantArrayType>(E->getType()); |
| 370 | |
| 371 | // Resize the string to the right size, adding zeros at the end, or |
| 372 | // truncating as needed. |
| 373 | Str.resize(CAT->getSize().getZExtValue(), '\0'); |
| 374 | return llvm::ConstantArray::get(Str, false); |
| 375 | } |
| 376 | |
Eli Friedman | bc5ed6e | 2008-05-29 11:22:45 +0000 | [diff] [blame] | 377 | llvm::Constant *VisitUnaryExtension(const UnaryOperator *E) { |
| 378 | return Visit(E->getSubExpr()); |
| 379 | } |
Mike Stump | b83d287 | 2009-02-19 22:01:56 +0000 | [diff] [blame] | 380 | |
Anders Carlsson | 3b1d57b | 2008-01-26 01:36:00 +0000 | [diff] [blame] | 381 | // Utility methods |
| 382 | const llvm::Type *ConvertType(QualType T) { |
| 383 | return CGM.getTypes().ConvertType(T); |
| 384 | } |
Anders Carlsson | 84005b4 | 2008-01-26 04:30:23 +0000 | [diff] [blame] | 385 | |
Anders Carlsson | 5ea7d07 | 2008-11-16 06:23:45 +0000 | [diff] [blame] | 386 | public: |
Anders Carlsson | 84005b4 | 2008-01-26 04:30:23 +0000 | [diff] [blame] | 387 | llvm::Constant *EmitLValue(Expr *E) { |
Anders Carlsson | e3d3b7d | 2008-01-26 02:08:50 +0000 | [diff] [blame] | 388 | switch (E->getStmtClass()) { |
Eli Friedman | 546d94c | 2008-02-11 00:23:10 +0000 | [diff] [blame] | 389 | default: break; |
Anders Carlsson | e3d3b7d | 2008-01-26 02:08:50 +0000 | [diff] [blame] | 390 | case Expr::CompoundLiteralExprClass: { |
| 391 | // Note that due to the nature of compound literals, this is guaranteed |
| 392 | // to be the only use of the variable, so we just generate it here. |
Anders Carlsson | 84005b4 | 2008-01-26 04:30:23 +0000 | [diff] [blame] | 393 | CompoundLiteralExpr *CLE = cast<CompoundLiteralExpr>(E); |
| 394 | llvm::Constant* C = Visit(CLE->getInitializer()); |
Daniel Dunbar | d60f2fb | 2009-02-17 18:43:32 +0000 | [diff] [blame] | 395 | // FIXME: "Leaked" on failure. |
| 396 | if (C) |
Owen Anderson | c14f0d2 | 2009-07-08 01:29:18 +0000 | [diff] [blame] | 397 | C = new llvm::GlobalVariable(CGM.getModule().getContext(), C->getType(), |
Daniel Dunbar | d60f2fb | 2009-02-17 18:43:32 +0000 | [diff] [blame] | 398 | E->getType().isConstQualified(), |
| 399 | llvm::GlobalValue::InternalLinkage, |
| 400 | C, ".compoundliteral", &CGM.getModule()); |
Anders Carlsson | e3d3b7d | 2008-01-26 02:08:50 +0000 | [diff] [blame] | 401 | return C; |
Anders Carlsson | 84005b4 | 2008-01-26 04:30:23 +0000 | [diff] [blame] | 402 | } |
Douglas Gregor | 1a49af9 | 2009-01-06 05:10:23 +0000 | [diff] [blame] | 403 | case Expr::DeclRefExprClass: |
| 404 | case Expr::QualifiedDeclRefExprClass: { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 405 | NamedDecl *Decl = cast<DeclRefExpr>(E)->getDecl(); |
Anders Carlsson | e3d3b7d | 2008-01-26 02:08:50 +0000 | [diff] [blame] | 406 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(Decl)) |
Chris Lattner | b4880ba | 2009-05-12 21:21:08 +0000 | [diff] [blame] | 407 | return CGM.GetAddrOfFunction(GlobalDecl(FD)); |
Steve Naroff | 248a753 | 2008-04-15 22:42:06 +0000 | [diff] [blame] | 408 | if (const VarDecl* VD = dyn_cast<VarDecl>(Decl)) { |
Daniel Dunbar | d01b669 | 2009-02-24 18:41:57 +0000 | [diff] [blame] | 409 | // We can never refer to a variable with local storage. |
| 410 | if (!VD->hasLocalStorage()) { |
| 411 | if (VD->isFileVarDecl() || VD->hasExternalStorage()) |
| 412 | return CGM.GetAddrOfGlobalVar(VD); |
| 413 | else if (VD->isBlockVarDecl()) { |
| 414 | assert(CGF && "Can't access static local vars without CGF"); |
| 415 | return CGF->GetAddrOfStaticLocalVar(VD); |
| 416 | } |
Steve Naroff | 248a753 | 2008-04-15 22:42:06 +0000 | [diff] [blame] | 417 | } |
Lauro Ramos Venancio | 8137335 | 2008-02-26 21:41:45 +0000 | [diff] [blame] | 418 | } |
Eli Friedman | 546d94c | 2008-02-11 00:23:10 +0000 | [diff] [blame] | 419 | break; |
Anders Carlsson | e3d3b7d | 2008-01-26 02:08:50 +0000 | [diff] [blame] | 420 | } |
Daniel Dunbar | 6143293 | 2008-08-13 23:20:05 +0000 | [diff] [blame] | 421 | case Expr::StringLiteralClass: |
| 422 | return CGM.GetAddrOfConstantStringFromLiteral(cast<StringLiteral>(E)); |
Chris Lattner | eaf2bb8 | 2009-02-24 22:18:39 +0000 | [diff] [blame] | 423 | case Expr::ObjCEncodeExprClass: |
| 424 | return CGM.GetAddrOfConstantStringFromObjCEncode(cast<ObjCEncodeExpr>(E)); |
Anders Carlsson | 5ea7d07 | 2008-11-16 06:23:45 +0000 | [diff] [blame] | 425 | case Expr::ObjCStringLiteralClass: { |
| 426 | ObjCStringLiteral* SL = cast<ObjCStringLiteral>(E); |
Steve Naroff | 33fdb73 | 2009-03-31 16:53:37 +0000 | [diff] [blame] | 427 | llvm::Constant *C = CGM.getObjCRuntime().GenerateConstantString(SL); |
Anders Carlsson | 5ea7d07 | 2008-11-16 06:23:45 +0000 | [diff] [blame] | 428 | return llvm::ConstantExpr::getBitCast(C, ConvertType(E->getType())); |
| 429 | } |
Chris Lattner | 3534564 | 2008-12-12 05:18:02 +0000 | [diff] [blame] | 430 | case Expr::PredefinedExprClass: { |
| 431 | // __func__/__FUNCTION__ -> "". __PRETTY_FUNCTION__ -> "top level". |
| 432 | std::string Str; |
| 433 | if (cast<PredefinedExpr>(E)->getIdentType() == |
| 434 | PredefinedExpr::PrettyFunction) |
| 435 | Str = "top level"; |
| 436 | |
| 437 | return CGM.GetAddrOfConstantCString(Str, ".tmp"); |
| 438 | } |
Eli Friedman | f011589 | 2009-01-25 01:21:06 +0000 | [diff] [blame] | 439 | case Expr::AddrLabelExprClass: { |
| 440 | assert(CGF && "Invalid address of label expression outside function."); |
| 441 | unsigned id = CGF->GetIDForAddrOfLabel(cast<AddrLabelExpr>(E)->getLabel()); |
| 442 | llvm::Constant *C = llvm::ConstantInt::get(llvm::Type::Int32Ty, id); |
| 443 | return llvm::ConstantExpr::getIntToPtr(C, ConvertType(E->getType())); |
| 444 | } |
Eli Friedman | 3941b18 | 2009-01-25 01:54:01 +0000 | [diff] [blame] | 445 | case Expr::CallExprClass: { |
| 446 | CallExpr* CE = cast<CallExpr>(E); |
Douglas Gregor | 3c385e5 | 2009-02-14 18:57:46 +0000 | [diff] [blame] | 447 | if (CE->isBuiltinCall(CGM.getContext()) != |
| 448 | Builtin::BI__builtin___CFStringMakeConstantString) |
Eli Friedman | 3941b18 | 2009-01-25 01:54:01 +0000 | [diff] [blame] | 449 | break; |
| 450 | const Expr *Arg = CE->getArg(0)->IgnoreParenCasts(); |
| 451 | const StringLiteral *Literal = cast<StringLiteral>(Arg); |
Steve Naroff | 33fdb73 | 2009-03-31 16:53:37 +0000 | [diff] [blame] | 452 | // FIXME: need to deal with UCN conversion issues. |
Steve Naroff | 8d4141f | 2009-04-01 13:55:36 +0000 | [diff] [blame] | 453 | return CGM.GetAddrOfConstantCFString(Literal); |
Eli Friedman | 3941b18 | 2009-01-25 01:54:01 +0000 | [diff] [blame] | 454 | } |
Mike Stump | bd65cac | 2009-02-19 01:01:04 +0000 | [diff] [blame] | 455 | case Expr::BlockExprClass: { |
Anders Carlsson | 4de9fce | 2009-03-01 01:09:12 +0000 | [diff] [blame] | 456 | std::string FunctionName; |
| 457 | if (CGF) |
| 458 | FunctionName = CGF->CurFn->getName(); |
| 459 | else |
| 460 | FunctionName = "global"; |
| 461 | |
| 462 | return CGM.GetAddrOfGlobalBlock(cast<BlockExpr>(E), FunctionName.c_str()); |
Mike Stump | bd65cac | 2009-02-19 01:01:04 +0000 | [diff] [blame] | 463 | } |
Eli Friedman | 546d94c | 2008-02-11 00:23:10 +0000 | [diff] [blame] | 464 | } |
Daniel Dunbar | d60f2fb | 2009-02-17 18:43:32 +0000 | [diff] [blame] | 465 | |
| 466 | return 0; |
Anders Carlsson | e3d3b7d | 2008-01-26 02:08:50 +0000 | [diff] [blame] | 467 | } |
Anders Carlsson | e3d3b7d | 2008-01-26 02:08:50 +0000 | [diff] [blame] | 468 | }; |
| 469 | |
Anders Carlsson | 3b1d57b | 2008-01-26 01:36:00 +0000 | [diff] [blame] | 470 | } // end anonymous namespace. |
| 471 | |
Lauro Ramos Venancio | 8137335 | 2008-02-26 21:41:45 +0000 | [diff] [blame] | 472 | llvm::Constant *CodeGenModule::EmitConstantExpr(const Expr *E, |
Anders Carlsson | e9352cc | 2009-04-08 04:48:15 +0000 | [diff] [blame] | 473 | QualType DestType, |
Chris Lattner | 9619662 | 2008-07-26 22:37:01 +0000 | [diff] [blame] | 474 | CodeGenFunction *CGF) { |
Anders Carlsson | 4b3f9c0 | 2008-12-01 02:42:14 +0000 | [diff] [blame] | 475 | Expr::EvalResult Result; |
| 476 | |
Anders Carlsson | 1e5dc6e | 2009-04-11 01:08:03 +0000 | [diff] [blame] | 477 | bool Success = false; |
| 478 | |
Eli Friedman | 50c39ea | 2009-05-27 06:04:58 +0000 | [diff] [blame] | 479 | if (DestType->isReferenceType()) |
| 480 | Success = E->EvaluateAsLValue(Result, Context); |
| 481 | else |
Anders Carlsson | 1e5dc6e | 2009-04-11 01:08:03 +0000 | [diff] [blame] | 482 | Success = E->Evaluate(Result, Context); |
| 483 | |
| 484 | if (Success) { |
Anders Carlsson | 4b3f9c0 | 2008-12-01 02:42:14 +0000 | [diff] [blame] | 485 | assert(!Result.HasSideEffects && |
| 486 | "Constant expr should not have any side effects!"); |
| 487 | switch (Result.Val.getKind()) { |
Daniel Dunbar | a5fd07b | 2009-01-28 22:24:07 +0000 | [diff] [blame] | 488 | case APValue::Uninitialized: |
Daniel Dunbar | d60f2fb | 2009-02-17 18:43:32 +0000 | [diff] [blame] | 489 | assert(0 && "Constant expressions should be initialized."); |
| 490 | return 0; |
Anders Carlsson | 5a9a457 | 2008-11-15 20:45:50 +0000 | [diff] [blame] | 491 | case APValue::LValue: { |
Anders Carlsson | 1e5dc6e | 2009-04-11 01:08:03 +0000 | [diff] [blame] | 492 | const llvm::Type *DestTy = getTypes().ConvertTypeForMem(DestType); |
Anders Carlsson | 4b3f9c0 | 2008-12-01 02:42:14 +0000 | [diff] [blame] | 493 | llvm::Constant *Offset = |
| 494 | llvm::ConstantInt::get(llvm::Type::Int64Ty, |
| 495 | Result.Val.getLValueOffset()); |
Anders Carlsson | 5a9a457 | 2008-11-15 20:45:50 +0000 | [diff] [blame] | 496 | |
Daniel Dunbar | e20de51 | 2009-02-19 21:44:24 +0000 | [diff] [blame] | 497 | llvm::Constant *C; |
Anders Carlsson | 4b3f9c0 | 2008-12-01 02:42:14 +0000 | [diff] [blame] | 498 | if (const Expr *LVBase = Result.Val.getLValueBase()) { |
Daniel Dunbar | e20de51 | 2009-02-19 21:44:24 +0000 | [diff] [blame] | 499 | C = ConstExprEmitter(*this, CGF).EmitLValue(const_cast<Expr*>(LVBase)); |
Anders Carlsson | 5ea7d07 | 2008-11-16 06:23:45 +0000 | [diff] [blame] | 500 | |
Daniel Dunbar | e20de51 | 2009-02-19 21:44:24 +0000 | [diff] [blame] | 501 | // Apply offset if necessary. |
| 502 | if (!Offset->isNullValue()) { |
| 503 | const llvm::Type *Type = |
| 504 | llvm::PointerType::getUnqual(llvm::Type::Int8Ty); |
| 505 | llvm::Constant *Casted = llvm::ConstantExpr::getBitCast(C, Type); |
| 506 | Casted = llvm::ConstantExpr::getGetElementPtr(Casted, &Offset, 1); |
| 507 | C = llvm::ConstantExpr::getBitCast(Casted, C->getType()); |
| 508 | } |
| 509 | |
| 510 | // Convert to the appropriate type; this could be an lvalue for |
| 511 | // an integer. |
Anders Carlsson | 12e330c | 2009-04-10 04:59:59 +0000 | [diff] [blame] | 512 | if (isa<llvm::PointerType>(DestTy)) |
| 513 | return llvm::ConstantExpr::getBitCast(C, DestTy); |
Daniel Dunbar | e20de51 | 2009-02-19 21:44:24 +0000 | [diff] [blame] | 514 | |
Anders Carlsson | 12e330c | 2009-04-10 04:59:59 +0000 | [diff] [blame] | 515 | return llvm::ConstantExpr::getPtrToInt(C, DestTy); |
Daniel Dunbar | e20de51 | 2009-02-19 21:44:24 +0000 | [diff] [blame] | 516 | } else { |
| 517 | C = Offset; |
| 518 | |
| 519 | // Convert to the appropriate type; this could be an lvalue for |
| 520 | // an integer. |
Anders Carlsson | 12e330c | 2009-04-10 04:59:59 +0000 | [diff] [blame] | 521 | if (isa<llvm::PointerType>(DestTy)) |
| 522 | return llvm::ConstantExpr::getIntToPtr(C, DestTy); |
Daniel Dunbar | e20de51 | 2009-02-19 21:44:24 +0000 | [diff] [blame] | 523 | |
| 524 | // If the types don't match this should only be a truncate. |
Anders Carlsson | 12e330c | 2009-04-10 04:59:59 +0000 | [diff] [blame] | 525 | if (C->getType() != DestTy) |
| 526 | return llvm::ConstantExpr::getTrunc(C, DestTy); |
Daniel Dunbar | e20de51 | 2009-02-19 21:44:24 +0000 | [diff] [blame] | 527 | |
| 528 | return C; |
Anders Carlsson | 5ea7d07 | 2008-11-16 06:23:45 +0000 | [diff] [blame] | 529 | } |
Anders Carlsson | 5a9a457 | 2008-11-15 20:45:50 +0000 | [diff] [blame] | 530 | } |
Eli Friedman | 7b30b1c | 2008-11-17 03:57:28 +0000 | [diff] [blame] | 531 | case APValue::Int: { |
Anders Carlsson | 4b3f9c0 | 2008-12-01 02:42:14 +0000 | [diff] [blame] | 532 | llvm::Constant *C = llvm::ConstantInt::get(Result.Val.getInt()); |
Anders Carlsson | 5a9a457 | 2008-11-15 20:45:50 +0000 | [diff] [blame] | 533 | |
| 534 | if (C->getType() == llvm::Type::Int1Ty) { |
| 535 | const llvm::Type *BoolTy = getTypes().ConvertTypeForMem(E->getType()); |
| 536 | C = llvm::ConstantExpr::getZExt(C, BoolTy); |
| 537 | } |
| 538 | return C; |
Eli Friedman | 7b30b1c | 2008-11-17 03:57:28 +0000 | [diff] [blame] | 539 | } |
Daniel Dunbar | a5fd07b | 2009-01-28 22:24:07 +0000 | [diff] [blame] | 540 | case APValue::ComplexInt: { |
| 541 | llvm::Constant *Complex[2]; |
| 542 | |
| 543 | Complex[0] = llvm::ConstantInt::get(Result.Val.getComplexIntReal()); |
| 544 | Complex[1] = llvm::ConstantInt::get(Result.Val.getComplexIntImag()); |
| 545 | |
| 546 | return llvm::ConstantStruct::get(Complex, 2); |
| 547 | } |
Anders Carlsson | 5a9a457 | 2008-11-15 20:45:50 +0000 | [diff] [blame] | 548 | case APValue::Float: |
Anders Carlsson | 4b3f9c0 | 2008-12-01 02:42:14 +0000 | [diff] [blame] | 549 | return llvm::ConstantFP::get(Result.Val.getFloat()); |
Anders Carlsson | aa43256 | 2008-11-17 01:58:55 +0000 | [diff] [blame] | 550 | case APValue::ComplexFloat: { |
| 551 | llvm::Constant *Complex[2]; |
| 552 | |
Anders Carlsson | 4b3f9c0 | 2008-12-01 02:42:14 +0000 | [diff] [blame] | 553 | Complex[0] = llvm::ConstantFP::get(Result.Val.getComplexFloatReal()); |
| 554 | Complex[1] = llvm::ConstantFP::get(Result.Val.getComplexFloatImag()); |
Anders Carlsson | aa43256 | 2008-11-17 01:58:55 +0000 | [diff] [blame] | 555 | |
| 556 | return llvm::ConstantStruct::get(Complex, 2); |
| 557 | } |
Nate Begeman | 3d309f9 | 2009-01-18 01:01:34 +0000 | [diff] [blame] | 558 | case APValue::Vector: { |
| 559 | llvm::SmallVector<llvm::Constant *, 4> Inits; |
| 560 | unsigned NumElts = Result.Val.getVectorLength(); |
| 561 | |
| 562 | for (unsigned i = 0; i != NumElts; ++i) { |
| 563 | APValue &Elt = Result.Val.getVectorElt(i); |
| 564 | if (Elt.isInt()) |
| 565 | Inits.push_back(llvm::ConstantInt::get(Elt.getInt())); |
| 566 | else |
| 567 | Inits.push_back(llvm::ConstantFP::get(Elt.getFloat())); |
| 568 | } |
| 569 | return llvm::ConstantVector::get(&Inits[0], Inits.size()); |
| 570 | } |
Anders Carlsson | 5a9a457 | 2008-11-15 20:45:50 +0000 | [diff] [blame] | 571 | } |
| 572 | } |
Eli Friedman | 7dfa639 | 2008-06-01 15:31:44 +0000 | [diff] [blame] | 573 | |
| 574 | llvm::Constant* C = ConstExprEmitter(*this, CGF).Visit(const_cast<Expr*>(E)); |
Daniel Dunbar | d60f2fb | 2009-02-17 18:43:32 +0000 | [diff] [blame] | 575 | if (C && C->getType() == llvm::Type::Int1Ty) { |
Eli Friedman | 7dfa639 | 2008-06-01 15:31:44 +0000 | [diff] [blame] | 576 | const llvm::Type *BoolTy = getTypes().ConvertTypeForMem(E->getType()); |
| 577 | C = llvm::ConstantExpr::getZExt(C, BoolTy); |
| 578 | } |
| 579 | return C; |
Anders Carlsson | 3b1d57b | 2008-01-26 01:36:00 +0000 | [diff] [blame] | 580 | } |
Eli Friedman | 0f59312 | 2009-04-13 21:47:26 +0000 | [diff] [blame] | 581 | |
| 582 | llvm::Constant *CodeGenModule::EmitNullConstant(QualType T) { |
| 583 | // Always return an LLVM null constant for now; this will change when we |
| 584 | // get support for IRGen of member pointers. |
| 585 | return llvm::Constant::getNullValue(getTypes().ConvertType(T)); |
| 586 | } |