blob: f710b036a649173b80e0ca1915397e8e67f37725 [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);
Chris Lattner03d6fb92007-08-21 04:43:17 +000095 void VisitBinAssign(const BinaryOperator *E);
Eli Friedman07fa52a2008-05-20 07:56:31 +000096 void VisitBinComma(const BinaryOperator *E);
Chris Lattner9c033562007-08-21 04:25:47 +000097
Chris Lattner8fdf3282008-06-24 17:04:18 +000098 void VisitObjCMessageExpr(ObjCMessageExpr *E);
Daniel Dunbar0a04d772008-08-23 10:51:21 +000099 void VisitObjCIvarRefExpr(ObjCIvarRefExpr *E) {
100 EmitAggLoadOfLValue(E);
101 }
Daniel Dunbar9c3fc702008-08-27 06:57:25 +0000102 void VisitObjCPropertyRefExpr(ObjCPropertyRefExpr *E);
Fariborz Jahanian09105f52009-08-20 17:02:02 +0000103 void VisitObjCImplicitSetterGetterRefExpr(ObjCImplicitSetterGetterRefExpr *E);
Mike Stump1eb44332009-09-09 15:08:12 +0000104
Chris Lattner9c033562007-08-21 04:25:47 +0000105 void VisitConditionalOperator(const ConditionalOperator *CO);
Anders Carlssona294ca82009-07-08 18:33:14 +0000106 void VisitChooseExpr(const ChooseExpr *CE);
Devang Patel636c3d02007-10-26 17:44:44 +0000107 void VisitInitListExpr(InitListExpr *E);
Chris Lattner04421082008-04-08 04:40:51 +0000108 void VisitCXXDefaultArgExpr(CXXDefaultArgExpr *DAE) {
109 Visit(DAE->getExpr());
110 }
Anders Carlssonb58d0172009-05-30 23:23:33 +0000111 void VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr *E);
Anders Carlsson31ccf372009-05-03 17:47:16 +0000112 void VisitCXXConstructExpr(const CXXConstructExpr *E);
Anders Carlsson7f6ad152009-05-19 04:48:36 +0000113 void VisitCXXExprWithTemporaries(CXXExprWithTemporaries *E);
114
Eli Friedmanb1851242008-05-27 15:51:49 +0000115 void VisitVAArgExpr(VAArgExpr *E);
Chris Lattnerf81557c2008-04-04 18:42:16 +0000116
117 void EmitInitializationToLValue(Expr *E, LValue Address);
118 void EmitNullInitializationToLValue(LValue Address, QualType T);
Chris Lattner9c033562007-08-21 04:25:47 +0000119 // case Expr::ChooseExprClass:
Lauro Ramos Venancio305762c2008-02-18 22:44:02 +0000120
Chris Lattner9c033562007-08-21 04:25:47 +0000121};
122} // end anonymous namespace.
123
Chris Lattneree755f92007-08-21 04:59:27 +0000124//===----------------------------------------------------------------------===//
125// Utilities
126//===----------------------------------------------------------------------===//
Chris Lattner9c033562007-08-21 04:25:47 +0000127
Chris Lattner883f6a72007-08-11 00:04:45 +0000128/// EmitAggLoadOfLValue - Given an expression with aggregate type that
129/// represents a value lvalue, this method emits the address of the lvalue,
130/// then loads the result into DestPtr.
Chris Lattner9c033562007-08-21 04:25:47 +0000131void AggExprEmitter::EmitAggLoadOfLValue(const Expr *E) {
132 LValue LV = CGF.EmitLValue(E);
Mike Stump4ac20dd2009-05-23 20:28:01 +0000133 EmitFinalDestCopy(E, LV);
134}
135
136/// EmitFinalDestCopy - Perform the final copy to DestPtr, if desired.
Mike Stump49d1cd52009-05-26 22:03:21 +0000137void AggExprEmitter::EmitFinalDestCopy(const Expr *E, RValue Src, bool Ignore) {
Mike Stump4ac20dd2009-05-23 20:28:01 +0000138 assert(Src.isAggregate() && "value must be aggregate value!");
139
Chris Lattner883f6a72007-08-11 00:04:45 +0000140 // If the result is ignored, don't copy from the value.
Mike Stump9ccb1032009-05-23 22:01:27 +0000141 if (DestPtr == 0) {
Mike Stump49d1cd52009-05-26 22:03:21 +0000142 if (!Src.isVolatileQualified() || (IgnoreResult && Ignore))
Mike Stump9ccb1032009-05-23 22:01:27 +0000143 return;
Mike Stump49d1cd52009-05-26 22:03:21 +0000144 // If the source is volatile, we must read from it; to do that, we need
145 // some place to put it.
146 DestPtr = CGF.CreateTempAlloca(CGF.ConvertType(E->getType()), "agg.tmp");
Mike Stump9ccb1032009-05-23 22:01:27 +0000147 }
Chris Lattner883f6a72007-08-11 00:04:45 +0000148
Fariborz Jahanian08c32132009-08-31 19:33:16 +0000149 if (RequiresGCollection) {
150 CGF.CGM.getObjCRuntime().EmitGCMemmoveCollectable(CGF,
151 DestPtr, Src.getAggregateAddr(),
152 E->getType());
153 return;
154 }
Mike Stump4ac20dd2009-05-23 20:28:01 +0000155 // If the result of the assignment is used, copy the LHS there also.
156 // FIXME: Pass VolatileDest as well. I think we also need to merge volatile
157 // from the source as well, as we can't eliminate it if either operand
158 // is volatile, unless copy has volatile for both source and destination..
Mike Stump27fe2e62009-05-23 22:29:41 +0000159 CGF.EmitAggregateCopy(DestPtr, Src.getAggregateAddr(), E->getType(),
160 VolatileDest|Src.isVolatileQualified());
Mike Stump4ac20dd2009-05-23 20:28:01 +0000161}
162
163/// EmitFinalDestCopy - Perform the final copy to DestPtr, if desired.
Mike Stump49d1cd52009-05-26 22:03:21 +0000164void AggExprEmitter::EmitFinalDestCopy(const Expr *E, LValue Src, bool Ignore) {
Mike Stump4ac20dd2009-05-23 20:28:01 +0000165 assert(Src.isSimple() && "Can't have aggregate bitfield, vector, etc");
166
167 EmitFinalDestCopy(E, RValue::getAggregate(Src.getAddress(),
Mike Stump49d1cd52009-05-26 22:03:21 +0000168 Src.isVolatileQualified()),
169 Ignore);
Chris Lattner883f6a72007-08-11 00:04:45 +0000170}
171
Chris Lattneree755f92007-08-21 04:59:27 +0000172//===----------------------------------------------------------------------===//
173// Visitor Methods
174//===----------------------------------------------------------------------===//
175
Anders Carlsson4d8673b2009-08-07 23:22:37 +0000176void AggExprEmitter::VisitCastExpr(CastExpr *E) {
Anders Carlsson30168422009-09-29 01:23:39 +0000177 switch (E->getCastKind()) {
178 default: assert(0 && "Unhandled cast kind!");
179
180 case CastExpr::CK_ToUnion: {
Anders Carlsson4d8673b2009-08-07 23:22:37 +0000181 // GCC union extension
Eli Friedman34ebf4d2009-06-03 20:45:06 +0000182 QualType PtrTy =
Anders Carlsson30168422009-09-29 01:23:39 +0000183 CGF.getContext().getPointerType(E->getSubExpr()->getType());
Eli Friedman34ebf4d2009-06-03 20:45:06 +0000184 llvm::Value *CastPtr = Builder.CreateBitCast(DestPtr,
185 CGF.ConvertType(PtrTy));
Mon P Wangc6a38a42009-07-22 03:08:17 +0000186 EmitInitializationToLValue(E->getSubExpr(),
John McCall0953e762009-09-24 19:53:00 +0000187 LValue::MakeAddr(CastPtr, Qualifiers()));
Anders Carlsson30168422009-09-29 01:23:39 +0000188 break;
Nuno Lopes7e916272009-01-15 20:14:33 +0000189 }
Mike Stump1eb44332009-09-09 15:08:12 +0000190
Anders Carlsson4d8673b2009-08-07 23:22:37 +0000191 // FIXME: Remove the CK_Unknown check here.
Anders Carlsson30168422009-09-29 01:23:39 +0000192 case CastExpr::CK_Unknown:
193 case CastExpr::CK_NoOp:
194 case CastExpr::CK_UserDefinedConversion:
195 case CastExpr::CK_ConstructorConversion:
196 assert(CGF.getContext().hasSameUnqualifiedType(E->getSubExpr()->getType(),
197 E->getType()) &&
198 "Implicit cast types must be compatible");
199 Visit(E->getSubExpr());
200 break;
201 }
Anders Carlssone4707ff2008-01-14 06:28:57 +0000202}
203
Chris Lattner96196622008-07-26 22:37:01 +0000204void AggExprEmitter::VisitCallExpr(const CallExpr *E) {
Anders Carlssone70e8f72009-05-27 16:45:02 +0000205 if (E->getCallReturnType()->isReferenceType()) {
206 EmitAggLoadOfLValue(E);
207 return;
208 }
Mike Stump1eb44332009-09-09 15:08:12 +0000209
Anders Carlsson148fe672007-10-31 22:04:46 +0000210 RValue RV = CGF.EmitCallExpr(E);
Mike Stump4ac20dd2009-05-23 20:28:01 +0000211 EmitFinalDestCopy(E, RV);
Anders Carlsson148fe672007-10-31 22:04:46 +0000212}
Chris Lattner96196622008-07-26 22:37:01 +0000213
214void AggExprEmitter::VisitObjCMessageExpr(ObjCMessageExpr *E) {
Daniel Dunbar8f2926b2008-08-23 03:46:30 +0000215 RValue RV = CGF.EmitObjCMessageExpr(E);
Mike Stump4ac20dd2009-05-23 20:28:01 +0000216 EmitFinalDestCopy(E, RV);
Chris Lattner8fdf3282008-06-24 17:04:18 +0000217}
Anders Carlsson148fe672007-10-31 22:04:46 +0000218
Daniel Dunbar9c3fc702008-08-27 06:57:25 +0000219void AggExprEmitter::VisitObjCPropertyRefExpr(ObjCPropertyRefExpr *E) {
220 RValue RV = CGF.EmitObjCPropertyGet(E);
Mike Stump4ac20dd2009-05-23 20:28:01 +0000221 EmitFinalDestCopy(E, RV);
Daniel Dunbar9c3fc702008-08-27 06:57:25 +0000222}
223
Fariborz Jahanian09105f52009-08-20 17:02:02 +0000224void AggExprEmitter::VisitObjCImplicitSetterGetterRefExpr(
225 ObjCImplicitSetterGetterRefExpr *E) {
Fariborz Jahanian5daf5702008-11-22 18:39:36 +0000226 RValue RV = CGF.EmitObjCPropertyGet(E);
Mike Stump4ac20dd2009-05-23 20:28:01 +0000227 EmitFinalDestCopy(E, RV);
Fariborz Jahanian5daf5702008-11-22 18:39:36 +0000228}
229
Chris Lattner96196622008-07-26 22:37:01 +0000230void AggExprEmitter::VisitBinComma(const BinaryOperator *E) {
Mike Stump7f79f9b2009-05-29 15:46:01 +0000231 CGF.EmitAnyExpr(E->getLHS(), 0, false, true);
Anders Carlsson14c5cbf2009-08-16 07:36:22 +0000232 CGF.EmitAggExpr(E->getRHS(), DestPtr, VolatileDest,
233 /*IgnoreResult=*/false, IsInitializer);
Eli Friedman07fa52a2008-05-20 07:56:31 +0000234}
235
Chris Lattnerb2d963f2007-08-31 22:54:14 +0000236void AggExprEmitter::VisitStmtExpr(const StmtExpr *E) {
237 CGF.EmitCompoundStmt(*E->getSubStmt(), true, DestPtr, VolatileDest);
238}
239
Chris Lattner9c033562007-08-21 04:25:47 +0000240void AggExprEmitter::VisitBinaryOperator(const BinaryOperator *E) {
Daniel Dunbar488e9932008-08-16 00:56:44 +0000241 CGF.ErrorUnsupported(E, "aggregate binary expression");
Chris Lattneree755f92007-08-21 04:59:27 +0000242}
243
Chris Lattner03d6fb92007-08-21 04:43:17 +0000244void AggExprEmitter::VisitBinAssign(const BinaryOperator *E) {
Eli Friedmanff6e2b72008-02-11 01:09:17 +0000245 // For an assignment to work, the value on the right has
246 // to be compatible with the value on the left.
Eli Friedman2dce5f82009-05-28 23:04:00 +0000247 assert(CGF.getContext().hasSameUnqualifiedType(E->getLHS()->getType(),
248 E->getRHS()->getType())
Eli Friedmanff6e2b72008-02-11 01:09:17 +0000249 && "Invalid assignment");
Chris Lattner9c033562007-08-21 04:25:47 +0000250 LValue LHS = CGF.EmitLValue(E->getLHS());
Chris Lattner883f6a72007-08-11 00:04:45 +0000251
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000252 // We have to special case property setters, otherwise we must have
253 // a simple lvalue (no aggregates inside vectors, bitfields).
254 if (LHS.isPropertyRef()) {
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000255 llvm::Value *AggLoc = DestPtr;
256 if (!AggLoc)
257 AggLoc = CGF.CreateTempAlloca(CGF.ConvertType(E->getRHS()->getType()));
Mike Stump240993d2009-05-23 23:48:13 +0000258 CGF.EmitAggExpr(E->getRHS(), AggLoc, VolatileDest);
Mike Stump1eb44332009-09-09 15:08:12 +0000259 CGF.EmitObjCPropertySet(LHS.getPropertyRefExpr(),
Mike Stump240993d2009-05-23 23:48:13 +0000260 RValue::getAggregate(AggLoc, VolatileDest));
Mike Stumpb3589f42009-07-30 22:28:39 +0000261 } else if (LHS.isKVCRef()) {
Fariborz Jahanian43f44702008-11-22 22:30:21 +0000262 llvm::Value *AggLoc = DestPtr;
263 if (!AggLoc)
264 AggLoc = CGF.CreateTempAlloca(CGF.ConvertType(E->getRHS()->getType()));
Mike Stumpa49af1a2009-05-23 23:52:31 +0000265 CGF.EmitAggExpr(E->getRHS(), AggLoc, VolatileDest);
Mike Stump1eb44332009-09-09 15:08:12 +0000266 CGF.EmitObjCPropertySet(LHS.getKVCRefExpr(),
Mike Stumpa49af1a2009-05-23 23:52:31 +0000267 RValue::getAggregate(AggLoc, VolatileDest));
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000268 } else {
Fariborz Jahanian08c32132009-08-31 19:33:16 +0000269 bool RequiresGCollection = false;
Fariborz Jahanian082b02e2009-07-08 01:18:33 +0000270 if (CGF.getContext().getLangOptions().NeXTRuntime) {
271 QualType LHSTy = E->getLHS()->getType();
Ted Kremenek6217b802009-07-29 21:53:49 +0000272 if (const RecordType *FDTTy = LHSTy.getTypePtr()->getAs<RecordType>())
Mike Stump1eb44332009-09-09 15:08:12 +0000273 RequiresGCollection = FDTTy->getDecl()->hasObjectMember();
Fariborz Jahanian082b02e2009-07-08 01:18:33 +0000274 }
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000275 // Codegen the RHS so that it stores directly into the LHS.
Fariborz Jahanian08c32132009-08-31 19:33:16 +0000276 CGF.EmitAggExpr(E->getRHS(), LHS.getAddress(), LHS.isVolatileQualified(),
277 false, false, RequiresGCollection);
Mike Stump49d1cd52009-05-26 22:03:21 +0000278 EmitFinalDestCopy(E, LHS, true);
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000279 }
Chris Lattner883f6a72007-08-11 00:04:45 +0000280}
281
Chris Lattner9c033562007-08-21 04:25:47 +0000282void AggExprEmitter::VisitConditionalOperator(const ConditionalOperator *E) {
Daniel Dunbar9615ecb2008-11-13 01:38:36 +0000283 llvm::BasicBlock *LHSBlock = CGF.createBasicBlock("cond.true");
284 llvm::BasicBlock *RHSBlock = CGF.createBasicBlock("cond.false");
285 llvm::BasicBlock *ContBlock = CGF.createBasicBlock("cond.end");
Mike Stump1eb44332009-09-09 15:08:12 +0000286
Chris Lattner9c033562007-08-21 04:25:47 +0000287 llvm::Value *Cond = CGF.EvaluateExprAsBool(E->getCond());
Chris Lattnerbfc0c1a2007-08-26 23:13:56 +0000288 Builder.CreateCondBr(Cond, LHSBlock, RHSBlock);
Mike Stump1eb44332009-09-09 15:08:12 +0000289
Anders Carlssonfb6fa302009-06-04 03:00:32 +0000290 CGF.PushConditionalTempDestruction();
Chris Lattner9c033562007-08-21 04:25:47 +0000291 CGF.EmitBlock(LHSBlock);
Mike Stump1eb44332009-09-09 15:08:12 +0000292
Chris Lattner883f6a72007-08-11 00:04:45 +0000293 // Handle the GNU extension for missing LHS.
294 assert(E->getLHS() && "Must have LHS for aggregate value");
295
Chris Lattnerc748f272007-08-21 05:02:10 +0000296 Visit(E->getLHS());
Anders Carlssonfb6fa302009-06-04 03:00:32 +0000297 CGF.PopConditionalTempDestruction();
Daniel Dunbard57a8712008-11-11 09:41:28 +0000298 CGF.EmitBranch(ContBlock);
Mike Stump1eb44332009-09-09 15:08:12 +0000299
Anders Carlssonfb6fa302009-06-04 03:00:32 +0000300 CGF.PushConditionalTempDestruction();
Chris Lattner9c033562007-08-21 04:25:47 +0000301 CGF.EmitBlock(RHSBlock);
Mike Stump1eb44332009-09-09 15:08:12 +0000302
Chris Lattnerc748f272007-08-21 05:02:10 +0000303 Visit(E->getRHS());
Anders Carlssonfb6fa302009-06-04 03:00:32 +0000304 CGF.PopConditionalTempDestruction();
Daniel Dunbard57a8712008-11-11 09:41:28 +0000305 CGF.EmitBranch(ContBlock);
Mike Stump1eb44332009-09-09 15:08:12 +0000306
Chris Lattner9c033562007-08-21 04:25:47 +0000307 CGF.EmitBlock(ContBlock);
Chris Lattner883f6a72007-08-11 00:04:45 +0000308}
Chris Lattneree755f92007-08-21 04:59:27 +0000309
Anders Carlssona294ca82009-07-08 18:33:14 +0000310void AggExprEmitter::VisitChooseExpr(const ChooseExpr *CE) {
311 Visit(CE->getChosenSubExpr(CGF.getContext()));
312}
313
Eli Friedmanb1851242008-05-27 15:51:49 +0000314void AggExprEmitter::VisitVAArgExpr(VAArgExpr *VE) {
Daniel Dunbar07855702009-02-11 22:25:55 +0000315 llvm::Value *ArgValue = CGF.EmitVAListRef(VE->getSubExpr());
Anders Carlssonddf7cac2008-11-04 05:30:00 +0000316 llvm::Value *ArgPtr = CGF.EmitVAArg(ArgValue, VE->getType());
317
Sebastian Redl0262f022009-01-09 21:09:38 +0000318 if (!ArgPtr) {
Anders Carlssonddf7cac2008-11-04 05:30:00 +0000319 CGF.ErrorUnsupported(VE, "aggregate va_arg expression");
Sebastian Redl0262f022009-01-09 21:09:38 +0000320 return;
321 }
322
John McCall0953e762009-09-24 19:53:00 +0000323 EmitFinalDestCopy(VE, LValue::MakeAddr(ArgPtr, Qualifiers()));
Eli Friedmanb1851242008-05-27 15:51:49 +0000324}
325
Anders Carlssonb58d0172009-05-30 23:23:33 +0000326void AggExprEmitter::VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr *E) {
327 llvm::Value *Val = DestPtr;
Mike Stump1eb44332009-09-09 15:08:12 +0000328
Anders Carlssonb58d0172009-05-30 23:23:33 +0000329 if (!Val) {
330 // Create a temporary variable.
331 Val = CGF.CreateTempAlloca(CGF.ConvertTypeForMem(E->getType()), "tmp");
332
333 // FIXME: volatile
334 CGF.EmitAggExpr(E->getSubExpr(), Val, false);
Mike Stump1eb44332009-09-09 15:08:12 +0000335 } else
Anders Carlssonb58d0172009-05-30 23:23:33 +0000336 Visit(E->getSubExpr());
Mike Stump1eb44332009-09-09 15:08:12 +0000337
Anders Carlsson14c5cbf2009-08-16 07:36:22 +0000338 // Don't make this a live temporary if we're emitting an initializer expr.
339 if (!IsInitializer)
340 CGF.PushCXXTemporary(E->getTemporary(), Val);
Anders Carlssonb58d0172009-05-30 23:23:33 +0000341}
342
Anders Carlssonb14095a2009-04-17 00:06:03 +0000343void
Anders Carlsson31ccf372009-05-03 17:47:16 +0000344AggExprEmitter::VisitCXXConstructExpr(const CXXConstructExpr *E) {
Anders Carlssonb58d0172009-05-30 23:23:33 +0000345 llvm::Value *Val = DestPtr;
Mike Stump1eb44332009-09-09 15:08:12 +0000346
Anders Carlssonb58d0172009-05-30 23:23:33 +0000347 if (!Val) {
348 // Create a temporary variable.
349 Val = CGF.CreateTempAlloca(CGF.ConvertTypeForMem(E->getType()), "tmp");
350 }
Anders Carlsson8e587a12009-05-30 20:56:46 +0000351
Anders Carlssonb58d0172009-05-30 23:23:33 +0000352 CGF.EmitCXXConstructExpr(Val, E);
Anders Carlsson7f6ad152009-05-19 04:48:36 +0000353}
354
355void AggExprEmitter::VisitCXXExprWithTemporaries(CXXExprWithTemporaries *E) {
Anders Carlsson14c5cbf2009-08-16 07:36:22 +0000356 CGF.EmitCXXExprWithTemporaries(E, DestPtr, VolatileDest, IsInitializer);
Anders Carlssonb14095a2009-04-17 00:06:03 +0000357}
358
Chris Lattnerf81557c2008-04-04 18:42:16 +0000359void AggExprEmitter::EmitInitializationToLValue(Expr* E, LValue LV) {
Mike Stump7f79f9b2009-05-29 15:46:01 +0000360 // FIXME: Ignore result?
Chris Lattnerf81557c2008-04-04 18:42:16 +0000361 // FIXME: Are initializers affected by volatile?
Douglas Gregor3498bdb2009-01-29 17:44:32 +0000362 if (isa<ImplicitValueInitExpr>(E)) {
Douglas Gregor4c678342009-01-28 21:54:33 +0000363 EmitNullInitializationToLValue(LV, E->getType());
Douglas Gregor3498bdb2009-01-29 17:44:32 +0000364 } else if (E->getType()->isComplexType()) {
365 CGF.EmitComplexExprIntoAddr(E, LV.getAddress(), false);
Eli Friedmanc8ba9612008-05-12 15:06:05 +0000366 } else if (CGF.hasAggregateLLVMType(E->getType())) {
367 CGF.EmitAnyExpr(E, LV.getAddress(), false);
368 } else {
369 CGF.EmitStoreThroughLValue(CGF.EmitAnyExpr(E), LV, E->getType());
Chris Lattnerf81557c2008-04-04 18:42:16 +0000370 }
Chris Lattnerf81557c2008-04-04 18:42:16 +0000371}
372
373void AggExprEmitter::EmitNullInitializationToLValue(LValue LV, QualType T) {
374 if (!CGF.hasAggregateLLVMType(T)) {
375 // For non-aggregates, we can store zero
Owen Andersonc9c88b42009-07-31 20:28:54 +0000376 llvm::Value *Null = llvm::Constant::getNullValue(CGF.ConvertType(T));
Daniel Dunbar82397132008-08-06 05:32:55 +0000377 CGF.EmitStoreThroughLValue(RValue::get(Null), LV, T);
Lauro Ramos Venancio145cd892008-02-19 19:27:31 +0000378 } else {
Chris Lattnerf81557c2008-04-04 18:42:16 +0000379 // Otherwise, just memset the whole thing to zero. This is legal
380 // because in LLVM, all default initializers are guaranteed to have a
381 // bit pattern of all zeros.
Eli Friedman0f593122009-04-13 21:47:26 +0000382 // FIXME: That isn't true for member pointers!
Chris Lattnerf81557c2008-04-04 18:42:16 +0000383 // There's a potential optimization opportunity in combining
384 // memsets; that would be easy for arrays, but relatively
385 // difficult for structures with the current code.
Eli Friedmanccf0ed82009-03-28 03:10:45 +0000386 CGF.EmitMemSetToZero(LV.getAddress(), T);
Chris Lattnerf81557c2008-04-04 18:42:16 +0000387 }
388}
389
Chris Lattnerf81557c2008-04-04 18:42:16 +0000390void AggExprEmitter::VisitInitListExpr(InitListExpr *E) {
Eli Friedmana385b3c2008-12-02 01:17:45 +0000391#if 0
Mike Stump1eb44332009-09-09 15:08:12 +0000392 // FIXME: Disabled while we figure out what to do about
Eli Friedmana385b3c2008-12-02 01:17:45 +0000393 // test/CodeGen/bitfield.c
394 //
Mike Stumpf5408fe2009-05-16 07:57:57 +0000395 // If we can, prefer a copy from a global; this is a lot less code for long
396 // globals, and it's easier for the current optimizers to analyze.
397 // FIXME: Should we really be doing this? Should we try to avoid cases where
398 // we emit a global with a lot of zeros? Should we try to avoid short
399 // globals?
Eli Friedmanc9e8f602009-01-25 02:32:41 +0000400 if (E->isConstantInitializer(CGF.getContext(), 0)) {
Eli Friedman994ffef2008-11-30 02:11:09 +0000401 llvm::Constant* C = CGF.CGM.EmitConstantExpr(E, &CGF);
402 llvm::GlobalVariable* GV =
403 new llvm::GlobalVariable(C->getType(), true,
404 llvm::GlobalValue::InternalLinkage,
405 C, "", &CGF.CGM.getModule(), 0);
Mike Stump4ac20dd2009-05-23 20:28:01 +0000406 EmitFinalDestCopy(E, LValue::MakeAddr(GV, 0));
Eli Friedman994ffef2008-11-30 02:11:09 +0000407 return;
408 }
Eli Friedmana385b3c2008-12-02 01:17:45 +0000409#endif
Douglas Gregora9c87802009-01-29 19:42:23 +0000410 if (E->hadArrayRangeDesignator()) {
411 CGF.ErrorUnsupported(E, "GNU array range designator extension");
412 }
413
Chris Lattnerf81557c2008-04-04 18:42:16 +0000414 // Handle initialization of an array.
415 if (E->getType()->isArrayType()) {
416 const llvm::PointerType *APType =
417 cast<llvm::PointerType>(DestPtr->getType());
418 const llvm::ArrayType *AType =
419 cast<llvm::ArrayType>(APType->getElementType());
Mike Stump1eb44332009-09-09 15:08:12 +0000420
Chris Lattnerf81557c2008-04-04 18:42:16 +0000421 uint64_t NumInitElements = E->getNumInits();
Eli Friedman922696f2008-05-19 17:51:16 +0000422
Chris Lattner96196622008-07-26 22:37:01 +0000423 if (E->getNumInits() > 0) {
424 QualType T1 = E->getType();
425 QualType T2 = E->getInit(0)->getType();
Eli Friedman2dce5f82009-05-28 23:04:00 +0000426 if (CGF.getContext().hasSameUnqualifiedType(T1, T2)) {
Chris Lattner96196622008-07-26 22:37:01 +0000427 EmitAggLoadOfLValue(E->getInit(0));
428 return;
429 }
Eli Friedman922696f2008-05-19 17:51:16 +0000430 }
431
Chris Lattnerf81557c2008-04-04 18:42:16 +0000432 uint64_t NumArrayElements = AType->getNumElements();
Chris Lattnerc63a1f22008-08-04 07:31:14 +0000433 QualType ElementType = CGF.getContext().getCanonicalType(E->getType());
Douglas Gregor4c678342009-01-28 21:54:33 +0000434 ElementType = CGF.getContext().getAsArrayType(ElementType)->getElementType();
Mike Stump1eb44332009-09-09 15:08:12 +0000435
John McCall0953e762009-09-24 19:53:00 +0000436 // FIXME: were we intentionally ignoring address spaces and GC attributes?
437 Qualifiers Quals = CGF.MakeQualifiers(ElementType);
Eli Friedman1e692ac2008-06-13 23:01:12 +0000438
Chris Lattnerf81557c2008-04-04 18:42:16 +0000439 for (uint64_t i = 0; i != NumArrayElements; ++i) {
440 llvm::Value *NextVal = Builder.CreateStructGEP(DestPtr, i, ".array");
441 if (i < NumInitElements)
Eli Friedman1e692ac2008-06-13 23:01:12 +0000442 EmitInitializationToLValue(E->getInit(i),
John McCall0953e762009-09-24 19:53:00 +0000443 LValue::MakeAddr(NextVal, Quals));
Chris Lattnerf81557c2008-04-04 18:42:16 +0000444 else
John McCall0953e762009-09-24 19:53:00 +0000445 EmitNullInitializationToLValue(LValue::MakeAddr(NextVal, Quals),
Chris Lattnerf81557c2008-04-04 18:42:16 +0000446 ElementType);
Lauro Ramos Venancio145cd892008-02-19 19:27:31 +0000447 }
Chris Lattnerf81557c2008-04-04 18:42:16 +0000448 return;
449 }
Mike Stump1eb44332009-09-09 15:08:12 +0000450
Chris Lattnerf81557c2008-04-04 18:42:16 +0000451 assert(E->getType()->isRecordType() && "Only support structs/unions here!");
Mike Stump1eb44332009-09-09 15:08:12 +0000452
Chris Lattnerf81557c2008-04-04 18:42:16 +0000453 // Do struct initialization; this code just sets each individual member
454 // to the approprate value. This makes bitfield support automatic;
455 // the disadvantage is that the generated code is more difficult for
456 // the optimizer, especially with bitfields.
457 unsigned NumInitElements = E->getNumInits();
Ted Kremenek6217b802009-07-29 21:53:49 +0000458 RecordDecl *SD = E->getType()->getAs<RecordType>()->getDecl();
Chris Lattnerf81557c2008-04-04 18:42:16 +0000459 unsigned CurInitVal = 0;
Douglas Gregor0bb76892009-01-29 16:53:55 +0000460
461 if (E->getType()->isUnionType()) {
462 // Only initialize one field of a union. The field itself is
463 // specified by the initializer list.
464 if (!E->getInitializedFieldInUnion()) {
465 // Empty union; we have nothing to do.
Mike Stump1eb44332009-09-09 15:08:12 +0000466
Douglas Gregor0bb76892009-01-29 16:53:55 +0000467#ifndef NDEBUG
468 // Make sure that it's really an empty and not a failure of
469 // semantic analysis.
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000470 for (RecordDecl::field_iterator Field = SD->field_begin(),
471 FieldEnd = SD->field_end();
Douglas Gregor0bb76892009-01-29 16:53:55 +0000472 Field != FieldEnd; ++Field)
473 assert(Field->isUnnamedBitfield() && "Only unnamed bitfields allowed");
474#endif
475 return;
476 }
477
478 // FIXME: volatility
479 FieldDecl *Field = E->getInitializedFieldInUnion();
480 LValue FieldLoc = CGF.EmitLValueForField(DestPtr, Field, true, 0);
481
482 if (NumInitElements) {
483 // Store the initializer into the field
484 EmitInitializationToLValue(E->getInit(0), FieldLoc);
485 } else {
486 // Default-initialize to null
487 EmitNullInitializationToLValue(FieldLoc, Field->getType());
488 }
489
490 return;
491 }
Mike Stump1eb44332009-09-09 15:08:12 +0000492
Chris Lattnerf81557c2008-04-04 18:42:16 +0000493 // Here we iterate over the fields; this makes it simpler to both
494 // default-initialize fields and skip over unnamed fields.
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000495 for (RecordDecl::field_iterator Field = SD->field_begin(),
496 FieldEnd = SD->field_end();
Douglas Gregor44b43212008-12-11 16:49:14 +0000497 Field != FieldEnd; ++Field) {
498 // We're done once we hit the flexible array member
499 if (Field->getType()->isIncompleteArrayType())
500 break;
501
Douglas Gregor34e79462009-01-28 23:36:17 +0000502 if (Field->isUnnamedBitfield())
Chris Lattnerf81557c2008-04-04 18:42:16 +0000503 continue;
Douglas Gregor34e79462009-01-28 23:36:17 +0000504
Eli Friedman1e692ac2008-06-13 23:01:12 +0000505 // FIXME: volatility
Douglas Gregor0bb76892009-01-29 16:53:55 +0000506 LValue FieldLoc = CGF.EmitLValueForField(DestPtr, *Field, false, 0);
Fariborz Jahanian14674ff2009-05-27 19:54:11 +0000507 // We never generate write-barries for initialized fields.
508 LValue::SetObjCNonGC(FieldLoc, true);
Chris Lattnerf81557c2008-04-04 18:42:16 +0000509 if (CurInitVal < NumInitElements) {
510 // Store the initializer into the field
Chris Lattnerf81557c2008-04-04 18:42:16 +0000511 EmitInitializationToLValue(E->getInit(CurInitVal++), FieldLoc);
512 } else {
513 // We're out of initalizers; default-initialize to null
Douglas Gregor44b43212008-12-11 16:49:14 +0000514 EmitNullInitializationToLValue(FieldLoc, Field->getType());
Chris Lattnerf81557c2008-04-04 18:42:16 +0000515 }
Lauro Ramos Venancio145cd892008-02-19 19:27:31 +0000516 }
Devang Patel636c3d02007-10-26 17:44:44 +0000517}
518
Chris Lattneree755f92007-08-21 04:59:27 +0000519//===----------------------------------------------------------------------===//
520// Entry Points into this File
521//===----------------------------------------------------------------------===//
522
Mike Stumpe1129a92009-05-26 18:57:45 +0000523/// EmitAggExpr - Emit the computation of the specified expression of aggregate
524/// type. The result is computed into DestPtr. Note that if DestPtr is null,
525/// the value of the aggregate expression is not needed. If VolatileDest is
526/// true, DestPtr cannot be 0.
Chris Lattneree755f92007-08-21 04:59:27 +0000527void CodeGenFunction::EmitAggExpr(const Expr *E, llvm::Value *DestPtr,
Anders Carlsson14c5cbf2009-08-16 07:36:22 +0000528 bool VolatileDest, bool IgnoreResult,
Mike Stump1eb44332009-09-09 15:08:12 +0000529 bool IsInitializer,
Fariborz Jahanian08c32132009-08-31 19:33:16 +0000530 bool RequiresGCollection) {
Chris Lattneree755f92007-08-21 04:59:27 +0000531 assert(E && hasAggregateLLVMType(E->getType()) &&
532 "Invalid aggregate expression to emit");
Mike Stumpe1129a92009-05-26 18:57:45 +0000533 assert ((DestPtr != 0 || VolatileDest == false)
534 && "volatile aggregate can't be 0");
Mike Stump1eb44332009-09-09 15:08:12 +0000535
Fariborz Jahanian08c32132009-08-31 19:33:16 +0000536 AggExprEmitter(*this, DestPtr, VolatileDest, IgnoreResult, IsInitializer,
537 RequiresGCollection)
Mike Stump49d1cd52009-05-26 22:03:21 +0000538 .Visit(const_cast<Expr*>(E));
Chris Lattneree755f92007-08-21 04:59:27 +0000539}
Daniel Dunbar7482d122008-09-09 20:49:46 +0000540
541void CodeGenFunction::EmitAggregateClear(llvm::Value *DestPtr, QualType Ty) {
542 assert(!Ty->isAnyComplexType() && "Shouldn't happen for complex");
543
544 EmitMemSetToZero(DestPtr, Ty);
545}
546
547void CodeGenFunction::EmitAggregateCopy(llvm::Value *DestPtr,
Mike Stump27fe2e62009-05-23 22:29:41 +0000548 llvm::Value *SrcPtr, QualType Ty,
549 bool isVolatile) {
Daniel Dunbar7482d122008-09-09 20:49:46 +0000550 assert(!Ty->isAnyComplexType() && "Shouldn't happen for complex");
Mike Stump1eb44332009-09-09 15:08:12 +0000551
Chris Lattner83c96292009-02-28 18:31:01 +0000552 // Aggregate assignment turns into llvm.memcpy. This is almost valid per
Chris Lattnerca4fc2c2009-02-28 18:18:58 +0000553 // C99 6.5.16.1p3, which states "If the value being stored in an object is
554 // read from another object that overlaps in anyway the storage of the first
555 // object, then the overlap shall be exact and the two objects shall have
556 // qualified or unqualified versions of a compatible type."
557 //
Chris Lattner83c96292009-02-28 18:31:01 +0000558 // memcpy is not defined if the source and destination pointers are exactly
Chris Lattnerca4fc2c2009-02-28 18:18:58 +0000559 // equal, but other compilers do this optimization, and almost every memcpy
560 // implementation handles this case safely. If there is a libc that does not
561 // safely handle this, we can add a target hook.
Owen Anderson0032b272009-08-13 21:57:51 +0000562 const llvm::Type *BP =
563 llvm::PointerType::getUnqual(llvm::Type::getInt8Ty(VMContext));
Daniel Dunbar7482d122008-09-09 20:49:46 +0000564 if (DestPtr->getType() != BP)
565 DestPtr = Builder.CreateBitCast(DestPtr, BP, "tmp");
566 if (SrcPtr->getType() != BP)
567 SrcPtr = Builder.CreateBitCast(SrcPtr, BP, "tmp");
Mike Stump1eb44332009-09-09 15:08:12 +0000568
Daniel Dunbar7482d122008-09-09 20:49:46 +0000569 // Get size and alignment info for this aggregate.
570 std::pair<uint64_t, unsigned> TypeInfo = getContext().getTypeInfo(Ty);
Mike Stump1eb44332009-09-09 15:08:12 +0000571
Daniel Dunbar7482d122008-09-09 20:49:46 +0000572 // FIXME: Handle variable sized types.
Owen Anderson0032b272009-08-13 21:57:51 +0000573 const llvm::Type *IntPtr =
574 llvm::IntegerType::get(VMContext, LLVMPointerWidth);
Mike Stump1eb44332009-09-09 15:08:12 +0000575
Mike Stumpfde64202009-05-23 04:13:59 +0000576 // FIXME: If we have a volatile struct, the optimizer can remove what might
577 // appear to be `extra' memory ops:
578 //
579 // volatile struct { int i; } a, b;
580 //
581 // int main() {
582 // a = b;
583 // a = b;
584 // }
585 //
Mike Stump49d1cd52009-05-26 22:03:21 +0000586 // we need to use a differnt call here. We use isVolatile to indicate when
587 // either the source or the destination is volatile.
Chris Lattnerca4fc2c2009-02-28 18:18:58 +0000588 Builder.CreateCall4(CGM.getMemCpyFn(),
Daniel Dunbar7482d122008-09-09 20:49:46 +0000589 DestPtr, SrcPtr,
590 // TypeInfo.first describes size in bits.
Owen Anderson4a28d5d2009-07-24 23:12:58 +0000591 llvm::ConstantInt::get(IntPtr, TypeInfo.first/8),
Mike Stump1eb44332009-09-09 15:08:12 +0000592 llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext),
Daniel Dunbar7482d122008-09-09 20:49:46 +0000593 TypeInfo.second/8));
594}