blob: 2981ed609a2961d35df1a08947e4db24664fc15e [file] [log] [blame]
Chris Lattnere47e4402007-06-01 18:02:12 +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"
Chris Lattnerb6984c42007-06-20 04:44:43 +000015#include "CodeGenModule.h"
Chris Lattnere47e4402007-06-01 18:02:12 +000016#include "clang/AST/AST.h"
Anders Carlsson1d8e5212007-08-20 18:05:56 +000017#include "clang/Lex/IdentifierTable.h"
Chris Lattnere47e4402007-06-01 18:02:12 +000018#include "llvm/Constants.h"
19#include "llvm/DerivedTypes.h"
Chris Lattner4347e3692007-06-06 04:54:52 +000020#include "llvm/Function.h"
21#include "llvm/GlobalVariable.h"
Chris Lattner651f0e92007-07-16 05:43:05 +000022#include "llvm/Support/MathExtras.h"
Chris Lattnere47e4402007-06-01 18:02:12 +000023using namespace clang;
24using namespace CodeGen;
25
Chris Lattnerd7f58862007-06-02 05:24:33 +000026//===--------------------------------------------------------------------===//
Chris Lattnerf0106d22007-06-02 19:33:17 +000027// Miscellaneous Helper Methods
28//===--------------------------------------------------------------------===//
29
Chris Lattnere9a64532007-06-22 21:44:33 +000030/// 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}
Chris Lattner8394d792007-06-05 20:53:16 +000036
37/// EvaluateExprAsBool - Perform the usual unary conversions on the specified
38/// expression and compare the result against zero, returning an Int1Ty value.
Chris Lattner23b7eb62007-06-15 23:05:46 +000039llvm::Value *CodeGenFunction::EvaluateExprAsBool(const Expr *E) {
Chris Lattner268fcce2007-08-26 16:46:58 +000040 QualType BoolTy = getContext().BoolTy;
41 if (!E->getType()->isComplexType())
42 return EmitScalarConversion(EmitScalarExpr(E), E->getType(), BoolTy);
Chris Lattner8394d792007-06-05 20:53:16 +000043
Chris Lattner268fcce2007-08-26 16:46:58 +000044 return EmitComplexToScalarConversion(EmitComplexExpr(E), E->getType(),BoolTy);
Chris Lattnerf0106d22007-06-02 19:33:17 +000045}
46
Chris Lattnera45c5af2007-06-02 19:47:04 +000047//===----------------------------------------------------------------------===//
Chris Lattnerd7f58862007-06-02 05:24:33 +000048// LValue Expression Emission
Chris Lattnera45c5af2007-06-02 19:47:04 +000049//===----------------------------------------------------------------------===//
Chris Lattnerd7f58862007-06-02 05:24:33 +000050
Chris Lattner8394d792007-06-05 20:53:16 +000051/// 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///
Chris Lattnerd7f58862007-06-02 05:24:33 +000067LValue CodeGenFunction::EmitLValue(const Expr *E) {
68 switch (E->getStmtClass()) {
Chris Lattnerc7f06ca2007-08-26 05:06:40 +000069 default: {
Chris Lattner8394d792007-06-05 20:53:16 +000070 fprintf(stderr, "Unimplemented lvalue expr!\n");
Chris Lattnerd7f58862007-06-02 05:24:33 +000071 E->dump();
Chris Lattnerc7f06ca2007-08-26 05:06:40 +000072 llvm::Type *Ty = llvm::PointerType::get(ConvertType(E->getType()));
73 return LValue::MakeAddr(llvm::UndefValue::get(Ty));
74 }
Chris Lattnerd7f58862007-06-02 05:24:33 +000075
76 case Expr::DeclRefExprClass: return EmitDeclRefLValue(cast<DeclRefExpr>(E));
Chris Lattner946aa312007-06-05 03:59:43 +000077 case Expr::ParenExprClass:return EmitLValue(cast<ParenExpr>(E)->getSubExpr());
Anders Carlsson625bfc82007-07-21 05:21:51 +000078 case Expr::PreDefinedExprClass:
79 return EmitPreDefinedLValue(cast<PreDefinedExpr>(E));
Chris Lattner4347e3692007-06-06 04:54:52 +000080 case Expr::StringLiteralClass:
81 return EmitStringLiteralLValue(cast<StringLiteral>(E));
Chris Lattner8394d792007-06-05 20:53:16 +000082
83 case Expr::UnaryOperatorClass:
84 return EmitUnaryOpLValue(cast<UnaryOperator>(E));
Chris Lattnerd9d2fb12007-06-08 23:31:14 +000085 case Expr::ArraySubscriptExprClass:
86 return EmitArraySubscriptExpr(cast<ArraySubscriptExpr>(E));
Chris Lattnerd268a7a2007-08-03 17:31:20 +000087 case Expr::OCUVectorElementExprClass:
88 return EmitOCUVectorElementExpr(cast<OCUVectorElementExpr>(E));
Chris Lattnerd7f58862007-06-02 05:24:33 +000089 }
90}
91
Chris Lattner8394d792007-06-05 20:53:16 +000092/// 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.
Chris Lattner9369a562007-06-29 16:31:29 +000095RValue CodeGenFunction::EmitLoadOfLValue(LValue LV, QualType ExprType) {
Chris Lattner08c4b9f2007-07-10 21:17:59 +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 Lattner6278e6a2007-08-11 00:04:45 +0000105 assert(ExprType->isFunctionType() && "Unknown scalar value");
106 return RValue::get(Ptr);
Chris Lattner08c4b9f2007-07-10 21:17:59 +0000107 }
Chris Lattner09153c02007-06-22 18:48:09 +0000108
Chris Lattner08c4b9f2007-07-10 21:17:59 +0000109 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 Lattner73ab9b32007-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 Lattnerd268a7a2007-08-03 17:31:20 +0000117 if (LV.isOCUVectorElt())
118 return EmitLoadOfOCUElementLValue(LV, ExprType);
Chris Lattner09153c02007-06-22 18:48:09 +0000119
Chris Lattner08c4b9f2007-07-10 21:17:59 +0000120 assert(0 && "Bitfield ref not impl!");
Chris Lattner8394d792007-06-05 20:53:16 +0000121}
122
Chris Lattner40ff7012007-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 Lattnerd268a7a2007-08-03 17:31:20 +0000125RValue CodeGenFunction::EmitLoadOfOCUElementLValue(LValue LV,
Chris Lattner8eab8ff2007-08-10 17:10:08 +0000126 QualType ExprType) {
Chris Lattner40ff7012007-08-03 16:18:34 +0000127 llvm::Value *Vec = Builder.CreateLoad(LV.getOCUVectorAddr(), "tmp");
128
Chris Lattnerd268a7a2007-08-03 17:31:20 +0000129 unsigned EncFields = LV.getOCUVectorElts();
Chris Lattner40ff7012007-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 Lattner8eab8ff2007-08-10 17:10:08 +0000133 const VectorType *ExprVT = ExprType->getAsVectorType();
134 if (!ExprVT) {
Chris Lattnerd268a7a2007-08-03 17:31:20 +0000135 unsigned InIdx = OCUVectorElementExpr::getAccessedFieldNo(0, EncFields);
Chris Lattner40ff7012007-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 Lattner8eab8ff2007-08-10 17:10:08 +0000142 unsigned NumResultElts = ExprVT->getNumElements();
Chris Lattner40ff7012007-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 Lattnerd268a7a2007-08-03 17:31:20 +0000149 unsigned InIdx = OCUVectorElementExpr::getAccessedFieldNo(i, EncFields);
Chris Lattner40ff7012007-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 Lattnerd268a7a2007-08-03 17:31:20 +0000165 unsigned InIdx = OCUVectorElementExpr::getAccessedFieldNo(i, EncFields);
Chris Lattner40ff7012007-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
Chris Lattner9369a562007-06-29 16:31:29 +0000177
Chris Lattner8394d792007-06-05 20:53:16 +0000178/// 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 Lattner41d480e2007-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 }
Chris Lattner08c4b9f2007-07-10 21:17:59 +0000193
Chris Lattner41d480e2007-08-03 16:28:33 +0000194 // If this is an update of elements of a vector, insert them as appropriate.
Chris Lattnerd268a7a2007-08-03 17:31:20 +0000195 if (Dst.isOCUVectorElt())
Chris Lattner41d480e2007-08-03 16:28:33 +0000196 return EmitStoreThroughOCUComponentLValue(Src, Dst, Ty);
197
198 assert(0 && "FIXME: Don't support store to bitfield yet");
199 }
Chris Lattner8394d792007-06-05 20:53:16 +0000200
Chris Lattner09153c02007-06-22 18:48:09 +0000201 llvm::Value *DstAddr = Dst.getAddress();
Chris Lattner6278e6a2007-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();
Chris Lattner8394d792007-06-05 20:53:16 +0000207
Chris Lattner6278e6a2007-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);
Chris Lattner8394d792007-06-05 20:53:16 +0000212}
213
Chris Lattner41d480e2007-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 Lattnerd268a7a2007-08-03 17:31:20 +0000220 unsigned EncFields = Dst.getOCUVectorElts();
Chris Lattner41d480e2007-08-03 16:28:33 +0000221
222 llvm::Value *SrcVal = Src.getVal();
223
Chris Lattner3a44aa72007-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 Lattnerd268a7a2007-08-03 17:31:20 +0000232 unsigned Idx = OCUVectorElementExpr::getAccessedFieldNo(i, EncFields);
Chris Lattner3a44aa72007-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 Lattnerd268a7a2007-08-03 17:31:20 +0000238 unsigned InIdx = OCUVectorElementExpr::getAccessedFieldNo(0, EncFields);
Chris Lattner41d480e2007-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 Lattner41d480e2007-08-03 16:28:33 +0000241 }
242
Chris Lattner41d480e2007-08-03 16:28:33 +0000243 Builder.CreateStore(Vec, Dst.getOCUVectorAddr());
244}
245
Chris Lattnerd7f58862007-06-02 05:24:33 +0000246
247LValue CodeGenFunction::EmitDeclRefLValue(const DeclRefExpr *E) {
248 const Decl *D = E->getDecl();
Chris Lattner53621a52007-06-13 20:44:40 +0000249 if (isa<BlockVarDecl>(D) || isa<ParmVarDecl>(D)) {
Chris Lattner23b7eb62007-06-15 23:05:46 +0000250 llvm::Value *V = LocalDeclMap[D];
Chris Lattnerd7f58862007-06-02 05:24:33 +0000251 assert(V && "BlockVarDecl not entered in LocalDeclMap?");
Chris Lattner08c4b9f2007-07-10 21:17:59 +0000252 return LValue::MakeAddr(V);
Chris Lattnerb6984c42007-06-20 04:44:43 +0000253 } else if (isa<FunctionDecl>(D) || isa<FileVarDecl>(D)) {
Chris Lattner08c4b9f2007-07-10 21:17:59 +0000254 return LValue::MakeAddr(CGM.GetAddrOfGlobalDecl(D));
Chris Lattnerd7f58862007-06-02 05:24:33 +0000255 }
256 assert(0 && "Unimp declref");
257}
Chris Lattnere47e4402007-06-01 18:02:12 +0000258
Chris Lattner8394d792007-06-05 20:53:16 +0000259LValue 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 Lattner2da04b32007-08-24 05:35:26 +0000266 return LValue::MakeAddr(EmitScalarExpr(E->getSubExpr()));
Chris Lattner8394d792007-06-05 20:53:16 +0000267}
268
Chris Lattner4347e3692007-06-06 04:54:52 +0000269LValue 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.
Chris Lattner23b7eb62007-06-15 23:05:46 +0000275 llvm::Constant *C=llvm::ConstantArray::get(std::string(StrData, StrData+Len));
Chris Lattner4347e3692007-06-06 04:54:52 +0000276
277 // Create a global variable for this.
Chris Lattner23b7eb62007-06-15 23:05:46 +0000278 C = new llvm::GlobalVariable(C->getType(), true,
279 llvm::GlobalValue::InternalLinkage,
Chris Lattner4347e3692007-06-06 04:54:52 +0000280 C, ".str", CurFn->getParent());
Chris Lattner23b7eb62007-06-15 23:05:46 +0000281 llvm::Constant *Zero = llvm::Constant::getNullValue(llvm::Type::Int32Ty);
282 llvm::Constant *Zeros[] = { Zero, Zero };
283 C = llvm::ConstantExpr::getGetElementPtr(C, Zeros, 2);
Chris Lattner08c4b9f2007-07-10 21:17:59 +0000284 return LValue::MakeAddr(C);
Chris Lattner4347e3692007-06-06 04:54:52 +0000285}
286
Anders Carlsson625bfc82007-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
Chris Lattnerd9d2fb12007-06-08 23:31:14 +0000321LValue CodeGenFunction::EmitArraySubscriptExpr(const ArraySubscriptExpr *E) {
Ted Kremenekc81614d2007-08-20 16:18:38 +0000322 // The index must always be an integer, which is not an aggregate. Emit it.
Chris Lattner2da04b32007-08-24 05:35:26 +0000323 llvm::Value *Idx = EmitScalarExpr(E->getIdx());
Chris Lattnerd9d2fb12007-06-08 23:31:14 +0000324
Chris Lattner08c4b9f2007-07-10 21:17:59 +0000325 // If the base is a vector type, then we are forming a vector element lvalue
326 // with this subscript.
Ted Kremenekc81614d2007-08-20 16:18:38 +0000327 if (E->getLHS()->getType()->isVectorType()) {
Chris Lattner08c4b9f2007-07-10 21:17:59 +0000328 // Emit the vector as an lvalue to get its address.
Ted Kremenekc81614d2007-08-20 16:18:38 +0000329 LValue LHS = EmitLValue(E->getLHS());
330 assert(LHS.isSimple() && "Can only subscript lvalue vectors here!");
Chris Lattner08c4b9f2007-07-10 21:17:59 +0000331 // FIXME: This should properly sign/zero/extend or truncate Idx to i32.
Ted Kremenekc81614d2007-08-20 16:18:38 +0000332 return LValue::MakeVectorElt(LHS.getAddress(), Idx);
Chris Lattner08c4b9f2007-07-10 21:17:59 +0000333 }
334
Ted Kremenekc81614d2007-08-20 16:18:38 +0000335 // The base must be a pointer, which is not an aggregate. Emit it.
Chris Lattner2da04b32007-08-24 05:35:26 +0000336 llvm::Value *Base = EmitScalarExpr(E->getBase());
Chris Lattner08c4b9f2007-07-10 21:17:59 +0000337
Ted Kremenekc81614d2007-08-20 16:18:38 +0000338 // Extend or truncate the index type to 32 or 64-bits.
Chris Lattner5ebb2fe2007-08-08 17:43:05 +0000339 QualType IdxTy = E->getIdx()->getType();
Chris Lattnerd9d2fb12007-06-08 23:31:14 +0000340 bool IdxSigned = IdxTy->isSignedIntegerType();
Chris Lattner23b7eb62007-06-15 23:05:46 +0000341 unsigned IdxBitwidth = cast<llvm::IntegerType>(Idx->getType())->getBitWidth();
Chris Lattnerd9d2fb12007-06-08 23:31:14 +0000342 if (IdxBitwidth != LLVMPointerWidth)
Chris Lattner23b7eb62007-06-15 23:05:46 +0000343 Idx = Builder.CreateIntCast(Idx, llvm::IntegerType::get(LLVMPointerWidth),
Chris Lattnerd9d2fb12007-06-08 23:31:14 +0000344 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 Lattner0e9d6222007-07-15 23:26:56 +0000348 if (!E->getType()->isConstantSizeType(getContext()))
Chris Lattnerd9d2fb12007-06-08 23:31:14 +0000349 assert(0 && "VLA idx not implemented");
Chris Lattner08c4b9f2007-07-10 21:17:59 +0000350 return LValue::MakeAddr(Builder.CreateGEP(Base, Idx, "arrayidx"));
Chris Lattnerd9d2fb12007-06-08 23:31:14 +0000351}
352
Chris Lattner9e751ca2007-08-02 23:37:31 +0000353LValue CodeGenFunction::
Chris Lattnerd268a7a2007-08-03 17:31:20 +0000354EmitOCUVectorElementExpr(const OCUVectorElementExpr *E) {
Chris Lattner9e751ca2007-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 Lattnerd268a7a2007-08-03 17:31:20 +0000359 return LValue::MakeOCUVectorElt(Base.getAddress(),
360 E->getEncodedElementAccess());
Chris Lattner9e751ca2007-08-02 23:37:31 +0000361}
362
Chris Lattnere47e4402007-06-01 18:02:12 +0000363//===--------------------------------------------------------------------===//
364// Expression Emission
365//===--------------------------------------------------------------------===//
366
Chris Lattner76ba8492007-08-20 22:37:10 +0000367
Chris Lattner2b228c92007-06-15 21:34:29 +0000368RValue CodeGenFunction::EmitCallExpr(const CallExpr *E) {
Anders Carlsson1d8e5212007-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 Lattner2da04b32007-08-24 05:35:26 +0000378 llvm::Value *Callee = EmitScalarExpr(E->getCallee());
Chris Lattnerc14236b2007-07-10 22:18:37 +0000379
380 // The callee type will always be a pointer to function type, get the function
381 // type.
Chris Lattner5ebb2fe2007-08-08 17:43:05 +0000382 QualType CalleeTy = E->getCallee()->getType();
Chris Lattnerc14236b2007-07-10 22:18:37 +0000383 CalleeTy = cast<PointerType>(CalleeTy.getCanonicalType())->getPointeeType();
384
385 // Get information about the argument types.
386 FunctionTypeProto::arg_type_iterator ArgTyIt = 0, ArgTyEnd = 0;
387
388 // Calling unprototyped functions provides no argument info.
389 if (const FunctionTypeProto *FTP = dyn_cast<FunctionTypeProto>(CalleeTy)) {
390 ArgTyIt = FTP->arg_type_begin();
391 ArgTyEnd = FTP->arg_type_end();
392 }
Chris Lattner2b228c92007-06-15 21:34:29 +0000393
Chris Lattner23b7eb62007-06-15 23:05:46 +0000394 llvm::SmallVector<llvm::Value*, 16> Args;
Chris Lattner2b228c92007-06-15 21:34:29 +0000395
Chris Lattner90d91202007-08-10 17:02:28 +0000396 // Handle struct-return functions by passing a pointer to the location that
397 // we would like to return into.
398 if (hasAggregateLLVMType(E->getType())) {
399 // Create a temporary alloca to hold the result of the call. :(
400 Args.push_back(CreateTempAlloca(ConvertType(E->getType())));
401 // FIXME: set the stret attribute on the argument.
402 }
403
Chris Lattner2b228c92007-06-15 21:34:29 +0000404 for (unsigned i = 0, e = E->getNumArgs(); i != e; ++i) {
Chris Lattner5ebb2fe2007-08-08 17:43:05 +0000405 QualType ArgTy = E->getArg(i)->getType();
Chris Lattnerc14236b2007-07-10 22:18:37 +0000406
Chris Lattnera811da52007-08-26 22:55:13 +0000407 if (!hasAggregateLLVMType(ArgTy)) {
408 // Scalar argument is passed by-value.
409 Args.push_back(EmitScalarExpr(E->getArg(i)));
Chris Lattnera811da52007-08-26 22:55:13 +0000410 } else if (ArgTy->isComplexType()) {
411 // Make a temporary alloca to pass the argument.
412 llvm::Value *DestMem = CreateTempAlloca(ConvertType(ArgTy));
413 EmitComplexExprIntoAddr(E->getArg(i), DestMem, false);
414 Args.push_back(DestMem);
415 } else {
416 llvm::Value *DestMem = CreateTempAlloca(ConvertType(ArgTy));
417 EmitAggExpr(E->getArg(i), DestMem, false);
418 Args.push_back(DestMem);
Chris Lattnerc14236b2007-07-10 22:18:37 +0000419 }
Chris Lattner2b228c92007-06-15 21:34:29 +0000420 }
421
Chris Lattner7b9f04e2007-08-01 06:24:52 +0000422 llvm::Value *V = Builder.CreateCall(Callee, &Args[0], &Args[0]+Args.size());
Chris Lattner2b228c92007-06-15 21:34:29 +0000423 if (V->getType() != llvm::Type::VoidTy)
424 V->setName("call");
Chris Lattner90d91202007-08-10 17:02:28 +0000425 else if (hasAggregateLLVMType(E->getType()))
426 // Struct return.
427 return RValue::getAggregate(Args[0]);
428
Chris Lattner2b228c92007-06-15 21:34:29 +0000429 return RValue::get(V);
430}