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" |
| 15 | #include "clang/AST/AST.h" |
| 16 | #include "llvm/Constants.h" |
| 17 | #include "llvm/DerivedTypes.h" |
| 18 | using namespace llvm; |
| 19 | using namespace clang; |
| 20 | using namespace CodeGen; |
| 21 | |
Chris Lattner | d7f5886 | 2007-06-02 05:24:33 +0000 | [diff] [blame] | 22 | //===--------------------------------------------------------------------===// |
Chris Lattner | f0106d2 | 2007-06-02 19:33:17 +0000 | [diff] [blame] | 23 | // Miscellaneous Helper Methods |
| 24 | //===--------------------------------------------------------------------===// |
| 25 | |
Chris Lattner | 8394d79 | 2007-06-05 20:53:16 +0000 | [diff] [blame] | 26 | |
| 27 | /// EvaluateExprAsBool - Perform the usual unary conversions on the specified |
| 28 | /// expression and compare the result against zero, returning an Int1Ty value. |
| 29 | Value *CodeGenFunction::EvaluateExprAsBool(const Expr *E) { |
| 30 | QualType Ty; |
| 31 | RValue Val = EmitExprWithUsualUnaryConversions(E, Ty); |
| 32 | return ConvertScalarValueToBool(Val, Ty); |
| 33 | } |
| 34 | |
| 35 | //===--------------------------------------------------------------------===// |
| 36 | // Conversions |
| 37 | //===--------------------------------------------------------------------===// |
| 38 | |
| 39 | /// EmitConversion - Convert the value specied by Val, whose type is ValTy, to |
| 40 | /// the type specified by DstTy, following the rules of C99 6.3. |
| 41 | RValue CodeGenFunction::EmitConversion(RValue Val, QualType ValTy, |
Chris Lattner | cf106ab | 2007-06-06 04:05:39 +0000 | [diff] [blame^] | 42 | QualType DstTy, SourceLocation Loc) { |
Chris Lattner | 8394d79 | 2007-06-05 20:53:16 +0000 | [diff] [blame] | 43 | ValTy = ValTy.getCanonicalType(); |
| 44 | DstTy = DstTy.getCanonicalType(); |
| 45 | if (ValTy == DstTy) return Val; |
| 46 | |
Chris Lattner | cf106ab | 2007-06-06 04:05:39 +0000 | [diff] [blame^] | 47 | if (isa<PointerType>(DstTy)) { |
| 48 | const llvm::Type *DestTy = ConvertType(DstTy, Loc); |
| 49 | |
| 50 | // The source value may be an integer, or a pointer. |
| 51 | assert(Val.isScalar() && "Can only convert from integer or pointer"); |
| 52 | if (isa<llvm::PointerType>(Val.getVal()->getType())) |
| 53 | return RValue::get(Builder.CreateBitCast(Val.getVal(), DestTy, "conv")); |
| 54 | assert(ValTy->isIntegerType() && "Not ptr->ptr or int->ptr conversion?"); |
| 55 | return RValue::get(Builder.CreatePtrToInt(Val.getVal(), DestTy, "conv")); |
| 56 | } else if (isa<PointerType>(ValTy)) { |
| 57 | // Must be an ptr to int cast. |
| 58 | const llvm::Type *DestTy = ConvertType(DstTy, Loc); |
| 59 | assert(isa<llvm::IntegerType>(DestTy) && "not ptr->int?"); |
| 60 | return RValue::get(Builder.CreateIntToPtr(Val.getVal(), DestTy, "conv")); |
| 61 | } else if (const BuiltinType *DestBT = dyn_cast<BuiltinType>(DstTy)) { |
Chris Lattner | 8394d79 | 2007-06-05 20:53:16 +0000 | [diff] [blame] | 62 | if (DestBT->getKind() == BuiltinType::Bool) |
| 63 | return RValue::get(ConvertScalarValueToBool(Val, ValTy)); |
| 64 | } |
Chris Lattner | 8394d79 | 2007-06-05 20:53:16 +0000 | [diff] [blame] | 65 | assert(0 && "FIXME: Unsupported conversion!"); |
| 66 | } |
| 67 | |
| 68 | |
| 69 | /// ConvertScalarValueToBool - Convert the specified expression value to a |
Chris Lattner | f0106d2 | 2007-06-02 19:33:17 +0000 | [diff] [blame] | 70 | /// boolean (i1) truth value. This is equivalent to "Val == 0". |
Chris Lattner | 8394d79 | 2007-06-05 20:53:16 +0000 | [diff] [blame] | 71 | Value *CodeGenFunction::ConvertScalarValueToBool(RValue Val, QualType Ty) { |
Chris Lattner | f0106d2 | 2007-06-02 19:33:17 +0000 | [diff] [blame] | 72 | Ty = Ty.getCanonicalType(); |
| 73 | Value *Result; |
| 74 | if (const BuiltinType *BT = dyn_cast<BuiltinType>(Ty)) { |
| 75 | switch (BT->getKind()) { |
| 76 | default: assert(0 && "Unknown scalar value"); |
| 77 | case BuiltinType::Bool: |
| 78 | Result = Val.getVal(); |
| 79 | // Bool is already evaluated right. |
| 80 | assert(Result->getType() == llvm::Type::Int1Ty && |
| 81 | "Unexpected bool value type!"); |
| 82 | return Result; |
Chris Lattner | b16f455 | 2007-06-03 07:25:34 +0000 | [diff] [blame] | 83 | case BuiltinType::Char_S: |
| 84 | case BuiltinType::Char_U: |
Chris Lattner | f0106d2 | 2007-06-02 19:33:17 +0000 | [diff] [blame] | 85 | case BuiltinType::SChar: |
| 86 | case BuiltinType::UChar: |
| 87 | case BuiltinType::Short: |
| 88 | case BuiltinType::UShort: |
| 89 | case BuiltinType::Int: |
| 90 | case BuiltinType::UInt: |
| 91 | case BuiltinType::Long: |
| 92 | case BuiltinType::ULong: |
| 93 | case BuiltinType::LongLong: |
| 94 | case BuiltinType::ULongLong: |
| 95 | // Code below handles simple integers. |
| 96 | break; |
| 97 | case BuiltinType::Float: |
| 98 | case BuiltinType::Double: |
| 99 | case BuiltinType::LongDouble: { |
| 100 | // Compare against 0.0 for fp scalars. |
| 101 | Result = Val.getVal(); |
| 102 | llvm::Value *Zero = Constant::getNullValue(Result->getType()); |
| 103 | // FIXME: llvm-gcc produces a une comparison: validate this is right. |
| 104 | Result = Builder.CreateFCmpUNE(Result, Zero, "tobool"); |
| 105 | return Result; |
| 106 | } |
| 107 | |
| 108 | case BuiltinType::FloatComplex: |
| 109 | case BuiltinType::DoubleComplex: |
| 110 | case BuiltinType::LongDoubleComplex: |
| 111 | assert(0 && "comparisons against complex not implemented yet"); |
| 112 | } |
| 113 | } else { |
| 114 | assert((isa<PointerType>(Ty) || |
| 115 | cast<TagType>(Ty)->getDecl()->getKind() == Decl::Enum) && |
| 116 | "Unknown scalar type"); |
| 117 | // Code below handles this fine. |
| 118 | } |
| 119 | |
| 120 | // Usual case for integers, pointers, and enums: compare against zero. |
| 121 | Result = Val.getVal(); |
Chris Lattner | a45c5af | 2007-06-02 19:47:04 +0000 | [diff] [blame] | 122 | |
| 123 | // Because of the type rules of C, we often end up computing a logical value, |
| 124 | // then zero extending it to int, then wanting it as a logical value again. |
| 125 | // Optimize this common case. |
| 126 | if (llvm::ZExtInst *ZI = dyn_cast<ZExtInst>(Result)) { |
| 127 | if (ZI->getOperand(0)->getType() == llvm::Type::Int1Ty) { |
| 128 | Result = ZI->getOperand(0); |
| 129 | ZI->eraseFromParent(); |
| 130 | return Result; |
| 131 | } |
| 132 | } |
| 133 | |
Chris Lattner | f0106d2 | 2007-06-02 19:33:17 +0000 | [diff] [blame] | 134 | llvm::Value *Zero = Constant::getNullValue(Result->getType()); |
| 135 | return Builder.CreateICmpNE(Result, Zero, "tobool"); |
| 136 | } |
| 137 | |
Chris Lattner | a45c5af | 2007-06-02 19:47:04 +0000 | [diff] [blame] | 138 | //===----------------------------------------------------------------------===// |
Chris Lattner | d7f5886 | 2007-06-02 05:24:33 +0000 | [diff] [blame] | 139 | // LValue Expression Emission |
Chris Lattner | a45c5af | 2007-06-02 19:47:04 +0000 | [diff] [blame] | 140 | //===----------------------------------------------------------------------===// |
Chris Lattner | d7f5886 | 2007-06-02 05:24:33 +0000 | [diff] [blame] | 141 | |
Chris Lattner | 8394d79 | 2007-06-05 20:53:16 +0000 | [diff] [blame] | 142 | /// EmitLValue - Emit code to compute a designator that specifies the location |
| 143 | /// of the expression. |
| 144 | /// |
| 145 | /// This can return one of two things: a simple address or a bitfield |
| 146 | /// reference. In either case, the LLVM Value* in the LValue structure is |
| 147 | /// guaranteed to be an LLVM pointer type. |
| 148 | /// |
| 149 | /// If this returns a bitfield reference, nothing about the pointee type of |
| 150 | /// the LLVM value is known: For example, it may not be a pointer to an |
| 151 | /// integer. |
| 152 | /// |
| 153 | /// If this returns a normal address, and if the lvalue's C type is fixed |
| 154 | /// size, this method guarantees that the returned pointer type will point to |
| 155 | /// an LLVM type of the same size of the lvalue's type. If the lvalue has a |
| 156 | /// variable length type, this is not possible. |
| 157 | /// |
Chris Lattner | d7f5886 | 2007-06-02 05:24:33 +0000 | [diff] [blame] | 158 | LValue CodeGenFunction::EmitLValue(const Expr *E) { |
| 159 | switch (E->getStmtClass()) { |
| 160 | default: |
Chris Lattner | 8394d79 | 2007-06-05 20:53:16 +0000 | [diff] [blame] | 161 | fprintf(stderr, "Unimplemented lvalue expr!\n"); |
Chris Lattner | d7f5886 | 2007-06-02 05:24:33 +0000 | [diff] [blame] | 162 | E->dump(); |
| 163 | return LValue::getAddr(UndefValue::get( |
| 164 | llvm::PointerType::get(llvm::Type::Int32Ty))); |
| 165 | |
| 166 | case Expr::DeclRefExprClass: return EmitDeclRefLValue(cast<DeclRefExpr>(E)); |
Chris Lattner | 946aa31 | 2007-06-05 03:59:43 +0000 | [diff] [blame] | 167 | case Expr::ParenExprClass:return EmitLValue(cast<ParenExpr>(E)->getSubExpr()); |
Chris Lattner | 8394d79 | 2007-06-05 20:53:16 +0000 | [diff] [blame] | 168 | |
| 169 | |
| 170 | case Expr::UnaryOperatorClass: |
| 171 | return EmitUnaryOpLValue(cast<UnaryOperator>(E)); |
Chris Lattner | d7f5886 | 2007-06-02 05:24:33 +0000 | [diff] [blame] | 172 | } |
| 173 | } |
| 174 | |
Chris Lattner | 8394d79 | 2007-06-05 20:53:16 +0000 | [diff] [blame] | 175 | /// EmitLoadOfLValue - Given an expression that represents a value lvalue, |
| 176 | /// this method emits the address of the lvalue, then loads the result as an |
| 177 | /// rvalue, returning the rvalue. |
| 178 | RValue CodeGenFunction::EmitLoadOfLValue(const Expr *E) { |
| 179 | LValue LV = EmitLValue(E); |
| 180 | |
| 181 | QualType ExprTy = E->getType().getCanonicalType(); |
| 182 | |
| 183 | // FIXME: this is silly and obviously wrong for non-scalars. |
| 184 | assert(!LV.isBitfield()); |
| 185 | return RValue::get(Builder.CreateLoad(LV.getAddress(), "tmp")); |
| 186 | } |
| 187 | |
| 188 | /// EmitStoreThroughLValue - Store the specified rvalue into the specified |
| 189 | /// lvalue, where both are guaranteed to the have the same type, and that type |
| 190 | /// is 'Ty'. |
| 191 | void CodeGenFunction::EmitStoreThroughLValue(RValue Src, LValue Dst, |
| 192 | QualType Ty) { |
| 193 | // FIXME: This is obviously bogus. |
| 194 | assert(!Dst.isBitfield() && "FIXME: Don't support store to bitfield yet"); |
| 195 | assert(Src.isScalar() && "FIXME: Don't support store of aggregate yet"); |
| 196 | |
| 197 | // TODO: Handle volatility etc. |
| 198 | Value *Addr = Dst.getAddress(); |
| 199 | const llvm::Type *SrcTy = Src.getVal()->getType(); |
| 200 | const llvm::Type *AddrTy = |
| 201 | cast<llvm::PointerType>(Addr->getType())->getElementType(); |
| 202 | |
| 203 | if (AddrTy != SrcTy) |
| 204 | Addr = Builder.CreateBitCast(Addr, llvm::PointerType::get(SrcTy), |
| 205 | "storetmp"); |
| 206 | Builder.CreateStore(Src.getVal(), Addr); |
| 207 | } |
| 208 | |
Chris Lattner | d7f5886 | 2007-06-02 05:24:33 +0000 | [diff] [blame] | 209 | |
| 210 | LValue CodeGenFunction::EmitDeclRefLValue(const DeclRefExpr *E) { |
| 211 | const Decl *D = E->getDecl(); |
| 212 | if (isa<BlockVarDecl>(D)) { |
| 213 | Value *V = LocalDeclMap[D]; |
| 214 | assert(V && "BlockVarDecl not entered in LocalDeclMap?"); |
| 215 | return LValue::getAddr(V); |
| 216 | } |
| 217 | assert(0 && "Unimp declref"); |
| 218 | } |
Chris Lattner | e47e440 | 2007-06-01 18:02:12 +0000 | [diff] [blame] | 219 | |
Chris Lattner | 8394d79 | 2007-06-05 20:53:16 +0000 | [diff] [blame] | 220 | LValue CodeGenFunction::EmitUnaryOpLValue(const UnaryOperator *E) { |
| 221 | // __extension__ doesn't affect lvalue-ness. |
| 222 | if (E->getOpcode() == UnaryOperator::Extension) |
| 223 | return EmitLValue(E->getSubExpr()); |
| 224 | |
| 225 | assert(E->getOpcode() == UnaryOperator::Deref && |
| 226 | "'*' is the only unary operator that produces an lvalue"); |
| 227 | return LValue::getAddr(EmitExpr(E->getSubExpr()).getVal()); |
| 228 | } |
| 229 | |
Chris Lattner | e47e440 | 2007-06-01 18:02:12 +0000 | [diff] [blame] | 230 | //===--------------------------------------------------------------------===// |
| 231 | // Expression Emission |
| 232 | //===--------------------------------------------------------------------===// |
| 233 | |
Chris Lattner | 8394d79 | 2007-06-05 20:53:16 +0000 | [diff] [blame] | 234 | RValue CodeGenFunction::EmitExpr(const Expr *E) { |
Chris Lattner | e47e440 | 2007-06-01 18:02:12 +0000 | [diff] [blame] | 235 | assert(E && "Null expression?"); |
| 236 | |
| 237 | switch (E->getStmtClass()) { |
| 238 | default: |
| 239 | printf("Unimplemented expr!\n"); |
| 240 | E->dump(); |
Chris Lattner | 8394d79 | 2007-06-05 20:53:16 +0000 | [diff] [blame] | 241 | return RValue::get(UndefValue::get(llvm::Type::Int32Ty)); |
Chris Lattner | d7f5886 | 2007-06-02 05:24:33 +0000 | [diff] [blame] | 242 | |
| 243 | // l-values. |
Chris Lattner | 8394d79 | 2007-06-05 20:53:16 +0000 | [diff] [blame] | 244 | case Expr::DeclRefExprClass: |
| 245 | // FIXME: EnumConstantDecl's are not lvalues. This is wrong for them. |
| 246 | return EmitLoadOfLValue(E); |
Chris Lattner | d7f5886 | 2007-06-02 05:24:33 +0000 | [diff] [blame] | 247 | |
| 248 | // Leaf expressions. |
| 249 | case Expr::IntegerLiteralClass: |
Chris Lattner | e47e440 | 2007-06-01 18:02:12 +0000 | [diff] [blame] | 250 | return EmitIntegerLiteral(cast<IntegerLiteral>(E)); |
Chris Lattner | db91b16 | 2007-06-02 00:16:28 +0000 | [diff] [blame] | 251 | |
Chris Lattner | d7f5886 | 2007-06-02 05:24:33 +0000 | [diff] [blame] | 252 | // Operators. |
| 253 | case Expr::ParenExprClass: |
| 254 | return EmitExpr(cast<ParenExpr>(E)->getSubExpr()); |
Chris Lattner | f0106d2 | 2007-06-02 19:33:17 +0000 | [diff] [blame] | 255 | case Expr::UnaryOperatorClass: |
| 256 | return EmitUnaryOperator(cast<UnaryOperator>(E)); |
Chris Lattner | 8394d79 | 2007-06-05 20:53:16 +0000 | [diff] [blame] | 257 | case Expr::CastExprClass: |
| 258 | return EmitCastExpr(cast<CastExpr>(E)); |
Chris Lattner | d7f5886 | 2007-06-02 05:24:33 +0000 | [diff] [blame] | 259 | case Expr::BinaryOperatorClass: |
Chris Lattner | db91b16 | 2007-06-02 00:16:28 +0000 | [diff] [blame] | 260 | return EmitBinaryOperator(cast<BinaryOperator>(E)); |
Chris Lattner | e47e440 | 2007-06-01 18:02:12 +0000 | [diff] [blame] | 261 | } |
| 262 | |
| 263 | } |
| 264 | |
Chris Lattner | 8394d79 | 2007-06-05 20:53:16 +0000 | [diff] [blame] | 265 | RValue CodeGenFunction::EmitIntegerLiteral(const IntegerLiteral *E) { |
| 266 | return RValue::get(ConstantInt::get(E->getValue())); |
Chris Lattner | e47e440 | 2007-06-01 18:02:12 +0000 | [diff] [blame] | 267 | } |
| 268 | |
Chris Lattner | 8394d79 | 2007-06-05 20:53:16 +0000 | [diff] [blame] | 269 | RValue CodeGenFunction::EmitCastExpr(const CastExpr *E) { |
| 270 | QualType SrcTy; |
| 271 | RValue Src = EmitExprWithUsualUnaryConversions(E->getSubExpr(), SrcTy); |
| 272 | |
| 273 | // If the destination is void, just evaluate the source. |
| 274 | if (E->getType()->isVoidType()) |
| 275 | return RValue::getAggregate(0); |
| 276 | |
Chris Lattner | cf106ab | 2007-06-06 04:05:39 +0000 | [diff] [blame^] | 277 | return EmitConversion(Src, SrcTy, E->getType(), E->getLParenLoc()); |
Chris Lattner | 8394d79 | 2007-06-05 20:53:16 +0000 | [diff] [blame] | 278 | } |
Chris Lattner | f0106d2 | 2007-06-02 19:33:17 +0000 | [diff] [blame] | 279 | |
Chris Lattner | 8394d79 | 2007-06-05 20:53:16 +0000 | [diff] [blame] | 280 | //===----------------------------------------------------------------------===// |
| 281 | // Unary Operator Emission |
| 282 | //===----------------------------------------------------------------------===// |
| 283 | |
| 284 | RValue CodeGenFunction::EmitExprWithUsualUnaryConversions(const Expr *E, |
| 285 | QualType &ResTy) { |
Chris Lattner | 6db1fb8 | 2007-06-02 22:49:07 +0000 | [diff] [blame] | 286 | ResTy = E->getType().getCanonicalType(); |
| 287 | |
| 288 | if (isa<FunctionType>(ResTy)) { // C99 6.3.2.1p4 |
| 289 | // Functions are promoted to their address. |
| 290 | ResTy = getContext().getPointerType(ResTy); |
Chris Lattner | 8394d79 | 2007-06-05 20:53:16 +0000 | [diff] [blame] | 291 | return RValue::get(EmitLValue(E).getAddress()); |
Chris Lattner | 6db1fb8 | 2007-06-02 22:49:07 +0000 | [diff] [blame] | 292 | } else if (const ArrayType *ary = dyn_cast<ArrayType>(ResTy)) { |
| 293 | // C99 6.3.2.1p3 |
| 294 | ResTy = getContext().getPointerType(ary->getElementType()); |
| 295 | |
| 296 | // FIXME: For now we assume that all source arrays map to LLVM arrays. This |
| 297 | // will not true when we add support for VLAs. |
| 298 | llvm::Value *V = EmitLValue(E).getAddress(); // Bitfields can't be arrays. |
| 299 | |
| 300 | assert(isa<llvm::PointerType>(V->getType()) && |
| 301 | isa<llvm::ArrayType>(cast<llvm::PointerType>(V->getType()) |
| 302 | ->getElementType()) && |
| 303 | "Doesn't support VLAs yet!"); |
| 304 | llvm::Constant *Idx0 = llvm::ConstantInt::get(llvm::Type::Int32Ty, 0); |
Chris Lattner | 8394d79 | 2007-06-05 20:53:16 +0000 | [diff] [blame] | 305 | return RValue::get(Builder.CreateGEP(V, Idx0, Idx0, "arraydecay")); |
Chris Lattner | 6db1fb8 | 2007-06-02 22:49:07 +0000 | [diff] [blame] | 306 | } else if (ResTy->isPromotableIntegerType()) { // C99 6.3.1.1p2 |
| 307 | // FIXME: this probably isn't right, pending clarification from Steve. |
| 308 | llvm::Value *Val = EmitExpr(E).getVal(); |
| 309 | |
Chris Lattner | 6db1fb8 | 2007-06-02 22:49:07 +0000 | [diff] [blame] | 310 | // If the input is a signed integer, sign extend to the destination. |
| 311 | if (ResTy->isSignedIntegerType()) { |
| 312 | Val = Builder.CreateSExt(Val, LLVMIntTy, "promote"); |
| 313 | } else { |
| 314 | // This handles unsigned types, including bool. |
| 315 | Val = Builder.CreateZExt(Val, LLVMIntTy, "promote"); |
| 316 | } |
| 317 | ResTy = getContext().IntTy; |
| 318 | |
Chris Lattner | 8394d79 | 2007-06-05 20:53:16 +0000 | [diff] [blame] | 319 | return RValue::get(Val); |
Chris Lattner | 6db1fb8 | 2007-06-02 22:49:07 +0000 | [diff] [blame] | 320 | } |
| 321 | |
| 322 | // Otherwise, this is a float, double, int, struct, etc. |
| 323 | return EmitExpr(E); |
| 324 | } |
| 325 | |
| 326 | |
Chris Lattner | 8394d79 | 2007-06-05 20:53:16 +0000 | [diff] [blame] | 327 | RValue CodeGenFunction::EmitUnaryOperator(const UnaryOperator *E) { |
Chris Lattner | f0106d2 | 2007-06-02 19:33:17 +0000 | [diff] [blame] | 328 | switch (E->getOpcode()) { |
| 329 | default: |
| 330 | printf("Unimplemented unary expr!\n"); |
| 331 | E->dump(); |
Chris Lattner | 8394d79 | 2007-06-05 20:53:16 +0000 | [diff] [blame] | 332 | return RValue::get(UndefValue::get(llvm::Type::Int32Ty)); |
| 333 | // FIXME: pre/post inc/dec |
| 334 | case UnaryOperator::AddrOf: return EmitUnaryAddrOf(E); |
| 335 | case UnaryOperator::Deref : return EmitLoadOfLValue(E); |
| 336 | case UnaryOperator::Plus : return EmitUnaryPlus(E); |
| 337 | case UnaryOperator::Minus : return EmitUnaryMinus(E); |
| 338 | case UnaryOperator::Not : return EmitUnaryNot(E); |
| 339 | case UnaryOperator::LNot : return EmitUnaryLNot(E); |
| 340 | // FIXME: SIZEOF/ALIGNOF(expr). |
| 341 | // FIXME: real/imag |
| 342 | case UnaryOperator::Extension: return EmitExpr(E->getSubExpr()); |
Chris Lattner | f0106d2 | 2007-06-02 19:33:17 +0000 | [diff] [blame] | 343 | } |
| 344 | } |
| 345 | |
Chris Lattner | 8394d79 | 2007-06-05 20:53:16 +0000 | [diff] [blame] | 346 | /// C99 6.5.3.2 |
| 347 | RValue CodeGenFunction::EmitUnaryAddrOf(const UnaryOperator *E) { |
| 348 | // The address of the operand is just its lvalue. It cannot be a bitfield. |
| 349 | return RValue::get(EmitLValue(E->getSubExpr()).getAddress()); |
| 350 | } |
| 351 | |
| 352 | RValue CodeGenFunction::EmitUnaryPlus(const UnaryOperator *E) { |
| 353 | // Unary plus just performs promotions on its arithmetic operand. |
| 354 | QualType Ty; |
| 355 | return EmitExprWithUsualUnaryConversions(E, Ty); |
| 356 | } |
| 357 | |
| 358 | RValue CodeGenFunction::EmitUnaryMinus(const UnaryOperator *E) { |
| 359 | // Unary minus performs promotions, then negates its arithmetic operand. |
| 360 | QualType Ty; |
| 361 | RValue V = EmitExprWithUsualUnaryConversions(E, Ty); |
Chris Lattner | f0106d2 | 2007-06-02 19:33:17 +0000 | [diff] [blame] | 362 | |
Chris Lattner | 8394d79 | 2007-06-05 20:53:16 +0000 | [diff] [blame] | 363 | if (V.isScalar()) |
| 364 | return RValue::get(Builder.CreateNeg(V.getVal(), "neg")); |
| 365 | |
| 366 | assert(0 && "FIXME: This doesn't handle complex operands yet"); |
| 367 | } |
| 368 | |
| 369 | RValue CodeGenFunction::EmitUnaryNot(const UnaryOperator *E) { |
| 370 | // Unary not performs promotions, then complements its integer operand. |
| 371 | QualType Ty; |
| 372 | RValue V = EmitExprWithUsualUnaryConversions(E, Ty); |
| 373 | |
| 374 | if (V.isScalar()) |
| 375 | return RValue::get(Builder.CreateNot(V.getVal(), "neg")); |
| 376 | |
| 377 | assert(0 && "FIXME: This doesn't handle integer complex operands yet (GNU)"); |
| 378 | } |
| 379 | |
| 380 | |
| 381 | /// C99 6.5.3.3 |
| 382 | RValue CodeGenFunction::EmitUnaryLNot(const UnaryOperator *E) { |
| 383 | // Compare operand to zero. |
| 384 | Value *BoolVal = EvaluateExprAsBool(E->getSubExpr()); |
Chris Lattner | f0106d2 | 2007-06-02 19:33:17 +0000 | [diff] [blame] | 385 | |
| 386 | // Invert value. |
Chris Lattner | a45c5af | 2007-06-02 19:47:04 +0000 | [diff] [blame] | 387 | // TODO: Could dynamically modify easy computations here. For example, if |
| 388 | // the operand is an icmp ne, turn into icmp eq. |
Chris Lattner | f0106d2 | 2007-06-02 19:33:17 +0000 | [diff] [blame] | 389 | BoolVal = Builder.CreateNot(BoolVal, "lnot"); |
| 390 | |
| 391 | // ZExt result to int. |
Chris Lattner | 8394d79 | 2007-06-05 20:53:16 +0000 | [diff] [blame] | 392 | return RValue::get(Builder.CreateZExt(BoolVal, LLVMIntTy, "lnot.ext")); |
Chris Lattner | f0106d2 | 2007-06-02 19:33:17 +0000 | [diff] [blame] | 393 | } |
| 394 | |
Chris Lattner | e47e440 | 2007-06-01 18:02:12 +0000 | [diff] [blame] | 395 | |
Chris Lattner | db91b16 | 2007-06-02 00:16:28 +0000 | [diff] [blame] | 396 | //===--------------------------------------------------------------------===// |
| 397 | // Binary Operator Emission |
| 398 | //===--------------------------------------------------------------------===// |
| 399 | |
| 400 | // FIXME describe. |
Chris Lattner | cf25024 | 2007-06-03 02:02:44 +0000 | [diff] [blame] | 401 | QualType CodeGenFunction:: |
Chris Lattner | 8394d79 | 2007-06-05 20:53:16 +0000 | [diff] [blame] | 402 | EmitUsualArithmeticConversions(const BinaryOperator *E, RValue &LHS, |
| 403 | RValue &RHS) { |
Chris Lattner | c18f9d1 | 2007-06-02 22:51:30 +0000 | [diff] [blame] | 404 | QualType LHSType, RHSType; |
| 405 | LHS = EmitExprWithUsualUnaryConversions(E->getLHS(), LHSType); |
| 406 | RHS = EmitExprWithUsualUnaryConversions(E->getRHS(), RHSType); |
| 407 | |
Chris Lattner | cf25024 | 2007-06-03 02:02:44 +0000 | [diff] [blame] | 408 | // If both operands have the same source type, we're done already. |
| 409 | if (LHSType == RHSType) return LHSType; |
| 410 | |
| 411 | // If either side is a non-arithmetic type (e.g. a pointer), we are done. |
| 412 | // The caller can deal with this (e.g. pointer + int). |
| 413 | if (!LHSType->isArithmeticType() || !RHSType->isArithmeticType()) |
| 414 | return LHSType; |
| 415 | |
| 416 | // At this point, we have two different arithmetic types. |
| 417 | |
| 418 | // Handle complex types first (C99 6.3.1.8p1). |
| 419 | if (LHSType->isComplexType() || RHSType->isComplexType()) { |
| 420 | assert(0 && "FIXME: complex types unimp"); |
| 421 | #if 0 |
| 422 | // if we have an integer operand, the result is the complex type. |
| 423 | if (rhs->isIntegerType()) |
| 424 | return lhs; |
| 425 | if (lhs->isIntegerType()) |
| 426 | return rhs; |
| 427 | return Context.maxComplexType(lhs, rhs); |
| 428 | #endif |
| 429 | } |
| 430 | |
| 431 | // If neither operand is complex, they must be scalars. |
| 432 | llvm::Value *LHSV = LHS.getVal(); |
| 433 | llvm::Value *RHSV = RHS.getVal(); |
| 434 | |
| 435 | // If the LLVM types are already equal, then they only differed in sign, or it |
| 436 | // was something like char/signed char or double/long double. |
| 437 | if (LHSV->getType() == RHSV->getType()) |
| 438 | return LHSType; |
| 439 | |
| 440 | // Now handle "real" floating types (i.e. float, double, long double). |
| 441 | if (LHSType->isRealFloatingType() || RHSType->isRealFloatingType()) { |
| 442 | // if we have an integer operand, the result is the real floating type, and |
| 443 | // the integer converts to FP. |
| 444 | if (RHSType->isIntegerType()) { |
| 445 | // Promote the RHS to an FP type of the LHS, with the sign following the |
| 446 | // RHS. |
| 447 | if (RHSType->isSignedIntegerType()) |
Chris Lattner | 8394d79 | 2007-06-05 20:53:16 +0000 | [diff] [blame] | 448 | RHS = RValue::get(Builder.CreateSIToFP(RHSV,LHSV->getType(),"promote")); |
Chris Lattner | cf25024 | 2007-06-03 02:02:44 +0000 | [diff] [blame] | 449 | else |
Chris Lattner | 8394d79 | 2007-06-05 20:53:16 +0000 | [diff] [blame] | 450 | RHS = RValue::get(Builder.CreateUIToFP(RHSV,LHSV->getType(),"promote")); |
Chris Lattner | cf25024 | 2007-06-03 02:02:44 +0000 | [diff] [blame] | 451 | return LHSType; |
| 452 | } |
| 453 | |
| 454 | if (LHSType->isIntegerType()) { |
| 455 | // Promote the LHS to an FP type of the RHS, with the sign following the |
| 456 | // LHS. |
| 457 | if (LHSType->isSignedIntegerType()) |
Chris Lattner | 8394d79 | 2007-06-05 20:53:16 +0000 | [diff] [blame] | 458 | LHS = RValue::get(Builder.CreateSIToFP(LHSV,RHSV->getType(),"promote")); |
Chris Lattner | cf25024 | 2007-06-03 02:02:44 +0000 | [diff] [blame] | 459 | else |
Chris Lattner | 8394d79 | 2007-06-05 20:53:16 +0000 | [diff] [blame] | 460 | LHS = RValue::get(Builder.CreateUIToFP(LHSV,RHSV->getType(),"promote")); |
Chris Lattner | cf25024 | 2007-06-03 02:02:44 +0000 | [diff] [blame] | 461 | return RHSType; |
| 462 | } |
| 463 | |
| 464 | // Otherwise, they are two FP types. Promote the smaller operand to the |
| 465 | // bigger result. |
| 466 | QualType BiggerType = ASTContext::maxFloatingType(LHSType, RHSType); |
| 467 | |
| 468 | if (BiggerType == LHSType) |
Chris Lattner | 8394d79 | 2007-06-05 20:53:16 +0000 | [diff] [blame] | 469 | RHS = RValue::get(Builder.CreateFPExt(RHSV, LHSV->getType(), "promote")); |
Chris Lattner | cf25024 | 2007-06-03 02:02:44 +0000 | [diff] [blame] | 470 | else |
Chris Lattner | 8394d79 | 2007-06-05 20:53:16 +0000 | [diff] [blame] | 471 | LHS = RValue::get(Builder.CreateFPExt(LHSV, RHSV->getType(), "promote")); |
Chris Lattner | cf25024 | 2007-06-03 02:02:44 +0000 | [diff] [blame] | 472 | return BiggerType; |
| 473 | } |
| 474 | |
| 475 | // Finally, we have two integer types that are different according to C. Do |
| 476 | // a sign or zero extension if needed. |
| 477 | |
| 478 | // Otherwise, one type is smaller than the other. |
| 479 | QualType ResTy = ASTContext::maxIntegerType(LHSType, RHSType); |
| 480 | |
| 481 | if (LHSType == ResTy) { |
| 482 | if (RHSType->isSignedIntegerType()) |
Chris Lattner | 8394d79 | 2007-06-05 20:53:16 +0000 | [diff] [blame] | 483 | RHS = RValue::get(Builder.CreateSExt(RHSV, LHSV->getType(), "promote")); |
Chris Lattner | cf25024 | 2007-06-03 02:02:44 +0000 | [diff] [blame] | 484 | else |
Chris Lattner | 8394d79 | 2007-06-05 20:53:16 +0000 | [diff] [blame] | 485 | RHS = RValue::get(Builder.CreateZExt(RHSV, LHSV->getType(), "promote")); |
Chris Lattner | cf25024 | 2007-06-03 02:02:44 +0000 | [diff] [blame] | 486 | } else { |
| 487 | assert(RHSType == ResTy && "Unknown conversion"); |
| 488 | if (LHSType->isSignedIntegerType()) |
Chris Lattner | 8394d79 | 2007-06-05 20:53:16 +0000 | [diff] [blame] | 489 | LHS = RValue::get(Builder.CreateSExt(LHSV, RHSV->getType(), "promote")); |
Chris Lattner | cf25024 | 2007-06-03 02:02:44 +0000 | [diff] [blame] | 490 | else |
Chris Lattner | 8394d79 | 2007-06-05 20:53:16 +0000 | [diff] [blame] | 491 | LHS = RValue::get(Builder.CreateZExt(LHSV, RHSV->getType(), "promote")); |
Chris Lattner | cf25024 | 2007-06-03 02:02:44 +0000 | [diff] [blame] | 492 | } |
| 493 | return ResTy; |
Chris Lattner | db91b16 | 2007-06-02 00:16:28 +0000 | [diff] [blame] | 494 | } |
| 495 | |
| 496 | |
Chris Lattner | 8394d79 | 2007-06-05 20:53:16 +0000 | [diff] [blame] | 497 | RValue CodeGenFunction::EmitBinaryOperator(const BinaryOperator *E) { |
Chris Lattner | db91b16 | 2007-06-02 00:16:28 +0000 | [diff] [blame] | 498 | switch (E->getOpcode()) { |
| 499 | default: |
Chris Lattner | 8394d79 | 2007-06-05 20:53:16 +0000 | [diff] [blame] | 500 | fprintf(stderr, "Unimplemented expr!\n"); |
Chris Lattner | db91b16 | 2007-06-02 00:16:28 +0000 | [diff] [blame] | 501 | E->dump(); |
Chris Lattner | 8394d79 | 2007-06-05 20:53:16 +0000 | [diff] [blame] | 502 | return RValue::get(UndefValue::get(llvm::Type::Int32Ty)); |
| 503 | case BinaryOperator::Mul: return EmitBinaryMul(E); |
| 504 | case BinaryOperator::Div: return EmitBinaryDiv(E); |
| 505 | case BinaryOperator::Rem: return EmitBinaryRem(E); |
Chris Lattner | db91b16 | 2007-06-02 00:16:28 +0000 | [diff] [blame] | 506 | case BinaryOperator::Add: return EmitBinaryAdd(E); |
Chris Lattner | 8394d79 | 2007-06-05 20:53:16 +0000 | [diff] [blame] | 507 | case BinaryOperator::Sub: return EmitBinarySub(E); |
| 508 | case BinaryOperator::Shl: return EmitBinaryShl(E); |
| 509 | case BinaryOperator::Shr: return EmitBinaryShr(E); |
| 510 | |
| 511 | // FIXME: relational |
| 512 | |
| 513 | case BinaryOperator::And: return EmitBinaryAnd(E); |
| 514 | case BinaryOperator::Xor: return EmitBinaryXor(E); |
| 515 | case BinaryOperator::Or : return EmitBinaryOr(E); |
| 516 | case BinaryOperator::LAnd: return EmitBinaryLAnd(E); |
| 517 | case BinaryOperator::LOr: return EmitBinaryLOr(E); |
| 518 | |
| 519 | case BinaryOperator::Assign: return EmitBinaryAssign(E); |
| 520 | // FIXME: Assignment. |
| 521 | case BinaryOperator::Comma: return EmitBinaryComma(E); |
Chris Lattner | db91b16 | 2007-06-02 00:16:28 +0000 | [diff] [blame] | 522 | } |
| 523 | } |
| 524 | |
Chris Lattner | 8394d79 | 2007-06-05 20:53:16 +0000 | [diff] [blame] | 525 | RValue CodeGenFunction::EmitBinaryMul(const BinaryOperator *E) { |
| 526 | RValue LHS, RHS; |
| 527 | EmitUsualArithmeticConversions(E, LHS, RHS); |
Chris Lattner | db91b16 | 2007-06-02 00:16:28 +0000 | [diff] [blame] | 528 | |
Chris Lattner | 8394d79 | 2007-06-05 20:53:16 +0000 | [diff] [blame] | 529 | if (LHS.isScalar()) |
| 530 | return RValue::get(Builder.CreateMul(LHS.getVal(), RHS.getVal(), "mul")); |
| 531 | |
| 532 | assert(0 && "FIXME: This doesn't handle complex operands yet"); |
| 533 | } |
| 534 | |
| 535 | RValue CodeGenFunction::EmitBinaryDiv(const BinaryOperator *E) { |
| 536 | RValue LHS, RHS; |
| 537 | EmitUsualArithmeticConversions(E, LHS, RHS); |
| 538 | |
| 539 | if (LHS.isScalar()) { |
| 540 | Value *RV; |
| 541 | if (LHS.getVal()->getType()->isFloatingPoint()) |
| 542 | RV = Builder.CreateFDiv(LHS.getVal(), RHS.getVal(), "div"); |
| 543 | else if (E->getType()->isUnsignedIntegerType()) |
| 544 | RV = Builder.CreateUDiv(LHS.getVal(), RHS.getVal(), "div"); |
| 545 | else |
| 546 | RV = Builder.CreateSDiv(LHS.getVal(), RHS.getVal(), "div"); |
| 547 | return RValue::get(RV); |
| 548 | } |
| 549 | assert(0 && "FIXME: This doesn't handle complex operands yet"); |
| 550 | } |
| 551 | |
| 552 | RValue CodeGenFunction::EmitBinaryRem(const BinaryOperator *E) { |
| 553 | RValue LHS, RHS; |
| 554 | EmitUsualArithmeticConversions(E, LHS, RHS); |
| 555 | |
| 556 | if (LHS.isScalar()) { |
| 557 | Value *RV; |
| 558 | // Rem in C can't be a floating point type: C99 6.5.5p2. |
| 559 | if (E->getType()->isUnsignedIntegerType()) |
| 560 | RV = Builder.CreateURem(LHS.getVal(), RHS.getVal(), "rem"); |
| 561 | else |
| 562 | RV = Builder.CreateSRem(LHS.getVal(), RHS.getVal(), "rem"); |
| 563 | return RValue::get(RV); |
| 564 | } |
| 565 | |
| 566 | assert(0 && "FIXME: This doesn't handle complex operands yet"); |
| 567 | } |
| 568 | |
| 569 | RValue CodeGenFunction::EmitBinaryAdd(const BinaryOperator *E) { |
| 570 | RValue LHS, RHS; |
Chris Lattner | db91b16 | 2007-06-02 00:16:28 +0000 | [diff] [blame] | 571 | EmitUsualArithmeticConversions(E, LHS, RHS); |
| 572 | |
Chris Lattner | 8394d79 | 2007-06-05 20:53:16 +0000 | [diff] [blame] | 573 | // FIXME: This doesn't handle ptr+int etc yet. |
| 574 | |
| 575 | if (LHS.isScalar()) |
| 576 | return RValue::get(Builder.CreateAdd(LHS.getVal(), RHS.getVal(), "add")); |
| 577 | |
| 578 | assert(0 && "FIXME: This doesn't handle complex operands yet"); |
| 579 | |
| 580 | } |
| 581 | |
| 582 | RValue CodeGenFunction::EmitBinarySub(const BinaryOperator *E) { |
| 583 | RValue LHS, RHS; |
| 584 | EmitUsualArithmeticConversions(E, LHS, RHS); |
| 585 | |
| 586 | // FIXME: This doesn't handle ptr-int or ptr-ptr, etc yet. |
| 587 | |
| 588 | if (LHS.isScalar()) |
| 589 | return RValue::get(Builder.CreateSub(LHS.getVal(), RHS.getVal(), "sub")); |
| 590 | |
| 591 | assert(0 && "FIXME: This doesn't handle complex operands yet"); |
| 592 | |
| 593 | } |
| 594 | |
| 595 | RValue CodeGenFunction::EmitBinaryShl(const BinaryOperator *E) { |
| 596 | // For shifts, integer promotions are performed, but the usual arithmetic |
| 597 | // conversions are not. The LHS and RHS need not have the same type. |
| 598 | |
| 599 | QualType ResTy; |
| 600 | Value *LHS = EmitExprWithUsualUnaryConversions(E->getLHS(), ResTy).getVal(); |
| 601 | Value *RHS = EmitExprWithUsualUnaryConversions(E->getRHS(), ResTy).getVal(); |
| 602 | |
| 603 | // LLVM requires the LHS and RHS to be the same type, promote or truncate the |
| 604 | // RHS to the same size as the LHS. |
| 605 | if (LHS->getType() != RHS->getType()) |
| 606 | RHS = Builder.CreateIntCast(RHS, LHS->getType(), false, "sh_prom"); |
| 607 | |
| 608 | return RValue::get(Builder.CreateShl(LHS, RHS, "shl")); |
| 609 | } |
| 610 | |
| 611 | RValue CodeGenFunction::EmitBinaryShr(const BinaryOperator *E) { |
| 612 | // For shifts, integer promotions are performed, but the usual arithmetic |
| 613 | // conversions are not. The LHS and RHS need not have the same type. |
| 614 | |
| 615 | QualType ResTy; |
| 616 | Value *LHS = EmitExprWithUsualUnaryConversions(E->getLHS(), ResTy).getVal(); |
| 617 | Value *RHS = EmitExprWithUsualUnaryConversions(E->getRHS(), ResTy).getVal(); |
| 618 | |
| 619 | // LLVM requires the LHS and RHS to be the same type, promote or truncate the |
| 620 | // RHS to the same size as the LHS. |
| 621 | if (LHS->getType() != RHS->getType()) |
| 622 | RHS = Builder.CreateIntCast(RHS, LHS->getType(), false, "sh_prom"); |
| 623 | |
| 624 | if (E->getType()->isUnsignedIntegerType()) |
| 625 | return RValue::get(Builder.CreateLShr(LHS, RHS, "shr")); |
| 626 | else |
| 627 | return RValue::get(Builder.CreateAShr(LHS, RHS, "shr")); |
| 628 | } |
| 629 | |
| 630 | RValue CodeGenFunction::EmitBinaryAnd(const BinaryOperator *E) { |
| 631 | RValue LHS, RHS; |
| 632 | EmitUsualArithmeticConversions(E, LHS, RHS); |
| 633 | |
| 634 | if (LHS.isScalar()) |
| 635 | return RValue::get(Builder.CreateAnd(LHS.getVal(), RHS.getVal(), "and")); |
| 636 | |
| 637 | assert(0 && "FIXME: This doesn't handle complex integer operands yet (GNU)"); |
| 638 | } |
| 639 | |
| 640 | RValue CodeGenFunction::EmitBinaryXor(const BinaryOperator *E) { |
| 641 | RValue LHS, RHS; |
| 642 | EmitUsualArithmeticConversions(E, LHS, RHS); |
| 643 | |
| 644 | if (LHS.isScalar()) |
| 645 | return RValue::get(Builder.CreateXor(LHS.getVal(), RHS.getVal(), "xor")); |
| 646 | |
| 647 | assert(0 && "FIXME: This doesn't handle complex integer operands yet (GNU)"); |
| 648 | } |
| 649 | |
| 650 | RValue CodeGenFunction::EmitBinaryOr(const BinaryOperator *E) { |
| 651 | RValue LHS, RHS; |
| 652 | EmitUsualArithmeticConversions(E, LHS, RHS); |
| 653 | |
| 654 | if (LHS.isScalar()) |
| 655 | return RValue::get(Builder.CreateOr(LHS.getVal(), RHS.getVal(), "or")); |
| 656 | |
| 657 | assert(0 && "FIXME: This doesn't handle complex integer operands yet (GNU)"); |
| 658 | } |
| 659 | |
| 660 | RValue CodeGenFunction::EmitBinaryLAnd(const BinaryOperator *E) { |
| 661 | Value *LHSCond = EvaluateExprAsBool(E->getLHS()); |
| 662 | |
| 663 | BasicBlock *ContBlock = new BasicBlock("land_cont"); |
| 664 | BasicBlock *RHSBlock = new BasicBlock("land_rhs"); |
| 665 | |
| 666 | BasicBlock *OrigBlock = Builder.GetInsertBlock(); |
| 667 | Builder.CreateCondBr(LHSCond, RHSBlock, ContBlock); |
| 668 | |
| 669 | EmitBlock(RHSBlock); |
| 670 | Value *RHSCond = EvaluateExprAsBool(E->getRHS()); |
| 671 | |
| 672 | // Reaquire the RHS block, as there may be subblocks inserted. |
| 673 | RHSBlock = Builder.GetInsertBlock(); |
| 674 | EmitBlock(ContBlock); |
| 675 | |
| 676 | // Create a PHI node. If we just evaluted the LHS condition, the result is |
| 677 | // false. If we evaluated both, the result is the RHS condition. |
| 678 | PHINode *PN = Builder.CreatePHI(llvm::Type::Int1Ty, "land"); |
| 679 | PN->reserveOperandSpace(2); |
| 680 | PN->addIncoming(ConstantInt::getFalse(), OrigBlock); |
| 681 | PN->addIncoming(RHSCond, RHSBlock); |
| 682 | |
| 683 | // ZExt result to int. |
| 684 | return RValue::get(Builder.CreateZExt(PN, LLVMIntTy, "land.ext")); |
| 685 | } |
| 686 | |
| 687 | RValue CodeGenFunction::EmitBinaryLOr(const BinaryOperator *E) { |
| 688 | Value *LHSCond = EvaluateExprAsBool(E->getLHS()); |
| 689 | |
| 690 | BasicBlock *ContBlock = new BasicBlock("lor_cont"); |
| 691 | BasicBlock *RHSBlock = new BasicBlock("lor_rhs"); |
| 692 | |
| 693 | BasicBlock *OrigBlock = Builder.GetInsertBlock(); |
| 694 | Builder.CreateCondBr(LHSCond, ContBlock, RHSBlock); |
| 695 | |
| 696 | EmitBlock(RHSBlock); |
| 697 | Value *RHSCond = EvaluateExprAsBool(E->getRHS()); |
| 698 | |
| 699 | // Reaquire the RHS block, as there may be subblocks inserted. |
| 700 | RHSBlock = Builder.GetInsertBlock(); |
| 701 | EmitBlock(ContBlock); |
| 702 | |
| 703 | // Create a PHI node. If we just evaluted the LHS condition, the result is |
| 704 | // true. If we evaluated both, the result is the RHS condition. |
| 705 | PHINode *PN = Builder.CreatePHI(llvm::Type::Int1Ty, "lor"); |
| 706 | PN->reserveOperandSpace(2); |
| 707 | PN->addIncoming(ConstantInt::getTrue(), OrigBlock); |
| 708 | PN->addIncoming(RHSCond, RHSBlock); |
| 709 | |
| 710 | // ZExt result to int. |
| 711 | return RValue::get(Builder.CreateZExt(PN, LLVMIntTy, "lor.ext")); |
| 712 | } |
| 713 | |
| 714 | RValue CodeGenFunction::EmitBinaryAssign(const BinaryOperator *E) { |
| 715 | LValue LHS = EmitLValue(E->getLHS()); |
| 716 | |
| 717 | QualType RHSTy; |
| 718 | RValue RHS = EmitExprWithUsualUnaryConversions(E->getRHS(), RHSTy); |
| 719 | |
| 720 | // Convert the RHS to the type of the LHS. |
Chris Lattner | cf106ab | 2007-06-06 04:05:39 +0000 | [diff] [blame^] | 721 | // FIXME: I'm not thrilled about having to call getLocStart() here... :( |
| 722 | RHS = EmitConversion(RHS, RHSTy, E->getType(), E->getLocStart()); |
Chris Lattner | 8394d79 | 2007-06-05 20:53:16 +0000 | [diff] [blame] | 723 | |
| 724 | // Store the value into the LHS. |
| 725 | EmitStoreThroughLValue(RHS, LHS, E->getType()); |
| 726 | |
| 727 | // Return the converted RHS. |
| 728 | return RHS; |
| 729 | } |
| 730 | |
| 731 | |
| 732 | RValue CodeGenFunction::EmitBinaryComma(const BinaryOperator *E) { |
| 733 | EmitExpr(E->getLHS()); |
| 734 | return EmitExpr(E->getRHS()); |
| 735 | } |