blob: 901f867a5913f2d5585a8e5aca4644718b9c93c5 [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"
Fariborz Jahanian082b02e2009-07-08 01:18:33 +000016#include "CGObjCRuntime.h"
Daniel Dunbarde7fb842008-08-11 05:00:27 +000017#include "clang/AST/ASTContext.h"
Anders Carlssonb14095a2009-04-17 00:06:03 +000018#include "clang/AST/DeclCXX.h"
Daniel Dunbarde7fb842008-08-11 05:00:27 +000019#include "clang/AST/StmtVisitor.h"
Chris Lattner883f6a72007-08-11 00:04:45 +000020#include "llvm/Constants.h"
21#include "llvm/Function.h"
Devang Patel636c3d02007-10-26 17:44:44 +000022#include "llvm/GlobalVariable.h"
Chris Lattner9c033562007-08-21 04:25:47 +000023#include "llvm/Support/Compiler.h"
Chris Lattnerf81557c2008-04-04 18:42:16 +000024#include "llvm/Intrinsics.h"
Chris Lattneraf6f5282007-08-10 20:13:28 +000025using namespace clang;
26using namespace CodeGen;
Chris Lattner883f6a72007-08-11 00:04:45 +000027
Chris Lattner9c033562007-08-21 04:25:47 +000028//===----------------------------------------------------------------------===//
29// Aggregate Expression Emitter
30//===----------------------------------------------------------------------===//
31
32namespace {
33class VISIBILITY_HIDDEN AggExprEmitter : public StmtVisitor<AggExprEmitter> {
34 CodeGenFunction &CGF;
Daniel Dunbar45d196b2008-11-01 01:53:16 +000035 CGBuilderTy &Builder;
Chris Lattner9c033562007-08-21 04:25:47 +000036 llvm::Value *DestPtr;
37 bool VolatileDest;
Mike Stump49d1cd52009-05-26 22:03:21 +000038 bool IgnoreResult;
Anders Carlsson14c5cbf2009-08-16 07:36:22 +000039 bool IsInitializer;
Fariborz Jahanian08c32132009-08-31 19:33:16 +000040 bool RequiresGCollection;
Chris Lattner9c033562007-08-21 04:25:47 +000041public:
Mike Stumpff4bf3b2009-05-27 01:42:21 +000042 AggExprEmitter(CodeGenFunction &cgf, llvm::Value *destPtr, bool v,
Fariborz Jahanian08c32132009-08-31 19:33:16 +000043 bool ignore, bool isinit, bool requiresGCollection)
Chris Lattnerbfc0c1a2007-08-26 23:13:56 +000044 : CGF(cgf), Builder(CGF.Builder),
Anders Carlsson14c5cbf2009-08-16 07:36:22 +000045 DestPtr(destPtr), VolatileDest(v), IgnoreResult(ignore),
Fariborz Jahanian08c32132009-08-31 19:33:16 +000046 IsInitializer(isinit), RequiresGCollection(requiresGCollection) {
Chris Lattner9c033562007-08-21 04:25:47 +000047 }
48
Chris Lattneree755f92007-08-21 04:59:27 +000049 //===--------------------------------------------------------------------===//
50 // Utilities
51 //===--------------------------------------------------------------------===//
52
Chris Lattner9c033562007-08-21 04:25:47 +000053 /// EmitAggLoadOfLValue - Given an expression with aggregate type that
54 /// represents a value lvalue, this method emits the address of the lvalue,
55 /// then loads the result into DestPtr.
56 void EmitAggLoadOfLValue(const Expr *E);
Eli Friedman922696f2008-05-19 17:51:16 +000057
Mike Stump4ac20dd2009-05-23 20:28:01 +000058 /// EmitFinalDestCopy - Perform the final copy to DestPtr, if desired.
Mike Stump49d1cd52009-05-26 22:03:21 +000059 void EmitFinalDestCopy(const Expr *E, LValue Src, bool Ignore = false);
60 void EmitFinalDestCopy(const Expr *E, RValue Src, bool Ignore = false);
Mike Stump4ac20dd2009-05-23 20:28:01 +000061
Chris Lattneree755f92007-08-21 04:59:27 +000062 //===--------------------------------------------------------------------===//
63 // Visitor Methods
64 //===--------------------------------------------------------------------===//
Mike Stump1eb44332009-09-09 15:08:12 +000065
Chris Lattner9c033562007-08-21 04:25:47 +000066 void VisitStmt(Stmt *S) {
Daniel Dunbar488e9932008-08-16 00:56:44 +000067 CGF.ErrorUnsupported(S, "aggregate expression");
Chris Lattner9c033562007-08-21 04:25:47 +000068 }
69 void VisitParenExpr(ParenExpr *PE) { Visit(PE->getSubExpr()); }
Eli Friedman12444a22009-01-27 09:03:41 +000070 void VisitUnaryExtension(UnaryOperator *E) { Visit(E->getSubExpr()); }
Chris Lattner9c033562007-08-21 04:25:47 +000071
72 // l-values.
Seo Sanghyeon9b73b392007-12-14 02:04:12 +000073 void VisitDeclRefExpr(DeclRefExpr *DRE) { EmitAggLoadOfLValue(DRE); }
74 void VisitMemberExpr(MemberExpr *ME) { EmitAggLoadOfLValue(ME); }
75 void VisitUnaryDeref(UnaryOperator *E) { EmitAggLoadOfLValue(E); }
Seo Sanghyeonad6ebd62007-12-23 03:11:58 +000076 void VisitStringLiteral(StringLiteral *E) { EmitAggLoadOfLValue(E); }
Chris Lattnerf0a990c2009-04-21 23:00:09 +000077 void VisitCompoundLiteralExpr(CompoundLiteralExpr *E) {
Mike Stump1eb44332009-09-09 15:08:12 +000078 EmitAggLoadOfLValue(E);
Chris Lattnerf0a990c2009-04-21 23:00:09 +000079 }
Seo Sanghyeon9b73b392007-12-14 02:04:12 +000080 void VisitArraySubscriptExpr(ArraySubscriptExpr *E) {
81 EmitAggLoadOfLValue(E);
82 }
Chris Lattnerf0a990c2009-04-21 23:00:09 +000083 void VisitBlockDeclRefExpr(const BlockDeclRefExpr *E) {
Mike Stump1eb44332009-09-09 15:08:12 +000084 EmitAggLoadOfLValue(E);
Chris Lattnerf0a990c2009-04-21 23:00:09 +000085 }
86 void VisitPredefinedExpr(const PredefinedExpr *E) {
Mike Stump1eb44332009-09-09 15:08:12 +000087 EmitAggLoadOfLValue(E);
Chris Lattnerf0a990c2009-04-21 23:00:09 +000088 }
Mike Stump1eb44332009-09-09 15:08:12 +000089
Chris Lattner9c033562007-08-21 04:25:47 +000090 // Operators.
Anders Carlsson4d8673b2009-08-07 23:22:37 +000091 void VisitCastExpr(CastExpr *E);
Anders Carlsson148fe672007-10-31 22:04:46 +000092 void VisitCallExpr(const CallExpr *E);
Chris Lattnerb2d963f2007-08-31 22:54:14 +000093 void VisitStmtExpr(const StmtExpr *E);
Chris Lattner9c033562007-08-21 04:25:47 +000094 void VisitBinaryOperator(const BinaryOperator *BO);
Fariborz Jahanian8bfd31f2009-10-22 22:57:31 +000095 void VisitPointerToDataMemberBinaryOperator(const BinaryOperator *BO);
Chris Lattner03d6fb92007-08-21 04:43:17 +000096 void VisitBinAssign(const BinaryOperator *E);
Eli Friedman07fa52a2008-05-20 07:56:31 +000097 void VisitBinComma(const BinaryOperator *E);
Anders Carlssona024d172009-10-03 15:43:24 +000098 void VisitUnaryAddrOf(const UnaryOperator *E);
Chris Lattner9c033562007-08-21 04:25:47 +000099
Chris Lattner8fdf3282008-06-24 17:04:18 +0000100 void VisitObjCMessageExpr(ObjCMessageExpr *E);
Daniel Dunbar0a04d772008-08-23 10:51:21 +0000101 void VisitObjCIvarRefExpr(ObjCIvarRefExpr *E) {
102 EmitAggLoadOfLValue(E);
103 }
Daniel Dunbar9c3fc702008-08-27 06:57:25 +0000104 void VisitObjCPropertyRefExpr(ObjCPropertyRefExpr *E);
Fariborz Jahanian09105f52009-08-20 17:02:02 +0000105 void VisitObjCImplicitSetterGetterRefExpr(ObjCImplicitSetterGetterRefExpr *E);
Mike Stump1eb44332009-09-09 15:08:12 +0000106
Chris Lattner9c033562007-08-21 04:25:47 +0000107 void VisitConditionalOperator(const ConditionalOperator *CO);
Anders Carlssona294ca82009-07-08 18:33:14 +0000108 void VisitChooseExpr(const ChooseExpr *CE);
Devang Patel636c3d02007-10-26 17:44:44 +0000109 void VisitInitListExpr(InitListExpr *E);
Chris Lattner04421082008-04-08 04:40:51 +0000110 void VisitCXXDefaultArgExpr(CXXDefaultArgExpr *DAE) {
111 Visit(DAE->getExpr());
112 }
Anders Carlssonb58d0172009-05-30 23:23:33 +0000113 void VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr *E);
Anders Carlsson31ccf372009-05-03 17:47:16 +0000114 void VisitCXXConstructExpr(const CXXConstructExpr *E);
Anders Carlsson7f6ad152009-05-19 04:48:36 +0000115 void VisitCXXExprWithTemporaries(CXXExprWithTemporaries *E);
Nuno Lopes329763b2009-10-18 15:18:11 +0000116 void VisitCXXZeroInitValueExpr(CXXZeroInitValueExpr *E);
Anders Carlsson7f6ad152009-05-19 04:48:36 +0000117
Eli Friedmanb1851242008-05-27 15:51:49 +0000118 void VisitVAArgExpr(VAArgExpr *E);
Chris Lattnerf81557c2008-04-04 18:42:16 +0000119
120 void EmitInitializationToLValue(Expr *E, LValue Address);
121 void EmitNullInitializationToLValue(LValue Address, QualType T);
Chris Lattner9c033562007-08-21 04:25:47 +0000122 // case Expr::ChooseExprClass:
Lauro Ramos Venancio305762c2008-02-18 22:44:02 +0000123
Chris Lattner9c033562007-08-21 04:25:47 +0000124};
125} // end anonymous namespace.
126
Chris Lattneree755f92007-08-21 04:59:27 +0000127//===----------------------------------------------------------------------===//
128// Utilities
129//===----------------------------------------------------------------------===//
Chris Lattner9c033562007-08-21 04:25:47 +0000130
Chris Lattner883f6a72007-08-11 00:04:45 +0000131/// EmitAggLoadOfLValue - Given an expression with aggregate type that
132/// represents a value lvalue, this method emits the address of the lvalue,
133/// then loads the result into DestPtr.
Chris Lattner9c033562007-08-21 04:25:47 +0000134void AggExprEmitter::EmitAggLoadOfLValue(const Expr *E) {
135 LValue LV = CGF.EmitLValue(E);
Mike Stump4ac20dd2009-05-23 20:28:01 +0000136 EmitFinalDestCopy(E, LV);
137}
138
139/// EmitFinalDestCopy - Perform the final copy to DestPtr, if desired.
Mike Stump49d1cd52009-05-26 22:03:21 +0000140void AggExprEmitter::EmitFinalDestCopy(const Expr *E, RValue Src, bool Ignore) {
Mike Stump4ac20dd2009-05-23 20:28:01 +0000141 assert(Src.isAggregate() && "value must be aggregate value!");
142
Chris Lattner883f6a72007-08-11 00:04:45 +0000143 // If the result is ignored, don't copy from the value.
Mike Stump9ccb1032009-05-23 22:01:27 +0000144 if (DestPtr == 0) {
Mike Stump49d1cd52009-05-26 22:03:21 +0000145 if (!Src.isVolatileQualified() || (IgnoreResult && Ignore))
Mike Stump9ccb1032009-05-23 22:01:27 +0000146 return;
Mike Stump49d1cd52009-05-26 22:03:21 +0000147 // If the source is volatile, we must read from it; to do that, we need
148 // some place to put it.
149 DestPtr = CGF.CreateTempAlloca(CGF.ConvertType(E->getType()), "agg.tmp");
Mike Stump9ccb1032009-05-23 22:01:27 +0000150 }
Chris Lattner883f6a72007-08-11 00:04:45 +0000151
Fariborz Jahanian08c32132009-08-31 19:33:16 +0000152 if (RequiresGCollection) {
153 CGF.CGM.getObjCRuntime().EmitGCMemmoveCollectable(CGF,
154 DestPtr, Src.getAggregateAddr(),
155 E->getType());
156 return;
157 }
Mike Stump4ac20dd2009-05-23 20:28:01 +0000158 // If the result of the assignment is used, copy the LHS there also.
159 // FIXME: Pass VolatileDest as well. I think we also need to merge volatile
160 // from the source as well, as we can't eliminate it if either operand
161 // is volatile, unless copy has volatile for both source and destination..
Mike Stump27fe2e62009-05-23 22:29:41 +0000162 CGF.EmitAggregateCopy(DestPtr, Src.getAggregateAddr(), E->getType(),
163 VolatileDest|Src.isVolatileQualified());
Mike Stump4ac20dd2009-05-23 20:28:01 +0000164}
165
166/// EmitFinalDestCopy - Perform the final copy to DestPtr, if desired.
Mike Stump49d1cd52009-05-26 22:03:21 +0000167void AggExprEmitter::EmitFinalDestCopy(const Expr *E, LValue Src, bool Ignore) {
Mike Stump4ac20dd2009-05-23 20:28:01 +0000168 assert(Src.isSimple() && "Can't have aggregate bitfield, vector, etc");
169
170 EmitFinalDestCopy(E, RValue::getAggregate(Src.getAddress(),
Mike Stump49d1cd52009-05-26 22:03:21 +0000171 Src.isVolatileQualified()),
172 Ignore);
Chris Lattner883f6a72007-08-11 00:04:45 +0000173}
174
Chris Lattneree755f92007-08-21 04:59:27 +0000175//===----------------------------------------------------------------------===//
176// Visitor Methods
177//===----------------------------------------------------------------------===//
178
Anders Carlsson4d8673b2009-08-07 23:22:37 +0000179void AggExprEmitter::VisitCastExpr(CastExpr *E) {
Anders Carlsson30168422009-09-29 01:23:39 +0000180 switch (E->getCastKind()) {
181 default: assert(0 && "Unhandled cast kind!");
182
183 case CastExpr::CK_ToUnion: {
Anders Carlsson4d8673b2009-08-07 23:22:37 +0000184 // GCC union extension
Eli Friedman34ebf4d2009-06-03 20:45:06 +0000185 QualType PtrTy =
Anders Carlsson30168422009-09-29 01:23:39 +0000186 CGF.getContext().getPointerType(E->getSubExpr()->getType());
Eli Friedman34ebf4d2009-06-03 20:45:06 +0000187 llvm::Value *CastPtr = Builder.CreateBitCast(DestPtr,
188 CGF.ConvertType(PtrTy));
Mon P Wangc6a38a42009-07-22 03:08:17 +0000189 EmitInitializationToLValue(E->getSubExpr(),
John McCall0953e762009-09-24 19:53:00 +0000190 LValue::MakeAddr(CastPtr, Qualifiers()));
Anders Carlsson30168422009-09-29 01:23:39 +0000191 break;
Nuno Lopes7e916272009-01-15 20:14:33 +0000192 }
Mike Stump1eb44332009-09-09 15:08:12 +0000193
Anders Carlsson4d8673b2009-08-07 23:22:37 +0000194 // FIXME: Remove the CK_Unknown check here.
Anders Carlsson30168422009-09-29 01:23:39 +0000195 case CastExpr::CK_Unknown:
196 case CastExpr::CK_NoOp:
197 case CastExpr::CK_UserDefinedConversion:
198 case CastExpr::CK_ConstructorConversion:
199 assert(CGF.getContext().hasSameUnqualifiedType(E->getSubExpr()->getType(),
200 E->getType()) &&
201 "Implicit cast types must be compatible");
202 Visit(E->getSubExpr());
203 break;
Anders Carlssone9d34dc2009-09-29 02:09:01 +0000204
205 case CastExpr::CK_NullToMemberPointer: {
Anders Carlssone9d34dc2009-09-29 02:09:01 +0000206 const llvm::Type *PtrDiffTy =
207 CGF.ConvertType(CGF.getContext().getPointerDiffType());
208
209 llvm::Value *NullValue = llvm::Constant::getNullValue(PtrDiffTy);
210 llvm::Value *Ptr = Builder.CreateStructGEP(DestPtr, 0, "ptr");
211 Builder.CreateStore(NullValue, Ptr, VolatileDest);
212
213 llvm::Value *Adj = Builder.CreateStructGEP(DestPtr, 1, "adj");
214 Builder.CreateStore(NullValue, Adj, VolatileDest);
215
216 break;
217 }
Anders Carlsson84080ec2009-09-29 03:13:20 +0000218
Anders Carlssonbb378cb2009-10-18 20:31:03 +0000219 case CastExpr::CK_BitCast: {
220 // This must be a member function pointer cast.
221 Visit(E->getSubExpr());
222 break;
223 }
224
Anders Carlsson84080ec2009-09-29 03:13:20 +0000225 case CastExpr::CK_BaseToDerivedMemberPointer: {
226 QualType SrcType = E->getSubExpr()->getType();
227
228 llvm::Value *Src = CGF.CreateTempAlloca(CGF.ConvertTypeForMem(SrcType),
229 "tmp");
230 CGF.EmitAggExpr(E->getSubExpr(), Src, SrcType.isVolatileQualified());
231
232 llvm::Value *SrcPtr = Builder.CreateStructGEP(Src, 0, "src.ptr");
233 SrcPtr = Builder.CreateLoad(SrcPtr);
234
235 llvm::Value *SrcAdj = Builder.CreateStructGEP(Src, 1, "src.adj");
236 SrcAdj = Builder.CreateLoad(SrcAdj);
237
238 llvm::Value *DstPtr = Builder.CreateStructGEP(DestPtr, 0, "dst.ptr");
239 Builder.CreateStore(SrcPtr, DstPtr, VolatileDest);
240
241 llvm::Value *DstAdj = Builder.CreateStructGEP(DestPtr, 1, "dst.adj");
242
243 // Now See if we need to update the adjustment.
244 const CXXRecordDecl *SrcDecl =
245 cast<CXXRecordDecl>(SrcType->getAs<MemberPointerType>()->
246 getClass()->getAs<RecordType>()->getDecl());
247 const CXXRecordDecl *DstDecl =
248 cast<CXXRecordDecl>(E->getType()->getAs<MemberPointerType>()->
249 getClass()->getAs<RecordType>()->getDecl());
250
Anders Carlsson2b358352009-10-03 14:56:57 +0000251 llvm::Constant *Adj = CGF.CGM.GetCXXBaseClassOffset(DstDecl, SrcDecl);
Anders Carlsson84080ec2009-09-29 03:13:20 +0000252 if (Adj)
253 SrcAdj = Builder.CreateAdd(SrcAdj, Adj, "adj");
254
255 Builder.CreateStore(SrcAdj, DstAdj, VolatileDest);
256 break;
257 }
Anders Carlsson30168422009-09-29 01:23:39 +0000258 }
Anders Carlssone4707ff2008-01-14 06:28:57 +0000259}
260
Chris Lattner96196622008-07-26 22:37:01 +0000261void AggExprEmitter::VisitCallExpr(const CallExpr *E) {
Anders Carlssone70e8f72009-05-27 16:45:02 +0000262 if (E->getCallReturnType()->isReferenceType()) {
263 EmitAggLoadOfLValue(E);
264 return;
265 }
Mike Stump1eb44332009-09-09 15:08:12 +0000266
Anders Carlsson148fe672007-10-31 22:04:46 +0000267 RValue RV = CGF.EmitCallExpr(E);
Mike Stump4ac20dd2009-05-23 20:28:01 +0000268 EmitFinalDestCopy(E, RV);
Anders Carlsson148fe672007-10-31 22:04:46 +0000269}
Chris Lattner96196622008-07-26 22:37:01 +0000270
271void AggExprEmitter::VisitObjCMessageExpr(ObjCMessageExpr *E) {
Daniel Dunbar8f2926b2008-08-23 03:46:30 +0000272 RValue RV = CGF.EmitObjCMessageExpr(E);
Mike Stump4ac20dd2009-05-23 20:28:01 +0000273 EmitFinalDestCopy(E, RV);
Chris Lattner8fdf3282008-06-24 17:04:18 +0000274}
Anders Carlsson148fe672007-10-31 22:04:46 +0000275
Daniel Dunbar9c3fc702008-08-27 06:57:25 +0000276void AggExprEmitter::VisitObjCPropertyRefExpr(ObjCPropertyRefExpr *E) {
277 RValue RV = CGF.EmitObjCPropertyGet(E);
Mike Stump4ac20dd2009-05-23 20:28:01 +0000278 EmitFinalDestCopy(E, RV);
Daniel Dunbar9c3fc702008-08-27 06:57:25 +0000279}
280
Fariborz Jahanian09105f52009-08-20 17:02:02 +0000281void AggExprEmitter::VisitObjCImplicitSetterGetterRefExpr(
282 ObjCImplicitSetterGetterRefExpr *E) {
Fariborz Jahanian5daf5702008-11-22 18:39:36 +0000283 RValue RV = CGF.EmitObjCPropertyGet(E);
Mike Stump4ac20dd2009-05-23 20:28:01 +0000284 EmitFinalDestCopy(E, RV);
Fariborz Jahanian5daf5702008-11-22 18:39:36 +0000285}
286
Chris Lattner96196622008-07-26 22:37:01 +0000287void AggExprEmitter::VisitBinComma(const BinaryOperator *E) {
Mike Stump7f79f9b2009-05-29 15:46:01 +0000288 CGF.EmitAnyExpr(E->getLHS(), 0, false, true);
Anders Carlsson14c5cbf2009-08-16 07:36:22 +0000289 CGF.EmitAggExpr(E->getRHS(), DestPtr, VolatileDest,
290 /*IgnoreResult=*/false, IsInitializer);
Eli Friedman07fa52a2008-05-20 07:56:31 +0000291}
292
Anders Carlssona024d172009-10-03 15:43:24 +0000293void AggExprEmitter::VisitUnaryAddrOf(const UnaryOperator *E) {
294 // We have a member function pointer.
295 const MemberPointerType *MPT = E->getType()->getAs<MemberPointerType>();
Daniel Dunbarbabac132009-10-17 09:39:30 +0000296 (void) MPT;
Anders Carlssona024d172009-10-03 15:43:24 +0000297 assert(MPT->getPointeeType()->isFunctionProtoType() &&
298 "Unexpected member pointer type!");
299
Douglas Gregora2813ce2009-10-23 18:54:35 +0000300 const DeclRefExpr *DRE = cast<DeclRefExpr>(E->getSubExpr());
Anders Carlssona024d172009-10-03 15:43:24 +0000301 const CXXMethodDecl *MD = cast<CXXMethodDecl>(DRE->getDecl());
302
303 const llvm::Type *PtrDiffTy =
304 CGF.ConvertType(CGF.getContext().getPointerDiffType());
305
306 llvm::Value *DstPtr = Builder.CreateStructGEP(DestPtr, 0, "dst.ptr");
307 llvm::Value *FuncPtr;
308
309 if (MD->isVirtual()) {
Anders Carlssondbd920c2009-10-11 22:13:54 +0000310 int64_t Index =
311 CGF.CGM.getVtableInfo().getMethodVtableIndex(MD);
Anders Carlssona024d172009-10-03 15:43:24 +0000312
313 FuncPtr = llvm::ConstantInt::get(PtrDiffTy, Index + 1);
314 } else {
315 FuncPtr = llvm::ConstantExpr::getPtrToInt(CGF.CGM.GetAddrOfFunction(MD),
316 PtrDiffTy);
317 }
318 Builder.CreateStore(FuncPtr, DstPtr, VolatileDest);
319
320 llvm::Value *AdjPtr = Builder.CreateStructGEP(DestPtr, 1, "dst.adj");
321
322 // The adjustment will always be 0.
323 Builder.CreateStore(llvm::ConstantInt::get(PtrDiffTy, 0), AdjPtr,
324 VolatileDest);
325}
326
Chris Lattnerb2d963f2007-08-31 22:54:14 +0000327void AggExprEmitter::VisitStmtExpr(const StmtExpr *E) {
328 CGF.EmitCompoundStmt(*E->getSubStmt(), true, DestPtr, VolatileDest);
329}
330
Chris Lattner9c033562007-08-21 04:25:47 +0000331void AggExprEmitter::VisitBinaryOperator(const BinaryOperator *E) {
Fariborz Jahanian52f08bc2009-10-26 21:58:25 +0000332 if (E->getOpcode() == BinaryOperator::PtrMemD ||
333 E->getOpcode() == BinaryOperator::PtrMemI)
Fariborz Jahanian8bfd31f2009-10-22 22:57:31 +0000334 VisitPointerToDataMemberBinaryOperator(E);
335 else
336 CGF.ErrorUnsupported(E, "aggregate binary expression");
337}
338
339void AggExprEmitter::VisitPointerToDataMemberBinaryOperator(
340 const BinaryOperator *E) {
341 LValue LV = CGF.EmitPointerToDataMemberBinaryExpr(E);
342 EmitFinalDestCopy(E, LV);
Chris Lattneree755f92007-08-21 04:59:27 +0000343}
344
Chris Lattner03d6fb92007-08-21 04:43:17 +0000345void AggExprEmitter::VisitBinAssign(const BinaryOperator *E) {
Eli Friedmanff6e2b72008-02-11 01:09:17 +0000346 // For an assignment to work, the value on the right has
347 // to be compatible with the value on the left.
Eli Friedman2dce5f82009-05-28 23:04:00 +0000348 assert(CGF.getContext().hasSameUnqualifiedType(E->getLHS()->getType(),
349 E->getRHS()->getType())
Eli Friedmanff6e2b72008-02-11 01:09:17 +0000350 && "Invalid assignment");
Chris Lattner9c033562007-08-21 04:25:47 +0000351 LValue LHS = CGF.EmitLValue(E->getLHS());
Chris Lattner883f6a72007-08-11 00:04:45 +0000352
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000353 // We have to special case property setters, otherwise we must have
354 // a simple lvalue (no aggregates inside vectors, bitfields).
355 if (LHS.isPropertyRef()) {
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000356 llvm::Value *AggLoc = DestPtr;
357 if (!AggLoc)
358 AggLoc = CGF.CreateTempAlloca(CGF.ConvertType(E->getRHS()->getType()));
Mike Stump240993d2009-05-23 23:48:13 +0000359 CGF.EmitAggExpr(E->getRHS(), AggLoc, VolatileDest);
Mike Stump1eb44332009-09-09 15:08:12 +0000360 CGF.EmitObjCPropertySet(LHS.getPropertyRefExpr(),
Mike Stump240993d2009-05-23 23:48:13 +0000361 RValue::getAggregate(AggLoc, VolatileDest));
Mike Stumpb3589f42009-07-30 22:28:39 +0000362 } else if (LHS.isKVCRef()) {
Fariborz Jahanian43f44702008-11-22 22:30:21 +0000363 llvm::Value *AggLoc = DestPtr;
364 if (!AggLoc)
365 AggLoc = CGF.CreateTempAlloca(CGF.ConvertType(E->getRHS()->getType()));
Mike Stumpa49af1a2009-05-23 23:52:31 +0000366 CGF.EmitAggExpr(E->getRHS(), AggLoc, VolatileDest);
Mike Stump1eb44332009-09-09 15:08:12 +0000367 CGF.EmitObjCPropertySet(LHS.getKVCRefExpr(),
Mike Stumpa49af1a2009-05-23 23:52:31 +0000368 RValue::getAggregate(AggLoc, VolatileDest));
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000369 } else {
Fariborz Jahanian08c32132009-08-31 19:33:16 +0000370 bool RequiresGCollection = false;
Fariborz Jahanian082b02e2009-07-08 01:18:33 +0000371 if (CGF.getContext().getLangOptions().NeXTRuntime) {
372 QualType LHSTy = E->getLHS()->getType();
Ted Kremenek6217b802009-07-29 21:53:49 +0000373 if (const RecordType *FDTTy = LHSTy.getTypePtr()->getAs<RecordType>())
Mike Stump1eb44332009-09-09 15:08:12 +0000374 RequiresGCollection = FDTTy->getDecl()->hasObjectMember();
Fariborz Jahanian082b02e2009-07-08 01:18:33 +0000375 }
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000376 // Codegen the RHS so that it stores directly into the LHS.
Fariborz Jahanian08c32132009-08-31 19:33:16 +0000377 CGF.EmitAggExpr(E->getRHS(), LHS.getAddress(), LHS.isVolatileQualified(),
378 false, false, RequiresGCollection);
Mike Stump49d1cd52009-05-26 22:03:21 +0000379 EmitFinalDestCopy(E, LHS, true);
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000380 }
Chris Lattner883f6a72007-08-11 00:04:45 +0000381}
382
Chris Lattner9c033562007-08-21 04:25:47 +0000383void AggExprEmitter::VisitConditionalOperator(const ConditionalOperator *E) {
Daniel Dunbar9615ecb2008-11-13 01:38:36 +0000384 llvm::BasicBlock *LHSBlock = CGF.createBasicBlock("cond.true");
385 llvm::BasicBlock *RHSBlock = CGF.createBasicBlock("cond.false");
386 llvm::BasicBlock *ContBlock = CGF.createBasicBlock("cond.end");
Mike Stump1eb44332009-09-09 15:08:12 +0000387
Chris Lattner9c033562007-08-21 04:25:47 +0000388 llvm::Value *Cond = CGF.EvaluateExprAsBool(E->getCond());
Chris Lattnerbfc0c1a2007-08-26 23:13:56 +0000389 Builder.CreateCondBr(Cond, LHSBlock, RHSBlock);
Mike Stump1eb44332009-09-09 15:08:12 +0000390
Anders Carlssonfb6fa302009-06-04 03:00:32 +0000391 CGF.PushConditionalTempDestruction();
Chris Lattner9c033562007-08-21 04:25:47 +0000392 CGF.EmitBlock(LHSBlock);
Mike Stump1eb44332009-09-09 15:08:12 +0000393
Chris Lattner883f6a72007-08-11 00:04:45 +0000394 // Handle the GNU extension for missing LHS.
395 assert(E->getLHS() && "Must have LHS for aggregate value");
396
Chris Lattnerc748f272007-08-21 05:02:10 +0000397 Visit(E->getLHS());
Anders Carlssonfb6fa302009-06-04 03:00:32 +0000398 CGF.PopConditionalTempDestruction();
Daniel Dunbard57a8712008-11-11 09:41:28 +0000399 CGF.EmitBranch(ContBlock);
Mike Stump1eb44332009-09-09 15:08:12 +0000400
Anders Carlssonfb6fa302009-06-04 03:00:32 +0000401 CGF.PushConditionalTempDestruction();
Chris Lattner9c033562007-08-21 04:25:47 +0000402 CGF.EmitBlock(RHSBlock);
Mike Stump1eb44332009-09-09 15:08:12 +0000403
Chris Lattnerc748f272007-08-21 05:02:10 +0000404 Visit(E->getRHS());
Anders Carlssonfb6fa302009-06-04 03:00:32 +0000405 CGF.PopConditionalTempDestruction();
Daniel Dunbard57a8712008-11-11 09:41:28 +0000406 CGF.EmitBranch(ContBlock);
Mike Stump1eb44332009-09-09 15:08:12 +0000407
Chris Lattner9c033562007-08-21 04:25:47 +0000408 CGF.EmitBlock(ContBlock);
Chris Lattner883f6a72007-08-11 00:04:45 +0000409}
Chris Lattneree755f92007-08-21 04:59:27 +0000410
Anders Carlssona294ca82009-07-08 18:33:14 +0000411void AggExprEmitter::VisitChooseExpr(const ChooseExpr *CE) {
412 Visit(CE->getChosenSubExpr(CGF.getContext()));
413}
414
Eli Friedmanb1851242008-05-27 15:51:49 +0000415void AggExprEmitter::VisitVAArgExpr(VAArgExpr *VE) {
Daniel Dunbar07855702009-02-11 22:25:55 +0000416 llvm::Value *ArgValue = CGF.EmitVAListRef(VE->getSubExpr());
Anders Carlssonddf7cac2008-11-04 05:30:00 +0000417 llvm::Value *ArgPtr = CGF.EmitVAArg(ArgValue, VE->getType());
418
Sebastian Redl0262f022009-01-09 21:09:38 +0000419 if (!ArgPtr) {
Anders Carlssonddf7cac2008-11-04 05:30:00 +0000420 CGF.ErrorUnsupported(VE, "aggregate va_arg expression");
Sebastian Redl0262f022009-01-09 21:09:38 +0000421 return;
422 }
423
John McCall0953e762009-09-24 19:53:00 +0000424 EmitFinalDestCopy(VE, LValue::MakeAddr(ArgPtr, Qualifiers()));
Eli Friedmanb1851242008-05-27 15:51:49 +0000425}
426
Anders Carlssonb58d0172009-05-30 23:23:33 +0000427void AggExprEmitter::VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr *E) {
428 llvm::Value *Val = DestPtr;
Mike Stump1eb44332009-09-09 15:08:12 +0000429
Anders Carlssonb58d0172009-05-30 23:23:33 +0000430 if (!Val) {
431 // Create a temporary variable.
432 Val = CGF.CreateTempAlloca(CGF.ConvertTypeForMem(E->getType()), "tmp");
433
434 // FIXME: volatile
435 CGF.EmitAggExpr(E->getSubExpr(), Val, false);
Mike Stump1eb44332009-09-09 15:08:12 +0000436 } else
Anders Carlssonb58d0172009-05-30 23:23:33 +0000437 Visit(E->getSubExpr());
Mike Stump1eb44332009-09-09 15:08:12 +0000438
Anders Carlsson14c5cbf2009-08-16 07:36:22 +0000439 // Don't make this a live temporary if we're emitting an initializer expr.
440 if (!IsInitializer)
441 CGF.PushCXXTemporary(E->getTemporary(), Val);
Anders Carlssonb58d0172009-05-30 23:23:33 +0000442}
443
Anders Carlssonb14095a2009-04-17 00:06:03 +0000444void
Anders Carlsson31ccf372009-05-03 17:47:16 +0000445AggExprEmitter::VisitCXXConstructExpr(const CXXConstructExpr *E) {
Anders Carlssonb58d0172009-05-30 23:23:33 +0000446 llvm::Value *Val = DestPtr;
Mike Stump1eb44332009-09-09 15:08:12 +0000447
Anders Carlssonb58d0172009-05-30 23:23:33 +0000448 if (!Val) {
449 // Create a temporary variable.
450 Val = CGF.CreateTempAlloca(CGF.ConvertTypeForMem(E->getType()), "tmp");
451 }
Anders Carlsson8e587a12009-05-30 20:56:46 +0000452
Anders Carlssonb58d0172009-05-30 23:23:33 +0000453 CGF.EmitCXXConstructExpr(Val, E);
Anders Carlsson7f6ad152009-05-19 04:48:36 +0000454}
455
456void AggExprEmitter::VisitCXXExprWithTemporaries(CXXExprWithTemporaries *E) {
Anders Carlsson14c5cbf2009-08-16 07:36:22 +0000457 CGF.EmitCXXExprWithTemporaries(E, DestPtr, VolatileDest, IsInitializer);
Anders Carlssonb14095a2009-04-17 00:06:03 +0000458}
459
Nuno Lopes329763b2009-10-18 15:18:11 +0000460void AggExprEmitter::VisitCXXZeroInitValueExpr(CXXZeroInitValueExpr *E) {
461 LValue lvalue = LValue::MakeAddr(DestPtr, Qualifiers());
462 EmitNullInitializationToLValue(lvalue, E->getType());
463}
464
Chris Lattnerf81557c2008-04-04 18:42:16 +0000465void AggExprEmitter::EmitInitializationToLValue(Expr* E, LValue LV) {
Mike Stump7f79f9b2009-05-29 15:46:01 +0000466 // FIXME: Ignore result?
Chris Lattnerf81557c2008-04-04 18:42:16 +0000467 // FIXME: Are initializers affected by volatile?
Douglas Gregor3498bdb2009-01-29 17:44:32 +0000468 if (isa<ImplicitValueInitExpr>(E)) {
Douglas Gregor4c678342009-01-28 21:54:33 +0000469 EmitNullInitializationToLValue(LV, E->getType());
Douglas Gregor3498bdb2009-01-29 17:44:32 +0000470 } else if (E->getType()->isComplexType()) {
471 CGF.EmitComplexExprIntoAddr(E, LV.getAddress(), false);
Eli Friedmanc8ba9612008-05-12 15:06:05 +0000472 } else if (CGF.hasAggregateLLVMType(E->getType())) {
473 CGF.EmitAnyExpr(E, LV.getAddress(), false);
474 } else {
475 CGF.EmitStoreThroughLValue(CGF.EmitAnyExpr(E), LV, E->getType());
Chris Lattnerf81557c2008-04-04 18:42:16 +0000476 }
Chris Lattnerf81557c2008-04-04 18:42:16 +0000477}
478
479void AggExprEmitter::EmitNullInitializationToLValue(LValue LV, QualType T) {
480 if (!CGF.hasAggregateLLVMType(T)) {
481 // For non-aggregates, we can store zero
Owen Andersonc9c88b42009-07-31 20:28:54 +0000482 llvm::Value *Null = llvm::Constant::getNullValue(CGF.ConvertType(T));
Daniel Dunbar82397132008-08-06 05:32:55 +0000483 CGF.EmitStoreThroughLValue(RValue::get(Null), LV, T);
Lauro Ramos Venancio145cd892008-02-19 19:27:31 +0000484 } else {
Chris Lattnerf81557c2008-04-04 18:42:16 +0000485 // Otherwise, just memset the whole thing to zero. This is legal
486 // because in LLVM, all default initializers are guaranteed to have a
487 // bit pattern of all zeros.
Eli Friedman0f593122009-04-13 21:47:26 +0000488 // FIXME: That isn't true for member pointers!
Chris Lattnerf81557c2008-04-04 18:42:16 +0000489 // There's a potential optimization opportunity in combining
490 // memsets; that would be easy for arrays, but relatively
491 // difficult for structures with the current code.
Eli Friedmanccf0ed82009-03-28 03:10:45 +0000492 CGF.EmitMemSetToZero(LV.getAddress(), T);
Chris Lattnerf81557c2008-04-04 18:42:16 +0000493 }
494}
495
Chris Lattnerf81557c2008-04-04 18:42:16 +0000496void AggExprEmitter::VisitInitListExpr(InitListExpr *E) {
Eli Friedmana385b3c2008-12-02 01:17:45 +0000497#if 0
Mike Stump1eb44332009-09-09 15:08:12 +0000498 // FIXME: Disabled while we figure out what to do about
Eli Friedmana385b3c2008-12-02 01:17:45 +0000499 // test/CodeGen/bitfield.c
500 //
Mike Stumpf5408fe2009-05-16 07:57:57 +0000501 // If we can, prefer a copy from a global; this is a lot less code for long
502 // globals, and it's easier for the current optimizers to analyze.
503 // FIXME: Should we really be doing this? Should we try to avoid cases where
504 // we emit a global with a lot of zeros? Should we try to avoid short
505 // globals?
Eli Friedmanc9e8f602009-01-25 02:32:41 +0000506 if (E->isConstantInitializer(CGF.getContext(), 0)) {
Eli Friedman994ffef2008-11-30 02:11:09 +0000507 llvm::Constant* C = CGF.CGM.EmitConstantExpr(E, &CGF);
508 llvm::GlobalVariable* GV =
509 new llvm::GlobalVariable(C->getType(), true,
510 llvm::GlobalValue::InternalLinkage,
511 C, "", &CGF.CGM.getModule(), 0);
Mike Stump4ac20dd2009-05-23 20:28:01 +0000512 EmitFinalDestCopy(E, LValue::MakeAddr(GV, 0));
Eli Friedman994ffef2008-11-30 02:11:09 +0000513 return;
514 }
Eli Friedmana385b3c2008-12-02 01:17:45 +0000515#endif
Douglas Gregora9c87802009-01-29 19:42:23 +0000516 if (E->hadArrayRangeDesignator()) {
517 CGF.ErrorUnsupported(E, "GNU array range designator extension");
518 }
519
Chris Lattnerf81557c2008-04-04 18:42:16 +0000520 // Handle initialization of an array.
521 if (E->getType()->isArrayType()) {
522 const llvm::PointerType *APType =
523 cast<llvm::PointerType>(DestPtr->getType());
524 const llvm::ArrayType *AType =
525 cast<llvm::ArrayType>(APType->getElementType());
Mike Stump1eb44332009-09-09 15:08:12 +0000526
Chris Lattnerf81557c2008-04-04 18:42:16 +0000527 uint64_t NumInitElements = E->getNumInits();
Eli Friedman922696f2008-05-19 17:51:16 +0000528
Chris Lattner96196622008-07-26 22:37:01 +0000529 if (E->getNumInits() > 0) {
530 QualType T1 = E->getType();
531 QualType T2 = E->getInit(0)->getType();
Eli Friedman2dce5f82009-05-28 23:04:00 +0000532 if (CGF.getContext().hasSameUnqualifiedType(T1, T2)) {
Chris Lattner96196622008-07-26 22:37:01 +0000533 EmitAggLoadOfLValue(E->getInit(0));
534 return;
535 }
Eli Friedman922696f2008-05-19 17:51:16 +0000536 }
537
Chris Lattnerf81557c2008-04-04 18:42:16 +0000538 uint64_t NumArrayElements = AType->getNumElements();
Chris Lattnerc63a1f22008-08-04 07:31:14 +0000539 QualType ElementType = CGF.getContext().getCanonicalType(E->getType());
Douglas Gregor4c678342009-01-28 21:54:33 +0000540 ElementType = CGF.getContext().getAsArrayType(ElementType)->getElementType();
Mike Stump1eb44332009-09-09 15:08:12 +0000541
John McCall0953e762009-09-24 19:53:00 +0000542 // FIXME: were we intentionally ignoring address spaces and GC attributes?
543 Qualifiers Quals = CGF.MakeQualifiers(ElementType);
Eli Friedman1e692ac2008-06-13 23:01:12 +0000544
Chris Lattnerf81557c2008-04-04 18:42:16 +0000545 for (uint64_t i = 0; i != NumArrayElements; ++i) {
546 llvm::Value *NextVal = Builder.CreateStructGEP(DestPtr, i, ".array");
547 if (i < NumInitElements)
Eli Friedman1e692ac2008-06-13 23:01:12 +0000548 EmitInitializationToLValue(E->getInit(i),
John McCall0953e762009-09-24 19:53:00 +0000549 LValue::MakeAddr(NextVal, Quals));
Chris Lattnerf81557c2008-04-04 18:42:16 +0000550 else
John McCall0953e762009-09-24 19:53:00 +0000551 EmitNullInitializationToLValue(LValue::MakeAddr(NextVal, Quals),
Chris Lattnerf81557c2008-04-04 18:42:16 +0000552 ElementType);
Lauro Ramos Venancio145cd892008-02-19 19:27:31 +0000553 }
Chris Lattnerf81557c2008-04-04 18:42:16 +0000554 return;
555 }
Mike Stump1eb44332009-09-09 15:08:12 +0000556
Chris Lattnerf81557c2008-04-04 18:42:16 +0000557 assert(E->getType()->isRecordType() && "Only support structs/unions here!");
Mike Stump1eb44332009-09-09 15:08:12 +0000558
Chris Lattnerf81557c2008-04-04 18:42:16 +0000559 // Do struct initialization; this code just sets each individual member
560 // to the approprate value. This makes bitfield support automatic;
561 // the disadvantage is that the generated code is more difficult for
562 // the optimizer, especially with bitfields.
563 unsigned NumInitElements = E->getNumInits();
Ted Kremenek6217b802009-07-29 21:53:49 +0000564 RecordDecl *SD = E->getType()->getAs<RecordType>()->getDecl();
Chris Lattnerf81557c2008-04-04 18:42:16 +0000565 unsigned CurInitVal = 0;
Douglas Gregor0bb76892009-01-29 16:53:55 +0000566
567 if (E->getType()->isUnionType()) {
568 // Only initialize one field of a union. The field itself is
569 // specified by the initializer list.
570 if (!E->getInitializedFieldInUnion()) {
571 // Empty union; we have nothing to do.
Mike Stump1eb44332009-09-09 15:08:12 +0000572
Douglas Gregor0bb76892009-01-29 16:53:55 +0000573#ifndef NDEBUG
574 // Make sure that it's really an empty and not a failure of
575 // semantic analysis.
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000576 for (RecordDecl::field_iterator Field = SD->field_begin(),
577 FieldEnd = SD->field_end();
Douglas Gregor0bb76892009-01-29 16:53:55 +0000578 Field != FieldEnd; ++Field)
579 assert(Field->isUnnamedBitfield() && "Only unnamed bitfields allowed");
580#endif
581 return;
582 }
583
584 // FIXME: volatility
585 FieldDecl *Field = E->getInitializedFieldInUnion();
586 LValue FieldLoc = CGF.EmitLValueForField(DestPtr, Field, true, 0);
587
588 if (NumInitElements) {
589 // Store the initializer into the field
590 EmitInitializationToLValue(E->getInit(0), FieldLoc);
591 } else {
592 // Default-initialize to null
593 EmitNullInitializationToLValue(FieldLoc, Field->getType());
594 }
595
596 return;
597 }
Mike Stump1eb44332009-09-09 15:08:12 +0000598
Chris Lattnerf81557c2008-04-04 18:42:16 +0000599 // Here we iterate over the fields; this makes it simpler to both
600 // default-initialize fields and skip over unnamed fields.
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000601 for (RecordDecl::field_iterator Field = SD->field_begin(),
602 FieldEnd = SD->field_end();
Douglas Gregor44b43212008-12-11 16:49:14 +0000603 Field != FieldEnd; ++Field) {
604 // We're done once we hit the flexible array member
605 if (Field->getType()->isIncompleteArrayType())
606 break;
607
Douglas Gregor34e79462009-01-28 23:36:17 +0000608 if (Field->isUnnamedBitfield())
Chris Lattnerf81557c2008-04-04 18:42:16 +0000609 continue;
Douglas Gregor34e79462009-01-28 23:36:17 +0000610
Eli Friedman1e692ac2008-06-13 23:01:12 +0000611 // FIXME: volatility
Douglas Gregor0bb76892009-01-29 16:53:55 +0000612 LValue FieldLoc = CGF.EmitLValueForField(DestPtr, *Field, false, 0);
Fariborz Jahanian14674ff2009-05-27 19:54:11 +0000613 // We never generate write-barries for initialized fields.
614 LValue::SetObjCNonGC(FieldLoc, true);
Chris Lattnerf81557c2008-04-04 18:42:16 +0000615 if (CurInitVal < NumInitElements) {
616 // Store the initializer into the field
Chris Lattnerf81557c2008-04-04 18:42:16 +0000617 EmitInitializationToLValue(E->getInit(CurInitVal++), FieldLoc);
618 } else {
619 // We're out of initalizers; default-initialize to null
Douglas Gregor44b43212008-12-11 16:49:14 +0000620 EmitNullInitializationToLValue(FieldLoc, Field->getType());
Chris Lattnerf81557c2008-04-04 18:42:16 +0000621 }
Lauro Ramos Venancio145cd892008-02-19 19:27:31 +0000622 }
Devang Patel636c3d02007-10-26 17:44:44 +0000623}
624
Chris Lattneree755f92007-08-21 04:59:27 +0000625//===----------------------------------------------------------------------===//
626// Entry Points into this File
627//===----------------------------------------------------------------------===//
628
Mike Stumpe1129a92009-05-26 18:57:45 +0000629/// EmitAggExpr - Emit the computation of the specified expression of aggregate
630/// type. The result is computed into DestPtr. Note that if DestPtr is null,
631/// the value of the aggregate expression is not needed. If VolatileDest is
632/// true, DestPtr cannot be 0.
Chris Lattneree755f92007-08-21 04:59:27 +0000633void CodeGenFunction::EmitAggExpr(const Expr *E, llvm::Value *DestPtr,
Anders Carlsson14c5cbf2009-08-16 07:36:22 +0000634 bool VolatileDest, bool IgnoreResult,
Mike Stump1eb44332009-09-09 15:08:12 +0000635 bool IsInitializer,
Fariborz Jahanian08c32132009-08-31 19:33:16 +0000636 bool RequiresGCollection) {
Chris Lattneree755f92007-08-21 04:59:27 +0000637 assert(E && hasAggregateLLVMType(E->getType()) &&
638 "Invalid aggregate expression to emit");
Mike Stumpe1129a92009-05-26 18:57:45 +0000639 assert ((DestPtr != 0 || VolatileDest == false)
640 && "volatile aggregate can't be 0");
Mike Stump1eb44332009-09-09 15:08:12 +0000641
Fariborz Jahanian08c32132009-08-31 19:33:16 +0000642 AggExprEmitter(*this, DestPtr, VolatileDest, IgnoreResult, IsInitializer,
643 RequiresGCollection)
Mike Stump49d1cd52009-05-26 22:03:21 +0000644 .Visit(const_cast<Expr*>(E));
Chris Lattneree755f92007-08-21 04:59:27 +0000645}
Daniel Dunbar7482d122008-09-09 20:49:46 +0000646
647void CodeGenFunction::EmitAggregateClear(llvm::Value *DestPtr, QualType Ty) {
648 assert(!Ty->isAnyComplexType() && "Shouldn't happen for complex");
649
650 EmitMemSetToZero(DestPtr, Ty);
651}
652
653void CodeGenFunction::EmitAggregateCopy(llvm::Value *DestPtr,
Mike Stump27fe2e62009-05-23 22:29:41 +0000654 llvm::Value *SrcPtr, QualType Ty,
655 bool isVolatile) {
Daniel Dunbar7482d122008-09-09 20:49:46 +0000656 assert(!Ty->isAnyComplexType() && "Shouldn't happen for complex");
Mike Stump1eb44332009-09-09 15:08:12 +0000657
Chris Lattner83c96292009-02-28 18:31:01 +0000658 // Aggregate assignment turns into llvm.memcpy. This is almost valid per
Chris Lattnerca4fc2c2009-02-28 18:18:58 +0000659 // C99 6.5.16.1p3, which states "If the value being stored in an object is
660 // read from another object that overlaps in anyway the storage of the first
661 // object, then the overlap shall be exact and the two objects shall have
662 // qualified or unqualified versions of a compatible type."
663 //
Chris Lattner83c96292009-02-28 18:31:01 +0000664 // memcpy is not defined if the source and destination pointers are exactly
Chris Lattnerca4fc2c2009-02-28 18:18:58 +0000665 // equal, but other compilers do this optimization, and almost every memcpy
666 // implementation handles this case safely. If there is a libc that does not
667 // safely handle this, we can add a target hook.
Benjamin Kramer3c0ef8c2009-10-13 10:07:13 +0000668 const llvm::Type *BP = llvm::Type::getInt8PtrTy(VMContext);
Daniel Dunbar7482d122008-09-09 20:49:46 +0000669 if (DestPtr->getType() != BP)
670 DestPtr = Builder.CreateBitCast(DestPtr, BP, "tmp");
671 if (SrcPtr->getType() != BP)
672 SrcPtr = Builder.CreateBitCast(SrcPtr, BP, "tmp");
Mike Stump1eb44332009-09-09 15:08:12 +0000673
Daniel Dunbar7482d122008-09-09 20:49:46 +0000674 // Get size and alignment info for this aggregate.
675 std::pair<uint64_t, unsigned> TypeInfo = getContext().getTypeInfo(Ty);
Mike Stump1eb44332009-09-09 15:08:12 +0000676
Daniel Dunbar7482d122008-09-09 20:49:46 +0000677 // FIXME: Handle variable sized types.
Owen Anderson0032b272009-08-13 21:57:51 +0000678 const llvm::Type *IntPtr =
679 llvm::IntegerType::get(VMContext, LLVMPointerWidth);
Mike Stump1eb44332009-09-09 15:08:12 +0000680
Mike Stumpfde64202009-05-23 04:13:59 +0000681 // FIXME: If we have a volatile struct, the optimizer can remove what might
682 // appear to be `extra' memory ops:
683 //
684 // volatile struct { int i; } a, b;
685 //
686 // int main() {
687 // a = b;
688 // a = b;
689 // }
690 //
Mike Stump49d1cd52009-05-26 22:03:21 +0000691 // we need to use a differnt call here. We use isVolatile to indicate when
692 // either the source or the destination is volatile.
Chris Lattnerca4fc2c2009-02-28 18:18:58 +0000693 Builder.CreateCall4(CGM.getMemCpyFn(),
Daniel Dunbar7482d122008-09-09 20:49:46 +0000694 DestPtr, SrcPtr,
695 // TypeInfo.first describes size in bits.
Owen Anderson4a28d5d2009-07-24 23:12:58 +0000696 llvm::ConstantInt::get(IntPtr, TypeInfo.first/8),
Mike Stump1eb44332009-09-09 15:08:12 +0000697 llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext),
Daniel Dunbar7482d122008-09-09 20:49:46 +0000698 TypeInfo.second/8));
699}