blob: 93f3d71eafaf3edf60454410032520c6d02f7a0c [file] [log] [blame]
Chris Lattner4b009652007-07-25 00:24:17 +00001//===--- CGExpr.cpp - Emit LLVM Code from Expressions ---------------------===//
2//
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 Expr nodes as LLVM code.
11//
12//===----------------------------------------------------------------------===//
13
14#include "CodeGenFunction.h"
15#include "CodeGenModule.h"
16#include "clang/AST/AST.h"
17#include "llvm/Constants.h"
18#include "llvm/DerivedTypes.h"
19#include "llvm/Function.h"
20#include "llvm/GlobalVariable.h"
21#include "llvm/Support/MathExtras.h"
22using namespace clang;
23using namespace CodeGen;
24
25//===--------------------------------------------------------------------===//
26// Miscellaneous Helper Methods
27//===--------------------------------------------------------------------===//
28
29/// CreateTempAlloca - This creates a alloca and inserts it into the entry
30/// block.
31llvm::AllocaInst *CodeGenFunction::CreateTempAlloca(const llvm::Type *Ty,
32 const char *Name) {
33 return new llvm::AllocaInst(Ty, 0, Name, AllocaInsertPt);
34}
35
36/// EvaluateExprAsBool - Perform the usual unary conversions on the specified
37/// expression and compare the result against zero, returning an Int1Ty value.
38llvm::Value *CodeGenFunction::EvaluateExprAsBool(const Expr *E) {
Chris Lattnercc50a512007-08-26 16:46:58 +000039 QualType BoolTy = getContext().BoolTy;
40 if (!E->getType()->isComplexType())
41 return EmitScalarConversion(EmitScalarExpr(E), E->getType(), BoolTy);
Chris Lattner4b009652007-07-25 00:24:17 +000042
Chris Lattnercc50a512007-08-26 16:46:58 +000043 return EmitComplexToScalarConversion(EmitComplexExpr(E), E->getType(),BoolTy);
Chris Lattner4b009652007-07-25 00:24:17 +000044}
45
Chris Lattnere24c4cf2007-08-31 22:49:20 +000046/// EmitAnyExpr - Emit code to compute the specified expression which can have
47/// any type. The result is returned as an RValue struct. If this is an
48/// aggregate expression, the aggloc/agglocvolatile arguments indicate where
49/// the result should be returned.
50RValue CodeGenFunction::EmitAnyExpr(const Expr *E, llvm::Value *AggLoc,
51 bool isAggLocVolatile) {
52 if (!hasAggregateLLVMType(E->getType()))
53 return RValue::get(EmitScalarExpr(E));
54 else if (E->getType()->isComplexType())
55 return RValue::getComplex(EmitComplexExpr(E));
56
57 EmitAggExpr(E, AggLoc, isAggLocVolatile);
58 return RValue::getAggregate(AggLoc);
59}
60
61
Chris Lattner4b009652007-07-25 00:24:17 +000062//===----------------------------------------------------------------------===//
63// LValue Expression Emission
64//===----------------------------------------------------------------------===//
65
66/// EmitLValue - Emit code to compute a designator that specifies the location
67/// of the expression.
68///
69/// This can return one of two things: a simple address or a bitfield
70/// reference. In either case, the LLVM Value* in the LValue structure is
71/// guaranteed to be an LLVM pointer type.
72///
73/// If this returns a bitfield reference, nothing about the pointee type of
74/// the LLVM value is known: For example, it may not be a pointer to an
75/// integer.
76///
77/// If this returns a normal address, and if the lvalue's C type is fixed
78/// size, this method guarantees that the returned pointer type will point to
79/// an LLVM type of the same size of the lvalue's type. If the lvalue has a
80/// variable length type, this is not possible.
81///
82LValue CodeGenFunction::EmitLValue(const Expr *E) {
83 switch (E->getStmtClass()) {
Chris Lattnera52c8892007-08-26 05:06:40 +000084 default: {
Chris Lattnere8f49632007-12-02 01:49:16 +000085 WarnUnsupported(E, "l-value expression");
Chris Lattnera52c8892007-08-26 05:06:40 +000086 llvm::Type *Ty = llvm::PointerType::get(ConvertType(E->getType()));
87 return LValue::MakeAddr(llvm::UndefValue::get(Ty));
88 }
Chris Lattner4b009652007-07-25 00:24:17 +000089
90 case Expr::DeclRefExprClass: return EmitDeclRefLValue(cast<DeclRefExpr>(E));
91 case Expr::ParenExprClass:return EmitLValue(cast<ParenExpr>(E)->getSubExpr());
92 case Expr::PreDefinedExprClass:
93 return EmitPreDefinedLValue(cast<PreDefinedExpr>(E));
94 case Expr::StringLiteralClass:
95 return EmitStringLiteralLValue(cast<StringLiteral>(E));
96
97 case Expr::UnaryOperatorClass:
98 return EmitUnaryOpLValue(cast<UnaryOperator>(E));
99 case Expr::ArraySubscriptExprClass:
100 return EmitArraySubscriptExpr(cast<ArraySubscriptExpr>(E));
Chris Lattnera0d03a72007-08-03 17:31:20 +0000101 case Expr::OCUVectorElementExprClass:
102 return EmitOCUVectorElementExpr(cast<OCUVectorElementExpr>(E));
Devang Patel41b66252007-10-23 20:28:39 +0000103 case Expr::MemberExprClass: return EmitMemberExpr(cast<MemberExpr>(E));
Chris Lattner4b009652007-07-25 00:24:17 +0000104 }
105}
106
107/// EmitLoadOfLValue - Given an expression that represents a value lvalue,
108/// this method emits the address of the lvalue, then loads the result as an
109/// rvalue, returning the rvalue.
110RValue CodeGenFunction::EmitLoadOfLValue(LValue LV, QualType ExprType) {
Chris Lattner4b009652007-07-25 00:24:17 +0000111 if (LV.isSimple()) {
112 llvm::Value *Ptr = LV.getAddress();
113 const llvm::Type *EltTy =
114 cast<llvm::PointerType>(Ptr->getType())->getElementType();
115
116 // Simple scalar l-value.
117 if (EltTy->isFirstClassType())
118 return RValue::get(Builder.CreateLoad(Ptr, "tmp"));
119
Chris Lattnerbdb8ffb2007-08-11 00:04:45 +0000120 assert(ExprType->isFunctionType() && "Unknown scalar value");
121 return RValue::get(Ptr);
Chris Lattner4b009652007-07-25 00:24:17 +0000122 }
123
124 if (LV.isVectorElt()) {
125 llvm::Value *Vec = Builder.CreateLoad(LV.getVectorAddr(), "tmp");
126 return RValue::get(Builder.CreateExtractElement(Vec, LV.getVectorIdx(),
127 "vecext"));
128 }
Chris Lattnera735fac2007-08-03 00:16:29 +0000129
130 // If this is a reference to a subset of the elements of a vector, either
131 // shuffle the input or extract/insert them as appropriate.
Chris Lattnera0d03a72007-08-03 17:31:20 +0000132 if (LV.isOCUVectorElt())
133 return EmitLoadOfOCUElementLValue(LV, ExprType);
Chris Lattner4b009652007-07-25 00:24:17 +0000134
135 assert(0 && "Bitfield ref not impl!");
Chris Lattner1d2b4612007-09-16 19:23:47 +0000136 //an invalid RValue, but the assert will
137 //ensure that this point is never reached
138 return RValue();
Chris Lattner4b009652007-07-25 00:24:17 +0000139}
140
Chris Lattner944f7962007-08-03 16:18:34 +0000141// If this is a reference to a subset of the elements of a vector, either
142// shuffle the input or extract/insert them as appropriate.
Chris Lattnera0d03a72007-08-03 17:31:20 +0000143RValue CodeGenFunction::EmitLoadOfOCUElementLValue(LValue LV,
Chris Lattner4b492962007-08-10 17:10:08 +0000144 QualType ExprType) {
Chris Lattner944f7962007-08-03 16:18:34 +0000145 llvm::Value *Vec = Builder.CreateLoad(LV.getOCUVectorAddr(), "tmp");
146
Chris Lattnera0d03a72007-08-03 17:31:20 +0000147 unsigned EncFields = LV.getOCUVectorElts();
Chris Lattner944f7962007-08-03 16:18:34 +0000148
149 // If the result of the expression is a non-vector type, we must be
150 // extracting a single element. Just codegen as an extractelement.
Chris Lattner4b492962007-08-10 17:10:08 +0000151 const VectorType *ExprVT = ExprType->getAsVectorType();
152 if (!ExprVT) {
Chris Lattnera0d03a72007-08-03 17:31:20 +0000153 unsigned InIdx = OCUVectorElementExpr::getAccessedFieldNo(0, EncFields);
Chris Lattner944f7962007-08-03 16:18:34 +0000154 llvm::Value *Elt = llvm::ConstantInt::get(llvm::Type::Int32Ty, InIdx);
155 return RValue::get(Builder.CreateExtractElement(Vec, Elt, "tmp"));
156 }
157
158 // If the source and destination have the same number of elements, use a
159 // vector shuffle instead of insert/extracts.
Chris Lattner4b492962007-08-10 17:10:08 +0000160 unsigned NumResultElts = ExprVT->getNumElements();
Chris Lattner944f7962007-08-03 16:18:34 +0000161 unsigned NumSourceElts =
162 cast<llvm::VectorType>(Vec->getType())->getNumElements();
163
164 if (NumResultElts == NumSourceElts) {
165 llvm::SmallVector<llvm::Constant*, 4> Mask;
166 for (unsigned i = 0; i != NumResultElts; ++i) {
Chris Lattnera0d03a72007-08-03 17:31:20 +0000167 unsigned InIdx = OCUVectorElementExpr::getAccessedFieldNo(i, EncFields);
Chris Lattner944f7962007-08-03 16:18:34 +0000168 Mask.push_back(llvm::ConstantInt::get(llvm::Type::Int32Ty, InIdx));
169 }
170
171 llvm::Value *MaskV = llvm::ConstantVector::get(&Mask[0], Mask.size());
172 Vec = Builder.CreateShuffleVector(Vec,
173 llvm::UndefValue::get(Vec->getType()),
174 MaskV, "tmp");
175 return RValue::get(Vec);
176 }
177
178 // Start out with an undef of the result type.
179 llvm::Value *Result = llvm::UndefValue::get(ConvertType(ExprType));
180
181 // Extract/Insert each element of the result.
182 for (unsigned i = 0; i != NumResultElts; ++i) {
Chris Lattnera0d03a72007-08-03 17:31:20 +0000183 unsigned InIdx = OCUVectorElementExpr::getAccessedFieldNo(i, EncFields);
Chris Lattner944f7962007-08-03 16:18:34 +0000184 llvm::Value *Elt = llvm::ConstantInt::get(llvm::Type::Int32Ty, InIdx);
185 Elt = Builder.CreateExtractElement(Vec, Elt, "tmp");
186
187 llvm::Value *OutIdx = llvm::ConstantInt::get(llvm::Type::Int32Ty, i);
188 Result = Builder.CreateInsertElement(Result, Elt, OutIdx, "tmp");
189 }
190
191 return RValue::get(Result);
192}
193
194
Chris Lattner4b009652007-07-25 00:24:17 +0000195
196/// EmitStoreThroughLValue - Store the specified rvalue into the specified
197/// lvalue, where both are guaranteed to the have the same type, and that type
198/// is 'Ty'.
199void CodeGenFunction::EmitStoreThroughLValue(RValue Src, LValue Dst,
200 QualType Ty) {
Chris Lattner5bfdd232007-08-03 16:28:33 +0000201 if (!Dst.isSimple()) {
202 if (Dst.isVectorElt()) {
203 // Read/modify/write the vector, inserting the new element.
204 // FIXME: Volatility.
205 llvm::Value *Vec = Builder.CreateLoad(Dst.getVectorAddr(), "tmp");
Chris Lattnere24c4cf2007-08-31 22:49:20 +0000206 Vec = Builder.CreateInsertElement(Vec, Src.getScalarVal(),
Chris Lattner5bfdd232007-08-03 16:28:33 +0000207 Dst.getVectorIdx(), "vecins");
208 Builder.CreateStore(Vec, Dst.getVectorAddr());
209 return;
210 }
Chris Lattner4b009652007-07-25 00:24:17 +0000211
Chris Lattner5bfdd232007-08-03 16:28:33 +0000212 // If this is an update of elements of a vector, insert them as appropriate.
Chris Lattnera0d03a72007-08-03 17:31:20 +0000213 if (Dst.isOCUVectorElt())
Chris Lattner5bfdd232007-08-03 16:28:33 +0000214 return EmitStoreThroughOCUComponentLValue(Src, Dst, Ty);
215
216 assert(0 && "FIXME: Don't support store to bitfield yet");
217 }
Chris Lattner4b009652007-07-25 00:24:17 +0000218
219 llvm::Value *DstAddr = Dst.getAddress();
Chris Lattnerbdb8ffb2007-08-11 00:04:45 +0000220 assert(Src.isScalar() && "Can't emit an agg store with this method");
221 // FIXME: Handle volatility etc.
Chris Lattnere24c4cf2007-08-31 22:49:20 +0000222 const llvm::Type *SrcTy = Src.getScalarVal()->getType();
Chris Lattnerbdb8ffb2007-08-11 00:04:45 +0000223 const llvm::Type *AddrTy =
224 cast<llvm::PointerType>(DstAddr->getType())->getElementType();
Chris Lattner4b009652007-07-25 00:24:17 +0000225
Chris Lattnerbdb8ffb2007-08-11 00:04:45 +0000226 if (AddrTy != SrcTy)
227 DstAddr = Builder.CreateBitCast(DstAddr, llvm::PointerType::get(SrcTy),
228 "storetmp");
Chris Lattnere24c4cf2007-08-31 22:49:20 +0000229 Builder.CreateStore(Src.getScalarVal(), DstAddr);
Chris Lattner4b009652007-07-25 00:24:17 +0000230}
231
Devang Patel0f2a8fb2007-10-30 20:59:40 +0000232void CodeGenFunction::EmitStoreThroughOCUComponentLValue(RValue Src, LValue Dst,
Chris Lattner5bfdd232007-08-03 16:28:33 +0000233 QualType Ty) {
234 // This access turns into a read/modify/write of the vector. Load the input
235 // value now.
236 llvm::Value *Vec = Builder.CreateLoad(Dst.getOCUVectorAddr(), "tmp");
237 // FIXME: Volatility.
Chris Lattnera0d03a72007-08-03 17:31:20 +0000238 unsigned EncFields = Dst.getOCUVectorElts();
Chris Lattner5bfdd232007-08-03 16:28:33 +0000239
Chris Lattnere24c4cf2007-08-31 22:49:20 +0000240 llvm::Value *SrcVal = Src.getScalarVal();
Chris Lattner5bfdd232007-08-03 16:28:33 +0000241
Chris Lattner940966d2007-08-03 16:37:04 +0000242 if (const VectorType *VTy = Ty->getAsVectorType()) {
243 unsigned NumSrcElts = VTy->getNumElements();
244
245 // Extract/Insert each element.
246 for (unsigned i = 0; i != NumSrcElts; ++i) {
247 llvm::Value *Elt = llvm::ConstantInt::get(llvm::Type::Int32Ty, i);
248 Elt = Builder.CreateExtractElement(SrcVal, Elt, "tmp");
249
Chris Lattnera0d03a72007-08-03 17:31:20 +0000250 unsigned Idx = OCUVectorElementExpr::getAccessedFieldNo(i, EncFields);
Chris Lattner940966d2007-08-03 16:37:04 +0000251 llvm::Value *OutIdx = llvm::ConstantInt::get(llvm::Type::Int32Ty, Idx);
252 Vec = Builder.CreateInsertElement(Vec, Elt, OutIdx, "tmp");
253 }
254 } else {
255 // If the Src is a scalar (not a vector) it must be updating one element.
Chris Lattnera0d03a72007-08-03 17:31:20 +0000256 unsigned InIdx = OCUVectorElementExpr::getAccessedFieldNo(0, EncFields);
Chris Lattner5bfdd232007-08-03 16:28:33 +0000257 llvm::Value *Elt = llvm::ConstantInt::get(llvm::Type::Int32Ty, InIdx);
258 Vec = Builder.CreateInsertElement(Vec, SrcVal, Elt, "tmp");
Chris Lattner5bfdd232007-08-03 16:28:33 +0000259 }
260
Chris Lattner5bfdd232007-08-03 16:28:33 +0000261 Builder.CreateStore(Vec, Dst.getOCUVectorAddr());
262}
263
Chris Lattner4b009652007-07-25 00:24:17 +0000264
265LValue CodeGenFunction::EmitDeclRefLValue(const DeclRefExpr *E) {
Steve Naroffcb597472007-09-13 21:41:19 +0000266 const ValueDecl *D = E->getDecl();
Chris Lattner4b009652007-07-25 00:24:17 +0000267 if (isa<BlockVarDecl>(D) || isa<ParmVarDecl>(D)) {
268 llvm::Value *V = LocalDeclMap[D];
269 assert(V && "BlockVarDecl not entered in LocalDeclMap?");
270 return LValue::MakeAddr(V);
271 } else if (isa<FunctionDecl>(D) || isa<FileVarDecl>(D)) {
272 return LValue::MakeAddr(CGM.GetAddrOfGlobalDecl(D));
273 }
274 assert(0 && "Unimp declref");
Chris Lattner1d2b4612007-09-16 19:23:47 +0000275 //an invalid LValue, but the assert will
276 //ensure that this point is never reached.
277 return LValue();
Chris Lattner4b009652007-07-25 00:24:17 +0000278}
279
280LValue CodeGenFunction::EmitUnaryOpLValue(const UnaryOperator *E) {
281 // __extension__ doesn't affect lvalue-ness.
282 if (E->getOpcode() == UnaryOperator::Extension)
283 return EmitLValue(E->getSubExpr());
284
Chris Lattner5bf72022007-10-30 22:53:42 +0000285 switch (E->getOpcode()) {
286 default: assert(0 && "Unknown unary operator lvalue!");
287 case UnaryOperator::Deref:
288 return LValue::MakeAddr(EmitScalarExpr(E->getSubExpr()));
289 case UnaryOperator::Real:
290 case UnaryOperator::Imag:
291 LValue LV = EmitLValue(E->getSubExpr());
292
293 llvm::Constant *Zero = llvm::ConstantInt::get(llvm::Type::Int32Ty, 0);
294 llvm::Constant *Idx = llvm::ConstantInt::get(llvm::Type::Int32Ty,
295 E->getOpcode() == UnaryOperator::Imag);
296 llvm::Value *Ops[] = {Zero, Idx};
297 return LValue::MakeAddr(Builder.CreateGEP(LV.getAddress(), Ops, Ops+2,
298 "idx"));
299 }
Chris Lattner4b009652007-07-25 00:24:17 +0000300}
301
302LValue CodeGenFunction::EmitStringLiteralLValue(const StringLiteral *E) {
303 assert(!E->isWide() && "FIXME: Wide strings not supported yet!");
304 const char *StrData = E->getStrData();
305 unsigned Len = E->getByteLength();
Chris Lattnerdb6be562007-11-28 05:34:05 +0000306 std::string StringLiteral(StrData, StrData+Len);
307 return LValue::MakeAddr(CGM.GetAddrOfConstantString(StringLiteral));
Chris Lattner4b009652007-07-25 00:24:17 +0000308}
309
310LValue CodeGenFunction::EmitPreDefinedLValue(const PreDefinedExpr *E) {
311 std::string FunctionName(CurFuncDecl->getName());
312 std::string GlobalVarName;
313
314 switch (E->getIdentType()) {
315 default:
316 assert(0 && "unknown pre-defined ident type");
317 case PreDefinedExpr::Func:
318 GlobalVarName = "__func__.";
319 break;
320 case PreDefinedExpr::Function:
321 GlobalVarName = "__FUNCTION__.";
322 break;
323 case PreDefinedExpr::PrettyFunction:
324 // FIXME:: Demangle C++ method names
325 GlobalVarName = "__PRETTY_FUNCTION__.";
326 break;
327 }
328
329 GlobalVarName += CurFuncDecl->getName();
330
331 // FIXME: Can cache/reuse these within the module.
332 llvm::Constant *C=llvm::ConstantArray::get(FunctionName);
333
334 // Create a global variable for this.
335 C = new llvm::GlobalVariable(C->getType(), true,
336 llvm::GlobalValue::InternalLinkage,
337 C, GlobalVarName, CurFn->getParent());
338 llvm::Constant *Zero = llvm::Constant::getNullValue(llvm::Type::Int32Ty);
339 llvm::Constant *Zeros[] = { Zero, Zero };
340 C = llvm::ConstantExpr::getGetElementPtr(C, Zeros, 2);
341 return LValue::MakeAddr(C);
342}
343
344LValue CodeGenFunction::EmitArraySubscriptExpr(const ArraySubscriptExpr *E) {
Ted Kremenek1c1700f2007-08-20 16:18:38 +0000345 // The index must always be an integer, which is not an aggregate. Emit it.
Chris Lattner9fba49a2007-08-24 05:35:26 +0000346 llvm::Value *Idx = EmitScalarExpr(E->getIdx());
Chris Lattner4b009652007-07-25 00:24:17 +0000347
348 // If the base is a vector type, then we are forming a vector element lvalue
349 // with this subscript.
Ted Kremenek1c1700f2007-08-20 16:18:38 +0000350 if (E->getLHS()->getType()->isVectorType()) {
Chris Lattner4b009652007-07-25 00:24:17 +0000351 // Emit the vector as an lvalue to get its address.
Ted Kremenek1c1700f2007-08-20 16:18:38 +0000352 LValue LHS = EmitLValue(E->getLHS());
353 assert(LHS.isSimple() && "Can only subscript lvalue vectors here!");
Chris Lattner4b009652007-07-25 00:24:17 +0000354 // FIXME: This should properly sign/zero/extend or truncate Idx to i32.
Ted Kremenek1c1700f2007-08-20 16:18:38 +0000355 return LValue::MakeVectorElt(LHS.getAddress(), Idx);
Chris Lattner4b009652007-07-25 00:24:17 +0000356 }
357
Ted Kremenek1c1700f2007-08-20 16:18:38 +0000358 // The base must be a pointer, which is not an aggregate. Emit it.
Chris Lattner9fba49a2007-08-24 05:35:26 +0000359 llvm::Value *Base = EmitScalarExpr(E->getBase());
Chris Lattner4b009652007-07-25 00:24:17 +0000360
Ted Kremenek1c1700f2007-08-20 16:18:38 +0000361 // Extend or truncate the index type to 32 or 64-bits.
Chris Lattner2af72ac2007-08-08 17:43:05 +0000362 QualType IdxTy = E->getIdx()->getType();
Chris Lattner4b009652007-07-25 00:24:17 +0000363 bool IdxSigned = IdxTy->isSignedIntegerType();
364 unsigned IdxBitwidth = cast<llvm::IntegerType>(Idx->getType())->getBitWidth();
365 if (IdxBitwidth != LLVMPointerWidth)
366 Idx = Builder.CreateIntCast(Idx, llvm::IntegerType::get(LLVMPointerWidth),
367 IdxSigned, "idxprom");
368
369 // We know that the pointer points to a type of the correct size, unless the
370 // size is a VLA.
371 if (!E->getType()->isConstantSizeType(getContext()))
372 assert(0 && "VLA idx not implemented");
373 return LValue::MakeAddr(Builder.CreateGEP(Base, Idx, "arrayidx"));
374}
375
Chris Lattner65520192007-08-02 23:37:31 +0000376LValue CodeGenFunction::
Chris Lattnera0d03a72007-08-03 17:31:20 +0000377EmitOCUVectorElementExpr(const OCUVectorElementExpr *E) {
Chris Lattner65520192007-08-02 23:37:31 +0000378 // Emit the base vector as an l-value.
379 LValue Base = EmitLValue(E->getBase());
380 assert(Base.isSimple() && "Can only subscript lvalue vectors here!");
381
Chris Lattnera0d03a72007-08-03 17:31:20 +0000382 return LValue::MakeOCUVectorElt(Base.getAddress(),
383 E->getEncodedElementAccess());
Chris Lattner65520192007-08-02 23:37:31 +0000384}
385
Devang Patel41b66252007-10-23 20:28:39 +0000386LValue CodeGenFunction::EmitMemberExpr(const MemberExpr *E) {
387
Devang Patel9dd3e2b2007-10-24 22:26:28 +0000388 Expr *BaseExpr = E->getBase();
Devang Patel9dd3e2b2007-10-24 22:26:28 +0000389 llvm::Value *BaseValue = NULL;
Devang Patel2b24fd92007-10-26 18:15:21 +0000390 if (BaseExpr->isLvalue() == Expr::LV_Valid) {
Devang Patel9dd3e2b2007-10-24 22:26:28 +0000391 LValue BaseLV = EmitLValue(BaseExpr);
392 BaseValue = BaseLV.getAddress();
Devang Patel2b24fd92007-10-26 18:15:21 +0000393
Devang Patel9dd3e2b2007-10-24 22:26:28 +0000394 if (E->isArrow()) {
Chris Lattnerfca02932007-11-30 18:02:19 +0000395 QualType Ty = BaseExpr->getType();
396 Ty = cast<PointerType>(Ty.getCanonicalType())->getPointeeType();
Devang Patel0f2a8fb2007-10-30 20:59:40 +0000397 BaseValue =
398 Builder.CreateBitCast(BaseValue,
Chris Lattnerfca02932007-11-30 18:02:19 +0000399 llvm::PointerType::get(ConvertType(Ty)), "tmp");
Devang Patel9dd3e2b2007-10-24 22:26:28 +0000400 }
Devang Patel2b24fd92007-10-26 18:15:21 +0000401 } else
402 BaseValue = EmitScalarExpr(BaseExpr);
Devang Patel41b66252007-10-23 20:28:39 +0000403
404 FieldDecl *Field = E->getMemberDecl();
405 unsigned idx = CGM.getTypes().getLLVMFieldNo(Field);
406 llvm::Value *Idxs[2] = { llvm::Constant::getNullValue(llvm::Type::Int32Ty),
Devang Patel30f6f132007-10-24 00:26:24 +0000407 llvm::ConstantInt::get(llvm::Type::Int32Ty, idx) };
Devang Patel41b66252007-10-23 20:28:39 +0000408
Devang Patel9b1ca9e2007-10-26 19:42:18 +0000409 llvm::Value *V = Builder.CreateGEP(BaseValue,Idxs, Idxs + 2, "tmp");
410 // Match union field type.
411 if (BaseExpr->getType()->isUnionType()) {
412 const llvm::Type * FieldTy = ConvertType(Field->getType());
Devang Patel0f2a8fb2007-10-30 20:59:40 +0000413 const llvm::PointerType * BaseTy =
414 cast<llvm::PointerType>(BaseValue->getType());
Devang Patel9b1ca9e2007-10-26 19:42:18 +0000415 if (FieldTy != BaseTy->getElementType()) {
416 V = Builder.CreateBitCast(V, llvm::PointerType::get(FieldTy), "tmp");
417 }
418 }
419 return LValue::MakeAddr(V);
Devang Patel41b66252007-10-23 20:28:39 +0000420
421 // FIXME: If record field does not have one to one match with llvm::StructType
422 // field then apply appropriate masks to select only member field bits.
423}
424
Chris Lattner4b009652007-07-25 00:24:17 +0000425//===--------------------------------------------------------------------===//
426// Expression Emission
427//===--------------------------------------------------------------------===//
428
Chris Lattnerb2cb9cb2007-08-20 22:37:10 +0000429
Chris Lattner4b009652007-07-25 00:24:17 +0000430RValue CodeGenFunction::EmitCallExpr(const CallExpr *E) {
Anders Carlsson49865302007-08-20 18:05:56 +0000431 if (const ImplicitCastExpr *IcExpr =
432 dyn_cast<const ImplicitCastExpr>(E->getCallee()))
433 if (const DeclRefExpr *DRExpr =
434 dyn_cast<const DeclRefExpr>(IcExpr->getSubExpr()))
435 if (const FunctionDecl *FDecl =
436 dyn_cast<const FunctionDecl>(DRExpr->getDecl()))
437 if (unsigned builtinID = FDecl->getIdentifier()->getBuiltinID())
438 return EmitBuiltinExpr(builtinID, E);
439
Chris Lattner9fba49a2007-08-24 05:35:26 +0000440 llvm::Value *Callee = EmitScalarExpr(E->getCallee());
Chris Lattner02c60f52007-08-31 04:44:06 +0000441 return EmitCallExpr(Callee, E);
442}
443
444RValue CodeGenFunction::EmitCallExpr(llvm::Value *Callee, const CallExpr *E) {
Chris Lattner4b009652007-07-25 00:24:17 +0000445 // The callee type will always be a pointer to function type, get the function
446 // type.
Chris Lattner2af72ac2007-08-08 17:43:05 +0000447 QualType CalleeTy = E->getCallee()->getType();
Chris Lattner4b009652007-07-25 00:24:17 +0000448 CalleeTy = cast<PointerType>(CalleeTy.getCanonicalType())->getPointeeType();
449
450 // Get information about the argument types.
451 FunctionTypeProto::arg_type_iterator ArgTyIt = 0, ArgTyEnd = 0;
452
453 // Calling unprototyped functions provides no argument info.
454 if (const FunctionTypeProto *FTP = dyn_cast<FunctionTypeProto>(CalleeTy)) {
455 ArgTyIt = FTP->arg_type_begin();
456 ArgTyEnd = FTP->arg_type_end();
457 }
458
459 llvm::SmallVector<llvm::Value*, 16> Args;
460
Chris Lattner59802042007-08-10 17:02:28 +0000461 // Handle struct-return functions by passing a pointer to the location that
462 // we would like to return into.
463 if (hasAggregateLLVMType(E->getType())) {
464 // Create a temporary alloca to hold the result of the call. :(
465 Args.push_back(CreateTempAlloca(ConvertType(E->getType())));
466 // FIXME: set the stret attribute on the argument.
467 }
468
Chris Lattner4b009652007-07-25 00:24:17 +0000469 for (unsigned i = 0, e = E->getNumArgs(); i != e; ++i) {
Chris Lattner2af72ac2007-08-08 17:43:05 +0000470 QualType ArgTy = E->getArg(i)->getType();
Chris Lattner4b009652007-07-25 00:24:17 +0000471
Chris Lattnerb06c8dd2007-08-26 22:55:13 +0000472 if (!hasAggregateLLVMType(ArgTy)) {
473 // Scalar argument is passed by-value.
474 Args.push_back(EmitScalarExpr(E->getArg(i)));
Chris Lattnerb06c8dd2007-08-26 22:55:13 +0000475 } else if (ArgTy->isComplexType()) {
476 // Make a temporary alloca to pass the argument.
477 llvm::Value *DestMem = CreateTempAlloca(ConvertType(ArgTy));
478 EmitComplexExprIntoAddr(E->getArg(i), DestMem, false);
479 Args.push_back(DestMem);
480 } else {
481 llvm::Value *DestMem = CreateTempAlloca(ConvertType(ArgTy));
482 EmitAggExpr(E->getArg(i), DestMem, false);
483 Args.push_back(DestMem);
Chris Lattner4b009652007-07-25 00:24:17 +0000484 }
Chris Lattner4b009652007-07-25 00:24:17 +0000485 }
486
Chris Lattnera9572252007-08-01 06:24:52 +0000487 llvm::Value *V = Builder.CreateCall(Callee, &Args[0], &Args[0]+Args.size());
Chris Lattner4b009652007-07-25 00:24:17 +0000488 if (V->getType() != llvm::Type::VoidTy)
489 V->setName("call");
Chris Lattnere24c4cf2007-08-31 22:49:20 +0000490 else if (E->getType()->isComplexType())
491 return RValue::getComplex(LoadComplexFromAddr(Args[0], false));
Chris Lattner59802042007-08-10 17:02:28 +0000492 else if (hasAggregateLLVMType(E->getType()))
493 // Struct return.
494 return RValue::getAggregate(Args[0]);
Chris Lattner307da022007-11-30 17:56:23 +0000495 else {
496 // void return.
497 assert(E->getType()->isVoidType() && "Should only have a void expr here");
498 V = 0;
499 }
Chris Lattner59802042007-08-10 17:02:28 +0000500
Chris Lattner4b009652007-07-25 00:24:17 +0000501 return RValue::get(V);
502}