Chris Lattner | 820dce8 | 2007-08-24 02:22:53 +0000 | [diff] [blame] | 1 | //===--- CGExprAgg.cpp - Emit LLVM Code from Aggregate Expressions --------===// |
Chris Lattner | 78ed840 | 2007-08-10 20:13:28 +0000 | [diff] [blame] | 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 959e5be | 2007-12-29 19:59:25 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Chris Lattner | 78ed840 | 2007-08-10 20:13:28 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This contains code to emit Aggregate Expr nodes as LLVM code. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "CodeGenFunction.h" |
Chris Lattner | bdb8ffb | 2007-08-11 00:04:45 +0000 | [diff] [blame] | 15 | #include "CodeGenModule.h" |
| 16 | #include "clang/AST/AST.h" |
| 17 | #include "llvm/Constants.h" |
| 18 | #include "llvm/Function.h" |
Devang Patel | ddde6d5 | 2007-10-26 17:44:44 +0000 | [diff] [blame] | 19 | #include "llvm/GlobalVariable.h" |
Chris Lattner | b50e390 | 2007-08-21 04:25:47 +0000 | [diff] [blame] | 20 | #include "llvm/Support/Compiler.h" |
Chris Lattner | 78ed840 | 2007-08-10 20:13:28 +0000 | [diff] [blame] | 21 | using namespace clang; |
| 22 | using namespace CodeGen; |
Chris Lattner | bdb8ffb | 2007-08-11 00:04:45 +0000 | [diff] [blame] | 23 | |
Chris Lattner | b50e390 | 2007-08-21 04:25:47 +0000 | [diff] [blame] | 24 | //===----------------------------------------------------------------------===// |
| 25 | // Aggregate Expression Emitter |
| 26 | //===----------------------------------------------------------------------===// |
| 27 | |
| 28 | namespace { |
| 29 | class VISIBILITY_HIDDEN AggExprEmitter : public StmtVisitor<AggExprEmitter> { |
| 30 | CodeGenFunction &CGF; |
Devang Patel | 638b64c | 2007-10-09 19:49:58 +0000 | [diff] [blame] | 31 | llvm::LLVMFoldingBuilder &Builder; |
Chris Lattner | b50e390 | 2007-08-21 04:25:47 +0000 | [diff] [blame] | 32 | llvm::Value *DestPtr; |
| 33 | bool VolatileDest; |
| 34 | public: |
| 35 | AggExprEmitter(CodeGenFunction &cgf, llvm::Value *destPtr, bool volatileDest) |
Chris Lattner | 41b1fca | 2007-08-26 23:13:56 +0000 | [diff] [blame] | 36 | : CGF(cgf), Builder(CGF.Builder), |
| 37 | DestPtr(destPtr), VolatileDest(volatileDest) { |
Chris Lattner | b50e390 | 2007-08-21 04:25:47 +0000 | [diff] [blame] | 38 | } |
| 39 | |
Chris Lattner | a730010 | 2007-08-21 04:59:27 +0000 | [diff] [blame] | 40 | //===--------------------------------------------------------------------===// |
| 41 | // Utilities |
| 42 | //===--------------------------------------------------------------------===// |
| 43 | |
Chris Lattner | b50e390 | 2007-08-21 04:25:47 +0000 | [diff] [blame] | 44 | /// EmitAggLoadOfLValue - Given an expression with aggregate type that |
| 45 | /// represents a value lvalue, this method emits the address of the lvalue, |
| 46 | /// then loads the result into DestPtr. |
| 47 | void EmitAggLoadOfLValue(const Expr *E); |
| 48 | |
Chris Lattner | 41b1fca | 2007-08-26 23:13:56 +0000 | [diff] [blame] | 49 | void EmitAggregateCopy(llvm::Value *DestPtr, llvm::Value *SrcPtr, |
| 50 | QualType EltTy); |
Lauro Ramos Venancio | 97b0957 | 2008-02-19 22:04:22 +0000 | [diff] [blame^] | 51 | |
| 52 | void EmitAggregateClear(llvm::Value *DestPtr, QualType Ty); |
| 53 | |
| 54 | void EmitNonConstInit(InitListExpr *E); |
Chris Lattner | a730010 | 2007-08-21 04:59:27 +0000 | [diff] [blame] | 55 | |
| 56 | //===--------------------------------------------------------------------===// |
| 57 | // Visitor Methods |
| 58 | //===--------------------------------------------------------------------===// |
| 59 | |
Chris Lattner | b50e390 | 2007-08-21 04:25:47 +0000 | [diff] [blame] | 60 | void VisitStmt(Stmt *S) { |
Chris Lattner | e8f4963 | 2007-12-02 01:49:16 +0000 | [diff] [blame] | 61 | CGF.WarnUnsupported(S, "aggregate expression"); |
Chris Lattner | b50e390 | 2007-08-21 04:25:47 +0000 | [diff] [blame] | 62 | } |
| 63 | void VisitParenExpr(ParenExpr *PE) { Visit(PE->getSubExpr()); } |
| 64 | |
| 65 | // l-values. |
Seo Sanghyeon | 32666c5 | 2007-12-14 02:04:12 +0000 | [diff] [blame] | 66 | void VisitDeclRefExpr(DeclRefExpr *DRE) { EmitAggLoadOfLValue(DRE); } |
| 67 | void VisitMemberExpr(MemberExpr *ME) { EmitAggLoadOfLValue(ME); } |
| 68 | void VisitUnaryDeref(UnaryOperator *E) { EmitAggLoadOfLValue(E); } |
Seo Sanghyeon | 3c2d5fb | 2007-12-23 03:11:58 +0000 | [diff] [blame] | 69 | void VisitStringLiteral(StringLiteral *E) { EmitAggLoadOfLValue(E); } |
Seo Sanghyeon | 32666c5 | 2007-12-14 02:04:12 +0000 | [diff] [blame] | 70 | |
| 71 | void VisitArraySubscriptExpr(ArraySubscriptExpr *E) { |
| 72 | EmitAggLoadOfLValue(E); |
| 73 | } |
Chris Lattner | b50e390 | 2007-08-21 04:25:47 +0000 | [diff] [blame] | 74 | |
| 75 | // Operators. |
| 76 | // case Expr::UnaryOperatorClass: |
Chris Lattner | b50e390 | 2007-08-21 04:25:47 +0000 | [diff] [blame] | 77 | // case Expr::CastExprClass: |
Anders Carlsson | f2712ac | 2008-01-14 06:28:57 +0000 | [diff] [blame] | 78 | void VisitImplicitCastExpr(ImplicitCastExpr *E); |
Anders Carlsson | 0ae1541 | 2007-10-31 22:04:46 +0000 | [diff] [blame] | 79 | void VisitCallExpr(const CallExpr *E); |
Chris Lattner | bab0bfb | 2007-08-31 22:54:14 +0000 | [diff] [blame] | 80 | void VisitStmtExpr(const StmtExpr *E); |
Chris Lattner | b50e390 | 2007-08-21 04:25:47 +0000 | [diff] [blame] | 81 | void VisitBinaryOperator(const BinaryOperator *BO); |
Chris Lattner | 76cb189 | 2007-08-21 04:43:17 +0000 | [diff] [blame] | 82 | void VisitBinAssign(const BinaryOperator *E); |
Nate Begeman | c6078c9 | 2008-01-31 05:38:29 +0000 | [diff] [blame] | 83 | void VisitOverloadExpr(const OverloadExpr *E); |
Chris Lattner | b50e390 | 2007-08-21 04:25:47 +0000 | [diff] [blame] | 84 | |
| 85 | |
| 86 | void VisitConditionalOperator(const ConditionalOperator *CO); |
Devang Patel | ddde6d5 | 2007-10-26 17:44:44 +0000 | [diff] [blame] | 87 | void VisitInitListExpr(InitListExpr *E); |
Chris Lattner | b50e390 | 2007-08-21 04:25:47 +0000 | [diff] [blame] | 88 | // case Expr::ChooseExprClass: |
Lauro Ramos Venancio | 9eb37a2 | 2008-02-18 22:44:02 +0000 | [diff] [blame] | 89 | |
Chris Lattner | b50e390 | 2007-08-21 04:25:47 +0000 | [diff] [blame] | 90 | }; |
| 91 | } // end anonymous namespace. |
| 92 | |
Chris Lattner | a730010 | 2007-08-21 04:59:27 +0000 | [diff] [blame] | 93 | //===----------------------------------------------------------------------===// |
| 94 | // Utilities |
| 95 | //===----------------------------------------------------------------------===// |
Chris Lattner | b50e390 | 2007-08-21 04:25:47 +0000 | [diff] [blame] | 96 | |
Lauro Ramos Venancio | 97b0957 | 2008-02-19 22:04:22 +0000 | [diff] [blame^] | 97 | void AggExprEmitter::EmitAggregateClear(llvm::Value *DestPtr, QualType Ty) { |
| 98 | assert(!Ty->isComplexType() && "Shouldn't happen for complex"); |
| 99 | |
| 100 | // Aggregate assignment turns into llvm.memset. |
| 101 | const llvm::Type *BP = llvm::PointerType::getUnqual(llvm::Type::Int8Ty); |
| 102 | if (DestPtr->getType() != BP) |
| 103 | DestPtr = Builder.CreateBitCast(DestPtr, BP, "tmp"); |
| 104 | |
| 105 | // Get size and alignment info for this aggregate. |
| 106 | std::pair<uint64_t, unsigned> TypeInfo = |
| 107 | CGF.getContext().getTypeInfo(Ty, SourceLocation()); |
| 108 | |
| 109 | // FIXME: Handle variable sized types. |
| 110 | const llvm::Type *IntPtr = llvm::IntegerType::get(CGF.LLVMPointerWidth); |
| 111 | |
| 112 | llvm::Value *MemSetOps[4] = { |
| 113 | DestPtr, |
| 114 | llvm::ConstantInt::getNullValue(llvm::Type::Int8Ty), |
| 115 | // TypeInfo.first describes size in bits. |
| 116 | llvm::ConstantInt::get(IntPtr, TypeInfo.first/8), |
| 117 | llvm::ConstantInt::get(llvm::Type::Int32Ty, TypeInfo.second/8) |
| 118 | }; |
| 119 | |
| 120 | Builder.CreateCall(CGF.CGM.getMemSetFn(), MemSetOps, MemSetOps+4); |
| 121 | } |
| 122 | |
Chris Lattner | 41b1fca | 2007-08-26 23:13:56 +0000 | [diff] [blame] | 123 | void AggExprEmitter::EmitAggregateCopy(llvm::Value *DestPtr, |
| 124 | llvm::Value *SrcPtr, QualType Ty) { |
| 125 | assert(!Ty->isComplexType() && "Shouldn't happen for complex"); |
| 126 | |
| 127 | // Aggregate assignment turns into llvm.memcpy. |
Christopher Lamb | 4fe5e70 | 2007-12-17 01:11:20 +0000 | [diff] [blame] | 128 | const llvm::Type *BP = llvm::PointerType::getUnqual(llvm::Type::Int8Ty); |
Chris Lattner | 41b1fca | 2007-08-26 23:13:56 +0000 | [diff] [blame] | 129 | if (DestPtr->getType() != BP) |
| 130 | DestPtr = Builder.CreateBitCast(DestPtr, BP, "tmp"); |
| 131 | if (SrcPtr->getType() != BP) |
| 132 | SrcPtr = Builder.CreateBitCast(SrcPtr, BP, "tmp"); |
| 133 | |
| 134 | // Get size and alignment info for this aggregate. |
| 135 | std::pair<uint64_t, unsigned> TypeInfo = |
| 136 | CGF.getContext().getTypeInfo(Ty, SourceLocation()); |
| 137 | |
| 138 | // FIXME: Handle variable sized types. |
| 139 | const llvm::Type *IntPtr = llvm::IntegerType::get(CGF.LLVMPointerWidth); |
| 140 | |
| 141 | llvm::Value *MemCpyOps[4] = { |
| 142 | DestPtr, SrcPtr, |
Devang Patel | ddde6d5 | 2007-10-26 17:44:44 +0000 | [diff] [blame] | 143 | // TypeInfo.first describes size in bits. |
| 144 | llvm::ConstantInt::get(IntPtr, TypeInfo.first/8), |
Lauro Ramos Venancio | 97b0957 | 2008-02-19 22:04:22 +0000 | [diff] [blame^] | 145 | llvm::ConstantInt::get(llvm::Type::Int32Ty, TypeInfo.second/8) |
Chris Lattner | 41b1fca | 2007-08-26 23:13:56 +0000 | [diff] [blame] | 146 | }; |
| 147 | |
| 148 | Builder.CreateCall(CGF.CGM.getMemCpyFn(), MemCpyOps, MemCpyOps+4); |
| 149 | } |
| 150 | |
| 151 | |
Chris Lattner | bdb8ffb | 2007-08-11 00:04:45 +0000 | [diff] [blame] | 152 | /// EmitAggLoadOfLValue - Given an expression with aggregate type that |
| 153 | /// represents a value lvalue, this method emits the address of the lvalue, |
| 154 | /// then loads the result into DestPtr. |
Chris Lattner | b50e390 | 2007-08-21 04:25:47 +0000 | [diff] [blame] | 155 | void AggExprEmitter::EmitAggLoadOfLValue(const Expr *E) { |
| 156 | LValue LV = CGF.EmitLValue(E); |
Chris Lattner | bdb8ffb | 2007-08-11 00:04:45 +0000 | [diff] [blame] | 157 | assert(LV.isSimple() && "Can't have aggregate bitfield, vector, etc"); |
| 158 | llvm::Value *SrcPtr = LV.getAddress(); |
| 159 | |
| 160 | // If the result is ignored, don't copy from the value. |
| 161 | if (DestPtr == 0) |
| 162 | // FIXME: If the source is volatile, we must read from it. |
| 163 | return; |
| 164 | |
Chris Lattner | 41b1fca | 2007-08-26 23:13:56 +0000 | [diff] [blame] | 165 | EmitAggregateCopy(DestPtr, SrcPtr, E->getType()); |
Chris Lattner | bdb8ffb | 2007-08-11 00:04:45 +0000 | [diff] [blame] | 166 | } |
| 167 | |
Chris Lattner | a730010 | 2007-08-21 04:59:27 +0000 | [diff] [blame] | 168 | //===----------------------------------------------------------------------===// |
| 169 | // Visitor Methods |
| 170 | //===----------------------------------------------------------------------===// |
| 171 | |
Anders Carlsson | f2712ac | 2008-01-14 06:28:57 +0000 | [diff] [blame] | 172 | void AggExprEmitter::VisitImplicitCastExpr(ImplicitCastExpr *E) |
| 173 | { |
| 174 | QualType STy = E->getSubExpr()->getType().getCanonicalType(); |
| 175 | QualType Ty = E->getType().getCanonicalType(); |
Eli Friedman | f021712 | 2008-02-11 01:09:17 +0000 | [diff] [blame] | 176 | |
| 177 | assert(CGF.getContext().typesAreCompatible( |
| 178 | STy.getUnqualifiedType(), Ty.getUnqualifiedType()) |
| 179 | && "Implicit cast types must be compatible"); |
Anders Carlsson | f2712ac | 2008-01-14 06:28:57 +0000 | [diff] [blame] | 180 | |
| 181 | Visit(E->getSubExpr()); |
| 182 | } |
| 183 | |
Anders Carlsson | 0ae1541 | 2007-10-31 22:04:46 +0000 | [diff] [blame] | 184 | void AggExprEmitter::VisitCallExpr(const CallExpr *E) |
| 185 | { |
| 186 | RValue RV = CGF.EmitCallExpr(E); |
| 187 | assert(RV.isAggregate() && "Return value must be aggregate value!"); |
| 188 | |
| 189 | // If the result is ignored, don't copy from the value. |
| 190 | if (DestPtr == 0) |
| 191 | // FIXME: If the source is volatile, we must read from it. |
| 192 | return; |
| 193 | |
| 194 | EmitAggregateCopy(DestPtr, RV.getAggregateAddr(), E->getType()); |
| 195 | } |
| 196 | |
Nate Begeman | c6078c9 | 2008-01-31 05:38:29 +0000 | [diff] [blame] | 197 | void AggExprEmitter::VisitOverloadExpr(const OverloadExpr *E) |
| 198 | { |
| 199 | RValue RV = CGF.EmitCallExpr(E->getFn(), E->arg_begin(), |
| 200 | E->getNumArgs(CGF.getContext())); |
| 201 | assert(RV.isAggregate() && "Return value must be aggregate value!"); |
| 202 | |
| 203 | // If the result is ignored, don't copy from the value. |
| 204 | if (DestPtr == 0) |
| 205 | // FIXME: If the source is volatile, we must read from it. |
| 206 | return; |
| 207 | |
| 208 | EmitAggregateCopy(DestPtr, RV.getAggregateAddr(), E->getType()); |
| 209 | } |
| 210 | |
Chris Lattner | bab0bfb | 2007-08-31 22:54:14 +0000 | [diff] [blame] | 211 | void AggExprEmitter::VisitStmtExpr(const StmtExpr *E) { |
| 212 | CGF.EmitCompoundStmt(*E->getSubStmt(), true, DestPtr, VolatileDest); |
| 213 | } |
| 214 | |
Chris Lattner | b50e390 | 2007-08-21 04:25:47 +0000 | [diff] [blame] | 215 | void AggExprEmitter::VisitBinaryOperator(const BinaryOperator *E) { |
Chris Lattner | e8f4963 | 2007-12-02 01:49:16 +0000 | [diff] [blame] | 216 | CGF.WarnUnsupported(E, "aggregate binary expression"); |
Chris Lattner | a730010 | 2007-08-21 04:59:27 +0000 | [diff] [blame] | 217 | } |
| 218 | |
Chris Lattner | 76cb189 | 2007-08-21 04:43:17 +0000 | [diff] [blame] | 219 | void AggExprEmitter::VisitBinAssign(const BinaryOperator *E) { |
Eli Friedman | f021712 | 2008-02-11 01:09:17 +0000 | [diff] [blame] | 220 | // For an assignment to work, the value on the right has |
| 221 | // to be compatible with the value on the left. |
| 222 | assert(CGF.getContext().typesAreCompatible( |
| 223 | E->getLHS()->getType().getUnqualifiedType(), |
| 224 | E->getRHS()->getType().getUnqualifiedType()) |
| 225 | && "Invalid assignment"); |
Chris Lattner | b50e390 | 2007-08-21 04:25:47 +0000 | [diff] [blame] | 226 | LValue LHS = CGF.EmitLValue(E->getLHS()); |
Chris Lattner | bdb8ffb | 2007-08-11 00:04:45 +0000 | [diff] [blame] | 227 | |
| 228 | // Codegen the RHS so that it stores directly into the LHS. |
Chris Lattner | b50e390 | 2007-08-21 04:25:47 +0000 | [diff] [blame] | 229 | CGF.EmitAggExpr(E->getRHS(), LHS.getAddress(), false /*FIXME: VOLATILE LHS*/); |
Chris Lattner | bdb8ffb | 2007-08-11 00:04:45 +0000 | [diff] [blame] | 230 | |
Eli Friedman | f021712 | 2008-02-11 01:09:17 +0000 | [diff] [blame] | 231 | if (DestPtr == 0) |
| 232 | return; |
| 233 | |
Chris Lattner | bdb8ffb | 2007-08-11 00:04:45 +0000 | [diff] [blame] | 234 | // If the result of the assignment is used, copy the RHS there also. |
Eli Friedman | f021712 | 2008-02-11 01:09:17 +0000 | [diff] [blame] | 235 | EmitAggregateCopy(DestPtr, LHS.getAddress(), E->getType()); |
Chris Lattner | bdb8ffb | 2007-08-11 00:04:45 +0000 | [diff] [blame] | 236 | } |
| 237 | |
Chris Lattner | b50e390 | 2007-08-21 04:25:47 +0000 | [diff] [blame] | 238 | void AggExprEmitter::VisitConditionalOperator(const ConditionalOperator *E) { |
Chris Lattner | bdb8ffb | 2007-08-11 00:04:45 +0000 | [diff] [blame] | 239 | llvm::BasicBlock *LHSBlock = new llvm::BasicBlock("cond.?"); |
| 240 | llvm::BasicBlock *RHSBlock = new llvm::BasicBlock("cond.:"); |
| 241 | llvm::BasicBlock *ContBlock = new llvm::BasicBlock("cond.cont"); |
| 242 | |
Chris Lattner | b50e390 | 2007-08-21 04:25:47 +0000 | [diff] [blame] | 243 | llvm::Value *Cond = CGF.EvaluateExprAsBool(E->getCond()); |
Chris Lattner | 41b1fca | 2007-08-26 23:13:56 +0000 | [diff] [blame] | 244 | Builder.CreateCondBr(Cond, LHSBlock, RHSBlock); |
Chris Lattner | bdb8ffb | 2007-08-11 00:04:45 +0000 | [diff] [blame] | 245 | |
Chris Lattner | b50e390 | 2007-08-21 04:25:47 +0000 | [diff] [blame] | 246 | CGF.EmitBlock(LHSBlock); |
Chris Lattner | bdb8ffb | 2007-08-11 00:04:45 +0000 | [diff] [blame] | 247 | |
| 248 | // Handle the GNU extension for missing LHS. |
| 249 | assert(E->getLHS() && "Must have LHS for aggregate value"); |
| 250 | |
Chris Lattner | 55165ba | 2007-08-21 05:02:10 +0000 | [diff] [blame] | 251 | Visit(E->getLHS()); |
Chris Lattner | 41b1fca | 2007-08-26 23:13:56 +0000 | [diff] [blame] | 252 | Builder.CreateBr(ContBlock); |
| 253 | LHSBlock = Builder.GetInsertBlock(); |
Chris Lattner | bdb8ffb | 2007-08-11 00:04:45 +0000 | [diff] [blame] | 254 | |
Chris Lattner | b50e390 | 2007-08-21 04:25:47 +0000 | [diff] [blame] | 255 | CGF.EmitBlock(RHSBlock); |
Chris Lattner | bdb8ffb | 2007-08-11 00:04:45 +0000 | [diff] [blame] | 256 | |
Chris Lattner | 55165ba | 2007-08-21 05:02:10 +0000 | [diff] [blame] | 257 | Visit(E->getRHS()); |
Chris Lattner | 41b1fca | 2007-08-26 23:13:56 +0000 | [diff] [blame] | 258 | Builder.CreateBr(ContBlock); |
| 259 | RHSBlock = Builder.GetInsertBlock(); |
Chris Lattner | bdb8ffb | 2007-08-11 00:04:45 +0000 | [diff] [blame] | 260 | |
Chris Lattner | b50e390 | 2007-08-21 04:25:47 +0000 | [diff] [blame] | 261 | CGF.EmitBlock(ContBlock); |
Chris Lattner | bdb8ffb | 2007-08-11 00:04:45 +0000 | [diff] [blame] | 262 | } |
Chris Lattner | a730010 | 2007-08-21 04:59:27 +0000 | [diff] [blame] | 263 | |
Lauro Ramos Venancio | 97b0957 | 2008-02-19 22:04:22 +0000 | [diff] [blame^] | 264 | void AggExprEmitter::EmitNonConstInit(InitListExpr *E) { |
| 265 | |
| 266 | const llvm::PointerType *APType = |
| 267 | cast<llvm::PointerType>(DestPtr->getType()); |
| 268 | const llvm::Type *DestType = APType->getElementType(); |
Lauro Ramos Venancio | 9eb37a2 | 2008-02-18 22:44:02 +0000 | [diff] [blame] | 269 | |
| 270 | if (const llvm::ArrayType *AType = dyn_cast<llvm::ArrayType>(DestType)) { |
Lauro Ramos Venancio | 97b0957 | 2008-02-19 22:04:22 +0000 | [diff] [blame^] | 271 | unsigned NumInitElements = E->getNumInits(); |
Lauro Ramos Venancio | 9eb37a2 | 2008-02-18 22:44:02 +0000 | [diff] [blame] | 272 | |
| 273 | llvm::Value *Idxs[] = { |
| 274 | llvm::Constant::getNullValue(llvm::Type::Int32Ty), |
| 275 | NULL |
| 276 | }; |
| 277 | llvm::Value *NextVal = NULL; |
| 278 | unsigned i; |
| 279 | for (i = 0; i != NumInitElements; ++i) { |
| 280 | Idxs[1] = llvm::ConstantInt::get(llvm::Type::Int32Ty, i); |
| 281 | NextVal = Builder.CreateGEP(DestPtr, Idxs, Idxs + 2,".array"); |
Lauro Ramos Venancio | 97b0957 | 2008-02-19 22:04:22 +0000 | [diff] [blame^] | 282 | Expr *Init = E->getInit(i); |
| 283 | if (isa<InitListExpr>(Init)) |
| 284 | CGF.EmitAggExpr(Init, NextVal, VolatileDest); |
| 285 | else |
| 286 | Builder.CreateStore(CGF.EmitScalarExpr(Init), NextVal); |
Lauro Ramos Venancio | 9eb37a2 | 2008-02-18 22:44:02 +0000 | [diff] [blame] | 287 | } |
| 288 | |
| 289 | // Emit remaining default initializers |
| 290 | unsigned NumArrayElements = AType->getNumElements(); |
Lauro Ramos Venancio | 97b0957 | 2008-02-19 22:04:22 +0000 | [diff] [blame^] | 291 | QualType QType = E->getInit(0)->getType(); |
| 292 | const llvm::Type *EType = AType->getElementType(); |
Lauro Ramos Venancio | 9eb37a2 | 2008-02-18 22:44:02 +0000 | [diff] [blame] | 293 | for (/*Do not initialize i*/; i < NumArrayElements; ++i) { |
| 294 | Idxs[1] = llvm::ConstantInt::get(llvm::Type::Int32Ty, i); |
| 295 | NextVal = Builder.CreateGEP(DestPtr, Idxs, Idxs + 2,".array"); |
Lauro Ramos Venancio | 97b0957 | 2008-02-19 22:04:22 +0000 | [diff] [blame^] | 296 | if (EType->isFirstClassType()) |
| 297 | Builder.CreateStore(llvm::Constant::getNullValue(EType), NextVal); |
| 298 | else |
| 299 | EmitAggregateClear(NextVal, QType); |
Lauro Ramos Venancio | 9eb37a2 | 2008-02-18 22:44:02 +0000 | [diff] [blame] | 300 | } |
Lauro Ramos Venancio | 97b0957 | 2008-02-19 22:04:22 +0000 | [diff] [blame^] | 301 | } else |
| 302 | assert(false && "Invalid initializer"); |
Lauro Ramos Venancio | 9eb37a2 | 2008-02-18 22:44:02 +0000 | [diff] [blame] | 303 | } |
| 304 | |
| 305 | void AggExprEmitter::VisitInitListExpr(InitListExpr *E) { |
Devang Patel | ddde6d5 | 2007-10-26 17:44:44 +0000 | [diff] [blame] | 306 | |
Lauro Ramos Venancio | a62b1df | 2008-02-19 19:27:31 +0000 | [diff] [blame] | 307 | if (E->isConstantExpr(CGF.CGM.getContext(), NULL)) { |
| 308 | llvm::Constant *V = CGF.CGM.EmitConstantExpr(E); |
Devang Patel | ddde6d5 | 2007-10-26 17:44:44 +0000 | [diff] [blame] | 309 | // Create global value to hold this array. |
Lauro Ramos Venancio | 9eb37a2 | 2008-02-18 22:44:02 +0000 | [diff] [blame] | 310 | V = new llvm::GlobalVariable(V->getType(), true, |
Devang Patel | ddde6d5 | 2007-10-26 17:44:44 +0000 | [diff] [blame] | 311 | llvm::GlobalValue::InternalLinkage, |
Lauro Ramos Venancio | 9eb37a2 | 2008-02-18 22:44:02 +0000 | [diff] [blame] | 312 | V, ".array", |
Devang Patel | ddde6d5 | 2007-10-26 17:44:44 +0000 | [diff] [blame] | 313 | &CGF.CGM.getModule()); |
Lauro Ramos Venancio | 9eb37a2 | 2008-02-18 22:44:02 +0000 | [diff] [blame] | 314 | |
Devang Patel | ddde6d5 | 2007-10-26 17:44:44 +0000 | [diff] [blame] | 315 | EmitAggregateCopy(DestPtr, V , E->getType()); |
| 316 | return; |
Lauro Ramos Venancio | a62b1df | 2008-02-19 19:27:31 +0000 | [diff] [blame] | 317 | } else { |
| 318 | if (!E->getType()->isArrayType()) { |
| 319 | CGF.WarnUnsupported(E, "aggregate init-list expression"); |
| 320 | return; |
| 321 | } |
Lauro Ramos Venancio | 97b0957 | 2008-02-19 22:04:22 +0000 | [diff] [blame^] | 322 | EmitNonConstInit(E); |
Lauro Ramos Venancio | a62b1df | 2008-02-19 19:27:31 +0000 | [diff] [blame] | 323 | } |
Devang Patel | ddde6d5 | 2007-10-26 17:44:44 +0000 | [diff] [blame] | 324 | } |
| 325 | |
Chris Lattner | a730010 | 2007-08-21 04:59:27 +0000 | [diff] [blame] | 326 | //===----------------------------------------------------------------------===// |
| 327 | // Entry Points into this File |
| 328 | //===----------------------------------------------------------------------===// |
| 329 | |
| 330 | /// EmitAggExpr - Emit the computation of the specified expression of |
| 331 | /// aggregate type. The result is computed into DestPtr. Note that if |
| 332 | /// DestPtr is null, the value of the aggregate expression is not needed. |
| 333 | void CodeGenFunction::EmitAggExpr(const Expr *E, llvm::Value *DestPtr, |
| 334 | bool VolatileDest) { |
| 335 | assert(E && hasAggregateLLVMType(E->getType()) && |
| 336 | "Invalid aggregate expression to emit"); |
| 337 | |
| 338 | AggExprEmitter(*this, DestPtr, VolatileDest).Visit(const_cast<Expr*>(E)); |
| 339 | } |