blob: acca396c0b077390fbd20e2ea4c2e753bf6e8a54 [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"
17#include "clang/AST/StmtVisitor.h"
Chris Lattnerbdb8ffb2007-08-11 00:04:45 +000018#include "llvm/Constants.h"
19#include "llvm/Function.h"
Devang Patelddde6d52007-10-26 17:44:44 +000020#include "llvm/GlobalVariable.h"
Chris Lattnerb50e3902007-08-21 04:25:47 +000021#include "llvm/Support/Compiler.h"
Chris Lattner6ee6ab02008-04-04 18:42:16 +000022#include "llvm/Intrinsics.h"
Chris Lattner78ed8402007-08-10 20:13:28 +000023using namespace clang;
24using namespace CodeGen;
Chris Lattnerbdb8ffb2007-08-11 00:04:45 +000025
Chris Lattnerb50e3902007-08-21 04:25:47 +000026//===----------------------------------------------------------------------===//
27// Aggregate Expression Emitter
28//===----------------------------------------------------------------------===//
29
30namespace {
31class VISIBILITY_HIDDEN AggExprEmitter : public StmtVisitor<AggExprEmitter> {
32 CodeGenFunction &CGF;
Daniel Dunbard916e6e2008-11-01 01:53:16 +000033 CGBuilderTy &Builder;
Chris Lattnerb50e3902007-08-21 04:25:47 +000034 llvm::Value *DestPtr;
35 bool VolatileDest;
36public:
37 AggExprEmitter(CodeGenFunction &cgf, llvm::Value *destPtr, bool volatileDest)
Chris Lattner41b1fca2007-08-26 23:13:56 +000038 : CGF(cgf), Builder(CGF.Builder),
39 DestPtr(destPtr), VolatileDest(volatileDest) {
Chris Lattnerb50e3902007-08-21 04:25:47 +000040 }
41
Chris Lattnera7300102007-08-21 04:59:27 +000042 //===--------------------------------------------------------------------===//
43 // Utilities
44 //===--------------------------------------------------------------------===//
45
Chris Lattnerb50e3902007-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);
Eli Friedman48ec5622008-05-19 17:51:16 +000050
Chris Lattnera7300102007-08-21 04:59:27 +000051 //===--------------------------------------------------------------------===//
52 // Visitor Methods
53 //===--------------------------------------------------------------------===//
54
Chris Lattnerb50e3902007-08-21 04:25:47 +000055 void VisitStmt(Stmt *S) {
Daniel Dunbar9503b782008-08-16 00:56:44 +000056 CGF.ErrorUnsupported(S, "aggregate expression");
Chris Lattnerb50e3902007-08-21 04:25:47 +000057 }
58 void VisitParenExpr(ParenExpr *PE) { Visit(PE->getSubExpr()); }
Eli Friedmand031ba52009-01-27 09:03:41 +000059 void VisitUnaryExtension(UnaryOperator *E) { Visit(E->getSubExpr()); }
Chris Lattnerb50e3902007-08-21 04:25:47 +000060
61 // l-values.
Seo Sanghyeon32666c52007-12-14 02:04:12 +000062 void VisitDeclRefExpr(DeclRefExpr *DRE) { EmitAggLoadOfLValue(DRE); }
63 void VisitMemberExpr(MemberExpr *ME) { EmitAggLoadOfLValue(ME); }
64 void VisitUnaryDeref(UnaryOperator *E) { EmitAggLoadOfLValue(E); }
Seo Sanghyeon3c2d5fb2007-12-23 03:11:58 +000065 void VisitStringLiteral(StringLiteral *E) { EmitAggLoadOfLValue(E); }
Eli Friedman42af3972008-05-27 15:51:49 +000066 void VisitCompoundLiteralExpr(CompoundLiteralExpr *E)
67 { EmitAggLoadOfLValue(E); }
Seo Sanghyeon32666c52007-12-14 02:04:12 +000068
69 void VisitArraySubscriptExpr(ArraySubscriptExpr *E) {
70 EmitAggLoadOfLValue(E);
71 }
Douglas Gregorf603b472009-01-28 21:54:33 +000072
Mike Stump68c3a8c2009-03-18 15:54:29 +000073 void VisitBlockDeclRefExpr(const BlockDeclRefExpr *E)
74 { EmitAggLoadOfLValue(E); }
75
Chris Lattnerb50e3902007-08-21 04:25:47 +000076 // Operators.
77 // case Expr::UnaryOperatorClass:
Chris Lattnerb50e3902007-08-21 04:25:47 +000078 // case Expr::CastExprClass:
Nuno Lopesdff17912009-01-15 20:14:33 +000079 void VisitCStyleCastExpr(CStyleCastExpr *E);
Anders Carlssonf2712ac2008-01-14 06:28:57 +000080 void VisitImplicitCastExpr(ImplicitCastExpr *E);
Anders Carlsson0ae15412007-10-31 22:04:46 +000081 void VisitCallExpr(const CallExpr *E);
Chris Lattnerbab0bfb2007-08-31 22:54:14 +000082 void VisitStmtExpr(const StmtExpr *E);
Chris Lattnerb50e3902007-08-21 04:25:47 +000083 void VisitBinaryOperator(const BinaryOperator *BO);
Chris Lattner76cb1892007-08-21 04:43:17 +000084 void VisitBinAssign(const BinaryOperator *E);
Eli Friedman91fc7802008-05-20 07:56:31 +000085 void VisitBinComma(const BinaryOperator *E);
Chris Lattnerb50e3902007-08-21 04:25:47 +000086
Chris Lattner6ee20e32008-06-24 17:04:18 +000087 void VisitObjCMessageExpr(ObjCMessageExpr *E);
Daniel Dunbar5e105892008-08-23 10:51:21 +000088 void VisitObjCIvarRefExpr(ObjCIvarRefExpr *E) {
89 EmitAggLoadOfLValue(E);
90 }
Daniel Dunbar91cc4022008-08-27 06:57:25 +000091 void VisitObjCPropertyRefExpr(ObjCPropertyRefExpr *E);
Fariborz Jahanianf18d4c82008-11-22 18:39:36 +000092 void VisitObjCKVCRefExpr(ObjCKVCRefExpr *E);
Chris Lattnerb50e3902007-08-21 04:25:47 +000093
94 void VisitConditionalOperator(const ConditionalOperator *CO);
Devang Patelddde6d52007-10-26 17:44:44 +000095 void VisitInitListExpr(InitListExpr *E);
Chris Lattner3e254fb2008-04-08 04:40:51 +000096 void VisitCXXDefaultArgExpr(CXXDefaultArgExpr *DAE) {
97 Visit(DAE->getExpr());
98 }
Eli Friedman42af3972008-05-27 15:51:49 +000099 void VisitVAArgExpr(VAArgExpr *E);
Chris Lattner6ee6ab02008-04-04 18:42:16 +0000100
101 void EmitInitializationToLValue(Expr *E, LValue Address);
102 void EmitNullInitializationToLValue(LValue Address, QualType T);
Chris Lattnerb50e3902007-08-21 04:25:47 +0000103 // case Expr::ChooseExprClass:
Lauro Ramos Venancio9eb37a22008-02-18 22:44:02 +0000104
Chris Lattnerb50e3902007-08-21 04:25:47 +0000105};
106} // end anonymous namespace.
107
Chris Lattnera7300102007-08-21 04:59:27 +0000108//===----------------------------------------------------------------------===//
109// Utilities
110//===----------------------------------------------------------------------===//
Chris Lattnerb50e3902007-08-21 04:25:47 +0000111
Chris Lattnerbdb8ffb2007-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 Lattnerb50e3902007-08-21 04:25:47 +0000115void AggExprEmitter::EmitAggLoadOfLValue(const Expr *E) {
116 LValue LV = CGF.EmitLValue(E);
Chris Lattnerbdb8ffb2007-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 Dunbarbde0c9d2008-09-09 20:49:46 +0000125 CGF.EmitAggregateCopy(DestPtr, SrcPtr, E->getType());
Chris Lattnerbdb8ffb2007-08-11 00:04:45 +0000126}
127
Chris Lattnera7300102007-08-21 04:59:27 +0000128//===----------------------------------------------------------------------===//
129// Visitor Methods
130//===----------------------------------------------------------------------===//
131
Nuno Lopesdff17912009-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();
Douglas Gregorc55b0b02009-04-09 21:40:53 +0000136 LValue FieldLoc = CGF.EmitLValueForField(DestPtr,
137 *SD->field_begin(CGF.getContext()),
138 true, 0);
Nuno Lopesdff17912009-01-15 20:14:33 +0000139 EmitInitializationToLValue(E->getSubExpr(), FieldLoc);
140 return;
141 }
142
143 Visit(E->getSubExpr());
144}
145
Chris Lattnerc154ac12008-07-26 22:37:01 +0000146void AggExprEmitter::VisitImplicitCastExpr(ImplicitCastExpr *E) {
Eli Friedmanf0217122008-02-11 01:09:17 +0000147 assert(CGF.getContext().typesAreCompatible(
Chris Lattnerc154ac12008-07-26 22:37:01 +0000148 E->getSubExpr()->getType().getUnqualifiedType(),
149 E->getType().getUnqualifiedType()) &&
150 "Implicit cast types must be compatible");
Anders Carlssonf2712ac2008-01-14 06:28:57 +0000151 Visit(E->getSubExpr());
152}
153
Chris Lattnerc154ac12008-07-26 22:37:01 +0000154void AggExprEmitter::VisitCallExpr(const CallExpr *E) {
Anders Carlsson0ae15412007-10-31 22:04:46 +0000155 RValue RV = CGF.EmitCallExpr(E);
156 assert(RV.isAggregate() && "Return value must be aggregate value!");
157
158 // If the result is ignored, don't copy from the value.
159 if (DestPtr == 0)
160 // FIXME: If the source is volatile, we must read from it.
161 return;
162
Daniel Dunbarbde0c9d2008-09-09 20:49:46 +0000163 CGF.EmitAggregateCopy(DestPtr, RV.getAggregateAddr(), E->getType());
Anders Carlsson0ae15412007-10-31 22:04:46 +0000164}
Chris Lattnerc154ac12008-07-26 22:37:01 +0000165
166void AggExprEmitter::VisitObjCMessageExpr(ObjCMessageExpr *E) {
Daniel Dunbara04840b2008-08-23 03:46:30 +0000167 RValue RV = CGF.EmitObjCMessageExpr(E);
168 assert(RV.isAggregate() && "Return value must be aggregate value!");
Chris Lattner6ee20e32008-06-24 17:04:18 +0000169
170 // If the result is ignored, don't copy from the value.
171 if (DestPtr == 0)
Daniel Dunbara04840b2008-08-23 03:46:30 +0000172 // FIXME: If the source is volatile, we must read from it.
Chris Lattner6ee20e32008-06-24 17:04:18 +0000173 return;
174
Daniel Dunbarbde0c9d2008-09-09 20:49:46 +0000175 CGF.EmitAggregateCopy(DestPtr, RV.getAggregateAddr(), E->getType());
Chris Lattner6ee20e32008-06-24 17:04:18 +0000176}
Anders Carlsson0ae15412007-10-31 22:04:46 +0000177
Daniel Dunbar91cc4022008-08-27 06:57:25 +0000178void AggExprEmitter::VisitObjCPropertyRefExpr(ObjCPropertyRefExpr *E) {
179 RValue RV = CGF.EmitObjCPropertyGet(E);
180 assert(RV.isAggregate() && "Return value must be aggregate value!");
181
182 // If the result is ignored, don't copy from the value.
183 if (DestPtr == 0)
184 // FIXME: If the source is volatile, we must read from it.
185 return;
186
Daniel Dunbarbde0c9d2008-09-09 20:49:46 +0000187 CGF.EmitAggregateCopy(DestPtr, RV.getAggregateAddr(), E->getType());
Daniel Dunbar91cc4022008-08-27 06:57:25 +0000188}
189
Fariborz Jahanianf18d4c82008-11-22 18:39:36 +0000190void AggExprEmitter::VisitObjCKVCRefExpr(ObjCKVCRefExpr *E) {
191 RValue RV = CGF.EmitObjCPropertyGet(E);
192 assert(RV.isAggregate() && "Return value must be aggregate value!");
193
194 // If the result is ignored, don't copy from the value.
195 if (DestPtr == 0)
196 // FIXME: If the source is volatile, we must read from it.
197 return;
198
199 CGF.EmitAggregateCopy(DestPtr, RV.getAggregateAddr(), E->getType());
200}
201
Chris Lattnerc154ac12008-07-26 22:37:01 +0000202void AggExprEmitter::VisitBinComma(const BinaryOperator *E) {
Eli Friedman91fc7802008-05-20 07:56:31 +0000203 CGF.EmitAnyExpr(E->getLHS());
204 CGF.EmitAggExpr(E->getRHS(), DestPtr, false);
205}
206
Chris Lattnerbab0bfb2007-08-31 22:54:14 +0000207void AggExprEmitter::VisitStmtExpr(const StmtExpr *E) {
208 CGF.EmitCompoundStmt(*E->getSubStmt(), true, DestPtr, VolatileDest);
209}
210
Chris Lattnerb50e3902007-08-21 04:25:47 +0000211void AggExprEmitter::VisitBinaryOperator(const BinaryOperator *E) {
Daniel Dunbar9503b782008-08-16 00:56:44 +0000212 CGF.ErrorUnsupported(E, "aggregate binary expression");
Chris Lattnera7300102007-08-21 04:59:27 +0000213}
214
Chris Lattner76cb1892007-08-21 04:43:17 +0000215void AggExprEmitter::VisitBinAssign(const BinaryOperator *E) {
Eli Friedmanf0217122008-02-11 01:09:17 +0000216 // For an assignment to work, the value on the right has
217 // to be compatible with the value on the left.
218 assert(CGF.getContext().typesAreCompatible(
219 E->getLHS()->getType().getUnqualifiedType(),
220 E->getRHS()->getType().getUnqualifiedType())
221 && "Invalid assignment");
Chris Lattnerb50e3902007-08-21 04:25:47 +0000222 LValue LHS = CGF.EmitLValue(E->getLHS());
Chris Lattnerbdb8ffb2007-08-11 00:04:45 +0000223
Daniel Dunbardd851282008-08-30 05:35:15 +0000224 // We have to special case property setters, otherwise we must have
225 // a simple lvalue (no aggregates inside vectors, bitfields).
226 if (LHS.isPropertyRef()) {
227 // FIXME: Volatility?
228 llvm::Value *AggLoc = DestPtr;
229 if (!AggLoc)
230 AggLoc = CGF.CreateTempAlloca(CGF.ConvertType(E->getRHS()->getType()));
231 CGF.EmitAggExpr(E->getRHS(), AggLoc, false);
232 CGF.EmitObjCPropertySet(LHS.getPropertyRefExpr(),
233 RValue::getAggregate(AggLoc));
Fariborz Jahanianb0973da2008-11-22 22:30:21 +0000234 }
235 else if (LHS.isKVCRef()) {
236 // FIXME: Volatility?
237 llvm::Value *AggLoc = DestPtr;
238 if (!AggLoc)
239 AggLoc = CGF.CreateTempAlloca(CGF.ConvertType(E->getRHS()->getType()));
240 CGF.EmitAggExpr(E->getRHS(), AggLoc, false);
241 CGF.EmitObjCPropertySet(LHS.getKVCRefExpr(),
242 RValue::getAggregate(AggLoc));
Daniel Dunbardd851282008-08-30 05:35:15 +0000243 } else {
244 // Codegen the RHS so that it stores directly into the LHS.
245 CGF.EmitAggExpr(E->getRHS(), LHS.getAddress(), false /*FIXME: VOLATILE LHS*/);
246
247 if (DestPtr == 0)
248 return;
249
250 // If the result of the assignment is used, copy the RHS there also.
Daniel Dunbarbde0c9d2008-09-09 20:49:46 +0000251 CGF.EmitAggregateCopy(DestPtr, LHS.getAddress(), E->getType());
Daniel Dunbardd851282008-08-30 05:35:15 +0000252 }
Chris Lattnerbdb8ffb2007-08-11 00:04:45 +0000253}
254
Chris Lattnerb50e3902007-08-21 04:25:47 +0000255void AggExprEmitter::VisitConditionalOperator(const ConditionalOperator *E) {
Daniel Dunbar6e3a10c2008-11-13 01:38:36 +0000256 llvm::BasicBlock *LHSBlock = CGF.createBasicBlock("cond.true");
257 llvm::BasicBlock *RHSBlock = CGF.createBasicBlock("cond.false");
258 llvm::BasicBlock *ContBlock = CGF.createBasicBlock("cond.end");
Chris Lattnerbdb8ffb2007-08-11 00:04:45 +0000259
Chris Lattnerb50e3902007-08-21 04:25:47 +0000260 llvm::Value *Cond = CGF.EvaluateExprAsBool(E->getCond());
Chris Lattner41b1fca2007-08-26 23:13:56 +0000261 Builder.CreateCondBr(Cond, LHSBlock, RHSBlock);
Chris Lattnerbdb8ffb2007-08-11 00:04:45 +0000262
Chris Lattnerb50e3902007-08-21 04:25:47 +0000263 CGF.EmitBlock(LHSBlock);
Chris Lattnerbdb8ffb2007-08-11 00:04:45 +0000264
265 // Handle the GNU extension for missing LHS.
266 assert(E->getLHS() && "Must have LHS for aggregate value");
267
Chris Lattner55165ba2007-08-21 05:02:10 +0000268 Visit(E->getLHS());
Daniel Dunbar5276caa2008-11-11 09:41:28 +0000269 CGF.EmitBranch(ContBlock);
Chris Lattnerbdb8ffb2007-08-11 00:04:45 +0000270
Chris Lattnerb50e3902007-08-21 04:25:47 +0000271 CGF.EmitBlock(RHSBlock);
Chris Lattnerbdb8ffb2007-08-11 00:04:45 +0000272
Chris Lattner55165ba2007-08-21 05:02:10 +0000273 Visit(E->getRHS());
Daniel Dunbar5276caa2008-11-11 09:41:28 +0000274 CGF.EmitBranch(ContBlock);
Chris Lattnerbdb8ffb2007-08-11 00:04:45 +0000275
Chris Lattnerb50e3902007-08-21 04:25:47 +0000276 CGF.EmitBlock(ContBlock);
Chris Lattnerbdb8ffb2007-08-11 00:04:45 +0000277}
Chris Lattnera7300102007-08-21 04:59:27 +0000278
Eli Friedman42af3972008-05-27 15:51:49 +0000279void AggExprEmitter::VisitVAArgExpr(VAArgExpr *VE) {
Daniel Dunbar63378cf2009-02-11 22:25:55 +0000280 llvm::Value *ArgValue = CGF.EmitVAListRef(VE->getSubExpr());
Anders Carlsson285611e2008-11-04 05:30:00 +0000281 llvm::Value *ArgPtr = CGF.EmitVAArg(ArgValue, VE->getType());
282
Sebastian Redl79f36c92009-01-09 21:09:38 +0000283 if (!ArgPtr) {
Anders Carlsson285611e2008-11-04 05:30:00 +0000284 CGF.ErrorUnsupported(VE, "aggregate va_arg expression");
Sebastian Redl79f36c92009-01-09 21:09:38 +0000285 return;
286 }
287
Eli Friedman42af3972008-05-27 15:51:49 +0000288 if (DestPtr)
Eli Friedman2e630542008-06-13 23:01:12 +0000289 // FIXME: volatility
Anders Carlsson285611e2008-11-04 05:30:00 +0000290 CGF.EmitAggregateCopy(DestPtr, ArgPtr, VE->getType());
Eli Friedman42af3972008-05-27 15:51:49 +0000291}
292
Chris Lattner6ee6ab02008-04-04 18:42:16 +0000293void AggExprEmitter::EmitInitializationToLValue(Expr* E, LValue LV) {
294 // FIXME: Are initializers affected by volatile?
Douglas Gregorc9e012a2009-01-29 17:44:32 +0000295 if (isa<ImplicitValueInitExpr>(E)) {
Douglas Gregorf603b472009-01-28 21:54:33 +0000296 EmitNullInitializationToLValue(LV, E->getType());
Douglas Gregorc9e012a2009-01-29 17:44:32 +0000297 } else if (E->getType()->isComplexType()) {
298 CGF.EmitComplexExprIntoAddr(E, LV.getAddress(), false);
Eli Friedmanbb822fb2008-05-12 15:06:05 +0000299 } else if (CGF.hasAggregateLLVMType(E->getType())) {
300 CGF.EmitAnyExpr(E, LV.getAddress(), false);
301 } else {
302 CGF.EmitStoreThroughLValue(CGF.EmitAnyExpr(E), LV, E->getType());
Chris Lattner6ee6ab02008-04-04 18:42:16 +0000303 }
Chris Lattner6ee6ab02008-04-04 18:42:16 +0000304}
305
306void AggExprEmitter::EmitNullInitializationToLValue(LValue LV, QualType T) {
307 if (!CGF.hasAggregateLLVMType(T)) {
308 // For non-aggregates, we can store zero
Daniel Dunbarde813312008-08-06 05:32:55 +0000309 llvm::Value *Null = llvm::Constant::getNullValue(CGF.ConvertType(T));
310 CGF.EmitStoreThroughLValue(RValue::get(Null), LV, T);
Lauro Ramos Venancioa62b1df2008-02-19 19:27:31 +0000311 } else {
Chris Lattner6ee6ab02008-04-04 18:42:16 +0000312 // Otherwise, just memset the whole thing to zero. This is legal
313 // because in LLVM, all default initializers are guaranteed to have a
314 // bit pattern of all zeros.
Eli Friedmanb836cd32009-04-13 21:47:26 +0000315 // FIXME: That isn't true for member pointers!
Chris Lattner6ee6ab02008-04-04 18:42:16 +0000316 // There's a potential optimization opportunity in combining
317 // memsets; that would be easy for arrays, but relatively
318 // difficult for structures with the current code.
Eli Friedman2494bc32009-03-28 03:10:45 +0000319 CGF.EmitMemSetToZero(LV.getAddress(), T);
Chris Lattner6ee6ab02008-04-04 18:42:16 +0000320 }
321}
322
Chris Lattner6ee6ab02008-04-04 18:42:16 +0000323void AggExprEmitter::VisitInitListExpr(InitListExpr *E) {
Eli Friedman2a4f27c2008-12-02 01:17:45 +0000324#if 0
325 // FIXME: Disabled while we figure out what to do about
326 // test/CodeGen/bitfield.c
327 //
Eli Friedmane9b227d2008-11-30 02:11:09 +0000328 // If we can, prefer a copy from a global; this is a lot less
329 // code for long globals, and it's easier for the current optimizers
330 // to analyze.
331 // FIXME: Should we really be doing this? Should we try to avoid
332 // cases where we emit a global with a lot of zeros? Should
333 // we try to avoid short globals?
Eli Friedmandee41122009-01-25 02:32:41 +0000334 if (E->isConstantInitializer(CGF.getContext(), 0)) {
Eli Friedmane9b227d2008-11-30 02:11:09 +0000335 llvm::Constant* C = CGF.CGM.EmitConstantExpr(E, &CGF);
336 llvm::GlobalVariable* GV =
337 new llvm::GlobalVariable(C->getType(), true,
338 llvm::GlobalValue::InternalLinkage,
339 C, "", &CGF.CGM.getModule(), 0);
340 CGF.EmitAggregateCopy(DestPtr, GV, E->getType());
341 return;
342 }
Eli Friedman2a4f27c2008-12-02 01:17:45 +0000343#endif
Douglas Gregor9fddded2009-01-29 19:42:23 +0000344 if (E->hadArrayRangeDesignator()) {
345 CGF.ErrorUnsupported(E, "GNU array range designator extension");
346 }
347
Chris Lattner6ee6ab02008-04-04 18:42:16 +0000348 // Handle initialization of an array.
349 if (E->getType()->isArrayType()) {
350 const llvm::PointerType *APType =
351 cast<llvm::PointerType>(DestPtr->getType());
352 const llvm::ArrayType *AType =
353 cast<llvm::ArrayType>(APType->getElementType());
354
355 uint64_t NumInitElements = E->getNumInits();
Eli Friedman48ec5622008-05-19 17:51:16 +0000356
Chris Lattnerc154ac12008-07-26 22:37:01 +0000357 if (E->getNumInits() > 0) {
358 QualType T1 = E->getType();
359 QualType T2 = E->getInit(0)->getType();
360 if (CGF.getContext().getCanonicalType(T1).getUnqualifiedType() ==
361 CGF.getContext().getCanonicalType(T2).getUnqualifiedType()) {
362 EmitAggLoadOfLValue(E->getInit(0));
363 return;
364 }
Eli Friedman48ec5622008-05-19 17:51:16 +0000365 }
366
Chris Lattner6ee6ab02008-04-04 18:42:16 +0000367 uint64_t NumArrayElements = AType->getNumElements();
Chris Lattnera1923f62008-08-04 07:31:14 +0000368 QualType ElementType = CGF.getContext().getCanonicalType(E->getType());
Douglas Gregorf603b472009-01-28 21:54:33 +0000369 ElementType = CGF.getContext().getAsArrayType(ElementType)->getElementType();
Chris Lattner6ee6ab02008-04-04 18:42:16 +0000370
Chris Lattnera1923f62008-08-04 07:31:14 +0000371 unsigned CVRqualifier = ElementType.getCVRQualifiers();
Eli Friedman2e630542008-06-13 23:01:12 +0000372
Chris Lattner6ee6ab02008-04-04 18:42:16 +0000373 for (uint64_t i = 0; i != NumArrayElements; ++i) {
374 llvm::Value *NextVal = Builder.CreateStructGEP(DestPtr, i, ".array");
375 if (i < NumInitElements)
Eli Friedman2e630542008-06-13 23:01:12 +0000376 EmitInitializationToLValue(E->getInit(i),
377 LValue::MakeAddr(NextVal, CVRqualifier));
Chris Lattner6ee6ab02008-04-04 18:42:16 +0000378 else
Eli Friedman2e630542008-06-13 23:01:12 +0000379 EmitNullInitializationToLValue(LValue::MakeAddr(NextVal, CVRqualifier),
Chris Lattner6ee6ab02008-04-04 18:42:16 +0000380 ElementType);
Lauro Ramos Venancioa62b1df2008-02-19 19:27:31 +0000381 }
Chris Lattner6ee6ab02008-04-04 18:42:16 +0000382 return;
383 }
384
385 assert(E->getType()->isRecordType() && "Only support structs/unions here!");
386
387 // Do struct initialization; this code just sets each individual member
388 // to the approprate value. This makes bitfield support automatic;
389 // the disadvantage is that the generated code is more difficult for
390 // the optimizer, especially with bitfields.
391 unsigned NumInitElements = E->getNumInits();
392 RecordDecl *SD = E->getType()->getAsRecordType()->getDecl();
Chris Lattner6ee6ab02008-04-04 18:42:16 +0000393 unsigned CurInitVal = 0;
Douglas Gregor82462762009-01-29 16:53:55 +0000394
395 if (E->getType()->isUnionType()) {
396 // Only initialize one field of a union. The field itself is
397 // specified by the initializer list.
398 if (!E->getInitializedFieldInUnion()) {
399 // Empty union; we have nothing to do.
400
401#ifndef NDEBUG
402 // Make sure that it's really an empty and not a failure of
403 // semantic analysis.
Douglas Gregorc55b0b02009-04-09 21:40:53 +0000404 for (RecordDecl::field_iterator Field = SD->field_begin(CGF.getContext()),
405 FieldEnd = SD->field_end(CGF.getContext());
Douglas Gregor82462762009-01-29 16:53:55 +0000406 Field != FieldEnd; ++Field)
407 assert(Field->isUnnamedBitfield() && "Only unnamed bitfields allowed");
408#endif
409 return;
410 }
411
412 // FIXME: volatility
413 FieldDecl *Field = E->getInitializedFieldInUnion();
414 LValue FieldLoc = CGF.EmitLValueForField(DestPtr, Field, true, 0);
415
416 if (NumInitElements) {
417 // Store the initializer into the field
418 EmitInitializationToLValue(E->getInit(0), FieldLoc);
419 } else {
420 // Default-initialize to null
421 EmitNullInitializationToLValue(FieldLoc, Field->getType());
422 }
423
424 return;
425 }
Chris Lattner6ee6ab02008-04-04 18:42:16 +0000426
427 // Here we iterate over the fields; this makes it simpler to both
428 // default-initialize fields and skip over unnamed fields.
Douglas Gregorc55b0b02009-04-09 21:40:53 +0000429 for (RecordDecl::field_iterator Field = SD->field_begin(CGF.getContext()),
430 FieldEnd = SD->field_end(CGF.getContext());
Douglas Gregor8acb7272008-12-11 16:49:14 +0000431 Field != FieldEnd; ++Field) {
432 // We're done once we hit the flexible array member
433 if (Field->getType()->isIncompleteArrayType())
434 break;
435
Douglas Gregor36dd0c52009-01-28 23:36:17 +0000436 if (Field->isUnnamedBitfield())
Chris Lattner6ee6ab02008-04-04 18:42:16 +0000437 continue;
Douglas Gregor36dd0c52009-01-28 23:36:17 +0000438
Eli Friedman2e630542008-06-13 23:01:12 +0000439 // FIXME: volatility
Douglas Gregor82462762009-01-29 16:53:55 +0000440 LValue FieldLoc = CGF.EmitLValueForField(DestPtr, *Field, false, 0);
Chris Lattner6ee6ab02008-04-04 18:42:16 +0000441 if (CurInitVal < NumInitElements) {
442 // Store the initializer into the field
Chris Lattner6ee6ab02008-04-04 18:42:16 +0000443 EmitInitializationToLValue(E->getInit(CurInitVal++), FieldLoc);
444 } else {
445 // We're out of initalizers; default-initialize to null
Douglas Gregor8acb7272008-12-11 16:49:14 +0000446 EmitNullInitializationToLValue(FieldLoc, Field->getType());
Chris Lattner6ee6ab02008-04-04 18:42:16 +0000447 }
Lauro Ramos Venancioa62b1df2008-02-19 19:27:31 +0000448 }
Devang Patelddde6d52007-10-26 17:44:44 +0000449}
450
Chris Lattnera7300102007-08-21 04:59:27 +0000451//===----------------------------------------------------------------------===//
452// Entry Points into this File
453//===----------------------------------------------------------------------===//
454
455/// EmitAggExpr - Emit the computation of the specified expression of
456/// aggregate type. The result is computed into DestPtr. Note that if
457/// DestPtr is null, the value of the aggregate expression is not needed.
458void CodeGenFunction::EmitAggExpr(const Expr *E, llvm::Value *DestPtr,
459 bool VolatileDest) {
460 assert(E && hasAggregateLLVMType(E->getType()) &&
461 "Invalid aggregate expression to emit");
462
463 AggExprEmitter(*this, DestPtr, VolatileDest).Visit(const_cast<Expr*>(E));
464}
Daniel Dunbarbde0c9d2008-09-09 20:49:46 +0000465
466void CodeGenFunction::EmitAggregateClear(llvm::Value *DestPtr, QualType Ty) {
467 assert(!Ty->isAnyComplexType() && "Shouldn't happen for complex");
468
469 EmitMemSetToZero(DestPtr, Ty);
470}
471
472void CodeGenFunction::EmitAggregateCopy(llvm::Value *DestPtr,
473 llvm::Value *SrcPtr, QualType Ty) {
474 assert(!Ty->isAnyComplexType() && "Shouldn't happen for complex");
475
Chris Lattner1ec02512009-02-28 18:31:01 +0000476 // Aggregate assignment turns into llvm.memcpy. This is almost valid per
Chris Lattner1db26ed2009-02-28 18:18:58 +0000477 // C99 6.5.16.1p3, which states "If the value being stored in an object is
478 // read from another object that overlaps in anyway the storage of the first
479 // object, then the overlap shall be exact and the two objects shall have
480 // qualified or unqualified versions of a compatible type."
481 //
Chris Lattner1ec02512009-02-28 18:31:01 +0000482 // memcpy is not defined if the source and destination pointers are exactly
Chris Lattner1db26ed2009-02-28 18:18:58 +0000483 // equal, but other compilers do this optimization, and almost every memcpy
484 // implementation handles this case safely. If there is a libc that does not
485 // safely handle this, we can add a target hook.
Daniel Dunbarbde0c9d2008-09-09 20:49:46 +0000486 const llvm::Type *BP = llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
487 if (DestPtr->getType() != BP)
488 DestPtr = Builder.CreateBitCast(DestPtr, BP, "tmp");
489 if (SrcPtr->getType() != BP)
490 SrcPtr = Builder.CreateBitCast(SrcPtr, BP, "tmp");
491
492 // Get size and alignment info for this aggregate.
493 std::pair<uint64_t, unsigned> TypeInfo = getContext().getTypeInfo(Ty);
494
495 // FIXME: Handle variable sized types.
496 const llvm::Type *IntPtr = llvm::IntegerType::get(LLVMPointerWidth);
497
Chris Lattner1db26ed2009-02-28 18:18:58 +0000498 Builder.CreateCall4(CGM.getMemCpyFn(),
Daniel Dunbarbde0c9d2008-09-09 20:49:46 +0000499 DestPtr, SrcPtr,
500 // TypeInfo.first describes size in bits.
501 llvm::ConstantInt::get(IntPtr, TypeInfo.first/8),
502 llvm::ConstantInt::get(llvm::Type::Int32Ty,
503 TypeInfo.second/8));
504}