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" |
Anders Carlsson | 4986530 | 2007-08-20 18:05:56 +0000 | [diff] [blame] | 17 | #include "clang/Lex/IdentifierTable.h" |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 18 | #include "llvm/Constants.h" |
| 19 | #include "llvm/DerivedTypes.h" |
| 20 | #include "llvm/Function.h" |
| 21 | #include "llvm/GlobalVariable.h" |
| 22 | #include "llvm/Support/MathExtras.h" |
| 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 | 5ac222c | 2007-08-24 00:01:20 +0000 | [diff] [blame] | 40 | return ConvertScalarValueToBool(EmitAnyExpr(E), E->getType()); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 41 | } |
| 42 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 43 | //===--------------------------------------------------------------------===// |
| 44 | // Conversions |
| 45 | //===--------------------------------------------------------------------===// |
| 46 | |
| 47 | /// EmitConversion - Convert the value specied by Val, whose type is ValTy, to |
| 48 | /// the type specified by DstTy, following the rules of C99 6.3. |
| 49 | RValue CodeGenFunction::EmitConversion(RValue Val, QualType ValTy, |
| 50 | QualType DstTy) { |
| 51 | ValTy = ValTy.getCanonicalType(); |
| 52 | DstTy = DstTy.getCanonicalType(); |
| 53 | if (ValTy == DstTy) return Val; |
| 54 | |
| 55 | // Handle conversions to bool first, they are special: comparisons against 0. |
| 56 | if (const BuiltinType *DestBT = dyn_cast<BuiltinType>(DstTy)) |
| 57 | if (DestBT->getKind() == BuiltinType::Bool) |
| 58 | return RValue::get(ConvertScalarValueToBool(Val, ValTy)); |
| 59 | |
| 60 | // Handle pointer conversions next: pointers can only be converted to/from |
| 61 | // other pointers and integers. |
| 62 | if (isa<PointerType>(DstTy)) { |
| 63 | const llvm::Type *DestTy = ConvertType(DstTy); |
| 64 | |
Chris Lattner | 2a42017 | 2007-08-10 16:33:59 +0000 | [diff] [blame] | 65 | if (Val.getVal()->getType() == DestTy) |
| 66 | return Val; |
| 67 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 68 | // The source value may be an integer, or a pointer. |
| 69 | assert(Val.isScalar() && "Can only convert from integer or pointer"); |
| 70 | if (isa<llvm::PointerType>(Val.getVal()->getType())) |
| 71 | return RValue::get(Builder.CreateBitCast(Val.getVal(), DestTy, "conv")); |
| 72 | assert(ValTy->isIntegerType() && "Not ptr->ptr or int->ptr conversion?"); |
| 73 | return RValue::get(Builder.CreateIntToPtr(Val.getVal(), DestTy, "conv")); |
| 74 | } |
| 75 | |
| 76 | if (isa<PointerType>(ValTy)) { |
| 77 | // Must be an ptr to int cast. |
| 78 | const llvm::Type *DestTy = ConvertType(DstTy); |
| 79 | assert(isa<llvm::IntegerType>(DestTy) && "not ptr->int?"); |
| 80 | return RValue::get(Builder.CreateIntToPtr(Val.getVal(), DestTy, "conv")); |
| 81 | } |
| 82 | |
| 83 | // Finally, we have the arithmetic types: real int/float and complex |
| 84 | // int/float. Handle real->real conversions first, they are the most |
| 85 | // common. |
| 86 | if (Val.isScalar() && DstTy->isRealType()) { |
| 87 | // We know that these are representable as scalars in LLVM, convert to LLVM |
| 88 | // types since they are easier to reason about. |
| 89 | llvm::Value *SrcVal = Val.getVal(); |
| 90 | const llvm::Type *DestTy = ConvertType(DstTy); |
| 91 | if (SrcVal->getType() == DestTy) return Val; |
| 92 | |
| 93 | llvm::Value *Result; |
| 94 | if (isa<llvm::IntegerType>(SrcVal->getType())) { |
| 95 | bool InputSigned = ValTy->isSignedIntegerType(); |
| 96 | if (isa<llvm::IntegerType>(DestTy)) |
| 97 | Result = Builder.CreateIntCast(SrcVal, DestTy, InputSigned, "conv"); |
| 98 | else if (InputSigned) |
| 99 | Result = Builder.CreateSIToFP(SrcVal, DestTy, "conv"); |
| 100 | else |
| 101 | Result = Builder.CreateUIToFP(SrcVal, DestTy, "conv"); |
| 102 | } else { |
| 103 | assert(SrcVal->getType()->isFloatingPoint() && "Unknown real conversion"); |
| 104 | if (isa<llvm::IntegerType>(DestTy)) { |
| 105 | if (DstTy->isSignedIntegerType()) |
| 106 | Result = Builder.CreateFPToSI(SrcVal, DestTy, "conv"); |
| 107 | else |
| 108 | Result = Builder.CreateFPToUI(SrcVal, DestTy, "conv"); |
| 109 | } else { |
| 110 | assert(DestTy->isFloatingPoint() && "Unknown real conversion"); |
| 111 | if (DestTy->getTypeID() < SrcVal->getType()->getTypeID()) |
| 112 | Result = Builder.CreateFPTrunc(SrcVal, DestTy, "conv"); |
| 113 | else |
| 114 | Result = Builder.CreateFPExt(SrcVal, DestTy, "conv"); |
| 115 | } |
| 116 | } |
| 117 | return RValue::get(Result); |
| 118 | } |
| 119 | |
| 120 | assert(0 && "FIXME: We don't support complex conversions yet!"); |
| 121 | } |
| 122 | |
| 123 | |
| 124 | /// ConvertScalarValueToBool - Convert the specified expression value to a |
| 125 | /// boolean (i1) truth value. This is equivalent to "Val == 0". |
| 126 | llvm::Value *CodeGenFunction::ConvertScalarValueToBool(RValue Val, QualType Ty){ |
| 127 | Ty = Ty.getCanonicalType(); |
| 128 | llvm::Value *Result; |
| 129 | if (const BuiltinType *BT = dyn_cast<BuiltinType>(Ty)) { |
| 130 | switch (BT->getKind()) { |
| 131 | default: assert(0 && "Unknown scalar value"); |
| 132 | case BuiltinType::Bool: |
| 133 | Result = Val.getVal(); |
| 134 | // Bool is already evaluated right. |
| 135 | assert(Result->getType() == llvm::Type::Int1Ty && |
| 136 | "Unexpected bool value type!"); |
| 137 | return Result; |
| 138 | case BuiltinType::Char_S: |
| 139 | case BuiltinType::Char_U: |
| 140 | case BuiltinType::SChar: |
| 141 | case BuiltinType::UChar: |
| 142 | case BuiltinType::Short: |
| 143 | case BuiltinType::UShort: |
| 144 | case BuiltinType::Int: |
| 145 | case BuiltinType::UInt: |
| 146 | case BuiltinType::Long: |
| 147 | case BuiltinType::ULong: |
| 148 | case BuiltinType::LongLong: |
| 149 | case BuiltinType::ULongLong: |
| 150 | // Code below handles simple integers. |
| 151 | break; |
| 152 | case BuiltinType::Float: |
| 153 | case BuiltinType::Double: |
| 154 | case BuiltinType::LongDouble: { |
| 155 | // Compare against 0.0 for fp scalars. |
| 156 | Result = Val.getVal(); |
| 157 | llvm::Value *Zero = llvm::Constant::getNullValue(Result->getType()); |
| 158 | // FIXME: llvm-gcc produces a une comparison: validate this is right. |
| 159 | Result = Builder.CreateFCmpUNE(Result, Zero, "tobool"); |
| 160 | return Result; |
| 161 | } |
| 162 | } |
Chris Lattner | 5ac222c | 2007-08-24 00:01:20 +0000 | [diff] [blame] | 163 | } else if (isa<ComplexType>(Ty)) { |
| 164 | assert(0 && "implement complex -> bool"); |
| 165 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 166 | } else { |
Chris Lattner | 5ac222c | 2007-08-24 00:01:20 +0000 | [diff] [blame] | 167 | assert((isa<PointerType>(Ty) || |
| 168 | (isa<TagType>(Ty) && |
| 169 | cast<TagType>(Ty)->getDecl()->getKind() == Decl::Enum)) && |
| 170 | "Unknown Type"); |
| 171 | // Code below handles this case fine. |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 172 | } |
| 173 | |
| 174 | // Usual case for integers, pointers, and enums: compare against zero. |
| 175 | Result = Val.getVal(); |
| 176 | |
| 177 | // Because of the type rules of C, we often end up computing a logical value, |
| 178 | // then zero extending it to int, then wanting it as a logical value again. |
| 179 | // Optimize this common case. |
| 180 | if (llvm::ZExtInst *ZI = dyn_cast<llvm::ZExtInst>(Result)) { |
| 181 | if (ZI->getOperand(0)->getType() == llvm::Type::Int1Ty) { |
| 182 | Result = ZI->getOperand(0); |
| 183 | ZI->eraseFromParent(); |
| 184 | return Result; |
| 185 | } |
| 186 | } |
| 187 | |
| 188 | llvm::Value *Zero = llvm::Constant::getNullValue(Result->getType()); |
| 189 | return Builder.CreateICmpNE(Result, Zero, "tobool"); |
| 190 | } |
| 191 | |
| 192 | //===----------------------------------------------------------------------===// |
| 193 | // LValue Expression Emission |
| 194 | //===----------------------------------------------------------------------===// |
| 195 | |
| 196 | /// EmitLValue - Emit code to compute a designator that specifies the location |
| 197 | /// of the expression. |
| 198 | /// |
| 199 | /// This can return one of two things: a simple address or a bitfield |
| 200 | /// reference. In either case, the LLVM Value* in the LValue structure is |
| 201 | /// guaranteed to be an LLVM pointer type. |
| 202 | /// |
| 203 | /// If this returns a bitfield reference, nothing about the pointee type of |
| 204 | /// the LLVM value is known: For example, it may not be a pointer to an |
| 205 | /// integer. |
| 206 | /// |
| 207 | /// If this returns a normal address, and if the lvalue's C type is fixed |
| 208 | /// size, this method guarantees that the returned pointer type will point to |
| 209 | /// an LLVM type of the same size of the lvalue's type. If the lvalue has a |
| 210 | /// variable length type, this is not possible. |
| 211 | /// |
| 212 | LValue CodeGenFunction::EmitLValue(const Expr *E) { |
| 213 | switch (E->getStmtClass()) { |
Chris Lattner | a52c889 | 2007-08-26 05:06:40 +0000 | [diff] [blame^] | 214 | default: { |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 215 | fprintf(stderr, "Unimplemented lvalue expr!\n"); |
| 216 | E->dump(); |
Chris Lattner | a52c889 | 2007-08-26 05:06:40 +0000 | [diff] [blame^] | 217 | llvm::Type *Ty = llvm::PointerType::get(ConvertType(E->getType())); |
| 218 | return LValue::MakeAddr(llvm::UndefValue::get(Ty)); |
| 219 | } |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 220 | |
| 221 | case Expr::DeclRefExprClass: return EmitDeclRefLValue(cast<DeclRefExpr>(E)); |
| 222 | case Expr::ParenExprClass:return EmitLValue(cast<ParenExpr>(E)->getSubExpr()); |
| 223 | case Expr::PreDefinedExprClass: |
| 224 | return EmitPreDefinedLValue(cast<PreDefinedExpr>(E)); |
| 225 | case Expr::StringLiteralClass: |
| 226 | return EmitStringLiteralLValue(cast<StringLiteral>(E)); |
| 227 | |
| 228 | case Expr::UnaryOperatorClass: |
| 229 | return EmitUnaryOpLValue(cast<UnaryOperator>(E)); |
| 230 | case Expr::ArraySubscriptExprClass: |
| 231 | return EmitArraySubscriptExpr(cast<ArraySubscriptExpr>(E)); |
Chris Lattner | a0d03a7 | 2007-08-03 17:31:20 +0000 | [diff] [blame] | 232 | case Expr::OCUVectorElementExprClass: |
| 233 | return EmitOCUVectorElementExpr(cast<OCUVectorElementExpr>(E)); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 234 | } |
| 235 | } |
| 236 | |
| 237 | /// EmitLoadOfLValue - Given an expression that represents a value lvalue, |
| 238 | /// this method emits the address of the lvalue, then loads the result as an |
| 239 | /// rvalue, returning the rvalue. |
| 240 | RValue CodeGenFunction::EmitLoadOfLValue(LValue LV, QualType ExprType) { |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 241 | if (LV.isSimple()) { |
| 242 | llvm::Value *Ptr = LV.getAddress(); |
| 243 | const llvm::Type *EltTy = |
| 244 | cast<llvm::PointerType>(Ptr->getType())->getElementType(); |
| 245 | |
| 246 | // Simple scalar l-value. |
| 247 | if (EltTy->isFirstClassType()) |
| 248 | return RValue::get(Builder.CreateLoad(Ptr, "tmp")); |
| 249 | |
Chris Lattner | bdb8ffb | 2007-08-11 00:04:45 +0000 | [diff] [blame] | 250 | assert(ExprType->isFunctionType() && "Unknown scalar value"); |
| 251 | return RValue::get(Ptr); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 252 | } |
| 253 | |
| 254 | if (LV.isVectorElt()) { |
| 255 | llvm::Value *Vec = Builder.CreateLoad(LV.getVectorAddr(), "tmp"); |
| 256 | return RValue::get(Builder.CreateExtractElement(Vec, LV.getVectorIdx(), |
| 257 | "vecext")); |
| 258 | } |
Chris Lattner | a735fac | 2007-08-03 00:16:29 +0000 | [diff] [blame] | 259 | |
| 260 | // If this is a reference to a subset of the elements of a vector, either |
| 261 | // shuffle the input or extract/insert them as appropriate. |
Chris Lattner | a0d03a7 | 2007-08-03 17:31:20 +0000 | [diff] [blame] | 262 | if (LV.isOCUVectorElt()) |
| 263 | return EmitLoadOfOCUElementLValue(LV, ExprType); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 264 | |
| 265 | assert(0 && "Bitfield ref not impl!"); |
| 266 | } |
| 267 | |
Chris Lattner | 944f796 | 2007-08-03 16:18:34 +0000 | [diff] [blame] | 268 | // If this is a reference to a subset of the elements of a vector, either |
| 269 | // shuffle the input or extract/insert them as appropriate. |
Chris Lattner | a0d03a7 | 2007-08-03 17:31:20 +0000 | [diff] [blame] | 270 | RValue CodeGenFunction::EmitLoadOfOCUElementLValue(LValue LV, |
Chris Lattner | 4b49296 | 2007-08-10 17:10:08 +0000 | [diff] [blame] | 271 | QualType ExprType) { |
Chris Lattner | 944f796 | 2007-08-03 16:18:34 +0000 | [diff] [blame] | 272 | llvm::Value *Vec = Builder.CreateLoad(LV.getOCUVectorAddr(), "tmp"); |
| 273 | |
Chris Lattner | a0d03a7 | 2007-08-03 17:31:20 +0000 | [diff] [blame] | 274 | unsigned EncFields = LV.getOCUVectorElts(); |
Chris Lattner | 944f796 | 2007-08-03 16:18:34 +0000 | [diff] [blame] | 275 | |
| 276 | // If the result of the expression is a non-vector type, we must be |
| 277 | // extracting a single element. Just codegen as an extractelement. |
Chris Lattner | 4b49296 | 2007-08-10 17:10:08 +0000 | [diff] [blame] | 278 | const VectorType *ExprVT = ExprType->getAsVectorType(); |
| 279 | if (!ExprVT) { |
Chris Lattner | a0d03a7 | 2007-08-03 17:31:20 +0000 | [diff] [blame] | 280 | unsigned InIdx = OCUVectorElementExpr::getAccessedFieldNo(0, EncFields); |
Chris Lattner | 944f796 | 2007-08-03 16:18:34 +0000 | [diff] [blame] | 281 | llvm::Value *Elt = llvm::ConstantInt::get(llvm::Type::Int32Ty, InIdx); |
| 282 | return RValue::get(Builder.CreateExtractElement(Vec, Elt, "tmp")); |
| 283 | } |
| 284 | |
| 285 | // If the source and destination have the same number of elements, use a |
| 286 | // vector shuffle instead of insert/extracts. |
Chris Lattner | 4b49296 | 2007-08-10 17:10:08 +0000 | [diff] [blame] | 287 | unsigned NumResultElts = ExprVT->getNumElements(); |
Chris Lattner | 944f796 | 2007-08-03 16:18:34 +0000 | [diff] [blame] | 288 | unsigned NumSourceElts = |
| 289 | cast<llvm::VectorType>(Vec->getType())->getNumElements(); |
| 290 | |
| 291 | if (NumResultElts == NumSourceElts) { |
| 292 | llvm::SmallVector<llvm::Constant*, 4> Mask; |
| 293 | for (unsigned i = 0; i != NumResultElts; ++i) { |
Chris Lattner | a0d03a7 | 2007-08-03 17:31:20 +0000 | [diff] [blame] | 294 | unsigned InIdx = OCUVectorElementExpr::getAccessedFieldNo(i, EncFields); |
Chris Lattner | 944f796 | 2007-08-03 16:18:34 +0000 | [diff] [blame] | 295 | Mask.push_back(llvm::ConstantInt::get(llvm::Type::Int32Ty, InIdx)); |
| 296 | } |
| 297 | |
| 298 | llvm::Value *MaskV = llvm::ConstantVector::get(&Mask[0], Mask.size()); |
| 299 | Vec = Builder.CreateShuffleVector(Vec, |
| 300 | llvm::UndefValue::get(Vec->getType()), |
| 301 | MaskV, "tmp"); |
| 302 | return RValue::get(Vec); |
| 303 | } |
| 304 | |
| 305 | // Start out with an undef of the result type. |
| 306 | llvm::Value *Result = llvm::UndefValue::get(ConvertType(ExprType)); |
| 307 | |
| 308 | // Extract/Insert each element of the result. |
| 309 | for (unsigned i = 0; i != NumResultElts; ++i) { |
Chris Lattner | a0d03a7 | 2007-08-03 17:31:20 +0000 | [diff] [blame] | 310 | unsigned InIdx = OCUVectorElementExpr::getAccessedFieldNo(i, EncFields); |
Chris Lattner | 944f796 | 2007-08-03 16:18:34 +0000 | [diff] [blame] | 311 | llvm::Value *Elt = llvm::ConstantInt::get(llvm::Type::Int32Ty, InIdx); |
| 312 | Elt = Builder.CreateExtractElement(Vec, Elt, "tmp"); |
| 313 | |
| 314 | llvm::Value *OutIdx = llvm::ConstantInt::get(llvm::Type::Int32Ty, i); |
| 315 | Result = Builder.CreateInsertElement(Result, Elt, OutIdx, "tmp"); |
| 316 | } |
| 317 | |
| 318 | return RValue::get(Result); |
| 319 | } |
| 320 | |
| 321 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 322 | RValue CodeGenFunction::EmitLoadOfLValue(const Expr *E) { |
| 323 | return EmitLoadOfLValue(EmitLValue(E), E->getType()); |
| 324 | } |
| 325 | |
| 326 | |
| 327 | /// EmitStoreThroughLValue - Store the specified rvalue into the specified |
| 328 | /// lvalue, where both are guaranteed to the have the same type, and that type |
| 329 | /// is 'Ty'. |
| 330 | void CodeGenFunction::EmitStoreThroughLValue(RValue Src, LValue Dst, |
| 331 | QualType Ty) { |
Chris Lattner | 5bfdd23 | 2007-08-03 16:28:33 +0000 | [diff] [blame] | 332 | if (!Dst.isSimple()) { |
| 333 | if (Dst.isVectorElt()) { |
| 334 | // Read/modify/write the vector, inserting the new element. |
| 335 | // FIXME: Volatility. |
| 336 | llvm::Value *Vec = Builder.CreateLoad(Dst.getVectorAddr(), "tmp"); |
| 337 | Vec = Builder.CreateInsertElement(Vec, Src.getVal(), |
| 338 | Dst.getVectorIdx(), "vecins"); |
| 339 | Builder.CreateStore(Vec, Dst.getVectorAddr()); |
| 340 | return; |
| 341 | } |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 342 | |
Chris Lattner | 5bfdd23 | 2007-08-03 16:28:33 +0000 | [diff] [blame] | 343 | // 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] | 344 | if (Dst.isOCUVectorElt()) |
Chris Lattner | 5bfdd23 | 2007-08-03 16:28:33 +0000 | [diff] [blame] | 345 | return EmitStoreThroughOCUComponentLValue(Src, Dst, Ty); |
| 346 | |
| 347 | assert(0 && "FIXME: Don't support store to bitfield yet"); |
| 348 | } |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 349 | |
| 350 | llvm::Value *DstAddr = Dst.getAddress(); |
Chris Lattner | bdb8ffb | 2007-08-11 00:04:45 +0000 | [diff] [blame] | 351 | assert(Src.isScalar() && "Can't emit an agg store with this method"); |
| 352 | // FIXME: Handle volatility etc. |
| 353 | const llvm::Type *SrcTy = Src.getVal()->getType(); |
| 354 | const llvm::Type *AddrTy = |
| 355 | cast<llvm::PointerType>(DstAddr->getType())->getElementType(); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 356 | |
Chris Lattner | bdb8ffb | 2007-08-11 00:04:45 +0000 | [diff] [blame] | 357 | if (AddrTy != SrcTy) |
| 358 | DstAddr = Builder.CreateBitCast(DstAddr, llvm::PointerType::get(SrcTy), |
| 359 | "storetmp"); |
| 360 | Builder.CreateStore(Src.getVal(), DstAddr); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 361 | } |
| 362 | |
Chris Lattner | 5bfdd23 | 2007-08-03 16:28:33 +0000 | [diff] [blame] | 363 | void CodeGenFunction::EmitStoreThroughOCUComponentLValue(RValue Src, LValue Dst, |
| 364 | QualType Ty) { |
| 365 | // This access turns into a read/modify/write of the vector. Load the input |
| 366 | // value now. |
| 367 | llvm::Value *Vec = Builder.CreateLoad(Dst.getOCUVectorAddr(), "tmp"); |
| 368 | // FIXME: Volatility. |
Chris Lattner | a0d03a7 | 2007-08-03 17:31:20 +0000 | [diff] [blame] | 369 | unsigned EncFields = Dst.getOCUVectorElts(); |
Chris Lattner | 5bfdd23 | 2007-08-03 16:28:33 +0000 | [diff] [blame] | 370 | |
| 371 | llvm::Value *SrcVal = Src.getVal(); |
| 372 | |
Chris Lattner | 940966d | 2007-08-03 16:37:04 +0000 | [diff] [blame] | 373 | if (const VectorType *VTy = Ty->getAsVectorType()) { |
| 374 | unsigned NumSrcElts = VTy->getNumElements(); |
| 375 | |
| 376 | // Extract/Insert each element. |
| 377 | for (unsigned i = 0; i != NumSrcElts; ++i) { |
| 378 | llvm::Value *Elt = llvm::ConstantInt::get(llvm::Type::Int32Ty, i); |
| 379 | Elt = Builder.CreateExtractElement(SrcVal, Elt, "tmp"); |
| 380 | |
Chris Lattner | a0d03a7 | 2007-08-03 17:31:20 +0000 | [diff] [blame] | 381 | unsigned Idx = OCUVectorElementExpr::getAccessedFieldNo(i, EncFields); |
Chris Lattner | 940966d | 2007-08-03 16:37:04 +0000 | [diff] [blame] | 382 | llvm::Value *OutIdx = llvm::ConstantInt::get(llvm::Type::Int32Ty, Idx); |
| 383 | Vec = Builder.CreateInsertElement(Vec, Elt, OutIdx, "tmp"); |
| 384 | } |
| 385 | } else { |
| 386 | // 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] | 387 | unsigned InIdx = OCUVectorElementExpr::getAccessedFieldNo(0, EncFields); |
Chris Lattner | 5bfdd23 | 2007-08-03 16:28:33 +0000 | [diff] [blame] | 388 | llvm::Value *Elt = llvm::ConstantInt::get(llvm::Type::Int32Ty, InIdx); |
| 389 | Vec = Builder.CreateInsertElement(Vec, SrcVal, Elt, "tmp"); |
Chris Lattner | 5bfdd23 | 2007-08-03 16:28:33 +0000 | [diff] [blame] | 390 | } |
| 391 | |
Chris Lattner | 5bfdd23 | 2007-08-03 16:28:33 +0000 | [diff] [blame] | 392 | Builder.CreateStore(Vec, Dst.getOCUVectorAddr()); |
| 393 | } |
| 394 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 395 | |
| 396 | LValue CodeGenFunction::EmitDeclRefLValue(const DeclRefExpr *E) { |
| 397 | const Decl *D = E->getDecl(); |
| 398 | if (isa<BlockVarDecl>(D) || isa<ParmVarDecl>(D)) { |
| 399 | llvm::Value *V = LocalDeclMap[D]; |
| 400 | assert(V && "BlockVarDecl not entered in LocalDeclMap?"); |
| 401 | return LValue::MakeAddr(V); |
| 402 | } else if (isa<FunctionDecl>(D) || isa<FileVarDecl>(D)) { |
| 403 | return LValue::MakeAddr(CGM.GetAddrOfGlobalDecl(D)); |
| 404 | } |
| 405 | assert(0 && "Unimp declref"); |
| 406 | } |
| 407 | |
| 408 | LValue CodeGenFunction::EmitUnaryOpLValue(const UnaryOperator *E) { |
| 409 | // __extension__ doesn't affect lvalue-ness. |
| 410 | if (E->getOpcode() == UnaryOperator::Extension) |
| 411 | return EmitLValue(E->getSubExpr()); |
| 412 | |
| 413 | assert(E->getOpcode() == UnaryOperator::Deref && |
| 414 | "'*' is the only unary operator that produces an lvalue"); |
Chris Lattner | 9fba49a | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 415 | return LValue::MakeAddr(EmitScalarExpr(E->getSubExpr())); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 416 | } |
| 417 | |
| 418 | LValue CodeGenFunction::EmitStringLiteralLValue(const StringLiteral *E) { |
| 419 | assert(!E->isWide() && "FIXME: Wide strings not supported yet!"); |
| 420 | const char *StrData = E->getStrData(); |
| 421 | unsigned Len = E->getByteLength(); |
| 422 | |
| 423 | // FIXME: Can cache/reuse these within the module. |
| 424 | llvm::Constant *C=llvm::ConstantArray::get(std::string(StrData, StrData+Len)); |
| 425 | |
| 426 | // Create a global variable for this. |
| 427 | C = new llvm::GlobalVariable(C->getType(), true, |
| 428 | llvm::GlobalValue::InternalLinkage, |
| 429 | C, ".str", CurFn->getParent()); |
| 430 | llvm::Constant *Zero = llvm::Constant::getNullValue(llvm::Type::Int32Ty); |
| 431 | llvm::Constant *Zeros[] = { Zero, Zero }; |
| 432 | C = llvm::ConstantExpr::getGetElementPtr(C, Zeros, 2); |
| 433 | return LValue::MakeAddr(C); |
| 434 | } |
| 435 | |
| 436 | LValue CodeGenFunction::EmitPreDefinedLValue(const PreDefinedExpr *E) { |
| 437 | std::string FunctionName(CurFuncDecl->getName()); |
| 438 | std::string GlobalVarName; |
| 439 | |
| 440 | switch (E->getIdentType()) { |
| 441 | default: |
| 442 | assert(0 && "unknown pre-defined ident type"); |
| 443 | case PreDefinedExpr::Func: |
| 444 | GlobalVarName = "__func__."; |
| 445 | break; |
| 446 | case PreDefinedExpr::Function: |
| 447 | GlobalVarName = "__FUNCTION__."; |
| 448 | break; |
| 449 | case PreDefinedExpr::PrettyFunction: |
| 450 | // FIXME:: Demangle C++ method names |
| 451 | GlobalVarName = "__PRETTY_FUNCTION__."; |
| 452 | break; |
| 453 | } |
| 454 | |
| 455 | GlobalVarName += CurFuncDecl->getName(); |
| 456 | |
| 457 | // FIXME: Can cache/reuse these within the module. |
| 458 | llvm::Constant *C=llvm::ConstantArray::get(FunctionName); |
| 459 | |
| 460 | // Create a global variable for this. |
| 461 | C = new llvm::GlobalVariable(C->getType(), true, |
| 462 | llvm::GlobalValue::InternalLinkage, |
| 463 | C, GlobalVarName, CurFn->getParent()); |
| 464 | llvm::Constant *Zero = llvm::Constant::getNullValue(llvm::Type::Int32Ty); |
| 465 | llvm::Constant *Zeros[] = { Zero, Zero }; |
| 466 | C = llvm::ConstantExpr::getGetElementPtr(C, Zeros, 2); |
| 467 | return LValue::MakeAddr(C); |
| 468 | } |
| 469 | |
| 470 | LValue CodeGenFunction::EmitArraySubscriptExpr(const ArraySubscriptExpr *E) { |
Ted Kremenek | 1c1700f | 2007-08-20 16:18:38 +0000 | [diff] [blame] | 471 | // 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] | 472 | llvm::Value *Idx = EmitScalarExpr(E->getIdx()); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 473 | |
| 474 | // If the base is a vector type, then we are forming a vector element lvalue |
| 475 | // with this subscript. |
Ted Kremenek | 1c1700f | 2007-08-20 16:18:38 +0000 | [diff] [blame] | 476 | if (E->getLHS()->getType()->isVectorType()) { |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 477 | // Emit the vector as an lvalue to get its address. |
Ted Kremenek | 1c1700f | 2007-08-20 16:18:38 +0000 | [diff] [blame] | 478 | LValue LHS = EmitLValue(E->getLHS()); |
| 479 | assert(LHS.isSimple() && "Can only subscript lvalue vectors here!"); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 480 | // FIXME: This should properly sign/zero/extend or truncate Idx to i32. |
Ted Kremenek | 1c1700f | 2007-08-20 16:18:38 +0000 | [diff] [blame] | 481 | return LValue::MakeVectorElt(LHS.getAddress(), Idx); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 482 | } |
| 483 | |
Ted Kremenek | 1c1700f | 2007-08-20 16:18:38 +0000 | [diff] [blame] | 484 | // 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] | 485 | llvm::Value *Base = EmitScalarExpr(E->getBase()); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 486 | |
Ted Kremenek | 1c1700f | 2007-08-20 16:18:38 +0000 | [diff] [blame] | 487 | // Extend or truncate the index type to 32 or 64-bits. |
Chris Lattner | 2af72ac | 2007-08-08 17:43:05 +0000 | [diff] [blame] | 488 | QualType IdxTy = E->getIdx()->getType(); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 489 | bool IdxSigned = IdxTy->isSignedIntegerType(); |
| 490 | unsigned IdxBitwidth = cast<llvm::IntegerType>(Idx->getType())->getBitWidth(); |
| 491 | if (IdxBitwidth != LLVMPointerWidth) |
| 492 | Idx = Builder.CreateIntCast(Idx, llvm::IntegerType::get(LLVMPointerWidth), |
| 493 | IdxSigned, "idxprom"); |
| 494 | |
| 495 | // We know that the pointer points to a type of the correct size, unless the |
| 496 | // size is a VLA. |
| 497 | if (!E->getType()->isConstantSizeType(getContext())) |
| 498 | assert(0 && "VLA idx not implemented"); |
| 499 | return LValue::MakeAddr(Builder.CreateGEP(Base, Idx, "arrayidx")); |
| 500 | } |
| 501 | |
Chris Lattner | 6552019 | 2007-08-02 23:37:31 +0000 | [diff] [blame] | 502 | LValue CodeGenFunction:: |
Chris Lattner | a0d03a7 | 2007-08-03 17:31:20 +0000 | [diff] [blame] | 503 | EmitOCUVectorElementExpr(const OCUVectorElementExpr *E) { |
Chris Lattner | 6552019 | 2007-08-02 23:37:31 +0000 | [diff] [blame] | 504 | // Emit the base vector as an l-value. |
| 505 | LValue Base = EmitLValue(E->getBase()); |
| 506 | assert(Base.isSimple() && "Can only subscript lvalue vectors here!"); |
| 507 | |
Chris Lattner | a0d03a7 | 2007-08-03 17:31:20 +0000 | [diff] [blame] | 508 | return LValue::MakeOCUVectorElt(Base.getAddress(), |
| 509 | E->getEncodedElementAccess()); |
Chris Lattner | 6552019 | 2007-08-02 23:37:31 +0000 | [diff] [blame] | 510 | } |
| 511 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 512 | //===--------------------------------------------------------------------===// |
| 513 | // Expression Emission |
| 514 | //===--------------------------------------------------------------------===// |
| 515 | |
Chris Lattner | 348c8a2 | 2007-08-23 23:43:33 +0000 | [diff] [blame] | 516 | /// EmitAnyExpr - Emit an expression of any type: scalar, complex, aggregate, |
| 517 | /// returning an rvalue corresponding to it. If NeedResult is false, the |
| 518 | /// result of the expression doesn't need to be generated into memory. |
| 519 | RValue CodeGenFunction::EmitAnyExpr(const Expr *E, bool NeedResult) { |
| 520 | if (!hasAggregateLLVMType(E->getType())) |
Chris Lattner | 9fba49a | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 521 | return RValue::get(EmitScalarExpr(E)); |
Chris Lattner | 348c8a2 | 2007-08-23 23:43:33 +0000 | [diff] [blame] | 522 | |
| 523 | llvm::Value *DestMem = 0; |
| 524 | if (NeedResult) |
| 525 | DestMem = CreateTempAlloca(ConvertType(E->getType())); |
| 526 | |
| 527 | if (!E->getType()->isComplexType()) { |
| 528 | EmitAggExpr(E, DestMem, false); |
| 529 | } else if (NeedResult) |
| 530 | EmitComplexExprIntoAddr(E, DestMem); |
| 531 | else |
| 532 | EmitComplexExpr(E); |
| 533 | |
| 534 | return RValue::getAggregate(DestMem); |
| 535 | } |
| 536 | |
Chris Lattner | b2cb9cb | 2007-08-20 22:37:10 +0000 | [diff] [blame] | 537 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 538 | RValue CodeGenFunction::EmitCallExpr(const CallExpr *E) { |
Anders Carlsson | 4986530 | 2007-08-20 18:05:56 +0000 | [diff] [blame] | 539 | if (const ImplicitCastExpr *IcExpr = |
| 540 | dyn_cast<const ImplicitCastExpr>(E->getCallee())) |
| 541 | if (const DeclRefExpr *DRExpr = |
| 542 | dyn_cast<const DeclRefExpr>(IcExpr->getSubExpr())) |
| 543 | if (const FunctionDecl *FDecl = |
| 544 | dyn_cast<const FunctionDecl>(DRExpr->getDecl())) |
| 545 | if (unsigned builtinID = FDecl->getIdentifier()->getBuiltinID()) |
| 546 | return EmitBuiltinExpr(builtinID, E); |
| 547 | |
Chris Lattner | 9fba49a | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 548 | llvm::Value *Callee = EmitScalarExpr(E->getCallee()); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 549 | |
| 550 | // The callee type will always be a pointer to function type, get the function |
| 551 | // type. |
Chris Lattner | 2af72ac | 2007-08-08 17:43:05 +0000 | [diff] [blame] | 552 | QualType CalleeTy = E->getCallee()->getType(); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 553 | CalleeTy = cast<PointerType>(CalleeTy.getCanonicalType())->getPointeeType(); |
| 554 | |
| 555 | // Get information about the argument types. |
| 556 | FunctionTypeProto::arg_type_iterator ArgTyIt = 0, ArgTyEnd = 0; |
| 557 | |
| 558 | // Calling unprototyped functions provides no argument info. |
| 559 | if (const FunctionTypeProto *FTP = dyn_cast<FunctionTypeProto>(CalleeTy)) { |
| 560 | ArgTyIt = FTP->arg_type_begin(); |
| 561 | ArgTyEnd = FTP->arg_type_end(); |
| 562 | } |
| 563 | |
| 564 | llvm::SmallVector<llvm::Value*, 16> Args; |
| 565 | |
Chris Lattner | 5980204 | 2007-08-10 17:02:28 +0000 | [diff] [blame] | 566 | // Handle struct-return functions by passing a pointer to the location that |
| 567 | // we would like to return into. |
| 568 | if (hasAggregateLLVMType(E->getType())) { |
| 569 | // Create a temporary alloca to hold the result of the call. :( |
| 570 | Args.push_back(CreateTempAlloca(ConvertType(E->getType()))); |
| 571 | // FIXME: set the stret attribute on the argument. |
| 572 | } |
| 573 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 574 | for (unsigned i = 0, e = E->getNumArgs(); i != e; ++i) { |
Chris Lattner | 2af72ac | 2007-08-08 17:43:05 +0000 | [diff] [blame] | 575 | QualType ArgTy = E->getArg(i)->getType(); |
Chris Lattner | 348c8a2 | 2007-08-23 23:43:33 +0000 | [diff] [blame] | 576 | RValue ArgVal = EmitAnyExpr(E->getArg(i)); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 577 | |
| 578 | // If this argument has prototype information, convert it. |
| 579 | if (ArgTyIt != ArgTyEnd) { |
| 580 | ArgVal = EmitConversion(ArgVal, ArgTy, *ArgTyIt++); |
| 581 | } else { |
| 582 | // Otherwise, if passing through "..." or to a function with no prototype, |
| 583 | // perform the "default argument promotions" (C99 6.5.2.2p6), which |
| 584 | // includes the usual unary conversions, but also promotes float to |
| 585 | // double. |
| 586 | if (const BuiltinType *BT = |
| 587 | dyn_cast<BuiltinType>(ArgTy.getCanonicalType())) { |
| 588 | if (BT->getKind() == BuiltinType::Float) |
| 589 | ArgVal = RValue::get(Builder.CreateFPExt(ArgVal.getVal(), |
| 590 | llvm::Type::DoubleTy,"tmp")); |
| 591 | } |
| 592 | } |
| 593 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 594 | if (ArgVal.isScalar()) |
| 595 | Args.push_back(ArgVal.getVal()); |
| 596 | else // Pass by-address. FIXME: Set attribute bit on call. |
| 597 | Args.push_back(ArgVal.getAggregateAddr()); |
| 598 | } |
| 599 | |
Chris Lattner | a957225 | 2007-08-01 06:24:52 +0000 | [diff] [blame] | 600 | llvm::Value *V = Builder.CreateCall(Callee, &Args[0], &Args[0]+Args.size()); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 601 | if (V->getType() != llvm::Type::VoidTy) |
| 602 | V->setName("call"); |
Chris Lattner | 5980204 | 2007-08-10 17:02:28 +0000 | [diff] [blame] | 603 | else if (hasAggregateLLVMType(E->getType())) |
| 604 | // Struct return. |
| 605 | return RValue::getAggregate(Args[0]); |
| 606 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 607 | return RValue::get(V); |
| 608 | } |