dan sinclair | 53f646c | 2022-11-30 16:36:46 +0000 | [diff] [blame] | 1 | // Copyright 2022 The Tint Authors. |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | #ifndef SRC_TINT_IR_BINARY_H_ |
| 16 | #define SRC_TINT_IR_BINARY_H_ |
| 17 | |
James Price | ae8a9f9 | 2023-06-06 19:37:51 +0000 | [diff] [blame] | 18 | #include "src/tint/ir/operand_instruction.h" |
dan sinclair | 12fa303 | 2023-04-19 23:52:33 +0000 | [diff] [blame] | 19 | #include "src/tint/utils/castable.h" |
dan sinclair | 53f646c | 2022-11-30 16:36:46 +0000 | [diff] [blame] | 20 | |
| 21 | namespace tint::ir { |
| 22 | |
James Price | ae8a9f9 | 2023-06-06 19:37:51 +0000 | [diff] [blame] | 23 | /// A binary instruction in the IR. |
| 24 | class Binary : public utils::Castable<Binary, OperandInstruction<2>> { |
dan sinclair | 53f646c | 2022-11-30 16:36:46 +0000 | [diff] [blame] | 25 | public: |
| 26 | /// The kind of instruction. |
| 27 | enum class Kind { |
| 28 | kAdd, |
| 29 | kSubtract, |
| 30 | kMultiply, |
| 31 | kDivide, |
| 32 | kModulo, |
| 33 | |
| 34 | kAnd, |
| 35 | kOr, |
| 36 | kXor, |
| 37 | |
dan sinclair | 53f646c | 2022-11-30 16:36:46 +0000 | [diff] [blame] | 38 | kEqual, |
| 39 | kNotEqual, |
| 40 | kLessThan, |
| 41 | kGreaterThan, |
| 42 | kLessThanEqual, |
| 43 | kGreaterThanEqual, |
| 44 | |
| 45 | kShiftLeft, |
| 46 | kShiftRight |
| 47 | }; |
| 48 | |
| 49 | /// Constructor |
| 50 | /// @param kind the kind of binary instruction |
dan sinclair | f00679f | 2023-04-27 14:00:06 +0000 | [diff] [blame] | 51 | /// @param type the result type |
dan sinclair | 53f646c | 2022-11-30 16:36:46 +0000 | [diff] [blame] | 52 | /// @param lhs the lhs of the instruction |
| 53 | /// @param rhs the rhs of the instruction |
dan sinclair | 24cb811 | 2023-05-18 22:16:08 +0000 | [diff] [blame] | 54 | Binary(enum Kind kind, const type::Type* type, Value* lhs, Value* rhs); |
dan sinclair | 53f646c | 2022-11-30 16:36:46 +0000 | [diff] [blame] | 55 | ~Binary() override; |
| 56 | |
dan sinclair | 24cb811 | 2023-05-18 22:16:08 +0000 | [diff] [blame] | 57 | /// @returns the kind of the binary instruction |
dan sinclair | a5268df | 2023-06-08 21:39:53 +0000 | [diff] [blame] | 58 | enum Kind Kind() { return kind_; } |
dan sinclair | 24cb811 | 2023-05-18 22:16:08 +0000 | [diff] [blame] | 59 | |
Ben Clayton | 9f83fa1 | 2023-05-10 17:56:14 +0000 | [diff] [blame] | 60 | /// @returns the type of the value |
dan sinclair | a5268df | 2023-06-08 21:39:53 +0000 | [diff] [blame] | 61 | const type::Type* Type() override { return result_type_; } |
dan sinclair | 53f646c | 2022-11-30 16:36:46 +0000 | [diff] [blame] | 62 | |
dan sinclair | 53f646c | 2022-11-30 16:36:46 +0000 | [diff] [blame] | 63 | /// @returns the left-hand-side value for the instruction |
dan sinclair | a5268df | 2023-06-08 21:39:53 +0000 | [diff] [blame] | 64 | Value* LHS() { return operands_[0]; } |
dan sinclair | 53f646c | 2022-11-30 16:36:46 +0000 | [diff] [blame] | 65 | |
| 66 | /// @returns the right-hand-side value for the instruction |
dan sinclair | a5268df | 2023-06-08 21:39:53 +0000 | [diff] [blame] | 67 | Value* RHS() { return operands_[1]; } |
dan sinclair | 53f646c | 2022-11-30 16:36:46 +0000 | [diff] [blame] | 68 | |
dan sinclair | 53f646c | 2022-11-30 16:36:46 +0000 | [diff] [blame] | 69 | private: |
dan sinclair | 24cb811 | 2023-05-18 22:16:08 +0000 | [diff] [blame] | 70 | enum Kind kind_; |
dan sinclair | eefa7fe | 2023-06-05 15:16:53 +0000 | [diff] [blame] | 71 | const type::Type* result_type_ = nullptr; |
dan sinclair | 53f646c | 2022-11-30 16:36:46 +0000 | [diff] [blame] | 72 | }; |
| 73 | |
dan sinclair | 53f646c | 2022-11-30 16:36:46 +0000 | [diff] [blame] | 74 | } // namespace tint::ir |
| 75 | |
| 76 | #endif // SRC_TINT_IR_BINARY_H_ |