main.cpp 5.7 KB

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