Chris Lattner | e47e440 | 2007-06-01 18:02:12 +0000 | [diff] [blame] | 1 | //===--- CGExpr.cpp - Emit LLVM Code from Expressions ---------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file was developed by Chris Lattner and is distributed under |
| 6 | // the University of Illinois Open Source License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This contains code to emit Expr nodes as LLVM code. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "CodeGenFunction.h" |
| 15 | #include "clang/AST/AST.h" |
| 16 | #include "llvm/Constants.h" |
| 17 | #include "llvm/DerivedTypes.h" |
| 18 | using namespace llvm; |
| 19 | using namespace clang; |
| 20 | using namespace CodeGen; |
| 21 | |
Chris Lattner | d7f5886 | 2007-06-02 05:24:33 +0000 | [diff] [blame] | 22 | //===--------------------------------------------------------------------===// |
Chris Lattner | f0106d2 | 2007-06-02 19:33:17 +0000 | [diff] [blame] | 23 | // Miscellaneous Helper Methods |
| 24 | //===--------------------------------------------------------------------===// |
| 25 | |
| 26 | /// EvaluateScalarValueToBool - Evaluate the specified expression value to a |
| 27 | /// boolean (i1) truth value. This is equivalent to "Val == 0". |
| 28 | Value *CodeGenFunction::EvaluateScalarValueToBool(ExprResult Val, QualType Ty) { |
| 29 | Ty = Ty.getCanonicalType(); |
| 30 | Value *Result; |
| 31 | if (const BuiltinType *BT = dyn_cast<BuiltinType>(Ty)) { |
| 32 | switch (BT->getKind()) { |
| 33 | default: assert(0 && "Unknown scalar value"); |
| 34 | case BuiltinType::Bool: |
| 35 | Result = Val.getVal(); |
| 36 | // Bool is already evaluated right. |
| 37 | assert(Result->getType() == llvm::Type::Int1Ty && |
| 38 | "Unexpected bool value type!"); |
| 39 | return Result; |
| 40 | case BuiltinType::Char: |
| 41 | case BuiltinType::SChar: |
| 42 | case BuiltinType::UChar: |
| 43 | case BuiltinType::Short: |
| 44 | case BuiltinType::UShort: |
| 45 | case BuiltinType::Int: |
| 46 | case BuiltinType::UInt: |
| 47 | case BuiltinType::Long: |
| 48 | case BuiltinType::ULong: |
| 49 | case BuiltinType::LongLong: |
| 50 | case BuiltinType::ULongLong: |
| 51 | // Code below handles simple integers. |
| 52 | break; |
| 53 | case BuiltinType::Float: |
| 54 | case BuiltinType::Double: |
| 55 | case BuiltinType::LongDouble: { |
| 56 | // Compare against 0.0 for fp scalars. |
| 57 | Result = Val.getVal(); |
| 58 | llvm::Value *Zero = Constant::getNullValue(Result->getType()); |
| 59 | // FIXME: llvm-gcc produces a une comparison: validate this is right. |
| 60 | Result = Builder.CreateFCmpUNE(Result, Zero, "tobool"); |
| 61 | return Result; |
| 62 | } |
| 63 | |
| 64 | case BuiltinType::FloatComplex: |
| 65 | case BuiltinType::DoubleComplex: |
| 66 | case BuiltinType::LongDoubleComplex: |
| 67 | assert(0 && "comparisons against complex not implemented yet"); |
| 68 | } |
| 69 | } else { |
| 70 | assert((isa<PointerType>(Ty) || |
| 71 | cast<TagType>(Ty)->getDecl()->getKind() == Decl::Enum) && |
| 72 | "Unknown scalar type"); |
| 73 | // Code below handles this fine. |
| 74 | } |
| 75 | |
| 76 | // Usual case for integers, pointers, and enums: compare against zero. |
| 77 | Result = Val.getVal(); |
Chris Lattner | a45c5af | 2007-06-02 19:47:04 +0000 | [diff] [blame^] | 78 | |
| 79 | // Because of the type rules of C, we often end up computing a logical value, |
| 80 | // then zero extending it to int, then wanting it as a logical value again. |
| 81 | // Optimize this common case. |
| 82 | if (llvm::ZExtInst *ZI = dyn_cast<ZExtInst>(Result)) { |
| 83 | if (ZI->getOperand(0)->getType() == llvm::Type::Int1Ty) { |
| 84 | Result = ZI->getOperand(0); |
| 85 | ZI->eraseFromParent(); |
| 86 | return Result; |
| 87 | } |
| 88 | } |
| 89 | |
Chris Lattner | f0106d2 | 2007-06-02 19:33:17 +0000 | [diff] [blame] | 90 | llvm::Value *Zero = Constant::getNullValue(Result->getType()); |
| 91 | return Builder.CreateICmpNE(Result, Zero, "tobool"); |
| 92 | } |
| 93 | |
Chris Lattner | a45c5af | 2007-06-02 19:47:04 +0000 | [diff] [blame^] | 94 | //===----------------------------------------------------------------------===// |
Chris Lattner | d7f5886 | 2007-06-02 05:24:33 +0000 | [diff] [blame] | 95 | // LValue Expression Emission |
Chris Lattner | a45c5af | 2007-06-02 19:47:04 +0000 | [diff] [blame^] | 96 | //===----------------------------------------------------------------------===// |
Chris Lattner | d7f5886 | 2007-06-02 05:24:33 +0000 | [diff] [blame] | 97 | |
| 98 | LValue CodeGenFunction::EmitLValue(const Expr *E) { |
| 99 | switch (E->getStmtClass()) { |
| 100 | default: |
| 101 | printf("Unimplemented lvalue expr!\n"); |
| 102 | E->dump(); |
| 103 | return LValue::getAddr(UndefValue::get( |
| 104 | llvm::PointerType::get(llvm::Type::Int32Ty))); |
| 105 | |
| 106 | case Expr::DeclRefExprClass: return EmitDeclRefLValue(cast<DeclRefExpr>(E)); |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | |
| 111 | LValue CodeGenFunction::EmitDeclRefLValue(const DeclRefExpr *E) { |
| 112 | const Decl *D = E->getDecl(); |
| 113 | if (isa<BlockVarDecl>(D)) { |
| 114 | Value *V = LocalDeclMap[D]; |
| 115 | assert(V && "BlockVarDecl not entered in LocalDeclMap?"); |
| 116 | return LValue::getAddr(V); |
| 117 | } |
| 118 | assert(0 && "Unimp declref"); |
| 119 | } |
Chris Lattner | e47e440 | 2007-06-01 18:02:12 +0000 | [diff] [blame] | 120 | |
| 121 | //===--------------------------------------------------------------------===// |
| 122 | // Expression Emission |
| 123 | //===--------------------------------------------------------------------===// |
| 124 | |
| 125 | ExprResult CodeGenFunction::EmitExpr(const Expr *E) { |
| 126 | assert(E && "Null expression?"); |
| 127 | |
| 128 | switch (E->getStmtClass()) { |
| 129 | default: |
| 130 | printf("Unimplemented expr!\n"); |
| 131 | E->dump(); |
| 132 | return ExprResult::get(UndefValue::get(llvm::Type::Int32Ty)); |
Chris Lattner | d7f5886 | 2007-06-02 05:24:33 +0000 | [diff] [blame] | 133 | |
| 134 | // l-values. |
| 135 | case Expr::DeclRefExprClass: { |
| 136 | // FIXME: EnumConstantDecl's are not lvalues. |
| 137 | LValue LV = EmitLValue(E); |
| 138 | // FIXME: this is silly. |
| 139 | assert(!LV.isBitfield()); |
| 140 | return ExprResult::get(Builder.CreateLoad(LV.getAddress(), "tmp")); |
| 141 | } |
| 142 | |
| 143 | // Leaf expressions. |
| 144 | case Expr::IntegerLiteralClass: |
Chris Lattner | e47e440 | 2007-06-01 18:02:12 +0000 | [diff] [blame] | 145 | return EmitIntegerLiteral(cast<IntegerLiteral>(E)); |
Chris Lattner | db91b16 | 2007-06-02 00:16:28 +0000 | [diff] [blame] | 146 | |
Chris Lattner | d7f5886 | 2007-06-02 05:24:33 +0000 | [diff] [blame] | 147 | // Operators. |
| 148 | case Expr::ParenExprClass: |
| 149 | return EmitExpr(cast<ParenExpr>(E)->getSubExpr()); |
Chris Lattner | f0106d2 | 2007-06-02 19:33:17 +0000 | [diff] [blame] | 150 | case Expr::UnaryOperatorClass: |
| 151 | return EmitUnaryOperator(cast<UnaryOperator>(E)); |
Chris Lattner | d7f5886 | 2007-06-02 05:24:33 +0000 | [diff] [blame] | 152 | case Expr::BinaryOperatorClass: |
Chris Lattner | db91b16 | 2007-06-02 00:16:28 +0000 | [diff] [blame] | 153 | return EmitBinaryOperator(cast<BinaryOperator>(E)); |
Chris Lattner | e47e440 | 2007-06-01 18:02:12 +0000 | [diff] [blame] | 154 | } |
| 155 | |
| 156 | } |
| 157 | |
| 158 | ExprResult CodeGenFunction::EmitIntegerLiteral(const IntegerLiteral *E) { |
| 159 | return ExprResult::get(ConstantInt::get(E->getValue())); |
| 160 | } |
| 161 | |
Chris Lattner | f0106d2 | 2007-06-02 19:33:17 +0000 | [diff] [blame] | 162 | //===--------------------------------------------------------------------===// |
| 163 | // Unary Operator Emission |
| 164 | //===--------------------------------------------------------------------===// |
| 165 | |
| 166 | ExprResult CodeGenFunction::EmitUnaryOperator(const UnaryOperator *E) { |
| 167 | switch (E->getOpcode()) { |
| 168 | default: |
| 169 | printf("Unimplemented unary expr!\n"); |
| 170 | E->dump(); |
| 171 | return ExprResult::get(UndefValue::get(llvm::Type::Int32Ty)); |
| 172 | case UnaryOperator::LNot: return EmitUnaryLNot(E); |
| 173 | } |
| 174 | } |
| 175 | |
| 176 | /// C99 6.5.3.3 |
| 177 | ExprResult CodeGenFunction::EmitUnaryLNot(const UnaryOperator *E) { |
| 178 | ExprResult Op = EmitExpr(E->getSubExpr()); |
| 179 | |
| 180 | //UsualUnary(); |
| 181 | |
| 182 | // Compare to zero. |
| 183 | Value *BoolVal = EvaluateScalarValueToBool(Op, E->getSubExpr()->getType()); |
| 184 | |
| 185 | // Invert value. |
Chris Lattner | a45c5af | 2007-06-02 19:47:04 +0000 | [diff] [blame^] | 186 | // TODO: Could dynamically modify easy computations here. For example, if |
| 187 | // the operand is an icmp ne, turn into icmp eq. |
Chris Lattner | f0106d2 | 2007-06-02 19:33:17 +0000 | [diff] [blame] | 188 | BoolVal = Builder.CreateNot(BoolVal, "lnot"); |
| 189 | |
| 190 | // ZExt result to int. |
| 191 | const llvm::Type *ResTy = ConvertType(E->getType(), E->getOperatorLoc()); |
| 192 | return ExprResult::get(Builder.CreateZExt(BoolVal, ResTy, "lnot.ext")); |
| 193 | } |
| 194 | |
Chris Lattner | e47e440 | 2007-06-01 18:02:12 +0000 | [diff] [blame] | 195 | |
Chris Lattner | db91b16 | 2007-06-02 00:16:28 +0000 | [diff] [blame] | 196 | //===--------------------------------------------------------------------===// |
| 197 | // Binary Operator Emission |
| 198 | //===--------------------------------------------------------------------===// |
| 199 | |
| 200 | // FIXME describe. |
| 201 | void CodeGenFunction::EmitUsualArithmeticConversions(const BinaryOperator *E, |
| 202 | ExprResult &LHS, |
| 203 | ExprResult &RHS) { |
| 204 | // FIXME: implement right. |
| 205 | LHS = EmitExpr(E->getLHS()); |
| 206 | RHS = EmitExpr(E->getRHS()); |
| 207 | } |
| 208 | |
| 209 | |
| 210 | ExprResult CodeGenFunction::EmitBinaryOperator(const BinaryOperator *E) { |
| 211 | switch (E->getOpcode()) { |
| 212 | default: |
| 213 | printf("Unimplemented expr!\n"); |
| 214 | E->dump(); |
| 215 | return ExprResult::get(UndefValue::get(llvm::Type::Int32Ty)); |
| 216 | case BinaryOperator::Add: return EmitBinaryAdd(E); |
| 217 | } |
| 218 | } |
| 219 | |
| 220 | |
| 221 | ExprResult CodeGenFunction::EmitBinaryAdd(const BinaryOperator *E) { |
| 222 | ExprResult LHS, RHS; |
| 223 | |
| 224 | EmitUsualArithmeticConversions(E, LHS, RHS); |
| 225 | |
| 226 | |
| 227 | return ExprResult::get(Builder.CreateAdd(LHS.getVal(), RHS.getVal(), "tmp")); |
| 228 | } |