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