From 61fa00d46cc1f70480c77e511c1edfbbe548c67f Mon Sep 17 00:00:00 2001 From: Dan Klishch Date: Fri, 29 Sep 2023 19:43:19 -0400 Subject: [PATCH] JSSpecCompiler: Refactor `CompilerPass` to accept TranslationUnitRef --- .../JSSpecCompiler/CMakeLists.txt | 1 + .../JSSpecCompiler/Compiler/CompilerPass.cpp | 20 +++++++++++++++++++ .../JSSpecCompiler/Compiler/CompilerPass.h | 19 ++++++++++++++++-- .../Compiler/GenericASTPass.cpp | 2 +- .../JSSpecCompiler/Compiler/GenericASTPass.h | 10 ++++------ .../CodeGenerators/JSSpecCompiler/Forward.h | 1 + .../CodeGenerators/JSSpecCompiler/Function.h | 2 +- .../CodeGenerators/JSSpecCompiler/main.cpp | 6 +++--- 8 files changed, 48 insertions(+), 13 deletions(-) create mode 100644 Meta/Lagom/Tools/CodeGenerators/JSSpecCompiler/Compiler/CompilerPass.cpp diff --git a/Meta/Lagom/Tools/CodeGenerators/JSSpecCompiler/CMakeLists.txt b/Meta/Lagom/Tools/CodeGenerators/JSSpecCompiler/CMakeLists.txt index 00dcd110dc..c9a915af6d 100644 --- a/Meta/Lagom/Tools/CodeGenerators/JSSpecCompiler/CMakeLists.txt +++ b/Meta/Lagom/Tools/CodeGenerators/JSSpecCompiler/CMakeLists.txt @@ -1,6 +1,7 @@ set(SOURCES AST/AST.cpp AST/ASTPrinting.cpp + Compiler/CompilerPass.cpp Compiler/FunctionCallCanonicalizationPass.cpp Compiler/GenericASTPass.cpp Compiler/IfBranchMergingPass.cpp diff --git a/Meta/Lagom/Tools/CodeGenerators/JSSpecCompiler/Compiler/CompilerPass.cpp b/Meta/Lagom/Tools/CodeGenerators/JSSpecCompiler/Compiler/CompilerPass.cpp new file mode 100644 index 0000000000..e4afa0d791 --- /dev/null +++ b/Meta/Lagom/Tools/CodeGenerators/JSSpecCompiler/Compiler/CompilerPass.cpp @@ -0,0 +1,20 @@ +/* + * Copyright (c) 2023, Dan Klishch + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +#include "Compiler/CompilerPass.h" +#include "Function.h" + +namespace JSSpecCompiler { + +void IntraproceduralCompilerPass::run() +{ + for (auto const& function : m_translation_unit->function_definitions) { + m_function = function; + process_function(); + } +} + +} diff --git a/Meta/Lagom/Tools/CodeGenerators/JSSpecCompiler/Compiler/CompilerPass.h b/Meta/Lagom/Tools/CodeGenerators/JSSpecCompiler/Compiler/CompilerPass.h index b033a5b87c..e243381813 100644 --- a/Meta/Lagom/Tools/CodeGenerators/JSSpecCompiler/Compiler/CompilerPass.h +++ b/Meta/Lagom/Tools/CodeGenerators/JSSpecCompiler/Compiler/CompilerPass.h @@ -15,8 +15,8 @@ namespace JSSpecCompiler { class CompilerPass { public: - CompilerPass(FunctionDefinitionRef function) - : m_function(function) + CompilerPass(TranslationUnitRef translation_unit) + : m_translation_unit(translation_unit) { } @@ -25,6 +25,21 @@ public: virtual void run() = 0; protected: + TranslationUnitRef m_translation_unit; +}; + +class IntraproceduralCompilerPass : public CompilerPass { +public: + IntraproceduralCompilerPass(TranslationUnitRef translation_unit) + : CompilerPass(translation_unit) + { + } + + void run() override final; + +protected: + virtual void process_function() = 0; + FunctionDefinitionRef m_function; }; diff --git a/Meta/Lagom/Tools/CodeGenerators/JSSpecCompiler/Compiler/GenericASTPass.cpp b/Meta/Lagom/Tools/CodeGenerators/JSSpecCompiler/Compiler/GenericASTPass.cpp index 23189e780f..5e0653dec7 100644 --- a/Meta/Lagom/Tools/CodeGenerators/JSSpecCompiler/Compiler/GenericASTPass.cpp +++ b/Meta/Lagom/Tools/CodeGenerators/JSSpecCompiler/Compiler/GenericASTPass.cpp @@ -52,7 +52,7 @@ RecursionDecision RecursiveASTVisitor::recurse(Tree root, NodeSubtreePointer& po return RecursionDecision::Continue; } -void GenericASTPass::run() +void GenericASTPass::process_function() { run_in_subtree(m_function->m_ast); } diff --git a/Meta/Lagom/Tools/CodeGenerators/JSSpecCompiler/Compiler/GenericASTPass.h b/Meta/Lagom/Tools/CodeGenerators/JSSpecCompiler/Compiler/GenericASTPass.h index 28fd987525..fbea80347a 100644 --- a/Meta/Lagom/Tools/CodeGenerators/JSSpecCompiler/Compiler/GenericASTPass.h +++ b/Meta/Lagom/Tools/CodeGenerators/JSSpecCompiler/Compiler/GenericASTPass.h @@ -33,15 +33,13 @@ private: }; class GenericASTPass - : public CompilerPass + : public IntraproceduralCompilerPass , protected RecursiveASTVisitor { public: - GenericASTPass(FunctionDefinitionRef function) - : CompilerPass(function) - { - } + using IntraproceduralCompilerPass::IntraproceduralCompilerPass; - void run() override; +protected: + void process_function() override; }; } diff --git a/Meta/Lagom/Tools/CodeGenerators/JSSpecCompiler/Forward.h b/Meta/Lagom/Tools/CodeGenerators/JSSpecCompiler/Forward.h index 4268b60098..4880b159d1 100644 --- a/Meta/Lagom/Tools/CodeGenerators/JSSpecCompiler/Forward.h +++ b/Meta/Lagom/Tools/CodeGenerators/JSSpecCompiler/Forward.h @@ -61,6 +61,7 @@ class SpecFunction; // Function.h struct TranslationUnit; +using TranslationUnitRef = TranslationUnit*; class FunctionDefinition; using FunctionDefinitionRef = FunctionDefinition*; diff --git a/Meta/Lagom/Tools/CodeGenerators/JSSpecCompiler/Function.h b/Meta/Lagom/Tools/CodeGenerators/JSSpecCompiler/Function.h index b363200a7a..bd34554eb8 100644 --- a/Meta/Lagom/Tools/CodeGenerators/JSSpecCompiler/Function.h +++ b/Meta/Lagom/Tools/CodeGenerators/JSSpecCompiler/Function.h @@ -28,7 +28,7 @@ public: virtual ~FunctionDeclaration() = default; - TranslationUnit* m_translation_unit = nullptr; + TranslationUnitRef m_translation_unit = nullptr; StringView m_name; }; diff --git a/Meta/Lagom/Tools/CodeGenerators/JSSpecCompiler/main.cpp b/Meta/Lagom/Tools/CodeGenerators/JSSpecCompiler/main.cpp index 400618bdc3..8f2604e03f 100644 --- a/Meta/Lagom/Tools/CodeGenerators/JSSpecCompiler/main.cpp +++ b/Meta/Lagom/Tools/CodeGenerators/JSSpecCompiler/main.cpp @@ -56,9 +56,9 @@ ErrorOr serenity_main(Main::Arguments) for (auto const& argument : spec_function.m_arguments) function->m_local_variables.set(argument.name, make_ref_counted(argument.name)); - FunctionCallCanonicalizationPass(function).run(); - IfBranchMergingPass(function).run(); - ReferenceResolvingPass(function).run(); + FunctionCallCanonicalizationPass(&translation_unit).run(); + IfBranchMergingPass(&translation_unit).run(); + ReferenceResolvingPass(&translation_unit).run(); out("{}", function->m_ast); return 0;