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