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 | // |
| 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" |
| 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 | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 85 | fprintf(stderr, "Unimplemented lvalue expr!\n"); |
Chris Lattner | 1aef621 | 2007-09-13 01:17:29 +0000 | [diff] [blame] | 86 | E->dump(getContext().SourceMgr); |
Chris Lattner | a52c889 | 2007-08-26 05:06:40 +0000 | [diff] [blame] | 87 | llvm::Type *Ty = llvm::PointerType::get(ConvertType(E->getType())); |
| 88 | return LValue::MakeAddr(llvm::UndefValue::get(Ty)); |
| 89 | } |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 90 | |
| 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); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 135 | |
| 136 | assert(0 && "Bitfield ref not impl!"); |
Chris Lattner | 1d2b461 | 2007-09-16 19:23:47 +0000 | [diff] [blame] | 137 | //an invalid RValue, but the assert will |
| 138 | //ensure that this point is never reached |
| 139 | return RValue(); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 140 | } |
| 141 | |
Chris Lattner | 944f796 | 2007-08-03 16:18:34 +0000 | [diff] [blame] | 142 | // If this is a reference to a subset of the elements of a vector, either |
| 143 | // shuffle the input or extract/insert them as appropriate. |
Chris Lattner | a0d03a7 | 2007-08-03 17:31:20 +0000 | [diff] [blame] | 144 | RValue CodeGenFunction::EmitLoadOfOCUElementLValue(LValue LV, |
Chris Lattner | 4b49296 | 2007-08-10 17:10:08 +0000 | [diff] [blame] | 145 | QualType ExprType) { |
Chris Lattner | 944f796 | 2007-08-03 16:18:34 +0000 | [diff] [blame] | 146 | llvm::Value *Vec = Builder.CreateLoad(LV.getOCUVectorAddr(), "tmp"); |
| 147 | |
Chris Lattner | a0d03a7 | 2007-08-03 17:31:20 +0000 | [diff] [blame] | 148 | unsigned EncFields = LV.getOCUVectorElts(); |
Chris Lattner | 944f796 | 2007-08-03 16:18:34 +0000 | [diff] [blame] | 149 | |
| 150 | // If the result of the expression is a non-vector type, we must be |
| 151 | // extracting a single element. Just codegen as an extractelement. |
Chris Lattner | 4b49296 | 2007-08-10 17:10:08 +0000 | [diff] [blame] | 152 | const VectorType *ExprVT = ExprType->getAsVectorType(); |
| 153 | if (!ExprVT) { |
Chris Lattner | a0d03a7 | 2007-08-03 17:31:20 +0000 | [diff] [blame] | 154 | unsigned InIdx = OCUVectorElementExpr::getAccessedFieldNo(0, EncFields); |
Chris Lattner | 944f796 | 2007-08-03 16:18:34 +0000 | [diff] [blame] | 155 | llvm::Value *Elt = llvm::ConstantInt::get(llvm::Type::Int32Ty, InIdx); |
| 156 | return RValue::get(Builder.CreateExtractElement(Vec, Elt, "tmp")); |
| 157 | } |
| 158 | |
| 159 | // If the source and destination have the same number of elements, use a |
| 160 | // vector shuffle instead of insert/extracts. |
Chris Lattner | 4b49296 | 2007-08-10 17:10:08 +0000 | [diff] [blame] | 161 | unsigned NumResultElts = ExprVT->getNumElements(); |
Chris Lattner | 944f796 | 2007-08-03 16:18:34 +0000 | [diff] [blame] | 162 | unsigned NumSourceElts = |
| 163 | cast<llvm::VectorType>(Vec->getType())->getNumElements(); |
| 164 | |
| 165 | if (NumResultElts == NumSourceElts) { |
| 166 | llvm::SmallVector<llvm::Constant*, 4> Mask; |
| 167 | for (unsigned i = 0; i != NumResultElts; ++i) { |
Chris Lattner | a0d03a7 | 2007-08-03 17:31:20 +0000 | [diff] [blame] | 168 | unsigned InIdx = OCUVectorElementExpr::getAccessedFieldNo(i, EncFields); |
Chris Lattner | 944f796 | 2007-08-03 16:18:34 +0000 | [diff] [blame] | 169 | Mask.push_back(llvm::ConstantInt::get(llvm::Type::Int32Ty, InIdx)); |
| 170 | } |
| 171 | |
| 172 | llvm::Value *MaskV = llvm::ConstantVector::get(&Mask[0], Mask.size()); |
| 173 | Vec = Builder.CreateShuffleVector(Vec, |
| 174 | llvm::UndefValue::get(Vec->getType()), |
| 175 | MaskV, "tmp"); |
| 176 | return RValue::get(Vec); |
| 177 | } |
| 178 | |
| 179 | // Start out with an undef of the result type. |
| 180 | llvm::Value *Result = llvm::UndefValue::get(ConvertType(ExprType)); |
| 181 | |
| 182 | // Extract/Insert each element of the result. |
| 183 | for (unsigned i = 0; i != NumResultElts; ++i) { |
Chris Lattner | a0d03a7 | 2007-08-03 17:31:20 +0000 | [diff] [blame] | 184 | unsigned InIdx = OCUVectorElementExpr::getAccessedFieldNo(i, EncFields); |
Chris Lattner | 944f796 | 2007-08-03 16:18:34 +0000 | [diff] [blame] | 185 | llvm::Value *Elt = llvm::ConstantInt::get(llvm::Type::Int32Ty, InIdx); |
| 186 | Elt = Builder.CreateExtractElement(Vec, Elt, "tmp"); |
| 187 | |
| 188 | llvm::Value *OutIdx = llvm::ConstantInt::get(llvm::Type::Int32Ty, i); |
| 189 | Result = Builder.CreateInsertElement(Result, Elt, OutIdx, "tmp"); |
| 190 | } |
| 191 | |
| 192 | return RValue::get(Result); |
| 193 | } |
| 194 | |
| 195 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 196 | |
| 197 | /// EmitStoreThroughLValue - Store the specified rvalue into the specified |
| 198 | /// lvalue, where both are guaranteed to the have the same type, and that type |
| 199 | /// is 'Ty'. |
| 200 | void CodeGenFunction::EmitStoreThroughLValue(RValue Src, LValue Dst, |
| 201 | QualType Ty) { |
Chris Lattner | 5bfdd23 | 2007-08-03 16:28:33 +0000 | [diff] [blame] | 202 | if (!Dst.isSimple()) { |
| 203 | if (Dst.isVectorElt()) { |
| 204 | // Read/modify/write the vector, inserting the new element. |
| 205 | // FIXME: Volatility. |
| 206 | llvm::Value *Vec = Builder.CreateLoad(Dst.getVectorAddr(), "tmp"); |
Chris Lattner | e24c4cf | 2007-08-31 22:49:20 +0000 | [diff] [blame] | 207 | Vec = Builder.CreateInsertElement(Vec, Src.getScalarVal(), |
Chris Lattner | 5bfdd23 | 2007-08-03 16:28:33 +0000 | [diff] [blame] | 208 | Dst.getVectorIdx(), "vecins"); |
| 209 | Builder.CreateStore(Vec, Dst.getVectorAddr()); |
| 210 | return; |
| 211 | } |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 212 | |
Chris Lattner | 5bfdd23 | 2007-08-03 16:28:33 +0000 | [diff] [blame] | 213 | // If this is an update of elements of a vector, insert them as appropriate. |
Chris Lattner | a0d03a7 | 2007-08-03 17:31:20 +0000 | [diff] [blame] | 214 | if (Dst.isOCUVectorElt()) |
Chris Lattner | 5bfdd23 | 2007-08-03 16:28:33 +0000 | [diff] [blame] | 215 | return EmitStoreThroughOCUComponentLValue(Src, Dst, Ty); |
| 216 | |
| 217 | assert(0 && "FIXME: Don't support store to bitfield yet"); |
| 218 | } |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 219 | |
| 220 | llvm::Value *DstAddr = Dst.getAddress(); |
Chris Lattner | bdb8ffb | 2007-08-11 00:04:45 +0000 | [diff] [blame] | 221 | assert(Src.isScalar() && "Can't emit an agg store with this method"); |
| 222 | // FIXME: Handle volatility etc. |
Chris Lattner | e24c4cf | 2007-08-31 22:49:20 +0000 | [diff] [blame] | 223 | const llvm::Type *SrcTy = Src.getScalarVal()->getType(); |
Chris Lattner | bdb8ffb | 2007-08-11 00:04:45 +0000 | [diff] [blame] | 224 | const llvm::Type *AddrTy = |
| 225 | cast<llvm::PointerType>(DstAddr->getType())->getElementType(); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 226 | |
Chris Lattner | bdb8ffb | 2007-08-11 00:04:45 +0000 | [diff] [blame] | 227 | if (AddrTy != SrcTy) |
| 228 | DstAddr = Builder.CreateBitCast(DstAddr, llvm::PointerType::get(SrcTy), |
| 229 | "storetmp"); |
Chris Lattner | e24c4cf | 2007-08-31 22:49:20 +0000 | [diff] [blame] | 230 | Builder.CreateStore(Src.getScalarVal(), DstAddr); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 231 | } |
| 232 | |
Chris Lattner | 5bfdd23 | 2007-08-03 16:28:33 +0000 | [diff] [blame] | 233 | void CodeGenFunction::EmitStoreThroughOCUComponentLValue(RValue Src, LValue Dst, |
| 234 | QualType Ty) { |
| 235 | // This access turns into a read/modify/write of the vector. Load the input |
| 236 | // value now. |
| 237 | llvm::Value *Vec = Builder.CreateLoad(Dst.getOCUVectorAddr(), "tmp"); |
| 238 | // FIXME: Volatility. |
Chris Lattner | a0d03a7 | 2007-08-03 17:31:20 +0000 | [diff] [blame] | 239 | unsigned EncFields = Dst.getOCUVectorElts(); |
Chris Lattner | 5bfdd23 | 2007-08-03 16:28:33 +0000 | [diff] [blame] | 240 | |
Chris Lattner | e24c4cf | 2007-08-31 22:49:20 +0000 | [diff] [blame] | 241 | llvm::Value *SrcVal = Src.getScalarVal(); |
Chris Lattner | 5bfdd23 | 2007-08-03 16:28:33 +0000 | [diff] [blame] | 242 | |
Chris Lattner | 940966d | 2007-08-03 16:37:04 +0000 | [diff] [blame] | 243 | if (const VectorType *VTy = Ty->getAsVectorType()) { |
| 244 | unsigned NumSrcElts = VTy->getNumElements(); |
| 245 | |
| 246 | // Extract/Insert each element. |
| 247 | for (unsigned i = 0; i != NumSrcElts; ++i) { |
| 248 | llvm::Value *Elt = llvm::ConstantInt::get(llvm::Type::Int32Ty, i); |
| 249 | Elt = Builder.CreateExtractElement(SrcVal, Elt, "tmp"); |
| 250 | |
Chris Lattner | a0d03a7 | 2007-08-03 17:31:20 +0000 | [diff] [blame] | 251 | unsigned Idx = OCUVectorElementExpr::getAccessedFieldNo(i, EncFields); |
Chris Lattner | 940966d | 2007-08-03 16:37:04 +0000 | [diff] [blame] | 252 | llvm::Value *OutIdx = llvm::ConstantInt::get(llvm::Type::Int32Ty, Idx); |
| 253 | Vec = Builder.CreateInsertElement(Vec, Elt, OutIdx, "tmp"); |
| 254 | } |
| 255 | } else { |
| 256 | // 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] | 257 | unsigned InIdx = OCUVectorElementExpr::getAccessedFieldNo(0, EncFields); |
Chris Lattner | 5bfdd23 | 2007-08-03 16:28:33 +0000 | [diff] [blame] | 258 | llvm::Value *Elt = llvm::ConstantInt::get(llvm::Type::Int32Ty, InIdx); |
| 259 | Vec = Builder.CreateInsertElement(Vec, SrcVal, Elt, "tmp"); |
Chris Lattner | 5bfdd23 | 2007-08-03 16:28:33 +0000 | [diff] [blame] | 260 | } |
| 261 | |
Chris Lattner | 5bfdd23 | 2007-08-03 16:28:33 +0000 | [diff] [blame] | 262 | Builder.CreateStore(Vec, Dst.getOCUVectorAddr()); |
| 263 | } |
| 264 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 265 | |
| 266 | LValue CodeGenFunction::EmitDeclRefLValue(const DeclRefExpr *E) { |
Steve Naroff | cb59747 | 2007-09-13 21:41:19 +0000 | [diff] [blame] | 267 | const ValueDecl *D = E->getDecl(); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 268 | if (isa<BlockVarDecl>(D) || isa<ParmVarDecl>(D)) { |
| 269 | llvm::Value *V = LocalDeclMap[D]; |
| 270 | assert(V && "BlockVarDecl not entered in LocalDeclMap?"); |
| 271 | return LValue::MakeAddr(V); |
| 272 | } else if (isa<FunctionDecl>(D) || isa<FileVarDecl>(D)) { |
| 273 | return LValue::MakeAddr(CGM.GetAddrOfGlobalDecl(D)); |
| 274 | } |
| 275 | assert(0 && "Unimp declref"); |
Chris Lattner | 1d2b461 | 2007-09-16 19:23:47 +0000 | [diff] [blame] | 276 | //an invalid LValue, but the assert will |
| 277 | //ensure that this point is never reached. |
| 278 | return LValue(); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 279 | } |
| 280 | |
| 281 | LValue CodeGenFunction::EmitUnaryOpLValue(const UnaryOperator *E) { |
| 282 | // __extension__ doesn't affect lvalue-ness. |
| 283 | if (E->getOpcode() == UnaryOperator::Extension) |
| 284 | return EmitLValue(E->getSubExpr()); |
| 285 | |
| 286 | assert(E->getOpcode() == UnaryOperator::Deref && |
| 287 | "'*' is the only unary operator that produces an lvalue"); |
Chris Lattner | 9fba49a | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 288 | return LValue::MakeAddr(EmitScalarExpr(E->getSubExpr())); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 289 | } |
| 290 | |
| 291 | LValue CodeGenFunction::EmitStringLiteralLValue(const StringLiteral *E) { |
| 292 | assert(!E->isWide() && "FIXME: Wide strings not supported yet!"); |
| 293 | const char *StrData = E->getStrData(); |
| 294 | unsigned Len = E->getByteLength(); |
| 295 | |
| 296 | // FIXME: Can cache/reuse these within the module. |
| 297 | llvm::Constant *C=llvm::ConstantArray::get(std::string(StrData, StrData+Len)); |
| 298 | |
| 299 | // Create a global variable for this. |
| 300 | C = new llvm::GlobalVariable(C->getType(), true, |
| 301 | llvm::GlobalValue::InternalLinkage, |
| 302 | C, ".str", CurFn->getParent()); |
| 303 | llvm::Constant *Zero = llvm::Constant::getNullValue(llvm::Type::Int32Ty); |
| 304 | llvm::Constant *Zeros[] = { Zero, Zero }; |
| 305 | C = llvm::ConstantExpr::getGetElementPtr(C, Zeros, 2); |
| 306 | return LValue::MakeAddr(C); |
| 307 | } |
| 308 | |
| 309 | LValue CodeGenFunction::EmitPreDefinedLValue(const PreDefinedExpr *E) { |
| 310 | std::string FunctionName(CurFuncDecl->getName()); |
| 311 | std::string GlobalVarName; |
| 312 | |
| 313 | switch (E->getIdentType()) { |
| 314 | default: |
| 315 | assert(0 && "unknown pre-defined ident type"); |
| 316 | case PreDefinedExpr::Func: |
| 317 | GlobalVarName = "__func__."; |
| 318 | break; |
| 319 | case PreDefinedExpr::Function: |
| 320 | GlobalVarName = "__FUNCTION__."; |
| 321 | break; |
| 322 | case PreDefinedExpr::PrettyFunction: |
| 323 | // FIXME:: Demangle C++ method names |
| 324 | GlobalVarName = "__PRETTY_FUNCTION__."; |
| 325 | break; |
| 326 | } |
| 327 | |
| 328 | GlobalVarName += CurFuncDecl->getName(); |
| 329 | |
| 330 | // FIXME: Can cache/reuse these within the module. |
| 331 | llvm::Constant *C=llvm::ConstantArray::get(FunctionName); |
| 332 | |
| 333 | // Create a global variable for this. |
| 334 | C = new llvm::GlobalVariable(C->getType(), true, |
| 335 | llvm::GlobalValue::InternalLinkage, |
| 336 | C, GlobalVarName, CurFn->getParent()); |
| 337 | llvm::Constant *Zero = llvm::Constant::getNullValue(llvm::Type::Int32Ty); |
| 338 | llvm::Constant *Zeros[] = { Zero, Zero }; |
| 339 | C = llvm::ConstantExpr::getGetElementPtr(C, Zeros, 2); |
| 340 | return LValue::MakeAddr(C); |
| 341 | } |
| 342 | |
| 343 | LValue CodeGenFunction::EmitArraySubscriptExpr(const ArraySubscriptExpr *E) { |
Ted Kremenek | 1c1700f | 2007-08-20 16:18:38 +0000 | [diff] [blame] | 344 | // 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] | 345 | llvm::Value *Idx = EmitScalarExpr(E->getIdx()); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 346 | |
| 347 | // If the base is a vector type, then we are forming a vector element lvalue |
| 348 | // with this subscript. |
Ted Kremenek | 1c1700f | 2007-08-20 16:18:38 +0000 | [diff] [blame] | 349 | if (E->getLHS()->getType()->isVectorType()) { |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 350 | // Emit the vector as an lvalue to get its address. |
Ted Kremenek | 1c1700f | 2007-08-20 16:18:38 +0000 | [diff] [blame] | 351 | LValue LHS = EmitLValue(E->getLHS()); |
| 352 | assert(LHS.isSimple() && "Can only subscript lvalue vectors here!"); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 353 | // FIXME: This should properly sign/zero/extend or truncate Idx to i32. |
Ted Kremenek | 1c1700f | 2007-08-20 16:18:38 +0000 | [diff] [blame] | 354 | return LValue::MakeVectorElt(LHS.getAddress(), Idx); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 355 | } |
| 356 | |
Ted Kremenek | 1c1700f | 2007-08-20 16:18:38 +0000 | [diff] [blame] | 357 | // 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] | 358 | llvm::Value *Base = EmitScalarExpr(E->getBase()); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 359 | |
Ted Kremenek | 1c1700f | 2007-08-20 16:18:38 +0000 | [diff] [blame] | 360 | // Extend or truncate the index type to 32 or 64-bits. |
Chris Lattner | 2af72ac | 2007-08-08 17:43:05 +0000 | [diff] [blame] | 361 | QualType IdxTy = E->getIdx()->getType(); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 362 | bool IdxSigned = IdxTy->isSignedIntegerType(); |
| 363 | unsigned IdxBitwidth = cast<llvm::IntegerType>(Idx->getType())->getBitWidth(); |
| 364 | if (IdxBitwidth != LLVMPointerWidth) |
| 365 | Idx = Builder.CreateIntCast(Idx, llvm::IntegerType::get(LLVMPointerWidth), |
| 366 | IdxSigned, "idxprom"); |
| 367 | |
| 368 | // We know that the pointer points to a type of the correct size, unless the |
| 369 | // size is a VLA. |
| 370 | if (!E->getType()->isConstantSizeType(getContext())) |
| 371 | assert(0 && "VLA idx not implemented"); |
| 372 | return LValue::MakeAddr(Builder.CreateGEP(Base, Idx, "arrayidx")); |
| 373 | } |
| 374 | |
Chris Lattner | 6552019 | 2007-08-02 23:37:31 +0000 | [diff] [blame] | 375 | LValue CodeGenFunction:: |
Chris Lattner | a0d03a7 | 2007-08-03 17:31:20 +0000 | [diff] [blame] | 376 | EmitOCUVectorElementExpr(const OCUVectorElementExpr *E) { |
Chris Lattner | 6552019 | 2007-08-02 23:37:31 +0000 | [diff] [blame] | 377 | // Emit the base vector as an l-value. |
| 378 | LValue Base = EmitLValue(E->getBase()); |
| 379 | assert(Base.isSimple() && "Can only subscript lvalue vectors here!"); |
| 380 | |
Chris Lattner | a0d03a7 | 2007-08-03 17:31:20 +0000 | [diff] [blame] | 381 | return LValue::MakeOCUVectorElt(Base.getAddress(), |
| 382 | E->getEncodedElementAccess()); |
Chris Lattner | 6552019 | 2007-08-02 23:37:31 +0000 | [diff] [blame] | 383 | } |
| 384 | |
Devang Patel | 41b6625 | 2007-10-23 20:28:39 +0000 | [diff] [blame] | 385 | LValue CodeGenFunction::EmitMemberExpr(const MemberExpr *E) { |
| 386 | |
Devang Patel | 9dd3e2b | 2007-10-24 22:26:28 +0000 | [diff] [blame] | 387 | Expr *BaseExpr = E->getBase(); |
Devang Patel | 41b6625 | 2007-10-23 20:28:39 +0000 | [diff] [blame] | 388 | // FIXME: Handle union members. |
Devang Patel | 9dd3e2b | 2007-10-24 22:26:28 +0000 | [diff] [blame] | 389 | if (BaseExpr->getType()->isUnionType()) { |
Devang Patel | 41b6625 | 2007-10-23 20:28:39 +0000 | [diff] [blame] | 390 | fprintf(stderr, "Unimplemented lvalue expr!\n"); |
| 391 | E->dump(getContext().SourceMgr); |
| 392 | llvm::Type *Ty = llvm::PointerType::get(ConvertType(E->getType())); |
| 393 | return LValue::MakeAddr(llvm::UndefValue::get(Ty)); |
| 394 | } |
Devang Patel | 9dd3e2b | 2007-10-24 22:26:28 +0000 | [diff] [blame] | 395 | |
| 396 | llvm::Value *BaseValue = NULL; |
Devang Patel | 2b24fd9 | 2007-10-26 18:15:21 +0000 | [diff] [blame^] | 397 | if (BaseExpr->isLvalue() == Expr::LV_Valid) { |
Devang Patel | 9dd3e2b | 2007-10-24 22:26:28 +0000 | [diff] [blame] | 398 | LValue BaseLV = EmitLValue(BaseExpr); |
| 399 | BaseValue = BaseLV.getAddress(); |
Devang Patel | 2b24fd9 | 2007-10-26 18:15:21 +0000 | [diff] [blame^] | 400 | |
Devang Patel | 9dd3e2b | 2007-10-24 22:26:28 +0000 | [diff] [blame] | 401 | if (E->isArrow()) { |
| 402 | QualType PTy = cast<PointerType>(BaseExpr->getType())->getPointeeType(); |
| 403 | BaseValue = Builder.CreateBitCast(BaseValue, |
Devang Patel | 2b24fd9 | 2007-10-26 18:15:21 +0000 | [diff] [blame^] | 404 | llvm::PointerType::get(ConvertType(PTy)), |
Devang Patel | 9dd3e2b | 2007-10-24 22:26:28 +0000 | [diff] [blame] | 405 | "tmp"); |
| 406 | } |
Devang Patel | 2b24fd9 | 2007-10-26 18:15:21 +0000 | [diff] [blame^] | 407 | } else |
| 408 | BaseValue = EmitScalarExpr(BaseExpr); |
Devang Patel | 41b6625 | 2007-10-23 20:28:39 +0000 | [diff] [blame] | 409 | |
| 410 | FieldDecl *Field = E->getMemberDecl(); |
| 411 | unsigned idx = CGM.getTypes().getLLVMFieldNo(Field); |
| 412 | llvm::Value *Idxs[2] = { llvm::Constant::getNullValue(llvm::Type::Int32Ty), |
Devang Patel | 30f6f13 | 2007-10-24 00:26:24 +0000 | [diff] [blame] | 413 | llvm::ConstantInt::get(llvm::Type::Int32Ty, idx) }; |
Devang Patel | 41b6625 | 2007-10-23 20:28:39 +0000 | [diff] [blame] | 414 | |
| 415 | return LValue::MakeAddr(Builder.CreateGEP(BaseValue,Idxs, Idxs + 2, "tmp")); |
| 416 | |
| 417 | // FIXME: If record field does not have one to one match with llvm::StructType |
| 418 | // field then apply appropriate masks to select only member field bits. |
| 419 | } |
| 420 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 421 | //===--------------------------------------------------------------------===// |
| 422 | // Expression Emission |
| 423 | //===--------------------------------------------------------------------===// |
| 424 | |
Chris Lattner | b2cb9cb | 2007-08-20 22:37:10 +0000 | [diff] [blame] | 425 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 426 | RValue CodeGenFunction::EmitCallExpr(const CallExpr *E) { |
Anders Carlsson | 4986530 | 2007-08-20 18:05:56 +0000 | [diff] [blame] | 427 | if (const ImplicitCastExpr *IcExpr = |
| 428 | dyn_cast<const ImplicitCastExpr>(E->getCallee())) |
| 429 | if (const DeclRefExpr *DRExpr = |
| 430 | dyn_cast<const DeclRefExpr>(IcExpr->getSubExpr())) |
| 431 | if (const FunctionDecl *FDecl = |
| 432 | dyn_cast<const FunctionDecl>(DRExpr->getDecl())) |
| 433 | if (unsigned builtinID = FDecl->getIdentifier()->getBuiltinID()) |
| 434 | return EmitBuiltinExpr(builtinID, E); |
| 435 | |
Chris Lattner | 9fba49a | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 436 | llvm::Value *Callee = EmitScalarExpr(E->getCallee()); |
Chris Lattner | 02c60f5 | 2007-08-31 04:44:06 +0000 | [diff] [blame] | 437 | return EmitCallExpr(Callee, E); |
| 438 | } |
| 439 | |
| 440 | RValue CodeGenFunction::EmitCallExpr(llvm::Value *Callee, const CallExpr *E) { |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 441 | // The callee type will always be a pointer to function type, get the function |
| 442 | // type. |
Chris Lattner | 2af72ac | 2007-08-08 17:43:05 +0000 | [diff] [blame] | 443 | QualType CalleeTy = E->getCallee()->getType(); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 444 | CalleeTy = cast<PointerType>(CalleeTy.getCanonicalType())->getPointeeType(); |
| 445 | |
| 446 | // Get information about the argument types. |
| 447 | FunctionTypeProto::arg_type_iterator ArgTyIt = 0, ArgTyEnd = 0; |
| 448 | |
| 449 | // Calling unprototyped functions provides no argument info. |
| 450 | if (const FunctionTypeProto *FTP = dyn_cast<FunctionTypeProto>(CalleeTy)) { |
| 451 | ArgTyIt = FTP->arg_type_begin(); |
| 452 | ArgTyEnd = FTP->arg_type_end(); |
| 453 | } |
| 454 | |
| 455 | llvm::SmallVector<llvm::Value*, 16> Args; |
| 456 | |
Chris Lattner | 5980204 | 2007-08-10 17:02:28 +0000 | [diff] [blame] | 457 | // Handle struct-return functions by passing a pointer to the location that |
| 458 | // we would like to return into. |
| 459 | if (hasAggregateLLVMType(E->getType())) { |
| 460 | // Create a temporary alloca to hold the result of the call. :( |
| 461 | Args.push_back(CreateTempAlloca(ConvertType(E->getType()))); |
| 462 | // FIXME: set the stret attribute on the argument. |
| 463 | } |
| 464 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 465 | for (unsigned i = 0, e = E->getNumArgs(); i != e; ++i) { |
Chris Lattner | 2af72ac | 2007-08-08 17:43:05 +0000 | [diff] [blame] | 466 | QualType ArgTy = E->getArg(i)->getType(); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 467 | |
Chris Lattner | b06c8dd | 2007-08-26 22:55:13 +0000 | [diff] [blame] | 468 | if (!hasAggregateLLVMType(ArgTy)) { |
| 469 | // Scalar argument is passed by-value. |
| 470 | Args.push_back(EmitScalarExpr(E->getArg(i))); |
Chris Lattner | b06c8dd | 2007-08-26 22:55:13 +0000 | [diff] [blame] | 471 | } else if (ArgTy->isComplexType()) { |
| 472 | // Make a temporary alloca to pass the argument. |
| 473 | llvm::Value *DestMem = CreateTempAlloca(ConvertType(ArgTy)); |
| 474 | EmitComplexExprIntoAddr(E->getArg(i), DestMem, false); |
| 475 | Args.push_back(DestMem); |
| 476 | } else { |
| 477 | llvm::Value *DestMem = CreateTempAlloca(ConvertType(ArgTy)); |
| 478 | EmitAggExpr(E->getArg(i), DestMem, false); |
| 479 | Args.push_back(DestMem); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 480 | } |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 481 | } |
| 482 | |
Chris Lattner | a957225 | 2007-08-01 06:24:52 +0000 | [diff] [blame] | 483 | llvm::Value *V = Builder.CreateCall(Callee, &Args[0], &Args[0]+Args.size()); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 484 | if (V->getType() != llvm::Type::VoidTy) |
| 485 | V->setName("call"); |
Chris Lattner | e24c4cf | 2007-08-31 22:49:20 +0000 | [diff] [blame] | 486 | else if (E->getType()->isComplexType()) |
| 487 | return RValue::getComplex(LoadComplexFromAddr(Args[0], false)); |
Chris Lattner | 5980204 | 2007-08-10 17:02:28 +0000 | [diff] [blame] | 488 | else if (hasAggregateLLVMType(E->getType())) |
| 489 | // Struct return. |
| 490 | return RValue::getAggregate(Args[0]); |
| 491 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 492 | return RValue::get(V); |
| 493 | } |