summaryrefslogtreecommitdiff
path: root/src/shader_recompiler/frontend/maxwell/program.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/shader_recompiler/frontend/maxwell/program.cpp')
-rw-r--r--src/shader_recompiler/frontend/maxwell/program.cpp69
1 files changed, 69 insertions, 0 deletions
diff --git a/src/shader_recompiler/frontend/maxwell/program.cpp b/src/shader_recompiler/frontend/maxwell/program.cpp
new file mode 100644
index 000000000..67a98ba57
--- /dev/null
+++ b/src/shader_recompiler/frontend/maxwell/program.cpp
@@ -0,0 +1,69 @@
1// Copyright 2021 yuzu Emulator Project
2// Licensed under GPLv2 or any later version
3// Refer to the license.txt file included.
4
5#include <algorithm>
6#include <memory>
7
8#include "shader_recompiler/frontend/maxwell/program.h"
9#include "shader_recompiler/frontend/maxwell/termination_code.h"
10#include "shader_recompiler/frontend/maxwell/translate/translate.h"
11
12namespace Shader::Maxwell {
13
14Program::Function::~Function() {
15 std::ranges::for_each(blocks, &std::destroy_at<IR::Block>);
16}
17
18Program::Program(Environment& env, const Flow::CFG& cfg) {
19 std::vector<IR::Block*> block_map;
20 functions.reserve(cfg.Functions().size());
21
22 for (const Flow::Function& cfg_function : cfg.Functions()) {
23 Function& function{functions.emplace_back()};
24
25 const size_t num_blocks{cfg_function.blocks.size()};
26 IR::Block* block_memory{block_alloc_pool.allocate(num_blocks)};
27 function.blocks.reserve(num_blocks);
28
29 block_map.resize(cfg_function.blocks_data.size());
30
31 // Visit the instructions of all blocks
32 for (const Flow::BlockId block_id : cfg_function.blocks) {
33 const Flow::Block& flow_block{cfg_function.blocks_data[block_id]};
34
35 IR::Block* const block{std::construct_at(block_memory, Translate(env, flow_block))};
36 ++block_memory;
37 function.blocks.push_back(block);
38 block_map[flow_block.id] = block;
39 }
40 // Now that all blocks are defined, emit the termination instructions
41 for (const Flow::BlockId block_id : cfg_function.blocks) {
42 const Flow::Block& flow_block{cfg_function.blocks_data[block_id]};
43 EmitTerminationCode(flow_block, block_map);
44 }
45 }
46}
47
48std::string DumpProgram(const Program& program) {
49 size_t index{0};
50 std::map<const IR::Inst*, size_t> inst_to_index;
51 std::map<const IR::Block*, size_t> block_to_index;
52
53 for (const Program::Function& function : program.functions) {
54 for (const IR::Block* const block : function.blocks) {
55 block_to_index.emplace(block, index);
56 ++index;
57 }
58 }
59 std::string ret;
60 for (const Program::Function& function : program.functions) {
61 ret += fmt::format("Function\n");
62 for (const IR::Block* const block : function.blocks) {
63 ret += IR::DumpBlock(*block, block_to_index, inst_to_index, index) + '\n';
64 }
65 }
66 return ret;
67}
68
69} // namespace Shader::Maxwell