mirror of
https://github.com/open-goal/jak-project
synced 2026-08-10 19:17:55 -04:00
[decompiler] clean up if/when/cond and recognize define-perm (#472)
* clean up if and when and cond decisions * recognize define perm
This commit is contained in:
@@ -219,6 +219,19 @@ std::string write_from_top_level(const Function& top_level,
|
||||
// (set! sym-val <expr>)
|
||||
auto define_symbol_matcher = Matcher::set(Matcher::any_symbol(0), Matcher::any(1));
|
||||
|
||||
// define-perm
|
||||
// (if (or (not <sym>) (zero? <sym>))
|
||||
// (set! <sym> <init-val>)
|
||||
// )
|
||||
auto define_perm_matcher = Matcher::if_no_else(
|
||||
Matcher::op(GenericOpMatcher::condition(IR2_Condition::Kind::TRUTHY),
|
||||
{Matcher::or_expression(
|
||||
{Matcher::op(GenericOpMatcher::condition(IR2_Condition::Kind::FALSE),
|
||||
{Matcher::any_symbol(0)}),
|
||||
Matcher::op(GenericOpMatcher::condition(IR2_Condition::Kind::ZERO),
|
||||
{Matcher::any_symbol(1)})})}),
|
||||
Matcher::set(Matcher::any_symbol(2), Matcher::any(3)));
|
||||
|
||||
for (auto& x : forms) {
|
||||
bool something_matched = false;
|
||||
Form f;
|
||||
@@ -308,6 +321,26 @@ std::string write_from_top_level(const Function& top_level,
|
||||
}
|
||||
}
|
||||
|
||||
if (!something_matched) {
|
||||
auto define_perm_match_result = match(define_perm_matcher, &f);
|
||||
if (define_perm_match_result.matched &&
|
||||
define_perm_match_result.maps.strings.at(0) ==
|
||||
define_perm_match_result.maps.strings.at(1) &&
|
||||
define_perm_match_result.maps.strings.at(0) ==
|
||||
define_perm_match_result.maps.strings.at(2)) {
|
||||
something_matched = true;
|
||||
auto sym_name = define_perm_match_result.maps.strings.at(0);
|
||||
auto symbol_type = dts.lookup_symbol_type(sym_name);
|
||||
|
||||
result += fmt::format(";; definition (perm) for symbol {}, type {}\n", sym_name,
|
||||
symbol_type.print());
|
||||
result += pretty_print::to_string(pretty_print::build_list(
|
||||
fmt::format("define-perm {} {}", sym_name, symbol_type.print()),
|
||||
define_perm_match_result.maps.forms.at(3)->to_form(env)));
|
||||
result += "\n\n";
|
||||
}
|
||||
}
|
||||
|
||||
if (!something_matched) {
|
||||
result += ";; failed to figure out what this is:\n";
|
||||
result += pretty_print::to_string(x->to_form(env));
|
||||
|
||||
Reference in New Issue
Block a user