blob: ecfbf4836c81270c5480a2d31de00957dcaa1599 [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"
Daniel Dunbareee5cd12008-08-11 05:00:27 +000016#include "clang/AST/ASTContext.h"
Anders Carlsson72f48292009-04-17 00:06:03 +000017#include "clang/AST/DeclCXX.h"
Daniel Dunbareee5cd12008-08-11 05:00:27 +000018#include "clang/AST/StmtVisitor.h"
Chris Lattnerbdb8ffb2007-08-11 00:04:45 +000019#include "llvm/Constants.h"
20#include "llvm/Function.h"
Devang Patelddde6d52007-10-26 17:44:44 +000021#include "llvm/GlobalVariable.h"
Chris Lattnerb50e3902007-08-21 04:25:47 +000022#include "llvm/Support/Compiler.h"
Chris Lattner6ee6ab02008-04-04 18:42:16 +000023#include "llvm/Intrinsics.h"
Chris Lattner78ed8402007-08-10 20:13:28 +000024using namespace clang;
25using namespace CodeGen;
Chris Lattnerbdb8ffb2007-08-11 00:04:45 +000026
Chris Lattnerb50e3902007-08-21 04:25:47 +000027//===----------------------------------------------------------------------===//
28// Aggregate Expression Emitter
29//===----------------------------------------------------------------------===//
30
31namespace {
32class VISIBILITY_HIDDEN AggExprEmitter : public StmtVisitor<AggExprEmitter> {
33 CodeGenFunction &CGF;
Daniel Dunbard916e6e2008-11-01 01:53:16 +000034 CGBuilderTy &Builder;
Chris Lattnerb50e3902007-08-21 04:25:47 +000035 llvm::Value *DestPtr;
36 bool VolatileDest;
37public:
38 AggExprEmitter(CodeGenFunction &cgf, llvm::Value *destPtr, bool volatileDest)
Chris Lattner41b1fca2007-08-26 23:13:56 +000039 : CGF(cgf), Builder(CGF.Builder),
40 DestPtr(destPtr), VolatileDest(volatileDest) {
Chris Lattnerb50e3902007-08-21 04:25:47 +000041 }
42
Chris Lattnera7300102007-08-21 04:59:27 +000043 //===--------------------------------------------------------------------===//
44 // Utilities
45 //===--------------------------------------------------------------------===//
46
Chris Lattnerb50e3902007-08-21 04:25:47 +000047 /// EmitAggLoadOfLValue - Given an expression with aggregate type that
48 /// represents a value lvalue, this method emits the address of the lvalue,
49 /// then loads the result into DestPtr.
50 void EmitAggLoadOfLValue(const Expr *E);
Eli Friedman48ec5622008-05-19 17:51:16 +000051
Mike Stump8e06e032009-05-23 20:28:01 +000052 /// EmitFinalDestCopy - Perform the final copy to DestPtr, if desired.
53 void EmitFinalDestCopy(const Expr *E, LValue Src);
54 void EmitFinalDestCopy(const Expr *E, RValue Src);
55
Chris Lattnera7300102007-08-21 04:59:27 +000056 //===--------------------------------------------------------------------===//
57 // Visitor Methods
58 //===--------------------------------------------------------------------===//
59
Chris Lattnerb50e3902007-08-21 04:25:47 +000060 void VisitStmt(Stmt *S) {
Daniel Dunbar9503b782008-08-16 00:56:44 +000061 CGF.ErrorUnsupported(S, "aggregate expression");
Chris Lattnerb50e3902007-08-21 04:25:47 +000062 }
63 void VisitParenExpr(ParenExpr *PE) { Visit(PE->getSubExpr()); }
Eli Friedmand031ba52009-01-27 09:03:41 +000064 void VisitUnaryExtension(UnaryOperator *E) { Visit(E->getSubExpr()); }
Chris Lattnerb50e3902007-08-21 04:25:47 +000065
66 // l-values.
Seo Sanghyeon32666c52007-12-14 02:04:12 +000067 void VisitDeclRefExpr(DeclRefExpr *DRE) { EmitAggLoadOfLValue(DRE); }
68 void VisitMemberExpr(MemberExpr *ME) { EmitAggLoadOfLValue(ME); }
69 void VisitUnaryDeref(UnaryOperator *E) { EmitAggLoadOfLValue(E); }
Seo Sanghyeon3c2d5fb2007-12-23 03:11:58 +000070 void VisitStringLiteral(StringLiteral *E) { EmitAggLoadOfLValue(E); }
Chris Lattner1a3965e2009-04-21 23:00:09 +000071 void VisitCompoundLiteralExpr(CompoundLiteralExpr *E) {
72 EmitAggLoadOfLValue(E);
73 }
Seo Sanghyeon32666c52007-12-14 02:04:12 +000074 void VisitArraySubscriptExpr(ArraySubscriptExpr *E) {
75 EmitAggLoadOfLValue(E);
76 }
Chris Lattner1a3965e2009-04-21 23:00:09 +000077 void VisitBlockDeclRefExpr(const BlockDeclRefExpr *E) {
78 EmitAggLoadOfLValue(E);
79 }
80 void VisitPredefinedExpr(const PredefinedExpr *E) {
81 EmitAggLoadOfLValue(E);
82 }
Douglas Gregorf603b472009-01-28 21:54:33 +000083
Chris Lattnerb50e3902007-08-21 04:25:47 +000084 // Operators.
Nuno Lopesdff17912009-01-15 20:14:33 +000085 void VisitCStyleCastExpr(CStyleCastExpr *E);
Anders Carlssonf2712ac2008-01-14 06:28:57 +000086 void VisitImplicitCastExpr(ImplicitCastExpr *E);
Anders Carlsson0ae15412007-10-31 22:04:46 +000087 void VisitCallExpr(const CallExpr *E);
Chris Lattnerbab0bfb2007-08-31 22:54:14 +000088 void VisitStmtExpr(const StmtExpr *E);
Chris Lattnerb50e3902007-08-21 04:25:47 +000089 void VisitBinaryOperator(const BinaryOperator *BO);
Chris Lattner76cb1892007-08-21 04:43:17 +000090 void VisitBinAssign(const BinaryOperator *E);
Eli Friedman91fc7802008-05-20 07:56:31 +000091 void VisitBinComma(const BinaryOperator *E);
Chris Lattnerb50e3902007-08-21 04:25:47 +000092
Chris Lattner6ee20e32008-06-24 17:04:18 +000093 void VisitObjCMessageExpr(ObjCMessageExpr *E);
Daniel Dunbar5e105892008-08-23 10:51:21 +000094 void VisitObjCIvarRefExpr(ObjCIvarRefExpr *E) {
95 EmitAggLoadOfLValue(E);
96 }
Daniel Dunbar91cc4022008-08-27 06:57:25 +000097 void VisitObjCPropertyRefExpr(ObjCPropertyRefExpr *E);
Fariborz Jahanianf18d4c82008-11-22 18:39:36 +000098 void VisitObjCKVCRefExpr(ObjCKVCRefExpr *E);
Chris Lattnerb50e3902007-08-21 04:25:47 +000099
100 void VisitConditionalOperator(const ConditionalOperator *CO);
Devang Patelddde6d52007-10-26 17:44:44 +0000101 void VisitInitListExpr(InitListExpr *E);
Chris Lattner3e254fb2008-04-08 04:40:51 +0000102 void VisitCXXDefaultArgExpr(CXXDefaultArgExpr *DAE) {
103 Visit(DAE->getExpr());
104 }
Anders Carlsson342aadc2009-05-03 17:47:16 +0000105 void VisitCXXConstructExpr(const CXXConstructExpr *E);
Anders Carlsson272b5f52009-05-19 04:48:36 +0000106 void VisitCXXExprWithTemporaries(CXXExprWithTemporaries *E);
107
Eli Friedman42af3972008-05-27 15:51:49 +0000108 void VisitVAArgExpr(VAArgExpr *E);
Chris Lattner6ee6ab02008-04-04 18:42:16 +0000109
110 void EmitInitializationToLValue(Expr *E, LValue Address);
111 void EmitNullInitializationToLValue(LValue Address, QualType T);
Chris Lattnerb50e3902007-08-21 04:25:47 +0000112 // case Expr::ChooseExprClass:
Lauro Ramos Venancio9eb37a22008-02-18 22:44:02 +0000113
Chris Lattnerb50e3902007-08-21 04:25:47 +0000114};
115} // end anonymous namespace.
116
Chris Lattnera7300102007-08-21 04:59:27 +0000117//===----------------------------------------------------------------------===//
118// Utilities
119//===----------------------------------------------------------------------===//
Chris Lattnerb50e3902007-08-21 04:25:47 +0000120
Chris Lattnerbdb8ffb2007-08-11 00:04:45 +0000121/// EmitAggLoadOfLValue - Given an expression with aggregate type that
122/// represents a value lvalue, this method emits the address of the lvalue,
123/// then loads the result into DestPtr.
Chris Lattnerb50e3902007-08-21 04:25:47 +0000124void AggExprEmitter::EmitAggLoadOfLValue(const Expr *E) {
125 LValue LV = CGF.EmitLValue(E);
Mike Stump8e06e032009-05-23 20:28:01 +0000126 EmitFinalDestCopy(E, LV);
127}
128
129/// EmitFinalDestCopy - Perform the final copy to DestPtr, if desired.
130void AggExprEmitter::EmitFinalDestCopy(const Expr *E, RValue Src) {
131 assert(Src.isAggregate() && "value must be aggregate value!");
132
Chris Lattnerbdb8ffb2007-08-11 00:04:45 +0000133 // If the result is ignored, don't copy from the value.
Mike Stumpaa5083e2009-05-23 22:01:27 +0000134 if (DestPtr == 0) {
135 if (Src.isVolatileQualified())
136 // If the source is volatile, we must read from it; to do that, we need
137 // some place to put it.
138 DestPtr = CGF.CreateTempAlloca(CGF.ConvertType(E->getType()), "agg.tmp");
139 else
140 return;
141 }
Chris Lattnerbdb8ffb2007-08-11 00:04:45 +0000142
Mike Stump8e06e032009-05-23 20:28:01 +0000143 // If the result of the assignment is used, copy the LHS there also.
144 // FIXME: Pass VolatileDest as well. I think we also need to merge volatile
145 // from the source as well, as we can't eliminate it if either operand
146 // is volatile, unless copy has volatile for both source and destination..
Mike Stump577d0312009-05-23 22:29:41 +0000147 CGF.EmitAggregateCopy(DestPtr, Src.getAggregateAddr(), E->getType(),
148 VolatileDest|Src.isVolatileQualified());
Mike Stump8e06e032009-05-23 20:28:01 +0000149}
150
151/// EmitFinalDestCopy - Perform the final copy to DestPtr, if desired.
152void AggExprEmitter::EmitFinalDestCopy(const Expr *E, LValue Src) {
153 assert(Src.isSimple() && "Can't have aggregate bitfield, vector, etc");
154
155 EmitFinalDestCopy(E, RValue::getAggregate(Src.getAddress(),
156 Src.isVolatileQualified()));
Chris Lattnerbdb8ffb2007-08-11 00:04:45 +0000157}
158
Chris Lattnera7300102007-08-21 04:59:27 +0000159//===----------------------------------------------------------------------===//
160// Visitor Methods
161//===----------------------------------------------------------------------===//
162
Nuno Lopesdff17912009-01-15 20:14:33 +0000163void AggExprEmitter::VisitCStyleCastExpr(CStyleCastExpr *E) {
164 // GCC union extension
165 if (E->getType()->isUnionType()) {
166 RecordDecl *SD = E->getType()->getAsRecordType()->getDecl();
Douglas Gregorc55b0b02009-04-09 21:40:53 +0000167 LValue FieldLoc = CGF.EmitLValueForField(DestPtr,
168 *SD->field_begin(CGF.getContext()),
169 true, 0);
Nuno Lopesdff17912009-01-15 20:14:33 +0000170 EmitInitializationToLValue(E->getSubExpr(), FieldLoc);
171 return;
172 }
173
174 Visit(E->getSubExpr());
175}
176
Chris Lattnerc154ac12008-07-26 22:37:01 +0000177void AggExprEmitter::VisitImplicitCastExpr(ImplicitCastExpr *E) {
Eli Friedmanf0217122008-02-11 01:09:17 +0000178 assert(CGF.getContext().typesAreCompatible(
Chris Lattnerc154ac12008-07-26 22:37:01 +0000179 E->getSubExpr()->getType().getUnqualifiedType(),
180 E->getType().getUnqualifiedType()) &&
181 "Implicit cast types must be compatible");
Anders Carlssonf2712ac2008-01-14 06:28:57 +0000182 Visit(E->getSubExpr());
183}
184
Chris Lattnerc154ac12008-07-26 22:37:01 +0000185void AggExprEmitter::VisitCallExpr(const CallExpr *E) {
Anders Carlsson0ae15412007-10-31 22:04:46 +0000186 RValue RV = CGF.EmitCallExpr(E);
Mike Stump8e06e032009-05-23 20:28:01 +0000187 EmitFinalDestCopy(E, RV);
Anders Carlsson0ae15412007-10-31 22:04:46 +0000188}
Chris Lattnerc154ac12008-07-26 22:37:01 +0000189
190void AggExprEmitter::VisitObjCMessageExpr(ObjCMessageExpr *E) {
Daniel Dunbara04840b2008-08-23 03:46:30 +0000191 RValue RV = CGF.EmitObjCMessageExpr(E);
Mike Stump8e06e032009-05-23 20:28:01 +0000192 EmitFinalDestCopy(E, RV);
Chris Lattner6ee20e32008-06-24 17:04:18 +0000193}
Anders Carlsson0ae15412007-10-31 22:04:46 +0000194
Daniel Dunbar91cc4022008-08-27 06:57:25 +0000195void AggExprEmitter::VisitObjCPropertyRefExpr(ObjCPropertyRefExpr *E) {
196 RValue RV = CGF.EmitObjCPropertyGet(E);
Mike Stump8e06e032009-05-23 20:28:01 +0000197 EmitFinalDestCopy(E, RV);
Daniel Dunbar91cc4022008-08-27 06:57:25 +0000198}
199
Fariborz Jahanianf18d4c82008-11-22 18:39:36 +0000200void AggExprEmitter::VisitObjCKVCRefExpr(ObjCKVCRefExpr *E) {
201 RValue RV = CGF.EmitObjCPropertyGet(E);
Mike Stump8e06e032009-05-23 20:28:01 +0000202 EmitFinalDestCopy(E, RV);
Fariborz Jahanianf18d4c82008-11-22 18:39:36 +0000203}
204
Chris Lattnerc154ac12008-07-26 22:37:01 +0000205void AggExprEmitter::VisitBinComma(const BinaryOperator *E) {
Mike Stumpaa5083e2009-05-23 22:01:27 +0000206 CGF.EmitAnyExpr(E->getLHS());
Mike Stumpa8bac022009-05-23 21:40:07 +0000207 CGF.EmitAggExpr(E->getRHS(), DestPtr, VolatileDest);
Eli Friedman91fc7802008-05-20 07:56:31 +0000208}
209
Chris Lattnerbab0bfb2007-08-31 22:54:14 +0000210void AggExprEmitter::VisitStmtExpr(const StmtExpr *E) {
211 CGF.EmitCompoundStmt(*E->getSubStmt(), true, DestPtr, VolatileDest);
212}
213
Chris Lattnerb50e3902007-08-21 04:25:47 +0000214void AggExprEmitter::VisitBinaryOperator(const BinaryOperator *E) {
Daniel Dunbar9503b782008-08-16 00:56:44 +0000215 CGF.ErrorUnsupported(E, "aggregate binary expression");
Chris Lattnera7300102007-08-21 04:59:27 +0000216}
217
Chris Lattner76cb1892007-08-21 04:43:17 +0000218void AggExprEmitter::VisitBinAssign(const BinaryOperator *E) {
Eli Friedmanf0217122008-02-11 01:09:17 +0000219 // For an assignment to work, the value on the right has
220 // to be compatible with the value on the left.
221 assert(CGF.getContext().typesAreCompatible(
222 E->getLHS()->getType().getUnqualifiedType(),
223 E->getRHS()->getType().getUnqualifiedType())
224 && "Invalid assignment");
Chris Lattnerb50e3902007-08-21 04:25:47 +0000225 LValue LHS = CGF.EmitLValue(E->getLHS());
Chris Lattnerbdb8ffb2007-08-11 00:04:45 +0000226
Daniel Dunbardd851282008-08-30 05:35:15 +0000227 // We have to special case property setters, otherwise we must have
228 // a simple lvalue (no aggregates inside vectors, bitfields).
229 if (LHS.isPropertyRef()) {
Daniel Dunbardd851282008-08-30 05:35:15 +0000230 llvm::Value *AggLoc = DestPtr;
231 if (!AggLoc)
232 AggLoc = CGF.CreateTempAlloca(CGF.ConvertType(E->getRHS()->getType()));
Mike Stump5f507fb2009-05-23 23:48:13 +0000233 CGF.EmitAggExpr(E->getRHS(), AggLoc, VolatileDest);
Daniel Dunbardd851282008-08-30 05:35:15 +0000234 CGF.EmitObjCPropertySet(LHS.getPropertyRefExpr(),
Mike Stump5f507fb2009-05-23 23:48:13 +0000235 RValue::getAggregate(AggLoc, VolatileDest));
Fariborz Jahanianb0973da2008-11-22 22:30:21 +0000236 }
237 else if (LHS.isKVCRef()) {
238 // FIXME: Volatility?
239 llvm::Value *AggLoc = DestPtr;
240 if (!AggLoc)
241 AggLoc = CGF.CreateTempAlloca(CGF.ConvertType(E->getRHS()->getType()));
242 CGF.EmitAggExpr(E->getRHS(), AggLoc, false);
243 CGF.EmitObjCPropertySet(LHS.getKVCRefExpr(),
244 RValue::getAggregate(AggLoc));
Daniel Dunbardd851282008-08-30 05:35:15 +0000245 } else {
246 // Codegen the RHS so that it stores directly into the LHS.
Mike Stumpb491f802009-05-23 04:13:59 +0000247 CGF.EmitAggExpr(E->getRHS(), LHS.getAddress(), LHS.isVolatileQualified());
Mike Stump8e06e032009-05-23 20:28:01 +0000248 EmitFinalDestCopy(E, LHS);
Daniel Dunbardd851282008-08-30 05:35:15 +0000249 }
Chris Lattnerbdb8ffb2007-08-11 00:04:45 +0000250}
251
Chris Lattnerb50e3902007-08-21 04:25:47 +0000252void AggExprEmitter::VisitConditionalOperator(const ConditionalOperator *E) {
Daniel Dunbar6e3a10c2008-11-13 01:38:36 +0000253 llvm::BasicBlock *LHSBlock = CGF.createBasicBlock("cond.true");
254 llvm::BasicBlock *RHSBlock = CGF.createBasicBlock("cond.false");
255 llvm::BasicBlock *ContBlock = CGF.createBasicBlock("cond.end");
Chris Lattnerbdb8ffb2007-08-11 00:04:45 +0000256
Chris Lattnerb50e3902007-08-21 04:25:47 +0000257 llvm::Value *Cond = CGF.EvaluateExprAsBool(E->getCond());
Chris Lattner41b1fca2007-08-26 23:13:56 +0000258 Builder.CreateCondBr(Cond, LHSBlock, RHSBlock);
Chris Lattnerbdb8ffb2007-08-11 00:04:45 +0000259
Chris Lattnerb50e3902007-08-21 04:25:47 +0000260 CGF.EmitBlock(LHSBlock);
Chris Lattnerbdb8ffb2007-08-11 00:04:45 +0000261
262 // Handle the GNU extension for missing LHS.
263 assert(E->getLHS() && "Must have LHS for aggregate value");
264
Chris Lattner55165ba2007-08-21 05:02:10 +0000265 Visit(E->getLHS());
Daniel Dunbar5276caa2008-11-11 09:41:28 +0000266 CGF.EmitBranch(ContBlock);
Chris Lattnerbdb8ffb2007-08-11 00:04:45 +0000267
Chris Lattnerb50e3902007-08-21 04:25:47 +0000268 CGF.EmitBlock(RHSBlock);
Chris Lattnerbdb8ffb2007-08-11 00:04:45 +0000269
Chris Lattner55165ba2007-08-21 05:02:10 +0000270 Visit(E->getRHS());
Daniel Dunbar5276caa2008-11-11 09:41:28 +0000271 CGF.EmitBranch(ContBlock);
Chris Lattnerbdb8ffb2007-08-11 00:04:45 +0000272
Chris Lattnerb50e3902007-08-21 04:25:47 +0000273 CGF.EmitBlock(ContBlock);
Chris Lattnerbdb8ffb2007-08-11 00:04:45 +0000274}
Chris Lattnera7300102007-08-21 04:59:27 +0000275
Eli Friedman42af3972008-05-27 15:51:49 +0000276void AggExprEmitter::VisitVAArgExpr(VAArgExpr *VE) {
Daniel Dunbar63378cf2009-02-11 22:25:55 +0000277 llvm::Value *ArgValue = CGF.EmitVAListRef(VE->getSubExpr());
Anders Carlsson285611e2008-11-04 05:30:00 +0000278 llvm::Value *ArgPtr = CGF.EmitVAArg(ArgValue, VE->getType());
279
Sebastian Redl79f36c92009-01-09 21:09:38 +0000280 if (!ArgPtr) {
Anders Carlsson285611e2008-11-04 05:30:00 +0000281 CGF.ErrorUnsupported(VE, "aggregate va_arg expression");
Sebastian Redl79f36c92009-01-09 21:09:38 +0000282 return;
283 }
284
Mike Stump8e06e032009-05-23 20:28:01 +0000285 EmitFinalDestCopy(VE, LValue::MakeAddr(ArgPtr, 0));
Eli Friedman42af3972008-05-27 15:51:49 +0000286}
287
Anders Carlsson72f48292009-04-17 00:06:03 +0000288void
Anders Carlsson342aadc2009-05-03 17:47:16 +0000289AggExprEmitter::VisitCXXConstructExpr(const CXXConstructExpr *E) {
Anders Carlsson272b5f52009-05-19 04:48:36 +0000290 llvm::Value *V = DestPtr;
Anders Carlsson72f48292009-04-17 00:06:03 +0000291
Anders Carlsson272b5f52009-05-19 04:48:36 +0000292 if (!V) {
293 assert(isa<CXXTempVarDecl>(E->getVarDecl()) &&
294 "Must have a temp var decl when there's no destination!");
295
296 V = CGF.CreateTempAlloca(CGF.ConvertType(E->getVarDecl()->getType()),
297 "tmpvar");
298 }
299
300 CGF.EmitCXXConstructExpr(V, E);
301}
302
303void AggExprEmitter::VisitCXXExprWithTemporaries(CXXExprWithTemporaries *E) {
304 // FIXME: Do something with the temporaries!
305 Visit(E->getSubExpr());
Anders Carlsson72f48292009-04-17 00:06:03 +0000306}
307
Chris Lattner6ee6ab02008-04-04 18:42:16 +0000308void AggExprEmitter::EmitInitializationToLValue(Expr* E, LValue LV) {
309 // FIXME: Are initializers affected by volatile?
Douglas Gregorc9e012a2009-01-29 17:44:32 +0000310 if (isa<ImplicitValueInitExpr>(E)) {
Douglas Gregorf603b472009-01-28 21:54:33 +0000311 EmitNullInitializationToLValue(LV, E->getType());
Douglas Gregorc9e012a2009-01-29 17:44:32 +0000312 } else if (E->getType()->isComplexType()) {
313 CGF.EmitComplexExprIntoAddr(E, LV.getAddress(), false);
Eli Friedmanbb822fb2008-05-12 15:06:05 +0000314 } else if (CGF.hasAggregateLLVMType(E->getType())) {
315 CGF.EmitAnyExpr(E, LV.getAddress(), false);
316 } else {
317 CGF.EmitStoreThroughLValue(CGF.EmitAnyExpr(E), LV, E->getType());
Chris Lattner6ee6ab02008-04-04 18:42:16 +0000318 }
Chris Lattner6ee6ab02008-04-04 18:42:16 +0000319}
320
321void AggExprEmitter::EmitNullInitializationToLValue(LValue LV, QualType T) {
322 if (!CGF.hasAggregateLLVMType(T)) {
323 // For non-aggregates, we can store zero
Daniel Dunbarde813312008-08-06 05:32:55 +0000324 llvm::Value *Null = llvm::Constant::getNullValue(CGF.ConvertType(T));
325 CGF.EmitStoreThroughLValue(RValue::get(Null), LV, T);
Lauro Ramos Venancioa62b1df2008-02-19 19:27:31 +0000326 } else {
Chris Lattner6ee6ab02008-04-04 18:42:16 +0000327 // Otherwise, just memset the whole thing to zero. This is legal
328 // because in LLVM, all default initializers are guaranteed to have a
329 // bit pattern of all zeros.
Eli Friedmanb836cd32009-04-13 21:47:26 +0000330 // FIXME: That isn't true for member pointers!
Chris Lattner6ee6ab02008-04-04 18:42:16 +0000331 // There's a potential optimization opportunity in combining
332 // memsets; that would be easy for arrays, but relatively
333 // difficult for structures with the current code.
Eli Friedman2494bc32009-03-28 03:10:45 +0000334 CGF.EmitMemSetToZero(LV.getAddress(), T);
Chris Lattner6ee6ab02008-04-04 18:42:16 +0000335 }
336}
337
Chris Lattner6ee6ab02008-04-04 18:42:16 +0000338void AggExprEmitter::VisitInitListExpr(InitListExpr *E) {
Eli Friedman2a4f27c2008-12-02 01:17:45 +0000339#if 0
340 // FIXME: Disabled while we figure out what to do about
341 // test/CodeGen/bitfield.c
342 //
Mike Stumpba2cb0e2009-05-16 07:57:57 +0000343 // If we can, prefer a copy from a global; this is a lot less code for long
344 // globals, and it's easier for the current optimizers to analyze.
345 // FIXME: Should we really be doing this? Should we try to avoid cases where
346 // we emit a global with a lot of zeros? Should we try to avoid short
347 // globals?
Eli Friedmandee41122009-01-25 02:32:41 +0000348 if (E->isConstantInitializer(CGF.getContext(), 0)) {
Eli Friedmane9b227d2008-11-30 02:11:09 +0000349 llvm::Constant* C = CGF.CGM.EmitConstantExpr(E, &CGF);
350 llvm::GlobalVariable* GV =
351 new llvm::GlobalVariable(C->getType(), true,
352 llvm::GlobalValue::InternalLinkage,
353 C, "", &CGF.CGM.getModule(), 0);
Mike Stump8e06e032009-05-23 20:28:01 +0000354 EmitFinalDestCopy(E, LValue::MakeAddr(GV, 0));
Eli Friedmane9b227d2008-11-30 02:11:09 +0000355 return;
356 }
Eli Friedman2a4f27c2008-12-02 01:17:45 +0000357#endif
Douglas Gregor9fddded2009-01-29 19:42:23 +0000358 if (E->hadArrayRangeDesignator()) {
359 CGF.ErrorUnsupported(E, "GNU array range designator extension");
360 }
361
Chris Lattner6ee6ab02008-04-04 18:42:16 +0000362 // Handle initialization of an array.
363 if (E->getType()->isArrayType()) {
364 const llvm::PointerType *APType =
365 cast<llvm::PointerType>(DestPtr->getType());
366 const llvm::ArrayType *AType =
367 cast<llvm::ArrayType>(APType->getElementType());
368
369 uint64_t NumInitElements = E->getNumInits();
Eli Friedman48ec5622008-05-19 17:51:16 +0000370
Chris Lattnerc154ac12008-07-26 22:37:01 +0000371 if (E->getNumInits() > 0) {
372 QualType T1 = E->getType();
373 QualType T2 = E->getInit(0)->getType();
374 if (CGF.getContext().getCanonicalType(T1).getUnqualifiedType() ==
375 CGF.getContext().getCanonicalType(T2).getUnqualifiedType()) {
376 EmitAggLoadOfLValue(E->getInit(0));
377 return;
378 }
Eli Friedman48ec5622008-05-19 17:51:16 +0000379 }
380
Chris Lattner6ee6ab02008-04-04 18:42:16 +0000381 uint64_t NumArrayElements = AType->getNumElements();
Chris Lattnera1923f62008-08-04 07:31:14 +0000382 QualType ElementType = CGF.getContext().getCanonicalType(E->getType());
Douglas Gregorf603b472009-01-28 21:54:33 +0000383 ElementType = CGF.getContext().getAsArrayType(ElementType)->getElementType();
Chris Lattner6ee6ab02008-04-04 18:42:16 +0000384
Chris Lattnera1923f62008-08-04 07:31:14 +0000385 unsigned CVRqualifier = ElementType.getCVRQualifiers();
Eli Friedman2e630542008-06-13 23:01:12 +0000386
Chris Lattner6ee6ab02008-04-04 18:42:16 +0000387 for (uint64_t i = 0; i != NumArrayElements; ++i) {
388 llvm::Value *NextVal = Builder.CreateStructGEP(DestPtr, i, ".array");
389 if (i < NumInitElements)
Eli Friedman2e630542008-06-13 23:01:12 +0000390 EmitInitializationToLValue(E->getInit(i),
391 LValue::MakeAddr(NextVal, CVRqualifier));
Chris Lattner6ee6ab02008-04-04 18:42:16 +0000392 else
Eli Friedman2e630542008-06-13 23:01:12 +0000393 EmitNullInitializationToLValue(LValue::MakeAddr(NextVal, CVRqualifier),
Chris Lattner6ee6ab02008-04-04 18:42:16 +0000394 ElementType);
Lauro Ramos Venancioa62b1df2008-02-19 19:27:31 +0000395 }
Chris Lattner6ee6ab02008-04-04 18:42:16 +0000396 return;
397 }
398
399 assert(E->getType()->isRecordType() && "Only support structs/unions here!");
400
401 // Do struct initialization; this code just sets each individual member
402 // to the approprate value. This makes bitfield support automatic;
403 // the disadvantage is that the generated code is more difficult for
404 // the optimizer, especially with bitfields.
405 unsigned NumInitElements = E->getNumInits();
406 RecordDecl *SD = E->getType()->getAsRecordType()->getDecl();
Chris Lattner6ee6ab02008-04-04 18:42:16 +0000407 unsigned CurInitVal = 0;
Douglas Gregor82462762009-01-29 16:53:55 +0000408
409 if (E->getType()->isUnionType()) {
410 // Only initialize one field of a union. The field itself is
411 // specified by the initializer list.
412 if (!E->getInitializedFieldInUnion()) {
413 // Empty union; we have nothing to do.
414
415#ifndef NDEBUG
416 // Make sure that it's really an empty and not a failure of
417 // semantic analysis.
Douglas Gregorc55b0b02009-04-09 21:40:53 +0000418 for (RecordDecl::field_iterator Field = SD->field_begin(CGF.getContext()),
419 FieldEnd = SD->field_end(CGF.getContext());
Douglas Gregor82462762009-01-29 16:53:55 +0000420 Field != FieldEnd; ++Field)
421 assert(Field->isUnnamedBitfield() && "Only unnamed bitfields allowed");
422#endif
423 return;
424 }
425
426 // FIXME: volatility
427 FieldDecl *Field = E->getInitializedFieldInUnion();
428 LValue FieldLoc = CGF.EmitLValueForField(DestPtr, Field, true, 0);
429
430 if (NumInitElements) {
431 // Store the initializer into the field
432 EmitInitializationToLValue(E->getInit(0), FieldLoc);
433 } else {
434 // Default-initialize to null
435 EmitNullInitializationToLValue(FieldLoc, Field->getType());
436 }
437
438 return;
439 }
Chris Lattner6ee6ab02008-04-04 18:42:16 +0000440
441 // Here we iterate over the fields; this makes it simpler to both
442 // default-initialize fields and skip over unnamed fields.
Douglas Gregorc55b0b02009-04-09 21:40:53 +0000443 for (RecordDecl::field_iterator Field = SD->field_begin(CGF.getContext()),
444 FieldEnd = SD->field_end(CGF.getContext());
Douglas Gregor8acb7272008-12-11 16:49:14 +0000445 Field != FieldEnd; ++Field) {
446 // We're done once we hit the flexible array member
447 if (Field->getType()->isIncompleteArrayType())
448 break;
449
Douglas Gregor36dd0c52009-01-28 23:36:17 +0000450 if (Field->isUnnamedBitfield())
Chris Lattner6ee6ab02008-04-04 18:42:16 +0000451 continue;
Douglas Gregor36dd0c52009-01-28 23:36:17 +0000452
Eli Friedman2e630542008-06-13 23:01:12 +0000453 // FIXME: volatility
Douglas Gregor82462762009-01-29 16:53:55 +0000454 LValue FieldLoc = CGF.EmitLValueForField(DestPtr, *Field, false, 0);
Chris Lattner6ee6ab02008-04-04 18:42:16 +0000455 if (CurInitVal < NumInitElements) {
456 // Store the initializer into the field
Chris Lattner6ee6ab02008-04-04 18:42:16 +0000457 EmitInitializationToLValue(E->getInit(CurInitVal++), FieldLoc);
458 } else {
459 // We're out of initalizers; default-initialize to null
Douglas Gregor8acb7272008-12-11 16:49:14 +0000460 EmitNullInitializationToLValue(FieldLoc, Field->getType());
Chris Lattner6ee6ab02008-04-04 18:42:16 +0000461 }
Lauro Ramos Venancioa62b1df2008-02-19 19:27:31 +0000462 }
Devang Patelddde6d52007-10-26 17:44:44 +0000463}
464
Chris Lattnera7300102007-08-21 04:59:27 +0000465//===----------------------------------------------------------------------===//
466// Entry Points into this File
467//===----------------------------------------------------------------------===//
468
469/// EmitAggExpr - Emit the computation of the specified expression of
470/// aggregate type. The result is computed into DestPtr. Note that if
471/// DestPtr is null, the value of the aggregate expression is not needed.
472void CodeGenFunction::EmitAggExpr(const Expr *E, llvm::Value *DestPtr,
473 bool VolatileDest) {
474 assert(E && hasAggregateLLVMType(E->getType()) &&
475 "Invalid aggregate expression to emit");
476
477 AggExprEmitter(*this, DestPtr, VolatileDest).Visit(const_cast<Expr*>(E));
478}
Daniel Dunbarbde0c9d2008-09-09 20:49:46 +0000479
480void CodeGenFunction::EmitAggregateClear(llvm::Value *DestPtr, QualType Ty) {
481 assert(!Ty->isAnyComplexType() && "Shouldn't happen for complex");
482
483 EmitMemSetToZero(DestPtr, Ty);
484}
485
486void CodeGenFunction::EmitAggregateCopy(llvm::Value *DestPtr,
Mike Stump577d0312009-05-23 22:29:41 +0000487 llvm::Value *SrcPtr, QualType Ty,
488 bool isVolatile) {
Daniel Dunbarbde0c9d2008-09-09 20:49:46 +0000489 assert(!Ty->isAnyComplexType() && "Shouldn't happen for complex");
490
Chris Lattner1ec02512009-02-28 18:31:01 +0000491 // Aggregate assignment turns into llvm.memcpy. This is almost valid per
Chris Lattner1db26ed2009-02-28 18:18:58 +0000492 // C99 6.5.16.1p3, which states "If the value being stored in an object is
493 // read from another object that overlaps in anyway the storage of the first
494 // object, then the overlap shall be exact and the two objects shall have
495 // qualified or unqualified versions of a compatible type."
496 //
Chris Lattner1ec02512009-02-28 18:31:01 +0000497 // memcpy is not defined if the source and destination pointers are exactly
Chris Lattner1db26ed2009-02-28 18:18:58 +0000498 // equal, but other compilers do this optimization, and almost every memcpy
499 // implementation handles this case safely. If there is a libc that does not
500 // safely handle this, we can add a target hook.
Daniel Dunbarbde0c9d2008-09-09 20:49:46 +0000501 const llvm::Type *BP = llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
502 if (DestPtr->getType() != BP)
503 DestPtr = Builder.CreateBitCast(DestPtr, BP, "tmp");
504 if (SrcPtr->getType() != BP)
505 SrcPtr = Builder.CreateBitCast(SrcPtr, BP, "tmp");
506
507 // Get size and alignment info for this aggregate.
508 std::pair<uint64_t, unsigned> TypeInfo = getContext().getTypeInfo(Ty);
509
510 // FIXME: Handle variable sized types.
511 const llvm::Type *IntPtr = llvm::IntegerType::get(LLVMPointerWidth);
512
Mike Stumpb491f802009-05-23 04:13:59 +0000513 // FIXME: If we have a volatile struct, the optimizer can remove what might
514 // appear to be `extra' memory ops:
515 //
516 // volatile struct { int i; } a, b;
517 //
518 // int main() {
519 // a = b;
520 // a = b;
521 // }
522 //
523 // either, we need to use a differnt call here, or the backend needs to be
Mike Stump577d0312009-05-23 22:29:41 +0000524 // taught to not do this. We use isVolatile to indicate when either the
525 // source or the destination is volatile.
Chris Lattner1db26ed2009-02-28 18:18:58 +0000526 Builder.CreateCall4(CGM.getMemCpyFn(),
Daniel Dunbarbde0c9d2008-09-09 20:49:46 +0000527 DestPtr, SrcPtr,
528 // TypeInfo.first describes size in bits.
529 llvm::ConstantInt::get(IntPtr, TypeInfo.first/8),
530 llvm::ConstantInt::get(llvm::Type::Int32Ty,
531 TypeInfo.second/8));
532}