/src/shaderc/third_party/spirv-tools/source/disassemble.cpp
Line | Count | Source (jump to first uncovered line) |
1 | | // Copyright (c) 2015-2020 The Khronos Group Inc. |
2 | | // Modifications Copyright (C) 2020 Advanced Micro Devices, Inc. All rights |
3 | | // reserved. |
4 | | // |
5 | | // Licensed under the Apache License, Version 2.0 (the "License"); |
6 | | // you may not use this file except in compliance with the License. |
7 | | // You may obtain a copy of the License at |
8 | | // |
9 | | // http://www.apache.org/licenses/LICENSE-2.0 |
10 | | // |
11 | | // Unless required by applicable law or agreed to in writing, software |
12 | | // distributed under the License is distributed on an "AS IS" BASIS, |
13 | | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
14 | | // See the License for the specific language governing permissions and |
15 | | // limitations under the License. |
16 | | |
17 | | // This file contains a disassembler: It converts a SPIR-V binary |
18 | | // to text. |
19 | | |
20 | | #include "source/disassemble.h" |
21 | | |
22 | | #include <algorithm> |
23 | | #include <cassert> |
24 | | #include <cstring> |
25 | | #include <iomanip> |
26 | | #include <ios> |
27 | | #include <memory> |
28 | | #include <set> |
29 | | #include <sstream> |
30 | | #include <stack> |
31 | | #include <unordered_map> |
32 | | #include <utility> |
33 | | |
34 | | #include "source/binary.h" |
35 | | #include "source/diagnostic.h" |
36 | | #include "source/ext_inst.h" |
37 | | #include "source/opcode.h" |
38 | | #include "source/parsed_operand.h" |
39 | | #include "source/print.h" |
40 | | #include "source/spirv_constant.h" |
41 | | #include "source/spirv_endian.h" |
42 | | #include "source/table2.h" |
43 | | #include "source/util/hex_float.h" |
44 | | #include "source/util/make_unique.h" |
45 | | #include "spirv-tools/libspirv.h" |
46 | | |
47 | | namespace spvtools { |
48 | | namespace { |
49 | | |
50 | | // Indices to ControlFlowGraph's list of blocks from one block to its successors |
51 | | struct BlockSuccessors { |
52 | | // Merge block in OpLoopMerge and OpSelectionMerge |
53 | | uint32_t merge_block_id = 0; |
54 | | // The continue block in OpLoopMerge |
55 | | uint32_t continue_block_id = 0; |
56 | | // The true and false blocks in OpBranchConditional |
57 | | uint32_t true_block_id = 0; |
58 | | uint32_t false_block_id = 0; |
59 | | // The body block of a loop, as specified by OpBranch after a merge |
60 | | // instruction |
61 | | uint32_t body_block_id = 0; |
62 | | // The same-nesting-level block that follows this one, indicated by an |
63 | | // OpBranch with no merge instruction. |
64 | | uint32_t next_block_id = 0; |
65 | | // The cases (including default) of an OpSwitch |
66 | | std::vector<uint32_t> case_block_ids; |
67 | | }; |
68 | | |
69 | | class ParsedInstruction { |
70 | | public: |
71 | 0 | ParsedInstruction(const spv_parsed_instruction_t* instruction) { |
72 | | // Make a copy of the parsed instruction, including stable memory for its |
73 | | // operands. |
74 | 0 | instruction_ = *instruction; |
75 | 0 | operands_ = |
76 | 0 | std::make_unique<spv_parsed_operand_t[]>(instruction->num_operands); |
77 | 0 | memcpy(operands_.get(), instruction->operands, |
78 | 0 | instruction->num_operands * sizeof(*instruction->operands)); |
79 | 0 | instruction_.operands = operands_.get(); |
80 | 0 | } |
81 | | |
82 | 0 | const spv_parsed_instruction_t* get() const { return &instruction_; } |
83 | | |
84 | | private: |
85 | | spv_parsed_instruction_t instruction_; |
86 | | std::unique_ptr<spv_parsed_operand_t[]> operands_; |
87 | | }; |
88 | | |
89 | | // One block in the CFG |
90 | | struct SingleBlock { |
91 | | // The byte offset in the SPIR-V where the block starts. Used for printing in |
92 | | // a comment. |
93 | | size_t byte_offset; |
94 | | |
95 | | // Block instructions |
96 | | std::vector<ParsedInstruction> instructions; |
97 | | |
98 | | // Successors of this block |
99 | | BlockSuccessors successors; |
100 | | |
101 | | // The nesting level for this block. |
102 | | uint32_t nest_level = 0; |
103 | | bool nest_level_assigned = false; |
104 | | |
105 | | // Whether the block was reachable |
106 | | bool reachable = false; |
107 | | }; |
108 | | |
109 | | // CFG for one function |
110 | | struct ControlFlowGraph { |
111 | | std::vector<SingleBlock> blocks; |
112 | | }; |
113 | | |
114 | | // A Disassembler instance converts a SPIR-V binary to its assembly |
115 | | // representation. |
116 | | class Disassembler { |
117 | | public: |
118 | | Disassembler(uint32_t options, NameMapper name_mapper) |
119 | 964 | : print_(spvIsInBitfield(SPV_BINARY_TO_TEXT_OPTION_PRINT, options)), |
120 | | nested_indent_( |
121 | 964 | spvIsInBitfield(SPV_BINARY_TO_TEXT_OPTION_NESTED_INDENT, options)), |
122 | | reorder_blocks_( |
123 | 964 | spvIsInBitfield(SPV_BINARY_TO_TEXT_OPTION_REORDER_BLOCKS, options)), |
124 | 964 | text_(), |
125 | 964 | out_(print_ ? out_stream() : out_stream(text_)), |
126 | 964 | instruction_disassembler_(out_.get(), options, name_mapper), |
127 | 964 | header_(!spvIsInBitfield(SPV_BINARY_TO_TEXT_OPTION_NO_HEADER, options)), |
128 | 964 | byte_offset_(0) {} |
129 | | |
130 | | // Emits the assembly header for the module, and sets up internal state |
131 | | // so subsequent callbacks can handle the cases where the entire module |
132 | | // is either big-endian or little-endian. |
133 | | spv_result_t HandleHeader(spv_endianness_t endian, uint32_t version, |
134 | | uint32_t generator, uint32_t id_bound, |
135 | | uint32_t schema); |
136 | | // Emits the assembly text for the given instruction. |
137 | | spv_result_t HandleInstruction(const spv_parsed_instruction_t& inst); |
138 | | |
139 | | // If not printing, populates text_result with the accumulated text. |
140 | | // Returns SPV_SUCCESS on success. |
141 | | spv_result_t SaveTextResult(spv_text* text_result) const; |
142 | | |
143 | | private: |
144 | | void EmitCFG(); |
145 | | |
146 | | const bool print_; // Should we also print to the standard output stream? |
147 | | const bool nested_indent_; // Should the blocks be indented according to the |
148 | | // control flow structure? |
149 | | const bool |
150 | | reorder_blocks_; // Should the blocks be reordered for readability? |
151 | | spv_endianness_t endian_; // The detected endianness of the binary. |
152 | | std::stringstream text_; // Captures the text, if not printing. |
153 | | out_stream out_; // The Output stream. Either to text_ or standard output. |
154 | | disassemble::InstructionDisassembler instruction_disassembler_; |
155 | | const bool header_; // Should we output header as the leading comment? |
156 | | size_t byte_offset_; // The number of bytes processed so far. |
157 | | bool inserted_decoration_space_ = false; |
158 | | bool inserted_debug_space_ = false; |
159 | | bool inserted_type_space_ = false; |
160 | | |
161 | | // The CFG for the current function |
162 | | ControlFlowGraph current_function_cfg_; |
163 | | }; |
164 | | |
165 | | spv_result_t Disassembler::HandleHeader(spv_endianness_t endian, |
166 | | uint32_t version, uint32_t generator, |
167 | 964 | uint32_t id_bound, uint32_t schema) { |
168 | 964 | endian_ = endian; |
169 | | |
170 | 964 | if (header_) { |
171 | 890 | instruction_disassembler_.EmitHeaderSpirv(); |
172 | 890 | instruction_disassembler_.EmitHeaderVersion(version); |
173 | 890 | instruction_disassembler_.EmitHeaderGenerator(generator); |
174 | 890 | instruction_disassembler_.EmitHeaderIdBound(id_bound); |
175 | 890 | instruction_disassembler_.EmitHeaderSchema(schema); |
176 | 890 | } |
177 | | |
178 | 964 | byte_offset_ = SPV_INDEX_INSTRUCTION * sizeof(uint32_t); |
179 | | |
180 | 964 | return SPV_SUCCESS; |
181 | 964 | } |
182 | | |
183 | | spv_result_t Disassembler::HandleInstruction( |
184 | 83.8k | const spv_parsed_instruction_t& inst) { |
185 | 83.8k | instruction_disassembler_.EmitSectionComment(inst, inserted_decoration_space_, |
186 | 83.8k | inserted_debug_space_, |
187 | 83.8k | inserted_type_space_); |
188 | | |
189 | | // When nesting needs to be calculated or when the blocks are reordered, we |
190 | | // have to have the full picture of the CFG first. Defer processing of the |
191 | | // instructions until the entire function is visited. This is not done |
192 | | // without those options (even if simpler) to improve debuggability; for |
193 | | // example to be able to see whatever is parsed so far even if there is a |
194 | | // parse error. |
195 | 83.8k | if (nested_indent_ || reorder_blocks_) { |
196 | 0 | switch (static_cast<spv::Op>(inst.opcode)) { |
197 | 0 | case spv::Op::OpLabel: { |
198 | | // Add a new block to the CFG |
199 | 0 | SingleBlock new_block; |
200 | 0 | new_block.byte_offset = byte_offset_; |
201 | 0 | new_block.instructions.emplace_back(&inst); |
202 | 0 | current_function_cfg_.blocks.push_back(std::move(new_block)); |
203 | 0 | break; |
204 | 0 | } |
205 | 0 | case spv::Op::OpFunctionEnd: |
206 | | // Process the CFG and output the instructions |
207 | 0 | EmitCFG(); |
208 | | // Output OpFunctionEnd itself too |
209 | 0 | [[fallthrough]]; |
210 | 0 | default: |
211 | 0 | if (!current_function_cfg_.blocks.empty()) { |
212 | | // If in a function, stash the instruction for later. |
213 | 0 | current_function_cfg_.blocks.back().instructions.emplace_back(&inst); |
214 | 0 | } else { |
215 | | // Otherwise emit the instruction right away. |
216 | 0 | instruction_disassembler_.EmitInstruction(inst, byte_offset_); |
217 | 0 | } |
218 | 0 | break; |
219 | 0 | } |
220 | 83.8k | } else { |
221 | 83.8k | instruction_disassembler_.EmitInstruction(inst, byte_offset_); |
222 | 83.8k | } |
223 | | |
224 | 83.8k | byte_offset_ += inst.num_words * sizeof(uint32_t); |
225 | | |
226 | 83.8k | return SPV_SUCCESS; |
227 | 83.8k | } |
228 | | |
229 | | // Helper to get the operand of an instruction as an id. |
230 | | uint32_t GetOperand(const spv_parsed_instruction_t* instruction, |
231 | 0 | uint32_t operand) { |
232 | 0 | return instruction->words[instruction->operands[operand].offset]; |
233 | 0 | } |
234 | | |
235 | | std::unordered_map<uint32_t, uint32_t> BuildControlFlowGraph( |
236 | 0 | ControlFlowGraph& cfg) { |
237 | 0 | std::unordered_map<uint32_t, uint32_t> id_to_index; |
238 | |
|
239 | 0 | for (size_t index = 0; index < cfg.blocks.size(); ++index) { |
240 | 0 | SingleBlock& block = cfg.blocks[index]; |
241 | | |
242 | | // For future use, build the ID->index map |
243 | 0 | assert(static_cast<spv::Op>(block.instructions[0].get()->opcode) == |
244 | 0 | spv::Op::OpLabel); |
245 | 0 | const uint32_t id = block.instructions[0].get()->result_id; |
246 | |
|
247 | 0 | id_to_index[id] = static_cast<uint32_t>(index); |
248 | | |
249 | | // Look for a merge instruction first. The function of OpBranch depends on |
250 | | // that. |
251 | 0 | if (block.instructions.size() >= 3) { |
252 | 0 | const spv_parsed_instruction_t* maybe_merge = |
253 | 0 | block.instructions[block.instructions.size() - 2].get(); |
254 | |
|
255 | 0 | switch (static_cast<spv::Op>(maybe_merge->opcode)) { |
256 | 0 | case spv::Op::OpLoopMerge: |
257 | 0 | block.successors.merge_block_id = GetOperand(maybe_merge, 0); |
258 | 0 | block.successors.continue_block_id = GetOperand(maybe_merge, 1); |
259 | 0 | break; |
260 | | |
261 | 0 | case spv::Op::OpSelectionMerge: |
262 | 0 | block.successors.merge_block_id = GetOperand(maybe_merge, 0); |
263 | 0 | break; |
264 | | |
265 | 0 | default: |
266 | 0 | break; |
267 | 0 | } |
268 | 0 | } |
269 | | |
270 | | // Then look at the last instruction; it must be a branch |
271 | 0 | assert(block.instructions.size() >= 2); |
272 | |
|
273 | 0 | const spv_parsed_instruction_t* branch = block.instructions.back().get(); |
274 | 0 | switch (static_cast<spv::Op>(branch->opcode)) { |
275 | 0 | case spv::Op::OpBranch: |
276 | 0 | if (block.successors.merge_block_id != 0) { |
277 | 0 | block.successors.body_block_id = GetOperand(branch, 0); |
278 | 0 | } else { |
279 | 0 | block.successors.next_block_id = GetOperand(branch, 0); |
280 | 0 | } |
281 | 0 | break; |
282 | | |
283 | 0 | case spv::Op::OpBranchConditional: |
284 | 0 | block.successors.true_block_id = GetOperand(branch, 1); |
285 | 0 | block.successors.false_block_id = GetOperand(branch, 2); |
286 | 0 | break; |
287 | | |
288 | 0 | case spv::Op::OpSwitch: |
289 | 0 | for (uint32_t case_index = 1; case_index < branch->num_operands; |
290 | 0 | case_index += 2) { |
291 | 0 | block.successors.case_block_ids.push_back( |
292 | 0 | GetOperand(branch, case_index)); |
293 | 0 | } |
294 | 0 | break; |
295 | | |
296 | 0 | default: |
297 | 0 | break; |
298 | 0 | } |
299 | 0 | } |
300 | | |
301 | 0 | return id_to_index; |
302 | 0 | } |
303 | | |
304 | | // Helper to deal with nesting and non-existing ids / previously-assigned |
305 | | // levels. It assigns a given nesting level `level` to the block identified by |
306 | | // `id` (unless that block already has a nesting level assigned). |
307 | | void Nest(ControlFlowGraph& cfg, |
308 | | const std::unordered_map<uint32_t, uint32_t>& id_to_index, |
309 | 0 | uint32_t id, uint32_t level) { |
310 | 0 | if (id == 0) { |
311 | 0 | return; |
312 | 0 | } |
313 | | |
314 | 0 | const uint32_t block_index = id_to_index.at(id); |
315 | 0 | SingleBlock& block = cfg.blocks[block_index]; |
316 | |
|
317 | 0 | if (!block.nest_level_assigned) { |
318 | 0 | block.nest_level = level; |
319 | 0 | block.nest_level_assigned = true; |
320 | 0 | } |
321 | 0 | } |
322 | | |
323 | | // For a given block, assign nesting level to its successors. |
324 | | void NestSuccessors(ControlFlowGraph& cfg, const SingleBlock& block, |
325 | 0 | const std::unordered_map<uint32_t, uint32_t>& id_to_index) { |
326 | 0 | assert(block.nest_level_assigned); |
327 | | |
328 | | // Nest loops as such: |
329 | | // |
330 | | // %loop = OpLabel |
331 | | // OpLoopMerge %merge %cont ... |
332 | | // OpBranch %body |
333 | | // %body = OpLabel |
334 | | // Op... |
335 | | // %cont = OpLabel |
336 | | // Op... |
337 | | // %merge = OpLabel |
338 | | // Op... |
339 | | // |
340 | | // Nest conditional branches as such: |
341 | | // |
342 | | // %header = OpLabel |
343 | | // OpSelectionMerge %merge ... |
344 | | // OpBranchConditional ... %true %false |
345 | | // %true = OpLabel |
346 | | // Op... |
347 | | // %false = OpLabel |
348 | | // Op... |
349 | | // %merge = OpLabel |
350 | | // Op... |
351 | | // |
352 | | // Nest switch/case as such: |
353 | | // |
354 | | // %header = OpLabel |
355 | | // OpSelectionMerge %merge ... |
356 | | // OpSwitch ... %default ... %case0 ... %case1 ... |
357 | | // %default = OpLabel |
358 | | // Op... |
359 | | // %case0 = OpLabel |
360 | | // Op... |
361 | | // %case1 = OpLabel |
362 | | // Op... |
363 | | // ... |
364 | | // %merge = OpLabel |
365 | | // Op... |
366 | | // |
367 | | // The following can be observed: |
368 | | // |
369 | | // - In all cases, the merge block has the same nesting as this block |
370 | | // - The continue block of loops is nested 1 level deeper |
371 | | // - The body/branches/cases are nested 2 levels deeper |
372 | | // |
373 | | // Back branches to the header block, branches to the merge block, etc |
374 | | // are correctly handled by processing the header block first (that is |
375 | | // _this_ block, already processed), then following the above rules |
376 | | // (in the same order) for any block that is not already processed. |
377 | 0 | Nest(cfg, id_to_index, block.successors.merge_block_id, block.nest_level); |
378 | 0 | Nest(cfg, id_to_index, block.successors.continue_block_id, |
379 | 0 | block.nest_level + 1); |
380 | 0 | Nest(cfg, id_to_index, block.successors.true_block_id, block.nest_level + 2); |
381 | 0 | Nest(cfg, id_to_index, block.successors.false_block_id, block.nest_level + 2); |
382 | 0 | Nest(cfg, id_to_index, block.successors.body_block_id, block.nest_level + 2); |
383 | 0 | Nest(cfg, id_to_index, block.successors.next_block_id, block.nest_level); |
384 | 0 | for (uint32_t case_block_id : block.successors.case_block_ids) { |
385 | 0 | Nest(cfg, id_to_index, case_block_id, block.nest_level + 2); |
386 | 0 | } |
387 | 0 | } |
388 | | |
389 | | struct StackEntry { |
390 | | // The index of the block (in ControlFlowGraph::blocks) to process. |
391 | | uint32_t block_index; |
392 | | // Whether this is the pre or post visit of the block. Because a post-visit |
393 | | // traversal is needed, the same block is pushed back on the stack on |
394 | | // pre-visit so it can be visited again on post-visit. |
395 | | bool post_visit = false; |
396 | | }; |
397 | | |
398 | | // Helper to deal with DFS traversal and non-existing ids |
399 | | void VisitSuccesor(std::stack<StackEntry>* dfs_stack, |
400 | | const std::unordered_map<uint32_t, uint32_t>& id_to_index, |
401 | 0 | uint32_t id) { |
402 | 0 | if (id != 0) { |
403 | 0 | dfs_stack->push({id_to_index.at(id), false}); |
404 | 0 | } |
405 | 0 | } |
406 | | |
407 | | // Given the control flow graph, calculates and returns the reverse post-order |
408 | | // ordering of the blocks. The blocks are then disassembled in that order for |
409 | | // readability. |
410 | | std::vector<uint32_t> OrderBlocks( |
411 | | ControlFlowGraph& cfg, |
412 | 0 | const std::unordered_map<uint32_t, uint32_t>& id_to_index) { |
413 | 0 | std::vector<uint32_t> post_order; |
414 | | |
415 | | // Nest level of a function's first block is 0. |
416 | 0 | cfg.blocks[0].nest_level = 0; |
417 | 0 | cfg.blocks[0].nest_level_assigned = true; |
418 | | |
419 | | // Stack of block indices as they are visited. |
420 | 0 | std::stack<StackEntry> dfs_stack; |
421 | 0 | dfs_stack.push({0, false}); |
422 | |
|
423 | 0 | std::set<uint32_t> visited; |
424 | |
|
425 | 0 | while (!dfs_stack.empty()) { |
426 | 0 | const uint32_t block_index = dfs_stack.top().block_index; |
427 | 0 | const bool post_visit = dfs_stack.top().post_visit; |
428 | 0 | dfs_stack.pop(); |
429 | | |
430 | | // If this is the second time the block is visited, that's the post-order |
431 | | // visit. |
432 | 0 | if (post_visit) { |
433 | 0 | post_order.push_back(block_index); |
434 | 0 | continue; |
435 | 0 | } |
436 | | |
437 | | // If already visited, another path got to it first (like a case |
438 | | // fallthrough), avoid reprocessing it. |
439 | 0 | if (visited.count(block_index) > 0) { |
440 | 0 | continue; |
441 | 0 | } |
442 | 0 | visited.insert(block_index); |
443 | | |
444 | | // Push it back in the stack for post-order visit |
445 | 0 | dfs_stack.push({block_index, true}); |
446 | |
|
447 | 0 | SingleBlock& block = cfg.blocks[block_index]; |
448 | | |
449 | | // Assign nest levels of successors right away. The successors are either |
450 | | // nested under this block, or are back or forward edges to blocks outside |
451 | | // this nesting level (no farther than the merge block), whose nesting |
452 | | // levels are already assigned before this block is visited. |
453 | 0 | NestSuccessors(cfg, block, id_to_index); |
454 | 0 | block.reachable = true; |
455 | | |
456 | | // The post-order visit yields the order in which the blocks are naturally |
457 | | // ordered _backwards_. So blocks to be ordered last should be visited |
458 | | // first. In other words, they should be pushed to the DFS stack last. |
459 | 0 | VisitSuccesor(&dfs_stack, id_to_index, block.successors.true_block_id); |
460 | 0 | VisitSuccesor(&dfs_stack, id_to_index, block.successors.false_block_id); |
461 | 0 | VisitSuccesor(&dfs_stack, id_to_index, block.successors.body_block_id); |
462 | 0 | VisitSuccesor(&dfs_stack, id_to_index, block.successors.next_block_id); |
463 | 0 | for (uint32_t case_block_id : block.successors.case_block_ids) { |
464 | 0 | VisitSuccesor(&dfs_stack, id_to_index, case_block_id); |
465 | 0 | } |
466 | 0 | VisitSuccesor(&dfs_stack, id_to_index, block.successors.continue_block_id); |
467 | 0 | VisitSuccesor(&dfs_stack, id_to_index, block.successors.merge_block_id); |
468 | 0 | } |
469 | |
|
470 | 0 | std::vector<uint32_t> order(post_order.rbegin(), post_order.rend()); |
471 | | |
472 | | // Finally, dump all unreachable blocks at the end |
473 | 0 | for (size_t index = 0; index < cfg.blocks.size(); ++index) { |
474 | 0 | SingleBlock& block = cfg.blocks[index]; |
475 | |
|
476 | 0 | if (!block.reachable) { |
477 | 0 | order.push_back(static_cast<uint32_t>(index)); |
478 | 0 | block.nest_level = 0; |
479 | 0 | block.nest_level_assigned = true; |
480 | 0 | } |
481 | 0 | } |
482 | |
|
483 | 0 | return order; |
484 | 0 | } |
485 | | |
486 | 0 | void Disassembler::EmitCFG() { |
487 | | // Build the CFG edges. At the same time, build an ID->block index map to |
488 | | // simplify building the CFG edges. |
489 | 0 | const std::unordered_map<uint32_t, uint32_t> id_to_index = |
490 | 0 | BuildControlFlowGraph(current_function_cfg_); |
491 | | |
492 | | // Walk the CFG in reverse post-order to find the best ordering of blocks for |
493 | | // presentation |
494 | 0 | std::vector<uint32_t> block_order = |
495 | 0 | OrderBlocks(current_function_cfg_, id_to_index); |
496 | 0 | assert(block_order.size() == current_function_cfg_.blocks.size()); |
497 | | |
498 | | // Walk the CFG either in block order or input order based on whether the |
499 | | // reorder_blocks_ option is given. |
500 | 0 | for (uint32_t index = 0; index < current_function_cfg_.blocks.size(); |
501 | 0 | ++index) { |
502 | 0 | const uint32_t block_index = reorder_blocks_ ? block_order[index] : index; |
503 | 0 | const SingleBlock& block = current_function_cfg_.blocks[block_index]; |
504 | | |
505 | | // Emit instructions for this block |
506 | 0 | size_t byte_offset = block.byte_offset; |
507 | 0 | assert(block.nest_level_assigned); |
508 | |
|
509 | 0 | for (const ParsedInstruction& inst : block.instructions) { |
510 | 0 | instruction_disassembler_.EmitInstructionInBlock(*inst.get(), byte_offset, |
511 | 0 | block.nest_level); |
512 | 0 | byte_offset += inst.get()->num_words * sizeof(uint32_t); |
513 | 0 | } |
514 | 0 | } |
515 | |
|
516 | 0 | current_function_cfg_.blocks.clear(); |
517 | 0 | } |
518 | | |
519 | 963 | spv_result_t Disassembler::SaveTextResult(spv_text* text_result) const { |
520 | 963 | if (!print_) { |
521 | 963 | size_t length = text_.str().size(); |
522 | 963 | char* str = new char[length + 1]; |
523 | 963 | if (!str) return SPV_ERROR_OUT_OF_MEMORY; |
524 | 963 | strncpy(str, text_.str().c_str(), length + 1); |
525 | 963 | spv_text text = new spv_text_t(); |
526 | 963 | if (!text) { |
527 | 0 | delete[] str; |
528 | 0 | return SPV_ERROR_OUT_OF_MEMORY; |
529 | 0 | } |
530 | 963 | text->str = str; |
531 | 963 | text->length = length; |
532 | 963 | *text_result = text; |
533 | 963 | } |
534 | 963 | return SPV_SUCCESS; |
535 | 963 | } |
536 | | |
537 | | spv_result_t DisassembleHeader(void* user_data, spv_endianness_t endian, |
538 | | uint32_t /* magic */, uint32_t version, |
539 | | uint32_t generator, uint32_t id_bound, |
540 | 890 | uint32_t schema) { |
541 | 890 | assert(user_data); |
542 | 890 | auto disassembler = static_cast<Disassembler*>(user_data); |
543 | 890 | return disassembler->HandleHeader(endian, version, generator, id_bound, |
544 | 890 | schema); |
545 | 890 | } |
546 | | |
547 | | spv_result_t DisassembleInstruction( |
548 | 83.8k | void* user_data, const spv_parsed_instruction_t* parsed_instruction) { |
549 | 83.8k | assert(user_data); |
550 | 83.8k | auto disassembler = static_cast<Disassembler*>(user_data); |
551 | 83.8k | return disassembler->HandleInstruction(*parsed_instruction); |
552 | 83.8k | } |
553 | | |
554 | | // Simple wrapper class to provide extra data necessary for targeted |
555 | | // instruction disassembly. |
556 | | class WrappedDisassembler { |
557 | | public: |
558 | | WrappedDisassembler(Disassembler* dis, const uint32_t* binary, size_t wc) |
559 | 74 | : disassembler_(dis), inst_binary_(binary), word_count_(wc) {} |
560 | | |
561 | 148 | Disassembler* disassembler() { return disassembler_; } |
562 | 1.08k | const uint32_t* inst_binary() const { return inst_binary_; } |
563 | 2.66k | size_t word_count() const { return word_count_; } |
564 | | |
565 | | private: |
566 | | Disassembler* disassembler_; |
567 | | const uint32_t* inst_binary_; |
568 | | const size_t word_count_; |
569 | | }; |
570 | | |
571 | | spv_result_t DisassembleTargetHeader(void* user_data, spv_endianness_t endian, |
572 | | uint32_t /* magic */, uint32_t version, |
573 | | uint32_t generator, uint32_t id_bound, |
574 | 74 | uint32_t schema) { |
575 | 74 | assert(user_data); |
576 | 74 | auto wrapped = static_cast<WrappedDisassembler*>(user_data); |
577 | 74 | return wrapped->disassembler()->HandleHeader(endian, version, generator, |
578 | 74 | id_bound, schema); |
579 | 74 | } |
580 | | |
581 | | spv_result_t DisassembleTargetInstruction( |
582 | 2.12k | void* user_data, const spv_parsed_instruction_t* parsed_instruction) { |
583 | 2.12k | assert(user_data); |
584 | 2.12k | auto wrapped = static_cast<WrappedDisassembler*>(user_data); |
585 | | // Check if this is the instruction we want to disassemble. |
586 | 2.12k | if (wrapped->word_count() == parsed_instruction->num_words && |
587 | 2.12k | std::equal(wrapped->inst_binary(), |
588 | 540 | wrapped->inst_binary() + wrapped->word_count(), |
589 | 540 | parsed_instruction->words)) { |
590 | | // Found the target instruction. Disassemble it and signal that we should |
591 | | // stop searching so we don't output the same instruction again. |
592 | 74 | if (auto error = |
593 | 74 | wrapped->disassembler()->HandleInstruction(*parsed_instruction)) |
594 | 0 | return error; |
595 | 74 | return SPV_REQUESTED_TERMINATION; |
596 | 74 | } |
597 | 2.04k | return SPV_SUCCESS; |
598 | 2.12k | } |
599 | | |
600 | 0 | uint32_t GetLineLengthWithoutColor(const std::string line) { |
601 | | // Currently, every added color is in the form \x1b...m, so instead of doing a |
602 | | // lot of string comparisons with spvtools::clr::* strings, we just ignore |
603 | | // those ranges. |
604 | 0 | uint32_t length = 0; |
605 | 0 | for (size_t i = 0; i < line.size(); ++i) { |
606 | 0 | if (line[i] == '\x1b') { |
607 | 0 | do { |
608 | 0 | ++i; |
609 | 0 | } while (i < line.size() && line[i] != 'm'); |
610 | 0 | continue; |
611 | 0 | } |
612 | | |
613 | 0 | ++length; |
614 | 0 | } |
615 | |
|
616 | 0 | return length; |
617 | 0 | } |
618 | | |
619 | | constexpr int kStandardIndent = 15; |
620 | | constexpr int kBlockNestIndent = 2; |
621 | | constexpr int kBlockBodyIndentOffset = 2; |
622 | | constexpr uint32_t kCommentColumn = 50; |
623 | | } // namespace |
624 | | |
625 | | namespace disassemble { |
626 | | InstructionDisassembler::InstructionDisassembler(std::ostream& stream, |
627 | | uint32_t options, |
628 | | NameMapper name_mapper) |
629 | 964 | : stream_(stream), |
630 | 964 | print_(spvIsInBitfield(SPV_BINARY_TO_TEXT_OPTION_PRINT, options)), |
631 | 964 | color_(spvIsInBitfield(SPV_BINARY_TO_TEXT_OPTION_COLOR, options)), |
632 | 964 | indent_(spvIsInBitfield(SPV_BINARY_TO_TEXT_OPTION_INDENT, options) |
633 | 964 | ? kStandardIndent |
634 | 964 | : 0), |
635 | | nested_indent_( |
636 | 964 | spvIsInBitfield(SPV_BINARY_TO_TEXT_OPTION_NESTED_INDENT, options)), |
637 | 964 | comment_(spvIsInBitfield(SPV_BINARY_TO_TEXT_OPTION_COMMENT, options)), |
638 | | show_byte_offset_( |
639 | 964 | spvIsInBitfield(SPV_BINARY_TO_TEXT_OPTION_SHOW_BYTE_OFFSET, options)), |
640 | 964 | name_mapper_(std::move(name_mapper)), |
641 | 964 | last_instruction_comment_alignment_(0) {} |
642 | | |
643 | 890 | void InstructionDisassembler::EmitHeaderSpirv() { stream_ << "; SPIR-V\n"; } |
644 | | |
645 | 890 | void InstructionDisassembler::EmitHeaderVersion(uint32_t version) { |
646 | 890 | stream_ << "; Version: " << SPV_SPIRV_VERSION_MAJOR_PART(version) << "." |
647 | 890 | << SPV_SPIRV_VERSION_MINOR_PART(version) << "\n"; |
648 | 890 | } |
649 | | |
650 | 890 | void InstructionDisassembler::EmitHeaderGenerator(uint32_t generator) { |
651 | 890 | const char* generator_tool = |
652 | 890 | spvGeneratorStr(SPV_GENERATOR_TOOL_PART(generator)); |
653 | 890 | stream_ << "; Generator: " << generator_tool; |
654 | | // For unknown tools, print the numeric tool value. |
655 | 890 | if (0 == strcmp("Unknown", generator_tool)) { |
656 | 0 | stream_ << "(" << SPV_GENERATOR_TOOL_PART(generator) << ")"; |
657 | 0 | } |
658 | | // Print the miscellaneous part of the generator word on the same |
659 | | // line as the tool name. |
660 | 890 | stream_ << "; " << SPV_GENERATOR_MISC_PART(generator) << "\n"; |
661 | 890 | } |
662 | | |
663 | 890 | void InstructionDisassembler::EmitHeaderIdBound(uint32_t id_bound) { |
664 | 890 | stream_ << "; Bound: " << id_bound << "\n"; |
665 | 890 | } |
666 | | |
667 | 890 | void InstructionDisassembler::EmitHeaderSchema(uint32_t schema) { |
668 | 890 | stream_ << "; Schema: " << schema << "\n"; |
669 | 890 | } |
670 | | |
671 | | void InstructionDisassembler::EmitInstruction( |
672 | 83.8k | const spv_parsed_instruction_t& inst, size_t inst_byte_offset) { |
673 | 83.8k | EmitInstructionImpl(inst, inst_byte_offset, 0, false); |
674 | 83.8k | } |
675 | | |
676 | | void InstructionDisassembler::EmitInstructionInBlock( |
677 | | const spv_parsed_instruction_t& inst, size_t inst_byte_offset, |
678 | 0 | uint32_t block_indent) { |
679 | 0 | EmitInstructionImpl(inst, inst_byte_offset, block_indent, true); |
680 | 0 | } |
681 | | |
682 | | void InstructionDisassembler::EmitInstructionImpl( |
683 | | const spv_parsed_instruction_t& inst, size_t inst_byte_offset, |
684 | 83.8k | uint32_t block_indent, bool is_in_block) { |
685 | 83.8k | auto opcode = static_cast<spv::Op>(inst.opcode); |
686 | | |
687 | | // To better align the comments (if any), write the instruction to a line |
688 | | // first so its length can be readily available. |
689 | 83.8k | std::ostringstream line; |
690 | | |
691 | 83.8k | if (nested_indent_ && opcode == spv::Op::OpLabel) { |
692 | | // Separate the blocks by an empty line to make them easier to separate |
693 | 0 | stream_ << std::endl; |
694 | 0 | } |
695 | | |
696 | 83.8k | if (inst.result_id) { |
697 | 56.0k | SetBlue(); |
698 | 56.0k | const std::string id_name = name_mapper_(inst.result_id); |
699 | 56.0k | if (indent_) |
700 | 56.0k | line << std::setw(std::max(0, indent_ - 3 - int(id_name.size()))); |
701 | 56.0k | line << "%" << id_name; |
702 | 56.0k | ResetColor(); |
703 | 56.0k | line << " = "; |
704 | 56.0k | } else { |
705 | 27.7k | line << std::string(indent_, ' '); |
706 | 27.7k | } |
707 | | |
708 | 83.8k | if (nested_indent_ && is_in_block) { |
709 | | // Output OpLabel at the specified nest level, and instructions inside |
710 | | // blocks nested a little more. |
711 | 0 | uint32_t indent = block_indent; |
712 | 0 | bool body_indent = opcode != spv::Op::OpLabel; |
713 | |
|
714 | 0 | line << std::string( |
715 | 0 | indent * kBlockNestIndent + (body_indent ? kBlockBodyIndentOffset : 0), |
716 | 0 | ' '); |
717 | 0 | } |
718 | | |
719 | 83.8k | line << "Op" << spvOpcodeString(opcode); |
720 | | |
721 | 337k | for (uint16_t i = 0; i < inst.num_operands; i++) { |
722 | 253k | const spv_operand_type_t type = inst.operands[i].type; |
723 | 253k | assert(type != SPV_OPERAND_TYPE_NONE); |
724 | 253k | if (type == SPV_OPERAND_TYPE_RESULT_ID) continue; |
725 | 197k | line << " "; |
726 | 197k | EmitOperand(line, inst, i); |
727 | 197k | } |
728 | | |
729 | | // For the sake of comment generation, store information from some |
730 | | // instructions for the future. |
731 | 83.8k | if (comment_) { |
732 | 0 | GenerateCommentForDecoratedId(inst); |
733 | 0 | } |
734 | | |
735 | 83.8k | std::ostringstream comments; |
736 | 83.8k | const char* comment_separator = ""; |
737 | | |
738 | 83.8k | if (show_byte_offset_) { |
739 | 0 | SetGrey(comments); |
740 | 0 | auto saved_flags = comments.flags(); |
741 | 0 | auto saved_fill = comments.fill(); |
742 | 0 | comments << comment_separator << "0x" << std::setw(8) << std::hex |
743 | 0 | << std::setfill('0') << inst_byte_offset; |
744 | 0 | comments.flags(saved_flags); |
745 | 0 | comments.fill(saved_fill); |
746 | 0 | ResetColor(comments); |
747 | 0 | comment_separator = ", "; |
748 | 0 | } |
749 | | |
750 | 83.8k | if (comment_ && opcode == spv::Op::OpName) { |
751 | 0 | const spv_parsed_operand_t& operand = inst.operands[0]; |
752 | 0 | const uint32_t word = inst.words[operand.offset]; |
753 | 0 | comments << comment_separator << "id %" << word; |
754 | 0 | comment_separator = ", "; |
755 | 0 | } |
756 | | |
757 | 83.8k | if (comment_ && inst.result_id && id_comments_.count(inst.result_id) > 0) { |
758 | 0 | comments << comment_separator << id_comments_[inst.result_id].str(); |
759 | 0 | comment_separator = ", "; |
760 | 0 | } |
761 | | |
762 | 83.8k | stream_ << line.str(); |
763 | | |
764 | 83.8k | if (!comments.str().empty()) { |
765 | | // Align the comments |
766 | 0 | const uint32_t line_length = GetLineLengthWithoutColor(line.str()); |
767 | 0 | uint32_t align = std::max( |
768 | 0 | {line_length + 2, last_instruction_comment_alignment_, kCommentColumn}); |
769 | | // Round up the alignment to a multiple of 4 for more niceness. |
770 | 0 | align = (align + 3) & ~0x3u; |
771 | 0 | last_instruction_comment_alignment_ = std::min({align, 256u}); |
772 | |
|
773 | 0 | stream_ << std::string(align - line_length, ' ') << "; " << comments.str(); |
774 | 83.8k | } else { |
775 | 83.8k | last_instruction_comment_alignment_ = 0; |
776 | 83.8k | } |
777 | | |
778 | 83.8k | stream_ << "\n"; |
779 | 83.8k | } |
780 | | |
781 | | void InstructionDisassembler::GenerateCommentForDecoratedId( |
782 | 0 | const spv_parsed_instruction_t& inst) { |
783 | 0 | assert(comment_); |
784 | 0 | auto opcode = static_cast<spv::Op>(inst.opcode); |
785 | |
|
786 | 0 | std::ostringstream partial; |
787 | 0 | uint32_t id = 0; |
788 | 0 | const char* separator = ""; |
789 | |
|
790 | 0 | switch (opcode) { |
791 | 0 | case spv::Op::OpDecorate: |
792 | | // Take everything after `OpDecorate %id` and associate it with id. |
793 | 0 | id = inst.words[inst.operands[0].offset]; |
794 | 0 | for (uint16_t i = 1; i < inst.num_operands; i++) { |
795 | 0 | partial << separator; |
796 | 0 | separator = " "; |
797 | 0 | EmitOperand(partial, inst, i); |
798 | 0 | } |
799 | 0 | break; |
800 | 0 | default: |
801 | 0 | break; |
802 | 0 | } |
803 | | |
804 | 0 | if (id == 0) { |
805 | 0 | return; |
806 | 0 | } |
807 | | |
808 | | // Add the new comment to the comments of this id |
809 | 0 | std::ostringstream& id_comment = id_comments_[id]; |
810 | 0 | if (!id_comment.str().empty()) { |
811 | 0 | id_comment << ", "; |
812 | 0 | } |
813 | 0 | id_comment << partial.str(); |
814 | 0 | } |
815 | | |
816 | | void InstructionDisassembler::EmitSectionComment( |
817 | | const spv_parsed_instruction_t& inst, bool& inserted_decoration_space, |
818 | 83.8k | bool& inserted_debug_space, bool& inserted_type_space) { |
819 | 83.8k | auto opcode = static_cast<spv::Op>(inst.opcode); |
820 | 83.8k | if (comment_ && opcode == spv::Op::OpFunction) { |
821 | 0 | stream_ << std::endl; |
822 | 0 | if (nested_indent_) { |
823 | | // Double the empty lines between Function sections since nested_indent_ |
824 | | // also separates blocks by a blank. |
825 | 0 | stream_ << std::endl; |
826 | 0 | } |
827 | 0 | stream_ << std::string(indent_, ' '); |
828 | 0 | stream_ << "; Function " << name_mapper_(inst.result_id) << std::endl; |
829 | 0 | } |
830 | 83.8k | if (comment_ && !inserted_decoration_space && spvOpcodeIsDecoration(opcode)) { |
831 | 0 | inserted_decoration_space = true; |
832 | 0 | stream_ << std::endl; |
833 | 0 | stream_ << std::string(indent_, ' '); |
834 | 0 | stream_ << "; Annotations" << std::endl; |
835 | 0 | } |
836 | 83.8k | if (comment_ && !inserted_debug_space && spvOpcodeIsDebug(opcode)) { |
837 | 0 | inserted_debug_space = true; |
838 | 0 | stream_ << std::endl; |
839 | 0 | stream_ << std::string(indent_, ' '); |
840 | 0 | stream_ << "; Debug Information" << std::endl; |
841 | 0 | } |
842 | 83.8k | if (comment_ && !inserted_type_space && spvOpcodeGeneratesType(opcode)) { |
843 | 0 | inserted_type_space = true; |
844 | 0 | stream_ << std::endl; |
845 | 0 | stream_ << std::string(indent_, ' '); |
846 | 0 | stream_ << "; Types, variables and constants" << std::endl; |
847 | 0 | } |
848 | 83.8k | } |
849 | | |
850 | | void InstructionDisassembler::EmitOperand(std::ostream& stream, |
851 | | const spv_parsed_instruction_t& inst, |
852 | 197k | const uint16_t operand_index) const { |
853 | 197k | assert(operand_index < inst.num_operands); |
854 | 197k | const spv_parsed_operand_t& operand = inst.operands[operand_index]; |
855 | 197k | const uint32_t word = inst.words[operand.offset]; |
856 | 197k | switch (operand.type) { |
857 | 0 | case SPV_OPERAND_TYPE_RESULT_ID: |
858 | 0 | assert(false && "<result-id> is not supposed to be handled here"); |
859 | 0 | SetBlue(stream); |
860 | 0 | stream << "%" << name_mapper_(word); |
861 | 0 | break; |
862 | 103k | case SPV_OPERAND_TYPE_ID: |
863 | 143k | case SPV_OPERAND_TYPE_TYPE_ID: |
864 | 144k | case SPV_OPERAND_TYPE_SCOPE_ID: |
865 | 144k | case SPV_OPERAND_TYPE_MEMORY_SEMANTICS_ID: |
866 | 144k | SetYellow(stream); |
867 | 144k | stream << "%" << name_mapper_(word); |
868 | 144k | break; |
869 | 962 | case SPV_OPERAND_TYPE_EXTENSION_INSTRUCTION_NUMBER: { |
870 | 962 | SetRed(stream); |
871 | 962 | const ExtInstDesc* desc = nullptr; |
872 | 962 | if (LookupExtInst(inst.ext_inst_type, word, &desc) == SPV_SUCCESS) { |
873 | 950 | stream << desc->name().data(); |
874 | 950 | } else { |
875 | 12 | if (!spvExtInstIsNonSemantic(inst.ext_inst_type)) { |
876 | 0 | assert(false && "should have caught this earlier"); |
877 | 12 | } else { |
878 | | // for non-semantic instruction sets we can just print the number |
879 | 12 | stream << word; |
880 | 12 | } |
881 | 12 | } |
882 | 962 | } break; |
883 | 19 | case SPV_OPERAND_TYPE_SPEC_CONSTANT_OP_NUMBER: { |
884 | 19 | const spvtools::InstructionDesc* opcodeEntry = nullptr; |
885 | 19 | if (LookupOpcode(spv::Op(word), &opcodeEntry)) |
886 | 0 | assert(false && "should have caught this earlier"); |
887 | 19 | SetRed(stream); |
888 | 19 | stream << opcodeEntry->name().data(); |
889 | 19 | } break; |
890 | 20.8k | case SPV_OPERAND_TYPE_LITERAL_INTEGER: |
891 | 24.6k | case SPV_OPERAND_TYPE_TYPED_LITERAL_NUMBER: |
892 | 24.6k | case SPV_OPERAND_TYPE_LITERAL_FLOAT: { |
893 | 24.6k | SetRed(stream); |
894 | 24.6k | EmitNumericLiteral(&stream, inst, operand); |
895 | 24.6k | ResetColor(stream); |
896 | 24.6k | } break; |
897 | 5.61k | case SPV_OPERAND_TYPE_LITERAL_STRING: { |
898 | 5.61k | stream << "\""; |
899 | 5.61k | SetGreen(stream); |
900 | | |
901 | 5.61k | std::string str = spvDecodeLiteralStringOperand(inst, operand_index); |
902 | 59.2k | for (char const& c : str) { |
903 | 59.2k | if (c == '"' || c == '\\') stream << '\\'; |
904 | 59.2k | stream << c; |
905 | 59.2k | } |
906 | 5.61k | ResetColor(stream); |
907 | 5.61k | stream << '"'; |
908 | 5.61k | } break; |
909 | 1.42k | case SPV_OPERAND_TYPE_CAPABILITY: |
910 | 1.42k | case SPV_OPERAND_TYPE_OPTIONAL_CAPABILITY: |
911 | 1.69k | case SPV_OPERAND_TYPE_SOURCE_LANGUAGE: |
912 | 2.58k | case SPV_OPERAND_TYPE_EXECUTION_MODEL: |
913 | 3.47k | case SPV_OPERAND_TYPE_ADDRESSING_MODEL: |
914 | 4.36k | case SPV_OPERAND_TYPE_MEMORY_MODEL: |
915 | 4.44k | case SPV_OPERAND_TYPE_EXECUTION_MODE: |
916 | 10.3k | case SPV_OPERAND_TYPE_STORAGE_CLASS: |
917 | 10.8k | case SPV_OPERAND_TYPE_DIMENSIONALITY: |
918 | 10.8k | case SPV_OPERAND_TYPE_SAMPLER_ADDRESSING_MODE: |
919 | 10.8k | case SPV_OPERAND_TYPE_SAMPLER_FILTER_MODE: |
920 | 11.3k | case SPV_OPERAND_TYPE_SAMPLER_IMAGE_FORMAT: |
921 | 11.3k | case SPV_OPERAND_TYPE_FP_ROUNDING_MODE: |
922 | 11.3k | case SPV_OPERAND_TYPE_LINKAGE_TYPE: |
923 | 11.3k | case SPV_OPERAND_TYPE_ACCESS_QUALIFIER: |
924 | 11.3k | case SPV_OPERAND_TYPE_FUNCTION_PARAMETER_ATTRIBUTE: |
925 | 17.7k | case SPV_OPERAND_TYPE_DECORATION: |
926 | 17.8k | case SPV_OPERAND_TYPE_BUILT_IN: |
927 | 17.8k | case SPV_OPERAND_TYPE_GROUP_OPERATION: |
928 | 17.8k | case SPV_OPERAND_TYPE_KERNEL_ENQ_FLAGS: |
929 | 17.8k | case SPV_OPERAND_TYPE_KERNEL_PROFILING_INFO: |
930 | 17.8k | case SPV_OPERAND_TYPE_RAY_FLAGS: |
931 | 17.8k | case SPV_OPERAND_TYPE_RAY_QUERY_INTERSECTION: |
932 | 17.8k | case SPV_OPERAND_TYPE_RAY_QUERY_COMMITTED_INTERSECTION_TYPE: |
933 | 17.8k | case SPV_OPERAND_TYPE_RAY_QUERY_CANDIDATE_INTERSECTION_TYPE: |
934 | 17.8k | case SPV_OPERAND_TYPE_DEBUG_BASE_TYPE_ATTRIBUTE_ENCODING: |
935 | 17.8k | case SPV_OPERAND_TYPE_DEBUG_COMPOSITE_TYPE: |
936 | 17.8k | case SPV_OPERAND_TYPE_DEBUG_TYPE_QUALIFIER: |
937 | 17.8k | case SPV_OPERAND_TYPE_DEBUG_OPERATION: |
938 | 17.8k | case SPV_OPERAND_TYPE_CLDEBUG100_DEBUG_BASE_TYPE_ATTRIBUTE_ENCODING: |
939 | 17.8k | case SPV_OPERAND_TYPE_CLDEBUG100_DEBUG_COMPOSITE_TYPE: |
940 | 17.8k | case SPV_OPERAND_TYPE_CLDEBUG100_DEBUG_TYPE_QUALIFIER: |
941 | 17.8k | case SPV_OPERAND_TYPE_CLDEBUG100_DEBUG_OPERATION: |
942 | 17.8k | case SPV_OPERAND_TYPE_CLDEBUG100_DEBUG_IMPORTED_ENTITY: |
943 | 17.8k | case SPV_OPERAND_TYPE_FPDENORM_MODE: |
944 | 17.8k | case SPV_OPERAND_TYPE_FPOPERATION_MODE: |
945 | 17.8k | case SPV_OPERAND_TYPE_QUANTIZATION_MODES: |
946 | 17.8k | case SPV_OPERAND_TYPE_FPENCODING: |
947 | 17.8k | case SPV_OPERAND_TYPE_OVERFLOW_MODES: { |
948 | 17.8k | const spvtools::OperandDesc* entry = nullptr; |
949 | 17.8k | if (spvtools::LookupOperand(operand.type, word, &entry)) |
950 | 0 | assert(false && "should have caught this earlier"); |
951 | 17.8k | stream << entry->name().data(); |
952 | 17.8k | } break; |
953 | 0 | case SPV_OPERAND_TYPE_FP_FAST_MATH_MODE: |
954 | 949 | case SPV_OPERAND_TYPE_FUNCTION_CONTROL: |
955 | 1.49k | case SPV_OPERAND_TYPE_LOOP_CONTROL: |
956 | 2.00k | case SPV_OPERAND_TYPE_IMAGE: |
957 | 2.11k | case SPV_OPERAND_TYPE_MEMORY_ACCESS: |
958 | 3.58k | case SPV_OPERAND_TYPE_SELECTION_CONTROL: |
959 | 3.58k | case SPV_OPERAND_TYPE_DEBUG_INFO_FLAGS: |
960 | 3.58k | case SPV_OPERAND_TYPE_CLDEBUG100_DEBUG_INFO_FLAGS: |
961 | 3.58k | case SPV_OPERAND_TYPE_RAW_ACCESS_CHAIN_OPERANDS: |
962 | 3.58k | EmitMaskOperand(stream, operand.type, word); |
963 | 3.58k | break; |
964 | 61 | default: |
965 | 61 | if (spvOperandIsConcreteMask(operand.type)) { |
966 | 0 | EmitMaskOperand(stream, operand.type, word); |
967 | 61 | } else if (spvOperandIsConcrete(operand.type)) { |
968 | 61 | const spvtools::OperandDesc* entry = nullptr; |
969 | 61 | if (spvtools::LookupOperand(operand.type, word, &entry)) |
970 | 0 | assert(false && "should have caught this earlier"); |
971 | 61 | stream << entry->name().data(); |
972 | 61 | } else { |
973 | 0 | assert(false && "unhandled or invalid case"); |
974 | 0 | } |
975 | 61 | break; |
976 | 197k | } |
977 | 197k | ResetColor(stream); |
978 | 197k | } |
979 | | |
980 | | void InstructionDisassembler::EmitMaskOperand(std::ostream& stream, |
981 | | const spv_operand_type_t type, |
982 | 3.58k | const uint32_t word) const { |
983 | | // Scan the mask from least significant bit to most significant bit. For each |
984 | | // set bit, emit the name of that bit. Separate multiple names with '|'. |
985 | 3.58k | uint32_t remaining_word = word; |
986 | 3.58k | uint32_t mask; |
987 | 3.58k | int num_emitted = 0; |
988 | 4.95k | for (mask = 1; remaining_word; mask <<= 1) { |
989 | 1.37k | if (remaining_word & mask) { |
990 | 679 | remaining_word ^= mask; |
991 | 679 | const spvtools::OperandDesc* entry = nullptr; |
992 | 679 | if (spvtools::LookupOperand(type, mask, &entry)) |
993 | 0 | assert(false && "should have caught this earlier"); |
994 | 679 | if (num_emitted) stream << "|"; |
995 | 679 | stream << entry->name().data(); |
996 | 679 | num_emitted++; |
997 | 679 | } |
998 | 1.37k | } |
999 | 3.58k | if (!num_emitted) { |
1000 | | // An operand value of 0 was provided, so represent it by the name |
1001 | | // of the 0 value. In many cases, that's "None". |
1002 | 2.93k | const spvtools::OperandDesc* entry = nullptr; |
1003 | 2.93k | if (SPV_SUCCESS == spvtools::LookupOperand(type, 0, &entry)) |
1004 | 2.93k | stream << entry->name().data(); |
1005 | 2.93k | } |
1006 | 3.58k | } |
1007 | | |
1008 | 283k | void InstructionDisassembler::ResetColor(std::ostream& stream) const { |
1009 | 283k | if (color_) stream << spvtools::clr::reset{print_}; |
1010 | 283k | } |
1011 | 0 | void InstructionDisassembler::SetGrey(std::ostream& stream) const { |
1012 | 0 | if (color_) stream << spvtools::clr::grey{print_}; |
1013 | 0 | } |
1014 | 56.0k | void InstructionDisassembler::SetBlue(std::ostream& stream) const { |
1015 | 56.0k | if (color_) stream << spvtools::clr::blue{print_}; |
1016 | 56.0k | } |
1017 | 144k | void InstructionDisassembler::SetYellow(std::ostream& stream) const { |
1018 | 144k | if (color_) stream << spvtools::clr::yellow{print_}; |
1019 | 144k | } |
1020 | 25.5k | void InstructionDisassembler::SetRed(std::ostream& stream) const { |
1021 | 25.5k | if (color_) stream << spvtools::clr::red{print_}; |
1022 | 25.5k | } |
1023 | 5.61k | void InstructionDisassembler::SetGreen(std::ostream& stream) const { |
1024 | 5.61k | if (color_) stream << spvtools::clr::green{print_}; |
1025 | 5.61k | } |
1026 | | |
1027 | 56.0k | void InstructionDisassembler::ResetColor() { ResetColor(stream_); } |
1028 | 0 | void InstructionDisassembler::SetGrey() { SetGrey(stream_); } |
1029 | 56.0k | void InstructionDisassembler::SetBlue() { SetBlue(stream_); } |
1030 | 0 | void InstructionDisassembler::SetYellow() { SetYellow(stream_); } |
1031 | 0 | void InstructionDisassembler::SetRed() { SetRed(stream_); } |
1032 | 0 | void InstructionDisassembler::SetGreen() { SetGreen(stream_); } |
1033 | | } // namespace disassemble |
1034 | | |
1035 | | std::string spvInstructionBinaryToText(const spv_target_env env, |
1036 | | const uint32_t* instCode, |
1037 | | const size_t instWordCount, |
1038 | | const uint32_t* code, |
1039 | | const size_t wordCount, |
1040 | 74 | const uint32_t options) { |
1041 | 74 | spv_context context = spvContextCreate(env); |
1042 | | |
1043 | | // Generate friendly names for Ids if requested. |
1044 | 74 | std::unique_ptr<FriendlyNameMapper> friendly_mapper; |
1045 | 74 | NameMapper name_mapper = GetTrivialNameMapper(); |
1046 | 74 | if (options & SPV_BINARY_TO_TEXT_OPTION_FRIENDLY_NAMES) { |
1047 | 74 | friendly_mapper = MakeUnique<FriendlyNameMapper>(context, code, wordCount); |
1048 | 74 | name_mapper = friendly_mapper->GetNameMapper(); |
1049 | 74 | } |
1050 | | |
1051 | | // Now disassemble! |
1052 | 74 | Disassembler disassembler(options, name_mapper); |
1053 | 74 | WrappedDisassembler wrapped(&disassembler, instCode, instWordCount); |
1054 | 74 | spvBinaryParse(context, &wrapped, code, wordCount, DisassembleTargetHeader, |
1055 | 74 | DisassembleTargetInstruction, nullptr); |
1056 | | |
1057 | 74 | spv_text text = nullptr; |
1058 | 74 | std::string output; |
1059 | 74 | if (disassembler.SaveTextResult(&text) == SPV_SUCCESS) { |
1060 | 74 | output.assign(text->str, text->str + text->length); |
1061 | | // Drop trailing newline characters. |
1062 | 148 | while (!output.empty() && output.back() == '\n') output.pop_back(); |
1063 | 74 | } |
1064 | 74 | spvTextDestroy(text); |
1065 | 74 | spvContextDestroy(context); |
1066 | | |
1067 | 74 | return output; |
1068 | 74 | } |
1069 | | } // namespace spvtools |
1070 | | |
1071 | | spv_result_t spvBinaryToText(const spv_const_context context, |
1072 | | const uint32_t* code, const size_t wordCount, |
1073 | | const uint32_t options, spv_text* pText, |
1074 | 890 | spv_diagnostic* pDiagnostic) { |
1075 | 890 | spv_context_t hijack_context = *context; |
1076 | 890 | if (pDiagnostic) { |
1077 | 0 | *pDiagnostic = nullptr; |
1078 | 0 | spvtools::UseDiagnosticAsMessageConsumer(&hijack_context, pDiagnostic); |
1079 | 0 | } |
1080 | | |
1081 | | // Generate friendly names for Ids if requested. |
1082 | 890 | std::unique_ptr<spvtools::FriendlyNameMapper> friendly_mapper; |
1083 | 890 | spvtools::NameMapper name_mapper = spvtools::GetTrivialNameMapper(); |
1084 | 890 | if (options & SPV_BINARY_TO_TEXT_OPTION_FRIENDLY_NAMES) { |
1085 | 890 | friendly_mapper = spvtools::MakeUnique<spvtools::FriendlyNameMapper>( |
1086 | 890 | &hijack_context, code, wordCount); |
1087 | 890 | name_mapper = friendly_mapper->GetNameMapper(); |
1088 | 890 | } |
1089 | | |
1090 | | // Now disassemble! |
1091 | 890 | spvtools::Disassembler disassembler(options, name_mapper); |
1092 | 890 | if (auto error = |
1093 | 890 | spvBinaryParse(&hijack_context, &disassembler, code, wordCount, |
1094 | 890 | spvtools::DisassembleHeader, |
1095 | 890 | spvtools::DisassembleInstruction, pDiagnostic)) { |
1096 | 1 | return error; |
1097 | 1 | } |
1098 | | |
1099 | 889 | return disassembler.SaveTextResult(pText); |
1100 | 890 | } |