• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (c) 2015-2016 The Khronos Group Inc.
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 #include "source/val/instruction.h"
16 
17 #include <utility>
18 
19 #include "source/binary.h"
20 #include "source/util/string_utils.h"
21 
22 namespace spvtools {
23 namespace val {
24 
Instruction(const spv_parsed_instruction_t * inst)25 Instruction::Instruction(const spv_parsed_instruction_t* inst)
26     : words_(inst->words, inst->words + inst->num_words),
27       operands_(inst->operands, inst->operands + inst->num_operands),
28       inst_({words_.data(), inst->num_words, inst->opcode, inst->ext_inst_type,
29              inst->type_id, inst->result_id, operands_.data(),
30              inst->num_operands}) {}
31 
RegisterUse(const Instruction * inst,uint32_t index)32 void Instruction::RegisterUse(const Instruction* inst, uint32_t index) {
33   uses_.push_back(std::make_pair(inst, index));
34 }
35 
operator <(const Instruction & lhs,const Instruction & rhs)36 bool operator<(const Instruction& lhs, const Instruction& rhs) {
37   return lhs.id() < rhs.id();
38 }
operator <(const Instruction & lhs,uint32_t rhs)39 bool operator<(const Instruction& lhs, uint32_t rhs) { return lhs.id() < rhs; }
operator ==(const Instruction & lhs,const Instruction & rhs)40 bool operator==(const Instruction& lhs, const Instruction& rhs) {
41   return lhs.id() == rhs.id();
42 }
operator ==(const Instruction & lhs,uint32_t rhs)43 bool operator==(const Instruction& lhs, uint32_t rhs) {
44   return lhs.id() == rhs;
45 }
46 
47 template <>
GetOperandAs(size_t index) const48 std::string Instruction::GetOperandAs<std::string>(size_t index) const {
49   const spv_parsed_operand_t& o = operands_.at(index);
50   assert(o.offset + o.num_words <= inst_.num_words);
51   return spvtools::utils::MakeString(words_.data() + o.offset, o.num_words);
52 }
53 
54 }  // namespace val
55 }  // namespace spvtools
56