file_context.h 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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_FILE_CONTEXT_H_
  5. #define CARBON_TOOLCHAIN_LOWER_FILE_CONTEXT_H_
  6. #include "llvm/IR/Constants.h"
  7. #include "llvm/IR/DIBuilder.h"
  8. #include "llvm/IR/LLVMContext.h"
  9. #include "llvm/IR/Module.h"
  10. #include "toolchain/check/sem_ir_diagnostic_converter.h"
  11. #include "toolchain/sem_ir/file.h"
  12. #include "toolchain/sem_ir/inst_namer.h"
  13. namespace Carbon::Lower {
  14. // Context and shared functionality for lowering handlers.
  15. class FileContext {
  16. public:
  17. // Location information for use with DebugInfo. The line_number and
  18. // column_number are >= 0, with 0 as unknown, so that they can be passed
  19. // directly to DebugInfo.
  20. struct LocForDI {
  21. llvm::StringRef filename;
  22. int32_t line_number;
  23. int32_t column_number;
  24. };
  25. explicit FileContext(
  26. llvm::LLVMContext& llvm_context,
  27. std::optional<llvm::ArrayRef<Parse::GetTreeAndSubtreesFn>>
  28. all_trees_and_subtrees_for_debug_info,
  29. llvm::StringRef module_name, const SemIR::File& sem_ir,
  30. const SemIR::InstNamer* inst_namer, llvm::raw_ostream* vlog_stream);
  31. // Lowers the SemIR::File to LLVM IR. Should only be called once, and handles
  32. // the main execution loop.
  33. auto Run() -> std::unique_ptr<llvm::Module>;
  34. // Create the DICompileUnit metadata for this compilation.
  35. auto BuildDICompileUnit(llvm::StringRef module_name,
  36. llvm::Module& llvm_module,
  37. llvm::DIBuilder& di_builder) -> llvm::DICompileUnit*;
  38. // Gets a callable's function. Returns nullptr for a builtin.
  39. auto GetFunction(SemIR::FunctionId function_id) -> llvm::Function* {
  40. return functions_[function_id.index];
  41. }
  42. // Gets a or creates callable's function. Returns nullptr for a builtin.
  43. auto GetOrCreateFunction(SemIR::FunctionId function_id,
  44. SemIR::SpecificId specific_id) -> llvm::Function*;
  45. // Returns a lowered type for the given type_id.
  46. auto GetType(SemIR::TypeId type_id) -> llvm::Type* {
  47. CARBON_CHECK(type_id.has_value(), "Should not be called with `None`");
  48. CARBON_CHECK(types_[type_id.index], "Missing type {0}: {1}", type_id,
  49. sem_ir().types().GetAsInst(type_id));
  50. return types_[type_id.index];
  51. }
  52. // Returns location information for use with DebugInfo.
  53. auto GetLocForDI(SemIR::InstId inst_id) -> LocForDI;
  54. // Returns a lowered value to use for a value of type `type`.
  55. auto GetTypeAsValue() -> llvm::Constant* {
  56. return llvm::ConstantStruct::get(GetTypeType());
  57. }
  58. // Returns a lowered value to use for a value of int literal type.
  59. auto GetIntLiteralAsValue() -> llvm::Constant* {
  60. // TODO: Consider adding a named struct type for integer literals.
  61. return llvm::ConstantStruct::get(llvm::StructType::get(llvm_context()));
  62. }
  63. // Returns a global value for the given instruction.
  64. auto GetGlobal(SemIR::InstId inst_id) -> llvm::Value*;
  65. // Returns the empty LLVM struct type used to represent the type `type`.
  66. auto GetTypeType() -> llvm::StructType* {
  67. if (!type_type_) {
  68. // `type` is lowered to an empty LLVM StructType.
  69. type_type_ = llvm::StructType::create(*llvm_context_, {}, "type");
  70. }
  71. return type_type_;
  72. }
  73. auto llvm_context() -> llvm::LLVMContext& { return *llvm_context_; }
  74. auto llvm_module() -> llvm::Module& { return *llvm_module_; }
  75. auto sem_ir() -> const SemIR::File& { return *sem_ir_; }
  76. auto inst_namer() -> const SemIR::InstNamer* { return inst_namer_; }
  77. auto global_variables() -> const Map<SemIR::InstId, llvm::GlobalVariable*>& {
  78. return global_variables_;
  79. }
  80. private:
  81. // Builds the declaration for the given function, which should then be cached
  82. // by the caller.
  83. auto BuildFunctionDecl(SemIR::FunctionId function_id,
  84. SemIR::SpecificId specific_id =
  85. SemIR::SpecificId::None) -> llvm::Function*;
  86. // Builds the definition for the given function. If the function is only a
  87. // declaration with no definition, does nothing.
  88. auto BuildFunctionDefinition(SemIR::FunctionId function_id) -> void;
  89. // Build the DISubprogram metadata for the given function.
  90. auto BuildDISubprogram(const SemIR::Function& function,
  91. const llvm::Function* llvm_function)
  92. -> llvm::DISubprogram*;
  93. // Builds the type for the given instruction, which should then be cached by
  94. // the caller.
  95. auto BuildType(SemIR::InstId inst_id) -> llvm::Type*;
  96. // Builds the global for the given instruction, which should then be cached by
  97. // the caller.
  98. auto BuildGlobalVariableDecl(SemIR::VarStorage var_storage)
  99. -> llvm::GlobalVariable*;
  100. // State for building the LLVM IR.
  101. llvm::LLVMContext* llvm_context_;
  102. std::unique_ptr<llvm::Module> llvm_module_;
  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. all_trees_and_subtrees_for_debug_info_;
  110. // The input SemIR.
  111. const SemIR::File* const sem_ir_;
  112. // The instruction namer, if given.
  113. const SemIR::InstNamer* const inst_namer_;
  114. // The optional vlog stream.
  115. llvm::raw_ostream* vlog_stream_;
  116. // Maps callables to lowered functions. SemIR treats callables as the
  117. // canonical form of a function, so lowering needs to do the same.
  118. // Vector indexes correspond to `FunctionId` indexes. We resize this directly
  119. // to the correct size.
  120. llvm::SmallVector<llvm::Function*, 0> functions_;
  121. // Maps specific callables to lowered functions. Vector indexes correspond to
  122. // `SpecificId` indexes. We resize this directly to the correct size.
  123. llvm::SmallVector<llvm::Function*, 0> specific_functions_;
  124. // Provides lowered versions of types.
  125. // Vector indexes correspond to `TypeId` indexes for non-symbolic types. We
  126. // resize this directly to the (often large) correct size.
  127. llvm::SmallVector<llvm::Type*, 0> types_;
  128. // Lowered version of the builtin type `type`.
  129. llvm::StructType* type_type_ = nullptr;
  130. // Maps constants to their lowered values.
  131. // Vector indexes correspond to `InstId` indexes for constant instructions. We
  132. // resize this directly to the (often large) correct size.
  133. llvm::SmallVector<llvm::Constant*, 0> constants_;
  134. // Maps global variables to their lowered variant.
  135. Map<SemIR::InstId, llvm::GlobalVariable*> global_variables_;
  136. };
  137. } // namespace Carbon::Lower
  138. #endif // CARBON_TOOLCHAIN_LOWER_FILE_CONTEXT_H_