• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_FUNCTION_H_
16 #define SRC_WRITER_SPIRV_FUNCTION_H_
17 
18 #include <functional>
19 
20 #include "src/writer/spirv/instruction.h"
21 
22 namespace tint {
23 namespace writer {
24 namespace spirv {
25 
26 /// A SPIR-V function
27 class Function {
28  public:
29   /// Constructor for testing purposes
30   /// This creates a bad declaration, so won't generate correct SPIR-V
31   Function();
32 
33   /// Constructor
34   /// @param declaration the function declaration
35   /// @param label_op the operand for the initial function label
36   /// @param params the function parameters
37   Function(const Instruction& declaration,
38            const Operand& label_op,
39            const InstructionList& params);
40   /// Copy constructor
41   /// @param other the function to copy
42   Function(const Function& other);
43   ~Function();
44 
45   /// Iterates over the function call the cb on each instruction
46   /// @param cb the callback to call
47   void iterate(std::function<void(const Instruction&)> cb) const;
48 
49   /// @returns the declaration
declaration()50   const Instruction& declaration() const { return declaration_; }
51 
52   /// @returns the function label id
label_id()53   uint32_t label_id() const { return label_op_.to_i(); }
54 
55   /// Adds an instruction to the instruction list
56   /// @param op the op to set
57   /// @param operands the operands for the instruction
push_inst(spv::Op op,const OperandList & operands)58   void push_inst(spv::Op op, const OperandList& operands) {
59     instructions_.push_back(Instruction{op, operands});
60   }
61   /// @returns the instruction list
instructions()62   const InstructionList& instructions() const { return instructions_; }
63 
64   /// Adds a variable to the variable list
65   /// @param operands the operands for the variable
push_var(const OperandList & operands)66   void push_var(const OperandList& operands) {
67     vars_.push_back(Instruction{spv::Op::OpVariable, operands});
68   }
69   /// @returns the variable list
variables()70   const InstructionList& variables() const { return vars_; }
71 
72   /// @returns the word length of the function
word_length()73   uint32_t word_length() const {
74     // 1 for the Label and 1 for the FunctionEnd
75     uint32_t size = 2 + declaration_.word_length();
76 
77     for (const auto& param : params_) {
78       size += param.word_length();
79     }
80     for (const auto& var : vars_) {
81       size += var.word_length();
82     }
83     for (const auto& inst : instructions_) {
84       size += inst.word_length();
85     }
86     return size;
87   }
88 
89  private:
90   Instruction declaration_;
91   Operand label_op_;
92   InstructionList params_;
93   InstructionList vars_;
94   InstructionList instructions_;
95 };
96 
97 }  // namespace spirv
98 }  // namespace writer
99 }  // namespace tint
100 
101 #endif  // SRC_WRITER_SPIRV_FUNCTION_H_
102