Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1 | //===--- CGExpr.cpp - Emit LLVM Code from Expressions ---------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 0bc735f | 2007-12-29 19:59:25 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 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" |
Daniel Dunbar | 0dbe227 | 2008-09-08 21:33:45 +0000 | [diff] [blame] | 16 | #include "CGCall.h" |
Daniel Dunbar | af2f62c | 2008-08-13 00:59:25 +0000 | [diff] [blame] | 17 | #include "CGObjCRuntime.h" |
Daniel Dunbar | de7fb84 | 2008-08-11 05:00:27 +0000 | [diff] [blame] | 18 | #include "clang/AST/ASTContext.h" |
Daniel Dunbar | c4a1dea | 2008-08-11 05:35:13 +0000 | [diff] [blame] | 19 | #include "clang/AST/DeclObjC.h" |
Eli Friedman | 316bb1b | 2008-05-17 20:03:47 +0000 | [diff] [blame] | 20 | #include "llvm/Target/TargetData.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 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 | return new llvm::AllocaInst(Ty, 0, Name, AllocaInsertPt); |
| 33 | } |
| 34 | |
| 35 | /// EvaluateExprAsBool - Perform the usual unary conversions on the specified |
| 36 | /// expression and compare the result against zero, returning an Int1Ty value. |
| 37 | llvm::Value *CodeGenFunction::EvaluateExprAsBool(const Expr *E) { |
Chris Lattner | 9069fa2 | 2007-08-26 16:46:58 +0000 | [diff] [blame] | 38 | QualType BoolTy = getContext().BoolTy; |
Chris Lattner | 9b2dc28 | 2008-04-04 16:54:41 +0000 | [diff] [blame] | 39 | if (!E->getType()->isAnyComplexType()) |
Chris Lattner | 9069fa2 | 2007-08-26 16:46:58 +0000 | [diff] [blame] | 40 | return EmitScalarConversion(EmitScalarExpr(E), E->getType(), BoolTy); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 41 | |
Chris Lattner | 9069fa2 | 2007-08-26 16:46:58 +0000 | [diff] [blame] | 42 | return EmitComplexToScalarConversion(EmitComplexExpr(E), E->getType(),BoolTy); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 43 | } |
| 44 | |
Chris Lattner | 9b65551 | 2007-08-31 22:49:20 +0000 | [diff] [blame] | 45 | /// EmitAnyExpr - Emit code to compute the specified expression which can have |
| 46 | /// any type. The result is returned as an RValue struct. If this is an |
| 47 | /// aggregate expression, the aggloc/agglocvolatile arguments indicate where |
| 48 | /// the result should be returned. |
| 49 | RValue CodeGenFunction::EmitAnyExpr(const Expr *E, llvm::Value *AggLoc, |
| 50 | bool isAggLocVolatile) { |
| 51 | if (!hasAggregateLLVMType(E->getType())) |
| 52 | return RValue::get(EmitScalarExpr(E)); |
Chris Lattner | 9b2dc28 | 2008-04-04 16:54:41 +0000 | [diff] [blame] | 53 | else if (E->getType()->isAnyComplexType()) |
Chris Lattner | 9b65551 | 2007-08-31 22:49:20 +0000 | [diff] [blame] | 54 | return RValue::getComplex(EmitComplexExpr(E)); |
| 55 | |
| 56 | EmitAggExpr(E, AggLoc, isAggLocVolatile); |
| 57 | return RValue::getAggregate(AggLoc); |
| 58 | } |
| 59 | |
Daniel Dunbar | 46f45b9 | 2008-09-09 01:06:48 +0000 | [diff] [blame] | 60 | /// EmitAnyExprToTemp - Similary to EmitAnyExpr(), however, the result |
| 61 | /// will always be accessible even if no aggregate location is |
| 62 | /// provided. |
| 63 | RValue CodeGenFunction::EmitAnyExprToTemp(const Expr *E, llvm::Value *AggLoc, |
| 64 | bool isAggLocVolatile) { |
| 65 | if (!AggLoc && hasAggregateLLVMType(E->getType()) && |
| 66 | !E->getType()->isAnyComplexType()) |
| 67 | AggLoc = CreateTempAlloca(ConvertType(E->getType()), "agg.tmp"); |
| 68 | return EmitAnyExpr(E, AggLoc, isAggLocVolatile); |
| 69 | } |
| 70 | |
Dan Gohman | 4f8d123 | 2008-05-22 00:50:06 +0000 | [diff] [blame] | 71 | /// getAccessedFieldNo - Given an encoded value and a result number, return |
| 72 | /// the input field number being accessed. |
| 73 | unsigned CodeGenFunction::getAccessedFieldNo(unsigned Idx, |
| 74 | const llvm::Constant *Elts) { |
| 75 | if (isa<llvm::ConstantAggregateZero>(Elts)) |
| 76 | return 0; |
| 77 | |
| 78 | return cast<llvm::ConstantInt>(Elts->getOperand(Idx))->getZExtValue(); |
| 79 | } |
| 80 | |
Chris Lattner | 9b65551 | 2007-08-31 22:49:20 +0000 | [diff] [blame] | 81 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 82 | //===----------------------------------------------------------------------===// |
| 83 | // LValue Expression Emission |
| 84 | //===----------------------------------------------------------------------===// |
| 85 | |
Daniel Dunbar | ce1d38b | 2009-01-09 16:50:52 +0000 | [diff] [blame] | 86 | RValue CodeGenFunction::EmitUnsupportedRValue(const Expr *E, |
| 87 | const char *Name) { |
| 88 | ErrorUnsupported(E, Name); |
Daniel Dunbar | 8fa73ed | 2009-01-09 20:09:28 +0000 | [diff] [blame] | 89 | if (const ComplexType *CTy = E->getType()->getAsComplexType()) { |
| 90 | const llvm::Type *EltTy = ConvertType(CTy->getElementType()); |
| 91 | llvm::Value *U = llvm::UndefValue::get(EltTy); |
| 92 | return RValue::getComplex(std::make_pair(U, U)); |
| 93 | } else if (hasAggregateLLVMType(E->getType())) { |
| 94 | const llvm::Type *Ty = |
| 95 | llvm::PointerType::getUnqual(ConvertType(E->getType())); |
| 96 | return RValue::getAggregate(llvm::UndefValue::get(Ty)); |
| 97 | } else { |
| 98 | const llvm::Type *Ty = ConvertType(E->getType()); |
| 99 | return RValue::get(llvm::UndefValue::get(Ty)); |
| 100 | } |
Daniel Dunbar | ce1d38b | 2009-01-09 16:50:52 +0000 | [diff] [blame] | 101 | } |
| 102 | |
Daniel Dunbar | 6ba82a4 | 2008-08-25 20:45:57 +0000 | [diff] [blame] | 103 | LValue CodeGenFunction::EmitUnsupportedLValue(const Expr *E, |
| 104 | const char *Name) { |
| 105 | ErrorUnsupported(E, Name); |
| 106 | llvm::Type *Ty = llvm::PointerType::getUnqual(ConvertType(E->getType())); |
| 107 | return LValue::MakeAddr(llvm::UndefValue::get(Ty), |
| 108 | E->getType().getCVRQualifiers()); |
| 109 | } |
| 110 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 111 | /// EmitLValue - Emit code to compute a designator that specifies the location |
| 112 | /// of the expression. |
| 113 | /// |
| 114 | /// This can return one of two things: a simple address or a bitfield |
| 115 | /// reference. In either case, the LLVM Value* in the LValue structure is |
| 116 | /// guaranteed to be an LLVM pointer type. |
| 117 | /// |
| 118 | /// If this returns a bitfield reference, nothing about the pointee type of |
| 119 | /// the LLVM value is known: For example, it may not be a pointer to an |
| 120 | /// integer. |
| 121 | /// |
| 122 | /// If this returns a normal address, and if the lvalue's C type is fixed |
| 123 | /// size, this method guarantees that the returned pointer type will point to |
| 124 | /// an LLVM type of the same size of the lvalue's type. If the lvalue has a |
| 125 | /// variable length type, this is not possible. |
| 126 | /// |
| 127 | LValue CodeGenFunction::EmitLValue(const Expr *E) { |
| 128 | switch (E->getStmtClass()) { |
Daniel Dunbar | 6ba82a4 | 2008-08-25 20:45:57 +0000 | [diff] [blame] | 129 | default: return EmitUnsupportedLValue(E, "l-value expression"); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 130 | |
Daniel Dunbar | 80e62c2 | 2008-09-04 03:20:13 +0000 | [diff] [blame] | 131 | case Expr::BinaryOperatorClass: |
| 132 | return EmitBinaryOperatorLValue(cast<BinaryOperator>(E)); |
Douglas Gregor | b460980 | 2008-11-14 16:09:21 +0000 | [diff] [blame] | 133 | case Expr::CallExprClass: |
| 134 | case Expr::CXXOperatorCallExprClass: |
| 135 | return EmitCallExprLValue(cast<CallExpr>(E)); |
Douglas Gregor | 1a49af9 | 2009-01-06 05:10:23 +0000 | [diff] [blame] | 136 | case Expr::DeclRefExprClass: |
| 137 | case Expr::QualifiedDeclRefExprClass: |
| 138 | return EmitDeclRefLValue(cast<DeclRefExpr>(E)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 139 | case Expr::ParenExprClass:return EmitLValue(cast<ParenExpr>(E)->getSubExpr()); |
Chris Lattner | d9f6910 | 2008-08-10 01:53:14 +0000 | [diff] [blame] | 140 | case Expr::PredefinedExprClass: |
| 141 | return EmitPredefinedLValue(cast<PredefinedExpr>(E)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 142 | case Expr::StringLiteralClass: |
| 143 | return EmitStringLiteralLValue(cast<StringLiteral>(E)); |
Chris Lattner | 391d77a | 2008-03-30 23:03:07 +0000 | [diff] [blame] | 144 | |
Argyrios Kyrtzidis | e3a09e6 | 2008-09-10 02:36:38 +0000 | [diff] [blame] | 145 | case Expr::CXXConditionDeclExprClass: |
| 146 | return EmitCXXConditionDeclLValue(cast<CXXConditionDeclExpr>(E)); |
| 147 | |
Daniel Dunbar | 0a04d77 | 2008-08-23 10:51:21 +0000 | [diff] [blame] | 148 | case Expr::ObjCMessageExprClass: |
| 149 | return EmitObjCMessageExprLValue(cast<ObjCMessageExpr>(E)); |
Chris Lattner | 391d77a | 2008-03-30 23:03:07 +0000 | [diff] [blame] | 150 | case Expr::ObjCIvarRefExprClass: |
| 151 | return EmitObjCIvarRefLValue(cast<ObjCIvarRefExpr>(E)); |
Daniel Dunbar | 6ba82a4 | 2008-08-25 20:45:57 +0000 | [diff] [blame] | 152 | case Expr::ObjCPropertyRefExprClass: |
Daniel Dunbar | 85c59ed | 2008-08-29 08:11:39 +0000 | [diff] [blame] | 153 | return EmitObjCPropertyRefLValue(cast<ObjCPropertyRefExpr>(E)); |
Fariborz Jahanian | 43f4470 | 2008-11-22 22:30:21 +0000 | [diff] [blame] | 154 | case Expr::ObjCKVCRefExprClass: |
| 155 | return EmitObjCKVCRefLValue(cast<ObjCKVCRefExpr>(E)); |
Douglas Gregor | cd9b46e | 2008-11-04 14:56:14 +0000 | [diff] [blame] | 156 | case Expr::ObjCSuperExprClass: |
| 157 | return EmitObjCSuperExpr(cast<ObjCSuperExpr>(E)); |
| 158 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 159 | case Expr::UnaryOperatorClass: |
| 160 | return EmitUnaryOpLValue(cast<UnaryOperator>(E)); |
| 161 | case Expr::ArraySubscriptExprClass: |
| 162 | return EmitArraySubscriptExpr(cast<ArraySubscriptExpr>(E)); |
Nate Begeman | 213541a | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 163 | case Expr::ExtVectorElementExprClass: |
| 164 | return EmitExtVectorElementExpr(cast<ExtVectorElementExpr>(E)); |
Devang Patel | b9b00ad | 2007-10-23 20:28:39 +0000 | [diff] [blame] | 165 | case Expr::MemberExprClass: return EmitMemberExpr(cast<MemberExpr>(E)); |
Eli Friedman | 06e863f | 2008-05-13 23:18:27 +0000 | [diff] [blame] | 166 | case Expr::CompoundLiteralExprClass: |
| 167 | return EmitCompoundLiteralLValue(cast<CompoundLiteralExpr>(E)); |
Chris Lattner | 670a62c | 2008-12-12 05:35:08 +0000 | [diff] [blame] | 168 | case Expr::ChooseExprClass: |
| 169 | // __builtin_choose_expr is the lvalue of the selected operand. |
| 170 | if (cast<ChooseExpr>(E)->isConditionTrue(getContext())) |
| 171 | return EmitLValue(cast<ChooseExpr>(E)->getLHS()); |
| 172 | else |
| 173 | return EmitLValue(cast<ChooseExpr>(E)->getRHS()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 174 | } |
| 175 | } |
| 176 | |
| 177 | /// EmitLoadOfLValue - Given an expression that represents a value lvalue, |
| 178 | /// this method emits the address of the lvalue, then loads the result as an |
| 179 | /// rvalue, returning the rvalue. |
| 180 | RValue CodeGenFunction::EmitLoadOfLValue(LValue LV, QualType ExprType) { |
Fariborz Jahanian | dbd32c2 | 2008-11-19 17:34:06 +0000 | [diff] [blame] | 181 | if (LV.isObjCWeak()) { |
Fariborz Jahanian | 6dc2317 | 2008-11-18 21:45:40 +0000 | [diff] [blame] | 182 | // load of a __weak object. |
| 183 | llvm::Value *AddrWeakObj = LV.getAddress(); |
Fariborz Jahanian | 3e283e3 | 2008-11-18 22:37:34 +0000 | [diff] [blame] | 184 | llvm::Value *read_weak = CGM.getObjCRuntime().EmitObjCWeakRead(*this, |
Fariborz Jahanian | 6dc2317 | 2008-11-18 21:45:40 +0000 | [diff] [blame] | 185 | AddrWeakObj); |
| 186 | return RValue::get(read_weak); |
| 187 | } |
| 188 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 189 | if (LV.isSimple()) { |
| 190 | llvm::Value *Ptr = LV.getAddress(); |
| 191 | const llvm::Type *EltTy = |
| 192 | cast<llvm::PointerType>(Ptr->getType())->getElementType(); |
| 193 | |
| 194 | // Simple scalar l-value. |
Dan Gohman | d79a726 | 2008-05-22 22:12:56 +0000 | [diff] [blame] | 195 | if (EltTy->isSingleValueType()) { |
Eli Friedman | 1e692ac | 2008-06-13 23:01:12 +0000 | [diff] [blame] | 196 | llvm::Value *V = Builder.CreateLoad(Ptr, LV.isVolatileQualified(),"tmp"); |
Chris Lattner | 01e3c9e | 2008-01-30 07:01:17 +0000 | [diff] [blame] | 197 | |
| 198 | // Bool can have different representation in memory than in registers. |
| 199 | if (ExprType->isBooleanType()) { |
| 200 | if (V->getType() != llvm::Type::Int1Ty) |
| 201 | V = Builder.CreateTrunc(V, llvm::Type::Int1Ty, "tobool"); |
| 202 | } |
| 203 | |
| 204 | return RValue::get(V); |
| 205 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 206 | |
Chris Lattner | 883f6a7 | 2007-08-11 00:04:45 +0000 | [diff] [blame] | 207 | assert(ExprType->isFunctionType() && "Unknown scalar value"); |
| 208 | return RValue::get(Ptr); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 209 | } |
| 210 | |
| 211 | if (LV.isVectorElt()) { |
Eli Friedman | 1e692ac | 2008-06-13 23:01:12 +0000 | [diff] [blame] | 212 | llvm::Value *Vec = Builder.CreateLoad(LV.getVectorAddr(), |
| 213 | LV.isVolatileQualified(), "tmp"); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 214 | return RValue::get(Builder.CreateExtractElement(Vec, LV.getVectorIdx(), |
| 215 | "vecext")); |
| 216 | } |
Chris Lattner | 46ea8eb | 2007-08-03 00:16:29 +0000 | [diff] [blame] | 217 | |
| 218 | // If this is a reference to a subset of the elements of a vector, either |
| 219 | // shuffle the input or extract/insert them as appropriate. |
Nate Begeman | 213541a | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 220 | if (LV.isExtVectorElt()) |
| 221 | return EmitLoadOfExtVectorElementLValue(LV, ExprType); |
Lauro Ramos Venancio | 3b8c22d | 2008-01-22 20:17:04 +0000 | [diff] [blame] | 222 | |
| 223 | if (LV.isBitfield()) |
| 224 | return EmitLoadOfBitfieldLValue(LV, ExprType); |
| 225 | |
Daniel Dunbar | 85c59ed | 2008-08-29 08:11:39 +0000 | [diff] [blame] | 226 | if (LV.isPropertyRef()) |
| 227 | return EmitLoadOfPropertyRefLValue(LV, ExprType); |
| 228 | |
Fariborz Jahanian | 43f4470 | 2008-11-22 22:30:21 +0000 | [diff] [blame] | 229 | if (LV.isKVCRef()) |
| 230 | return EmitLoadOfKVCRefLValue(LV, ExprType); |
| 231 | |
Lauro Ramos Venancio | 3b8c22d | 2008-01-22 20:17:04 +0000 | [diff] [blame] | 232 | assert(0 && "Unknown LValue type!"); |
Chris Lattner | b1776cb | 2007-09-16 19:23:47 +0000 | [diff] [blame] | 233 | //an invalid RValue, but the assert will |
| 234 | //ensure that this point is never reached |
| 235 | return RValue(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 236 | } |
| 237 | |
Lauro Ramos Venancio | 3b8c22d | 2008-01-22 20:17:04 +0000 | [diff] [blame] | 238 | RValue CodeGenFunction::EmitLoadOfBitfieldLValue(LValue LV, |
| 239 | QualType ExprType) { |
Daniel Dunbar | 10e3ded | 2008-08-06 05:08:45 +0000 | [diff] [blame] | 240 | unsigned StartBit = LV.getBitfieldStartBit(); |
| 241 | unsigned BitfieldSize = LV.getBitfieldSize(); |
Lauro Ramos Venancio | 3b8c22d | 2008-01-22 20:17:04 +0000 | [diff] [blame] | 242 | llvm::Value *Ptr = LV.getBitfieldAddr(); |
Daniel Dunbar | 10e3ded | 2008-08-06 05:08:45 +0000 | [diff] [blame] | 243 | |
| 244 | const llvm::Type *EltTy = |
Lauro Ramos Venancio | 3b8c22d | 2008-01-22 20:17:04 +0000 | [diff] [blame] | 245 | cast<llvm::PointerType>(Ptr->getType())->getElementType(); |
Daniel Dunbar | 10e3ded | 2008-08-06 05:08:45 +0000 | [diff] [blame] | 246 | unsigned EltTySize = CGM.getTargetData().getTypeSizeInBits(EltTy); |
Lauro Ramos Venancio | 3b8c22d | 2008-01-22 20:17:04 +0000 | [diff] [blame] | 247 | |
Daniel Dunbar | 10e3ded | 2008-08-06 05:08:45 +0000 | [diff] [blame] | 248 | // In some cases the bitfield may straddle two memory locations. |
| 249 | // Currently we load the entire bitfield, then do the magic to |
| 250 | // sign-extend it if necessary. This results in somewhat more code |
| 251 | // than necessary for the common case (one load), since two shifts |
| 252 | // accomplish both the masking and sign extension. |
| 253 | unsigned LowBits = std::min(BitfieldSize, EltTySize - StartBit); |
| 254 | llvm::Value *Val = Builder.CreateLoad(Ptr, LV.isVolatileQualified(), "tmp"); |
| 255 | |
| 256 | // Shift to proper location. |
Daniel Dunbar | f3edc2f | 2008-11-13 02:20:34 +0000 | [diff] [blame] | 257 | if (StartBit) |
| 258 | Val = Builder.CreateLShr(Val, llvm::ConstantInt::get(EltTy, StartBit), |
| 259 | "bf.lo"); |
Daniel Dunbar | 10e3ded | 2008-08-06 05:08:45 +0000 | [diff] [blame] | 260 | |
| 261 | // Mask off unused bits. |
| 262 | llvm::Constant *LowMask = |
| 263 | llvm::ConstantInt::get(llvm::APInt::getLowBitsSet(EltTySize, LowBits)); |
| 264 | Val = Builder.CreateAnd(Val, LowMask, "bf.lo.cleared"); |
| 265 | |
| 266 | // Fetch the high bits if necessary. |
| 267 | if (LowBits < BitfieldSize) { |
| 268 | unsigned HighBits = BitfieldSize - LowBits; |
| 269 | llvm::Value *HighPtr = |
| 270 | Builder.CreateGEP(Ptr, llvm::ConstantInt::get(llvm::Type::Int32Ty, 1), |
| 271 | "bf.ptr.hi"); |
| 272 | llvm::Value *HighVal = Builder.CreateLoad(HighPtr, |
| 273 | LV.isVolatileQualified(), |
| 274 | "tmp"); |
| 275 | |
| 276 | // Mask off unused bits. |
| 277 | llvm::Constant *HighMask = |
| 278 | llvm::ConstantInt::get(llvm::APInt::getLowBitsSet(EltTySize, HighBits)); |
| 279 | HighVal = Builder.CreateAnd(HighVal, HighMask, "bf.lo.cleared"); |
Lauro Ramos Venancio | 3b8c22d | 2008-01-22 20:17:04 +0000 | [diff] [blame] | 280 | |
Daniel Dunbar | 10e3ded | 2008-08-06 05:08:45 +0000 | [diff] [blame] | 281 | // Shift to proper location and or in to bitfield value. |
| 282 | HighVal = Builder.CreateShl(HighVal, |
| 283 | llvm::ConstantInt::get(EltTy, LowBits)); |
| 284 | Val = Builder.CreateOr(Val, HighVal, "bf.val"); |
| 285 | } |
Lauro Ramos Venancio | 3b8c22d | 2008-01-22 20:17:04 +0000 | [diff] [blame] | 286 | |
Daniel Dunbar | 10e3ded | 2008-08-06 05:08:45 +0000 | [diff] [blame] | 287 | // Sign extend if necessary. |
| 288 | if (LV.isBitfieldSigned()) { |
| 289 | llvm::Value *ExtraBits = llvm::ConstantInt::get(EltTy, |
| 290 | EltTySize - BitfieldSize); |
| 291 | Val = Builder.CreateAShr(Builder.CreateShl(Val, ExtraBits), |
| 292 | ExtraBits, "bf.val.sext"); |
| 293 | } |
Eli Friedman | 316bb1b | 2008-05-17 20:03:47 +0000 | [diff] [blame] | 294 | |
| 295 | // The bitfield type and the normal type differ when the storage sizes |
| 296 | // differ (currently just _Bool). |
Daniel Dunbar | 10e3ded | 2008-08-06 05:08:45 +0000 | [diff] [blame] | 297 | Val = Builder.CreateIntCast(Val, ConvertType(ExprType), false, "tmp"); |
Eli Friedman | 316bb1b | 2008-05-17 20:03:47 +0000 | [diff] [blame] | 298 | |
Daniel Dunbar | 10e3ded | 2008-08-06 05:08:45 +0000 | [diff] [blame] | 299 | return RValue::get(Val); |
Lauro Ramos Venancio | 3b8c22d | 2008-01-22 20:17:04 +0000 | [diff] [blame] | 300 | } |
| 301 | |
Daniel Dunbar | 85c59ed | 2008-08-29 08:11:39 +0000 | [diff] [blame] | 302 | RValue CodeGenFunction::EmitLoadOfPropertyRefLValue(LValue LV, |
| 303 | QualType ExprType) { |
| 304 | return EmitObjCPropertyGet(LV.getPropertyRefExpr()); |
| 305 | } |
| 306 | |
Fariborz Jahanian | 43f4470 | 2008-11-22 22:30:21 +0000 | [diff] [blame] | 307 | RValue CodeGenFunction::EmitLoadOfKVCRefLValue(LValue LV, |
| 308 | QualType ExprType) { |
| 309 | return EmitObjCPropertyGet(LV.getKVCRefExpr()); |
| 310 | } |
| 311 | |
Nate Begeman | 6fe7c8a | 2009-01-18 06:42:49 +0000 | [diff] [blame] | 312 | // If this is a reference to a subset of the elements of a vector, create an |
| 313 | // appropriate shufflevector. |
Nate Begeman | 213541a | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 314 | RValue CodeGenFunction::EmitLoadOfExtVectorElementLValue(LValue LV, |
| 315 | QualType ExprType) { |
Eli Friedman | 1e692ac | 2008-06-13 23:01:12 +0000 | [diff] [blame] | 316 | llvm::Value *Vec = Builder.CreateLoad(LV.getExtVectorAddr(), |
| 317 | LV.isVolatileQualified(), "tmp"); |
Chris Lattner | 34cdc86 | 2007-08-03 16:18:34 +0000 | [diff] [blame] | 318 | |
Nate Begeman | 8a99764 | 2008-05-09 06:41:27 +0000 | [diff] [blame] | 319 | const llvm::Constant *Elts = LV.getExtVectorElts(); |
Chris Lattner | 34cdc86 | 2007-08-03 16:18:34 +0000 | [diff] [blame] | 320 | |
| 321 | // If the result of the expression is a non-vector type, we must be |
| 322 | // extracting a single element. Just codegen as an extractelement. |
Chris Lattner | cf60cd2 | 2007-08-10 17:10:08 +0000 | [diff] [blame] | 323 | const VectorType *ExprVT = ExprType->getAsVectorType(); |
| 324 | if (!ExprVT) { |
Dan Gohman | 4f8d123 | 2008-05-22 00:50:06 +0000 | [diff] [blame] | 325 | unsigned InIdx = getAccessedFieldNo(0, Elts); |
Chris Lattner | 34cdc86 | 2007-08-03 16:18:34 +0000 | [diff] [blame] | 326 | llvm::Value *Elt = llvm::ConstantInt::get(llvm::Type::Int32Ty, InIdx); |
| 327 | return RValue::get(Builder.CreateExtractElement(Vec, Elt, "tmp")); |
| 328 | } |
Nate Begeman | 6fe7c8a | 2009-01-18 06:42:49 +0000 | [diff] [blame] | 329 | |
| 330 | // Always use shuffle vector to try to retain the original program structure |
Chris Lattner | cf60cd2 | 2007-08-10 17:10:08 +0000 | [diff] [blame] | 331 | unsigned NumResultElts = ExprVT->getNumElements(); |
Chris Lattner | 34cdc86 | 2007-08-03 16:18:34 +0000 | [diff] [blame] | 332 | |
Nate Begeman | 6fe7c8a | 2009-01-18 06:42:49 +0000 | [diff] [blame] | 333 | llvm::SmallVector<llvm::Constant*, 4> Mask; |
Chris Lattner | 34cdc86 | 2007-08-03 16:18:34 +0000 | [diff] [blame] | 334 | for (unsigned i = 0; i != NumResultElts; ++i) { |
Dan Gohman | 4f8d123 | 2008-05-22 00:50:06 +0000 | [diff] [blame] | 335 | unsigned InIdx = getAccessedFieldNo(i, Elts); |
Nate Begeman | 6fe7c8a | 2009-01-18 06:42:49 +0000 | [diff] [blame] | 336 | Mask.push_back(llvm::ConstantInt::get(llvm::Type::Int32Ty, InIdx)); |
Chris Lattner | 34cdc86 | 2007-08-03 16:18:34 +0000 | [diff] [blame] | 337 | } |
| 338 | |
Nate Begeman | 6fe7c8a | 2009-01-18 06:42:49 +0000 | [diff] [blame] | 339 | llvm::Value *MaskV = llvm::ConstantVector::get(&Mask[0], Mask.size()); |
| 340 | Vec = Builder.CreateShuffleVector(Vec, |
| 341 | llvm::UndefValue::get(Vec->getType()), |
| 342 | MaskV, "tmp"); |
| 343 | return RValue::get(Vec); |
Chris Lattner | 34cdc86 | 2007-08-03 16:18:34 +0000 | [diff] [blame] | 344 | } |
| 345 | |
| 346 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 347 | |
| 348 | /// EmitStoreThroughLValue - Store the specified rvalue into the specified |
| 349 | /// lvalue, where both are guaranteed to the have the same type, and that type |
| 350 | /// is 'Ty'. |
| 351 | void CodeGenFunction::EmitStoreThroughLValue(RValue Src, LValue Dst, |
| 352 | QualType Ty) { |
Chris Lattner | 017d6aa | 2007-08-03 16:28:33 +0000 | [diff] [blame] | 353 | if (!Dst.isSimple()) { |
| 354 | if (Dst.isVectorElt()) { |
| 355 | // Read/modify/write the vector, inserting the new element. |
Eli Friedman | 1e692ac | 2008-06-13 23:01:12 +0000 | [diff] [blame] | 356 | llvm::Value *Vec = Builder.CreateLoad(Dst.getVectorAddr(), |
| 357 | Dst.isVolatileQualified(), "tmp"); |
Chris Lattner | 9b65551 | 2007-08-31 22:49:20 +0000 | [diff] [blame] | 358 | Vec = Builder.CreateInsertElement(Vec, Src.getScalarVal(), |
Chris Lattner | 017d6aa | 2007-08-03 16:28:33 +0000 | [diff] [blame] | 359 | Dst.getVectorIdx(), "vecins"); |
Eli Friedman | 1e692ac | 2008-06-13 23:01:12 +0000 | [diff] [blame] | 360 | Builder.CreateStore(Vec, Dst.getVectorAddr(),Dst.isVolatileQualified()); |
Chris Lattner | 017d6aa | 2007-08-03 16:28:33 +0000 | [diff] [blame] | 361 | return; |
| 362 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 363 | |
Nate Begeman | 213541a | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 364 | // If this is an update of extended vector elements, insert them as |
| 365 | // appropriate. |
| 366 | if (Dst.isExtVectorElt()) |
| 367 | return EmitStoreThroughExtVectorComponentLValue(Src, Dst, Ty); |
Lauro Ramos Venancio | a0c5d0e | 2008-01-22 22:36:45 +0000 | [diff] [blame] | 368 | |
| 369 | if (Dst.isBitfield()) |
| 370 | return EmitStoreThroughBitfieldLValue(Src, Dst, Ty); |
| 371 | |
Daniel Dunbar | 85c59ed | 2008-08-29 08:11:39 +0000 | [diff] [blame] | 372 | if (Dst.isPropertyRef()) |
| 373 | return EmitStoreThroughPropertyRefLValue(Src, Dst, Ty); |
| 374 | |
Fariborz Jahanian | 43f4470 | 2008-11-22 22:30:21 +0000 | [diff] [blame] | 375 | if (Dst.isKVCRef()) |
| 376 | return EmitStoreThroughKVCRefLValue(Src, Dst, Ty); |
| 377 | |
Lauro Ramos Venancio | 6553982 | 2008-01-22 22:38:35 +0000 | [diff] [blame] | 378 | assert(0 && "Unknown LValue type"); |
Chris Lattner | 017d6aa | 2007-08-03 16:28:33 +0000 | [diff] [blame] | 379 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 380 | |
Fariborz Jahanian | dbd32c2 | 2008-11-19 17:34:06 +0000 | [diff] [blame] | 381 | if (Dst.isObjCWeak()) { |
| 382 | // load of a __weak object. |
| 383 | llvm::Value *LvalueDst = Dst.getAddress(); |
| 384 | llvm::Value *src = Src.getScalarVal(); |
| 385 | CGM.getObjCRuntime().EmitObjCWeakAssign(*this, src, LvalueDst); |
| 386 | return; |
| 387 | } |
| 388 | |
| 389 | if (Dst.isObjCStrong()) { |
| 390 | // load of a __strong object. |
| 391 | llvm::Value *LvalueDst = Dst.getAddress(); |
| 392 | llvm::Value *src = Src.getScalarVal(); |
Fariborz Jahanian | d1cc804 | 2008-11-20 20:53:20 +0000 | [diff] [blame] | 393 | if (Dst.isObjCIvar()) |
| 394 | CGM.getObjCRuntime().EmitObjCIvarAssign(*this, src, LvalueDst); |
| 395 | else |
| 396 | CGM.getObjCRuntime().EmitObjCGlobalAssign(*this, src, LvalueDst); |
Fariborz Jahanian | dbd32c2 | 2008-11-19 17:34:06 +0000 | [diff] [blame] | 397 | return; |
| 398 | } |
| 399 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 400 | llvm::Value *DstAddr = Dst.getAddress(); |
Chris Lattner | 883f6a7 | 2007-08-11 00:04:45 +0000 | [diff] [blame] | 401 | assert(Src.isScalar() && "Can't emit an agg store with this method"); |
| 402 | // FIXME: Handle volatility etc. |
Chris Lattner | 9b65551 | 2007-08-31 22:49:20 +0000 | [diff] [blame] | 403 | const llvm::Type *SrcTy = Src.getScalarVal()->getType(); |
Christopher Lamb | ddc23f3 | 2007-12-17 01:11:20 +0000 | [diff] [blame] | 404 | const llvm::PointerType *DstPtr = cast<llvm::PointerType>(DstAddr->getType()); |
| 405 | const llvm::Type *AddrTy = DstPtr->getElementType(); |
| 406 | unsigned AS = DstPtr->getAddressSpace(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 407 | |
Chris Lattner | 883f6a7 | 2007-08-11 00:04:45 +0000 | [diff] [blame] | 408 | if (AddrTy != SrcTy) |
Christopher Lamb | ddc23f3 | 2007-12-17 01:11:20 +0000 | [diff] [blame] | 409 | DstAddr = Builder.CreateBitCast(DstAddr, |
| 410 | llvm::PointerType::get(SrcTy, AS), |
Chris Lattner | 883f6a7 | 2007-08-11 00:04:45 +0000 | [diff] [blame] | 411 | "storetmp"); |
Eli Friedman | 1e692ac | 2008-06-13 23:01:12 +0000 | [diff] [blame] | 412 | Builder.CreateStore(Src.getScalarVal(), DstAddr, Dst.isVolatileQualified()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 413 | } |
| 414 | |
Lauro Ramos Venancio | a0c5d0e | 2008-01-22 22:36:45 +0000 | [diff] [blame] | 415 | void CodeGenFunction::EmitStoreThroughBitfieldLValue(RValue Src, LValue Dst, |
Daniel Dunbar | ed3849b | 2008-11-19 09:36:46 +0000 | [diff] [blame] | 416 | QualType Ty, |
| 417 | llvm::Value **Result) { |
Daniel Dunbar | 10e3ded | 2008-08-06 05:08:45 +0000 | [diff] [blame] | 418 | unsigned StartBit = Dst.getBitfieldStartBit(); |
| 419 | unsigned BitfieldSize = Dst.getBitfieldSize(); |
Lauro Ramos Venancio | a0c5d0e | 2008-01-22 22:36:45 +0000 | [diff] [blame] | 420 | llvm::Value *Ptr = Dst.getBitfieldAddr(); |
Lauro Ramos Venancio | a0c5d0e | 2008-01-22 22:36:45 +0000 | [diff] [blame] | 421 | |
Daniel Dunbar | 10e3ded | 2008-08-06 05:08:45 +0000 | [diff] [blame] | 422 | const llvm::Type *EltTy = |
| 423 | cast<llvm::PointerType>(Ptr->getType())->getElementType(); |
| 424 | unsigned EltTySize = CGM.getTargetData().getTypeSizeInBits(EltTy); |
| 425 | |
| 426 | // Get the new value, cast to the appropriate type and masked to |
| 427 | // exactly the size of the bit-field. |
Daniel Dunbar | ed3849b | 2008-11-19 09:36:46 +0000 | [diff] [blame] | 428 | llvm::Value *SrcVal = Src.getScalarVal(); |
| 429 | llvm::Value *NewVal = Builder.CreateIntCast(SrcVal, EltTy, false, "tmp"); |
Daniel Dunbar | 10e3ded | 2008-08-06 05:08:45 +0000 | [diff] [blame] | 430 | llvm::Constant *Mask = |
| 431 | llvm::ConstantInt::get(llvm::APInt::getLowBitsSet(EltTySize, BitfieldSize)); |
| 432 | NewVal = Builder.CreateAnd(NewVal, Mask, "bf.value"); |
Lauro Ramos Venancio | a0c5d0e | 2008-01-22 22:36:45 +0000 | [diff] [blame] | 433 | |
Daniel Dunbar | ed3849b | 2008-11-19 09:36:46 +0000 | [diff] [blame] | 434 | // Return the new value of the bit-field, if requested. |
| 435 | if (Result) { |
| 436 | // Cast back to the proper type for result. |
| 437 | const llvm::Type *SrcTy = SrcVal->getType(); |
| 438 | llvm::Value *SrcTrunc = Builder.CreateIntCast(NewVal, SrcTy, false, |
| 439 | "bf.reload.val"); |
| 440 | |
| 441 | // Sign extend if necessary. |
| 442 | if (Dst.isBitfieldSigned()) { |
| 443 | unsigned SrcTySize = CGM.getTargetData().getTypeSizeInBits(SrcTy); |
| 444 | llvm::Value *ExtraBits = llvm::ConstantInt::get(SrcTy, |
| 445 | SrcTySize - BitfieldSize); |
| 446 | SrcTrunc = Builder.CreateAShr(Builder.CreateShl(SrcTrunc, ExtraBits), |
| 447 | ExtraBits, "bf.reload.sext"); |
| 448 | } |
| 449 | |
| 450 | *Result = SrcTrunc; |
| 451 | } |
| 452 | |
Daniel Dunbar | 10e3ded | 2008-08-06 05:08:45 +0000 | [diff] [blame] | 453 | // In some cases the bitfield may straddle two memory locations. |
| 454 | // Emit the low part first and check to see if the high needs to be |
| 455 | // done. |
| 456 | unsigned LowBits = std::min(BitfieldSize, EltTySize - StartBit); |
| 457 | llvm::Value *LowVal = Builder.CreateLoad(Ptr, Dst.isVolatileQualified(), |
| 458 | "bf.prev.low"); |
Eli Friedman | 316bb1b | 2008-05-17 20:03:47 +0000 | [diff] [blame] | 459 | |
Daniel Dunbar | 10e3ded | 2008-08-06 05:08:45 +0000 | [diff] [blame] | 460 | // Compute the mask for zero-ing the low part of this bitfield. |
| 461 | llvm::Constant *InvMask = |
| 462 | llvm::ConstantInt::get(~llvm::APInt::getBitsSet(EltTySize, StartBit, |
| 463 | StartBit + LowBits)); |
| 464 | |
| 465 | // Compute the new low part as |
| 466 | // LowVal = (LowVal & InvMask) | (NewVal << StartBit), |
| 467 | // with the shift of NewVal implicitly stripping the high bits. |
| 468 | llvm::Value *NewLowVal = |
| 469 | Builder.CreateShl(NewVal, llvm::ConstantInt::get(EltTy, StartBit), |
| 470 | "bf.value.lo"); |
| 471 | LowVal = Builder.CreateAnd(LowVal, InvMask, "bf.prev.lo.cleared"); |
| 472 | LowVal = Builder.CreateOr(LowVal, NewLowVal, "bf.new.lo"); |
| 473 | |
| 474 | // Write back. |
| 475 | Builder.CreateStore(LowVal, Ptr, Dst.isVolatileQualified()); |
Eli Friedman | 316bb1b | 2008-05-17 20:03:47 +0000 | [diff] [blame] | 476 | |
Daniel Dunbar | 10e3ded | 2008-08-06 05:08:45 +0000 | [diff] [blame] | 477 | // If the low part doesn't cover the bitfield emit a high part. |
| 478 | if (LowBits < BitfieldSize) { |
| 479 | unsigned HighBits = BitfieldSize - LowBits; |
| 480 | llvm::Value *HighPtr = |
| 481 | Builder.CreateGEP(Ptr, llvm::ConstantInt::get(llvm::Type::Int32Ty, 1), |
| 482 | "bf.ptr.hi"); |
| 483 | llvm::Value *HighVal = Builder.CreateLoad(HighPtr, |
| 484 | Dst.isVolatileQualified(), |
| 485 | "bf.prev.hi"); |
| 486 | |
| 487 | // Compute the mask for zero-ing the high part of this bitfield. |
| 488 | llvm::Constant *InvMask = |
| 489 | llvm::ConstantInt::get(~llvm::APInt::getLowBitsSet(EltTySize, HighBits)); |
| 490 | |
| 491 | // Compute the new high part as |
| 492 | // HighVal = (HighVal & InvMask) | (NewVal lshr LowBits), |
| 493 | // where the high bits of NewVal have already been cleared and the |
| 494 | // shift stripping the low bits. |
| 495 | llvm::Value *NewHighVal = |
| 496 | Builder.CreateLShr(NewVal, llvm::ConstantInt::get(EltTy, LowBits), |
| 497 | "bf.value.high"); |
| 498 | HighVal = Builder.CreateAnd(HighVal, InvMask, "bf.prev.hi.cleared"); |
| 499 | HighVal = Builder.CreateOr(HighVal, NewHighVal, "bf.new.hi"); |
| 500 | |
| 501 | // Write back. |
| 502 | Builder.CreateStore(HighVal, HighPtr, Dst.isVolatileQualified()); |
| 503 | } |
Lauro Ramos Venancio | a0c5d0e | 2008-01-22 22:36:45 +0000 | [diff] [blame] | 504 | } |
| 505 | |
Daniel Dunbar | 85c59ed | 2008-08-29 08:11:39 +0000 | [diff] [blame] | 506 | void CodeGenFunction::EmitStoreThroughPropertyRefLValue(RValue Src, |
| 507 | LValue Dst, |
| 508 | QualType Ty) { |
| 509 | EmitObjCPropertySet(Dst.getPropertyRefExpr(), Src); |
| 510 | } |
| 511 | |
Fariborz Jahanian | 43f4470 | 2008-11-22 22:30:21 +0000 | [diff] [blame] | 512 | void CodeGenFunction::EmitStoreThroughKVCRefLValue(RValue Src, |
| 513 | LValue Dst, |
| 514 | QualType Ty) { |
| 515 | EmitObjCPropertySet(Dst.getKVCRefExpr(), Src); |
| 516 | } |
| 517 | |
Nate Begeman | 213541a | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 518 | void CodeGenFunction::EmitStoreThroughExtVectorComponentLValue(RValue Src, |
| 519 | LValue Dst, |
| 520 | QualType Ty) { |
Chris Lattner | 017d6aa | 2007-08-03 16:28:33 +0000 | [diff] [blame] | 521 | // This access turns into a read/modify/write of the vector. Load the input |
| 522 | // value now. |
Eli Friedman | 1e692ac | 2008-06-13 23:01:12 +0000 | [diff] [blame] | 523 | llvm::Value *Vec = Builder.CreateLoad(Dst.getExtVectorAddr(), |
| 524 | Dst.isVolatileQualified(), "tmp"); |
Nate Begeman | 8a99764 | 2008-05-09 06:41:27 +0000 | [diff] [blame] | 525 | const llvm::Constant *Elts = Dst.getExtVectorElts(); |
Chris Lattner | 017d6aa | 2007-08-03 16:28:33 +0000 | [diff] [blame] | 526 | |
Chris Lattner | 9b65551 | 2007-08-31 22:49:20 +0000 | [diff] [blame] | 527 | llvm::Value *SrcVal = Src.getScalarVal(); |
Chris Lattner | 017d6aa | 2007-08-03 16:28:33 +0000 | [diff] [blame] | 528 | |
Chris Lattner | 7e6b51b | 2007-08-03 16:37:04 +0000 | [diff] [blame] | 529 | if (const VectorType *VTy = Ty->getAsVectorType()) { |
| 530 | unsigned NumSrcElts = VTy->getNumElements(); |
Nate Begeman | 6fe7c8a | 2009-01-18 06:42:49 +0000 | [diff] [blame] | 531 | unsigned NumDstElts = |
| 532 | cast<llvm::VectorType>(Vec->getType())->getNumElements(); |
| 533 | if (NumDstElts == NumSrcElts) { |
| 534 | // Use shuffle vector is the src and destination are the same number |
| 535 | // of elements |
| 536 | llvm::SmallVector<llvm::Constant*, 4> Mask; |
| 537 | for (unsigned i = 0; i != NumSrcElts; ++i) { |
| 538 | unsigned InIdx = getAccessedFieldNo(i, Elts); |
| 539 | Mask.push_back(llvm::ConstantInt::get(llvm::Type::Int32Ty, InIdx)); |
| 540 | } |
| 541 | |
| 542 | llvm::Value *MaskV = llvm::ConstantVector::get(&Mask[0], Mask.size()); |
| 543 | Vec = Builder.CreateShuffleVector(SrcVal, |
| 544 | llvm::UndefValue::get(Vec->getType()), |
| 545 | MaskV, "tmp"); |
| 546 | } |
| 547 | else if (NumDstElts > NumSrcElts) { |
| 548 | // Extended the source vector to the same length and then shuffle it |
| 549 | // into the destination. |
| 550 | // FIXME: since we're shuffling with undef, can we just use the indices |
| 551 | // into that? This could be simpler. |
| 552 | llvm::SmallVector<llvm::Constant*, 4> ExtMask; |
| 553 | unsigned i; |
| 554 | for (i = 0; i != NumSrcElts; ++i) |
| 555 | ExtMask.push_back(llvm::ConstantInt::get(llvm::Type::Int32Ty, i)); |
| 556 | for (; i != NumDstElts; ++i) |
| 557 | ExtMask.push_back(llvm::UndefValue::get(llvm::Type::Int32Ty)); |
| 558 | llvm::Value *ExtMaskV = llvm::ConstantVector::get(&ExtMask[0], |
| 559 | ExtMask.size()); |
| 560 | llvm::Value *ExtSrcVal = Builder.CreateShuffleVector(SrcVal, |
| 561 | llvm::UndefValue::get(SrcVal->getType()), |
| 562 | ExtMaskV, "tmp"); |
| 563 | // build identity |
| 564 | llvm::SmallVector<llvm::Constant*, 4> Mask; |
| 565 | for (unsigned i = 0; i != NumDstElts; ++i) { |
| 566 | Mask.push_back(llvm::ConstantInt::get(llvm::Type::Int32Ty, i)); |
| 567 | } |
| 568 | // modify when what gets shuffled in |
| 569 | for (unsigned i = 0; i != NumSrcElts; ++i) { |
| 570 | unsigned Idx = getAccessedFieldNo(i, Elts); |
| 571 | Mask[Idx] =llvm::ConstantInt::get(llvm::Type::Int32Ty, i+NumDstElts); |
| 572 | } |
| 573 | llvm::Value *MaskV = llvm::ConstantVector::get(&Mask[0], Mask.size()); |
| 574 | Vec = Builder.CreateShuffleVector(Vec, ExtSrcVal, MaskV, "tmp"); |
| 575 | } |
| 576 | else { |
| 577 | // We should never shorten the vector |
| 578 | assert(0 && "unexpected shorten vector length"); |
Chris Lattner | 7e6b51b | 2007-08-03 16:37:04 +0000 | [diff] [blame] | 579 | } |
| 580 | } else { |
| 581 | // If the Src is a scalar (not a vector) it must be updating one element. |
Dan Gohman | 4f8d123 | 2008-05-22 00:50:06 +0000 | [diff] [blame] | 582 | unsigned InIdx = getAccessedFieldNo(0, Elts); |
Chris Lattner | 017d6aa | 2007-08-03 16:28:33 +0000 | [diff] [blame] | 583 | llvm::Value *Elt = llvm::ConstantInt::get(llvm::Type::Int32Ty, InIdx); |
| 584 | Vec = Builder.CreateInsertElement(Vec, SrcVal, Elt, "tmp"); |
Chris Lattner | 017d6aa | 2007-08-03 16:28:33 +0000 | [diff] [blame] | 585 | } |
| 586 | |
Eli Friedman | 1e692ac | 2008-06-13 23:01:12 +0000 | [diff] [blame] | 587 | Builder.CreateStore(Vec, Dst.getExtVectorAddr(), Dst.isVolatileQualified()); |
Chris Lattner | 017d6aa | 2007-08-03 16:28:33 +0000 | [diff] [blame] | 588 | } |
| 589 | |
Fariborz Jahanian | 2682d8b | 2008-11-20 00:15:42 +0000 | [diff] [blame] | 590 | /// SetVarDeclObjCAttribute - Set __weak/__strong attributes into the LValue |
| 591 | /// object. |
Fariborz Jahanian | d1cc804 | 2008-11-20 20:53:20 +0000 | [diff] [blame] | 592 | static void SetVarDeclObjCAttribute(ASTContext &Ctx, const Decl *VD, |
Fariborz Jahanian | 80b0b42 | 2008-11-20 18:10:58 +0000 | [diff] [blame] | 593 | const QualType &Ty, LValue &LV) |
Fariborz Jahanian | 2682d8b | 2008-11-20 00:15:42 +0000 | [diff] [blame] | 594 | { |
| 595 | if (const ObjCGCAttr *A = VD->getAttr<ObjCGCAttr>()) { |
| 596 | ObjCGCAttr::GCAttrTypes attrType = A->getType(); |
| 597 | LValue::SetObjCType(attrType == ObjCGCAttr::Weak, |
| 598 | attrType == ObjCGCAttr::Strong, LV); |
| 599 | } |
Fariborz Jahanian | 80b0b42 | 2008-11-20 18:10:58 +0000 | [diff] [blame] | 600 | else if (Ctx.getLangOptions().ObjC1 && |
| 601 | Ctx.getLangOptions().getGCMode() != LangOptions::NonGC) { |
| 602 | // Default behavious under objective-c's gc is for objective-c pointers |
| 603 | // be treated as though they were declared as __strong. |
| 604 | if (Ctx.isObjCObjectPointerType(Ty)) |
Fariborz Jahanian | 2682d8b | 2008-11-20 00:15:42 +0000 | [diff] [blame] | 605 | LValue::SetObjCType(false, true, LV); |
| 606 | } |
| 607 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 608 | |
| 609 | LValue CodeGenFunction::EmitDeclRefLValue(const DeclRefExpr *E) { |
Steve Naroff | 248a753 | 2008-04-15 22:42:06 +0000 | [diff] [blame] | 610 | const VarDecl *VD = dyn_cast<VarDecl>(E->getDecl()); |
| 611 | |
Chris Lattner | 4111024 | 2008-06-17 18:05:57 +0000 | [diff] [blame] | 612 | if (VD && (VD->isBlockVarDecl() || isa<ParmVarDecl>(VD) || |
| 613 | isa<ImplicitParamDecl>(VD))) { |
Fariborz Jahanian | 2682d8b | 2008-11-20 00:15:42 +0000 | [diff] [blame] | 614 | LValue LV; |
| 615 | if (VD->getStorageClass() == VarDecl::Extern) { |
| 616 | LV = LValue::MakeAddr(CGM.GetAddrOfGlobalVar(VD), |
| 617 | E->getType().getCVRQualifiers()); |
| 618 | } |
Lauro Ramos Venancio | fea90b8 | 2008-02-16 22:30:38 +0000 | [diff] [blame] | 619 | else { |
Steve Naroff | 248a753 | 2008-04-15 22:42:06 +0000 | [diff] [blame] | 620 | llvm::Value *V = LocalDeclMap[VD]; |
Lauro Ramos Venancio | fea90b8 | 2008-02-16 22:30:38 +0000 | [diff] [blame] | 621 | assert(V && "BlockVarDecl not entered in LocalDeclMap?"); |
Fariborz Jahanian | 2682d8b | 2008-11-20 00:15:42 +0000 | [diff] [blame] | 622 | LV = LValue::MakeAddr(V, E->getType().getCVRQualifiers()); |
Lauro Ramos Venancio | fea90b8 | 2008-02-16 22:30:38 +0000 | [diff] [blame] | 623 | } |
Fariborz Jahanian | 2682d8b | 2008-11-20 00:15:42 +0000 | [diff] [blame] | 624 | if (VD->isBlockVarDecl() && |
| 625 | (VD->getStorageClass() == VarDecl::Static || |
| 626 | VD->getStorageClass() == VarDecl::Extern)) |
Fariborz Jahanian | 80b0b42 | 2008-11-20 18:10:58 +0000 | [diff] [blame] | 627 | SetVarDeclObjCAttribute(getContext(), VD, E->getType(), LV); |
Fariborz Jahanian | 2682d8b | 2008-11-20 00:15:42 +0000 | [diff] [blame] | 628 | return LV; |
Steve Naroff | 248a753 | 2008-04-15 22:42:06 +0000 | [diff] [blame] | 629 | } else if (VD && VD->isFileVarDecl()) { |
Fariborz Jahanian | 6d657c4 | 2008-11-18 20:18:11 +0000 | [diff] [blame] | 630 | LValue LV = LValue::MakeAddr(CGM.GetAddrOfGlobalVar(VD), |
| 631 | E->getType().getCVRQualifiers()); |
Fariborz Jahanian | 80b0b42 | 2008-11-20 18:10:58 +0000 | [diff] [blame] | 632 | SetVarDeclObjCAttribute(getContext(), VD, E->getType(), LV); |
Fariborz Jahanian | 6d657c4 | 2008-11-18 20:18:11 +0000 | [diff] [blame] | 633 | return LV; |
Steve Naroff | 248a753 | 2008-04-15 22:42:06 +0000 | [diff] [blame] | 634 | } else if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(E->getDecl())) { |
Daniel Dunbar | bd012ff | 2008-07-29 23:18:29 +0000 | [diff] [blame] | 635 | return LValue::MakeAddr(CGM.GetAddrOfFunction(FD), |
Eli Friedman | 1e692ac | 2008-06-13 23:01:12 +0000 | [diff] [blame] | 636 | E->getType().getCVRQualifiers()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 637 | } |
Chris Lattner | 4111024 | 2008-06-17 18:05:57 +0000 | [diff] [blame] | 638 | else if (const ImplicitParamDecl *IPD = |
| 639 | dyn_cast<ImplicitParamDecl>(E->getDecl())) { |
| 640 | llvm::Value *V = LocalDeclMap[IPD]; |
| 641 | assert(V && "BlockVarDecl not entered in LocalDeclMap?"); |
| 642 | return LValue::MakeAddr(V, E->getType().getCVRQualifiers()); |
| 643 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 644 | assert(0 && "Unimp declref"); |
Chris Lattner | b1776cb | 2007-09-16 19:23:47 +0000 | [diff] [blame] | 645 | //an invalid LValue, but the assert will |
| 646 | //ensure that this point is never reached. |
| 647 | return LValue(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 648 | } |
| 649 | |
| 650 | LValue CodeGenFunction::EmitUnaryOpLValue(const UnaryOperator *E) { |
| 651 | // __extension__ doesn't affect lvalue-ness. |
| 652 | if (E->getOpcode() == UnaryOperator::Extension) |
| 653 | return EmitLValue(E->getSubExpr()); |
| 654 | |
Chris Lattner | 9619662 | 2008-07-26 22:37:01 +0000 | [diff] [blame] | 655 | QualType ExprTy = getContext().getCanonicalType(E->getSubExpr()->getType()); |
Chris Lattner | 7da36f6 | 2007-10-30 22:53:42 +0000 | [diff] [blame] | 656 | switch (E->getOpcode()) { |
| 657 | default: assert(0 && "Unknown unary operator lvalue!"); |
| 658 | case UnaryOperator::Deref: |
Eli Friedman | 1e692ac | 2008-06-13 23:01:12 +0000 | [diff] [blame] | 659 | return LValue::MakeAddr(EmitScalarExpr(E->getSubExpr()), |
Chris Lattner | b77792e | 2008-07-26 22:17:49 +0000 | [diff] [blame] | 660 | ExprTy->getAsPointerType()->getPointeeType() |
| 661 | .getCVRQualifiers()); |
Chris Lattner | 7da36f6 | 2007-10-30 22:53:42 +0000 | [diff] [blame] | 662 | case UnaryOperator::Real: |
| 663 | case UnaryOperator::Imag: |
| 664 | LValue LV = EmitLValue(E->getSubExpr()); |
Chris Lattner | 36b6a0a | 2008-03-19 05:19:41 +0000 | [diff] [blame] | 665 | unsigned Idx = E->getOpcode() == UnaryOperator::Imag; |
| 666 | return LValue::MakeAddr(Builder.CreateStructGEP(LV.getAddress(), |
Chris Lattner | b77792e | 2008-07-26 22:17:49 +0000 | [diff] [blame] | 667 | Idx, "idx"), |
| 668 | ExprTy.getCVRQualifiers()); |
Chris Lattner | 7da36f6 | 2007-10-30 22:53:42 +0000 | [diff] [blame] | 669 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 670 | } |
| 671 | |
| 672 | LValue CodeGenFunction::EmitStringLiteralLValue(const StringLiteral *E) { |
Daniel Dunbar | 6143293 | 2008-08-13 23:20:05 +0000 | [diff] [blame] | 673 | return LValue::MakeAddr(CGM.GetAddrOfConstantStringFromLiteral(E), 0); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 674 | } |
| 675 | |
Daniel Dunbar | 662b71e | 2008-10-17 21:58:32 +0000 | [diff] [blame] | 676 | LValue CodeGenFunction::EmitPredefinedFunctionName(unsigned Type) { |
Anders Carlsson | 2274266 | 2007-07-21 05:21:51 +0000 | [diff] [blame] | 677 | std::string GlobalVarName; |
Daniel Dunbar | 662b71e | 2008-10-17 21:58:32 +0000 | [diff] [blame] | 678 | |
| 679 | switch (Type) { |
Anders Carlsson | 2274266 | 2007-07-21 05:21:51 +0000 | [diff] [blame] | 680 | default: |
Daniel Dunbar | 662b71e | 2008-10-17 21:58:32 +0000 | [diff] [blame] | 681 | assert(0 && "Invalid type"); |
Chris Lattner | d9f6910 | 2008-08-10 01:53:14 +0000 | [diff] [blame] | 682 | case PredefinedExpr::Func: |
Anders Carlsson | 2274266 | 2007-07-21 05:21:51 +0000 | [diff] [blame] | 683 | GlobalVarName = "__func__."; |
| 684 | break; |
Chris Lattner | d9f6910 | 2008-08-10 01:53:14 +0000 | [diff] [blame] | 685 | case PredefinedExpr::Function: |
Anders Carlsson | 2274266 | 2007-07-21 05:21:51 +0000 | [diff] [blame] | 686 | GlobalVarName = "__FUNCTION__."; |
| 687 | break; |
Chris Lattner | d9f6910 | 2008-08-10 01:53:14 +0000 | [diff] [blame] | 688 | case PredefinedExpr::PrettyFunction: |
Anders Carlsson | 2274266 | 2007-07-21 05:21:51 +0000 | [diff] [blame] | 689 | // FIXME:: Demangle C++ method names |
| 690 | GlobalVarName = "__PRETTY_FUNCTION__."; |
| 691 | break; |
| 692 | } |
Daniel Dunbar | 662b71e | 2008-10-17 21:58:32 +0000 | [diff] [blame] | 693 | |
| 694 | std::string FunctionName; |
| 695 | if(const FunctionDecl *FD = dyn_cast<FunctionDecl>(CurFuncDecl)) { |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 696 | FunctionName = FD->getNameAsString(); |
Daniel Dunbar | 662b71e | 2008-10-17 21:58:32 +0000 | [diff] [blame] | 697 | } else { |
| 698 | // Just get the mangled name. |
| 699 | FunctionName = CurFn->getName(); |
| 700 | } |
| 701 | |
Chris Lattner | c8aa5f1 | 2008-04-04 04:07:35 +0000 | [diff] [blame] | 702 | GlobalVarName += FunctionName; |
Daniel Dunbar | 662b71e | 2008-10-17 21:58:32 +0000 | [diff] [blame] | 703 | llvm::Constant *C = |
| 704 | CGM.GetAddrOfConstantCString(FunctionName, GlobalVarName.c_str()); |
| 705 | return LValue::MakeAddr(C, 0); |
| 706 | } |
| 707 | |
| 708 | LValue CodeGenFunction::EmitPredefinedLValue(const PredefinedExpr *E) { |
| 709 | switch (E->getIdentType()) { |
| 710 | default: |
| 711 | return EmitUnsupportedLValue(E, "predefined expression"); |
| 712 | case PredefinedExpr::Func: |
| 713 | case PredefinedExpr::Function: |
| 714 | case PredefinedExpr::PrettyFunction: |
| 715 | return EmitPredefinedFunctionName(E->getIdentType()); |
| 716 | } |
Anders Carlsson | 2274266 | 2007-07-21 05:21:51 +0000 | [diff] [blame] | 717 | } |
| 718 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 719 | LValue CodeGenFunction::EmitArraySubscriptExpr(const ArraySubscriptExpr *E) { |
Ted Kremenek | 2324512 | 2007-08-20 16:18:38 +0000 | [diff] [blame] | 720 | // The index must always be an integer, which is not an aggregate. Emit it. |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 721 | llvm::Value *Idx = EmitScalarExpr(E->getIdx()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 722 | |
| 723 | // If the base is a vector type, then we are forming a vector element lvalue |
| 724 | // with this subscript. |
Eli Friedman | 1e692ac | 2008-06-13 23:01:12 +0000 | [diff] [blame] | 725 | if (E->getBase()->getType()->isVectorType()) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 726 | // Emit the vector as an lvalue to get its address. |
Eli Friedman | 1e692ac | 2008-06-13 23:01:12 +0000 | [diff] [blame] | 727 | LValue LHS = EmitLValue(E->getBase()); |
Ted Kremenek | 2324512 | 2007-08-20 16:18:38 +0000 | [diff] [blame] | 728 | assert(LHS.isSimple() && "Can only subscript lvalue vectors here!"); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 729 | // FIXME: This should properly sign/zero/extend or truncate Idx to i32. |
Eli Friedman | 1e692ac | 2008-06-13 23:01:12 +0000 | [diff] [blame] | 730 | return LValue::MakeVectorElt(LHS.getAddress(), Idx, |
| 731 | E->getBase()->getType().getCVRQualifiers()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 732 | } |
| 733 | |
Ted Kremenek | 2324512 | 2007-08-20 16:18:38 +0000 | [diff] [blame] | 734 | // The base must be a pointer, which is not an aggregate. Emit it. |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 735 | llvm::Value *Base = EmitScalarExpr(E->getBase()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 736 | |
Ted Kremenek | 2324512 | 2007-08-20 16:18:38 +0000 | [diff] [blame] | 737 | // Extend or truncate the index type to 32 or 64-bits. |
Chris Lattner | d4f0802 | 2007-08-08 17:43:05 +0000 | [diff] [blame] | 738 | QualType IdxTy = E->getIdx()->getType(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 739 | bool IdxSigned = IdxTy->isSignedIntegerType(); |
| 740 | unsigned IdxBitwidth = cast<llvm::IntegerType>(Idx->getType())->getBitWidth(); |
| 741 | if (IdxBitwidth != LLVMPointerWidth) |
| 742 | Idx = Builder.CreateIntCast(Idx, llvm::IntegerType::get(LLVMPointerWidth), |
| 743 | IdxSigned, "idxprom"); |
| 744 | |
| 745 | // We know that the pointer points to a type of the correct size, unless the |
| 746 | // size is a VLA. |
Anders Carlsson | 8b33c08 | 2008-12-21 00:11:23 +0000 | [diff] [blame] | 747 | if (const VariableArrayType *VAT = |
| 748 | getContext().getAsVariableArrayType(E->getType())) { |
| 749 | llvm::Value *VLASize = VLASizeMap[VAT]; |
| 750 | |
| 751 | Idx = Builder.CreateMul(Idx, VLASize); |
| 752 | |
Anders Carlsson | 6183a99 | 2008-12-21 03:44:36 +0000 | [diff] [blame] | 753 | QualType BaseType = getContext().getBaseElementType(VAT); |
Anders Carlsson | 8b33c08 | 2008-12-21 00:11:23 +0000 | [diff] [blame] | 754 | |
| 755 | uint64_t BaseTypeSize = getContext().getTypeSize(BaseType) / 8; |
| 756 | Idx = Builder.CreateUDiv(Idx, |
| 757 | llvm::ConstantInt::get(Idx->getType(), |
| 758 | BaseTypeSize)); |
| 759 | } |
| 760 | |
Chris Lattner | 9619662 | 2008-07-26 22:37:01 +0000 | [diff] [blame] | 761 | QualType ExprTy = getContext().getCanonicalType(E->getBase()->getType()); |
Chris Lattner | b77792e | 2008-07-26 22:17:49 +0000 | [diff] [blame] | 762 | |
Eli Friedman | 1e692ac | 2008-06-13 23:01:12 +0000 | [diff] [blame] | 763 | return LValue::MakeAddr(Builder.CreateGEP(Base, Idx, "arrayidx"), |
Chris Lattner | b77792e | 2008-07-26 22:17:49 +0000 | [diff] [blame] | 764 | ExprTy->getAsPointerType()->getPointeeType() |
| 765 | .getCVRQualifiers()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 766 | } |
| 767 | |
Nate Begeman | 3b8d116 | 2008-05-13 21:03:02 +0000 | [diff] [blame] | 768 | static |
| 769 | llvm::Constant *GenerateConstantVector(llvm::SmallVector<unsigned, 4> &Elts) { |
| 770 | llvm::SmallVector<llvm::Constant *, 4> CElts; |
| 771 | |
| 772 | for (unsigned i = 0, e = Elts.size(); i != e; ++i) |
| 773 | CElts.push_back(llvm::ConstantInt::get(llvm::Type::Int32Ty, Elts[i])); |
| 774 | |
| 775 | return llvm::ConstantVector::get(&CElts[0], CElts.size()); |
| 776 | } |
| 777 | |
Chris Lattner | 349aaec | 2007-08-02 23:37:31 +0000 | [diff] [blame] | 778 | LValue CodeGenFunction:: |
Nate Begeman | 213541a | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 779 | EmitExtVectorElementExpr(const ExtVectorElementExpr *E) { |
Chris Lattner | 349aaec | 2007-08-02 23:37:31 +0000 | [diff] [blame] | 780 | // Emit the base vector as an l-value. |
| 781 | LValue Base = EmitLValue(E->getBase()); |
Chris Lattner | 349aaec | 2007-08-02 23:37:31 +0000 | [diff] [blame] | 782 | |
Nate Begeman | 3b8d116 | 2008-05-13 21:03:02 +0000 | [diff] [blame] | 783 | // Encode the element access list into a vector of unsigned indices. |
| 784 | llvm::SmallVector<unsigned, 4> Indices; |
| 785 | E->getEncodedElementAccess(Indices); |
| 786 | |
| 787 | if (Base.isSimple()) { |
| 788 | llvm::Constant *CV = GenerateConstantVector(Indices); |
Eli Friedman | 1e692ac | 2008-06-13 23:01:12 +0000 | [diff] [blame] | 789 | return LValue::MakeExtVectorElt(Base.getAddress(), CV, |
| 790 | E->getBase()->getType().getCVRQualifiers()); |
Nate Begeman | 3b8d116 | 2008-05-13 21:03:02 +0000 | [diff] [blame] | 791 | } |
| 792 | assert(Base.isExtVectorElt() && "Can only subscript lvalue vec elts here!"); |
| 793 | |
| 794 | llvm::Constant *BaseElts = Base.getExtVectorElts(); |
| 795 | llvm::SmallVector<llvm::Constant *, 4> CElts; |
| 796 | |
| 797 | for (unsigned i = 0, e = Indices.size(); i != e; ++i) { |
| 798 | if (isa<llvm::ConstantAggregateZero>(BaseElts)) |
| 799 | CElts.push_back(llvm::ConstantInt::get(llvm::Type::Int32Ty, 0)); |
| 800 | else |
| 801 | CElts.push_back(BaseElts->getOperand(Indices[i])); |
| 802 | } |
| 803 | llvm::Constant *CV = llvm::ConstantVector::get(&CElts[0], CElts.size()); |
Eli Friedman | 1e692ac | 2008-06-13 23:01:12 +0000 | [diff] [blame] | 804 | return LValue::MakeExtVectorElt(Base.getExtVectorAddr(), CV, |
| 805 | E->getBase()->getType().getCVRQualifiers()); |
Chris Lattner | 349aaec | 2007-08-02 23:37:31 +0000 | [diff] [blame] | 806 | } |
| 807 | |
Devang Patel | b9b00ad | 2007-10-23 20:28:39 +0000 | [diff] [blame] | 808 | LValue CodeGenFunction::EmitMemberExpr(const MemberExpr *E) { |
Devang Patel | fe2419a | 2007-12-11 21:33:16 +0000 | [diff] [blame] | 809 | bool isUnion = false; |
Fariborz Jahanian | 2ab1968 | 2008-11-21 18:14:01 +0000 | [diff] [blame] | 810 | bool isIvar = false; |
Devang Patel | 126a856 | 2007-10-24 22:26:28 +0000 | [diff] [blame] | 811 | Expr *BaseExpr = E->getBase(); |
Devang Patel | 126a856 | 2007-10-24 22:26:28 +0000 | [diff] [blame] | 812 | llvm::Value *BaseValue = NULL; |
Eli Friedman | 1e692ac | 2008-06-13 23:01:12 +0000 | [diff] [blame] | 813 | unsigned CVRQualifiers=0; |
| 814 | |
Chris Lattner | 12f65f6 | 2007-12-02 18:52:07 +0000 | [diff] [blame] | 815 | // If this is s.x, emit s as an lvalue. If it is s->x, emit s as a scalar. |
Devang Patel | fe2419a | 2007-12-11 21:33:16 +0000 | [diff] [blame] | 816 | if (E->isArrow()) { |
Devang Patel | 0a96118 | 2007-10-26 18:15:21 +0000 | [diff] [blame] | 817 | BaseValue = EmitScalarExpr(BaseExpr); |
Devang Patel | fe2419a | 2007-12-11 21:33:16 +0000 | [diff] [blame] | 818 | const PointerType *PTy = |
Chris Lattner | 9619662 | 2008-07-26 22:37:01 +0000 | [diff] [blame] | 819 | cast<PointerType>(getContext().getCanonicalType(BaseExpr->getType())); |
Devang Patel | fe2419a | 2007-12-11 21:33:16 +0000 | [diff] [blame] | 820 | if (PTy->getPointeeType()->isUnionType()) |
| 821 | isUnion = true; |
Eli Friedman | 1e692ac | 2008-06-13 23:01:12 +0000 | [diff] [blame] | 822 | CVRQualifiers = PTy->getPointeeType().getCVRQualifiers(); |
Devang Patel | fe2419a | 2007-12-11 21:33:16 +0000 | [diff] [blame] | 823 | } |
Fariborz Jahanian | 35c3329 | 2009-01-12 23:27:26 +0000 | [diff] [blame] | 824 | else if (BaseExpr->getStmtClass() == Expr::ObjCPropertyRefExprClass || |
| 825 | BaseExpr->getStmtClass() == Expr::ObjCKVCRefExprClass) { |
| 826 | RValue RV = EmitObjCPropertyGet(BaseExpr); |
| 827 | BaseValue = RV.getAggregateAddr(); |
| 828 | if (BaseExpr->getType()->isUnionType()) |
| 829 | isUnion = true; |
| 830 | CVRQualifiers = BaseExpr->getType().getCVRQualifiers(); |
| 831 | } |
Chris Lattner | 12f65f6 | 2007-12-02 18:52:07 +0000 | [diff] [blame] | 832 | else { |
| 833 | LValue BaseLV = EmitLValue(BaseExpr); |
Fariborz Jahanian | 2ab1968 | 2008-11-21 18:14:01 +0000 | [diff] [blame] | 834 | if (BaseLV.isObjCIvar()) |
| 835 | isIvar = true; |
Chris Lattner | 12f65f6 | 2007-12-02 18:52:07 +0000 | [diff] [blame] | 836 | // FIXME: this isn't right for bitfields. |
| 837 | BaseValue = BaseLV.getAddress(); |
Devang Patel | fe2419a | 2007-12-11 21:33:16 +0000 | [diff] [blame] | 838 | if (BaseExpr->getType()->isUnionType()) |
| 839 | isUnion = true; |
Eli Friedman | 1e692ac | 2008-06-13 23:01:12 +0000 | [diff] [blame] | 840 | CVRQualifiers = BaseExpr->getType().getCVRQualifiers(); |
Chris Lattner | 12f65f6 | 2007-12-02 18:52:07 +0000 | [diff] [blame] | 841 | } |
Devang Patel | b9b00ad | 2007-10-23 20:28:39 +0000 | [diff] [blame] | 842 | |
Douglas Gregor | 86f1940 | 2008-12-20 23:49:58 +0000 | [diff] [blame] | 843 | FieldDecl *Field = dyn_cast<FieldDecl>(E->getMemberDecl()); |
| 844 | // FIXME: Handle non-field member expressions |
| 845 | assert(Field && "No code generation for non-field member references"); |
Fariborz Jahanian | 2ab1968 | 2008-11-21 18:14:01 +0000 | [diff] [blame] | 846 | LValue MemExpLV = EmitLValueForField(BaseValue, Field, isUnion, CVRQualifiers); |
| 847 | LValue::SetObjCIvar(MemExpLV, isIvar); |
| 848 | return MemExpLV; |
Eli Friedman | 472778e | 2008-02-09 08:50:58 +0000 | [diff] [blame] | 849 | } |
Devang Patel | b9b00ad | 2007-10-23 20:28:39 +0000 | [diff] [blame] | 850 | |
Fariborz Jahanian | fd64bb6 | 2008-12-15 20:35:07 +0000 | [diff] [blame] | 851 | LValue CodeGenFunction::EmitLValueForBitfield(llvm::Value* BaseValue, |
| 852 | FieldDecl* Field, |
Fariborz Jahanian | 598d3f6 | 2009-02-03 19:03:09 +0000 | [diff] [blame^] | 853 | unsigned CVRQualifiers) { |
| 854 | unsigned idx = CGM.getTypes().getLLVMFieldNo(Field); |
Fariborz Jahanian | fd64bb6 | 2008-12-15 20:35:07 +0000 | [diff] [blame] | 855 | // FIXME: CodeGenTypes should expose a method to get the appropriate |
| 856 | // type for FieldTy (the appropriate type is ABI-dependent). |
| 857 | const llvm::Type *FieldTy = CGM.getTypes().ConvertTypeForMem(Field->getType()); |
| 858 | const llvm::PointerType *BaseTy = |
| 859 | cast<llvm::PointerType>(BaseValue->getType()); |
| 860 | unsigned AS = BaseTy->getAddressSpace(); |
| 861 | BaseValue = Builder.CreateBitCast(BaseValue, |
| 862 | llvm::PointerType::get(FieldTy, AS), |
| 863 | "tmp"); |
| 864 | llvm::Value *V = Builder.CreateGEP(BaseValue, |
| 865 | llvm::ConstantInt::get(llvm::Type::Int32Ty, idx), |
| 866 | "tmp"); |
| 867 | |
| 868 | CodeGenTypes::BitFieldInfo bitFieldInfo = |
| 869 | CGM.getTypes().getBitFieldInfo(Field); |
| 870 | return LValue::MakeBitfield(V, bitFieldInfo.Begin, bitFieldInfo.Size, |
| 871 | Field->getType()->isSignedIntegerType(), |
| 872 | Field->getType().getCVRQualifiers()|CVRQualifiers); |
| 873 | } |
| 874 | |
Eli Friedman | 472778e | 2008-02-09 08:50:58 +0000 | [diff] [blame] | 875 | LValue CodeGenFunction::EmitLValueForField(llvm::Value* BaseValue, |
| 876 | FieldDecl* Field, |
Eli Friedman | 1e692ac | 2008-06-13 23:01:12 +0000 | [diff] [blame] | 877 | bool isUnion, |
| 878 | unsigned CVRQualifiers) |
Eli Friedman | 472778e | 2008-02-09 08:50:58 +0000 | [diff] [blame] | 879 | { |
Fariborz Jahanian | fd64bb6 | 2008-12-15 20:35:07 +0000 | [diff] [blame] | 880 | if (Field->isBitField()) |
Fariborz Jahanian | 598d3f6 | 2009-02-03 19:03:09 +0000 | [diff] [blame^] | 881 | return EmitLValueForBitfield(BaseValue, Field, CVRQualifiers); |
Fariborz Jahanian | 2682d8b | 2008-11-20 00:15:42 +0000 | [diff] [blame] | 882 | |
Fariborz Jahanian | 598d3f6 | 2009-02-03 19:03:09 +0000 | [diff] [blame^] | 883 | unsigned idx = CGM.getTypes().getLLVMFieldNo(Field); |
Fariborz Jahanian | fd64bb6 | 2008-12-15 20:35:07 +0000 | [diff] [blame] | 884 | llvm::Value *V = Builder.CreateStructGEP(BaseValue, idx, "tmp"); |
Eli Friedman | 1e86b34 | 2008-05-29 11:33:25 +0000 | [diff] [blame] | 885 | |
Devang Patel | abad06c | 2007-10-26 19:42:18 +0000 | [diff] [blame] | 886 | // Match union field type. |
Lauro Ramos Venancio | d957aa0 | 2008-02-07 19:29:53 +0000 | [diff] [blame] | 887 | if (isUnion) { |
Eli Friedman | 1e692ac | 2008-06-13 23:01:12 +0000 | [diff] [blame] | 888 | const llvm::Type *FieldTy = |
| 889 | CGM.getTypes().ConvertTypeForMem(Field->getType()); |
Devang Patel | e9b8c0a | 2007-10-30 20:59:40 +0000 | [diff] [blame] | 890 | const llvm::PointerType * BaseTy = |
| 891 | cast<llvm::PointerType>(BaseValue->getType()); |
Eli Friedman | 788d571 | 2008-05-21 13:24:44 +0000 | [diff] [blame] | 892 | unsigned AS = BaseTy->getAddressSpace(); |
| 893 | V = Builder.CreateBitCast(V, |
| 894 | llvm::PointerType::get(FieldTy, AS), |
| 895 | "tmp"); |
Devang Patel | abad06c | 2007-10-26 19:42:18 +0000 | [diff] [blame] | 896 | } |
Lauro Ramos Venancio | 3b8c22d | 2008-01-22 20:17:04 +0000 | [diff] [blame] | 897 | |
Fariborz Jahanian | 2682d8b | 2008-11-20 00:15:42 +0000 | [diff] [blame] | 898 | LValue LV = |
| 899 | LValue::MakeAddr(V, |
| 900 | Field->getType().getCVRQualifiers()|CVRQualifiers); |
| 901 | if (const ObjCGCAttr *A = Field->getAttr<ObjCGCAttr>()) { |
| 902 | ObjCGCAttr::GCAttrTypes attrType = A->getType(); |
| 903 | // __weak attribute on a field is ignored. |
| 904 | LValue::SetObjCType(false, attrType == ObjCGCAttr::Strong, LV); |
| 905 | } |
| 906 | else if (CGM.getLangOptions().ObjC1 && |
| 907 | CGM.getLangOptions().getGCMode() != LangOptions::NonGC) { |
| 908 | QualType ExprTy = Field->getType(); |
| 909 | if (getContext().isObjCObjectPointerType(ExprTy)) |
| 910 | LValue::SetObjCType(false, true, LV); |
| 911 | } |
| 912 | return LV; |
Devang Patel | b9b00ad | 2007-10-23 20:28:39 +0000 | [diff] [blame] | 913 | } |
| 914 | |
Eli Friedman | 1e692ac | 2008-06-13 23:01:12 +0000 | [diff] [blame] | 915 | LValue CodeGenFunction::EmitCompoundLiteralLValue(const CompoundLiteralExpr* E) |
| 916 | { |
Eli Friedman | 06e863f | 2008-05-13 23:18:27 +0000 | [diff] [blame] | 917 | const llvm::Type *LTy = ConvertType(E->getType()); |
| 918 | llvm::Value *DeclPtr = CreateTempAlloca(LTy, ".compoundliteral"); |
| 919 | |
| 920 | const Expr* InitExpr = E->getInitializer(); |
Eli Friedman | 1e692ac | 2008-06-13 23:01:12 +0000 | [diff] [blame] | 921 | LValue Result = LValue::MakeAddr(DeclPtr, E->getType().getCVRQualifiers()); |
Eli Friedman | 06e863f | 2008-05-13 23:18:27 +0000 | [diff] [blame] | 922 | |
| 923 | if (E->getType()->isComplexType()) { |
| 924 | EmitComplexExprIntoAddr(InitExpr, DeclPtr, false); |
| 925 | } else if (hasAggregateLLVMType(E->getType())) { |
| 926 | EmitAnyExpr(InitExpr, DeclPtr, false); |
| 927 | } else { |
| 928 | EmitStoreThroughLValue(EmitAnyExpr(InitExpr), Result, E->getType()); |
| 929 | } |
| 930 | |
| 931 | return Result; |
| 932 | } |
| 933 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 934 | //===--------------------------------------------------------------------===// |
| 935 | // Expression Emission |
| 936 | //===--------------------------------------------------------------------===// |
| 937 | |
Chris Lattner | 7016a70 | 2007-08-20 22:37:10 +0000 | [diff] [blame] | 938 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 939 | RValue CodeGenFunction::EmitCallExpr(const CallExpr *E) { |
Anders Carlsson | 022012e | 2007-08-20 18:05:56 +0000 | [diff] [blame] | 940 | if (const ImplicitCastExpr *IcExpr = |
| 941 | dyn_cast<const ImplicitCastExpr>(E->getCallee())) |
| 942 | if (const DeclRefExpr *DRExpr = |
| 943 | dyn_cast<const DeclRefExpr>(IcExpr->getSubExpr())) |
| 944 | if (const FunctionDecl *FDecl = |
| 945 | dyn_cast<const FunctionDecl>(DRExpr->getDecl())) |
| 946 | if (unsigned builtinID = FDecl->getIdentifier()->getBuiltinID()) |
| 947 | return EmitBuiltinExpr(builtinID, E); |
Daniel Dunbar | 80e62c2 | 2008-09-04 03:20:13 +0000 | [diff] [blame] | 948 | |
Daniel Dunbar | ce1d38b | 2009-01-09 16:50:52 +0000 | [diff] [blame] | 949 | if (E->getCallee()->getType()->isBlockPointerType()) |
Daniel Dunbar | 8fa73ed | 2009-01-09 20:09:28 +0000 | [diff] [blame] | 950 | return EmitUnsupportedRValue(E, "block pointer reference"); |
Daniel Dunbar | ce1d38b | 2009-01-09 16:50:52 +0000 | [diff] [blame] | 951 | |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 952 | llvm::Value *Callee = EmitScalarExpr(E->getCallee()); |
Eli Friedman | 5193b8a | 2008-01-30 01:32:06 +0000 | [diff] [blame] | 953 | return EmitCallExpr(Callee, E->getCallee()->getType(), |
Ted Kremenek | 5549976 | 2008-06-17 02:43:46 +0000 | [diff] [blame] | 954 | E->arg_begin(), E->arg_end()); |
Nate Begeman | e2ce1d9 | 2008-01-17 17:46:27 +0000 | [diff] [blame] | 955 | } |
| 956 | |
Ted Kremenek | 5549976 | 2008-06-17 02:43:46 +0000 | [diff] [blame] | 957 | RValue CodeGenFunction::EmitCallExpr(Expr *FnExpr, |
| 958 | CallExpr::const_arg_iterator ArgBeg, |
| 959 | CallExpr::const_arg_iterator ArgEnd) { |
| 960 | |
Nate Begeman | e2ce1d9 | 2008-01-17 17:46:27 +0000 | [diff] [blame] | 961 | llvm::Value *Callee = EmitScalarExpr(FnExpr); |
Ted Kremenek | 5549976 | 2008-06-17 02:43:46 +0000 | [diff] [blame] | 962 | return EmitCallExpr(Callee, FnExpr->getType(), ArgBeg, ArgEnd); |
Chris Lattner | c5e940f | 2007-08-31 04:44:06 +0000 | [diff] [blame] | 963 | } |
| 964 | |
Daniel Dunbar | 80e62c2 | 2008-09-04 03:20:13 +0000 | [diff] [blame] | 965 | LValue CodeGenFunction::EmitBinaryOperatorLValue(const BinaryOperator *E) { |
| 966 | // Can only get l-value for binary operator expressions which are a |
| 967 | // simple assignment of aggregate type. |
| 968 | if (E->getOpcode() != BinaryOperator::Assign) |
| 969 | return EmitUnsupportedLValue(E, "binary l-value expression"); |
| 970 | |
| 971 | llvm::Value *Temp = CreateTempAlloca(ConvertType(E->getType())); |
| 972 | EmitAggExpr(E, Temp, false); |
| 973 | // FIXME: Are these qualifiers correct? |
| 974 | return LValue::MakeAddr(Temp, E->getType().getCVRQualifiers()); |
| 975 | } |
| 976 | |
Christopher Lamb | 22c940e | 2007-12-29 05:02:41 +0000 | [diff] [blame] | 977 | LValue CodeGenFunction::EmitCallExprLValue(const CallExpr *E) { |
| 978 | // Can only get l-value for call expression returning aggregate type |
| 979 | RValue RV = EmitCallExpr(E); |
Eli Friedman | 1e692ac | 2008-06-13 23:01:12 +0000 | [diff] [blame] | 980 | // FIXME: can this be volatile? |
| 981 | return LValue::MakeAddr(RV.getAggregateAddr(), |
| 982 | E->getType().getCVRQualifiers()); |
Christopher Lamb | 22c940e | 2007-12-29 05:02:41 +0000 | [diff] [blame] | 983 | } |
| 984 | |
Argyrios Kyrtzidis | e3a09e6 | 2008-09-10 02:36:38 +0000 | [diff] [blame] | 985 | LValue |
| 986 | CodeGenFunction::EmitCXXConditionDeclLValue(const CXXConditionDeclExpr *E) { |
| 987 | EmitLocalBlockVarDecl(*E->getVarDecl()); |
| 988 | return EmitDeclRefLValue(E); |
| 989 | } |
| 990 | |
Daniel Dunbar | 0a04d77 | 2008-08-23 10:51:21 +0000 | [diff] [blame] | 991 | LValue CodeGenFunction::EmitObjCMessageExprLValue(const ObjCMessageExpr *E) { |
| 992 | // Can only get l-value for message expression returning aggregate type |
| 993 | RValue RV = EmitObjCMessageExpr(E); |
| 994 | // FIXME: can this be volatile? |
| 995 | return LValue::MakeAddr(RV.getAggregateAddr(), |
| 996 | E->getType().getCVRQualifiers()); |
| 997 | } |
| 998 | |
Daniel Dunbar | 29e0bcc | 2008-09-24 04:00:38 +0000 | [diff] [blame] | 999 | llvm::Value *CodeGenFunction::EmitIvarOffset(ObjCInterfaceDecl *Interface, |
| 1000 | const ObjCIvarDecl *Ivar) { |
Chris Lattner | 391d77a | 2008-03-30 23:03:07 +0000 | [diff] [blame] | 1001 | // Objective-C objects are traditionally C structures with their layout |
| 1002 | // defined at compile-time. In some implementations, their layout is not |
| 1003 | // defined until run time in order to allow instance variables to be added to |
| 1004 | // a class without recompiling all of the subclasses. If this is the case |
| 1005 | // then the CGObjCRuntime subclass must return true to LateBoundIvars and |
| 1006 | // implement the lookup itself. |
Daniel Dunbar | 29e0bcc | 2008-09-24 04:00:38 +0000 | [diff] [blame] | 1007 | if (CGM.getObjCRuntime().LateBoundIVars()) |
| 1008 | assert(0 && "late-bound ivars are unsupported"); |
Chris Lattner | c8aa5f1 | 2008-04-04 04:07:35 +0000 | [diff] [blame] | 1009 | |
Daniel Dunbar | 29e0bcc | 2008-09-24 04:00:38 +0000 | [diff] [blame] | 1010 | const llvm::Type *InterfaceLTy = |
| 1011 | CGM.getTypes().ConvertType(getContext().getObjCInterfaceType(Interface)); |
| 1012 | const llvm::StructLayout *Layout = |
| 1013 | CGM.getTargetData().getStructLayout(cast<llvm::StructType>(InterfaceLTy)); |
Fariborz Jahanian | fd64bb6 | 2008-12-15 20:35:07 +0000 | [diff] [blame] | 1014 | FieldDecl *Field = Interface->lookupFieldDeclForIvar(getContext(), Ivar); |
Daniel Dunbar | 29e0bcc | 2008-09-24 04:00:38 +0000 | [diff] [blame] | 1015 | uint64_t Offset = |
Fariborz Jahanian | fd64bb6 | 2008-12-15 20:35:07 +0000 | [diff] [blame] | 1016 | Layout->getElementOffset(CGM.getTypes().getLLVMFieldNo(Field)); |
Daniel Dunbar | 29e0bcc | 2008-09-24 04:00:38 +0000 | [diff] [blame] | 1017 | |
| 1018 | return llvm::ConstantInt::get(CGM.getTypes().ConvertType(getContext().LongTy), |
| 1019 | Offset); |
| 1020 | } |
| 1021 | |
Fariborz Jahanian | 45012a7 | 2009-02-03 00:09:52 +0000 | [diff] [blame] | 1022 | LValue CodeGenFunction::EmitLValueForIvar(QualType ObjectTy, |
| 1023 | llvm::Value *BaseValue, |
Daniel Dunbar | 29e0bcc | 2008-09-24 04:00:38 +0000 | [diff] [blame] | 1024 | const ObjCIvarDecl *Ivar, |
Fariborz Jahanian | fd64bb6 | 2008-12-15 20:35:07 +0000 | [diff] [blame] | 1025 | const FieldDecl *Field, |
Daniel Dunbar | 29e0bcc | 2008-09-24 04:00:38 +0000 | [diff] [blame] | 1026 | unsigned CVRQualifiers) { |
| 1027 | // See comment in EmitIvarOffset. |
| 1028 | if (CGM.getObjCRuntime().LateBoundIVars()) |
| 1029 | assert(0 && "late-bound ivars are unsupported"); |
Daniel Dunbar | 29e0bcc | 2008-09-24 04:00:38 +0000 | [diff] [blame] | 1030 | |
Fariborz Jahanian | 598d3f6 | 2009-02-03 19:03:09 +0000 | [diff] [blame^] | 1031 | LValue LV = CGM.getObjCRuntime().EmitObjCValueForIvar(*this, |
| 1032 | ObjectTy, |
| 1033 | BaseValue, Ivar, Field, |
| 1034 | CVRQualifiers); |
Fariborz Jahanian | d1cc804 | 2008-11-20 20:53:20 +0000 | [diff] [blame] | 1035 | SetVarDeclObjCAttribute(getContext(), Ivar, Ivar->getType(), LV); |
Fariborz Jahanian | d1cc804 | 2008-11-20 20:53:20 +0000 | [diff] [blame] | 1036 | return LV; |
Daniel Dunbar | 29e0bcc | 2008-09-24 04:00:38 +0000 | [diff] [blame] | 1037 | } |
| 1038 | |
| 1039 | LValue CodeGenFunction::EmitObjCIvarRefLValue(const ObjCIvarRefExpr *E) { |
Anders Carlsson | 29b7e50 | 2008-08-25 01:53:23 +0000 | [diff] [blame] | 1040 | // FIXME: A lot of the code below could be shared with EmitMemberExpr. |
| 1041 | llvm::Value *BaseValue = 0; |
| 1042 | const Expr *BaseExpr = E->getBase(); |
| 1043 | unsigned CVRQualifiers = 0; |
Fariborz Jahanian | 45012a7 | 2009-02-03 00:09:52 +0000 | [diff] [blame] | 1044 | QualType ObjectTy; |
Anders Carlsson | 29b7e50 | 2008-08-25 01:53:23 +0000 | [diff] [blame] | 1045 | if (E->isArrow()) { |
| 1046 | BaseValue = EmitScalarExpr(BaseExpr); |
| 1047 | const PointerType *PTy = |
| 1048 | cast<PointerType>(getContext().getCanonicalType(BaseExpr->getType())); |
Fariborz Jahanian | 45012a7 | 2009-02-03 00:09:52 +0000 | [diff] [blame] | 1049 | ObjectTy = PTy->getPointeeType(); |
| 1050 | CVRQualifiers = ObjectTy.getCVRQualifiers(); |
Anders Carlsson | 29b7e50 | 2008-08-25 01:53:23 +0000 | [diff] [blame] | 1051 | } else { |
| 1052 | LValue BaseLV = EmitLValue(BaseExpr); |
| 1053 | // FIXME: this isn't right for bitfields. |
| 1054 | BaseValue = BaseLV.getAddress(); |
Fariborz Jahanian | 45012a7 | 2009-02-03 00:09:52 +0000 | [diff] [blame] | 1055 | ObjectTy = BaseExpr->getType(); |
| 1056 | CVRQualifiers = ObjectTy.getCVRQualifiers(); |
Anders Carlsson | 29b7e50 | 2008-08-25 01:53:23 +0000 | [diff] [blame] | 1057 | } |
Daniel Dunbar | 29e0bcc | 2008-09-24 04:00:38 +0000 | [diff] [blame] | 1058 | |
Fariborz Jahanian | 45012a7 | 2009-02-03 00:09:52 +0000 | [diff] [blame] | 1059 | return EmitLValueForIvar(ObjectTy, BaseValue, E->getDecl(), |
Fariborz Jahanian | efc4c4b | 2008-12-18 17:29:46 +0000 | [diff] [blame] | 1060 | getContext().getFieldDecl(E), CVRQualifiers); |
Chris Lattner | 391d77a | 2008-03-30 23:03:07 +0000 | [diff] [blame] | 1061 | } |
| 1062 | |
Daniel Dunbar | 85c59ed | 2008-08-29 08:11:39 +0000 | [diff] [blame] | 1063 | LValue |
| 1064 | CodeGenFunction::EmitObjCPropertyRefLValue(const ObjCPropertyRefExpr *E) { |
| 1065 | // This is a special l-value that just issues sends when we load or |
| 1066 | // store through it. |
| 1067 | return LValue::MakePropertyRef(E, E->getType().getCVRQualifiers()); |
| 1068 | } |
| 1069 | |
Fariborz Jahanian | 43f4470 | 2008-11-22 22:30:21 +0000 | [diff] [blame] | 1070 | LValue |
| 1071 | CodeGenFunction::EmitObjCKVCRefLValue(const ObjCKVCRefExpr *E) { |
| 1072 | // This is a special l-value that just issues sends when we load or |
| 1073 | // store through it. |
| 1074 | return LValue::MakeKVCRef(E, E->getType().getCVRQualifiers()); |
| 1075 | } |
| 1076 | |
Douglas Gregor | cd9b46e | 2008-11-04 14:56:14 +0000 | [diff] [blame] | 1077 | LValue |
| 1078 | CodeGenFunction::EmitObjCSuperExpr(const ObjCSuperExpr *E) { |
| 1079 | return EmitUnsupportedLValue(E, "use of super"); |
| 1080 | } |
| 1081 | |
Daniel Dunbar | ce1d38b | 2009-01-09 16:50:52 +0000 | [diff] [blame] | 1082 | RValue CodeGenFunction::EmitCallExpr(llvm::Value *Callee, QualType CalleeType, |
Ted Kremenek | 5549976 | 2008-06-17 02:43:46 +0000 | [diff] [blame] | 1083 | CallExpr::const_arg_iterator ArgBeg, |
| 1084 | CallExpr::const_arg_iterator ArgEnd) { |
Daniel Dunbar | ce1d38b | 2009-01-09 16:50:52 +0000 | [diff] [blame] | 1085 | // Get the actual function type. The callee type will always be a |
| 1086 | // pointer to function type or a block pointer type. |
| 1087 | QualType ResultType; |
| 1088 | if (const BlockPointerType *BPT = dyn_cast<BlockPointerType>(CalleeType)) { |
| 1089 | ResultType = BPT->getPointeeType()->getAsFunctionType()->getResultType(); |
| 1090 | } else { |
| 1091 | assert(CalleeType->isFunctionPointerType() && |
| 1092 | "Call must have function pointer type!"); |
| 1093 | QualType FnType = CalleeType->getAsPointerType()->getPointeeType(); |
| 1094 | ResultType = FnType->getAsFunctionType()->getResultType(); |
| 1095 | } |
Daniel Dunbar | 19cd87e | 2008-08-30 03:02:31 +0000 | [diff] [blame] | 1096 | |
| 1097 | CallArgList Args; |
| 1098 | for (CallExpr::const_arg_iterator I = ArgBeg; I != ArgEnd; ++I) |
Daniel Dunbar | 46f45b9 | 2008-09-09 01:06:48 +0000 | [diff] [blame] | 1099 | Args.push_back(std::make_pair(EmitAnyExprToTemp(*I), |
| 1100 | I->getType())); |
Daniel Dunbar | 19cd87e | 2008-08-30 03:02:31 +0000 | [diff] [blame] | 1101 | |
Daniel Dunbar | 541b63b | 2009-02-02 23:23:47 +0000 | [diff] [blame] | 1102 | return EmitCall(CGM.getTypes().getFunctionInfo(ResultType, Args), |
| 1103 | Callee, Args); |
Daniel Dunbar | 8f2926b | 2008-08-23 03:46:30 +0000 | [diff] [blame] | 1104 | } |