main.cpp 5.5 KB

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