Anders Carlsson | 3b1d57b | 2008-01-26 01:36:00 +0000 | [diff] [blame] | 1 | //===--- CGExprConstant.cpp - Emit LLVM Code from Constant Expressions ----===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This contains code to emit Constant Expr nodes as LLVM code. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "CodeGenFunction.h" |
| 15 | #include "CodeGenModule.h" |
Daniel Dunbar | af2f62c | 2008-08-13 00:59:25 +0000 | [diff] [blame] | 16 | #include "CGObjCRuntime.h" |
Chris Lattner | f6b24ea | 2008-10-06 05:59:01 +0000 | [diff] [blame] | 17 | #include "clang/AST/APValue.h" |
Daniel Dunbar | de7fb84 | 2008-08-11 05:00:27 +0000 | [diff] [blame] | 18 | #include "clang/AST/ASTContext.h" |
| 19 | #include "clang/AST/StmtVisitor.h" |
Anders Carlsson | 3b1d57b | 2008-01-26 01:36:00 +0000 | [diff] [blame] | 20 | #include "llvm/Constants.h" |
| 21 | #include "llvm/Function.h" |
| 22 | #include "llvm/GlobalVariable.h" |
| 23 | #include "llvm/Support/Compiler.h" |
Eli Friedman | a823400 | 2008-05-30 10:24:46 +0000 | [diff] [blame] | 24 | #include "llvm/Target/TargetData.h" |
Anders Carlsson | 3b1d57b | 2008-01-26 01:36:00 +0000 | [diff] [blame] | 25 | using namespace clang; |
| 26 | using namespace CodeGen; |
| 27 | |
| 28 | namespace { |
Anders Carlsson | 84005b4 | 2008-01-26 04:30:23 +0000 | [diff] [blame] | 29 | class VISIBILITY_HIDDEN ConstExprEmitter : |
| 30 | public StmtVisitor<ConstExprEmitter, llvm::Constant*> { |
Anders Carlsson | 3b1d57b | 2008-01-26 01:36:00 +0000 | [diff] [blame] | 31 | CodeGenModule &CGM; |
Lauro Ramos Venancio | 8137335 | 2008-02-26 21:41:45 +0000 | [diff] [blame] | 32 | CodeGenFunction *CGF; |
Anders Carlsson | 3b1d57b | 2008-01-26 01:36:00 +0000 | [diff] [blame] | 33 | public: |
Lauro Ramos Venancio | 8137335 | 2008-02-26 21:41:45 +0000 | [diff] [blame] | 34 | ConstExprEmitter(CodeGenModule &cgm, CodeGenFunction *cgf) |
| 35 | : CGM(cgm), CGF(cgf) { |
Anders Carlsson | 3b1d57b | 2008-01-26 01:36:00 +0000 | [diff] [blame] | 36 | } |
| 37 | |
| 38 | //===--------------------------------------------------------------------===// |
| 39 | // Visitor Methods |
| 40 | //===--------------------------------------------------------------------===// |
| 41 | |
| 42 | llvm::Constant *VisitStmt(Stmt *S) { |
Daniel Dunbar | 488e993 | 2008-08-16 00:56:44 +0000 | [diff] [blame] | 43 | CGM.ErrorUnsupported(S, "constant expression"); |
Chris Lattner | 98be494 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 44 | QualType T = cast<Expr>(S)->getType(); |
| 45 | return llvm::UndefValue::get(CGM.getTypes().ConvertType(T)); |
Anders Carlsson | 3b1d57b | 2008-01-26 01:36:00 +0000 | [diff] [blame] | 46 | } |
| 47 | |
| 48 | llvm::Constant *VisitParenExpr(ParenExpr *PE) { |
| 49 | return Visit(PE->getSubExpr()); |
| 50 | } |
| 51 | |
Chris Lattner | 13fd7e5 | 2008-06-21 21:44:18 +0000 | [diff] [blame] | 52 | llvm::Constant *VisitObjCStringLiteral(const ObjCStringLiteral *E) { |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 53 | std::string S(E->getString()->getStrData(), |
| 54 | E->getString()->getByteLength()); |
| 55 | llvm::Constant *C = CGM.getObjCRuntime().GenerateConstantString(S); |
| 56 | return llvm::ConstantExpr::getBitCast(C, ConvertType(E->getType())); |
Chris Lattner | 13fd7e5 | 2008-06-21 21:44:18 +0000 | [diff] [blame] | 57 | } |
Anders Carlsson | bcf6225 | 2008-11-22 02:34:39 +0000 | [diff] [blame] | 58 | |
Anders Carlsson | 3b1d57b | 2008-01-26 01:36:00 +0000 | [diff] [blame] | 59 | llvm::Constant *VisitCompoundLiteralExpr(CompoundLiteralExpr *E) { |
| 60 | return Visit(E->getInitializer()); |
| 61 | } |
| 62 | |
Argyrios Kyrtzidis | 0835a3c | 2008-08-18 23:01:59 +0000 | [diff] [blame] | 63 | llvm::Constant *VisitCastExpr(CastExpr* E) { |
Anders Carlsson | 3b1d57b | 2008-01-26 01:36:00 +0000 | [diff] [blame] | 64 | llvm::Constant *C = Visit(E->getSubExpr()); |
| 65 | |
| 66 | return EmitConversion(C, E->getSubExpr()->getType(), E->getType()); |
| 67 | } |
Devang Patel | eae1560 | 2008-02-05 02:39:50 +0000 | [diff] [blame] | 68 | |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 69 | llvm::Constant *VisitCXXDefaultArgExpr(CXXDefaultArgExpr *DAE) { |
| 70 | return Visit(DAE->getExpr()); |
| 71 | } |
| 72 | |
Eli Friedman | a823400 | 2008-05-30 10:24:46 +0000 | [diff] [blame] | 73 | llvm::Constant *EmitArrayInitialization(InitListExpr *ILE) { |
Eli Friedman | 4d1c018 | 2008-02-21 17:45:41 +0000 | [diff] [blame] | 74 | std::vector<llvm::Constant*> Elts; |
Eli Friedman | a823400 | 2008-05-30 10:24:46 +0000 | [diff] [blame] | 75 | const llvm::ArrayType *AType = |
| 76 | cast<llvm::ArrayType>(ConvertType(ILE->getType())); |
Eli Friedman | 4d1c018 | 2008-02-21 17:45:41 +0000 | [diff] [blame] | 77 | unsigned NumInitElements = ILE->getNumInits(); |
| 78 | // FIXME: Check for wide strings |
| 79 | if (NumInitElements > 0 && isa<StringLiteral>(ILE->getInit(0)) && |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 80 | ILE->getType()->getArrayElementTypeNoTypeQual()->isCharType()) |
Eli Friedman | 4d1c018 | 2008-02-21 17:45:41 +0000 | [diff] [blame] | 81 | return Visit(ILE->getInit(0)); |
Devang Patel | eae1560 | 2008-02-05 02:39:50 +0000 | [diff] [blame] | 82 | const llvm::Type *ElemTy = AType->getElementType(); |
| 83 | unsigned NumElements = AType->getNumElements(); |
| 84 | |
| 85 | // Initialising an array requires us to automatically |
| 86 | // initialise any elements that have not been initialised explicitly |
| 87 | unsigned NumInitableElts = std::min(NumInitElements, NumElements); |
| 88 | |
| 89 | // Copy initializer elements. |
| 90 | unsigned i = 0; |
Eli Friedman | a04a153 | 2008-05-30 19:58:50 +0000 | [diff] [blame] | 91 | bool RewriteType = false; |
Devang Patel | eae1560 | 2008-02-05 02:39:50 +0000 | [diff] [blame] | 92 | for (; i < NumInitableElts; ++i) { |
Anders Carlsson | aa43256 | 2008-11-17 01:58:55 +0000 | [diff] [blame] | 93 | llvm::Constant *C = CGM.EmitConstantExpr(ILE->getInit(i), CGF); |
Eli Friedman | a04a153 | 2008-05-30 19:58:50 +0000 | [diff] [blame] | 94 | RewriteType |= (C->getType() != ElemTy); |
Devang Patel | eae1560 | 2008-02-05 02:39:50 +0000 | [diff] [blame] | 95 | Elts.push_back(C); |
| 96 | } |
Eli Friedman | a04a153 | 2008-05-30 19:58:50 +0000 | [diff] [blame] | 97 | |
Devang Patel | eae1560 | 2008-02-05 02:39:50 +0000 | [diff] [blame] | 98 | // Initialize remaining array elements. |
| 99 | for (; i < NumElements; ++i) |
| 100 | Elts.push_back(llvm::Constant::getNullValue(ElemTy)); |
| 101 | |
Eli Friedman | a04a153 | 2008-05-30 19:58:50 +0000 | [diff] [blame] | 102 | if (RewriteType) { |
| 103 | // FIXME: Try to avoid packing the array |
| 104 | std::vector<const llvm::Type*> Types; |
| 105 | for (unsigned i = 0; i < Elts.size(); ++i) |
| 106 | Types.push_back(Elts[i]->getType()); |
| 107 | const llvm::StructType *SType = llvm::StructType::get(Types, true); |
| 108 | return llvm::ConstantStruct::get(SType, Elts); |
| 109 | } |
| 110 | |
Devang Patel | eae1560 | 2008-02-05 02:39:50 +0000 | [diff] [blame] | 111 | return llvm::ConstantArray::get(AType, Elts); |
| 112 | } |
| 113 | |
Eli Friedman | a823400 | 2008-05-30 10:24:46 +0000 | [diff] [blame] | 114 | void InsertBitfieldIntoStruct(std::vector<llvm::Constant*>& Elts, |
| 115 | FieldDecl* Field, Expr* E) { |
| 116 | // Calculate the value to insert |
Anders Carlsson | aa43256 | 2008-11-17 01:58:55 +0000 | [diff] [blame] | 117 | llvm::Constant *C = CGM.EmitConstantExpr(E, CGF); |
Eli Friedman | a823400 | 2008-05-30 10:24:46 +0000 | [diff] [blame] | 118 | llvm::ConstantInt *CI = dyn_cast<llvm::ConstantInt>(C); |
| 119 | if (!CI) { |
Daniel Dunbar | 488e993 | 2008-08-16 00:56:44 +0000 | [diff] [blame] | 120 | CGM.ErrorUnsupported(E, "bitfield initialization"); |
Eli Friedman | a823400 | 2008-05-30 10:24:46 +0000 | [diff] [blame] | 121 | return; |
| 122 | } |
| 123 | llvm::APInt V = CI->getValue(); |
Devang Patel | eae1560 | 2008-02-05 02:39:50 +0000 | [diff] [blame] | 124 | |
Eli Friedman | a823400 | 2008-05-30 10:24:46 +0000 | [diff] [blame] | 125 | // Calculate information about the relevant field |
| 126 | const llvm::Type* Ty = CI->getType(); |
Daniel Dunbar | 9ca8bb0 | 2008-08-10 07:00:24 +0000 | [diff] [blame] | 127 | const llvm::TargetData &TD = CGM.getTypes().getTargetData(); |
| 128 | unsigned size = TD.getTypeStoreSizeInBits(Ty); |
Eli Friedman | a823400 | 2008-05-30 10:24:46 +0000 | [diff] [blame] | 129 | unsigned fieldOffset = CGM.getTypes().getLLVMFieldNo(Field) * size; |
| 130 | CodeGenTypes::BitFieldInfo bitFieldInfo = |
| 131 | CGM.getTypes().getBitFieldInfo(Field); |
| 132 | fieldOffset += bitFieldInfo.Begin; |
| 133 | |
| 134 | // Find where to start the insertion |
| 135 | // FIXME: This is O(n^2) in the number of bit-fields! |
| 136 | // FIXME: This won't work if the struct isn't completely packed! |
| 137 | unsigned offset = 0, i = 0; |
| 138 | while (offset < (fieldOffset & -8)) |
Daniel Dunbar | 9ca8bb0 | 2008-08-10 07:00:24 +0000 | [diff] [blame] | 139 | offset += TD.getTypeStoreSizeInBits(Elts[i++]->getType()); |
| 140 | |
| 141 | // Advance over 0 sized elements (must terminate in bounds since |
| 142 | // the bitfield must have a size). |
| 143 | while (TD.getTypeStoreSizeInBits(Elts[i]->getType()) == 0) |
| 144 | ++i; |
Eli Friedman | a823400 | 2008-05-30 10:24:46 +0000 | [diff] [blame] | 145 | |
Daniel Dunbar | 1f3846e | 2008-07-17 21:07:48 +0000 | [diff] [blame] | 146 | // Promote the size of V if necessary |
| 147 | // FIXME: This should never occur, but currently it can because |
| 148 | // initializer constants are cast to bool, and because clang is |
| 149 | // not enforcing bitfield width limits. |
| 150 | if (bitFieldInfo.Size > V.getBitWidth()) |
| 151 | V.zext(bitFieldInfo.Size); |
| 152 | |
Eli Friedman | a823400 | 2008-05-30 10:24:46 +0000 | [diff] [blame] | 153 | // Insert the bits into the struct |
| 154 | // FIXME: This algorthm is only correct on X86! |
| 155 | // FIXME: THis algorthm assumes bit-fields only have byte-size elements! |
| 156 | unsigned bitsToInsert = bitFieldInfo.Size; |
| 157 | unsigned curBits = std::min(8 - (fieldOffset & 7), bitsToInsert); |
| 158 | unsigned byte = V.getLoBits(curBits).getZExtValue() << (fieldOffset & 7); |
| 159 | do { |
| 160 | llvm::Constant* byteC = llvm::ConstantInt::get(llvm::Type::Int8Ty, byte); |
| 161 | Elts[i] = llvm::ConstantExpr::getOr(Elts[i], byteC); |
| 162 | ++i; |
| 163 | V = V.lshr(curBits); |
| 164 | bitsToInsert -= curBits; |
| 165 | |
| 166 | if (!bitsToInsert) |
| 167 | break; |
| 168 | |
| 169 | curBits = bitsToInsert > 8 ? 8 : bitsToInsert; |
| 170 | byte = V.getLoBits(curBits).getZExtValue(); |
| 171 | } while (true); |
| 172 | } |
| 173 | |
| 174 | llvm::Constant *EmitStructInitialization(InitListExpr *ILE) { |
| 175 | const llvm::StructType *SType = |
| 176 | cast<llvm::StructType>(ConvertType(ILE->getType())); |
Eli Friedman | 017cbdf | 2008-05-20 00:11:07 +0000 | [diff] [blame] | 177 | RecordDecl *RD = ILE->getType()->getAsRecordType()->getDecl(); |
Devang Patel | eae1560 | 2008-02-05 02:39:50 +0000 | [diff] [blame] | 178 | std::vector<llvm::Constant*> Elts; |
Devang Patel | eae1560 | 2008-02-05 02:39:50 +0000 | [diff] [blame] | 179 | |
Eli Friedman | 017cbdf | 2008-05-20 00:11:07 +0000 | [diff] [blame] | 180 | // Initialize the whole structure to zero. |
| 181 | for (unsigned i = 0; i < SType->getNumElements(); ++i) { |
Devang Patel | eae1560 | 2008-02-05 02:39:50 +0000 | [diff] [blame] | 182 | const llvm::Type *FieldTy = SType->getElementType(i); |
| 183 | Elts.push_back(llvm::Constant::getNullValue(FieldTy)); |
| 184 | } |
Eli Friedman | 017cbdf | 2008-05-20 00:11:07 +0000 | [diff] [blame] | 185 | |
| 186 | // Copy initializer elements. Skip padding fields. |
| 187 | unsigned EltNo = 0; // Element no in ILE |
| 188 | int FieldNo = 0; // Field no in RecordDecl |
Eli Friedman | a04a153 | 2008-05-30 19:58:50 +0000 | [diff] [blame] | 189 | bool RewriteType = false; |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 190 | for (RecordDecl::field_iterator Field = RD->field_begin(), |
| 191 | FieldEnd = RD->field_end(); |
| 192 | EltNo < ILE->getNumInits() && Field != FieldEnd; ++Field) { |
Eli Friedman | 017cbdf | 2008-05-20 00:11:07 +0000 | [diff] [blame] | 193 | FieldNo++; |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 194 | if (!Field->getIdentifier()) |
Eli Friedman | 017cbdf | 2008-05-20 00:11:07 +0000 | [diff] [blame] | 195 | continue; |
| 196 | |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 197 | if (Field->isBitField()) { |
| 198 | InsertBitfieldIntoStruct(Elts, *Field, ILE->getInit(EltNo)); |
Eli Friedman | 017cbdf | 2008-05-20 00:11:07 +0000 | [diff] [blame] | 199 | } else { |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 200 | unsigned FieldNo = CGM.getTypes().getLLVMFieldNo(*Field); |
Anders Carlsson | aa43256 | 2008-11-17 01:58:55 +0000 | [diff] [blame] | 201 | llvm::Constant *C = CGM.EmitConstantExpr(ILE->getInit(EltNo), CGF); |
Eli Friedman | a04a153 | 2008-05-30 19:58:50 +0000 | [diff] [blame] | 202 | RewriteType |= (C->getType() != Elts[FieldNo]->getType()); |
| 203 | Elts[FieldNo] = C; |
Eli Friedman | 017cbdf | 2008-05-20 00:11:07 +0000 | [diff] [blame] | 204 | } |
| 205 | EltNo++; |
| 206 | } |
| 207 | |
Eli Friedman | a04a153 | 2008-05-30 19:58:50 +0000 | [diff] [blame] | 208 | if (RewriteType) { |
| 209 | // FIXME: Make this work for non-packed structs |
| 210 | assert(SType->isPacked() && "Cannot recreate unpacked structs"); |
| 211 | std::vector<const llvm::Type*> Types; |
| 212 | for (unsigned i = 0; i < Elts.size(); ++i) |
| 213 | Types.push_back(Elts[i]->getType()); |
| 214 | SType = llvm::StructType::get(Types, true); |
| 215 | } |
| 216 | |
Devang Patel | eae1560 | 2008-02-05 02:39:50 +0000 | [diff] [blame] | 217 | return llvm::ConstantStruct::get(SType, Elts); |
| 218 | } |
| 219 | |
Eli Friedman | a823400 | 2008-05-30 10:24:46 +0000 | [diff] [blame] | 220 | llvm::Constant *EmitUnionInitialization(InitListExpr *ILE) { |
Eli Friedman | a823400 | 2008-05-30 10:24:46 +0000 | [diff] [blame] | 221 | RecordDecl *RD = ILE->getType()->getAsRecordType()->getDecl(); |
| 222 | const llvm::Type *Ty = ConvertType(ILE->getType()); |
| 223 | |
| 224 | // Find the field decl we're initializing, if any |
| 225 | int FieldNo = 0; // Field no in RecordDecl |
Eli Friedman | c55f98d | 2008-06-06 20:12:37 +0000 | [diff] [blame] | 226 | FieldDecl* curField = 0; |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 227 | for (RecordDecl::field_iterator Field = RD->field_begin(), |
| 228 | FieldEnd = RD->field_end(); |
| 229 | Field != FieldEnd; ++Field) { |
| 230 | curField = *Field; |
Eli Friedman | a823400 | 2008-05-30 10:24:46 +0000 | [diff] [blame] | 231 | FieldNo++; |
Eli Friedman | c55f98d | 2008-06-06 20:12:37 +0000 | [diff] [blame] | 232 | if (curField->getIdentifier()) |
| 233 | break; |
| 234 | } |
Eli Friedman | a823400 | 2008-05-30 10:24:46 +0000 | [diff] [blame] | 235 | |
Eli Friedman | c55f98d | 2008-06-06 20:12:37 +0000 | [diff] [blame] | 236 | if (!curField || !curField->getIdentifier() || ILE->getNumInits() == 0) |
Eli Friedman | a823400 | 2008-05-30 10:24:46 +0000 | [diff] [blame] | 237 | return llvm::Constant::getNullValue(Ty); |
| 238 | |
| 239 | if (curField->isBitField()) { |
| 240 | // Create a dummy struct for bit-field insertion |
| 241 | unsigned NumElts = CGM.getTargetData().getABITypeSize(Ty) / 8; |
| 242 | llvm::Constant* NV = llvm::Constant::getNullValue(llvm::Type::Int8Ty); |
| 243 | std::vector<llvm::Constant*> Elts(NumElts, NV); |
| 244 | |
| 245 | InsertBitfieldIntoStruct(Elts, curField, ILE->getInit(0)); |
| 246 | const llvm::ArrayType *RetTy = |
| 247 | llvm::ArrayType::get(NV->getType(), NumElts); |
| 248 | return llvm::ConstantArray::get(RetTy, Elts); |
| 249 | } |
| 250 | |
Anders Carlsson | aa43256 | 2008-11-17 01:58:55 +0000 | [diff] [blame] | 251 | llvm::Constant *C = CGM.EmitConstantExpr(ILE->getInit(0), CGF); |
Eli Friedman | a823400 | 2008-05-30 10:24:46 +0000 | [diff] [blame] | 252 | |
| 253 | // Build a struct with the union sub-element as the first member, |
| 254 | // and padded to the appropriate size |
| 255 | std::vector<llvm::Constant*> Elts; |
| 256 | std::vector<const llvm::Type*> Types; |
| 257 | Elts.push_back(C); |
| 258 | Types.push_back(C->getType()); |
| 259 | unsigned CurSize = CGM.getTargetData().getTypeStoreSize(C->getType()); |
| 260 | unsigned TotalSize = CGM.getTargetData().getTypeStoreSize(Ty); |
| 261 | while (CurSize < TotalSize) { |
| 262 | Elts.push_back(llvm::Constant::getNullValue(llvm::Type::Int8Ty)); |
| 263 | Types.push_back(llvm::Type::Int8Ty); |
| 264 | CurSize++; |
| 265 | } |
| 266 | |
| 267 | // This always generates a packed struct |
| 268 | // FIXME: Try to generate an unpacked struct when we can |
| 269 | llvm::StructType* STy = llvm::StructType::get(Types, true); |
| 270 | return llvm::ConstantStruct::get(STy, Elts); |
| 271 | } |
| 272 | |
| 273 | llvm::Constant *EmitVectorInitialization(InitListExpr *ILE) { |
| 274 | const llvm::VectorType *VType = |
| 275 | cast<llvm::VectorType>(ConvertType(ILE->getType())); |
Eli Friedman | cfb313b | 2008-05-30 12:02:14 +0000 | [diff] [blame] | 276 | const llvm::Type *ElemTy = VType->getElementType(); |
| 277 | std::vector<llvm::Constant*> Elts; |
Devang Patel | eae1560 | 2008-02-05 02:39:50 +0000 | [diff] [blame] | 278 | unsigned NumElements = VType->getNumElements(); |
Eli Friedman | cfb313b | 2008-05-30 12:02:14 +0000 | [diff] [blame] | 279 | unsigned NumInitElements = ILE->getNumInits(); |
Devang Patel | eae1560 | 2008-02-05 02:39:50 +0000 | [diff] [blame] | 280 | |
Eli Friedman | cfb313b | 2008-05-30 12:02:14 +0000 | [diff] [blame] | 281 | unsigned NumInitableElts = std::min(NumInitElements, NumElements); |
| 282 | |
Devang Patel | eae1560 | 2008-02-05 02:39:50 +0000 | [diff] [blame] | 283 | // Copy initializer elements. |
| 284 | unsigned i = 0; |
Eli Friedman | cfb313b | 2008-05-30 12:02:14 +0000 | [diff] [blame] | 285 | for (; i < NumInitableElts; ++i) { |
Anders Carlsson | aa43256 | 2008-11-17 01:58:55 +0000 | [diff] [blame] | 286 | llvm::Constant *C = CGM.EmitConstantExpr(ILE->getInit(i), CGF); |
Devang Patel | eae1560 | 2008-02-05 02:39:50 +0000 | [diff] [blame] | 287 | Elts.push_back(C); |
| 288 | } |
| 289 | |
Eli Friedman | cfb313b | 2008-05-30 12:02:14 +0000 | [diff] [blame] | 290 | for (; i < NumElements; ++i) |
| 291 | Elts.push_back(llvm::Constant::getNullValue(ElemTy)); |
| 292 | |
Devang Patel | eae1560 | 2008-02-05 02:39:50 +0000 | [diff] [blame] | 293 | return llvm::ConstantVector::get(VType, Elts); |
| 294 | } |
| 295 | |
Anders Carlsson | 3b1d57b | 2008-01-26 01:36:00 +0000 | [diff] [blame] | 296 | llvm::Constant *VisitInitListExpr(InitListExpr *ILE) { |
Eli Friedman | a823400 | 2008-05-30 10:24:46 +0000 | [diff] [blame] | 297 | if (ILE->getType()->isScalarType()) { |
| 298 | // We have a scalar in braces. Just use the first element. |
| 299 | if (ILE->getNumInits() > 0) |
Anders Carlsson | aa43256 | 2008-11-17 01:58:55 +0000 | [diff] [blame] | 300 | return CGM.EmitConstantExpr(ILE->getInit(0), CGF); |
Eli Friedman | a823400 | 2008-05-30 10:24:46 +0000 | [diff] [blame] | 301 | |
| 302 | const llvm::Type* RetTy = CGM.getTypes().ConvertType(ILE->getType()); |
| 303 | return llvm::Constant::getNullValue(RetTy); |
Anders Carlsson | 3b1d57b | 2008-01-26 01:36:00 +0000 | [diff] [blame] | 304 | } |
Chris Lattner | be20bb5 | 2008-10-26 23:53:12 +0000 | [diff] [blame] | 305 | |
| 306 | // FIXME: We don't codegen or sema designators yet. |
| 307 | if (ILE->hadDesignators()) { |
| 308 | CGM.ErrorUnsupported(ILE, "initializer list with designators"); |
| 309 | return llvm::UndefValue::get(ConvertType(ILE->getType())); |
| 310 | } |
Devang Patel | 47fb697 | 2008-01-29 23:23:18 +0000 | [diff] [blame] | 311 | |
Eli Friedman | a823400 | 2008-05-30 10:24:46 +0000 | [diff] [blame] | 312 | if (ILE->getType()->isArrayType()) |
| 313 | return EmitArrayInitialization(ILE); |
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()->isStructureType()) |
| 316 | return EmitStructInitialization(ILE); |
| 317 | |
| 318 | if (ILE->getType()->isUnionType()) |
| 319 | return EmitUnionInitialization(ILE); |
| 320 | |
| 321 | if (ILE->getType()->isVectorType()) |
| 322 | return EmitVectorInitialization(ILE); |
| 323 | |
Devang Patel | eae1560 | 2008-02-05 02:39:50 +0000 | [diff] [blame] | 324 | assert(0 && "Unable to handle InitListExpr"); |
Chris Lattner | 2b94fe3 | 2008-03-01 08:45:05 +0000 | [diff] [blame] | 325 | // Get rid of control reaches end of void function warning. |
| 326 | // Not reached. |
| 327 | return 0; |
Anders Carlsson | 3b1d57b | 2008-01-26 01:36:00 +0000 | [diff] [blame] | 328 | } |
Eli Friedman | 3b707e7 | 2008-02-21 17:57:49 +0000 | [diff] [blame] | 329 | |
Anders Carlsson | 3b1d57b | 2008-01-26 01:36:00 +0000 | [diff] [blame] | 330 | llvm::Constant *VisitImplicitCastExpr(ImplicitCastExpr *ICExpr) { |
Eli Friedman | 3b707e7 | 2008-02-21 17:57:49 +0000 | [diff] [blame] | 331 | Expr* SExpr = ICExpr->getSubExpr(); |
| 332 | QualType SType = SExpr->getType(); |
| 333 | llvm::Constant *C; // the intermediate expression |
| 334 | QualType T; // the type of the intermediate expression |
| 335 | if (SType->isArrayType()) { |
| 336 | // Arrays decay to a pointer to the first element |
| 337 | // VLAs would require special handling, but they can't occur here |
| 338 | C = EmitLValue(SExpr); |
Anders Carlsson | 3b1d57b | 2008-01-26 01:36:00 +0000 | [diff] [blame] | 339 | llvm::Constant *Idx0 = llvm::ConstantInt::get(llvm::Type::Int32Ty, 0); |
Anders Carlsson | 3b1d57b | 2008-01-26 01:36:00 +0000 | [diff] [blame] | 340 | llvm::Constant *Ops[] = {Idx0, Idx0}; |
| 341 | C = llvm::ConstantExpr::getGetElementPtr(C, Ops, 2); |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 342 | T = CGM.getContext().getArrayDecayedType(SType); |
Eli Friedman | 3b707e7 | 2008-02-21 17:57:49 +0000 | [diff] [blame] | 343 | } else if (SType->isFunctionType()) { |
| 344 | // Function types decay to a pointer to the function |
| 345 | C = EmitLValue(SExpr); |
| 346 | T = CGM.getContext().getPointerType(SType); |
| 347 | } else { |
| 348 | C = Visit(SExpr); |
| 349 | T = SType; |
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 | |
| 352 | // Perform the conversion; note that an implicit cast can both promote |
| 353 | // and convert an array/function |
| 354 | return EmitConversion(C, T, ICExpr->getType()); |
Anders Carlsson | 3b1d57b | 2008-01-26 01:36:00 +0000 | [diff] [blame] | 355 | } |
Eli Friedman | 3b707e7 | 2008-02-21 17:57:49 +0000 | [diff] [blame] | 356 | |
Anders Carlsson | 3b1d57b | 2008-01-26 01:36:00 +0000 | [diff] [blame] | 357 | llvm::Constant *VisitStringLiteral(StringLiteral *E) { |
Chris Lattner | a7ad98f | 2008-02-11 00:02:17 +0000 | [diff] [blame] | 358 | assert(!E->getType()->isPointerType() && "Strings are always arrays"); |
Anders Carlsson | 3b1d57b | 2008-01-26 01:36:00 +0000 | [diff] [blame] | 359 | |
| 360 | // Otherwise this must be a string initializing an array in a static |
| 361 | // initializer. Don't emit it as the address of the string, emit the string |
| 362 | // data itself as an inline array. |
Daniel Dunbar | 6143293 | 2008-08-13 23:20:05 +0000 | [diff] [blame] | 363 | return llvm::ConstantArray::get(CGM.GetStringForStringLiteral(E), false); |
Anders Carlsson | 3b1d57b | 2008-01-26 01:36:00 +0000 | [diff] [blame] | 364 | } |
| 365 | |
Daniel Dunbar | 54d1909 | 2008-08-16 01:41:47 +0000 | [diff] [blame] | 366 | llvm::Constant *VisitAddrLabelExpr(const AddrLabelExpr *E) { |
| 367 | assert(CGF && "Invalid address of label expression outside function."); |
| 368 | llvm::Constant *C = |
| 369 | llvm::ConstantInt::get(llvm::Type::Int32Ty, |
| 370 | CGF->GetIDForAddrOfLabel(E->getLabel())); |
| 371 | return llvm::ConstantExpr::getIntToPtr(C, ConvertType(E->getType())); |
| 372 | } |
| 373 | |
Anders Carlsson | e3d3b7d | 2008-01-26 02:08:50 +0000 | [diff] [blame] | 374 | llvm::Constant *VisitUnaryAddrOf(const UnaryOperator *E) { |
| 375 | return EmitLValue(E->getSubExpr()); |
| 376 | } |
Anders Carlsson | 5a1deb8 | 2008-01-29 15:56:48 +0000 | [diff] [blame] | 377 | llvm::Constant *VisitUnaryOffsetOf(const UnaryOperator *E) { |
| 378 | int64_t Val = E->evaluateOffsetOf(CGM.getContext()); |
| 379 | |
| 380 | assert(E->getType()->isIntegerType() && "Result type must be an integer!"); |
| 381 | |
Chris Lattner | 98be494 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 382 | uint32_t ResultWidth = |
| 383 | static_cast<uint32_t>(CGM.getContext().getTypeSize(E->getType())); |
Anders Carlsson | 5a1deb8 | 2008-01-29 15:56:48 +0000 | [diff] [blame] | 384 | return llvm::ConstantInt::get(llvm::APInt(ResultWidth, Val)); |
| 385 | } |
Eli Friedman | bc5ed6e | 2008-05-29 11:22:45 +0000 | [diff] [blame] | 386 | |
| 387 | llvm::Constant *VisitUnaryExtension(const UnaryOperator *E) { |
| 388 | return Visit(E->getSubExpr()); |
| 389 | } |
Anders Carlsson | 3b1d57b | 2008-01-26 01:36:00 +0000 | [diff] [blame] | 390 | |
Anders Carlsson | ce6237e | 2008-01-29 01:33:32 +0000 | [diff] [blame] | 391 | // Binary operators |
Anders Carlsson | 2ea2b5e | 2008-08-25 03:27:15 +0000 | [diff] [blame] | 392 | |
| 393 | llvm::Constant *VisitCallExpr(const CallExpr *E) { |
Anders Carlsson | 4b3f9c0 | 2008-12-01 02:42:14 +0000 | [diff] [blame] | 394 | Expr::EvalResult Result; |
Chris Lattner | 6ee7aa1 | 2008-11-16 21:24:15 +0000 | [diff] [blame] | 395 | if (E->Evaluate(Result, CGM.getContext())) { |
Anders Carlsson | 4b3f9c0 | 2008-12-01 02:42:14 +0000 | [diff] [blame] | 396 | if (Result.Val.isInt()) |
| 397 | return llvm::ConstantInt::get(Result.Val.getInt()); |
| 398 | if (Result.Val.isFloat()) |
| 399 | return llvm::ConstantFP::get(Result.Val.getFloat()); |
Chris Lattner | f6b24ea | 2008-10-06 05:59:01 +0000 | [diff] [blame] | 400 | } |
Anders Carlsson | 2ea2b5e | 2008-08-25 03:27:15 +0000 | [diff] [blame] | 401 | |
Chris Lattner | 506ff88 | 2008-10-06 07:26:43 +0000 | [diff] [blame] | 402 | // Handle __builtin___CFStringMakeConstantString. |
| 403 | if (E->isBuiltinCall() ==Builtin::BI__builtin___CFStringMakeConstantString){ |
Anders Carlsson | d114ade | 2008-11-15 18:52:06 +0000 | [diff] [blame] | 404 | const Expr *Arg = E->getArg(0)->IgnoreParenCasts(); |
Chris Lattner | 506ff88 | 2008-10-06 07:26:43 +0000 | [diff] [blame] | 405 | |
| 406 | const StringLiteral *Literal = cast<StringLiteral>(Arg); |
| 407 | std::string S(Literal->getStrData(), Literal->getByteLength()); |
| 408 | return CGM.GetAddrOfConstantCFString(S); |
| 409 | } |
| 410 | |
Anders Carlsson | 2ea2b5e | 2008-08-25 03:27:15 +0000 | [diff] [blame] | 411 | CGM.ErrorUnsupported(E, "constant call expression"); |
| 412 | return llvm::Constant::getNullValue(ConvertType(E->getType())); |
| 413 | } |
Anders Carlsson | 7be3d01 | 2008-02-05 20:54:21 +0000 | [diff] [blame] | 414 | |
Anders Carlsson | 3b1d57b | 2008-01-26 01:36:00 +0000 | [diff] [blame] | 415 | // Utility methods |
| 416 | const llvm::Type *ConvertType(QualType T) { |
| 417 | return CGM.getTypes().ConvertType(T); |
| 418 | } |
| 419 | |
| 420 | llvm::Constant *EmitConversionToBool(llvm::Constant *Src, QualType SrcType) { |
| 421 | assert(SrcType->isCanonical() && "EmitConversion strips typedefs"); |
| 422 | |
| 423 | if (SrcType->isRealFloatingType()) { |
| 424 | // Compare against 0.0 for fp scalars. |
| 425 | llvm::Constant *Zero = llvm::Constant::getNullValue(Src->getType()); |
| 426 | return llvm::ConstantExpr::getFCmp(llvm::FCmpInst::FCMP_UNE, Src, Zero); |
| 427 | } |
| 428 | |
| 429 | assert((SrcType->isIntegerType() || SrcType->isPointerType()) && |
| 430 | "Unknown scalar type to convert"); |
| 431 | |
| 432 | // Compare against an integer or pointer null. |
| 433 | llvm::Constant *Zero = llvm::Constant::getNullValue(Src->getType()); |
| 434 | return llvm::ConstantExpr::getICmp(llvm::ICmpInst::ICMP_NE, Src, Zero); |
| 435 | } |
| 436 | |
| 437 | llvm::Constant *EmitConversion(llvm::Constant *Src, QualType SrcType, |
| 438 | QualType DstType) { |
Chris Lattner | d394cc6 | 2008-07-26 22:39:33 +0000 | [diff] [blame] | 439 | SrcType = CGM.getContext().getCanonicalType(SrcType); |
| 440 | DstType = CGM.getContext().getCanonicalType(DstType); |
Anders Carlsson | 3b1d57b | 2008-01-26 01:36:00 +0000 | [diff] [blame] | 441 | if (SrcType == DstType) return Src; |
| 442 | |
| 443 | // Handle conversions to bool first, they are special: comparisons against 0. |
| 444 | if (DstType->isBooleanType()) |
| 445 | return EmitConversionToBool(Src, SrcType); |
| 446 | |
| 447 | const llvm::Type *DstTy = ConvertType(DstType); |
| 448 | |
| 449 | // Ignore conversions like int -> uint. |
| 450 | if (Src->getType() == DstTy) |
| 451 | return Src; |
| 452 | |
| 453 | // Handle pointer conversions next: pointers can only be converted to/from |
| 454 | // other pointers and integers. |
Eli Friedman | c53296e | 2008-06-07 17:27:51 +0000 | [diff] [blame] | 455 | if (isa<llvm::PointerType>(DstTy)) { |
Anders Carlsson | 3b1d57b | 2008-01-26 01:36:00 +0000 | [diff] [blame] | 456 | // The source value may be an integer, or a pointer. |
| 457 | if (isa<llvm::PointerType>(Src->getType())) |
| 458 | return llvm::ConstantExpr::getBitCast(Src, DstTy); |
Anders Carlsson | 84005b4 | 2008-01-26 04:30:23 +0000 | [diff] [blame] | 459 | assert(SrcType->isIntegerType() &&"Not ptr->ptr or int->ptr conversion?"); |
Anders Carlsson | 3b1d57b | 2008-01-26 01:36:00 +0000 | [diff] [blame] | 460 | return llvm::ConstantExpr::getIntToPtr(Src, DstTy); |
| 461 | } |
| 462 | |
Eli Friedman | c53296e | 2008-06-07 17:27:51 +0000 | [diff] [blame] | 463 | if (isa<llvm::PointerType>(Src->getType())) { |
Anders Carlsson | 3b1d57b | 2008-01-26 01:36:00 +0000 | [diff] [blame] | 464 | // Must be an ptr to int cast. |
| 465 | assert(isa<llvm::IntegerType>(DstTy) && "not ptr->int?"); |
| 466 | return llvm::ConstantExpr::getPtrToInt(Src, DstTy); |
| 467 | } |
| 468 | |
| 469 | // A scalar source can be splatted to a vector of the same element type |
| 470 | if (isa<llvm::VectorType>(DstTy) && !isa<VectorType>(SrcType)) { |
Chris Lattner | 3304e55 | 2008-10-12 00:31:50 +0000 | [diff] [blame] | 471 | assert((cast<llvm::VectorType>(DstTy)->getElementType() |
| 472 | == Src->getType()) && |
Anders Carlsson | 3b1d57b | 2008-01-26 01:36:00 +0000 | [diff] [blame] | 473 | "Vector element type must match scalar type to splat."); |
| 474 | unsigned NumElements = DstType->getAsVectorType()->getNumElements(); |
| 475 | llvm::SmallVector<llvm::Constant*, 16> Elements; |
| 476 | for (unsigned i = 0; i < NumElements; i++) |
| 477 | Elements.push_back(Src); |
| 478 | |
| 479 | return llvm::ConstantVector::get(&Elements[0], NumElements); |
| 480 | } |
| 481 | |
| 482 | if (isa<llvm::VectorType>(Src->getType()) || |
| 483 | isa<llvm::VectorType>(DstTy)) { |
| 484 | return llvm::ConstantExpr::getBitCast(Src, DstTy); |
| 485 | } |
| 486 | |
| 487 | // Finally, we have the arithmetic types: real int/float. |
| 488 | if (isa<llvm::IntegerType>(Src->getType())) { |
| 489 | bool InputSigned = SrcType->isSignedIntegerType(); |
| 490 | if (isa<llvm::IntegerType>(DstTy)) |
| 491 | return llvm::ConstantExpr::getIntegerCast(Src, DstTy, InputSigned); |
| 492 | else if (InputSigned) |
| 493 | return llvm::ConstantExpr::getSIToFP(Src, DstTy); |
| 494 | else |
| 495 | return llvm::ConstantExpr::getUIToFP(Src, DstTy); |
| 496 | } |
| 497 | |
| 498 | assert(Src->getType()->isFloatingPoint() && "Unknown real conversion"); |
| 499 | if (isa<llvm::IntegerType>(DstTy)) { |
| 500 | if (DstType->isSignedIntegerType()) |
| 501 | return llvm::ConstantExpr::getFPToSI(Src, DstTy); |
| 502 | else |
| 503 | return llvm::ConstantExpr::getFPToUI(Src, DstTy); |
| 504 | } |
| 505 | |
| 506 | assert(DstTy->isFloatingPoint() && "Unknown real conversion"); |
| 507 | if (DstTy->getTypeID() < Src->getType()->getTypeID()) |
| 508 | return llvm::ConstantExpr::getFPTrunc(Src, DstTy); |
| 509 | else |
| 510 | return llvm::ConstantExpr::getFPExtend(Src, DstTy); |
| 511 | } |
| 512 | |
| 513 | llvm::Constant *EmitSizeAlignOf(QualType TypeToSize, |
| 514 | QualType RetType, bool isSizeOf) { |
| 515 | std::pair<uint64_t, unsigned> Info = |
Chris Lattner | 98be494 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 516 | CGM.getContext().getTypeInfo(TypeToSize); |
Anders Carlsson | 3b1d57b | 2008-01-26 01:36:00 +0000 | [diff] [blame] | 517 | |
| 518 | uint64_t Val = isSizeOf ? Info.first : Info.second; |
| 519 | Val /= 8; // Return size in bytes, not bits. |
| 520 | |
| 521 | assert(RetType->isIntegerType() && "Result type must be an integer!"); |
| 522 | |
Chris Lattner | 98be494 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 523 | uint32_t ResultWidth = |
| 524 | static_cast<uint32_t>(CGM.getContext().getTypeSize(RetType)); |
Anders Carlsson | 3b1d57b | 2008-01-26 01:36:00 +0000 | [diff] [blame] | 525 | return llvm::ConstantInt::get(llvm::APInt(ResultWidth, Val)); |
| 526 | } |
Anders Carlsson | 84005b4 | 2008-01-26 04:30:23 +0000 | [diff] [blame] | 527 | |
Anders Carlsson | 5ea7d07 | 2008-11-16 06:23:45 +0000 | [diff] [blame] | 528 | public: |
Anders Carlsson | 84005b4 | 2008-01-26 04:30:23 +0000 | [diff] [blame] | 529 | llvm::Constant *EmitLValue(Expr *E) { |
Anders Carlsson | e3d3b7d | 2008-01-26 02:08:50 +0000 | [diff] [blame] | 530 | switch (E->getStmtClass()) { |
Eli Friedman | 546d94c | 2008-02-11 00:23:10 +0000 | [diff] [blame] | 531 | default: break; |
Anders Carlsson | e3d3b7d | 2008-01-26 02:08:50 +0000 | [diff] [blame] | 532 | case Expr::ParenExprClass: |
| 533 | // Elide parenthesis |
| 534 | return EmitLValue(cast<ParenExpr>(E)->getSubExpr()); |
| 535 | case Expr::CompoundLiteralExprClass: { |
| 536 | // Note that due to the nature of compound literals, this is guaranteed |
| 537 | // 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] | 538 | CompoundLiteralExpr *CLE = cast<CompoundLiteralExpr>(E); |
| 539 | llvm::Constant* C = Visit(CLE->getInitializer()); |
Chris Lattner | a7ad98f | 2008-02-11 00:02:17 +0000 | [diff] [blame] | 540 | C = new llvm::GlobalVariable(C->getType(),E->getType().isConstQualified(), |
Anders Carlsson | e3d3b7d | 2008-01-26 02:08:50 +0000 | [diff] [blame] | 541 | llvm::GlobalValue::InternalLinkage, |
| 542 | C, ".compoundliteral", &CGM.getModule()); |
| 543 | return C; |
Anders Carlsson | 84005b4 | 2008-01-26 04:30:23 +0000 | [diff] [blame] | 544 | } |
Douglas Gregor | 1a49af9 | 2009-01-06 05:10:23 +0000 | [diff] [blame^] | 545 | case Expr::DeclRefExprClass: |
| 546 | case Expr::QualifiedDeclRefExprClass: { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 547 | NamedDecl *Decl = cast<DeclRefExpr>(E)->getDecl(); |
Anders Carlsson | e3d3b7d | 2008-01-26 02:08:50 +0000 | [diff] [blame] | 548 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(Decl)) |
Daniel Dunbar | bd012ff | 2008-07-29 23:18:29 +0000 | [diff] [blame] | 549 | return CGM.GetAddrOfFunction(FD); |
Steve Naroff | 248a753 | 2008-04-15 22:42:06 +0000 | [diff] [blame] | 550 | if (const VarDecl* VD = dyn_cast<VarDecl>(Decl)) { |
| 551 | if (VD->isFileVarDecl()) |
Daniel Dunbar | bd012ff | 2008-07-29 23:18:29 +0000 | [diff] [blame] | 552 | return CGM.GetAddrOfGlobalVar(VD); |
Steve Naroff | 248a753 | 2008-04-15 22:42:06 +0000 | [diff] [blame] | 553 | else if (VD->isBlockVarDecl()) { |
| 554 | assert(CGF && "Can't access static local vars without CGF"); |
| 555 | return CGF->GetAddrOfStaticLocalVar(VD); |
| 556 | } |
Lauro Ramos Venancio | 8137335 | 2008-02-26 21:41:45 +0000 | [diff] [blame] | 557 | } |
Eli Friedman | 546d94c | 2008-02-11 00:23:10 +0000 | [diff] [blame] | 558 | break; |
Anders Carlsson | e3d3b7d | 2008-01-26 02:08:50 +0000 | [diff] [blame] | 559 | } |
| 560 | case Expr::MemberExprClass: { |
Anders Carlsson | 84005b4 | 2008-01-26 04:30:23 +0000 | [diff] [blame] | 561 | MemberExpr* ME = cast<MemberExpr>(E); |
Anders Carlsson | 84005b4 | 2008-01-26 04:30:23 +0000 | [diff] [blame] | 562 | llvm::Constant *Base; |
| 563 | if (ME->isArrow()) |
| 564 | Base = Visit(ME->getBase()); |
| 565 | else |
| 566 | Base = EmitLValue(ME->getBase()); |
Lauro Ramos Venancio | 5bfe18c | 2008-01-30 21:23:20 +0000 | [diff] [blame] | 567 | |
Douglas Gregor | 86f1940 | 2008-12-20 23:49:58 +0000 | [diff] [blame] | 568 | FieldDecl *Field = dyn_cast<FieldDecl>(ME->getMemberDecl()); |
| 569 | // FIXME: Handle other kinds of member expressions. |
| 570 | assert(Field && "No code generation for non-field member expressions"); |
| 571 | unsigned FieldNumber = CGM.getTypes().getLLVMFieldNo(Field); |
Anders Carlsson | e3d3b7d | 2008-01-26 02:08:50 +0000 | [diff] [blame] | 572 | llvm::Constant *Zero = llvm::ConstantInt::get(llvm::Type::Int32Ty, 0); |
| 573 | llvm::Constant *Idx = llvm::ConstantInt::get(llvm::Type::Int32Ty, |
| 574 | FieldNumber); |
| 575 | llvm::Value *Ops[] = {Zero, Idx}; |
| 576 | return llvm::ConstantExpr::getGetElementPtr(Base, Ops, 2); |
| 577 | } |
| 578 | case Expr::ArraySubscriptExprClass: { |
Anders Carlsson | 84005b4 | 2008-01-26 04:30:23 +0000 | [diff] [blame] | 579 | ArraySubscriptExpr* ASExpr = cast<ArraySubscriptExpr>(E); |
| 580 | llvm::Constant *Base = Visit(ASExpr->getBase()); |
| 581 | llvm::Constant *Index = Visit(ASExpr->getIdx()); |
Anders Carlsson | e3d3b7d | 2008-01-26 02:08:50 +0000 | [diff] [blame] | 582 | assert(!ASExpr->getBase()->getType()->isVectorType() && |
| 583 | "Taking the address of a vector component is illegal!"); |
| 584 | return llvm::ConstantExpr::getGetElementPtr(Base, &Index, 1); |
| 585 | } |
Daniel Dunbar | 6143293 | 2008-08-13 23:20:05 +0000 | [diff] [blame] | 586 | case Expr::StringLiteralClass: |
| 587 | return CGM.GetAddrOfConstantStringFromLiteral(cast<StringLiteral>(E)); |
Anders Carlsson | 5ea7d07 | 2008-11-16 06:23:45 +0000 | [diff] [blame] | 588 | case Expr::ObjCStringLiteralClass: { |
| 589 | ObjCStringLiteral* SL = cast<ObjCStringLiteral>(E); |
| 590 | std::string S(SL->getString()->getStrData(), |
| 591 | SL->getString()->getByteLength()); |
| 592 | llvm::Constant *C = CGM.getObjCRuntime().GenerateConstantString(S); |
| 593 | return llvm::ConstantExpr::getBitCast(C, ConvertType(E->getType())); |
| 594 | } |
Anders Carlsson | 84005b4 | 2008-01-26 04:30:23 +0000 | [diff] [blame] | 595 | case Expr::UnaryOperatorClass: { |
| 596 | UnaryOperator *Exp = cast<UnaryOperator>(E); |
| 597 | switch (Exp->getOpcode()) { |
Eli Friedman | 546d94c | 2008-02-11 00:23:10 +0000 | [diff] [blame] | 598 | default: break; |
| 599 | case UnaryOperator::Extension: |
Anders Carlsson | 84005b4 | 2008-01-26 04:30:23 +0000 | [diff] [blame] | 600 | // Extension is just a wrapper for expressions |
| 601 | return EmitLValue(Exp->getSubExpr()); |
| 602 | case UnaryOperator::Real: |
| 603 | case UnaryOperator::Imag: { |
| 604 | // The address of __real or __imag is just a GEP off the address |
| 605 | // of the internal expression |
| 606 | llvm::Constant* C = EmitLValue(Exp->getSubExpr()); |
| 607 | llvm::Constant *Zero = llvm::ConstantInt::get(llvm::Type::Int32Ty, 0); |
| 608 | llvm::Constant *Idx = llvm::ConstantInt::get(llvm::Type::Int32Ty, |
Anders Carlsson | e3d3b7d | 2008-01-26 02:08:50 +0000 | [diff] [blame] | 609 | Exp->getOpcode() == UnaryOperator::Imag); |
Anders Carlsson | 84005b4 | 2008-01-26 04:30:23 +0000 | [diff] [blame] | 610 | llvm::Value *Ops[] = {Zero, Idx}; |
| 611 | return llvm::ConstantExpr::getGetElementPtr(C, Ops, 2); |
| 612 | } |
| 613 | case UnaryOperator::Deref: |
| 614 | // The address of a deref is just the value of the expression |
| 615 | return Visit(Exp->getSubExpr()); |
| 616 | } |
Eli Friedman | 546d94c | 2008-02-11 00:23:10 +0000 | [diff] [blame] | 617 | break; |
Anders Carlsson | e3d3b7d | 2008-01-26 02:08:50 +0000 | [diff] [blame] | 618 | } |
Chris Lattner | 3534564 | 2008-12-12 05:18:02 +0000 | [diff] [blame] | 619 | |
| 620 | case Expr::PredefinedExprClass: { |
| 621 | // __func__/__FUNCTION__ -> "". __PRETTY_FUNCTION__ -> "top level". |
| 622 | std::string Str; |
| 623 | if (cast<PredefinedExpr>(E)->getIdentType() == |
| 624 | PredefinedExpr::PrettyFunction) |
| 625 | Str = "top level"; |
| 626 | |
| 627 | return CGM.GetAddrOfConstantCString(Str, ".tmp"); |
| 628 | } |
Eli Friedman | 546d94c | 2008-02-11 00:23:10 +0000 | [diff] [blame] | 629 | } |
Daniel Dunbar | 488e993 | 2008-08-16 00:56:44 +0000 | [diff] [blame] | 630 | CGM.ErrorUnsupported(E, "constant l-value expression"); |
Eli Friedman | 546d94c | 2008-02-11 00:23:10 +0000 | [diff] [blame] | 631 | llvm::Type *Ty = llvm::PointerType::getUnqual(ConvertType(E->getType())); |
| 632 | return llvm::UndefValue::get(Ty); |
Anders Carlsson | e3d3b7d | 2008-01-26 02:08:50 +0000 | [diff] [blame] | 633 | } |
Anders Carlsson | e3d3b7d | 2008-01-26 02:08:50 +0000 | [diff] [blame] | 634 | }; |
| 635 | |
Anders Carlsson | 3b1d57b | 2008-01-26 01:36:00 +0000 | [diff] [blame] | 636 | } // end anonymous namespace. |
| 637 | |
Lauro Ramos Venancio | 8137335 | 2008-02-26 21:41:45 +0000 | [diff] [blame] | 638 | llvm::Constant *CodeGenModule::EmitConstantExpr(const Expr *E, |
Chris Lattner | 9619662 | 2008-07-26 22:37:01 +0000 | [diff] [blame] | 639 | CodeGenFunction *CGF) { |
| 640 | QualType type = Context.getCanonicalType(E->getType()); |
Eli Friedman | 7dfa639 | 2008-06-01 15:31:44 +0000 | [diff] [blame] | 641 | |
Anders Carlsson | 4b3f9c0 | 2008-12-01 02:42:14 +0000 | [diff] [blame] | 642 | Expr::EvalResult Result; |
| 643 | |
| 644 | if (E->Evaluate(Result, Context)) { |
| 645 | assert(!Result.HasSideEffects && |
| 646 | "Constant expr should not have any side effects!"); |
| 647 | switch (Result.Val.getKind()) { |
Anders Carlsson | 5a9a457 | 2008-11-15 20:45:50 +0000 | [diff] [blame] | 648 | default: assert(0 && "unhandled value kind!"); |
| 649 | case APValue::LValue: { |
Anders Carlsson | 4b3f9c0 | 2008-12-01 02:42:14 +0000 | [diff] [blame] | 650 | llvm::Constant *Offset = |
| 651 | llvm::ConstantInt::get(llvm::Type::Int64Ty, |
| 652 | Result.Val.getLValueOffset()); |
Anders Carlsson | 5a9a457 | 2008-11-15 20:45:50 +0000 | [diff] [blame] | 653 | |
Anders Carlsson | 4b3f9c0 | 2008-12-01 02:42:14 +0000 | [diff] [blame] | 654 | if (const Expr *LVBase = Result.Val.getLValueBase()) { |
Anders Carlsson | aa43256 | 2008-11-17 01:58:55 +0000 | [diff] [blame] | 655 | llvm::Constant *C = |
Anders Carlsson | 5ea7d07 | 2008-11-16 06:23:45 +0000 | [diff] [blame] | 656 | ConstExprEmitter(*this, CGF).EmitLValue(const_cast<Expr*>(LVBase)); |
| 657 | |
Anders Carlsson | aa43256 | 2008-11-17 01:58:55 +0000 | [diff] [blame] | 658 | const llvm::Type *Type = |
| 659 | llvm::PointerType::getUnqual(llvm::Type::Int8Ty); |
Eli Friedman | 7b30b1c | 2008-11-17 03:57:28 +0000 | [diff] [blame] | 660 | const llvm::Type *DestType = getTypes().ConvertTypeForMem(E->getType()); |
Anders Carlsson | aa43256 | 2008-11-17 01:58:55 +0000 | [diff] [blame] | 661 | |
| 662 | // FIXME: It's a little ugly that we need to cast to a pointer, |
| 663 | // apply the GEP and then cast back. |
| 664 | C = llvm::ConstantExpr::getBitCast(C, Type); |
| 665 | C = llvm::ConstantExpr::getGetElementPtr(C, &Offset, 1); |
| 666 | |
| 667 | return llvm::ConstantExpr::getBitCast(C, DestType); |
Anders Carlsson | 5ea7d07 | 2008-11-16 06:23:45 +0000 | [diff] [blame] | 668 | } |
Anders Carlsson | 5a9a457 | 2008-11-15 20:45:50 +0000 | [diff] [blame] | 669 | |
Anders Carlsson | 5ea7d07 | 2008-11-16 06:23:45 +0000 | [diff] [blame] | 670 | return llvm::ConstantExpr::getIntToPtr(Offset, |
| 671 | getTypes().ConvertType(type)); |
Anders Carlsson | 5a9a457 | 2008-11-15 20:45:50 +0000 | [diff] [blame] | 672 | } |
Eli Friedman | 7b30b1c | 2008-11-17 03:57:28 +0000 | [diff] [blame] | 673 | case APValue::Int: { |
Anders Carlsson | 4b3f9c0 | 2008-12-01 02:42:14 +0000 | [diff] [blame] | 674 | llvm::Constant *C = llvm::ConstantInt::get(Result.Val.getInt()); |
Anders Carlsson | 5a9a457 | 2008-11-15 20:45:50 +0000 | [diff] [blame] | 675 | |
| 676 | if (C->getType() == llvm::Type::Int1Ty) { |
| 677 | const llvm::Type *BoolTy = getTypes().ConvertTypeForMem(E->getType()); |
| 678 | C = llvm::ConstantExpr::getZExt(C, BoolTy); |
| 679 | } |
| 680 | return C; |
Eli Friedman | 7b30b1c | 2008-11-17 03:57:28 +0000 | [diff] [blame] | 681 | } |
Anders Carlsson | 5a9a457 | 2008-11-15 20:45:50 +0000 | [diff] [blame] | 682 | case APValue::Float: |
Anders Carlsson | 4b3f9c0 | 2008-12-01 02:42:14 +0000 | [diff] [blame] | 683 | return llvm::ConstantFP::get(Result.Val.getFloat()); |
Anders Carlsson | aa43256 | 2008-11-17 01:58:55 +0000 | [diff] [blame] | 684 | case APValue::ComplexFloat: { |
| 685 | llvm::Constant *Complex[2]; |
| 686 | |
Anders Carlsson | 4b3f9c0 | 2008-12-01 02:42:14 +0000 | [diff] [blame] | 687 | Complex[0] = llvm::ConstantFP::get(Result.Val.getComplexFloatReal()); |
| 688 | Complex[1] = llvm::ConstantFP::get(Result.Val.getComplexFloatImag()); |
Anders Carlsson | aa43256 | 2008-11-17 01:58:55 +0000 | [diff] [blame] | 689 | |
| 690 | return llvm::ConstantStruct::get(Complex, 2); |
| 691 | } |
Anders Carlsson | 5a9a457 | 2008-11-15 20:45:50 +0000 | [diff] [blame] | 692 | } |
| 693 | } |
Eli Friedman | 7dfa639 | 2008-06-01 15:31:44 +0000 | [diff] [blame] | 694 | |
| 695 | llvm::Constant* C = ConstExprEmitter(*this, CGF).Visit(const_cast<Expr*>(E)); |
| 696 | if (C->getType() == llvm::Type::Int1Ty) { |
| 697 | const llvm::Type *BoolTy = getTypes().ConvertTypeForMem(E->getType()); |
| 698 | C = llvm::ConstantExpr::getZExt(C, BoolTy); |
| 699 | } |
| 700 | return C; |
Anders Carlsson | 3b1d57b | 2008-01-26 01:36:00 +0000 | [diff] [blame] | 701 | } |