parse_and_execute.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. // Part of the Carbon Language project, under the Apache License v2.0 with LLVM
  2. // Exceptions. See /LICENSE for license information.
  3. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. #include "explorer/parse_and_execute/parse_and_execute.h"
  5. #include <locale>
  6. #include "common/check.h"
  7. #include "common/error.h"
  8. #include "explorer/common/trace_stream.h"
  9. #include "explorer/interpreter/exec_program.h"
  10. #include "explorer/interpreter/stack_space.h"
  11. #include "explorer/syntax/parse.h"
  12. #include "explorer/syntax/prelude.h"
  13. #include "llvm/ADT/ScopeExit.h"
  14. namespace Carbon {
  15. // Returns a scope exit function for printing the timing of a step on scope
  16. // exit. Note the use prints step timings in reverse order.
  17. static auto PrintTimingOnExit(TraceStream* trace_stream, const char* label,
  18. std::chrono::steady_clock::time_point* cursor) {
  19. auto end = std::chrono::steady_clock::now();
  20. auto duration = end - *cursor;
  21. *cursor = end;
  22. auto exit_scope_function = llvm::make_scope_exit([=]() {
  23. SetProgramPhase set_program_phase(*trace_stream, ProgramPhase::Timing);
  24. if (trace_stream->is_enabled()) {
  25. *trace_stream << "Time elapsed in " << label << ": "
  26. << std::chrono::duration_cast<std::chrono::milliseconds>(
  27. duration)
  28. .count()
  29. << "ms\n";
  30. }
  31. });
  32. return exit_scope_function;
  33. }
  34. static auto ParseAndExecuteHelper(std::function<ErrorOr<AST>(Arena*)> parse,
  35. std::string_view prelude_path,
  36. Nonnull<TraceStream*> trace_stream,
  37. Nonnull<llvm::raw_ostream*> print_stream)
  38. -> ErrorOr<int> {
  39. return RunWithExtraStack([&]() -> ErrorOr<int> {
  40. Arena arena;
  41. auto cursor = std::chrono::steady_clock::now();
  42. ErrorOr<AST> parse_result = parse(&arena);
  43. auto print_parse_time = PrintTimingOnExit(trace_stream, "Parse", &cursor);
  44. if (!parse_result.ok()) {
  45. return ErrorBuilder() << "SYNTAX ERROR: " << parse_result.error();
  46. }
  47. AddPrelude(prelude_path, &arena, &parse_result->declarations,
  48. &parse_result->num_prelude_declarations);
  49. auto print_prelude_time =
  50. PrintTimingOnExit(trace_stream, "AddPrelude", &cursor);
  51. // Semantically analyze the parsed program.
  52. ErrorOr<AST> analyze_result =
  53. AnalyzeProgram(&arena, *parse_result, trace_stream, print_stream);
  54. auto print_analyze_time =
  55. PrintTimingOnExit(trace_stream, "AnalyzeProgram", &cursor);
  56. if (!analyze_result.ok()) {
  57. return ErrorBuilder() << "COMPILATION ERROR: " << analyze_result.error();
  58. }
  59. // Run the program.
  60. ErrorOr<int> exec_result =
  61. ExecProgram(&arena, *analyze_result, trace_stream, print_stream);
  62. auto print_exec_time =
  63. PrintTimingOnExit(trace_stream, "ExecProgram", &cursor);
  64. if (!exec_result.ok()) {
  65. return ErrorBuilder() << "RUNTIME ERROR: " << exec_result.error();
  66. }
  67. auto print_trace_timing_heading = llvm::make_scope_exit([=]() {
  68. SetProgramPhase set_prog_phase(*trace_stream, ProgramPhase::Timing);
  69. if (trace_stream->is_enabled()) {
  70. *trace_stream << "********** printing timing **********\n";
  71. }
  72. });
  73. return exec_result;
  74. });
  75. }
  76. auto ParseAndExecuteFile(std::string_view prelude_path,
  77. std::string_view input_file_name, bool parser_debug,
  78. Nonnull<TraceStream*> trace_stream,
  79. Nonnull<llvm::raw_ostream*> print_stream)
  80. -> ErrorOr<int> {
  81. auto parse = [&](Arena* arena) {
  82. return Parse(arena, input_file_name, FileKind::Main, parser_debug);
  83. };
  84. return ParseAndExecuteHelper(parse, prelude_path, trace_stream, print_stream);
  85. }
  86. auto ParseAndExecute(std::string_view prelude_path,
  87. std::string_view input_file_name,
  88. std::string_view file_contents, bool parser_debug,
  89. Nonnull<TraceStream*> trace_stream,
  90. Nonnull<llvm::raw_ostream*> print_stream) -> ErrorOr<int> {
  91. auto parse = [&](Arena* arena) {
  92. return ParseFromString(arena, input_file_name, FileKind::Main,
  93. file_contents, parser_debug);
  94. };
  95. return ParseAndExecuteHelper(parse, prelude_path, trace_stream, print_stream);
  96. }
  97. } // namespace Carbon