2019-06-27 04:39:40 +00:00
|
|
|
// Copyright 2019 yuzu Emulator Project
|
|
|
|
// Licensed under GPLv2 or any later version
|
|
|
|
// Refer to the license.txt file included.
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
2019-06-29 00:54:21 +00:00
|
|
|
#include <functional>
|
2019-06-27 04:39:40 +00: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 ASTProgram;
|
2019-06-27 22:57:47 +00:00
|
|
|
class ASTIfThen;
|
|
|
|
class ASTIfElse;
|
2019-06-27 04:39:40 +00:00
|
|
|
class ASTBlockEncoded;
|
2019-06-29 00:54:21 +00:00
|
|
|
class ASTBlockDecoded;
|
2019-06-27 04:39:40 +00:00
|
|
|
class ASTVarSet;
|
|
|
|
class ASTGoto;
|
|
|
|
class ASTLabel;
|
|
|
|
class ASTDoWhile;
|
|
|
|
class ASTReturn;
|
2019-06-27 22:57:47 +00:00
|
|
|
class ASTBreak;
|
2019-06-27 04:39:40 +00:00
|
|
|
|
2019-06-29 02:59:43 +00:00
|
|
|
using ASTData = std::variant<ASTProgram, ASTIfThen, ASTIfElse, ASTBlockEncoded, ASTBlockDecoded,
|
|
|
|
ASTVarSet, ASTGoto, ASTLabel, ASTDoWhile, ASTReturn, ASTBreak>;
|
2019-06-27 04:39:40 +00:00
|
|
|
|
|
|
|
using ASTNode = std::shared_ptr<ASTBase>;
|
|
|
|
|
2019-06-27 22:57:47 +00:00
|
|
|
enum class ASTZipperType : u32 {
|
|
|
|
Program,
|
|
|
|
IfThen,
|
|
|
|
IfElse,
|
|
|
|
Loop,
|
|
|
|
};
|
|
|
|
|
|
|
|
class ASTZipper final {
|
|
|
|
public:
|
|
|
|
ASTZipper();
|
2019-06-29 00:54:21 +00:00
|
|
|
|
|
|
|
void Init(ASTNode first, ASTNode parent);
|
2019-06-27 22:57:47 +00:00
|
|
|
|
|
|
|
ASTNode GetFirst() {
|
|
|
|
return first;
|
|
|
|
}
|
|
|
|
|
|
|
|
ASTNode GetLast() {
|
|
|
|
return last;
|
|
|
|
}
|
|
|
|
|
|
|
|
void PushBack(ASTNode new_node);
|
|
|
|
void PushFront(ASTNode new_node);
|
|
|
|
void InsertAfter(ASTNode new_node, ASTNode at_node);
|
2019-06-29 00:54:21 +00:00
|
|
|
void InsertBefore(ASTNode new_node, ASTNode at_node);
|
2019-06-27 22:57:47 +00: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-27 04:39:40 +00:00
|
|
|
class ASTProgram {
|
|
|
|
public:
|
2019-06-27 22:57:47 +00:00
|
|
|
ASTProgram() : nodes{} {};
|
|
|
|
ASTZipper nodes;
|
2019-06-27 04:39:40 +00:00
|
|
|
};
|
|
|
|
|
2019-06-27 22:57:47 +00:00
|
|
|
class ASTIfThen {
|
2019-06-27 04:39:40 +00:00
|
|
|
public:
|
2019-06-29 00:54:21 +00:00
|
|
|
ASTIfThen(Expr condition) : condition(condition), nodes{} {}
|
2019-06-27 04:39:40 +00:00
|
|
|
Expr condition;
|
2019-06-27 22:57:47 +00:00
|
|
|
ASTZipper nodes;
|
|
|
|
};
|
|
|
|
|
|
|
|
class ASTIfElse {
|
|
|
|
public:
|
2019-06-29 00:54:21 +00:00
|
|
|
ASTIfElse() : nodes{} {}
|
2019-06-27 22:57:47 +00:00
|
|
|
ASTZipper nodes;
|
2019-06-27 04:39:40 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
class ASTBlockEncoded {
|
|
|
|
public:
|
|
|
|
ASTBlockEncoded(u32 start, u32 end) : start{start}, end{end} {}
|
|
|
|
u32 start;
|
|
|
|
u32 end;
|
|
|
|
};
|
|
|
|
|
2019-06-29 00:54:21 +00:00
|
|
|
class ASTBlockDecoded {
|
|
|
|
public:
|
|
|
|
ASTBlockDecoded(NodeBlock& new_nodes) : nodes(std::move(new_nodes)) {}
|
|
|
|
NodeBlock nodes;
|
|
|
|
};
|
|
|
|
|
2019-06-27 04:39:40 +00:00
|
|
|
class ASTVarSet {
|
|
|
|
public:
|
|
|
|
ASTVarSet(u32 index, Expr condition) : index{index}, condition{condition} {}
|
|
|
|
u32 index;
|
|
|
|
Expr condition;
|
|
|
|
};
|
|
|
|
|
|
|
|
class ASTLabel {
|
|
|
|
public:
|
|
|
|
ASTLabel(u32 index) : index{index} {}
|
|
|
|
u32 index;
|
2019-08-21 15:54:47 +00:00
|
|
|
bool unused{};
|
2019-06-27 04:39:40 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
class ASTGoto {
|
|
|
|
public:
|
|
|
|
ASTGoto(Expr condition, u32 label) : condition{condition}, label{label} {}
|
|
|
|
Expr condition;
|
|
|
|
u32 label;
|
|
|
|
};
|
|
|
|
|
|
|
|
class ASTDoWhile {
|
|
|
|
public:
|
2019-06-29 00:54:21 +00:00
|
|
|
ASTDoWhile(Expr condition) : condition(condition), nodes{} {}
|
2019-06-27 04:39:40 +00:00
|
|
|
Expr condition;
|
2019-06-27 22:57:47 +00:00
|
|
|
ASTZipper nodes;
|
2019-06-27 04:39:40 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
class ASTReturn {
|
|
|
|
public:
|
|
|
|
ASTReturn(Expr condition, bool kills) : condition{condition}, kills{kills} {}
|
|
|
|
Expr condition;
|
|
|
|
bool kills;
|
|
|
|
};
|
|
|
|
|
2019-06-27 22:57:47 +00:00
|
|
|
class ASTBreak {
|
|
|
|
public:
|
|
|
|
ASTBreak(Expr condition) : condition{condition} {}
|
|
|
|
Expr condition;
|
|
|
|
};
|
|
|
|
|
2019-06-27 04:39:40 +00:00
|
|
|
class ASTBase {
|
|
|
|
public:
|
|
|
|
explicit ASTBase(ASTNode parent, ASTData data) : parent{parent}, data{data} {}
|
|
|
|
|
|
|
|
template <class U, class... Args>
|
|
|
|
static ASTNode Make(ASTNode parent, Args&&... args) {
|
|
|
|
return std::make_shared<ASTBase>(parent, ASTData(U(std::forward<Args>(args)...)));
|
|
|
|
}
|
|
|
|
|
|
|
|
void SetParent(ASTNode new_parent) {
|
|
|
|
parent = new_parent;
|
|
|
|
}
|
|
|
|
|
|
|
|
ASTNode& GetParent() {
|
|
|
|
return parent;
|
|
|
|
}
|
|
|
|
|
|
|
|
const ASTNode& GetParent() const {
|
|
|
|
return parent;
|
|
|
|
}
|
|
|
|
|
|
|
|
u32 GetLevel() const {
|
|
|
|
u32 level = 0;
|
2019-06-27 22:57:47 +00:00
|
|
|
auto next_parent = parent;
|
|
|
|
while (next_parent) {
|
|
|
|
next_parent = next_parent->GetParent();
|
2019-06-27 04:39:40 +00:00
|
|
|
level++;
|
|
|
|
}
|
|
|
|
return level;
|
|
|
|
}
|
|
|
|
|
|
|
|
ASTData* GetInnerData() {
|
|
|
|
return &data;
|
|
|
|
}
|
|
|
|
|
2019-06-27 22:57:47 +00:00
|
|
|
ASTNode GetNext() {
|
|
|
|
return next;
|
|
|
|
}
|
|
|
|
|
|
|
|
ASTNode GetPrevious() {
|
|
|
|
return previous;
|
|
|
|
}
|
|
|
|
|
|
|
|
ASTZipper& GetManager() {
|
|
|
|
return *manager;
|
|
|
|
}
|
|
|
|
|
|
|
|
u32 GetGotoLabel() const {
|
|
|
|
auto inner = std::get_if<ASTGoto>(&data);
|
|
|
|
if (inner) {
|
|
|
|
return inner->label;
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
Expr GetGotoCondition() const {
|
|
|
|
auto inner = std::get_if<ASTGoto>(&data);
|
|
|
|
if (inner) {
|
|
|
|
return inner->condition;
|
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2019-08-21 15:54:47 +00:00
|
|
|
void MarkLabelUnused() const {
|
|
|
|
auto inner = std::get_if<ASTLabel>(&data);
|
|
|
|
if (inner) {
|
|
|
|
inner->unused = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-29 00:54:21 +00:00
|
|
|
Expr GetIfCondition() const {
|
|
|
|
auto inner = std::get_if<ASTIfThen>(&data);
|
|
|
|
if (inner) {
|
|
|
|
return inner->condition;
|
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2019-06-27 22:57:47 +00:00
|
|
|
void SetGotoCondition(Expr new_condition) {
|
|
|
|
auto inner = std::get_if<ASTGoto>(&data);
|
|
|
|
if (inner) {
|
|
|
|
inner->condition = new_condition;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool IsIfThen() const {
|
|
|
|
return std::holds_alternative<ASTIfThen>(data);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool IsIfElse() const {
|
|
|
|
return std::holds_alternative<ASTIfElse>(data);
|
|
|
|
}
|
|
|
|
|
2019-06-29 00:54:21 +00:00
|
|
|
bool IsBlockEncoded() const {
|
|
|
|
return std::holds_alternative<ASTBlockEncoded>(data);
|
|
|
|
}
|
|
|
|
|
2019-06-29 05:44:07 +00:00
|
|
|
void TransformBlockEncoded(NodeBlock& nodes) {
|
2019-06-29 00:54:21 +00:00
|
|
|
data = ASTBlockDecoded(nodes);
|
|
|
|
}
|
|
|
|
|
2019-06-27 22:57:47 +00: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;
|
|
|
|
}
|
|
|
|
|
2019-06-29 02:59:43 +00:00
|
|
|
void Clear() {
|
|
|
|
next.reset();
|
|
|
|
previous.reset();
|
|
|
|
parent.reset();
|
|
|
|
manager = nullptr;
|
|
|
|
}
|
|
|
|
|
2019-06-27 04:39:40 +00:00
|
|
|
private:
|
2019-06-27 22:57:47 +00:00
|
|
|
friend class ASTZipper;
|
|
|
|
|
2019-06-27 04:39:40 +00:00
|
|
|
ASTData data;
|
|
|
|
ASTNode parent;
|
2019-06-27 22:57:47 +00:00
|
|
|
ASTNode next{};
|
|
|
|
ASTNode previous{};
|
|
|
|
ASTZipper* manager{};
|
2019-06-27 04:39:40 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
class ASTManager final {
|
|
|
|
public:
|
2019-08-16 20:25:02 +00:00
|
|
|
ASTManager(bool full_decompile);
|
2019-06-29 02:59:43 +00:00
|
|
|
~ASTManager();
|
2019-06-27 04:39:40 +00:00
|
|
|
|
2019-06-29 02:59:43 +00:00
|
|
|
ASTManager(const ASTManager& o) = delete;
|
|
|
|
ASTManager& operator=(const ASTManager& other) = delete;
|
2019-06-27 04:39:40 +00:00
|
|
|
|
2019-06-29 02:59:43 +00:00
|
|
|
ASTManager(ASTManager&& other);
|
|
|
|
ASTManager& operator=(ASTManager&& other);
|
2019-06-27 04:39:40 +00:00
|
|
|
|
2019-06-29 02:59:43 +00:00
|
|
|
void Init();
|
2019-06-27 04:39:40 +00:00
|
|
|
|
2019-06-29 02:59:43 +00:00
|
|
|
void DeclareLabel(u32 address);
|
2019-06-27 04:39:40 +00:00
|
|
|
|
2019-06-29 02:59:43 +00: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 04:39:40 +00:00
|
|
|
|
|
|
|
std::string Print();
|
|
|
|
|
2019-06-27 22:57:47 +00:00
|
|
|
void Decompile();
|
|
|
|
|
2019-06-29 00:54:21 +00:00
|
|
|
void ShowCurrentState(std::string state);
|
2019-06-27 22:57:47 +00:00
|
|
|
|
2019-06-29 00:54:21 +00:00
|
|
|
void SanityCheck();
|
|
|
|
|
2019-06-29 05:44:07 +00:00
|
|
|
bool IsFullyDecompiled() const {
|
2019-08-16 20:25:02 +00:00
|
|
|
if (full_decompile) {
|
|
|
|
return gotos.size() == 0;
|
|
|
|
} else {
|
|
|
|
for (ASTNode goto_node : gotos) {
|
|
|
|
u32 label_index = goto_node->GetGotoLabel();
|
|
|
|
ASTNode glabel = labels[label_index];
|
|
|
|
if (IsBackwardsJump(goto_node, glabel)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
2019-06-29 00:54:21 +00:00
|
|
|
}
|
2019-06-27 04:39:40 +00:00
|
|
|
|
2019-06-29 05:44:07 +00:00
|
|
|
ASTNode GetProgram() const {
|
2019-06-29 02:59:43 +00:00
|
|
|
return main_node;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Clear();
|
|
|
|
|
2019-06-29 05:44:07 +00:00
|
|
|
u32 GetVariables() const {
|
|
|
|
return variables;
|
|
|
|
}
|
|
|
|
|
2019-06-27 04:39:40 +00:00
|
|
|
private:
|
2019-08-16 20:25:02 +00:00
|
|
|
bool IsBackwardsJump(ASTNode goto_node, ASTNode label_node) const;
|
|
|
|
|
|
|
|
ASTNode CommonParent(ASTNode first, ASTNode second);
|
|
|
|
|
2019-06-27 22:57:47 +00:00
|
|
|
bool IndirectlyRelated(ASTNode first, ASTNode second);
|
|
|
|
|
|
|
|
bool DirectlyRelated(ASTNode first, ASTNode second);
|
|
|
|
|
|
|
|
void EncloseDoWhile(ASTNode goto_node, ASTNode label);
|
|
|
|
|
|
|
|
void EncloseIfThen(ASTNode goto_node, ASTNode label);
|
|
|
|
|
2019-06-29 02:59:43 +00:00
|
|
|
void MoveOutward(ASTNode goto_node);
|
2019-06-27 22:57:47 +00:00
|
|
|
|
|
|
|
u32 NewVariable() {
|
|
|
|
u32 new_var = variables;
|
|
|
|
variables++;
|
|
|
|
return new_var;
|
|
|
|
}
|
|
|
|
|
2019-08-16 20:25:02 +00:00
|
|
|
bool full_decompile{};
|
2019-06-27 04:39:40 +00:00
|
|
|
std::unordered_map<u32, u32> labels_map{};
|
|
|
|
u32 labels_count{};
|
|
|
|
std::vector<ASTNode> labels{};
|
|
|
|
std::list<ASTNode> gotos{};
|
|
|
|
u32 variables{};
|
2019-06-29 02:59:43 +00:00
|
|
|
ASTProgram* program{};
|
|
|
|
ASTNode main_node{};
|
2019-06-29 05:44:07 +00:00
|
|
|
Expr false_condition{};
|
2019-06-27 04:39:40 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace VideoCommon::Shader
|