blob: 3898d1644780af1b5667fc636b7317a4cc7dedd1 [file] [log] [blame]
Chris Lattner820dce82007-08-24 02:22:53 +00001//===--- CGExprAgg.cpp - Emit LLVM Code from Aggregate Expressions --------===//
Chris Lattner78ed8402007-08-10 20:13:28 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner959e5be2007-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 Lattner78ed8402007-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 Lattnerbdb8ffb2007-08-11 00:04:45 +000015#include "CodeGenModule.h"
Fariborz Jahanian614e8f02009-07-08 01:18:33 +000016#include "CGObjCRuntime.h"
Daniel Dunbareee5cd12008-08-11 05:00:27 +000017#include "clang/AST/ASTContext.h"
Anders Carlsson72f48292009-04-17 00:06:03 +000018#include "clang/AST/DeclCXX.h"
Daniel Dunbareee5cd12008-08-11 05:00:27 +000019#include "clang/AST/StmtVisitor.h"
Chris Lattnerbdb8ffb2007-08-11 00:04:45 +000020#include "llvm/Constants.h"
21#include "llvm/Function.h"
Devang Patelddde6d52007-10-26 17:44:44 +000022#include "llvm/GlobalVariable.h"
Chris Lattnerb50e3902007-08-21 04:25:47 +000023#include "llvm/Support/Compiler.h"
Chris Lattner6ee6ab02008-04-04 18:42:16 +000024#include "llvm/Intrinsics.h"
Chris Lattner78ed8402007-08-10 20:13:28 +000025using namespace clang;
26using namespace CodeGen;
Chris Lattnerbdb8ffb2007-08-11 00:04:45 +000027
Chris Lattnerb50e3902007-08-21 04:25:47 +000028//===----------------------------------------------------------------------===//
29// Aggregate Expression Emitter
30//===----------------------------------------------------------------------===//
31
32namespace {
33class VISIBILITY_HIDDEN AggExprEmitter : public StmtVisitor<AggExprEmitter> {
34 CodeGenFunction &CGF;
Daniel Dunbard916e6e2008-11-01 01:53:16 +000035 CGBuilderTy &Builder;
Chris Lattnerb50e3902007-08-21 04:25:47 +000036 llvm::Value *DestPtr;
37 bool VolatileDest;
Mike Stumpb27442c2009-05-26 22:03:21 +000038 bool IgnoreResult;
39
Chris Lattnerb50e3902007-08-21 04:25:47 +000040public:
Mike Stumpc58c15a2009-05-27 01:42:21 +000041 AggExprEmitter(CodeGenFunction &cgf, llvm::Value *destPtr, bool v,
42 bool ignore)
Chris Lattner41b1fca2007-08-26 23:13:56 +000043 : CGF(cgf), Builder(CGF.Builder),
Mike Stumpc58c15a2009-05-27 01:42:21 +000044 DestPtr(destPtr), VolatileDest(v), IgnoreResult(ignore) {
Chris Lattnerb50e3902007-08-21 04:25:47 +000045 }
46
Chris Lattnera7300102007-08-21 04:59:27 +000047 //===--------------------------------------------------------------------===//
48 // Utilities
49 //===--------------------------------------------------------------------===//
50
Chris Lattnerb50e3902007-08-21 04:25:47 +000051 /// EmitAggLoadOfLValue - Given an expression with aggregate type that
52 /// represents a value lvalue, this method emits the address of the lvalue,
53 /// then loads the result into DestPtr.
54 void EmitAggLoadOfLValue(const Expr *E);
Eli Friedman48ec5622008-05-19 17:51:16 +000055
Mike Stump8e06e032009-05-23 20:28:01 +000056 /// EmitFinalDestCopy - Perform the final copy to DestPtr, if desired.
Mike Stumpb27442c2009-05-26 22:03:21 +000057 void EmitFinalDestCopy(const Expr *E, LValue Src, bool Ignore = false);
58 void EmitFinalDestCopy(const Expr *E, RValue Src, bool Ignore = false);
Mike Stump8e06e032009-05-23 20:28:01 +000059
Chris Lattnera7300102007-08-21 04:59:27 +000060 //===--------------------------------------------------------------------===//
61 // Visitor Methods
62 //===--------------------------------------------------------------------===//
63
Chris Lattnerb50e3902007-08-21 04:25:47 +000064 void VisitStmt(Stmt *S) {
Daniel Dunbar9503b782008-08-16 00:56:44 +000065 CGF.ErrorUnsupported(S, "aggregate expression");
Chris Lattnerb50e3902007-08-21 04:25:47 +000066 }
67 void VisitParenExpr(ParenExpr *PE) { Visit(PE->getSubExpr()); }
Eli Friedmand031ba52009-01-27 09:03:41 +000068 void VisitUnaryExtension(UnaryOperator *E) { Visit(E->getSubExpr()); }
Chris Lattnerb50e3902007-08-21 04:25:47 +000069
70 // l-values.
Seo Sanghyeon32666c52007-12-14 02:04:12 +000071 void VisitDeclRefExpr(DeclRefExpr *DRE) { EmitAggLoadOfLValue(DRE); }
72 void VisitMemberExpr(MemberExpr *ME) { EmitAggLoadOfLValue(ME); }
73 void VisitUnaryDeref(UnaryOperator *E) { EmitAggLoadOfLValue(E); }
Seo Sanghyeon3c2d5fb2007-12-23 03:11:58 +000074 void VisitStringLiteral(StringLiteral *E) { EmitAggLoadOfLValue(E); }
Chris Lattner1a3965e2009-04-21 23:00:09 +000075 void VisitCompoundLiteralExpr(CompoundLiteralExpr *E) {
76 EmitAggLoadOfLValue(E);
77 }
Seo Sanghyeon32666c52007-12-14 02:04:12 +000078 void VisitArraySubscriptExpr(ArraySubscriptExpr *E) {
79 EmitAggLoadOfLValue(E);
80 }
Chris Lattner1a3965e2009-04-21 23:00:09 +000081 void VisitBlockDeclRefExpr(const BlockDeclRefExpr *E) {
82 EmitAggLoadOfLValue(E);
83 }
84 void VisitPredefinedExpr(const PredefinedExpr *E) {
85 EmitAggLoadOfLValue(E);
86 }
Douglas Gregorf603b472009-01-28 21:54:33 +000087
Chris Lattnerb50e3902007-08-21 04:25:47 +000088 // Operators.
Nuno Lopesdff17912009-01-15 20:14:33 +000089 void VisitCStyleCastExpr(CStyleCastExpr *E);
Anders Carlssonf2712ac2008-01-14 06:28:57 +000090 void VisitImplicitCastExpr(ImplicitCastExpr *E);
Anders Carlsson0ae15412007-10-31 22:04:46 +000091 void VisitCallExpr(const CallExpr *E);
Chris Lattnerbab0bfb2007-08-31 22:54:14 +000092 void VisitStmtExpr(const StmtExpr *E);
Chris Lattnerb50e3902007-08-21 04:25:47 +000093 void VisitBinaryOperator(const BinaryOperator *BO);
Chris Lattner76cb1892007-08-21 04:43:17 +000094 void VisitBinAssign(const BinaryOperator *E);
Eli Friedman91fc7802008-05-20 07:56:31 +000095 void VisitBinComma(const BinaryOperator *E);
Chris Lattnerb50e3902007-08-21 04:25:47 +000096
Chris Lattner6ee20e32008-06-24 17:04:18 +000097 void VisitObjCMessageExpr(ObjCMessageExpr *E);
Daniel Dunbar5e105892008-08-23 10:51:21 +000098 void VisitObjCIvarRefExpr(ObjCIvarRefExpr *E) {
99 EmitAggLoadOfLValue(E);
100 }
Daniel Dunbar91cc4022008-08-27 06:57:25 +0000101 void VisitObjCPropertyRefExpr(ObjCPropertyRefExpr *E);
Fariborz Jahanianf18d4c82008-11-22 18:39:36 +0000102 void VisitObjCKVCRefExpr(ObjCKVCRefExpr *E);
Chris Lattnerb50e3902007-08-21 04:25:47 +0000103
104 void VisitConditionalOperator(const ConditionalOperator *CO);
Devang Patelddde6d52007-10-26 17:44:44 +0000105 void VisitInitListExpr(InitListExpr *E);
Chris Lattner3e254fb2008-04-08 04:40:51 +0000106 void VisitCXXDefaultArgExpr(CXXDefaultArgExpr *DAE) {
107 Visit(DAE->getExpr());
108 }
Anders Carlsson2af296a2009-05-30 23:23:33 +0000109 void VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr *E);
Anders Carlsson342aadc2009-05-03 17:47:16 +0000110 void VisitCXXConstructExpr(const CXXConstructExpr *E);
Anders Carlsson272b5f52009-05-19 04:48:36 +0000111 void VisitCXXExprWithTemporaries(CXXExprWithTemporaries *E);
112
Eli Friedman42af3972008-05-27 15:51:49 +0000113 void VisitVAArgExpr(VAArgExpr *E);
Chris Lattner6ee6ab02008-04-04 18:42:16 +0000114
115 void EmitInitializationToLValue(Expr *E, LValue Address);
116 void EmitNullInitializationToLValue(LValue Address, QualType T);
Chris Lattnerb50e3902007-08-21 04:25:47 +0000117 // case Expr::ChooseExprClass:
Lauro Ramos Venancio9eb37a22008-02-18 22:44:02 +0000118
Chris Lattnerb50e3902007-08-21 04:25:47 +0000119};
120} // end anonymous namespace.
121
Chris Lattnera7300102007-08-21 04:59:27 +0000122//===----------------------------------------------------------------------===//
123// Utilities
124//===----------------------------------------------------------------------===//
Chris Lattnerb50e3902007-08-21 04:25:47 +0000125
Chris Lattnerbdb8ffb2007-08-11 00:04:45 +0000126/// EmitAggLoadOfLValue - Given an expression with aggregate type that
127/// represents a value lvalue, this method emits the address of the lvalue,
128/// then loads the result into DestPtr.
Chris Lattnerb50e3902007-08-21 04:25:47 +0000129void AggExprEmitter::EmitAggLoadOfLValue(const Expr *E) {
130 LValue LV = CGF.EmitLValue(E);
Mike Stump8e06e032009-05-23 20:28:01 +0000131 EmitFinalDestCopy(E, LV);
132}
133
134/// EmitFinalDestCopy - Perform the final copy to DestPtr, if desired.
Mike Stumpb27442c2009-05-26 22:03:21 +0000135void AggExprEmitter::EmitFinalDestCopy(const Expr *E, RValue Src, bool Ignore) {
Mike Stump8e06e032009-05-23 20:28:01 +0000136 assert(Src.isAggregate() && "value must be aggregate value!");
137
Chris Lattnerbdb8ffb2007-08-11 00:04:45 +0000138 // If the result is ignored, don't copy from the value.
Mike Stumpaa5083e2009-05-23 22:01:27 +0000139 if (DestPtr == 0) {
Mike Stumpb27442c2009-05-26 22:03:21 +0000140 if (!Src.isVolatileQualified() || (IgnoreResult && Ignore))
Mike Stumpaa5083e2009-05-23 22:01:27 +0000141 return;
Mike Stumpb27442c2009-05-26 22:03:21 +0000142 // If the source is volatile, we must read from it; to do that, we need
143 // some place to put it.
144 DestPtr = CGF.CreateTempAlloca(CGF.ConvertType(E->getType()), "agg.tmp");
Mike Stumpaa5083e2009-05-23 22:01:27 +0000145 }
Chris Lattnerbdb8ffb2007-08-11 00:04:45 +0000146
Mike Stump8e06e032009-05-23 20:28:01 +0000147 // If the result of the assignment is used, copy the LHS there also.
148 // FIXME: Pass VolatileDest as well. I think we also need to merge volatile
149 // from the source as well, as we can't eliminate it if either operand
150 // is volatile, unless copy has volatile for both source and destination..
Mike Stump577d0312009-05-23 22:29:41 +0000151 CGF.EmitAggregateCopy(DestPtr, Src.getAggregateAddr(), E->getType(),
152 VolatileDest|Src.isVolatileQualified());
Mike Stump8e06e032009-05-23 20:28:01 +0000153}
154
155/// EmitFinalDestCopy - Perform the final copy to DestPtr, if desired.
Mike Stumpb27442c2009-05-26 22:03:21 +0000156void AggExprEmitter::EmitFinalDestCopy(const Expr *E, LValue Src, bool Ignore) {
Mike Stump8e06e032009-05-23 20:28:01 +0000157 assert(Src.isSimple() && "Can't have aggregate bitfield, vector, etc");
158
159 EmitFinalDestCopy(E, RValue::getAggregate(Src.getAddress(),
Mike Stumpb27442c2009-05-26 22:03:21 +0000160 Src.isVolatileQualified()),
161 Ignore);
Chris Lattnerbdb8ffb2007-08-11 00:04:45 +0000162}
163
Chris Lattnera7300102007-08-21 04:59:27 +0000164//===----------------------------------------------------------------------===//
165// Visitor Methods
166//===----------------------------------------------------------------------===//
167
Nuno Lopesdff17912009-01-15 20:14:33 +0000168void AggExprEmitter::VisitCStyleCastExpr(CStyleCastExpr *E) {
169 // GCC union extension
Eli Friedmancbffe3e2009-06-03 20:45:06 +0000170 if (E->getSubExpr()->getType()->isScalarType()) {
171 QualType PtrTy =
172 CGF.getContext().getPointerType(E->getSubExpr()->getType());
173 llvm::Value *CastPtr = Builder.CreateBitCast(DestPtr,
174 CGF.ConvertType(PtrTy));
175 EmitInitializationToLValue(E->getSubExpr(), LValue::MakeAddr(CastPtr, 0));
Nuno Lopesdff17912009-01-15 20:14:33 +0000176 return;
177 }
178
179 Visit(E->getSubExpr());
180}
181
Chris Lattnerc154ac12008-07-26 22:37:01 +0000182void AggExprEmitter::VisitImplicitCastExpr(ImplicitCastExpr *E) {
Eli Friedman26dd2d32009-05-28 23:04:00 +0000183 assert(CGF.getContext().hasSameUnqualifiedType(E->getSubExpr()->getType(),
184 E->getType()) &&
Chris Lattnerc154ac12008-07-26 22:37:01 +0000185 "Implicit cast types must be compatible");
Anders Carlssonf2712ac2008-01-14 06:28:57 +0000186 Visit(E->getSubExpr());
187}
188
Chris Lattnerc154ac12008-07-26 22:37:01 +0000189void AggExprEmitter::VisitCallExpr(const CallExpr *E) {
Anders Carlssoneb3bd042009-05-27 16:45:02 +0000190 if (E->getCallReturnType()->isReferenceType()) {
191 EmitAggLoadOfLValue(E);
192 return;
193 }
194
Anders Carlsson0ae15412007-10-31 22:04:46 +0000195 RValue RV = CGF.EmitCallExpr(E);
Mike Stump8e06e032009-05-23 20:28:01 +0000196 EmitFinalDestCopy(E, RV);
Anders Carlsson0ae15412007-10-31 22:04:46 +0000197}
Chris Lattnerc154ac12008-07-26 22:37:01 +0000198
199void AggExprEmitter::VisitObjCMessageExpr(ObjCMessageExpr *E) {
Daniel Dunbara04840b2008-08-23 03:46:30 +0000200 RValue RV = CGF.EmitObjCMessageExpr(E);
Mike Stump8e06e032009-05-23 20:28:01 +0000201 EmitFinalDestCopy(E, RV);
Chris Lattner6ee20e32008-06-24 17:04:18 +0000202}
Anders Carlsson0ae15412007-10-31 22:04:46 +0000203
Daniel Dunbar91cc4022008-08-27 06:57:25 +0000204void AggExprEmitter::VisitObjCPropertyRefExpr(ObjCPropertyRefExpr *E) {
205 RValue RV = CGF.EmitObjCPropertyGet(E);
Mike Stump8e06e032009-05-23 20:28:01 +0000206 EmitFinalDestCopy(E, RV);
Daniel Dunbar91cc4022008-08-27 06:57:25 +0000207}
208
Fariborz Jahanianf18d4c82008-11-22 18:39:36 +0000209void AggExprEmitter::VisitObjCKVCRefExpr(ObjCKVCRefExpr *E) {
210 RValue RV = CGF.EmitObjCPropertyGet(E);
Mike Stump8e06e032009-05-23 20:28:01 +0000211 EmitFinalDestCopy(E, RV);
Fariborz Jahanianf18d4c82008-11-22 18:39:36 +0000212}
213
Chris Lattnerc154ac12008-07-26 22:37:01 +0000214void AggExprEmitter::VisitBinComma(const BinaryOperator *E) {
Mike Stumpb8fc73e2009-05-29 15:46:01 +0000215 CGF.EmitAnyExpr(E->getLHS(), 0, false, true);
Mike Stumpa8bac022009-05-23 21:40:07 +0000216 CGF.EmitAggExpr(E->getRHS(), DestPtr, VolatileDest);
Eli Friedman91fc7802008-05-20 07:56:31 +0000217}
218
Chris Lattnerbab0bfb2007-08-31 22:54:14 +0000219void AggExprEmitter::VisitStmtExpr(const StmtExpr *E) {
220 CGF.EmitCompoundStmt(*E->getSubStmt(), true, DestPtr, VolatileDest);
221}
222
Chris Lattnerb50e3902007-08-21 04:25:47 +0000223void AggExprEmitter::VisitBinaryOperator(const BinaryOperator *E) {
Daniel Dunbar9503b782008-08-16 00:56:44 +0000224 CGF.ErrorUnsupported(E, "aggregate binary expression");
Chris Lattnera7300102007-08-21 04:59:27 +0000225}
226
Chris Lattner76cb1892007-08-21 04:43:17 +0000227void AggExprEmitter::VisitBinAssign(const BinaryOperator *E) {
Eli Friedmanf0217122008-02-11 01:09:17 +0000228 // For an assignment to work, the value on the right has
229 // to be compatible with the value on the left.
Eli Friedman26dd2d32009-05-28 23:04:00 +0000230 assert(CGF.getContext().hasSameUnqualifiedType(E->getLHS()->getType(),
231 E->getRHS()->getType())
Eli Friedmanf0217122008-02-11 01:09:17 +0000232 && "Invalid assignment");
Chris Lattnerb50e3902007-08-21 04:25:47 +0000233 LValue LHS = CGF.EmitLValue(E->getLHS());
Chris Lattnerbdb8ffb2007-08-11 00:04:45 +0000234
Daniel Dunbardd851282008-08-30 05:35:15 +0000235 // We have to special case property setters, otherwise we must have
236 // a simple lvalue (no aggregates inside vectors, bitfields).
237 if (LHS.isPropertyRef()) {
Daniel Dunbardd851282008-08-30 05:35:15 +0000238 llvm::Value *AggLoc = DestPtr;
239 if (!AggLoc)
240 AggLoc = CGF.CreateTempAlloca(CGF.ConvertType(E->getRHS()->getType()));
Mike Stump5f507fb2009-05-23 23:48:13 +0000241 CGF.EmitAggExpr(E->getRHS(), AggLoc, VolatileDest);
Daniel Dunbardd851282008-08-30 05:35:15 +0000242 CGF.EmitObjCPropertySet(LHS.getPropertyRefExpr(),
Mike Stump5f507fb2009-05-23 23:48:13 +0000243 RValue::getAggregate(AggLoc, VolatileDest));
Fariborz Jahanianb0973da2008-11-22 22:30:21 +0000244 }
245 else if (LHS.isKVCRef()) {
Fariborz Jahanianb0973da2008-11-22 22:30:21 +0000246 llvm::Value *AggLoc = DestPtr;
247 if (!AggLoc)
248 AggLoc = CGF.CreateTempAlloca(CGF.ConvertType(E->getRHS()->getType()));
Mike Stump2a936c72009-05-23 23:52:31 +0000249 CGF.EmitAggExpr(E->getRHS(), AggLoc, VolatileDest);
Fariborz Jahanianb0973da2008-11-22 22:30:21 +0000250 CGF.EmitObjCPropertySet(LHS.getKVCRefExpr(),
Mike Stump2a936c72009-05-23 23:52:31 +0000251 RValue::getAggregate(AggLoc, VolatileDest));
Daniel Dunbardd851282008-08-30 05:35:15 +0000252 } else {
Fariborz Jahanian614e8f02009-07-08 01:18:33 +0000253 if (CGF.getContext().getLangOptions().NeXTRuntime) {
254 QualType LHSTy = E->getLHS()->getType();
255 if (const RecordType *FDTTy = LHSTy.getTypePtr()->getAsRecordType())
256 if (FDTTy->getDecl()->hasObjectMember()) {
257 LValue RHS = CGF.EmitLValue(E->getRHS());
258 CGF.CGM.getObjCRuntime().EmitGCMemmoveCollectable(CGF, LHS.getAddress(),
259 RHS.getAddress(),
260 CGF.getContext().getTypeSize(LHSTy) / 8);
261 return;
262 }
263 }
Daniel Dunbardd851282008-08-30 05:35:15 +0000264 // Codegen the RHS so that it stores directly into the LHS.
Mike Stumpb491f802009-05-23 04:13:59 +0000265 CGF.EmitAggExpr(E->getRHS(), LHS.getAddress(), LHS.isVolatileQualified());
Mike Stumpb27442c2009-05-26 22:03:21 +0000266 EmitFinalDestCopy(E, LHS, true);
Daniel Dunbardd851282008-08-30 05:35:15 +0000267 }
Chris Lattnerbdb8ffb2007-08-11 00:04:45 +0000268}
269
Chris Lattnerb50e3902007-08-21 04:25:47 +0000270void AggExprEmitter::VisitConditionalOperator(const ConditionalOperator *E) {
Daniel Dunbar6e3a10c2008-11-13 01:38:36 +0000271 llvm::BasicBlock *LHSBlock = CGF.createBasicBlock("cond.true");
272 llvm::BasicBlock *RHSBlock = CGF.createBasicBlock("cond.false");
273 llvm::BasicBlock *ContBlock = CGF.createBasicBlock("cond.end");
Chris Lattnerbdb8ffb2007-08-11 00:04:45 +0000274
Chris Lattnerb50e3902007-08-21 04:25:47 +0000275 llvm::Value *Cond = CGF.EvaluateExprAsBool(E->getCond());
Chris Lattner41b1fca2007-08-26 23:13:56 +0000276 Builder.CreateCondBr(Cond, LHSBlock, RHSBlock);
Chris Lattnerbdb8ffb2007-08-11 00:04:45 +0000277
Anders Carlssonbf3b93a2009-06-04 03:00:32 +0000278 CGF.PushConditionalTempDestruction();
Chris Lattnerb50e3902007-08-21 04:25:47 +0000279 CGF.EmitBlock(LHSBlock);
Chris Lattnerbdb8ffb2007-08-11 00:04:45 +0000280
281 // Handle the GNU extension for missing LHS.
282 assert(E->getLHS() && "Must have LHS for aggregate value");
283
Chris Lattner55165ba2007-08-21 05:02:10 +0000284 Visit(E->getLHS());
Anders Carlssonbf3b93a2009-06-04 03:00:32 +0000285 CGF.PopConditionalTempDestruction();
Daniel Dunbar5276caa2008-11-11 09:41:28 +0000286 CGF.EmitBranch(ContBlock);
Chris Lattnerbdb8ffb2007-08-11 00:04:45 +0000287
Anders Carlssonbf3b93a2009-06-04 03:00:32 +0000288 CGF.PushConditionalTempDestruction();
Chris Lattnerb50e3902007-08-21 04:25:47 +0000289 CGF.EmitBlock(RHSBlock);
Chris Lattnerbdb8ffb2007-08-11 00:04:45 +0000290
Chris Lattner55165ba2007-08-21 05:02:10 +0000291 Visit(E->getRHS());
Anders Carlssonbf3b93a2009-06-04 03:00:32 +0000292 CGF.PopConditionalTempDestruction();
Daniel Dunbar5276caa2008-11-11 09:41:28 +0000293 CGF.EmitBranch(ContBlock);
Chris Lattnerbdb8ffb2007-08-11 00:04:45 +0000294
Chris Lattnerb50e3902007-08-21 04:25:47 +0000295 CGF.EmitBlock(ContBlock);
Chris Lattnerbdb8ffb2007-08-11 00:04:45 +0000296}
Chris Lattnera7300102007-08-21 04:59:27 +0000297
Eli Friedman42af3972008-05-27 15:51:49 +0000298void AggExprEmitter::VisitVAArgExpr(VAArgExpr *VE) {
Daniel Dunbar63378cf2009-02-11 22:25:55 +0000299 llvm::Value *ArgValue = CGF.EmitVAListRef(VE->getSubExpr());
Anders Carlsson285611e2008-11-04 05:30:00 +0000300 llvm::Value *ArgPtr = CGF.EmitVAArg(ArgValue, VE->getType());
301
Sebastian Redl79f36c92009-01-09 21:09:38 +0000302 if (!ArgPtr) {
Anders Carlsson285611e2008-11-04 05:30:00 +0000303 CGF.ErrorUnsupported(VE, "aggregate va_arg expression");
Sebastian Redl79f36c92009-01-09 21:09:38 +0000304 return;
305 }
306
Mike Stump8e06e032009-05-23 20:28:01 +0000307 EmitFinalDestCopy(VE, LValue::MakeAddr(ArgPtr, 0));
Eli Friedman42af3972008-05-27 15:51:49 +0000308}
309
Anders Carlsson2af296a2009-05-30 23:23:33 +0000310void AggExprEmitter::VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr *E) {
311 llvm::Value *Val = DestPtr;
312
313 if (!Val) {
314 // Create a temporary variable.
315 Val = CGF.CreateTempAlloca(CGF.ConvertTypeForMem(E->getType()), "tmp");
316
317 // FIXME: volatile
318 CGF.EmitAggExpr(E->getSubExpr(), Val, false);
319 } else
320 Visit(E->getSubExpr());
321
Anders Carlsson7fa1db82009-05-31 00:34:10 +0000322 CGF.PushCXXTemporary(E->getTemporary(), Val);
Anders Carlsson2af296a2009-05-30 23:23:33 +0000323}
324
Anders Carlsson72f48292009-04-17 00:06:03 +0000325void
Anders Carlsson342aadc2009-05-03 17:47:16 +0000326AggExprEmitter::VisitCXXConstructExpr(const CXXConstructExpr *E) {
Anders Carlsson2af296a2009-05-30 23:23:33 +0000327 llvm::Value *Val = DestPtr;
328
329 if (!Val) {
330 // Create a temporary variable.
331 Val = CGF.CreateTempAlloca(CGF.ConvertTypeForMem(E->getType()), "tmp");
332 }
Anders Carlsson7b7b2552009-05-30 20:56:46 +0000333
Anders Carlsson2af296a2009-05-30 23:23:33 +0000334 CGF.EmitCXXConstructExpr(Val, E);
Anders Carlsson272b5f52009-05-19 04:48:36 +0000335}
336
337void AggExprEmitter::VisitCXXExprWithTemporaries(CXXExprWithTemporaries *E) {
Anders Carlsson2af296a2009-05-30 23:23:33 +0000338 CGF.EmitCXXExprWithTemporaries(E, DestPtr, VolatileDest);
Anders Carlsson72f48292009-04-17 00:06:03 +0000339}
340
Chris Lattner6ee6ab02008-04-04 18:42:16 +0000341void AggExprEmitter::EmitInitializationToLValue(Expr* E, LValue LV) {
Mike Stumpb8fc73e2009-05-29 15:46:01 +0000342 // FIXME: Ignore result?
Chris Lattner6ee6ab02008-04-04 18:42:16 +0000343 // FIXME: Are initializers affected by volatile?
Douglas Gregorc9e012a2009-01-29 17:44:32 +0000344 if (isa<ImplicitValueInitExpr>(E)) {
Douglas Gregorf603b472009-01-28 21:54:33 +0000345 EmitNullInitializationToLValue(LV, E->getType());
Douglas Gregorc9e012a2009-01-29 17:44:32 +0000346 } else if (E->getType()->isComplexType()) {
347 CGF.EmitComplexExprIntoAddr(E, LV.getAddress(), false);
Eli Friedmanbb822fb2008-05-12 15:06:05 +0000348 } else if (CGF.hasAggregateLLVMType(E->getType())) {
349 CGF.EmitAnyExpr(E, LV.getAddress(), false);
350 } else {
351 CGF.EmitStoreThroughLValue(CGF.EmitAnyExpr(E), LV, E->getType());
Chris Lattner6ee6ab02008-04-04 18:42:16 +0000352 }
Chris Lattner6ee6ab02008-04-04 18:42:16 +0000353}
354
355void AggExprEmitter::EmitNullInitializationToLValue(LValue LV, QualType T) {
356 if (!CGF.hasAggregateLLVMType(T)) {
357 // For non-aggregates, we can store zero
Daniel Dunbarde813312008-08-06 05:32:55 +0000358 llvm::Value *Null = llvm::Constant::getNullValue(CGF.ConvertType(T));
359 CGF.EmitStoreThroughLValue(RValue::get(Null), LV, T);
Lauro Ramos Venancioa62b1df2008-02-19 19:27:31 +0000360 } else {
Chris Lattner6ee6ab02008-04-04 18:42:16 +0000361 // Otherwise, just memset the whole thing to zero. This is legal
362 // because in LLVM, all default initializers are guaranteed to have a
363 // bit pattern of all zeros.
Eli Friedmanb836cd32009-04-13 21:47:26 +0000364 // FIXME: That isn't true for member pointers!
Chris Lattner6ee6ab02008-04-04 18:42:16 +0000365 // There's a potential optimization opportunity in combining
366 // memsets; that would be easy for arrays, but relatively
367 // difficult for structures with the current code.
Eli Friedman2494bc32009-03-28 03:10:45 +0000368 CGF.EmitMemSetToZero(LV.getAddress(), T);
Chris Lattner6ee6ab02008-04-04 18:42:16 +0000369 }
370}
371
Chris Lattner6ee6ab02008-04-04 18:42:16 +0000372void AggExprEmitter::VisitInitListExpr(InitListExpr *E) {
Eli Friedman2a4f27c2008-12-02 01:17:45 +0000373#if 0
374 // FIXME: Disabled while we figure out what to do about
375 // test/CodeGen/bitfield.c
376 //
Mike Stumpba2cb0e2009-05-16 07:57:57 +0000377 // If we can, prefer a copy from a global; this is a lot less code for long
378 // globals, and it's easier for the current optimizers to analyze.
379 // FIXME: Should we really be doing this? Should we try to avoid cases where
380 // we emit a global with a lot of zeros? Should we try to avoid short
381 // globals?
Eli Friedmandee41122009-01-25 02:32:41 +0000382 if (E->isConstantInitializer(CGF.getContext(), 0)) {
Eli Friedmane9b227d2008-11-30 02:11:09 +0000383 llvm::Constant* C = CGF.CGM.EmitConstantExpr(E, &CGF);
384 llvm::GlobalVariable* GV =
385 new llvm::GlobalVariable(C->getType(), true,
386 llvm::GlobalValue::InternalLinkage,
387 C, "", &CGF.CGM.getModule(), 0);
Mike Stump8e06e032009-05-23 20:28:01 +0000388 EmitFinalDestCopy(E, LValue::MakeAddr(GV, 0));
Eli Friedmane9b227d2008-11-30 02:11:09 +0000389 return;
390 }
Eli Friedman2a4f27c2008-12-02 01:17:45 +0000391#endif
Douglas Gregor9fddded2009-01-29 19:42:23 +0000392 if (E->hadArrayRangeDesignator()) {
393 CGF.ErrorUnsupported(E, "GNU array range designator extension");
394 }
395
Chris Lattner6ee6ab02008-04-04 18:42:16 +0000396 // Handle initialization of an array.
397 if (E->getType()->isArrayType()) {
398 const llvm::PointerType *APType =
399 cast<llvm::PointerType>(DestPtr->getType());
400 const llvm::ArrayType *AType =
401 cast<llvm::ArrayType>(APType->getElementType());
402
403 uint64_t NumInitElements = E->getNumInits();
Eli Friedman48ec5622008-05-19 17:51:16 +0000404
Chris Lattnerc154ac12008-07-26 22:37:01 +0000405 if (E->getNumInits() > 0) {
406 QualType T1 = E->getType();
407 QualType T2 = E->getInit(0)->getType();
Eli Friedman26dd2d32009-05-28 23:04:00 +0000408 if (CGF.getContext().hasSameUnqualifiedType(T1, T2)) {
Chris Lattnerc154ac12008-07-26 22:37:01 +0000409 EmitAggLoadOfLValue(E->getInit(0));
410 return;
411 }
Eli Friedman48ec5622008-05-19 17:51:16 +0000412 }
413
Chris Lattner6ee6ab02008-04-04 18:42:16 +0000414 uint64_t NumArrayElements = AType->getNumElements();
Chris Lattnera1923f62008-08-04 07:31:14 +0000415 QualType ElementType = CGF.getContext().getCanonicalType(E->getType());
Douglas Gregorf603b472009-01-28 21:54:33 +0000416 ElementType = CGF.getContext().getAsArrayType(ElementType)->getElementType();
Chris Lattner6ee6ab02008-04-04 18:42:16 +0000417
Chris Lattnera1923f62008-08-04 07:31:14 +0000418 unsigned CVRqualifier = ElementType.getCVRQualifiers();
Eli Friedman2e630542008-06-13 23:01:12 +0000419
Chris Lattner6ee6ab02008-04-04 18:42:16 +0000420 for (uint64_t i = 0; i != NumArrayElements; ++i) {
421 llvm::Value *NextVal = Builder.CreateStructGEP(DestPtr, i, ".array");
422 if (i < NumInitElements)
Eli Friedman2e630542008-06-13 23:01:12 +0000423 EmitInitializationToLValue(E->getInit(i),
424 LValue::MakeAddr(NextVal, CVRqualifier));
Chris Lattner6ee6ab02008-04-04 18:42:16 +0000425 else
Eli Friedman2e630542008-06-13 23:01:12 +0000426 EmitNullInitializationToLValue(LValue::MakeAddr(NextVal, CVRqualifier),
Chris Lattner6ee6ab02008-04-04 18:42:16 +0000427 ElementType);
Lauro Ramos Venancioa62b1df2008-02-19 19:27:31 +0000428 }
Chris Lattner6ee6ab02008-04-04 18:42:16 +0000429 return;
430 }
431
432 assert(E->getType()->isRecordType() && "Only support structs/unions here!");
433
434 // Do struct initialization; this code just sets each individual member
435 // to the approprate value. This makes bitfield support automatic;
436 // the disadvantage is that the generated code is more difficult for
437 // the optimizer, especially with bitfields.
438 unsigned NumInitElements = E->getNumInits();
439 RecordDecl *SD = E->getType()->getAsRecordType()->getDecl();
Chris Lattner6ee6ab02008-04-04 18:42:16 +0000440 unsigned CurInitVal = 0;
Douglas Gregor82462762009-01-29 16:53:55 +0000441
442 if (E->getType()->isUnionType()) {
443 // Only initialize one field of a union. The field itself is
444 // specified by the initializer list.
445 if (!E->getInitializedFieldInUnion()) {
446 // Empty union; we have nothing to do.
447
448#ifndef NDEBUG
449 // Make sure that it's really an empty and not a failure of
450 // semantic analysis.
Argiris Kirtzidisab6e38a2009-06-30 02:36:12 +0000451 for (RecordDecl::field_iterator Field = SD->field_begin(),
452 FieldEnd = SD->field_end();
Douglas Gregor82462762009-01-29 16:53:55 +0000453 Field != FieldEnd; ++Field)
454 assert(Field->isUnnamedBitfield() && "Only unnamed bitfields allowed");
455#endif
456 return;
457 }
458
459 // FIXME: volatility
460 FieldDecl *Field = E->getInitializedFieldInUnion();
461 LValue FieldLoc = CGF.EmitLValueForField(DestPtr, Field, true, 0);
462
463 if (NumInitElements) {
464 // Store the initializer into the field
465 EmitInitializationToLValue(E->getInit(0), FieldLoc);
466 } else {
467 // Default-initialize to null
468 EmitNullInitializationToLValue(FieldLoc, Field->getType());
469 }
470
471 return;
472 }
Chris Lattner6ee6ab02008-04-04 18:42:16 +0000473
474 // Here we iterate over the fields; this makes it simpler to both
475 // default-initialize fields and skip over unnamed fields.
Argiris Kirtzidisab6e38a2009-06-30 02:36:12 +0000476 for (RecordDecl::field_iterator Field = SD->field_begin(),
477 FieldEnd = SD->field_end();
Douglas Gregor8acb7272008-12-11 16:49:14 +0000478 Field != FieldEnd; ++Field) {
479 // We're done once we hit the flexible array member
480 if (Field->getType()->isIncompleteArrayType())
481 break;
482
Douglas Gregor36dd0c52009-01-28 23:36:17 +0000483 if (Field->isUnnamedBitfield())
Chris Lattner6ee6ab02008-04-04 18:42:16 +0000484 continue;
Douglas Gregor36dd0c52009-01-28 23:36:17 +0000485
Eli Friedman2e630542008-06-13 23:01:12 +0000486 // FIXME: volatility
Douglas Gregor82462762009-01-29 16:53:55 +0000487 LValue FieldLoc = CGF.EmitLValueForField(DestPtr, *Field, false, 0);
Fariborz Jahanian7f85b482009-05-27 19:54:11 +0000488 // We never generate write-barries for initialized fields.
489 LValue::SetObjCNonGC(FieldLoc, true);
Chris Lattner6ee6ab02008-04-04 18:42:16 +0000490 if (CurInitVal < NumInitElements) {
491 // Store the initializer into the field
Chris Lattner6ee6ab02008-04-04 18:42:16 +0000492 EmitInitializationToLValue(E->getInit(CurInitVal++), FieldLoc);
493 } else {
494 // We're out of initalizers; default-initialize to null
Douglas Gregor8acb7272008-12-11 16:49:14 +0000495 EmitNullInitializationToLValue(FieldLoc, Field->getType());
Chris Lattner6ee6ab02008-04-04 18:42:16 +0000496 }
Lauro Ramos Venancioa62b1df2008-02-19 19:27:31 +0000497 }
Devang Patelddde6d52007-10-26 17:44:44 +0000498}
499
Chris Lattnera7300102007-08-21 04:59:27 +0000500//===----------------------------------------------------------------------===//
501// Entry Points into this File
502//===----------------------------------------------------------------------===//
503
Mike Stump3541e842009-05-26 18:57:45 +0000504/// EmitAggExpr - Emit the computation of the specified expression of aggregate
505/// type. The result is computed into DestPtr. Note that if DestPtr is null,
506/// the value of the aggregate expression is not needed. If VolatileDest is
507/// true, DestPtr cannot be 0.
Chris Lattnera7300102007-08-21 04:59:27 +0000508void CodeGenFunction::EmitAggExpr(const Expr *E, llvm::Value *DestPtr,
Mike Stumpb27442c2009-05-26 22:03:21 +0000509 bool VolatileDest, bool IgnoreResult) {
Chris Lattnera7300102007-08-21 04:59:27 +0000510 assert(E && hasAggregateLLVMType(E->getType()) &&
511 "Invalid aggregate expression to emit");
Mike Stump3541e842009-05-26 18:57:45 +0000512 assert ((DestPtr != 0 || VolatileDest == false)
513 && "volatile aggregate can't be 0");
Chris Lattnera7300102007-08-21 04:59:27 +0000514
Mike Stumpb27442c2009-05-26 22:03:21 +0000515 AggExprEmitter(*this, DestPtr, VolatileDest, IgnoreResult)
516 .Visit(const_cast<Expr*>(E));
Chris Lattnera7300102007-08-21 04:59:27 +0000517}
Daniel Dunbarbde0c9d2008-09-09 20:49:46 +0000518
519void CodeGenFunction::EmitAggregateClear(llvm::Value *DestPtr, QualType Ty) {
520 assert(!Ty->isAnyComplexType() && "Shouldn't happen for complex");
521
522 EmitMemSetToZero(DestPtr, Ty);
523}
524
525void CodeGenFunction::EmitAggregateCopy(llvm::Value *DestPtr,
Mike Stump577d0312009-05-23 22:29:41 +0000526 llvm::Value *SrcPtr, QualType Ty,
527 bool isVolatile) {
Daniel Dunbarbde0c9d2008-09-09 20:49:46 +0000528 assert(!Ty->isAnyComplexType() && "Shouldn't happen for complex");
529
Chris Lattner1ec02512009-02-28 18:31:01 +0000530 // Aggregate assignment turns into llvm.memcpy. This is almost valid per
Chris Lattner1db26ed2009-02-28 18:18:58 +0000531 // C99 6.5.16.1p3, which states "If the value being stored in an object is
532 // read from another object that overlaps in anyway the storage of the first
533 // object, then the overlap shall be exact and the two objects shall have
534 // qualified or unqualified versions of a compatible type."
535 //
Chris Lattner1ec02512009-02-28 18:31:01 +0000536 // memcpy is not defined if the source and destination pointers are exactly
Chris Lattner1db26ed2009-02-28 18:18:58 +0000537 // equal, but other compilers do this optimization, and almost every memcpy
538 // implementation handles this case safely. If there is a libc that does not
539 // safely handle this, we can add a target hook.
Daniel Dunbarbde0c9d2008-09-09 20:49:46 +0000540 const llvm::Type *BP = llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
541 if (DestPtr->getType() != BP)
542 DestPtr = Builder.CreateBitCast(DestPtr, BP, "tmp");
543 if (SrcPtr->getType() != BP)
544 SrcPtr = Builder.CreateBitCast(SrcPtr, BP, "tmp");
545
546 // Get size and alignment info for this aggregate.
547 std::pair<uint64_t, unsigned> TypeInfo = getContext().getTypeInfo(Ty);
548
549 // FIXME: Handle variable sized types.
550 const llvm::Type *IntPtr = llvm::IntegerType::get(LLVMPointerWidth);
551
Mike Stumpb491f802009-05-23 04:13:59 +0000552 // FIXME: If we have a volatile struct, the optimizer can remove what might
553 // appear to be `extra' memory ops:
554 //
555 // volatile struct { int i; } a, b;
556 //
557 // int main() {
558 // a = b;
559 // a = b;
560 // }
561 //
Mike Stumpb27442c2009-05-26 22:03:21 +0000562 // we need to use a differnt call here. We use isVolatile to indicate when
563 // either the source or the destination is volatile.
Chris Lattner1db26ed2009-02-28 18:18:58 +0000564 Builder.CreateCall4(CGM.getMemCpyFn(),
Daniel Dunbarbde0c9d2008-09-09 20:49:46 +0000565 DestPtr, SrcPtr,
566 // TypeInfo.first describes size in bits.
567 llvm::ConstantInt::get(IntPtr, TypeInfo.first/8),
568 llvm::ConstantInt::get(llvm::Type::Int32Ty,
569 TypeInfo.second/8));
570}