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