file_context.h 5.3 KB

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