file_context.h 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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(llvm::LLVMContext& llvm_context, bool include_debug_info,
  26. const Check::SemIRDiagnosticConverter& converter,
  27. llvm::StringRef module_name, const SemIR::File& sem_ir,
  28. const SemIR::InstNamer* inst_namer,
  29. llvm::raw_ostream* vlog_stream);
  30. // Lowers the SemIR::File to LLVM IR. Should only be called once, and handles
  31. // the main execution loop.
  32. auto Run() -> std::unique_ptr<llvm::Module>;
  33. // Create the DICompileUnit metadata for this compilation.
  34. auto BuildDICompileUnit(llvm::StringRef module_name,
  35. llvm::Module& llvm_module,
  36. llvm::DIBuilder& di_builder) -> llvm::DICompileUnit*;
  37. // Gets a callable's function. Returns nullptr for a builtin.
  38. auto GetFunction(SemIR::FunctionId function_id) -> llvm::Function* {
  39. return functions_[function_id.index];
  40. }
  41. // Gets a or creates callable's function. Returns nullptr for a builtin.
  42. auto GetOrCreateFunction(SemIR::FunctionId function_id,
  43. SemIR::SpecificId specific_id) -> llvm::Function*;
  44. // Returns a lowered type for the given type_id.
  45. auto GetType(SemIR::TypeId type_id) -> llvm::Type* {
  46. // InvalidType should not be passed in.
  47. CARBON_CHECK(type_id.index >= 0, "{0}", type_id);
  48. CARBON_CHECK(types_[type_id.index], "Missing type {0}", type_id);
  49. return types_[type_id.index];
  50. }
  51. // Returns location information for use with DebugInfo.
  52. auto GetLocForDI(SemIR::InstId inst_id) -> LocForDI;
  53. // Returns a lowered value to use for a value of type `type`.
  54. auto GetTypeAsValue() -> llvm::Constant* {
  55. return llvm::ConstantStruct::get(GetTypeType());
  56. }
  57. // Returns a global value for the given instruction.
  58. auto GetGlobal(SemIR::InstId inst_id) -> llvm::Value*;
  59. // Returns the empty LLVM struct type used to represent the type `type`.
  60. auto GetTypeType() -> llvm::StructType* {
  61. if (!type_type_) {
  62. // `type` is lowered to an empty LLVM StructType.
  63. type_type_ = llvm::StructType::create(*llvm_context_, {}, "type");
  64. }
  65. return type_type_;
  66. }
  67. auto llvm_context() -> llvm::LLVMContext& { return *llvm_context_; }
  68. auto llvm_module() -> llvm::Module& { return *llvm_module_; }
  69. auto sem_ir() -> const SemIR::File& { return *sem_ir_; }
  70. auto inst_namer() -> const SemIR::InstNamer* { return inst_namer_; }
  71. auto global_variables() -> const Map<SemIR::InstId, llvm::GlobalVariable*>& {
  72. return global_variables_;
  73. }
  74. private:
  75. // Builds the declaration for the given function, which should then be cached
  76. // by the caller.
  77. auto BuildFunctionDecl(SemIR::FunctionId function_id,
  78. SemIR::SpecificId specific_id =
  79. SemIR::SpecificId::Invalid) -> llvm::Function*;
  80. // Builds the definition for the given function. If the function is only a
  81. // declaration with no definition, does nothing.
  82. auto BuildFunctionDefinition(SemIR::FunctionId function_id) -> void;
  83. // Build the DISubprogram metadata for the given function.
  84. auto BuildDISubprogram(const SemIR::Function& function,
  85. const llvm::Function* llvm_function)
  86. -> llvm::DISubprogram*;
  87. // Builds the type for the given instruction, which should then be cached by
  88. // the caller.
  89. auto BuildType(SemIR::InstId inst_id) -> llvm::Type*;
  90. // Builds the global for the given instruction, which should then be cached by
  91. // the caller.
  92. auto BuildGlobalVariableDecl(SemIR::VarStorage var_storage)
  93. -> llvm::GlobalVariable*;
  94. // State for building the LLVM IR.
  95. llvm::LLVMContext* llvm_context_;
  96. std::unique_ptr<llvm::Module> llvm_module_;
  97. // State for building the LLVM IR debug info metadata.
  98. llvm::DIBuilder di_builder_;
  99. // The DICompileUnit, if any - null implies debug info is not being emitted.
  100. llvm::DICompileUnit* di_compile_unit_;
  101. // The source location converter.
  102. const Check::SemIRDiagnosticConverter& converter_;
  103. // The input SemIR.
  104. const SemIR::File* const sem_ir_;
  105. // The instruction namer, if given.
  106. const SemIR::InstNamer* const inst_namer_;
  107. // The optional vlog stream.
  108. llvm::raw_ostream* vlog_stream_;
  109. // Maps callables to lowered functions. SemIR treats callables as the
  110. // canonical form of a function, so lowering needs to do the same.
  111. // Vector indexes correspond to `FunctionId` indexes. We resize this directly
  112. // to the correct size.
  113. llvm::SmallVector<llvm::Function*, 0> functions_;
  114. // Maps specific callables to lowered functions. Vector indexes correspond to
  115. // `SpecificId` indexes. We resize this directly to the correct size.
  116. llvm::SmallVector<llvm::Function*, 0> specific_functions_;
  117. // Provides lowered versions of types.
  118. // Vector indexes correspond to `TypeId` indexes for non-symbolic types. We
  119. // resize this directly to the (often large) correct size.
  120. llvm::SmallVector<llvm::Type*, 0> types_;
  121. // Lowered version of the builtin type `type`.
  122. llvm::StructType* type_type_ = nullptr;
  123. // Maps constants to their lowered values.
  124. // Vector indexes correspond to `InstId` indexes for constant instructions. We
  125. // resize this directly to the (often large) correct size.
  126. llvm::SmallVector<llvm::Constant*, 0> constants_;
  127. // Maps global variables to their lowered variant.
  128. Map<SemIR::InstId, llvm::GlobalVariable*> global_variables_;
  129. };
  130. } // namespace Carbon::Lower
  131. #endif // CARBON_TOOLCHAIN_LOWER_FILE_CONTEXT_H_