blob: 13559acc0df3794d603d5fc1e1ac17a99683a1d0 [file] [log] [blame]
Chris Lattner11e0de52007-08-24 02:22:53 +00001//===--- CGExprAgg.cpp - Emit LLVM Code from Aggregate Expressions --------===//
Chris Lattnerd79671f2007-08-10 20:13:28 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner5b12ab82007-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 Lattnerd79671f2007-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 Lattner6278e6a2007-08-11 00:04:45 +000015#include "CodeGenModule.h"
Daniel Dunbarad319a72008-08-11 05:00:27 +000016#include "clang/AST/ASTContext.h"
Anders Carlssonb7f8f592009-04-17 00:06:03 +000017#include "clang/AST/DeclCXX.h"
Daniel Dunbarad319a72008-08-11 05:00:27 +000018#include "clang/AST/StmtVisitor.h"
Chris Lattner6278e6a2007-08-11 00:04:45 +000019#include "llvm/Constants.h"
20#include "llvm/Function.h"
Devang Patel87174172007-10-26 17:44:44 +000021#include "llvm/GlobalVariable.h"
Chris Lattner4758b402007-08-21 04:25:47 +000022#include "llvm/Support/Compiler.h"
Chris Lattner579a05d2008-04-04 18:42:16 +000023#include "llvm/Intrinsics.h"
Chris Lattnerd79671f2007-08-10 20:13:28 +000024using namespace clang;
25using namespace CodeGen;
Chris Lattner6278e6a2007-08-11 00:04:45 +000026
Chris Lattner4758b402007-08-21 04:25:47 +000027//===----------------------------------------------------------------------===//
28// Aggregate Expression Emitter
29//===----------------------------------------------------------------------===//
30
31namespace {
32class VISIBILITY_HIDDEN AggExprEmitter : public StmtVisitor<AggExprEmitter> {
33 CodeGenFunction &CGF;
Daniel Dunbarcb463852008-11-01 01:53:16 +000034 CGBuilderTy &Builder;
Chris Lattner4758b402007-08-21 04:25:47 +000035 llvm::Value *DestPtr;
36 bool VolatileDest;
37public:
38 AggExprEmitter(CodeGenFunction &cgf, llvm::Value *destPtr, bool volatileDest)
Chris Lattnerbda69f82007-08-26 23:13:56 +000039 : CGF(cgf), Builder(CGF.Builder),
40 DestPtr(destPtr), VolatileDest(volatileDest) {
Chris Lattner4758b402007-08-21 04:25:47 +000041 }
42
Chris Lattner835635d2007-08-21 04:59:27 +000043 //===--------------------------------------------------------------------===//
44 // Utilities
45 //===--------------------------------------------------------------------===//
46
Chris Lattner4758b402007-08-21 04:25:47 +000047 /// EmitAggLoadOfLValue - Given an expression with aggregate type that
48 /// represents a value lvalue, this method emits the address of the lvalue,
49 /// then loads the result into DestPtr.
50 void EmitAggLoadOfLValue(const Expr *E);
Eli Friedmanf23b6fa2008-05-19 17:51:16 +000051
Chris Lattner835635d2007-08-21 04:59:27 +000052 //===--------------------------------------------------------------------===//
53 // Visitor Methods
54 //===--------------------------------------------------------------------===//
55
Chris Lattner4758b402007-08-21 04:25:47 +000056 void VisitStmt(Stmt *S) {
Daniel Dunbara7c8cf62008-08-16 00:56:44 +000057 CGF.ErrorUnsupported(S, "aggregate expression");
Chris Lattner4758b402007-08-21 04:25:47 +000058 }
59 void VisitParenExpr(ParenExpr *PE) { Visit(PE->getSubExpr()); }
Eli Friedman3f66b842009-01-27 09:03:41 +000060 void VisitUnaryExtension(UnaryOperator *E) { Visit(E->getSubExpr()); }
Chris Lattner4758b402007-08-21 04:25:47 +000061
62 // l-values.
Seo Sanghyeond4d8c3c2007-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 Sanghyeon6f1b2742007-12-23 03:11:58 +000066 void VisitStringLiteral(StringLiteral *E) { EmitAggLoadOfLValue(E); }
Chris Lattner2f343dd2009-04-21 23:00:09 +000067 void VisitCompoundLiteralExpr(CompoundLiteralExpr *E) {
68 EmitAggLoadOfLValue(E);
69 }
Seo Sanghyeond4d8c3c2007-12-14 02:04:12 +000070 void VisitArraySubscriptExpr(ArraySubscriptExpr *E) {
71 EmitAggLoadOfLValue(E);
72 }
Chris Lattner2f343dd2009-04-21 23:00:09 +000073 void VisitBlockDeclRefExpr(const BlockDeclRefExpr *E) {
74 EmitAggLoadOfLValue(E);
75 }
76 void VisitPredefinedExpr(const PredefinedExpr *E) {
77 EmitAggLoadOfLValue(E);
78 }
Douglas Gregor347f7ea2009-01-28 21:54:33 +000079
Chris Lattner4758b402007-08-21 04:25:47 +000080 // Operators.
Nuno Lopes7ffcf932009-01-15 20:14:33 +000081 void VisitCStyleCastExpr(CStyleCastExpr *E);
Anders Carlsson1ba25ca2008-01-14 06:28:57 +000082 void VisitImplicitCastExpr(ImplicitCastExpr *E);
Anders Carlsson0370eb22007-10-31 22:04:46 +000083 void VisitCallExpr(const CallExpr *E);
Chris Lattner49e3bfa2007-08-31 22:54:14 +000084 void VisitStmtExpr(const StmtExpr *E);
Chris Lattner4758b402007-08-21 04:25:47 +000085 void VisitBinaryOperator(const BinaryOperator *BO);
Chris Lattnercd9fb242007-08-21 04:43:17 +000086 void VisitBinAssign(const BinaryOperator *E);
Eli Friedman4b0e2a32008-05-20 07:56:31 +000087 void VisitBinComma(const BinaryOperator *E);
Chris Lattner4758b402007-08-21 04:25:47 +000088
Chris Lattnerb1d329d2008-06-24 17:04:18 +000089 void VisitObjCMessageExpr(ObjCMessageExpr *E);
Daniel Dunbarc8317a42008-08-23 10:51:21 +000090 void VisitObjCIvarRefExpr(ObjCIvarRefExpr *E) {
91 EmitAggLoadOfLValue(E);
92 }
Daniel Dunbar55310df2008-08-27 06:57:25 +000093 void VisitObjCPropertyRefExpr(ObjCPropertyRefExpr *E);
Fariborz Jahanian8a1810f2008-11-22 18:39:36 +000094 void VisitObjCKVCRefExpr(ObjCKVCRefExpr *E);
Chris Lattner4758b402007-08-21 04:25:47 +000095
96 void VisitConditionalOperator(const ConditionalOperator *CO);
Devang Patel87174172007-10-26 17:44:44 +000097 void VisitInitListExpr(InitListExpr *E);
Chris Lattneraa9c7ae2008-04-08 04:40:51 +000098 void VisitCXXDefaultArgExpr(CXXDefaultArgExpr *DAE) {
99 Visit(DAE->getExpr());
100 }
Anders Carlsson1619a5042009-05-03 17:47:16 +0000101 void VisitCXXConstructExpr(const CXXConstructExpr *E);
Anders Carlssonc82b86d2009-05-19 04:48:36 +0000102 void VisitCXXExprWithTemporaries(CXXExprWithTemporaries *E);
103
Eli Friedman21911e82008-05-27 15:51:49 +0000104 void VisitVAArgExpr(VAArgExpr *E);
Chris Lattner579a05d2008-04-04 18:42:16 +0000105
106 void EmitInitializationToLValue(Expr *E, LValue Address);
107 void EmitNullInitializationToLValue(LValue Address, QualType T);
Chris Lattner4758b402007-08-21 04:25:47 +0000108 // case Expr::ChooseExprClass:
Lauro Ramos Venanciodec89732008-02-18 22:44:02 +0000109
Chris Lattner4758b402007-08-21 04:25:47 +0000110};
111} // end anonymous namespace.
112
Chris Lattner835635d2007-08-21 04:59:27 +0000113//===----------------------------------------------------------------------===//
114// Utilities
115//===----------------------------------------------------------------------===//
Chris Lattner4758b402007-08-21 04:25:47 +0000116
Chris Lattner6278e6a2007-08-11 00:04:45 +0000117/// EmitAggLoadOfLValue - Given an expression with aggregate type that
118/// represents a value lvalue, this method emits the address of the lvalue,
119/// then loads the result into DestPtr.
Chris Lattner4758b402007-08-21 04:25:47 +0000120void AggExprEmitter::EmitAggLoadOfLValue(const Expr *E) {
121 LValue LV = CGF.EmitLValue(E);
Chris Lattner6278e6a2007-08-11 00:04:45 +0000122 assert(LV.isSimple() && "Can't have aggregate bitfield, vector, etc");
123 llvm::Value *SrcPtr = LV.getAddress();
124
125 // If the result is ignored, don't copy from the value.
126 if (DestPtr == 0)
127 // FIXME: If the source is volatile, we must read from it.
128 return;
129
Daniel Dunbar0bc8e862008-09-09 20:49:46 +0000130 CGF.EmitAggregateCopy(DestPtr, SrcPtr, E->getType());
Chris Lattner6278e6a2007-08-11 00:04:45 +0000131}
132
Chris Lattner835635d2007-08-21 04:59:27 +0000133//===----------------------------------------------------------------------===//
134// Visitor Methods
135//===----------------------------------------------------------------------===//
136
Nuno Lopes7ffcf932009-01-15 20:14:33 +0000137void AggExprEmitter::VisitCStyleCastExpr(CStyleCastExpr *E) {
138 // GCC union extension
139 if (E->getType()->isUnionType()) {
140 RecordDecl *SD = E->getType()->getAsRecordType()->getDecl();
Douglas Gregorbcced4e2009-04-09 21:40:53 +0000141 LValue FieldLoc = CGF.EmitLValueForField(DestPtr,
142 *SD->field_begin(CGF.getContext()),
143 true, 0);
Nuno Lopes7ffcf932009-01-15 20:14:33 +0000144 EmitInitializationToLValue(E->getSubExpr(), FieldLoc);
145 return;
146 }
147
148 Visit(E->getSubExpr());
149}
150
Chris Lattner0f398c42008-07-26 22:37:01 +0000151void AggExprEmitter::VisitImplicitCastExpr(ImplicitCastExpr *E) {
Eli Friedmanf54c4e52008-02-11 01:09:17 +0000152 assert(CGF.getContext().typesAreCompatible(
Chris Lattner0f398c42008-07-26 22:37:01 +0000153 E->getSubExpr()->getType().getUnqualifiedType(),
154 E->getType().getUnqualifiedType()) &&
155 "Implicit cast types must be compatible");
Anders Carlsson1ba25ca2008-01-14 06:28:57 +0000156 Visit(E->getSubExpr());
157}
158
Chris Lattner0f398c42008-07-26 22:37:01 +0000159void AggExprEmitter::VisitCallExpr(const CallExpr *E) {
Anders Carlsson0370eb22007-10-31 22:04:46 +0000160 RValue RV = CGF.EmitCallExpr(E);
161 assert(RV.isAggregate() && "Return value must be aggregate value!");
162
163 // If the result is ignored, don't copy from the value.
164 if (DestPtr == 0)
165 // FIXME: If the source is volatile, we must read from it.
166 return;
167
Daniel Dunbar0bc8e862008-09-09 20:49:46 +0000168 CGF.EmitAggregateCopy(DestPtr, RV.getAggregateAddr(), E->getType());
Anders Carlsson0370eb22007-10-31 22:04:46 +0000169}
Chris Lattner0f398c42008-07-26 22:37:01 +0000170
171void AggExprEmitter::VisitObjCMessageExpr(ObjCMessageExpr *E) {
Daniel Dunbar97db84c2008-08-23 03:46:30 +0000172 RValue RV = CGF.EmitObjCMessageExpr(E);
173 assert(RV.isAggregate() && "Return value must be aggregate value!");
Chris Lattnerb1d329d2008-06-24 17:04:18 +0000174
175 // If the result is ignored, don't copy from the value.
176 if (DestPtr == 0)
Daniel Dunbar97db84c2008-08-23 03:46:30 +0000177 // FIXME: If the source is volatile, we must read from it.
Chris Lattnerb1d329d2008-06-24 17:04:18 +0000178 return;
179
Daniel Dunbar0bc8e862008-09-09 20:49:46 +0000180 CGF.EmitAggregateCopy(DestPtr, RV.getAggregateAddr(), E->getType());
Chris Lattnerb1d329d2008-06-24 17:04:18 +0000181}
Anders Carlsson0370eb22007-10-31 22:04:46 +0000182
Daniel Dunbar55310df2008-08-27 06:57:25 +0000183void AggExprEmitter::VisitObjCPropertyRefExpr(ObjCPropertyRefExpr *E) {
184 RValue RV = CGF.EmitObjCPropertyGet(E);
185 assert(RV.isAggregate() && "Return value must be aggregate value!");
186
187 // If the result is ignored, don't copy from the value.
188 if (DestPtr == 0)
189 // FIXME: If the source is volatile, we must read from it.
190 return;
191
Daniel Dunbar0bc8e862008-09-09 20:49:46 +0000192 CGF.EmitAggregateCopy(DestPtr, RV.getAggregateAddr(), E->getType());
Daniel Dunbar55310df2008-08-27 06:57:25 +0000193}
194
Fariborz Jahanian8a1810f2008-11-22 18:39:36 +0000195void AggExprEmitter::VisitObjCKVCRefExpr(ObjCKVCRefExpr *E) {
196 RValue RV = CGF.EmitObjCPropertyGet(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 CGF.EmitAggregateCopy(DestPtr, RV.getAggregateAddr(), E->getType());
205}
206
Chris Lattner0f398c42008-07-26 22:37:01 +0000207void AggExprEmitter::VisitBinComma(const BinaryOperator *E) {
Eli Friedman4b0e2a32008-05-20 07:56:31 +0000208 CGF.EmitAnyExpr(E->getLHS());
209 CGF.EmitAggExpr(E->getRHS(), DestPtr, false);
210}
211
Chris Lattner49e3bfa2007-08-31 22:54:14 +0000212void AggExprEmitter::VisitStmtExpr(const StmtExpr *E) {
213 CGF.EmitCompoundStmt(*E->getSubStmt(), true, DestPtr, VolatileDest);
214}
215
Chris Lattner4758b402007-08-21 04:25:47 +0000216void AggExprEmitter::VisitBinaryOperator(const BinaryOperator *E) {
Daniel Dunbara7c8cf62008-08-16 00:56:44 +0000217 CGF.ErrorUnsupported(E, "aggregate binary expression");
Chris Lattner835635d2007-08-21 04:59:27 +0000218}
219
Chris Lattnercd9fb242007-08-21 04:43:17 +0000220void AggExprEmitter::VisitBinAssign(const BinaryOperator *E) {
Eli Friedmanf54c4e52008-02-11 01:09:17 +0000221 // For an assignment to work, the value on the right has
222 // to be compatible with the value on the left.
223 assert(CGF.getContext().typesAreCompatible(
224 E->getLHS()->getType().getUnqualifiedType(),
225 E->getRHS()->getType().getUnqualifiedType())
226 && "Invalid assignment");
Chris Lattner4758b402007-08-21 04:25:47 +0000227 LValue LHS = CGF.EmitLValue(E->getLHS());
Chris Lattner6278e6a2007-08-11 00:04:45 +0000228
Daniel Dunbar4b8c6db2008-08-30 05:35:15 +0000229 // We have to special case property setters, otherwise we must have
230 // a simple lvalue (no aggregates inside vectors, bitfields).
231 if (LHS.isPropertyRef()) {
232 // FIXME: Volatility?
233 llvm::Value *AggLoc = DestPtr;
234 if (!AggLoc)
235 AggLoc = CGF.CreateTempAlloca(CGF.ConvertType(E->getRHS()->getType()));
236 CGF.EmitAggExpr(E->getRHS(), AggLoc, false);
237 CGF.EmitObjCPropertySet(LHS.getPropertyRefExpr(),
238 RValue::getAggregate(AggLoc));
Fariborz Jahanian9ac53512008-11-22 22:30:21 +0000239 }
240 else if (LHS.isKVCRef()) {
241 // FIXME: Volatility?
242 llvm::Value *AggLoc = DestPtr;
243 if (!AggLoc)
244 AggLoc = CGF.CreateTempAlloca(CGF.ConvertType(E->getRHS()->getType()));
245 CGF.EmitAggExpr(E->getRHS(), AggLoc, false);
246 CGF.EmitObjCPropertySet(LHS.getKVCRefExpr(),
247 RValue::getAggregate(AggLoc));
Daniel Dunbar4b8c6db2008-08-30 05:35:15 +0000248 } else {
249 // Codegen the RHS so that it stores directly into the LHS.
Mike Stump86736572009-05-23 04:13:59 +0000250 CGF.EmitAggExpr(E->getRHS(), LHS.getAddress(), LHS.isVolatileQualified());
Daniel Dunbar4b8c6db2008-08-30 05:35:15 +0000251
252 if (DestPtr == 0)
253 return;
254
Mike Stumpa35af822009-05-23 02:02:29 +0000255 // If the result of the assignment is used, copy the LHS there also.
Daniel Dunbar0bc8e862008-09-09 20:49:46 +0000256 CGF.EmitAggregateCopy(DestPtr, LHS.getAddress(), E->getType());
Daniel Dunbar4b8c6db2008-08-30 05:35:15 +0000257 }
Chris Lattner6278e6a2007-08-11 00:04:45 +0000258}
259
Chris Lattner4758b402007-08-21 04:25:47 +0000260void AggExprEmitter::VisitConditionalOperator(const ConditionalOperator *E) {
Daniel Dunbara612e792008-11-13 01:38:36 +0000261 llvm::BasicBlock *LHSBlock = CGF.createBasicBlock("cond.true");
262 llvm::BasicBlock *RHSBlock = CGF.createBasicBlock("cond.false");
263 llvm::BasicBlock *ContBlock = CGF.createBasicBlock("cond.end");
Chris Lattner6278e6a2007-08-11 00:04:45 +0000264
Chris Lattner4758b402007-08-21 04:25:47 +0000265 llvm::Value *Cond = CGF.EvaluateExprAsBool(E->getCond());
Chris Lattnerbda69f82007-08-26 23:13:56 +0000266 Builder.CreateCondBr(Cond, LHSBlock, RHSBlock);
Chris Lattner6278e6a2007-08-11 00:04:45 +0000267
Chris Lattner4758b402007-08-21 04:25:47 +0000268 CGF.EmitBlock(LHSBlock);
Chris Lattner6278e6a2007-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 Lattner926792f2007-08-21 05:02:10 +0000273 Visit(E->getLHS());
Daniel Dunbarc56e6762008-11-11 09:41:28 +0000274 CGF.EmitBranch(ContBlock);
Chris Lattner6278e6a2007-08-11 00:04:45 +0000275
Chris Lattner4758b402007-08-21 04:25:47 +0000276 CGF.EmitBlock(RHSBlock);
Chris Lattner6278e6a2007-08-11 00:04:45 +0000277
Chris Lattner926792f2007-08-21 05:02:10 +0000278 Visit(E->getRHS());
Daniel Dunbarc56e6762008-11-11 09:41:28 +0000279 CGF.EmitBranch(ContBlock);
Chris Lattner6278e6a2007-08-11 00:04:45 +0000280
Chris Lattner4758b402007-08-21 04:25:47 +0000281 CGF.EmitBlock(ContBlock);
Chris Lattner6278e6a2007-08-11 00:04:45 +0000282}
Chris Lattner835635d2007-08-21 04:59:27 +0000283
Eli Friedman21911e82008-05-27 15:51:49 +0000284void AggExprEmitter::VisitVAArgExpr(VAArgExpr *VE) {
Daniel Dunbare9fcadd22009-02-11 22:25:55 +0000285 llvm::Value *ArgValue = CGF.EmitVAListRef(VE->getSubExpr());
Anders Carlsson13abd7e2008-11-04 05:30:00 +0000286 llvm::Value *ArgPtr = CGF.EmitVAArg(ArgValue, VE->getType());
287
Sebastian Redl020cddc2009-01-09 21:09:38 +0000288 if (!ArgPtr) {
Anders Carlsson13abd7e2008-11-04 05:30:00 +0000289 CGF.ErrorUnsupported(VE, "aggregate va_arg expression");
Sebastian Redl020cddc2009-01-09 21:09:38 +0000290 return;
291 }
292
Eli Friedman21911e82008-05-27 15:51:49 +0000293 if (DestPtr)
Eli Friedman327944b2008-06-13 23:01:12 +0000294 // FIXME: volatility
Anders Carlsson13abd7e2008-11-04 05:30:00 +0000295 CGF.EmitAggregateCopy(DestPtr, ArgPtr, VE->getType());
Eli Friedman21911e82008-05-27 15:51:49 +0000296}
297
Anders Carlssonb7f8f592009-04-17 00:06:03 +0000298void
Anders Carlsson1619a5042009-05-03 17:47:16 +0000299AggExprEmitter::VisitCXXConstructExpr(const CXXConstructExpr *E) {
Anders Carlssonc82b86d2009-05-19 04:48:36 +0000300 llvm::Value *V = DestPtr;
Anders Carlssonb7f8f592009-04-17 00:06:03 +0000301
Anders Carlssonc82b86d2009-05-19 04:48:36 +0000302 if (!V) {
303 assert(isa<CXXTempVarDecl>(E->getVarDecl()) &&
304 "Must have a temp var decl when there's no destination!");
305
306 V = CGF.CreateTempAlloca(CGF.ConvertType(E->getVarDecl()->getType()),
307 "tmpvar");
308 }
309
310 CGF.EmitCXXConstructExpr(V, E);
311}
312
313void AggExprEmitter::VisitCXXExprWithTemporaries(CXXExprWithTemporaries *E) {
314 // FIXME: Do something with the temporaries!
315 Visit(E->getSubExpr());
Anders Carlssonb7f8f592009-04-17 00:06:03 +0000316}
317
Chris Lattner579a05d2008-04-04 18:42:16 +0000318void AggExprEmitter::EmitInitializationToLValue(Expr* E, LValue LV) {
319 // FIXME: Are initializers affected by volatile?
Douglas Gregor0202cb42009-01-29 17:44:32 +0000320 if (isa<ImplicitValueInitExpr>(E)) {
Douglas Gregor347f7ea2009-01-28 21:54:33 +0000321 EmitNullInitializationToLValue(LV, E->getType());
Douglas Gregor0202cb42009-01-29 17:44:32 +0000322 } else if (E->getType()->isComplexType()) {
323 CGF.EmitComplexExprIntoAddr(E, LV.getAddress(), false);
Eli Friedman6e313212008-05-12 15:06:05 +0000324 } else if (CGF.hasAggregateLLVMType(E->getType())) {
325 CGF.EmitAnyExpr(E, LV.getAddress(), false);
326 } else {
327 CGF.EmitStoreThroughLValue(CGF.EmitAnyExpr(E), LV, E->getType());
Chris Lattner579a05d2008-04-04 18:42:16 +0000328 }
Chris Lattner579a05d2008-04-04 18:42:16 +0000329}
330
331void AggExprEmitter::EmitNullInitializationToLValue(LValue LV, QualType T) {
332 if (!CGF.hasAggregateLLVMType(T)) {
333 // For non-aggregates, we can store zero
Daniel Dunbare8bdce42008-08-06 05:32:55 +0000334 llvm::Value *Null = llvm::Constant::getNullValue(CGF.ConvertType(T));
335 CGF.EmitStoreThroughLValue(RValue::get(Null), LV, T);
Lauro Ramos Venancioe2162c62008-02-19 19:27:31 +0000336 } else {
Chris Lattner579a05d2008-04-04 18:42:16 +0000337 // Otherwise, just memset the whole thing to zero. This is legal
338 // because in LLVM, all default initializers are guaranteed to have a
339 // bit pattern of all zeros.
Eli Friedman20bb5e02009-04-13 21:47:26 +0000340 // FIXME: That isn't true for member pointers!
Chris Lattner579a05d2008-04-04 18:42:16 +0000341 // There's a potential optimization opportunity in combining
342 // memsets; that would be easy for arrays, but relatively
343 // difficult for structures with the current code.
Eli Friedman9127aa12009-03-28 03:10:45 +0000344 CGF.EmitMemSetToZero(LV.getAddress(), T);
Chris Lattner579a05d2008-04-04 18:42:16 +0000345 }
346}
347
Chris Lattner579a05d2008-04-04 18:42:16 +0000348void AggExprEmitter::VisitInitListExpr(InitListExpr *E) {
Eli Friedmanf5d08c92008-12-02 01:17:45 +0000349#if 0
350 // FIXME: Disabled while we figure out what to do about
351 // test/CodeGen/bitfield.c
352 //
Mike Stump18bb9282009-05-16 07:57:57 +0000353 // If we can, prefer a copy from a global; this is a lot less code for long
354 // globals, and it's easier for the current optimizers to analyze.
355 // FIXME: Should we really be doing this? Should we try to avoid cases where
356 // we emit a global with a lot of zeros? Should we try to avoid short
357 // globals?
Eli Friedman7139af42009-01-25 02:32:41 +0000358 if (E->isConstantInitializer(CGF.getContext(), 0)) {
Eli Friedmanc59bb482008-11-30 02:11:09 +0000359 llvm::Constant* C = CGF.CGM.EmitConstantExpr(E, &CGF);
360 llvm::GlobalVariable* GV =
361 new llvm::GlobalVariable(C->getType(), true,
362 llvm::GlobalValue::InternalLinkage,
363 C, "", &CGF.CGM.getModule(), 0);
364 CGF.EmitAggregateCopy(DestPtr, GV, E->getType());
365 return;
366 }
Eli Friedmanf5d08c92008-12-02 01:17:45 +0000367#endif
Douglas Gregorbf7207a2009-01-29 19:42:23 +0000368 if (E->hadArrayRangeDesignator()) {
369 CGF.ErrorUnsupported(E, "GNU array range designator extension");
370 }
371
Chris Lattner579a05d2008-04-04 18:42:16 +0000372 // Handle initialization of an array.
373 if (E->getType()->isArrayType()) {
374 const llvm::PointerType *APType =
375 cast<llvm::PointerType>(DestPtr->getType());
376 const llvm::ArrayType *AType =
377 cast<llvm::ArrayType>(APType->getElementType());
378
379 uint64_t NumInitElements = E->getNumInits();
Eli Friedmanf23b6fa2008-05-19 17:51:16 +0000380
Chris Lattner0f398c42008-07-26 22:37:01 +0000381 if (E->getNumInits() > 0) {
382 QualType T1 = E->getType();
383 QualType T2 = E->getInit(0)->getType();
384 if (CGF.getContext().getCanonicalType(T1).getUnqualifiedType() ==
385 CGF.getContext().getCanonicalType(T2).getUnqualifiedType()) {
386 EmitAggLoadOfLValue(E->getInit(0));
387 return;
388 }
Eli Friedmanf23b6fa2008-05-19 17:51:16 +0000389 }
390
Chris Lattner579a05d2008-04-04 18:42:16 +0000391 uint64_t NumArrayElements = AType->getNumElements();
Chris Lattner7adf0762008-08-04 07:31:14 +0000392 QualType ElementType = CGF.getContext().getCanonicalType(E->getType());
Douglas Gregor347f7ea2009-01-28 21:54:33 +0000393 ElementType = CGF.getContext().getAsArrayType(ElementType)->getElementType();
Chris Lattner579a05d2008-04-04 18:42:16 +0000394
Chris Lattner7adf0762008-08-04 07:31:14 +0000395 unsigned CVRqualifier = ElementType.getCVRQualifiers();
Eli Friedman327944b2008-06-13 23:01:12 +0000396
Chris Lattner579a05d2008-04-04 18:42:16 +0000397 for (uint64_t i = 0; i != NumArrayElements; ++i) {
398 llvm::Value *NextVal = Builder.CreateStructGEP(DestPtr, i, ".array");
399 if (i < NumInitElements)
Eli Friedman327944b2008-06-13 23:01:12 +0000400 EmitInitializationToLValue(E->getInit(i),
401 LValue::MakeAddr(NextVal, CVRqualifier));
Chris Lattner579a05d2008-04-04 18:42:16 +0000402 else
Eli Friedman327944b2008-06-13 23:01:12 +0000403 EmitNullInitializationToLValue(LValue::MakeAddr(NextVal, CVRqualifier),
Chris Lattner579a05d2008-04-04 18:42:16 +0000404 ElementType);
Lauro Ramos Venancioe2162c62008-02-19 19:27:31 +0000405 }
Chris Lattner579a05d2008-04-04 18:42:16 +0000406 return;
407 }
408
409 assert(E->getType()->isRecordType() && "Only support structs/unions here!");
410
411 // Do struct initialization; this code just sets each individual member
412 // to the approprate value. This makes bitfield support automatic;
413 // the disadvantage is that the generated code is more difficult for
414 // the optimizer, especially with bitfields.
415 unsigned NumInitElements = E->getNumInits();
416 RecordDecl *SD = E->getType()->getAsRecordType()->getDecl();
Chris Lattner579a05d2008-04-04 18:42:16 +0000417 unsigned CurInitVal = 0;
Douglas Gregor51695702009-01-29 16:53:55 +0000418
419 if (E->getType()->isUnionType()) {
420 // Only initialize one field of a union. The field itself is
421 // specified by the initializer list.
422 if (!E->getInitializedFieldInUnion()) {
423 // Empty union; we have nothing to do.
424
425#ifndef NDEBUG
426 // Make sure that it's really an empty and not a failure of
427 // semantic analysis.
Douglas Gregorbcced4e2009-04-09 21:40:53 +0000428 for (RecordDecl::field_iterator Field = SD->field_begin(CGF.getContext()),
429 FieldEnd = SD->field_end(CGF.getContext());
Douglas Gregor51695702009-01-29 16:53:55 +0000430 Field != FieldEnd; ++Field)
431 assert(Field->isUnnamedBitfield() && "Only unnamed bitfields allowed");
432#endif
433 return;
434 }
435
436 // FIXME: volatility
437 FieldDecl *Field = E->getInitializedFieldInUnion();
438 LValue FieldLoc = CGF.EmitLValueForField(DestPtr, Field, true, 0);
439
440 if (NumInitElements) {
441 // Store the initializer into the field
442 EmitInitializationToLValue(E->getInit(0), FieldLoc);
443 } else {
444 // Default-initialize to null
445 EmitNullInitializationToLValue(FieldLoc, Field->getType());
446 }
447
448 return;
449 }
Chris Lattner579a05d2008-04-04 18:42:16 +0000450
451 // Here we iterate over the fields; this makes it simpler to both
452 // default-initialize fields and skip over unnamed fields.
Douglas Gregorbcced4e2009-04-09 21:40:53 +0000453 for (RecordDecl::field_iterator Field = SD->field_begin(CGF.getContext()),
454 FieldEnd = SD->field_end(CGF.getContext());
Douglas Gregor91f84212008-12-11 16:49:14 +0000455 Field != FieldEnd; ++Field) {
456 // We're done once we hit the flexible array member
457 if (Field->getType()->isIncompleteArrayType())
458 break;
459
Douglas Gregor17bd0942009-01-28 23:36:17 +0000460 if (Field->isUnnamedBitfield())
Chris Lattner579a05d2008-04-04 18:42:16 +0000461 continue;
Douglas Gregor17bd0942009-01-28 23:36:17 +0000462
Eli Friedman327944b2008-06-13 23:01:12 +0000463 // FIXME: volatility
Douglas Gregor51695702009-01-29 16:53:55 +0000464 LValue FieldLoc = CGF.EmitLValueForField(DestPtr, *Field, false, 0);
Chris Lattner579a05d2008-04-04 18:42:16 +0000465 if (CurInitVal < NumInitElements) {
466 // Store the initializer into the field
Chris Lattner579a05d2008-04-04 18:42:16 +0000467 EmitInitializationToLValue(E->getInit(CurInitVal++), FieldLoc);
468 } else {
469 // We're out of initalizers; default-initialize to null
Douglas Gregor91f84212008-12-11 16:49:14 +0000470 EmitNullInitializationToLValue(FieldLoc, Field->getType());
Chris Lattner579a05d2008-04-04 18:42:16 +0000471 }
Lauro Ramos Venancioe2162c62008-02-19 19:27:31 +0000472 }
Devang Patel87174172007-10-26 17:44:44 +0000473}
474
Chris Lattner835635d2007-08-21 04:59:27 +0000475//===----------------------------------------------------------------------===//
476// Entry Points into this File
477//===----------------------------------------------------------------------===//
478
479/// EmitAggExpr - Emit the computation of the specified expression of
480/// aggregate type. The result is computed into DestPtr. Note that if
481/// DestPtr is null, the value of the aggregate expression is not needed.
482void CodeGenFunction::EmitAggExpr(const Expr *E, llvm::Value *DestPtr,
483 bool VolatileDest) {
484 assert(E && hasAggregateLLVMType(E->getType()) &&
485 "Invalid aggregate expression to emit");
486
487 AggExprEmitter(*this, DestPtr, VolatileDest).Visit(const_cast<Expr*>(E));
488}
Daniel Dunbar0bc8e862008-09-09 20:49:46 +0000489
490void CodeGenFunction::EmitAggregateClear(llvm::Value *DestPtr, QualType Ty) {
491 assert(!Ty->isAnyComplexType() && "Shouldn't happen for complex");
492
493 EmitMemSetToZero(DestPtr, Ty);
494}
495
496void CodeGenFunction::EmitAggregateCopy(llvm::Value *DestPtr,
497 llvm::Value *SrcPtr, QualType Ty) {
498 assert(!Ty->isAnyComplexType() && "Shouldn't happen for complex");
499
Chris Lattnerca05dfe2009-02-28 18:31:01 +0000500 // Aggregate assignment turns into llvm.memcpy. This is almost valid per
Chris Lattner3ef668c2009-02-28 18:18:58 +0000501 // C99 6.5.16.1p3, which states "If the value being stored in an object is
502 // read from another object that overlaps in anyway the storage of the first
503 // object, then the overlap shall be exact and the two objects shall have
504 // qualified or unqualified versions of a compatible type."
505 //
Chris Lattnerca05dfe2009-02-28 18:31:01 +0000506 // memcpy is not defined if the source and destination pointers are exactly
Chris Lattner3ef668c2009-02-28 18:18:58 +0000507 // equal, but other compilers do this optimization, and almost every memcpy
508 // implementation handles this case safely. If there is a libc that does not
509 // safely handle this, we can add a target hook.
Daniel Dunbar0bc8e862008-09-09 20:49:46 +0000510 const llvm::Type *BP = llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
511 if (DestPtr->getType() != BP)
512 DestPtr = Builder.CreateBitCast(DestPtr, BP, "tmp");
513 if (SrcPtr->getType() != BP)
514 SrcPtr = Builder.CreateBitCast(SrcPtr, BP, "tmp");
515
516 // Get size and alignment info for this aggregate.
517 std::pair<uint64_t, unsigned> TypeInfo = getContext().getTypeInfo(Ty);
518
519 // FIXME: Handle variable sized types.
520 const llvm::Type *IntPtr = llvm::IntegerType::get(LLVMPointerWidth);
521
Mike Stump86736572009-05-23 04:13:59 +0000522 // FIXME: If we have a volatile struct, the optimizer can remove what might
523 // appear to be `extra' memory ops:
524 //
525 // volatile struct { int i; } a, b;
526 //
527 // int main() {
528 // a = b;
529 // a = b;
530 // }
531 //
532 // either, we need to use a differnt call here, or the backend needs to be
533 // taught to not do this.
Chris Lattner3ef668c2009-02-28 18:18:58 +0000534 Builder.CreateCall4(CGM.getMemCpyFn(),
Daniel Dunbar0bc8e862008-09-09 20:49:46 +0000535 DestPtr, SrcPtr,
536 // TypeInfo.first describes size in bits.
537 llvm::ConstantInt::get(IntPtr, TypeInfo.first/8),
538 llvm::ConstantInt::get(llvm::Type::Int32Ty,
539 TypeInfo.second/8));
540}