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