blob: e8ab1bbb7f35d7e769d45cedac7bf499a0148fa4 [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);
Chris Lattner9c033562007-08-21 04:25:47 +000090
91 void VisitConditionalOperator(const ConditionalOperator *CO);
Devang Patel636c3d02007-10-26 17:44:44 +000092 void VisitInitListExpr(InitListExpr *E);
Chris Lattner04421082008-04-08 04:40:51 +000093 void VisitCXXDefaultArgExpr(CXXDefaultArgExpr *DAE) {
94 Visit(DAE->getExpr());
95 }
Eli Friedmanb1851242008-05-27 15:51:49 +000096 void VisitVAArgExpr(VAArgExpr *E);
Chris Lattnerf81557c2008-04-04 18:42:16 +000097
98 void EmitInitializationToLValue(Expr *E, LValue Address);
99 void EmitNullInitializationToLValue(LValue Address, QualType T);
Chris Lattner9c033562007-08-21 04:25:47 +0000100 // case Expr::ChooseExprClass:
Lauro Ramos Venancio305762c2008-02-18 22:44:02 +0000101
Chris Lattner9c033562007-08-21 04:25:47 +0000102};
103} // end anonymous namespace.
104
Chris Lattneree755f92007-08-21 04:59:27 +0000105//===----------------------------------------------------------------------===//
106// Utilities
107//===----------------------------------------------------------------------===//
Chris Lattner9c033562007-08-21 04:25:47 +0000108
Chris Lattner883f6a72007-08-11 00:04:45 +0000109/// EmitAggLoadOfLValue - Given an expression with aggregate type that
110/// represents a value lvalue, this method emits the address of the lvalue,
111/// then loads the result into DestPtr.
Chris Lattner9c033562007-08-21 04:25:47 +0000112void AggExprEmitter::EmitAggLoadOfLValue(const Expr *E) {
113 LValue LV = CGF.EmitLValue(E);
Chris Lattner883f6a72007-08-11 00:04:45 +0000114 assert(LV.isSimple() && "Can't have aggregate bitfield, vector, etc");
115 llvm::Value *SrcPtr = LV.getAddress();
116
117 // If the result is ignored, don't copy from the value.
118 if (DestPtr == 0)
119 // FIXME: If the source is volatile, we must read from it.
120 return;
121
Daniel Dunbar7482d122008-09-09 20:49:46 +0000122 CGF.EmitAggregateCopy(DestPtr, SrcPtr, E->getType());
Chris Lattner883f6a72007-08-11 00:04:45 +0000123}
124
Chris Lattneree755f92007-08-21 04:59:27 +0000125//===----------------------------------------------------------------------===//
126// Visitor Methods
127//===----------------------------------------------------------------------===//
128
Chris Lattner96196622008-07-26 22:37:01 +0000129void AggExprEmitter::VisitImplicitCastExpr(ImplicitCastExpr *E) {
Eli Friedmanff6e2b72008-02-11 01:09:17 +0000130 assert(CGF.getContext().typesAreCompatible(
Chris Lattner96196622008-07-26 22:37:01 +0000131 E->getSubExpr()->getType().getUnqualifiedType(),
132 E->getType().getUnqualifiedType()) &&
133 "Implicit cast types must be compatible");
Anders Carlssone4707ff2008-01-14 06:28:57 +0000134 Visit(E->getSubExpr());
135}
136
Chris Lattner96196622008-07-26 22:37:01 +0000137void AggExprEmitter::VisitCallExpr(const CallExpr *E) {
Anders Carlsson148fe672007-10-31 22:04:46 +0000138 RValue RV = CGF.EmitCallExpr(E);
139 assert(RV.isAggregate() && "Return value must be aggregate value!");
140
141 // If the result is ignored, don't copy from the value.
142 if (DestPtr == 0)
143 // FIXME: If the source is volatile, we must read from it.
144 return;
145
Daniel Dunbar7482d122008-09-09 20:49:46 +0000146 CGF.EmitAggregateCopy(DestPtr, RV.getAggregateAddr(), E->getType());
Anders Carlsson148fe672007-10-31 22:04:46 +0000147}
Chris Lattner96196622008-07-26 22:37:01 +0000148
149void AggExprEmitter::VisitObjCMessageExpr(ObjCMessageExpr *E) {
Daniel Dunbar8f2926b2008-08-23 03:46:30 +0000150 RValue RV = CGF.EmitObjCMessageExpr(E);
151 assert(RV.isAggregate() && "Return value must be aggregate value!");
Chris Lattner8fdf3282008-06-24 17:04:18 +0000152
153 // If the result is ignored, don't copy from the value.
154 if (DestPtr == 0)
Daniel Dunbar8f2926b2008-08-23 03:46:30 +0000155 // FIXME: If the source is volatile, we must read from it.
Chris Lattner8fdf3282008-06-24 17:04:18 +0000156 return;
157
Daniel Dunbar7482d122008-09-09 20:49:46 +0000158 CGF.EmitAggregateCopy(DestPtr, RV.getAggregateAddr(), E->getType());
Chris Lattner8fdf3282008-06-24 17:04:18 +0000159}
Anders Carlsson148fe672007-10-31 22:04:46 +0000160
Daniel Dunbar9c3fc702008-08-27 06:57:25 +0000161void AggExprEmitter::VisitObjCPropertyRefExpr(ObjCPropertyRefExpr *E) {
162 RValue RV = CGF.EmitObjCPropertyGet(E);
163 assert(RV.isAggregate() && "Return value must be aggregate value!");
164
165 // If the result is ignored, don't copy from the value.
166 if (DestPtr == 0)
167 // FIXME: If the source is volatile, we must read from it.
168 return;
169
Daniel Dunbar7482d122008-09-09 20:49:46 +0000170 CGF.EmitAggregateCopy(DestPtr, RV.getAggregateAddr(), E->getType());
Daniel Dunbar9c3fc702008-08-27 06:57:25 +0000171}
172
Chris Lattner96196622008-07-26 22:37:01 +0000173void AggExprEmitter::VisitOverloadExpr(const OverloadExpr *E) {
Nate Begeman796ef3d2008-01-31 05:38:29 +0000174 RValue RV = CGF.EmitCallExpr(E->getFn(), E->arg_begin(),
Ted Kremenek55499762008-06-17 02:43:46 +0000175 E->arg_end(CGF.getContext()));
176
Nate Begeman796ef3d2008-01-31 05:38:29 +0000177 assert(RV.isAggregate() && "Return value must be aggregate value!");
178
179 // If the result is ignored, don't copy from the value.
180 if (DestPtr == 0)
181 // FIXME: If the source is volatile, we must read from it.
182 return;
183
Daniel Dunbar7482d122008-09-09 20:49:46 +0000184 CGF.EmitAggregateCopy(DestPtr, RV.getAggregateAddr(), E->getType());
Nate Begeman796ef3d2008-01-31 05:38:29 +0000185}
186
Chris Lattner96196622008-07-26 22:37:01 +0000187void AggExprEmitter::VisitBinComma(const BinaryOperator *E) {
Eli Friedman07fa52a2008-05-20 07:56:31 +0000188 CGF.EmitAnyExpr(E->getLHS());
189 CGF.EmitAggExpr(E->getRHS(), DestPtr, false);
190}
191
Chris Lattnerb2d963f2007-08-31 22:54:14 +0000192void AggExprEmitter::VisitStmtExpr(const StmtExpr *E) {
193 CGF.EmitCompoundStmt(*E->getSubStmt(), true, DestPtr, VolatileDest);
194}
195
Chris Lattner9c033562007-08-21 04:25:47 +0000196void AggExprEmitter::VisitBinaryOperator(const BinaryOperator *E) {
Daniel Dunbar488e9932008-08-16 00:56:44 +0000197 CGF.ErrorUnsupported(E, "aggregate binary expression");
Chris Lattneree755f92007-08-21 04:59:27 +0000198}
199
Chris Lattner03d6fb92007-08-21 04:43:17 +0000200void AggExprEmitter::VisitBinAssign(const BinaryOperator *E) {
Eli Friedmanff6e2b72008-02-11 01:09:17 +0000201 // For an assignment to work, the value on the right has
202 // to be compatible with the value on the left.
203 assert(CGF.getContext().typesAreCompatible(
204 E->getLHS()->getType().getUnqualifiedType(),
205 E->getRHS()->getType().getUnqualifiedType())
206 && "Invalid assignment");
Chris Lattner9c033562007-08-21 04:25:47 +0000207 LValue LHS = CGF.EmitLValue(E->getLHS());
Chris Lattner883f6a72007-08-11 00:04:45 +0000208
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000209 // We have to special case property setters, otherwise we must have
210 // a simple lvalue (no aggregates inside vectors, bitfields).
211 if (LHS.isPropertyRef()) {
212 // FIXME: Volatility?
213 llvm::Value *AggLoc = DestPtr;
214 if (!AggLoc)
215 AggLoc = CGF.CreateTempAlloca(CGF.ConvertType(E->getRHS()->getType()));
216 CGF.EmitAggExpr(E->getRHS(), AggLoc, false);
217 CGF.EmitObjCPropertySet(LHS.getPropertyRefExpr(),
218 RValue::getAggregate(AggLoc));
219 } else {
220 // Codegen the RHS so that it stores directly into the LHS.
221 CGF.EmitAggExpr(E->getRHS(), LHS.getAddress(), false /*FIXME: VOLATILE LHS*/);
222
223 if (DestPtr == 0)
224 return;
225
226 // If the result of the assignment is used, copy the RHS there also.
Daniel Dunbar7482d122008-09-09 20:49:46 +0000227 CGF.EmitAggregateCopy(DestPtr, LHS.getAddress(), E->getType());
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000228 }
Chris Lattner883f6a72007-08-11 00:04:45 +0000229}
230
Chris Lattner9c033562007-08-21 04:25:47 +0000231void AggExprEmitter::VisitConditionalOperator(const ConditionalOperator *E) {
Gabor Greif984d0b42008-04-06 20:42:52 +0000232 llvm::BasicBlock *LHSBlock = llvm::BasicBlock::Create("cond.?");
233 llvm::BasicBlock *RHSBlock = llvm::BasicBlock::Create("cond.:");
234 llvm::BasicBlock *ContBlock = llvm::BasicBlock::Create("cond.cont");
Chris Lattner883f6a72007-08-11 00:04:45 +0000235
Chris Lattner9c033562007-08-21 04:25:47 +0000236 llvm::Value *Cond = CGF.EvaluateExprAsBool(E->getCond());
Chris Lattnerbfc0c1a2007-08-26 23:13:56 +0000237 Builder.CreateCondBr(Cond, LHSBlock, RHSBlock);
Chris Lattner883f6a72007-08-11 00:04:45 +0000238
Chris Lattner9c033562007-08-21 04:25:47 +0000239 CGF.EmitBlock(LHSBlock);
Chris Lattner883f6a72007-08-11 00:04:45 +0000240
241 // Handle the GNU extension for missing LHS.
242 assert(E->getLHS() && "Must have LHS for aggregate value");
243
Chris Lattnerc748f272007-08-21 05:02:10 +0000244 Visit(E->getLHS());
Chris Lattnerbfc0c1a2007-08-26 23:13:56 +0000245 Builder.CreateBr(ContBlock);
246 LHSBlock = Builder.GetInsertBlock();
Chris Lattner883f6a72007-08-11 00:04:45 +0000247
Chris Lattner9c033562007-08-21 04:25:47 +0000248 CGF.EmitBlock(RHSBlock);
Chris Lattner883f6a72007-08-11 00:04:45 +0000249
Chris Lattnerc748f272007-08-21 05:02:10 +0000250 Visit(E->getRHS());
Chris Lattnerbfc0c1a2007-08-26 23:13:56 +0000251 Builder.CreateBr(ContBlock);
252 RHSBlock = Builder.GetInsertBlock();
Chris Lattner883f6a72007-08-11 00:04:45 +0000253
Chris Lattner9c033562007-08-21 04:25:47 +0000254 CGF.EmitBlock(ContBlock);
Chris Lattner883f6a72007-08-11 00:04:45 +0000255}
Chris Lattneree755f92007-08-21 04:59:27 +0000256
Eli Friedmanb1851242008-05-27 15:51:49 +0000257void AggExprEmitter::VisitVAArgExpr(VAArgExpr *VE) {
258 llvm::Value *ArgValue = CGF.EmitLValue(VE->getSubExpr()).getAddress();
Anders Carlssonddf7cac2008-11-04 05:30:00 +0000259 llvm::Value *ArgPtr = CGF.EmitVAArg(ArgValue, VE->getType());
260
261 if (!ArgPtr)
262 CGF.ErrorUnsupported(VE, "aggregate va_arg expression");
263
Eli Friedmanb1851242008-05-27 15:51:49 +0000264 if (DestPtr)
Eli Friedman1e692ac2008-06-13 23:01:12 +0000265 // FIXME: volatility
Anders Carlssonddf7cac2008-11-04 05:30:00 +0000266 CGF.EmitAggregateCopy(DestPtr, ArgPtr, VE->getType());
Eli Friedmanb1851242008-05-27 15:51:49 +0000267}
268
Lauro Ramos Venancio13e22cf2008-02-19 22:04:22 +0000269void AggExprEmitter::EmitNonConstInit(InitListExpr *E) {
Chris Lattnerbe20bb52008-10-26 23:53:12 +0000270 if (E->hadDesignators()) {
271 CGF.ErrorUnsupported(E, "initializer list with designators");
272 return;
273 }
274
Lauro Ramos Venancio13e22cf2008-02-19 22:04:22 +0000275 const llvm::PointerType *APType =
276 cast<llvm::PointerType>(DestPtr->getType());
277 const llvm::Type *DestType = APType->getElementType();
Lauro Ramos Venancio305762c2008-02-18 22:44:02 +0000278
279 if (const llvm::ArrayType *AType = dyn_cast<llvm::ArrayType>(DestType)) {
Lauro Ramos Venancio13e22cf2008-02-19 22:04:22 +0000280 unsigned NumInitElements = E->getNumInits();
Lauro Ramos Venancio305762c2008-02-18 22:44:02 +0000281
Lauro Ramos Venancio305762c2008-02-18 22:44:02 +0000282 unsigned i;
283 for (i = 0; i != NumInitElements; ++i) {
Chris Lattner36b6a0a2008-03-19 05:19:41 +0000284 llvm::Value *NextVal = Builder.CreateStructGEP(DestPtr, i, ".array");
Lauro Ramos Venancio13e22cf2008-02-19 22:04:22 +0000285 Expr *Init = E->getInit(i);
286 if (isa<InitListExpr>(Init))
287 CGF.EmitAggExpr(Init, NextVal, VolatileDest);
288 else
Eli Friedman1e692ac2008-06-13 23:01:12 +0000289 // FIXME: volatility
Lauro Ramos Venancio13e22cf2008-02-19 22:04:22 +0000290 Builder.CreateStore(CGF.EmitScalarExpr(Init), NextVal);
Lauro Ramos Venancio305762c2008-02-18 22:44:02 +0000291 }
292
293 // Emit remaining default initializers
294 unsigned NumArrayElements = AType->getNumElements();
Lauro Ramos Venancio13e22cf2008-02-19 22:04:22 +0000295 QualType QType = E->getInit(0)->getType();
296 const llvm::Type *EType = AType->getElementType();
Lauro Ramos Venancio305762c2008-02-18 22:44:02 +0000297 for (/*Do not initialize i*/; i < NumArrayElements; ++i) {
Chris Lattner36b6a0a2008-03-19 05:19:41 +0000298 llvm::Value *NextVal = Builder.CreateStructGEP(DestPtr, i, ".array");
Dan Gohmand79a7262008-05-22 22:12:56 +0000299 if (EType->isSingleValueType())
Eli Friedman1e692ac2008-06-13 23:01:12 +0000300 // FIXME: volatility
Lauro Ramos Venancio13e22cf2008-02-19 22:04:22 +0000301 Builder.CreateStore(llvm::Constant::getNullValue(EType), NextVal);
302 else
Daniel Dunbar7482d122008-09-09 20:49:46 +0000303 CGF.EmitAggregateClear(NextVal, QType);
Lauro Ramos Venancio305762c2008-02-18 22:44:02 +0000304 }
Lauro Ramos Venancio13e22cf2008-02-19 22:04:22 +0000305 } else
306 assert(false && "Invalid initializer");
Lauro Ramos Venancio305762c2008-02-18 22:44:02 +0000307}
308
Chris Lattnerf81557c2008-04-04 18:42:16 +0000309void AggExprEmitter::EmitInitializationToLValue(Expr* E, LValue LV) {
310 // FIXME: Are initializers affected by volatile?
311 if (E->getType()->isComplexType()) {
312 CGF.EmitComplexExprIntoAddr(E, LV.getAddress(), false);
Eli Friedmanc8ba9612008-05-12 15:06:05 +0000313 } else if (CGF.hasAggregateLLVMType(E->getType())) {
314 CGF.EmitAnyExpr(E, LV.getAddress(), false);
315 } else {
316 CGF.EmitStoreThroughLValue(CGF.EmitAnyExpr(E), LV, E->getType());
Chris Lattnerf81557c2008-04-04 18:42:16 +0000317 }
Chris Lattnerf81557c2008-04-04 18:42:16 +0000318}
319
320void AggExprEmitter::EmitNullInitializationToLValue(LValue LV, QualType T) {
321 if (!CGF.hasAggregateLLVMType(T)) {
322 // For non-aggregates, we can store zero
Daniel Dunbar82397132008-08-06 05:32:55 +0000323 llvm::Value *Null = llvm::Constant::getNullValue(CGF.ConvertType(T));
324 CGF.EmitStoreThroughLValue(RValue::get(Null), LV, T);
Lauro Ramos Venancio145cd892008-02-19 19:27:31 +0000325 } else {
Chris Lattnerf81557c2008-04-04 18:42:16 +0000326 // Otherwise, just memset the whole thing to zero. This is legal
327 // because in LLVM, all default initializers are guaranteed to have a
328 // bit pattern of all zeros.
329 // There's a potential optimization opportunity in combining
330 // memsets; that would be easy for arrays, but relatively
331 // difficult for structures with the current code.
332 llvm::Value *MemSet = CGF.CGM.getIntrinsic(llvm::Intrinsic::memset_i64);
333 uint64_t Size = CGF.getContext().getTypeSize(T);
334
335 const llvm::Type *BP = llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
336 llvm::Value* DestPtr = Builder.CreateBitCast(LV.getAddress(), BP, "tmp");
Chris Lattner3eae03e2008-05-06 00:56:42 +0000337 Builder.CreateCall4(MemSet, DestPtr,
338 llvm::ConstantInt::get(llvm::Type::Int8Ty, 0),
339 llvm::ConstantInt::get(llvm::Type::Int64Ty, Size/8),
340 llvm::ConstantInt::get(llvm::Type::Int32Ty, 0));
Chris Lattnerf81557c2008-04-04 18:42:16 +0000341 }
342}
343
Chris Lattnerf81557c2008-04-04 18:42:16 +0000344void AggExprEmitter::VisitInitListExpr(InitListExpr *E) {
Chris Lattnerbe20bb52008-10-26 23:53:12 +0000345 if (E->hadDesignators()) {
346 CGF.ErrorUnsupported(E, "initializer list with designators");
347 return;
348 }
349
Daniel Dunbarbe38d022008-08-23 18:44:10 +0000350 // FIXME: For constant expressions, call into const expr emitter so
351 // that we can emit a memcpy instead of storing the individual
352 // members. This is purely for perf; both codepaths lead to
353 // equivalent (although not necessarily identical) code. It's worth
354 // noting that LLVM keeps on getting smarter, though, so it might
355 // not be worth bothering.
Chris Lattnerf81557c2008-04-04 18:42:16 +0000356
357 // Handle initialization of an array.
358 if (E->getType()->isArrayType()) {
359 const llvm::PointerType *APType =
360 cast<llvm::PointerType>(DestPtr->getType());
361 const llvm::ArrayType *AType =
362 cast<llvm::ArrayType>(APType->getElementType());
363
364 uint64_t NumInitElements = E->getNumInits();
Eli Friedman922696f2008-05-19 17:51:16 +0000365
Chris Lattner96196622008-07-26 22:37:01 +0000366 if (E->getNumInits() > 0) {
367 QualType T1 = E->getType();
368 QualType T2 = E->getInit(0)->getType();
369 if (CGF.getContext().getCanonicalType(T1).getUnqualifiedType() ==
370 CGF.getContext().getCanonicalType(T2).getUnqualifiedType()) {
371 EmitAggLoadOfLValue(E->getInit(0));
372 return;
373 }
Eli Friedman922696f2008-05-19 17:51:16 +0000374 }
375
Chris Lattnerf81557c2008-04-04 18:42:16 +0000376 uint64_t NumArrayElements = AType->getNumElements();
Chris Lattnerc63a1f22008-08-04 07:31:14 +0000377 QualType ElementType = CGF.getContext().getCanonicalType(E->getType());
378 ElementType =CGF.getContext().getAsArrayType(ElementType)->getElementType();
Chris Lattnerf81557c2008-04-04 18:42:16 +0000379
Chris Lattnerc63a1f22008-08-04 07:31:14 +0000380 unsigned CVRqualifier = ElementType.getCVRQualifiers();
Eli Friedman1e692ac2008-06-13 23:01:12 +0000381
Chris Lattnerf81557c2008-04-04 18:42:16 +0000382 for (uint64_t i = 0; i != NumArrayElements; ++i) {
383 llvm::Value *NextVal = Builder.CreateStructGEP(DestPtr, i, ".array");
384 if (i < NumInitElements)
Eli Friedman1e692ac2008-06-13 23:01:12 +0000385 EmitInitializationToLValue(E->getInit(i),
386 LValue::MakeAddr(NextVal, CVRqualifier));
Chris Lattnerf81557c2008-04-04 18:42:16 +0000387 else
Eli Friedman1e692ac2008-06-13 23:01:12 +0000388 EmitNullInitializationToLValue(LValue::MakeAddr(NextVal, CVRqualifier),
Chris Lattnerf81557c2008-04-04 18:42:16 +0000389 ElementType);
Lauro Ramos Venancio145cd892008-02-19 19:27:31 +0000390 }
Chris Lattnerf81557c2008-04-04 18:42:16 +0000391 return;
392 }
393
394 assert(E->getType()->isRecordType() && "Only support structs/unions here!");
395
396 // Do struct initialization; this code just sets each individual member
397 // to the approprate value. This makes bitfield support automatic;
398 // the disadvantage is that the generated code is more difficult for
399 // the optimizer, especially with bitfields.
400 unsigned NumInitElements = E->getNumInits();
401 RecordDecl *SD = E->getType()->getAsRecordType()->getDecl();
402 unsigned NumMembers = SD->getNumMembers() - SD->hasFlexibleArrayMember();
403 unsigned CurInitVal = 0;
404 bool isUnion = E->getType()->isUnionType();
405
406 // Here we iterate over the fields; this makes it simpler to both
407 // default-initialize fields and skip over unnamed fields.
408 for (unsigned CurFieldNo = 0; CurFieldNo != NumMembers; ++CurFieldNo) {
Chris Lattnerf81557c2008-04-04 18:42:16 +0000409 FieldDecl *CurField = SD->getMember(CurFieldNo);
410 if (CurField->getIdentifier() == 0) {
411 // Initializers can't initialize unnamed fields, e.g. "int : 20;"
412 continue;
413 }
Eli Friedman1e692ac2008-06-13 23:01:12 +0000414 // FIXME: volatility
415 LValue FieldLoc = CGF.EmitLValueForField(DestPtr, CurField, isUnion,0);
Chris Lattnerf81557c2008-04-04 18:42:16 +0000416 if (CurInitVal < NumInitElements) {
417 // Store the initializer into the field
418 // This will probably have to get a bit smarter when we support
419 // designators in initializers
420 EmitInitializationToLValue(E->getInit(CurInitVal++), FieldLoc);
421 } else {
422 // We're out of initalizers; default-initialize to null
423 EmitNullInitializationToLValue(FieldLoc, CurField->getType());
424 }
425
426 // Unions only initialize one field.
427 // (things can get weird with designators, but they aren't
428 // supported yet.)
429 if (E->getType()->isUnionType())
430 break;
Lauro Ramos Venancio145cd892008-02-19 19:27:31 +0000431 }
Devang Patel636c3d02007-10-26 17:44:44 +0000432}
433
Chris Lattneree755f92007-08-21 04:59:27 +0000434//===----------------------------------------------------------------------===//
435// Entry Points into this File
436//===----------------------------------------------------------------------===//
437
438/// EmitAggExpr - Emit the computation of the specified expression of
439/// aggregate type. The result is computed into DestPtr. Note that if
440/// DestPtr is null, the value of the aggregate expression is not needed.
441void CodeGenFunction::EmitAggExpr(const Expr *E, llvm::Value *DestPtr,
442 bool VolatileDest) {
443 assert(E && hasAggregateLLVMType(E->getType()) &&
444 "Invalid aggregate expression to emit");
445
446 AggExprEmitter(*this, DestPtr, VolatileDest).Visit(const_cast<Expr*>(E));
447}
Daniel Dunbar7482d122008-09-09 20:49:46 +0000448
449void CodeGenFunction::EmitAggregateClear(llvm::Value *DestPtr, QualType Ty) {
450 assert(!Ty->isAnyComplexType() && "Shouldn't happen for complex");
451
452 EmitMemSetToZero(DestPtr, Ty);
453}
454
455void CodeGenFunction::EmitAggregateCopy(llvm::Value *DestPtr,
456 llvm::Value *SrcPtr, QualType Ty) {
457 assert(!Ty->isAnyComplexType() && "Shouldn't happen for complex");
458
459 // Aggregate assignment turns into llvm.memmove.
460 const llvm::Type *BP = llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
461 if (DestPtr->getType() != BP)
462 DestPtr = Builder.CreateBitCast(DestPtr, BP, "tmp");
463 if (SrcPtr->getType() != BP)
464 SrcPtr = Builder.CreateBitCast(SrcPtr, BP, "tmp");
465
466 // Get size and alignment info for this aggregate.
467 std::pair<uint64_t, unsigned> TypeInfo = getContext().getTypeInfo(Ty);
468
469 // FIXME: Handle variable sized types.
470 const llvm::Type *IntPtr = llvm::IntegerType::get(LLVMPointerWidth);
471
472 Builder.CreateCall4(CGM.getMemMoveFn(),
473 DestPtr, SrcPtr,
474 // TypeInfo.first describes size in bits.
475 llvm::ConstantInt::get(IntPtr, TypeInfo.first/8),
476 llvm::ConstantInt::get(llvm::Type::Int32Ty,
477 TypeInfo.second/8));
478}