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