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