blob: b216cf70a49b33ed9726198233b9a620167be117 [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"
Anders Carlssonb14095a2009-04-17 00:06:03 +000017#include "clang/AST/DeclCXX.h"
Daniel Dunbarde7fb842008-08-11 05:00:27 +000018#include "clang/AST/StmtVisitor.h"
Chris Lattner883f6a72007-08-11 00:04:45 +000019#include "llvm/Constants.h"
20#include "llvm/Function.h"
Devang Patel636c3d02007-10-26 17:44:44 +000021#include "llvm/GlobalVariable.h"
Chris Lattner9c033562007-08-21 04:25:47 +000022#include "llvm/Support/Compiler.h"
Chris Lattnerf81557c2008-04-04 18:42:16 +000023#include "llvm/Intrinsics.h"
Chris Lattneraf6f5282007-08-10 20:13:28 +000024using namespace clang;
25using namespace CodeGen;
Chris Lattner883f6a72007-08-11 00:04:45 +000026
Chris Lattner9c033562007-08-21 04:25:47 +000027//===----------------------------------------------------------------------===//
28// Aggregate Expression Emitter
29//===----------------------------------------------------------------------===//
30
31namespace {
32class VISIBILITY_HIDDEN AggExprEmitter : public StmtVisitor<AggExprEmitter> {
33 CodeGenFunction &CGF;
Daniel Dunbar45d196b2008-11-01 01:53:16 +000034 CGBuilderTy &Builder;
Chris Lattner9c033562007-08-21 04:25:47 +000035 llvm::Value *DestPtr;
36 bool VolatileDest;
37public:
38 AggExprEmitter(CodeGenFunction &cgf, llvm::Value *destPtr, bool volatileDest)
Chris Lattnerbfc0c1a2007-08-26 23:13:56 +000039 : CGF(cgf), Builder(CGF.Builder),
40 DestPtr(destPtr), VolatileDest(volatileDest) {
Chris Lattner9c033562007-08-21 04:25:47 +000041 }
42
Chris Lattneree755f92007-08-21 04:59:27 +000043 //===--------------------------------------------------------------------===//
44 // Utilities
45 //===--------------------------------------------------------------------===//
46
Chris Lattner9c033562007-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 Friedman922696f2008-05-19 17:51:16 +000051
Chris Lattneree755f92007-08-21 04:59:27 +000052 //===--------------------------------------------------------------------===//
53 // Visitor Methods
54 //===--------------------------------------------------------------------===//
55
Chris Lattner9c033562007-08-21 04:25:47 +000056 void VisitStmt(Stmt *S) {
Daniel Dunbar488e9932008-08-16 00:56:44 +000057 CGF.ErrorUnsupported(S, "aggregate expression");
Chris Lattner9c033562007-08-21 04:25:47 +000058 }
59 void VisitParenExpr(ParenExpr *PE) { Visit(PE->getSubExpr()); }
Eli Friedman12444a22009-01-27 09:03:41 +000060 void VisitUnaryExtension(UnaryOperator *E) { Visit(E->getSubExpr()); }
Chris Lattner9c033562007-08-21 04:25:47 +000061
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); }
Chris Lattnerf0a990c2009-04-21 23:00:09 +000067 void VisitCompoundLiteralExpr(CompoundLiteralExpr *E) {
68 EmitAggLoadOfLValue(E);
69 }
Seo Sanghyeon9b73b392007-12-14 02:04:12 +000070 void VisitArraySubscriptExpr(ArraySubscriptExpr *E) {
71 EmitAggLoadOfLValue(E);
72 }
Chris Lattnerf0a990c2009-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 Gregor4c678342009-01-28 21:54:33 +000079
Chris Lattner9c033562007-08-21 04:25:47 +000080 // Operators.
Nuno Lopes7e916272009-01-15 20:14:33 +000081 void VisitCStyleCastExpr(CStyleCastExpr *E);
Anders Carlssone4707ff2008-01-14 06:28:57 +000082 void VisitImplicitCastExpr(ImplicitCastExpr *E);
Anders Carlsson148fe672007-10-31 22:04:46 +000083 void VisitCallExpr(const CallExpr *E);
Chris Lattnerb2d963f2007-08-31 22:54:14 +000084 void VisitStmtExpr(const StmtExpr *E);
Chris Lattner9c033562007-08-21 04:25:47 +000085 void VisitBinaryOperator(const BinaryOperator *BO);
Chris Lattner03d6fb92007-08-21 04:43:17 +000086 void VisitBinAssign(const BinaryOperator *E);
Eli Friedman07fa52a2008-05-20 07:56:31 +000087 void VisitBinComma(const BinaryOperator *E);
Chris Lattner9c033562007-08-21 04:25:47 +000088
Chris Lattner8fdf3282008-06-24 17:04:18 +000089 void VisitObjCMessageExpr(ObjCMessageExpr *E);
Daniel Dunbar0a04d772008-08-23 10:51:21 +000090 void VisitObjCIvarRefExpr(ObjCIvarRefExpr *E) {
91 EmitAggLoadOfLValue(E);
92 }
Daniel Dunbar9c3fc702008-08-27 06:57:25 +000093 void VisitObjCPropertyRefExpr(ObjCPropertyRefExpr *E);
Fariborz Jahanian5daf5702008-11-22 18:39:36 +000094 void VisitObjCKVCRefExpr(ObjCKVCRefExpr *E);
Chris Lattner9c033562007-08-21 04:25:47 +000095
96 void VisitConditionalOperator(const ConditionalOperator *CO);
Devang Patel636c3d02007-10-26 17:44:44 +000097 void VisitInitListExpr(InitListExpr *E);
Chris Lattner04421082008-04-08 04:40:51 +000098 void VisitCXXDefaultArgExpr(CXXDefaultArgExpr *DAE) {
99 Visit(DAE->getExpr());
100 }
Anders Carlsson31ccf372009-05-03 17:47:16 +0000101 void VisitCXXConstructExpr(const CXXConstructExpr *E);
Eli Friedmanb1851242008-05-27 15:51:49 +0000102 void VisitVAArgExpr(VAArgExpr *E);
Chris Lattnerf81557c2008-04-04 18:42:16 +0000103
104 void EmitInitializationToLValue(Expr *E, LValue Address);
105 void EmitNullInitializationToLValue(LValue Address, QualType T);
Chris Lattner9c033562007-08-21 04:25:47 +0000106 // case Expr::ChooseExprClass:
Lauro Ramos Venancio305762c2008-02-18 22:44:02 +0000107
Chris Lattner9c033562007-08-21 04:25:47 +0000108};
109} // end anonymous namespace.
110
Chris Lattneree755f92007-08-21 04:59:27 +0000111//===----------------------------------------------------------------------===//
112// Utilities
113//===----------------------------------------------------------------------===//
Chris Lattner9c033562007-08-21 04:25:47 +0000114
Chris Lattner883f6a72007-08-11 00:04:45 +0000115/// EmitAggLoadOfLValue - Given an expression with aggregate type that
116/// represents a value lvalue, this method emits the address of the lvalue,
117/// then loads the result into DestPtr.
Chris Lattner9c033562007-08-21 04:25:47 +0000118void AggExprEmitter::EmitAggLoadOfLValue(const Expr *E) {
119 LValue LV = CGF.EmitLValue(E);
Chris Lattner883f6a72007-08-11 00:04:45 +0000120 assert(LV.isSimple() && "Can't have aggregate bitfield, vector, etc");
121 llvm::Value *SrcPtr = LV.getAddress();
122
123 // If the result is ignored, don't copy from the value.
124 if (DestPtr == 0)
125 // FIXME: If the source is volatile, we must read from it.
126 return;
127
Daniel Dunbar7482d122008-09-09 20:49:46 +0000128 CGF.EmitAggregateCopy(DestPtr, SrcPtr, E->getType());
Chris Lattner883f6a72007-08-11 00:04:45 +0000129}
130
Chris Lattneree755f92007-08-21 04:59:27 +0000131//===----------------------------------------------------------------------===//
132// Visitor Methods
133//===----------------------------------------------------------------------===//
134
Nuno Lopes7e916272009-01-15 20:14:33 +0000135void AggExprEmitter::VisitCStyleCastExpr(CStyleCastExpr *E) {
136 // GCC union extension
137 if (E->getType()->isUnionType()) {
138 RecordDecl *SD = E->getType()->getAsRecordType()->getDecl();
Douglas Gregor6ab35242009-04-09 21:40:53 +0000139 LValue FieldLoc = CGF.EmitLValueForField(DestPtr,
140 *SD->field_begin(CGF.getContext()),
141 true, 0);
Nuno Lopes7e916272009-01-15 20:14:33 +0000142 EmitInitializationToLValue(E->getSubExpr(), FieldLoc);
143 return;
144 }
145
146 Visit(E->getSubExpr());
147}
148
Chris Lattner96196622008-07-26 22:37:01 +0000149void AggExprEmitter::VisitImplicitCastExpr(ImplicitCastExpr *E) {
Eli Friedmanff6e2b72008-02-11 01:09:17 +0000150 assert(CGF.getContext().typesAreCompatible(
Chris Lattner96196622008-07-26 22:37:01 +0000151 E->getSubExpr()->getType().getUnqualifiedType(),
152 E->getType().getUnqualifiedType()) &&
153 "Implicit cast types must be compatible");
Anders Carlssone4707ff2008-01-14 06:28:57 +0000154 Visit(E->getSubExpr());
155}
156
Chris Lattner96196622008-07-26 22:37:01 +0000157void AggExprEmitter::VisitCallExpr(const CallExpr *E) {
Anders Carlsson148fe672007-10-31 22:04:46 +0000158 RValue RV = CGF.EmitCallExpr(E);
159 assert(RV.isAggregate() && "Return value must be aggregate value!");
160
161 // If the result is ignored, don't copy from the value.
162 if (DestPtr == 0)
163 // FIXME: If the source is volatile, we must read from it.
164 return;
165
Daniel Dunbar7482d122008-09-09 20:49:46 +0000166 CGF.EmitAggregateCopy(DestPtr, RV.getAggregateAddr(), E->getType());
Anders Carlsson148fe672007-10-31 22:04:46 +0000167}
Chris Lattner96196622008-07-26 22:37:01 +0000168
169void AggExprEmitter::VisitObjCMessageExpr(ObjCMessageExpr *E) {
Daniel Dunbar8f2926b2008-08-23 03:46:30 +0000170 RValue RV = CGF.EmitObjCMessageExpr(E);
171 assert(RV.isAggregate() && "Return value must be aggregate value!");
Chris Lattner8fdf3282008-06-24 17:04:18 +0000172
173 // If the result is ignored, don't copy from the value.
174 if (DestPtr == 0)
Daniel Dunbar8f2926b2008-08-23 03:46:30 +0000175 // FIXME: If the source is volatile, we must read from it.
Chris Lattner8fdf3282008-06-24 17:04:18 +0000176 return;
177
Daniel Dunbar7482d122008-09-09 20:49:46 +0000178 CGF.EmitAggregateCopy(DestPtr, RV.getAggregateAddr(), E->getType());
Chris Lattner8fdf3282008-06-24 17:04:18 +0000179}
Anders Carlsson148fe672007-10-31 22:04:46 +0000180
Daniel Dunbar9c3fc702008-08-27 06:57:25 +0000181void AggExprEmitter::VisitObjCPropertyRefExpr(ObjCPropertyRefExpr *E) {
182 RValue RV = CGF.EmitObjCPropertyGet(E);
183 assert(RV.isAggregate() && "Return value must be aggregate value!");
184
185 // If the result is ignored, don't copy from the value.
186 if (DestPtr == 0)
187 // FIXME: If the source is volatile, we must read from it.
188 return;
189
Daniel Dunbar7482d122008-09-09 20:49:46 +0000190 CGF.EmitAggregateCopy(DestPtr, RV.getAggregateAddr(), E->getType());
Daniel Dunbar9c3fc702008-08-27 06:57:25 +0000191}
192
Fariborz Jahanian5daf5702008-11-22 18:39:36 +0000193void AggExprEmitter::VisitObjCKVCRefExpr(ObjCKVCRefExpr *E) {
194 RValue RV = CGF.EmitObjCPropertyGet(E);
195 assert(RV.isAggregate() && "Return value must be aggregate value!");
196
197 // If the result is ignored, don't copy from the value.
198 if (DestPtr == 0)
199 // FIXME: If the source is volatile, we must read from it.
200 return;
201
202 CGF.EmitAggregateCopy(DestPtr, RV.getAggregateAddr(), E->getType());
203}
204
Chris Lattner96196622008-07-26 22:37:01 +0000205void AggExprEmitter::VisitBinComma(const BinaryOperator *E) {
Eli Friedman07fa52a2008-05-20 07:56:31 +0000206 CGF.EmitAnyExpr(E->getLHS());
207 CGF.EmitAggExpr(E->getRHS(), DestPtr, false);
208}
209
Chris Lattnerb2d963f2007-08-31 22:54:14 +0000210void AggExprEmitter::VisitStmtExpr(const StmtExpr *E) {
211 CGF.EmitCompoundStmt(*E->getSubStmt(), true, DestPtr, VolatileDest);
212}
213
Chris Lattner9c033562007-08-21 04:25:47 +0000214void AggExprEmitter::VisitBinaryOperator(const BinaryOperator *E) {
Daniel Dunbar488e9932008-08-16 00:56:44 +0000215 CGF.ErrorUnsupported(E, "aggregate binary expression");
Chris Lattneree755f92007-08-21 04:59:27 +0000216}
217
Chris Lattner03d6fb92007-08-21 04:43:17 +0000218void AggExprEmitter::VisitBinAssign(const BinaryOperator *E) {
Eli Friedmanff6e2b72008-02-11 01:09:17 +0000219 // For an assignment to work, the value on the right has
220 // to be compatible with the value on the left.
221 assert(CGF.getContext().typesAreCompatible(
222 E->getLHS()->getType().getUnqualifiedType(),
223 E->getRHS()->getType().getUnqualifiedType())
224 && "Invalid assignment");
Chris Lattner9c033562007-08-21 04:25:47 +0000225 LValue LHS = CGF.EmitLValue(E->getLHS());
Chris Lattner883f6a72007-08-11 00:04:45 +0000226
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000227 // We have to special case property setters, otherwise we must have
228 // a simple lvalue (no aggregates inside vectors, bitfields).
229 if (LHS.isPropertyRef()) {
230 // FIXME: Volatility?
231 llvm::Value *AggLoc = DestPtr;
232 if (!AggLoc)
233 AggLoc = CGF.CreateTempAlloca(CGF.ConvertType(E->getRHS()->getType()));
234 CGF.EmitAggExpr(E->getRHS(), AggLoc, false);
235 CGF.EmitObjCPropertySet(LHS.getPropertyRefExpr(),
236 RValue::getAggregate(AggLoc));
Fariborz Jahanian43f44702008-11-22 22:30:21 +0000237 }
238 else if (LHS.isKVCRef()) {
239 // FIXME: Volatility?
240 llvm::Value *AggLoc = DestPtr;
241 if (!AggLoc)
242 AggLoc = CGF.CreateTempAlloca(CGF.ConvertType(E->getRHS()->getType()));
243 CGF.EmitAggExpr(E->getRHS(), AggLoc, false);
244 CGF.EmitObjCPropertySet(LHS.getKVCRefExpr(),
245 RValue::getAggregate(AggLoc));
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000246 } else {
247 // Codegen the RHS so that it stores directly into the LHS.
248 CGF.EmitAggExpr(E->getRHS(), LHS.getAddress(), false /*FIXME: VOLATILE LHS*/);
249
250 if (DestPtr == 0)
251 return;
252
253 // If the result of the assignment is used, copy the RHS there also.
Daniel Dunbar7482d122008-09-09 20:49:46 +0000254 CGF.EmitAggregateCopy(DestPtr, LHS.getAddress(), E->getType());
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000255 }
Chris Lattner883f6a72007-08-11 00:04:45 +0000256}
257
Chris Lattner9c033562007-08-21 04:25:47 +0000258void AggExprEmitter::VisitConditionalOperator(const ConditionalOperator *E) {
Daniel Dunbar9615ecb2008-11-13 01:38:36 +0000259 llvm::BasicBlock *LHSBlock = CGF.createBasicBlock("cond.true");
260 llvm::BasicBlock *RHSBlock = CGF.createBasicBlock("cond.false");
261 llvm::BasicBlock *ContBlock = CGF.createBasicBlock("cond.end");
Chris Lattner883f6a72007-08-11 00:04:45 +0000262
Chris Lattner9c033562007-08-21 04:25:47 +0000263 llvm::Value *Cond = CGF.EvaluateExprAsBool(E->getCond());
Chris Lattnerbfc0c1a2007-08-26 23:13:56 +0000264 Builder.CreateCondBr(Cond, LHSBlock, RHSBlock);
Chris Lattner883f6a72007-08-11 00:04:45 +0000265
Chris Lattner9c033562007-08-21 04:25:47 +0000266 CGF.EmitBlock(LHSBlock);
Chris Lattner883f6a72007-08-11 00:04:45 +0000267
268 // Handle the GNU extension for missing LHS.
269 assert(E->getLHS() && "Must have LHS for aggregate value");
270
Chris Lattnerc748f272007-08-21 05:02:10 +0000271 Visit(E->getLHS());
Daniel Dunbard57a8712008-11-11 09:41:28 +0000272 CGF.EmitBranch(ContBlock);
Chris Lattner883f6a72007-08-11 00:04:45 +0000273
Chris Lattner9c033562007-08-21 04:25:47 +0000274 CGF.EmitBlock(RHSBlock);
Chris Lattner883f6a72007-08-11 00:04:45 +0000275
Chris Lattnerc748f272007-08-21 05:02:10 +0000276 Visit(E->getRHS());
Daniel Dunbard57a8712008-11-11 09:41:28 +0000277 CGF.EmitBranch(ContBlock);
Chris Lattner883f6a72007-08-11 00:04:45 +0000278
Chris Lattner9c033562007-08-21 04:25:47 +0000279 CGF.EmitBlock(ContBlock);
Chris Lattner883f6a72007-08-11 00:04:45 +0000280}
Chris Lattneree755f92007-08-21 04:59:27 +0000281
Eli Friedmanb1851242008-05-27 15:51:49 +0000282void AggExprEmitter::VisitVAArgExpr(VAArgExpr *VE) {
Daniel Dunbar07855702009-02-11 22:25:55 +0000283 llvm::Value *ArgValue = CGF.EmitVAListRef(VE->getSubExpr());
Anders Carlssonddf7cac2008-11-04 05:30:00 +0000284 llvm::Value *ArgPtr = CGF.EmitVAArg(ArgValue, VE->getType());
285
Sebastian Redl0262f022009-01-09 21:09:38 +0000286 if (!ArgPtr) {
Anders Carlssonddf7cac2008-11-04 05:30:00 +0000287 CGF.ErrorUnsupported(VE, "aggregate va_arg expression");
Sebastian Redl0262f022009-01-09 21:09:38 +0000288 return;
289 }
290
Eli Friedmanb1851242008-05-27 15:51:49 +0000291 if (DestPtr)
Eli Friedman1e692ac2008-06-13 23:01:12 +0000292 // FIXME: volatility
Anders Carlssonddf7cac2008-11-04 05:30:00 +0000293 CGF.EmitAggregateCopy(DestPtr, ArgPtr, VE->getType());
Eli Friedmanb1851242008-05-27 15:51:49 +0000294}
295
Anders Carlssonb14095a2009-04-17 00:06:03 +0000296void
Anders Carlsson31ccf372009-05-03 17:47:16 +0000297AggExprEmitter::VisitCXXConstructExpr(const CXXConstructExpr *E) {
298 assert(DestPtr && "Must have a dest to emit into!");
Anders Carlssonb14095a2009-04-17 00:06:03 +0000299
Anders Carlsson31ccf372009-05-03 17:47:16 +0000300 CGF.EmitCXXConstructExpr(DestPtr, E);
Anders Carlssonb14095a2009-04-17 00:06:03 +0000301}
302
Chris Lattnerf81557c2008-04-04 18:42:16 +0000303void AggExprEmitter::EmitInitializationToLValue(Expr* E, LValue LV) {
304 // FIXME: Are initializers affected by volatile?
Douglas Gregor3498bdb2009-01-29 17:44:32 +0000305 if (isa<ImplicitValueInitExpr>(E)) {
Douglas Gregor4c678342009-01-28 21:54:33 +0000306 EmitNullInitializationToLValue(LV, E->getType());
Douglas Gregor3498bdb2009-01-29 17:44:32 +0000307 } else if (E->getType()->isComplexType()) {
308 CGF.EmitComplexExprIntoAddr(E, LV.getAddress(), false);
Eli Friedmanc8ba9612008-05-12 15:06:05 +0000309 } else if (CGF.hasAggregateLLVMType(E->getType())) {
310 CGF.EmitAnyExpr(E, LV.getAddress(), false);
311 } else {
312 CGF.EmitStoreThroughLValue(CGF.EmitAnyExpr(E), LV, E->getType());
Chris Lattnerf81557c2008-04-04 18:42:16 +0000313 }
Chris Lattnerf81557c2008-04-04 18:42:16 +0000314}
315
316void AggExprEmitter::EmitNullInitializationToLValue(LValue LV, QualType T) {
317 if (!CGF.hasAggregateLLVMType(T)) {
318 // For non-aggregates, we can store zero
Daniel Dunbar82397132008-08-06 05:32:55 +0000319 llvm::Value *Null = llvm::Constant::getNullValue(CGF.ConvertType(T));
320 CGF.EmitStoreThroughLValue(RValue::get(Null), LV, T);
Lauro Ramos Venancio145cd892008-02-19 19:27:31 +0000321 } else {
Chris Lattnerf81557c2008-04-04 18:42:16 +0000322 // Otherwise, just memset the whole thing to zero. This is legal
323 // because in LLVM, all default initializers are guaranteed to have a
324 // bit pattern of all zeros.
Eli Friedman0f593122009-04-13 21:47:26 +0000325 // FIXME: That isn't true for member pointers!
Chris Lattnerf81557c2008-04-04 18:42:16 +0000326 // There's a potential optimization opportunity in combining
327 // memsets; that would be easy for arrays, but relatively
328 // difficult for structures with the current code.
Eli Friedmanccf0ed82009-03-28 03:10:45 +0000329 CGF.EmitMemSetToZero(LV.getAddress(), T);
Chris Lattnerf81557c2008-04-04 18:42:16 +0000330 }
331}
332
Chris Lattnerf81557c2008-04-04 18:42:16 +0000333void AggExprEmitter::VisitInitListExpr(InitListExpr *E) {
Eli Friedmana385b3c2008-12-02 01:17:45 +0000334#if 0
335 // FIXME: Disabled while we figure out what to do about
336 // test/CodeGen/bitfield.c
337 //
Mike Stumpf5408fe2009-05-16 07:57:57 +0000338 // If we can, prefer a copy from a global; this is a lot less code for long
339 // globals, and it's easier for the current optimizers to analyze.
340 // FIXME: Should we really be doing this? Should we try to avoid cases where
341 // we emit a global with a lot of zeros? Should we try to avoid short
342 // globals?
Eli Friedmanc9e8f602009-01-25 02:32:41 +0000343 if (E->isConstantInitializer(CGF.getContext(), 0)) {
Eli Friedman994ffef2008-11-30 02:11:09 +0000344 llvm::Constant* C = CGF.CGM.EmitConstantExpr(E, &CGF);
345 llvm::GlobalVariable* GV =
346 new llvm::GlobalVariable(C->getType(), true,
347 llvm::GlobalValue::InternalLinkage,
348 C, "", &CGF.CGM.getModule(), 0);
349 CGF.EmitAggregateCopy(DestPtr, GV, E->getType());
350 return;
351 }
Eli Friedmana385b3c2008-12-02 01:17:45 +0000352#endif
Douglas Gregora9c87802009-01-29 19:42:23 +0000353 if (E->hadArrayRangeDesignator()) {
354 CGF.ErrorUnsupported(E, "GNU array range designator extension");
355 }
356
Chris Lattnerf81557c2008-04-04 18:42:16 +0000357 // Handle initialization of an array.
358 if (E->getType()->isArrayType()) {
359 const llvm::PointerType *APType =
360 cast<llvm::PointerType>(DestPtr->getType());
361 const llvm::ArrayType *AType =
362 cast<llvm::ArrayType>(APType->getElementType());
363
364 uint64_t NumInitElements = E->getNumInits();
Eli Friedman922696f2008-05-19 17:51:16 +0000365
Chris Lattner96196622008-07-26 22:37:01 +0000366 if (E->getNumInits() > 0) {
367 QualType T1 = E->getType();
368 QualType T2 = E->getInit(0)->getType();
369 if (CGF.getContext().getCanonicalType(T1).getUnqualifiedType() ==
370 CGF.getContext().getCanonicalType(T2).getUnqualifiedType()) {
371 EmitAggLoadOfLValue(E->getInit(0));
372 return;
373 }
Eli Friedman922696f2008-05-19 17:51:16 +0000374 }
375
Chris Lattnerf81557c2008-04-04 18:42:16 +0000376 uint64_t NumArrayElements = AType->getNumElements();
Chris Lattnerc63a1f22008-08-04 07:31:14 +0000377 QualType ElementType = CGF.getContext().getCanonicalType(E->getType());
Douglas Gregor4c678342009-01-28 21:54:33 +0000378 ElementType = CGF.getContext().getAsArrayType(ElementType)->getElementType();
Chris Lattnerf81557c2008-04-04 18:42:16 +0000379
Chris Lattnerc63a1f22008-08-04 07:31:14 +0000380 unsigned CVRqualifier = ElementType.getCVRQualifiers();
Eli Friedman1e692ac2008-06-13 23:01:12 +0000381
Chris Lattnerf81557c2008-04-04 18:42:16 +0000382 for (uint64_t i = 0; i != NumArrayElements; ++i) {
383 llvm::Value *NextVal = Builder.CreateStructGEP(DestPtr, i, ".array");
384 if (i < NumInitElements)
Eli Friedman1e692ac2008-06-13 23:01:12 +0000385 EmitInitializationToLValue(E->getInit(i),
386 LValue::MakeAddr(NextVal, CVRqualifier));
Chris Lattnerf81557c2008-04-04 18:42:16 +0000387 else
Eli Friedman1e692ac2008-06-13 23:01:12 +0000388 EmitNullInitializationToLValue(LValue::MakeAddr(NextVal, CVRqualifier),
Chris Lattnerf81557c2008-04-04 18:42:16 +0000389 ElementType);
Lauro Ramos Venancio145cd892008-02-19 19:27:31 +0000390 }
Chris Lattnerf81557c2008-04-04 18:42:16 +0000391 return;
392 }
393
394 assert(E->getType()->isRecordType() && "Only support structs/unions here!");
395
396 // Do struct initialization; this code just sets each individual member
397 // to the approprate value. This makes bitfield support automatic;
398 // the disadvantage is that the generated code is more difficult for
399 // the optimizer, especially with bitfields.
400 unsigned NumInitElements = E->getNumInits();
401 RecordDecl *SD = E->getType()->getAsRecordType()->getDecl();
Chris Lattnerf81557c2008-04-04 18:42:16 +0000402 unsigned CurInitVal = 0;
Douglas Gregor0bb76892009-01-29 16:53:55 +0000403
404 if (E->getType()->isUnionType()) {
405 // Only initialize one field of a union. The field itself is
406 // specified by the initializer list.
407 if (!E->getInitializedFieldInUnion()) {
408 // Empty union; we have nothing to do.
409
410#ifndef NDEBUG
411 // Make sure that it's really an empty and not a failure of
412 // semantic analysis.
Douglas Gregor6ab35242009-04-09 21:40:53 +0000413 for (RecordDecl::field_iterator Field = SD->field_begin(CGF.getContext()),
414 FieldEnd = SD->field_end(CGF.getContext());
Douglas Gregor0bb76892009-01-29 16:53:55 +0000415 Field != FieldEnd; ++Field)
416 assert(Field->isUnnamedBitfield() && "Only unnamed bitfields allowed");
417#endif
418 return;
419 }
420
421 // FIXME: volatility
422 FieldDecl *Field = E->getInitializedFieldInUnion();
423 LValue FieldLoc = CGF.EmitLValueForField(DestPtr, Field, true, 0);
424
425 if (NumInitElements) {
426 // Store the initializer into the field
427 EmitInitializationToLValue(E->getInit(0), FieldLoc);
428 } else {
429 // Default-initialize to null
430 EmitNullInitializationToLValue(FieldLoc, Field->getType());
431 }
432
433 return;
434 }
Chris Lattnerf81557c2008-04-04 18:42:16 +0000435
436 // Here we iterate over the fields; this makes it simpler to both
437 // default-initialize fields and skip over unnamed fields.
Douglas Gregor6ab35242009-04-09 21:40:53 +0000438 for (RecordDecl::field_iterator Field = SD->field_begin(CGF.getContext()),
439 FieldEnd = SD->field_end(CGF.getContext());
Douglas Gregor44b43212008-12-11 16:49:14 +0000440 Field != FieldEnd; ++Field) {
441 // We're done once we hit the flexible array member
442 if (Field->getType()->isIncompleteArrayType())
443 break;
444
Douglas Gregor34e79462009-01-28 23:36:17 +0000445 if (Field->isUnnamedBitfield())
Chris Lattnerf81557c2008-04-04 18:42:16 +0000446 continue;
Douglas Gregor34e79462009-01-28 23:36:17 +0000447
Eli Friedman1e692ac2008-06-13 23:01:12 +0000448 // FIXME: volatility
Douglas Gregor0bb76892009-01-29 16:53:55 +0000449 LValue FieldLoc = CGF.EmitLValueForField(DestPtr, *Field, false, 0);
Chris Lattnerf81557c2008-04-04 18:42:16 +0000450 if (CurInitVal < NumInitElements) {
451 // Store the initializer into the field
Chris Lattnerf81557c2008-04-04 18:42:16 +0000452 EmitInitializationToLValue(E->getInit(CurInitVal++), FieldLoc);
453 } else {
454 // We're out of initalizers; default-initialize to null
Douglas Gregor44b43212008-12-11 16:49:14 +0000455 EmitNullInitializationToLValue(FieldLoc, Field->getType());
Chris Lattnerf81557c2008-04-04 18:42:16 +0000456 }
Lauro Ramos Venancio145cd892008-02-19 19:27:31 +0000457 }
Devang Patel636c3d02007-10-26 17:44:44 +0000458}
459
Chris Lattneree755f92007-08-21 04:59:27 +0000460//===----------------------------------------------------------------------===//
461// Entry Points into this File
462//===----------------------------------------------------------------------===//
463
464/// EmitAggExpr - Emit the computation of the specified expression of
465/// aggregate type. The result is computed into DestPtr. Note that if
466/// DestPtr is null, the value of the aggregate expression is not needed.
467void CodeGenFunction::EmitAggExpr(const Expr *E, llvm::Value *DestPtr,
468 bool VolatileDest) {
469 assert(E && hasAggregateLLVMType(E->getType()) &&
470 "Invalid aggregate expression to emit");
471
472 AggExprEmitter(*this, DestPtr, VolatileDest).Visit(const_cast<Expr*>(E));
473}
Daniel Dunbar7482d122008-09-09 20:49:46 +0000474
475void CodeGenFunction::EmitAggregateClear(llvm::Value *DestPtr, QualType Ty) {
476 assert(!Ty->isAnyComplexType() && "Shouldn't happen for complex");
477
478 EmitMemSetToZero(DestPtr, Ty);
479}
480
481void CodeGenFunction::EmitAggregateCopy(llvm::Value *DestPtr,
482 llvm::Value *SrcPtr, QualType Ty) {
483 assert(!Ty->isAnyComplexType() && "Shouldn't happen for complex");
484
Chris Lattner83c96292009-02-28 18:31:01 +0000485 // Aggregate assignment turns into llvm.memcpy. This is almost valid per
Chris Lattnerca4fc2c2009-02-28 18:18:58 +0000486 // C99 6.5.16.1p3, which states "If the value being stored in an object is
487 // read from another object that overlaps in anyway the storage of the first
488 // object, then the overlap shall be exact and the two objects shall have
489 // qualified or unqualified versions of a compatible type."
490 //
Chris Lattner83c96292009-02-28 18:31:01 +0000491 // memcpy is not defined if the source and destination pointers are exactly
Chris Lattnerca4fc2c2009-02-28 18:18:58 +0000492 // equal, but other compilers do this optimization, and almost every memcpy
493 // implementation handles this case safely. If there is a libc that does not
494 // safely handle this, we can add a target hook.
Daniel Dunbar7482d122008-09-09 20:49:46 +0000495 const llvm::Type *BP = llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
496 if (DestPtr->getType() != BP)
497 DestPtr = Builder.CreateBitCast(DestPtr, BP, "tmp");
498 if (SrcPtr->getType() != BP)
499 SrcPtr = Builder.CreateBitCast(SrcPtr, BP, "tmp");
500
501 // Get size and alignment info for this aggregate.
502 std::pair<uint64_t, unsigned> TypeInfo = getContext().getTypeInfo(Ty);
503
504 // FIXME: Handle variable sized types.
505 const llvm::Type *IntPtr = llvm::IntegerType::get(LLVMPointerWidth);
506
Chris Lattnerca4fc2c2009-02-28 18:18:58 +0000507 Builder.CreateCall4(CGM.getMemCpyFn(),
Daniel Dunbar7482d122008-09-09 20:49:46 +0000508 DestPtr, SrcPtr,
509 // TypeInfo.first describes size in bits.
510 llvm::ConstantInt::get(IntPtr, TypeInfo.first/8),
511 llvm::ConstantInt::get(llvm::Type::Int32Ty,
512 TypeInfo.second/8));
513}