main.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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/main.h"
  5. #include <unistd.h>
  6. #include <chrono>
  7. #include <cstdio>
  8. #include <cstring>
  9. #include <iostream>
  10. #include <optional>
  11. #include <string>
  12. #include <vector>
  13. #include "common/error.h"
  14. #include "explorer/common/arena.h"
  15. #include "explorer/common/nonnull.h"
  16. #include "explorer/interpreter/exec_program.h"
  17. #include "explorer/interpreter/trace_stream.h"
  18. #include "explorer/syntax/parse.h"
  19. #include "explorer/syntax/prelude.h"
  20. #include "llvm/ADT/SmallString.h"
  21. #include "llvm/Support/CommandLine.h"
  22. #include "llvm/Support/FileSystem.h"
  23. #include "llvm/Support/InitLLVM.h"
  24. #include "llvm/Support/Path.h"
  25. #include "llvm/Support/raw_ostream.h"
  26. namespace Carbon {
  27. namespace cl = llvm::cl;
  28. namespace path = llvm::sys::path;
  29. auto ExplorerMain(int argc, char** argv, void* static_for_main_addr,
  30. llvm::StringRef relative_prelude_path) -> int {
  31. llvm::setBugReportMsg(
  32. "Please report issues to "
  33. "https://github.com/carbon-language/carbon-lang/issues and include the "
  34. "crash backtrace.\n");
  35. llvm::InitLLVM init_llvm(argc, argv);
  36. // Printing to stderr should flush stdout. This is most noticeable when stderr
  37. // is piped to stdout.
  38. llvm::errs().tie(&llvm::outs());
  39. cl::opt<std::string> input_file_name(cl::Positional, cl::desc("<input file>"),
  40. cl::Required);
  41. cl::opt<bool> parser_debug("parser_debug",
  42. cl::desc("Enable debug output from the parser"));
  43. cl::opt<std::string> trace_file_name(
  44. "trace_file",
  45. cl::desc("Output file for tracing; set to `-` to output to stdout."));
  46. // Use the executable path as a base for the relative prelude path.
  47. std::string exe =
  48. llvm::sys::fs::getMainExecutable(argv[0], static_for_main_addr);
  49. llvm::StringRef install_path = path::parent_path(exe);
  50. llvm::SmallString<256> default_prelude_file(install_path);
  51. path::append(default_prelude_file,
  52. path::begin(relative_prelude_path, path::Style::posix),
  53. path::end(relative_prelude_path));
  54. std::string default_prelude_file_str(default_prelude_file);
  55. cl::opt<std::string> prelude_file_name("prelude", cl::desc("<prelude file>"),
  56. cl::init(default_prelude_file_str));
  57. cl::ParseCommandLineOptions(argc, argv);
  58. // Set up a stream for trace output.
  59. std::unique_ptr<llvm::raw_ostream> scoped_trace_stream;
  60. TraceStream trace_stream;
  61. if (!trace_file_name.empty()) {
  62. if (trace_file_name == "-") {
  63. trace_stream.set_stream(&llvm::outs());
  64. } else {
  65. std::error_code err;
  66. scoped_trace_stream =
  67. std::make_unique<llvm::raw_fd_ostream>(trace_file_name, err);
  68. if (err) {
  69. llvm::errs() << err.message() << "\n";
  70. return EXIT_FAILURE;
  71. }
  72. trace_stream.set_stream(scoped_trace_stream.get());
  73. }
  74. }
  75. auto time_start = std::chrono::system_clock::now();
  76. Arena arena;
  77. AST ast;
  78. if (ErrorOr<AST> parse_result = Parse(&arena, input_file_name, parser_debug);
  79. parse_result.ok()) {
  80. ast = *std::move(parse_result);
  81. } else {
  82. llvm::errs() << "SYNTAX ERROR: " << parse_result.error() << "\n";
  83. return EXIT_FAILURE;
  84. }
  85. auto time_after_parse = std::chrono::system_clock::now();
  86. AddPrelude(prelude_file_name, &arena, &ast.declarations,
  87. &ast.num_prelude_declarations);
  88. auto time_after_prelude = std::chrono::system_clock::now();
  89. // Semantically analyze the parsed program.
  90. if (ErrorOr<AST> analyze_result = AnalyzeProgram(&arena, ast, &trace_stream);
  91. analyze_result.ok()) {
  92. ast = *std::move(analyze_result);
  93. } else {
  94. llvm::errs() << "COMPILATION ERROR: " << analyze_result.error() << "\n";
  95. return EXIT_FAILURE;
  96. }
  97. auto time_after_analyze = std::chrono::system_clock::now();
  98. // Run the program.
  99. auto ret = EXIT_SUCCESS;
  100. if (ErrorOr<int> exec_result = ExecProgram(&arena, ast, &trace_stream);
  101. exec_result.ok()) {
  102. // Print the return code to stdout.
  103. llvm::outs() << "result: " << *exec_result << "\n";
  104. // When there's a dedicated trace file, print the return code to it too.
  105. if (scoped_trace_stream) {
  106. trace_stream << "result: " << *exec_result << "\n";
  107. }
  108. } else {
  109. llvm::errs() << "RUNTIME ERROR: " << exec_result.error() << "\n";
  110. ret = EXIT_FAILURE;
  111. }
  112. auto time_after_exec = std::chrono::system_clock::now();
  113. if (trace_stream.is_enabled()) {
  114. trace_stream << "Timings:\n"
  115. << "- Parse: "
  116. << std::chrono::duration_cast<std::chrono::milliseconds>(
  117. time_after_parse - time_start)
  118. .count()
  119. << "ms\n"
  120. << "- AddPrelude: "
  121. << std::chrono::duration_cast<std::chrono::milliseconds>(
  122. time_after_prelude - time_after_parse)
  123. .count()
  124. << "ms\n"
  125. << "- AnalyzeProgram: "
  126. << std::chrono::duration_cast<std::chrono::milliseconds>(
  127. time_after_analyze - time_after_prelude)
  128. .count()
  129. << "ms\n"
  130. << "- ExecProgram: "
  131. << std::chrono::duration_cast<std::chrono::milliseconds>(
  132. time_after_exec - time_after_analyze)
  133. .count()
  134. << "ms\n";
  135. }
  136. return ret;
  137. }
  138. } // namespace Carbon