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