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()) { |
| 214 | default: |
| 215 | fprintf(stderr, "Unimplemented lvalue expr!\n"); |
| 216 | E->dump(); |
| 217 | return LValue::MakeAddr(llvm::UndefValue::get( |
| 218 | llvm::PointerType::get(llvm::Type::Int32Ty))); |
| 219 | |
| 220 | case Expr::DeclRefExprClass: return EmitDeclRefLValue(cast<DeclRefExpr>(E)); |
| 221 | case Expr::ParenExprClass:return EmitLValue(cast<ParenExpr>(E)->getSubExpr()); |
| 222 | case Expr::PreDefinedExprClass: |
| 223 | return EmitPreDefinedLValue(cast<PreDefinedExpr>(E)); |
| 224 | case Expr::StringLiteralClass: |
| 225 | return EmitStringLiteralLValue(cast<StringLiteral>(E)); |
| 226 | |
| 227 | case Expr::UnaryOperatorClass: |
| 228 | return EmitUnaryOpLValue(cast<UnaryOperator>(E)); |
| 229 | case Expr::ArraySubscriptExprClass: |
| 230 | return EmitArraySubscriptExpr(cast<ArraySubscriptExpr>(E)); |
Chris Lattner | a0d03a7 | 2007-08-03 17:31:20 +0000 | [diff] [blame] | 231 | case Expr::OCUVectorElementExprClass: |
| 232 | return EmitOCUVectorElementExpr(cast<OCUVectorElementExpr>(E)); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 233 | } |
| 234 | } |
| 235 | |
| 236 | /// EmitLoadOfLValue - Given an expression that represents a value lvalue, |
| 237 | /// this method emits the address of the lvalue, then loads the result as an |
| 238 | /// rvalue, returning the rvalue. |
| 239 | RValue CodeGenFunction::EmitLoadOfLValue(LValue LV, QualType ExprType) { |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 240 | if (LV.isSimple()) { |
| 241 | llvm::Value *Ptr = LV.getAddress(); |
| 242 | const llvm::Type *EltTy = |
| 243 | cast<llvm::PointerType>(Ptr->getType())->getElementType(); |
| 244 | |
| 245 | // Simple scalar l-value. |
| 246 | if (EltTy->isFirstClassType()) |
| 247 | return RValue::get(Builder.CreateLoad(Ptr, "tmp")); |
| 248 | |
Chris Lattner | bdb8ffb | 2007-08-11 00:04:45 +0000 | [diff] [blame] | 249 | assert(ExprType->isFunctionType() && "Unknown scalar value"); |
| 250 | return RValue::get(Ptr); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 251 | } |
| 252 | |
| 253 | if (LV.isVectorElt()) { |
| 254 | llvm::Value *Vec = Builder.CreateLoad(LV.getVectorAddr(), "tmp"); |
| 255 | return RValue::get(Builder.CreateExtractElement(Vec, LV.getVectorIdx(), |
| 256 | "vecext")); |
| 257 | } |
Chris Lattner | a735fac | 2007-08-03 00:16:29 +0000 | [diff] [blame] | 258 | |
| 259 | // If this is a reference to a subset of the elements of a vector, either |
| 260 | // shuffle the input or extract/insert them as appropriate. |
Chris Lattner | a0d03a7 | 2007-08-03 17:31:20 +0000 | [diff] [blame] | 261 | if (LV.isOCUVectorElt()) |
| 262 | return EmitLoadOfOCUElementLValue(LV, ExprType); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 263 | |
| 264 | assert(0 && "Bitfield ref not impl!"); |
| 265 | } |
| 266 | |
Chris Lattner | 944f796 | 2007-08-03 16:18:34 +0000 | [diff] [blame] | 267 | // If this is a reference to a subset of the elements of a vector, either |
| 268 | // shuffle the input or extract/insert them as appropriate. |
Chris Lattner | a0d03a7 | 2007-08-03 17:31:20 +0000 | [diff] [blame] | 269 | RValue CodeGenFunction::EmitLoadOfOCUElementLValue(LValue LV, |
Chris Lattner | 4b49296 | 2007-08-10 17:10:08 +0000 | [diff] [blame] | 270 | QualType ExprType) { |
Chris Lattner | 944f796 | 2007-08-03 16:18:34 +0000 | [diff] [blame] | 271 | llvm::Value *Vec = Builder.CreateLoad(LV.getOCUVectorAddr(), "tmp"); |
| 272 | |
Chris Lattner | a0d03a7 | 2007-08-03 17:31:20 +0000 | [diff] [blame] | 273 | unsigned EncFields = LV.getOCUVectorElts(); |
Chris Lattner | 944f796 | 2007-08-03 16:18:34 +0000 | [diff] [blame] | 274 | |
| 275 | // If the result of the expression is a non-vector type, we must be |
| 276 | // extracting a single element. Just codegen as an extractelement. |
Chris Lattner | 4b49296 | 2007-08-10 17:10:08 +0000 | [diff] [blame] | 277 | const VectorType *ExprVT = ExprType->getAsVectorType(); |
| 278 | if (!ExprVT) { |
Chris Lattner | a0d03a7 | 2007-08-03 17:31:20 +0000 | [diff] [blame] | 279 | unsigned InIdx = OCUVectorElementExpr::getAccessedFieldNo(0, EncFields); |
Chris Lattner | 944f796 | 2007-08-03 16:18:34 +0000 | [diff] [blame] | 280 | llvm::Value *Elt = llvm::ConstantInt::get(llvm::Type::Int32Ty, InIdx); |
| 281 | return RValue::get(Builder.CreateExtractElement(Vec, Elt, "tmp")); |
| 282 | } |
| 283 | |
| 284 | // If the source and destination have the same number of elements, use a |
| 285 | // vector shuffle instead of insert/extracts. |
Chris Lattner | 4b49296 | 2007-08-10 17:10:08 +0000 | [diff] [blame] | 286 | unsigned NumResultElts = ExprVT->getNumElements(); |
Chris Lattner | 944f796 | 2007-08-03 16:18:34 +0000 | [diff] [blame] | 287 | unsigned NumSourceElts = |
| 288 | cast<llvm::VectorType>(Vec->getType())->getNumElements(); |
| 289 | |
| 290 | if (NumResultElts == NumSourceElts) { |
| 291 | llvm::SmallVector<llvm::Constant*, 4> Mask; |
| 292 | for (unsigned i = 0; i != NumResultElts; ++i) { |
Chris Lattner | a0d03a7 | 2007-08-03 17:31:20 +0000 | [diff] [blame] | 293 | unsigned InIdx = OCUVectorElementExpr::getAccessedFieldNo(i, EncFields); |
Chris Lattner | 944f796 | 2007-08-03 16:18:34 +0000 | [diff] [blame] | 294 | Mask.push_back(llvm::ConstantInt::get(llvm::Type::Int32Ty, InIdx)); |
| 295 | } |
| 296 | |
| 297 | llvm::Value *MaskV = llvm::ConstantVector::get(&Mask[0], Mask.size()); |
| 298 | Vec = Builder.CreateShuffleVector(Vec, |
| 299 | llvm::UndefValue::get(Vec->getType()), |
| 300 | MaskV, "tmp"); |
| 301 | return RValue::get(Vec); |
| 302 | } |
| 303 | |
| 304 | // Start out with an undef of the result type. |
| 305 | llvm::Value *Result = llvm::UndefValue::get(ConvertType(ExprType)); |
| 306 | |
| 307 | // Extract/Insert each element of the result. |
| 308 | for (unsigned i = 0; i != NumResultElts; ++i) { |
Chris Lattner | a0d03a7 | 2007-08-03 17:31:20 +0000 | [diff] [blame] | 309 | unsigned InIdx = OCUVectorElementExpr::getAccessedFieldNo(i, EncFields); |
Chris Lattner | 944f796 | 2007-08-03 16:18:34 +0000 | [diff] [blame] | 310 | llvm::Value *Elt = llvm::ConstantInt::get(llvm::Type::Int32Ty, InIdx); |
| 311 | Elt = Builder.CreateExtractElement(Vec, Elt, "tmp"); |
| 312 | |
| 313 | llvm::Value *OutIdx = llvm::ConstantInt::get(llvm::Type::Int32Ty, i); |
| 314 | Result = Builder.CreateInsertElement(Result, Elt, OutIdx, "tmp"); |
| 315 | } |
| 316 | |
| 317 | return RValue::get(Result); |
| 318 | } |
| 319 | |
| 320 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 321 | RValue CodeGenFunction::EmitLoadOfLValue(const Expr *E) { |
| 322 | return EmitLoadOfLValue(EmitLValue(E), E->getType()); |
| 323 | } |
| 324 | |
| 325 | |
| 326 | /// EmitStoreThroughLValue - Store the specified rvalue into the specified |
| 327 | /// lvalue, where both are guaranteed to the have the same type, and that type |
| 328 | /// is 'Ty'. |
| 329 | void CodeGenFunction::EmitStoreThroughLValue(RValue Src, LValue Dst, |
| 330 | QualType Ty) { |
Chris Lattner | 5bfdd23 | 2007-08-03 16:28:33 +0000 | [diff] [blame] | 331 | if (!Dst.isSimple()) { |
| 332 | if (Dst.isVectorElt()) { |
| 333 | // Read/modify/write the vector, inserting the new element. |
| 334 | // FIXME: Volatility. |
| 335 | llvm::Value *Vec = Builder.CreateLoad(Dst.getVectorAddr(), "tmp"); |
| 336 | Vec = Builder.CreateInsertElement(Vec, Src.getVal(), |
| 337 | Dst.getVectorIdx(), "vecins"); |
| 338 | Builder.CreateStore(Vec, Dst.getVectorAddr()); |
| 339 | return; |
| 340 | } |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 341 | |
Chris Lattner | 5bfdd23 | 2007-08-03 16:28:33 +0000 | [diff] [blame] | 342 | // 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] | 343 | if (Dst.isOCUVectorElt()) |
Chris Lattner | 5bfdd23 | 2007-08-03 16:28:33 +0000 | [diff] [blame] | 344 | return EmitStoreThroughOCUComponentLValue(Src, Dst, Ty); |
| 345 | |
| 346 | assert(0 && "FIXME: Don't support store to bitfield yet"); |
| 347 | } |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 348 | |
| 349 | llvm::Value *DstAddr = Dst.getAddress(); |
Chris Lattner | bdb8ffb | 2007-08-11 00:04:45 +0000 | [diff] [blame] | 350 | assert(Src.isScalar() && "Can't emit an agg store with this method"); |
| 351 | // FIXME: Handle volatility etc. |
| 352 | const llvm::Type *SrcTy = Src.getVal()->getType(); |
| 353 | const llvm::Type *AddrTy = |
| 354 | cast<llvm::PointerType>(DstAddr->getType())->getElementType(); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 355 | |
Chris Lattner | bdb8ffb | 2007-08-11 00:04:45 +0000 | [diff] [blame] | 356 | if (AddrTy != SrcTy) |
| 357 | DstAddr = Builder.CreateBitCast(DstAddr, llvm::PointerType::get(SrcTy), |
| 358 | "storetmp"); |
| 359 | Builder.CreateStore(Src.getVal(), DstAddr); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 360 | } |
| 361 | |
Chris Lattner | 5bfdd23 | 2007-08-03 16:28:33 +0000 | [diff] [blame] | 362 | void CodeGenFunction::EmitStoreThroughOCUComponentLValue(RValue Src, LValue Dst, |
| 363 | QualType Ty) { |
| 364 | // This access turns into a read/modify/write of the vector. Load the input |
| 365 | // value now. |
| 366 | llvm::Value *Vec = Builder.CreateLoad(Dst.getOCUVectorAddr(), "tmp"); |
| 367 | // FIXME: Volatility. |
Chris Lattner | a0d03a7 | 2007-08-03 17:31:20 +0000 | [diff] [blame] | 368 | unsigned EncFields = Dst.getOCUVectorElts(); |
Chris Lattner | 5bfdd23 | 2007-08-03 16:28:33 +0000 | [diff] [blame] | 369 | |
| 370 | llvm::Value *SrcVal = Src.getVal(); |
| 371 | |
Chris Lattner | 940966d | 2007-08-03 16:37:04 +0000 | [diff] [blame] | 372 | if (const VectorType *VTy = Ty->getAsVectorType()) { |
| 373 | unsigned NumSrcElts = VTy->getNumElements(); |
| 374 | |
| 375 | // Extract/Insert each element. |
| 376 | for (unsigned i = 0; i != NumSrcElts; ++i) { |
| 377 | llvm::Value *Elt = llvm::ConstantInt::get(llvm::Type::Int32Ty, i); |
| 378 | Elt = Builder.CreateExtractElement(SrcVal, Elt, "tmp"); |
| 379 | |
Chris Lattner | a0d03a7 | 2007-08-03 17:31:20 +0000 | [diff] [blame] | 380 | unsigned Idx = OCUVectorElementExpr::getAccessedFieldNo(i, EncFields); |
Chris Lattner | 940966d | 2007-08-03 16:37:04 +0000 | [diff] [blame] | 381 | llvm::Value *OutIdx = llvm::ConstantInt::get(llvm::Type::Int32Ty, Idx); |
| 382 | Vec = Builder.CreateInsertElement(Vec, Elt, OutIdx, "tmp"); |
| 383 | } |
| 384 | } else { |
| 385 | // 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] | 386 | unsigned InIdx = OCUVectorElementExpr::getAccessedFieldNo(0, EncFields); |
Chris Lattner | 5bfdd23 | 2007-08-03 16:28:33 +0000 | [diff] [blame] | 387 | llvm::Value *Elt = llvm::ConstantInt::get(llvm::Type::Int32Ty, InIdx); |
| 388 | Vec = Builder.CreateInsertElement(Vec, SrcVal, Elt, "tmp"); |
Chris Lattner | 5bfdd23 | 2007-08-03 16:28:33 +0000 | [diff] [blame] | 389 | } |
| 390 | |
Chris Lattner | 5bfdd23 | 2007-08-03 16:28:33 +0000 | [diff] [blame] | 391 | Builder.CreateStore(Vec, Dst.getOCUVectorAddr()); |
| 392 | } |
| 393 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 394 | |
| 395 | LValue CodeGenFunction::EmitDeclRefLValue(const DeclRefExpr *E) { |
| 396 | const Decl *D = E->getDecl(); |
| 397 | if (isa<BlockVarDecl>(D) || isa<ParmVarDecl>(D)) { |
| 398 | llvm::Value *V = LocalDeclMap[D]; |
| 399 | assert(V && "BlockVarDecl not entered in LocalDeclMap?"); |
| 400 | return LValue::MakeAddr(V); |
| 401 | } else if (isa<FunctionDecl>(D) || isa<FileVarDecl>(D)) { |
| 402 | return LValue::MakeAddr(CGM.GetAddrOfGlobalDecl(D)); |
| 403 | } |
| 404 | assert(0 && "Unimp declref"); |
| 405 | } |
| 406 | |
| 407 | LValue CodeGenFunction::EmitUnaryOpLValue(const UnaryOperator *E) { |
| 408 | // __extension__ doesn't affect lvalue-ness. |
| 409 | if (E->getOpcode() == UnaryOperator::Extension) |
| 410 | return EmitLValue(E->getSubExpr()); |
| 411 | |
| 412 | assert(E->getOpcode() == UnaryOperator::Deref && |
| 413 | "'*' is the only unary operator that produces an lvalue"); |
| 414 | return LValue::MakeAddr(EmitExpr(E->getSubExpr()).getVal()); |
| 415 | } |
| 416 | |
| 417 | LValue CodeGenFunction::EmitStringLiteralLValue(const StringLiteral *E) { |
| 418 | assert(!E->isWide() && "FIXME: Wide strings not supported yet!"); |
| 419 | const char *StrData = E->getStrData(); |
| 420 | unsigned Len = E->getByteLength(); |
| 421 | |
| 422 | // FIXME: Can cache/reuse these within the module. |
| 423 | llvm::Constant *C=llvm::ConstantArray::get(std::string(StrData, StrData+Len)); |
| 424 | |
| 425 | // Create a global variable for this. |
| 426 | C = new llvm::GlobalVariable(C->getType(), true, |
| 427 | llvm::GlobalValue::InternalLinkage, |
| 428 | C, ".str", CurFn->getParent()); |
| 429 | llvm::Constant *Zero = llvm::Constant::getNullValue(llvm::Type::Int32Ty); |
| 430 | llvm::Constant *Zeros[] = { Zero, Zero }; |
| 431 | C = llvm::ConstantExpr::getGetElementPtr(C, Zeros, 2); |
| 432 | return LValue::MakeAddr(C); |
| 433 | } |
| 434 | |
| 435 | LValue CodeGenFunction::EmitPreDefinedLValue(const PreDefinedExpr *E) { |
| 436 | std::string FunctionName(CurFuncDecl->getName()); |
| 437 | std::string GlobalVarName; |
| 438 | |
| 439 | switch (E->getIdentType()) { |
| 440 | default: |
| 441 | assert(0 && "unknown pre-defined ident type"); |
| 442 | case PreDefinedExpr::Func: |
| 443 | GlobalVarName = "__func__."; |
| 444 | break; |
| 445 | case PreDefinedExpr::Function: |
| 446 | GlobalVarName = "__FUNCTION__."; |
| 447 | break; |
| 448 | case PreDefinedExpr::PrettyFunction: |
| 449 | // FIXME:: Demangle C++ method names |
| 450 | GlobalVarName = "__PRETTY_FUNCTION__."; |
| 451 | break; |
| 452 | } |
| 453 | |
| 454 | GlobalVarName += CurFuncDecl->getName(); |
| 455 | |
| 456 | // FIXME: Can cache/reuse these within the module. |
| 457 | llvm::Constant *C=llvm::ConstantArray::get(FunctionName); |
| 458 | |
| 459 | // Create a global variable for this. |
| 460 | C = new llvm::GlobalVariable(C->getType(), true, |
| 461 | llvm::GlobalValue::InternalLinkage, |
| 462 | C, GlobalVarName, CurFn->getParent()); |
| 463 | llvm::Constant *Zero = llvm::Constant::getNullValue(llvm::Type::Int32Ty); |
| 464 | llvm::Constant *Zeros[] = { Zero, Zero }; |
| 465 | C = llvm::ConstantExpr::getGetElementPtr(C, Zeros, 2); |
| 466 | return LValue::MakeAddr(C); |
| 467 | } |
| 468 | |
| 469 | LValue CodeGenFunction::EmitArraySubscriptExpr(const ArraySubscriptExpr *E) { |
Ted Kremenek | 1c1700f | 2007-08-20 16:18:38 +0000 | [diff] [blame] | 470 | // The index must always be an integer, which is not an aggregate. Emit it. |
Chris Lattner | 2af72ac | 2007-08-08 17:43:05 +0000 | [diff] [blame] | 471 | llvm::Value *Idx = EmitExpr(E->getIdx()).getVal(); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 472 | |
| 473 | // If the base is a vector type, then we are forming a vector element lvalue |
| 474 | // with this subscript. |
Ted Kremenek | 1c1700f | 2007-08-20 16:18:38 +0000 | [diff] [blame] | 475 | if (E->getLHS()->getType()->isVectorType()) { |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 476 | // Emit the vector as an lvalue to get its address. |
Ted Kremenek | 1c1700f | 2007-08-20 16:18:38 +0000 | [diff] [blame] | 477 | LValue LHS = EmitLValue(E->getLHS()); |
| 478 | assert(LHS.isSimple() && "Can only subscript lvalue vectors here!"); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 479 | // FIXME: This should properly sign/zero/extend or truncate Idx to i32. |
Ted Kremenek | 1c1700f | 2007-08-20 16:18:38 +0000 | [diff] [blame] | 480 | return LValue::MakeVectorElt(LHS.getAddress(), Idx); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 481 | } |
| 482 | |
Ted Kremenek | 1c1700f | 2007-08-20 16:18:38 +0000 | [diff] [blame] | 483 | // The base must be a pointer, which is not an aggregate. Emit it. |
Chris Lattner | 2af72ac | 2007-08-08 17:43:05 +0000 | [diff] [blame] | 484 | llvm::Value *Base = EmitExpr(E->getBase()).getVal(); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 485 | |
Ted Kremenek | 1c1700f | 2007-08-20 16:18:38 +0000 | [diff] [blame] | 486 | // Extend or truncate the index type to 32 or 64-bits. |
Chris Lattner | 2af72ac | 2007-08-08 17:43:05 +0000 | [diff] [blame] | 487 | QualType IdxTy = E->getIdx()->getType(); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 488 | bool IdxSigned = IdxTy->isSignedIntegerType(); |
| 489 | unsigned IdxBitwidth = cast<llvm::IntegerType>(Idx->getType())->getBitWidth(); |
| 490 | if (IdxBitwidth != LLVMPointerWidth) |
| 491 | Idx = Builder.CreateIntCast(Idx, llvm::IntegerType::get(LLVMPointerWidth), |
| 492 | IdxSigned, "idxprom"); |
| 493 | |
| 494 | // We know that the pointer points to a type of the correct size, unless the |
| 495 | // size is a VLA. |
| 496 | if (!E->getType()->isConstantSizeType(getContext())) |
| 497 | assert(0 && "VLA idx not implemented"); |
| 498 | return LValue::MakeAddr(Builder.CreateGEP(Base, Idx, "arrayidx")); |
| 499 | } |
| 500 | |
Chris Lattner | 6552019 | 2007-08-02 23:37:31 +0000 | [diff] [blame] | 501 | LValue CodeGenFunction:: |
Chris Lattner | a0d03a7 | 2007-08-03 17:31:20 +0000 | [diff] [blame] | 502 | EmitOCUVectorElementExpr(const OCUVectorElementExpr *E) { |
Chris Lattner | 6552019 | 2007-08-02 23:37:31 +0000 | [diff] [blame] | 503 | // Emit the base vector as an l-value. |
| 504 | LValue Base = EmitLValue(E->getBase()); |
| 505 | assert(Base.isSimple() && "Can only subscript lvalue vectors here!"); |
| 506 | |
Chris Lattner | a0d03a7 | 2007-08-03 17:31:20 +0000 | [diff] [blame] | 507 | return LValue::MakeOCUVectorElt(Base.getAddress(), |
| 508 | E->getEncodedElementAccess()); |
Chris Lattner | 6552019 | 2007-08-02 23:37:31 +0000 | [diff] [blame] | 509 | } |
| 510 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 511 | //===--------------------------------------------------------------------===// |
| 512 | // Expression Emission |
| 513 | //===--------------------------------------------------------------------===// |
| 514 | |
Chris Lattner | 348c8a2 | 2007-08-23 23:43:33 +0000 | [diff] [blame] | 515 | /// EmitAnyExpr - Emit an expression of any type: scalar, complex, aggregate, |
| 516 | /// returning an rvalue corresponding to it. If NeedResult is false, the |
| 517 | /// result of the expression doesn't need to be generated into memory. |
| 518 | RValue CodeGenFunction::EmitAnyExpr(const Expr *E, bool NeedResult) { |
| 519 | if (!hasAggregateLLVMType(E->getType())) |
| 520 | return EmitExpr(E); |
| 521 | |
| 522 | llvm::Value *DestMem = 0; |
| 523 | if (NeedResult) |
| 524 | DestMem = CreateTempAlloca(ConvertType(E->getType())); |
| 525 | |
| 526 | if (!E->getType()->isComplexType()) { |
| 527 | EmitAggExpr(E, DestMem, false); |
| 528 | } else if (NeedResult) |
| 529 | EmitComplexExprIntoAddr(E, DestMem); |
| 530 | else |
| 531 | EmitComplexExpr(E); |
| 532 | |
| 533 | return RValue::getAggregate(DestMem); |
| 534 | } |
| 535 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 536 | RValue CodeGenFunction::EmitExpr(const Expr *E) { |
Chris Lattner | bdb8ffb | 2007-08-11 00:04:45 +0000 | [diff] [blame] | 537 | assert(E && !hasAggregateLLVMType(E->getType()) && |
| 538 | "Invalid scalar expression to emit"); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 539 | |
| 540 | switch (E->getStmtClass()) { |
| 541 | default: |
| 542 | fprintf(stderr, "Unimplemented expr!\n"); |
| 543 | E->dump(); |
| 544 | return RValue::get(llvm::UndefValue::get(llvm::Type::Int32Ty)); |
| 545 | |
| 546 | // l-values. |
| 547 | case Expr::DeclRefExprClass: |
| 548 | // DeclRef's of EnumConstantDecl's are simple rvalues. |
| 549 | if (const EnumConstantDecl *EC = |
| 550 | dyn_cast<EnumConstantDecl>(cast<DeclRefExpr>(E)->getDecl())) |
| 551 | return RValue::get(llvm::ConstantInt::get(EC->getInitVal())); |
| 552 | return EmitLoadOfLValue(E); |
| 553 | case Expr::ArraySubscriptExprClass: |
| 554 | return EmitArraySubscriptExprRV(cast<ArraySubscriptExpr>(E)); |
Chris Lattner | a0d03a7 | 2007-08-03 17:31:20 +0000 | [diff] [blame] | 555 | case Expr::OCUVectorElementExprClass: |
Chris Lattner | a735fac | 2007-08-03 00:16:29 +0000 | [diff] [blame] | 556 | return EmitLoadOfLValue(E); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 557 | case Expr::PreDefinedExprClass: |
| 558 | case Expr::StringLiteralClass: |
| 559 | return RValue::get(EmitLValue(E).getAddress()); |
| 560 | |
| 561 | // Leaf expressions. |
| 562 | case Expr::IntegerLiteralClass: |
| 563 | return EmitIntegerLiteral(cast<IntegerLiteral>(E)); |
| 564 | case Expr::FloatingLiteralClass: |
| 565 | return EmitFloatingLiteral(cast<FloatingLiteral>(E)); |
| 566 | case Expr::CharacterLiteralClass: |
| 567 | return EmitCharacterLiteral(cast<CharacterLiteral>(E)); |
Chris Lattner | 4ca7e75 | 2007-08-03 17:51:03 +0000 | [diff] [blame] | 568 | case Expr::TypesCompatibleExprClass: |
| 569 | return EmitTypesCompatibleExpr(cast<TypesCompatibleExpr>(E)); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 570 | |
| 571 | // Operators. |
| 572 | case Expr::ParenExprClass: |
| 573 | return EmitExpr(cast<ParenExpr>(E)->getSubExpr()); |
| 574 | case Expr::UnaryOperatorClass: |
| 575 | return EmitUnaryOperator(cast<UnaryOperator>(E)); |
| 576 | case Expr::SizeOfAlignOfTypeExprClass: |
| 577 | return EmitSizeAlignOf(cast<SizeOfAlignOfTypeExpr>(E)->getArgumentType(), |
| 578 | E->getType(), |
| 579 | cast<SizeOfAlignOfTypeExpr>(E)->isSizeOf()); |
| 580 | case Expr::ImplicitCastExprClass: |
Chris Lattner | b2cb9cb | 2007-08-20 22:37:10 +0000 | [diff] [blame] | 581 | return EmitImplicitCastExpr(cast<ImplicitCastExpr>(E)); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 582 | case Expr::CastExprClass: |
| 583 | return EmitCastExpr(cast<CastExpr>(E)->getSubExpr(), E->getType()); |
| 584 | case Expr::CallExprClass: |
| 585 | return EmitCallExpr(cast<CallExpr>(E)); |
| 586 | case Expr::BinaryOperatorClass: |
| 587 | return EmitBinaryOperator(cast<BinaryOperator>(E)); |
| 588 | |
| 589 | case Expr::ConditionalOperatorClass: |
| 590 | return EmitConditionalOperator(cast<ConditionalOperator>(E)); |
Chris Lattner | 44fcf4f | 2007-08-04 00:20:15 +0000 | [diff] [blame] | 591 | case Expr::ChooseExprClass: |
| 592 | return EmitChooseExpr(cast<ChooseExpr>(E)); |
Anders Carlsson | a66cad4 | 2007-08-21 17:43:55 +0000 | [diff] [blame] | 593 | case Expr::ObjCStringLiteralClass: |
| 594 | return EmitObjCStringLiteral(cast<ObjCStringLiteral>(E)); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 595 | } |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 596 | } |
| 597 | |
| 598 | RValue CodeGenFunction::EmitIntegerLiteral(const IntegerLiteral *E) { |
| 599 | return RValue::get(llvm::ConstantInt::get(E->getValue())); |
| 600 | } |
| 601 | RValue CodeGenFunction::EmitFloatingLiteral(const FloatingLiteral *E) { |
| 602 | return RValue::get(llvm::ConstantFP::get(ConvertType(E->getType()), |
| 603 | E->getValue())); |
| 604 | } |
| 605 | RValue CodeGenFunction::EmitCharacterLiteral(const CharacterLiteral *E) { |
| 606 | return RValue::get(llvm::ConstantInt::get(ConvertType(E->getType()), |
| 607 | E->getValue())); |
| 608 | } |
| 609 | |
Chris Lattner | 4ca7e75 | 2007-08-03 17:51:03 +0000 | [diff] [blame] | 610 | RValue CodeGenFunction::EmitTypesCompatibleExpr(const TypesCompatibleExpr *E) { |
| 611 | return RValue::get(llvm::ConstantInt::get(ConvertType(E->getType()), |
| 612 | E->typesAreCompatible())); |
| 613 | } |
| 614 | |
Chris Lattner | 44fcf4f | 2007-08-04 00:20:15 +0000 | [diff] [blame] | 615 | /// EmitChooseExpr - Implement __builtin_choose_expr. |
| 616 | RValue CodeGenFunction::EmitChooseExpr(const ChooseExpr *E) { |
| 617 | llvm::APSInt CondVal(32); |
| 618 | bool IsConst = E->getCond()->isIntegerConstantExpr(CondVal, getContext()); |
| 619 | assert(IsConst && "Condition of choose expr must be i-c-e"); IsConst=IsConst; |
| 620 | |
| 621 | // Emit the LHS or RHS as appropriate. |
| 622 | return EmitExpr(CondVal != 0 ? E->getLHS() : E->getRHS()); |
| 623 | } |
| 624 | |
Chris Lattner | 4ca7e75 | 2007-08-03 17:51:03 +0000 | [diff] [blame] | 625 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 626 | RValue CodeGenFunction::EmitArraySubscriptExprRV(const ArraySubscriptExpr *E) { |
| 627 | // Emit subscript expressions in rvalue context's. For most cases, this just |
| 628 | // loads the lvalue formed by the subscript expr. However, we have to be |
| 629 | // careful, because the base of a vector subscript is occasionally an rvalue, |
| 630 | // so we can't get it as an lvalue. |
| 631 | if (!E->getBase()->getType()->isVectorType()) |
| 632 | return EmitLoadOfLValue(E); |
| 633 | |
| 634 | // Handle the vector case. The base must be a vector, the index must be an |
| 635 | // integer value. |
Chris Lattner | 2af72ac | 2007-08-08 17:43:05 +0000 | [diff] [blame] | 636 | llvm::Value *Base = EmitExpr(E->getBase()).getVal(); |
| 637 | llvm::Value *Idx = EmitExpr(E->getIdx()).getVal(); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 638 | |
| 639 | // FIXME: Convert Idx to i32 type. |
| 640 | |
| 641 | return RValue::get(Builder.CreateExtractElement(Base, Idx, "vecext")); |
| 642 | } |
| 643 | |
| 644 | // EmitCastExpr - Emit code for an explicit or implicit cast. Implicit casts |
| 645 | // have to handle a more broad range of conversions than explicit casts, as they |
| 646 | // handle things like function to ptr-to-function decay etc. |
| 647 | RValue CodeGenFunction::EmitCastExpr(const Expr *Op, QualType DestTy) { |
Chris Lattner | 5ac222c | 2007-08-24 00:01:20 +0000 | [diff] [blame] | 648 | RValue Src = EmitAnyExpr(Op); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 649 | |
| 650 | // If the destination is void, just evaluate the source. |
| 651 | if (DestTy->isVoidType()) |
| 652 | return RValue::getAggregate(0); |
| 653 | |
Chris Lattner | 2af72ac | 2007-08-08 17:43:05 +0000 | [diff] [blame] | 654 | return EmitConversion(Src, Op->getType(), DestTy); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 655 | } |
| 656 | |
Chris Lattner | b2cb9cb | 2007-08-20 22:37:10 +0000 | [diff] [blame] | 657 | /// EmitImplicitCastExpr - Implicit casts are the same as normal casts, but also |
| 658 | /// handle things like function to pointer-to-function decay, and array to |
| 659 | /// pointer decay. |
| 660 | RValue CodeGenFunction::EmitImplicitCastExpr(const ImplicitCastExpr *E) { |
| 661 | const Expr *Op = E->getSubExpr(); |
| 662 | QualType OpTy = Op->getType().getCanonicalType(); |
| 663 | |
| 664 | // If this is due to array->pointer conversion, emit the array expression as |
| 665 | // an l-value. |
| 666 | if (isa<ArrayType>(OpTy)) { |
| 667 | // FIXME: For now we assume that all source arrays map to LLVM arrays. This |
| 668 | // will not true when we add support for VLAs. |
| 669 | llvm::Value *V = EmitLValue(Op).getAddress(); // Bitfields can't be arrays. |
| 670 | |
| 671 | assert(isa<llvm::PointerType>(V->getType()) && |
| 672 | isa<llvm::ArrayType>(cast<llvm::PointerType>(V->getType()) |
| 673 | ->getElementType()) && |
| 674 | "Doesn't support VLAs yet!"); |
| 675 | llvm::Constant *Idx0 = llvm::ConstantInt::get(llvm::Type::Int32Ty, 0); |
| 676 | return RValue::get(Builder.CreateGEP(V, Idx0, Idx0, "arraydecay")); |
| 677 | } |
| 678 | |
| 679 | return EmitCastExpr(Op, E->getType()); |
| 680 | } |
| 681 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 682 | RValue CodeGenFunction::EmitCallExpr(const CallExpr *E) { |
Anders Carlsson | 4986530 | 2007-08-20 18:05:56 +0000 | [diff] [blame] | 683 | if (const ImplicitCastExpr *IcExpr = |
| 684 | dyn_cast<const ImplicitCastExpr>(E->getCallee())) |
| 685 | if (const DeclRefExpr *DRExpr = |
| 686 | dyn_cast<const DeclRefExpr>(IcExpr->getSubExpr())) |
| 687 | if (const FunctionDecl *FDecl = |
| 688 | dyn_cast<const FunctionDecl>(DRExpr->getDecl())) |
| 689 | if (unsigned builtinID = FDecl->getIdentifier()->getBuiltinID()) |
| 690 | return EmitBuiltinExpr(builtinID, E); |
| 691 | |
Chris Lattner | 2af72ac | 2007-08-08 17:43:05 +0000 | [diff] [blame] | 692 | llvm::Value *Callee = EmitExpr(E->getCallee()).getVal(); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 693 | |
| 694 | // The callee type will always be a pointer to function type, get the function |
| 695 | // type. |
Chris Lattner | 2af72ac | 2007-08-08 17:43:05 +0000 | [diff] [blame] | 696 | QualType CalleeTy = E->getCallee()->getType(); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 697 | CalleeTy = cast<PointerType>(CalleeTy.getCanonicalType())->getPointeeType(); |
| 698 | |
| 699 | // Get information about the argument types. |
| 700 | FunctionTypeProto::arg_type_iterator ArgTyIt = 0, ArgTyEnd = 0; |
| 701 | |
| 702 | // Calling unprototyped functions provides no argument info. |
| 703 | if (const FunctionTypeProto *FTP = dyn_cast<FunctionTypeProto>(CalleeTy)) { |
| 704 | ArgTyIt = FTP->arg_type_begin(); |
| 705 | ArgTyEnd = FTP->arg_type_end(); |
| 706 | } |
| 707 | |
| 708 | llvm::SmallVector<llvm::Value*, 16> Args; |
| 709 | |
Chris Lattner | 5980204 | 2007-08-10 17:02:28 +0000 | [diff] [blame] | 710 | // Handle struct-return functions by passing a pointer to the location that |
| 711 | // we would like to return into. |
| 712 | if (hasAggregateLLVMType(E->getType())) { |
| 713 | // Create a temporary alloca to hold the result of the call. :( |
| 714 | Args.push_back(CreateTempAlloca(ConvertType(E->getType()))); |
| 715 | // FIXME: set the stret attribute on the argument. |
| 716 | } |
| 717 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 718 | for (unsigned i = 0, e = E->getNumArgs(); i != e; ++i) { |
Chris Lattner | 2af72ac | 2007-08-08 17:43:05 +0000 | [diff] [blame] | 719 | QualType ArgTy = E->getArg(i)->getType(); |
Chris Lattner | 348c8a2 | 2007-08-23 23:43:33 +0000 | [diff] [blame] | 720 | RValue ArgVal = EmitAnyExpr(E->getArg(i)); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 721 | |
| 722 | // If this argument has prototype information, convert it. |
| 723 | if (ArgTyIt != ArgTyEnd) { |
| 724 | ArgVal = EmitConversion(ArgVal, ArgTy, *ArgTyIt++); |
| 725 | } else { |
| 726 | // Otherwise, if passing through "..." or to a function with no prototype, |
| 727 | // perform the "default argument promotions" (C99 6.5.2.2p6), which |
| 728 | // includes the usual unary conversions, but also promotes float to |
| 729 | // double. |
| 730 | if (const BuiltinType *BT = |
| 731 | dyn_cast<BuiltinType>(ArgTy.getCanonicalType())) { |
| 732 | if (BT->getKind() == BuiltinType::Float) |
| 733 | ArgVal = RValue::get(Builder.CreateFPExt(ArgVal.getVal(), |
| 734 | llvm::Type::DoubleTy,"tmp")); |
| 735 | } |
| 736 | } |
| 737 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 738 | if (ArgVal.isScalar()) |
| 739 | Args.push_back(ArgVal.getVal()); |
| 740 | else // Pass by-address. FIXME: Set attribute bit on call. |
| 741 | Args.push_back(ArgVal.getAggregateAddr()); |
| 742 | } |
| 743 | |
Chris Lattner | a957225 | 2007-08-01 06:24:52 +0000 | [diff] [blame] | 744 | llvm::Value *V = Builder.CreateCall(Callee, &Args[0], &Args[0]+Args.size()); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 745 | if (V->getType() != llvm::Type::VoidTy) |
| 746 | V->setName("call"); |
Chris Lattner | 5980204 | 2007-08-10 17:02:28 +0000 | [diff] [blame] | 747 | else if (hasAggregateLLVMType(E->getType())) |
| 748 | // Struct return. |
| 749 | return RValue::getAggregate(Args[0]); |
| 750 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 751 | return RValue::get(V); |
| 752 | } |
| 753 | |
| 754 | |
| 755 | //===----------------------------------------------------------------------===// |
| 756 | // Unary Operator Emission |
| 757 | //===----------------------------------------------------------------------===// |
| 758 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 759 | RValue CodeGenFunction::EmitUnaryOperator(const UnaryOperator *E) { |
| 760 | switch (E->getOpcode()) { |
| 761 | default: |
| 762 | printf("Unimplemented unary expr!\n"); |
| 763 | E->dump(); |
| 764 | return RValue::get(llvm::UndefValue::get(llvm::Type::Int32Ty)); |
| 765 | case UnaryOperator::PostInc: |
| 766 | case UnaryOperator::PostDec: |
| 767 | case UnaryOperator::PreInc : |
| 768 | case UnaryOperator::PreDec : return EmitUnaryIncDec(E); |
| 769 | case UnaryOperator::AddrOf : return EmitUnaryAddrOf(E); |
| 770 | case UnaryOperator::Deref : return EmitLoadOfLValue(E); |
| 771 | case UnaryOperator::Plus : return EmitUnaryPlus(E); |
| 772 | case UnaryOperator::Minus : return EmitUnaryMinus(E); |
| 773 | case UnaryOperator::Not : return EmitUnaryNot(E); |
| 774 | case UnaryOperator::LNot : return EmitUnaryLNot(E); |
| 775 | case UnaryOperator::SizeOf : |
| 776 | return EmitSizeAlignOf(E->getSubExpr()->getType(), E->getType(), true); |
| 777 | case UnaryOperator::AlignOf : |
| 778 | return EmitSizeAlignOf(E->getSubExpr()->getType(), E->getType(), false); |
| 779 | // FIXME: real/imag |
| 780 | case UnaryOperator::Extension: return EmitExpr(E->getSubExpr()); |
| 781 | } |
| 782 | } |
| 783 | |
| 784 | RValue CodeGenFunction::EmitUnaryIncDec(const UnaryOperator *E) { |
| 785 | LValue LV = EmitLValue(E->getSubExpr()); |
| 786 | RValue InVal = EmitLoadOfLValue(LV, E->getSubExpr()->getType()); |
| 787 | |
| 788 | // We know the operand is real or pointer type, so it must be an LLVM scalar. |
| 789 | assert(InVal.isScalar() && "Unknown thing to increment"); |
| 790 | llvm::Value *InV = InVal.getVal(); |
| 791 | |
| 792 | int AmountVal = 1; |
| 793 | if (E->getOpcode() == UnaryOperator::PreDec || |
| 794 | E->getOpcode() == UnaryOperator::PostDec) |
| 795 | AmountVal = -1; |
| 796 | |
| 797 | llvm::Value *NextVal; |
| 798 | if (isa<llvm::IntegerType>(InV->getType())) { |
| 799 | NextVal = llvm::ConstantInt::get(InV->getType(), AmountVal); |
| 800 | NextVal = Builder.CreateAdd(InV, NextVal, AmountVal == 1 ? "inc" : "dec"); |
| 801 | } else if (InV->getType()->isFloatingPoint()) { |
| 802 | NextVal = llvm::ConstantFP::get(InV->getType(), AmountVal); |
| 803 | NextVal = Builder.CreateAdd(InV, NextVal, AmountVal == 1 ? "inc" : "dec"); |
| 804 | } else { |
| 805 | // FIXME: This is not right for pointers to VLA types. |
| 806 | assert(isa<llvm::PointerType>(InV->getType())); |
| 807 | NextVal = llvm::ConstantInt::get(llvm::Type::Int32Ty, AmountVal); |
| 808 | NextVal = Builder.CreateGEP(InV, NextVal, AmountVal == 1 ? "inc" : "dec"); |
| 809 | } |
| 810 | |
| 811 | RValue NextValToStore = RValue::get(NextVal); |
| 812 | |
| 813 | // Store the updated result through the lvalue. |
| 814 | EmitStoreThroughLValue(NextValToStore, LV, E->getSubExpr()->getType()); |
| 815 | |
| 816 | // If this is a postinc, return the value read from memory, otherwise use the |
| 817 | // updated value. |
| 818 | if (E->getOpcode() == UnaryOperator::PreDec || |
| 819 | E->getOpcode() == UnaryOperator::PreInc) |
| 820 | return NextValToStore; |
| 821 | else |
| 822 | return InVal; |
| 823 | } |
| 824 | |
| 825 | /// C99 6.5.3.2 |
| 826 | RValue CodeGenFunction::EmitUnaryAddrOf(const UnaryOperator *E) { |
| 827 | // The address of the operand is just its lvalue. It cannot be a bitfield. |
| 828 | return RValue::get(EmitLValue(E->getSubExpr()).getAddress()); |
| 829 | } |
| 830 | |
| 831 | RValue CodeGenFunction::EmitUnaryPlus(const UnaryOperator *E) { |
Chris Lattner | 2af72ac | 2007-08-08 17:43:05 +0000 | [diff] [blame] | 832 | assert(E->getType().getCanonicalType() == |
| 833 | E->getSubExpr()->getType().getCanonicalType() && "Bad unary plus!"); |
| 834 | // Unary plus just returns its value. |
| 835 | return EmitExpr(E->getSubExpr()); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 836 | } |
| 837 | |
| 838 | RValue CodeGenFunction::EmitUnaryMinus(const UnaryOperator *E) { |
Chris Lattner | 2af72ac | 2007-08-08 17:43:05 +0000 | [diff] [blame] | 839 | assert(E->getType().getCanonicalType() == |
| 840 | E->getSubExpr()->getType().getCanonicalType() && "Bad unary minus!"); |
| 841 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 842 | // Unary minus performs promotions, then negates its arithmetic operand. |
Chris Lattner | 2af72ac | 2007-08-08 17:43:05 +0000 | [diff] [blame] | 843 | RValue V = EmitExpr(E->getSubExpr()); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 844 | |
| 845 | if (V.isScalar()) |
| 846 | return RValue::get(Builder.CreateNeg(V.getVal(), "neg")); |
| 847 | |
| 848 | assert(0 && "FIXME: This doesn't handle complex operands yet"); |
| 849 | } |
| 850 | |
| 851 | RValue CodeGenFunction::EmitUnaryNot(const UnaryOperator *E) { |
| 852 | // Unary not performs promotions, then complements its integer operand. |
Chris Lattner | 2af72ac | 2007-08-08 17:43:05 +0000 | [diff] [blame] | 853 | RValue V = EmitExpr(E->getSubExpr()); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 854 | |
| 855 | if (V.isScalar()) |
| 856 | return RValue::get(Builder.CreateNot(V.getVal(), "neg")); |
| 857 | |
| 858 | assert(0 && "FIXME: This doesn't handle integer complex operands yet (GNU)"); |
| 859 | } |
| 860 | |
| 861 | |
| 862 | /// C99 6.5.3.3 |
| 863 | RValue CodeGenFunction::EmitUnaryLNot(const UnaryOperator *E) { |
| 864 | // Compare operand to zero. |
| 865 | llvm::Value *BoolVal = EvaluateExprAsBool(E->getSubExpr()); |
| 866 | |
| 867 | // Invert value. |
| 868 | // TODO: Could dynamically modify easy computations here. For example, if |
| 869 | // the operand is an icmp ne, turn into icmp eq. |
| 870 | BoolVal = Builder.CreateNot(BoolVal, "lnot"); |
| 871 | |
| 872 | // ZExt result to int. |
| 873 | return RValue::get(Builder.CreateZExt(BoolVal, LLVMIntTy, "lnot.ext")); |
| 874 | } |
| 875 | |
| 876 | /// EmitSizeAlignOf - Return the size or alignment of the 'TypeToSize' type as |
| 877 | /// an integer (RetType). |
| 878 | RValue CodeGenFunction::EmitSizeAlignOf(QualType TypeToSize, |
| 879 | QualType RetType, bool isSizeOf) { |
| 880 | /// FIXME: This doesn't handle VLAs yet! |
| 881 | std::pair<uint64_t, unsigned> Info = |
| 882 | getContext().getTypeInfo(TypeToSize, SourceLocation()); |
| 883 | |
| 884 | uint64_t Val = isSizeOf ? Info.first : Info.second; |
| 885 | Val /= 8; // Return size in bytes, not bits. |
| 886 | |
| 887 | assert(RetType->isIntegerType() && "Result type must be an integer!"); |
| 888 | |
| 889 | unsigned ResultWidth = getContext().getTypeSize(RetType, SourceLocation()); |
| 890 | return RValue::get(llvm::ConstantInt::get(llvm::APInt(ResultWidth, Val))); |
| 891 | } |
| 892 | |
| 893 | |
| 894 | //===--------------------------------------------------------------------===// |
| 895 | // Binary Operator Emission |
| 896 | //===--------------------------------------------------------------------===// |
| 897 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 898 | |
| 899 | /// EmitCompoundAssignmentOperands - Compound assignment operations (like +=) |
| 900 | /// are strange in that the result of the operation is not the same type as the |
| 901 | /// intermediate computation. This function emits the LHS and RHS operands of |
| 902 | /// the compound assignment, promoting them to their common computation type. |
| 903 | /// |
| 904 | /// Since the LHS is an lvalue, and the result is stored back through it, we |
| 905 | /// return the lvalue as well as the LHS/RHS rvalues. On return, the LHS and |
| 906 | /// RHS values are both in the computation type for the operator. |
| 907 | void CodeGenFunction:: |
| 908 | EmitCompoundAssignmentOperands(const CompoundAssignOperator *E, |
| 909 | LValue &LHSLV, RValue &LHS, RValue &RHS) { |
| 910 | LHSLV = EmitLValue(E->getLHS()); |
| 911 | |
| 912 | // Load the LHS and RHS operands. |
| 913 | QualType LHSTy = E->getLHS()->getType(); |
| 914 | LHS = EmitLoadOfLValue(LHSLV, LHSTy); |
Chris Lattner | 2af72ac | 2007-08-08 17:43:05 +0000 | [diff] [blame] | 915 | RHS = EmitExpr(E->getRHS()); |
| 916 | QualType RHSTy = E->getRHS()->getType(); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 917 | |
| 918 | // Convert the LHS and RHS to the common evaluation type. |
| 919 | LHS = EmitConversion(LHS, LHSTy, E->getComputationType()); |
| 920 | RHS = EmitConversion(RHS, RHSTy, E->getComputationType()); |
| 921 | } |
| 922 | |
| 923 | /// EmitCompoundAssignmentResult - Given a result value in the computation type, |
| 924 | /// truncate it down to the actual result type, store it through the LHS lvalue, |
| 925 | /// and return it. |
| 926 | RValue CodeGenFunction:: |
| 927 | EmitCompoundAssignmentResult(const CompoundAssignOperator *E, |
| 928 | LValue LHSLV, RValue ResV) { |
| 929 | |
| 930 | // Truncate back to the destination type. |
| 931 | if (E->getComputationType() != E->getType()) |
| 932 | ResV = EmitConversion(ResV, E->getComputationType(), E->getType()); |
| 933 | |
| 934 | // Store the result value into the LHS. |
| 935 | EmitStoreThroughLValue(ResV, LHSLV, E->getType()); |
| 936 | |
| 937 | // Return the result. |
| 938 | return ResV; |
| 939 | } |
| 940 | |
| 941 | |
| 942 | RValue CodeGenFunction::EmitBinaryOperator(const BinaryOperator *E) { |
| 943 | RValue LHS, RHS; |
| 944 | switch (E->getOpcode()) { |
| 945 | default: |
| 946 | fprintf(stderr, "Unimplemented binary expr!\n"); |
| 947 | E->dump(); |
| 948 | return RValue::get(llvm::UndefValue::get(llvm::Type::Int32Ty)); |
| 949 | case BinaryOperator::Mul: |
Chris Lattner | bf49e99 | 2007-08-08 17:49:18 +0000 | [diff] [blame] | 950 | LHS = EmitExpr(E->getLHS()); |
| 951 | RHS = EmitExpr(E->getRHS()); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 952 | return EmitMul(LHS, RHS, E->getType()); |
| 953 | case BinaryOperator::Div: |
Chris Lattner | bf49e99 | 2007-08-08 17:49:18 +0000 | [diff] [blame] | 954 | LHS = EmitExpr(E->getLHS()); |
| 955 | RHS = EmitExpr(E->getRHS()); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 956 | return EmitDiv(LHS, RHS, E->getType()); |
| 957 | case BinaryOperator::Rem: |
Chris Lattner | bf49e99 | 2007-08-08 17:49:18 +0000 | [diff] [blame] | 958 | LHS = EmitExpr(E->getLHS()); |
| 959 | RHS = EmitExpr(E->getRHS()); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 960 | return EmitRem(LHS, RHS, E->getType()); |
Chris Lattner | bf49e99 | 2007-08-08 17:49:18 +0000 | [diff] [blame] | 961 | case BinaryOperator::Add: |
| 962 | LHS = EmitExpr(E->getLHS()); |
| 963 | RHS = EmitExpr(E->getRHS()); |
| 964 | if (!E->getType()->isPointerType()) |
| 965 | return EmitAdd(LHS, RHS, E->getType()); |
| 966 | |
| 967 | return EmitPointerAdd(LHS, E->getLHS()->getType(), |
| 968 | RHS, E->getRHS()->getType(), E->getType()); |
| 969 | case BinaryOperator::Sub: |
| 970 | LHS = EmitExpr(E->getLHS()); |
| 971 | RHS = EmitExpr(E->getRHS()); |
| 972 | |
| 973 | if (!E->getLHS()->getType()->isPointerType()) |
| 974 | return EmitSub(LHS, RHS, E->getType()); |
| 975 | |
| 976 | return EmitPointerSub(LHS, E->getLHS()->getType(), |
| 977 | RHS, E->getRHS()->getType(), E->getType()); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 978 | case BinaryOperator::Shl: |
Chris Lattner | 2af72ac | 2007-08-08 17:43:05 +0000 | [diff] [blame] | 979 | LHS = EmitExpr(E->getLHS()); |
| 980 | RHS = EmitExpr(E->getRHS()); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 981 | return EmitShl(LHS, RHS, E->getType()); |
| 982 | case BinaryOperator::Shr: |
Chris Lattner | 2af72ac | 2007-08-08 17:43:05 +0000 | [diff] [blame] | 983 | LHS = EmitExpr(E->getLHS()); |
| 984 | RHS = EmitExpr(E->getRHS()); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 985 | return EmitShr(LHS, RHS, E->getType()); |
| 986 | case BinaryOperator::And: |
Chris Lattner | bf49e99 | 2007-08-08 17:49:18 +0000 | [diff] [blame] | 987 | LHS = EmitExpr(E->getLHS()); |
| 988 | RHS = EmitExpr(E->getRHS()); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 989 | return EmitAnd(LHS, RHS, E->getType()); |
| 990 | case BinaryOperator::Xor: |
Chris Lattner | bf49e99 | 2007-08-08 17:49:18 +0000 | [diff] [blame] | 991 | LHS = EmitExpr(E->getLHS()); |
| 992 | RHS = EmitExpr(E->getRHS()); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 993 | return EmitXor(LHS, RHS, E->getType()); |
| 994 | case BinaryOperator::Or : |
Chris Lattner | bf49e99 | 2007-08-08 17:49:18 +0000 | [diff] [blame] | 995 | LHS = EmitExpr(E->getLHS()); |
| 996 | RHS = EmitExpr(E->getRHS()); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 997 | return EmitOr(LHS, RHS, E->getType()); |
| 998 | case BinaryOperator::LAnd: return EmitBinaryLAnd(E); |
| 999 | case BinaryOperator::LOr: return EmitBinaryLOr(E); |
| 1000 | case BinaryOperator::LT: |
| 1001 | return EmitBinaryCompare(E, llvm::ICmpInst::ICMP_ULT, |
| 1002 | llvm::ICmpInst::ICMP_SLT, |
| 1003 | llvm::FCmpInst::FCMP_OLT); |
| 1004 | case BinaryOperator::GT: |
| 1005 | return EmitBinaryCompare(E, llvm::ICmpInst::ICMP_UGT, |
| 1006 | llvm::ICmpInst::ICMP_SGT, |
| 1007 | llvm::FCmpInst::FCMP_OGT); |
| 1008 | case BinaryOperator::LE: |
| 1009 | return EmitBinaryCompare(E, llvm::ICmpInst::ICMP_ULE, |
| 1010 | llvm::ICmpInst::ICMP_SLE, |
| 1011 | llvm::FCmpInst::FCMP_OLE); |
| 1012 | case BinaryOperator::GE: |
| 1013 | return EmitBinaryCompare(E, llvm::ICmpInst::ICMP_UGE, |
| 1014 | llvm::ICmpInst::ICMP_SGE, |
| 1015 | llvm::FCmpInst::FCMP_OGE); |
| 1016 | case BinaryOperator::EQ: |
| 1017 | return EmitBinaryCompare(E, llvm::ICmpInst::ICMP_EQ, |
| 1018 | llvm::ICmpInst::ICMP_EQ, |
| 1019 | llvm::FCmpInst::FCMP_OEQ); |
| 1020 | case BinaryOperator::NE: |
| 1021 | return EmitBinaryCompare(E, llvm::ICmpInst::ICMP_NE, |
| 1022 | llvm::ICmpInst::ICMP_NE, |
| 1023 | llvm::FCmpInst::FCMP_UNE); |
| 1024 | case BinaryOperator::Assign: |
| 1025 | return EmitBinaryAssign(E); |
| 1026 | |
| 1027 | case BinaryOperator::MulAssign: { |
| 1028 | const CompoundAssignOperator *CAO = cast<CompoundAssignOperator>(E); |
| 1029 | LValue LHSLV; |
| 1030 | EmitCompoundAssignmentOperands(CAO, LHSLV, LHS, RHS); |
| 1031 | LHS = EmitMul(LHS, RHS, CAO->getComputationType()); |
| 1032 | return EmitCompoundAssignmentResult(CAO, LHSLV, LHS); |
| 1033 | } |
| 1034 | case BinaryOperator::DivAssign: { |
| 1035 | const CompoundAssignOperator *CAO = cast<CompoundAssignOperator>(E); |
| 1036 | LValue LHSLV; |
| 1037 | EmitCompoundAssignmentOperands(CAO, LHSLV, LHS, RHS); |
| 1038 | LHS = EmitDiv(LHS, RHS, CAO->getComputationType()); |
| 1039 | return EmitCompoundAssignmentResult(CAO, LHSLV, LHS); |
| 1040 | } |
| 1041 | case BinaryOperator::RemAssign: { |
| 1042 | const CompoundAssignOperator *CAO = cast<CompoundAssignOperator>(E); |
| 1043 | LValue LHSLV; |
| 1044 | EmitCompoundAssignmentOperands(CAO, LHSLV, LHS, RHS); |
| 1045 | LHS = EmitRem(LHS, RHS, CAO->getComputationType()); |
| 1046 | return EmitCompoundAssignmentResult(CAO, LHSLV, LHS); |
| 1047 | } |
| 1048 | case BinaryOperator::AddAssign: { |
| 1049 | const CompoundAssignOperator *CAO = cast<CompoundAssignOperator>(E); |
| 1050 | LValue LHSLV; |
| 1051 | EmitCompoundAssignmentOperands(CAO, LHSLV, LHS, RHS); |
| 1052 | LHS = EmitAdd(LHS, RHS, CAO->getComputationType()); |
| 1053 | return EmitCompoundAssignmentResult(CAO, LHSLV, LHS); |
| 1054 | } |
| 1055 | case BinaryOperator::SubAssign: { |
| 1056 | const CompoundAssignOperator *CAO = cast<CompoundAssignOperator>(E); |
| 1057 | LValue LHSLV; |
| 1058 | EmitCompoundAssignmentOperands(CAO, LHSLV, LHS, RHS); |
| 1059 | LHS = EmitSub(LHS, RHS, CAO->getComputationType()); |
| 1060 | return EmitCompoundAssignmentResult(CAO, LHSLV, LHS); |
| 1061 | } |
| 1062 | case BinaryOperator::ShlAssign: { |
| 1063 | const CompoundAssignOperator *CAO = cast<CompoundAssignOperator>(E); |
| 1064 | LValue LHSLV; |
| 1065 | EmitCompoundAssignmentOperands(CAO, LHSLV, LHS, RHS); |
| 1066 | LHS = EmitShl(LHS, RHS, CAO->getComputationType()); |
| 1067 | return EmitCompoundAssignmentResult(CAO, LHSLV, LHS); |
| 1068 | } |
| 1069 | case BinaryOperator::ShrAssign: { |
| 1070 | const CompoundAssignOperator *CAO = cast<CompoundAssignOperator>(E); |
| 1071 | LValue LHSLV; |
| 1072 | EmitCompoundAssignmentOperands(CAO, LHSLV, LHS, RHS); |
| 1073 | LHS = EmitShr(LHS, RHS, CAO->getComputationType()); |
| 1074 | return EmitCompoundAssignmentResult(CAO, LHSLV, LHS); |
| 1075 | } |
| 1076 | case BinaryOperator::AndAssign: { |
| 1077 | const CompoundAssignOperator *CAO = cast<CompoundAssignOperator>(E); |
| 1078 | LValue LHSLV; |
| 1079 | EmitCompoundAssignmentOperands(CAO, LHSLV, LHS, RHS); |
| 1080 | LHS = EmitAnd(LHS, RHS, CAO->getComputationType()); |
| 1081 | return EmitCompoundAssignmentResult(CAO, LHSLV, LHS); |
| 1082 | } |
| 1083 | case BinaryOperator::OrAssign: { |
| 1084 | const CompoundAssignOperator *CAO = cast<CompoundAssignOperator>(E); |
| 1085 | LValue LHSLV; |
| 1086 | EmitCompoundAssignmentOperands(CAO, LHSLV, LHS, RHS); |
| 1087 | LHS = EmitOr(LHS, RHS, CAO->getComputationType()); |
| 1088 | return EmitCompoundAssignmentResult(CAO, LHSLV, LHS); |
| 1089 | } |
| 1090 | case BinaryOperator::XorAssign: { |
| 1091 | const CompoundAssignOperator *CAO = cast<CompoundAssignOperator>(E); |
| 1092 | LValue LHSLV; |
| 1093 | EmitCompoundAssignmentOperands(CAO, LHSLV, LHS, RHS); |
| 1094 | LHS = EmitXor(LHS, RHS, CAO->getComputationType()); |
| 1095 | return EmitCompoundAssignmentResult(CAO, LHSLV, LHS); |
| 1096 | } |
| 1097 | case BinaryOperator::Comma: return EmitBinaryComma(E); |
| 1098 | } |
| 1099 | } |
| 1100 | |
| 1101 | RValue CodeGenFunction::EmitMul(RValue LHS, RValue RHS, QualType ResTy) { |
Chris Lattner | a730010 | 2007-08-21 04:59:27 +0000 | [diff] [blame] | 1102 | return RValue::get(Builder.CreateMul(LHS.getVal(), RHS.getVal(), "mul")); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1103 | } |
| 1104 | |
| 1105 | RValue CodeGenFunction::EmitDiv(RValue LHS, RValue RHS, QualType ResTy) { |
Chris Lattner | 9a4e2c6 | 2007-08-21 16:34:16 +0000 | [diff] [blame] | 1106 | if (LHS.getVal()->getType()->isFloatingPoint()) |
| 1107 | return RValue::get(Builder.CreateFDiv(LHS.getVal(), RHS.getVal(), "div")); |
| 1108 | else if (ResTy->isUnsignedIntegerType()) |
| 1109 | return RValue::get(Builder.CreateUDiv(LHS.getVal(), RHS.getVal(), "div")); |
| 1110 | else |
| 1111 | return RValue::get(Builder.CreateSDiv(LHS.getVal(), RHS.getVal(), "div")); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1112 | } |
| 1113 | |
| 1114 | RValue CodeGenFunction::EmitRem(RValue LHS, RValue RHS, QualType ResTy) { |
Chris Lattner | 9a4e2c6 | 2007-08-21 16:34:16 +0000 | [diff] [blame] | 1115 | // Rem in C can't be a floating point type: C99 6.5.5p2. |
| 1116 | if (ResTy->isUnsignedIntegerType()) |
| 1117 | return RValue::get(Builder.CreateURem(LHS.getVal(), RHS.getVal(), "rem")); |
| 1118 | else |
| 1119 | return RValue::get(Builder.CreateSRem(LHS.getVal(), RHS.getVal(), "rem")); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1120 | } |
| 1121 | |
| 1122 | RValue CodeGenFunction::EmitAdd(RValue LHS, RValue RHS, QualType ResTy) { |
Chris Lattner | a730010 | 2007-08-21 04:59:27 +0000 | [diff] [blame] | 1123 | return RValue::get(Builder.CreateAdd(LHS.getVal(), RHS.getVal(), "add")); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1124 | } |
| 1125 | |
| 1126 | RValue CodeGenFunction::EmitPointerAdd(RValue LHS, QualType LHSTy, |
| 1127 | RValue RHS, QualType RHSTy, |
| 1128 | QualType ResTy) { |
| 1129 | llvm::Value *LHSValue = LHS.getVal(); |
| 1130 | llvm::Value *RHSValue = RHS.getVal(); |
| 1131 | if (LHSTy->isPointerType()) { |
| 1132 | // pointer + int |
| 1133 | return RValue::get(Builder.CreateGEP(LHSValue, RHSValue, "add.ptr")); |
| 1134 | } else { |
| 1135 | // int + pointer |
| 1136 | return RValue::get(Builder.CreateGEP(RHSValue, LHSValue, "add.ptr")); |
| 1137 | } |
| 1138 | } |
| 1139 | |
| 1140 | RValue CodeGenFunction::EmitSub(RValue LHS, RValue RHS, QualType ResTy) { |
Chris Lattner | 9a4e2c6 | 2007-08-21 16:34:16 +0000 | [diff] [blame] | 1141 | return RValue::get(Builder.CreateSub(LHS.getVal(), RHS.getVal(), "sub")); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1142 | } |
| 1143 | |
| 1144 | RValue CodeGenFunction::EmitPointerSub(RValue LHS, QualType LHSTy, |
| 1145 | RValue RHS, QualType RHSTy, |
| 1146 | QualType ResTy) { |
| 1147 | llvm::Value *LHSValue = LHS.getVal(); |
| 1148 | llvm::Value *RHSValue = RHS.getVal(); |
| 1149 | if (const PointerType *RHSPtrType = |
| 1150 | dyn_cast<PointerType>(RHSTy.getTypePtr())) { |
| 1151 | // pointer - pointer |
| 1152 | const PointerType *LHSPtrType = cast<PointerType>(LHSTy.getTypePtr()); |
| 1153 | QualType LHSElementType = LHSPtrType->getPointeeType(); |
| 1154 | assert(LHSElementType == RHSPtrType->getPointeeType() && |
| 1155 | "can't subtract pointers with differing element types"); |
| 1156 | uint64_t ElementSize = getContext().getTypeSize(LHSElementType, |
| 1157 | SourceLocation()) / 8; |
| 1158 | const llvm::Type *ResultType = ConvertType(ResTy); |
| 1159 | llvm::Value *CastLHS = Builder.CreatePtrToInt(LHSValue, ResultType, |
| 1160 | "sub.ptr.lhs.cast"); |
| 1161 | llvm::Value *CastRHS = Builder.CreatePtrToInt(RHSValue, ResultType, |
| 1162 | "sub.ptr.rhs.cast"); |
| 1163 | llvm::Value *BytesBetween = Builder.CreateSub(CastLHS, CastRHS, |
| 1164 | "sub.ptr.sub"); |
| 1165 | |
| 1166 | // HACK: LLVM doesn't have an divide instruction that 'knows' there is no |
| 1167 | // remainder. As such, we handle common power-of-two cases here to generate |
| 1168 | // better code. |
| 1169 | if (llvm::isPowerOf2_64(ElementSize)) { |
| 1170 | llvm::Value *ShAmt = |
| 1171 | llvm::ConstantInt::get(ResultType, llvm::Log2_64(ElementSize)); |
| 1172 | return RValue::get(Builder.CreateAShr(BytesBetween, ShAmt,"sub.ptr.shr")); |
| 1173 | } else { |
| 1174 | // Otherwise, do a full sdiv. |
| 1175 | llvm::Value *BytesPerElement = |
| 1176 | llvm::ConstantInt::get(ResultType, ElementSize); |
| 1177 | return RValue::get(Builder.CreateSDiv(BytesBetween, BytesPerElement, |
| 1178 | "sub.ptr.div")); |
| 1179 | } |
| 1180 | } else { |
| 1181 | // pointer - int |
| 1182 | llvm::Value *NegatedRHS = Builder.CreateNeg(RHSValue, "sub.ptr.neg"); |
| 1183 | return RValue::get(Builder.CreateGEP(LHSValue, NegatedRHS, "sub.ptr")); |
| 1184 | } |
| 1185 | } |
| 1186 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1187 | RValue CodeGenFunction::EmitShl(RValue LHSV, RValue RHSV, QualType ResTy) { |
| 1188 | llvm::Value *LHS = LHSV.getVal(), *RHS = RHSV.getVal(); |
| 1189 | |
| 1190 | // LLVM requires the LHS and RHS to be the same type, promote or truncate the |
| 1191 | // RHS to the same size as the LHS. |
| 1192 | if (LHS->getType() != RHS->getType()) |
| 1193 | RHS = Builder.CreateIntCast(RHS, LHS->getType(), false, "sh_prom"); |
| 1194 | |
| 1195 | return RValue::get(Builder.CreateShl(LHS, RHS, "shl")); |
| 1196 | } |
| 1197 | |
| 1198 | RValue CodeGenFunction::EmitShr(RValue LHSV, RValue RHSV, QualType ResTy) { |
| 1199 | llvm::Value *LHS = LHSV.getVal(), *RHS = RHSV.getVal(); |
| 1200 | |
| 1201 | // LLVM requires the LHS and RHS to be the same type, promote or truncate the |
| 1202 | // RHS to the same size as the LHS. |
| 1203 | if (LHS->getType() != RHS->getType()) |
| 1204 | RHS = Builder.CreateIntCast(RHS, LHS->getType(), false, "sh_prom"); |
| 1205 | |
| 1206 | if (ResTy->isUnsignedIntegerType()) |
| 1207 | return RValue::get(Builder.CreateLShr(LHS, RHS, "shr")); |
| 1208 | else |
| 1209 | return RValue::get(Builder.CreateAShr(LHS, RHS, "shr")); |
| 1210 | } |
| 1211 | |
| 1212 | RValue CodeGenFunction::EmitBinaryCompare(const BinaryOperator *E, |
| 1213 | unsigned UICmpOpc, unsigned SICmpOpc, |
| 1214 | unsigned FCmpOpc) { |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1215 | llvm::Value *Result; |
Chris Lattner | 5280c5f | 2007-08-21 16:57:55 +0000 | [diff] [blame] | 1216 | QualType LHSTy = E->getLHS()->getType(); |
| 1217 | if (!LHSTy->isComplexType()) { |
| 1218 | RValue LHS = EmitExpr(E->getLHS()); |
| 1219 | RValue RHS = EmitExpr(E->getRHS()); |
| 1220 | |
| 1221 | if (LHSTy->isRealFloatingType()) { |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1222 | Result = Builder.CreateFCmp((llvm::FCmpInst::Predicate)FCmpOpc, |
| 1223 | LHS.getVal(), RHS.getVal(), "cmp"); |
Chris Lattner | 5280c5f | 2007-08-21 16:57:55 +0000 | [diff] [blame] | 1224 | } else if (LHSTy->isUnsignedIntegerType()) { |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1225 | // FIXME: This check isn't right for "unsigned short < int" where ushort |
| 1226 | // promotes to int and does a signed compare. |
| 1227 | Result = Builder.CreateICmp((llvm::ICmpInst::Predicate)UICmpOpc, |
| 1228 | LHS.getVal(), RHS.getVal(), "cmp"); |
| 1229 | } else { |
| 1230 | // Signed integers and pointers. |
| 1231 | Result = Builder.CreateICmp((llvm::ICmpInst::Predicate)SICmpOpc, |
| 1232 | LHS.getVal(), RHS.getVal(), "cmp"); |
| 1233 | } |
| 1234 | } else { |
Chris Lattner | 5280c5f | 2007-08-21 16:57:55 +0000 | [diff] [blame] | 1235 | // Complex Comparison: can only be an equality comparison. |
| 1236 | ComplexPairTy LHS = EmitComplexExpr(E->getLHS()); |
| 1237 | ComplexPairTy RHS = EmitComplexExpr(E->getRHS()); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1238 | |
Chris Lattner | 5280c5f | 2007-08-21 16:57:55 +0000 | [diff] [blame] | 1239 | QualType CETy = |
| 1240 | cast<ComplexType>(LHSTy.getCanonicalType())->getElementType(); |
| 1241 | |
| 1242 | llvm::Value *ResultR, *ResultI; |
| 1243 | if (CETy->isRealFloatingType()) { |
| 1244 | ResultR = Builder.CreateFCmp((llvm::FCmpInst::Predicate)FCmpOpc, |
| 1245 | LHS.first, RHS.first, "cmp.r"); |
| 1246 | ResultI = Builder.CreateFCmp((llvm::FCmpInst::Predicate)FCmpOpc, |
| 1247 | LHS.second, RHS.second, "cmp.i"); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1248 | } else { |
Chris Lattner | 14b0dad | 2007-08-21 17:03:38 +0000 | [diff] [blame] | 1249 | // Complex comparisons can only be equality comparisons. As such, signed |
| 1250 | // and unsigned opcodes are the same. |
| 1251 | ResultR = Builder.CreateICmp((llvm::ICmpInst::Predicate)UICmpOpc, |
Chris Lattner | 5280c5f | 2007-08-21 16:57:55 +0000 | [diff] [blame] | 1252 | LHS.first, RHS.first, "cmp.r"); |
Chris Lattner | 14b0dad | 2007-08-21 17:03:38 +0000 | [diff] [blame] | 1253 | ResultI = Builder.CreateICmp((llvm::ICmpInst::Predicate)UICmpOpc, |
Chris Lattner | 5280c5f | 2007-08-21 16:57:55 +0000 | [diff] [blame] | 1254 | LHS.second, RHS.second, "cmp.i"); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1255 | } |
Chris Lattner | 5280c5f | 2007-08-21 16:57:55 +0000 | [diff] [blame] | 1256 | |
| 1257 | if (E->getOpcode() == BinaryOperator::EQ) { |
| 1258 | Result = Builder.CreateAnd(ResultR, ResultI, "and.ri"); |
| 1259 | } else { |
| 1260 | assert(E->getOpcode() == BinaryOperator::NE && |
| 1261 | "Complex comparison other than == or != ?"); |
| 1262 | Result = Builder.CreateOr(ResultR, ResultI, "or.ri"); |
| 1263 | } |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1264 | } |
| 1265 | |
| 1266 | // ZExt result to int. |
| 1267 | return RValue::get(Builder.CreateZExt(Result, LLVMIntTy, "cmp.ext")); |
| 1268 | } |
| 1269 | |
| 1270 | RValue CodeGenFunction::EmitAnd(RValue LHS, RValue RHS, QualType ResTy) { |
Chris Lattner | 7c257f4 | 2007-08-21 17:12:50 +0000 | [diff] [blame] | 1271 | return RValue::get(Builder.CreateAnd(LHS.getVal(), RHS.getVal(), "and")); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1272 | } |
| 1273 | |
| 1274 | RValue CodeGenFunction::EmitXor(RValue LHS, RValue RHS, QualType ResTy) { |
Chris Lattner | 7c257f4 | 2007-08-21 17:12:50 +0000 | [diff] [blame] | 1275 | return RValue::get(Builder.CreateXor(LHS.getVal(), RHS.getVal(), "xor")); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1276 | } |
| 1277 | |
| 1278 | RValue CodeGenFunction::EmitOr(RValue LHS, RValue RHS, QualType ResTy) { |
Chris Lattner | 7c257f4 | 2007-08-21 17:12:50 +0000 | [diff] [blame] | 1279 | return RValue::get(Builder.CreateOr(LHS.getVal(), RHS.getVal(), "or")); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1280 | } |
| 1281 | |
| 1282 | RValue CodeGenFunction::EmitBinaryLAnd(const BinaryOperator *E) { |
| 1283 | llvm::Value *LHSCond = EvaluateExprAsBool(E->getLHS()); |
| 1284 | |
| 1285 | llvm::BasicBlock *ContBlock = new llvm::BasicBlock("land_cont"); |
| 1286 | llvm::BasicBlock *RHSBlock = new llvm::BasicBlock("land_rhs"); |
| 1287 | |
| 1288 | llvm::BasicBlock *OrigBlock = Builder.GetInsertBlock(); |
| 1289 | Builder.CreateCondBr(LHSCond, RHSBlock, ContBlock); |
| 1290 | |
| 1291 | EmitBlock(RHSBlock); |
| 1292 | llvm::Value *RHSCond = EvaluateExprAsBool(E->getRHS()); |
| 1293 | |
| 1294 | // Reaquire the RHS block, as there may be subblocks inserted. |
| 1295 | RHSBlock = Builder.GetInsertBlock(); |
| 1296 | EmitBlock(ContBlock); |
| 1297 | |
| 1298 | // Create a PHI node. If we just evaluted the LHS condition, the result is |
| 1299 | // false. If we evaluated both, the result is the RHS condition. |
| 1300 | llvm::PHINode *PN = Builder.CreatePHI(llvm::Type::Int1Ty, "land"); |
| 1301 | PN->reserveOperandSpace(2); |
| 1302 | PN->addIncoming(llvm::ConstantInt::getFalse(), OrigBlock); |
| 1303 | PN->addIncoming(RHSCond, RHSBlock); |
| 1304 | |
| 1305 | // ZExt result to int. |
| 1306 | return RValue::get(Builder.CreateZExt(PN, LLVMIntTy, "land.ext")); |
| 1307 | } |
| 1308 | |
| 1309 | RValue CodeGenFunction::EmitBinaryLOr(const BinaryOperator *E) { |
| 1310 | llvm::Value *LHSCond = EvaluateExprAsBool(E->getLHS()); |
| 1311 | |
| 1312 | llvm::BasicBlock *ContBlock = new llvm::BasicBlock("lor_cont"); |
| 1313 | llvm::BasicBlock *RHSBlock = new llvm::BasicBlock("lor_rhs"); |
| 1314 | |
| 1315 | llvm::BasicBlock *OrigBlock = Builder.GetInsertBlock(); |
| 1316 | Builder.CreateCondBr(LHSCond, ContBlock, RHSBlock); |
| 1317 | |
| 1318 | EmitBlock(RHSBlock); |
| 1319 | llvm::Value *RHSCond = EvaluateExprAsBool(E->getRHS()); |
| 1320 | |
| 1321 | // Reaquire the RHS block, as there may be subblocks inserted. |
| 1322 | RHSBlock = Builder.GetInsertBlock(); |
| 1323 | EmitBlock(ContBlock); |
| 1324 | |
| 1325 | // Create a PHI node. If we just evaluted the LHS condition, the result is |
| 1326 | // true. If we evaluated both, the result is the RHS condition. |
| 1327 | llvm::PHINode *PN = Builder.CreatePHI(llvm::Type::Int1Ty, "lor"); |
| 1328 | PN->reserveOperandSpace(2); |
| 1329 | PN->addIncoming(llvm::ConstantInt::getTrue(), OrigBlock); |
| 1330 | PN->addIncoming(RHSCond, RHSBlock); |
| 1331 | |
| 1332 | // ZExt result to int. |
| 1333 | return RValue::get(Builder.CreateZExt(PN, LLVMIntTy, "lor.ext")); |
| 1334 | } |
| 1335 | |
| 1336 | RValue CodeGenFunction::EmitBinaryAssign(const BinaryOperator *E) { |
Chris Lattner | 2af72ac | 2007-08-08 17:43:05 +0000 | [diff] [blame] | 1337 | assert(E->getLHS()->getType().getCanonicalType() == |
| 1338 | E->getRHS()->getType().getCanonicalType() && "Invalid assignment"); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1339 | LValue LHS = EmitLValue(E->getLHS()); |
Chris Lattner | 2af72ac | 2007-08-08 17:43:05 +0000 | [diff] [blame] | 1340 | RValue RHS = EmitExpr(E->getRHS()); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1341 | |
| 1342 | // Store the value into the LHS. |
| 1343 | EmitStoreThroughLValue(RHS, LHS, E->getType()); |
Chris Lattner | bdb8ffb | 2007-08-11 00:04:45 +0000 | [diff] [blame] | 1344 | |
| 1345 | // Return the RHS. |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1346 | return RHS; |
| 1347 | } |
| 1348 | |
| 1349 | |
| 1350 | RValue CodeGenFunction::EmitBinaryComma(const BinaryOperator *E) { |
Chris Lattner | c15b0db | 2007-08-21 17:15:50 +0000 | [diff] [blame] | 1351 | EmitStmt(E->getLHS()); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1352 | return EmitExpr(E->getRHS()); |
| 1353 | } |
| 1354 | |
| 1355 | RValue CodeGenFunction::EmitConditionalOperator(const ConditionalOperator *E) { |
| 1356 | llvm::BasicBlock *LHSBlock = new llvm::BasicBlock("cond.?"); |
| 1357 | llvm::BasicBlock *RHSBlock = new llvm::BasicBlock("cond.:"); |
| 1358 | llvm::BasicBlock *ContBlock = new llvm::BasicBlock("cond.cont"); |
| 1359 | |
| 1360 | llvm::Value *Cond = EvaluateExprAsBool(E->getCond()); |
| 1361 | Builder.CreateCondBr(Cond, LHSBlock, RHSBlock); |
| 1362 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1363 | EmitBlock(LHSBlock); |
Chris Lattner | 2af72ac | 2007-08-08 17:43:05 +0000 | [diff] [blame] | 1364 | // Handle the GNU extension for missing LHS. |
| 1365 | llvm::Value *LHSValue = E->getLHS() ? EmitExpr(E->getLHS()).getVal() : Cond; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1366 | Builder.CreateBr(ContBlock); |
| 1367 | LHSBlock = Builder.GetInsertBlock(); |
| 1368 | |
| 1369 | EmitBlock(RHSBlock); |
Chris Lattner | 2af72ac | 2007-08-08 17:43:05 +0000 | [diff] [blame] | 1370 | |
| 1371 | llvm::Value *RHSValue = EmitExpr(E->getRHS()).getVal(); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1372 | Builder.CreateBr(ContBlock); |
| 1373 | RHSBlock = Builder.GetInsertBlock(); |
| 1374 | |
| 1375 | const llvm::Type *LHSType = LHSValue->getType(); |
| 1376 | assert(LHSType == RHSValue->getType() && "?: LHS & RHS must have same type"); |
| 1377 | |
| 1378 | EmitBlock(ContBlock); |
| 1379 | llvm::PHINode *PN = Builder.CreatePHI(LHSType, "cond"); |
| 1380 | PN->reserveOperandSpace(2); |
| 1381 | PN->addIncoming(LHSValue, LHSBlock); |
| 1382 | PN->addIncoming(RHSValue, RHSBlock); |
| 1383 | |
| 1384 | return RValue::get(PN); |
| 1385 | } |