main.cpp 4.8 KB

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