blob: 5d563e19b969be0d91a5e704dc382eac73fa69a7 [file] [log] [blame]
Reid Spencer5f016e22007-07-11 17:01:13 +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"
Chris Lattner99e0d792007-07-16 05:43:05 +000021#include "llvm/Support/MathExtras.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000022using 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 Lattner9069fa22007-08-26 16:46:58 +000039 QualType BoolTy = getContext().BoolTy;
40 if (!E->getType()->isComplexType())
41 return EmitScalarConversion(EmitScalarExpr(E), E->getType(), BoolTy);
Reid Spencer5f016e22007-07-11 17:01:13 +000042
Chris Lattner9069fa22007-08-26 16:46:58 +000043 return EmitComplexToScalarConversion(EmitComplexExpr(E), E->getType(),BoolTy);
Reid Spencer5f016e22007-07-11 17:01:13 +000044}
45
Chris Lattner9b655512007-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
Reid Spencer5f016e22007-07-11 17:01:13 +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 Lattner7013c8c2007-08-26 05:06:40 +000084 default: {
Chris Lattnerdc4d2802007-12-02 01:49:16 +000085 WarnUnsupported(E, "l-value expression");
Chris Lattner7013c8c2007-08-26 05:06:40 +000086 llvm::Type *Ty = llvm::PointerType::get(ConvertType(E->getType()));
87 return LValue::MakeAddr(llvm::UndefValue::get(Ty));
88 }
Reid Spencer5f016e22007-07-11 17:01:13 +000089
90 case Expr::DeclRefExprClass: return EmitDeclRefLValue(cast<DeclRefExpr>(E));
91 case Expr::ParenExprClass:return EmitLValue(cast<ParenExpr>(E)->getSubExpr());
Anders Carlsson22742662007-07-21 05:21:51 +000092 case Expr::PreDefinedExprClass:
93 return EmitPreDefinedLValue(cast<PreDefinedExpr>(E));
Reid Spencer5f016e22007-07-11 17:01:13 +000094 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 Lattner6481a572007-08-03 17:31:20 +0000101 case Expr::OCUVectorElementExprClass:
102 return EmitOCUVectorElementExpr(cast<OCUVectorElementExpr>(E));
Devang Patelb9b00ad2007-10-23 20:28:39 +0000103 case Expr::MemberExprClass: return EmitMemberExpr(cast<MemberExpr>(E));
Reid Spencer5f016e22007-07-11 17:01:13 +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) {
Reid Spencer5f016e22007-07-11 17:01:13 +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 Lattner883f6a72007-08-11 00:04:45 +0000120 assert(ExprType->isFunctionType() && "Unknown scalar value");
121 return RValue::get(Ptr);
Reid Spencer5f016e22007-07-11 17:01:13 +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 Lattner46ea8eb2007-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 Lattner6481a572007-08-03 17:31:20 +0000132 if (LV.isOCUVectorElt())
133 return EmitLoadOfOCUElementLValue(LV, ExprType);
Reid Spencer5f016e22007-07-11 17:01:13 +0000134
135 assert(0 && "Bitfield ref not impl!");
Chris Lattnerb1776cb2007-09-16 19:23:47 +0000136 //an invalid RValue, but the assert will
137 //ensure that this point is never reached
138 return RValue();
Reid Spencer5f016e22007-07-11 17:01:13 +0000139}
140
Chris Lattner34cdc862007-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 Lattner6481a572007-08-03 17:31:20 +0000143RValue CodeGenFunction::EmitLoadOfOCUElementLValue(LValue LV,
Chris Lattnercf60cd22007-08-10 17:10:08 +0000144 QualType ExprType) {
Chris Lattner34cdc862007-08-03 16:18:34 +0000145 llvm::Value *Vec = Builder.CreateLoad(LV.getOCUVectorAddr(), "tmp");
146
Chris Lattner6481a572007-08-03 17:31:20 +0000147 unsigned EncFields = LV.getOCUVectorElts();
Chris Lattner34cdc862007-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 Lattnercf60cd22007-08-10 17:10:08 +0000151 const VectorType *ExprVT = ExprType->getAsVectorType();
152 if (!ExprVT) {
Chris Lattner6481a572007-08-03 17:31:20 +0000153 unsigned InIdx = OCUVectorElementExpr::getAccessedFieldNo(0, EncFields);
Chris Lattner34cdc862007-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 Lattnercf60cd22007-08-10 17:10:08 +0000160 unsigned NumResultElts = ExprVT->getNumElements();
Chris Lattner34cdc862007-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 Lattner6481a572007-08-03 17:31:20 +0000167 unsigned InIdx = OCUVectorElementExpr::getAccessedFieldNo(i, EncFields);
Chris Lattner34cdc862007-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 Lattner6481a572007-08-03 17:31:20 +0000183 unsigned InIdx = OCUVectorElementExpr::getAccessedFieldNo(i, EncFields);
Chris Lattner34cdc862007-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
Reid Spencer5f016e22007-07-11 17:01:13 +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 Lattner017d6aa2007-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 Lattner9b655512007-08-31 22:49:20 +0000206 Vec = Builder.CreateInsertElement(Vec, Src.getScalarVal(),
Chris Lattner017d6aa2007-08-03 16:28:33 +0000207 Dst.getVectorIdx(), "vecins");
208 Builder.CreateStore(Vec, Dst.getVectorAddr());
209 return;
210 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000211
Chris Lattner017d6aa2007-08-03 16:28:33 +0000212 // If this is an update of elements of a vector, insert them as appropriate.
Chris Lattner6481a572007-08-03 17:31:20 +0000213 if (Dst.isOCUVectorElt())
Chris Lattner017d6aa2007-08-03 16:28:33 +0000214 return EmitStoreThroughOCUComponentLValue(Src, Dst, Ty);
215
216 assert(0 && "FIXME: Don't support store to bitfield yet");
217 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000218
219 llvm::Value *DstAddr = Dst.getAddress();
Chris Lattner883f6a72007-08-11 00:04:45 +0000220 assert(Src.isScalar() && "Can't emit an agg store with this method");
221 // FIXME: Handle volatility etc.
Chris Lattner9b655512007-08-31 22:49:20 +0000222 const llvm::Type *SrcTy = Src.getScalarVal()->getType();
Chris Lattner883f6a72007-08-11 00:04:45 +0000223 const llvm::Type *AddrTy =
224 cast<llvm::PointerType>(DstAddr->getType())->getElementType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000225
Chris Lattner883f6a72007-08-11 00:04:45 +0000226 if (AddrTy != SrcTy)
227 DstAddr = Builder.CreateBitCast(DstAddr, llvm::PointerType::get(SrcTy),
228 "storetmp");
Chris Lattner9b655512007-08-31 22:49:20 +0000229 Builder.CreateStore(Src.getScalarVal(), DstAddr);
Reid Spencer5f016e22007-07-11 17:01:13 +0000230}
231
Devang Patele9b8c0a2007-10-30 20:59:40 +0000232void CodeGenFunction::EmitStoreThroughOCUComponentLValue(RValue Src, LValue Dst,
Chris Lattner017d6aa2007-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 Lattner6481a572007-08-03 17:31:20 +0000238 unsigned EncFields = Dst.getOCUVectorElts();
Chris Lattner017d6aa2007-08-03 16:28:33 +0000239
Chris Lattner9b655512007-08-31 22:49:20 +0000240 llvm::Value *SrcVal = Src.getScalarVal();
Chris Lattner017d6aa2007-08-03 16:28:33 +0000241
Chris Lattner7e6b51b2007-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 Lattner6481a572007-08-03 17:31:20 +0000250 unsigned Idx = OCUVectorElementExpr::getAccessedFieldNo(i, EncFields);
Chris Lattner7e6b51b2007-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 Lattner6481a572007-08-03 17:31:20 +0000256 unsigned InIdx = OCUVectorElementExpr::getAccessedFieldNo(0, EncFields);
Chris Lattner017d6aa2007-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 Lattner017d6aa2007-08-03 16:28:33 +0000259 }
260
Chris Lattner017d6aa2007-08-03 16:28:33 +0000261 Builder.CreateStore(Vec, Dst.getOCUVectorAddr());
262}
263
Reid Spencer5f016e22007-07-11 17:01:13 +0000264
265LValue CodeGenFunction::EmitDeclRefLValue(const DeclRefExpr *E) {
Steve Naroff8e74c932007-09-13 21:41:19 +0000266 const ValueDecl *D = E->getDecl();
Reid Spencer5f016e22007-07-11 17:01:13 +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);
Chris Lattner9cd4fe42007-12-02 07:09:19 +0000271 } else if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
272 return LValue::MakeAddr(CGM.GetAddrOfFunctionDecl(FD, false));
273 } else if (const FileVarDecl *FVD = dyn_cast<FileVarDecl>(D)) {
274 return LValue::MakeAddr(CGM.GetAddrOfFileVarDecl(FVD, false));
Reid Spencer5f016e22007-07-11 17:01:13 +0000275 }
276 assert(0 && "Unimp declref");
Chris Lattnerb1776cb2007-09-16 19:23:47 +0000277 //an invalid LValue, but the assert will
278 //ensure that this point is never reached.
279 return LValue();
Reid Spencer5f016e22007-07-11 17:01:13 +0000280}
281
282LValue CodeGenFunction::EmitUnaryOpLValue(const UnaryOperator *E) {
283 // __extension__ doesn't affect lvalue-ness.
284 if (E->getOpcode() == UnaryOperator::Extension)
285 return EmitLValue(E->getSubExpr());
286
Chris Lattner7da36f62007-10-30 22:53:42 +0000287 switch (E->getOpcode()) {
288 default: assert(0 && "Unknown unary operator lvalue!");
289 case UnaryOperator::Deref:
290 return LValue::MakeAddr(EmitScalarExpr(E->getSubExpr()));
291 case UnaryOperator::Real:
292 case UnaryOperator::Imag:
293 LValue LV = EmitLValue(E->getSubExpr());
294
295 llvm::Constant *Zero = llvm::ConstantInt::get(llvm::Type::Int32Ty, 0);
296 llvm::Constant *Idx = llvm::ConstantInt::get(llvm::Type::Int32Ty,
297 E->getOpcode() == UnaryOperator::Imag);
298 llvm::Value *Ops[] = {Zero, Idx};
299 return LValue::MakeAddr(Builder.CreateGEP(LV.getAddress(), Ops, Ops+2,
300 "idx"));
301 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000302}
303
304LValue CodeGenFunction::EmitStringLiteralLValue(const StringLiteral *E) {
305 assert(!E->isWide() && "FIXME: Wide strings not supported yet!");
306 const char *StrData = E->getStrData();
307 unsigned Len = E->getByteLength();
Chris Lattner45e8cbd2007-11-28 05:34:05 +0000308 std::string StringLiteral(StrData, StrData+Len);
309 return LValue::MakeAddr(CGM.GetAddrOfConstantString(StringLiteral));
Reid Spencer5f016e22007-07-11 17:01:13 +0000310}
311
Anders Carlsson22742662007-07-21 05:21:51 +0000312LValue CodeGenFunction::EmitPreDefinedLValue(const PreDefinedExpr *E) {
313 std::string FunctionName(CurFuncDecl->getName());
314 std::string GlobalVarName;
315
316 switch (E->getIdentType()) {
317 default:
318 assert(0 && "unknown pre-defined ident type");
319 case PreDefinedExpr::Func:
320 GlobalVarName = "__func__.";
321 break;
322 case PreDefinedExpr::Function:
323 GlobalVarName = "__FUNCTION__.";
324 break;
325 case PreDefinedExpr::PrettyFunction:
326 // FIXME:: Demangle C++ method names
327 GlobalVarName = "__PRETTY_FUNCTION__.";
328 break;
329 }
330
331 GlobalVarName += CurFuncDecl->getName();
332
333 // FIXME: Can cache/reuse these within the module.
334 llvm::Constant *C=llvm::ConstantArray::get(FunctionName);
335
336 // Create a global variable for this.
337 C = new llvm::GlobalVariable(C->getType(), true,
338 llvm::GlobalValue::InternalLinkage,
339 C, GlobalVarName, CurFn->getParent());
340 llvm::Constant *Zero = llvm::Constant::getNullValue(llvm::Type::Int32Ty);
341 llvm::Constant *Zeros[] = { Zero, Zero };
342 C = llvm::ConstantExpr::getGetElementPtr(C, Zeros, 2);
343 return LValue::MakeAddr(C);
344}
345
Reid Spencer5f016e22007-07-11 17:01:13 +0000346LValue CodeGenFunction::EmitArraySubscriptExpr(const ArraySubscriptExpr *E) {
Ted Kremenek23245122007-08-20 16:18:38 +0000347 // The index must always be an integer, which is not an aggregate. Emit it.
Chris Lattner7f02f722007-08-24 05:35:26 +0000348 llvm::Value *Idx = EmitScalarExpr(E->getIdx());
Reid Spencer5f016e22007-07-11 17:01:13 +0000349
350 // If the base is a vector type, then we are forming a vector element lvalue
351 // with this subscript.
Ted Kremenek23245122007-08-20 16:18:38 +0000352 if (E->getLHS()->getType()->isVectorType()) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000353 // Emit the vector as an lvalue to get its address.
Ted Kremenek23245122007-08-20 16:18:38 +0000354 LValue LHS = EmitLValue(E->getLHS());
355 assert(LHS.isSimple() && "Can only subscript lvalue vectors here!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000356 // FIXME: This should properly sign/zero/extend or truncate Idx to i32.
Ted Kremenek23245122007-08-20 16:18:38 +0000357 return LValue::MakeVectorElt(LHS.getAddress(), Idx);
Reid Spencer5f016e22007-07-11 17:01:13 +0000358 }
359
Ted Kremenek23245122007-08-20 16:18:38 +0000360 // The base must be a pointer, which is not an aggregate. Emit it.
Chris Lattner7f02f722007-08-24 05:35:26 +0000361 llvm::Value *Base = EmitScalarExpr(E->getBase());
Reid Spencer5f016e22007-07-11 17:01:13 +0000362
Ted Kremenek23245122007-08-20 16:18:38 +0000363 // Extend or truncate the index type to 32 or 64-bits.
Chris Lattnerd4f08022007-08-08 17:43:05 +0000364 QualType IdxTy = E->getIdx()->getType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000365 bool IdxSigned = IdxTy->isSignedIntegerType();
366 unsigned IdxBitwidth = cast<llvm::IntegerType>(Idx->getType())->getBitWidth();
367 if (IdxBitwidth != LLVMPointerWidth)
368 Idx = Builder.CreateIntCast(Idx, llvm::IntegerType::get(LLVMPointerWidth),
369 IdxSigned, "idxprom");
370
371 // We know that the pointer points to a type of the correct size, unless the
372 // size is a VLA.
Chris Lattner590b6642007-07-15 23:26:56 +0000373 if (!E->getType()->isConstantSizeType(getContext()))
Reid Spencer5f016e22007-07-11 17:01:13 +0000374 assert(0 && "VLA idx not implemented");
375 return LValue::MakeAddr(Builder.CreateGEP(Base, Idx, "arrayidx"));
376}
377
Chris Lattner349aaec2007-08-02 23:37:31 +0000378LValue CodeGenFunction::
Chris Lattner6481a572007-08-03 17:31:20 +0000379EmitOCUVectorElementExpr(const OCUVectorElementExpr *E) {
Chris Lattner349aaec2007-08-02 23:37:31 +0000380 // Emit the base vector as an l-value.
381 LValue Base = EmitLValue(E->getBase());
382 assert(Base.isSimple() && "Can only subscript lvalue vectors here!");
383
Chris Lattner6481a572007-08-03 17:31:20 +0000384 return LValue::MakeOCUVectorElt(Base.getAddress(),
385 E->getEncodedElementAccess());
Chris Lattner349aaec2007-08-02 23:37:31 +0000386}
387
Devang Patelb9b00ad2007-10-23 20:28:39 +0000388LValue CodeGenFunction::EmitMemberExpr(const MemberExpr *E) {
389
Devang Patel126a8562007-10-24 22:26:28 +0000390 Expr *BaseExpr = E->getBase();
Devang Patel126a8562007-10-24 22:26:28 +0000391 llvm::Value *BaseValue = NULL;
Chris Lattner12f65f62007-12-02 18:52:07 +0000392
393 // If this is s.x, emit s as an lvalue. If it is s->x, emit s as a scalar.
394 if (E->isArrow())
Devang Patel0a961182007-10-26 18:15:21 +0000395 BaseValue = EmitScalarExpr(BaseExpr);
Chris Lattner12f65f62007-12-02 18:52:07 +0000396 else {
397 LValue BaseLV = EmitLValue(BaseExpr);
398 // FIXME: this isn't right for bitfields.
399 BaseValue = BaseLV.getAddress();
400 }
Devang Patelb9b00ad2007-10-23 20:28:39 +0000401
402 FieldDecl *Field = E->getMemberDecl();
403 unsigned idx = CGM.getTypes().getLLVMFieldNo(Field);
404 llvm::Value *Idxs[2] = { llvm::Constant::getNullValue(llvm::Type::Int32Ty),
Devang Patel655bf3d2007-10-24 00:26:24 +0000405 llvm::ConstantInt::get(llvm::Type::Int32Ty, idx) };
Devang Patelb9b00ad2007-10-23 20:28:39 +0000406
Devang Patelabad06c2007-10-26 19:42:18 +0000407 llvm::Value *V = Builder.CreateGEP(BaseValue,Idxs, Idxs + 2, "tmp");
408 // Match union field type.
409 if (BaseExpr->getType()->isUnionType()) {
410 const llvm::Type * FieldTy = ConvertType(Field->getType());
Devang Patele9b8c0a2007-10-30 20:59:40 +0000411 const llvm::PointerType * BaseTy =
412 cast<llvm::PointerType>(BaseValue->getType());
Devang Patelabad06c2007-10-26 19:42:18 +0000413 if (FieldTy != BaseTy->getElementType()) {
414 V = Builder.CreateBitCast(V, llvm::PointerType::get(FieldTy), "tmp");
415 }
416 }
417 return LValue::MakeAddr(V);
Devang Patelb9b00ad2007-10-23 20:28:39 +0000418
419 // FIXME: If record field does not have one to one match with llvm::StructType
420 // field then apply appropriate masks to select only member field bits.
421}
422
Reid Spencer5f016e22007-07-11 17:01:13 +0000423//===--------------------------------------------------------------------===//
424// Expression Emission
425//===--------------------------------------------------------------------===//
426
Chris Lattner7016a702007-08-20 22:37:10 +0000427
Reid Spencer5f016e22007-07-11 17:01:13 +0000428RValue CodeGenFunction::EmitCallExpr(const CallExpr *E) {
Anders Carlsson022012e2007-08-20 18:05:56 +0000429 if (const ImplicitCastExpr *IcExpr =
430 dyn_cast<const ImplicitCastExpr>(E->getCallee()))
431 if (const DeclRefExpr *DRExpr =
432 dyn_cast<const DeclRefExpr>(IcExpr->getSubExpr()))
433 if (const FunctionDecl *FDecl =
434 dyn_cast<const FunctionDecl>(DRExpr->getDecl()))
435 if (unsigned builtinID = FDecl->getIdentifier()->getBuiltinID())
436 return EmitBuiltinExpr(builtinID, E);
437
Chris Lattner7f02f722007-08-24 05:35:26 +0000438 llvm::Value *Callee = EmitScalarExpr(E->getCallee());
Chris Lattnerc5e940f2007-08-31 04:44:06 +0000439 return EmitCallExpr(Callee, E);
440}
441
442RValue CodeGenFunction::EmitCallExpr(llvm::Value *Callee, const CallExpr *E) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000443 // The callee type will always be a pointer to function type, get the function
444 // type.
Chris Lattnerd4f08022007-08-08 17:43:05 +0000445 QualType CalleeTy = E->getCallee()->getType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000446 CalleeTy = cast<PointerType>(CalleeTy.getCanonicalType())->getPointeeType();
447
448 // Get information about the argument types.
449 FunctionTypeProto::arg_type_iterator ArgTyIt = 0, ArgTyEnd = 0;
450
451 // Calling unprototyped functions provides no argument info.
452 if (const FunctionTypeProto *FTP = dyn_cast<FunctionTypeProto>(CalleeTy)) {
453 ArgTyIt = FTP->arg_type_begin();
454 ArgTyEnd = FTP->arg_type_end();
455 }
456
457 llvm::SmallVector<llvm::Value*, 16> Args;
458
Chris Lattnercc666af2007-08-10 17:02:28 +0000459 // Handle struct-return functions by passing a pointer to the location that
460 // we would like to return into.
461 if (hasAggregateLLVMType(E->getType())) {
462 // Create a temporary alloca to hold the result of the call. :(
463 Args.push_back(CreateTempAlloca(ConvertType(E->getType())));
464 // FIXME: set the stret attribute on the argument.
465 }
466
Reid Spencer5f016e22007-07-11 17:01:13 +0000467 for (unsigned i = 0, e = E->getNumArgs(); i != e; ++i) {
Chris Lattnerd4f08022007-08-08 17:43:05 +0000468 QualType ArgTy = E->getArg(i)->getType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000469
Chris Lattner660ac122007-08-26 22:55:13 +0000470 if (!hasAggregateLLVMType(ArgTy)) {
471 // Scalar argument is passed by-value.
472 Args.push_back(EmitScalarExpr(E->getArg(i)));
Chris Lattner660ac122007-08-26 22:55:13 +0000473 } else if (ArgTy->isComplexType()) {
474 // Make a temporary alloca to pass the argument.
475 llvm::Value *DestMem = CreateTempAlloca(ConvertType(ArgTy));
476 EmitComplexExprIntoAddr(E->getArg(i), DestMem, false);
477 Args.push_back(DestMem);
478 } else {
479 llvm::Value *DestMem = CreateTempAlloca(ConvertType(ArgTy));
480 EmitAggExpr(E->getArg(i), DestMem, false);
481 Args.push_back(DestMem);
Reid Spencer5f016e22007-07-11 17:01:13 +0000482 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000483 }
484
Chris Lattnerbf986512007-08-01 06:24:52 +0000485 llvm::Value *V = Builder.CreateCall(Callee, &Args[0], &Args[0]+Args.size());
Reid Spencer5f016e22007-07-11 17:01:13 +0000486 if (V->getType() != llvm::Type::VoidTy)
487 V->setName("call");
Chris Lattner9b655512007-08-31 22:49:20 +0000488 else if (E->getType()->isComplexType())
489 return RValue::getComplex(LoadComplexFromAddr(Args[0], false));
Chris Lattnercc666af2007-08-10 17:02:28 +0000490 else if (hasAggregateLLVMType(E->getType()))
491 // Struct return.
492 return RValue::getAggregate(Args[0]);
Chris Lattner2202bce2007-11-30 17:56:23 +0000493 else {
494 // void return.
495 assert(E->getType()->isVoidType() && "Should only have a void expr here");
496 V = 0;
497 }
Chris Lattnercc666af2007-08-10 17:02:28 +0000498
Reid Spencer5f016e22007-07-11 17:01:13 +0000499 return RValue::get(V);
500}