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