blob: 5fa7ef5c5243a2f72077767d87cec277b58e96a8 [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) {
Chris Lattnere8f49632007-12-02 01:49:16 +000063 CGF.WarnUnsupported(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);
91
Chris Lattnerb50e3902007-08-21 04:25:47 +000092
93 void VisitConditionalOperator(const ConditionalOperator *CO);
Devang Patelddde6d52007-10-26 17:44:44 +000094 void VisitInitListExpr(InitListExpr *E);
Chris Lattner3e254fb2008-04-08 04:40:51 +000095 void VisitCXXDefaultArgExpr(CXXDefaultArgExpr *DAE) {
96 Visit(DAE->getExpr());
97 }
Eli Friedman42af3972008-05-27 15:51:49 +000098 void VisitVAArgExpr(VAArgExpr *E);
Chris Lattner6ee6ab02008-04-04 18:42:16 +000099
100 void EmitInitializationToLValue(Expr *E, LValue Address);
101 void EmitNullInitializationToLValue(LValue Address, QualType T);
Chris Lattnerb50e3902007-08-21 04:25:47 +0000102 // case Expr::ChooseExprClass:
Lauro Ramos Venancio9eb37a22008-02-18 22:44:02 +0000103
Chris Lattnerb50e3902007-08-21 04:25:47 +0000104};
105} // end anonymous namespace.
106
Chris Lattnera7300102007-08-21 04:59:27 +0000107//===----------------------------------------------------------------------===//
108// Utilities
109//===----------------------------------------------------------------------===//
Chris Lattnerb50e3902007-08-21 04:25:47 +0000110
Lauro Ramos Venancio97b09572008-02-19 22:04:22 +0000111void AggExprEmitter::EmitAggregateClear(llvm::Value *DestPtr, QualType Ty) {
Chris Lattnerde0908b2008-04-04 16:54:41 +0000112 assert(!Ty->isAnyComplexType() && "Shouldn't happen for complex");
Lauro Ramos Venancio97b09572008-02-19 22:04:22 +0000113
114 // Aggregate assignment turns into llvm.memset.
115 const llvm::Type *BP = llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
116 if (DestPtr->getType() != BP)
117 DestPtr = Builder.CreateBitCast(DestPtr, BP, "tmp");
118
119 // Get size and alignment info for this aggregate.
Chris Lattner8cd0e932008-03-05 18:54:05 +0000120 std::pair<uint64_t, unsigned> TypeInfo = CGF.getContext().getTypeInfo(Ty);
Lauro Ramos Venancio97b09572008-02-19 22:04:22 +0000121
122 // FIXME: Handle variable sized types.
123 const llvm::Type *IntPtr = llvm::IntegerType::get(CGF.LLVMPointerWidth);
124
125 llvm::Value *MemSetOps[4] = {
126 DestPtr,
127 llvm::ConstantInt::getNullValue(llvm::Type::Int8Ty),
128 // TypeInfo.first describes size in bits.
129 llvm::ConstantInt::get(IntPtr, TypeInfo.first/8),
130 llvm::ConstantInt::get(llvm::Type::Int32Ty, TypeInfo.second/8)
131 };
132
133 Builder.CreateCall(CGF.CGM.getMemSetFn(), MemSetOps, MemSetOps+4);
134}
135
Chris Lattner41b1fca2007-08-26 23:13:56 +0000136void AggExprEmitter::EmitAggregateCopy(llvm::Value *DestPtr,
137 llvm::Value *SrcPtr, QualType Ty) {
Chris Lattnerde0908b2008-04-04 16:54:41 +0000138 assert(!Ty->isAnyComplexType() && "Shouldn't happen for complex");
Chris Lattner41b1fca2007-08-26 23:13:56 +0000139
Eli Friedman8f08a252008-05-26 12:59:39 +0000140 // Aggregate assignment turns into llvm.memmove.
Christopher Lamb4fe5e702007-12-17 01:11:20 +0000141 const llvm::Type *BP = llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
Chris Lattner41b1fca2007-08-26 23:13:56 +0000142 if (DestPtr->getType() != BP)
143 DestPtr = Builder.CreateBitCast(DestPtr, BP, "tmp");
144 if (SrcPtr->getType() != BP)
145 SrcPtr = Builder.CreateBitCast(SrcPtr, BP, "tmp");
146
147 // Get size and alignment info for this aggregate.
Chris Lattner8cd0e932008-03-05 18:54:05 +0000148 std::pair<uint64_t, unsigned> TypeInfo = CGF.getContext().getTypeInfo(Ty);
Chris Lattner41b1fca2007-08-26 23:13:56 +0000149
150 // FIXME: Handle variable sized types.
151 const llvm::Type *IntPtr = llvm::IntegerType::get(CGF.LLVMPointerWidth);
152
Eli Friedman8f08a252008-05-26 12:59:39 +0000153 llvm::Value *MemMoveOps[4] = {
Chris Lattner41b1fca2007-08-26 23:13:56 +0000154 DestPtr, SrcPtr,
Devang Patelddde6d52007-10-26 17:44:44 +0000155 // TypeInfo.first describes size in bits.
156 llvm::ConstantInt::get(IntPtr, TypeInfo.first/8),
Lauro Ramos Venancio97b09572008-02-19 22:04:22 +0000157 llvm::ConstantInt::get(llvm::Type::Int32Ty, TypeInfo.second/8)
Chris Lattner41b1fca2007-08-26 23:13:56 +0000158 };
159
Eli Friedman8f08a252008-05-26 12:59:39 +0000160 Builder.CreateCall(CGF.CGM.getMemMoveFn(), MemMoveOps, MemMoveOps+4);
Chris Lattner41b1fca2007-08-26 23:13:56 +0000161}
162
163
Chris Lattnerbdb8ffb2007-08-11 00:04:45 +0000164/// EmitAggLoadOfLValue - Given an expression with aggregate type that
165/// represents a value lvalue, this method emits the address of the lvalue,
166/// then loads the result into DestPtr.
Chris Lattnerb50e3902007-08-21 04:25:47 +0000167void AggExprEmitter::EmitAggLoadOfLValue(const Expr *E) {
168 LValue LV = CGF.EmitLValue(E);
Chris Lattnerbdb8ffb2007-08-11 00:04:45 +0000169 assert(LV.isSimple() && "Can't have aggregate bitfield, vector, etc");
170 llvm::Value *SrcPtr = LV.getAddress();
171
172 // If the result is ignored, don't copy from the value.
173 if (DestPtr == 0)
174 // FIXME: If the source is volatile, we must read from it.
175 return;
176
Chris Lattner41b1fca2007-08-26 23:13:56 +0000177 EmitAggregateCopy(DestPtr, SrcPtr, E->getType());
Chris Lattnerbdb8ffb2007-08-11 00:04:45 +0000178}
179
Chris Lattnera7300102007-08-21 04:59:27 +0000180//===----------------------------------------------------------------------===//
181// Visitor Methods
182//===----------------------------------------------------------------------===//
183
Chris Lattnerc154ac12008-07-26 22:37:01 +0000184void AggExprEmitter::VisitImplicitCastExpr(ImplicitCastExpr *E) {
Eli Friedmanf0217122008-02-11 01:09:17 +0000185 assert(CGF.getContext().typesAreCompatible(
Chris Lattnerc154ac12008-07-26 22:37:01 +0000186 E->getSubExpr()->getType().getUnqualifiedType(),
187 E->getType().getUnqualifiedType()) &&
188 "Implicit cast types must be compatible");
Anders Carlssonf2712ac2008-01-14 06:28:57 +0000189 Visit(E->getSubExpr());
190}
191
Chris Lattnerc154ac12008-07-26 22:37:01 +0000192void AggExprEmitter::VisitCallExpr(const CallExpr *E) {
Anders Carlsson0ae15412007-10-31 22:04:46 +0000193 RValue RV = CGF.EmitCallExpr(E);
194 assert(RV.isAggregate() && "Return value must be aggregate value!");
195
196 // If the result is ignored, don't copy from the value.
197 if (DestPtr == 0)
198 // FIXME: If the source is volatile, we must read from it.
199 return;
200
201 EmitAggregateCopy(DestPtr, RV.getAggregateAddr(), E->getType());
202}
Chris Lattnerc154ac12008-07-26 22:37:01 +0000203
204void AggExprEmitter::VisitObjCMessageExpr(ObjCMessageExpr *E) {
Chris Lattner6ee20e32008-06-24 17:04:18 +0000205 RValue RV = RValue::getAggregate(CGF.EmitObjCMessageExpr(E));
206
207 // If the result is ignored, don't copy from the value.
208 if (DestPtr == 0)
209 return;
210
211 EmitAggregateCopy(DestPtr, RV.getAggregateAddr(), E->getType());
212}
Anders Carlsson0ae15412007-10-31 22:04:46 +0000213
Chris Lattnerc154ac12008-07-26 22:37:01 +0000214void AggExprEmitter::VisitOverloadExpr(const OverloadExpr *E) {
Nate Begemanc6078c92008-01-31 05:38:29 +0000215 RValue RV = CGF.EmitCallExpr(E->getFn(), E->arg_begin(),
Ted Kremenek2719e982008-06-17 02:43:46 +0000216 E->arg_end(CGF.getContext()));
217
Nate Begemanc6078c92008-01-31 05:38:29 +0000218 assert(RV.isAggregate() && "Return value must be aggregate value!");
219
220 // If the result is ignored, don't copy from the value.
221 if (DestPtr == 0)
222 // FIXME: If the source is volatile, we must read from it.
223 return;
224
225 EmitAggregateCopy(DestPtr, RV.getAggregateAddr(), E->getType());
226}
227
Chris Lattnerc154ac12008-07-26 22:37:01 +0000228void AggExprEmitter::VisitBinComma(const BinaryOperator *E) {
Eli Friedman91fc7802008-05-20 07:56:31 +0000229 CGF.EmitAnyExpr(E->getLHS());
230 CGF.EmitAggExpr(E->getRHS(), DestPtr, false);
231}
232
Chris Lattnerbab0bfb2007-08-31 22:54:14 +0000233void AggExprEmitter::VisitStmtExpr(const StmtExpr *E) {
234 CGF.EmitCompoundStmt(*E->getSubStmt(), true, DestPtr, VolatileDest);
235}
236
Chris Lattnerb50e3902007-08-21 04:25:47 +0000237void AggExprEmitter::VisitBinaryOperator(const BinaryOperator *E) {
Chris Lattnere8f49632007-12-02 01:49:16 +0000238 CGF.WarnUnsupported(E, "aggregate binary expression");
Chris Lattnera7300102007-08-21 04:59:27 +0000239}
240
Chris Lattner76cb1892007-08-21 04:43:17 +0000241void AggExprEmitter::VisitBinAssign(const BinaryOperator *E) {
Eli Friedmanf0217122008-02-11 01:09:17 +0000242 // For an assignment to work, the value on the right has
243 // to be compatible with the value on the left.
244 assert(CGF.getContext().typesAreCompatible(
245 E->getLHS()->getType().getUnqualifiedType(),
246 E->getRHS()->getType().getUnqualifiedType())
247 && "Invalid assignment");
Chris Lattnerb50e3902007-08-21 04:25:47 +0000248 LValue LHS = CGF.EmitLValue(E->getLHS());
Chris Lattnerbdb8ffb2007-08-11 00:04:45 +0000249
250 // Codegen the RHS so that it stores directly into the LHS.
Chris Lattnerb50e3902007-08-21 04:25:47 +0000251 CGF.EmitAggExpr(E->getRHS(), LHS.getAddress(), false /*FIXME: VOLATILE LHS*/);
Chris Lattnerbdb8ffb2007-08-11 00:04:45 +0000252
Eli Friedmanf0217122008-02-11 01:09:17 +0000253 if (DestPtr == 0)
254 return;
255
Chris Lattnerbdb8ffb2007-08-11 00:04:45 +0000256 // If the result of the assignment is used, copy the RHS there also.
Eli Friedmanf0217122008-02-11 01:09:17 +0000257 EmitAggregateCopy(DestPtr, LHS.getAddress(), E->getType());
Chris Lattnerbdb8ffb2007-08-11 00:04:45 +0000258}
259
Chris Lattnerb50e3902007-08-21 04:25:47 +0000260void AggExprEmitter::VisitConditionalOperator(const ConditionalOperator *E) {
Gabor Greif815e2c12008-04-06 20:42:52 +0000261 llvm::BasicBlock *LHSBlock = llvm::BasicBlock::Create("cond.?");
262 llvm::BasicBlock *RHSBlock = llvm::BasicBlock::Create("cond.:");
263 llvm::BasicBlock *ContBlock = llvm::BasicBlock::Create("cond.cont");
Chris Lattnerbdb8ffb2007-08-11 00:04:45 +0000264
Chris Lattnerb50e3902007-08-21 04:25:47 +0000265 llvm::Value *Cond = CGF.EvaluateExprAsBool(E->getCond());
Chris Lattner41b1fca2007-08-26 23:13:56 +0000266 Builder.CreateCondBr(Cond, LHSBlock, RHSBlock);
Chris Lattnerbdb8ffb2007-08-11 00:04:45 +0000267
Chris Lattnerb50e3902007-08-21 04:25:47 +0000268 CGF.EmitBlock(LHSBlock);
Chris Lattnerbdb8ffb2007-08-11 00:04:45 +0000269
270 // Handle the GNU extension for missing LHS.
271 assert(E->getLHS() && "Must have LHS for aggregate value");
272
Chris Lattner55165ba2007-08-21 05:02:10 +0000273 Visit(E->getLHS());
Chris Lattner41b1fca2007-08-26 23:13:56 +0000274 Builder.CreateBr(ContBlock);
275 LHSBlock = Builder.GetInsertBlock();
Chris Lattnerbdb8ffb2007-08-11 00:04:45 +0000276
Chris Lattnerb50e3902007-08-21 04:25:47 +0000277 CGF.EmitBlock(RHSBlock);
Chris Lattnerbdb8ffb2007-08-11 00:04:45 +0000278
Chris Lattner55165ba2007-08-21 05:02:10 +0000279 Visit(E->getRHS());
Chris Lattner41b1fca2007-08-26 23:13:56 +0000280 Builder.CreateBr(ContBlock);
281 RHSBlock = Builder.GetInsertBlock();
Chris Lattnerbdb8ffb2007-08-11 00:04:45 +0000282
Chris Lattnerb50e3902007-08-21 04:25:47 +0000283 CGF.EmitBlock(ContBlock);
Chris Lattnerbdb8ffb2007-08-11 00:04:45 +0000284}
Chris Lattnera7300102007-08-21 04:59:27 +0000285
Eli Friedman42af3972008-05-27 15:51:49 +0000286void AggExprEmitter::VisitVAArgExpr(VAArgExpr *VE) {
287 llvm::Value *ArgValue = CGF.EmitLValue(VE->getSubExpr()).getAddress();
288 llvm::Value *V = Builder.CreateVAArg(ArgValue, CGF.ConvertType(VE->getType()));
289 if (DestPtr)
Eli Friedman2e630542008-06-13 23:01:12 +0000290 // FIXME: volatility
Eli Friedman42af3972008-05-27 15:51:49 +0000291 Builder.CreateStore(V, DestPtr);
292}
293
Lauro Ramos Venancio97b09572008-02-19 22:04:22 +0000294void AggExprEmitter::EmitNonConstInit(InitListExpr *E) {
295
296 const llvm::PointerType *APType =
297 cast<llvm::PointerType>(DestPtr->getType());
298 const llvm::Type *DestType = APType->getElementType();
Lauro Ramos Venancio9eb37a22008-02-18 22:44:02 +0000299
300 if (const llvm::ArrayType *AType = dyn_cast<llvm::ArrayType>(DestType)) {
Lauro Ramos Venancio97b09572008-02-19 22:04:22 +0000301 unsigned NumInitElements = E->getNumInits();
Lauro Ramos Venancio9eb37a22008-02-18 22:44:02 +0000302
Lauro Ramos Venancio9eb37a22008-02-18 22:44:02 +0000303 unsigned i;
304 for (i = 0; i != NumInitElements; ++i) {
Chris Lattner07307562008-03-19 05:19:41 +0000305 llvm::Value *NextVal = Builder.CreateStructGEP(DestPtr, i, ".array");
Lauro Ramos Venancio97b09572008-02-19 22:04:22 +0000306 Expr *Init = E->getInit(i);
307 if (isa<InitListExpr>(Init))
308 CGF.EmitAggExpr(Init, NextVal, VolatileDest);
309 else
Eli Friedman2e630542008-06-13 23:01:12 +0000310 // FIXME: volatility
Lauro Ramos Venancio97b09572008-02-19 22:04:22 +0000311 Builder.CreateStore(CGF.EmitScalarExpr(Init), NextVal);
Lauro Ramos Venancio9eb37a22008-02-18 22:44:02 +0000312 }
313
314 // Emit remaining default initializers
315 unsigned NumArrayElements = AType->getNumElements();
Lauro Ramos Venancio97b09572008-02-19 22:04:22 +0000316 QualType QType = E->getInit(0)->getType();
317 const llvm::Type *EType = AType->getElementType();
Lauro Ramos Venancio9eb37a22008-02-18 22:44:02 +0000318 for (/*Do not initialize i*/; i < NumArrayElements; ++i) {
Chris Lattner07307562008-03-19 05:19:41 +0000319 llvm::Value *NextVal = Builder.CreateStructGEP(DestPtr, i, ".array");
Dan Gohman377ba9f2008-05-22 22:12:56 +0000320 if (EType->isSingleValueType())
Eli Friedman2e630542008-06-13 23:01:12 +0000321 // FIXME: volatility
Lauro Ramos Venancio97b09572008-02-19 22:04:22 +0000322 Builder.CreateStore(llvm::Constant::getNullValue(EType), NextVal);
323 else
324 EmitAggregateClear(NextVal, QType);
Lauro Ramos Venancio9eb37a22008-02-18 22:44:02 +0000325 }
Lauro Ramos Venancio97b09572008-02-19 22:04:22 +0000326 } else
327 assert(false && "Invalid initializer");
Lauro Ramos Venancio9eb37a22008-02-18 22:44:02 +0000328}
329
Chris Lattner6ee6ab02008-04-04 18:42:16 +0000330void AggExprEmitter::EmitInitializationToLValue(Expr* E, LValue LV) {
331 // FIXME: Are initializers affected by volatile?
332 if (E->getType()->isComplexType()) {
333 CGF.EmitComplexExprIntoAddr(E, LV.getAddress(), false);
Eli Friedmanbb822fb2008-05-12 15:06:05 +0000334 } else if (CGF.hasAggregateLLVMType(E->getType())) {
335 CGF.EmitAnyExpr(E, LV.getAddress(), false);
336 } else {
337 CGF.EmitStoreThroughLValue(CGF.EmitAnyExpr(E), LV, E->getType());
Chris Lattner6ee6ab02008-04-04 18:42:16 +0000338 }
Chris Lattner6ee6ab02008-04-04 18:42:16 +0000339}
340
341void AggExprEmitter::EmitNullInitializationToLValue(LValue LV, QualType T) {
342 if (!CGF.hasAggregateLLVMType(T)) {
343 // For non-aggregates, we can store zero
Daniel Dunbarde813312008-08-06 05:32:55 +0000344 llvm::Value *Null = llvm::Constant::getNullValue(CGF.ConvertType(T));
345 CGF.EmitStoreThroughLValue(RValue::get(Null), LV, T);
Lauro Ramos Venancioa62b1df2008-02-19 19:27:31 +0000346 } else {
Chris Lattner6ee6ab02008-04-04 18:42:16 +0000347 // Otherwise, just memset the whole thing to zero. This is legal
348 // because in LLVM, all default initializers are guaranteed to have a
349 // bit pattern of all zeros.
350 // There's a potential optimization opportunity in combining
351 // memsets; that would be easy for arrays, but relatively
352 // difficult for structures with the current code.
353 llvm::Value *MemSet = CGF.CGM.getIntrinsic(llvm::Intrinsic::memset_i64);
354 uint64_t Size = CGF.getContext().getTypeSize(T);
355
356 const llvm::Type *BP = llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
357 llvm::Value* DestPtr = Builder.CreateBitCast(LV.getAddress(), BP, "tmp");
Chris Lattner7db5e942008-05-06 00:56:42 +0000358 Builder.CreateCall4(MemSet, DestPtr,
359 llvm::ConstantInt::get(llvm::Type::Int8Ty, 0),
360 llvm::ConstantInt::get(llvm::Type::Int64Ty, Size/8),
361 llvm::ConstantInt::get(llvm::Type::Int32Ty, 0));
Chris Lattner6ee6ab02008-04-04 18:42:16 +0000362 }
363}
364
Chris Lattner6ee6ab02008-04-04 18:42:16 +0000365void AggExprEmitter::VisitInitListExpr(InitListExpr *E) {
366 if (E->isConstantExpr(CGF.getContext(), 0)) {
367 // FIXME: call into const expr emitter so that we can emit
368 // a memcpy instead of storing the individual members.
369 // This is purely for perf; both codepaths lead to equivalent
370 // (although not necessarily identical) code.
371 // It's worth noting that LLVM keeps on getting smarter, though,
372 // so it might not be worth bothering.
373 }
374
375 // Handle initialization of an array.
376 if (E->getType()->isArrayType()) {
377 const llvm::PointerType *APType =
378 cast<llvm::PointerType>(DestPtr->getType());
379 const llvm::ArrayType *AType =
380 cast<llvm::ArrayType>(APType->getElementType());
381
382 uint64_t NumInitElements = E->getNumInits();
Eli Friedman48ec5622008-05-19 17:51:16 +0000383
Chris Lattnerc154ac12008-07-26 22:37:01 +0000384 if (E->getNumInits() > 0) {
385 QualType T1 = E->getType();
386 QualType T2 = E->getInit(0)->getType();
387 if (CGF.getContext().getCanonicalType(T1).getUnqualifiedType() ==
388 CGF.getContext().getCanonicalType(T2).getUnqualifiedType()) {
389 EmitAggLoadOfLValue(E->getInit(0));
390 return;
391 }
Eli Friedman48ec5622008-05-19 17:51:16 +0000392 }
393
Chris Lattner6ee6ab02008-04-04 18:42:16 +0000394 uint64_t NumArrayElements = AType->getNumElements();
Chris Lattnera1923f62008-08-04 07:31:14 +0000395 QualType ElementType = CGF.getContext().getCanonicalType(E->getType());
396 ElementType =CGF.getContext().getAsArrayType(ElementType)->getElementType();
Chris Lattner6ee6ab02008-04-04 18:42:16 +0000397
Chris Lattnera1923f62008-08-04 07:31:14 +0000398 unsigned CVRqualifier = ElementType.getCVRQualifiers();
Eli Friedman2e630542008-06-13 23:01:12 +0000399
Chris Lattner6ee6ab02008-04-04 18:42:16 +0000400 for (uint64_t i = 0; i != NumArrayElements; ++i) {
401 llvm::Value *NextVal = Builder.CreateStructGEP(DestPtr, i, ".array");
402 if (i < NumInitElements)
Eli Friedman2e630542008-06-13 23:01:12 +0000403 EmitInitializationToLValue(E->getInit(i),
404 LValue::MakeAddr(NextVal, CVRqualifier));
Chris Lattner6ee6ab02008-04-04 18:42:16 +0000405 else
Eli Friedman2e630542008-06-13 23:01:12 +0000406 EmitNullInitializationToLValue(LValue::MakeAddr(NextVal, CVRqualifier),
Chris Lattner6ee6ab02008-04-04 18:42:16 +0000407 ElementType);
Lauro Ramos Venancioa62b1df2008-02-19 19:27:31 +0000408 }
Chris Lattner6ee6ab02008-04-04 18:42:16 +0000409 return;
410 }
411
412 assert(E->getType()->isRecordType() && "Only support structs/unions here!");
413
414 // Do struct initialization; this code just sets each individual member
415 // to the approprate value. This makes bitfield support automatic;
416 // the disadvantage is that the generated code is more difficult for
417 // the optimizer, especially with bitfields.
418 unsigned NumInitElements = E->getNumInits();
419 RecordDecl *SD = E->getType()->getAsRecordType()->getDecl();
420 unsigned NumMembers = SD->getNumMembers() - SD->hasFlexibleArrayMember();
421 unsigned CurInitVal = 0;
422 bool isUnion = E->getType()->isUnionType();
423
424 // Here we iterate over the fields; this makes it simpler to both
425 // default-initialize fields and skip over unnamed fields.
426 for (unsigned CurFieldNo = 0; CurFieldNo != NumMembers; ++CurFieldNo) {
Chris Lattner6ee6ab02008-04-04 18:42:16 +0000427 FieldDecl *CurField = SD->getMember(CurFieldNo);
428 if (CurField->getIdentifier() == 0) {
429 // Initializers can't initialize unnamed fields, e.g. "int : 20;"
430 continue;
431 }
Eli Friedman2e630542008-06-13 23:01:12 +0000432 // FIXME: volatility
433 LValue FieldLoc = CGF.EmitLValueForField(DestPtr, CurField, isUnion,0);
Chris Lattner6ee6ab02008-04-04 18:42:16 +0000434 if (CurInitVal < NumInitElements) {
435 // Store the initializer into the field
436 // This will probably have to get a bit smarter when we support
437 // designators in initializers
438 EmitInitializationToLValue(E->getInit(CurInitVal++), FieldLoc);
439 } else {
440 // We're out of initalizers; default-initialize to null
441 EmitNullInitializationToLValue(FieldLoc, CurField->getType());
442 }
443
444 // Unions only initialize one field.
445 // (things can get weird with designators, but they aren't
446 // supported yet.)
447 if (E->getType()->isUnionType())
448 break;
Lauro Ramos Venancioa62b1df2008-02-19 19:27:31 +0000449 }
Devang Patelddde6d52007-10-26 17:44:44 +0000450}
451
Chris Lattnera7300102007-08-21 04:59:27 +0000452//===----------------------------------------------------------------------===//
453// Entry Points into this File
454//===----------------------------------------------------------------------===//
455
456/// EmitAggExpr - Emit the computation of the specified expression of
457/// aggregate type. The result is computed into DestPtr. Note that if
458/// DestPtr is null, the value of the aggregate expression is not needed.
459void CodeGenFunction::EmitAggExpr(const Expr *E, llvm::Value *DestPtr,
460 bool VolatileDest) {
461 assert(E && hasAggregateLLVMType(E->getType()) &&
462 "Invalid aggregate expression to emit");
463
464 AggExprEmitter(*this, DestPtr, VolatileDest).Visit(const_cast<Expr*>(E));
465}