parse_and_execute.cpp 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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/error.h"
  7. #include "explorer/base/trace_stream.h"
  8. #include "explorer/interpreter/exec_program.h"
  9. #include "explorer/interpreter/stack_space.h"
  10. #include "explorer/syntax/parse.h"
  11. #include "explorer/syntax/prelude.h"
  12. #include "llvm/ADT/ScopeExit.h"
  13. namespace Carbon {
  14. // Returns a scope exit function for printing the timing of a step on scope
  15. // exit. Note the use prints step timings in reverse order.
  16. static auto PrintTimingOnExit(TraceStream* trace_stream, const char* label,
  17. std::chrono::steady_clock::time_point* cursor) {
  18. auto end = std::chrono::steady_clock::now();
  19. auto duration = end - *cursor;
  20. *cursor = end;
  21. auto exit_scope_function = llvm::make_scope_exit([=]() {
  22. SetProgramPhase set_program_phase(*trace_stream, ProgramPhase::Timing);
  23. if (trace_stream->is_enabled()) {
  24. *trace_stream << "Time elapsed in " << label << ": "
  25. << std::chrono::duration_cast<std::chrono::milliseconds>(
  26. duration)
  27. .count()
  28. << "ms\n";
  29. }
  30. });
  31. return exit_scope_function;
  32. }
  33. auto ParseAndExecute(llvm::vfs::FileSystem& fs, std::string_view prelude_path,
  34. std::string_view input_file_name, bool parser_debug,
  35. Nonnull<TraceStream*> trace_stream,
  36. Nonnull<llvm::raw_ostream*> print_stream) -> ErrorOr<int> {
  37. return RunWithExtraStack([&]() -> ErrorOr<int> {
  38. Arena arena;
  39. auto cursor = std::chrono::steady_clock::now();
  40. ErrorOr<AST> parse_result =
  41. Parse(fs, &arena, input_file_name, FileKind::Main, parser_debug);
  42. auto print_parse_time = PrintTimingOnExit(trace_stream, "Parse", &cursor);
  43. if (!parse_result.ok()) {
  44. return ErrorBuilder() << "SYNTAX ERROR: " << parse_result.error();
  45. }
  46. AddPrelude(fs, prelude_path, &arena, &parse_result->declarations,
  47. &parse_result->num_prelude_declarations);
  48. auto print_prelude_time =
  49. PrintTimingOnExit(trace_stream, "AddPrelude", &cursor);
  50. // Semantically analyze the parsed program.
  51. ErrorOr<AST> analyze_result =
  52. AnalyzeProgram(&arena, *parse_result, trace_stream, print_stream);
  53. auto print_analyze_time =
  54. PrintTimingOnExit(trace_stream, "AnalyzeProgram", &cursor);
  55. if (!analyze_result.ok()) {
  56. return ErrorBuilder() << "COMPILATION ERROR: " << analyze_result.error();
  57. }
  58. // Run the program.
  59. ErrorOr<int> exec_result =
  60. ExecProgram(&arena, *analyze_result, trace_stream, print_stream);
  61. auto print_exec_time =
  62. PrintTimingOnExit(trace_stream, "ExecProgram", &cursor);
  63. if (!exec_result.ok()) {
  64. return ErrorBuilder() << "RUNTIME ERROR: " << exec_result.error();
  65. }
  66. auto print_trace_timing_heading = llvm::make_scope_exit([=]() {
  67. SetProgramPhase set_prog_phase(*trace_stream, ProgramPhase::Timing);
  68. if (trace_stream->is_enabled()) {
  69. trace_stream->Heading("printing timing");
  70. }
  71. });
  72. return exec_result;
  73. });
  74. }
  75. } // namespace Carbon