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