file_context.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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/LLVMContext.h"
  8. #include "llvm/IR/Module.h"
  9. #include "toolchain/sem_ir/file.h"
  10. #include "toolchain/sem_ir/inst_namer.h"
  11. namespace Carbon::Lower {
  12. // Context and shared functionality for lowering handlers.
  13. class FileContext {
  14. public:
  15. explicit FileContext(llvm::LLVMContext& llvm_context,
  16. llvm::StringRef module_name, const SemIR::File& sem_ir,
  17. const SemIR::InstNamer* inst_namer,
  18. llvm::raw_ostream* vlog_stream);
  19. // Lowers the SemIR::File to LLVM IR. Should only be called once, and handles
  20. // the main execution loop.
  21. auto Run() -> std::unique_ptr<llvm::Module>;
  22. // Gets a callable's function. Returns nullptr for a builtin.
  23. auto GetFunction(SemIR::FunctionId function_id) -> llvm::Function* {
  24. return functions_[function_id.index];
  25. }
  26. // Returns a lowered type for the given type_id.
  27. auto GetType(SemIR::TypeId type_id) -> llvm::Type* {
  28. // InvalidType should not be passed in.
  29. CARBON_CHECK(type_id.index >= 0) << type_id;
  30. CARBON_CHECK(types_[type_id.index]) << "Missing type " << type_id;
  31. return types_[type_id.index];
  32. }
  33. // Returns a lowered value to use for a value of type `type`.
  34. auto GetTypeAsValue() -> llvm::Constant* {
  35. return llvm::ConstantStruct::get(GetTypeType());
  36. }
  37. // Returns a global value for the given instruction.
  38. auto GetGlobal(SemIR::InstId inst_id) -> llvm::Value*;
  39. // Returns the empty LLVM struct type used to represent the type `type`.
  40. auto GetTypeType() -> llvm::StructType* {
  41. if (!type_type_) {
  42. // `type` is lowered to an empty LLVM StructType.
  43. type_type_ = llvm::StructType::create(*llvm_context_, {}, "type");
  44. }
  45. return type_type_;
  46. }
  47. auto llvm_context() -> llvm::LLVMContext& { return *llvm_context_; }
  48. auto llvm_module() -> llvm::Module& { return *llvm_module_; }
  49. auto sem_ir() -> const SemIR::File& { return *sem_ir_; }
  50. auto inst_namer() -> const SemIR::InstNamer* { return inst_namer_; }
  51. auto global_variables() -> const Map<SemIR::InstId, llvm::GlobalVariable*>& {
  52. return global_variables_;
  53. }
  54. private:
  55. // Builds the declaration for the given function, which should then be cached
  56. // by the caller.
  57. auto BuildFunctionDecl(SemIR::FunctionId function_id) -> llvm::Function*;
  58. // Builds the definition for the given function. If the function is only a
  59. // declaration with no definition, does nothing.
  60. auto BuildFunctionDefinition(SemIR::FunctionId function_id) -> void;
  61. // Builds the type for the given instruction, which should then be cached by
  62. // the caller.
  63. auto BuildType(SemIR::InstId inst_id) -> llvm::Type*;
  64. // Builds the global for the given instruction, which should then be cached by
  65. // the caller.
  66. auto BuildGlobalVariableDecl(SemIR::VarStorage var_storage)
  67. -> llvm::GlobalVariable*;
  68. // State for building the LLVM IR.
  69. llvm::LLVMContext* llvm_context_;
  70. std::unique_ptr<llvm::Module> llvm_module_;
  71. // The input SemIR.
  72. const SemIR::File* const sem_ir_;
  73. // The instruction namer, if given.
  74. const SemIR::InstNamer* const inst_namer_;
  75. // The optional vlog stream.
  76. llvm::raw_ostream* vlog_stream_;
  77. // Maps callables to lowered functions. SemIR treats callables as the
  78. // canonical form of a function, so lowering needs to do the same.
  79. // We resize this directly to the (often large) correct size.
  80. llvm::SmallVector<llvm::Function*, 0> functions_;
  81. // Provides lowered versions of types.
  82. // We resize this directly to the (often large) correct size.
  83. llvm::SmallVector<llvm::Type*, 0> types_;
  84. // Lowered version of the builtin type `type`.
  85. llvm::StructType* type_type_ = nullptr;
  86. // Maps constants to their lowered values.
  87. // We resize this directly to the (often large) correct size.
  88. llvm::SmallVector<llvm::Constant*, 0> constants_;
  89. // Maps global variables to their lowered variant.
  90. Map<SemIR::InstId, llvm::GlobalVariable*> global_variables_;
  91. };
  92. } // namespace Carbon::Lower
  93. #endif // CARBON_TOOLCHAIN_LOWER_FILE_CONTEXT_H_