file_context.h 5.6 KB

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