2021-01-22 20:50:37 -05:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <optional>
|
|
|
|
#include "decompiler/Disasm/Register.h"
|
|
|
|
#include "decompiler/IR2/AtomicOp.h"
|
|
|
|
|
|
|
|
namespace decompiler {
|
|
|
|
class Form;
|
|
|
|
/*!
|
|
|
|
* A FormStack is used to track partial expressions when rebuilding the tree structure of
|
|
|
|
* GOAL code. Linear sequences of operations are added onto the expression stack.
|
|
|
|
*/
|
|
|
|
class FormStack {
|
|
|
|
public:
|
2021-02-14 18:50:45 -05:00
|
|
|
explicit FormStack(bool is_root_stack) : m_is_root_stack(is_root_stack) {}
|
2021-02-28 12:38:29 -05:00
|
|
|
void push_value_to_reg(RegisterAccess var,
|
2021-02-09 20:59:14 -05:00
|
|
|
Form* value,
|
|
|
|
bool sequence_point,
|
2021-03-13 16:10:39 -05:00
|
|
|
TypeSpec type,
|
2021-02-11 16:13:03 -05:00
|
|
|
const SetVarInfo& info = {});
|
2021-02-28 12:38:29 -05:00
|
|
|
void push_non_seq_reg_to_reg(const RegisterAccess& dst,
|
|
|
|
const RegisterAccess& src,
|
2021-02-09 20:59:14 -05:00
|
|
|
Form* src_as_form,
|
2021-03-13 16:10:39 -05:00
|
|
|
TypeSpec type,
|
2021-02-11 16:13:03 -05:00
|
|
|
const SetVarInfo& info = {});
|
2021-02-28 12:38:29 -05:00
|
|
|
void push_value_to_reg_dead(RegisterAccess var,
|
2021-02-15 11:36:59 -05:00
|
|
|
Form* value,
|
|
|
|
bool sequence_point,
|
2021-03-13 16:10:39 -05:00
|
|
|
TypeSpec type,
|
2021-02-15 11:36:59 -05:00
|
|
|
const SetVarInfo& info = {});
|
2021-03-13 16:10:39 -05:00
|
|
|
|
2021-01-22 20:50:37 -05:00
|
|
|
void push_form_element(FormElement* elt, bool sequence_point);
|
2021-02-28 12:38:29 -05:00
|
|
|
Form* pop_reg(const RegisterAccess& var,
|
2021-02-05 19:41:09 -05:00
|
|
|
const RegSet& barrier,
|
|
|
|
const Env& env,
|
2021-02-14 18:50:45 -05:00
|
|
|
bool allow_side_effects,
|
|
|
|
int begin_idx = -1);
|
|
|
|
Form* pop_reg(Register reg,
|
|
|
|
const RegSet& barrier,
|
|
|
|
const Env& env,
|
|
|
|
bool allow_side_effects,
|
|
|
|
int begin_idx = -1);
|
2021-02-18 11:35:45 -05:00
|
|
|
FormElement* pop_back(FormPool& pool);
|
2021-02-15 11:36:59 -05:00
|
|
|
Form* unsafe_peek(Register reg, const Env& env);
|
2021-01-22 20:50:37 -05:00
|
|
|
bool is_single_expression();
|
2021-02-22 09:36:30 -05:00
|
|
|
std::vector<FormElement*> rewrite(FormPool& pool, const Env& env);
|
2021-01-22 20:50:37 -05:00
|
|
|
std::string print(const Env& env);
|
2021-02-14 18:50:45 -05:00
|
|
|
bool is_root() const { return m_is_root_stack; }
|
2021-01-22 20:50:37 -05:00
|
|
|
|
|
|
|
private:
|
|
|
|
struct StackEntry {
|
2021-02-28 12:38:29 -05:00
|
|
|
bool active = true; // should this appear in the output?
|
|
|
|
std::optional<RegisterAccess>
|
|
|
|
destination; // what register we are setting (or nullopt if no dest.)
|
|
|
|
std::optional<RegisterAccess> non_seq_source; // source variable, if we are setting var to var.
|
|
|
|
Form* source = nullptr; // the value we are setting the register to.
|
2021-01-22 20:50:37 -05:00
|
|
|
|
|
|
|
FormElement* elt = nullptr;
|
|
|
|
bool sequence_point = false;
|
2021-03-13 16:10:39 -05:00
|
|
|
// TP_Type type;
|
2021-02-24 19:08:55 -05:00
|
|
|
bool is_compactable = false;
|
2021-02-11 16:13:03 -05:00
|
|
|
|
|
|
|
SetVarInfo set_info;
|
2021-03-13 16:10:39 -05:00
|
|
|
TypeSpec set_type;
|
2021-02-11 16:13:03 -05:00
|
|
|
|
2021-01-22 20:50:37 -05:00
|
|
|
std::string print(const Env& env) const;
|
|
|
|
};
|
|
|
|
std::vector<StackEntry> m_stack;
|
2021-02-14 18:50:45 -05:00
|
|
|
bool m_is_root_stack = false;
|
2021-01-22 20:50:37 -05:00
|
|
|
};
|
2021-02-14 18:50:45 -05:00
|
|
|
|
|
|
|
void rewrite_to_get_var(std::vector<FormElement*>& default_result,
|
|
|
|
FormPool& pool,
|
2021-02-28 12:38:29 -05:00
|
|
|
const RegisterAccess& var,
|
2021-02-22 09:36:30 -05:00
|
|
|
const Env& env);
|
|
|
|
std::vector<FormElement*> rewrite_to_get_var(FormStack& stack,
|
|
|
|
FormPool& pool,
|
2021-02-28 12:38:29 -05:00
|
|
|
const RegisterAccess& var,
|
2021-02-22 09:36:30 -05:00
|
|
|
const Env& env);
|
2021-01-22 20:50:37 -05:00
|
|
|
} // namespace decompiler
|