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