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