blob: 66f3feb8cc4292b88a10cb98303264231005e445 [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"
Daniel Dunbarde7fb842008-08-11 05:00:27 +000016#include "clang/AST/ASTContext.h"
17#include "clang/AST/StmtVisitor.h"
Chris Lattner883f6a72007-08-11 00:04:45 +000018#include "llvm/Constants.h"
19#include "llvm/Function.h"
Devang Patel636c3d02007-10-26 17:44:44 +000020#include "llvm/GlobalVariable.h"
Chris Lattner9c033562007-08-21 04:25:47 +000021#include "llvm/Support/Compiler.h"
Chris Lattnerf81557c2008-04-04 18:42:16 +000022#include "llvm/Intrinsics.h"
Chris Lattneraf6f5282007-08-10 20:13:28 +000023using namespace clang;
24using namespace CodeGen;
Chris Lattner883f6a72007-08-11 00:04:45 +000025
Chris Lattner9c033562007-08-21 04:25:47 +000026//===----------------------------------------------------------------------===//
27// Aggregate Expression Emitter
28//===----------------------------------------------------------------------===//
29
30namespace {
31class VISIBILITY_HIDDEN AggExprEmitter : public StmtVisitor<AggExprEmitter> {
32 CodeGenFunction &CGF;
Daniel Dunbar45d196b2008-11-01 01:53:16 +000033 CGBuilderTy &Builder;
Chris Lattner9c033562007-08-21 04:25:47 +000034 llvm::Value *DestPtr;
35 bool VolatileDest;
36public:
37 AggExprEmitter(CodeGenFunction &cgf, llvm::Value *destPtr, bool volatileDest)
Chris Lattnerbfc0c1a2007-08-26 23:13:56 +000038 : CGF(cgf), Builder(CGF.Builder),
39 DestPtr(destPtr), VolatileDest(volatileDest) {
Chris Lattner9c033562007-08-21 04:25:47 +000040 }
41
Chris Lattneree755f92007-08-21 04:59:27 +000042 //===--------------------------------------------------------------------===//
43 // Utilities
44 //===--------------------------------------------------------------------===//
45
Chris Lattner9c033562007-08-21 04:25:47 +000046 /// EmitAggLoadOfLValue - Given an expression with aggregate type that
47 /// represents a value lvalue, this method emits the address of the lvalue,
48 /// then loads the result into DestPtr.
49 void EmitAggLoadOfLValue(const Expr *E);
50
Lauro Ramos Venancio13e22cf2008-02-19 22:04:22 +000051 void EmitNonConstInit(InitListExpr *E);
Eli Friedman922696f2008-05-19 17:51:16 +000052
Chris Lattneree755f92007-08-21 04:59:27 +000053 //===--------------------------------------------------------------------===//
54 // Visitor Methods
55 //===--------------------------------------------------------------------===//
56
Chris Lattner9c033562007-08-21 04:25:47 +000057 void VisitStmt(Stmt *S) {
Daniel Dunbar488e9932008-08-16 00:56:44 +000058 CGF.ErrorUnsupported(S, "aggregate expression");
Chris Lattner9c033562007-08-21 04:25:47 +000059 }
60 void VisitParenExpr(ParenExpr *PE) { Visit(PE->getSubExpr()); }
61
62 // l-values.
Seo Sanghyeon9b73b392007-12-14 02:04:12 +000063 void VisitDeclRefExpr(DeclRefExpr *DRE) { EmitAggLoadOfLValue(DRE); }
64 void VisitMemberExpr(MemberExpr *ME) { EmitAggLoadOfLValue(ME); }
65 void VisitUnaryDeref(UnaryOperator *E) { EmitAggLoadOfLValue(E); }
Seo Sanghyeonad6ebd62007-12-23 03:11:58 +000066 void VisitStringLiteral(StringLiteral *E) { EmitAggLoadOfLValue(E); }
Eli Friedmanb1851242008-05-27 15:51:49 +000067 void VisitCompoundLiteralExpr(CompoundLiteralExpr *E)
68 { EmitAggLoadOfLValue(E); }
Seo Sanghyeon9b73b392007-12-14 02:04:12 +000069
70 void VisitArraySubscriptExpr(ArraySubscriptExpr *E) {
71 EmitAggLoadOfLValue(E);
72 }
Chris Lattner9c033562007-08-21 04:25:47 +000073
74 // Operators.
75 // case Expr::UnaryOperatorClass:
Chris Lattner9c033562007-08-21 04:25:47 +000076 // case Expr::CastExprClass:
Anders Carlssone4707ff2008-01-14 06:28:57 +000077 void VisitImplicitCastExpr(ImplicitCastExpr *E);
Anders Carlsson148fe672007-10-31 22:04:46 +000078 void VisitCallExpr(const CallExpr *E);
Chris Lattnerb2d963f2007-08-31 22:54:14 +000079 void VisitStmtExpr(const StmtExpr *E);
Chris Lattner9c033562007-08-21 04:25:47 +000080 void VisitBinaryOperator(const BinaryOperator *BO);
Chris Lattner03d6fb92007-08-21 04:43:17 +000081 void VisitBinAssign(const BinaryOperator *E);
Nate Begeman796ef3d2008-01-31 05:38:29 +000082 void VisitOverloadExpr(const OverloadExpr *E);
Eli Friedman07fa52a2008-05-20 07:56:31 +000083 void VisitBinComma(const BinaryOperator *E);
Chris Lattner9c033562007-08-21 04:25:47 +000084
Chris Lattner8fdf3282008-06-24 17:04:18 +000085 void VisitObjCMessageExpr(ObjCMessageExpr *E);
Daniel Dunbar0a04d772008-08-23 10:51:21 +000086 void VisitObjCIvarRefExpr(ObjCIvarRefExpr *E) {
87 EmitAggLoadOfLValue(E);
88 }
Daniel Dunbar9c3fc702008-08-27 06:57:25 +000089 void VisitObjCPropertyRefExpr(ObjCPropertyRefExpr *E);
Fariborz Jahanian5daf5702008-11-22 18:39:36 +000090 void VisitObjCKVCRefExpr(ObjCKVCRefExpr *E);
Chris Lattner9c033562007-08-21 04:25:47 +000091
92 void VisitConditionalOperator(const ConditionalOperator *CO);
Devang Patel636c3d02007-10-26 17:44:44 +000093 void VisitInitListExpr(InitListExpr *E);
Chris Lattner04421082008-04-08 04:40:51 +000094 void VisitCXXDefaultArgExpr(CXXDefaultArgExpr *DAE) {
95 Visit(DAE->getExpr());
96 }
Eli Friedmanb1851242008-05-27 15:51:49 +000097 void VisitVAArgExpr(VAArgExpr *E);
Chris Lattnerf81557c2008-04-04 18:42:16 +000098
99 void EmitInitializationToLValue(Expr *E, LValue Address);
100 void EmitNullInitializationToLValue(LValue Address, QualType T);
Chris Lattner9c033562007-08-21 04:25:47 +0000101 // case Expr::ChooseExprClass:
Lauro Ramos Venancio305762c2008-02-18 22:44:02 +0000102
Chris Lattner9c033562007-08-21 04:25:47 +0000103};
104} // end anonymous namespace.
105
Chris Lattneree755f92007-08-21 04:59:27 +0000106//===----------------------------------------------------------------------===//
107// Utilities
108//===----------------------------------------------------------------------===//
Chris Lattner9c033562007-08-21 04:25:47 +0000109
Chris Lattner883f6a72007-08-11 00:04:45 +0000110/// EmitAggLoadOfLValue - Given an expression with aggregate type that
111/// represents a value lvalue, this method emits the address of the lvalue,
112/// then loads the result into DestPtr.
Chris Lattner9c033562007-08-21 04:25:47 +0000113void AggExprEmitter::EmitAggLoadOfLValue(const Expr *E) {
114 LValue LV = CGF.EmitLValue(E);
Chris Lattner883f6a72007-08-11 00:04:45 +0000115 assert(LV.isSimple() && "Can't have aggregate bitfield, vector, etc");
116 llvm::Value *SrcPtr = LV.getAddress();
117
118 // If the result is ignored, don't copy from the value.
119 if (DestPtr == 0)
120 // FIXME: If the source is volatile, we must read from it.
121 return;
122
Daniel Dunbar7482d122008-09-09 20:49:46 +0000123 CGF.EmitAggregateCopy(DestPtr, SrcPtr, E->getType());
Chris Lattner883f6a72007-08-11 00:04:45 +0000124}
125
Chris Lattneree755f92007-08-21 04:59:27 +0000126//===----------------------------------------------------------------------===//
127// Visitor Methods
128//===----------------------------------------------------------------------===//
129
Chris Lattner96196622008-07-26 22:37:01 +0000130void AggExprEmitter::VisitImplicitCastExpr(ImplicitCastExpr *E) {
Eli Friedmanff6e2b72008-02-11 01:09:17 +0000131 assert(CGF.getContext().typesAreCompatible(
Chris Lattner96196622008-07-26 22:37:01 +0000132 E->getSubExpr()->getType().getUnqualifiedType(),
133 E->getType().getUnqualifiedType()) &&
134 "Implicit cast types must be compatible");
Anders Carlssone4707ff2008-01-14 06:28:57 +0000135 Visit(E->getSubExpr());
136}
137
Chris Lattner96196622008-07-26 22:37:01 +0000138void AggExprEmitter::VisitCallExpr(const CallExpr *E) {
Anders Carlsson148fe672007-10-31 22:04:46 +0000139 RValue RV = CGF.EmitCallExpr(E);
140 assert(RV.isAggregate() && "Return value must be aggregate value!");
141
142 // If the result is ignored, don't copy from the value.
143 if (DestPtr == 0)
144 // FIXME: If the source is volatile, we must read from it.
145 return;
146
Daniel Dunbar7482d122008-09-09 20:49:46 +0000147 CGF.EmitAggregateCopy(DestPtr, RV.getAggregateAddr(), E->getType());
Anders Carlsson148fe672007-10-31 22:04:46 +0000148}
Chris Lattner96196622008-07-26 22:37:01 +0000149
150void AggExprEmitter::VisitObjCMessageExpr(ObjCMessageExpr *E) {
Daniel Dunbar8f2926b2008-08-23 03:46:30 +0000151 RValue RV = CGF.EmitObjCMessageExpr(E);
152 assert(RV.isAggregate() && "Return value must be aggregate value!");
Chris Lattner8fdf3282008-06-24 17:04:18 +0000153
154 // If the result is ignored, don't copy from the value.
155 if (DestPtr == 0)
Daniel Dunbar8f2926b2008-08-23 03:46:30 +0000156 // FIXME: If the source is volatile, we must read from it.
Chris Lattner8fdf3282008-06-24 17:04:18 +0000157 return;
158
Daniel Dunbar7482d122008-09-09 20:49:46 +0000159 CGF.EmitAggregateCopy(DestPtr, RV.getAggregateAddr(), E->getType());
Chris Lattner8fdf3282008-06-24 17:04:18 +0000160}
Anders Carlsson148fe672007-10-31 22:04:46 +0000161
Daniel Dunbar9c3fc702008-08-27 06:57:25 +0000162void AggExprEmitter::VisitObjCPropertyRefExpr(ObjCPropertyRefExpr *E) {
163 RValue RV = CGF.EmitObjCPropertyGet(E);
164 assert(RV.isAggregate() && "Return value must be aggregate value!");
165
166 // If the result is ignored, don't copy from the value.
167 if (DestPtr == 0)
168 // FIXME: If the source is volatile, we must read from it.
169 return;
170
Daniel Dunbar7482d122008-09-09 20:49:46 +0000171 CGF.EmitAggregateCopy(DestPtr, RV.getAggregateAddr(), E->getType());
Daniel Dunbar9c3fc702008-08-27 06:57:25 +0000172}
173
Fariborz Jahanian5daf5702008-11-22 18:39:36 +0000174void AggExprEmitter::VisitObjCKVCRefExpr(ObjCKVCRefExpr *E) {
175 RValue RV = CGF.EmitObjCPropertyGet(E);
176 assert(RV.isAggregate() && "Return value must be aggregate value!");
177
178 // If the result is ignored, don't copy from the value.
179 if (DestPtr == 0)
180 // FIXME: If the source is volatile, we must read from it.
181 return;
182
183 CGF.EmitAggregateCopy(DestPtr, RV.getAggregateAddr(), E->getType());
184}
185
Chris Lattner96196622008-07-26 22:37:01 +0000186void AggExprEmitter::VisitOverloadExpr(const OverloadExpr *E) {
Nate Begeman796ef3d2008-01-31 05:38:29 +0000187 RValue RV = CGF.EmitCallExpr(E->getFn(), E->arg_begin(),
Ted Kremenek55499762008-06-17 02:43:46 +0000188 E->arg_end(CGF.getContext()));
189
Nate Begeman796ef3d2008-01-31 05:38:29 +0000190 assert(RV.isAggregate() && "Return value must be aggregate value!");
191
192 // If the result is ignored, don't copy from the value.
193 if (DestPtr == 0)
194 // FIXME: If the source is volatile, we must read from it.
195 return;
196
Daniel Dunbar7482d122008-09-09 20:49:46 +0000197 CGF.EmitAggregateCopy(DestPtr, RV.getAggregateAddr(), E->getType());
Nate Begeman796ef3d2008-01-31 05:38:29 +0000198}
199
Chris Lattner96196622008-07-26 22:37:01 +0000200void AggExprEmitter::VisitBinComma(const BinaryOperator *E) {
Eli Friedman07fa52a2008-05-20 07:56:31 +0000201 CGF.EmitAnyExpr(E->getLHS());
202 CGF.EmitAggExpr(E->getRHS(), DestPtr, false);
203}
204
Chris Lattnerb2d963f2007-08-31 22:54:14 +0000205void AggExprEmitter::VisitStmtExpr(const StmtExpr *E) {
206 CGF.EmitCompoundStmt(*E->getSubStmt(), true, DestPtr, VolatileDest);
207}
208
Chris Lattner9c033562007-08-21 04:25:47 +0000209void AggExprEmitter::VisitBinaryOperator(const BinaryOperator *E) {
Daniel Dunbar488e9932008-08-16 00:56:44 +0000210 CGF.ErrorUnsupported(E, "aggregate binary expression");
Chris Lattneree755f92007-08-21 04:59:27 +0000211}
212
Chris Lattner03d6fb92007-08-21 04:43:17 +0000213void AggExprEmitter::VisitBinAssign(const BinaryOperator *E) {
Eli Friedmanff6e2b72008-02-11 01:09:17 +0000214 // For an assignment to work, the value on the right has
215 // to be compatible with the value on the left.
216 assert(CGF.getContext().typesAreCompatible(
217 E->getLHS()->getType().getUnqualifiedType(),
218 E->getRHS()->getType().getUnqualifiedType())
219 && "Invalid assignment");
Chris Lattner9c033562007-08-21 04:25:47 +0000220 LValue LHS = CGF.EmitLValue(E->getLHS());
Chris Lattner883f6a72007-08-11 00:04:45 +0000221
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000222 // We have to special case property setters, otherwise we must have
223 // a simple lvalue (no aggregates inside vectors, bitfields).
224 if (LHS.isPropertyRef()) {
225 // FIXME: Volatility?
226 llvm::Value *AggLoc = DestPtr;
227 if (!AggLoc)
228 AggLoc = CGF.CreateTempAlloca(CGF.ConvertType(E->getRHS()->getType()));
229 CGF.EmitAggExpr(E->getRHS(), AggLoc, false);
230 CGF.EmitObjCPropertySet(LHS.getPropertyRefExpr(),
231 RValue::getAggregate(AggLoc));
232 } else {
233 // Codegen the RHS so that it stores directly into the LHS.
234 CGF.EmitAggExpr(E->getRHS(), LHS.getAddress(), false /*FIXME: VOLATILE LHS*/);
235
236 if (DestPtr == 0)
237 return;
238
239 // If the result of the assignment is used, copy the RHS there also.
Daniel Dunbar7482d122008-09-09 20:49:46 +0000240 CGF.EmitAggregateCopy(DestPtr, LHS.getAddress(), E->getType());
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000241 }
Chris Lattner883f6a72007-08-11 00:04:45 +0000242}
243
Chris Lattner9c033562007-08-21 04:25:47 +0000244void AggExprEmitter::VisitConditionalOperator(const ConditionalOperator *E) {
Daniel Dunbar9615ecb2008-11-13 01:38:36 +0000245 llvm::BasicBlock *LHSBlock = CGF.createBasicBlock("cond.true");
246 llvm::BasicBlock *RHSBlock = CGF.createBasicBlock("cond.false");
247 llvm::BasicBlock *ContBlock = CGF.createBasicBlock("cond.end");
Chris Lattner883f6a72007-08-11 00:04:45 +0000248
Chris Lattner9c033562007-08-21 04:25:47 +0000249 llvm::Value *Cond = CGF.EvaluateExprAsBool(E->getCond());
Chris Lattnerbfc0c1a2007-08-26 23:13:56 +0000250 Builder.CreateCondBr(Cond, LHSBlock, RHSBlock);
Chris Lattner883f6a72007-08-11 00:04:45 +0000251
Chris Lattner9c033562007-08-21 04:25:47 +0000252 CGF.EmitBlock(LHSBlock);
Chris Lattner883f6a72007-08-11 00:04:45 +0000253
254 // Handle the GNU extension for missing LHS.
255 assert(E->getLHS() && "Must have LHS for aggregate value");
256
Chris Lattnerc748f272007-08-21 05:02:10 +0000257 Visit(E->getLHS());
Daniel Dunbard57a8712008-11-11 09:41:28 +0000258 CGF.EmitBranch(ContBlock);
Chris Lattner883f6a72007-08-11 00:04:45 +0000259
Chris Lattner9c033562007-08-21 04:25:47 +0000260 CGF.EmitBlock(RHSBlock);
Chris Lattner883f6a72007-08-11 00:04:45 +0000261
Chris Lattnerc748f272007-08-21 05:02:10 +0000262 Visit(E->getRHS());
Daniel Dunbard57a8712008-11-11 09:41:28 +0000263 CGF.EmitBranch(ContBlock);
Chris Lattner883f6a72007-08-11 00:04:45 +0000264
Chris Lattner9c033562007-08-21 04:25:47 +0000265 CGF.EmitBlock(ContBlock);
Chris Lattner883f6a72007-08-11 00:04:45 +0000266}
Chris Lattneree755f92007-08-21 04:59:27 +0000267
Eli Friedmanb1851242008-05-27 15:51:49 +0000268void AggExprEmitter::VisitVAArgExpr(VAArgExpr *VE) {
269 llvm::Value *ArgValue = CGF.EmitLValue(VE->getSubExpr()).getAddress();
Anders Carlssonddf7cac2008-11-04 05:30:00 +0000270 llvm::Value *ArgPtr = CGF.EmitVAArg(ArgValue, VE->getType());
271
272 if (!ArgPtr)
273 CGF.ErrorUnsupported(VE, "aggregate va_arg expression");
274
Eli Friedmanb1851242008-05-27 15:51:49 +0000275 if (DestPtr)
Eli Friedman1e692ac2008-06-13 23:01:12 +0000276 // FIXME: volatility
Anders Carlssonddf7cac2008-11-04 05:30:00 +0000277 CGF.EmitAggregateCopy(DestPtr, ArgPtr, VE->getType());
Eli Friedmanb1851242008-05-27 15:51:49 +0000278}
279
Lauro Ramos Venancio13e22cf2008-02-19 22:04:22 +0000280void AggExprEmitter::EmitNonConstInit(InitListExpr *E) {
Chris Lattnerbe20bb52008-10-26 23:53:12 +0000281 if (E->hadDesignators()) {
282 CGF.ErrorUnsupported(E, "initializer list with designators");
283 return;
284 }
285
Lauro Ramos Venancio13e22cf2008-02-19 22:04:22 +0000286 const llvm::PointerType *APType =
287 cast<llvm::PointerType>(DestPtr->getType());
288 const llvm::Type *DestType = APType->getElementType();
Lauro Ramos Venancio305762c2008-02-18 22:44:02 +0000289
290 if (const llvm::ArrayType *AType = dyn_cast<llvm::ArrayType>(DestType)) {
Lauro Ramos Venancio13e22cf2008-02-19 22:04:22 +0000291 unsigned NumInitElements = E->getNumInits();
Lauro Ramos Venancio305762c2008-02-18 22:44:02 +0000292
Lauro Ramos Venancio305762c2008-02-18 22:44:02 +0000293 unsigned i;
294 for (i = 0; i != NumInitElements; ++i) {
Chris Lattner36b6a0a2008-03-19 05:19:41 +0000295 llvm::Value *NextVal = Builder.CreateStructGEP(DestPtr, i, ".array");
Lauro Ramos Venancio13e22cf2008-02-19 22:04:22 +0000296 Expr *Init = E->getInit(i);
297 if (isa<InitListExpr>(Init))
298 CGF.EmitAggExpr(Init, NextVal, VolatileDest);
299 else
Eli Friedman1e692ac2008-06-13 23:01:12 +0000300 // FIXME: volatility
Lauro Ramos Venancio13e22cf2008-02-19 22:04:22 +0000301 Builder.CreateStore(CGF.EmitScalarExpr(Init), NextVal);
Lauro Ramos Venancio305762c2008-02-18 22:44:02 +0000302 }
303
304 // Emit remaining default initializers
305 unsigned NumArrayElements = AType->getNumElements();
Lauro Ramos Venancio13e22cf2008-02-19 22:04:22 +0000306 QualType QType = E->getInit(0)->getType();
307 const llvm::Type *EType = AType->getElementType();
Lauro Ramos Venancio305762c2008-02-18 22:44:02 +0000308 for (/*Do not initialize i*/; i < NumArrayElements; ++i) {
Chris Lattner36b6a0a2008-03-19 05:19:41 +0000309 llvm::Value *NextVal = Builder.CreateStructGEP(DestPtr, i, ".array");
Dan Gohmand79a7262008-05-22 22:12:56 +0000310 if (EType->isSingleValueType())
Eli Friedman1e692ac2008-06-13 23:01:12 +0000311 // FIXME: volatility
Lauro Ramos Venancio13e22cf2008-02-19 22:04:22 +0000312 Builder.CreateStore(llvm::Constant::getNullValue(EType), NextVal);
313 else
Daniel Dunbar7482d122008-09-09 20:49:46 +0000314 CGF.EmitAggregateClear(NextVal, QType);
Lauro Ramos Venancio305762c2008-02-18 22:44:02 +0000315 }
Lauro Ramos Venancio13e22cf2008-02-19 22:04:22 +0000316 } else
317 assert(false && "Invalid initializer");
Lauro Ramos Venancio305762c2008-02-18 22:44:02 +0000318}
319
Chris Lattnerf81557c2008-04-04 18:42:16 +0000320void AggExprEmitter::EmitInitializationToLValue(Expr* E, LValue LV) {
321 // FIXME: Are initializers affected by volatile?
322 if (E->getType()->isComplexType()) {
323 CGF.EmitComplexExprIntoAddr(E, LV.getAddress(), false);
Eli Friedmanc8ba9612008-05-12 15:06:05 +0000324 } else if (CGF.hasAggregateLLVMType(E->getType())) {
325 CGF.EmitAnyExpr(E, LV.getAddress(), false);
326 } else {
327 CGF.EmitStoreThroughLValue(CGF.EmitAnyExpr(E), LV, E->getType());
Chris Lattnerf81557c2008-04-04 18:42:16 +0000328 }
Chris Lattnerf81557c2008-04-04 18:42:16 +0000329}
330
331void AggExprEmitter::EmitNullInitializationToLValue(LValue LV, QualType T) {
332 if (!CGF.hasAggregateLLVMType(T)) {
333 // For non-aggregates, we can store zero
Daniel Dunbar82397132008-08-06 05:32:55 +0000334 llvm::Value *Null = llvm::Constant::getNullValue(CGF.ConvertType(T));
335 CGF.EmitStoreThroughLValue(RValue::get(Null), LV, T);
Lauro Ramos Venancio145cd892008-02-19 19:27:31 +0000336 } else {
Chris Lattnerf81557c2008-04-04 18:42:16 +0000337 // Otherwise, just memset the whole thing to zero. This is legal
338 // because in LLVM, all default initializers are guaranteed to have a
339 // bit pattern of all zeros.
340 // There's a potential optimization opportunity in combining
341 // memsets; that would be easy for arrays, but relatively
342 // difficult for structures with the current code.
Chris Lattner4e8a9e82008-11-21 16:43:15 +0000343 const llvm::Type *SizeTy = llvm::Type::Int64Ty;
344 llvm::Value *MemSet = CGF.CGM.getIntrinsic(llvm::Intrinsic::memset,
345 &SizeTy, 1);
Chris Lattnerf81557c2008-04-04 18:42:16 +0000346 uint64_t Size = CGF.getContext().getTypeSize(T);
347
348 const llvm::Type *BP = llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
349 llvm::Value* DestPtr = Builder.CreateBitCast(LV.getAddress(), BP, "tmp");
Chris Lattner3eae03e2008-05-06 00:56:42 +0000350 Builder.CreateCall4(MemSet, DestPtr,
351 llvm::ConstantInt::get(llvm::Type::Int8Ty, 0),
Chris Lattner4e8a9e82008-11-21 16:43:15 +0000352 llvm::ConstantInt::get(SizeTy, Size/8),
Chris Lattner3eae03e2008-05-06 00:56:42 +0000353 llvm::ConstantInt::get(llvm::Type::Int32Ty, 0));
Chris Lattnerf81557c2008-04-04 18:42:16 +0000354 }
355}
356
Chris Lattnerf81557c2008-04-04 18:42:16 +0000357void AggExprEmitter::VisitInitListExpr(InitListExpr *E) {
Chris Lattnerbe20bb52008-10-26 23:53:12 +0000358 if (E->hadDesignators()) {
359 CGF.ErrorUnsupported(E, "initializer list with designators");
360 return;
361 }
362
Daniel Dunbarbe38d022008-08-23 18:44:10 +0000363 // FIXME: For constant expressions, call into const expr emitter so
364 // that we can emit a memcpy instead of storing the individual
365 // members. This is purely for perf; both codepaths lead to
366 // equivalent (although not necessarily identical) code. It's worth
367 // noting that LLVM keeps on getting smarter, though, so it might
368 // not be worth bothering.
Chris Lattnerf81557c2008-04-04 18:42:16 +0000369
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
Chris Lattner96196622008-07-26 22:37:01 +0000379 if (E->getNumInits() > 0) {
380 QualType T1 = E->getType();
381 QualType T2 = E->getInit(0)->getType();
382 if (CGF.getContext().getCanonicalType(T1).getUnqualifiedType() ==
383 CGF.getContext().getCanonicalType(T2).getUnqualifiedType()) {
384 EmitAggLoadOfLValue(E->getInit(0));
385 return;
386 }
Eli Friedman922696f2008-05-19 17:51:16 +0000387 }
388
Chris Lattnerf81557c2008-04-04 18:42:16 +0000389 uint64_t NumArrayElements = AType->getNumElements();
Chris Lattnerc63a1f22008-08-04 07:31:14 +0000390 QualType ElementType = CGF.getContext().getCanonicalType(E->getType());
391 ElementType =CGF.getContext().getAsArrayType(ElementType)->getElementType();
Chris Lattnerf81557c2008-04-04 18:42:16 +0000392
Chris Lattnerc63a1f22008-08-04 07:31:14 +0000393 unsigned CVRqualifier = ElementType.getCVRQualifiers();
Eli Friedman1e692ac2008-06-13 23:01:12 +0000394
Chris Lattnerf81557c2008-04-04 18:42:16 +0000395 for (uint64_t i = 0; i != NumArrayElements; ++i) {
396 llvm::Value *NextVal = Builder.CreateStructGEP(DestPtr, i, ".array");
397 if (i < NumInitElements)
Eli Friedman1e692ac2008-06-13 23:01:12 +0000398 EmitInitializationToLValue(E->getInit(i),
399 LValue::MakeAddr(NextVal, CVRqualifier));
Chris Lattnerf81557c2008-04-04 18:42:16 +0000400 else
Eli Friedman1e692ac2008-06-13 23:01:12 +0000401 EmitNullInitializationToLValue(LValue::MakeAddr(NextVal, CVRqualifier),
Chris Lattnerf81557c2008-04-04 18:42:16 +0000402 ElementType);
Lauro Ramos Venancio145cd892008-02-19 19:27:31 +0000403 }
Chris Lattnerf81557c2008-04-04 18:42:16 +0000404 return;
405 }
406
407 assert(E->getType()->isRecordType() && "Only support structs/unions here!");
408
409 // Do struct initialization; this code just sets each individual member
410 // to the approprate value. This makes bitfield support automatic;
411 // the disadvantage is that the generated code is more difficult for
412 // the optimizer, especially with bitfields.
413 unsigned NumInitElements = E->getNumInits();
414 RecordDecl *SD = E->getType()->getAsRecordType()->getDecl();
415 unsigned NumMembers = SD->getNumMembers() - SD->hasFlexibleArrayMember();
416 unsigned CurInitVal = 0;
417 bool isUnion = E->getType()->isUnionType();
418
419 // Here we iterate over the fields; this makes it simpler to both
420 // default-initialize fields and skip over unnamed fields.
421 for (unsigned CurFieldNo = 0; CurFieldNo != NumMembers; ++CurFieldNo) {
Chris Lattnerf81557c2008-04-04 18:42:16 +0000422 FieldDecl *CurField = SD->getMember(CurFieldNo);
423 if (CurField->getIdentifier() == 0) {
424 // Initializers can't initialize unnamed fields, e.g. "int : 20;"
425 continue;
426 }
Eli Friedman1e692ac2008-06-13 23:01:12 +0000427 // FIXME: volatility
428 LValue FieldLoc = CGF.EmitLValueForField(DestPtr, CurField, isUnion,0);
Chris Lattnerf81557c2008-04-04 18:42:16 +0000429 if (CurInitVal < NumInitElements) {
430 // Store the initializer into the field
431 // This will probably have to get a bit smarter when we support
432 // designators in initializers
433 EmitInitializationToLValue(E->getInit(CurInitVal++), FieldLoc);
434 } else {
435 // We're out of initalizers; default-initialize to null
436 EmitNullInitializationToLValue(FieldLoc, CurField->getType());
437 }
438
439 // Unions only initialize one field.
440 // (things can get weird with designators, but they aren't
441 // supported yet.)
442 if (E->getType()->isUnionType())
443 break;
Lauro Ramos Venancio145cd892008-02-19 19:27:31 +0000444 }
Devang Patel636c3d02007-10-26 17:44:44 +0000445}
446
Chris Lattneree755f92007-08-21 04:59:27 +0000447//===----------------------------------------------------------------------===//
448// Entry Points into this File
449//===----------------------------------------------------------------------===//
450
451/// EmitAggExpr - Emit the computation of the specified expression of
452/// aggregate type. The result is computed into DestPtr. Note that if
453/// DestPtr is null, the value of the aggregate expression is not needed.
454void CodeGenFunction::EmitAggExpr(const Expr *E, llvm::Value *DestPtr,
455 bool VolatileDest) {
456 assert(E && hasAggregateLLVMType(E->getType()) &&
457 "Invalid aggregate expression to emit");
458
459 AggExprEmitter(*this, DestPtr, VolatileDest).Visit(const_cast<Expr*>(E));
460}
Daniel Dunbar7482d122008-09-09 20:49:46 +0000461
462void CodeGenFunction::EmitAggregateClear(llvm::Value *DestPtr, QualType Ty) {
463 assert(!Ty->isAnyComplexType() && "Shouldn't happen for complex");
464
465 EmitMemSetToZero(DestPtr, Ty);
466}
467
468void CodeGenFunction::EmitAggregateCopy(llvm::Value *DestPtr,
469 llvm::Value *SrcPtr, QualType Ty) {
470 assert(!Ty->isAnyComplexType() && "Shouldn't happen for complex");
471
472 // Aggregate assignment turns into llvm.memmove.
473 const llvm::Type *BP = llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
474 if (DestPtr->getType() != BP)
475 DestPtr = Builder.CreateBitCast(DestPtr, BP, "tmp");
476 if (SrcPtr->getType() != BP)
477 SrcPtr = Builder.CreateBitCast(SrcPtr, BP, "tmp");
478
479 // Get size and alignment info for this aggregate.
480 std::pair<uint64_t, unsigned> TypeInfo = getContext().getTypeInfo(Ty);
481
482 // FIXME: Handle variable sized types.
483 const llvm::Type *IntPtr = llvm::IntegerType::get(LLVMPointerWidth);
484
485 Builder.CreateCall4(CGM.getMemMoveFn(),
486 DestPtr, SrcPtr,
487 // TypeInfo.first describes size in bits.
488 llvm::ConstantInt::get(IntPtr, TypeInfo.first/8),
489 llvm::ConstantInt::get(llvm::Type::Int32Ty,
490 TypeInfo.second/8));
491}