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