context.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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/lower/context.h"
  5. #include "common/check.h"
  6. #include "common/growing_range.h"
  7. #include "common/raw_string_ostream.h"
  8. #include "common/vlog.h"
  9. #include "llvm/Transforms/Utils/ModuleUtils.h"
  10. #include "toolchain/lower/file_context.h"
  11. #include "toolchain/sem_ir/inst_namer.h"
  12. namespace Carbon::Lower {
  13. Context::Context(
  14. llvm::LLVMContext* llvm_context,
  15. llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> fs, bool want_debug_info,
  16. const Parse::GetTreeAndSubtreesStore* tree_and_subtrees_getters,
  17. llvm::StringRef module_name, int total_ir_count,
  18. Lower::OptimizationLevel opt_level, llvm::raw_ostream* vlog_stream)
  19. : llvm_context_(llvm_context),
  20. llvm_module_(std::make_unique<llvm::Module>(module_name, *llvm_context)),
  21. file_system_(std::move(fs)),
  22. opt_level_(opt_level),
  23. di_builder_(*llvm_module_),
  24. di_compile_unit_(
  25. want_debug_info
  26. ? BuildDICompileUnit(module_name, *llvm_module_, di_builder_)
  27. : nullptr),
  28. tree_and_subtrees_getters_(tree_and_subtrees_getters),
  29. vlog_stream_(vlog_stream),
  30. total_ir_count_(total_ir_count),
  31. file_contexts_(
  32. FileContextStore::MakeForOverwriteWithExplicitSize(total_ir_count_)) {
  33. }
  34. auto Context::GetFileContext(const SemIR::File* file,
  35. const SemIR::InstNamer* inst_namer)
  36. -> FileContext& {
  37. auto& file_context = file_contexts_.Get(file->check_ir_id());
  38. if (!file_context) {
  39. file_context =
  40. std::make_unique<FileContext>(*this, *file, inst_namer, vlog_stream_);
  41. file_context->PrepareToLower();
  42. }
  43. return *file_context;
  44. }
  45. auto Context::LowerPendingDefinitions() -> void {
  46. // Lower function definitions for generics.
  47. for (auto [file_context, function_id, specific_id] :
  48. GrowingRange(specific_function_definitions_)) {
  49. file_context->BuildFunctionDefinition(function_id, specific_id);
  50. }
  51. }
  52. auto Context::Finalize() && -> std::unique_ptr<llvm::Module> {
  53. LowerPendingDefinitions();
  54. for (auto& file_context : file_contexts_.values()) {
  55. if (file_context) {
  56. file_context->Finalize();
  57. }
  58. }
  59. return std::move(llvm_module_);
  60. }
  61. auto Context::BuildDICompileUnit(llvm::StringRef module_name,
  62. llvm::Module& llvm_module,
  63. llvm::DIBuilder& di_builder)
  64. -> llvm::DICompileUnit* {
  65. llvm_module.addModuleFlag(llvm::Module::Max, "Dwarf Version", 5);
  66. llvm_module.addModuleFlag(llvm::Module::Warning, "Debug Info Version",
  67. llvm::DEBUG_METADATA_VERSION);
  68. // TODO: Include directory path in the compile_unit_file.
  69. llvm::DIFile* compile_unit_file = di_builder.createFile(module_name, "");
  70. // TODO: Introduce a new language code for Carbon. C works well for now since
  71. // it's something debuggers will already know/have support for at least.
  72. // Probably have to bump to C++ at some point for virtual functions,
  73. // templates, etc.
  74. return di_builder.createCompileUnit(llvm::dwarf::DW_LANG_C, compile_unit_file,
  75. "carbon",
  76. /*isOptimized=*/false, /*Flags=*/"",
  77. /*RV=*/0);
  78. }
  79. auto Context::GetLocForDI(SemIR::AbsoluteNodeId abs_node_id) -> LocForDI {
  80. const auto& tree_and_subtrees =
  81. tree_and_subtrees_getters().Get(abs_node_id.check_ir_id())();
  82. const auto& tokens = tree_and_subtrees.tree().tokens();
  83. if (abs_node_id.node_id().has_value()) {
  84. auto token =
  85. tree_and_subtrees.GetSubtreeTokenRange(abs_node_id.node_id()).begin;
  86. return {.filename = tokens.source().filename(),
  87. .line_number = tokens.GetLineNumber(token),
  88. .column_number = tokens.GetColumnNumber(token)};
  89. } else {
  90. return {.filename = tokens.source().filename(),
  91. .line_number = 0,
  92. .column_number = 0};
  93. }
  94. }
  95. } // namespace Carbon::Lower