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" |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +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" |
Anders Carlsson | 06a3675 | 2008-07-08 05:49:43 +0000 | [diff] [blame] | 19 | #include "clang/Basic/TargetInfo.h" |
Anders Carlsson | c754aa6 | 2008-07-08 05:13:58 +0000 | [diff] [blame] | 20 | #include "llvm/Support/Compiler.h" |
Anders Carlsson | c44eec6 | 2008-07-03 04:20:39 +0000 | [diff] [blame] | 21 | using namespace clang; |
Chris Lattner | f5eeb05 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 22 | using llvm::APSInt; |
Eli Friedman | d8bfe7f | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 23 | using llvm::APFloat; |
Anders Carlsson | c44eec6 | 2008-07-03 04:20:39 +0000 | [diff] [blame] | 24 | |
Chris Lattner | 87eae5e | 2008-07-11 22:52:41 +0000 | [diff] [blame] | 25 | /// EvalInfo - This is a private struct used by the evaluator to capture |
| 26 | /// information about a subexpression as it is folded. It retains information |
| 27 | /// about the AST context, but also maintains information about the folded |
| 28 | /// expression. |
| 29 | /// |
| 30 | /// If an expression could be evaluated, it is still possible it is not a C |
| 31 | /// "integer constant expression" or constant expression. If not, this struct |
| 32 | /// captures information about how and why not. |
| 33 | /// |
| 34 | /// One bit of information passed *into* the request for constant folding |
| 35 | /// indicates whether the subexpression is "evaluated" or not according to C |
| 36 | /// rules. For example, the RHS of (0 && foo()) is not evaluated. We can |
| 37 | /// evaluate the expression regardless of what the RHS is, but C only allows |
| 38 | /// certain things in certain situations. |
| 39 | struct EvalInfo { |
| 40 | ASTContext &Ctx; |
| 41 | |
Anders Carlsson | 54da049 | 2008-11-30 16:38:33 +0000 | [diff] [blame] | 42 | /// EvalResult - Contains information about the evaluation. |
| 43 | Expr::EvalResult &EvalResult; |
Anders Carlsson | f0c1e4b | 2008-11-30 18:26:25 +0000 | [diff] [blame] | 44 | |
| 45 | /// ShortCircuit - will be greater than zero if the current subexpression has |
| 46 | /// will not be evaluated because it's short-circuited (according to C rules). |
| 47 | unsigned ShortCircuit; |
Chris Lattner | 87eae5e | 2008-07-11 22:52:41 +0000 | [diff] [blame] | 48 | |
Anders Carlsson | 54da049 | 2008-11-30 16:38:33 +0000 | [diff] [blame] | 49 | EvalInfo(ASTContext &ctx, Expr::EvalResult& evalresult) : Ctx(ctx), |
Anders Carlsson | f0c1e4b | 2008-11-30 18:26:25 +0000 | [diff] [blame] | 50 | EvalResult(evalresult), ShortCircuit(0) {} |
Chris Lattner | 87eae5e | 2008-07-11 22:52:41 +0000 | [diff] [blame] | 51 | }; |
| 52 | |
| 53 | |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 54 | static bool EvaluateLValue(const Expr *E, APValue &Result, EvalInfo &Info); |
Chris Lattner | 87eae5e | 2008-07-11 22:52:41 +0000 | [diff] [blame] | 55 | static bool EvaluatePointer(const Expr *E, APValue &Result, EvalInfo &Info); |
| 56 | static bool EvaluateInteger(const Expr *E, APSInt &Result, EvalInfo &Info); |
Daniel Dunbar | 69ab26a | 2009-02-20 18:22:23 +0000 | [diff] [blame] | 57 | static bool EvaluateIntegerOrLValue(const Expr *E, APValue &Result, EvalInfo &Info); |
Eli Friedman | d8bfe7f | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 58 | static bool EvaluateFloat(const Expr *E, APFloat &Result, EvalInfo &Info); |
Daniel Dunbar | a5fd07b | 2009-01-28 22:24:07 +0000 | [diff] [blame] | 59 | static bool EvaluateComplex(const Expr *E, APValue &Result, EvalInfo &Info); |
Chris Lattner | f5eeb05 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 60 | |
| 61 | //===----------------------------------------------------------------------===// |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 62 | // Misc utilities |
| 63 | //===----------------------------------------------------------------------===// |
| 64 | |
| 65 | static bool HandleConversionToBool(Expr* E, bool& Result, EvalInfo &Info) { |
| 66 | if (E->getType()->isIntegralType()) { |
| 67 | APSInt IntResult; |
| 68 | if (!EvaluateInteger(E, IntResult, Info)) |
| 69 | return false; |
| 70 | Result = IntResult != 0; |
| 71 | return true; |
| 72 | } else if (E->getType()->isRealFloatingType()) { |
| 73 | APFloat FloatResult(0.0); |
| 74 | if (!EvaluateFloat(E, FloatResult, Info)) |
| 75 | return false; |
| 76 | Result = !FloatResult.isZero(); |
| 77 | return true; |
| 78 | } else if (E->getType()->isPointerType()) { |
| 79 | APValue PointerResult; |
| 80 | if (!EvaluatePointer(E, PointerResult, Info)) |
| 81 | return false; |
| 82 | // FIXME: Is this accurate for all kinds of bases? If not, what would |
| 83 | // the check look like? |
| 84 | Result = PointerResult.getLValueBase() || PointerResult.getLValueOffset(); |
| 85 | return true; |
| 86 | } |
| 87 | |
| 88 | return false; |
| 89 | } |
| 90 | |
Daniel Dunbar | a2cfd34 | 2009-01-29 06:16:07 +0000 | [diff] [blame] | 91 | static APSInt HandleFloatToIntCast(QualType DestType, QualType SrcType, |
| 92 | APFloat &Value, ASTContext &Ctx) { |
| 93 | unsigned DestWidth = Ctx.getIntWidth(DestType); |
| 94 | // Determine whether we are converting to unsigned or signed. |
| 95 | bool DestSigned = DestType->isSignedIntegerType(); |
| 96 | |
| 97 | // FIXME: Warning for overflow. |
| 98 | uint64_t Space[4]; |
| 99 | bool ignored; |
| 100 | (void)Value.convertToInteger(Space, DestWidth, DestSigned, |
| 101 | llvm::APFloat::rmTowardZero, &ignored); |
| 102 | return APSInt(llvm::APInt(DestWidth, 4, Space), !DestSigned); |
| 103 | } |
| 104 | |
| 105 | static APFloat HandleFloatToFloatCast(QualType DestType, QualType SrcType, |
| 106 | APFloat &Value, ASTContext &Ctx) { |
| 107 | bool ignored; |
| 108 | APFloat Result = Value; |
| 109 | Result.convert(Ctx.getFloatTypeSemantics(DestType), |
| 110 | APFloat::rmNearestTiesToEven, &ignored); |
| 111 | return Result; |
| 112 | } |
| 113 | |
| 114 | static APSInt HandleIntToIntCast(QualType DestType, QualType SrcType, |
| 115 | APSInt &Value, ASTContext &Ctx) { |
| 116 | unsigned DestWidth = Ctx.getIntWidth(DestType); |
| 117 | APSInt Result = Value; |
| 118 | // Figure out if this is a truncate, extend or noop cast. |
| 119 | // If the input is signed, do a sign extend, noop, or truncate. |
| 120 | Result.extOrTrunc(DestWidth); |
| 121 | Result.setIsUnsigned(DestType->isUnsignedIntegerType()); |
| 122 | return Result; |
| 123 | } |
| 124 | |
| 125 | static APFloat HandleIntToFloatCast(QualType DestType, QualType SrcType, |
| 126 | APSInt &Value, ASTContext &Ctx) { |
| 127 | |
| 128 | APFloat Result(Ctx.getFloatTypeSemantics(DestType), 1); |
| 129 | Result.convertFromAPInt(Value, Value.isSigned(), |
| 130 | APFloat::rmNearestTiesToEven); |
| 131 | return Result; |
| 132 | } |
| 133 | |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 134 | //===----------------------------------------------------------------------===// |
| 135 | // LValue Evaluation |
| 136 | //===----------------------------------------------------------------------===// |
| 137 | namespace { |
| 138 | class VISIBILITY_HIDDEN LValueExprEvaluator |
| 139 | : public StmtVisitor<LValueExprEvaluator, APValue> { |
| 140 | EvalInfo &Info; |
| 141 | public: |
| 142 | |
| 143 | LValueExprEvaluator(EvalInfo &info) : Info(info) {} |
| 144 | |
| 145 | APValue VisitStmt(Stmt *S) { |
Daniel Dunbar | 8a7b7c6 | 2008-11-12 21:52:46 +0000 | [diff] [blame] | 146 | #if 0 |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 147 | // FIXME: Remove this when we support more expressions. |
| 148 | printf("Unhandled pointer statement\n"); |
| 149 | S->dump(); |
Daniel Dunbar | 8a7b7c6 | 2008-11-12 21:52:46 +0000 | [diff] [blame] | 150 | #endif |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 151 | return APValue(); |
| 152 | } |
| 153 | |
| 154 | APValue VisitParenExpr(ParenExpr *E) { return Visit(E->getSubExpr()); } |
Anders Carlsson | 35873c4 | 2008-11-24 04:41:22 +0000 | [diff] [blame] | 155 | APValue VisitDeclRefExpr(DeclRefExpr *E); |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 156 | APValue VisitPredefinedExpr(PredefinedExpr *E) { return APValue(E, 0); } |
| 157 | APValue VisitCompoundLiteralExpr(CompoundLiteralExpr *E); |
| 158 | APValue VisitMemberExpr(MemberExpr *E); |
| 159 | APValue VisitStringLiteral(StringLiteral *E) { return APValue(E, 0); } |
Anders Carlsson | 3068d11 | 2008-11-16 19:01:22 +0000 | [diff] [blame] | 160 | APValue VisitArraySubscriptExpr(ArraySubscriptExpr *E); |
Eli Friedman | e8761c8 | 2009-02-20 01:57:15 +0000 | [diff] [blame] | 161 | APValue VisitUnaryDeref(UnaryOperator *E); |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 162 | }; |
| 163 | } // end anonymous namespace |
| 164 | |
| 165 | static bool EvaluateLValue(const Expr* E, APValue& Result, EvalInfo &Info) { |
| 166 | Result = LValueExprEvaluator(Info).Visit(const_cast<Expr*>(E)); |
| 167 | return Result.isLValue(); |
| 168 | } |
| 169 | |
Anders Carlsson | 35873c4 | 2008-11-24 04:41:22 +0000 | [diff] [blame] | 170 | APValue LValueExprEvaluator::VisitDeclRefExpr(DeclRefExpr *E) |
| 171 | { |
| 172 | if (!E->hasGlobalStorage()) |
| 173 | return APValue(); |
| 174 | |
| 175 | return APValue(E, 0); |
| 176 | } |
| 177 | |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 178 | APValue LValueExprEvaluator::VisitCompoundLiteralExpr(CompoundLiteralExpr *E) { |
| 179 | if (E->isFileScope()) |
| 180 | return APValue(E, 0); |
| 181 | return APValue(); |
| 182 | } |
| 183 | |
| 184 | APValue LValueExprEvaluator::VisitMemberExpr(MemberExpr *E) { |
| 185 | APValue result; |
| 186 | QualType Ty; |
| 187 | if (E->isArrow()) { |
| 188 | if (!EvaluatePointer(E->getBase(), result, Info)) |
| 189 | return APValue(); |
| 190 | Ty = E->getBase()->getType()->getAsPointerType()->getPointeeType(); |
| 191 | } else { |
| 192 | result = Visit(E->getBase()); |
| 193 | if (result.isUninit()) |
| 194 | return APValue(); |
| 195 | Ty = E->getBase()->getType(); |
| 196 | } |
| 197 | |
| 198 | RecordDecl *RD = Ty->getAsRecordType()->getDecl(); |
| 199 | const ASTRecordLayout &RL = Info.Ctx.getASTRecordLayout(RD); |
Douglas Gregor | 86f1940 | 2008-12-20 23:49:58 +0000 | [diff] [blame] | 200 | |
| 201 | FieldDecl *FD = dyn_cast<FieldDecl>(E->getMemberDecl()); |
| 202 | if (!FD) // FIXME: deal with other kinds of member expressions |
| 203 | return APValue(); |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 204 | |
| 205 | // FIXME: This is linear time. |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 206 | unsigned i = 0; |
| 207 | for (RecordDecl::field_iterator Field = RD->field_begin(), |
| 208 | FieldEnd = RD->field_end(); |
| 209 | Field != FieldEnd; (void)++Field, ++i) { |
| 210 | if (*Field == FD) |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 211 | break; |
| 212 | } |
| 213 | |
| 214 | result.setLValue(result.getLValueBase(), |
| 215 | result.getLValueOffset() + RL.getFieldOffset(i) / 8); |
| 216 | |
| 217 | return result; |
| 218 | } |
| 219 | |
Anders Carlsson | 3068d11 | 2008-11-16 19:01:22 +0000 | [diff] [blame] | 220 | APValue LValueExprEvaluator::VisitArraySubscriptExpr(ArraySubscriptExpr *E) |
| 221 | { |
| 222 | APValue Result; |
| 223 | |
| 224 | if (!EvaluatePointer(E->getBase(), Result, Info)) |
| 225 | return APValue(); |
| 226 | |
| 227 | APSInt Index; |
| 228 | if (!EvaluateInteger(E->getIdx(), Index, Info)) |
| 229 | return APValue(); |
| 230 | |
| 231 | uint64_t ElementSize = Info.Ctx.getTypeSize(E->getType()) / 8; |
| 232 | |
| 233 | uint64_t Offset = Index.getSExtValue() * ElementSize; |
| 234 | Result.setLValue(Result.getLValueBase(), |
| 235 | Result.getLValueOffset() + Offset); |
| 236 | return Result; |
| 237 | } |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 238 | |
Eli Friedman | e8761c8 | 2009-02-20 01:57:15 +0000 | [diff] [blame] | 239 | APValue LValueExprEvaluator::VisitUnaryDeref(UnaryOperator *E) |
| 240 | { |
| 241 | APValue Result; |
| 242 | if (!EvaluatePointer(E->getSubExpr(), Result, Info)) |
| 243 | return APValue(); |
| 244 | return Result; |
| 245 | } |
| 246 | |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 247 | //===----------------------------------------------------------------------===// |
Chris Lattner | f5eeb05 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 248 | // Pointer Evaluation |
| 249 | //===----------------------------------------------------------------------===// |
| 250 | |
Anders Carlsson | c754aa6 | 2008-07-08 05:13:58 +0000 | [diff] [blame] | 251 | namespace { |
Anders Carlsson | 2bad168 | 2008-07-08 14:30:00 +0000 | [diff] [blame] | 252 | class VISIBILITY_HIDDEN PointerExprEvaluator |
| 253 | : public StmtVisitor<PointerExprEvaluator, APValue> { |
Chris Lattner | 87eae5e | 2008-07-11 22:52:41 +0000 | [diff] [blame] | 254 | EvalInfo &Info; |
Anders Carlsson | 2bad168 | 2008-07-08 14:30:00 +0000 | [diff] [blame] | 255 | public: |
Anders Carlsson | 2bad168 | 2008-07-08 14:30:00 +0000 | [diff] [blame] | 256 | |
Chris Lattner | 87eae5e | 2008-07-11 22:52:41 +0000 | [diff] [blame] | 257 | PointerExprEvaluator(EvalInfo &info) : Info(info) {} |
Chris Lattner | f5eeb05 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 258 | |
Anders Carlsson | 2bad168 | 2008-07-08 14:30:00 +0000 | [diff] [blame] | 259 | APValue VisitStmt(Stmt *S) { |
Anders Carlsson | 2bad168 | 2008-07-08 14:30:00 +0000 | [diff] [blame] | 260 | return APValue(); |
| 261 | } |
| 262 | |
| 263 | APValue VisitParenExpr(ParenExpr *E) { return Visit(E->getSubExpr()); } |
| 264 | |
Anders Carlsson | 650c92f | 2008-07-08 15:34:11 +0000 | [diff] [blame] | 265 | APValue VisitBinaryOperator(const BinaryOperator *E); |
| 266 | APValue VisitCastExpr(const CastExpr* E); |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 267 | APValue VisitUnaryOperator(const UnaryOperator *E); |
| 268 | APValue VisitObjCStringLiteral(ObjCStringLiteral *E) |
| 269 | { return APValue(E, 0); } |
Eli Friedman | f011589 | 2009-01-25 01:21:06 +0000 | [diff] [blame] | 270 | APValue VisitAddrLabelExpr(AddrLabelExpr *E) |
| 271 | { return APValue(E, 0); } |
Eli Friedman | 3941b18 | 2009-01-25 01:54:01 +0000 | [diff] [blame] | 272 | APValue VisitCallExpr(CallExpr *E); |
Mike Stump | b83d287 | 2009-02-19 22:01:56 +0000 | [diff] [blame] | 273 | APValue VisitBlockExpr(BlockExpr *E) { |
| 274 | if (!E->hasBlockDeclRefExprs()) |
| 275 | return APValue(E, 0); |
| 276 | return APValue(); |
| 277 | } |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 278 | APValue VisitConditionalOperator(ConditionalOperator *E); |
Anders Carlsson | 650c92f | 2008-07-08 15:34:11 +0000 | [diff] [blame] | 279 | }; |
Chris Lattner | f5eeb05 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 280 | } // end anonymous namespace |
Anders Carlsson | 650c92f | 2008-07-08 15:34:11 +0000 | [diff] [blame] | 281 | |
Chris Lattner | 87eae5e | 2008-07-11 22:52:41 +0000 | [diff] [blame] | 282 | static bool EvaluatePointer(const Expr* E, APValue& Result, EvalInfo &Info) { |
Mike Stump | ca2f3fd | 2009-02-18 21:44:49 +0000 | [diff] [blame] | 283 | if (!E->getType()->isPointerType() |
| 284 | && !E->getType()->isBlockPointerType()) |
Chris Lattner | f5eeb05 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 285 | return false; |
Chris Lattner | 87eae5e | 2008-07-11 22:52:41 +0000 | [diff] [blame] | 286 | Result = PointerExprEvaluator(Info).Visit(const_cast<Expr*>(E)); |
Chris Lattner | f5eeb05 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 287 | return Result.isLValue(); |
| 288 | } |
| 289 | |
| 290 | APValue PointerExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) { |
| 291 | if (E->getOpcode() != BinaryOperator::Add && |
| 292 | E->getOpcode() != BinaryOperator::Sub) |
| 293 | return APValue(); |
| 294 | |
| 295 | const Expr *PExp = E->getLHS(); |
| 296 | const Expr *IExp = E->getRHS(); |
| 297 | if (IExp->getType()->isPointerType()) |
| 298 | std::swap(PExp, IExp); |
| 299 | |
| 300 | APValue ResultLValue; |
Chris Lattner | 87eae5e | 2008-07-11 22:52:41 +0000 | [diff] [blame] | 301 | if (!EvaluatePointer(PExp, ResultLValue, Info)) |
Chris Lattner | f5eeb05 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 302 | return APValue(); |
| 303 | |
| 304 | llvm::APSInt AdditionalOffset(32); |
Chris Lattner | 87eae5e | 2008-07-11 22:52:41 +0000 | [diff] [blame] | 305 | if (!EvaluateInteger(IExp, AdditionalOffset, Info)) |
Chris Lattner | f5eeb05 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 306 | return APValue(); |
| 307 | |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 308 | QualType PointeeType = PExp->getType()->getAsPointerType()->getPointeeType(); |
Anders Carlsson | 4d4c50d | 2009-02-19 04:55:58 +0000 | [diff] [blame] | 309 | uint64_t SizeOfPointee; |
| 310 | |
| 311 | // Explicitly handle GNU void* and function pointer arithmetic extensions. |
| 312 | if (PointeeType->isVoidType() || PointeeType->isFunctionType()) |
| 313 | SizeOfPointee = 1; |
| 314 | else |
| 315 | SizeOfPointee = Info.Ctx.getTypeSize(PointeeType) / 8; |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 316 | |
Chris Lattner | f5eeb05 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 317 | uint64_t Offset = ResultLValue.getLValueOffset(); |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 318 | |
Chris Lattner | f5eeb05 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 319 | if (E->getOpcode() == BinaryOperator::Add) |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 320 | Offset += AdditionalOffset.getLimitedValue() * SizeOfPointee; |
Chris Lattner | f5eeb05 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 321 | else |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 322 | Offset -= AdditionalOffset.getLimitedValue() * SizeOfPointee; |
| 323 | |
Chris Lattner | f5eeb05 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 324 | return APValue(ResultLValue.getLValueBase(), Offset); |
| 325 | } |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 326 | |
| 327 | APValue PointerExprEvaluator::VisitUnaryOperator(const UnaryOperator *E) { |
| 328 | if (E->getOpcode() == UnaryOperator::Extension) { |
| 329 | // FIXME: Deal with warnings? |
| 330 | return Visit(E->getSubExpr()); |
| 331 | } |
| 332 | |
| 333 | if (E->getOpcode() == UnaryOperator::AddrOf) { |
| 334 | APValue result; |
| 335 | if (EvaluateLValue(E->getSubExpr(), result, Info)) |
| 336 | return result; |
| 337 | } |
| 338 | |
| 339 | return APValue(); |
| 340 | } |
Anders Carlsson | d407a76 | 2008-12-05 05:24:13 +0000 | [diff] [blame] | 341 | |
Chris Lattner | f5eeb05 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 342 | |
Chris Lattner | b542afe | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 343 | APValue PointerExprEvaluator::VisitCastExpr(const CastExpr* E) { |
Chris Lattner | f5eeb05 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 344 | const Expr* SubExpr = E->getSubExpr(); |
| 345 | |
| 346 | // Check for pointer->pointer cast |
| 347 | if (SubExpr->getType()->isPointerType()) { |
| 348 | APValue Result; |
Chris Lattner | 87eae5e | 2008-07-11 22:52:41 +0000 | [diff] [blame] | 349 | if (EvaluatePointer(SubExpr, Result, Info)) |
Chris Lattner | f5eeb05 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 350 | return Result; |
| 351 | return APValue(); |
| 352 | } |
| 353 | |
Eli Friedman | d9f4bcd | 2008-07-27 05:46:18 +0000 | [diff] [blame] | 354 | if (SubExpr->getType()->isIntegralType()) { |
Daniel Dunbar | 69ab26a | 2009-02-20 18:22:23 +0000 | [diff] [blame] | 355 | APValue Result; |
| 356 | if (!EvaluateIntegerOrLValue(SubExpr, Result, Info)) |
| 357 | return APValue(); |
| 358 | |
| 359 | if (Result.isInt()) { |
| 360 | Result.getInt().extOrTrunc((unsigned)Info.Ctx.getTypeSize(E->getType())); |
| 361 | return APValue(0, Result.getInt().getZExtValue()); |
Chris Lattner | f5eeb05 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 362 | } |
Daniel Dunbar | 69ab26a | 2009-02-20 18:22:23 +0000 | [diff] [blame] | 363 | |
| 364 | // Cast is of an lvalue, no need to change value. |
| 365 | return Result; |
Chris Lattner | f5eeb05 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 366 | } |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 367 | |
| 368 | if (SubExpr->getType()->isFunctionType() || |
| 369 | SubExpr->getType()->isArrayType()) { |
| 370 | APValue Result; |
| 371 | if (EvaluateLValue(SubExpr, Result, Info)) |
| 372 | return Result; |
| 373 | return APValue(); |
| 374 | } |
| 375 | |
| 376 | //assert(0 && "Unhandled cast"); |
Chris Lattner | f5eeb05 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 377 | return APValue(); |
| 378 | } |
| 379 | |
Eli Friedman | 3941b18 | 2009-01-25 01:54:01 +0000 | [diff] [blame] | 380 | APValue PointerExprEvaluator::VisitCallExpr(CallExpr *E) { |
Douglas Gregor | 3c385e5 | 2009-02-14 18:57:46 +0000 | [diff] [blame] | 381 | if (E->isBuiltinCall(Info.Ctx) == |
| 382 | Builtin::BI__builtin___CFStringMakeConstantString) |
Eli Friedman | 3941b18 | 2009-01-25 01:54:01 +0000 | [diff] [blame] | 383 | return APValue(E, 0); |
| 384 | return APValue(); |
| 385 | } |
| 386 | |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 387 | APValue PointerExprEvaluator::VisitConditionalOperator(ConditionalOperator *E) { |
| 388 | bool BoolResult; |
| 389 | if (!HandleConversionToBool(E->getCond(), BoolResult, Info)) |
| 390 | return APValue(); |
| 391 | |
| 392 | Expr* EvalExpr = BoolResult ? E->getTrueExpr() : E->getFalseExpr(); |
| 393 | |
| 394 | APValue Result; |
| 395 | if (EvaluatePointer(EvalExpr, Result, Info)) |
| 396 | return Result; |
| 397 | return APValue(); |
| 398 | } |
Chris Lattner | f5eeb05 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 399 | |
| 400 | //===----------------------------------------------------------------------===// |
Nate Begeman | 59b5da6 | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 401 | // Vector Evaluation |
| 402 | //===----------------------------------------------------------------------===// |
| 403 | |
| 404 | namespace { |
| 405 | class VISIBILITY_HIDDEN VectorExprEvaluator |
| 406 | : public StmtVisitor<VectorExprEvaluator, APValue> { |
| 407 | EvalInfo &Info; |
| 408 | public: |
| 409 | |
| 410 | VectorExprEvaluator(EvalInfo &info) : Info(info) {} |
| 411 | |
| 412 | APValue VisitStmt(Stmt *S) { |
| 413 | return APValue(); |
| 414 | } |
| 415 | |
| 416 | APValue VisitParenExpr(ParenExpr *E) { return Visit(E->getSubExpr()); } |
| 417 | APValue VisitCastExpr(const CastExpr* E); |
| 418 | APValue VisitCompoundLiteralExpr(const CompoundLiteralExpr *E); |
| 419 | APValue VisitInitListExpr(const InitListExpr *E); |
| 420 | }; |
| 421 | } // end anonymous namespace |
| 422 | |
| 423 | static bool EvaluateVector(const Expr* E, APValue& Result, EvalInfo &Info) { |
| 424 | if (!E->getType()->isVectorType()) |
| 425 | return false; |
| 426 | Result = VectorExprEvaluator(Info).Visit(const_cast<Expr*>(E)); |
| 427 | return !Result.isUninit(); |
| 428 | } |
| 429 | |
| 430 | APValue VectorExprEvaluator::VisitCastExpr(const CastExpr* E) { |
| 431 | const Expr* SE = E->getSubExpr(); |
| 432 | |
| 433 | // Check for vector->vector bitcast. |
| 434 | if (SE->getType()->isVectorType()) |
| 435 | return this->Visit(const_cast<Expr*>(SE)); |
| 436 | |
| 437 | return APValue(); |
| 438 | } |
| 439 | |
| 440 | APValue |
| 441 | VectorExprEvaluator::VisitCompoundLiteralExpr(const CompoundLiteralExpr *E) { |
| 442 | return this->Visit(const_cast<Expr*>(E->getInitializer())); |
| 443 | } |
| 444 | |
| 445 | APValue |
| 446 | VectorExprEvaluator::VisitInitListExpr(const InitListExpr *E) { |
| 447 | const VectorType *VT = E->getType()->getAsVectorType(); |
| 448 | unsigned NumInits = E->getNumInits(); |
| 449 | |
| 450 | if (!VT || VT->getNumElements() != NumInits) |
| 451 | return APValue(); |
| 452 | |
| 453 | QualType EltTy = VT->getElementType(); |
| 454 | llvm::SmallVector<APValue, 4> Elements; |
| 455 | |
| 456 | for (unsigned i = 0; i < NumInits; i++) { |
| 457 | if (EltTy->isIntegerType()) { |
| 458 | llvm::APSInt sInt(32); |
| 459 | if (!EvaluateInteger(E->getInit(i), sInt, Info)) |
| 460 | return APValue(); |
| 461 | Elements.push_back(APValue(sInt)); |
| 462 | } else { |
| 463 | llvm::APFloat f(0.0); |
| 464 | if (!EvaluateFloat(E->getInit(i), f, Info)) |
| 465 | return APValue(); |
| 466 | Elements.push_back(APValue(f)); |
| 467 | } |
| 468 | } |
| 469 | return APValue(&Elements[0], Elements.size()); |
| 470 | } |
| 471 | |
| 472 | //===----------------------------------------------------------------------===// |
Chris Lattner | f5eeb05 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 473 | // Integer Evaluation |
| 474 | //===----------------------------------------------------------------------===// |
Chris Lattner | f5eeb05 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 475 | |
| 476 | namespace { |
Anders Carlsson | c754aa6 | 2008-07-08 05:13:58 +0000 | [diff] [blame] | 477 | class VISIBILITY_HIDDEN IntExprEvaluator |
Chris Lattner | b542afe | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 478 | : public StmtVisitor<IntExprEvaluator, bool> { |
Chris Lattner | 87eae5e | 2008-07-11 22:52:41 +0000 | [diff] [blame] | 479 | EvalInfo &Info; |
Daniel Dunbar | 30c37f4 | 2009-02-19 20:17:33 +0000 | [diff] [blame] | 480 | APValue &Result; |
Anders Carlsson | c754aa6 | 2008-07-08 05:13:58 +0000 | [diff] [blame] | 481 | public: |
Daniel Dunbar | 30c37f4 | 2009-02-19 20:17:33 +0000 | [diff] [blame] | 482 | IntExprEvaluator(EvalInfo &info, APValue &result) |
Chris Lattner | 87eae5e | 2008-07-11 22:52:41 +0000 | [diff] [blame] | 483 | : Info(info), Result(result) {} |
Chris Lattner | f5eeb05 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 484 | |
Anders Carlsson | 82206e2 | 2008-11-30 18:14:57 +0000 | [diff] [blame] | 485 | bool Extension(SourceLocation L, diag::kind D, const Expr *E) { |
Anders Carlsson | 54da049 | 2008-11-30 16:38:33 +0000 | [diff] [blame] | 486 | Info.EvalResult.DiagLoc = L; |
| 487 | Info.EvalResult.Diag = D; |
Anders Carlsson | 82206e2 | 2008-11-30 18:14:57 +0000 | [diff] [blame] | 488 | Info.EvalResult.DiagExpr = E; |
Chris Lattner | 54176fd | 2008-07-12 00:14:42 +0000 | [diff] [blame] | 489 | return true; // still a constant. |
| 490 | } |
Daniel Dunbar | 131eb43 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 491 | |
Daniel Dunbar | 3f7d995 | 2009-02-19 18:37:50 +0000 | [diff] [blame] | 492 | bool Success(const llvm::APSInt &SI, const Expr *E) { |
Daniel Dunbar | 30c37f4 | 2009-02-19 20:17:33 +0000 | [diff] [blame] | 493 | assert(SI.isSigned() == E->getType()->isSignedIntegerType() && |
Daniel Dunbar | 3f7d995 | 2009-02-19 18:37:50 +0000 | [diff] [blame] | 494 | "Invalid evaluation result."); |
Daniel Dunbar | 30c37f4 | 2009-02-19 20:17:33 +0000 | [diff] [blame] | 495 | assert(SI.getBitWidth() == Info.Ctx.getIntWidth(E->getType()) && |
Daniel Dunbar | 3f7d995 | 2009-02-19 18:37:50 +0000 | [diff] [blame] | 496 | "Invalid evaluation result."); |
Daniel Dunbar | 30c37f4 | 2009-02-19 20:17:33 +0000 | [diff] [blame] | 497 | Result = APValue(SI); |
Daniel Dunbar | 3f7d995 | 2009-02-19 18:37:50 +0000 | [diff] [blame] | 498 | return true; |
| 499 | } |
| 500 | |
Daniel Dunbar | 131eb43 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 501 | bool Success(const llvm::APInt &I, const Expr *E) { |
Daniel Dunbar | 30c37f4 | 2009-02-19 20:17:33 +0000 | [diff] [blame] | 502 | assert(I.getBitWidth() == Info.Ctx.getIntWidth(E->getType()) && |
Daniel Dunbar | 3f7d995 | 2009-02-19 18:37:50 +0000 | [diff] [blame] | 503 | "Invalid evaluation result."); |
Daniel Dunbar | 30c37f4 | 2009-02-19 20:17:33 +0000 | [diff] [blame] | 504 | Result = APValue(APSInt(I)); |
| 505 | Result.getInt().setIsUnsigned(E->getType()->isUnsignedIntegerType()); |
Daniel Dunbar | 131eb43 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 506 | return true; |
| 507 | } |
| 508 | |
| 509 | bool Success(uint64_t Value, const Expr *E) { |
Daniel Dunbar | 30c37f4 | 2009-02-19 20:17:33 +0000 | [diff] [blame] | 510 | Result = APValue(Info.Ctx.MakeIntValue(Value, E->getType())); |
Daniel Dunbar | 131eb43 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 511 | return true; |
| 512 | } |
| 513 | |
Anders Carlsson | 82206e2 | 2008-11-30 18:14:57 +0000 | [diff] [blame] | 514 | bool Error(SourceLocation L, diag::kind D, const Expr *E) { |
Chris Lattner | 54176fd | 2008-07-12 00:14:42 +0000 | [diff] [blame] | 515 | // If this is in an unevaluated portion of the subexpression, ignore the |
| 516 | // error. |
Anders Carlsson | f0c1e4b | 2008-11-30 18:26:25 +0000 | [diff] [blame] | 517 | if (Info.ShortCircuit) { |
Chris Lattner | 32fea9d | 2008-11-12 07:43:42 +0000 | [diff] [blame] | 518 | // If error is ignored because the value isn't evaluated, get the real |
| 519 | // type at least to prevent errors downstream. |
Daniel Dunbar | 131eb43 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 520 | return Success(0, E); |
Chris Lattner | 32fea9d | 2008-11-12 07:43:42 +0000 | [diff] [blame] | 521 | } |
Chris Lattner | 54176fd | 2008-07-12 00:14:42 +0000 | [diff] [blame] | 522 | |
Chris Lattner | 32fea9d | 2008-11-12 07:43:42 +0000 | [diff] [blame] | 523 | // Take the first error. |
Daniel Dunbar | 30c37f4 | 2009-02-19 20:17:33 +0000 | [diff] [blame] | 524 | |
| 525 | // FIXME: This is wrong if we happen to have already emitted an |
| 526 | // extension diagnostic; in that case we should report this error. |
Anders Carlsson | 54da049 | 2008-11-30 16:38:33 +0000 | [diff] [blame] | 527 | if (Info.EvalResult.Diag == 0) { |
| 528 | Info.EvalResult.DiagLoc = L; |
| 529 | Info.EvalResult.Diag = D; |
Anders Carlsson | 82206e2 | 2008-11-30 18:14:57 +0000 | [diff] [blame] | 530 | Info.EvalResult.DiagExpr = E; |
Chris Lattner | 32fea9d | 2008-11-12 07:43:42 +0000 | [diff] [blame] | 531 | } |
Chris Lattner | 54176fd | 2008-07-12 00:14:42 +0000 | [diff] [blame] | 532 | return false; |
Chris Lattner | 7a76778 | 2008-07-11 19:24:49 +0000 | [diff] [blame] | 533 | } |
| 534 | |
Anders Carlsson | c754aa6 | 2008-07-08 05:13:58 +0000 | [diff] [blame] | 535 | //===--------------------------------------------------------------------===// |
| 536 | // Visitor Methods |
| 537 | //===--------------------------------------------------------------------===// |
Chris Lattner | 32fea9d | 2008-11-12 07:43:42 +0000 | [diff] [blame] | 538 | |
| 539 | bool VisitStmt(Stmt *) { |
| 540 | assert(0 && "This should be called on integers, stmts are not integers"); |
| 541 | return false; |
| 542 | } |
Chris Lattner | 7a76778 | 2008-07-11 19:24:49 +0000 | [diff] [blame] | 543 | |
Chris Lattner | 32fea9d | 2008-11-12 07:43:42 +0000 | [diff] [blame] | 544 | bool VisitExpr(Expr *E) { |
Anders Carlsson | 0e8acbb | 2008-11-30 18:37:00 +0000 | [diff] [blame] | 545 | return Error(E->getLocStart(), diag::note_invalid_subexpr_in_ice, E); |
Anders Carlsson | c754aa6 | 2008-07-08 05:13:58 +0000 | [diff] [blame] | 546 | } |
| 547 | |
Chris Lattner | b542afe | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 548 | bool VisitParenExpr(ParenExpr *E) { return Visit(E->getSubExpr()); } |
Anders Carlsson | c754aa6 | 2008-07-08 05:13:58 +0000 | [diff] [blame] | 549 | |
Chris Lattner | 4c4867e | 2008-07-12 00:38:25 +0000 | [diff] [blame] | 550 | bool VisitIntegerLiteral(const IntegerLiteral *E) { |
Daniel Dunbar | 131eb43 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 551 | return Success(E->getValue(), E); |
Chris Lattner | 4c4867e | 2008-07-12 00:38:25 +0000 | [diff] [blame] | 552 | } |
| 553 | bool VisitCharacterLiteral(const CharacterLiteral *E) { |
Daniel Dunbar | 131eb43 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 554 | return Success(E->getValue(), E); |
Chris Lattner | 4c4867e | 2008-07-12 00:38:25 +0000 | [diff] [blame] | 555 | } |
| 556 | bool VisitTypesCompatibleExpr(const TypesCompatibleExpr *E) { |
Daniel Dunbar | ac620de | 2008-10-24 08:07:57 +0000 | [diff] [blame] | 557 | // Per gcc docs "this built-in function ignores top level |
| 558 | // qualifiers". We need to use the canonical version to properly |
| 559 | // be able to strip CRV qualifiers from the type. |
| 560 | QualType T0 = Info.Ctx.getCanonicalType(E->getArgType1()); |
| 561 | QualType T1 = Info.Ctx.getCanonicalType(E->getArgType2()); |
Daniel Dunbar | 131eb43 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 562 | return Success(Info.Ctx.typesAreCompatible(T0.getUnqualifiedType(), |
| 563 | T1.getUnqualifiedType()), |
| 564 | E); |
Chris Lattner | 4c4867e | 2008-07-12 00:38:25 +0000 | [diff] [blame] | 565 | } |
| 566 | bool VisitDeclRefExpr(const DeclRefExpr *E); |
| 567 | bool VisitCallExpr(const CallExpr *E); |
Chris Lattner | b542afe | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 568 | bool VisitBinaryOperator(const BinaryOperator *E); |
| 569 | bool VisitUnaryOperator(const UnaryOperator *E); |
Nuno Lopes | ca7c2ea | 2008-11-16 19:28:31 +0000 | [diff] [blame] | 570 | bool VisitConditionalOperator(const ConditionalOperator *E); |
Anders Carlsson | 06a3675 | 2008-07-08 05:49:43 +0000 | [diff] [blame] | 571 | |
Daniel Dunbar | a2cfd34 | 2009-01-29 06:16:07 +0000 | [diff] [blame] | 572 | bool VisitCastExpr(CastExpr* E); |
Sebastian Redl | 0518999 | 2008-11-11 17:56:53 +0000 | [diff] [blame] | 573 | bool VisitSizeOfAlignOfExpr(const SizeOfAlignOfExpr *E); |
| 574 | |
Anders Carlsson | 3068d11 | 2008-11-16 19:01:22 +0000 | [diff] [blame] | 575 | bool VisitCXXBoolLiteralExpr(const CXXBoolLiteralExpr *E) { |
Daniel Dunbar | 131eb43 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 576 | return Success(E->getValue(), E); |
Anders Carlsson | 3068d11 | 2008-11-16 19:01:22 +0000 | [diff] [blame] | 577 | } |
| 578 | |
Anders Carlsson | 3f70456 | 2008-12-21 22:39:40 +0000 | [diff] [blame] | 579 | bool VisitGNUNullExpr(const GNUNullExpr *E) { |
Daniel Dunbar | 131eb43 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 580 | return Success(0, E); |
Anders Carlsson | 3f70456 | 2008-12-21 22:39:40 +0000 | [diff] [blame] | 581 | } |
| 582 | |
Anders Carlsson | 3068d11 | 2008-11-16 19:01:22 +0000 | [diff] [blame] | 583 | bool VisitCXXZeroInitValueExpr(const CXXZeroInitValueExpr *E) { |
Daniel Dunbar | 131eb43 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 584 | return Success(0, E); |
Anders Carlsson | 3068d11 | 2008-11-16 19:01:22 +0000 | [diff] [blame] | 585 | } |
| 586 | |
Sebastian Redl | 64b45f7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 587 | bool VisitUnaryTypeTraitExpr(const UnaryTypeTraitExpr *E) { |
Daniel Dunbar | 131eb43 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 588 | return Success(E->EvaluateTrait(), E); |
Sebastian Redl | 64b45f7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 589 | } |
| 590 | |
Chris Lattner | fcee001 | 2008-07-11 21:24:13 +0000 | [diff] [blame] | 591 | private: |
Chris Lattner | af707ab | 2009-01-24 21:53:27 +0000 | [diff] [blame] | 592 | unsigned GetAlignOfExpr(const Expr *E); |
| 593 | unsigned GetAlignOfType(QualType T); |
Anders Carlsson | a25ae3d | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 594 | }; |
Chris Lattner | f5eeb05 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 595 | } // end anonymous namespace |
Anders Carlsson | 650c92f | 2008-07-08 15:34:11 +0000 | [diff] [blame] | 596 | |
Daniel Dunbar | 69ab26a | 2009-02-20 18:22:23 +0000 | [diff] [blame] | 597 | static bool EvaluateIntegerOrLValue(const Expr* E, APValue &Result, EvalInfo &Info) { |
Daniel Dunbar | 3f7d995 | 2009-02-19 18:37:50 +0000 | [diff] [blame] | 598 | if (!E->getType()->isIntegralType()) |
| 599 | return false; |
Daniel Dunbar | 30c37f4 | 2009-02-19 20:17:33 +0000 | [diff] [blame] | 600 | |
Daniel Dunbar | 69ab26a | 2009-02-20 18:22:23 +0000 | [diff] [blame] | 601 | return IntExprEvaluator(Info, Result).Visit(const_cast<Expr*>(E)); |
| 602 | } |
Daniel Dunbar | 30c37f4 | 2009-02-19 20:17:33 +0000 | [diff] [blame] | 603 | |
Daniel Dunbar | 69ab26a | 2009-02-20 18:22:23 +0000 | [diff] [blame] | 604 | static bool EvaluateInteger(const Expr* E, APSInt &Result, EvalInfo &Info) { |
| 605 | APValue Val; |
| 606 | if (!EvaluateIntegerOrLValue(E, Val, Info) || !Val.isInt()) |
| 607 | return false; |
Daniel Dunbar | 30c37f4 | 2009-02-19 20:17:33 +0000 | [diff] [blame] | 608 | Result = Val.getInt(); |
| 609 | return true; |
Anders Carlsson | 650c92f | 2008-07-08 15:34:11 +0000 | [diff] [blame] | 610 | } |
Anders Carlsson | 650c92f | 2008-07-08 15:34:11 +0000 | [diff] [blame] | 611 | |
Chris Lattner | 4c4867e | 2008-07-12 00:38:25 +0000 | [diff] [blame] | 612 | bool IntExprEvaluator::VisitDeclRefExpr(const DeclRefExpr *E) { |
| 613 | // Enums are integer constant exprs. |
| 614 | if (const EnumConstantDecl *D = dyn_cast<EnumConstantDecl>(E->getDecl())) { |
Eli Friedman | e9a0f43 | 2008-12-08 02:21:03 +0000 | [diff] [blame] | 615 | // 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] | 616 | // signedness consistently; see PR3173. |
| 617 | APSInt SI = D->getInitVal(); |
| 618 | SI.setIsUnsigned(!E->getType()->isSignedIntegerType()); |
| 619 | // FIXME: This is an ugly hack around the fact that enums don't |
| 620 | // set their width (!?!) consistently; see PR3173. |
| 621 | SI.extOrTrunc(Info.Ctx.getIntWidth(E->getType())); |
| 622 | return Success(SI, E); |
Chris Lattner | 4c4867e | 2008-07-12 00:38:25 +0000 | [diff] [blame] | 623 | } |
Sebastian Redl | b2bc62b | 2009-02-08 15:51:17 +0000 | [diff] [blame] | 624 | |
| 625 | // In C++, const, non-volatile integers initialized with ICEs are ICEs. |
| 626 | if (Info.Ctx.getLangOptions().CPlusPlus && |
| 627 | E->getType().getCVRQualifiers() == QualType::Const) { |
| 628 | if (const VarDecl *D = dyn_cast<VarDecl>(E->getDecl())) { |
| 629 | if (const Expr *Init = D->getInit()) |
| 630 | return Visit(const_cast<Expr*>(Init)); |
| 631 | } |
| 632 | } |
| 633 | |
Chris Lattner | 4c4867e | 2008-07-12 00:38:25 +0000 | [diff] [blame] | 634 | // Otherwise, random variable references are not constants. |
Anders Carlsson | 0e8acbb | 2008-11-30 18:37:00 +0000 | [diff] [blame] | 635 | return Error(E->getLocStart(), diag::note_invalid_subexpr_in_ice, E); |
Chris Lattner | 4c4867e | 2008-07-12 00:38:25 +0000 | [diff] [blame] | 636 | } |
| 637 | |
Chris Lattner | a4d55d8 | 2008-10-06 06:40:35 +0000 | [diff] [blame] | 638 | /// EvaluateBuiltinClassifyType - Evaluate __builtin_classify_type the same way |
| 639 | /// as GCC. |
| 640 | static int EvaluateBuiltinClassifyType(const CallExpr *E) { |
| 641 | // The following enum mimics the values returned by GCC. |
| 642 | enum gcc_type_class { |
| 643 | no_type_class = -1, |
| 644 | void_type_class, integer_type_class, char_type_class, |
| 645 | enumeral_type_class, boolean_type_class, |
| 646 | pointer_type_class, reference_type_class, offset_type_class, |
| 647 | real_type_class, complex_type_class, |
| 648 | function_type_class, method_type_class, |
| 649 | record_type_class, union_type_class, |
| 650 | array_type_class, string_type_class, |
| 651 | lang_type_class |
| 652 | }; |
| 653 | |
| 654 | // If no argument was supplied, default to "no_type_class". This isn't |
| 655 | // ideal, however it is what gcc does. |
| 656 | if (E->getNumArgs() == 0) |
| 657 | return no_type_class; |
| 658 | |
| 659 | QualType ArgTy = E->getArg(0)->getType(); |
| 660 | if (ArgTy->isVoidType()) |
| 661 | return void_type_class; |
| 662 | else if (ArgTy->isEnumeralType()) |
| 663 | return enumeral_type_class; |
| 664 | else if (ArgTy->isBooleanType()) |
| 665 | return boolean_type_class; |
| 666 | else if (ArgTy->isCharType()) |
| 667 | return string_type_class; // gcc doesn't appear to use char_type_class |
| 668 | else if (ArgTy->isIntegerType()) |
| 669 | return integer_type_class; |
| 670 | else if (ArgTy->isPointerType()) |
| 671 | return pointer_type_class; |
| 672 | else if (ArgTy->isReferenceType()) |
| 673 | return reference_type_class; |
| 674 | else if (ArgTy->isRealType()) |
| 675 | return real_type_class; |
| 676 | else if (ArgTy->isComplexType()) |
| 677 | return complex_type_class; |
| 678 | else if (ArgTy->isFunctionType()) |
| 679 | return function_type_class; |
| 680 | else if (ArgTy->isStructureType()) |
| 681 | return record_type_class; |
| 682 | else if (ArgTy->isUnionType()) |
| 683 | return union_type_class; |
| 684 | else if (ArgTy->isArrayType()) |
| 685 | return array_type_class; |
| 686 | else if (ArgTy->isUnionType()) |
| 687 | return union_type_class; |
| 688 | else // FIXME: offset_type_class, method_type_class, & lang_type_class? |
| 689 | assert(0 && "CallExpr::isBuiltinClassifyType(): unimplemented type"); |
| 690 | return -1; |
| 691 | } |
| 692 | |
Chris Lattner | 4c4867e | 2008-07-12 00:38:25 +0000 | [diff] [blame] | 693 | bool IntExprEvaluator::VisitCallExpr(const CallExpr *E) { |
Douglas Gregor | 3c385e5 | 2009-02-14 18:57:46 +0000 | [diff] [blame] | 694 | switch (E->isBuiltinCall(Info.Ctx)) { |
Chris Lattner | 019f4e8 | 2008-10-06 05:28:25 +0000 | [diff] [blame] | 695 | default: |
Anders Carlsson | 0e8acbb | 2008-11-30 18:37:00 +0000 | [diff] [blame] | 696 | return Error(E->getLocStart(), diag::note_invalid_subexpr_in_ice, E); |
Chris Lattner | 019f4e8 | 2008-10-06 05:28:25 +0000 | [diff] [blame] | 697 | case Builtin::BI__builtin_classify_type: |
Daniel Dunbar | 131eb43 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 698 | return Success(EvaluateBuiltinClassifyType(E), E); |
Chris Lattner | 019f4e8 | 2008-10-06 05:28:25 +0000 | [diff] [blame] | 699 | |
Anders Carlsson | 4bbc0e0 | 2008-11-24 04:21:33 +0000 | [diff] [blame] | 700 | case Builtin::BI__builtin_constant_p: |
Chris Lattner | 019f4e8 | 2008-10-06 05:28:25 +0000 | [diff] [blame] | 701 | // __builtin_constant_p always has one operand: it returns true if that |
| 702 | // operand can be folded, false otherwise. |
Daniel Dunbar | 131eb43 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 703 | return Success(E->getArg(0)->isEvaluatable(Info.Ctx), E); |
Chris Lattner | 019f4e8 | 2008-10-06 05:28:25 +0000 | [diff] [blame] | 704 | } |
Chris Lattner | 4c4867e | 2008-07-12 00:38:25 +0000 | [diff] [blame] | 705 | } |
Anders Carlsson | 650c92f | 2008-07-08 15:34:11 +0000 | [diff] [blame] | 706 | |
Chris Lattner | b542afe | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 707 | bool IntExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) { |
Eli Friedman | a6afa76 | 2008-11-13 06:09:17 +0000 | [diff] [blame] | 708 | if (E->getOpcode() == BinaryOperator::Comma) { |
Anders Carlsson | 027f62e | 2008-12-01 02:07:06 +0000 | [diff] [blame] | 709 | if (!Visit(E->getRHS())) |
| 710 | return false; |
Anders Carlsson | 4fdfb09 | 2008-12-01 06:44:05 +0000 | [diff] [blame] | 711 | |
| 712 | if (!Info.ShortCircuit) { |
| 713 | // If we can't evaluate the LHS, it must be because it has |
| 714 | // side effects. |
| 715 | if (!E->getLHS()->isEvaluatable(Info.Ctx)) |
| 716 | Info.EvalResult.HasSideEffects = true; |
| 717 | |
Anders Carlsson | 0e8acbb | 2008-11-30 18:37:00 +0000 | [diff] [blame] | 718 | return Extension(E->getOperatorLoc(), diag::note_comma_in_ice, E); |
Anders Carlsson | 4fdfb09 | 2008-12-01 06:44:05 +0000 | [diff] [blame] | 719 | } |
Eli Friedman | a6afa76 | 2008-11-13 06:09:17 +0000 | [diff] [blame] | 720 | |
Anders Carlsson | 027f62e | 2008-12-01 02:07:06 +0000 | [diff] [blame] | 721 | return true; |
Eli Friedman | a6afa76 | 2008-11-13 06:09:17 +0000 | [diff] [blame] | 722 | } |
| 723 | |
| 724 | if (E->isLogicalOp()) { |
| 725 | // These need to be handled specially because the operands aren't |
| 726 | // necessarily integral |
Anders Carlsson | fcb4d09 | 2008-11-30 16:51:17 +0000 | [diff] [blame] | 727 | bool lhsResult, rhsResult; |
Anders Carlsson | 51fe996 | 2008-11-22 21:04:56 +0000 | [diff] [blame] | 728 | |
Anders Carlsson | fcb4d09 | 2008-11-30 16:51:17 +0000 | [diff] [blame] | 729 | if (HandleConversionToBool(E->getLHS(), lhsResult, Info)) { |
Anders Carlsson | 51fe996 | 2008-11-22 21:04:56 +0000 | [diff] [blame] | 730 | // We were able to evaluate the LHS, see if we can get away with not |
| 731 | // evaluating the RHS: 0 && X -> 0, 1 || X -> 1 |
Anders Carlsson | fcb4d09 | 2008-11-30 16:51:17 +0000 | [diff] [blame] | 732 | if (lhsResult == (E->getOpcode() == BinaryOperator::LOr) || |
| 733 | !lhsResult == (E->getOpcode() == BinaryOperator::LAnd)) { |
Anders Carlsson | f0c1e4b | 2008-11-30 18:26:25 +0000 | [diff] [blame] | 734 | Info.ShortCircuit++; |
Anders Carlsson | fcb4d09 | 2008-11-30 16:51:17 +0000 | [diff] [blame] | 735 | bool rhsEvaluated = HandleConversionToBool(E->getRHS(), rhsResult, Info); |
Anders Carlsson | f0c1e4b | 2008-11-30 18:26:25 +0000 | [diff] [blame] | 736 | Info.ShortCircuit--; |
| 737 | |
Anders Carlsson | fcb4d09 | 2008-11-30 16:51:17 +0000 | [diff] [blame] | 738 | // FIXME: Return an extension warning saying that the RHS could not be |
| 739 | // evaluated. |
Daniel Dunbar | 3f7d995 | 2009-02-19 18:37:50 +0000 | [diff] [blame] | 740 | // if (!rhsEvaluated) ... |
| 741 | (void) rhsEvaluated; |
| 742 | |
| 743 | return Success(lhsResult, E); |
Eli Friedman | a6afa76 | 2008-11-13 06:09:17 +0000 | [diff] [blame] | 744 | } |
Anders Carlsson | 4bbc0e0 | 2008-11-24 04:21:33 +0000 | [diff] [blame] | 745 | |
Anders Carlsson | fcb4d09 | 2008-11-30 16:51:17 +0000 | [diff] [blame] | 746 | if (HandleConversionToBool(E->getRHS(), rhsResult, Info)) { |
Anders Carlsson | 4bbc0e0 | 2008-11-24 04:21:33 +0000 | [diff] [blame] | 747 | if (E->getOpcode() == BinaryOperator::LOr) |
Daniel Dunbar | 131eb43 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 748 | return Success(lhsResult || rhsResult, E); |
Anders Carlsson | 4bbc0e0 | 2008-11-24 04:21:33 +0000 | [diff] [blame] | 749 | else |
Daniel Dunbar | 131eb43 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 750 | return Success(lhsResult && rhsResult, E); |
Anders Carlsson | 4bbc0e0 | 2008-11-24 04:21:33 +0000 | [diff] [blame] | 751 | } |
| 752 | } else { |
Anders Carlsson | fcb4d09 | 2008-11-30 16:51:17 +0000 | [diff] [blame] | 753 | if (HandleConversionToBool(E->getRHS(), rhsResult, Info)) { |
Anders Carlsson | 4bbc0e0 | 2008-11-24 04:21:33 +0000 | [diff] [blame] | 754 | // We can't evaluate the LHS; however, sometimes the result |
| 755 | // is determined by the RHS: X && 0 -> 0, X || 1 -> 1. |
Anders Carlsson | fcb4d09 | 2008-11-30 16:51:17 +0000 | [diff] [blame] | 756 | if (rhsResult == (E->getOpcode() == BinaryOperator::LOr) || |
| 757 | !rhsResult == (E->getOpcode() == BinaryOperator::LAnd)) { |
Daniel Dunbar | 131eb43 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 758 | // Since we weren't able to evaluate the left hand side, it |
Anders Carlsson | fcb4d09 | 2008-11-30 16:51:17 +0000 | [diff] [blame] | 759 | // must have had side effects. |
| 760 | Info.EvalResult.HasSideEffects = true; |
Daniel Dunbar | 131eb43 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 761 | |
| 762 | return Success(rhsResult, E); |
Anders Carlsson | 4bbc0e0 | 2008-11-24 04:21:33 +0000 | [diff] [blame] | 763 | } |
| 764 | } |
Anders Carlsson | 51fe996 | 2008-11-22 21:04:56 +0000 | [diff] [blame] | 765 | } |
Eli Friedman | a6afa76 | 2008-11-13 06:09:17 +0000 | [diff] [blame] | 766 | |
Eli Friedman | a6afa76 | 2008-11-13 06:09:17 +0000 | [diff] [blame] | 767 | return false; |
| 768 | } |
| 769 | |
Anders Carlsson | 286f85e | 2008-11-16 07:17:21 +0000 | [diff] [blame] | 770 | QualType LHSTy = E->getLHS()->getType(); |
| 771 | QualType RHSTy = E->getRHS()->getType(); |
Daniel Dunbar | 4087e24 | 2009-01-29 06:43:41 +0000 | [diff] [blame] | 772 | |
| 773 | if (LHSTy->isAnyComplexType()) { |
| 774 | assert(RHSTy->isAnyComplexType() && "Invalid comparison"); |
| 775 | APValue LHS, RHS; |
| 776 | |
| 777 | if (!EvaluateComplex(E->getLHS(), LHS, Info)) |
| 778 | return false; |
| 779 | |
| 780 | if (!EvaluateComplex(E->getRHS(), RHS, Info)) |
| 781 | return false; |
| 782 | |
| 783 | if (LHS.isComplexFloat()) { |
| 784 | APFloat::cmpResult CR_r = |
| 785 | LHS.getComplexFloatReal().compare(RHS.getComplexFloatReal()); |
| 786 | APFloat::cmpResult CR_i = |
| 787 | LHS.getComplexFloatImag().compare(RHS.getComplexFloatImag()); |
| 788 | |
Daniel Dunbar | 4087e24 | 2009-01-29 06:43:41 +0000 | [diff] [blame] | 789 | if (E->getOpcode() == BinaryOperator::EQ) |
Daniel Dunbar | 131eb43 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 790 | return Success((CR_r == APFloat::cmpEqual && |
| 791 | CR_i == APFloat::cmpEqual), E); |
| 792 | else { |
| 793 | assert(E->getOpcode() == BinaryOperator::NE && |
| 794 | "Invalid complex comparison."); |
| 795 | return Success(((CR_r == APFloat::cmpGreaterThan || |
| 796 | CR_r == APFloat::cmpLessThan) && |
| 797 | (CR_i == APFloat::cmpGreaterThan || |
| 798 | CR_i == APFloat::cmpLessThan)), E); |
| 799 | } |
Daniel Dunbar | 4087e24 | 2009-01-29 06:43:41 +0000 | [diff] [blame] | 800 | } else { |
Daniel Dunbar | 4087e24 | 2009-01-29 06:43:41 +0000 | [diff] [blame] | 801 | if (E->getOpcode() == BinaryOperator::EQ) |
Daniel Dunbar | 131eb43 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 802 | return Success((LHS.getComplexIntReal() == RHS.getComplexIntReal() && |
| 803 | LHS.getComplexIntImag() == RHS.getComplexIntImag()), E); |
| 804 | else { |
| 805 | assert(E->getOpcode() == BinaryOperator::NE && |
| 806 | "Invalid compex comparison."); |
| 807 | return Success((LHS.getComplexIntReal() != RHS.getComplexIntReal() || |
| 808 | LHS.getComplexIntImag() != RHS.getComplexIntImag()), E); |
| 809 | } |
Daniel Dunbar | 4087e24 | 2009-01-29 06:43:41 +0000 | [diff] [blame] | 810 | } |
| 811 | } |
Anders Carlsson | 286f85e | 2008-11-16 07:17:21 +0000 | [diff] [blame] | 812 | |
| 813 | if (LHSTy->isRealFloatingType() && |
| 814 | RHSTy->isRealFloatingType()) { |
| 815 | APFloat RHS(0.0), LHS(0.0); |
| 816 | |
| 817 | if (!EvaluateFloat(E->getRHS(), RHS, Info)) |
| 818 | return false; |
| 819 | |
| 820 | if (!EvaluateFloat(E->getLHS(), LHS, Info)) |
| 821 | return false; |
| 822 | |
| 823 | APFloat::cmpResult CR = LHS.compare(RHS); |
Anders Carlsson | 529569e | 2008-11-16 22:46:56 +0000 | [diff] [blame] | 824 | |
Anders Carlsson | 286f85e | 2008-11-16 07:17:21 +0000 | [diff] [blame] | 825 | switch (E->getOpcode()) { |
| 826 | default: |
| 827 | assert(0 && "Invalid binary operator!"); |
| 828 | case BinaryOperator::LT: |
Daniel Dunbar | 131eb43 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 829 | return Success(CR == APFloat::cmpLessThan, E); |
Anders Carlsson | 286f85e | 2008-11-16 07:17:21 +0000 | [diff] [blame] | 830 | case BinaryOperator::GT: |
Daniel Dunbar | 131eb43 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 831 | return Success(CR == APFloat::cmpGreaterThan, E); |
Anders Carlsson | 286f85e | 2008-11-16 07:17:21 +0000 | [diff] [blame] | 832 | case BinaryOperator::LE: |
Daniel Dunbar | 131eb43 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 833 | return Success(CR == APFloat::cmpLessThan || CR == APFloat::cmpEqual, E); |
Anders Carlsson | 286f85e | 2008-11-16 07:17:21 +0000 | [diff] [blame] | 834 | case BinaryOperator::GE: |
Daniel Dunbar | 131eb43 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 835 | return Success(CR == APFloat::cmpGreaterThan || CR == APFloat::cmpEqual, |
| 836 | E); |
Anders Carlsson | 286f85e | 2008-11-16 07:17:21 +0000 | [diff] [blame] | 837 | case BinaryOperator::EQ: |
Daniel Dunbar | 131eb43 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 838 | return Success(CR == APFloat::cmpEqual, E); |
Anders Carlsson | 286f85e | 2008-11-16 07:17:21 +0000 | [diff] [blame] | 839 | case BinaryOperator::NE: |
Daniel Dunbar | 131eb43 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 840 | return Success(CR == APFloat::cmpGreaterThan |
| 841 | || CR == APFloat::cmpLessThan, E); |
Anders Carlsson | 286f85e | 2008-11-16 07:17:21 +0000 | [diff] [blame] | 842 | } |
Anders Carlsson | 286f85e | 2008-11-16 07:17:21 +0000 | [diff] [blame] | 843 | } |
| 844 | |
Anders Carlsson | 3068d11 | 2008-11-16 19:01:22 +0000 | [diff] [blame] | 845 | if (E->getOpcode() == BinaryOperator::Sub) { |
Anders Carlsson | 529569e | 2008-11-16 22:46:56 +0000 | [diff] [blame] | 846 | if (LHSTy->isPointerType() && RHSTy->isPointerType()) { |
Anders Carlsson | 3068d11 | 2008-11-16 19:01:22 +0000 | [diff] [blame] | 847 | APValue LHSValue; |
| 848 | if (!EvaluatePointer(E->getLHS(), LHSValue, Info)) |
| 849 | return false; |
| 850 | |
| 851 | APValue RHSValue; |
| 852 | if (!EvaluatePointer(E->getRHS(), RHSValue, Info)) |
| 853 | return false; |
| 854 | |
| 855 | // FIXME: Is this correct? What if only one of the operands has a base? |
| 856 | if (LHSValue.getLValueBase() || RHSValue.getLValueBase()) |
| 857 | return false; |
| 858 | |
| 859 | const QualType Type = E->getLHS()->getType(); |
| 860 | const QualType ElementType = Type->getAsPointerType()->getPointeeType(); |
| 861 | |
| 862 | uint64_t D = LHSValue.getLValueOffset() - RHSValue.getLValueOffset(); |
| 863 | D /= Info.Ctx.getTypeSize(ElementType) / 8; |
| 864 | |
Daniel Dunbar | 131eb43 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 865 | return Success(D, E); |
Anders Carlsson | 3068d11 | 2008-11-16 19:01:22 +0000 | [diff] [blame] | 866 | } |
| 867 | } |
Anders Carlsson | 286f85e | 2008-11-16 07:17:21 +0000 | [diff] [blame] | 868 | if (!LHSTy->isIntegralType() || |
| 869 | !RHSTy->isIntegralType()) { |
Eli Friedman | a6afa76 | 2008-11-13 06:09:17 +0000 | [diff] [blame] | 870 | // We can't continue from here for non-integral types, and they |
| 871 | // could potentially confuse the following operations. |
| 872 | // FIXME: Deal with EQ and friends. |
| 873 | return false; |
| 874 | } |
| 875 | |
Anders Carlsson | a25ae3d | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 876 | // The LHS of a constant expr is always evaluated and needed. |
Daniel Dunbar | 30c37f4 | 2009-02-19 20:17:33 +0000 | [diff] [blame] | 877 | if (!Visit(E->getLHS())) |
Chris Lattner | 54176fd | 2008-07-12 00:14:42 +0000 | [diff] [blame] | 878 | return false; // error in subexpression. |
Eli Friedman | d9f4bcd | 2008-07-27 05:46:18 +0000 | [diff] [blame] | 879 | |
Daniel Dunbar | 30c37f4 | 2009-02-19 20:17:33 +0000 | [diff] [blame] | 880 | // Only support arithmetic on integers for now. |
| 881 | if (!Result.isInt()) |
| 882 | return false; |
| 883 | |
Daniel Dunbar | 131eb43 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 884 | llvm::APSInt RHS; |
Chris Lattner | 54176fd | 2008-07-12 00:14:42 +0000 | [diff] [blame] | 885 | if (!EvaluateInteger(E->getRHS(), RHS, Info)) |
Chris Lattner | b542afe | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 886 | return false; |
Eli Friedman | a6afa76 | 2008-11-13 06:09:17 +0000 | [diff] [blame] | 887 | |
Anders Carlsson | a25ae3d | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 888 | switch (E->getOpcode()) { |
Chris Lattner | 32fea9d | 2008-11-12 07:43:42 +0000 | [diff] [blame] | 889 | default: |
Anders Carlsson | 0e8acbb | 2008-11-30 18:37:00 +0000 | [diff] [blame] | 890 | return Error(E->getOperatorLoc(), diag::note_invalid_subexpr_in_ice, E); |
Daniel Dunbar | 30c37f4 | 2009-02-19 20:17:33 +0000 | [diff] [blame] | 891 | case BinaryOperator::Mul: return Success(Result.getInt() * RHS, E); |
| 892 | case BinaryOperator::Add: return Success(Result.getInt() + RHS, E); |
| 893 | case BinaryOperator::Sub: return Success(Result.getInt() - RHS, E); |
| 894 | case BinaryOperator::And: return Success(Result.getInt() & RHS, E); |
| 895 | case BinaryOperator::Xor: return Success(Result.getInt() ^ RHS, E); |
| 896 | case BinaryOperator::Or: return Success(Result.getInt() | RHS, E); |
Chris Lattner | 75a4881 | 2008-07-11 22:15:16 +0000 | [diff] [blame] | 897 | case BinaryOperator::Div: |
Chris Lattner | 54176fd | 2008-07-12 00:14:42 +0000 | [diff] [blame] | 898 | if (RHS == 0) |
Anders Carlsson | 0e8acbb | 2008-11-30 18:37:00 +0000 | [diff] [blame] | 899 | return Error(E->getOperatorLoc(), diag::note_expr_divide_by_zero, E); |
Daniel Dunbar | 30c37f4 | 2009-02-19 20:17:33 +0000 | [diff] [blame] | 900 | return Success(Result.getInt() / RHS, E); |
Chris Lattner | 75a4881 | 2008-07-11 22:15:16 +0000 | [diff] [blame] | 901 | case BinaryOperator::Rem: |
Chris Lattner | 54176fd | 2008-07-12 00:14:42 +0000 | [diff] [blame] | 902 | if (RHS == 0) |
Anders Carlsson | 0e8acbb | 2008-11-30 18:37:00 +0000 | [diff] [blame] | 903 | return Error(E->getOperatorLoc(), diag::note_expr_divide_by_zero, E); |
Daniel Dunbar | 30c37f4 | 2009-02-19 20:17:33 +0000 | [diff] [blame] | 904 | return Success(Result.getInt() % RHS, E); |
Daniel Dunbar | 3f7d995 | 2009-02-19 18:37:50 +0000 | [diff] [blame] | 905 | case BinaryOperator::Shl: { |
Chris Lattner | 54176fd | 2008-07-12 00:14:42 +0000 | [diff] [blame] | 906 | // FIXME: Warn about out of range shift amounts! |
Daniel Dunbar | 30c37f4 | 2009-02-19 20:17:33 +0000 | [diff] [blame] | 907 | unsigned SA = |
| 908 | (unsigned) RHS.getLimitedValue(Result.getInt().getBitWidth()-1); |
| 909 | return Success(Result.getInt() << SA, E); |
Daniel Dunbar | 3f7d995 | 2009-02-19 18:37:50 +0000 | [diff] [blame] | 910 | } |
| 911 | case BinaryOperator::Shr: { |
Daniel Dunbar | 30c37f4 | 2009-02-19 20:17:33 +0000 | [diff] [blame] | 912 | unsigned SA = |
| 913 | (unsigned) RHS.getLimitedValue(Result.getInt().getBitWidth()-1); |
| 914 | return Success(Result.getInt() >> SA, E); |
Daniel Dunbar | 3f7d995 | 2009-02-19 18:37:50 +0000 | [diff] [blame] | 915 | } |
Chris Lattner | b542afe | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 916 | |
Daniel Dunbar | 30c37f4 | 2009-02-19 20:17:33 +0000 | [diff] [blame] | 917 | case BinaryOperator::LT: return Success(Result.getInt() < RHS, E); |
| 918 | case BinaryOperator::GT: return Success(Result.getInt() > RHS, E); |
| 919 | case BinaryOperator::LE: return Success(Result.getInt() <= RHS, E); |
| 920 | case BinaryOperator::GE: return Success(Result.getInt() >= RHS, E); |
| 921 | case BinaryOperator::EQ: return Success(Result.getInt() == RHS, E); |
| 922 | case BinaryOperator::NE: return Success(Result.getInt() != RHS, E); |
Eli Friedman | b11e778 | 2008-11-13 02:13:11 +0000 | [diff] [blame] | 923 | } |
Anders Carlsson | a25ae3d | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 924 | } |
| 925 | |
Nuno Lopes | ca7c2ea | 2008-11-16 19:28:31 +0000 | [diff] [blame] | 926 | bool IntExprEvaluator::VisitConditionalOperator(const ConditionalOperator *E) { |
Nuno Lopes | a25bd55 | 2008-11-16 22:06:39 +0000 | [diff] [blame] | 927 | bool Cond; |
| 928 | if (!HandleConversionToBool(E->getCond(), Cond, Info)) |
Nuno Lopes | ca7c2ea | 2008-11-16 19:28:31 +0000 | [diff] [blame] | 929 | return false; |
| 930 | |
Nuno Lopes | a25bd55 | 2008-11-16 22:06:39 +0000 | [diff] [blame] | 931 | return Visit(Cond ? E->getTrueExpr() : E->getFalseExpr()); |
Nuno Lopes | ca7c2ea | 2008-11-16 19:28:31 +0000 | [diff] [blame] | 932 | } |
| 933 | |
Chris Lattner | af707ab | 2009-01-24 21:53:27 +0000 | [diff] [blame] | 934 | unsigned IntExprEvaluator::GetAlignOfType(QualType T) { |
Chris Lattner | e9feb47 | 2009-01-24 21:09:06 +0000 | [diff] [blame] | 935 | const Type *Ty = Info.Ctx.getCanonicalType(T).getTypePtr(); |
| 936 | |
| 937 | // __alignof__(void) = 1 as a gcc extension. |
| 938 | if (Ty->isVoidType()) |
| 939 | return 1; |
| 940 | |
| 941 | // GCC extension: alignof(function) = 4. |
| 942 | // FIXME: AlignOf shouldn't be unconditionally 4! It should listen to the |
| 943 | // attribute(align) directive. |
| 944 | if (Ty->isFunctionType()) |
| 945 | return 4; |
| 946 | |
Fariborz Jahanian | f11284a | 2009-02-17 18:27:45 +0000 | [diff] [blame] | 947 | if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(Ty)) |
| 948 | return GetAlignOfType(QualType(EXTQT->getBaseType(), 0)); |
Chris Lattner | e9feb47 | 2009-01-24 21:09:06 +0000 | [diff] [blame] | 949 | |
| 950 | // alignof VLA/incomplete array. |
| 951 | if (const ArrayType *VAT = dyn_cast<ArrayType>(Ty)) |
| 952 | return GetAlignOfType(VAT->getElementType()); |
| 953 | |
| 954 | // sizeof (objc class)? |
| 955 | if (isa<ObjCInterfaceType>(Ty)) |
| 956 | return 1; // FIXME: This probably isn't right. |
| 957 | |
| 958 | // Get information about the alignment. |
| 959 | unsigned CharSize = Info.Ctx.Target.getCharWidth(); |
| 960 | return Info.Ctx.getTypeAlign(Ty) / CharSize; |
| 961 | } |
| 962 | |
Chris Lattner | af707ab | 2009-01-24 21:53:27 +0000 | [diff] [blame] | 963 | unsigned IntExprEvaluator::GetAlignOfExpr(const Expr *E) { |
| 964 | E = E->IgnoreParens(); |
| 965 | |
| 966 | // alignof decl is always accepted, even if it doesn't make sense: we default |
| 967 | // to 1 in those cases. |
| 968 | if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E)) |
Daniel Dunbar | b7d0844 | 2009-02-17 22:16:19 +0000 | [diff] [blame] | 969 | return Info.Ctx.getDeclAlignInBytes(DRE->getDecl()); |
Chris Lattner | af707ab | 2009-01-24 21:53:27 +0000 | [diff] [blame] | 970 | |
| 971 | if (const MemberExpr *ME = dyn_cast<MemberExpr>(E)) |
Daniel Dunbar | b7d0844 | 2009-02-17 22:16:19 +0000 | [diff] [blame] | 972 | return Info.Ctx.getDeclAlignInBytes(ME->getMemberDecl()); |
Chris Lattner | af707ab | 2009-01-24 21:53:27 +0000 | [diff] [blame] | 973 | |
Chris Lattner | e9feb47 | 2009-01-24 21:09:06 +0000 | [diff] [blame] | 974 | return GetAlignOfType(E->getType()); |
| 975 | } |
| 976 | |
| 977 | |
Sebastian Redl | 0518999 | 2008-11-11 17:56:53 +0000 | [diff] [blame] | 978 | /// VisitSizeAlignOfExpr - Evaluate a sizeof or alignof with a result as the |
| 979 | /// expression's type. |
| 980 | bool IntExprEvaluator::VisitSizeOfAlignOfExpr(const SizeOfAlignOfExpr *E) { |
| 981 | QualType DstTy = E->getType(); |
Chris Lattner | fcee001 | 2008-07-11 21:24:13 +0000 | [diff] [blame] | 982 | |
Chris Lattner | e9feb47 | 2009-01-24 21:09:06 +0000 | [diff] [blame] | 983 | // Handle alignof separately. |
| 984 | if (!E->isSizeOf()) { |
| 985 | if (E->isArgumentType()) |
Daniel Dunbar | 131eb43 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 986 | return Success(GetAlignOfType(E->getArgumentType()), E); |
Chris Lattner | e9feb47 | 2009-01-24 21:09:06 +0000 | [diff] [blame] | 987 | else |
Daniel Dunbar | 131eb43 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 988 | return Success(GetAlignOfExpr(E->getArgumentExpr()), E); |
Chris Lattner | e9feb47 | 2009-01-24 21:09:06 +0000 | [diff] [blame] | 989 | } |
| 990 | |
Sebastian Redl | 0518999 | 2008-11-11 17:56:53 +0000 | [diff] [blame] | 991 | QualType SrcTy = E->getTypeOfArgument(); |
| 992 | |
Daniel Dunbar | 131eb43 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 993 | // sizeof(void), __alignof__(void), sizeof(function) = 1 as a gcc |
| 994 | // extension. |
| 995 | if (SrcTy->isVoidType() || SrcTy->isFunctionType()) |
| 996 | return Success(1, E); |
Chris Lattner | fcee001 | 2008-07-11 21:24:13 +0000 | [diff] [blame] | 997 | |
| 998 | // sizeof(vla) is not a constantexpr: C99 6.5.3.4p2. |
Chris Lattner | e9feb47 | 2009-01-24 21:09:06 +0000 | [diff] [blame] | 999 | if (!SrcTy->isConstantSizeType()) |
Chris Lattner | fcee001 | 2008-07-11 21:24:13 +0000 | [diff] [blame] | 1000 | return false; |
Eli Friedman | f2da9df | 2009-01-24 22:19:05 +0000 | [diff] [blame] | 1001 | |
| 1002 | if (SrcTy->isObjCInterfaceType()) { |
| 1003 | // Slightly unusual case: the size of an ObjC interface type is the |
| 1004 | // size of the class. This code intentionally falls through to the normal |
| 1005 | // case. |
| 1006 | ObjCInterfaceDecl *OI = SrcTy->getAsObjCInterfaceType()->getDecl(); |
| 1007 | RecordDecl *RD = const_cast<RecordDecl*>(Info.Ctx.addRecordToClass(OI)); |
| 1008 | SrcTy = Info.Ctx.getTagDeclType(static_cast<TagDecl*>(RD)); |
| 1009 | } |
| 1010 | |
Chris Lattner | e9feb47 | 2009-01-24 21:09:06 +0000 | [diff] [blame] | 1011 | // Get information about the size. |
Chris Lattner | 87eae5e | 2008-07-11 22:52:41 +0000 | [diff] [blame] | 1012 | unsigned CharSize = Info.Ctx.Target.getCharWidth(); |
Daniel Dunbar | 131eb43 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 1013 | return Success(Info.Ctx.getTypeSize(SrcTy) / CharSize, E); |
Chris Lattner | fcee001 | 2008-07-11 21:24:13 +0000 | [diff] [blame] | 1014 | } |
| 1015 | |
Chris Lattner | b542afe | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 1016 | bool IntExprEvaluator::VisitUnaryOperator(const UnaryOperator *E) { |
Chris Lattner | 4c4867e | 2008-07-12 00:38:25 +0000 | [diff] [blame] | 1017 | // Special case unary operators that do not need their subexpression |
| 1018 | // evaluated. offsetof/sizeof/alignof are all special. |
Daniel Dunbar | 131eb43 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 1019 | if (E->isOffsetOfOp()) |
| 1020 | return Success(E->evaluateOffsetOf(Info.Ctx), E); |
Eli Friedman | a6afa76 | 2008-11-13 06:09:17 +0000 | [diff] [blame] | 1021 | |
| 1022 | if (E->getOpcode() == UnaryOperator::LNot) { |
| 1023 | // LNot's operand isn't necessarily an integer, so we handle it specially. |
| 1024 | bool bres; |
| 1025 | if (!HandleConversionToBool(E->getSubExpr(), bres, Info)) |
| 1026 | return false; |
Daniel Dunbar | 131eb43 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 1027 | return Success(!bres, E); |
Eli Friedman | a6afa76 | 2008-11-13 06:09:17 +0000 | [diff] [blame] | 1028 | } |
| 1029 | |
Chris Lattner | 87eae5e | 2008-07-11 22:52:41 +0000 | [diff] [blame] | 1030 | // Get the operand value into 'Result'. |
| 1031 | if (!Visit(E->getSubExpr())) |
Chris Lattner | 75a4881 | 2008-07-11 22:15:16 +0000 | [diff] [blame] | 1032 | return false; |
Anders Carlsson | a25ae3d | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 1033 | |
Chris Lattner | 75a4881 | 2008-07-11 22:15:16 +0000 | [diff] [blame] | 1034 | switch (E->getOpcode()) { |
Chris Lattner | 4c4867e | 2008-07-12 00:38:25 +0000 | [diff] [blame] | 1035 | default: |
Chris Lattner | 75a4881 | 2008-07-11 22:15:16 +0000 | [diff] [blame] | 1036 | // Address, indirect, pre/post inc/dec, etc are not valid constant exprs. |
| 1037 | // See C99 6.6p3. |
Anders Carlsson | 0e8acbb | 2008-11-30 18:37:00 +0000 | [diff] [blame] | 1038 | return Error(E->getOperatorLoc(), diag::note_invalid_subexpr_in_ice, E); |
Chris Lattner | 75a4881 | 2008-07-11 22:15:16 +0000 | [diff] [blame] | 1039 | case UnaryOperator::Extension: |
Chris Lattner | 4c4867e | 2008-07-12 00:38:25 +0000 | [diff] [blame] | 1040 | // FIXME: Should extension allow i-c-e extension expressions in its scope? |
| 1041 | // If so, we could clear the diagnostic ID. |
Daniel Dunbar | 3f7d995 | 2009-02-19 18:37:50 +0000 | [diff] [blame] | 1042 | return true; |
Chris Lattner | 75a4881 | 2008-07-11 22:15:16 +0000 | [diff] [blame] | 1043 | case UnaryOperator::Plus: |
Chris Lattner | 4c4867e | 2008-07-12 00:38:25 +0000 | [diff] [blame] | 1044 | // The result is always just the subexpr. |
Daniel Dunbar | 3f7d995 | 2009-02-19 18:37:50 +0000 | [diff] [blame] | 1045 | return true; |
Chris Lattner | 75a4881 | 2008-07-11 22:15:16 +0000 | [diff] [blame] | 1046 | case UnaryOperator::Minus: |
Daniel Dunbar | 30c37f4 | 2009-02-19 20:17:33 +0000 | [diff] [blame] | 1047 | if (!Result.isInt()) return false; |
| 1048 | return Success(-Result.getInt(), E); |
Chris Lattner | 75a4881 | 2008-07-11 22:15:16 +0000 | [diff] [blame] | 1049 | case UnaryOperator::Not: |
Daniel Dunbar | 30c37f4 | 2009-02-19 20:17:33 +0000 | [diff] [blame] | 1050 | if (!Result.isInt()) return false; |
| 1051 | return Success(~Result.getInt(), E); |
Anders Carlsson | a25ae3d | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 1052 | } |
Anders Carlsson | a25ae3d | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 1053 | } |
| 1054 | |
Chris Lattner | 732b223 | 2008-07-12 01:15:53 +0000 | [diff] [blame] | 1055 | /// HandleCast - This is used to evaluate implicit or explicit casts where the |
| 1056 | /// result type is integer. |
Daniel Dunbar | a2cfd34 | 2009-01-29 06:16:07 +0000 | [diff] [blame] | 1057 | bool IntExprEvaluator::VisitCastExpr(CastExpr *E) { |
Anders Carlsson | 82206e2 | 2008-11-30 18:14:57 +0000 | [diff] [blame] | 1058 | Expr *SubExpr = E->getSubExpr(); |
| 1059 | QualType DestType = E->getType(); |
Daniel Dunbar | b92dac8 | 2009-02-19 22:16:29 +0000 | [diff] [blame] | 1060 | QualType SrcType = SubExpr->getType(); |
Anders Carlsson | 82206e2 | 2008-11-30 18:14:57 +0000 | [diff] [blame] | 1061 | |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 1062 | if (DestType->isBooleanType()) { |
| 1063 | bool BoolResult; |
| 1064 | if (!HandleConversionToBool(SubExpr, BoolResult, Info)) |
| 1065 | return false; |
Daniel Dunbar | 131eb43 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 1066 | return Success(BoolResult, E); |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 1067 | } |
| 1068 | |
Anders Carlsson | a25ae3d | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 1069 | // Handle simple integer->integer casts. |
Daniel Dunbar | b92dac8 | 2009-02-19 22:16:29 +0000 | [diff] [blame] | 1070 | if (SrcType->isIntegralType()) { |
Chris Lattner | 732b223 | 2008-07-12 01:15:53 +0000 | [diff] [blame] | 1071 | if (!Visit(SubExpr)) |
Chris Lattner | b542afe | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 1072 | return false; |
Daniel Dunbar | a2cfd34 | 2009-01-29 06:16:07 +0000 | [diff] [blame] | 1073 | |
Eli Friedman | be26570 | 2009-02-20 01:15:07 +0000 | [diff] [blame] | 1074 | if (!Result.isInt()) { |
| 1075 | // Only allow casts of lvalues if they are lossless. |
| 1076 | return Info.Ctx.getTypeSize(DestType) == Info.Ctx.getTypeSize(SrcType); |
| 1077 | } |
Daniel Dunbar | 30c37f4 | 2009-02-19 20:17:33 +0000 | [diff] [blame] | 1078 | |
Daniel Dunbar | dd21164 | 2009-02-19 22:24:01 +0000 | [diff] [blame] | 1079 | return Success(HandleIntToIntCast(DestType, SrcType, |
Daniel Dunbar | 30c37f4 | 2009-02-19 20:17:33 +0000 | [diff] [blame] | 1080 | Result.getInt(), Info.Ctx), E); |
Chris Lattner | 732b223 | 2008-07-12 01:15:53 +0000 | [diff] [blame] | 1081 | } |
| 1082 | |
| 1083 | // FIXME: Clean this up! |
Daniel Dunbar | b92dac8 | 2009-02-19 22:16:29 +0000 | [diff] [blame] | 1084 | if (SrcType->isPointerType()) { |
Anders Carlsson | a25ae3d | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 1085 | APValue LV; |
Chris Lattner | 87eae5e | 2008-07-11 22:52:41 +0000 | [diff] [blame] | 1086 | if (!EvaluatePointer(SubExpr, LV, Info)) |
Chris Lattner | b542afe | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 1087 | return false; |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 1088 | |
Daniel Dunbar | dd21164 | 2009-02-19 22:24:01 +0000 | [diff] [blame] | 1089 | if (LV.getLValueBase()) { |
| 1090 | // Only allow based lvalue casts if they are lossless. |
| 1091 | if (Info.Ctx.getTypeSize(DestType) != Info.Ctx.getTypeSize(SrcType)) |
| 1092 | return false; |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 1093 | |
Daniel Dunbar | dd21164 | 2009-02-19 22:24:01 +0000 | [diff] [blame] | 1094 | Result = LV; |
| 1095 | return true; |
| 1096 | } |
| 1097 | |
| 1098 | APSInt AsInt = Info.Ctx.MakeIntValue(LV.getLValueOffset(), SrcType); |
| 1099 | return Success(HandleIntToIntCast(DestType, SrcType, AsInt, Info.Ctx), E); |
Anders Carlsson | 2bad168 | 2008-07-08 14:30:00 +0000 | [diff] [blame] | 1100 | } |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 1101 | |
Eli Friedman | be26570 | 2009-02-20 01:15:07 +0000 | [diff] [blame] | 1102 | if (SrcType->isArrayType() || SrcType->isFunctionType()) { |
| 1103 | // This handles double-conversion cases, where there's both |
| 1104 | // an l-value promotion and an implicit conversion to int. |
| 1105 | APValue LV; |
| 1106 | if (!EvaluateLValue(SubExpr, LV, Info)) |
| 1107 | return false; |
| 1108 | |
| 1109 | if (Info.Ctx.getTypeSize(DestType) != Info.Ctx.getTypeSize(Info.Ctx.VoidPtrTy)) |
| 1110 | return false; |
| 1111 | |
| 1112 | Result = LV; |
| 1113 | return true; |
| 1114 | } |
| 1115 | |
Daniel Dunbar | b92dac8 | 2009-02-19 22:16:29 +0000 | [diff] [blame] | 1116 | if (!SrcType->isRealFloatingType()) |
Anders Carlsson | 0e8acbb | 2008-11-30 18:37:00 +0000 | [diff] [blame] | 1117 | return Error(E->getExprLoc(), diag::note_invalid_subexpr_in_ice, E); |
Chris Lattner | 732b223 | 2008-07-12 01:15:53 +0000 | [diff] [blame] | 1118 | |
Eli Friedman | d8bfe7f | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 1119 | APFloat F(0.0); |
| 1120 | if (!EvaluateFloat(SubExpr, F, Info)) |
Anders Carlsson | 0e8acbb | 2008-11-30 18:37:00 +0000 | [diff] [blame] | 1121 | return Error(E->getExprLoc(), diag::note_invalid_subexpr_in_ice, E); |
Chris Lattner | 732b223 | 2008-07-12 01:15:53 +0000 | [diff] [blame] | 1122 | |
Daniel Dunbar | b92dac8 | 2009-02-19 22:16:29 +0000 | [diff] [blame] | 1123 | return Success(HandleFloatToIntCast(DestType, SrcType, F, Info.Ctx), E); |
Anders Carlsson | a25ae3d | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 1124 | } |
Anders Carlsson | 2bad168 | 2008-07-08 14:30:00 +0000 | [diff] [blame] | 1125 | |
Chris Lattner | f5eeb05 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 1126 | //===----------------------------------------------------------------------===// |
Eli Friedman | d8bfe7f | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 1127 | // Float Evaluation |
| 1128 | //===----------------------------------------------------------------------===// |
| 1129 | |
| 1130 | namespace { |
| 1131 | class VISIBILITY_HIDDEN FloatExprEvaluator |
| 1132 | : public StmtVisitor<FloatExprEvaluator, bool> { |
| 1133 | EvalInfo &Info; |
| 1134 | APFloat &Result; |
| 1135 | public: |
| 1136 | FloatExprEvaluator(EvalInfo &info, APFloat &result) |
| 1137 | : Info(info), Result(result) {} |
| 1138 | |
| 1139 | bool VisitStmt(Stmt *S) { |
| 1140 | return false; |
| 1141 | } |
| 1142 | |
| 1143 | bool VisitParenExpr(ParenExpr *E) { return Visit(E->getSubExpr()); } |
Chris Lattner | 019f4e8 | 2008-10-06 05:28:25 +0000 | [diff] [blame] | 1144 | bool VisitCallExpr(const CallExpr *E); |
Eli Friedman | d8bfe7f | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 1145 | |
Daniel Dunbar | 5db4b3f | 2008-10-16 03:51:50 +0000 | [diff] [blame] | 1146 | bool VisitUnaryOperator(const UnaryOperator *E); |
Eli Friedman | d8bfe7f | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 1147 | bool VisitBinaryOperator(const BinaryOperator *E); |
| 1148 | bool VisitFloatingLiteral(const FloatingLiteral *E); |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 1149 | bool VisitCastExpr(CastExpr *E); |
| 1150 | bool VisitCXXZeroInitValueExpr(CXXZeroInitValueExpr *E); |
Eli Friedman | d8bfe7f | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 1151 | }; |
| 1152 | } // end anonymous namespace |
| 1153 | |
| 1154 | static bool EvaluateFloat(const Expr* E, APFloat& Result, EvalInfo &Info) { |
| 1155 | return FloatExprEvaluator(Info, Result).Visit(const_cast<Expr*>(E)); |
| 1156 | } |
| 1157 | |
Chris Lattner | 019f4e8 | 2008-10-06 05:28:25 +0000 | [diff] [blame] | 1158 | bool FloatExprEvaluator::VisitCallExpr(const CallExpr *E) { |
Douglas Gregor | 3c385e5 | 2009-02-14 18:57:46 +0000 | [diff] [blame] | 1159 | switch (E->isBuiltinCall(Info.Ctx)) { |
Chris Lattner | 34a74ab | 2008-10-06 05:53:16 +0000 | [diff] [blame] | 1160 | default: return false; |
Chris Lattner | 019f4e8 | 2008-10-06 05:28:25 +0000 | [diff] [blame] | 1161 | case Builtin::BI__builtin_huge_val: |
| 1162 | case Builtin::BI__builtin_huge_valf: |
| 1163 | case Builtin::BI__builtin_huge_vall: |
| 1164 | case Builtin::BI__builtin_inf: |
| 1165 | case Builtin::BI__builtin_inff: |
Daniel Dunbar | 7cbed03 | 2008-10-14 05:41:12 +0000 | [diff] [blame] | 1166 | case Builtin::BI__builtin_infl: { |
| 1167 | const llvm::fltSemantics &Sem = |
| 1168 | Info.Ctx.getFloatTypeSemantics(E->getType()); |
Chris Lattner | 34a74ab | 2008-10-06 05:53:16 +0000 | [diff] [blame] | 1169 | Result = llvm::APFloat::getInf(Sem); |
| 1170 | return true; |
Daniel Dunbar | 7cbed03 | 2008-10-14 05:41:12 +0000 | [diff] [blame] | 1171 | } |
Chris Lattner | 9e62171 | 2008-10-06 06:31:58 +0000 | [diff] [blame] | 1172 | |
| 1173 | case Builtin::BI__builtin_nan: |
| 1174 | case Builtin::BI__builtin_nanf: |
| 1175 | case Builtin::BI__builtin_nanl: |
| 1176 | // If this is __builtin_nan("") turn this into a simple nan, otherwise we |
| 1177 | // can't constant fold it. |
| 1178 | if (const StringLiteral *S = |
| 1179 | dyn_cast<StringLiteral>(E->getArg(0)->IgnoreParenCasts())) { |
| 1180 | if (!S->isWide() && S->getByteLength() == 0) { // empty string. |
Daniel Dunbar | 7cbed03 | 2008-10-14 05:41:12 +0000 | [diff] [blame] | 1181 | const llvm::fltSemantics &Sem = |
| 1182 | Info.Ctx.getFloatTypeSemantics(E->getType()); |
Chris Lattner | 9e62171 | 2008-10-06 06:31:58 +0000 | [diff] [blame] | 1183 | Result = llvm::APFloat::getNaN(Sem); |
| 1184 | return true; |
| 1185 | } |
| 1186 | } |
| 1187 | return false; |
Daniel Dunbar | 5db4b3f | 2008-10-16 03:51:50 +0000 | [diff] [blame] | 1188 | |
| 1189 | case Builtin::BI__builtin_fabs: |
| 1190 | case Builtin::BI__builtin_fabsf: |
| 1191 | case Builtin::BI__builtin_fabsl: |
| 1192 | if (!EvaluateFloat(E->getArg(0), Result, Info)) |
| 1193 | return false; |
| 1194 | |
| 1195 | if (Result.isNegative()) |
| 1196 | Result.changeSign(); |
| 1197 | return true; |
| 1198 | |
| 1199 | case Builtin::BI__builtin_copysign: |
| 1200 | case Builtin::BI__builtin_copysignf: |
| 1201 | case Builtin::BI__builtin_copysignl: { |
| 1202 | APFloat RHS(0.); |
| 1203 | if (!EvaluateFloat(E->getArg(0), Result, Info) || |
| 1204 | !EvaluateFloat(E->getArg(1), RHS, Info)) |
| 1205 | return false; |
| 1206 | Result.copySign(RHS); |
| 1207 | return true; |
| 1208 | } |
Chris Lattner | 019f4e8 | 2008-10-06 05:28:25 +0000 | [diff] [blame] | 1209 | } |
| 1210 | } |
| 1211 | |
Daniel Dunbar | 5db4b3f | 2008-10-16 03:51:50 +0000 | [diff] [blame] | 1212 | bool FloatExprEvaluator::VisitUnaryOperator(const UnaryOperator *E) { |
Nuno Lopes | a468d34 | 2008-11-19 17:44:31 +0000 | [diff] [blame] | 1213 | if (E->getOpcode() == UnaryOperator::Deref) |
| 1214 | return false; |
| 1215 | |
Daniel Dunbar | 5db4b3f | 2008-10-16 03:51:50 +0000 | [diff] [blame] | 1216 | if (!EvaluateFloat(E->getSubExpr(), Result, Info)) |
| 1217 | return false; |
| 1218 | |
| 1219 | switch (E->getOpcode()) { |
| 1220 | default: return false; |
| 1221 | case UnaryOperator::Plus: |
| 1222 | return true; |
| 1223 | case UnaryOperator::Minus: |
| 1224 | Result.changeSign(); |
| 1225 | return true; |
| 1226 | } |
| 1227 | } |
Chris Lattner | 019f4e8 | 2008-10-06 05:28:25 +0000 | [diff] [blame] | 1228 | |
Eli Friedman | d8bfe7f | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 1229 | bool FloatExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) { |
| 1230 | // FIXME: Diagnostics? I really don't understand how the warnings |
| 1231 | // and errors are supposed to work. |
Daniel Dunbar | 5db4b3f | 2008-10-16 03:51:50 +0000 | [diff] [blame] | 1232 | APFloat RHS(0.0); |
Eli Friedman | d8bfe7f | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 1233 | if (!EvaluateFloat(E->getLHS(), Result, Info)) |
| 1234 | return false; |
| 1235 | if (!EvaluateFloat(E->getRHS(), RHS, Info)) |
| 1236 | return false; |
| 1237 | |
| 1238 | switch (E->getOpcode()) { |
| 1239 | default: return false; |
| 1240 | case BinaryOperator::Mul: |
| 1241 | Result.multiply(RHS, APFloat::rmNearestTiesToEven); |
| 1242 | return true; |
| 1243 | case BinaryOperator::Add: |
| 1244 | Result.add(RHS, APFloat::rmNearestTiesToEven); |
| 1245 | return true; |
| 1246 | case BinaryOperator::Sub: |
| 1247 | Result.subtract(RHS, APFloat::rmNearestTiesToEven); |
| 1248 | return true; |
| 1249 | case BinaryOperator::Div: |
| 1250 | Result.divide(RHS, APFloat::rmNearestTiesToEven); |
| 1251 | return true; |
| 1252 | case BinaryOperator::Rem: |
| 1253 | Result.mod(RHS, APFloat::rmNearestTiesToEven); |
| 1254 | return true; |
| 1255 | } |
| 1256 | } |
| 1257 | |
| 1258 | bool FloatExprEvaluator::VisitFloatingLiteral(const FloatingLiteral *E) { |
| 1259 | Result = E->getValue(); |
| 1260 | return true; |
| 1261 | } |
| 1262 | |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 1263 | bool FloatExprEvaluator::VisitCastExpr(CastExpr *E) { |
| 1264 | Expr* SubExpr = E->getSubExpr(); |
Nate Begeman | 59b5da6 | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 1265 | |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 1266 | if (SubExpr->getType()->isIntegralType()) { |
| 1267 | APSInt IntResult; |
Daniel Dunbar | 3f7d995 | 2009-02-19 18:37:50 +0000 | [diff] [blame] | 1268 | if (!EvaluateInteger(SubExpr, IntResult, Info)) |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 1269 | return false; |
Daniel Dunbar | a2cfd34 | 2009-01-29 06:16:07 +0000 | [diff] [blame] | 1270 | Result = HandleIntToFloatCast(E->getType(), SubExpr->getType(), |
| 1271 | IntResult, Info.Ctx); |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 1272 | return true; |
| 1273 | } |
| 1274 | if (SubExpr->getType()->isRealFloatingType()) { |
| 1275 | if (!Visit(SubExpr)) |
| 1276 | return false; |
Daniel Dunbar | a2cfd34 | 2009-01-29 06:16:07 +0000 | [diff] [blame] | 1277 | Result = HandleFloatToFloatCast(E->getType(), SubExpr->getType(), |
| 1278 | Result, Info.Ctx); |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 1279 | return true; |
| 1280 | } |
| 1281 | |
| 1282 | return false; |
| 1283 | } |
| 1284 | |
| 1285 | bool FloatExprEvaluator::VisitCXXZeroInitValueExpr(CXXZeroInitValueExpr *E) { |
| 1286 | Result = APFloat::getZero(Info.Ctx.getFloatTypeSemantics(E->getType())); |
| 1287 | return true; |
| 1288 | } |
| 1289 | |
Eli Friedman | d8bfe7f | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 1290 | //===----------------------------------------------------------------------===// |
Daniel Dunbar | a5fd07b | 2009-01-28 22:24:07 +0000 | [diff] [blame] | 1291 | // Complex Evaluation (for float and integer) |
Anders Carlsson | 9ad16ae | 2008-11-16 20:27:53 +0000 | [diff] [blame] | 1292 | //===----------------------------------------------------------------------===// |
| 1293 | |
| 1294 | namespace { |
Daniel Dunbar | a5fd07b | 2009-01-28 22:24:07 +0000 | [diff] [blame] | 1295 | class VISIBILITY_HIDDEN ComplexExprEvaluator |
| 1296 | : public StmtVisitor<ComplexExprEvaluator, APValue> { |
Anders Carlsson | 9ad16ae | 2008-11-16 20:27:53 +0000 | [diff] [blame] | 1297 | EvalInfo &Info; |
| 1298 | |
| 1299 | public: |
Daniel Dunbar | a5fd07b | 2009-01-28 22:24:07 +0000 | [diff] [blame] | 1300 | ComplexExprEvaluator(EvalInfo &info) : Info(info) {} |
Anders Carlsson | 9ad16ae | 2008-11-16 20:27:53 +0000 | [diff] [blame] | 1301 | |
| 1302 | //===--------------------------------------------------------------------===// |
| 1303 | // Visitor Methods |
| 1304 | //===--------------------------------------------------------------------===// |
| 1305 | |
| 1306 | APValue VisitStmt(Stmt *S) { |
Anders Carlsson | 9ad16ae | 2008-11-16 20:27:53 +0000 | [diff] [blame] | 1307 | return APValue(); |
| 1308 | } |
| 1309 | |
| 1310 | APValue VisitParenExpr(ParenExpr *E) { return Visit(E->getSubExpr()); } |
| 1311 | |
| 1312 | APValue VisitImaginaryLiteral(ImaginaryLiteral *E) { |
Daniel Dunbar | a5fd07b | 2009-01-28 22:24:07 +0000 | [diff] [blame] | 1313 | Expr* SubExpr = E->getSubExpr(); |
| 1314 | |
| 1315 | if (SubExpr->getType()->isRealFloatingType()) { |
| 1316 | APFloat Result(0.0); |
| 1317 | |
| 1318 | if (!EvaluateFloat(SubExpr, Result, Info)) |
| 1319 | return APValue(); |
Anders Carlsson | 9ad16ae | 2008-11-16 20:27:53 +0000 | [diff] [blame] | 1320 | |
Daniel Dunbar | 3f27987 | 2009-01-29 01:32:56 +0000 | [diff] [blame] | 1321 | return APValue(APFloat(Result.getSemantics(), APFloat::fcZero, false), |
Daniel Dunbar | a5fd07b | 2009-01-28 22:24:07 +0000 | [diff] [blame] | 1322 | Result); |
| 1323 | } else { |
| 1324 | assert(SubExpr->getType()->isIntegerType() && |
| 1325 | "Unexpected imaginary literal."); |
| 1326 | |
| 1327 | llvm::APSInt Result; |
| 1328 | if (!EvaluateInteger(SubExpr, Result, Info)) |
| 1329 | return APValue(); |
| 1330 | |
| 1331 | llvm::APSInt Zero(Result.getBitWidth(), !Result.isSigned()); |
| 1332 | Zero = 0; |
| 1333 | return APValue(Zero, Result); |
| 1334 | } |
Anders Carlsson | 9ad16ae | 2008-11-16 20:27:53 +0000 | [diff] [blame] | 1335 | } |
| 1336 | |
Anders Carlsson | ccc3fce | 2008-11-16 21:51:21 +0000 | [diff] [blame] | 1337 | APValue VisitCastExpr(CastExpr *E) { |
| 1338 | Expr* SubExpr = E->getSubExpr(); |
Daniel Dunbar | a2cfd34 | 2009-01-29 06:16:07 +0000 | [diff] [blame] | 1339 | QualType EltType = E->getType()->getAsComplexType()->getElementType(); |
| 1340 | QualType SubType = SubExpr->getType(); |
Anders Carlsson | ccc3fce | 2008-11-16 21:51:21 +0000 | [diff] [blame] | 1341 | |
Daniel Dunbar | a2cfd34 | 2009-01-29 06:16:07 +0000 | [diff] [blame] | 1342 | if (SubType->isRealFloatingType()) { |
Anders Carlsson | ccc3fce | 2008-11-16 21:51:21 +0000 | [diff] [blame] | 1343 | APFloat Result(0.0); |
| 1344 | |
| 1345 | if (!EvaluateFloat(SubExpr, Result, Info)) |
| 1346 | return APValue(); |
| 1347 | |
Daniel Dunbar | a2cfd34 | 2009-01-29 06:16:07 +0000 | [diff] [blame] | 1348 | // Apply float conversion if necessary. |
| 1349 | Result = HandleFloatToFloatCast(EltType, SubType, Result, Info.Ctx); |
Daniel Dunbar | 8f826f0 | 2009-01-24 19:08:01 +0000 | [diff] [blame] | 1350 | return APValue(Result, |
Daniel Dunbar | 3f27987 | 2009-01-29 01:32:56 +0000 | [diff] [blame] | 1351 | APFloat(Result.getSemantics(), APFloat::fcZero, false)); |
Daniel Dunbar | a2cfd34 | 2009-01-29 06:16:07 +0000 | [diff] [blame] | 1352 | } else if (SubType->isIntegerType()) { |
Daniel Dunbar | a5fd07b | 2009-01-28 22:24:07 +0000 | [diff] [blame] | 1353 | APSInt Result; |
| 1354 | |
| 1355 | if (!EvaluateInteger(SubExpr, Result, Info)) |
| 1356 | return APValue(); |
Daniel Dunbar | a2cfd34 | 2009-01-29 06:16:07 +0000 | [diff] [blame] | 1357 | |
| 1358 | // Apply integer conversion if necessary. |
| 1359 | Result = HandleIntToIntCast(EltType, SubType, Result, Info.Ctx); |
Daniel Dunbar | a5fd07b | 2009-01-28 22:24:07 +0000 | [diff] [blame] | 1360 | llvm::APSInt Zero(Result.getBitWidth(), !Result.isSigned()); |
| 1361 | Zero = 0; |
| 1362 | return APValue(Result, Zero); |
Daniel Dunbar | a2cfd34 | 2009-01-29 06:16:07 +0000 | [diff] [blame] | 1363 | } else if (const ComplexType *CT = SubType->getAsComplexType()) { |
| 1364 | APValue Src; |
| 1365 | |
| 1366 | if (!EvaluateComplex(SubExpr, Src, Info)) |
| 1367 | return APValue(); |
| 1368 | |
| 1369 | QualType SrcType = CT->getElementType(); |
| 1370 | |
| 1371 | if (Src.isComplexFloat()) { |
| 1372 | if (EltType->isRealFloatingType()) { |
| 1373 | return APValue(HandleFloatToFloatCast(EltType, SrcType, |
| 1374 | Src.getComplexFloatReal(), |
| 1375 | Info.Ctx), |
| 1376 | HandleFloatToFloatCast(EltType, SrcType, |
| 1377 | Src.getComplexFloatImag(), |
| 1378 | Info.Ctx)); |
| 1379 | } else { |
| 1380 | return APValue(HandleFloatToIntCast(EltType, SrcType, |
| 1381 | Src.getComplexFloatReal(), |
| 1382 | Info.Ctx), |
| 1383 | HandleFloatToIntCast(EltType, SrcType, |
| 1384 | Src.getComplexFloatImag(), |
| 1385 | Info.Ctx)); |
| 1386 | } |
| 1387 | } else { |
| 1388 | assert(Src.isComplexInt() && "Invalid evaluate result."); |
| 1389 | if (EltType->isRealFloatingType()) { |
| 1390 | return APValue(HandleIntToFloatCast(EltType, SrcType, |
| 1391 | Src.getComplexIntReal(), |
| 1392 | Info.Ctx), |
| 1393 | HandleIntToFloatCast(EltType, SrcType, |
| 1394 | Src.getComplexIntImag(), |
| 1395 | Info.Ctx)); |
| 1396 | } else { |
| 1397 | return APValue(HandleIntToIntCast(EltType, SrcType, |
| 1398 | Src.getComplexIntReal(), |
| 1399 | Info.Ctx), |
| 1400 | HandleIntToIntCast(EltType, SrcType, |
| 1401 | Src.getComplexIntImag(), |
| 1402 | Info.Ctx)); |
| 1403 | } |
| 1404 | } |
Anders Carlsson | ccc3fce | 2008-11-16 21:51:21 +0000 | [diff] [blame] | 1405 | } |
| 1406 | |
| 1407 | // FIXME: Handle more casts. |
| 1408 | return APValue(); |
| 1409 | } |
| 1410 | |
| 1411 | APValue VisitBinaryOperator(const BinaryOperator *E); |
| 1412 | |
Anders Carlsson | 9ad16ae | 2008-11-16 20:27:53 +0000 | [diff] [blame] | 1413 | }; |
| 1414 | } // end anonymous namespace |
| 1415 | |
Daniel Dunbar | a5fd07b | 2009-01-28 22:24:07 +0000 | [diff] [blame] | 1416 | static bool EvaluateComplex(const Expr *E, APValue &Result, EvalInfo &Info) |
Anders Carlsson | 9ad16ae | 2008-11-16 20:27:53 +0000 | [diff] [blame] | 1417 | { |
Daniel Dunbar | a5fd07b | 2009-01-28 22:24:07 +0000 | [diff] [blame] | 1418 | Result = ComplexExprEvaluator(Info).Visit(const_cast<Expr*>(E)); |
| 1419 | assert((!Result.isComplexFloat() || |
| 1420 | (&Result.getComplexFloatReal().getSemantics() == |
| 1421 | &Result.getComplexFloatImag().getSemantics())) && |
| 1422 | "Invalid complex evaluation."); |
| 1423 | return Result.isComplexFloat() || Result.isComplexInt(); |
Anders Carlsson | 9ad16ae | 2008-11-16 20:27:53 +0000 | [diff] [blame] | 1424 | } |
| 1425 | |
Daniel Dunbar | a5fd07b | 2009-01-28 22:24:07 +0000 | [diff] [blame] | 1426 | APValue ComplexExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) |
Anders Carlsson | ccc3fce | 2008-11-16 21:51:21 +0000 | [diff] [blame] | 1427 | { |
| 1428 | APValue Result, RHS; |
| 1429 | |
Daniel Dunbar | a5fd07b | 2009-01-28 22:24:07 +0000 | [diff] [blame] | 1430 | if (!EvaluateComplex(E->getLHS(), Result, Info)) |
Anders Carlsson | ccc3fce | 2008-11-16 21:51:21 +0000 | [diff] [blame] | 1431 | return APValue(); |
| 1432 | |
Daniel Dunbar | a5fd07b | 2009-01-28 22:24:07 +0000 | [diff] [blame] | 1433 | if (!EvaluateComplex(E->getRHS(), RHS, Info)) |
Anders Carlsson | ccc3fce | 2008-11-16 21:51:21 +0000 | [diff] [blame] | 1434 | return APValue(); |
Daniel Dunbar | a5fd07b | 2009-01-28 22:24:07 +0000 | [diff] [blame] | 1435 | |
Daniel Dunbar | 3f27987 | 2009-01-29 01:32:56 +0000 | [diff] [blame] | 1436 | assert(Result.isComplexFloat() == RHS.isComplexFloat() && |
| 1437 | "Invalid operands to binary operator."); |
Anders Carlsson | ccc3fce | 2008-11-16 21:51:21 +0000 | [diff] [blame] | 1438 | switch (E->getOpcode()) { |
| 1439 | default: return APValue(); |
| 1440 | case BinaryOperator::Add: |
Daniel Dunbar | a5fd07b | 2009-01-28 22:24:07 +0000 | [diff] [blame] | 1441 | if (Result.isComplexFloat()) { |
| 1442 | Result.getComplexFloatReal().add(RHS.getComplexFloatReal(), |
| 1443 | APFloat::rmNearestTiesToEven); |
| 1444 | Result.getComplexFloatImag().add(RHS.getComplexFloatImag(), |
| 1445 | APFloat::rmNearestTiesToEven); |
| 1446 | } else { |
| 1447 | Result.getComplexIntReal() += RHS.getComplexIntReal(); |
| 1448 | Result.getComplexIntImag() += RHS.getComplexIntImag(); |
| 1449 | } |
Daniel Dunbar | 3f27987 | 2009-01-29 01:32:56 +0000 | [diff] [blame] | 1450 | break; |
Anders Carlsson | ccc3fce | 2008-11-16 21:51:21 +0000 | [diff] [blame] | 1451 | case BinaryOperator::Sub: |
Daniel Dunbar | a5fd07b | 2009-01-28 22:24:07 +0000 | [diff] [blame] | 1452 | if (Result.isComplexFloat()) { |
| 1453 | Result.getComplexFloatReal().subtract(RHS.getComplexFloatReal(), |
| 1454 | APFloat::rmNearestTiesToEven); |
| 1455 | Result.getComplexFloatImag().subtract(RHS.getComplexFloatImag(), |
| 1456 | APFloat::rmNearestTiesToEven); |
| 1457 | } else { |
| 1458 | Result.getComplexIntReal() -= RHS.getComplexIntReal(); |
| 1459 | Result.getComplexIntImag() -= RHS.getComplexIntImag(); |
| 1460 | } |
Daniel Dunbar | 3f27987 | 2009-01-29 01:32:56 +0000 | [diff] [blame] | 1461 | break; |
| 1462 | case BinaryOperator::Mul: |
| 1463 | if (Result.isComplexFloat()) { |
| 1464 | APValue LHS = Result; |
| 1465 | APFloat &LHS_r = LHS.getComplexFloatReal(); |
| 1466 | APFloat &LHS_i = LHS.getComplexFloatImag(); |
| 1467 | APFloat &RHS_r = RHS.getComplexFloatReal(); |
| 1468 | APFloat &RHS_i = RHS.getComplexFloatImag(); |
| 1469 | |
| 1470 | APFloat Tmp = LHS_r; |
| 1471 | Tmp.multiply(RHS_r, APFloat::rmNearestTiesToEven); |
| 1472 | Result.getComplexFloatReal() = Tmp; |
| 1473 | Tmp = LHS_i; |
| 1474 | Tmp.multiply(RHS_i, APFloat::rmNearestTiesToEven); |
| 1475 | Result.getComplexFloatReal().subtract(Tmp, APFloat::rmNearestTiesToEven); |
| 1476 | |
| 1477 | Tmp = LHS_r; |
| 1478 | Tmp.multiply(RHS_i, APFloat::rmNearestTiesToEven); |
| 1479 | Result.getComplexFloatImag() = Tmp; |
| 1480 | Tmp = LHS_i; |
| 1481 | Tmp.multiply(RHS_r, APFloat::rmNearestTiesToEven); |
| 1482 | Result.getComplexFloatImag().add(Tmp, APFloat::rmNearestTiesToEven); |
| 1483 | } else { |
| 1484 | APValue LHS = Result; |
| 1485 | Result.getComplexIntReal() = |
| 1486 | (LHS.getComplexIntReal() * RHS.getComplexIntReal() - |
| 1487 | LHS.getComplexIntImag() * RHS.getComplexIntImag()); |
| 1488 | Result.getComplexIntImag() = |
| 1489 | (LHS.getComplexIntReal() * RHS.getComplexIntImag() + |
| 1490 | LHS.getComplexIntImag() * RHS.getComplexIntReal()); |
| 1491 | } |
| 1492 | break; |
Anders Carlsson | ccc3fce | 2008-11-16 21:51:21 +0000 | [diff] [blame] | 1493 | } |
| 1494 | |
| 1495 | return Result; |
| 1496 | } |
| 1497 | |
Anders Carlsson | 9ad16ae | 2008-11-16 20:27:53 +0000 | [diff] [blame] | 1498 | //===----------------------------------------------------------------------===// |
Chris Lattner | 6ee7aa1 | 2008-11-16 21:24:15 +0000 | [diff] [blame] | 1499 | // Top level Expr::Evaluate method. |
Chris Lattner | f5eeb05 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 1500 | //===----------------------------------------------------------------------===// |
| 1501 | |
Chris Lattner | 6ee7aa1 | 2008-11-16 21:24:15 +0000 | [diff] [blame] | 1502 | /// 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] | 1503 | /// any crazy technique (that has nothing to do with language standards) that |
| 1504 | /// we want to. If this function returns true, it returns the folded constant |
| 1505 | /// in Result. |
Anders Carlsson | 5b45d4e | 2008-11-30 16:58:53 +0000 | [diff] [blame] | 1506 | bool Expr::Evaluate(EvalResult &Result, ASTContext &Ctx) const { |
| 1507 | EvalInfo Info(Ctx, Result); |
Anders Carlsson | 54da049 | 2008-11-30 16:38:33 +0000 | [diff] [blame] | 1508 | |
Nate Begeman | 59b5da6 | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 1509 | if (getType()->isVectorType()) { |
| 1510 | if (!EvaluateVector(this, Result.Val, Info)) |
| 1511 | return false; |
| 1512 | } else if (getType()->isIntegerType()) { |
Daniel Dunbar | 30c37f4 | 2009-02-19 20:17:33 +0000 | [diff] [blame] | 1513 | if (!IntExprEvaluator(Info, Result.Val).Visit(const_cast<Expr*>(this))) |
Anders Carlsson | 6dde0d5 | 2008-11-22 21:50:49 +0000 | [diff] [blame] | 1514 | return false; |
Mike Stump | ca2f3fd | 2009-02-18 21:44:49 +0000 | [diff] [blame] | 1515 | } else if (getType()->isPointerType() |
| 1516 | || getType()->isBlockPointerType()) { |
Anders Carlsson | 5b45d4e | 2008-11-30 16:58:53 +0000 | [diff] [blame] | 1517 | if (!EvaluatePointer(this, Result.Val, Info)) |
Anders Carlsson | 6dde0d5 | 2008-11-22 21:50:49 +0000 | [diff] [blame] | 1518 | return false; |
Eli Friedman | d8bfe7f | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 1519 | } else if (getType()->isRealFloatingType()) { |
| 1520 | llvm::APFloat f(0.0); |
Anders Carlsson | 6dde0d5 | 2008-11-22 21:50:49 +0000 | [diff] [blame] | 1521 | if (!EvaluateFloat(this, f, Info)) |
| 1522 | return false; |
| 1523 | |
Anders Carlsson | 5b45d4e | 2008-11-30 16:58:53 +0000 | [diff] [blame] | 1524 | Result.Val = APValue(f); |
Daniel Dunbar | a5fd07b | 2009-01-28 22:24:07 +0000 | [diff] [blame] | 1525 | } else if (getType()->isAnyComplexType()) { |
| 1526 | if (!EvaluateComplex(this, Result.Val, Info)) |
Anders Carlsson | 6dde0d5 | 2008-11-22 21:50:49 +0000 | [diff] [blame] | 1527 | return false; |
Daniel Dunbar | a5fd07b | 2009-01-28 22:24:07 +0000 | [diff] [blame] | 1528 | } else |
Anders Carlsson | 9d4c157 | 2008-11-22 22:56:32 +0000 | [diff] [blame] | 1529 | return false; |
Anders Carlsson | 6dde0d5 | 2008-11-22 21:50:49 +0000 | [diff] [blame] | 1530 | |
Anders Carlsson | 5b45d4e | 2008-11-30 16:58:53 +0000 | [diff] [blame] | 1531 | return true; |
| 1532 | } |
| 1533 | |
Chris Lattner | 6ee7aa1 | 2008-11-16 21:24:15 +0000 | [diff] [blame] | 1534 | /// isEvaluatable - Call Evaluate to see if this expression can be constant |
Chris Lattner | 45b6b9d | 2008-10-06 06:49:02 +0000 | [diff] [blame] | 1535 | /// folded, but discard the result. |
| 1536 | bool Expr::isEvaluatable(ASTContext &Ctx) const { |
Anders Carlsson | 4fdfb09 | 2008-12-01 06:44:05 +0000 | [diff] [blame] | 1537 | EvalResult Result; |
| 1538 | return Evaluate(Result, Ctx) && !Result.HasSideEffects; |
Chris Lattner | 45b6b9d | 2008-10-06 06:49:02 +0000 | [diff] [blame] | 1539 | } |
Anders Carlsson | 51fe996 | 2008-11-22 21:04:56 +0000 | [diff] [blame] | 1540 | |
| 1541 | APSInt Expr::EvaluateAsInt(ASTContext &Ctx) const { |
Anders Carlsson | 1c0cfd4 | 2008-12-19 20:58:05 +0000 | [diff] [blame] | 1542 | EvalResult EvalResult; |
| 1543 | bool Result = Evaluate(EvalResult, Ctx); |
Daniel Dunbar | f185319 | 2009-01-15 18:32:35 +0000 | [diff] [blame] | 1544 | Result = Result; |
Anders Carlsson | 51fe996 | 2008-11-22 21:04:56 +0000 | [diff] [blame] | 1545 | assert(Result && "Could not evaluate expression"); |
Anders Carlsson | 1c0cfd4 | 2008-12-19 20:58:05 +0000 | [diff] [blame] | 1546 | assert(EvalResult.Val.isInt() && "Expression did not evaluate to integer"); |
Anders Carlsson | 51fe996 | 2008-11-22 21:04:56 +0000 | [diff] [blame] | 1547 | |
Anders Carlsson | 1c0cfd4 | 2008-12-19 20:58:05 +0000 | [diff] [blame] | 1548 | return EvalResult.Val.getInt(); |
Anders Carlsson | 51fe996 | 2008-11-22 21:04:56 +0000 | [diff] [blame] | 1549 | } |