file_context.h 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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}: {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 global value for the given instruction.
  59. auto GetGlobal(SemIR::InstId inst_id) -> llvm::Value*;
  60. // Returns the empty LLVM struct type used to represent the type `type`.
  61. auto GetTypeType() -> llvm::StructType* {
  62. if (!type_type_) {
  63. // `type` is lowered to an empty LLVM StructType.
  64. type_type_ = llvm::StructType::create(*llvm_context_, {}, "type");
  65. }
  66. return type_type_;
  67. }
  68. auto llvm_context() -> llvm::LLVMContext& { return *llvm_context_; }
  69. auto llvm_module() -> llvm::Module& { return *llvm_module_; }
  70. auto sem_ir() -> const SemIR::File& { return *sem_ir_; }
  71. auto inst_namer() -> const SemIR::InstNamer* { return inst_namer_; }
  72. auto global_variables() -> const Map<SemIR::InstId, llvm::GlobalVariable*>& {
  73. return global_variables_;
  74. }
  75. private:
  76. // Builds the declaration for the given function, which should then be cached
  77. // by the caller.
  78. auto BuildFunctionDecl(SemIR::FunctionId function_id,
  79. SemIR::SpecificId specific_id =
  80. SemIR::SpecificId::Invalid) -> llvm::Function*;
  81. // Builds the definition for the given function. If the function is only a
  82. // declaration with no definition, does nothing.
  83. auto BuildFunctionDefinition(SemIR::FunctionId function_id) -> void;
  84. // Build the DISubprogram metadata for the given function.
  85. auto BuildDISubprogram(const SemIR::Function& function,
  86. const llvm::Function* llvm_function)
  87. -> llvm::DISubprogram*;
  88. // Builds the type for the given instruction, which should then be cached by
  89. // the caller.
  90. auto BuildType(SemIR::InstId inst_id) -> llvm::Type*;
  91. // Builds the global for the given instruction, which should then be cached by
  92. // the caller.
  93. auto BuildGlobalVariableDecl(SemIR::VarStorage var_storage)
  94. -> llvm::GlobalVariable*;
  95. // State for building the LLVM IR.
  96. llvm::LLVMContext* llvm_context_;
  97. std::unique_ptr<llvm::Module> llvm_module_;
  98. // State for building the LLVM IR debug info metadata.
  99. llvm::DIBuilder di_builder_;
  100. // The DICompileUnit, if any - null implies debug info is not being emitted.
  101. llvm::DICompileUnit* di_compile_unit_;
  102. // The source location converter.
  103. const Check::SemIRDiagnosticConverter& converter_;
  104. // The input SemIR.
  105. const SemIR::File* const sem_ir_;
  106. // The instruction namer, if given.
  107. const SemIR::InstNamer* const inst_namer_;
  108. // The optional vlog stream.
  109. llvm::raw_ostream* vlog_stream_;
  110. // Maps callables to lowered functions. SemIR treats callables as the
  111. // canonical form of a function, so lowering needs to do the same.
  112. // Vector indexes correspond to `FunctionId` indexes. We resize this directly
  113. // to the correct size.
  114. llvm::SmallVector<llvm::Function*, 0> functions_;
  115. // Maps specific callables to lowered functions. Vector indexes correspond to
  116. // `SpecificId` indexes. We resize this directly to the correct size.
  117. llvm::SmallVector<llvm::Function*, 0> specific_functions_;
  118. // Provides lowered versions of types.
  119. // Vector indexes correspond to `TypeId` indexes for non-symbolic types. We
  120. // resize this directly to the (often large) correct size.
  121. llvm::SmallVector<llvm::Type*, 0> types_;
  122. // Lowered version of the builtin type `type`.
  123. llvm::StructType* type_type_ = nullptr;
  124. // Maps constants to their lowered values.
  125. // Vector indexes correspond to `InstId` indexes for constant instructions. We
  126. // resize this directly to the (often large) correct size.
  127. llvm::SmallVector<llvm::Constant*, 0> constants_;
  128. // Maps global variables to their lowered variant.
  129. Map<SemIR::InstId, llvm::GlobalVariable*> global_variables_;
  130. };
  131. } // namespace Carbon::Lower
  132. #endif // CARBON_TOOLCHAIN_LOWER_FILE_CONTEXT_H_