blob: 549402892f8cda8d10d0a435ffe4bd9d6474430c [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 +0000177RValue CodeGenFunction::EmitLoadOfLValue(const Expr *E) {
178 return EmitLoadOfLValue(EmitLValue(E), E->getType());
179}
180
181
Chris Lattner8394d792007-06-05 20:53:16 +0000182/// EmitStoreThroughLValue - Store the specified rvalue into the specified
183/// lvalue, where both are guaranteed to the have the same type, and that type
184/// is 'Ty'.
185void CodeGenFunction::EmitStoreThroughLValue(RValue Src, LValue Dst,
186 QualType Ty) {
Chris Lattner41d480e2007-08-03 16:28:33 +0000187 if (!Dst.isSimple()) {
188 if (Dst.isVectorElt()) {
189 // Read/modify/write the vector, inserting the new element.
190 // FIXME: Volatility.
191 llvm::Value *Vec = Builder.CreateLoad(Dst.getVectorAddr(), "tmp");
192 Vec = Builder.CreateInsertElement(Vec, Src.getVal(),
193 Dst.getVectorIdx(), "vecins");
194 Builder.CreateStore(Vec, Dst.getVectorAddr());
195 return;
196 }
Chris Lattner08c4b9f2007-07-10 21:17:59 +0000197
Chris Lattner41d480e2007-08-03 16:28:33 +0000198 // If this is an update of elements of a vector, insert them as appropriate.
Chris Lattnerd268a7a2007-08-03 17:31:20 +0000199 if (Dst.isOCUVectorElt())
Chris Lattner41d480e2007-08-03 16:28:33 +0000200 return EmitStoreThroughOCUComponentLValue(Src, Dst, Ty);
201
202 assert(0 && "FIXME: Don't support store to bitfield yet");
203 }
Chris Lattner8394d792007-06-05 20:53:16 +0000204
Chris Lattner09153c02007-06-22 18:48:09 +0000205 llvm::Value *DstAddr = Dst.getAddress();
Chris Lattner6278e6a2007-08-11 00:04:45 +0000206 assert(Src.isScalar() && "Can't emit an agg store with this method");
207 // FIXME: Handle volatility etc.
208 const llvm::Type *SrcTy = Src.getVal()->getType();
209 const llvm::Type *AddrTy =
210 cast<llvm::PointerType>(DstAddr->getType())->getElementType();
Chris Lattner8394d792007-06-05 20:53:16 +0000211
Chris Lattner6278e6a2007-08-11 00:04:45 +0000212 if (AddrTy != SrcTy)
213 DstAddr = Builder.CreateBitCast(DstAddr, llvm::PointerType::get(SrcTy),
214 "storetmp");
215 Builder.CreateStore(Src.getVal(), DstAddr);
Chris Lattner8394d792007-06-05 20:53:16 +0000216}
217
Chris Lattner41d480e2007-08-03 16:28:33 +0000218void CodeGenFunction::EmitStoreThroughOCUComponentLValue(RValue Src, LValue Dst,
219 QualType Ty) {
220 // This access turns into a read/modify/write of the vector. Load the input
221 // value now.
222 llvm::Value *Vec = Builder.CreateLoad(Dst.getOCUVectorAddr(), "tmp");
223 // FIXME: Volatility.
Chris Lattnerd268a7a2007-08-03 17:31:20 +0000224 unsigned EncFields = Dst.getOCUVectorElts();
Chris Lattner41d480e2007-08-03 16:28:33 +0000225
226 llvm::Value *SrcVal = Src.getVal();
227
Chris Lattner3a44aa72007-08-03 16:37:04 +0000228 if (const VectorType *VTy = Ty->getAsVectorType()) {
229 unsigned NumSrcElts = VTy->getNumElements();
230
231 // Extract/Insert each element.
232 for (unsigned i = 0; i != NumSrcElts; ++i) {
233 llvm::Value *Elt = llvm::ConstantInt::get(llvm::Type::Int32Ty, i);
234 Elt = Builder.CreateExtractElement(SrcVal, Elt, "tmp");
235
Chris Lattnerd268a7a2007-08-03 17:31:20 +0000236 unsigned Idx = OCUVectorElementExpr::getAccessedFieldNo(i, EncFields);
Chris Lattner3a44aa72007-08-03 16:37:04 +0000237 llvm::Value *OutIdx = llvm::ConstantInt::get(llvm::Type::Int32Ty, Idx);
238 Vec = Builder.CreateInsertElement(Vec, Elt, OutIdx, "tmp");
239 }
240 } else {
241 // If the Src is a scalar (not a vector) it must be updating one element.
Chris Lattnerd268a7a2007-08-03 17:31:20 +0000242 unsigned InIdx = OCUVectorElementExpr::getAccessedFieldNo(0, EncFields);
Chris Lattner41d480e2007-08-03 16:28:33 +0000243 llvm::Value *Elt = llvm::ConstantInt::get(llvm::Type::Int32Ty, InIdx);
244 Vec = Builder.CreateInsertElement(Vec, SrcVal, Elt, "tmp");
Chris Lattner41d480e2007-08-03 16:28:33 +0000245 }
246
Chris Lattner41d480e2007-08-03 16:28:33 +0000247 Builder.CreateStore(Vec, Dst.getOCUVectorAddr());
248}
249
Chris Lattnerd7f58862007-06-02 05:24:33 +0000250
251LValue CodeGenFunction::EmitDeclRefLValue(const DeclRefExpr *E) {
252 const Decl *D = E->getDecl();
Chris Lattner53621a52007-06-13 20:44:40 +0000253 if (isa<BlockVarDecl>(D) || isa<ParmVarDecl>(D)) {
Chris Lattner23b7eb62007-06-15 23:05:46 +0000254 llvm::Value *V = LocalDeclMap[D];
Chris Lattnerd7f58862007-06-02 05:24:33 +0000255 assert(V && "BlockVarDecl not entered in LocalDeclMap?");
Chris Lattner08c4b9f2007-07-10 21:17:59 +0000256 return LValue::MakeAddr(V);
Chris Lattnerb6984c42007-06-20 04:44:43 +0000257 } else if (isa<FunctionDecl>(D) || isa<FileVarDecl>(D)) {
Chris Lattner08c4b9f2007-07-10 21:17:59 +0000258 return LValue::MakeAddr(CGM.GetAddrOfGlobalDecl(D));
Chris Lattnerd7f58862007-06-02 05:24:33 +0000259 }
260 assert(0 && "Unimp declref");
261}
Chris Lattnere47e4402007-06-01 18:02:12 +0000262
Chris Lattner8394d792007-06-05 20:53:16 +0000263LValue CodeGenFunction::EmitUnaryOpLValue(const UnaryOperator *E) {
264 // __extension__ doesn't affect lvalue-ness.
265 if (E->getOpcode() == UnaryOperator::Extension)
266 return EmitLValue(E->getSubExpr());
267
268 assert(E->getOpcode() == UnaryOperator::Deref &&
269 "'*' is the only unary operator that produces an lvalue");
Chris Lattner2da04b32007-08-24 05:35:26 +0000270 return LValue::MakeAddr(EmitScalarExpr(E->getSubExpr()));
Chris Lattner8394d792007-06-05 20:53:16 +0000271}
272
Chris Lattner4347e3692007-06-06 04:54:52 +0000273LValue CodeGenFunction::EmitStringLiteralLValue(const StringLiteral *E) {
274 assert(!E->isWide() && "FIXME: Wide strings not supported yet!");
275 const char *StrData = E->getStrData();
276 unsigned Len = E->getByteLength();
277
278 // FIXME: Can cache/reuse these within the module.
Chris Lattner23b7eb62007-06-15 23:05:46 +0000279 llvm::Constant *C=llvm::ConstantArray::get(std::string(StrData, StrData+Len));
Chris Lattner4347e3692007-06-06 04:54:52 +0000280
281 // Create a global variable for this.
Chris Lattner23b7eb62007-06-15 23:05:46 +0000282 C = new llvm::GlobalVariable(C->getType(), true,
283 llvm::GlobalValue::InternalLinkage,
Chris Lattner4347e3692007-06-06 04:54:52 +0000284 C, ".str", CurFn->getParent());
Chris Lattner23b7eb62007-06-15 23:05:46 +0000285 llvm::Constant *Zero = llvm::Constant::getNullValue(llvm::Type::Int32Ty);
286 llvm::Constant *Zeros[] = { Zero, Zero };
287 C = llvm::ConstantExpr::getGetElementPtr(C, Zeros, 2);
Chris Lattner08c4b9f2007-07-10 21:17:59 +0000288 return LValue::MakeAddr(C);
Chris Lattner4347e3692007-06-06 04:54:52 +0000289}
290
Anders Carlsson625bfc82007-07-21 05:21:51 +0000291LValue CodeGenFunction::EmitPreDefinedLValue(const PreDefinedExpr *E) {
292 std::string FunctionName(CurFuncDecl->getName());
293 std::string GlobalVarName;
294
295 switch (E->getIdentType()) {
296 default:
297 assert(0 && "unknown pre-defined ident type");
298 case PreDefinedExpr::Func:
299 GlobalVarName = "__func__.";
300 break;
301 case PreDefinedExpr::Function:
302 GlobalVarName = "__FUNCTION__.";
303 break;
304 case PreDefinedExpr::PrettyFunction:
305 // FIXME:: Demangle C++ method names
306 GlobalVarName = "__PRETTY_FUNCTION__.";
307 break;
308 }
309
310 GlobalVarName += CurFuncDecl->getName();
311
312 // FIXME: Can cache/reuse these within the module.
313 llvm::Constant *C=llvm::ConstantArray::get(FunctionName);
314
315 // Create a global variable for this.
316 C = new llvm::GlobalVariable(C->getType(), true,
317 llvm::GlobalValue::InternalLinkage,
318 C, GlobalVarName, CurFn->getParent());
319 llvm::Constant *Zero = llvm::Constant::getNullValue(llvm::Type::Int32Ty);
320 llvm::Constant *Zeros[] = { Zero, Zero };
321 C = llvm::ConstantExpr::getGetElementPtr(C, Zeros, 2);
322 return LValue::MakeAddr(C);
323}
324
Chris Lattnerd9d2fb12007-06-08 23:31:14 +0000325LValue CodeGenFunction::EmitArraySubscriptExpr(const ArraySubscriptExpr *E) {
Ted Kremenekc81614d2007-08-20 16:18:38 +0000326 // The index must always be an integer, which is not an aggregate. Emit it.
Chris Lattner2da04b32007-08-24 05:35:26 +0000327 llvm::Value *Idx = EmitScalarExpr(E->getIdx());
Chris Lattnerd9d2fb12007-06-08 23:31:14 +0000328
Chris Lattner08c4b9f2007-07-10 21:17:59 +0000329 // If the base is a vector type, then we are forming a vector element lvalue
330 // with this subscript.
Ted Kremenekc81614d2007-08-20 16:18:38 +0000331 if (E->getLHS()->getType()->isVectorType()) {
Chris Lattner08c4b9f2007-07-10 21:17:59 +0000332 // Emit the vector as an lvalue to get its address.
Ted Kremenekc81614d2007-08-20 16:18:38 +0000333 LValue LHS = EmitLValue(E->getLHS());
334 assert(LHS.isSimple() && "Can only subscript lvalue vectors here!");
Chris Lattner08c4b9f2007-07-10 21:17:59 +0000335 // FIXME: This should properly sign/zero/extend or truncate Idx to i32.
Ted Kremenekc81614d2007-08-20 16:18:38 +0000336 return LValue::MakeVectorElt(LHS.getAddress(), Idx);
Chris Lattner08c4b9f2007-07-10 21:17:59 +0000337 }
338
Ted Kremenekc81614d2007-08-20 16:18:38 +0000339 // The base must be a pointer, which is not an aggregate. Emit it.
Chris Lattner2da04b32007-08-24 05:35:26 +0000340 llvm::Value *Base = EmitScalarExpr(E->getBase());
Chris Lattner08c4b9f2007-07-10 21:17:59 +0000341
Ted Kremenekc81614d2007-08-20 16:18:38 +0000342 // Extend or truncate the index type to 32 or 64-bits.
Chris Lattner5ebb2fe2007-08-08 17:43:05 +0000343 QualType IdxTy = E->getIdx()->getType();
Chris Lattnerd9d2fb12007-06-08 23:31:14 +0000344 bool IdxSigned = IdxTy->isSignedIntegerType();
Chris Lattner23b7eb62007-06-15 23:05:46 +0000345 unsigned IdxBitwidth = cast<llvm::IntegerType>(Idx->getType())->getBitWidth();
Chris Lattnerd9d2fb12007-06-08 23:31:14 +0000346 if (IdxBitwidth != LLVMPointerWidth)
Chris Lattner23b7eb62007-06-15 23:05:46 +0000347 Idx = Builder.CreateIntCast(Idx, llvm::IntegerType::get(LLVMPointerWidth),
Chris Lattnerd9d2fb12007-06-08 23:31:14 +0000348 IdxSigned, "idxprom");
349
350 // We know that the pointer points to a type of the correct size, unless the
351 // size is a VLA.
Chris Lattner0e9d6222007-07-15 23:26:56 +0000352 if (!E->getType()->isConstantSizeType(getContext()))
Chris Lattnerd9d2fb12007-06-08 23:31:14 +0000353 assert(0 && "VLA idx not implemented");
Chris Lattner08c4b9f2007-07-10 21:17:59 +0000354 return LValue::MakeAddr(Builder.CreateGEP(Base, Idx, "arrayidx"));
Chris Lattnerd9d2fb12007-06-08 23:31:14 +0000355}
356
Chris Lattner9e751ca2007-08-02 23:37:31 +0000357LValue CodeGenFunction::
Chris Lattnerd268a7a2007-08-03 17:31:20 +0000358EmitOCUVectorElementExpr(const OCUVectorElementExpr *E) {
Chris Lattner9e751ca2007-08-02 23:37:31 +0000359 // Emit the base vector as an l-value.
360 LValue Base = EmitLValue(E->getBase());
361 assert(Base.isSimple() && "Can only subscript lvalue vectors here!");
362
Chris Lattnerd268a7a2007-08-03 17:31:20 +0000363 return LValue::MakeOCUVectorElt(Base.getAddress(),
364 E->getEncodedElementAccess());
Chris Lattner9e751ca2007-08-02 23:37:31 +0000365}
366
Chris Lattnere47e4402007-06-01 18:02:12 +0000367//===--------------------------------------------------------------------===//
368// Expression Emission
369//===--------------------------------------------------------------------===//
370
Chris Lattner08b15df2007-08-23 23:43:33 +0000371/// EmitAnyExpr - Emit an expression of any type: scalar, complex, aggregate,
372/// returning an rvalue corresponding to it. If NeedResult is false, the
373/// result of the expression doesn't need to be generated into memory.
374RValue CodeGenFunction::EmitAnyExpr(const Expr *E, bool NeedResult) {
375 if (!hasAggregateLLVMType(E->getType()))
Chris Lattner2da04b32007-08-24 05:35:26 +0000376 return RValue::get(EmitScalarExpr(E));
Chris Lattner08b15df2007-08-23 23:43:33 +0000377
378 llvm::Value *DestMem = 0;
379 if (NeedResult)
380 DestMem = CreateTempAlloca(ConvertType(E->getType()));
381
382 if (!E->getType()->isComplexType()) {
383 EmitAggExpr(E, DestMem, false);
384 } else if (NeedResult)
Chris Lattnerb84bb952007-08-26 16:22:13 +0000385 EmitComplexExprIntoAddr(E, DestMem, false);
Chris Lattner08b15df2007-08-23 23:43:33 +0000386 else
387 EmitComplexExpr(E);
388
389 return RValue::getAggregate(DestMem);
390}
391
Chris Lattner76ba8492007-08-20 22:37:10 +0000392
Chris Lattner2b228c92007-06-15 21:34:29 +0000393RValue CodeGenFunction::EmitCallExpr(const CallExpr *E) {
Anders Carlsson1d8e5212007-08-20 18:05:56 +0000394 if (const ImplicitCastExpr *IcExpr =
395 dyn_cast<const ImplicitCastExpr>(E->getCallee()))
396 if (const DeclRefExpr *DRExpr =
397 dyn_cast<const DeclRefExpr>(IcExpr->getSubExpr()))
398 if (const FunctionDecl *FDecl =
399 dyn_cast<const FunctionDecl>(DRExpr->getDecl()))
400 if (unsigned builtinID = FDecl->getIdentifier()->getBuiltinID())
401 return EmitBuiltinExpr(builtinID, E);
402
Chris Lattner2da04b32007-08-24 05:35:26 +0000403 llvm::Value *Callee = EmitScalarExpr(E->getCallee());
Chris Lattnerc14236b2007-07-10 22:18:37 +0000404
405 // The callee type will always be a pointer to function type, get the function
406 // type.
Chris Lattner5ebb2fe2007-08-08 17:43:05 +0000407 QualType CalleeTy = E->getCallee()->getType();
Chris Lattnerc14236b2007-07-10 22:18:37 +0000408 CalleeTy = cast<PointerType>(CalleeTy.getCanonicalType())->getPointeeType();
409
410 // Get information about the argument types.
411 FunctionTypeProto::arg_type_iterator ArgTyIt = 0, ArgTyEnd = 0;
412
413 // Calling unprototyped functions provides no argument info.
414 if (const FunctionTypeProto *FTP = dyn_cast<FunctionTypeProto>(CalleeTy)) {
415 ArgTyIt = FTP->arg_type_begin();
416 ArgTyEnd = FTP->arg_type_end();
417 }
Chris Lattner2b228c92007-06-15 21:34:29 +0000418
Chris Lattner23b7eb62007-06-15 23:05:46 +0000419 llvm::SmallVector<llvm::Value*, 16> Args;
Chris Lattner2b228c92007-06-15 21:34:29 +0000420
Chris Lattner90d91202007-08-10 17:02:28 +0000421 // Handle struct-return functions by passing a pointer to the location that
422 // we would like to return into.
423 if (hasAggregateLLVMType(E->getType())) {
424 // Create a temporary alloca to hold the result of the call. :(
425 Args.push_back(CreateTempAlloca(ConvertType(E->getType())));
426 // FIXME: set the stret attribute on the argument.
427 }
428
Chris Lattner2b228c92007-06-15 21:34:29 +0000429 for (unsigned i = 0, e = E->getNumArgs(); i != e; ++i) {
Chris Lattner5ebb2fe2007-08-08 17:43:05 +0000430 QualType ArgTy = E->getArg(i)->getType();
Chris Lattnerc14236b2007-07-10 22:18:37 +0000431
Chris Lattnera811da52007-08-26 22:55:13 +0000432 if (!hasAggregateLLVMType(ArgTy)) {
433 // Scalar argument is passed by-value.
434 Args.push_back(EmitScalarExpr(E->getArg(i)));
435
436 if (ArgTyIt == ArgTyEnd) {
437 // Otherwise, if passing through "..." or to a function with no prototype,
438 // perform the "default argument promotions" (C99 6.5.2.2p6), which
439 // includes the usual unary conversions, but also promotes float to
440 // double.
441 // FIXME: remove this when the impcast is in place.
442 if (Args.back()->getType() == llvm::Type::FloatTy)
443 Args.back() = Builder.CreateFPExt(Args.back(), llvm::Type::DoubleTy,
444 "tmp");
445 // FIXME: Remove ArgIt when this is gone.
Chris Lattnerc14236b2007-07-10 22:18:37 +0000446 }
Chris Lattnera811da52007-08-26 22:55:13 +0000447 } else if (ArgTy->isComplexType()) {
448 // Make a temporary alloca to pass the argument.
449 llvm::Value *DestMem = CreateTempAlloca(ConvertType(ArgTy));
450 EmitComplexExprIntoAddr(E->getArg(i), DestMem, false);
451 Args.push_back(DestMem);
452 } else {
453 llvm::Value *DestMem = CreateTempAlloca(ConvertType(ArgTy));
454 EmitAggExpr(E->getArg(i), DestMem, false);
455 Args.push_back(DestMem);
Chris Lattnerc14236b2007-07-10 22:18:37 +0000456 }
457
Chris Lattnera811da52007-08-26 22:55:13 +0000458 if (ArgTyIt != ArgTyEnd)
459 ++ArgTyIt;
Chris Lattner2b228c92007-06-15 21:34:29 +0000460 }
461
Chris Lattner7b9f04e2007-08-01 06:24:52 +0000462 llvm::Value *V = Builder.CreateCall(Callee, &Args[0], &Args[0]+Args.size());
Chris Lattner2b228c92007-06-15 21:34:29 +0000463 if (V->getType() != llvm::Type::VoidTy)
464 V->setName("call");
Chris Lattner90d91202007-08-10 17:02:28 +0000465 else if (hasAggregateLLVMType(E->getType()))
466 // Struct return.
467 return RValue::getAggregate(Args[0]);
468
Chris Lattner2b228c92007-06-15 21:34:29 +0000469 return RValue::get(V);
470}