1// Copyright (c) 2016 Google 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/opt/module.h"
16
17#include <algorithm>
18#include <cstring>
19#include <ostream>
20
21#include "source/operand.h"
22#include "source/opt/ir_context.h"
23#include "source/opt/reflect.h"
24
25namespace spvtools {
26namespace opt {
27
28uint32_t Module::TakeNextIdBound() {
29 if (context()) {
30 if (id_bound() >= context()->max_id_bound()) {
31 return 0;
32 }
33 } else if (id_bound() >= kDefaultMaxIdBound) {
34 return 0;
35 }
36
37 return header_.bound++;
38}
39
40std::vector<Instruction*> Module::GetTypes() {
41 std::vector<Instruction*> type_insts;
42 for (auto& inst : types_values_) {
43 if (IsTypeInst(inst.opcode())) type_insts.push_back(&inst);
44 }
45 return type_insts;
46}
47
48std::vector<const Instruction*> Module::GetTypes() const {
49 std::vector<const Instruction*> type_insts;
50 for (auto& inst : types_values_) {
51 if (IsTypeInst(inst.opcode())) type_insts.push_back(&inst);
52 }
53 return type_insts;
54}
55
56std::vector<Instruction*> Module::GetConstants() {
57 std::vector<Instruction*> const_insts;
58 for (auto& inst : types_values_) {
59 if (IsConstantInst(inst.opcode())) const_insts.push_back(&inst);
60 }
61 return const_insts;
62}
63
64std::vector<const Instruction*> Module::GetConstants() const {
65 std::vector<const Instruction*> const_insts;
66 for (auto& inst : types_values_) {
67 if (IsConstantInst(inst.opcode())) const_insts.push_back(&inst);
68 }
69 return const_insts;
70}
71
72uint32_t Module::GetGlobalValue(SpvOp opcode) const {
73 for (auto& inst : types_values_) {
74 if (inst.opcode() == opcode) return inst.result_id();
75 }
76 return 0;
77}
78
79void Module::AddGlobalValue(SpvOp opcode, uint32_t result_id,
80 uint32_t type_id) {
81 std::unique_ptr<Instruction> newGlobal(
82 new Instruction(context(), opcode, type_id, result_id, {}));
83 AddGlobalValue(std::move(newGlobal));
84}
85
86void Module::ForEachInst(const std::function<void(Instruction*)>& f,
87 bool run_on_debug_line_insts) {
88#define DELEGATE(list) list.ForEachInst(f, run_on_debug_line_insts)
89 DELEGATE(capabilities_);
90 DELEGATE(extensions_);
91 DELEGATE(ext_inst_imports_);
92 if (memory_model_) memory_model_->ForEachInst(f, run_on_debug_line_insts);
93 DELEGATE(entry_points_);
94 DELEGATE(execution_modes_);
95 DELEGATE(debugs1_);
96 DELEGATE(debugs2_);
97 DELEGATE(debugs3_);
98 DELEGATE(ext_inst_debuginfo_);
99 DELEGATE(annotations_);
100 DELEGATE(types_values_);
101 for (auto& i : functions_) i->ForEachInst(f, run_on_debug_line_insts);
102#undef DELEGATE
103}
104
105void Module::ForEachInst(const std::function<void(const Instruction*)>& f,
106 bool run_on_debug_line_insts) const {
107#define DELEGATE(i) i.ForEachInst(f, run_on_debug_line_insts)
108 for (auto& i : capabilities_) DELEGATE(i);
109 for (auto& i : extensions_) DELEGATE(i);
110 for (auto& i : ext_inst_imports_) DELEGATE(i);
111 if (memory_model_)
112 static_cast<const Instruction*>(memory_model_.get())
113 ->ForEachInst(f, run_on_debug_line_insts);
114 for (auto& i : entry_points_) DELEGATE(i);
115 for (auto& i : execution_modes_) DELEGATE(i);
116 for (auto& i : debugs1_) DELEGATE(i);
117 for (auto& i : debugs2_) DELEGATE(i);
118 for (auto& i : debugs3_) DELEGATE(i);
119 for (auto& i : annotations_) DELEGATE(i);
120 for (auto& i : types_values_) DELEGATE(i);
121 for (auto& i : ext_inst_debuginfo_) DELEGATE(i);
122 for (auto& i : functions_) {
123 static_cast<const Function*>(i.get())->ForEachInst(f,
124 run_on_debug_line_insts);
125 }
126 if (run_on_debug_line_insts) {
127 for (auto& i : trailing_dbg_line_info_) DELEGATE(i);
128 }
129#undef DELEGATE
130}
131
132void Module::ToBinary(std::vector<uint32_t>* binary, bool skip_nop) const {
133 binary->push_back(header_.magic_number);
134 binary->push_back(header_.version);
135 // TODO(antiagainst): should we change the generator number?
136 binary->push_back(header_.generator);
137 binary->push_back(header_.bound);
138 binary->push_back(header_.reserved);
139
140 size_t bound_idx = binary->size() - 2;
141 DebugScope last_scope(kNoDebugScope, kNoInlinedAt);
142 auto write_inst = [binary, skip_nop, &last_scope,
143 this](const Instruction* i) {
144 if (!(skip_nop && i->IsNop())) {
145 const auto& scope = i->GetDebugScope();
146 if (scope != last_scope) {
147 // Emit DebugScope |scope| to |binary|.
148 auto dbg_inst = ext_inst_debuginfo_.begin();
149 scope.ToBinary(dbg_inst->type_id(), context()->TakeNextId(),
150 dbg_inst->GetSingleWordOperand(2), binary);
151 last_scope = scope;
152 }
153
154 i->ToBinaryWithoutAttachedDebugInsts(binary);
155 }
156 };
157 ForEachInst(write_inst, true);
158
159 // We create new instructions for DebugScope. The bound must be updated.
160 binary->data()[bound_idx] = header_.bound;
161}
162
163uint32_t Module::ComputeIdBound() const {
164 uint32_t highest = 0;
165
166 ForEachInst(
167 [&highest](const Instruction* inst) {
168 for (const auto& operand : *inst) {
169 if (spvIsIdType(operand.type)) {
170 highest = std::max(highest, operand.words[0]);
171 }
172 }
173 },
174 true /* scan debug line insts as well */);
175
176 return highest + 1;
177}
178
179bool Module::HasExplicitCapability(uint32_t cap) {
180 for (auto& ci : capabilities_) {
181 uint32_t tcap = ci.GetSingleWordOperand(0);
182 if (tcap == cap) {
183 return true;
184 }
185 }
186 return false;
187}
188
189uint32_t Module::GetExtInstImportId(const char* extstr) {
190 for (auto& ei : ext_inst_imports_)
191 if (!strcmp(extstr,
192 reinterpret_cast<const char*>(&(ei.GetInOperand(0).words[0]))))
193 return ei.result_id();
194 return 0;
195}
196
197std::ostream& operator<<(std::ostream& str, const Module& module) {
198 module.ForEachInst([&str](const Instruction* inst) {
199 str << *inst;
200 if (inst->opcode() != SpvOpFunctionEnd) {
201 str << std::endl;
202 }
203 });
204 return str;
205}
206
207} // namespace opt
208} // namespace spvtools
209