blob: 0324ebf63297b282dfa74aadf71b0afbd49498a9 [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);
Chris Lattneree755f92007-08-21 04:59:27 +000051
52 //===--------------------------------------------------------------------===//
53 // Visitor Methods
54 //===--------------------------------------------------------------------===//
55
Chris Lattner9c033562007-08-21 04:25:47 +000056 void VisitStmt(Stmt *S) {
Chris Lattnerdc4d2802007-12-02 01:49:16 +000057 CGF.WarnUnsupported(S, "aggregate expression");
Chris Lattner9c033562007-08-21 04:25:47 +000058 }
59 void VisitParenExpr(ParenExpr *PE) { Visit(PE->getSubExpr()); }
60
61 // l-values.
Seo Sanghyeon9b73b392007-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 Sanghyeonad6ebd62007-12-23 03:11:58 +000065 void VisitStringLiteral(StringLiteral *E) { EmitAggLoadOfLValue(E); }
Seo Sanghyeon9b73b392007-12-14 02:04:12 +000066
67 void VisitArraySubscriptExpr(ArraySubscriptExpr *E) {
68 EmitAggLoadOfLValue(E);
69 }
Chris Lattner9c033562007-08-21 04:25:47 +000070
71 // Operators.
72 // case Expr::UnaryOperatorClass:
Chris Lattner9c033562007-08-21 04:25:47 +000073 // case Expr::CastExprClass:
Anders Carlssone4707ff2008-01-14 06:28:57 +000074 void VisitImplicitCastExpr(ImplicitCastExpr *E);
Anders Carlsson148fe672007-10-31 22:04:46 +000075 void VisitCallExpr(const CallExpr *E);
Chris Lattnerb2d963f2007-08-31 22:54:14 +000076 void VisitStmtExpr(const StmtExpr *E);
Chris Lattner9c033562007-08-21 04:25:47 +000077 void VisitBinaryOperator(const BinaryOperator *BO);
Chris Lattner03d6fb92007-08-21 04:43:17 +000078 void VisitBinAssign(const BinaryOperator *E);
Nate Begeman796ef3d2008-01-31 05:38:29 +000079 void VisitOverloadExpr(const OverloadExpr *E);
Chris Lattner9c033562007-08-21 04:25:47 +000080
81
82 void VisitConditionalOperator(const ConditionalOperator *CO);
Devang Patel636c3d02007-10-26 17:44:44 +000083 void VisitInitListExpr(InitListExpr *E);
Chris Lattner9c033562007-08-21 04:25:47 +000084 // case Expr::ChooseExprClass:
Lauro Ramos Venancio305762c2008-02-18 22:44:02 +000085
86private:
87
Lauro Ramos Venancio305762c2008-02-18 22:44:02 +000088 void EmitNonConstInit(Expr *E, llvm::Value *Dest, const llvm::Type *DestType);
Chris Lattner9c033562007-08-21 04:25:47 +000089};
90} // end anonymous namespace.
91
Chris Lattneree755f92007-08-21 04:59:27 +000092//===----------------------------------------------------------------------===//
93// Utilities
94//===----------------------------------------------------------------------===//
Chris Lattner9c033562007-08-21 04:25:47 +000095
Chris Lattnerbfc0c1a2007-08-26 23:13:56 +000096void AggExprEmitter::EmitAggregateCopy(llvm::Value *DestPtr,
97 llvm::Value *SrcPtr, QualType Ty) {
98 assert(!Ty->isComplexType() && "Shouldn't happen for complex");
99
100 // Aggregate assignment turns into llvm.memcpy.
Christopher Lambddc23f32007-12-17 01:11:20 +0000101 const llvm::Type *BP = llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
Chris Lattnerbfc0c1a2007-08-26 23:13:56 +0000102 if (DestPtr->getType() != BP)
103 DestPtr = Builder.CreateBitCast(DestPtr, BP, "tmp");
104 if (SrcPtr->getType() != BP)
105 SrcPtr = Builder.CreateBitCast(SrcPtr, BP, "tmp");
106
107 // Get size and alignment info for this aggregate.
108 std::pair<uint64_t, unsigned> TypeInfo =
109 CGF.getContext().getTypeInfo(Ty, SourceLocation());
110
111 // FIXME: Handle variable sized types.
112 const llvm::Type *IntPtr = llvm::IntegerType::get(CGF.LLVMPointerWidth);
113
114 llvm::Value *MemCpyOps[4] = {
115 DestPtr, SrcPtr,
Devang Patel636c3d02007-10-26 17:44:44 +0000116 // TypeInfo.first describes size in bits.
117 llvm::ConstantInt::get(IntPtr, TypeInfo.first/8),
Chris Lattnerbfc0c1a2007-08-26 23:13:56 +0000118 llvm::ConstantInt::get(llvm::Type::Int32Ty, TypeInfo.second)
119 };
120
121 Builder.CreateCall(CGF.CGM.getMemCpyFn(), MemCpyOps, MemCpyOps+4);
122}
123
124
Chris Lattner883f6a72007-08-11 00:04:45 +0000125/// EmitAggLoadOfLValue - Given an expression with aggregate type that
126/// represents a value lvalue, this method emits the address of the lvalue,
127/// then loads the result into DestPtr.
Chris Lattner9c033562007-08-21 04:25:47 +0000128void AggExprEmitter::EmitAggLoadOfLValue(const Expr *E) {
129 LValue LV = CGF.EmitLValue(E);
Chris Lattner883f6a72007-08-11 00:04:45 +0000130 assert(LV.isSimple() && "Can't have aggregate bitfield, vector, etc");
131 llvm::Value *SrcPtr = LV.getAddress();
132
133 // If the result is ignored, don't copy from the value.
134 if (DestPtr == 0)
135 // FIXME: If the source is volatile, we must read from it.
136 return;
137
Chris Lattnerbfc0c1a2007-08-26 23:13:56 +0000138 EmitAggregateCopy(DestPtr, SrcPtr, E->getType());
Chris Lattner883f6a72007-08-11 00:04:45 +0000139}
140
Chris Lattneree755f92007-08-21 04:59:27 +0000141//===----------------------------------------------------------------------===//
142// Visitor Methods
143//===----------------------------------------------------------------------===//
144
Anders Carlssone4707ff2008-01-14 06:28:57 +0000145void AggExprEmitter::VisitImplicitCastExpr(ImplicitCastExpr *E)
146{
147 QualType STy = E->getSubExpr()->getType().getCanonicalType();
148 QualType Ty = E->getType().getCanonicalType();
Eli Friedmanff6e2b72008-02-11 01:09:17 +0000149
150 assert(CGF.getContext().typesAreCompatible(
151 STy.getUnqualifiedType(), Ty.getUnqualifiedType())
152 && "Implicit cast types must be compatible");
Anders Carlssone4707ff2008-01-14 06:28:57 +0000153
154 Visit(E->getSubExpr());
155}
156
Anders Carlsson148fe672007-10-31 22:04:46 +0000157void AggExprEmitter::VisitCallExpr(const CallExpr *E)
158{
159 RValue RV = CGF.EmitCallExpr(E);
160 assert(RV.isAggregate() && "Return value must be aggregate value!");
161
162 // If the result is ignored, don't copy from the value.
163 if (DestPtr == 0)
164 // FIXME: If the source is volatile, we must read from it.
165 return;
166
167 EmitAggregateCopy(DestPtr, RV.getAggregateAddr(), E->getType());
168}
169
Nate Begeman796ef3d2008-01-31 05:38:29 +0000170void AggExprEmitter::VisitOverloadExpr(const OverloadExpr *E)
171{
172 RValue RV = CGF.EmitCallExpr(E->getFn(), E->arg_begin(),
173 E->getNumArgs(CGF.getContext()));
174 assert(RV.isAggregate() && "Return value must be aggregate value!");
175
176 // If the result is ignored, don't copy from the value.
177 if (DestPtr == 0)
178 // FIXME: If the source is volatile, we must read from it.
179 return;
180
181 EmitAggregateCopy(DestPtr, RV.getAggregateAddr(), E->getType());
182}
183
Chris Lattnerb2d963f2007-08-31 22:54:14 +0000184void AggExprEmitter::VisitStmtExpr(const StmtExpr *E) {
185 CGF.EmitCompoundStmt(*E->getSubStmt(), true, DestPtr, VolatileDest);
186}
187
Chris Lattner9c033562007-08-21 04:25:47 +0000188void AggExprEmitter::VisitBinaryOperator(const BinaryOperator *E) {
Chris Lattnerdc4d2802007-12-02 01:49:16 +0000189 CGF.WarnUnsupported(E, "aggregate binary expression");
Chris Lattneree755f92007-08-21 04:59:27 +0000190}
191
Chris Lattner03d6fb92007-08-21 04:43:17 +0000192void AggExprEmitter::VisitBinAssign(const BinaryOperator *E) {
Eli Friedmanff6e2b72008-02-11 01:09:17 +0000193 // For an assignment to work, the value on the right has
194 // to be compatible with the value on the left.
195 assert(CGF.getContext().typesAreCompatible(
196 E->getLHS()->getType().getUnqualifiedType(),
197 E->getRHS()->getType().getUnqualifiedType())
198 && "Invalid assignment");
Chris Lattner9c033562007-08-21 04:25:47 +0000199 LValue LHS = CGF.EmitLValue(E->getLHS());
Chris Lattner883f6a72007-08-11 00:04:45 +0000200
201 // Codegen the RHS so that it stores directly into the LHS.
Chris Lattner9c033562007-08-21 04:25:47 +0000202 CGF.EmitAggExpr(E->getRHS(), LHS.getAddress(), false /*FIXME: VOLATILE LHS*/);
Chris Lattner883f6a72007-08-11 00:04:45 +0000203
Eli Friedmanff6e2b72008-02-11 01:09:17 +0000204 if (DestPtr == 0)
205 return;
206
Chris Lattner883f6a72007-08-11 00:04:45 +0000207 // If the result of the assignment is used, copy the RHS there also.
Eli Friedmanff6e2b72008-02-11 01:09:17 +0000208 EmitAggregateCopy(DestPtr, LHS.getAddress(), E->getType());
Chris Lattner883f6a72007-08-11 00:04:45 +0000209}
210
Chris Lattner9c033562007-08-21 04:25:47 +0000211void AggExprEmitter::VisitConditionalOperator(const ConditionalOperator *E) {
Chris Lattner883f6a72007-08-11 00:04:45 +0000212 llvm::BasicBlock *LHSBlock = new llvm::BasicBlock("cond.?");
213 llvm::BasicBlock *RHSBlock = new llvm::BasicBlock("cond.:");
214 llvm::BasicBlock *ContBlock = new llvm::BasicBlock("cond.cont");
215
Chris Lattner9c033562007-08-21 04:25:47 +0000216 llvm::Value *Cond = CGF.EvaluateExprAsBool(E->getCond());
Chris Lattnerbfc0c1a2007-08-26 23:13:56 +0000217 Builder.CreateCondBr(Cond, LHSBlock, RHSBlock);
Chris Lattner883f6a72007-08-11 00:04:45 +0000218
Chris Lattner9c033562007-08-21 04:25:47 +0000219 CGF.EmitBlock(LHSBlock);
Chris Lattner883f6a72007-08-11 00:04:45 +0000220
221 // Handle the GNU extension for missing LHS.
222 assert(E->getLHS() && "Must have LHS for aggregate value");
223
Chris Lattnerc748f272007-08-21 05:02:10 +0000224 Visit(E->getLHS());
Chris Lattnerbfc0c1a2007-08-26 23:13:56 +0000225 Builder.CreateBr(ContBlock);
226 LHSBlock = Builder.GetInsertBlock();
Chris Lattner883f6a72007-08-11 00:04:45 +0000227
Chris Lattner9c033562007-08-21 04:25:47 +0000228 CGF.EmitBlock(RHSBlock);
Chris Lattner883f6a72007-08-11 00:04:45 +0000229
Chris Lattnerc748f272007-08-21 05:02:10 +0000230 Visit(E->getRHS());
Chris Lattnerbfc0c1a2007-08-26 23:13:56 +0000231 Builder.CreateBr(ContBlock);
232 RHSBlock = Builder.GetInsertBlock();
Chris Lattner883f6a72007-08-11 00:04:45 +0000233
Chris Lattner9c033562007-08-21 04:25:47 +0000234 CGF.EmitBlock(ContBlock);
Chris Lattner883f6a72007-08-11 00:04:45 +0000235}
Chris Lattneree755f92007-08-21 04:59:27 +0000236
Lauro Ramos Venancio305762c2008-02-18 22:44:02 +0000237void AggExprEmitter::EmitNonConstInit(Expr *E, llvm::Value *DestPtr,
238 const llvm::Type *DestType) {
239
240 if (const llvm::ArrayType *AType = dyn_cast<llvm::ArrayType>(DestType)) {
241 unsigned NumInitElements = 0;
242 InitListExpr *InitList = NULL;
243
244 if (E) {
245 InitList = cast<InitListExpr>(E);
246 NumInitElements = InitList->getNumInits();
247 }
248
249 llvm::Value *Idxs[] = {
250 llvm::Constant::getNullValue(llvm::Type::Int32Ty),
251 NULL
252 };
253 llvm::Value *NextVal = NULL;
254 unsigned i;
255 for (i = 0; i != NumInitElements; ++i) {
256 Idxs[1] = llvm::ConstantInt::get(llvm::Type::Int32Ty, i);
257 NextVal = Builder.CreateGEP(DestPtr, Idxs, Idxs + 2,".array");
258 EmitNonConstInit(InitList->getInit(i), NextVal, AType->getElementType());
259 }
260
261 // Emit remaining default initializers
262 unsigned NumArrayElements = AType->getNumElements();
263 for (/*Do not initialize i*/; i < NumArrayElements; ++i) {
264 Idxs[1] = llvm::ConstantInt::get(llvm::Type::Int32Ty, i);
265 NextVal = Builder.CreateGEP(DestPtr, Idxs, Idxs + 2,".array");
266 EmitNonConstInit(NULL, NextVal, AType->getElementType());
267 }
268
269 } else {
270 llvm::Value *V;
271 if (E)
272 V = CGF.EmitScalarExpr(E);
273 else
274 V = llvm::Constant::getNullValue(DestType);
275 Builder.CreateStore(V, DestPtr);
276 }
277}
278
279void AggExprEmitter::VisitInitListExpr(InitListExpr *E) {
Devang Patel636c3d02007-10-26 17:44:44 +0000280
Lauro Ramos Venancio145cd892008-02-19 19:27:31 +0000281 if (E->isConstantExpr(CGF.CGM.getContext(), NULL)) {
282 llvm::Constant *V = CGF.CGM.EmitConstantExpr(E);
Devang Patel636c3d02007-10-26 17:44:44 +0000283 // Create global value to hold this array.
Lauro Ramos Venancio305762c2008-02-18 22:44:02 +0000284 V = new llvm::GlobalVariable(V->getType(), true,
Devang Patel636c3d02007-10-26 17:44:44 +0000285 llvm::GlobalValue::InternalLinkage,
Lauro Ramos Venancio305762c2008-02-18 22:44:02 +0000286 V, ".array",
Devang Patel636c3d02007-10-26 17:44:44 +0000287 &CGF.CGM.getModule());
Lauro Ramos Venancio305762c2008-02-18 22:44:02 +0000288
Devang Patel636c3d02007-10-26 17:44:44 +0000289 EmitAggregateCopy(DestPtr, V , E->getType());
290 return;
Lauro Ramos Venancio145cd892008-02-19 19:27:31 +0000291 } else {
292 if (!E->getType()->isArrayType()) {
293 CGF.WarnUnsupported(E, "aggregate init-list expression");
294 return;
295 }
296
297 const llvm::PointerType *APType =
298 cast<llvm::PointerType>(DestPtr->getType());
299 const llvm::ArrayType *AType =
300 cast<llvm::ArrayType>(APType->getElementType());
301
Lauro Ramos Venancio305762c2008-02-18 22:44:02 +0000302 EmitNonConstInit(E, DestPtr, AType);
Lauro Ramos Venancio145cd892008-02-19 19:27:31 +0000303 }
Devang Patel636c3d02007-10-26 17:44:44 +0000304}
305
Chris Lattneree755f92007-08-21 04:59:27 +0000306//===----------------------------------------------------------------------===//
307// Entry Points into this File
308//===----------------------------------------------------------------------===//
309
310/// EmitAggExpr - Emit the computation of the specified expression of
311/// aggregate type. The result is computed into DestPtr. Note that if
312/// DestPtr is null, the value of the aggregate expression is not needed.
313void CodeGenFunction::EmitAggExpr(const Expr *E, llvm::Value *DestPtr,
314 bool VolatileDest) {
315 assert(E && hasAggregateLLVMType(E->getType()) &&
316 "Invalid aggregate expression to emit");
317
318 AggExprEmitter(*this, DestPtr, VolatileDest).Visit(const_cast<Expr*>(E));
319}