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" |
| 16 | #include "clang/AST/AST.h" |
| 17 | #include "llvm/Constants.h" |
| 18 | #include "llvm/Function.h" |
| 19 | #include "llvm/GlobalVariable.h" |
| 20 | #include "llvm/Support/Compiler.h" |
Eli Friedman | a823400 | 2008-05-30 10:24:46 +0000 | [diff] [blame] | 21 | #include "llvm/Target/TargetData.h" |
Anders Carlsson | 3b1d57b | 2008-01-26 01:36:00 +0000 | [diff] [blame] | 22 | using namespace clang; |
| 23 | using namespace CodeGen; |
| 24 | |
| 25 | namespace { |
Anders Carlsson | 84005b4 | 2008-01-26 04:30:23 +0000 | [diff] [blame] | 26 | class VISIBILITY_HIDDEN ConstExprEmitter : |
| 27 | public StmtVisitor<ConstExprEmitter, llvm::Constant*> { |
Anders Carlsson | 3b1d57b | 2008-01-26 01:36:00 +0000 | [diff] [blame] | 28 | CodeGenModule &CGM; |
Lauro Ramos Venancio | 8137335 | 2008-02-26 21:41:45 +0000 | [diff] [blame] | 29 | CodeGenFunction *CGF; |
Anders Carlsson | 3b1d57b | 2008-01-26 01:36:00 +0000 | [diff] [blame] | 30 | public: |
Lauro Ramos Venancio | 8137335 | 2008-02-26 21:41:45 +0000 | [diff] [blame] | 31 | ConstExprEmitter(CodeGenModule &cgm, CodeGenFunction *cgf) |
| 32 | : CGM(cgm), CGF(cgf) { |
Anders Carlsson | 3b1d57b | 2008-01-26 01:36:00 +0000 | [diff] [blame] | 33 | } |
| 34 | |
| 35 | //===--------------------------------------------------------------------===// |
| 36 | // Visitor Methods |
| 37 | //===--------------------------------------------------------------------===// |
| 38 | |
| 39 | llvm::Constant *VisitStmt(Stmt *S) { |
| 40 | CGM.WarnUnsupported(S, "constant expression"); |
Chris Lattner | 98be494 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 41 | QualType T = cast<Expr>(S)->getType(); |
| 42 | return llvm::UndefValue::get(CGM.getTypes().ConvertType(T)); |
Anders Carlsson | 3b1d57b | 2008-01-26 01:36:00 +0000 | [diff] [blame] | 43 | } |
| 44 | |
| 45 | llvm::Constant *VisitParenExpr(ParenExpr *PE) { |
| 46 | return Visit(PE->getSubExpr()); |
| 47 | } |
| 48 | |
| 49 | // Leaves |
| 50 | llvm::Constant *VisitIntegerLiteral(const IntegerLiteral *E) { |
| 51 | return llvm::ConstantInt::get(E->getValue()); |
| 52 | } |
| 53 | llvm::Constant *VisitFloatingLiteral(const FloatingLiteral *E) { |
Chris Lattner | 59138ba | 2008-04-20 00:45:53 +0000 | [diff] [blame] | 54 | return llvm::ConstantFP::get(E->getValue()); |
Anders Carlsson | 3b1d57b | 2008-01-26 01:36:00 +0000 | [diff] [blame] | 55 | } |
| 56 | llvm::Constant *VisitCharacterLiteral(const CharacterLiteral *E) { |
| 57 | return llvm::ConstantInt::get(ConvertType(E->getType()), E->getValue()); |
| 58 | } |
| 59 | llvm::Constant *VisitCXXBoolLiteralExpr(const CXXBoolLiteralExpr *E) { |
| 60 | return llvm::ConstantInt::get(ConvertType(E->getType()), E->getValue()); |
| 61 | } |
Chris Lattner | 13fd7e5 | 2008-06-21 21:44:18 +0000 | [diff] [blame] | 62 | llvm::Constant *VisitObjCStringLiteral(const ObjCStringLiteral *E) { |
| 63 | return CGM.getObjCRuntime()->GenerateConstantString( |
| 64 | E->getString()->getStrData(), E->getString()->getByteLength()); |
| 65 | } |
Anders Carlsson | 3b1d57b | 2008-01-26 01:36:00 +0000 | [diff] [blame] | 66 | |
| 67 | llvm::Constant *VisitCompoundLiteralExpr(CompoundLiteralExpr *E) { |
| 68 | return Visit(E->getInitializer()); |
| 69 | } |
| 70 | |
| 71 | llvm::Constant *VisitCastExpr(const CastExpr* E) { |
| 72 | llvm::Constant *C = Visit(E->getSubExpr()); |
| 73 | |
| 74 | return EmitConversion(C, E->getSubExpr()->getType(), E->getType()); |
| 75 | } |
Devang Patel | eae1560 | 2008-02-05 02:39:50 +0000 | [diff] [blame] | 76 | |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 77 | llvm::Constant *VisitCXXDefaultArgExpr(CXXDefaultArgExpr *DAE) { |
| 78 | return Visit(DAE->getExpr()); |
| 79 | } |
| 80 | |
Eli Friedman | a823400 | 2008-05-30 10:24:46 +0000 | [diff] [blame] | 81 | llvm::Constant *EmitArrayInitialization(InitListExpr *ILE) { |
Eli Friedman | 4d1c018 | 2008-02-21 17:45:41 +0000 | [diff] [blame] | 82 | std::vector<llvm::Constant*> Elts; |
Eli Friedman | a823400 | 2008-05-30 10:24:46 +0000 | [diff] [blame] | 83 | const llvm::ArrayType *AType = |
| 84 | cast<llvm::ArrayType>(ConvertType(ILE->getType())); |
Eli Friedman | 4d1c018 | 2008-02-21 17:45:41 +0000 | [diff] [blame] | 85 | unsigned NumInitElements = ILE->getNumInits(); |
| 86 | // FIXME: Check for wide strings |
| 87 | if (NumInitElements > 0 && isa<StringLiteral>(ILE->getInit(0)) && |
| 88 | ILE->getType()->getAsArrayType()->getElementType()->isCharType()) |
| 89 | return Visit(ILE->getInit(0)); |
Devang Patel | eae1560 | 2008-02-05 02:39:50 +0000 | [diff] [blame] | 90 | const llvm::Type *ElemTy = AType->getElementType(); |
| 91 | unsigned NumElements = AType->getNumElements(); |
| 92 | |
| 93 | // Initialising an array requires us to automatically |
| 94 | // initialise any elements that have not been initialised explicitly |
| 95 | unsigned NumInitableElts = std::min(NumInitElements, NumElements); |
| 96 | |
| 97 | // Copy initializer elements. |
| 98 | unsigned i = 0; |
Eli Friedman | a04a153 | 2008-05-30 19:58:50 +0000 | [diff] [blame] | 99 | bool RewriteType = false; |
Devang Patel | eae1560 | 2008-02-05 02:39:50 +0000 | [diff] [blame] | 100 | for (; i < NumInitableElts; ++i) { |
Devang Patel | eae1560 | 2008-02-05 02:39:50 +0000 | [diff] [blame] | 101 | llvm::Constant *C = Visit(ILE->getInit(i)); |
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. |
| 107 | for (; i < NumElements; ++i) |
| 108 | Elts.push_back(llvm::Constant::getNullValue(ElemTy)); |
| 109 | |
Eli Friedman | a04a153 | 2008-05-30 19:58:50 +0000 | [diff] [blame] | 110 | if (RewriteType) { |
| 111 | // FIXME: Try to avoid packing the array |
| 112 | std::vector<const llvm::Type*> Types; |
| 113 | for (unsigned i = 0; i < Elts.size(); ++i) |
| 114 | Types.push_back(Elts[i]->getType()); |
| 115 | const llvm::StructType *SType = llvm::StructType::get(Types, true); |
| 116 | return llvm::ConstantStruct::get(SType, Elts); |
| 117 | } |
| 118 | |
Devang Patel | eae1560 | 2008-02-05 02:39:50 +0000 | [diff] [blame] | 119 | return llvm::ConstantArray::get(AType, Elts); |
| 120 | } |
| 121 | |
Eli Friedman | a823400 | 2008-05-30 10:24:46 +0000 | [diff] [blame] | 122 | void InsertBitfieldIntoStruct(std::vector<llvm::Constant*>& Elts, |
| 123 | FieldDecl* Field, Expr* E) { |
| 124 | // Calculate the value to insert |
| 125 | llvm::Constant *C = Visit(E); |
| 126 | llvm::ConstantInt *CI = dyn_cast<llvm::ConstantInt>(C); |
| 127 | if (!CI) { |
| 128 | CGM.WarnUnsupported(E, "bitfield initialization"); |
| 129 | return; |
| 130 | } |
| 131 | llvm::APInt V = CI->getValue(); |
Devang Patel | eae1560 | 2008-02-05 02:39:50 +0000 | [diff] [blame] | 132 | |
Eli Friedman | a823400 | 2008-05-30 10:24:46 +0000 | [diff] [blame] | 133 | // Calculate information about the relevant field |
| 134 | const llvm::Type* Ty = CI->getType(); |
| 135 | unsigned size = CGM.getTypes().getTargetData().getTypeStoreSizeInBits(Ty); |
| 136 | unsigned fieldOffset = CGM.getTypes().getLLVMFieldNo(Field) * size; |
| 137 | CodeGenTypes::BitFieldInfo bitFieldInfo = |
| 138 | CGM.getTypes().getBitFieldInfo(Field); |
| 139 | fieldOffset += bitFieldInfo.Begin; |
| 140 | |
| 141 | // Find where to start the insertion |
| 142 | // FIXME: This is O(n^2) in the number of bit-fields! |
| 143 | // FIXME: This won't work if the struct isn't completely packed! |
| 144 | unsigned offset = 0, i = 0; |
| 145 | while (offset < (fieldOffset & -8)) |
| 146 | offset += CGM.getTypes().getTargetData().getTypeStoreSizeInBits(Elts[i++]->getType()); |
| 147 | |
Daniel Dunbar | 1f3846e | 2008-07-17 21:07:48 +0000 | [diff] [blame] | 148 | // Promote the size of V if necessary |
| 149 | // FIXME: This should never occur, but currently it can because |
| 150 | // initializer constants are cast to bool, and because clang is |
| 151 | // not enforcing bitfield width limits. |
| 152 | if (bitFieldInfo.Size > V.getBitWidth()) |
| 153 | V.zext(bitFieldInfo.Size); |
| 154 | |
Eli Friedman | a823400 | 2008-05-30 10:24:46 +0000 | [diff] [blame] | 155 | // Insert the bits into the struct |
| 156 | // FIXME: This algorthm is only correct on X86! |
| 157 | // FIXME: THis algorthm assumes bit-fields only have byte-size elements! |
| 158 | unsigned bitsToInsert = bitFieldInfo.Size; |
| 159 | unsigned curBits = std::min(8 - (fieldOffset & 7), bitsToInsert); |
| 160 | unsigned byte = V.getLoBits(curBits).getZExtValue() << (fieldOffset & 7); |
| 161 | do { |
| 162 | llvm::Constant* byteC = llvm::ConstantInt::get(llvm::Type::Int8Ty, byte); |
| 163 | Elts[i] = llvm::ConstantExpr::getOr(Elts[i], byteC); |
| 164 | ++i; |
| 165 | V = V.lshr(curBits); |
| 166 | bitsToInsert -= curBits; |
| 167 | |
| 168 | if (!bitsToInsert) |
| 169 | break; |
| 170 | |
| 171 | curBits = bitsToInsert > 8 ? 8 : bitsToInsert; |
| 172 | byte = V.getLoBits(curBits).getZExtValue(); |
| 173 | } while (true); |
| 174 | } |
| 175 | |
| 176 | llvm::Constant *EmitStructInitialization(InitListExpr *ILE) { |
| 177 | const llvm::StructType *SType = |
| 178 | cast<llvm::StructType>(ConvertType(ILE->getType())); |
Eli Friedman | 017cbdf | 2008-05-20 00:11:07 +0000 | [diff] [blame] | 179 | RecordDecl *RD = ILE->getType()->getAsRecordType()->getDecl(); |
Devang Patel | eae1560 | 2008-02-05 02:39:50 +0000 | [diff] [blame] | 180 | std::vector<llvm::Constant*> Elts; |
Devang Patel | eae1560 | 2008-02-05 02:39:50 +0000 | [diff] [blame] | 181 | |
Eli Friedman | 017cbdf | 2008-05-20 00:11:07 +0000 | [diff] [blame] | 182 | // Initialize the whole structure to zero. |
| 183 | for (unsigned i = 0; i < SType->getNumElements(); ++i) { |
Devang Patel | eae1560 | 2008-02-05 02:39:50 +0000 | [diff] [blame] | 184 | const llvm::Type *FieldTy = SType->getElementType(i); |
| 185 | Elts.push_back(llvm::Constant::getNullValue(FieldTy)); |
| 186 | } |
Eli Friedman | 017cbdf | 2008-05-20 00:11:07 +0000 | [diff] [blame] | 187 | |
| 188 | // Copy initializer elements. Skip padding fields. |
| 189 | unsigned EltNo = 0; // Element no in ILE |
| 190 | int FieldNo = 0; // Field no in RecordDecl |
Eli Friedman | a04a153 | 2008-05-30 19:58:50 +0000 | [diff] [blame] | 191 | bool RewriteType = false; |
Eli Friedman | 017cbdf | 2008-05-20 00:11:07 +0000 | [diff] [blame] | 192 | while (EltNo < ILE->getNumInits() && FieldNo < RD->getNumMembers()) { |
| 193 | FieldDecl* curField = RD->getMember(FieldNo); |
| 194 | FieldNo++; |
| 195 | if (!curField->getIdentifier()) |
| 196 | continue; |
| 197 | |
Eli Friedman | 017cbdf | 2008-05-20 00:11:07 +0000 | [diff] [blame] | 198 | if (curField->isBitField()) { |
Eli Friedman | a823400 | 2008-05-30 10:24:46 +0000 | [diff] [blame] | 199 | InsertBitfieldIntoStruct(Elts, curField, ILE->getInit(EltNo)); |
Eli Friedman | 017cbdf | 2008-05-20 00:11:07 +0000 | [diff] [blame] | 200 | } else { |
Eli Friedman | a04a153 | 2008-05-30 19:58:50 +0000 | [diff] [blame] | 201 | unsigned FieldNo = CGM.getTypes().getLLVMFieldNo(curField); |
| 202 | llvm::Constant* C = Visit(ILE->getInit(EltNo)); |
| 203 | RewriteType |= (C->getType() != Elts[FieldNo]->getType()); |
| 204 | Elts[FieldNo] = C; |
Eli Friedman | 017cbdf | 2008-05-20 00:11:07 +0000 | [diff] [blame] | 205 | } |
| 206 | EltNo++; |
| 207 | } |
| 208 | |
Eli Friedman | a04a153 | 2008-05-30 19:58:50 +0000 | [diff] [blame] | 209 | if (RewriteType) { |
| 210 | // FIXME: Make this work for non-packed structs |
| 211 | assert(SType->isPacked() && "Cannot recreate unpacked structs"); |
| 212 | std::vector<const llvm::Type*> Types; |
| 213 | for (unsigned i = 0; i < Elts.size(); ++i) |
| 214 | Types.push_back(Elts[i]->getType()); |
| 215 | SType = llvm::StructType::get(Types, true); |
| 216 | } |
| 217 | |
Devang Patel | eae1560 | 2008-02-05 02:39:50 +0000 | [diff] [blame] | 218 | return llvm::ConstantStruct::get(SType, Elts); |
| 219 | } |
| 220 | |
Eli Friedman | a823400 | 2008-05-30 10:24:46 +0000 | [diff] [blame] | 221 | llvm::Constant *EmitUnionInitialization(InitListExpr *ILE) { |
Eli Friedman | a823400 | 2008-05-30 10:24:46 +0000 | [diff] [blame] | 222 | RecordDecl *RD = ILE->getType()->getAsRecordType()->getDecl(); |
| 223 | const llvm::Type *Ty = ConvertType(ILE->getType()); |
| 224 | |
| 225 | // Find the field decl we're initializing, if any |
| 226 | int FieldNo = 0; // Field no in RecordDecl |
Eli Friedman | c55f98d | 2008-06-06 20:12:37 +0000 | [diff] [blame] | 227 | FieldDecl* curField = 0; |
| 228 | while (FieldNo < RD->getNumMembers()) { |
Eli Friedman | a823400 | 2008-05-30 10:24:46 +0000 | [diff] [blame] | 229 | curField = RD->getMember(FieldNo); |
| 230 | FieldNo++; |
Eli Friedman | c55f98d | 2008-06-06 20:12:37 +0000 | [diff] [blame] | 231 | if (curField->getIdentifier()) |
| 232 | break; |
| 233 | } |
Eli Friedman | a823400 | 2008-05-30 10:24:46 +0000 | [diff] [blame] | 234 | |
Eli Friedman | c55f98d | 2008-06-06 20:12:37 +0000 | [diff] [blame] | 235 | if (!curField || !curField->getIdentifier() || ILE->getNumInits() == 0) |
Eli Friedman | a823400 | 2008-05-30 10:24:46 +0000 | [diff] [blame] | 236 | return llvm::Constant::getNullValue(Ty); |
| 237 | |
| 238 | if (curField->isBitField()) { |
| 239 | // Create a dummy struct for bit-field insertion |
| 240 | unsigned NumElts = CGM.getTargetData().getABITypeSize(Ty) / 8; |
| 241 | llvm::Constant* NV = llvm::Constant::getNullValue(llvm::Type::Int8Ty); |
| 242 | std::vector<llvm::Constant*> Elts(NumElts, NV); |
| 243 | |
| 244 | InsertBitfieldIntoStruct(Elts, curField, ILE->getInit(0)); |
| 245 | const llvm::ArrayType *RetTy = |
| 246 | llvm::ArrayType::get(NV->getType(), NumElts); |
| 247 | return llvm::ConstantArray::get(RetTy, Elts); |
| 248 | } |
| 249 | |
| 250 | llvm::Constant *C = Visit(ILE->getInit(0)); |
| 251 | |
| 252 | // Build a struct with the union sub-element as the first member, |
| 253 | // and padded to the appropriate size |
| 254 | std::vector<llvm::Constant*> Elts; |
| 255 | std::vector<const llvm::Type*> Types; |
| 256 | Elts.push_back(C); |
| 257 | Types.push_back(C->getType()); |
| 258 | unsigned CurSize = CGM.getTargetData().getTypeStoreSize(C->getType()); |
| 259 | unsigned TotalSize = CGM.getTargetData().getTypeStoreSize(Ty); |
| 260 | while (CurSize < TotalSize) { |
| 261 | Elts.push_back(llvm::Constant::getNullValue(llvm::Type::Int8Ty)); |
| 262 | Types.push_back(llvm::Type::Int8Ty); |
| 263 | CurSize++; |
| 264 | } |
| 265 | |
| 266 | // This always generates a packed struct |
| 267 | // FIXME: Try to generate an unpacked struct when we can |
| 268 | llvm::StructType* STy = llvm::StructType::get(Types, true); |
| 269 | return llvm::ConstantStruct::get(STy, Elts); |
| 270 | } |
| 271 | |
| 272 | llvm::Constant *EmitVectorInitialization(InitListExpr *ILE) { |
| 273 | const llvm::VectorType *VType = |
| 274 | cast<llvm::VectorType>(ConvertType(ILE->getType())); |
Eli Friedman | cfb313b | 2008-05-30 12:02:14 +0000 | [diff] [blame] | 275 | const llvm::Type *ElemTy = VType->getElementType(); |
| 276 | std::vector<llvm::Constant*> Elts; |
Devang Patel | eae1560 | 2008-02-05 02:39:50 +0000 | [diff] [blame] | 277 | unsigned NumElements = VType->getNumElements(); |
Eli Friedman | cfb313b | 2008-05-30 12:02:14 +0000 | [diff] [blame] | 278 | unsigned NumInitElements = ILE->getNumInits(); |
Devang Patel | eae1560 | 2008-02-05 02:39:50 +0000 | [diff] [blame] | 279 | |
Eli Friedman | cfb313b | 2008-05-30 12:02:14 +0000 | [diff] [blame] | 280 | unsigned NumInitableElts = std::min(NumInitElements, NumElements); |
| 281 | |
Devang Patel | eae1560 | 2008-02-05 02:39:50 +0000 | [diff] [blame] | 282 | // Copy initializer elements. |
| 283 | unsigned i = 0; |
Eli Friedman | cfb313b | 2008-05-30 12:02:14 +0000 | [diff] [blame] | 284 | for (; i < NumInitableElts; ++i) { |
Devang Patel | eae1560 | 2008-02-05 02:39:50 +0000 | [diff] [blame] | 285 | llvm::Constant *C = Visit(ILE->getInit(i)); |
Devang Patel | eae1560 | 2008-02-05 02:39:50 +0000 | [diff] [blame] | 286 | Elts.push_back(C); |
| 287 | } |
| 288 | |
Eli Friedman | cfb313b | 2008-05-30 12:02:14 +0000 | [diff] [blame] | 289 | for (; i < NumElements; ++i) |
| 290 | Elts.push_back(llvm::Constant::getNullValue(ElemTy)); |
| 291 | |
Devang Patel | eae1560 | 2008-02-05 02:39:50 +0000 | [diff] [blame] | 292 | return llvm::ConstantVector::get(VType, Elts); |
| 293 | } |
| 294 | |
Anders Carlsson | 3b1d57b | 2008-01-26 01:36:00 +0000 | [diff] [blame] | 295 | llvm::Constant *VisitInitListExpr(InitListExpr *ILE) { |
Eli Friedman | a823400 | 2008-05-30 10:24:46 +0000 | [diff] [blame] | 296 | if (ILE->getType()->isScalarType()) { |
| 297 | // We have a scalar in braces. Just use the first element. |
| 298 | if (ILE->getNumInits() > 0) |
Anders Carlsson | f6884ac | 2008-01-29 01:15:48 +0000 | [diff] [blame] | 299 | return Visit(ILE->getInit(0)); |
Eli Friedman | a823400 | 2008-05-30 10:24:46 +0000 | [diff] [blame] | 300 | |
| 301 | const llvm::Type* RetTy = CGM.getTypes().ConvertType(ILE->getType()); |
| 302 | return llvm::Constant::getNullValue(RetTy); |
Anders Carlsson | 3b1d57b | 2008-01-26 01:36:00 +0000 | [diff] [blame] | 303 | } |
Devang Patel | 47fb697 | 2008-01-29 23:23:18 +0000 | [diff] [blame] | 304 | |
Eli Friedman | a823400 | 2008-05-30 10:24:46 +0000 | [diff] [blame] | 305 | if (ILE->getType()->isArrayType()) |
| 306 | return EmitArrayInitialization(ILE); |
Devang Patel | 47fb697 | 2008-01-29 23:23:18 +0000 | [diff] [blame] | 307 | |
Eli Friedman | a823400 | 2008-05-30 10:24:46 +0000 | [diff] [blame] | 308 | if (ILE->getType()->isStructureType()) |
| 309 | return EmitStructInitialization(ILE); |
| 310 | |
| 311 | if (ILE->getType()->isUnionType()) |
| 312 | return EmitUnionInitialization(ILE); |
| 313 | |
| 314 | if (ILE->getType()->isVectorType()) |
| 315 | return EmitVectorInitialization(ILE); |
| 316 | |
Devang Patel | eae1560 | 2008-02-05 02:39:50 +0000 | [diff] [blame] | 317 | assert(0 && "Unable to handle InitListExpr"); |
Chris Lattner | 2b94fe3 | 2008-03-01 08:45:05 +0000 | [diff] [blame] | 318 | // Get rid of control reaches end of void function warning. |
| 319 | // Not reached. |
| 320 | return 0; |
Anders Carlsson | 3b1d57b | 2008-01-26 01:36:00 +0000 | [diff] [blame] | 321 | } |
Eli Friedman | 3b707e7 | 2008-02-21 17:57:49 +0000 | [diff] [blame] | 322 | |
Anders Carlsson | 3b1d57b | 2008-01-26 01:36:00 +0000 | [diff] [blame] | 323 | llvm::Constant *VisitImplicitCastExpr(ImplicitCastExpr *ICExpr) { |
Eli Friedman | 3b707e7 | 2008-02-21 17:57:49 +0000 | [diff] [blame] | 324 | Expr* SExpr = ICExpr->getSubExpr(); |
| 325 | QualType SType = SExpr->getType(); |
| 326 | llvm::Constant *C; // the intermediate expression |
| 327 | QualType T; // the type of the intermediate expression |
| 328 | if (SType->isArrayType()) { |
| 329 | // Arrays decay to a pointer to the first element |
| 330 | // VLAs would require special handling, but they can't occur here |
| 331 | C = EmitLValue(SExpr); |
Anders Carlsson | 3b1d57b | 2008-01-26 01:36:00 +0000 | [diff] [blame] | 332 | llvm::Constant *Idx0 = llvm::ConstantInt::get(llvm::Type::Int32Ty, 0); |
Anders Carlsson | 3b1d57b | 2008-01-26 01:36:00 +0000 | [diff] [blame] | 333 | llvm::Constant *Ops[] = {Idx0, Idx0}; |
| 334 | C = llvm::ConstantExpr::getGetElementPtr(C, Ops, 2); |
Eli Friedman | 3b707e7 | 2008-02-21 17:57:49 +0000 | [diff] [blame] | 335 | |
| 336 | QualType ElemType = SType->getAsArrayType()->getElementType(); |
| 337 | T = CGM.getContext().getPointerType(ElemType); |
| 338 | } else if (SType->isFunctionType()) { |
| 339 | // Function types decay to a pointer to the function |
| 340 | C = EmitLValue(SExpr); |
| 341 | T = CGM.getContext().getPointerType(SType); |
| 342 | } else { |
| 343 | C = Visit(SExpr); |
| 344 | T = SType; |
Anders Carlsson | 3b1d57b | 2008-01-26 01:36:00 +0000 | [diff] [blame] | 345 | } |
Eli Friedman | 3b707e7 | 2008-02-21 17:57:49 +0000 | [diff] [blame] | 346 | |
| 347 | // Perform the conversion; note that an implicit cast can both promote |
| 348 | // and convert an array/function |
| 349 | return EmitConversion(C, T, ICExpr->getType()); |
Anders Carlsson | 3b1d57b | 2008-01-26 01:36:00 +0000 | [diff] [blame] | 350 | } |
Eli Friedman | 3b707e7 | 2008-02-21 17:57:49 +0000 | [diff] [blame] | 351 | |
Anders Carlsson | 3b1d57b | 2008-01-26 01:36:00 +0000 | [diff] [blame] | 352 | llvm::Constant *VisitStringLiteral(StringLiteral *E) { |
| 353 | const char *StrData = E->getStrData(); |
| 354 | unsigned Len = E->getByteLength(); |
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 | |
| 357 | // Otherwise this must be a string initializing an array in a static |
| 358 | // initializer. Don't emit it as the address of the string, emit the string |
| 359 | // data itself as an inline array. |
| 360 | const ConstantArrayType *CAT = E->getType()->getAsConstantArrayType(); |
| 361 | assert(CAT && "String isn't pointer or array!"); |
| 362 | |
| 363 | std::string Str(StrData, StrData + Len); |
| 364 | // Null terminate the string before potentially truncating it. |
| 365 | // FIXME: What about wchar_t strings? |
| 366 | Str.push_back(0); |
| 367 | |
| 368 | uint64_t RealLen = CAT->getSize().getZExtValue(); |
| 369 | // String or grow the initializer to the required size. |
| 370 | if (RealLen != Str.size()) |
| 371 | Str.resize(RealLen); |
| 372 | |
| 373 | return llvm::ConstantArray::get(Str, false); |
| 374 | } |
| 375 | |
| 376 | llvm::Constant *VisitDeclRefExpr(DeclRefExpr *E) { |
| 377 | const ValueDecl *Decl = E->getDecl(); |
Anders Carlsson | a3881fc | 2008-01-29 01:28:48 +0000 | [diff] [blame] | 378 | if (const EnumConstantDecl *EC = dyn_cast<EnumConstantDecl>(Decl)) |
| 379 | return llvm::ConstantInt::get(EC->getInitVal()); |
Anders Carlsson | 3b1d57b | 2008-01-26 01:36:00 +0000 | [diff] [blame] | 380 | assert(0 && "Unsupported decl ref type!"); |
| 381 | return 0; |
| 382 | } |
| 383 | |
| 384 | llvm::Constant *VisitSizeOfAlignOfTypeExpr(const SizeOfAlignOfTypeExpr *E) { |
| 385 | return EmitSizeAlignOf(E->getArgumentType(), E->getType(), E->isSizeOf()); |
| 386 | } |
| 387 | |
| 388 | // Unary operators |
| 389 | llvm::Constant *VisitUnaryPlus(const UnaryOperator *E) { |
| 390 | return Visit(E->getSubExpr()); |
| 391 | } |
| 392 | llvm::Constant *VisitUnaryMinus(const UnaryOperator *E) { |
| 393 | return llvm::ConstantExpr::getNeg(Visit(E->getSubExpr())); |
| 394 | } |
| 395 | llvm::Constant *VisitUnaryNot(const UnaryOperator *E) { |
| 396 | return llvm::ConstantExpr::getNot(Visit(E->getSubExpr())); |
| 397 | } |
| 398 | llvm::Constant *VisitUnaryLNot(const UnaryOperator *E) { |
| 399 | llvm::Constant *SubExpr = Visit(E->getSubExpr()); |
| 400 | |
| 401 | if (E->getSubExpr()->getType()->isRealFloatingType()) { |
| 402 | // Compare against 0.0 for fp scalars. |
| 403 | llvm::Constant *Zero = llvm::Constant::getNullValue(SubExpr->getType()); |
| 404 | SubExpr = llvm::ConstantExpr::getFCmp(llvm::FCmpInst::FCMP_UEQ, SubExpr, |
| 405 | Zero); |
| 406 | } else { |
| 407 | assert((E->getSubExpr()->getType()->isIntegerType() || |
| 408 | E->getSubExpr()->getType()->isPointerType()) && |
| 409 | "Unknown scalar type to convert"); |
| 410 | // Compare against an integer or pointer null. |
| 411 | llvm::Constant *Zero = llvm::Constant::getNullValue(SubExpr->getType()); |
| 412 | SubExpr = llvm::ConstantExpr::getICmp(llvm::ICmpInst::ICMP_EQ, SubExpr, |
| 413 | Zero); |
| 414 | } |
| 415 | |
| 416 | return llvm::ConstantExpr::getZExt(SubExpr, ConvertType(E->getType())); |
| 417 | } |
| 418 | llvm::Constant *VisitUnarySizeOf(const UnaryOperator *E) { |
| 419 | return EmitSizeAlignOf(E->getSubExpr()->getType(), E->getType(), true); |
| 420 | } |
| 421 | llvm::Constant *VisitUnaryAlignOf(const UnaryOperator *E) { |
| 422 | return EmitSizeAlignOf(E->getSubExpr()->getType(), E->getType(), false); |
| 423 | } |
Anders Carlsson | e3d3b7d | 2008-01-26 02:08:50 +0000 | [diff] [blame] | 424 | llvm::Constant *VisitUnaryAddrOf(const UnaryOperator *E) { |
| 425 | return EmitLValue(E->getSubExpr()); |
| 426 | } |
Anders Carlsson | 5a1deb8 | 2008-01-29 15:56:48 +0000 | [diff] [blame] | 427 | llvm::Constant *VisitUnaryOffsetOf(const UnaryOperator *E) { |
| 428 | int64_t Val = E->evaluateOffsetOf(CGM.getContext()); |
| 429 | |
| 430 | assert(E->getType()->isIntegerType() && "Result type must be an integer!"); |
| 431 | |
Chris Lattner | 98be494 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 432 | uint32_t ResultWidth = |
| 433 | static_cast<uint32_t>(CGM.getContext().getTypeSize(E->getType())); |
Anders Carlsson | 5a1deb8 | 2008-01-29 15:56:48 +0000 | [diff] [blame] | 434 | return llvm::ConstantInt::get(llvm::APInt(ResultWidth, Val)); |
| 435 | } |
Eli Friedman | bc5ed6e | 2008-05-29 11:22:45 +0000 | [diff] [blame] | 436 | |
| 437 | llvm::Constant *VisitUnaryExtension(const UnaryOperator *E) { |
| 438 | return Visit(E->getSubExpr()); |
| 439 | } |
Anders Carlsson | 3b1d57b | 2008-01-26 01:36:00 +0000 | [diff] [blame] | 440 | |
Anders Carlsson | ce6237e | 2008-01-29 01:33:32 +0000 | [diff] [blame] | 441 | // Binary operators |
| 442 | llvm::Constant *VisitBinOr(const BinaryOperator *E) { |
| 443 | llvm::Constant *LHS = Visit(E->getLHS()); |
| 444 | llvm::Constant *RHS = Visit(E->getRHS()); |
| 445 | |
| 446 | return llvm::ConstantExpr::getOr(LHS, RHS); |
| 447 | } |
Anders Carlsson | 7be3d01 | 2008-02-05 20:54:21 +0000 | [diff] [blame] | 448 | llvm::Constant *VisitBinSub(const BinaryOperator *E) { |
| 449 | llvm::Constant *LHS = Visit(E->getLHS()); |
| 450 | llvm::Constant *RHS = Visit(E->getRHS()); |
| 451 | |
| 452 | if (!isa<llvm::PointerType>(RHS->getType())) { |
| 453 | // pointer - int |
| 454 | if (isa<llvm::PointerType>(LHS->getType())) { |
| 455 | llvm::Constant *Idx = llvm::ConstantExpr::getNeg(RHS); |
| 456 | |
| 457 | return llvm::ConstantExpr::getGetElementPtr(LHS, &Idx, 1); |
| 458 | } |
| 459 | |
| 460 | // int - int |
| 461 | return llvm::ConstantExpr::getSub(LHS, RHS); |
| 462 | } |
Nuno Lopes | 77e1dfc | 2008-06-06 16:41:08 +0000 | [diff] [blame] | 463 | |
| 464 | assert(isa<llvm::PointerType>(LHS->getType())); |
| 465 | |
| 466 | const llvm::Type *ResultType = ConvertType(E->getType()); |
| 467 | const QualType Type = E->getLHS()->getType(); |
| 468 | const QualType ElementType = Type->getAsPointerType()->getPointeeType(); |
| 469 | |
| 470 | LHS = llvm::ConstantExpr::getPtrToInt(LHS, ResultType); |
| 471 | RHS = llvm::ConstantExpr::getPtrToInt(RHS, ResultType); |
| 472 | |
| 473 | llvm::Constant *sub = llvm::ConstantExpr::getSub(LHS, RHS); |
| 474 | llvm::Constant *size = EmitSizeAlignOf(ElementType, E->getType(), true); |
| 475 | return llvm::ConstantExpr::getSDiv(sub, size); |
Anders Carlsson | 7be3d01 | 2008-02-05 20:54:21 +0000 | [diff] [blame] | 476 | } |
| 477 | |
| 478 | llvm::Constant *VisitBinShl(const BinaryOperator *E) { |
| 479 | llvm::Constant *LHS = Visit(E->getLHS()); |
| 480 | llvm::Constant *RHS = Visit(E->getRHS()); |
Anders Carlsson | ce6237e | 2008-01-29 01:33:32 +0000 | [diff] [blame] | 481 | |
Anders Carlsson | 7be3d01 | 2008-02-05 20:54:21 +0000 | [diff] [blame] | 482 | // LLVM requires the LHS and RHS to be the same type: promote or truncate the |
| 483 | // RHS to the same size as the LHS. |
| 484 | if (LHS->getType() != RHS->getType()) |
| 485 | RHS = llvm::ConstantExpr::getIntegerCast(RHS, LHS->getType(), false); |
| 486 | |
| 487 | return llvm::ConstantExpr::getShl(LHS, RHS); |
| 488 | } |
| 489 | |
| 490 | llvm::Constant *VisitBinMul(const BinaryOperator *E) { |
| 491 | llvm::Constant *LHS = Visit(E->getLHS()); |
| 492 | llvm::Constant *RHS = Visit(E->getRHS()); |
| 493 | |
| 494 | return llvm::ConstantExpr::getMul(LHS, RHS); |
| 495 | } |
| 496 | |
| 497 | llvm::Constant *VisitBinDiv(const BinaryOperator *E) { |
| 498 | llvm::Constant *LHS = Visit(E->getLHS()); |
| 499 | llvm::Constant *RHS = Visit(E->getRHS()); |
| 500 | |
| 501 | if (LHS->getType()->isFPOrFPVector()) |
| 502 | return llvm::ConstantExpr::getFDiv(LHS, RHS); |
| 503 | else if (E->getType()->isUnsignedIntegerType()) |
| 504 | return llvm::ConstantExpr::getUDiv(LHS, RHS); |
| 505 | else |
| 506 | return llvm::ConstantExpr::getSDiv(LHS, RHS); |
| 507 | } |
| 508 | |
| 509 | llvm::Constant *VisitBinAdd(const BinaryOperator *E) { |
| 510 | llvm::Constant *LHS = Visit(E->getLHS()); |
| 511 | llvm::Constant *RHS = Visit(E->getRHS()); |
| 512 | |
| 513 | if (!E->getType()->isPointerType()) |
| 514 | return llvm::ConstantExpr::getAdd(LHS, RHS); |
| 515 | |
Anders Carlsson | 87a9875 | 2008-02-06 07:23:14 +0000 | [diff] [blame] | 516 | llvm::Constant *Ptr, *Idx; |
| 517 | if (isa<llvm::PointerType>(LHS->getType())) { // pointer + int |
| 518 | Ptr = LHS; |
| 519 | Idx = RHS; |
| 520 | } else { // int + pointer |
| 521 | Ptr = RHS; |
| 522 | Idx = LHS; |
| 523 | } |
| 524 | |
| 525 | return llvm::ConstantExpr::getGetElementPtr(Ptr, &Idx, 1); |
Anders Carlsson | 7be3d01 | 2008-02-05 20:54:21 +0000 | [diff] [blame] | 526 | } |
| 527 | |
| 528 | llvm::Constant *VisitBinAnd(const BinaryOperator *E) { |
| 529 | llvm::Constant *LHS = Visit(E->getLHS()); |
| 530 | llvm::Constant *RHS = Visit(E->getRHS()); |
| 531 | |
| 532 | return llvm::ConstantExpr::getAnd(LHS, RHS); |
| 533 | } |
Eli Friedman | bc5ed6e | 2008-05-29 11:22:45 +0000 | [diff] [blame] | 534 | |
Eli Friedman | b896596 | 2008-05-29 15:17:49 +0000 | [diff] [blame] | 535 | llvm::Constant *EmitCmp(const BinaryOperator *E, |
| 536 | llvm::CmpInst::Predicate SignedPred, |
| 537 | llvm::CmpInst::Predicate UnsignedPred, |
| 538 | llvm::CmpInst::Predicate FloatPred) { |
Eli Friedman | bc5ed6e | 2008-05-29 11:22:45 +0000 | [diff] [blame] | 539 | llvm::Constant *LHS = Visit(E->getLHS()); |
| 540 | llvm::Constant *RHS = Visit(E->getRHS()); |
Eli Friedman | b896596 | 2008-05-29 15:17:49 +0000 | [diff] [blame] | 541 | llvm::Constant *Result; |
| 542 | if (LHS->getType()->isInteger() || |
| 543 | isa<llvm::PointerType>(LHS->getType())) { |
| 544 | if (E->getLHS()->getType()->isSignedIntegerType()) |
| 545 | Result = llvm::ConstantExpr::getICmp(SignedPred, LHS, RHS); |
| 546 | else |
| 547 | Result = llvm::ConstantExpr::getICmp(UnsignedPred, LHS, RHS); |
| 548 | } else if (LHS->getType()->isFloatingPoint()) { |
| 549 | Result = llvm::ConstantExpr::getFCmp(FloatPred, LHS, RHS); |
| 550 | } else { |
| 551 | CGM.WarnUnsupported(E, "constant expression"); |
| 552 | Result = llvm::ConstantInt::getFalse(); |
| 553 | } |
Eli Friedman | bc5ed6e | 2008-05-29 11:22:45 +0000 | [diff] [blame] | 554 | |
| 555 | const llvm::Type* ResultType = ConvertType(E->getType()); |
Eli Friedman | bc5ed6e | 2008-05-29 11:22:45 +0000 | [diff] [blame] | 556 | return llvm::ConstantExpr::getZExtOrBitCast(Result, ResultType); |
| 557 | } |
| 558 | |
Eli Friedman | b896596 | 2008-05-29 15:17:49 +0000 | [diff] [blame] | 559 | llvm::Constant *VisitBinNE(const BinaryOperator *E) { |
| 560 | return EmitCmp(E, llvm::CmpInst::ICMP_NE, llvm::CmpInst::ICMP_NE, |
| 561 | llvm::CmpInst::FCMP_ONE); |
| 562 | } |
| 563 | |
| 564 | llvm::Constant *VisitBinEQ(const BinaryOperator *E) { |
| 565 | return EmitCmp(E, llvm::CmpInst::ICMP_EQ, llvm::CmpInst::ICMP_EQ, |
| 566 | llvm::CmpInst::FCMP_OEQ); |
| 567 | } |
| 568 | |
| 569 | llvm::Constant *VisitBinLT(const BinaryOperator *E) { |
| 570 | return EmitCmp(E, llvm::CmpInst::ICMP_SLT, llvm::CmpInst::ICMP_ULT, |
| 571 | llvm::CmpInst::FCMP_OLT); |
| 572 | } |
| 573 | |
| 574 | llvm::Constant *VisitBinLE(const BinaryOperator *E) { |
| 575 | return EmitCmp(E, llvm::CmpInst::ICMP_SLE, llvm::CmpInst::ICMP_ULE, |
| 576 | llvm::CmpInst::FCMP_OLE); |
| 577 | } |
| 578 | |
| 579 | llvm::Constant *VisitBinGT(const BinaryOperator *E) { |
| 580 | return EmitCmp(E, llvm::CmpInst::ICMP_SGT, llvm::CmpInst::ICMP_UGT, |
| 581 | llvm::CmpInst::FCMP_OGT); |
| 582 | } |
| 583 | |
| 584 | llvm::Constant *VisitBinGE(const BinaryOperator *E) { |
| 585 | return EmitCmp(E, llvm::CmpInst::ICMP_SGE, llvm::CmpInst::ICMP_SGE, |
| 586 | llvm::CmpInst::FCMP_OGE); |
| 587 | } |
| 588 | |
Eli Friedman | bc5ed6e | 2008-05-29 11:22:45 +0000 | [diff] [blame] | 589 | llvm::Constant *VisitConditionalOperator(const ConditionalOperator *E) { |
| 590 | llvm::Constant *Cond = Visit(E->getCond()); |
| 591 | llvm::Constant *CondVal = EmitConversionToBool(Cond, E->getType()); |
| 592 | llvm::ConstantInt *CondValInt = dyn_cast<llvm::ConstantInt>(CondVal); |
| 593 | if (!CondValInt) { |
| 594 | CGM.WarnUnsupported(E, "constant expression"); |
| 595 | return llvm::Constant::getNullValue(ConvertType(E->getType())); |
| 596 | } |
| 597 | if (CondValInt->isOne()) { |
| 598 | if (E->getLHS()) |
| 599 | return Visit(E->getLHS()); |
| 600 | return Cond; |
| 601 | } |
| 602 | |
| 603 | return Visit(E->getRHS()); |
| 604 | } |
Anders Carlsson | 7be3d01 | 2008-02-05 20:54:21 +0000 | [diff] [blame] | 605 | |
Anders Carlsson | 3b1d57b | 2008-01-26 01:36:00 +0000 | [diff] [blame] | 606 | // Utility methods |
| 607 | const llvm::Type *ConvertType(QualType T) { |
| 608 | return CGM.getTypes().ConvertType(T); |
| 609 | } |
| 610 | |
| 611 | llvm::Constant *EmitConversionToBool(llvm::Constant *Src, QualType SrcType) { |
| 612 | assert(SrcType->isCanonical() && "EmitConversion strips typedefs"); |
| 613 | |
| 614 | if (SrcType->isRealFloatingType()) { |
| 615 | // Compare against 0.0 for fp scalars. |
| 616 | llvm::Constant *Zero = llvm::Constant::getNullValue(Src->getType()); |
| 617 | return llvm::ConstantExpr::getFCmp(llvm::FCmpInst::FCMP_UNE, Src, Zero); |
| 618 | } |
| 619 | |
| 620 | assert((SrcType->isIntegerType() || SrcType->isPointerType()) && |
| 621 | "Unknown scalar type to convert"); |
| 622 | |
| 623 | // Compare against an integer or pointer null. |
| 624 | llvm::Constant *Zero = llvm::Constant::getNullValue(Src->getType()); |
| 625 | return llvm::ConstantExpr::getICmp(llvm::ICmpInst::ICMP_NE, Src, Zero); |
| 626 | } |
| 627 | |
| 628 | llvm::Constant *EmitConversion(llvm::Constant *Src, QualType SrcType, |
| 629 | QualType DstType) { |
Chris Lattner | d394cc6 | 2008-07-26 22:39:33 +0000 | [diff] [blame] | 630 | SrcType = CGM.getContext().getCanonicalType(SrcType); |
| 631 | DstType = CGM.getContext().getCanonicalType(DstType); |
Anders Carlsson | 3b1d57b | 2008-01-26 01:36:00 +0000 | [diff] [blame] | 632 | if (SrcType == DstType) return Src; |
| 633 | |
| 634 | // Handle conversions to bool first, they are special: comparisons against 0. |
| 635 | if (DstType->isBooleanType()) |
| 636 | return EmitConversionToBool(Src, SrcType); |
| 637 | |
| 638 | const llvm::Type *DstTy = ConvertType(DstType); |
| 639 | |
| 640 | // Ignore conversions like int -> uint. |
| 641 | if (Src->getType() == DstTy) |
| 642 | return Src; |
| 643 | |
| 644 | // Handle pointer conversions next: pointers can only be converted to/from |
| 645 | // other pointers and integers. |
Eli Friedman | c53296e | 2008-06-07 17:27:51 +0000 | [diff] [blame] | 646 | if (isa<llvm::PointerType>(DstTy)) { |
Anders Carlsson | 3b1d57b | 2008-01-26 01:36:00 +0000 | [diff] [blame] | 647 | // The source value may be an integer, or a pointer. |
| 648 | if (isa<llvm::PointerType>(Src->getType())) |
| 649 | return llvm::ConstantExpr::getBitCast(Src, DstTy); |
Anders Carlsson | 84005b4 | 2008-01-26 04:30:23 +0000 | [diff] [blame] | 650 | assert(SrcType->isIntegerType() &&"Not ptr->ptr or int->ptr conversion?"); |
Anders Carlsson | 3b1d57b | 2008-01-26 01:36:00 +0000 | [diff] [blame] | 651 | return llvm::ConstantExpr::getIntToPtr(Src, DstTy); |
| 652 | } |
| 653 | |
Eli Friedman | c53296e | 2008-06-07 17:27:51 +0000 | [diff] [blame] | 654 | if (isa<llvm::PointerType>(Src->getType())) { |
Anders Carlsson | 3b1d57b | 2008-01-26 01:36:00 +0000 | [diff] [blame] | 655 | // Must be an ptr to int cast. |
| 656 | assert(isa<llvm::IntegerType>(DstTy) && "not ptr->int?"); |
| 657 | return llvm::ConstantExpr::getPtrToInt(Src, DstTy); |
| 658 | } |
| 659 | |
| 660 | // A scalar source can be splatted to a vector of the same element type |
| 661 | if (isa<llvm::VectorType>(DstTy) && !isa<VectorType>(SrcType)) { |
| 662 | const llvm::VectorType *VT = cast<llvm::VectorType>(DstTy); |
| 663 | assert((VT->getElementType() == Src->getType()) && |
| 664 | "Vector element type must match scalar type to splat."); |
| 665 | unsigned NumElements = DstType->getAsVectorType()->getNumElements(); |
| 666 | llvm::SmallVector<llvm::Constant*, 16> Elements; |
| 667 | for (unsigned i = 0; i < NumElements; i++) |
| 668 | Elements.push_back(Src); |
| 669 | |
| 670 | return llvm::ConstantVector::get(&Elements[0], NumElements); |
| 671 | } |
| 672 | |
| 673 | if (isa<llvm::VectorType>(Src->getType()) || |
| 674 | isa<llvm::VectorType>(DstTy)) { |
| 675 | return llvm::ConstantExpr::getBitCast(Src, DstTy); |
| 676 | } |
| 677 | |
| 678 | // Finally, we have the arithmetic types: real int/float. |
| 679 | if (isa<llvm::IntegerType>(Src->getType())) { |
| 680 | bool InputSigned = SrcType->isSignedIntegerType(); |
| 681 | if (isa<llvm::IntegerType>(DstTy)) |
| 682 | return llvm::ConstantExpr::getIntegerCast(Src, DstTy, InputSigned); |
| 683 | else if (InputSigned) |
| 684 | return llvm::ConstantExpr::getSIToFP(Src, DstTy); |
| 685 | else |
| 686 | return llvm::ConstantExpr::getUIToFP(Src, DstTy); |
| 687 | } |
| 688 | |
| 689 | assert(Src->getType()->isFloatingPoint() && "Unknown real conversion"); |
| 690 | if (isa<llvm::IntegerType>(DstTy)) { |
| 691 | if (DstType->isSignedIntegerType()) |
| 692 | return llvm::ConstantExpr::getFPToSI(Src, DstTy); |
| 693 | else |
| 694 | return llvm::ConstantExpr::getFPToUI(Src, DstTy); |
| 695 | } |
| 696 | |
| 697 | assert(DstTy->isFloatingPoint() && "Unknown real conversion"); |
| 698 | if (DstTy->getTypeID() < Src->getType()->getTypeID()) |
| 699 | return llvm::ConstantExpr::getFPTrunc(Src, DstTy); |
| 700 | else |
| 701 | return llvm::ConstantExpr::getFPExtend(Src, DstTy); |
| 702 | } |
| 703 | |
| 704 | llvm::Constant *EmitSizeAlignOf(QualType TypeToSize, |
| 705 | QualType RetType, bool isSizeOf) { |
| 706 | std::pair<uint64_t, unsigned> Info = |
Chris Lattner | 98be494 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 707 | CGM.getContext().getTypeInfo(TypeToSize); |
Anders Carlsson | 3b1d57b | 2008-01-26 01:36:00 +0000 | [diff] [blame] | 708 | |
| 709 | uint64_t Val = isSizeOf ? Info.first : Info.second; |
| 710 | Val /= 8; // Return size in bytes, not bits. |
| 711 | |
| 712 | assert(RetType->isIntegerType() && "Result type must be an integer!"); |
| 713 | |
Chris Lattner | 98be494 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 714 | uint32_t ResultWidth = |
| 715 | static_cast<uint32_t>(CGM.getContext().getTypeSize(RetType)); |
Anders Carlsson | 3b1d57b | 2008-01-26 01:36:00 +0000 | [diff] [blame] | 716 | return llvm::ConstantInt::get(llvm::APInt(ResultWidth, Val)); |
| 717 | } |
Anders Carlsson | 84005b4 | 2008-01-26 04:30:23 +0000 | [diff] [blame] | 718 | |
| 719 | llvm::Constant *EmitLValue(Expr *E) { |
Anders Carlsson | e3d3b7d | 2008-01-26 02:08:50 +0000 | [diff] [blame] | 720 | switch (E->getStmtClass()) { |
Eli Friedman | 546d94c | 2008-02-11 00:23:10 +0000 | [diff] [blame] | 721 | default: break; |
Anders Carlsson | e3d3b7d | 2008-01-26 02:08:50 +0000 | [diff] [blame] | 722 | case Expr::ParenExprClass: |
| 723 | // Elide parenthesis |
| 724 | return EmitLValue(cast<ParenExpr>(E)->getSubExpr()); |
| 725 | case Expr::CompoundLiteralExprClass: { |
| 726 | // Note that due to the nature of compound literals, this is guaranteed |
| 727 | // 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] | 728 | CompoundLiteralExpr *CLE = cast<CompoundLiteralExpr>(E); |
| 729 | llvm::Constant* C = Visit(CLE->getInitializer()); |
Chris Lattner | a7ad98f | 2008-02-11 00:02:17 +0000 | [diff] [blame] | 730 | C = new llvm::GlobalVariable(C->getType(),E->getType().isConstQualified(), |
Anders Carlsson | e3d3b7d | 2008-01-26 02:08:50 +0000 | [diff] [blame] | 731 | llvm::GlobalValue::InternalLinkage, |
| 732 | C, ".compoundliteral", &CGM.getModule()); |
| 733 | return C; |
Anders Carlsson | 84005b4 | 2008-01-26 04:30:23 +0000 | [diff] [blame] | 734 | } |
Anders Carlsson | e3d3b7d | 2008-01-26 02:08:50 +0000 | [diff] [blame] | 735 | case Expr::DeclRefExprClass: { |
Anders Carlsson | 84005b4 | 2008-01-26 04:30:23 +0000 | [diff] [blame] | 736 | ValueDecl *Decl = cast<DeclRefExpr>(E)->getDecl(); |
Anders Carlsson | e3d3b7d | 2008-01-26 02:08:50 +0000 | [diff] [blame] | 737 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(Decl)) |
Daniel Dunbar | bd012ff | 2008-07-29 23:18:29 +0000 | [diff] [blame^] | 738 | return CGM.GetAddrOfFunction(FD); |
Steve Naroff | 248a753 | 2008-04-15 22:42:06 +0000 | [diff] [blame] | 739 | if (const VarDecl* VD = dyn_cast<VarDecl>(Decl)) { |
| 740 | if (VD->isFileVarDecl()) |
Daniel Dunbar | bd012ff | 2008-07-29 23:18:29 +0000 | [diff] [blame^] | 741 | return CGM.GetAddrOfGlobalVar(VD); |
Steve Naroff | 248a753 | 2008-04-15 22:42:06 +0000 | [diff] [blame] | 742 | else if (VD->isBlockVarDecl()) { |
| 743 | assert(CGF && "Can't access static local vars without CGF"); |
| 744 | return CGF->GetAddrOfStaticLocalVar(VD); |
| 745 | } |
Lauro Ramos Venancio | 8137335 | 2008-02-26 21:41:45 +0000 | [diff] [blame] | 746 | } |
Eli Friedman | 546d94c | 2008-02-11 00:23:10 +0000 | [diff] [blame] | 747 | break; |
Anders Carlsson | e3d3b7d | 2008-01-26 02:08:50 +0000 | [diff] [blame] | 748 | } |
| 749 | case Expr::MemberExprClass: { |
Anders Carlsson | 84005b4 | 2008-01-26 04:30:23 +0000 | [diff] [blame] | 750 | MemberExpr* ME = cast<MemberExpr>(E); |
Anders Carlsson | 84005b4 | 2008-01-26 04:30:23 +0000 | [diff] [blame] | 751 | llvm::Constant *Base; |
| 752 | if (ME->isArrow()) |
| 753 | Base = Visit(ME->getBase()); |
| 754 | else |
| 755 | Base = EmitLValue(ME->getBase()); |
Lauro Ramos Venancio | 5bfe18c | 2008-01-30 21:23:20 +0000 | [diff] [blame] | 756 | |
| 757 | unsigned FieldNumber = CGM.getTypes().getLLVMFieldNo(ME->getMemberDecl()); |
Anders Carlsson | e3d3b7d | 2008-01-26 02:08:50 +0000 | [diff] [blame] | 758 | llvm::Constant *Zero = llvm::ConstantInt::get(llvm::Type::Int32Ty, 0); |
| 759 | llvm::Constant *Idx = llvm::ConstantInt::get(llvm::Type::Int32Ty, |
| 760 | FieldNumber); |
| 761 | llvm::Value *Ops[] = {Zero, Idx}; |
| 762 | return llvm::ConstantExpr::getGetElementPtr(Base, Ops, 2); |
| 763 | } |
| 764 | case Expr::ArraySubscriptExprClass: { |
Anders Carlsson | 84005b4 | 2008-01-26 04:30:23 +0000 | [diff] [blame] | 765 | ArraySubscriptExpr* ASExpr = cast<ArraySubscriptExpr>(E); |
| 766 | llvm::Constant *Base = Visit(ASExpr->getBase()); |
| 767 | llvm::Constant *Index = Visit(ASExpr->getIdx()); |
Anders Carlsson | e3d3b7d | 2008-01-26 02:08:50 +0000 | [diff] [blame] | 768 | assert(!ASExpr->getBase()->getType()->isVectorType() && |
| 769 | "Taking the address of a vector component is illegal!"); |
| 770 | return llvm::ConstantExpr::getGetElementPtr(Base, &Index, 1); |
| 771 | } |
| 772 | case Expr::StringLiteralClass: { |
Anders Carlsson | 84005b4 | 2008-01-26 04:30:23 +0000 | [diff] [blame] | 773 | StringLiteral *String = cast<StringLiteral>(E); |
Anders Carlsson | e3d3b7d | 2008-01-26 02:08:50 +0000 | [diff] [blame] | 774 | assert(!String->isWide() && "Cannot codegen wide strings yet"); |
Anders Carlsson | 84005b4 | 2008-01-26 04:30:23 +0000 | [diff] [blame] | 775 | const char *StrData = String->getStrData(); |
| 776 | unsigned Len = String->getByteLength(); |
Anders Carlsson | e3d3b7d | 2008-01-26 02:08:50 +0000 | [diff] [blame] | 777 | |
Anders Carlsson | 84005b4 | 2008-01-26 04:30:23 +0000 | [diff] [blame] | 778 | return CGM.GetAddrOfConstantString(std::string(StrData, StrData + Len)); |
| 779 | } |
| 780 | case Expr::UnaryOperatorClass: { |
| 781 | UnaryOperator *Exp = cast<UnaryOperator>(E); |
| 782 | switch (Exp->getOpcode()) { |
Eli Friedman | 546d94c | 2008-02-11 00:23:10 +0000 | [diff] [blame] | 783 | default: break; |
| 784 | case UnaryOperator::Extension: |
Anders Carlsson | 84005b4 | 2008-01-26 04:30:23 +0000 | [diff] [blame] | 785 | // Extension is just a wrapper for expressions |
| 786 | return EmitLValue(Exp->getSubExpr()); |
| 787 | case UnaryOperator::Real: |
| 788 | case UnaryOperator::Imag: { |
| 789 | // The address of __real or __imag is just a GEP off the address |
| 790 | // of the internal expression |
| 791 | llvm::Constant* C = EmitLValue(Exp->getSubExpr()); |
| 792 | llvm::Constant *Zero = llvm::ConstantInt::get(llvm::Type::Int32Ty, 0); |
| 793 | llvm::Constant *Idx = llvm::ConstantInt::get(llvm::Type::Int32Ty, |
Anders Carlsson | e3d3b7d | 2008-01-26 02:08:50 +0000 | [diff] [blame] | 794 | Exp->getOpcode() == UnaryOperator::Imag); |
Anders Carlsson | 84005b4 | 2008-01-26 04:30:23 +0000 | [diff] [blame] | 795 | llvm::Value *Ops[] = {Zero, Idx}; |
| 796 | return llvm::ConstantExpr::getGetElementPtr(C, Ops, 2); |
| 797 | } |
| 798 | case UnaryOperator::Deref: |
| 799 | // The address of a deref is just the value of the expression |
| 800 | return Visit(Exp->getSubExpr()); |
| 801 | } |
Eli Friedman | 546d94c | 2008-02-11 00:23:10 +0000 | [diff] [blame] | 802 | break; |
Anders Carlsson | e3d3b7d | 2008-01-26 02:08:50 +0000 | [diff] [blame] | 803 | } |
Eli Friedman | 546d94c | 2008-02-11 00:23:10 +0000 | [diff] [blame] | 804 | } |
| 805 | CGM.WarnUnsupported(E, "constant l-value expression"); |
| 806 | llvm::Type *Ty = llvm::PointerType::getUnqual(ConvertType(E->getType())); |
| 807 | return llvm::UndefValue::get(Ty); |
Anders Carlsson | e3d3b7d | 2008-01-26 02:08:50 +0000 | [diff] [blame] | 808 | } |
Anders Carlsson | e3d3b7d | 2008-01-26 02:08:50 +0000 | [diff] [blame] | 809 | |
| 810 | }; |
| 811 | |
Anders Carlsson | 3b1d57b | 2008-01-26 01:36:00 +0000 | [diff] [blame] | 812 | } // end anonymous namespace. |
| 813 | |
| 814 | |
Lauro Ramos Venancio | 8137335 | 2008-02-26 21:41:45 +0000 | [diff] [blame] | 815 | llvm::Constant *CodeGenModule::EmitConstantExpr(const Expr *E, |
Chris Lattner | 9619662 | 2008-07-26 22:37:01 +0000 | [diff] [blame] | 816 | CodeGenFunction *CGF) { |
| 817 | QualType type = Context.getCanonicalType(E->getType()); |
Eli Friedman | 7dfa639 | 2008-06-01 15:31:44 +0000 | [diff] [blame] | 818 | |
Anders Carlsson | 3b1d57b | 2008-01-26 01:36:00 +0000 | [diff] [blame] | 819 | if (type->isIntegerType()) { |
Chris Lattner | 98be494 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 820 | llvm::APSInt Value(static_cast<uint32_t>(Context.getTypeSize(type))); |
Anders Carlsson | 3b1d57b | 2008-01-26 01:36:00 +0000 | [diff] [blame] | 821 | if (E->isIntegerConstantExpr(Value, Context)) { |
| 822 | return llvm::ConstantInt::get(Value); |
| 823 | } |
| 824 | } |
Eli Friedman | 7dfa639 | 2008-06-01 15:31:44 +0000 | [diff] [blame] | 825 | |
| 826 | llvm::Constant* C = ConstExprEmitter(*this, CGF).Visit(const_cast<Expr*>(E)); |
| 827 | if (C->getType() == llvm::Type::Int1Ty) { |
| 828 | const llvm::Type *BoolTy = getTypes().ConvertTypeForMem(E->getType()); |
| 829 | C = llvm::ConstantExpr::getZExt(C, BoolTy); |
| 830 | } |
| 831 | return C; |
Anders Carlsson | 3b1d57b | 2008-01-26 01:36:00 +0000 | [diff] [blame] | 832 | } |