file_context.h 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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_loc_diagnostic_emitter.h"
  11. #include "toolchain/sem_ir/file.h"
  12. #include "toolchain/sem_ir/ids.h"
  13. #include "toolchain/sem_ir/inst_namer.h"
  14. namespace Carbon::Lower {
  15. // Context and shared functionality for lowering handlers.
  16. class FileContext {
  17. public:
  18. // Location information for use with DebugInfo. The line_number and
  19. // column_number are >= 0, with 0 as unknown, so that they can be passed
  20. // directly to DebugInfo.
  21. struct LocForDI {
  22. llvm::StringRef filename;
  23. int32_t line_number;
  24. int32_t column_number;
  25. };
  26. explicit FileContext(
  27. llvm::LLVMContext& llvm_context,
  28. std::optional<llvm::ArrayRef<Parse::GetTreeAndSubtreesFn>>
  29. tree_and_subtrees_getters_for_debug_info,
  30. llvm::StringRef module_name, const SemIR::File& sem_ir,
  31. const SemIR::InstNamer* inst_namer, llvm::raw_ostream* vlog_stream);
  32. // Lowers the SemIR::File to LLVM IR. Should only be called once, and handles
  33. // the main execution loop.
  34. auto Run() -> std::unique_ptr<llvm::Module>;
  35. // Create the DICompileUnit metadata for this compilation.
  36. auto BuildDICompileUnit(llvm::StringRef module_name,
  37. llvm::Module& llvm_module,
  38. llvm::DIBuilder& di_builder) -> llvm::DICompileUnit*;
  39. // Gets a callable's function. Returns nullptr for a builtin.
  40. auto GetFunction(SemIR::FunctionId function_id) -> llvm::Function* {
  41. return functions_[function_id.index];
  42. }
  43. // Gets a or creates callable's function. Returns nullptr for a builtin.
  44. auto GetOrCreateFunction(SemIR::FunctionId function_id,
  45. SemIR::SpecificId specific_id) -> llvm::Function*;
  46. // Returns a lowered type for the given type_id.
  47. auto GetType(SemIR::TypeId type_id) -> llvm::Type* {
  48. CARBON_CHECK(type_id.has_value(), "Should not be called with `None`");
  49. CARBON_CHECK(types_[type_id.index], "Missing type {0}: {1}", type_id,
  50. sem_ir().types().GetAsInst(type_id));
  51. return types_[type_id.index];
  52. }
  53. // Returns location information for use with DebugInfo.
  54. auto GetLocForDI(SemIR::InstId inst_id) -> LocForDI;
  55. // Returns a lowered value to use for a value of type `type`.
  56. auto GetTypeAsValue() -> llvm::Constant* {
  57. return llvm::ConstantStruct::get(GetTypeType());
  58. }
  59. // Returns a lowered value to use for a value of int literal type.
  60. auto GetIntLiteralAsValue() -> llvm::Constant* {
  61. // TODO: Consider adding a named struct type for integer literals.
  62. return llvm::ConstantStruct::get(llvm::StructType::get(llvm_context()));
  63. }
  64. // Returns a global value for the given instruction.
  65. auto GetGlobal(SemIR::InstId inst_id, SemIR::SpecificId specific_id)
  66. -> llvm::Value*;
  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 sem_ir() -> const SemIR::File& { return *sem_ir_; }
  78. auto inst_namer() -> const SemIR::InstNamer* { return inst_namer_; }
  79. auto global_variables() -> const Map<SemIR::InstId, llvm::GlobalVariable*>& {
  80. return global_variables_;
  81. }
  82. private:
  83. struct FunctionTypeInfo {
  84. llvm::FunctionType* type;
  85. llvm::SmallVector<SemIR::InstId> param_inst_ids;
  86. llvm::Type* return_type = nullptr;
  87. SemIR::InstId return_param_id = SemIR::InstId::None;
  88. };
  89. // Retrieve various features of the function's type useful for constructing
  90. // the `llvm::Type` for the `llvm::Function`. If any part of the type can't be
  91. // manifest (eg: incomplete return or parameter types), then the result is as
  92. // if the type was `void()`.
  93. auto BuildFunctionTypeInfo(const SemIR::Function& function,
  94. SemIR::SpecificId specific_id) -> FunctionTypeInfo;
  95. // Builds the declaration for the given function, which should then be cached
  96. // by the caller.
  97. auto BuildFunctionDecl(SemIR::FunctionId function_id,
  98. SemIR::SpecificId specific_id =
  99. SemIR::SpecificId::None) -> llvm::Function*;
  100. // Builds the definition for the given function. If the function is only a
  101. // declaration with no definition, does nothing. If this is a generic it'll
  102. // only be lowered if the specific_id is specified. During this lowering of
  103. // a generic, more generic functions may be added for lowering.
  104. auto BuildFunctionDefinition(
  105. SemIR::FunctionId function_id,
  106. SemIR::SpecificId specific_id = SemIR::SpecificId::None) -> void;
  107. // Builds a functions body. Common functionality for all functions.
  108. auto BuildFunctionBody(
  109. SemIR::FunctionId function_id, const SemIR::Function& function,
  110. llvm::Function* llvm_function,
  111. SemIR::SpecificId specific_id = SemIR::SpecificId::None) -> void;
  112. // Build the DISubprogram metadata for the given function.
  113. auto BuildDISubprogram(const SemIR::Function& function,
  114. const llvm::Function* llvm_function)
  115. -> llvm::DISubprogram*;
  116. // Builds the type for the given instruction, which should then be cached by
  117. // the caller.
  118. auto BuildType(SemIR::InstId inst_id) -> llvm::Type*;
  119. // Builds the global for the given instruction, which should then be cached by
  120. // the caller.
  121. auto BuildGlobalVariableDecl(SemIR::VarStorage var_storage)
  122. -> llvm::GlobalVariable*;
  123. // State for building the LLVM IR.
  124. llvm::LLVMContext* llvm_context_;
  125. std::unique_ptr<llvm::Module> llvm_module_;
  126. // State for building the LLVM IR debug info metadata.
  127. llvm::DIBuilder di_builder_;
  128. // The DICompileUnit, if any - null implies debug info is not being emitted.
  129. llvm::DICompileUnit* di_compile_unit_;
  130. // The trees are only provided when debug info should be emitted.
  131. std::optional<llvm::ArrayRef<Parse::GetTreeAndSubtreesFn>>
  132. tree_and_subtrees_getters_for_debug_info_;
  133. // The input SemIR.
  134. const SemIR::File* const sem_ir_;
  135. // The instruction namer, if given.
  136. const SemIR::InstNamer* const inst_namer_;
  137. // The optional vlog stream.
  138. llvm::raw_ostream* vlog_stream_;
  139. // Maps callables to lowered functions. SemIR treats callables as the
  140. // canonical form of a function, so lowering needs to do the same.
  141. // Vector indexes correspond to `FunctionId` indexes. We resize this directly
  142. // to the correct size.
  143. llvm::SmallVector<llvm::Function*, 0> functions_;
  144. // Maps specific callables to lowered functions. Vector indexes correspond to
  145. // `SpecificId` indexes. We resize this directly to the correct size.
  146. llvm::SmallVector<llvm::Function*, 0> specific_functions_;
  147. // Maps which specific functions are generics that need to have their
  148. // definitions lowered after the lowering of other definitions.
  149. // This list may grow while lowering generic definitions from this list.
  150. // The list uses the `SpecificId` to index into specific_functions_.
  151. llvm::SmallVector<std::pair<SemIR::FunctionId, SemIR::SpecificId>, 10>
  152. specific_function_definitions_;
  153. // Provides lowered versions of types.
  154. // Vector indexes correspond to `TypeId` indexes for non-symbolic types. We
  155. // resize this directly to the (often large) correct size.
  156. llvm::SmallVector<llvm::Type*, 0> types_;
  157. // Lowered version of the builtin type `type`.
  158. llvm::StructType* type_type_ = nullptr;
  159. // Maps constants to their lowered values.
  160. // Vector indexes correspond to `InstId` indexes for constant instructions. We
  161. // resize this directly to the (often large) correct size.
  162. llvm::SmallVector<llvm::Constant*, 0> constants_;
  163. // Maps global variables to their lowered variant.
  164. Map<SemIR::InstId, llvm::GlobalVariable*> global_variables_;
  165. };
  166. } // namespace Carbon::Lower
  167. #endif // CARBON_TOOLCHAIN_LOWER_FILE_CONTEXT_H_