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 "module.h"
28#include "reflect.h"
29
30namespace spvtools {
31namespace ir {
32
33std::vector<Instruction*> Module::types() {
34  std::vector<Instruction*> insts;
35  for (uint32_t i = 0; i < types_values_.size(); ++i) {
36    if (IsTypeInst(types_values_[i].opcode()))
37      insts.push_back(&types_values_[i]);
38  }
39  return insts;
40};
41
42void Module::ForEachInst(const std::function<void(Instruction*)>& f) {
43  for (auto& i : capabilities_) f(&i);
44  for (auto& i : extensions_) f(&i);
45  for (auto& i : ext_inst_imports_) f(&i);
46  f(&memory_model_);
47  for (auto& i : entry_points_) f(&i);
48  for (auto& i : execution_modes_) f(&i);
49  for (auto& i : debugs_) f(&i);
50  for (auto& i : annotations_) f(&i);
51  for (auto& i : types_values_) f(&i);
52  for (auto& i : functions_) i.ForEachInst(f);
53}
54
55void Module::ToBinary(std::vector<uint32_t>* binary, bool skip_nop) const {
56  binary->push_back(header_.magic_number);
57  binary->push_back(header_.version);
58  // TODO(antiagainst): should we change the generator number?
59  binary->push_back(header_.generator);
60  binary->push_back(header_.bound);
61  binary->push_back(header_.reserved);
62
63  // TODO(antiagainst): wow, looks like a duplication of the above.
64  for (const auto& c : capabilities_) c.ToBinary(binary, skip_nop);
65  for (const auto& e : extensions_) e.ToBinary(binary, skip_nop);
66  for (const auto& e : ext_inst_imports_) e.ToBinary(binary, skip_nop);
67  memory_model_.ToBinary(binary, skip_nop);
68  for (const auto& e : entry_points_) e.ToBinary(binary, skip_nop);
69  for (const auto& e : execution_modes_) e.ToBinary(binary, skip_nop);
70  for (const auto& d : debugs_) d.ToBinary(binary, skip_nop);
71  for (const auto& a : annotations_) a.ToBinary(binary, skip_nop);
72  for (const auto& t : types_values_) t.ToBinary(binary, skip_nop);
73  for (const auto& f : functions_) f.ToBinary(binary, skip_nop);
74}
75
76}  // namespace ir
77}  // namespace spvtools
78