blob: d5a5c3f2580592c4b45a26400e6f158130be0ae5 [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"
Anders Carlsson022012e2007-08-20 18:05:56 +000017#include "clang/Lex/IdentifierTable.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000018#include "llvm/Constants.h"
19#include "llvm/DerivedTypes.h"
20#include "llvm/Function.h"
21#include "llvm/GlobalVariable.h"
Chris Lattner99e0d792007-07-16 05:43:05 +000022#include "llvm/Support/MathExtras.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000023using namespace clang;
24using namespace CodeGen;
25
26//===--------------------------------------------------------------------===//
27// Miscellaneous Helper Methods
28//===--------------------------------------------------------------------===//
29
30/// CreateTempAlloca - This creates a alloca and inserts it into the entry
31/// block.
32llvm::AllocaInst *CodeGenFunction::CreateTempAlloca(const llvm::Type *Ty,
33 const char *Name) {
34 return new llvm::AllocaInst(Ty, 0, Name, AllocaInsertPt);
35}
36
37/// EvaluateExprAsBool - Perform the usual unary conversions on the specified
38/// expression and compare the result against zero, returning an Int1Ty value.
39llvm::Value *CodeGenFunction::EvaluateExprAsBool(const Expr *E) {
Chris Lattner9069fa22007-08-26 16:46:58 +000040 QualType BoolTy = getContext().BoolTy;
41 if (!E->getType()->isComplexType())
42 return EmitScalarConversion(EmitScalarExpr(E), E->getType(), BoolTy);
Reid Spencer5f016e22007-07-11 17:01:13 +000043
Chris Lattner9069fa22007-08-26 16:46:58 +000044 return EmitComplexToScalarConversion(EmitComplexExpr(E), E->getType(),BoolTy);
Reid Spencer5f016e22007-07-11 17:01:13 +000045}
46
Chris Lattner9b655512007-08-31 22:49:20 +000047/// EmitAnyExpr - Emit code to compute the specified expression which can have
48/// any type. The result is returned as an RValue struct. If this is an
49/// aggregate expression, the aggloc/agglocvolatile arguments indicate where
50/// the result should be returned.
51RValue CodeGenFunction::EmitAnyExpr(const Expr *E, llvm::Value *AggLoc,
52 bool isAggLocVolatile) {
53 if (!hasAggregateLLVMType(E->getType()))
54 return RValue::get(EmitScalarExpr(E));
55 else if (E->getType()->isComplexType())
56 return RValue::getComplex(EmitComplexExpr(E));
57
58 EmitAggExpr(E, AggLoc, isAggLocVolatile);
59 return RValue::getAggregate(AggLoc);
60}
61
62
Reid Spencer5f016e22007-07-11 17:01:13 +000063//===----------------------------------------------------------------------===//
64// LValue Expression Emission
65//===----------------------------------------------------------------------===//
66
67/// EmitLValue - Emit code to compute a designator that specifies the location
68/// of the expression.
69///
70/// This can return one of two things: a simple address or a bitfield
71/// reference. In either case, the LLVM Value* in the LValue structure is
72/// guaranteed to be an LLVM pointer type.
73///
74/// If this returns a bitfield reference, nothing about the pointee type of
75/// the LLVM value is known: For example, it may not be a pointer to an
76/// integer.
77///
78/// If this returns a normal address, and if the lvalue's C type is fixed
79/// size, this method guarantees that the returned pointer type will point to
80/// an LLVM type of the same size of the lvalue's type. If the lvalue has a
81/// variable length type, this is not possible.
82///
83LValue CodeGenFunction::EmitLValue(const Expr *E) {
84 switch (E->getStmtClass()) {
Chris Lattner7013c8c2007-08-26 05:06:40 +000085 default: {
Reid Spencer5f016e22007-07-11 17:01:13 +000086 fprintf(stderr, "Unimplemented lvalue expr!\n");
87 E->dump();
Chris Lattner7013c8c2007-08-26 05:06:40 +000088 llvm::Type *Ty = llvm::PointerType::get(ConvertType(E->getType()));
89 return LValue::MakeAddr(llvm::UndefValue::get(Ty));
90 }
Reid Spencer5f016e22007-07-11 17:01:13 +000091
92 case Expr::DeclRefExprClass: return EmitDeclRefLValue(cast<DeclRefExpr>(E));
93 case Expr::ParenExprClass:return EmitLValue(cast<ParenExpr>(E)->getSubExpr());
Anders Carlsson22742662007-07-21 05:21:51 +000094 case Expr::PreDefinedExprClass:
95 return EmitPreDefinedLValue(cast<PreDefinedExpr>(E));
Reid Spencer5f016e22007-07-11 17:01:13 +000096 case Expr::StringLiteralClass:
97 return EmitStringLiteralLValue(cast<StringLiteral>(E));
98
99 case Expr::UnaryOperatorClass:
100 return EmitUnaryOpLValue(cast<UnaryOperator>(E));
101 case Expr::ArraySubscriptExprClass:
102 return EmitArraySubscriptExpr(cast<ArraySubscriptExpr>(E));
Chris Lattner6481a572007-08-03 17:31:20 +0000103 case Expr::OCUVectorElementExprClass:
104 return EmitOCUVectorElementExpr(cast<OCUVectorElementExpr>(E));
Reid Spencer5f016e22007-07-11 17:01:13 +0000105 }
106}
107
108/// EmitLoadOfLValue - Given an expression that represents a value lvalue,
109/// this method emits the address of the lvalue, then loads the result as an
110/// rvalue, returning the rvalue.
111RValue CodeGenFunction::EmitLoadOfLValue(LValue LV, QualType ExprType) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000112 if (LV.isSimple()) {
113 llvm::Value *Ptr = LV.getAddress();
114 const llvm::Type *EltTy =
115 cast<llvm::PointerType>(Ptr->getType())->getElementType();
116
117 // Simple scalar l-value.
118 if (EltTy->isFirstClassType())
119 return RValue::get(Builder.CreateLoad(Ptr, "tmp"));
120
Chris Lattner883f6a72007-08-11 00:04:45 +0000121 assert(ExprType->isFunctionType() && "Unknown scalar value");
122 return RValue::get(Ptr);
Reid Spencer5f016e22007-07-11 17:01:13 +0000123 }
124
125 if (LV.isVectorElt()) {
126 llvm::Value *Vec = Builder.CreateLoad(LV.getVectorAddr(), "tmp");
127 return RValue::get(Builder.CreateExtractElement(Vec, LV.getVectorIdx(),
128 "vecext"));
129 }
Chris Lattner46ea8eb2007-08-03 00:16:29 +0000130
131 // If this is a reference to a subset of the elements of a vector, either
132 // shuffle the input or extract/insert them as appropriate.
Chris Lattner6481a572007-08-03 17:31:20 +0000133 if (LV.isOCUVectorElt())
134 return EmitLoadOfOCUElementLValue(LV, ExprType);
Reid Spencer5f016e22007-07-11 17:01:13 +0000135
136 assert(0 && "Bitfield ref not impl!");
137}
138
Chris Lattner34cdc862007-08-03 16:18:34 +0000139// If this is a reference to a subset of the elements of a vector, either
140// shuffle the input or extract/insert them as appropriate.
Chris Lattner6481a572007-08-03 17:31:20 +0000141RValue CodeGenFunction::EmitLoadOfOCUElementLValue(LValue LV,
Chris Lattnercf60cd22007-08-10 17:10:08 +0000142 QualType ExprType) {
Chris Lattner34cdc862007-08-03 16:18:34 +0000143 llvm::Value *Vec = Builder.CreateLoad(LV.getOCUVectorAddr(), "tmp");
144
Chris Lattner6481a572007-08-03 17:31:20 +0000145 unsigned EncFields = LV.getOCUVectorElts();
Chris Lattner34cdc862007-08-03 16:18:34 +0000146
147 // If the result of the expression is a non-vector type, we must be
148 // extracting a single element. Just codegen as an extractelement.
Chris Lattnercf60cd22007-08-10 17:10:08 +0000149 const VectorType *ExprVT = ExprType->getAsVectorType();
150 if (!ExprVT) {
Chris Lattner6481a572007-08-03 17:31:20 +0000151 unsigned InIdx = OCUVectorElementExpr::getAccessedFieldNo(0, EncFields);
Chris Lattner34cdc862007-08-03 16:18:34 +0000152 llvm::Value *Elt = llvm::ConstantInt::get(llvm::Type::Int32Ty, InIdx);
153 return RValue::get(Builder.CreateExtractElement(Vec, Elt, "tmp"));
154 }
155
156 // If the source and destination have the same number of elements, use a
157 // vector shuffle instead of insert/extracts.
Chris Lattnercf60cd22007-08-10 17:10:08 +0000158 unsigned NumResultElts = ExprVT->getNumElements();
Chris Lattner34cdc862007-08-03 16:18:34 +0000159 unsigned NumSourceElts =
160 cast<llvm::VectorType>(Vec->getType())->getNumElements();
161
162 if (NumResultElts == NumSourceElts) {
163 llvm::SmallVector<llvm::Constant*, 4> Mask;
164 for (unsigned i = 0; i != NumResultElts; ++i) {
Chris Lattner6481a572007-08-03 17:31:20 +0000165 unsigned InIdx = OCUVectorElementExpr::getAccessedFieldNo(i, EncFields);
Chris Lattner34cdc862007-08-03 16:18:34 +0000166 Mask.push_back(llvm::ConstantInt::get(llvm::Type::Int32Ty, InIdx));
167 }
168
169 llvm::Value *MaskV = llvm::ConstantVector::get(&Mask[0], Mask.size());
170 Vec = Builder.CreateShuffleVector(Vec,
171 llvm::UndefValue::get(Vec->getType()),
172 MaskV, "tmp");
173 return RValue::get(Vec);
174 }
175
176 // Start out with an undef of the result type.
177 llvm::Value *Result = llvm::UndefValue::get(ConvertType(ExprType));
178
179 // Extract/Insert each element of the result.
180 for (unsigned i = 0; i != NumResultElts; ++i) {
Chris Lattner6481a572007-08-03 17:31:20 +0000181 unsigned InIdx = OCUVectorElementExpr::getAccessedFieldNo(i, EncFields);
Chris Lattner34cdc862007-08-03 16:18:34 +0000182 llvm::Value *Elt = llvm::ConstantInt::get(llvm::Type::Int32Ty, InIdx);
183 Elt = Builder.CreateExtractElement(Vec, Elt, "tmp");
184
185 llvm::Value *OutIdx = llvm::ConstantInt::get(llvm::Type::Int32Ty, i);
186 Result = Builder.CreateInsertElement(Result, Elt, OutIdx, "tmp");
187 }
188
189 return RValue::get(Result);
190}
191
192
Reid Spencer5f016e22007-07-11 17:01:13 +0000193
194/// EmitStoreThroughLValue - Store the specified rvalue into the specified
195/// lvalue, where both are guaranteed to the have the same type, and that type
196/// is 'Ty'.
197void CodeGenFunction::EmitStoreThroughLValue(RValue Src, LValue Dst,
198 QualType Ty) {
Chris Lattner017d6aa2007-08-03 16:28:33 +0000199 if (!Dst.isSimple()) {
200 if (Dst.isVectorElt()) {
201 // Read/modify/write the vector, inserting the new element.
202 // FIXME: Volatility.
203 llvm::Value *Vec = Builder.CreateLoad(Dst.getVectorAddr(), "tmp");
Chris Lattner9b655512007-08-31 22:49:20 +0000204 Vec = Builder.CreateInsertElement(Vec, Src.getScalarVal(),
Chris Lattner017d6aa2007-08-03 16:28:33 +0000205 Dst.getVectorIdx(), "vecins");
206 Builder.CreateStore(Vec, Dst.getVectorAddr());
207 return;
208 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000209
Chris Lattner017d6aa2007-08-03 16:28:33 +0000210 // If this is an update of elements of a vector, insert them as appropriate.
Chris Lattner6481a572007-08-03 17:31:20 +0000211 if (Dst.isOCUVectorElt())
Chris Lattner017d6aa2007-08-03 16:28:33 +0000212 return EmitStoreThroughOCUComponentLValue(Src, Dst, Ty);
213
214 assert(0 && "FIXME: Don't support store to bitfield yet");
215 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000216
217 llvm::Value *DstAddr = Dst.getAddress();
Chris Lattner883f6a72007-08-11 00:04:45 +0000218 assert(Src.isScalar() && "Can't emit an agg store with this method");
219 // FIXME: Handle volatility etc.
Chris Lattner9b655512007-08-31 22:49:20 +0000220 const llvm::Type *SrcTy = Src.getScalarVal()->getType();
Chris Lattner883f6a72007-08-11 00:04:45 +0000221 const llvm::Type *AddrTy =
222 cast<llvm::PointerType>(DstAddr->getType())->getElementType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000223
Chris Lattner883f6a72007-08-11 00:04:45 +0000224 if (AddrTy != SrcTy)
225 DstAddr = Builder.CreateBitCast(DstAddr, llvm::PointerType::get(SrcTy),
226 "storetmp");
Chris Lattner9b655512007-08-31 22:49:20 +0000227 Builder.CreateStore(Src.getScalarVal(), DstAddr);
Reid Spencer5f016e22007-07-11 17:01:13 +0000228}
229
Chris Lattner017d6aa2007-08-03 16:28:33 +0000230void CodeGenFunction::EmitStoreThroughOCUComponentLValue(RValue Src, LValue Dst,
231 QualType Ty) {
232 // This access turns into a read/modify/write of the vector. Load the input
233 // value now.
234 llvm::Value *Vec = Builder.CreateLoad(Dst.getOCUVectorAddr(), "tmp");
235 // FIXME: Volatility.
Chris Lattner6481a572007-08-03 17:31:20 +0000236 unsigned EncFields = Dst.getOCUVectorElts();
Chris Lattner017d6aa2007-08-03 16:28:33 +0000237
Chris Lattner9b655512007-08-31 22:49:20 +0000238 llvm::Value *SrcVal = Src.getScalarVal();
Chris Lattner017d6aa2007-08-03 16:28:33 +0000239
Chris Lattner7e6b51b2007-08-03 16:37:04 +0000240 if (const VectorType *VTy = Ty->getAsVectorType()) {
241 unsigned NumSrcElts = VTy->getNumElements();
242
243 // Extract/Insert each element.
244 for (unsigned i = 0; i != NumSrcElts; ++i) {
245 llvm::Value *Elt = llvm::ConstantInt::get(llvm::Type::Int32Ty, i);
246 Elt = Builder.CreateExtractElement(SrcVal, Elt, "tmp");
247
Chris Lattner6481a572007-08-03 17:31:20 +0000248 unsigned Idx = OCUVectorElementExpr::getAccessedFieldNo(i, EncFields);
Chris Lattner7e6b51b2007-08-03 16:37:04 +0000249 llvm::Value *OutIdx = llvm::ConstantInt::get(llvm::Type::Int32Ty, Idx);
250 Vec = Builder.CreateInsertElement(Vec, Elt, OutIdx, "tmp");
251 }
252 } else {
253 // If the Src is a scalar (not a vector) it must be updating one element.
Chris Lattner6481a572007-08-03 17:31:20 +0000254 unsigned InIdx = OCUVectorElementExpr::getAccessedFieldNo(0, EncFields);
Chris Lattner017d6aa2007-08-03 16:28:33 +0000255 llvm::Value *Elt = llvm::ConstantInt::get(llvm::Type::Int32Ty, InIdx);
256 Vec = Builder.CreateInsertElement(Vec, SrcVal, Elt, "tmp");
Chris Lattner017d6aa2007-08-03 16:28:33 +0000257 }
258
Chris Lattner017d6aa2007-08-03 16:28:33 +0000259 Builder.CreateStore(Vec, Dst.getOCUVectorAddr());
260}
261
Reid Spencer5f016e22007-07-11 17:01:13 +0000262
263LValue CodeGenFunction::EmitDeclRefLValue(const DeclRefExpr *E) {
264 const Decl *D = E->getDecl();
265 if (isa<BlockVarDecl>(D) || isa<ParmVarDecl>(D)) {
266 llvm::Value *V = LocalDeclMap[D];
267 assert(V && "BlockVarDecl not entered in LocalDeclMap?");
268 return LValue::MakeAddr(V);
269 } else if (isa<FunctionDecl>(D) || isa<FileVarDecl>(D)) {
270 return LValue::MakeAddr(CGM.GetAddrOfGlobalDecl(D));
271 }
272 assert(0 && "Unimp declref");
273}
274
275LValue CodeGenFunction::EmitUnaryOpLValue(const UnaryOperator *E) {
276 // __extension__ doesn't affect lvalue-ness.
277 if (E->getOpcode() == UnaryOperator::Extension)
278 return EmitLValue(E->getSubExpr());
279
280 assert(E->getOpcode() == UnaryOperator::Deref &&
281 "'*' is the only unary operator that produces an lvalue");
Chris Lattner7f02f722007-08-24 05:35:26 +0000282 return LValue::MakeAddr(EmitScalarExpr(E->getSubExpr()));
Reid Spencer5f016e22007-07-11 17:01:13 +0000283}
284
285LValue CodeGenFunction::EmitStringLiteralLValue(const StringLiteral *E) {
286 assert(!E->isWide() && "FIXME: Wide strings not supported yet!");
287 const char *StrData = E->getStrData();
288 unsigned Len = E->getByteLength();
289
290 // FIXME: Can cache/reuse these within the module.
291 llvm::Constant *C=llvm::ConstantArray::get(std::string(StrData, StrData+Len));
292
293 // Create a global variable for this.
294 C = new llvm::GlobalVariable(C->getType(), true,
295 llvm::GlobalValue::InternalLinkage,
296 C, ".str", CurFn->getParent());
297 llvm::Constant *Zero = llvm::Constant::getNullValue(llvm::Type::Int32Ty);
298 llvm::Constant *Zeros[] = { Zero, Zero };
299 C = llvm::ConstantExpr::getGetElementPtr(C, Zeros, 2);
300 return LValue::MakeAddr(C);
301}
302
Anders Carlsson22742662007-07-21 05:21:51 +0000303LValue CodeGenFunction::EmitPreDefinedLValue(const PreDefinedExpr *E) {
304 std::string FunctionName(CurFuncDecl->getName());
305 std::string GlobalVarName;
306
307 switch (E->getIdentType()) {
308 default:
309 assert(0 && "unknown pre-defined ident type");
310 case PreDefinedExpr::Func:
311 GlobalVarName = "__func__.";
312 break;
313 case PreDefinedExpr::Function:
314 GlobalVarName = "__FUNCTION__.";
315 break;
316 case PreDefinedExpr::PrettyFunction:
317 // FIXME:: Demangle C++ method names
318 GlobalVarName = "__PRETTY_FUNCTION__.";
319 break;
320 }
321
322 GlobalVarName += CurFuncDecl->getName();
323
324 // FIXME: Can cache/reuse these within the module.
325 llvm::Constant *C=llvm::ConstantArray::get(FunctionName);
326
327 // Create a global variable for this.
328 C = new llvm::GlobalVariable(C->getType(), true,
329 llvm::GlobalValue::InternalLinkage,
330 C, GlobalVarName, CurFn->getParent());
331 llvm::Constant *Zero = llvm::Constant::getNullValue(llvm::Type::Int32Ty);
332 llvm::Constant *Zeros[] = { Zero, Zero };
333 C = llvm::ConstantExpr::getGetElementPtr(C, Zeros, 2);
334 return LValue::MakeAddr(C);
335}
336
Reid Spencer5f016e22007-07-11 17:01:13 +0000337LValue CodeGenFunction::EmitArraySubscriptExpr(const ArraySubscriptExpr *E) {
Ted Kremenek23245122007-08-20 16:18:38 +0000338 // The index must always be an integer, which is not an aggregate. Emit it.
Chris Lattner7f02f722007-08-24 05:35:26 +0000339 llvm::Value *Idx = EmitScalarExpr(E->getIdx());
Reid Spencer5f016e22007-07-11 17:01:13 +0000340
341 // If the base is a vector type, then we are forming a vector element lvalue
342 // with this subscript.
Ted Kremenek23245122007-08-20 16:18:38 +0000343 if (E->getLHS()->getType()->isVectorType()) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000344 // Emit the vector as an lvalue to get its address.
Ted Kremenek23245122007-08-20 16:18:38 +0000345 LValue LHS = EmitLValue(E->getLHS());
346 assert(LHS.isSimple() && "Can only subscript lvalue vectors here!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000347 // FIXME: This should properly sign/zero/extend or truncate Idx to i32.
Ted Kremenek23245122007-08-20 16:18:38 +0000348 return LValue::MakeVectorElt(LHS.getAddress(), Idx);
Reid Spencer5f016e22007-07-11 17:01:13 +0000349 }
350
Ted Kremenek23245122007-08-20 16:18:38 +0000351 // The base must be a pointer, which is not an aggregate. Emit it.
Chris Lattner7f02f722007-08-24 05:35:26 +0000352 llvm::Value *Base = EmitScalarExpr(E->getBase());
Reid Spencer5f016e22007-07-11 17:01:13 +0000353
Ted Kremenek23245122007-08-20 16:18:38 +0000354 // Extend or truncate the index type to 32 or 64-bits.
Chris Lattnerd4f08022007-08-08 17:43:05 +0000355 QualType IdxTy = E->getIdx()->getType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000356 bool IdxSigned = IdxTy->isSignedIntegerType();
357 unsigned IdxBitwidth = cast<llvm::IntegerType>(Idx->getType())->getBitWidth();
358 if (IdxBitwidth != LLVMPointerWidth)
359 Idx = Builder.CreateIntCast(Idx, llvm::IntegerType::get(LLVMPointerWidth),
360 IdxSigned, "idxprom");
361
362 // We know that the pointer points to a type of the correct size, unless the
363 // size is a VLA.
Chris Lattner590b6642007-07-15 23:26:56 +0000364 if (!E->getType()->isConstantSizeType(getContext()))
Reid Spencer5f016e22007-07-11 17:01:13 +0000365 assert(0 && "VLA idx not implemented");
366 return LValue::MakeAddr(Builder.CreateGEP(Base, Idx, "arrayidx"));
367}
368
Chris Lattner349aaec2007-08-02 23:37:31 +0000369LValue CodeGenFunction::
Chris Lattner6481a572007-08-03 17:31:20 +0000370EmitOCUVectorElementExpr(const OCUVectorElementExpr *E) {
Chris Lattner349aaec2007-08-02 23:37:31 +0000371 // Emit the base vector as an l-value.
372 LValue Base = EmitLValue(E->getBase());
373 assert(Base.isSimple() && "Can only subscript lvalue vectors here!");
374
Chris Lattner6481a572007-08-03 17:31:20 +0000375 return LValue::MakeOCUVectorElt(Base.getAddress(),
376 E->getEncodedElementAccess());
Chris Lattner349aaec2007-08-02 23:37:31 +0000377}
378
Reid Spencer5f016e22007-07-11 17:01:13 +0000379//===--------------------------------------------------------------------===//
380// Expression Emission
381//===--------------------------------------------------------------------===//
382
Chris Lattner7016a702007-08-20 22:37:10 +0000383
Reid Spencer5f016e22007-07-11 17:01:13 +0000384RValue CodeGenFunction::EmitCallExpr(const CallExpr *E) {
Anders Carlsson022012e2007-08-20 18:05:56 +0000385 if (const ImplicitCastExpr *IcExpr =
386 dyn_cast<const ImplicitCastExpr>(E->getCallee()))
387 if (const DeclRefExpr *DRExpr =
388 dyn_cast<const DeclRefExpr>(IcExpr->getSubExpr()))
389 if (const FunctionDecl *FDecl =
390 dyn_cast<const FunctionDecl>(DRExpr->getDecl()))
391 if (unsigned builtinID = FDecl->getIdentifier()->getBuiltinID())
392 return EmitBuiltinExpr(builtinID, E);
393
Chris Lattner7f02f722007-08-24 05:35:26 +0000394 llvm::Value *Callee = EmitScalarExpr(E->getCallee());
Chris Lattnerc5e940f2007-08-31 04:44:06 +0000395 return EmitCallExpr(Callee, E);
396}
397
398RValue CodeGenFunction::EmitCallExpr(llvm::Value *Callee, const CallExpr *E) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000399 // The callee type will always be a pointer to function type, get the function
400 // type.
Chris Lattnerd4f08022007-08-08 17:43:05 +0000401 QualType CalleeTy = E->getCallee()->getType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000402 CalleeTy = cast<PointerType>(CalleeTy.getCanonicalType())->getPointeeType();
403
404 // Get information about the argument types.
405 FunctionTypeProto::arg_type_iterator ArgTyIt = 0, ArgTyEnd = 0;
406
407 // Calling unprototyped functions provides no argument info.
408 if (const FunctionTypeProto *FTP = dyn_cast<FunctionTypeProto>(CalleeTy)) {
409 ArgTyIt = FTP->arg_type_begin();
410 ArgTyEnd = FTP->arg_type_end();
411 }
412
413 llvm::SmallVector<llvm::Value*, 16> Args;
414
Chris Lattnercc666af2007-08-10 17:02:28 +0000415 // Handle struct-return functions by passing a pointer to the location that
416 // we would like to return into.
417 if (hasAggregateLLVMType(E->getType())) {
418 // Create a temporary alloca to hold the result of the call. :(
419 Args.push_back(CreateTempAlloca(ConvertType(E->getType())));
420 // FIXME: set the stret attribute on the argument.
421 }
422
Reid Spencer5f016e22007-07-11 17:01:13 +0000423 for (unsigned i = 0, e = E->getNumArgs(); i != e; ++i) {
Chris Lattnerd4f08022007-08-08 17:43:05 +0000424 QualType ArgTy = E->getArg(i)->getType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000425
Chris Lattner660ac122007-08-26 22:55:13 +0000426 if (!hasAggregateLLVMType(ArgTy)) {
427 // Scalar argument is passed by-value.
428 Args.push_back(EmitScalarExpr(E->getArg(i)));
Chris Lattner660ac122007-08-26 22:55:13 +0000429 } else if (ArgTy->isComplexType()) {
430 // Make a temporary alloca to pass the argument.
431 llvm::Value *DestMem = CreateTempAlloca(ConvertType(ArgTy));
432 EmitComplexExprIntoAddr(E->getArg(i), DestMem, false);
433 Args.push_back(DestMem);
434 } else {
435 llvm::Value *DestMem = CreateTempAlloca(ConvertType(ArgTy));
436 EmitAggExpr(E->getArg(i), DestMem, false);
437 Args.push_back(DestMem);
Reid Spencer5f016e22007-07-11 17:01:13 +0000438 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000439 }
440
Chris Lattnerbf986512007-08-01 06:24:52 +0000441 llvm::Value *V = Builder.CreateCall(Callee, &Args[0], &Args[0]+Args.size());
Reid Spencer5f016e22007-07-11 17:01:13 +0000442 if (V->getType() != llvm::Type::VoidTy)
443 V->setName("call");
Chris Lattner9b655512007-08-31 22:49:20 +0000444 else if (E->getType()->isComplexType())
445 return RValue::getComplex(LoadComplexFromAddr(Args[0], false));
Chris Lattnercc666af2007-08-10 17:02:28 +0000446 else if (hasAggregateLLVMType(E->getType()))
447 // Struct return.
448 return RValue::getAggregate(Args[0]);
449
Reid Spencer5f016e22007-07-11 17:01:13 +0000450 return RValue::get(V);
451}