blob: d90f701758c77e246110ec80b996707c2224992e [file] [log] [blame]
Chris Lattner11e0de52007-08-24 02:22:53 +00001//===--- CGExprAgg.cpp - Emit LLVM Code from Aggregate Expressions --------===//
Chris Lattnerd79671f2007-08-10 20:13:28 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner5b12ab82007-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 Lattnerd79671f2007-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 Lattner6278e6a2007-08-11 00:04:45 +000015#include "CodeGenModule.h"
Daniel Dunbarad319a72008-08-11 05:00:27 +000016#include "clang/AST/ASTContext.h"
Anders Carlssonb7f8f592009-04-17 00:06:03 +000017#include "clang/AST/DeclCXX.h"
Daniel Dunbarad319a72008-08-11 05:00:27 +000018#include "clang/AST/StmtVisitor.h"
Chris Lattner6278e6a2007-08-11 00:04:45 +000019#include "llvm/Constants.h"
20#include "llvm/Function.h"
Devang Patel87174172007-10-26 17:44:44 +000021#include "llvm/GlobalVariable.h"
Chris Lattner4758b402007-08-21 04:25:47 +000022#include "llvm/Support/Compiler.h"
Chris Lattner579a05d2008-04-04 18:42:16 +000023#include "llvm/Intrinsics.h"
Chris Lattnerd79671f2007-08-10 20:13:28 +000024using namespace clang;
25using namespace CodeGen;
Chris Lattner6278e6a2007-08-11 00:04:45 +000026
Chris Lattner4758b402007-08-21 04:25:47 +000027//===----------------------------------------------------------------------===//
28// Aggregate Expression Emitter
29//===----------------------------------------------------------------------===//
30
31namespace {
32class VISIBILITY_HIDDEN AggExprEmitter : public StmtVisitor<AggExprEmitter> {
33 CodeGenFunction &CGF;
Daniel Dunbarcb463852008-11-01 01:53:16 +000034 CGBuilderTy &Builder;
Chris Lattner4758b402007-08-21 04:25:47 +000035 llvm::Value *DestPtr;
36 bool VolatileDest;
Mike Stumpec3cbfe2009-05-26 22:03:21 +000037 bool IgnoreResult;
38
Chris Lattner4758b402007-08-21 04:25:47 +000039public:
Mike Stump3e97f3b2009-05-27 01:42:21 +000040 AggExprEmitter(CodeGenFunction &cgf, llvm::Value *destPtr, bool v,
41 bool ignore)
Chris Lattnerbda69f82007-08-26 23:13:56 +000042 : CGF(cgf), Builder(CGF.Builder),
Mike Stump3e97f3b2009-05-27 01:42:21 +000043 DestPtr(destPtr), VolatileDest(v), IgnoreResult(ignore) {
Chris Lattner4758b402007-08-21 04:25:47 +000044 }
45
Chris Lattner835635d2007-08-21 04:59:27 +000046 //===--------------------------------------------------------------------===//
47 // Utilities
48 //===--------------------------------------------------------------------===//
49
Chris Lattner4758b402007-08-21 04:25:47 +000050 /// EmitAggLoadOfLValue - Given an expression with aggregate type that
51 /// represents a value lvalue, this method emits the address of the lvalue,
52 /// then loads the result into DestPtr.
53 void EmitAggLoadOfLValue(const Expr *E);
Eli Friedmanf23b6fa2008-05-19 17:51:16 +000054
Mike Stumpca9fc092009-05-23 20:28:01 +000055 /// EmitFinalDestCopy - Perform the final copy to DestPtr, if desired.
Mike Stumpec3cbfe2009-05-26 22:03:21 +000056 void EmitFinalDestCopy(const Expr *E, LValue Src, bool Ignore = false);
57 void EmitFinalDestCopy(const Expr *E, RValue Src, bool Ignore = false);
Mike Stumpca9fc092009-05-23 20:28:01 +000058
Chris Lattner835635d2007-08-21 04:59:27 +000059 //===--------------------------------------------------------------------===//
60 // Visitor Methods
61 //===--------------------------------------------------------------------===//
62
Chris Lattner4758b402007-08-21 04:25:47 +000063 void VisitStmt(Stmt *S) {
Daniel Dunbara7c8cf62008-08-16 00:56:44 +000064 CGF.ErrorUnsupported(S, "aggregate expression");
Chris Lattner4758b402007-08-21 04:25:47 +000065 }
66 void VisitParenExpr(ParenExpr *PE) { Visit(PE->getSubExpr()); }
Eli Friedman3f66b842009-01-27 09:03:41 +000067 void VisitUnaryExtension(UnaryOperator *E) { Visit(E->getSubExpr()); }
Chris Lattner4758b402007-08-21 04:25:47 +000068
69 // l-values.
Seo Sanghyeond4d8c3c2007-12-14 02:04:12 +000070 void VisitDeclRefExpr(DeclRefExpr *DRE) { EmitAggLoadOfLValue(DRE); }
71 void VisitMemberExpr(MemberExpr *ME) { EmitAggLoadOfLValue(ME); }
72 void VisitUnaryDeref(UnaryOperator *E) { EmitAggLoadOfLValue(E); }
Seo Sanghyeon6f1b2742007-12-23 03:11:58 +000073 void VisitStringLiteral(StringLiteral *E) { EmitAggLoadOfLValue(E); }
Chris Lattner2f343dd2009-04-21 23:00:09 +000074 void VisitCompoundLiteralExpr(CompoundLiteralExpr *E) {
75 EmitAggLoadOfLValue(E);
76 }
Seo Sanghyeond4d8c3c2007-12-14 02:04:12 +000077 void VisitArraySubscriptExpr(ArraySubscriptExpr *E) {
78 EmitAggLoadOfLValue(E);
79 }
Chris Lattner2f343dd2009-04-21 23:00:09 +000080 void VisitBlockDeclRefExpr(const BlockDeclRefExpr *E) {
81 EmitAggLoadOfLValue(E);
82 }
83 void VisitPredefinedExpr(const PredefinedExpr *E) {
84 EmitAggLoadOfLValue(E);
85 }
Douglas Gregor347f7ea2009-01-28 21:54:33 +000086
Chris Lattner4758b402007-08-21 04:25:47 +000087 // Operators.
Nuno Lopes7ffcf932009-01-15 20:14:33 +000088 void VisitCStyleCastExpr(CStyleCastExpr *E);
Anders Carlsson1ba25ca2008-01-14 06:28:57 +000089 void VisitImplicitCastExpr(ImplicitCastExpr *E);
Anders Carlsson0370eb22007-10-31 22:04:46 +000090 void VisitCallExpr(const CallExpr *E);
Chris Lattner49e3bfa2007-08-31 22:54:14 +000091 void VisitStmtExpr(const StmtExpr *E);
Chris Lattner4758b402007-08-21 04:25:47 +000092 void VisitBinaryOperator(const BinaryOperator *BO);
Chris Lattnercd9fb242007-08-21 04:43:17 +000093 void VisitBinAssign(const BinaryOperator *E);
Eli Friedman4b0e2a32008-05-20 07:56:31 +000094 void VisitBinComma(const BinaryOperator *E);
Chris Lattner4758b402007-08-21 04:25:47 +000095
Chris Lattnerb1d329d2008-06-24 17:04:18 +000096 void VisitObjCMessageExpr(ObjCMessageExpr *E);
Daniel Dunbarc8317a42008-08-23 10:51:21 +000097 void VisitObjCIvarRefExpr(ObjCIvarRefExpr *E) {
98 EmitAggLoadOfLValue(E);
99 }
Daniel Dunbar55310df2008-08-27 06:57:25 +0000100 void VisitObjCPropertyRefExpr(ObjCPropertyRefExpr *E);
Fariborz Jahanian8a1810f2008-11-22 18:39:36 +0000101 void VisitObjCKVCRefExpr(ObjCKVCRefExpr *E);
Chris Lattner4758b402007-08-21 04:25:47 +0000102
103 void VisitConditionalOperator(const ConditionalOperator *CO);
Devang Patel87174172007-10-26 17:44:44 +0000104 void VisitInitListExpr(InitListExpr *E);
Chris Lattneraa9c7ae2008-04-08 04:40:51 +0000105 void VisitCXXDefaultArgExpr(CXXDefaultArgExpr *DAE) {
106 Visit(DAE->getExpr());
107 }
Anders Carlsson1619a5042009-05-03 17:47:16 +0000108 void VisitCXXConstructExpr(const CXXConstructExpr *E);
Anders Carlssonc82b86d2009-05-19 04:48:36 +0000109 void VisitCXXExprWithTemporaries(CXXExprWithTemporaries *E);
110
Eli Friedman21911e82008-05-27 15:51:49 +0000111 void VisitVAArgExpr(VAArgExpr *E);
Chris Lattner579a05d2008-04-04 18:42:16 +0000112
113 void EmitInitializationToLValue(Expr *E, LValue Address);
114 void EmitNullInitializationToLValue(LValue Address, QualType T);
Chris Lattner4758b402007-08-21 04:25:47 +0000115 // case Expr::ChooseExprClass:
Lauro Ramos Venanciodec89732008-02-18 22:44:02 +0000116
Chris Lattner4758b402007-08-21 04:25:47 +0000117};
118} // end anonymous namespace.
119
Chris Lattner835635d2007-08-21 04:59:27 +0000120//===----------------------------------------------------------------------===//
121// Utilities
122//===----------------------------------------------------------------------===//
Chris Lattner4758b402007-08-21 04:25:47 +0000123
Chris Lattner6278e6a2007-08-11 00:04:45 +0000124/// EmitAggLoadOfLValue - Given an expression with aggregate type that
125/// represents a value lvalue, this method emits the address of the lvalue,
126/// then loads the result into DestPtr.
Chris Lattner4758b402007-08-21 04:25:47 +0000127void AggExprEmitter::EmitAggLoadOfLValue(const Expr *E) {
128 LValue LV = CGF.EmitLValue(E);
Mike Stumpca9fc092009-05-23 20:28:01 +0000129 EmitFinalDestCopy(E, LV);
130}
131
132/// EmitFinalDestCopy - Perform the final copy to DestPtr, if desired.
Mike Stumpec3cbfe2009-05-26 22:03:21 +0000133void AggExprEmitter::EmitFinalDestCopy(const Expr *E, RValue Src, bool Ignore) {
Mike Stumpca9fc092009-05-23 20:28:01 +0000134 assert(Src.isAggregate() && "value must be aggregate value!");
135
Chris Lattner6278e6a2007-08-11 00:04:45 +0000136 // If the result is ignored, don't copy from the value.
Mike Stump332ec2c2009-05-23 22:01:27 +0000137 if (DestPtr == 0) {
Mike Stumpec3cbfe2009-05-26 22:03:21 +0000138 if (!Src.isVolatileQualified() || (IgnoreResult && Ignore))
Mike Stump332ec2c2009-05-23 22:01:27 +0000139 return;
Mike Stumpec3cbfe2009-05-26 22:03:21 +0000140 // If the source is volatile, we must read from it; to do that, we need
141 // some place to put it.
142 DestPtr = CGF.CreateTempAlloca(CGF.ConvertType(E->getType()), "agg.tmp");
Mike Stump332ec2c2009-05-23 22:01:27 +0000143 }
Chris Lattner6278e6a2007-08-11 00:04:45 +0000144
Mike Stumpca9fc092009-05-23 20:28:01 +0000145 // If the result of the assignment is used, copy the LHS there also.
146 // FIXME: Pass VolatileDest as well. I think we also need to merge volatile
147 // from the source as well, as we can't eliminate it if either operand
148 // is volatile, unless copy has volatile for both source and destination..
Mike Stump5e9e61b2009-05-23 22:29:41 +0000149 CGF.EmitAggregateCopy(DestPtr, Src.getAggregateAddr(), E->getType(),
150 VolatileDest|Src.isVolatileQualified());
Mike Stumpca9fc092009-05-23 20:28:01 +0000151}
152
153/// EmitFinalDestCopy - Perform the final copy to DestPtr, if desired.
Mike Stumpec3cbfe2009-05-26 22:03:21 +0000154void AggExprEmitter::EmitFinalDestCopy(const Expr *E, LValue Src, bool Ignore) {
Mike Stumpca9fc092009-05-23 20:28:01 +0000155 assert(Src.isSimple() && "Can't have aggregate bitfield, vector, etc");
156
157 EmitFinalDestCopy(E, RValue::getAggregate(Src.getAddress(),
Mike Stumpec3cbfe2009-05-26 22:03:21 +0000158 Src.isVolatileQualified()),
159 Ignore);
Chris Lattner6278e6a2007-08-11 00:04:45 +0000160}
161
Chris Lattner835635d2007-08-21 04:59:27 +0000162//===----------------------------------------------------------------------===//
163// Visitor Methods
164//===----------------------------------------------------------------------===//
165
Nuno Lopes7ffcf932009-01-15 20:14:33 +0000166void AggExprEmitter::VisitCStyleCastExpr(CStyleCastExpr *E) {
167 // GCC union extension
168 if (E->getType()->isUnionType()) {
169 RecordDecl *SD = E->getType()->getAsRecordType()->getDecl();
Douglas Gregorbcced4e2009-04-09 21:40:53 +0000170 LValue FieldLoc = CGF.EmitLValueForField(DestPtr,
171 *SD->field_begin(CGF.getContext()),
172 true, 0);
Nuno Lopes7ffcf932009-01-15 20:14:33 +0000173 EmitInitializationToLValue(E->getSubExpr(), FieldLoc);
174 return;
175 }
176
177 Visit(E->getSubExpr());
178}
179
Chris Lattner0f398c42008-07-26 22:37:01 +0000180void AggExprEmitter::VisitImplicitCastExpr(ImplicitCastExpr *E) {
Eli Friedman2a695472009-05-28 23:04:00 +0000181 assert(CGF.getContext().hasSameUnqualifiedType(E->getSubExpr()->getType(),
182 E->getType()) &&
Chris Lattner0f398c42008-07-26 22:37:01 +0000183 "Implicit cast types must be compatible");
Anders Carlsson1ba25ca2008-01-14 06:28:57 +0000184 Visit(E->getSubExpr());
185}
186
Chris Lattner0f398c42008-07-26 22:37:01 +0000187void AggExprEmitter::VisitCallExpr(const CallExpr *E) {
Anders Carlssonddcbfe72009-05-27 16:45:02 +0000188 if (E->getCallReturnType()->isReferenceType()) {
189 EmitAggLoadOfLValue(E);
190 return;
191 }
192
Anders Carlsson0370eb22007-10-31 22:04:46 +0000193 RValue RV = CGF.EmitCallExpr(E);
Mike Stumpca9fc092009-05-23 20:28:01 +0000194 EmitFinalDestCopy(E, RV);
Anders Carlsson0370eb22007-10-31 22:04:46 +0000195}
Chris Lattner0f398c42008-07-26 22:37:01 +0000196
197void AggExprEmitter::VisitObjCMessageExpr(ObjCMessageExpr *E) {
Daniel Dunbar97db84c2008-08-23 03:46:30 +0000198 RValue RV = CGF.EmitObjCMessageExpr(E);
Mike Stumpca9fc092009-05-23 20:28:01 +0000199 EmitFinalDestCopy(E, RV);
Chris Lattnerb1d329d2008-06-24 17:04:18 +0000200}
Anders Carlsson0370eb22007-10-31 22:04:46 +0000201
Daniel Dunbar55310df2008-08-27 06:57:25 +0000202void AggExprEmitter::VisitObjCPropertyRefExpr(ObjCPropertyRefExpr *E) {
203 RValue RV = CGF.EmitObjCPropertyGet(E);
Mike Stumpca9fc092009-05-23 20:28:01 +0000204 EmitFinalDestCopy(E, RV);
Daniel Dunbar55310df2008-08-27 06:57:25 +0000205}
206
Fariborz Jahanian8a1810f2008-11-22 18:39:36 +0000207void AggExprEmitter::VisitObjCKVCRefExpr(ObjCKVCRefExpr *E) {
208 RValue RV = CGF.EmitObjCPropertyGet(E);
Mike Stumpca9fc092009-05-23 20:28:01 +0000209 EmitFinalDestCopy(E, RV);
Fariborz Jahanian8a1810f2008-11-22 18:39:36 +0000210}
211
Chris Lattner0f398c42008-07-26 22:37:01 +0000212void AggExprEmitter::VisitBinComma(const BinaryOperator *E) {
Mike Stump332ec2c2009-05-23 22:01:27 +0000213 CGF.EmitAnyExpr(E->getLHS());
Mike Stump23abd462009-05-23 21:40:07 +0000214 CGF.EmitAggExpr(E->getRHS(), DestPtr, VolatileDest);
Eli Friedman4b0e2a32008-05-20 07:56:31 +0000215}
216
Chris Lattner49e3bfa2007-08-31 22:54:14 +0000217void AggExprEmitter::VisitStmtExpr(const StmtExpr *E) {
218 CGF.EmitCompoundStmt(*E->getSubStmt(), true, DestPtr, VolatileDest);
219}
220
Chris Lattner4758b402007-08-21 04:25:47 +0000221void AggExprEmitter::VisitBinaryOperator(const BinaryOperator *E) {
Daniel Dunbara7c8cf62008-08-16 00:56:44 +0000222 CGF.ErrorUnsupported(E, "aggregate binary expression");
Chris Lattner835635d2007-08-21 04:59:27 +0000223}
224
Chris Lattnercd9fb242007-08-21 04:43:17 +0000225void AggExprEmitter::VisitBinAssign(const BinaryOperator *E) {
Eli Friedmanf54c4e52008-02-11 01:09:17 +0000226 // For an assignment to work, the value on the right has
227 // to be compatible with the value on the left.
Eli Friedman2a695472009-05-28 23:04:00 +0000228 assert(CGF.getContext().hasSameUnqualifiedType(E->getLHS()->getType(),
229 E->getRHS()->getType())
Eli Friedmanf54c4e52008-02-11 01:09:17 +0000230 && "Invalid assignment");
Chris Lattner4758b402007-08-21 04:25:47 +0000231 LValue LHS = CGF.EmitLValue(E->getLHS());
Chris Lattner6278e6a2007-08-11 00:04:45 +0000232
Daniel Dunbar4b8c6db2008-08-30 05:35:15 +0000233 // We have to special case property setters, otherwise we must have
234 // a simple lvalue (no aggregates inside vectors, bitfields).
235 if (LHS.isPropertyRef()) {
Daniel Dunbar4b8c6db2008-08-30 05:35:15 +0000236 llvm::Value *AggLoc = DestPtr;
237 if (!AggLoc)
238 AggLoc = CGF.CreateTempAlloca(CGF.ConvertType(E->getRHS()->getType()));
Mike Stump9afc476d2009-05-23 23:48:13 +0000239 CGF.EmitAggExpr(E->getRHS(), AggLoc, VolatileDest);
Daniel Dunbar4b8c6db2008-08-30 05:35:15 +0000240 CGF.EmitObjCPropertySet(LHS.getPropertyRefExpr(),
Mike Stump9afc476d2009-05-23 23:48:13 +0000241 RValue::getAggregate(AggLoc, VolatileDest));
Fariborz Jahanian9ac53512008-11-22 22:30:21 +0000242 }
243 else if (LHS.isKVCRef()) {
Fariborz Jahanian9ac53512008-11-22 22:30:21 +0000244 llvm::Value *AggLoc = DestPtr;
245 if (!AggLoc)
246 AggLoc = CGF.CreateTempAlloca(CGF.ConvertType(E->getRHS()->getType()));
Mike Stumpb9f25182009-05-23 23:52:31 +0000247 CGF.EmitAggExpr(E->getRHS(), AggLoc, VolatileDest);
Fariborz Jahanian9ac53512008-11-22 22:30:21 +0000248 CGF.EmitObjCPropertySet(LHS.getKVCRefExpr(),
Mike Stumpb9f25182009-05-23 23:52:31 +0000249 RValue::getAggregate(AggLoc, VolatileDest));
Daniel Dunbar4b8c6db2008-08-30 05:35:15 +0000250 } else {
251 // Codegen the RHS so that it stores directly into the LHS.
Mike Stump86736572009-05-23 04:13:59 +0000252 CGF.EmitAggExpr(E->getRHS(), LHS.getAddress(), LHS.isVolatileQualified());
Mike Stumpec3cbfe2009-05-26 22:03:21 +0000253 EmitFinalDestCopy(E, LHS, true);
Daniel Dunbar4b8c6db2008-08-30 05:35:15 +0000254 }
Chris Lattner6278e6a2007-08-11 00:04:45 +0000255}
256
Chris Lattner4758b402007-08-21 04:25:47 +0000257void AggExprEmitter::VisitConditionalOperator(const ConditionalOperator *E) {
Daniel Dunbara612e792008-11-13 01:38:36 +0000258 llvm::BasicBlock *LHSBlock = CGF.createBasicBlock("cond.true");
259 llvm::BasicBlock *RHSBlock = CGF.createBasicBlock("cond.false");
260 llvm::BasicBlock *ContBlock = CGF.createBasicBlock("cond.end");
Chris Lattner6278e6a2007-08-11 00:04:45 +0000261
Chris Lattner4758b402007-08-21 04:25:47 +0000262 llvm::Value *Cond = CGF.EvaluateExprAsBool(E->getCond());
Chris Lattnerbda69f82007-08-26 23:13:56 +0000263 Builder.CreateCondBr(Cond, LHSBlock, RHSBlock);
Chris Lattner6278e6a2007-08-11 00:04:45 +0000264
Chris Lattner4758b402007-08-21 04:25:47 +0000265 CGF.EmitBlock(LHSBlock);
Chris Lattner6278e6a2007-08-11 00:04:45 +0000266
267 // Handle the GNU extension for missing LHS.
268 assert(E->getLHS() && "Must have LHS for aggregate value");
269
Chris Lattner926792f2007-08-21 05:02:10 +0000270 Visit(E->getLHS());
Daniel Dunbarc56e6762008-11-11 09:41:28 +0000271 CGF.EmitBranch(ContBlock);
Chris Lattner6278e6a2007-08-11 00:04:45 +0000272
Chris Lattner4758b402007-08-21 04:25:47 +0000273 CGF.EmitBlock(RHSBlock);
Chris Lattner6278e6a2007-08-11 00:04:45 +0000274
Chris Lattner926792f2007-08-21 05:02:10 +0000275 Visit(E->getRHS());
Daniel Dunbarc56e6762008-11-11 09:41:28 +0000276 CGF.EmitBranch(ContBlock);
Chris Lattner6278e6a2007-08-11 00:04:45 +0000277
Chris Lattner4758b402007-08-21 04:25:47 +0000278 CGF.EmitBlock(ContBlock);
Chris Lattner6278e6a2007-08-11 00:04:45 +0000279}
Chris Lattner835635d2007-08-21 04:59:27 +0000280
Eli Friedman21911e82008-05-27 15:51:49 +0000281void AggExprEmitter::VisitVAArgExpr(VAArgExpr *VE) {
Daniel Dunbare9fcadd22009-02-11 22:25:55 +0000282 llvm::Value *ArgValue = CGF.EmitVAListRef(VE->getSubExpr());
Anders Carlsson13abd7e2008-11-04 05:30:00 +0000283 llvm::Value *ArgPtr = CGF.EmitVAArg(ArgValue, VE->getType());
284
Sebastian Redl020cddc2009-01-09 21:09:38 +0000285 if (!ArgPtr) {
Anders Carlsson13abd7e2008-11-04 05:30:00 +0000286 CGF.ErrorUnsupported(VE, "aggregate va_arg expression");
Sebastian Redl020cddc2009-01-09 21:09:38 +0000287 return;
288 }
289
Mike Stumpca9fc092009-05-23 20:28:01 +0000290 EmitFinalDestCopy(VE, LValue::MakeAddr(ArgPtr, 0));
Eli Friedman21911e82008-05-27 15:51:49 +0000291}
292
Anders Carlssonb7f8f592009-04-17 00:06:03 +0000293void
Anders Carlsson1619a5042009-05-03 17:47:16 +0000294AggExprEmitter::VisitCXXConstructExpr(const CXXConstructExpr *E) {
Anders Carlssonc82b86d2009-05-19 04:48:36 +0000295 llvm::Value *V = DestPtr;
Anders Carlssonb7f8f592009-04-17 00:06:03 +0000296
Anders Carlssonc82b86d2009-05-19 04:48:36 +0000297 if (!V) {
298 assert(isa<CXXTempVarDecl>(E->getVarDecl()) &&
299 "Must have a temp var decl when there's no destination!");
300
301 V = CGF.CreateTempAlloca(CGF.ConvertType(E->getVarDecl()->getType()),
302 "tmpvar");
303 }
304
305 CGF.EmitCXXConstructExpr(V, E);
306}
307
308void AggExprEmitter::VisitCXXExprWithTemporaries(CXXExprWithTemporaries *E) {
309 // FIXME: Do something with the temporaries!
310 Visit(E->getSubExpr());
Anders Carlssonb7f8f592009-04-17 00:06:03 +0000311}
312
Chris Lattner579a05d2008-04-04 18:42:16 +0000313void AggExprEmitter::EmitInitializationToLValue(Expr* E, LValue LV) {
314 // FIXME: Are initializers affected by volatile?
Douglas Gregor0202cb42009-01-29 17:44:32 +0000315 if (isa<ImplicitValueInitExpr>(E)) {
Douglas Gregor347f7ea2009-01-28 21:54:33 +0000316 EmitNullInitializationToLValue(LV, E->getType());
Douglas Gregor0202cb42009-01-29 17:44:32 +0000317 } else if (E->getType()->isComplexType()) {
318 CGF.EmitComplexExprIntoAddr(E, LV.getAddress(), false);
Eli Friedman6e313212008-05-12 15:06:05 +0000319 } else if (CGF.hasAggregateLLVMType(E->getType())) {
320 CGF.EmitAnyExpr(E, LV.getAddress(), false);
321 } else {
322 CGF.EmitStoreThroughLValue(CGF.EmitAnyExpr(E), LV, E->getType());
Chris Lattner579a05d2008-04-04 18:42:16 +0000323 }
Chris Lattner579a05d2008-04-04 18:42:16 +0000324}
325
326void AggExprEmitter::EmitNullInitializationToLValue(LValue LV, QualType T) {
327 if (!CGF.hasAggregateLLVMType(T)) {
328 // For non-aggregates, we can store zero
Daniel Dunbare8bdce42008-08-06 05:32:55 +0000329 llvm::Value *Null = llvm::Constant::getNullValue(CGF.ConvertType(T));
330 CGF.EmitStoreThroughLValue(RValue::get(Null), LV, T);
Lauro Ramos Venancioe2162c62008-02-19 19:27:31 +0000331 } else {
Chris Lattner579a05d2008-04-04 18:42:16 +0000332 // Otherwise, just memset the whole thing to zero. This is legal
333 // because in LLVM, all default initializers are guaranteed to have a
334 // bit pattern of all zeros.
Eli Friedman20bb5e02009-04-13 21:47:26 +0000335 // FIXME: That isn't true for member pointers!
Chris Lattner579a05d2008-04-04 18:42:16 +0000336 // There's a potential optimization opportunity in combining
337 // memsets; that would be easy for arrays, but relatively
338 // difficult for structures with the current code.
Eli Friedman9127aa12009-03-28 03:10:45 +0000339 CGF.EmitMemSetToZero(LV.getAddress(), T);
Chris Lattner579a05d2008-04-04 18:42:16 +0000340 }
341}
342
Chris Lattner579a05d2008-04-04 18:42:16 +0000343void AggExprEmitter::VisitInitListExpr(InitListExpr *E) {
Eli Friedmanf5d08c92008-12-02 01:17:45 +0000344#if 0
345 // FIXME: Disabled while we figure out what to do about
346 // test/CodeGen/bitfield.c
347 //
Mike Stump18bb9282009-05-16 07:57:57 +0000348 // If we can, prefer a copy from a global; this is a lot less code for long
349 // globals, and it's easier for the current optimizers to analyze.
350 // FIXME: Should we really be doing this? Should we try to avoid cases where
351 // we emit a global with a lot of zeros? Should we try to avoid short
352 // globals?
Eli Friedman7139af42009-01-25 02:32:41 +0000353 if (E->isConstantInitializer(CGF.getContext(), 0)) {
Eli Friedmanc59bb482008-11-30 02:11:09 +0000354 llvm::Constant* C = CGF.CGM.EmitConstantExpr(E, &CGF);
355 llvm::GlobalVariable* GV =
356 new llvm::GlobalVariable(C->getType(), true,
357 llvm::GlobalValue::InternalLinkage,
358 C, "", &CGF.CGM.getModule(), 0);
Mike Stumpca9fc092009-05-23 20:28:01 +0000359 EmitFinalDestCopy(E, LValue::MakeAddr(GV, 0));
Eli Friedmanc59bb482008-11-30 02:11:09 +0000360 return;
361 }
Eli Friedmanf5d08c92008-12-02 01:17:45 +0000362#endif
Douglas Gregorbf7207a2009-01-29 19:42:23 +0000363 if (E->hadArrayRangeDesignator()) {
364 CGF.ErrorUnsupported(E, "GNU array range designator extension");
365 }
366
Chris Lattner579a05d2008-04-04 18:42:16 +0000367 // Handle initialization of an array.
368 if (E->getType()->isArrayType()) {
369 const llvm::PointerType *APType =
370 cast<llvm::PointerType>(DestPtr->getType());
371 const llvm::ArrayType *AType =
372 cast<llvm::ArrayType>(APType->getElementType());
373
374 uint64_t NumInitElements = E->getNumInits();
Eli Friedmanf23b6fa2008-05-19 17:51:16 +0000375
Chris Lattner0f398c42008-07-26 22:37:01 +0000376 if (E->getNumInits() > 0) {
377 QualType T1 = E->getType();
378 QualType T2 = E->getInit(0)->getType();
Eli Friedman2a695472009-05-28 23:04:00 +0000379 if (CGF.getContext().hasSameUnqualifiedType(T1, T2)) {
Chris Lattner0f398c42008-07-26 22:37:01 +0000380 EmitAggLoadOfLValue(E->getInit(0));
381 return;
382 }
Eli Friedmanf23b6fa2008-05-19 17:51:16 +0000383 }
384
Chris Lattner579a05d2008-04-04 18:42:16 +0000385 uint64_t NumArrayElements = AType->getNumElements();
Chris Lattner7adf0762008-08-04 07:31:14 +0000386 QualType ElementType = CGF.getContext().getCanonicalType(E->getType());
Douglas Gregor347f7ea2009-01-28 21:54:33 +0000387 ElementType = CGF.getContext().getAsArrayType(ElementType)->getElementType();
Chris Lattner579a05d2008-04-04 18:42:16 +0000388
Chris Lattner7adf0762008-08-04 07:31:14 +0000389 unsigned CVRqualifier = ElementType.getCVRQualifiers();
Eli Friedman327944b2008-06-13 23:01:12 +0000390
Chris Lattner579a05d2008-04-04 18:42:16 +0000391 for (uint64_t i = 0; i != NumArrayElements; ++i) {
392 llvm::Value *NextVal = Builder.CreateStructGEP(DestPtr, i, ".array");
393 if (i < NumInitElements)
Eli Friedman327944b2008-06-13 23:01:12 +0000394 EmitInitializationToLValue(E->getInit(i),
395 LValue::MakeAddr(NextVal, CVRqualifier));
Chris Lattner579a05d2008-04-04 18:42:16 +0000396 else
Eli Friedman327944b2008-06-13 23:01:12 +0000397 EmitNullInitializationToLValue(LValue::MakeAddr(NextVal, CVRqualifier),
Chris Lattner579a05d2008-04-04 18:42:16 +0000398 ElementType);
Lauro Ramos Venancioe2162c62008-02-19 19:27:31 +0000399 }
Chris Lattner579a05d2008-04-04 18:42:16 +0000400 return;
401 }
402
403 assert(E->getType()->isRecordType() && "Only support structs/unions here!");
404
405 // Do struct initialization; this code just sets each individual member
406 // to the approprate value. This makes bitfield support automatic;
407 // the disadvantage is that the generated code is more difficult for
408 // the optimizer, especially with bitfields.
409 unsigned NumInitElements = E->getNumInits();
410 RecordDecl *SD = E->getType()->getAsRecordType()->getDecl();
Chris Lattner579a05d2008-04-04 18:42:16 +0000411 unsigned CurInitVal = 0;
Douglas Gregor51695702009-01-29 16:53:55 +0000412
413 if (E->getType()->isUnionType()) {
414 // Only initialize one field of a union. The field itself is
415 // specified by the initializer list.
416 if (!E->getInitializedFieldInUnion()) {
417 // Empty union; we have nothing to do.
418
419#ifndef NDEBUG
420 // Make sure that it's really an empty and not a failure of
421 // semantic analysis.
Douglas Gregorbcced4e2009-04-09 21:40:53 +0000422 for (RecordDecl::field_iterator Field = SD->field_begin(CGF.getContext()),
423 FieldEnd = SD->field_end(CGF.getContext());
Douglas Gregor51695702009-01-29 16:53:55 +0000424 Field != FieldEnd; ++Field)
425 assert(Field->isUnnamedBitfield() && "Only unnamed bitfields allowed");
426#endif
427 return;
428 }
429
430 // FIXME: volatility
431 FieldDecl *Field = E->getInitializedFieldInUnion();
432 LValue FieldLoc = CGF.EmitLValueForField(DestPtr, Field, true, 0);
433
434 if (NumInitElements) {
435 // Store the initializer into the field
436 EmitInitializationToLValue(E->getInit(0), FieldLoc);
437 } else {
438 // Default-initialize to null
439 EmitNullInitializationToLValue(FieldLoc, Field->getType());
440 }
441
442 return;
443 }
Chris Lattner579a05d2008-04-04 18:42:16 +0000444
445 // Here we iterate over the fields; this makes it simpler to both
446 // default-initialize fields and skip over unnamed fields.
Douglas Gregorbcced4e2009-04-09 21:40:53 +0000447 for (RecordDecl::field_iterator Field = SD->field_begin(CGF.getContext()),
448 FieldEnd = SD->field_end(CGF.getContext());
Douglas Gregor91f84212008-12-11 16:49:14 +0000449 Field != FieldEnd; ++Field) {
450 // We're done once we hit the flexible array member
451 if (Field->getType()->isIncompleteArrayType())
452 break;
453
Douglas Gregor17bd0942009-01-28 23:36:17 +0000454 if (Field->isUnnamedBitfield())
Chris Lattner579a05d2008-04-04 18:42:16 +0000455 continue;
Douglas Gregor17bd0942009-01-28 23:36:17 +0000456
Eli Friedman327944b2008-06-13 23:01:12 +0000457 // FIXME: volatility
Douglas Gregor51695702009-01-29 16:53:55 +0000458 LValue FieldLoc = CGF.EmitLValueForField(DestPtr, *Field, false, 0);
Fariborz Jahanian7c1baf42009-05-27 19:54:11 +0000459 // We never generate write-barries for initialized fields.
460 LValue::SetObjCNonGC(FieldLoc, true);
Chris Lattner579a05d2008-04-04 18:42:16 +0000461 if (CurInitVal < NumInitElements) {
462 // Store the initializer into the field
Chris Lattner579a05d2008-04-04 18:42:16 +0000463 EmitInitializationToLValue(E->getInit(CurInitVal++), FieldLoc);
464 } else {
465 // We're out of initalizers; default-initialize to null
Douglas Gregor91f84212008-12-11 16:49:14 +0000466 EmitNullInitializationToLValue(FieldLoc, Field->getType());
Chris Lattner579a05d2008-04-04 18:42:16 +0000467 }
Lauro Ramos Venancioe2162c62008-02-19 19:27:31 +0000468 }
Devang Patel87174172007-10-26 17:44:44 +0000469}
470
Chris Lattner835635d2007-08-21 04:59:27 +0000471//===----------------------------------------------------------------------===//
472// Entry Points into this File
473//===----------------------------------------------------------------------===//
474
Mike Stump25306ca2009-05-26 18:57:45 +0000475/// EmitAggExpr - Emit the computation of the specified expression of aggregate
476/// type. The result is computed into DestPtr. Note that if DestPtr is null,
477/// the value of the aggregate expression is not needed. If VolatileDest is
478/// true, DestPtr cannot be 0.
Chris Lattner835635d2007-08-21 04:59:27 +0000479void CodeGenFunction::EmitAggExpr(const Expr *E, llvm::Value *DestPtr,
Mike Stumpec3cbfe2009-05-26 22:03:21 +0000480 bool VolatileDest, bool IgnoreResult) {
Chris Lattner835635d2007-08-21 04:59:27 +0000481 assert(E && hasAggregateLLVMType(E->getType()) &&
482 "Invalid aggregate expression to emit");
Mike Stump25306ca2009-05-26 18:57:45 +0000483 assert ((DestPtr != 0 || VolatileDest == false)
484 && "volatile aggregate can't be 0");
Chris Lattner835635d2007-08-21 04:59:27 +0000485
Mike Stumpec3cbfe2009-05-26 22:03:21 +0000486 AggExprEmitter(*this, DestPtr, VolatileDest, IgnoreResult)
487 .Visit(const_cast<Expr*>(E));
Chris Lattner835635d2007-08-21 04:59:27 +0000488}
Daniel Dunbar0bc8e862008-09-09 20:49:46 +0000489
490void CodeGenFunction::EmitAggregateClear(llvm::Value *DestPtr, QualType Ty) {
491 assert(!Ty->isAnyComplexType() && "Shouldn't happen for complex");
492
493 EmitMemSetToZero(DestPtr, Ty);
494}
495
496void CodeGenFunction::EmitAggregateCopy(llvm::Value *DestPtr,
Mike Stump5e9e61b2009-05-23 22:29:41 +0000497 llvm::Value *SrcPtr, QualType Ty,
498 bool isVolatile) {
Daniel Dunbar0bc8e862008-09-09 20:49:46 +0000499 assert(!Ty->isAnyComplexType() && "Shouldn't happen for complex");
500
Chris Lattnerca05dfe2009-02-28 18:31:01 +0000501 // Aggregate assignment turns into llvm.memcpy. This is almost valid per
Chris Lattner3ef668c2009-02-28 18:18:58 +0000502 // C99 6.5.16.1p3, which states "If the value being stored in an object is
503 // read from another object that overlaps in anyway the storage of the first
504 // object, then the overlap shall be exact and the two objects shall have
505 // qualified or unqualified versions of a compatible type."
506 //
Chris Lattnerca05dfe2009-02-28 18:31:01 +0000507 // memcpy is not defined if the source and destination pointers are exactly
Chris Lattner3ef668c2009-02-28 18:18:58 +0000508 // equal, but other compilers do this optimization, and almost every memcpy
509 // implementation handles this case safely. If there is a libc that does not
510 // safely handle this, we can add a target hook.
Daniel Dunbar0bc8e862008-09-09 20:49:46 +0000511 const llvm::Type *BP = llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
512 if (DestPtr->getType() != BP)
513 DestPtr = Builder.CreateBitCast(DestPtr, BP, "tmp");
514 if (SrcPtr->getType() != BP)
515 SrcPtr = Builder.CreateBitCast(SrcPtr, BP, "tmp");
516
517 // Get size and alignment info for this aggregate.
518 std::pair<uint64_t, unsigned> TypeInfo = getContext().getTypeInfo(Ty);
519
520 // FIXME: Handle variable sized types.
521 const llvm::Type *IntPtr = llvm::IntegerType::get(LLVMPointerWidth);
522
Mike Stump86736572009-05-23 04:13:59 +0000523 // FIXME: If we have a volatile struct, the optimizer can remove what might
524 // appear to be `extra' memory ops:
525 //
526 // volatile struct { int i; } a, b;
527 //
528 // int main() {
529 // a = b;
530 // a = b;
531 // }
532 //
Mike Stumpec3cbfe2009-05-26 22:03:21 +0000533 // we need to use a differnt call here. We use isVolatile to indicate when
534 // either the source or the destination is volatile.
Chris Lattner3ef668c2009-02-28 18:18:58 +0000535 Builder.CreateCall4(CGM.getMemCpyFn(),
Daniel Dunbar0bc8e862008-09-09 20:49:46 +0000536 DestPtr, SrcPtr,
537 // TypeInfo.first describes size in bits.
538 llvm::ConstantInt::get(IntPtr, TypeInfo.first/8),
539 llvm::ConstantInt::get(llvm::Type::Int32Ty,
540 TypeInfo.second/8));
541}