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