1 //===--- CGExpr.cpp - Emit LLVM Code from Expressions ---------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This contains code to emit Expr nodes as LLVM code.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #include "CodeGenFunction.h"
15 #include "CodeGenModule.h"
16 #include "CGCall.h"
17 #include "CGObjCRuntime.h"
18 #include "clang/AST/ASTContext.h"
19 #include "clang/AST/DeclObjC.h"
20 #include "llvm/Target/TargetData.h"
21 using namespace clang;
22 using namespace CodeGen;
23 
24 //===--------------------------------------------------------------------===//
25 //                        Miscellaneous Helper Methods
26 //===--------------------------------------------------------------------===//
27 
28 /// CreateTempAlloca - This creates a alloca and inserts it into the entry
29 /// block.
30 llvm::AllocaInst *CodeGenFunction::CreateTempAlloca(const llvm::Type *Ty,
31                                                     const char *Name) {
32   if (!Builder.isNamePreserving())
33     Name = "";
34   return new llvm::AllocaInst(Ty, 0, Name, AllocaInsertPt);
35 }
36 
37 /// EvaluateExprAsBool - Perform the usual unary conversions on the specified
38 /// expression and compare the result against zero, returning an Int1Ty value.
39 llvm::Value *CodeGenFunction::EvaluateExprAsBool(const Expr *E) {
40   QualType BoolTy = getContext().BoolTy;
41   if (!E->getType()->isAnyComplexType())
42     return EmitScalarConversion(EmitScalarExpr(E), E->getType(), BoolTy);
43 
44   return EmitComplexToScalarConversion(EmitComplexExpr(E), E->getType(),BoolTy);
45 }
46 
47 /// EmitAnyExpr - Emit code to compute the specified expression which can have
48 /// any type.  The result is returned as an RValue struct.  If this is an
49 /// aggregate expression, the aggloc/agglocvolatile arguments indicate where
50 /// the result should be returned.
51 RValue CodeGenFunction::EmitAnyExpr(const Expr *E, llvm::Value *AggLoc,
52                                     bool isAggLocVolatile, bool IgnoreResult) {
53   if (!hasAggregateLLVMType(E->getType()))
54     return RValue::get(EmitScalarExpr(E, IgnoreResult));
55   else if (E->getType()->isAnyComplexType())
56     return RValue::getComplex(EmitComplexExpr(E, false, false,
57                                               IgnoreResult, IgnoreResult));
58 
59   EmitAggExpr(E, AggLoc, isAggLocVolatile, IgnoreResult);
60   return RValue::getAggregate(AggLoc, isAggLocVolatile);
61 }
62 
63 /// EmitAnyExprToTemp - Similary to EmitAnyExpr(), however, the result
64 /// will always be accessible even if no aggregate location is
65 /// provided.
66 RValue CodeGenFunction::EmitAnyExprToTemp(const Expr *E, llvm::Value *AggLoc,
67                                           bool isAggLocVolatile) {
68   if (!AggLoc && hasAggregateLLVMType(E->getType()) &&
69       !E->getType()->isAnyComplexType())
70     AggLoc = CreateTempAlloca(ConvertType(E->getType()), "agg.tmp");
71   return EmitAnyExpr(E, AggLoc, isAggLocVolatile);
72 }
73 
74 RValue CodeGenFunction::EmitReferenceBindingToExpr(const Expr* E,
75                                                    QualType DestType) {
76   RValue Val;
77   if (E->isLvalue(getContext()) == Expr::LV_Valid) {
78     // Emit the expr as an lvalue.
79     LValue LV = EmitLValue(E);
80     if (LV.isSimple())
81       return RValue::get(LV.getAddress());
82     Val = EmitLoadOfLValue(LV, E->getType());
83   } else {
84     Val = EmitAnyExprToTemp(E);
85   }
86 
87   if (Val.isAggregate()) {
88     Val = RValue::get(Val.getAggregateAddr());
89   } else {
90     // Create a temporary variable that we can bind the reference to.
91     llvm::Value *Temp = CreateTempAlloca(ConvertTypeForMem(E->getType()),
92                                          "reftmp");
93     if (Val.isScalar())
94       EmitStoreOfScalar(Val.getScalarVal(), Temp, false, E->getType());
95     else
96       StoreComplexToAddr(Val.getComplexVal(), Temp, false);
97     Val = RValue::get(Temp);
98   }
99 
100   return Val;
101 }
102 
103 
104 /// getAccessedFieldNo - Given an encoded value and a result number, return
105 /// the input field number being accessed.
106 unsigned CodeGenFunction::getAccessedFieldNo(unsigned Idx,
107                                              const llvm::Constant *Elts) {
108   if (isa<llvm::ConstantAggregateZero>(Elts))
109     return 0;
110 
111   return cast<llvm::ConstantInt>(Elts->getOperand(Idx))->getZExtValue();
112 }
113 
114 
115 //===----------------------------------------------------------------------===//
116 //                         LValue Expression Emission
117 //===----------------------------------------------------------------------===//
118 
119 RValue CodeGenFunction::GetUndefRValue(QualType Ty) {
120   if (Ty->isVoidType()) {
121     return RValue::get(0);
122   } else if (const ComplexType *CTy = Ty->getAsComplexType()) {
123     const llvm::Type *EltTy = ConvertType(CTy->getElementType());
124     llvm::Value *U = VMContext.getUndef(EltTy);
125     return RValue::getComplex(std::make_pair(U, U));
126   } else if (hasAggregateLLVMType(Ty)) {
127     const llvm::Type *LTy = VMContext.getPointerTypeUnqual(ConvertType(Ty));
128     return RValue::getAggregate(VMContext.getUndef(LTy));
129   } else {
130     return RValue::get(VMContext.getUndef(ConvertType(Ty)));
131   }
132 }
133 
134 RValue CodeGenFunction::EmitUnsupportedRValue(const Expr *E,
135                                               const char *Name) {
136   ErrorUnsupported(E, Name);
137   return GetUndefRValue(E->getType());
138 }
139 
140 LValue CodeGenFunction::EmitUnsupportedLValue(const Expr *E,
141                                               const char *Name) {
142   ErrorUnsupported(E, Name);
143   llvm::Type *Ty = VMContext.getPointerTypeUnqual(ConvertType(E->getType()));
144   return LValue::MakeAddr(VMContext.getUndef(Ty),
145                           E->getType().getCVRQualifiers(),
146                           getContext().getObjCGCAttrKind(E->getType()),
147                           E->getType().getAddressSpace());
148 }
149 
150 /// EmitLValue - Emit code to compute a designator that specifies the location
151 /// of the expression.
152 ///
153 /// This can return one of two things: a simple address or a bitfield
154 /// reference.  In either case, the LLVM Value* in the LValue structure is
155 /// guaranteed to be an LLVM pointer type.
156 ///
157 /// If this returns a bitfield reference, nothing about the pointee type of
158 /// the LLVM value is known: For example, it may not be a pointer to an
159 /// integer.
160 ///
161 /// If this returns a normal address, and if the lvalue's C type is fixed
162 /// size, this method guarantees that the returned pointer type will point to
163 /// an LLVM type of the same size of the lvalue's type.  If the lvalue has a
164 /// variable length type, this is not possible.
165 ///
166 LValue CodeGenFunction::EmitLValue(const Expr *E) {
167   switch (E->getStmtClass()) {
168   default: return EmitUnsupportedLValue(E, "l-value expression");
169 
170   case Expr::BinaryOperatorClass:
171     return EmitBinaryOperatorLValue(cast<BinaryOperator>(E));
172   case Expr::CallExprClass:
173   case Expr::CXXOperatorCallExprClass:
174     return EmitCallExprLValue(cast<CallExpr>(E));
175   case Expr::VAArgExprClass:
176     return EmitVAArgExprLValue(cast<VAArgExpr>(E));
177   case Expr::DeclRefExprClass:
178   case Expr::QualifiedDeclRefExprClass:
179     return EmitDeclRefLValue(cast<DeclRefExpr>(E));
180   case Expr::ParenExprClass:return EmitLValue(cast<ParenExpr>(E)->getSubExpr());
181   case Expr::PredefinedExprClass:
182     return EmitPredefinedLValue(cast<PredefinedExpr>(E));
183   case Expr::StringLiteralClass:
184     return EmitStringLiteralLValue(cast<StringLiteral>(E));
185   case Expr::ObjCEncodeExprClass:
186     return EmitObjCEncodeExprLValue(cast<ObjCEncodeExpr>(E));
187 
188   case Expr::BlockDeclRefExprClass:
189     return EmitBlockDeclRefLValue(cast<BlockDeclRefExpr>(E));
190 
191   case Expr::CXXConditionDeclExprClass:
192     return EmitCXXConditionDeclLValue(cast<CXXConditionDeclExpr>(E));
193   case Expr::CXXTemporaryObjectExprClass:
194   case Expr::CXXConstructExprClass:
195     return EmitCXXConstructLValue(cast<CXXConstructExpr>(E));
196   case Expr::CXXBindTemporaryExprClass:
197     return EmitCXXBindTemporaryLValue(cast<CXXBindTemporaryExpr>(E));
198 
199   case Expr::ObjCMessageExprClass:
200     return EmitObjCMessageExprLValue(cast<ObjCMessageExpr>(E));
201   case Expr::ObjCIvarRefExprClass:
202     return EmitObjCIvarRefLValue(cast<ObjCIvarRefExpr>(E));
203   case Expr::ObjCPropertyRefExprClass:
204     return EmitObjCPropertyRefLValue(cast<ObjCPropertyRefExpr>(E));
205   case Expr::ObjCKVCRefExprClass:
206     return EmitObjCKVCRefLValue(cast<ObjCKVCRefExpr>(E));
207   case Expr::ObjCSuperExprClass:
208     return EmitObjCSuperExprLValue(cast<ObjCSuperExpr>(E));
209 
210   case Expr::StmtExprClass:
211     return EmitStmtExprLValue(cast<StmtExpr>(E));
212   case Expr::UnaryOperatorClass:
213     return EmitUnaryOpLValue(cast<UnaryOperator>(E));
214   case Expr::ArraySubscriptExprClass:
215     return EmitArraySubscriptExpr(cast<ArraySubscriptExpr>(E));
216   case Expr::ExtVectorElementExprClass:
217     return EmitExtVectorElementExpr(cast<ExtVectorElementExpr>(E));
218   case Expr::MemberExprClass: return EmitMemberExpr(cast<MemberExpr>(E));
219   case Expr::CompoundLiteralExprClass:
220     return EmitCompoundLiteralLValue(cast<CompoundLiteralExpr>(E));
221   case Expr::ConditionalOperatorClass:
222     return EmitConditionalOperator(cast<ConditionalOperator>(E));
223   case Expr::ChooseExprClass:
224     return EmitLValue(cast<ChooseExpr>(E)->getChosenSubExpr(getContext()));
225   case Expr::ImplicitCastExprClass:
226   case Expr::CStyleCastExprClass:
227   case Expr::CXXFunctionalCastExprClass:
228   case Expr::CXXStaticCastExprClass:
229   case Expr::CXXDynamicCastExprClass:
230   case Expr::CXXReinterpretCastExprClass:
231   case Expr::CXXConstCastExprClass:
232     return EmitCastLValue(cast<CastExpr>(E));
233   }
234 }
235 
236 llvm::Value *CodeGenFunction::EmitLoadOfScalar(llvm::Value *Addr, bool Volatile,
237                                                QualType Ty) {
238   llvm::Value *V = Builder.CreateLoad(Addr, Volatile, "tmp");
239 
240   // Bool can have different representation in memory than in registers.
241   if (Ty->isBooleanType())
242     if (V->getType() != llvm::Type::Int1Ty)
243       V = Builder.CreateTrunc(V, llvm::Type::Int1Ty, "tobool");
244 
245   return V;
246 }
247 
248 void CodeGenFunction::EmitStoreOfScalar(llvm::Value *Value, llvm::Value *Addr,
249                                         bool Volatile, QualType Ty) {
250 
251   if (Ty->isBooleanType()) {
252     // Bool can have different representation in memory than in registers.
253     const llvm::Type *SrcTy = Value->getType();
254     const llvm::PointerType *DstPtr = cast<llvm::PointerType>(Addr->getType());
255     if (DstPtr->getElementType() != SrcTy) {
256       const llvm::Type *MemTy =
257         VMContext.getPointerType(SrcTy, DstPtr->getAddressSpace());
258       Addr = Builder.CreateBitCast(Addr, MemTy, "storetmp");
259     }
260   }
261   Builder.CreateStore(Value, Addr, Volatile);
262 }
263 
264 /// EmitLoadOfLValue - Given an expression that represents a value lvalue,
265 /// this method emits the address of the lvalue, then loads the result as an
266 /// rvalue, returning the rvalue.
267 RValue CodeGenFunction::EmitLoadOfLValue(LValue LV, QualType ExprType) {
268   if (LV.isObjCWeak()) {
269     // load of a __weak object.
270     llvm::Value *AddrWeakObj = LV.getAddress();
271     llvm::Value *read_weak = CGM.getObjCRuntime().EmitObjCWeakRead(*this,
272                                                                    AddrWeakObj);
273     return RValue::get(read_weak);
274   }
275 
276   if (LV.isSimple()) {
277     llvm::Value *Ptr = LV.getAddress();
278     const llvm::Type *EltTy =
279       cast<llvm::PointerType>(Ptr->getType())->getElementType();
280 
281     // Simple scalar l-value.
282     if (EltTy->isSingleValueType())
283       return RValue::get(EmitLoadOfScalar(Ptr, LV.isVolatileQualified(),
284                                           ExprType));
285 
286     assert(ExprType->isFunctionType() && "Unknown scalar value");
287     return RValue::get(Ptr);
288   }
289 
290   if (LV.isVectorElt()) {
291     llvm::Value *Vec = Builder.CreateLoad(LV.getVectorAddr(),
292                                           LV.isVolatileQualified(), "tmp");
293     return RValue::get(Builder.CreateExtractElement(Vec, LV.getVectorIdx(),
294                                                     "vecext"));
295   }
296 
297   // If this is a reference to a subset of the elements of a vector, either
298   // shuffle the input or extract/insert them as appropriate.
299   if (LV.isExtVectorElt())
300     return EmitLoadOfExtVectorElementLValue(LV, ExprType);
301 
302   if (LV.isBitfield())
303     return EmitLoadOfBitfieldLValue(LV, ExprType);
304 
305   if (LV.isPropertyRef())
306     return EmitLoadOfPropertyRefLValue(LV, ExprType);
307 
308   assert(LV.isKVCRef() && "Unknown LValue type!");
309   return EmitLoadOfKVCRefLValue(LV, ExprType);
310 }
311 
312 RValue CodeGenFunction::EmitLoadOfBitfieldLValue(LValue LV,
313                                                  QualType ExprType) {
314   unsigned StartBit = LV.getBitfieldStartBit();
315   unsigned BitfieldSize = LV.getBitfieldSize();
316   llvm::Value *Ptr = LV.getBitfieldAddr();
317 
318   const llvm::Type *EltTy =
319     cast<llvm::PointerType>(Ptr->getType())->getElementType();
320   unsigned EltTySize = CGM.getTargetData().getTypeSizeInBits(EltTy);
321 
322   // In some cases the bitfield may straddle two memory locations.
323   // Currently we load the entire bitfield, then do the magic to
324   // sign-extend it if necessary. This results in somewhat more code
325   // than necessary for the common case (one load), since two shifts
326   // accomplish both the masking and sign extension.
327   unsigned LowBits = std::min(BitfieldSize, EltTySize - StartBit);
328   llvm::Value *Val = Builder.CreateLoad(Ptr, LV.isVolatileQualified(), "tmp");
329 
330   // Shift to proper location.
331   if (StartBit)
332     Val = Builder.CreateLShr(Val, llvm::ConstantInt::get(EltTy, StartBit),
333                              "bf.lo");
334 
335   // Mask off unused bits.
336   llvm::Constant *LowMask = llvm::ConstantInt::get(VMContext,
337                                 llvm::APInt::getLowBitsSet(EltTySize, LowBits));
338   Val = Builder.CreateAnd(Val, LowMask, "bf.lo.cleared");
339 
340   // Fetch the high bits if necessary.
341   if (LowBits < BitfieldSize) {
342     unsigned HighBits = BitfieldSize - LowBits;
343     llvm::Value *HighPtr =
344       Builder.CreateGEP(Ptr, llvm::ConstantInt::get(llvm::Type::Int32Ty, 1),
345                         "bf.ptr.hi");
346     llvm::Value *HighVal = Builder.CreateLoad(HighPtr,
347                                               LV.isVolatileQualified(),
348                                               "tmp");
349 
350     // Mask off unused bits.
351     llvm::Constant *HighMask = llvm::ConstantInt::get(VMContext,
352                                llvm::APInt::getLowBitsSet(EltTySize, HighBits));
353     HighVal = Builder.CreateAnd(HighVal, HighMask, "bf.lo.cleared");
354 
355     // Shift to proper location and or in to bitfield value.
356     HighVal = Builder.CreateShl(HighVal,
357                                 llvm::ConstantInt::get(EltTy, LowBits));
358     Val = Builder.CreateOr(Val, HighVal, "bf.val");
359   }
360 
361   // Sign extend if necessary.
362   if (LV.isBitfieldSigned()) {
363     llvm::Value *ExtraBits = llvm::ConstantInt::get(EltTy,
364                                                     EltTySize - BitfieldSize);
365     Val = Builder.CreateAShr(Builder.CreateShl(Val, ExtraBits),
366                              ExtraBits, "bf.val.sext");
367   }
368 
369   // The bitfield type and the normal type differ when the storage sizes
370   // differ (currently just _Bool).
371   Val = Builder.CreateIntCast(Val, ConvertType(ExprType), false, "tmp");
372 
373   return RValue::get(Val);
374 }
375 
376 RValue CodeGenFunction::EmitLoadOfPropertyRefLValue(LValue LV,
377                                                     QualType ExprType) {
378   return EmitObjCPropertyGet(LV.getPropertyRefExpr());
379 }
380 
381 RValue CodeGenFunction::EmitLoadOfKVCRefLValue(LValue LV,
382                                                QualType ExprType) {
383   return EmitObjCPropertyGet(LV.getKVCRefExpr());
384 }
385 
386 // If this is a reference to a subset of the elements of a vector, create an
387 // appropriate shufflevector.
388 RValue CodeGenFunction::EmitLoadOfExtVectorElementLValue(LValue LV,
389                                                          QualType ExprType) {
390   llvm::Value *Vec = Builder.CreateLoad(LV.getExtVectorAddr(),
391                                         LV.isVolatileQualified(), "tmp");
392 
393   const llvm::Constant *Elts = LV.getExtVectorElts();
394 
395   // If the result of the expression is a non-vector type, we must be
396   // extracting a single element.  Just codegen as an extractelement.
397   const VectorType *ExprVT = ExprType->getAsVectorType();
398   if (!ExprVT) {
399     unsigned InIdx = getAccessedFieldNo(0, Elts);
400     llvm::Value *Elt = llvm::ConstantInt::get(llvm::Type::Int32Ty, InIdx);
401     return RValue::get(Builder.CreateExtractElement(Vec, Elt, "tmp"));
402   }
403 
404   // Always use shuffle vector to try to retain the original program structure
405   unsigned NumResultElts = ExprVT->getNumElements();
406 
407   llvm::SmallVector<llvm::Constant*, 4> Mask;
408   for (unsigned i = 0; i != NumResultElts; ++i) {
409     unsigned InIdx = getAccessedFieldNo(i, Elts);
410     Mask.push_back(llvm::ConstantInt::get(llvm::Type::Int32Ty, InIdx));
411   }
412 
413   llvm::Value *MaskV = VMContext.getConstantVector(&Mask[0], Mask.size());
414   Vec = Builder.CreateShuffleVector(Vec,
415                                     VMContext.getUndef(Vec->getType()),
416                                     MaskV, "tmp");
417   return RValue::get(Vec);
418 }
419 
420 
421 
422 /// EmitStoreThroughLValue - Store the specified rvalue into the specified
423 /// lvalue, where both are guaranteed to the have the same type, and that type
424 /// is 'Ty'.
425 void CodeGenFunction::EmitStoreThroughLValue(RValue Src, LValue Dst,
426                                              QualType Ty) {
427   if (!Dst.isSimple()) {
428     if (Dst.isVectorElt()) {
429       // Read/modify/write the vector, inserting the new element.
430       llvm::Value *Vec = Builder.CreateLoad(Dst.getVectorAddr(),
431                                             Dst.isVolatileQualified(), "tmp");
432       Vec = Builder.CreateInsertElement(Vec, Src.getScalarVal(),
433                                         Dst.getVectorIdx(), "vecins");
434       Builder.CreateStore(Vec, Dst.getVectorAddr(),Dst.isVolatileQualified());
435       return;
436     }
437 
438     // If this is an update of extended vector elements, insert them as
439     // appropriate.
440     if (Dst.isExtVectorElt())
441       return EmitStoreThroughExtVectorComponentLValue(Src, Dst, Ty);
442 
443     if (Dst.isBitfield())
444       return EmitStoreThroughBitfieldLValue(Src, Dst, Ty);
445 
446     if (Dst.isPropertyRef())
447       return EmitStoreThroughPropertyRefLValue(Src, Dst, Ty);
448 
449     if (Dst.isKVCRef())
450       return EmitStoreThroughKVCRefLValue(Src, Dst, Ty);
451 
452     assert(0 && "Unknown LValue type");
453   }
454 
455   if (Dst.isObjCWeak() && !Dst.isNonGC()) {
456     // load of a __weak object.
457     llvm::Value *LvalueDst = Dst.getAddress();
458     llvm::Value *src = Src.getScalarVal();
459      CGM.getObjCRuntime().EmitObjCWeakAssign(*this, src, LvalueDst);
460     return;
461   }
462 
463   if (Dst.isObjCStrong() && !Dst.isNonGC()) {
464     // load of a __strong object.
465     llvm::Value *LvalueDst = Dst.getAddress();
466     llvm::Value *src = Src.getScalarVal();
467 #if 0
468     // FIXME. We cannot positively determine if we have an 'ivar' assignment,
469     // object assignment or an unknown assignment. For now, generate call to
470     // objc_assign_strongCast assignment which is a safe, but consevative
471     // assumption.
472     if (Dst.isObjCIvar())
473       CGM.getObjCRuntime().EmitObjCIvarAssign(*this, src, LvalueDst);
474     else
475       CGM.getObjCRuntime().EmitObjCGlobalAssign(*this, src, LvalueDst);
476 #endif
477     if (Dst.isGlobalObjCRef())
478       CGM.getObjCRuntime().EmitObjCGlobalAssign(*this, src, LvalueDst);
479     else
480       CGM.getObjCRuntime().EmitObjCStrongCastAssign(*this, src, LvalueDst);
481     return;
482   }
483 
484   assert(Src.isScalar() && "Can't emit an agg store with this method");
485   EmitStoreOfScalar(Src.getScalarVal(), Dst.getAddress(),
486                     Dst.isVolatileQualified(), Ty);
487 }
488 
489 void CodeGenFunction::EmitStoreThroughBitfieldLValue(RValue Src, LValue Dst,
490                                                      QualType Ty,
491                                                      llvm::Value **Result) {
492   unsigned StartBit = Dst.getBitfieldStartBit();
493   unsigned BitfieldSize = Dst.getBitfieldSize();
494   llvm::Value *Ptr = Dst.getBitfieldAddr();
495 
496   const llvm::Type *EltTy =
497     cast<llvm::PointerType>(Ptr->getType())->getElementType();
498   unsigned EltTySize = CGM.getTargetData().getTypeSizeInBits(EltTy);
499 
500   // Get the new value, cast to the appropriate type and masked to
501   // exactly the size of the bit-field.
502   llvm::Value *SrcVal = Src.getScalarVal();
503   llvm::Value *NewVal = Builder.CreateIntCast(SrcVal, EltTy, false, "tmp");
504   llvm::Constant *Mask = llvm::ConstantInt::get(VMContext,
505                            llvm::APInt::getLowBitsSet(EltTySize, BitfieldSize));
506   NewVal = Builder.CreateAnd(NewVal, Mask, "bf.value");
507 
508   // Return the new value of the bit-field, if requested.
509   if (Result) {
510     // Cast back to the proper type for result.
511     const llvm::Type *SrcTy = SrcVal->getType();
512     llvm::Value *SrcTrunc = Builder.CreateIntCast(NewVal, SrcTy, false,
513                                                   "bf.reload.val");
514 
515     // Sign extend if necessary.
516     if (Dst.isBitfieldSigned()) {
517       unsigned SrcTySize = CGM.getTargetData().getTypeSizeInBits(SrcTy);
518       llvm::Value *ExtraBits = llvm::ConstantInt::get(SrcTy,
519                                                       SrcTySize - BitfieldSize);
520       SrcTrunc = Builder.CreateAShr(Builder.CreateShl(SrcTrunc, ExtraBits),
521                                     ExtraBits, "bf.reload.sext");
522     }
523 
524     *Result = SrcTrunc;
525   }
526 
527   // In some cases the bitfield may straddle two memory locations.
528   // Emit the low part first and check to see if the high needs to be
529   // done.
530   unsigned LowBits = std::min(BitfieldSize, EltTySize - StartBit);
531   llvm::Value *LowVal = Builder.CreateLoad(Ptr, Dst.isVolatileQualified(),
532                                            "bf.prev.low");
533 
534   // Compute the mask for zero-ing the low part of this bitfield.
535   llvm::Constant *InvMask =
536     llvm::ConstantInt::get(VMContext,
537              ~llvm::APInt::getBitsSet(EltTySize, StartBit, StartBit + LowBits));
538 
539   // Compute the new low part as
540   //   LowVal = (LowVal & InvMask) | (NewVal << StartBit),
541   // with the shift of NewVal implicitly stripping the high bits.
542   llvm::Value *NewLowVal =
543     Builder.CreateShl(NewVal, llvm::ConstantInt::get(EltTy, StartBit),
544                       "bf.value.lo");
545   LowVal = Builder.CreateAnd(LowVal, InvMask, "bf.prev.lo.cleared");
546   LowVal = Builder.CreateOr(LowVal, NewLowVal, "bf.new.lo");
547 
548   // Write back.
549   Builder.CreateStore(LowVal, Ptr, Dst.isVolatileQualified());
550 
551   // If the low part doesn't cover the bitfield emit a high part.
552   if (LowBits < BitfieldSize) {
553     unsigned HighBits = BitfieldSize - LowBits;
554     llvm::Value *HighPtr =
555       Builder.CreateGEP(Ptr, llvm::ConstantInt::get(llvm::Type::Int32Ty, 1),
556                         "bf.ptr.hi");
557     llvm::Value *HighVal = Builder.CreateLoad(HighPtr,
558                                               Dst.isVolatileQualified(),
559                                               "bf.prev.hi");
560 
561     // Compute the mask for zero-ing the high part of this bitfield.
562     llvm::Constant *InvMask =
563       llvm::ConstantInt::get(VMContext, ~llvm::APInt::getLowBitsSet(EltTySize,
564                                HighBits));
565 
566     // Compute the new high part as
567     //   HighVal = (HighVal & InvMask) | (NewVal lshr LowBits),
568     // where the high bits of NewVal have already been cleared and the
569     // shift stripping the low bits.
570     llvm::Value *NewHighVal =
571       Builder.CreateLShr(NewVal, llvm::ConstantInt::get(EltTy, LowBits),
572                         "bf.value.high");
573     HighVal = Builder.CreateAnd(HighVal, InvMask, "bf.prev.hi.cleared");
574     HighVal = Builder.CreateOr(HighVal, NewHighVal, "bf.new.hi");
575 
576     // Write back.
577     Builder.CreateStore(HighVal, HighPtr, Dst.isVolatileQualified());
578   }
579 }
580 
581 void CodeGenFunction::EmitStoreThroughPropertyRefLValue(RValue Src,
582                                                         LValue Dst,
583                                                         QualType Ty) {
584   EmitObjCPropertySet(Dst.getPropertyRefExpr(), Src);
585 }
586 
587 void CodeGenFunction::EmitStoreThroughKVCRefLValue(RValue Src,
588                                                    LValue Dst,
589                                                    QualType Ty) {
590   EmitObjCPropertySet(Dst.getKVCRefExpr(), Src);
591 }
592 
593 void CodeGenFunction::EmitStoreThroughExtVectorComponentLValue(RValue Src,
594                                                                LValue Dst,
595                                                                QualType Ty) {
596   // This access turns into a read/modify/write of the vector.  Load the input
597   // value now.
598   llvm::Value *Vec = Builder.CreateLoad(Dst.getExtVectorAddr(),
599                                         Dst.isVolatileQualified(), "tmp");
600   const llvm::Constant *Elts = Dst.getExtVectorElts();
601 
602   llvm::Value *SrcVal = Src.getScalarVal();
603 
604   if (const VectorType *VTy = Ty->getAsVectorType()) {
605     unsigned NumSrcElts = VTy->getNumElements();
606     unsigned NumDstElts =
607        cast<llvm::VectorType>(Vec->getType())->getNumElements();
608     if (NumDstElts == NumSrcElts) {
609       // Use shuffle vector is the src and destination are the same number
610       // of elements and restore the vector mask since it is on the side
611       // it will be stored.
612       llvm::SmallVector<llvm::Constant*, 4> Mask(NumDstElts);
613       for (unsigned i = 0; i != NumSrcElts; ++i) {
614         unsigned InIdx = getAccessedFieldNo(i, Elts);
615         Mask[InIdx] = llvm::ConstantInt::get(llvm::Type::Int32Ty, i);
616       }
617 
618       llvm::Value *MaskV = VMContext.getConstantVector(&Mask[0], Mask.size());
619       Vec = Builder.CreateShuffleVector(SrcVal,
620                                         VMContext.getUndef(Vec->getType()),
621                                         MaskV, "tmp");
622     }
623     else if (NumDstElts > NumSrcElts) {
624       // Extended the source vector to the same length and then shuffle it
625       // into the destination.
626       // FIXME: since we're shuffling with undef, can we just use the indices
627       //        into that?  This could be simpler.
628       llvm::SmallVector<llvm::Constant*, 4> ExtMask;
629       unsigned i;
630       for (i = 0; i != NumSrcElts; ++i)
631         ExtMask.push_back(llvm::ConstantInt::get(llvm::Type::Int32Ty, i));
632       for (; i != NumDstElts; ++i)
633         ExtMask.push_back(VMContext.getUndef(llvm::Type::Int32Ty));
634       llvm::Value *ExtMaskV = VMContext.getConstantVector(&ExtMask[0],
635                                                         ExtMask.size());
636       llvm::Value *ExtSrcVal =
637         Builder.CreateShuffleVector(SrcVal,
638                                     VMContext.getUndef(SrcVal->getType()),
639                                     ExtMaskV, "tmp");
640       // build identity
641       llvm::SmallVector<llvm::Constant*, 4> Mask;
642       for (unsigned i = 0; i != NumDstElts; ++i) {
643         Mask.push_back(llvm::ConstantInt::get(llvm::Type::Int32Ty, i));
644       }
645       // modify when what gets shuffled in
646       for (unsigned i = 0; i != NumSrcElts; ++i) {
647         unsigned Idx = getAccessedFieldNo(i, Elts);
648         Mask[Idx] = llvm::ConstantInt::get(llvm::Type::Int32Ty, i+NumDstElts);
649       }
650       llvm::Value *MaskV = VMContext.getConstantVector(&Mask[0], Mask.size());
651       Vec = Builder.CreateShuffleVector(Vec, ExtSrcVal, MaskV, "tmp");
652     }
653     else {
654       // We should never shorten the vector
655       assert(0 && "unexpected shorten vector length");
656     }
657   } else {
658     // If the Src is a scalar (not a vector) it must be updating one element.
659     unsigned InIdx = getAccessedFieldNo(0, Elts);
660     llvm::Value *Elt = llvm::ConstantInt::get(llvm::Type::Int32Ty, InIdx);
661     Vec = Builder.CreateInsertElement(Vec, SrcVal, Elt, "tmp");
662   }
663 
664   Builder.CreateStore(Vec, Dst.getExtVectorAddr(), Dst.isVolatileQualified());
665 }
666 
667 LValue CodeGenFunction::EmitDeclRefLValue(const DeclRefExpr *E) {
668   const VarDecl *VD = dyn_cast<VarDecl>(E->getDecl());
669 
670   if (VD && (VD->isBlockVarDecl() || isa<ParmVarDecl>(VD) ||
671         isa<ImplicitParamDecl>(VD))) {
672     LValue LV;
673     bool NonGCable = VD->hasLocalStorage() &&
674       !VD->hasAttr<BlocksAttr>();
675     if (VD->hasExternalStorage()) {
676       llvm::Value *V = CGM.GetAddrOfGlobalVar(VD);
677       if (VD->getType()->isReferenceType())
678         V = Builder.CreateLoad(V, "tmp");
679       LV = LValue::MakeAddr(V, E->getType().getCVRQualifiers(),
680                             getContext().getObjCGCAttrKind(E->getType()),
681                             E->getType().getAddressSpace());
682     }
683     else {
684       llvm::Value *V = LocalDeclMap[VD];
685       assert(V && "DeclRefExpr not entered in LocalDeclMap?");
686       // local variables do not get their gc attribute set.
687       QualType::GCAttrTypes attr = QualType::GCNone;
688       // local static?
689       if (!NonGCable)
690         attr = getContext().getObjCGCAttrKind(E->getType());
691       if (VD->hasAttr<BlocksAttr>()) {
692         bool needsCopyDispose = BlockRequiresCopying(VD->getType());
693         const llvm::Type *PtrStructTy = V->getType();
694         const llvm::Type *Ty = PtrStructTy;
695         Ty = VMContext.getPointerType(Ty, 0);
696         V = Builder.CreateStructGEP(V, 1, "forwarding");
697         V = Builder.CreateBitCast(V, Ty);
698         V = Builder.CreateLoad(V, false);
699         V = Builder.CreateBitCast(V, PtrStructTy);
700         V = Builder.CreateStructGEP(V, needsCopyDispose*2 + 4, "x");
701       }
702       if (VD->getType()->isReferenceType())
703         V = Builder.CreateLoad(V, "tmp");
704       LV = LValue::MakeAddr(V, E->getType().getCVRQualifiers(), attr,
705                             E->getType().getAddressSpace());
706     }
707     LValue::SetObjCNonGC(LV, NonGCable);
708     return LV;
709   } else if (VD && VD->isFileVarDecl()) {
710     llvm::Value *V = CGM.GetAddrOfGlobalVar(VD);
711     if (VD->getType()->isReferenceType())
712       V = Builder.CreateLoad(V, "tmp");
713     LValue LV = LValue::MakeAddr(V, E->getType().getCVRQualifiers(),
714                                  getContext().getObjCGCAttrKind(E->getType()),
715                                  E->getType().getAddressSpace());
716     if (LV.isObjCStrong())
717       LV.SetGlobalObjCRef(LV, true);
718     return LV;
719   } else if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(E->getDecl())) {
720     llvm::Value* V = CGM.GetAddrOfFunction(GlobalDecl(FD));
721     if (!FD->hasPrototype()) {
722       if (const FunctionProtoType *Proto =
723               FD->getType()->getAsFunctionProtoType()) {
724         // Ugly case: for a K&R-style definition, the type of the definition
725         // isn't the same as the type of a use.  Correct for this with a
726         // bitcast.
727         QualType NoProtoType =
728             getContext().getFunctionNoProtoType(Proto->getResultType());
729         NoProtoType = getContext().getPointerType(NoProtoType);
730         V = Builder.CreateBitCast(V, ConvertType(NoProtoType), "tmp");
731       }
732     }
733     return LValue::MakeAddr(V, E->getType().getCVRQualifiers(),
734                             getContext().getObjCGCAttrKind(E->getType()),
735                             E->getType().getAddressSpace());
736   }
737   else if (const ImplicitParamDecl *IPD =
738       dyn_cast<ImplicitParamDecl>(E->getDecl())) {
739     llvm::Value *V = LocalDeclMap[IPD];
740     assert(V && "BlockVarDecl not entered in LocalDeclMap?");
741     return LValue::MakeAddr(V, E->getType().getCVRQualifiers(),
742                             getContext().getObjCGCAttrKind(E->getType()),
743                             E->getType().getAddressSpace());
744   }
745   assert(0 && "Unimp declref");
746   //an invalid LValue, but the assert will
747   //ensure that this point is never reached.
748   return LValue();
749 }
750 
751 LValue CodeGenFunction::EmitBlockDeclRefLValue(const BlockDeclRefExpr *E) {
752   return LValue::MakeAddr(GetAddrOfBlockDecl(E),
753                           E->getType().getCVRQualifiers(),
754                           getContext().getObjCGCAttrKind(E->getType()),
755                           E->getType().getAddressSpace());
756 }
757 
758 LValue CodeGenFunction::EmitUnaryOpLValue(const UnaryOperator *E) {
759   // __extension__ doesn't affect lvalue-ness.
760   if (E->getOpcode() == UnaryOperator::Extension)
761     return EmitLValue(E->getSubExpr());
762 
763   QualType ExprTy = getContext().getCanonicalType(E->getSubExpr()->getType());
764   switch (E->getOpcode()) {
765   default: assert(0 && "Unknown unary operator lvalue!");
766   case UnaryOperator::Deref:
767     {
768       QualType T = E->getSubExpr()->getType()->getPointeeType();
769       assert(!T.isNull() && "CodeGenFunction::EmitUnaryOpLValue: Illegal type");
770 
771       LValue LV = LValue::MakeAddr(EmitScalarExpr(E->getSubExpr()),
772                                    T.getCVRQualifiers(),
773                                    getContext().getObjCGCAttrKind(T),
774                                    ExprTy.getAddressSpace());
775      // We should not generate __weak write barrier on indirect reference
776      // of a pointer to object; as in void foo (__weak id *param); *param = 0;
777      // But, we continue to generate __strong write barrier on indirect write
778      // into a pointer to object.
779      if (getContext().getLangOptions().ObjC1 &&
780          getContext().getLangOptions().getGCMode() != LangOptions::NonGC &&
781          LV.isObjCWeak())
782        LValue::SetObjCNonGC(LV, !E->isOBJCGCCandidate(getContext()));
783      return LV;
784     }
785   case UnaryOperator::Real:
786   case UnaryOperator::Imag:
787     LValue LV = EmitLValue(E->getSubExpr());
788     unsigned Idx = E->getOpcode() == UnaryOperator::Imag;
789     return LValue::MakeAddr(Builder.CreateStructGEP(LV.getAddress(),
790                                                     Idx, "idx"),
791                             ExprTy.getCVRQualifiers(),
792                             QualType::GCNone,
793                             ExprTy.getAddressSpace());
794   }
795 }
796 
797 LValue CodeGenFunction::EmitStringLiteralLValue(const StringLiteral *E) {
798   return LValue::MakeAddr(CGM.GetAddrOfConstantStringFromLiteral(E), 0);
799 }
800 
801 LValue CodeGenFunction::EmitObjCEncodeExprLValue(const ObjCEncodeExpr *E) {
802   return LValue::MakeAddr(CGM.GetAddrOfConstantStringFromObjCEncode(E), 0);
803 }
804 
805 
806 LValue CodeGenFunction::EmitPredefinedFunctionName(unsigned Type) {
807   std::string GlobalVarName;
808 
809   switch (Type) {
810   default:
811     assert(0 && "Invalid type");
812   case PredefinedExpr::Func:
813     GlobalVarName = "__func__.";
814     break;
815   case PredefinedExpr::Function:
816     GlobalVarName = "__FUNCTION__.";
817     break;
818   case PredefinedExpr::PrettyFunction:
819     // FIXME:: Demangle C++ method names
820     GlobalVarName = "__PRETTY_FUNCTION__.";
821     break;
822   }
823 
824   // FIXME: This isn't right at all.  The logic for computing this should go
825   // into a method on PredefinedExpr.  This would allow sema and codegen to be
826   // consistent for things like sizeof(__func__) etc.
827   std::string FunctionName;
828   if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(CurCodeDecl)) {
829     FunctionName = CGM.getMangledName(FD);
830   } else {
831     // Just get the mangled name; skipping the asm prefix if it
832     // exists.
833     FunctionName = CurFn->getName();
834     if (FunctionName[0] == '\01')
835       FunctionName = FunctionName.substr(1, std::string::npos);
836   }
837 
838   GlobalVarName += FunctionName;
839   llvm::Constant *C =
840     CGM.GetAddrOfConstantCString(FunctionName, GlobalVarName.c_str());
841   return LValue::MakeAddr(C, 0);
842 }
843 
844 LValue CodeGenFunction::EmitPredefinedLValue(const PredefinedExpr *E) {
845   switch (E->getIdentType()) {
846   default:
847     return EmitUnsupportedLValue(E, "predefined expression");
848   case PredefinedExpr::Func:
849   case PredefinedExpr::Function:
850   case PredefinedExpr::PrettyFunction:
851     return EmitPredefinedFunctionName(E->getIdentType());
852   }
853 }
854 
855 LValue CodeGenFunction::EmitArraySubscriptExpr(const ArraySubscriptExpr *E) {
856   // The index must always be an integer, which is not an aggregate.  Emit it.
857   llvm::Value *Idx = EmitScalarExpr(E->getIdx());
858   QualType IdxTy  = E->getIdx()->getType();
859   bool IdxSigned = IdxTy->isSignedIntegerType();
860 
861   // If the base is a vector type, then we are forming a vector element lvalue
862   // with this subscript.
863   if (E->getBase()->getType()->isVectorType()) {
864     // Emit the vector as an lvalue to get its address.
865     LValue LHS = EmitLValue(E->getBase());
866     assert(LHS.isSimple() && "Can only subscript lvalue vectors here!");
867     Idx = Builder.CreateIntCast(Idx, llvm::Type::Int32Ty, IdxSigned, "vidx");
868     return LValue::MakeVectorElt(LHS.getAddress(), Idx,
869       E->getBase()->getType().getCVRQualifiers());
870   }
871 
872   // The base must be a pointer, which is not an aggregate.  Emit it.
873   llvm::Value *Base = EmitScalarExpr(E->getBase());
874 
875   // Extend or truncate the index type to 32 or 64-bits.
876   unsigned IdxBitwidth = cast<llvm::IntegerType>(Idx->getType())->getBitWidth();
877   if (IdxBitwidth != LLVMPointerWidth)
878     Idx = Builder.CreateIntCast(Idx, VMContext.getIntegerType(LLVMPointerWidth),
879                                 IdxSigned, "idxprom");
880 
881   // We know that the pointer points to a type of the correct size,
882   // unless the size is a VLA or Objective-C interface.
883   llvm::Value *Address = 0;
884   if (const VariableArrayType *VAT =
885         getContext().getAsVariableArrayType(E->getType())) {
886     llvm::Value *VLASize = VLASizeMap[VAT];
887 
888     Idx = Builder.CreateMul(Idx, VLASize);
889 
890     QualType BaseType = getContext().getBaseElementType(VAT);
891 
892     uint64_t BaseTypeSize = getContext().getTypeSize(BaseType) / 8;
893     Idx = Builder.CreateUDiv(Idx,
894                              llvm::ConstantInt::get(Idx->getType(),
895                                                     BaseTypeSize));
896     Address = Builder.CreateGEP(Base, Idx, "arrayidx");
897   } else if (const ObjCInterfaceType *OIT =
898              dyn_cast<ObjCInterfaceType>(E->getType())) {
899     llvm::Value *InterfaceSize =
900       llvm::ConstantInt::get(Idx->getType(),
901                              getContext().getTypeSize(OIT) / 8);
902 
903     Idx = Builder.CreateMul(Idx, InterfaceSize);
904 
905     llvm::Type *i8PTy = VMContext.getPointerTypeUnqual(llvm::Type::Int8Ty);
906     Address = Builder.CreateGEP(Builder.CreateBitCast(Base, i8PTy),
907                                 Idx, "arrayidx");
908     Address = Builder.CreateBitCast(Address, Base->getType());
909   } else {
910     Address = Builder.CreateGEP(Base, Idx, "arrayidx");
911   }
912 
913   QualType T = E->getBase()->getType()->getPointeeType();
914   assert(!T.isNull() &&
915          "CodeGenFunction::EmitArraySubscriptExpr(): Illegal base type");
916 
917   LValue LV = LValue::MakeAddr(Address,
918                                T.getCVRQualifiers(),
919                                getContext().getObjCGCAttrKind(T),
920                                E->getBase()->getType().getAddressSpace());
921   if (getContext().getLangOptions().ObjC1 &&
922       getContext().getLangOptions().getGCMode() != LangOptions::NonGC)
923     LValue::SetObjCNonGC(LV, !E->isOBJCGCCandidate(getContext()));
924   return LV;
925 }
926 
927 static
928 llvm::Constant *GenerateConstantVector(llvm::LLVMContext &VMContext,
929                                        llvm::SmallVector<unsigned, 4> &Elts) {
930   llvm::SmallVector<llvm::Constant *, 4> CElts;
931 
932   for (unsigned i = 0, e = Elts.size(); i != e; ++i)
933     CElts.push_back(llvm::ConstantInt::get(llvm::Type::Int32Ty, Elts[i]));
934 
935   return VMContext.getConstantVector(&CElts[0], CElts.size());
936 }
937 
938 LValue CodeGenFunction::
939 EmitExtVectorElementExpr(const ExtVectorElementExpr *E) {
940   // Emit the base vector as an l-value.
941   LValue Base;
942 
943   // ExtVectorElementExpr's base can either be a vector or pointer to vector.
944   if (!E->isArrow()) {
945     assert(E->getBase()->getType()->isVectorType());
946     Base = EmitLValue(E->getBase());
947   } else {
948     const PointerType *PT = E->getBase()->getType()->getAsPointerType();
949     llvm::Value *Ptr = EmitScalarExpr(E->getBase());
950     Base = LValue::MakeAddr(Ptr, PT->getPointeeType().getCVRQualifiers(),
951                             QualType::GCNone,
952                             PT->getPointeeType().getAddressSpace());
953   }
954 
955   // Encode the element access list into a vector of unsigned indices.
956   llvm::SmallVector<unsigned, 4> Indices;
957   E->getEncodedElementAccess(Indices);
958 
959   if (Base.isSimple()) {
960     llvm::Constant *CV = GenerateConstantVector(VMContext, Indices);
961     return LValue::MakeExtVectorElt(Base.getAddress(), CV,
962                                     Base.getQualifiers());
963   }
964   assert(Base.isExtVectorElt() && "Can only subscript lvalue vec elts here!");
965 
966   llvm::Constant *BaseElts = Base.getExtVectorElts();
967   llvm::SmallVector<llvm::Constant *, 4> CElts;
968 
969   for (unsigned i = 0, e = Indices.size(); i != e; ++i) {
970     if (isa<llvm::ConstantAggregateZero>(BaseElts))
971       CElts.push_back(llvm::ConstantInt::get(llvm::Type::Int32Ty, 0));
972     else
973       CElts.push_back(BaseElts->getOperand(Indices[i]));
974   }
975   llvm::Constant *CV = VMContext.getConstantVector(&CElts[0], CElts.size());
976   return LValue::MakeExtVectorElt(Base.getExtVectorAddr(), CV,
977                                   Base.getQualifiers());
978 }
979 
980 LValue CodeGenFunction::EmitMemberExpr(const MemberExpr *E) {
981   bool isUnion = false;
982   bool isIvar = false;
983   bool isNonGC = false;
984   Expr *BaseExpr = E->getBase();
985   llvm::Value *BaseValue = NULL;
986   unsigned CVRQualifiers=0;
987 
988   // If this is s.x, emit s as an lvalue.  If it is s->x, emit s as a scalar.
989   if (E->isArrow()) {
990     BaseValue = EmitScalarExpr(BaseExpr);
991     const PointerType *PTy =
992       BaseExpr->getType()->getAsPointerType();
993     if (PTy->getPointeeType()->isUnionType())
994       isUnion = true;
995     CVRQualifiers = PTy->getPointeeType().getCVRQualifiers();
996   } else if (isa<ObjCPropertyRefExpr>(BaseExpr) ||
997              isa<ObjCKVCRefExpr>(BaseExpr)) {
998     RValue RV = EmitObjCPropertyGet(BaseExpr);
999     BaseValue = RV.getAggregateAddr();
1000     if (BaseExpr->getType()->isUnionType())
1001       isUnion = true;
1002     CVRQualifiers = BaseExpr->getType().getCVRQualifiers();
1003   } else {
1004     LValue BaseLV = EmitLValue(BaseExpr);
1005     if (BaseLV.isObjCIvar())
1006       isIvar = true;
1007     if (BaseLV.isNonGC())
1008       isNonGC = true;
1009     // FIXME: this isn't right for bitfields.
1010     BaseValue = BaseLV.getAddress();
1011     if (BaseExpr->getType()->isUnionType())
1012       isUnion = true;
1013     CVRQualifiers = BaseExpr->getType().getCVRQualifiers();
1014   }
1015 
1016   FieldDecl *Field = dyn_cast<FieldDecl>(E->getMemberDecl());
1017   // FIXME: Handle non-field member expressions
1018   assert(Field && "No code generation for non-field member references");
1019   LValue MemExpLV = EmitLValueForField(BaseValue, Field, isUnion,
1020                                        CVRQualifiers);
1021   LValue::SetObjCIvar(MemExpLV, isIvar);
1022   LValue::SetObjCNonGC(MemExpLV, isNonGC);
1023   return MemExpLV;
1024 }
1025 
1026 LValue CodeGenFunction::EmitLValueForBitfield(llvm::Value* BaseValue,
1027                                               FieldDecl* Field,
1028                                               unsigned CVRQualifiers) {
1029   CodeGenTypes::BitFieldInfo Info = CGM.getTypes().getBitFieldInfo(Field);
1030 
1031   // FIXME: CodeGenTypes should expose a method to get the appropriate type for
1032   // FieldTy (the appropriate type is ABI-dependent).
1033   const llvm::Type *FieldTy =
1034     CGM.getTypes().ConvertTypeForMem(Field->getType());
1035   const llvm::PointerType *BaseTy =
1036   cast<llvm::PointerType>(BaseValue->getType());
1037   unsigned AS = BaseTy->getAddressSpace();
1038   BaseValue = Builder.CreateBitCast(BaseValue,
1039                                     VMContext.getPointerType(FieldTy, AS),
1040                                     "tmp");
1041 
1042   llvm::Value *Idx =
1043     llvm::ConstantInt::get(llvm::Type::Int32Ty, Info.FieldNo);
1044   llvm::Value *V = Builder.CreateGEP(BaseValue, Idx, "tmp");
1045 
1046   return LValue::MakeBitfield(V, Info.Start, Info.Size,
1047                               Field->getType()->isSignedIntegerType(),
1048                             Field->getType().getCVRQualifiers()|CVRQualifiers);
1049 }
1050 
1051 LValue CodeGenFunction::EmitLValueForField(llvm::Value* BaseValue,
1052                                            FieldDecl* Field,
1053                                            bool isUnion,
1054                                            unsigned CVRQualifiers)
1055 {
1056   if (Field->isBitField())
1057     return EmitLValueForBitfield(BaseValue, Field, CVRQualifiers);
1058 
1059   unsigned idx = CGM.getTypes().getLLVMFieldNo(Field);
1060   llvm::Value *V = Builder.CreateStructGEP(BaseValue, idx, "tmp");
1061 
1062   // Match union field type.
1063   if (isUnion) {
1064     const llvm::Type *FieldTy =
1065       CGM.getTypes().ConvertTypeForMem(Field->getType());
1066     const llvm::PointerType * BaseTy =
1067       cast<llvm::PointerType>(BaseValue->getType());
1068     unsigned AS = BaseTy->getAddressSpace();
1069     V = Builder.CreateBitCast(V,
1070                               VMContext.getPointerType(FieldTy, AS),
1071                               "tmp");
1072   }
1073   if (Field->getType()->isReferenceType())
1074     V = Builder.CreateLoad(V, "tmp");
1075 
1076   QualType::GCAttrTypes attr = QualType::GCNone;
1077   if (CGM.getLangOptions().ObjC1 &&
1078       CGM.getLangOptions().getGCMode() != LangOptions::NonGC) {
1079     QualType Ty = Field->getType();
1080     attr = Ty.getObjCGCAttr();
1081     if (attr != QualType::GCNone) {
1082       // __weak attribute on a field is ignored.
1083       if (attr == QualType::Weak)
1084         attr = QualType::GCNone;
1085     }
1086     else if (Ty->isObjCObjectPointerType())
1087       attr = QualType::Strong;
1088   }
1089   LValue LV =
1090     LValue::MakeAddr(V,
1091                      Field->getType().getCVRQualifiers()|CVRQualifiers,
1092                      attr,
1093                      Field->getType().getAddressSpace());
1094   return LV;
1095 }
1096 
1097 LValue CodeGenFunction::EmitCompoundLiteralLValue(const CompoundLiteralExpr* E){
1098   const llvm::Type *LTy = ConvertType(E->getType());
1099   llvm::Value *DeclPtr = CreateTempAlloca(LTy, ".compoundliteral");
1100 
1101   const Expr* InitExpr = E->getInitializer();
1102   LValue Result = LValue::MakeAddr(DeclPtr, E->getType().getCVRQualifiers(),
1103                                    QualType::GCNone,
1104                                    E->getType().getAddressSpace());
1105 
1106   if (E->getType()->isComplexType()) {
1107     EmitComplexExprIntoAddr(InitExpr, DeclPtr, false);
1108   } else if (hasAggregateLLVMType(E->getType())) {
1109     EmitAnyExpr(InitExpr, DeclPtr, false);
1110   } else {
1111     EmitStoreThroughLValue(EmitAnyExpr(InitExpr), Result, E->getType());
1112   }
1113 
1114   return Result;
1115 }
1116 
1117 LValue CodeGenFunction::EmitConditionalOperator(const ConditionalOperator* E) {
1118   // We don't handle vectors yet.
1119   if (E->getType()->isVectorType())
1120     return EmitUnsupportedLValue(E, "conditional operator");
1121 
1122   // ?: here should be an aggregate.
1123   assert((hasAggregateLLVMType(E->getType()) &&
1124           !E->getType()->isAnyComplexType()) &&
1125          "Unexpected conditional operator!");
1126 
1127   llvm::Value *Temp = CreateTempAlloca(ConvertType(E->getType()));
1128   EmitAggExpr(E, Temp, false);
1129 
1130   return LValue::MakeAddr(Temp, E->getType().getCVRQualifiers(),
1131                           getContext().getObjCGCAttrKind(E->getType()),
1132                           E->getType().getAddressSpace());
1133 
1134 }
1135 
1136 /// EmitCastLValue - Casts are never lvalues.  If a cast is needed by the code
1137 /// generator in an lvalue context, then it must mean that we need the address
1138 /// of an aggregate in order to access one of its fields.  This can happen for
1139 /// all the reasons that casts are permitted with aggregate result, including
1140 /// noop aggregate casts, and cast from scalar to union.
1141 LValue CodeGenFunction::EmitCastLValue(const CastExpr *E) {
1142   // If this is an aggregate-to-aggregate cast, just use the input's address as
1143   // the lvalue.
1144   if (getContext().hasSameUnqualifiedType(E->getType(),
1145                                           E->getSubExpr()->getType()))
1146     return EmitLValue(E->getSubExpr());
1147 
1148   // Otherwise, we must have a cast from scalar to union.
1149   assert(E->getType()->isUnionType() && "Expected scalar-to-union cast");
1150 
1151   // Casts are only lvalues when the source and destination types are the same.
1152   llvm::Value *Temp = CreateTempAlloca(ConvertType(E->getType()));
1153   EmitAnyExpr(E->getSubExpr(), Temp, false);
1154 
1155   return LValue::MakeAddr(Temp, E->getType().getCVRQualifiers(),
1156                           getContext().getObjCGCAttrKind(E->getType()),
1157                           E->getType().getAddressSpace());
1158 }
1159 
1160 //===--------------------------------------------------------------------===//
1161 //                             Expression Emission
1162 //===--------------------------------------------------------------------===//
1163 
1164 
1165 RValue CodeGenFunction::EmitCallExpr(const CallExpr *E) {
1166   // Builtins never have block type.
1167   if (E->getCallee()->getType()->isBlockPointerType())
1168     return EmitBlockCallExpr(E);
1169 
1170   if (const CXXMemberCallExpr *CE = dyn_cast<CXXMemberCallExpr>(E))
1171     return EmitCXXMemberCallExpr(CE);
1172 
1173   const Decl *TargetDecl = 0;
1174   if (const ImplicitCastExpr *CE = dyn_cast<ImplicitCastExpr>(E->getCallee())) {
1175     if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(CE->getSubExpr())) {
1176       TargetDecl = DRE->getDecl();
1177       if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(TargetDecl))
1178         if (unsigned builtinID = FD->getBuiltinID(getContext()))
1179           return EmitBuiltinExpr(FD, builtinID, E);
1180     }
1181   }
1182 
1183   if (const CXXOperatorCallExpr *CE = dyn_cast<CXXOperatorCallExpr>(E))
1184     if (const CXXMethodDecl *MD = dyn_cast_or_null<CXXMethodDecl>(TargetDecl))
1185       return EmitCXXOperatorMemberCallExpr(CE, MD);
1186 
1187   llvm::Value *Callee = EmitScalarExpr(E->getCallee());
1188   return EmitCall(Callee, E->getCallee()->getType(),
1189                   E->arg_begin(), E->arg_end(), TargetDecl);
1190 }
1191 
1192 LValue CodeGenFunction::EmitBinaryOperatorLValue(const BinaryOperator *E) {
1193   // Comma expressions just emit their LHS then their RHS as an l-value.
1194   if (E->getOpcode() == BinaryOperator::Comma) {
1195     EmitAnyExpr(E->getLHS());
1196     return EmitLValue(E->getRHS());
1197   }
1198 
1199   // Can only get l-value for binary operator expressions which are a
1200   // simple assignment of aggregate type.
1201   if (E->getOpcode() != BinaryOperator::Assign)
1202     return EmitUnsupportedLValue(E, "binary l-value expression");
1203 
1204   llvm::Value *Temp = CreateTempAlloca(ConvertType(E->getType()));
1205   EmitAggExpr(E, Temp, false);
1206   // FIXME: Are these qualifiers correct?
1207   return LValue::MakeAddr(Temp, E->getType().getCVRQualifiers(),
1208                           getContext().getObjCGCAttrKind(E->getType()),
1209                           E->getType().getAddressSpace());
1210 }
1211 
1212 LValue CodeGenFunction::EmitCallExprLValue(const CallExpr *E) {
1213   RValue RV = EmitCallExpr(E);
1214 
1215   if (RV.isScalar()) {
1216     assert(E->getCallReturnType()->isReferenceType() &&
1217            "Can't have a scalar return unless the return type is a "
1218            "reference type!");
1219 
1220     return LValue::MakeAddr(RV.getScalarVal(), E->getType().getCVRQualifiers(),
1221                             getContext().getObjCGCAttrKind(E->getType()),
1222                             E->getType().getAddressSpace());
1223   }
1224 
1225   return LValue::MakeAddr(RV.getAggregateAddr(),
1226                           E->getType().getCVRQualifiers(),
1227                           getContext().getObjCGCAttrKind(E->getType()),
1228                           E->getType().getAddressSpace());
1229 }
1230 
1231 LValue CodeGenFunction::EmitVAArgExprLValue(const VAArgExpr *E) {
1232   // FIXME: This shouldn't require another copy.
1233   llvm::Value *Temp = CreateTempAlloca(ConvertType(E->getType()));
1234   EmitAggExpr(E, Temp, false);
1235   return LValue::MakeAddr(Temp, E->getType().getCVRQualifiers(),
1236                           QualType::GCNone, E->getType().getAddressSpace());
1237 }
1238 
1239 LValue
1240 CodeGenFunction::EmitCXXConditionDeclLValue(const CXXConditionDeclExpr *E) {
1241   EmitLocalBlockVarDecl(*E->getVarDecl());
1242   return EmitDeclRefLValue(E);
1243 }
1244 
1245 LValue CodeGenFunction::EmitCXXConstructLValue(const CXXConstructExpr *E) {
1246   llvm::Value *Temp = CreateTempAlloca(ConvertTypeForMem(E->getType()), "tmp");
1247   EmitCXXConstructExpr(Temp, E);
1248   return LValue::MakeAddr(Temp, E->getType().getCVRQualifiers(),
1249                           QualType::GCNone, E->getType().getAddressSpace());
1250 }
1251 
1252 LValue
1253 CodeGenFunction::EmitCXXBindTemporaryLValue(const CXXBindTemporaryExpr *E) {
1254   LValue LV = EmitLValue(E->getSubExpr());
1255 
1256   PushCXXTemporary(E->getTemporary(), LV.getAddress());
1257 
1258   return LV;
1259 }
1260 
1261 LValue CodeGenFunction::EmitObjCMessageExprLValue(const ObjCMessageExpr *E) {
1262   // Can only get l-value for message expression returning aggregate type
1263   RValue RV = EmitObjCMessageExpr(E);
1264   // FIXME: can this be volatile?
1265   return LValue::MakeAddr(RV.getAggregateAddr(),
1266                           E->getType().getCVRQualifiers(),
1267                           getContext().getObjCGCAttrKind(E->getType()),
1268                           E->getType().getAddressSpace());
1269 }
1270 
1271 llvm::Value *CodeGenFunction::EmitIvarOffset(const ObjCInterfaceDecl *Interface,
1272                                              const ObjCIvarDecl *Ivar) {
1273   return CGM.getObjCRuntime().EmitIvarOffset(*this, Interface, Ivar);
1274 }
1275 
1276 LValue CodeGenFunction::EmitLValueForIvar(QualType ObjectTy,
1277                                           llvm::Value *BaseValue,
1278                                           const ObjCIvarDecl *Ivar,
1279                                           unsigned CVRQualifiers) {
1280   return CGM.getObjCRuntime().EmitObjCValueForIvar(*this, ObjectTy, BaseValue,
1281                                                    Ivar, CVRQualifiers);
1282 }
1283 
1284 LValue CodeGenFunction::EmitObjCIvarRefLValue(const ObjCIvarRefExpr *E) {
1285   // FIXME: A lot of the code below could be shared with EmitMemberExpr.
1286   llvm::Value *BaseValue = 0;
1287   const Expr *BaseExpr = E->getBase();
1288   unsigned CVRQualifiers = 0;
1289   QualType ObjectTy;
1290   if (E->isArrow()) {
1291     BaseValue = EmitScalarExpr(BaseExpr);
1292     ObjectTy = BaseExpr->getType()->getPointeeType();
1293     CVRQualifiers = ObjectTy.getCVRQualifiers();
1294   } else {
1295     LValue BaseLV = EmitLValue(BaseExpr);
1296     // FIXME: this isn't right for bitfields.
1297     BaseValue = BaseLV.getAddress();
1298     ObjectTy = BaseExpr->getType();
1299     CVRQualifiers = ObjectTy.getCVRQualifiers();
1300   }
1301 
1302   return EmitLValueForIvar(ObjectTy, BaseValue, E->getDecl(), CVRQualifiers);
1303 }
1304 
1305 LValue
1306 CodeGenFunction::EmitObjCPropertyRefLValue(const ObjCPropertyRefExpr *E) {
1307   // This is a special l-value that just issues sends when we load or
1308   // store through it.
1309   return LValue::MakePropertyRef(E, E->getType().getCVRQualifiers());
1310 }
1311 
1312 LValue
1313 CodeGenFunction::EmitObjCKVCRefLValue(const ObjCKVCRefExpr *E) {
1314   // This is a special l-value that just issues sends when we load or
1315   // store through it.
1316   return LValue::MakeKVCRef(E, E->getType().getCVRQualifiers());
1317 }
1318 
1319 LValue
1320 CodeGenFunction::EmitObjCSuperExprLValue(const ObjCSuperExpr *E) {
1321   return EmitUnsupportedLValue(E, "use of super");
1322 }
1323 
1324 LValue CodeGenFunction::EmitStmtExprLValue(const StmtExpr *E) {
1325 
1326   // Can only get l-value for message expression returning aggregate type
1327   RValue RV = EmitAnyExprToTemp(E);
1328   // FIXME: can this be volatile?
1329   return LValue::MakeAddr(RV.getAggregateAddr(),
1330                           E->getType().getCVRQualifiers(),
1331                           getContext().getObjCGCAttrKind(E->getType()),
1332                           E->getType().getAddressSpace());
1333 }
1334 
1335 
1336 RValue CodeGenFunction::EmitCall(llvm::Value *Callee, QualType CalleeType,
1337                                  CallExpr::const_arg_iterator ArgBeg,
1338                                  CallExpr::const_arg_iterator ArgEnd,
1339                                  const Decl *TargetDecl) {
1340   // Get the actual function type. The callee type will always be a
1341   // pointer to function type or a block pointer type.
1342   assert(CalleeType->isFunctionPointerType() &&
1343          "Call must have function pointer type!");
1344 
1345   QualType FnType = CalleeType->getAsPointerType()->getPointeeType();
1346   QualType ResultType = FnType->getAsFunctionType()->getResultType();
1347 
1348   CallArgList Args;
1349   EmitCallArgs(Args, FnType->getAsFunctionProtoType(), ArgBeg, ArgEnd);
1350 
1351   return EmitCall(CGM.getTypes().getFunctionInfo(ResultType, Args),
1352                   Callee, Args, TargetDecl);
1353 }
1354