ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016 Google Inc. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
| 6 | */ |
Ethan Nicholas | 86a4340 | 2017-01-19 13:32:00 -0500 | [diff] [blame] | 7 | |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 8 | #ifndef SKSL_BINARYEXPRESSION |
| 9 | #define SKSL_BINARYEXPRESSION |
| 10 | |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 11 | #include "src/sksl/SkSLCompiler.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 12 | #include "src/sksl/SkSLIRGenerator.h" |
| 13 | #include "src/sksl/SkSLLexer.h" |
| 14 | #include "src/sksl/ir/SkSLExpression.h" |
Ethan Nicholas | 4fadce4 | 2020-07-30 13:29:30 -0400 | [diff] [blame] | 15 | #include "src/sksl/ir/SkSLFieldAccess.h" |
| 16 | #include "src/sksl/ir/SkSLIndexExpression.h" |
| 17 | #include "src/sksl/ir/SkSLSwizzle.h" |
| 18 | #include "src/sksl/ir/SkSLTernaryExpression.h" |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 19 | |
| 20 | namespace SkSL { |
| 21 | |
Ethan Nicholas | b61c3a9 | 2020-09-16 18:33:21 +0000 | [diff] [blame^] | 22 | static inline bool check_ref(Expression* expr) { |
| 23 | switch (expr->kind()) { |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 24 | case Expression::Kind::kExternalValue: |
Ethan Nicholas | 4fadce4 | 2020-07-30 13:29:30 -0400 | [diff] [blame] | 25 | return true; |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 26 | case Expression::Kind::kFieldAccess: |
Ethan Nicholas | b61c3a9 | 2020-09-16 18:33:21 +0000 | [diff] [blame^] | 27 | return check_ref(((FieldAccess*) expr)->fBase.get()); |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 28 | case Expression::Kind::kIndex: |
Ethan Nicholas | b61c3a9 | 2020-09-16 18:33:21 +0000 | [diff] [blame^] | 29 | return check_ref(((IndexExpression*) expr)->fBase.get()); |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 30 | case Expression::Kind::kSwizzle: |
Ethan Nicholas | b61c3a9 | 2020-09-16 18:33:21 +0000 | [diff] [blame^] | 31 | return check_ref(((Swizzle*) expr)->fBase.get()); |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 32 | case Expression::Kind::kTernary: { |
Ethan Nicholas | b61c3a9 | 2020-09-16 18:33:21 +0000 | [diff] [blame^] | 33 | TernaryExpression* t = (TernaryExpression*) expr; |
| 34 | return check_ref(t->fIfTrue.get()) && check_ref(t->fIfFalse.get()); |
Ethan Nicholas | 4fadce4 | 2020-07-30 13:29:30 -0400 | [diff] [blame] | 35 | } |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 36 | case Expression::Kind::kVariableReference: { |
Ethan Nicholas | b61c3a9 | 2020-09-16 18:33:21 +0000 | [diff] [blame^] | 37 | VariableReference* ref = (VariableReference*) expr; |
| 38 | return ref->fRefKind == VariableReference::kWrite_RefKind || |
| 39 | ref->fRefKind == VariableReference::kReadWrite_RefKind; |
Ethan Nicholas | 4fadce4 | 2020-07-30 13:29:30 -0400 | [diff] [blame] | 40 | } |
| 41 | default: |
| 42 | return false; |
| 43 | } |
| 44 | } |
| 45 | |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 46 | /** |
Ethan Nicholas | 86a4340 | 2017-01-19 13:32:00 -0500 | [diff] [blame] | 47 | * A binary operation. |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 48 | */ |
| 49 | struct BinaryExpression : public Expression { |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 50 | static constexpr Kind kExpressionKind = Kind::kBinary; |
John Stiles | 81365af | 2020-08-18 09:24:00 -0400 | [diff] [blame] | 51 | |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 52 | BinaryExpression(int offset, std::unique_ptr<Expression> left, Token::Kind op, |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 53 | std::unique_ptr<Expression> right, const Type* type) |
Ethan Nicholas | b61c3a9 | 2020-09-16 18:33:21 +0000 | [diff] [blame^] | 54 | : INHERITED(offset, kExpressionKind, type) |
| 55 | , fLeft(std::move(left)) |
| 56 | , fOperator(op) |
| 57 | , fRight(std::move(right)) { |
Ethan Nicholas | 4fadce4 | 2020-07-30 13:29:30 -0400 | [diff] [blame] | 58 | // If we are assigning to a VariableReference, ensure that it is set to Write or ReadWrite |
Ethan Nicholas | b61c3a9 | 2020-09-16 18:33:21 +0000 | [diff] [blame^] | 59 | SkASSERT(!Compiler::IsAssignment(op) || check_ref(fLeft.get())); |
Ethan Nicholas | 4fadce4 | 2020-07-30 13:29:30 -0400 | [diff] [blame] | 60 | } |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 61 | |
Ethan Nicholas | 5843012 | 2020-04-14 09:54:02 -0400 | [diff] [blame] | 62 | bool isConstantOrUniform() const override { |
Ethan Nicholas | b61c3a9 | 2020-09-16 18:33:21 +0000 | [diff] [blame^] | 63 | return fLeft->isConstantOrUniform() && fRight->isConstantOrUniform(); |
Ethan Nicholas | 5843012 | 2020-04-14 09:54:02 -0400 | [diff] [blame] | 64 | } |
| 65 | |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 66 | std::unique_ptr<Expression> constantPropagate(const IRGenerator& irGenerator, |
| 67 | const DefinitionMap& definitions) override { |
Ethan Nicholas | b61c3a9 | 2020-09-16 18:33:21 +0000 | [diff] [blame^] | 68 | return irGenerator.constantFold(*fLeft, |
| 69 | fOperator, |
| 70 | *fRight); |
Ethan Nicholas | 86a4340 | 2017-01-19 13:32:00 -0500 | [diff] [blame] | 71 | } |
| 72 | |
Ethan Nicholas | 2a099da | 2020-01-02 14:40:54 -0500 | [diff] [blame] | 73 | bool hasProperty(Property property) const override { |
Ethan Nicholas | b61c3a9 | 2020-09-16 18:33:21 +0000 | [diff] [blame^] | 74 | if (property == Property::kSideEffects && Compiler::IsAssignment(fOperator)) { |
Ethan Nicholas | 2a099da | 2020-01-02 14:40:54 -0500 | [diff] [blame] | 75 | return true; |
| 76 | } |
Ethan Nicholas | b61c3a9 | 2020-09-16 18:33:21 +0000 | [diff] [blame^] | 77 | return fLeft->hasProperty(property) || fRight->hasProperty(property); |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 78 | } |
| 79 | |
Ethan Nicholas | 0054311 | 2018-07-31 09:44:36 -0400 | [diff] [blame] | 80 | std::unique_ptr<Expression> clone() const override { |
Ethan Nicholas | b61c3a9 | 2020-09-16 18:33:21 +0000 | [diff] [blame^] | 81 | return std::unique_ptr<Expression>(new BinaryExpression(fOffset, fLeft->clone(), fOperator, |
| 82 | fRight->clone(), &this->type())); |
Ethan Nicholas | 0054311 | 2018-07-31 09:44:36 -0400 | [diff] [blame] | 83 | } |
| 84 | |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 85 | String description() const override { |
Ethan Nicholas | b61c3a9 | 2020-09-16 18:33:21 +0000 | [diff] [blame^] | 86 | return "(" + fLeft->description() + " " + Compiler::OperatorName(fOperator) + " " + |
| 87 | fRight->description() + ")"; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 88 | } |
| 89 | |
Ethan Nicholas | b61c3a9 | 2020-09-16 18:33:21 +0000 | [diff] [blame^] | 90 | std::unique_ptr<Expression> fLeft; |
| 91 | const Token::Kind fOperator; |
| 92 | std::unique_ptr<Expression> fRight; |
| 93 | |
John Stiles | 7571f9e | 2020-09-02 22:42:33 -0400 | [diff] [blame] | 94 | using INHERITED = Expression; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 95 | }; |
| 96 | |
John Stiles | a6841be | 2020-08-06 14:11:56 -0400 | [diff] [blame] | 97 | } // namespace SkSL |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 98 | |
| 99 | #endif |