1 // Copyright 2020 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_WRITER_SPIRV_OPERAND_H_
16 #define SRC_WRITER_SPIRV_OPERAND_H_
17 
18 #include <string>
19 #include <vector>
20 
21 namespace tint {
22 namespace writer {
23 namespace spirv {
24 
25 /// A single SPIR-V instruction operand
26 class Operand {
27  public:
28   /// The kind of the operand
29   // Note, the `kInt` will cover most cases as things like IDs in SPIR-V are
30   // just ints for the purpose of converting to binary.
31   enum class Kind { kInt = 0, kFloat, kString };
32 
33   /// Creates a float operand
34   /// @param val the float value
35   /// @returns the operand
36   static Operand Float(float val);
37   /// Creates an int operand
38   /// @param val the int value
39   /// @returns the operand
40   static Operand Int(uint32_t val);
41   /// Creates a string operand
42   /// @param val the string value
43   /// @returns the operand
44   static Operand String(const std::string& val);
45 
46   /// Constructor
47   /// @param kind the type of operand
48   explicit Operand(Kind kind);
49   /// Copy Constructor
50   Operand(const Operand&) = default;
51   ~Operand();
52 
53   /// Copy assignment
54   /// @param b the operand to copy
55   /// @returns a copy of this operand
56   Operand& operator=(const Operand& b) = default;
57 
58   /// Sets the float value
59   /// @param val the value to set
set_float(float val)60   void set_float(float val) { float_val_ = val; }
61   /// Sets the int value
62   /// @param val the value to set
set_int(uint32_t val)63   void set_int(uint32_t val) { int_val_ = val; }
64   /// Sets the string value
65   /// @param val the value to set
set_string(const std::string & val)66   void set_string(const std::string& val) { str_val_ = val; }
67 
68   /// @returns true if this is a float operand
IsFloat()69   bool IsFloat() const { return kind_ == Kind::kFloat; }
70   /// @returns true if this is an integer operand
IsInt()71   bool IsInt() const { return kind_ == Kind::kInt; }
72   /// @returns true if this is a string operand
IsString()73   bool IsString() const { return kind_ == Kind::kString; }
74 
75   /// @returns the number of uint32_t's needed for this operand
76   uint32_t length() const;
77 
78   /// @returns the float value
to_f()79   float to_f() const { return float_val_; }
80   /// @returns the int value
to_i()81   uint32_t to_i() const { return int_val_; }
82   /// @returns the string value
to_s()83   const std::string& to_s() const { return str_val_; }
84 
85  private:
86   Kind kind_ = Kind::kInt;
87   float float_val_ = 0.0;
88   uint32_t int_val_ = 0;
89   std::string str_val_;
90 };
91 
92 /// A list of operands
93 using OperandList = std::vector<Operand>;
94 
95 }  // namespace spirv
96 }  // namespace writer
97 }  // namespace tint
98 
99 #endif  // SRC_WRITER_SPIRV_OPERAND_H_
100