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