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" |
John McCall | 4c40d98 | 2010-08-31 07:33:07 +0000 | [diff] [blame] | 16 | #include "CGCXXABI.h" |
Daniel Dunbar | af2f62c | 2008-08-13 00:59:25 +0000 | [diff] [blame] | 17 | #include "CGObjCRuntime.h" |
Daniel Dunbar | 2924ade | 2010-03-30 22:26:10 +0000 | [diff] [blame] | 18 | #include "CGRecordLayout.h" |
Chris Lattner | f6b24ea | 2008-10-06 05:59:01 +0000 | [diff] [blame] | 19 | #include "clang/AST/APValue.h" |
Daniel Dunbar | de7fb84 | 2008-08-11 05:00:27 +0000 | [diff] [blame] | 20 | #include "clang/AST/ASTContext.h" |
Anders Carlsson | 4c98efd | 2009-07-24 15:20:52 +0000 | [diff] [blame] | 21 | #include "clang/AST/RecordLayout.h" |
Daniel Dunbar | de7fb84 | 2008-08-11 05:00:27 +0000 | [diff] [blame] | 22 | #include "clang/AST/StmtVisitor.h" |
Chris Lattner | 1b63e4f | 2009-06-14 01:54:56 +0000 | [diff] [blame] | 23 | #include "clang/Basic/Builtins.h" |
Anders Carlsson | 3b1d57b | 2008-01-26 01:36:00 +0000 | [diff] [blame] | 24 | #include "llvm/Constants.h" |
| 25 | #include "llvm/Function.h" |
| 26 | #include "llvm/GlobalVariable.h" |
Eli Friedman | a823400 | 2008-05-30 10:24:46 +0000 | [diff] [blame] | 27 | #include "llvm/Target/TargetData.h" |
Anders Carlsson | 3b1d57b | 2008-01-26 01:36:00 +0000 | [diff] [blame] | 28 | using namespace clang; |
| 29 | using namespace CodeGen; |
| 30 | |
Chris Lattner | eba3e5a | 2010-04-13 17:45:57 +0000 | [diff] [blame] | 31 | //===----------------------------------------------------------------------===// |
| 32 | // ConstStructBuilder |
| 33 | //===----------------------------------------------------------------------===// |
| 34 | |
| 35 | namespace { |
Benjamin Kramer | 85b4521 | 2009-11-28 19:45:26 +0000 | [diff] [blame] | 36 | class ConstStructBuilder { |
Anders Carlsson | 4c98efd | 2009-07-24 15:20:52 +0000 | [diff] [blame] | 37 | CodeGenModule &CGM; |
| 38 | CodeGenFunction *CGF; |
| 39 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 40 | bool Packed; |
Ken Dyck | dc49639 | 2011-03-17 01:33:18 +0000 | [diff] [blame] | 41 | CharUnits NextFieldOffsetInChars; |
Ken Dyck | 7d2f9d5 | 2011-03-18 01:26:17 +0000 | [diff] [blame] | 42 | CharUnits LLVMStructAlignment; |
Anders Carlsson | 4c98efd | 2009-07-24 15:20:52 +0000 | [diff] [blame] | 43 | std::vector<llvm::Constant *> Elements; |
Chris Lattner | eba3e5a | 2010-04-13 17:45:57 +0000 | [diff] [blame] | 44 | public: |
| 45 | static llvm::Constant *BuildStruct(CodeGenModule &CGM, CodeGenFunction *CGF, |
| 46 | InitListExpr *ILE); |
| 47 | |
| 48 | private: |
Anders Carlsson | 4c98efd | 2009-07-24 15:20:52 +0000 | [diff] [blame] | 49 | ConstStructBuilder(CodeGenModule &CGM, CodeGenFunction *CGF) |
Ken Dyck | dc49639 | 2011-03-17 01:33:18 +0000 | [diff] [blame] | 50 | : CGM(CGM), CGF(CGF), Packed(false), |
| 51 | NextFieldOffsetInChars(CharUnits::Zero()), |
Ken Dyck | 7d2f9d5 | 2011-03-18 01:26:17 +0000 | [diff] [blame] | 52 | LLVMStructAlignment(CharUnits::One()) { } |
Anders Carlsson | 4c98efd | 2009-07-24 15:20:52 +0000 | [diff] [blame] | 53 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 54 | bool AppendField(const FieldDecl *Field, uint64_t FieldOffset, |
Chris Lattner | 8ce9e45 | 2010-04-13 18:16:19 +0000 | [diff] [blame] | 55 | llvm::Constant *InitExpr); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 56 | |
Chris Lattner | 1880666 | 2010-07-05 17:04:23 +0000 | [diff] [blame] | 57 | void AppendBitField(const FieldDecl *Field, uint64_t FieldOffset, |
| 58 | llvm::ConstantInt *InitExpr); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 59 | |
Ken Dyck | c29ea8f | 2011-03-11 23:42:54 +0000 | [diff] [blame] | 60 | void AppendPadding(CharUnits PadSize); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 61 | |
Ken Dyck | 106ca04 | 2011-03-11 02:17:05 +0000 | [diff] [blame] | 62 | void AppendTailPadding(CharUnits RecordSize); |
Anders Carlsson | d24393b | 2009-07-27 01:23:51 +0000 | [diff] [blame] | 63 | |
Chris Lattner | eba3e5a | 2010-04-13 17:45:57 +0000 | [diff] [blame] | 64 | void ConvertStructToPacked(); |
Anders Carlsson | 31f2f9c | 2009-10-02 02:15:20 +0000 | [diff] [blame] | 65 | |
Chris Lattner | eba3e5a | 2010-04-13 17:45:57 +0000 | [diff] [blame] | 66 | bool Build(InitListExpr *ILE); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 67 | |
Ken Dyck | 7d2f9d5 | 2011-03-18 01:26:17 +0000 | [diff] [blame] | 68 | CharUnits getAlignment(const llvm::Constant *C) const { |
| 69 | if (Packed) return CharUnits::One(); |
| 70 | return CharUnits::fromQuantity( |
| 71 | CGM.getTargetData().getABITypeAlignment(C->getType())); |
Anders Carlsson | 4c98efd | 2009-07-24 15:20:52 +0000 | [diff] [blame] | 72 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 73 | |
Ken Dyck | 15c152e | 2011-03-18 01:12:13 +0000 | [diff] [blame] | 74 | CharUnits getSizeInChars(const llvm::Constant *C) const { |
| 75 | return CharUnits::fromQuantity( |
| 76 | CGM.getTargetData().getTypeAllocSize(C->getType())); |
Anders Carlsson | 4c98efd | 2009-07-24 15:20:52 +0000 | [diff] [blame] | 77 | } |
Anders Carlsson | 4c98efd | 2009-07-24 15:20:52 +0000 | [diff] [blame] | 78 | }; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 79 | |
Chris Lattner | eba3e5a | 2010-04-13 17:45:57 +0000 | [diff] [blame] | 80 | bool ConstStructBuilder:: |
Chris Lattner | 8ce9e45 | 2010-04-13 18:16:19 +0000 | [diff] [blame] | 81 | AppendField(const FieldDecl *Field, uint64_t FieldOffset, |
| 82 | llvm::Constant *InitCst) { |
Ken Dyck | 6925cc4 | 2011-03-15 01:09:02 +0000 | [diff] [blame] | 83 | |
| 84 | const ASTContext &Context = CGM.getContext(); |
| 85 | |
| 86 | CharUnits FieldOffsetInChars = Context.toCharUnitsFromBits(FieldOffset); |
Chris Lattner | eba3e5a | 2010-04-13 17:45:57 +0000 | [diff] [blame] | 87 | |
Ken Dyck | dc49639 | 2011-03-17 01:33:18 +0000 | [diff] [blame] | 88 | assert(NextFieldOffsetInChars <= FieldOffsetInChars |
Chris Lattner | eba3e5a | 2010-04-13 17:45:57 +0000 | [diff] [blame] | 89 | && "Field offset mismatch!"); |
| 90 | |
Ken Dyck | 7d2f9d5 | 2011-03-18 01:26:17 +0000 | [diff] [blame] | 91 | CharUnits FieldAlignment = getAlignment(InitCst); |
Chris Lattner | eba3e5a | 2010-04-13 17:45:57 +0000 | [diff] [blame] | 92 | |
| 93 | // Round up the field offset to the alignment of the field type. |
Ken Dyck | dc49639 | 2011-03-17 01:33:18 +0000 | [diff] [blame] | 94 | CharUnits AlignedNextFieldOffsetInChars = |
Ken Dyck | 7d2f9d5 | 2011-03-18 01:26:17 +0000 | [diff] [blame] | 95 | NextFieldOffsetInChars.RoundUpToAlignment(FieldAlignment); |
Chris Lattner | eba3e5a | 2010-04-13 17:45:57 +0000 | [diff] [blame] | 96 | |
Ken Dyck | dc49639 | 2011-03-17 01:33:18 +0000 | [diff] [blame] | 97 | if (AlignedNextFieldOffsetInChars > FieldOffsetInChars) { |
Chris Lattner | eba3e5a | 2010-04-13 17:45:57 +0000 | [diff] [blame] | 98 | assert(!Packed && "Alignment is wrong even with a packed struct!"); |
| 99 | |
| 100 | // Convert the struct to a packed struct. |
| 101 | ConvertStructToPacked(); |
| 102 | |
Ken Dyck | dc49639 | 2011-03-17 01:33:18 +0000 | [diff] [blame] | 103 | AlignedNextFieldOffsetInChars = NextFieldOffsetInChars; |
Chris Lattner | eba3e5a | 2010-04-13 17:45:57 +0000 | [diff] [blame] | 104 | } |
| 105 | |
Ken Dyck | dc49639 | 2011-03-17 01:33:18 +0000 | [diff] [blame] | 106 | if (AlignedNextFieldOffsetInChars < FieldOffsetInChars) { |
Chris Lattner | eba3e5a | 2010-04-13 17:45:57 +0000 | [diff] [blame] | 107 | // We need to append padding. |
Ken Dyck | c29ea8f | 2011-03-11 23:42:54 +0000 | [diff] [blame] | 108 | AppendPadding( |
Ken Dyck | dc49639 | 2011-03-17 01:33:18 +0000 | [diff] [blame] | 109 | FieldOffsetInChars - NextFieldOffsetInChars); |
Chris Lattner | eba3e5a | 2010-04-13 17:45:57 +0000 | [diff] [blame] | 110 | |
Ken Dyck | dc49639 | 2011-03-17 01:33:18 +0000 | [diff] [blame] | 111 | assert(NextFieldOffsetInChars == FieldOffsetInChars && |
Chris Lattner | eba3e5a | 2010-04-13 17:45:57 +0000 | [diff] [blame] | 112 | "Did not add enough padding!"); |
| 113 | |
Ken Dyck | dc49639 | 2011-03-17 01:33:18 +0000 | [diff] [blame] | 114 | AlignedNextFieldOffsetInChars = NextFieldOffsetInChars; |
Chris Lattner | eba3e5a | 2010-04-13 17:45:57 +0000 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | // Add the field. |
Chris Lattner | 8ce9e45 | 2010-04-13 18:16:19 +0000 | [diff] [blame] | 118 | Elements.push_back(InitCst); |
Ken Dyck | dc49639 | 2011-03-17 01:33:18 +0000 | [diff] [blame] | 119 | NextFieldOffsetInChars = AlignedNextFieldOffsetInChars + |
Ken Dyck | 15c152e | 2011-03-18 01:12:13 +0000 | [diff] [blame] | 120 | getSizeInChars(InitCst); |
Chris Lattner | eba3e5a | 2010-04-13 17:45:57 +0000 | [diff] [blame] | 121 | |
| 122 | if (Packed) |
Ken Dyck | 7d2f9d5 | 2011-03-18 01:26:17 +0000 | [diff] [blame] | 123 | assert(LLVMStructAlignment == CharUnits::One() && |
| 124 | "Packed struct not byte-aligned!"); |
Chris Lattner | eba3e5a | 2010-04-13 17:45:57 +0000 | [diff] [blame] | 125 | else |
| 126 | LLVMStructAlignment = std::max(LLVMStructAlignment, FieldAlignment); |
| 127 | |
| 128 | return true; |
| 129 | } |
| 130 | |
Chris Lattner | 1880666 | 2010-07-05 17:04:23 +0000 | [diff] [blame] | 131 | void ConstStructBuilder::AppendBitField(const FieldDecl *Field, |
| 132 | uint64_t FieldOffset, |
| 133 | llvm::ConstantInt *CI) { |
Ken Dyck | f004da4 | 2011-03-12 12:03:11 +0000 | [diff] [blame] | 134 | const ASTContext &Context = CGM.getContext(); |
Ken Dyck | 4f318c0 | 2011-03-19 00:57:28 +0000 | [diff] [blame] | 135 | const uint64_t CharWidth = Context.getCharWidth(); |
Ken Dyck | dc49639 | 2011-03-17 01:33:18 +0000 | [diff] [blame] | 136 | uint64_t NextFieldOffsetInBits = Context.toBits(NextFieldOffsetInChars); |
| 137 | if (FieldOffset > NextFieldOffsetInBits) { |
Chris Lattner | eba3e5a | 2010-04-13 17:45:57 +0000 | [diff] [blame] | 138 | // We need to add padding. |
Ken Dyck | f004da4 | 2011-03-12 12:03:11 +0000 | [diff] [blame] | 139 | CharUnits PadSize = Context.toCharUnitsFromBits( |
Ken Dyck | dc49639 | 2011-03-17 01:33:18 +0000 | [diff] [blame] | 140 | llvm::RoundUpToAlignment(FieldOffset - NextFieldOffsetInBits, |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 141 | Context.getTargetInfo().getCharAlign())); |
Chris Lattner | eba3e5a | 2010-04-13 17:45:57 +0000 | [diff] [blame] | 142 | |
Ken Dyck | f004da4 | 2011-03-12 12:03:11 +0000 | [diff] [blame] | 143 | AppendPadding(PadSize); |
Chris Lattner | eba3e5a | 2010-04-13 17:45:57 +0000 | [diff] [blame] | 144 | } |
| 145 | |
Richard Smith | a6b8b2c | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 146 | uint64_t FieldSize = Field->getBitWidthValue(Context); |
Chris Lattner | eba3e5a | 2010-04-13 17:45:57 +0000 | [diff] [blame] | 147 | |
| 148 | llvm::APInt FieldValue = CI->getValue(); |
| 149 | |
| 150 | // Promote the size of FieldValue if necessary |
| 151 | // FIXME: This should never occur, but currently it can because initializer |
| 152 | // constants are cast to bool, and because clang is not enforcing bitfield |
| 153 | // width limits. |
| 154 | if (FieldSize > FieldValue.getBitWidth()) |
Jay Foad | 9f71a8f | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 155 | FieldValue = FieldValue.zext(FieldSize); |
Chris Lattner | eba3e5a | 2010-04-13 17:45:57 +0000 | [diff] [blame] | 156 | |
| 157 | // Truncate the size of FieldValue to the bit field size. |
| 158 | if (FieldSize < FieldValue.getBitWidth()) |
Jay Foad | 9f71a8f | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 159 | FieldValue = FieldValue.trunc(FieldSize); |
Chris Lattner | eba3e5a | 2010-04-13 17:45:57 +0000 | [diff] [blame] | 160 | |
Ken Dyck | dc49639 | 2011-03-17 01:33:18 +0000 | [diff] [blame] | 161 | NextFieldOffsetInBits = Context.toBits(NextFieldOffsetInChars); |
| 162 | if (FieldOffset < NextFieldOffsetInBits) { |
Chris Lattner | eba3e5a | 2010-04-13 17:45:57 +0000 | [diff] [blame] | 163 | // Either part of the field or the entire field can go into the previous |
| 164 | // byte. |
| 165 | assert(!Elements.empty() && "Elements can't be empty!"); |
| 166 | |
Ken Dyck | dc49639 | 2011-03-17 01:33:18 +0000 | [diff] [blame] | 167 | unsigned BitsInPreviousByte = NextFieldOffsetInBits - FieldOffset; |
Chris Lattner | eba3e5a | 2010-04-13 17:45:57 +0000 | [diff] [blame] | 168 | |
| 169 | bool FitsCompletelyInPreviousByte = |
| 170 | BitsInPreviousByte >= FieldValue.getBitWidth(); |
| 171 | |
| 172 | llvm::APInt Tmp = FieldValue; |
| 173 | |
| 174 | if (!FitsCompletelyInPreviousByte) { |
| 175 | unsigned NewFieldWidth = FieldSize - BitsInPreviousByte; |
| 176 | |
| 177 | if (CGM.getTargetData().isBigEndian()) { |
| 178 | Tmp = Tmp.lshr(NewFieldWidth); |
Jay Foad | 9f71a8f | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 179 | Tmp = Tmp.trunc(BitsInPreviousByte); |
Chris Lattner | eba3e5a | 2010-04-13 17:45:57 +0000 | [diff] [blame] | 180 | |
| 181 | // We want the remaining high bits. |
Jay Foad | 9f71a8f | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 182 | FieldValue = FieldValue.trunc(NewFieldWidth); |
Chris Lattner | eba3e5a | 2010-04-13 17:45:57 +0000 | [diff] [blame] | 183 | } else { |
Jay Foad | 9f71a8f | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 184 | Tmp = Tmp.trunc(BitsInPreviousByte); |
Chris Lattner | eba3e5a | 2010-04-13 17:45:57 +0000 | [diff] [blame] | 185 | |
| 186 | // We want the remaining low bits. |
| 187 | FieldValue = FieldValue.lshr(BitsInPreviousByte); |
Jay Foad | 9f71a8f | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 188 | FieldValue = FieldValue.trunc(NewFieldWidth); |
Chris Lattner | eba3e5a | 2010-04-13 17:45:57 +0000 | [diff] [blame] | 189 | } |
| 190 | } |
| 191 | |
Ken Dyck | 4f318c0 | 2011-03-19 00:57:28 +0000 | [diff] [blame] | 192 | Tmp = Tmp.zext(CharWidth); |
Chris Lattner | eba3e5a | 2010-04-13 17:45:57 +0000 | [diff] [blame] | 193 | if (CGM.getTargetData().isBigEndian()) { |
| 194 | if (FitsCompletelyInPreviousByte) |
| 195 | Tmp = Tmp.shl(BitsInPreviousByte - FieldValue.getBitWidth()); |
| 196 | } else { |
Ken Dyck | 4f318c0 | 2011-03-19 00:57:28 +0000 | [diff] [blame] | 197 | Tmp = Tmp.shl(CharWidth - BitsInPreviousByte); |
Chris Lattner | eba3e5a | 2010-04-13 17:45:57 +0000 | [diff] [blame] | 198 | } |
| 199 | |
Chris Lattner | 2bc0025 | 2010-07-05 18:03:30 +0000 | [diff] [blame] | 200 | // 'or' in the bits that go into the previous byte. |
| 201 | llvm::Value *LastElt = Elements.back(); |
| 202 | if (llvm::ConstantInt *Val = dyn_cast<llvm::ConstantInt>(LastElt)) |
Chris Lattner | eba3e5a | 2010-04-13 17:45:57 +0000 | [diff] [blame] | 203 | Tmp |= Val->getValue(); |
Chris Lattner | 2bc0025 | 2010-07-05 18:03:30 +0000 | [diff] [blame] | 204 | else { |
| 205 | assert(isa<llvm::UndefValue>(LastElt)); |
| 206 | // If there is an undef field that we're adding to, it can either be a |
| 207 | // scalar undef (in which case, we just replace it with our field) or it |
| 208 | // is an array. If it is an array, we have to pull one byte off the |
| 209 | // array so that the other undef bytes stay around. |
| 210 | if (!isa<llvm::IntegerType>(LastElt->getType())) { |
| 211 | // The undef padding will be a multibyte array, create a new smaller |
| 212 | // padding and then an hole for our i8 to get plopped into. |
| 213 | assert(isa<llvm::ArrayType>(LastElt->getType()) && |
| 214 | "Expected array padding of undefs"); |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 215 | llvm::ArrayType *AT = cast<llvm::ArrayType>(LastElt->getType()); |
Ken Dyck | 4f318c0 | 2011-03-19 00:57:28 +0000 | [diff] [blame] | 216 | assert(AT->getElementType()->isIntegerTy(CharWidth) && |
Chris Lattner | 2bc0025 | 2010-07-05 18:03:30 +0000 | [diff] [blame] | 217 | AT->getNumElements() != 0 && |
| 218 | "Expected non-empty array padding of undefs"); |
| 219 | |
| 220 | // Remove the padding array. |
Ken Dyck | dc49639 | 2011-03-17 01:33:18 +0000 | [diff] [blame] | 221 | NextFieldOffsetInChars -= CharUnits::fromQuantity(AT->getNumElements()); |
Chris Lattner | 2bc0025 | 2010-07-05 18:03:30 +0000 | [diff] [blame] | 222 | Elements.pop_back(); |
| 223 | |
| 224 | // Add the padding back in two chunks. |
Ken Dyck | c29ea8f | 2011-03-11 23:42:54 +0000 | [diff] [blame] | 225 | AppendPadding(CharUnits::fromQuantity(AT->getNumElements()-1)); |
| 226 | AppendPadding(CharUnits::One()); |
Chris Lattner | 2bc0025 | 2010-07-05 18:03:30 +0000 | [diff] [blame] | 227 | assert(isa<llvm::UndefValue>(Elements.back()) && |
Ken Dyck | 4f318c0 | 2011-03-19 00:57:28 +0000 | [diff] [blame] | 228 | Elements.back()->getType()->isIntegerTy(CharWidth) && |
Chris Lattner | 2bc0025 | 2010-07-05 18:03:30 +0000 | [diff] [blame] | 229 | "Padding addition didn't work right"); |
| 230 | } |
| 231 | } |
Chris Lattner | eba3e5a | 2010-04-13 17:45:57 +0000 | [diff] [blame] | 232 | |
| 233 | Elements.back() = llvm::ConstantInt::get(CGM.getLLVMContext(), Tmp); |
| 234 | |
| 235 | if (FitsCompletelyInPreviousByte) |
Chris Lattner | 1880666 | 2010-07-05 17:04:23 +0000 | [diff] [blame] | 236 | return; |
Chris Lattner | eba3e5a | 2010-04-13 17:45:57 +0000 | [diff] [blame] | 237 | } |
| 238 | |
Ken Dyck | 4f318c0 | 2011-03-19 00:57:28 +0000 | [diff] [blame] | 239 | while (FieldValue.getBitWidth() > CharWidth) { |
Chris Lattner | eba3e5a | 2010-04-13 17:45:57 +0000 | [diff] [blame] | 240 | llvm::APInt Tmp; |
| 241 | |
| 242 | if (CGM.getTargetData().isBigEndian()) { |
| 243 | // We want the high bits. |
Ken Dyck | 4f318c0 | 2011-03-19 00:57:28 +0000 | [diff] [blame] | 244 | Tmp = |
| 245 | FieldValue.lshr(FieldValue.getBitWidth() - CharWidth).trunc(CharWidth); |
Chris Lattner | eba3e5a | 2010-04-13 17:45:57 +0000 | [diff] [blame] | 246 | } else { |
| 247 | // We want the low bits. |
Ken Dyck | 4f318c0 | 2011-03-19 00:57:28 +0000 | [diff] [blame] | 248 | Tmp = FieldValue.trunc(CharWidth); |
Chris Lattner | eba3e5a | 2010-04-13 17:45:57 +0000 | [diff] [blame] | 249 | |
Ken Dyck | 4f318c0 | 2011-03-19 00:57:28 +0000 | [diff] [blame] | 250 | FieldValue = FieldValue.lshr(CharWidth); |
Chris Lattner | eba3e5a | 2010-04-13 17:45:57 +0000 | [diff] [blame] | 251 | } |
| 252 | |
| 253 | Elements.push_back(llvm::ConstantInt::get(CGM.getLLVMContext(), Tmp)); |
Ken Dyck | 199c189 | 2011-03-19 01:28:06 +0000 | [diff] [blame] | 254 | ++NextFieldOffsetInChars; |
Chris Lattner | eba3e5a | 2010-04-13 17:45:57 +0000 | [diff] [blame] | 255 | |
Ken Dyck | 4f318c0 | 2011-03-19 00:57:28 +0000 | [diff] [blame] | 256 | FieldValue = FieldValue.trunc(FieldValue.getBitWidth() - CharWidth); |
Chris Lattner | eba3e5a | 2010-04-13 17:45:57 +0000 | [diff] [blame] | 257 | } |
| 258 | |
| 259 | assert(FieldValue.getBitWidth() > 0 && |
| 260 | "Should have at least one bit left!"); |
Ken Dyck | 4f318c0 | 2011-03-19 00:57:28 +0000 | [diff] [blame] | 261 | assert(FieldValue.getBitWidth() <= CharWidth && |
Chris Lattner | eba3e5a | 2010-04-13 17:45:57 +0000 | [diff] [blame] | 262 | "Should not have more than a byte left!"); |
| 263 | |
Ken Dyck | 4f318c0 | 2011-03-19 00:57:28 +0000 | [diff] [blame] | 264 | if (FieldValue.getBitWidth() < CharWidth) { |
Chris Lattner | eba3e5a | 2010-04-13 17:45:57 +0000 | [diff] [blame] | 265 | if (CGM.getTargetData().isBigEndian()) { |
| 266 | unsigned BitWidth = FieldValue.getBitWidth(); |
| 267 | |
Ken Dyck | 4f318c0 | 2011-03-19 00:57:28 +0000 | [diff] [blame] | 268 | FieldValue = FieldValue.zext(CharWidth) << (CharWidth - BitWidth); |
Chris Lattner | eba3e5a | 2010-04-13 17:45:57 +0000 | [diff] [blame] | 269 | } else |
Ken Dyck | 4f318c0 | 2011-03-19 00:57:28 +0000 | [diff] [blame] | 270 | FieldValue = FieldValue.zext(CharWidth); |
Chris Lattner | eba3e5a | 2010-04-13 17:45:57 +0000 | [diff] [blame] | 271 | } |
| 272 | |
| 273 | // Append the last element. |
| 274 | Elements.push_back(llvm::ConstantInt::get(CGM.getLLVMContext(), |
| 275 | FieldValue)); |
Ken Dyck | 199c189 | 2011-03-19 01:28:06 +0000 | [diff] [blame] | 276 | ++NextFieldOffsetInChars; |
Chris Lattner | eba3e5a | 2010-04-13 17:45:57 +0000 | [diff] [blame] | 277 | } |
| 278 | |
Ken Dyck | c29ea8f | 2011-03-11 23:42:54 +0000 | [diff] [blame] | 279 | void ConstStructBuilder::AppendPadding(CharUnits PadSize) { |
| 280 | if (PadSize.isZero()) |
Chris Lattner | eba3e5a | 2010-04-13 17:45:57 +0000 | [diff] [blame] | 281 | return; |
| 282 | |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 283 | llvm::Type *Ty = llvm::Type::getInt8Ty(CGM.getLLVMContext()); |
Ken Dyck | c29ea8f | 2011-03-11 23:42:54 +0000 | [diff] [blame] | 284 | if (PadSize > CharUnits::One()) |
| 285 | Ty = llvm::ArrayType::get(Ty, PadSize.getQuantity()); |
Chris Lattner | eba3e5a | 2010-04-13 17:45:57 +0000 | [diff] [blame] | 286 | |
Nuno Lopes | cdb30b4 | 2010-04-16 20:56:35 +0000 | [diff] [blame] | 287 | llvm::Constant *C = llvm::UndefValue::get(Ty); |
Chris Lattner | eba3e5a | 2010-04-13 17:45:57 +0000 | [diff] [blame] | 288 | Elements.push_back(C); |
Ken Dyck | 7d2f9d5 | 2011-03-18 01:26:17 +0000 | [diff] [blame] | 289 | assert(getAlignment(C) == CharUnits::One() && |
| 290 | "Padding must have 1 byte alignment!"); |
Chris Lattner | eba3e5a | 2010-04-13 17:45:57 +0000 | [diff] [blame] | 291 | |
Ken Dyck | 15c152e | 2011-03-18 01:12:13 +0000 | [diff] [blame] | 292 | NextFieldOffsetInChars += getSizeInChars(C); |
Chris Lattner | eba3e5a | 2010-04-13 17:45:57 +0000 | [diff] [blame] | 293 | } |
| 294 | |
Ken Dyck | 106ca04 | 2011-03-11 02:17:05 +0000 | [diff] [blame] | 295 | void ConstStructBuilder::AppendTailPadding(CharUnits RecordSize) { |
Ken Dyck | dc49639 | 2011-03-17 01:33:18 +0000 | [diff] [blame] | 296 | assert(NextFieldOffsetInChars <= RecordSize && |
Ken Dyck | 106ca04 | 2011-03-11 02:17:05 +0000 | [diff] [blame] | 297 | "Size mismatch!"); |
Chris Lattner | eba3e5a | 2010-04-13 17:45:57 +0000 | [diff] [blame] | 298 | |
Ken Dyck | dc49639 | 2011-03-17 01:33:18 +0000 | [diff] [blame] | 299 | AppendPadding(RecordSize - NextFieldOffsetInChars); |
Chris Lattner | eba3e5a | 2010-04-13 17:45:57 +0000 | [diff] [blame] | 300 | } |
| 301 | |
| 302 | void ConstStructBuilder::ConvertStructToPacked() { |
| 303 | std::vector<llvm::Constant *> PackedElements; |
Ken Dyck | 4a5c87e | 2011-03-18 00:55:06 +0000 | [diff] [blame] | 304 | CharUnits ElementOffsetInChars = CharUnits::Zero(); |
Chris Lattner | eba3e5a | 2010-04-13 17:45:57 +0000 | [diff] [blame] | 305 | |
| 306 | for (unsigned i = 0, e = Elements.size(); i != e; ++i) { |
| 307 | llvm::Constant *C = Elements[i]; |
| 308 | |
Ken Dyck | 7d2f9d5 | 2011-03-18 01:26:17 +0000 | [diff] [blame] | 309 | CharUnits ElementAlign = CharUnits::fromQuantity( |
| 310 | CGM.getTargetData().getABITypeAlignment(C->getType())); |
Ken Dyck | 4a5c87e | 2011-03-18 00:55:06 +0000 | [diff] [blame] | 311 | CharUnits AlignedElementOffsetInChars = |
Ken Dyck | 7d2f9d5 | 2011-03-18 01:26:17 +0000 | [diff] [blame] | 312 | ElementOffsetInChars.RoundUpToAlignment(ElementAlign); |
Chris Lattner | eba3e5a | 2010-04-13 17:45:57 +0000 | [diff] [blame] | 313 | |
Ken Dyck | 4a5c87e | 2011-03-18 00:55:06 +0000 | [diff] [blame] | 314 | if (AlignedElementOffsetInChars > ElementOffsetInChars) { |
Chris Lattner | eba3e5a | 2010-04-13 17:45:57 +0000 | [diff] [blame] | 315 | // We need some padding. |
Ken Dyck | 4a5c87e | 2011-03-18 00:55:06 +0000 | [diff] [blame] | 316 | CharUnits NumChars = |
| 317 | AlignedElementOffsetInChars - ElementOffsetInChars; |
Chris Lattner | eba3e5a | 2010-04-13 17:45:57 +0000 | [diff] [blame] | 318 | |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 319 | llvm::Type *Ty = llvm::Type::getInt8Ty(CGM.getLLVMContext()); |
Ken Dyck | 4a5c87e | 2011-03-18 00:55:06 +0000 | [diff] [blame] | 320 | if (NumChars > CharUnits::One()) |
| 321 | Ty = llvm::ArrayType::get(Ty, NumChars.getQuantity()); |
Chris Lattner | eba3e5a | 2010-04-13 17:45:57 +0000 | [diff] [blame] | 322 | |
Nuno Lopes | cdb30b4 | 2010-04-16 20:56:35 +0000 | [diff] [blame] | 323 | llvm::Constant *Padding = llvm::UndefValue::get(Ty); |
Chris Lattner | eba3e5a | 2010-04-13 17:45:57 +0000 | [diff] [blame] | 324 | PackedElements.push_back(Padding); |
Ken Dyck | 15c152e | 2011-03-18 01:12:13 +0000 | [diff] [blame] | 325 | ElementOffsetInChars += getSizeInChars(Padding); |
Chris Lattner | eba3e5a | 2010-04-13 17:45:57 +0000 | [diff] [blame] | 326 | } |
| 327 | |
| 328 | PackedElements.push_back(C); |
Ken Dyck | 15c152e | 2011-03-18 01:12:13 +0000 | [diff] [blame] | 329 | ElementOffsetInChars += getSizeInChars(C); |
Chris Lattner | eba3e5a | 2010-04-13 17:45:57 +0000 | [diff] [blame] | 330 | } |
| 331 | |
Ken Dyck | 4a5c87e | 2011-03-18 00:55:06 +0000 | [diff] [blame] | 332 | assert(ElementOffsetInChars == NextFieldOffsetInChars && |
Chris Lattner | eba3e5a | 2010-04-13 17:45:57 +0000 | [diff] [blame] | 333 | "Packing the struct changed its size!"); |
| 334 | |
| 335 | Elements = PackedElements; |
Ken Dyck | 7d2f9d5 | 2011-03-18 01:26:17 +0000 | [diff] [blame] | 336 | LLVMStructAlignment = CharUnits::One(); |
Chris Lattner | eba3e5a | 2010-04-13 17:45:57 +0000 | [diff] [blame] | 337 | Packed = true; |
| 338 | } |
| 339 | |
| 340 | bool ConstStructBuilder::Build(InitListExpr *ILE) { |
| 341 | RecordDecl *RD = ILE->getType()->getAs<RecordType>()->getDecl(); |
| 342 | const ASTRecordLayout &Layout = CGM.getContext().getASTRecordLayout(RD); |
| 343 | |
| 344 | unsigned FieldNo = 0; |
| 345 | unsigned ElementNo = 0; |
Fariborz Jahanian | 07a8a21 | 2011-04-28 22:49:46 +0000 | [diff] [blame] | 346 | const FieldDecl *LastFD = 0; |
| 347 | bool IsMsStruct = RD->hasAttr<MsStructAttr>(); |
| 348 | |
Chris Lattner | eba3e5a | 2010-04-13 17:45:57 +0000 | [diff] [blame] | 349 | for (RecordDecl::field_iterator Field = RD->field_begin(), |
Chris Lattner | 8ce9e45 | 2010-04-13 18:16:19 +0000 | [diff] [blame] | 350 | FieldEnd = RD->field_end(); Field != FieldEnd; ++Field, ++FieldNo) { |
Fariborz Jahanian | 07a8a21 | 2011-04-28 22:49:46 +0000 | [diff] [blame] | 351 | if (IsMsStruct) { |
| 352 | // Zero-length bitfields following non-bitfield members are |
| 353 | // ignored: |
Fariborz Jahanian | 855a8e7 | 2011-05-03 20:21:04 +0000 | [diff] [blame] | 354 | if (CGM.getContext().ZeroBitfieldFollowsNonBitfield((*Field), LastFD)) { |
Fariborz Jahanian | 07a8a21 | 2011-04-28 22:49:46 +0000 | [diff] [blame] | 355 | --FieldNo; |
| 356 | continue; |
| 357 | } |
| 358 | LastFD = (*Field); |
| 359 | } |
Chris Lattner | 8ce9e45 | 2010-04-13 18:16:19 +0000 | [diff] [blame] | 360 | |
| 361 | // If this is a union, skip all the fields that aren't being initialized. |
Chris Lattner | eba3e5a | 2010-04-13 17:45:57 +0000 | [diff] [blame] | 362 | if (RD->isUnion() && ILE->getInitializedFieldInUnion() != *Field) |
| 363 | continue; |
| 364 | |
Chris Lattner | 8ce9e45 | 2010-04-13 18:16:19 +0000 | [diff] [blame] | 365 | // Don't emit anonymous bitfields, they just affect layout. |
Eli Friedman | dbff1a0 | 2011-08-23 20:22:34 +0000 | [diff] [blame] | 366 | if (Field->isUnnamedBitfield()) { |
Fariborz Jahanian | 07a8a21 | 2011-04-28 22:49:46 +0000 | [diff] [blame] | 367 | LastFD = (*Field); |
Chris Lattner | 8ce9e45 | 2010-04-13 18:16:19 +0000 | [diff] [blame] | 368 | continue; |
Fariborz Jahanian | 07a8a21 | 2011-04-28 22:49:46 +0000 | [diff] [blame] | 369 | } |
Chris Lattner | eba3e5a | 2010-04-13 17:45:57 +0000 | [diff] [blame] | 370 | |
Chris Lattner | 8ce9e45 | 2010-04-13 18:16:19 +0000 | [diff] [blame] | 371 | // Get the initializer. A struct can include fields without initializers, |
| 372 | // we just use explicit null values for them. |
| 373 | llvm::Constant *EltInit; |
| 374 | if (ElementNo < ILE->getNumInits()) |
| 375 | EltInit = CGM.EmitConstantExpr(ILE->getInit(ElementNo++), |
| 376 | Field->getType(), CGF); |
| 377 | else |
| 378 | EltInit = CGM.EmitNullConstant(Field->getType()); |
Eli Friedman | f0ca0ee | 2010-07-17 23:55:01 +0000 | [diff] [blame] | 379 | |
| 380 | if (!EltInit) |
| 381 | return false; |
Chris Lattner | 8ce9e45 | 2010-04-13 18:16:19 +0000 | [diff] [blame] | 382 | |
| 383 | if (!Field->isBitField()) { |
| 384 | // Handle non-bitfield members. |
| 385 | if (!AppendField(*Field, Layout.getFieldOffset(FieldNo), EltInit)) |
Chris Lattner | eba3e5a | 2010-04-13 17:45:57 +0000 | [diff] [blame] | 386 | return false; |
| 387 | } else { |
Chris Lattner | 8ce9e45 | 2010-04-13 18:16:19 +0000 | [diff] [blame] | 388 | // Otherwise we have a bitfield. |
Chris Lattner | 1880666 | 2010-07-05 17:04:23 +0000 | [diff] [blame] | 389 | AppendBitField(*Field, Layout.getFieldOffset(FieldNo), |
| 390 | cast<llvm::ConstantInt>(EltInit)); |
Chris Lattner | eba3e5a | 2010-04-13 17:45:57 +0000 | [diff] [blame] | 391 | } |
Chris Lattner | eba3e5a | 2010-04-13 17:45:57 +0000 | [diff] [blame] | 392 | } |
| 393 | |
Ken Dyck | dc49639 | 2011-03-17 01:33:18 +0000 | [diff] [blame] | 394 | CharUnits LayoutSizeInChars = Layout.getSize(); |
Chris Lattner | eba3e5a | 2010-04-13 17:45:57 +0000 | [diff] [blame] | 395 | |
Ken Dyck | dc49639 | 2011-03-17 01:33:18 +0000 | [diff] [blame] | 396 | if (NextFieldOffsetInChars > LayoutSizeInChars) { |
Chris Lattner | eba3e5a | 2010-04-13 17:45:57 +0000 | [diff] [blame] | 397 | // If the struct is bigger than the size of the record type, |
| 398 | // we must have a flexible array member at the end. |
| 399 | assert(RD->hasFlexibleArrayMember() && |
| 400 | "Must have flexible array member if struct is bigger than type!"); |
| 401 | |
| 402 | // No tail padding is necessary. |
| 403 | return true; |
| 404 | } |
| 405 | |
Ken Dyck | dc49639 | 2011-03-17 01:33:18 +0000 | [diff] [blame] | 406 | CharUnits LLVMSizeInChars = |
Ken Dyck | 7d2f9d5 | 2011-03-18 01:26:17 +0000 | [diff] [blame] | 407 | NextFieldOffsetInChars.RoundUpToAlignment(LLVMStructAlignment); |
Chris Lattner | eba3e5a | 2010-04-13 17:45:57 +0000 | [diff] [blame] | 408 | |
| 409 | // Check if we need to convert the struct to a packed struct. |
Ken Dyck | dc49639 | 2011-03-17 01:33:18 +0000 | [diff] [blame] | 410 | if (NextFieldOffsetInChars <= LayoutSizeInChars && |
| 411 | LLVMSizeInChars > LayoutSizeInChars) { |
Chris Lattner | eba3e5a | 2010-04-13 17:45:57 +0000 | [diff] [blame] | 412 | assert(!Packed && "Size mismatch!"); |
| 413 | |
| 414 | ConvertStructToPacked(); |
Ken Dyck | dc49639 | 2011-03-17 01:33:18 +0000 | [diff] [blame] | 415 | assert(NextFieldOffsetInChars <= LayoutSizeInChars && |
Chris Lattner | eba3e5a | 2010-04-13 17:45:57 +0000 | [diff] [blame] | 416 | "Converting to packed did not help!"); |
| 417 | } |
| 418 | |
| 419 | // Append tail padding if necessary. |
Ken Dyck | dc49639 | 2011-03-17 01:33:18 +0000 | [diff] [blame] | 420 | AppendTailPadding(LayoutSizeInChars); |
Chris Lattner | eba3e5a | 2010-04-13 17:45:57 +0000 | [diff] [blame] | 421 | |
Ken Dyck | dc49639 | 2011-03-17 01:33:18 +0000 | [diff] [blame] | 422 | assert(LayoutSizeInChars == NextFieldOffsetInChars && |
Chris Lattner | eba3e5a | 2010-04-13 17:45:57 +0000 | [diff] [blame] | 423 | "Tail padding mismatch!"); |
| 424 | |
| 425 | return true; |
| 426 | } |
| 427 | |
| 428 | llvm::Constant *ConstStructBuilder:: |
| 429 | BuildStruct(CodeGenModule &CGM, CodeGenFunction *CGF, InitListExpr *ILE) { |
| 430 | ConstStructBuilder Builder(CGM, CGF); |
| 431 | |
| 432 | if (!Builder.Build(ILE)) |
| 433 | return 0; |
| 434 | |
Chris Lattner | c5cbb90 | 2011-06-20 04:01:35 +0000 | [diff] [blame] | 435 | // Pick the type to use. If the type is layout identical to the ConvertType |
| 436 | // type then use it, otherwise use whatever the builder produced for us. |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 437 | llvm::StructType *STy = |
Chris Lattner | c5cbb90 | 2011-06-20 04:01:35 +0000 | [diff] [blame] | 438 | llvm::ConstantStruct::getTypeForElements(CGM.getLLVMContext(), |
| 439 | Builder.Elements,Builder.Packed); |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 440 | llvm::Type *ILETy = CGM.getTypes().ConvertType(ILE->getType()); |
| 441 | if (llvm::StructType *ILESTy = dyn_cast<llvm::StructType>(ILETy)) { |
Chris Lattner | c5cbb90 | 2011-06-20 04:01:35 +0000 | [diff] [blame] | 442 | if (ILESTy->isLayoutIdentical(STy)) |
| 443 | STy = ILESTy; |
| 444 | } |
| 445 | |
Chris Lattner | eba3e5a | 2010-04-13 17:45:57 +0000 | [diff] [blame] | 446 | llvm::Constant *Result = |
Chris Lattner | c5cbb90 | 2011-06-20 04:01:35 +0000 | [diff] [blame] | 447 | llvm::ConstantStruct::get(STy, Builder.Elements); |
Chris Lattner | eba3e5a | 2010-04-13 17:45:57 +0000 | [diff] [blame] | 448 | |
Ken Dyck | 15c152e | 2011-03-18 01:12:13 +0000 | [diff] [blame] | 449 | assert(Builder.NextFieldOffsetInChars.RoundUpToAlignment( |
Ken Dyck | 7d2f9d5 | 2011-03-18 01:26:17 +0000 | [diff] [blame] | 450 | Builder.getAlignment(Result)) == |
Ken Dyck | 15c152e | 2011-03-18 01:12:13 +0000 | [diff] [blame] | 451 | Builder.getSizeInChars(Result) && "Size mismatch!"); |
Chris Lattner | eba3e5a | 2010-04-13 17:45:57 +0000 | [diff] [blame] | 452 | |
| 453 | return Result; |
| 454 | } |
| 455 | |
| 456 | |
| 457 | //===----------------------------------------------------------------------===// |
| 458 | // ConstExprEmitter |
| 459 | //===----------------------------------------------------------------------===// |
| 460 | |
Benjamin Kramer | 85b4521 | 2009-11-28 19:45:26 +0000 | [diff] [blame] | 461 | class ConstExprEmitter : |
Anders Carlsson | 84005b4 | 2008-01-26 04:30:23 +0000 | [diff] [blame] | 462 | public StmtVisitor<ConstExprEmitter, llvm::Constant*> { |
Anders Carlsson | 3b1d57b | 2008-01-26 01:36:00 +0000 | [diff] [blame] | 463 | CodeGenModule &CGM; |
Lauro Ramos Venancio | 8137335 | 2008-02-26 21:41:45 +0000 | [diff] [blame] | 464 | CodeGenFunction *CGF; |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 465 | llvm::LLVMContext &VMContext; |
Anders Carlsson | 3b1d57b | 2008-01-26 01:36:00 +0000 | [diff] [blame] | 466 | public: |
Lauro Ramos Venancio | 8137335 | 2008-02-26 21:41:45 +0000 | [diff] [blame] | 467 | ConstExprEmitter(CodeGenModule &cgm, CodeGenFunction *cgf) |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 468 | : CGM(cgm), CGF(cgf), VMContext(cgm.getLLVMContext()) { |
Anders Carlsson | 3b1d57b | 2008-01-26 01:36:00 +0000 | [diff] [blame] | 469 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 470 | |
Anders Carlsson | 3b1d57b | 2008-01-26 01:36:00 +0000 | [diff] [blame] | 471 | //===--------------------------------------------------------------------===// |
| 472 | // Visitor Methods |
| 473 | //===--------------------------------------------------------------------===// |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 474 | |
Anders Carlsson | 3b1d57b | 2008-01-26 01:36:00 +0000 | [diff] [blame] | 475 | llvm::Constant *VisitStmt(Stmt *S) { |
Anders Carlsson | 069880e | 2009-03-03 16:43:34 +0000 | [diff] [blame] | 476 | return 0; |
Anders Carlsson | 3b1d57b | 2008-01-26 01:36:00 +0000 | [diff] [blame] | 477 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 478 | |
| 479 | llvm::Constant *VisitParenExpr(ParenExpr *PE) { |
| 480 | return Visit(PE->getSubExpr()); |
Anders Carlsson | 3b1d57b | 2008-01-26 01:36:00 +0000 | [diff] [blame] | 481 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 482 | |
John McCall | 91a5755 | 2011-07-15 05:09:51 +0000 | [diff] [blame] | 483 | llvm::Constant * |
| 484 | VisitSubstNonTypeTemplateParmExpr(SubstNonTypeTemplateParmExpr *PE) { |
| 485 | return Visit(PE->getReplacement()); |
| 486 | } |
| 487 | |
Peter Collingbourne | f111d93 | 2011-04-15 00:35:48 +0000 | [diff] [blame] | 488 | llvm::Constant *VisitGenericSelectionExpr(GenericSelectionExpr *GE) { |
| 489 | return Visit(GE->getResultExpr()); |
| 490 | } |
| 491 | |
Anders Carlsson | 3b1d57b | 2008-01-26 01:36:00 +0000 | [diff] [blame] | 492 | llvm::Constant *VisitCompoundLiteralExpr(CompoundLiteralExpr *E) { |
| 493 | return Visit(E->getInitializer()); |
| 494 | } |
John McCall | 5808ce4 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 495 | |
Anders Carlsson | f57b4e4 | 2009-10-03 15:02:02 +0000 | [diff] [blame] | 496 | llvm::Constant *VisitUnaryAddrOf(UnaryOperator *E) { |
John McCall | 5808ce4 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 497 | if (E->getType()->isMemberPointerType()) |
| 498 | return CGM.getMemberPointerConstant(E); |
Anders Carlsson | 45147d0 | 2010-02-02 03:37:46 +0000 | [diff] [blame] | 499 | |
Anders Carlsson | f57b4e4 | 2009-10-03 15:02:02 +0000 | [diff] [blame] | 500 | return 0; |
| 501 | } |
| 502 | |
Chris Lattner | 3ae9f48 | 2009-10-13 07:14:16 +0000 | [diff] [blame] | 503 | llvm::Constant *VisitBinSub(BinaryOperator *E) { |
| 504 | // This must be a pointer/pointer subtraction. This only happens for |
| 505 | // address of label. |
| 506 | if (!isa<AddrLabelExpr>(E->getLHS()->IgnoreParenNoopCasts(CGM.getContext())) || |
| 507 | !isa<AddrLabelExpr>(E->getRHS()->IgnoreParenNoopCasts(CGM.getContext()))) |
| 508 | return 0; |
| 509 | |
| 510 | llvm::Constant *LHS = CGM.EmitConstantExpr(E->getLHS(), |
| 511 | E->getLHS()->getType(), CGF); |
| 512 | llvm::Constant *RHS = CGM.EmitConstantExpr(E->getRHS(), |
| 513 | E->getRHS()->getType(), CGF); |
| 514 | |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 515 | llvm::Type *ResultType = ConvertType(E->getType()); |
Chris Lattner | 3ae9f48 | 2009-10-13 07:14:16 +0000 | [diff] [blame] | 516 | LHS = llvm::ConstantExpr::getPtrToInt(LHS, ResultType); |
| 517 | RHS = llvm::ConstantExpr::getPtrToInt(RHS, ResultType); |
| 518 | |
| 519 | // No need to divide by element size, since addr of label is always void*, |
| 520 | // which has size 1 in GNUish. |
| 521 | return llvm::ConstantExpr::getSub(LHS, RHS); |
| 522 | } |
| 523 | |
Argyrios Kyrtzidis | 0835a3c | 2008-08-18 23:01:59 +0000 | [diff] [blame] | 524 | llvm::Constant *VisitCastExpr(CastExpr* E) { |
John McCall | 8bba1f4 | 2011-03-15 21:17:48 +0000 | [diff] [blame] | 525 | Expr *subExpr = E->getSubExpr(); |
| 526 | llvm::Constant *C = CGM.EmitConstantExpr(subExpr, subExpr->getType(), CGF); |
| 527 | if (!C) return 0; |
| 528 | |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 529 | llvm::Type *destType = ConvertType(E->getType()); |
John McCall | 8bba1f4 | 2011-03-15 21:17:48 +0000 | [diff] [blame] | 530 | |
Anders Carlsson | 0086ccb | 2009-08-22 23:54:44 +0000 | [diff] [blame] | 531 | switch (E->getCastKind()) { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 532 | case CK_ToUnion: { |
Anders Carlsson | 0086ccb | 2009-08-22 23:54:44 +0000 | [diff] [blame] | 533 | // GCC cast to union extension |
| 534 | assert(E->getType()->isUnionType() && |
| 535 | "Destination type is not union type!"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 536 | |
Anders Carlsson | aaed50d | 2009-07-31 21:38:39 +0000 | [diff] [blame] | 537 | // Build a struct with the union sub-element as the first member, |
| 538 | // and padded to the appropriate size |
| 539 | std::vector<llvm::Constant*> Elts; |
Jay Foad | ef6de3d | 2011-07-11 09:56:20 +0000 | [diff] [blame] | 540 | std::vector<llvm::Type*> Types; |
Anders Carlsson | aaed50d | 2009-07-31 21:38:39 +0000 | [diff] [blame] | 541 | Elts.push_back(C); |
| 542 | Types.push_back(C->getType()); |
| 543 | unsigned CurSize = CGM.getTargetData().getTypeAllocSize(C->getType()); |
John McCall | 8bba1f4 | 2011-03-15 21:17:48 +0000 | [diff] [blame] | 544 | unsigned TotalSize = CGM.getTargetData().getTypeAllocSize(destType); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 545 | |
Anders Carlsson | aaed50d | 2009-07-31 21:38:39 +0000 | [diff] [blame] | 546 | assert(CurSize <= TotalSize && "Union size mismatch!"); |
| 547 | if (unsigned NumPadBytes = TotalSize - CurSize) { |
Jay Foad | ef6de3d | 2011-07-11 09:56:20 +0000 | [diff] [blame] | 548 | llvm::Type *Ty = llvm::Type::getInt8Ty(VMContext); |
Anders Carlsson | aaed50d | 2009-07-31 21:38:39 +0000 | [diff] [blame] | 549 | if (NumPadBytes > 1) |
| 550 | Ty = llvm::ArrayType::get(Ty, NumPadBytes); |
Anders Carlsson | 0086ccb | 2009-08-22 23:54:44 +0000 | [diff] [blame] | 551 | |
Nuno Lopes | cdb30b4 | 2010-04-16 20:56:35 +0000 | [diff] [blame] | 552 | Elts.push_back(llvm::UndefValue::get(Ty)); |
Anders Carlsson | aaed50d | 2009-07-31 21:38:39 +0000 | [diff] [blame] | 553 | Types.push_back(Ty); |
| 554 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 555 | |
Anders Carlsson | 0086ccb | 2009-08-22 23:54:44 +0000 | [diff] [blame] | 556 | llvm::StructType* STy = |
| 557 | llvm::StructType::get(C->getType()->getContext(), Types, false); |
Anders Carlsson | aaed50d | 2009-07-31 21:38:39 +0000 | [diff] [blame] | 558 | return llvm::ConstantStruct::get(STy, Elts); |
Nuno Lopes | 81e51e2 | 2009-01-17 00:48:48 +0000 | [diff] [blame] | 559 | } |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 560 | case CK_NullToMemberPointer: { |
John McCall | d608cdb | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 561 | const MemberPointerType *MPT = E->getType()->getAs<MemberPointerType>(); |
John McCall | 0bab0cd | 2010-08-23 01:21:21 +0000 | [diff] [blame] | 562 | return CGM.getCXXABI().EmitNullMemberPointer(MPT); |
John McCall | d608cdb | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 563 | } |
Anders Carlsson | 2c51f09 | 2009-10-03 15:13:22 +0000 | [diff] [blame] | 564 | |
John McCall | 8bba1f4 | 2011-03-15 21:17:48 +0000 | [diff] [blame] | 565 | case CK_DerivedToBaseMemberPointer: |
| 566 | case CK_BaseToDerivedMemberPointer: |
John McCall | 0bab0cd | 2010-08-23 01:21:21 +0000 | [diff] [blame] | 567 | return CGM.getCXXABI().EmitMemberPointerConversion(C, E); |
Anders Carlsson | bb378cb | 2009-10-18 20:31:03 +0000 | [diff] [blame] | 568 | |
John McCall | 8bba1f4 | 2011-03-15 21:17:48 +0000 | [diff] [blame] | 569 | case CK_LValueToRValue: |
| 570 | case CK_NoOp: |
| 571 | return C; |
Anders Carlsson | bb378cb | 2009-10-18 20:31:03 +0000 | [diff] [blame] | 572 | |
John McCall | 1d9b3b2 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 573 | case CK_CPointerToObjCPointerCast: |
| 574 | case CK_BlockPointerToObjCPointerCast: |
John McCall | 8bba1f4 | 2011-03-15 21:17:48 +0000 | [diff] [blame] | 575 | case CK_AnyPointerToBlockPointerCast: |
John McCall | 8bba1f4 | 2011-03-15 21:17:48 +0000 | [diff] [blame] | 576 | case CK_BitCast: |
| 577 | if (C->getType() == destType) return C; |
| 578 | return llvm::ConstantExpr::getBitCast(C, destType); |
Chris Lattner | 430656e | 2009-10-13 22:12:09 +0000 | [diff] [blame] | 579 | |
John McCall | 8bba1f4 | 2011-03-15 21:17:48 +0000 | [diff] [blame] | 580 | case CK_Dependent: llvm_unreachable("saw dependent cast!"); |
| 581 | |
| 582 | // These will never be supported. |
| 583 | case CK_ObjCObjectLValueCast: |
John McCall | 8bba1f4 | 2011-03-15 21:17:48 +0000 | [diff] [blame] | 584 | case CK_ToVoid: |
| 585 | case CK_Dynamic: |
John McCall | 33e56f3 | 2011-09-10 06:18:15 +0000 | [diff] [blame] | 586 | case CK_ARCProduceObject: |
| 587 | case CK_ARCConsumeObject: |
| 588 | case CK_ARCReclaimReturnedObject: |
| 589 | case CK_ARCExtendBlockObject: |
Eli Friedman | b000459 | 2011-10-24 22:25:55 +0000 | [diff] [blame] | 590 | case CK_LValueBitCast: |
Anders Carlsson | 0086ccb | 2009-08-22 23:54:44 +0000 | [diff] [blame] | 591 | return 0; |
John McCall | 8bba1f4 | 2011-03-15 21:17:48 +0000 | [diff] [blame] | 592 | |
| 593 | // These might need to be supported for constexpr. |
| 594 | case CK_UserDefinedConversion: |
| 595 | case CK_ConstructorConversion: |
| 596 | return 0; |
| 597 | |
| 598 | // These should eventually be supported. |
| 599 | case CK_ArrayToPointerDecay: |
| 600 | case CK_FunctionToPointerDecay: |
| 601 | case CK_BaseToDerived: |
| 602 | case CK_DerivedToBase: |
| 603 | case CK_UncheckedDerivedToBase: |
| 604 | case CK_MemberPointerToBoolean: |
| 605 | case CK_VectorSplat: |
| 606 | case CK_FloatingRealToComplex: |
| 607 | case CK_FloatingComplexToReal: |
| 608 | case CK_FloatingComplexToBoolean: |
| 609 | case CK_FloatingComplexCast: |
| 610 | case CK_FloatingComplexToIntegralComplex: |
| 611 | case CK_IntegralRealToComplex: |
| 612 | case CK_IntegralComplexToReal: |
| 613 | case CK_IntegralComplexToBoolean: |
| 614 | case CK_IntegralComplexCast: |
| 615 | case CK_IntegralComplexToFloatingComplex: |
| 616 | return 0; |
| 617 | |
| 618 | case CK_PointerToIntegral: |
| 619 | if (!E->getType()->isBooleanType()) |
| 620 | return llvm::ConstantExpr::getPtrToInt(C, destType); |
| 621 | // fallthrough |
| 622 | |
| 623 | case CK_PointerToBoolean: |
| 624 | return llvm::ConstantExpr::getICmp(llvm::CmpInst::ICMP_EQ, C, |
| 625 | llvm::ConstantPointerNull::get(cast<llvm::PointerType>(C->getType()))); |
| 626 | |
| 627 | case CK_NullToPointer: |
| 628 | return llvm::ConstantPointerNull::get(cast<llvm::PointerType>(destType)); |
| 629 | |
| 630 | case CK_IntegralCast: { |
Douglas Gregor | 575a1c9 | 2011-05-20 16:38:50 +0000 | [diff] [blame] | 631 | bool isSigned = subExpr->getType()->isSignedIntegerOrEnumerationType(); |
John McCall | 8bba1f4 | 2011-03-15 21:17:48 +0000 | [diff] [blame] | 632 | return llvm::ConstantExpr::getIntegerCast(C, destType, isSigned); |
Eli Friedman | ddd2b6e | 2009-02-22 07:29:04 +0000 | [diff] [blame] | 633 | } |
John McCall | 8bba1f4 | 2011-03-15 21:17:48 +0000 | [diff] [blame] | 634 | |
| 635 | case CK_IntegralToPointer: { |
Douglas Gregor | 575a1c9 | 2011-05-20 16:38:50 +0000 | [diff] [blame] | 636 | bool isSigned = subExpr->getType()->isSignedIntegerOrEnumerationType(); |
John McCall | 8bba1f4 | 2011-03-15 21:17:48 +0000 | [diff] [blame] | 637 | C = llvm::ConstantExpr::getIntegerCast(C, CGM.IntPtrTy, isSigned); |
| 638 | return llvm::ConstantExpr::getIntToPtr(C, destType); |
| 639 | } |
| 640 | |
| 641 | case CK_IntegralToBoolean: |
| 642 | return llvm::ConstantExpr::getICmp(llvm::CmpInst::ICMP_EQ, C, |
| 643 | llvm::Constant::getNullValue(C->getType())); |
| 644 | |
| 645 | case CK_IntegralToFloating: |
Douglas Gregor | 575a1c9 | 2011-05-20 16:38:50 +0000 | [diff] [blame] | 646 | if (subExpr->getType()->isSignedIntegerOrEnumerationType()) |
John McCall | 8bba1f4 | 2011-03-15 21:17:48 +0000 | [diff] [blame] | 647 | return llvm::ConstantExpr::getSIToFP(C, destType); |
| 648 | else |
| 649 | return llvm::ConstantExpr::getUIToFP(C, destType); |
| 650 | |
| 651 | case CK_FloatingToIntegral: |
Douglas Gregor | 575a1c9 | 2011-05-20 16:38:50 +0000 | [diff] [blame] | 652 | if (E->getType()->isSignedIntegerOrEnumerationType()) |
John McCall | 8bba1f4 | 2011-03-15 21:17:48 +0000 | [diff] [blame] | 653 | return llvm::ConstantExpr::getFPToSI(C, destType); |
| 654 | else |
| 655 | return llvm::ConstantExpr::getFPToUI(C, destType); |
| 656 | |
| 657 | case CK_FloatingToBoolean: |
| 658 | return llvm::ConstantExpr::getFCmp(llvm::CmpInst::FCMP_UNE, C, |
| 659 | llvm::Constant::getNullValue(C->getType())); |
| 660 | |
| 661 | case CK_FloatingCast: |
| 662 | return llvm::ConstantExpr::getFPCast(C, destType); |
Anders Carlsson | 0086ccb | 2009-08-22 23:54:44 +0000 | [diff] [blame] | 663 | } |
Matt Beaumont-Gay | 17d12fe | 2011-03-17 00:46:34 +0000 | [diff] [blame] | 664 | llvm_unreachable("Invalid CastKind"); |
Anders Carlsson | 3b1d57b | 2008-01-26 01:36:00 +0000 | [diff] [blame] | 665 | } |
Devang Patel | eae1560 | 2008-02-05 02:39:50 +0000 | [diff] [blame] | 666 | |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 667 | llvm::Constant *VisitCXXDefaultArgExpr(CXXDefaultArgExpr *DAE) { |
| 668 | return Visit(DAE->getExpr()); |
| 669 | } |
| 670 | |
Douglas Gregor | 03e8003 | 2011-06-21 17:03:29 +0000 | [diff] [blame] | 671 | llvm::Constant *VisitMaterializeTemporaryExpr(MaterializeTemporaryExpr *E) { |
| 672 | return Visit(E->GetTemporaryExpr()); |
| 673 | } |
| 674 | |
Eli Friedman | a823400 | 2008-05-30 10:24:46 +0000 | [diff] [blame] | 675 | llvm::Constant *EmitArrayInitialization(InitListExpr *ILE) { |
Nuno Lopes | a75b71f | 2010-04-18 19:06:43 +0000 | [diff] [blame] | 676 | unsigned NumInitElements = ILE->getNumInits(); |
Benjamin Kramer | 0483a6f | 2011-03-02 21:27:44 +0000 | [diff] [blame] | 677 | if (NumInitElements == 1 && ILE->getType() == ILE->getInit(0)->getType() && |
Nuno Lopes | a75b71f | 2010-04-18 19:06:43 +0000 | [diff] [blame] | 678 | (isa<StringLiteral>(ILE->getInit(0)) || |
| 679 | isa<ObjCEncodeExpr>(ILE->getInit(0)))) |
| 680 | return Visit(ILE->getInit(0)); |
| 681 | |
Nuno Lopes | 6784704 | 2010-04-16 23:19:41 +0000 | [diff] [blame] | 682 | std::vector<llvm::Constant*> Elts; |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 683 | llvm::ArrayType *AType = |
Nuno Lopes | 6784704 | 2010-04-16 23:19:41 +0000 | [diff] [blame] | 684 | cast<llvm::ArrayType>(ConvertType(ILE->getType())); |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 685 | llvm::Type *ElemTy = AType->getElementType(); |
Devang Patel | eae1560 | 2008-02-05 02:39:50 +0000 | [diff] [blame] | 686 | unsigned NumElements = AType->getNumElements(); |
| 687 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 688 | // Initialising an array requires us to automatically |
Devang Patel | eae1560 | 2008-02-05 02:39:50 +0000 | [diff] [blame] | 689 | // initialise any elements that have not been initialised explicitly |
| 690 | unsigned NumInitableElts = std::min(NumInitElements, NumElements); |
| 691 | |
| 692 | // Copy initializer elements. |
| 693 | unsigned i = 0; |
Eli Friedman | a04a153 | 2008-05-30 19:58:50 +0000 | [diff] [blame] | 694 | bool RewriteType = false; |
Devang Patel | eae1560 | 2008-02-05 02:39:50 +0000 | [diff] [blame] | 695 | for (; i < NumInitableElts; ++i) { |
Anders Carlsson | e9352cc | 2009-04-08 04:48:15 +0000 | [diff] [blame] | 696 | Expr *Init = ILE->getInit(i); |
| 697 | llvm::Constant *C = CGM.EmitConstantExpr(Init, Init->getType(), CGF); |
Daniel Dunbar | d60f2fb | 2009-02-17 18:43:32 +0000 | [diff] [blame] | 698 | if (!C) |
| 699 | return 0; |
Eli Friedman | a04a153 | 2008-05-30 19:58:50 +0000 | [diff] [blame] | 700 | RewriteType |= (C->getType() != ElemTy); |
Devang Patel | eae1560 | 2008-02-05 02:39:50 +0000 | [diff] [blame] | 701 | Elts.push_back(C); |
| 702 | } |
Eli Friedman | a04a153 | 2008-05-30 19:58:50 +0000 | [diff] [blame] | 703 | |
Devang Patel | eae1560 | 2008-02-05 02:39:50 +0000 | [diff] [blame] | 704 | // Initialize remaining array elements. |
Eli Friedman | 0f59312 | 2009-04-13 21:47:26 +0000 | [diff] [blame] | 705 | // FIXME: This doesn't handle member pointers correctly! |
Argyrios Kyrtzidis | 4423ac0 | 2011-04-21 00:27:41 +0000 | [diff] [blame] | 706 | llvm::Constant *fillC; |
| 707 | if (Expr *filler = ILE->getArrayFiller()) |
| 708 | fillC = CGM.EmitConstantExpr(filler, filler->getType(), CGF); |
| 709 | else |
| 710 | fillC = llvm::Constant::getNullValue(ElemTy); |
| 711 | if (!fillC) |
| 712 | return 0; |
| 713 | RewriteType |= (fillC->getType() != ElemTy); |
Devang Patel | eae1560 | 2008-02-05 02:39:50 +0000 | [diff] [blame] | 714 | for (; i < NumElements; ++i) |
Argyrios Kyrtzidis | 4423ac0 | 2011-04-21 00:27:41 +0000 | [diff] [blame] | 715 | Elts.push_back(fillC); |
Devang Patel | eae1560 | 2008-02-05 02:39:50 +0000 | [diff] [blame] | 716 | |
Eli Friedman | a04a153 | 2008-05-30 19:58:50 +0000 | [diff] [blame] | 717 | if (RewriteType) { |
| 718 | // FIXME: Try to avoid packing the array |
Jay Foad | ef6de3d | 2011-07-11 09:56:20 +0000 | [diff] [blame] | 719 | std::vector<llvm::Type*> Types; |
Eli Friedman | a04a153 | 2008-05-30 19:58:50 +0000 | [diff] [blame] | 720 | for (unsigned i = 0; i < Elts.size(); ++i) |
| 721 | Types.push_back(Elts[i]->getType()); |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 722 | llvm::StructType *SType = llvm::StructType::get(AType->getContext(), |
Owen Anderson | 47a434f | 2009-08-05 23:18:46 +0000 | [diff] [blame] | 723 | Types, true); |
Owen Anderson | 08e2524 | 2009-07-27 22:29:56 +0000 | [diff] [blame] | 724 | return llvm::ConstantStruct::get(SType, Elts); |
Eli Friedman | a04a153 | 2008-05-30 19:58:50 +0000 | [diff] [blame] | 725 | } |
| 726 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 727 | return llvm::ConstantArray::get(AType, Elts); |
Devang Patel | eae1560 | 2008-02-05 02:39:50 +0000 | [diff] [blame] | 728 | } |
| 729 | |
Eli Friedman | a823400 | 2008-05-30 10:24:46 +0000 | [diff] [blame] | 730 | llvm::Constant *EmitStructInitialization(InitListExpr *ILE) { |
Anders Carlsson | 8dca3b3 | 2009-07-27 05:54:15 +0000 | [diff] [blame] | 731 | return ConstStructBuilder::BuildStruct(CGM, CGF, ILE); |
Devang Patel | eae1560 | 2008-02-05 02:39:50 +0000 | [diff] [blame] | 732 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 733 | |
Eli Friedman | a823400 | 2008-05-30 10:24:46 +0000 | [diff] [blame] | 734 | llvm::Constant *EmitUnionInitialization(InitListExpr *ILE) { |
Anders Carlsson | f6c4396 | 2009-07-31 21:34:04 +0000 | [diff] [blame] | 735 | return ConstStructBuilder::BuildStruct(CGM, CGF, ILE); |
Eli Friedman | a823400 | 2008-05-30 10:24:46 +0000 | [diff] [blame] | 736 | } |
| 737 | |
Anders Carlsson | 7d801ea | 2009-01-30 06:13:25 +0000 | [diff] [blame] | 738 | llvm::Constant *VisitImplicitValueInitExpr(ImplicitValueInitExpr* E) { |
Eli Friedman | 0f59312 | 2009-04-13 21:47:26 +0000 | [diff] [blame] | 739 | return CGM.EmitNullConstant(E->getType()); |
Anders Carlsson | 7d801ea | 2009-01-30 06:13:25 +0000 | [diff] [blame] | 740 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 741 | |
Anders Carlsson | 3b1d57b | 2008-01-26 01:36:00 +0000 | [diff] [blame] | 742 | llvm::Constant *VisitInitListExpr(InitListExpr *ILE) { |
Eli Friedman | 0c706c2 | 2011-09-19 23:17:44 +0000 | [diff] [blame] | 743 | if (ILE->getType()->isAnyComplexType() && ILE->getNumInits() == 2) { |
| 744 | // Complex type with element initializers |
| 745 | Expr *Real = ILE->getInit(0); |
| 746 | Expr *Imag = ILE->getInit(1); |
| 747 | llvm::Constant *Complex[2]; |
| 748 | Complex[0] = CGM.EmitConstantExpr(Real, Real->getType(), CGF); |
| 749 | if (!Complex[0]) |
| 750 | return 0; |
| 751 | Complex[1] = CGM.EmitConstantExpr(Imag, Imag->getType(), CGF); |
| 752 | if (!Complex[1]) |
| 753 | return 0; |
| 754 | llvm::StructType *STy = |
| 755 | cast<llvm::StructType>(ConvertType(ILE->getType())); |
| 756 | return llvm::ConstantStruct::get(STy, Complex); |
| 757 | } |
| 758 | |
Eli Friedman | a823400 | 2008-05-30 10:24:46 +0000 | [diff] [blame] | 759 | if (ILE->getType()->isScalarType()) { |
| 760 | // We have a scalar in braces. Just use the first element. |
Anders Carlsson | e9352cc | 2009-04-08 04:48:15 +0000 | [diff] [blame] | 761 | if (ILE->getNumInits() > 0) { |
| 762 | Expr *Init = ILE->getInit(0); |
| 763 | return CGM.EmitConstantExpr(Init, Init->getType(), CGF); |
| 764 | } |
Eli Friedman | 0f59312 | 2009-04-13 21:47:26 +0000 | [diff] [blame] | 765 | return CGM.EmitNullConstant(ILE->getType()); |
Anders Carlsson | 3b1d57b | 2008-01-26 01:36:00 +0000 | [diff] [blame] | 766 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 767 | |
Eli Friedman | a823400 | 2008-05-30 10:24:46 +0000 | [diff] [blame] | 768 | if (ILE->getType()->isArrayType()) |
| 769 | return EmitArrayInitialization(ILE); |
Devang Patel | 47fb697 | 2008-01-29 23:23:18 +0000 | [diff] [blame] | 770 | |
Anders Carlsson | 01a79ac | 2009-11-21 23:56:04 +0000 | [diff] [blame] | 771 | if (ILE->getType()->isRecordType()) |
Eli Friedman | a823400 | 2008-05-30 10:24:46 +0000 | [diff] [blame] | 772 | return EmitStructInitialization(ILE); |
| 773 | |
| 774 | if (ILE->getType()->isUnionType()) |
| 775 | return EmitUnionInitialization(ILE); |
| 776 | |
Eli Friedman | 8ac5549 | 2010-01-02 23:43:59 +0000 | [diff] [blame] | 777 | // If ILE was a constant vector, we would have handled it already. |
Eli Friedman | a823400 | 2008-05-30 10:24:46 +0000 | [diff] [blame] | 778 | if (ILE->getType()->isVectorType()) |
Eli Friedman | 8ac5549 | 2010-01-02 23:43:59 +0000 | [diff] [blame] | 779 | return 0; |
Eli Friedman | a823400 | 2008-05-30 10:24:46 +0000 | [diff] [blame] | 780 | |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 781 | llvm_unreachable("Unable to handle InitListExpr"); |
Anders Carlsson | 3b1d57b | 2008-01-26 01:36:00 +0000 | [diff] [blame] | 782 | } |
Eli Friedman | 3b707e7 | 2008-02-21 17:57:49 +0000 | [diff] [blame] | 783 | |
John McCall | fb8b69a | 2010-02-02 08:02:49 +0000 | [diff] [blame] | 784 | llvm::Constant *VisitCXXConstructExpr(CXXConstructExpr *E) { |
| 785 | if (!E->getConstructor()->isTrivial()) |
| 786 | return 0; |
| 787 | |
Anders Carlsson | 2ca4f63 | 2010-02-05 18:38:45 +0000 | [diff] [blame] | 788 | QualType Ty = E->getType(); |
| 789 | |
| 790 | // FIXME: We should not have to call getBaseElementType here. |
| 791 | const RecordType *RT = |
| 792 | CGM.getContext().getBaseElementType(Ty)->getAs<RecordType>(); |
| 793 | const CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl()); |
| 794 | |
| 795 | // If the class doesn't have a trivial destructor, we can't emit it as a |
| 796 | // constant expr. |
| 797 | if (!RD->hasTrivialDestructor()) |
| 798 | return 0; |
| 799 | |
John McCall | fb8b69a | 2010-02-02 08:02:49 +0000 | [diff] [blame] | 800 | // Only copy and default constructors can be trivial. |
| 801 | |
John McCall | fb8b69a | 2010-02-02 08:02:49 +0000 | [diff] [blame] | 802 | |
| 803 | if (E->getNumArgs()) { |
| 804 | assert(E->getNumArgs() == 1 && "trivial ctor with > 1 argument"); |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 805 | assert(E->getConstructor()->isCopyOrMoveConstructor() && |
| 806 | "trivial ctor has argument but isn't a copy/move ctor"); |
John McCall | fb8b69a | 2010-02-02 08:02:49 +0000 | [diff] [blame] | 807 | |
| 808 | Expr *Arg = E->getArg(0); |
| 809 | assert(CGM.getContext().hasSameUnqualifiedType(Ty, Arg->getType()) && |
| 810 | "argument to copy ctor is of wrong type"); |
| 811 | |
Chandler Carruth | 878b55c | 2010-02-02 12:15:55 +0000 | [diff] [blame] | 812 | return Visit(Arg); |
John McCall | fb8b69a | 2010-02-02 08:02:49 +0000 | [diff] [blame] | 813 | } |
| 814 | |
| 815 | return CGM.EmitNullConstant(Ty); |
| 816 | } |
| 817 | |
Anders Carlsson | 3b1d57b | 2008-01-26 01:36:00 +0000 | [diff] [blame] | 818 | llvm::Constant *VisitStringLiteral(StringLiteral *E) { |
Eli Friedman | 64f45a2 | 2011-11-01 02:23:42 +0000 | [diff] [blame] | 819 | return CGM.GetConstantArrayFromStringLiteral(E); |
Anders Carlsson | 3b1d57b | 2008-01-26 01:36:00 +0000 | [diff] [blame] | 820 | } |
| 821 | |
Chris Lattner | eaf2bb8 | 2009-02-24 22:18:39 +0000 | [diff] [blame] | 822 | llvm::Constant *VisitObjCEncodeExpr(ObjCEncodeExpr *E) { |
| 823 | // This must be an @encode initializing an array in a static initializer. |
| 824 | // Don't emit it as the address of the string, emit the string data itself |
| 825 | // as an inline array. |
| 826 | std::string Str; |
| 827 | CGM.getContext().getObjCEncodingForType(E->getEncodedType(), Str); |
| 828 | const ConstantArrayType *CAT = cast<ConstantArrayType>(E->getType()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 829 | |
Chris Lattner | eaf2bb8 | 2009-02-24 22:18:39 +0000 | [diff] [blame] | 830 | // Resize the string to the right size, adding zeros at the end, or |
| 831 | // truncating as needed. |
| 832 | Str.resize(CAT->getSize().getZExtValue(), '\0'); |
Owen Anderson | 0032b27 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 833 | return llvm::ConstantArray::get(VMContext, Str, false); |
Chris Lattner | eaf2bb8 | 2009-02-24 22:18:39 +0000 | [diff] [blame] | 834 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 835 | |
Eli Friedman | bc5ed6e | 2008-05-29 11:22:45 +0000 | [diff] [blame] | 836 | llvm::Constant *VisitUnaryExtension(const UnaryOperator *E) { |
| 837 | return Visit(E->getSubExpr()); |
| 838 | } |
Mike Stump | b83d287 | 2009-02-19 22:01:56 +0000 | [diff] [blame] | 839 | |
Anders Carlsson | 3b1d57b | 2008-01-26 01:36:00 +0000 | [diff] [blame] | 840 | // Utility methods |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 841 | llvm::Type *ConvertType(QualType T) { |
Anders Carlsson | 3b1d57b | 2008-01-26 01:36:00 +0000 | [diff] [blame] | 842 | return CGM.getTypes().ConvertType(T); |
| 843 | } |
Anders Carlsson | 84005b4 | 2008-01-26 04:30:23 +0000 | [diff] [blame] | 844 | |
Anders Carlsson | 5ea7d07 | 2008-11-16 06:23:45 +0000 | [diff] [blame] | 845 | public: |
Anders Carlsson | 84005b4 | 2008-01-26 04:30:23 +0000 | [diff] [blame] | 846 | llvm::Constant *EmitLValue(Expr *E) { |
Anders Carlsson | e3d3b7d | 2008-01-26 02:08:50 +0000 | [diff] [blame] | 847 | switch (E->getStmtClass()) { |
Eli Friedman | 546d94c | 2008-02-11 00:23:10 +0000 | [diff] [blame] | 848 | default: break; |
Anders Carlsson | e3d3b7d | 2008-01-26 02:08:50 +0000 | [diff] [blame] | 849 | case Expr::CompoundLiteralExprClass: { |
| 850 | // Note that due to the nature of compound literals, this is guaranteed |
| 851 | // 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] | 852 | CompoundLiteralExpr *CLE = cast<CompoundLiteralExpr>(E); |
| 853 | llvm::Constant* C = Visit(CLE->getInitializer()); |
Daniel Dunbar | d60f2fb | 2009-02-17 18:43:32 +0000 | [diff] [blame] | 854 | // FIXME: "Leaked" on failure. |
| 855 | if (C) |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 856 | C = new llvm::GlobalVariable(CGM.getModule(), C->getType(), |
Eli Friedman | d15d8c6 | 2009-08-26 20:01:39 +0000 | [diff] [blame] | 857 | E->getType().isConstant(CGM.getContext()), |
Daniel Dunbar | d60f2fb | 2009-02-17 18:43:32 +0000 | [diff] [blame] | 858 | llvm::GlobalValue::InternalLinkage, |
Eli Friedman | d15d8c6 | 2009-08-26 20:01:39 +0000 | [diff] [blame] | 859 | C, ".compoundliteral", 0, false, |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 860 | CGM.getContext().getTargetAddressSpace(E->getType())); |
Anders Carlsson | e3d3b7d | 2008-01-26 02:08:50 +0000 | [diff] [blame] | 861 | return C; |
Anders Carlsson | 84005b4 | 2008-01-26 04:30:23 +0000 | [diff] [blame] | 862 | } |
Douglas Gregor | a2813ce | 2009-10-23 18:54:35 +0000 | [diff] [blame] | 863 | case Expr::DeclRefExprClass: { |
Rafael Espindola | da18261 | 2010-03-04 21:26:03 +0000 | [diff] [blame] | 864 | ValueDecl *Decl = cast<DeclRefExpr>(E)->getDecl(); |
| 865 | if (Decl->hasAttr<WeakRefAttr>()) |
Eli Friedman | a7e6845 | 2010-08-22 01:00:03 +0000 | [diff] [blame] | 866 | return CGM.GetWeakRefReference(Decl); |
Anders Carlsson | e3d3b7d | 2008-01-26 02:08:50 +0000 | [diff] [blame] | 867 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(Decl)) |
Anders Carlsson | 555b4bb | 2009-09-10 23:43:36 +0000 | [diff] [blame] | 868 | return CGM.GetAddrOfFunction(FD); |
Steve Naroff | 248a753 | 2008-04-15 22:42:06 +0000 | [diff] [blame] | 869 | if (const VarDecl* VD = dyn_cast<VarDecl>(Decl)) { |
Daniel Dunbar | d01b669 | 2009-02-24 18:41:57 +0000 | [diff] [blame] | 870 | // We can never refer to a variable with local storage. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 871 | if (!VD->hasLocalStorage()) { |
Daniel Dunbar | d01b669 | 2009-02-24 18:41:57 +0000 | [diff] [blame] | 872 | if (VD->isFileVarDecl() || VD->hasExternalStorage()) |
| 873 | return CGM.GetAddrOfGlobalVar(VD); |
John McCall | b6bbcc9 | 2010-10-15 04:57:14 +0000 | [diff] [blame] | 874 | else if (VD->isLocalVarDecl()) { |
Daniel Dunbar | d01b669 | 2009-02-24 18:41:57 +0000 | [diff] [blame] | 875 | assert(CGF && "Can't access static local vars without CGF"); |
| 876 | return CGF->GetAddrOfStaticLocalVar(VD); |
| 877 | } |
Steve Naroff | 248a753 | 2008-04-15 22:42:06 +0000 | [diff] [blame] | 878 | } |
Lauro Ramos Venancio | 8137335 | 2008-02-26 21:41:45 +0000 | [diff] [blame] | 879 | } |
Eli Friedman | 546d94c | 2008-02-11 00:23:10 +0000 | [diff] [blame] | 880 | break; |
Anders Carlsson | e3d3b7d | 2008-01-26 02:08:50 +0000 | [diff] [blame] | 881 | } |
Daniel Dunbar | 6143293 | 2008-08-13 23:20:05 +0000 | [diff] [blame] | 882 | case Expr::StringLiteralClass: |
| 883 | return CGM.GetAddrOfConstantStringFromLiteral(cast<StringLiteral>(E)); |
Chris Lattner | eaf2bb8 | 2009-02-24 22:18:39 +0000 | [diff] [blame] | 884 | case Expr::ObjCEncodeExprClass: |
| 885 | return CGM.GetAddrOfConstantStringFromObjCEncode(cast<ObjCEncodeExpr>(E)); |
Anders Carlsson | 5ea7d07 | 2008-11-16 06:23:45 +0000 | [diff] [blame] | 886 | case Expr::ObjCStringLiteralClass: { |
| 887 | ObjCStringLiteral* SL = cast<ObjCStringLiteral>(E); |
David Chisnall | 0d13f6f | 2010-01-23 02:40:42 +0000 | [diff] [blame] | 888 | llvm::Constant *C = |
| 889 | CGM.getObjCRuntime().GenerateConstantString(SL->getString()); |
Owen Anderson | 3c4972d | 2009-07-29 18:54:39 +0000 | [diff] [blame] | 890 | return llvm::ConstantExpr::getBitCast(C, ConvertType(E->getType())); |
Anders Carlsson | 5ea7d07 | 2008-11-16 06:23:45 +0000 | [diff] [blame] | 891 | } |
Chris Lattner | 3534564 | 2008-12-12 05:18:02 +0000 | [diff] [blame] | 892 | case Expr::PredefinedExprClass: { |
Eli Friedman | f70b24e | 2009-11-14 08:37:13 +0000 | [diff] [blame] | 893 | unsigned Type = cast<PredefinedExpr>(E)->getIdentType(); |
| 894 | if (CGF) { |
Daniel Dunbar | 3ec0baf | 2010-08-21 03:01:12 +0000 | [diff] [blame] | 895 | LValue Res = CGF->EmitPredefinedLValue(cast<PredefinedExpr>(E)); |
Eli Friedman | f70b24e | 2009-11-14 08:37:13 +0000 | [diff] [blame] | 896 | return cast<llvm::Constant>(Res.getAddress()); |
| 897 | } else if (Type == PredefinedExpr::PrettyFunction) { |
| 898 | return CGM.GetAddrOfConstantCString("top level", ".tmp"); |
| 899 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 900 | |
Eli Friedman | f70b24e | 2009-11-14 08:37:13 +0000 | [diff] [blame] | 901 | return CGM.GetAddrOfConstantCString("", ".tmp"); |
Chris Lattner | 3534564 | 2008-12-12 05:18:02 +0000 | [diff] [blame] | 902 | } |
Eli Friedman | f011589 | 2009-01-25 01:21:06 +0000 | [diff] [blame] | 903 | case Expr::AddrLabelExprClass: { |
| 904 | assert(CGF && "Invalid address of label expression outside function."); |
Chris Lattner | d9becd1 | 2009-10-28 23:59:40 +0000 | [diff] [blame] | 905 | llvm::Constant *Ptr = |
| 906 | CGF->GetAddrOfLabel(cast<AddrLabelExpr>(E)->getLabel()); |
| 907 | return llvm::ConstantExpr::getBitCast(Ptr, ConvertType(E->getType())); |
Eli Friedman | f011589 | 2009-01-25 01:21:06 +0000 | [diff] [blame] | 908 | } |
Eli Friedman | 3941b18 | 2009-01-25 01:54:01 +0000 | [diff] [blame] | 909 | case Expr::CallExprClass: { |
| 910 | CallExpr* CE = cast<CallExpr>(E); |
David Chisnall | 0d13f6f | 2010-01-23 02:40:42 +0000 | [diff] [blame] | 911 | unsigned builtin = CE->isBuiltinCall(CGM.getContext()); |
| 912 | if (builtin != |
| 913 | Builtin::BI__builtin___CFStringMakeConstantString && |
| 914 | builtin != |
| 915 | Builtin::BI__builtin___NSStringMakeConstantString) |
Eli Friedman | 3941b18 | 2009-01-25 01:54:01 +0000 | [diff] [blame] | 916 | break; |
| 917 | const Expr *Arg = CE->getArg(0)->IgnoreParenCasts(); |
| 918 | const StringLiteral *Literal = cast<StringLiteral>(Arg); |
David Chisnall | 0d13f6f | 2010-01-23 02:40:42 +0000 | [diff] [blame] | 919 | if (builtin == |
| 920 | Builtin::BI__builtin___NSStringMakeConstantString) { |
| 921 | return CGM.getObjCRuntime().GenerateConstantString(Literal); |
| 922 | } |
Steve Naroff | 33fdb73 | 2009-03-31 16:53:37 +0000 | [diff] [blame] | 923 | // FIXME: need to deal with UCN conversion issues. |
Steve Naroff | 8d4141f | 2009-04-01 13:55:36 +0000 | [diff] [blame] | 924 | return CGM.GetAddrOfConstantCFString(Literal); |
Eli Friedman | 3941b18 | 2009-01-25 01:54:01 +0000 | [diff] [blame] | 925 | } |
Mike Stump | bd65cac | 2009-02-19 01:01:04 +0000 | [diff] [blame] | 926 | case Expr::BlockExprClass: { |
Anders Carlsson | 4de9fce | 2009-03-01 01:09:12 +0000 | [diff] [blame] | 927 | std::string FunctionName; |
| 928 | if (CGF) |
| 929 | FunctionName = CGF->CurFn->getName(); |
| 930 | else |
| 931 | FunctionName = "global"; |
| 932 | |
| 933 | return CGM.GetAddrOfGlobalBlock(cast<BlockExpr>(E), FunctionName.c_str()); |
Mike Stump | bd65cac | 2009-02-19 01:01:04 +0000 | [diff] [blame] | 934 | } |
Eli Friedman | 546d94c | 2008-02-11 00:23:10 +0000 | [diff] [blame] | 935 | } |
Daniel Dunbar | d60f2fb | 2009-02-17 18:43:32 +0000 | [diff] [blame] | 936 | |
| 937 | return 0; |
Anders Carlsson | e3d3b7d | 2008-01-26 02:08:50 +0000 | [diff] [blame] | 938 | } |
Anders Carlsson | e3d3b7d | 2008-01-26 02:08:50 +0000 | [diff] [blame] | 939 | }; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 940 | |
Anders Carlsson | 3b1d57b | 2008-01-26 01:36:00 +0000 | [diff] [blame] | 941 | } // end anonymous namespace. |
| 942 | |
Lauro Ramos Venancio | 8137335 | 2008-02-26 21:41:45 +0000 | [diff] [blame] | 943 | llvm::Constant *CodeGenModule::EmitConstantExpr(const Expr *E, |
Anders Carlsson | e9352cc | 2009-04-08 04:48:15 +0000 | [diff] [blame] | 944 | QualType DestType, |
Chris Lattner | 9619662 | 2008-07-26 22:37:01 +0000 | [diff] [blame] | 945 | CodeGenFunction *CGF) { |
Anders Carlsson | 4b3f9c0 | 2008-12-01 02:42:14 +0000 | [diff] [blame] | 946 | Expr::EvalResult Result; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 947 | |
Anders Carlsson | 1e5dc6e | 2009-04-11 01:08:03 +0000 | [diff] [blame] | 948 | bool Success = false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 949 | |
Eli Friedman | 50c39ea | 2009-05-27 06:04:58 +0000 | [diff] [blame] | 950 | if (DestType->isReferenceType()) |
| 951 | Success = E->EvaluateAsLValue(Result, Context); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 952 | else |
Richard Smith | 51f4708 | 2011-10-29 00:50:52 +0000 | [diff] [blame] | 953 | Success = E->EvaluateAsRValue(Result, Context); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 954 | |
Eli Friedman | 55f9bdd | 2009-11-14 08:51:33 +0000 | [diff] [blame] | 955 | if (Success && !Result.HasSideEffects) { |
Anders Carlsson | 4b3f9c0 | 2008-12-01 02:42:14 +0000 | [diff] [blame] | 956 | switch (Result.Val.getKind()) { |
Daniel Dunbar | a5fd07b | 2009-01-28 22:24:07 +0000 | [diff] [blame] | 957 | case APValue::Uninitialized: |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 958 | llvm_unreachable("Constant expressions should be initialized."); |
Anders Carlsson | 5a9a457 | 2008-11-15 20:45:50 +0000 | [diff] [blame] | 959 | case APValue::LValue: { |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 960 | llvm::Type *DestTy = getTypes().ConvertTypeForMem(DestType); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 961 | llvm::Constant *Offset = |
| 962 | llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext), |
Ken Dyck | a730583 | 2010-01-15 12:37:54 +0000 | [diff] [blame] | 963 | Result.Val.getLValueOffset().getQuantity()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 964 | |
Daniel Dunbar | e20de51 | 2009-02-19 21:44:24 +0000 | [diff] [blame] | 965 | llvm::Constant *C; |
Anders Carlsson | 4b3f9c0 | 2008-12-01 02:42:14 +0000 | [diff] [blame] | 966 | if (const Expr *LVBase = Result.Val.getLValueBase()) { |
Daniel Dunbar | e20de51 | 2009-02-19 21:44:24 +0000 | [diff] [blame] | 967 | C = ConstExprEmitter(*this, CGF).EmitLValue(const_cast<Expr*>(LVBase)); |
Anders Carlsson | 5ea7d07 | 2008-11-16 06:23:45 +0000 | [diff] [blame] | 968 | |
Daniel Dunbar | e20de51 | 2009-02-19 21:44:24 +0000 | [diff] [blame] | 969 | // Apply offset if necessary. |
| 970 | if (!Offset->isNullValue()) { |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 971 | llvm::Type *Type = llvm::Type::getInt8PtrTy(VMContext); |
Owen Anderson | 3c4972d | 2009-07-29 18:54:39 +0000 | [diff] [blame] | 972 | llvm::Constant *Casted = llvm::ConstantExpr::getBitCast(C, Type); |
Jay Foad | a5c0434 | 2011-07-21 14:31:17 +0000 | [diff] [blame] | 973 | Casted = llvm::ConstantExpr::getGetElementPtr(Casted, Offset); |
Owen Anderson | 3c4972d | 2009-07-29 18:54:39 +0000 | [diff] [blame] | 974 | C = llvm::ConstantExpr::getBitCast(Casted, C->getType()); |
Daniel Dunbar | e20de51 | 2009-02-19 21:44:24 +0000 | [diff] [blame] | 975 | } |
| 976 | |
| 977 | // Convert to the appropriate type; this could be an lvalue for |
| 978 | // an integer. |
Anders Carlsson | 12e330c | 2009-04-10 04:59:59 +0000 | [diff] [blame] | 979 | if (isa<llvm::PointerType>(DestTy)) |
Owen Anderson | 3c4972d | 2009-07-29 18:54:39 +0000 | [diff] [blame] | 980 | return llvm::ConstantExpr::getBitCast(C, DestTy); |
Daniel Dunbar | e20de51 | 2009-02-19 21:44:24 +0000 | [diff] [blame] | 981 | |
Owen Anderson | 3c4972d | 2009-07-29 18:54:39 +0000 | [diff] [blame] | 982 | return llvm::ConstantExpr::getPtrToInt(C, DestTy); |
Daniel Dunbar | e20de51 | 2009-02-19 21:44:24 +0000 | [diff] [blame] | 983 | } else { |
| 984 | C = Offset; |
| 985 | |
| 986 | // Convert to the appropriate type; this could be an lvalue for |
| 987 | // an integer. |
Anders Carlsson | 12e330c | 2009-04-10 04:59:59 +0000 | [diff] [blame] | 988 | if (isa<llvm::PointerType>(DestTy)) |
Owen Anderson | 3c4972d | 2009-07-29 18:54:39 +0000 | [diff] [blame] | 989 | return llvm::ConstantExpr::getIntToPtr(C, DestTy); |
Daniel Dunbar | e20de51 | 2009-02-19 21:44:24 +0000 | [diff] [blame] | 990 | |
| 991 | // If the types don't match this should only be a truncate. |
Anders Carlsson | 12e330c | 2009-04-10 04:59:59 +0000 | [diff] [blame] | 992 | if (C->getType() != DestTy) |
Owen Anderson | 3c4972d | 2009-07-29 18:54:39 +0000 | [diff] [blame] | 993 | return llvm::ConstantExpr::getTrunc(C, DestTy); |
Daniel Dunbar | e20de51 | 2009-02-19 21:44:24 +0000 | [diff] [blame] | 994 | |
| 995 | return C; |
Anders Carlsson | 5ea7d07 | 2008-11-16 06:23:45 +0000 | [diff] [blame] | 996 | } |
Anders Carlsson | 5a9a457 | 2008-11-15 20:45:50 +0000 | [diff] [blame] | 997 | } |
Eli Friedman | 7b30b1c | 2008-11-17 03:57:28 +0000 | [diff] [blame] | 998 | case APValue::Int: { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 999 | llvm::Constant *C = llvm::ConstantInt::get(VMContext, |
Owen Anderson | 4a28d5d | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 1000 | Result.Val.getInt()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1001 | |
Chris Lattner | 7f215c1 | 2010-06-26 21:52:32 +0000 | [diff] [blame] | 1002 | if (C->getType()->isIntegerTy(1)) { |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 1003 | llvm::Type *BoolTy = getTypes().ConvertTypeForMem(E->getType()); |
Owen Anderson | 3c4972d | 2009-07-29 18:54:39 +0000 | [diff] [blame] | 1004 | C = llvm::ConstantExpr::getZExt(C, BoolTy); |
Anders Carlsson | 5a9a457 | 2008-11-15 20:45:50 +0000 | [diff] [blame] | 1005 | } |
| 1006 | return C; |
Eli Friedman | 7b30b1c | 2008-11-17 03:57:28 +0000 | [diff] [blame] | 1007 | } |
Daniel Dunbar | a5fd07b | 2009-01-28 22:24:07 +0000 | [diff] [blame] | 1008 | case APValue::ComplexInt: { |
| 1009 | llvm::Constant *Complex[2]; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1010 | |
Owen Anderson | 4a28d5d | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 1011 | Complex[0] = llvm::ConstantInt::get(VMContext, |
| 1012 | Result.Val.getComplexIntReal()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1013 | Complex[1] = llvm::ConstantInt::get(VMContext, |
Owen Anderson | 4a28d5d | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 1014 | Result.Val.getComplexIntImag()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1015 | |
Nick Lewycky | 0d36dd2 | 2009-09-19 20:00:52 +0000 | [diff] [blame] | 1016 | // FIXME: the target may want to specify that this is packed. |
Chris Lattner | c5cbb90 | 2011-06-20 04:01:35 +0000 | [diff] [blame] | 1017 | llvm::StructType *STy = llvm::StructType::get(Complex[0]->getType(), |
| 1018 | Complex[1]->getType(), |
| 1019 | NULL); |
| 1020 | return llvm::ConstantStruct::get(STy, Complex); |
Daniel Dunbar | a5fd07b | 2009-01-28 22:24:07 +0000 | [diff] [blame] | 1021 | } |
Anton Korobeynikov | aa4a99b | 2011-10-14 23:23:15 +0000 | [diff] [blame] | 1022 | case APValue::Float: { |
| 1023 | const llvm::APFloat &Init = Result.Val.getFloat(); |
| 1024 | if (&Init.getSemantics() == &llvm::APFloat::IEEEhalf) |
| 1025 | return llvm::ConstantInt::get(VMContext, Init.bitcastToAPInt()); |
| 1026 | else |
| 1027 | return llvm::ConstantFP::get(VMContext, Init); |
| 1028 | } |
Anders Carlsson | aa43256 | 2008-11-17 01:58:55 +0000 | [diff] [blame] | 1029 | case APValue::ComplexFloat: { |
| 1030 | llvm::Constant *Complex[2]; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1031 | |
| 1032 | Complex[0] = llvm::ConstantFP::get(VMContext, |
Owen Anderson | bc0a222 | 2009-07-27 21:00:51 +0000 | [diff] [blame] | 1033 | Result.Val.getComplexFloatReal()); |
| 1034 | Complex[1] = llvm::ConstantFP::get(VMContext, |
| 1035 | Result.Val.getComplexFloatImag()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1036 | |
Nick Lewycky | 0d36dd2 | 2009-09-19 20:00:52 +0000 | [diff] [blame] | 1037 | // FIXME: the target may want to specify that this is packed. |
Chris Lattner | c5cbb90 | 2011-06-20 04:01:35 +0000 | [diff] [blame] | 1038 | llvm::StructType *STy = llvm::StructType::get(Complex[0]->getType(), |
| 1039 | Complex[1]->getType(), |
| 1040 | NULL); |
| 1041 | return llvm::ConstantStruct::get(STy, Complex); |
Anders Carlsson | aa43256 | 2008-11-17 01:58:55 +0000 | [diff] [blame] | 1042 | } |
Nate Begeman | 3d309f9 | 2009-01-18 01:01:34 +0000 | [diff] [blame] | 1043 | case APValue::Vector: { |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1044 | SmallVector<llvm::Constant *, 4> Inits; |
Nate Begeman | 3d309f9 | 2009-01-18 01:01:34 +0000 | [diff] [blame] | 1045 | unsigned NumElts = Result.Val.getVectorLength(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1046 | |
Anton Yartsev | d06fea8 | 2011-03-27 09:32:40 +0000 | [diff] [blame] | 1047 | if (Context.getLangOptions().AltiVec && |
| 1048 | isa<CastExpr>(E) && |
| 1049 | cast<CastExpr>(E)->getCastKind() == CK_VectorSplat) { |
| 1050 | // AltiVec vector initialization with a single literal |
| 1051 | APValue &Elt = Result.Val.getVectorElt(0); |
| 1052 | |
| 1053 | llvm::Constant* InitValue = Elt.isInt() |
| 1054 | ? cast<llvm::Constant> |
| 1055 | (llvm::ConstantInt::get(VMContext, Elt.getInt())) |
| 1056 | : cast<llvm::Constant> |
| 1057 | (llvm::ConstantFP::get(VMContext, Elt.getFloat())); |
| 1058 | |
| 1059 | for (unsigned i = 0; i != NumElts; ++i) |
| 1060 | Inits.push_back(InitValue); |
| 1061 | |
| 1062 | } else { |
| 1063 | for (unsigned i = 0; i != NumElts; ++i) { |
| 1064 | APValue &Elt = Result.Val.getVectorElt(i); |
| 1065 | if (Elt.isInt()) |
| 1066 | Inits.push_back(llvm::ConstantInt::get(VMContext, Elt.getInt())); |
| 1067 | else |
| 1068 | Inits.push_back(llvm::ConstantFP::get(VMContext, Elt.getFloat())); |
| 1069 | } |
Nate Begeman | 3d309f9 | 2009-01-18 01:01:34 +0000 | [diff] [blame] | 1070 | } |
Chris Lattner | fb018d1 | 2011-02-15 00:14:06 +0000 | [diff] [blame] | 1071 | return llvm::ConstantVector::get(Inits); |
Nate Begeman | 3d309f9 | 2009-01-18 01:01:34 +0000 | [diff] [blame] | 1072 | } |
Anders Carlsson | 5a9a457 | 2008-11-15 20:45:50 +0000 | [diff] [blame] | 1073 | } |
| 1074 | } |
Eli Friedman | 7dfa639 | 2008-06-01 15:31:44 +0000 | [diff] [blame] | 1075 | |
| 1076 | llvm::Constant* C = ConstExprEmitter(*this, CGF).Visit(const_cast<Expr*>(E)); |
Chris Lattner | 7f215c1 | 2010-06-26 21:52:32 +0000 | [diff] [blame] | 1077 | if (C && C->getType()->isIntegerTy(1)) { |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 1078 | llvm::Type *BoolTy = getTypes().ConvertTypeForMem(E->getType()); |
Owen Anderson | 3c4972d | 2009-07-29 18:54:39 +0000 | [diff] [blame] | 1079 | C = llvm::ConstantExpr::getZExt(C, BoolTy); |
Eli Friedman | 7dfa639 | 2008-06-01 15:31:44 +0000 | [diff] [blame] | 1080 | } |
| 1081 | return C; |
Anders Carlsson | 3b1d57b | 2008-01-26 01:36:00 +0000 | [diff] [blame] | 1082 | } |
Eli Friedman | 0f59312 | 2009-04-13 21:47:26 +0000 | [diff] [blame] | 1083 | |
John McCall | 5808ce4 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 1084 | static uint64_t getFieldOffset(ASTContext &C, const FieldDecl *field) { |
| 1085 | const ASTRecordLayout &layout = C.getASTRecordLayout(field->getParent()); |
| 1086 | return layout.getFieldOffset(field->getFieldIndex()); |
| 1087 | } |
| 1088 | |
| 1089 | llvm::Constant * |
| 1090 | CodeGenModule::getMemberPointerConstant(const UnaryOperator *uo) { |
| 1091 | // Member pointer constants always have a very particular form. |
| 1092 | const MemberPointerType *type = cast<MemberPointerType>(uo->getType()); |
| 1093 | const ValueDecl *decl = cast<DeclRefExpr>(uo->getSubExpr())->getDecl(); |
| 1094 | |
| 1095 | // A member function pointer. |
| 1096 | if (const CXXMethodDecl *method = dyn_cast<CXXMethodDecl>(decl)) |
| 1097 | return getCXXABI().EmitMemberPointer(method); |
| 1098 | |
| 1099 | // Otherwise, a member data pointer. |
| 1100 | uint64_t fieldOffset; |
| 1101 | if (const FieldDecl *field = dyn_cast<FieldDecl>(decl)) |
| 1102 | fieldOffset = getFieldOffset(getContext(), field); |
| 1103 | else { |
| 1104 | const IndirectFieldDecl *ifield = cast<IndirectFieldDecl>(decl); |
| 1105 | |
| 1106 | fieldOffset = 0; |
| 1107 | for (IndirectFieldDecl::chain_iterator ci = ifield->chain_begin(), |
| 1108 | ce = ifield->chain_end(); ci != ce; ++ci) |
| 1109 | fieldOffset += getFieldOffset(getContext(), cast<FieldDecl>(*ci)); |
| 1110 | } |
| 1111 | |
| 1112 | CharUnits chars = getContext().toCharUnitsFromBits((int64_t) fieldOffset); |
| 1113 | return getCXXABI().EmitMemberDataPointer(type, chars); |
| 1114 | } |
| 1115 | |
Anders Carlsson | a83fb4b | 2010-05-18 16:51:41 +0000 | [diff] [blame] | 1116 | static void |
| 1117 | FillInNullDataMemberPointers(CodeGenModule &CGM, QualType T, |
| 1118 | std::vector<llvm::Constant *> &Elements, |
| 1119 | uint64_t StartOffset) { |
Ken Dyck | c4d68db | 2011-03-22 00:24:31 +0000 | [diff] [blame] | 1120 | assert(StartOffset % CGM.getContext().getCharWidth() == 0 && |
| 1121 | "StartOffset not byte aligned!"); |
Anders Carlsson | a83fb4b | 2010-05-18 16:51:41 +0000 | [diff] [blame] | 1122 | |
John McCall | f16aa10 | 2010-08-22 21:01:12 +0000 | [diff] [blame] | 1123 | if (CGM.getTypes().isZeroInitializable(T)) |
Anders Carlsson | a83fb4b | 2010-05-18 16:51:41 +0000 | [diff] [blame] | 1124 | return; |
| 1125 | |
| 1126 | if (const ConstantArrayType *CAT = |
| 1127 | CGM.getContext().getAsConstantArrayType(T)) { |
| 1128 | QualType ElementTy = CAT->getElementType(); |
| 1129 | uint64_t ElementSize = CGM.getContext().getTypeSize(ElementTy); |
| 1130 | |
| 1131 | for (uint64_t I = 0, E = CAT->getSize().getZExtValue(); I != E; ++I) { |
| 1132 | FillInNullDataMemberPointers(CGM, ElementTy, Elements, |
| 1133 | StartOffset + I * ElementSize); |
| 1134 | } |
| 1135 | } else if (const RecordType *RT = T->getAs<RecordType>()) { |
| 1136 | const CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl()); |
| 1137 | const ASTRecordLayout &Layout = CGM.getContext().getASTRecordLayout(RD); |
| 1138 | |
| 1139 | // Go through all bases and fill in any null pointer to data members. |
| 1140 | for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(), |
| 1141 | E = RD->bases_end(); I != E; ++I) { |
Anders Carlsson | 0a87b37 | 2010-05-27 18:51:01 +0000 | [diff] [blame] | 1142 | if (I->isVirtual()) { |
Anders Carlsson | ada4b32 | 2010-11-22 18:42:14 +0000 | [diff] [blame] | 1143 | // Ignore virtual bases. |
Anders Carlsson | 0a87b37 | 2010-05-27 18:51:01 +0000 | [diff] [blame] | 1144 | continue; |
| 1145 | } |
Anders Carlsson | a83fb4b | 2010-05-18 16:51:41 +0000 | [diff] [blame] | 1146 | |
| 1147 | const CXXRecordDecl *BaseDecl = |
| 1148 | cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl()); |
| 1149 | |
| 1150 | // Ignore empty bases. |
| 1151 | if (BaseDecl->isEmpty()) |
| 1152 | continue; |
| 1153 | |
| 1154 | // Ignore bases that don't have any pointer to data members. |
John McCall | f16aa10 | 2010-08-22 21:01:12 +0000 | [diff] [blame] | 1155 | if (CGM.getTypes().isZeroInitializable(BaseDecl)) |
Anders Carlsson | a83fb4b | 2010-05-18 16:51:41 +0000 | [diff] [blame] | 1156 | continue; |
| 1157 | |
Anders Carlsson | a14f597 | 2010-10-31 23:22:37 +0000 | [diff] [blame] | 1158 | uint64_t BaseOffset = Layout.getBaseClassOffsetInBits(BaseDecl); |
Anders Carlsson | a83fb4b | 2010-05-18 16:51:41 +0000 | [diff] [blame] | 1159 | FillInNullDataMemberPointers(CGM, I->getType(), |
| 1160 | Elements, StartOffset + BaseOffset); |
| 1161 | } |
| 1162 | |
| 1163 | // Visit all fields. |
| 1164 | unsigned FieldNo = 0; |
| 1165 | for (RecordDecl::field_iterator I = RD->field_begin(), |
| 1166 | E = RD->field_end(); I != E; ++I, ++FieldNo) { |
| 1167 | QualType FieldType = I->getType(); |
| 1168 | |
John McCall | f16aa10 | 2010-08-22 21:01:12 +0000 | [diff] [blame] | 1169 | if (CGM.getTypes().isZeroInitializable(FieldType)) |
Anders Carlsson | a83fb4b | 2010-05-18 16:51:41 +0000 | [diff] [blame] | 1170 | continue; |
| 1171 | |
| 1172 | uint64_t FieldOffset = StartOffset + Layout.getFieldOffset(FieldNo); |
| 1173 | FillInNullDataMemberPointers(CGM, FieldType, Elements, FieldOffset); |
| 1174 | } |
| 1175 | } else { |
| 1176 | assert(T->isMemberPointerType() && "Should only see member pointers here!"); |
| 1177 | assert(!T->getAs<MemberPointerType>()->getPointeeType()->isFunctionType() && |
| 1178 | "Should only see pointers to data members here!"); |
| 1179 | |
Ken Dyck | c4d68db | 2011-03-22 00:24:31 +0000 | [diff] [blame] | 1180 | CharUnits StartIndex = CGM.getContext().toCharUnitsFromBits(StartOffset); |
| 1181 | CharUnits EndIndex = StartIndex + CGM.getContext().getTypeSizeInChars(T); |
Anders Carlsson | a83fb4b | 2010-05-18 16:51:41 +0000 | [diff] [blame] | 1182 | |
John McCall | 5808ce4 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 1183 | // FIXME: hardcodes Itanium member pointer representation! |
Anders Carlsson | a83fb4b | 2010-05-18 16:51:41 +0000 | [diff] [blame] | 1184 | llvm::Constant *NegativeOne = |
| 1185 | llvm::ConstantInt::get(llvm::Type::getInt8Ty(CGM.getLLVMContext()), |
John McCall | 5808ce4 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 1186 | -1ULL, /*isSigned*/true); |
Anders Carlsson | a83fb4b | 2010-05-18 16:51:41 +0000 | [diff] [blame] | 1187 | |
| 1188 | // Fill in the null data member pointer. |
Ken Dyck | c4d68db | 2011-03-22 00:24:31 +0000 | [diff] [blame] | 1189 | for (CharUnits I = StartIndex; I != EndIndex; ++I) |
| 1190 | Elements[I.getQuantity()] = NegativeOne; |
Anders Carlsson | a83fb4b | 2010-05-18 16:51:41 +0000 | [diff] [blame] | 1191 | } |
| 1192 | } |
| 1193 | |
John McCall | 9b7da1c | 2011-02-15 06:40:56 +0000 | [diff] [blame] | 1194 | static llvm::Constant *EmitNullConstantForBase(CodeGenModule &CGM, |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 1195 | llvm::Type *baseType, |
John McCall | 9b7da1c | 2011-02-15 06:40:56 +0000 | [diff] [blame] | 1196 | const CXXRecordDecl *base); |
| 1197 | |
Anders Carlsson | ada4b32 | 2010-11-22 18:42:14 +0000 | [diff] [blame] | 1198 | static llvm::Constant *EmitNullConstant(CodeGenModule &CGM, |
John McCall | 9b7da1c | 2011-02-15 06:40:56 +0000 | [diff] [blame] | 1199 | const CXXRecordDecl *record, |
| 1200 | bool asCompleteObject) { |
| 1201 | const CGRecordLayout &layout = CGM.getTypes().getCGRecordLayout(record); |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 1202 | llvm::StructType *structure = |
John McCall | 9b7da1c | 2011-02-15 06:40:56 +0000 | [diff] [blame] | 1203 | (asCompleteObject ? layout.getLLVMType() |
| 1204 | : layout.getBaseSubobjectLLVMType()); |
Anders Carlsson | ada4b32 | 2010-11-22 18:42:14 +0000 | [diff] [blame] | 1205 | |
John McCall | 9b7da1c | 2011-02-15 06:40:56 +0000 | [diff] [blame] | 1206 | unsigned numElements = structure->getNumElements(); |
| 1207 | std::vector<llvm::Constant *> elements(numElements); |
Anders Carlsson | ada4b32 | 2010-11-22 18:42:14 +0000 | [diff] [blame] | 1208 | |
John McCall | 9b7da1c | 2011-02-15 06:40:56 +0000 | [diff] [blame] | 1209 | // Fill in all the bases. |
| 1210 | for (CXXRecordDecl::base_class_const_iterator |
| 1211 | I = record->bases_begin(), E = record->bases_end(); I != E; ++I) { |
Anders Carlsson | ada4b32 | 2010-11-22 18:42:14 +0000 | [diff] [blame] | 1212 | if (I->isVirtual()) { |
John McCall | 9b7da1c | 2011-02-15 06:40:56 +0000 | [diff] [blame] | 1213 | // Ignore virtual bases; if we're laying out for a complete |
| 1214 | // object, we'll lay these out later. |
Anders Carlsson | ada4b32 | 2010-11-22 18:42:14 +0000 | [diff] [blame] | 1215 | continue; |
| 1216 | } |
| 1217 | |
John McCall | 9b7da1c | 2011-02-15 06:40:56 +0000 | [diff] [blame] | 1218 | const CXXRecordDecl *base = |
| 1219 | cast<CXXRecordDecl>(I->getType()->castAs<RecordType>()->getDecl()); |
Anders Carlsson | ada4b32 | 2010-11-22 18:42:14 +0000 | [diff] [blame] | 1220 | |
| 1221 | // Ignore empty bases. |
John McCall | 9b7da1c | 2011-02-15 06:40:56 +0000 | [diff] [blame] | 1222 | if (base->isEmpty()) |
Anders Carlsson | ada4b32 | 2010-11-22 18:42:14 +0000 | [diff] [blame] | 1223 | continue; |
| 1224 | |
John McCall | 9b7da1c | 2011-02-15 06:40:56 +0000 | [diff] [blame] | 1225 | unsigned fieldIndex = layout.getNonVirtualBaseLLVMFieldNo(base); |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 1226 | llvm::Type *baseType = structure->getElementType(fieldIndex); |
John McCall | 9b7da1c | 2011-02-15 06:40:56 +0000 | [diff] [blame] | 1227 | elements[fieldIndex] = EmitNullConstantForBase(CGM, baseType, base); |
Anders Carlsson | ada4b32 | 2010-11-22 18:42:14 +0000 | [diff] [blame] | 1228 | } |
| 1229 | |
John McCall | 9b7da1c | 2011-02-15 06:40:56 +0000 | [diff] [blame] | 1230 | // Fill in all the fields. |
| 1231 | for (RecordDecl::field_iterator I = record->field_begin(), |
| 1232 | E = record->field_end(); I != E; ++I) { |
| 1233 | const FieldDecl *field = *I; |
Anders Carlsson | ada4b32 | 2010-11-22 18:42:14 +0000 | [diff] [blame] | 1234 | |
| 1235 | // Ignore bit fields. |
John McCall | 9b7da1c | 2011-02-15 06:40:56 +0000 | [diff] [blame] | 1236 | if (field->isBitField()) |
Anders Carlsson | ada4b32 | 2010-11-22 18:42:14 +0000 | [diff] [blame] | 1237 | continue; |
| 1238 | |
John McCall | 9b7da1c | 2011-02-15 06:40:56 +0000 | [diff] [blame] | 1239 | unsigned fieldIndex = layout.getLLVMFieldNo(field); |
| 1240 | elements[fieldIndex] = CGM.EmitNullConstant(field->getType()); |
| 1241 | } |
| 1242 | |
| 1243 | // Fill in the virtual bases, if we're working with the complete object. |
| 1244 | if (asCompleteObject) { |
| 1245 | for (CXXRecordDecl::base_class_const_iterator |
| 1246 | I = record->vbases_begin(), E = record->vbases_end(); I != E; ++I) { |
| 1247 | const CXXRecordDecl *base = |
| 1248 | cast<CXXRecordDecl>(I->getType()->castAs<RecordType>()->getDecl()); |
| 1249 | |
| 1250 | // Ignore empty bases. |
| 1251 | if (base->isEmpty()) |
| 1252 | continue; |
| 1253 | |
| 1254 | unsigned fieldIndex = layout.getVirtualBaseIndex(base); |
| 1255 | |
| 1256 | // We might have already laid this field out. |
| 1257 | if (elements[fieldIndex]) continue; |
| 1258 | |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 1259 | llvm::Type *baseType = structure->getElementType(fieldIndex); |
John McCall | 9b7da1c | 2011-02-15 06:40:56 +0000 | [diff] [blame] | 1260 | elements[fieldIndex] = EmitNullConstantForBase(CGM, baseType, base); |
| 1261 | } |
Anders Carlsson | ada4b32 | 2010-11-22 18:42:14 +0000 | [diff] [blame] | 1262 | } |
| 1263 | |
| 1264 | // Now go through all other fields and zero them out. |
John McCall | 9b7da1c | 2011-02-15 06:40:56 +0000 | [diff] [blame] | 1265 | for (unsigned i = 0; i != numElements; ++i) { |
| 1266 | if (!elements[i]) |
| 1267 | elements[i] = llvm::Constant::getNullValue(structure->getElementType(i)); |
Anders Carlsson | ada4b32 | 2010-11-22 18:42:14 +0000 | [diff] [blame] | 1268 | } |
| 1269 | |
John McCall | 9b7da1c | 2011-02-15 06:40:56 +0000 | [diff] [blame] | 1270 | return llvm::ConstantStruct::get(structure, elements); |
| 1271 | } |
| 1272 | |
| 1273 | /// Emit the null constant for a base subobject. |
| 1274 | static llvm::Constant *EmitNullConstantForBase(CodeGenModule &CGM, |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 1275 | llvm::Type *baseType, |
John McCall | 9b7da1c | 2011-02-15 06:40:56 +0000 | [diff] [blame] | 1276 | const CXXRecordDecl *base) { |
| 1277 | const CGRecordLayout &baseLayout = CGM.getTypes().getCGRecordLayout(base); |
| 1278 | |
| 1279 | // Just zero out bases that don't have any pointer to data members. |
| 1280 | if (baseLayout.isZeroInitializableAsBase()) |
| 1281 | return llvm::Constant::getNullValue(baseType); |
| 1282 | |
| 1283 | // If the base type is a struct, we can just use its null constant. |
| 1284 | if (isa<llvm::StructType>(baseType)) { |
| 1285 | return EmitNullConstant(CGM, base, /*complete*/ false); |
| 1286 | } |
| 1287 | |
| 1288 | // Otherwise, some bases are represented as arrays of i8 if the size |
| 1289 | // of the base is smaller than its corresponding LLVM type. Figure |
| 1290 | // out how many elements this base array has. |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 1291 | llvm::ArrayType *baseArrayType = cast<llvm::ArrayType>(baseType); |
John McCall | 9b7da1c | 2011-02-15 06:40:56 +0000 | [diff] [blame] | 1292 | unsigned numBaseElements = baseArrayType->getNumElements(); |
| 1293 | |
| 1294 | // Fill in null data member pointers. |
| 1295 | std::vector<llvm::Constant *> baseElements(numBaseElements); |
| 1296 | FillInNullDataMemberPointers(CGM, CGM.getContext().getTypeDeclType(base), |
| 1297 | baseElements, 0); |
| 1298 | |
| 1299 | // Now go through all other elements and zero them out. |
| 1300 | if (numBaseElements) { |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 1301 | llvm::Type *i8 = llvm::Type::getInt8Ty(CGM.getLLVMContext()); |
John McCall | 9b7da1c | 2011-02-15 06:40:56 +0000 | [diff] [blame] | 1302 | llvm::Constant *i8_zero = llvm::Constant::getNullValue(i8); |
| 1303 | for (unsigned i = 0; i != numBaseElements; ++i) { |
| 1304 | if (!baseElements[i]) |
| 1305 | baseElements[i] = i8_zero; |
| 1306 | } |
| 1307 | } |
| 1308 | |
| 1309 | return llvm::ConstantArray::get(baseArrayType, baseElements); |
Anders Carlsson | ada4b32 | 2010-11-22 18:42:14 +0000 | [diff] [blame] | 1310 | } |
| 1311 | |
Eli Friedman | 0f59312 | 2009-04-13 21:47:26 +0000 | [diff] [blame] | 1312 | llvm::Constant *CodeGenModule::EmitNullConstant(QualType T) { |
John McCall | f16aa10 | 2010-08-22 21:01:12 +0000 | [diff] [blame] | 1313 | if (getTypes().isZeroInitializable(T)) |
Anders Carlsson | ad3e711 | 2009-08-24 17:16:23 +0000 | [diff] [blame] | 1314 | return llvm::Constant::getNullValue(getTypes().ConvertTypeForMem(T)); |
Anders Carlsson | 2c12d03 | 2010-02-02 05:17:25 +0000 | [diff] [blame] | 1315 | |
Anders Carlsson | 237957c | 2009-08-09 18:26:27 +0000 | [diff] [blame] | 1316 | if (const ConstantArrayType *CAT = Context.getAsConstantArrayType(T)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1317 | |
Anders Carlsson | 237957c | 2009-08-09 18:26:27 +0000 | [diff] [blame] | 1318 | QualType ElementTy = CAT->getElementType(); |
| 1319 | |
Anders Carlsson | 2c12d03 | 2010-02-02 05:17:25 +0000 | [diff] [blame] | 1320 | llvm::Constant *Element = EmitNullConstant(ElementTy); |
| 1321 | unsigned NumElements = CAT->getSize().getZExtValue(); |
| 1322 | std::vector<llvm::Constant *> Array(NumElements); |
| 1323 | for (unsigned i = 0; i != NumElements; ++i) |
| 1324 | Array[i] = Element; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1325 | |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 1326 | llvm::ArrayType *ATy = |
Anders Carlsson | 2c12d03 | 2010-02-02 05:17:25 +0000 | [diff] [blame] | 1327 | cast<llvm::ArrayType>(getTypes().ConvertTypeForMem(T)); |
| 1328 | return llvm::ConstantArray::get(ATy, Array); |
Anders Carlsson | 237957c | 2009-08-09 18:26:27 +0000 | [diff] [blame] | 1329 | } |
Anders Carlsson | fc3eaa4 | 2009-08-23 01:25:01 +0000 | [diff] [blame] | 1330 | |
| 1331 | if (const RecordType *RT = T->getAs<RecordType>()) { |
Anders Carlsson | 2c12d03 | 2010-02-02 05:17:25 +0000 | [diff] [blame] | 1332 | const CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl()); |
John McCall | 9b7da1c | 2011-02-15 06:40:56 +0000 | [diff] [blame] | 1333 | return ::EmitNullConstant(*this, RD, /*complete object*/ true); |
Anders Carlsson | fc3eaa4 | 2009-08-23 01:25:01 +0000 | [diff] [blame] | 1334 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1335 | |
Anders Carlsson | a83fb4b | 2010-05-18 16:51:41 +0000 | [diff] [blame] | 1336 | assert(T->isMemberPointerType() && "Should only see member pointers here!"); |
Anders Carlsson | 2c12d03 | 2010-02-02 05:17:25 +0000 | [diff] [blame] | 1337 | assert(!T->getAs<MemberPointerType>()->getPointeeType()->isFunctionType() && |
| 1338 | "Should only see pointers to data members here!"); |
| 1339 | |
| 1340 | // Itanium C++ ABI 2.3: |
| 1341 | // A NULL pointer is represented as -1. |
John McCall | 5808ce4 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 1342 | return getCXXABI().EmitNullMemberPointer(T->castAs<MemberPointerType>()); |
Eli Friedman | 0f59312 | 2009-04-13 21:47:26 +0000 | [diff] [blame] | 1343 | } |
Eli Friedman | 2ed7cb6 | 2011-10-14 02:27:24 +0000 | [diff] [blame] | 1344 | |
| 1345 | llvm::Constant * |
| 1346 | CodeGenModule::EmitNullConstantForBase(const CXXRecordDecl *Record) { |
| 1347 | return ::EmitNullConstant(*this, Record, false); |
| 1348 | } |