improve decompiler last stmt detection, loop locals

This commit is contained in:
xensik 2022-07-15 16:31:05 +02:00
parent 9ecd86a1d7
commit e8ba428353
10 changed files with 130 additions and 22 deletions

View File

@ -1900,6 +1900,18 @@ void decompiler::decompile_ifelses(const ast::stmt_list::ptr& stmt)
}
else if (stmt->list.at(j) == ast::kind::stmt_return && stmt->list.at(j).as_return->expr == ast::kind::null)
{
if(entry.as_cond->value != blocks_.back().loc_end)
{
auto ref = stmt->list.at(j + 1).loc().label();
if (find_location_reference(stmt, i + 1, j, ref))
{
// after return is referenced inside the block
decompile_if(stmt, i, j);
continue;
}
}
if (blocks_.back().loc_break != "" || blocks_.back().loc_continue != "")
{
decompile_if(stmt, i, j); // inside a loop cant be last
@ -2170,7 +2182,7 @@ void decompiler::decompile_loop(const ast::stmt_list::ptr& block, std::size_t st
if (block->list.at(start - index) == ast::kind::stmt_assign)
{
auto ref = block->list.at(end).loc().label();
auto ref2 = block->list.at(start).loc().label();
auto ref2 = block->list.at(start - index + 1).loc().label();
if (find_location_reference(block, start, end, ref))
{
@ -2180,7 +2192,7 @@ void decompiler::decompile_loop(const ast::stmt_list::ptr& block, std::size_t st
}
else if (find_location_reference(block, 0, start, ref2))
{
// begin is at condition, not pre-expr
// begin is at condition or localVarCreate, not pre-expr
decompile_while(block, start, end);
return;
}

View File

@ -1900,6 +1900,18 @@ void decompiler::decompile_ifelses(const ast::stmt_list::ptr& stmt)
}
else if (stmt->list.at(j) == ast::kind::stmt_return && stmt->list.at(j).as_return->expr == ast::kind::null)
{
if(entry.as_cond->value != blocks_.back().loc_end)
{
auto ref = stmt->list.at(j + 1).loc().label();
if (find_location_reference(stmt, i + 1, j, ref))
{
// after return is referenced inside the block
decompile_if(stmt, i, j);
continue;
}
}
if (blocks_.back().loc_break != "" || blocks_.back().loc_continue != "")
{
decompile_if(stmt, i, j); // inside a loop cant be last
@ -2170,7 +2182,7 @@ void decompiler::decompile_loop(const ast::stmt_list::ptr& block, std::size_t st
if (block->list.at(start - index) == ast::kind::stmt_assign)
{
auto ref = block->list.at(end).loc().label();
auto ref2 = block->list.at(start).loc().label();
auto ref2 = block->list.at(start - index + 1).loc().label();
if (find_location_reference(block, start, end, ref))
{
@ -2180,7 +2192,7 @@ void decompiler::decompile_loop(const ast::stmt_list::ptr& block, std::size_t st
}
else if (find_location_reference(block, 0, start, ref2))
{
// begin is at condition, not pre-expr
// begin is at condition or localVarCreate, not pre-expr
decompile_while(block, start, end);
return;
}

View File

@ -1894,6 +1894,18 @@ void decompiler::decompile_ifelses(const ast::stmt_list::ptr& stmt)
}
else if (stmt->list.at(j) == ast::kind::stmt_return && stmt->list.at(j).as_return->expr == ast::kind::null)
{
if(entry.as_cond->value != blocks_.back().loc_end)
{
auto ref = stmt->list.at(j + 1).loc().label();
if (find_location_reference(stmt, i + 1, j, ref))
{
// after return is referenced inside the block
decompile_if(stmt, i, j);
continue;
}
}
if (blocks_.back().loc_break != "" || blocks_.back().loc_continue != "")
{
decompile_if(stmt, i, j); // inside a loop cant be last
@ -2164,7 +2176,7 @@ void decompiler::decompile_loop(const ast::stmt_list::ptr& block, std::size_t st
if (block->list.at(start - index) == ast::kind::stmt_assign)
{
auto ref = block->list.at(end).loc().label();
auto ref2 = block->list.at(start).loc().label();
auto ref2 = block->list.at(start - index + 1).loc().label();
if (find_location_reference(block, start, end, ref))
{
@ -2174,7 +2186,7 @@ void decompiler::decompile_loop(const ast::stmt_list::ptr& block, std::size_t st
}
else if (find_location_reference(block, 0, start, ref2))
{
// begin is at condition, not pre-expr
// begin is at condition or localVarCreate, not pre-expr
decompile_while(block, start, end);
return;
}

View File

@ -1894,6 +1894,18 @@ void decompiler::decompile_ifelses(const ast::stmt_list::ptr& stmt)
}
else if (stmt->list.at(j) == ast::kind::stmt_return && stmt->list.at(j).as_return->expr == ast::kind::null)
{
if(entry.as_cond->value != blocks_.back().loc_end)
{
auto ref = stmt->list.at(j + 1).loc().label();
if (find_location_reference(stmt, i + 1, j, ref))
{
// after return is referenced inside the block
decompile_if(stmt, i, j);
continue;
}
}
if (blocks_.back().loc_break != "" || blocks_.back().loc_continue != "")
{
decompile_if(stmt, i, j); // inside a loop cant be last
@ -2164,7 +2176,7 @@ void decompiler::decompile_loop(const ast::stmt_list::ptr& block, std::size_t st
if (block->list.at(start - index) == ast::kind::stmt_assign)
{
auto ref = block->list.at(end).loc().label();
auto ref2 = block->list.at(start).loc().label();
auto ref2 = block->list.at(start - index + 1).loc().label();
if (find_location_reference(block, start, end, ref))
{
@ -2174,7 +2186,7 @@ void decompiler::decompile_loop(const ast::stmt_list::ptr& block, std::size_t st
}
else if (find_location_reference(block, 0, start, ref2))
{
// begin is at condition, not pre-expr
// begin is at condition or localVarCreate, not pre-expr
decompile_while(block, start, end);
return;
}

View File

@ -1894,6 +1894,18 @@ void decompiler::decompile_ifelses(const ast::stmt_list::ptr& stmt)
}
else if (stmt->list.at(j) == ast::kind::stmt_return && stmt->list.at(j).as_return->expr == ast::kind::null)
{
if(entry.as_cond->value != blocks_.back().loc_end)
{
auto ref = stmt->list.at(j + 1).loc().label();
if (find_location_reference(stmt, i + 1, j, ref))
{
// after return is referenced inside the block
decompile_if(stmt, i, j);
continue;
}
}
if (blocks_.back().loc_break != "" || blocks_.back().loc_continue != "")
{
decompile_if(stmt, i, j); // inside a loop cant be last
@ -2164,7 +2176,7 @@ void decompiler::decompile_loop(const ast::stmt_list::ptr& block, std::size_t st
if (block->list.at(start - index) == ast::kind::stmt_assign)
{
auto ref = block->list.at(end).loc().label();
auto ref2 = block->list.at(start).loc().label();
auto ref2 = block->list.at(start - index + 1).loc().label();
if (find_location_reference(block, start, end, ref))
{
@ -2174,7 +2186,7 @@ void decompiler::decompile_loop(const ast::stmt_list::ptr& block, std::size_t st
}
else if (find_location_reference(block, 0, start, ref2))
{
// begin is at condition, not pre-expr
// begin is at condition or localVarCreate, not pre-expr
decompile_while(block, start, end);
return;
}

View File

@ -1931,6 +1931,18 @@ void decompiler::decompile_ifelses(const ast::stmt_list::ptr& stmt)
}
else if (stmt->list.at(j) == ast::kind::stmt_return && stmt->list.at(j).as_return->expr == ast::kind::null)
{
if(entry.as_cond->value != blocks_.back().loc_end)
{
auto ref = stmt->list.at(j + 1).loc().label();
if (find_location_reference(stmt, i + 1, j, ref))
{
// after return is referenced inside the block
decompile_if(stmt, i, j);
continue;
}
}
if (blocks_.back().loc_break != "" || blocks_.back().loc_continue != "")
{
decompile_if(stmt, i, j); // inside a loop cant be last
@ -2201,7 +2213,7 @@ void decompiler::decompile_loop(const ast::stmt_list::ptr& block, std::size_t st
if (block->list.at(start - index) == ast::kind::stmt_assign)
{
auto ref = block->list.at(end).loc().label();
auto ref2 = block->list.at(start).loc().label();
auto ref2 = block->list.at(start - index + 1).loc().label();
if (find_location_reference(block, start, end, ref))
{
@ -2211,7 +2223,7 @@ void decompiler::decompile_loop(const ast::stmt_list::ptr& block, std::size_t st
}
else if (find_location_reference(block, 0, start, ref2))
{
// begin is at condition, not pre-expr
// begin is at condition or localVarCreate, not pre-expr
decompile_while(block, start, end);
return;
}

View File

@ -1900,6 +1900,18 @@ void decompiler::decompile_ifelses(const ast::stmt_list::ptr& stmt)
}
else if (stmt->list.at(j) == ast::kind::stmt_return && stmt->list.at(j).as_return->expr == ast::kind::null)
{
if(entry.as_cond->value != blocks_.back().loc_end)
{
auto ref = stmt->list.at(j + 1).loc().label();
if (find_location_reference(stmt, i + 1, j, ref))
{
// after return is referenced inside the block
decompile_if(stmt, i, j);
continue;
}
}
if (blocks_.back().loc_break != "" || blocks_.back().loc_continue != "")
{
decompile_if(stmt, i, j); // inside a loop cant be last
@ -2170,7 +2182,7 @@ void decompiler::decompile_loop(const ast::stmt_list::ptr& block, std::size_t st
if (block->list.at(start - index) == ast::kind::stmt_assign)
{
auto ref = block->list.at(end).loc().label();
auto ref2 = block->list.at(start).loc().label();
auto ref2 = block->list.at(start - index + 1).loc().label();
if (find_location_reference(block, start, end, ref))
{
@ -2180,7 +2192,7 @@ void decompiler::decompile_loop(const ast::stmt_list::ptr& block, std::size_t st
}
else if (find_location_reference(block, 0, start, ref2))
{
// begin is at condition, not pre-expr
// begin is at condition or localVarCreate, not pre-expr
decompile_while(block, start, end);
return;
}

View File

@ -1907,6 +1907,18 @@ void decompiler::decompile_ifelses(const ast::stmt_list::ptr& stmt)
}
else if (stmt->list.at(j) == ast::kind::stmt_return && stmt->list.at(j).as_return->expr == ast::kind::null)
{
if(entry.as_cond->value != blocks_.back().loc_end)
{
auto ref = stmt->list.at(j + 1).loc().label();
if (find_location_reference(stmt, i + 1, j, ref))
{
// after return is referenced inside the block
decompile_if(stmt, i, j);
continue;
}
}
if (blocks_.back().loc_break != "" || blocks_.back().loc_continue != "")
{
decompile_if(stmt, i, j); // inside a loop cant be last
@ -2177,7 +2189,7 @@ void decompiler::decompile_loop(const ast::stmt_list::ptr& block, std::size_t st
if (block->list.at(start - index) == ast::kind::stmt_assign)
{
auto ref = block->list.at(end).loc().label();
auto ref2 = block->list.at(start).loc().label();
auto ref2 = block->list.at(start - index + 1).loc().label();
if (find_location_reference(block, start, end, ref))
{
@ -2187,7 +2199,7 @@ void decompiler::decompile_loop(const ast::stmt_list::ptr& block, std::size_t st
}
else if (find_location_reference(block, 0, start, ref2))
{
// begin is at condition, not pre-expr
// begin is at condition or localVarCreate, not pre-expr
decompile_while(block, start, end);
return;
}

View File

@ -1931,6 +1931,18 @@ void decompiler::decompile_ifelses(const ast::stmt_list::ptr& stmt)
}
else if (stmt->list.at(j) == ast::kind::stmt_return && stmt->list.at(j).as_return->expr == ast::kind::null)
{
if(entry.as_cond->value != blocks_.back().loc_end)
{
auto ref = stmt->list.at(j + 1).loc().label();
if (find_location_reference(stmt, i + 1, j, ref))
{
// after return is referenced inside the block
decompile_if(stmt, i, j);
continue;
}
}
if (blocks_.back().loc_break != "" || blocks_.back().loc_continue != "")
{
decompile_if(stmt, i, j); // inside a loop cant be last
@ -2201,7 +2213,7 @@ void decompiler::decompile_loop(const ast::stmt_list::ptr& block, std::size_t st
if (block->list.at(start - index) == ast::kind::stmt_assign)
{
auto ref = block->list.at(end).loc().label();
auto ref2 = block->list.at(start).loc().label();
auto ref2 = block->list.at(start - index + 1).loc().label();
if (find_location_reference(block, start, end, ref))
{
@ -2211,7 +2223,7 @@ void decompiler::decompile_loop(const ast::stmt_list::ptr& block, std::size_t st
}
else if (find_location_reference(block, 0, start, ref2))
{
// begin is at condition, not pre-expr
// begin is at condition or localVarCreate, not pre-expr
decompile_while(block, start, end);
return;
}

View File

@ -250,7 +250,7 @@ void disassemble_file(game game, std::string file)
}
catch (const std::exception& e)
{
std::cerr << e.what() << '\n';
std::cerr << e.what() << " at " << file << '\n';
}
}
@ -394,7 +394,7 @@ void decompile_file(game game, std::string file)
}
catch (const std::exception& e)
{
std::cerr << e.what() << '\n';
std::cerr << e.what() << " at " << file << '\n';
}
}
@ -471,7 +471,7 @@ void disassemble_file(game game, const std::filesystem::path& file)
}
catch (const std::exception& e)
{
std::cerr << e.what() << '\n';
std::cerr << e.what() << " at " << file.string() << '\n';
}
}
@ -521,7 +521,7 @@ void decompile_file(game game, const std::filesystem::path& file)
}
catch (const std::exception& e)
{
std::cerr << e.what() << '\n';
std::cerr << e.what() << " at " << file.string() << '\n';
}
}