blob: 89f552225c1a31f386cf14e52961cba97b8dc0a1 [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"
Daniel Dunbareee5cd12008-08-11 05:00:27 +000016#include "clang/AST/ASTContext.h"
Daniel Dunbar64789f82008-08-11 05:35:13 +000017#include "clang/AST/DeclObjC.h"
Eli Friedmana04e70d2008-05-17 20:03:47 +000018#include "llvm/Target/TargetData.h"
Chris Lattner4b009652007-07-25 00:24:17 +000019using namespace clang;
20using namespace CodeGen;
21
22//===--------------------------------------------------------------------===//
23// Miscellaneous Helper Methods
24//===--------------------------------------------------------------------===//
25
26/// CreateTempAlloca - This creates a alloca and inserts it into the entry
27/// block.
28llvm::AllocaInst *CodeGenFunction::CreateTempAlloca(const llvm::Type *Ty,
29 const char *Name) {
30 return new llvm::AllocaInst(Ty, 0, Name, AllocaInsertPt);
31}
32
33/// EvaluateExprAsBool - Perform the usual unary conversions on the specified
34/// expression and compare the result against zero, returning an Int1Ty value.
35llvm::Value *CodeGenFunction::EvaluateExprAsBool(const Expr *E) {
Chris Lattnercc50a512007-08-26 16:46:58 +000036 QualType BoolTy = getContext().BoolTy;
Chris Lattnerde0908b2008-04-04 16:54:41 +000037 if (!E->getType()->isAnyComplexType())
Chris Lattnercc50a512007-08-26 16:46:58 +000038 return EmitScalarConversion(EmitScalarExpr(E), E->getType(), BoolTy);
Chris Lattner4b009652007-07-25 00:24:17 +000039
Chris Lattnercc50a512007-08-26 16:46:58 +000040 return EmitComplexToScalarConversion(EmitComplexExpr(E), E->getType(),BoolTy);
Chris Lattner4b009652007-07-25 00:24:17 +000041}
42
Chris Lattnere24c4cf2007-08-31 22:49:20 +000043/// EmitAnyExpr - Emit code to compute the specified expression which can have
44/// any type. The result is returned as an RValue struct. If this is an
45/// aggregate expression, the aggloc/agglocvolatile arguments indicate where
46/// the result should be returned.
47RValue CodeGenFunction::EmitAnyExpr(const Expr *E, llvm::Value *AggLoc,
48 bool isAggLocVolatile) {
49 if (!hasAggregateLLVMType(E->getType()))
50 return RValue::get(EmitScalarExpr(E));
Chris Lattnerde0908b2008-04-04 16:54:41 +000051 else if (E->getType()->isAnyComplexType())
Chris Lattnere24c4cf2007-08-31 22:49:20 +000052 return RValue::getComplex(EmitComplexExpr(E));
53
54 EmitAggExpr(E, AggLoc, isAggLocVolatile);
55 return RValue::getAggregate(AggLoc);
56}
57
Dan Gohman4751a3a2008-05-22 00:50:06 +000058/// getAccessedFieldNo - Given an encoded value and a result number, return
59/// the input field number being accessed.
60unsigned CodeGenFunction::getAccessedFieldNo(unsigned Idx,
61 const llvm::Constant *Elts) {
62 if (isa<llvm::ConstantAggregateZero>(Elts))
63 return 0;
64
65 return cast<llvm::ConstantInt>(Elts->getOperand(Idx))->getZExtValue();
66}
67
Chris Lattnere24c4cf2007-08-31 22:49:20 +000068
Chris Lattner4b009652007-07-25 00:24:17 +000069//===----------------------------------------------------------------------===//
70// LValue Expression Emission
71//===----------------------------------------------------------------------===//
72
73/// EmitLValue - Emit code to compute a designator that specifies the location
74/// of the expression.
75///
76/// This can return one of two things: a simple address or a bitfield
77/// reference. In either case, the LLVM Value* in the LValue structure is
78/// guaranteed to be an LLVM pointer type.
79///
80/// If this returns a bitfield reference, nothing about the pointee type of
81/// the LLVM value is known: For example, it may not be a pointer to an
82/// integer.
83///
84/// If this returns a normal address, and if the lvalue's C type is fixed
85/// size, this method guarantees that the returned pointer type will point to
86/// an LLVM type of the same size of the lvalue's type. If the lvalue has a
87/// variable length type, this is not possible.
88///
89LValue CodeGenFunction::EmitLValue(const Expr *E) {
90 switch (E->getStmtClass()) {
Chris Lattnera52c8892007-08-26 05:06:40 +000091 default: {
Chris Lattnerc61e9f82008-03-30 23:25:33 +000092 printf("Statement class: %d\n", E->getStmtClass());
Chris Lattnere8f49632007-12-02 01:49:16 +000093 WarnUnsupported(E, "l-value expression");
Christopher Lamb4fe5e702007-12-17 01:11:20 +000094 llvm::Type *Ty = llvm::PointerType::getUnqual(ConvertType(E->getType()));
Eli Friedman2e630542008-06-13 23:01:12 +000095 return LValue::MakeAddr(llvm::UndefValue::get(Ty),
96 E->getType().getCVRQualifiers());
Chris Lattnera52c8892007-08-26 05:06:40 +000097 }
Chris Lattner4b009652007-07-25 00:24:17 +000098
Christopher Lambad327ba2007-12-29 05:02:41 +000099 case Expr::CallExprClass: return EmitCallExprLValue(cast<CallExpr>(E));
Chris Lattner4b009652007-07-25 00:24:17 +0000100 case Expr::DeclRefExprClass: return EmitDeclRefLValue(cast<DeclRefExpr>(E));
101 case Expr::ParenExprClass:return EmitLValue(cast<ParenExpr>(E)->getSubExpr());
Chris Lattner69909292008-08-10 01:53:14 +0000102 case Expr::PredefinedExprClass:
103 return EmitPredefinedLValue(cast<PredefinedExpr>(E));
Chris Lattner4b009652007-07-25 00:24:17 +0000104 case Expr::StringLiteralClass:
105 return EmitStringLiteralLValue(cast<StringLiteral>(E));
Chris Lattnerb326b172008-03-30 23:03:07 +0000106
107 case Expr::ObjCIvarRefExprClass:
108 return EmitObjCIvarRefLValue(cast<ObjCIvarRefExpr>(E));
Chris Lattner4b009652007-07-25 00:24:17 +0000109
110 case Expr::UnaryOperatorClass:
111 return EmitUnaryOpLValue(cast<UnaryOperator>(E));
112 case Expr::ArraySubscriptExprClass:
113 return EmitArraySubscriptExpr(cast<ArraySubscriptExpr>(E));
Nate Begemanaf6ed502008-04-18 23:10:10 +0000114 case Expr::ExtVectorElementExprClass:
115 return EmitExtVectorElementExpr(cast<ExtVectorElementExpr>(E));
Devang Patel41b66252007-10-23 20:28:39 +0000116 case Expr::MemberExprClass: return EmitMemberExpr(cast<MemberExpr>(E));
Eli Friedmanf3c2cb42008-05-13 23:18:27 +0000117 case Expr::CompoundLiteralExprClass:
118 return EmitCompoundLiteralLValue(cast<CompoundLiteralExpr>(E));
Chris Lattner4b009652007-07-25 00:24:17 +0000119 }
120}
121
122/// EmitLoadOfLValue - Given an expression that represents a value lvalue,
123/// this method emits the address of the lvalue, then loads the result as an
124/// rvalue, returning the rvalue.
125RValue CodeGenFunction::EmitLoadOfLValue(LValue LV, QualType ExprType) {
Chris Lattner4b009652007-07-25 00:24:17 +0000126 if (LV.isSimple()) {
127 llvm::Value *Ptr = LV.getAddress();
128 const llvm::Type *EltTy =
129 cast<llvm::PointerType>(Ptr->getType())->getElementType();
130
131 // Simple scalar l-value.
Dan Gohman377ba9f2008-05-22 22:12:56 +0000132 if (EltTy->isSingleValueType()) {
Eli Friedman2e630542008-06-13 23:01:12 +0000133 llvm::Value *V = Builder.CreateLoad(Ptr, LV.isVolatileQualified(),"tmp");
Chris Lattner6b79f4e2008-01-30 07:01:17 +0000134
135 // Bool can have different representation in memory than in registers.
136 if (ExprType->isBooleanType()) {
137 if (V->getType() != llvm::Type::Int1Ty)
138 V = Builder.CreateTrunc(V, llvm::Type::Int1Ty, "tobool");
139 }
140
141 return RValue::get(V);
142 }
Chris Lattner4b009652007-07-25 00:24:17 +0000143
Chris Lattnerbdb8ffb2007-08-11 00:04:45 +0000144 assert(ExprType->isFunctionType() && "Unknown scalar value");
145 return RValue::get(Ptr);
Chris Lattner4b009652007-07-25 00:24:17 +0000146 }
147
148 if (LV.isVectorElt()) {
Eli Friedman2e630542008-06-13 23:01:12 +0000149 llvm::Value *Vec = Builder.CreateLoad(LV.getVectorAddr(),
150 LV.isVolatileQualified(), "tmp");
Chris Lattner4b009652007-07-25 00:24:17 +0000151 return RValue::get(Builder.CreateExtractElement(Vec, LV.getVectorIdx(),
152 "vecext"));
153 }
Chris Lattnera735fac2007-08-03 00:16:29 +0000154
155 // If this is a reference to a subset of the elements of a vector, either
156 // shuffle the input or extract/insert them as appropriate.
Nate Begemanaf6ed502008-04-18 23:10:10 +0000157 if (LV.isExtVectorElt())
158 return EmitLoadOfExtVectorElementLValue(LV, ExprType);
Lauro Ramos Venanciob40307c2008-01-22 20:17:04 +0000159
160 if (LV.isBitfield())
161 return EmitLoadOfBitfieldLValue(LV, ExprType);
162
163 assert(0 && "Unknown LValue type!");
Chris Lattner1d2b4612007-09-16 19:23:47 +0000164 //an invalid RValue, but the assert will
165 //ensure that this point is never reached
166 return RValue();
Chris Lattner4b009652007-07-25 00:24:17 +0000167}
168
Lauro Ramos Venanciob40307c2008-01-22 20:17:04 +0000169RValue CodeGenFunction::EmitLoadOfBitfieldLValue(LValue LV,
170 QualType ExprType) {
Daniel Dunbar833b03b2008-08-06 05:08:45 +0000171 unsigned StartBit = LV.getBitfieldStartBit();
172 unsigned BitfieldSize = LV.getBitfieldSize();
Lauro Ramos Venanciob40307c2008-01-22 20:17:04 +0000173 llvm::Value *Ptr = LV.getBitfieldAddr();
Daniel Dunbar833b03b2008-08-06 05:08:45 +0000174
175 const llvm::Type *EltTy =
Lauro Ramos Venanciob40307c2008-01-22 20:17:04 +0000176 cast<llvm::PointerType>(Ptr->getType())->getElementType();
Daniel Dunbar833b03b2008-08-06 05:08:45 +0000177 unsigned EltTySize = CGM.getTargetData().getTypeSizeInBits(EltTy);
Lauro Ramos Venanciob40307c2008-01-22 20:17:04 +0000178
Daniel Dunbar833b03b2008-08-06 05:08:45 +0000179 // In some cases the bitfield may straddle two memory locations.
180 // Currently we load the entire bitfield, then do the magic to
181 // sign-extend it if necessary. This results in somewhat more code
182 // than necessary for the common case (one load), since two shifts
183 // accomplish both the masking and sign extension.
184 unsigned LowBits = std::min(BitfieldSize, EltTySize - StartBit);
185 llvm::Value *Val = Builder.CreateLoad(Ptr, LV.isVolatileQualified(), "tmp");
186
187 // Shift to proper location.
188 Val = Builder.CreateLShr(Val, llvm::ConstantInt::get(EltTy, StartBit),
189 "bf.lo");
190
191 // Mask off unused bits.
192 llvm::Constant *LowMask =
193 llvm::ConstantInt::get(llvm::APInt::getLowBitsSet(EltTySize, LowBits));
194 Val = Builder.CreateAnd(Val, LowMask, "bf.lo.cleared");
195
196 // Fetch the high bits if necessary.
197 if (LowBits < BitfieldSize) {
198 unsigned HighBits = BitfieldSize - LowBits;
199 llvm::Value *HighPtr =
200 Builder.CreateGEP(Ptr, llvm::ConstantInt::get(llvm::Type::Int32Ty, 1),
201 "bf.ptr.hi");
202 llvm::Value *HighVal = Builder.CreateLoad(HighPtr,
203 LV.isVolatileQualified(),
204 "tmp");
205
206 // Mask off unused bits.
207 llvm::Constant *HighMask =
208 llvm::ConstantInt::get(llvm::APInt::getLowBitsSet(EltTySize, HighBits));
209 HighVal = Builder.CreateAnd(HighVal, HighMask, "bf.lo.cleared");
Lauro Ramos Venanciob40307c2008-01-22 20:17:04 +0000210
Daniel Dunbar833b03b2008-08-06 05:08:45 +0000211 // Shift to proper location and or in to bitfield value.
212 HighVal = Builder.CreateShl(HighVal,
213 llvm::ConstantInt::get(EltTy, LowBits));
214 Val = Builder.CreateOr(Val, HighVal, "bf.val");
215 }
Lauro Ramos Venanciob40307c2008-01-22 20:17:04 +0000216
Daniel Dunbar833b03b2008-08-06 05:08:45 +0000217 // Sign extend if necessary.
218 if (LV.isBitfieldSigned()) {
219 llvm::Value *ExtraBits = llvm::ConstantInt::get(EltTy,
220 EltTySize - BitfieldSize);
221 Val = Builder.CreateAShr(Builder.CreateShl(Val, ExtraBits),
222 ExtraBits, "bf.val.sext");
223 }
Eli Friedmana04e70d2008-05-17 20:03:47 +0000224
225 // The bitfield type and the normal type differ when the storage sizes
226 // differ (currently just _Bool).
Daniel Dunbar833b03b2008-08-06 05:08:45 +0000227 Val = Builder.CreateIntCast(Val, ConvertType(ExprType), false, "tmp");
Eli Friedmana04e70d2008-05-17 20:03:47 +0000228
Daniel Dunbar833b03b2008-08-06 05:08:45 +0000229 return RValue::get(Val);
Lauro Ramos Venanciob40307c2008-01-22 20:17:04 +0000230}
231
Chris Lattner944f7962007-08-03 16:18:34 +0000232// If this is a reference to a subset of the elements of a vector, either
233// shuffle the input or extract/insert them as appropriate.
Nate Begemanaf6ed502008-04-18 23:10:10 +0000234RValue CodeGenFunction::EmitLoadOfExtVectorElementLValue(LValue LV,
235 QualType ExprType) {
Eli Friedman2e630542008-06-13 23:01:12 +0000236 llvm::Value *Vec = Builder.CreateLoad(LV.getExtVectorAddr(),
237 LV.isVolatileQualified(), "tmp");
Chris Lattner944f7962007-08-03 16:18:34 +0000238
Nate Begemanc8e51f82008-05-09 06:41:27 +0000239 const llvm::Constant *Elts = LV.getExtVectorElts();
Chris Lattner944f7962007-08-03 16:18:34 +0000240
241 // If the result of the expression is a non-vector type, we must be
242 // extracting a single element. Just codegen as an extractelement.
Chris Lattner4b492962007-08-10 17:10:08 +0000243 const VectorType *ExprVT = ExprType->getAsVectorType();
244 if (!ExprVT) {
Dan Gohman4751a3a2008-05-22 00:50:06 +0000245 unsigned InIdx = getAccessedFieldNo(0, Elts);
Chris Lattner944f7962007-08-03 16:18:34 +0000246 llvm::Value *Elt = llvm::ConstantInt::get(llvm::Type::Int32Ty, InIdx);
247 return RValue::get(Builder.CreateExtractElement(Vec, Elt, "tmp"));
248 }
249
250 // If the source and destination have the same number of elements, use a
251 // vector shuffle instead of insert/extracts.
Chris Lattner4b492962007-08-10 17:10:08 +0000252 unsigned NumResultElts = ExprVT->getNumElements();
Chris Lattner944f7962007-08-03 16:18:34 +0000253 unsigned NumSourceElts =
254 cast<llvm::VectorType>(Vec->getType())->getNumElements();
255
256 if (NumResultElts == NumSourceElts) {
257 llvm::SmallVector<llvm::Constant*, 4> Mask;
258 for (unsigned i = 0; i != NumResultElts; ++i) {
Dan Gohman4751a3a2008-05-22 00:50:06 +0000259 unsigned InIdx = getAccessedFieldNo(i, Elts);
Chris Lattner944f7962007-08-03 16:18:34 +0000260 Mask.push_back(llvm::ConstantInt::get(llvm::Type::Int32Ty, InIdx));
261 }
262
263 llvm::Value *MaskV = llvm::ConstantVector::get(&Mask[0], Mask.size());
264 Vec = Builder.CreateShuffleVector(Vec,
265 llvm::UndefValue::get(Vec->getType()),
266 MaskV, "tmp");
267 return RValue::get(Vec);
268 }
269
270 // Start out with an undef of the result type.
271 llvm::Value *Result = llvm::UndefValue::get(ConvertType(ExprType));
272
273 // Extract/Insert each element of the result.
274 for (unsigned i = 0; i != NumResultElts; ++i) {
Dan Gohman4751a3a2008-05-22 00:50:06 +0000275 unsigned InIdx = getAccessedFieldNo(i, Elts);
Chris Lattner944f7962007-08-03 16:18:34 +0000276 llvm::Value *Elt = llvm::ConstantInt::get(llvm::Type::Int32Ty, InIdx);
277 Elt = Builder.CreateExtractElement(Vec, Elt, "tmp");
278
279 llvm::Value *OutIdx = llvm::ConstantInt::get(llvm::Type::Int32Ty, i);
280 Result = Builder.CreateInsertElement(Result, Elt, OutIdx, "tmp");
281 }
282
283 return RValue::get(Result);
284}
285
286
Chris Lattner4b009652007-07-25 00:24:17 +0000287
288/// EmitStoreThroughLValue - Store the specified rvalue into the specified
289/// lvalue, where both are guaranteed to the have the same type, and that type
290/// is 'Ty'.
291void CodeGenFunction::EmitStoreThroughLValue(RValue Src, LValue Dst,
292 QualType Ty) {
Chris Lattner5bfdd232007-08-03 16:28:33 +0000293 if (!Dst.isSimple()) {
294 if (Dst.isVectorElt()) {
295 // Read/modify/write the vector, inserting the new element.
Eli Friedman2e630542008-06-13 23:01:12 +0000296 llvm::Value *Vec = Builder.CreateLoad(Dst.getVectorAddr(),
297 Dst.isVolatileQualified(), "tmp");
Chris Lattnere24c4cf2007-08-31 22:49:20 +0000298 Vec = Builder.CreateInsertElement(Vec, Src.getScalarVal(),
Chris Lattner5bfdd232007-08-03 16:28:33 +0000299 Dst.getVectorIdx(), "vecins");
Eli Friedman2e630542008-06-13 23:01:12 +0000300 Builder.CreateStore(Vec, Dst.getVectorAddr(),Dst.isVolatileQualified());
Chris Lattner5bfdd232007-08-03 16:28:33 +0000301 return;
302 }
Chris Lattner4b009652007-07-25 00:24:17 +0000303
Nate Begemanaf6ed502008-04-18 23:10:10 +0000304 // If this is an update of extended vector elements, insert them as
305 // appropriate.
306 if (Dst.isExtVectorElt())
307 return EmitStoreThroughExtVectorComponentLValue(Src, Dst, Ty);
Lauro Ramos Venancio2d7a34c2008-01-22 22:36:45 +0000308
309 if (Dst.isBitfield())
310 return EmitStoreThroughBitfieldLValue(Src, Dst, Ty);
311
Lauro Ramos Venancio14d39842008-01-22 22:38:35 +0000312 assert(0 && "Unknown LValue type");
Chris Lattner5bfdd232007-08-03 16:28:33 +0000313 }
Chris Lattner4b009652007-07-25 00:24:17 +0000314
315 llvm::Value *DstAddr = Dst.getAddress();
Chris Lattnerbdb8ffb2007-08-11 00:04:45 +0000316 assert(Src.isScalar() && "Can't emit an agg store with this method");
317 // FIXME: Handle volatility etc.
Chris Lattnere24c4cf2007-08-31 22:49:20 +0000318 const llvm::Type *SrcTy = Src.getScalarVal()->getType();
Christopher Lamb4fe5e702007-12-17 01:11:20 +0000319 const llvm::PointerType *DstPtr = cast<llvm::PointerType>(DstAddr->getType());
320 const llvm::Type *AddrTy = DstPtr->getElementType();
321 unsigned AS = DstPtr->getAddressSpace();
Chris Lattner4b009652007-07-25 00:24:17 +0000322
Chris Lattnerbdb8ffb2007-08-11 00:04:45 +0000323 if (AddrTy != SrcTy)
Christopher Lamb4fe5e702007-12-17 01:11:20 +0000324 DstAddr = Builder.CreateBitCast(DstAddr,
325 llvm::PointerType::get(SrcTy, AS),
Chris Lattnerbdb8ffb2007-08-11 00:04:45 +0000326 "storetmp");
Eli Friedman2e630542008-06-13 23:01:12 +0000327 Builder.CreateStore(Src.getScalarVal(), DstAddr, Dst.isVolatileQualified());
Chris Lattner4b009652007-07-25 00:24:17 +0000328}
329
Lauro Ramos Venancio2d7a34c2008-01-22 22:36:45 +0000330void CodeGenFunction::EmitStoreThroughBitfieldLValue(RValue Src, LValue Dst,
331 QualType Ty) {
Daniel Dunbar833b03b2008-08-06 05:08:45 +0000332 unsigned StartBit = Dst.getBitfieldStartBit();
333 unsigned BitfieldSize = Dst.getBitfieldSize();
Lauro Ramos Venancio2d7a34c2008-01-22 22:36:45 +0000334 llvm::Value *Ptr = Dst.getBitfieldAddr();
Lauro Ramos Venancio2d7a34c2008-01-22 22:36:45 +0000335
Daniel Dunbar833b03b2008-08-06 05:08:45 +0000336 const llvm::Type *EltTy =
337 cast<llvm::PointerType>(Ptr->getType())->getElementType();
338 unsigned EltTySize = CGM.getTargetData().getTypeSizeInBits(EltTy);
339
340 // Get the new value, cast to the appropriate type and masked to
341 // exactly the size of the bit-field.
Lauro Ramos Venancio2d7a34c2008-01-22 22:36:45 +0000342 llvm::Value *NewVal = Src.getScalarVal();
Daniel Dunbar833b03b2008-08-06 05:08:45 +0000343 NewVal = Builder.CreateIntCast(NewVal, EltTy, false, "tmp");
344 llvm::Constant *Mask =
345 llvm::ConstantInt::get(llvm::APInt::getLowBitsSet(EltTySize, BitfieldSize));
346 NewVal = Builder.CreateAnd(NewVal, Mask, "bf.value");
Lauro Ramos Venancio2d7a34c2008-01-22 22:36:45 +0000347
Daniel Dunbar833b03b2008-08-06 05:08:45 +0000348 // In some cases the bitfield may straddle two memory locations.
349 // Emit the low part first and check to see if the high needs to be
350 // done.
351 unsigned LowBits = std::min(BitfieldSize, EltTySize - StartBit);
352 llvm::Value *LowVal = Builder.CreateLoad(Ptr, Dst.isVolatileQualified(),
353 "bf.prev.low");
Eli Friedmana04e70d2008-05-17 20:03:47 +0000354
Daniel Dunbar833b03b2008-08-06 05:08:45 +0000355 // Compute the mask for zero-ing the low part of this bitfield.
356 llvm::Constant *InvMask =
357 llvm::ConstantInt::get(~llvm::APInt::getBitsSet(EltTySize, StartBit,
358 StartBit + LowBits));
359
360 // Compute the new low part as
361 // LowVal = (LowVal & InvMask) | (NewVal << StartBit),
362 // with the shift of NewVal implicitly stripping the high bits.
363 llvm::Value *NewLowVal =
364 Builder.CreateShl(NewVal, llvm::ConstantInt::get(EltTy, StartBit),
365 "bf.value.lo");
366 LowVal = Builder.CreateAnd(LowVal, InvMask, "bf.prev.lo.cleared");
367 LowVal = Builder.CreateOr(LowVal, NewLowVal, "bf.new.lo");
368
369 // Write back.
370 Builder.CreateStore(LowVal, Ptr, Dst.isVolatileQualified());
Eli Friedmana04e70d2008-05-17 20:03:47 +0000371
Daniel Dunbar833b03b2008-08-06 05:08:45 +0000372 // If the low part doesn't cover the bitfield emit a high part.
373 if (LowBits < BitfieldSize) {
374 unsigned HighBits = BitfieldSize - LowBits;
375 llvm::Value *HighPtr =
376 Builder.CreateGEP(Ptr, llvm::ConstantInt::get(llvm::Type::Int32Ty, 1),
377 "bf.ptr.hi");
378 llvm::Value *HighVal = Builder.CreateLoad(HighPtr,
379 Dst.isVolatileQualified(),
380 "bf.prev.hi");
381
382 // Compute the mask for zero-ing the high part of this bitfield.
383 llvm::Constant *InvMask =
384 llvm::ConstantInt::get(~llvm::APInt::getLowBitsSet(EltTySize, HighBits));
385
386 // Compute the new high part as
387 // HighVal = (HighVal & InvMask) | (NewVal lshr LowBits),
388 // where the high bits of NewVal have already been cleared and the
389 // shift stripping the low bits.
390 llvm::Value *NewHighVal =
391 Builder.CreateLShr(NewVal, llvm::ConstantInt::get(EltTy, LowBits),
392 "bf.value.high");
393 HighVal = Builder.CreateAnd(HighVal, InvMask, "bf.prev.hi.cleared");
394 HighVal = Builder.CreateOr(HighVal, NewHighVal, "bf.new.hi");
395
396 // Write back.
397 Builder.CreateStore(HighVal, HighPtr, Dst.isVolatileQualified());
398 }
Lauro Ramos Venancio2d7a34c2008-01-22 22:36:45 +0000399}
400
Nate Begemanaf6ed502008-04-18 23:10:10 +0000401void CodeGenFunction::EmitStoreThroughExtVectorComponentLValue(RValue Src,
402 LValue Dst,
403 QualType Ty) {
Chris Lattner5bfdd232007-08-03 16:28:33 +0000404 // This access turns into a read/modify/write of the vector. Load the input
405 // value now.
Eli Friedman2e630542008-06-13 23:01:12 +0000406 llvm::Value *Vec = Builder.CreateLoad(Dst.getExtVectorAddr(),
407 Dst.isVolatileQualified(), "tmp");
Nate Begemanc8e51f82008-05-09 06:41:27 +0000408 const llvm::Constant *Elts = Dst.getExtVectorElts();
Chris Lattner5bfdd232007-08-03 16:28:33 +0000409
Chris Lattnere24c4cf2007-08-31 22:49:20 +0000410 llvm::Value *SrcVal = Src.getScalarVal();
Chris Lattner5bfdd232007-08-03 16:28:33 +0000411
Chris Lattner940966d2007-08-03 16:37:04 +0000412 if (const VectorType *VTy = Ty->getAsVectorType()) {
413 unsigned NumSrcElts = VTy->getNumElements();
414
415 // Extract/Insert each element.
416 for (unsigned i = 0; i != NumSrcElts; ++i) {
417 llvm::Value *Elt = llvm::ConstantInt::get(llvm::Type::Int32Ty, i);
418 Elt = Builder.CreateExtractElement(SrcVal, Elt, "tmp");
419
Dan Gohman4751a3a2008-05-22 00:50:06 +0000420 unsigned Idx = getAccessedFieldNo(i, Elts);
Chris Lattner940966d2007-08-03 16:37:04 +0000421 llvm::Value *OutIdx = llvm::ConstantInt::get(llvm::Type::Int32Ty, Idx);
422 Vec = Builder.CreateInsertElement(Vec, Elt, OutIdx, "tmp");
423 }
424 } else {
425 // If the Src is a scalar (not a vector) it must be updating one element.
Dan Gohman4751a3a2008-05-22 00:50:06 +0000426 unsigned InIdx = getAccessedFieldNo(0, Elts);
Chris Lattner5bfdd232007-08-03 16:28:33 +0000427 llvm::Value *Elt = llvm::ConstantInt::get(llvm::Type::Int32Ty, InIdx);
428 Vec = Builder.CreateInsertElement(Vec, SrcVal, Elt, "tmp");
Chris Lattner5bfdd232007-08-03 16:28:33 +0000429 }
430
Eli Friedman2e630542008-06-13 23:01:12 +0000431 Builder.CreateStore(Vec, Dst.getExtVectorAddr(), Dst.isVolatileQualified());
Chris Lattner5bfdd232007-08-03 16:28:33 +0000432}
433
Chris Lattner4b009652007-07-25 00:24:17 +0000434
435LValue CodeGenFunction::EmitDeclRefLValue(const DeclRefExpr *E) {
Steve Naroff72a6ebc2008-04-15 22:42:06 +0000436 const VarDecl *VD = dyn_cast<VarDecl>(E->getDecl());
437
Chris Lattner8c7c6a12008-06-17 18:05:57 +0000438 if (VD && (VD->isBlockVarDecl() || isa<ParmVarDecl>(VD) ||
439 isa<ImplicitParamDecl>(VD))) {
Lauro Ramos Venancio2348d972008-02-16 22:30:38 +0000440 if (VD->getStorageClass() == VarDecl::Extern)
Daniel Dunbar7bf5b3d2008-07-29 23:18:29 +0000441 return LValue::MakeAddr(CGM.GetAddrOfGlobalVar(VD),
Eli Friedman2e630542008-06-13 23:01:12 +0000442 E->getType().getCVRQualifiers());
Lauro Ramos Venancio2348d972008-02-16 22:30:38 +0000443 else {
Steve Naroff72a6ebc2008-04-15 22:42:06 +0000444 llvm::Value *V = LocalDeclMap[VD];
Lauro Ramos Venancio2348d972008-02-16 22:30:38 +0000445 assert(V && "BlockVarDecl not entered in LocalDeclMap?");
Eli Friedman2e630542008-06-13 23:01:12 +0000446 return LValue::MakeAddr(V, E->getType().getCVRQualifiers());
Lauro Ramos Venancio2348d972008-02-16 22:30:38 +0000447 }
Steve Naroff72a6ebc2008-04-15 22:42:06 +0000448 } else if (VD && VD->isFileVarDecl()) {
Daniel Dunbar7bf5b3d2008-07-29 23:18:29 +0000449 return LValue::MakeAddr(CGM.GetAddrOfGlobalVar(VD),
Eli Friedman2e630542008-06-13 23:01:12 +0000450 E->getType().getCVRQualifiers());
Steve Naroff72a6ebc2008-04-15 22:42:06 +0000451 } else if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(E->getDecl())) {
Daniel Dunbar7bf5b3d2008-07-29 23:18:29 +0000452 return LValue::MakeAddr(CGM.GetAddrOfFunction(FD),
Eli Friedman2e630542008-06-13 23:01:12 +0000453 E->getType().getCVRQualifiers());
Chris Lattner4b009652007-07-25 00:24:17 +0000454 }
Chris Lattner8c7c6a12008-06-17 18:05:57 +0000455 else if (const ImplicitParamDecl *IPD =
456 dyn_cast<ImplicitParamDecl>(E->getDecl())) {
457 llvm::Value *V = LocalDeclMap[IPD];
458 assert(V && "BlockVarDecl not entered in LocalDeclMap?");
459 return LValue::MakeAddr(V, E->getType().getCVRQualifiers());
460 }
Chris Lattner4b009652007-07-25 00:24:17 +0000461 assert(0 && "Unimp declref");
Chris Lattner1d2b4612007-09-16 19:23:47 +0000462 //an invalid LValue, but the assert will
463 //ensure that this point is never reached.
464 return LValue();
Chris Lattner4b009652007-07-25 00:24:17 +0000465}
466
467LValue CodeGenFunction::EmitUnaryOpLValue(const UnaryOperator *E) {
468 // __extension__ doesn't affect lvalue-ness.
469 if (E->getOpcode() == UnaryOperator::Extension)
470 return EmitLValue(E->getSubExpr());
471
Chris Lattnerc154ac12008-07-26 22:37:01 +0000472 QualType ExprTy = getContext().getCanonicalType(E->getSubExpr()->getType());
Chris Lattner5bf72022007-10-30 22:53:42 +0000473 switch (E->getOpcode()) {
474 default: assert(0 && "Unknown unary operator lvalue!");
475 case UnaryOperator::Deref:
Eli Friedman2e630542008-06-13 23:01:12 +0000476 return LValue::MakeAddr(EmitScalarExpr(E->getSubExpr()),
Chris Lattnerd5a56aa2008-07-26 22:17:49 +0000477 ExprTy->getAsPointerType()->getPointeeType()
478 .getCVRQualifiers());
Chris Lattner5bf72022007-10-30 22:53:42 +0000479 case UnaryOperator::Real:
480 case UnaryOperator::Imag:
481 LValue LV = EmitLValue(E->getSubExpr());
Chris Lattner07307562008-03-19 05:19:41 +0000482 unsigned Idx = E->getOpcode() == UnaryOperator::Imag;
483 return LValue::MakeAddr(Builder.CreateStructGEP(LV.getAddress(),
Chris Lattnerd5a56aa2008-07-26 22:17:49 +0000484 Idx, "idx"),
485 ExprTy.getCVRQualifiers());
Chris Lattner5bf72022007-10-30 22:53:42 +0000486 }
Chris Lattner4b009652007-07-25 00:24:17 +0000487}
488
489LValue CodeGenFunction::EmitStringLiteralLValue(const StringLiteral *E) {
Daniel Dunbar3c670e12008-08-10 20:25:57 +0000490 llvm::Constant *C =
491 CGM.GetAddrOfConstantString(CGM.getStringForStringLiteral(E));
Eli Friedman48ec5622008-05-19 17:51:16 +0000492
Daniel Dunbar3c670e12008-08-10 20:25:57 +0000493 return LValue::MakeAddr(C,0);
Chris Lattner4b009652007-07-25 00:24:17 +0000494}
495
Chris Lattner69909292008-08-10 01:53:14 +0000496LValue CodeGenFunction::EmitPredefinedLValue(const PredefinedExpr *E) {
Chris Lattner6e6a5972008-04-04 04:07:35 +0000497 std::string FunctionName;
498 if(const FunctionDecl *FD = dyn_cast<FunctionDecl>(CurFuncDecl)) {
499 FunctionName = FD->getName();
500 }
501 else {
502 assert(0 && "Attempting to load predefined constant for invalid decl type");
503 }
Chris Lattner4b009652007-07-25 00:24:17 +0000504 std::string GlobalVarName;
505
506 switch (E->getIdentType()) {
507 default:
508 assert(0 && "unknown pre-defined ident type");
Chris Lattner69909292008-08-10 01:53:14 +0000509 case PredefinedExpr::Func:
Chris Lattner4b009652007-07-25 00:24:17 +0000510 GlobalVarName = "__func__.";
511 break;
Chris Lattner69909292008-08-10 01:53:14 +0000512 case PredefinedExpr::Function:
Chris Lattner4b009652007-07-25 00:24:17 +0000513 GlobalVarName = "__FUNCTION__.";
514 break;
Chris Lattner69909292008-08-10 01:53:14 +0000515 case PredefinedExpr::PrettyFunction:
Chris Lattner4b009652007-07-25 00:24:17 +0000516 // FIXME:: Demangle C++ method names
517 GlobalVarName = "__PRETTY_FUNCTION__.";
518 break;
519 }
520
Chris Lattner6e6a5972008-04-04 04:07:35 +0000521 GlobalVarName += FunctionName;
Chris Lattner4b009652007-07-25 00:24:17 +0000522
523 // FIXME: Can cache/reuse these within the module.
524 llvm::Constant *C=llvm::ConstantArray::get(FunctionName);
525
526 // Create a global variable for this.
527 C = new llvm::GlobalVariable(C->getType(), true,
528 llvm::GlobalValue::InternalLinkage,
529 C, GlobalVarName, CurFn->getParent());
Eli Friedman2e630542008-06-13 23:01:12 +0000530 return LValue::MakeAddr(C,0);
Chris Lattner4b009652007-07-25 00:24:17 +0000531}
532
533LValue CodeGenFunction::EmitArraySubscriptExpr(const ArraySubscriptExpr *E) {
Ted Kremenek1c1700f2007-08-20 16:18:38 +0000534 // The index must always be an integer, which is not an aggregate. Emit it.
Chris Lattner9fba49a2007-08-24 05:35:26 +0000535 llvm::Value *Idx = EmitScalarExpr(E->getIdx());
Chris Lattner4b009652007-07-25 00:24:17 +0000536
537 // If the base is a vector type, then we are forming a vector element lvalue
538 // with this subscript.
Eli Friedman2e630542008-06-13 23:01:12 +0000539 if (E->getBase()->getType()->isVectorType()) {
Chris Lattner4b009652007-07-25 00:24:17 +0000540 // Emit the vector as an lvalue to get its address.
Eli Friedman2e630542008-06-13 23:01:12 +0000541 LValue LHS = EmitLValue(E->getBase());
Ted Kremenek1c1700f2007-08-20 16:18:38 +0000542 assert(LHS.isSimple() && "Can only subscript lvalue vectors here!");
Chris Lattner4b009652007-07-25 00:24:17 +0000543 // FIXME: This should properly sign/zero/extend or truncate Idx to i32.
Eli Friedman2e630542008-06-13 23:01:12 +0000544 return LValue::MakeVectorElt(LHS.getAddress(), Idx,
545 E->getBase()->getType().getCVRQualifiers());
Chris Lattner4b009652007-07-25 00:24:17 +0000546 }
547
Ted Kremenek1c1700f2007-08-20 16:18:38 +0000548 // The base must be a pointer, which is not an aggregate. Emit it.
Chris Lattner9fba49a2007-08-24 05:35:26 +0000549 llvm::Value *Base = EmitScalarExpr(E->getBase());
Chris Lattner4b009652007-07-25 00:24:17 +0000550
Ted Kremenek1c1700f2007-08-20 16:18:38 +0000551 // Extend or truncate the index type to 32 or 64-bits.
Chris Lattner2af72ac2007-08-08 17:43:05 +0000552 QualType IdxTy = E->getIdx()->getType();
Chris Lattner4b009652007-07-25 00:24:17 +0000553 bool IdxSigned = IdxTy->isSignedIntegerType();
554 unsigned IdxBitwidth = cast<llvm::IntegerType>(Idx->getType())->getBitWidth();
555 if (IdxBitwidth != LLVMPointerWidth)
556 Idx = Builder.CreateIntCast(Idx, llvm::IntegerType::get(LLVMPointerWidth),
557 IdxSigned, "idxprom");
558
559 // We know that the pointer points to a type of the correct size, unless the
560 // size is a VLA.
Eli Friedman62f67fd2008-02-15 12:20:59 +0000561 if (!E->getType()->isConstantSizeType())
Chris Lattner4b009652007-07-25 00:24:17 +0000562 assert(0 && "VLA idx not implemented");
Chris Lattnerc154ac12008-07-26 22:37:01 +0000563 QualType ExprTy = getContext().getCanonicalType(E->getBase()->getType());
Chris Lattnerd5a56aa2008-07-26 22:17:49 +0000564
Eli Friedman2e630542008-06-13 23:01:12 +0000565 return LValue::MakeAddr(Builder.CreateGEP(Base, Idx, "arrayidx"),
Chris Lattnerd5a56aa2008-07-26 22:17:49 +0000566 ExprTy->getAsPointerType()->getPointeeType()
567 .getCVRQualifiers());
Chris Lattner4b009652007-07-25 00:24:17 +0000568}
569
Nate Begemana1ae7442008-05-13 21:03:02 +0000570static
571llvm::Constant *GenerateConstantVector(llvm::SmallVector<unsigned, 4> &Elts) {
572 llvm::SmallVector<llvm::Constant *, 4> CElts;
573
574 for (unsigned i = 0, e = Elts.size(); i != e; ++i)
575 CElts.push_back(llvm::ConstantInt::get(llvm::Type::Int32Ty, Elts[i]));
576
577 return llvm::ConstantVector::get(&CElts[0], CElts.size());
578}
579
Chris Lattner65520192007-08-02 23:37:31 +0000580LValue CodeGenFunction::
Nate Begemanaf6ed502008-04-18 23:10:10 +0000581EmitExtVectorElementExpr(const ExtVectorElementExpr *E) {
Chris Lattner65520192007-08-02 23:37:31 +0000582 // Emit the base vector as an l-value.
583 LValue Base = EmitLValue(E->getBase());
Chris Lattner65520192007-08-02 23:37:31 +0000584
Nate Begemana1ae7442008-05-13 21:03:02 +0000585 // Encode the element access list into a vector of unsigned indices.
586 llvm::SmallVector<unsigned, 4> Indices;
587 E->getEncodedElementAccess(Indices);
588
589 if (Base.isSimple()) {
590 llvm::Constant *CV = GenerateConstantVector(Indices);
Eli Friedman2e630542008-06-13 23:01:12 +0000591 return LValue::MakeExtVectorElt(Base.getAddress(), CV,
592 E->getBase()->getType().getCVRQualifiers());
Nate Begemana1ae7442008-05-13 21:03:02 +0000593 }
594 assert(Base.isExtVectorElt() && "Can only subscript lvalue vec elts here!");
595
596 llvm::Constant *BaseElts = Base.getExtVectorElts();
597 llvm::SmallVector<llvm::Constant *, 4> CElts;
598
599 for (unsigned i = 0, e = Indices.size(); i != e; ++i) {
600 if (isa<llvm::ConstantAggregateZero>(BaseElts))
601 CElts.push_back(llvm::ConstantInt::get(llvm::Type::Int32Ty, 0));
602 else
603 CElts.push_back(BaseElts->getOperand(Indices[i]));
604 }
605 llvm::Constant *CV = llvm::ConstantVector::get(&CElts[0], CElts.size());
Eli Friedman2e630542008-06-13 23:01:12 +0000606 return LValue::MakeExtVectorElt(Base.getExtVectorAddr(), CV,
607 E->getBase()->getType().getCVRQualifiers());
Chris Lattner65520192007-08-02 23:37:31 +0000608}
609
Devang Patel41b66252007-10-23 20:28:39 +0000610LValue CodeGenFunction::EmitMemberExpr(const MemberExpr *E) {
Devang Patele1f79db2007-12-11 21:33:16 +0000611 bool isUnion = false;
Devang Patel9dd3e2b2007-10-24 22:26:28 +0000612 Expr *BaseExpr = E->getBase();
Devang Patel9dd3e2b2007-10-24 22:26:28 +0000613 llvm::Value *BaseValue = NULL;
Eli Friedman2e630542008-06-13 23:01:12 +0000614 unsigned CVRQualifiers=0;
615
Chris Lattner659079e2007-12-02 18:52:07 +0000616 // 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 +0000617 if (E->isArrow()) {
Devang Patel2b24fd92007-10-26 18:15:21 +0000618 BaseValue = EmitScalarExpr(BaseExpr);
Devang Patele1f79db2007-12-11 21:33:16 +0000619 const PointerType *PTy =
Chris Lattnerc154ac12008-07-26 22:37:01 +0000620 cast<PointerType>(getContext().getCanonicalType(BaseExpr->getType()));
Devang Patele1f79db2007-12-11 21:33:16 +0000621 if (PTy->getPointeeType()->isUnionType())
622 isUnion = true;
Eli Friedman2e630542008-06-13 23:01:12 +0000623 CVRQualifiers = PTy->getPointeeType().getCVRQualifiers();
Devang Patele1f79db2007-12-11 21:33:16 +0000624 }
Chris Lattner659079e2007-12-02 18:52:07 +0000625 else {
626 LValue BaseLV = EmitLValue(BaseExpr);
627 // FIXME: this isn't right for bitfields.
628 BaseValue = BaseLV.getAddress();
Devang Patele1f79db2007-12-11 21:33:16 +0000629 if (BaseExpr->getType()->isUnionType())
630 isUnion = true;
Eli Friedman2e630542008-06-13 23:01:12 +0000631 CVRQualifiers = BaseExpr->getType().getCVRQualifiers();
Chris Lattner659079e2007-12-02 18:52:07 +0000632 }
Devang Patel41b66252007-10-23 20:28:39 +0000633
634 FieldDecl *Field = E->getMemberDecl();
Eli Friedman2e630542008-06-13 23:01:12 +0000635 return EmitLValueForField(BaseValue, Field, isUnion, CVRQualifiers);
Eli Friedmand3550112008-02-09 08:50:58 +0000636}
Devang Patel41b66252007-10-23 20:28:39 +0000637
Eli Friedmand3550112008-02-09 08:50:58 +0000638LValue CodeGenFunction::EmitLValueForField(llvm::Value* BaseValue,
639 FieldDecl* Field,
Eli Friedman2e630542008-06-13 23:01:12 +0000640 bool isUnion,
641 unsigned CVRQualifiers)
Eli Friedmand3550112008-02-09 08:50:58 +0000642{
643 llvm::Value *V;
644 unsigned idx = CGM.getTypes().getLLVMFieldNo(Field);
Lauro Ramos Venancio63fc38f2008-02-07 19:29:53 +0000645
Eli Friedman66813742008-05-29 11:33:25 +0000646 if (Field->isBitField()) {
Eli Friedmana04e70d2008-05-17 20:03:47 +0000647 // FIXME: CodeGenTypes should expose a method to get the appropriate
648 // type for FieldTy (the appropriate type is ABI-dependent).
Eli Friedman070fee42008-06-01 15:16:01 +0000649 const llvm::Type *FieldTy = CGM.getTypes().ConvertTypeForMem(Field->getType());
Chris Lattner07307562008-03-19 05:19:41 +0000650 const llvm::PointerType *BaseTy =
Lauro Ramos Venancio63fc38f2008-02-07 19:29:53 +0000651 cast<llvm::PointerType>(BaseValue->getType());
652 unsigned AS = BaseTy->getAddressSpace();
653 BaseValue = Builder.CreateBitCast(BaseValue,
654 llvm::PointerType::get(FieldTy, AS),
655 "tmp");
656 V = Builder.CreateGEP(BaseValue,
657 llvm::ConstantInt::get(llvm::Type::Int32Ty, idx),
658 "tmp");
Eli Friedman66813742008-05-29 11:33:25 +0000659
660 CodeGenTypes::BitFieldInfo bitFieldInfo =
661 CGM.getTypes().getBitFieldInfo(Field);
662 return LValue::MakeBitfield(V, bitFieldInfo.Begin, bitFieldInfo.Size,
Eli Friedman2e630542008-06-13 23:01:12 +0000663 Field->getType()->isSignedIntegerType(),
664 Field->getType().getCVRQualifiers()|CVRQualifiers);
Lauro Ramos Venancio63fc38f2008-02-07 19:29:53 +0000665 }
Eli Friedman070fee42008-06-01 15:16:01 +0000666
Eli Friedman66813742008-05-29 11:33:25 +0000667 V = Builder.CreateStructGEP(BaseValue, idx, "tmp");
668
Devang Patel9b1ca9e2007-10-26 19:42:18 +0000669 // Match union field type.
Lauro Ramos Venancio63fc38f2008-02-07 19:29:53 +0000670 if (isUnion) {
Eli Friedman2e630542008-06-13 23:01:12 +0000671 const llvm::Type *FieldTy =
672 CGM.getTypes().ConvertTypeForMem(Field->getType());
Devang Patel0f2a8fb2007-10-30 20:59:40 +0000673 const llvm::PointerType * BaseTy =
674 cast<llvm::PointerType>(BaseValue->getType());
Eli Friedmancecdc6b2008-05-21 13:24:44 +0000675 unsigned AS = BaseTy->getAddressSpace();
676 V = Builder.CreateBitCast(V,
677 llvm::PointerType::get(FieldTy, AS),
678 "tmp");
Devang Patel9b1ca9e2007-10-26 19:42:18 +0000679 }
Lauro Ramos Venanciob40307c2008-01-22 20:17:04 +0000680
Eli Friedman2e630542008-06-13 23:01:12 +0000681 return LValue::MakeAddr(V,
682 Field->getType().getCVRQualifiers()|CVRQualifiers);
Devang Patel41b66252007-10-23 20:28:39 +0000683}
684
Eli Friedman2e630542008-06-13 23:01:12 +0000685LValue CodeGenFunction::EmitCompoundLiteralLValue(const CompoundLiteralExpr* E)
686{
Eli Friedmanf3c2cb42008-05-13 23:18:27 +0000687 const llvm::Type *LTy = ConvertType(E->getType());
688 llvm::Value *DeclPtr = CreateTempAlloca(LTy, ".compoundliteral");
689
690 const Expr* InitExpr = E->getInitializer();
Eli Friedman2e630542008-06-13 23:01:12 +0000691 LValue Result = LValue::MakeAddr(DeclPtr, E->getType().getCVRQualifiers());
Eli Friedmanf3c2cb42008-05-13 23:18:27 +0000692
693 if (E->getType()->isComplexType()) {
694 EmitComplexExprIntoAddr(InitExpr, DeclPtr, false);
695 } else if (hasAggregateLLVMType(E->getType())) {
696 EmitAnyExpr(InitExpr, DeclPtr, false);
697 } else {
698 EmitStoreThroughLValue(EmitAnyExpr(InitExpr), Result, E->getType());
699 }
700
701 return Result;
702}
703
Chris Lattner4b009652007-07-25 00:24:17 +0000704//===--------------------------------------------------------------------===//
705// Expression Emission
706//===--------------------------------------------------------------------===//
707
Chris Lattnerb2cb9cb2007-08-20 22:37:10 +0000708
Chris Lattner4b009652007-07-25 00:24:17 +0000709RValue CodeGenFunction::EmitCallExpr(const CallExpr *E) {
Anders Carlsson49865302007-08-20 18:05:56 +0000710 if (const ImplicitCastExpr *IcExpr =
711 dyn_cast<const ImplicitCastExpr>(E->getCallee()))
712 if (const DeclRefExpr *DRExpr =
713 dyn_cast<const DeclRefExpr>(IcExpr->getSubExpr()))
714 if (const FunctionDecl *FDecl =
715 dyn_cast<const FunctionDecl>(DRExpr->getDecl()))
716 if (unsigned builtinID = FDecl->getIdentifier()->getBuiltinID())
717 return EmitBuiltinExpr(builtinID, E);
718
Chris Lattner9fba49a2007-08-24 05:35:26 +0000719 llvm::Value *Callee = EmitScalarExpr(E->getCallee());
Eli Friedman261f4ad2008-01-30 01:32:06 +0000720 return EmitCallExpr(Callee, E->getCallee()->getType(),
Ted Kremenek2719e982008-06-17 02:43:46 +0000721 E->arg_begin(), E->arg_end());
Nate Begeman9f3bfb72008-01-17 17:46:27 +0000722}
723
Ted Kremenek2719e982008-06-17 02:43:46 +0000724RValue CodeGenFunction::EmitCallExpr(Expr *FnExpr,
725 CallExpr::const_arg_iterator ArgBeg,
726 CallExpr::const_arg_iterator ArgEnd) {
727
Nate Begeman9f3bfb72008-01-17 17:46:27 +0000728 llvm::Value *Callee = EmitScalarExpr(FnExpr);
Ted Kremenek2719e982008-06-17 02:43:46 +0000729 return EmitCallExpr(Callee, FnExpr->getType(), ArgBeg, ArgEnd);
Chris Lattner02c60f52007-08-31 04:44:06 +0000730}
731
Christopher Lambad327ba2007-12-29 05:02:41 +0000732LValue CodeGenFunction::EmitCallExprLValue(const CallExpr *E) {
733 // Can only get l-value for call expression returning aggregate type
734 RValue RV = EmitCallExpr(E);
Eli Friedman2e630542008-06-13 23:01:12 +0000735 // FIXME: can this be volatile?
736 return LValue::MakeAddr(RV.getAggregateAddr(),
737 E->getType().getCVRQualifiers());
Christopher Lambad327ba2007-12-29 05:02:41 +0000738}
739
Chris Lattnerb326b172008-03-30 23:03:07 +0000740LValue CodeGenFunction::EmitObjCIvarRefLValue(const ObjCIvarRefExpr *E) {
741 // Objective-C objects are traditionally C structures with their layout
742 // defined at compile-time. In some implementations, their layout is not
743 // defined until run time in order to allow instance variables to be added to
744 // a class without recompiling all of the subclasses. If this is the case
745 // then the CGObjCRuntime subclass must return true to LateBoundIvars and
746 // implement the lookup itself.
Daniel Dunbarfc69bde2008-08-11 18:12:00 +0000747 if (CGM.getObjCRuntime().LateBoundIVars()) {
Chris Lattnerb326b172008-03-30 23:03:07 +0000748 assert(0 && "FIXME: Implement support for late-bound instance variables");
749 return LValue(); // Not reached.
750 }
Chris Lattnerc61e9f82008-03-30 23:25:33 +0000751
752 // Get a structure type for the object
753 QualType ExprTy = E->getBase()->getType();
754 const llvm::Type *ObjectType = ConvertType(ExprTy);
755 // TODO: Add a special case for isa (index 0)
756 // Work out which index the ivar is
757 const ObjCIvarDecl *Decl = E->getDecl();
758 unsigned Index = CGM.getTypes().getLLVMFieldNo(Decl);
Chris Lattnerb326b172008-03-30 23:03:07 +0000759
Chris Lattnerc61e9f82008-03-30 23:25:33 +0000760 // Get object pointer and coerce object pointer to correct type.
761 llvm::Value *Object = EmitLValue(E->getBase()).getAddress();
Eli Friedman2e630542008-06-13 23:01:12 +0000762 // FIXME: Volatility
Chris Lattner6e6a5972008-04-04 04:07:35 +0000763 Object = Builder.CreateLoad(Object, E->getDecl()->getName());
Chris Lattnerc61e9f82008-03-30 23:25:33 +0000764 if (Object->getType() != ObjectType)
765 Object = Builder.CreateBitCast(Object, ObjectType);
Chris Lattner6e6a5972008-04-04 04:07:35 +0000766
Chris Lattnerc61e9f82008-03-30 23:25:33 +0000767
768 // Return a pointer to the right element.
Eli Friedman2e630542008-06-13 23:01:12 +0000769 // FIXME: volatile
Chris Lattnerc61e9f82008-03-30 23:25:33 +0000770 return LValue::MakeAddr(Builder.CreateStructGEP(Object, Index,
Eli Friedman2e630542008-06-13 23:01:12 +0000771 Decl->getName()),0);
Chris Lattnerb326b172008-03-30 23:03:07 +0000772}
773
Nate Begeman9f3bfb72008-01-17 17:46:27 +0000774RValue CodeGenFunction::EmitCallExpr(llvm::Value *Callee, QualType FnType,
Ted Kremenek2719e982008-06-17 02:43:46 +0000775 CallExpr::const_arg_iterator ArgBeg,
776 CallExpr::const_arg_iterator ArgEnd) {
777
Chris Lattner4b009652007-07-25 00:24:17 +0000778 // The callee type will always be a pointer to function type, get the function
779 // type.
Chris Lattnerc154ac12008-07-26 22:37:01 +0000780 FnType = FnType->getAsPointerType()->getPointeeType();
Chris Lattner69cc2f92008-07-31 04:58:58 +0000781 QualType ResultType = FnType->getAsFunctionType()->getResultType();
Eli Friedman261f4ad2008-01-30 01:32:06 +0000782
Chris Lattner4b009652007-07-25 00:24:17 +0000783 llvm::SmallVector<llvm::Value*, 16> Args;
784
Chris Lattner59802042007-08-10 17:02:28 +0000785 // Handle struct-return functions by passing a pointer to the location that
786 // we would like to return into.
Nate Begeman9f3bfb72008-01-17 17:46:27 +0000787 if (hasAggregateLLVMType(ResultType)) {
Chris Lattner59802042007-08-10 17:02:28 +0000788 // Create a temporary alloca to hold the result of the call. :(
Nate Begeman9f3bfb72008-01-17 17:46:27 +0000789 Args.push_back(CreateTempAlloca(ConvertType(ResultType)));
Chris Lattner59802042007-08-10 17:02:28 +0000790 // FIXME: set the stret attribute on the argument.
791 }
792
Ted Kremenek2719e982008-06-17 02:43:46 +0000793 for (CallExpr::const_arg_iterator I = ArgBeg; I != ArgEnd; ++I) {
794 QualType ArgTy = I->getType();
Eli Friedmand3550112008-02-09 08:50:58 +0000795
Chris Lattnerb06c8dd2007-08-26 22:55:13 +0000796 if (!hasAggregateLLVMType(ArgTy)) {
797 // Scalar argument is passed by-value.
Ted Kremenek2719e982008-06-17 02:43:46 +0000798 Args.push_back(EmitScalarExpr(*I));
Chris Lattnerde0908b2008-04-04 16:54:41 +0000799 } else if (ArgTy->isAnyComplexType()) {
Chris Lattnerb06c8dd2007-08-26 22:55:13 +0000800 // Make a temporary alloca to pass the argument.
801 llvm::Value *DestMem = CreateTempAlloca(ConvertType(ArgTy));
Ted Kremenek2719e982008-06-17 02:43:46 +0000802 EmitComplexExprIntoAddr(*I, DestMem, false);
Chris Lattnerb06c8dd2007-08-26 22:55:13 +0000803 Args.push_back(DestMem);
804 } else {
805 llvm::Value *DestMem = CreateTempAlloca(ConvertType(ArgTy));
Ted Kremenek2719e982008-06-17 02:43:46 +0000806 EmitAggExpr(*I, DestMem, false);
Chris Lattnerb06c8dd2007-08-26 22:55:13 +0000807 Args.push_back(DestMem);
Chris Lattner4b009652007-07-25 00:24:17 +0000808 }
Chris Lattner4b009652007-07-25 00:24:17 +0000809 }
810
Nate Begemandc6262e2008-03-09 03:09:36 +0000811 llvm::CallInst *CI = Builder.CreateCall(Callee,&Args[0],&Args[0]+Args.size());
Eli Friedman9be42212008-06-01 15:54:49 +0000812
813 // Note that there is parallel code in SetFunctionAttributes in CodeGenModule
814 llvm::SmallVector<llvm::ParamAttrsWithIndex, 8> ParamAttrList;
815 if (hasAggregateLLVMType(ResultType))
816 ParamAttrList.push_back(
817 llvm::ParamAttrsWithIndex::get(1, llvm::ParamAttr::StructRet));
818 unsigned increment = hasAggregateLLVMType(ResultType) ? 2 : 1;
Ted Kremenek2719e982008-06-17 02:43:46 +0000819
820 unsigned i = 0;
821 for (CallExpr::const_arg_iterator I = ArgBeg; I != ArgEnd; ++I, ++i) {
822 QualType ParamType = I->getType();
Eli Friedman9be42212008-06-01 15:54:49 +0000823 unsigned ParamAttrs = 0;
824 if (ParamType->isRecordType())
825 ParamAttrs |= llvm::ParamAttr::ByVal;
826 if (ParamType->isSignedIntegerType() && ParamType->isPromotableIntegerType())
827 ParamAttrs |= llvm::ParamAttr::SExt;
828 if (ParamType->isUnsignedIntegerType() && ParamType->isPromotableIntegerType())
829 ParamAttrs |= llvm::ParamAttr::ZExt;
830 if (ParamAttrs)
831 ParamAttrList.push_back(llvm::ParamAttrsWithIndex::get(i + increment,
832 ParamAttrs));
833 }
834 CI->setParamAttrs(llvm::PAListPtr::get(ParamAttrList.begin(),
835 ParamAttrList.size()));
836
Nate Begemandc6262e2008-03-09 03:09:36 +0000837 if (const llvm::Function *F = dyn_cast<llvm::Function>(Callee))
838 CI->setCallingConv(F->getCallingConv());
839 if (CI->getType() != llvm::Type::VoidTy)
840 CI->setName("call");
Chris Lattnerde0908b2008-04-04 16:54:41 +0000841 else if (ResultType->isAnyComplexType())
Chris Lattnere24c4cf2007-08-31 22:49:20 +0000842 return RValue::getComplex(LoadComplexFromAddr(Args[0], false));
Nate Begeman9f3bfb72008-01-17 17:46:27 +0000843 else if (hasAggregateLLVMType(ResultType))
Chris Lattner59802042007-08-10 17:02:28 +0000844 // Struct return.
845 return RValue::getAggregate(Args[0]);
Chris Lattner307da022007-11-30 17:56:23 +0000846 else {
847 // void return.
Nate Begeman9f3bfb72008-01-17 17:46:27 +0000848 assert(ResultType->isVoidType() && "Should only have a void expr here");
Nate Begemandc6262e2008-03-09 03:09:36 +0000849 CI = 0;
Chris Lattner307da022007-11-30 17:56:23 +0000850 }
Chris Lattner59802042007-08-10 17:02:28 +0000851
Nate Begemandc6262e2008-03-09 03:09:36 +0000852 return RValue::get(CI);
Chris Lattner4b009652007-07-25 00:24:17 +0000853}