blob: 5f353edcf0bb1328a97716076b6d1e9077b249b2 [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//
5// This file was developed by Chris Lattner and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
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) {
57 fprintf(stderr, "Unimplemented agg expr!\n");
Chris Lattner419ea7e2007-09-13 01:17:29 +000058 S->dump(CGF.getContext().SourceMgr);
Chris Lattner9c033562007-08-21 04:25:47 +000059 }
60 void VisitParenExpr(ParenExpr *PE) { Visit(PE->getSubExpr()); }
61
62 // l-values.
63 void VisitDeclRefExpr(DeclRefExpr *DRE) { return EmitAggLoadOfLValue(DRE); }
64 // case Expr::ArraySubscriptExprClass:
65
66 // Operators.
67 // case Expr::UnaryOperatorClass:
68 // case Expr::ImplicitCastExprClass:
69 // case Expr::CastExprClass:
70 // case Expr::CallExprClass:
Chris Lattnerb2d963f2007-08-31 22:54:14 +000071 void VisitStmtExpr(const StmtExpr *E);
Chris Lattner9c033562007-08-21 04:25:47 +000072 void VisitBinaryOperator(const BinaryOperator *BO);
Chris Lattner03d6fb92007-08-21 04:43:17 +000073 void VisitBinAssign(const BinaryOperator *E);
Chris Lattner9c033562007-08-21 04:25:47 +000074
75
76 void VisitConditionalOperator(const ConditionalOperator *CO);
Devang Patel636c3d02007-10-26 17:44:44 +000077 void VisitInitListExpr(InitListExpr *E);
Chris Lattner9c033562007-08-21 04:25:47 +000078 // case Expr::ChooseExprClass:
79};
80} // end anonymous namespace.
81
Chris Lattneree755f92007-08-21 04:59:27 +000082//===----------------------------------------------------------------------===//
83// Utilities
84//===----------------------------------------------------------------------===//
Chris Lattner9c033562007-08-21 04:25:47 +000085
Chris Lattnerbfc0c1a2007-08-26 23:13:56 +000086void AggExprEmitter::EmitAggregateCopy(llvm::Value *DestPtr,
87 llvm::Value *SrcPtr, QualType Ty) {
88 assert(!Ty->isComplexType() && "Shouldn't happen for complex");
89
90 // Aggregate assignment turns into llvm.memcpy.
91 const llvm::Type *BP = llvm::PointerType::get(llvm::Type::Int8Ty);
92 if (DestPtr->getType() != BP)
93 DestPtr = Builder.CreateBitCast(DestPtr, BP, "tmp");
94 if (SrcPtr->getType() != BP)
95 SrcPtr = Builder.CreateBitCast(SrcPtr, BP, "tmp");
96
97 // Get size and alignment info for this aggregate.
98 std::pair<uint64_t, unsigned> TypeInfo =
99 CGF.getContext().getTypeInfo(Ty, SourceLocation());
100
101 // FIXME: Handle variable sized types.
102 const llvm::Type *IntPtr = llvm::IntegerType::get(CGF.LLVMPointerWidth);
103
104 llvm::Value *MemCpyOps[4] = {
105 DestPtr, SrcPtr,
Devang Patel636c3d02007-10-26 17:44:44 +0000106 // TypeInfo.first describes size in bits.
107 llvm::ConstantInt::get(IntPtr, TypeInfo.first/8),
Chris Lattnerbfc0c1a2007-08-26 23:13:56 +0000108 llvm::ConstantInt::get(llvm::Type::Int32Ty, TypeInfo.second)
109 };
110
111 Builder.CreateCall(CGF.CGM.getMemCpyFn(), MemCpyOps, MemCpyOps+4);
112}
113
114
Chris Lattner883f6a72007-08-11 00:04:45 +0000115/// EmitAggLoadOfLValue - Given an expression with aggregate type that
116/// represents a value lvalue, this method emits the address of the lvalue,
117/// then loads the result into DestPtr.
Chris Lattner9c033562007-08-21 04:25:47 +0000118void AggExprEmitter::EmitAggLoadOfLValue(const Expr *E) {
119 LValue LV = CGF.EmitLValue(E);
Chris Lattner883f6a72007-08-11 00:04:45 +0000120 assert(LV.isSimple() && "Can't have aggregate bitfield, vector, etc");
121 llvm::Value *SrcPtr = LV.getAddress();
122
123 // If the result is ignored, don't copy from the value.
124 if (DestPtr == 0)
125 // FIXME: If the source is volatile, we must read from it.
126 return;
127
Chris Lattnerbfc0c1a2007-08-26 23:13:56 +0000128 EmitAggregateCopy(DestPtr, SrcPtr, E->getType());
Chris Lattner883f6a72007-08-11 00:04:45 +0000129}
130
Chris Lattneree755f92007-08-21 04:59:27 +0000131//===----------------------------------------------------------------------===//
132// Visitor Methods
133//===----------------------------------------------------------------------===//
134
Chris Lattnerb2d963f2007-08-31 22:54:14 +0000135void AggExprEmitter::VisitStmtExpr(const StmtExpr *E) {
136 CGF.EmitCompoundStmt(*E->getSubStmt(), true, DestPtr, VolatileDest);
137}
138
Chris Lattner9c033562007-08-21 04:25:47 +0000139void AggExprEmitter::VisitBinaryOperator(const BinaryOperator *E) {
Chris Lattner03d6fb92007-08-21 04:43:17 +0000140 fprintf(stderr, "Unimplemented aggregate binary expr!\n");
Chris Lattner419ea7e2007-09-13 01:17:29 +0000141 E->dump(CGF.getContext().SourceMgr);
Chris Lattneree755f92007-08-21 04:59:27 +0000142}
143
Chris Lattner03d6fb92007-08-21 04:43:17 +0000144void AggExprEmitter::VisitBinAssign(const BinaryOperator *E) {
Chris Lattner883f6a72007-08-11 00:04:45 +0000145 assert(E->getLHS()->getType().getCanonicalType() ==
146 E->getRHS()->getType().getCanonicalType() && "Invalid assignment");
Chris Lattner9c033562007-08-21 04:25:47 +0000147 LValue LHS = CGF.EmitLValue(E->getLHS());
Chris Lattner883f6a72007-08-11 00:04:45 +0000148
149 // Codegen the RHS so that it stores directly into the LHS.
Chris Lattner9c033562007-08-21 04:25:47 +0000150 CGF.EmitAggExpr(E->getRHS(), LHS.getAddress(), false /*FIXME: VOLATILE LHS*/);
Chris Lattner883f6a72007-08-11 00:04:45 +0000151
152 // If the result of the assignment is used, copy the RHS there also.
153 if (DestPtr) {
154 assert(0 && "FIXME: Chained agg assignment not implemented yet");
155 }
156}
157
Chris Lattner9c033562007-08-21 04:25:47 +0000158void AggExprEmitter::VisitConditionalOperator(const ConditionalOperator *E) {
Chris Lattner883f6a72007-08-11 00:04:45 +0000159 llvm::BasicBlock *LHSBlock = new llvm::BasicBlock("cond.?");
160 llvm::BasicBlock *RHSBlock = new llvm::BasicBlock("cond.:");
161 llvm::BasicBlock *ContBlock = new llvm::BasicBlock("cond.cont");
162
Chris Lattner9c033562007-08-21 04:25:47 +0000163 llvm::Value *Cond = CGF.EvaluateExprAsBool(E->getCond());
Chris Lattnerbfc0c1a2007-08-26 23:13:56 +0000164 Builder.CreateCondBr(Cond, LHSBlock, RHSBlock);
Chris Lattner883f6a72007-08-11 00:04:45 +0000165
Chris Lattner9c033562007-08-21 04:25:47 +0000166 CGF.EmitBlock(LHSBlock);
Chris Lattner883f6a72007-08-11 00:04:45 +0000167
168 // Handle the GNU extension for missing LHS.
169 assert(E->getLHS() && "Must have LHS for aggregate value");
170
Chris Lattnerc748f272007-08-21 05:02:10 +0000171 Visit(E->getLHS());
Chris Lattnerbfc0c1a2007-08-26 23:13:56 +0000172 Builder.CreateBr(ContBlock);
173 LHSBlock = Builder.GetInsertBlock();
Chris Lattner883f6a72007-08-11 00:04:45 +0000174
Chris Lattner9c033562007-08-21 04:25:47 +0000175 CGF.EmitBlock(RHSBlock);
Chris Lattner883f6a72007-08-11 00:04:45 +0000176
Chris Lattnerc748f272007-08-21 05:02:10 +0000177 Visit(E->getRHS());
Chris Lattnerbfc0c1a2007-08-26 23:13:56 +0000178 Builder.CreateBr(ContBlock);
179 RHSBlock = Builder.GetInsertBlock();
Chris Lattner883f6a72007-08-11 00:04:45 +0000180
Chris Lattner9c033562007-08-21 04:25:47 +0000181 CGF.EmitBlock(ContBlock);
Chris Lattner883f6a72007-08-11 00:04:45 +0000182}
Chris Lattneree755f92007-08-21 04:59:27 +0000183
Devang Patel636c3d02007-10-26 17:44:44 +0000184void AggExprEmitter::VisitInitListExpr(InitListExpr *E) {
185
186 unsigned NumInitElements = E->getNumInits();
187
188 assert ( E->getType()->isArrayType()
189 && "Only Array initializers are supported");
190
191 std::vector<llvm::Constant*> ArrayElts;
192 const llvm::PointerType *APType = cast<llvm::PointerType>(DestPtr->getType());
193 const llvm::ArrayType *AType = cast<llvm::ArrayType>(APType->getElementType());
194
195 // Copy initializer elements.
196 bool AllConstElements = true;
197 unsigned i = 0;
198 for (i = 0; i < NumInitElements; ++i) {
199 if (llvm::Constant *C = dyn_cast<llvm::Constant>(CGF.EmitScalarExpr(E->getInit(i))))
200 ArrayElts.push_back(C);
201 else {
202 AllConstElements = false;
203 break;
204 }
205 }
206
207 unsigned NumArrayElements = AType->getNumElements();
208 const llvm::Type *ElementType = CGF.ConvertType(E->getInit(0)->getType());
209
210 if (AllConstElements) {
211 // Initialize remaining array elements.
212 for (/*Do not initialize i*/; i < NumArrayElements; ++i)
213 ArrayElts.push_back(llvm::Constant::getNullValue(ElementType));
214
215 // Create global value to hold this array.
216 llvm::Constant *V = llvm::ConstantArray::get(AType, ArrayElts);
217 V = new llvm::GlobalVariable(V->getType(), true,
218 llvm::GlobalValue::InternalLinkage,
219 V, ".array",
220 &CGF.CGM.getModule());
221
222 EmitAggregateCopy(DestPtr, V , E->getType());
223 return;
224 }
225
226 // Emit indiviudal array element stores.
227 unsigned index = 0;
228 llvm::Value *NextVal = NULL;
229 llvm::Value *Idxs[] = {
230 llvm::Constant::getNullValue(llvm::Type::Int32Ty),
231 NULL
232 };
233
234 // Emit already seen constants initializers.
235 for (i = 0; i < ArrayElts.size(); i++) {
236 Idxs[1] = llvm::ConstantInt::get(llvm::Type::Int32Ty, index++);
237 NextVal = Builder.CreateGEP(DestPtr, Idxs, Idxs + 2, ".array");
238 Builder.CreateStore(ArrayElts[i], NextVal);
239 }
240
241 // Emit remaining initializers
242 for (/*Do not initizalize i*/; i < NumInitElements; ++i) {
243 Idxs[1] = llvm::ConstantInt::get(llvm::Type::Int32Ty, index++);
244 NextVal = Builder.CreateGEP(DestPtr, Idxs, Idxs + 2, ".array");
245 llvm::Value *V = CGF.EmitScalarExpr(E->getInit(i));
246 Builder.CreateStore(V, NextVal);
247 }
248
249 // Emit remaining default initializers
250 for (/*Do not initialize i*/; i < NumArrayElements; ++i) {
251 Idxs[1] = llvm::ConstantInt::get(llvm::Type::Int32Ty, index++);
252 NextVal = Builder.CreateGEP(DestPtr, Idxs, Idxs + 2, ".array");
253 Builder.CreateStore(llvm::Constant::getNullValue(ElementType), NextVal);
254 }
255}
256
Chris Lattneree755f92007-08-21 04:59:27 +0000257//===----------------------------------------------------------------------===//
258// Entry Points into this File
259//===----------------------------------------------------------------------===//
260
261/// EmitAggExpr - Emit the computation of the specified expression of
262/// aggregate type. The result is computed into DestPtr. Note that if
263/// DestPtr is null, the value of the aggregate expression is not needed.
264void CodeGenFunction::EmitAggExpr(const Expr *E, llvm::Value *DestPtr,
265 bool VolatileDest) {
266 assert(E && hasAggregateLLVMType(E->getType()) &&
267 "Invalid aggregate expression to emit");
268
269 AggExprEmitter(*this, DestPtr, VolatileDest).Visit(const_cast<Expr*>(E));
270}