file_context.h 3.6 KB

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