blob: 04e0044aaf2bb8806641ee22372bb2d30ca33c54 [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 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 {
Benjamin Kramer85b45212009-11-28 19:45:26 +000032class AggExprEmitter : public StmtVisitor<AggExprEmitter> {
Chris Lattner9c033562007-08-21 04:25:47 +000033 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;
Mike Stump49d1cd52009-05-26 22:03:21 +000037 bool IgnoreResult;
Anders Carlsson14c5cbf2009-08-16 07:36:22 +000038 bool IsInitializer;
Fariborz Jahanian08c32132009-08-31 19:33:16 +000039 bool RequiresGCollection;
John McCallef072fd2010-05-22 01:48:05 +000040
41 ReturnValueSlot getReturnValueSlot() const {
John McCallfa037bd2010-05-22 22:13:32 +000042 // If the destination slot requires garbage collection, we can't
43 // use the real return value slot, because we have to use the GC
44 // API.
45 if (RequiresGCollection) return ReturnValueSlot();
46
John McCallef072fd2010-05-22 01:48:05 +000047 return ReturnValueSlot(DestPtr, VolatileDest);
48 }
John McCallfa037bd2010-05-22 22:13:32 +000049
Chris Lattner9c033562007-08-21 04:25:47 +000050public:
Mike Stumpff4bf3b2009-05-27 01:42:21 +000051 AggExprEmitter(CodeGenFunction &cgf, llvm::Value *destPtr, bool v,
Fariborz Jahanian08c32132009-08-31 19:33:16 +000052 bool ignore, bool isinit, bool requiresGCollection)
Chris Lattnerbfc0c1a2007-08-26 23:13:56 +000053 : CGF(cgf), Builder(CGF.Builder),
Anders Carlsson14c5cbf2009-08-16 07:36:22 +000054 DestPtr(destPtr), VolatileDest(v), IgnoreResult(ignore),
Fariborz Jahanian08c32132009-08-31 19:33:16 +000055 IsInitializer(isinit), RequiresGCollection(requiresGCollection) {
Chris Lattner9c033562007-08-21 04:25:47 +000056 }
57
Chris Lattneree755f92007-08-21 04:59:27 +000058 //===--------------------------------------------------------------------===//
59 // Utilities
60 //===--------------------------------------------------------------------===//
61
Chris Lattner9c033562007-08-21 04:25:47 +000062 /// EmitAggLoadOfLValue - Given an expression with aggregate type that
63 /// represents a value lvalue, this method emits the address of the lvalue,
64 /// then loads the result into DestPtr.
65 void EmitAggLoadOfLValue(const Expr *E);
Eli Friedman922696f2008-05-19 17:51:16 +000066
Mike Stump4ac20dd2009-05-23 20:28:01 +000067 /// EmitFinalDestCopy - Perform the final copy to DestPtr, if desired.
Mike Stump49d1cd52009-05-26 22:03:21 +000068 void EmitFinalDestCopy(const Expr *E, LValue Src, bool Ignore = false);
69 void EmitFinalDestCopy(const Expr *E, RValue Src, bool Ignore = false);
Mike Stump4ac20dd2009-05-23 20:28:01 +000070
John McCallfa037bd2010-05-22 22:13:32 +000071 void EmitGCMove(const Expr *E, RValue Src);
72
73 bool TypeRequiresGCollection(QualType T);
74
Chris Lattneree755f92007-08-21 04:59:27 +000075 //===--------------------------------------------------------------------===//
76 // Visitor Methods
77 //===--------------------------------------------------------------------===//
Mike Stump1eb44332009-09-09 15:08:12 +000078
Chris Lattner9c033562007-08-21 04:25:47 +000079 void VisitStmt(Stmt *S) {
Daniel Dunbar488e9932008-08-16 00:56:44 +000080 CGF.ErrorUnsupported(S, "aggregate expression");
Chris Lattner9c033562007-08-21 04:25:47 +000081 }
82 void VisitParenExpr(ParenExpr *PE) { Visit(PE->getSubExpr()); }
Eli Friedman12444a22009-01-27 09:03:41 +000083 void VisitUnaryExtension(UnaryOperator *E) { Visit(E->getSubExpr()); }
Chris Lattner9c033562007-08-21 04:25:47 +000084
85 // l-values.
Seo Sanghyeon9b73b392007-12-14 02:04:12 +000086 void VisitDeclRefExpr(DeclRefExpr *DRE) { EmitAggLoadOfLValue(DRE); }
87 void VisitMemberExpr(MemberExpr *ME) { EmitAggLoadOfLValue(ME); }
88 void VisitUnaryDeref(UnaryOperator *E) { EmitAggLoadOfLValue(E); }
Daniel Dunbar5be028f2010-01-04 18:47:06 +000089 void VisitStringLiteral(StringLiteral *E) { EmitAggLoadOfLValue(E); }
Chris Lattnerf0a990c2009-04-21 23:00:09 +000090 void VisitCompoundLiteralExpr(CompoundLiteralExpr *E) {
Mike Stump1eb44332009-09-09 15:08:12 +000091 EmitAggLoadOfLValue(E);
Chris Lattnerf0a990c2009-04-21 23:00:09 +000092 }
Seo Sanghyeon9b73b392007-12-14 02:04:12 +000093 void VisitArraySubscriptExpr(ArraySubscriptExpr *E) {
94 EmitAggLoadOfLValue(E);
95 }
Chris Lattnerf0a990c2009-04-21 23:00:09 +000096 void VisitBlockDeclRefExpr(const BlockDeclRefExpr *E) {
Mike Stump1eb44332009-09-09 15:08:12 +000097 EmitAggLoadOfLValue(E);
Chris Lattnerf0a990c2009-04-21 23:00:09 +000098 }
99 void VisitPredefinedExpr(const PredefinedExpr *E) {
Mike Stump1eb44332009-09-09 15:08:12 +0000100 EmitAggLoadOfLValue(E);
Chris Lattnerf0a990c2009-04-21 23:00:09 +0000101 }
Mike Stump1eb44332009-09-09 15:08:12 +0000102
Chris Lattner9c033562007-08-21 04:25:47 +0000103 // Operators.
Anders Carlsson4d8673b2009-08-07 23:22:37 +0000104 void VisitCastExpr(CastExpr *E);
Anders Carlsson148fe672007-10-31 22:04:46 +0000105 void VisitCallExpr(const CallExpr *E);
Chris Lattnerb2d963f2007-08-31 22:54:14 +0000106 void VisitStmtExpr(const StmtExpr *E);
Chris Lattner9c033562007-08-21 04:25:47 +0000107 void VisitBinaryOperator(const BinaryOperator *BO);
Fariborz Jahanian8bfd31f2009-10-22 22:57:31 +0000108 void VisitPointerToDataMemberBinaryOperator(const BinaryOperator *BO);
Chris Lattner03d6fb92007-08-21 04:43:17 +0000109 void VisitBinAssign(const BinaryOperator *E);
Eli Friedman07fa52a2008-05-20 07:56:31 +0000110 void VisitBinComma(const BinaryOperator *E);
Anders Carlssona024d172009-10-03 15:43:24 +0000111 void VisitUnaryAddrOf(const UnaryOperator *E);
Chris Lattner9c033562007-08-21 04:25:47 +0000112
Chris Lattner8fdf3282008-06-24 17:04:18 +0000113 void VisitObjCMessageExpr(ObjCMessageExpr *E);
Daniel Dunbar0a04d772008-08-23 10:51:21 +0000114 void VisitObjCIvarRefExpr(ObjCIvarRefExpr *E) {
115 EmitAggLoadOfLValue(E);
116 }
Daniel Dunbar9c3fc702008-08-27 06:57:25 +0000117 void VisitObjCPropertyRefExpr(ObjCPropertyRefExpr *E);
Fariborz Jahanian09105f52009-08-20 17:02:02 +0000118 void VisitObjCImplicitSetterGetterRefExpr(ObjCImplicitSetterGetterRefExpr *E);
Mike Stump1eb44332009-09-09 15:08:12 +0000119
Chris Lattner9c033562007-08-21 04:25:47 +0000120 void VisitConditionalOperator(const ConditionalOperator *CO);
Anders Carlssona294ca82009-07-08 18:33:14 +0000121 void VisitChooseExpr(const ChooseExpr *CE);
Devang Patel636c3d02007-10-26 17:44:44 +0000122 void VisitInitListExpr(InitListExpr *E);
Anders Carlsson30311fa2009-12-16 06:57:54 +0000123 void VisitImplicitValueInitExpr(ImplicitValueInitExpr *E);
Chris Lattner04421082008-04-08 04:40:51 +0000124 void VisitCXXDefaultArgExpr(CXXDefaultArgExpr *DAE) {
125 Visit(DAE->getExpr());
126 }
Anders Carlssonb58d0172009-05-30 23:23:33 +0000127 void VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr *E);
Anders Carlsson31ccf372009-05-03 17:47:16 +0000128 void VisitCXXConstructExpr(const CXXConstructExpr *E);
Anders Carlsson7f6ad152009-05-19 04:48:36 +0000129 void VisitCXXExprWithTemporaries(CXXExprWithTemporaries *E);
Nuno Lopes329763b2009-10-18 15:18:11 +0000130 void VisitCXXZeroInitValueExpr(CXXZeroInitValueExpr *E);
Mike Stump2710c412009-11-18 00:40:12 +0000131 void VisitCXXTypeidExpr(CXXTypeidExpr *E) { EmitAggLoadOfLValue(E); }
Anders Carlsson7f6ad152009-05-19 04:48:36 +0000132
Eli Friedmanb1851242008-05-27 15:51:49 +0000133 void VisitVAArgExpr(VAArgExpr *E);
Chris Lattnerf81557c2008-04-04 18:42:16 +0000134
Anders Carlsson78e83f82010-02-03 17:33:16 +0000135 void EmitInitializationToLValue(Expr *E, LValue Address, QualType T);
Chris Lattnerf81557c2008-04-04 18:42:16 +0000136 void EmitNullInitializationToLValue(LValue Address, QualType T);
Chris Lattner9c033562007-08-21 04:25:47 +0000137 // case Expr::ChooseExprClass:
Mike Stump39406b12009-12-09 19:24:08 +0000138 void VisitCXXThrowExpr(const CXXThrowExpr *E) { CGF.EmitCXXThrowExpr(E); }
Chris Lattner9c033562007-08-21 04:25:47 +0000139};
140} // end anonymous namespace.
141
Chris Lattneree755f92007-08-21 04:59:27 +0000142//===----------------------------------------------------------------------===//
143// Utilities
144//===----------------------------------------------------------------------===//
Chris Lattner9c033562007-08-21 04:25:47 +0000145
Chris Lattner883f6a72007-08-11 00:04:45 +0000146/// EmitAggLoadOfLValue - Given an expression with aggregate type that
147/// represents a value lvalue, this method emits the address of the lvalue,
148/// then loads the result into DestPtr.
Chris Lattner9c033562007-08-21 04:25:47 +0000149void AggExprEmitter::EmitAggLoadOfLValue(const Expr *E) {
150 LValue LV = CGF.EmitLValue(E);
Mike Stump4ac20dd2009-05-23 20:28:01 +0000151 EmitFinalDestCopy(E, LV);
152}
153
John McCallfa037bd2010-05-22 22:13:32 +0000154/// \brief True if the given aggregate type requires special GC API calls.
155bool AggExprEmitter::TypeRequiresGCollection(QualType T) {
156 // Only record types have members that might require garbage collection.
157 const RecordType *RecordTy = T->getAs<RecordType>();
158 if (!RecordTy) return false;
159
160 // Don't mess with non-trivial C++ types.
161 RecordDecl *Record = RecordTy->getDecl();
162 if (isa<CXXRecordDecl>(Record) &&
163 (!cast<CXXRecordDecl>(Record)->hasTrivialCopyConstructor() ||
164 !cast<CXXRecordDecl>(Record)->hasTrivialDestructor()))
165 return false;
166
167 // Check whether the type has an object member.
168 return Record->hasObjectMember();
169}
170
171/// \brief Perform the final move to DestPtr if RequiresGCollection is set.
172///
173/// The idea is that you do something like this:
174/// RValue Result = EmitSomething(..., getReturnValueSlot());
175/// EmitGCMove(E, Result);
176/// If GC doesn't interfere, this will cause the result to be emitted
177/// directly into the return value slot. If GC does interfere, a final
178/// move will be performed.
179void AggExprEmitter::EmitGCMove(const Expr *E, RValue Src) {
Fariborz Jahanian55bcace2010-06-15 22:44:06 +0000180 if (RequiresGCollection) {
181 std::pair<uint64_t, unsigned> TypeInfo =
182 CGF.getContext().getTypeInfo(E->getType());
183 unsigned long size = TypeInfo.first/8;
184 const llvm::Type *SizeTy = CGF.ConvertType(CGF.getContext().getSizeType());
185 llvm::Value *SizeVal = llvm::ConstantInt::get(SizeTy, size);
186 CGF.CGM.getObjCRuntime().EmitGCMemmoveCollectable(CGF, DestPtr,
John McCallfa037bd2010-05-22 22:13:32 +0000187 Src.getAggregateAddr(),
Fariborz Jahanian55bcace2010-06-15 22:44:06 +0000188 SizeVal);
189 }
John McCallfa037bd2010-05-22 22:13:32 +0000190}
191
Mike Stump4ac20dd2009-05-23 20:28:01 +0000192/// EmitFinalDestCopy - Perform the final copy to DestPtr, if desired.
Mike Stump49d1cd52009-05-26 22:03:21 +0000193void AggExprEmitter::EmitFinalDestCopy(const Expr *E, RValue Src, bool Ignore) {
Mike Stump4ac20dd2009-05-23 20:28:01 +0000194 assert(Src.isAggregate() && "value must be aggregate value!");
195
Chris Lattner883f6a72007-08-11 00:04:45 +0000196 // If the result is ignored, don't copy from the value.
Mike Stump9ccb1032009-05-23 22:01:27 +0000197 if (DestPtr == 0) {
Mike Stump49d1cd52009-05-26 22:03:21 +0000198 if (!Src.isVolatileQualified() || (IgnoreResult && Ignore))
Mike Stump9ccb1032009-05-23 22:01:27 +0000199 return;
Mike Stump49d1cd52009-05-26 22:03:21 +0000200 // If the source is volatile, we must read from it; to do that, we need
201 // some place to put it.
Daniel Dunbar195337d2010-02-09 02:48:28 +0000202 DestPtr = CGF.CreateMemTemp(E->getType(), "agg.tmp");
Mike Stump9ccb1032009-05-23 22:01:27 +0000203 }
Chris Lattner883f6a72007-08-11 00:04:45 +0000204
Fariborz Jahanian08c32132009-08-31 19:33:16 +0000205 if (RequiresGCollection) {
Fariborz Jahanian55bcace2010-06-15 22:44:06 +0000206 std::pair<uint64_t, unsigned> TypeInfo =
207 CGF.getContext().getTypeInfo(E->getType());
208 unsigned long size = TypeInfo.first/8;
209 const llvm::Type *SizeTy = CGF.ConvertType(CGF.getContext().getSizeType());
210 llvm::Value *SizeVal = llvm::ConstantInt::get(SizeTy, size);
Fariborz Jahanian08c32132009-08-31 19:33:16 +0000211 CGF.CGM.getObjCRuntime().EmitGCMemmoveCollectable(CGF,
212 DestPtr, Src.getAggregateAddr(),
Fariborz Jahanian55bcace2010-06-15 22:44:06 +0000213 SizeVal);
Fariborz Jahanian08c32132009-08-31 19:33:16 +0000214 return;
215 }
Mike Stump4ac20dd2009-05-23 20:28:01 +0000216 // If the result of the assignment is used, copy the LHS there also.
217 // FIXME: Pass VolatileDest as well. I think we also need to merge volatile
218 // from the source as well, as we can't eliminate it if either operand
219 // is volatile, unless copy has volatile for both source and destination..
Mike Stump27fe2e62009-05-23 22:29:41 +0000220 CGF.EmitAggregateCopy(DestPtr, Src.getAggregateAddr(), E->getType(),
221 VolatileDest|Src.isVolatileQualified());
Mike Stump4ac20dd2009-05-23 20:28:01 +0000222}
223
224/// EmitFinalDestCopy - Perform the final copy to DestPtr, if desired.
Mike Stump49d1cd52009-05-26 22:03:21 +0000225void AggExprEmitter::EmitFinalDestCopy(const Expr *E, LValue Src, bool Ignore) {
Mike Stump4ac20dd2009-05-23 20:28:01 +0000226 assert(Src.isSimple() && "Can't have aggregate bitfield, vector, etc");
227
228 EmitFinalDestCopy(E, RValue::getAggregate(Src.getAddress(),
Mike Stump49d1cd52009-05-26 22:03:21 +0000229 Src.isVolatileQualified()),
230 Ignore);
Chris Lattner883f6a72007-08-11 00:04:45 +0000231}
232
Chris Lattneree755f92007-08-21 04:59:27 +0000233//===----------------------------------------------------------------------===//
234// Visitor Methods
235//===----------------------------------------------------------------------===//
236
Anders Carlsson4d8673b2009-08-07 23:22:37 +0000237void AggExprEmitter::VisitCastExpr(CastExpr *E) {
Douglas Gregor69cfeb12010-05-14 21:31:02 +0000238 if (!DestPtr && E->getCastKind() != CastExpr::CK_Dynamic) {
Douglas Gregor4ce46c22010-03-07 23:24:59 +0000239 Visit(E->getSubExpr());
240 return;
241 }
242
Anders Carlsson30168422009-09-29 01:23:39 +0000243 switch (E->getCastKind()) {
244 default: assert(0 && "Unhandled cast kind!");
245
Douglas Gregor69cfeb12010-05-14 21:31:02 +0000246 case CastExpr::CK_Dynamic: {
247 assert(isa<CXXDynamicCastExpr>(E) && "CK_Dynamic without a dynamic_cast?");
248 LValue LV = CGF.EmitCheckedLValue(E->getSubExpr());
249 // FIXME: Do we also need to handle property references here?
250 if (LV.isSimple())
251 CGF.EmitDynamicCast(LV.getAddress(), cast<CXXDynamicCastExpr>(E));
252 else
253 CGF.CGM.ErrorUnsupported(E, "non-simple lvalue dynamic_cast");
254
255 if (DestPtr)
256 CGF.CGM.ErrorUnsupported(E, "lvalue dynamic_cast with a destination");
257 break;
258 }
259
Anders Carlsson30168422009-09-29 01:23:39 +0000260 case CastExpr::CK_ToUnion: {
Anders Carlsson4d8673b2009-08-07 23:22:37 +0000261 // GCC union extension
Eli Friedman34ebf4d2009-06-03 20:45:06 +0000262 QualType PtrTy =
Anders Carlsson30168422009-09-29 01:23:39 +0000263 CGF.getContext().getPointerType(E->getSubExpr()->getType());
Eli Friedman34ebf4d2009-06-03 20:45:06 +0000264 llvm::Value *CastPtr = Builder.CreateBitCast(DestPtr,
265 CGF.ConvertType(PtrTy));
Mon P Wangc6a38a42009-07-22 03:08:17 +0000266 EmitInitializationToLValue(E->getSubExpr(),
Anders Carlsson78e83f82010-02-03 17:33:16 +0000267 LValue::MakeAddr(CastPtr, Qualifiers()),
Eli Friedman18da88a2010-02-23 17:58:35 +0000268 E->getSubExpr()->getType());
Anders Carlsson30168422009-09-29 01:23:39 +0000269 break;
Nuno Lopes7e916272009-01-15 20:14:33 +0000270 }
Mike Stump1eb44332009-09-09 15:08:12 +0000271
Douglas Gregor2d6b0e92010-05-22 05:17:18 +0000272 case CastExpr::CK_DerivedToBase:
273 case CastExpr::CK_BaseToDerived:
274 case CastExpr::CK_UncheckedDerivedToBase: {
275 assert(0 && "cannot perform hierarchy conversion in EmitAggExpr: "
276 "should have been unpacked before we got here");
277 break;
278 }
279
Anders Carlsson4d8673b2009-08-07 23:22:37 +0000280 // FIXME: Remove the CK_Unknown check here.
Anders Carlsson30168422009-09-29 01:23:39 +0000281 case CastExpr::CK_Unknown:
282 case CastExpr::CK_NoOp:
283 case CastExpr::CK_UserDefinedConversion:
284 case CastExpr::CK_ConstructorConversion:
285 assert(CGF.getContext().hasSameUnqualifiedType(E->getSubExpr()->getType(),
286 E->getType()) &&
287 "Implicit cast types must be compatible");
288 Visit(E->getSubExpr());
289 break;
Anders Carlssone9d34dc2009-09-29 02:09:01 +0000290
291 case CastExpr::CK_NullToMemberPointer: {
Douglas Gregor4ce46c22010-03-07 23:24:59 +0000292 // If the subexpression's type is the C++0x nullptr_t, emit the
293 // subexpression, which may have side effects.
294 if (E->getSubExpr()->getType()->isNullPtrType())
295 Visit(E->getSubExpr());
296
Anders Carlssone9d34dc2009-09-29 02:09:01 +0000297 const llvm::Type *PtrDiffTy =
298 CGF.ConvertType(CGF.getContext().getPointerDiffType());
299
300 llvm::Value *NullValue = llvm::Constant::getNullValue(PtrDiffTy);
301 llvm::Value *Ptr = Builder.CreateStructGEP(DestPtr, 0, "ptr");
302 Builder.CreateStore(NullValue, Ptr, VolatileDest);
303
304 llvm::Value *Adj = Builder.CreateStructGEP(DestPtr, 1, "adj");
305 Builder.CreateStore(NullValue, Adj, VolatileDest);
306
307 break;
308 }
Anders Carlsson84080ec2009-09-29 03:13:20 +0000309
Anders Carlssonbb378cb2009-10-18 20:31:03 +0000310 case CastExpr::CK_BitCast: {
311 // This must be a member function pointer cast.
312 Visit(E->getSubExpr());
313 break;
314 }
315
Eli Friedmanc6fdb7762009-11-27 04:46:20 +0000316 case CastExpr::CK_DerivedToBaseMemberPointer:
Anders Carlsson84080ec2009-09-29 03:13:20 +0000317 case CastExpr::CK_BaseToDerivedMemberPointer: {
318 QualType SrcType = E->getSubExpr()->getType();
319
Daniel Dunbar195337d2010-02-09 02:48:28 +0000320 llvm::Value *Src = CGF.CreateMemTemp(SrcType, "tmp");
Anders Carlsson84080ec2009-09-29 03:13:20 +0000321 CGF.EmitAggExpr(E->getSubExpr(), Src, SrcType.isVolatileQualified());
322
323 llvm::Value *SrcPtr = Builder.CreateStructGEP(Src, 0, "src.ptr");
324 SrcPtr = Builder.CreateLoad(SrcPtr);
325
326 llvm::Value *SrcAdj = Builder.CreateStructGEP(Src, 1, "src.adj");
327 SrcAdj = Builder.CreateLoad(SrcAdj);
328
329 llvm::Value *DstPtr = Builder.CreateStructGEP(DestPtr, 0, "dst.ptr");
330 Builder.CreateStore(SrcPtr, DstPtr, VolatileDest);
331
332 llvm::Value *DstAdj = Builder.CreateStructGEP(DestPtr, 1, "dst.adj");
333
334 // Now See if we need to update the adjustment.
Eli Friedmanc6fdb7762009-11-27 04:46:20 +0000335 const CXXRecordDecl *BaseDecl =
Anders Carlsson84080ec2009-09-29 03:13:20 +0000336 cast<CXXRecordDecl>(SrcType->getAs<MemberPointerType>()->
337 getClass()->getAs<RecordType>()->getDecl());
Eli Friedmanc6fdb7762009-11-27 04:46:20 +0000338 const CXXRecordDecl *DerivedDecl =
Anders Carlsson84080ec2009-09-29 03:13:20 +0000339 cast<CXXRecordDecl>(E->getType()->getAs<MemberPointerType>()->
340 getClass()->getAs<RecordType>()->getDecl());
Eli Friedmanc6fdb7762009-11-27 04:46:20 +0000341 if (E->getCastKind() == CastExpr::CK_DerivedToBaseMemberPointer)
342 std::swap(DerivedDecl, BaseDecl);
343
Anders Carlssonbb7e17b2010-01-31 01:36:53 +0000344 if (llvm::Constant *Adj =
Anders Carlssone04d45e2010-04-24 21:27:51 +0000345 CGF.CGM.GetNonVirtualBaseClassOffset(DerivedDecl, E->getBasePath())) {
Eli Friedmanc6fdb7762009-11-27 04:46:20 +0000346 if (E->getCastKind() == CastExpr::CK_DerivedToBaseMemberPointer)
347 SrcAdj = Builder.CreateSub(SrcAdj, Adj, "adj");
348 else
349 SrcAdj = Builder.CreateAdd(SrcAdj, Adj, "adj");
350 }
Anders Carlsson84080ec2009-09-29 03:13:20 +0000351
352 Builder.CreateStore(SrcAdj, DstAdj, VolatileDest);
353 break;
354 }
Anders Carlsson30168422009-09-29 01:23:39 +0000355 }
Anders Carlssone4707ff2008-01-14 06:28:57 +0000356}
357
Chris Lattner96196622008-07-26 22:37:01 +0000358void AggExprEmitter::VisitCallExpr(const CallExpr *E) {
Anders Carlssone70e8f72009-05-27 16:45:02 +0000359 if (E->getCallReturnType()->isReferenceType()) {
360 EmitAggLoadOfLValue(E);
361 return;
362 }
Mike Stump1eb44332009-09-09 15:08:12 +0000363
John McCallfa037bd2010-05-22 22:13:32 +0000364 RValue RV = CGF.EmitCallExpr(E, getReturnValueSlot());
365 EmitGCMove(E, RV);
Anders Carlsson148fe672007-10-31 22:04:46 +0000366}
Chris Lattner96196622008-07-26 22:37:01 +0000367
368void AggExprEmitter::VisitObjCMessageExpr(ObjCMessageExpr *E) {
John McCallfa037bd2010-05-22 22:13:32 +0000369 RValue RV = CGF.EmitObjCMessageExpr(E, getReturnValueSlot());
370 EmitGCMove(E, RV);
Chris Lattner8fdf3282008-06-24 17:04:18 +0000371}
Anders Carlsson148fe672007-10-31 22:04:46 +0000372
Daniel Dunbar9c3fc702008-08-27 06:57:25 +0000373void AggExprEmitter::VisitObjCPropertyRefExpr(ObjCPropertyRefExpr *E) {
John McCallfa037bd2010-05-22 22:13:32 +0000374 RValue RV = CGF.EmitObjCPropertyGet(E, getReturnValueSlot());
375 EmitGCMove(E, RV);
Daniel Dunbar9c3fc702008-08-27 06:57:25 +0000376}
377
Fariborz Jahanian09105f52009-08-20 17:02:02 +0000378void AggExprEmitter::VisitObjCImplicitSetterGetterRefExpr(
379 ObjCImplicitSetterGetterRefExpr *E) {
John McCallfa037bd2010-05-22 22:13:32 +0000380 RValue RV = CGF.EmitObjCPropertyGet(E, getReturnValueSlot());
381 EmitGCMove(E, RV);
Fariborz Jahanian5daf5702008-11-22 18:39:36 +0000382}
383
Chris Lattner96196622008-07-26 22:37:01 +0000384void AggExprEmitter::VisitBinComma(const BinaryOperator *E) {
Mike Stump7f79f9b2009-05-29 15:46:01 +0000385 CGF.EmitAnyExpr(E->getLHS(), 0, false, true);
Anders Carlsson14c5cbf2009-08-16 07:36:22 +0000386 CGF.EmitAggExpr(E->getRHS(), DestPtr, VolatileDest,
387 /*IgnoreResult=*/false, IsInitializer);
Eli Friedman07fa52a2008-05-20 07:56:31 +0000388}
389
Anders Carlssona024d172009-10-03 15:43:24 +0000390void AggExprEmitter::VisitUnaryAddrOf(const UnaryOperator *E) {
391 // We have a member function pointer.
392 const MemberPointerType *MPT = E->getType()->getAs<MemberPointerType>();
Daniel Dunbarbabac132009-10-17 09:39:30 +0000393 (void) MPT;
Anders Carlssona024d172009-10-03 15:43:24 +0000394 assert(MPT->getPointeeType()->isFunctionProtoType() &&
395 "Unexpected member pointer type!");
Douglas Gregore174bd02010-05-03 20:00:27 +0000396
397 // The creation of member function pointers has no side effects; if
398 // there is no destination pointer, we have nothing to do.
399 if (!DestPtr)
400 return;
Anders Carlssona024d172009-10-03 15:43:24 +0000401
Douglas Gregora2813ce2009-10-23 18:54:35 +0000402 const DeclRefExpr *DRE = cast<DeclRefExpr>(E->getSubExpr());
Anders Carlsson7af4ec72010-01-05 05:04:05 +0000403 const CXXMethodDecl *MD =
404 cast<CXXMethodDecl>(DRE->getDecl())->getCanonicalDecl();
Anders Carlssona024d172009-10-03 15:43:24 +0000405
406 const llvm::Type *PtrDiffTy =
407 CGF.ConvertType(CGF.getContext().getPointerDiffType());
408
409 llvm::Value *DstPtr = Builder.CreateStructGEP(DestPtr, 0, "dst.ptr");
Eli Friedmanc00129a2010-05-30 06:03:20 +0000410 llvm::Value *FuncPtr = CGF.CGM.GetCXXMemberFunctionPointerValue(MD);
Anders Carlssona024d172009-10-03 15:43:24 +0000411 Builder.CreateStore(FuncPtr, DstPtr, VolatileDest);
412
413 llvm::Value *AdjPtr = Builder.CreateStructGEP(DestPtr, 1, "dst.adj");
Anders Carlssona024d172009-10-03 15:43:24 +0000414 // The adjustment will always be 0.
415 Builder.CreateStore(llvm::ConstantInt::get(PtrDiffTy, 0), AdjPtr,
416 VolatileDest);
417}
418
Chris Lattnerb2d963f2007-08-31 22:54:14 +0000419void AggExprEmitter::VisitStmtExpr(const StmtExpr *E) {
420 CGF.EmitCompoundStmt(*E->getSubStmt(), true, DestPtr, VolatileDest);
421}
422
Chris Lattner9c033562007-08-21 04:25:47 +0000423void AggExprEmitter::VisitBinaryOperator(const BinaryOperator *E) {
Fariborz Jahanian52f08bc2009-10-26 21:58:25 +0000424 if (E->getOpcode() == BinaryOperator::PtrMemD ||
425 E->getOpcode() == BinaryOperator::PtrMemI)
Fariborz Jahanian8bfd31f2009-10-22 22:57:31 +0000426 VisitPointerToDataMemberBinaryOperator(E);
427 else
428 CGF.ErrorUnsupported(E, "aggregate binary expression");
429}
430
431void AggExprEmitter::VisitPointerToDataMemberBinaryOperator(
432 const BinaryOperator *E) {
433 LValue LV = CGF.EmitPointerToDataMemberBinaryExpr(E);
434 EmitFinalDestCopy(E, LV);
Chris Lattneree755f92007-08-21 04:59:27 +0000435}
436
Chris Lattner03d6fb92007-08-21 04:43:17 +0000437void AggExprEmitter::VisitBinAssign(const BinaryOperator *E) {
Eli Friedmanff6e2b72008-02-11 01:09:17 +0000438 // For an assignment to work, the value on the right has
439 // to be compatible with the value on the left.
Eli Friedman2dce5f82009-05-28 23:04:00 +0000440 assert(CGF.getContext().hasSameUnqualifiedType(E->getLHS()->getType(),
441 E->getRHS()->getType())
Eli Friedmanff6e2b72008-02-11 01:09:17 +0000442 && "Invalid assignment");
Chris Lattner9c033562007-08-21 04:25:47 +0000443 LValue LHS = CGF.EmitLValue(E->getLHS());
Chris Lattner883f6a72007-08-11 00:04:45 +0000444
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000445 // We have to special case property setters, otherwise we must have
446 // a simple lvalue (no aggregates inside vectors, bitfields).
447 if (LHS.isPropertyRef()) {
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000448 llvm::Value *AggLoc = DestPtr;
449 if (!AggLoc)
Daniel Dunbar195337d2010-02-09 02:48:28 +0000450 AggLoc = CGF.CreateMemTemp(E->getRHS()->getType());
Mike Stump240993d2009-05-23 23:48:13 +0000451 CGF.EmitAggExpr(E->getRHS(), AggLoc, VolatileDest);
Mike Stump1eb44332009-09-09 15:08:12 +0000452 CGF.EmitObjCPropertySet(LHS.getPropertyRefExpr(),
Mike Stump240993d2009-05-23 23:48:13 +0000453 RValue::getAggregate(AggLoc, VolatileDest));
Mike Stumpb3589f42009-07-30 22:28:39 +0000454 } else if (LHS.isKVCRef()) {
Fariborz Jahanian43f44702008-11-22 22:30:21 +0000455 llvm::Value *AggLoc = DestPtr;
456 if (!AggLoc)
Daniel Dunbar195337d2010-02-09 02:48:28 +0000457 AggLoc = CGF.CreateMemTemp(E->getRHS()->getType());
Mike Stumpa49af1a2009-05-23 23:52:31 +0000458 CGF.EmitAggExpr(E->getRHS(), AggLoc, VolatileDest);
Mike Stump1eb44332009-09-09 15:08:12 +0000459 CGF.EmitObjCPropertySet(LHS.getKVCRefExpr(),
Mike Stumpa49af1a2009-05-23 23:52:31 +0000460 RValue::getAggregate(AggLoc, VolatileDest));
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000461 } else {
Fariborz Jahanian08c32132009-08-31 19:33:16 +0000462 bool RequiresGCollection = false;
John McCallfa037bd2010-05-22 22:13:32 +0000463 if (CGF.getContext().getLangOptions().getGCMode())
464 RequiresGCollection = TypeRequiresGCollection(E->getLHS()->getType());
465
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000466 // Codegen the RHS so that it stores directly into the LHS.
Fariborz Jahanian08c32132009-08-31 19:33:16 +0000467 CGF.EmitAggExpr(E->getRHS(), LHS.getAddress(), LHS.isVolatileQualified(),
468 false, false, RequiresGCollection);
Mike Stump49d1cd52009-05-26 22:03:21 +0000469 EmitFinalDestCopy(E, LHS, true);
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000470 }
Chris Lattner883f6a72007-08-11 00:04:45 +0000471}
472
Chris Lattner9c033562007-08-21 04:25:47 +0000473void AggExprEmitter::VisitConditionalOperator(const ConditionalOperator *E) {
Eli Friedman8e274bd2009-12-25 06:17:05 +0000474 if (!E->getLHS()) {
475 CGF.ErrorUnsupported(E, "conditional operator with missing LHS");
476 return;
477 }
478
Daniel Dunbar9615ecb2008-11-13 01:38:36 +0000479 llvm::BasicBlock *LHSBlock = CGF.createBasicBlock("cond.true");
480 llvm::BasicBlock *RHSBlock = CGF.createBasicBlock("cond.false");
481 llvm::BasicBlock *ContBlock = CGF.createBasicBlock("cond.end");
Mike Stump1eb44332009-09-09 15:08:12 +0000482
Eli Friedman8e274bd2009-12-25 06:17:05 +0000483 CGF.EmitBranchOnBoolExpr(E->getCond(), LHSBlock, RHSBlock);
Mike Stump1eb44332009-09-09 15:08:12 +0000484
Anders Carlsson72119a82010-02-04 17:18:07 +0000485 CGF.BeginConditionalBranch();
Chris Lattner9c033562007-08-21 04:25:47 +0000486 CGF.EmitBlock(LHSBlock);
Mike Stump1eb44332009-09-09 15:08:12 +0000487
Chris Lattner883f6a72007-08-11 00:04:45 +0000488 // Handle the GNU extension for missing LHS.
489 assert(E->getLHS() && "Must have LHS for aggregate value");
490
Chris Lattnerc748f272007-08-21 05:02:10 +0000491 Visit(E->getLHS());
Anders Carlsson72119a82010-02-04 17:18:07 +0000492 CGF.EndConditionalBranch();
Daniel Dunbard57a8712008-11-11 09:41:28 +0000493 CGF.EmitBranch(ContBlock);
Mike Stump1eb44332009-09-09 15:08:12 +0000494
Anders Carlsson72119a82010-02-04 17:18:07 +0000495 CGF.BeginConditionalBranch();
Chris Lattner9c033562007-08-21 04:25:47 +0000496 CGF.EmitBlock(RHSBlock);
Mike Stump1eb44332009-09-09 15:08:12 +0000497
Chris Lattnerc748f272007-08-21 05:02:10 +0000498 Visit(E->getRHS());
Anders Carlsson72119a82010-02-04 17:18:07 +0000499 CGF.EndConditionalBranch();
Daniel Dunbard57a8712008-11-11 09:41:28 +0000500 CGF.EmitBranch(ContBlock);
Mike Stump1eb44332009-09-09 15:08:12 +0000501
Chris Lattner9c033562007-08-21 04:25:47 +0000502 CGF.EmitBlock(ContBlock);
Chris Lattner883f6a72007-08-11 00:04:45 +0000503}
Chris Lattneree755f92007-08-21 04:59:27 +0000504
Anders Carlssona294ca82009-07-08 18:33:14 +0000505void AggExprEmitter::VisitChooseExpr(const ChooseExpr *CE) {
506 Visit(CE->getChosenSubExpr(CGF.getContext()));
507}
508
Eli Friedmanb1851242008-05-27 15:51:49 +0000509void AggExprEmitter::VisitVAArgExpr(VAArgExpr *VE) {
Daniel Dunbar07855702009-02-11 22:25:55 +0000510 llvm::Value *ArgValue = CGF.EmitVAListRef(VE->getSubExpr());
Anders Carlssonddf7cac2008-11-04 05:30:00 +0000511 llvm::Value *ArgPtr = CGF.EmitVAArg(ArgValue, VE->getType());
512
Sebastian Redl0262f022009-01-09 21:09:38 +0000513 if (!ArgPtr) {
Anders Carlssonddf7cac2008-11-04 05:30:00 +0000514 CGF.ErrorUnsupported(VE, "aggregate va_arg expression");
Sebastian Redl0262f022009-01-09 21:09:38 +0000515 return;
516 }
517
John McCall0953e762009-09-24 19:53:00 +0000518 EmitFinalDestCopy(VE, LValue::MakeAddr(ArgPtr, Qualifiers()));
Eli Friedmanb1851242008-05-27 15:51:49 +0000519}
520
Anders Carlssonb58d0172009-05-30 23:23:33 +0000521void AggExprEmitter::VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr *E) {
522 llvm::Value *Val = DestPtr;
Mike Stump1eb44332009-09-09 15:08:12 +0000523
Anders Carlssonb58d0172009-05-30 23:23:33 +0000524 if (!Val) {
525 // Create a temporary variable.
Daniel Dunbar195337d2010-02-09 02:48:28 +0000526 Val = CGF.CreateMemTemp(E->getType(), "tmp");
Anders Carlssonb58d0172009-05-30 23:23:33 +0000527
528 // FIXME: volatile
529 CGF.EmitAggExpr(E->getSubExpr(), Val, false);
Mike Stump1eb44332009-09-09 15:08:12 +0000530 } else
Anders Carlssonb58d0172009-05-30 23:23:33 +0000531 Visit(E->getSubExpr());
Mike Stump1eb44332009-09-09 15:08:12 +0000532
Anders Carlsson14c5cbf2009-08-16 07:36:22 +0000533 // Don't make this a live temporary if we're emitting an initializer expr.
534 if (!IsInitializer)
535 CGF.PushCXXTemporary(E->getTemporary(), Val);
Anders Carlssonb58d0172009-05-30 23:23:33 +0000536}
537
Anders Carlssonb14095a2009-04-17 00:06:03 +0000538void
Anders Carlsson31ccf372009-05-03 17:47:16 +0000539AggExprEmitter::VisitCXXConstructExpr(const CXXConstructExpr *E) {
Anders Carlssonb58d0172009-05-30 23:23:33 +0000540 llvm::Value *Val = DestPtr;
Mike Stump1eb44332009-09-09 15:08:12 +0000541
Anders Carlssonb58d0172009-05-30 23:23:33 +0000542 if (!Val) {
543 // Create a temporary variable.
Daniel Dunbar195337d2010-02-09 02:48:28 +0000544 Val = CGF.CreateMemTemp(E->getType(), "tmp");
Anders Carlssonb58d0172009-05-30 23:23:33 +0000545 }
Anders Carlsson8e587a12009-05-30 20:56:46 +0000546
Douglas Gregor16006c92009-12-16 18:50:27 +0000547 if (E->requiresZeroInitialization())
548 EmitNullInitializationToLValue(LValue::MakeAddr(Val,
Mike Stumpdd5614b2009-12-18 02:14:27 +0000549 // FIXME: Qualifiers()?
Douglas Gregor16006c92009-12-16 18:50:27 +0000550 E->getType().getQualifiers()),
551 E->getType());
552
Anders Carlssonb58d0172009-05-30 23:23:33 +0000553 CGF.EmitCXXConstructExpr(Val, E);
Anders Carlsson7f6ad152009-05-19 04:48:36 +0000554}
555
556void AggExprEmitter::VisitCXXExprWithTemporaries(CXXExprWithTemporaries *E) {
Mike Stumpdd5614b2009-12-18 02:14:27 +0000557 llvm::Value *Val = DestPtr;
558
Mike Stumpdd5614b2009-12-18 02:14:27 +0000559 CGF.EmitCXXExprWithTemporaries(E, Val, VolatileDest, IsInitializer);
Anders Carlssonb14095a2009-04-17 00:06:03 +0000560}
561
Nuno Lopes329763b2009-10-18 15:18:11 +0000562void AggExprEmitter::VisitCXXZeroInitValueExpr(CXXZeroInitValueExpr *E) {
Mike Stumpdd5614b2009-12-18 02:14:27 +0000563 llvm::Value *Val = DestPtr;
564
565 if (!Val) {
566 // Create a temporary variable.
Daniel Dunbar195337d2010-02-09 02:48:28 +0000567 Val = CGF.CreateMemTemp(E->getType(), "tmp");
Mike Stumpdd5614b2009-12-18 02:14:27 +0000568 }
569 LValue LV = LValue::MakeAddr(Val, Qualifiers());
Anders Carlsson30311fa2009-12-16 06:57:54 +0000570 EmitNullInitializationToLValue(LV, E->getType());
571}
572
573void AggExprEmitter::VisitImplicitValueInitExpr(ImplicitValueInitExpr *E) {
Mike Stumpdd5614b2009-12-18 02:14:27 +0000574 llvm::Value *Val = DestPtr;
575
576 if (!Val) {
577 // Create a temporary variable.
Daniel Dunbar195337d2010-02-09 02:48:28 +0000578 Val = CGF.CreateMemTemp(E->getType(), "tmp");
Mike Stumpdd5614b2009-12-18 02:14:27 +0000579 }
580 LValue LV = LValue::MakeAddr(Val, Qualifiers());
Anders Carlsson30311fa2009-12-16 06:57:54 +0000581 EmitNullInitializationToLValue(LV, E->getType());
Nuno Lopes329763b2009-10-18 15:18:11 +0000582}
583
Anders Carlsson78e83f82010-02-03 17:33:16 +0000584void
585AggExprEmitter::EmitInitializationToLValue(Expr* E, LValue LV, QualType T) {
Mike Stump7f79f9b2009-05-29 15:46:01 +0000586 // FIXME: Ignore result?
Chris Lattnerf81557c2008-04-04 18:42:16 +0000587 // FIXME: Are initializers affected by volatile?
Douglas Gregor3498bdb2009-01-29 17:44:32 +0000588 if (isa<ImplicitValueInitExpr>(E)) {
Anders Carlsson78e83f82010-02-03 17:33:16 +0000589 EmitNullInitializationToLValue(LV, T);
Anders Carlssone78ccb42010-02-03 19:13:55 +0000590 } else if (T->isReferenceType()) {
Anders Carlsson32f36ba2010-06-26 16:35:32 +0000591 RValue RV = CGF.EmitReferenceBindingToExpr(E, /*InitializedDecl=*/0);
Anders Carlssone78ccb42010-02-03 19:13:55 +0000592 CGF.EmitStoreThroughLValue(RV, LV, T);
Anders Carlsson78e83f82010-02-03 17:33:16 +0000593 } else if (T->isAnyComplexType()) {
Douglas Gregor3498bdb2009-01-29 17:44:32 +0000594 CGF.EmitComplexExprIntoAddr(E, LV.getAddress(), false);
Anders Carlsson78e83f82010-02-03 17:33:16 +0000595 } else if (CGF.hasAggregateLLVMType(T)) {
Eli Friedmanc8ba9612008-05-12 15:06:05 +0000596 CGF.EmitAnyExpr(E, LV.getAddress(), false);
597 } else {
Anders Carlsson78e83f82010-02-03 17:33:16 +0000598 CGF.EmitStoreThroughLValue(CGF.EmitAnyExpr(E), LV, T);
Chris Lattnerf81557c2008-04-04 18:42:16 +0000599 }
Chris Lattnerf81557c2008-04-04 18:42:16 +0000600}
601
602void AggExprEmitter::EmitNullInitializationToLValue(LValue LV, QualType T) {
603 if (!CGF.hasAggregateLLVMType(T)) {
604 // For non-aggregates, we can store zero
Owen Andersonc9c88b42009-07-31 20:28:54 +0000605 llvm::Value *Null = llvm::Constant::getNullValue(CGF.ConvertType(T));
Daniel Dunbar82397132008-08-06 05:32:55 +0000606 CGF.EmitStoreThroughLValue(RValue::get(Null), LV, T);
Lauro Ramos Venancio145cd892008-02-19 19:27:31 +0000607 } else {
Chris Lattnerf81557c2008-04-04 18:42:16 +0000608 // There's a potential optimization opportunity in combining
609 // memsets; that would be easy for arrays, but relatively
610 // difficult for structures with the current code.
Anders Carlsson1884eb02010-05-22 17:35:42 +0000611 CGF.EmitNullInitialization(LV.getAddress(), T);
Chris Lattnerf81557c2008-04-04 18:42:16 +0000612 }
613}
614
Chris Lattnerf81557c2008-04-04 18:42:16 +0000615void AggExprEmitter::VisitInitListExpr(InitListExpr *E) {
Eli Friedmana385b3c2008-12-02 01:17:45 +0000616#if 0
Eli Friedman13a5be12009-12-04 01:30:56 +0000617 // FIXME: Assess perf here? Figure out what cases are worth optimizing here
618 // (Length of globals? Chunks of zeroed-out space?).
Eli Friedmana385b3c2008-12-02 01:17:45 +0000619 //
Mike Stumpf5408fe2009-05-16 07:57:57 +0000620 // If we can, prefer a copy from a global; this is a lot less code for long
621 // globals, and it's easier for the current optimizers to analyze.
Eli Friedman13a5be12009-12-04 01:30:56 +0000622 if (llvm::Constant* C = CGF.CGM.EmitConstantExpr(E, E->getType(), &CGF)) {
Eli Friedman994ffef2008-11-30 02:11:09 +0000623 llvm::GlobalVariable* GV =
Eli Friedman13a5be12009-12-04 01:30:56 +0000624 new llvm::GlobalVariable(CGF.CGM.getModule(), C->getType(), true,
625 llvm::GlobalValue::InternalLinkage, C, "");
626 EmitFinalDestCopy(E, LValue::MakeAddr(GV, Qualifiers()));
Eli Friedman994ffef2008-11-30 02:11:09 +0000627 return;
628 }
Eli Friedmana385b3c2008-12-02 01:17:45 +0000629#endif
Douglas Gregora9c87802009-01-29 19:42:23 +0000630 if (E->hadArrayRangeDesignator()) {
631 CGF.ErrorUnsupported(E, "GNU array range designator extension");
632 }
633
Chris Lattnerf81557c2008-04-04 18:42:16 +0000634 // Handle initialization of an array.
635 if (E->getType()->isArrayType()) {
636 const llvm::PointerType *APType =
637 cast<llvm::PointerType>(DestPtr->getType());
638 const llvm::ArrayType *AType =
639 cast<llvm::ArrayType>(APType->getElementType());
Mike Stump1eb44332009-09-09 15:08:12 +0000640
Chris Lattnerf81557c2008-04-04 18:42:16 +0000641 uint64_t NumInitElements = E->getNumInits();
Eli Friedman922696f2008-05-19 17:51:16 +0000642
Chris Lattner96196622008-07-26 22:37:01 +0000643 if (E->getNumInits() > 0) {
644 QualType T1 = E->getType();
645 QualType T2 = E->getInit(0)->getType();
Eli Friedman2dce5f82009-05-28 23:04:00 +0000646 if (CGF.getContext().hasSameUnqualifiedType(T1, T2)) {
Chris Lattner96196622008-07-26 22:37:01 +0000647 EmitAggLoadOfLValue(E->getInit(0));
648 return;
649 }
Eli Friedman922696f2008-05-19 17:51:16 +0000650 }
651
Chris Lattnerf81557c2008-04-04 18:42:16 +0000652 uint64_t NumArrayElements = AType->getNumElements();
Chris Lattnerc63a1f22008-08-04 07:31:14 +0000653 QualType ElementType = CGF.getContext().getCanonicalType(E->getType());
Douglas Gregor4c678342009-01-28 21:54:33 +0000654 ElementType = CGF.getContext().getAsArrayType(ElementType)->getElementType();
Mike Stump1eb44332009-09-09 15:08:12 +0000655
John McCall0953e762009-09-24 19:53:00 +0000656 // FIXME: were we intentionally ignoring address spaces and GC attributes?
657 Qualifiers Quals = CGF.MakeQualifiers(ElementType);
Eli Friedman1e692ac2008-06-13 23:01:12 +0000658
Chris Lattnerf81557c2008-04-04 18:42:16 +0000659 for (uint64_t i = 0; i != NumArrayElements; ++i) {
660 llvm::Value *NextVal = Builder.CreateStructGEP(DestPtr, i, ".array");
661 if (i < NumInitElements)
Eli Friedman1e692ac2008-06-13 23:01:12 +0000662 EmitInitializationToLValue(E->getInit(i),
Anders Carlsson78e83f82010-02-03 17:33:16 +0000663 LValue::MakeAddr(NextVal, Quals),
664 ElementType);
Chris Lattnerf81557c2008-04-04 18:42:16 +0000665 else
John McCall0953e762009-09-24 19:53:00 +0000666 EmitNullInitializationToLValue(LValue::MakeAddr(NextVal, Quals),
Chris Lattnerf81557c2008-04-04 18:42:16 +0000667 ElementType);
Lauro Ramos Venancio145cd892008-02-19 19:27:31 +0000668 }
Chris Lattnerf81557c2008-04-04 18:42:16 +0000669 return;
670 }
Mike Stump1eb44332009-09-09 15:08:12 +0000671
Chris Lattnerf81557c2008-04-04 18:42:16 +0000672 assert(E->getType()->isRecordType() && "Only support structs/unions here!");
Mike Stump1eb44332009-09-09 15:08:12 +0000673
Chris Lattnerf81557c2008-04-04 18:42:16 +0000674 // Do struct initialization; this code just sets each individual member
675 // to the approprate value. This makes bitfield support automatic;
676 // the disadvantage is that the generated code is more difficult for
677 // the optimizer, especially with bitfields.
678 unsigned NumInitElements = E->getNumInits();
Ted Kremenek6217b802009-07-29 21:53:49 +0000679 RecordDecl *SD = E->getType()->getAs<RecordType>()->getDecl();
Chris Lattnerf81557c2008-04-04 18:42:16 +0000680 unsigned CurInitVal = 0;
Douglas Gregor0bb76892009-01-29 16:53:55 +0000681
682 if (E->getType()->isUnionType()) {
683 // Only initialize one field of a union. The field itself is
684 // specified by the initializer list.
685 if (!E->getInitializedFieldInUnion()) {
686 // Empty union; we have nothing to do.
Mike Stump1eb44332009-09-09 15:08:12 +0000687
Douglas Gregor0bb76892009-01-29 16:53:55 +0000688#ifndef NDEBUG
689 // Make sure that it's really an empty and not a failure of
690 // semantic analysis.
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000691 for (RecordDecl::field_iterator Field = SD->field_begin(),
692 FieldEnd = SD->field_end();
Douglas Gregor0bb76892009-01-29 16:53:55 +0000693 Field != FieldEnd; ++Field)
694 assert(Field->isUnnamedBitfield() && "Only unnamed bitfields allowed");
695#endif
696 return;
697 }
698
699 // FIXME: volatility
700 FieldDecl *Field = E->getInitializedFieldInUnion();
Anders Carlssone78ccb42010-02-03 19:13:55 +0000701 LValue FieldLoc = CGF.EmitLValueForFieldInitialization(DestPtr, Field, 0);
Douglas Gregor0bb76892009-01-29 16:53:55 +0000702
703 if (NumInitElements) {
704 // Store the initializer into the field
Anders Carlsson78e83f82010-02-03 17:33:16 +0000705 EmitInitializationToLValue(E->getInit(0), FieldLoc, Field->getType());
Douglas Gregor0bb76892009-01-29 16:53:55 +0000706 } else {
707 // Default-initialize to null
708 EmitNullInitializationToLValue(FieldLoc, Field->getType());
709 }
710
711 return;
712 }
Chris Lattnerb35baae2010-03-08 21:08:07 +0000713
714 // If we're initializing the whole aggregate, just do it in place.
715 // FIXME: This is a hack around an AST bug (PR6537).
716 if (NumInitElements == 1 && E->getType() == E->getInit(0)->getType()) {
717 EmitInitializationToLValue(E->getInit(0),
718 LValue::MakeAddr(DestPtr, Qualifiers()),
719 E->getType());
720 return;
721 }
722
Mike Stump1eb44332009-09-09 15:08:12 +0000723
Chris Lattnerf81557c2008-04-04 18:42:16 +0000724 // Here we iterate over the fields; this makes it simpler to both
725 // default-initialize fields and skip over unnamed fields.
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000726 for (RecordDecl::field_iterator Field = SD->field_begin(),
727 FieldEnd = SD->field_end();
Douglas Gregor44b43212008-12-11 16:49:14 +0000728 Field != FieldEnd; ++Field) {
729 // We're done once we hit the flexible array member
730 if (Field->getType()->isIncompleteArrayType())
731 break;
732
Douglas Gregor34e79462009-01-28 23:36:17 +0000733 if (Field->isUnnamedBitfield())
Chris Lattnerf81557c2008-04-04 18:42:16 +0000734 continue;
Douglas Gregor34e79462009-01-28 23:36:17 +0000735
Eli Friedman1e692ac2008-06-13 23:01:12 +0000736 // FIXME: volatility
Anders Carlssone78ccb42010-02-03 19:13:55 +0000737 LValue FieldLoc = CGF.EmitLValueForFieldInitialization(DestPtr, *Field, 0);
Fariborz Jahanian14674ff2009-05-27 19:54:11 +0000738 // We never generate write-barries for initialized fields.
739 LValue::SetObjCNonGC(FieldLoc, true);
Chris Lattnerf81557c2008-04-04 18:42:16 +0000740 if (CurInitVal < NumInitElements) {
Chris Lattnerb35baae2010-03-08 21:08:07 +0000741 // Store the initializer into the field.
742 EmitInitializationToLValue(E->getInit(CurInitVal++), FieldLoc,
Anders Carlsson78e83f82010-02-03 17:33:16 +0000743 Field->getType());
Chris Lattnerf81557c2008-04-04 18:42:16 +0000744 } else {
745 // We're out of initalizers; default-initialize to null
Douglas Gregor44b43212008-12-11 16:49:14 +0000746 EmitNullInitializationToLValue(FieldLoc, Field->getType());
Chris Lattnerf81557c2008-04-04 18:42:16 +0000747 }
Lauro Ramos Venancio145cd892008-02-19 19:27:31 +0000748 }
Devang Patel636c3d02007-10-26 17:44:44 +0000749}
750
Chris Lattneree755f92007-08-21 04:59:27 +0000751//===----------------------------------------------------------------------===//
752// Entry Points into this File
753//===----------------------------------------------------------------------===//
754
Mike Stumpe1129a92009-05-26 18:57:45 +0000755/// EmitAggExpr - Emit the computation of the specified expression of aggregate
756/// type. The result is computed into DestPtr. Note that if DestPtr is null,
757/// the value of the aggregate expression is not needed. If VolatileDest is
758/// true, DestPtr cannot be 0.
Daniel Dunbar18aba0d2010-02-05 19:38:31 +0000759//
760// FIXME: Take Qualifiers object.
Chris Lattneree755f92007-08-21 04:59:27 +0000761void CodeGenFunction::EmitAggExpr(const Expr *E, llvm::Value *DestPtr,
Anders Carlsson14c5cbf2009-08-16 07:36:22 +0000762 bool VolatileDest, bool IgnoreResult,
Mike Stump1eb44332009-09-09 15:08:12 +0000763 bool IsInitializer,
Fariborz Jahanian08c32132009-08-31 19:33:16 +0000764 bool RequiresGCollection) {
Chris Lattneree755f92007-08-21 04:59:27 +0000765 assert(E && hasAggregateLLVMType(E->getType()) &&
766 "Invalid aggregate expression to emit");
Mike Stumpe1129a92009-05-26 18:57:45 +0000767 assert ((DestPtr != 0 || VolatileDest == false)
768 && "volatile aggregate can't be 0");
Mike Stump1eb44332009-09-09 15:08:12 +0000769
Fariborz Jahanian08c32132009-08-31 19:33:16 +0000770 AggExprEmitter(*this, DestPtr, VolatileDest, IgnoreResult, IsInitializer,
771 RequiresGCollection)
Mike Stump49d1cd52009-05-26 22:03:21 +0000772 .Visit(const_cast<Expr*>(E));
Chris Lattneree755f92007-08-21 04:59:27 +0000773}
Daniel Dunbar7482d122008-09-09 20:49:46 +0000774
Daniel Dunbar18aba0d2010-02-05 19:38:31 +0000775LValue CodeGenFunction::EmitAggExprToLValue(const Expr *E) {
776 assert(hasAggregateLLVMType(E->getType()) && "Invalid argument!");
777 Qualifiers Q = MakeQualifiers(E->getType());
Daniel Dunbar195337d2010-02-09 02:48:28 +0000778 llvm::Value *Temp = CreateMemTemp(E->getType());
Daniel Dunbar18aba0d2010-02-05 19:38:31 +0000779 EmitAggExpr(E, Temp, Q.hasVolatile());
780 return LValue::MakeAddr(Temp, Q);
781}
782
Daniel Dunbar7482d122008-09-09 20:49:46 +0000783void CodeGenFunction::EmitAggregateCopy(llvm::Value *DestPtr,
Mike Stump27fe2e62009-05-23 22:29:41 +0000784 llvm::Value *SrcPtr, QualType Ty,
785 bool isVolatile) {
Daniel Dunbar7482d122008-09-09 20:49:46 +0000786 assert(!Ty->isAnyComplexType() && "Shouldn't happen for complex");
Mike Stump1eb44332009-09-09 15:08:12 +0000787
Anders Carlsson0d7c5832010-05-03 01:20:20 +0000788 if (getContext().getLangOptions().CPlusPlus) {
789 if (const RecordType *RT = Ty->getAs<RecordType>()) {
Douglas Gregore9979482010-05-20 15:39:01 +0000790 CXXRecordDecl *Record = cast<CXXRecordDecl>(RT->getDecl());
791 assert((Record->hasTrivialCopyConstructor() ||
Fariborz Jahanian1d49f212010-05-20 16:46:55 +0000792 Record->hasTrivialCopyAssignment()) &&
Douglas Gregore9979482010-05-20 15:39:01 +0000793 "Trying to aggregate-copy a type without a trivial copy "
794 "constructor or assignment operator");
Douglas Gregor419aa962010-05-20 15:48:29 +0000795 // Ignore empty classes in C++.
Douglas Gregore9979482010-05-20 15:39:01 +0000796 if (Record->isEmpty())
Anders Carlsson0d7c5832010-05-03 01:20:20 +0000797 return;
798 }
799 }
800
Chris Lattner83c96292009-02-28 18:31:01 +0000801 // Aggregate assignment turns into llvm.memcpy. This is almost valid per
Chris Lattnerca4fc2c2009-02-28 18:18:58 +0000802 // C99 6.5.16.1p3, which states "If the value being stored in an object is
803 // read from another object that overlaps in anyway the storage of the first
804 // object, then the overlap shall be exact and the two objects shall have
805 // qualified or unqualified versions of a compatible type."
806 //
Chris Lattner83c96292009-02-28 18:31:01 +0000807 // memcpy is not defined if the source and destination pointers are exactly
Chris Lattnerca4fc2c2009-02-28 18:18:58 +0000808 // equal, but other compilers do this optimization, and almost every memcpy
809 // implementation handles this case safely. If there is a libc that does not
810 // safely handle this, we can add a target hook.
Benjamin Kramer3c0ef8c2009-10-13 10:07:13 +0000811 const llvm::Type *BP = llvm::Type::getInt8PtrTy(VMContext);
Daniel Dunbar7482d122008-09-09 20:49:46 +0000812 if (DestPtr->getType() != BP)
813 DestPtr = Builder.CreateBitCast(DestPtr, BP, "tmp");
814 if (SrcPtr->getType() != BP)
815 SrcPtr = Builder.CreateBitCast(SrcPtr, BP, "tmp");
Mike Stump1eb44332009-09-09 15:08:12 +0000816
Daniel Dunbar7482d122008-09-09 20:49:46 +0000817 // Get size and alignment info for this aggregate.
818 std::pair<uint64_t, unsigned> TypeInfo = getContext().getTypeInfo(Ty);
Mike Stump1eb44332009-09-09 15:08:12 +0000819
Daniel Dunbar7482d122008-09-09 20:49:46 +0000820 // FIXME: Handle variable sized types.
Owen Anderson0032b272009-08-13 21:57:51 +0000821 const llvm::Type *IntPtr =
822 llvm::IntegerType::get(VMContext, LLVMPointerWidth);
Mike Stump1eb44332009-09-09 15:08:12 +0000823
Mike Stumpfde64202009-05-23 04:13:59 +0000824 // FIXME: If we have a volatile struct, the optimizer can remove what might
825 // appear to be `extra' memory ops:
826 //
827 // volatile struct { int i; } a, b;
828 //
829 // int main() {
830 // a = b;
831 // a = b;
832 // }
833 //
Mon P Wang3ecd7852010-04-04 03:10:52 +0000834 // we need to use a different call here. We use isVolatile to indicate when
Mike Stump49d1cd52009-05-26 22:03:21 +0000835 // either the source or the destination is volatile.
Mon P Wang3ecd7852010-04-04 03:10:52 +0000836 const llvm::Type *I1Ty = llvm::Type::getInt1Ty(VMContext);
837 const llvm::Type *I8Ty = llvm::Type::getInt8Ty(VMContext);
838 const llvm::Type *I32Ty = llvm::Type::getInt32Ty(VMContext);
839
840 const llvm::PointerType *DPT = cast<llvm::PointerType>(DestPtr->getType());
841 const llvm::Type *DBP = llvm::PointerType::get(I8Ty, DPT->getAddressSpace());
842 if (DestPtr->getType() != DBP)
843 DestPtr = Builder.CreateBitCast(DestPtr, DBP, "tmp");
844
845 const llvm::PointerType *SPT = cast<llvm::PointerType>(SrcPtr->getType());
846 const llvm::Type *SBP = llvm::PointerType::get(I8Ty, SPT->getAddressSpace());
847 if (SrcPtr->getType() != SBP)
848 SrcPtr = Builder.CreateBitCast(SrcPtr, SBP, "tmp");
849
Fariborz Jahanian55bcace2010-06-15 22:44:06 +0000850 if (const RecordType *RecordTy = Ty->getAs<RecordType>()) {
851 RecordDecl *Record = RecordTy->getDecl();
852 if (Record->hasObjectMember()) {
853 unsigned long size = TypeInfo.first/8;
854 const llvm::Type *SizeTy = ConvertType(getContext().getSizeType());
855 llvm::Value *SizeVal = llvm::ConstantInt::get(SizeTy, size);
856 CGM.getObjCRuntime().EmitGCMemmoveCollectable(*this, DestPtr, SrcPtr,
857 SizeVal);
858 return;
859 }
860 } else if (getContext().getAsArrayType(Ty)) {
861 QualType BaseType = getContext().getBaseElementType(Ty);
862 if (const RecordType *RecordTy = BaseType->getAs<RecordType>()) {
863 if (RecordTy->getDecl()->hasObjectMember()) {
864 unsigned long size = TypeInfo.first/8;
865 const llvm::Type *SizeTy = ConvertType(getContext().getSizeType());
866 llvm::Value *SizeVal = llvm::ConstantInt::get(SizeTy, size);
867 CGM.getObjCRuntime().EmitGCMemmoveCollectable(*this, DestPtr, SrcPtr,
868 SizeVal);
869 return;
870 }
871 }
872 }
873
Mon P Wang3ecd7852010-04-04 03:10:52 +0000874 Builder.CreateCall5(CGM.getMemCpyFn(DestPtr->getType(), SrcPtr->getType(),
875 IntPtr),
Daniel Dunbar7482d122008-09-09 20:49:46 +0000876 DestPtr, SrcPtr,
877 // TypeInfo.first describes size in bits.
Owen Anderson4a28d5d2009-07-24 23:12:58 +0000878 llvm::ConstantInt::get(IntPtr, TypeInfo.first/8),
Mon P Wang3ecd7852010-04-04 03:10:52 +0000879 llvm::ConstantInt::get(I32Ty, TypeInfo.second/8),
880 llvm::ConstantInt::get(I1Ty, isVolatile));
Daniel Dunbar7482d122008-09-09 20:49:46 +0000881}