blob: 493d2790085a8b8320c618c8db11ec959dc19f3e [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"
16#include "clang/AST/AST.h"
17#include "llvm/Constants.h"
18#include "llvm/Function.h"
Devang Patelddde6d52007-10-26 17:44:44 +000019#include "llvm/GlobalVariable.h"
Chris Lattnerb50e3902007-08-21 04:25:47 +000020#include "llvm/Support/Compiler.h"
Chris Lattner78ed8402007-08-10 20:13:28 +000021using namespace clang;
22using namespace CodeGen;
Chris Lattnerbdb8ffb2007-08-11 00:04:45 +000023
Chris Lattnerb50e3902007-08-21 04:25:47 +000024//===----------------------------------------------------------------------===//
25// Aggregate Expression Emitter
26//===----------------------------------------------------------------------===//
27
28namespace {
29class VISIBILITY_HIDDEN AggExprEmitter : public StmtVisitor<AggExprEmitter> {
30 CodeGenFunction &CGF;
Devang Patel638b64c2007-10-09 19:49:58 +000031 llvm::LLVMFoldingBuilder &Builder;
Chris Lattnerb50e3902007-08-21 04:25:47 +000032 llvm::Value *DestPtr;
33 bool VolatileDest;
34public:
35 AggExprEmitter(CodeGenFunction &cgf, llvm::Value *destPtr, bool volatileDest)
Chris Lattner41b1fca2007-08-26 23:13:56 +000036 : CGF(cgf), Builder(CGF.Builder),
37 DestPtr(destPtr), VolatileDest(volatileDest) {
Chris Lattnerb50e3902007-08-21 04:25:47 +000038 }
39
Chris Lattnera7300102007-08-21 04:59:27 +000040 //===--------------------------------------------------------------------===//
41 // Utilities
42 //===--------------------------------------------------------------------===//
43
Chris Lattnerb50e3902007-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 Lattner41b1fca2007-08-26 23:13:56 +000049 void EmitAggregateCopy(llvm::Value *DestPtr, llvm::Value *SrcPtr,
50 QualType EltTy);
Lauro Ramos Venancio97b09572008-02-19 22:04:22 +000051
52 void EmitAggregateClear(llvm::Value *DestPtr, QualType Ty);
53
54 void EmitNonConstInit(InitListExpr *E);
Chris Lattnera7300102007-08-21 04:59:27 +000055
56 //===--------------------------------------------------------------------===//
57 // Visitor Methods
58 //===--------------------------------------------------------------------===//
59
Chris Lattnerb50e3902007-08-21 04:25:47 +000060 void VisitStmt(Stmt *S) {
Chris Lattnere8f49632007-12-02 01:49:16 +000061 CGF.WarnUnsupported(S, "aggregate expression");
Chris Lattnerb50e3902007-08-21 04:25:47 +000062 }
63 void VisitParenExpr(ParenExpr *PE) { Visit(PE->getSubExpr()); }
64
65 // l-values.
Seo Sanghyeon32666c52007-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 Sanghyeon3c2d5fb2007-12-23 03:11:58 +000069 void VisitStringLiteral(StringLiteral *E) { EmitAggLoadOfLValue(E); }
Seo Sanghyeon32666c52007-12-14 02:04:12 +000070
71 void VisitArraySubscriptExpr(ArraySubscriptExpr *E) {
72 EmitAggLoadOfLValue(E);
73 }
Chris Lattnerb50e3902007-08-21 04:25:47 +000074
75 // Operators.
76 // case Expr::UnaryOperatorClass:
Chris Lattnerb50e3902007-08-21 04:25:47 +000077 // case Expr::CastExprClass:
Anders Carlssonf2712ac2008-01-14 06:28:57 +000078 void VisitImplicitCastExpr(ImplicitCastExpr *E);
Anders Carlsson0ae15412007-10-31 22:04:46 +000079 void VisitCallExpr(const CallExpr *E);
Chris Lattnerbab0bfb2007-08-31 22:54:14 +000080 void VisitStmtExpr(const StmtExpr *E);
Chris Lattnerb50e3902007-08-21 04:25:47 +000081 void VisitBinaryOperator(const BinaryOperator *BO);
Chris Lattner76cb1892007-08-21 04:43:17 +000082 void VisitBinAssign(const BinaryOperator *E);
Nate Begemanc6078c92008-01-31 05:38:29 +000083 void VisitOverloadExpr(const OverloadExpr *E);
Chris Lattnerb50e3902007-08-21 04:25:47 +000084
85
86 void VisitConditionalOperator(const ConditionalOperator *CO);
Devang Patelddde6d52007-10-26 17:44:44 +000087 void VisitInitListExpr(InitListExpr *E);
Chris Lattnerb50e3902007-08-21 04:25:47 +000088 // case Expr::ChooseExprClass:
Lauro Ramos Venancio9eb37a22008-02-18 22:44:02 +000089
Chris Lattnerb50e3902007-08-21 04:25:47 +000090};
91} // end anonymous namespace.
92
Chris Lattnera7300102007-08-21 04:59:27 +000093//===----------------------------------------------------------------------===//
94// Utilities
95//===----------------------------------------------------------------------===//
Chris Lattnerb50e3902007-08-21 04:25:47 +000096
Lauro Ramos Venancio97b09572008-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.
106 std::pair<uint64_t, unsigned> TypeInfo =
107 CGF.getContext().getTypeInfo(Ty, SourceLocation());
108
109 // FIXME: Handle variable sized types.
110 const llvm::Type *IntPtr = llvm::IntegerType::get(CGF.LLVMPointerWidth);
111
112 llvm::Value *MemSetOps[4] = {
113 DestPtr,
114 llvm::ConstantInt::getNullValue(llvm::Type::Int8Ty),
115 // TypeInfo.first describes size in bits.
116 llvm::ConstantInt::get(IntPtr, TypeInfo.first/8),
117 llvm::ConstantInt::get(llvm::Type::Int32Ty, TypeInfo.second/8)
118 };
119
120 Builder.CreateCall(CGF.CGM.getMemSetFn(), MemSetOps, MemSetOps+4);
121}
122
Chris Lattner41b1fca2007-08-26 23:13:56 +0000123void AggExprEmitter::EmitAggregateCopy(llvm::Value *DestPtr,
124 llvm::Value *SrcPtr, QualType Ty) {
125 assert(!Ty->isComplexType() && "Shouldn't happen for complex");
126
127 // Aggregate assignment turns into llvm.memcpy.
Christopher Lamb4fe5e702007-12-17 01:11:20 +0000128 const llvm::Type *BP = llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
Chris Lattner41b1fca2007-08-26 23:13:56 +0000129 if (DestPtr->getType() != BP)
130 DestPtr = Builder.CreateBitCast(DestPtr, BP, "tmp");
131 if (SrcPtr->getType() != BP)
132 SrcPtr = Builder.CreateBitCast(SrcPtr, BP, "tmp");
133
134 // Get size and alignment info for this aggregate.
135 std::pair<uint64_t, unsigned> TypeInfo =
136 CGF.getContext().getTypeInfo(Ty, SourceLocation());
137
138 // FIXME: Handle variable sized types.
139 const llvm::Type *IntPtr = llvm::IntegerType::get(CGF.LLVMPointerWidth);
140
141 llvm::Value *MemCpyOps[4] = {
142 DestPtr, SrcPtr,
Devang Patelddde6d52007-10-26 17:44:44 +0000143 // TypeInfo.first describes size in bits.
144 llvm::ConstantInt::get(IntPtr, TypeInfo.first/8),
Lauro Ramos Venancio97b09572008-02-19 22:04:22 +0000145 llvm::ConstantInt::get(llvm::Type::Int32Ty, TypeInfo.second/8)
Chris Lattner41b1fca2007-08-26 23:13:56 +0000146 };
147
148 Builder.CreateCall(CGF.CGM.getMemCpyFn(), MemCpyOps, MemCpyOps+4);
149}
150
151
Chris Lattnerbdb8ffb2007-08-11 00:04:45 +0000152/// EmitAggLoadOfLValue - Given an expression with aggregate type that
153/// represents a value lvalue, this method emits the address of the lvalue,
154/// then loads the result into DestPtr.
Chris Lattnerb50e3902007-08-21 04:25:47 +0000155void AggExprEmitter::EmitAggLoadOfLValue(const Expr *E) {
156 LValue LV = CGF.EmitLValue(E);
Chris Lattnerbdb8ffb2007-08-11 00:04:45 +0000157 assert(LV.isSimple() && "Can't have aggregate bitfield, vector, etc");
158 llvm::Value *SrcPtr = LV.getAddress();
159
160 // If the result is ignored, don't copy from the value.
161 if (DestPtr == 0)
162 // FIXME: If the source is volatile, we must read from it.
163 return;
164
Chris Lattner41b1fca2007-08-26 23:13:56 +0000165 EmitAggregateCopy(DestPtr, SrcPtr, E->getType());
Chris Lattnerbdb8ffb2007-08-11 00:04:45 +0000166}
167
Chris Lattnera7300102007-08-21 04:59:27 +0000168//===----------------------------------------------------------------------===//
169// Visitor Methods
170//===----------------------------------------------------------------------===//
171
Anders Carlssonf2712ac2008-01-14 06:28:57 +0000172void AggExprEmitter::VisitImplicitCastExpr(ImplicitCastExpr *E)
173{
174 QualType STy = E->getSubExpr()->getType().getCanonicalType();
175 QualType Ty = E->getType().getCanonicalType();
Eli Friedmanf0217122008-02-11 01:09:17 +0000176
177 assert(CGF.getContext().typesAreCompatible(
178 STy.getUnqualifiedType(), Ty.getUnqualifiedType())
179 && "Implicit cast types must be compatible");
Anders Carlssonf2712ac2008-01-14 06:28:57 +0000180
181 Visit(E->getSubExpr());
182}
183
Anders Carlsson0ae15412007-10-31 22:04:46 +0000184void AggExprEmitter::VisitCallExpr(const CallExpr *E)
185{
186 RValue RV = CGF.EmitCallExpr(E);
187 assert(RV.isAggregate() && "Return value must be aggregate value!");
188
189 // If the result is ignored, don't copy from the value.
190 if (DestPtr == 0)
191 // FIXME: If the source is volatile, we must read from it.
192 return;
193
194 EmitAggregateCopy(DestPtr, RV.getAggregateAddr(), E->getType());
195}
196
Nate Begemanc6078c92008-01-31 05:38:29 +0000197void AggExprEmitter::VisitOverloadExpr(const OverloadExpr *E)
198{
199 RValue RV = CGF.EmitCallExpr(E->getFn(), E->arg_begin(),
200 E->getNumArgs(CGF.getContext()));
201 assert(RV.isAggregate() && "Return value must be aggregate value!");
202
203 // If the result is ignored, don't copy from the value.
204 if (DestPtr == 0)
205 // FIXME: If the source is volatile, we must read from it.
206 return;
207
208 EmitAggregateCopy(DestPtr, RV.getAggregateAddr(), E->getType());
209}
210
Chris Lattnerbab0bfb2007-08-31 22:54:14 +0000211void AggExprEmitter::VisitStmtExpr(const StmtExpr *E) {
212 CGF.EmitCompoundStmt(*E->getSubStmt(), true, DestPtr, VolatileDest);
213}
214
Chris Lattnerb50e3902007-08-21 04:25:47 +0000215void AggExprEmitter::VisitBinaryOperator(const BinaryOperator *E) {
Chris Lattnere8f49632007-12-02 01:49:16 +0000216 CGF.WarnUnsupported(E, "aggregate binary expression");
Chris Lattnera7300102007-08-21 04:59:27 +0000217}
218
Chris Lattner76cb1892007-08-21 04:43:17 +0000219void AggExprEmitter::VisitBinAssign(const BinaryOperator *E) {
Eli Friedmanf0217122008-02-11 01:09:17 +0000220 // For an assignment to work, the value on the right has
221 // to be compatible with the value on the left.
222 assert(CGF.getContext().typesAreCompatible(
223 E->getLHS()->getType().getUnqualifiedType(),
224 E->getRHS()->getType().getUnqualifiedType())
225 && "Invalid assignment");
Chris Lattnerb50e3902007-08-21 04:25:47 +0000226 LValue LHS = CGF.EmitLValue(E->getLHS());
Chris Lattnerbdb8ffb2007-08-11 00:04:45 +0000227
228 // Codegen the RHS so that it stores directly into the LHS.
Chris Lattnerb50e3902007-08-21 04:25:47 +0000229 CGF.EmitAggExpr(E->getRHS(), LHS.getAddress(), false /*FIXME: VOLATILE LHS*/);
Chris Lattnerbdb8ffb2007-08-11 00:04:45 +0000230
Eli Friedmanf0217122008-02-11 01:09:17 +0000231 if (DestPtr == 0)
232 return;
233
Chris Lattnerbdb8ffb2007-08-11 00:04:45 +0000234 // If the result of the assignment is used, copy the RHS there also.
Eli Friedmanf0217122008-02-11 01:09:17 +0000235 EmitAggregateCopy(DestPtr, LHS.getAddress(), E->getType());
Chris Lattnerbdb8ffb2007-08-11 00:04:45 +0000236}
237
Chris Lattnerb50e3902007-08-21 04:25:47 +0000238void AggExprEmitter::VisitConditionalOperator(const ConditionalOperator *E) {
Chris Lattnerbdb8ffb2007-08-11 00:04:45 +0000239 llvm::BasicBlock *LHSBlock = new llvm::BasicBlock("cond.?");
240 llvm::BasicBlock *RHSBlock = new llvm::BasicBlock("cond.:");
241 llvm::BasicBlock *ContBlock = new llvm::BasicBlock("cond.cont");
242
Chris Lattnerb50e3902007-08-21 04:25:47 +0000243 llvm::Value *Cond = CGF.EvaluateExprAsBool(E->getCond());
Chris Lattner41b1fca2007-08-26 23:13:56 +0000244 Builder.CreateCondBr(Cond, LHSBlock, RHSBlock);
Chris Lattnerbdb8ffb2007-08-11 00:04:45 +0000245
Chris Lattnerb50e3902007-08-21 04:25:47 +0000246 CGF.EmitBlock(LHSBlock);
Chris Lattnerbdb8ffb2007-08-11 00:04:45 +0000247
248 // Handle the GNU extension for missing LHS.
249 assert(E->getLHS() && "Must have LHS for aggregate value");
250
Chris Lattner55165ba2007-08-21 05:02:10 +0000251 Visit(E->getLHS());
Chris Lattner41b1fca2007-08-26 23:13:56 +0000252 Builder.CreateBr(ContBlock);
253 LHSBlock = Builder.GetInsertBlock();
Chris Lattnerbdb8ffb2007-08-11 00:04:45 +0000254
Chris Lattnerb50e3902007-08-21 04:25:47 +0000255 CGF.EmitBlock(RHSBlock);
Chris Lattnerbdb8ffb2007-08-11 00:04:45 +0000256
Chris Lattner55165ba2007-08-21 05:02:10 +0000257 Visit(E->getRHS());
Chris Lattner41b1fca2007-08-26 23:13:56 +0000258 Builder.CreateBr(ContBlock);
259 RHSBlock = Builder.GetInsertBlock();
Chris Lattnerbdb8ffb2007-08-11 00:04:45 +0000260
Chris Lattnerb50e3902007-08-21 04:25:47 +0000261 CGF.EmitBlock(ContBlock);
Chris Lattnerbdb8ffb2007-08-11 00:04:45 +0000262}
Chris Lattnera7300102007-08-21 04:59:27 +0000263
Lauro Ramos Venancio97b09572008-02-19 22:04:22 +0000264void AggExprEmitter::EmitNonConstInit(InitListExpr *E) {
265
266 const llvm::PointerType *APType =
267 cast<llvm::PointerType>(DestPtr->getType());
268 const llvm::Type *DestType = APType->getElementType();
Lauro Ramos Venancio9eb37a22008-02-18 22:44:02 +0000269
270 if (const llvm::ArrayType *AType = dyn_cast<llvm::ArrayType>(DestType)) {
Lauro Ramos Venancio97b09572008-02-19 22:04:22 +0000271 unsigned NumInitElements = E->getNumInits();
Lauro Ramos Venancio9eb37a22008-02-18 22:44:02 +0000272
273 llvm::Value *Idxs[] = {
274 llvm::Constant::getNullValue(llvm::Type::Int32Ty),
275 NULL
276 };
277 llvm::Value *NextVal = NULL;
278 unsigned i;
279 for (i = 0; i != NumInitElements; ++i) {
280 Idxs[1] = llvm::ConstantInt::get(llvm::Type::Int32Ty, i);
281 NextVal = Builder.CreateGEP(DestPtr, Idxs, Idxs + 2,".array");
Lauro Ramos Venancio97b09572008-02-19 22:04:22 +0000282 Expr *Init = E->getInit(i);
283 if (isa<InitListExpr>(Init))
284 CGF.EmitAggExpr(Init, NextVal, VolatileDest);
285 else
286 Builder.CreateStore(CGF.EmitScalarExpr(Init), NextVal);
Lauro Ramos Venancio9eb37a22008-02-18 22:44:02 +0000287 }
288
289 // Emit remaining default initializers
290 unsigned NumArrayElements = AType->getNumElements();
Lauro Ramos Venancio97b09572008-02-19 22:04:22 +0000291 QualType QType = E->getInit(0)->getType();
292 const llvm::Type *EType = AType->getElementType();
Lauro Ramos Venancio9eb37a22008-02-18 22:44:02 +0000293 for (/*Do not initialize i*/; i < NumArrayElements; ++i) {
294 Idxs[1] = llvm::ConstantInt::get(llvm::Type::Int32Ty, i);
295 NextVal = Builder.CreateGEP(DestPtr, Idxs, Idxs + 2,".array");
Lauro Ramos Venancio97b09572008-02-19 22:04:22 +0000296 if (EType->isFirstClassType())
297 Builder.CreateStore(llvm::Constant::getNullValue(EType), NextVal);
298 else
299 EmitAggregateClear(NextVal, QType);
Lauro Ramos Venancio9eb37a22008-02-18 22:44:02 +0000300 }
Lauro Ramos Venancio97b09572008-02-19 22:04:22 +0000301 } else
302 assert(false && "Invalid initializer");
Lauro Ramos Venancio9eb37a22008-02-18 22:44:02 +0000303}
304
305void AggExprEmitter::VisitInitListExpr(InitListExpr *E) {
Devang Patelddde6d52007-10-26 17:44:44 +0000306
Lauro Ramos Venancioa62b1df2008-02-19 19:27:31 +0000307 if (E->isConstantExpr(CGF.CGM.getContext(), NULL)) {
308 llvm::Constant *V = CGF.CGM.EmitConstantExpr(E);
Devang Patelddde6d52007-10-26 17:44:44 +0000309 // Create global value to hold this array.
Lauro Ramos Venancio9eb37a22008-02-18 22:44:02 +0000310 V = new llvm::GlobalVariable(V->getType(), true,
Devang Patelddde6d52007-10-26 17:44:44 +0000311 llvm::GlobalValue::InternalLinkage,
Lauro Ramos Venancio9eb37a22008-02-18 22:44:02 +0000312 V, ".array",
Devang Patelddde6d52007-10-26 17:44:44 +0000313 &CGF.CGM.getModule());
Lauro Ramos Venancio9eb37a22008-02-18 22:44:02 +0000314
Devang Patelddde6d52007-10-26 17:44:44 +0000315 EmitAggregateCopy(DestPtr, V , E->getType());
316 return;
Lauro Ramos Venancioa62b1df2008-02-19 19:27:31 +0000317 } else {
318 if (!E->getType()->isArrayType()) {
319 CGF.WarnUnsupported(E, "aggregate init-list expression");
320 return;
321 }
Lauro Ramos Venancio97b09572008-02-19 22:04:22 +0000322 EmitNonConstInit(E);
Lauro Ramos Venancioa62b1df2008-02-19 19:27:31 +0000323 }
Devang Patelddde6d52007-10-26 17:44:44 +0000324}
325
Chris Lattnera7300102007-08-21 04:59:27 +0000326//===----------------------------------------------------------------------===//
327// Entry Points into this File
328//===----------------------------------------------------------------------===//
329
330/// EmitAggExpr - Emit the computation of the specified expression of
331/// aggregate type. The result is computed into DestPtr. Note that if
332/// DestPtr is null, the value of the aggregate expression is not needed.
333void CodeGenFunction::EmitAggExpr(const Expr *E, llvm::Value *DestPtr,
334 bool VolatileDest) {
335 assert(E && hasAggregateLLVMType(E->getType()) &&
336 "Invalid aggregate expression to emit");
337
338 AggExprEmitter(*this, DestPtr, VolatileDest).Visit(const_cast<Expr*>(E));
339}