main.cpp 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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. template<>
  64. struct AK::Formatter<Vector<FunctionArgument>> : AK::Formatter<StringView> {
  65. ErrorOr<void> format(FormatBuilder& builder, Vector<FunctionArgument> const& arguments)
  66. {
  67. for (size_t i = 0; i < arguments.size(); ++i) {
  68. TRY(builder.put_string(arguments[i].name));
  69. if (i + 1 != arguments.size())
  70. TRY(builder.put_literal(", "sv));
  71. }
  72. return {};
  73. }
  74. };
  75. ErrorOr<int> serenity_main(Main::Arguments arguments)
  76. {
  77. Core::ArgsParser args_parser;
  78. StringView filename;
  79. args_parser.add_positional_argument(filename, "File to compile", "file");
  80. constexpr StringView language_spec = "spec"sv;
  81. constexpr StringView language_cpp = "c++"sv;
  82. StringView language = language_spec;
  83. args_parser.add_option(Core::ArgsParser::Option {
  84. .argument_mode = Core::ArgsParser::OptionArgumentMode::Optional,
  85. .help_string = "Specify the language of the input file.",
  86. .short_name = 'x',
  87. .value_name = "{c++|spec}",
  88. .accept_value = [&](StringView value) {
  89. language = value;
  90. return language.is_one_of(language_spec, language_cpp);
  91. },
  92. });
  93. StringView passes_to_dump_ast;
  94. args_parser.add_option(passes_to_dump_ast, "Dump AST after specified passes.", "dump-ast", 0, "{all|last|<pass-name>|-<pass-name>[,...]}");
  95. StringView passes_to_dump_cfg;
  96. args_parser.add_option(passes_to_dump_cfg, "Dump CFG after specified passes.", "dump-cfg", 0, "{all|last|<pass-name>|-<pass-name>[,...]}");
  97. args_parser.parse(arguments);
  98. CompilationPipeline pipeline;
  99. if (language == language_cpp)
  100. pipeline.add_step(adopt_own_if_nonnull(new CppParsingStep()));
  101. else
  102. pipeline.add_step(adopt_own_if_nonnull(new SpecParsingStep()));
  103. pipeline.add_compilation_pass<FunctionCallCanonicalizationPass>();
  104. pipeline.add_compilation_pass<IfBranchMergingPass>();
  105. pipeline.add_compilation_pass<ReferenceResolvingPass>();
  106. pipeline.add_compilation_pass<CFGBuildingPass>();
  107. pipeline.add_compilation_pass<CFGSimplificationPass>();
  108. pipeline.add_compilation_pass<SSABuildingPass>();
  109. pipeline.add_compilation_pass<DeadCodeEliminationPass>();
  110. pipeline.for_each_step_in(passes_to_dump_ast, [](CompilationStepWithDumpOptions& step) {
  111. step.dump_ast = true;
  112. });
  113. pipeline.for_each_step_in(passes_to_dump_cfg, [](CompilationStepWithDumpOptions& step) {
  114. step.dump_cfg = true;
  115. });
  116. TranslationUnit translation_unit(filename);
  117. // Functions referenced in DifferenceISODate
  118. // TODO: This is here just for testing. In a long run, we need some place, which is not
  119. // `serenity_main`, to store built-in functions.
  120. translation_unit.adopt_declaration(make_ref_counted<FunctionDeclaration>("CompareISODate"sv, Vector<FunctionArgument> {}));
  121. translation_unit.adopt_declaration(make_ref_counted<FunctionDeclaration>("CreateDateDurationRecord"sv, Vector<FunctionArgument> {}));
  122. translation_unit.adopt_declaration(make_ref_counted<FunctionDeclaration>("AddISODate"sv, Vector<FunctionArgument> {}));
  123. translation_unit.adopt_declaration(make_ref_counted<FunctionDeclaration>("ISODaysInMonth"sv, Vector<FunctionArgument> {}));
  124. translation_unit.adopt_declaration(make_ref_counted<FunctionDeclaration>("ISODateToEpochDays"sv, Vector<FunctionArgument> {}));
  125. translation_unit.adopt_declaration(make_ref_counted<FunctionDeclaration>("truncate"sv, Vector<FunctionArgument> {}));
  126. translation_unit.adopt_declaration(make_ref_counted<FunctionDeclaration>("remainder"sv, Vector<FunctionArgument> {}));
  127. for (auto const& step : pipeline.pipeline()) {
  128. step.step->run(&translation_unit);
  129. if (translation_unit.diag().has_fatal_errors()) {
  130. translation_unit.diag().print_diagnostics();
  131. return 1;
  132. }
  133. if (step.dump_ast) {
  134. outln(stderr, "===== AST after {} =====", step.step->name());
  135. for (auto const& function : translation_unit.functions_to_compile()) {
  136. outln(stderr, "{}({}):", function->m_name, function->m_arguments);
  137. outln(stderr, "{}", function->m_ast);
  138. }
  139. }
  140. if (step.dump_cfg && translation_unit.functions_to_compile()[0]->m_cfg != nullptr) {
  141. outln(stderr, "===== CFG after {} =====", step.step->name());
  142. for (auto const& function : translation_unit.functions_to_compile()) {
  143. outln(stderr, "{}({}):", function->m_name, function->m_arguments);
  144. outln(stderr, "{}", *function->m_cfg);
  145. }
  146. }
  147. }
  148. translation_unit.diag().print_diagnostics();
  149. return 0;
  150. }