1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 23:17:45 +00:00

JSSpecCompiler: Save references to return value and function arguments

This commit is contained in:
Dan Klishch 2023-10-21 21:18:58 -04:00 committed by Andrew Kaster
parent 7f47340c82
commit 5825eaa264
10 changed files with 89 additions and 49 deletions

View file

@ -16,7 +16,7 @@ void CFGBuildingPass::process_function()
m_current_block = m_cfg->start_block = create_empty_block();
m_cfg->end_block = create_empty_block();
m_cfg->end_block->m_continuation = make_ref_counted<ControlFlowFunctionReturn>(
make_ref_counted<Variable>(m_function->m_return_value));
make_ref_counted<Variable>(m_function->m_named_return_value));
m_is_expression_stack = { false };
run_in_subtree(m_function->m_ast);
@ -25,7 +25,7 @@ void CFGBuildingPass::process_function()
// error_tree will 100% confuse future passes.
m_current_block->m_expressions.append(make_ref_counted<BinaryOperation>(
BinaryOperator::Assignment,
make_ref_counted<Variable>(m_function->m_return_value),
make_ref_counted<Variable>(m_function->m_named_return_value),
error_tree));
m_current_block->m_continuation = make_ref_counted<ControlFlowJump>(m_cfg->end_block);
}
@ -63,7 +63,7 @@ RecursionDecision CFGBuildingPass::on_entry(Tree tree)
if (auto return_node = as<ReturnNode>(tree); return_node) {
Tree return_assignment = make_ref_counted<BinaryOperation>(
BinaryOperator::Assignment,
make_ref_counted<Variable>(m_function->m_return_value),
make_ref_counted<Variable>(m_function->m_named_return_value),
return_node->m_return_value);
run_in_subtree(return_assignment);
auto* return_block = exchange_current_with_empty();

View file

@ -12,6 +12,13 @@
namespace JSSpecCompiler {
void ReferenceResolvingPass::process_function()
{
for (auto name : m_function->m_argument_names)
m_function->m_local_variables.set(name, make_ref_counted<NamedVariableDeclaration>(name));
GenericASTPass::process_function();
}
RecursionDecision ReferenceResolvingPass::on_entry(Tree tree)
{
if (auto binary_operation = as<BinaryOperation>(tree); binary_operation) {

View file

@ -19,8 +19,8 @@ public:
using GenericASTPass::GenericASTPass;
protected:
void process_function() override;
RecursionDecision on_entry(Tree tree) override;
void on_leave(Tree tree) override;
};

View file

@ -279,7 +279,7 @@ void SSABuildingPass::place_phi_nodes()
OrderedHashMap<NamedVariableDeclarationRef, Vector<BasicBlockRef>> m_declarations;
for (auto const& [name, var_decl] : m_function->m_local_variables)
m_declarations.set(var_decl, { m_order[0] });
m_declarations.set(m_function->m_return_value, { m_order[0] });
m_declarations.set(m_function->m_named_return_value, { m_order[0] });
VariableAssignmentCollector collector(m_declarations);
for (auto const& block : m_order)
@ -414,6 +414,12 @@ void SSABuildingPass::rename_variables(Vertex u, Vertex from)
});
renamer.run(u.block());
if (auto function_return = as<ControlFlowFunctionReturn>(u.block()->m_continuation); function_return) {
// CFG should have exactly one ControlFlowFunctionReturn.
VERIFY(m_function->m_return_value == nullptr);
m_function->m_return_value = function_return->m_return_value->m_ssa;
}
for (size_t j : u->outgoing_edges)
rename_variables(j, u);
@ -423,12 +429,24 @@ void SSABuildingPass::rename_variables(Vertex u, Vertex from)
void SSABuildingPass::rename_variables()
{
for (auto const& [name, var_decl] : m_function->m_local_variables)
HashMap<StringView, size_t> argument_index_by_name;
for (size_t i = 0; i < m_function->m_argument_names.size(); ++i)
argument_index_by_name.set(m_function->m_argument_names[i], i);
m_function->m_arguments.resize(m_function->m_argument_names.size());
for (auto const& [name, var_decl] : m_function->m_local_variables) {
make_new_ssa_variable_for(var_decl);
make_new_ssa_variable_for(m_function->m_return_value);
if (auto maybe_index = argument_index_by_name.get(name); maybe_index.has_value()) {
size_t index = maybe_index.value();
m_function->m_arguments[index] = m_def_stack.get(var_decl).value()[0];
}
}
make_new_ssa_variable_for(m_function->m_named_return_value);
++m_mark_version;
rename_variables(0);
VERIFY(m_function->m_return_value);
m_function->reindex_ssa_variables();
}