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