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