blob: f57c2ed8816b0ac442e19e33c23fc5663a962bd5 [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"
16#include "clang/AST/AST.h"
17#include "llvm/Constants.h"
18#include "llvm/Function.h"
Devang Patel636c3d02007-10-26 17:44:44 +000019#include "llvm/GlobalVariable.h"
Chris Lattner9c033562007-08-21 04:25:47 +000020#include "llvm/Support/Compiler.h"
Chris Lattneraf6f5282007-08-10 20:13:28 +000021using namespace clang;
22using namespace CodeGen;
Chris Lattner883f6a72007-08-11 00:04:45 +000023
Chris Lattner9c033562007-08-21 04:25:47 +000024//===----------------------------------------------------------------------===//
25// Aggregate Expression Emitter
26//===----------------------------------------------------------------------===//
27
28namespace {
29class VISIBILITY_HIDDEN AggExprEmitter : public StmtVisitor<AggExprEmitter> {
30 CodeGenFunction &CGF;
Devang Patel50c90342007-10-09 19:49:58 +000031 llvm::LLVMFoldingBuilder &Builder;
Chris Lattner9c033562007-08-21 04:25:47 +000032 llvm::Value *DestPtr;
33 bool VolatileDest;
34public:
35 AggExprEmitter(CodeGenFunction &cgf, llvm::Value *destPtr, bool volatileDest)
Chris Lattnerbfc0c1a2007-08-26 23:13:56 +000036 : CGF(cgf), Builder(CGF.Builder),
37 DestPtr(destPtr), VolatileDest(volatileDest) {
Chris Lattner9c033562007-08-21 04:25:47 +000038 }
39
Chris Lattneree755f92007-08-21 04:59:27 +000040 //===--------------------------------------------------------------------===//
41 // Utilities
42 //===--------------------------------------------------------------------===//
43
Chris Lattner9c033562007-08-21 04:25:47 +000044 /// EmitAggLoadOfLValue - Given an expression with aggregate type that
45 /// represents a value lvalue, this method emits the address of the lvalue,
46 /// then loads the result into DestPtr.
47 void EmitAggLoadOfLValue(const Expr *E);
48
Chris Lattnerbfc0c1a2007-08-26 23:13:56 +000049 void EmitAggregateCopy(llvm::Value *DestPtr, llvm::Value *SrcPtr,
50 QualType EltTy);
Lauro Ramos Venancio13e22cf2008-02-19 22:04:22 +000051
52 void EmitAggregateClear(llvm::Value *DestPtr, QualType Ty);
53
54 void EmitNonConstInit(InitListExpr *E);
Chris Lattneree755f92007-08-21 04:59:27 +000055
56 //===--------------------------------------------------------------------===//
57 // Visitor Methods
58 //===--------------------------------------------------------------------===//
59
Chris Lattner9c033562007-08-21 04:25:47 +000060 void VisitStmt(Stmt *S) {
Chris Lattnerdc4d2802007-12-02 01:49:16 +000061 CGF.WarnUnsupported(S, "aggregate expression");
Chris Lattner9c033562007-08-21 04:25:47 +000062 }
63 void VisitParenExpr(ParenExpr *PE) { Visit(PE->getSubExpr()); }
64
65 // l-values.
Seo Sanghyeon9b73b392007-12-14 02:04:12 +000066 void VisitDeclRefExpr(DeclRefExpr *DRE) { EmitAggLoadOfLValue(DRE); }
67 void VisitMemberExpr(MemberExpr *ME) { EmitAggLoadOfLValue(ME); }
68 void VisitUnaryDeref(UnaryOperator *E) { EmitAggLoadOfLValue(E); }
Seo Sanghyeonad6ebd62007-12-23 03:11:58 +000069 void VisitStringLiteral(StringLiteral *E) { 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:
Anders Carlssone4707ff2008-01-14 06:28:57 +000078 void VisitImplicitCastExpr(ImplicitCastExpr *E);
Anders Carlsson148fe672007-10-31 22:04:46 +000079 void VisitCallExpr(const CallExpr *E);
Chris Lattnerb2d963f2007-08-31 22:54:14 +000080 void VisitStmtExpr(const StmtExpr *E);
Chris Lattner9c033562007-08-21 04:25:47 +000081 void VisitBinaryOperator(const BinaryOperator *BO);
Chris Lattner03d6fb92007-08-21 04:43:17 +000082 void VisitBinAssign(const BinaryOperator *E);
Nate Begeman796ef3d2008-01-31 05:38:29 +000083 void VisitOverloadExpr(const OverloadExpr *E);
Chris Lattner9c033562007-08-21 04:25:47 +000084
85
86 void VisitConditionalOperator(const ConditionalOperator *CO);
Devang Patel636c3d02007-10-26 17:44:44 +000087 void VisitInitListExpr(InitListExpr *E);
Chris Lattner9c033562007-08-21 04:25:47 +000088 // case Expr::ChooseExprClass:
Lauro Ramos Venancio305762c2008-02-18 22:44:02 +000089
Chris Lattner9c033562007-08-21 04:25:47 +000090};
91} // end anonymous namespace.
92
Chris Lattneree755f92007-08-21 04:59:27 +000093//===----------------------------------------------------------------------===//
94// Utilities
95//===----------------------------------------------------------------------===//
Chris Lattner9c033562007-08-21 04:25:47 +000096
Lauro Ramos Venancio13e22cf2008-02-19 22:04:22 +000097void AggExprEmitter::EmitAggregateClear(llvm::Value *DestPtr, QualType Ty) {
98 assert(!Ty->isComplexType() && "Shouldn't happen for complex");
99
100 // Aggregate assignment turns into llvm.memset.
101 const llvm::Type *BP = llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
102 if (DestPtr->getType() != BP)
103 DestPtr = Builder.CreateBitCast(DestPtr, BP, "tmp");
104
105 // Get size and alignment info for this aggregate.
Chris Lattner98be4942008-03-05 18:54:05 +0000106 std::pair<uint64_t, unsigned> TypeInfo = CGF.getContext().getTypeInfo(Ty);
Lauro Ramos Venancio13e22cf2008-02-19 22:04:22 +0000107
108 // FIXME: Handle variable sized types.
109 const llvm::Type *IntPtr = llvm::IntegerType::get(CGF.LLVMPointerWidth);
110
111 llvm::Value *MemSetOps[4] = {
112 DestPtr,
113 llvm::ConstantInt::getNullValue(llvm::Type::Int8Ty),
114 // TypeInfo.first describes size in bits.
115 llvm::ConstantInt::get(IntPtr, TypeInfo.first/8),
116 llvm::ConstantInt::get(llvm::Type::Int32Ty, TypeInfo.second/8)
117 };
118
119 Builder.CreateCall(CGF.CGM.getMemSetFn(), MemSetOps, MemSetOps+4);
120}
121
Chris Lattnerbfc0c1a2007-08-26 23:13:56 +0000122void AggExprEmitter::EmitAggregateCopy(llvm::Value *DestPtr,
123 llvm::Value *SrcPtr, QualType Ty) {
124 assert(!Ty->isComplexType() && "Shouldn't happen for complex");
125
126 // Aggregate assignment turns into llvm.memcpy.
Christopher Lambddc23f32007-12-17 01:11:20 +0000127 const llvm::Type *BP = llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
Chris Lattnerbfc0c1a2007-08-26 23:13:56 +0000128 if (DestPtr->getType() != BP)
129 DestPtr = Builder.CreateBitCast(DestPtr, BP, "tmp");
130 if (SrcPtr->getType() != BP)
131 SrcPtr = Builder.CreateBitCast(SrcPtr, BP, "tmp");
132
133 // Get size and alignment info for this aggregate.
Chris Lattner98be4942008-03-05 18:54:05 +0000134 std::pair<uint64_t, unsigned> TypeInfo = CGF.getContext().getTypeInfo(Ty);
Chris Lattnerbfc0c1a2007-08-26 23:13:56 +0000135
136 // FIXME: Handle variable sized types.
137 const llvm::Type *IntPtr = llvm::IntegerType::get(CGF.LLVMPointerWidth);
138
139 llvm::Value *MemCpyOps[4] = {
140 DestPtr, SrcPtr,
Devang Patel636c3d02007-10-26 17:44:44 +0000141 // TypeInfo.first describes size in bits.
142 llvm::ConstantInt::get(IntPtr, TypeInfo.first/8),
Lauro Ramos Venancio13e22cf2008-02-19 22:04:22 +0000143 llvm::ConstantInt::get(llvm::Type::Int32Ty, TypeInfo.second/8)
Chris Lattnerbfc0c1a2007-08-26 23:13:56 +0000144 };
145
146 Builder.CreateCall(CGF.CGM.getMemCpyFn(), MemCpyOps, MemCpyOps+4);
147}
148
149
Chris Lattner883f6a72007-08-11 00:04:45 +0000150/// EmitAggLoadOfLValue - Given an expression with aggregate type that
151/// represents a value lvalue, this method emits the address of the lvalue,
152/// then loads the result into DestPtr.
Chris Lattner9c033562007-08-21 04:25:47 +0000153void AggExprEmitter::EmitAggLoadOfLValue(const Expr *E) {
154 LValue LV = CGF.EmitLValue(E);
Chris Lattner883f6a72007-08-11 00:04:45 +0000155 assert(LV.isSimple() && "Can't have aggregate bitfield, vector, etc");
156 llvm::Value *SrcPtr = LV.getAddress();
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
Chris Lattnerbfc0c1a2007-08-26 23:13:56 +0000163 EmitAggregateCopy(DestPtr, SrcPtr, E->getType());
Chris Lattner883f6a72007-08-11 00:04:45 +0000164}
165
Chris Lattneree755f92007-08-21 04:59:27 +0000166//===----------------------------------------------------------------------===//
167// Visitor Methods
168//===----------------------------------------------------------------------===//
169
Anders Carlssone4707ff2008-01-14 06:28:57 +0000170void AggExprEmitter::VisitImplicitCastExpr(ImplicitCastExpr *E)
171{
172 QualType STy = E->getSubExpr()->getType().getCanonicalType();
173 QualType Ty = E->getType().getCanonicalType();
Eli Friedmanff6e2b72008-02-11 01:09:17 +0000174
175 assert(CGF.getContext().typesAreCompatible(
176 STy.getUnqualifiedType(), Ty.getUnqualifiedType())
177 && "Implicit cast types must be compatible");
Anders Carlssone4707ff2008-01-14 06:28:57 +0000178
179 Visit(E->getSubExpr());
180}
181
Anders Carlsson148fe672007-10-31 22:04:46 +0000182void AggExprEmitter::VisitCallExpr(const CallExpr *E)
183{
184 RValue RV = CGF.EmitCallExpr(E);
185 assert(RV.isAggregate() && "Return value must be aggregate value!");
186
187 // If the result is ignored, don't copy from the value.
188 if (DestPtr == 0)
189 // FIXME: If the source is volatile, we must read from it.
190 return;
191
192 EmitAggregateCopy(DestPtr, RV.getAggregateAddr(), E->getType());
193}
194
Nate Begeman796ef3d2008-01-31 05:38:29 +0000195void AggExprEmitter::VisitOverloadExpr(const OverloadExpr *E)
196{
197 RValue RV = CGF.EmitCallExpr(E->getFn(), E->arg_begin(),
198 E->getNumArgs(CGF.getContext()));
199 assert(RV.isAggregate() && "Return value must be aggregate value!");
200
201 // If the result is ignored, don't copy from the value.
202 if (DestPtr == 0)
203 // FIXME: If the source is volatile, we must read from it.
204 return;
205
206 EmitAggregateCopy(DestPtr, RV.getAggregateAddr(), E->getType());
207}
208
Chris Lattnerb2d963f2007-08-31 22:54:14 +0000209void AggExprEmitter::VisitStmtExpr(const StmtExpr *E) {
210 CGF.EmitCompoundStmt(*E->getSubStmt(), true, DestPtr, VolatileDest);
211}
212
Chris Lattner9c033562007-08-21 04:25:47 +0000213void AggExprEmitter::VisitBinaryOperator(const BinaryOperator *E) {
Chris Lattnerdc4d2802007-12-02 01:49:16 +0000214 CGF.WarnUnsupported(E, "aggregate binary expression");
Chris Lattneree755f92007-08-21 04:59:27 +0000215}
216
Chris Lattner03d6fb92007-08-21 04:43:17 +0000217void AggExprEmitter::VisitBinAssign(const BinaryOperator *E) {
Eli Friedmanff6e2b72008-02-11 01:09:17 +0000218 // For an assignment to work, the value on the right has
219 // to be compatible with the value on the left.
220 assert(CGF.getContext().typesAreCompatible(
221 E->getLHS()->getType().getUnqualifiedType(),
222 E->getRHS()->getType().getUnqualifiedType())
223 && "Invalid assignment");
Chris Lattner9c033562007-08-21 04:25:47 +0000224 LValue LHS = CGF.EmitLValue(E->getLHS());
Chris Lattner883f6a72007-08-11 00:04:45 +0000225
226 // Codegen the RHS so that it stores directly into the LHS.
Chris Lattner9c033562007-08-21 04:25:47 +0000227 CGF.EmitAggExpr(E->getRHS(), LHS.getAddress(), false /*FIXME: VOLATILE LHS*/);
Chris Lattner883f6a72007-08-11 00:04:45 +0000228
Eli Friedmanff6e2b72008-02-11 01:09:17 +0000229 if (DestPtr == 0)
230 return;
231
Chris Lattner883f6a72007-08-11 00:04:45 +0000232 // If the result of the assignment is used, copy the RHS there also.
Eli Friedmanff6e2b72008-02-11 01:09:17 +0000233 EmitAggregateCopy(DestPtr, LHS.getAddress(), E->getType());
Chris Lattner883f6a72007-08-11 00:04:45 +0000234}
235
Chris Lattner9c033562007-08-21 04:25:47 +0000236void AggExprEmitter::VisitConditionalOperator(const ConditionalOperator *E) {
Chris Lattner883f6a72007-08-11 00:04:45 +0000237 llvm::BasicBlock *LHSBlock = new llvm::BasicBlock("cond.?");
238 llvm::BasicBlock *RHSBlock = new llvm::BasicBlock("cond.:");
239 llvm::BasicBlock *ContBlock = new llvm::BasicBlock("cond.cont");
240
Chris Lattner9c033562007-08-21 04:25:47 +0000241 llvm::Value *Cond = CGF.EvaluateExprAsBool(E->getCond());
Chris Lattnerbfc0c1a2007-08-26 23:13:56 +0000242 Builder.CreateCondBr(Cond, LHSBlock, RHSBlock);
Chris Lattner883f6a72007-08-11 00:04:45 +0000243
Chris Lattner9c033562007-08-21 04:25:47 +0000244 CGF.EmitBlock(LHSBlock);
Chris Lattner883f6a72007-08-11 00:04:45 +0000245
246 // Handle the GNU extension for missing LHS.
247 assert(E->getLHS() && "Must have LHS for aggregate value");
248
Chris Lattnerc748f272007-08-21 05:02:10 +0000249 Visit(E->getLHS());
Chris Lattnerbfc0c1a2007-08-26 23:13:56 +0000250 Builder.CreateBr(ContBlock);
251 LHSBlock = Builder.GetInsertBlock();
Chris Lattner883f6a72007-08-11 00:04:45 +0000252
Chris Lattner9c033562007-08-21 04:25:47 +0000253 CGF.EmitBlock(RHSBlock);
Chris Lattner883f6a72007-08-11 00:04:45 +0000254
Chris Lattnerc748f272007-08-21 05:02:10 +0000255 Visit(E->getRHS());
Chris Lattnerbfc0c1a2007-08-26 23:13:56 +0000256 Builder.CreateBr(ContBlock);
257 RHSBlock = Builder.GetInsertBlock();
Chris Lattner883f6a72007-08-11 00:04:45 +0000258
Chris Lattner9c033562007-08-21 04:25:47 +0000259 CGF.EmitBlock(ContBlock);
Chris Lattner883f6a72007-08-11 00:04:45 +0000260}
Chris Lattneree755f92007-08-21 04:59:27 +0000261
Lauro Ramos Venancio13e22cf2008-02-19 22:04:22 +0000262void AggExprEmitter::EmitNonConstInit(InitListExpr *E) {
263
264 const llvm::PointerType *APType =
265 cast<llvm::PointerType>(DestPtr->getType());
266 const llvm::Type *DestType = APType->getElementType();
Lauro Ramos Venancio305762c2008-02-18 22:44:02 +0000267
268 if (const llvm::ArrayType *AType = dyn_cast<llvm::ArrayType>(DestType)) {
Lauro Ramos Venancio13e22cf2008-02-19 22:04:22 +0000269 unsigned NumInitElements = E->getNumInits();
Lauro Ramos Venancio305762c2008-02-18 22:44:02 +0000270
Lauro Ramos Venancio305762c2008-02-18 22:44:02 +0000271 unsigned i;
272 for (i = 0; i != NumInitElements; ++i) {
Chris Lattner36b6a0a2008-03-19 05:19:41 +0000273 llvm::Value *NextVal = Builder.CreateStructGEP(DestPtr, i, ".array");
Lauro Ramos Venancio13e22cf2008-02-19 22:04:22 +0000274 Expr *Init = E->getInit(i);
275 if (isa<InitListExpr>(Init))
276 CGF.EmitAggExpr(Init, NextVal, VolatileDest);
277 else
278 Builder.CreateStore(CGF.EmitScalarExpr(Init), NextVal);
Lauro Ramos Venancio305762c2008-02-18 22:44:02 +0000279 }
280
281 // Emit remaining default initializers
282 unsigned NumArrayElements = AType->getNumElements();
Lauro Ramos Venancio13e22cf2008-02-19 22:04:22 +0000283 QualType QType = E->getInit(0)->getType();
284 const llvm::Type *EType = AType->getElementType();
Lauro Ramos Venancio305762c2008-02-18 22:44:02 +0000285 for (/*Do not initialize i*/; i < NumArrayElements; ++i) {
Chris Lattner36b6a0a2008-03-19 05:19:41 +0000286 llvm::Value *NextVal = Builder.CreateStructGEP(DestPtr, i, ".array");
Lauro Ramos Venancio13e22cf2008-02-19 22:04:22 +0000287 if (EType->isFirstClassType())
288 Builder.CreateStore(llvm::Constant::getNullValue(EType), NextVal);
289 else
290 EmitAggregateClear(NextVal, QType);
Lauro Ramos Venancio305762c2008-02-18 22:44:02 +0000291 }
Lauro Ramos Venancio13e22cf2008-02-19 22:04:22 +0000292 } else
293 assert(false && "Invalid initializer");
Lauro Ramos Venancio305762c2008-02-18 22:44:02 +0000294}
295
296void AggExprEmitter::VisitInitListExpr(InitListExpr *E) {
Devang Patel636c3d02007-10-26 17:44:44 +0000297
Lauro Ramos Venancio145cd892008-02-19 19:27:31 +0000298 if (E->isConstantExpr(CGF.CGM.getContext(), NULL)) {
299 llvm::Constant *V = CGF.CGM.EmitConstantExpr(E);
Devang Patel636c3d02007-10-26 17:44:44 +0000300 // Create global value to hold this array.
Lauro Ramos Venancio305762c2008-02-18 22:44:02 +0000301 V = new llvm::GlobalVariable(V->getType(), true,
Devang Patel636c3d02007-10-26 17:44:44 +0000302 llvm::GlobalValue::InternalLinkage,
Lauro Ramos Venancio305762c2008-02-18 22:44:02 +0000303 V, ".array",
Devang Patel636c3d02007-10-26 17:44:44 +0000304 &CGF.CGM.getModule());
Lauro Ramos Venancio305762c2008-02-18 22:44:02 +0000305
Devang Patel636c3d02007-10-26 17:44:44 +0000306 EmitAggregateCopy(DestPtr, V , E->getType());
307 return;
Lauro Ramos Venancio145cd892008-02-19 19:27:31 +0000308 } else {
309 if (!E->getType()->isArrayType()) {
310 CGF.WarnUnsupported(E, "aggregate init-list expression");
311 return;
312 }
Lauro Ramos Venancio13e22cf2008-02-19 22:04:22 +0000313 EmitNonConstInit(E);
Lauro Ramos Venancio145cd892008-02-19 19:27:31 +0000314 }
Devang Patel636c3d02007-10-26 17:44:44 +0000315}
316
Chris Lattneree755f92007-08-21 04:59:27 +0000317//===----------------------------------------------------------------------===//
318// Entry Points into this File
319//===----------------------------------------------------------------------===//
320
321/// EmitAggExpr - Emit the computation of the specified expression of
322/// aggregate type. The result is computed into DestPtr. Note that if
323/// DestPtr is null, the value of the aggregate expression is not needed.
324void CodeGenFunction::EmitAggExpr(const Expr *E, llvm::Value *DestPtr,
325 bool VolatileDest) {
326 assert(E && hasAggregateLLVMType(E->getType()) &&
327 "Invalid aggregate expression to emit");
328
329 AggExprEmitter(*this, DestPtr, VolatileDest).Visit(const_cast<Expr*>(E));
330}