context.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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. #ifndef CARBON_TOOLCHAIN_LOWER_CONTEXT_H_
  5. #define CARBON_TOOLCHAIN_LOWER_CONTEXT_H_
  6. #include <memory>
  7. #include <optional>
  8. #include <utility>
  9. #include "llvm/IR/Constants.h"
  10. #include "llvm/IR/DIBuilder.h"
  11. #include "llvm/IR/LLVMContext.h"
  12. #include "llvm/IR/Module.h"
  13. #include "toolchain/parse/tree_and_subtrees.h"
  14. #include "toolchain/sem_ir/absolute_node_id.h"
  15. #include "toolchain/sem_ir/ids.h"
  16. #include "toolchain/sem_ir/inst_namer.h"
  17. namespace Carbon::Lower {
  18. class FileContext;
  19. // Context for lowering to an LLVM module.
  20. class Context {
  21. public:
  22. // Location information for use with DebugInfo. The line_number and
  23. // column_number are >= 0, with 0 as unknown, so that they can be passed
  24. // directly to DebugInfo.
  25. struct LocForDI {
  26. llvm::StringRef filename;
  27. int32_t line_number;
  28. int32_t column_number;
  29. };
  30. // A specific function whose definition needs to be lowered.
  31. struct PendingSpecificFunctionDefinition {
  32. FileContext* context;
  33. SemIR::FunctionId function_id;
  34. SemIR::SpecificId specific_id;
  35. };
  36. explicit Context(llvm::LLVMContext& llvm_context,
  37. llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> fs,
  38. std::optional<llvm::ArrayRef<Parse::GetTreeAndSubtreesFn>>
  39. tree_and_subtrees_getters_for_debug_info,
  40. llvm::StringRef module_name, llvm::raw_ostream* vlog_stream);
  41. // Gets or creates the `FileContext` for a given SemIR file. If an
  42. // `inst_namer` is specified the first time this is called for a file, it will
  43. // be used for that file. Otherwise, no instruction namer will be used.
  44. // TODO: Consider building an InstNamer if we're not given one.
  45. auto GetFileContext(const SemIR::File* file,
  46. const SemIR::InstNamer* inst_namer = nullptr)
  47. -> FileContext&;
  48. // Registers a specific function definition to be lowered later.
  49. auto AddPendingSpecificFunctionDefinition(
  50. PendingSpecificFunctionDefinition pending) -> void {
  51. specific_function_definitions_.push_back(pending);
  52. }
  53. // Finishes lowering and takes ownership of the LLVM module. The context
  54. // cannot be used further after calling this.
  55. auto Finalize() && -> std::unique_ptr<llvm::Module>;
  56. // Returns location information for use with DebugInfo.
  57. auto GetLocForDI(SemIR::AbsoluteNodeId abs_node_id) -> LocForDI;
  58. // Returns a lowered value to use for a value of type `type`.
  59. auto GetTypeAsValue() -> llvm::Constant* {
  60. return llvm::ConstantStruct::get(GetTypeType());
  61. }
  62. // Returns a lowered value to use for a value of int literal type.
  63. auto GetIntLiteralAsValue() -> llvm::Constant* {
  64. // TODO: Consider adding a named struct type for integer literals.
  65. return llvm::ConstantStruct::get(llvm::StructType::get(llvm_context()));
  66. }
  67. // Returns the empty LLVM struct type used to represent the type `type`.
  68. auto GetTypeType() -> llvm::StructType* {
  69. if (!type_type_) {
  70. // `type` is lowered to an empty LLVM StructType.
  71. type_type_ = llvm::StructType::create(*llvm_context_, {}, "type");
  72. }
  73. return type_type_;
  74. }
  75. auto llvm_context() -> llvm::LLVMContext& { return *llvm_context_; }
  76. auto llvm_module() -> llvm::Module& { return *llvm_module_; }
  77. auto file_system() -> llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem>& {
  78. return file_system_;
  79. }
  80. auto di_builder() -> llvm::DIBuilder& { return di_builder_; }
  81. auto di_compile_unit() -> llvm::DICompileUnit* { return di_compile_unit_; }
  82. auto printf_int_format_string() -> llvm::Value* {
  83. return printf_int_format_string_;
  84. }
  85. auto SetPrintfIntFormatString(llvm::Value* printf_int_format_string) {
  86. CARBON_CHECK(!printf_int_format_string_,
  87. "PrintInt formatting string already generated");
  88. printf_int_format_string_ = printf_int_format_string;
  89. }
  90. private:
  91. // Create the DICompileUnit metadata for this compilation.
  92. auto BuildDICompileUnit(llvm::StringRef module_name,
  93. llvm::Module& llvm_module,
  94. llvm::DIBuilder& di_builder) -> llvm::DICompileUnit*;
  95. // Lower any definitions that have been registered for later lowering.
  96. // Currently, this lowers specifics for generic functions.
  97. auto LowerPendingDefinitions() -> void;
  98. // State for building the LLVM IR.
  99. llvm::LLVMContext* llvm_context_;
  100. std::unique_ptr<llvm::Module> llvm_module_;
  101. // The filesystem for source code.
  102. llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> file_system_;
  103. // State for building the LLVM IR debug info metadata.
  104. llvm::DIBuilder di_builder_;
  105. // The DICompileUnit, if any - null implies debug info is not being emitted.
  106. llvm::DICompileUnit* di_compile_unit_;
  107. // The trees are only provided when debug info should be emitted.
  108. std::optional<llvm::ArrayRef<Parse::GetTreeAndSubtreesFn>>
  109. tree_and_subtrees_getters_for_debug_info_;
  110. // The optional vlog stream.
  111. llvm::raw_ostream* vlog_stream_;
  112. // The `FileContext`s for each IR that is involved in this lowering action.
  113. Map<SemIR::CheckIRId, std::unique_ptr<FileContext>> file_contexts_;
  114. // Lowered version of the builtin type `type`.
  115. llvm::StructType* type_type_ = nullptr;
  116. // Global format string for `printf.int.format` used by the PrintInt builtin.
  117. llvm::Value* printf_int_format_string_ = nullptr;
  118. // Tracks which specific functions need to have their definitions lowered.
  119. // This list may grow while lowering generic definitions from this list.
  120. llvm::SmallVector<PendingSpecificFunctionDefinition>
  121. specific_function_definitions_;
  122. };
  123. } // namespace Carbon::Lower
  124. #endif // CARBON_TOOLCHAIN_LOWER_CONTEXT_H_