blob: 058b561f95b3874d8e108af100ae33a5a625f6f8 [file] [log] [blame]
Chris Lattner566b6ce2007-08-24 02:22:53 +00001//===--- CGExprAgg.cpp - Emit LLVM Code from Aggregate Expressions --------===//
Chris Lattneraf6f5282007-08-10 20:13:28 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattneraf6f5282007-08-10 20:13:28 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This contains code to emit Aggregate Expr nodes as LLVM code.
11//
12//===----------------------------------------------------------------------===//
13
14#include "CodeGenFunction.h"
Chris Lattner883f6a72007-08-11 00:04:45 +000015#include "CodeGenModule.h"
Daniel Dunbarde7fb842008-08-11 05:00:27 +000016#include "clang/AST/ASTContext.h"
17#include "clang/AST/StmtVisitor.h"
Chris Lattner883f6a72007-08-11 00:04:45 +000018#include "llvm/Constants.h"
19#include "llvm/Function.h"
Devang Patel636c3d02007-10-26 17:44:44 +000020#include "llvm/GlobalVariable.h"
Chris Lattner9c033562007-08-21 04:25:47 +000021#include "llvm/Support/Compiler.h"
Chris Lattnerf81557c2008-04-04 18:42:16 +000022#include "llvm/Intrinsics.h"
Chris Lattneraf6f5282007-08-10 20:13:28 +000023using namespace clang;
24using namespace CodeGen;
Chris Lattner883f6a72007-08-11 00:04:45 +000025
Chris Lattner9c033562007-08-21 04:25:47 +000026//===----------------------------------------------------------------------===//
27// Aggregate Expression Emitter
28//===----------------------------------------------------------------------===//
29
30namespace {
31class VISIBILITY_HIDDEN AggExprEmitter : public StmtVisitor<AggExprEmitter> {
32 CodeGenFunction &CGF;
Daniel Dunbar45d196b2008-11-01 01:53:16 +000033 CGBuilderTy &Builder;
Chris Lattner9c033562007-08-21 04:25:47 +000034 llvm::Value *DestPtr;
35 bool VolatileDest;
36public:
37 AggExprEmitter(CodeGenFunction &cgf, llvm::Value *destPtr, bool volatileDest)
Chris Lattnerbfc0c1a2007-08-26 23:13:56 +000038 : CGF(cgf), Builder(CGF.Builder),
39 DestPtr(destPtr), VolatileDest(volatileDest) {
Chris Lattner9c033562007-08-21 04:25:47 +000040 }
41
Chris Lattneree755f92007-08-21 04:59:27 +000042 //===--------------------------------------------------------------------===//
43 // Utilities
44 //===--------------------------------------------------------------------===//
45
Chris Lattner9c033562007-08-21 04:25:47 +000046 /// EmitAggLoadOfLValue - Given an expression with aggregate type that
47 /// represents a value lvalue, this method emits the address of the lvalue,
48 /// then loads the result into DestPtr.
49 void EmitAggLoadOfLValue(const Expr *E);
50
Lauro Ramos Venancio13e22cf2008-02-19 22:04:22 +000051 void EmitNonConstInit(InitListExpr *E);
Eli Friedman922696f2008-05-19 17:51:16 +000052
Chris Lattneree755f92007-08-21 04:59:27 +000053 //===--------------------------------------------------------------------===//
54 // Visitor Methods
55 //===--------------------------------------------------------------------===//
56
Chris Lattner9c033562007-08-21 04:25:47 +000057 void VisitStmt(Stmt *S) {
Daniel Dunbar488e9932008-08-16 00:56:44 +000058 CGF.ErrorUnsupported(S, "aggregate expression");
Chris Lattner9c033562007-08-21 04:25:47 +000059 }
60 void VisitParenExpr(ParenExpr *PE) { Visit(PE->getSubExpr()); }
61
62 // l-values.
Seo Sanghyeon9b73b392007-12-14 02:04:12 +000063 void VisitDeclRefExpr(DeclRefExpr *DRE) { EmitAggLoadOfLValue(DRE); }
64 void VisitMemberExpr(MemberExpr *ME) { EmitAggLoadOfLValue(ME); }
65 void VisitUnaryDeref(UnaryOperator *E) { EmitAggLoadOfLValue(E); }
Seo Sanghyeonad6ebd62007-12-23 03:11:58 +000066 void VisitStringLiteral(StringLiteral *E) { EmitAggLoadOfLValue(E); }
Eli Friedmanb1851242008-05-27 15:51:49 +000067 void VisitCompoundLiteralExpr(CompoundLiteralExpr *E)
68 { EmitAggLoadOfLValue(E); }
Seo Sanghyeon9b73b392007-12-14 02:04:12 +000069
70 void VisitArraySubscriptExpr(ArraySubscriptExpr *E) {
71 EmitAggLoadOfLValue(E);
72 }
Chris Lattner9c033562007-08-21 04:25:47 +000073
74 // Operators.
75 // case Expr::UnaryOperatorClass:
Chris Lattner9c033562007-08-21 04:25:47 +000076 // case Expr::CastExprClass:
Nuno Lopes7e916272009-01-15 20:14:33 +000077 void VisitCStyleCastExpr(CStyleCastExpr *E);
Anders Carlssone4707ff2008-01-14 06:28:57 +000078 void VisitImplicitCastExpr(ImplicitCastExpr *E);
Anders Carlsson148fe672007-10-31 22:04:46 +000079 void VisitCallExpr(const CallExpr *E);
Chris Lattnerb2d963f2007-08-31 22:54:14 +000080 void VisitStmtExpr(const StmtExpr *E);
Chris Lattner9c033562007-08-21 04:25:47 +000081 void VisitBinaryOperator(const BinaryOperator *BO);
Chris Lattner03d6fb92007-08-21 04:43:17 +000082 void VisitBinAssign(const BinaryOperator *E);
Nate Begeman796ef3d2008-01-31 05:38:29 +000083 void VisitOverloadExpr(const OverloadExpr *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::VisitOverloadExpr(const OverloadExpr *E) {
Nate Begeman796ef3d2008-01-31 05:38:29 +0000200 RValue RV = CGF.EmitCallExpr(E->getFn(), E->arg_begin(),
Ted Kremenek55499762008-06-17 02:43:46 +0000201 E->arg_end(CGF.getContext()));
202
Nate Begeman796ef3d2008-01-31 05:38:29 +0000203 assert(RV.isAggregate() && "Return value must be aggregate value!");
204
205 // If the result is ignored, don't copy from the value.
206 if (DestPtr == 0)
207 // FIXME: If the source is volatile, we must read from it.
208 return;
209
Daniel Dunbar7482d122008-09-09 20:49:46 +0000210 CGF.EmitAggregateCopy(DestPtr, RV.getAggregateAddr(), E->getType());
Nate Begeman796ef3d2008-01-31 05:38:29 +0000211}
212
Chris Lattner96196622008-07-26 22:37:01 +0000213void AggExprEmitter::VisitBinComma(const BinaryOperator *E) {
Eli Friedman07fa52a2008-05-20 07:56:31 +0000214 CGF.EmitAnyExpr(E->getLHS());
215 CGF.EmitAggExpr(E->getRHS(), DestPtr, false);
216}
217
Chris Lattnerb2d963f2007-08-31 22:54:14 +0000218void AggExprEmitter::VisitStmtExpr(const StmtExpr *E) {
219 CGF.EmitCompoundStmt(*E->getSubStmt(), true, DestPtr, VolatileDest);
220}
221
Chris Lattner9c033562007-08-21 04:25:47 +0000222void AggExprEmitter::VisitBinaryOperator(const BinaryOperator *E) {
Daniel Dunbar488e9932008-08-16 00:56:44 +0000223 CGF.ErrorUnsupported(E, "aggregate binary expression");
Chris Lattneree755f92007-08-21 04:59:27 +0000224}
225
Chris Lattner03d6fb92007-08-21 04:43:17 +0000226void AggExprEmitter::VisitBinAssign(const BinaryOperator *E) {
Eli Friedmanff6e2b72008-02-11 01:09:17 +0000227 // For an assignment to work, the value on the right has
228 // to be compatible with the value on the left.
229 assert(CGF.getContext().typesAreCompatible(
230 E->getLHS()->getType().getUnqualifiedType(),
231 E->getRHS()->getType().getUnqualifiedType())
232 && "Invalid assignment");
Chris Lattner9c033562007-08-21 04:25:47 +0000233 LValue LHS = CGF.EmitLValue(E->getLHS());
Chris Lattner883f6a72007-08-11 00:04:45 +0000234
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000235 // We have to special case property setters, otherwise we must have
236 // a simple lvalue (no aggregates inside vectors, bitfields).
237 if (LHS.isPropertyRef()) {
238 // FIXME: Volatility?
239 llvm::Value *AggLoc = DestPtr;
240 if (!AggLoc)
241 AggLoc = CGF.CreateTempAlloca(CGF.ConvertType(E->getRHS()->getType()));
242 CGF.EmitAggExpr(E->getRHS(), AggLoc, false);
243 CGF.EmitObjCPropertySet(LHS.getPropertyRefExpr(),
244 RValue::getAggregate(AggLoc));
Fariborz Jahanian43f44702008-11-22 22:30:21 +0000245 }
246 else if (LHS.isKVCRef()) {
247 // FIXME: Volatility?
248 llvm::Value *AggLoc = DestPtr;
249 if (!AggLoc)
250 AggLoc = CGF.CreateTempAlloca(CGF.ConvertType(E->getRHS()->getType()));
251 CGF.EmitAggExpr(E->getRHS(), AggLoc, false);
252 CGF.EmitObjCPropertySet(LHS.getKVCRefExpr(),
253 RValue::getAggregate(AggLoc));
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000254 } else {
255 // Codegen the RHS so that it stores directly into the LHS.
256 CGF.EmitAggExpr(E->getRHS(), LHS.getAddress(), false /*FIXME: VOLATILE LHS*/);
257
258 if (DestPtr == 0)
259 return;
260
261 // If the result of the assignment is used, copy the RHS there also.
Daniel Dunbar7482d122008-09-09 20:49:46 +0000262 CGF.EmitAggregateCopy(DestPtr, LHS.getAddress(), E->getType());
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000263 }
Chris Lattner883f6a72007-08-11 00:04:45 +0000264}
265
Chris Lattner9c033562007-08-21 04:25:47 +0000266void AggExprEmitter::VisitConditionalOperator(const ConditionalOperator *E) {
Daniel Dunbar9615ecb2008-11-13 01:38:36 +0000267 llvm::BasicBlock *LHSBlock = CGF.createBasicBlock("cond.true");
268 llvm::BasicBlock *RHSBlock = CGF.createBasicBlock("cond.false");
269 llvm::BasicBlock *ContBlock = CGF.createBasicBlock("cond.end");
Chris Lattner883f6a72007-08-11 00:04:45 +0000270
Chris Lattner9c033562007-08-21 04:25:47 +0000271 llvm::Value *Cond = CGF.EvaluateExprAsBool(E->getCond());
Chris Lattnerbfc0c1a2007-08-26 23:13:56 +0000272 Builder.CreateCondBr(Cond, LHSBlock, RHSBlock);
Chris Lattner883f6a72007-08-11 00:04:45 +0000273
Chris Lattner9c033562007-08-21 04:25:47 +0000274 CGF.EmitBlock(LHSBlock);
Chris Lattner883f6a72007-08-11 00:04:45 +0000275
276 // Handle the GNU extension for missing LHS.
277 assert(E->getLHS() && "Must have LHS for aggregate value");
278
Chris Lattnerc748f272007-08-21 05:02:10 +0000279 Visit(E->getLHS());
Daniel Dunbard57a8712008-11-11 09:41:28 +0000280 CGF.EmitBranch(ContBlock);
Chris Lattner883f6a72007-08-11 00:04:45 +0000281
Chris Lattner9c033562007-08-21 04:25:47 +0000282 CGF.EmitBlock(RHSBlock);
Chris Lattner883f6a72007-08-11 00:04:45 +0000283
Chris Lattnerc748f272007-08-21 05:02:10 +0000284 Visit(E->getRHS());
Daniel Dunbard57a8712008-11-11 09:41:28 +0000285 CGF.EmitBranch(ContBlock);
Chris Lattner883f6a72007-08-11 00:04:45 +0000286
Chris Lattner9c033562007-08-21 04:25:47 +0000287 CGF.EmitBlock(ContBlock);
Chris Lattner883f6a72007-08-11 00:04:45 +0000288}
Chris Lattneree755f92007-08-21 04:59:27 +0000289
Eli Friedmanb1851242008-05-27 15:51:49 +0000290void AggExprEmitter::VisitVAArgExpr(VAArgExpr *VE) {
291 llvm::Value *ArgValue = CGF.EmitLValue(VE->getSubExpr()).getAddress();
Anders Carlssonddf7cac2008-11-04 05:30:00 +0000292 llvm::Value *ArgPtr = CGF.EmitVAArg(ArgValue, VE->getType());
293
Sebastian Redl0262f022009-01-09 21:09:38 +0000294 if (!ArgPtr) {
Anders Carlssonddf7cac2008-11-04 05:30:00 +0000295 CGF.ErrorUnsupported(VE, "aggregate va_arg expression");
Sebastian Redl0262f022009-01-09 21:09:38 +0000296 return;
297 }
298
Eli Friedmanb1851242008-05-27 15:51:49 +0000299 if (DestPtr)
Eli Friedman1e692ac2008-06-13 23:01:12 +0000300 // FIXME: volatility
Anders Carlssonddf7cac2008-11-04 05:30:00 +0000301 CGF.EmitAggregateCopy(DestPtr, ArgPtr, VE->getType());
Eli Friedmanb1851242008-05-27 15:51:49 +0000302}
303
Lauro Ramos Venancio13e22cf2008-02-19 22:04:22 +0000304void AggExprEmitter::EmitNonConstInit(InitListExpr *E) {
Chris Lattnerbe20bb52008-10-26 23:53:12 +0000305 if (E->hadDesignators()) {
306 CGF.ErrorUnsupported(E, "initializer list with designators");
307 return;
308 }
309
Lauro Ramos Venancio13e22cf2008-02-19 22:04:22 +0000310 const llvm::PointerType *APType =
311 cast<llvm::PointerType>(DestPtr->getType());
312 const llvm::Type *DestType = APType->getElementType();
Lauro Ramos Venancio305762c2008-02-18 22:44:02 +0000313
314 if (const llvm::ArrayType *AType = dyn_cast<llvm::ArrayType>(DestType)) {
Lauro Ramos Venancio13e22cf2008-02-19 22:04:22 +0000315 unsigned NumInitElements = E->getNumInits();
Lauro Ramos Venancio305762c2008-02-18 22:44:02 +0000316
Lauro Ramos Venancio305762c2008-02-18 22:44:02 +0000317 unsigned i;
318 for (i = 0; i != NumInitElements; ++i) {
Chris Lattner36b6a0a2008-03-19 05:19:41 +0000319 llvm::Value *NextVal = Builder.CreateStructGEP(DestPtr, i, ".array");
Lauro Ramos Venancio13e22cf2008-02-19 22:04:22 +0000320 Expr *Init = E->getInit(i);
321 if (isa<InitListExpr>(Init))
322 CGF.EmitAggExpr(Init, NextVal, VolatileDest);
323 else
Eli Friedman1e692ac2008-06-13 23:01:12 +0000324 // FIXME: volatility
Lauro Ramos Venancio13e22cf2008-02-19 22:04:22 +0000325 Builder.CreateStore(CGF.EmitScalarExpr(Init), NextVal);
Lauro Ramos Venancio305762c2008-02-18 22:44:02 +0000326 }
327
328 // Emit remaining default initializers
329 unsigned NumArrayElements = AType->getNumElements();
Lauro Ramos Venancio13e22cf2008-02-19 22:04:22 +0000330 QualType QType = E->getInit(0)->getType();
331 const llvm::Type *EType = AType->getElementType();
Lauro Ramos Venancio305762c2008-02-18 22:44:02 +0000332 for (/*Do not initialize i*/; i < NumArrayElements; ++i) {
Chris Lattner36b6a0a2008-03-19 05:19:41 +0000333 llvm::Value *NextVal = Builder.CreateStructGEP(DestPtr, i, ".array");
Dan Gohmand79a7262008-05-22 22:12:56 +0000334 if (EType->isSingleValueType())
Eli Friedman1e692ac2008-06-13 23:01:12 +0000335 // FIXME: volatility
Lauro Ramos Venancio13e22cf2008-02-19 22:04:22 +0000336 Builder.CreateStore(llvm::Constant::getNullValue(EType), NextVal);
337 else
Daniel Dunbar7482d122008-09-09 20:49:46 +0000338 CGF.EmitAggregateClear(NextVal, QType);
Lauro Ramos Venancio305762c2008-02-18 22:44:02 +0000339 }
Lauro Ramos Venancio13e22cf2008-02-19 22:04:22 +0000340 } else
341 assert(false && "Invalid initializer");
Lauro Ramos Venancio305762c2008-02-18 22:44:02 +0000342}
343
Chris Lattnerf81557c2008-04-04 18:42:16 +0000344void AggExprEmitter::EmitInitializationToLValue(Expr* E, LValue LV) {
345 // FIXME: Are initializers affected by volatile?
346 if (E->getType()->isComplexType()) {
347 CGF.EmitComplexExprIntoAddr(E, LV.getAddress(), false);
Eli Friedmanc8ba9612008-05-12 15:06:05 +0000348 } else if (CGF.hasAggregateLLVMType(E->getType())) {
349 CGF.EmitAnyExpr(E, LV.getAddress(), false);
350 } else {
351 CGF.EmitStoreThroughLValue(CGF.EmitAnyExpr(E), LV, E->getType());
Chris Lattnerf81557c2008-04-04 18:42:16 +0000352 }
Chris Lattnerf81557c2008-04-04 18:42:16 +0000353}
354
355void AggExprEmitter::EmitNullInitializationToLValue(LValue LV, QualType T) {
356 if (!CGF.hasAggregateLLVMType(T)) {
357 // For non-aggregates, we can store zero
Daniel Dunbar82397132008-08-06 05:32:55 +0000358 llvm::Value *Null = llvm::Constant::getNullValue(CGF.ConvertType(T));
359 CGF.EmitStoreThroughLValue(RValue::get(Null), LV, T);
Lauro Ramos Venancio145cd892008-02-19 19:27:31 +0000360 } else {
Chris Lattnerf81557c2008-04-04 18:42:16 +0000361 // Otherwise, just memset the whole thing to zero. This is legal
362 // because in LLVM, all default initializers are guaranteed to have a
363 // bit pattern of all zeros.
364 // There's a potential optimization opportunity in combining
365 // memsets; that would be easy for arrays, but relatively
366 // difficult for structures with the current code.
Chris Lattner4e8a9e82008-11-21 16:43:15 +0000367 const llvm::Type *SizeTy = llvm::Type::Int64Ty;
368 llvm::Value *MemSet = CGF.CGM.getIntrinsic(llvm::Intrinsic::memset,
369 &SizeTy, 1);
Chris Lattnerf81557c2008-04-04 18:42:16 +0000370 uint64_t Size = CGF.getContext().getTypeSize(T);
371
372 const llvm::Type *BP = llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
373 llvm::Value* DestPtr = Builder.CreateBitCast(LV.getAddress(), BP, "tmp");
Chris Lattner3eae03e2008-05-06 00:56:42 +0000374 Builder.CreateCall4(MemSet, DestPtr,
375 llvm::ConstantInt::get(llvm::Type::Int8Ty, 0),
Chris Lattner4e8a9e82008-11-21 16:43:15 +0000376 llvm::ConstantInt::get(SizeTy, Size/8),
Chris Lattner3eae03e2008-05-06 00:56:42 +0000377 llvm::ConstantInt::get(llvm::Type::Int32Ty, 0));
Chris Lattnerf81557c2008-04-04 18:42:16 +0000378 }
379}
380
Chris Lattnerf81557c2008-04-04 18:42:16 +0000381void AggExprEmitter::VisitInitListExpr(InitListExpr *E) {
Chris Lattnerbe20bb52008-10-26 23:53:12 +0000382 if (E->hadDesignators()) {
383 CGF.ErrorUnsupported(E, "initializer list with designators");
384 return;
385 }
Eli Friedman994ffef2008-11-30 02:11:09 +0000386
Eli Friedmana385b3c2008-12-02 01:17:45 +0000387#if 0
388 // FIXME: Disabled while we figure out what to do about
389 // test/CodeGen/bitfield.c
390 //
Eli Friedman994ffef2008-11-30 02:11:09 +0000391 // If we can, prefer a copy from a global; this is a lot less
392 // code for long globals, and it's easier for the current optimizers
393 // to analyze.
394 // FIXME: Should we really be doing this? Should we try to avoid
395 // cases where we emit a global with a lot of zeros? Should
396 // we try to avoid short globals?
Eli Friedmanc9e8f602009-01-25 02:32:41 +0000397 if (E->isConstantInitializer(CGF.getContext(), 0)) {
Eli Friedman994ffef2008-11-30 02:11:09 +0000398 llvm::Constant* C = CGF.CGM.EmitConstantExpr(E, &CGF);
399 llvm::GlobalVariable* GV =
400 new llvm::GlobalVariable(C->getType(), true,
401 llvm::GlobalValue::InternalLinkage,
402 C, "", &CGF.CGM.getModule(), 0);
403 CGF.EmitAggregateCopy(DestPtr, GV, E->getType());
404 return;
405 }
Eli Friedmana385b3c2008-12-02 01:17:45 +0000406#endif
Chris Lattnerf81557c2008-04-04 18:42:16 +0000407 // Handle initialization of an array.
408 if (E->getType()->isArrayType()) {
409 const llvm::PointerType *APType =
410 cast<llvm::PointerType>(DestPtr->getType());
411 const llvm::ArrayType *AType =
412 cast<llvm::ArrayType>(APType->getElementType());
413
414 uint64_t NumInitElements = E->getNumInits();
Eli Friedman922696f2008-05-19 17:51:16 +0000415
Chris Lattner96196622008-07-26 22:37:01 +0000416 if (E->getNumInits() > 0) {
417 QualType T1 = E->getType();
418 QualType T2 = E->getInit(0)->getType();
419 if (CGF.getContext().getCanonicalType(T1).getUnqualifiedType() ==
420 CGF.getContext().getCanonicalType(T2).getUnqualifiedType()) {
421 EmitAggLoadOfLValue(E->getInit(0));
422 return;
423 }
Eli Friedman922696f2008-05-19 17:51:16 +0000424 }
425
Chris Lattnerf81557c2008-04-04 18:42:16 +0000426 uint64_t NumArrayElements = AType->getNumElements();
Chris Lattnerc63a1f22008-08-04 07:31:14 +0000427 QualType ElementType = CGF.getContext().getCanonicalType(E->getType());
428 ElementType =CGF.getContext().getAsArrayType(ElementType)->getElementType();
Chris Lattnerf81557c2008-04-04 18:42:16 +0000429
Chris Lattnerc63a1f22008-08-04 07:31:14 +0000430 unsigned CVRqualifier = ElementType.getCVRQualifiers();
Eli Friedman1e692ac2008-06-13 23:01:12 +0000431
Chris Lattnerf81557c2008-04-04 18:42:16 +0000432 for (uint64_t i = 0; i != NumArrayElements; ++i) {
433 llvm::Value *NextVal = Builder.CreateStructGEP(DestPtr, i, ".array");
434 if (i < NumInitElements)
Eli Friedman1e692ac2008-06-13 23:01:12 +0000435 EmitInitializationToLValue(E->getInit(i),
436 LValue::MakeAddr(NextVal, CVRqualifier));
Chris Lattnerf81557c2008-04-04 18:42:16 +0000437 else
Eli Friedman1e692ac2008-06-13 23:01:12 +0000438 EmitNullInitializationToLValue(LValue::MakeAddr(NextVal, CVRqualifier),
Chris Lattnerf81557c2008-04-04 18:42:16 +0000439 ElementType);
Lauro Ramos Venancio145cd892008-02-19 19:27:31 +0000440 }
Chris Lattnerf81557c2008-04-04 18:42:16 +0000441 return;
442 }
443
444 assert(E->getType()->isRecordType() && "Only support structs/unions here!");
445
446 // Do struct initialization; this code just sets each individual member
447 // to the approprate value. This makes bitfield support automatic;
448 // the disadvantage is that the generated code is more difficult for
449 // the optimizer, especially with bitfields.
450 unsigned NumInitElements = E->getNumInits();
451 RecordDecl *SD = E->getType()->getAsRecordType()->getDecl();
Chris Lattnerf81557c2008-04-04 18:42:16 +0000452 unsigned CurInitVal = 0;
453 bool isUnion = E->getType()->isUnionType();
454
455 // Here we iterate over the fields; this makes it simpler to both
456 // default-initialize fields and skip over unnamed fields.
Douglas Gregor44b43212008-12-11 16:49:14 +0000457 for (RecordDecl::field_iterator Field = SD->field_begin(),
458 FieldEnd = SD->field_end();
459 Field != FieldEnd; ++Field) {
460 // We're done once we hit the flexible array member
461 if (Field->getType()->isIncompleteArrayType())
462 break;
463
464 if (Field->getIdentifier() == 0) {
Chris Lattnerf81557c2008-04-04 18:42:16 +0000465 // Initializers can't initialize unnamed fields, e.g. "int : 20;"
466 continue;
467 }
Eli Friedman1e692ac2008-06-13 23:01:12 +0000468 // FIXME: volatility
Douglas Gregor44b43212008-12-11 16:49:14 +0000469 LValue FieldLoc = CGF.EmitLValueForField(DestPtr, *Field, isUnion,0);
Chris Lattnerf81557c2008-04-04 18:42:16 +0000470 if (CurInitVal < NumInitElements) {
471 // Store the initializer into the field
472 // This will probably have to get a bit smarter when we support
473 // designators in initializers
474 EmitInitializationToLValue(E->getInit(CurInitVal++), FieldLoc);
475 } else {
476 // We're out of initalizers; default-initialize to null
Douglas Gregor44b43212008-12-11 16:49:14 +0000477 EmitNullInitializationToLValue(FieldLoc, Field->getType());
Chris Lattnerf81557c2008-04-04 18:42:16 +0000478 }
479
480 // Unions only initialize one field.
481 // (things can get weird with designators, but they aren't
482 // supported yet.)
Nuno Lopes7e916272009-01-15 20:14:33 +0000483 if (isUnion)
Chris Lattnerf81557c2008-04-04 18:42:16 +0000484 break;
Lauro Ramos Venancio145cd892008-02-19 19:27:31 +0000485 }
Devang Patel636c3d02007-10-26 17:44:44 +0000486}
487
Chris Lattneree755f92007-08-21 04:59:27 +0000488//===----------------------------------------------------------------------===//
489// Entry Points into this File
490//===----------------------------------------------------------------------===//
491
492/// EmitAggExpr - Emit the computation of the specified expression of
493/// aggregate type. The result is computed into DestPtr. Note that if
494/// DestPtr is null, the value of the aggregate expression is not needed.
495void CodeGenFunction::EmitAggExpr(const Expr *E, llvm::Value *DestPtr,
496 bool VolatileDest) {
497 assert(E && hasAggregateLLVMType(E->getType()) &&
498 "Invalid aggregate expression to emit");
499
500 AggExprEmitter(*this, DestPtr, VolatileDest).Visit(const_cast<Expr*>(E));
501}
Daniel Dunbar7482d122008-09-09 20:49:46 +0000502
503void CodeGenFunction::EmitAggregateClear(llvm::Value *DestPtr, QualType Ty) {
504 assert(!Ty->isAnyComplexType() && "Shouldn't happen for complex");
505
506 EmitMemSetToZero(DestPtr, Ty);
507}
508
509void CodeGenFunction::EmitAggregateCopy(llvm::Value *DestPtr,
510 llvm::Value *SrcPtr, QualType Ty) {
511 assert(!Ty->isAnyComplexType() && "Shouldn't happen for complex");
512
513 // Aggregate assignment turns into llvm.memmove.
514 const llvm::Type *BP = llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
515 if (DestPtr->getType() != BP)
516 DestPtr = Builder.CreateBitCast(DestPtr, BP, "tmp");
517 if (SrcPtr->getType() != BP)
518 SrcPtr = Builder.CreateBitCast(SrcPtr, BP, "tmp");
519
520 // Get size and alignment info for this aggregate.
521 std::pair<uint64_t, unsigned> TypeInfo = getContext().getTypeInfo(Ty);
522
523 // FIXME: Handle variable sized types.
524 const llvm::Type *IntPtr = llvm::IntegerType::get(LLVMPointerWidth);
525
526 Builder.CreateCall4(CGM.getMemMoveFn(),
527 DestPtr, SrcPtr,
528 // TypeInfo.first describes size in bits.
529 llvm::ConstantInt::get(IntPtr, TypeInfo.first/8),
530 llvm::ConstantInt::get(llvm::Type::Int32Ty,
531 TypeInfo.second/8));
532}