driver.cpp 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832
  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 "toolchain/driver/driver.h"
  5. #include <algorithm>
  6. #include <memory>
  7. #include <optional>
  8. #include "common/command_line.h"
  9. #include "common/vlog.h"
  10. #include "llvm/ADT/ArrayRef.h"
  11. #include "llvm/ADT/ScopeExit.h"
  12. #include "llvm/ADT/StringExtras.h"
  13. #include "llvm/ADT/StringRef.h"
  14. #include "llvm/IR/LLVMContext.h"
  15. #include "llvm/Support/Path.h"
  16. #include "llvm/TargetParser/Host.h"
  17. #include "toolchain/base/value_store.h"
  18. #include "toolchain/check/check.h"
  19. #include "toolchain/codegen/codegen.h"
  20. #include "toolchain/diagnostics/sorting_diagnostic_consumer.h"
  21. #include "toolchain/lex/lex.h"
  22. #include "toolchain/lower/lower.h"
  23. #include "toolchain/parse/parse.h"
  24. #include "toolchain/sem_ir/formatter.h"
  25. #include "toolchain/sem_ir/inst_namer.h"
  26. #include "toolchain/source/source_buffer.h"
  27. namespace Carbon {
  28. auto Driver::FindPreludeFiles(llvm::StringRef data_dir,
  29. llvm::raw_ostream& error_stream)
  30. -> llvm::SmallVector<std::string> {
  31. llvm::SmallVector<std::string> result;
  32. // Include <data>/core/prelude.carbon, which is the entry point into the
  33. // prelude.
  34. {
  35. llvm::SmallString<256> prelude_file(data_dir);
  36. llvm::sys::path::append(prelude_file, llvm::sys::path::Style::posix,
  37. "core/prelude.carbon");
  38. result.push_back(prelude_file.str().str());
  39. }
  40. // Glob for <data>/core/prelude/**/*.carbon and add all the files we find.
  41. llvm::SmallString<256> prelude_dir(data_dir);
  42. llvm::sys::path::append(prelude_dir, llvm::sys::path::Style::posix,
  43. "core/prelude");
  44. std::error_code ec;
  45. for (llvm::sys::fs::recursive_directory_iterator prelude_files_it(
  46. prelude_dir, ec, /*follow_symlinks=*/false);
  47. prelude_files_it != llvm::sys::fs::recursive_directory_iterator();
  48. prelude_files_it.increment(ec)) {
  49. if (ec) {
  50. error_stream << "ERROR: Could not find prelude: " << ec.message() << "\n";
  51. result.clear();
  52. return result;
  53. }
  54. auto prelude_file = prelude_files_it->path();
  55. if (llvm::sys::path::extension(prelude_file) == ".carbon") {
  56. result.push_back(prelude_file);
  57. }
  58. }
  59. return result;
  60. }
  61. struct Driver::CompileOptions {
  62. static constexpr CommandLine::CommandInfo Info = {
  63. .name = "compile",
  64. .help = R"""(
  65. Compile Carbon source code.
  66. This subcommand runs the Carbon compiler over input source code, checking it for
  67. errors and producing the requested output.
  68. Error messages are written to the standard error stream.
  69. Different phases of the compiler can be selected to run, and intermediate state
  70. can be written to standard output as these phases progress.
  71. )""",
  72. };
  73. enum class Phase : int8_t {
  74. Lex,
  75. Parse,
  76. Check,
  77. Lower,
  78. CodeGen,
  79. };
  80. friend auto operator<<(llvm::raw_ostream& out, Phase phase)
  81. -> llvm::raw_ostream& {
  82. switch (phase) {
  83. case Phase::Lex:
  84. out << "lex";
  85. break;
  86. case Phase::Parse:
  87. out << "parse";
  88. break;
  89. case Phase::Check:
  90. out << "check";
  91. break;
  92. case Phase::Lower:
  93. out << "lower";
  94. break;
  95. case Phase::CodeGen:
  96. out << "codegen";
  97. break;
  98. }
  99. return out;
  100. }
  101. void Build(CommandLine::CommandBuilder& b) {
  102. b.AddStringPositionalArg(
  103. {
  104. .name = "FILE",
  105. .help = R"""(
  106. The input Carbon source file to compile.
  107. )""",
  108. },
  109. [&](auto& arg_b) {
  110. arg_b.Required(true);
  111. arg_b.Append(&input_filenames);
  112. });
  113. b.AddOneOfOption(
  114. {
  115. .name = "phase",
  116. .help = R"""(
  117. Selects the compilation phase to run. These phases are always run in sequence,
  118. so every phase before the one selected will also be run. The default is to
  119. compile to machine code.
  120. )""",
  121. },
  122. [&](auto& arg_b) {
  123. arg_b.SetOneOf(
  124. {
  125. arg_b.OneOfValue("lex", Phase::Lex),
  126. arg_b.OneOfValue("parse", Phase::Parse),
  127. arg_b.OneOfValue("check", Phase::Check),
  128. arg_b.OneOfValue("lower", Phase::Lower),
  129. arg_b.OneOfValue("codegen", Phase::CodeGen).Default(true),
  130. },
  131. &phase);
  132. });
  133. // TODO: Rearrange the code setting this option and two related ones to
  134. // allow them to reference each other instead of hard-coding their names.
  135. b.AddStringOption(
  136. {
  137. .name = "output",
  138. .value_name = "FILE",
  139. .help = R"""(
  140. The output filename for codegen.
  141. When this is a file name, either textual assembly or a binary object will be
  142. written to it based on the flag `--asm-output`. The default is to write a binary
  143. object file.
  144. Passing `--output=-` will write the output to stdout. In that case, the flag
  145. `--asm-output` is ignored and the output defaults to textual assembly. Binary
  146. object output can be forced by enabling `--force-obj-output`.
  147. )""",
  148. },
  149. [&](auto& arg_b) { arg_b.Set(&output_filename); });
  150. b.AddStringOption(
  151. {
  152. .name = "target",
  153. .help = R"""(
  154. Select a target platform. Uses the LLVM target syntax. Also known as a "triple"
  155. for historical reasons.
  156. This corresponds to the `target` flag to Clang and accepts the same strings
  157. documented there:
  158. https://clang.llvm.org/docs/CrossCompilation.html#target-triple
  159. )""",
  160. },
  161. [&](auto& arg_b) {
  162. arg_b.Default(host);
  163. arg_b.Set(&target);
  164. });
  165. b.AddFlag(
  166. {
  167. .name = "asm-output",
  168. .help = R"""(
  169. Write textual assembly rather than a binary object file to the code generation
  170. output.
  171. This flag only applies when writing to a file. When writing to stdout, the
  172. default is textual assembly and this flag is ignored.
  173. )""",
  174. },
  175. [&](auto& arg_b) { arg_b.Set(&asm_output); });
  176. b.AddFlag(
  177. {
  178. .name = "force-obj-output",
  179. .help = R"""(
  180. Force binary object output, even with `--output=-`.
  181. When `--output=-` is set, the default is textual assembly; this forces printing
  182. of a binary object file instead. Ignored for other `--output` values.
  183. )""",
  184. },
  185. [&](auto& arg_b) { arg_b.Set(&force_obj_output); });
  186. b.AddFlag(
  187. {
  188. .name = "stream-errors",
  189. .help = R"""(
  190. Stream error messages to stderr as they are generated rather than sorting them
  191. and displaying them in source order.
  192. )""",
  193. },
  194. [&](auto& arg_b) { arg_b.Set(&stream_errors); });
  195. b.AddFlag(
  196. {
  197. .name = "dump-shared-values",
  198. .help = R"""(
  199. Dumps shared values. These aren't owned by any particular file or phase.
  200. )""",
  201. },
  202. [&](auto& arg_b) { arg_b.Set(&dump_shared_values); });
  203. b.AddFlag(
  204. {
  205. .name = "dump-tokens",
  206. .help = R"""(
  207. Dump the tokens to stdout when lexed.
  208. )""",
  209. },
  210. [&](auto& arg_b) { arg_b.Set(&dump_tokens); });
  211. b.AddFlag(
  212. {
  213. .name = "dump-parse-tree",
  214. .help = R"""(
  215. Dump the parse tree to stdout when parsed.
  216. )""",
  217. },
  218. [&](auto& arg_b) { arg_b.Set(&dump_parse_tree); });
  219. b.AddFlag(
  220. {
  221. .name = "preorder-parse-tree",
  222. .help = R"""(
  223. When dumping the parse tree, reorder it so that it is in preorder rather than
  224. postorder.
  225. )""",
  226. },
  227. [&](auto& arg_b) { arg_b.Set(&preorder_parse_tree); });
  228. b.AddFlag(
  229. {
  230. .name = "dump-raw-sem-ir",
  231. .help = R"""(
  232. Dump the raw JSON structure of SemIR to stdout when built.
  233. )""",
  234. },
  235. [&](auto& arg_b) { arg_b.Set(&dump_raw_sem_ir); });
  236. b.AddFlag(
  237. {
  238. .name = "dump-sem-ir",
  239. .help = R"""(
  240. Dump the SemIR to stdout when built.
  241. )""",
  242. },
  243. [&](auto& arg_b) { arg_b.Set(&dump_sem_ir); });
  244. b.AddFlag(
  245. {
  246. .name = "builtin-sem-ir",
  247. .help = R"""(
  248. Include the SemIR for builtins when dumping it.
  249. )""",
  250. },
  251. [&](auto& arg_b) { arg_b.Set(&builtin_sem_ir); });
  252. b.AddFlag(
  253. {
  254. .name = "dump-llvm-ir",
  255. .help = R"""(
  256. Dump the LLVM IR to stdout after lowering.
  257. )""",
  258. },
  259. [&](auto& arg_b) { arg_b.Set(&dump_llvm_ir); });
  260. b.AddFlag(
  261. {
  262. .name = "dump-asm",
  263. .help = R"""(
  264. Dump the generated assembly to stdout after codegen.
  265. )""",
  266. },
  267. [&](auto& arg_b) { arg_b.Set(&dump_asm); });
  268. b.AddFlag(
  269. {
  270. .name = "prelude-import",
  271. .help = R"""(
  272. Whether to use the implicit prelude import. Enabled by default.
  273. )""",
  274. },
  275. [&](auto& arg_b) {
  276. arg_b.Default(true);
  277. arg_b.Set(&prelude_import);
  278. });
  279. b.AddStringOption(
  280. {
  281. .name = "exclude-dump-file-prefix",
  282. .value_name = "PREFIX",
  283. .help = R"""(
  284. Excludes files with the given prefix from dumps.
  285. )""",
  286. },
  287. [&](auto& arg_b) { arg_b.Set(&exclude_dump_file_prefix); });
  288. }
  289. Phase phase;
  290. std::string host = llvm::sys::getDefaultTargetTriple();
  291. llvm::StringRef target;
  292. llvm::StringRef output_filename;
  293. llvm::SmallVector<llvm::StringRef> input_filenames;
  294. bool asm_output = false;
  295. bool force_obj_output = false;
  296. bool dump_shared_values = false;
  297. bool dump_tokens = false;
  298. bool dump_parse_tree = false;
  299. bool dump_raw_sem_ir = false;
  300. bool dump_sem_ir = false;
  301. bool dump_llvm_ir = false;
  302. bool dump_asm = false;
  303. bool stream_errors = false;
  304. bool preorder_parse_tree = false;
  305. bool builtin_sem_ir = false;
  306. bool prelude_import = false;
  307. llvm::StringRef exclude_dump_file_prefix;
  308. };
  309. struct Driver::Options {
  310. static constexpr CommandLine::CommandInfo Info = {
  311. .name = "carbon",
  312. // TODO: Set up more detailed version information and use that here.
  313. .version = R"""(
  314. Carbon Language toolchain -- version 0.0.0
  315. )""",
  316. .help = R"""(
  317. This is the unified Carbon Language toolchain driver. Its subcommands provide
  318. all of the core behavior of the toolchain, including compilation, linking, and
  319. developer tools. Each of these has its own subcommand, and you can pass a
  320. specific subcommand to the `help` subcommand to get details about its usage.
  321. )""",
  322. .help_epilogue = R"""(
  323. For questions, issues, or bug reports, please use our GitHub project:
  324. https://github.com/carbon-language/carbon-lang
  325. )""",
  326. };
  327. enum class Subcommand : int8_t {
  328. Compile,
  329. };
  330. void Build(CommandLine::CommandBuilder& b) {
  331. b.AddFlag(
  332. {
  333. .name = "verbose",
  334. .short_name = "v",
  335. .help = "Enable verbose logging to the stderr stream.",
  336. },
  337. [&](CommandLine::FlagBuilder& arg_b) { arg_b.Set(&verbose); });
  338. b.AddSubcommand(CompileOptions::Info,
  339. [&](CommandLine::CommandBuilder& sub_b) {
  340. compile_options.Build(sub_b);
  341. sub_b.Do([&] { subcommand = Subcommand::Compile; });
  342. });
  343. b.RequiresSubcommand();
  344. }
  345. bool verbose;
  346. Subcommand subcommand;
  347. CompileOptions compile_options;
  348. };
  349. auto Driver::ParseArgs(llvm::ArrayRef<llvm::StringRef> args, Options& options)
  350. -> CommandLine::ParseResult {
  351. return CommandLine::Parse(
  352. args, output_stream_, error_stream_, Options::Info,
  353. [&](CommandLine::CommandBuilder& b) { options.Build(b); });
  354. }
  355. auto Driver::RunCommand(llvm::ArrayRef<llvm::StringRef> args) -> RunResult {
  356. Options options;
  357. CommandLine::ParseResult result = ParseArgs(args, options);
  358. if (result == CommandLine::ParseResult::Error) {
  359. return {.success = false};
  360. } else if (result == CommandLine::ParseResult::MetaSuccess) {
  361. return {.success = true};
  362. }
  363. if (options.verbose) {
  364. // Note this implies streamed output in order to interleave.
  365. vlog_stream_ = &error_stream_;
  366. }
  367. switch (options.subcommand) {
  368. case Options::Subcommand::Compile:
  369. return Compile(options.compile_options);
  370. }
  371. llvm_unreachable("All subcommands handled!");
  372. }
  373. auto Driver::ValidateCompileOptions(const CompileOptions& options) const
  374. -> bool {
  375. using Phase = CompileOptions::Phase;
  376. switch (options.phase) {
  377. case Phase::Lex:
  378. if (options.dump_parse_tree) {
  379. error_stream_ << "ERROR: Requested dumping the parse tree but compile "
  380. "phase is limited to '"
  381. << options.phase << "'.\n";
  382. return false;
  383. }
  384. [[fallthrough]];
  385. case Phase::Parse:
  386. if (options.dump_sem_ir) {
  387. error_stream_ << "ERROR: Requested dumping the SemIR but compile phase "
  388. "is limited to '"
  389. << options.phase << "'.\n";
  390. return false;
  391. }
  392. [[fallthrough]];
  393. case Phase::Check:
  394. if (options.dump_llvm_ir) {
  395. error_stream_ << "ERROR: Requested dumping the LLVM IR but compile "
  396. "phase is limited to '"
  397. << options.phase << "'.\n";
  398. return false;
  399. }
  400. [[fallthrough]];
  401. case Phase::Lower:
  402. case Phase::CodeGen:
  403. // Everything can be dumped in these phases.
  404. break;
  405. }
  406. return true;
  407. }
  408. // Ties together information for a file being compiled.
  409. class Driver::CompilationUnit {
  410. public:
  411. explicit CompilationUnit(Driver* driver, const CompileOptions& options,
  412. DiagnosticConsumer* consumer,
  413. llvm::StringRef input_filename)
  414. : driver_(driver),
  415. options_(options),
  416. input_filename_(input_filename),
  417. vlog_stream_(driver_->vlog_stream_) {
  418. if (vlog_stream_ != nullptr || options_.stream_errors) {
  419. consumer_ = consumer;
  420. } else {
  421. sorting_consumer_ = SortingDiagnosticConsumer(*consumer);
  422. consumer_ = &*sorting_consumer_;
  423. }
  424. }
  425. // Loads source and lexes it. Returns true on success.
  426. auto RunLex() -> void {
  427. LogCall("SourceBuffer::MakeFromFile", [&] {
  428. if (input_filename_ == "-") {
  429. source_ = SourceBuffer::MakeFromStdin(*consumer_);
  430. } else {
  431. source_ = SourceBuffer::MakeFromFile(driver_->fs_, input_filename_,
  432. *consumer_);
  433. }
  434. });
  435. if (!source_) {
  436. success_ = false;
  437. return;
  438. }
  439. CARBON_VLOG() << "*** SourceBuffer ***\n```\n"
  440. << source_->text() << "\n```\n";
  441. LogCall("Lex::Lex",
  442. [&] { tokens_ = Lex::Lex(value_stores_, *source_, *consumer_); });
  443. if (options_.dump_tokens && IncludeInDumps()) {
  444. consumer_->Flush();
  445. driver_->output_stream_ << tokens_;
  446. }
  447. CARBON_VLOG() << "*** Lex::TokenizedBuffer ***\n" << tokens_;
  448. if (tokens_->has_errors()) {
  449. success_ = false;
  450. }
  451. }
  452. // Parses tokens. Returns true on success.
  453. auto RunParse() -> void {
  454. CARBON_CHECK(tokens_);
  455. LogCall("Parse::Parse", [&] {
  456. parse_tree_ = Parse::Parse(*tokens_, *consumer_, vlog_stream_);
  457. });
  458. if (options_.dump_parse_tree && IncludeInDumps()) {
  459. consumer_->Flush();
  460. parse_tree_->Print(driver_->output_stream_, options_.preorder_parse_tree);
  461. }
  462. CARBON_VLOG() << "*** Parse::Tree ***\n" << parse_tree_;
  463. if (parse_tree_->has_errors()) {
  464. success_ = false;
  465. }
  466. }
  467. // Returns information needed to check this unit.
  468. auto GetCheckUnit() -> Check::Unit {
  469. CARBON_CHECK(parse_tree_);
  470. return {.value_stores = &value_stores_,
  471. .tokens = &*tokens_,
  472. .parse_tree = &*parse_tree_,
  473. .consumer = consumer_,
  474. .sem_ir = &sem_ir_};
  475. }
  476. // Runs post-check logic. Returns true if checking succeeded for the IR.
  477. auto PostCheck() -> void {
  478. CARBON_CHECK(sem_ir_);
  479. // We've finished all steps that can produce diagnostics. Emit the
  480. // diagnostics now, so that the developer sees them sooner and doesn't need
  481. // to wait for code generation.
  482. consumer_->Flush();
  483. CARBON_VLOG() << "*** Raw SemIR::File ***\n" << *sem_ir_ << "\n";
  484. if (options_.dump_raw_sem_ir && IncludeInDumps()) {
  485. sem_ir_->Print(driver_->output_stream_, options_.builtin_sem_ir);
  486. if (options_.dump_sem_ir) {
  487. driver_->output_stream_ << "\n";
  488. }
  489. }
  490. if (vlog_stream_) {
  491. CARBON_VLOG() << "*** SemIR::File ***\n";
  492. SemIR::FormatFile(*tokens_, *parse_tree_, *sem_ir_, *vlog_stream_);
  493. }
  494. if (options_.dump_sem_ir && IncludeInDumps()) {
  495. SemIR::FormatFile(*tokens_, *parse_tree_, *sem_ir_,
  496. driver_->output_stream_);
  497. }
  498. if (sem_ir_->has_errors()) {
  499. success_ = false;
  500. }
  501. }
  502. // Lower SemIR to LLVM IR.
  503. auto RunLower() -> void {
  504. CARBON_CHECK(sem_ir_);
  505. LogCall("Lower::LowerToLLVM", [&] {
  506. llvm_context_ = std::make_unique<llvm::LLVMContext>();
  507. // TODO: Consider disabling instruction naming by default if we're not
  508. // producing textual LLVM IR.
  509. SemIR::InstNamer inst_namer(*tokens_, *parse_tree_, *sem_ir_);
  510. module_ = Lower::LowerToLLVM(*llvm_context_, input_filename_, *sem_ir_,
  511. &inst_namer, vlog_stream_);
  512. });
  513. if (vlog_stream_) {
  514. CARBON_VLOG() << "*** llvm::Module ***\n";
  515. module_->print(*vlog_stream_, /*AAW=*/nullptr,
  516. /*ShouldPreserveUseListOrder=*/false,
  517. /*IsForDebug=*/true);
  518. }
  519. if (options_.dump_llvm_ir && IncludeInDumps()) {
  520. module_->print(driver_->output_stream_, /*AAW=*/nullptr,
  521. /*ShouldPreserveUseListOrder=*/true);
  522. }
  523. }
  524. auto RunCodeGen() -> void {
  525. CARBON_CHECK(module_);
  526. LogCall("CodeGen", [&] { success_ = RunCodeGenHelper(); });
  527. }
  528. // Runs post-compile logic. This is always called, and called after all other
  529. // actions on the CompilationUnit.
  530. auto PostCompile() const -> void {
  531. if (options_.dump_shared_values && IncludeInDumps()) {
  532. Yaml::Print(driver_->output_stream_,
  533. value_stores_.OutputYaml(input_filename_));
  534. }
  535. // The diagnostics consumer must be flushed before compilation artifacts are
  536. // destructed, because diagnostics can refer to their state.
  537. consumer_->Flush();
  538. }
  539. auto input_filename() -> llvm::StringRef { return input_filename_; }
  540. auto success() -> bool { return success_; }
  541. auto has_source() -> bool { return source_.has_value(); }
  542. private:
  543. // Do codegen. Returns true on success.
  544. auto RunCodeGenHelper() -> bool {
  545. std::optional<CodeGen> codegen =
  546. CodeGen::Make(*module_, options_.target, driver_->error_stream_);
  547. if (!codegen) {
  548. return false;
  549. }
  550. if (vlog_stream_) {
  551. CARBON_VLOG() << "*** Assembly ***\n";
  552. codegen->EmitAssembly(*vlog_stream_);
  553. }
  554. if (options_.output_filename == "-") {
  555. // TODO: the output file name, forcing object output, and requesting
  556. // textual assembly output are all somewhat linked flags. We should add
  557. // some validation that they are used correctly.
  558. if (options_.force_obj_output) {
  559. if (!codegen->EmitObject(driver_->output_stream_)) {
  560. return false;
  561. }
  562. } else {
  563. if (!codegen->EmitAssembly(driver_->output_stream_)) {
  564. return false;
  565. }
  566. }
  567. } else {
  568. llvm::SmallString<256> output_filename = options_.output_filename;
  569. if (output_filename.empty()) {
  570. if (!source_->is_regular_file()) {
  571. // Don't invent file names like `-.o` or `/dev/stdin.o`.
  572. driver_->error_stream_
  573. << "ERROR: Output file name must be specified for input '"
  574. << input_filename_ << "' that is not a regular file.\n";
  575. return false;
  576. }
  577. output_filename = input_filename_;
  578. llvm::sys::path::replace_extension(output_filename,
  579. options_.asm_output ? ".s" : ".o");
  580. } else {
  581. // TODO: Handle the case where multiple input files were specified
  582. // along with an output file name. That should either be an error or
  583. // should produce a single LLVM IR module containing all inputs.
  584. // Currently each unit overwrites the output from the previous one in
  585. // this case.
  586. }
  587. CARBON_VLOG() << "Writing output to: " << output_filename << "\n";
  588. std::error_code ec;
  589. llvm::raw_fd_ostream output_file(output_filename, ec,
  590. llvm::sys::fs::OF_None);
  591. if (ec) {
  592. driver_->error_stream_ << "ERROR: Could not open output file '"
  593. << output_filename << "': " << ec.message()
  594. << "\n";
  595. return false;
  596. }
  597. if (options_.asm_output) {
  598. if (!codegen->EmitAssembly(output_file)) {
  599. return false;
  600. }
  601. } else {
  602. if (!codegen->EmitObject(output_file)) {
  603. return false;
  604. }
  605. }
  606. }
  607. return true;
  608. }
  609. // Wraps a call with log statements to indicate start and end.
  610. auto LogCall(llvm::StringLiteral label, llvm::function_ref<void()> fn)
  611. -> void {
  612. CARBON_VLOG() << "*** " << label << ": " << input_filename_ << " ***\n";
  613. fn();
  614. CARBON_VLOG() << "*** " << label << " done ***\n";
  615. }
  616. // Returns true if the file can be dumped.
  617. auto IncludeInDumps() const -> bool {
  618. return options_.exclude_dump_file_prefix.empty() ||
  619. !input_filename_.starts_with(options_.exclude_dump_file_prefix);
  620. }
  621. Driver* driver_;
  622. SharedValueStores value_stores_;
  623. const CompileOptions& options_;
  624. std::string input_filename_;
  625. // Copied from driver_ for CARBON_VLOG.
  626. llvm::raw_pwrite_stream* vlog_stream_;
  627. // Diagnostics are sent to consumer_, with optional sorting.
  628. std::optional<SortingDiagnosticConsumer> sorting_consumer_;
  629. DiagnosticConsumer* consumer_;
  630. bool success_ = true;
  631. // These are initialized as steps are run.
  632. std::optional<SourceBuffer> source_;
  633. std::optional<Lex::TokenizedBuffer> tokens_;
  634. std::optional<Parse::Tree> parse_tree_;
  635. std::optional<SemIR::File> sem_ir_;
  636. std::unique_ptr<llvm::LLVMContext> llvm_context_;
  637. std::unique_ptr<llvm::Module> module_;
  638. };
  639. auto Driver::Compile(const CompileOptions& options) -> RunResult {
  640. if (!ValidateCompileOptions(options)) {
  641. return {.success = false};
  642. }
  643. // Find the files comprising the prelude if we are importing it.
  644. // TODO: Replace this with a search for library api files in a
  645. // package-specific search path based on the library name.
  646. bool want_prelude =
  647. options.prelude_import && options.phase >= CompileOptions::Phase::Check;
  648. auto prelude = want_prelude ? FindPreludeFiles(data_dir_, error_stream_)
  649. : llvm::SmallVector<std::string>{};
  650. if (want_prelude && prelude.empty()) {
  651. return {.success = false};
  652. }
  653. // Prepare CompilationUnits before building scope exit handlers.
  654. StreamDiagnosticConsumer stream_consumer(error_stream_);
  655. llvm::SmallVector<std::unique_ptr<CompilationUnit>> units;
  656. units.reserve(prelude.size() + options.input_filenames.size());
  657. // Add the prelude files.
  658. for (const auto& input_filename : prelude) {
  659. units.push_back(std::make_unique<CompilationUnit>(
  660. this, options, &stream_consumer, input_filename));
  661. }
  662. // Add the input source files.
  663. for (const auto& input_filename : options.input_filenames) {
  664. units.push_back(std::make_unique<CompilationUnit>(
  665. this, options, &stream_consumer, input_filename));
  666. }
  667. auto on_exit = llvm::make_scope_exit([&]() {
  668. // Finish compilation units. This flushes their diagnostics in the order in
  669. // which they were specified on the command line.
  670. for (auto& unit : units) {
  671. unit->PostCompile();
  672. }
  673. stream_consumer.Flush();
  674. });
  675. // Returns a RunResult object. Called whenever Compile returns.
  676. auto make_result = [&]() {
  677. RunResult result = {.success = true};
  678. for (const auto& unit : units) {
  679. result.success &= unit->success();
  680. result.per_file_success.push_back(
  681. {unit->input_filename().str(), unit->success()});
  682. }
  683. return result;
  684. };
  685. // Lex.
  686. for (auto& unit : units) {
  687. unit->RunLex();
  688. }
  689. if (options.phase == CompileOptions::Phase::Lex) {
  690. return make_result();
  691. }
  692. // Parse and check phases examine `has_source` because they want to proceed if
  693. // lex failed, but not if source doesn't exist. Later steps are skipped if
  694. // anything failed, so don't need this.
  695. // Parse.
  696. for (auto& unit : units) {
  697. if (unit->has_source()) {
  698. unit->RunParse();
  699. }
  700. }
  701. if (options.phase == CompileOptions::Phase::Parse) {
  702. return make_result();
  703. }
  704. // Check.
  705. SharedValueStores builtin_value_stores;
  706. llvm::SmallVector<Check::Unit> check_units;
  707. for (auto& unit : units) {
  708. if (unit->has_source()) {
  709. check_units.push_back(unit->GetCheckUnit());
  710. }
  711. }
  712. CARBON_VLOG() << "*** Check::CheckParseTrees ***\n";
  713. Check::CheckParseTrees(llvm::MutableArrayRef(check_units),
  714. options.prelude_import, vlog_stream_);
  715. CARBON_VLOG() << "*** Check::CheckParseTrees done ***\n";
  716. for (auto& unit : units) {
  717. if (unit->has_source()) {
  718. unit->PostCheck();
  719. }
  720. }
  721. if (options.phase == CompileOptions::Phase::Check) {
  722. return make_result();
  723. }
  724. // Unlike previous steps, errors block further progress.
  725. if (std::any_of(units.begin(), units.end(),
  726. [&](const auto& unit) { return !unit->success(); })) {
  727. CARBON_VLOG() << "*** Stopping before lowering due to errors ***";
  728. return make_result();
  729. }
  730. // Lower.
  731. for (auto& unit : units) {
  732. unit->RunLower();
  733. }
  734. if (options.phase == CompileOptions::Phase::Lower) {
  735. return make_result();
  736. }
  737. CARBON_CHECK(options.phase == CompileOptions::Phase::CodeGen)
  738. << "CodeGen should be the last stage";
  739. // Codegen.
  740. for (auto& unit : units) {
  741. unit->RunCodeGen();
  742. }
  743. return make_result();
  744. }
  745. } // namespace Carbon