blob: 9181bf665450a999a26cfce26eb2d28ce83f73b2 [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"
Daniel Dunbarde7fb842008-08-11 05:00:27 +000016#include "clang/AST/ASTContext.h"
17#include "clang/AST/StmtVisitor.h"
Chris Lattner883f6a72007-08-11 00:04:45 +000018#include "llvm/Constants.h"
19#include "llvm/Function.h"
Devang Patel636c3d02007-10-26 17:44:44 +000020#include "llvm/GlobalVariable.h"
Chris Lattner9c033562007-08-21 04:25:47 +000021#include "llvm/Support/Compiler.h"
Chris Lattnerf81557c2008-04-04 18:42:16 +000022#include "llvm/Intrinsics.h"
Chris Lattneraf6f5282007-08-10 20:13:28 +000023using namespace clang;
24using namespace CodeGen;
Chris Lattner883f6a72007-08-11 00:04:45 +000025
Chris Lattner9c033562007-08-21 04:25:47 +000026//===----------------------------------------------------------------------===//
27// Aggregate Expression Emitter
28//===----------------------------------------------------------------------===//
29
30namespace {
31class VISIBILITY_HIDDEN AggExprEmitter : public StmtVisitor<AggExprEmitter> {
32 CodeGenFunction &CGF;
Daniel Dunbar45d196b2008-11-01 01:53:16 +000033 CGBuilderTy &Builder;
Chris Lattner9c033562007-08-21 04:25:47 +000034 llvm::Value *DestPtr;
35 bool VolatileDest;
36public:
37 AggExprEmitter(CodeGenFunction &cgf, llvm::Value *destPtr, bool volatileDest)
Chris Lattnerbfc0c1a2007-08-26 23:13:56 +000038 : CGF(cgf), Builder(CGF.Builder),
39 DestPtr(destPtr), VolatileDest(volatileDest) {
Chris Lattner9c033562007-08-21 04:25:47 +000040 }
41
Chris Lattneree755f92007-08-21 04:59:27 +000042 //===--------------------------------------------------------------------===//
43 // Utilities
44 //===--------------------------------------------------------------------===//
45
Chris Lattner9c033562007-08-21 04:25:47 +000046 /// EmitAggLoadOfLValue - Given an expression with aggregate type that
47 /// represents a value lvalue, this method emits the address of the lvalue,
48 /// then loads the result into DestPtr.
49 void EmitAggLoadOfLValue(const Expr *E);
50
Lauro Ramos Venancio13e22cf2008-02-19 22:04:22 +000051 void EmitNonConstInit(InitListExpr *E);
Eli Friedman922696f2008-05-19 17:51:16 +000052
Chris Lattneree755f92007-08-21 04:59:27 +000053 //===--------------------------------------------------------------------===//
54 // Visitor Methods
55 //===--------------------------------------------------------------------===//
56
Chris Lattner9c033562007-08-21 04:25:47 +000057 void VisitStmt(Stmt *S) {
Daniel Dunbar488e9932008-08-16 00:56:44 +000058 CGF.ErrorUnsupported(S, "aggregate expression");
Chris Lattner9c033562007-08-21 04:25:47 +000059 }
60 void VisitParenExpr(ParenExpr *PE) { Visit(PE->getSubExpr()); }
Eli Friedman12444a22009-01-27 09:03:41 +000061 void VisitUnaryExtension(UnaryOperator *E) { Visit(E->getSubExpr()); }
Chris Lattner9c033562007-08-21 04:25:47 +000062
63 // l-values.
Seo Sanghyeon9b73b392007-12-14 02:04:12 +000064 void VisitDeclRefExpr(DeclRefExpr *DRE) { EmitAggLoadOfLValue(DRE); }
65 void VisitMemberExpr(MemberExpr *ME) { EmitAggLoadOfLValue(ME); }
66 void VisitUnaryDeref(UnaryOperator *E) { EmitAggLoadOfLValue(E); }
Seo Sanghyeonad6ebd62007-12-23 03:11:58 +000067 void VisitStringLiteral(StringLiteral *E) { EmitAggLoadOfLValue(E); }
Eli Friedmanb1851242008-05-27 15:51:49 +000068 void VisitCompoundLiteralExpr(CompoundLiteralExpr *E)
69 { EmitAggLoadOfLValue(E); }
Seo Sanghyeon9b73b392007-12-14 02:04:12 +000070
71 void VisitArraySubscriptExpr(ArraySubscriptExpr *E) {
72 EmitAggLoadOfLValue(E);
73 }
Chris Lattner9c033562007-08-21 04:25:47 +000074
75 // Operators.
76 // case Expr::UnaryOperatorClass:
Chris Lattner9c033562007-08-21 04:25:47 +000077 // case Expr::CastExprClass:
Nuno Lopes7e916272009-01-15 20:14:33 +000078 void VisitCStyleCastExpr(CStyleCastExpr *E);
Anders Carlssone4707ff2008-01-14 06:28:57 +000079 void VisitImplicitCastExpr(ImplicitCastExpr *E);
Anders Carlsson148fe672007-10-31 22:04:46 +000080 void VisitCallExpr(const CallExpr *E);
Chris Lattnerb2d963f2007-08-31 22:54:14 +000081 void VisitStmtExpr(const StmtExpr *E);
Chris Lattner9c033562007-08-21 04:25:47 +000082 void VisitBinaryOperator(const BinaryOperator *BO);
Chris Lattner03d6fb92007-08-21 04:43:17 +000083 void VisitBinAssign(const BinaryOperator *E);
Nate Begeman796ef3d2008-01-31 05:38:29 +000084 void VisitOverloadExpr(const OverloadExpr *E);
Eli Friedman07fa52a2008-05-20 07:56:31 +000085 void VisitBinComma(const BinaryOperator *E);
Chris Lattner9c033562007-08-21 04:25:47 +000086
Chris Lattner8fdf3282008-06-24 17:04:18 +000087 void VisitObjCMessageExpr(ObjCMessageExpr *E);
Daniel Dunbar0a04d772008-08-23 10:51:21 +000088 void VisitObjCIvarRefExpr(ObjCIvarRefExpr *E) {
89 EmitAggLoadOfLValue(E);
90 }
Daniel Dunbar9c3fc702008-08-27 06:57:25 +000091 void VisitObjCPropertyRefExpr(ObjCPropertyRefExpr *E);
Fariborz Jahanian5daf5702008-11-22 18:39:36 +000092 void VisitObjCKVCRefExpr(ObjCKVCRefExpr *E);
Chris Lattner9c033562007-08-21 04:25:47 +000093
94 void VisitConditionalOperator(const ConditionalOperator *CO);
Devang Patel636c3d02007-10-26 17:44:44 +000095 void VisitInitListExpr(InitListExpr *E);
Chris Lattner04421082008-04-08 04:40:51 +000096 void VisitCXXDefaultArgExpr(CXXDefaultArgExpr *DAE) {
97 Visit(DAE->getExpr());
98 }
Eli Friedmanb1851242008-05-27 15:51:49 +000099 void VisitVAArgExpr(VAArgExpr *E);
Chris Lattnerf81557c2008-04-04 18:42:16 +0000100
101 void EmitInitializationToLValue(Expr *E, LValue Address);
102 void EmitNullInitializationToLValue(LValue Address, QualType T);
Chris Lattner9c033562007-08-21 04:25:47 +0000103 // case Expr::ChooseExprClass:
Lauro Ramos Venancio305762c2008-02-18 22:44:02 +0000104
Chris Lattner9c033562007-08-21 04:25:47 +0000105};
106} // end anonymous namespace.
107
Chris Lattneree755f92007-08-21 04:59:27 +0000108//===----------------------------------------------------------------------===//
109// Utilities
110//===----------------------------------------------------------------------===//
Chris Lattner9c033562007-08-21 04:25:47 +0000111
Chris Lattner883f6a72007-08-11 00:04:45 +0000112/// EmitAggLoadOfLValue - Given an expression with aggregate type that
113/// represents a value lvalue, this method emits the address of the lvalue,
114/// then loads the result into DestPtr.
Chris Lattner9c033562007-08-21 04:25:47 +0000115void AggExprEmitter::EmitAggLoadOfLValue(const Expr *E) {
116 LValue LV = CGF.EmitLValue(E);
Chris Lattner883f6a72007-08-11 00:04:45 +0000117 assert(LV.isSimple() && "Can't have aggregate bitfield, vector, etc");
118 llvm::Value *SrcPtr = LV.getAddress();
119
120 // If the result is ignored, don't copy from the value.
121 if (DestPtr == 0)
122 // FIXME: If the source is volatile, we must read from it.
123 return;
124
Daniel Dunbar7482d122008-09-09 20:49:46 +0000125 CGF.EmitAggregateCopy(DestPtr, SrcPtr, E->getType());
Chris Lattner883f6a72007-08-11 00:04:45 +0000126}
127
Chris Lattneree755f92007-08-21 04:59:27 +0000128//===----------------------------------------------------------------------===//
129// Visitor Methods
130//===----------------------------------------------------------------------===//
131
Nuno Lopes7e916272009-01-15 20:14:33 +0000132void AggExprEmitter::VisitCStyleCastExpr(CStyleCastExpr *E) {
133 // GCC union extension
134 if (E->getType()->isUnionType()) {
135 RecordDecl *SD = E->getType()->getAsRecordType()->getDecl();
136 LValue FieldLoc = CGF.EmitLValueForField(DestPtr, *SD->field_begin(), true, 0);
137 EmitInitializationToLValue(E->getSubExpr(), FieldLoc);
138 return;
139 }
140
141 Visit(E->getSubExpr());
142}
143
Chris Lattner96196622008-07-26 22:37:01 +0000144void AggExprEmitter::VisitImplicitCastExpr(ImplicitCastExpr *E) {
Eli Friedmanff6e2b72008-02-11 01:09:17 +0000145 assert(CGF.getContext().typesAreCompatible(
Chris Lattner96196622008-07-26 22:37:01 +0000146 E->getSubExpr()->getType().getUnqualifiedType(),
147 E->getType().getUnqualifiedType()) &&
148 "Implicit cast types must be compatible");
Anders Carlssone4707ff2008-01-14 06:28:57 +0000149 Visit(E->getSubExpr());
150}
151
Chris Lattner96196622008-07-26 22:37:01 +0000152void AggExprEmitter::VisitCallExpr(const CallExpr *E) {
Anders Carlsson148fe672007-10-31 22:04:46 +0000153 RValue RV = CGF.EmitCallExpr(E);
154 assert(RV.isAggregate() && "Return value must be aggregate value!");
155
156 // If the result is ignored, don't copy from the value.
157 if (DestPtr == 0)
158 // FIXME: If the source is volatile, we must read from it.
159 return;
160
Daniel Dunbar7482d122008-09-09 20:49:46 +0000161 CGF.EmitAggregateCopy(DestPtr, RV.getAggregateAddr(), E->getType());
Anders Carlsson148fe672007-10-31 22:04:46 +0000162}
Chris Lattner96196622008-07-26 22:37:01 +0000163
164void AggExprEmitter::VisitObjCMessageExpr(ObjCMessageExpr *E) {
Daniel Dunbar8f2926b2008-08-23 03:46:30 +0000165 RValue RV = CGF.EmitObjCMessageExpr(E);
166 assert(RV.isAggregate() && "Return value must be aggregate value!");
Chris Lattner8fdf3282008-06-24 17:04:18 +0000167
168 // If the result is ignored, don't copy from the value.
169 if (DestPtr == 0)
Daniel Dunbar8f2926b2008-08-23 03:46:30 +0000170 // FIXME: If the source is volatile, we must read from it.
Chris Lattner8fdf3282008-06-24 17:04:18 +0000171 return;
172
Daniel Dunbar7482d122008-09-09 20:49:46 +0000173 CGF.EmitAggregateCopy(DestPtr, RV.getAggregateAddr(), E->getType());
Chris Lattner8fdf3282008-06-24 17:04:18 +0000174}
Anders Carlsson148fe672007-10-31 22:04:46 +0000175
Daniel Dunbar9c3fc702008-08-27 06:57:25 +0000176void AggExprEmitter::VisitObjCPropertyRefExpr(ObjCPropertyRefExpr *E) {
177 RValue RV = CGF.EmitObjCPropertyGet(E);
178 assert(RV.isAggregate() && "Return value must be aggregate value!");
179
180 // If the result is ignored, don't copy from the value.
181 if (DestPtr == 0)
182 // FIXME: If the source is volatile, we must read from it.
183 return;
184
Daniel Dunbar7482d122008-09-09 20:49:46 +0000185 CGF.EmitAggregateCopy(DestPtr, RV.getAggregateAddr(), E->getType());
Daniel Dunbar9c3fc702008-08-27 06:57:25 +0000186}
187
Fariborz Jahanian5daf5702008-11-22 18:39:36 +0000188void AggExprEmitter::VisitObjCKVCRefExpr(ObjCKVCRefExpr *E) {
189 RValue RV = CGF.EmitObjCPropertyGet(E);
190 assert(RV.isAggregate() && "Return value must be aggregate value!");
191
192 // If the result is ignored, don't copy from the value.
193 if (DestPtr == 0)
194 // FIXME: If the source is volatile, we must read from it.
195 return;
196
197 CGF.EmitAggregateCopy(DestPtr, RV.getAggregateAddr(), E->getType());
198}
199
Chris Lattner96196622008-07-26 22:37:01 +0000200void AggExprEmitter::VisitOverloadExpr(const OverloadExpr *E) {
Nate Begeman796ef3d2008-01-31 05:38:29 +0000201 RValue RV = CGF.EmitCallExpr(E->getFn(), E->arg_begin(),
Ted Kremenek55499762008-06-17 02:43:46 +0000202 E->arg_end(CGF.getContext()));
203
Nate Begeman796ef3d2008-01-31 05:38:29 +0000204 assert(RV.isAggregate() && "Return value must be aggregate value!");
205
206 // If the result is ignored, don't copy from the value.
207 if (DestPtr == 0)
208 // FIXME: If the source is volatile, we must read from it.
209 return;
210
Daniel Dunbar7482d122008-09-09 20:49:46 +0000211 CGF.EmitAggregateCopy(DestPtr, RV.getAggregateAddr(), E->getType());
Nate Begeman796ef3d2008-01-31 05:38:29 +0000212}
213
Chris Lattner96196622008-07-26 22:37:01 +0000214void AggExprEmitter::VisitBinComma(const BinaryOperator *E) {
Eli Friedman07fa52a2008-05-20 07:56:31 +0000215 CGF.EmitAnyExpr(E->getLHS());
216 CGF.EmitAggExpr(E->getRHS(), DestPtr, false);
217}
218
Chris Lattnerb2d963f2007-08-31 22:54:14 +0000219void AggExprEmitter::VisitStmtExpr(const StmtExpr *E) {
220 CGF.EmitCompoundStmt(*E->getSubStmt(), true, DestPtr, VolatileDest);
221}
222
Chris Lattner9c033562007-08-21 04:25:47 +0000223void AggExprEmitter::VisitBinaryOperator(const BinaryOperator *E) {
Daniel Dunbar488e9932008-08-16 00:56:44 +0000224 CGF.ErrorUnsupported(E, "aggregate binary expression");
Chris Lattneree755f92007-08-21 04:59:27 +0000225}
226
Chris Lattner03d6fb92007-08-21 04:43:17 +0000227void AggExprEmitter::VisitBinAssign(const BinaryOperator *E) {
Eli Friedmanff6e2b72008-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.
230 assert(CGF.getContext().typesAreCompatible(
231 E->getLHS()->getType().getUnqualifiedType(),
232 E->getRHS()->getType().getUnqualifiedType())
233 && "Invalid assignment");
Chris Lattner9c033562007-08-21 04:25:47 +0000234 LValue LHS = CGF.EmitLValue(E->getLHS());
Chris Lattner883f6a72007-08-11 00:04:45 +0000235
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000236 // We have to special case property setters, otherwise we must have
237 // a simple lvalue (no aggregates inside vectors, bitfields).
238 if (LHS.isPropertyRef()) {
239 // FIXME: Volatility?
240 llvm::Value *AggLoc = DestPtr;
241 if (!AggLoc)
242 AggLoc = CGF.CreateTempAlloca(CGF.ConvertType(E->getRHS()->getType()));
243 CGF.EmitAggExpr(E->getRHS(), AggLoc, false);
244 CGF.EmitObjCPropertySet(LHS.getPropertyRefExpr(),
245 RValue::getAggregate(AggLoc));
Fariborz Jahanian43f44702008-11-22 22:30:21 +0000246 }
247 else if (LHS.isKVCRef()) {
248 // FIXME: Volatility?
249 llvm::Value *AggLoc = DestPtr;
250 if (!AggLoc)
251 AggLoc = CGF.CreateTempAlloca(CGF.ConvertType(E->getRHS()->getType()));
252 CGF.EmitAggExpr(E->getRHS(), AggLoc, false);
253 CGF.EmitObjCPropertySet(LHS.getKVCRefExpr(),
254 RValue::getAggregate(AggLoc));
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000255 } else {
256 // Codegen the RHS so that it stores directly into the LHS.
257 CGF.EmitAggExpr(E->getRHS(), LHS.getAddress(), false /*FIXME: VOLATILE LHS*/);
258
259 if (DestPtr == 0)
260 return;
261
262 // If the result of the assignment is used, copy the RHS there also.
Daniel Dunbar7482d122008-09-09 20:49:46 +0000263 CGF.EmitAggregateCopy(DestPtr, LHS.getAddress(), E->getType());
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000264 }
Chris Lattner883f6a72007-08-11 00:04:45 +0000265}
266
Chris Lattner9c033562007-08-21 04:25:47 +0000267void AggExprEmitter::VisitConditionalOperator(const ConditionalOperator *E) {
Daniel Dunbar9615ecb2008-11-13 01:38:36 +0000268 llvm::BasicBlock *LHSBlock = CGF.createBasicBlock("cond.true");
269 llvm::BasicBlock *RHSBlock = CGF.createBasicBlock("cond.false");
270 llvm::BasicBlock *ContBlock = CGF.createBasicBlock("cond.end");
Chris Lattner883f6a72007-08-11 00:04:45 +0000271
Chris Lattner9c033562007-08-21 04:25:47 +0000272 llvm::Value *Cond = CGF.EvaluateExprAsBool(E->getCond());
Chris Lattnerbfc0c1a2007-08-26 23:13:56 +0000273 Builder.CreateCondBr(Cond, LHSBlock, RHSBlock);
Chris Lattner883f6a72007-08-11 00:04:45 +0000274
Chris Lattner9c033562007-08-21 04:25:47 +0000275 CGF.EmitBlock(LHSBlock);
Chris Lattner883f6a72007-08-11 00:04:45 +0000276
277 // Handle the GNU extension for missing LHS.
278 assert(E->getLHS() && "Must have LHS for aggregate value");
279
Chris Lattnerc748f272007-08-21 05:02:10 +0000280 Visit(E->getLHS());
Daniel Dunbard57a8712008-11-11 09:41:28 +0000281 CGF.EmitBranch(ContBlock);
Chris Lattner883f6a72007-08-11 00:04:45 +0000282
Chris Lattner9c033562007-08-21 04:25:47 +0000283 CGF.EmitBlock(RHSBlock);
Chris Lattner883f6a72007-08-11 00:04:45 +0000284
Chris Lattnerc748f272007-08-21 05:02:10 +0000285 Visit(E->getRHS());
Daniel Dunbard57a8712008-11-11 09:41:28 +0000286 CGF.EmitBranch(ContBlock);
Chris Lattner883f6a72007-08-11 00:04:45 +0000287
Chris Lattner9c033562007-08-21 04:25:47 +0000288 CGF.EmitBlock(ContBlock);
Chris Lattner883f6a72007-08-11 00:04:45 +0000289}
Chris Lattneree755f92007-08-21 04:59:27 +0000290
Eli Friedmanb1851242008-05-27 15:51:49 +0000291void AggExprEmitter::VisitVAArgExpr(VAArgExpr *VE) {
292 llvm::Value *ArgValue = CGF.EmitLValue(VE->getSubExpr()).getAddress();
Anders Carlssonddf7cac2008-11-04 05:30:00 +0000293 llvm::Value *ArgPtr = CGF.EmitVAArg(ArgValue, VE->getType());
294
Sebastian Redl0262f022009-01-09 21:09:38 +0000295 if (!ArgPtr) {
Anders Carlssonddf7cac2008-11-04 05:30:00 +0000296 CGF.ErrorUnsupported(VE, "aggregate va_arg expression");
Sebastian Redl0262f022009-01-09 21:09:38 +0000297 return;
298 }
299
Eli Friedmanb1851242008-05-27 15:51:49 +0000300 if (DestPtr)
Eli Friedman1e692ac2008-06-13 23:01:12 +0000301 // FIXME: volatility
Anders Carlssonddf7cac2008-11-04 05:30:00 +0000302 CGF.EmitAggregateCopy(DestPtr, ArgPtr, VE->getType());
Eli Friedmanb1851242008-05-27 15:51:49 +0000303}
304
Lauro Ramos Venancio13e22cf2008-02-19 22:04:22 +0000305void AggExprEmitter::EmitNonConstInit(InitListExpr *E) {
Chris Lattnerbe20bb52008-10-26 23:53:12 +0000306 if (E->hadDesignators()) {
307 CGF.ErrorUnsupported(E, "initializer list with designators");
308 return;
309 }
310
Lauro Ramos Venancio13e22cf2008-02-19 22:04:22 +0000311 const llvm::PointerType *APType =
312 cast<llvm::PointerType>(DestPtr->getType());
313 const llvm::Type *DestType = APType->getElementType();
Lauro Ramos Venancio305762c2008-02-18 22:44:02 +0000314
315 if (const llvm::ArrayType *AType = dyn_cast<llvm::ArrayType>(DestType)) {
Lauro Ramos Venancio13e22cf2008-02-19 22:04:22 +0000316 unsigned NumInitElements = E->getNumInits();
Lauro Ramos Venancio305762c2008-02-18 22:44:02 +0000317
Lauro Ramos Venancio305762c2008-02-18 22:44:02 +0000318 unsigned i;
319 for (i = 0; i != NumInitElements; ++i) {
Chris Lattner36b6a0a2008-03-19 05:19:41 +0000320 llvm::Value *NextVal = Builder.CreateStructGEP(DestPtr, i, ".array");
Lauro Ramos Venancio13e22cf2008-02-19 22:04:22 +0000321 Expr *Init = E->getInit(i);
322 if (isa<InitListExpr>(Init))
323 CGF.EmitAggExpr(Init, NextVal, VolatileDest);
324 else
Eli Friedman1e692ac2008-06-13 23:01:12 +0000325 // FIXME: volatility
Lauro Ramos Venancio13e22cf2008-02-19 22:04:22 +0000326 Builder.CreateStore(CGF.EmitScalarExpr(Init), NextVal);
Lauro Ramos Venancio305762c2008-02-18 22:44:02 +0000327 }
328
329 // Emit remaining default initializers
330 unsigned NumArrayElements = AType->getNumElements();
Lauro Ramos Venancio13e22cf2008-02-19 22:04:22 +0000331 QualType QType = E->getInit(0)->getType();
332 const llvm::Type *EType = AType->getElementType();
Lauro Ramos Venancio305762c2008-02-18 22:44:02 +0000333 for (/*Do not initialize i*/; i < NumArrayElements; ++i) {
Chris Lattner36b6a0a2008-03-19 05:19:41 +0000334 llvm::Value *NextVal = Builder.CreateStructGEP(DestPtr, i, ".array");
Dan Gohmand79a7262008-05-22 22:12:56 +0000335 if (EType->isSingleValueType())
Eli Friedman1e692ac2008-06-13 23:01:12 +0000336 // FIXME: volatility
Lauro Ramos Venancio13e22cf2008-02-19 22:04:22 +0000337 Builder.CreateStore(llvm::Constant::getNullValue(EType), NextVal);
338 else
Daniel Dunbar7482d122008-09-09 20:49:46 +0000339 CGF.EmitAggregateClear(NextVal, QType);
Lauro Ramos Venancio305762c2008-02-18 22:44:02 +0000340 }
Lauro Ramos Venancio13e22cf2008-02-19 22:04:22 +0000341 } else
342 assert(false && "Invalid initializer");
Lauro Ramos Venancio305762c2008-02-18 22:44:02 +0000343}
344
Chris Lattnerf81557c2008-04-04 18:42:16 +0000345void AggExprEmitter::EmitInitializationToLValue(Expr* E, LValue LV) {
346 // FIXME: Are initializers affected by volatile?
347 if (E->getType()->isComplexType()) {
348 CGF.EmitComplexExprIntoAddr(E, LV.getAddress(), false);
Eli Friedmanc8ba9612008-05-12 15:06:05 +0000349 } else if (CGF.hasAggregateLLVMType(E->getType())) {
350 CGF.EmitAnyExpr(E, LV.getAddress(), false);
351 } else {
352 CGF.EmitStoreThroughLValue(CGF.EmitAnyExpr(E), LV, E->getType());
Chris Lattnerf81557c2008-04-04 18:42:16 +0000353 }
Chris Lattnerf81557c2008-04-04 18:42:16 +0000354}
355
356void AggExprEmitter::EmitNullInitializationToLValue(LValue LV, QualType T) {
357 if (!CGF.hasAggregateLLVMType(T)) {
358 // For non-aggregates, we can store zero
Daniel Dunbar82397132008-08-06 05:32:55 +0000359 llvm::Value *Null = llvm::Constant::getNullValue(CGF.ConvertType(T));
360 CGF.EmitStoreThroughLValue(RValue::get(Null), LV, T);
Lauro Ramos Venancio145cd892008-02-19 19:27:31 +0000361 } else {
Chris Lattnerf81557c2008-04-04 18:42:16 +0000362 // Otherwise, just memset the whole thing to zero. This is legal
363 // because in LLVM, all default initializers are guaranteed to have a
364 // bit pattern of all zeros.
365 // 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.
Chris Lattner4e8a9e82008-11-21 16:43:15 +0000368 const llvm::Type *SizeTy = llvm::Type::Int64Ty;
369 llvm::Value *MemSet = CGF.CGM.getIntrinsic(llvm::Intrinsic::memset,
370 &SizeTy, 1);
Chris Lattnerf81557c2008-04-04 18:42:16 +0000371 uint64_t Size = CGF.getContext().getTypeSize(T);
372
373 const llvm::Type *BP = llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
374 llvm::Value* DestPtr = Builder.CreateBitCast(LV.getAddress(), BP, "tmp");
Chris Lattner3eae03e2008-05-06 00:56:42 +0000375 Builder.CreateCall4(MemSet, DestPtr,
376 llvm::ConstantInt::get(llvm::Type::Int8Ty, 0),
Chris Lattner4e8a9e82008-11-21 16:43:15 +0000377 llvm::ConstantInt::get(SizeTy, Size/8),
Chris Lattner3eae03e2008-05-06 00:56:42 +0000378 llvm::ConstantInt::get(llvm::Type::Int32Ty, 0));
Chris Lattnerf81557c2008-04-04 18:42:16 +0000379 }
380}
381
Chris Lattnerf81557c2008-04-04 18:42:16 +0000382void AggExprEmitter::VisitInitListExpr(InitListExpr *E) {
Chris Lattnerbe20bb52008-10-26 23:53:12 +0000383 if (E->hadDesignators()) {
384 CGF.ErrorUnsupported(E, "initializer list with designators");
385 return;
386 }
Eli Friedman994ffef2008-11-30 02:11:09 +0000387
Eli Friedmana385b3c2008-12-02 01:17:45 +0000388#if 0
389 // FIXME: Disabled while we figure out what to do about
390 // test/CodeGen/bitfield.c
391 //
Eli Friedman994ffef2008-11-30 02:11:09 +0000392 // If we can, prefer a copy from a global; this is a lot less
393 // code for long globals, and it's easier for the current optimizers
394 // to analyze.
395 // FIXME: Should we really be doing this? Should we try to avoid
396 // cases where we emit a global with a lot of zeros? Should
397 // we try to avoid short globals?
Eli Friedmanc9e8f602009-01-25 02:32:41 +0000398 if (E->isConstantInitializer(CGF.getContext(), 0)) {
Eli Friedman994ffef2008-11-30 02:11:09 +0000399 llvm::Constant* C = CGF.CGM.EmitConstantExpr(E, &CGF);
400 llvm::GlobalVariable* GV =
401 new llvm::GlobalVariable(C->getType(), true,
402 llvm::GlobalValue::InternalLinkage,
403 C, "", &CGF.CGM.getModule(), 0);
404 CGF.EmitAggregateCopy(DestPtr, GV, E->getType());
405 return;
406 }
Eli Friedmana385b3c2008-12-02 01:17:45 +0000407#endif
Chris Lattnerf81557c2008-04-04 18:42:16 +0000408 // Handle initialization of an array.
409 if (E->getType()->isArrayType()) {
410 const llvm::PointerType *APType =
411 cast<llvm::PointerType>(DestPtr->getType());
412 const llvm::ArrayType *AType =
413 cast<llvm::ArrayType>(APType->getElementType());
414
415 uint64_t NumInitElements = E->getNumInits();
Eli Friedman922696f2008-05-19 17:51:16 +0000416
Chris Lattner96196622008-07-26 22:37:01 +0000417 if (E->getNumInits() > 0) {
418 QualType T1 = E->getType();
419 QualType T2 = E->getInit(0)->getType();
420 if (CGF.getContext().getCanonicalType(T1).getUnqualifiedType() ==
421 CGF.getContext().getCanonicalType(T2).getUnqualifiedType()) {
422 EmitAggLoadOfLValue(E->getInit(0));
423 return;
424 }
Eli Friedman922696f2008-05-19 17:51:16 +0000425 }
426
Chris Lattnerf81557c2008-04-04 18:42:16 +0000427 uint64_t NumArrayElements = AType->getNumElements();
Chris Lattnerc63a1f22008-08-04 07:31:14 +0000428 QualType ElementType = CGF.getContext().getCanonicalType(E->getType());
429 ElementType =CGF.getContext().getAsArrayType(ElementType)->getElementType();
Chris Lattnerf81557c2008-04-04 18:42:16 +0000430
Chris Lattnerc63a1f22008-08-04 07:31:14 +0000431 unsigned CVRqualifier = ElementType.getCVRQualifiers();
Eli Friedman1e692ac2008-06-13 23:01:12 +0000432
Chris Lattnerf81557c2008-04-04 18:42:16 +0000433 for (uint64_t i = 0; i != NumArrayElements; ++i) {
434 llvm::Value *NextVal = Builder.CreateStructGEP(DestPtr, i, ".array");
435 if (i < NumInitElements)
Eli Friedman1e692ac2008-06-13 23:01:12 +0000436 EmitInitializationToLValue(E->getInit(i),
437 LValue::MakeAddr(NextVal, CVRqualifier));
Chris Lattnerf81557c2008-04-04 18:42:16 +0000438 else
Eli Friedman1e692ac2008-06-13 23:01:12 +0000439 EmitNullInitializationToLValue(LValue::MakeAddr(NextVal, CVRqualifier),
Chris Lattnerf81557c2008-04-04 18:42:16 +0000440 ElementType);
Lauro Ramos Venancio145cd892008-02-19 19:27:31 +0000441 }
Chris Lattnerf81557c2008-04-04 18:42:16 +0000442 return;
443 }
444
445 assert(E->getType()->isRecordType() && "Only support structs/unions here!");
446
447 // Do struct initialization; this code just sets each individual member
448 // to the approprate value. This makes bitfield support automatic;
449 // the disadvantage is that the generated code is more difficult for
450 // the optimizer, especially with bitfields.
451 unsigned NumInitElements = E->getNumInits();
452 RecordDecl *SD = E->getType()->getAsRecordType()->getDecl();
Chris Lattnerf81557c2008-04-04 18:42:16 +0000453 unsigned CurInitVal = 0;
454 bool isUnion = E->getType()->isUnionType();
455
456 // Here we iterate over the fields; this makes it simpler to both
457 // default-initialize fields and skip over unnamed fields.
Douglas Gregor44b43212008-12-11 16:49:14 +0000458 for (RecordDecl::field_iterator Field = SD->field_begin(),
459 FieldEnd = SD->field_end();
460 Field != FieldEnd; ++Field) {
461 // We're done once we hit the flexible array member
462 if (Field->getType()->isIncompleteArrayType())
463 break;
464
465 if (Field->getIdentifier() == 0) {
Chris Lattnerf81557c2008-04-04 18:42:16 +0000466 // Initializers can't initialize unnamed fields, e.g. "int : 20;"
467 continue;
468 }
Eli Friedman1e692ac2008-06-13 23:01:12 +0000469 // FIXME: volatility
Douglas Gregor44b43212008-12-11 16:49:14 +0000470 LValue FieldLoc = CGF.EmitLValueForField(DestPtr, *Field, isUnion,0);
Chris Lattnerf81557c2008-04-04 18:42:16 +0000471 if (CurInitVal < NumInitElements) {
472 // Store the initializer into the field
473 // This will probably have to get a bit smarter when we support
474 // designators in initializers
475 EmitInitializationToLValue(E->getInit(CurInitVal++), FieldLoc);
476 } else {
477 // We're out of initalizers; default-initialize to null
Douglas Gregor44b43212008-12-11 16:49:14 +0000478 EmitNullInitializationToLValue(FieldLoc, Field->getType());
Chris Lattnerf81557c2008-04-04 18:42:16 +0000479 }
480
481 // Unions only initialize one field.
482 // (things can get weird with designators, but they aren't
483 // supported yet.)
Nuno Lopes7e916272009-01-15 20:14:33 +0000484 if (isUnion)
Chris Lattnerf81557c2008-04-04 18:42:16 +0000485 break;
Lauro Ramos Venancio145cd892008-02-19 19:27:31 +0000486 }
Devang Patel636c3d02007-10-26 17:44:44 +0000487}
488
Chris Lattneree755f92007-08-21 04:59:27 +0000489//===----------------------------------------------------------------------===//
490// Entry Points into this File
491//===----------------------------------------------------------------------===//
492
493/// EmitAggExpr - Emit the computation of the specified expression of
494/// aggregate type. The result is computed into DestPtr. Note that if
495/// DestPtr is null, the value of the aggregate expression is not needed.
496void CodeGenFunction::EmitAggExpr(const Expr *E, llvm::Value *DestPtr,
497 bool VolatileDest) {
498 assert(E && hasAggregateLLVMType(E->getType()) &&
499 "Invalid aggregate expression to emit");
500
501 AggExprEmitter(*this, DestPtr, VolatileDest).Visit(const_cast<Expr*>(E));
502}
Daniel Dunbar7482d122008-09-09 20:49:46 +0000503
504void CodeGenFunction::EmitAggregateClear(llvm::Value *DestPtr, QualType Ty) {
505 assert(!Ty->isAnyComplexType() && "Shouldn't happen for complex");
506
507 EmitMemSetToZero(DestPtr, Ty);
508}
509
510void CodeGenFunction::EmitAggregateCopy(llvm::Value *DestPtr,
511 llvm::Value *SrcPtr, QualType Ty) {
512 assert(!Ty->isAnyComplexType() && "Shouldn't happen for complex");
513
514 // Aggregate assignment turns into llvm.memmove.
515 const llvm::Type *BP = llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
516 if (DestPtr->getType() != BP)
517 DestPtr = Builder.CreateBitCast(DestPtr, BP, "tmp");
518 if (SrcPtr->getType() != BP)
519 SrcPtr = Builder.CreateBitCast(SrcPtr, BP, "tmp");
520
521 // Get size and alignment info for this aggregate.
522 std::pair<uint64_t, unsigned> TypeInfo = getContext().getTypeInfo(Ty);
523
524 // FIXME: Handle variable sized types.
525 const llvm::Type *IntPtr = llvm::IntegerType::get(LLVMPointerWidth);
526
527 Builder.CreateCall4(CGM.getMemMoveFn(),
528 DestPtr, SrcPtr,
529 // TypeInfo.first describes size in bits.
530 llvm::ConstantInt::get(IntPtr, TypeInfo.first/8),
531 llvm::ConstantInt::get(llvm::Type::Int32Ty,
532 TypeInfo.second/8));
533}