blob: 924a5cf46d8df41336a701b83ec44cdf8432a323 [file] [log] [blame]
Chris Lattner566b6ce2007-08-24 02:22:53 +00001//===--- CGExprAgg.cpp - Emit LLVM Code from Aggregate Expressions --------===//
Chris Lattneraf6f5282007-08-10 20:13:28 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattneraf6f5282007-08-10 20:13:28 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This contains code to emit Aggregate Expr nodes as LLVM code.
11//
12//===----------------------------------------------------------------------===//
13
14#include "CodeGenFunction.h"
Chris Lattner883f6a72007-08-11 00:04:45 +000015#include "CodeGenModule.h"
16#include "clang/AST/AST.h"
17#include "llvm/Constants.h"
18#include "llvm/Function.h"
Devang Patel636c3d02007-10-26 17:44:44 +000019#include "llvm/GlobalVariable.h"
Chris Lattner9c033562007-08-21 04:25:47 +000020#include "llvm/Support/Compiler.h"
Chris Lattnerf81557c2008-04-04 18:42:16 +000021#include "llvm/Intrinsics.h"
Chris Lattneraf6f5282007-08-10 20:13:28 +000022using namespace clang;
23using namespace CodeGen;
Chris Lattner883f6a72007-08-11 00:04:45 +000024
Chris Lattner9c033562007-08-21 04:25:47 +000025//===----------------------------------------------------------------------===//
26// Aggregate Expression Emitter
27//===----------------------------------------------------------------------===//
28
29namespace {
30class VISIBILITY_HIDDEN AggExprEmitter : public StmtVisitor<AggExprEmitter> {
31 CodeGenFunction &CGF;
Chris Lattner50b36742008-04-13 07:32:11 +000032 llvm::IRBuilder &Builder;
Chris Lattner9c033562007-08-21 04:25:47 +000033 llvm::Value *DestPtr;
34 bool VolatileDest;
35public:
36 AggExprEmitter(CodeGenFunction &cgf, llvm::Value *destPtr, bool volatileDest)
Chris Lattnerbfc0c1a2007-08-26 23:13:56 +000037 : CGF(cgf), Builder(CGF.Builder),
38 DestPtr(destPtr), VolatileDest(volatileDest) {
Chris Lattner9c033562007-08-21 04:25:47 +000039 }
40
Chris Lattneree755f92007-08-21 04:59:27 +000041 //===--------------------------------------------------------------------===//
42 // Utilities
43 //===--------------------------------------------------------------------===//
44
Chris Lattner9c033562007-08-21 04:25:47 +000045 /// 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 Lattnerbfc0c1a2007-08-26 23:13:56 +000050 void EmitAggregateCopy(llvm::Value *DestPtr, llvm::Value *SrcPtr,
51 QualType EltTy);
Lauro Ramos Venancio13e22cf2008-02-19 22:04:22 +000052
53 void EmitAggregateClear(llvm::Value *DestPtr, QualType Ty);
54
55 void EmitNonConstInit(InitListExpr *E);
Eli Friedman922696f2008-05-19 17:51:16 +000056
Chris Lattneree755f92007-08-21 04:59:27 +000057 //===--------------------------------------------------------------------===//
58 // Visitor Methods
59 //===--------------------------------------------------------------------===//
60
Chris Lattner9c033562007-08-21 04:25:47 +000061 void VisitStmt(Stmt *S) {
Chris Lattnerdc4d2802007-12-02 01:49:16 +000062 CGF.WarnUnsupported(S, "aggregate expression");
Chris Lattner9c033562007-08-21 04:25:47 +000063 }
64 void VisitParenExpr(ParenExpr *PE) { Visit(PE->getSubExpr()); }
65
66 // l-values.
Seo Sanghyeon9b73b392007-12-14 02:04:12 +000067 void VisitDeclRefExpr(DeclRefExpr *DRE) { EmitAggLoadOfLValue(DRE); }
68 void VisitMemberExpr(MemberExpr *ME) { EmitAggLoadOfLValue(ME); }
69 void VisitUnaryDeref(UnaryOperator *E) { EmitAggLoadOfLValue(E); }
Seo Sanghyeonad6ebd62007-12-23 03:11:58 +000070 void VisitStringLiteral(StringLiteral *E) { EmitAggLoadOfLValue(E); }
Eli Friedmanb1851242008-05-27 15:51:49 +000071 void VisitCompoundLiteralExpr(CompoundLiteralExpr *E)
72 { EmitAggLoadOfLValue(E); }
Seo Sanghyeon9b73b392007-12-14 02:04:12 +000073
74 void VisitArraySubscriptExpr(ArraySubscriptExpr *E) {
75 EmitAggLoadOfLValue(E);
76 }
Chris Lattner9c033562007-08-21 04:25:47 +000077
78 // Operators.
79 // case Expr::UnaryOperatorClass:
Chris Lattner9c033562007-08-21 04:25:47 +000080 // case Expr::CastExprClass:
Anders Carlssone4707ff2008-01-14 06:28:57 +000081 void VisitImplicitCastExpr(ImplicitCastExpr *E);
Anders Carlsson148fe672007-10-31 22:04:46 +000082 void VisitCallExpr(const CallExpr *E);
Chris Lattnerb2d963f2007-08-31 22:54:14 +000083 void VisitStmtExpr(const StmtExpr *E);
Chris Lattner9c033562007-08-21 04:25:47 +000084 void VisitBinaryOperator(const BinaryOperator *BO);
Chris Lattner03d6fb92007-08-21 04:43:17 +000085 void VisitBinAssign(const BinaryOperator *E);
Nate Begeman796ef3d2008-01-31 05:38:29 +000086 void VisitOverloadExpr(const OverloadExpr *E);
Eli Friedman07fa52a2008-05-20 07:56:31 +000087 void VisitBinComma(const BinaryOperator *E);
Chris Lattner9c033562007-08-21 04:25:47 +000088
89
90 void VisitConditionalOperator(const ConditionalOperator *CO);
Devang Patel636c3d02007-10-26 17:44:44 +000091 void VisitInitListExpr(InitListExpr *E);
Chris Lattner04421082008-04-08 04:40:51 +000092 void VisitCXXDefaultArgExpr(CXXDefaultArgExpr *DAE) {
93 Visit(DAE->getExpr());
94 }
Eli Friedmanb1851242008-05-27 15:51:49 +000095 void VisitVAArgExpr(VAArgExpr *E);
Chris Lattnerf81557c2008-04-04 18:42:16 +000096
97 void EmitInitializationToLValue(Expr *E, LValue Address);
98 void EmitNullInitializationToLValue(LValue Address, QualType T);
Chris Lattner9c033562007-08-21 04:25:47 +000099 // case Expr::ChooseExprClass:
Lauro Ramos Venancio305762c2008-02-18 22:44:02 +0000100
Chris Lattner9c033562007-08-21 04:25:47 +0000101};
102} // end anonymous namespace.
103
Chris Lattneree755f92007-08-21 04:59:27 +0000104//===----------------------------------------------------------------------===//
105// Utilities
106//===----------------------------------------------------------------------===//
Chris Lattner9c033562007-08-21 04:25:47 +0000107
Lauro Ramos Venancio13e22cf2008-02-19 22:04:22 +0000108void AggExprEmitter::EmitAggregateClear(llvm::Value *DestPtr, QualType Ty) {
Chris Lattner9b2dc282008-04-04 16:54:41 +0000109 assert(!Ty->isAnyComplexType() && "Shouldn't happen for complex");
Lauro Ramos Venancio13e22cf2008-02-19 22:04:22 +0000110
111 // Aggregate assignment turns into llvm.memset.
112 const llvm::Type *BP = llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
113 if (DestPtr->getType() != BP)
114 DestPtr = Builder.CreateBitCast(DestPtr, BP, "tmp");
115
116 // Get size and alignment info for this aggregate.
Chris Lattner98be4942008-03-05 18:54:05 +0000117 std::pair<uint64_t, unsigned> TypeInfo = CGF.getContext().getTypeInfo(Ty);
Lauro Ramos Venancio13e22cf2008-02-19 22:04:22 +0000118
119 // FIXME: Handle variable sized types.
120 const llvm::Type *IntPtr = llvm::IntegerType::get(CGF.LLVMPointerWidth);
121
122 llvm::Value *MemSetOps[4] = {
123 DestPtr,
124 llvm::ConstantInt::getNullValue(llvm::Type::Int8Ty),
125 // TypeInfo.first describes size in bits.
126 llvm::ConstantInt::get(IntPtr, TypeInfo.first/8),
127 llvm::ConstantInt::get(llvm::Type::Int32Ty, TypeInfo.second/8)
128 };
129
130 Builder.CreateCall(CGF.CGM.getMemSetFn(), MemSetOps, MemSetOps+4);
131}
132
Chris Lattnerbfc0c1a2007-08-26 23:13:56 +0000133void AggExprEmitter::EmitAggregateCopy(llvm::Value *DestPtr,
134 llvm::Value *SrcPtr, QualType Ty) {
Chris Lattner9b2dc282008-04-04 16:54:41 +0000135 assert(!Ty->isAnyComplexType() && "Shouldn't happen for complex");
Chris Lattnerbfc0c1a2007-08-26 23:13:56 +0000136
Eli Friedman0c995092008-05-26 12:59:39 +0000137 // Aggregate assignment turns into llvm.memmove.
Christopher Lambddc23f32007-12-17 01:11:20 +0000138 const llvm::Type *BP = llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
Chris Lattnerbfc0c1a2007-08-26 23:13:56 +0000139 if (DestPtr->getType() != BP)
140 DestPtr = Builder.CreateBitCast(DestPtr, BP, "tmp");
141 if (SrcPtr->getType() != BP)
142 SrcPtr = Builder.CreateBitCast(SrcPtr, BP, "tmp");
143
144 // Get size and alignment info for this aggregate.
Chris Lattner98be4942008-03-05 18:54:05 +0000145 std::pair<uint64_t, unsigned> TypeInfo = CGF.getContext().getTypeInfo(Ty);
Chris Lattnerbfc0c1a2007-08-26 23:13:56 +0000146
147 // FIXME: Handle variable sized types.
148 const llvm::Type *IntPtr = llvm::IntegerType::get(CGF.LLVMPointerWidth);
149
Eli Friedman0c995092008-05-26 12:59:39 +0000150 llvm::Value *MemMoveOps[4] = {
Chris Lattnerbfc0c1a2007-08-26 23:13:56 +0000151 DestPtr, SrcPtr,
Devang Patel636c3d02007-10-26 17:44:44 +0000152 // TypeInfo.first describes size in bits.
153 llvm::ConstantInt::get(IntPtr, TypeInfo.first/8),
Lauro Ramos Venancio13e22cf2008-02-19 22:04:22 +0000154 llvm::ConstantInt::get(llvm::Type::Int32Ty, TypeInfo.second/8)
Chris Lattnerbfc0c1a2007-08-26 23:13:56 +0000155 };
156
Eli Friedman0c995092008-05-26 12:59:39 +0000157 Builder.CreateCall(CGF.CGM.getMemMoveFn(), MemMoveOps, MemMoveOps+4);
Chris Lattnerbfc0c1a2007-08-26 23:13:56 +0000158}
159
160
Chris Lattner883f6a72007-08-11 00:04:45 +0000161/// EmitAggLoadOfLValue - Given an expression with aggregate type that
162/// represents a value lvalue, this method emits the address of the lvalue,
163/// then loads the result into DestPtr.
Chris Lattner9c033562007-08-21 04:25:47 +0000164void AggExprEmitter::EmitAggLoadOfLValue(const Expr *E) {
165 LValue LV = CGF.EmitLValue(E);
Chris Lattner883f6a72007-08-11 00:04:45 +0000166 assert(LV.isSimple() && "Can't have aggregate bitfield, vector, etc");
167 llvm::Value *SrcPtr = LV.getAddress();
168
169 // If the result is ignored, don't copy from the value.
170 if (DestPtr == 0)
171 // FIXME: If the source is volatile, we must read from it.
172 return;
173
Chris Lattnerbfc0c1a2007-08-26 23:13:56 +0000174 EmitAggregateCopy(DestPtr, SrcPtr, E->getType());
Chris Lattner883f6a72007-08-11 00:04:45 +0000175}
176
Chris Lattneree755f92007-08-21 04:59:27 +0000177//===----------------------------------------------------------------------===//
178// Visitor Methods
179//===----------------------------------------------------------------------===//
180
Anders Carlssone4707ff2008-01-14 06:28:57 +0000181void AggExprEmitter::VisitImplicitCastExpr(ImplicitCastExpr *E)
182{
183 QualType STy = E->getSubExpr()->getType().getCanonicalType();
184 QualType Ty = E->getType().getCanonicalType();
Eli Friedmanff6e2b72008-02-11 01:09:17 +0000185
186 assert(CGF.getContext().typesAreCompatible(
187 STy.getUnqualifiedType(), Ty.getUnqualifiedType())
188 && "Implicit cast types must be compatible");
Anders Carlssone4707ff2008-01-14 06:28:57 +0000189
190 Visit(E->getSubExpr());
191}
192
Anders Carlsson148fe672007-10-31 22:04:46 +0000193void AggExprEmitter::VisitCallExpr(const CallExpr *E)
194{
195 RValue RV = CGF.EmitCallExpr(E);
196 assert(RV.isAggregate() && "Return value must be aggregate value!");
197
198 // If the result is ignored, don't copy from the value.
199 if (DestPtr == 0)
200 // FIXME: If the source is volatile, we must read from it.
201 return;
202
203 EmitAggregateCopy(DestPtr, RV.getAggregateAddr(), E->getType());
204}
205
Nate Begeman796ef3d2008-01-31 05:38:29 +0000206void AggExprEmitter::VisitOverloadExpr(const OverloadExpr *E)
207{
208 RValue RV = CGF.EmitCallExpr(E->getFn(), E->arg_begin(),
209 E->getNumArgs(CGF.getContext()));
210 assert(RV.isAggregate() && "Return value must be aggregate value!");
211
212 // If the result is ignored, don't copy from the value.
213 if (DestPtr == 0)
214 // FIXME: If the source is volatile, we must read from it.
215 return;
216
217 EmitAggregateCopy(DestPtr, RV.getAggregateAddr(), E->getType());
218}
219
Eli Friedman07fa52a2008-05-20 07:56:31 +0000220void AggExprEmitter::VisitBinComma(const BinaryOperator *E)
221{
222 CGF.EmitAnyExpr(E->getLHS());
223 CGF.EmitAggExpr(E->getRHS(), DestPtr, false);
224}
225
Chris Lattnerb2d963f2007-08-31 22:54:14 +0000226void AggExprEmitter::VisitStmtExpr(const StmtExpr *E) {
227 CGF.EmitCompoundStmt(*E->getSubStmt(), true, DestPtr, VolatileDest);
228}
229
Chris Lattner9c033562007-08-21 04:25:47 +0000230void AggExprEmitter::VisitBinaryOperator(const BinaryOperator *E) {
Chris Lattnerdc4d2802007-12-02 01:49:16 +0000231 CGF.WarnUnsupported(E, "aggregate binary expression");
Chris Lattneree755f92007-08-21 04:59:27 +0000232}
233
Chris Lattner03d6fb92007-08-21 04:43:17 +0000234void AggExprEmitter::VisitBinAssign(const BinaryOperator *E) {
Eli Friedmanff6e2b72008-02-11 01:09:17 +0000235 // For an assignment to work, the value on the right has
236 // to be compatible with the value on the left.
237 assert(CGF.getContext().typesAreCompatible(
238 E->getLHS()->getType().getUnqualifiedType(),
239 E->getRHS()->getType().getUnqualifiedType())
240 && "Invalid assignment");
Chris Lattner9c033562007-08-21 04:25:47 +0000241 LValue LHS = CGF.EmitLValue(E->getLHS());
Chris Lattner883f6a72007-08-11 00:04:45 +0000242
243 // Codegen the RHS so that it stores directly into the LHS.
Chris Lattner9c033562007-08-21 04:25:47 +0000244 CGF.EmitAggExpr(E->getRHS(), LHS.getAddress(), false /*FIXME: VOLATILE LHS*/);
Chris Lattner883f6a72007-08-11 00:04:45 +0000245
Eli Friedmanff6e2b72008-02-11 01:09:17 +0000246 if (DestPtr == 0)
247 return;
248
Chris Lattner883f6a72007-08-11 00:04:45 +0000249 // If the result of the assignment is used, copy the RHS there also.
Eli Friedmanff6e2b72008-02-11 01:09:17 +0000250 EmitAggregateCopy(DestPtr, LHS.getAddress(), E->getType());
Chris Lattner883f6a72007-08-11 00:04:45 +0000251}
252
Chris Lattner9c033562007-08-21 04:25:47 +0000253void AggExprEmitter::VisitConditionalOperator(const ConditionalOperator *E) {
Gabor Greif984d0b42008-04-06 20:42:52 +0000254 llvm::BasicBlock *LHSBlock = llvm::BasicBlock::Create("cond.?");
255 llvm::BasicBlock *RHSBlock = llvm::BasicBlock::Create("cond.:");
256 llvm::BasicBlock *ContBlock = llvm::BasicBlock::Create("cond.cont");
Chris Lattner883f6a72007-08-11 00:04:45 +0000257
Chris Lattner9c033562007-08-21 04:25:47 +0000258 llvm::Value *Cond = CGF.EvaluateExprAsBool(E->getCond());
Chris Lattnerbfc0c1a2007-08-26 23:13:56 +0000259 Builder.CreateCondBr(Cond, LHSBlock, RHSBlock);
Chris Lattner883f6a72007-08-11 00:04:45 +0000260
Chris Lattner9c033562007-08-21 04:25:47 +0000261 CGF.EmitBlock(LHSBlock);
Chris Lattner883f6a72007-08-11 00:04:45 +0000262
263 // Handle the GNU extension for missing LHS.
264 assert(E->getLHS() && "Must have LHS for aggregate value");
265
Chris Lattnerc748f272007-08-21 05:02:10 +0000266 Visit(E->getLHS());
Chris Lattnerbfc0c1a2007-08-26 23:13:56 +0000267 Builder.CreateBr(ContBlock);
268 LHSBlock = Builder.GetInsertBlock();
Chris Lattner883f6a72007-08-11 00:04:45 +0000269
Chris Lattner9c033562007-08-21 04:25:47 +0000270 CGF.EmitBlock(RHSBlock);
Chris Lattner883f6a72007-08-11 00:04:45 +0000271
Chris Lattnerc748f272007-08-21 05:02:10 +0000272 Visit(E->getRHS());
Chris Lattnerbfc0c1a2007-08-26 23:13:56 +0000273 Builder.CreateBr(ContBlock);
274 RHSBlock = Builder.GetInsertBlock();
Chris Lattner883f6a72007-08-11 00:04:45 +0000275
Chris Lattner9c033562007-08-21 04:25:47 +0000276 CGF.EmitBlock(ContBlock);
Chris Lattner883f6a72007-08-11 00:04:45 +0000277}
Chris Lattneree755f92007-08-21 04:59:27 +0000278
Eli Friedmanb1851242008-05-27 15:51:49 +0000279void AggExprEmitter::VisitVAArgExpr(VAArgExpr *VE) {
280 llvm::Value *ArgValue = CGF.EmitLValue(VE->getSubExpr()).getAddress();
281 llvm::Value *V = Builder.CreateVAArg(ArgValue, CGF.ConvertType(VE->getType()));
282 if (DestPtr)
Eli Friedman1e692ac2008-06-13 23:01:12 +0000283 // FIXME: volatility
Eli Friedmanb1851242008-05-27 15:51:49 +0000284 Builder.CreateStore(V, DestPtr);
285}
286
Lauro Ramos Venancio13e22cf2008-02-19 22:04:22 +0000287void AggExprEmitter::EmitNonConstInit(InitListExpr *E) {
288
289 const llvm::PointerType *APType =
290 cast<llvm::PointerType>(DestPtr->getType());
291 const llvm::Type *DestType = APType->getElementType();
Lauro Ramos Venancio305762c2008-02-18 22:44:02 +0000292
293 if (const llvm::ArrayType *AType = dyn_cast<llvm::ArrayType>(DestType)) {
Lauro Ramos Venancio13e22cf2008-02-19 22:04:22 +0000294 unsigned NumInitElements = E->getNumInits();
Lauro Ramos Venancio305762c2008-02-18 22:44:02 +0000295
Lauro Ramos Venancio305762c2008-02-18 22:44:02 +0000296 unsigned i;
297 for (i = 0; i != NumInitElements; ++i) {
Chris Lattner36b6a0a2008-03-19 05:19:41 +0000298 llvm::Value *NextVal = Builder.CreateStructGEP(DestPtr, i, ".array");
Lauro Ramos Venancio13e22cf2008-02-19 22:04:22 +0000299 Expr *Init = E->getInit(i);
300 if (isa<InitListExpr>(Init))
301 CGF.EmitAggExpr(Init, NextVal, VolatileDest);
302 else
Eli Friedman1e692ac2008-06-13 23:01:12 +0000303 // FIXME: volatility
Lauro Ramos Venancio13e22cf2008-02-19 22:04:22 +0000304 Builder.CreateStore(CGF.EmitScalarExpr(Init), NextVal);
Lauro Ramos Venancio305762c2008-02-18 22:44:02 +0000305 }
306
307 // Emit remaining default initializers
308 unsigned NumArrayElements = AType->getNumElements();
Lauro Ramos Venancio13e22cf2008-02-19 22:04:22 +0000309 QualType QType = E->getInit(0)->getType();
310 const llvm::Type *EType = AType->getElementType();
Lauro Ramos Venancio305762c2008-02-18 22:44:02 +0000311 for (/*Do not initialize i*/; i < NumArrayElements; ++i) {
Chris Lattner36b6a0a2008-03-19 05:19:41 +0000312 llvm::Value *NextVal = Builder.CreateStructGEP(DestPtr, i, ".array");
Dan Gohmand79a7262008-05-22 22:12:56 +0000313 if (EType->isSingleValueType())
Eli Friedman1e692ac2008-06-13 23:01:12 +0000314 // FIXME: volatility
Lauro Ramos Venancio13e22cf2008-02-19 22:04:22 +0000315 Builder.CreateStore(llvm::Constant::getNullValue(EType), NextVal);
316 else
317 EmitAggregateClear(NextVal, QType);
Lauro Ramos Venancio305762c2008-02-18 22:44:02 +0000318 }
Lauro Ramos Venancio13e22cf2008-02-19 22:04:22 +0000319 } else
320 assert(false && "Invalid initializer");
Lauro Ramos Venancio305762c2008-02-18 22:44:02 +0000321}
322
Chris Lattnerf81557c2008-04-04 18:42:16 +0000323void AggExprEmitter::EmitInitializationToLValue(Expr* E, LValue LV) {
324 // FIXME: Are initializers affected by volatile?
325 if (E->getType()->isComplexType()) {
326 CGF.EmitComplexExprIntoAddr(E, LV.getAddress(), false);
Eli Friedmanc8ba9612008-05-12 15:06:05 +0000327 } else if (CGF.hasAggregateLLVMType(E->getType())) {
328 CGF.EmitAnyExpr(E, LV.getAddress(), false);
329 } else {
330 CGF.EmitStoreThroughLValue(CGF.EmitAnyExpr(E), LV, E->getType());
Chris Lattnerf81557c2008-04-04 18:42:16 +0000331 }
Chris Lattnerf81557c2008-04-04 18:42:16 +0000332}
333
334void AggExprEmitter::EmitNullInitializationToLValue(LValue LV, QualType T) {
335 if (!CGF.hasAggregateLLVMType(T)) {
336 // For non-aggregates, we can store zero
337 const llvm::Type *T =
338 cast<llvm::PointerType>(LV.getAddress()->getType())->getElementType();
Eli Friedman1e692ac2008-06-13 23:01:12 +0000339 // FIXME: volatility
Chris Lattnerf81557c2008-04-04 18:42:16 +0000340 Builder.CreateStore(llvm::Constant::getNullValue(T), LV.getAddress());
Lauro Ramos Venancio145cd892008-02-19 19:27:31 +0000341 } else {
Chris Lattnerf81557c2008-04-04 18:42:16 +0000342 // Otherwise, just memset the whole thing to zero. This is legal
343 // because in LLVM, all default initializers are guaranteed to have a
344 // bit pattern of all zeros.
345 // There's a potential optimization opportunity in combining
346 // memsets; that would be easy for arrays, but relatively
347 // difficult for structures with the current code.
348 llvm::Value *MemSet = CGF.CGM.getIntrinsic(llvm::Intrinsic::memset_i64);
349 uint64_t Size = CGF.getContext().getTypeSize(T);
350
351 const llvm::Type *BP = llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
352 llvm::Value* DestPtr = Builder.CreateBitCast(LV.getAddress(), BP, "tmp");
Chris Lattner3eae03e2008-05-06 00:56:42 +0000353 Builder.CreateCall4(MemSet, DestPtr,
354 llvm::ConstantInt::get(llvm::Type::Int8Ty, 0),
355 llvm::ConstantInt::get(llvm::Type::Int64Ty, Size/8),
356 llvm::ConstantInt::get(llvm::Type::Int32Ty, 0));
Chris Lattnerf81557c2008-04-04 18:42:16 +0000357 }
358}
359
Chris Lattnerf81557c2008-04-04 18:42:16 +0000360void AggExprEmitter::VisitInitListExpr(InitListExpr *E) {
361 if (E->isConstantExpr(CGF.getContext(), 0)) {
362 // FIXME: call into const expr emitter so that we can emit
363 // a memcpy instead of storing the individual members.
364 // This is purely for perf; both codepaths lead to equivalent
365 // (although not necessarily identical) code.
366 // It's worth noting that LLVM keeps on getting smarter, though,
367 // so it might not be worth bothering.
368 }
369
370 // Handle initialization of an array.
371 if (E->getType()->isArrayType()) {
372 const llvm::PointerType *APType =
373 cast<llvm::PointerType>(DestPtr->getType());
374 const llvm::ArrayType *AType =
375 cast<llvm::ArrayType>(APType->getElementType());
376
377 uint64_t NumInitElements = E->getNumInits();
Eli Friedman922696f2008-05-19 17:51:16 +0000378
379 if (E->getNumInits() > 0 &&
380 E->getType().getCanonicalType().getUnqualifiedType() ==
381 E->getInit(0)->getType().getCanonicalType().getUnqualifiedType()) {
382 EmitAggLoadOfLValue(E->getInit(0));
383 return;
384 }
385
Chris Lattnerf81557c2008-04-04 18:42:16 +0000386 uint64_t NumArrayElements = AType->getNumElements();
387 QualType ElementType = E->getType()->getAsArrayType()->getElementType();
388
Eli Friedman1e692ac2008-06-13 23:01:12 +0000389 unsigned CVRqualifier = E->getType().getCanonicalType()->getAsArrayType()
390 ->getElementType().getCVRQualifiers();
391
Chris Lattnerf81557c2008-04-04 18:42:16 +0000392 for (uint64_t i = 0; i != NumArrayElements; ++i) {
393 llvm::Value *NextVal = Builder.CreateStructGEP(DestPtr, i, ".array");
394 if (i < NumInitElements)
Eli Friedman1e692ac2008-06-13 23:01:12 +0000395 EmitInitializationToLValue(E->getInit(i),
396 LValue::MakeAddr(NextVal, CVRqualifier));
Chris Lattnerf81557c2008-04-04 18:42:16 +0000397 else
Eli Friedman1e692ac2008-06-13 23:01:12 +0000398 EmitNullInitializationToLValue(LValue::MakeAddr(NextVal, CVRqualifier),
Chris Lattnerf81557c2008-04-04 18:42:16 +0000399 ElementType);
Lauro Ramos Venancio145cd892008-02-19 19:27:31 +0000400 }
Chris Lattnerf81557c2008-04-04 18:42:16 +0000401 return;
402 }
403
404 assert(E->getType()->isRecordType() && "Only support structs/unions here!");
405
406 // Do struct initialization; this code just sets each individual member
407 // to the approprate value. This makes bitfield support automatic;
408 // the disadvantage is that the generated code is more difficult for
409 // the optimizer, especially with bitfields.
410 unsigned NumInitElements = E->getNumInits();
411 RecordDecl *SD = E->getType()->getAsRecordType()->getDecl();
412 unsigned NumMembers = SD->getNumMembers() - SD->hasFlexibleArrayMember();
413 unsigned CurInitVal = 0;
414 bool isUnion = E->getType()->isUnionType();
415
416 // Here we iterate over the fields; this makes it simpler to both
417 // default-initialize fields and skip over unnamed fields.
418 for (unsigned CurFieldNo = 0; CurFieldNo != NumMembers; ++CurFieldNo) {
419 if (CurInitVal >= NumInitElements) {
420 // No more initializers; we're done.
421 break;
422 }
423
424 FieldDecl *CurField = SD->getMember(CurFieldNo);
425 if (CurField->getIdentifier() == 0) {
426 // Initializers can't initialize unnamed fields, e.g. "int : 20;"
427 continue;
428 }
Eli Friedman1e692ac2008-06-13 23:01:12 +0000429 // FIXME: volatility
430 LValue FieldLoc = CGF.EmitLValueForField(DestPtr, CurField, isUnion,0);
Chris Lattnerf81557c2008-04-04 18:42:16 +0000431 if (CurInitVal < NumInitElements) {
432 // Store the initializer into the field
433 // This will probably have to get a bit smarter when we support
434 // designators in initializers
435 EmitInitializationToLValue(E->getInit(CurInitVal++), FieldLoc);
436 } else {
437 // We're out of initalizers; default-initialize to null
438 EmitNullInitializationToLValue(FieldLoc, CurField->getType());
439 }
440
441 // Unions only initialize one field.
442 // (things can get weird with designators, but they aren't
443 // supported yet.)
444 if (E->getType()->isUnionType())
445 break;
Lauro Ramos Venancio145cd892008-02-19 19:27:31 +0000446 }
Devang Patel636c3d02007-10-26 17:44:44 +0000447}
448
Chris Lattneree755f92007-08-21 04:59:27 +0000449//===----------------------------------------------------------------------===//
450// Entry Points into this File
451//===----------------------------------------------------------------------===//
452
453/// EmitAggExpr - Emit the computation of the specified expression of
454/// aggregate type. The result is computed into DestPtr. Note that if
455/// DestPtr is null, the value of the aggregate expression is not needed.
456void CodeGenFunction::EmitAggExpr(const Expr *E, llvm::Value *DestPtr,
457 bool VolatileDest) {
458 assert(E && hasAggregateLLVMType(E->getType()) &&
459 "Invalid aggregate expression to emit");
460
461 AggExprEmitter(*this, DestPtr, VolatileDest).Visit(const_cast<Expr*>(E));
462}