blob: 951a5f0e39645852bedcc0f6cfe371131217289e [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
Mike Stumpca9fc092009-05-23 20:28:01 +000052 /// EmitFinalDestCopy - Perform the final copy to DestPtr, if desired.
53 void EmitFinalDestCopy(const Expr *E, LValue Src);
54 void EmitFinalDestCopy(const Expr *E, RValue Src);
55
Chris Lattner835635d2007-08-21 04:59:27 +000056 //===--------------------------------------------------------------------===//
57 // Visitor Methods
58 //===--------------------------------------------------------------------===//
59
Chris Lattner4758b402007-08-21 04:25:47 +000060 void VisitStmt(Stmt *S) {
Daniel Dunbara7c8cf62008-08-16 00:56:44 +000061 CGF.ErrorUnsupported(S, "aggregate expression");
Chris Lattner4758b402007-08-21 04:25:47 +000062 }
63 void VisitParenExpr(ParenExpr *PE) { Visit(PE->getSubExpr()); }
Eli Friedman3f66b842009-01-27 09:03:41 +000064 void VisitUnaryExtension(UnaryOperator *E) { Visit(E->getSubExpr()); }
Chris Lattner4758b402007-08-21 04:25:47 +000065
66 // l-values.
Seo Sanghyeond4d8c3c2007-12-14 02:04:12 +000067 void VisitDeclRefExpr(DeclRefExpr *DRE) { EmitAggLoadOfLValue(DRE); }
68 void VisitMemberExpr(MemberExpr *ME) { EmitAggLoadOfLValue(ME); }
69 void VisitUnaryDeref(UnaryOperator *E) { EmitAggLoadOfLValue(E); }
Seo Sanghyeon6f1b2742007-12-23 03:11:58 +000070 void VisitStringLiteral(StringLiteral *E) { EmitAggLoadOfLValue(E); }
Chris Lattner2f343dd2009-04-21 23:00:09 +000071 void VisitCompoundLiteralExpr(CompoundLiteralExpr *E) {
72 EmitAggLoadOfLValue(E);
73 }
Seo Sanghyeond4d8c3c2007-12-14 02:04:12 +000074 void VisitArraySubscriptExpr(ArraySubscriptExpr *E) {
75 EmitAggLoadOfLValue(E);
76 }
Chris Lattner2f343dd2009-04-21 23:00:09 +000077 void VisitBlockDeclRefExpr(const BlockDeclRefExpr *E) {
78 EmitAggLoadOfLValue(E);
79 }
80 void VisitPredefinedExpr(const PredefinedExpr *E) {
81 EmitAggLoadOfLValue(E);
82 }
Douglas Gregor347f7ea2009-01-28 21:54:33 +000083
Chris Lattner4758b402007-08-21 04:25:47 +000084 // Operators.
Nuno Lopes7ffcf932009-01-15 20:14:33 +000085 void VisitCStyleCastExpr(CStyleCastExpr *E);
Anders Carlsson1ba25ca2008-01-14 06:28:57 +000086 void VisitImplicitCastExpr(ImplicitCastExpr *E);
Anders Carlsson0370eb22007-10-31 22:04:46 +000087 void VisitCallExpr(const CallExpr *E);
Chris Lattner49e3bfa2007-08-31 22:54:14 +000088 void VisitStmtExpr(const StmtExpr *E);
Chris Lattner4758b402007-08-21 04:25:47 +000089 void VisitBinaryOperator(const BinaryOperator *BO);
Chris Lattnercd9fb242007-08-21 04:43:17 +000090 void VisitBinAssign(const BinaryOperator *E);
Eli Friedman4b0e2a32008-05-20 07:56:31 +000091 void VisitBinComma(const BinaryOperator *E);
Chris Lattner4758b402007-08-21 04:25:47 +000092
Chris Lattnerb1d329d2008-06-24 17:04:18 +000093 void VisitObjCMessageExpr(ObjCMessageExpr *E);
Daniel Dunbarc8317a42008-08-23 10:51:21 +000094 void VisitObjCIvarRefExpr(ObjCIvarRefExpr *E) {
95 EmitAggLoadOfLValue(E);
96 }
Daniel Dunbar55310df2008-08-27 06:57:25 +000097 void VisitObjCPropertyRefExpr(ObjCPropertyRefExpr *E);
Fariborz Jahanian8a1810f2008-11-22 18:39:36 +000098 void VisitObjCKVCRefExpr(ObjCKVCRefExpr *E);
Chris Lattner4758b402007-08-21 04:25:47 +000099
100 void VisitConditionalOperator(const ConditionalOperator *CO);
Devang Patel87174172007-10-26 17:44:44 +0000101 void VisitInitListExpr(InitListExpr *E);
Chris Lattneraa9c7ae2008-04-08 04:40:51 +0000102 void VisitCXXDefaultArgExpr(CXXDefaultArgExpr *DAE) {
103 Visit(DAE->getExpr());
104 }
Anders Carlsson1619a5042009-05-03 17:47:16 +0000105 void VisitCXXConstructExpr(const CXXConstructExpr *E);
Anders Carlssonc82b86d2009-05-19 04:48:36 +0000106 void VisitCXXExprWithTemporaries(CXXExprWithTemporaries *E);
107
Eli Friedman21911e82008-05-27 15:51:49 +0000108 void VisitVAArgExpr(VAArgExpr *E);
Chris Lattner579a05d2008-04-04 18:42:16 +0000109
110 void EmitInitializationToLValue(Expr *E, LValue Address);
111 void EmitNullInitializationToLValue(LValue Address, QualType T);
Chris Lattner4758b402007-08-21 04:25:47 +0000112 // case Expr::ChooseExprClass:
Lauro Ramos Venanciodec89732008-02-18 22:44:02 +0000113
Chris Lattner4758b402007-08-21 04:25:47 +0000114};
115} // end anonymous namespace.
116
Chris Lattner835635d2007-08-21 04:59:27 +0000117//===----------------------------------------------------------------------===//
118// Utilities
119//===----------------------------------------------------------------------===//
Chris Lattner4758b402007-08-21 04:25:47 +0000120
Chris Lattner6278e6a2007-08-11 00:04:45 +0000121/// EmitAggLoadOfLValue - Given an expression with aggregate type that
122/// represents a value lvalue, this method emits the address of the lvalue,
123/// then loads the result into DestPtr.
Chris Lattner4758b402007-08-21 04:25:47 +0000124void AggExprEmitter::EmitAggLoadOfLValue(const Expr *E) {
125 LValue LV = CGF.EmitLValue(E);
Mike Stumpca9fc092009-05-23 20:28:01 +0000126 EmitFinalDestCopy(E, LV);
127}
128
129/// EmitFinalDestCopy - Perform the final copy to DestPtr, if desired.
130void AggExprEmitter::EmitFinalDestCopy(const Expr *E, RValue Src) {
131 assert(Src.isAggregate() && "value must be aggregate value!");
132
Chris Lattner6278e6a2007-08-11 00:04:45 +0000133 // If the result is ignored, don't copy from the value.
134 if (DestPtr == 0)
135 // FIXME: If the source is volatile, we must read from it.
136 return;
137
Mike Stumpca9fc092009-05-23 20:28:01 +0000138 // If the result of the assignment is used, copy the LHS there also.
139 // FIXME: Pass VolatileDest as well. I think we also need to merge volatile
140 // from the source as well, as we can't eliminate it if either operand
141 // is volatile, unless copy has volatile for both source and destination..
142 CGF.EmitAggregateCopy(DestPtr, Src.getAggregateAddr(), E->getType());
143}
144
145/// EmitFinalDestCopy - Perform the final copy to DestPtr, if desired.
146void AggExprEmitter::EmitFinalDestCopy(const Expr *E, LValue Src) {
147 assert(Src.isSimple() && "Can't have aggregate bitfield, vector, etc");
148
149 EmitFinalDestCopy(E, RValue::getAggregate(Src.getAddress(),
150 Src.isVolatileQualified()));
Chris Lattner6278e6a2007-08-11 00:04:45 +0000151}
152
Chris Lattner835635d2007-08-21 04:59:27 +0000153//===----------------------------------------------------------------------===//
154// Visitor Methods
155//===----------------------------------------------------------------------===//
156
Nuno Lopes7ffcf932009-01-15 20:14:33 +0000157void AggExprEmitter::VisitCStyleCastExpr(CStyleCastExpr *E) {
158 // GCC union extension
159 if (E->getType()->isUnionType()) {
160 RecordDecl *SD = E->getType()->getAsRecordType()->getDecl();
Douglas Gregorbcced4e2009-04-09 21:40:53 +0000161 LValue FieldLoc = CGF.EmitLValueForField(DestPtr,
162 *SD->field_begin(CGF.getContext()),
163 true, 0);
Nuno Lopes7ffcf932009-01-15 20:14:33 +0000164 EmitInitializationToLValue(E->getSubExpr(), FieldLoc);
165 return;
166 }
167
168 Visit(E->getSubExpr());
169}
170
Chris Lattner0f398c42008-07-26 22:37:01 +0000171void AggExprEmitter::VisitImplicitCastExpr(ImplicitCastExpr *E) {
Eli Friedmanf54c4e52008-02-11 01:09:17 +0000172 assert(CGF.getContext().typesAreCompatible(
Chris Lattner0f398c42008-07-26 22:37:01 +0000173 E->getSubExpr()->getType().getUnqualifiedType(),
174 E->getType().getUnqualifiedType()) &&
175 "Implicit cast types must be compatible");
Anders Carlsson1ba25ca2008-01-14 06:28:57 +0000176 Visit(E->getSubExpr());
177}
178
Chris Lattner0f398c42008-07-26 22:37:01 +0000179void AggExprEmitter::VisitCallExpr(const CallExpr *E) {
Anders Carlsson0370eb22007-10-31 22:04:46 +0000180 RValue RV = CGF.EmitCallExpr(E);
Mike Stumpca9fc092009-05-23 20:28:01 +0000181 EmitFinalDestCopy(E, RV);
Anders Carlsson0370eb22007-10-31 22:04:46 +0000182}
Chris Lattner0f398c42008-07-26 22:37:01 +0000183
184void AggExprEmitter::VisitObjCMessageExpr(ObjCMessageExpr *E) {
Daniel Dunbar97db84c2008-08-23 03:46:30 +0000185 RValue RV = CGF.EmitObjCMessageExpr(E);
Mike Stumpca9fc092009-05-23 20:28:01 +0000186 EmitFinalDestCopy(E, RV);
Chris Lattnerb1d329d2008-06-24 17:04:18 +0000187}
Anders Carlsson0370eb22007-10-31 22:04:46 +0000188
Daniel Dunbar55310df2008-08-27 06:57:25 +0000189void AggExprEmitter::VisitObjCPropertyRefExpr(ObjCPropertyRefExpr *E) {
190 RValue RV = CGF.EmitObjCPropertyGet(E);
Mike Stumpca9fc092009-05-23 20:28:01 +0000191 EmitFinalDestCopy(E, RV);
Daniel Dunbar55310df2008-08-27 06:57:25 +0000192}
193
Fariborz Jahanian8a1810f2008-11-22 18:39:36 +0000194void AggExprEmitter::VisitObjCKVCRefExpr(ObjCKVCRefExpr *E) {
195 RValue RV = CGF.EmitObjCPropertyGet(E);
Mike Stumpca9fc092009-05-23 20:28:01 +0000196 EmitFinalDestCopy(E, RV);
Fariborz Jahanian8a1810f2008-11-22 18:39:36 +0000197}
198
Chris Lattner0f398c42008-07-26 22:37:01 +0000199void AggExprEmitter::VisitBinComma(const BinaryOperator *E) {
Mike Stump23abd462009-05-23 21:40:07 +0000200 CGF.EmitAnyExprToTemp(E->getLHS(), 0, VolatileDest);
201 CGF.EmitAggExpr(E->getRHS(), DestPtr, VolatileDest);
Eli Friedman4b0e2a32008-05-20 07:56:31 +0000202}
203
Chris Lattner49e3bfa2007-08-31 22:54:14 +0000204void AggExprEmitter::VisitStmtExpr(const StmtExpr *E) {
205 CGF.EmitCompoundStmt(*E->getSubStmt(), true, DestPtr, VolatileDest);
206}
207
Chris Lattner4758b402007-08-21 04:25:47 +0000208void AggExprEmitter::VisitBinaryOperator(const BinaryOperator *E) {
Daniel Dunbara7c8cf62008-08-16 00:56:44 +0000209 CGF.ErrorUnsupported(E, "aggregate binary expression");
Chris Lattner835635d2007-08-21 04:59:27 +0000210}
211
Chris Lattnercd9fb242007-08-21 04:43:17 +0000212void AggExprEmitter::VisitBinAssign(const BinaryOperator *E) {
Eli Friedmanf54c4e52008-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 Lattner4758b402007-08-21 04:25:47 +0000219 LValue LHS = CGF.EmitLValue(E->getLHS());
Chris Lattner6278e6a2007-08-11 00:04:45 +0000220
Daniel Dunbar4b8c6db2008-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 Jahanian9ac53512008-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 Dunbar4b8c6db2008-08-30 05:35:15 +0000240 } else {
241 // Codegen the RHS so that it stores directly into the LHS.
Mike Stump86736572009-05-23 04:13:59 +0000242 CGF.EmitAggExpr(E->getRHS(), LHS.getAddress(), LHS.isVolatileQualified());
Mike Stumpca9fc092009-05-23 20:28:01 +0000243 EmitFinalDestCopy(E, LHS);
Daniel Dunbar4b8c6db2008-08-30 05:35:15 +0000244 }
Chris Lattner6278e6a2007-08-11 00:04:45 +0000245}
246
Chris Lattner4758b402007-08-21 04:25:47 +0000247void AggExprEmitter::VisitConditionalOperator(const ConditionalOperator *E) {
Daniel Dunbara612e792008-11-13 01:38:36 +0000248 llvm::BasicBlock *LHSBlock = CGF.createBasicBlock("cond.true");
249 llvm::BasicBlock *RHSBlock = CGF.createBasicBlock("cond.false");
250 llvm::BasicBlock *ContBlock = CGF.createBasicBlock("cond.end");
Chris Lattner6278e6a2007-08-11 00:04:45 +0000251
Chris Lattner4758b402007-08-21 04:25:47 +0000252 llvm::Value *Cond = CGF.EvaluateExprAsBool(E->getCond());
Chris Lattnerbda69f82007-08-26 23:13:56 +0000253 Builder.CreateCondBr(Cond, LHSBlock, RHSBlock);
Chris Lattner6278e6a2007-08-11 00:04:45 +0000254
Chris Lattner4758b402007-08-21 04:25:47 +0000255 CGF.EmitBlock(LHSBlock);
Chris Lattner6278e6a2007-08-11 00:04:45 +0000256
257 // Handle the GNU extension for missing LHS.
258 assert(E->getLHS() && "Must have LHS for aggregate value");
259
Chris Lattner926792f2007-08-21 05:02:10 +0000260 Visit(E->getLHS());
Daniel Dunbarc56e6762008-11-11 09:41:28 +0000261 CGF.EmitBranch(ContBlock);
Chris Lattner6278e6a2007-08-11 00:04:45 +0000262
Chris Lattner4758b402007-08-21 04:25:47 +0000263 CGF.EmitBlock(RHSBlock);
Chris Lattner6278e6a2007-08-11 00:04:45 +0000264
Chris Lattner926792f2007-08-21 05:02:10 +0000265 Visit(E->getRHS());
Daniel Dunbarc56e6762008-11-11 09:41:28 +0000266 CGF.EmitBranch(ContBlock);
Chris Lattner6278e6a2007-08-11 00:04:45 +0000267
Chris Lattner4758b402007-08-21 04:25:47 +0000268 CGF.EmitBlock(ContBlock);
Chris Lattner6278e6a2007-08-11 00:04:45 +0000269}
Chris Lattner835635d2007-08-21 04:59:27 +0000270
Eli Friedman21911e82008-05-27 15:51:49 +0000271void AggExprEmitter::VisitVAArgExpr(VAArgExpr *VE) {
Daniel Dunbare9fcadd22009-02-11 22:25:55 +0000272 llvm::Value *ArgValue = CGF.EmitVAListRef(VE->getSubExpr());
Anders Carlsson13abd7e2008-11-04 05:30:00 +0000273 llvm::Value *ArgPtr = CGF.EmitVAArg(ArgValue, VE->getType());
274
Sebastian Redl020cddc2009-01-09 21:09:38 +0000275 if (!ArgPtr) {
Anders Carlsson13abd7e2008-11-04 05:30:00 +0000276 CGF.ErrorUnsupported(VE, "aggregate va_arg expression");
Sebastian Redl020cddc2009-01-09 21:09:38 +0000277 return;
278 }
279
Mike Stumpca9fc092009-05-23 20:28:01 +0000280 EmitFinalDestCopy(VE, LValue::MakeAddr(ArgPtr, 0));
Eli Friedman21911e82008-05-27 15:51:49 +0000281}
282
Anders Carlssonb7f8f592009-04-17 00:06:03 +0000283void
Anders Carlsson1619a5042009-05-03 17:47:16 +0000284AggExprEmitter::VisitCXXConstructExpr(const CXXConstructExpr *E) {
Anders Carlssonc82b86d2009-05-19 04:48:36 +0000285 llvm::Value *V = DestPtr;
Anders Carlssonb7f8f592009-04-17 00:06:03 +0000286
Anders Carlssonc82b86d2009-05-19 04:48:36 +0000287 if (!V) {
288 assert(isa<CXXTempVarDecl>(E->getVarDecl()) &&
289 "Must have a temp var decl when there's no destination!");
290
291 V = CGF.CreateTempAlloca(CGF.ConvertType(E->getVarDecl()->getType()),
292 "tmpvar");
293 }
294
295 CGF.EmitCXXConstructExpr(V, E);
296}
297
298void AggExprEmitter::VisitCXXExprWithTemporaries(CXXExprWithTemporaries *E) {
299 // FIXME: Do something with the temporaries!
300 Visit(E->getSubExpr());
Anders Carlssonb7f8f592009-04-17 00:06:03 +0000301}
302
Chris Lattner579a05d2008-04-04 18:42:16 +0000303void AggExprEmitter::EmitInitializationToLValue(Expr* E, LValue LV) {
304 // FIXME: Are initializers affected by volatile?
Douglas Gregor0202cb42009-01-29 17:44:32 +0000305 if (isa<ImplicitValueInitExpr>(E)) {
Douglas Gregor347f7ea2009-01-28 21:54:33 +0000306 EmitNullInitializationToLValue(LV, E->getType());
Douglas Gregor0202cb42009-01-29 17:44:32 +0000307 } else if (E->getType()->isComplexType()) {
308 CGF.EmitComplexExprIntoAddr(E, LV.getAddress(), false);
Eli Friedman6e313212008-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 Lattner579a05d2008-04-04 18:42:16 +0000313 }
Chris Lattner579a05d2008-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 Dunbare8bdce42008-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 Venancioe2162c62008-02-19 19:27:31 +0000321 } else {
Chris Lattner579a05d2008-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 Friedman20bb5e02009-04-13 21:47:26 +0000325 // FIXME: That isn't true for member pointers!
Chris Lattner579a05d2008-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 Friedman9127aa12009-03-28 03:10:45 +0000329 CGF.EmitMemSetToZero(LV.getAddress(), T);
Chris Lattner579a05d2008-04-04 18:42:16 +0000330 }
331}
332
Chris Lattner579a05d2008-04-04 18:42:16 +0000333void AggExprEmitter::VisitInitListExpr(InitListExpr *E) {
Eli Friedmanf5d08c92008-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 Stump18bb9282009-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 Friedman7139af42009-01-25 02:32:41 +0000343 if (E->isConstantInitializer(CGF.getContext(), 0)) {
Eli Friedmanc59bb482008-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);
Mike Stumpca9fc092009-05-23 20:28:01 +0000349 EmitFinalDestCopy(E, LValue::MakeAddr(GV, 0));
Eli Friedmanc59bb482008-11-30 02:11:09 +0000350 return;
351 }
Eli Friedmanf5d08c92008-12-02 01:17:45 +0000352#endif
Douglas Gregorbf7207a2009-01-29 19:42:23 +0000353 if (E->hadArrayRangeDesignator()) {
354 CGF.ErrorUnsupported(E, "GNU array range designator extension");
355 }
356
Chris Lattner579a05d2008-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 Friedmanf23b6fa2008-05-19 17:51:16 +0000365
Chris Lattner0f398c42008-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 Friedmanf23b6fa2008-05-19 17:51:16 +0000374 }
375
Chris Lattner579a05d2008-04-04 18:42:16 +0000376 uint64_t NumArrayElements = AType->getNumElements();
Chris Lattner7adf0762008-08-04 07:31:14 +0000377 QualType ElementType = CGF.getContext().getCanonicalType(E->getType());
Douglas Gregor347f7ea2009-01-28 21:54:33 +0000378 ElementType = CGF.getContext().getAsArrayType(ElementType)->getElementType();
Chris Lattner579a05d2008-04-04 18:42:16 +0000379
Chris Lattner7adf0762008-08-04 07:31:14 +0000380 unsigned CVRqualifier = ElementType.getCVRQualifiers();
Eli Friedman327944b2008-06-13 23:01:12 +0000381
Chris Lattner579a05d2008-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 Friedman327944b2008-06-13 23:01:12 +0000385 EmitInitializationToLValue(E->getInit(i),
386 LValue::MakeAddr(NextVal, CVRqualifier));
Chris Lattner579a05d2008-04-04 18:42:16 +0000387 else
Eli Friedman327944b2008-06-13 23:01:12 +0000388 EmitNullInitializationToLValue(LValue::MakeAddr(NextVal, CVRqualifier),
Chris Lattner579a05d2008-04-04 18:42:16 +0000389 ElementType);
Lauro Ramos Venancioe2162c62008-02-19 19:27:31 +0000390 }
Chris Lattner579a05d2008-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 Lattner579a05d2008-04-04 18:42:16 +0000402 unsigned CurInitVal = 0;
Douglas Gregor51695702009-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 Gregorbcced4e2009-04-09 21:40:53 +0000413 for (RecordDecl::field_iterator Field = SD->field_begin(CGF.getContext()),
414 FieldEnd = SD->field_end(CGF.getContext());
Douglas Gregor51695702009-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 Lattner579a05d2008-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 Gregorbcced4e2009-04-09 21:40:53 +0000438 for (RecordDecl::field_iterator Field = SD->field_begin(CGF.getContext()),
439 FieldEnd = SD->field_end(CGF.getContext());
Douglas Gregor91f84212008-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 Gregor17bd0942009-01-28 23:36:17 +0000445 if (Field->isUnnamedBitfield())
Chris Lattner579a05d2008-04-04 18:42:16 +0000446 continue;
Douglas Gregor17bd0942009-01-28 23:36:17 +0000447
Eli Friedman327944b2008-06-13 23:01:12 +0000448 // FIXME: volatility
Douglas Gregor51695702009-01-29 16:53:55 +0000449 LValue FieldLoc = CGF.EmitLValueForField(DestPtr, *Field, false, 0);
Chris Lattner579a05d2008-04-04 18:42:16 +0000450 if (CurInitVal < NumInitElements) {
451 // Store the initializer into the field
Chris Lattner579a05d2008-04-04 18:42:16 +0000452 EmitInitializationToLValue(E->getInit(CurInitVal++), FieldLoc);
453 } else {
454 // We're out of initalizers; default-initialize to null
Douglas Gregor91f84212008-12-11 16:49:14 +0000455 EmitNullInitializationToLValue(FieldLoc, Field->getType());
Chris Lattner579a05d2008-04-04 18:42:16 +0000456 }
Lauro Ramos Venancioe2162c62008-02-19 19:27:31 +0000457 }
Devang Patel87174172007-10-26 17:44:44 +0000458}
459
Chris Lattner835635d2007-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 Dunbar0bc8e862008-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 Lattnerca05dfe2009-02-28 18:31:01 +0000485 // Aggregate assignment turns into llvm.memcpy. This is almost valid per
Chris Lattner3ef668c2009-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 Lattnerca05dfe2009-02-28 18:31:01 +0000491 // memcpy is not defined if the source and destination pointers are exactly
Chris Lattner3ef668c2009-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 Dunbar0bc8e862008-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
Mike Stump86736572009-05-23 04:13:59 +0000507 // FIXME: If we have a volatile struct, the optimizer can remove what might
508 // appear to be `extra' memory ops:
509 //
510 // volatile struct { int i; } a, b;
511 //
512 // int main() {
513 // a = b;
514 // a = b;
515 // }
516 //
517 // either, we need to use a differnt call here, or the backend needs to be
518 // taught to not do this.
Chris Lattner3ef668c2009-02-28 18:18:58 +0000519 Builder.CreateCall4(CGM.getMemCpyFn(),
Daniel Dunbar0bc8e862008-09-09 20:49:46 +0000520 DestPtr, SrcPtr,
521 // TypeInfo.first describes size in bits.
522 llvm::ConstantInt::get(IntPtr, TypeInfo.first/8),
523 llvm::ConstantInt::get(llvm::Type::Int32Ty,
524 TypeInfo.second/8));
525}