blob: d175610a380fa11309fd09f429195d1101c5cca7 [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
47//===----------------------------------------------------------------------===//
48// LValue Expression Emission
49//===----------------------------------------------------------------------===//
50
51/// EmitLValue - Emit code to compute a designator that specifies the location
52/// of the expression.
53///
54/// This can return one of two things: a simple address or a bitfield
55/// reference. In either case, the LLVM Value* in the LValue structure is
56/// guaranteed to be an LLVM pointer type.
57///
58/// If this returns a bitfield reference, nothing about the pointee type of
59/// the LLVM value is known: For example, it may not be a pointer to an
60/// integer.
61///
62/// If this returns a normal address, and if the lvalue's C type is fixed
63/// size, this method guarantees that the returned pointer type will point to
64/// an LLVM type of the same size of the lvalue's type. If the lvalue has a
65/// variable length type, this is not possible.
66///
67LValue CodeGenFunction::EmitLValue(const Expr *E) {
68 switch (E->getStmtClass()) {
Chris Lattner7013c8c2007-08-26 05:06:40 +000069 default: {
Reid Spencer5f016e22007-07-11 17:01:13 +000070 fprintf(stderr, "Unimplemented lvalue expr!\n");
71 E->dump();
Chris Lattner7013c8c2007-08-26 05:06:40 +000072 llvm::Type *Ty = llvm::PointerType::get(ConvertType(E->getType()));
73 return LValue::MakeAddr(llvm::UndefValue::get(Ty));
74 }
Reid Spencer5f016e22007-07-11 17:01:13 +000075
76 case Expr::DeclRefExprClass: return EmitDeclRefLValue(cast<DeclRefExpr>(E));
77 case Expr::ParenExprClass:return EmitLValue(cast<ParenExpr>(E)->getSubExpr());
Anders Carlsson22742662007-07-21 05:21:51 +000078 case Expr::PreDefinedExprClass:
79 return EmitPreDefinedLValue(cast<PreDefinedExpr>(E));
Reid Spencer5f016e22007-07-11 17:01:13 +000080 case Expr::StringLiteralClass:
81 return EmitStringLiteralLValue(cast<StringLiteral>(E));
82
83 case Expr::UnaryOperatorClass:
84 return EmitUnaryOpLValue(cast<UnaryOperator>(E));
85 case Expr::ArraySubscriptExprClass:
86 return EmitArraySubscriptExpr(cast<ArraySubscriptExpr>(E));
Chris Lattner6481a572007-08-03 17:31:20 +000087 case Expr::OCUVectorElementExprClass:
88 return EmitOCUVectorElementExpr(cast<OCUVectorElementExpr>(E));
Reid Spencer5f016e22007-07-11 17:01:13 +000089 }
90}
91
92/// EmitLoadOfLValue - Given an expression that represents a value lvalue,
93/// this method emits the address of the lvalue, then loads the result as an
94/// rvalue, returning the rvalue.
95RValue CodeGenFunction::EmitLoadOfLValue(LValue LV, QualType ExprType) {
Reid Spencer5f016e22007-07-11 17:01:13 +000096 if (LV.isSimple()) {
97 llvm::Value *Ptr = LV.getAddress();
98 const llvm::Type *EltTy =
99 cast<llvm::PointerType>(Ptr->getType())->getElementType();
100
101 // Simple scalar l-value.
102 if (EltTy->isFirstClassType())
103 return RValue::get(Builder.CreateLoad(Ptr, "tmp"));
104
Chris Lattner883f6a72007-08-11 00:04:45 +0000105 assert(ExprType->isFunctionType() && "Unknown scalar value");
106 return RValue::get(Ptr);
Reid Spencer5f016e22007-07-11 17:01:13 +0000107 }
108
109 if (LV.isVectorElt()) {
110 llvm::Value *Vec = Builder.CreateLoad(LV.getVectorAddr(), "tmp");
111 return RValue::get(Builder.CreateExtractElement(Vec, LV.getVectorIdx(),
112 "vecext"));
113 }
Chris Lattner46ea8eb2007-08-03 00:16:29 +0000114
115 // If this is a reference to a subset of the elements of a vector, either
116 // shuffle the input or extract/insert them as appropriate.
Chris Lattner6481a572007-08-03 17:31:20 +0000117 if (LV.isOCUVectorElt())
118 return EmitLoadOfOCUElementLValue(LV, ExprType);
Reid Spencer5f016e22007-07-11 17:01:13 +0000119
120 assert(0 && "Bitfield ref not impl!");
121}
122
Chris Lattner34cdc862007-08-03 16:18:34 +0000123// If this is a reference to a subset of the elements of a vector, either
124// shuffle the input or extract/insert them as appropriate.
Chris Lattner6481a572007-08-03 17:31:20 +0000125RValue CodeGenFunction::EmitLoadOfOCUElementLValue(LValue LV,
Chris Lattnercf60cd22007-08-10 17:10:08 +0000126 QualType ExprType) {
Chris Lattner34cdc862007-08-03 16:18:34 +0000127 llvm::Value *Vec = Builder.CreateLoad(LV.getOCUVectorAddr(), "tmp");
128
Chris Lattner6481a572007-08-03 17:31:20 +0000129 unsigned EncFields = LV.getOCUVectorElts();
Chris Lattner34cdc862007-08-03 16:18:34 +0000130
131 // If the result of the expression is a non-vector type, we must be
132 // extracting a single element. Just codegen as an extractelement.
Chris Lattnercf60cd22007-08-10 17:10:08 +0000133 const VectorType *ExprVT = ExprType->getAsVectorType();
134 if (!ExprVT) {
Chris Lattner6481a572007-08-03 17:31:20 +0000135 unsigned InIdx = OCUVectorElementExpr::getAccessedFieldNo(0, EncFields);
Chris Lattner34cdc862007-08-03 16:18:34 +0000136 llvm::Value *Elt = llvm::ConstantInt::get(llvm::Type::Int32Ty, InIdx);
137 return RValue::get(Builder.CreateExtractElement(Vec, Elt, "tmp"));
138 }
139
140 // If the source and destination have the same number of elements, use a
141 // vector shuffle instead of insert/extracts.
Chris Lattnercf60cd22007-08-10 17:10:08 +0000142 unsigned NumResultElts = ExprVT->getNumElements();
Chris Lattner34cdc862007-08-03 16:18:34 +0000143 unsigned NumSourceElts =
144 cast<llvm::VectorType>(Vec->getType())->getNumElements();
145
146 if (NumResultElts == NumSourceElts) {
147 llvm::SmallVector<llvm::Constant*, 4> Mask;
148 for (unsigned i = 0; i != NumResultElts; ++i) {
Chris Lattner6481a572007-08-03 17:31:20 +0000149 unsigned InIdx = OCUVectorElementExpr::getAccessedFieldNo(i, EncFields);
Chris Lattner34cdc862007-08-03 16:18:34 +0000150 Mask.push_back(llvm::ConstantInt::get(llvm::Type::Int32Ty, InIdx));
151 }
152
153 llvm::Value *MaskV = llvm::ConstantVector::get(&Mask[0], Mask.size());
154 Vec = Builder.CreateShuffleVector(Vec,
155 llvm::UndefValue::get(Vec->getType()),
156 MaskV, "tmp");
157 return RValue::get(Vec);
158 }
159
160 // Start out with an undef of the result type.
161 llvm::Value *Result = llvm::UndefValue::get(ConvertType(ExprType));
162
163 // Extract/Insert each element of the result.
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 llvm::Value *Elt = llvm::ConstantInt::get(llvm::Type::Int32Ty, InIdx);
167 Elt = Builder.CreateExtractElement(Vec, Elt, "tmp");
168
169 llvm::Value *OutIdx = llvm::ConstantInt::get(llvm::Type::Int32Ty, i);
170 Result = Builder.CreateInsertElement(Result, Elt, OutIdx, "tmp");
171 }
172
173 return RValue::get(Result);
174}
175
176
Reid Spencer5f016e22007-07-11 17:01:13 +0000177
178/// EmitStoreThroughLValue - Store the specified rvalue into the specified
179/// lvalue, where both are guaranteed to the have the same type, and that type
180/// is 'Ty'.
181void CodeGenFunction::EmitStoreThroughLValue(RValue Src, LValue Dst,
182 QualType Ty) {
Chris Lattner017d6aa2007-08-03 16:28:33 +0000183 if (!Dst.isSimple()) {
184 if (Dst.isVectorElt()) {
185 // Read/modify/write the vector, inserting the new element.
186 // FIXME: Volatility.
187 llvm::Value *Vec = Builder.CreateLoad(Dst.getVectorAddr(), "tmp");
188 Vec = Builder.CreateInsertElement(Vec, Src.getVal(),
189 Dst.getVectorIdx(), "vecins");
190 Builder.CreateStore(Vec, Dst.getVectorAddr());
191 return;
192 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000193
Chris Lattner017d6aa2007-08-03 16:28:33 +0000194 // If this is an update of elements of a vector, insert them as appropriate.
Chris Lattner6481a572007-08-03 17:31:20 +0000195 if (Dst.isOCUVectorElt())
Chris Lattner017d6aa2007-08-03 16:28:33 +0000196 return EmitStoreThroughOCUComponentLValue(Src, Dst, Ty);
197
198 assert(0 && "FIXME: Don't support store to bitfield yet");
199 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000200
201 llvm::Value *DstAddr = Dst.getAddress();
Chris Lattner883f6a72007-08-11 00:04:45 +0000202 assert(Src.isScalar() && "Can't emit an agg store with this method");
203 // FIXME: Handle volatility etc.
204 const llvm::Type *SrcTy = Src.getVal()->getType();
205 const llvm::Type *AddrTy =
206 cast<llvm::PointerType>(DstAddr->getType())->getElementType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000207
Chris Lattner883f6a72007-08-11 00:04:45 +0000208 if (AddrTy != SrcTy)
209 DstAddr = Builder.CreateBitCast(DstAddr, llvm::PointerType::get(SrcTy),
210 "storetmp");
211 Builder.CreateStore(Src.getVal(), DstAddr);
Reid Spencer5f016e22007-07-11 17:01:13 +0000212}
213
Chris Lattner017d6aa2007-08-03 16:28:33 +0000214void CodeGenFunction::EmitStoreThroughOCUComponentLValue(RValue Src, LValue Dst,
215 QualType Ty) {
216 // This access turns into a read/modify/write of the vector. Load the input
217 // value now.
218 llvm::Value *Vec = Builder.CreateLoad(Dst.getOCUVectorAddr(), "tmp");
219 // FIXME: Volatility.
Chris Lattner6481a572007-08-03 17:31:20 +0000220 unsigned EncFields = Dst.getOCUVectorElts();
Chris Lattner017d6aa2007-08-03 16:28:33 +0000221
222 llvm::Value *SrcVal = Src.getVal();
223
Chris Lattner7e6b51b2007-08-03 16:37:04 +0000224 if (const VectorType *VTy = Ty->getAsVectorType()) {
225 unsigned NumSrcElts = VTy->getNumElements();
226
227 // Extract/Insert each element.
228 for (unsigned i = 0; i != NumSrcElts; ++i) {
229 llvm::Value *Elt = llvm::ConstantInt::get(llvm::Type::Int32Ty, i);
230 Elt = Builder.CreateExtractElement(SrcVal, Elt, "tmp");
231
Chris Lattner6481a572007-08-03 17:31:20 +0000232 unsigned Idx = OCUVectorElementExpr::getAccessedFieldNo(i, EncFields);
Chris Lattner7e6b51b2007-08-03 16:37:04 +0000233 llvm::Value *OutIdx = llvm::ConstantInt::get(llvm::Type::Int32Ty, Idx);
234 Vec = Builder.CreateInsertElement(Vec, Elt, OutIdx, "tmp");
235 }
236 } else {
237 // If the Src is a scalar (not a vector) it must be updating one element.
Chris Lattner6481a572007-08-03 17:31:20 +0000238 unsigned InIdx = OCUVectorElementExpr::getAccessedFieldNo(0, EncFields);
Chris Lattner017d6aa2007-08-03 16:28:33 +0000239 llvm::Value *Elt = llvm::ConstantInt::get(llvm::Type::Int32Ty, InIdx);
240 Vec = Builder.CreateInsertElement(Vec, SrcVal, Elt, "tmp");
Chris Lattner017d6aa2007-08-03 16:28:33 +0000241 }
242
Chris Lattner017d6aa2007-08-03 16:28:33 +0000243 Builder.CreateStore(Vec, Dst.getOCUVectorAddr());
244}
245
Reid Spencer5f016e22007-07-11 17:01:13 +0000246
247LValue CodeGenFunction::EmitDeclRefLValue(const DeclRefExpr *E) {
248 const Decl *D = E->getDecl();
249 if (isa<BlockVarDecl>(D) || isa<ParmVarDecl>(D)) {
250 llvm::Value *V = LocalDeclMap[D];
251 assert(V && "BlockVarDecl not entered in LocalDeclMap?");
252 return LValue::MakeAddr(V);
253 } else if (isa<FunctionDecl>(D) || isa<FileVarDecl>(D)) {
254 return LValue::MakeAddr(CGM.GetAddrOfGlobalDecl(D));
255 }
256 assert(0 && "Unimp declref");
257}
258
259LValue CodeGenFunction::EmitUnaryOpLValue(const UnaryOperator *E) {
260 // __extension__ doesn't affect lvalue-ness.
261 if (E->getOpcode() == UnaryOperator::Extension)
262 return EmitLValue(E->getSubExpr());
263
264 assert(E->getOpcode() == UnaryOperator::Deref &&
265 "'*' is the only unary operator that produces an lvalue");
Chris Lattner7f02f722007-08-24 05:35:26 +0000266 return LValue::MakeAddr(EmitScalarExpr(E->getSubExpr()));
Reid Spencer5f016e22007-07-11 17:01:13 +0000267}
268
269LValue CodeGenFunction::EmitStringLiteralLValue(const StringLiteral *E) {
270 assert(!E->isWide() && "FIXME: Wide strings not supported yet!");
271 const char *StrData = E->getStrData();
272 unsigned Len = E->getByteLength();
273
274 // FIXME: Can cache/reuse these within the module.
275 llvm::Constant *C=llvm::ConstantArray::get(std::string(StrData, StrData+Len));
276
277 // Create a global variable for this.
278 C = new llvm::GlobalVariable(C->getType(), true,
279 llvm::GlobalValue::InternalLinkage,
280 C, ".str", CurFn->getParent());
281 llvm::Constant *Zero = llvm::Constant::getNullValue(llvm::Type::Int32Ty);
282 llvm::Constant *Zeros[] = { Zero, Zero };
283 C = llvm::ConstantExpr::getGetElementPtr(C, Zeros, 2);
284 return LValue::MakeAddr(C);
285}
286
Anders Carlsson22742662007-07-21 05:21:51 +0000287LValue CodeGenFunction::EmitPreDefinedLValue(const PreDefinedExpr *E) {
288 std::string FunctionName(CurFuncDecl->getName());
289 std::string GlobalVarName;
290
291 switch (E->getIdentType()) {
292 default:
293 assert(0 && "unknown pre-defined ident type");
294 case PreDefinedExpr::Func:
295 GlobalVarName = "__func__.";
296 break;
297 case PreDefinedExpr::Function:
298 GlobalVarName = "__FUNCTION__.";
299 break;
300 case PreDefinedExpr::PrettyFunction:
301 // FIXME:: Demangle C++ method names
302 GlobalVarName = "__PRETTY_FUNCTION__.";
303 break;
304 }
305
306 GlobalVarName += CurFuncDecl->getName();
307
308 // FIXME: Can cache/reuse these within the module.
309 llvm::Constant *C=llvm::ConstantArray::get(FunctionName);
310
311 // Create a global variable for this.
312 C = new llvm::GlobalVariable(C->getType(), true,
313 llvm::GlobalValue::InternalLinkage,
314 C, GlobalVarName, CurFn->getParent());
315 llvm::Constant *Zero = llvm::Constant::getNullValue(llvm::Type::Int32Ty);
316 llvm::Constant *Zeros[] = { Zero, Zero };
317 C = llvm::ConstantExpr::getGetElementPtr(C, Zeros, 2);
318 return LValue::MakeAddr(C);
319}
320
Reid Spencer5f016e22007-07-11 17:01:13 +0000321LValue CodeGenFunction::EmitArraySubscriptExpr(const ArraySubscriptExpr *E) {
Ted Kremenek23245122007-08-20 16:18:38 +0000322 // The index must always be an integer, which is not an aggregate. Emit it.
Chris Lattner7f02f722007-08-24 05:35:26 +0000323 llvm::Value *Idx = EmitScalarExpr(E->getIdx());
Reid Spencer5f016e22007-07-11 17:01:13 +0000324
325 // If the base is a vector type, then we are forming a vector element lvalue
326 // with this subscript.
Ted Kremenek23245122007-08-20 16:18:38 +0000327 if (E->getLHS()->getType()->isVectorType()) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000328 // Emit the vector as an lvalue to get its address.
Ted Kremenek23245122007-08-20 16:18:38 +0000329 LValue LHS = EmitLValue(E->getLHS());
330 assert(LHS.isSimple() && "Can only subscript lvalue vectors here!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000331 // FIXME: This should properly sign/zero/extend or truncate Idx to i32.
Ted Kremenek23245122007-08-20 16:18:38 +0000332 return LValue::MakeVectorElt(LHS.getAddress(), Idx);
Reid Spencer5f016e22007-07-11 17:01:13 +0000333 }
334
Ted Kremenek23245122007-08-20 16:18:38 +0000335 // The base must be a pointer, which is not an aggregate. Emit it.
Chris Lattner7f02f722007-08-24 05:35:26 +0000336 llvm::Value *Base = EmitScalarExpr(E->getBase());
Reid Spencer5f016e22007-07-11 17:01:13 +0000337
Ted Kremenek23245122007-08-20 16:18:38 +0000338 // Extend or truncate the index type to 32 or 64-bits.
Chris Lattnerd4f08022007-08-08 17:43:05 +0000339 QualType IdxTy = E->getIdx()->getType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000340 bool IdxSigned = IdxTy->isSignedIntegerType();
341 unsigned IdxBitwidth = cast<llvm::IntegerType>(Idx->getType())->getBitWidth();
342 if (IdxBitwidth != LLVMPointerWidth)
343 Idx = Builder.CreateIntCast(Idx, llvm::IntegerType::get(LLVMPointerWidth),
344 IdxSigned, "idxprom");
345
346 // We know that the pointer points to a type of the correct size, unless the
347 // size is a VLA.
Chris Lattner590b6642007-07-15 23:26:56 +0000348 if (!E->getType()->isConstantSizeType(getContext()))
Reid Spencer5f016e22007-07-11 17:01:13 +0000349 assert(0 && "VLA idx not implemented");
350 return LValue::MakeAddr(Builder.CreateGEP(Base, Idx, "arrayidx"));
351}
352
Chris Lattner349aaec2007-08-02 23:37:31 +0000353LValue CodeGenFunction::
Chris Lattner6481a572007-08-03 17:31:20 +0000354EmitOCUVectorElementExpr(const OCUVectorElementExpr *E) {
Chris Lattner349aaec2007-08-02 23:37:31 +0000355 // Emit the base vector as an l-value.
356 LValue Base = EmitLValue(E->getBase());
357 assert(Base.isSimple() && "Can only subscript lvalue vectors here!");
358
Chris Lattner6481a572007-08-03 17:31:20 +0000359 return LValue::MakeOCUVectorElt(Base.getAddress(),
360 E->getEncodedElementAccess());
Chris Lattner349aaec2007-08-02 23:37:31 +0000361}
362
Reid Spencer5f016e22007-07-11 17:01:13 +0000363//===--------------------------------------------------------------------===//
364// Expression Emission
365//===--------------------------------------------------------------------===//
366
Chris Lattner7016a702007-08-20 22:37:10 +0000367
Reid Spencer5f016e22007-07-11 17:01:13 +0000368RValue CodeGenFunction::EmitCallExpr(const CallExpr *E) {
Anders Carlsson022012e2007-08-20 18:05:56 +0000369 if (const ImplicitCastExpr *IcExpr =
370 dyn_cast<const ImplicitCastExpr>(E->getCallee()))
371 if (const DeclRefExpr *DRExpr =
372 dyn_cast<const DeclRefExpr>(IcExpr->getSubExpr()))
373 if (const FunctionDecl *FDecl =
374 dyn_cast<const FunctionDecl>(DRExpr->getDecl()))
375 if (unsigned builtinID = FDecl->getIdentifier()->getBuiltinID())
376 return EmitBuiltinExpr(builtinID, E);
377
Chris Lattner7f02f722007-08-24 05:35:26 +0000378 llvm::Value *Callee = EmitScalarExpr(E->getCallee());
Chris Lattnerc5e940f2007-08-31 04:44:06 +0000379 return EmitCallExpr(Callee, E);
380}
381
382RValue CodeGenFunction::EmitCallExpr(llvm::Value *Callee, const CallExpr *E) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000383 // The callee type will always be a pointer to function type, get the function
384 // type.
Chris Lattnerd4f08022007-08-08 17:43:05 +0000385 QualType CalleeTy = E->getCallee()->getType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000386 CalleeTy = cast<PointerType>(CalleeTy.getCanonicalType())->getPointeeType();
387
388 // Get information about the argument types.
389 FunctionTypeProto::arg_type_iterator ArgTyIt = 0, ArgTyEnd = 0;
390
391 // Calling unprototyped functions provides no argument info.
392 if (const FunctionTypeProto *FTP = dyn_cast<FunctionTypeProto>(CalleeTy)) {
393 ArgTyIt = FTP->arg_type_begin();
394 ArgTyEnd = FTP->arg_type_end();
395 }
396
397 llvm::SmallVector<llvm::Value*, 16> Args;
398
Chris Lattnercc666af2007-08-10 17:02:28 +0000399 // Handle struct-return functions by passing a pointer to the location that
400 // we would like to return into.
401 if (hasAggregateLLVMType(E->getType())) {
402 // Create a temporary alloca to hold the result of the call. :(
403 Args.push_back(CreateTempAlloca(ConvertType(E->getType())));
404 // FIXME: set the stret attribute on the argument.
405 }
406
Reid Spencer5f016e22007-07-11 17:01:13 +0000407 for (unsigned i = 0, e = E->getNumArgs(); i != e; ++i) {
Chris Lattnerd4f08022007-08-08 17:43:05 +0000408 QualType ArgTy = E->getArg(i)->getType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000409
Chris Lattner660ac122007-08-26 22:55:13 +0000410 if (!hasAggregateLLVMType(ArgTy)) {
411 // Scalar argument is passed by-value.
412 Args.push_back(EmitScalarExpr(E->getArg(i)));
Chris Lattner660ac122007-08-26 22:55:13 +0000413 } else if (ArgTy->isComplexType()) {
414 // Make a temporary alloca to pass the argument.
415 llvm::Value *DestMem = CreateTempAlloca(ConvertType(ArgTy));
416 EmitComplexExprIntoAddr(E->getArg(i), DestMem, false);
417 Args.push_back(DestMem);
418 } else {
419 llvm::Value *DestMem = CreateTempAlloca(ConvertType(ArgTy));
420 EmitAggExpr(E->getArg(i), DestMem, false);
421 Args.push_back(DestMem);
Reid Spencer5f016e22007-07-11 17:01:13 +0000422 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000423 }
424
Chris Lattnerbf986512007-08-01 06:24:52 +0000425 llvm::Value *V = Builder.CreateCall(Callee, &Args[0], &Args[0]+Args.size());
Reid Spencer5f016e22007-07-11 17:01:13 +0000426 if (V->getType() != llvm::Type::VoidTy)
427 V->setName("call");
Chris Lattnercc666af2007-08-10 17:02:28 +0000428 else if (hasAggregateLLVMType(E->getType()))
429 // Struct return.
430 return RValue::getAggregate(Args[0]);
431
Reid Spencer5f016e22007-07-11 17:01:13 +0000432 return RValue::get(V);
433}