main.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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/IfBranchMergingPass.h"
  13. #include "Compiler/Passes/ReferenceResolvingPass.h"
  14. #include "Compiler/Passes/SSABuildingPass.h"
  15. #include "Function.h"
  16. #include "Parser/CppASTConverter.h"
  17. #include "Parser/SpecificationParsing.h"
  18. using namespace JSSpecCompiler;
  19. struct CompilationStepWithDumpOptions {
  20. OwnPtr<CompilationStep> step;
  21. bool dump_ast = false;
  22. bool dump_cfg = false;
  23. };
  24. class CompilationPipeline {
  25. public:
  26. template<typename T>
  27. void add_compilation_pass()
  28. {
  29. auto func = +[](TranslationUnitRef translation_unit) {
  30. T { translation_unit }.run();
  31. };
  32. add_step(adopt_own_if_nonnull(new NonOwningCompilationStep(T::name, func)));
  33. }
  34. template<typename T>
  35. void for_each_step_in(StringView pass_list, T&& func)
  36. {
  37. HashTable<StringView> selected_steps;
  38. for (auto pass : pass_list.split_view(',')) {
  39. if (pass == "all") {
  40. for (auto const& step : m_pipeline)
  41. selected_steps.set(step.step->name());
  42. } else if (pass == "last") {
  43. selected_steps.set(m_pipeline.last().step->name());
  44. } else if (pass.starts_with('-')) {
  45. VERIFY(selected_steps.remove(pass.substring_view(1)));
  46. } else {
  47. selected_steps.set(pass);
  48. }
  49. }
  50. for (auto& step : m_pipeline)
  51. if (selected_steps.contains(step.step->name()))
  52. func(step);
  53. }
  54. void add_step(OwnPtr<CompilationStep>&& step)
  55. {
  56. m_pipeline.append({ move(step) });
  57. }
  58. auto const& pipeline() const { return m_pipeline; }
  59. private:
  60. Vector<CompilationStepWithDumpOptions> m_pipeline;
  61. };
  62. template<>
  63. struct AK::Formatter<ReadonlySpan<FunctionArgument>> : AK::Formatter<StringView> {
  64. ErrorOr<void> format(FormatBuilder& builder, ReadonlySpan<FunctionArgument> const& arguments)
  65. {
  66. size_t previous_optional_group = 0;
  67. for (size_t i = 0; i < arguments.size(); ++i) {
  68. if (previous_optional_group != arguments[i].optional_arguments_group) {
  69. previous_optional_group = arguments[i].optional_arguments_group;
  70. TRY(builder.put_string("["sv));
  71. }
  72. TRY(builder.put_string(arguments[i].name));
  73. if (i + 1 != arguments.size())
  74. TRY(builder.put_literal(", "sv));
  75. }
  76. TRY(builder.put_string(TRY(String::repeated(']', previous_optional_group))));
  77. return {};
  78. }
  79. };
  80. ErrorOr<int> serenity_main(Main::Arguments arguments)
  81. {
  82. Core::ArgsParser args_parser;
  83. StringView filename;
  84. args_parser.add_positional_argument(filename, "File to compile", "file");
  85. constexpr StringView language_spec = "spec"sv;
  86. constexpr StringView language_cpp = "c++"sv;
  87. StringView language = language_spec;
  88. args_parser.add_option(Core::ArgsParser::Option {
  89. .argument_mode = Core::ArgsParser::OptionArgumentMode::Optional,
  90. .help_string = "Specify the language of the input file.",
  91. .short_name = 'x',
  92. .value_name = "{c++|spec}",
  93. .accept_value = [&](StringView value) {
  94. language = value;
  95. return language.is_one_of(language_spec, language_cpp);
  96. },
  97. });
  98. StringView passes_to_dump_ast;
  99. args_parser.add_option(passes_to_dump_ast, "Dump AST after specified passes.", "dump-ast", 0, "{all|last|<pass-name>|-<pass-name>[,...]}");
  100. StringView passes_to_dump_cfg;
  101. args_parser.add_option(passes_to_dump_cfg, "Dump CFG after specified passes.", "dump-cfg", 0, "{all|last|<pass-name>|-<pass-name>[,...]}");
  102. bool silence_diagnostics = false;
  103. args_parser.add_option(silence_diagnostics, "Silence all diagnostics.", "silence-diagnostics", 0);
  104. args_parser.parse(arguments);
  105. CompilationPipeline pipeline;
  106. if (language == language_cpp)
  107. pipeline.add_step(adopt_own_if_nonnull(new CppParsingStep()));
  108. else
  109. pipeline.add_step(adopt_own_if_nonnull(new SpecificationParsingStep()));
  110. pipeline.add_compilation_pass<IfBranchMergingPass>();
  111. pipeline.add_compilation_pass<ReferenceResolvingPass>();
  112. pipeline.add_compilation_pass<CFGBuildingPass>();
  113. pipeline.add_compilation_pass<CFGSimplificationPass>();
  114. pipeline.add_compilation_pass<SSABuildingPass>();
  115. pipeline.add_compilation_pass<DeadCodeEliminationPass>();
  116. pipeline.for_each_step_in(passes_to_dump_ast, [](CompilationStepWithDumpOptions& step) {
  117. step.dump_ast = true;
  118. });
  119. pipeline.for_each_step_in(passes_to_dump_cfg, [](CompilationStepWithDumpOptions& step) {
  120. step.dump_cfg = true;
  121. });
  122. TranslationUnit translation_unit(filename);
  123. for (auto const& step : pipeline.pipeline()) {
  124. step.step->run(&translation_unit);
  125. if (translation_unit.diag().has_fatal_errors()) {
  126. translation_unit.diag().print_diagnostics();
  127. return 1;
  128. }
  129. if (step.dump_ast) {
  130. outln(stderr, "===== AST after {} =====", step.step->name());
  131. for (auto const& function : translation_unit.functions_to_compile()) {
  132. outln(stderr, "{}({}):", function->name(), function->arguments());
  133. outln(stderr, "{}", function->m_ast);
  134. }
  135. }
  136. if (step.dump_cfg && translation_unit.functions_to_compile().size() && translation_unit.functions_to_compile()[0]->m_cfg != nullptr) {
  137. outln(stderr, "===== CFG after {} =====", step.step->name());
  138. for (auto const& function : translation_unit.functions_to_compile()) {
  139. outln(stderr, "{}({}):", function->name(), function->arguments());
  140. outln(stderr, "{}", *function->m_cfg);
  141. }
  142. }
  143. }
  144. if (!silence_diagnostics)
  145. translation_unit.diag().print_diagnostics();
  146. return 0;
  147. }