mirror of
https://github.com/open-goal/jak-project.git
synced 2024-10-20 11:26:18 -04:00
5093b97cda
* wip decompiler ir * add AtomicOp stuff * fix windows build and warnings * add instruction parser * include * make minilzo shared * odr fix * a * fix merge conflicts * move decompiler into namespace * update the code coverage to include the decompiler * add demo test * add register use test to example test
60 lines
1.8 KiB
C++
60 lines
1.8 KiB
C++
#include "Function.h"
|
|
#include "decompiler/IR/IR.h"
|
|
#include "ExpressionStack.h"
|
|
|
|
namespace decompiler {
|
|
namespace {
|
|
bool expressionize_begin(IR_Begin* begin, LinkedObjectFile& file) {
|
|
ExpressionStack stack;
|
|
// todo - this might need to run multiple times?
|
|
for (auto& op : begin->forms) {
|
|
op->expression_stack(stack, file);
|
|
}
|
|
// printf("%s\n", stack.print(file).c_str());
|
|
begin->forms = stack.get_result();
|
|
return true;
|
|
}
|
|
} // namespace
|
|
|
|
bool Function::build_expression(LinkedObjectFile& file) {
|
|
if (!ir) {
|
|
printf("build_expression on %s failed due to no IR.\n", guessed_name.to_string().c_str());
|
|
return false;
|
|
}
|
|
|
|
try {
|
|
// first we get a list of begins, which are where we can build up expressions.
|
|
// we want to start with innermost begins because we'll probably need to do some fixing up
|
|
// or more complicated analysis to do as good as possible on outer begins.
|
|
auto all_children = ir->get_all_ir(file);
|
|
std::vector<IR_Begin*> all_begins;
|
|
|
|
// the top level may also be a begin
|
|
auto as_begin = dynamic_cast<IR_Begin*>(ir.get());
|
|
if (as_begin) {
|
|
all_begins.push_back(as_begin);
|
|
}
|
|
|
|
for (auto& i : all_children) {
|
|
auto child_as_begin = dynamic_cast<IR_Begin*>(i.get());
|
|
if (child_as_begin) {
|
|
all_begins.push_back(child_as_begin);
|
|
}
|
|
}
|
|
|
|
// turn each begin into an expression
|
|
for (auto b : all_begins) {
|
|
// printf("BEFORE:\n%s\n", b->print(file).c_str());
|
|
if (!expressionize_begin(b, file)) {
|
|
return false;
|
|
}
|
|
// printf("AFTER:\n%s\n", b->print(file).c_str());
|
|
}
|
|
} catch (std::exception& e) {
|
|
printf("build_expression failed on %s due to %s\n", guessed_name.to_string().c_str(), e.what());
|
|
return false;
|
|
}
|
|
|
|
return true;
|
|
}
|
|
} // namespace decompiler
|