blob: fbba9c6c0c407ac2737dc46c52c1407d366e3097 [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;
Chris Lattner85e35682008-08-08 19:57:58 +000033 llvm::IRBuilder<> &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
Chris Lattnerbfc0c1a2007-08-26 23:13:56 +000051 void EmitAggregateCopy(llvm::Value *DestPtr, llvm::Value *SrcPtr,
52 QualType EltTy);
Lauro Ramos Venancio13e22cf2008-02-19 22:04:22 +000053
54 void EmitAggregateClear(llvm::Value *DestPtr, QualType Ty);
55
56 void EmitNonConstInit(InitListExpr *E);
Eli Friedman922696f2008-05-19 17:51:16 +000057
Chris Lattneree755f92007-08-21 04:59:27 +000058 //===--------------------------------------------------------------------===//
59 // Visitor Methods
60 //===--------------------------------------------------------------------===//
61
Chris Lattner9c033562007-08-21 04:25:47 +000062 void VisitStmt(Stmt *S) {
Daniel Dunbar488e9932008-08-16 00:56:44 +000063 CGF.ErrorUnsupported(S, "aggregate expression");
Chris Lattner9c033562007-08-21 04:25:47 +000064 }
65 void VisitParenExpr(ParenExpr *PE) { Visit(PE->getSubExpr()); }
66
67 // l-values.
Seo Sanghyeon9b73b392007-12-14 02:04:12 +000068 void VisitDeclRefExpr(DeclRefExpr *DRE) { EmitAggLoadOfLValue(DRE); }
69 void VisitMemberExpr(MemberExpr *ME) { EmitAggLoadOfLValue(ME); }
70 void VisitUnaryDeref(UnaryOperator *E) { EmitAggLoadOfLValue(E); }
Seo Sanghyeonad6ebd62007-12-23 03:11:58 +000071 void VisitStringLiteral(StringLiteral *E) { EmitAggLoadOfLValue(E); }
Eli Friedmanb1851242008-05-27 15:51:49 +000072 void VisitCompoundLiteralExpr(CompoundLiteralExpr *E)
73 { EmitAggLoadOfLValue(E); }
Seo Sanghyeon9b73b392007-12-14 02:04:12 +000074
75 void VisitArraySubscriptExpr(ArraySubscriptExpr *E) {
76 EmitAggLoadOfLValue(E);
77 }
Chris Lattner9c033562007-08-21 04:25:47 +000078
79 // Operators.
80 // case Expr::UnaryOperatorClass:
Chris Lattner9c033562007-08-21 04:25:47 +000081 // case Expr::CastExprClass:
Anders Carlssone4707ff2008-01-14 06:28:57 +000082 void VisitImplicitCastExpr(ImplicitCastExpr *E);
Anders Carlsson148fe672007-10-31 22:04:46 +000083 void VisitCallExpr(const CallExpr *E);
Chris Lattnerb2d963f2007-08-31 22:54:14 +000084 void VisitStmtExpr(const StmtExpr *E);
Chris Lattner9c033562007-08-21 04:25:47 +000085 void VisitBinaryOperator(const BinaryOperator *BO);
Chris Lattner03d6fb92007-08-21 04:43:17 +000086 void VisitBinAssign(const BinaryOperator *E);
Nate Begeman796ef3d2008-01-31 05:38:29 +000087 void VisitOverloadExpr(const OverloadExpr *E);
Eli Friedman07fa52a2008-05-20 07:56:31 +000088 void VisitBinComma(const BinaryOperator *E);
Chris Lattner9c033562007-08-21 04:25:47 +000089
Chris Lattner8fdf3282008-06-24 17:04:18 +000090 void VisitObjCMessageExpr(ObjCMessageExpr *E);
Daniel Dunbar0a04d772008-08-23 10:51:21 +000091 void VisitObjCIvarRefExpr(ObjCIvarRefExpr *E) {
92 EmitAggLoadOfLValue(E);
93 }
94 void VisitObjCPropertyRefExpr(ObjCPropertyRefExpr *E) {
95 // FIXME: Implement!
96 CGF.ErrorUnsupported(E, "aggregate expression (Objective-C property reference)");
97 }
Chris Lattner9c033562007-08-21 04:25:47 +000098
99 void VisitConditionalOperator(const ConditionalOperator *CO);
Devang Patel636c3d02007-10-26 17:44:44 +0000100 void VisitInitListExpr(InitListExpr *E);
Chris Lattner04421082008-04-08 04:40:51 +0000101 void VisitCXXDefaultArgExpr(CXXDefaultArgExpr *DAE) {
102 Visit(DAE->getExpr());
103 }
Eli Friedmanb1851242008-05-27 15:51:49 +0000104 void VisitVAArgExpr(VAArgExpr *E);
Chris Lattnerf81557c2008-04-04 18:42:16 +0000105
106 void EmitInitializationToLValue(Expr *E, LValue Address);
107 void EmitNullInitializationToLValue(LValue Address, QualType T);
Chris Lattner9c033562007-08-21 04:25:47 +0000108 // case Expr::ChooseExprClass:
Lauro Ramos Venancio305762c2008-02-18 22:44:02 +0000109
Chris Lattner9c033562007-08-21 04:25:47 +0000110};
111} // end anonymous namespace.
112
Chris Lattneree755f92007-08-21 04:59:27 +0000113//===----------------------------------------------------------------------===//
114// Utilities
115//===----------------------------------------------------------------------===//
Chris Lattner9c033562007-08-21 04:25:47 +0000116
Lauro Ramos Venancio13e22cf2008-02-19 22:04:22 +0000117void AggExprEmitter::EmitAggregateClear(llvm::Value *DestPtr, QualType Ty) {
Chris Lattner9b2dc282008-04-04 16:54:41 +0000118 assert(!Ty->isAnyComplexType() && "Shouldn't happen for complex");
Lauro Ramos Venancio13e22cf2008-02-19 22:04:22 +0000119
120 // Aggregate assignment turns into llvm.memset.
121 const llvm::Type *BP = llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
122 if (DestPtr->getType() != BP)
123 DestPtr = Builder.CreateBitCast(DestPtr, BP, "tmp");
124
125 // Get size and alignment info for this aggregate.
Chris Lattner98be4942008-03-05 18:54:05 +0000126 std::pair<uint64_t, unsigned> TypeInfo = CGF.getContext().getTypeInfo(Ty);
Lauro Ramos Venancio13e22cf2008-02-19 22:04:22 +0000127
128 // FIXME: Handle variable sized types.
129 const llvm::Type *IntPtr = llvm::IntegerType::get(CGF.LLVMPointerWidth);
130
131 llvm::Value *MemSetOps[4] = {
132 DestPtr,
133 llvm::ConstantInt::getNullValue(llvm::Type::Int8Ty),
134 // TypeInfo.first describes size in bits.
135 llvm::ConstantInt::get(IntPtr, TypeInfo.first/8),
136 llvm::ConstantInt::get(llvm::Type::Int32Ty, TypeInfo.second/8)
137 };
138
139 Builder.CreateCall(CGF.CGM.getMemSetFn(), MemSetOps, MemSetOps+4);
140}
141
Chris Lattnerbfc0c1a2007-08-26 23:13:56 +0000142void AggExprEmitter::EmitAggregateCopy(llvm::Value *DestPtr,
143 llvm::Value *SrcPtr, QualType Ty) {
Chris Lattner9b2dc282008-04-04 16:54:41 +0000144 assert(!Ty->isAnyComplexType() && "Shouldn't happen for complex");
Chris Lattnerbfc0c1a2007-08-26 23:13:56 +0000145
Eli Friedman0c995092008-05-26 12:59:39 +0000146 // Aggregate assignment turns into llvm.memmove.
Christopher Lambddc23f32007-12-17 01:11:20 +0000147 const llvm::Type *BP = llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
Chris Lattnerbfc0c1a2007-08-26 23:13:56 +0000148 if (DestPtr->getType() != BP)
149 DestPtr = Builder.CreateBitCast(DestPtr, BP, "tmp");
150 if (SrcPtr->getType() != BP)
151 SrcPtr = Builder.CreateBitCast(SrcPtr, BP, "tmp");
152
153 // Get size and alignment info for this aggregate.
Chris Lattner98be4942008-03-05 18:54:05 +0000154 std::pair<uint64_t, unsigned> TypeInfo = CGF.getContext().getTypeInfo(Ty);
Chris Lattnerbfc0c1a2007-08-26 23:13:56 +0000155
156 // FIXME: Handle variable sized types.
157 const llvm::Type *IntPtr = llvm::IntegerType::get(CGF.LLVMPointerWidth);
158
Eli Friedman0c995092008-05-26 12:59:39 +0000159 llvm::Value *MemMoveOps[4] = {
Chris Lattnerbfc0c1a2007-08-26 23:13:56 +0000160 DestPtr, SrcPtr,
Devang Patel636c3d02007-10-26 17:44:44 +0000161 // TypeInfo.first describes size in bits.
162 llvm::ConstantInt::get(IntPtr, TypeInfo.first/8),
Lauro Ramos Venancio13e22cf2008-02-19 22:04:22 +0000163 llvm::ConstantInt::get(llvm::Type::Int32Ty, TypeInfo.second/8)
Chris Lattnerbfc0c1a2007-08-26 23:13:56 +0000164 };
165
Eli Friedman0c995092008-05-26 12:59:39 +0000166 Builder.CreateCall(CGF.CGM.getMemMoveFn(), MemMoveOps, MemMoveOps+4);
Chris Lattnerbfc0c1a2007-08-26 23:13:56 +0000167}
168
169
Chris Lattner883f6a72007-08-11 00:04:45 +0000170/// EmitAggLoadOfLValue - Given an expression with aggregate type that
171/// represents a value lvalue, this method emits the address of the lvalue,
172/// then loads the result into DestPtr.
Chris Lattner9c033562007-08-21 04:25:47 +0000173void AggExprEmitter::EmitAggLoadOfLValue(const Expr *E) {
174 LValue LV = CGF.EmitLValue(E);
Chris Lattner883f6a72007-08-11 00:04:45 +0000175 assert(LV.isSimple() && "Can't have aggregate bitfield, vector, etc");
176 llvm::Value *SrcPtr = LV.getAddress();
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
Chris Lattnerbfc0c1a2007-08-26 23:13:56 +0000183 EmitAggregateCopy(DestPtr, SrcPtr, E->getType());
Chris Lattner883f6a72007-08-11 00:04:45 +0000184}
185
Chris Lattneree755f92007-08-21 04:59:27 +0000186//===----------------------------------------------------------------------===//
187// Visitor Methods
188//===----------------------------------------------------------------------===//
189
Chris Lattner96196622008-07-26 22:37:01 +0000190void AggExprEmitter::VisitImplicitCastExpr(ImplicitCastExpr *E) {
Eli Friedmanff6e2b72008-02-11 01:09:17 +0000191 assert(CGF.getContext().typesAreCompatible(
Chris Lattner96196622008-07-26 22:37:01 +0000192 E->getSubExpr()->getType().getUnqualifiedType(),
193 E->getType().getUnqualifiedType()) &&
194 "Implicit cast types must be compatible");
Anders Carlssone4707ff2008-01-14 06:28:57 +0000195 Visit(E->getSubExpr());
196}
197
Chris Lattner96196622008-07-26 22:37:01 +0000198void AggExprEmitter::VisitCallExpr(const CallExpr *E) {
Anders Carlsson148fe672007-10-31 22:04:46 +0000199 RValue RV = CGF.EmitCallExpr(E);
200 assert(RV.isAggregate() && "Return value must be aggregate value!");
201
202 // If the result is ignored, don't copy from the value.
203 if (DestPtr == 0)
204 // FIXME: If the source is volatile, we must read from it.
205 return;
206
207 EmitAggregateCopy(DestPtr, RV.getAggregateAddr(), E->getType());
208}
Chris Lattner96196622008-07-26 22:37:01 +0000209
210void AggExprEmitter::VisitObjCMessageExpr(ObjCMessageExpr *E) {
Daniel Dunbar8f2926b2008-08-23 03:46:30 +0000211 RValue RV = CGF.EmitObjCMessageExpr(E);
212 assert(RV.isAggregate() && "Return value must be aggregate value!");
Chris Lattner8fdf3282008-06-24 17:04:18 +0000213
214 // If the result is ignored, don't copy from the value.
215 if (DestPtr == 0)
Daniel Dunbar8f2926b2008-08-23 03:46:30 +0000216 // FIXME: If the source is volatile, we must read from it.
Chris Lattner8fdf3282008-06-24 17:04:18 +0000217 return;
218
219 EmitAggregateCopy(DestPtr, RV.getAggregateAddr(), E->getType());
220}
Anders Carlsson148fe672007-10-31 22:04:46 +0000221
Chris Lattner96196622008-07-26 22:37:01 +0000222void AggExprEmitter::VisitOverloadExpr(const OverloadExpr *E) {
Nate Begeman796ef3d2008-01-31 05:38:29 +0000223 RValue RV = CGF.EmitCallExpr(E->getFn(), E->arg_begin(),
Ted Kremenek55499762008-06-17 02:43:46 +0000224 E->arg_end(CGF.getContext()));
225
Nate Begeman796ef3d2008-01-31 05:38:29 +0000226 assert(RV.isAggregate() && "Return value must be aggregate value!");
227
228 // If the result is ignored, don't copy from the value.
229 if (DestPtr == 0)
230 // FIXME: If the source is volatile, we must read from it.
231 return;
232
233 EmitAggregateCopy(DestPtr, RV.getAggregateAddr(), E->getType());
234}
235
Chris Lattner96196622008-07-26 22:37:01 +0000236void AggExprEmitter::VisitBinComma(const BinaryOperator *E) {
Eli Friedman07fa52a2008-05-20 07:56:31 +0000237 CGF.EmitAnyExpr(E->getLHS());
238 CGF.EmitAggExpr(E->getRHS(), DestPtr, false);
239}
240
Chris Lattnerb2d963f2007-08-31 22:54:14 +0000241void AggExprEmitter::VisitStmtExpr(const StmtExpr *E) {
242 CGF.EmitCompoundStmt(*E->getSubStmt(), true, DestPtr, VolatileDest);
243}
244
Chris Lattner9c033562007-08-21 04:25:47 +0000245void AggExprEmitter::VisitBinaryOperator(const BinaryOperator *E) {
Daniel Dunbar488e9932008-08-16 00:56:44 +0000246 CGF.ErrorUnsupported(E, "aggregate binary expression");
Chris Lattneree755f92007-08-21 04:59:27 +0000247}
248
Chris Lattner03d6fb92007-08-21 04:43:17 +0000249void AggExprEmitter::VisitBinAssign(const BinaryOperator *E) {
Eli Friedmanff6e2b72008-02-11 01:09:17 +0000250 // For an assignment to work, the value on the right has
251 // to be compatible with the value on the left.
252 assert(CGF.getContext().typesAreCompatible(
253 E->getLHS()->getType().getUnqualifiedType(),
254 E->getRHS()->getType().getUnqualifiedType())
255 && "Invalid assignment");
Chris Lattner9c033562007-08-21 04:25:47 +0000256 LValue LHS = CGF.EmitLValue(E->getLHS());
Chris Lattner883f6a72007-08-11 00:04:45 +0000257
258 // Codegen the RHS so that it stores directly into the LHS.
Chris Lattner9c033562007-08-21 04:25:47 +0000259 CGF.EmitAggExpr(E->getRHS(), LHS.getAddress(), false /*FIXME: VOLATILE LHS*/);
Chris Lattner883f6a72007-08-11 00:04:45 +0000260
Eli Friedmanff6e2b72008-02-11 01:09:17 +0000261 if (DestPtr == 0)
262 return;
263
Chris Lattner883f6a72007-08-11 00:04:45 +0000264 // If the result of the assignment is used, copy the RHS there also.
Eli Friedmanff6e2b72008-02-11 01:09:17 +0000265 EmitAggregateCopy(DestPtr, LHS.getAddress(), E->getType());
Chris Lattner883f6a72007-08-11 00:04:45 +0000266}
267
Chris Lattner9c033562007-08-21 04:25:47 +0000268void AggExprEmitter::VisitConditionalOperator(const ConditionalOperator *E) {
Gabor Greif984d0b42008-04-06 20:42:52 +0000269 llvm::BasicBlock *LHSBlock = llvm::BasicBlock::Create("cond.?");
270 llvm::BasicBlock *RHSBlock = llvm::BasicBlock::Create("cond.:");
271 llvm::BasicBlock *ContBlock = llvm::BasicBlock::Create("cond.cont");
Chris Lattner883f6a72007-08-11 00:04:45 +0000272
Chris Lattner9c033562007-08-21 04:25:47 +0000273 llvm::Value *Cond = CGF.EvaluateExprAsBool(E->getCond());
Chris Lattnerbfc0c1a2007-08-26 23:13:56 +0000274 Builder.CreateCondBr(Cond, LHSBlock, RHSBlock);
Chris Lattner883f6a72007-08-11 00:04:45 +0000275
Chris Lattner9c033562007-08-21 04:25:47 +0000276 CGF.EmitBlock(LHSBlock);
Chris Lattner883f6a72007-08-11 00:04:45 +0000277
278 // Handle the GNU extension for missing LHS.
279 assert(E->getLHS() && "Must have LHS for aggregate value");
280
Chris Lattnerc748f272007-08-21 05:02:10 +0000281 Visit(E->getLHS());
Chris Lattnerbfc0c1a2007-08-26 23:13:56 +0000282 Builder.CreateBr(ContBlock);
283 LHSBlock = Builder.GetInsertBlock();
Chris Lattner883f6a72007-08-11 00:04:45 +0000284
Chris Lattner9c033562007-08-21 04:25:47 +0000285 CGF.EmitBlock(RHSBlock);
Chris Lattner883f6a72007-08-11 00:04:45 +0000286
Chris Lattnerc748f272007-08-21 05:02:10 +0000287 Visit(E->getRHS());
Chris Lattnerbfc0c1a2007-08-26 23:13:56 +0000288 Builder.CreateBr(ContBlock);
289 RHSBlock = Builder.GetInsertBlock();
Chris Lattner883f6a72007-08-11 00:04:45 +0000290
Chris Lattner9c033562007-08-21 04:25:47 +0000291 CGF.EmitBlock(ContBlock);
Chris Lattner883f6a72007-08-11 00:04:45 +0000292}
Chris Lattneree755f92007-08-21 04:59:27 +0000293
Eli Friedmanb1851242008-05-27 15:51:49 +0000294void AggExprEmitter::VisitVAArgExpr(VAArgExpr *VE) {
295 llvm::Value *ArgValue = CGF.EmitLValue(VE->getSubExpr()).getAddress();
296 llvm::Value *V = Builder.CreateVAArg(ArgValue, CGF.ConvertType(VE->getType()));
297 if (DestPtr)
Eli Friedman1e692ac2008-06-13 23:01:12 +0000298 // FIXME: volatility
Eli Friedmanb1851242008-05-27 15:51:49 +0000299 Builder.CreateStore(V, DestPtr);
300}
301
Lauro Ramos Venancio13e22cf2008-02-19 22:04:22 +0000302void AggExprEmitter::EmitNonConstInit(InitListExpr *E) {
303
304 const llvm::PointerType *APType =
305 cast<llvm::PointerType>(DestPtr->getType());
306 const llvm::Type *DestType = APType->getElementType();
Lauro Ramos Venancio305762c2008-02-18 22:44:02 +0000307
308 if (const llvm::ArrayType *AType = dyn_cast<llvm::ArrayType>(DestType)) {
Lauro Ramos Venancio13e22cf2008-02-19 22:04:22 +0000309 unsigned NumInitElements = E->getNumInits();
Lauro Ramos Venancio305762c2008-02-18 22:44:02 +0000310
Lauro Ramos Venancio305762c2008-02-18 22:44:02 +0000311 unsigned i;
312 for (i = 0; i != NumInitElements; ++i) {
Chris Lattner36b6a0a2008-03-19 05:19:41 +0000313 llvm::Value *NextVal = Builder.CreateStructGEP(DestPtr, i, ".array");
Lauro Ramos Venancio13e22cf2008-02-19 22:04:22 +0000314 Expr *Init = E->getInit(i);
315 if (isa<InitListExpr>(Init))
316 CGF.EmitAggExpr(Init, NextVal, VolatileDest);
317 else
Eli Friedman1e692ac2008-06-13 23:01:12 +0000318 // FIXME: volatility
Lauro Ramos Venancio13e22cf2008-02-19 22:04:22 +0000319 Builder.CreateStore(CGF.EmitScalarExpr(Init), NextVal);
Lauro Ramos Venancio305762c2008-02-18 22:44:02 +0000320 }
321
322 // Emit remaining default initializers
323 unsigned NumArrayElements = AType->getNumElements();
Lauro Ramos Venancio13e22cf2008-02-19 22:04:22 +0000324 QualType QType = E->getInit(0)->getType();
325 const llvm::Type *EType = AType->getElementType();
Lauro Ramos Venancio305762c2008-02-18 22:44:02 +0000326 for (/*Do not initialize i*/; i < NumArrayElements; ++i) {
Chris Lattner36b6a0a2008-03-19 05:19:41 +0000327 llvm::Value *NextVal = Builder.CreateStructGEP(DestPtr, i, ".array");
Dan Gohmand79a7262008-05-22 22:12:56 +0000328 if (EType->isSingleValueType())
Eli Friedman1e692ac2008-06-13 23:01:12 +0000329 // FIXME: volatility
Lauro Ramos Venancio13e22cf2008-02-19 22:04:22 +0000330 Builder.CreateStore(llvm::Constant::getNullValue(EType), NextVal);
331 else
332 EmitAggregateClear(NextVal, QType);
Lauro Ramos Venancio305762c2008-02-18 22:44:02 +0000333 }
Lauro Ramos Venancio13e22cf2008-02-19 22:04:22 +0000334 } else
335 assert(false && "Invalid initializer");
Lauro Ramos Venancio305762c2008-02-18 22:44:02 +0000336}
337
Chris Lattnerf81557c2008-04-04 18:42:16 +0000338void AggExprEmitter::EmitInitializationToLValue(Expr* E, LValue LV) {
339 // FIXME: Are initializers affected by volatile?
340 if (E->getType()->isComplexType()) {
341 CGF.EmitComplexExprIntoAddr(E, LV.getAddress(), false);
Eli Friedmanc8ba9612008-05-12 15:06:05 +0000342 } else if (CGF.hasAggregateLLVMType(E->getType())) {
343 CGF.EmitAnyExpr(E, LV.getAddress(), false);
344 } else {
345 CGF.EmitStoreThroughLValue(CGF.EmitAnyExpr(E), LV, E->getType());
Chris Lattnerf81557c2008-04-04 18:42:16 +0000346 }
Chris Lattnerf81557c2008-04-04 18:42:16 +0000347}
348
349void AggExprEmitter::EmitNullInitializationToLValue(LValue LV, QualType T) {
350 if (!CGF.hasAggregateLLVMType(T)) {
351 // For non-aggregates, we can store zero
Daniel Dunbar82397132008-08-06 05:32:55 +0000352 llvm::Value *Null = llvm::Constant::getNullValue(CGF.ConvertType(T));
353 CGF.EmitStoreThroughLValue(RValue::get(Null), LV, T);
Lauro Ramos Venancio145cd892008-02-19 19:27:31 +0000354 } else {
Chris Lattnerf81557c2008-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 Lattner3eae03e2008-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 Lattnerf81557c2008-04-04 18:42:16 +0000370 }
371}
372
Chris Lattnerf81557c2008-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 Friedman922696f2008-05-19 17:51:16 +0000391
Chris Lattner96196622008-07-26 22:37:01 +0000392 if (E->getNumInits() > 0) {
393 QualType T1 = E->getType();
394 QualType T2 = E->getInit(0)->getType();
395 if (CGF.getContext().getCanonicalType(T1).getUnqualifiedType() ==
396 CGF.getContext().getCanonicalType(T2).getUnqualifiedType()) {
397 EmitAggLoadOfLValue(E->getInit(0));
398 return;
399 }
Eli Friedman922696f2008-05-19 17:51:16 +0000400 }
401
Chris Lattnerf81557c2008-04-04 18:42:16 +0000402 uint64_t NumArrayElements = AType->getNumElements();
Chris Lattnerc63a1f22008-08-04 07:31:14 +0000403 QualType ElementType = CGF.getContext().getCanonicalType(E->getType());
404 ElementType =CGF.getContext().getAsArrayType(ElementType)->getElementType();
Chris Lattnerf81557c2008-04-04 18:42:16 +0000405
Chris Lattnerc63a1f22008-08-04 07:31:14 +0000406 unsigned CVRqualifier = ElementType.getCVRQualifiers();
Eli Friedman1e692ac2008-06-13 23:01:12 +0000407
Chris Lattnerf81557c2008-04-04 18:42:16 +0000408 for (uint64_t i = 0; i != NumArrayElements; ++i) {
409 llvm::Value *NextVal = Builder.CreateStructGEP(DestPtr, i, ".array");
410 if (i < NumInitElements)
Eli Friedman1e692ac2008-06-13 23:01:12 +0000411 EmitInitializationToLValue(E->getInit(i),
412 LValue::MakeAddr(NextVal, CVRqualifier));
Chris Lattnerf81557c2008-04-04 18:42:16 +0000413 else
Eli Friedman1e692ac2008-06-13 23:01:12 +0000414 EmitNullInitializationToLValue(LValue::MakeAddr(NextVal, CVRqualifier),
Chris Lattnerf81557c2008-04-04 18:42:16 +0000415 ElementType);
Lauro Ramos Venancio145cd892008-02-19 19:27:31 +0000416 }
Chris Lattnerf81557c2008-04-04 18:42:16 +0000417 return;
418 }
419
420 assert(E->getType()->isRecordType() && "Only support structs/unions here!");
421
422 // Do struct initialization; this code just sets each individual member
423 // to the approprate value. This makes bitfield support automatic;
424 // the disadvantage is that the generated code is more difficult for
425 // the optimizer, especially with bitfields.
426 unsigned NumInitElements = E->getNumInits();
427 RecordDecl *SD = E->getType()->getAsRecordType()->getDecl();
428 unsigned NumMembers = SD->getNumMembers() - SD->hasFlexibleArrayMember();
429 unsigned CurInitVal = 0;
430 bool isUnion = E->getType()->isUnionType();
431
432 // Here we iterate over the fields; this makes it simpler to both
433 // default-initialize fields and skip over unnamed fields.
434 for (unsigned CurFieldNo = 0; CurFieldNo != NumMembers; ++CurFieldNo) {
Chris Lattnerf81557c2008-04-04 18:42:16 +0000435 FieldDecl *CurField = SD->getMember(CurFieldNo);
436 if (CurField->getIdentifier() == 0) {
437 // Initializers can't initialize unnamed fields, e.g. "int : 20;"
438 continue;
439 }
Eli Friedman1e692ac2008-06-13 23:01:12 +0000440 // FIXME: volatility
441 LValue FieldLoc = CGF.EmitLValueForField(DestPtr, CurField, isUnion,0);
Chris Lattnerf81557c2008-04-04 18:42:16 +0000442 if (CurInitVal < NumInitElements) {
443 // Store the initializer into the field
444 // This will probably have to get a bit smarter when we support
445 // designators in initializers
446 EmitInitializationToLValue(E->getInit(CurInitVal++), FieldLoc);
447 } else {
448 // We're out of initalizers; default-initialize to null
449 EmitNullInitializationToLValue(FieldLoc, CurField->getType());
450 }
451
452 // Unions only initialize one field.
453 // (things can get weird with designators, but they aren't
454 // supported yet.)
455 if (E->getType()->isUnionType())
456 break;
Lauro Ramos Venancio145cd892008-02-19 19:27:31 +0000457 }
Devang Patel636c3d02007-10-26 17:44:44 +0000458}
459
Chris Lattneree755f92007-08-21 04:59:27 +0000460//===----------------------------------------------------------------------===//
461// Entry Points into this File
462//===----------------------------------------------------------------------===//
463
464/// EmitAggExpr - Emit the computation of the specified expression of
465/// aggregate type. The result is computed into DestPtr. Note that if
466/// DestPtr is null, the value of the aggregate expression is not needed.
467void CodeGenFunction::EmitAggExpr(const Expr *E, llvm::Value *DestPtr,
468 bool VolatileDest) {
469 assert(E && hasAggregateLLVMType(E->getType()) &&
470 "Invalid aggregate expression to emit");
471
472 AggExprEmitter(*this, DestPtr, VolatileDest).Visit(const_cast<Expr*>(E));
473}