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" |
| 16 | #include "clang/AST/AST.h" |
| 17 | #include "llvm/Constants.h" |
| 18 | #include "llvm/DerivedTypes.h" |
| 19 | #include "llvm/Function.h" |
| 20 | #include "llvm/GlobalVariable.h" |
Chris Lattner | 99e0d79 | 2007-07-16 05:43:05 +0000 | [diff] [blame] | 21 | #include "llvm/Support/MathExtras.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 22 | using namespace clang; |
| 23 | using namespace CodeGen; |
| 24 | |
| 25 | //===--------------------------------------------------------------------===// |
| 26 | // Miscellaneous Helper Methods |
| 27 | //===--------------------------------------------------------------------===// |
| 28 | |
| 29 | /// CreateTempAlloca - This creates a alloca and inserts it into the entry |
| 30 | /// block. |
| 31 | llvm::AllocaInst *CodeGenFunction::CreateTempAlloca(const llvm::Type *Ty, |
| 32 | const char *Name) { |
| 33 | return new llvm::AllocaInst(Ty, 0, Name, AllocaInsertPt); |
| 34 | } |
| 35 | |
| 36 | /// EvaluateExprAsBool - Perform the usual unary conversions on the specified |
| 37 | /// expression and compare the result against zero, returning an Int1Ty value. |
| 38 | llvm::Value *CodeGenFunction::EvaluateExprAsBool(const Expr *E) { |
Chris Lattner | 9069fa2 | 2007-08-26 16:46:58 +0000 | [diff] [blame] | 39 | QualType BoolTy = getContext().BoolTy; |
Chris Lattner | 9b2dc28 | 2008-04-04 16:54:41 +0000 | [diff] [blame] | 40 | if (!E->getType()->isAnyComplexType()) |
Chris Lattner | 9069fa2 | 2007-08-26 16:46:58 +0000 | [diff] [blame] | 41 | return EmitScalarConversion(EmitScalarExpr(E), E->getType(), BoolTy); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 42 | |
Chris Lattner | 9069fa2 | 2007-08-26 16:46:58 +0000 | [diff] [blame] | 43 | return EmitComplexToScalarConversion(EmitComplexExpr(E), E->getType(),BoolTy); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 44 | } |
| 45 | |
Chris Lattner | 9b65551 | 2007-08-31 22:49:20 +0000 | [diff] [blame] | 46 | /// EmitAnyExpr - Emit code to compute the specified expression which can have |
| 47 | /// any type. The result is returned as an RValue struct. If this is an |
| 48 | /// aggregate expression, the aggloc/agglocvolatile arguments indicate where |
| 49 | /// the result should be returned. |
| 50 | RValue CodeGenFunction::EmitAnyExpr(const Expr *E, llvm::Value *AggLoc, |
| 51 | bool isAggLocVolatile) { |
| 52 | if (!hasAggregateLLVMType(E->getType())) |
| 53 | return RValue::get(EmitScalarExpr(E)); |
Chris Lattner | 9b2dc28 | 2008-04-04 16:54:41 +0000 | [diff] [blame] | 54 | else if (E->getType()->isAnyComplexType()) |
Chris Lattner | 9b65551 | 2007-08-31 22:49:20 +0000 | [diff] [blame] | 55 | return RValue::getComplex(EmitComplexExpr(E)); |
| 56 | |
| 57 | EmitAggExpr(E, AggLoc, isAggLocVolatile); |
| 58 | return RValue::getAggregate(AggLoc); |
| 59 | } |
| 60 | |
| 61 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 62 | //===----------------------------------------------------------------------===// |
| 63 | // LValue Expression Emission |
| 64 | //===----------------------------------------------------------------------===// |
| 65 | |
| 66 | /// EmitLValue - Emit code to compute a designator that specifies the location |
| 67 | /// of the expression. |
| 68 | /// |
| 69 | /// This can return one of two things: a simple address or a bitfield |
| 70 | /// reference. In either case, the LLVM Value* in the LValue structure is |
| 71 | /// guaranteed to be an LLVM pointer type. |
| 72 | /// |
| 73 | /// If this returns a bitfield reference, nothing about the pointee type of |
| 74 | /// the LLVM value is known: For example, it may not be a pointer to an |
| 75 | /// integer. |
| 76 | /// |
| 77 | /// If this returns a normal address, and if the lvalue's C type is fixed |
| 78 | /// size, this method guarantees that the returned pointer type will point to |
| 79 | /// an LLVM type of the same size of the lvalue's type. If the lvalue has a |
| 80 | /// variable length type, this is not possible. |
| 81 | /// |
| 82 | LValue CodeGenFunction::EmitLValue(const Expr *E) { |
| 83 | switch (E->getStmtClass()) { |
Chris Lattner | 7013c8c | 2007-08-26 05:06:40 +0000 | [diff] [blame] | 84 | default: { |
Chris Lattner | ce5605e | 2008-03-30 23:25:33 +0000 | [diff] [blame] | 85 | printf("Statement class: %d\n", E->getStmtClass()); |
Chris Lattner | dc4d280 | 2007-12-02 01:49:16 +0000 | [diff] [blame] | 86 | WarnUnsupported(E, "l-value expression"); |
Christopher Lamb | ddc23f3 | 2007-12-17 01:11:20 +0000 | [diff] [blame] | 87 | llvm::Type *Ty = llvm::PointerType::getUnqual(ConvertType(E->getType())); |
Chris Lattner | 7013c8c | 2007-08-26 05:06:40 +0000 | [diff] [blame] | 88 | return LValue::MakeAddr(llvm::UndefValue::get(Ty)); |
| 89 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 90 | |
Christopher Lamb | 22c940e | 2007-12-29 05:02:41 +0000 | [diff] [blame] | 91 | case Expr::CallExprClass: return EmitCallExprLValue(cast<CallExpr>(E)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 92 | case Expr::DeclRefExprClass: return EmitDeclRefLValue(cast<DeclRefExpr>(E)); |
| 93 | case Expr::ParenExprClass:return EmitLValue(cast<ParenExpr>(E)->getSubExpr()); |
Anders Carlsson | 2274266 | 2007-07-21 05:21:51 +0000 | [diff] [blame] | 94 | case Expr::PreDefinedExprClass: |
| 95 | return EmitPreDefinedLValue(cast<PreDefinedExpr>(E)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 96 | case Expr::StringLiteralClass: |
| 97 | return EmitStringLiteralLValue(cast<StringLiteral>(E)); |
Chris Lattner | 391d77a | 2008-03-30 23:03:07 +0000 | [diff] [blame] | 98 | |
| 99 | case Expr::ObjCIvarRefExprClass: |
| 100 | return EmitObjCIvarRefLValue(cast<ObjCIvarRefExpr>(E)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 101 | |
| 102 | case Expr::UnaryOperatorClass: |
| 103 | return EmitUnaryOpLValue(cast<UnaryOperator>(E)); |
| 104 | case Expr::ArraySubscriptExprClass: |
| 105 | return EmitArraySubscriptExpr(cast<ArraySubscriptExpr>(E)); |
Nate Begeman | 213541a | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 106 | case Expr::ExtVectorElementExprClass: |
| 107 | return EmitExtVectorElementExpr(cast<ExtVectorElementExpr>(E)); |
Devang Patel | b9b00ad | 2007-10-23 20:28:39 +0000 | [diff] [blame] | 108 | case Expr::MemberExprClass: return EmitMemberExpr(cast<MemberExpr>(E)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 109 | } |
| 110 | } |
| 111 | |
| 112 | /// EmitLoadOfLValue - Given an expression that represents a value lvalue, |
| 113 | /// this method emits the address of the lvalue, then loads the result as an |
| 114 | /// rvalue, returning the rvalue. |
| 115 | RValue CodeGenFunction::EmitLoadOfLValue(LValue LV, QualType ExprType) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 116 | if (LV.isSimple()) { |
| 117 | llvm::Value *Ptr = LV.getAddress(); |
| 118 | const llvm::Type *EltTy = |
| 119 | cast<llvm::PointerType>(Ptr->getType())->getElementType(); |
| 120 | |
| 121 | // Simple scalar l-value. |
Chris Lattner | 01e3c9e | 2008-01-30 07:01:17 +0000 | [diff] [blame] | 122 | if (EltTy->isFirstClassType()) { |
| 123 | llvm::Value *V = Builder.CreateLoad(Ptr, "tmp"); |
| 124 | |
| 125 | // Bool can have different representation in memory than in registers. |
| 126 | if (ExprType->isBooleanType()) { |
| 127 | if (V->getType() != llvm::Type::Int1Ty) |
| 128 | V = Builder.CreateTrunc(V, llvm::Type::Int1Ty, "tobool"); |
| 129 | } |
| 130 | |
| 131 | return RValue::get(V); |
| 132 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 133 | |
Chris Lattner | 883f6a7 | 2007-08-11 00:04:45 +0000 | [diff] [blame] | 134 | assert(ExprType->isFunctionType() && "Unknown scalar value"); |
| 135 | return RValue::get(Ptr); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | if (LV.isVectorElt()) { |
| 139 | llvm::Value *Vec = Builder.CreateLoad(LV.getVectorAddr(), "tmp"); |
| 140 | return RValue::get(Builder.CreateExtractElement(Vec, LV.getVectorIdx(), |
| 141 | "vecext")); |
| 142 | } |
Chris Lattner | 46ea8eb | 2007-08-03 00:16:29 +0000 | [diff] [blame] | 143 | |
| 144 | // If this is a reference to a subset of the elements of a vector, either |
| 145 | // shuffle the input or extract/insert them as appropriate. |
Nate Begeman | 213541a | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 146 | if (LV.isExtVectorElt()) |
| 147 | return EmitLoadOfExtVectorElementLValue(LV, ExprType); |
Lauro Ramos Venancio | 3b8c22d | 2008-01-22 20:17:04 +0000 | [diff] [blame] | 148 | |
| 149 | if (LV.isBitfield()) |
| 150 | return EmitLoadOfBitfieldLValue(LV, ExprType); |
| 151 | |
| 152 | assert(0 && "Unknown LValue type!"); |
Chris Lattner | b1776cb | 2007-09-16 19:23:47 +0000 | [diff] [blame] | 153 | //an invalid RValue, but the assert will |
| 154 | //ensure that this point is never reached |
| 155 | return RValue(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 156 | } |
| 157 | |
Lauro Ramos Venancio | 3b8c22d | 2008-01-22 20:17:04 +0000 | [diff] [blame] | 158 | RValue CodeGenFunction::EmitLoadOfBitfieldLValue(LValue LV, |
| 159 | QualType ExprType) { |
| 160 | llvm::Value *Ptr = LV.getBitfieldAddr(); |
| 161 | const llvm::Type *EltTy = |
| 162 | cast<llvm::PointerType>(Ptr->getType())->getElementType(); |
| 163 | unsigned EltTySize = EltTy->getPrimitiveSizeInBits(); |
| 164 | unsigned short BitfieldSize = LV.getBitfieldSize(); |
| 165 | unsigned short EndBit = LV.getBitfieldStartBit() + BitfieldSize; |
| 166 | |
| 167 | llvm::Value *V = Builder.CreateLoad(Ptr, "tmp"); |
| 168 | |
| 169 | llvm::Value *ShAmt = llvm::ConstantInt::get(EltTy, EltTySize - EndBit); |
| 170 | V = Builder.CreateShl(V, ShAmt, "tmp"); |
| 171 | |
| 172 | ShAmt = llvm::ConstantInt::get(EltTy, EltTySize - BitfieldSize); |
| 173 | V = LV.isBitfieldSigned() ? |
| 174 | Builder.CreateAShr(V, ShAmt, "tmp") : |
| 175 | Builder.CreateLShr(V, ShAmt, "tmp"); |
| 176 | return RValue::get(V); |
| 177 | } |
| 178 | |
Chris Lattner | 34cdc86 | 2007-08-03 16:18:34 +0000 | [diff] [blame] | 179 | // If this is a reference to a subset of the elements of a vector, either |
| 180 | // shuffle the input or extract/insert them as appropriate. |
Nate Begeman | 213541a | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 181 | RValue CodeGenFunction::EmitLoadOfExtVectorElementLValue(LValue LV, |
| 182 | QualType ExprType) { |
| 183 | llvm::Value *Vec = Builder.CreateLoad(LV.getExtVectorAddr(), "tmp"); |
Chris Lattner | 34cdc86 | 2007-08-03 16:18:34 +0000 | [diff] [blame] | 184 | |
Nate Begeman | 8a99764 | 2008-05-09 06:41:27 +0000 | [diff] [blame] | 185 | const llvm::Constant *Elts = LV.getExtVectorElts(); |
Chris Lattner | 34cdc86 | 2007-08-03 16:18:34 +0000 | [diff] [blame] | 186 | |
| 187 | // If the result of the expression is a non-vector type, we must be |
| 188 | // extracting a single element. Just codegen as an extractelement. |
Chris Lattner | cf60cd2 | 2007-08-10 17:10:08 +0000 | [diff] [blame] | 189 | const VectorType *ExprVT = ExprType->getAsVectorType(); |
| 190 | if (!ExprVT) { |
Nate Begeman | 8a99764 | 2008-05-09 06:41:27 +0000 | [diff] [blame] | 191 | unsigned InIdx = ExtVectorElementExpr::getAccessedFieldNo(0, Elts); |
Chris Lattner | 34cdc86 | 2007-08-03 16:18:34 +0000 | [diff] [blame] | 192 | llvm::Value *Elt = llvm::ConstantInt::get(llvm::Type::Int32Ty, InIdx); |
| 193 | return RValue::get(Builder.CreateExtractElement(Vec, Elt, "tmp")); |
| 194 | } |
| 195 | |
| 196 | // If the source and destination have the same number of elements, use a |
| 197 | // vector shuffle instead of insert/extracts. |
Chris Lattner | cf60cd2 | 2007-08-10 17:10:08 +0000 | [diff] [blame] | 198 | unsigned NumResultElts = ExprVT->getNumElements(); |
Chris Lattner | 34cdc86 | 2007-08-03 16:18:34 +0000 | [diff] [blame] | 199 | unsigned NumSourceElts = |
| 200 | cast<llvm::VectorType>(Vec->getType())->getNumElements(); |
| 201 | |
| 202 | if (NumResultElts == NumSourceElts) { |
| 203 | llvm::SmallVector<llvm::Constant*, 4> Mask; |
| 204 | for (unsigned i = 0; i != NumResultElts; ++i) { |
Nate Begeman | 8a99764 | 2008-05-09 06:41:27 +0000 | [diff] [blame] | 205 | unsigned InIdx = ExtVectorElementExpr::getAccessedFieldNo(i, Elts); |
Chris Lattner | 34cdc86 | 2007-08-03 16:18:34 +0000 | [diff] [blame] | 206 | Mask.push_back(llvm::ConstantInt::get(llvm::Type::Int32Ty, InIdx)); |
| 207 | } |
| 208 | |
| 209 | llvm::Value *MaskV = llvm::ConstantVector::get(&Mask[0], Mask.size()); |
| 210 | Vec = Builder.CreateShuffleVector(Vec, |
| 211 | llvm::UndefValue::get(Vec->getType()), |
| 212 | MaskV, "tmp"); |
| 213 | return RValue::get(Vec); |
| 214 | } |
| 215 | |
| 216 | // Start out with an undef of the result type. |
| 217 | llvm::Value *Result = llvm::UndefValue::get(ConvertType(ExprType)); |
| 218 | |
| 219 | // Extract/Insert each element of the result. |
| 220 | for (unsigned i = 0; i != NumResultElts; ++i) { |
Nate Begeman | 8a99764 | 2008-05-09 06:41:27 +0000 | [diff] [blame] | 221 | unsigned InIdx = ExtVectorElementExpr::getAccessedFieldNo(i, Elts); |
Chris Lattner | 34cdc86 | 2007-08-03 16:18:34 +0000 | [diff] [blame] | 222 | llvm::Value *Elt = llvm::ConstantInt::get(llvm::Type::Int32Ty, InIdx); |
| 223 | Elt = Builder.CreateExtractElement(Vec, Elt, "tmp"); |
| 224 | |
| 225 | llvm::Value *OutIdx = llvm::ConstantInt::get(llvm::Type::Int32Ty, i); |
| 226 | Result = Builder.CreateInsertElement(Result, Elt, OutIdx, "tmp"); |
| 227 | } |
| 228 | |
| 229 | return RValue::get(Result); |
| 230 | } |
| 231 | |
| 232 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 233 | |
| 234 | /// EmitStoreThroughLValue - Store the specified rvalue into the specified |
| 235 | /// lvalue, where both are guaranteed to the have the same type, and that type |
| 236 | /// is 'Ty'. |
| 237 | void CodeGenFunction::EmitStoreThroughLValue(RValue Src, LValue Dst, |
| 238 | QualType Ty) { |
Chris Lattner | 017d6aa | 2007-08-03 16:28:33 +0000 | [diff] [blame] | 239 | if (!Dst.isSimple()) { |
| 240 | if (Dst.isVectorElt()) { |
| 241 | // Read/modify/write the vector, inserting the new element. |
| 242 | // FIXME: Volatility. |
| 243 | llvm::Value *Vec = Builder.CreateLoad(Dst.getVectorAddr(), "tmp"); |
Chris Lattner | 9b65551 | 2007-08-31 22:49:20 +0000 | [diff] [blame] | 244 | Vec = Builder.CreateInsertElement(Vec, Src.getScalarVal(), |
Chris Lattner | 017d6aa | 2007-08-03 16:28:33 +0000 | [diff] [blame] | 245 | Dst.getVectorIdx(), "vecins"); |
| 246 | Builder.CreateStore(Vec, Dst.getVectorAddr()); |
| 247 | return; |
| 248 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 249 | |
Nate Begeman | 213541a | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 250 | // If this is an update of extended vector elements, insert them as |
| 251 | // appropriate. |
| 252 | if (Dst.isExtVectorElt()) |
| 253 | return EmitStoreThroughExtVectorComponentLValue(Src, Dst, Ty); |
Lauro Ramos Venancio | a0c5d0e | 2008-01-22 22:36:45 +0000 | [diff] [blame] | 254 | |
| 255 | if (Dst.isBitfield()) |
| 256 | return EmitStoreThroughBitfieldLValue(Src, Dst, Ty); |
| 257 | |
Lauro Ramos Venancio | 6553982 | 2008-01-22 22:38:35 +0000 | [diff] [blame] | 258 | assert(0 && "Unknown LValue type"); |
Chris Lattner | 017d6aa | 2007-08-03 16:28:33 +0000 | [diff] [blame] | 259 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 260 | |
| 261 | llvm::Value *DstAddr = Dst.getAddress(); |
Chris Lattner | 883f6a7 | 2007-08-11 00:04:45 +0000 | [diff] [blame] | 262 | assert(Src.isScalar() && "Can't emit an agg store with this method"); |
| 263 | // FIXME: Handle volatility etc. |
Chris Lattner | 9b65551 | 2007-08-31 22:49:20 +0000 | [diff] [blame] | 264 | const llvm::Type *SrcTy = Src.getScalarVal()->getType(); |
Christopher Lamb | ddc23f3 | 2007-12-17 01:11:20 +0000 | [diff] [blame] | 265 | const llvm::PointerType *DstPtr = cast<llvm::PointerType>(DstAddr->getType()); |
| 266 | const llvm::Type *AddrTy = DstPtr->getElementType(); |
| 267 | unsigned AS = DstPtr->getAddressSpace(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 268 | |
Chris Lattner | 883f6a7 | 2007-08-11 00:04:45 +0000 | [diff] [blame] | 269 | if (AddrTy != SrcTy) |
Christopher Lamb | ddc23f3 | 2007-12-17 01:11:20 +0000 | [diff] [blame] | 270 | DstAddr = Builder.CreateBitCast(DstAddr, |
| 271 | llvm::PointerType::get(SrcTy, AS), |
Chris Lattner | 883f6a7 | 2007-08-11 00:04:45 +0000 | [diff] [blame] | 272 | "storetmp"); |
Chris Lattner | 9b65551 | 2007-08-31 22:49:20 +0000 | [diff] [blame] | 273 | Builder.CreateStore(Src.getScalarVal(), DstAddr); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 274 | } |
| 275 | |
Lauro Ramos Venancio | a0c5d0e | 2008-01-22 22:36:45 +0000 | [diff] [blame] | 276 | void CodeGenFunction::EmitStoreThroughBitfieldLValue(RValue Src, LValue Dst, |
| 277 | QualType Ty) { |
| 278 | unsigned short StartBit = Dst.getBitfieldStartBit(); |
| 279 | unsigned short BitfieldSize = Dst.getBitfieldSize(); |
| 280 | llvm::Value *Ptr = Dst.getBitfieldAddr(); |
| 281 | const llvm::Type *EltTy = |
| 282 | cast<llvm::PointerType>(Ptr->getType())->getElementType(); |
| 283 | unsigned EltTySize = EltTy->getPrimitiveSizeInBits(); |
| 284 | |
| 285 | llvm::Value *NewVal = Src.getScalarVal(); |
| 286 | llvm::Value *OldVal = Builder.CreateLoad(Ptr, "tmp"); |
| 287 | |
| 288 | llvm::Value *ShAmt = llvm::ConstantInt::get(EltTy, StartBit); |
| 289 | NewVal = Builder.CreateShl(NewVal, ShAmt, "tmp"); |
| 290 | |
| 291 | llvm::Constant *Mask = llvm::ConstantInt::get( |
| 292 | llvm::APInt::getBitsSet(EltTySize, StartBit, |
Dan Gohman | d254f00 | 2008-02-12 21:49:34 +0000 | [diff] [blame] | 293 | StartBit + BitfieldSize)); |
Lauro Ramos Venancio | a0c5d0e | 2008-01-22 22:36:45 +0000 | [diff] [blame] | 294 | |
| 295 | // Mask out any bits that shouldn't be set in the result. |
| 296 | NewVal = Builder.CreateAnd(NewVal, Mask, "tmp"); |
| 297 | |
| 298 | // Next, mask out the bits this bit-field should include from the old value. |
| 299 | Mask = llvm::ConstantExpr::getNot(Mask); |
| 300 | OldVal = Builder.CreateAnd(OldVal, Mask, "tmp"); |
| 301 | |
| 302 | // Finally, merge the two together and store it. |
| 303 | NewVal = Builder.CreateOr(OldVal, NewVal, "tmp"); |
| 304 | |
| 305 | Builder.CreateStore(NewVal, Ptr); |
| 306 | } |
| 307 | |
Nate Begeman | 213541a | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 308 | void CodeGenFunction::EmitStoreThroughExtVectorComponentLValue(RValue Src, |
| 309 | LValue Dst, |
| 310 | QualType Ty) { |
Chris Lattner | 017d6aa | 2007-08-03 16:28:33 +0000 | [diff] [blame] | 311 | // This access turns into a read/modify/write of the vector. Load the input |
| 312 | // value now. |
Nate Begeman | 213541a | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 313 | llvm::Value *Vec = Builder.CreateLoad(Dst.getExtVectorAddr(), "tmp"); |
Chris Lattner | 017d6aa | 2007-08-03 16:28:33 +0000 | [diff] [blame] | 314 | // FIXME: Volatility. |
Nate Begeman | 8a99764 | 2008-05-09 06:41:27 +0000 | [diff] [blame] | 315 | const llvm::Constant *Elts = Dst.getExtVectorElts(); |
Chris Lattner | 017d6aa | 2007-08-03 16:28:33 +0000 | [diff] [blame] | 316 | |
Chris Lattner | 9b65551 | 2007-08-31 22:49:20 +0000 | [diff] [blame] | 317 | llvm::Value *SrcVal = Src.getScalarVal(); |
Chris Lattner | 017d6aa | 2007-08-03 16:28:33 +0000 | [diff] [blame] | 318 | |
Chris Lattner | 7e6b51b | 2007-08-03 16:37:04 +0000 | [diff] [blame] | 319 | if (const VectorType *VTy = Ty->getAsVectorType()) { |
| 320 | unsigned NumSrcElts = VTy->getNumElements(); |
| 321 | |
| 322 | // Extract/Insert each element. |
| 323 | for (unsigned i = 0; i != NumSrcElts; ++i) { |
| 324 | llvm::Value *Elt = llvm::ConstantInt::get(llvm::Type::Int32Ty, i); |
| 325 | Elt = Builder.CreateExtractElement(SrcVal, Elt, "tmp"); |
| 326 | |
Nate Begeman | 8a99764 | 2008-05-09 06:41:27 +0000 | [diff] [blame] | 327 | unsigned Idx = ExtVectorElementExpr::getAccessedFieldNo(i, Elts); |
Chris Lattner | 7e6b51b | 2007-08-03 16:37:04 +0000 | [diff] [blame] | 328 | llvm::Value *OutIdx = llvm::ConstantInt::get(llvm::Type::Int32Ty, Idx); |
| 329 | Vec = Builder.CreateInsertElement(Vec, Elt, OutIdx, "tmp"); |
| 330 | } |
| 331 | } else { |
| 332 | // If the Src is a scalar (not a vector) it must be updating one element. |
Nate Begeman | 8a99764 | 2008-05-09 06:41:27 +0000 | [diff] [blame] | 333 | unsigned InIdx = ExtVectorElementExpr::getAccessedFieldNo(0, Elts); |
Chris Lattner | 017d6aa | 2007-08-03 16:28:33 +0000 | [diff] [blame] | 334 | llvm::Value *Elt = llvm::ConstantInt::get(llvm::Type::Int32Ty, InIdx); |
| 335 | Vec = Builder.CreateInsertElement(Vec, SrcVal, Elt, "tmp"); |
Chris Lattner | 017d6aa | 2007-08-03 16:28:33 +0000 | [diff] [blame] | 336 | } |
| 337 | |
Nate Begeman | 213541a | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 338 | Builder.CreateStore(Vec, Dst.getExtVectorAddr()); |
Chris Lattner | 017d6aa | 2007-08-03 16:28:33 +0000 | [diff] [blame] | 339 | } |
| 340 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 341 | |
| 342 | LValue CodeGenFunction::EmitDeclRefLValue(const DeclRefExpr *E) { |
Steve Naroff | 248a753 | 2008-04-15 22:42:06 +0000 | [diff] [blame] | 343 | const VarDecl *VD = dyn_cast<VarDecl>(E->getDecl()); |
| 344 | |
| 345 | if (VD && (VD->isBlockVarDecl() || isa<ParmVarDecl>(VD))) { |
Lauro Ramos Venancio | fea90b8 | 2008-02-16 22:30:38 +0000 | [diff] [blame] | 346 | if (VD->getStorageClass() == VarDecl::Extern) |
| 347 | return LValue::MakeAddr(CGM.GetAddrOfGlobalVar(VD, false)); |
| 348 | else { |
Steve Naroff | 248a753 | 2008-04-15 22:42:06 +0000 | [diff] [blame] | 349 | llvm::Value *V = LocalDeclMap[VD]; |
Lauro Ramos Venancio | fea90b8 | 2008-02-16 22:30:38 +0000 | [diff] [blame] | 350 | assert(V && "BlockVarDecl not entered in LocalDeclMap?"); |
| 351 | return LValue::MakeAddr(V); |
| 352 | } |
Steve Naroff | 248a753 | 2008-04-15 22:42:06 +0000 | [diff] [blame] | 353 | } else if (VD && VD->isFileVarDecl()) { |
| 354 | return LValue::MakeAddr(CGM.GetAddrOfGlobalVar(VD, false)); |
| 355 | } else if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(E->getDecl())) { |
Chris Lattner | 9cd4fe4 | 2007-12-02 07:09:19 +0000 | [diff] [blame] | 356 | return LValue::MakeAddr(CGM.GetAddrOfFunctionDecl(FD, false)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 357 | } |
| 358 | assert(0 && "Unimp declref"); |
Chris Lattner | b1776cb | 2007-09-16 19:23:47 +0000 | [diff] [blame] | 359 | //an invalid LValue, but the assert will |
| 360 | //ensure that this point is never reached. |
| 361 | return LValue(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 362 | } |
| 363 | |
| 364 | LValue CodeGenFunction::EmitUnaryOpLValue(const UnaryOperator *E) { |
| 365 | // __extension__ doesn't affect lvalue-ness. |
| 366 | if (E->getOpcode() == UnaryOperator::Extension) |
| 367 | return EmitLValue(E->getSubExpr()); |
| 368 | |
Chris Lattner | 7da36f6 | 2007-10-30 22:53:42 +0000 | [diff] [blame] | 369 | switch (E->getOpcode()) { |
| 370 | default: assert(0 && "Unknown unary operator lvalue!"); |
| 371 | case UnaryOperator::Deref: |
| 372 | return LValue::MakeAddr(EmitScalarExpr(E->getSubExpr())); |
| 373 | case UnaryOperator::Real: |
| 374 | case UnaryOperator::Imag: |
| 375 | LValue LV = EmitLValue(E->getSubExpr()); |
Chris Lattner | 36b6a0a | 2008-03-19 05:19:41 +0000 | [diff] [blame] | 376 | unsigned Idx = E->getOpcode() == UnaryOperator::Imag; |
| 377 | return LValue::MakeAddr(Builder.CreateStructGEP(LV.getAddress(), |
| 378 | Idx, "idx")); |
Chris Lattner | 7da36f6 | 2007-10-30 22:53:42 +0000 | [diff] [blame] | 379 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 380 | } |
| 381 | |
| 382 | LValue CodeGenFunction::EmitStringLiteralLValue(const StringLiteral *E) { |
| 383 | assert(!E->isWide() && "FIXME: Wide strings not supported yet!"); |
| 384 | const char *StrData = E->getStrData(); |
| 385 | unsigned Len = E->getByteLength(); |
Chris Lattner | 45e8cbd | 2007-11-28 05:34:05 +0000 | [diff] [blame] | 386 | std::string StringLiteral(StrData, StrData+Len); |
| 387 | return LValue::MakeAddr(CGM.GetAddrOfConstantString(StringLiteral)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 388 | } |
| 389 | |
Anders Carlsson | 2274266 | 2007-07-21 05:21:51 +0000 | [diff] [blame] | 390 | LValue CodeGenFunction::EmitPreDefinedLValue(const PreDefinedExpr *E) { |
Chris Lattner | c8aa5f1 | 2008-04-04 04:07:35 +0000 | [diff] [blame] | 391 | std::string FunctionName; |
| 392 | if(const FunctionDecl *FD = dyn_cast<FunctionDecl>(CurFuncDecl)) { |
| 393 | FunctionName = FD->getName(); |
| 394 | } |
| 395 | else { |
| 396 | assert(0 && "Attempting to load predefined constant for invalid decl type"); |
| 397 | } |
Anders Carlsson | 2274266 | 2007-07-21 05:21:51 +0000 | [diff] [blame] | 398 | std::string GlobalVarName; |
| 399 | |
| 400 | switch (E->getIdentType()) { |
| 401 | default: |
| 402 | assert(0 && "unknown pre-defined ident type"); |
| 403 | case PreDefinedExpr::Func: |
| 404 | GlobalVarName = "__func__."; |
| 405 | break; |
| 406 | case PreDefinedExpr::Function: |
| 407 | GlobalVarName = "__FUNCTION__."; |
| 408 | break; |
| 409 | case PreDefinedExpr::PrettyFunction: |
| 410 | // FIXME:: Demangle C++ method names |
| 411 | GlobalVarName = "__PRETTY_FUNCTION__."; |
| 412 | break; |
| 413 | } |
| 414 | |
Chris Lattner | c8aa5f1 | 2008-04-04 04:07:35 +0000 | [diff] [blame] | 415 | GlobalVarName += FunctionName; |
Anders Carlsson | 2274266 | 2007-07-21 05:21:51 +0000 | [diff] [blame] | 416 | |
| 417 | // FIXME: Can cache/reuse these within the module. |
| 418 | llvm::Constant *C=llvm::ConstantArray::get(FunctionName); |
| 419 | |
| 420 | // Create a global variable for this. |
| 421 | C = new llvm::GlobalVariable(C->getType(), true, |
| 422 | llvm::GlobalValue::InternalLinkage, |
| 423 | C, GlobalVarName, CurFn->getParent()); |
Anders Carlsson | 2274266 | 2007-07-21 05:21:51 +0000 | [diff] [blame] | 424 | return LValue::MakeAddr(C); |
| 425 | } |
| 426 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 427 | LValue CodeGenFunction::EmitArraySubscriptExpr(const ArraySubscriptExpr *E) { |
Ted Kremenek | 2324512 | 2007-08-20 16:18:38 +0000 | [diff] [blame] | 428 | // 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] | 429 | llvm::Value *Idx = EmitScalarExpr(E->getIdx()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 430 | |
| 431 | // If the base is a vector type, then we are forming a vector element lvalue |
| 432 | // with this subscript. |
Ted Kremenek | 2324512 | 2007-08-20 16:18:38 +0000 | [diff] [blame] | 433 | if (E->getLHS()->getType()->isVectorType()) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 434 | // Emit the vector as an lvalue to get its address. |
Ted Kremenek | 2324512 | 2007-08-20 16:18:38 +0000 | [diff] [blame] | 435 | LValue LHS = EmitLValue(E->getLHS()); |
| 436 | assert(LHS.isSimple() && "Can only subscript lvalue vectors here!"); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 437 | // FIXME: This should properly sign/zero/extend or truncate Idx to i32. |
Ted Kremenek | 2324512 | 2007-08-20 16:18:38 +0000 | [diff] [blame] | 438 | return LValue::MakeVectorElt(LHS.getAddress(), Idx); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 439 | } |
| 440 | |
Ted Kremenek | 2324512 | 2007-08-20 16:18:38 +0000 | [diff] [blame] | 441 | // 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] | 442 | llvm::Value *Base = EmitScalarExpr(E->getBase()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 443 | |
Ted Kremenek | 2324512 | 2007-08-20 16:18:38 +0000 | [diff] [blame] | 444 | // Extend or truncate the index type to 32 or 64-bits. |
Chris Lattner | d4f0802 | 2007-08-08 17:43:05 +0000 | [diff] [blame] | 445 | QualType IdxTy = E->getIdx()->getType(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 446 | bool IdxSigned = IdxTy->isSignedIntegerType(); |
| 447 | unsigned IdxBitwidth = cast<llvm::IntegerType>(Idx->getType())->getBitWidth(); |
| 448 | if (IdxBitwidth != LLVMPointerWidth) |
| 449 | Idx = Builder.CreateIntCast(Idx, llvm::IntegerType::get(LLVMPointerWidth), |
| 450 | IdxSigned, "idxprom"); |
| 451 | |
| 452 | // We know that the pointer points to a type of the correct size, unless the |
| 453 | // size is a VLA. |
Eli Friedman | 3c2b317 | 2008-02-15 12:20:59 +0000 | [diff] [blame] | 454 | if (!E->getType()->isConstantSizeType()) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 455 | assert(0 && "VLA idx not implemented"); |
| 456 | return LValue::MakeAddr(Builder.CreateGEP(Base, Idx, "arrayidx")); |
| 457 | } |
| 458 | |
Nate Begeman | 3b8d116 | 2008-05-13 21:03:02 +0000 | [diff] [blame^] | 459 | static |
| 460 | llvm::Constant *GenerateConstantVector(llvm::SmallVector<unsigned, 4> &Elts) { |
| 461 | llvm::SmallVector<llvm::Constant *, 4> CElts; |
| 462 | |
| 463 | for (unsigned i = 0, e = Elts.size(); i != e; ++i) |
| 464 | CElts.push_back(llvm::ConstantInt::get(llvm::Type::Int32Ty, Elts[i])); |
| 465 | |
| 466 | return llvm::ConstantVector::get(&CElts[0], CElts.size()); |
| 467 | } |
| 468 | |
Chris Lattner | 349aaec | 2007-08-02 23:37:31 +0000 | [diff] [blame] | 469 | LValue CodeGenFunction:: |
Nate Begeman | 213541a | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 470 | EmitExtVectorElementExpr(const ExtVectorElementExpr *E) { |
Chris Lattner | 349aaec | 2007-08-02 23:37:31 +0000 | [diff] [blame] | 471 | // Emit the base vector as an l-value. |
| 472 | LValue Base = EmitLValue(E->getBase()); |
Chris Lattner | 349aaec | 2007-08-02 23:37:31 +0000 | [diff] [blame] | 473 | |
Nate Begeman | 3b8d116 | 2008-05-13 21:03:02 +0000 | [diff] [blame^] | 474 | // Encode the element access list into a vector of unsigned indices. |
| 475 | llvm::SmallVector<unsigned, 4> Indices; |
| 476 | E->getEncodedElementAccess(Indices); |
| 477 | |
| 478 | if (Base.isSimple()) { |
| 479 | llvm::Constant *CV = GenerateConstantVector(Indices); |
| 480 | return LValue::MakeExtVectorElt(Base.getAddress(), CV); |
| 481 | } |
| 482 | assert(Base.isExtVectorElt() && "Can only subscript lvalue vec elts here!"); |
| 483 | |
| 484 | llvm::Constant *BaseElts = Base.getExtVectorElts(); |
| 485 | llvm::SmallVector<llvm::Constant *, 4> CElts; |
| 486 | |
| 487 | for (unsigned i = 0, e = Indices.size(); i != e; ++i) { |
| 488 | if (isa<llvm::ConstantAggregateZero>(BaseElts)) |
| 489 | CElts.push_back(llvm::ConstantInt::get(llvm::Type::Int32Ty, 0)); |
| 490 | else |
| 491 | CElts.push_back(BaseElts->getOperand(Indices[i])); |
| 492 | } |
| 493 | llvm::Constant *CV = llvm::ConstantVector::get(&CElts[0], CElts.size()); |
| 494 | return LValue::MakeExtVectorElt(Base.getExtVectorAddr(), CV); |
Chris Lattner | 349aaec | 2007-08-02 23:37:31 +0000 | [diff] [blame] | 495 | } |
| 496 | |
Devang Patel | b9b00ad | 2007-10-23 20:28:39 +0000 | [diff] [blame] | 497 | LValue CodeGenFunction::EmitMemberExpr(const MemberExpr *E) { |
Devang Patel | fe2419a | 2007-12-11 21:33:16 +0000 | [diff] [blame] | 498 | bool isUnion = false; |
Devang Patel | 126a856 | 2007-10-24 22:26:28 +0000 | [diff] [blame] | 499 | Expr *BaseExpr = E->getBase(); |
Devang Patel | 126a856 | 2007-10-24 22:26:28 +0000 | [diff] [blame] | 500 | llvm::Value *BaseValue = NULL; |
Chris Lattner | 12f65f6 | 2007-12-02 18:52:07 +0000 | [diff] [blame] | 501 | |
| 502 | // 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] | 503 | if (E->isArrow()) { |
Devang Patel | 0a96118 | 2007-10-26 18:15:21 +0000 | [diff] [blame] | 504 | BaseValue = EmitScalarExpr(BaseExpr); |
Devang Patel | fe2419a | 2007-12-11 21:33:16 +0000 | [diff] [blame] | 505 | const PointerType *PTy = |
| 506 | cast<PointerType>(BaseExpr->getType().getCanonicalType()); |
| 507 | if (PTy->getPointeeType()->isUnionType()) |
| 508 | isUnion = true; |
| 509 | } |
Chris Lattner | 12f65f6 | 2007-12-02 18:52:07 +0000 | [diff] [blame] | 510 | else { |
| 511 | LValue BaseLV = EmitLValue(BaseExpr); |
| 512 | // FIXME: this isn't right for bitfields. |
| 513 | BaseValue = BaseLV.getAddress(); |
Devang Patel | fe2419a | 2007-12-11 21:33:16 +0000 | [diff] [blame] | 514 | if (BaseExpr->getType()->isUnionType()) |
| 515 | isUnion = true; |
Chris Lattner | 12f65f6 | 2007-12-02 18:52:07 +0000 | [diff] [blame] | 516 | } |
Devang Patel | b9b00ad | 2007-10-23 20:28:39 +0000 | [diff] [blame] | 517 | |
| 518 | FieldDecl *Field = E->getMemberDecl(); |
Eli Friedman | 472778e | 2008-02-09 08:50:58 +0000 | [diff] [blame] | 519 | return EmitLValueForField(BaseValue, Field, isUnion); |
| 520 | } |
Devang Patel | b9b00ad | 2007-10-23 20:28:39 +0000 | [diff] [blame] | 521 | |
Eli Friedman | 472778e | 2008-02-09 08:50:58 +0000 | [diff] [blame] | 522 | LValue CodeGenFunction::EmitLValueForField(llvm::Value* BaseValue, |
| 523 | FieldDecl* Field, |
| 524 | bool isUnion) |
| 525 | { |
| 526 | llvm::Value *V; |
| 527 | unsigned idx = CGM.getTypes().getLLVMFieldNo(Field); |
Lauro Ramos Venancio | d957aa0 | 2008-02-07 19:29:53 +0000 | [diff] [blame] | 528 | |
Chris Lattner | 36b6a0a | 2008-03-19 05:19:41 +0000 | [diff] [blame] | 529 | if (!Field->isBitField()) { |
| 530 | V = Builder.CreateStructGEP(BaseValue, idx, "tmp"); |
| 531 | } else { |
| 532 | const llvm::Type *FieldTy = ConvertType(Field->getType()); |
| 533 | const llvm::PointerType *BaseTy = |
Lauro Ramos Venancio | d957aa0 | 2008-02-07 19:29:53 +0000 | [diff] [blame] | 534 | cast<llvm::PointerType>(BaseValue->getType()); |
| 535 | unsigned AS = BaseTy->getAddressSpace(); |
| 536 | BaseValue = Builder.CreateBitCast(BaseValue, |
| 537 | llvm::PointerType::get(FieldTy, AS), |
| 538 | "tmp"); |
| 539 | V = Builder.CreateGEP(BaseValue, |
| 540 | llvm::ConstantInt::get(llvm::Type::Int32Ty, idx), |
| 541 | "tmp"); |
Lauro Ramos Venancio | d957aa0 | 2008-02-07 19:29:53 +0000 | [diff] [blame] | 542 | } |
Chris Lattner | 36b6a0a | 2008-03-19 05:19:41 +0000 | [diff] [blame] | 543 | |
Devang Patel | abad06c | 2007-10-26 19:42:18 +0000 | [diff] [blame] | 544 | // Match union field type. |
Lauro Ramos Venancio | d957aa0 | 2008-02-07 19:29:53 +0000 | [diff] [blame] | 545 | if (isUnion) { |
Devang Patel | abad06c | 2007-10-26 19:42:18 +0000 | [diff] [blame] | 546 | const llvm::Type * FieldTy = ConvertType(Field->getType()); |
Devang Patel | e9b8c0a | 2007-10-30 20:59:40 +0000 | [diff] [blame] | 547 | const llvm::PointerType * BaseTy = |
| 548 | cast<llvm::PointerType>(BaseValue->getType()); |
Devang Patel | abad06c | 2007-10-26 19:42:18 +0000 | [diff] [blame] | 549 | if (FieldTy != BaseTy->getElementType()) { |
Christopher Lamb | d3ba3f4 | 2007-12-29 04:06:57 +0000 | [diff] [blame] | 550 | unsigned AS = BaseTy->getAddressSpace(); |
Christopher Lamb | ddc23f3 | 2007-12-17 01:11:20 +0000 | [diff] [blame] | 551 | V = Builder.CreateBitCast(V, |
Christopher Lamb | d3ba3f4 | 2007-12-29 04:06:57 +0000 | [diff] [blame] | 552 | llvm::PointerType::get(FieldTy, AS), |
Christopher Lamb | ddc23f3 | 2007-12-17 01:11:20 +0000 | [diff] [blame] | 553 | "tmp"); |
Devang Patel | abad06c | 2007-10-26 19:42:18 +0000 | [diff] [blame] | 554 | } |
| 555 | } |
Lauro Ramos Venancio | 3b8c22d | 2008-01-22 20:17:04 +0000 | [diff] [blame] | 556 | |
Chris Lattner | 36b6a0a | 2008-03-19 05:19:41 +0000 | [diff] [blame] | 557 | if (!Field->isBitField()) |
Lauro Ramos Venancio | 3b8c22d | 2008-01-22 20:17:04 +0000 | [diff] [blame] | 558 | return LValue::MakeAddr(V); |
Chris Lattner | 36b6a0a | 2008-03-19 05:19:41 +0000 | [diff] [blame] | 559 | |
| 560 | CodeGenTypes::BitFieldInfo bitFieldInfo = |
| 561 | CGM.getTypes().getBitFieldInfo(Field); |
| 562 | return LValue::MakeBitfield(V, bitFieldInfo.Begin, bitFieldInfo.Size, |
| 563 | Field->getType()->isSignedIntegerType()); |
Devang Patel | b9b00ad | 2007-10-23 20:28:39 +0000 | [diff] [blame] | 564 | } |
| 565 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 566 | //===--------------------------------------------------------------------===// |
| 567 | // Expression Emission |
| 568 | //===--------------------------------------------------------------------===// |
| 569 | |
Chris Lattner | 7016a70 | 2007-08-20 22:37:10 +0000 | [diff] [blame] | 570 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 571 | RValue CodeGenFunction::EmitCallExpr(const CallExpr *E) { |
Anders Carlsson | 022012e | 2007-08-20 18:05:56 +0000 | [diff] [blame] | 572 | if (const ImplicitCastExpr *IcExpr = |
| 573 | dyn_cast<const ImplicitCastExpr>(E->getCallee())) |
| 574 | if (const DeclRefExpr *DRExpr = |
| 575 | dyn_cast<const DeclRefExpr>(IcExpr->getSubExpr())) |
| 576 | if (const FunctionDecl *FDecl = |
| 577 | dyn_cast<const FunctionDecl>(DRExpr->getDecl())) |
| 578 | if (unsigned builtinID = FDecl->getIdentifier()->getBuiltinID()) |
| 579 | return EmitBuiltinExpr(builtinID, E); |
| 580 | |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 581 | llvm::Value *Callee = EmitScalarExpr(E->getCallee()); |
Eli Friedman | 5193b8a | 2008-01-30 01:32:06 +0000 | [diff] [blame] | 582 | return EmitCallExpr(Callee, E->getCallee()->getType(), |
| 583 | E->arg_begin(), E->getNumArgs()); |
Nate Begeman | e2ce1d9 | 2008-01-17 17:46:27 +0000 | [diff] [blame] | 584 | } |
| 585 | |
Eli Friedman | 5193b8a | 2008-01-30 01:32:06 +0000 | [diff] [blame] | 586 | RValue CodeGenFunction::EmitCallExpr(Expr *FnExpr, Expr *const *Args, |
| 587 | unsigned NumArgs) { |
Nate Begeman | e2ce1d9 | 2008-01-17 17:46:27 +0000 | [diff] [blame] | 588 | llvm::Value *Callee = EmitScalarExpr(FnExpr); |
Eli Friedman | 5193b8a | 2008-01-30 01:32:06 +0000 | [diff] [blame] | 589 | return EmitCallExpr(Callee, FnExpr->getType(), Args, NumArgs); |
Chris Lattner | c5e940f | 2007-08-31 04:44:06 +0000 | [diff] [blame] | 590 | } |
| 591 | |
Christopher Lamb | 22c940e | 2007-12-29 05:02:41 +0000 | [diff] [blame] | 592 | LValue CodeGenFunction::EmitCallExprLValue(const CallExpr *E) { |
| 593 | // Can only get l-value for call expression returning aggregate type |
| 594 | RValue RV = EmitCallExpr(E); |
| 595 | return LValue::MakeAddr(RV.getAggregateAddr()); |
| 596 | } |
| 597 | |
Chris Lattner | 391d77a | 2008-03-30 23:03:07 +0000 | [diff] [blame] | 598 | LValue CodeGenFunction::EmitObjCIvarRefLValue(const ObjCIvarRefExpr *E) { |
| 599 | // Objective-C objects are traditionally C structures with their layout |
| 600 | // defined at compile-time. In some implementations, their layout is not |
| 601 | // defined until run time in order to allow instance variables to be added to |
| 602 | // a class without recompiling all of the subclasses. If this is the case |
| 603 | // then the CGObjCRuntime subclass must return true to LateBoundIvars and |
| 604 | // implement the lookup itself. |
Chris Lattner | ce5605e | 2008-03-30 23:25:33 +0000 | [diff] [blame] | 605 | if (CGM.getObjCRuntime()->LateBoundIVars()) { |
Chris Lattner | 391d77a | 2008-03-30 23:03:07 +0000 | [diff] [blame] | 606 | assert(0 && "FIXME: Implement support for late-bound instance variables"); |
| 607 | return LValue(); // Not reached. |
| 608 | } |
Chris Lattner | ce5605e | 2008-03-30 23:25:33 +0000 | [diff] [blame] | 609 | |
| 610 | // Get a structure type for the object |
| 611 | QualType ExprTy = E->getBase()->getType(); |
| 612 | const llvm::Type *ObjectType = ConvertType(ExprTy); |
| 613 | // TODO: Add a special case for isa (index 0) |
| 614 | // Work out which index the ivar is |
| 615 | const ObjCIvarDecl *Decl = E->getDecl(); |
| 616 | unsigned Index = CGM.getTypes().getLLVMFieldNo(Decl); |
Chris Lattner | 391d77a | 2008-03-30 23:03:07 +0000 | [diff] [blame] | 617 | |
Chris Lattner | ce5605e | 2008-03-30 23:25:33 +0000 | [diff] [blame] | 618 | // Get object pointer and coerce object pointer to correct type. |
| 619 | llvm::Value *Object = EmitLValue(E->getBase()).getAddress(); |
Chris Lattner | c8aa5f1 | 2008-04-04 04:07:35 +0000 | [diff] [blame] | 620 | Object = Builder.CreateLoad(Object, E->getDecl()->getName()); |
Chris Lattner | ce5605e | 2008-03-30 23:25:33 +0000 | [diff] [blame] | 621 | if (Object->getType() != ObjectType) |
| 622 | Object = Builder.CreateBitCast(Object, ObjectType); |
Chris Lattner | c8aa5f1 | 2008-04-04 04:07:35 +0000 | [diff] [blame] | 623 | |
Chris Lattner | ce5605e | 2008-03-30 23:25:33 +0000 | [diff] [blame] | 624 | |
| 625 | // Return a pointer to the right element. |
| 626 | return LValue::MakeAddr(Builder.CreateStructGEP(Object, Index, |
| 627 | Decl->getName())); |
Chris Lattner | 391d77a | 2008-03-30 23:03:07 +0000 | [diff] [blame] | 628 | } |
| 629 | |
Nate Begeman | e2ce1d9 | 2008-01-17 17:46:27 +0000 | [diff] [blame] | 630 | RValue CodeGenFunction::EmitCallExpr(llvm::Value *Callee, QualType FnType, |
Eli Friedman | 5193b8a | 2008-01-30 01:32:06 +0000 | [diff] [blame] | 631 | Expr *const *ArgExprs, unsigned NumArgs) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 632 | // The callee type will always be a pointer to function type, get the function |
| 633 | // type. |
Nate Begeman | e2ce1d9 | 2008-01-17 17:46:27 +0000 | [diff] [blame] | 634 | FnType = cast<PointerType>(FnType.getCanonicalType())->getPointeeType(); |
| 635 | QualType ResultType = cast<FunctionType>(FnType)->getResultType(); |
Eli Friedman | 5193b8a | 2008-01-30 01:32:06 +0000 | [diff] [blame] | 636 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 637 | llvm::SmallVector<llvm::Value*, 16> Args; |
| 638 | |
Chris Lattner | cc666af | 2007-08-10 17:02:28 +0000 | [diff] [blame] | 639 | // Handle struct-return functions by passing a pointer to the location that |
| 640 | // we would like to return into. |
Nate Begeman | e2ce1d9 | 2008-01-17 17:46:27 +0000 | [diff] [blame] | 641 | if (hasAggregateLLVMType(ResultType)) { |
Chris Lattner | cc666af | 2007-08-10 17:02:28 +0000 | [diff] [blame] | 642 | // Create a temporary alloca to hold the result of the call. :( |
Nate Begeman | e2ce1d9 | 2008-01-17 17:46:27 +0000 | [diff] [blame] | 643 | Args.push_back(CreateTempAlloca(ConvertType(ResultType))); |
Chris Lattner | cc666af | 2007-08-10 17:02:28 +0000 | [diff] [blame] | 644 | // FIXME: set the stret attribute on the argument. |
| 645 | } |
| 646 | |
Nate Begeman | e2ce1d9 | 2008-01-17 17:46:27 +0000 | [diff] [blame] | 647 | for (unsigned i = 0, e = NumArgs; i != e; ++i) { |
| 648 | QualType ArgTy = ArgExprs[i]->getType(); |
Eli Friedman | 472778e | 2008-02-09 08:50:58 +0000 | [diff] [blame] | 649 | |
Chris Lattner | 660ac12 | 2007-08-26 22:55:13 +0000 | [diff] [blame] | 650 | if (!hasAggregateLLVMType(ArgTy)) { |
| 651 | // Scalar argument is passed by-value. |
Nate Begeman | e2ce1d9 | 2008-01-17 17:46:27 +0000 | [diff] [blame] | 652 | Args.push_back(EmitScalarExpr(ArgExprs[i])); |
Chris Lattner | 9b2dc28 | 2008-04-04 16:54:41 +0000 | [diff] [blame] | 653 | } else if (ArgTy->isAnyComplexType()) { |
Chris Lattner | 660ac12 | 2007-08-26 22:55:13 +0000 | [diff] [blame] | 654 | // Make a temporary alloca to pass the argument. |
| 655 | llvm::Value *DestMem = CreateTempAlloca(ConvertType(ArgTy)); |
Nate Begeman | e2ce1d9 | 2008-01-17 17:46:27 +0000 | [diff] [blame] | 656 | EmitComplexExprIntoAddr(ArgExprs[i], DestMem, false); |
Chris Lattner | 660ac12 | 2007-08-26 22:55:13 +0000 | [diff] [blame] | 657 | Args.push_back(DestMem); |
| 658 | } else { |
| 659 | llvm::Value *DestMem = CreateTempAlloca(ConvertType(ArgTy)); |
Nate Begeman | e2ce1d9 | 2008-01-17 17:46:27 +0000 | [diff] [blame] | 660 | EmitAggExpr(ArgExprs[i], DestMem, false); |
Chris Lattner | 660ac12 | 2007-08-26 22:55:13 +0000 | [diff] [blame] | 661 | Args.push_back(DestMem); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 662 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 663 | } |
| 664 | |
Nate Begeman | ec9426c | 2008-03-09 03:09:36 +0000 | [diff] [blame] | 665 | llvm::CallInst *CI = Builder.CreateCall(Callee,&Args[0],&Args[0]+Args.size()); |
| 666 | if (const llvm::Function *F = dyn_cast<llvm::Function>(Callee)) |
| 667 | CI->setCallingConv(F->getCallingConv()); |
| 668 | if (CI->getType() != llvm::Type::VoidTy) |
| 669 | CI->setName("call"); |
Chris Lattner | 9b2dc28 | 2008-04-04 16:54:41 +0000 | [diff] [blame] | 670 | else if (ResultType->isAnyComplexType()) |
Chris Lattner | 9b65551 | 2007-08-31 22:49:20 +0000 | [diff] [blame] | 671 | return RValue::getComplex(LoadComplexFromAddr(Args[0], false)); |
Nate Begeman | e2ce1d9 | 2008-01-17 17:46:27 +0000 | [diff] [blame] | 672 | else if (hasAggregateLLVMType(ResultType)) |
Chris Lattner | cc666af | 2007-08-10 17:02:28 +0000 | [diff] [blame] | 673 | // Struct return. |
| 674 | return RValue::getAggregate(Args[0]); |
Chris Lattner | 2202bce | 2007-11-30 17:56:23 +0000 | [diff] [blame] | 675 | else { |
| 676 | // void return. |
Nate Begeman | e2ce1d9 | 2008-01-17 17:46:27 +0000 | [diff] [blame] | 677 | assert(ResultType->isVoidType() && "Should only have a void expr here"); |
Nate Begeman | ec9426c | 2008-03-09 03:09:36 +0000 | [diff] [blame] | 678 | CI = 0; |
Chris Lattner | 2202bce | 2007-11-30 17:56:23 +0000 | [diff] [blame] | 679 | } |
Chris Lattner | cc666af | 2007-08-10 17:02:28 +0000 | [diff] [blame] | 680 | |
Nate Begeman | ec9426c | 2008-03-09 03:09:36 +0000 | [diff] [blame] | 681 | return RValue::get(CI); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 682 | } |