Merge pull request #2994 from lioncash/fmt

video_core/shader/ast: Minor changes to ASTPrinter
This commit is contained in:
Rodrigo Locatti 2019-10-18 01:05:25 -03:00 committed by GitHub
commit dc5eedef71
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 51 additions and 40 deletions

View File

@ -3,6 +3,9 @@
// Refer to the license.txt file included. // Refer to the license.txt file included.
#include <string> #include <string>
#include <string_view>
#include <fmt/format.h>
#include "common/assert.h" #include "common/assert.h"
#include "common/common_types.h" #include "common/common_types.h"
@ -229,7 +232,8 @@ public:
return inner; return inner;
} }
std::string inner{}; private:
std::string inner;
}; };
class ASTPrinter { class ASTPrinter {
@ -249,7 +253,7 @@ public:
void operator()(const ASTIfThen& ast) { void operator()(const ASTIfThen& ast) {
ExprPrinter expr_parser{}; ExprPrinter expr_parser{};
std::visit(expr_parser, *ast.condition); std::visit(expr_parser, *ast.condition);
inner += Ident() + "if (" + expr_parser.GetResult() + ") {\n"; inner += fmt::format("{}if ({}) {{\n", Indent(), expr_parser.GetResult());
scope++; scope++;
ASTNode current = ast.nodes.GetFirst(); ASTNode current = ast.nodes.GetFirst();
while (current) { while (current) {
@ -257,11 +261,13 @@ public:
current = current->GetNext(); current = current->GetNext();
} }
scope--; scope--;
inner += Ident() + "}\n"; inner += fmt::format("{}}}\n", Indent());
} }
void operator()(const ASTIfElse& ast) { void operator()(const ASTIfElse& ast) {
inner += Ident() + "else {\n"; inner += Indent();
inner += "else {\n";
scope++; scope++;
ASTNode current = ast.nodes.GetFirst(); ASTNode current = ast.nodes.GetFirst();
while (current) { while (current) {
@ -269,40 +275,41 @@ public:
current = current->GetNext(); current = current->GetNext();
} }
scope--; scope--;
inner += Ident() + "}\n";
inner += Indent();
inner += "}\n";
} }
void operator()(const ASTBlockEncoded& ast) { void operator()(const ASTBlockEncoded& ast) {
inner += Ident() + "Block(" + std::to_string(ast.start) + ", " + std::to_string(ast.end) + inner += fmt::format("{}Block({}, {});\n", Indent(), ast.start, ast.end);
");\n";
} }
void operator()(const ASTBlockDecoded& ast) { void operator()([[maybe_unused]] const ASTBlockDecoded& ast) {
inner += Ident() + "Block;\n"; inner += Indent();
inner += "Block;\n";
} }
void operator()(const ASTVarSet& ast) { void operator()(const ASTVarSet& ast) {
ExprPrinter expr_parser{}; ExprPrinter expr_parser{};
std::visit(expr_parser, *ast.condition); std::visit(expr_parser, *ast.condition);
inner += inner += fmt::format("{}V{} := {};\n", Indent(), ast.index, expr_parser.GetResult());
Ident() + "V" + std::to_string(ast.index) + " := " + expr_parser.GetResult() + ";\n";
} }
void operator()(const ASTLabel& ast) { void operator()(const ASTLabel& ast) {
inner += "Label_" + std::to_string(ast.index) + ":\n"; inner += fmt::format("Label_{}:\n", ast.index);
} }
void operator()(const ASTGoto& ast) { void operator()(const ASTGoto& ast) {
ExprPrinter expr_parser{}; ExprPrinter expr_parser{};
std::visit(expr_parser, *ast.condition); std::visit(expr_parser, *ast.condition);
inner += Ident() + "(" + expr_parser.GetResult() + ") -> goto Label_" + inner +=
std::to_string(ast.label) + ";\n"; fmt::format("{}({}) -> goto Label_{};\n", Indent(), expr_parser.GetResult(), ast.label);
} }
void operator()(const ASTDoWhile& ast) { void operator()(const ASTDoWhile& ast) {
ExprPrinter expr_parser{}; ExprPrinter expr_parser{};
std::visit(expr_parser, *ast.condition); std::visit(expr_parser, *ast.condition);
inner += Ident() + "do {\n"; inner += fmt::format("{}do {{\n", Indent());
scope++; scope++;
ASTNode current = ast.nodes.GetFirst(); ASTNode current = ast.nodes.GetFirst();
while (current) { while (current) {
@ -310,32 +317,23 @@ public:
current = current->GetNext(); current = current->GetNext();
} }
scope--; scope--;
inner += Ident() + "} while (" + expr_parser.GetResult() + ");\n"; inner += fmt::format("{}}} while ({});\n", Indent(), expr_parser.GetResult());
} }
void operator()(const ASTReturn& ast) { void operator()(const ASTReturn& ast) {
ExprPrinter expr_parser{}; ExprPrinter expr_parser{};
std::visit(expr_parser, *ast.condition); std::visit(expr_parser, *ast.condition);
inner += Ident() + "(" + expr_parser.GetResult() + ") -> " + inner += fmt::format("{}({}) -> {};\n", Indent(), expr_parser.GetResult(),
(ast.kills ? "discard" : "exit") + ";\n"; ast.kills ? "discard" : "exit");
} }
void operator()(const ASTBreak& ast) { void operator()(const ASTBreak& ast) {
ExprPrinter expr_parser{}; ExprPrinter expr_parser{};
std::visit(expr_parser, *ast.condition); std::visit(expr_parser, *ast.condition);
inner += Ident() + "(" + expr_parser.GetResult() + ") -> break;\n"; inner += fmt::format("{}({}) -> break;\n", Indent(), expr_parser.GetResult());
} }
std::string& Ident() { void Visit(const ASTNode& node) {
if (memo_scope == scope) {
return tabs_memo;
}
tabs_memo = tabs.substr(0, scope * 2);
memo_scope = scope;
return tabs_memo;
}
void Visit(ASTNode& node) {
std::visit(*this, *node->GetInnerData()); std::visit(*this, *node->GetInnerData());
} }
@ -344,16 +342,29 @@ public:
} }
private: private:
std::string_view Indent() {
if (space_segment_scope == scope) {
return space_segment;
}
// Ensure that we don't exceed our view.
ASSERT(scope * 2 < spaces.size());
space_segment = spaces.substr(0, scope * 2);
space_segment_scope = scope;
return space_segment;
}
std::string inner{}; std::string inner{};
std::string_view space_segment;
u32 scope{}; u32 scope{};
u32 space_segment_scope{};
std::string tabs_memo{}; static constexpr std::string_view spaces{" "};
u32 memo_scope{};
static constexpr std::string_view tabs{" "};
}; };
std::string ASTManager::Print() { std::string ASTManager::Print() const {
ASTPrinter printer{}; ASTPrinter printer{};
printer.Visit(main_node); printer.Visit(main_node);
return printer.GetResult(); return printer.GetResult();
@ -549,13 +560,13 @@ bool ASTManager::DirectlyRelated(const ASTNode& first, const ASTNode& second) co
return min->GetParent() == max->GetParent(); return min->GetParent() == max->GetParent();
} }
void ASTManager::ShowCurrentState(std::string_view state) { void ASTManager::ShowCurrentState(std::string_view state) const {
LOG_CRITICAL(HW_GPU, "\nState {}:\n\n{}\n", state, Print()); LOG_CRITICAL(HW_GPU, "\nState {}:\n\n{}\n", state, Print());
SanityCheck(); SanityCheck();
} }
void ASTManager::SanityCheck() { void ASTManager::SanityCheck() const {
for (auto& label : labels) { for (const auto& label : labels) {
if (!label->GetParent()) { if (!label->GetParent()) {
LOG_CRITICAL(HW_GPU, "Sanity Check Failed"); LOG_CRITICAL(HW_GPU, "Sanity Check Failed");
} }

View File

@ -328,13 +328,13 @@ public:
void InsertReturn(Expr condition, bool kills); void InsertReturn(Expr condition, bool kills);
std::string Print(); std::string Print() const;
void Decompile(); void Decompile();
void ShowCurrentState(std::string_view state); void ShowCurrentState(std::string_view state) const;
void SanityCheck(); void SanityCheck() const;
void Clear(); void Clear();