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 | f81557c | 2008-04-04 18:42:16 +0000 | [diff] [blame] | 21 | #include "llvm/Intrinsics.h" |
Chris Lattner | af6f528 | 2007-08-10 20:13:28 +0000 | [diff] [blame] | 22 | using namespace clang; |
| 23 | using namespace CodeGen; |
Chris Lattner | 883f6a7 | 2007-08-11 00:04:45 +0000 | [diff] [blame] | 24 | |
Chris Lattner | 9c03356 | 2007-08-21 04:25:47 +0000 | [diff] [blame] | 25 | //===----------------------------------------------------------------------===// |
| 26 | // Aggregate Expression Emitter |
| 27 | //===----------------------------------------------------------------------===// |
| 28 | |
| 29 | namespace { |
| 30 | class VISIBILITY_HIDDEN AggExprEmitter : public StmtVisitor<AggExprEmitter> { |
| 31 | CodeGenFunction &CGF; |
Devang Patel | 50c9034 | 2007-10-09 19:49:58 +0000 | [diff] [blame] | 32 | llvm::LLVMFoldingBuilder &Builder; |
Chris Lattner | 9c03356 | 2007-08-21 04:25:47 +0000 | [diff] [blame] | 33 | llvm::Value *DestPtr; |
| 34 | bool VolatileDest; |
| 35 | public: |
| 36 | AggExprEmitter(CodeGenFunction &cgf, llvm::Value *destPtr, bool volatileDest) |
Chris Lattner | bfc0c1a | 2007-08-26 23:13:56 +0000 | [diff] [blame] | 37 | : CGF(cgf), Builder(CGF.Builder), |
| 38 | DestPtr(destPtr), VolatileDest(volatileDest) { |
Chris Lattner | 9c03356 | 2007-08-21 04:25:47 +0000 | [diff] [blame] | 39 | } |
| 40 | |
Chris Lattner | ee755f9 | 2007-08-21 04:59:27 +0000 | [diff] [blame] | 41 | //===--------------------------------------------------------------------===// |
| 42 | // Utilities |
| 43 | //===--------------------------------------------------------------------===// |
| 44 | |
Chris Lattner | 9c03356 | 2007-08-21 04:25:47 +0000 | [diff] [blame] | 45 | /// EmitAggLoadOfLValue - Given an expression with aggregate type that |
| 46 | /// represents a value lvalue, this method emits the address of the lvalue, |
| 47 | /// then loads the result into DestPtr. |
| 48 | void EmitAggLoadOfLValue(const Expr *E); |
| 49 | |
Chris Lattner | bfc0c1a | 2007-08-26 23:13:56 +0000 | [diff] [blame] | 50 | void EmitAggregateCopy(llvm::Value *DestPtr, llvm::Value *SrcPtr, |
| 51 | QualType EltTy); |
Lauro Ramos Venancio | 13e22cf | 2008-02-19 22:04:22 +0000 | [diff] [blame] | 52 | |
| 53 | void EmitAggregateClear(llvm::Value *DestPtr, QualType Ty); |
| 54 | |
| 55 | void EmitNonConstInit(InitListExpr *E); |
Chris Lattner | ee755f9 | 2007-08-21 04:59:27 +0000 | [diff] [blame] | 56 | |
| 57 | //===--------------------------------------------------------------------===// |
| 58 | // Visitor Methods |
| 59 | //===--------------------------------------------------------------------===// |
| 60 | |
Chris Lattner | 9c03356 | 2007-08-21 04:25:47 +0000 | [diff] [blame] | 61 | void VisitStmt(Stmt *S) { |
Chris Lattner | dc4d280 | 2007-12-02 01:49:16 +0000 | [diff] [blame] | 62 | CGF.WarnUnsupported(S, "aggregate expression"); |
Chris Lattner | 9c03356 | 2007-08-21 04:25:47 +0000 | [diff] [blame] | 63 | } |
| 64 | void VisitParenExpr(ParenExpr *PE) { Visit(PE->getSubExpr()); } |
| 65 | |
| 66 | // l-values. |
Seo Sanghyeon | 9b73b39 | 2007-12-14 02:04:12 +0000 | [diff] [blame] | 67 | void VisitDeclRefExpr(DeclRefExpr *DRE) { EmitAggLoadOfLValue(DRE); } |
| 68 | void VisitMemberExpr(MemberExpr *ME) { EmitAggLoadOfLValue(ME); } |
| 69 | void VisitUnaryDeref(UnaryOperator *E) { EmitAggLoadOfLValue(E); } |
Seo Sanghyeon | ad6ebd6 | 2007-12-23 03:11:58 +0000 | [diff] [blame] | 70 | void VisitStringLiteral(StringLiteral *E) { EmitAggLoadOfLValue(E); } |
Seo Sanghyeon | 9b73b39 | 2007-12-14 02:04:12 +0000 | [diff] [blame] | 71 | |
| 72 | void VisitArraySubscriptExpr(ArraySubscriptExpr *E) { |
| 73 | EmitAggLoadOfLValue(E); |
| 74 | } |
Chris Lattner | 9c03356 | 2007-08-21 04:25:47 +0000 | [diff] [blame] | 75 | |
| 76 | // Operators. |
| 77 | // case Expr::UnaryOperatorClass: |
Chris Lattner | 9c03356 | 2007-08-21 04:25:47 +0000 | [diff] [blame] | 78 | // case Expr::CastExprClass: |
Anders Carlsson | e4707ff | 2008-01-14 06:28:57 +0000 | [diff] [blame] | 79 | void VisitImplicitCastExpr(ImplicitCastExpr *E); |
Anders Carlsson | 148fe67 | 2007-10-31 22:04:46 +0000 | [diff] [blame] | 80 | void VisitCallExpr(const CallExpr *E); |
Chris Lattner | b2d963f | 2007-08-31 22:54:14 +0000 | [diff] [blame] | 81 | void VisitStmtExpr(const StmtExpr *E); |
Chris Lattner | 9c03356 | 2007-08-21 04:25:47 +0000 | [diff] [blame] | 82 | void VisitBinaryOperator(const BinaryOperator *BO); |
Chris Lattner | 03d6fb9 | 2007-08-21 04:43:17 +0000 | [diff] [blame] | 83 | void VisitBinAssign(const BinaryOperator *E); |
Nate Begeman | 796ef3d | 2008-01-31 05:38:29 +0000 | [diff] [blame] | 84 | void VisitOverloadExpr(const OverloadExpr *E); |
Chris Lattner | 9c03356 | 2007-08-21 04:25:47 +0000 | [diff] [blame] | 85 | |
| 86 | |
| 87 | void VisitConditionalOperator(const ConditionalOperator *CO); |
Devang Patel | 636c3d0 | 2007-10-26 17:44:44 +0000 | [diff] [blame] | 88 | void VisitInitListExpr(InitListExpr *E); |
Chris Lattner | f81557c | 2008-04-04 18:42:16 +0000 | [diff] [blame] | 89 | |
| 90 | void EmitInitializationToLValue(Expr *E, LValue Address); |
| 91 | void EmitNullInitializationToLValue(LValue Address, QualType T); |
Chris Lattner | 9c03356 | 2007-08-21 04:25:47 +0000 | [diff] [blame] | 92 | // case Expr::ChooseExprClass: |
Lauro Ramos Venancio | 305762c | 2008-02-18 22:44:02 +0000 | [diff] [blame] | 93 | |
Chris Lattner | 9c03356 | 2007-08-21 04:25:47 +0000 | [diff] [blame] | 94 | }; |
| 95 | } // end anonymous namespace. |
| 96 | |
Chris Lattner | ee755f9 | 2007-08-21 04:59:27 +0000 | [diff] [blame] | 97 | //===----------------------------------------------------------------------===// |
| 98 | // Utilities |
| 99 | //===----------------------------------------------------------------------===// |
Chris Lattner | 9c03356 | 2007-08-21 04:25:47 +0000 | [diff] [blame] | 100 | |
Lauro Ramos Venancio | 13e22cf | 2008-02-19 22:04:22 +0000 | [diff] [blame] | 101 | void AggExprEmitter::EmitAggregateClear(llvm::Value *DestPtr, QualType Ty) { |
Chris Lattner | 9b2dc28 | 2008-04-04 16:54:41 +0000 | [diff] [blame] | 102 | assert(!Ty->isAnyComplexType() && "Shouldn't happen for complex"); |
Lauro Ramos Venancio | 13e22cf | 2008-02-19 22:04:22 +0000 | [diff] [blame] | 103 | |
| 104 | // Aggregate assignment turns into llvm.memset. |
| 105 | const llvm::Type *BP = llvm::PointerType::getUnqual(llvm::Type::Int8Ty); |
| 106 | if (DestPtr->getType() != BP) |
| 107 | DestPtr = Builder.CreateBitCast(DestPtr, BP, "tmp"); |
| 108 | |
| 109 | // Get size and alignment info for this aggregate. |
Chris Lattner | 98be494 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 110 | std::pair<uint64_t, unsigned> TypeInfo = CGF.getContext().getTypeInfo(Ty); |
Lauro Ramos Venancio | 13e22cf | 2008-02-19 22:04:22 +0000 | [diff] [blame] | 111 | |
| 112 | // FIXME: Handle variable sized types. |
| 113 | const llvm::Type *IntPtr = llvm::IntegerType::get(CGF.LLVMPointerWidth); |
| 114 | |
| 115 | llvm::Value *MemSetOps[4] = { |
| 116 | DestPtr, |
| 117 | llvm::ConstantInt::getNullValue(llvm::Type::Int8Ty), |
| 118 | // TypeInfo.first describes size in bits. |
| 119 | llvm::ConstantInt::get(IntPtr, TypeInfo.first/8), |
| 120 | llvm::ConstantInt::get(llvm::Type::Int32Ty, TypeInfo.second/8) |
| 121 | }; |
| 122 | |
| 123 | Builder.CreateCall(CGF.CGM.getMemSetFn(), MemSetOps, MemSetOps+4); |
| 124 | } |
| 125 | |
Chris Lattner | bfc0c1a | 2007-08-26 23:13:56 +0000 | [diff] [blame] | 126 | void AggExprEmitter::EmitAggregateCopy(llvm::Value *DestPtr, |
| 127 | llvm::Value *SrcPtr, QualType Ty) { |
Chris Lattner | 9b2dc28 | 2008-04-04 16:54:41 +0000 | [diff] [blame] | 128 | assert(!Ty->isAnyComplexType() && "Shouldn't happen for complex"); |
Chris Lattner | bfc0c1a | 2007-08-26 23:13:56 +0000 | [diff] [blame] | 129 | |
| 130 | // Aggregate assignment turns into llvm.memcpy. |
Christopher Lamb | ddc23f3 | 2007-12-17 01:11:20 +0000 | [diff] [blame] | 131 | const llvm::Type *BP = llvm::PointerType::getUnqual(llvm::Type::Int8Ty); |
Chris Lattner | bfc0c1a | 2007-08-26 23:13:56 +0000 | [diff] [blame] | 132 | if (DestPtr->getType() != BP) |
| 133 | DestPtr = Builder.CreateBitCast(DestPtr, BP, "tmp"); |
| 134 | if (SrcPtr->getType() != BP) |
| 135 | SrcPtr = Builder.CreateBitCast(SrcPtr, BP, "tmp"); |
| 136 | |
| 137 | // Get size and alignment info for this aggregate. |
Chris Lattner | 98be494 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 138 | std::pair<uint64_t, unsigned> TypeInfo = CGF.getContext().getTypeInfo(Ty); |
Chris Lattner | bfc0c1a | 2007-08-26 23:13:56 +0000 | [diff] [blame] | 139 | |
| 140 | // FIXME: Handle variable sized types. |
| 141 | const llvm::Type *IntPtr = llvm::IntegerType::get(CGF.LLVMPointerWidth); |
| 142 | |
| 143 | llvm::Value *MemCpyOps[4] = { |
| 144 | DestPtr, SrcPtr, |
Devang Patel | 636c3d0 | 2007-10-26 17:44:44 +0000 | [diff] [blame] | 145 | // TypeInfo.first describes size in bits. |
| 146 | llvm::ConstantInt::get(IntPtr, TypeInfo.first/8), |
Lauro Ramos Venancio | 13e22cf | 2008-02-19 22:04:22 +0000 | [diff] [blame] | 147 | llvm::ConstantInt::get(llvm::Type::Int32Ty, TypeInfo.second/8) |
Chris Lattner | bfc0c1a | 2007-08-26 23:13:56 +0000 | [diff] [blame] | 148 | }; |
| 149 | |
| 150 | Builder.CreateCall(CGF.CGM.getMemCpyFn(), MemCpyOps, MemCpyOps+4); |
| 151 | } |
| 152 | |
| 153 | |
Chris Lattner | 883f6a7 | 2007-08-11 00:04:45 +0000 | [diff] [blame] | 154 | /// EmitAggLoadOfLValue - Given an expression with aggregate type that |
| 155 | /// represents a value lvalue, this method emits the address of the lvalue, |
| 156 | /// then loads the result into DestPtr. |
Chris Lattner | 9c03356 | 2007-08-21 04:25:47 +0000 | [diff] [blame] | 157 | void AggExprEmitter::EmitAggLoadOfLValue(const Expr *E) { |
| 158 | LValue LV = CGF.EmitLValue(E); |
Chris Lattner | 883f6a7 | 2007-08-11 00:04:45 +0000 | [diff] [blame] | 159 | assert(LV.isSimple() && "Can't have aggregate bitfield, vector, etc"); |
| 160 | llvm::Value *SrcPtr = LV.getAddress(); |
| 161 | |
| 162 | // If the result is ignored, don't copy from the value. |
| 163 | if (DestPtr == 0) |
| 164 | // FIXME: If the source is volatile, we must read from it. |
| 165 | return; |
| 166 | |
Chris Lattner | bfc0c1a | 2007-08-26 23:13:56 +0000 | [diff] [blame] | 167 | EmitAggregateCopy(DestPtr, SrcPtr, E->getType()); |
Chris Lattner | 883f6a7 | 2007-08-11 00:04:45 +0000 | [diff] [blame] | 168 | } |
| 169 | |
Chris Lattner | ee755f9 | 2007-08-21 04:59:27 +0000 | [diff] [blame] | 170 | //===----------------------------------------------------------------------===// |
| 171 | // Visitor Methods |
| 172 | //===----------------------------------------------------------------------===// |
| 173 | |
Anders Carlsson | e4707ff | 2008-01-14 06:28:57 +0000 | [diff] [blame] | 174 | void AggExprEmitter::VisitImplicitCastExpr(ImplicitCastExpr *E) |
| 175 | { |
| 176 | QualType STy = E->getSubExpr()->getType().getCanonicalType(); |
| 177 | QualType Ty = E->getType().getCanonicalType(); |
Eli Friedman | ff6e2b7 | 2008-02-11 01:09:17 +0000 | [diff] [blame] | 178 | |
| 179 | assert(CGF.getContext().typesAreCompatible( |
| 180 | STy.getUnqualifiedType(), Ty.getUnqualifiedType()) |
| 181 | && "Implicit cast types must be compatible"); |
Anders Carlsson | e4707ff | 2008-01-14 06:28:57 +0000 | [diff] [blame] | 182 | |
| 183 | Visit(E->getSubExpr()); |
| 184 | } |
| 185 | |
Anders Carlsson | 148fe67 | 2007-10-31 22:04:46 +0000 | [diff] [blame] | 186 | void AggExprEmitter::VisitCallExpr(const CallExpr *E) |
| 187 | { |
| 188 | RValue RV = CGF.EmitCallExpr(E); |
| 189 | assert(RV.isAggregate() && "Return value must be aggregate value!"); |
| 190 | |
| 191 | // If the result is ignored, don't copy from the value. |
| 192 | if (DestPtr == 0) |
| 193 | // FIXME: If the source is volatile, we must read from it. |
| 194 | return; |
| 195 | |
| 196 | EmitAggregateCopy(DestPtr, RV.getAggregateAddr(), E->getType()); |
| 197 | } |
| 198 | |
Nate Begeman | 796ef3d | 2008-01-31 05:38:29 +0000 | [diff] [blame] | 199 | void AggExprEmitter::VisitOverloadExpr(const OverloadExpr *E) |
| 200 | { |
| 201 | RValue RV = CGF.EmitCallExpr(E->getFn(), E->arg_begin(), |
| 202 | E->getNumArgs(CGF.getContext())); |
| 203 | assert(RV.isAggregate() && "Return value must be aggregate value!"); |
| 204 | |
| 205 | // If the result is ignored, don't copy from the value. |
| 206 | if (DestPtr == 0) |
| 207 | // FIXME: If the source is volatile, we must read from it. |
| 208 | return; |
| 209 | |
| 210 | EmitAggregateCopy(DestPtr, RV.getAggregateAddr(), E->getType()); |
| 211 | } |
| 212 | |
Chris Lattner | b2d963f | 2007-08-31 22:54:14 +0000 | [diff] [blame] | 213 | void AggExprEmitter::VisitStmtExpr(const StmtExpr *E) { |
| 214 | CGF.EmitCompoundStmt(*E->getSubStmt(), true, DestPtr, VolatileDest); |
| 215 | } |
| 216 | |
Chris Lattner | 9c03356 | 2007-08-21 04:25:47 +0000 | [diff] [blame] | 217 | void AggExprEmitter::VisitBinaryOperator(const BinaryOperator *E) { |
Chris Lattner | dc4d280 | 2007-12-02 01:49:16 +0000 | [diff] [blame] | 218 | CGF.WarnUnsupported(E, "aggregate binary expression"); |
Chris Lattner | ee755f9 | 2007-08-21 04:59:27 +0000 | [diff] [blame] | 219 | } |
| 220 | |
Chris Lattner | 03d6fb9 | 2007-08-21 04:43:17 +0000 | [diff] [blame] | 221 | void AggExprEmitter::VisitBinAssign(const BinaryOperator *E) { |
Eli Friedman | ff6e2b7 | 2008-02-11 01:09:17 +0000 | [diff] [blame] | 222 | // For an assignment to work, the value on the right has |
| 223 | // to be compatible with the value on the left. |
| 224 | assert(CGF.getContext().typesAreCompatible( |
| 225 | E->getLHS()->getType().getUnqualifiedType(), |
| 226 | E->getRHS()->getType().getUnqualifiedType()) |
| 227 | && "Invalid assignment"); |
Chris Lattner | 9c03356 | 2007-08-21 04:25:47 +0000 | [diff] [blame] | 228 | LValue LHS = CGF.EmitLValue(E->getLHS()); |
Chris Lattner | 883f6a7 | 2007-08-11 00:04:45 +0000 | [diff] [blame] | 229 | |
| 230 | // Codegen the RHS so that it stores directly into the LHS. |
Chris Lattner | 9c03356 | 2007-08-21 04:25:47 +0000 | [diff] [blame] | 231 | CGF.EmitAggExpr(E->getRHS(), LHS.getAddress(), false /*FIXME: VOLATILE LHS*/); |
Chris Lattner | 883f6a7 | 2007-08-11 00:04:45 +0000 | [diff] [blame] | 232 | |
Eli Friedman | ff6e2b7 | 2008-02-11 01:09:17 +0000 | [diff] [blame] | 233 | if (DestPtr == 0) |
| 234 | return; |
| 235 | |
Chris Lattner | 883f6a7 | 2007-08-11 00:04:45 +0000 | [diff] [blame] | 236 | // 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] | 237 | EmitAggregateCopy(DestPtr, LHS.getAddress(), E->getType()); |
Chris Lattner | 883f6a7 | 2007-08-11 00:04:45 +0000 | [diff] [blame] | 238 | } |
| 239 | |
Chris Lattner | 9c03356 | 2007-08-21 04:25:47 +0000 | [diff] [blame] | 240 | void AggExprEmitter::VisitConditionalOperator(const ConditionalOperator *E) { |
Chris Lattner | 883f6a7 | 2007-08-11 00:04:45 +0000 | [diff] [blame] | 241 | llvm::BasicBlock *LHSBlock = new llvm::BasicBlock("cond.?"); |
| 242 | llvm::BasicBlock *RHSBlock = new llvm::BasicBlock("cond.:"); |
| 243 | llvm::BasicBlock *ContBlock = new llvm::BasicBlock("cond.cont"); |
| 244 | |
Chris Lattner | 9c03356 | 2007-08-21 04:25:47 +0000 | [diff] [blame] | 245 | llvm::Value *Cond = CGF.EvaluateExprAsBool(E->getCond()); |
Chris Lattner | bfc0c1a | 2007-08-26 23:13:56 +0000 | [diff] [blame] | 246 | Builder.CreateCondBr(Cond, LHSBlock, RHSBlock); |
Chris Lattner | 883f6a7 | 2007-08-11 00:04:45 +0000 | [diff] [blame] | 247 | |
Chris Lattner | 9c03356 | 2007-08-21 04:25:47 +0000 | [diff] [blame] | 248 | CGF.EmitBlock(LHSBlock); |
Chris Lattner | 883f6a7 | 2007-08-11 00:04:45 +0000 | [diff] [blame] | 249 | |
| 250 | // Handle the GNU extension for missing LHS. |
| 251 | assert(E->getLHS() && "Must have LHS for aggregate value"); |
| 252 | |
Chris Lattner | c748f27 | 2007-08-21 05:02:10 +0000 | [diff] [blame] | 253 | Visit(E->getLHS()); |
Chris Lattner | bfc0c1a | 2007-08-26 23:13:56 +0000 | [diff] [blame] | 254 | Builder.CreateBr(ContBlock); |
| 255 | LHSBlock = Builder.GetInsertBlock(); |
Chris Lattner | 883f6a7 | 2007-08-11 00:04:45 +0000 | [diff] [blame] | 256 | |
Chris Lattner | 9c03356 | 2007-08-21 04:25:47 +0000 | [diff] [blame] | 257 | CGF.EmitBlock(RHSBlock); |
Chris Lattner | 883f6a7 | 2007-08-11 00:04:45 +0000 | [diff] [blame] | 258 | |
Chris Lattner | c748f27 | 2007-08-21 05:02:10 +0000 | [diff] [blame] | 259 | Visit(E->getRHS()); |
Chris Lattner | bfc0c1a | 2007-08-26 23:13:56 +0000 | [diff] [blame] | 260 | Builder.CreateBr(ContBlock); |
| 261 | RHSBlock = Builder.GetInsertBlock(); |
Chris Lattner | 883f6a7 | 2007-08-11 00:04:45 +0000 | [diff] [blame] | 262 | |
Chris Lattner | 9c03356 | 2007-08-21 04:25:47 +0000 | [diff] [blame] | 263 | CGF.EmitBlock(ContBlock); |
Chris Lattner | 883f6a7 | 2007-08-11 00:04:45 +0000 | [diff] [blame] | 264 | } |
Chris Lattner | ee755f9 | 2007-08-21 04:59:27 +0000 | [diff] [blame] | 265 | |
Lauro Ramos Venancio | 13e22cf | 2008-02-19 22:04:22 +0000 | [diff] [blame] | 266 | void AggExprEmitter::EmitNonConstInit(InitListExpr *E) { |
| 267 | |
| 268 | const llvm::PointerType *APType = |
| 269 | cast<llvm::PointerType>(DestPtr->getType()); |
| 270 | const llvm::Type *DestType = APType->getElementType(); |
Lauro Ramos Venancio | 305762c | 2008-02-18 22:44:02 +0000 | [diff] [blame] | 271 | |
| 272 | if (const llvm::ArrayType *AType = dyn_cast<llvm::ArrayType>(DestType)) { |
Lauro Ramos Venancio | 13e22cf | 2008-02-19 22:04:22 +0000 | [diff] [blame] | 273 | unsigned NumInitElements = E->getNumInits(); |
Lauro Ramos Venancio | 305762c | 2008-02-18 22:44:02 +0000 | [diff] [blame] | 274 | |
Lauro Ramos Venancio | 305762c | 2008-02-18 22:44:02 +0000 | [diff] [blame] | 275 | unsigned i; |
| 276 | for (i = 0; i != NumInitElements; ++i) { |
Chris Lattner | 36b6a0a | 2008-03-19 05:19:41 +0000 | [diff] [blame] | 277 | llvm::Value *NextVal = Builder.CreateStructGEP(DestPtr, i, ".array"); |
Lauro Ramos Venancio | 13e22cf | 2008-02-19 22:04:22 +0000 | [diff] [blame] | 278 | Expr *Init = E->getInit(i); |
| 279 | if (isa<InitListExpr>(Init)) |
| 280 | CGF.EmitAggExpr(Init, NextVal, VolatileDest); |
| 281 | else |
| 282 | Builder.CreateStore(CGF.EmitScalarExpr(Init), NextVal); |
Lauro Ramos Venancio | 305762c | 2008-02-18 22:44:02 +0000 | [diff] [blame] | 283 | } |
| 284 | |
| 285 | // Emit remaining default initializers |
| 286 | unsigned NumArrayElements = AType->getNumElements(); |
Lauro Ramos Venancio | 13e22cf | 2008-02-19 22:04:22 +0000 | [diff] [blame] | 287 | QualType QType = E->getInit(0)->getType(); |
| 288 | const llvm::Type *EType = AType->getElementType(); |
Lauro Ramos Venancio | 305762c | 2008-02-18 22:44:02 +0000 | [diff] [blame] | 289 | for (/*Do not initialize i*/; i < NumArrayElements; ++i) { |
Chris Lattner | 36b6a0a | 2008-03-19 05:19:41 +0000 | [diff] [blame] | 290 | llvm::Value *NextVal = Builder.CreateStructGEP(DestPtr, i, ".array"); |
Lauro Ramos Venancio | 13e22cf | 2008-02-19 22:04:22 +0000 | [diff] [blame] | 291 | if (EType->isFirstClassType()) |
| 292 | Builder.CreateStore(llvm::Constant::getNullValue(EType), NextVal); |
| 293 | else |
| 294 | EmitAggregateClear(NextVal, QType); |
Lauro Ramos Venancio | 305762c | 2008-02-18 22:44:02 +0000 | [diff] [blame] | 295 | } |
Lauro Ramos Venancio | 13e22cf | 2008-02-19 22:04:22 +0000 | [diff] [blame] | 296 | } else |
| 297 | assert(false && "Invalid initializer"); |
Lauro Ramos Venancio | 305762c | 2008-02-18 22:44:02 +0000 | [diff] [blame] | 298 | } |
| 299 | |
Chris Lattner | f81557c | 2008-04-04 18:42:16 +0000 | [diff] [blame] | 300 | void AggExprEmitter::EmitInitializationToLValue(Expr* E, LValue LV) { |
| 301 | // FIXME: Are initializers affected by volatile? |
| 302 | if (E->getType()->isComplexType()) { |
| 303 | CGF.EmitComplexExprIntoAddr(E, LV.getAddress(), false); |
Devang Patel | 636c3d0 | 2007-10-26 17:44:44 +0000 | [diff] [blame] | 304 | return; |
Chris Lattner | f81557c | 2008-04-04 18:42:16 +0000 | [diff] [blame] | 305 | } |
| 306 | RValue RV = CGF.EmitAnyExpr(E, LV.getAddress(), false); |
| 307 | if (CGF.hasAggregateLLVMType(E->getType())) |
| 308 | return; |
| 309 | CGF.EmitStoreThroughLValue(RV, LV, E->getType()); |
| 310 | } |
| 311 | |
| 312 | void AggExprEmitter::EmitNullInitializationToLValue(LValue LV, QualType T) { |
| 313 | if (!CGF.hasAggregateLLVMType(T)) { |
| 314 | // For non-aggregates, we can store zero |
| 315 | const llvm::Type *T = |
| 316 | cast<llvm::PointerType>(LV.getAddress()->getType())->getElementType(); |
| 317 | Builder.CreateStore(llvm::Constant::getNullValue(T), LV.getAddress()); |
Lauro Ramos Venancio | 145cd89 | 2008-02-19 19:27:31 +0000 | [diff] [blame] | 318 | } else { |
Chris Lattner | f81557c | 2008-04-04 18:42:16 +0000 | [diff] [blame] | 319 | // Otherwise, just memset the whole thing to zero. This is legal |
| 320 | // because in LLVM, all default initializers are guaranteed to have a |
| 321 | // bit pattern of all zeros. |
| 322 | // There's a potential optimization opportunity in combining |
| 323 | // memsets; that would be easy for arrays, but relatively |
| 324 | // difficult for structures with the current code. |
| 325 | llvm::Value *MemSet = CGF.CGM.getIntrinsic(llvm::Intrinsic::memset_i64); |
| 326 | uint64_t Size = CGF.getContext().getTypeSize(T); |
| 327 | |
| 328 | const llvm::Type *BP = llvm::PointerType::getUnqual(llvm::Type::Int8Ty); |
| 329 | llvm::Value* DestPtr = Builder.CreateBitCast(LV.getAddress(), BP, "tmp"); |
| 330 | |
| 331 | llvm::Value *MemSetOps[4] = { |
| 332 | DestPtr, llvm::ConstantInt::get(llvm::Type::Int8Ty, 0), |
| 333 | llvm::ConstantInt::get(llvm::Type::Int64Ty, Size/8), |
| 334 | llvm::ConstantInt::get(llvm::Type::Int32Ty, 0) |
| 335 | }; |
| 336 | |
| 337 | Builder.CreateCall(MemSet, MemSetOps, MemSetOps+4); |
| 338 | } |
| 339 | } |
| 340 | |
| 341 | |
| 342 | void AggExprEmitter::VisitInitListExpr(InitListExpr *E) { |
| 343 | if (E->isConstantExpr(CGF.getContext(), 0)) { |
| 344 | // FIXME: call into const expr emitter so that we can emit |
| 345 | // a memcpy instead of storing the individual members. |
| 346 | // This is purely for perf; both codepaths lead to equivalent |
| 347 | // (although not necessarily identical) code. |
| 348 | // It's worth noting that LLVM keeps on getting smarter, though, |
| 349 | // so it might not be worth bothering. |
| 350 | } |
| 351 | |
| 352 | // Handle initialization of an array. |
| 353 | if (E->getType()->isArrayType()) { |
| 354 | const llvm::PointerType *APType = |
| 355 | cast<llvm::PointerType>(DestPtr->getType()); |
| 356 | const llvm::ArrayType *AType = |
| 357 | cast<llvm::ArrayType>(APType->getElementType()); |
| 358 | |
| 359 | uint64_t NumInitElements = E->getNumInits(); |
| 360 | uint64_t NumArrayElements = AType->getNumElements(); |
| 361 | QualType ElementType = E->getType()->getAsArrayType()->getElementType(); |
| 362 | |
| 363 | for (uint64_t i = 0; i != NumArrayElements; ++i) { |
| 364 | llvm::Value *NextVal = Builder.CreateStructGEP(DestPtr, i, ".array"); |
| 365 | if (i < NumInitElements) |
| 366 | EmitInitializationToLValue(E->getInit(i), LValue::MakeAddr(NextVal)); |
| 367 | else |
| 368 | EmitNullInitializationToLValue(LValue::MakeAddr(NextVal), |
| 369 | ElementType); |
Lauro Ramos Venancio | 145cd89 | 2008-02-19 19:27:31 +0000 | [diff] [blame] | 370 | } |
Chris Lattner | f81557c | 2008-04-04 18:42:16 +0000 | [diff] [blame] | 371 | return; |
| 372 | } |
| 373 | |
| 374 | assert(E->getType()->isRecordType() && "Only support structs/unions here!"); |
| 375 | |
| 376 | // Do struct initialization; this code just sets each individual member |
| 377 | // to the approprate value. This makes bitfield support automatic; |
| 378 | // the disadvantage is that the generated code is more difficult for |
| 379 | // the optimizer, especially with bitfields. |
| 380 | unsigned NumInitElements = E->getNumInits(); |
| 381 | RecordDecl *SD = E->getType()->getAsRecordType()->getDecl(); |
| 382 | unsigned NumMembers = SD->getNumMembers() - SD->hasFlexibleArrayMember(); |
| 383 | unsigned CurInitVal = 0; |
| 384 | bool isUnion = E->getType()->isUnionType(); |
| 385 | |
| 386 | // Here we iterate over the fields; this makes it simpler to both |
| 387 | // default-initialize fields and skip over unnamed fields. |
| 388 | for (unsigned CurFieldNo = 0; CurFieldNo != NumMembers; ++CurFieldNo) { |
| 389 | if (CurInitVal >= NumInitElements) { |
| 390 | // No more initializers; we're done. |
| 391 | break; |
| 392 | } |
| 393 | |
| 394 | FieldDecl *CurField = SD->getMember(CurFieldNo); |
| 395 | if (CurField->getIdentifier() == 0) { |
| 396 | // Initializers can't initialize unnamed fields, e.g. "int : 20;" |
| 397 | continue; |
| 398 | } |
| 399 | LValue FieldLoc = CGF.EmitLValueForField(DestPtr, CurField, isUnion); |
| 400 | if (CurInitVal < NumInitElements) { |
| 401 | // Store the initializer into the field |
| 402 | // This will probably have to get a bit smarter when we support |
| 403 | // designators in initializers |
| 404 | EmitInitializationToLValue(E->getInit(CurInitVal++), FieldLoc); |
| 405 | } else { |
| 406 | // We're out of initalizers; default-initialize to null |
| 407 | EmitNullInitializationToLValue(FieldLoc, CurField->getType()); |
| 408 | } |
| 409 | |
| 410 | // Unions only initialize one field. |
| 411 | // (things can get weird with designators, but they aren't |
| 412 | // supported yet.) |
| 413 | if (E->getType()->isUnionType()) |
| 414 | break; |
Lauro Ramos Venancio | 145cd89 | 2008-02-19 19:27:31 +0000 | [diff] [blame] | 415 | } |
Devang Patel | 636c3d0 | 2007-10-26 17:44:44 +0000 | [diff] [blame] | 416 | } |
| 417 | |
Chris Lattner | ee755f9 | 2007-08-21 04:59:27 +0000 | [diff] [blame] | 418 | //===----------------------------------------------------------------------===// |
| 419 | // Entry Points into this File |
| 420 | //===----------------------------------------------------------------------===// |
| 421 | |
| 422 | /// EmitAggExpr - Emit the computation of the specified expression of |
| 423 | /// aggregate type. The result is computed into DestPtr. Note that if |
| 424 | /// DestPtr is null, the value of the aggregate expression is not needed. |
| 425 | void CodeGenFunction::EmitAggExpr(const Expr *E, llvm::Value *DestPtr, |
| 426 | bool VolatileDest) { |
| 427 | assert(E && hasAggregateLLVMType(E->getType()) && |
| 428 | "Invalid aggregate expression to emit"); |
| 429 | |
| 430 | AggExprEmitter(*this, DestPtr, VolatileDest).Visit(const_cast<Expr*>(E)); |
| 431 | } |