suyu/src/video_core/shader/ast.h

399 lines
8.8 KiB
C++
Raw Normal View History

2019-06-27 06:39:40 +02:00
// Copyright 2019 yuzu Emulator Project
// Licensed under GPLv2 or any later version
// Refer to the license.txt file included.
#pragma once
#include <functional>
2019-06-27 06:39:40 +02:00
#include <list>
#include <memory>
#include <optional>
#include <string>
#include <unordered_map>
#include <vector>
#include "video_core/shader/expr.h"
#include "video_core/shader/node.h"
namespace VideoCommon::Shader {
class ASTBase;
class ASTBlockDecoded;
class ASTBlockEncoded;
class ASTBreak;
class ASTDoWhile;
2019-06-27 06:39:40 +02:00
class ASTGoto;
class ASTIfElse;
class ASTIfThen;
2019-06-27 06:39:40 +02:00
class ASTLabel;
class ASTProgram;
2019-06-27 06:39:40 +02:00
class ASTReturn;
class ASTVarSet;
2019-06-27 06:39:40 +02:00
using ASTData = std::variant<ASTProgram, ASTIfThen, ASTIfElse, ASTBlockEncoded, ASTBlockDecoded,
ASTVarSet, ASTGoto, ASTLabel, ASTDoWhile, ASTReturn, ASTBreak>;
2019-06-27 06:39:40 +02:00
using ASTNode = std::shared_ptr<ASTBase>;
2019-06-28 00:57:47 +02:00
enum class ASTZipperType : u32 {
Program,
IfThen,
IfElse,
Loop,
};
class ASTZipper final {
public:
explicit ASTZipper();
void Init(ASTNode first, ASTNode parent);
2019-06-28 00:57:47 +02:00
ASTNode GetFirst() const {
2019-06-28 00:57:47 +02:00
return first;
}
ASTNode GetLast() const {
2019-06-28 00:57:47 +02:00
return last;
}
void PushBack(ASTNode new_node);
void PushFront(ASTNode new_node);
void InsertAfter(ASTNode new_node, ASTNode at_node);
void InsertBefore(ASTNode new_node, ASTNode at_node);
2019-06-28 00:57:47 +02:00
void DetachTail(ASTNode node);
void DetachSingle(ASTNode node);
void DetachSegment(ASTNode start, ASTNode end);
void Remove(ASTNode node);
ASTNode first;
ASTNode last;
2019-06-28 00:57:47 +02:00
};
2019-06-27 06:39:40 +02:00
class ASTProgram {
public:
ASTZipper nodes{};
2019-06-27 06:39:40 +02:00
};
2019-06-28 00:57:47 +02:00
class ASTIfThen {
2019-06-27 06:39:40 +02:00
public:
explicit ASTIfThen(Expr condition_) : condition{std::move(condition_)} {}
2019-06-27 06:39:40 +02:00
Expr condition;
ASTZipper nodes{};
2019-06-28 00:57:47 +02:00
};
class ASTIfElse {
public:
ASTZipper nodes{};
2019-06-27 06:39:40 +02:00
};
class ASTBlockEncoded {
public:
explicit ASTBlockEncoded(u32 start_, u32 _) : start{start_}, end{_} {}
2019-06-27 06:39:40 +02:00
u32 start;
u32 end;
};
class ASTBlockDecoded {
public:
explicit ASTBlockDecoded(NodeBlock&& new_nodes_) : nodes(std::move(new_nodes_)) {}
NodeBlock nodes;
};
2019-06-27 06:39:40 +02:00
class ASTVarSet {
public:
explicit ASTVarSet(u32 index_, Expr condition_)
: index{index_}, condition{std::move(condition_)} {}
2019-06-27 06:39:40 +02:00
u32 index;
Expr condition;
};
class ASTLabel {
public:
explicit ASTLabel(u32 index_) : index{index_} {}
2019-06-27 06:39:40 +02:00
u32 index;
bool unused{};
2019-06-27 06:39:40 +02:00
};
class ASTGoto {
public:
explicit ASTGoto(Expr condition_, u32 label_)
: condition{std::move(condition_)}, label{label_} {}
2019-06-27 06:39:40 +02:00
Expr condition;
u32 label;
};
class ASTDoWhile {
public:
explicit ASTDoWhile(Expr condition_) : condition{std::move(condition_)} {}
2019-06-27 06:39:40 +02:00
Expr condition;
ASTZipper nodes{};
2019-06-27 06:39:40 +02:00
};
class ASTReturn {
public:
explicit ASTReturn(Expr condition_, bool kills_)
: condition{std::move(condition_)}, kills{kills_} {}
2019-06-27 06:39:40 +02:00
Expr condition;
bool kills;
};
2019-06-28 00:57:47 +02:00
class ASTBreak {
public:
explicit ASTBreak(Expr condition_) : condition{std::move(condition_)} {}
2019-06-28 00:57:47 +02:00
Expr condition;
};
2019-06-27 06:39:40 +02:00
class ASTBase {
public:
explicit ASTBase(ASTNode parent_, ASTData data_)
: data{std::move(data_)}, parent{std::move(parent_)} {}
2019-06-27 06:39:40 +02:00
template <class U, class... Args>
static ASTNode Make(ASTNode parent, Args&&... args) {
return std::make_shared<ASTBase>(std::move(parent),
ASTData(U(std::forward<Args>(args)...)));
2019-06-27 06:39:40 +02:00
}
void SetParent(ASTNode new_parent) {
parent = std::move(new_parent);
2019-06-27 06:39:40 +02:00
}
ASTNode& GetParent() {
return parent;
}
const ASTNode& GetParent() const {
return parent;
}
u32 GetLevel() const {
u32 level = 0;
2019-06-28 00:57:47 +02:00
auto next_parent = parent;
while (next_parent) {
next_parent = next_parent->GetParent();
2019-06-27 06:39:40 +02:00
level++;
}
return level;
}
ASTData* GetInnerData() {
return &data;
}
const ASTData* GetInnerData() const {
return &data;
}
ASTNode GetNext() const {
2019-06-28 00:57:47 +02:00
return next;
}
ASTNode GetPrevious() const {
2019-06-28 00:57:47 +02:00
return previous;
}
ASTZipper& GetManager() {
return *manager;
}
const ASTZipper& GetManager() const {
return *manager;
}
std::optional<u32> GetGotoLabel() const {
if (const auto* inner = std::get_if<ASTGoto>(&data)) {
return {inner->label};
2019-06-28 00:57:47 +02:00
}
return std::nullopt;
2019-06-28 00:57:47 +02:00
}
Expr GetGotoCondition() const {
if (const auto* inner = std::get_if<ASTGoto>(&data)) {
2019-06-28 00:57:47 +02:00
return inner->condition;
}
return nullptr;
}
void MarkLabelUnused() {
if (auto* inner = std::get_if<ASTLabel>(&data)) {
inner->unused = true;
}
}
bool IsLabelUnused() const {
if (const auto* inner = std::get_if<ASTLabel>(&data)) {
return inner->unused;
}
return true;
}
std::optional<u32> GetLabelIndex() const {
if (const auto* inner = std::get_if<ASTLabel>(&data)) {
return {inner->index};
}
return std::nullopt;
}
Expr GetIfCondition() const {
if (const auto* inner = std::get_if<ASTIfThen>(&data)) {
return inner->condition;
}
return nullptr;
}
2019-06-28 00:57:47 +02:00
void SetGotoCondition(Expr new_condition) {
if (auto* inner = std::get_if<ASTGoto>(&data)) {
inner->condition = std::move(new_condition);
2019-06-28 00:57:47 +02:00
}
}
bool IsIfThen() const {
return std::holds_alternative<ASTIfThen>(data);
}
bool IsIfElse() const {
return std::holds_alternative<ASTIfElse>(data);
}
bool IsBlockEncoded() const {
return std::holds_alternative<ASTBlockEncoded>(data);
}
2019-10-04 23:23:16 +02:00
void TransformBlockEncoded(NodeBlock&& nodes) {
data = ASTBlockDecoded(std::move(nodes));
}
2019-06-28 00:57:47 +02:00
bool IsLoop() const {
return std::holds_alternative<ASTDoWhile>(data);
}
ASTZipper* GetSubNodes() {
if (std::holds_alternative<ASTProgram>(data)) {
return &std::get_if<ASTProgram>(&data)->nodes;
}
if (std::holds_alternative<ASTIfThen>(data)) {
return &std::get_if<ASTIfThen>(&data)->nodes;
}
if (std::holds_alternative<ASTIfElse>(data)) {
return &std::get_if<ASTIfElse>(&data)->nodes;
}
if (std::holds_alternative<ASTDoWhile>(data)) {
return &std::get_if<ASTDoWhile>(&data)->nodes;
}
return nullptr;
}
void Clear() {
next.reset();
previous.reset();
parent.reset();
manager = nullptr;
}
2019-06-27 06:39:40 +02:00
private:
2019-06-28 00:57:47 +02:00
friend class ASTZipper;
2019-06-27 06:39:40 +02:00
ASTData data;
ASTNode parent;
ASTNode next;
ASTNode previous;
2019-06-28 00:57:47 +02:00
ASTZipper* manager{};
2019-06-27 06:39:40 +02:00
};
class ASTManager final {
public:
explicit ASTManager(bool do_full_decompile, bool disable_else_derivation_);
~ASTManager();
2019-06-27 06:39:40 +02:00
ASTManager(const ASTManager& o) = delete;
ASTManager& operator=(const ASTManager& other) = delete;
2019-06-27 06:39:40 +02:00
ASTManager(ASTManager&& other) noexcept = default;
ASTManager& operator=(ASTManager&& other) noexcept = default;
2019-06-27 06:39:40 +02:00
void Init();
2019-06-27 06:39:40 +02:00
void DeclareLabel(u32 address);
2019-06-27 06:39:40 +02:00
void InsertLabel(u32 address);
void InsertGoto(Expr condition, u32 address);
void InsertBlock(u32 start_address, u32 end_address);
void InsertReturn(Expr condition, bool kills);
2019-06-27 06:39:40 +02:00
std::string Print() const;
2019-06-27 06:39:40 +02:00
2019-06-28 00:57:47 +02:00
void Decompile();
void ShowCurrentState(std::string_view state) const;
2019-06-28 00:57:47 +02:00
void SanityCheck() const;
void Clear();
bool IsFullyDecompiled() const {
if (full_decompile) {
return gotos.empty();
}
for (ASTNode goto_node : gotos) {
auto label_index = goto_node->GetGotoLabel();
if (!label_index) {
return false;
}
ASTNode glabel = labels[*label_index];
if (IsBackwardsJump(goto_node, glabel)) {
return false;
}
}
return true;
}
2019-06-27 06:39:40 +02:00
ASTNode GetProgram() const {
return main_node;
}
u32 GetVariables() const {
return variables;
}
const std::vector<ASTNode>& GetLabels() const {
return labels;
}
2019-06-27 06:39:40 +02:00
private:
bool IsBackwardsJump(ASTNode goto_node, ASTNode label_node) const;
bool IndirectlyRelated(const ASTNode& first, const ASTNode& second) const;
2019-06-28 00:57:47 +02:00
bool DirectlyRelated(const ASTNode& first, const ASTNode& second) const;
2019-06-28 00:57:47 +02:00
void EncloseDoWhile(ASTNode goto_node, ASTNode label);
void EncloseIfThen(ASTNode goto_node, ASTNode label);
void MoveOutward(ASTNode goto_node);
2019-06-28 00:57:47 +02:00
u32 NewVariable() {
return variables++;
2019-06-28 00:57:47 +02:00
}
bool full_decompile{};
bool disable_else_derivation{};
2019-06-27 06:39:40 +02:00
std::unordered_map<u32, u32> labels_map{};
u32 labels_count{};
std::vector<ASTNode> labels{};
std::list<ASTNode> gotos{};
u32 variables{};
ASTProgram* program{};
ASTNode main_node{};
Expr false_condition{};
2019-06-27 06:39:40 +02:00
};
} // namespace VideoCommon::Shader