context.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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. // `llvm_context` and `tree_and_subtrees_getters` must be non-null.
  37. // `vlog_stream` is optional.
  38. explicit Context(
  39. llvm::LLVMContext* llvm_context,
  40. llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> fs, bool want_debug_info,
  41. const Parse::GetTreeAndSubtreesStore* tree_and_subtrees_getters,
  42. llvm::StringRef module_name, llvm::raw_ostream* vlog_stream);
  43. // Gets or creates the `FileContext` for a given SemIR file. If an
  44. // `inst_namer` is specified the first time this is called for a file, it will
  45. // be used for that file. Otherwise, no instruction namer will be used.
  46. // TODO: Consider building an InstNamer if we're not given one.
  47. auto GetFileContext(const SemIR::File* file,
  48. const SemIR::InstNamer* inst_namer = nullptr)
  49. -> FileContext&;
  50. // Registers a specific function definition to be lowered later.
  51. auto AddPendingSpecificFunctionDefinition(
  52. PendingSpecificFunctionDefinition pending) -> void {
  53. specific_function_definitions_.push_back(pending);
  54. }
  55. // Finishes lowering and takes ownership of the LLVM module. The context
  56. // cannot be used further after calling this.
  57. auto Finalize() && -> std::unique_ptr<llvm::Module>;
  58. // Returns location information for use with DebugInfo.
  59. auto GetLocForDI(SemIR::AbsoluteNodeId abs_node_id) -> LocForDI;
  60. // Returns a lowered value to use for a value of type `type`.
  61. auto GetTypeAsValue() -> llvm::Constant* {
  62. return llvm::ConstantStruct::get(GetTypeType());
  63. }
  64. // Returns a lowered value to use for a value of int literal type.
  65. auto GetIntLiteralAsValue() -> llvm::Constant* {
  66. // TODO: Consider adding a named struct type for integer literals.
  67. return llvm::ConstantStruct::get(llvm::StructType::get(llvm_context()));
  68. }
  69. // Returns the empty LLVM struct type used to represent the type `type`.
  70. auto GetTypeType() -> llvm::StructType* {
  71. if (!type_type_) {
  72. // `type` is lowered to an empty LLVM StructType.
  73. type_type_ = llvm::StructType::create(*llvm_context_, {}, "type");
  74. }
  75. return type_type_;
  76. }
  77. auto llvm_context() -> llvm::LLVMContext& { return *llvm_context_; }
  78. auto llvm_module() -> llvm::Module& { return *llvm_module_; }
  79. auto file_system() -> llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem>& {
  80. return file_system_;
  81. }
  82. auto di_builder() -> llvm::DIBuilder& { return di_builder_; }
  83. auto di_compile_unit() -> llvm::DICompileUnit* { return di_compile_unit_; }
  84. auto tree_and_subtrees_getters() -> const Parse::GetTreeAndSubtreesStore& {
  85. return *tree_and_subtrees_getters_;
  86. }
  87. auto printf_int_format_string() -> llvm::Value* {
  88. return printf_int_format_string_;
  89. }
  90. auto SetPrintfIntFormatString(llvm::Value* printf_int_format_string) {
  91. CARBON_CHECK(!printf_int_format_string_,
  92. "PrintInt formatting string already generated");
  93. printf_int_format_string_ = printf_int_format_string;
  94. }
  95. private:
  96. // Create the DICompileUnit metadata for this compilation.
  97. auto BuildDICompileUnit(llvm::StringRef module_name,
  98. llvm::Module& llvm_module,
  99. llvm::DIBuilder& di_builder) -> llvm::DICompileUnit*;
  100. // Lower any definitions that have been registered for later lowering.
  101. // Currently, this lowers specifics for generic functions.
  102. auto LowerPendingDefinitions() -> void;
  103. // State for building the LLVM IR.
  104. llvm::LLVMContext* llvm_context_;
  105. std::unique_ptr<llvm::Module> llvm_module_;
  106. // The filesystem for source code.
  107. llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> file_system_;
  108. // State for building the LLVM IR debug info metadata.
  109. llvm::DIBuilder di_builder_;
  110. // The DICompileUnit, if any - null implies debug info is not being emitted.
  111. llvm::DICompileUnit* di_compile_unit_;
  112. // Parse trees. Used for debug information and crash diagnostics.
  113. const Parse::GetTreeAndSubtreesStore* tree_and_subtrees_getters_;
  114. // The optional vlog stream.
  115. llvm::raw_ostream* vlog_stream_;
  116. // The `FileContext`s for each IR that is involved in this lowering action.
  117. Map<SemIR::CheckIRId, std::unique_ptr<FileContext>> file_contexts_;
  118. // Lowered version of the builtin type `type`.
  119. llvm::StructType* type_type_ = nullptr;
  120. // Global format string for `printf.int.format` used by the PrintInt builtin.
  121. llvm::Value* printf_int_format_string_ = nullptr;
  122. // Tracks which specific functions need to have their definitions lowered.
  123. // This list may grow while lowering generic definitions from this list.
  124. llvm::SmallVector<PendingSpecificFunctionDefinition>
  125. specific_function_definitions_;
  126. };
  127. } // namespace Carbon::Lower
  128. #endif // CARBON_TOOLCHAIN_LOWER_CONTEXT_H_