parse_and_execute.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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/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. trace_stream->set_current_phase(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. trace_stream->set_current_phase(ProgramPhase::Unknown);
  30. }
  31. });
  32. return exit_scope_function;
  33. }
  34. static auto ParseAndExecuteHelper(std::function<ErrorOr<AST>(Arena*)> parse,
  35. const std::string& 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. trace_stream->set_current_phase(ProgramPhase::Execution);
  61. ErrorOr<int> exec_result =
  62. ExecProgram(&arena, *analyze_result, trace_stream, print_stream);
  63. auto print_exec_time =
  64. PrintTimingOnExit(trace_stream, "ExecProgram", &cursor);
  65. if (!exec_result.ok()) {
  66. return ErrorBuilder() << "RUNTIME ERROR: " << exec_result.error();
  67. }
  68. trace_stream->set_current_phase(ProgramPhase::Unknown);
  69. auto print_trace_timing_heading = llvm::make_scope_exit([=]() {
  70. trace_stream->set_current_phase(ProgramPhase::Timing);
  71. if (trace_stream->is_enabled()) {
  72. *trace_stream << "********** printing timing **********\n";
  73. }
  74. trace_stream->set_current_phase(ProgramPhase::Unknown);
  75. });
  76. return exec_result;
  77. });
  78. }
  79. auto ParseAndExecuteFile(const std::string& prelude_path,
  80. const std::string& input_file_name, bool parser_debug,
  81. Nonnull<TraceStream*> trace_stream,
  82. Nonnull<llvm::raw_ostream*> print_stream)
  83. -> ErrorOr<int> {
  84. auto parse = [&](Arena* arena) {
  85. return Parse(arena, input_file_name, parser_debug);
  86. };
  87. return ParseAndExecuteHelper(parse, prelude_path, trace_stream, print_stream);
  88. }
  89. auto ParseAndExecute(const std::string& prelude_path, const std::string& source)
  90. -> ErrorOr<int> {
  91. auto parse = [&](Arena* arena) {
  92. return ParseFromString(arena, "test.carbon", source,
  93. /*parser_debug=*/false);
  94. };
  95. TraceStream trace_stream;
  96. return ParseAndExecuteHelper(parse, prelude_path, &trace_stream,
  97. &llvm::nulls());
  98. }
  99. } // namespace Carbon