blob: 1fd616c648850971fef3cef875061e9d0f76c653 [file] [log] [blame]
Chris Lattner4b009652007-07-25 00:24:17 +00001//===--- CGExpr.cpp - Emit LLVM Code from Expressions ---------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner959e5be2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattner4b009652007-07-25 00:24:17 +00007//
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"
Eli Friedmana04e70d2008-05-17 20:03:47 +000017#include "clang/Basic/TargetInfo.h"
Chris Lattner4b009652007-07-25 00:24:17 +000018#include "llvm/Constants.h"
19#include "llvm/DerivedTypes.h"
20#include "llvm/Function.h"
21#include "llvm/GlobalVariable.h"
22#include "llvm/Support/MathExtras.h"
Eli Friedmana04e70d2008-05-17 20:03:47 +000023#include "llvm/Target/TargetData.h"
Chris Lattner4b009652007-07-25 00:24:17 +000024using namespace clang;
25using namespace CodeGen;
26
27//===--------------------------------------------------------------------===//
28// Miscellaneous Helper Methods
29//===--------------------------------------------------------------------===//
30
31/// CreateTempAlloca - This creates a alloca and inserts it into the entry
32/// block.
33llvm::AllocaInst *CodeGenFunction::CreateTempAlloca(const llvm::Type *Ty,
34 const char *Name) {
35 return new llvm::AllocaInst(Ty, 0, Name, AllocaInsertPt);
36}
37
38/// EvaluateExprAsBool - Perform the usual unary conversions on the specified
39/// expression and compare the result against zero, returning an Int1Ty value.
40llvm::Value *CodeGenFunction::EvaluateExprAsBool(const Expr *E) {
Chris Lattnercc50a512007-08-26 16:46:58 +000041 QualType BoolTy = getContext().BoolTy;
Chris Lattnerde0908b2008-04-04 16:54:41 +000042 if (!E->getType()->isAnyComplexType())
Chris Lattnercc50a512007-08-26 16:46:58 +000043 return EmitScalarConversion(EmitScalarExpr(E), E->getType(), BoolTy);
Chris Lattner4b009652007-07-25 00:24:17 +000044
Chris Lattnercc50a512007-08-26 16:46:58 +000045 return EmitComplexToScalarConversion(EmitComplexExpr(E), E->getType(),BoolTy);
Chris Lattner4b009652007-07-25 00:24:17 +000046}
47
Chris Lattnere24c4cf2007-08-31 22:49:20 +000048/// EmitAnyExpr - Emit code to compute the specified expression which can have
49/// any type. The result is returned as an RValue struct. If this is an
50/// aggregate expression, the aggloc/agglocvolatile arguments indicate where
51/// the result should be returned.
52RValue CodeGenFunction::EmitAnyExpr(const Expr *E, llvm::Value *AggLoc,
53 bool isAggLocVolatile) {
54 if (!hasAggregateLLVMType(E->getType()))
55 return RValue::get(EmitScalarExpr(E));
Chris Lattnerde0908b2008-04-04 16:54:41 +000056 else if (E->getType()->isAnyComplexType())
Chris Lattnere24c4cf2007-08-31 22:49:20 +000057 return RValue::getComplex(EmitComplexExpr(E));
58
59 EmitAggExpr(E, AggLoc, isAggLocVolatile);
60 return RValue::getAggregate(AggLoc);
61}
62
Dan Gohman4751a3a2008-05-22 00:50:06 +000063/// getAccessedFieldNo - Given an encoded value and a result number, return
64/// the input field number being accessed.
65unsigned CodeGenFunction::getAccessedFieldNo(unsigned Idx,
66 const llvm::Constant *Elts) {
67 if (isa<llvm::ConstantAggregateZero>(Elts))
68 return 0;
69
70 return cast<llvm::ConstantInt>(Elts->getOperand(Idx))->getZExtValue();
71}
72
Chris Lattnere24c4cf2007-08-31 22:49:20 +000073
Chris Lattner4b009652007-07-25 00:24:17 +000074//===----------------------------------------------------------------------===//
75// LValue Expression Emission
76//===----------------------------------------------------------------------===//
77
78/// EmitLValue - Emit code to compute a designator that specifies the location
79/// of the expression.
80///
81/// This can return one of two things: a simple address or a bitfield
82/// reference. In either case, the LLVM Value* in the LValue structure is
83/// guaranteed to be an LLVM pointer type.
84///
85/// If this returns a bitfield reference, nothing about the pointee type of
86/// the LLVM value is known: For example, it may not be a pointer to an
87/// integer.
88///
89/// If this returns a normal address, and if the lvalue's C type is fixed
90/// size, this method guarantees that the returned pointer type will point to
91/// an LLVM type of the same size of the lvalue's type. If the lvalue has a
92/// variable length type, this is not possible.
93///
94LValue CodeGenFunction::EmitLValue(const Expr *E) {
95 switch (E->getStmtClass()) {
Chris Lattnera52c8892007-08-26 05:06:40 +000096 default: {
Chris Lattnerc61e9f82008-03-30 23:25:33 +000097 printf("Statement class: %d\n", E->getStmtClass());
Chris Lattnere8f49632007-12-02 01:49:16 +000098 WarnUnsupported(E, "l-value expression");
Christopher Lamb4fe5e702007-12-17 01:11:20 +000099 llvm::Type *Ty = llvm::PointerType::getUnqual(ConvertType(E->getType()));
Eli Friedman2e630542008-06-13 23:01:12 +0000100 return LValue::MakeAddr(llvm::UndefValue::get(Ty),
101 E->getType().getCVRQualifiers());
Chris Lattnera52c8892007-08-26 05:06:40 +0000102 }
Chris Lattner4b009652007-07-25 00:24:17 +0000103
Christopher Lambad327ba2007-12-29 05:02:41 +0000104 case Expr::CallExprClass: return EmitCallExprLValue(cast<CallExpr>(E));
Chris Lattner4b009652007-07-25 00:24:17 +0000105 case Expr::DeclRefExprClass: return EmitDeclRefLValue(cast<DeclRefExpr>(E));
106 case Expr::ParenExprClass:return EmitLValue(cast<ParenExpr>(E)->getSubExpr());
107 case Expr::PreDefinedExprClass:
108 return EmitPreDefinedLValue(cast<PreDefinedExpr>(E));
109 case Expr::StringLiteralClass:
110 return EmitStringLiteralLValue(cast<StringLiteral>(E));
Chris Lattnerb326b172008-03-30 23:03:07 +0000111
112 case Expr::ObjCIvarRefExprClass:
113 return EmitObjCIvarRefLValue(cast<ObjCIvarRefExpr>(E));
Chris Lattner4b009652007-07-25 00:24:17 +0000114
115 case Expr::UnaryOperatorClass:
116 return EmitUnaryOpLValue(cast<UnaryOperator>(E));
117 case Expr::ArraySubscriptExprClass:
118 return EmitArraySubscriptExpr(cast<ArraySubscriptExpr>(E));
Nate Begemanaf6ed502008-04-18 23:10:10 +0000119 case Expr::ExtVectorElementExprClass:
120 return EmitExtVectorElementExpr(cast<ExtVectorElementExpr>(E));
Devang Patel41b66252007-10-23 20:28:39 +0000121 case Expr::MemberExprClass: return EmitMemberExpr(cast<MemberExpr>(E));
Eli Friedmanf3c2cb42008-05-13 23:18:27 +0000122 case Expr::CompoundLiteralExprClass:
123 return EmitCompoundLiteralLValue(cast<CompoundLiteralExpr>(E));
Chris Lattner4b009652007-07-25 00:24:17 +0000124 }
125}
126
127/// EmitLoadOfLValue - Given an expression that represents a value lvalue,
128/// this method emits the address of the lvalue, then loads the result as an
129/// rvalue, returning the rvalue.
130RValue CodeGenFunction::EmitLoadOfLValue(LValue LV, QualType ExprType) {
Chris Lattner4b009652007-07-25 00:24:17 +0000131 if (LV.isSimple()) {
132 llvm::Value *Ptr = LV.getAddress();
133 const llvm::Type *EltTy =
134 cast<llvm::PointerType>(Ptr->getType())->getElementType();
135
136 // Simple scalar l-value.
Dan Gohman377ba9f2008-05-22 22:12:56 +0000137 if (EltTy->isSingleValueType()) {
Eli Friedman2e630542008-06-13 23:01:12 +0000138 llvm::Value *V = Builder.CreateLoad(Ptr, LV.isVolatileQualified(),"tmp");
Chris Lattner6b79f4e2008-01-30 07:01:17 +0000139
140 // Bool can have different representation in memory than in registers.
141 if (ExprType->isBooleanType()) {
142 if (V->getType() != llvm::Type::Int1Ty)
143 V = Builder.CreateTrunc(V, llvm::Type::Int1Ty, "tobool");
144 }
145
146 return RValue::get(V);
147 }
Chris Lattner4b009652007-07-25 00:24:17 +0000148
Chris Lattnerbdb8ffb2007-08-11 00:04:45 +0000149 assert(ExprType->isFunctionType() && "Unknown scalar value");
150 return RValue::get(Ptr);
Chris Lattner4b009652007-07-25 00:24:17 +0000151 }
152
153 if (LV.isVectorElt()) {
Eli Friedman2e630542008-06-13 23:01:12 +0000154 llvm::Value *Vec = Builder.CreateLoad(LV.getVectorAddr(),
155 LV.isVolatileQualified(), "tmp");
Chris Lattner4b009652007-07-25 00:24:17 +0000156 return RValue::get(Builder.CreateExtractElement(Vec, LV.getVectorIdx(),
157 "vecext"));
158 }
Chris Lattnera735fac2007-08-03 00:16:29 +0000159
160 // If this is a reference to a subset of the elements of a vector, either
161 // shuffle the input or extract/insert them as appropriate.
Nate Begemanaf6ed502008-04-18 23:10:10 +0000162 if (LV.isExtVectorElt())
163 return EmitLoadOfExtVectorElementLValue(LV, ExprType);
Lauro Ramos Venanciob40307c2008-01-22 20:17:04 +0000164
165 if (LV.isBitfield())
166 return EmitLoadOfBitfieldLValue(LV, ExprType);
167
168 assert(0 && "Unknown LValue type!");
Chris Lattner1d2b4612007-09-16 19:23:47 +0000169 //an invalid RValue, but the assert will
170 //ensure that this point is never reached
171 return RValue();
Chris Lattner4b009652007-07-25 00:24:17 +0000172}
173
Lauro Ramos Venanciob40307c2008-01-22 20:17:04 +0000174RValue CodeGenFunction::EmitLoadOfBitfieldLValue(LValue LV,
175 QualType ExprType) {
176 llvm::Value *Ptr = LV.getBitfieldAddr();
177 const llvm::Type *EltTy =
178 cast<llvm::PointerType>(Ptr->getType())->getElementType();
179 unsigned EltTySize = EltTy->getPrimitiveSizeInBits();
180 unsigned short BitfieldSize = LV.getBitfieldSize();
181 unsigned short EndBit = LV.getBitfieldStartBit() + BitfieldSize;
182
Eli Friedman2e630542008-06-13 23:01:12 +0000183 llvm::Value *V = Builder.CreateLoad(Ptr, LV.isVolatileQualified(), "tmp");
Lauro Ramos Venanciob40307c2008-01-22 20:17:04 +0000184
185 llvm::Value *ShAmt = llvm::ConstantInt::get(EltTy, EltTySize - EndBit);
186 V = Builder.CreateShl(V, ShAmt, "tmp");
187
188 ShAmt = llvm::ConstantInt::get(EltTy, EltTySize - BitfieldSize);
189 V = LV.isBitfieldSigned() ?
190 Builder.CreateAShr(V, ShAmt, "tmp") :
191 Builder.CreateLShr(V, ShAmt, "tmp");
Eli Friedmana04e70d2008-05-17 20:03:47 +0000192
193 // The bitfield type and the normal type differ when the storage sizes
194 // differ (currently just _Bool).
195 V = Builder.CreateIntCast(V, ConvertType(ExprType), false, "tmp");
196
Lauro Ramos Venanciob40307c2008-01-22 20:17:04 +0000197 return RValue::get(V);
198}
199
Chris Lattner944f7962007-08-03 16:18:34 +0000200// If this is a reference to a subset of the elements of a vector, either
201// shuffle the input or extract/insert them as appropriate.
Nate Begemanaf6ed502008-04-18 23:10:10 +0000202RValue CodeGenFunction::EmitLoadOfExtVectorElementLValue(LValue LV,
203 QualType ExprType) {
Eli Friedman2e630542008-06-13 23:01:12 +0000204 llvm::Value *Vec = Builder.CreateLoad(LV.getExtVectorAddr(),
205 LV.isVolatileQualified(), "tmp");
Chris Lattner944f7962007-08-03 16:18:34 +0000206
Nate Begemanc8e51f82008-05-09 06:41:27 +0000207 const llvm::Constant *Elts = LV.getExtVectorElts();
Chris Lattner944f7962007-08-03 16:18:34 +0000208
209 // If the result of the expression is a non-vector type, we must be
210 // extracting a single element. Just codegen as an extractelement.
Chris Lattner4b492962007-08-10 17:10:08 +0000211 const VectorType *ExprVT = ExprType->getAsVectorType();
212 if (!ExprVT) {
Dan Gohman4751a3a2008-05-22 00:50:06 +0000213 unsigned InIdx = getAccessedFieldNo(0, Elts);
Chris Lattner944f7962007-08-03 16:18:34 +0000214 llvm::Value *Elt = llvm::ConstantInt::get(llvm::Type::Int32Ty, InIdx);
215 return RValue::get(Builder.CreateExtractElement(Vec, Elt, "tmp"));
216 }
217
218 // If the source and destination have the same number of elements, use a
219 // vector shuffle instead of insert/extracts.
Chris Lattner4b492962007-08-10 17:10:08 +0000220 unsigned NumResultElts = ExprVT->getNumElements();
Chris Lattner944f7962007-08-03 16:18:34 +0000221 unsigned NumSourceElts =
222 cast<llvm::VectorType>(Vec->getType())->getNumElements();
223
224 if (NumResultElts == NumSourceElts) {
225 llvm::SmallVector<llvm::Constant*, 4> Mask;
226 for (unsigned i = 0; i != NumResultElts; ++i) {
Dan Gohman4751a3a2008-05-22 00:50:06 +0000227 unsigned InIdx = getAccessedFieldNo(i, Elts);
Chris Lattner944f7962007-08-03 16:18:34 +0000228 Mask.push_back(llvm::ConstantInt::get(llvm::Type::Int32Ty, InIdx));
229 }
230
231 llvm::Value *MaskV = llvm::ConstantVector::get(&Mask[0], Mask.size());
232 Vec = Builder.CreateShuffleVector(Vec,
233 llvm::UndefValue::get(Vec->getType()),
234 MaskV, "tmp");
235 return RValue::get(Vec);
236 }
237
238 // Start out with an undef of the result type.
239 llvm::Value *Result = llvm::UndefValue::get(ConvertType(ExprType));
240
241 // Extract/Insert each element of the result.
242 for (unsigned i = 0; i != NumResultElts; ++i) {
Dan Gohman4751a3a2008-05-22 00:50:06 +0000243 unsigned InIdx = getAccessedFieldNo(i, Elts);
Chris Lattner944f7962007-08-03 16:18:34 +0000244 llvm::Value *Elt = llvm::ConstantInt::get(llvm::Type::Int32Ty, InIdx);
245 Elt = Builder.CreateExtractElement(Vec, Elt, "tmp");
246
247 llvm::Value *OutIdx = llvm::ConstantInt::get(llvm::Type::Int32Ty, i);
248 Result = Builder.CreateInsertElement(Result, Elt, OutIdx, "tmp");
249 }
250
251 return RValue::get(Result);
252}
253
254
Chris Lattner4b009652007-07-25 00:24:17 +0000255
256/// EmitStoreThroughLValue - Store the specified rvalue into the specified
257/// lvalue, where both are guaranteed to the have the same type, and that type
258/// is 'Ty'.
259void CodeGenFunction::EmitStoreThroughLValue(RValue Src, LValue Dst,
260 QualType Ty) {
Chris Lattner5bfdd232007-08-03 16:28:33 +0000261 if (!Dst.isSimple()) {
262 if (Dst.isVectorElt()) {
263 // Read/modify/write the vector, inserting the new element.
Eli Friedman2e630542008-06-13 23:01:12 +0000264 llvm::Value *Vec = Builder.CreateLoad(Dst.getVectorAddr(),
265 Dst.isVolatileQualified(), "tmp");
Chris Lattnere24c4cf2007-08-31 22:49:20 +0000266 Vec = Builder.CreateInsertElement(Vec, Src.getScalarVal(),
Chris Lattner5bfdd232007-08-03 16:28:33 +0000267 Dst.getVectorIdx(), "vecins");
Eli Friedman2e630542008-06-13 23:01:12 +0000268 Builder.CreateStore(Vec, Dst.getVectorAddr(),Dst.isVolatileQualified());
Chris Lattner5bfdd232007-08-03 16:28:33 +0000269 return;
270 }
Chris Lattner4b009652007-07-25 00:24:17 +0000271
Nate Begemanaf6ed502008-04-18 23:10:10 +0000272 // If this is an update of extended vector elements, insert them as
273 // appropriate.
274 if (Dst.isExtVectorElt())
275 return EmitStoreThroughExtVectorComponentLValue(Src, Dst, Ty);
Lauro Ramos Venancio2d7a34c2008-01-22 22:36:45 +0000276
277 if (Dst.isBitfield())
278 return EmitStoreThroughBitfieldLValue(Src, Dst, Ty);
279
Lauro Ramos Venancio14d39842008-01-22 22:38:35 +0000280 assert(0 && "Unknown LValue type");
Chris Lattner5bfdd232007-08-03 16:28:33 +0000281 }
Chris Lattner4b009652007-07-25 00:24:17 +0000282
283 llvm::Value *DstAddr = Dst.getAddress();
Chris Lattnerbdb8ffb2007-08-11 00:04:45 +0000284 assert(Src.isScalar() && "Can't emit an agg store with this method");
285 // FIXME: Handle volatility etc.
Chris Lattnere24c4cf2007-08-31 22:49:20 +0000286 const llvm::Type *SrcTy = Src.getScalarVal()->getType();
Christopher Lamb4fe5e702007-12-17 01:11:20 +0000287 const llvm::PointerType *DstPtr = cast<llvm::PointerType>(DstAddr->getType());
288 const llvm::Type *AddrTy = DstPtr->getElementType();
289 unsigned AS = DstPtr->getAddressSpace();
Chris Lattner4b009652007-07-25 00:24:17 +0000290
Chris Lattnerbdb8ffb2007-08-11 00:04:45 +0000291 if (AddrTy != SrcTy)
Christopher Lamb4fe5e702007-12-17 01:11:20 +0000292 DstAddr = Builder.CreateBitCast(DstAddr,
293 llvm::PointerType::get(SrcTy, AS),
Chris Lattnerbdb8ffb2007-08-11 00:04:45 +0000294 "storetmp");
Eli Friedman2e630542008-06-13 23:01:12 +0000295 Builder.CreateStore(Src.getScalarVal(), DstAddr, Dst.isVolatileQualified());
Chris Lattner4b009652007-07-25 00:24:17 +0000296}
297
Lauro Ramos Venancio2d7a34c2008-01-22 22:36:45 +0000298void CodeGenFunction::EmitStoreThroughBitfieldLValue(RValue Src, LValue Dst,
299 QualType Ty) {
300 unsigned short StartBit = Dst.getBitfieldStartBit();
301 unsigned short BitfieldSize = Dst.getBitfieldSize();
302 llvm::Value *Ptr = Dst.getBitfieldAddr();
Lauro Ramos Venancio2d7a34c2008-01-22 22:36:45 +0000303
304 llvm::Value *NewVal = Src.getScalarVal();
Eli Friedman2e630542008-06-13 23:01:12 +0000305 llvm::Value *OldVal = Builder.CreateLoad(Ptr, Dst.isVolatileQualified(),
306 "tmp");
Lauro Ramos Venancio2d7a34c2008-01-22 22:36:45 +0000307
Eli Friedmana04e70d2008-05-17 20:03:47 +0000308 // The bitfield type and the normal type differ when the storage sizes
309 // differ (currently just _Bool).
310 const llvm::Type *EltTy = OldVal->getType();
311 unsigned EltTySize = CGM.getTargetData().getABITypeSizeInBits(EltTy);
312
313 NewVal = Builder.CreateIntCast(NewVal, EltTy, false, "tmp");
314
315 // Move the bits into the appropriate location
Lauro Ramos Venancio2d7a34c2008-01-22 22:36:45 +0000316 llvm::Value *ShAmt = llvm::ConstantInt::get(EltTy, StartBit);
317 NewVal = Builder.CreateShl(NewVal, ShAmt, "tmp");
318
319 llvm::Constant *Mask = llvm::ConstantInt::get(
320 llvm::APInt::getBitsSet(EltTySize, StartBit,
Dan Gohmana63ce8f2008-02-12 21:49:34 +0000321 StartBit + BitfieldSize));
Lauro Ramos Venancio2d7a34c2008-01-22 22:36:45 +0000322
323 // Mask out any bits that shouldn't be set in the result.
324 NewVal = Builder.CreateAnd(NewVal, Mask, "tmp");
325
326 // Next, mask out the bits this bit-field should include from the old value.
327 Mask = llvm::ConstantExpr::getNot(Mask);
328 OldVal = Builder.CreateAnd(OldVal, Mask, "tmp");
329
330 // Finally, merge the two together and store it.
331 NewVal = Builder.CreateOr(OldVal, NewVal, "tmp");
332
Eli Friedman2e630542008-06-13 23:01:12 +0000333 Builder.CreateStore(NewVal, Ptr, Dst.isVolatileQualified());
Lauro Ramos Venancio2d7a34c2008-01-22 22:36:45 +0000334}
335
Nate Begemanaf6ed502008-04-18 23:10:10 +0000336void CodeGenFunction::EmitStoreThroughExtVectorComponentLValue(RValue Src,
337 LValue Dst,
338 QualType Ty) {
Chris Lattner5bfdd232007-08-03 16:28:33 +0000339 // This access turns into a read/modify/write of the vector. Load the input
340 // value now.
Eli Friedman2e630542008-06-13 23:01:12 +0000341 llvm::Value *Vec = Builder.CreateLoad(Dst.getExtVectorAddr(),
342 Dst.isVolatileQualified(), "tmp");
Nate Begemanc8e51f82008-05-09 06:41:27 +0000343 const llvm::Constant *Elts = Dst.getExtVectorElts();
Chris Lattner5bfdd232007-08-03 16:28:33 +0000344
Chris Lattnere24c4cf2007-08-31 22:49:20 +0000345 llvm::Value *SrcVal = Src.getScalarVal();
Chris Lattner5bfdd232007-08-03 16:28:33 +0000346
Chris Lattner940966d2007-08-03 16:37:04 +0000347 if (const VectorType *VTy = Ty->getAsVectorType()) {
348 unsigned NumSrcElts = VTy->getNumElements();
349
350 // Extract/Insert each element.
351 for (unsigned i = 0; i != NumSrcElts; ++i) {
352 llvm::Value *Elt = llvm::ConstantInt::get(llvm::Type::Int32Ty, i);
353 Elt = Builder.CreateExtractElement(SrcVal, Elt, "tmp");
354
Dan Gohman4751a3a2008-05-22 00:50:06 +0000355 unsigned Idx = getAccessedFieldNo(i, Elts);
Chris Lattner940966d2007-08-03 16:37:04 +0000356 llvm::Value *OutIdx = llvm::ConstantInt::get(llvm::Type::Int32Ty, Idx);
357 Vec = Builder.CreateInsertElement(Vec, Elt, OutIdx, "tmp");
358 }
359 } else {
360 // If the Src is a scalar (not a vector) it must be updating one element.
Dan Gohman4751a3a2008-05-22 00:50:06 +0000361 unsigned InIdx = getAccessedFieldNo(0, Elts);
Chris Lattner5bfdd232007-08-03 16:28:33 +0000362 llvm::Value *Elt = llvm::ConstantInt::get(llvm::Type::Int32Ty, InIdx);
363 Vec = Builder.CreateInsertElement(Vec, SrcVal, Elt, "tmp");
Chris Lattner5bfdd232007-08-03 16:28:33 +0000364 }
365
Eli Friedman2e630542008-06-13 23:01:12 +0000366 Builder.CreateStore(Vec, Dst.getExtVectorAddr(), Dst.isVolatileQualified());
Chris Lattner5bfdd232007-08-03 16:28:33 +0000367}
368
Chris Lattner4b009652007-07-25 00:24:17 +0000369
370LValue CodeGenFunction::EmitDeclRefLValue(const DeclRefExpr *E) {
Steve Naroff72a6ebc2008-04-15 22:42:06 +0000371 const VarDecl *VD = dyn_cast<VarDecl>(E->getDecl());
372
Chris Lattner8c7c6a12008-06-17 18:05:57 +0000373 if (VD && (VD->isBlockVarDecl() || isa<ParmVarDecl>(VD) ||
374 isa<ImplicitParamDecl>(VD))) {
Lauro Ramos Venancio2348d972008-02-16 22:30:38 +0000375 if (VD->getStorageClass() == VarDecl::Extern)
Daniel Dunbar7bf5b3d2008-07-29 23:18:29 +0000376 return LValue::MakeAddr(CGM.GetAddrOfGlobalVar(VD),
Eli Friedman2e630542008-06-13 23:01:12 +0000377 E->getType().getCVRQualifiers());
Lauro Ramos Venancio2348d972008-02-16 22:30:38 +0000378 else {
Steve Naroff72a6ebc2008-04-15 22:42:06 +0000379 llvm::Value *V = LocalDeclMap[VD];
Lauro Ramos Venancio2348d972008-02-16 22:30:38 +0000380 assert(V && "BlockVarDecl not entered in LocalDeclMap?");
Eli Friedman2e630542008-06-13 23:01:12 +0000381 return LValue::MakeAddr(V, E->getType().getCVRQualifiers());
Lauro Ramos Venancio2348d972008-02-16 22:30:38 +0000382 }
Steve Naroff72a6ebc2008-04-15 22:42:06 +0000383 } else if (VD && VD->isFileVarDecl()) {
Daniel Dunbar7bf5b3d2008-07-29 23:18:29 +0000384 return LValue::MakeAddr(CGM.GetAddrOfGlobalVar(VD),
Eli Friedman2e630542008-06-13 23:01:12 +0000385 E->getType().getCVRQualifiers());
Steve Naroff72a6ebc2008-04-15 22:42:06 +0000386 } else if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(E->getDecl())) {
Daniel Dunbar7bf5b3d2008-07-29 23:18:29 +0000387 return LValue::MakeAddr(CGM.GetAddrOfFunction(FD),
Eli Friedman2e630542008-06-13 23:01:12 +0000388 E->getType().getCVRQualifiers());
Chris Lattner4b009652007-07-25 00:24:17 +0000389 }
Chris Lattner8c7c6a12008-06-17 18:05:57 +0000390 else if (const ImplicitParamDecl *IPD =
391 dyn_cast<ImplicitParamDecl>(E->getDecl())) {
392 llvm::Value *V = LocalDeclMap[IPD];
393 assert(V && "BlockVarDecl not entered in LocalDeclMap?");
394 return LValue::MakeAddr(V, E->getType().getCVRQualifiers());
395 }
Chris Lattner4b009652007-07-25 00:24:17 +0000396 assert(0 && "Unimp declref");
Chris Lattner1d2b4612007-09-16 19:23:47 +0000397 //an invalid LValue, but the assert will
398 //ensure that this point is never reached.
399 return LValue();
Chris Lattner4b009652007-07-25 00:24:17 +0000400}
401
402LValue CodeGenFunction::EmitUnaryOpLValue(const UnaryOperator *E) {
403 // __extension__ doesn't affect lvalue-ness.
404 if (E->getOpcode() == UnaryOperator::Extension)
405 return EmitLValue(E->getSubExpr());
406
Chris Lattnerc154ac12008-07-26 22:37:01 +0000407 QualType ExprTy = getContext().getCanonicalType(E->getSubExpr()->getType());
Chris Lattner5bf72022007-10-30 22:53:42 +0000408 switch (E->getOpcode()) {
409 default: assert(0 && "Unknown unary operator lvalue!");
410 case UnaryOperator::Deref:
Eli Friedman2e630542008-06-13 23:01:12 +0000411 return LValue::MakeAddr(EmitScalarExpr(E->getSubExpr()),
Chris Lattnerd5a56aa2008-07-26 22:17:49 +0000412 ExprTy->getAsPointerType()->getPointeeType()
413 .getCVRQualifiers());
Chris Lattner5bf72022007-10-30 22:53:42 +0000414 case UnaryOperator::Real:
415 case UnaryOperator::Imag:
416 LValue LV = EmitLValue(E->getSubExpr());
Chris Lattner07307562008-03-19 05:19:41 +0000417 unsigned Idx = E->getOpcode() == UnaryOperator::Imag;
418 return LValue::MakeAddr(Builder.CreateStructGEP(LV.getAddress(),
Chris Lattnerd5a56aa2008-07-26 22:17:49 +0000419 Idx, "idx"),
420 ExprTy.getCVRQualifiers());
Chris Lattner5bf72022007-10-30 22:53:42 +0000421 }
Chris Lattner4b009652007-07-25 00:24:17 +0000422}
423
424LValue CodeGenFunction::EmitStringLiteralLValue(const StringLiteral *E) {
425 assert(!E->isWide() && "FIXME: Wide strings not supported yet!");
Eli Friedman48ec5622008-05-19 17:51:16 +0000426 // Get the string data
Chris Lattner4b009652007-07-25 00:24:17 +0000427 const char *StrData = E->getStrData();
428 unsigned Len = E->getByteLength();
Chris Lattnerdb6be562007-11-28 05:34:05 +0000429 std::string StringLiteral(StrData, StrData+Len);
Eli Friedman48ec5622008-05-19 17:51:16 +0000430
431 // Resize the string to the right size
432 const ConstantArrayType *CAT = E->getType()->getAsConstantArrayType();
433 uint64_t RealLen = CAT->getSize().getZExtValue();
434 StringLiteral.resize(RealLen, '\0');
435
Eli Friedman2e630542008-06-13 23:01:12 +0000436 return LValue::MakeAddr(CGM.GetAddrOfConstantString(StringLiteral),0);
Chris Lattner4b009652007-07-25 00:24:17 +0000437}
438
439LValue CodeGenFunction::EmitPreDefinedLValue(const PreDefinedExpr *E) {
Chris Lattner6e6a5972008-04-04 04:07:35 +0000440 std::string FunctionName;
441 if(const FunctionDecl *FD = dyn_cast<FunctionDecl>(CurFuncDecl)) {
442 FunctionName = FD->getName();
443 }
444 else {
445 assert(0 && "Attempting to load predefined constant for invalid decl type");
446 }
Chris Lattner4b009652007-07-25 00:24:17 +0000447 std::string GlobalVarName;
448
449 switch (E->getIdentType()) {
450 default:
451 assert(0 && "unknown pre-defined ident type");
452 case PreDefinedExpr::Func:
453 GlobalVarName = "__func__.";
454 break;
455 case PreDefinedExpr::Function:
456 GlobalVarName = "__FUNCTION__.";
457 break;
458 case PreDefinedExpr::PrettyFunction:
459 // FIXME:: Demangle C++ method names
460 GlobalVarName = "__PRETTY_FUNCTION__.";
461 break;
462 }
463
Chris Lattner6e6a5972008-04-04 04:07:35 +0000464 GlobalVarName += FunctionName;
Chris Lattner4b009652007-07-25 00:24:17 +0000465
466 // FIXME: Can cache/reuse these within the module.
467 llvm::Constant *C=llvm::ConstantArray::get(FunctionName);
468
469 // Create a global variable for this.
470 C = new llvm::GlobalVariable(C->getType(), true,
471 llvm::GlobalValue::InternalLinkage,
472 C, GlobalVarName, CurFn->getParent());
Eli Friedman2e630542008-06-13 23:01:12 +0000473 return LValue::MakeAddr(C,0);
Chris Lattner4b009652007-07-25 00:24:17 +0000474}
475
476LValue CodeGenFunction::EmitArraySubscriptExpr(const ArraySubscriptExpr *E) {
Ted Kremenek1c1700f2007-08-20 16:18:38 +0000477 // The index must always be an integer, which is not an aggregate. Emit it.
Chris Lattner9fba49a2007-08-24 05:35:26 +0000478 llvm::Value *Idx = EmitScalarExpr(E->getIdx());
Chris Lattner4b009652007-07-25 00:24:17 +0000479
480 // If the base is a vector type, then we are forming a vector element lvalue
481 // with this subscript.
Eli Friedman2e630542008-06-13 23:01:12 +0000482 if (E->getBase()->getType()->isVectorType()) {
Chris Lattner4b009652007-07-25 00:24:17 +0000483 // Emit the vector as an lvalue to get its address.
Eli Friedman2e630542008-06-13 23:01:12 +0000484 LValue LHS = EmitLValue(E->getBase());
Ted Kremenek1c1700f2007-08-20 16:18:38 +0000485 assert(LHS.isSimple() && "Can only subscript lvalue vectors here!");
Chris Lattner4b009652007-07-25 00:24:17 +0000486 // FIXME: This should properly sign/zero/extend or truncate Idx to i32.
Eli Friedman2e630542008-06-13 23:01:12 +0000487 return LValue::MakeVectorElt(LHS.getAddress(), Idx,
488 E->getBase()->getType().getCVRQualifiers());
Chris Lattner4b009652007-07-25 00:24:17 +0000489 }
490
Ted Kremenek1c1700f2007-08-20 16:18:38 +0000491 // The base must be a pointer, which is not an aggregate. Emit it.
Chris Lattner9fba49a2007-08-24 05:35:26 +0000492 llvm::Value *Base = EmitScalarExpr(E->getBase());
Chris Lattner4b009652007-07-25 00:24:17 +0000493
Ted Kremenek1c1700f2007-08-20 16:18:38 +0000494 // Extend or truncate the index type to 32 or 64-bits.
Chris Lattner2af72ac2007-08-08 17:43:05 +0000495 QualType IdxTy = E->getIdx()->getType();
Chris Lattner4b009652007-07-25 00:24:17 +0000496 bool IdxSigned = IdxTy->isSignedIntegerType();
497 unsigned IdxBitwidth = cast<llvm::IntegerType>(Idx->getType())->getBitWidth();
498 if (IdxBitwidth != LLVMPointerWidth)
499 Idx = Builder.CreateIntCast(Idx, llvm::IntegerType::get(LLVMPointerWidth),
500 IdxSigned, "idxprom");
501
502 // We know that the pointer points to a type of the correct size, unless the
503 // size is a VLA.
Eli Friedman62f67fd2008-02-15 12:20:59 +0000504 if (!E->getType()->isConstantSizeType())
Chris Lattner4b009652007-07-25 00:24:17 +0000505 assert(0 && "VLA idx not implemented");
Chris Lattnerc154ac12008-07-26 22:37:01 +0000506 QualType ExprTy = getContext().getCanonicalType(E->getBase()->getType());
Chris Lattnerd5a56aa2008-07-26 22:17:49 +0000507
Eli Friedman2e630542008-06-13 23:01:12 +0000508 return LValue::MakeAddr(Builder.CreateGEP(Base, Idx, "arrayidx"),
Chris Lattnerd5a56aa2008-07-26 22:17:49 +0000509 ExprTy->getAsPointerType()->getPointeeType()
510 .getCVRQualifiers());
Chris Lattner4b009652007-07-25 00:24:17 +0000511}
512
Nate Begemana1ae7442008-05-13 21:03:02 +0000513static
514llvm::Constant *GenerateConstantVector(llvm::SmallVector<unsigned, 4> &Elts) {
515 llvm::SmallVector<llvm::Constant *, 4> CElts;
516
517 for (unsigned i = 0, e = Elts.size(); i != e; ++i)
518 CElts.push_back(llvm::ConstantInt::get(llvm::Type::Int32Ty, Elts[i]));
519
520 return llvm::ConstantVector::get(&CElts[0], CElts.size());
521}
522
Chris Lattner65520192007-08-02 23:37:31 +0000523LValue CodeGenFunction::
Nate Begemanaf6ed502008-04-18 23:10:10 +0000524EmitExtVectorElementExpr(const ExtVectorElementExpr *E) {
Chris Lattner65520192007-08-02 23:37:31 +0000525 // Emit the base vector as an l-value.
526 LValue Base = EmitLValue(E->getBase());
Chris Lattner65520192007-08-02 23:37:31 +0000527
Nate Begemana1ae7442008-05-13 21:03:02 +0000528 // Encode the element access list into a vector of unsigned indices.
529 llvm::SmallVector<unsigned, 4> Indices;
530 E->getEncodedElementAccess(Indices);
531
532 if (Base.isSimple()) {
533 llvm::Constant *CV = GenerateConstantVector(Indices);
Eli Friedman2e630542008-06-13 23:01:12 +0000534 return LValue::MakeExtVectorElt(Base.getAddress(), CV,
535 E->getBase()->getType().getCVRQualifiers());
Nate Begemana1ae7442008-05-13 21:03:02 +0000536 }
537 assert(Base.isExtVectorElt() && "Can only subscript lvalue vec elts here!");
538
539 llvm::Constant *BaseElts = Base.getExtVectorElts();
540 llvm::SmallVector<llvm::Constant *, 4> CElts;
541
542 for (unsigned i = 0, e = Indices.size(); i != e; ++i) {
543 if (isa<llvm::ConstantAggregateZero>(BaseElts))
544 CElts.push_back(llvm::ConstantInt::get(llvm::Type::Int32Ty, 0));
545 else
546 CElts.push_back(BaseElts->getOperand(Indices[i]));
547 }
548 llvm::Constant *CV = llvm::ConstantVector::get(&CElts[0], CElts.size());
Eli Friedman2e630542008-06-13 23:01:12 +0000549 return LValue::MakeExtVectorElt(Base.getExtVectorAddr(), CV,
550 E->getBase()->getType().getCVRQualifiers());
Chris Lattner65520192007-08-02 23:37:31 +0000551}
552
Devang Patel41b66252007-10-23 20:28:39 +0000553LValue CodeGenFunction::EmitMemberExpr(const MemberExpr *E) {
Devang Patele1f79db2007-12-11 21:33:16 +0000554 bool isUnion = false;
Devang Patel9dd3e2b2007-10-24 22:26:28 +0000555 Expr *BaseExpr = E->getBase();
Devang Patel9dd3e2b2007-10-24 22:26:28 +0000556 llvm::Value *BaseValue = NULL;
Eli Friedman2e630542008-06-13 23:01:12 +0000557 unsigned CVRQualifiers=0;
558
Chris Lattner659079e2007-12-02 18:52:07 +0000559 // If this is s.x, emit s as an lvalue. If it is s->x, emit s as a scalar.
Devang Patele1f79db2007-12-11 21:33:16 +0000560 if (E->isArrow()) {
Devang Patel2b24fd92007-10-26 18:15:21 +0000561 BaseValue = EmitScalarExpr(BaseExpr);
Devang Patele1f79db2007-12-11 21:33:16 +0000562 const PointerType *PTy =
Chris Lattnerc154ac12008-07-26 22:37:01 +0000563 cast<PointerType>(getContext().getCanonicalType(BaseExpr->getType()));
Devang Patele1f79db2007-12-11 21:33:16 +0000564 if (PTy->getPointeeType()->isUnionType())
565 isUnion = true;
Eli Friedman2e630542008-06-13 23:01:12 +0000566 CVRQualifiers = PTy->getPointeeType().getCVRQualifiers();
Devang Patele1f79db2007-12-11 21:33:16 +0000567 }
Chris Lattner659079e2007-12-02 18:52:07 +0000568 else {
569 LValue BaseLV = EmitLValue(BaseExpr);
570 // FIXME: this isn't right for bitfields.
571 BaseValue = BaseLV.getAddress();
Devang Patele1f79db2007-12-11 21:33:16 +0000572 if (BaseExpr->getType()->isUnionType())
573 isUnion = true;
Eli Friedman2e630542008-06-13 23:01:12 +0000574 CVRQualifiers = BaseExpr->getType().getCVRQualifiers();
Chris Lattner659079e2007-12-02 18:52:07 +0000575 }
Devang Patel41b66252007-10-23 20:28:39 +0000576
577 FieldDecl *Field = E->getMemberDecl();
Eli Friedman2e630542008-06-13 23:01:12 +0000578 return EmitLValueForField(BaseValue, Field, isUnion, CVRQualifiers);
Eli Friedmand3550112008-02-09 08:50:58 +0000579}
Devang Patel41b66252007-10-23 20:28:39 +0000580
Eli Friedmand3550112008-02-09 08:50:58 +0000581LValue CodeGenFunction::EmitLValueForField(llvm::Value* BaseValue,
582 FieldDecl* Field,
Eli Friedman2e630542008-06-13 23:01:12 +0000583 bool isUnion,
584 unsigned CVRQualifiers)
Eli Friedmand3550112008-02-09 08:50:58 +0000585{
586 llvm::Value *V;
587 unsigned idx = CGM.getTypes().getLLVMFieldNo(Field);
Lauro Ramos Venancio63fc38f2008-02-07 19:29:53 +0000588
Eli Friedman66813742008-05-29 11:33:25 +0000589 if (Field->isBitField()) {
Eli Friedmana04e70d2008-05-17 20:03:47 +0000590 // FIXME: CodeGenTypes should expose a method to get the appropriate
591 // type for FieldTy (the appropriate type is ABI-dependent).
Eli Friedman070fee42008-06-01 15:16:01 +0000592 const llvm::Type *FieldTy = CGM.getTypes().ConvertTypeForMem(Field->getType());
Chris Lattner07307562008-03-19 05:19:41 +0000593 const llvm::PointerType *BaseTy =
Lauro Ramos Venancio63fc38f2008-02-07 19:29:53 +0000594 cast<llvm::PointerType>(BaseValue->getType());
595 unsigned AS = BaseTy->getAddressSpace();
596 BaseValue = Builder.CreateBitCast(BaseValue,
597 llvm::PointerType::get(FieldTy, AS),
598 "tmp");
599 V = Builder.CreateGEP(BaseValue,
600 llvm::ConstantInt::get(llvm::Type::Int32Ty, idx),
601 "tmp");
Eli Friedman66813742008-05-29 11:33:25 +0000602
603 CodeGenTypes::BitFieldInfo bitFieldInfo =
604 CGM.getTypes().getBitFieldInfo(Field);
605 return LValue::MakeBitfield(V, bitFieldInfo.Begin, bitFieldInfo.Size,
Eli Friedman2e630542008-06-13 23:01:12 +0000606 Field->getType()->isSignedIntegerType(),
607 Field->getType().getCVRQualifiers()|CVRQualifiers);
Lauro Ramos Venancio63fc38f2008-02-07 19:29:53 +0000608 }
Eli Friedman070fee42008-06-01 15:16:01 +0000609
Eli Friedman66813742008-05-29 11:33:25 +0000610 V = Builder.CreateStructGEP(BaseValue, idx, "tmp");
611
Devang Patel9b1ca9e2007-10-26 19:42:18 +0000612 // Match union field type.
Lauro Ramos Venancio63fc38f2008-02-07 19:29:53 +0000613 if (isUnion) {
Eli Friedman2e630542008-06-13 23:01:12 +0000614 const llvm::Type *FieldTy =
615 CGM.getTypes().ConvertTypeForMem(Field->getType());
Devang Patel0f2a8fb2007-10-30 20:59:40 +0000616 const llvm::PointerType * BaseTy =
617 cast<llvm::PointerType>(BaseValue->getType());
Eli Friedmancecdc6b2008-05-21 13:24:44 +0000618 unsigned AS = BaseTy->getAddressSpace();
619 V = Builder.CreateBitCast(V,
620 llvm::PointerType::get(FieldTy, AS),
621 "tmp");
Devang Patel9b1ca9e2007-10-26 19:42:18 +0000622 }
Lauro Ramos Venanciob40307c2008-01-22 20:17:04 +0000623
Eli Friedman2e630542008-06-13 23:01:12 +0000624 return LValue::MakeAddr(V,
625 Field->getType().getCVRQualifiers()|CVRQualifiers);
Devang Patel41b66252007-10-23 20:28:39 +0000626}
627
Eli Friedman2e630542008-06-13 23:01:12 +0000628LValue CodeGenFunction::EmitCompoundLiteralLValue(const CompoundLiteralExpr* E)
629{
Eli Friedmanf3c2cb42008-05-13 23:18:27 +0000630 const llvm::Type *LTy = ConvertType(E->getType());
631 llvm::Value *DeclPtr = CreateTempAlloca(LTy, ".compoundliteral");
632
633 const Expr* InitExpr = E->getInitializer();
Eli Friedman2e630542008-06-13 23:01:12 +0000634 LValue Result = LValue::MakeAddr(DeclPtr, E->getType().getCVRQualifiers());
Eli Friedmanf3c2cb42008-05-13 23:18:27 +0000635
636 if (E->getType()->isComplexType()) {
637 EmitComplexExprIntoAddr(InitExpr, DeclPtr, false);
638 } else if (hasAggregateLLVMType(E->getType())) {
639 EmitAnyExpr(InitExpr, DeclPtr, false);
640 } else {
641 EmitStoreThroughLValue(EmitAnyExpr(InitExpr), Result, E->getType());
642 }
643
644 return Result;
645}
646
Chris Lattner4b009652007-07-25 00:24:17 +0000647//===--------------------------------------------------------------------===//
648// Expression Emission
649//===--------------------------------------------------------------------===//
650
Chris Lattnerb2cb9cb2007-08-20 22:37:10 +0000651
Chris Lattner4b009652007-07-25 00:24:17 +0000652RValue CodeGenFunction::EmitCallExpr(const CallExpr *E) {
Anders Carlsson49865302007-08-20 18:05:56 +0000653 if (const ImplicitCastExpr *IcExpr =
654 dyn_cast<const ImplicitCastExpr>(E->getCallee()))
655 if (const DeclRefExpr *DRExpr =
656 dyn_cast<const DeclRefExpr>(IcExpr->getSubExpr()))
657 if (const FunctionDecl *FDecl =
658 dyn_cast<const FunctionDecl>(DRExpr->getDecl()))
659 if (unsigned builtinID = FDecl->getIdentifier()->getBuiltinID())
660 return EmitBuiltinExpr(builtinID, E);
661
Chris Lattner9fba49a2007-08-24 05:35:26 +0000662 llvm::Value *Callee = EmitScalarExpr(E->getCallee());
Eli Friedman261f4ad2008-01-30 01:32:06 +0000663 return EmitCallExpr(Callee, E->getCallee()->getType(),
Ted Kremenek2719e982008-06-17 02:43:46 +0000664 E->arg_begin(), E->arg_end());
Nate Begeman9f3bfb72008-01-17 17:46:27 +0000665}
666
Ted Kremenek2719e982008-06-17 02:43:46 +0000667RValue CodeGenFunction::EmitCallExpr(Expr *FnExpr,
668 CallExpr::const_arg_iterator ArgBeg,
669 CallExpr::const_arg_iterator ArgEnd) {
670
Nate Begeman9f3bfb72008-01-17 17:46:27 +0000671 llvm::Value *Callee = EmitScalarExpr(FnExpr);
Ted Kremenek2719e982008-06-17 02:43:46 +0000672 return EmitCallExpr(Callee, FnExpr->getType(), ArgBeg, ArgEnd);
Chris Lattner02c60f52007-08-31 04:44:06 +0000673}
674
Christopher Lambad327ba2007-12-29 05:02:41 +0000675LValue CodeGenFunction::EmitCallExprLValue(const CallExpr *E) {
676 // Can only get l-value for call expression returning aggregate type
677 RValue RV = EmitCallExpr(E);
Eli Friedman2e630542008-06-13 23:01:12 +0000678 // FIXME: can this be volatile?
679 return LValue::MakeAddr(RV.getAggregateAddr(),
680 E->getType().getCVRQualifiers());
Christopher Lambad327ba2007-12-29 05:02:41 +0000681}
682
Chris Lattnerb326b172008-03-30 23:03:07 +0000683LValue CodeGenFunction::EmitObjCIvarRefLValue(const ObjCIvarRefExpr *E) {
684 // Objective-C objects are traditionally C structures with their layout
685 // defined at compile-time. In some implementations, their layout is not
686 // defined until run time in order to allow instance variables to be added to
687 // a class without recompiling all of the subclasses. If this is the case
688 // then the CGObjCRuntime subclass must return true to LateBoundIvars and
689 // implement the lookup itself.
Chris Lattnerc61e9f82008-03-30 23:25:33 +0000690 if (CGM.getObjCRuntime()->LateBoundIVars()) {
Chris Lattnerb326b172008-03-30 23:03:07 +0000691 assert(0 && "FIXME: Implement support for late-bound instance variables");
692 return LValue(); // Not reached.
693 }
Chris Lattnerc61e9f82008-03-30 23:25:33 +0000694
695 // Get a structure type for the object
696 QualType ExprTy = E->getBase()->getType();
697 const llvm::Type *ObjectType = ConvertType(ExprTy);
698 // TODO: Add a special case for isa (index 0)
699 // Work out which index the ivar is
700 const ObjCIvarDecl *Decl = E->getDecl();
701 unsigned Index = CGM.getTypes().getLLVMFieldNo(Decl);
Chris Lattnerb326b172008-03-30 23:03:07 +0000702
Chris Lattnerc61e9f82008-03-30 23:25:33 +0000703 // Get object pointer and coerce object pointer to correct type.
704 llvm::Value *Object = EmitLValue(E->getBase()).getAddress();
Eli Friedman2e630542008-06-13 23:01:12 +0000705 // FIXME: Volatility
Chris Lattner6e6a5972008-04-04 04:07:35 +0000706 Object = Builder.CreateLoad(Object, E->getDecl()->getName());
Chris Lattnerc61e9f82008-03-30 23:25:33 +0000707 if (Object->getType() != ObjectType)
708 Object = Builder.CreateBitCast(Object, ObjectType);
Chris Lattner6e6a5972008-04-04 04:07:35 +0000709
Chris Lattnerc61e9f82008-03-30 23:25:33 +0000710
711 // Return a pointer to the right element.
Eli Friedman2e630542008-06-13 23:01:12 +0000712 // FIXME: volatile
Chris Lattnerc61e9f82008-03-30 23:25:33 +0000713 return LValue::MakeAddr(Builder.CreateStructGEP(Object, Index,
Eli Friedman2e630542008-06-13 23:01:12 +0000714 Decl->getName()),0);
Chris Lattnerb326b172008-03-30 23:03:07 +0000715}
716
Nate Begeman9f3bfb72008-01-17 17:46:27 +0000717RValue CodeGenFunction::EmitCallExpr(llvm::Value *Callee, QualType FnType,
Ted Kremenek2719e982008-06-17 02:43:46 +0000718 CallExpr::const_arg_iterator ArgBeg,
719 CallExpr::const_arg_iterator ArgEnd) {
720
Chris Lattner4b009652007-07-25 00:24:17 +0000721 // The callee type will always be a pointer to function type, get the function
722 // type.
Chris Lattnerc154ac12008-07-26 22:37:01 +0000723 FnType = FnType->getAsPointerType()->getPointeeType();
Chris Lattner69cc2f92008-07-31 04:58:58 +0000724 QualType ResultType = FnType->getAsFunctionType()->getResultType();
Eli Friedman261f4ad2008-01-30 01:32:06 +0000725
Chris Lattner4b009652007-07-25 00:24:17 +0000726 llvm::SmallVector<llvm::Value*, 16> Args;
727
Chris Lattner59802042007-08-10 17:02:28 +0000728 // Handle struct-return functions by passing a pointer to the location that
729 // we would like to return into.
Nate Begeman9f3bfb72008-01-17 17:46:27 +0000730 if (hasAggregateLLVMType(ResultType)) {
Chris Lattner59802042007-08-10 17:02:28 +0000731 // Create a temporary alloca to hold the result of the call. :(
Nate Begeman9f3bfb72008-01-17 17:46:27 +0000732 Args.push_back(CreateTempAlloca(ConvertType(ResultType)));
Chris Lattner59802042007-08-10 17:02:28 +0000733 // FIXME: set the stret attribute on the argument.
734 }
735
Ted Kremenek2719e982008-06-17 02:43:46 +0000736 for (CallExpr::const_arg_iterator I = ArgBeg; I != ArgEnd; ++I) {
737 QualType ArgTy = I->getType();
Eli Friedmand3550112008-02-09 08:50:58 +0000738
Chris Lattnerb06c8dd2007-08-26 22:55:13 +0000739 if (!hasAggregateLLVMType(ArgTy)) {
740 // Scalar argument is passed by-value.
Ted Kremenek2719e982008-06-17 02:43:46 +0000741 Args.push_back(EmitScalarExpr(*I));
Chris Lattnerde0908b2008-04-04 16:54:41 +0000742 } else if (ArgTy->isAnyComplexType()) {
Chris Lattnerb06c8dd2007-08-26 22:55:13 +0000743 // Make a temporary alloca to pass the argument.
744 llvm::Value *DestMem = CreateTempAlloca(ConvertType(ArgTy));
Ted Kremenek2719e982008-06-17 02:43:46 +0000745 EmitComplexExprIntoAddr(*I, DestMem, false);
Chris Lattnerb06c8dd2007-08-26 22:55:13 +0000746 Args.push_back(DestMem);
747 } else {
748 llvm::Value *DestMem = CreateTempAlloca(ConvertType(ArgTy));
Ted Kremenek2719e982008-06-17 02:43:46 +0000749 EmitAggExpr(*I, DestMem, false);
Chris Lattnerb06c8dd2007-08-26 22:55:13 +0000750 Args.push_back(DestMem);
Chris Lattner4b009652007-07-25 00:24:17 +0000751 }
Chris Lattner4b009652007-07-25 00:24:17 +0000752 }
753
Nate Begemandc6262e2008-03-09 03:09:36 +0000754 llvm::CallInst *CI = Builder.CreateCall(Callee,&Args[0],&Args[0]+Args.size());
Eli Friedman9be42212008-06-01 15:54:49 +0000755
756 // Note that there is parallel code in SetFunctionAttributes in CodeGenModule
757 llvm::SmallVector<llvm::ParamAttrsWithIndex, 8> ParamAttrList;
758 if (hasAggregateLLVMType(ResultType))
759 ParamAttrList.push_back(
760 llvm::ParamAttrsWithIndex::get(1, llvm::ParamAttr::StructRet));
761 unsigned increment = hasAggregateLLVMType(ResultType) ? 2 : 1;
Ted Kremenek2719e982008-06-17 02:43:46 +0000762
763 unsigned i = 0;
764 for (CallExpr::const_arg_iterator I = ArgBeg; I != ArgEnd; ++I, ++i) {
765 QualType ParamType = I->getType();
Eli Friedman9be42212008-06-01 15:54:49 +0000766 unsigned ParamAttrs = 0;
767 if (ParamType->isRecordType())
768 ParamAttrs |= llvm::ParamAttr::ByVal;
769 if (ParamType->isSignedIntegerType() && ParamType->isPromotableIntegerType())
770 ParamAttrs |= llvm::ParamAttr::SExt;
771 if (ParamType->isUnsignedIntegerType() && ParamType->isPromotableIntegerType())
772 ParamAttrs |= llvm::ParamAttr::ZExt;
773 if (ParamAttrs)
774 ParamAttrList.push_back(llvm::ParamAttrsWithIndex::get(i + increment,
775 ParamAttrs));
776 }
777 CI->setParamAttrs(llvm::PAListPtr::get(ParamAttrList.begin(),
778 ParamAttrList.size()));
779
Nate Begemandc6262e2008-03-09 03:09:36 +0000780 if (const llvm::Function *F = dyn_cast<llvm::Function>(Callee))
781 CI->setCallingConv(F->getCallingConv());
782 if (CI->getType() != llvm::Type::VoidTy)
783 CI->setName("call");
Chris Lattnerde0908b2008-04-04 16:54:41 +0000784 else if (ResultType->isAnyComplexType())
Chris Lattnere24c4cf2007-08-31 22:49:20 +0000785 return RValue::getComplex(LoadComplexFromAddr(Args[0], false));
Nate Begeman9f3bfb72008-01-17 17:46:27 +0000786 else if (hasAggregateLLVMType(ResultType))
Chris Lattner59802042007-08-10 17:02:28 +0000787 // Struct return.
788 return RValue::getAggregate(Args[0]);
Chris Lattner307da022007-11-30 17:56:23 +0000789 else {
790 // void return.
Nate Begeman9f3bfb72008-01-17 17:46:27 +0000791 assert(ResultType->isVoidType() && "Should only have a void expr here");
Nate Begemandc6262e2008-03-09 03:09:36 +0000792 CI = 0;
Chris Lattner307da022007-11-30 17:56:23 +0000793 }
Chris Lattner59802042007-08-10 17:02:28 +0000794
Nate Begemandc6262e2008-03-09 03:09:36 +0000795 return RValue::get(CI);
Chris Lattner4b009652007-07-25 00:24:17 +0000796}