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; |
| 40 | if (!E->getType()->isComplexType()) |
| 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)); |
| 54 | else if (E->getType()->isComplexType()) |
| 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 | dc4d280 | 2007-12-02 01:49:16 +0000 | [diff] [blame] | 85 | WarnUnsupported(E, "l-value expression"); |
Christopher Lamb | ddc23f3 | 2007-12-17 01:11:20 +0000 | [diff] [blame] | 86 | llvm::Type *Ty = llvm::PointerType::getUnqual(ConvertType(E->getType())); |
Chris Lattner | 7013c8c | 2007-08-26 05:06:40 +0000 | [diff] [blame] | 87 | return LValue::MakeAddr(llvm::UndefValue::get(Ty)); |
| 88 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 89 | |
Christopher Lamb | 22c940e | 2007-12-29 05:02:41 +0000 | [diff] [blame] | 90 | case Expr::CallExprClass: return EmitCallExprLValue(cast<CallExpr>(E)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 91 | case Expr::DeclRefExprClass: return EmitDeclRefLValue(cast<DeclRefExpr>(E)); |
| 92 | case Expr::ParenExprClass:return EmitLValue(cast<ParenExpr>(E)->getSubExpr()); |
Anders Carlsson | 2274266 | 2007-07-21 05:21:51 +0000 | [diff] [blame] | 93 | case Expr::PreDefinedExprClass: |
| 94 | return EmitPreDefinedLValue(cast<PreDefinedExpr>(E)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 95 | case Expr::StringLiteralClass: |
| 96 | return EmitStringLiteralLValue(cast<StringLiteral>(E)); |
| 97 | |
| 98 | case Expr::UnaryOperatorClass: |
| 99 | return EmitUnaryOpLValue(cast<UnaryOperator>(E)); |
| 100 | case Expr::ArraySubscriptExprClass: |
| 101 | return EmitArraySubscriptExpr(cast<ArraySubscriptExpr>(E)); |
Chris Lattner | 6481a57 | 2007-08-03 17:31:20 +0000 | [diff] [blame] | 102 | case Expr::OCUVectorElementExprClass: |
| 103 | return EmitOCUVectorElementExpr(cast<OCUVectorElementExpr>(E)); |
Devang Patel | b9b00ad | 2007-10-23 20:28:39 +0000 | [diff] [blame] | 104 | case Expr::MemberExprClass: return EmitMemberExpr(cast<MemberExpr>(E)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 105 | } |
| 106 | } |
| 107 | |
| 108 | /// EmitLoadOfLValue - Given an expression that represents a value lvalue, |
| 109 | /// this method emits the address of the lvalue, then loads the result as an |
| 110 | /// rvalue, returning the rvalue. |
| 111 | RValue CodeGenFunction::EmitLoadOfLValue(LValue LV, QualType ExprType) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 112 | if (LV.isSimple()) { |
| 113 | llvm::Value *Ptr = LV.getAddress(); |
| 114 | const llvm::Type *EltTy = |
| 115 | cast<llvm::PointerType>(Ptr->getType())->getElementType(); |
| 116 | |
| 117 | // Simple scalar l-value. |
| 118 | if (EltTy->isFirstClassType()) |
| 119 | return RValue::get(Builder.CreateLoad(Ptr, "tmp")); |
| 120 | |
Chris Lattner | 883f6a7 | 2007-08-11 00:04:45 +0000 | [diff] [blame] | 121 | assert(ExprType->isFunctionType() && "Unknown scalar value"); |
| 122 | return RValue::get(Ptr); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | if (LV.isVectorElt()) { |
| 126 | llvm::Value *Vec = Builder.CreateLoad(LV.getVectorAddr(), "tmp"); |
| 127 | return RValue::get(Builder.CreateExtractElement(Vec, LV.getVectorIdx(), |
| 128 | "vecext")); |
| 129 | } |
Chris Lattner | 46ea8eb | 2007-08-03 00:16:29 +0000 | [diff] [blame] | 130 | |
| 131 | // If this is a reference to a subset of the elements of a vector, either |
| 132 | // shuffle the input or extract/insert them as appropriate. |
Chris Lattner | 6481a57 | 2007-08-03 17:31:20 +0000 | [diff] [blame] | 133 | if (LV.isOCUVectorElt()) |
| 134 | return EmitLoadOfOCUElementLValue(LV, ExprType); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 135 | |
| 136 | assert(0 && "Bitfield ref not impl!"); |
Chris Lattner | b1776cb | 2007-09-16 19:23:47 +0000 | [diff] [blame] | 137 | //an invalid RValue, but the assert will |
| 138 | //ensure that this point is never reached |
| 139 | return RValue(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 140 | } |
| 141 | |
Chris Lattner | 34cdc86 | 2007-08-03 16:18:34 +0000 | [diff] [blame] | 142 | // If this is a reference to a subset of the elements of a vector, either |
| 143 | // shuffle the input or extract/insert them as appropriate. |
Chris Lattner | 6481a57 | 2007-08-03 17:31:20 +0000 | [diff] [blame] | 144 | RValue CodeGenFunction::EmitLoadOfOCUElementLValue(LValue LV, |
Chris Lattner | cf60cd2 | 2007-08-10 17:10:08 +0000 | [diff] [blame] | 145 | QualType ExprType) { |
Chris Lattner | 34cdc86 | 2007-08-03 16:18:34 +0000 | [diff] [blame] | 146 | llvm::Value *Vec = Builder.CreateLoad(LV.getOCUVectorAddr(), "tmp"); |
| 147 | |
Chris Lattner | 6481a57 | 2007-08-03 17:31:20 +0000 | [diff] [blame] | 148 | unsigned EncFields = LV.getOCUVectorElts(); |
Chris Lattner | 34cdc86 | 2007-08-03 16:18:34 +0000 | [diff] [blame] | 149 | |
| 150 | // If the result of the expression is a non-vector type, we must be |
| 151 | // extracting a single element. Just codegen as an extractelement. |
Chris Lattner | cf60cd2 | 2007-08-10 17:10:08 +0000 | [diff] [blame] | 152 | const VectorType *ExprVT = ExprType->getAsVectorType(); |
| 153 | if (!ExprVT) { |
Chris Lattner | 6481a57 | 2007-08-03 17:31:20 +0000 | [diff] [blame] | 154 | unsigned InIdx = OCUVectorElementExpr::getAccessedFieldNo(0, EncFields); |
Chris Lattner | 34cdc86 | 2007-08-03 16:18:34 +0000 | [diff] [blame] | 155 | llvm::Value *Elt = llvm::ConstantInt::get(llvm::Type::Int32Ty, InIdx); |
| 156 | return RValue::get(Builder.CreateExtractElement(Vec, Elt, "tmp")); |
| 157 | } |
| 158 | |
| 159 | // If the source and destination have the same number of elements, use a |
| 160 | // vector shuffle instead of insert/extracts. |
Chris Lattner | cf60cd2 | 2007-08-10 17:10:08 +0000 | [diff] [blame] | 161 | unsigned NumResultElts = ExprVT->getNumElements(); |
Chris Lattner | 34cdc86 | 2007-08-03 16:18:34 +0000 | [diff] [blame] | 162 | unsigned NumSourceElts = |
| 163 | cast<llvm::VectorType>(Vec->getType())->getNumElements(); |
| 164 | |
| 165 | if (NumResultElts == NumSourceElts) { |
| 166 | llvm::SmallVector<llvm::Constant*, 4> Mask; |
| 167 | for (unsigned i = 0; i != NumResultElts; ++i) { |
Chris Lattner | 6481a57 | 2007-08-03 17:31:20 +0000 | [diff] [blame] | 168 | unsigned InIdx = OCUVectorElementExpr::getAccessedFieldNo(i, EncFields); |
Chris Lattner | 34cdc86 | 2007-08-03 16:18:34 +0000 | [diff] [blame] | 169 | Mask.push_back(llvm::ConstantInt::get(llvm::Type::Int32Ty, InIdx)); |
| 170 | } |
| 171 | |
| 172 | llvm::Value *MaskV = llvm::ConstantVector::get(&Mask[0], Mask.size()); |
| 173 | Vec = Builder.CreateShuffleVector(Vec, |
| 174 | llvm::UndefValue::get(Vec->getType()), |
| 175 | MaskV, "tmp"); |
| 176 | return RValue::get(Vec); |
| 177 | } |
| 178 | |
| 179 | // Start out with an undef of the result type. |
| 180 | llvm::Value *Result = llvm::UndefValue::get(ConvertType(ExprType)); |
| 181 | |
| 182 | // Extract/Insert each element of the result. |
| 183 | for (unsigned i = 0; i != NumResultElts; ++i) { |
Chris Lattner | 6481a57 | 2007-08-03 17:31:20 +0000 | [diff] [blame] | 184 | unsigned InIdx = OCUVectorElementExpr::getAccessedFieldNo(i, EncFields); |
Chris Lattner | 34cdc86 | 2007-08-03 16:18:34 +0000 | [diff] [blame] | 185 | llvm::Value *Elt = llvm::ConstantInt::get(llvm::Type::Int32Ty, InIdx); |
| 186 | Elt = Builder.CreateExtractElement(Vec, Elt, "tmp"); |
| 187 | |
| 188 | llvm::Value *OutIdx = llvm::ConstantInt::get(llvm::Type::Int32Ty, i); |
| 189 | Result = Builder.CreateInsertElement(Result, Elt, OutIdx, "tmp"); |
| 190 | } |
| 191 | |
| 192 | return RValue::get(Result); |
| 193 | } |
| 194 | |
| 195 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 196 | |
| 197 | /// EmitStoreThroughLValue - Store the specified rvalue into the specified |
| 198 | /// lvalue, where both are guaranteed to the have the same type, and that type |
| 199 | /// is 'Ty'. |
| 200 | void CodeGenFunction::EmitStoreThroughLValue(RValue Src, LValue Dst, |
| 201 | QualType Ty) { |
Chris Lattner | 017d6aa | 2007-08-03 16:28:33 +0000 | [diff] [blame] | 202 | if (!Dst.isSimple()) { |
| 203 | if (Dst.isVectorElt()) { |
| 204 | // Read/modify/write the vector, inserting the new element. |
| 205 | // FIXME: Volatility. |
| 206 | llvm::Value *Vec = Builder.CreateLoad(Dst.getVectorAddr(), "tmp"); |
Chris Lattner | 9b65551 | 2007-08-31 22:49:20 +0000 | [diff] [blame] | 207 | Vec = Builder.CreateInsertElement(Vec, Src.getScalarVal(), |
Chris Lattner | 017d6aa | 2007-08-03 16:28:33 +0000 | [diff] [blame] | 208 | Dst.getVectorIdx(), "vecins"); |
| 209 | Builder.CreateStore(Vec, Dst.getVectorAddr()); |
| 210 | return; |
| 211 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 212 | |
Chris Lattner | 017d6aa | 2007-08-03 16:28:33 +0000 | [diff] [blame] | 213 | // If this is an update of elements of a vector, insert them as appropriate. |
Chris Lattner | 6481a57 | 2007-08-03 17:31:20 +0000 | [diff] [blame] | 214 | if (Dst.isOCUVectorElt()) |
Chris Lattner | 017d6aa | 2007-08-03 16:28:33 +0000 | [diff] [blame] | 215 | return EmitStoreThroughOCUComponentLValue(Src, Dst, Ty); |
| 216 | |
| 217 | assert(0 && "FIXME: Don't support store to bitfield yet"); |
| 218 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 219 | |
| 220 | llvm::Value *DstAddr = Dst.getAddress(); |
Chris Lattner | 883f6a7 | 2007-08-11 00:04:45 +0000 | [diff] [blame] | 221 | assert(Src.isScalar() && "Can't emit an agg store with this method"); |
| 222 | // FIXME: Handle volatility etc. |
Chris Lattner | 9b65551 | 2007-08-31 22:49:20 +0000 | [diff] [blame] | 223 | const llvm::Type *SrcTy = Src.getScalarVal()->getType(); |
Christopher Lamb | ddc23f3 | 2007-12-17 01:11:20 +0000 | [diff] [blame] | 224 | const llvm::PointerType *DstPtr = cast<llvm::PointerType>(DstAddr->getType()); |
| 225 | const llvm::Type *AddrTy = DstPtr->getElementType(); |
| 226 | unsigned AS = DstPtr->getAddressSpace(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 227 | |
Chris Lattner | 883f6a7 | 2007-08-11 00:04:45 +0000 | [diff] [blame] | 228 | if (AddrTy != SrcTy) |
Christopher Lamb | ddc23f3 | 2007-12-17 01:11:20 +0000 | [diff] [blame] | 229 | DstAddr = Builder.CreateBitCast(DstAddr, |
| 230 | llvm::PointerType::get(SrcTy, AS), |
Chris Lattner | 883f6a7 | 2007-08-11 00:04:45 +0000 | [diff] [blame] | 231 | "storetmp"); |
Chris Lattner | 9b65551 | 2007-08-31 22:49:20 +0000 | [diff] [blame] | 232 | Builder.CreateStore(Src.getScalarVal(), DstAddr); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 233 | } |
| 234 | |
Devang Patel | e9b8c0a | 2007-10-30 20:59:40 +0000 | [diff] [blame] | 235 | void CodeGenFunction::EmitStoreThroughOCUComponentLValue(RValue Src, LValue Dst, |
Chris Lattner | 017d6aa | 2007-08-03 16:28:33 +0000 | [diff] [blame] | 236 | QualType Ty) { |
| 237 | // This access turns into a read/modify/write of the vector. Load the input |
| 238 | // value now. |
| 239 | llvm::Value *Vec = Builder.CreateLoad(Dst.getOCUVectorAddr(), "tmp"); |
| 240 | // FIXME: Volatility. |
Chris Lattner | 6481a57 | 2007-08-03 17:31:20 +0000 | [diff] [blame] | 241 | unsigned EncFields = Dst.getOCUVectorElts(); |
Chris Lattner | 017d6aa | 2007-08-03 16:28:33 +0000 | [diff] [blame] | 242 | |
Chris Lattner | 9b65551 | 2007-08-31 22:49:20 +0000 | [diff] [blame] | 243 | llvm::Value *SrcVal = Src.getScalarVal(); |
Chris Lattner | 017d6aa | 2007-08-03 16:28:33 +0000 | [diff] [blame] | 244 | |
Chris Lattner | 7e6b51b | 2007-08-03 16:37:04 +0000 | [diff] [blame] | 245 | if (const VectorType *VTy = Ty->getAsVectorType()) { |
| 246 | unsigned NumSrcElts = VTy->getNumElements(); |
| 247 | |
| 248 | // Extract/Insert each element. |
| 249 | for (unsigned i = 0; i != NumSrcElts; ++i) { |
| 250 | llvm::Value *Elt = llvm::ConstantInt::get(llvm::Type::Int32Ty, i); |
| 251 | Elt = Builder.CreateExtractElement(SrcVal, Elt, "tmp"); |
| 252 | |
Chris Lattner | 6481a57 | 2007-08-03 17:31:20 +0000 | [diff] [blame] | 253 | unsigned Idx = OCUVectorElementExpr::getAccessedFieldNo(i, EncFields); |
Chris Lattner | 7e6b51b | 2007-08-03 16:37:04 +0000 | [diff] [blame] | 254 | llvm::Value *OutIdx = llvm::ConstantInt::get(llvm::Type::Int32Ty, Idx); |
| 255 | Vec = Builder.CreateInsertElement(Vec, Elt, OutIdx, "tmp"); |
| 256 | } |
| 257 | } else { |
| 258 | // If the Src is a scalar (not a vector) it must be updating one element. |
Chris Lattner | 6481a57 | 2007-08-03 17:31:20 +0000 | [diff] [blame] | 259 | unsigned InIdx = OCUVectorElementExpr::getAccessedFieldNo(0, EncFields); |
Chris Lattner | 017d6aa | 2007-08-03 16:28:33 +0000 | [diff] [blame] | 260 | llvm::Value *Elt = llvm::ConstantInt::get(llvm::Type::Int32Ty, InIdx); |
| 261 | Vec = Builder.CreateInsertElement(Vec, SrcVal, Elt, "tmp"); |
Chris Lattner | 017d6aa | 2007-08-03 16:28:33 +0000 | [diff] [blame] | 262 | } |
| 263 | |
Chris Lattner | 017d6aa | 2007-08-03 16:28:33 +0000 | [diff] [blame] | 264 | Builder.CreateStore(Vec, Dst.getOCUVectorAddr()); |
| 265 | } |
| 266 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 267 | |
| 268 | LValue CodeGenFunction::EmitDeclRefLValue(const DeclRefExpr *E) { |
Steve Naroff | 8e74c93 | 2007-09-13 21:41:19 +0000 | [diff] [blame] | 269 | const ValueDecl *D = E->getDecl(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 270 | if (isa<BlockVarDecl>(D) || isa<ParmVarDecl>(D)) { |
| 271 | llvm::Value *V = LocalDeclMap[D]; |
| 272 | assert(V && "BlockVarDecl not entered in LocalDeclMap?"); |
| 273 | return LValue::MakeAddr(V); |
Chris Lattner | 9cd4fe4 | 2007-12-02 07:09:19 +0000 | [diff] [blame] | 274 | } else if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
| 275 | return LValue::MakeAddr(CGM.GetAddrOfFunctionDecl(FD, false)); |
| 276 | } else if (const FileVarDecl *FVD = dyn_cast<FileVarDecl>(D)) { |
Chris Lattner | 2b9d2ca | 2007-12-18 08:16:44 +0000 | [diff] [blame] | 277 | return LValue::MakeAddr(CGM.GetAddrOfGlobalVar(FVD, false)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 278 | } |
| 279 | assert(0 && "Unimp declref"); |
Chris Lattner | b1776cb | 2007-09-16 19:23:47 +0000 | [diff] [blame] | 280 | //an invalid LValue, but the assert will |
| 281 | //ensure that this point is never reached. |
| 282 | return LValue(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 283 | } |
| 284 | |
| 285 | LValue CodeGenFunction::EmitUnaryOpLValue(const UnaryOperator *E) { |
| 286 | // __extension__ doesn't affect lvalue-ness. |
| 287 | if (E->getOpcode() == UnaryOperator::Extension) |
| 288 | return EmitLValue(E->getSubExpr()); |
| 289 | |
Chris Lattner | 7da36f6 | 2007-10-30 22:53:42 +0000 | [diff] [blame] | 290 | switch (E->getOpcode()) { |
| 291 | default: assert(0 && "Unknown unary operator lvalue!"); |
| 292 | case UnaryOperator::Deref: |
| 293 | return LValue::MakeAddr(EmitScalarExpr(E->getSubExpr())); |
| 294 | case UnaryOperator::Real: |
| 295 | case UnaryOperator::Imag: |
| 296 | LValue LV = EmitLValue(E->getSubExpr()); |
| 297 | |
| 298 | llvm::Constant *Zero = llvm::ConstantInt::get(llvm::Type::Int32Ty, 0); |
| 299 | llvm::Constant *Idx = llvm::ConstantInt::get(llvm::Type::Int32Ty, |
| 300 | E->getOpcode() == UnaryOperator::Imag); |
| 301 | llvm::Value *Ops[] = {Zero, Idx}; |
| 302 | return LValue::MakeAddr(Builder.CreateGEP(LV.getAddress(), Ops, Ops+2, |
| 303 | "idx")); |
| 304 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 305 | } |
| 306 | |
| 307 | LValue CodeGenFunction::EmitStringLiteralLValue(const StringLiteral *E) { |
| 308 | assert(!E->isWide() && "FIXME: Wide strings not supported yet!"); |
| 309 | const char *StrData = E->getStrData(); |
| 310 | unsigned Len = E->getByteLength(); |
Chris Lattner | 45e8cbd | 2007-11-28 05:34:05 +0000 | [diff] [blame] | 311 | std::string StringLiteral(StrData, StrData+Len); |
| 312 | return LValue::MakeAddr(CGM.GetAddrOfConstantString(StringLiteral)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 313 | } |
| 314 | |
Anders Carlsson | 2274266 | 2007-07-21 05:21:51 +0000 | [diff] [blame] | 315 | LValue CodeGenFunction::EmitPreDefinedLValue(const PreDefinedExpr *E) { |
| 316 | std::string FunctionName(CurFuncDecl->getName()); |
| 317 | std::string GlobalVarName; |
| 318 | |
| 319 | switch (E->getIdentType()) { |
| 320 | default: |
| 321 | assert(0 && "unknown pre-defined ident type"); |
| 322 | case PreDefinedExpr::Func: |
| 323 | GlobalVarName = "__func__."; |
| 324 | break; |
| 325 | case PreDefinedExpr::Function: |
| 326 | GlobalVarName = "__FUNCTION__."; |
| 327 | break; |
| 328 | case PreDefinedExpr::PrettyFunction: |
| 329 | // FIXME:: Demangle C++ method names |
| 330 | GlobalVarName = "__PRETTY_FUNCTION__."; |
| 331 | break; |
| 332 | } |
| 333 | |
| 334 | GlobalVarName += CurFuncDecl->getName(); |
| 335 | |
| 336 | // FIXME: Can cache/reuse these within the module. |
| 337 | llvm::Constant *C=llvm::ConstantArray::get(FunctionName); |
| 338 | |
| 339 | // Create a global variable for this. |
| 340 | C = new llvm::GlobalVariable(C->getType(), true, |
| 341 | llvm::GlobalValue::InternalLinkage, |
| 342 | C, GlobalVarName, CurFn->getParent()); |
| 343 | llvm::Constant *Zero = llvm::Constant::getNullValue(llvm::Type::Int32Ty); |
| 344 | llvm::Constant *Zeros[] = { Zero, Zero }; |
| 345 | C = llvm::ConstantExpr::getGetElementPtr(C, Zeros, 2); |
| 346 | return LValue::MakeAddr(C); |
| 347 | } |
| 348 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 349 | LValue CodeGenFunction::EmitArraySubscriptExpr(const ArraySubscriptExpr *E) { |
Ted Kremenek | 2324512 | 2007-08-20 16:18:38 +0000 | [diff] [blame] | 350 | // 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] | 351 | llvm::Value *Idx = EmitScalarExpr(E->getIdx()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 352 | |
| 353 | // If the base is a vector type, then we are forming a vector element lvalue |
| 354 | // with this subscript. |
Ted Kremenek | 2324512 | 2007-08-20 16:18:38 +0000 | [diff] [blame] | 355 | if (E->getLHS()->getType()->isVectorType()) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 356 | // Emit the vector as an lvalue to get its address. |
Ted Kremenek | 2324512 | 2007-08-20 16:18:38 +0000 | [diff] [blame] | 357 | LValue LHS = EmitLValue(E->getLHS()); |
| 358 | assert(LHS.isSimple() && "Can only subscript lvalue vectors here!"); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 359 | // FIXME: This should properly sign/zero/extend or truncate Idx to i32. |
Ted Kremenek | 2324512 | 2007-08-20 16:18:38 +0000 | [diff] [blame] | 360 | return LValue::MakeVectorElt(LHS.getAddress(), Idx); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 361 | } |
| 362 | |
Ted Kremenek | 2324512 | 2007-08-20 16:18:38 +0000 | [diff] [blame] | 363 | // 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] | 364 | llvm::Value *Base = EmitScalarExpr(E->getBase()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 365 | |
Ted Kremenek | 2324512 | 2007-08-20 16:18:38 +0000 | [diff] [blame] | 366 | // Extend or truncate the index type to 32 or 64-bits. |
Chris Lattner | d4f0802 | 2007-08-08 17:43:05 +0000 | [diff] [blame] | 367 | QualType IdxTy = E->getIdx()->getType(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 368 | bool IdxSigned = IdxTy->isSignedIntegerType(); |
| 369 | unsigned IdxBitwidth = cast<llvm::IntegerType>(Idx->getType())->getBitWidth(); |
| 370 | if (IdxBitwidth != LLVMPointerWidth) |
| 371 | Idx = Builder.CreateIntCast(Idx, llvm::IntegerType::get(LLVMPointerWidth), |
| 372 | IdxSigned, "idxprom"); |
| 373 | |
| 374 | // We know that the pointer points to a type of the correct size, unless the |
| 375 | // size is a VLA. |
Chris Lattner | 590b664 | 2007-07-15 23:26:56 +0000 | [diff] [blame] | 376 | if (!E->getType()->isConstantSizeType(getContext())) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 377 | assert(0 && "VLA idx not implemented"); |
| 378 | return LValue::MakeAddr(Builder.CreateGEP(Base, Idx, "arrayidx")); |
| 379 | } |
| 380 | |
Chris Lattner | 349aaec | 2007-08-02 23:37:31 +0000 | [diff] [blame] | 381 | LValue CodeGenFunction:: |
Chris Lattner | 6481a57 | 2007-08-03 17:31:20 +0000 | [diff] [blame] | 382 | EmitOCUVectorElementExpr(const OCUVectorElementExpr *E) { |
Chris Lattner | 349aaec | 2007-08-02 23:37:31 +0000 | [diff] [blame] | 383 | // Emit the base vector as an l-value. |
| 384 | LValue Base = EmitLValue(E->getBase()); |
| 385 | assert(Base.isSimple() && "Can only subscript lvalue vectors here!"); |
| 386 | |
Chris Lattner | 6481a57 | 2007-08-03 17:31:20 +0000 | [diff] [blame] | 387 | return LValue::MakeOCUVectorElt(Base.getAddress(), |
| 388 | E->getEncodedElementAccess()); |
Chris Lattner | 349aaec | 2007-08-02 23:37:31 +0000 | [diff] [blame] | 389 | } |
| 390 | |
Devang Patel | b9b00ad | 2007-10-23 20:28:39 +0000 | [diff] [blame] | 391 | LValue CodeGenFunction::EmitMemberExpr(const MemberExpr *E) { |
| 392 | |
Devang Patel | fe2419a | 2007-12-11 21:33:16 +0000 | [diff] [blame] | 393 | bool isUnion = false; |
Devang Patel | 126a856 | 2007-10-24 22:26:28 +0000 | [diff] [blame] | 394 | Expr *BaseExpr = E->getBase(); |
Devang Patel | 126a856 | 2007-10-24 22:26:28 +0000 | [diff] [blame] | 395 | llvm::Value *BaseValue = NULL; |
Chris Lattner | 12f65f6 | 2007-12-02 18:52:07 +0000 | [diff] [blame] | 396 | |
| 397 | // 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] | 398 | if (E->isArrow()) { |
Devang Patel | 0a96118 | 2007-10-26 18:15:21 +0000 | [diff] [blame] | 399 | BaseValue = EmitScalarExpr(BaseExpr); |
Devang Patel | fe2419a | 2007-12-11 21:33:16 +0000 | [diff] [blame] | 400 | const PointerType *PTy = |
| 401 | cast<PointerType>(BaseExpr->getType().getCanonicalType()); |
| 402 | if (PTy->getPointeeType()->isUnionType()) |
| 403 | isUnion = true; |
| 404 | } |
Chris Lattner | 12f65f6 | 2007-12-02 18:52:07 +0000 | [diff] [blame] | 405 | else { |
| 406 | LValue BaseLV = EmitLValue(BaseExpr); |
| 407 | // FIXME: this isn't right for bitfields. |
| 408 | BaseValue = BaseLV.getAddress(); |
Devang Patel | fe2419a | 2007-12-11 21:33:16 +0000 | [diff] [blame] | 409 | if (BaseExpr->getType()->isUnionType()) |
| 410 | isUnion = true; |
Chris Lattner | 12f65f6 | 2007-12-02 18:52:07 +0000 | [diff] [blame] | 411 | } |
Devang Patel | b9b00ad | 2007-10-23 20:28:39 +0000 | [diff] [blame] | 412 | |
| 413 | FieldDecl *Field = E->getMemberDecl(); |
Devang Patel | 9418d31 | 2007-12-10 18:52:06 +0000 | [diff] [blame] | 414 | |
| 415 | assert (!Field->isBitField() && "Bit-field access is not yet implmented"); |
| 416 | |
Devang Patel | b9b00ad | 2007-10-23 20:28:39 +0000 | [diff] [blame] | 417 | unsigned idx = CGM.getTypes().getLLVMFieldNo(Field); |
| 418 | llvm::Value *Idxs[2] = { llvm::Constant::getNullValue(llvm::Type::Int32Ty), |
Devang Patel | 655bf3d | 2007-10-24 00:26:24 +0000 | [diff] [blame] | 419 | llvm::ConstantInt::get(llvm::Type::Int32Ty, idx) }; |
Devang Patel | b9b00ad | 2007-10-23 20:28:39 +0000 | [diff] [blame] | 420 | |
Devang Patel | abad06c | 2007-10-26 19:42:18 +0000 | [diff] [blame] | 421 | llvm::Value *V = Builder.CreateGEP(BaseValue,Idxs, Idxs + 2, "tmp"); |
| 422 | // Match union field type. |
Devang Patel | fe2419a | 2007-12-11 21:33:16 +0000 | [diff] [blame] | 423 | if (isUnion) { |
Devang Patel | abad06c | 2007-10-26 19:42:18 +0000 | [diff] [blame] | 424 | const llvm::Type * FieldTy = ConvertType(Field->getType()); |
Devang Patel | e9b8c0a | 2007-10-30 20:59:40 +0000 | [diff] [blame] | 425 | const llvm::PointerType * BaseTy = |
| 426 | cast<llvm::PointerType>(BaseValue->getType()); |
Devang Patel | abad06c | 2007-10-26 19:42:18 +0000 | [diff] [blame] | 427 | if (FieldTy != BaseTy->getElementType()) { |
Christopher Lamb | d3ba3f4 | 2007-12-29 04:06:57 +0000 | [diff] [blame] | 428 | unsigned AS = BaseTy->getAddressSpace(); |
Christopher Lamb | ddc23f3 | 2007-12-17 01:11:20 +0000 | [diff] [blame] | 429 | V = Builder.CreateBitCast(V, |
Christopher Lamb | d3ba3f4 | 2007-12-29 04:06:57 +0000 | [diff] [blame] | 430 | llvm::PointerType::get(FieldTy, AS), |
Christopher Lamb | ddc23f3 | 2007-12-17 01:11:20 +0000 | [diff] [blame] | 431 | "tmp"); |
Devang Patel | abad06c | 2007-10-26 19:42:18 +0000 | [diff] [blame] | 432 | } |
| 433 | } |
| 434 | return LValue::MakeAddr(V); |
Devang Patel | b9b00ad | 2007-10-23 20:28:39 +0000 | [diff] [blame] | 435 | |
| 436 | // FIXME: If record field does not have one to one match with llvm::StructType |
| 437 | // field then apply appropriate masks to select only member field bits. |
| 438 | } |
| 439 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 440 | //===--------------------------------------------------------------------===// |
| 441 | // Expression Emission |
| 442 | //===--------------------------------------------------------------------===// |
| 443 | |
Chris Lattner | 7016a70 | 2007-08-20 22:37:10 +0000 | [diff] [blame] | 444 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 445 | RValue CodeGenFunction::EmitCallExpr(const CallExpr *E) { |
Anders Carlsson | 022012e | 2007-08-20 18:05:56 +0000 | [diff] [blame] | 446 | if (const ImplicitCastExpr *IcExpr = |
| 447 | dyn_cast<const ImplicitCastExpr>(E->getCallee())) |
| 448 | if (const DeclRefExpr *DRExpr = |
| 449 | dyn_cast<const DeclRefExpr>(IcExpr->getSubExpr())) |
| 450 | if (const FunctionDecl *FDecl = |
| 451 | dyn_cast<const FunctionDecl>(DRExpr->getDecl())) |
| 452 | if (unsigned builtinID = FDecl->getIdentifier()->getBuiltinID()) |
| 453 | return EmitBuiltinExpr(builtinID, E); |
| 454 | |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 455 | llvm::Value *Callee = EmitScalarExpr(E->getCallee()); |
Chris Lattner | c5e940f | 2007-08-31 04:44:06 +0000 | [diff] [blame] | 456 | return EmitCallExpr(Callee, E); |
| 457 | } |
| 458 | |
Christopher Lamb | 22c940e | 2007-12-29 05:02:41 +0000 | [diff] [blame] | 459 | LValue CodeGenFunction::EmitCallExprLValue(const CallExpr *E) { |
| 460 | // Can only get l-value for call expression returning aggregate type |
| 461 | RValue RV = EmitCallExpr(E); |
| 462 | return LValue::MakeAddr(RV.getAggregateAddr()); |
| 463 | } |
| 464 | |
Chris Lattner | c5e940f | 2007-08-31 04:44:06 +0000 | [diff] [blame] | 465 | RValue CodeGenFunction::EmitCallExpr(llvm::Value *Callee, const CallExpr *E) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 466 | // The callee type will always be a pointer to function type, get the function |
| 467 | // type. |
Chris Lattner | d4f0802 | 2007-08-08 17:43:05 +0000 | [diff] [blame] | 468 | QualType CalleeTy = E->getCallee()->getType(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 469 | CalleeTy = cast<PointerType>(CalleeTy.getCanonicalType())->getPointeeType(); |
| 470 | |
| 471 | // Get information about the argument types. |
| 472 | FunctionTypeProto::arg_type_iterator ArgTyIt = 0, ArgTyEnd = 0; |
| 473 | |
| 474 | // Calling unprototyped functions provides no argument info. |
| 475 | if (const FunctionTypeProto *FTP = dyn_cast<FunctionTypeProto>(CalleeTy)) { |
| 476 | ArgTyIt = FTP->arg_type_begin(); |
| 477 | ArgTyEnd = FTP->arg_type_end(); |
| 478 | } |
| 479 | |
| 480 | llvm::SmallVector<llvm::Value*, 16> Args; |
| 481 | |
Chris Lattner | cc666af | 2007-08-10 17:02:28 +0000 | [diff] [blame] | 482 | // Handle struct-return functions by passing a pointer to the location that |
| 483 | // we would like to return into. |
| 484 | if (hasAggregateLLVMType(E->getType())) { |
| 485 | // Create a temporary alloca to hold the result of the call. :( |
| 486 | Args.push_back(CreateTempAlloca(ConvertType(E->getType()))); |
| 487 | // FIXME: set the stret attribute on the argument. |
| 488 | } |
| 489 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 490 | for (unsigned i = 0, e = E->getNumArgs(); i != e; ++i) { |
Chris Lattner | d4f0802 | 2007-08-08 17:43:05 +0000 | [diff] [blame] | 491 | QualType ArgTy = E->getArg(i)->getType(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 492 | |
Chris Lattner | 660ac12 | 2007-08-26 22:55:13 +0000 | [diff] [blame] | 493 | if (!hasAggregateLLVMType(ArgTy)) { |
| 494 | // Scalar argument is passed by-value. |
| 495 | Args.push_back(EmitScalarExpr(E->getArg(i))); |
Chris Lattner | 660ac12 | 2007-08-26 22:55:13 +0000 | [diff] [blame] | 496 | } else if (ArgTy->isComplexType()) { |
| 497 | // Make a temporary alloca to pass the argument. |
| 498 | llvm::Value *DestMem = CreateTempAlloca(ConvertType(ArgTy)); |
| 499 | EmitComplexExprIntoAddr(E->getArg(i), DestMem, false); |
| 500 | Args.push_back(DestMem); |
| 501 | } else { |
| 502 | llvm::Value *DestMem = CreateTempAlloca(ConvertType(ArgTy)); |
| 503 | EmitAggExpr(E->getArg(i), DestMem, false); |
| 504 | Args.push_back(DestMem); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 505 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 506 | } |
| 507 | |
Chris Lattner | bf98651 | 2007-08-01 06:24:52 +0000 | [diff] [blame] | 508 | llvm::Value *V = Builder.CreateCall(Callee, &Args[0], &Args[0]+Args.size()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 509 | if (V->getType() != llvm::Type::VoidTy) |
| 510 | V->setName("call"); |
Chris Lattner | 9b65551 | 2007-08-31 22:49:20 +0000 | [diff] [blame] | 511 | else if (E->getType()->isComplexType()) |
| 512 | return RValue::getComplex(LoadComplexFromAddr(Args[0], false)); |
Chris Lattner | cc666af | 2007-08-10 17:02:28 +0000 | [diff] [blame] | 513 | else if (hasAggregateLLVMType(E->getType())) |
| 514 | // Struct return. |
| 515 | return RValue::getAggregate(Args[0]); |
Chris Lattner | 2202bce | 2007-11-30 17:56:23 +0000 | [diff] [blame] | 516 | else { |
| 517 | // void return. |
| 518 | assert(E->getType()->isVoidType() && "Should only have a void expr here"); |
| 519 | V = 0; |
| 520 | } |
Chris Lattner | cc666af | 2007-08-10 17:02:28 +0000 | [diff] [blame] | 521 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 522 | return RValue::get(V); |
| 523 | } |