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 | // |
| 5 | // This file was developed by Chris Lattner and is distributed under |
| 6 | // the University of Illinois Open Source License. See LICENSE.TXT for details. |
| 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" |
Anders Carlsson | 022012e | 2007-08-20 18:05:56 +0000 | [diff] [blame] | 17 | #include "clang/Lex/IdentifierTable.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 18 | #include "llvm/Constants.h" |
| 19 | #include "llvm/DerivedTypes.h" |
| 20 | #include "llvm/Function.h" |
| 21 | #include "llvm/GlobalVariable.h" |
Chris Lattner | 99e0d79 | 2007-07-16 05:43:05 +0000 | [diff] [blame] | 22 | #include "llvm/Support/MathExtras.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 23 | using namespace clang; |
| 24 | using namespace CodeGen; |
| 25 | |
| 26 | //===--------------------------------------------------------------------===// |
| 27 | // Miscellaneous Helper Methods |
| 28 | //===--------------------------------------------------------------------===// |
| 29 | |
| 30 | /// CreateTempAlloca - This creates a alloca and inserts it into the entry |
| 31 | /// block. |
| 32 | llvm::AllocaInst *CodeGenFunction::CreateTempAlloca(const llvm::Type *Ty, |
| 33 | const char *Name) { |
| 34 | return new llvm::AllocaInst(Ty, 0, Name, AllocaInsertPt); |
| 35 | } |
| 36 | |
| 37 | /// EvaluateExprAsBool - Perform the usual unary conversions on the specified |
| 38 | /// expression and compare the result against zero, returning an Int1Ty value. |
| 39 | llvm::Value *CodeGenFunction::EvaluateExprAsBool(const Expr *E) { |
Chris Lattner | 9069fa2 | 2007-08-26 16:46:58 +0000 | [diff] [blame] | 40 | QualType BoolTy = getContext().BoolTy; |
| 41 | if (!E->getType()->isComplexType()) |
| 42 | return EmitScalarConversion(EmitScalarExpr(E), E->getType(), BoolTy); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 43 | |
Chris Lattner | 9069fa2 | 2007-08-26 16:46:58 +0000 | [diff] [blame] | 44 | return EmitComplexToScalarConversion(EmitComplexExpr(E), E->getType(),BoolTy); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 45 | } |
| 46 | |
| 47 | //===----------------------------------------------------------------------===// |
| 48 | // LValue Expression Emission |
| 49 | //===----------------------------------------------------------------------===// |
| 50 | |
| 51 | /// EmitLValue - Emit code to compute a designator that specifies the location |
| 52 | /// of the expression. |
| 53 | /// |
| 54 | /// This can return one of two things: a simple address or a bitfield |
| 55 | /// reference. In either case, the LLVM Value* in the LValue structure is |
| 56 | /// guaranteed to be an LLVM pointer type. |
| 57 | /// |
| 58 | /// If this returns a bitfield reference, nothing about the pointee type of |
| 59 | /// the LLVM value is known: For example, it may not be a pointer to an |
| 60 | /// integer. |
| 61 | /// |
| 62 | /// If this returns a normal address, and if the lvalue's C type is fixed |
| 63 | /// size, this method guarantees that the returned pointer type will point to |
| 64 | /// an LLVM type of the same size of the lvalue's type. If the lvalue has a |
| 65 | /// variable length type, this is not possible. |
| 66 | /// |
| 67 | LValue CodeGenFunction::EmitLValue(const Expr *E) { |
| 68 | switch (E->getStmtClass()) { |
Chris Lattner | 7013c8c | 2007-08-26 05:06:40 +0000 | [diff] [blame] | 69 | default: { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 70 | fprintf(stderr, "Unimplemented lvalue expr!\n"); |
| 71 | E->dump(); |
Chris Lattner | 7013c8c | 2007-08-26 05:06:40 +0000 | [diff] [blame] | 72 | llvm::Type *Ty = llvm::PointerType::get(ConvertType(E->getType())); |
| 73 | return LValue::MakeAddr(llvm::UndefValue::get(Ty)); |
| 74 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 75 | |
| 76 | case Expr::DeclRefExprClass: return EmitDeclRefLValue(cast<DeclRefExpr>(E)); |
| 77 | case Expr::ParenExprClass:return EmitLValue(cast<ParenExpr>(E)->getSubExpr()); |
Anders Carlsson | 2274266 | 2007-07-21 05:21:51 +0000 | [diff] [blame] | 78 | case Expr::PreDefinedExprClass: |
| 79 | return EmitPreDefinedLValue(cast<PreDefinedExpr>(E)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 80 | case Expr::StringLiteralClass: |
| 81 | return EmitStringLiteralLValue(cast<StringLiteral>(E)); |
| 82 | |
| 83 | case Expr::UnaryOperatorClass: |
| 84 | return EmitUnaryOpLValue(cast<UnaryOperator>(E)); |
| 85 | case Expr::ArraySubscriptExprClass: |
| 86 | return EmitArraySubscriptExpr(cast<ArraySubscriptExpr>(E)); |
Chris Lattner | 6481a57 | 2007-08-03 17:31:20 +0000 | [diff] [blame] | 87 | case Expr::OCUVectorElementExprClass: |
| 88 | return EmitOCUVectorElementExpr(cast<OCUVectorElementExpr>(E)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 89 | } |
| 90 | } |
| 91 | |
| 92 | /// EmitLoadOfLValue - Given an expression that represents a value lvalue, |
| 93 | /// this method emits the address of the lvalue, then loads the result as an |
| 94 | /// rvalue, returning the rvalue. |
| 95 | RValue CodeGenFunction::EmitLoadOfLValue(LValue LV, QualType ExprType) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 96 | if (LV.isSimple()) { |
| 97 | llvm::Value *Ptr = LV.getAddress(); |
| 98 | const llvm::Type *EltTy = |
| 99 | cast<llvm::PointerType>(Ptr->getType())->getElementType(); |
| 100 | |
| 101 | // Simple scalar l-value. |
| 102 | if (EltTy->isFirstClassType()) |
| 103 | return RValue::get(Builder.CreateLoad(Ptr, "tmp")); |
| 104 | |
Chris Lattner | 883f6a7 | 2007-08-11 00:04:45 +0000 | [diff] [blame] | 105 | assert(ExprType->isFunctionType() && "Unknown scalar value"); |
| 106 | return RValue::get(Ptr); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | if (LV.isVectorElt()) { |
| 110 | llvm::Value *Vec = Builder.CreateLoad(LV.getVectorAddr(), "tmp"); |
| 111 | return RValue::get(Builder.CreateExtractElement(Vec, LV.getVectorIdx(), |
| 112 | "vecext")); |
| 113 | } |
Chris Lattner | 46ea8eb | 2007-08-03 00:16:29 +0000 | [diff] [blame] | 114 | |
| 115 | // If this is a reference to a subset of the elements of a vector, either |
| 116 | // shuffle the input or extract/insert them as appropriate. |
Chris Lattner | 6481a57 | 2007-08-03 17:31:20 +0000 | [diff] [blame] | 117 | if (LV.isOCUVectorElt()) |
| 118 | return EmitLoadOfOCUElementLValue(LV, ExprType); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 119 | |
| 120 | assert(0 && "Bitfield ref not impl!"); |
| 121 | } |
| 122 | |
Chris Lattner | 34cdc86 | 2007-08-03 16:18:34 +0000 | [diff] [blame] | 123 | // If this is a reference to a subset of the elements of a vector, either |
| 124 | // shuffle the input or extract/insert them as appropriate. |
Chris Lattner | 6481a57 | 2007-08-03 17:31:20 +0000 | [diff] [blame] | 125 | RValue CodeGenFunction::EmitLoadOfOCUElementLValue(LValue LV, |
Chris Lattner | cf60cd2 | 2007-08-10 17:10:08 +0000 | [diff] [blame] | 126 | QualType ExprType) { |
Chris Lattner | 34cdc86 | 2007-08-03 16:18:34 +0000 | [diff] [blame] | 127 | llvm::Value *Vec = Builder.CreateLoad(LV.getOCUVectorAddr(), "tmp"); |
| 128 | |
Chris Lattner | 6481a57 | 2007-08-03 17:31:20 +0000 | [diff] [blame] | 129 | unsigned EncFields = LV.getOCUVectorElts(); |
Chris Lattner | 34cdc86 | 2007-08-03 16:18:34 +0000 | [diff] [blame] | 130 | |
| 131 | // If the result of the expression is a non-vector type, we must be |
| 132 | // extracting a single element. Just codegen as an extractelement. |
Chris Lattner | cf60cd2 | 2007-08-10 17:10:08 +0000 | [diff] [blame] | 133 | const VectorType *ExprVT = ExprType->getAsVectorType(); |
| 134 | if (!ExprVT) { |
Chris Lattner | 6481a57 | 2007-08-03 17:31:20 +0000 | [diff] [blame] | 135 | unsigned InIdx = OCUVectorElementExpr::getAccessedFieldNo(0, EncFields); |
Chris Lattner | 34cdc86 | 2007-08-03 16:18:34 +0000 | [diff] [blame] | 136 | llvm::Value *Elt = llvm::ConstantInt::get(llvm::Type::Int32Ty, InIdx); |
| 137 | return RValue::get(Builder.CreateExtractElement(Vec, Elt, "tmp")); |
| 138 | } |
| 139 | |
| 140 | // If the source and destination have the same number of elements, use a |
| 141 | // vector shuffle instead of insert/extracts. |
Chris Lattner | cf60cd2 | 2007-08-10 17:10:08 +0000 | [diff] [blame] | 142 | unsigned NumResultElts = ExprVT->getNumElements(); |
Chris Lattner | 34cdc86 | 2007-08-03 16:18:34 +0000 | [diff] [blame] | 143 | unsigned NumSourceElts = |
| 144 | cast<llvm::VectorType>(Vec->getType())->getNumElements(); |
| 145 | |
| 146 | if (NumResultElts == NumSourceElts) { |
| 147 | llvm::SmallVector<llvm::Constant*, 4> Mask; |
| 148 | for (unsigned i = 0; i != NumResultElts; ++i) { |
Chris Lattner | 6481a57 | 2007-08-03 17:31:20 +0000 | [diff] [blame] | 149 | unsigned InIdx = OCUVectorElementExpr::getAccessedFieldNo(i, EncFields); |
Chris Lattner | 34cdc86 | 2007-08-03 16:18:34 +0000 | [diff] [blame] | 150 | Mask.push_back(llvm::ConstantInt::get(llvm::Type::Int32Ty, InIdx)); |
| 151 | } |
| 152 | |
| 153 | llvm::Value *MaskV = llvm::ConstantVector::get(&Mask[0], Mask.size()); |
| 154 | Vec = Builder.CreateShuffleVector(Vec, |
| 155 | llvm::UndefValue::get(Vec->getType()), |
| 156 | MaskV, "tmp"); |
| 157 | return RValue::get(Vec); |
| 158 | } |
| 159 | |
| 160 | // Start out with an undef of the result type. |
| 161 | llvm::Value *Result = llvm::UndefValue::get(ConvertType(ExprType)); |
| 162 | |
| 163 | // Extract/Insert each element of the result. |
| 164 | for (unsigned i = 0; i != NumResultElts; ++i) { |
Chris Lattner | 6481a57 | 2007-08-03 17:31:20 +0000 | [diff] [blame] | 165 | unsigned InIdx = OCUVectorElementExpr::getAccessedFieldNo(i, EncFields); |
Chris Lattner | 34cdc86 | 2007-08-03 16:18:34 +0000 | [diff] [blame] | 166 | llvm::Value *Elt = llvm::ConstantInt::get(llvm::Type::Int32Ty, InIdx); |
| 167 | Elt = Builder.CreateExtractElement(Vec, Elt, "tmp"); |
| 168 | |
| 169 | llvm::Value *OutIdx = llvm::ConstantInt::get(llvm::Type::Int32Ty, i); |
| 170 | Result = Builder.CreateInsertElement(Result, Elt, OutIdx, "tmp"); |
| 171 | } |
| 172 | |
| 173 | return RValue::get(Result); |
| 174 | } |
| 175 | |
| 176 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 177 | |
| 178 | /// EmitStoreThroughLValue - Store the specified rvalue into the specified |
| 179 | /// lvalue, where both are guaranteed to the have the same type, and that type |
| 180 | /// is 'Ty'. |
| 181 | void CodeGenFunction::EmitStoreThroughLValue(RValue Src, LValue Dst, |
| 182 | QualType Ty) { |
Chris Lattner | 017d6aa | 2007-08-03 16:28:33 +0000 | [diff] [blame] | 183 | if (!Dst.isSimple()) { |
| 184 | if (Dst.isVectorElt()) { |
| 185 | // Read/modify/write the vector, inserting the new element. |
| 186 | // FIXME: Volatility. |
| 187 | llvm::Value *Vec = Builder.CreateLoad(Dst.getVectorAddr(), "tmp"); |
| 188 | Vec = Builder.CreateInsertElement(Vec, Src.getVal(), |
| 189 | Dst.getVectorIdx(), "vecins"); |
| 190 | Builder.CreateStore(Vec, Dst.getVectorAddr()); |
| 191 | return; |
| 192 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 193 | |
Chris Lattner | 017d6aa | 2007-08-03 16:28:33 +0000 | [diff] [blame] | 194 | // 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] | 195 | if (Dst.isOCUVectorElt()) |
Chris Lattner | 017d6aa | 2007-08-03 16:28:33 +0000 | [diff] [blame] | 196 | return EmitStoreThroughOCUComponentLValue(Src, Dst, Ty); |
| 197 | |
| 198 | assert(0 && "FIXME: Don't support store to bitfield yet"); |
| 199 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 200 | |
| 201 | llvm::Value *DstAddr = Dst.getAddress(); |
Chris Lattner | 883f6a7 | 2007-08-11 00:04:45 +0000 | [diff] [blame] | 202 | assert(Src.isScalar() && "Can't emit an agg store with this method"); |
| 203 | // FIXME: Handle volatility etc. |
| 204 | const llvm::Type *SrcTy = Src.getVal()->getType(); |
| 205 | const llvm::Type *AddrTy = |
| 206 | cast<llvm::PointerType>(DstAddr->getType())->getElementType(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 207 | |
Chris Lattner | 883f6a7 | 2007-08-11 00:04:45 +0000 | [diff] [blame] | 208 | if (AddrTy != SrcTy) |
| 209 | DstAddr = Builder.CreateBitCast(DstAddr, llvm::PointerType::get(SrcTy), |
| 210 | "storetmp"); |
| 211 | Builder.CreateStore(Src.getVal(), DstAddr); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 212 | } |
| 213 | |
Chris Lattner | 017d6aa | 2007-08-03 16:28:33 +0000 | [diff] [blame] | 214 | void CodeGenFunction::EmitStoreThroughOCUComponentLValue(RValue Src, LValue Dst, |
| 215 | QualType Ty) { |
| 216 | // This access turns into a read/modify/write of the vector. Load the input |
| 217 | // value now. |
| 218 | llvm::Value *Vec = Builder.CreateLoad(Dst.getOCUVectorAddr(), "tmp"); |
| 219 | // FIXME: Volatility. |
Chris Lattner | 6481a57 | 2007-08-03 17:31:20 +0000 | [diff] [blame] | 220 | unsigned EncFields = Dst.getOCUVectorElts(); |
Chris Lattner | 017d6aa | 2007-08-03 16:28:33 +0000 | [diff] [blame] | 221 | |
| 222 | llvm::Value *SrcVal = Src.getVal(); |
| 223 | |
Chris Lattner | 7e6b51b | 2007-08-03 16:37:04 +0000 | [diff] [blame] | 224 | if (const VectorType *VTy = Ty->getAsVectorType()) { |
| 225 | unsigned NumSrcElts = VTy->getNumElements(); |
| 226 | |
| 227 | // Extract/Insert each element. |
| 228 | for (unsigned i = 0; i != NumSrcElts; ++i) { |
| 229 | llvm::Value *Elt = llvm::ConstantInt::get(llvm::Type::Int32Ty, i); |
| 230 | Elt = Builder.CreateExtractElement(SrcVal, Elt, "tmp"); |
| 231 | |
Chris Lattner | 6481a57 | 2007-08-03 17:31:20 +0000 | [diff] [blame] | 232 | unsigned Idx = OCUVectorElementExpr::getAccessedFieldNo(i, EncFields); |
Chris Lattner | 7e6b51b | 2007-08-03 16:37:04 +0000 | [diff] [blame] | 233 | llvm::Value *OutIdx = llvm::ConstantInt::get(llvm::Type::Int32Ty, Idx); |
| 234 | Vec = Builder.CreateInsertElement(Vec, Elt, OutIdx, "tmp"); |
| 235 | } |
| 236 | } else { |
| 237 | // 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] | 238 | unsigned InIdx = OCUVectorElementExpr::getAccessedFieldNo(0, EncFields); |
Chris Lattner | 017d6aa | 2007-08-03 16:28:33 +0000 | [diff] [blame] | 239 | llvm::Value *Elt = llvm::ConstantInt::get(llvm::Type::Int32Ty, InIdx); |
| 240 | Vec = Builder.CreateInsertElement(Vec, SrcVal, Elt, "tmp"); |
Chris Lattner | 017d6aa | 2007-08-03 16:28:33 +0000 | [diff] [blame] | 241 | } |
| 242 | |
Chris Lattner | 017d6aa | 2007-08-03 16:28:33 +0000 | [diff] [blame] | 243 | Builder.CreateStore(Vec, Dst.getOCUVectorAddr()); |
| 244 | } |
| 245 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 246 | |
| 247 | LValue CodeGenFunction::EmitDeclRefLValue(const DeclRefExpr *E) { |
| 248 | const Decl *D = E->getDecl(); |
| 249 | if (isa<BlockVarDecl>(D) || isa<ParmVarDecl>(D)) { |
| 250 | llvm::Value *V = LocalDeclMap[D]; |
| 251 | assert(V && "BlockVarDecl not entered in LocalDeclMap?"); |
| 252 | return LValue::MakeAddr(V); |
| 253 | } else if (isa<FunctionDecl>(D) || isa<FileVarDecl>(D)) { |
| 254 | return LValue::MakeAddr(CGM.GetAddrOfGlobalDecl(D)); |
| 255 | } |
| 256 | assert(0 && "Unimp declref"); |
| 257 | } |
| 258 | |
| 259 | LValue CodeGenFunction::EmitUnaryOpLValue(const UnaryOperator *E) { |
| 260 | // __extension__ doesn't affect lvalue-ness. |
| 261 | if (E->getOpcode() == UnaryOperator::Extension) |
| 262 | return EmitLValue(E->getSubExpr()); |
| 263 | |
| 264 | assert(E->getOpcode() == UnaryOperator::Deref && |
| 265 | "'*' is the only unary operator that produces an lvalue"); |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 266 | return LValue::MakeAddr(EmitScalarExpr(E->getSubExpr())); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 267 | } |
| 268 | |
| 269 | LValue CodeGenFunction::EmitStringLiteralLValue(const StringLiteral *E) { |
| 270 | assert(!E->isWide() && "FIXME: Wide strings not supported yet!"); |
| 271 | const char *StrData = E->getStrData(); |
| 272 | unsigned Len = E->getByteLength(); |
| 273 | |
| 274 | // FIXME: Can cache/reuse these within the module. |
| 275 | llvm::Constant *C=llvm::ConstantArray::get(std::string(StrData, StrData+Len)); |
| 276 | |
| 277 | // Create a global variable for this. |
| 278 | C = new llvm::GlobalVariable(C->getType(), true, |
| 279 | llvm::GlobalValue::InternalLinkage, |
| 280 | C, ".str", CurFn->getParent()); |
| 281 | llvm::Constant *Zero = llvm::Constant::getNullValue(llvm::Type::Int32Ty); |
| 282 | llvm::Constant *Zeros[] = { Zero, Zero }; |
| 283 | C = llvm::ConstantExpr::getGetElementPtr(C, Zeros, 2); |
| 284 | return LValue::MakeAddr(C); |
| 285 | } |
| 286 | |
Anders Carlsson | 2274266 | 2007-07-21 05:21:51 +0000 | [diff] [blame] | 287 | LValue CodeGenFunction::EmitPreDefinedLValue(const PreDefinedExpr *E) { |
| 288 | std::string FunctionName(CurFuncDecl->getName()); |
| 289 | std::string GlobalVarName; |
| 290 | |
| 291 | switch (E->getIdentType()) { |
| 292 | default: |
| 293 | assert(0 && "unknown pre-defined ident type"); |
| 294 | case PreDefinedExpr::Func: |
| 295 | GlobalVarName = "__func__."; |
| 296 | break; |
| 297 | case PreDefinedExpr::Function: |
| 298 | GlobalVarName = "__FUNCTION__."; |
| 299 | break; |
| 300 | case PreDefinedExpr::PrettyFunction: |
| 301 | // FIXME:: Demangle C++ method names |
| 302 | GlobalVarName = "__PRETTY_FUNCTION__."; |
| 303 | break; |
| 304 | } |
| 305 | |
| 306 | GlobalVarName += CurFuncDecl->getName(); |
| 307 | |
| 308 | // FIXME: Can cache/reuse these within the module. |
| 309 | llvm::Constant *C=llvm::ConstantArray::get(FunctionName); |
| 310 | |
| 311 | // Create a global variable for this. |
| 312 | C = new llvm::GlobalVariable(C->getType(), true, |
| 313 | llvm::GlobalValue::InternalLinkage, |
| 314 | C, GlobalVarName, CurFn->getParent()); |
| 315 | llvm::Constant *Zero = llvm::Constant::getNullValue(llvm::Type::Int32Ty); |
| 316 | llvm::Constant *Zeros[] = { Zero, Zero }; |
| 317 | C = llvm::ConstantExpr::getGetElementPtr(C, Zeros, 2); |
| 318 | return LValue::MakeAddr(C); |
| 319 | } |
| 320 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 321 | LValue CodeGenFunction::EmitArraySubscriptExpr(const ArraySubscriptExpr *E) { |
Ted Kremenek | 2324512 | 2007-08-20 16:18:38 +0000 | [diff] [blame] | 322 | // 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] | 323 | llvm::Value *Idx = EmitScalarExpr(E->getIdx()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 324 | |
| 325 | // If the base is a vector type, then we are forming a vector element lvalue |
| 326 | // with this subscript. |
Ted Kremenek | 2324512 | 2007-08-20 16:18:38 +0000 | [diff] [blame] | 327 | if (E->getLHS()->getType()->isVectorType()) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 328 | // Emit the vector as an lvalue to get its address. |
Ted Kremenek | 2324512 | 2007-08-20 16:18:38 +0000 | [diff] [blame] | 329 | LValue LHS = EmitLValue(E->getLHS()); |
| 330 | assert(LHS.isSimple() && "Can only subscript lvalue vectors here!"); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 331 | // FIXME: This should properly sign/zero/extend or truncate Idx to i32. |
Ted Kremenek | 2324512 | 2007-08-20 16:18:38 +0000 | [diff] [blame] | 332 | return LValue::MakeVectorElt(LHS.getAddress(), Idx); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 333 | } |
| 334 | |
Ted Kremenek | 2324512 | 2007-08-20 16:18:38 +0000 | [diff] [blame] | 335 | // 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] | 336 | llvm::Value *Base = EmitScalarExpr(E->getBase()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 337 | |
Ted Kremenek | 2324512 | 2007-08-20 16:18:38 +0000 | [diff] [blame] | 338 | // Extend or truncate the index type to 32 or 64-bits. |
Chris Lattner | d4f0802 | 2007-08-08 17:43:05 +0000 | [diff] [blame] | 339 | QualType IdxTy = E->getIdx()->getType(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 340 | bool IdxSigned = IdxTy->isSignedIntegerType(); |
| 341 | unsigned IdxBitwidth = cast<llvm::IntegerType>(Idx->getType())->getBitWidth(); |
| 342 | if (IdxBitwidth != LLVMPointerWidth) |
| 343 | Idx = Builder.CreateIntCast(Idx, llvm::IntegerType::get(LLVMPointerWidth), |
| 344 | IdxSigned, "idxprom"); |
| 345 | |
| 346 | // We know that the pointer points to a type of the correct size, unless the |
| 347 | // size is a VLA. |
Chris Lattner | 590b664 | 2007-07-15 23:26:56 +0000 | [diff] [blame] | 348 | if (!E->getType()->isConstantSizeType(getContext())) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 349 | assert(0 && "VLA idx not implemented"); |
| 350 | return LValue::MakeAddr(Builder.CreateGEP(Base, Idx, "arrayidx")); |
| 351 | } |
| 352 | |
Chris Lattner | 349aaec | 2007-08-02 23:37:31 +0000 | [diff] [blame] | 353 | LValue CodeGenFunction:: |
Chris Lattner | 6481a57 | 2007-08-03 17:31:20 +0000 | [diff] [blame] | 354 | EmitOCUVectorElementExpr(const OCUVectorElementExpr *E) { |
Chris Lattner | 349aaec | 2007-08-02 23:37:31 +0000 | [diff] [blame] | 355 | // Emit the base vector as an l-value. |
| 356 | LValue Base = EmitLValue(E->getBase()); |
| 357 | assert(Base.isSimple() && "Can only subscript lvalue vectors here!"); |
| 358 | |
Chris Lattner | 6481a57 | 2007-08-03 17:31:20 +0000 | [diff] [blame] | 359 | return LValue::MakeOCUVectorElt(Base.getAddress(), |
| 360 | E->getEncodedElementAccess()); |
Chris Lattner | 349aaec | 2007-08-02 23:37:31 +0000 | [diff] [blame] | 361 | } |
| 362 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 363 | //===--------------------------------------------------------------------===// |
| 364 | // Expression Emission |
| 365 | //===--------------------------------------------------------------------===// |
| 366 | |
Chris Lattner | 7016a70 | 2007-08-20 22:37:10 +0000 | [diff] [blame] | 367 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 368 | RValue CodeGenFunction::EmitCallExpr(const CallExpr *E) { |
Anders Carlsson | 022012e | 2007-08-20 18:05:56 +0000 | [diff] [blame] | 369 | if (const ImplicitCastExpr *IcExpr = |
| 370 | dyn_cast<const ImplicitCastExpr>(E->getCallee())) |
| 371 | if (const DeclRefExpr *DRExpr = |
| 372 | dyn_cast<const DeclRefExpr>(IcExpr->getSubExpr())) |
| 373 | if (const FunctionDecl *FDecl = |
| 374 | dyn_cast<const FunctionDecl>(DRExpr->getDecl())) |
| 375 | if (unsigned builtinID = FDecl->getIdentifier()->getBuiltinID()) |
| 376 | return EmitBuiltinExpr(builtinID, E); |
| 377 | |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 378 | llvm::Value *Callee = EmitScalarExpr(E->getCallee()); |
Chris Lattner | c5e940f | 2007-08-31 04:44:06 +0000 | [diff] [blame^] | 379 | return EmitCallExpr(Callee, E); |
| 380 | } |
| 381 | |
| 382 | RValue CodeGenFunction::EmitCallExpr(llvm::Value *Callee, const CallExpr *E) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 383 | // The callee type will always be a pointer to function type, get the function |
| 384 | // type. |
Chris Lattner | d4f0802 | 2007-08-08 17:43:05 +0000 | [diff] [blame] | 385 | QualType CalleeTy = E->getCallee()->getType(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 386 | CalleeTy = cast<PointerType>(CalleeTy.getCanonicalType())->getPointeeType(); |
| 387 | |
| 388 | // Get information about the argument types. |
| 389 | FunctionTypeProto::arg_type_iterator ArgTyIt = 0, ArgTyEnd = 0; |
| 390 | |
| 391 | // Calling unprototyped functions provides no argument info. |
| 392 | if (const FunctionTypeProto *FTP = dyn_cast<FunctionTypeProto>(CalleeTy)) { |
| 393 | ArgTyIt = FTP->arg_type_begin(); |
| 394 | ArgTyEnd = FTP->arg_type_end(); |
| 395 | } |
| 396 | |
| 397 | llvm::SmallVector<llvm::Value*, 16> Args; |
| 398 | |
Chris Lattner | cc666af | 2007-08-10 17:02:28 +0000 | [diff] [blame] | 399 | // Handle struct-return functions by passing a pointer to the location that |
| 400 | // we would like to return into. |
| 401 | if (hasAggregateLLVMType(E->getType())) { |
| 402 | // Create a temporary alloca to hold the result of the call. :( |
| 403 | Args.push_back(CreateTempAlloca(ConvertType(E->getType()))); |
| 404 | // FIXME: set the stret attribute on the argument. |
| 405 | } |
| 406 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 407 | for (unsigned i = 0, e = E->getNumArgs(); i != e; ++i) { |
Chris Lattner | d4f0802 | 2007-08-08 17:43:05 +0000 | [diff] [blame] | 408 | QualType ArgTy = E->getArg(i)->getType(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 409 | |
Chris Lattner | 660ac12 | 2007-08-26 22:55:13 +0000 | [diff] [blame] | 410 | if (!hasAggregateLLVMType(ArgTy)) { |
| 411 | // Scalar argument is passed by-value. |
| 412 | Args.push_back(EmitScalarExpr(E->getArg(i))); |
Chris Lattner | 660ac12 | 2007-08-26 22:55:13 +0000 | [diff] [blame] | 413 | } else if (ArgTy->isComplexType()) { |
| 414 | // Make a temporary alloca to pass the argument. |
| 415 | llvm::Value *DestMem = CreateTempAlloca(ConvertType(ArgTy)); |
| 416 | EmitComplexExprIntoAddr(E->getArg(i), DestMem, false); |
| 417 | Args.push_back(DestMem); |
| 418 | } else { |
| 419 | llvm::Value *DestMem = CreateTempAlloca(ConvertType(ArgTy)); |
| 420 | EmitAggExpr(E->getArg(i), DestMem, false); |
| 421 | Args.push_back(DestMem); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 422 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 423 | } |
| 424 | |
Chris Lattner | bf98651 | 2007-08-01 06:24:52 +0000 | [diff] [blame] | 425 | llvm::Value *V = Builder.CreateCall(Callee, &Args[0], &Args[0]+Args.size()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 426 | if (V->getType() != llvm::Type::VoidTy) |
| 427 | V->setName("call"); |
Chris Lattner | cc666af | 2007-08-10 17:02:28 +0000 | [diff] [blame] | 428 | else if (hasAggregateLLVMType(E->getType())) |
| 429 | // Struct return. |
| 430 | return RValue::getAggregate(Args[0]); |
| 431 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 432 | return RValue::get(V); |
| 433 | } |