blob: 11eb9fc1157592586392c792c5457ba5b1b29e13 [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"
Daniel Dunbareee5cd12008-08-11 05:00:27 +000016#include "clang/AST/ASTContext.h"
17#include "clang/AST/StmtVisitor.h"
Chris Lattnerbdb8ffb2007-08-11 00:04:45 +000018#include "llvm/Constants.h"
19#include "llvm/Function.h"
Devang Patelddde6d52007-10-26 17:44:44 +000020#include "llvm/GlobalVariable.h"
Chris Lattnerb50e3902007-08-21 04:25:47 +000021#include "llvm/Support/Compiler.h"
Chris Lattner6ee6ab02008-04-04 18:42:16 +000022#include "llvm/Intrinsics.h"
Chris Lattner78ed8402007-08-10 20:13:28 +000023using namespace clang;
24using namespace CodeGen;
Chris Lattnerbdb8ffb2007-08-11 00:04:45 +000025
Chris Lattnerb50e3902007-08-21 04:25:47 +000026//===----------------------------------------------------------------------===//
27// Aggregate Expression Emitter
28//===----------------------------------------------------------------------===//
29
30namespace {
31class VISIBILITY_HIDDEN AggExprEmitter : public StmtVisitor<AggExprEmitter> {
32 CodeGenFunction &CGF;
Chris Lattnerfaf23db2008-08-08 19:57:58 +000033 llvm::IRBuilder<> &Builder;
Chris Lattnerb50e3902007-08-21 04:25:47 +000034 llvm::Value *DestPtr;
35 bool VolatileDest;
36public:
37 AggExprEmitter(CodeGenFunction &cgf, llvm::Value *destPtr, bool volatileDest)
Chris Lattner41b1fca2007-08-26 23:13:56 +000038 : CGF(cgf), Builder(CGF.Builder),
39 DestPtr(destPtr), VolatileDest(volatileDest) {
Chris Lattnerb50e3902007-08-21 04:25:47 +000040 }
41
Chris Lattnera7300102007-08-21 04:59:27 +000042 //===--------------------------------------------------------------------===//
43 // Utilities
44 //===--------------------------------------------------------------------===//
45
Chris Lattnerb50e3902007-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 Lattner41b1fca2007-08-26 23:13:56 +000051 void EmitAggregateCopy(llvm::Value *DestPtr, llvm::Value *SrcPtr,
52 QualType EltTy);
Lauro Ramos Venancio97b09572008-02-19 22:04:22 +000053
54 void EmitAggregateClear(llvm::Value *DestPtr, QualType Ty);
55
56 void EmitNonConstInit(InitListExpr *E);
Eli Friedman48ec5622008-05-19 17:51:16 +000057
Chris Lattnera7300102007-08-21 04:59:27 +000058 //===--------------------------------------------------------------------===//
59 // Visitor Methods
60 //===--------------------------------------------------------------------===//
61
Chris Lattnerb50e3902007-08-21 04:25:47 +000062 void VisitStmt(Stmt *S) {
Daniel Dunbar9503b782008-08-16 00:56:44 +000063 CGF.ErrorUnsupported(S, "aggregate expression");
Chris Lattnerb50e3902007-08-21 04:25:47 +000064 }
65 void VisitParenExpr(ParenExpr *PE) { Visit(PE->getSubExpr()); }
66
67 // l-values.
Seo Sanghyeon32666c52007-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 Sanghyeon3c2d5fb2007-12-23 03:11:58 +000071 void VisitStringLiteral(StringLiteral *E) { EmitAggLoadOfLValue(E); }
Eli Friedman42af3972008-05-27 15:51:49 +000072 void VisitCompoundLiteralExpr(CompoundLiteralExpr *E)
73 { EmitAggLoadOfLValue(E); }
Seo Sanghyeon32666c52007-12-14 02:04:12 +000074
75 void VisitArraySubscriptExpr(ArraySubscriptExpr *E) {
76 EmitAggLoadOfLValue(E);
77 }
Chris Lattnerb50e3902007-08-21 04:25:47 +000078
79 // Operators.
80 // case Expr::UnaryOperatorClass:
Chris Lattnerb50e3902007-08-21 04:25:47 +000081 // case Expr::CastExprClass:
Anders Carlssonf2712ac2008-01-14 06:28:57 +000082 void VisitImplicitCastExpr(ImplicitCastExpr *E);
Anders Carlsson0ae15412007-10-31 22:04:46 +000083 void VisitCallExpr(const CallExpr *E);
Chris Lattnerbab0bfb2007-08-31 22:54:14 +000084 void VisitStmtExpr(const StmtExpr *E);
Chris Lattnerb50e3902007-08-21 04:25:47 +000085 void VisitBinaryOperator(const BinaryOperator *BO);
Chris Lattner76cb1892007-08-21 04:43:17 +000086 void VisitBinAssign(const BinaryOperator *E);
Nate Begemanc6078c92008-01-31 05:38:29 +000087 void VisitOverloadExpr(const OverloadExpr *E);
Eli Friedman91fc7802008-05-20 07:56:31 +000088 void VisitBinComma(const BinaryOperator *E);
Chris Lattnerb50e3902007-08-21 04:25:47 +000089
Chris Lattner6ee20e32008-06-24 17:04:18 +000090 void VisitObjCMessageExpr(ObjCMessageExpr *E);
Daniel Dunbar5e105892008-08-23 10:51:21 +000091 void VisitObjCIvarRefExpr(ObjCIvarRefExpr *E) {
92 EmitAggLoadOfLValue(E);
93 }
Daniel Dunbar91cc4022008-08-27 06:57:25 +000094 void VisitObjCPropertyRefExpr(ObjCPropertyRefExpr *E);
Chris Lattnerb50e3902007-08-21 04:25:47 +000095
96 void VisitConditionalOperator(const ConditionalOperator *CO);
Devang Patelddde6d52007-10-26 17:44:44 +000097 void VisitInitListExpr(InitListExpr *E);
Chris Lattner3e254fb2008-04-08 04:40:51 +000098 void VisitCXXDefaultArgExpr(CXXDefaultArgExpr *DAE) {
99 Visit(DAE->getExpr());
100 }
Eli Friedman42af3972008-05-27 15:51:49 +0000101 void VisitVAArgExpr(VAArgExpr *E);
Chris Lattner6ee6ab02008-04-04 18:42:16 +0000102
103 void EmitInitializationToLValue(Expr *E, LValue Address);
104 void EmitNullInitializationToLValue(LValue Address, QualType T);
Chris Lattnerb50e3902007-08-21 04:25:47 +0000105 // case Expr::ChooseExprClass:
Lauro Ramos Venancio9eb37a22008-02-18 22:44:02 +0000106
Chris Lattnerb50e3902007-08-21 04:25:47 +0000107};
108} // end anonymous namespace.
109
Chris Lattnera7300102007-08-21 04:59:27 +0000110//===----------------------------------------------------------------------===//
111// Utilities
112//===----------------------------------------------------------------------===//
Chris Lattnerb50e3902007-08-21 04:25:47 +0000113
Lauro Ramos Venancio97b09572008-02-19 22:04:22 +0000114void AggExprEmitter::EmitAggregateClear(llvm::Value *DestPtr, QualType Ty) {
Chris Lattnerde0908b2008-04-04 16:54:41 +0000115 assert(!Ty->isAnyComplexType() && "Shouldn't happen for complex");
Lauro Ramos Venancio97b09572008-02-19 22:04:22 +0000116
117 // Aggregate assignment turns into llvm.memset.
118 const llvm::Type *BP = llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
119 if (DestPtr->getType() != BP)
120 DestPtr = Builder.CreateBitCast(DestPtr, BP, "tmp");
121
122 // Get size and alignment info for this aggregate.
Chris Lattner8cd0e932008-03-05 18:54:05 +0000123 std::pair<uint64_t, unsigned> TypeInfo = CGF.getContext().getTypeInfo(Ty);
Lauro Ramos Venancio97b09572008-02-19 22:04:22 +0000124
125 // FIXME: Handle variable sized types.
126 const llvm::Type *IntPtr = llvm::IntegerType::get(CGF.LLVMPointerWidth);
127
128 llvm::Value *MemSetOps[4] = {
129 DestPtr,
130 llvm::ConstantInt::getNullValue(llvm::Type::Int8Ty),
131 // TypeInfo.first describes size in bits.
132 llvm::ConstantInt::get(IntPtr, TypeInfo.first/8),
133 llvm::ConstantInt::get(llvm::Type::Int32Ty, TypeInfo.second/8)
134 };
135
136 Builder.CreateCall(CGF.CGM.getMemSetFn(), MemSetOps, MemSetOps+4);
137}
138
Chris Lattner41b1fca2007-08-26 23:13:56 +0000139void AggExprEmitter::EmitAggregateCopy(llvm::Value *DestPtr,
140 llvm::Value *SrcPtr, QualType Ty) {
Chris Lattnerde0908b2008-04-04 16:54:41 +0000141 assert(!Ty->isAnyComplexType() && "Shouldn't happen for complex");
Chris Lattner41b1fca2007-08-26 23:13:56 +0000142
Eli Friedman8f08a252008-05-26 12:59:39 +0000143 // Aggregate assignment turns into llvm.memmove.
Christopher Lamb4fe5e702007-12-17 01:11:20 +0000144 const llvm::Type *BP = llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
Chris Lattner41b1fca2007-08-26 23:13:56 +0000145 if (DestPtr->getType() != BP)
146 DestPtr = Builder.CreateBitCast(DestPtr, BP, "tmp");
147 if (SrcPtr->getType() != BP)
148 SrcPtr = Builder.CreateBitCast(SrcPtr, BP, "tmp");
149
150 // Get size and alignment info for this aggregate.
Chris Lattner8cd0e932008-03-05 18:54:05 +0000151 std::pair<uint64_t, unsigned> TypeInfo = CGF.getContext().getTypeInfo(Ty);
Chris Lattner41b1fca2007-08-26 23:13:56 +0000152
153 // FIXME: Handle variable sized types.
154 const llvm::Type *IntPtr = llvm::IntegerType::get(CGF.LLVMPointerWidth);
155
Eli Friedman8f08a252008-05-26 12:59:39 +0000156 llvm::Value *MemMoveOps[4] = {
Chris Lattner41b1fca2007-08-26 23:13:56 +0000157 DestPtr, SrcPtr,
Devang Patelddde6d52007-10-26 17:44:44 +0000158 // TypeInfo.first describes size in bits.
159 llvm::ConstantInt::get(IntPtr, TypeInfo.first/8),
Lauro Ramos Venancio97b09572008-02-19 22:04:22 +0000160 llvm::ConstantInt::get(llvm::Type::Int32Ty, TypeInfo.second/8)
Chris Lattner41b1fca2007-08-26 23:13:56 +0000161 };
162
Eli Friedman8f08a252008-05-26 12:59:39 +0000163 Builder.CreateCall(CGF.CGM.getMemMoveFn(), MemMoveOps, MemMoveOps+4);
Chris Lattner41b1fca2007-08-26 23:13:56 +0000164}
165
166
Chris Lattnerbdb8ffb2007-08-11 00:04:45 +0000167/// EmitAggLoadOfLValue - Given an expression with aggregate type that
168/// represents a value lvalue, this method emits the address of the lvalue,
169/// then loads the result into DestPtr.
Chris Lattnerb50e3902007-08-21 04:25:47 +0000170void AggExprEmitter::EmitAggLoadOfLValue(const Expr *E) {
171 LValue LV = CGF.EmitLValue(E);
Chris Lattnerbdb8ffb2007-08-11 00:04:45 +0000172 assert(LV.isSimple() && "Can't have aggregate bitfield, vector, etc");
173 llvm::Value *SrcPtr = LV.getAddress();
174
175 // If the result is ignored, don't copy from the value.
176 if (DestPtr == 0)
177 // FIXME: If the source is volatile, we must read from it.
178 return;
179
Chris Lattner41b1fca2007-08-26 23:13:56 +0000180 EmitAggregateCopy(DestPtr, SrcPtr, E->getType());
Chris Lattnerbdb8ffb2007-08-11 00:04:45 +0000181}
182
Chris Lattnera7300102007-08-21 04:59:27 +0000183//===----------------------------------------------------------------------===//
184// Visitor Methods
185//===----------------------------------------------------------------------===//
186
Chris Lattnerc154ac12008-07-26 22:37:01 +0000187void AggExprEmitter::VisitImplicitCastExpr(ImplicitCastExpr *E) {
Eli Friedmanf0217122008-02-11 01:09:17 +0000188 assert(CGF.getContext().typesAreCompatible(
Chris Lattnerc154ac12008-07-26 22:37:01 +0000189 E->getSubExpr()->getType().getUnqualifiedType(),
190 E->getType().getUnqualifiedType()) &&
191 "Implicit cast types must be compatible");
Anders Carlssonf2712ac2008-01-14 06:28:57 +0000192 Visit(E->getSubExpr());
193}
194
Chris Lattnerc154ac12008-07-26 22:37:01 +0000195void AggExprEmitter::VisitCallExpr(const CallExpr *E) {
Anders Carlsson0ae15412007-10-31 22:04:46 +0000196 RValue RV = CGF.EmitCallExpr(E);
197 assert(RV.isAggregate() && "Return value must be aggregate value!");
198
199 // If the result is ignored, don't copy from the value.
200 if (DestPtr == 0)
201 // FIXME: If the source is volatile, we must read from it.
202 return;
203
204 EmitAggregateCopy(DestPtr, RV.getAggregateAddr(), E->getType());
205}
Chris Lattnerc154ac12008-07-26 22:37:01 +0000206
207void AggExprEmitter::VisitObjCMessageExpr(ObjCMessageExpr *E) {
Daniel Dunbara04840b2008-08-23 03:46:30 +0000208 RValue RV = CGF.EmitObjCMessageExpr(E);
209 assert(RV.isAggregate() && "Return value must be aggregate value!");
Chris Lattner6ee20e32008-06-24 17:04:18 +0000210
211 // If the result is ignored, don't copy from the value.
212 if (DestPtr == 0)
Daniel Dunbara04840b2008-08-23 03:46:30 +0000213 // FIXME: If the source is volatile, we must read from it.
Chris Lattner6ee20e32008-06-24 17:04:18 +0000214 return;
215
216 EmitAggregateCopy(DestPtr, RV.getAggregateAddr(), E->getType());
217}
Anders Carlsson0ae15412007-10-31 22:04:46 +0000218
Daniel Dunbar91cc4022008-08-27 06:57:25 +0000219void AggExprEmitter::VisitObjCPropertyRefExpr(ObjCPropertyRefExpr *E) {
220 RValue RV = CGF.EmitObjCPropertyGet(E);
221 assert(RV.isAggregate() && "Return value must be aggregate value!");
222
223 // If the result is ignored, don't copy from the value.
224 if (DestPtr == 0)
225 // FIXME: If the source is volatile, we must read from it.
226 return;
227
228 EmitAggregateCopy(DestPtr, RV.getAggregateAddr(), E->getType());
229}
230
Chris Lattnerc154ac12008-07-26 22:37:01 +0000231void AggExprEmitter::VisitOverloadExpr(const OverloadExpr *E) {
Nate Begemanc6078c92008-01-31 05:38:29 +0000232 RValue RV = CGF.EmitCallExpr(E->getFn(), E->arg_begin(),
Ted Kremenek2719e982008-06-17 02:43:46 +0000233 E->arg_end(CGF.getContext()));
234
Nate Begemanc6078c92008-01-31 05:38:29 +0000235 assert(RV.isAggregate() && "Return value must be aggregate value!");
236
237 // If the result is ignored, don't copy from the value.
238 if (DestPtr == 0)
239 // FIXME: If the source is volatile, we must read from it.
240 return;
241
242 EmitAggregateCopy(DestPtr, RV.getAggregateAddr(), E->getType());
243}
244
Chris Lattnerc154ac12008-07-26 22:37:01 +0000245void AggExprEmitter::VisitBinComma(const BinaryOperator *E) {
Eli Friedman91fc7802008-05-20 07:56:31 +0000246 CGF.EmitAnyExpr(E->getLHS());
247 CGF.EmitAggExpr(E->getRHS(), DestPtr, false);
248}
249
Chris Lattnerbab0bfb2007-08-31 22:54:14 +0000250void AggExprEmitter::VisitStmtExpr(const StmtExpr *E) {
251 CGF.EmitCompoundStmt(*E->getSubStmt(), true, DestPtr, VolatileDest);
252}
253
Chris Lattnerb50e3902007-08-21 04:25:47 +0000254void AggExprEmitter::VisitBinaryOperator(const BinaryOperator *E) {
Daniel Dunbar9503b782008-08-16 00:56:44 +0000255 CGF.ErrorUnsupported(E, "aggregate binary expression");
Chris Lattnera7300102007-08-21 04:59:27 +0000256}
257
Chris Lattner76cb1892007-08-21 04:43:17 +0000258void AggExprEmitter::VisitBinAssign(const BinaryOperator *E) {
Eli Friedmanf0217122008-02-11 01:09:17 +0000259 // For an assignment to work, the value on the right has
260 // to be compatible with the value on the left.
261 assert(CGF.getContext().typesAreCompatible(
262 E->getLHS()->getType().getUnqualifiedType(),
263 E->getRHS()->getType().getUnqualifiedType())
264 && "Invalid assignment");
Chris Lattnerb50e3902007-08-21 04:25:47 +0000265 LValue LHS = CGF.EmitLValue(E->getLHS());
Chris Lattnerbdb8ffb2007-08-11 00:04:45 +0000266
Daniel Dunbardd851282008-08-30 05:35:15 +0000267 // We have to special case property setters, otherwise we must have
268 // a simple lvalue (no aggregates inside vectors, bitfields).
269 if (LHS.isPropertyRef()) {
270 // FIXME: Volatility?
271 llvm::Value *AggLoc = DestPtr;
272 if (!AggLoc)
273 AggLoc = CGF.CreateTempAlloca(CGF.ConvertType(E->getRHS()->getType()));
274 CGF.EmitAggExpr(E->getRHS(), AggLoc, false);
275 CGF.EmitObjCPropertySet(LHS.getPropertyRefExpr(),
276 RValue::getAggregate(AggLoc));
277 } else {
278 // Codegen the RHS so that it stores directly into the LHS.
279 CGF.EmitAggExpr(E->getRHS(), LHS.getAddress(), false /*FIXME: VOLATILE LHS*/);
280
281 if (DestPtr == 0)
282 return;
283
284 // If the result of the assignment is used, copy the RHS there also.
285 EmitAggregateCopy(DestPtr, LHS.getAddress(), E->getType());
286 }
Chris Lattnerbdb8ffb2007-08-11 00:04:45 +0000287}
288
Chris Lattnerb50e3902007-08-21 04:25:47 +0000289void AggExprEmitter::VisitConditionalOperator(const ConditionalOperator *E) {
Gabor Greif815e2c12008-04-06 20:42:52 +0000290 llvm::BasicBlock *LHSBlock = llvm::BasicBlock::Create("cond.?");
291 llvm::BasicBlock *RHSBlock = llvm::BasicBlock::Create("cond.:");
292 llvm::BasicBlock *ContBlock = llvm::BasicBlock::Create("cond.cont");
Chris Lattnerbdb8ffb2007-08-11 00:04:45 +0000293
Chris Lattnerb50e3902007-08-21 04:25:47 +0000294 llvm::Value *Cond = CGF.EvaluateExprAsBool(E->getCond());
Chris Lattner41b1fca2007-08-26 23:13:56 +0000295 Builder.CreateCondBr(Cond, LHSBlock, RHSBlock);
Chris Lattnerbdb8ffb2007-08-11 00:04:45 +0000296
Chris Lattnerb50e3902007-08-21 04:25:47 +0000297 CGF.EmitBlock(LHSBlock);
Chris Lattnerbdb8ffb2007-08-11 00:04:45 +0000298
299 // Handle the GNU extension for missing LHS.
300 assert(E->getLHS() && "Must have LHS for aggregate value");
301
Chris Lattner55165ba2007-08-21 05:02:10 +0000302 Visit(E->getLHS());
Chris Lattner41b1fca2007-08-26 23:13:56 +0000303 Builder.CreateBr(ContBlock);
304 LHSBlock = Builder.GetInsertBlock();
Chris Lattnerbdb8ffb2007-08-11 00:04:45 +0000305
Chris Lattnerb50e3902007-08-21 04:25:47 +0000306 CGF.EmitBlock(RHSBlock);
Chris Lattnerbdb8ffb2007-08-11 00:04:45 +0000307
Chris Lattner55165ba2007-08-21 05:02:10 +0000308 Visit(E->getRHS());
Chris Lattner41b1fca2007-08-26 23:13:56 +0000309 Builder.CreateBr(ContBlock);
310 RHSBlock = Builder.GetInsertBlock();
Chris Lattnerbdb8ffb2007-08-11 00:04:45 +0000311
Chris Lattnerb50e3902007-08-21 04:25:47 +0000312 CGF.EmitBlock(ContBlock);
Chris Lattnerbdb8ffb2007-08-11 00:04:45 +0000313}
Chris Lattnera7300102007-08-21 04:59:27 +0000314
Eli Friedman42af3972008-05-27 15:51:49 +0000315void AggExprEmitter::VisitVAArgExpr(VAArgExpr *VE) {
316 llvm::Value *ArgValue = CGF.EmitLValue(VE->getSubExpr()).getAddress();
317 llvm::Value *V = Builder.CreateVAArg(ArgValue, CGF.ConvertType(VE->getType()));
318 if (DestPtr)
Eli Friedman2e630542008-06-13 23:01:12 +0000319 // FIXME: volatility
Eli Friedman42af3972008-05-27 15:51:49 +0000320 Builder.CreateStore(V, DestPtr);
321}
322
Lauro Ramos Venancio97b09572008-02-19 22:04:22 +0000323void AggExprEmitter::EmitNonConstInit(InitListExpr *E) {
324
325 const llvm::PointerType *APType =
326 cast<llvm::PointerType>(DestPtr->getType());
327 const llvm::Type *DestType = APType->getElementType();
Lauro Ramos Venancio9eb37a22008-02-18 22:44:02 +0000328
329 if (const llvm::ArrayType *AType = dyn_cast<llvm::ArrayType>(DestType)) {
Lauro Ramos Venancio97b09572008-02-19 22:04:22 +0000330 unsigned NumInitElements = E->getNumInits();
Lauro Ramos Venancio9eb37a22008-02-18 22:44:02 +0000331
Lauro Ramos Venancio9eb37a22008-02-18 22:44:02 +0000332 unsigned i;
333 for (i = 0; i != NumInitElements; ++i) {
Chris Lattner07307562008-03-19 05:19:41 +0000334 llvm::Value *NextVal = Builder.CreateStructGEP(DestPtr, i, ".array");
Lauro Ramos Venancio97b09572008-02-19 22:04:22 +0000335 Expr *Init = E->getInit(i);
336 if (isa<InitListExpr>(Init))
337 CGF.EmitAggExpr(Init, NextVal, VolatileDest);
338 else
Eli Friedman2e630542008-06-13 23:01:12 +0000339 // FIXME: volatility
Lauro Ramos Venancio97b09572008-02-19 22:04:22 +0000340 Builder.CreateStore(CGF.EmitScalarExpr(Init), NextVal);
Lauro Ramos Venancio9eb37a22008-02-18 22:44:02 +0000341 }
342
343 // Emit remaining default initializers
344 unsigned NumArrayElements = AType->getNumElements();
Lauro Ramos Venancio97b09572008-02-19 22:04:22 +0000345 QualType QType = E->getInit(0)->getType();
346 const llvm::Type *EType = AType->getElementType();
Lauro Ramos Venancio9eb37a22008-02-18 22:44:02 +0000347 for (/*Do not initialize i*/; i < NumArrayElements; ++i) {
Chris Lattner07307562008-03-19 05:19:41 +0000348 llvm::Value *NextVal = Builder.CreateStructGEP(DestPtr, i, ".array");
Dan Gohman377ba9f2008-05-22 22:12:56 +0000349 if (EType->isSingleValueType())
Eli Friedman2e630542008-06-13 23:01:12 +0000350 // FIXME: volatility
Lauro Ramos Venancio97b09572008-02-19 22:04:22 +0000351 Builder.CreateStore(llvm::Constant::getNullValue(EType), NextVal);
352 else
353 EmitAggregateClear(NextVal, QType);
Lauro Ramos Venancio9eb37a22008-02-18 22:44:02 +0000354 }
Lauro Ramos Venancio97b09572008-02-19 22:04:22 +0000355 } else
356 assert(false && "Invalid initializer");
Lauro Ramos Venancio9eb37a22008-02-18 22:44:02 +0000357}
358
Chris Lattner6ee6ab02008-04-04 18:42:16 +0000359void AggExprEmitter::EmitInitializationToLValue(Expr* E, LValue LV) {
360 // FIXME: Are initializers affected by volatile?
361 if (E->getType()->isComplexType()) {
362 CGF.EmitComplexExprIntoAddr(E, LV.getAddress(), false);
Eli Friedmanbb822fb2008-05-12 15:06:05 +0000363 } else if (CGF.hasAggregateLLVMType(E->getType())) {
364 CGF.EmitAnyExpr(E, LV.getAddress(), false);
365 } else {
366 CGF.EmitStoreThroughLValue(CGF.EmitAnyExpr(E), LV, E->getType());
Chris Lattner6ee6ab02008-04-04 18:42:16 +0000367 }
Chris Lattner6ee6ab02008-04-04 18:42:16 +0000368}
369
370void AggExprEmitter::EmitNullInitializationToLValue(LValue LV, QualType T) {
371 if (!CGF.hasAggregateLLVMType(T)) {
372 // For non-aggregates, we can store zero
Daniel Dunbarde813312008-08-06 05:32:55 +0000373 llvm::Value *Null = llvm::Constant::getNullValue(CGF.ConvertType(T));
374 CGF.EmitStoreThroughLValue(RValue::get(Null), LV, T);
Lauro Ramos Venancioa62b1df2008-02-19 19:27:31 +0000375 } else {
Chris Lattner6ee6ab02008-04-04 18:42:16 +0000376 // Otherwise, just memset the whole thing to zero. This is legal
377 // because in LLVM, all default initializers are guaranteed to have a
378 // bit pattern of all zeros.
379 // There's a potential optimization opportunity in combining
380 // memsets; that would be easy for arrays, but relatively
381 // difficult for structures with the current code.
382 llvm::Value *MemSet = CGF.CGM.getIntrinsic(llvm::Intrinsic::memset_i64);
383 uint64_t Size = CGF.getContext().getTypeSize(T);
384
385 const llvm::Type *BP = llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
386 llvm::Value* DestPtr = Builder.CreateBitCast(LV.getAddress(), BP, "tmp");
Chris Lattner7db5e942008-05-06 00:56:42 +0000387 Builder.CreateCall4(MemSet, DestPtr,
388 llvm::ConstantInt::get(llvm::Type::Int8Ty, 0),
389 llvm::ConstantInt::get(llvm::Type::Int64Ty, Size/8),
390 llvm::ConstantInt::get(llvm::Type::Int32Ty, 0));
Chris Lattner6ee6ab02008-04-04 18:42:16 +0000391 }
392}
393
Chris Lattner6ee6ab02008-04-04 18:42:16 +0000394void AggExprEmitter::VisitInitListExpr(InitListExpr *E) {
Daniel Dunbar182418c2008-08-23 18:44:10 +0000395 // FIXME: For constant expressions, call into const expr emitter so
396 // that we can emit a memcpy instead of storing the individual
397 // members. This is purely for perf; both codepaths lead to
398 // equivalent (although not necessarily identical) code. It's worth
399 // noting that LLVM keeps on getting smarter, though, so it might
400 // not be worth bothering.
Chris Lattner6ee6ab02008-04-04 18:42:16 +0000401
402 // Handle initialization of an array.
403 if (E->getType()->isArrayType()) {
404 const llvm::PointerType *APType =
405 cast<llvm::PointerType>(DestPtr->getType());
406 const llvm::ArrayType *AType =
407 cast<llvm::ArrayType>(APType->getElementType());
408
409 uint64_t NumInitElements = E->getNumInits();
Eli Friedman48ec5622008-05-19 17:51:16 +0000410
Chris Lattnerc154ac12008-07-26 22:37:01 +0000411 if (E->getNumInits() > 0) {
412 QualType T1 = E->getType();
413 QualType T2 = E->getInit(0)->getType();
414 if (CGF.getContext().getCanonicalType(T1).getUnqualifiedType() ==
415 CGF.getContext().getCanonicalType(T2).getUnqualifiedType()) {
416 EmitAggLoadOfLValue(E->getInit(0));
417 return;
418 }
Eli Friedman48ec5622008-05-19 17:51:16 +0000419 }
420
Chris Lattner6ee6ab02008-04-04 18:42:16 +0000421 uint64_t NumArrayElements = AType->getNumElements();
Chris Lattnera1923f62008-08-04 07:31:14 +0000422 QualType ElementType = CGF.getContext().getCanonicalType(E->getType());
423 ElementType =CGF.getContext().getAsArrayType(ElementType)->getElementType();
Chris Lattner6ee6ab02008-04-04 18:42:16 +0000424
Chris Lattnera1923f62008-08-04 07:31:14 +0000425 unsigned CVRqualifier = ElementType.getCVRQualifiers();
Eli Friedman2e630542008-06-13 23:01:12 +0000426
Chris Lattner6ee6ab02008-04-04 18:42:16 +0000427 for (uint64_t i = 0; i != NumArrayElements; ++i) {
428 llvm::Value *NextVal = Builder.CreateStructGEP(DestPtr, i, ".array");
429 if (i < NumInitElements)
Eli Friedman2e630542008-06-13 23:01:12 +0000430 EmitInitializationToLValue(E->getInit(i),
431 LValue::MakeAddr(NextVal, CVRqualifier));
Chris Lattner6ee6ab02008-04-04 18:42:16 +0000432 else
Eli Friedman2e630542008-06-13 23:01:12 +0000433 EmitNullInitializationToLValue(LValue::MakeAddr(NextVal, CVRqualifier),
Chris Lattner6ee6ab02008-04-04 18:42:16 +0000434 ElementType);
Lauro Ramos Venancioa62b1df2008-02-19 19:27:31 +0000435 }
Chris Lattner6ee6ab02008-04-04 18:42:16 +0000436 return;
437 }
438
439 assert(E->getType()->isRecordType() && "Only support structs/unions here!");
440
441 // Do struct initialization; this code just sets each individual member
442 // to the approprate value. This makes bitfield support automatic;
443 // the disadvantage is that the generated code is more difficult for
444 // the optimizer, especially with bitfields.
445 unsigned NumInitElements = E->getNumInits();
446 RecordDecl *SD = E->getType()->getAsRecordType()->getDecl();
447 unsigned NumMembers = SD->getNumMembers() - SD->hasFlexibleArrayMember();
448 unsigned CurInitVal = 0;
449 bool isUnion = E->getType()->isUnionType();
450
451 // Here we iterate over the fields; this makes it simpler to both
452 // default-initialize fields and skip over unnamed fields.
453 for (unsigned CurFieldNo = 0; CurFieldNo != NumMembers; ++CurFieldNo) {
Chris Lattner6ee6ab02008-04-04 18:42:16 +0000454 FieldDecl *CurField = SD->getMember(CurFieldNo);
455 if (CurField->getIdentifier() == 0) {
456 // Initializers can't initialize unnamed fields, e.g. "int : 20;"
457 continue;
458 }
Eli Friedman2e630542008-06-13 23:01:12 +0000459 // FIXME: volatility
460 LValue FieldLoc = CGF.EmitLValueForField(DestPtr, CurField, isUnion,0);
Chris Lattner6ee6ab02008-04-04 18:42:16 +0000461 if (CurInitVal < NumInitElements) {
462 // Store the initializer into the field
463 // This will probably have to get a bit smarter when we support
464 // designators in initializers
465 EmitInitializationToLValue(E->getInit(CurInitVal++), FieldLoc);
466 } else {
467 // We're out of initalizers; default-initialize to null
468 EmitNullInitializationToLValue(FieldLoc, CurField->getType());
469 }
470
471 // Unions only initialize one field.
472 // (things can get weird with designators, but they aren't
473 // supported yet.)
474 if (E->getType()->isUnionType())
475 break;
Lauro Ramos Venancioa62b1df2008-02-19 19:27:31 +0000476 }
Devang Patelddde6d52007-10-26 17:44:44 +0000477}
478
Chris Lattnera7300102007-08-21 04:59:27 +0000479//===----------------------------------------------------------------------===//
480// Entry Points into this File
481//===----------------------------------------------------------------------===//
482
483/// EmitAggExpr - Emit the computation of the specified expression of
484/// aggregate type. The result is computed into DestPtr. Note that if
485/// DestPtr is null, the value of the aggregate expression is not needed.
486void CodeGenFunction::EmitAggExpr(const Expr *E, llvm::Value *DestPtr,
487 bool VolatileDest) {
488 assert(E && hasAggregateLLVMType(E->getType()) &&
489 "Invalid aggregate expression to emit");
490
491 AggExprEmitter(*this, DestPtr, VolatileDest).Visit(const_cast<Expr*>(E));
492}