Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[decompiler] fix lambda definitions missing with-pp and similar #670

Merged
merged 1 commit into from
Jul 2, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion decompiler/analysis/expression_build.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
23 changes: 16 additions & 7 deletions decompiler/analysis/final_output.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<goos::Object>& inline_body,
const FunctionVariableDefinitions& var_dec) {
void append_body_to_function_definition(goos::Object* top_form,
const std::vector<goos::Object>& inline_body,
const FunctionVariableDefinitions& var_dec) {
if (var_dec.local_vars) {
pretty_print::append(*top_form, pretty_print::build_list(*var_dec.local_vars));
}
Expand All @@ -43,6 +43,15 @@ void append_to_body(goos::Object* top_form,
}
} // namespace

goos::Object final_output_lambda(const Function& func) {
std::vector<goos::Object> 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,
Expand Down Expand Up @@ -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);
}

Expand All @@ -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);
}

Expand All @@ -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);
}

Expand All @@ -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";
Expand Down
1 change: 1 addition & 0 deletions decompiler/analysis/final_output.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,5 @@ std::string write_from_top_level(const Function& top_level,
const std::unordered_set<std::string>& 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
7 changes: 1 addition & 6 deletions decompiler/analysis/static_refs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<goos::Object> 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<LambdaDefinitionElement>(result));
replaced++;
Expand Down
7 changes: 1 addition & 6 deletions decompiler/util/data_decompile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<goos::Object> 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("<lambda at {}>", label.name));
Expand Down
48 changes: 24 additions & 24 deletions test/decompiler/reference/kernel/gkernel_REF.gc
Original file line number Diff line number Diff line change
Expand Up @@ -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*))
)
)
)
Expand All @@ -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*)))
)
)
)
Expand All @@ -1160,7 +1160,7 @@
(iterate-process-tree
arg0
(lambda
((a0-0 process))
((arg0 process))
(set! *global-search-count* (+ *global-search-count* 1))
#t
)
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand All @@ -1361,66 +1361,66 @@
(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)
)
)
(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
Expand Down