1// Copyright (c) 2016 Google Inc.
2//
3// Permission is hereby granted, free of charge, to any person obtaining a
4// copy of this software and/or associated documentation files (the
5// "Materials"), to deal in the Materials without restriction, including
6// without limitation the rights to use, copy, modify, merge, publish,
7// distribute, sublicense, and/or sell copies of the Materials, and to
8// permit persons to whom the Materials are furnished to do so, subject to
9// the following conditions:
10//
11// The above copyright notice and this permission notice shall be included
12// in all copies or substantial portions of the Materials.
13//
14// MODIFICATIONS TO THIS FILE MAY MEAN IT NO LONGER ACCURATELY REFLECTS
15// KHRONOS STANDARDS. THE UNMODIFIED, NORMATIVE VERSIONS OF KHRONOS
16// SPECIFICATIONS AND HEADER INFORMATION ARE LOCATED AT
17//    https://www.khronos.org/registry/
18//
19// THE MATERIALS ARE PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
20// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
22// IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
23// CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
24// TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
25// MATERIALS OR THE USE OR OTHER DEALINGS IN THE MATERIALS.
26
27#include "instruction.h"
28
29#include <cassert>
30
31#include "reflect.h"
32
33namespace spvtools {
34namespace ir {
35
36Instruction::Instruction(const spv_parsed_instruction_t& inst,
37                         std::vector<Instruction>&& dbg_line)
38    : opcode_(static_cast<SpvOp>(inst.opcode)),
39      type_id_(inst.type_id),
40      result_id_(inst.result_id),
41      dbg_line_insts_(std::move(dbg_line)) {
42  assert((!IsDebugLineInst(opcode_) || dbg_line.empty()) &&
43         "Op(No)Line attaching to Op(No)Line found");
44  for (uint32_t i = 0; i < inst.num_operands; ++i) {
45    const auto& current_payload = inst.operands[i];
46    std::vector<uint32_t> words(
47        inst.words + current_payload.offset,
48        inst.words + current_payload.offset + current_payload.num_words);
49    operands_.emplace_back(current_payload.type, std::move(words));
50  }
51}
52
53uint32_t Instruction::GetSingleWordOperand(uint32_t index) const {
54  const auto& words = GetOperand(index).words;
55  assert(words.size() == 1 && "expected the operand only taking one word");
56  return words.front();
57}
58
59uint32_t Instruction::NumInOperandWords() const {
60  uint32_t size = 0;
61  for (uint32_t i = TypeResultIdCount(); i < operands_.size(); ++i)
62    size += static_cast<uint32_t>(operands_[i].words.size());
63  return size;
64}
65
66void Instruction::ToBinary(std::vector<uint32_t>* binary, bool skip_nop) const {
67  for (const auto& dbg_line : dbg_line_insts_) {
68    dbg_line.ToBinary(binary, skip_nop);
69  }
70
71  if (skip_nop && IsNop()) return;
72
73  const uint32_t num_words = 1 + NumOperandWords();
74  binary->push_back((num_words << 16) | static_cast<uint16_t>(opcode_));
75  for (const auto& operand : operands_)
76    binary->insert(binary->end(), operand.words.begin(), operand.words.end());
77}
78
79}  // namespace ir
80}  // namespace spvtools
81