blob: b0596e2feebd0f68ee0de8e71e40ec8e649e25ec [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()); }
Eli Friedman12444a22009-01-27 09:03:41 +000061 void VisitUnaryExtension(UnaryOperator *E) { Visit(E->getSubExpr()); }
Chris Lattner9c033562007-08-21 04:25:47 +000062
63 // l-values.
Seo Sanghyeon9b73b392007-12-14 02:04:12 +000064 void VisitDeclRefExpr(DeclRefExpr *DRE) { EmitAggLoadOfLValue(DRE); }
65 void VisitMemberExpr(MemberExpr *ME) { EmitAggLoadOfLValue(ME); }
66 void VisitUnaryDeref(UnaryOperator *E) { EmitAggLoadOfLValue(E); }
Seo Sanghyeonad6ebd62007-12-23 03:11:58 +000067 void VisitStringLiteral(StringLiteral *E) { EmitAggLoadOfLValue(E); }
Eli Friedmanb1851242008-05-27 15:51:49 +000068 void VisitCompoundLiteralExpr(CompoundLiteralExpr *E)
69 { EmitAggLoadOfLValue(E); }
Seo Sanghyeon9b73b392007-12-14 02:04:12 +000070
71 void VisitArraySubscriptExpr(ArraySubscriptExpr *E) {
72 EmitAggLoadOfLValue(E);
73 }
Douglas Gregor4c678342009-01-28 21:54:33 +000074
Chris Lattner9c033562007-08-21 04:25:47 +000075 // Operators.
76 // case Expr::UnaryOperatorClass:
Chris Lattner9c033562007-08-21 04:25:47 +000077 // case Expr::CastExprClass:
Nuno Lopes7e916272009-01-15 20:14:33 +000078 void VisitCStyleCastExpr(CStyleCastExpr *E);
Anders Carlssone4707ff2008-01-14 06:28:57 +000079 void VisitImplicitCastExpr(ImplicitCastExpr *E);
Anders Carlsson148fe672007-10-31 22:04:46 +000080 void VisitCallExpr(const CallExpr *E);
Chris Lattnerb2d963f2007-08-31 22:54:14 +000081 void VisitStmtExpr(const StmtExpr *E);
Chris Lattner9c033562007-08-21 04:25:47 +000082 void VisitBinaryOperator(const BinaryOperator *BO);
Chris Lattner03d6fb92007-08-21 04:43:17 +000083 void VisitBinAssign(const BinaryOperator *E);
Eli Friedman07fa52a2008-05-20 07:56:31 +000084 void VisitBinComma(const BinaryOperator *E);
Chris Lattner9c033562007-08-21 04:25:47 +000085
Chris Lattner8fdf3282008-06-24 17:04:18 +000086 void VisitObjCMessageExpr(ObjCMessageExpr *E);
Daniel Dunbar0a04d772008-08-23 10:51:21 +000087 void VisitObjCIvarRefExpr(ObjCIvarRefExpr *E) {
88 EmitAggLoadOfLValue(E);
89 }
Daniel Dunbar9c3fc702008-08-27 06:57:25 +000090 void VisitObjCPropertyRefExpr(ObjCPropertyRefExpr *E);
Fariborz Jahanian5daf5702008-11-22 18:39:36 +000091 void VisitObjCKVCRefExpr(ObjCKVCRefExpr *E);
Chris Lattner9c033562007-08-21 04:25:47 +000092
93 void VisitConditionalOperator(const ConditionalOperator *CO);
Devang Patel636c3d02007-10-26 17:44:44 +000094 void VisitInitListExpr(InitListExpr *E);
Chris Lattner04421082008-04-08 04:40:51 +000095 void VisitCXXDefaultArgExpr(CXXDefaultArgExpr *DAE) {
96 Visit(DAE->getExpr());
97 }
Eli Friedmanb1851242008-05-27 15:51:49 +000098 void VisitVAArgExpr(VAArgExpr *E);
Chris Lattnerf81557c2008-04-04 18:42:16 +000099
100 void EmitInitializationToLValue(Expr *E, LValue Address);
101 void EmitNullInitializationToLValue(LValue Address, QualType T);
Chris Lattner9c033562007-08-21 04:25:47 +0000102 // case Expr::ChooseExprClass:
Lauro Ramos Venancio305762c2008-02-18 22:44:02 +0000103
Chris Lattner9c033562007-08-21 04:25:47 +0000104};
105} // end anonymous namespace.
106
Chris Lattneree755f92007-08-21 04:59:27 +0000107//===----------------------------------------------------------------------===//
108// Utilities
109//===----------------------------------------------------------------------===//
Chris Lattner9c033562007-08-21 04:25:47 +0000110
Chris Lattner883f6a72007-08-11 00:04:45 +0000111/// EmitAggLoadOfLValue - Given an expression with aggregate type that
112/// represents a value lvalue, this method emits the address of the lvalue,
113/// then loads the result into DestPtr.
Chris Lattner9c033562007-08-21 04:25:47 +0000114void AggExprEmitter::EmitAggLoadOfLValue(const Expr *E) {
115 LValue LV = CGF.EmitLValue(E);
Chris Lattner883f6a72007-08-11 00:04:45 +0000116 assert(LV.isSimple() && "Can't have aggregate bitfield, vector, etc");
117 llvm::Value *SrcPtr = LV.getAddress();
118
119 // If the result is ignored, don't copy from the value.
120 if (DestPtr == 0)
121 // FIXME: If the source is volatile, we must read from it.
122 return;
123
Daniel Dunbar7482d122008-09-09 20:49:46 +0000124 CGF.EmitAggregateCopy(DestPtr, SrcPtr, E->getType());
Chris Lattner883f6a72007-08-11 00:04:45 +0000125}
126
Chris Lattneree755f92007-08-21 04:59:27 +0000127//===----------------------------------------------------------------------===//
128// Visitor Methods
129//===----------------------------------------------------------------------===//
130
Nuno Lopes7e916272009-01-15 20:14:33 +0000131void AggExprEmitter::VisitCStyleCastExpr(CStyleCastExpr *E) {
132 // GCC union extension
133 if (E->getType()->isUnionType()) {
134 RecordDecl *SD = E->getType()->getAsRecordType()->getDecl();
135 LValue FieldLoc = CGF.EmitLValueForField(DestPtr, *SD->field_begin(), true, 0);
136 EmitInitializationToLValue(E->getSubExpr(), FieldLoc);
137 return;
138 }
139
140 Visit(E->getSubExpr());
141}
142
Chris Lattner96196622008-07-26 22:37:01 +0000143void AggExprEmitter::VisitImplicitCastExpr(ImplicitCastExpr *E) {
Eli Friedmanff6e2b72008-02-11 01:09:17 +0000144 assert(CGF.getContext().typesAreCompatible(
Chris Lattner96196622008-07-26 22:37:01 +0000145 E->getSubExpr()->getType().getUnqualifiedType(),
146 E->getType().getUnqualifiedType()) &&
147 "Implicit cast types must be compatible");
Anders Carlssone4707ff2008-01-14 06:28:57 +0000148 Visit(E->getSubExpr());
149}
150
Chris Lattner96196622008-07-26 22:37:01 +0000151void AggExprEmitter::VisitCallExpr(const CallExpr *E) {
Anders Carlsson148fe672007-10-31 22:04:46 +0000152 RValue RV = CGF.EmitCallExpr(E);
153 assert(RV.isAggregate() && "Return value must be aggregate value!");
154
155 // If the result is ignored, don't copy from the value.
156 if (DestPtr == 0)
157 // FIXME: If the source is volatile, we must read from it.
158 return;
159
Daniel Dunbar7482d122008-09-09 20:49:46 +0000160 CGF.EmitAggregateCopy(DestPtr, RV.getAggregateAddr(), E->getType());
Anders Carlsson148fe672007-10-31 22:04:46 +0000161}
Chris Lattner96196622008-07-26 22:37:01 +0000162
163void AggExprEmitter::VisitObjCMessageExpr(ObjCMessageExpr *E) {
Daniel Dunbar8f2926b2008-08-23 03:46:30 +0000164 RValue RV = CGF.EmitObjCMessageExpr(E);
165 assert(RV.isAggregate() && "Return value must be aggregate value!");
Chris Lattner8fdf3282008-06-24 17:04:18 +0000166
167 // If the result is ignored, don't copy from the value.
168 if (DestPtr == 0)
Daniel Dunbar8f2926b2008-08-23 03:46:30 +0000169 // FIXME: If the source is volatile, we must read from it.
Chris Lattner8fdf3282008-06-24 17:04:18 +0000170 return;
171
Daniel Dunbar7482d122008-09-09 20:49:46 +0000172 CGF.EmitAggregateCopy(DestPtr, RV.getAggregateAddr(), E->getType());
Chris Lattner8fdf3282008-06-24 17:04:18 +0000173}
Anders Carlsson148fe672007-10-31 22:04:46 +0000174
Daniel Dunbar9c3fc702008-08-27 06:57:25 +0000175void AggExprEmitter::VisitObjCPropertyRefExpr(ObjCPropertyRefExpr *E) {
176 RValue RV = CGF.EmitObjCPropertyGet(E);
177 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());
Daniel Dunbar9c3fc702008-08-27 06:57:25 +0000185}
186
Fariborz Jahanian5daf5702008-11-22 18:39:36 +0000187void AggExprEmitter::VisitObjCKVCRefExpr(ObjCKVCRefExpr *E) {
188 RValue RV = CGF.EmitObjCPropertyGet(E);
189 assert(RV.isAggregate() && "Return value must be aggregate value!");
190
191 // If the result is ignored, don't copy from the value.
192 if (DestPtr == 0)
193 // FIXME: If the source is volatile, we must read from it.
194 return;
195
196 CGF.EmitAggregateCopy(DestPtr, RV.getAggregateAddr(), E->getType());
197}
198
Chris Lattner96196622008-07-26 22:37:01 +0000199void AggExprEmitter::VisitBinComma(const BinaryOperator *E) {
Eli Friedman07fa52a2008-05-20 07:56:31 +0000200 CGF.EmitAnyExpr(E->getLHS());
201 CGF.EmitAggExpr(E->getRHS(), DestPtr, false);
202}
203
Chris Lattnerb2d963f2007-08-31 22:54:14 +0000204void AggExprEmitter::VisitStmtExpr(const StmtExpr *E) {
205 CGF.EmitCompoundStmt(*E->getSubStmt(), true, DestPtr, VolatileDest);
206}
207
Chris Lattner9c033562007-08-21 04:25:47 +0000208void AggExprEmitter::VisitBinaryOperator(const BinaryOperator *E) {
Daniel Dunbar488e9932008-08-16 00:56:44 +0000209 CGF.ErrorUnsupported(E, "aggregate binary expression");
Chris Lattneree755f92007-08-21 04:59:27 +0000210}
211
Chris Lattner03d6fb92007-08-21 04:43:17 +0000212void AggExprEmitter::VisitBinAssign(const BinaryOperator *E) {
Eli Friedmanff6e2b72008-02-11 01:09:17 +0000213 // For an assignment to work, the value on the right has
214 // to be compatible with the value on the left.
215 assert(CGF.getContext().typesAreCompatible(
216 E->getLHS()->getType().getUnqualifiedType(),
217 E->getRHS()->getType().getUnqualifiedType())
218 && "Invalid assignment");
Chris Lattner9c033562007-08-21 04:25:47 +0000219 LValue LHS = CGF.EmitLValue(E->getLHS());
Chris Lattner883f6a72007-08-11 00:04:45 +0000220
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000221 // We have to special case property setters, otherwise we must have
222 // a simple lvalue (no aggregates inside vectors, bitfields).
223 if (LHS.isPropertyRef()) {
224 // FIXME: Volatility?
225 llvm::Value *AggLoc = DestPtr;
226 if (!AggLoc)
227 AggLoc = CGF.CreateTempAlloca(CGF.ConvertType(E->getRHS()->getType()));
228 CGF.EmitAggExpr(E->getRHS(), AggLoc, false);
229 CGF.EmitObjCPropertySet(LHS.getPropertyRefExpr(),
230 RValue::getAggregate(AggLoc));
Fariborz Jahanian43f44702008-11-22 22:30:21 +0000231 }
232 else if (LHS.isKVCRef()) {
233 // FIXME: Volatility?
234 llvm::Value *AggLoc = DestPtr;
235 if (!AggLoc)
236 AggLoc = CGF.CreateTempAlloca(CGF.ConvertType(E->getRHS()->getType()));
237 CGF.EmitAggExpr(E->getRHS(), AggLoc, false);
238 CGF.EmitObjCPropertySet(LHS.getKVCRefExpr(),
239 RValue::getAggregate(AggLoc));
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000240 } else {
241 // Codegen the RHS so that it stores directly into the LHS.
242 CGF.EmitAggExpr(E->getRHS(), LHS.getAddress(), false /*FIXME: VOLATILE LHS*/);
243
244 if (DestPtr == 0)
245 return;
246
247 // If the result of the assignment is used, copy the RHS there also.
Daniel Dunbar7482d122008-09-09 20:49:46 +0000248 CGF.EmitAggregateCopy(DestPtr, LHS.getAddress(), E->getType());
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000249 }
Chris Lattner883f6a72007-08-11 00:04:45 +0000250}
251
Chris Lattner9c033562007-08-21 04:25:47 +0000252void AggExprEmitter::VisitConditionalOperator(const ConditionalOperator *E) {
Daniel Dunbar9615ecb2008-11-13 01:38:36 +0000253 llvm::BasicBlock *LHSBlock = CGF.createBasicBlock("cond.true");
254 llvm::BasicBlock *RHSBlock = CGF.createBasicBlock("cond.false");
255 llvm::BasicBlock *ContBlock = CGF.createBasicBlock("cond.end");
Chris Lattner883f6a72007-08-11 00:04:45 +0000256
Chris Lattner9c033562007-08-21 04:25:47 +0000257 llvm::Value *Cond = CGF.EvaluateExprAsBool(E->getCond());
Chris Lattnerbfc0c1a2007-08-26 23:13:56 +0000258 Builder.CreateCondBr(Cond, LHSBlock, RHSBlock);
Chris Lattner883f6a72007-08-11 00:04:45 +0000259
Chris Lattner9c033562007-08-21 04:25:47 +0000260 CGF.EmitBlock(LHSBlock);
Chris Lattner883f6a72007-08-11 00:04:45 +0000261
262 // Handle the GNU extension for missing LHS.
263 assert(E->getLHS() && "Must have LHS for aggregate value");
264
Chris Lattnerc748f272007-08-21 05:02:10 +0000265 Visit(E->getLHS());
Daniel Dunbard57a8712008-11-11 09:41:28 +0000266 CGF.EmitBranch(ContBlock);
Chris Lattner883f6a72007-08-11 00:04:45 +0000267
Chris Lattner9c033562007-08-21 04:25:47 +0000268 CGF.EmitBlock(RHSBlock);
Chris Lattner883f6a72007-08-11 00:04:45 +0000269
Chris Lattnerc748f272007-08-21 05:02:10 +0000270 Visit(E->getRHS());
Daniel Dunbard57a8712008-11-11 09:41:28 +0000271 CGF.EmitBranch(ContBlock);
Chris Lattner883f6a72007-08-11 00:04:45 +0000272
Chris Lattner9c033562007-08-21 04:25:47 +0000273 CGF.EmitBlock(ContBlock);
Chris Lattner883f6a72007-08-11 00:04:45 +0000274}
Chris Lattneree755f92007-08-21 04:59:27 +0000275
Eli Friedmanb1851242008-05-27 15:51:49 +0000276void AggExprEmitter::VisitVAArgExpr(VAArgExpr *VE) {
Daniel Dunbar07855702009-02-11 22:25:55 +0000277 llvm::Value *ArgValue = CGF.EmitVAListRef(VE->getSubExpr());
Anders Carlssonddf7cac2008-11-04 05:30:00 +0000278 llvm::Value *ArgPtr = CGF.EmitVAArg(ArgValue, VE->getType());
279
Sebastian Redl0262f022009-01-09 21:09:38 +0000280 if (!ArgPtr) {
Anders Carlssonddf7cac2008-11-04 05:30:00 +0000281 CGF.ErrorUnsupported(VE, "aggregate va_arg expression");
Sebastian Redl0262f022009-01-09 21:09:38 +0000282 return;
283 }
284
Eli Friedmanb1851242008-05-27 15:51:49 +0000285 if (DestPtr)
Eli Friedman1e692ac2008-06-13 23:01:12 +0000286 // FIXME: volatility
Anders Carlssonddf7cac2008-11-04 05:30:00 +0000287 CGF.EmitAggregateCopy(DestPtr, ArgPtr, VE->getType());
Eli Friedmanb1851242008-05-27 15:51:49 +0000288}
289
Lauro Ramos Venancio13e22cf2008-02-19 22:04:22 +0000290void AggExprEmitter::EmitNonConstInit(InitListExpr *E) {
Lauro Ramos Venancio13e22cf2008-02-19 22:04:22 +0000291 const llvm::PointerType *APType =
292 cast<llvm::PointerType>(DestPtr->getType());
293 const llvm::Type *DestType = APType->getElementType();
Lauro Ramos Venancio305762c2008-02-18 22:44:02 +0000294
Douglas Gregora9c87802009-01-29 19:42:23 +0000295 if (E->hadArrayRangeDesignator()) {
296 CGF.ErrorUnsupported(E, "GNU array range designator extension");
297 }
298
Lauro Ramos Venancio305762c2008-02-18 22:44:02 +0000299 if (const llvm::ArrayType *AType = dyn_cast<llvm::ArrayType>(DestType)) {
Lauro Ramos Venancio13e22cf2008-02-19 22:04:22 +0000300 unsigned NumInitElements = E->getNumInits();
Lauro Ramos Venancio305762c2008-02-18 22:44:02 +0000301
Lauro Ramos Venancio305762c2008-02-18 22:44:02 +0000302 unsigned i;
303 for (i = 0; i != NumInitElements; ++i) {
Chris Lattner36b6a0a2008-03-19 05:19:41 +0000304 llvm::Value *NextVal = Builder.CreateStructGEP(DestPtr, i, ".array");
Lauro Ramos Venancio13e22cf2008-02-19 22:04:22 +0000305 Expr *Init = E->getInit(i);
306 if (isa<InitListExpr>(Init))
307 CGF.EmitAggExpr(Init, NextVal, VolatileDest);
308 else
Eli Friedman1e692ac2008-06-13 23:01:12 +0000309 // FIXME: volatility
Lauro Ramos Venancio13e22cf2008-02-19 22:04:22 +0000310 Builder.CreateStore(CGF.EmitScalarExpr(Init), NextVal);
Lauro Ramos Venancio305762c2008-02-18 22:44:02 +0000311 }
312
313 // Emit remaining default initializers
314 unsigned NumArrayElements = AType->getNumElements();
Lauro Ramos Venancio13e22cf2008-02-19 22:04:22 +0000315 QualType QType = E->getInit(0)->getType();
316 const llvm::Type *EType = AType->getElementType();
Lauro Ramos Venancio305762c2008-02-18 22:44:02 +0000317 for (/*Do not initialize i*/; i < NumArrayElements; ++i) {
Chris Lattner36b6a0a2008-03-19 05:19:41 +0000318 llvm::Value *NextVal = Builder.CreateStructGEP(DestPtr, i, ".array");
Dan Gohmand79a7262008-05-22 22:12:56 +0000319 if (EType->isSingleValueType())
Eli Friedman1e692ac2008-06-13 23:01:12 +0000320 // FIXME: volatility
Lauro Ramos Venancio13e22cf2008-02-19 22:04:22 +0000321 Builder.CreateStore(llvm::Constant::getNullValue(EType), NextVal);
322 else
Daniel Dunbar7482d122008-09-09 20:49:46 +0000323 CGF.EmitAggregateClear(NextVal, QType);
Lauro Ramos Venancio305762c2008-02-18 22:44:02 +0000324 }
Lauro Ramos Venancio13e22cf2008-02-19 22:04:22 +0000325 } else
326 assert(false && "Invalid initializer");
Lauro Ramos Venancio305762c2008-02-18 22:44:02 +0000327}
328
Chris Lattnerf81557c2008-04-04 18:42:16 +0000329void AggExprEmitter::EmitInitializationToLValue(Expr* E, LValue LV) {
330 // FIXME: Are initializers affected by volatile?
Douglas Gregor3498bdb2009-01-29 17:44:32 +0000331 if (isa<ImplicitValueInitExpr>(E)) {
Douglas Gregor4c678342009-01-28 21:54:33 +0000332 EmitNullInitializationToLValue(LV, E->getType());
Douglas Gregor3498bdb2009-01-29 17:44:32 +0000333 } else if (E->getType()->isComplexType()) {
334 CGF.EmitComplexExprIntoAddr(E, LV.getAddress(), false);
Eli Friedmanc8ba9612008-05-12 15:06:05 +0000335 } else if (CGF.hasAggregateLLVMType(E->getType())) {
336 CGF.EmitAnyExpr(E, LV.getAddress(), false);
337 } else {
338 CGF.EmitStoreThroughLValue(CGF.EmitAnyExpr(E), LV, E->getType());
Chris Lattnerf81557c2008-04-04 18:42:16 +0000339 }
Chris Lattnerf81557c2008-04-04 18:42:16 +0000340}
341
342void AggExprEmitter::EmitNullInitializationToLValue(LValue LV, QualType T) {
343 if (!CGF.hasAggregateLLVMType(T)) {
344 // For non-aggregates, we can store zero
Daniel Dunbar82397132008-08-06 05:32:55 +0000345 llvm::Value *Null = llvm::Constant::getNullValue(CGF.ConvertType(T));
346 CGF.EmitStoreThroughLValue(RValue::get(Null), LV, T);
Lauro Ramos Venancio145cd892008-02-19 19:27:31 +0000347 } else {
Chris Lattnerf81557c2008-04-04 18:42:16 +0000348 // Otherwise, just memset the whole thing to zero. This is legal
349 // because in LLVM, all default initializers are guaranteed to have a
350 // bit pattern of all zeros.
351 // There's a potential optimization opportunity in combining
352 // memsets; that would be easy for arrays, but relatively
353 // difficult for structures with the current code.
Chris Lattner4e8a9e82008-11-21 16:43:15 +0000354 const llvm::Type *SizeTy = llvm::Type::Int64Ty;
355 llvm::Value *MemSet = CGF.CGM.getIntrinsic(llvm::Intrinsic::memset,
356 &SizeTy, 1);
Chris Lattnerf81557c2008-04-04 18:42:16 +0000357 uint64_t Size = CGF.getContext().getTypeSize(T);
358
359 const llvm::Type *BP = llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
360 llvm::Value* DestPtr = Builder.CreateBitCast(LV.getAddress(), BP, "tmp");
Chris Lattner3eae03e2008-05-06 00:56:42 +0000361 Builder.CreateCall4(MemSet, DestPtr,
362 llvm::ConstantInt::get(llvm::Type::Int8Ty, 0),
Chris Lattner4e8a9e82008-11-21 16:43:15 +0000363 llvm::ConstantInt::get(SizeTy, Size/8),
Chris Lattner3eae03e2008-05-06 00:56:42 +0000364 llvm::ConstantInt::get(llvm::Type::Int32Ty, 0));
Chris Lattnerf81557c2008-04-04 18:42:16 +0000365 }
366}
367
Chris Lattnerf81557c2008-04-04 18:42:16 +0000368void AggExprEmitter::VisitInitListExpr(InitListExpr *E) {
Eli Friedmana385b3c2008-12-02 01:17:45 +0000369#if 0
370 // FIXME: Disabled while we figure out what to do about
371 // test/CodeGen/bitfield.c
372 //
Eli Friedman994ffef2008-11-30 02:11:09 +0000373 // If we can, prefer a copy from a global; this is a lot less
374 // code for long globals, and it's easier for the current optimizers
375 // to analyze.
376 // FIXME: Should we really be doing this? Should we try to avoid
377 // cases where we emit a global with a lot of zeros? Should
378 // we try to avoid short globals?
Eli Friedmanc9e8f602009-01-25 02:32:41 +0000379 if (E->isConstantInitializer(CGF.getContext(), 0)) {
Eli Friedman994ffef2008-11-30 02:11:09 +0000380 llvm::Constant* C = CGF.CGM.EmitConstantExpr(E, &CGF);
381 llvm::GlobalVariable* GV =
382 new llvm::GlobalVariable(C->getType(), true,
383 llvm::GlobalValue::InternalLinkage,
384 C, "", &CGF.CGM.getModule(), 0);
385 CGF.EmitAggregateCopy(DestPtr, GV, E->getType());
386 return;
387 }
Eli Friedmana385b3c2008-12-02 01:17:45 +0000388#endif
Douglas Gregora9c87802009-01-29 19:42:23 +0000389 if (E->hadArrayRangeDesignator()) {
390 CGF.ErrorUnsupported(E, "GNU array range designator extension");
391 }
392
Chris Lattnerf81557c2008-04-04 18:42:16 +0000393 // Handle initialization of an array.
394 if (E->getType()->isArrayType()) {
395 const llvm::PointerType *APType =
396 cast<llvm::PointerType>(DestPtr->getType());
397 const llvm::ArrayType *AType =
398 cast<llvm::ArrayType>(APType->getElementType());
399
400 uint64_t NumInitElements = E->getNumInits();
Eli Friedman922696f2008-05-19 17:51:16 +0000401
Chris Lattner96196622008-07-26 22:37:01 +0000402 if (E->getNumInits() > 0) {
403 QualType T1 = E->getType();
404 QualType T2 = E->getInit(0)->getType();
405 if (CGF.getContext().getCanonicalType(T1).getUnqualifiedType() ==
406 CGF.getContext().getCanonicalType(T2).getUnqualifiedType()) {
407 EmitAggLoadOfLValue(E->getInit(0));
408 return;
409 }
Eli Friedman922696f2008-05-19 17:51:16 +0000410 }
411
Chris Lattnerf81557c2008-04-04 18:42:16 +0000412 uint64_t NumArrayElements = AType->getNumElements();
Chris Lattnerc63a1f22008-08-04 07:31:14 +0000413 QualType ElementType = CGF.getContext().getCanonicalType(E->getType());
Douglas Gregor4c678342009-01-28 21:54:33 +0000414 ElementType = CGF.getContext().getAsArrayType(ElementType)->getElementType();
Chris Lattnerf81557c2008-04-04 18:42:16 +0000415
Chris Lattnerc63a1f22008-08-04 07:31:14 +0000416 unsigned CVRqualifier = ElementType.getCVRQualifiers();
Eli Friedman1e692ac2008-06-13 23:01:12 +0000417
Chris Lattnerf81557c2008-04-04 18:42:16 +0000418 for (uint64_t i = 0; i != NumArrayElements; ++i) {
419 llvm::Value *NextVal = Builder.CreateStructGEP(DestPtr, i, ".array");
420 if (i < NumInitElements)
Eli Friedman1e692ac2008-06-13 23:01:12 +0000421 EmitInitializationToLValue(E->getInit(i),
422 LValue::MakeAddr(NextVal, CVRqualifier));
Chris Lattnerf81557c2008-04-04 18:42:16 +0000423 else
Eli Friedman1e692ac2008-06-13 23:01:12 +0000424 EmitNullInitializationToLValue(LValue::MakeAddr(NextVal, CVRqualifier),
Chris Lattnerf81557c2008-04-04 18:42:16 +0000425 ElementType);
Lauro Ramos Venancio145cd892008-02-19 19:27:31 +0000426 }
Chris Lattnerf81557c2008-04-04 18:42:16 +0000427 return;
428 }
429
430 assert(E->getType()->isRecordType() && "Only support structs/unions here!");
431
432 // Do struct initialization; this code just sets each individual member
433 // to the approprate value. This makes bitfield support automatic;
434 // the disadvantage is that the generated code is more difficult for
435 // the optimizer, especially with bitfields.
436 unsigned NumInitElements = E->getNumInits();
437 RecordDecl *SD = E->getType()->getAsRecordType()->getDecl();
Chris Lattnerf81557c2008-04-04 18:42:16 +0000438 unsigned CurInitVal = 0;
Douglas Gregor0bb76892009-01-29 16:53:55 +0000439
440 if (E->getType()->isUnionType()) {
441 // Only initialize one field of a union. The field itself is
442 // specified by the initializer list.
443 if (!E->getInitializedFieldInUnion()) {
444 // Empty union; we have nothing to do.
445
446#ifndef NDEBUG
447 // Make sure that it's really an empty and not a failure of
448 // semantic analysis.
449 for (RecordDecl::field_iterator Field = SD->field_begin(),
450 FieldEnd = SD->field_end();
451 Field != FieldEnd; ++Field)
452 assert(Field->isUnnamedBitfield() && "Only unnamed bitfields allowed");
453#endif
454 return;
455 }
456
457 // FIXME: volatility
458 FieldDecl *Field = E->getInitializedFieldInUnion();
459 LValue FieldLoc = CGF.EmitLValueForField(DestPtr, Field, true, 0);
460
461 if (NumInitElements) {
462 // Store the initializer into the field
463 EmitInitializationToLValue(E->getInit(0), FieldLoc);
464 } else {
465 // Default-initialize to null
466 EmitNullInitializationToLValue(FieldLoc, Field->getType());
467 }
468
469 return;
470 }
Chris Lattnerf81557c2008-04-04 18:42:16 +0000471
472 // Here we iterate over the fields; this makes it simpler to both
473 // default-initialize fields and skip over unnamed fields.
Douglas Gregor44b43212008-12-11 16:49:14 +0000474 for (RecordDecl::field_iterator Field = SD->field_begin(),
475 FieldEnd = SD->field_end();
476 Field != FieldEnd; ++Field) {
477 // We're done once we hit the flexible array member
478 if (Field->getType()->isIncompleteArrayType())
479 break;
480
Douglas Gregor34e79462009-01-28 23:36:17 +0000481 if (Field->isUnnamedBitfield())
Chris Lattnerf81557c2008-04-04 18:42:16 +0000482 continue;
Douglas Gregor34e79462009-01-28 23:36:17 +0000483
Eli Friedman1e692ac2008-06-13 23:01:12 +0000484 // FIXME: volatility
Douglas Gregor0bb76892009-01-29 16:53:55 +0000485 LValue FieldLoc = CGF.EmitLValueForField(DestPtr, *Field, false, 0);
Chris Lattnerf81557c2008-04-04 18:42:16 +0000486 if (CurInitVal < NumInitElements) {
487 // Store the initializer into the field
Chris Lattnerf81557c2008-04-04 18:42:16 +0000488 EmitInitializationToLValue(E->getInit(CurInitVal++), FieldLoc);
489 } else {
490 // We're out of initalizers; default-initialize to null
Douglas Gregor44b43212008-12-11 16:49:14 +0000491 EmitNullInitializationToLValue(FieldLoc, Field->getType());
Chris Lattnerf81557c2008-04-04 18:42:16 +0000492 }
Lauro Ramos Venancio145cd892008-02-19 19:27:31 +0000493 }
Devang Patel636c3d02007-10-26 17:44:44 +0000494}
495
Chris Lattneree755f92007-08-21 04:59:27 +0000496//===----------------------------------------------------------------------===//
497// Entry Points into this File
498//===----------------------------------------------------------------------===//
499
500/// EmitAggExpr - Emit the computation of the specified expression of
501/// aggregate type. The result is computed into DestPtr. Note that if
502/// DestPtr is null, the value of the aggregate expression is not needed.
503void CodeGenFunction::EmitAggExpr(const Expr *E, llvm::Value *DestPtr,
504 bool VolatileDest) {
505 assert(E && hasAggregateLLVMType(E->getType()) &&
506 "Invalid aggregate expression to emit");
507
508 AggExprEmitter(*this, DestPtr, VolatileDest).Visit(const_cast<Expr*>(E));
509}
Daniel Dunbar7482d122008-09-09 20:49:46 +0000510
511void CodeGenFunction::EmitAggregateClear(llvm::Value *DestPtr, QualType Ty) {
512 assert(!Ty->isAnyComplexType() && "Shouldn't happen for complex");
513
514 EmitMemSetToZero(DestPtr, Ty);
515}
516
517void CodeGenFunction::EmitAggregateCopy(llvm::Value *DestPtr,
518 llvm::Value *SrcPtr, QualType Ty) {
519 assert(!Ty->isAnyComplexType() && "Shouldn't happen for complex");
520
Chris Lattnerca4fc2c2009-02-28 18:18:58 +0000521 // Aggregate assignment turns into llvm.memset. This is almost valid per
522 // C99 6.5.16.1p3, which states "If the value being stored in an object is
523 // read from another object that overlaps in anyway the storage of the first
524 // object, then the overlap shall be exact and the two objects shall have
525 // qualified or unqualified versions of a compatible type."
526 //
527 // memset is not defined if the source and destination pointers are exactly
528 // equal, but other compilers do this optimization, and almost every memcpy
529 // implementation handles this case safely. If there is a libc that does not
530 // safely handle this, we can add a target hook.
Daniel Dunbar7482d122008-09-09 20:49:46 +0000531 const llvm::Type *BP = llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
532 if (DestPtr->getType() != BP)
533 DestPtr = Builder.CreateBitCast(DestPtr, BP, "tmp");
534 if (SrcPtr->getType() != BP)
535 SrcPtr = Builder.CreateBitCast(SrcPtr, BP, "tmp");
536
537 // Get size and alignment info for this aggregate.
538 std::pair<uint64_t, unsigned> TypeInfo = getContext().getTypeInfo(Ty);
539
540 // FIXME: Handle variable sized types.
541 const llvm::Type *IntPtr = llvm::IntegerType::get(LLVMPointerWidth);
542
Chris Lattnerca4fc2c2009-02-28 18:18:58 +0000543 Builder.CreateCall4(CGM.getMemCpyFn(),
Daniel Dunbar7482d122008-09-09 20:49:46 +0000544 DestPtr, SrcPtr,
545 // TypeInfo.first describes size in bits.
546 llvm::ConstantInt::get(IntPtr, TypeInfo.first/8),
547 llvm::ConstantInt::get(llvm::Type::Int32Ty,
548 TypeInfo.second/8));
549}