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