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