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" |
Seo Sanghyeon | 0fe52e1 | 2008-07-08 07:23:12 +0000 | [diff] [blame] | 16 | #include "clang/AST/StmtVisitor.h" |
Chris Lattner | 54176fd | 2008-07-12 00:14:42 +0000 | [diff] [blame] | 17 | #include "clang/Basic/Diagnostic.h" |
Anders Carlsson | 06a3675 | 2008-07-08 05:49:43 +0000 | [diff] [blame] | 18 | #include "clang/Basic/TargetInfo.h" |
Anders Carlsson | c754aa6 | 2008-07-08 05:13:58 +0000 | [diff] [blame] | 19 | #include "llvm/Support/Compiler.h" |
Anders Carlsson | c44eec6 | 2008-07-03 04:20:39 +0000 | [diff] [blame] | 20 | using namespace clang; |
Chris Lattner | f5eeb05 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 21 | using llvm::APSInt; |
Eli Friedman | d8bfe7f | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 22 | using llvm::APFloat; |
Anders Carlsson | c44eec6 | 2008-07-03 04:20:39 +0000 | [diff] [blame] | 23 | |
Chris Lattner | 87eae5e | 2008-07-11 22:52:41 +0000 | [diff] [blame] | 24 | /// EvalInfo - This is a private struct used by the evaluator to capture |
| 25 | /// information about a subexpression as it is folded. It retains information |
| 26 | /// about the AST context, but also maintains information about the folded |
| 27 | /// expression. |
| 28 | /// |
| 29 | /// If an expression could be evaluated, it is still possible it is not a C |
| 30 | /// "integer constant expression" or constant expression. If not, this struct |
| 31 | /// captures information about how and why not. |
| 32 | /// |
| 33 | /// One bit of information passed *into* the request for constant folding |
| 34 | /// indicates whether the subexpression is "evaluated" or not according to C |
| 35 | /// rules. For example, the RHS of (0 && foo()) is not evaluated. We can |
| 36 | /// evaluate the expression regardless of what the RHS is, but C only allows |
| 37 | /// certain things in certain situations. |
| 38 | struct EvalInfo { |
| 39 | ASTContext &Ctx; |
| 40 | |
| 41 | /// isEvaluated - True if the subexpression is required to be evaluated, false |
| 42 | /// if it is short-circuited (according to C rules). |
| 43 | bool isEvaluated; |
| 44 | |
Chris Lattner | 54176fd | 2008-07-12 00:14:42 +0000 | [diff] [blame] | 45 | /// ICEDiag - If the expression is unfoldable, then ICEDiag contains the |
| 46 | /// error diagnostic indicating why it is not foldable and DiagLoc indicates a |
| 47 | /// caret position for the error. If it is foldable, but the expression is |
| 48 | /// not an integer constant expression, ICEDiag contains the extension |
| 49 | /// diagnostic to emit which describes why it isn't an integer constant |
| 50 | /// expression. If this expression *is* an integer-constant-expr, then |
| 51 | /// ICEDiag is zero. |
Chris Lattner | 87eae5e | 2008-07-11 22:52:41 +0000 | [diff] [blame] | 52 | /// |
Chris Lattner | 54176fd | 2008-07-12 00:14:42 +0000 | [diff] [blame] | 53 | /// The caller can choose to emit this diagnostic or not, depending on whether |
| 54 | /// they require an i-c-e or a constant or not. DiagLoc indicates the caret |
| 55 | /// position for the report. |
| 56 | /// |
| 57 | /// If ICEDiag is zero, then this expression is an i-c-e. |
Chris Lattner | 87eae5e | 2008-07-11 22:52:41 +0000 | [diff] [blame] | 58 | unsigned ICEDiag; |
| 59 | SourceLocation DiagLoc; |
| 60 | |
| 61 | EvalInfo(ASTContext &ctx) : Ctx(ctx), isEvaluated(true), ICEDiag(0) {} |
| 62 | }; |
| 63 | |
| 64 | |
| 65 | static bool EvaluatePointer(const Expr *E, APValue &Result, EvalInfo &Info); |
| 66 | static bool EvaluateInteger(const Expr *E, APSInt &Result, EvalInfo &Info); |
Eli Friedman | d8bfe7f | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 67 | static bool EvaluateFloat(const Expr *E, APFloat &Result, EvalInfo &Info); |
Chris Lattner | f5eeb05 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 68 | |
| 69 | //===----------------------------------------------------------------------===// |
| 70 | // Pointer Evaluation |
| 71 | //===----------------------------------------------------------------------===// |
| 72 | |
Anders Carlsson | c754aa6 | 2008-07-08 05:13:58 +0000 | [diff] [blame] | 73 | namespace { |
Anders Carlsson | 2bad168 | 2008-07-08 14:30:00 +0000 | [diff] [blame] | 74 | class VISIBILITY_HIDDEN PointerExprEvaluator |
| 75 | : public StmtVisitor<PointerExprEvaluator, APValue> { |
Chris Lattner | 87eae5e | 2008-07-11 22:52:41 +0000 | [diff] [blame] | 76 | EvalInfo &Info; |
Anders Carlsson | 2bad168 | 2008-07-08 14:30:00 +0000 | [diff] [blame] | 77 | public: |
Anders Carlsson | 2bad168 | 2008-07-08 14:30:00 +0000 | [diff] [blame] | 78 | |
Chris Lattner | 87eae5e | 2008-07-11 22:52:41 +0000 | [diff] [blame] | 79 | PointerExprEvaluator(EvalInfo &info) : Info(info) {} |
Chris Lattner | f5eeb05 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 80 | |
Anders Carlsson | 2bad168 | 2008-07-08 14:30:00 +0000 | [diff] [blame] | 81 | APValue VisitStmt(Stmt *S) { |
| 82 | // FIXME: Remove this when we support more expressions. |
Anders Carlsson | 650c92f | 2008-07-08 15:34:11 +0000 | [diff] [blame] | 83 | printf("Unhandled pointer statement\n"); |
Anders Carlsson | 2bad168 | 2008-07-08 14:30:00 +0000 | [diff] [blame] | 84 | S->dump(); |
| 85 | return APValue(); |
| 86 | } |
| 87 | |
| 88 | APValue VisitParenExpr(ParenExpr *E) { return Visit(E->getSubExpr()); } |
| 89 | |
Anders Carlsson | 650c92f | 2008-07-08 15:34:11 +0000 | [diff] [blame] | 90 | APValue VisitBinaryOperator(const BinaryOperator *E); |
| 91 | APValue VisitCastExpr(const CastExpr* E); |
Anders Carlsson | 650c92f | 2008-07-08 15:34:11 +0000 | [diff] [blame] | 92 | }; |
Chris Lattner | f5eeb05 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 93 | } // end anonymous namespace |
Anders Carlsson | 650c92f | 2008-07-08 15:34:11 +0000 | [diff] [blame] | 94 | |
Chris Lattner | 87eae5e | 2008-07-11 22:52:41 +0000 | [diff] [blame] | 95 | static bool EvaluatePointer(const Expr* E, APValue& Result, EvalInfo &Info) { |
Chris Lattner | f5eeb05 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 96 | if (!E->getType()->isPointerType()) |
| 97 | return false; |
Chris Lattner | 87eae5e | 2008-07-11 22:52:41 +0000 | [diff] [blame] | 98 | Result = PointerExprEvaluator(Info).Visit(const_cast<Expr*>(E)); |
Chris Lattner | f5eeb05 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 99 | return Result.isLValue(); |
| 100 | } |
| 101 | |
| 102 | APValue PointerExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) { |
| 103 | if (E->getOpcode() != BinaryOperator::Add && |
| 104 | E->getOpcode() != BinaryOperator::Sub) |
| 105 | return APValue(); |
| 106 | |
| 107 | const Expr *PExp = E->getLHS(); |
| 108 | const Expr *IExp = E->getRHS(); |
| 109 | if (IExp->getType()->isPointerType()) |
| 110 | std::swap(PExp, IExp); |
| 111 | |
| 112 | APValue ResultLValue; |
Chris Lattner | 87eae5e | 2008-07-11 22:52:41 +0000 | [diff] [blame] | 113 | if (!EvaluatePointer(PExp, ResultLValue, Info)) |
Chris Lattner | f5eeb05 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 114 | return APValue(); |
| 115 | |
| 116 | llvm::APSInt AdditionalOffset(32); |
Chris Lattner | 87eae5e | 2008-07-11 22:52:41 +0000 | [diff] [blame] | 117 | if (!EvaluateInteger(IExp, AdditionalOffset, Info)) |
Chris Lattner | f5eeb05 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 118 | return APValue(); |
| 119 | |
| 120 | uint64_t Offset = ResultLValue.getLValueOffset(); |
| 121 | if (E->getOpcode() == BinaryOperator::Add) |
| 122 | Offset += AdditionalOffset.getZExtValue(); |
| 123 | else |
| 124 | Offset -= AdditionalOffset.getZExtValue(); |
| 125 | |
| 126 | return APValue(ResultLValue.getLValueBase(), Offset); |
| 127 | } |
| 128 | |
| 129 | |
Chris Lattner | b542afe | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 130 | APValue PointerExprEvaluator::VisitCastExpr(const CastExpr* E) { |
Chris Lattner | f5eeb05 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 131 | const Expr* SubExpr = E->getSubExpr(); |
| 132 | |
| 133 | // Check for pointer->pointer cast |
| 134 | if (SubExpr->getType()->isPointerType()) { |
| 135 | APValue Result; |
Chris Lattner | 87eae5e | 2008-07-11 22:52:41 +0000 | [diff] [blame] | 136 | if (EvaluatePointer(SubExpr, Result, Info)) |
Chris Lattner | f5eeb05 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 137 | return Result; |
| 138 | return APValue(); |
| 139 | } |
| 140 | |
Eli Friedman | d9f4bcd | 2008-07-27 05:46:18 +0000 | [diff] [blame] | 141 | if (SubExpr->getType()->isIntegralType()) { |
Chris Lattner | f5eeb05 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 142 | llvm::APSInt Result(32); |
Chris Lattner | 87eae5e | 2008-07-11 22:52:41 +0000 | [diff] [blame] | 143 | if (EvaluateInteger(SubExpr, Result, Info)) { |
| 144 | Result.extOrTrunc((unsigned)Info.Ctx.getTypeSize(E->getType())); |
Chris Lattner | f5eeb05 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 145 | return APValue(0, Result.getZExtValue()); |
| 146 | } |
| 147 | } |
| 148 | |
| 149 | assert(0 && "Unhandled cast"); |
| 150 | return APValue(); |
| 151 | } |
| 152 | |
| 153 | |
| 154 | //===----------------------------------------------------------------------===// |
| 155 | // Integer Evaluation |
| 156 | //===----------------------------------------------------------------------===// |
Chris Lattner | f5eeb05 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 157 | |
| 158 | namespace { |
Anders Carlsson | c754aa6 | 2008-07-08 05:13:58 +0000 | [diff] [blame] | 159 | class VISIBILITY_HIDDEN IntExprEvaluator |
Chris Lattner | b542afe | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 160 | : public StmtVisitor<IntExprEvaluator, bool> { |
Chris Lattner | 87eae5e | 2008-07-11 22:52:41 +0000 | [diff] [blame] | 161 | EvalInfo &Info; |
Chris Lattner | b542afe | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 162 | APSInt &Result; |
Anders Carlsson | c754aa6 | 2008-07-08 05:13:58 +0000 | [diff] [blame] | 163 | public: |
Chris Lattner | 87eae5e | 2008-07-11 22:52:41 +0000 | [diff] [blame] | 164 | IntExprEvaluator(EvalInfo &info, APSInt &result) |
| 165 | : Info(info), Result(result) {} |
Chris Lattner | f5eeb05 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 166 | |
Chris Lattner | 7a76778 | 2008-07-11 19:24:49 +0000 | [diff] [blame] | 167 | unsigned getIntTypeSizeInBits(QualType T) const { |
Chris Lattner | 54176fd | 2008-07-12 00:14:42 +0000 | [diff] [blame] | 168 | return (unsigned)Info.Ctx.getIntWidth(T); |
| 169 | } |
| 170 | |
| 171 | bool Extension(SourceLocation L, diag::kind D) { |
| 172 | Info.DiagLoc = L; |
| 173 | Info.ICEDiag = D; |
| 174 | return true; // still a constant. |
| 175 | } |
| 176 | |
| 177 | bool Error(SourceLocation L, diag::kind D) { |
| 178 | // If this is in an unevaluated portion of the subexpression, ignore the |
| 179 | // error. |
| 180 | if (!Info.isEvaluated) |
| 181 | return true; |
| 182 | |
| 183 | Info.DiagLoc = L; |
| 184 | Info.ICEDiag = D; |
| 185 | return false; |
Chris Lattner | 7a76778 | 2008-07-11 19:24:49 +0000 | [diff] [blame] | 186 | } |
| 187 | |
Anders Carlsson | c754aa6 | 2008-07-08 05:13:58 +0000 | [diff] [blame] | 188 | //===--------------------------------------------------------------------===// |
| 189 | // Visitor Methods |
| 190 | //===--------------------------------------------------------------------===// |
Chris Lattner | 7a76778 | 2008-07-11 19:24:49 +0000 | [diff] [blame] | 191 | |
Chris Lattner | b542afe | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 192 | bool VisitStmt(Stmt *S) { |
Chris Lattner | 54176fd | 2008-07-12 00:14:42 +0000 | [diff] [blame] | 193 | return Error(S->getLocStart(), diag::err_expr_not_constant); |
Anders Carlsson | c754aa6 | 2008-07-08 05:13:58 +0000 | [diff] [blame] | 194 | } |
| 195 | |
Chris Lattner | b542afe | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 196 | bool VisitParenExpr(ParenExpr *E) { return Visit(E->getSubExpr()); } |
Anders Carlsson | c754aa6 | 2008-07-08 05:13:58 +0000 | [diff] [blame] | 197 | |
Chris Lattner | 4c4867e | 2008-07-12 00:38:25 +0000 | [diff] [blame] | 198 | bool VisitIntegerLiteral(const IntegerLiteral *E) { |
| 199 | Result = E->getValue(); |
| 200 | Result.setIsUnsigned(E->getType()->isUnsignedIntegerType()); |
| 201 | return true; |
| 202 | } |
| 203 | bool VisitCharacterLiteral(const CharacterLiteral *E) { |
| 204 | Result.zextOrTrunc(getIntTypeSizeInBits(E->getType())); |
| 205 | Result = E->getValue(); |
| 206 | Result.setIsUnsigned(E->getType()->isUnsignedIntegerType()); |
| 207 | return true; |
| 208 | } |
| 209 | bool VisitTypesCompatibleExpr(const TypesCompatibleExpr *E) { |
| 210 | Result.zextOrTrunc(getIntTypeSizeInBits(E->getType())); |
| 211 | Result = Info.Ctx.typesAreCompatible(E->getArgType1(), E->getArgType2()); |
| 212 | return true; |
| 213 | } |
| 214 | bool VisitDeclRefExpr(const DeclRefExpr *E); |
| 215 | bool VisitCallExpr(const CallExpr *E); |
Chris Lattner | b542afe | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 216 | bool VisitBinaryOperator(const BinaryOperator *E); |
| 217 | bool VisitUnaryOperator(const UnaryOperator *E); |
Anders Carlsson | 06a3675 | 2008-07-08 05:49:43 +0000 | [diff] [blame] | 218 | |
Chris Lattner | 732b223 | 2008-07-12 01:15:53 +0000 | [diff] [blame] | 219 | bool VisitCastExpr(CastExpr* E) { |
Chris Lattner | 732b223 | 2008-07-12 01:15:53 +0000 | [diff] [blame] | 220 | return HandleCast(E->getLocStart(), E->getSubExpr(), E->getType()); |
Anders Carlsson | 650c92f | 2008-07-08 15:34:11 +0000 | [diff] [blame] | 221 | } |
Chris Lattner | fcee001 | 2008-07-11 21:24:13 +0000 | [diff] [blame] | 222 | bool VisitSizeOfAlignOfTypeExpr(const SizeOfAlignOfTypeExpr *E) { |
Chris Lattner | 54176fd | 2008-07-12 00:14:42 +0000 | [diff] [blame] | 223 | return EvaluateSizeAlignOf(E->isSizeOf(), E->getArgumentType(), |
| 224 | E->getType()); |
Chris Lattner | fcee001 | 2008-07-11 21:24:13 +0000 | [diff] [blame] | 225 | } |
Chris Lattner | 4c4867e | 2008-07-12 00:38:25 +0000 | [diff] [blame] | 226 | |
Chris Lattner | fcee001 | 2008-07-11 21:24:13 +0000 | [diff] [blame] | 227 | private: |
Chris Lattner | 732b223 | 2008-07-12 01:15:53 +0000 | [diff] [blame] | 228 | bool HandleCast(SourceLocation CastLoc, Expr *SubExpr, QualType DestType); |
Chris Lattner | fcee001 | 2008-07-11 21:24:13 +0000 | [diff] [blame] | 229 | bool EvaluateSizeAlignOf(bool isSizeOf, QualType SrcTy, QualType DstTy); |
Anders Carlsson | a25ae3d | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 230 | }; |
Chris Lattner | f5eeb05 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 231 | } // end anonymous namespace |
Anders Carlsson | 650c92f | 2008-07-08 15:34:11 +0000 | [diff] [blame] | 232 | |
Chris Lattner | 87eae5e | 2008-07-11 22:52:41 +0000 | [diff] [blame] | 233 | static bool EvaluateInteger(const Expr* E, APSInt &Result, EvalInfo &Info) { |
| 234 | return IntExprEvaluator(Info, Result).Visit(const_cast<Expr*>(E)); |
Anders Carlsson | 650c92f | 2008-07-08 15:34:11 +0000 | [diff] [blame] | 235 | } |
Anders Carlsson | 650c92f | 2008-07-08 15:34:11 +0000 | [diff] [blame] | 236 | |
Chris Lattner | 4c4867e | 2008-07-12 00:38:25 +0000 | [diff] [blame] | 237 | bool IntExprEvaluator::VisitDeclRefExpr(const DeclRefExpr *E) { |
| 238 | // Enums are integer constant exprs. |
| 239 | if (const EnumConstantDecl *D = dyn_cast<EnumConstantDecl>(E->getDecl())) { |
| 240 | Result = D->getInitVal(); |
| 241 | return true; |
| 242 | } |
| 243 | |
| 244 | // Otherwise, random variable references are not constants. |
| 245 | return Error(E->getLocStart(), diag::err_expr_not_constant); |
| 246 | } |
| 247 | |
Chris Lattner | a4d55d8 | 2008-10-06 06:40:35 +0000 | [diff] [blame^] | 248 | /// EvaluateBuiltinClassifyType - Evaluate __builtin_classify_type the same way |
| 249 | /// as GCC. |
| 250 | static int EvaluateBuiltinClassifyType(const CallExpr *E) { |
| 251 | // The following enum mimics the values returned by GCC. |
| 252 | enum gcc_type_class { |
| 253 | no_type_class = -1, |
| 254 | void_type_class, integer_type_class, char_type_class, |
| 255 | enumeral_type_class, boolean_type_class, |
| 256 | pointer_type_class, reference_type_class, offset_type_class, |
| 257 | real_type_class, complex_type_class, |
| 258 | function_type_class, method_type_class, |
| 259 | record_type_class, union_type_class, |
| 260 | array_type_class, string_type_class, |
| 261 | lang_type_class |
| 262 | }; |
| 263 | |
| 264 | // If no argument was supplied, default to "no_type_class". This isn't |
| 265 | // ideal, however it is what gcc does. |
| 266 | if (E->getNumArgs() == 0) |
| 267 | return no_type_class; |
| 268 | |
| 269 | QualType ArgTy = E->getArg(0)->getType(); |
| 270 | if (ArgTy->isVoidType()) |
| 271 | return void_type_class; |
| 272 | else if (ArgTy->isEnumeralType()) |
| 273 | return enumeral_type_class; |
| 274 | else if (ArgTy->isBooleanType()) |
| 275 | return boolean_type_class; |
| 276 | else if (ArgTy->isCharType()) |
| 277 | return string_type_class; // gcc doesn't appear to use char_type_class |
| 278 | else if (ArgTy->isIntegerType()) |
| 279 | return integer_type_class; |
| 280 | else if (ArgTy->isPointerType()) |
| 281 | return pointer_type_class; |
| 282 | else if (ArgTy->isReferenceType()) |
| 283 | return reference_type_class; |
| 284 | else if (ArgTy->isRealType()) |
| 285 | return real_type_class; |
| 286 | else if (ArgTy->isComplexType()) |
| 287 | return complex_type_class; |
| 288 | else if (ArgTy->isFunctionType()) |
| 289 | return function_type_class; |
| 290 | else if (ArgTy->isStructureType()) |
| 291 | return record_type_class; |
| 292 | else if (ArgTy->isUnionType()) |
| 293 | return union_type_class; |
| 294 | else if (ArgTy->isArrayType()) |
| 295 | return array_type_class; |
| 296 | else if (ArgTy->isUnionType()) |
| 297 | return union_type_class; |
| 298 | else // FIXME: offset_type_class, method_type_class, & lang_type_class? |
| 299 | assert(0 && "CallExpr::isBuiltinClassifyType(): unimplemented type"); |
| 300 | return -1; |
| 301 | } |
| 302 | |
Chris Lattner | 4c4867e | 2008-07-12 00:38:25 +0000 | [diff] [blame] | 303 | bool IntExprEvaluator::VisitCallExpr(const CallExpr *E) { |
| 304 | Result.zextOrTrunc(getIntTypeSizeInBits(E->getType())); |
Chris Lattner | 4c4867e | 2008-07-12 00:38:25 +0000 | [diff] [blame] | 305 | |
Chris Lattner | 019f4e8 | 2008-10-06 05:28:25 +0000 | [diff] [blame] | 306 | switch (E->isBuiltinCall()) { |
| 307 | default: |
| 308 | return Error(E->getLocStart(), diag::err_expr_not_constant); |
| 309 | case Builtin::BI__builtin_classify_type: |
Chris Lattner | a4d55d8 | 2008-10-06 06:40:35 +0000 | [diff] [blame^] | 310 | Result.setIsSigned(true); |
| 311 | Result = EvaluateBuiltinClassifyType(E); |
Chris Lattner | 019f4e8 | 2008-10-06 05:28:25 +0000 | [diff] [blame] | 312 | return true; |
| 313 | |
| 314 | case Builtin::BI__builtin_constant_p: { |
| 315 | // __builtin_constant_p always has one operand: it returns true if that |
| 316 | // operand can be folded, false otherwise. |
| 317 | APValue Res; |
| 318 | Result = E->getArg(0)->tryEvaluate(Res, Info.Ctx); |
| 319 | return true; |
| 320 | } |
| 321 | } |
Chris Lattner | 4c4867e | 2008-07-12 00:38:25 +0000 | [diff] [blame] | 322 | } |
Anders Carlsson | 650c92f | 2008-07-08 15:34:11 +0000 | [diff] [blame] | 323 | |
Chris Lattner | b542afe | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 324 | bool IntExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) { |
Anders Carlsson | a25ae3d | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 325 | // The LHS of a constant expr is always evaluated and needed. |
Anders Carlsson | a25ae3d | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 326 | llvm::APSInt RHS(32); |
Chris Lattner | 54176fd | 2008-07-12 00:14:42 +0000 | [diff] [blame] | 327 | if (!Visit(E->getLHS())) |
| 328 | return false; // error in subexpression. |
| 329 | |
| 330 | bool OldEval = Info.isEvaluated; |
| 331 | |
| 332 | // The short-circuiting &&/|| operators don't necessarily evaluate their |
| 333 | // RHS. Make sure to pass isEvaluated down correctly. |
| 334 | if ((E->getOpcode() == BinaryOperator::LAnd && Result == 0) || |
| 335 | (E->getOpcode() == BinaryOperator::LOr && Result != 0)) |
| 336 | Info.isEvaluated = false; |
Eli Friedman | d9f4bcd | 2008-07-27 05:46:18 +0000 | [diff] [blame] | 337 | |
| 338 | // FIXME: Handle pointer subtraction |
| 339 | |
| 340 | // FIXME Maybe we want to succeed even where we can't evaluate the |
| 341 | // right side of LAnd/LOr? |
| 342 | // For example, see http://llvm.org/bugs/show_bug.cgi?id=2525 |
Chris Lattner | 54176fd | 2008-07-12 00:14:42 +0000 | [diff] [blame] | 343 | if (!EvaluateInteger(E->getRHS(), RHS, Info)) |
Chris Lattner | b542afe | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 344 | return false; |
Chris Lattner | 54176fd | 2008-07-12 00:14:42 +0000 | [diff] [blame] | 345 | Info.isEvaluated = OldEval; |
Anders Carlsson | a25ae3d | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 346 | |
| 347 | switch (E->getOpcode()) { |
Chris Lattner | 54176fd | 2008-07-12 00:14:42 +0000 | [diff] [blame] | 348 | default: return Error(E->getOperatorLoc(), diag::err_expr_not_constant); |
| 349 | case BinaryOperator::Mul: Result *= RHS; return true; |
| 350 | case BinaryOperator::Add: Result += RHS; return true; |
| 351 | case BinaryOperator::Sub: Result -= RHS; return true; |
| 352 | case BinaryOperator::And: Result &= RHS; return true; |
| 353 | case BinaryOperator::Xor: Result ^= RHS; return true; |
| 354 | case BinaryOperator::Or: Result |= RHS; return true; |
Chris Lattner | 75a4881 | 2008-07-11 22:15:16 +0000 | [diff] [blame] | 355 | case BinaryOperator::Div: |
Chris Lattner | 54176fd | 2008-07-12 00:14:42 +0000 | [diff] [blame] | 356 | if (RHS == 0) |
| 357 | return Error(E->getOperatorLoc(), diag::err_expr_divide_by_zero); |
Chris Lattner | 75a4881 | 2008-07-11 22:15:16 +0000 | [diff] [blame] | 358 | Result /= RHS; |
Chris Lattner | 54176fd | 2008-07-12 00:14:42 +0000 | [diff] [blame] | 359 | return true; |
Chris Lattner | 75a4881 | 2008-07-11 22:15:16 +0000 | [diff] [blame] | 360 | case BinaryOperator::Rem: |
Chris Lattner | 54176fd | 2008-07-12 00:14:42 +0000 | [diff] [blame] | 361 | if (RHS == 0) |
| 362 | return Error(E->getOperatorLoc(), diag::err_expr_divide_by_zero); |
Chris Lattner | 75a4881 | 2008-07-11 22:15:16 +0000 | [diff] [blame] | 363 | Result %= RHS; |
Chris Lattner | 54176fd | 2008-07-12 00:14:42 +0000 | [diff] [blame] | 364 | return true; |
Anders Carlsson | a25ae3d | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 365 | case BinaryOperator::Shl: |
Chris Lattner | 54176fd | 2008-07-12 00:14:42 +0000 | [diff] [blame] | 366 | // FIXME: Warn about out of range shift amounts! |
Chris Lattner | b542afe | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 367 | Result <<= (unsigned)RHS.getLimitedValue(Result.getBitWidth()-1); |
Anders Carlsson | a25ae3d | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 368 | break; |
| 369 | case BinaryOperator::Shr: |
Chris Lattner | b542afe | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 370 | Result >>= (unsigned)RHS.getLimitedValue(Result.getBitWidth()-1); |
Anders Carlsson | a25ae3d | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 371 | break; |
Chris Lattner | b542afe | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 372 | |
Chris Lattner | ac7cb60 | 2008-07-11 19:29:32 +0000 | [diff] [blame] | 373 | case BinaryOperator::LT: |
| 374 | Result = Result < RHS; |
| 375 | Result.zextOrTrunc(getIntTypeSizeInBits(E->getType())); |
| 376 | break; |
| 377 | case BinaryOperator::GT: |
| 378 | Result = Result > RHS; |
| 379 | Result.zextOrTrunc(getIntTypeSizeInBits(E->getType())); |
| 380 | break; |
| 381 | case BinaryOperator::LE: |
| 382 | Result = Result <= RHS; |
| 383 | Result.zextOrTrunc(getIntTypeSizeInBits(E->getType())); |
| 384 | break; |
| 385 | case BinaryOperator::GE: |
| 386 | Result = Result >= RHS; |
| 387 | Result.zextOrTrunc(getIntTypeSizeInBits(E->getType())); |
| 388 | break; |
| 389 | case BinaryOperator::EQ: |
| 390 | Result = Result == RHS; |
| 391 | Result.zextOrTrunc(getIntTypeSizeInBits(E->getType())); |
| 392 | break; |
| 393 | case BinaryOperator::NE: |
| 394 | Result = Result != RHS; |
| 395 | Result.zextOrTrunc(getIntTypeSizeInBits(E->getType())); |
| 396 | break; |
Chris Lattner | 54176fd | 2008-07-12 00:14:42 +0000 | [diff] [blame] | 397 | case BinaryOperator::LAnd: |
| 398 | Result = Result != 0 && RHS != 0; |
| 399 | Result.zextOrTrunc(getIntTypeSizeInBits(E->getType())); |
| 400 | break; |
| 401 | case BinaryOperator::LOr: |
| 402 | Result = Result != 0 || RHS != 0; |
| 403 | Result.zextOrTrunc(getIntTypeSizeInBits(E->getType())); |
| 404 | break; |
| 405 | |
Anders Carlsson | 06a3675 | 2008-07-08 05:49:43 +0000 | [diff] [blame] | 406 | |
Anders Carlsson | a25ae3d | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 407 | case BinaryOperator::Comma: |
Chris Lattner | 54176fd | 2008-07-12 00:14:42 +0000 | [diff] [blame] | 408 | // Result of the comma is just the result of the RHS. |
| 409 | Result = RHS; |
| 410 | |
Anders Carlsson | a25ae3d | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 411 | // C99 6.6p3: "shall not contain assignment, ..., or comma operators, |
| 412 | // *except* when they are contained within a subexpression that is not |
| 413 | // evaluated". Note that Assignment can never happen due to constraints |
| 414 | // on the LHS subexpr, so we don't need to check it here. |
Chris Lattner | 54176fd | 2008-07-12 00:14:42 +0000 | [diff] [blame] | 415 | if (!Info.isEvaluated) |
| 416 | return true; |
| 417 | |
| 418 | // If the value is evaluated, we can accept it as an extension. |
| 419 | return Extension(E->getOperatorLoc(), diag::ext_comma_in_constant_expr); |
Anders Carlsson | a25ae3d | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 420 | } |
| 421 | |
| 422 | Result.setIsUnsigned(E->getType()->isUnsignedIntegerType()); |
Chris Lattner | b542afe | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 423 | return true; |
Anders Carlsson | a25ae3d | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 424 | } |
| 425 | |
Chris Lattner | fcee001 | 2008-07-11 21:24:13 +0000 | [diff] [blame] | 426 | /// EvaluateSizeAlignOf - Evaluate sizeof(SrcTy) or alignof(SrcTy) with a result |
| 427 | /// as a DstTy type. |
| 428 | bool IntExprEvaluator::EvaluateSizeAlignOf(bool isSizeOf, QualType SrcTy, |
| 429 | QualType DstTy) { |
| 430 | // Return the result in the right width. |
| 431 | Result.zextOrTrunc(getIntTypeSizeInBits(DstTy)); |
| 432 | Result.setIsUnsigned(DstTy->isUnsignedIntegerType()); |
| 433 | |
| 434 | // sizeof(void) and __alignof__(void) = 1 as a gcc extension. |
| 435 | if (SrcTy->isVoidType()) |
| 436 | Result = 1; |
| 437 | |
| 438 | // sizeof(vla) is not a constantexpr: C99 6.5.3.4p2. |
| 439 | if (!SrcTy->isConstantSizeType()) { |
| 440 | // FIXME: Should we attempt to evaluate this? |
| 441 | return false; |
| 442 | } |
| 443 | |
| 444 | // GCC extension: sizeof(function) = 1. |
| 445 | if (SrcTy->isFunctionType()) { |
| 446 | // FIXME: AlignOf shouldn't be unconditionally 4! |
| 447 | Result = isSizeOf ? 1 : 4; |
| 448 | return true; |
| 449 | } |
| 450 | |
| 451 | // Get information about the size or align. |
Chris Lattner | 87eae5e | 2008-07-11 22:52:41 +0000 | [diff] [blame] | 452 | unsigned CharSize = Info.Ctx.Target.getCharWidth(); |
Chris Lattner | fcee001 | 2008-07-11 21:24:13 +0000 | [diff] [blame] | 453 | if (isSizeOf) |
| 454 | Result = getIntTypeSizeInBits(SrcTy) / CharSize; |
| 455 | else |
Chris Lattner | 87eae5e | 2008-07-11 22:52:41 +0000 | [diff] [blame] | 456 | Result = Info.Ctx.getTypeAlign(SrcTy) / CharSize; |
Chris Lattner | fcee001 | 2008-07-11 21:24:13 +0000 | [diff] [blame] | 457 | return true; |
| 458 | } |
| 459 | |
Chris Lattner | b542afe | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 460 | bool IntExprEvaluator::VisitUnaryOperator(const UnaryOperator *E) { |
Chris Lattner | 4c4867e | 2008-07-12 00:38:25 +0000 | [diff] [blame] | 461 | // Special case unary operators that do not need their subexpression |
| 462 | // evaluated. offsetof/sizeof/alignof are all special. |
Chris Lattner | 75a4881 | 2008-07-11 22:15:16 +0000 | [diff] [blame] | 463 | if (E->isOffsetOfOp()) { |
Chris Lattner | 4c4867e | 2008-07-12 00:38:25 +0000 | [diff] [blame] | 464 | Result.zextOrTrunc(getIntTypeSizeInBits(E->getType())); |
Chris Lattner | 87eae5e | 2008-07-11 22:52:41 +0000 | [diff] [blame] | 465 | Result = E->evaluateOffsetOf(Info.Ctx); |
Chris Lattner | 75a4881 | 2008-07-11 22:15:16 +0000 | [diff] [blame] | 466 | Result.setIsUnsigned(E->getType()->isUnsignedIntegerType()); |
| 467 | return true; |
| 468 | } |
| 469 | |
| 470 | if (E->isSizeOfAlignOfOp()) |
Chris Lattner | fcee001 | 2008-07-11 21:24:13 +0000 | [diff] [blame] | 471 | return EvaluateSizeAlignOf(E->getOpcode() == UnaryOperator::SizeOf, |
| 472 | E->getSubExpr()->getType(), E->getType()); |
Chris Lattner | 75a4881 | 2008-07-11 22:15:16 +0000 | [diff] [blame] | 473 | |
Chris Lattner | 87eae5e | 2008-07-11 22:52:41 +0000 | [diff] [blame] | 474 | // Get the operand value into 'Result'. |
| 475 | if (!Visit(E->getSubExpr())) |
Chris Lattner | 75a4881 | 2008-07-11 22:15:16 +0000 | [diff] [blame] | 476 | return false; |
Anders Carlsson | a25ae3d | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 477 | |
Chris Lattner | 75a4881 | 2008-07-11 22:15:16 +0000 | [diff] [blame] | 478 | switch (E->getOpcode()) { |
Chris Lattner | 4c4867e | 2008-07-12 00:38:25 +0000 | [diff] [blame] | 479 | default: |
Chris Lattner | 75a4881 | 2008-07-11 22:15:16 +0000 | [diff] [blame] | 480 | // Address, indirect, pre/post inc/dec, etc are not valid constant exprs. |
| 481 | // See C99 6.6p3. |
Chris Lattner | 4c4867e | 2008-07-12 00:38:25 +0000 | [diff] [blame] | 482 | return Error(E->getOperatorLoc(), diag::err_expr_not_constant); |
Chris Lattner | 75a4881 | 2008-07-11 22:15:16 +0000 | [diff] [blame] | 483 | case UnaryOperator::LNot: { |
| 484 | bool Val = Result == 0; |
| 485 | Result.zextOrTrunc(getIntTypeSizeInBits(E->getType())); |
| 486 | Result = Val; |
| 487 | break; |
| 488 | } |
| 489 | case UnaryOperator::Extension: |
Chris Lattner | 4c4867e | 2008-07-12 00:38:25 +0000 | [diff] [blame] | 490 | // FIXME: Should extension allow i-c-e extension expressions in its scope? |
| 491 | // If so, we could clear the diagnostic ID. |
Chris Lattner | 75a4881 | 2008-07-11 22:15:16 +0000 | [diff] [blame] | 492 | case UnaryOperator::Plus: |
Chris Lattner | 4c4867e | 2008-07-12 00:38:25 +0000 | [diff] [blame] | 493 | // The result is always just the subexpr. |
Chris Lattner | 75a4881 | 2008-07-11 22:15:16 +0000 | [diff] [blame] | 494 | break; |
| 495 | case UnaryOperator::Minus: |
| 496 | Result = -Result; |
| 497 | break; |
| 498 | case UnaryOperator::Not: |
| 499 | Result = ~Result; |
| 500 | break; |
Anders Carlsson | a25ae3d | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 501 | } |
| 502 | |
| 503 | Result.setIsUnsigned(E->getType()->isUnsignedIntegerType()); |
Chris Lattner | b542afe | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 504 | return true; |
Anders Carlsson | a25ae3d | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 505 | } |
| 506 | |
Chris Lattner | 732b223 | 2008-07-12 01:15:53 +0000 | [diff] [blame] | 507 | /// HandleCast - This is used to evaluate implicit or explicit casts where the |
| 508 | /// result type is integer. |
| 509 | bool IntExprEvaluator::HandleCast(SourceLocation CastLoc, |
| 510 | Expr *SubExpr, QualType DestType) { |
Chris Lattner | 7a76778 | 2008-07-11 19:24:49 +0000 | [diff] [blame] | 511 | unsigned DestWidth = getIntTypeSizeInBits(DestType); |
Anders Carlsson | a25ae3d | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 512 | |
| 513 | // Handle simple integer->integer casts. |
| 514 | if (SubExpr->getType()->isIntegerType()) { |
Chris Lattner | 732b223 | 2008-07-12 01:15:53 +0000 | [diff] [blame] | 515 | if (!Visit(SubExpr)) |
Chris Lattner | b542afe | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 516 | return false; |
Anders Carlsson | a25ae3d | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 517 | |
| 518 | // Figure out if this is a truncate, extend or noop cast. |
| 519 | // If the input is signed, do a sign extend, noop, or truncate. |
| 520 | if (DestType->isBooleanType()) { |
| 521 | // Conversion to bool compares against zero. |
| 522 | Result = Result != 0; |
| 523 | Result.zextOrTrunc(DestWidth); |
Chris Lattner | 7a76778 | 2008-07-11 19:24:49 +0000 | [diff] [blame] | 524 | } else |
Anders Carlsson | a25ae3d | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 525 | Result.extOrTrunc(DestWidth); |
Chris Lattner | 732b223 | 2008-07-12 01:15:53 +0000 | [diff] [blame] | 526 | Result.setIsUnsigned(DestType->isUnsignedIntegerType()); |
| 527 | return true; |
| 528 | } |
| 529 | |
| 530 | // FIXME: Clean this up! |
| 531 | if (SubExpr->getType()->isPointerType()) { |
Anders Carlsson | a25ae3d | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 532 | APValue LV; |
Chris Lattner | 87eae5e | 2008-07-11 22:52:41 +0000 | [diff] [blame] | 533 | if (!EvaluatePointer(SubExpr, LV, Info)) |
Chris Lattner | b542afe | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 534 | return false; |
Anders Carlsson | a25ae3d | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 535 | if (LV.getLValueBase()) |
Chris Lattner | b542afe | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 536 | return false; |
Anders Carlsson | 06a3675 | 2008-07-08 05:49:43 +0000 | [diff] [blame] | 537 | |
Anders Carlsson | 559e56b | 2008-07-08 16:49:00 +0000 | [diff] [blame] | 538 | Result.extOrTrunc(DestWidth); |
| 539 | Result = LV.getLValueOffset(); |
Chris Lattner | 732b223 | 2008-07-12 01:15:53 +0000 | [diff] [blame] | 540 | Result.setIsUnsigned(DestType->isUnsignedIntegerType()); |
| 541 | return true; |
Anders Carlsson | 2bad168 | 2008-07-08 14:30:00 +0000 | [diff] [blame] | 542 | } |
| 543 | |
Chris Lattner | 732b223 | 2008-07-12 01:15:53 +0000 | [diff] [blame] | 544 | if (!SubExpr->getType()->isRealFloatingType()) |
| 545 | return Error(CastLoc, diag::err_expr_not_constant); |
| 546 | |
Eli Friedman | d8bfe7f | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 547 | APFloat F(0.0); |
| 548 | if (!EvaluateFloat(SubExpr, F, Info)) |
Chris Lattner | 732b223 | 2008-07-12 01:15:53 +0000 | [diff] [blame] | 549 | return Error(CastLoc, diag::err_expr_not_constant); |
Eli Friedman | d8bfe7f | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 550 | |
Chris Lattner | 732b223 | 2008-07-12 01:15:53 +0000 | [diff] [blame] | 551 | // If the destination is boolean, compare against zero. |
| 552 | if (DestType->isBooleanType()) { |
Eli Friedman | d8bfe7f | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 553 | Result = !F.isZero(); |
Chris Lattner | 732b223 | 2008-07-12 01:15:53 +0000 | [diff] [blame] | 554 | Result.zextOrTrunc(DestWidth); |
| 555 | Result.setIsUnsigned(DestType->isUnsignedIntegerType()); |
| 556 | return true; |
| 557 | } |
| 558 | |
| 559 | // Determine whether we are converting to unsigned or signed. |
| 560 | bool DestSigned = DestType->isSignedIntegerType(); |
| 561 | |
| 562 | // FIXME: Warning for overflow. |
| 563 | uint64_t Space[4]; |
Eli Friedman | d8bfe7f | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 564 | (void)F.convertToInteger(Space, DestWidth, DestSigned, |
| 565 | llvm::APFloat::rmTowardZero); |
Chris Lattner | 732b223 | 2008-07-12 01:15:53 +0000 | [diff] [blame] | 566 | Result = llvm::APInt(DestWidth, 4, Space); |
| 567 | Result.setIsUnsigned(!DestSigned); |
Chris Lattner | b542afe | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 568 | return true; |
Anders Carlsson | a25ae3d | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 569 | } |
Anders Carlsson | 2bad168 | 2008-07-08 14:30:00 +0000 | [diff] [blame] | 570 | |
Chris Lattner | f5eeb05 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 571 | //===----------------------------------------------------------------------===// |
Eli Friedman | d8bfe7f | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 572 | // Float Evaluation |
| 573 | //===----------------------------------------------------------------------===// |
| 574 | |
| 575 | namespace { |
| 576 | class VISIBILITY_HIDDEN FloatExprEvaluator |
| 577 | : public StmtVisitor<FloatExprEvaluator, bool> { |
| 578 | EvalInfo &Info; |
| 579 | APFloat &Result; |
| 580 | public: |
| 581 | FloatExprEvaluator(EvalInfo &info, APFloat &result) |
| 582 | : Info(info), Result(result) {} |
| 583 | |
| 584 | bool VisitStmt(Stmt *S) { |
| 585 | return false; |
| 586 | } |
| 587 | |
| 588 | bool VisitParenExpr(ParenExpr *E) { return Visit(E->getSubExpr()); } |
Chris Lattner | 019f4e8 | 2008-10-06 05:28:25 +0000 | [diff] [blame] | 589 | bool VisitCallExpr(const CallExpr *E); |
Eli Friedman | d8bfe7f | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 590 | |
| 591 | bool VisitBinaryOperator(const BinaryOperator *E); |
| 592 | bool VisitFloatingLiteral(const FloatingLiteral *E); |
| 593 | }; |
| 594 | } // end anonymous namespace |
| 595 | |
| 596 | static bool EvaluateFloat(const Expr* E, APFloat& Result, EvalInfo &Info) { |
| 597 | return FloatExprEvaluator(Info, Result).Visit(const_cast<Expr*>(E)); |
| 598 | } |
| 599 | |
Chris Lattner | 019f4e8 | 2008-10-06 05:28:25 +0000 | [diff] [blame] | 600 | bool FloatExprEvaluator::VisitCallExpr(const CallExpr *E) { |
Chris Lattner | 34a74ab | 2008-10-06 05:53:16 +0000 | [diff] [blame] | 601 | const llvm::fltSemantics &Sem = |
| 602 | Info.Ctx.getFloatTypeSemantics(E->getType()); |
Chris Lattner | 019f4e8 | 2008-10-06 05:28:25 +0000 | [diff] [blame] | 603 | |
| 604 | switch (E->isBuiltinCall()) { |
Chris Lattner | 34a74ab | 2008-10-06 05:53:16 +0000 | [diff] [blame] | 605 | default: return false; |
Chris Lattner | 019f4e8 | 2008-10-06 05:28:25 +0000 | [diff] [blame] | 606 | case Builtin::BI__builtin_huge_val: |
| 607 | case Builtin::BI__builtin_huge_valf: |
| 608 | case Builtin::BI__builtin_huge_vall: |
| 609 | case Builtin::BI__builtin_inf: |
| 610 | case Builtin::BI__builtin_inff: |
| 611 | case Builtin::BI__builtin_infl: |
Chris Lattner | 34a74ab | 2008-10-06 05:53:16 +0000 | [diff] [blame] | 612 | Result = llvm::APFloat::getInf(Sem); |
| 613 | return true; |
Chris Lattner | 9e62171 | 2008-10-06 06:31:58 +0000 | [diff] [blame] | 614 | |
| 615 | case Builtin::BI__builtin_nan: |
| 616 | case Builtin::BI__builtin_nanf: |
| 617 | case Builtin::BI__builtin_nanl: |
| 618 | // If this is __builtin_nan("") turn this into a simple nan, otherwise we |
| 619 | // can't constant fold it. |
| 620 | if (const StringLiteral *S = |
| 621 | dyn_cast<StringLiteral>(E->getArg(0)->IgnoreParenCasts())) { |
| 622 | if (!S->isWide() && S->getByteLength() == 0) { // empty string. |
| 623 | Result = llvm::APFloat::getNaN(Sem); |
| 624 | return true; |
| 625 | } |
| 626 | } |
| 627 | return false; |
Chris Lattner | 019f4e8 | 2008-10-06 05:28:25 +0000 | [diff] [blame] | 628 | } |
| 629 | } |
| 630 | |
| 631 | |
Eli Friedman | d8bfe7f | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 632 | bool FloatExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) { |
| 633 | // FIXME: Diagnostics? I really don't understand how the warnings |
| 634 | // and errors are supposed to work. |
| 635 | APFloat LHS(0.0), RHS(0.0); |
| 636 | if (!EvaluateFloat(E->getLHS(), Result, Info)) |
| 637 | return false; |
| 638 | if (!EvaluateFloat(E->getRHS(), RHS, Info)) |
| 639 | return false; |
| 640 | |
| 641 | switch (E->getOpcode()) { |
| 642 | default: return false; |
| 643 | case BinaryOperator::Mul: |
| 644 | Result.multiply(RHS, APFloat::rmNearestTiesToEven); |
| 645 | return true; |
| 646 | case BinaryOperator::Add: |
| 647 | Result.add(RHS, APFloat::rmNearestTiesToEven); |
| 648 | return true; |
| 649 | case BinaryOperator::Sub: |
| 650 | Result.subtract(RHS, APFloat::rmNearestTiesToEven); |
| 651 | return true; |
| 652 | case BinaryOperator::Div: |
| 653 | Result.divide(RHS, APFloat::rmNearestTiesToEven); |
| 654 | return true; |
| 655 | case BinaryOperator::Rem: |
| 656 | Result.mod(RHS, APFloat::rmNearestTiesToEven); |
| 657 | return true; |
| 658 | } |
| 659 | } |
| 660 | |
| 661 | bool FloatExprEvaluator::VisitFloatingLiteral(const FloatingLiteral *E) { |
| 662 | Result = E->getValue(); |
| 663 | return true; |
| 664 | } |
| 665 | |
| 666 | //===----------------------------------------------------------------------===// |
Chris Lattner | f5eeb05 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 667 | // Top level TryEvaluate. |
| 668 | //===----------------------------------------------------------------------===// |
| 669 | |
Chris Lattner | 019f4e8 | 2008-10-06 05:28:25 +0000 | [diff] [blame] | 670 | /// tryEvaluate - Return true if this is a constant which we can fold using |
| 671 | /// any crazy technique (that has nothing to do with language standards) that |
| 672 | /// we want to. If this function returns true, it returns the folded constant |
| 673 | /// in Result. |
Chris Lattner | b542afe | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 674 | bool Expr::tryEvaluate(APValue &Result, ASTContext &Ctx) const { |
Chris Lattner | 87eae5e | 2008-07-11 22:52:41 +0000 | [diff] [blame] | 675 | EvalInfo Info(Ctx); |
Anders Carlsson | 06a3675 | 2008-07-08 05:49:43 +0000 | [diff] [blame] | 676 | if (getType()->isIntegerType()) { |
Eli Friedman | d8bfe7f | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 677 | llvm::APSInt sInt(32); |
Chris Lattner | 87eae5e | 2008-07-11 22:52:41 +0000 | [diff] [blame] | 678 | if (EvaluateInteger(this, sInt, Info)) { |
Anders Carlsson | 06a3675 | 2008-07-08 05:49:43 +0000 | [diff] [blame] | 679 | Result = APValue(sInt); |
| 680 | return true; |
| 681 | } |
Eli Friedman | d8bfe7f | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 682 | } else if (getType()->isPointerType()) { |
| 683 | if (EvaluatePointer(this, Result, Info)) { |
| 684 | return true; |
| 685 | } |
| 686 | } else if (getType()->isRealFloatingType()) { |
| 687 | llvm::APFloat f(0.0); |
| 688 | if (EvaluateFloat(this, f, Info)) { |
| 689 | Result = APValue(f); |
| 690 | return true; |
| 691 | } |
| 692 | } |
Anders Carlsson | 165a70f | 2008-08-10 17:03:01 +0000 | [diff] [blame] | 693 | |
Anders Carlsson | c44eec6 | 2008-07-03 04:20:39 +0000 | [diff] [blame] | 694 | return false; |
| 695 | } |