main.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. /*
  2. * Copyright (c) 2023, Dan Klishch <danilklishch@gmail.com>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/Format.h>
  7. #include <LibCore/ArgsParser.h>
  8. #include <LibMain/Main.h>
  9. #include "Compiler/Passes/CFGBuildingPass.h"
  10. #include "Compiler/Passes/CFGSimplificationPass.h"
  11. #include "Compiler/Passes/DeadCodeEliminationPass.h"
  12. #include "Compiler/Passes/FunctionCallCanonicalizationPass.h"
  13. #include "Compiler/Passes/IfBranchMergingPass.h"
  14. #include "Compiler/Passes/ReferenceResolvingPass.h"
  15. #include "Compiler/Passes/SSABuildingPass.h"
  16. #include "Function.h"
  17. #include "Parser/CppASTConverter.h"
  18. #include "Parser/SpecParser.h"
  19. using namespace JSSpecCompiler;
  20. struct CompilationStepWithDumpOptions {
  21. OwnPtr<CompilationStep> step;
  22. bool dump_ast = false;
  23. bool dump_cfg = false;
  24. };
  25. class CompilationPipeline {
  26. public:
  27. template<typename T>
  28. void add_compilation_pass()
  29. {
  30. auto func = +[](TranslationUnitRef translation_unit) {
  31. T { translation_unit }.run();
  32. };
  33. add_step(adopt_own_if_nonnull(new NonOwningCompilationStep(T::name, func)));
  34. }
  35. template<typename T>
  36. void for_each_step_in(StringView pass_list, T&& func)
  37. {
  38. HashTable<StringView> selected_steps;
  39. for (auto pass : pass_list.split_view(',')) {
  40. if (pass == "all") {
  41. for (auto const& step : m_pipeline)
  42. selected_steps.set(step.step->name());
  43. } else if (pass == "last") {
  44. selected_steps.set(m_pipeline.last().step->name());
  45. } else if (pass.starts_with('-')) {
  46. VERIFY(selected_steps.remove(pass.substring_view(1)));
  47. } else {
  48. selected_steps.set(pass);
  49. }
  50. }
  51. for (auto& step : m_pipeline)
  52. if (selected_steps.contains(step.step->name()))
  53. func(step);
  54. }
  55. void add_step(OwnPtr<CompilationStep>&& step)
  56. {
  57. m_pipeline.append({ move(step) });
  58. }
  59. auto const& pipeline() const { return m_pipeline; }
  60. private:
  61. Vector<CompilationStepWithDumpOptions> m_pipeline;
  62. };
  63. ErrorOr<int> serenity_main(Main::Arguments arguments)
  64. {
  65. Core::ArgsParser args_parser;
  66. StringView filename;
  67. args_parser.add_positional_argument(filename, "File to compile", "file");
  68. constexpr StringView language_spec = "spec"sv;
  69. constexpr StringView language_cpp = "c++"sv;
  70. StringView language = language_spec;
  71. args_parser.add_option(Core::ArgsParser::Option {
  72. .argument_mode = Core::ArgsParser::OptionArgumentMode::Optional,
  73. .help_string = "Specify the language of the input file.",
  74. .short_name = 'x',
  75. .value_name = "{c++|spec}",
  76. .accept_value = [&](StringView value) {
  77. language = value;
  78. return language.is_one_of(language_spec, language_cpp);
  79. },
  80. });
  81. StringView passes_to_dump_ast;
  82. args_parser.add_option(passes_to_dump_ast, "Dump AST after specified passes.", "dump-ast", 0, "{all|last|<pass-name>|-<pass-name>[,...]}");
  83. StringView passes_to_dump_cfg;
  84. args_parser.add_option(passes_to_dump_cfg, "Dump CFG after specified passes.", "dump-cfg", 0, "{all|last|<pass-name>|-<pass-name>[,...]}");
  85. args_parser.parse(arguments);
  86. CompilationPipeline pipeline;
  87. if (language == language_cpp)
  88. pipeline.add_step(adopt_own_if_nonnull(new CppParsingStep()));
  89. else
  90. pipeline.add_step(adopt_own_if_nonnull(new SpecParsingStep()));
  91. pipeline.add_compilation_pass<FunctionCallCanonicalizationPass>();
  92. pipeline.add_compilation_pass<IfBranchMergingPass>();
  93. pipeline.add_compilation_pass<ReferenceResolvingPass>();
  94. pipeline.add_compilation_pass<CFGBuildingPass>();
  95. pipeline.add_compilation_pass<CFGSimplificationPass>();
  96. pipeline.add_compilation_pass<SSABuildingPass>();
  97. pipeline.add_compilation_pass<DeadCodeEliminationPass>();
  98. pipeline.for_each_step_in(passes_to_dump_ast, [](CompilationStepWithDumpOptions& step) {
  99. step.dump_ast = true;
  100. });
  101. pipeline.for_each_step_in(passes_to_dump_cfg, [](CompilationStepWithDumpOptions& step) {
  102. step.dump_cfg = true;
  103. });
  104. TranslationUnit translation_unit(filename);
  105. // Functions referenced in DifferenceISODate
  106. // TODO: This is here just for testing. In a long run, we need some place, which is not
  107. // `serenity_main`, to store built-in functions.
  108. translation_unit.adopt_declaration(make_ref_counted<FunctionDeclaration>("CompareISODate"sv));
  109. translation_unit.adopt_declaration(make_ref_counted<FunctionDeclaration>("CreateDateDurationRecord"sv));
  110. translation_unit.adopt_declaration(make_ref_counted<FunctionDeclaration>("AddISODate"sv));
  111. translation_unit.adopt_declaration(make_ref_counted<FunctionDeclaration>("ISODaysInMonth"sv));
  112. translation_unit.adopt_declaration(make_ref_counted<FunctionDeclaration>("ISODateToEpochDays"sv));
  113. translation_unit.adopt_declaration(make_ref_counted<FunctionDeclaration>("truncate"sv));
  114. translation_unit.adopt_declaration(make_ref_counted<FunctionDeclaration>("remainder"sv));
  115. for (auto const& step : pipeline.pipeline()) {
  116. step.step->run(&translation_unit);
  117. if (translation_unit.diag().has_fatal_errors()) {
  118. translation_unit.diag().print_diagnostics();
  119. return 1;
  120. }
  121. if (step.dump_ast) {
  122. outln(stderr, "===== AST after {} =====", step.step->name());
  123. for (auto const& function : translation_unit.functions_to_compile()) {
  124. outln(stderr, "{}({}):", function->m_name, function->m_argument_names);
  125. outln(stderr, "{}", function->m_ast);
  126. }
  127. }
  128. if (step.dump_cfg && translation_unit.functions_to_compile()[0]->m_cfg != nullptr) {
  129. outln(stderr, "===== CFG after {} =====", step.step->name());
  130. for (auto const& function : translation_unit.functions_to_compile()) {
  131. outln(stderr, "{}({}):", function->m_name, function->m_argument_names);
  132. outln(stderr, "{}", *function->m_cfg);
  133. }
  134. }
  135. }
  136. translation_unit.diag().print_diagnostics();
  137. return 0;
  138. }