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" |
| 17 | #include "llvm/Constants.h" |
| 18 | #include "llvm/DerivedTypes.h" |
| 19 | #include "llvm/Function.h" |
| 20 | #include "llvm/GlobalVariable.h" |
| 21 | #include "llvm/Support/MathExtras.h" |
| 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 | cc50a51 | 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); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 42 | |
Chris Lattner | cc50a51 | 2007-08-26 16:46:58 +0000 | [diff] [blame] | 43 | return EmitComplexToScalarConversion(EmitComplexExpr(E), E->getType(),BoolTy); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 44 | } |
| 45 | |
Chris Lattner | e24c4cf | 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 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +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 | a52c889 | 2007-08-26 05:06:40 +0000 | [diff] [blame] | 84 | default: { |
Chris Lattner | e8f4963 | 2007-12-02 01:49:16 +0000 | [diff] [blame] | 85 | WarnUnsupported(E, "l-value expression"); |
Christopher Lamb | 4fe5e70 | 2007-12-17 01:11:20 +0000 | [diff] [blame] | 86 | llvm::Type *Ty = llvm::PointerType::getUnqual(ConvertType(E->getType())); |
Chris Lattner | a52c889 | 2007-08-26 05:06:40 +0000 | [diff] [blame] | 87 | return LValue::MakeAddr(llvm::UndefValue::get(Ty)); |
| 88 | } |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 89 | |
Christopher Lamb | ad327ba | 2007-12-29 05:02:41 +0000 | [diff] [blame] | 90 | case Expr::CallExprClass: return EmitCallExprLValue(cast<CallExpr>(E)); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 91 | case Expr::DeclRefExprClass: return EmitDeclRefLValue(cast<DeclRefExpr>(E)); |
| 92 | case Expr::ParenExprClass:return EmitLValue(cast<ParenExpr>(E)->getSubExpr()); |
| 93 | case Expr::PreDefinedExprClass: |
| 94 | return EmitPreDefinedLValue(cast<PreDefinedExpr>(E)); |
| 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 | a0d03a7 | 2007-08-03 17:31:20 +0000 | [diff] [blame] | 102 | case Expr::OCUVectorElementExprClass: |
| 103 | return EmitOCUVectorElementExpr(cast<OCUVectorElementExpr>(E)); |
Devang Patel | 41b6625 | 2007-10-23 20:28:39 +0000 | [diff] [blame] | 104 | case Expr::MemberExprClass: return EmitMemberExpr(cast<MemberExpr>(E)); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +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) { |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +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 | bdb8ffb | 2007-08-11 00:04:45 +0000 | [diff] [blame] | 121 | assert(ExprType->isFunctionType() && "Unknown scalar value"); |
| 122 | return RValue::get(Ptr); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +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 | a735fac | 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 | a0d03a7 | 2007-08-03 17:31:20 +0000 | [diff] [blame] | 133 | if (LV.isOCUVectorElt()) |
| 134 | return EmitLoadOfOCUElementLValue(LV, ExprType); |
Lauro Ramos Venancio | b40307c | 2008-01-22 20:17:04 +0000 | [diff] [blame] | 135 | |
| 136 | if (LV.isBitfield()) |
| 137 | return EmitLoadOfBitfieldLValue(LV, ExprType); |
| 138 | |
| 139 | assert(0 && "Unknown LValue type!"); |
Chris Lattner | 1d2b461 | 2007-09-16 19:23:47 +0000 | [diff] [blame] | 140 | //an invalid RValue, but the assert will |
| 141 | //ensure that this point is never reached |
| 142 | return RValue(); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 143 | } |
| 144 | |
Lauro Ramos Venancio | b40307c | 2008-01-22 20:17:04 +0000 | [diff] [blame] | 145 | RValue CodeGenFunction::EmitLoadOfBitfieldLValue(LValue LV, |
| 146 | QualType ExprType) { |
| 147 | llvm::Value *Ptr = LV.getBitfieldAddr(); |
| 148 | const llvm::Type *EltTy = |
| 149 | cast<llvm::PointerType>(Ptr->getType())->getElementType(); |
| 150 | unsigned EltTySize = EltTy->getPrimitiveSizeInBits(); |
| 151 | unsigned short BitfieldSize = LV.getBitfieldSize(); |
| 152 | unsigned short EndBit = LV.getBitfieldStartBit() + BitfieldSize; |
| 153 | |
| 154 | llvm::Value *V = Builder.CreateLoad(Ptr, "tmp"); |
| 155 | |
| 156 | llvm::Value *ShAmt = llvm::ConstantInt::get(EltTy, EltTySize - EndBit); |
| 157 | V = Builder.CreateShl(V, ShAmt, "tmp"); |
| 158 | |
| 159 | ShAmt = llvm::ConstantInt::get(EltTy, EltTySize - BitfieldSize); |
| 160 | V = LV.isBitfieldSigned() ? |
| 161 | Builder.CreateAShr(V, ShAmt, "tmp") : |
| 162 | Builder.CreateLShr(V, ShAmt, "tmp"); |
| 163 | return RValue::get(V); |
| 164 | } |
| 165 | |
Chris Lattner | 944f796 | 2007-08-03 16:18:34 +0000 | [diff] [blame] | 166 | // If this is a reference to a subset of the elements of a vector, either |
| 167 | // shuffle the input or extract/insert them as appropriate. |
Chris Lattner | a0d03a7 | 2007-08-03 17:31:20 +0000 | [diff] [blame] | 168 | RValue CodeGenFunction::EmitLoadOfOCUElementLValue(LValue LV, |
Chris Lattner | 4b49296 | 2007-08-10 17:10:08 +0000 | [diff] [blame] | 169 | QualType ExprType) { |
Chris Lattner | 944f796 | 2007-08-03 16:18:34 +0000 | [diff] [blame] | 170 | llvm::Value *Vec = Builder.CreateLoad(LV.getOCUVectorAddr(), "tmp"); |
| 171 | |
Chris Lattner | a0d03a7 | 2007-08-03 17:31:20 +0000 | [diff] [blame] | 172 | unsigned EncFields = LV.getOCUVectorElts(); |
Chris Lattner | 944f796 | 2007-08-03 16:18:34 +0000 | [diff] [blame] | 173 | |
| 174 | // If the result of the expression is a non-vector type, we must be |
| 175 | // extracting a single element. Just codegen as an extractelement. |
Chris Lattner | 4b49296 | 2007-08-10 17:10:08 +0000 | [diff] [blame] | 176 | const VectorType *ExprVT = ExprType->getAsVectorType(); |
| 177 | if (!ExprVT) { |
Chris Lattner | a0d03a7 | 2007-08-03 17:31:20 +0000 | [diff] [blame] | 178 | unsigned InIdx = OCUVectorElementExpr::getAccessedFieldNo(0, EncFields); |
Chris Lattner | 944f796 | 2007-08-03 16:18:34 +0000 | [diff] [blame] | 179 | llvm::Value *Elt = llvm::ConstantInt::get(llvm::Type::Int32Ty, InIdx); |
| 180 | return RValue::get(Builder.CreateExtractElement(Vec, Elt, "tmp")); |
| 181 | } |
| 182 | |
| 183 | // If the source and destination have the same number of elements, use a |
| 184 | // vector shuffle instead of insert/extracts. |
Chris Lattner | 4b49296 | 2007-08-10 17:10:08 +0000 | [diff] [blame] | 185 | unsigned NumResultElts = ExprVT->getNumElements(); |
Chris Lattner | 944f796 | 2007-08-03 16:18:34 +0000 | [diff] [blame] | 186 | unsigned NumSourceElts = |
| 187 | cast<llvm::VectorType>(Vec->getType())->getNumElements(); |
| 188 | |
| 189 | if (NumResultElts == NumSourceElts) { |
| 190 | llvm::SmallVector<llvm::Constant*, 4> Mask; |
| 191 | for (unsigned i = 0; i != NumResultElts; ++i) { |
Chris Lattner | a0d03a7 | 2007-08-03 17:31:20 +0000 | [diff] [blame] | 192 | unsigned InIdx = OCUVectorElementExpr::getAccessedFieldNo(i, EncFields); |
Chris Lattner | 944f796 | 2007-08-03 16:18:34 +0000 | [diff] [blame] | 193 | Mask.push_back(llvm::ConstantInt::get(llvm::Type::Int32Ty, InIdx)); |
| 194 | } |
| 195 | |
| 196 | llvm::Value *MaskV = llvm::ConstantVector::get(&Mask[0], Mask.size()); |
| 197 | Vec = Builder.CreateShuffleVector(Vec, |
| 198 | llvm::UndefValue::get(Vec->getType()), |
| 199 | MaskV, "tmp"); |
| 200 | return RValue::get(Vec); |
| 201 | } |
| 202 | |
| 203 | // Start out with an undef of the result type. |
| 204 | llvm::Value *Result = llvm::UndefValue::get(ConvertType(ExprType)); |
| 205 | |
| 206 | // Extract/Insert each element of the result. |
| 207 | for (unsigned i = 0; i != NumResultElts; ++i) { |
Chris Lattner | a0d03a7 | 2007-08-03 17:31:20 +0000 | [diff] [blame] | 208 | unsigned InIdx = OCUVectorElementExpr::getAccessedFieldNo(i, EncFields); |
Chris Lattner | 944f796 | 2007-08-03 16:18:34 +0000 | [diff] [blame] | 209 | llvm::Value *Elt = llvm::ConstantInt::get(llvm::Type::Int32Ty, InIdx); |
| 210 | Elt = Builder.CreateExtractElement(Vec, Elt, "tmp"); |
| 211 | |
| 212 | llvm::Value *OutIdx = llvm::ConstantInt::get(llvm::Type::Int32Ty, i); |
| 213 | Result = Builder.CreateInsertElement(Result, Elt, OutIdx, "tmp"); |
| 214 | } |
| 215 | |
| 216 | return RValue::get(Result); |
| 217 | } |
| 218 | |
| 219 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 220 | |
| 221 | /// EmitStoreThroughLValue - Store the specified rvalue into the specified |
| 222 | /// lvalue, where both are guaranteed to the have the same type, and that type |
| 223 | /// is 'Ty'. |
| 224 | void CodeGenFunction::EmitStoreThroughLValue(RValue Src, LValue Dst, |
| 225 | QualType Ty) { |
Chris Lattner | 5bfdd23 | 2007-08-03 16:28:33 +0000 | [diff] [blame] | 226 | if (!Dst.isSimple()) { |
| 227 | if (Dst.isVectorElt()) { |
| 228 | // Read/modify/write the vector, inserting the new element. |
| 229 | // FIXME: Volatility. |
| 230 | llvm::Value *Vec = Builder.CreateLoad(Dst.getVectorAddr(), "tmp"); |
Chris Lattner | e24c4cf | 2007-08-31 22:49:20 +0000 | [diff] [blame] | 231 | Vec = Builder.CreateInsertElement(Vec, Src.getScalarVal(), |
Chris Lattner | 5bfdd23 | 2007-08-03 16:28:33 +0000 | [diff] [blame] | 232 | Dst.getVectorIdx(), "vecins"); |
| 233 | Builder.CreateStore(Vec, Dst.getVectorAddr()); |
| 234 | return; |
| 235 | } |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 236 | |
Chris Lattner | 5bfdd23 | 2007-08-03 16:28:33 +0000 | [diff] [blame] | 237 | // If this is an update of elements of a vector, insert them as appropriate. |
Chris Lattner | a0d03a7 | 2007-08-03 17:31:20 +0000 | [diff] [blame] | 238 | if (Dst.isOCUVectorElt()) |
Chris Lattner | 5bfdd23 | 2007-08-03 16:28:33 +0000 | [diff] [blame] | 239 | return EmitStoreThroughOCUComponentLValue(Src, Dst, Ty); |
Lauro Ramos Venancio | 2d7a34c | 2008-01-22 22:36:45 +0000 | [diff] [blame] | 240 | |
| 241 | if (Dst.isBitfield()) |
| 242 | return EmitStoreThroughBitfieldLValue(Src, Dst, Ty); |
| 243 | |
Lauro Ramos Venancio | 14d3984 | 2008-01-22 22:38:35 +0000 | [diff] [blame] | 244 | assert(0 && "Unknown LValue type"); |
Chris Lattner | 5bfdd23 | 2007-08-03 16:28:33 +0000 | [diff] [blame] | 245 | } |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 246 | |
| 247 | llvm::Value *DstAddr = Dst.getAddress(); |
Chris Lattner | bdb8ffb | 2007-08-11 00:04:45 +0000 | [diff] [blame] | 248 | assert(Src.isScalar() && "Can't emit an agg store with this method"); |
| 249 | // FIXME: Handle volatility etc. |
Chris Lattner | e24c4cf | 2007-08-31 22:49:20 +0000 | [diff] [blame] | 250 | const llvm::Type *SrcTy = Src.getScalarVal()->getType(); |
Christopher Lamb | 4fe5e70 | 2007-12-17 01:11:20 +0000 | [diff] [blame] | 251 | const llvm::PointerType *DstPtr = cast<llvm::PointerType>(DstAddr->getType()); |
| 252 | const llvm::Type *AddrTy = DstPtr->getElementType(); |
| 253 | unsigned AS = DstPtr->getAddressSpace(); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 254 | |
Chris Lattner | bdb8ffb | 2007-08-11 00:04:45 +0000 | [diff] [blame] | 255 | if (AddrTy != SrcTy) |
Christopher Lamb | 4fe5e70 | 2007-12-17 01:11:20 +0000 | [diff] [blame] | 256 | DstAddr = Builder.CreateBitCast(DstAddr, |
| 257 | llvm::PointerType::get(SrcTy, AS), |
Chris Lattner | bdb8ffb | 2007-08-11 00:04:45 +0000 | [diff] [blame] | 258 | "storetmp"); |
Chris Lattner | e24c4cf | 2007-08-31 22:49:20 +0000 | [diff] [blame] | 259 | Builder.CreateStore(Src.getScalarVal(), DstAddr); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 260 | } |
| 261 | |
Lauro Ramos Venancio | 2d7a34c | 2008-01-22 22:36:45 +0000 | [diff] [blame] | 262 | void CodeGenFunction::EmitStoreThroughBitfieldLValue(RValue Src, LValue Dst, |
| 263 | QualType Ty) { |
| 264 | unsigned short StartBit = Dst.getBitfieldStartBit(); |
| 265 | unsigned short BitfieldSize = Dst.getBitfieldSize(); |
| 266 | llvm::Value *Ptr = Dst.getBitfieldAddr(); |
| 267 | const llvm::Type *EltTy = |
| 268 | cast<llvm::PointerType>(Ptr->getType())->getElementType(); |
| 269 | unsigned EltTySize = EltTy->getPrimitiveSizeInBits(); |
| 270 | |
| 271 | llvm::Value *NewVal = Src.getScalarVal(); |
| 272 | llvm::Value *OldVal = Builder.CreateLoad(Ptr, "tmp"); |
| 273 | |
| 274 | llvm::Value *ShAmt = llvm::ConstantInt::get(EltTy, StartBit); |
| 275 | NewVal = Builder.CreateShl(NewVal, ShAmt, "tmp"); |
| 276 | |
| 277 | llvm::Constant *Mask = llvm::ConstantInt::get( |
| 278 | llvm::APInt::getBitsSet(EltTySize, StartBit, |
| 279 | StartBit + BitfieldSize - 1)); |
| 280 | |
| 281 | // Mask out any bits that shouldn't be set in the result. |
| 282 | NewVal = Builder.CreateAnd(NewVal, Mask, "tmp"); |
| 283 | |
| 284 | // Next, mask out the bits this bit-field should include from the old value. |
| 285 | Mask = llvm::ConstantExpr::getNot(Mask); |
| 286 | OldVal = Builder.CreateAnd(OldVal, Mask, "tmp"); |
| 287 | |
| 288 | // Finally, merge the two together and store it. |
| 289 | NewVal = Builder.CreateOr(OldVal, NewVal, "tmp"); |
| 290 | |
| 291 | Builder.CreateStore(NewVal, Ptr); |
| 292 | } |
| 293 | |
Devang Patel | 0f2a8fb | 2007-10-30 20:59:40 +0000 | [diff] [blame] | 294 | void CodeGenFunction::EmitStoreThroughOCUComponentLValue(RValue Src, LValue Dst, |
Chris Lattner | 5bfdd23 | 2007-08-03 16:28:33 +0000 | [diff] [blame] | 295 | QualType Ty) { |
| 296 | // This access turns into a read/modify/write of the vector. Load the input |
| 297 | // value now. |
| 298 | llvm::Value *Vec = Builder.CreateLoad(Dst.getOCUVectorAddr(), "tmp"); |
| 299 | // FIXME: Volatility. |
Chris Lattner | a0d03a7 | 2007-08-03 17:31:20 +0000 | [diff] [blame] | 300 | unsigned EncFields = Dst.getOCUVectorElts(); |
Chris Lattner | 5bfdd23 | 2007-08-03 16:28:33 +0000 | [diff] [blame] | 301 | |
Chris Lattner | e24c4cf | 2007-08-31 22:49:20 +0000 | [diff] [blame] | 302 | llvm::Value *SrcVal = Src.getScalarVal(); |
Chris Lattner | 5bfdd23 | 2007-08-03 16:28:33 +0000 | [diff] [blame] | 303 | |
Chris Lattner | 940966d | 2007-08-03 16:37:04 +0000 | [diff] [blame] | 304 | if (const VectorType *VTy = Ty->getAsVectorType()) { |
| 305 | unsigned NumSrcElts = VTy->getNumElements(); |
| 306 | |
| 307 | // Extract/Insert each element. |
| 308 | for (unsigned i = 0; i != NumSrcElts; ++i) { |
| 309 | llvm::Value *Elt = llvm::ConstantInt::get(llvm::Type::Int32Ty, i); |
| 310 | Elt = Builder.CreateExtractElement(SrcVal, Elt, "tmp"); |
| 311 | |
Chris Lattner | a0d03a7 | 2007-08-03 17:31:20 +0000 | [diff] [blame] | 312 | unsigned Idx = OCUVectorElementExpr::getAccessedFieldNo(i, EncFields); |
Chris Lattner | 940966d | 2007-08-03 16:37:04 +0000 | [diff] [blame] | 313 | llvm::Value *OutIdx = llvm::ConstantInt::get(llvm::Type::Int32Ty, Idx); |
| 314 | Vec = Builder.CreateInsertElement(Vec, Elt, OutIdx, "tmp"); |
| 315 | } |
| 316 | } else { |
| 317 | // If the Src is a scalar (not a vector) it must be updating one element. |
Chris Lattner | a0d03a7 | 2007-08-03 17:31:20 +0000 | [diff] [blame] | 318 | unsigned InIdx = OCUVectorElementExpr::getAccessedFieldNo(0, EncFields); |
Chris Lattner | 5bfdd23 | 2007-08-03 16:28:33 +0000 | [diff] [blame] | 319 | llvm::Value *Elt = llvm::ConstantInt::get(llvm::Type::Int32Ty, InIdx); |
| 320 | Vec = Builder.CreateInsertElement(Vec, SrcVal, Elt, "tmp"); |
Chris Lattner | 5bfdd23 | 2007-08-03 16:28:33 +0000 | [diff] [blame] | 321 | } |
| 322 | |
Chris Lattner | 5bfdd23 | 2007-08-03 16:28:33 +0000 | [diff] [blame] | 323 | Builder.CreateStore(Vec, Dst.getOCUVectorAddr()); |
| 324 | } |
| 325 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 326 | |
| 327 | LValue CodeGenFunction::EmitDeclRefLValue(const DeclRefExpr *E) { |
Steve Naroff | cb59747 | 2007-09-13 21:41:19 +0000 | [diff] [blame] | 328 | const ValueDecl *D = E->getDecl(); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 329 | if (isa<BlockVarDecl>(D) || isa<ParmVarDecl>(D)) { |
| 330 | llvm::Value *V = LocalDeclMap[D]; |
| 331 | assert(V && "BlockVarDecl not entered in LocalDeclMap?"); |
| 332 | return LValue::MakeAddr(V); |
Chris Lattner | 1a3c1e2 | 2007-12-02 07:09:19 +0000 | [diff] [blame] | 333 | } else if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
| 334 | return LValue::MakeAddr(CGM.GetAddrOfFunctionDecl(FD, false)); |
| 335 | } else if (const FileVarDecl *FVD = dyn_cast<FileVarDecl>(D)) { |
Chris Lattner | d2df2b5 | 2007-12-18 08:16:44 +0000 | [diff] [blame] | 336 | return LValue::MakeAddr(CGM.GetAddrOfGlobalVar(FVD, false)); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 337 | } |
| 338 | assert(0 && "Unimp declref"); |
Chris Lattner | 1d2b461 | 2007-09-16 19:23:47 +0000 | [diff] [blame] | 339 | //an invalid LValue, but the assert will |
| 340 | //ensure that this point is never reached. |
| 341 | return LValue(); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 342 | } |
| 343 | |
| 344 | LValue CodeGenFunction::EmitUnaryOpLValue(const UnaryOperator *E) { |
| 345 | // __extension__ doesn't affect lvalue-ness. |
| 346 | if (E->getOpcode() == UnaryOperator::Extension) |
| 347 | return EmitLValue(E->getSubExpr()); |
| 348 | |
Chris Lattner | 5bf7202 | 2007-10-30 22:53:42 +0000 | [diff] [blame] | 349 | switch (E->getOpcode()) { |
| 350 | default: assert(0 && "Unknown unary operator lvalue!"); |
| 351 | case UnaryOperator::Deref: |
| 352 | return LValue::MakeAddr(EmitScalarExpr(E->getSubExpr())); |
| 353 | case UnaryOperator::Real: |
| 354 | case UnaryOperator::Imag: |
| 355 | LValue LV = EmitLValue(E->getSubExpr()); |
| 356 | |
| 357 | llvm::Constant *Zero = llvm::ConstantInt::get(llvm::Type::Int32Ty, 0); |
| 358 | llvm::Constant *Idx = llvm::ConstantInt::get(llvm::Type::Int32Ty, |
| 359 | E->getOpcode() == UnaryOperator::Imag); |
| 360 | llvm::Value *Ops[] = {Zero, Idx}; |
| 361 | return LValue::MakeAddr(Builder.CreateGEP(LV.getAddress(), Ops, Ops+2, |
| 362 | "idx")); |
| 363 | } |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 364 | } |
| 365 | |
| 366 | LValue CodeGenFunction::EmitStringLiteralLValue(const StringLiteral *E) { |
| 367 | assert(!E->isWide() && "FIXME: Wide strings not supported yet!"); |
| 368 | const char *StrData = E->getStrData(); |
| 369 | unsigned Len = E->getByteLength(); |
Chris Lattner | db6be56 | 2007-11-28 05:34:05 +0000 | [diff] [blame] | 370 | std::string StringLiteral(StrData, StrData+Len); |
| 371 | return LValue::MakeAddr(CGM.GetAddrOfConstantString(StringLiteral)); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 372 | } |
| 373 | |
| 374 | LValue CodeGenFunction::EmitPreDefinedLValue(const PreDefinedExpr *E) { |
| 375 | std::string FunctionName(CurFuncDecl->getName()); |
| 376 | std::string GlobalVarName; |
| 377 | |
| 378 | switch (E->getIdentType()) { |
| 379 | default: |
| 380 | assert(0 && "unknown pre-defined ident type"); |
| 381 | case PreDefinedExpr::Func: |
| 382 | GlobalVarName = "__func__."; |
| 383 | break; |
| 384 | case PreDefinedExpr::Function: |
| 385 | GlobalVarName = "__FUNCTION__."; |
| 386 | break; |
| 387 | case PreDefinedExpr::PrettyFunction: |
| 388 | // FIXME:: Demangle C++ method names |
| 389 | GlobalVarName = "__PRETTY_FUNCTION__."; |
| 390 | break; |
| 391 | } |
| 392 | |
| 393 | GlobalVarName += CurFuncDecl->getName(); |
| 394 | |
| 395 | // FIXME: Can cache/reuse these within the module. |
| 396 | llvm::Constant *C=llvm::ConstantArray::get(FunctionName); |
| 397 | |
| 398 | // Create a global variable for this. |
| 399 | C = new llvm::GlobalVariable(C->getType(), true, |
| 400 | llvm::GlobalValue::InternalLinkage, |
| 401 | C, GlobalVarName, CurFn->getParent()); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 402 | return LValue::MakeAddr(C); |
| 403 | } |
| 404 | |
| 405 | LValue CodeGenFunction::EmitArraySubscriptExpr(const ArraySubscriptExpr *E) { |
Ted Kremenek | 1c1700f | 2007-08-20 16:18:38 +0000 | [diff] [blame] | 406 | // 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] | 407 | llvm::Value *Idx = EmitScalarExpr(E->getIdx()); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 408 | |
| 409 | // If the base is a vector type, then we are forming a vector element lvalue |
| 410 | // with this subscript. |
Ted Kremenek | 1c1700f | 2007-08-20 16:18:38 +0000 | [diff] [blame] | 411 | if (E->getLHS()->getType()->isVectorType()) { |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 412 | // Emit the vector as an lvalue to get its address. |
Ted Kremenek | 1c1700f | 2007-08-20 16:18:38 +0000 | [diff] [blame] | 413 | LValue LHS = EmitLValue(E->getLHS()); |
| 414 | assert(LHS.isSimple() && "Can only subscript lvalue vectors here!"); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 415 | // FIXME: This should properly sign/zero/extend or truncate Idx to i32. |
Ted Kremenek | 1c1700f | 2007-08-20 16:18:38 +0000 | [diff] [blame] | 416 | return LValue::MakeVectorElt(LHS.getAddress(), Idx); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 417 | } |
| 418 | |
Ted Kremenek | 1c1700f | 2007-08-20 16:18:38 +0000 | [diff] [blame] | 419 | // 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] | 420 | llvm::Value *Base = EmitScalarExpr(E->getBase()); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 421 | |
Ted Kremenek | 1c1700f | 2007-08-20 16:18:38 +0000 | [diff] [blame] | 422 | // Extend or truncate the index type to 32 or 64-bits. |
Chris Lattner | 2af72ac | 2007-08-08 17:43:05 +0000 | [diff] [blame] | 423 | QualType IdxTy = E->getIdx()->getType(); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 424 | bool IdxSigned = IdxTy->isSignedIntegerType(); |
| 425 | unsigned IdxBitwidth = cast<llvm::IntegerType>(Idx->getType())->getBitWidth(); |
| 426 | if (IdxBitwidth != LLVMPointerWidth) |
| 427 | Idx = Builder.CreateIntCast(Idx, llvm::IntegerType::get(LLVMPointerWidth), |
| 428 | IdxSigned, "idxprom"); |
| 429 | |
| 430 | // We know that the pointer points to a type of the correct size, unless the |
| 431 | // size is a VLA. |
| 432 | if (!E->getType()->isConstantSizeType(getContext())) |
| 433 | assert(0 && "VLA idx not implemented"); |
| 434 | return LValue::MakeAddr(Builder.CreateGEP(Base, Idx, "arrayidx")); |
| 435 | } |
| 436 | |
Chris Lattner | 6552019 | 2007-08-02 23:37:31 +0000 | [diff] [blame] | 437 | LValue CodeGenFunction:: |
Chris Lattner | a0d03a7 | 2007-08-03 17:31:20 +0000 | [diff] [blame] | 438 | EmitOCUVectorElementExpr(const OCUVectorElementExpr *E) { |
Chris Lattner | 6552019 | 2007-08-02 23:37:31 +0000 | [diff] [blame] | 439 | // Emit the base vector as an l-value. |
| 440 | LValue Base = EmitLValue(E->getBase()); |
| 441 | assert(Base.isSimple() && "Can only subscript lvalue vectors here!"); |
| 442 | |
Chris Lattner | a0d03a7 | 2007-08-03 17:31:20 +0000 | [diff] [blame] | 443 | return LValue::MakeOCUVectorElt(Base.getAddress(), |
| 444 | E->getEncodedElementAccess()); |
Chris Lattner | 6552019 | 2007-08-02 23:37:31 +0000 | [diff] [blame] | 445 | } |
| 446 | |
Devang Patel | 41b6625 | 2007-10-23 20:28:39 +0000 | [diff] [blame] | 447 | LValue CodeGenFunction::EmitMemberExpr(const MemberExpr *E) { |
| 448 | |
Devang Patel | e1f79db | 2007-12-11 21:33:16 +0000 | [diff] [blame] | 449 | bool isUnion = false; |
Devang Patel | 9dd3e2b | 2007-10-24 22:26:28 +0000 | [diff] [blame] | 450 | Expr *BaseExpr = E->getBase(); |
Devang Patel | 9dd3e2b | 2007-10-24 22:26:28 +0000 | [diff] [blame] | 451 | llvm::Value *BaseValue = NULL; |
Chris Lattner | 659079e | 2007-12-02 18:52:07 +0000 | [diff] [blame] | 452 | |
| 453 | // 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] | 454 | if (E->isArrow()) { |
Devang Patel | 2b24fd9 | 2007-10-26 18:15:21 +0000 | [diff] [blame] | 455 | BaseValue = EmitScalarExpr(BaseExpr); |
Devang Patel | e1f79db | 2007-12-11 21:33:16 +0000 | [diff] [blame] | 456 | const PointerType *PTy = |
| 457 | cast<PointerType>(BaseExpr->getType().getCanonicalType()); |
| 458 | if (PTy->getPointeeType()->isUnionType()) |
| 459 | isUnion = true; |
| 460 | } |
Chris Lattner | 659079e | 2007-12-02 18:52:07 +0000 | [diff] [blame] | 461 | else { |
| 462 | LValue BaseLV = EmitLValue(BaseExpr); |
| 463 | // FIXME: this isn't right for bitfields. |
| 464 | BaseValue = BaseLV.getAddress(); |
Devang Patel | e1f79db | 2007-12-11 21:33:16 +0000 | [diff] [blame] | 465 | if (BaseExpr->getType()->isUnionType()) |
| 466 | isUnion = true; |
Chris Lattner | 659079e | 2007-12-02 18:52:07 +0000 | [diff] [blame] | 467 | } |
Devang Patel | 41b6625 | 2007-10-23 20:28:39 +0000 | [diff] [blame] | 468 | |
| 469 | FieldDecl *Field = E->getMemberDecl(); |
Devang Patel | 691e9da | 2007-12-10 18:52:06 +0000 | [diff] [blame] | 470 | |
Devang Patel | 41b6625 | 2007-10-23 20:28:39 +0000 | [diff] [blame] | 471 | unsigned idx = CGM.getTypes().getLLVMFieldNo(Field); |
| 472 | llvm::Value *Idxs[2] = { llvm::Constant::getNullValue(llvm::Type::Int32Ty), |
Devang Patel | 30f6f13 | 2007-10-24 00:26:24 +0000 | [diff] [blame] | 473 | llvm::ConstantInt::get(llvm::Type::Int32Ty, idx) }; |
Devang Patel | 41b6625 | 2007-10-23 20:28:39 +0000 | [diff] [blame] | 474 | |
Devang Patel | 9b1ca9e | 2007-10-26 19:42:18 +0000 | [diff] [blame] | 475 | llvm::Value *V = Builder.CreateGEP(BaseValue,Idxs, Idxs + 2, "tmp"); |
| 476 | // Match union field type. |
Lauro Ramos Venancio | b40307c | 2008-01-22 20:17:04 +0000 | [diff] [blame] | 477 | if (isUnion || Field->isBitField()) { |
Devang Patel | 9b1ca9e | 2007-10-26 19:42:18 +0000 | [diff] [blame] | 478 | const llvm::Type * FieldTy = ConvertType(Field->getType()); |
Devang Patel | 0f2a8fb | 2007-10-30 20:59:40 +0000 | [diff] [blame] | 479 | const llvm::PointerType * BaseTy = |
| 480 | cast<llvm::PointerType>(BaseValue->getType()); |
Devang Patel | 9b1ca9e | 2007-10-26 19:42:18 +0000 | [diff] [blame] | 481 | if (FieldTy != BaseTy->getElementType()) { |
Christopher Lamb | d62ab38 | 2007-12-29 04:06:57 +0000 | [diff] [blame] | 482 | unsigned AS = BaseTy->getAddressSpace(); |
Christopher Lamb | 4fe5e70 | 2007-12-17 01:11:20 +0000 | [diff] [blame] | 483 | V = Builder.CreateBitCast(V, |
Christopher Lamb | d62ab38 | 2007-12-29 04:06:57 +0000 | [diff] [blame] | 484 | llvm::PointerType::get(FieldTy, AS), |
Christopher Lamb | 4fe5e70 | 2007-12-17 01:11:20 +0000 | [diff] [blame] | 485 | "tmp"); |
Devang Patel | 9b1ca9e | 2007-10-26 19:42:18 +0000 | [diff] [blame] | 486 | } |
| 487 | } |
Lauro Ramos Venancio | b40307c | 2008-01-22 20:17:04 +0000 | [diff] [blame] | 488 | |
| 489 | if (Field->isBitField()) { |
| 490 | CodeGenTypes::BitFieldInfo bitFieldInfo = |
| 491 | CGM.getTypes().getBitFieldInfo(Field); |
| 492 | return LValue::MakeBitfield(V, bitFieldInfo.Begin, bitFieldInfo.Size, |
| 493 | Field->getType()->isSignedIntegerType()); |
| 494 | } else |
| 495 | return LValue::MakeAddr(V); |
Devang Patel | 41b6625 | 2007-10-23 20:28:39 +0000 | [diff] [blame] | 496 | } |
| 497 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 498 | //===--------------------------------------------------------------------===// |
| 499 | // Expression Emission |
| 500 | //===--------------------------------------------------------------------===// |
| 501 | |
Chris Lattner | b2cb9cb | 2007-08-20 22:37:10 +0000 | [diff] [blame] | 502 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 503 | RValue CodeGenFunction::EmitCallExpr(const CallExpr *E) { |
Anders Carlsson | 4986530 | 2007-08-20 18:05:56 +0000 | [diff] [blame] | 504 | if (const ImplicitCastExpr *IcExpr = |
| 505 | dyn_cast<const ImplicitCastExpr>(E->getCallee())) |
| 506 | if (const DeclRefExpr *DRExpr = |
| 507 | dyn_cast<const DeclRefExpr>(IcExpr->getSubExpr())) |
| 508 | if (const FunctionDecl *FDecl = |
| 509 | dyn_cast<const FunctionDecl>(DRExpr->getDecl())) |
| 510 | if (unsigned builtinID = FDecl->getIdentifier()->getBuiltinID()) |
| 511 | return EmitBuiltinExpr(builtinID, E); |
| 512 | |
Chris Lattner | 9fba49a | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 513 | llvm::Value *Callee = EmitScalarExpr(E->getCallee()); |
Eli Friedman | 261f4ad | 2008-01-30 01:32:06 +0000 | [diff] [blame^] | 514 | return EmitCallExpr(Callee, E->getCallee()->getType(), |
| 515 | E->arg_begin(), E->getNumArgs()); |
Nate Begeman | 9f3bfb7 | 2008-01-17 17:46:27 +0000 | [diff] [blame] | 516 | } |
| 517 | |
Eli Friedman | 261f4ad | 2008-01-30 01:32:06 +0000 | [diff] [blame^] | 518 | RValue CodeGenFunction::EmitCallExpr(Expr *FnExpr, Expr *const *Args, |
| 519 | unsigned NumArgs) { |
Nate Begeman | 9f3bfb7 | 2008-01-17 17:46:27 +0000 | [diff] [blame] | 520 | llvm::Value *Callee = EmitScalarExpr(FnExpr); |
Eli Friedman | 261f4ad | 2008-01-30 01:32:06 +0000 | [diff] [blame^] | 521 | return EmitCallExpr(Callee, FnExpr->getType(), Args, NumArgs); |
Chris Lattner | 02c60f5 | 2007-08-31 04:44:06 +0000 | [diff] [blame] | 522 | } |
| 523 | |
Christopher Lamb | ad327ba | 2007-12-29 05:02:41 +0000 | [diff] [blame] | 524 | LValue CodeGenFunction::EmitCallExprLValue(const CallExpr *E) { |
| 525 | // Can only get l-value for call expression returning aggregate type |
| 526 | RValue RV = EmitCallExpr(E); |
| 527 | return LValue::MakeAddr(RV.getAggregateAddr()); |
| 528 | } |
| 529 | |
Nate Begeman | 9f3bfb7 | 2008-01-17 17:46:27 +0000 | [diff] [blame] | 530 | RValue CodeGenFunction::EmitCallExpr(llvm::Value *Callee, QualType FnType, |
Eli Friedman | 261f4ad | 2008-01-30 01:32:06 +0000 | [diff] [blame^] | 531 | Expr *const *ArgExprs, unsigned NumArgs) { |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 532 | // The callee type will always be a pointer to function type, get the function |
| 533 | // type. |
Nate Begeman | 9f3bfb7 | 2008-01-17 17:46:27 +0000 | [diff] [blame] | 534 | FnType = cast<PointerType>(FnType.getCanonicalType())->getPointeeType(); |
| 535 | QualType ResultType = cast<FunctionType>(FnType)->getResultType(); |
Eli Friedman | 261f4ad | 2008-01-30 01:32:06 +0000 | [diff] [blame^] | 536 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 537 | llvm::SmallVector<llvm::Value*, 16> Args; |
| 538 | |
Chris Lattner | 5980204 | 2007-08-10 17:02:28 +0000 | [diff] [blame] | 539 | // Handle struct-return functions by passing a pointer to the location that |
| 540 | // we would like to return into. |
Nate Begeman | 9f3bfb7 | 2008-01-17 17:46:27 +0000 | [diff] [blame] | 541 | if (hasAggregateLLVMType(ResultType)) { |
Chris Lattner | 5980204 | 2007-08-10 17:02:28 +0000 | [diff] [blame] | 542 | // Create a temporary alloca to hold the result of the call. :( |
Nate Begeman | 9f3bfb7 | 2008-01-17 17:46:27 +0000 | [diff] [blame] | 543 | Args.push_back(CreateTempAlloca(ConvertType(ResultType))); |
Chris Lattner | 5980204 | 2007-08-10 17:02:28 +0000 | [diff] [blame] | 544 | // FIXME: set the stret attribute on the argument. |
| 545 | } |
| 546 | |
Nate Begeman | 9f3bfb7 | 2008-01-17 17:46:27 +0000 | [diff] [blame] | 547 | for (unsigned i = 0, e = NumArgs; i != e; ++i) { |
| 548 | QualType ArgTy = ArgExprs[i]->getType(); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 549 | |
Chris Lattner | b06c8dd | 2007-08-26 22:55:13 +0000 | [diff] [blame] | 550 | if (!hasAggregateLLVMType(ArgTy)) { |
| 551 | // Scalar argument is passed by-value. |
Nate Begeman | 9f3bfb7 | 2008-01-17 17:46:27 +0000 | [diff] [blame] | 552 | Args.push_back(EmitScalarExpr(ArgExprs[i])); |
Chris Lattner | b06c8dd | 2007-08-26 22:55:13 +0000 | [diff] [blame] | 553 | } else if (ArgTy->isComplexType()) { |
| 554 | // Make a temporary alloca to pass the argument. |
| 555 | llvm::Value *DestMem = CreateTempAlloca(ConvertType(ArgTy)); |
Nate Begeman | 9f3bfb7 | 2008-01-17 17:46:27 +0000 | [diff] [blame] | 556 | EmitComplexExprIntoAddr(ArgExprs[i], DestMem, false); |
Chris Lattner | b06c8dd | 2007-08-26 22:55:13 +0000 | [diff] [blame] | 557 | Args.push_back(DestMem); |
| 558 | } else { |
| 559 | llvm::Value *DestMem = CreateTempAlloca(ConvertType(ArgTy)); |
Nate Begeman | 9f3bfb7 | 2008-01-17 17:46:27 +0000 | [diff] [blame] | 560 | EmitAggExpr(ArgExprs[i], DestMem, false); |
Chris Lattner | b06c8dd | 2007-08-26 22:55:13 +0000 | [diff] [blame] | 561 | Args.push_back(DestMem); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 562 | } |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 563 | } |
| 564 | |
Chris Lattner | a957225 | 2007-08-01 06:24:52 +0000 | [diff] [blame] | 565 | llvm::Value *V = Builder.CreateCall(Callee, &Args[0], &Args[0]+Args.size()); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 566 | if (V->getType() != llvm::Type::VoidTy) |
| 567 | V->setName("call"); |
Nate Begeman | 9f3bfb7 | 2008-01-17 17:46:27 +0000 | [diff] [blame] | 568 | else if (ResultType->isComplexType()) |
Chris Lattner | e24c4cf | 2007-08-31 22:49:20 +0000 | [diff] [blame] | 569 | return RValue::getComplex(LoadComplexFromAddr(Args[0], false)); |
Nate Begeman | 9f3bfb7 | 2008-01-17 17:46:27 +0000 | [diff] [blame] | 570 | else if (hasAggregateLLVMType(ResultType)) |
Chris Lattner | 5980204 | 2007-08-10 17:02:28 +0000 | [diff] [blame] | 571 | // Struct return. |
| 572 | return RValue::getAggregate(Args[0]); |
Chris Lattner | 307da02 | 2007-11-30 17:56:23 +0000 | [diff] [blame] | 573 | else { |
| 574 | // void return. |
Nate Begeman | 9f3bfb7 | 2008-01-17 17:46:27 +0000 | [diff] [blame] | 575 | assert(ResultType->isVoidType() && "Should only have a void expr here"); |
Chris Lattner | 307da02 | 2007-11-30 17:56:23 +0000 | [diff] [blame] | 576 | V = 0; |
| 577 | } |
Chris Lattner | 5980204 | 2007-08-10 17:02:28 +0000 | [diff] [blame] | 578 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 579 | return RValue::get(V); |
| 580 | } |