John Stiles | dc8ec31 | 2021-01-11 11:05:21 -0500 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2020 Google LLC |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
| 6 | */ |
| 7 | |
| 8 | #ifndef SKSL_CONSTANT_FOLDER |
| 9 | #define SKSL_CONSTANT_FOLDER |
| 10 | |
| 11 | #include <memory> |
| 12 | |
Ethan Nicholas | c0f9815 | 2021-02-05 16:21:10 -0500 | [diff] [blame] | 13 | #include "src/sksl/SkSLDefines.h" |
John Stiles | 4599050 | 2021-02-16 10:55:27 -0500 | [diff] [blame] | 14 | #include "src/sksl/SkSLOperators.h" |
John Stiles | dc8ec31 | 2021-01-11 11:05:21 -0500 | [diff] [blame] | 15 | |
| 16 | namespace SkSL { |
| 17 | |
| 18 | class Context; |
| 19 | class ErrorReporter; |
| 20 | class Expression; |
| 21 | |
| 22 | /** |
| 23 | * Performs constant folding on IR expressions. This simplifies expressions containing |
| 24 | * compile-time constants, such as replacing `IntLiteral(2) + IntLiteral(2)` with `IntLiteral(4)`. |
| 25 | */ |
| 26 | class ConstantFolder { |
| 27 | public: |
Ethan Nicholas | c0f9815 | 2021-02-05 16:21:10 -0500 | [diff] [blame] | 28 | /** |
| 29 | * If value is an int literal or const int variable with a known value, returns true and stores |
| 30 | * the value in out. Otherwise returns false. |
| 31 | */ |
| 32 | static bool GetConstantInt(const Expression& value, SKSL_INT* out); |
| 33 | |
| 34 | /** |
| 35 | * If value is a float literal or const float variable with a known value, returns true and |
| 36 | * stores the value in out. Otherwise returns false. |
| 37 | */ |
| 38 | static bool GetConstantFloat(const Expression& value, SKSL_FLOAT* out); |
| 39 | |
| 40 | /** |
John Stiles | e80e169 | 2021-03-02 17:02:25 -0500 | [diff] [blame^] | 41 | * If the expression is a const variable with a known compile-time-constant value, returns that |
| 42 | * value. If not, returns the original expression as-is. |
| 43 | */ |
| 44 | static const Expression* GetConstantValueForVariable(const Expression& value); |
| 45 | |
| 46 | /** |
Ethan Nicholas | c0f9815 | 2021-02-05 16:21:10 -0500 | [diff] [blame] | 47 | * Reports an error and returns true if op is a division / mod operator and right is zero or |
| 48 | * contains a zero element. |
| 49 | */ |
John Stiles | 4599050 | 2021-02-16 10:55:27 -0500 | [diff] [blame] | 50 | static bool ErrorOnDivideByZero(const Context& context, int offset, Operator op, |
Ethan Nicholas | c0f9815 | 2021-02-05 16:21:10 -0500 | [diff] [blame] | 51 | const Expression& right); |
| 52 | |
John Stiles | dc8ec31 | 2021-01-11 11:05:21 -0500 | [diff] [blame] | 53 | /** Simplifies the binary expression `left OP right`. Returns null if it can't be simplified. */ |
| 54 | static std::unique_ptr<Expression> Simplify(const Context& context, |
Ethan Nicholas | c0f9815 | 2021-02-05 16:21:10 -0500 | [diff] [blame] | 55 | int offset, |
John Stiles | dc8ec31 | 2021-01-11 11:05:21 -0500 | [diff] [blame] | 56 | const Expression& left, |
John Stiles | 4599050 | 2021-02-16 10:55:27 -0500 | [diff] [blame] | 57 | Operator op, |
John Stiles | dc8ec31 | 2021-01-11 11:05:21 -0500 | [diff] [blame] | 58 | const Expression& right); |
| 59 | }; |
| 60 | |
| 61 | } // namespace SkSL |
| 62 | |
| 63 | #endif // SKSL_CONSTANT_FOLDER |