blob: 7fbd1f17aa3b4bbd6c2d8643422bf36e661bbb96 [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 Lattner4b009652007-07-25 00:24:17 +000085 fprintf(stderr, "Unimplemented lvalue expr!\n");
Chris Lattner1aef6212007-09-13 01:17:29 +000086 E->dump(getContext().SourceMgr);
Chris Lattnera52c8892007-08-26 05:06:40 +000087 llvm::Type *Ty = llvm::PointerType::get(ConvertType(E->getType()));
88 return LValue::MakeAddr(llvm::UndefValue::get(Ty));
89 }
Chris Lattner4b009652007-07-25 00:24:17 +000090
91 case Expr::DeclRefExprClass: return EmitDeclRefLValue(cast<DeclRefExpr>(E));
92 case Expr::ParenExprClass:return EmitLValue(cast<ParenExpr>(E)->getSubExpr());
93 case Expr::PreDefinedExprClass:
94 return EmitPreDefinedLValue(cast<PreDefinedExpr>(E));
95 case Expr::StringLiteralClass:
96 return EmitStringLiteralLValue(cast<StringLiteral>(E));
97
98 case Expr::UnaryOperatorClass:
99 return EmitUnaryOpLValue(cast<UnaryOperator>(E));
100 case Expr::ArraySubscriptExprClass:
101 return EmitArraySubscriptExpr(cast<ArraySubscriptExpr>(E));
Chris Lattnera0d03a72007-08-03 17:31:20 +0000102 case Expr::OCUVectorElementExprClass:
103 return EmitOCUVectorElementExpr(cast<OCUVectorElementExpr>(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
Chris Lattner5bfdd232007-08-03 16:28:33 +0000232void CodeGenFunction::EmitStoreThroughOCUComponentLValue(RValue Src, LValue Dst,
233 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
285 assert(E->getOpcode() == UnaryOperator::Deref &&
286 "'*' is the only unary operator that produces an lvalue");
Chris Lattner9fba49a2007-08-24 05:35:26 +0000287 return LValue::MakeAddr(EmitScalarExpr(E->getSubExpr()));
Chris Lattner4b009652007-07-25 00:24:17 +0000288}
289
290LValue CodeGenFunction::EmitStringLiteralLValue(const StringLiteral *E) {
291 assert(!E->isWide() && "FIXME: Wide strings not supported yet!");
292 const char *StrData = E->getStrData();
293 unsigned Len = E->getByteLength();
294
295 // FIXME: Can cache/reuse these within the module.
296 llvm::Constant *C=llvm::ConstantArray::get(std::string(StrData, StrData+Len));
297
298 // Create a global variable for this.
299 C = new llvm::GlobalVariable(C->getType(), true,
300 llvm::GlobalValue::InternalLinkage,
301 C, ".str", CurFn->getParent());
302 llvm::Constant *Zero = llvm::Constant::getNullValue(llvm::Type::Int32Ty);
303 llvm::Constant *Zeros[] = { Zero, Zero };
304 C = llvm::ConstantExpr::getGetElementPtr(C, Zeros, 2);
305 return LValue::MakeAddr(C);
306}
307
308LValue CodeGenFunction::EmitPreDefinedLValue(const PreDefinedExpr *E) {
309 std::string FunctionName(CurFuncDecl->getName());
310 std::string GlobalVarName;
311
312 switch (E->getIdentType()) {
313 default:
314 assert(0 && "unknown pre-defined ident type");
315 case PreDefinedExpr::Func:
316 GlobalVarName = "__func__.";
317 break;
318 case PreDefinedExpr::Function:
319 GlobalVarName = "__FUNCTION__.";
320 break;
321 case PreDefinedExpr::PrettyFunction:
322 // FIXME:: Demangle C++ method names
323 GlobalVarName = "__PRETTY_FUNCTION__.";
324 break;
325 }
326
327 GlobalVarName += CurFuncDecl->getName();
328
329 // FIXME: Can cache/reuse these within the module.
330 llvm::Constant *C=llvm::ConstantArray::get(FunctionName);
331
332 // Create a global variable for this.
333 C = new llvm::GlobalVariable(C->getType(), true,
334 llvm::GlobalValue::InternalLinkage,
335 C, GlobalVarName, CurFn->getParent());
336 llvm::Constant *Zero = llvm::Constant::getNullValue(llvm::Type::Int32Ty);
337 llvm::Constant *Zeros[] = { Zero, Zero };
338 C = llvm::ConstantExpr::getGetElementPtr(C, Zeros, 2);
339 return LValue::MakeAddr(C);
340}
341
342LValue CodeGenFunction::EmitArraySubscriptExpr(const ArraySubscriptExpr *E) {
Ted Kremenek1c1700f2007-08-20 16:18:38 +0000343 // The index must always be an integer, which is not an aggregate. Emit it.
Chris Lattner9fba49a2007-08-24 05:35:26 +0000344 llvm::Value *Idx = EmitScalarExpr(E->getIdx());
Chris Lattner4b009652007-07-25 00:24:17 +0000345
346 // If the base is a vector type, then we are forming a vector element lvalue
347 // with this subscript.
Ted Kremenek1c1700f2007-08-20 16:18:38 +0000348 if (E->getLHS()->getType()->isVectorType()) {
Chris Lattner4b009652007-07-25 00:24:17 +0000349 // Emit the vector as an lvalue to get its address.
Ted Kremenek1c1700f2007-08-20 16:18:38 +0000350 LValue LHS = EmitLValue(E->getLHS());
351 assert(LHS.isSimple() && "Can only subscript lvalue vectors here!");
Chris Lattner4b009652007-07-25 00:24:17 +0000352 // FIXME: This should properly sign/zero/extend or truncate Idx to i32.
Ted Kremenek1c1700f2007-08-20 16:18:38 +0000353 return LValue::MakeVectorElt(LHS.getAddress(), Idx);
Chris Lattner4b009652007-07-25 00:24:17 +0000354 }
355
Ted Kremenek1c1700f2007-08-20 16:18:38 +0000356 // The base must be a pointer, which is not an aggregate. Emit it.
Chris Lattner9fba49a2007-08-24 05:35:26 +0000357 llvm::Value *Base = EmitScalarExpr(E->getBase());
Chris Lattner4b009652007-07-25 00:24:17 +0000358
Ted Kremenek1c1700f2007-08-20 16:18:38 +0000359 // Extend or truncate the index type to 32 or 64-bits.
Chris Lattner2af72ac2007-08-08 17:43:05 +0000360 QualType IdxTy = E->getIdx()->getType();
Chris Lattner4b009652007-07-25 00:24:17 +0000361 bool IdxSigned = IdxTy->isSignedIntegerType();
362 unsigned IdxBitwidth = cast<llvm::IntegerType>(Idx->getType())->getBitWidth();
363 if (IdxBitwidth != LLVMPointerWidth)
364 Idx = Builder.CreateIntCast(Idx, llvm::IntegerType::get(LLVMPointerWidth),
365 IdxSigned, "idxprom");
366
367 // We know that the pointer points to a type of the correct size, unless the
368 // size is a VLA.
369 if (!E->getType()->isConstantSizeType(getContext()))
370 assert(0 && "VLA idx not implemented");
371 return LValue::MakeAddr(Builder.CreateGEP(Base, Idx, "arrayidx"));
372}
373
Chris Lattner65520192007-08-02 23:37:31 +0000374LValue CodeGenFunction::
Chris Lattnera0d03a72007-08-03 17:31:20 +0000375EmitOCUVectorElementExpr(const OCUVectorElementExpr *E) {
Chris Lattner65520192007-08-02 23:37:31 +0000376 // Emit the base vector as an l-value.
377 LValue Base = EmitLValue(E->getBase());
378 assert(Base.isSimple() && "Can only subscript lvalue vectors here!");
379
Chris Lattnera0d03a72007-08-03 17:31:20 +0000380 return LValue::MakeOCUVectorElt(Base.getAddress(),
381 E->getEncodedElementAccess());
Chris Lattner65520192007-08-02 23:37:31 +0000382}
383
Chris Lattner4b009652007-07-25 00:24:17 +0000384//===--------------------------------------------------------------------===//
385// Expression Emission
386//===--------------------------------------------------------------------===//
387
Chris Lattnerb2cb9cb2007-08-20 22:37:10 +0000388
Chris Lattner4b009652007-07-25 00:24:17 +0000389RValue CodeGenFunction::EmitCallExpr(const CallExpr *E) {
Anders Carlsson49865302007-08-20 18:05:56 +0000390 if (const ImplicitCastExpr *IcExpr =
391 dyn_cast<const ImplicitCastExpr>(E->getCallee()))
392 if (const DeclRefExpr *DRExpr =
393 dyn_cast<const DeclRefExpr>(IcExpr->getSubExpr()))
394 if (const FunctionDecl *FDecl =
395 dyn_cast<const FunctionDecl>(DRExpr->getDecl()))
396 if (unsigned builtinID = FDecl->getIdentifier()->getBuiltinID())
397 return EmitBuiltinExpr(builtinID, E);
398
Chris Lattner9fba49a2007-08-24 05:35:26 +0000399 llvm::Value *Callee = EmitScalarExpr(E->getCallee());
Chris Lattner02c60f52007-08-31 04:44:06 +0000400 return EmitCallExpr(Callee, E);
401}
402
403RValue CodeGenFunction::EmitCallExpr(llvm::Value *Callee, const CallExpr *E) {
Chris Lattner4b009652007-07-25 00:24:17 +0000404 // The callee type will always be a pointer to function type, get the function
405 // type.
Chris Lattner2af72ac2007-08-08 17:43:05 +0000406 QualType CalleeTy = E->getCallee()->getType();
Chris Lattner4b009652007-07-25 00:24:17 +0000407 CalleeTy = cast<PointerType>(CalleeTy.getCanonicalType())->getPointeeType();
408
409 // Get information about the argument types.
410 FunctionTypeProto::arg_type_iterator ArgTyIt = 0, ArgTyEnd = 0;
411
412 // Calling unprototyped functions provides no argument info.
413 if (const FunctionTypeProto *FTP = dyn_cast<FunctionTypeProto>(CalleeTy)) {
414 ArgTyIt = FTP->arg_type_begin();
415 ArgTyEnd = FTP->arg_type_end();
416 }
417
418 llvm::SmallVector<llvm::Value*, 16> Args;
419
Chris Lattner59802042007-08-10 17:02:28 +0000420 // Handle struct-return functions by passing a pointer to the location that
421 // we would like to return into.
422 if (hasAggregateLLVMType(E->getType())) {
423 // Create a temporary alloca to hold the result of the call. :(
424 Args.push_back(CreateTempAlloca(ConvertType(E->getType())));
425 // FIXME: set the stret attribute on the argument.
426 }
427
Chris Lattner4b009652007-07-25 00:24:17 +0000428 for (unsigned i = 0, e = E->getNumArgs(); i != e; ++i) {
Chris Lattner2af72ac2007-08-08 17:43:05 +0000429 QualType ArgTy = E->getArg(i)->getType();
Chris Lattner4b009652007-07-25 00:24:17 +0000430
Chris Lattnerb06c8dd2007-08-26 22:55:13 +0000431 if (!hasAggregateLLVMType(ArgTy)) {
432 // Scalar argument is passed by-value.
433 Args.push_back(EmitScalarExpr(E->getArg(i)));
Chris Lattnerb06c8dd2007-08-26 22:55:13 +0000434 } else if (ArgTy->isComplexType()) {
435 // Make a temporary alloca to pass the argument.
436 llvm::Value *DestMem = CreateTempAlloca(ConvertType(ArgTy));
437 EmitComplexExprIntoAddr(E->getArg(i), DestMem, false);
438 Args.push_back(DestMem);
439 } else {
440 llvm::Value *DestMem = CreateTempAlloca(ConvertType(ArgTy));
441 EmitAggExpr(E->getArg(i), DestMem, false);
442 Args.push_back(DestMem);
Chris Lattner4b009652007-07-25 00:24:17 +0000443 }
Chris Lattner4b009652007-07-25 00:24:17 +0000444 }
445
Chris Lattnera9572252007-08-01 06:24:52 +0000446 llvm::Value *V = Builder.CreateCall(Callee, &Args[0], &Args[0]+Args.size());
Chris Lattner4b009652007-07-25 00:24:17 +0000447 if (V->getType() != llvm::Type::VoidTy)
448 V->setName("call");
Chris Lattnere24c4cf2007-08-31 22:49:20 +0000449 else if (E->getType()->isComplexType())
450 return RValue::getComplex(LoadComplexFromAddr(Args[0], false));
Chris Lattner59802042007-08-10 17:02:28 +0000451 else if (hasAggregateLLVMType(E->getType()))
452 // Struct return.
453 return RValue::getAggregate(Args[0]);
454
Chris Lattner4b009652007-07-25 00:24:17 +0000455 return RValue::get(V);
456}