Chris Lattner | b542afe | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 1 | //===--- ExprConstant.cpp - Expression Constant Evaluator -----------------===// |
Anders Carlsson | c44eec6 | 2008-07-03 04:20:39 +0000 | [diff] [blame] | 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements the Expr constant evaluator. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "clang/AST/APValue.h" |
| 15 | #include "clang/AST/ASTContext.h" |
Anders Carlsson | 19cc4ab | 2009-07-18 19:43:29 +0000 | [diff] [blame] | 16 | #include "clang/AST/RecordLayout.h" |
Seo Sanghyeon | 0fe52e1 | 2008-07-08 07:23:12 +0000 | [diff] [blame] | 17 | #include "clang/AST/StmtVisitor.h" |
Chris Lattner | 500d329 | 2009-01-29 05:15:15 +0000 | [diff] [blame] | 18 | #include "clang/AST/ASTDiagnostic.h" |
Chris Lattner | 1b63e4f | 2009-06-14 01:54:56 +0000 | [diff] [blame] | 19 | #include "clang/Basic/Builtins.h" |
Anders Carlsson | 06a3675 | 2008-07-08 05:49:43 +0000 | [diff] [blame] | 20 | #include "clang/Basic/TargetInfo.h" |
Mike Stump | 7462b39 | 2009-05-30 14:43:18 +0000 | [diff] [blame] | 21 | #include "llvm/ADT/SmallString.h" |
Anders Carlsson | c754aa6 | 2008-07-08 05:13:58 +0000 | [diff] [blame] | 22 | #include "llvm/Support/Compiler.h" |
Mike Stump | 4572bab | 2009-05-30 03:56:50 +0000 | [diff] [blame] | 23 | #include <cstring> |
| 24 | |
Anders Carlsson | c44eec6 | 2008-07-03 04:20:39 +0000 | [diff] [blame] | 25 | using namespace clang; |
Chris Lattner | f5eeb05 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 26 | using llvm::APSInt; |
Eli Friedman | d8bfe7f | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 27 | using llvm::APFloat; |
Anders Carlsson | c44eec6 | 2008-07-03 04:20:39 +0000 | [diff] [blame] | 28 | |
Chris Lattner | 87eae5e | 2008-07-11 22:52:41 +0000 | [diff] [blame] | 29 | /// EvalInfo - This is a private struct used by the evaluator to capture |
| 30 | /// information about a subexpression as it is folded. It retains information |
| 31 | /// about the AST context, but also maintains information about the folded |
| 32 | /// expression. |
| 33 | /// |
| 34 | /// If an expression could be evaluated, it is still possible it is not a C |
| 35 | /// "integer constant expression" or constant expression. If not, this struct |
| 36 | /// captures information about how and why not. |
| 37 | /// |
| 38 | /// One bit of information passed *into* the request for constant folding |
| 39 | /// indicates whether the subexpression is "evaluated" or not according to C |
| 40 | /// rules. For example, the RHS of (0 && foo()) is not evaluated. We can |
| 41 | /// evaluate the expression regardless of what the RHS is, but C only allows |
| 42 | /// certain things in certain situations. |
| 43 | struct EvalInfo { |
| 44 | ASTContext &Ctx; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 45 | |
Anders Carlsson | 54da049 | 2008-11-30 16:38:33 +0000 | [diff] [blame] | 46 | /// EvalResult - Contains information about the evaluation. |
| 47 | Expr::EvalResult &EvalResult; |
Anders Carlsson | f0c1e4b | 2008-11-30 18:26:25 +0000 | [diff] [blame] | 48 | |
Eli Friedman | b2f295c | 2009-09-13 10:17:44 +0000 | [diff] [blame] | 49 | /// AnyLValue - Stack based LValue results are not discarded. |
| 50 | bool AnyLValue; |
| 51 | |
| 52 | EvalInfo(ASTContext &ctx, Expr::EvalResult& evalresult, |
| 53 | bool anylvalue = false) |
| 54 | : Ctx(ctx), EvalResult(evalresult), AnyLValue(anylvalue) {} |
Chris Lattner | 87eae5e | 2008-07-11 22:52:41 +0000 | [diff] [blame] | 55 | }; |
| 56 | |
| 57 | |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 58 | static bool EvaluateLValue(const Expr *E, APValue &Result, EvalInfo &Info); |
Chris Lattner | 87eae5e | 2008-07-11 22:52:41 +0000 | [diff] [blame] | 59 | static bool EvaluatePointer(const Expr *E, APValue &Result, EvalInfo &Info); |
| 60 | static bool EvaluateInteger(const Expr *E, APSInt &Result, EvalInfo &Info); |
Chris Lattner | d9becd1 | 2009-10-28 23:59:40 +0000 | [diff] [blame^] | 61 | #ifndef USEINDIRECTBRANCH |
Daniel Dunbar | 69ab26a | 2009-02-20 18:22:23 +0000 | [diff] [blame] | 62 | static bool EvaluateIntegerOrLValue(const Expr *E, APValue &Result, EvalInfo &Info); |
Chris Lattner | d9becd1 | 2009-10-28 23:59:40 +0000 | [diff] [blame^] | 63 | #else |
| 64 | static bool EvaluateIntegerOrLValue(const Expr *E, APValue &Result, |
| 65 | EvalInfo &Info); |
| 66 | #endif |
Eli Friedman | d8bfe7f | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 67 | static bool EvaluateFloat(const Expr *E, APFloat &Result, EvalInfo &Info); |
Daniel Dunbar | a5fd07b | 2009-01-28 22:24:07 +0000 | [diff] [blame] | 68 | static bool EvaluateComplex(const Expr *E, APValue &Result, EvalInfo &Info); |
Chris Lattner | f5eeb05 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 69 | |
| 70 | //===----------------------------------------------------------------------===// |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 71 | // Misc utilities |
| 72 | //===----------------------------------------------------------------------===// |
| 73 | |
Eli Friedman | 5bc8610 | 2009-06-14 02:17:33 +0000 | [diff] [blame] | 74 | static bool EvalPointerValueAsBool(APValue& Value, bool& Result) { |
| 75 | // FIXME: Is this accurate for all kinds of bases? If not, what would |
| 76 | // the check look like? |
| 77 | Result = Value.getLValueBase() || Value.getLValueOffset(); |
| 78 | return true; |
| 79 | } |
| 80 | |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 81 | static bool HandleConversionToBool(Expr* E, bool& Result, EvalInfo &Info) { |
| 82 | if (E->getType()->isIntegralType()) { |
| 83 | APSInt IntResult; |
| 84 | if (!EvaluateInteger(E, IntResult, Info)) |
| 85 | return false; |
| 86 | Result = IntResult != 0; |
| 87 | return true; |
| 88 | } else if (E->getType()->isRealFloatingType()) { |
| 89 | APFloat FloatResult(0.0); |
| 90 | if (!EvaluateFloat(E, FloatResult, Info)) |
| 91 | return false; |
| 92 | Result = !FloatResult.isZero(); |
| 93 | return true; |
Eli Friedman | a1f47c4 | 2009-03-23 04:38:34 +0000 | [diff] [blame] | 94 | } else if (E->getType()->hasPointerRepresentation()) { |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 95 | APValue PointerResult; |
| 96 | if (!EvaluatePointer(E, PointerResult, Info)) |
| 97 | return false; |
Eli Friedman | 5bc8610 | 2009-06-14 02:17:33 +0000 | [diff] [blame] | 98 | return EvalPointerValueAsBool(PointerResult, Result); |
Eli Friedman | a1f47c4 | 2009-03-23 04:38:34 +0000 | [diff] [blame] | 99 | } else if (E->getType()->isAnyComplexType()) { |
| 100 | APValue ComplexResult; |
| 101 | if (!EvaluateComplex(E, ComplexResult, Info)) |
| 102 | return false; |
| 103 | if (ComplexResult.isComplexFloat()) { |
| 104 | Result = !ComplexResult.getComplexFloatReal().isZero() || |
| 105 | !ComplexResult.getComplexFloatImag().isZero(); |
| 106 | } else { |
| 107 | Result = ComplexResult.getComplexIntReal().getBoolValue() || |
| 108 | ComplexResult.getComplexIntImag().getBoolValue(); |
| 109 | } |
| 110 | return true; |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | return false; |
| 114 | } |
| 115 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 116 | static APSInt HandleFloatToIntCast(QualType DestType, QualType SrcType, |
Daniel Dunbar | a2cfd34 | 2009-01-29 06:16:07 +0000 | [diff] [blame] | 117 | APFloat &Value, ASTContext &Ctx) { |
| 118 | unsigned DestWidth = Ctx.getIntWidth(DestType); |
| 119 | // Determine whether we are converting to unsigned or signed. |
| 120 | bool DestSigned = DestType->isSignedIntegerType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 121 | |
Daniel Dunbar | a2cfd34 | 2009-01-29 06:16:07 +0000 | [diff] [blame] | 122 | // FIXME: Warning for overflow. |
| 123 | uint64_t Space[4]; |
| 124 | bool ignored; |
| 125 | (void)Value.convertToInteger(Space, DestWidth, DestSigned, |
| 126 | llvm::APFloat::rmTowardZero, &ignored); |
| 127 | return APSInt(llvm::APInt(DestWidth, 4, Space), !DestSigned); |
| 128 | } |
| 129 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 130 | static APFloat HandleFloatToFloatCast(QualType DestType, QualType SrcType, |
Daniel Dunbar | a2cfd34 | 2009-01-29 06:16:07 +0000 | [diff] [blame] | 131 | APFloat &Value, ASTContext &Ctx) { |
| 132 | bool ignored; |
| 133 | APFloat Result = Value; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 134 | Result.convert(Ctx.getFloatTypeSemantics(DestType), |
Daniel Dunbar | a2cfd34 | 2009-01-29 06:16:07 +0000 | [diff] [blame] | 135 | APFloat::rmNearestTiesToEven, &ignored); |
| 136 | return Result; |
| 137 | } |
| 138 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 139 | static APSInt HandleIntToIntCast(QualType DestType, QualType SrcType, |
Daniel Dunbar | a2cfd34 | 2009-01-29 06:16:07 +0000 | [diff] [blame] | 140 | APSInt &Value, ASTContext &Ctx) { |
| 141 | unsigned DestWidth = Ctx.getIntWidth(DestType); |
| 142 | APSInt Result = Value; |
| 143 | // Figure out if this is a truncate, extend or noop cast. |
| 144 | // If the input is signed, do a sign extend, noop, or truncate. |
| 145 | Result.extOrTrunc(DestWidth); |
| 146 | Result.setIsUnsigned(DestType->isUnsignedIntegerType()); |
| 147 | return Result; |
| 148 | } |
| 149 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 150 | static APFloat HandleIntToFloatCast(QualType DestType, QualType SrcType, |
Daniel Dunbar | a2cfd34 | 2009-01-29 06:16:07 +0000 | [diff] [blame] | 151 | APSInt &Value, ASTContext &Ctx) { |
| 152 | |
| 153 | APFloat Result(Ctx.getFloatTypeSemantics(DestType), 1); |
| 154 | Result.convertFromAPInt(Value, Value.isSigned(), |
| 155 | APFloat::rmNearestTiesToEven); |
| 156 | return Result; |
| 157 | } |
| 158 | |
Mike Stump | c4c9045 | 2009-10-27 22:09:17 +0000 | [diff] [blame] | 159 | namespace { |
| 160 | class VISIBILITY_HIDDEN HasSideEffect |
| 161 | : public StmtVisitor<HasSideEffect, bool> { |
| 162 | EvalInfo &Info; |
| 163 | public: |
| 164 | |
| 165 | HasSideEffect(EvalInfo &info) : Info(info) {} |
| 166 | |
| 167 | // Unhandled nodes conservatively default to having side effects. |
| 168 | bool VisitStmt(Stmt *S) { |
| 169 | return true; |
| 170 | } |
| 171 | |
| 172 | bool VisitParenExpr(ParenExpr *E) { return Visit(E->getSubExpr()); } |
| 173 | bool VisitDeclRefExpr(DeclRefExpr *E) { |
| 174 | if (E->getType().isVolatileQualified()) |
| 175 | return true; |
| 176 | return false; |
| 177 | } |
| 178 | // We don't want to evaluate BlockExprs multiple times, as they generate |
| 179 | // a ton of code. |
| 180 | bool VisitBlockExpr(BlockExpr *E) { return true; } |
| 181 | bool VisitPredefinedExpr(PredefinedExpr *E) { return false; } |
| 182 | bool VisitCompoundLiteralExpr(CompoundLiteralExpr *E) |
| 183 | { return Visit(E->getInitializer()); } |
| 184 | bool VisitMemberExpr(MemberExpr *E) { return Visit(E->getBase()); } |
| 185 | bool VisitIntegerLiteral(IntegerLiteral *E) { return false; } |
| 186 | bool VisitFloatingLiteral(FloatingLiteral *E) { return false; } |
| 187 | bool VisitStringLiteral(StringLiteral *E) { return false; } |
| 188 | bool VisitCharacterLiteral(CharacterLiteral *E) { return false; } |
| 189 | bool VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E) { return false; } |
| 190 | bool VisitArraySubscriptExpr(ArraySubscriptExpr *E) |
| 191 | { return Visit(E->getLHS()) && Visit(E->getRHS()); } |
| 192 | bool VisitChooseExpr(ChooseExpr *E) |
| 193 | { return Visit(E->getChosenSubExpr(Info.Ctx)); } |
| 194 | bool VisitCastExpr(CastExpr *E) { return Visit(E->getSubExpr()); } |
| 195 | bool VisitBinAssign(BinaryOperator *E) { return true; } |
| 196 | bool VisitCompoundAssign(BinaryOperator *E) { return true; } |
| 197 | bool VisitBinaryOperator(BinaryOperator *E) { return false; } |
| 198 | bool VisitUnaryPreInc(UnaryOperator *E) { return true; } |
| 199 | bool VisitUnaryPostInc(UnaryOperator *E) { return true; } |
| 200 | bool VisitUnaryPreDec(UnaryOperator *E) { return true; } |
| 201 | bool VisitUnaryPostDec(UnaryOperator *E) { return true; } |
| 202 | bool VisitUnaryDeref(UnaryOperator *E) { |
| 203 | if (E->getType().isVolatileQualified()) |
| 204 | return true; |
| 205 | return false; |
| 206 | } |
| 207 | bool VisitUnaryOperator(UnaryOperator *E) { return Visit(E->getSubExpr()); } |
| 208 | }; |
| 209 | |
| 210 | bool HasSideEffects(const Expr* E, ASTContext &Ctx) { |
| 211 | Expr::EvalResult Result; |
| 212 | EvalInfo Info(Ctx, Result); |
| 213 | |
| 214 | return HasSideEffect(Info).Visit(const_cast<Expr*>(E)); |
| 215 | } |
| 216 | |
| 217 | } // end anonymous namespace |
| 218 | |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 219 | //===----------------------------------------------------------------------===// |
| 220 | // LValue Evaluation |
| 221 | //===----------------------------------------------------------------------===// |
| 222 | namespace { |
| 223 | class VISIBILITY_HIDDEN LValueExprEvaluator |
| 224 | : public StmtVisitor<LValueExprEvaluator, APValue> { |
| 225 | EvalInfo &Info; |
| 226 | public: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 227 | |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 228 | LValueExprEvaluator(EvalInfo &info) : Info(info) {} |
| 229 | |
| 230 | APValue VisitStmt(Stmt *S) { |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 231 | return APValue(); |
| 232 | } |
| 233 | |
| 234 | APValue VisitParenExpr(ParenExpr *E) { return Visit(E->getSubExpr()); } |
Anders Carlsson | 35873c4 | 2008-11-24 04:41:22 +0000 | [diff] [blame] | 235 | APValue VisitDeclRefExpr(DeclRefExpr *E); |
Steve Naroff | 3aaa482 | 2009-04-16 19:02:57 +0000 | [diff] [blame] | 236 | APValue VisitBlockExpr(BlockExpr *E); |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 237 | APValue VisitPredefinedExpr(PredefinedExpr *E) { return APValue(E, 0); } |
| 238 | APValue VisitCompoundLiteralExpr(CompoundLiteralExpr *E); |
| 239 | APValue VisitMemberExpr(MemberExpr *E); |
| 240 | APValue VisitStringLiteral(StringLiteral *E) { return APValue(E, 0); } |
Chris Lattner | eaf2bb8 | 2009-02-24 22:18:39 +0000 | [diff] [blame] | 241 | APValue VisitObjCEncodeExpr(ObjCEncodeExpr *E) { return APValue(E, 0); } |
Anders Carlsson | 3068d11 | 2008-11-16 19:01:22 +0000 | [diff] [blame] | 242 | APValue VisitArraySubscriptExpr(ArraySubscriptExpr *E); |
Eli Friedman | e8761c8 | 2009-02-20 01:57:15 +0000 | [diff] [blame] | 243 | APValue VisitUnaryDeref(UnaryOperator *E); |
Eli Friedman | ba98d6b | 2009-03-23 04:56:01 +0000 | [diff] [blame] | 244 | APValue VisitUnaryExtension(const UnaryOperator *E) |
| 245 | { return Visit(E->getSubExpr()); } |
| 246 | APValue VisitChooseExpr(const ChooseExpr *E) |
| 247 | { return Visit(E->getChosenSubExpr(Info.Ctx)); } |
Anders Carlsson | 26bc220 | 2009-10-03 16:30:22 +0000 | [diff] [blame] | 248 | |
| 249 | APValue VisitCastExpr(CastExpr *E) { |
| 250 | switch (E->getCastKind()) { |
| 251 | default: |
| 252 | return APValue(); |
| 253 | |
| 254 | case CastExpr::CK_NoOp: |
| 255 | return Visit(E->getSubExpr()); |
| 256 | } |
| 257 | } |
Eli Friedman | ba98d6b | 2009-03-23 04:56:01 +0000 | [diff] [blame] | 258 | // FIXME: Missing: __real__, __imag__ |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 259 | }; |
| 260 | } // end anonymous namespace |
| 261 | |
| 262 | static bool EvaluateLValue(const Expr* E, APValue& Result, EvalInfo &Info) { |
| 263 | Result = LValueExprEvaluator(Info).Visit(const_cast<Expr*>(E)); |
| 264 | return Result.isLValue(); |
| 265 | } |
| 266 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 267 | APValue LValueExprEvaluator::VisitDeclRefExpr(DeclRefExpr *E) { |
Eli Friedman | 50c39ea | 2009-05-27 06:04:58 +0000 | [diff] [blame] | 268 | if (isa<FunctionDecl>(E->getDecl())) { |
| 269 | return APValue(E, 0); |
| 270 | } else if (VarDecl* VD = dyn_cast<VarDecl>(E->getDecl())) { |
Eli Friedman | b2f295c | 2009-09-13 10:17:44 +0000 | [diff] [blame] | 271 | if (!Info.AnyLValue && !VD->hasGlobalStorage()) |
Eli Friedman | d933a01 | 2009-08-29 19:09:59 +0000 | [diff] [blame] | 272 | return APValue(); |
Eli Friedman | 50c39ea | 2009-05-27 06:04:58 +0000 | [diff] [blame] | 273 | if (!VD->getType()->isReferenceType()) |
| 274 | return APValue(E, 0); |
Eli Friedman | d933a01 | 2009-08-29 19:09:59 +0000 | [diff] [blame] | 275 | // FIXME: Check whether VD might be overridden! |
Eli Friedman | 50c39ea | 2009-05-27 06:04:58 +0000 | [diff] [blame] | 276 | if (VD->getInit()) |
| 277 | return Visit(VD->getInit()); |
| 278 | } |
| 279 | |
| 280 | return APValue(); |
Anders Carlsson | 35873c4 | 2008-11-24 04:41:22 +0000 | [diff] [blame] | 281 | } |
| 282 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 283 | APValue LValueExprEvaluator::VisitBlockExpr(BlockExpr *E) { |
Steve Naroff | 3aaa482 | 2009-04-16 19:02:57 +0000 | [diff] [blame] | 284 | if (E->hasBlockDeclRefExprs()) |
| 285 | return APValue(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 286 | |
Steve Naroff | 3aaa482 | 2009-04-16 19:02:57 +0000 | [diff] [blame] | 287 | return APValue(E, 0); |
| 288 | } |
| 289 | |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 290 | APValue LValueExprEvaluator::VisitCompoundLiteralExpr(CompoundLiteralExpr *E) { |
Eli Friedman | b2f295c | 2009-09-13 10:17:44 +0000 | [diff] [blame] | 291 | if (!Info.AnyLValue && !E->isFileScope()) |
| 292 | return APValue(); |
| 293 | return APValue(E, 0); |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 294 | } |
| 295 | |
| 296 | APValue LValueExprEvaluator::VisitMemberExpr(MemberExpr *E) { |
| 297 | APValue result; |
| 298 | QualType Ty; |
| 299 | if (E->isArrow()) { |
| 300 | if (!EvaluatePointer(E->getBase(), result, Info)) |
| 301 | return APValue(); |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 302 | Ty = E->getBase()->getType()->getAs<PointerType>()->getPointeeType(); |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 303 | } else { |
| 304 | result = Visit(E->getBase()); |
| 305 | if (result.isUninit()) |
| 306 | return APValue(); |
| 307 | Ty = E->getBase()->getType(); |
| 308 | } |
| 309 | |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 310 | RecordDecl *RD = Ty->getAs<RecordType>()->getDecl(); |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 311 | const ASTRecordLayout &RL = Info.Ctx.getASTRecordLayout(RD); |
Douglas Gregor | 86f1940 | 2008-12-20 23:49:58 +0000 | [diff] [blame] | 312 | |
| 313 | FieldDecl *FD = dyn_cast<FieldDecl>(E->getMemberDecl()); |
| 314 | if (!FD) // FIXME: deal with other kinds of member expressions |
| 315 | return APValue(); |
Eli Friedman | 2be5861 | 2009-05-30 21:09:44 +0000 | [diff] [blame] | 316 | |
| 317 | if (FD->getType()->isReferenceType()) |
| 318 | return APValue(); |
| 319 | |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 320 | // FIXME: This is linear time. |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 321 | unsigned i = 0; |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 322 | for (RecordDecl::field_iterator Field = RD->field_begin(), |
| 323 | FieldEnd = RD->field_end(); |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 324 | Field != FieldEnd; (void)++Field, ++i) { |
| 325 | if (*Field == FD) |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 326 | break; |
| 327 | } |
| 328 | |
| 329 | result.setLValue(result.getLValueBase(), |
| 330 | result.getLValueOffset() + RL.getFieldOffset(i) / 8); |
| 331 | |
| 332 | return result; |
| 333 | } |
| 334 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 335 | APValue LValueExprEvaluator::VisitArraySubscriptExpr(ArraySubscriptExpr *E) { |
Anders Carlsson | 3068d11 | 2008-11-16 19:01:22 +0000 | [diff] [blame] | 336 | APValue Result; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 337 | |
Anders Carlsson | 3068d11 | 2008-11-16 19:01:22 +0000 | [diff] [blame] | 338 | if (!EvaluatePointer(E->getBase(), Result, Info)) |
| 339 | return APValue(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 340 | |
Anders Carlsson | 3068d11 | 2008-11-16 19:01:22 +0000 | [diff] [blame] | 341 | APSInt Index; |
| 342 | if (!EvaluateInteger(E->getIdx(), Index, Info)) |
| 343 | return APValue(); |
| 344 | |
| 345 | uint64_t ElementSize = Info.Ctx.getTypeSize(E->getType()) / 8; |
| 346 | |
| 347 | uint64_t Offset = Index.getSExtValue() * ElementSize; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 348 | Result.setLValue(Result.getLValueBase(), |
Anders Carlsson | 3068d11 | 2008-11-16 19:01:22 +0000 | [diff] [blame] | 349 | Result.getLValueOffset() + Offset); |
| 350 | return Result; |
| 351 | } |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 352 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 353 | APValue LValueExprEvaluator::VisitUnaryDeref(UnaryOperator *E) { |
Eli Friedman | e8761c8 | 2009-02-20 01:57:15 +0000 | [diff] [blame] | 354 | APValue Result; |
| 355 | if (!EvaluatePointer(E->getSubExpr(), Result, Info)) |
| 356 | return APValue(); |
| 357 | return Result; |
| 358 | } |
| 359 | |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 360 | //===----------------------------------------------------------------------===// |
Chris Lattner | f5eeb05 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 361 | // Pointer Evaluation |
| 362 | //===----------------------------------------------------------------------===// |
| 363 | |
Anders Carlsson | c754aa6 | 2008-07-08 05:13:58 +0000 | [diff] [blame] | 364 | namespace { |
Anders Carlsson | 2bad168 | 2008-07-08 14:30:00 +0000 | [diff] [blame] | 365 | class VISIBILITY_HIDDEN PointerExprEvaluator |
| 366 | : public StmtVisitor<PointerExprEvaluator, APValue> { |
Chris Lattner | 87eae5e | 2008-07-11 22:52:41 +0000 | [diff] [blame] | 367 | EvalInfo &Info; |
Anders Carlsson | 2bad168 | 2008-07-08 14:30:00 +0000 | [diff] [blame] | 368 | public: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 369 | |
Chris Lattner | 87eae5e | 2008-07-11 22:52:41 +0000 | [diff] [blame] | 370 | PointerExprEvaluator(EvalInfo &info) : Info(info) {} |
Chris Lattner | f5eeb05 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 371 | |
Anders Carlsson | 2bad168 | 2008-07-08 14:30:00 +0000 | [diff] [blame] | 372 | APValue VisitStmt(Stmt *S) { |
Anders Carlsson | 2bad168 | 2008-07-08 14:30:00 +0000 | [diff] [blame] | 373 | return APValue(); |
| 374 | } |
| 375 | |
| 376 | APValue VisitParenExpr(ParenExpr *E) { return Visit(E->getSubExpr()); } |
| 377 | |
Anders Carlsson | 650c92f | 2008-07-08 15:34:11 +0000 | [diff] [blame] | 378 | APValue VisitBinaryOperator(const BinaryOperator *E); |
| 379 | APValue VisitCastExpr(const CastExpr* E); |
Eli Friedman | 2217c87 | 2009-02-22 11:46:18 +0000 | [diff] [blame] | 380 | APValue VisitUnaryExtension(const UnaryOperator *E) |
| 381 | { return Visit(E->getSubExpr()); } |
| 382 | APValue VisitUnaryAddrOf(const UnaryOperator *E); |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 383 | APValue VisitObjCStringLiteral(ObjCStringLiteral *E) |
| 384 | { return APValue(E, 0); } |
Eli Friedman | f011589 | 2009-01-25 01:21:06 +0000 | [diff] [blame] | 385 | APValue VisitAddrLabelExpr(AddrLabelExpr *E) |
| 386 | { return APValue(E, 0); } |
Eli Friedman | 3941b18 | 2009-01-25 01:54:01 +0000 | [diff] [blame] | 387 | APValue VisitCallExpr(CallExpr *E); |
Mike Stump | b83d287 | 2009-02-19 22:01:56 +0000 | [diff] [blame] | 388 | APValue VisitBlockExpr(BlockExpr *E) { |
| 389 | if (!E->hasBlockDeclRefExprs()) |
| 390 | return APValue(E, 0); |
| 391 | return APValue(); |
| 392 | } |
Eli Friedman | 91110ee | 2009-02-23 04:23:56 +0000 | [diff] [blame] | 393 | APValue VisitImplicitValueInitExpr(ImplicitValueInitExpr *E) |
| 394 | { return APValue((Expr*)0, 0); } |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 395 | APValue VisitConditionalOperator(ConditionalOperator *E); |
Eli Friedman | ba98d6b | 2009-03-23 04:56:01 +0000 | [diff] [blame] | 396 | APValue VisitChooseExpr(ChooseExpr *E) |
Sebastian Redl | 6e8ed16 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 397 | { return Visit(E->getChosenSubExpr(Info.Ctx)); } |
| 398 | APValue VisitCXXNullPtrLiteralExpr(CXXNullPtrLiteralExpr *E) |
| 399 | { return APValue((Expr*)0, 0); } |
Eli Friedman | ba98d6b | 2009-03-23 04:56:01 +0000 | [diff] [blame] | 400 | // FIXME: Missing: @protocol, @selector |
Anders Carlsson | 650c92f | 2008-07-08 15:34:11 +0000 | [diff] [blame] | 401 | }; |
Chris Lattner | f5eeb05 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 402 | } // end anonymous namespace |
Anders Carlsson | 650c92f | 2008-07-08 15:34:11 +0000 | [diff] [blame] | 403 | |
Chris Lattner | 87eae5e | 2008-07-11 22:52:41 +0000 | [diff] [blame] | 404 | static bool EvaluatePointer(const Expr* E, APValue& Result, EvalInfo &Info) { |
Daniel Dunbar | 8958891 | 2009-02-26 20:52:22 +0000 | [diff] [blame] | 405 | if (!E->getType()->hasPointerRepresentation()) |
Chris Lattner | f5eeb05 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 406 | return false; |
Chris Lattner | 87eae5e | 2008-07-11 22:52:41 +0000 | [diff] [blame] | 407 | Result = PointerExprEvaluator(Info).Visit(const_cast<Expr*>(E)); |
Chris Lattner | f5eeb05 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 408 | return Result.isLValue(); |
| 409 | } |
| 410 | |
| 411 | APValue PointerExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) { |
| 412 | if (E->getOpcode() != BinaryOperator::Add && |
| 413 | E->getOpcode() != BinaryOperator::Sub) |
| 414 | return APValue(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 415 | |
Chris Lattner | f5eeb05 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 416 | const Expr *PExp = E->getLHS(); |
| 417 | const Expr *IExp = E->getRHS(); |
| 418 | if (IExp->getType()->isPointerType()) |
| 419 | std::swap(PExp, IExp); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 420 | |
Chris Lattner | f5eeb05 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 421 | APValue ResultLValue; |
Chris Lattner | 87eae5e | 2008-07-11 22:52:41 +0000 | [diff] [blame] | 422 | if (!EvaluatePointer(PExp, ResultLValue, Info)) |
Chris Lattner | f5eeb05 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 423 | return APValue(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 424 | |
Chris Lattner | f5eeb05 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 425 | llvm::APSInt AdditionalOffset(32); |
Chris Lattner | 87eae5e | 2008-07-11 22:52:41 +0000 | [diff] [blame] | 426 | if (!EvaluateInteger(IExp, AdditionalOffset, Info)) |
Chris Lattner | f5eeb05 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 427 | return APValue(); |
| 428 | |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 429 | QualType PointeeType = PExp->getType()->getAs<PointerType>()->getPointeeType(); |
Anders Carlsson | 4d4c50d | 2009-02-19 04:55:58 +0000 | [diff] [blame] | 430 | uint64_t SizeOfPointee; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 431 | |
Anders Carlsson | 4d4c50d | 2009-02-19 04:55:58 +0000 | [diff] [blame] | 432 | // Explicitly handle GNU void* and function pointer arithmetic extensions. |
| 433 | if (PointeeType->isVoidType() || PointeeType->isFunctionType()) |
| 434 | SizeOfPointee = 1; |
| 435 | else |
| 436 | SizeOfPointee = Info.Ctx.getTypeSize(PointeeType) / 8; |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 437 | |
Chris Lattner | f5eeb05 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 438 | uint64_t Offset = ResultLValue.getLValueOffset(); |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 439 | |
Chris Lattner | f5eeb05 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 440 | if (E->getOpcode() == BinaryOperator::Add) |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 441 | Offset += AdditionalOffset.getLimitedValue() * SizeOfPointee; |
Chris Lattner | f5eeb05 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 442 | else |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 443 | Offset -= AdditionalOffset.getLimitedValue() * SizeOfPointee; |
| 444 | |
Chris Lattner | f5eeb05 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 445 | return APValue(ResultLValue.getLValueBase(), Offset); |
| 446 | } |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 447 | |
Eli Friedman | 2217c87 | 2009-02-22 11:46:18 +0000 | [diff] [blame] | 448 | APValue PointerExprEvaluator::VisitUnaryAddrOf(const UnaryOperator *E) { |
| 449 | APValue result; |
| 450 | if (EvaluateLValue(E->getSubExpr(), result, Info)) |
| 451 | return result; |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 452 | return APValue(); |
| 453 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 454 | |
Chris Lattner | f5eeb05 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 455 | |
Chris Lattner | b542afe | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 456 | APValue PointerExprEvaluator::VisitCastExpr(const CastExpr* E) { |
Chris Lattner | f5eeb05 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 457 | const Expr* SubExpr = E->getSubExpr(); |
| 458 | |
| 459 | // Check for pointer->pointer cast |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 460 | if (SubExpr->getType()->isPointerType() || |
Anders Carlsson | c1eb14a | 2009-09-15 04:39:46 +0000 | [diff] [blame] | 461 | SubExpr->getType()->isObjCObjectPointerType() || |
| 462 | SubExpr->getType()->isNullPtrType()) { |
Chris Lattner | f5eeb05 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 463 | APValue Result; |
Chris Lattner | 87eae5e | 2008-07-11 22:52:41 +0000 | [diff] [blame] | 464 | if (EvaluatePointer(SubExpr, Result, Info)) |
Chris Lattner | f5eeb05 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 465 | return Result; |
| 466 | return APValue(); |
| 467 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 468 | |
Eli Friedman | d9f4bcd | 2008-07-27 05:46:18 +0000 | [diff] [blame] | 469 | if (SubExpr->getType()->isIntegralType()) { |
Daniel Dunbar | 69ab26a | 2009-02-20 18:22:23 +0000 | [diff] [blame] | 470 | APValue Result; |
| 471 | if (!EvaluateIntegerOrLValue(SubExpr, Result, Info)) |
| 472 | return APValue(); |
| 473 | |
| 474 | if (Result.isInt()) { |
| 475 | Result.getInt().extOrTrunc((unsigned)Info.Ctx.getTypeSize(E->getType())); |
| 476 | return APValue(0, Result.getInt().getZExtValue()); |
Chris Lattner | f5eeb05 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 477 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 478 | |
Daniel Dunbar | 69ab26a | 2009-02-20 18:22:23 +0000 | [diff] [blame] | 479 | // Cast is of an lvalue, no need to change value. |
| 480 | return Result; |
Chris Lattner | f5eeb05 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 481 | } |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 482 | |
| 483 | if (SubExpr->getType()->isFunctionType() || |
Steve Naroff | 3aaa482 | 2009-04-16 19:02:57 +0000 | [diff] [blame] | 484 | SubExpr->getType()->isBlockPointerType() || |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 485 | SubExpr->getType()->isArrayType()) { |
| 486 | APValue Result; |
| 487 | if (EvaluateLValue(SubExpr, Result, Info)) |
| 488 | return Result; |
| 489 | return APValue(); |
| 490 | } |
| 491 | |
Chris Lattner | f5eeb05 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 492 | return APValue(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 493 | } |
Chris Lattner | f5eeb05 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 494 | |
Eli Friedman | 3941b18 | 2009-01-25 01:54:01 +0000 | [diff] [blame] | 495 | APValue PointerExprEvaluator::VisitCallExpr(CallExpr *E) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 496 | if (E->isBuiltinCall(Info.Ctx) == |
Douglas Gregor | 3c385e5 | 2009-02-14 18:57:46 +0000 | [diff] [blame] | 497 | Builtin::BI__builtin___CFStringMakeConstantString) |
Eli Friedman | 3941b18 | 2009-01-25 01:54:01 +0000 | [diff] [blame] | 498 | return APValue(E, 0); |
| 499 | return APValue(); |
| 500 | } |
| 501 | |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 502 | APValue PointerExprEvaluator::VisitConditionalOperator(ConditionalOperator *E) { |
| 503 | bool BoolResult; |
| 504 | if (!HandleConversionToBool(E->getCond(), BoolResult, Info)) |
| 505 | return APValue(); |
| 506 | |
| 507 | Expr* EvalExpr = BoolResult ? E->getTrueExpr() : E->getFalseExpr(); |
| 508 | |
| 509 | APValue Result; |
| 510 | if (EvaluatePointer(EvalExpr, Result, Info)) |
| 511 | return Result; |
| 512 | return APValue(); |
| 513 | } |
Chris Lattner | f5eeb05 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 514 | |
| 515 | //===----------------------------------------------------------------------===// |
Nate Begeman | 59b5da6 | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 516 | // Vector Evaluation |
| 517 | //===----------------------------------------------------------------------===// |
| 518 | |
| 519 | namespace { |
| 520 | class VISIBILITY_HIDDEN VectorExprEvaluator |
| 521 | : public StmtVisitor<VectorExprEvaluator, APValue> { |
| 522 | EvalInfo &Info; |
Eli Friedman | 91110ee | 2009-02-23 04:23:56 +0000 | [diff] [blame] | 523 | APValue GetZeroVector(QualType VecType); |
Nate Begeman | 59b5da6 | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 524 | public: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 525 | |
Nate Begeman | 59b5da6 | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 526 | VectorExprEvaluator(EvalInfo &info) : Info(info) {} |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 527 | |
Nate Begeman | 59b5da6 | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 528 | APValue VisitStmt(Stmt *S) { |
| 529 | return APValue(); |
| 530 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 531 | |
Eli Friedman | 91110ee | 2009-02-23 04:23:56 +0000 | [diff] [blame] | 532 | APValue VisitParenExpr(ParenExpr *E) |
| 533 | { return Visit(E->getSubExpr()); } |
| 534 | APValue VisitUnaryExtension(const UnaryOperator *E) |
| 535 | { return Visit(E->getSubExpr()); } |
| 536 | APValue VisitUnaryPlus(const UnaryOperator *E) |
| 537 | { return Visit(E->getSubExpr()); } |
| 538 | APValue VisitUnaryReal(const UnaryOperator *E) |
| 539 | { return Visit(E->getSubExpr()); } |
| 540 | APValue VisitImplicitValueInitExpr(const ImplicitValueInitExpr *E) |
| 541 | { return GetZeroVector(E->getType()); } |
Nate Begeman | 59b5da6 | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 542 | APValue VisitCastExpr(const CastExpr* E); |
| 543 | APValue VisitCompoundLiteralExpr(const CompoundLiteralExpr *E); |
| 544 | APValue VisitInitListExpr(const InitListExpr *E); |
Eli Friedman | 91110ee | 2009-02-23 04:23:56 +0000 | [diff] [blame] | 545 | APValue VisitConditionalOperator(const ConditionalOperator *E); |
Eli Friedman | ba98d6b | 2009-03-23 04:56:01 +0000 | [diff] [blame] | 546 | APValue VisitChooseExpr(const ChooseExpr *E) |
| 547 | { return Visit(E->getChosenSubExpr(Info.Ctx)); } |
Eli Friedman | 91110ee | 2009-02-23 04:23:56 +0000 | [diff] [blame] | 548 | APValue VisitUnaryImag(const UnaryOperator *E); |
| 549 | // FIXME: Missing: unary -, unary ~, binary add/sub/mul/div, |
Eli Friedman | 2217c87 | 2009-02-22 11:46:18 +0000 | [diff] [blame] | 550 | // binary comparisons, binary and/or/xor, |
Eli Friedman | 91110ee | 2009-02-23 04:23:56 +0000 | [diff] [blame] | 551 | // shufflevector, ExtVectorElementExpr |
| 552 | // (Note that these require implementing conversions |
| 553 | // between vector types.) |
Nate Begeman | 59b5da6 | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 554 | }; |
| 555 | } // end anonymous namespace |
| 556 | |
| 557 | static bool EvaluateVector(const Expr* E, APValue& Result, EvalInfo &Info) { |
| 558 | if (!E->getType()->isVectorType()) |
| 559 | return false; |
| 560 | Result = VectorExprEvaluator(Info).Visit(const_cast<Expr*>(E)); |
| 561 | return !Result.isUninit(); |
| 562 | } |
| 563 | |
| 564 | APValue VectorExprEvaluator::VisitCastExpr(const CastExpr* E) { |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 565 | const VectorType *VTy = E->getType()->getAs<VectorType>(); |
Nate Begeman | c0b8b19 | 2009-07-01 07:50:47 +0000 | [diff] [blame] | 566 | QualType EltTy = VTy->getElementType(); |
| 567 | unsigned NElts = VTy->getNumElements(); |
| 568 | unsigned EltWidth = Info.Ctx.getTypeSize(EltTy); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 569 | |
Nate Begeman | 59b5da6 | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 570 | const Expr* SE = E->getSubExpr(); |
Nate Begeman | e8c9e92 | 2009-06-26 18:22:18 +0000 | [diff] [blame] | 571 | QualType SETy = SE->getType(); |
| 572 | APValue Result = APValue(); |
Nate Begeman | 59b5da6 | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 573 | |
Nate Begeman | e8c9e92 | 2009-06-26 18:22:18 +0000 | [diff] [blame] | 574 | // Check for vector->vector bitcast and scalar->vector splat. |
| 575 | if (SETy->isVectorType()) { |
Nate Begeman | 59b5da6 | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 576 | return this->Visit(const_cast<Expr*>(SE)); |
Nate Begeman | e8c9e92 | 2009-06-26 18:22:18 +0000 | [diff] [blame] | 577 | } else if (SETy->isIntegerType()) { |
| 578 | APSInt IntResult; |
Daniel Dunbar | d906dc7 | 2009-07-01 20:37:45 +0000 | [diff] [blame] | 579 | if (!EvaluateInteger(SE, IntResult, Info)) |
| 580 | return APValue(); |
| 581 | Result = APValue(IntResult); |
Nate Begeman | e8c9e92 | 2009-06-26 18:22:18 +0000 | [diff] [blame] | 582 | } else if (SETy->isRealFloatingType()) { |
| 583 | APFloat F(0.0); |
Daniel Dunbar | d906dc7 | 2009-07-01 20:37:45 +0000 | [diff] [blame] | 584 | if (!EvaluateFloat(SE, F, Info)) |
| 585 | return APValue(); |
| 586 | Result = APValue(F); |
| 587 | } else |
Nate Begeman | c0b8b19 | 2009-07-01 07:50:47 +0000 | [diff] [blame] | 588 | return APValue(); |
Nate Begeman | 59b5da6 | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 589 | |
Nate Begeman | c0b8b19 | 2009-07-01 07:50:47 +0000 | [diff] [blame] | 590 | // For casts of a scalar to ExtVector, convert the scalar to the element type |
| 591 | // and splat it to all elements. |
| 592 | if (E->getType()->isExtVectorType()) { |
| 593 | if (EltTy->isIntegerType() && Result.isInt()) |
| 594 | Result = APValue(HandleIntToIntCast(EltTy, SETy, Result.getInt(), |
| 595 | Info.Ctx)); |
| 596 | else if (EltTy->isIntegerType()) |
| 597 | Result = APValue(HandleFloatToIntCast(EltTy, SETy, Result.getFloat(), |
| 598 | Info.Ctx)); |
| 599 | else if (EltTy->isRealFloatingType() && Result.isInt()) |
| 600 | Result = APValue(HandleIntToFloatCast(EltTy, SETy, Result.getInt(), |
| 601 | Info.Ctx)); |
| 602 | else if (EltTy->isRealFloatingType()) |
| 603 | Result = APValue(HandleFloatToFloatCast(EltTy, SETy, Result.getFloat(), |
| 604 | Info.Ctx)); |
| 605 | else |
| 606 | return APValue(); |
| 607 | |
| 608 | // Splat and create vector APValue. |
| 609 | llvm::SmallVector<APValue, 4> Elts(NElts, Result); |
| 610 | return APValue(&Elts[0], Elts.size()); |
Nate Begeman | e8c9e92 | 2009-06-26 18:22:18 +0000 | [diff] [blame] | 611 | } |
Nate Begeman | c0b8b19 | 2009-07-01 07:50:47 +0000 | [diff] [blame] | 612 | |
| 613 | // For casts of a scalar to regular gcc-style vector type, bitcast the scalar |
| 614 | // to the vector. To construct the APValue vector initializer, bitcast the |
| 615 | // initializing value to an APInt, and shift out the bits pertaining to each |
| 616 | // element. |
| 617 | APSInt Init; |
| 618 | Init = Result.isInt() ? Result.getInt() : Result.getFloat().bitcastToAPInt(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 619 | |
Nate Begeman | c0b8b19 | 2009-07-01 07:50:47 +0000 | [diff] [blame] | 620 | llvm::SmallVector<APValue, 4> Elts; |
| 621 | for (unsigned i = 0; i != NElts; ++i) { |
| 622 | APSInt Tmp = Init; |
| 623 | Tmp.extOrTrunc(EltWidth); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 624 | |
Nate Begeman | c0b8b19 | 2009-07-01 07:50:47 +0000 | [diff] [blame] | 625 | if (EltTy->isIntegerType()) |
| 626 | Elts.push_back(APValue(Tmp)); |
| 627 | else if (EltTy->isRealFloatingType()) |
| 628 | Elts.push_back(APValue(APFloat(Tmp))); |
| 629 | else |
| 630 | return APValue(); |
| 631 | |
| 632 | Init >>= EltWidth; |
| 633 | } |
| 634 | return APValue(&Elts[0], Elts.size()); |
Nate Begeman | 59b5da6 | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 635 | } |
| 636 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 637 | APValue |
Nate Begeman | 59b5da6 | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 638 | VectorExprEvaluator::VisitCompoundLiteralExpr(const CompoundLiteralExpr *E) { |
| 639 | return this->Visit(const_cast<Expr*>(E->getInitializer())); |
| 640 | } |
| 641 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 642 | APValue |
Nate Begeman | 59b5da6 | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 643 | VectorExprEvaluator::VisitInitListExpr(const InitListExpr *E) { |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 644 | const VectorType *VT = E->getType()->getAs<VectorType>(); |
Nate Begeman | 59b5da6 | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 645 | unsigned NumInits = E->getNumInits(); |
Eli Friedman | 91110ee | 2009-02-23 04:23:56 +0000 | [diff] [blame] | 646 | unsigned NumElements = VT->getNumElements(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 647 | |
Nate Begeman | 59b5da6 | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 648 | QualType EltTy = VT->getElementType(); |
| 649 | llvm::SmallVector<APValue, 4> Elements; |
| 650 | |
Eli Friedman | 91110ee | 2009-02-23 04:23:56 +0000 | [diff] [blame] | 651 | for (unsigned i = 0; i < NumElements; i++) { |
Nate Begeman | 59b5da6 | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 652 | if (EltTy->isIntegerType()) { |
| 653 | llvm::APSInt sInt(32); |
Eli Friedman | 91110ee | 2009-02-23 04:23:56 +0000 | [diff] [blame] | 654 | if (i < NumInits) { |
| 655 | if (!EvaluateInteger(E->getInit(i), sInt, Info)) |
| 656 | return APValue(); |
| 657 | } else { |
| 658 | sInt = Info.Ctx.MakeIntValue(0, EltTy); |
| 659 | } |
Nate Begeman | 59b5da6 | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 660 | Elements.push_back(APValue(sInt)); |
| 661 | } else { |
| 662 | llvm::APFloat f(0.0); |
Eli Friedman | 91110ee | 2009-02-23 04:23:56 +0000 | [diff] [blame] | 663 | if (i < NumInits) { |
| 664 | if (!EvaluateFloat(E->getInit(i), f, Info)) |
| 665 | return APValue(); |
| 666 | } else { |
| 667 | f = APFloat::getZero(Info.Ctx.getFloatTypeSemantics(EltTy)); |
| 668 | } |
Nate Begeman | 59b5da6 | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 669 | Elements.push_back(APValue(f)); |
| 670 | } |
| 671 | } |
| 672 | return APValue(&Elements[0], Elements.size()); |
| 673 | } |
| 674 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 675 | APValue |
Eli Friedman | 91110ee | 2009-02-23 04:23:56 +0000 | [diff] [blame] | 676 | VectorExprEvaluator::GetZeroVector(QualType T) { |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 677 | const VectorType *VT = T->getAs<VectorType>(); |
Eli Friedman | 91110ee | 2009-02-23 04:23:56 +0000 | [diff] [blame] | 678 | QualType EltTy = VT->getElementType(); |
| 679 | APValue ZeroElement; |
| 680 | if (EltTy->isIntegerType()) |
| 681 | ZeroElement = APValue(Info.Ctx.MakeIntValue(0, EltTy)); |
| 682 | else |
| 683 | ZeroElement = |
| 684 | APValue(APFloat::getZero(Info.Ctx.getFloatTypeSemantics(EltTy))); |
| 685 | |
| 686 | llvm::SmallVector<APValue, 4> Elements(VT->getNumElements(), ZeroElement); |
| 687 | return APValue(&Elements[0], Elements.size()); |
| 688 | } |
| 689 | |
| 690 | APValue VectorExprEvaluator::VisitConditionalOperator(const ConditionalOperator *E) { |
| 691 | bool BoolResult; |
| 692 | if (!HandleConversionToBool(E->getCond(), BoolResult, Info)) |
| 693 | return APValue(); |
| 694 | |
| 695 | Expr* EvalExpr = BoolResult ? E->getTrueExpr() : E->getFalseExpr(); |
| 696 | |
| 697 | APValue Result; |
| 698 | if (EvaluateVector(EvalExpr, Result, Info)) |
| 699 | return Result; |
| 700 | return APValue(); |
| 701 | } |
| 702 | |
Eli Friedman | 91110ee | 2009-02-23 04:23:56 +0000 | [diff] [blame] | 703 | APValue VectorExprEvaluator::VisitUnaryImag(const UnaryOperator *E) { |
| 704 | if (!E->getSubExpr()->isEvaluatable(Info.Ctx)) |
| 705 | Info.EvalResult.HasSideEffects = true; |
| 706 | return GetZeroVector(E->getType()); |
| 707 | } |
| 708 | |
Nate Begeman | 59b5da6 | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 709 | //===----------------------------------------------------------------------===// |
Chris Lattner | f5eeb05 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 710 | // Integer Evaluation |
| 711 | //===----------------------------------------------------------------------===// |
Chris Lattner | f5eeb05 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 712 | |
| 713 | namespace { |
Anders Carlsson | c754aa6 | 2008-07-08 05:13:58 +0000 | [diff] [blame] | 714 | class VISIBILITY_HIDDEN IntExprEvaluator |
Chris Lattner | b542afe | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 715 | : public StmtVisitor<IntExprEvaluator, bool> { |
Chris Lattner | 87eae5e | 2008-07-11 22:52:41 +0000 | [diff] [blame] | 716 | EvalInfo &Info; |
Daniel Dunbar | 30c37f4 | 2009-02-19 20:17:33 +0000 | [diff] [blame] | 717 | APValue &Result; |
Anders Carlsson | c754aa6 | 2008-07-08 05:13:58 +0000 | [diff] [blame] | 718 | public: |
Daniel Dunbar | 30c37f4 | 2009-02-19 20:17:33 +0000 | [diff] [blame] | 719 | IntExprEvaluator(EvalInfo &info, APValue &result) |
Chris Lattner | 87eae5e | 2008-07-11 22:52:41 +0000 | [diff] [blame] | 720 | : Info(info), Result(result) {} |
Chris Lattner | f5eeb05 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 721 | |
Daniel Dunbar | 3f7d995 | 2009-02-19 18:37:50 +0000 | [diff] [blame] | 722 | bool Success(const llvm::APSInt &SI, const Expr *E) { |
Daniel Dunbar | 4fff481 | 2009-02-21 18:14:20 +0000 | [diff] [blame] | 723 | assert(E->getType()->isIntegralType() && "Invalid evaluation result."); |
Daniel Dunbar | 30c37f4 | 2009-02-19 20:17:33 +0000 | [diff] [blame] | 724 | assert(SI.isSigned() == E->getType()->isSignedIntegerType() && |
Daniel Dunbar | 3f7d995 | 2009-02-19 18:37:50 +0000 | [diff] [blame] | 725 | "Invalid evaluation result."); |
Daniel Dunbar | 30c37f4 | 2009-02-19 20:17:33 +0000 | [diff] [blame] | 726 | assert(SI.getBitWidth() == Info.Ctx.getIntWidth(E->getType()) && |
Daniel Dunbar | 3f7d995 | 2009-02-19 18:37:50 +0000 | [diff] [blame] | 727 | "Invalid evaluation result."); |
Daniel Dunbar | 30c37f4 | 2009-02-19 20:17:33 +0000 | [diff] [blame] | 728 | Result = APValue(SI); |
Daniel Dunbar | 3f7d995 | 2009-02-19 18:37:50 +0000 | [diff] [blame] | 729 | return true; |
| 730 | } |
| 731 | |
Daniel Dunbar | 131eb43 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 732 | bool Success(const llvm::APInt &I, const Expr *E) { |
Daniel Dunbar | 4fff481 | 2009-02-21 18:14:20 +0000 | [diff] [blame] | 733 | assert(E->getType()->isIntegralType() && "Invalid evaluation result."); |
Daniel Dunbar | 30c37f4 | 2009-02-19 20:17:33 +0000 | [diff] [blame] | 734 | assert(I.getBitWidth() == Info.Ctx.getIntWidth(E->getType()) && |
Daniel Dunbar | 3f7d995 | 2009-02-19 18:37:50 +0000 | [diff] [blame] | 735 | "Invalid evaluation result."); |
Daniel Dunbar | 30c37f4 | 2009-02-19 20:17:33 +0000 | [diff] [blame] | 736 | Result = APValue(APSInt(I)); |
| 737 | Result.getInt().setIsUnsigned(E->getType()->isUnsignedIntegerType()); |
Daniel Dunbar | 131eb43 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 738 | return true; |
| 739 | } |
| 740 | |
| 741 | bool Success(uint64_t Value, const Expr *E) { |
Daniel Dunbar | 4fff481 | 2009-02-21 18:14:20 +0000 | [diff] [blame] | 742 | assert(E->getType()->isIntegralType() && "Invalid evaluation result."); |
Daniel Dunbar | 30c37f4 | 2009-02-19 20:17:33 +0000 | [diff] [blame] | 743 | Result = APValue(Info.Ctx.MakeIntValue(Value, E->getType())); |
Daniel Dunbar | 131eb43 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 744 | return true; |
| 745 | } |
| 746 | |
Anders Carlsson | 82206e2 | 2008-11-30 18:14:57 +0000 | [diff] [blame] | 747 | bool Error(SourceLocation L, diag::kind D, const Expr *E) { |
Chris Lattner | 32fea9d | 2008-11-12 07:43:42 +0000 | [diff] [blame] | 748 | // Take the first error. |
Anders Carlsson | 54da049 | 2008-11-30 16:38:33 +0000 | [diff] [blame] | 749 | if (Info.EvalResult.Diag == 0) { |
| 750 | Info.EvalResult.DiagLoc = L; |
| 751 | Info.EvalResult.Diag = D; |
Anders Carlsson | 82206e2 | 2008-11-30 18:14:57 +0000 | [diff] [blame] | 752 | Info.EvalResult.DiagExpr = E; |
Chris Lattner | 32fea9d | 2008-11-12 07:43:42 +0000 | [diff] [blame] | 753 | } |
Chris Lattner | 54176fd | 2008-07-12 00:14:42 +0000 | [diff] [blame] | 754 | return false; |
Chris Lattner | 7a76778 | 2008-07-11 19:24:49 +0000 | [diff] [blame] | 755 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 756 | |
Anders Carlsson | c754aa6 | 2008-07-08 05:13:58 +0000 | [diff] [blame] | 757 | //===--------------------------------------------------------------------===// |
| 758 | // Visitor Methods |
| 759 | //===--------------------------------------------------------------------===// |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 760 | |
Chris Lattner | 32fea9d | 2008-11-12 07:43:42 +0000 | [diff] [blame] | 761 | bool VisitStmt(Stmt *) { |
| 762 | assert(0 && "This should be called on integers, stmts are not integers"); |
| 763 | return false; |
| 764 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 765 | |
Chris Lattner | 32fea9d | 2008-11-12 07:43:42 +0000 | [diff] [blame] | 766 | bool VisitExpr(Expr *E) { |
Anders Carlsson | 0e8acbb | 2008-11-30 18:37:00 +0000 | [diff] [blame] | 767 | return Error(E->getLocStart(), diag::note_invalid_subexpr_in_ice, E); |
Anders Carlsson | c754aa6 | 2008-07-08 05:13:58 +0000 | [diff] [blame] | 768 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 769 | |
Chris Lattner | b542afe | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 770 | bool VisitParenExpr(ParenExpr *E) { return Visit(E->getSubExpr()); } |
Anders Carlsson | c754aa6 | 2008-07-08 05:13:58 +0000 | [diff] [blame] | 771 | |
Chris Lattner | 4c4867e | 2008-07-12 00:38:25 +0000 | [diff] [blame] | 772 | bool VisitIntegerLiteral(const IntegerLiteral *E) { |
Daniel Dunbar | 131eb43 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 773 | return Success(E->getValue(), E); |
Chris Lattner | 4c4867e | 2008-07-12 00:38:25 +0000 | [diff] [blame] | 774 | } |
| 775 | bool VisitCharacterLiteral(const CharacterLiteral *E) { |
Daniel Dunbar | 131eb43 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 776 | return Success(E->getValue(), E); |
Chris Lattner | 4c4867e | 2008-07-12 00:38:25 +0000 | [diff] [blame] | 777 | } |
| 778 | bool VisitTypesCompatibleExpr(const TypesCompatibleExpr *E) { |
Daniel Dunbar | ac620de | 2008-10-24 08:07:57 +0000 | [diff] [blame] | 779 | // Per gcc docs "this built-in function ignores top level |
| 780 | // qualifiers". We need to use the canonical version to properly |
| 781 | // be able to strip CRV qualifiers from the type. |
| 782 | QualType T0 = Info.Ctx.getCanonicalType(E->getArgType1()); |
| 783 | QualType T1 = Info.Ctx.getCanonicalType(E->getArgType2()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 784 | return Success(Info.Ctx.typesAreCompatible(T0.getUnqualifiedType(), |
Daniel Dunbar | 131eb43 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 785 | T1.getUnqualifiedType()), |
| 786 | E); |
Chris Lattner | 4c4867e | 2008-07-12 00:38:25 +0000 | [diff] [blame] | 787 | } |
| 788 | bool VisitDeclRefExpr(const DeclRefExpr *E); |
| 789 | bool VisitCallExpr(const CallExpr *E); |
Chris Lattner | b542afe | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 790 | bool VisitBinaryOperator(const BinaryOperator *E); |
| 791 | bool VisitUnaryOperator(const UnaryOperator *E); |
Nuno Lopes | ca7c2ea | 2008-11-16 19:28:31 +0000 | [diff] [blame] | 792 | bool VisitConditionalOperator(const ConditionalOperator *E); |
Anders Carlsson | 06a3675 | 2008-07-08 05:49:43 +0000 | [diff] [blame] | 793 | |
Daniel Dunbar | a2cfd34 | 2009-01-29 06:16:07 +0000 | [diff] [blame] | 794 | bool VisitCastExpr(CastExpr* E); |
Sebastian Redl | 0518999 | 2008-11-11 17:56:53 +0000 | [diff] [blame] | 795 | bool VisitSizeOfAlignOfExpr(const SizeOfAlignOfExpr *E); |
| 796 | |
Anders Carlsson | 3068d11 | 2008-11-16 19:01:22 +0000 | [diff] [blame] | 797 | bool VisitCXXBoolLiteralExpr(const CXXBoolLiteralExpr *E) { |
Daniel Dunbar | 131eb43 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 798 | return Success(E->getValue(), E); |
Anders Carlsson | 3068d11 | 2008-11-16 19:01:22 +0000 | [diff] [blame] | 799 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 800 | |
Anders Carlsson | 3f70456 | 2008-12-21 22:39:40 +0000 | [diff] [blame] | 801 | bool VisitGNUNullExpr(const GNUNullExpr *E) { |
Daniel Dunbar | 131eb43 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 802 | return Success(0, E); |
Anders Carlsson | 3f70456 | 2008-12-21 22:39:40 +0000 | [diff] [blame] | 803 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 804 | |
Anders Carlsson | 3068d11 | 2008-11-16 19:01:22 +0000 | [diff] [blame] | 805 | bool VisitCXXZeroInitValueExpr(const CXXZeroInitValueExpr *E) { |
Daniel Dunbar | 131eb43 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 806 | return Success(0, E); |
Anders Carlsson | 3068d11 | 2008-11-16 19:01:22 +0000 | [diff] [blame] | 807 | } |
| 808 | |
Eli Friedman | 664a104 | 2009-02-27 04:45:43 +0000 | [diff] [blame] | 809 | bool VisitImplicitValueInitExpr(const ImplicitValueInitExpr *E) { |
| 810 | return Success(0, E); |
| 811 | } |
| 812 | |
Sebastian Redl | 64b45f7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 813 | bool VisitUnaryTypeTraitExpr(const UnaryTypeTraitExpr *E) { |
Douglas Gregor | 5e03f9e | 2009-07-23 23:49:00 +0000 | [diff] [blame] | 814 | return Success(E->EvaluateTrait(Info.Ctx), E); |
Sebastian Redl | 64b45f7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 815 | } |
| 816 | |
Eli Friedman | ba98d6b | 2009-03-23 04:56:01 +0000 | [diff] [blame] | 817 | bool VisitChooseExpr(const ChooseExpr *E) { |
| 818 | return Visit(E->getChosenSubExpr(Info.Ctx)); |
| 819 | } |
| 820 | |
Eli Friedman | 722c717 | 2009-02-28 03:59:05 +0000 | [diff] [blame] | 821 | bool VisitUnaryReal(const UnaryOperator *E); |
Eli Friedman | 664a104 | 2009-02-27 04:45:43 +0000 | [diff] [blame] | 822 | bool VisitUnaryImag(const UnaryOperator *E); |
| 823 | |
Chris Lattner | fcee001 | 2008-07-11 21:24:13 +0000 | [diff] [blame] | 824 | private: |
Chris Lattner | af707ab | 2009-01-24 21:53:27 +0000 | [diff] [blame] | 825 | unsigned GetAlignOfExpr(const Expr *E); |
| 826 | unsigned GetAlignOfType(QualType T); |
Eli Friedman | 664a104 | 2009-02-27 04:45:43 +0000 | [diff] [blame] | 827 | // FIXME: Missing: array subscript of vector, member of vector |
Anders Carlsson | a25ae3d | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 828 | }; |
Chris Lattner | f5eeb05 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 829 | } // end anonymous namespace |
Anders Carlsson | 650c92f | 2008-07-08 15:34:11 +0000 | [diff] [blame] | 830 | |
Daniel Dunbar | 69ab26a | 2009-02-20 18:22:23 +0000 | [diff] [blame] | 831 | static bool EvaluateIntegerOrLValue(const Expr* E, APValue &Result, EvalInfo &Info) { |
Daniel Dunbar | 3f7d995 | 2009-02-19 18:37:50 +0000 | [diff] [blame] | 832 | if (!E->getType()->isIntegralType()) |
| 833 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 834 | |
Daniel Dunbar | 69ab26a | 2009-02-20 18:22:23 +0000 | [diff] [blame] | 835 | return IntExprEvaluator(Info, Result).Visit(const_cast<Expr*>(E)); |
| 836 | } |
Daniel Dunbar | 30c37f4 | 2009-02-19 20:17:33 +0000 | [diff] [blame] | 837 | |
Daniel Dunbar | 69ab26a | 2009-02-20 18:22:23 +0000 | [diff] [blame] | 838 | static bool EvaluateInteger(const Expr* E, APSInt &Result, EvalInfo &Info) { |
| 839 | APValue Val; |
| 840 | if (!EvaluateIntegerOrLValue(E, Val, Info) || !Val.isInt()) |
| 841 | return false; |
Daniel Dunbar | 30c37f4 | 2009-02-19 20:17:33 +0000 | [diff] [blame] | 842 | Result = Val.getInt(); |
| 843 | return true; |
Anders Carlsson | 650c92f | 2008-07-08 15:34:11 +0000 | [diff] [blame] | 844 | } |
Anders Carlsson | 650c92f | 2008-07-08 15:34:11 +0000 | [diff] [blame] | 845 | |
Chris Lattner | 4c4867e | 2008-07-12 00:38:25 +0000 | [diff] [blame] | 846 | bool IntExprEvaluator::VisitDeclRefExpr(const DeclRefExpr *E) { |
| 847 | // Enums are integer constant exprs. |
| 848 | if (const EnumConstantDecl *D = dyn_cast<EnumConstantDecl>(E->getDecl())) { |
Eli Friedman | e9a0f43 | 2008-12-08 02:21:03 +0000 | [diff] [blame] | 849 | // FIXME: This is an ugly hack around the fact that enums don't set their |
Daniel Dunbar | 3f7d995 | 2009-02-19 18:37:50 +0000 | [diff] [blame] | 850 | // signedness consistently; see PR3173. |
| 851 | APSInt SI = D->getInitVal(); |
| 852 | SI.setIsUnsigned(!E->getType()->isSignedIntegerType()); |
| 853 | // FIXME: This is an ugly hack around the fact that enums don't |
| 854 | // set their width (!?!) consistently; see PR3173. |
| 855 | SI.extOrTrunc(Info.Ctx.getIntWidth(E->getType())); |
| 856 | return Success(SI, E); |
Chris Lattner | 4c4867e | 2008-07-12 00:38:25 +0000 | [diff] [blame] | 857 | } |
Sebastian Redl | b2bc62b | 2009-02-08 15:51:17 +0000 | [diff] [blame] | 858 | |
| 859 | // In C++, const, non-volatile integers initialized with ICEs are ICEs. |
Eli Friedman | e1646da | 2009-03-30 23:39:01 +0000 | [diff] [blame] | 860 | // In C, they can also be folded, although they are not ICEs. |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 861 | if (E->getType().getCVRQualifiers() == Qualifiers::Const) { |
Sebastian Redl | b2bc62b | 2009-02-08 15:51:17 +0000 | [diff] [blame] | 862 | if (const VarDecl *D = dyn_cast<VarDecl>(E->getDecl())) { |
Douglas Gregor | 78d1583 | 2009-05-26 18:54:04 +0000 | [diff] [blame] | 863 | if (APValue *V = D->getEvaluatedValue()) |
| 864 | return Success(V->getInt(), E); |
| 865 | if (const Expr *Init = D->getInit()) { |
| 866 | if (Visit(const_cast<Expr*>(Init))) { |
| 867 | // Cache the evaluated value in the variable declaration. |
| 868 | D->setEvaluatedValue(Info.Ctx, Result); |
| 869 | return true; |
| 870 | } |
| 871 | |
| 872 | return false; |
| 873 | } |
Sebastian Redl | b2bc62b | 2009-02-08 15:51:17 +0000 | [diff] [blame] | 874 | } |
| 875 | } |
| 876 | |
Chris Lattner | 4c4867e | 2008-07-12 00:38:25 +0000 | [diff] [blame] | 877 | // Otherwise, random variable references are not constants. |
Anders Carlsson | 0e8acbb | 2008-11-30 18:37:00 +0000 | [diff] [blame] | 878 | return Error(E->getLocStart(), diag::note_invalid_subexpr_in_ice, E); |
Chris Lattner | 4c4867e | 2008-07-12 00:38:25 +0000 | [diff] [blame] | 879 | } |
| 880 | |
Chris Lattner | a4d55d8 | 2008-10-06 06:40:35 +0000 | [diff] [blame] | 881 | /// EvaluateBuiltinClassifyType - Evaluate __builtin_classify_type the same way |
| 882 | /// as GCC. |
| 883 | static int EvaluateBuiltinClassifyType(const CallExpr *E) { |
| 884 | // The following enum mimics the values returned by GCC. |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 885 | // FIXME: Does GCC differ between lvalue and rvalue references here? |
Chris Lattner | a4d55d8 | 2008-10-06 06:40:35 +0000 | [diff] [blame] | 886 | enum gcc_type_class { |
| 887 | no_type_class = -1, |
| 888 | void_type_class, integer_type_class, char_type_class, |
| 889 | enumeral_type_class, boolean_type_class, |
| 890 | pointer_type_class, reference_type_class, offset_type_class, |
| 891 | real_type_class, complex_type_class, |
| 892 | function_type_class, method_type_class, |
| 893 | record_type_class, union_type_class, |
| 894 | array_type_class, string_type_class, |
| 895 | lang_type_class |
| 896 | }; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 897 | |
| 898 | // If no argument was supplied, default to "no_type_class". This isn't |
Chris Lattner | a4d55d8 | 2008-10-06 06:40:35 +0000 | [diff] [blame] | 899 | // ideal, however it is what gcc does. |
| 900 | if (E->getNumArgs() == 0) |
| 901 | return no_type_class; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 902 | |
Chris Lattner | a4d55d8 | 2008-10-06 06:40:35 +0000 | [diff] [blame] | 903 | QualType ArgTy = E->getArg(0)->getType(); |
| 904 | if (ArgTy->isVoidType()) |
| 905 | return void_type_class; |
| 906 | else if (ArgTy->isEnumeralType()) |
| 907 | return enumeral_type_class; |
| 908 | else if (ArgTy->isBooleanType()) |
| 909 | return boolean_type_class; |
| 910 | else if (ArgTy->isCharType()) |
| 911 | return string_type_class; // gcc doesn't appear to use char_type_class |
| 912 | else if (ArgTy->isIntegerType()) |
| 913 | return integer_type_class; |
| 914 | else if (ArgTy->isPointerType()) |
| 915 | return pointer_type_class; |
| 916 | else if (ArgTy->isReferenceType()) |
| 917 | return reference_type_class; |
| 918 | else if (ArgTy->isRealType()) |
| 919 | return real_type_class; |
| 920 | else if (ArgTy->isComplexType()) |
| 921 | return complex_type_class; |
| 922 | else if (ArgTy->isFunctionType()) |
| 923 | return function_type_class; |
| 924 | else if (ArgTy->isStructureType()) |
| 925 | return record_type_class; |
| 926 | else if (ArgTy->isUnionType()) |
| 927 | return union_type_class; |
| 928 | else if (ArgTy->isArrayType()) |
| 929 | return array_type_class; |
| 930 | else if (ArgTy->isUnionType()) |
| 931 | return union_type_class; |
| 932 | else // FIXME: offset_type_class, method_type_class, & lang_type_class? |
| 933 | assert(0 && "CallExpr::isBuiltinClassifyType(): unimplemented type"); |
| 934 | return -1; |
| 935 | } |
| 936 | |
Chris Lattner | 4c4867e | 2008-07-12 00:38:25 +0000 | [diff] [blame] | 937 | bool IntExprEvaluator::VisitCallExpr(const CallExpr *E) { |
Douglas Gregor | 3c385e5 | 2009-02-14 18:57:46 +0000 | [diff] [blame] | 938 | switch (E->isBuiltinCall(Info.Ctx)) { |
Chris Lattner | 019f4e8 | 2008-10-06 05:28:25 +0000 | [diff] [blame] | 939 | default: |
Anders Carlsson | 0e8acbb | 2008-11-30 18:37:00 +0000 | [diff] [blame] | 940 | return Error(E->getLocStart(), diag::note_invalid_subexpr_in_ice, E); |
Mike Stump | 64eda9e | 2009-10-26 18:35:08 +0000 | [diff] [blame] | 941 | |
| 942 | case Builtin::BI__builtin_object_size: { |
Mike Stump | 64eda9e | 2009-10-26 18:35:08 +0000 | [diff] [blame] | 943 | const Expr *Arg = E->getArg(0)->IgnoreParens(); |
| 944 | Expr::EvalResult Base; |
Mike Stump | 660e6f7 | 2009-10-26 23:05:19 +0000 | [diff] [blame] | 945 | if (Arg->EvaluateAsAny(Base, Info.Ctx) |
Mike Stump | 64eda9e | 2009-10-26 18:35:08 +0000 | [diff] [blame] | 946 | && Base.Val.getKind() == APValue::LValue |
| 947 | && !Base.HasSideEffects) |
| 948 | if (const Expr *LVBase = Base.Val.getLValueBase()) |
| 949 | if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(LVBase)) { |
| 950 | if (const VarDecl *VD = dyn_cast<VarDecl>(DRE->getDecl())) { |
Mike Stump | 460d138 | 2009-10-28 21:22:24 +0000 | [diff] [blame] | 951 | if (!VD->getType()->isIncompleteType() |
| 952 | && VD->getType()->isObjectType() |
| 953 | && !VD->getType()->isVariablyModifiedType() |
| 954 | && !VD->getType()->isDependentType()) { |
| 955 | uint64_t Size = Info.Ctx.getTypeSize(VD->getType()) / 8; |
| 956 | uint64_t Offset = Base.Val.getLValueOffset(); |
| 957 | if (Offset <= Size) |
| 958 | Size -= Base.Val.getLValueOffset(); |
| 959 | else |
| 960 | Size = 0; |
| 961 | return Success(Size, E); |
| 962 | } |
Mike Stump | 64eda9e | 2009-10-26 18:35:08 +0000 | [diff] [blame] | 963 | } |
| 964 | } |
| 965 | |
Mike Stump | c4c9045 | 2009-10-27 22:09:17 +0000 | [diff] [blame] | 966 | if (HasSideEffects(E->getArg(0), Info.Ctx)) { |
Mike Stump | 4fc8758 | 2009-10-26 18:57:47 +0000 | [diff] [blame] | 967 | if (E->getArg(1)->EvaluateAsInt(Info.Ctx).getZExtValue() < 2) |
Mike Stump | 64eda9e | 2009-10-26 18:35:08 +0000 | [diff] [blame] | 968 | return Success(-1, E); |
| 969 | return Success(0, E); |
| 970 | } |
Mike Stump | c4c9045 | 2009-10-27 22:09:17 +0000 | [diff] [blame] | 971 | |
Mike Stump | 64eda9e | 2009-10-26 18:35:08 +0000 | [diff] [blame] | 972 | return Error(E->getLocStart(), diag::note_invalid_subexpr_in_ice, E); |
| 973 | } |
| 974 | |
Chris Lattner | 019f4e8 | 2008-10-06 05:28:25 +0000 | [diff] [blame] | 975 | case Builtin::BI__builtin_classify_type: |
Daniel Dunbar | 131eb43 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 976 | return Success(EvaluateBuiltinClassifyType(E), E); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 977 | |
Anders Carlsson | 4bbc0e0 | 2008-11-24 04:21:33 +0000 | [diff] [blame] | 978 | case Builtin::BI__builtin_constant_p: |
Chris Lattner | 019f4e8 | 2008-10-06 05:28:25 +0000 | [diff] [blame] | 979 | // __builtin_constant_p always has one operand: it returns true if that |
| 980 | // operand can be folded, false otherwise. |
Daniel Dunbar | 131eb43 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 981 | return Success(E->getArg(0)->isEvaluatable(Info.Ctx), E); |
Chris Lattner | 21fb98e | 2009-09-23 06:06:36 +0000 | [diff] [blame] | 982 | |
| 983 | case Builtin::BI__builtin_eh_return_data_regno: { |
| 984 | int Operand = E->getArg(0)->EvaluateAsInt(Info.Ctx).getZExtValue(); |
| 985 | Operand = Info.Ctx.Target.getEHDataRegisterNumber(Operand); |
| 986 | return Success(Operand, E); |
| 987 | } |
Chris Lattner | 019f4e8 | 2008-10-06 05:28:25 +0000 | [diff] [blame] | 988 | } |
Chris Lattner | 4c4867e | 2008-07-12 00:38:25 +0000 | [diff] [blame] | 989 | } |
Anders Carlsson | 650c92f | 2008-07-08 15:34:11 +0000 | [diff] [blame] | 990 | |
Chris Lattner | b542afe | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 991 | bool IntExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) { |
Eli Friedman | a6afa76 | 2008-11-13 06:09:17 +0000 | [diff] [blame] | 992 | if (E->getOpcode() == BinaryOperator::Comma) { |
Anders Carlsson | 027f62e | 2008-12-01 02:07:06 +0000 | [diff] [blame] | 993 | if (!Visit(E->getRHS())) |
| 994 | return false; |
Anders Carlsson | 4fdfb09 | 2008-12-01 06:44:05 +0000 | [diff] [blame] | 995 | |
Eli Friedman | 33ef145 | 2009-02-26 10:19:36 +0000 | [diff] [blame] | 996 | // If we can't evaluate the LHS, it might have side effects; |
| 997 | // conservatively mark it. |
| 998 | if (!E->getLHS()->isEvaluatable(Info.Ctx)) |
| 999 | Info.EvalResult.HasSideEffects = true; |
Eli Friedman | a6afa76 | 2008-11-13 06:09:17 +0000 | [diff] [blame] | 1000 | |
Anders Carlsson | 027f62e | 2008-12-01 02:07:06 +0000 | [diff] [blame] | 1001 | return true; |
Eli Friedman | a6afa76 | 2008-11-13 06:09:17 +0000 | [diff] [blame] | 1002 | } |
| 1003 | |
| 1004 | if (E->isLogicalOp()) { |
| 1005 | // These need to be handled specially because the operands aren't |
| 1006 | // necessarily integral |
Anders Carlsson | fcb4d09 | 2008-11-30 16:51:17 +0000 | [diff] [blame] | 1007 | bool lhsResult, rhsResult; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1008 | |
Anders Carlsson | fcb4d09 | 2008-11-30 16:51:17 +0000 | [diff] [blame] | 1009 | if (HandleConversionToBool(E->getLHS(), lhsResult, Info)) { |
Anders Carlsson | 51fe996 | 2008-11-22 21:04:56 +0000 | [diff] [blame] | 1010 | // We were able to evaluate the LHS, see if we can get away with not |
| 1011 | // evaluating the RHS: 0 && X -> 0, 1 || X -> 1 |
Eli Friedman | 33ef145 | 2009-02-26 10:19:36 +0000 | [diff] [blame] | 1012 | if (lhsResult == (E->getOpcode() == BinaryOperator::LOr)) |
Daniel Dunbar | 3f7d995 | 2009-02-19 18:37:50 +0000 | [diff] [blame] | 1013 | return Success(lhsResult, E); |
Anders Carlsson | 4bbc0e0 | 2008-11-24 04:21:33 +0000 | [diff] [blame] | 1014 | |
Anders Carlsson | fcb4d09 | 2008-11-30 16:51:17 +0000 | [diff] [blame] | 1015 | if (HandleConversionToBool(E->getRHS(), rhsResult, Info)) { |
Anders Carlsson | 4bbc0e0 | 2008-11-24 04:21:33 +0000 | [diff] [blame] | 1016 | if (E->getOpcode() == BinaryOperator::LOr) |
Daniel Dunbar | 131eb43 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 1017 | return Success(lhsResult || rhsResult, E); |
Anders Carlsson | 4bbc0e0 | 2008-11-24 04:21:33 +0000 | [diff] [blame] | 1018 | else |
Daniel Dunbar | 131eb43 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 1019 | return Success(lhsResult && rhsResult, E); |
Anders Carlsson | 4bbc0e0 | 2008-11-24 04:21:33 +0000 | [diff] [blame] | 1020 | } |
| 1021 | } else { |
Anders Carlsson | fcb4d09 | 2008-11-30 16:51:17 +0000 | [diff] [blame] | 1022 | if (HandleConversionToBool(E->getRHS(), rhsResult, Info)) { |
Anders Carlsson | 4bbc0e0 | 2008-11-24 04:21:33 +0000 | [diff] [blame] | 1023 | // We can't evaluate the LHS; however, sometimes the result |
| 1024 | // is determined by the RHS: X && 0 -> 0, X || 1 -> 1. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1025 | if (rhsResult == (E->getOpcode() == BinaryOperator::LOr) || |
Anders Carlsson | fcb4d09 | 2008-11-30 16:51:17 +0000 | [diff] [blame] | 1026 | !rhsResult == (E->getOpcode() == BinaryOperator::LAnd)) { |
Daniel Dunbar | 131eb43 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 1027 | // Since we weren't able to evaluate the left hand side, it |
Anders Carlsson | fcb4d09 | 2008-11-30 16:51:17 +0000 | [diff] [blame] | 1028 | // must have had side effects. |
| 1029 | Info.EvalResult.HasSideEffects = true; |
Daniel Dunbar | 131eb43 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 1030 | |
| 1031 | return Success(rhsResult, E); |
Anders Carlsson | 4bbc0e0 | 2008-11-24 04:21:33 +0000 | [diff] [blame] | 1032 | } |
| 1033 | } |
Anders Carlsson | 51fe996 | 2008-11-22 21:04:56 +0000 | [diff] [blame] | 1034 | } |
Eli Friedman | a6afa76 | 2008-11-13 06:09:17 +0000 | [diff] [blame] | 1035 | |
Eli Friedman | a6afa76 | 2008-11-13 06:09:17 +0000 | [diff] [blame] | 1036 | return false; |
| 1037 | } |
| 1038 | |
Anders Carlsson | 286f85e | 2008-11-16 07:17:21 +0000 | [diff] [blame] | 1039 | QualType LHSTy = E->getLHS()->getType(); |
| 1040 | QualType RHSTy = E->getRHS()->getType(); |
Daniel Dunbar | 4087e24 | 2009-01-29 06:43:41 +0000 | [diff] [blame] | 1041 | |
| 1042 | if (LHSTy->isAnyComplexType()) { |
| 1043 | assert(RHSTy->isAnyComplexType() && "Invalid comparison"); |
| 1044 | APValue LHS, RHS; |
| 1045 | |
| 1046 | if (!EvaluateComplex(E->getLHS(), LHS, Info)) |
| 1047 | return false; |
| 1048 | |
| 1049 | if (!EvaluateComplex(E->getRHS(), RHS, Info)) |
| 1050 | return false; |
| 1051 | |
| 1052 | if (LHS.isComplexFloat()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1053 | APFloat::cmpResult CR_r = |
Daniel Dunbar | 4087e24 | 2009-01-29 06:43:41 +0000 | [diff] [blame] | 1054 | LHS.getComplexFloatReal().compare(RHS.getComplexFloatReal()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1055 | APFloat::cmpResult CR_i = |
Daniel Dunbar | 4087e24 | 2009-01-29 06:43:41 +0000 | [diff] [blame] | 1056 | LHS.getComplexFloatImag().compare(RHS.getComplexFloatImag()); |
| 1057 | |
Daniel Dunbar | 4087e24 | 2009-01-29 06:43:41 +0000 | [diff] [blame] | 1058 | if (E->getOpcode() == BinaryOperator::EQ) |
Daniel Dunbar | 131eb43 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 1059 | return Success((CR_r == APFloat::cmpEqual && |
| 1060 | CR_i == APFloat::cmpEqual), E); |
| 1061 | else { |
| 1062 | assert(E->getOpcode() == BinaryOperator::NE && |
| 1063 | "Invalid complex comparison."); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1064 | return Success(((CR_r == APFloat::cmpGreaterThan || |
Daniel Dunbar | 131eb43 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 1065 | CR_r == APFloat::cmpLessThan) && |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1066 | (CR_i == APFloat::cmpGreaterThan || |
Daniel Dunbar | 131eb43 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 1067 | CR_i == APFloat::cmpLessThan)), E); |
| 1068 | } |
Daniel Dunbar | 4087e24 | 2009-01-29 06:43:41 +0000 | [diff] [blame] | 1069 | } else { |
Daniel Dunbar | 4087e24 | 2009-01-29 06:43:41 +0000 | [diff] [blame] | 1070 | if (E->getOpcode() == BinaryOperator::EQ) |
Daniel Dunbar | 131eb43 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 1071 | return Success((LHS.getComplexIntReal() == RHS.getComplexIntReal() && |
| 1072 | LHS.getComplexIntImag() == RHS.getComplexIntImag()), E); |
| 1073 | else { |
| 1074 | assert(E->getOpcode() == BinaryOperator::NE && |
| 1075 | "Invalid compex comparison."); |
| 1076 | return Success((LHS.getComplexIntReal() != RHS.getComplexIntReal() || |
| 1077 | LHS.getComplexIntImag() != RHS.getComplexIntImag()), E); |
| 1078 | } |
Daniel Dunbar | 4087e24 | 2009-01-29 06:43:41 +0000 | [diff] [blame] | 1079 | } |
| 1080 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1081 | |
Anders Carlsson | 286f85e | 2008-11-16 07:17:21 +0000 | [diff] [blame] | 1082 | if (LHSTy->isRealFloatingType() && |
| 1083 | RHSTy->isRealFloatingType()) { |
| 1084 | APFloat RHS(0.0), LHS(0.0); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1085 | |
Anders Carlsson | 286f85e | 2008-11-16 07:17:21 +0000 | [diff] [blame] | 1086 | if (!EvaluateFloat(E->getRHS(), RHS, Info)) |
| 1087 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1088 | |
Anders Carlsson | 286f85e | 2008-11-16 07:17:21 +0000 | [diff] [blame] | 1089 | if (!EvaluateFloat(E->getLHS(), LHS, Info)) |
| 1090 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1091 | |
Anders Carlsson | 286f85e | 2008-11-16 07:17:21 +0000 | [diff] [blame] | 1092 | APFloat::cmpResult CR = LHS.compare(RHS); |
Anders Carlsson | 529569e | 2008-11-16 22:46:56 +0000 | [diff] [blame] | 1093 | |
Anders Carlsson | 286f85e | 2008-11-16 07:17:21 +0000 | [diff] [blame] | 1094 | switch (E->getOpcode()) { |
| 1095 | default: |
| 1096 | assert(0 && "Invalid binary operator!"); |
| 1097 | case BinaryOperator::LT: |
Daniel Dunbar | 131eb43 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 1098 | return Success(CR == APFloat::cmpLessThan, E); |
Anders Carlsson | 286f85e | 2008-11-16 07:17:21 +0000 | [diff] [blame] | 1099 | case BinaryOperator::GT: |
Daniel Dunbar | 131eb43 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 1100 | return Success(CR == APFloat::cmpGreaterThan, E); |
Anders Carlsson | 286f85e | 2008-11-16 07:17:21 +0000 | [diff] [blame] | 1101 | case BinaryOperator::LE: |
Daniel Dunbar | 131eb43 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 1102 | return Success(CR == APFloat::cmpLessThan || CR == APFloat::cmpEqual, E); |
Anders Carlsson | 286f85e | 2008-11-16 07:17:21 +0000 | [diff] [blame] | 1103 | case BinaryOperator::GE: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1104 | return Success(CR == APFloat::cmpGreaterThan || CR == APFloat::cmpEqual, |
Daniel Dunbar | 131eb43 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 1105 | E); |
Anders Carlsson | 286f85e | 2008-11-16 07:17:21 +0000 | [diff] [blame] | 1106 | case BinaryOperator::EQ: |
Daniel Dunbar | 131eb43 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 1107 | return Success(CR == APFloat::cmpEqual, E); |
Anders Carlsson | 286f85e | 2008-11-16 07:17:21 +0000 | [diff] [blame] | 1108 | case BinaryOperator::NE: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1109 | return Success(CR == APFloat::cmpGreaterThan |
Daniel Dunbar | 131eb43 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 1110 | || CR == APFloat::cmpLessThan, E); |
Anders Carlsson | 286f85e | 2008-11-16 07:17:21 +0000 | [diff] [blame] | 1111 | } |
Anders Carlsson | 286f85e | 2008-11-16 07:17:21 +0000 | [diff] [blame] | 1112 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1113 | |
Eli Friedman | ad02d7d | 2009-04-28 19:17:36 +0000 | [diff] [blame] | 1114 | if (LHSTy->isPointerType() && RHSTy->isPointerType()) { |
| 1115 | if (E->getOpcode() == BinaryOperator::Sub || E->isEqualityOp()) { |
Anders Carlsson | 3068d11 | 2008-11-16 19:01:22 +0000 | [diff] [blame] | 1116 | APValue LHSValue; |
| 1117 | if (!EvaluatePointer(E->getLHS(), LHSValue, Info)) |
| 1118 | return false; |
Eli Friedman | a1f47c4 | 2009-03-23 04:38:34 +0000 | [diff] [blame] | 1119 | |
Anders Carlsson | 3068d11 | 2008-11-16 19:01:22 +0000 | [diff] [blame] | 1120 | APValue RHSValue; |
| 1121 | if (!EvaluatePointer(E->getRHS(), RHSValue, Info)) |
| 1122 | return false; |
Eli Friedman | a1f47c4 | 2009-03-23 04:38:34 +0000 | [diff] [blame] | 1123 | |
Eli Friedman | 5bc8610 | 2009-06-14 02:17:33 +0000 | [diff] [blame] | 1124 | // Reject any bases from the normal codepath; we special-case comparisons |
| 1125 | // to null. |
| 1126 | if (LHSValue.getLValueBase()) { |
| 1127 | if (!E->isEqualityOp()) |
| 1128 | return false; |
| 1129 | if (RHSValue.getLValueBase() || RHSValue.getLValueOffset()) |
| 1130 | return false; |
| 1131 | bool bres; |
| 1132 | if (!EvalPointerValueAsBool(LHSValue, bres)) |
| 1133 | return false; |
| 1134 | return Success(bres ^ (E->getOpcode() == BinaryOperator::EQ), E); |
| 1135 | } else if (RHSValue.getLValueBase()) { |
| 1136 | if (!E->isEqualityOp()) |
| 1137 | return false; |
| 1138 | if (LHSValue.getLValueBase() || LHSValue.getLValueOffset()) |
| 1139 | return false; |
| 1140 | bool bres; |
| 1141 | if (!EvalPointerValueAsBool(RHSValue, bres)) |
| 1142 | return false; |
| 1143 | return Success(bres ^ (E->getOpcode() == BinaryOperator::EQ), E); |
| 1144 | } |
Eli Friedman | a1f47c4 | 2009-03-23 04:38:34 +0000 | [diff] [blame] | 1145 | |
Eli Friedman | ad02d7d | 2009-04-28 19:17:36 +0000 | [diff] [blame] | 1146 | if (E->getOpcode() == BinaryOperator::Sub) { |
| 1147 | const QualType Type = E->getLHS()->getType(); |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1148 | const QualType ElementType = Type->getAs<PointerType>()->getPointeeType(); |
Anders Carlsson | 3068d11 | 2008-11-16 19:01:22 +0000 | [diff] [blame] | 1149 | |
Eli Friedman | ad02d7d | 2009-04-28 19:17:36 +0000 | [diff] [blame] | 1150 | uint64_t D = LHSValue.getLValueOffset() - RHSValue.getLValueOffset(); |
Eli Friedman | ce1bca7 | 2009-06-04 20:23:20 +0000 | [diff] [blame] | 1151 | if (!ElementType->isVoidType() && !ElementType->isFunctionType()) |
| 1152 | D /= Info.Ctx.getTypeSize(ElementType) / 8; |
Eli Friedman | a1f47c4 | 2009-03-23 04:38:34 +0000 | [diff] [blame] | 1153 | |
Eli Friedman | ad02d7d | 2009-04-28 19:17:36 +0000 | [diff] [blame] | 1154 | return Success(D, E); |
| 1155 | } |
| 1156 | bool Result; |
| 1157 | if (E->getOpcode() == BinaryOperator::EQ) { |
| 1158 | Result = LHSValue.getLValueOffset() == RHSValue.getLValueOffset(); |
Eli Friedman | 267c0ab | 2009-04-29 20:29:43 +0000 | [diff] [blame] | 1159 | } else { |
Eli Friedman | ad02d7d | 2009-04-28 19:17:36 +0000 | [diff] [blame] | 1160 | Result = LHSValue.getLValueOffset() != RHSValue.getLValueOffset(); |
| 1161 | } |
| 1162 | return Success(Result, E); |
Anders Carlsson | 3068d11 | 2008-11-16 19:01:22 +0000 | [diff] [blame] | 1163 | } |
| 1164 | } |
Anders Carlsson | 286f85e | 2008-11-16 07:17:21 +0000 | [diff] [blame] | 1165 | if (!LHSTy->isIntegralType() || |
| 1166 | !RHSTy->isIntegralType()) { |
Eli Friedman | a6afa76 | 2008-11-13 06:09:17 +0000 | [diff] [blame] | 1167 | // We can't continue from here for non-integral types, and they |
| 1168 | // could potentially confuse the following operations. |
Eli Friedman | a6afa76 | 2008-11-13 06:09:17 +0000 | [diff] [blame] | 1169 | return false; |
| 1170 | } |
| 1171 | |
Anders Carlsson | a25ae3d | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 1172 | // The LHS of a constant expr is always evaluated and needed. |
Daniel Dunbar | 30c37f4 | 2009-02-19 20:17:33 +0000 | [diff] [blame] | 1173 | if (!Visit(E->getLHS())) |
Chris Lattner | 54176fd | 2008-07-12 00:14:42 +0000 | [diff] [blame] | 1174 | return false; // error in subexpression. |
Eli Friedman | d9f4bcd | 2008-07-27 05:46:18 +0000 | [diff] [blame] | 1175 | |
Eli Friedman | 42edd0d | 2009-03-24 01:14:50 +0000 | [diff] [blame] | 1176 | APValue RHSVal; |
| 1177 | if (!EvaluateIntegerOrLValue(E->getRHS(), RHSVal, Info)) |
Daniel Dunbar | 30c37f4 | 2009-02-19 20:17:33 +0000 | [diff] [blame] | 1178 | return false; |
Eli Friedman | 42edd0d | 2009-03-24 01:14:50 +0000 | [diff] [blame] | 1179 | |
| 1180 | // Handle cases like (unsigned long)&a + 4. |
| 1181 | if (E->isAdditiveOp() && Result.isLValue() && RHSVal.isInt()) { |
| 1182 | uint64_t offset = Result.getLValueOffset(); |
| 1183 | if (E->getOpcode() == BinaryOperator::Add) |
| 1184 | offset += RHSVal.getInt().getZExtValue(); |
| 1185 | else |
| 1186 | offset -= RHSVal.getInt().getZExtValue(); |
| 1187 | Result = APValue(Result.getLValueBase(), offset); |
| 1188 | return true; |
| 1189 | } |
| 1190 | |
| 1191 | // Handle cases like 4 + (unsigned long)&a |
| 1192 | if (E->getOpcode() == BinaryOperator::Add && |
| 1193 | RHSVal.isLValue() && Result.isInt()) { |
| 1194 | uint64_t offset = RHSVal.getLValueOffset(); |
| 1195 | offset += Result.getInt().getZExtValue(); |
| 1196 | Result = APValue(RHSVal.getLValueBase(), offset); |
| 1197 | return true; |
| 1198 | } |
| 1199 | |
| 1200 | // All the following cases expect both operands to be an integer |
| 1201 | if (!Result.isInt() || !RHSVal.isInt()) |
Chris Lattner | b542afe | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 1202 | return false; |
Eli Friedman | a6afa76 | 2008-11-13 06:09:17 +0000 | [diff] [blame] | 1203 | |
Eli Friedman | 42edd0d | 2009-03-24 01:14:50 +0000 | [diff] [blame] | 1204 | APSInt& RHS = RHSVal.getInt(); |
| 1205 | |
Anders Carlsson | a25ae3d | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 1206 | switch (E->getOpcode()) { |
Chris Lattner | 32fea9d | 2008-11-12 07:43:42 +0000 | [diff] [blame] | 1207 | default: |
Anders Carlsson | 0e8acbb | 2008-11-30 18:37:00 +0000 | [diff] [blame] | 1208 | return Error(E->getOperatorLoc(), diag::note_invalid_subexpr_in_ice, E); |
Daniel Dunbar | 30c37f4 | 2009-02-19 20:17:33 +0000 | [diff] [blame] | 1209 | case BinaryOperator::Mul: return Success(Result.getInt() * RHS, E); |
| 1210 | case BinaryOperator::Add: return Success(Result.getInt() + RHS, E); |
| 1211 | case BinaryOperator::Sub: return Success(Result.getInt() - RHS, E); |
| 1212 | case BinaryOperator::And: return Success(Result.getInt() & RHS, E); |
| 1213 | case BinaryOperator::Xor: return Success(Result.getInt() ^ RHS, E); |
| 1214 | case BinaryOperator::Or: return Success(Result.getInt() | RHS, E); |
Chris Lattner | 75a4881 | 2008-07-11 22:15:16 +0000 | [diff] [blame] | 1215 | case BinaryOperator::Div: |
Chris Lattner | 54176fd | 2008-07-12 00:14:42 +0000 | [diff] [blame] | 1216 | if (RHS == 0) |
Anders Carlsson | 0e8acbb | 2008-11-30 18:37:00 +0000 | [diff] [blame] | 1217 | return Error(E->getOperatorLoc(), diag::note_expr_divide_by_zero, E); |
Daniel Dunbar | 30c37f4 | 2009-02-19 20:17:33 +0000 | [diff] [blame] | 1218 | return Success(Result.getInt() / RHS, E); |
Chris Lattner | 75a4881 | 2008-07-11 22:15:16 +0000 | [diff] [blame] | 1219 | case BinaryOperator::Rem: |
Chris Lattner | 54176fd | 2008-07-12 00:14:42 +0000 | [diff] [blame] | 1220 | if (RHS == 0) |
Anders Carlsson | 0e8acbb | 2008-11-30 18:37:00 +0000 | [diff] [blame] | 1221 | return Error(E->getOperatorLoc(), diag::note_expr_divide_by_zero, E); |
Daniel Dunbar | 30c37f4 | 2009-02-19 20:17:33 +0000 | [diff] [blame] | 1222 | return Success(Result.getInt() % RHS, E); |
Daniel Dunbar | 3f7d995 | 2009-02-19 18:37:50 +0000 | [diff] [blame] | 1223 | case BinaryOperator::Shl: { |
Chris Lattner | 54176fd | 2008-07-12 00:14:42 +0000 | [diff] [blame] | 1224 | // FIXME: Warn about out of range shift amounts! |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1225 | unsigned SA = |
Daniel Dunbar | 30c37f4 | 2009-02-19 20:17:33 +0000 | [diff] [blame] | 1226 | (unsigned) RHS.getLimitedValue(Result.getInt().getBitWidth()-1); |
| 1227 | return Success(Result.getInt() << SA, E); |
Daniel Dunbar | 3f7d995 | 2009-02-19 18:37:50 +0000 | [diff] [blame] | 1228 | } |
| 1229 | case BinaryOperator::Shr: { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1230 | unsigned SA = |
Daniel Dunbar | 30c37f4 | 2009-02-19 20:17:33 +0000 | [diff] [blame] | 1231 | (unsigned) RHS.getLimitedValue(Result.getInt().getBitWidth()-1); |
| 1232 | return Success(Result.getInt() >> SA, E); |
Daniel Dunbar | 3f7d995 | 2009-02-19 18:37:50 +0000 | [diff] [blame] | 1233 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1234 | |
Daniel Dunbar | 30c37f4 | 2009-02-19 20:17:33 +0000 | [diff] [blame] | 1235 | case BinaryOperator::LT: return Success(Result.getInt() < RHS, E); |
| 1236 | case BinaryOperator::GT: return Success(Result.getInt() > RHS, E); |
| 1237 | case BinaryOperator::LE: return Success(Result.getInt() <= RHS, E); |
| 1238 | case BinaryOperator::GE: return Success(Result.getInt() >= RHS, E); |
| 1239 | case BinaryOperator::EQ: return Success(Result.getInt() == RHS, E); |
| 1240 | case BinaryOperator::NE: return Success(Result.getInt() != RHS, E); |
Eli Friedman | b11e778 | 2008-11-13 02:13:11 +0000 | [diff] [blame] | 1241 | } |
Anders Carlsson | a25ae3d | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 1242 | } |
| 1243 | |
Nuno Lopes | ca7c2ea | 2008-11-16 19:28:31 +0000 | [diff] [blame] | 1244 | bool IntExprEvaluator::VisitConditionalOperator(const ConditionalOperator *E) { |
Nuno Lopes | a25bd55 | 2008-11-16 22:06:39 +0000 | [diff] [blame] | 1245 | bool Cond; |
| 1246 | if (!HandleConversionToBool(E->getCond(), Cond, Info)) |
Nuno Lopes | ca7c2ea | 2008-11-16 19:28:31 +0000 | [diff] [blame] | 1247 | return false; |
| 1248 | |
Nuno Lopes | a25bd55 | 2008-11-16 22:06:39 +0000 | [diff] [blame] | 1249 | return Visit(Cond ? E->getTrueExpr() : E->getFalseExpr()); |
Nuno Lopes | ca7c2ea | 2008-11-16 19:28:31 +0000 | [diff] [blame] | 1250 | } |
| 1251 | |
Chris Lattner | af707ab | 2009-01-24 21:53:27 +0000 | [diff] [blame] | 1252 | unsigned IntExprEvaluator::GetAlignOfType(QualType T) { |
Chris Lattner | e9feb47 | 2009-01-24 21:09:06 +0000 | [diff] [blame] | 1253 | // Get information about the alignment. |
| 1254 | unsigned CharSize = Info.Ctx.Target.getCharWidth(); |
Douglas Gregor | 1885764 | 2009-04-30 17:32:17 +0000 | [diff] [blame] | 1255 | |
Eli Friedman | 2be5861 | 2009-05-30 21:09:44 +0000 | [diff] [blame] | 1256 | // __alignof is defined to return the preferred alignment. |
Douglas Gregor | 1885764 | 2009-04-30 17:32:17 +0000 | [diff] [blame] | 1257 | return Info.Ctx.getPreferredTypeAlign(T.getTypePtr()) / CharSize; |
Chris Lattner | e9feb47 | 2009-01-24 21:09:06 +0000 | [diff] [blame] | 1258 | } |
| 1259 | |
Chris Lattner | af707ab | 2009-01-24 21:53:27 +0000 | [diff] [blame] | 1260 | unsigned IntExprEvaluator::GetAlignOfExpr(const Expr *E) { |
| 1261 | E = E->IgnoreParens(); |
| 1262 | |
| 1263 | // alignof decl is always accepted, even if it doesn't make sense: we default |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1264 | // to 1 in those cases. |
Chris Lattner | af707ab | 2009-01-24 21:53:27 +0000 | [diff] [blame] | 1265 | if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E)) |
Daniel Dunbar | b7d0844 | 2009-02-17 22:16:19 +0000 | [diff] [blame] | 1266 | return Info.Ctx.getDeclAlignInBytes(DRE->getDecl()); |
Eli Friedman | a1f47c4 | 2009-03-23 04:38:34 +0000 | [diff] [blame] | 1267 | |
Chris Lattner | af707ab | 2009-01-24 21:53:27 +0000 | [diff] [blame] | 1268 | if (const MemberExpr *ME = dyn_cast<MemberExpr>(E)) |
Daniel Dunbar | b7d0844 | 2009-02-17 22:16:19 +0000 | [diff] [blame] | 1269 | return Info.Ctx.getDeclAlignInBytes(ME->getMemberDecl()); |
Chris Lattner | af707ab | 2009-01-24 21:53:27 +0000 | [diff] [blame] | 1270 | |
Chris Lattner | e9feb47 | 2009-01-24 21:09:06 +0000 | [diff] [blame] | 1271 | return GetAlignOfType(E->getType()); |
| 1272 | } |
| 1273 | |
| 1274 | |
Sebastian Redl | 0518999 | 2008-11-11 17:56:53 +0000 | [diff] [blame] | 1275 | /// VisitSizeAlignOfExpr - Evaluate a sizeof or alignof with a result as the |
| 1276 | /// expression's type. |
| 1277 | bool IntExprEvaluator::VisitSizeOfAlignOfExpr(const SizeOfAlignOfExpr *E) { |
| 1278 | QualType DstTy = E->getType(); |
Chris Lattner | fcee001 | 2008-07-11 21:24:13 +0000 | [diff] [blame] | 1279 | |
Chris Lattner | e9feb47 | 2009-01-24 21:09:06 +0000 | [diff] [blame] | 1280 | // Handle alignof separately. |
| 1281 | if (!E->isSizeOf()) { |
| 1282 | if (E->isArgumentType()) |
Daniel Dunbar | 131eb43 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 1283 | return Success(GetAlignOfType(E->getArgumentType()), E); |
Chris Lattner | e9feb47 | 2009-01-24 21:09:06 +0000 | [diff] [blame] | 1284 | else |
Daniel Dunbar | 131eb43 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 1285 | return Success(GetAlignOfExpr(E->getArgumentExpr()), E); |
Chris Lattner | e9feb47 | 2009-01-24 21:09:06 +0000 | [diff] [blame] | 1286 | } |
Eli Friedman | a1f47c4 | 2009-03-23 04:38:34 +0000 | [diff] [blame] | 1287 | |
Sebastian Redl | 0518999 | 2008-11-11 17:56:53 +0000 | [diff] [blame] | 1288 | QualType SrcTy = E->getTypeOfArgument(); |
| 1289 | |
Daniel Dunbar | 131eb43 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 1290 | // sizeof(void), __alignof__(void), sizeof(function) = 1 as a gcc |
| 1291 | // extension. |
| 1292 | if (SrcTy->isVoidType() || SrcTy->isFunctionType()) |
| 1293 | return Success(1, E); |
Eli Friedman | a1f47c4 | 2009-03-23 04:38:34 +0000 | [diff] [blame] | 1294 | |
Chris Lattner | fcee001 | 2008-07-11 21:24:13 +0000 | [diff] [blame] | 1295 | // sizeof(vla) is not a constantexpr: C99 6.5.3.4p2. |
Chris Lattner | e9feb47 | 2009-01-24 21:09:06 +0000 | [diff] [blame] | 1296 | if (!SrcTy->isConstantSizeType()) |
Chris Lattner | fcee001 | 2008-07-11 21:24:13 +0000 | [diff] [blame] | 1297 | return false; |
Eli Friedman | f2da9df | 2009-01-24 22:19:05 +0000 | [diff] [blame] | 1298 | |
Chris Lattner | e9feb47 | 2009-01-24 21:09:06 +0000 | [diff] [blame] | 1299 | // Get information about the size. |
Daniel Dunbar | 24cbfb9 | 2009-05-03 10:35:52 +0000 | [diff] [blame] | 1300 | unsigned BitWidth = Info.Ctx.getTypeSize(SrcTy); |
Daniel Dunbar | ff89666 | 2009-04-21 15:48:54 +0000 | [diff] [blame] | 1301 | return Success(BitWidth / Info.Ctx.Target.getCharWidth(), E); |
Chris Lattner | fcee001 | 2008-07-11 21:24:13 +0000 | [diff] [blame] | 1302 | } |
| 1303 | |
Chris Lattner | b542afe | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 1304 | bool IntExprEvaluator::VisitUnaryOperator(const UnaryOperator *E) { |
Chris Lattner | 4c4867e | 2008-07-12 00:38:25 +0000 | [diff] [blame] | 1305 | // Special case unary operators that do not need their subexpression |
| 1306 | // evaluated. offsetof/sizeof/alignof are all special. |
Eli Friedman | 35183ac | 2009-02-27 06:44:11 +0000 | [diff] [blame] | 1307 | if (E->isOffsetOfOp()) { |
| 1308 | // The AST for offsetof is defined in such a way that we can just |
| 1309 | // directly Evaluate it as an l-value. |
| 1310 | APValue LV; |
| 1311 | if (!EvaluateLValue(E->getSubExpr(), LV, Info)) |
| 1312 | return false; |
| 1313 | if (LV.getLValueBase()) |
| 1314 | return false; |
| 1315 | return Success(LV.getLValueOffset(), E); |
| 1316 | } |
Eli Friedman | a6afa76 | 2008-11-13 06:09:17 +0000 | [diff] [blame] | 1317 | |
| 1318 | if (E->getOpcode() == UnaryOperator::LNot) { |
| 1319 | // LNot's operand isn't necessarily an integer, so we handle it specially. |
| 1320 | bool bres; |
| 1321 | if (!HandleConversionToBool(E->getSubExpr(), bres, Info)) |
| 1322 | return false; |
Daniel Dunbar | 131eb43 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 1323 | return Success(!bres, E); |
Eli Friedman | a6afa76 | 2008-11-13 06:09:17 +0000 | [diff] [blame] | 1324 | } |
| 1325 | |
Daniel Dunbar | 4fff481 | 2009-02-21 18:14:20 +0000 | [diff] [blame] | 1326 | // Only handle integral operations... |
| 1327 | if (!E->getSubExpr()->getType()->isIntegralType()) |
| 1328 | return false; |
| 1329 | |
Chris Lattner | 87eae5e | 2008-07-11 22:52:41 +0000 | [diff] [blame] | 1330 | // Get the operand value into 'Result'. |
| 1331 | if (!Visit(E->getSubExpr())) |
Chris Lattner | 75a4881 | 2008-07-11 22:15:16 +0000 | [diff] [blame] | 1332 | return false; |
Anders Carlsson | a25ae3d | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 1333 | |
Chris Lattner | 75a4881 | 2008-07-11 22:15:16 +0000 | [diff] [blame] | 1334 | switch (E->getOpcode()) { |
Chris Lattner | 4c4867e | 2008-07-12 00:38:25 +0000 | [diff] [blame] | 1335 | default: |
Chris Lattner | 75a4881 | 2008-07-11 22:15:16 +0000 | [diff] [blame] | 1336 | // Address, indirect, pre/post inc/dec, etc are not valid constant exprs. |
| 1337 | // See C99 6.6p3. |
Anders Carlsson | 0e8acbb | 2008-11-30 18:37:00 +0000 | [diff] [blame] | 1338 | return Error(E->getOperatorLoc(), diag::note_invalid_subexpr_in_ice, E); |
Chris Lattner | 75a4881 | 2008-07-11 22:15:16 +0000 | [diff] [blame] | 1339 | case UnaryOperator::Extension: |
Chris Lattner | 4c4867e | 2008-07-12 00:38:25 +0000 | [diff] [blame] | 1340 | // FIXME: Should extension allow i-c-e extension expressions in its scope? |
| 1341 | // If so, we could clear the diagnostic ID. |
Daniel Dunbar | 3f7d995 | 2009-02-19 18:37:50 +0000 | [diff] [blame] | 1342 | return true; |
Chris Lattner | 75a4881 | 2008-07-11 22:15:16 +0000 | [diff] [blame] | 1343 | case UnaryOperator::Plus: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1344 | // The result is always just the subexpr. |
Daniel Dunbar | 3f7d995 | 2009-02-19 18:37:50 +0000 | [diff] [blame] | 1345 | return true; |
Chris Lattner | 75a4881 | 2008-07-11 22:15:16 +0000 | [diff] [blame] | 1346 | case UnaryOperator::Minus: |
Daniel Dunbar | 30c37f4 | 2009-02-19 20:17:33 +0000 | [diff] [blame] | 1347 | if (!Result.isInt()) return false; |
| 1348 | return Success(-Result.getInt(), E); |
Chris Lattner | 75a4881 | 2008-07-11 22:15:16 +0000 | [diff] [blame] | 1349 | case UnaryOperator::Not: |
Daniel Dunbar | 30c37f4 | 2009-02-19 20:17:33 +0000 | [diff] [blame] | 1350 | if (!Result.isInt()) return false; |
| 1351 | return Success(~Result.getInt(), E); |
Anders Carlsson | a25ae3d | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 1352 | } |
Anders Carlsson | a25ae3d | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 1353 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1354 | |
Chris Lattner | 732b223 | 2008-07-12 01:15:53 +0000 | [diff] [blame] | 1355 | /// HandleCast - This is used to evaluate implicit or explicit casts where the |
| 1356 | /// result type is integer. |
Daniel Dunbar | a2cfd34 | 2009-01-29 06:16:07 +0000 | [diff] [blame] | 1357 | bool IntExprEvaluator::VisitCastExpr(CastExpr *E) { |
Anders Carlsson | 82206e2 | 2008-11-30 18:14:57 +0000 | [diff] [blame] | 1358 | Expr *SubExpr = E->getSubExpr(); |
| 1359 | QualType DestType = E->getType(); |
Daniel Dunbar | b92dac8 | 2009-02-19 22:16:29 +0000 | [diff] [blame] | 1360 | QualType SrcType = SubExpr->getType(); |
Anders Carlsson | 82206e2 | 2008-11-30 18:14:57 +0000 | [diff] [blame] | 1361 | |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 1362 | if (DestType->isBooleanType()) { |
| 1363 | bool BoolResult; |
| 1364 | if (!HandleConversionToBool(SubExpr, BoolResult, Info)) |
| 1365 | return false; |
Daniel Dunbar | 131eb43 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 1366 | return Success(BoolResult, E); |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 1367 | } |
| 1368 | |
Anders Carlsson | a25ae3d | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 1369 | // Handle simple integer->integer casts. |
Daniel Dunbar | b92dac8 | 2009-02-19 22:16:29 +0000 | [diff] [blame] | 1370 | if (SrcType->isIntegralType()) { |
Chris Lattner | 732b223 | 2008-07-12 01:15:53 +0000 | [diff] [blame] | 1371 | if (!Visit(SubExpr)) |
Chris Lattner | b542afe | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 1372 | return false; |
Daniel Dunbar | a2cfd34 | 2009-01-29 06:16:07 +0000 | [diff] [blame] | 1373 | |
Eli Friedman | be26570 | 2009-02-20 01:15:07 +0000 | [diff] [blame] | 1374 | if (!Result.isInt()) { |
| 1375 | // Only allow casts of lvalues if they are lossless. |
| 1376 | return Info.Ctx.getTypeSize(DestType) == Info.Ctx.getTypeSize(SrcType); |
| 1377 | } |
Daniel Dunbar | 30c37f4 | 2009-02-19 20:17:33 +0000 | [diff] [blame] | 1378 | |
Daniel Dunbar | dd21164 | 2009-02-19 22:24:01 +0000 | [diff] [blame] | 1379 | return Success(HandleIntToIntCast(DestType, SrcType, |
Daniel Dunbar | 30c37f4 | 2009-02-19 20:17:33 +0000 | [diff] [blame] | 1380 | Result.getInt(), Info.Ctx), E); |
Chris Lattner | 732b223 | 2008-07-12 01:15:53 +0000 | [diff] [blame] | 1381 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1382 | |
Chris Lattner | 732b223 | 2008-07-12 01:15:53 +0000 | [diff] [blame] | 1383 | // FIXME: Clean this up! |
Daniel Dunbar | b92dac8 | 2009-02-19 22:16:29 +0000 | [diff] [blame] | 1384 | if (SrcType->isPointerType()) { |
Anders Carlsson | a25ae3d | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 1385 | APValue LV; |
Chris Lattner | 87eae5e | 2008-07-11 22:52:41 +0000 | [diff] [blame] | 1386 | if (!EvaluatePointer(SubExpr, LV, Info)) |
Chris Lattner | b542afe | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 1387 | return false; |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 1388 | |
Daniel Dunbar | dd21164 | 2009-02-19 22:24:01 +0000 | [diff] [blame] | 1389 | if (LV.getLValueBase()) { |
| 1390 | // Only allow based lvalue casts if they are lossless. |
| 1391 | if (Info.Ctx.getTypeSize(DestType) != Info.Ctx.getTypeSize(SrcType)) |
| 1392 | return false; |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 1393 | |
Daniel Dunbar | dd21164 | 2009-02-19 22:24:01 +0000 | [diff] [blame] | 1394 | Result = LV; |
| 1395 | return true; |
| 1396 | } |
| 1397 | |
| 1398 | APSInt AsInt = Info.Ctx.MakeIntValue(LV.getLValueOffset(), SrcType); |
| 1399 | return Success(HandleIntToIntCast(DestType, SrcType, AsInt, Info.Ctx), E); |
Anders Carlsson | 2bad168 | 2008-07-08 14:30:00 +0000 | [diff] [blame] | 1400 | } |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 1401 | |
Eli Friedman | be26570 | 2009-02-20 01:15:07 +0000 | [diff] [blame] | 1402 | if (SrcType->isArrayType() || SrcType->isFunctionType()) { |
| 1403 | // This handles double-conversion cases, where there's both |
| 1404 | // an l-value promotion and an implicit conversion to int. |
| 1405 | APValue LV; |
| 1406 | if (!EvaluateLValue(SubExpr, LV, Info)) |
| 1407 | return false; |
| 1408 | |
| 1409 | if (Info.Ctx.getTypeSize(DestType) != Info.Ctx.getTypeSize(Info.Ctx.VoidPtrTy)) |
| 1410 | return false; |
| 1411 | |
| 1412 | Result = LV; |
| 1413 | return true; |
| 1414 | } |
| 1415 | |
Eli Friedman | 1725f68 | 2009-04-22 19:23:09 +0000 | [diff] [blame] | 1416 | if (SrcType->isAnyComplexType()) { |
| 1417 | APValue C; |
| 1418 | if (!EvaluateComplex(SubExpr, C, Info)) |
| 1419 | return false; |
| 1420 | if (C.isComplexFloat()) |
| 1421 | return Success(HandleFloatToIntCast(DestType, SrcType, |
| 1422 | C.getComplexFloatReal(), Info.Ctx), |
| 1423 | E); |
| 1424 | else |
| 1425 | return Success(HandleIntToIntCast(DestType, SrcType, |
| 1426 | C.getComplexIntReal(), Info.Ctx), E); |
| 1427 | } |
Eli Friedman | 2217c87 | 2009-02-22 11:46:18 +0000 | [diff] [blame] | 1428 | // FIXME: Handle vectors |
| 1429 | |
Daniel Dunbar | b92dac8 | 2009-02-19 22:16:29 +0000 | [diff] [blame] | 1430 | if (!SrcType->isRealFloatingType()) |
Anders Carlsson | 0e8acbb | 2008-11-30 18:37:00 +0000 | [diff] [blame] | 1431 | return Error(E->getExprLoc(), diag::note_invalid_subexpr_in_ice, E); |
Chris Lattner | 732b223 | 2008-07-12 01:15:53 +0000 | [diff] [blame] | 1432 | |
Eli Friedman | d8bfe7f | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 1433 | APFloat F(0.0); |
| 1434 | if (!EvaluateFloat(SubExpr, F, Info)) |
Anders Carlsson | 0e8acbb | 2008-11-30 18:37:00 +0000 | [diff] [blame] | 1435 | return Error(E->getExprLoc(), diag::note_invalid_subexpr_in_ice, E); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1436 | |
Daniel Dunbar | b92dac8 | 2009-02-19 22:16:29 +0000 | [diff] [blame] | 1437 | return Success(HandleFloatToIntCast(DestType, SrcType, F, Info.Ctx), E); |
Anders Carlsson | a25ae3d | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 1438 | } |
Anders Carlsson | 2bad168 | 2008-07-08 14:30:00 +0000 | [diff] [blame] | 1439 | |
Eli Friedman | 722c717 | 2009-02-28 03:59:05 +0000 | [diff] [blame] | 1440 | bool IntExprEvaluator::VisitUnaryReal(const UnaryOperator *E) { |
| 1441 | if (E->getSubExpr()->getType()->isAnyComplexType()) { |
| 1442 | APValue LV; |
| 1443 | if (!EvaluateComplex(E->getSubExpr(), LV, Info) || !LV.isComplexInt()) |
| 1444 | return Error(E->getExprLoc(), diag::note_invalid_subexpr_in_ice, E); |
| 1445 | return Success(LV.getComplexIntReal(), E); |
| 1446 | } |
| 1447 | |
| 1448 | return Visit(E->getSubExpr()); |
| 1449 | } |
| 1450 | |
Eli Friedman | 664a104 | 2009-02-27 04:45:43 +0000 | [diff] [blame] | 1451 | bool IntExprEvaluator::VisitUnaryImag(const UnaryOperator *E) { |
Eli Friedman | 722c717 | 2009-02-28 03:59:05 +0000 | [diff] [blame] | 1452 | if (E->getSubExpr()->getType()->isComplexIntegerType()) { |
| 1453 | APValue LV; |
| 1454 | if (!EvaluateComplex(E->getSubExpr(), LV, Info) || !LV.isComplexInt()) |
| 1455 | return Error(E->getExprLoc(), diag::note_invalid_subexpr_in_ice, E); |
| 1456 | return Success(LV.getComplexIntImag(), E); |
| 1457 | } |
| 1458 | |
Eli Friedman | 664a104 | 2009-02-27 04:45:43 +0000 | [diff] [blame] | 1459 | if (!E->getSubExpr()->isEvaluatable(Info.Ctx)) |
| 1460 | Info.EvalResult.HasSideEffects = true; |
| 1461 | return Success(0, E); |
| 1462 | } |
| 1463 | |
Chris Lattner | f5eeb05 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 1464 | //===----------------------------------------------------------------------===// |
Eli Friedman | d8bfe7f | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 1465 | // Float Evaluation |
| 1466 | //===----------------------------------------------------------------------===// |
| 1467 | |
| 1468 | namespace { |
| 1469 | class VISIBILITY_HIDDEN FloatExprEvaluator |
| 1470 | : public StmtVisitor<FloatExprEvaluator, bool> { |
| 1471 | EvalInfo &Info; |
| 1472 | APFloat &Result; |
| 1473 | public: |
| 1474 | FloatExprEvaluator(EvalInfo &info, APFloat &result) |
| 1475 | : Info(info), Result(result) {} |
| 1476 | |
| 1477 | bool VisitStmt(Stmt *S) { |
| 1478 | return false; |
| 1479 | } |
| 1480 | |
| 1481 | bool VisitParenExpr(ParenExpr *E) { return Visit(E->getSubExpr()); } |
Chris Lattner | 019f4e8 | 2008-10-06 05:28:25 +0000 | [diff] [blame] | 1482 | bool VisitCallExpr(const CallExpr *E); |
Eli Friedman | d8bfe7f | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 1483 | |
Daniel Dunbar | 5db4b3f | 2008-10-16 03:51:50 +0000 | [diff] [blame] | 1484 | bool VisitUnaryOperator(const UnaryOperator *E); |
Eli Friedman | d8bfe7f | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 1485 | bool VisitBinaryOperator(const BinaryOperator *E); |
| 1486 | bool VisitFloatingLiteral(const FloatingLiteral *E); |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 1487 | bool VisitCastExpr(CastExpr *E); |
| 1488 | bool VisitCXXZeroInitValueExpr(CXXZeroInitValueExpr *E); |
Eli Friedman | 2217c87 | 2009-02-22 11:46:18 +0000 | [diff] [blame] | 1489 | |
Eli Friedman | ba98d6b | 2009-03-23 04:56:01 +0000 | [diff] [blame] | 1490 | bool VisitChooseExpr(const ChooseExpr *E) |
| 1491 | { return Visit(E->getChosenSubExpr(Info.Ctx)); } |
| 1492 | bool VisitUnaryExtension(const UnaryOperator *E) |
| 1493 | { return Visit(E->getSubExpr()); } |
| 1494 | |
| 1495 | // FIXME: Missing: __real__/__imag__, array subscript of vector, |
| 1496 | // member of vector, ImplicitValueInitExpr, |
Eli Friedman | 2217c87 | 2009-02-22 11:46:18 +0000 | [diff] [blame] | 1497 | // conditional ?:, comma |
Eli Friedman | d8bfe7f | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 1498 | }; |
| 1499 | } // end anonymous namespace |
| 1500 | |
| 1501 | static bool EvaluateFloat(const Expr* E, APFloat& Result, EvalInfo &Info) { |
| 1502 | return FloatExprEvaluator(Info, Result).Visit(const_cast<Expr*>(E)); |
| 1503 | } |
| 1504 | |
Chris Lattner | 019f4e8 | 2008-10-06 05:28:25 +0000 | [diff] [blame] | 1505 | bool FloatExprEvaluator::VisitCallExpr(const CallExpr *E) { |
Douglas Gregor | 3c385e5 | 2009-02-14 18:57:46 +0000 | [diff] [blame] | 1506 | switch (E->isBuiltinCall(Info.Ctx)) { |
Chris Lattner | 34a74ab | 2008-10-06 05:53:16 +0000 | [diff] [blame] | 1507 | default: return false; |
Chris Lattner | 019f4e8 | 2008-10-06 05:28:25 +0000 | [diff] [blame] | 1508 | case Builtin::BI__builtin_huge_val: |
| 1509 | case Builtin::BI__builtin_huge_valf: |
| 1510 | case Builtin::BI__builtin_huge_vall: |
| 1511 | case Builtin::BI__builtin_inf: |
| 1512 | case Builtin::BI__builtin_inff: |
Daniel Dunbar | 7cbed03 | 2008-10-14 05:41:12 +0000 | [diff] [blame] | 1513 | case Builtin::BI__builtin_infl: { |
| 1514 | const llvm::fltSemantics &Sem = |
| 1515 | Info.Ctx.getFloatTypeSemantics(E->getType()); |
Chris Lattner | 34a74ab | 2008-10-06 05:53:16 +0000 | [diff] [blame] | 1516 | Result = llvm::APFloat::getInf(Sem); |
| 1517 | return true; |
Daniel Dunbar | 7cbed03 | 2008-10-14 05:41:12 +0000 | [diff] [blame] | 1518 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1519 | |
Chris Lattner | 9e62171 | 2008-10-06 06:31:58 +0000 | [diff] [blame] | 1520 | case Builtin::BI__builtin_nan: |
| 1521 | case Builtin::BI__builtin_nanf: |
| 1522 | case Builtin::BI__builtin_nanl: |
Mike Stump | 4572bab | 2009-05-30 03:56:50 +0000 | [diff] [blame] | 1523 | // If this is __builtin_nan() turn this into a nan, otherwise we |
Chris Lattner | 9e62171 | 2008-10-06 06:31:58 +0000 | [diff] [blame] | 1524 | // can't constant fold it. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1525 | if (const StringLiteral *S = |
Chris Lattner | 9e62171 | 2008-10-06 06:31:58 +0000 | [diff] [blame] | 1526 | dyn_cast<StringLiteral>(E->getArg(0)->IgnoreParenCasts())) { |
Mike Stump | 4572bab | 2009-05-30 03:56:50 +0000 | [diff] [blame] | 1527 | if (!S->isWide()) { |
Daniel Dunbar | 7cbed03 | 2008-10-14 05:41:12 +0000 | [diff] [blame] | 1528 | const llvm::fltSemantics &Sem = |
| 1529 | Info.Ctx.getFloatTypeSemantics(E->getType()); |
Mike Stump | 7462b39 | 2009-05-30 14:43:18 +0000 | [diff] [blame] | 1530 | llvm::SmallString<16> s; |
| 1531 | s.append(S->getStrData(), S->getStrData() + S->getByteLength()); |
| 1532 | s += '\0'; |
Mike Stump | 4572bab | 2009-05-30 03:56:50 +0000 | [diff] [blame] | 1533 | long l; |
| 1534 | char *endp; |
Mike Stump | 7462b39 | 2009-05-30 14:43:18 +0000 | [diff] [blame] | 1535 | l = strtol(&s[0], &endp, 0); |
| 1536 | if (endp != s.end()-1) |
Mike Stump | 4572bab | 2009-05-30 03:56:50 +0000 | [diff] [blame] | 1537 | return false; |
| 1538 | unsigned type = (unsigned int)l;; |
| 1539 | Result = llvm::APFloat::getNaN(Sem, false, type); |
Chris Lattner | 9e62171 | 2008-10-06 06:31:58 +0000 | [diff] [blame] | 1540 | return true; |
| 1541 | } |
| 1542 | } |
| 1543 | return false; |
Daniel Dunbar | 5db4b3f | 2008-10-16 03:51:50 +0000 | [diff] [blame] | 1544 | |
| 1545 | case Builtin::BI__builtin_fabs: |
| 1546 | case Builtin::BI__builtin_fabsf: |
| 1547 | case Builtin::BI__builtin_fabsl: |
| 1548 | if (!EvaluateFloat(E->getArg(0), Result, Info)) |
| 1549 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1550 | |
Daniel Dunbar | 5db4b3f | 2008-10-16 03:51:50 +0000 | [diff] [blame] | 1551 | if (Result.isNegative()) |
| 1552 | Result.changeSign(); |
| 1553 | return true; |
| 1554 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1555 | case Builtin::BI__builtin_copysign: |
| 1556 | case Builtin::BI__builtin_copysignf: |
Daniel Dunbar | 5db4b3f | 2008-10-16 03:51:50 +0000 | [diff] [blame] | 1557 | case Builtin::BI__builtin_copysignl: { |
| 1558 | APFloat RHS(0.); |
| 1559 | if (!EvaluateFloat(E->getArg(0), Result, Info) || |
| 1560 | !EvaluateFloat(E->getArg(1), RHS, Info)) |
| 1561 | return false; |
| 1562 | Result.copySign(RHS); |
| 1563 | return true; |
| 1564 | } |
Chris Lattner | 019f4e8 | 2008-10-06 05:28:25 +0000 | [diff] [blame] | 1565 | } |
| 1566 | } |
| 1567 | |
Daniel Dunbar | 5db4b3f | 2008-10-16 03:51:50 +0000 | [diff] [blame] | 1568 | bool FloatExprEvaluator::VisitUnaryOperator(const UnaryOperator *E) { |
Nuno Lopes | a468d34 | 2008-11-19 17:44:31 +0000 | [diff] [blame] | 1569 | if (E->getOpcode() == UnaryOperator::Deref) |
| 1570 | return false; |
| 1571 | |
Daniel Dunbar | 5db4b3f | 2008-10-16 03:51:50 +0000 | [diff] [blame] | 1572 | if (!EvaluateFloat(E->getSubExpr(), Result, Info)) |
| 1573 | return false; |
| 1574 | |
| 1575 | switch (E->getOpcode()) { |
| 1576 | default: return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1577 | case UnaryOperator::Plus: |
Daniel Dunbar | 5db4b3f | 2008-10-16 03:51:50 +0000 | [diff] [blame] | 1578 | return true; |
| 1579 | case UnaryOperator::Minus: |
| 1580 | Result.changeSign(); |
| 1581 | return true; |
| 1582 | } |
| 1583 | } |
Chris Lattner | 019f4e8 | 2008-10-06 05:28:25 +0000 | [diff] [blame] | 1584 | |
Eli Friedman | d8bfe7f | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 1585 | bool FloatExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) { |
| 1586 | // FIXME: Diagnostics? I really don't understand how the warnings |
| 1587 | // and errors are supposed to work. |
Daniel Dunbar | 5db4b3f | 2008-10-16 03:51:50 +0000 | [diff] [blame] | 1588 | APFloat RHS(0.0); |
Eli Friedman | d8bfe7f | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 1589 | if (!EvaluateFloat(E->getLHS(), Result, Info)) |
| 1590 | return false; |
| 1591 | if (!EvaluateFloat(E->getRHS(), RHS, Info)) |
| 1592 | return false; |
| 1593 | |
| 1594 | switch (E->getOpcode()) { |
| 1595 | default: return false; |
| 1596 | case BinaryOperator::Mul: |
| 1597 | Result.multiply(RHS, APFloat::rmNearestTiesToEven); |
| 1598 | return true; |
| 1599 | case BinaryOperator::Add: |
| 1600 | Result.add(RHS, APFloat::rmNearestTiesToEven); |
| 1601 | return true; |
| 1602 | case BinaryOperator::Sub: |
| 1603 | Result.subtract(RHS, APFloat::rmNearestTiesToEven); |
| 1604 | return true; |
| 1605 | case BinaryOperator::Div: |
| 1606 | Result.divide(RHS, APFloat::rmNearestTiesToEven); |
| 1607 | return true; |
Eli Friedman | d8bfe7f | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 1608 | } |
| 1609 | } |
| 1610 | |
| 1611 | bool FloatExprEvaluator::VisitFloatingLiteral(const FloatingLiteral *E) { |
| 1612 | Result = E->getValue(); |
| 1613 | return true; |
| 1614 | } |
| 1615 | |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 1616 | bool FloatExprEvaluator::VisitCastExpr(CastExpr *E) { |
| 1617 | Expr* SubExpr = E->getSubExpr(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1618 | |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 1619 | if (SubExpr->getType()->isIntegralType()) { |
| 1620 | APSInt IntResult; |
Daniel Dunbar | 3f7d995 | 2009-02-19 18:37:50 +0000 | [diff] [blame] | 1621 | if (!EvaluateInteger(SubExpr, IntResult, Info)) |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 1622 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1623 | Result = HandleIntToFloatCast(E->getType(), SubExpr->getType(), |
Daniel Dunbar | a2cfd34 | 2009-01-29 06:16:07 +0000 | [diff] [blame] | 1624 | IntResult, Info.Ctx); |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 1625 | return true; |
| 1626 | } |
| 1627 | if (SubExpr->getType()->isRealFloatingType()) { |
| 1628 | if (!Visit(SubExpr)) |
| 1629 | return false; |
Daniel Dunbar | a2cfd34 | 2009-01-29 06:16:07 +0000 | [diff] [blame] | 1630 | Result = HandleFloatToFloatCast(E->getType(), SubExpr->getType(), |
| 1631 | Result, Info.Ctx); |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 1632 | return true; |
| 1633 | } |
Eli Friedman | 2217c87 | 2009-02-22 11:46:18 +0000 | [diff] [blame] | 1634 | // FIXME: Handle complex types |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 1635 | |
| 1636 | return false; |
| 1637 | } |
| 1638 | |
| 1639 | bool FloatExprEvaluator::VisitCXXZeroInitValueExpr(CXXZeroInitValueExpr *E) { |
| 1640 | Result = APFloat::getZero(Info.Ctx.getFloatTypeSemantics(E->getType())); |
| 1641 | return true; |
| 1642 | } |
| 1643 | |
Eli Friedman | d8bfe7f | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 1644 | //===----------------------------------------------------------------------===// |
Daniel Dunbar | a5fd07b | 2009-01-28 22:24:07 +0000 | [diff] [blame] | 1645 | // Complex Evaluation (for float and integer) |
Anders Carlsson | 9ad16ae | 2008-11-16 20:27:53 +0000 | [diff] [blame] | 1646 | //===----------------------------------------------------------------------===// |
| 1647 | |
| 1648 | namespace { |
Daniel Dunbar | a5fd07b | 2009-01-28 22:24:07 +0000 | [diff] [blame] | 1649 | class VISIBILITY_HIDDEN ComplexExprEvaluator |
| 1650 | : public StmtVisitor<ComplexExprEvaluator, APValue> { |
Anders Carlsson | 9ad16ae | 2008-11-16 20:27:53 +0000 | [diff] [blame] | 1651 | EvalInfo &Info; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1652 | |
Anders Carlsson | 9ad16ae | 2008-11-16 20:27:53 +0000 | [diff] [blame] | 1653 | public: |
Daniel Dunbar | a5fd07b | 2009-01-28 22:24:07 +0000 | [diff] [blame] | 1654 | ComplexExprEvaluator(EvalInfo &info) : Info(info) {} |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1655 | |
Anders Carlsson | 9ad16ae | 2008-11-16 20:27:53 +0000 | [diff] [blame] | 1656 | //===--------------------------------------------------------------------===// |
| 1657 | // Visitor Methods |
| 1658 | //===--------------------------------------------------------------------===// |
| 1659 | |
| 1660 | APValue VisitStmt(Stmt *S) { |
Anders Carlsson | 9ad16ae | 2008-11-16 20:27:53 +0000 | [diff] [blame] | 1661 | return APValue(); |
| 1662 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1663 | |
Anders Carlsson | 9ad16ae | 2008-11-16 20:27:53 +0000 | [diff] [blame] | 1664 | APValue VisitParenExpr(ParenExpr *E) { return Visit(E->getSubExpr()); } |
| 1665 | |
| 1666 | APValue VisitImaginaryLiteral(ImaginaryLiteral *E) { |
Daniel Dunbar | a5fd07b | 2009-01-28 22:24:07 +0000 | [diff] [blame] | 1667 | Expr* SubExpr = E->getSubExpr(); |
| 1668 | |
| 1669 | if (SubExpr->getType()->isRealFloatingType()) { |
| 1670 | APFloat Result(0.0); |
| 1671 | |
| 1672 | if (!EvaluateFloat(SubExpr, Result, Info)) |
| 1673 | return APValue(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1674 | |
| 1675 | return APValue(APFloat(Result.getSemantics(), APFloat::fcZero, false), |
Daniel Dunbar | a5fd07b | 2009-01-28 22:24:07 +0000 | [diff] [blame] | 1676 | Result); |
| 1677 | } else { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1678 | assert(SubExpr->getType()->isIntegerType() && |
Daniel Dunbar | a5fd07b | 2009-01-28 22:24:07 +0000 | [diff] [blame] | 1679 | "Unexpected imaginary literal."); |
| 1680 | |
| 1681 | llvm::APSInt Result; |
| 1682 | if (!EvaluateInteger(SubExpr, Result, Info)) |
| 1683 | return APValue(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1684 | |
Daniel Dunbar | a5fd07b | 2009-01-28 22:24:07 +0000 | [diff] [blame] | 1685 | llvm::APSInt Zero(Result.getBitWidth(), !Result.isSigned()); |
| 1686 | Zero = 0; |
| 1687 | return APValue(Zero, Result); |
| 1688 | } |
Anders Carlsson | 9ad16ae | 2008-11-16 20:27:53 +0000 | [diff] [blame] | 1689 | } |
| 1690 | |
Anders Carlsson | ccc3fce | 2008-11-16 21:51:21 +0000 | [diff] [blame] | 1691 | APValue VisitCastExpr(CastExpr *E) { |
| 1692 | Expr* SubExpr = E->getSubExpr(); |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1693 | QualType EltType = E->getType()->getAs<ComplexType>()->getElementType(); |
Daniel Dunbar | a2cfd34 | 2009-01-29 06:16:07 +0000 | [diff] [blame] | 1694 | QualType SubType = SubExpr->getType(); |
Anders Carlsson | ccc3fce | 2008-11-16 21:51:21 +0000 | [diff] [blame] | 1695 | |
Daniel Dunbar | a2cfd34 | 2009-01-29 06:16:07 +0000 | [diff] [blame] | 1696 | if (SubType->isRealFloatingType()) { |
Anders Carlsson | ccc3fce | 2008-11-16 21:51:21 +0000 | [diff] [blame] | 1697 | APFloat Result(0.0); |
Eli Friedman | 1725f68 | 2009-04-22 19:23:09 +0000 | [diff] [blame] | 1698 | |
Anders Carlsson | ccc3fce | 2008-11-16 21:51:21 +0000 | [diff] [blame] | 1699 | if (!EvaluateFloat(SubExpr, Result, Info)) |
| 1700 | return APValue(); |
Eli Friedman | 1725f68 | 2009-04-22 19:23:09 +0000 | [diff] [blame] | 1701 | |
| 1702 | if (EltType->isRealFloatingType()) { |
| 1703 | Result = HandleFloatToFloatCast(EltType, SubType, Result, Info.Ctx); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1704 | return APValue(Result, |
Eli Friedman | 1725f68 | 2009-04-22 19:23:09 +0000 | [diff] [blame] | 1705 | APFloat(Result.getSemantics(), APFloat::fcZero, false)); |
| 1706 | } else { |
| 1707 | llvm::APSInt IResult; |
| 1708 | IResult = HandleFloatToIntCast(EltType, SubType, Result, Info.Ctx); |
| 1709 | llvm::APSInt Zero(IResult.getBitWidth(), !IResult.isSigned()); |
| 1710 | Zero = 0; |
| 1711 | return APValue(IResult, Zero); |
| 1712 | } |
Daniel Dunbar | a2cfd34 | 2009-01-29 06:16:07 +0000 | [diff] [blame] | 1713 | } else if (SubType->isIntegerType()) { |
Daniel Dunbar | a5fd07b | 2009-01-28 22:24:07 +0000 | [diff] [blame] | 1714 | APSInt Result; |
Eli Friedman | 1725f68 | 2009-04-22 19:23:09 +0000 | [diff] [blame] | 1715 | |
Daniel Dunbar | a5fd07b | 2009-01-28 22:24:07 +0000 | [diff] [blame] | 1716 | if (!EvaluateInteger(SubExpr, Result, Info)) |
| 1717 | return APValue(); |
Daniel Dunbar | a2cfd34 | 2009-01-29 06:16:07 +0000 | [diff] [blame] | 1718 | |
Eli Friedman | 1725f68 | 2009-04-22 19:23:09 +0000 | [diff] [blame] | 1719 | if (EltType->isRealFloatingType()) { |
| 1720 | APFloat FResult = |
| 1721 | HandleIntToFloatCast(EltType, SubType, Result, Info.Ctx); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1722 | return APValue(FResult, |
Eli Friedman | 1725f68 | 2009-04-22 19:23:09 +0000 | [diff] [blame] | 1723 | APFloat(FResult.getSemantics(), APFloat::fcZero, false)); |
| 1724 | } else { |
| 1725 | Result = HandleIntToIntCast(EltType, SubType, Result, Info.Ctx); |
| 1726 | llvm::APSInt Zero(Result.getBitWidth(), !Result.isSigned()); |
| 1727 | Zero = 0; |
| 1728 | return APValue(Result, Zero); |
| 1729 | } |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1730 | } else if (const ComplexType *CT = SubType->getAs<ComplexType>()) { |
Daniel Dunbar | a2cfd34 | 2009-01-29 06:16:07 +0000 | [diff] [blame] | 1731 | APValue Src; |
Eli Friedman | 1725f68 | 2009-04-22 19:23:09 +0000 | [diff] [blame] | 1732 | |
Daniel Dunbar | a2cfd34 | 2009-01-29 06:16:07 +0000 | [diff] [blame] | 1733 | if (!EvaluateComplex(SubExpr, Src, Info)) |
| 1734 | return APValue(); |
| 1735 | |
| 1736 | QualType SrcType = CT->getElementType(); |
| 1737 | |
| 1738 | if (Src.isComplexFloat()) { |
| 1739 | if (EltType->isRealFloatingType()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1740 | return APValue(HandleFloatToFloatCast(EltType, SrcType, |
Daniel Dunbar | a2cfd34 | 2009-01-29 06:16:07 +0000 | [diff] [blame] | 1741 | Src.getComplexFloatReal(), |
| 1742 | Info.Ctx), |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1743 | HandleFloatToFloatCast(EltType, SrcType, |
Daniel Dunbar | a2cfd34 | 2009-01-29 06:16:07 +0000 | [diff] [blame] | 1744 | Src.getComplexFloatImag(), |
| 1745 | Info.Ctx)); |
| 1746 | } else { |
| 1747 | return APValue(HandleFloatToIntCast(EltType, SrcType, |
| 1748 | Src.getComplexFloatReal(), |
| 1749 | Info.Ctx), |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1750 | HandleFloatToIntCast(EltType, SrcType, |
Daniel Dunbar | a2cfd34 | 2009-01-29 06:16:07 +0000 | [diff] [blame] | 1751 | Src.getComplexFloatImag(), |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1752 | Info.Ctx)); |
Daniel Dunbar | a2cfd34 | 2009-01-29 06:16:07 +0000 | [diff] [blame] | 1753 | } |
| 1754 | } else { |
| 1755 | assert(Src.isComplexInt() && "Invalid evaluate result."); |
| 1756 | if (EltType->isRealFloatingType()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1757 | return APValue(HandleIntToFloatCast(EltType, SrcType, |
Daniel Dunbar | a2cfd34 | 2009-01-29 06:16:07 +0000 | [diff] [blame] | 1758 | Src.getComplexIntReal(), |
| 1759 | Info.Ctx), |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1760 | HandleIntToFloatCast(EltType, SrcType, |
Daniel Dunbar | a2cfd34 | 2009-01-29 06:16:07 +0000 | [diff] [blame] | 1761 | Src.getComplexIntImag(), |
| 1762 | Info.Ctx)); |
| 1763 | } else { |
| 1764 | return APValue(HandleIntToIntCast(EltType, SrcType, |
| 1765 | Src.getComplexIntReal(), |
| 1766 | Info.Ctx), |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1767 | HandleIntToIntCast(EltType, SrcType, |
Daniel Dunbar | a2cfd34 | 2009-01-29 06:16:07 +0000 | [diff] [blame] | 1768 | Src.getComplexIntImag(), |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1769 | Info.Ctx)); |
Daniel Dunbar | a2cfd34 | 2009-01-29 06:16:07 +0000 | [diff] [blame] | 1770 | } |
| 1771 | } |
Anders Carlsson | ccc3fce | 2008-11-16 21:51:21 +0000 | [diff] [blame] | 1772 | } |
| 1773 | |
| 1774 | // FIXME: Handle more casts. |
| 1775 | return APValue(); |
| 1776 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1777 | |
Anders Carlsson | ccc3fce | 2008-11-16 21:51:21 +0000 | [diff] [blame] | 1778 | APValue VisitBinaryOperator(const BinaryOperator *E); |
Eli Friedman | ba98d6b | 2009-03-23 04:56:01 +0000 | [diff] [blame] | 1779 | APValue VisitChooseExpr(const ChooseExpr *E) |
| 1780 | { return Visit(E->getChosenSubExpr(Info.Ctx)); } |
| 1781 | APValue VisitUnaryExtension(const UnaryOperator *E) |
| 1782 | { return Visit(E->getSubExpr()); } |
| 1783 | // FIXME Missing: unary +/-/~, binary div, ImplicitValueInitExpr, |
Eli Friedman | 2217c87 | 2009-02-22 11:46:18 +0000 | [diff] [blame] | 1784 | // conditional ?:, comma |
Anders Carlsson | 9ad16ae | 2008-11-16 20:27:53 +0000 | [diff] [blame] | 1785 | }; |
| 1786 | } // end anonymous namespace |
| 1787 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1788 | static bool EvaluateComplex(const Expr *E, APValue &Result, EvalInfo &Info) { |
Daniel Dunbar | a5fd07b | 2009-01-28 22:24:07 +0000 | [diff] [blame] | 1789 | Result = ComplexExprEvaluator(Info).Visit(const_cast<Expr*>(E)); |
| 1790 | assert((!Result.isComplexFloat() || |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1791 | (&Result.getComplexFloatReal().getSemantics() == |
| 1792 | &Result.getComplexFloatImag().getSemantics())) && |
Daniel Dunbar | a5fd07b | 2009-01-28 22:24:07 +0000 | [diff] [blame] | 1793 | "Invalid complex evaluation."); |
| 1794 | return Result.isComplexFloat() || Result.isComplexInt(); |
Anders Carlsson | 9ad16ae | 2008-11-16 20:27:53 +0000 | [diff] [blame] | 1795 | } |
| 1796 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1797 | APValue ComplexExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) { |
Anders Carlsson | ccc3fce | 2008-11-16 21:51:21 +0000 | [diff] [blame] | 1798 | APValue Result, RHS; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1799 | |
Daniel Dunbar | a5fd07b | 2009-01-28 22:24:07 +0000 | [diff] [blame] | 1800 | if (!EvaluateComplex(E->getLHS(), Result, Info)) |
Anders Carlsson | ccc3fce | 2008-11-16 21:51:21 +0000 | [diff] [blame] | 1801 | return APValue(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1802 | |
Daniel Dunbar | a5fd07b | 2009-01-28 22:24:07 +0000 | [diff] [blame] | 1803 | if (!EvaluateComplex(E->getRHS(), RHS, Info)) |
Anders Carlsson | ccc3fce | 2008-11-16 21:51:21 +0000 | [diff] [blame] | 1804 | return APValue(); |
Daniel Dunbar | a5fd07b | 2009-01-28 22:24:07 +0000 | [diff] [blame] | 1805 | |
Daniel Dunbar | 3f27987 | 2009-01-29 01:32:56 +0000 | [diff] [blame] | 1806 | assert(Result.isComplexFloat() == RHS.isComplexFloat() && |
| 1807 | "Invalid operands to binary operator."); |
Anders Carlsson | ccc3fce | 2008-11-16 21:51:21 +0000 | [diff] [blame] | 1808 | switch (E->getOpcode()) { |
| 1809 | default: return APValue(); |
| 1810 | case BinaryOperator::Add: |
Daniel Dunbar | a5fd07b | 2009-01-28 22:24:07 +0000 | [diff] [blame] | 1811 | if (Result.isComplexFloat()) { |
| 1812 | Result.getComplexFloatReal().add(RHS.getComplexFloatReal(), |
| 1813 | APFloat::rmNearestTiesToEven); |
| 1814 | Result.getComplexFloatImag().add(RHS.getComplexFloatImag(), |
| 1815 | APFloat::rmNearestTiesToEven); |
| 1816 | } else { |
| 1817 | Result.getComplexIntReal() += RHS.getComplexIntReal(); |
| 1818 | Result.getComplexIntImag() += RHS.getComplexIntImag(); |
| 1819 | } |
Daniel Dunbar | 3f27987 | 2009-01-29 01:32:56 +0000 | [diff] [blame] | 1820 | break; |
Anders Carlsson | ccc3fce | 2008-11-16 21:51:21 +0000 | [diff] [blame] | 1821 | case BinaryOperator::Sub: |
Daniel Dunbar | a5fd07b | 2009-01-28 22:24:07 +0000 | [diff] [blame] | 1822 | if (Result.isComplexFloat()) { |
| 1823 | Result.getComplexFloatReal().subtract(RHS.getComplexFloatReal(), |
| 1824 | APFloat::rmNearestTiesToEven); |
| 1825 | Result.getComplexFloatImag().subtract(RHS.getComplexFloatImag(), |
| 1826 | APFloat::rmNearestTiesToEven); |
| 1827 | } else { |
| 1828 | Result.getComplexIntReal() -= RHS.getComplexIntReal(); |
| 1829 | Result.getComplexIntImag() -= RHS.getComplexIntImag(); |
| 1830 | } |
Daniel Dunbar | 3f27987 | 2009-01-29 01:32:56 +0000 | [diff] [blame] | 1831 | break; |
| 1832 | case BinaryOperator::Mul: |
| 1833 | if (Result.isComplexFloat()) { |
| 1834 | APValue LHS = Result; |
| 1835 | APFloat &LHS_r = LHS.getComplexFloatReal(); |
| 1836 | APFloat &LHS_i = LHS.getComplexFloatImag(); |
| 1837 | APFloat &RHS_r = RHS.getComplexFloatReal(); |
| 1838 | APFloat &RHS_i = RHS.getComplexFloatImag(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1839 | |
Daniel Dunbar | 3f27987 | 2009-01-29 01:32:56 +0000 | [diff] [blame] | 1840 | APFloat Tmp = LHS_r; |
| 1841 | Tmp.multiply(RHS_r, APFloat::rmNearestTiesToEven); |
| 1842 | Result.getComplexFloatReal() = Tmp; |
| 1843 | Tmp = LHS_i; |
| 1844 | Tmp.multiply(RHS_i, APFloat::rmNearestTiesToEven); |
| 1845 | Result.getComplexFloatReal().subtract(Tmp, APFloat::rmNearestTiesToEven); |
| 1846 | |
| 1847 | Tmp = LHS_r; |
| 1848 | Tmp.multiply(RHS_i, APFloat::rmNearestTiesToEven); |
| 1849 | Result.getComplexFloatImag() = Tmp; |
| 1850 | Tmp = LHS_i; |
| 1851 | Tmp.multiply(RHS_r, APFloat::rmNearestTiesToEven); |
| 1852 | Result.getComplexFloatImag().add(Tmp, APFloat::rmNearestTiesToEven); |
| 1853 | } else { |
| 1854 | APValue LHS = Result; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1855 | Result.getComplexIntReal() = |
Daniel Dunbar | 3f27987 | 2009-01-29 01:32:56 +0000 | [diff] [blame] | 1856 | (LHS.getComplexIntReal() * RHS.getComplexIntReal() - |
| 1857 | LHS.getComplexIntImag() * RHS.getComplexIntImag()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1858 | Result.getComplexIntImag() = |
Daniel Dunbar | 3f27987 | 2009-01-29 01:32:56 +0000 | [diff] [blame] | 1859 | (LHS.getComplexIntReal() * RHS.getComplexIntImag() + |
| 1860 | LHS.getComplexIntImag() * RHS.getComplexIntReal()); |
| 1861 | } |
| 1862 | break; |
Anders Carlsson | ccc3fce | 2008-11-16 21:51:21 +0000 | [diff] [blame] | 1863 | } |
| 1864 | |
| 1865 | return Result; |
| 1866 | } |
| 1867 | |
Anders Carlsson | 9ad16ae | 2008-11-16 20:27:53 +0000 | [diff] [blame] | 1868 | //===----------------------------------------------------------------------===// |
Chris Lattner | 6ee7aa1 | 2008-11-16 21:24:15 +0000 | [diff] [blame] | 1869 | // Top level Expr::Evaluate method. |
Chris Lattner | f5eeb05 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 1870 | //===----------------------------------------------------------------------===// |
| 1871 | |
Chris Lattner | 6ee7aa1 | 2008-11-16 21:24:15 +0000 | [diff] [blame] | 1872 | /// Evaluate - Return true if this is a constant which we can fold using |
Chris Lattner | 019f4e8 | 2008-10-06 05:28:25 +0000 | [diff] [blame] | 1873 | /// any crazy technique (that has nothing to do with language standards) that |
| 1874 | /// we want to. If this function returns true, it returns the folded constant |
| 1875 | /// in Result. |
Anders Carlsson | 5b45d4e | 2008-11-30 16:58:53 +0000 | [diff] [blame] | 1876 | bool Expr::Evaluate(EvalResult &Result, ASTContext &Ctx) const { |
| 1877 | EvalInfo Info(Ctx, Result); |
Anders Carlsson | 54da049 | 2008-11-30 16:38:33 +0000 | [diff] [blame] | 1878 | |
Nate Begeman | 59b5da6 | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 1879 | if (getType()->isVectorType()) { |
| 1880 | if (!EvaluateVector(this, Result.Val, Info)) |
| 1881 | return false; |
| 1882 | } else if (getType()->isIntegerType()) { |
Daniel Dunbar | 30c37f4 | 2009-02-19 20:17:33 +0000 | [diff] [blame] | 1883 | if (!IntExprEvaluator(Info, Result.Val).Visit(const_cast<Expr*>(this))) |
Anders Carlsson | 6dde0d5 | 2008-11-22 21:50:49 +0000 | [diff] [blame] | 1884 | return false; |
Daniel Dunbar | 8958891 | 2009-02-26 20:52:22 +0000 | [diff] [blame] | 1885 | } else if (getType()->hasPointerRepresentation()) { |
Anders Carlsson | 5b45d4e | 2008-11-30 16:58:53 +0000 | [diff] [blame] | 1886 | if (!EvaluatePointer(this, Result.Val, Info)) |
Anders Carlsson | 6dde0d5 | 2008-11-22 21:50:49 +0000 | [diff] [blame] | 1887 | return false; |
Eli Friedman | d8bfe7f | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 1888 | } else if (getType()->isRealFloatingType()) { |
| 1889 | llvm::APFloat f(0.0); |
Anders Carlsson | 6dde0d5 | 2008-11-22 21:50:49 +0000 | [diff] [blame] | 1890 | if (!EvaluateFloat(this, f, Info)) |
| 1891 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1892 | |
Anders Carlsson | 5b45d4e | 2008-11-30 16:58:53 +0000 | [diff] [blame] | 1893 | Result.Val = APValue(f); |
Daniel Dunbar | a5fd07b | 2009-01-28 22:24:07 +0000 | [diff] [blame] | 1894 | } else if (getType()->isAnyComplexType()) { |
| 1895 | if (!EvaluateComplex(this, Result.Val, Info)) |
Anders Carlsson | 6dde0d5 | 2008-11-22 21:50:49 +0000 | [diff] [blame] | 1896 | return false; |
Daniel Dunbar | a5fd07b | 2009-01-28 22:24:07 +0000 | [diff] [blame] | 1897 | } else |
Anders Carlsson | 9d4c157 | 2008-11-22 22:56:32 +0000 | [diff] [blame] | 1898 | return false; |
Anders Carlsson | 6dde0d5 | 2008-11-22 21:50:49 +0000 | [diff] [blame] | 1899 | |
Anders Carlsson | 5b45d4e | 2008-11-30 16:58:53 +0000 | [diff] [blame] | 1900 | return true; |
| 1901 | } |
| 1902 | |
Mike Stump | 660e6f7 | 2009-10-26 23:05:19 +0000 | [diff] [blame] | 1903 | bool Expr::EvaluateAsAny(EvalResult &Result, ASTContext &Ctx) const { |
| 1904 | EvalInfo Info(Ctx, Result, true); |
| 1905 | |
| 1906 | if (getType()->isVectorType()) { |
| 1907 | if (!EvaluateVector(this, Result.Val, Info)) |
| 1908 | return false; |
| 1909 | } else if (getType()->isIntegerType()) { |
| 1910 | if (!IntExprEvaluator(Info, Result.Val).Visit(const_cast<Expr*>(this))) |
| 1911 | return false; |
| 1912 | } else if (getType()->hasPointerRepresentation()) { |
| 1913 | if (!EvaluatePointer(this, Result.Val, Info)) |
| 1914 | return false; |
| 1915 | } else if (getType()->isRealFloatingType()) { |
| 1916 | llvm::APFloat f(0.0); |
| 1917 | if (!EvaluateFloat(this, f, Info)) |
| 1918 | return false; |
| 1919 | |
| 1920 | Result.Val = APValue(f); |
| 1921 | } else if (getType()->isAnyComplexType()) { |
| 1922 | if (!EvaluateComplex(this, Result.Val, Info)) |
| 1923 | return false; |
| 1924 | } else |
| 1925 | return false; |
| 1926 | |
| 1927 | return true; |
| 1928 | } |
| 1929 | |
Anders Carlsson | 1b78276 | 2009-04-10 04:54:13 +0000 | [diff] [blame] | 1930 | bool Expr::EvaluateAsLValue(EvalResult &Result, ASTContext &Ctx) const { |
| 1931 | EvalInfo Info(Ctx, Result); |
| 1932 | |
| 1933 | return EvaluateLValue(this, Result.Val, Info) && !Result.HasSideEffects; |
| 1934 | } |
| 1935 | |
Eli Friedman | b2f295c | 2009-09-13 10:17:44 +0000 | [diff] [blame] | 1936 | bool Expr::EvaluateAsAnyLValue(EvalResult &Result, ASTContext &Ctx) const { |
| 1937 | EvalInfo Info(Ctx, Result, true); |
| 1938 | |
| 1939 | return EvaluateLValue(this, Result.Val, Info) && !Result.HasSideEffects; |
| 1940 | } |
| 1941 | |
Chris Lattner | 6ee7aa1 | 2008-11-16 21:24:15 +0000 | [diff] [blame] | 1942 | /// isEvaluatable - Call Evaluate to see if this expression can be constant |
Chris Lattner | 45b6b9d | 2008-10-06 06:49:02 +0000 | [diff] [blame] | 1943 | /// folded, but discard the result. |
| 1944 | bool Expr::isEvaluatable(ASTContext &Ctx) const { |
Anders Carlsson | 4fdfb09 | 2008-12-01 06:44:05 +0000 | [diff] [blame] | 1945 | EvalResult Result; |
| 1946 | return Evaluate(Result, Ctx) && !Result.HasSideEffects; |
Chris Lattner | 45b6b9d | 2008-10-06 06:49:02 +0000 | [diff] [blame] | 1947 | } |
Anders Carlsson | 51fe996 | 2008-11-22 21:04:56 +0000 | [diff] [blame] | 1948 | |
| 1949 | APSInt Expr::EvaluateAsInt(ASTContext &Ctx) const { |
Anders Carlsson | 1c0cfd4 | 2008-12-19 20:58:05 +0000 | [diff] [blame] | 1950 | EvalResult EvalResult; |
| 1951 | bool Result = Evaluate(EvalResult, Ctx); |
Daniel Dunbar | f185319 | 2009-01-15 18:32:35 +0000 | [diff] [blame] | 1952 | Result = Result; |
Anders Carlsson | 51fe996 | 2008-11-22 21:04:56 +0000 | [diff] [blame] | 1953 | assert(Result && "Could not evaluate expression"); |
Anders Carlsson | 1c0cfd4 | 2008-12-19 20:58:05 +0000 | [diff] [blame] | 1954 | assert(EvalResult.Val.isInt() && "Expression did not evaluate to integer"); |
Anders Carlsson | 51fe996 | 2008-11-22 21:04:56 +0000 | [diff] [blame] | 1955 | |
Anders Carlsson | 1c0cfd4 | 2008-12-19 20:58:05 +0000 | [diff] [blame] | 1956 | return EvalResult.Val.getInt(); |
Anders Carlsson | 51fe996 | 2008-11-22 21:04:56 +0000 | [diff] [blame] | 1957 | } |