From b96d865e2bebce725d809f69958e35decf8a2544 Mon Sep 17 00:00:00 2001 From: water111 <48171810+water111@users.noreply.github.com> Date: Fri, 2 Jul 2021 12:27:46 -0400 Subject: [PATCH] fix labmda defs (#670) --- decompiler/analysis/expression_build.cpp | 3 +- decompiler/analysis/final_output.cpp | 23 ++++++--- decompiler/analysis/final_output.h | 1 + decompiler/analysis/static_refs.cpp | 7 +-- decompiler/util/data_decompile.cpp | 7 +-- .../reference/kernel/gkernel_REF.gc | 48 +++++++++---------- 6 files changed, 45 insertions(+), 44 deletions(-) diff --git a/decompiler/analysis/expression_build.cpp b/decompiler/analysis/expression_build.cpp index 1167d070c8..b38ae9214d 100644 --- a/decompiler/analysis/expression_build.cpp +++ b/decompiler/analysis/expression_build.cpp @@ -21,7 +21,8 @@ bool convert_to_expressions( // set argument names to some reasonable defaults. these will be used if the user doesn't // give us anything more specific. - if (f.guessed_name.kind == FunctionName::FunctionKind::GLOBAL) { + if (f.guessed_name.kind == FunctionName::FunctionKind::GLOBAL || + f.guessed_name.kind == FunctionName::FunctionKind::UNIDENTIFIED) { f.ir2.env.set_remap_for_function(f.type.arg_count() - 1); } else if (f.guessed_name.kind == FunctionName::FunctionKind::METHOD) { if (f.guessed_name.method_id == GOAL_NEW_METHOD) { diff --git a/decompiler/analysis/final_output.cpp b/decompiler/analysis/final_output.cpp index 032a0068ec..c2aaea1059 100644 --- a/decompiler/analysis/final_output.cpp +++ b/decompiler/analysis/final_output.cpp @@ -24,9 +24,9 @@ goos::Object get_arg_list_for_function(const Function& func, const Env& env) { } namespace { -void append_to_body(goos::Object* top_form, - const std::vector& inline_body, - const FunctionVariableDefinitions& var_dec) { +void append_body_to_function_definition(goos::Object* top_form, + const std::vector& inline_body, + const FunctionVariableDefinitions& var_dec) { if (var_dec.local_vars) { pretty_print::append(*top_form, pretty_print::build_list(*var_dec.local_vars)); } @@ -43,6 +43,15 @@ void append_to_body(goos::Object* top_form, } } // namespace +goos::Object final_output_lambda(const Function& func) { + std::vector inline_body; + func.ir2.top_form->inline_forms(inline_body, func.ir2.env); + auto var_dec = func.ir2.env.local_var_type_list(func.ir2.top_form, func.type.arg_count() - 1); + auto result = pretty_print::build_list("lambda", get_arg_list_for_function(func, func.ir2.env)); + append_body_to_function_definition(&result, inline_body, var_dec); + return result; +} + std::string final_defun_out(const Function& func, const Env& env, const DecompilerTypeSystem& dts, @@ -72,7 +81,7 @@ std::string final_defun_out(const Function& func, top.push_back(arguments); auto top_form = pretty_print::build_list(top); - append_to_body(&top_form, inline_body, var_dec); + append_body_to_function_definition(&top_form, inline_body, var_dec); return pretty_print::to_string(top_form); } @@ -87,7 +96,7 @@ std::string final_defun_out(const Function& func, top.push_back(arguments); auto top_form = pretty_print::build_list(top); - append_to_body(&top_form, inline_body, var_dec); + append_body_to_function_definition(&top_form, inline_body, var_dec); return pretty_print::to_string(top_form); } @@ -98,7 +107,7 @@ std::string final_defun_out(const Function& func, top.push_back(arguments); auto top_form = pretty_print::build_list(top); - append_to_body(&top_form, inline_body, var_dec); + append_body_to_function_definition(&top_form, inline_body, var_dec); return pretty_print::to_string(top_form); } @@ -111,7 +120,7 @@ std::string final_defun_out(const Function& func, top.push_back(arguments); auto top_form = pretty_print::build_list(top); - append_to_body(&top_form, inline_body, var_dec); + append_body_to_function_definition(&top_form, inline_body, var_dec); return pretty_print::to_string(top_form); } return "nyi"; diff --git a/decompiler/analysis/final_output.h b/decompiler/analysis/final_output.h index 051977d69c..7bc804e1a0 100644 --- a/decompiler/analysis/final_output.h +++ b/decompiler/analysis/final_output.h @@ -16,4 +16,5 @@ std::string write_from_top_level(const Function& top_level, const std::unordered_set& skip_functions = {}); goos::Object get_arg_list_for_function(const Function& func, const Env& env); +goos::Object final_output_lambda(const Function& function); } // namespace decompiler diff --git a/decompiler/analysis/static_refs.cpp b/decompiler/analysis/static_refs.cpp index 9b6625e8d9..32c5afb850 100644 --- a/decompiler/analysis/static_refs.cpp +++ b/decompiler/analysis/static_refs.cpp @@ -21,12 +21,7 @@ int insert_static_refs(Form* top_level_form, auto& file = env.file; auto other_func = file->try_get_function_at_label(atom->label()); if (other_func) { - std::vector inline_body; - other_func->ir2.top_form->inline_forms(inline_body, other_func->ir2.env); - auto result = pretty_print::build_list( - "lambda", get_arg_list_for_function(*other_func, other_func->ir2.env)); - pretty_print::append(result, pretty_print::build_list(inline_body)); - + auto result = final_output_lambda(*other_func); f->clear(); f->push_back(pool.alloc_element(result)); replaced++; diff --git a/decompiler/util/data_decompile.cpp b/decompiler/util/data_decompile.cpp index 8ce0b3f9ae..69a79581cf 100644 --- a/decompiler/util/data_decompile.cpp +++ b/decompiler/util/data_decompile.cpp @@ -139,12 +139,7 @@ goos::Object decompile_function_at_label(const DecompilerLabel& label, if (file) { auto other_func = file->try_get_function_at_label(label); if (other_func) { - std::vector inline_body; - other_func->ir2.top_form->inline_forms(inline_body, other_func->ir2.env); - auto result = pretty_print::build_list( - "lambda", get_arg_list_for_function(*other_func, other_func->ir2.env)); - pretty_print::append(result, pretty_print::build_list(inline_body)); - return result; + return final_output_lambda(*other_func); } } return pretty_print::to_symbol(fmt::format("", label.name)); diff --git a/test/decompiler/reference/kernel/gkernel_REF.gc b/test/decompiler/reference/kernel/gkernel_REF.gc index 412cce9bfb..8eb29768f8 100644 --- a/test/decompiler/reference/kernel/gkernel_REF.gc +++ b/test/decompiler/reference/kernel/gkernel_REF.gc @@ -1136,7 +1136,7 @@ process (search-process-tree arg1 - (lambda ((a0-0 process)) (name= (-> a0-0 name) *global-search-name*)) + (lambda ((arg0 process)) (name= (-> arg0 name) *global-search-name*)) ) ) ) @@ -1149,7 +1149,7 @@ process (search-process-tree arg1 - (lambda ((a0-0 process)) (not (name= (-> a0-0 name) *global-search-name*))) + (lambda ((arg0 process)) (not (name= (-> arg0 name) *global-search-name*))) ) ) ) @@ -1160,7 +1160,7 @@ (iterate-process-tree arg0 (lambda - ((a0-0 process)) + ((arg0 process)) (set! *global-search-count* (+ *global-search-count* 1)) #t ) @@ -1190,7 +1190,7 @@ a0-1 (search-process-tree arg1 - (lambda ((a0-0 process)) (= (-> a0-0 type) *global-search-name*)) + (lambda ((arg0 process)) (= (-> arg0 type) *global-search-name*)) ) ) a0-1 @@ -1221,7 +1221,7 @@ a0-1 (search-process-tree arg1 - (lambda ((a0-0 process)) (!= (-> a0-0 type) *global-search-name*)) + (lambda ((arg0 process)) (!= (-> arg0 type) *global-search-name*)) ) ) a0-1 @@ -1343,15 +1343,15 @@ ) (execute-process-tree *active-pool* - (lambda ((a0-0 process)) (let ((s5-0 *kernel-context*) - (v1-0 (-> a0-0 status)) + (lambda ((arg0 process)) (let ((s5-0 *kernel-context*) + (v1-0 (-> arg0 status)) ) (cond ((or (= v1-0 'waiting-to-run) (= v1-0 'suspended)) - (set! (-> s5-0 current-process) a0-0) + (set! (-> s5-0 current-process) arg0) (cond ((nonzero? - (logand (-> a0-0 mask) (process-mask pause)) + (logand (-> arg0 mask) (process-mask pause)) ) (set! *stdcon* *stdcon1*) (set! *debug-draw-pauseable* #t) @@ -1361,23 +1361,23 @@ (set! *debug-draw-pauseable* #f) ) ) - (when (-> a0-0 trans-hook) + (when (-> arg0 trans-hook) (let ((s4-0 (new 'process 'cpu-thread - a0-0 + arg0 'trans 256 - (-> a0-0 main-thread stack-top) + (-> arg0 main-thread stack-top) ) ) ) - (reset-and-call s4-0 (-> a0-0 trans-hook)) + (reset-and-call s4-0 (-> arg0 trans-hook)) (delete s4-0) ) - (when (= (-> a0-0 status) 'dead) + (when (= (-> arg0 status) 'dead) (set! (-> s5-0 current-process) #f) (return 'dead) ) @@ -1385,42 +1385,42 @@ (if (nonzero? (logand - (-> a0-0 mask) + (-> arg0 mask) (process-mask sleep-code) ) ) - (set! (-> a0-0 status) 'suspended) - ((-> a0-0 main-thread resume-hook) - (-> a0-0 main-thread) + (set! (-> arg0 status) 'suspended) + ((-> arg0 main-thread resume-hook) + (-> arg0 main-thread) ) ) (cond - ((= (-> a0-0 status) 'dead) + ((= (-> arg0 status) 'dead) (set! (-> s5-0 current-process) #f) 'dead ) (else - (when (-> a0-0 post-hook) + (when (-> arg0 post-hook) (let ((s4-1 (new 'process 'cpu-thread - a0-0 + arg0 'post 256 (&-> *dram-stack* 14336) ) ) ) - (reset-and-call s4-1 (-> a0-0 post-hook)) + (reset-and-call s4-1 (-> arg0 post-hook)) (delete s4-1) ) - (when (= (-> a0-0 status) 'dead) + (when (= (-> arg0 status) 'dead) (set! (-> s5-0 current-process) #f) (return 'dead) ) - (set! (-> a0-0 status) 'suspended) + (set! (-> arg0 status) 'suspended) ) (set! (-> s5-0 current-process) #f) #f