file_context.h 3.5 KB

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