From 9e6dec9829376441461168544ce5e0431f5f14e3 Mon Sep 17 00:00:00 2001 From: water111 <48171810+water111@users.noreply.github.com> Date: Thu, 24 Jun 2021 20:06:12 -0400 Subject: [PATCH] [decomp] make bg decompile (#624) * make bg decopmile * fix parent method call --- common/type_system/TypeSystem.cpp | 3 ++- decompiler/IR2/AtomicOpTypeAnalysis.cpp | 3 ++- decompiler/IR2/FormExpressionAnalysis.cpp | 11 +++++------ decompiler/analysis/type_analysis.cpp | 3 ++- decompiler/config/all-types.gc | 2 +- decompiler/config/jak1_ntsc_black_label/hacks.jsonc | 3 ++- .../config/jak1_ntsc_black_label/type_casts.jsonc | 4 ++++ goal_src/engine/level/level-h.gc | 2 +- test/decompiler/reference/engine/level/level-h_REF.gc | 2 +- 9 files changed, 20 insertions(+), 13 deletions(-) diff --git a/common/type_system/TypeSystem.cpp b/common/type_system/TypeSystem.cpp index 764db6004..6a0186a83 100644 --- a/common/type_system/TypeSystem.cpp +++ b/common/type_system/TypeSystem.cpp @@ -22,7 +22,8 @@ template fmt::print(fg(fmt::color::yellow), str + '\n', std::forward(args)...); } - throw std::runtime_error("Type Error"); + throw std::runtime_error( + fmt::format("Type Error: {}", fmt::format(str, std::forward(args)...))); } } // namespace diff --git a/decompiler/IR2/AtomicOpTypeAnalysis.cpp b/decompiler/IR2/AtomicOpTypeAnalysis.cpp index 1b4e4cc3e..a0ce930fd 100644 --- a/decompiler/IR2/AtomicOpTypeAnalysis.cpp +++ b/decompiler/IR2/AtomicOpTypeAnalysis.cpp @@ -454,7 +454,8 @@ TP_Type SimpleExpression::get_type_int2(const TypeState& input, } // special cases for non-integers - if ((arg0_type.typespec() == TypeSpec("object") || arg0_type.typespec() == TypeSpec("pair")) && + if ((arg0_type.typespec() == TypeSpec("object") || arg0_type.typespec() == TypeSpec("pair") || + tc(dts, TypeSpec("basic"), arg0_type)) && (arg1_type.is_integer_constant(62) || arg1_type.is_integer_constant(61))) { // boxed object tag trick. return TP_Type::make_from_ts("int"); diff --git a/decompiler/IR2/FormExpressionAnalysis.cpp b/decompiler/IR2/FormExpressionAnalysis.cpp index f8c77d622..865ed1d58 100644 --- a/decompiler/IR2/FormExpressionAnalysis.cpp +++ b/decompiler/IR2/FormExpressionAnalysis.cpp @@ -2059,7 +2059,12 @@ void FunctionCallElement::update_from_stack(const Env& env, "type. Got {} instead.", tp_type.print())); } + } + auto type_source_form = match_result.maps.forms.at(type_source); + + // if the type is the exact type of the argument, we want to build it into a method call + if (type_source_form->to_string(env) == first_arg_type.base_type() && name != "new") { if (env.dts->ts.should_use_virtual_methods(tp_type.method_from_type(), tp_type.method_id())) { throw std::runtime_error(fmt::format( @@ -2067,12 +2072,6 @@ void FunctionCallElement::update_from_stack(const Env& env, ":final in the deftype to disable virtual method calls", tp_type.method_from_type().print(), tp_type.method_id())); } - } - - auto type_source_form = match_result.maps.forms.at(type_source); - - // if the type is the exact type of the argument, we want to build it into a method call - if (type_source_form->to_string(env) == first_arg_type.base_type() && name != "new") { auto method_op = pool.alloc_single_element_form(nullptr, name); auto gop = GenericOperator::make_function(method_op); diff --git a/decompiler/analysis/type_analysis.cpp b/decompiler/analysis/type_analysis.cpp index d84ee2ede..cc92fdfea 100644 --- a/decompiler/analysis/type_analysis.cpp +++ b/decompiler/analysis/type_analysis.cpp @@ -126,7 +126,8 @@ bool run_type_analysis_ir2(const TypeSpec& my_type, DecompilerTypeSystem& dts, F } catch (std::runtime_error& e) { lg::warn("Function {} failed type prop at op {}: {}", func.guessed_name.to_string(), op_id, e.what()); - func.warnings.type_prop_warning("{}", e.what()); + func.warnings.type_prop_warning("Failed type prop at op {} ({})\n:{}", op_id, + op->to_string(func.ir2.env), e.what()); func.ir2.env.set_types(block_init_types, op_types, *func.ir2.atomic_ops, my_type); return false; } diff --git a/decompiler/config/all-types.gc b/decompiler/config/all-types.gc index f825a1763..26909b15a 100644 --- a/decompiler/config/all-types.gc +++ b/decompiler/config/all-types.gc @@ -4553,7 +4553,7 @@ (:methods (dummy-9 (_type_ symbol) level 9) (dummy-10 (_type_ symbol) symbol 10) - (dummy-11 (_type_ symbol symbol) _type_ 11) + (dummy-11 (_type_ symbol symbol) level 11) (dummy-12 (_type_) none 12) (dummy-13 () none 13) (dummy-14 () none 14) diff --git a/decompiler/config/jak1_ntsc_black_label/hacks.jsonc b/decompiler/config/jak1_ntsc_black_label/hacks.jsonc index 191a4d47d..10493f140 100644 --- a/decompiler/config/jak1_ntsc_black_label/hacks.jsonc +++ b/decompiler/config/jak1_ntsc_black_label/hacks.jsonc @@ -468,6 +468,7 @@ "nassoc", "nassoce", "lookup-level-info", - "(method 21 level-group)" + "(method 21 level-group)", + "bg" ] } diff --git a/decompiler/config/jak1_ntsc_black_label/type_casts.jsonc b/decompiler/config/jak1_ntsc_black_label/type_casts.jsonc index 013aed9be..dfa078555 100644 --- a/decompiler/config/jak1_ntsc_black_label/type_casts.jsonc +++ b/decompiler/config/jak1_ntsc_black_label/type_casts.jsonc @@ -701,5 +701,9 @@ ], "on": [ [33, "t9", "(function cpu-thread function none)"] + ], + + "bg": [ + [37, "a0", "symbol"] ] } diff --git a/goal_src/engine/level/level-h.gc b/goal_src/engine/level/level-h.gc index ccc705f8b..cd340e017 100644 --- a/goal_src/engine/level/level-h.gc +++ b/goal_src/engine/level/level-h.gc @@ -197,7 +197,7 @@ (:methods (dummy-9 (_type_ symbol) level 9) (dummy-10 (_type_ symbol) symbol 10) - (dummy-11 (_type_ symbol symbol) _type_ 11) + (dummy-11 (_type_ symbol symbol) level 11) (dummy-12 (_type_) none 12) (dummy-13 () none 13) (dummy-14 () none 14) diff --git a/test/decompiler/reference/engine/level/level-h_REF.gc b/test/decompiler/reference/engine/level/level-h_REF.gc index b956ed056..3dba508ba 100644 --- a/test/decompiler/reference/engine/level/level-h_REF.gc +++ b/test/decompiler/reference/engine/level/level-h_REF.gc @@ -291,7 +291,7 @@ (:methods (dummy-9 (_type_ symbol) level 9) (dummy-10 (_type_ symbol) symbol 10) - (dummy-11 (_type_ symbol symbol) _type_ 11) + (dummy-11 (_type_ symbol symbol) level 11) (dummy-12 (_type_) none 12) (dummy-13 () none 13) (dummy-14 () none 14)