blob: b9e850a473dcc1267c315f4175149cb9f8a2f46e [file] [log] [blame]
Chris Lattner820dce82007-08-24 02:22:53 +00001//===--- CGExprAgg.cpp - Emit LLVM Code from Aggregate Expressions --------===//
Chris Lattner78ed8402007-08-10 20:13:28 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner959e5be2007-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 Lattner78ed8402007-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 Lattnerbdb8ffb2007-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 Patelddde6d52007-10-26 17:44:44 +000019#include "llvm/GlobalVariable.h"
Chris Lattnerb50e3902007-08-21 04:25:47 +000020#include "llvm/Support/Compiler.h"
Chris Lattner6ee6ab02008-04-04 18:42:16 +000021#include "llvm/Intrinsics.h"
Chris Lattner78ed8402007-08-10 20:13:28 +000022using namespace clang;
23using namespace CodeGen;
Chris Lattnerbdb8ffb2007-08-11 00:04:45 +000024
Chris Lattnerb50e3902007-08-21 04:25:47 +000025//===----------------------------------------------------------------------===//
26// Aggregate Expression Emitter
27//===----------------------------------------------------------------------===//
28
29namespace {
30class VISIBILITY_HIDDEN AggExprEmitter : public StmtVisitor<AggExprEmitter> {
31 CodeGenFunction &CGF;
Chris Lattner676bf212008-04-13 07:32:11 +000032 llvm::IRBuilder &Builder;
Chris Lattnerb50e3902007-08-21 04:25:47 +000033 llvm::Value *DestPtr;
34 bool VolatileDest;
35public:
36 AggExprEmitter(CodeGenFunction &cgf, llvm::Value *destPtr, bool volatileDest)
Chris Lattner41b1fca2007-08-26 23:13:56 +000037 : CGF(cgf), Builder(CGF.Builder),
38 DestPtr(destPtr), VolatileDest(volatileDest) {
Chris Lattnerb50e3902007-08-21 04:25:47 +000039 }
40
Chris Lattnera7300102007-08-21 04:59:27 +000041 //===--------------------------------------------------------------------===//
42 // Utilities
43 //===--------------------------------------------------------------------===//
44
Chris Lattnerb50e3902007-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 Lattner41b1fca2007-08-26 23:13:56 +000050 void EmitAggregateCopy(llvm::Value *DestPtr, llvm::Value *SrcPtr,
51 QualType EltTy);
Lauro Ramos Venancio97b09572008-02-19 22:04:22 +000052
53 void EmitAggregateClear(llvm::Value *DestPtr, QualType Ty);
54
55 void EmitNonConstInit(InitListExpr *E);
Eli Friedman48ec5622008-05-19 17:51:16 +000056
Chris Lattnera7300102007-08-21 04:59:27 +000057 //===--------------------------------------------------------------------===//
58 // Visitor Methods
59 //===--------------------------------------------------------------------===//
60
Chris Lattnerb50e3902007-08-21 04:25:47 +000061 void VisitStmt(Stmt *S) {
Chris Lattnere8f49632007-12-02 01:49:16 +000062 CGF.WarnUnsupported(S, "aggregate expression");
Chris Lattnerb50e3902007-08-21 04:25:47 +000063 }
64 void VisitParenExpr(ParenExpr *PE) { Visit(PE->getSubExpr()); }
65
66 // l-values.
Seo Sanghyeon32666c52007-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 Sanghyeon3c2d5fb2007-12-23 03:11:58 +000070 void VisitStringLiteral(StringLiteral *E) { EmitAggLoadOfLValue(E); }
Eli Friedman42af3972008-05-27 15:51:49 +000071 void VisitCompoundLiteralExpr(CompoundLiteralExpr *E)
72 { EmitAggLoadOfLValue(E); }
Seo Sanghyeon32666c52007-12-14 02:04:12 +000073
74 void VisitArraySubscriptExpr(ArraySubscriptExpr *E) {
75 EmitAggLoadOfLValue(E);
76 }
Chris Lattnerb50e3902007-08-21 04:25:47 +000077
78 // Operators.
79 // case Expr::UnaryOperatorClass:
Chris Lattnerb50e3902007-08-21 04:25:47 +000080 // case Expr::CastExprClass:
Anders Carlssonf2712ac2008-01-14 06:28:57 +000081 void VisitImplicitCastExpr(ImplicitCastExpr *E);
Anders Carlsson0ae15412007-10-31 22:04:46 +000082 void VisitCallExpr(const CallExpr *E);
Chris Lattnerbab0bfb2007-08-31 22:54:14 +000083 void VisitStmtExpr(const StmtExpr *E);
Chris Lattnerb50e3902007-08-21 04:25:47 +000084 void VisitBinaryOperator(const BinaryOperator *BO);
Chris Lattner76cb1892007-08-21 04:43:17 +000085 void VisitBinAssign(const BinaryOperator *E);
Nate Begemanc6078c92008-01-31 05:38:29 +000086 void VisitOverloadExpr(const OverloadExpr *E);
Eli Friedman91fc7802008-05-20 07:56:31 +000087 void VisitBinComma(const BinaryOperator *E);
Chris Lattnerb50e3902007-08-21 04:25:47 +000088
Chris Lattner6ee20e32008-06-24 17:04:18 +000089 void VisitObjCMessageExpr(ObjCMessageExpr *E);
90
Chris Lattnerb50e3902007-08-21 04:25:47 +000091
92 void VisitConditionalOperator(const ConditionalOperator *CO);
Devang Patelddde6d52007-10-26 17:44:44 +000093 void VisitInitListExpr(InitListExpr *E);
Chris Lattner3e254fb2008-04-08 04:40:51 +000094 void VisitCXXDefaultArgExpr(CXXDefaultArgExpr *DAE) {
95 Visit(DAE->getExpr());
96 }
Eli Friedman42af3972008-05-27 15:51:49 +000097 void VisitVAArgExpr(VAArgExpr *E);
Chris Lattner6ee6ab02008-04-04 18:42:16 +000098
99 void EmitInitializationToLValue(Expr *E, LValue Address);
100 void EmitNullInitializationToLValue(LValue Address, QualType T);
Chris Lattnerb50e3902007-08-21 04:25:47 +0000101 // case Expr::ChooseExprClass:
Lauro Ramos Venancio9eb37a22008-02-18 22:44:02 +0000102
Chris Lattnerb50e3902007-08-21 04:25:47 +0000103};
104} // end anonymous namespace.
105
Chris Lattnera7300102007-08-21 04:59:27 +0000106//===----------------------------------------------------------------------===//
107// Utilities
108//===----------------------------------------------------------------------===//
Chris Lattnerb50e3902007-08-21 04:25:47 +0000109
Lauro Ramos Venancio97b09572008-02-19 22:04:22 +0000110void AggExprEmitter::EmitAggregateClear(llvm::Value *DestPtr, QualType Ty) {
Chris Lattnerde0908b2008-04-04 16:54:41 +0000111 assert(!Ty->isAnyComplexType() && "Shouldn't happen for complex");
Lauro Ramos Venancio97b09572008-02-19 22:04:22 +0000112
113 // Aggregate assignment turns into llvm.memset.
114 const llvm::Type *BP = llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
115 if (DestPtr->getType() != BP)
116 DestPtr = Builder.CreateBitCast(DestPtr, BP, "tmp");
117
118 // Get size and alignment info for this aggregate.
Chris Lattner8cd0e932008-03-05 18:54:05 +0000119 std::pair<uint64_t, unsigned> TypeInfo = CGF.getContext().getTypeInfo(Ty);
Lauro Ramos Venancio97b09572008-02-19 22:04:22 +0000120
121 // FIXME: Handle variable sized types.
122 const llvm::Type *IntPtr = llvm::IntegerType::get(CGF.LLVMPointerWidth);
123
124 llvm::Value *MemSetOps[4] = {
125 DestPtr,
126 llvm::ConstantInt::getNullValue(llvm::Type::Int8Ty),
127 // TypeInfo.first describes size in bits.
128 llvm::ConstantInt::get(IntPtr, TypeInfo.first/8),
129 llvm::ConstantInt::get(llvm::Type::Int32Ty, TypeInfo.second/8)
130 };
131
132 Builder.CreateCall(CGF.CGM.getMemSetFn(), MemSetOps, MemSetOps+4);
133}
134
Chris Lattner41b1fca2007-08-26 23:13:56 +0000135void AggExprEmitter::EmitAggregateCopy(llvm::Value *DestPtr,
136 llvm::Value *SrcPtr, QualType Ty) {
Chris Lattnerde0908b2008-04-04 16:54:41 +0000137 assert(!Ty->isAnyComplexType() && "Shouldn't happen for complex");
Chris Lattner41b1fca2007-08-26 23:13:56 +0000138
Eli Friedman8f08a252008-05-26 12:59:39 +0000139 // Aggregate assignment turns into llvm.memmove.
Christopher Lamb4fe5e702007-12-17 01:11:20 +0000140 const llvm::Type *BP = llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
Chris Lattner41b1fca2007-08-26 23:13:56 +0000141 if (DestPtr->getType() != BP)
142 DestPtr = Builder.CreateBitCast(DestPtr, BP, "tmp");
143 if (SrcPtr->getType() != BP)
144 SrcPtr = Builder.CreateBitCast(SrcPtr, BP, "tmp");
145
146 // Get size and alignment info for this aggregate.
Chris Lattner8cd0e932008-03-05 18:54:05 +0000147 std::pair<uint64_t, unsigned> TypeInfo = CGF.getContext().getTypeInfo(Ty);
Chris Lattner41b1fca2007-08-26 23:13:56 +0000148
149 // FIXME: Handle variable sized types.
150 const llvm::Type *IntPtr = llvm::IntegerType::get(CGF.LLVMPointerWidth);
151
Eli Friedman8f08a252008-05-26 12:59:39 +0000152 llvm::Value *MemMoveOps[4] = {
Chris Lattner41b1fca2007-08-26 23:13:56 +0000153 DestPtr, SrcPtr,
Devang Patelddde6d52007-10-26 17:44:44 +0000154 // TypeInfo.first describes size in bits.
155 llvm::ConstantInt::get(IntPtr, TypeInfo.first/8),
Lauro Ramos Venancio97b09572008-02-19 22:04:22 +0000156 llvm::ConstantInt::get(llvm::Type::Int32Ty, TypeInfo.second/8)
Chris Lattner41b1fca2007-08-26 23:13:56 +0000157 };
158
Eli Friedman8f08a252008-05-26 12:59:39 +0000159 Builder.CreateCall(CGF.CGM.getMemMoveFn(), MemMoveOps, MemMoveOps+4);
Chris Lattner41b1fca2007-08-26 23:13:56 +0000160}
161
162
Chris Lattnerbdb8ffb2007-08-11 00:04:45 +0000163/// EmitAggLoadOfLValue - Given an expression with aggregate type that
164/// represents a value lvalue, this method emits the address of the lvalue,
165/// then loads the result into DestPtr.
Chris Lattnerb50e3902007-08-21 04:25:47 +0000166void AggExprEmitter::EmitAggLoadOfLValue(const Expr *E) {
167 LValue LV = CGF.EmitLValue(E);
Chris Lattnerbdb8ffb2007-08-11 00:04:45 +0000168 assert(LV.isSimple() && "Can't have aggregate bitfield, vector, etc");
169 llvm::Value *SrcPtr = LV.getAddress();
170
171 // If the result is ignored, don't copy from the value.
172 if (DestPtr == 0)
173 // FIXME: If the source is volatile, we must read from it.
174 return;
175
Chris Lattner41b1fca2007-08-26 23:13:56 +0000176 EmitAggregateCopy(DestPtr, SrcPtr, E->getType());
Chris Lattnerbdb8ffb2007-08-11 00:04:45 +0000177}
178
Chris Lattnera7300102007-08-21 04:59:27 +0000179//===----------------------------------------------------------------------===//
180// Visitor Methods
181//===----------------------------------------------------------------------===//
182
Anders Carlssonf2712ac2008-01-14 06:28:57 +0000183void AggExprEmitter::VisitImplicitCastExpr(ImplicitCastExpr *E)
184{
185 QualType STy = E->getSubExpr()->getType().getCanonicalType();
186 QualType Ty = E->getType().getCanonicalType();
Eli Friedmanf0217122008-02-11 01:09:17 +0000187
188 assert(CGF.getContext().typesAreCompatible(
189 STy.getUnqualifiedType(), Ty.getUnqualifiedType())
190 && "Implicit cast types must be compatible");
Anders Carlssonf2712ac2008-01-14 06:28:57 +0000191
192 Visit(E->getSubExpr());
193}
194
Anders Carlsson0ae15412007-10-31 22:04:46 +0000195void AggExprEmitter::VisitCallExpr(const CallExpr *E)
196{
197 RValue RV = CGF.EmitCallExpr(E);
198 assert(RV.isAggregate() && "Return value must be aggregate value!");
199
200 // If the result is ignored, don't copy from the value.
201 if (DestPtr == 0)
202 // FIXME: If the source is volatile, we must read from it.
203 return;
204
205 EmitAggregateCopy(DestPtr, RV.getAggregateAddr(), E->getType());
206}
Chris Lattner6ee20e32008-06-24 17:04:18 +0000207void AggExprEmitter::VisitObjCMessageExpr(ObjCMessageExpr *E)
208{
209 RValue RV = RValue::getAggregate(CGF.EmitObjCMessageExpr(E));
210
211 // If the result is ignored, don't copy from the value.
212 if (DestPtr == 0)
213 return;
214
215 EmitAggregateCopy(DestPtr, RV.getAggregateAddr(), E->getType());
216}
Anders Carlsson0ae15412007-10-31 22:04:46 +0000217
Nate Begemanc6078c92008-01-31 05:38:29 +0000218void AggExprEmitter::VisitOverloadExpr(const OverloadExpr *E)
219{
220 RValue RV = CGF.EmitCallExpr(E->getFn(), E->arg_begin(),
Ted Kremenek2719e982008-06-17 02:43:46 +0000221 E->arg_end(CGF.getContext()));
222
Nate Begemanc6078c92008-01-31 05:38:29 +0000223 assert(RV.isAggregate() && "Return value must be aggregate value!");
224
225 // If the result is ignored, don't copy from the value.
226 if (DestPtr == 0)
227 // FIXME: If the source is volatile, we must read from it.
228 return;
229
230 EmitAggregateCopy(DestPtr, RV.getAggregateAddr(), E->getType());
231}
232
Eli Friedman91fc7802008-05-20 07:56:31 +0000233void AggExprEmitter::VisitBinComma(const BinaryOperator *E)
234{
235 CGF.EmitAnyExpr(E->getLHS());
236 CGF.EmitAggExpr(E->getRHS(), DestPtr, false);
237}
238
Chris Lattnerbab0bfb2007-08-31 22:54:14 +0000239void AggExprEmitter::VisitStmtExpr(const StmtExpr *E) {
240 CGF.EmitCompoundStmt(*E->getSubStmt(), true, DestPtr, VolatileDest);
241}
242
Chris Lattnerb50e3902007-08-21 04:25:47 +0000243void AggExprEmitter::VisitBinaryOperator(const BinaryOperator *E) {
Chris Lattnere8f49632007-12-02 01:49:16 +0000244 CGF.WarnUnsupported(E, "aggregate binary expression");
Chris Lattnera7300102007-08-21 04:59:27 +0000245}
246
Chris Lattner76cb1892007-08-21 04:43:17 +0000247void AggExprEmitter::VisitBinAssign(const BinaryOperator *E) {
Eli Friedmanf0217122008-02-11 01:09:17 +0000248 // For an assignment to work, the value on the right has
249 // to be compatible with the value on the left.
250 assert(CGF.getContext().typesAreCompatible(
251 E->getLHS()->getType().getUnqualifiedType(),
252 E->getRHS()->getType().getUnqualifiedType())
253 && "Invalid assignment");
Chris Lattnerb50e3902007-08-21 04:25:47 +0000254 LValue LHS = CGF.EmitLValue(E->getLHS());
Chris Lattnerbdb8ffb2007-08-11 00:04:45 +0000255
256 // Codegen the RHS so that it stores directly into the LHS.
Chris Lattnerb50e3902007-08-21 04:25:47 +0000257 CGF.EmitAggExpr(E->getRHS(), LHS.getAddress(), false /*FIXME: VOLATILE LHS*/);
Chris Lattnerbdb8ffb2007-08-11 00:04:45 +0000258
Eli Friedmanf0217122008-02-11 01:09:17 +0000259 if (DestPtr == 0)
260 return;
261
Chris Lattnerbdb8ffb2007-08-11 00:04:45 +0000262 // If the result of the assignment is used, copy the RHS there also.
Eli Friedmanf0217122008-02-11 01:09:17 +0000263 EmitAggregateCopy(DestPtr, LHS.getAddress(), E->getType());
Chris Lattnerbdb8ffb2007-08-11 00:04:45 +0000264}
265
Chris Lattnerb50e3902007-08-21 04:25:47 +0000266void AggExprEmitter::VisitConditionalOperator(const ConditionalOperator *E) {
Gabor Greif815e2c12008-04-06 20:42:52 +0000267 llvm::BasicBlock *LHSBlock = llvm::BasicBlock::Create("cond.?");
268 llvm::BasicBlock *RHSBlock = llvm::BasicBlock::Create("cond.:");
269 llvm::BasicBlock *ContBlock = llvm::BasicBlock::Create("cond.cont");
Chris Lattnerbdb8ffb2007-08-11 00:04:45 +0000270
Chris Lattnerb50e3902007-08-21 04:25:47 +0000271 llvm::Value *Cond = CGF.EvaluateExprAsBool(E->getCond());
Chris Lattner41b1fca2007-08-26 23:13:56 +0000272 Builder.CreateCondBr(Cond, LHSBlock, RHSBlock);
Chris Lattnerbdb8ffb2007-08-11 00:04:45 +0000273
Chris Lattnerb50e3902007-08-21 04:25:47 +0000274 CGF.EmitBlock(LHSBlock);
Chris Lattnerbdb8ffb2007-08-11 00:04:45 +0000275
276 // Handle the GNU extension for missing LHS.
277 assert(E->getLHS() && "Must have LHS for aggregate value");
278
Chris Lattner55165ba2007-08-21 05:02:10 +0000279 Visit(E->getLHS());
Chris Lattner41b1fca2007-08-26 23:13:56 +0000280 Builder.CreateBr(ContBlock);
281 LHSBlock = Builder.GetInsertBlock();
Chris Lattnerbdb8ffb2007-08-11 00:04:45 +0000282
Chris Lattnerb50e3902007-08-21 04:25:47 +0000283 CGF.EmitBlock(RHSBlock);
Chris Lattnerbdb8ffb2007-08-11 00:04:45 +0000284
Chris Lattner55165ba2007-08-21 05:02:10 +0000285 Visit(E->getRHS());
Chris Lattner41b1fca2007-08-26 23:13:56 +0000286 Builder.CreateBr(ContBlock);
287 RHSBlock = Builder.GetInsertBlock();
Chris Lattnerbdb8ffb2007-08-11 00:04:45 +0000288
Chris Lattnerb50e3902007-08-21 04:25:47 +0000289 CGF.EmitBlock(ContBlock);
Chris Lattnerbdb8ffb2007-08-11 00:04:45 +0000290}
Chris Lattnera7300102007-08-21 04:59:27 +0000291
Eli Friedman42af3972008-05-27 15:51:49 +0000292void AggExprEmitter::VisitVAArgExpr(VAArgExpr *VE) {
293 llvm::Value *ArgValue = CGF.EmitLValue(VE->getSubExpr()).getAddress();
294 llvm::Value *V = Builder.CreateVAArg(ArgValue, CGF.ConvertType(VE->getType()));
295 if (DestPtr)
Eli Friedman2e630542008-06-13 23:01:12 +0000296 // FIXME: volatility
Eli Friedman42af3972008-05-27 15:51:49 +0000297 Builder.CreateStore(V, DestPtr);
298}
299
Lauro Ramos Venancio97b09572008-02-19 22:04:22 +0000300void AggExprEmitter::EmitNonConstInit(InitListExpr *E) {
301
302 const llvm::PointerType *APType =
303 cast<llvm::PointerType>(DestPtr->getType());
304 const llvm::Type *DestType = APType->getElementType();
Lauro Ramos Venancio9eb37a22008-02-18 22:44:02 +0000305
306 if (const llvm::ArrayType *AType = dyn_cast<llvm::ArrayType>(DestType)) {
Lauro Ramos Venancio97b09572008-02-19 22:04:22 +0000307 unsigned NumInitElements = E->getNumInits();
Lauro Ramos Venancio9eb37a22008-02-18 22:44:02 +0000308
Lauro Ramos Venancio9eb37a22008-02-18 22:44:02 +0000309 unsigned i;
310 for (i = 0; i != NumInitElements; ++i) {
Chris Lattner07307562008-03-19 05:19:41 +0000311 llvm::Value *NextVal = Builder.CreateStructGEP(DestPtr, i, ".array");
Lauro Ramos Venancio97b09572008-02-19 22:04:22 +0000312 Expr *Init = E->getInit(i);
313 if (isa<InitListExpr>(Init))
314 CGF.EmitAggExpr(Init, NextVal, VolatileDest);
315 else
Eli Friedman2e630542008-06-13 23:01:12 +0000316 // FIXME: volatility
Lauro Ramos Venancio97b09572008-02-19 22:04:22 +0000317 Builder.CreateStore(CGF.EmitScalarExpr(Init), NextVal);
Lauro Ramos Venancio9eb37a22008-02-18 22:44:02 +0000318 }
319
320 // Emit remaining default initializers
321 unsigned NumArrayElements = AType->getNumElements();
Lauro Ramos Venancio97b09572008-02-19 22:04:22 +0000322 QualType QType = E->getInit(0)->getType();
323 const llvm::Type *EType = AType->getElementType();
Lauro Ramos Venancio9eb37a22008-02-18 22:44:02 +0000324 for (/*Do not initialize i*/; i < NumArrayElements; ++i) {
Chris Lattner07307562008-03-19 05:19:41 +0000325 llvm::Value *NextVal = Builder.CreateStructGEP(DestPtr, i, ".array");
Dan Gohman377ba9f2008-05-22 22:12:56 +0000326 if (EType->isSingleValueType())
Eli Friedman2e630542008-06-13 23:01:12 +0000327 // FIXME: volatility
Lauro Ramos Venancio97b09572008-02-19 22:04:22 +0000328 Builder.CreateStore(llvm::Constant::getNullValue(EType), NextVal);
329 else
330 EmitAggregateClear(NextVal, QType);
Lauro Ramos Venancio9eb37a22008-02-18 22:44:02 +0000331 }
Lauro Ramos Venancio97b09572008-02-19 22:04:22 +0000332 } else
333 assert(false && "Invalid initializer");
Lauro Ramos Venancio9eb37a22008-02-18 22:44:02 +0000334}
335
Chris Lattner6ee6ab02008-04-04 18:42:16 +0000336void AggExprEmitter::EmitInitializationToLValue(Expr* E, LValue LV) {
337 // FIXME: Are initializers affected by volatile?
338 if (E->getType()->isComplexType()) {
339 CGF.EmitComplexExprIntoAddr(E, LV.getAddress(), false);
Eli Friedmanbb822fb2008-05-12 15:06:05 +0000340 } else if (CGF.hasAggregateLLVMType(E->getType())) {
341 CGF.EmitAnyExpr(E, LV.getAddress(), false);
342 } else {
343 CGF.EmitStoreThroughLValue(CGF.EmitAnyExpr(E), LV, E->getType());
Chris Lattner6ee6ab02008-04-04 18:42:16 +0000344 }
Chris Lattner6ee6ab02008-04-04 18:42:16 +0000345}
346
347void AggExprEmitter::EmitNullInitializationToLValue(LValue LV, QualType T) {
348 if (!CGF.hasAggregateLLVMType(T)) {
349 // For non-aggregates, we can store zero
350 const llvm::Type *T =
351 cast<llvm::PointerType>(LV.getAddress()->getType())->getElementType();
Eli Friedman2e630542008-06-13 23:01:12 +0000352 // FIXME: volatility
Chris Lattner6ee6ab02008-04-04 18:42:16 +0000353 Builder.CreateStore(llvm::Constant::getNullValue(T), LV.getAddress());
Lauro Ramos Venancioa62b1df2008-02-19 19:27:31 +0000354 } else {
Chris Lattner6ee6ab02008-04-04 18:42:16 +0000355 // Otherwise, just memset the whole thing to zero. This is legal
356 // because in LLVM, all default initializers are guaranteed to have a
357 // bit pattern of all zeros.
358 // There's a potential optimization opportunity in combining
359 // memsets; that would be easy for arrays, but relatively
360 // difficult for structures with the current code.
361 llvm::Value *MemSet = CGF.CGM.getIntrinsic(llvm::Intrinsic::memset_i64);
362 uint64_t Size = CGF.getContext().getTypeSize(T);
363
364 const llvm::Type *BP = llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
365 llvm::Value* DestPtr = Builder.CreateBitCast(LV.getAddress(), BP, "tmp");
Chris Lattner7db5e942008-05-06 00:56:42 +0000366 Builder.CreateCall4(MemSet, DestPtr,
367 llvm::ConstantInt::get(llvm::Type::Int8Ty, 0),
368 llvm::ConstantInt::get(llvm::Type::Int64Ty, Size/8),
369 llvm::ConstantInt::get(llvm::Type::Int32Ty, 0));
Chris Lattner6ee6ab02008-04-04 18:42:16 +0000370 }
371}
372
Chris Lattner6ee6ab02008-04-04 18:42:16 +0000373void AggExprEmitter::VisitInitListExpr(InitListExpr *E) {
374 if (E->isConstantExpr(CGF.getContext(), 0)) {
375 // FIXME: call into const expr emitter so that we can emit
376 // a memcpy instead of storing the individual members.
377 // This is purely for perf; both codepaths lead to equivalent
378 // (although not necessarily identical) code.
379 // It's worth noting that LLVM keeps on getting smarter, though,
380 // so it might not be worth bothering.
381 }
382
383 // Handle initialization of an array.
384 if (E->getType()->isArrayType()) {
385 const llvm::PointerType *APType =
386 cast<llvm::PointerType>(DestPtr->getType());
387 const llvm::ArrayType *AType =
388 cast<llvm::ArrayType>(APType->getElementType());
389
390 uint64_t NumInitElements = E->getNumInits();
Eli Friedman48ec5622008-05-19 17:51:16 +0000391
392 if (E->getNumInits() > 0 &&
393 E->getType().getCanonicalType().getUnqualifiedType() ==
394 E->getInit(0)->getType().getCanonicalType().getUnqualifiedType()) {
395 EmitAggLoadOfLValue(E->getInit(0));
396 return;
397 }
398
Chris Lattner6ee6ab02008-04-04 18:42:16 +0000399 uint64_t NumArrayElements = AType->getNumElements();
400 QualType ElementType = E->getType()->getAsArrayType()->getElementType();
401
Eli Friedman2e630542008-06-13 23:01:12 +0000402 unsigned CVRqualifier = E->getType().getCanonicalType()->getAsArrayType()
403 ->getElementType().getCVRQualifiers();
404
Chris Lattner6ee6ab02008-04-04 18:42:16 +0000405 for (uint64_t i = 0; i != NumArrayElements; ++i) {
406 llvm::Value *NextVal = Builder.CreateStructGEP(DestPtr, i, ".array");
407 if (i < NumInitElements)
Eli Friedman2e630542008-06-13 23:01:12 +0000408 EmitInitializationToLValue(E->getInit(i),
409 LValue::MakeAddr(NextVal, CVRqualifier));
Chris Lattner6ee6ab02008-04-04 18:42:16 +0000410 else
Eli Friedman2e630542008-06-13 23:01:12 +0000411 EmitNullInitializationToLValue(LValue::MakeAddr(NextVal, CVRqualifier),
Chris Lattner6ee6ab02008-04-04 18:42:16 +0000412 ElementType);
Lauro Ramos Venancioa62b1df2008-02-19 19:27:31 +0000413 }
Chris Lattner6ee6ab02008-04-04 18:42:16 +0000414 return;
415 }
416
417 assert(E->getType()->isRecordType() && "Only support structs/unions here!");
418
419 // Do struct initialization; this code just sets each individual member
420 // to the approprate value. This makes bitfield support automatic;
421 // the disadvantage is that the generated code is more difficult for
422 // the optimizer, especially with bitfields.
423 unsigned NumInitElements = E->getNumInits();
424 RecordDecl *SD = E->getType()->getAsRecordType()->getDecl();
425 unsigned NumMembers = SD->getNumMembers() - SD->hasFlexibleArrayMember();
426 unsigned CurInitVal = 0;
427 bool isUnion = E->getType()->isUnionType();
428
429 // Here we iterate over the fields; this makes it simpler to both
430 // default-initialize fields and skip over unnamed fields.
431 for (unsigned CurFieldNo = 0; CurFieldNo != NumMembers; ++CurFieldNo) {
432 if (CurInitVal >= NumInitElements) {
433 // No more initializers; we're done.
434 break;
435 }
436
437 FieldDecl *CurField = SD->getMember(CurFieldNo);
438 if (CurField->getIdentifier() == 0) {
439 // Initializers can't initialize unnamed fields, e.g. "int : 20;"
440 continue;
441 }
Eli Friedman2e630542008-06-13 23:01:12 +0000442 // FIXME: volatility
443 LValue FieldLoc = CGF.EmitLValueForField(DestPtr, CurField, isUnion,0);
Chris Lattner6ee6ab02008-04-04 18:42:16 +0000444 if (CurInitVal < NumInitElements) {
445 // Store the initializer into the field
446 // This will probably have to get a bit smarter when we support
447 // designators in initializers
448 EmitInitializationToLValue(E->getInit(CurInitVal++), FieldLoc);
449 } else {
450 // We're out of initalizers; default-initialize to null
451 EmitNullInitializationToLValue(FieldLoc, CurField->getType());
452 }
453
454 // Unions only initialize one field.
455 // (things can get weird with designators, but they aren't
456 // supported yet.)
457 if (E->getType()->isUnionType())
458 break;
Lauro Ramos Venancioa62b1df2008-02-19 19:27:31 +0000459 }
Devang Patelddde6d52007-10-26 17:44:44 +0000460}
461
Chris Lattnera7300102007-08-21 04:59:27 +0000462//===----------------------------------------------------------------------===//
463// Entry Points into this File
464//===----------------------------------------------------------------------===//
465
466/// EmitAggExpr - Emit the computation of the specified expression of
467/// aggregate type. The result is computed into DestPtr. Note that if
468/// DestPtr is null, the value of the aggregate expression is not needed.
469void CodeGenFunction::EmitAggExpr(const Expr *E, llvm::Value *DestPtr,
470 bool VolatileDest) {
471 assert(E && hasAggregateLLVMType(E->getType()) &&
472 "Invalid aggregate expression to emit");
473
474 AggExprEmitter(*this, DestPtr, VolatileDest).Visit(const_cast<Expr*>(E));
475}