instruction.cpp 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  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. #include "source/val/instruction.h"
  15. #include <utility>
  16. #include "source/binary.h"
  17. #include "source/util/string_utils.h"
  18. namespace spvtools {
  19. namespace val {
  20. Instruction::Instruction(const spv_parsed_instruction_t* inst)
  21. : words_(inst->words, inst->words + inst->num_words),
  22. operands_(inst->operands, inst->operands + inst->num_operands),
  23. inst_({words_.data(), inst->num_words, inst->opcode, inst->ext_inst_type,
  24. inst->type_id, inst->result_id, operands_.data(),
  25. inst->num_operands}) {}
  26. void Instruction::RegisterUse(const Instruction* inst, uint32_t index) {
  27. uses_.push_back(std::make_pair(inst, index));
  28. }
  29. bool operator<(const Instruction& lhs, const Instruction& rhs) {
  30. return lhs.id() < rhs.id();
  31. }
  32. bool operator<(const Instruction& lhs, uint32_t rhs) { return lhs.id() < rhs; }
  33. bool operator==(const Instruction& lhs, const Instruction& rhs) {
  34. return lhs.id() == rhs.id();
  35. }
  36. bool operator==(const Instruction& lhs, uint32_t rhs) {
  37. return lhs.id() == rhs;
  38. }
  39. template <>
  40. std::string Instruction::GetOperandAs<std::string>(size_t index) const {
  41. const spv_parsed_operand_t& o = operands_.at(index);
  42. assert(o.offset + o.num_words <= inst_.num_words);
  43. return spvtools::utils::MakeString(words_.data() + o.offset, o.num_words);
  44. }
  45. } // namespace val
  46. } // namespace spvtools