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